Cosmetics
[llpp.git] / main.ml
blob9083d34d73d8063a1cde88683c64f86bc3c5685a
1 open Utils;;
2 open Config;;
3 open Glutils;;
4 open Listview;;
5 open Link;;
7 let selfexec = ref E.s;;
8 let ignoredoctitlte = ref false;;
9 let layouth = ref ~-1;;
10 let checkerstexid = ref None;;
12 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
13 dolog {|rect {
14 x0,y0=(% f, % f)
15 x1,y1=(% f, % f)
16 x2,y2=(% f, % f)
17 x3,y3=(% f, % f)
18 }|} x0 y0 x1 y1 x2 y2 x3 y3;
21 let pgscale h = truncate (float h *. conf.pgscale);;
23 let hscrollh () =
24 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
25 && (state.w > state.winw))
26 then conf.scrollbw
27 else 0
30 let setfontsize n =
31 fstate.fontsize <- n;
32 fstate.wwidth <- measurestr fstate.fontsize "w";
33 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
36 let vlog fmt =
37 if conf.verbose
38 then dolog fmt
39 else Printf.kprintf ignore fmt
42 let launchpath () =
43 if emptystr conf.pathlauncher
44 then dolog "%s" state.path
45 else (
46 let command =
47 Str.global_replace Utils.Re.percent state.path conf.pathlauncher in
48 match spawn command [] with
49 | _pid -> ()
50 | exception exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
54 let getopaque pageno =
55 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
56 with Not_found -> None
59 let pagetranslatepoint l x y =
60 let dy = y - l.pagedispy in
61 let y = dy + l.pagey in
62 let dx = x - l.pagedispx in
63 let x = dx + l.pagex in
64 (x, y);
67 let onppundermouse g x y d =
68 let rec f = function
69 | l :: rest ->
70 begin match getopaque l.pageno with
71 | Some opaque ->
72 let x0 = l.pagedispx in
73 let x1 = x0 + l.pagevw in
74 let y0 = l.pagedispy in
75 let y1 = y0 + l.pagevh in
76 if y >= y0 && y <= y1 && x >= x0 && x <= x1
77 then
78 let px, py = pagetranslatepoint l x y in
79 match g opaque l px py with
80 | Some res -> res
81 | None -> f rest
82 else f rest
83 | _ -> f rest
84 end
85 | [] -> d
87 f state.layout
90 let getunder x y =
91 let g opaque l px py =
92 if state.bzoom
93 then (
94 match rectofblock opaque px py with
95 | Some [|x0;x1;y0;y1|] ->
96 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
97 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
98 state.rects <- [l.pageno, color, rect];
99 postRedisplay "getunder";
100 | _ -> ()
102 let under = whatsunder opaque px py in
103 if under = Unone then None else Some under
105 onppundermouse g x y Unone
108 let unproject x y =
109 let g opaque l x y =
110 match unproject opaque x y with
111 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
112 | None -> None
114 onppundermouse g x y None;
117 let showtext c s =
118 state.text <- Printf.sprintf "%c%s" c s;
119 postRedisplay "showtext";
122 let impmsg fmt = Format.ksprintf (fun s -> showtext '!' s) fmt;;
124 let pipesel opaque cmd =
125 if hassel opaque
126 then pipef ~closew:false "pipesel"
127 (fun w ->
128 copysel w opaque;
129 postRedisplay "pipesel"
130 ) cmd
133 let paxunder x y =
134 let g opaque l px py =
135 if markunder opaque px py conf.paxmark
136 then
137 Some (fun () ->
138 match getopaque l.pageno with
139 | None -> ()
140 | Some opaque -> pipesel opaque conf.paxcmd
142 else None
144 postRedisplay "paxunder";
145 if conf.paxmark = Mark_page
146 then
147 List.iter (fun l ->
148 match getopaque l.pageno with
149 | None -> ()
150 | Some opaque -> clearmark opaque) state.layout;
151 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
154 let undertext = function
155 | Unone -> "none"
156 | Ulinkuri s -> s
157 | Utext s -> "font: " ^ s
158 | Uannotation (opaque, slinkindex) ->
159 "annotation: " ^ getannotcontents opaque slinkindex
162 let updateunder x y =
163 match getunder x y with
164 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
165 | Ulinkuri uri ->
166 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
167 Wsi.setcursor Wsi.CURSOR_INFO
168 | Utext s ->
169 if conf.underinfo then showtext 'f' ("ont: " ^ s);
170 Wsi.setcursor Wsi.CURSOR_TEXT
171 | Uannotation _ ->
172 if conf.underinfo then showtext 'a' "nnotation";
173 Wsi.setcursor Wsi.CURSOR_INFO
176 let showlinktype under =
177 if conf.underinfo && under != Unone
178 then showtext ' ' @@ undertext under
181 let intentry_with_suffix text key =
182 let text =
183 match [@warning "-4"] key with
184 | Keys.Ascii ('0'..'9' as c) -> addchar text c
185 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
186 addchar text @@ asciilower c
187 | _ ->
188 state.text <- "invalid key";
189 text
191 TEcont text
194 let wcmd fmt =
195 let b = Buffer.create 16 in
196 Printf.kbprintf
197 (fun b ->
198 let b = Buffer.to_bytes b in
199 wcmd state.ss b @@ Bytes.length b
200 ) b fmt
203 let nogeomcmds = function
204 | s, [] -> emptystr s
205 | _ -> false
208 let layoutN ((columns, coverA, coverB), b) x y sw sh =
209 let rec fold accu n =
210 if n = Array.length b
211 then accu
212 else
213 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
214 if (vy - y) > sh &&
215 (n = coverA - 1
216 || n = state.pagecount - coverB
217 || (n - coverA) mod columns = columns - 1)
218 then accu
219 else
220 let accu =
221 if vy + h > y
222 then
223 let pagey = max 0 (y - vy) in
224 let pagedispy = if pagey > 0 then 0 else vy - y in
225 let pagedispx, pagex =
226 let pdx =
227 if n = coverA - 1 || n = state.pagecount - coverB
228 then x + (sw - w) / 2
229 else dx + xoff + x
231 if pdx < 0
232 then 0, -pdx
233 else pdx, 0
235 let pagevw =
236 let vw = sw - pagedispx in
237 let pw = w - pagex in
238 min vw pw
240 let pagevh = min (h - pagey) (sh - pagedispy) in
241 if pagevw > 0 && pagevh > 0
242 then
243 let e =
244 { pageno = n
245 ; pagedimno = pdimno
246 ; pagew = w
247 ; pageh = h
248 ; pagex = pagex
249 ; pagey = pagey
250 ; pagevw = pagevw
251 ; pagevh = pagevh
252 ; pagedispx = pagedispx
253 ; pagedispy = pagedispy
254 ; pagecol = 0
257 e :: accu
258 else accu
259 else accu
261 fold accu (n+1)
263 if Array.length b = 0
264 then []
265 else List.rev (fold [] (page_of_y y))
268 let layoutS (columns, b) x y sw sh =
269 let rec fold accu n =
270 if n = Array.length b
271 then accu
272 else
273 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
274 if (vy - y) > sh
275 then accu
276 else
277 let accu =
278 if vy + pageh > y
279 then
280 let x = xoff + x in
281 let pagey = max 0 (y - vy) in
282 let pagedispy = if pagey > 0 then 0 else vy - y in
283 let pagedispx, pagex =
284 if px = 0
285 then (
286 if x < 0
287 then 0, -x
288 else x, 0
290 else (
291 let px = px - x in
292 if px < 0
293 then -px, 0
294 else 0, px
297 let pagecolw = pagew/columns in
298 let pagedispx =
299 if pagecolw < sw
300 then pagedispx + ((sw - pagecolw) / 2)
301 else pagedispx
303 let pagevw =
304 let vw = sw - pagedispx in
305 let pw = pagew - pagex in
306 min vw pw
308 let pagevw = min pagevw pagecolw in
309 let pagevh = min (pageh - pagey) (sh - pagedispy) in
310 if pagevw > 0 && pagevh > 0
311 then
312 let e =
313 { pageno = n/columns
314 ; pagedimno = pdimno
315 ; pagew = pagew
316 ; pageh = pageh
317 ; pagex = pagex
318 ; pagey = pagey
319 ; pagevw = pagevw
320 ; pagevh = pagevh
321 ; pagedispx = pagedispx
322 ; pagedispy = pagedispy
323 ; pagecol = n mod columns
326 e :: accu
327 else accu
328 else accu
330 fold accu (n+1)
332 List.rev (fold [] 0)
335 let layout x y sw sh =
336 if nogeomcmds state.geomcmds
337 then
338 match conf.columns with
339 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
340 | Cmulti c -> layoutN c x y sw sh
341 | Csplit s -> layoutS s x y sw sh
342 else []
345 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
346 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
348 let itertiles l f =
349 let tilex = l.pagex mod conf.tilew in
350 let tiley = l.pagey mod conf.tileh in
352 let col = l.pagex / conf.tilew in
353 let row = l.pagey / conf.tileh in
355 let rec rowloop row y0 dispy h =
356 if h != 0
357 then
358 let dh = conf.tileh - y0 in
359 let dh = min h dh in
360 let rec colloop col x0 dispx w =
361 if w != 0
362 then
363 let dw = conf.tilew - x0 in
364 let dw = min w dw in
365 f col row dispx dispy x0 y0 dw dh;
366 colloop (col+1) 0 (dispx+dw) (w-dw)
368 colloop col tilex l.pagedispx l.pagevw;
369 rowloop (row+1) 0 (dispy+dh) (h-dh)
371 if l.pagevw > 0 && l.pagevh > 0
372 then rowloop row tiley l.pagedispy l.pagevh;
375 let gettileopaque l col row =
376 let key = l.pageno, state.gen, conf.colorspace,
377 conf.angle, l.pagew, l.pageh, col, row in
378 try Some (Hashtbl.find state.tilemap key)
379 with Not_found -> None
382 let puttileopaque l col row gen colorspace angle opaque size elapsed =
383 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
384 Hashtbl.add state.tilemap key (opaque, size, elapsed)
387 let drawtiles l color =
388 GlDraw.color color;
389 begintiles ();
390 let f col row x y tilex tiley w h =
391 match gettileopaque l col row with
392 | Some (opaque, _, t) ->
393 let params = x, y, w, h, tilex, tiley in
394 if conf.invert
395 then GlTex.env (`mode `blend);
396 drawtile params opaque;
397 if conf.invert
398 then GlTex.env (`mode `modulate);
399 if conf.debug
400 then (
401 endtiles ();
402 let s = Printf.sprintf "%d[%d,%d] %f sec" l.pageno col row t in
403 let w = measurestr fstate.fontsize s in
404 GlDraw.color (0.0, 0.0, 0.0);
405 filledrect
406 (float (x-2))
407 (float (y-2))
408 (float (x+2) +. w)
409 (float (y + fstate.fontsize + 2));
410 GlDraw.color color;
411 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
412 begintiles ();
415 | None ->
416 endtiles ();
417 let w = let lw = state.winw - x in min lw w
418 and h = let lh = state.winh - y in min lh h
420 if conf.invert
421 then GlTex.env (`mode `blend);
422 begin match !checkerstexid with
423 | Some id ->
424 Gl.enable `texture_2d;
425 GlTex.bind_texture ~target:`texture_2d id;
426 let x0 = float x
427 and y0 = float y
428 and x1 = float (x+w)
429 and y1 = float (y+h) in
431 let tw = float w /. 16.0
432 and th = float h /. 16.0 in
433 let tx0 = float tilex /. 16.0
434 and ty0 = float tiley /. 16.0 in
435 let tx1 = tx0 +. tw
436 and ty1 = ty0 +. th in
437 Raw.sets_float Glutils.vraw ~pos:0
438 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
439 Raw.sets_float Glutils.traw ~pos:0
440 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
441 GlArray.vertex `two Glutils.vraw;
442 GlArray.tex_coord `two Glutils.traw;
443 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
444 Gl.disable `texture_2d;
446 | None ->
447 GlDraw.color (1.0, 1.0, 1.0);
448 filledrect (float x) (float y) (float (x+w)) (float (y+h));
449 end;
450 if conf.invert
451 then GlTex.env (`mode `modulate);
452 if w > 128 && h > fstate.fontsize + 10
453 then (
454 let c = if conf.invert then 1.0 else 0.0 in
455 GlDraw.color (c, c, c);
456 let c, r =
457 if conf.verbose
458 then (col*conf.tilew, row*conf.tileh)
459 else col, row
461 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
463 GlDraw.color color;
464 begintiles ();
466 itertiles l f;
467 endtiles ();
470 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
472 let tilevisible1 l x y =
473 let ax0 = l.pagex
474 and ax1 = l.pagex + l.pagevw
475 and ay0 = l.pagey
476 and ay1 = l.pagey + l.pagevh in
478 let bx0 = x
479 and by0 = y in
480 let bx1 = min (bx0 + conf.tilew) l.pagew
481 and by1 = min (by0 + conf.tileh) l.pageh in
483 let rx0 = max ax0 bx0
484 and ry0 = max ay0 by0
485 and rx1 = min ax1 bx1
486 and ry1 = min ay1 by1 in
488 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
489 nonemptyintersection
492 let tilevisible layout n x y =
493 let rec findpageinlayout m = function
494 | l :: rest when l.pageno = n ->
495 tilevisible1 l x y || (
496 match conf.columns with
497 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
498 | Csplit _ | Csingle _ | Cmulti _ -> false
500 | _ :: rest -> findpageinlayout 0 rest
501 | [] -> false
503 findpageinlayout 0 layout;
506 let tileready l x y =
507 tilevisible1 l x y &&
508 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
511 let tilepage n p layout =
512 let rec loop = function
513 | l :: rest ->
514 if l.pageno = n
515 then
516 let f col row _ _ _ _ _ _ =
517 if state.currently = Idle
518 then
519 match gettileopaque l col row with
520 | Some _ -> ()
521 | None ->
522 let x = col*conf.tilew
523 and y = row*conf.tileh in
524 let w =
525 let w = l.pagew - x in
526 min w conf.tilew
528 let h =
529 let h = l.pageh - y in
530 min h conf.tileh
532 let pbo =
533 if conf.usepbo
534 then getpbo w h conf.colorspace
535 else ~< "0"
537 wcmd "tile %s %d %d %d %d %s" (~> p) x y w h (~> pbo);
538 state.currently <-
539 Tiling (
540 l, p, conf.colorspace, conf.angle,
541 state.gen, col, row, conf.tilew, conf.tileh
544 itertiles l f;
545 else
546 loop rest
548 | [] -> ()
550 if nogeomcmds state.geomcmds
551 then loop layout;
554 let preloadlayout x y sw sh =
555 let y = if y < sh then 0 else y - sh in
556 let x = min 0 (x + sw) in
557 let h = sh*3 in
558 let w = sw*3 in
559 layout x y w h;
562 let load pages =
563 let rec loop pages =
564 if state.currently = Idle
565 then
566 match pages with
567 | l :: rest ->
568 begin match getopaque l.pageno with
569 | None ->
570 wcmd "page %d %d" l.pageno l.pagedimno;
571 state.currently <- Loading (l, state.gen);
572 | Some opaque ->
573 tilepage l.pageno opaque pages;
574 loop rest
575 end;
576 | _ -> ()
578 if nogeomcmds state.geomcmds
579 then loop pages
582 let preload pages =
583 load pages;
584 if conf.preload && state.currently = Idle
585 then load (preloadlayout state.x state.y state.winw state.winh);
588 let layoutready layout =
589 let rec fold all ls =
590 all && match ls with
591 | l :: rest ->
592 let seen = ref false in
593 let allvisible = ref true in
594 let foo col row _ _ _ _ _ _ =
595 seen := true;
596 allvisible := !allvisible &&
597 begin match gettileopaque l col row with
598 | Some _ -> true
599 | None -> false
602 itertiles l foo;
603 fold (!seen && !allvisible) rest
604 | [] -> true
606 let alltilesvisible = fold true layout in
607 alltilesvisible;
610 let gotoxy x y =
611 let y = bound y 0 state.maxy in
612 let y, layout =
613 let layout = layout x y state.winw state.winh in
614 postRedisplay "gotoxy ready";
615 y, layout
617 state.x <- x;
618 state.y <- y;
619 state.layout <- layout;
620 begin match state.mode with
621 | LinkNav ln ->
622 begin match ln with
623 | Ltexact (pageno, linkno) ->
624 let rec loop = function
625 | [] ->
626 state.lnava <- Some (pageno, linkno);
627 state.mode <- LinkNav (Ltgendir 0)
628 | l :: _ when l.pageno = pageno ->
629 begin match getopaque pageno with
630 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
631 | Some opaque ->
632 let x0, y0, x1, y1 = getlinkrect opaque linkno in
633 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
634 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
635 then state.mode <- LinkNav (Ltgendir 0)
637 | _ :: rest -> loop rest
639 loop layout
640 | Ltnotready _ | Ltgendir _ -> ()
642 | Birdseye _ | Textentry _ | View -> ()
643 end;
644 begin match state.mode with
645 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
646 if not (pagevisible layout pageno)
647 then (
648 match state.layout with
649 | [] -> ()
650 | l :: _ ->
651 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
653 | LinkNav lt ->
654 begin match lt with
655 | Ltnotready (_, dir)
656 | Ltgendir dir ->
657 let linknav =
658 let rec loop = function
659 | [] -> lt
660 | l :: rest ->
661 match getopaque l.pageno with
662 | None -> Ltnotready (l.pageno, dir)
663 | Some opaque ->
664 let link =
665 let ld =
666 if dir = 0
667 then LDfirstvisible (l.pagex, l.pagey, dir)
668 else (
669 if dir > 0 then LDfirst else LDlast
672 findlink opaque ld
674 match link with
675 | Lnotfound -> loop rest
676 | Lfound n ->
677 showlinktype (getlink opaque n);
678 Ltexact (l.pageno, n)
680 loop state.layout
682 state.mode <- LinkNav linknav
683 | Ltexact _ -> ()
685 | Textentry _ | View -> ()
686 end;
687 preload layout;
688 if conf.updatecurs
689 then (
690 let mx, my = state.mpos in
691 updateunder mx my;
695 let conttiling pageno opaque =
696 tilepage pageno opaque
697 (if conf.preload
698 then preloadlayout state.x state.y state.winw state.winh
699 else state.layout)
702 let gotoxy x y =
703 if not conf.verbose then state.text <- E.s;
704 gotoxy x y;
707 let getanchory (n, top, dtop) =
708 let y, h = getpageyh n in
709 if conf.presentation
710 then
711 let ips = calcips h in
712 y + truncate (top*.float h -. dtop*.float ips) + ips;
713 else y + truncate (top*.float h -. dtop*.float conf.interpagespace)
716 let gotoanchor anchor = gotoxy state.x (getanchory anchor);;
717 let addnav () = getanchor () |> cbput state.hists.nav;;
718 let addnavnorc () = getanchor () |> cbput_dont_update_rc state.hists.nav;;
720 let getnav dir =
721 let anchor = cbgetc state.hists.nav dir in
722 getanchory anchor;
725 let gotopage n top =
726 let y, h = getpageyh n in
727 let y = y + (truncate (top *. float h)) in
728 gotoxy state.x y
731 let gotopage1 n top =
732 let y = getpagey n in
733 let y = y + top in
734 gotoxy state.x y
737 let invalidate s f =
738 Glutils.redisplay := false;
739 state.layout <- [];
740 state.pdims <- [];
741 state.rects <- [];
742 state.rects1 <- [];
743 match state.geomcmds with
744 | ps, [] when emptystr ps ->
745 f ();
746 state.geomcmds <- s, [];
747 | ps, [] -> state.geomcmds <- ps, [s, f];
748 | ps, (s', _) :: rest when s' = s -> state.geomcmds <- ps, ((s, f) :: rest);
749 | ps, cmds -> state.geomcmds <- ps, ((s, f) :: cmds);
752 let flushpages () =
753 Hashtbl.iter (fun _ opaque -> wcmd "freepage %s" (~> opaque)) state.pagemap;
754 Hashtbl.clear state.pagemap;
757 let flushtiles () =
758 if not (Queue.is_empty state.tilelru)
759 then (
760 Queue.iter (fun (k, p, s) ->
761 wcmd "freetile %s" (~> p);
762 state.memused <- state.memused - s;
763 Hashtbl.remove state.tilemap k;
764 ) state.tilelru;
765 state.uioh#infochanged Memused;
766 Queue.clear state.tilelru;
768 load state.layout;
771 let stateh h =
772 let h = truncate (float h*.conf.zoom) in
773 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
774 h - d
777 let fillhelp () =
778 state.help <-
779 let sl = keystostrlist conf in
780 let rec loop accu =
781 function | [] -> accu
782 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
783 in Help.makehelp conf.urilauncher
784 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
787 let opendoc path password =
788 state.path <- path;
789 state.password <- password;
790 state.gen <- state.gen + 1;
791 state.docinfo <- [];
792 state.outlines <- [||];
794 flushpages ();
795 setaalevel conf.aalevel;
796 let titlepath =
797 if emptystr state.origin
798 then path
799 else state.origin
801 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
802 wcmd "open %d %d %s\000%s\000%s\000"
803 (btod conf.usedoccss) !layouth
804 path password conf.css;
805 invalidate "reqlayout"
806 (fun () ->
807 wcmd "reqlayout %d %d %d %s\000"
808 conf.angle (FMTE.to_int conf.fitmodel)
809 (stateh state.winh) state.nameddest
811 fillhelp ();
814 let reload () =
815 state.anchor <- getanchor ();
816 opendoc state.path state.password;
819 let scalecolor c =
820 let c = c *. conf.colorscale in
821 (c, c, c);
824 let scalecolor2 (r, g, b) =
825 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
828 let docolumns columns =
829 match columns with
830 | Csingle _ ->
831 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
832 let rec loop pageno pdimno pdim y ph pdims =
833 if pageno != state.pagecount
834 then
835 let pdimno, ((_, w, h, xoff) as pdim), pdims =
836 match pdims with
837 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
838 pdimno+1, pdim, rest
839 | _ ->
840 pdimno, pdim, pdims
842 let x = max 0 (((state.winw - w) / 2) - xoff) in
843 let y =
844 y + (if conf.presentation
845 then (if pageno = 0 then calcips h else calcips ph + calcips h)
846 else (if pageno = 0 then 0 else conf.interpagespace))
848 a.(pageno) <- (pdimno, x, y, pdim);
849 loop (pageno+1) pdimno pdim (y + h) h pdims
851 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
852 conf.columns <- Csingle a;
854 | Cmulti ((columns, coverA, coverB), _) ->
855 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
856 let rec loop pageno pdimno pdim x y rowh pdims =
857 let rec fixrow m =
858 if m = pageno then () else
859 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
860 if h < rowh
861 then (
862 let y = y + (rowh - h) / 2 in
863 a.(m) <- (pdimno, x, y, pdim);
865 fixrow (m+1)
867 if pageno = state.pagecount
868 then fixrow (((pageno - 1) / columns) * columns)
869 else
870 let pdimno, ((_, w, h, xoff) as pdim), pdims =
871 match pdims with
872 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
873 pdimno+1, pdim, rest
874 | _ -> pdimno, pdim, pdims
876 let x, y, rowh' =
877 if pageno = coverA - 1 || pageno = state.pagecount - coverB
878 then (
879 let x = (state.winw - w) / 2 in
880 let ips =
881 if conf.presentation then calcips h else conf.interpagespace in
882 x, y + ips + rowh, h
884 else (
885 if (pageno - coverA) mod columns = 0
886 then (
887 let x = max 0 (state.winw - state.w) / 2 in
888 let y =
889 if conf.presentation
890 then
891 let ips = calcips h in
892 y + (if pageno = 0 then 0 else calcips rowh + ips)
893 else
894 y + (if pageno = 0 then 0 else conf.interpagespace)
896 x, y + rowh, h
898 else x, y, max rowh h
901 let y =
902 if pageno > 1 && (pageno - coverA) mod columns = 0
903 then (
904 let y =
905 if pageno = columns && conf.presentation
906 then (
907 let ips = calcips rowh in
908 for i = 0 to pred columns
910 let (pdimno, x, y, pdim) = a.(i) in
911 a.(i) <- (pdimno, x, y+ips, pdim)
912 done;
913 y+ips;
915 else y
917 fixrow (pageno - columns);
920 else y
922 a.(pageno) <- (pdimno, x, y, pdim);
923 let x = x + w + xoff*2 + conf.interpagespace in
924 loop (pageno+1) pdimno pdim x y rowh' pdims
926 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
927 conf.columns <- Cmulti ((columns, coverA, coverB), a);
929 | Csplit (c, _) ->
930 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
931 let rec loop pageno pdimno pdim y pdims =
932 if pageno != state.pagecount
933 then
934 let pdimno, ((_, w, h, _) as pdim), pdims =
935 match pdims with
936 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
937 pdimno+1, pdim, rest
938 | _ -> pdimno, pdim, pdims
940 let cw = w / c in
941 let rec loop1 n x y =
942 if n = c then y else (
943 a.(pageno*c + n) <- (pdimno, x, y, pdim);
944 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
947 let y = loop1 0 0 y in
948 loop (pageno+1) pdimno pdim y pdims
950 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
951 conf.columns <- Csplit (c, a);
954 let represent () =
955 docolumns conf.columns;
956 state.maxy <- calcheight ();
957 if state.reprf == noreprf
958 then (
959 match state.mode with
960 | Birdseye (_, _, pageno, _, _) ->
961 let y, h = getpageyh pageno in
962 let top = (state.winh - h) / 2 in
963 gotoxy state.x (max 0 (y - top))
964 | Textentry _ | View | LinkNav _ ->
965 let y = getanchory state.anchor in
966 let y = min y (state.maxy - state.winh) in
967 gotoxy state.x y;
969 else (
970 state.reprf ();
971 state.reprf <- noreprf;
975 let reshape ?(firsttime=false) w h =
976 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
977 if not firsttime && nogeomcmds state.geomcmds
978 then state.anchor <- getanchor ();
980 state.winw <- w;
981 let w = truncate (float w *. conf.zoom) in
982 let w = max w 2 in
983 state.winh <- h;
984 setfontsize fstate.fontsize;
985 GlMat.mode `modelview;
986 GlMat.load_identity ();
988 GlMat.mode `projection;
989 GlMat.load_identity ();
990 GlMat.rotate ~x:1.0 ~angle:180.0 ();
991 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
992 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
994 let relx =
995 if conf.zoom <= 1.0
996 then 0.0
997 else float state.x /. float state.w
999 invalidate "geometry"
1000 (fun () ->
1001 state.w <- w;
1002 if not firsttime
1003 then state.x <- truncate (relx *. float w);
1004 let w =
1005 match conf.columns with
1006 | Csingle _ -> w
1007 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1008 | Csplit (c, _) -> w * c
1010 wcmd "geometry %d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
1014 let gctiles () =
1015 let len = Queue.length state.tilelru in
1016 let layout = lazy (if conf.preload
1017 then preloadlayout state.x state.y state.winw state.winh
1018 else state.layout) in
1019 let rec loop qpos =
1020 if state.memused > conf.memlimit
1021 then (
1022 if qpos < len
1023 then
1024 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1025 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1026 let (_, pw, ph, _) = getpagedim n in
1027 if gen = state.gen
1028 && colorspace = conf.colorspace
1029 && angle = conf.angle
1030 && pagew = pw
1031 && pageh = ph
1032 && (
1033 let x = col*conf.tilew and y = row*conf.tileh in
1034 tilevisible (Lazy.force_val layout) n x y
1036 then Queue.push lruitem state.tilelru
1037 else (
1038 freepbo p;
1039 wcmd "freetile %s" (~> p);
1040 state.memused <- state.memused - s;
1041 state.uioh#infochanged Memused;
1042 Hashtbl.remove state.tilemap k;
1044 loop (qpos+1)
1047 loop 0
1050 let onpagerect pageno f =
1051 let b =
1052 match conf.columns with
1053 | Cmulti (_, b) -> b
1054 | Csingle b -> b
1055 | Csplit (_, b) -> b
1057 if pageno >= 0 && pageno < Array.length b
1058 then
1059 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1060 f w h
1063 let gotopagexy1 pageno x y =
1064 let _,w1,h1,leftx = getpagedim pageno in
1065 let top = y /. (float h1) in
1066 let left = x /. (float w1) in
1067 let py, w, h = getpageywh pageno in
1068 let wh = state.winh in
1069 let x = left *. (float w) in
1070 let x = leftx + state.x + truncate x in
1071 let sx =
1072 if x < 0 || x >= state.winw
1073 then state.x - x
1074 else state.x
1076 let pdy = truncate (top *. float h) in
1077 let y' = py + pdy in
1078 let dy = y' - state.y in
1079 let sy =
1080 if x != state.x || not (dy > 0 && dy < wh)
1081 then (
1082 if conf.presentation
1083 then
1084 if abs (py - y') > wh
1085 then y'
1086 else py
1087 else y';
1089 else state.y
1091 if state.x != sx || state.y != sy
1092 then gotoxy sx sy
1093 else gotoxy state.x state.y;
1096 let gotopagexy pageno x y =
1097 match state.mode with
1098 | Birdseye _ -> gotopage pageno 0.0
1099 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1102 let getpassword () =
1103 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1104 if emptystr passcmd
1105 then E.s
1106 else getcmdoutput
1107 (fun s ->
1108 impmsg "error getting password: %s" s;
1109 dolog "%s" s) passcmd;
1112 let pgoto opaque pageno x y =
1113 let pdimno = getpdimno pageno in
1114 let x, y = project opaque pageno pdimno x y in
1115 gotopagexy pageno x y;
1118 let act cmds =
1119 (* dolog "%S" cmds; *)
1120 let spl = splitatchar cmds ' ' in
1121 let scan s fmt f =
1122 try Scanf.sscanf s fmt f
1123 with exn ->
1124 dolog "error processing '%S': %s" cmds @@ exntos exn;
1125 exit 1
1127 let addoutline outline =
1128 match state.currently with
1129 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1130 | Idle -> state.currently <- Outlining [outline]
1131 | Loading _ | Tiling _ ->
1132 dolog "invalid outlining state";
1133 logcurrently state.currently
1135 match spl with
1136 | "clear", "" ->
1137 state.pdims <- [];
1138 state.uioh#infochanged Pdim;
1140 | "clearrects", "" ->
1141 state.rects <- state.rects1;
1142 postRedisplay "clearrects";
1144 | "continue", args ->
1145 let n = scan args "%u" (fun n -> n) in
1146 state.pagecount <- n;
1147 begin match state.currently with
1148 | Outlining l ->
1149 state.currently <- Idle;
1150 state.outlines <- Array.of_list (List.rev l)
1151 | Idle | Loading _ | Tiling _ -> ()
1152 end;
1154 let cur, cmds = state.geomcmds in
1155 if emptystr cur
1156 then error "empty geomcmd";
1158 begin match List.rev cmds with
1159 | [] ->
1160 state.geomcmds <- E.s, [];
1161 represent ();
1162 | (s, f) :: rest ->
1163 f ();
1164 state.geomcmds <- s, List.rev rest;
1165 end;
1166 postRedisplay "continue";
1168 | "msg", args ->
1169 showtext ' ' args
1171 | "vmsg", args ->
1172 if conf.verbose
1173 then showtext ' ' args
1175 | "emsg", args ->
1176 Buffer.add_string state.errmsgs args;
1177 state.newerrmsgs <- true;
1178 postRedisplay "error message"
1180 | "progress", args ->
1181 let progress, text =
1182 scan args "%f %n"
1183 (fun f pos ->
1184 f, String.sub args pos (String.length args - pos))
1186 state.text <- text;
1187 state.progress <- progress;
1188 postRedisplay "progress"
1190 | "firstmatch", args ->
1191 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1192 scan args "%u %d %f %f %f %f %f %f %f %f"
1193 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1194 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1196 let y = (getpagey pageno) + truncate y0 in
1197 let x =
1198 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1199 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1200 else state.x
1202 addnav ();
1203 gotoxy x y;
1204 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1205 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1207 | "match", args ->
1208 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1209 scan args "%u %d %f %f %f %f %f %f %f %f"
1210 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1211 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1213 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1214 state.rects1 <-
1215 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1217 | "page", args ->
1218 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1219 let pageopaque = ~< pageopaques in
1220 begin match state.currently with
1221 | Loading (l, gen) ->
1222 vlog "page %d took %f sec" l.pageno t;
1223 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1224 let preloadedpages =
1225 if conf.preload
1226 then preloadlayout state.x state.y state.winw state.winh
1227 else state.layout
1229 let evict () =
1230 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1231 IntSet.empty preloadedpages
1233 let evictedpages =
1234 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1235 if not (IntSet.mem pageno set)
1236 then (
1237 wcmd "freepage %s" (~> opaque);
1238 key :: accu
1240 else accu
1241 ) state.pagemap []
1243 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1245 evict ();
1246 state.currently <- Idle;
1247 if gen = state.gen
1248 then (
1249 tilepage l.pageno pageopaque state.layout;
1250 load state.layout;
1251 load preloadedpages;
1252 let visible = pagevisible state.layout l.pageno in
1253 if visible
1254 then (
1255 match state.mode with
1256 | LinkNav (Ltnotready (pageno, dir)) ->
1257 if pageno = l.pageno
1258 then (
1259 let link =
1260 let ld =
1261 if dir = 0
1262 then LDfirstvisible (l.pagex, l.pagey, dir)
1263 else (
1264 if dir > 0 then LDfirst else LDlast
1267 findlink pageopaque ld
1269 match link with
1270 | Lnotfound -> ()
1271 | Lfound n ->
1272 showlinktype (getlink pageopaque n);
1273 state.mode <- LinkNav (Ltexact (l.pageno, n))
1275 | LinkNav (Ltgendir _)
1276 | LinkNav (Ltexact _)
1277 | View
1278 | Birdseye _
1279 | Textentry _ -> ()
1282 if visible && layoutready state.layout
1283 then (
1284 postRedisplay "page";
1288 | Idle | Tiling _ | Outlining _ ->
1289 dolog "Inconsistent loading state";
1290 logcurrently state.currently;
1291 exit 1
1294 | "tile" , args ->
1295 let (x, y, opaques, size, t) =
1296 scan args "%u %u %s %u %f"
1297 (fun x y p size t -> (x, y, p, size, t))
1299 let opaque = ~< opaques in
1300 begin match state.currently with
1301 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1302 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1304 unmappbo opaque;
1305 if tilew != conf.tilew || tileh != conf.tileh
1306 then (
1307 wcmd "freetile %s" (~> opaque);
1308 state.currently <- Idle;
1309 load state.layout;
1311 else (
1312 puttileopaque l col row gen cs angle opaque size t;
1313 state.memused <- state.memused + size;
1314 state.uioh#infochanged Memused;
1315 gctiles ();
1316 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1317 opaque, size) state.tilelru;
1319 state.currently <- Idle;
1320 if gen = state.gen
1321 && conf.colorspace = cs
1322 && conf.angle = angle
1323 && tilevisible state.layout l.pageno x y
1324 then conttiling l.pageno pageopaque;
1326 preload state.layout;
1327 if gen = state.gen
1328 && conf.colorspace = cs
1329 && conf.angle = angle
1330 && tilevisible state.layout l.pageno x y
1331 && layoutready state.layout
1332 then postRedisplay "tile nothrottle";
1335 | Idle | Loading _ | Outlining _ ->
1336 dolog "Inconsistent tiling state";
1337 logcurrently state.currently;
1338 exit 1
1341 | "pdim", args ->
1342 let (n, w, h, _) as pdim =
1343 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1345 let pdim =
1346 match conf.fitmodel with
1347 | FitWidth -> pdim
1348 | FitPage | FitProportional ->
1349 match conf.columns with
1350 | Csplit _ -> (n, w, h, 0)
1351 | Csingle _ | Cmulti _ -> pdim
1353 state.pdims <- pdim :: state.pdims;
1354 state.uioh#infochanged Pdim
1356 | "o", args ->
1357 let (l, n, t, h, pos) =
1358 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1360 let s = String.sub args pos (String.length args - pos) in
1361 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1363 | "ou", args ->
1364 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1365 let s = String.sub args pos len in
1366 let pos2 = pos + len + 1 in
1367 let uri = String.sub args pos2 (String.length args - pos2) in
1368 addoutline (s, l, Ouri uri)
1370 | "on", args ->
1371 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1372 let s = String.sub args pos (String.length args - pos) in
1373 addoutline (s, l, Onone)
1375 | "a", args ->
1376 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1377 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1379 | "info", args ->
1380 let c, v = splitatchar args '\t' in
1381 let s =
1382 if nonemptystr v
1383 then
1384 if c = "Title"
1385 then (
1386 conf.title <- v;
1387 if not !ignoredoctitlte
1388 then Wsi.settitle v;
1389 args
1391 else
1392 if let len = String.length c in
1393 len > 6 && ((String.sub c (len-4) 4) = "date")
1394 then (
1395 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1396 then
1397 let b = Buffer.create 10 in
1398 Printf.bprintf b "%s\t" c;
1399 let sub p l c =
1401 Buffer.add_substring b v p l;
1402 Buffer.add_char b c;
1403 with exn -> Buffer.add_string b @@ exntos exn
1405 sub 2 4 '/';
1406 sub 6 2 '/';
1407 sub 8 2 ' ';
1408 sub 10 2 ':';
1409 sub 12 2 ':';
1410 sub 14 2 ' ';
1411 Buffer.add_char b '[';
1412 Buffer.add_string b v;
1413 Buffer.add_char b ']';
1414 Buffer.contents b
1415 else args
1417 else args
1418 else args
1420 state.docinfo <- (1, s) :: state.docinfo
1422 | "infoend", "" ->
1423 state.docinfo <- List.rev state.docinfo;
1424 state.uioh#infochanged Docinfo
1426 | "pass", args ->
1427 if args = "fail"
1428 then Wsi.settitle "Wrong password";
1429 let password = getpassword () in
1430 if emptystr password
1431 then error "document is password protected"
1432 else opendoc state.path password
1434 | _ -> error "unknown cmd `%S'" cmds
1437 let onhist cb =
1438 let rc = cb.rc in
1439 let action = function
1440 | HCprev -> cbget cb ~-1
1441 | HCnext -> cbget cb 1
1442 | HCfirst -> cbget cb ~-(cb.rc)
1443 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1444 and cancel () = cb.rc <- rc
1445 in (action, cancel)
1448 let search pattern forward =
1449 match conf.columns with
1450 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1451 | Csingle _ | Cmulti _ ->
1452 if nonemptystr pattern
1453 then
1454 let pn, py =
1455 match state.layout with
1456 | [] -> 0, 0
1457 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1459 wcmd "search %d %d %d %d,%s\000"
1460 (btod conf.icase) pn py (btod forward) pattern;
1463 let intentry text key =
1464 let text =
1465 if emptystr text && key = Keys.Ascii '-'
1466 then addchar text '-'
1467 else
1468 match [@warning "-4"] key with
1469 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1470 | _ ->
1471 state.text <- "invalid key";
1472 text
1474 TEcont text
1477 let linknact f s =
1478 if nonemptystr s
1479 then (
1480 let n =
1481 let l = String.length s in
1482 let rec loop pos n =
1483 if pos = l
1484 then n
1485 else
1486 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1487 loop (pos+1) (n*26 + m)
1488 in loop 0 0
1490 let rec loop n = function
1491 | [] -> ()
1492 | l :: rest ->
1493 match getopaque l.pageno with
1494 | None -> loop n rest
1495 | Some opaque ->
1496 let m = getlinkcount opaque in
1497 if n < m
1498 then (
1499 let under = getlink opaque n in
1500 f under
1502 else loop (n-m) rest
1504 loop n state.layout;
1508 let linknentry text key = match [@warning "-4"] key with
1509 | Keys.Ascii c ->
1510 let text = addchar text c in
1511 linknact (fun under -> state.text <- undertext under) text;
1512 TEcont text
1513 | _ ->
1514 state.text <- Printf.sprintf "invalid key";
1515 TEcont text
1518 let textentry text key = match [@warning "-4"] key with
1519 | Keys.Ascii c -> TEcont (addchar text c)
1520 | Keys.Code c -> TEcont (text ^ toutf8 c)
1521 | _ -> TEcont text
1524 let reqlayout angle fitmodel =
1525 if nogeomcmds state.geomcmds
1526 then state.anchor <- getanchor ();
1527 conf.angle <- angle mod 360;
1528 if conf.angle != 0
1529 then (
1530 match state.mode with
1531 | LinkNav _ -> state.mode <- View
1532 | Birdseye _ | Textentry _ | View -> ()
1534 conf.fitmodel <- fitmodel;
1535 invalidate "reqlayout"
1536 (fun () ->
1537 wcmd "reqlayout %d %d %d"
1538 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1542 let settrim trimmargins trimfuzz =
1543 if nogeomcmds state.geomcmds
1544 then state.anchor <- getanchor ();
1545 conf.trimmargins <- trimmargins;
1546 conf.trimfuzz <- trimfuzz;
1547 let x0, y0, x1, y1 = trimfuzz in
1548 invalidate "settrim"
1549 (fun () -> wcmd "settrim %d %d %d %d %d"
1550 (btod conf.trimmargins) x0 y0 x1 y1);
1551 flushpages ();
1554 let setzoom zoom =
1555 let zoom = max 0.0001 zoom in
1556 if zoom <> conf.zoom
1557 then (
1558 state.prevzoom <- (conf.zoom, state.x);
1559 conf.zoom <- zoom;
1560 reshape state.winw state.winh;
1561 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1565 let pivotzoom ?(vw=min state.w state.winw)
1566 ?(vh=min (state.maxy-state.y) state.winh)
1567 ?(x=vw/2) ?(y=vh/2) zoom =
1568 let w = float state.w /. zoom in
1569 let hw = w /. 2.0 in
1570 let ratio = float vh /. float vw in
1571 let hh = hw *. ratio in
1572 let x0 = float x -. hw
1573 and y0 = float y -. hh in
1574 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1575 setzoom zoom;
1578 let pivotzoom ?vw ?vh ?x ?y zoom =
1579 if nogeomcmds state.geomcmds
1580 then
1581 if zoom > 1.0
1582 then pivotzoom ?vw ?vh ?x ?y zoom
1583 else setzoom zoom
1586 let setcolumns mode columns coverA coverB =
1587 state.prevcolumns <- Some (conf.columns, conf.zoom);
1588 if columns < 0
1589 then (
1590 if isbirdseye mode
1591 then impmsg "split mode doesn't work in bird's eye"
1592 else (
1593 conf.columns <- Csplit (-columns, E.a);
1594 state.x <- 0;
1595 conf.zoom <- 1.0;
1598 else (
1599 if columns < 2
1600 then (
1601 conf.columns <- Csingle E.a;
1602 state.x <- 0;
1603 setzoom 1.0;
1605 else (
1606 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1607 conf.zoom <- 1.0;
1610 reshape state.winw state.winh;
1613 let resetmstate () =
1614 state.mstate <- Mnone;
1615 Wsi.setcursor Wsi.CURSOR_INHERIT;
1618 let enterbirdseye () =
1619 let zoom = float conf.thumbw /. float state.winw in
1620 let birdseyepageno =
1621 let cy = state.winh / 2 in
1622 let fold = function
1623 | [] -> 0
1624 | l :: rest ->
1625 let rec fold best = function
1626 | [] -> best.pageno
1627 | l :: rest ->
1628 let d = cy - (l.pagedispy + l.pagevh/2)
1629 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1630 if abs d < abs dbest
1631 then fold l rest
1632 else best.pageno
1633 in fold l rest
1635 fold state.layout
1637 state.mode <-
1638 Birdseye (
1639 { conf with zoom = conf.zoom },
1640 state.x, birdseyepageno, -1, getanchor ()
1642 resetmstate ();
1643 conf.zoom <- zoom;
1644 conf.presentation <- false;
1645 conf.interpagespace <- 10;
1646 conf.hlinks <- false;
1647 conf.fitmodel <- FitPage;
1648 state.x <- 0;
1649 conf.columns <- (
1650 match conf.beyecolumns with
1651 | Some c ->
1652 conf.zoom <- 1.0;
1653 Cmulti ((c, 0, 0), E.a)
1654 | None -> Csingle E.a
1656 if conf.verbose
1657 then
1658 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1659 (100.0*.zoom)
1660 else state.text <- E.s;
1661 reshape state.winw state.winh;
1664 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1665 state.mode <- View;
1666 conf.zoom <- c.zoom;
1667 conf.presentation <- c.presentation;
1668 conf.interpagespace <- c.interpagespace;
1669 conf.hlinks <- c.hlinks;
1670 conf.fitmodel <- c.fitmodel;
1671 conf.beyecolumns <- (
1672 match conf.columns with
1673 | Cmulti ((c, _, _), _) -> Some c
1674 | Csingle _ -> None
1675 | Csplit _ -> error "leaving bird's eye split mode"
1677 conf.columns <- (
1678 match c.columns with
1679 | Cmulti (c, _) -> Cmulti (c, E.a)
1680 | Csingle _ -> Csingle E.a
1681 | Csplit (c, _) -> Csplit (c, E.a)
1683 if conf.verbose
1684 then
1685 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1686 (100.0*.conf.zoom);
1687 reshape state.winw state.winh;
1688 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1689 state.x <- leftx;
1692 let togglebirdseye () =
1693 match state.mode with
1694 | Birdseye vals -> leavebirdseye vals true
1695 | View -> enterbirdseye ()
1696 | Textentry _ | LinkNav _ -> ()
1699 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1700 let pageno = max 0 (pageno - incr) in
1701 let rec loop = function
1702 | [] -> gotopage1 pageno 0
1703 | l :: _ when l.pageno = pageno ->
1704 if l.pagedispy >= 0 && l.pagey = 0
1705 then postRedisplay "upbirdseye"
1706 else gotopage1 pageno 0
1707 | _ :: rest -> loop rest
1709 loop state.layout;
1710 state.text <- E.s;
1711 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1714 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1715 let pageno = min (state.pagecount - 1) (pageno + incr) in
1716 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1717 let rec loop = function
1718 | [] ->
1719 let y, h = getpageyh pageno in
1720 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1721 gotoxy state.x (clamp dy)
1722 | l :: _ when l.pageno = pageno ->
1723 if l.pagevh != l.pageh
1724 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1725 else postRedisplay "downbirdseye"
1726 | _ :: rest -> loop rest
1728 loop state.layout;
1729 state.text <- E.s;
1732 let optentry mode _ key =
1733 let btos b = if b then "on" else "off" in
1734 match [@warning "-4"] key with
1735 | Keys.Ascii 'C' ->
1736 let ondone s =
1738 let n, a, b = multicolumns_of_string s in
1739 setcolumns mode n a b;
1740 with exn ->
1741 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1743 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1745 | Keys.Ascii 'Z' ->
1746 let ondone s =
1748 let zoom = float (int_of_string s) /. 100.0 in
1749 pivotzoom zoom
1750 with exn ->
1751 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1753 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1755 | Keys.Ascii 'i' ->
1756 conf.icase <- not conf.icase;
1757 TEdone ("case insensitive search " ^ (btos conf.icase))
1759 | Keys.Ascii 'v' ->
1760 conf.verbose <- not conf.verbose;
1761 TEdone ("verbose " ^ (btos conf.verbose))
1763 | Keys.Ascii 'd' ->
1764 conf.debug <- not conf.debug;
1765 TEdone ("debug " ^ (btos conf.debug))
1767 | Keys.Ascii 'f' ->
1768 conf.underinfo <- not conf.underinfo;
1769 TEdone ("underinfo " ^ btos conf.underinfo)
1771 | Keys.Ascii 'T' ->
1772 settrim (not conf.trimmargins) conf.trimfuzz;
1773 TEdone ("trim margins " ^ btos conf.trimmargins)
1775 | Keys.Ascii 'I' ->
1776 conf.invert <- not conf.invert;
1777 TEdone ("invert colors " ^ btos conf.invert)
1779 | Keys.Ascii 'x' ->
1780 let ondone s =
1781 cbput state.hists.sel s;
1782 conf.selcmd <- s;
1784 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1785 textentry, ondone, true)
1787 | Keys.Ascii 'M' ->
1788 if conf.pax == None
1789 then conf.pax <- Some 0.0
1790 else conf.pax <- None;
1791 TEdone ("PAX " ^ btos (conf.pax != None))
1793 | (Keys.Ascii c) ->
1794 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1795 TEstop
1797 | _ -> TEcont state.text
1800 let adderrmsg src msg =
1801 Buffer.add_string state.errmsgs msg;
1802 state.newerrmsgs <- true;
1803 postRedisplay src
1806 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1808 class outlinelistview ~zebra ~source =
1809 let settext autonarrow s =
1810 if autonarrow
1811 then
1812 let ss = source#statestr in
1813 state.text <-
1814 if emptystr ss
1815 then "[" ^ s ^ "]"
1816 else "{" ^ ss ^ "} [" ^ s ^ "]"
1817 else state.text <- s
1819 object (self)
1820 inherit listview
1821 ~zebra
1822 ~helpmode:false
1823 ~source:(source :> lvsource)
1824 ~trusted:false
1825 ~modehash:(findkeyhash conf "outline")
1826 as super
1828 val m_autonarrow = false
1830 method! key key mask =
1831 let maxrows =
1832 if emptystr state.text
1833 then fstate.maxrows
1834 else fstate.maxrows - 2
1836 let calcfirst first active =
1837 if active > first
1838 then
1839 let rows = active - first in
1840 if rows > maxrows then active - maxrows else first
1841 else active
1843 let navigate incr =
1844 let active = m_active + incr in
1845 let active = bound active 0 (source#getitemcount - 1) in
1846 let first = calcfirst m_first active in
1847 postRedisplay "outline navigate";
1848 coe {< m_active = active; m_first = first >}
1850 let navscroll first =
1851 let active =
1852 let dist = m_active - first in
1853 if dist < 0
1854 then first
1855 else (
1856 if dist < maxrows
1857 then m_active
1858 else first + maxrows
1861 postRedisplay "outline navscroll";
1862 coe {< m_first = first; m_active = active >}
1864 let ctrl = Wsi.withctrl mask in
1865 let open Keys in
1866 match Wsi.kc2kt key with
1867 | Ascii 'a' when ctrl ->
1868 let text =
1869 if m_autonarrow
1870 then (
1871 source#denarrow;
1874 else (
1875 let pattern = source#renarrow in
1876 if nonemptystr m_qsearch
1877 then (source#narrow m_qsearch; m_qsearch)
1878 else pattern
1881 settext (not m_autonarrow) text;
1882 postRedisplay "toggle auto narrowing";
1883 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1885 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1886 settext true E.s;
1887 postRedisplay "toggle auto narrowing";
1888 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1890 | Ascii 'n' when ctrl ->
1891 source#narrow m_qsearch;
1892 if not m_autonarrow
1893 then source#add_narrow_pattern m_qsearch;
1894 postRedisplay "outline ctrl-n";
1895 coe {< m_first = 0; m_active = 0 >}
1897 | Ascii 'S' when ctrl ->
1898 let active = source#calcactive (getanchor ()) in
1899 let first = firstof m_first active in
1900 postRedisplay "outline ctrl-s";
1901 coe {< m_first = first; m_active = active >}
1903 | Ascii 'u' when ctrl ->
1904 postRedisplay "outline ctrl-u";
1905 if m_autonarrow && nonemptystr m_qsearch
1906 then (
1907 ignore (source#renarrow);
1908 settext m_autonarrow E.s;
1909 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1911 else (
1912 source#del_narrow_pattern;
1913 let pattern = source#renarrow in
1914 let text =
1915 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1917 settext m_autonarrow text;
1918 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1921 | Ascii 'l' when ctrl ->
1922 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1923 postRedisplay "outline ctrl-l";
1924 coe {< m_first = first >}
1926 | Ascii '\t' when m_autonarrow ->
1927 if nonemptystr m_qsearch
1928 then (
1929 postRedisplay "outline list view tab";
1930 source#add_narrow_pattern m_qsearch;
1931 settext true E.s;
1932 coe {< m_qsearch = E.s >}
1934 else coe self
1936 | Escape when m_autonarrow ->
1937 if nonemptystr m_qsearch
1938 then source#add_narrow_pattern m_qsearch;
1939 super#key key mask
1941 | Enter when m_autonarrow ->
1942 if nonemptystr m_qsearch
1943 then source#add_narrow_pattern m_qsearch;
1944 super#key key mask
1946 | (Ascii _ | Code _) when m_autonarrow ->
1947 let pattern = m_qsearch ^ toutf8 key in
1948 postRedisplay "outlinelistview autonarrow add";
1949 source#narrow pattern;
1950 settext true pattern;
1951 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1953 | Backspace when m_autonarrow ->
1954 if emptystr m_qsearch
1955 then coe self
1956 else
1957 let pattern = withoutlastutf8 m_qsearch in
1958 postRedisplay "outlinelistview autonarrow backspace";
1959 ignore (source#renarrow);
1960 source#narrow pattern;
1961 settext true pattern;
1962 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1964 | Up when ctrl -> navscroll (max 0 (m_first - 1))
1966 | Down when ctrl ->
1967 navscroll (min (source#getitemcount - 1) (m_first + 1))
1969 | Up -> navigate ~-1
1970 | Down -> navigate 1
1971 | Prior -> navigate ~-(fstate.maxrows)
1972 | Next -> navigate fstate.maxrows
1974 | Right ->
1975 let o =
1976 if ctrl
1977 then (
1978 postRedisplay "outline ctrl right";
1979 {< m_pan = m_pan + 1 >}
1981 else self#updownlevel 1
1983 coe o
1985 | Left ->
1986 let o =
1987 if ctrl
1988 then (
1989 postRedisplay "outline ctrl left";
1990 {< m_pan = m_pan - 1 >}
1992 else self#updownlevel ~-1
1994 coe o
1996 | Home ->
1997 postRedisplay "outline home";
1998 coe {< m_first = 0; m_active = 0 >}
2000 | End ->
2001 let active = source#getitemcount - 1 in
2002 let first = max 0 (active - fstate.maxrows) in
2003 postRedisplay "outline end";
2004 coe {< m_active = active; m_first = first >}
2006 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
2007 super#key key mask
2008 end;;
2010 let genhistoutlines () =
2011 Config.gethist ()
2012 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
2013 compare c2.lastvisit c1.lastvisit)
2014 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
2015 let path = if nonemptystr origin then origin else path in
2016 let base = mbtoutf8 @@ Filename.basename path in
2017 (base ^ "\000" ^ c.title, 1, Ohistory hist)
2021 let gotohist (path, c, bookmarks, x, anchor, origin) =
2022 Config.save leavebirdseye;
2023 state.anchor <- anchor;
2024 state.bookmarks <- bookmarks;
2025 state.origin <- origin;
2026 state.x <- x;
2027 setconf conf c;
2028 let x0, y0, x1, y1 = conf.trimfuzz in
2029 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2030 reshape ~firsttime:true state.winw state.winh;
2031 opendoc path origin;
2032 setzoom c.zoom;
2035 let setcheckers enabled =
2036 match !checkerstexid with
2037 | None -> if enabled then checkerstexid := Some (makecheckers ())
2038 | Some id ->
2039 if not enabled
2040 then (
2041 GlTex.delete_texture id;
2042 checkerstexid := None;
2046 let describe_layout layout =
2047 let d =
2048 match layout with
2049 | [] -> "Page 0"
2050 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2051 | l :: rest ->
2052 let rangestr a b =
2053 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2054 else Printf.sprintf "%d%s%d" (a.pageno+1)
2055 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2056 (b.pageno+1)
2058 let rec fold s la lb = function
2059 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2060 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2061 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2063 fold "Pages" l l rest
2065 let percent =
2066 let maxy = maxy () in
2067 if maxy <= 0
2068 then 100.
2069 else 100. *. (float state.y /. float maxy)
2071 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2074 let setpresentationmode v =
2075 let n = page_of_y state.y in
2076 state.anchor <- (n, 0.0, 1.0);
2077 conf.presentation <- v;
2078 if conf.fitmodel = FitPage
2079 then reqlayout conf.angle conf.fitmodel;
2080 represent ();
2083 let enterinfomode =
2084 let btos b = if b then Utf8syms.radical else E.s in
2085 let showextended = ref false in
2086 let showcolors = ref false in
2087 let leave mode _ = state.mode <- mode in
2088 let src =
2089 (object
2090 val mutable m_l = []
2091 val mutable m_a = E.a
2092 val mutable m_prev_uioh = nouioh
2093 val mutable m_prev_mode = View
2095 inherit lvsourcebase
2097 method reset prev_mode prev_uioh =
2098 m_a <- Array.of_list (List.rev m_l);
2099 m_l <- [];
2100 m_prev_mode <- prev_mode;
2101 m_prev_uioh <- prev_uioh;
2103 method int name get set =
2104 m_l <-
2105 (name, `int get, 1,
2106 Action (
2107 fun u ->
2108 let ondone s =
2109 try set (int_of_string s)
2110 with exn ->
2111 state.text <- Printf.sprintf "bad integer `%s': %s"
2112 s @@ exntos exn
2114 state.text <- E.s;
2115 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2116 state.mode <- Textentry (te, leave m_prev_mode);
2118 )) :: m_l
2120 method int_with_suffix name get set =
2121 m_l <-
2122 (name, `intws get, 1,
2123 Action (
2124 fun u ->
2125 let ondone s =
2126 try set (int_of_string_with_suffix s)
2127 with exn ->
2128 state.text <- Printf.sprintf "bad integer `%s': %s"
2129 s @@ exntos exn
2131 state.text <- E.s;
2132 let te =
2133 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2135 state.mode <- Textentry (te, leave m_prev_mode);
2137 )) :: m_l
2139 method bool ?(offset=1) ?(btos=btos) name get set =
2140 m_l <-
2141 (name, `bool (btos, get), offset, Action (
2142 fun u ->
2143 let v = get () in
2144 set (not v);
2146 )) :: m_l
2148 method color name get set =
2149 m_l <-
2150 (name, `color get, 1,
2151 Action (
2152 fun u ->
2153 let invalid = (nan, nan, nan) in
2154 let ondone s =
2155 let c =
2156 try color_of_string s
2157 with exn ->
2158 state.text <- Printf.sprintf "bad color `%s': %s"
2159 s @@ exntos exn;
2160 invalid
2162 if c <> invalid
2163 then set c;
2165 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2166 state.text <- color_to_string (get ());
2167 state.mode <- Textentry (te, leave m_prev_mode);
2169 )) :: m_l
2171 method string name get set =
2172 m_l <-
2173 (name, `string get, 1,
2174 Action (
2175 fun u ->
2176 let ondone s = set s in
2177 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2178 state.mode <- Textentry (te, leave m_prev_mode);
2180 )) :: m_l
2182 method colorspace name get set =
2183 m_l <-
2184 (name, `string get, 1,
2185 Action (
2186 fun _ ->
2187 let source =
2188 (object
2189 inherit lvsourcebase
2191 initializer
2192 m_active <- CSTE.to_int conf.colorspace;
2193 m_first <- 0;
2195 method getitemcount =
2196 Array.length CSTE.names
2197 method getitem n =
2198 (CSTE.names.(n), 0)
2199 method exit ~uioh ~cancel ~active ~first ~pan =
2200 ignore (uioh, first, pan);
2201 if not cancel then set active;
2202 None
2203 method hasaction _ = true
2204 end)
2206 state.text <- E.s;
2207 let modehash = findkeyhash conf "info" in
2208 coe (new listview ~zebra:false ~helpmode:false
2209 ~source ~trusted:true ~modehash)
2210 )) :: m_l
2212 method paxmark name get set =
2213 m_l <-
2214 (name, `string get, 1,
2215 Action (
2216 fun _ ->
2217 let source =
2218 (object
2219 inherit lvsourcebase
2221 initializer
2222 m_active <- MTE.to_int conf.paxmark;
2223 m_first <- 0;
2225 method getitemcount = Array.length MTE.names
2226 method getitem n = (MTE.names.(n), 0)
2227 method exit ~uioh ~cancel ~active ~first ~pan =
2228 ignore (uioh, first, pan);
2229 if not cancel then set active;
2230 None
2231 method hasaction _ = true
2232 end)
2234 state.text <- E.s;
2235 let modehash = findkeyhash conf "info" in
2236 coe (new listview ~zebra:false ~helpmode:false
2237 ~source ~trusted:true ~modehash)
2238 )) :: m_l
2240 method fitmodel name get set =
2241 m_l <-
2242 (name, `string get, 1,
2243 Action (
2244 fun _ ->
2245 let source =
2246 (object
2247 inherit lvsourcebase
2249 initializer
2250 m_active <- FMTE.to_int conf.fitmodel;
2251 m_first <- 0;
2253 method getitemcount = Array.length FMTE.names
2254 method getitem n = (FMTE.names.(n), 0)
2255 method exit ~uioh ~cancel ~active ~first ~pan =
2256 ignore (uioh, first, pan);
2257 if not cancel then set active;
2258 None
2259 method hasaction _ = true
2260 end)
2262 state.text <- E.s;
2263 let modehash = findkeyhash conf "info" in
2264 coe (new listview ~zebra:false ~helpmode:false
2265 ~source ~trusted:true ~modehash)
2266 )) :: m_l
2268 method caption s offset =
2269 m_l <- (s, `empty, offset, Noaction) :: m_l
2271 method caption2 s f offset =
2272 m_l <- (s, `string f, offset, Noaction) :: m_l
2274 method getitemcount = Array.length m_a
2276 method getitem n =
2277 let tostr = function
2278 | `int f -> string_of_int (f ())
2279 | `intws f -> string_with_suffix_of_int (f ())
2280 | `string f -> f ()
2281 | `color f -> color_to_string (f ())
2282 | `bool (btos, f) -> btos (f ())
2283 | `empty -> E.s
2285 let name, t, offset, _ = m_a.(n) in
2286 ((let s = tostr t in
2287 if nonemptystr s
2288 then Printf.sprintf "%s\t%s" name s
2289 else name),
2290 offset)
2292 method exit ~uioh ~cancel ~active ~first ~pan =
2293 let uiohopt =
2294 if not cancel
2295 then (
2296 let uioh =
2297 match m_a.(active) with
2298 | _, _, _, Action f -> f uioh
2299 | _, _, _, Noaction -> uioh
2301 Some uioh
2303 else None
2305 m_active <- active;
2306 m_first <- first;
2307 m_pan <- pan;
2308 uiohopt
2310 method hasaction n =
2311 match m_a.(n) with
2312 | _, _, _, Action _ -> true
2313 | _, _, _, Noaction -> false
2315 initializer m_active <- 1
2316 end)
2318 let rec fillsrc prevmode prevuioh =
2319 let sep () = src#caption E.s 0 in
2320 let colorp name get set =
2321 src#string name
2322 (fun () -> color_to_string (get ()))
2323 (fun v ->
2324 try set @@ color_of_string v
2325 with exn ->
2326 state.text <-
2327 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2330 let rgba name get set =
2331 src#string name
2332 (fun () -> get () |> rgba_to_string)
2333 (fun v ->
2334 try set @@ rgba_of_string v
2335 with exn ->
2336 state.text <-
2337 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2340 let oldmode = state.mode in
2341 let birdseye = isbirdseye state.mode in
2343 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2345 src#bool "presentation mode"
2346 (fun () -> conf.presentation)
2347 (fun v -> setpresentationmode v);
2349 src#bool "ignore case in searches"
2350 (fun () -> conf.icase)
2351 (fun v -> conf.icase <- v);
2353 src#bool "preload"
2354 (fun () -> conf.preload)
2355 (fun v -> conf.preload <- v);
2357 src#bool "highlight links"
2358 (fun () -> conf.hlinks)
2359 (fun v -> conf.hlinks <- v);
2361 src#bool "under info"
2362 (fun () -> conf.underinfo)
2363 (fun v -> conf.underinfo <- v);
2365 src#fitmodel "fit model"
2366 (fun () -> FMTE.to_string conf.fitmodel)
2367 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2369 src#bool "trim margins"
2370 (fun () -> conf.trimmargins)
2371 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2373 sep ();
2374 src#int "inter-page space"
2375 (fun () -> conf.interpagespace)
2376 (fun n ->
2377 conf.interpagespace <- n;
2378 docolumns conf.columns;
2379 let pageno, py =
2380 match state.layout with
2381 | [] -> 0, 0
2382 | l :: _ -> l.pageno, l.pagey
2384 state.maxy <- calcheight ();
2385 let y = getpagey pageno in
2386 gotoxy state.x (y + py)
2389 src#int "page bias"
2390 (fun () -> conf.pagebias)
2391 (fun v -> conf.pagebias <- v);
2393 src#int "scroll step"
2394 (fun () -> conf.scrollstep)
2395 (fun n -> conf.scrollstep <- n);
2397 src#int "horizontal scroll step"
2398 (fun () -> conf.hscrollstep)
2399 (fun v -> conf.hscrollstep <- v);
2401 src#int "auto scroll step"
2402 (fun () ->
2403 match state.autoscroll with
2404 | Some step -> step
2405 | _ -> conf.autoscrollstep)
2406 (fun n ->
2407 let n = boundastep state.winh n in
2408 if state.autoscroll <> None
2409 then state.autoscroll <- Some n;
2410 conf.autoscrollstep <- n);
2412 src#int "zoom"
2413 (fun () -> truncate (conf.zoom *. 100.))
2414 (fun v -> pivotzoom ((float v) /. 100.));
2416 src#int "rotation"
2417 (fun () -> conf.angle)
2418 (fun v -> reqlayout v conf.fitmodel);
2420 src#int "scroll bar width"
2421 (fun () -> conf.scrollbw)
2422 (fun v ->
2423 conf.scrollbw <- v;
2424 reshape state.winw state.winh;
2427 src#int "scroll handle height"
2428 (fun () -> conf.scrollh)
2429 (fun v -> conf.scrollh <- v;);
2431 src#int "thumbnail width"
2432 (fun () -> conf.thumbw)
2433 (fun v ->
2434 conf.thumbw <- min 4096 v;
2435 match oldmode with
2436 | Birdseye beye ->
2437 leavebirdseye beye false;
2438 enterbirdseye ()
2439 | Textentry _
2440 | View
2441 | LinkNav _ -> ()
2444 let mode = state.mode in
2445 src#string "columns"
2446 (fun () ->
2447 match conf.columns with
2448 | Csingle _ -> "1"
2449 | Cmulti (multi, _) -> multicolumns_to_string multi
2450 | Csplit (count, _) -> "-" ^ string_of_int count
2452 (fun v ->
2453 let n, a, b = multicolumns_of_string v in
2454 setcolumns mode n a b);
2456 sep ();
2457 src#caption "Pixmap cache" 0;
2458 src#int_with_suffix "size (advisory)"
2459 (fun () -> conf.memlimit)
2460 (fun v -> conf.memlimit <- v);
2462 src#caption2 "used"
2463 (fun () ->
2464 Printf.sprintf "%s bytes, %d tiles"
2465 (string_with_suffix_of_int state.memused)
2466 (Hashtbl.length state.tilemap)) 1;
2468 sep ();
2469 src#caption "Layout" 0;
2470 src#caption2 "Dimension"
2471 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2472 state.winw state.winh
2473 state.w state.maxy)
2475 if conf.debug
2476 then src#caption2 "Position" (fun () ->
2477 Printf.sprintf "%dx%d" state.x state.y
2479 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2481 sep ();
2482 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2483 "Save these parameters as global defaults at exit"
2484 (fun () -> conf.bedefault)
2485 (fun v -> conf.bedefault <- v);
2487 sep ();
2488 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2489 src#bool ~offset:0 ~btos "Extended parameters"
2490 (fun () -> !showextended)
2491 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2492 if !showextended
2493 then (
2494 src#bool "checkers"
2495 (fun () -> conf.checkers)
2496 (fun v -> conf.checkers <- v; setcheckers v);
2497 src#bool "update cursor"
2498 (fun () -> conf.updatecurs)
2499 (fun v -> conf.updatecurs <- v);
2500 src#bool "scroll-bar on the left"
2501 (fun () -> conf.leftscroll)
2502 (fun v -> conf.leftscroll <- v);
2503 src#bool "verbose"
2504 (fun () -> conf.verbose)
2505 (fun v -> conf.verbose <- v);
2506 src#bool "invert colors"
2507 (fun () -> conf.invert)
2508 (fun v -> conf.invert <- v);
2509 src#bool "max fit"
2510 (fun () -> conf.maxhfit)
2511 (fun v -> conf.maxhfit <- v);
2512 src#bool "pax mode"
2513 (fun () -> conf.pax != None)
2514 (fun v ->
2515 if v
2516 then conf.pax <- Some (now ())
2517 else conf.pax <- None);
2518 src#string "uri launcher"
2519 (fun () -> conf.urilauncher)
2520 (fun v -> conf.urilauncher <- v);
2521 src#string "path launcher"
2522 (fun () -> conf.pathlauncher)
2523 (fun v -> conf.pathlauncher <- v);
2524 src#string "tile size"
2525 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2526 (fun v ->
2528 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2529 conf.tilew <- max 64 w;
2530 conf.tileh <- max 64 h;
2531 flushtiles ();
2532 with exn ->
2533 state.text <- Printf.sprintf "bad tile size `%s': %s"
2534 v @@ exntos exn
2536 src#int "texture count"
2537 (fun () -> conf.texcount)
2538 (fun v ->
2539 if realloctexts v
2540 then conf.texcount <- v
2541 else impmsg "failed to set texture count please retry later"
2543 src#int "slice height"
2544 (fun () -> conf.sliceheight)
2545 (fun v ->
2546 conf.sliceheight <- v;
2547 wcmd "sliceh %d" conf.sliceheight;
2549 src#int "anti-aliasing level"
2550 (fun () -> conf.aalevel)
2551 (fun v ->
2552 conf.aalevel <- bound v 0 8;
2553 state.anchor <- getanchor ();
2554 opendoc state.path state.password;
2556 src#string "page scroll scaling factor"
2557 (fun () -> string_of_float conf.pgscale)
2558 (fun v ->
2559 try conf.pgscale <- float_of_string v
2560 with exn ->
2561 state.text <-
2562 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2563 @@ exntos exn
2565 src#int "ui font size"
2566 (fun () -> fstate.fontsize)
2567 (fun v -> setfontsize (bound v 5 100));
2568 src#int "hint font size"
2569 (fun () -> conf.hfsize)
2570 (fun v -> conf.hfsize <- bound v 5 100);
2571 src#string "trim fuzz"
2572 (fun () -> irect_to_string conf.trimfuzz)
2573 (fun v ->
2575 conf.trimfuzz <- irect_of_string v;
2576 if conf.trimmargins
2577 then settrim true conf.trimfuzz;
2578 with exn ->
2579 state.text <- Printf.sprintf "bad irect `%s': %s" v
2580 @@ exntos exn
2582 src#string "selection command"
2583 (fun () -> conf.selcmd)
2584 (fun v -> conf.selcmd <- v);
2585 src#string "synctex command"
2586 (fun () -> conf.stcmd)
2587 (fun v -> conf.stcmd <- v);
2588 src#string "pax command"
2589 (fun () -> conf.paxcmd)
2590 (fun v -> conf.paxcmd <- v);
2591 src#string "ask password command"
2592 (fun () -> conf.passcmd)
2593 (fun v -> conf.passcmd <- v);
2594 src#string "save path command"
2595 (fun () -> conf.savecmd)
2596 (fun v -> conf.savecmd <- v);
2597 src#colorspace "color space"
2598 (fun () -> CSTE.to_string conf.colorspace)
2599 (fun v ->
2600 conf.colorspace <- CSTE.of_int v;
2601 wcmd "cs %d" v;
2602 load state.layout;
2604 src#paxmark "pax mark method"
2605 (fun () -> MTE.to_string conf.paxmark)
2606 (fun v -> conf.paxmark <- MTE.of_int v);
2607 if bousable ()
2608 then
2609 src#bool "use PBO"
2610 (fun () -> conf.usepbo)
2611 (fun v -> conf.usepbo <- v);
2612 src#bool "mouse wheel scrolls pages"
2613 (fun () -> conf.wheelbypage)
2614 (fun v -> conf.wheelbypage <- v);
2615 src#bool "open remote links in a new instance"
2616 (fun () -> conf.riani)
2617 (fun v -> conf.riani <- v);
2618 src#bool "edit annotations inline"
2619 (fun () -> conf.annotinline)
2620 (fun v -> conf.annotinline <- v);
2621 src#bool "coarse positioning in presentation mode"
2622 (fun () -> conf.coarseprespos)
2623 (fun v -> conf.coarseprespos <- v);
2624 src#bool "use document CSS"
2625 (fun () -> conf.usedoccss)
2626 (fun v ->
2627 conf.usedoccss <- v;
2628 state.anchor <- getanchor ();
2629 opendoc state.path state.password;
2631 src#bool ~btos "colors"
2632 (fun () -> !showcolors)
2633 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2634 if !showcolors
2635 then (
2636 colorp " background"
2637 (fun () -> conf.bgcolor)
2638 (fun v -> conf.bgcolor <- v);
2639 rgba " scrollbar"
2640 (fun () -> conf.sbarcolor)
2641 (fun v -> conf.sbarcolor <- v);
2642 rgba " scrollbar handle"
2643 (fun () -> conf.sbarhndlcolor)
2644 (fun v -> conf.sbarhndlcolor <- v);
2648 sep ();
2649 src#caption "Document" 0;
2650 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2651 src#caption2 "Pages"
2652 (fun () -> string_of_int state.pagecount) 1;
2653 src#caption2 "Dimensions"
2654 (fun () -> string_of_int (List.length state.pdims)) 1;
2655 if nonemptystr conf.css
2656 then src#caption2 "CSS" (fun () -> conf.css) 1;
2657 if conf.trimmargins
2658 then (
2659 sep ();
2660 src#caption "Trimmed margins" 0;
2661 src#caption2 "Dimensions"
2662 (fun () -> string_of_int (List.length state.pdims)) 1;
2665 sep ();
2666 src#caption "OpenGL" 0;
2667 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2668 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2670 sep ();
2671 src#caption "Location" 0;
2672 if nonemptystr state.origin
2673 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2674 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2676 src#reset prevmode prevuioh;
2678 fun () ->
2679 state.text <- E.s;
2680 resetmstate ();
2681 let prevmode = state.mode
2682 and prevuioh = state.uioh in
2683 fillsrc prevmode prevuioh;
2684 let source = (src :> lvsource) in
2685 let modehash = findkeyhash conf "info" in
2686 state.uioh <-
2687 coe (object (self)
2688 inherit listview ~zebra:false ~helpmode:false
2689 ~source ~trusted:true ~modehash as super
2690 val mutable m_prevmemused = 0
2691 method! infochanged = function
2692 | Memused ->
2693 if m_prevmemused != state.memused
2694 then (
2695 m_prevmemused <- state.memused;
2696 postRedisplay "memusedchanged";
2698 | Pdim -> postRedisplay "pdimchanged"
2699 | Docinfo -> fillsrc prevmode prevuioh
2701 method! key key mask =
2702 if not (Wsi.withctrl mask)
2703 then
2704 match [@warning "-4"] Wsi.kc2kt key with
2705 | Keys.Left -> coe (self#updownlevel ~-1)
2706 | Keys.Right -> coe (self#updownlevel 1)
2707 | _ -> super#key key mask
2708 else super#key key mask
2709 end);
2710 postRedisplay "info";
2713 let enterhelpmode =
2714 let source =
2715 (object
2716 inherit lvsourcebase
2717 method getitemcount = Array.length state.help
2718 method getitem n =
2719 let s, l, _ = state.help.(n) in
2720 (s, l)
2722 method exit ~uioh ~cancel ~active ~first ~pan =
2723 let optuioh =
2724 if not cancel
2725 then (
2726 match state.help.(active) with
2727 | _, _, Action f -> Some (f uioh)
2728 | _, _, Noaction -> Some uioh
2730 else None
2732 m_active <- active;
2733 m_first <- first;
2734 m_pan <- pan;
2735 optuioh
2737 method hasaction n =
2738 match state.help.(n) with
2739 | _, _, Action _ -> true
2740 | _, _, Noaction -> false
2742 initializer
2743 m_active <- -1
2744 end)
2745 in fun () ->
2746 let modehash = findkeyhash conf "help" in
2747 resetmstate ();
2748 state.uioh <- coe (new listview
2749 ~zebra:false ~helpmode:true
2750 ~source ~trusted:true ~modehash);
2751 postRedisplay "help";
2754 let entermsgsmode =
2755 let msgsource =
2756 (object
2757 inherit lvsourcebase
2758 val mutable m_items = E.a
2760 method getitemcount = 1 + Array.length m_items
2762 method getitem n =
2763 if n = 0
2764 then "[Clear]", 0
2765 else m_items.(n-1), 0
2767 method exit ~uioh ~cancel ~active ~first ~pan =
2768 ignore uioh;
2769 if not cancel
2770 then (
2771 if active = 0
2772 then Buffer.clear state.errmsgs;
2774 m_active <- active;
2775 m_first <- first;
2776 m_pan <- pan;
2777 None
2779 method hasaction n =
2780 n = 0
2782 method reset =
2783 state.newerrmsgs <- false;
2784 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2785 m_items <- Array.of_list l
2787 initializer
2788 m_active <- 0
2789 end)
2790 in fun () ->
2791 state.text <- E.s;
2792 resetmstate ();
2793 msgsource#reset;
2794 let source = (msgsource :> lvsource) in
2795 let modehash = findkeyhash conf "listview" in
2796 state.uioh <-
2797 coe (object
2798 inherit listview ~zebra:false ~helpmode:false
2799 ~source ~trusted:false ~modehash as super
2800 method! display =
2801 if state.newerrmsgs
2802 then msgsource#reset;
2803 super#display
2804 end);
2805 postRedisplay "msgs";
2808 let getusertext s =
2809 let editor = getenvwithdef "EDITOR" E.s in
2810 if emptystr editor
2811 then E.s
2812 else
2813 let tmppath = Filename.temp_file "llpp" "note" in
2814 if nonemptystr s
2815 then (
2816 let oc = open_out tmppath in
2817 output_string oc s;
2818 close_out oc;
2820 let execstr = editor ^ " " ^ tmppath in
2821 let s =
2822 match spawn execstr [] with
2823 | exception exn ->
2824 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2826 | pid ->
2827 match Unix.waitpid [] pid with
2828 | exception exn ->
2829 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2831 | (_pid, status) ->
2832 match status with
2833 | Unix.WEXITED 0 -> filecontents tmppath
2834 | Unix.WEXITED n ->
2835 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2837 | Unix.WSIGNALED n ->
2838 impmsg "editor process(%s) was killed by signal %d" execstr n;
2840 | Unix.WSTOPPED n ->
2841 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2844 match Unix.unlink tmppath with
2845 | exception exn ->
2846 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2848 | () -> s
2851 let enterannotmode opaque slinkindex =
2852 let msgsource =
2853 (object
2854 inherit lvsourcebase
2855 val mutable m_text = E.s
2856 val mutable m_items = E.a
2858 method getitemcount = Array.length m_items
2860 method getitem n =
2861 let label, _func = m_items.(n) in
2862 label, 0
2864 method exit ~uioh ~cancel ~active ~first ~pan =
2865 ignore (uioh, first, pan);
2866 if not cancel
2867 then (
2868 let _label, func = m_items.(active) in
2869 func ()
2871 None
2873 method hasaction n = nonemptystr @@ fst m_items.(n)
2875 method reset s =
2876 let rec split accu b i =
2877 let p = b+i in
2878 if p = String.length s
2879 then (String.sub s b (p-b), unit) :: accu
2880 else
2881 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2882 then
2883 let ss = if i = 0 then E.s else String.sub s b i in
2884 split ((ss, unit)::accu) (p+1) 0
2885 else split accu b (i+1)
2887 let cleanup () =
2888 wcmd "freepage %s" (~> opaque);
2889 let keys =
2890 Hashtbl.fold (fun key opaque' accu ->
2891 if opaque' = opaque'
2892 then key :: accu else accu) state.pagemap []
2894 List.iter (Hashtbl.remove state.pagemap) keys;
2895 flushtiles ();
2896 gotoxy state.x state.y
2898 let dele () =
2899 delannot opaque slinkindex;
2900 cleanup ();
2902 let edit inline () =
2903 let update s =
2904 if emptystr s
2905 then dele ()
2906 else (
2907 modannot opaque slinkindex s;
2908 cleanup ();
2911 if inline
2912 then
2913 let mode = state.mode in
2914 state.mode <-
2915 Textentry (
2916 ("annotation: ", m_text, None, textentry, update, true),
2917 fun _ -> state.mode <- mode
2919 state.text <- E.s;
2920 enttext ();
2921 else
2922 let s = getusertext m_text in
2923 update s
2925 m_text <- s;
2926 m_items <-
2927 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2928 :: ("[Delete]", dele)
2929 :: ("[Edit]", edit conf.annotinline)
2930 :: (E.s, unit)
2931 :: split [] 0 0 |> List.rev |> Array.of_list
2933 initializer
2934 m_active <- 0
2935 end)
2937 state.text <- E.s;
2938 let s = getannotcontents opaque slinkindex in
2939 resetmstate ();
2940 msgsource#reset s;
2941 let source = (msgsource :> lvsource) in
2942 let modehash = findkeyhash conf "listview" in
2943 state.uioh <- coe (object
2944 inherit listview ~zebra:false ~helpmode:false
2945 ~source ~trusted:false ~modehash
2946 end);
2947 postRedisplay "enterannotmode";
2950 let gotoremote spec =
2951 let filename, dest = splitatchar spec '#' in
2952 let getpath filename =
2953 let path =
2954 if nonemptystr filename
2955 then
2956 if Filename.is_relative filename
2957 then
2958 let dir = Filename.dirname state.path in
2959 let dir =
2960 if Filename.is_implicit dir
2961 then Filename.concat (Sys.getcwd ()) dir
2962 else dir
2964 Filename.concat dir filename
2965 else filename
2966 else E.s
2968 if Sys.file_exists path
2969 then path
2970 else E.s
2972 let path = getpath filename in
2973 let dospawn lcmd =
2974 if conf.riani
2975 then
2976 let cmd = Lazy.force_val lcmd in
2977 match spawn cmd with
2978 | _pid -> ()
2979 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
2980 else
2981 let anchor = getanchor () in
2982 let ranchor = state.path, state.password, anchor, state.origin in
2983 state.origin <- E.s;
2984 state.ranchors <- ranchor :: state.ranchors;
2985 opendoc path E.s;
2987 if substratis spec 0 "page="
2988 then
2989 match Scanf.sscanf spec "page=%d" (fun n -> n) with
2990 | pageno ->
2991 state.anchor <- (pageno, 0.0, 0.0);
2992 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
2993 | exception exn ->
2994 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
2995 else (
2996 state.nameddest <- dest;
2997 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
3001 let gotounder = function
3002 | Ulinkuri s when isexternallink s ->
3003 if substratis s 0 "file://"
3004 then gotoremote @@ String.sub s 7 (String.length s - 7)
3005 else Help.gotouri conf.urilauncher s
3006 | Ulinkuri s ->
3007 let pageno, x, y = uritolocation s in
3008 addnav ();
3009 gotopagexy pageno x y
3010 | Utext _ | Unone -> ()
3011 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
3014 let gotooutline (_, _, kind) =
3015 match kind with
3016 | Onone -> ()
3017 | Oanchor anchor ->
3018 let (pageno, y, _) = anchor in
3019 let y = getanchory
3020 (if conf.presentation then (pageno, y, 1.0) else anchor)
3022 addnav ();
3023 gotoxy state.x y
3024 | Ouri uri -> gotounder (Ulinkuri uri)
3025 | Olaunch cmd -> error "gotounder (Ulaunch %S)" cmd
3026 | Oremote (remote, pageno) ->
3027 error "gotounder (Uremote (%S,%d) )" remote pageno
3028 | Ohistory hist -> gotohist hist
3029 | Oremotedest (path, dest) ->
3030 error "gotounder (Uremotedest (%S, %S))" path dest
3033 class outlinesoucebase fetchoutlines = object (self)
3034 inherit lvsourcebase
3035 val mutable m_items = E.a
3036 val mutable m_minfo = E.a
3037 val mutable m_orig_items = E.a
3038 val mutable m_orig_minfo = E.a
3039 val mutable m_narrow_patterns = []
3040 val mutable m_gen = -1
3042 method getitemcount = Array.length m_items
3044 method getitem n =
3045 let s, n, _ = m_items.(n) in
3046 (s, n+0)
3048 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3049 ignore (uioh, first);
3050 let items, minfo =
3051 if m_narrow_patterns = []
3052 then m_orig_items, m_orig_minfo
3053 else m_items, m_minfo
3055 m_pan <- pan;
3056 if not cancel
3057 then (
3058 m_items <- items;
3059 m_minfo <- minfo;
3060 gotooutline m_items.(active);
3062 else (
3063 m_items <- items;
3064 m_minfo <- minfo;
3066 None
3068 method hasaction (_:int) = true
3070 method greetmsg =
3071 if Array.length m_items != Array.length m_orig_items
3072 then
3073 let s =
3074 match m_narrow_patterns with
3075 | one :: [] -> one
3076 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3078 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3079 else E.s
3081 method statestr =
3082 match m_narrow_patterns with
3083 | [] -> E.s
3084 | one :: [] -> one
3085 | head :: _ -> Utf8syms.ellipsis ^ head
3087 method narrow pattern =
3088 match Str.regexp_case_fold pattern with
3089 | exception _ -> ()
3090 | re ->
3091 let rec loop accu minfo n =
3092 if n = -1
3093 then (
3094 m_items <- Array.of_list accu;
3095 m_minfo <- Array.of_list minfo;
3097 else
3098 let (s, _, _) as o = m_items.(n) in
3099 let accu, minfo =
3100 match Str.search_forward re s 0 with
3101 | exception Not_found -> accu, minfo
3102 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3104 loop accu minfo (n-1)
3106 loop [] [] (Array.length m_items - 1)
3108 method! getminfo = m_minfo
3110 method denarrow =
3111 m_orig_items <- fetchoutlines ();
3112 m_minfo <- m_orig_minfo;
3113 m_items <- m_orig_items
3115 method add_narrow_pattern pattern =
3116 m_narrow_patterns <- pattern :: m_narrow_patterns
3118 method del_narrow_pattern =
3119 match m_narrow_patterns with
3120 | _ :: rest -> m_narrow_patterns <- rest
3121 | [] -> ()
3123 method renarrow =
3124 self#denarrow;
3125 match m_narrow_patterns with
3126 | pattern :: [] -> self#narrow pattern; pattern
3127 | list ->
3128 List.fold_left (fun accu pattern ->
3129 self#narrow pattern;
3130 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3132 method calcactive (_:anchor) = 0
3134 method reset anchor items =
3135 if state.gen != m_gen
3136 then (
3137 m_orig_items <- items;
3138 m_items <- items;
3139 m_narrow_patterns <- [];
3140 m_minfo <- E.a;
3141 m_orig_minfo <- E.a;
3142 m_gen <- state.gen;
3144 else (
3145 if items != m_orig_items
3146 then (
3147 m_orig_items <- items;
3148 if m_narrow_patterns == []
3149 then m_items <- items;
3152 let active = self#calcactive anchor in
3153 m_active <- active;
3154 m_first <- firstof m_first active
3158 let outlinesource fetchoutlines =
3159 (object
3160 inherit outlinesoucebase fetchoutlines
3161 method! calcactive anchor =
3162 let rely = getanchory anchor in
3163 let rec loop n best bestd =
3164 if n = Array.length m_items
3165 then best
3166 else
3167 let _, _, kind = m_items.(n) in
3168 match kind with
3169 | Oanchor anchor ->
3170 let orely = getanchory anchor in
3171 let d = abs (orely - rely) in
3172 if d < bestd
3173 then loop (n+1) n d
3174 else loop (n+1) best bestd
3175 | Onone | Oremote _ | Olaunch _
3176 | Oremotedest _ | Ouri _ | Ohistory _ ->
3177 loop (n+1) best bestd
3179 loop 0 ~-1 max_int
3180 end)
3183 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3184 let mkselector sourcetype =
3185 let fetchoutlines () =
3186 match sourcetype with
3187 | `bookmarks -> Array.of_list state.bookmarks
3188 | `outlines -> state.outlines
3189 | `history -> genhistoutlines () |> Array.of_list
3191 let source =
3192 if sourcetype = `history
3193 then new outlinesoucebase fetchoutlines
3194 else outlinesource fetchoutlines
3196 (fun errmsg ->
3197 let outlines = fetchoutlines () in
3198 if Array.length outlines = 0
3199 then showtext ' ' errmsg
3200 else (
3201 resetmstate ();
3202 Wsi.setcursor Wsi.CURSOR_INHERIT;
3203 let anchor = getanchor () in
3204 source#reset anchor outlines;
3205 state.text <- source#greetmsg;
3206 state.uioh <-
3207 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3208 postRedisplay "enter selector";
3212 let mkenter sourcetype errmsg = fun () -> mkselector sourcetype errmsg in
3213 ( mkenter `outlines "document has no outline"
3214 , mkenter `bookmarks "document has no bookmarks (yet)"
3215 , mkenter `history "history is empty" )
3218 let quickbookmark ?title () =
3219 match state.layout with
3220 | [] -> ()
3221 | l :: _ ->
3222 let title =
3223 match title with
3224 | None ->
3225 Unix.(
3226 let tm = localtime (now ()) in
3227 Printf.sprintf
3228 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3229 (l.pageno+1)
3230 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3232 | Some title -> title
3234 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3237 let setautoscrollspeed step goingdown =
3238 let incr = max 1 ((abs step) / 2) in
3239 let incr = if goingdown then incr else -incr in
3240 let astep = boundastep state.winh (step + incr) in
3241 state.autoscroll <- Some astep;
3244 let canpan () =
3245 match conf.columns with
3246 | Csplit _ -> true
3247 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3250 let panbound x = bound x (-state.w) state.winw;;
3252 let existsinrow pageno (columns, coverA, coverB) p =
3253 let last = ((pageno - coverA) mod columns) + columns in
3254 let rec any = function
3255 | [] -> false
3256 | l :: rest ->
3257 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3258 then p l
3259 else (
3260 if not (p l)
3261 then (if l.pageno = last then false else any rest)
3262 else true
3265 any state.layout
3268 let nextpage () =
3269 match state.layout with
3270 | [] ->
3271 let pageno = page_of_y state.y in
3272 gotoxy state.x (getpagey (pageno+1))
3273 | l :: rest ->
3274 match conf.columns with
3275 | Csingle _ ->
3276 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3277 then
3278 let y = clamp (pgscale state.winh) in
3279 gotoxy state.x y
3280 else
3281 let pageno = min (l.pageno+1) (state.pagecount-1) in
3282 gotoxy state.x (getpagey pageno)
3283 | Cmulti ((c, _, _) as cl, _) ->
3284 if conf.presentation
3285 && (existsinrow l.pageno cl
3286 (fun l -> l.pageh > l.pagey + l.pagevh))
3287 then
3288 let y = clamp (pgscale state.winh) in
3289 gotoxy state.x y
3290 else
3291 let pageno = min (l.pageno+c) (state.pagecount-1) in
3292 gotoxy state.x (getpagey pageno)
3293 | Csplit (n, _) ->
3294 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3295 then
3296 let pagey, pageh = getpageyh l.pageno in
3297 let pagey = pagey + pageh * l.pagecol in
3298 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3299 gotoxy state.x (pagey + pageh + ips)
3302 let prevpage () =
3303 match state.layout with
3304 | [] ->
3305 let pageno = page_of_y state.y in
3306 gotoxy state.x (getpagey (pageno-1))
3307 | l :: _ ->
3308 match conf.columns with
3309 | Csingle _ ->
3310 if conf.presentation && l.pagey != 0
3311 then
3312 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3313 else
3314 let pageno = max 0 (l.pageno-1) in
3315 gotoxy state.x (getpagey pageno)
3316 | Cmulti ((c, _, coverB) as cl, _) ->
3317 if conf.presentation &&
3318 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3319 then
3320 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3321 else
3322 let decr =
3323 if l.pageno = state.pagecount - coverB
3324 then 1
3325 else c
3327 let pageno = max 0 (l.pageno-decr) in
3328 gotoxy state.x (getpagey pageno)
3329 | Csplit (n, _) ->
3330 let y =
3331 if l.pagecol = 0
3332 then
3333 if l.pageno = 0
3334 then l.pagey
3335 else
3336 let pageno = max 0 (l.pageno-1) in
3337 let pagey, pageh = getpageyh pageno in
3338 pagey + (n-1)*pageh
3339 else
3340 let pagey, pageh = getpageyh l.pageno in
3341 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3343 gotoxy state.x y
3346 let save () =
3347 if emptystr conf.savecmd
3348 then adderrmsg "savepath-command is empty"
3349 "don't know where to save modified document"
3350 else
3351 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3352 let path =
3353 getcmdoutput
3354 (fun exn ->
3355 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3356 savecmd
3358 if nonemptystr path
3359 then
3360 let tmp = path ^ ".tmp" in
3361 savedoc tmp;
3362 Unix.rename tmp path;
3365 let viewkeyboard key mask =
3366 let enttext te =
3367 let mode = state.mode in
3368 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3369 state.text <- E.s;
3370 enttext ();
3371 postRedisplay "view:enttext"
3373 let ctrl = Wsi.withctrl mask in
3374 let open Keys in
3375 match Wsi.kc2kt key with
3376 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3378 | Ascii 'Q' -> exit 0
3380 | Ascii 'W' ->
3381 if hasunsavedchanges ()
3382 then save ()
3384 | Insert ->
3385 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3386 then (
3387 state.mode <- (
3388 match state.lnava with
3389 | None -> LinkNav (Ltgendir 0)
3390 | Some pn -> LinkNav (Ltexact pn)
3392 gotoxy state.x state.y;
3394 else impmsg "keyboard link navigation does not work under rotation"
3396 | Escape | Ascii 'q' ->
3397 begin match state.mstate with
3398 | Mzoomrect _ ->
3399 resetmstate ();
3400 postRedisplay "kill rect";
3401 | Msel _
3402 | Mpan _
3403 | Mscrolly | Mscrollx
3404 | Mzoom _
3405 | Mnone ->
3406 begin match state.mode with
3407 | LinkNav ln ->
3408 begin match ln with
3409 | Ltexact pl -> state.lnava <- Some pl
3410 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3411 end;
3412 state.mode <- View;
3413 postRedisplay "esc leave linknav"
3414 | Birdseye _ | Textentry _ | View ->
3415 match state.ranchors with
3416 | [] -> raise Quit
3417 | (path, password, anchor, origin) :: rest ->
3418 state.ranchors <- rest;
3419 state.anchor <- anchor;
3420 state.origin <- origin;
3421 state.nameddest <- E.s;
3422 opendoc path password
3423 end;
3424 end;
3426 | Backspace ->
3427 addnavnorc ();
3428 gotoxy state.x (getnav ~-1)
3430 | Ascii 'o' ->
3431 enteroutlinemode ()
3433 | Ascii 'H' ->
3434 enterhistmode ()
3436 | Ascii 'u' ->
3437 state.rects <- [];
3438 state.text <- E.s;
3439 Hashtbl.iter (fun _ opaque ->
3440 clearmark opaque;
3441 Hashtbl.clear state.prects) state.pagemap;
3442 postRedisplay "dehighlight";
3444 | Ascii (('/' | '?') as c) ->
3445 let ondone isforw s =
3446 cbput state.hists.pat s;
3447 state.searchpattern <- s;
3448 search s isforw
3450 let s = String.make 1 c in
3451 enttext (s, E.s, Some (onhist state.hists.pat),
3452 textentry, ondone (c = '/'), true)
3454 | Ascii '+' | Ascii '=' when ctrl ->
3455 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3456 pivotzoom (conf.zoom +. incr)
3458 | Ascii '+' ->
3459 let ondone s =
3460 let n =
3461 try int_of_string s with exn ->
3462 state.text <-
3463 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3464 max_int
3466 if n != max_int
3467 then (
3468 conf.pagebias <- n;
3469 state.text <- "page bias is now " ^ string_of_int n;
3472 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3474 | Ascii '-' when ctrl ->
3475 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3476 pivotzoom (max 0.01 (conf.zoom -. decr))
3478 | Ascii '-' ->
3479 let ondone msg = state.text <- msg in
3480 enttext ("option: ", E.s, None,
3481 optentry state.mode, ondone, true)
3483 | Ascii '0' when ctrl ->
3484 if conf.zoom = 1.0
3485 then gotoxy 0 state.y
3486 else setzoom 1.0
3488 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3489 let cols =
3490 match conf.columns with
3491 | Csingle _ | Cmulti _ -> 1
3492 | Csplit (n, _) -> n
3494 let h = state.winh -
3495 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3497 let zoom = zoomforh state.winw h 0 cols in
3498 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3499 then setzoom zoom
3501 | Ascii '3' when ctrl ->
3502 let fm =
3503 match conf.fitmodel with
3504 | FitWidth -> FitProportional
3505 | FitProportional -> FitPage
3506 | FitPage -> FitWidth
3508 state.text <- "fit model: " ^ FMTE.to_string fm;
3509 reqlayout conf.angle fm
3511 | Ascii '4' when ctrl ->
3512 let zoom = getmaxw () /. float state.winw in
3513 if zoom > 0.0 then setzoom zoom
3515 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3517 | Ascii ('0'..'9' as c) when not ctrl ->
3518 let ondone s =
3519 let n =
3520 try int_of_string s with exn ->
3521 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3524 if n >= 0
3525 then (
3526 addnav ();
3527 cbput state.hists.pag (string_of_int n);
3528 gotopage1 (n + conf.pagebias - 1) 0;
3531 let pageentry text = function [@warning "-4"]
3532 | Keys.Ascii 'g' -> TEdone text
3533 | key -> intentry text key
3535 let text = String.make 1 c in
3536 enttext (":", text, Some (onhist state.hists.pag),
3537 pageentry, ondone, true)
3539 | Ascii 'b' ->
3540 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3541 postRedisplay "toggle scrollbar";
3543 | Ascii 'B' ->
3544 state.bzoom <- not state.bzoom;
3545 state.rects <- [];
3546 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3548 | Ascii 'l' ->
3549 conf.hlinks <- not conf.hlinks;
3550 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3551 postRedisplay "toggle highlightlinks";
3553 | Ascii 'F' ->
3554 if conf.angle mod 360 = 0
3555 then (
3556 state.glinks <- true;
3557 let mode = state.mode in
3558 state.mode <-
3559 Textentry (
3560 (":", E.s, None, linknentry, linknact gotounder, false),
3561 (fun _ ->
3562 state.glinks <- false;
3563 state.mode <- mode)
3565 state.text <- E.s;
3566 postRedisplay "view:linkent(F)"
3568 else impmsg "hint mode does not work under rotation"
3570 | Ascii 'y' ->
3571 state.glinks <- true;
3572 let mode = state.mode in
3573 state.mode <-
3574 Textentry (
3575 (":", E.s, None, linknentry,
3576 linknact (fun under ->
3577 selstring conf.selcmd (undertext under)), false),
3578 (fun _ ->
3579 state.glinks <- false;
3580 state.mode <- mode)
3582 state.text <- E.s;
3583 postRedisplay "view:linkent"
3585 | Ascii 'a' ->
3586 begin match state.autoscroll with
3587 | Some step ->
3588 conf.autoscrollstep <- step;
3589 state.autoscroll <- None
3590 | None ->
3591 state.autoscroll <- Some conf.autoscrollstep;
3592 state.slideshow <- state.slideshow land lnot 2
3595 | Ascii 'p' when ctrl ->
3596 launchpath () (* XXX where do error messages go? *)
3598 | Ascii 'P' ->
3599 setpresentationmode (not conf.presentation);
3600 showtext ' ' ("presentation mode " ^
3601 if conf.presentation then "on" else "off");
3603 | Ascii 'f' ->
3604 if List.mem Wsi.Fullscreen state.winstate
3605 then Wsi.reshape conf.cwinw conf.cwinh
3606 else Wsi.fullscreen ()
3608 | Ascii ('p'|'N') ->
3609 search state.searchpattern false
3611 | Ascii 'n' | Fn 3 ->
3612 search state.searchpattern true
3614 | Ascii 't' ->
3615 begin match state.layout with
3616 | [] -> ()
3617 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3620 | Ascii ' ' -> nextpage ()
3621 | Delete -> prevpage ()
3622 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3624 | Ascii 'w' ->
3625 begin match state.layout with
3626 | [] -> ()
3627 | l :: _ ->
3628 Wsi.reshape l.pagew l.pageh;
3629 postRedisplay "w"
3632 | Ascii '\'' -> enterbookmarkmode ()
3633 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3634 | Ascii 'i' -> enterinfomode ()
3635 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3637 | Ascii 'm' ->
3638 let ondone s =
3639 match state.layout with
3640 | l :: _ when nonemptystr s ->
3641 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3642 | _ -> ()
3644 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3646 | Ascii '~' ->
3647 quickbookmark ();
3648 showtext ' ' "Quick bookmark added";
3650 | Ascii 'x' -> state.roam ()
3652 | Ascii ('<'|'>' as c) ->
3653 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3655 | Ascii ('['|']' as c) ->
3656 conf.colorscale <-
3657 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3658 postRedisplay "brightness";
3660 | Ascii 'c' when state.mode = View ->
3661 if Wsi.withalt mask
3662 then (
3663 if conf.zoom > 1.0
3664 then
3665 let m = (state.winw - state.w) / 2 in
3666 gotoxy m state.y
3668 else
3669 let (c, a, b), z =
3670 match state.prevcolumns with
3671 | None -> (1, 0, 0), 1.0
3672 | Some (columns, z) ->
3673 let cab =
3674 match columns with
3675 | Csplit (c, _) -> -c, 0, 0
3676 | Cmulti ((c, a, b), _) -> c, a, b
3677 | Csingle _ -> 1, 0, 0
3679 cab, z
3681 setcolumns View c a b;
3682 setzoom z
3684 | Down | Up when ctrl && Wsi.withshift mask ->
3685 let zoom, x = state.prevzoom in
3686 setzoom zoom;
3687 state.x <- x;
3689 | Ascii 'k' | Up ->
3690 begin match state.autoscroll with
3691 | None ->
3692 begin match state.mode with
3693 | Birdseye beye -> upbirdseye 1 beye
3694 | Textentry _ | View | LinkNav _ ->
3695 if ctrl
3696 then gotoxy state.x (clamp ~-(state.winh/2))
3697 else (
3698 if not (Wsi.withshift mask) && conf.presentation
3699 then prevpage ()
3700 else gotoxy state.x (clamp (-conf.scrollstep))
3703 | Some n -> setautoscrollspeed n false
3706 | Ascii 'j' | Down ->
3707 begin match state.autoscroll with
3708 | None ->
3709 begin match state.mode with
3710 | Birdseye beye -> downbirdseye 1 beye
3711 | Textentry _ | View | LinkNav _ ->
3712 if ctrl
3713 then gotoxy state.x (clamp (state.winh/2))
3714 else (
3715 if not (Wsi.withshift mask) && conf.presentation
3716 then nextpage ()
3717 else gotoxy state.x (clamp (conf.scrollstep))
3720 | Some n -> setautoscrollspeed n true
3723 | Left | Right when not (Wsi.withalt mask) ->
3724 if canpan ()
3725 then
3726 let dx =
3727 if ctrl
3728 then state.winw / 2
3729 else conf.hscrollstep
3731 let dx =
3732 let pv = Wsi.kc2kt key in
3733 if pv = Keys.Left then dx else -dx
3735 gotoxy (panbound (state.x + dx)) state.y
3736 else (
3737 state.text <- E.s;
3738 postRedisplay "left/right"
3741 | Prior ->
3742 let y =
3743 if ctrl
3744 then
3745 match state.layout with
3746 | [] -> state.y
3747 | l :: _ -> state.y - l.pagey
3748 else clamp (pgscale (-state.winh))
3750 gotoxy state.x y
3752 | Next ->
3753 let y =
3754 if ctrl
3755 then
3756 match List.rev state.layout with
3757 | [] -> state.y
3758 | l :: _ -> getpagey l.pageno
3759 else clamp (pgscale state.winh)
3761 gotoxy state.x y
3763 | Ascii 'g' | Home ->
3764 addnav ();
3765 gotoxy 0 0
3766 | Ascii 'G' | End ->
3767 addnav ();
3768 gotoxy 0 (clamp state.maxy)
3770 | Right when Wsi.withalt mask ->
3771 addnavnorc ();
3772 gotoxy state.x (getnav 1)
3773 | Left when Wsi.withalt mask ->
3774 addnavnorc ();
3775 gotoxy state.x (getnav ~-1)
3777 | Ascii 'r' ->
3778 reload ()
3780 | Ascii 'v' when conf.debug ->
3781 state.rects <- [];
3782 List.iter (fun l ->
3783 match getopaque l.pageno with
3784 | None -> ()
3785 | Some opaque ->
3786 let x0, y0, x1, y1 = pagebbox opaque in
3787 let rect = (float x0, float y0,
3788 float x1, float y0,
3789 float x1, float y1,
3790 float x0, float y1) in
3791 debugrect rect;
3792 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3793 state.rects <- (l.pageno, color, rect) :: state.rects;
3794 ) state.layout;
3795 postRedisplay "v";
3797 | Ascii '|' ->
3798 let mode = state.mode in
3799 let cmd = ref E.s in
3800 let onleave = function
3801 | Cancel -> state.mode <- mode
3802 | Confirm ->
3803 List.iter (fun l ->
3804 match getopaque l.pageno with
3805 | Some opaque -> pipesel opaque !cmd
3806 | None -> ()) state.layout;
3807 state.mode <- mode
3809 let ondone s =
3810 cbput state.hists.sel s;
3811 cmd := s
3813 let te =
3814 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3816 postRedisplay "|";
3817 state.mode <- Textentry (te, onleave);
3819 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3820 vlog "huh? %s" (Wsi.keyname key)
3823 let linknavkeyboard key mask linknav =
3824 let pv = Wsi.kc2kt key in
3825 let getpage pageno =
3826 let rec loop = function
3827 | [] -> None
3828 | l :: _ when l.pageno = pageno -> Some l
3829 | _ :: rest -> loop rest
3830 in loop state.layout
3832 let doexact (pageno, n) =
3833 match getopaque pageno, getpage pageno with
3834 | Some opaque, Some l ->
3835 if pv = Keys.Enter
3836 then
3837 let under = getlink opaque n in
3838 postRedisplay "link gotounder";
3839 gotounder under;
3840 state.mode <- View;
3841 else
3842 let opt, dir =
3843 let open Keys in
3844 match pv with
3845 | Home -> Some (findlink opaque LDfirst), -1
3846 | End -> Some (findlink opaque LDlast), 1
3847 | Left -> Some (findlink opaque (LDleft n)), -1
3848 | Right -> Some (findlink opaque (LDright n)), 1
3849 | Up -> Some (findlink opaque (LDup n)), -1
3850 | Down -> Some (findlink opaque (LDdown n)), 1
3851 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3852 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3854 let pwl l dir =
3855 begin match findpwl l.pageno dir with
3856 | Pwlnotfound -> ()
3857 | Pwl pageno ->
3858 let notfound dir =
3859 state.mode <- LinkNav (Ltgendir dir);
3860 let y, h = getpageyh pageno in
3861 let y =
3862 if dir < 0
3863 then y + h - state.winh
3864 else y
3866 gotoxy state.x y
3868 begin match getopaque pageno, getpage pageno with
3869 | Some opaque, Some _ ->
3870 let link =
3871 let ld = if dir > 0 then LDfirst else LDlast in
3872 findlink opaque ld
3874 begin match link with
3875 | Lfound m ->
3876 showlinktype (getlink opaque m);
3877 state.mode <- LinkNav (Ltexact (pageno, m));
3878 postRedisplay "linknav jpage";
3879 | Lnotfound -> notfound dir
3880 end;
3881 | _ -> notfound dir
3882 end;
3883 end;
3885 begin match opt with
3886 | Some Lnotfound -> pwl l dir;
3887 | Some (Lfound m) ->
3888 if m = n
3889 then pwl l dir
3890 else (
3891 let _, y0, _, y1 = getlinkrect opaque m in
3892 if y0 < l.pagey
3893 then gotopage1 l.pageno y0
3894 else (
3895 let d = fstate.fontsize + 1 in
3896 if y1 - l.pagey > l.pagevh - d
3897 then gotopage1 l.pageno (y1 - state.winh + d)
3898 else postRedisplay "linknav";
3900 showlinktype (getlink opaque m);
3901 state.mode <- LinkNav (Ltexact (l.pageno, m));
3904 | None -> viewkeyboard key mask
3905 end;
3906 | _ -> viewkeyboard key mask
3908 if pv = Keys.Insert
3909 then (
3910 begin match linknav with
3911 | Ltexact pa -> state.lnava <- Some pa
3912 | Ltgendir _ | Ltnotready _ -> ()
3913 end;
3914 state.mode <- View;
3915 postRedisplay "leave linknav"
3917 else
3918 match linknav with
3919 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3920 | Ltexact exact -> doexact exact
3923 let keyboard key mask =
3924 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3925 then wcmd "interrupt"
3926 else state.uioh <- state.uioh#key key mask
3929 let birdseyekeyboard key mask
3930 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3931 let incr =
3932 match conf.columns with
3933 | Csingle _ -> 1
3934 | Cmulti ((c, _, _), _) -> c
3935 | Csplit _ -> error "bird's eye split mode"
3937 let pgh layout = List.fold_left
3938 (fun m l -> max l.pageh m) state.winh layout in
3939 let open Keys in
3940 match Wsi.kc2kt key with
3941 | Ascii 'l' when Wsi.withctrl mask ->
3942 let y, h = getpageyh pageno in
3943 let top = (state.winh - h) / 2 in
3944 gotoxy state.x (max 0 (y - top))
3945 | Enter -> leavebirdseye beye false
3946 | Escape -> leavebirdseye beye true
3947 | Up -> upbirdseye incr beye
3948 | Down -> downbirdseye incr beye
3949 | Left -> upbirdseye 1 beye
3950 | Right -> downbirdseye 1 beye
3952 | Prior ->
3953 begin match state.layout with
3954 | l :: _ ->
3955 if l.pagey != 0
3956 then (
3957 state.mode <- Birdseye (
3958 oconf, leftx, l.pageno, hooverpageno, anchor
3960 gotopage1 l.pageno 0;
3962 else (
3963 let layout = layout state.x (state.y-state.winh)
3964 state.winw
3965 (pgh state.layout) in
3966 match layout with
3967 | [] -> gotoxy state.x (clamp (-state.winh))
3968 | l :: _ ->
3969 state.mode <- Birdseye (
3970 oconf, leftx, l.pageno, hooverpageno, anchor
3972 gotopage1 l.pageno 0
3975 | [] -> gotoxy state.x (clamp (-state.winh))
3976 end;
3978 | Next ->
3979 begin match List.rev state.layout with
3980 | l :: _ ->
3981 let layout = layout state.x
3982 (state.y + (pgh state.layout))
3983 state.winw state.winh in
3984 begin match layout with
3985 | [] ->
3986 let incr = l.pageh - l.pagevh in
3987 if incr = 0
3988 then (
3989 state.mode <-
3990 Birdseye (
3991 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
3993 postRedisplay "birdseye pagedown";
3995 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
3997 | l :: _ ->
3998 state.mode <-
3999 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4000 gotopage1 l.pageno 0;
4003 | [] -> gotoxy state.x (clamp state.winh)
4004 end;
4006 | Home ->
4007 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4008 gotopage1 0 0
4010 | End ->
4011 let pageno = state.pagecount - 1 in
4012 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4013 if not (pagevisible state.layout pageno)
4014 then
4015 let h =
4016 match List.rev state.pdims with
4017 | [] -> state.winh
4018 | (_, _, h, _) :: _ -> h
4020 gotoxy
4021 state.x
4022 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4023 else postRedisplay "birdseye end";
4025 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4028 let drawpage l =
4029 let color =
4030 match state.mode with
4031 | Textentry _ -> scalecolor 0.4
4032 | LinkNav _ | View -> scalecolor 1.0
4033 | Birdseye (_, _, pageno, hooverpageno, _) ->
4034 if l.pageno = hooverpageno
4035 then scalecolor 0.9
4036 else (
4037 if l.pageno = pageno
4038 then (
4039 let c = scalecolor 1.0 in
4040 GlDraw.color c;
4041 GlDraw.line_width 3.0;
4042 let dispx = l.pagedispx in
4043 linerect
4044 (float (dispx-1)) (float (l.pagedispy-1))
4045 (float (dispx+l.pagevw+1))
4046 (float (l.pagedispy+l.pagevh+1));
4047 GlDraw.line_width 1.0;
4050 else scalecolor 0.8
4053 drawtiles l color;
4056 let postdrawpage l linkindexbase =
4057 match getopaque l.pageno with
4058 | Some opaque ->
4059 if tileready l l.pagex l.pagey
4060 then
4061 let x = l.pagedispx - l.pagex
4062 and y = l.pagedispy - l.pagey in
4063 let hlmask =
4064 match conf.columns with
4065 | Csingle _ | Cmulti _ ->
4066 (if conf.hlinks then 1 else 0)
4067 + (if state.glinks
4068 && not (isbirdseye state.mode) then 2 else 0)
4069 | Csplit _ -> 0
4071 let s =
4072 match state.mode with
4073 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4074 | Textentry _
4075 | Birdseye _
4076 | View
4077 | LinkNav _ -> E.s
4079 Hashtbl.find_all state.prects l.pageno |>
4080 List.iter (fun vals -> drawprect opaque x y vals);
4081 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4082 if n < 0
4083 then (Glutils.redisplay := true; 0)
4084 else n
4085 else 0
4086 | _ -> 0
4089 let scrollindicator () =
4090 let sbw, ph, sh = state.uioh#scrollph in
4091 let sbh, pw, sw = state.uioh#scrollpw in
4093 let x0,x1,hx0 =
4094 if conf.leftscroll
4095 then (0, sbw, sbw)
4096 else ((state.winw - sbw), state.winw, 0)
4099 Gl.enable `blend;
4100 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4101 let (r, g, b, alpha) = conf.sbarcolor in
4102 GlDraw.color (r, g, b) ~alpha;
4103 filledrect (float x0) 0. (float x1) (float state.winh);
4104 filledrect
4105 (float hx0) (float (state.winh - sbh))
4106 (float (hx0 + state.winw)) (float state.winh);
4107 let (r, g, b, alpha) = conf.sbarhndlcolor in
4108 GlDraw.color (r, g, b) ~alpha;
4110 filledrect (float x0) ph (float x1) (ph +. sh);
4111 let pw = pw +. float hx0 in
4112 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4113 Gl.disable `blend;
4116 let showsel () =
4117 match state.mstate with
4118 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4119 | Msel ((x0, y0), (x1, y1)) ->
4120 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4121 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4122 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4123 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4126 let showrects = function
4127 | [] -> ()
4128 | rects ->
4129 Gl.enable `blend;
4130 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4131 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4132 List.iter
4133 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4134 List.iter (fun l ->
4135 if l.pageno = pageno
4136 then (
4137 let dx = float (l.pagedispx - l.pagex) in
4138 let dy = float (l.pagedispy - l.pagey) in
4139 let r, g, b, alpha = c in
4140 GlDraw.color (r, g, b) ~alpha;
4141 filledrect2
4142 (x0+.dx) (y0+.dy)
4143 (x1+.dx) (y1+.dy)
4144 (x3+.dx) (y3+.dy)
4145 (x2+.dx) (y2+.dy);
4147 ) state.layout
4148 ) rects;
4149 Gl.disable `blend;
4152 let display () =
4153 GlDraw.color (scalecolor2 conf.bgcolor);
4154 GlClear.color (scalecolor2 conf.bgcolor);
4155 GlClear.clear [`color];
4156 List.iter drawpage state.layout;
4157 let rects =
4158 match state.mode with
4159 | LinkNav (Ltexact (pageno, linkno)) ->
4160 begin match getopaque pageno with
4161 | Some opaque ->
4162 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4163 let color = (0.0, 0.0, 0.5, 0.5) in
4164 (pageno, color,
4165 (float x0, float y0,
4166 float x1, float y0,
4167 float x1, float y1,
4168 float x0, float y1)
4169 ) :: state.rects
4170 | None -> state.rects
4172 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4173 | Birdseye _
4174 | Textentry _
4175 | View -> state.rects
4177 showrects rects;
4178 let rec postloop linkindexbase = function
4179 | l :: rest ->
4180 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4181 postloop linkindexbase rest
4182 | [] -> ()
4184 showsel ();
4185 postloop 0 state.layout;
4186 state.uioh#display;
4187 begin match state.mstate with
4188 | Mzoomrect ((x0, y0), (x1, y1)) ->
4189 Gl.enable `blend;
4190 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4191 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4192 filledrect (float x0) (float y0) (float x1) (float y1);
4193 Gl.disable `blend;
4194 | Msel _
4195 | Mpan _
4196 | Mscrolly | Mscrollx
4197 | Mzoom _
4198 | Mnone -> ()
4199 end;
4200 enttext ();
4201 scrollindicator ();
4202 Wsi.swapb ();
4205 let zoomrect x y x1 y1 =
4206 let x0 = min x x1
4207 and x1 = max x x1
4208 and y0 = min y y1 in
4209 let zoom = (float state.w) /. float (x1 - x0) in
4210 let margin =
4211 let simple () =
4212 if state.w < state.winw
4213 then (state.winw - state.w) / 2
4214 else 0
4216 match conf.fitmodel with
4217 | FitWidth | FitProportional -> simple ()
4218 | FitPage ->
4219 match conf.columns with
4220 | Csplit _ ->
4221 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4222 | Cmulti _ | Csingle _ -> simple ()
4224 gotoxy ((state.x + margin) - x0) (state.y + y0);
4225 state.anchor <- getanchor ();
4226 setzoom zoom;
4227 resetmstate ();
4230 let annot inline x y =
4231 match unproject x y with
4232 | Some (opaque, n, ux, uy) ->
4233 let add text =
4234 addannot opaque ux uy text;
4235 wcmd "freepage %s" (~> opaque);
4236 Hashtbl.remove state.pagemap (n, state.gen);
4237 flushtiles ();
4238 gotoxy state.x state.y
4240 if inline
4241 then
4242 let ondone s = add s in
4243 let mode = state.mode in
4244 state.mode <- Textentry (
4245 ("annotation: ", E.s, None, textentry, ondone, true),
4246 fun _ -> state.mode <- mode);
4247 state.text <- E.s;
4248 enttext ();
4249 postRedisplay "annot"
4250 else add @@ getusertext E.s
4251 | _ -> ()
4254 let zoomblock x y =
4255 let g opaque l px py =
4256 match rectofblock opaque px py with
4257 | Some a ->
4258 let x0 = a.(0) -. 20. in
4259 let x1 = a.(1) +. 20. in
4260 let y0 = a.(2) -. 20. in
4261 let zoom = (float state.w) /. (x1 -. x0) in
4262 let pagey = getpagey l.pageno in
4263 let margin = (state.w - l.pagew)/2 in
4264 let nx = -truncate x0 - margin in
4265 gotoxy nx (pagey + truncate y0);
4266 state.anchor <- getanchor ();
4267 setzoom zoom;
4268 None
4269 | None -> None
4271 match conf.columns with
4272 | Csplit _ ->
4273 impmsg "block zooming does not work properly in split columns mode"
4274 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4277 let scrollx x =
4278 let winw = state.winw - 1 in
4279 let s = float x /. float winw in
4280 let destx = truncate (float (state.w + winw) *. s) in
4281 gotoxy (winw - destx) state.y;
4282 state.mstate <- Mscrollx;
4285 let scrolly y =
4286 let s = float y /. float state.winh in
4287 let desty = truncate (s *. float (maxy ())) in
4288 gotoxy state.x desty;
4289 state.mstate <- Mscrolly;
4292 let viewmulticlick clicks x y mask =
4293 let g opaque l px py =
4294 let mark =
4295 match clicks with
4296 | 2 -> Mark_word
4297 | 3 -> Mark_line
4298 | 4 -> Mark_block
4299 | _ -> Mark_page
4301 if markunder opaque px py mark
4302 then (
4303 Some (fun () ->
4304 let dopipe cmd =
4305 match getopaque l.pageno with
4306 | None -> ()
4307 | Some opaque -> pipesel opaque cmd
4309 state.roam <- (fun () -> dopipe conf.paxcmd);
4310 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4313 else None
4315 postRedisplay "viewmulticlick";
4316 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4319 let canselect () =
4320 match conf.columns with
4321 | Csplit _ -> false
4322 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4325 let viewmouse button down x y mask =
4326 match button with
4327 | n when (n == 4 || n == 5) && not down ->
4328 if Wsi.withctrl mask
4329 then (
4330 let incr =
4331 if n = 5
4332 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4333 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4335 let fx, fy =
4336 match state.mstate with
4337 | Mzoom (oldn, _, pos) when n = oldn -> pos
4338 | Mzoomrect _ | Mnone | Mpan _
4339 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4341 let zoom = conf.zoom -. incr in
4342 state.mstate <- Mzoom (n, 0, (x, y));
4343 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4344 then pivotzoom ~x ~y zoom
4345 else pivotzoom zoom
4347 else (
4348 match state.autoscroll with
4349 | Some step -> setautoscrollspeed step (n=4)
4350 | None ->
4351 if conf.wheelbypage || conf.presentation
4352 then (
4353 if n = 4
4354 then prevpage ()
4355 else nextpage ()
4357 else
4358 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4359 let incr = incr * 2 in
4360 let y = clamp incr in
4361 gotoxy state.x y
4364 | n when (n = 6 || n = 7) && not down && canpan () ->
4365 let x =
4366 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4367 gotoxy x state.y
4369 | 1 when Wsi.withshift mask ->
4370 state.mstate <- Mnone;
4371 if not down
4372 then (
4373 match unproject x y with
4374 | None -> ()
4375 | Some (_, pageno, ux, uy) ->
4376 let cmd = Printf.sprintf
4377 "%s %s %d %d %d"
4378 conf.stcmd state.path pageno ux uy
4380 match spawn cmd [] with
4381 | exception exn ->
4382 impmsg "execution of synctex command(%S) failed: %S"
4383 conf.stcmd @@ exntos exn
4384 | _pid -> ()
4387 | 1 when Wsi.withctrl mask ->
4388 if down
4389 then (
4390 Wsi.setcursor Wsi.CURSOR_FLEUR;
4391 state.mstate <- Mpan (x, y)
4393 else state.mstate <- Mnone
4395 | 3 ->
4396 if down
4397 then (
4398 if Wsi.withshift mask
4399 then (
4400 annot conf.annotinline x y;
4401 postRedisplay "addannot"
4403 else
4404 let p = (x, y) in
4405 Wsi.setcursor Wsi.CURSOR_CYCLE;
4406 state.mstate <- Mzoomrect (p, p)
4408 else (
4409 match state.mstate with
4410 | Mzoomrect ((x0, y0), _) ->
4411 if abs (x-x0) > 10 && abs (y - y0) > 10
4412 then zoomrect x0 y0 x y
4413 else (
4414 resetmstate ();
4415 postRedisplay "kill accidental zoom rect";
4417 | Msel _
4418 | Mpan _
4419 | Mscrolly | Mscrollx
4420 | Mzoom _
4421 | Mnone -> resetmstate ()
4424 | 1 when vscrollhit x ->
4425 if down
4426 then
4427 let _, position, sh = state.uioh#scrollph in
4428 if y > truncate position && y < truncate (position +. sh)
4429 then state.mstate <- Mscrolly
4430 else scrolly y
4431 else state.mstate <- Mnone
4433 | 1 when y > state.winh - hscrollh () ->
4434 if down
4435 then
4436 let _, position, sw = state.uioh#scrollpw in
4437 if x > truncate position && x < truncate (position +. sw)
4438 then state.mstate <- Mscrollx
4439 else scrollx x
4440 else state.mstate <- Mnone
4442 | 1 when state.bzoom -> if not down then zoomblock x y
4444 | 1 ->
4445 let dest = if down then getunder x y else Unone in
4446 begin match dest with
4447 | Ulinkuri _ -> gotounder dest
4448 | Unone when down ->
4449 Wsi.setcursor Wsi.CURSOR_FLEUR;
4450 state.mstate <- Mpan (x, y);
4451 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4452 | Unone | Utext _ ->
4453 if down
4454 then (
4455 if canselect ()
4456 then (
4457 state.mstate <- Msel ((x, y), (x, y));
4458 postRedisplay "mouse select";
4461 else (
4462 match state.mstate with
4463 | Mnone -> ()
4464 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4465 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4466 | Mpan _ ->
4467 Wsi.setcursor Wsi.CURSOR_INHERIT;
4468 state.mstate <- Mnone
4469 | Msel ((x0, y0), (x1, y1)) ->
4470 let rec loop = function
4471 | [] -> ()
4472 | l :: rest ->
4473 let inside =
4474 let a0 = l.pagedispy in
4475 let a1 = a0 + l.pagevh in
4476 let b0 = l.pagedispx in
4477 let b1 = b0 + l.pagevw in
4478 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4479 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4481 if inside
4482 then
4483 match getopaque l.pageno with
4484 | Some opaque ->
4485 let dosel cmd () =
4486 pipef ~closew:false "Msel"
4487 (fun w ->
4488 copysel w opaque;
4489 postRedisplay "Msel") cmd
4491 dosel conf.selcmd ();
4492 state.roam <- dosel conf.paxcmd;
4493 | None -> ()
4494 else loop rest
4496 loop state.layout;
4497 resetmstate ();
4500 | _ -> ()
4503 let birdseyemouse button down x y mask
4504 (conf, leftx, _, hooverpageno, anchor) =
4505 match button with
4506 | 1 when down ->
4507 let rec loop = function
4508 | [] -> ()
4509 | l :: rest ->
4510 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4511 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4512 then (
4513 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4515 else loop rest
4517 loop state.layout
4518 | 3 -> ()
4519 | _ -> viewmouse button down x y mask
4522 let uioh = object
4523 method display = ()
4525 method key key mask =
4526 begin match state.mode with
4527 | Textentry textentry -> textentrykeyboard key mask textentry
4528 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4529 | View -> viewkeyboard key mask
4530 | LinkNav linknav -> linknavkeyboard key mask linknav
4531 end;
4532 state.uioh
4534 method button button bstate x y mask =
4535 begin match state.mode with
4536 | LinkNav _ | View -> viewmouse button bstate x y mask
4537 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4538 | Textentry _ -> ()
4539 end;
4540 state.uioh
4542 method multiclick clicks x y mask =
4543 begin match state.mode with
4544 | LinkNav _ | View -> viewmulticlick clicks x y mask
4545 | Birdseye _ | Textentry _ -> ()
4546 end;
4547 state.uioh
4549 method motion x y =
4550 begin match state.mode with
4551 | Textentry _ -> ()
4552 | View | Birdseye _ | LinkNav _ ->
4553 match state.mstate with
4554 | Mzoom _ | Mnone -> ()
4555 | Mpan (x0, y0) ->
4556 let dx = x - x0
4557 and dy = y0 - y in
4558 state.mstate <- Mpan (x, y);
4559 let x = if canpan () then panbound (state.x + dx) else state.x in
4560 let y = clamp dy in
4561 gotoxy x y
4563 | Msel (a, _) ->
4564 state.mstate <- Msel (a, (x, y));
4565 postRedisplay "motion select";
4567 | Mscrolly ->
4568 let y = min state.winh (max 0 y) in
4569 scrolly y
4571 | Mscrollx ->
4572 let x = min state.winw (max 0 x) in
4573 scrollx x
4575 | Mzoomrect (p0, _) ->
4576 state.mstate <- Mzoomrect (p0, (x, y));
4577 postRedisplay "motion zoomrect";
4578 end;
4579 state.uioh
4581 method pmotion x y =
4582 begin match state.mode with
4583 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4584 let rec loop = function
4585 | [] ->
4586 if hooverpageno != -1
4587 then (
4588 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4589 postRedisplay "pmotion birdseye no hoover";
4591 | l :: rest ->
4592 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4593 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4594 then (
4595 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4596 postRedisplay "pmotion birdseye hoover";
4598 else loop rest
4600 loop state.layout
4602 | Textentry _ -> ()
4604 | LinkNav _ | View ->
4605 match state.mstate with
4606 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4607 | Mnone ->
4608 updateunder x y;
4609 if canselect ()
4610 then
4611 match conf.pax with
4612 | None -> ()
4613 | Some past ->
4614 let now = now () in
4615 let delta = now -. past in
4616 if delta > 0.01
4617 then paxunder x y
4618 else conf.pax <- Some now
4619 end;
4620 state.uioh
4622 method infochanged _ = ()
4624 method scrollph =
4625 let maxy = maxy () in
4626 let p, h =
4627 if maxy = 0
4628 then 0.0, float state.winh
4629 else scrollph state.y maxy
4631 vscrollw (), p, h
4633 method scrollpw =
4634 let fwinw = float (state.winw - vscrollw ()) in
4635 let sw =
4636 let sw = fwinw /. float state.w in
4637 let sw = fwinw *. sw in
4638 max sw (float conf.scrollh)
4640 let position =
4641 let maxx = state.w + state.winw in
4642 let x = state.winw - state.x in
4643 let percent = float x /. float maxx in
4644 (fwinw -. sw) *. percent
4646 hscrollh (), position, sw
4648 method modehash =
4649 let modename =
4650 match state.mode with
4651 | LinkNav _ -> "links"
4652 | Textentry _ -> "textentry"
4653 | Birdseye _ -> "birdseye"
4654 | View -> "view"
4656 findkeyhash conf modename
4658 method eformsgs = true
4659 method alwaysscrolly = false
4660 method scroll dx dy =
4661 let x = if canpan () then panbound (state.x + dx) else state.x in
4662 gotoxy x (clamp (2 * dy));
4663 state.uioh
4664 method zoom z x y =
4665 pivotzoom ~x ~y (conf.zoom *. exp z);
4666 end;;
4668 let addrect pageno r g b a x0 y0 x1 y1 =
4669 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4672 let ract cmds =
4673 let cl = splitatchar cmds ' ' in
4674 let scan s fmt f =
4675 try Scanf.sscanf s fmt f
4676 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4677 cmds @@ exntos exn
4679 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4680 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4681 s pageno r g b a x0 y0 x1 y1;
4682 onpagerect
4683 pageno
4684 (fun w h ->
4685 let _,w1,h1,_ = getpagedim pageno in
4686 let sw = float w1 /. float w
4687 and sh = float h1 /. float h in
4688 let x0s = x0 *. sw
4689 and x1s = x1 *. sw
4690 and y0s = y0 *. sh
4691 and y1s = y1 *. sh in
4692 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4693 let color = (r, g, b, a) in
4694 if conf.verbose then debugrect rect;
4695 state.rects <- (pageno, color, rect) :: state.rects;
4696 postRedisplay s;
4699 match cl with
4700 | "reload", "" -> reload ()
4701 | "goto", args ->
4702 scan args "%u %f %f"
4703 (fun pageno x y ->
4704 let cmd, _ = state.geomcmds in
4705 if emptystr cmd
4706 then gotopagexy pageno x y
4707 else
4708 let f prevf () =
4709 gotopagexy pageno x y;
4710 prevf ()
4712 state.reprf <- f state.reprf
4714 | "goto1", args -> scan args "%u %f" gotopage
4715 | "gotor", args -> scan args "%S" gotoremote
4716 | "rect", args ->
4717 scan args "%u %u %f %f %f %f"
4718 (fun pageno c x0 y0 x1 y1 ->
4719 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4720 rectx "rect" pageno color x0 y0 x1 y1;
4722 | "prect", args ->
4723 scan args "%u %f %f %f %f %f %f %f %f"
4724 (fun pageno r g b alpha x0 y0 x1 y1 ->
4725 addrect pageno r g b alpha x0 y0 x1 y1;
4726 postRedisplay "prect"
4728 | "pgoto", args ->
4729 scan args "%u %f %f"
4730 (fun pageno x y ->
4731 let optopaque =
4732 match getopaque pageno with
4733 | Some opaque -> opaque
4734 | None -> ~< E.s
4736 pgoto optopaque pageno x y;
4737 let rec fixx = function
4738 | [] -> ()
4739 | l :: rest ->
4740 if l.pageno = pageno
4741 then gotoxy (state.x - l.pagedispx) state.y
4742 else fixx rest
4744 let layout =
4745 let mult =
4746 match conf.columns with
4747 | Csingle _ | Csplit _ -> 1
4748 | Cmulti ((n, _, _), _) -> n
4750 layout 0 state.y (state.winw * mult) state.winh
4752 fixx layout
4754 | "activatewin", "" -> Wsi.activatewin ()
4755 | "quit", "" -> raise Quit
4756 | "keys", keys ->
4757 begin try
4758 let l = Config.keys_of_string keys in
4759 List.iter (fun (k, m) -> keyboard k m) l
4760 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4761 cmds @@ exntos exn
4763 | "clearrects", "" ->
4764 Hashtbl.clear state.prects;
4765 postRedisplay "clearrects"
4766 | _ ->
4767 adderrfmt "remote command"
4768 "error processing remote command: %S\n" cmds;
4771 let remote =
4772 let scratch = Bytes.create 80 in
4773 let buf = Buffer.create 80 in
4774 fun fd ->
4775 match tempfailureretry (Unix.read fd scratch 0) 80 with
4776 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4777 | 0 ->
4778 Unix.close fd;
4779 if Buffer.length buf > 0
4780 then (
4781 let s = Buffer.contents buf in
4782 Buffer.clear buf;
4783 ract s;
4785 None
4786 | n ->
4787 let rec eat ppos =
4788 let nlpos =
4789 match Bytes.index_from scratch ppos '\n' with
4790 | pos -> if pos >= n then -1 else pos
4791 | exception Not_found -> -1
4793 if nlpos >= 0
4794 then (
4795 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4796 let s = Buffer.contents buf in
4797 Buffer.clear buf;
4798 ract s;
4799 eat (nlpos+1);
4801 else (
4802 Buffer.add_subbytes buf scratch ppos (n-ppos);
4803 Some fd
4805 in eat 0
4808 let remoteopen path =
4809 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4810 with exn ->
4811 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4812 None
4815 let () =
4816 let gcconfig = ref false in
4817 let trimcachepath = ref E.s in
4818 let rcmdpath = ref E.s in
4819 let pageno = ref None in
4820 let openlast = ref false in
4821 let doreap = ref false in
4822 let csspath = ref None in
4823 selfexec := Sys.executable_name;
4824 Arg.parse
4825 (Arg.align
4826 [("-p", Arg.String (fun s -> state.password <- s),
4827 "<password> Set password");
4829 ("-f", Arg.String
4830 (fun s ->
4831 Config.fontpath := s;
4832 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4834 "<path> Set path to the user interface font");
4836 ("-c", Arg.String
4837 (fun s ->
4838 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4839 Config.confpath := s),
4840 "<path> Set path to the configuration file");
4842 ("-last", Arg.Set openlast, " Open last document");
4844 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4845 "<page-number> Jump to page");
4847 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4848 "<path> Set path to the trim cache file");
4850 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4851 "<named-destination> Set named destination");
4853 ("-remote", Arg.String (fun s -> rcmdpath := s),
4854 "<path> Set path to the source of remote commands");
4856 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4858 ("-v", Arg.Unit (fun () ->
4859 Printf.printf
4860 "%s\nconfiguration file: %s\n"
4861 (Help.version ())
4862 Config.defconfpath;
4863 exit 0), " Print version and exit");
4865 ("-css", Arg.String (fun s -> csspath := Some s),
4866 "<path> Set path to the style sheet to use with EPUB/HTML");
4868 ("-origin", Arg.String (fun s -> state.origin <- s),
4869 "<origin> <undocumented>");
4871 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4872 ("-layout-height", Arg.Set_int layouth,
4873 "<height> layout height html/epub/etc (-1, 0, N)");
4876 (fun s -> state.path <- s)
4877 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4879 let histmode = emptystr state.path && not !openlast in
4881 if not (Config.load !openlast)
4882 then dolog "failed to load configuration";
4884 begin match !pageno with
4885 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4886 | None -> ()
4887 end;
4889 fillhelp ();
4890 if !gcconfig
4891 then (
4892 Config.gc ();
4893 exit 0
4896 let mu =
4897 object (self)
4898 val mutable m_clicks = 0
4899 val mutable m_click_x = 0
4900 val mutable m_click_y = 0
4901 val mutable m_lastclicktime = infinity
4903 method private cleanup =
4904 state.roam <- noroam;
4905 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4906 method expose = postRedisplay "expose"
4907 method visible v =
4908 let name =
4909 match v with
4910 | Wsi.Unobscured -> "unobscured"
4911 | Wsi.PartiallyObscured -> "partiallyobscured"
4912 | Wsi.FullyObscured -> "fullyobscured"
4914 vlog "visibility change %s" name
4915 method display = display ()
4916 method map mapped = vlog "mapped %b" mapped
4917 method reshape w h =
4918 self#cleanup;
4919 reshape w h
4920 method mouse b d x y m =
4921 if d && canselect ()
4922 then (
4924 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4926 m_click_x <- x;
4927 m_click_y <- y;
4928 if b = 1
4929 then (
4930 let t = now () in
4931 if abs x - m_click_x > 10
4932 || abs y - m_click_y > 10
4933 || abs_float (t -. m_lastclicktime) > 0.3
4934 then m_clicks <- 0;
4935 m_clicks <- m_clicks + 1;
4936 m_lastclicktime <- t;
4937 if m_clicks = 1
4938 then (
4939 self#cleanup;
4940 postRedisplay "cleanup";
4941 state.uioh <- state.uioh#button b d x y m;
4943 else state.uioh <- state.uioh#multiclick m_clicks x y m
4945 else (
4946 self#cleanup;
4947 m_clicks <- 0;
4948 m_lastclicktime <- infinity;
4949 state.uioh <- state.uioh#button b d x y m
4952 else state.uioh <- state.uioh#button b d x y m
4953 method motion x y =
4954 state.mpos <- (x, y);
4955 state.uioh <- state.uioh#motion x y
4956 method pmotion x y =
4957 state.mpos <- (x, y);
4958 state.uioh <- state.uioh#pmotion x y
4959 method key k m =
4960 vlog "k=%#x m=%#x" k m;
4961 let mascm = m land (
4962 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
4963 ) in
4964 let keyboard k m =
4965 let x = state.x and y = state.y in
4966 keyboard k m;
4967 if x != state.x || y != state.y then self#cleanup
4969 match state.keystate with
4970 | KSnone ->
4971 let km = k, mascm in
4972 begin
4973 match
4974 let modehash = state.uioh#modehash in
4975 try Hashtbl.find modehash km
4976 with Not_found ->
4977 try Hashtbl.find (findkeyhash conf "global") km
4978 with Not_found -> KMinsrt (k, m)
4979 with
4980 | KMinsrt (k, m) -> keyboard k m
4981 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
4982 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
4984 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
4985 List.iter (fun (k, m) -> keyboard k m) insrt;
4986 state.keystate <- KSnone
4987 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
4988 state.keystate <- KSinto (keys, insrt)
4989 | KSinto _ -> state.keystate <- KSnone
4991 method enter x y =
4992 state.mpos <- (x, y);
4993 state.uioh <- state.uioh#pmotion x y
4994 method leave = state.mpos <- (-1, -1)
4995 method winstate wsl = state.winstate <- wsl
4996 method quit : 'a. 'a = raise Quit
4997 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
4998 method zoom z x y = state.uioh#zoom z x y
4999 method opendoc path =
5000 state.mode <- View;
5001 state.uioh <- uioh;
5002 postRedisplay "opendoc";
5003 opendoc path state.password
5006 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5007 state.wsfd <- wsfd;
5009 if not @@ List.exists GlMisc.check_extension
5010 [ "GL_ARB_texture_rectangle"
5011 ; "GL_EXT_texture_recangle"
5012 ; "GL_NV_texture_rectangle" ]
5013 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5015 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
5016 then (
5017 defconf.sliceheight <- 1024;
5018 defconf.texcount <- 32;
5019 defconf.usepbo <- true;
5022 let cs, ss =
5023 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5024 | exception exn ->
5025 dolog "socketpair failed: %s" @@ exntos exn;
5026 exit 1
5027 | (r, w) ->
5028 cloexec r;
5029 cloexec w;
5030 r, w
5033 setcheckers conf.checkers;
5034 begin match !csspath with
5035 | None -> ()
5036 | Some "" -> conf.css <- E.s
5037 | Some path ->
5038 let css = filecontents path in
5039 let l = String.length css in
5040 conf.css <-
5041 if substratis css (l-2) "\r\n"
5042 then String.sub css 0 (l-2)
5043 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
5044 end;
5045 init cs (
5046 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5047 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5048 !Config.fontpath, !trimcachepath
5050 List.iter GlArray.enable [`texture_coord; `vertex];
5051 state.ss <- ss;
5052 reshape ~firsttime:true winw winh;
5053 state.uioh <- uioh;
5054 if histmode
5055 then (
5056 Wsi.settitle "llpp (history)";
5057 enterhistmode ();
5059 else (
5060 state.text <- "Opening " ^ (mbtoutf8 state.path);
5061 opendoc state.path state.password;
5063 display ();
5064 Wsi.mapwin ();
5065 Wsi.setcursor Wsi.CURSOR_INHERIT;
5066 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5068 let rec reap () =
5069 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5070 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5071 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5072 | 0, _ -> ()
5073 | _pid, _status -> reap ()
5075 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5077 let optrfd =
5078 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5081 let rec loop deadline =
5082 if !doreap
5083 then (
5084 doreap := false;
5085 reap ()
5087 let r = [state.ss; state.wsfd] in
5088 let r =
5089 match !optrfd with
5090 | None -> r
5091 | Some fd -> fd :: r
5093 if !redisplay
5094 then (
5095 Glutils.redisplay := false;
5096 display ();
5098 let timeout =
5099 let now = now () in
5100 if deadline > now
5101 then (
5102 if deadline = infinity
5103 then ~-.1.0
5104 else max 0.0 (deadline -. now)
5106 else 0.0
5108 let r, _, _ =
5109 try Unix.select r [] [] timeout
5110 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5112 begin match r with
5113 | [] ->
5114 let newdeadline =
5115 match state.autoscroll with
5116 | Some step when step != 0 ->
5117 if state.slideshow land 1 = 1
5118 then (
5119 if state.slideshow land 2 = 0
5120 then state.slideshow <- state.slideshow lor 2
5121 else if step < 0 then prevpage () else nextpage ();
5122 deadline +. (float (abs step))
5124 else
5125 let y = state.y + step in
5126 let fy = if conf.maxhfit then state.winh else 0 in
5127 let y =
5128 if y < 0
5129 then state.maxy - fy
5130 else if y >= state.maxy - fy then 0 else y
5132 gotoxy state.x y;
5133 deadline +. 0.01
5134 | _ -> infinity
5136 loop newdeadline
5138 | l ->
5139 let rec checkfds = function
5140 | [] -> ()
5141 | fd :: rest when fd = state.ss ->
5142 let cmd = rcmd state.ss in
5143 act cmd;
5144 checkfds rest
5146 | fd :: rest when fd = state.wsfd ->
5147 Wsi.readresp fd;
5148 checkfds rest
5150 | fd :: rest when Some fd = !optrfd ->
5151 begin match remote fd with
5152 | None -> optrfd := remoteopen !rcmdpath;
5153 | opt -> optrfd := opt
5154 end;
5155 checkfds rest
5157 | _ :: rest ->
5158 dolog "select returned unknown descriptor";
5159 checkfds rest
5161 checkfds l;
5162 let newdeadline =
5163 let deadline1 =
5164 if deadline = infinity
5165 then now () +. 0.01
5166 else deadline
5168 match state.autoscroll with
5169 | Some step when step != 0 -> deadline1
5170 | _ -> infinity
5172 loop newdeadline
5173 end;
5175 match loop infinity with
5176 | exception Quit ->
5177 Config.save leavebirdseye;
5178 if hasunsavedchanges ()
5179 then save ()
5180 | _ -> error "umpossible - infinity reached"