Add some timings
[llpp.git] / main.ml
blob0b053ac2a885bbb0b2d0f41ce6c272d609bc971e
1 open Utils;;
2 open Config;;
3 open Glutils;;
4 open Listview;;
5 open Ffi;;
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 ((conf.scrollb land scrollbhv != 0) && (state.w > state.winw))
25 || state.uioh#alwaysscrolly
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 launchpath () =
37 if emptystr conf.pathlauncher
38 then dolog "%s" state.path
39 else (
40 let command =
41 Str.global_replace Utils.Re.percent state.path conf.pathlauncher in
42 match spawn command [] with
43 | _pid -> ()
44 | exception exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
48 let getopaque pageno =
49 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
50 with Not_found -> None
53 let pagetranslatepoint l x y =
54 let dy = y - l.pagedispy in
55 let y = dy + l.pagey in
56 let dx = x - l.pagedispx in
57 let x = dx + l.pagex in
58 (x, y);
61 let onppundermouse g x y d =
62 let rec f = function
63 | l :: rest ->
64 begin match getopaque l.pageno with
65 | Some opaque ->
66 let x0 = l.pagedispx in
67 let x1 = x0 + l.pagevw in
68 let y0 = l.pagedispy in
69 let y1 = y0 + l.pagevh in
70 if y >= y0 && y <= y1 && x >= x0 && x <= x1
71 then
72 let px, py = pagetranslatepoint l x y in
73 match g opaque l px py with
74 | Some res -> res
75 | None -> f rest
76 else f rest
77 | _ -> f rest
78 end
79 | [] -> d
81 f state.layout
84 let getunder x y =
85 let g opaque l px py =
86 if state.bzoom
87 then (
88 match rectofblock opaque px py with
89 | Some [|x0;x1;y0;y1|] ->
90 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
91 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
92 state.rects <- [l.pageno, color, rect];
93 postRedisplay "getunder";
94 | _ -> ()
96 let under = whatsunder opaque px py in
97 if under = Unone then None else Some under
99 onppundermouse g x y Unone
102 let unproject x y =
103 let g opaque l x y =
104 match unproject opaque x y with
105 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
106 | None -> None
108 onppundermouse g x y None;
111 let showtext c s =
112 state.text <- Printf.sprintf "%c%s" c s;
113 postRedisplay "showtext";
116 let impmsg fmt = Format.ksprintf (fun s -> showtext '!' s) fmt;;
118 let pipesel opaque cmd =
119 if hassel opaque
120 then pipef ~closew:false "pipesel"
121 (fun w ->
122 copysel w opaque;
123 postRedisplay "pipesel"
124 ) cmd
127 let paxunder x y =
128 let g opaque l px py =
129 if markunder opaque px py conf.paxmark
130 then
131 Some (fun () ->
132 match getopaque l.pageno with
133 | None -> ()
134 | Some opaque -> pipesel opaque conf.paxcmd
136 else None
138 postRedisplay "paxunder";
139 if conf.paxmark = Mark_page
140 then
141 List.iter (fun l ->
142 match getopaque l.pageno with
143 | None -> ()
144 | Some opaque -> clearmark opaque) state.layout;
145 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
148 let undertext = function
149 | Unone -> "none"
150 | Ulinkuri s -> s
151 | Utext s -> "font: " ^ s
152 | Uannotation (opaque, slinkindex) ->
153 "annotation: " ^ getannotcontents opaque slinkindex
156 let updateunder x y =
157 match getunder x y with
158 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
159 | Ulinkuri uri ->
160 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
161 Wsi.setcursor Wsi.CURSOR_INFO
162 | Utext s ->
163 if conf.underinfo then showtext 'f' ("ont: " ^ s);
164 Wsi.setcursor Wsi.CURSOR_TEXT
165 | Uannotation _ ->
166 if conf.underinfo then showtext 'a' "nnotation";
167 Wsi.setcursor Wsi.CURSOR_INFO
170 let showlinktype under =
171 if conf.underinfo && under != Unone
172 then showtext ' ' @@ undertext under
175 let intentry_with_suffix text key =
176 let text =
177 match [@warning "-4"] key with
178 | Keys.Ascii ('0'..'9' as c) -> addchar text c
179 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
180 addchar text @@ asciilower c
181 | _ ->
182 state.text <- "invalid key";
183 text
185 TEcont text
188 let wcmd fmt =
189 let b = Buffer.create 16 in
190 Printf.kbprintf
191 (fun b ->
192 let b = Buffer.to_bytes b in
193 wcmd state.ss b @@ Bytes.length b
194 ) b fmt
197 let nogeomcmds = function
198 | s, [] -> emptystr s
199 | _ -> false
202 let layoutN ((columns, coverA, coverB), b) x y sw sh =
203 let rec fold accu n =
204 if n = Array.length b
205 then accu
206 else
207 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
208 if (vy - y) > sh &&
209 (n = coverA - 1
210 || n = state.pagecount - coverB
211 || (n - coverA) mod columns = columns - 1)
212 then accu
213 else
214 let accu =
215 if vy + h > y
216 then
217 let pagey = max 0 (y - vy) in
218 let pagedispy = if pagey > 0 then 0 else vy - y in
219 let pagedispx, pagex =
220 let pdx =
221 if n = coverA - 1 || n = state.pagecount - coverB
222 then x + (sw - w) / 2
223 else dx + xoff + x
225 if pdx < 0
226 then 0, -pdx
227 else pdx, 0
229 let pagevw =
230 let vw = sw - pagedispx in
231 let pw = w - pagex in
232 min vw pw
234 let pagevh = min (h - pagey) (sh - pagedispy) in
235 if pagevw > 0 && pagevh > 0
236 then
237 { pageno = n
238 ; pagedimno = pdimno
239 ; pagew = w
240 ; pageh = h
241 ; pagex = pagex
242 ; pagey = pagey
243 ; pagevw = pagevw
244 ; pagevh = pagevh
245 ; pagedispx = pagedispx
246 ; pagedispy = pagedispy
247 ; pagecol = 0
248 } :: accu
249 else accu
250 else accu
252 fold accu (n+1)
254 if Array.length b = 0
255 then []
256 else List.rev (fold [] (page_of_y y))
259 let layoutS (columns, b) x y sw sh =
260 let rec fold accu n =
261 if n = Array.length b
262 then accu
263 else
264 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
265 if (vy - y) > sh
266 then accu
267 else
268 let accu =
269 if vy + pageh > y
270 then
271 let x = xoff + x in
272 let pagey = max 0 (y - vy) in
273 let pagedispy = if pagey > 0 then 0 else vy - y in
274 let pagedispx, pagex =
275 if px = 0
276 then (
277 if x < 0
278 then 0, -x
279 else x, 0
281 else (
282 let px = px - x in
283 if px < 0
284 then -px, 0
285 else 0, px
288 let pagecolw = pagew/columns in
289 let pagedispx =
290 if pagecolw < sw
291 then pagedispx + ((sw - pagecolw) / 2)
292 else pagedispx
294 let pagevw =
295 let vw = sw - pagedispx in
296 let pw = pagew - pagex in
297 min vw pw
299 let pagevw = min pagevw pagecolw in
300 let pagevh = min (pageh - pagey) (sh - pagedispy) in
301 if pagevw > 0 && pagevh > 0
302 then
303 { pageno = n/columns
304 ; pagedimno = pdimno
305 ; pagew = pagew
306 ; pageh = pageh
307 ; pagex = pagex
308 ; pagey = pagey
309 ; pagevw = pagevw
310 ; pagevh = pagevh
311 ; pagedispx = pagedispx
312 ; pagedispy = pagedispy
313 ; pagecol = n mod columns
314 } :: accu
315 else accu
316 else accu
318 fold accu (n+1)
320 List.rev (fold [] 0)
323 let layout x y sw sh =
324 if nogeomcmds state.geomcmds
325 then
326 match conf.columns with
327 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
328 | Cmulti c -> layoutN c x y sw sh
329 | Csplit s -> layoutS s x y sw sh
330 else []
333 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
334 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
336 let itertiles l f =
337 let tilex = l.pagex mod conf.tilew in
338 let tiley = l.pagey mod conf.tileh in
340 let col = l.pagex / conf.tilew in
341 let row = l.pagey / conf.tileh in
343 let rec rowloop row y0 dispy h =
344 if h != 0
345 then
346 let dh = conf.tileh - y0 in
347 let dh = min h dh in
348 let rec colloop col x0 dispx w =
349 if w != 0
350 then
351 let dw = conf.tilew - x0 in
352 let dw = min w dw in
353 f col row dispx dispy x0 y0 dw dh;
354 colloop (col+1) 0 (dispx+dw) (w-dw)
356 colloop col tilex l.pagedispx l.pagevw;
357 rowloop (row+1) 0 (dispy+dh) (h-dh)
359 if l.pagevw > 0 && l.pagevh > 0
360 then rowloop row tiley l.pagedispy l.pagevh;
363 let gettileopaque l col row =
364 let key = l.pageno, state.gen, conf.colorspace,
365 conf.angle, l.pagew, l.pageh, col, row in
366 try Some (Hashtbl.find state.tilemap key)
367 with Not_found -> None
370 let puttileopaque l col row gen colorspace angle opaque size elapsed =
371 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
372 Hashtbl.add state.tilemap key (opaque, size, elapsed)
375 let drawtiles l color =
376 GlDraw.color color;
377 begintiles ();
378 let f col row x y tilex tiley w h =
379 match gettileopaque l col row with
380 | Some (opaque, _, t) ->
381 let params = x, y, w, h, tilex, tiley in
382 if conf.invert
383 then GlTex.env (`mode `blend);
384 drawtile params opaque;
385 if conf.invert
386 then GlTex.env (`mode `modulate);
387 if conf.debug
388 then (
389 endtiles ();
390 let s = Printf.sprintf "%d[%d,%d] %f sec" l.pageno col row t in
391 let w = measurestr fstate.fontsize s in
392 GlDraw.color (0.0, 0.0, 0.0);
393 filledrect
394 (float (x-2))
395 (float (y-2))
396 (float (x+2) +. w)
397 (float (y + fstate.fontsize + 2));
398 GlDraw.color color;
399 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
400 begintiles ();
403 | None ->
404 endtiles ();
405 let w = let lw = state.winw - x in min lw w
406 and h = let lh = state.winh - y in min lh h
408 if conf.invert
409 then GlTex.env (`mode `blend);
410 begin match !checkerstexid with
411 | Some id ->
412 Gl.enable `texture_2d;
413 GlTex.bind_texture ~target:`texture_2d id;
414 let x0 = float x
415 and y0 = float y
416 and x1 = float (x+w)
417 and y1 = float (y+h) in
419 let tw = float w /. 16.0
420 and th = float h /. 16.0 in
421 let tx0 = float tilex /. 16.0
422 and ty0 = float tiley /. 16.0 in
423 let tx1 = tx0 +. tw
424 and ty1 = ty0 +. th in
425 Raw.sets_float Glutils.vraw ~pos:0
426 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
427 Raw.sets_float Glutils.traw ~pos:0
428 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
429 GlArray.vertex `two Glutils.vraw;
430 GlArray.tex_coord `two Glutils.traw;
431 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
432 Gl.disable `texture_2d;
434 | None ->
435 GlDraw.color (1.0, 1.0, 1.0);
436 filledrect (float x) (float y) (float (x+w)) (float (y+h));
437 end;
438 if conf.invert
439 then GlTex.env (`mode `modulate);
440 if w > 128 && h > fstate.fontsize + 10
441 then (
442 let c = if conf.invert then 1.0 else 0.0 in
443 GlDraw.color (c, c, c);
444 let c, r =
445 if conf.verbose
446 then (col*conf.tilew, row*conf.tileh)
447 else col, row
449 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
451 GlDraw.color color;
452 begintiles ();
454 itertiles l f;
455 endtiles ();
458 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
460 let tilevisible1 l x y =
461 let ax0 = l.pagex
462 and ax1 = l.pagex + l.pagevw
463 and ay0 = l.pagey
464 and ay1 = l.pagey + l.pagevh in
466 let bx0 = x
467 and by0 = y in
468 let bx1 = min (bx0 + conf.tilew) l.pagew
469 and by1 = min (by0 + conf.tileh) l.pageh in
471 let rx0 = max ax0 bx0
472 and ry0 = max ay0 by0
473 and rx1 = min ax1 bx1
474 and ry1 = min ay1 by1 in
476 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
477 nonemptyintersection
480 let tilevisible layout n x y =
481 let rec findpageinlayout m = function
482 | l :: rest when l.pageno = n ->
483 tilevisible1 l x y || (
484 match conf.columns with
485 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
486 | Csplit _ | Csingle _ | Cmulti _ -> false
488 | _ :: rest -> findpageinlayout 0 rest
489 | [] -> false
491 findpageinlayout 0 layout;
494 let tileready l x y =
495 tilevisible1 l x y &&
496 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
499 let tilepage n p layout =
500 let rec loop = function
501 | l :: rest ->
502 if l.pageno = n
503 then
504 let f col row _ _ _ _ _ _ =
505 if state.currently = Idle
506 then
507 match gettileopaque l col row with
508 | Some _ -> ()
509 | None ->
510 let x = col*conf.tilew
511 and y = row*conf.tileh in
512 let w =
513 let w = l.pagew - x in
514 min w conf.tilew
516 let h =
517 let h = l.pageh - y in
518 min h conf.tileh
520 let pbo =
521 if conf.usepbo
522 then getpbo w h conf.colorspace
523 else ~< "0"
525 wcmd "tile %s %d %d %d %d %s" (~> p) x y w h (~> pbo);
526 state.currently <-
527 Tiling (
528 l, p, conf.colorspace, conf.angle,
529 state.gen, col, row, conf.tilew, conf.tileh
532 itertiles l f;
533 else
534 loop rest
536 | [] -> ()
538 if nogeomcmds state.geomcmds
539 then loop layout;
542 let preloadlayout x y sw sh =
543 let y = if y < sh then 0 else y - sh in
544 let x = min 0 (x + sw) in
545 let h = sh*3 in
546 let w = sw*3 in
547 layout x y w h;
550 let load pages =
551 let rec loop pages =
552 if state.currently = Idle
553 then
554 match pages with
555 | l :: rest ->
556 begin match getopaque l.pageno with
557 | None ->
558 wcmd "page %d %d" l.pageno l.pagedimno;
559 state.currently <- Loading (l, state.gen);
560 | Some opaque ->
561 tilepage l.pageno opaque pages;
562 loop rest
563 end;
564 | _ -> ()
566 if nogeomcmds state.geomcmds
567 then loop pages
570 let preload pages =
571 load pages;
572 if conf.preload && state.currently = Idle
573 then load (preloadlayout state.x state.y state.winw state.winh);
576 let layoutready layout =
577 let rec fold all ls =
578 all && match ls with
579 | l :: rest ->
580 let seen = ref false in
581 let allvisible = ref true in
582 let foo col row _ _ _ _ _ _ =
583 seen := true;
584 allvisible := !allvisible &&
585 begin match gettileopaque l col row with
586 | Some _ -> true
587 | None -> false
590 itertiles l foo;
591 fold (!seen && !allvisible) rest
592 | [] -> true
594 let alltilesvisible = fold true layout in
595 alltilesvisible;
598 let gotoxy x y =
599 let y = bound y 0 state.maxy in
600 let y, layout =
601 let layout = layout x y state.winw state.winh in
602 postRedisplay "gotoxy ready";
603 y, layout
605 state.x <- x;
606 state.y <- y;
607 state.layout <- layout;
608 begin match state.mode with
609 | LinkNav ln ->
610 begin match ln with
611 | Ltexact (pageno, linkno) ->
612 let rec loop = function
613 | [] ->
614 state.lnava <- Some (pageno, linkno);
615 state.mode <- LinkNav (Ltgendir 0)
616 | l :: _ when l.pageno = pageno ->
617 begin match getopaque pageno with
618 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
619 | Some opaque ->
620 let x0, y0, x1, y1 = getlinkrect opaque linkno in
621 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
622 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
623 then state.mode <- LinkNav (Ltgendir 0)
625 | _ :: rest -> loop rest
627 loop layout
628 | Ltnotready _ | Ltgendir _ -> ()
630 | Birdseye _ | Textentry _ | View -> ()
631 end;
632 begin match state.mode with
633 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
634 if not (pagevisible layout pageno)
635 then (
636 match state.layout with
637 | [] -> ()
638 | l :: _ ->
639 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
641 | LinkNav lt ->
642 begin match lt with
643 | Ltnotready (_, dir)
644 | Ltgendir dir ->
645 let linknav =
646 let rec loop = function
647 | [] -> lt
648 | l :: rest ->
649 match getopaque l.pageno with
650 | None -> Ltnotready (l.pageno, dir)
651 | Some opaque ->
652 let link =
653 let ld =
654 if dir = 0
655 then LDfirstvisible (l.pagex, l.pagey, dir)
656 else if dir > 0 then LDfirst else LDlast
658 findlink opaque ld
660 match link with
661 | Lnotfound -> loop rest
662 | Lfound n ->
663 showlinktype (getlink opaque n);
664 Ltexact (l.pageno, n)
666 loop state.layout
668 state.mode <- LinkNav linknav
669 | Ltexact _ -> ()
671 | Textentry _ | View -> ()
672 end;
673 preload layout;
674 if conf.updatecurs
675 then (
676 let mx, my = state.mpos in
677 updateunder mx my;
681 let conttiling pageno opaque =
682 tilepage pageno opaque
683 (if conf.preload
684 then preloadlayout state.x state.y state.winw state.winh
685 else state.layout)
688 let gotoxy x y =
689 if not conf.verbose then state.text <- E.s;
690 gotoxy x y;
693 let getanchory (n, top, dtop) =
694 let y, h = getpageyh n in
695 if conf.presentation
696 then
697 let ips = calcips h in
698 y + truncate (top*.float h -. dtop*.float ips) + ips;
699 else y + truncate (top*.float h -. dtop*.float conf.interpagespace)
702 let gotoanchor anchor = gotoxy state.x (getanchory anchor);;
703 let addnav () = getanchor () |> cbput state.hists.nav;;
704 let addnavnorc () = getanchor () |> cbput_dont_update_rc state.hists.nav;;
706 let getnav dir =
707 let anchor = cbgetc state.hists.nav dir in
708 getanchory anchor;
711 let gotopage n top =
712 let y, h = getpageyh n in
713 let y = y + (truncate (top *. float h)) in
714 gotoxy state.x y
717 let gotopage1 n top =
718 let y = getpagey n in
719 let y = y + top in
720 gotoxy state.x y
723 let invalidate s f =
724 Glutils.redisplay := false;
725 state.layout <- [];
726 state.pdims <- [];
727 state.rects <- [];
728 state.rects1 <- [];
729 match state.geomcmds with
730 | ps, [] when emptystr ps ->
731 f ();
732 state.geomcmds <- s, [];
733 | ps, [] -> state.geomcmds <- ps, [s, f];
734 | ps, (s', _) :: rest when s' = s -> state.geomcmds <- ps, ((s, f) :: rest);
735 | ps, cmds -> state.geomcmds <- ps, ((s, f) :: cmds);
738 let flushpages () =
739 Hashtbl.iter (fun _ opaque -> wcmd "freepage %s" (~> opaque)) state.pagemap;
740 Hashtbl.clear state.pagemap;
743 let flushtiles () =
744 if not (Queue.is_empty state.tilelru)
745 then (
746 Queue.iter (fun (k, p, s) ->
747 wcmd "freetile %s" (~> p);
748 state.memused <- state.memused - s;
749 Hashtbl.remove state.tilemap k;
750 ) state.tilelru;
751 state.uioh#infochanged Memused;
752 Queue.clear state.tilelru;
754 load state.layout;
757 let stateh h =
758 let h = truncate (float h*.conf.zoom) in
759 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
760 h - d
763 let fillhelp () =
764 state.help <-
765 let sl = keystostrlist conf in
766 let rec loop accu =
767 function | [] -> accu
768 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
769 in Help.makehelp conf.urilauncher
770 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
773 let opendoc path password =
774 state.path <- path;
775 state.password <- password;
776 state.gen <- state.gen + 1;
777 state.docinfo <- [];
778 state.outlines <- [||];
780 flushpages ();
781 setaalevel conf.aalevel;
782 let titlepath =
783 if emptystr state.origin
784 then path
785 else state.origin
787 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
788 wcmd "open %d %d %s\000%s\000%s\000"
789 (btod conf.usedoccss) !layouth
790 path password conf.css;
791 invalidate "reqlayout"
792 (fun () ->
793 wcmd "reqlayout %d %d %d %s\000"
794 conf.angle (FMTE.to_int conf.fitmodel)
795 (stateh state.winh) state.nameddest
797 fillhelp ();
800 let reload () =
801 state.anchor <- getanchor ();
802 state.reload <- Some (state.x, state.y, now ());
803 opendoc state.path state.password;
806 let scalecolor c = let c = c *. conf.colorscale in (c, c, c);;
807 let scalecolor2 (r, g, b) =
808 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
811 let docolumns columns =
812 match columns with
813 | Csingle _ ->
814 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
815 let rec loop pageno pdimno pdim y ph pdims =
816 if pageno != state.pagecount
817 then
818 let pdimno, ((_, w, h, xoff) as pdim), pdims =
819 match pdims with
820 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
821 pdimno+1, pdim, rest
822 | _ ->
823 pdimno, pdim, pdims
825 let x = max 0 (((state.winw - w) / 2) - xoff) in
826 let y =
827 y + (if conf.presentation
828 then (if pageno = 0 then calcips h else calcips ph + calcips h)
829 else (if pageno = 0 then 0 else conf.interpagespace))
831 a.(pageno) <- (pdimno, x, y, pdim);
832 loop (pageno+1) pdimno pdim (y + h) h pdims
834 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
835 conf.columns <- Csingle a;
837 | Cmulti ((columns, coverA, coverB), _) ->
838 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
839 let rec loop pageno pdimno pdim x y rowh pdims =
840 let rec fixrow m =
841 if m = pageno then () else
842 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
843 if h < rowh
844 then (
845 let y = y + (rowh - h) / 2 in
846 a.(m) <- (pdimno, x, y, pdim);
848 fixrow (m+1)
850 if pageno = state.pagecount
851 then fixrow (((pageno - 1) / columns) * columns)
852 else
853 let pdimno, ((_, w, h, xoff) as pdim), pdims =
854 match pdims with
855 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
856 pdimno+1, pdim, rest
857 | _ -> pdimno, pdim, pdims
859 let x, y, rowh' =
860 if pageno = coverA - 1 || pageno = state.pagecount - coverB
861 then (
862 let x = (state.winw - w) / 2 in
863 let ips =
864 if conf.presentation then calcips h else conf.interpagespace in
865 x, y + ips + rowh, h
867 else (
868 if (pageno - coverA) mod columns = 0
869 then (
870 let x = max 0 (state.winw - state.w) / 2 in
871 let y =
872 if conf.presentation
873 then
874 let ips = calcips h in
875 y + (if pageno = 0 then 0 else calcips rowh + ips)
876 else
877 y + (if pageno = 0 then 0 else conf.interpagespace)
879 x, y + rowh, h
881 else x, y, max rowh h
884 let y =
885 if pageno > 1 && (pageno - coverA) mod columns = 0
886 then (
887 let y =
888 if pageno = columns && conf.presentation
889 then (
890 let ips = calcips rowh in
891 for i = 0 to pred columns
893 let (pdimno, x, y, pdim) = a.(i) in
894 a.(i) <- (pdimno, x, y+ips, pdim)
895 done;
896 y+ips;
898 else y
900 fixrow (pageno - columns);
903 else y
905 a.(pageno) <- (pdimno, x, y, pdim);
906 let x = x + w + xoff*2 + conf.interpagespace in
907 loop (pageno+1) pdimno pdim x y rowh' pdims
909 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
910 conf.columns <- Cmulti ((columns, coverA, coverB), a);
912 | Csplit (c, _) ->
913 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
914 let rec loop pageno pdimno pdim y pdims =
915 if pageno != state.pagecount
916 then
917 let pdimno, ((_, w, h, _) as pdim), pdims =
918 match pdims with
919 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
920 pdimno+1, pdim, rest
921 | _ -> pdimno, pdim, pdims
923 let cw = w / c in
924 let rec loop1 n x y =
925 if n = c then y else (
926 a.(pageno*c + n) <- (pdimno, x, y, pdim);
927 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
930 let y = loop1 0 0 y in
931 loop (pageno+1) pdimno pdim y pdims
933 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
934 conf.columns <- Csplit (c, a);
937 let represent () =
938 docolumns conf.columns;
939 state.maxy <- calcheight ();
940 if state.reprf == noreprf
941 then (
942 match state.mode with
943 | Birdseye (_, _, pageno, _, _) ->
944 let y, h = getpageyh pageno in
945 let top = (state.winh - h) / 2 in
946 gotoxy state.x (max 0 (y - top))
947 | Textentry _ | View | LinkNav _ ->
948 let y = getanchory state.anchor in
949 let y = min y (state.maxy - state.winh) in
950 gotoxy state.x y;
952 else (
953 state.reprf ();
954 state.reprf <- noreprf;
958 let reshape ?(firsttime=false) w h =
959 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
960 if not firsttime && nogeomcmds state.geomcmds
961 then state.anchor <- getanchor ();
963 state.winw <- w;
964 let w = truncate (float w *. conf.zoom) in
965 let w = max w 2 in
966 state.winh <- h;
967 setfontsize fstate.fontsize;
968 GlMat.mode `modelview;
969 GlMat.load_identity ();
971 GlMat.mode `projection;
972 GlMat.load_identity ();
973 GlMat.rotate ~x:1.0 ~angle:180.0 ();
974 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
975 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
977 let relx =
978 if conf.zoom <= 1.0
979 then 0.0
980 else float state.x /. float state.w
982 invalidate "geometry"
983 (fun () ->
984 state.w <- w;
985 if not firsttime
986 then state.x <- truncate (relx *. float w);
987 let w =
988 match conf.columns with
989 | Csingle _ -> w
990 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
991 | Csplit (c, _) -> w * c
993 wcmd "geometry %d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
997 let gctiles () =
998 let len = Queue.length state.tilelru in
999 let layout = lazy (if conf.preload
1000 then preloadlayout state.x state.y state.winw state.winh
1001 else state.layout) in
1002 let rec loop qpos =
1003 if state.memused > conf.memlimit
1004 then (
1005 if qpos < len
1006 then
1007 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1008 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1009 let (_, pw, ph, _) = getpagedim n in
1010 if gen = state.gen
1011 && colorspace = conf.colorspace
1012 && angle = conf.angle
1013 && pagew = pw
1014 && pageh = ph
1015 && (
1016 let x = col*conf.tilew and y = row*conf.tileh in
1017 tilevisible (Lazy.force_val layout) n x y
1019 then Queue.push lruitem state.tilelru
1020 else (
1021 freepbo p;
1022 wcmd "freetile %s" (~> p);
1023 state.memused <- state.memused - s;
1024 state.uioh#infochanged Memused;
1025 Hashtbl.remove state.tilemap k;
1027 loop (qpos+1)
1030 loop 0
1033 let onpagerect pageno f =
1034 let b =
1035 match conf.columns with
1036 | Cmulti (_, b) -> b
1037 | Csingle b -> b
1038 | Csplit (_, b) -> b
1040 if pageno >= 0 && pageno < Array.length b
1041 then
1042 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1043 f w h
1046 let gotopagexy1 pageno x y =
1047 let _,w1,h1,leftx = getpagedim pageno in
1048 let top = y /. (float h1) in
1049 let left = x /. (float w1) in
1050 let py, w, h = getpageywh pageno in
1051 let wh = state.winh in
1052 let x = left *. (float w) in
1053 let x = leftx + state.x + truncate x in
1054 let sx =
1055 if x < 0 || x >= state.winw
1056 then state.x - x
1057 else state.x
1059 let pdy = truncate (top *. float h) in
1060 let y' = py + pdy in
1061 let dy = y' - state.y in
1062 let sy =
1063 if x != state.x || not (dy > 0 && dy < wh)
1064 then (
1065 if conf.presentation
1066 then
1067 if abs (py - y') > wh
1068 then y'
1069 else py
1070 else y';
1072 else state.y
1074 if state.x != sx || state.y != sy
1075 then gotoxy sx sy
1076 else gotoxy state.x state.y;
1079 let gotopagexy pageno x y =
1080 match state.mode with
1081 | Birdseye _ -> gotopage pageno 0.0
1082 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1085 let getpassword () =
1086 let passcmd = getenvdef "LLPP_ASKPASS" conf.passcmd in
1087 if emptystr passcmd
1088 then E.s
1089 else getcmdoutput (fun s ->
1090 impmsg "error getting password: %s" s;
1091 dolog "%s" s) passcmd;
1094 let pgoto opaque pageno x y =
1095 let pdimno = getpdimno pageno in
1096 let x, y = project opaque pageno pdimno x y in
1097 gotopagexy pageno x y;
1100 let act cmds =
1101 (* dolog "%S" cmds; *)
1102 let spl = splitatchar cmds ' ' in
1103 let scan s fmt f =
1104 try Scanf.sscanf s fmt f
1105 with exn ->
1106 dolog "error processing '%S': %s" cmds @@ exntos exn;
1107 exit 1
1109 let addoutline outline =
1110 match state.currently with
1111 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1112 | Idle -> state.currently <- Outlining [outline]
1113 | Loading _ | Tiling _ ->
1114 dolog "invalid outlining state";
1115 logcurrently state.currently
1117 match spl with
1118 | "clear", "" ->
1119 state.pdims <- [];
1120 state.uioh#infochanged Pdim;
1122 | "clearrects", "" ->
1123 state.rects <- state.rects1;
1124 postRedisplay "clearrects";
1126 | "continue", args ->
1127 let n = scan args "%u" (fun n -> n) in
1128 state.pagecount <- n;
1129 begin match state.currently with
1130 | Outlining l ->
1131 state.currently <- Idle;
1132 state.outlines <- Array.of_list (List.rev l)
1133 | Idle | Loading _ | Tiling _ -> ()
1134 end;
1136 let cur, cmds = state.geomcmds in
1137 if emptystr cur then error "empty geomcmd";
1139 begin match List.rev cmds with
1140 | [] ->
1141 state.geomcmds <- E.s, [];
1142 represent ();
1143 | (s, f) :: rest ->
1144 f ();
1145 state.geomcmds <- s, List.rev rest;
1146 end;
1147 postRedisplay "continue";
1149 | "msg", args ->
1150 showtext ' ' args
1152 | "vmsg", args ->
1153 if conf.verbose then showtext ' ' args
1155 | "emsg", args ->
1156 Buffer.add_string state.errmsgs args;
1157 state.newerrmsgs <- true;
1158 postRedisplay "error message"
1160 | "progress", args ->
1161 let progress, text =
1162 scan args "%f %n"
1163 (fun f pos -> f, String.sub args pos (String.length args - pos))
1165 state.text <- text;
1166 state.progress <- progress;
1167 postRedisplay "progress"
1169 | "firstmatch", args ->
1170 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1171 scan args "%u %d %f %f %f %f %f %f %f %f"
1172 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1173 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1175 let y = (getpagey pageno) + truncate y0 in
1176 let x =
1177 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1178 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1179 else state.x
1181 addnav ();
1182 gotoxy x y;
1183 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1184 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1186 | "match", args ->
1187 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1188 scan args "%u %d %f %f %f %f %f %f %f %f"
1189 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1190 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1192 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1193 state.rects1 <-
1194 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1196 | "page", args ->
1197 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1198 let pageopaque = ~< pageopaques in
1199 begin match state.currently with
1200 | Loading (l, gen) ->
1201 vlog "page %d took %f sec" l.pageno t;
1202 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1203 let preloadedpages =
1204 if conf.preload
1205 then preloadlayout state.x state.y state.winw state.winh
1206 else state.layout
1208 let evict () =
1209 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1210 IntSet.empty preloadedpages
1212 let evictedpages =
1213 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1214 if not (IntSet.mem pageno set)
1215 then (
1216 wcmd "freepage %s" (~> opaque);
1217 key :: accu
1219 else accu
1220 ) state.pagemap []
1222 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1224 evict ();
1225 state.currently <- Idle;
1226 if gen = state.gen
1227 then (
1228 tilepage l.pageno pageopaque state.layout;
1229 load state.layout;
1230 load preloadedpages;
1231 let visible = pagevisible state.layout l.pageno in
1232 if visible
1233 then (
1234 match state.mode with
1235 | LinkNav (Ltnotready (pageno, dir)) ->
1236 if pageno = l.pageno
1237 then (
1238 let link =
1239 let ld =
1240 if dir = 0
1241 then LDfirstvisible (l.pagex, l.pagey, dir)
1242 else if dir > 0 then LDfirst else LDlast
1244 findlink pageopaque ld
1246 match link with
1247 | Lnotfound -> ()
1248 | Lfound n ->
1249 showlinktype (getlink pageopaque n);
1250 state.mode <- LinkNav (Ltexact (l.pageno, n))
1252 | LinkNav (Ltgendir _)
1253 | LinkNav (Ltexact _)
1254 | View
1255 | Birdseye _
1256 | Textentry _ -> ()
1259 if visible && layoutready state.layout
1260 then postRedisplay "page";
1263 | Idle | Tiling _ | Outlining _ ->
1264 dolog "Inconsistent loading state";
1265 logcurrently state.currently;
1266 exit 1
1269 | "tile" , args ->
1270 let (x, y, opaques, size, t) =
1271 scan args "%u %u %s %u %f" (fun x y p size t -> (x, y, p, size, t))
1273 let opaque = ~< opaques in
1274 begin match state.currently with
1275 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1276 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1278 unmappbo opaque;
1279 if tilew != conf.tilew || tileh != conf.tileh
1280 then (
1281 wcmd "freetile %s" (~> opaque);
1282 state.currently <- Idle;
1283 load state.layout;
1285 else (
1286 puttileopaque l col row gen cs angle opaque size t;
1287 state.memused <- state.memused + size;
1288 state.uioh#infochanged Memused;
1289 gctiles ();
1290 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1291 opaque, size) state.tilelru;
1293 state.currently <- Idle;
1294 if gen = state.gen
1295 && conf.colorspace = cs
1296 && conf.angle = angle
1297 && tilevisible state.layout l.pageno x y
1298 then conttiling l.pageno pageopaque;
1300 preload state.layout;
1301 if gen = state.gen
1302 && conf.colorspace = cs
1303 && conf.angle = angle
1304 && tilevisible state.layout l.pageno x y
1305 && layoutready state.layout
1306 then postRedisplay "tile nothrottle";
1309 | Idle | Loading _ | Outlining _ ->
1310 dolog "Inconsistent tiling state";
1311 logcurrently state.currently;
1312 exit 1
1315 | "pdim", args ->
1316 let (n, w, h, _) as pdim =
1317 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1319 let pdim =
1320 match conf.fitmodel with
1321 | FitWidth -> pdim
1322 | FitPage | FitProportional ->
1323 match conf.columns with
1324 | Csplit _ -> (n, w, h, 0)
1325 | Csingle _ | Cmulti _ -> pdim
1327 state.pdims <- pdim :: state.pdims;
1328 state.uioh#infochanged Pdim
1330 | "o", args ->
1331 let (l, n, t, h, pos) =
1332 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1334 let s = String.sub args pos (String.length args - pos) in
1335 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1337 | "ou", args ->
1338 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1339 let s = String.sub args pos len in
1340 let pos2 = pos + len + 1 in
1341 let uri = String.sub args pos2 (String.length args - pos2) in
1342 addoutline (s, l, Ouri uri)
1344 | "on", args ->
1345 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1346 let s = String.sub args pos (String.length args - pos) in
1347 addoutline (s, l, Onone)
1349 | "a", args ->
1350 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1351 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1353 | "info", args ->
1354 let c, v = splitatchar args '\t' in
1355 let s =
1356 if nonemptystr v
1357 then
1358 if c = "Title"
1359 then (
1360 conf.title <- v;
1361 if not !ignoredoctitlte then Wsi.settitle v;
1362 args
1364 else
1365 if let len = String.length c in
1366 len > 6 && ((String.sub c (len-4) 4) = "date")
1367 then (
1368 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1369 then
1370 let b = Buffer.create 10 in
1371 Printf.bprintf b "%s\t" c;
1372 let sub p l c =
1374 Buffer.add_substring b v p l;
1375 Buffer.add_char b c;
1376 with exn -> Buffer.add_string b @@ exntos exn
1378 sub 2 4 '/';
1379 sub 6 2 '/';
1380 sub 8 2 ' ';
1381 sub 10 2 ':';
1382 sub 12 2 ':';
1383 sub 14 2 ' ';
1384 Buffer.add_char b '[';
1385 Buffer.add_string b v;
1386 Buffer.add_char b ']';
1387 Buffer.contents b
1388 else args
1390 else args
1391 else args
1393 state.docinfo <- (1, s) :: state.docinfo
1395 | "infoend", "" ->
1396 state.docinfo <- List.rev state.docinfo;
1397 state.uioh#infochanged Docinfo
1399 | "pass", args ->
1400 if args = "fail"
1401 then Wsi.settitle "Wrong password";
1402 let password = getpassword () in
1403 if emptystr password
1404 then error "document is password protected"
1405 else opendoc state.path password
1407 | _ -> error "unknown cmd `%S'" cmds
1410 let onhist cb =
1411 let rc = cb.rc in
1412 let action = function
1413 | HCprev -> cbget cb ~-1
1414 | HCnext -> cbget cb 1
1415 | HCfirst -> cbget cb ~-(cb.rc)
1416 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1417 and cancel () = cb.rc <- rc
1418 in (action, cancel)
1421 let search pattern forward =
1422 match conf.columns with
1423 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1424 | Csingle _ | Cmulti _ ->
1425 if nonemptystr pattern
1426 then
1427 let pn, py =
1428 match state.layout with
1429 | [] -> 0, 0
1430 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1432 wcmd "search %d %d %d %d,%s\000"
1433 (btod conf.icase) pn py (btod forward) pattern;
1436 let intentry text key =
1437 let text =
1438 if emptystr text && key = Keys.Ascii '-'
1439 then addchar text '-'
1440 else
1441 match [@warning "-4"] key with
1442 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1443 | _ ->
1444 state.text <- "invalid key";
1445 text
1447 TEcont text
1450 let linknact f s =
1451 if nonemptystr s
1452 then
1453 let n =
1454 let l = String.length s in
1455 let rec loop pos n =
1456 if pos = l
1457 then n
1458 else
1459 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1460 loop (pos+1) (n*26 + m)
1461 in loop 0 0
1463 let rec loop n = function
1464 | [] -> ()
1465 | l :: rest ->
1466 match getopaque l.pageno with
1467 | None -> loop n rest
1468 | Some opaque ->
1469 let m = getlinkcount opaque in
1470 if n < m
1471 then
1472 let under = getlink opaque n in
1473 f under
1474 else loop (n-m) rest
1476 loop n state.layout;
1479 let linknentry text key = match [@warning "-4"] key with
1480 | Keys.Ascii ('a' .. 'z' as c) ->
1481 let text = addchar text c in
1482 linknact (fun under -> state.text <- undertext under) text;
1483 TEcont text
1484 | _ ->
1485 state.text <- Printf.sprintf "invalid key %s" @@ Keys.to_string key;
1486 TEcont text
1489 let textentry text key = match [@warning "-4"] key with
1490 | Keys.Ascii c -> TEcont (addchar text c)
1491 | Keys.Code c -> TEcont (text ^ toutf8 c)
1492 | _ -> TEcont text
1495 let reqlayout angle fitmodel =
1496 if nogeomcmds state.geomcmds
1497 then state.anchor <- getanchor ();
1498 conf.angle <- angle mod 360;
1499 if conf.angle != 0
1500 then (
1501 match state.mode with
1502 | LinkNav _ -> state.mode <- View
1503 | Birdseye _ | Textentry _ | View -> ()
1505 conf.fitmodel <- fitmodel;
1506 invalidate "reqlayout"
1507 (fun () -> wcmd "reqlayout %d %d %d"
1508 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh));
1511 let settrim trimmargins trimfuzz =
1512 if nogeomcmds state.geomcmds
1513 then state.anchor <- getanchor ();
1514 conf.trimmargins <- trimmargins;
1515 conf.trimfuzz <- trimfuzz;
1516 let x0, y0, x1, y1 = trimfuzz in
1517 invalidate "settrim"
1518 (fun () -> wcmd "settrim %d %d %d %d %d"
1519 (btod conf.trimmargins) x0 y0 x1 y1);
1520 flushpages ();
1523 let setzoom zoom =
1524 let zoom = max 0.0001 zoom in
1525 if zoom <> conf.zoom
1526 then (
1527 state.prevzoom <- (conf.zoom, state.x);
1528 conf.zoom <- zoom;
1529 reshape state.winw state.winh;
1530 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1534 let pivotzoom ?(vw=min state.w state.winw)
1535 ?(vh=min (state.maxy-state.y) state.winh)
1536 ?(x=vw/2) ?(y=vh/2) zoom =
1537 let w = float state.w /. zoom in
1538 let hw = w /. 2.0 in
1539 let ratio = float vh /. float vw in
1540 let hh = hw *. ratio in
1541 let x0 = float x -. hw
1542 and y0 = float y -. hh in
1543 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1544 setzoom zoom;
1547 let pivotzoom ?vw ?vh ?x ?y zoom =
1548 if nogeomcmds state.geomcmds
1549 then
1550 if zoom > 1.0
1551 then pivotzoom ?vw ?vh ?x ?y zoom
1552 else setzoom zoom
1555 let setcolumns mode columns coverA coverB =
1556 state.prevcolumns <- Some (conf.columns, conf.zoom);
1557 if columns < 0
1558 then (
1559 if isbirdseye mode
1560 then impmsg "split mode doesn't work in bird's eye"
1561 else (
1562 conf.columns <- Csplit (-columns, E.a);
1563 state.x <- 0;
1564 conf.zoom <- 1.0;
1567 else (
1568 if columns < 2
1569 then (
1570 conf.columns <- Csingle E.a;
1571 state.x <- 0;
1572 setzoom 1.0;
1574 else (
1575 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1576 conf.zoom <- 1.0;
1579 reshape state.winw state.winh;
1582 let resetmstate () =
1583 state.mstate <- Mnone;
1584 Wsi.setcursor Wsi.CURSOR_INHERIT;
1587 let enterbirdseye () =
1588 let zoom = float conf.thumbw /. float state.winw in
1589 let birdseyepageno =
1590 let cy = state.winh / 2 in
1591 let fold = function
1592 | [] -> 0
1593 | l :: rest ->
1594 let rec fold best = function
1595 | [] -> best.pageno
1596 | l :: rest ->
1597 let d = cy - (l.pagedispy + l.pagevh/2)
1598 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1599 if abs d < abs dbest
1600 then fold l rest
1601 else best.pageno
1602 in fold l rest
1604 fold state.layout
1606 state.mode <-
1607 Birdseye (
1608 { conf with zoom = conf.zoom },
1609 state.x, birdseyepageno, -1, getanchor ()
1611 resetmstate ();
1612 conf.zoom <- zoom;
1613 conf.presentation <- false;
1614 conf.interpagespace <- 10;
1615 conf.hlinks <- false;
1616 conf.fitmodel <- FitPage;
1617 state.x <- 0;
1618 conf.columns <- (
1619 match conf.beyecolumns with
1620 | Some c ->
1621 conf.zoom <- 1.0;
1622 Cmulti ((c, 0, 0), E.a)
1623 | None -> Csingle E.a
1625 if conf.verbose
1626 then state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1627 (100.0*.zoom)
1628 else state.text <- E.s;
1629 reshape state.winw state.winh;
1632 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1633 state.mode <- View;
1634 conf.zoom <- c.zoom;
1635 conf.presentation <- c.presentation;
1636 conf.interpagespace <- c.interpagespace;
1637 conf.hlinks <- c.hlinks;
1638 conf.fitmodel <- c.fitmodel;
1639 conf.beyecolumns <- (
1640 match conf.columns with
1641 | Cmulti ((c, _, _), _) -> Some c
1642 | Csingle _ -> None
1643 | Csplit _ -> error "leaving bird's eye split mode"
1645 conf.columns <- (
1646 match c.columns with
1647 | Cmulti (c, _) -> Cmulti (c, E.a)
1648 | Csingle _ -> Csingle E.a
1649 | Csplit (c, _) -> Csplit (c, E.a)
1651 if conf.verbose
1652 then state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1653 (100.0*.conf.zoom);
1654 reshape state.winw state.winh;
1655 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1656 state.x <- leftx;
1659 let togglebirdseye () =
1660 match state.mode with
1661 | Birdseye vals -> leavebirdseye vals true
1662 | View -> enterbirdseye ()
1663 | Textentry _ | LinkNav _ -> ()
1666 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1667 let pageno = max 0 (pageno - incr) in
1668 let rec loop = function
1669 | [] -> gotopage1 pageno 0
1670 | l :: _ when l.pageno = pageno ->
1671 if l.pagedispy >= 0 && l.pagey = 0
1672 then postRedisplay "upbirdseye"
1673 else gotopage1 pageno 0
1674 | _ :: rest -> loop rest
1676 loop state.layout;
1677 state.text <- E.s;
1678 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1681 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1682 let pageno = min (state.pagecount - 1) (pageno + incr) in
1683 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1684 let rec loop = function
1685 | [] ->
1686 let y, h = getpageyh pageno in
1687 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1688 gotoxy state.x (clamp dy)
1689 | l :: _ when l.pageno = pageno ->
1690 if l.pagevh != l.pageh
1691 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1692 else postRedisplay "downbirdseye"
1693 | _ :: rest -> loop rest
1695 loop state.layout;
1696 state.text <- E.s;
1699 let optentry mode _ key =
1700 let btos b = if b then "on" else "off" in
1701 match [@warning "-4"] key with
1702 | Keys.Ascii 'C' ->
1703 let ondone s =
1705 let n, a, b = multicolumns_of_string s in
1706 setcolumns mode n a b;
1707 with exn ->
1708 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1710 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1712 | Keys.Ascii 'Z' ->
1713 let ondone s =
1715 let zoom = float (int_of_string s) /. 100.0 in
1716 pivotzoom zoom
1717 with exn ->
1718 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1720 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1722 | Keys.Ascii 'i' ->
1723 conf.icase <- not conf.icase;
1724 TEdone ("case insensitive search " ^ (btos conf.icase))
1726 | Keys.Ascii 'v' ->
1727 conf.verbose <- not conf.verbose;
1728 TEdone ("verbose " ^ (btos conf.verbose))
1730 | Keys.Ascii 'd' ->
1731 conf.debug <- not conf.debug;
1732 TEdone ("debug " ^ (btos conf.debug))
1734 | Keys.Ascii 'f' ->
1735 conf.underinfo <- not conf.underinfo;
1736 TEdone ("underinfo " ^ btos conf.underinfo)
1738 | Keys.Ascii 'T' ->
1739 settrim (not conf.trimmargins) conf.trimfuzz;
1740 TEdone ("trim margins " ^ btos conf.trimmargins)
1742 | Keys.Ascii 'I' ->
1743 conf.invert <- not conf.invert;
1744 TEdone ("invert colors " ^ btos conf.invert)
1746 | Keys.Ascii 'x' ->
1747 let ondone s =
1748 cbput state.hists.sel s;
1749 conf.selcmd <- s;
1751 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1752 textentry, ondone, true)
1754 | Keys.Ascii 'M' ->
1755 if conf.pax == None
1756 then conf.pax <- Some 0.0
1757 else conf.pax <- None;
1758 TEdone ("PAX " ^ btos (conf.pax != None))
1760 | (Keys.Ascii c) ->
1761 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1762 TEstop
1764 | _ -> TEcont state.text
1767 let adderrmsg src msg =
1768 Buffer.add_string state.errmsgs msg;
1769 state.newerrmsgs <- true;
1770 postRedisplay src
1773 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1775 class outlinelistview ~zebra ~source =
1776 let settext autonarrow s =
1777 if autonarrow
1778 then
1779 let ss = source#statestr in
1780 state.text <- if emptystr ss
1781 then "[" ^ s ^ "]"
1782 else "{" ^ ss ^ "} [" ^ s ^ "]"
1783 else state.text <- s
1785 object (self)
1786 inherit listview
1787 ~zebra
1788 ~helpmode:false
1789 ~source:(source :> lvsource)
1790 ~trusted:false
1791 ~modehash:(findkeyhash conf "outline")
1792 as super
1794 val m_autonarrow = false
1796 method! key key mask =
1797 let maxrows =
1798 if emptystr state.text
1799 then fstate.maxrows
1800 else fstate.maxrows - 2
1802 let calcfirst first active =
1803 if active > first
1804 then
1805 let rows = active - first in
1806 if rows > maxrows then active - maxrows else first
1807 else active
1809 let navigate incr =
1810 let active = m_active + incr in
1811 let active = bound active 0 (source#getitemcount - 1) in
1812 let first = calcfirst m_first active in
1813 postRedisplay "outline navigate";
1814 coe {< m_active = active; m_first = first >}
1816 let navscroll first =
1817 let active =
1818 let dist = m_active - first in
1819 if dist < 0
1820 then first
1821 else (
1822 if dist < maxrows
1823 then m_active
1824 else first + maxrows
1827 postRedisplay "outline navscroll";
1828 coe {< m_first = first; m_active = active >}
1830 let ctrl = Wsi.withctrl mask in
1831 let open Keys in
1832 match Wsi.kc2kt key with
1833 | Ascii 'a' when ctrl ->
1834 let text =
1835 if m_autonarrow
1836 then (
1837 source#denarrow;
1840 else (
1841 let pattern = source#renarrow in
1842 if nonemptystr m_qsearch
1843 then (source#narrow m_qsearch; m_qsearch)
1844 else pattern
1847 settext (not m_autonarrow) text;
1848 postRedisplay "toggle auto narrowing";
1849 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1851 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1852 settext true E.s;
1853 postRedisplay "toggle auto narrowing";
1854 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1856 | Ascii 'n' when ctrl ->
1857 source#narrow m_qsearch;
1858 if not m_autonarrow
1859 then source#add_narrow_pattern m_qsearch;
1860 postRedisplay "outline ctrl-n";
1861 coe {< m_first = 0; m_active = 0 >}
1863 | Ascii 'S' when ctrl ->
1864 let active = source#calcactive (getanchor ()) in
1865 let first = firstof m_first active in
1866 postRedisplay "outline ctrl-s";
1867 coe {< m_first = first; m_active = active >}
1869 | Ascii 'u' when ctrl ->
1870 postRedisplay "outline ctrl-u";
1871 if m_autonarrow && nonemptystr m_qsearch
1872 then (
1873 ignore (source#renarrow);
1874 settext m_autonarrow E.s;
1875 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1877 else (
1878 source#del_narrow_pattern;
1879 let pattern = source#renarrow in
1880 let text =
1881 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1883 settext m_autonarrow text;
1884 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1887 | Ascii 'l' when ctrl ->
1888 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1889 postRedisplay "outline ctrl-l";
1890 coe {< m_first = first >}
1892 | Ascii '\t' when m_autonarrow ->
1893 if nonemptystr m_qsearch
1894 then (
1895 postRedisplay "outline list view tab";
1896 source#add_narrow_pattern m_qsearch;
1897 settext true E.s;
1898 coe {< m_qsearch = E.s >}
1900 else coe self
1902 | Escape when m_autonarrow ->
1903 if nonemptystr m_qsearch
1904 then source#add_narrow_pattern m_qsearch;
1905 super#key key mask
1907 | Enter when m_autonarrow ->
1908 if nonemptystr m_qsearch
1909 then source#add_narrow_pattern m_qsearch;
1910 super#key key mask
1912 | (Ascii _ | Code _) when m_autonarrow ->
1913 let pattern = m_qsearch ^ toutf8 key in
1914 postRedisplay "outlinelistview autonarrow add";
1915 source#narrow pattern;
1916 settext true pattern;
1917 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1919 | Backspace when m_autonarrow ->
1920 if emptystr m_qsearch
1921 then coe self
1922 else
1923 let pattern = withoutlastutf8 m_qsearch in
1924 postRedisplay "outlinelistview autonarrow backspace";
1925 ignore (source#renarrow);
1926 source#narrow pattern;
1927 settext true pattern;
1928 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1930 | Up when ctrl -> navscroll (max 0 (m_first - 1))
1932 | Down when ctrl ->
1933 navscroll (min (source#getitemcount - 1) (m_first + 1))
1935 | Up -> navigate ~-1
1936 | Down -> navigate 1
1937 | Prior -> navigate ~-(fstate.maxrows)
1938 | Next -> navigate fstate.maxrows
1940 | Right ->
1941 let o =
1942 if ctrl
1943 then (
1944 postRedisplay "outline ctrl right";
1945 {< m_pan = m_pan + 1 >}
1947 else self#updownlevel 1
1949 coe o
1951 | Left ->
1952 let o =
1953 if ctrl
1954 then (
1955 postRedisplay "outline ctrl left";
1956 {< m_pan = m_pan - 1 >}
1958 else self#updownlevel ~-1
1960 coe o
1962 | Home ->
1963 postRedisplay "outline home";
1964 coe {< m_first = 0; m_active = 0 >}
1966 | End ->
1967 let active = source#getitemcount - 1 in
1968 let first = max 0 (active - fstate.maxrows) in
1969 postRedisplay "outline end";
1970 coe {< m_active = active; m_first = first >}
1972 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
1973 super#key key mask
1974 end;;
1976 let genhistoutlines () =
1977 Config.gethist ()
1978 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
1979 compare c2.lastvisit c1.lastvisit)
1980 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
1981 let path = if nonemptystr origin then origin else path in
1982 let base = mbtoutf8 @@ Filename.basename path in
1983 (base ^ "\000" ^ c.title, 1, Ohistory hist)
1987 let gotohist (path, c, bookmarks, x, anchor, origin) =
1988 Config.save leavebirdseye;
1989 state.anchor <- anchor;
1990 state.bookmarks <- bookmarks;
1991 state.origin <- origin;
1992 state.x <- x;
1993 setconf conf c;
1994 let x0, y0, x1, y1 = conf.trimfuzz in
1995 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
1996 reshape ~firsttime:true state.winw state.winh;
1997 opendoc path origin;
1998 setzoom c.zoom;
2001 let setcheckers enabled =
2002 match !checkerstexid with
2003 | None -> if enabled then checkerstexid := Some (makecheckers ())
2004 | Some id ->
2005 if not enabled
2006 then (
2007 GlTex.delete_texture id;
2008 checkerstexid := None;
2012 let describe_layout layout =
2013 let d =
2014 match layout with
2015 | [] -> "Page 0"
2016 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2017 | l :: rest ->
2018 let rangestr a b =
2019 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2020 else Printf.sprintf "%d%s%d" (a.pageno+1)
2021 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2022 (b.pageno+1)
2024 let rec fold s la lb = function
2025 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2026 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2027 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2029 fold "Pages" l l rest
2031 let percent =
2032 let maxy = maxy () in
2033 if maxy <= 0
2034 then 100.
2035 else 100. *. (float state.y /. float maxy)
2037 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2040 let setpresentationmode v =
2041 let n = page_of_y state.y in
2042 state.anchor <- (n, 0.0, 1.0);
2043 conf.presentation <- v;
2044 if conf.fitmodel = FitPage
2045 then reqlayout conf.angle conf.fitmodel;
2046 represent ();
2049 let enterinfomode =
2050 let btos b = if b then Utf8syms.radical else E.s in
2051 let showextended = ref false in
2052 let showcolors = ref false in
2053 let leave mode _ = state.mode <- mode in
2054 let src =
2055 (object
2056 val mutable m_l = []
2057 val mutable m_a = E.a
2058 val mutable m_prev_uioh = nouioh
2059 val mutable m_prev_mode = View
2061 inherit lvsourcebase
2063 method reset prev_mode prev_uioh =
2064 m_a <- Array.of_list (List.rev m_l);
2065 m_l <- [];
2066 m_prev_mode <- prev_mode;
2067 m_prev_uioh <- prev_uioh;
2069 method int name get set =
2070 m_l <-
2071 (name, `int get, 1,
2072 Action (
2073 fun u ->
2074 let ondone s =
2075 try set (int_of_string s)
2076 with exn ->
2077 state.text <- Printf.sprintf "bad integer `%s': %s"
2078 s @@ exntos exn
2080 state.text <- E.s;
2081 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2082 state.mode <- Textentry (te, leave m_prev_mode);
2084 )) :: m_l
2086 method int_with_suffix name get set =
2087 m_l <-
2088 (name, `intws get, 1,
2089 Action (
2090 fun u ->
2091 let ondone s =
2092 try set (int_of_string_with_suffix s)
2093 with exn ->
2094 state.text <- Printf.sprintf "bad integer `%s': %s"
2095 s @@ exntos exn
2097 state.text <- E.s;
2098 let te =
2099 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2101 state.mode <- Textentry (te, leave m_prev_mode);
2103 )) :: m_l
2105 method bool ?(offset=1) ?(btos=btos) name get set =
2106 m_l <-
2107 (name, `bool (btos, get), offset, Action (
2108 fun u ->
2109 let v = get () in
2110 set (not v);
2112 )) :: m_l
2114 method color name get set =
2115 m_l <-
2116 (name, `color get, 1,
2117 Action (
2118 fun u ->
2119 let invalid = (nan, nan, nan) in
2120 let ondone s =
2121 let c =
2122 try color_of_string s
2123 with exn ->
2124 state.text <- Printf.sprintf "bad color `%s': %s"
2125 s @@ exntos exn;
2126 invalid
2128 if c <> invalid
2129 then set c;
2131 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2132 state.text <- color_to_string (get ());
2133 state.mode <- Textentry (te, leave m_prev_mode);
2135 )) :: m_l
2137 method string name get set =
2138 m_l <-
2139 (name, `string get, 1,
2140 Action (
2141 fun u ->
2142 let ondone s = set s in
2143 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2144 state.mode <- Textentry (te, leave m_prev_mode);
2146 )) :: m_l
2148 method colorspace name get set =
2149 m_l <-
2150 (name, `string get, 1,
2151 Action (
2152 fun _ ->
2153 let source =
2154 (object
2155 inherit lvsourcebase
2157 initializer
2158 m_active <- CSTE.to_int conf.colorspace;
2159 m_first <- 0;
2161 method getitemcount =
2162 Array.length CSTE.names
2163 method getitem n =
2164 (CSTE.names.(n), 0)
2165 method exit ~uioh ~cancel ~active ~first ~pan =
2166 ignore (uioh, first, pan);
2167 if not cancel then set active;
2168 None
2169 method hasaction _ = true
2170 end)
2172 state.text <- E.s;
2173 let modehash = findkeyhash conf "info" in
2174 coe (new listview ~zebra:false ~helpmode:false
2175 ~source ~trusted:true ~modehash)
2176 )) :: m_l
2178 method paxmark name get set =
2179 m_l <-
2180 (name, `string get, 1,
2181 Action (
2182 fun _ ->
2183 let source =
2184 (object
2185 inherit lvsourcebase
2187 initializer
2188 m_active <- MTE.to_int conf.paxmark;
2189 m_first <- 0;
2191 method getitemcount = Array.length MTE.names
2192 method getitem n = (MTE.names.(n), 0)
2193 method exit ~uioh ~cancel ~active ~first ~pan =
2194 ignore (uioh, first, pan);
2195 if not cancel then set active;
2196 None
2197 method hasaction _ = true
2198 end)
2200 state.text <- E.s;
2201 let modehash = findkeyhash conf "info" in
2202 coe (new listview ~zebra:false ~helpmode:false
2203 ~source ~trusted:true ~modehash)
2204 )) :: m_l
2206 method fitmodel name get set =
2207 m_l <-
2208 (name, `string get, 1,
2209 Action (
2210 fun _ ->
2211 let source =
2212 (object
2213 inherit lvsourcebase
2215 initializer
2216 m_active <- FMTE.to_int conf.fitmodel;
2217 m_first <- 0;
2219 method getitemcount = Array.length FMTE.names
2220 method getitem n = (FMTE.names.(n), 0)
2221 method exit ~uioh ~cancel ~active ~first ~pan =
2222 ignore (uioh, first, pan);
2223 if not cancel then set active;
2224 None
2225 method hasaction _ = true
2226 end)
2228 state.text <- E.s;
2229 let modehash = findkeyhash conf "info" in
2230 coe (new listview ~zebra:false ~helpmode:false
2231 ~source ~trusted:true ~modehash)
2232 )) :: m_l
2234 method caption s offset =
2235 m_l <- (s, `empty, offset, Noaction) :: m_l
2237 method caption2 s f offset =
2238 m_l <- (s, `string f, offset, Noaction) :: m_l
2240 method getitemcount = Array.length m_a
2242 method getitem n =
2243 let tostr = function
2244 | `int f -> string_of_int (f ())
2245 | `intws f -> string_with_suffix_of_int (f ())
2246 | `string f -> f ()
2247 | `color f -> color_to_string (f ())
2248 | `bool (btos, f) -> btos (f ())
2249 | `empty -> E.s
2251 let name, t, offset, _ = m_a.(n) in
2252 ((let s = tostr t in
2253 if nonemptystr s
2254 then Printf.sprintf "%s\t%s" name s
2255 else name),
2256 offset)
2258 method exit ~uioh ~cancel ~active ~first ~pan =
2259 let uiohopt =
2260 if not cancel
2261 then (
2262 let uioh =
2263 match m_a.(active) with
2264 | _, _, _, Action f -> f uioh
2265 | _, _, _, Noaction -> uioh
2267 Some uioh
2269 else None
2271 m_active <- active;
2272 m_first <- first;
2273 m_pan <- pan;
2274 uiohopt
2276 method hasaction n =
2277 match m_a.(n) with
2278 | _, _, _, Action _ -> true
2279 | _, _, _, Noaction -> false
2281 initializer m_active <- 1
2282 end)
2284 let rec fillsrc prevmode prevuioh =
2285 let sep () = src#caption E.s 0 in
2286 let colorp name get set =
2287 src#string name
2288 (fun () -> color_to_string (get ()))
2289 (fun v ->
2290 try set @@ color_of_string v
2291 with exn ->
2292 state.text <-
2293 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2296 let rgba name get set =
2297 src#string name
2298 (fun () -> get () |> rgba_to_string)
2299 (fun v ->
2300 try set @@ rgba_of_string v
2301 with exn ->
2302 state.text <-
2303 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2306 let oldmode = state.mode in
2307 let birdseye = isbirdseye state.mode in
2309 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2311 src#bool "presentation mode"
2312 (fun () -> conf.presentation)
2313 (fun v -> setpresentationmode v);
2315 src#bool "ignore case in searches"
2316 (fun () -> conf.icase)
2317 (fun v -> conf.icase <- v);
2319 src#bool "preload"
2320 (fun () -> conf.preload)
2321 (fun v -> conf.preload <- v);
2323 src#bool "highlight links"
2324 (fun () -> conf.hlinks)
2325 (fun v -> conf.hlinks <- v);
2327 src#bool "under info"
2328 (fun () -> conf.underinfo)
2329 (fun v -> conf.underinfo <- v);
2331 src#fitmodel "fit model"
2332 (fun () -> FMTE.to_string conf.fitmodel)
2333 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2335 src#bool "trim margins"
2336 (fun () -> conf.trimmargins)
2337 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2339 sep ();
2340 src#int "inter-page space"
2341 (fun () -> conf.interpagespace)
2342 (fun n ->
2343 conf.interpagespace <- n;
2344 docolumns conf.columns;
2345 let pageno, py =
2346 match state.layout with
2347 | [] -> 0, 0
2348 | l :: _ -> l.pageno, l.pagey
2350 state.maxy <- calcheight ();
2351 let y = getpagey pageno in
2352 gotoxy state.x (y + py)
2355 src#int "page bias"
2356 (fun () -> conf.pagebias)
2357 (fun v -> conf.pagebias <- v);
2359 src#int "scroll step"
2360 (fun () -> conf.scrollstep)
2361 (fun n -> conf.scrollstep <- n);
2363 src#int "horizontal scroll step"
2364 (fun () -> conf.hscrollstep)
2365 (fun v -> conf.hscrollstep <- v);
2367 src#int "auto scroll step"
2368 (fun () ->
2369 match state.autoscroll with
2370 | Some step -> step
2371 | _ -> conf.autoscrollstep)
2372 (fun n ->
2373 let n = boundastep state.winh n in
2374 if state.autoscroll <> None
2375 then state.autoscroll <- Some n;
2376 conf.autoscrollstep <- n);
2378 src#int "zoom"
2379 (fun () -> truncate (conf.zoom *. 100.))
2380 (fun v -> pivotzoom ((float v) /. 100.));
2382 src#int "rotation"
2383 (fun () -> conf.angle)
2384 (fun v -> reqlayout v conf.fitmodel);
2386 src#int "scroll bar width"
2387 (fun () -> conf.scrollbw)
2388 (fun v ->
2389 conf.scrollbw <- v;
2390 reshape state.winw state.winh;
2393 src#int "scroll handle height"
2394 (fun () -> conf.scrollh)
2395 (fun v -> conf.scrollh <- v;);
2397 src#int "thumbnail width"
2398 (fun () -> conf.thumbw)
2399 (fun v ->
2400 conf.thumbw <- min 4096 v;
2401 match oldmode with
2402 | Birdseye beye ->
2403 leavebirdseye beye false;
2404 enterbirdseye ()
2405 | Textentry _
2406 | View
2407 | LinkNav _ -> ()
2410 let mode = state.mode in
2411 src#string "columns"
2412 (fun () ->
2413 match conf.columns with
2414 | Csingle _ -> "1"
2415 | Cmulti (multi, _) -> multicolumns_to_string multi
2416 | Csplit (count, _) -> "-" ^ string_of_int count
2418 (fun v ->
2419 let n, a, b = multicolumns_of_string v in
2420 setcolumns mode n a b);
2422 sep ();
2423 src#caption "Pixmap cache" 0;
2424 src#int_with_suffix "size (advisory)"
2425 (fun () -> conf.memlimit)
2426 (fun v -> conf.memlimit <- v);
2428 src#caption2 "used"
2429 (fun () ->
2430 Printf.sprintf "%s bytes, %d tiles"
2431 (string_with_suffix_of_int state.memused)
2432 (Hashtbl.length state.tilemap)) 1;
2434 sep ();
2435 src#caption "Layout" 0;
2436 src#caption2 "Dimension"
2437 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2438 state.winw state.winh
2439 state.w state.maxy)
2441 if conf.debug
2442 then src#caption2 "Position" (fun () ->
2443 Printf.sprintf "%dx%d" state.x state.y
2445 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2447 sep ();
2448 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2449 "Save these parameters as global defaults at exit"
2450 (fun () -> conf.bedefault)
2451 (fun v -> conf.bedefault <- v);
2453 sep ();
2454 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2455 src#bool ~offset:0 ~btos "Extended parameters"
2456 (fun () -> !showextended)
2457 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2458 if !showextended
2459 then (
2460 src#bool "checkers"
2461 (fun () -> conf.checkers)
2462 (fun v -> conf.checkers <- v; setcheckers v);
2463 src#bool "update cursor"
2464 (fun () -> conf.updatecurs)
2465 (fun v -> conf.updatecurs <- v);
2466 src#bool "scroll-bar on the left"
2467 (fun () -> conf.leftscroll)
2468 (fun v -> conf.leftscroll <- v);
2469 src#bool "verbose"
2470 (fun () -> conf.verbose)
2471 (fun v -> conf.verbose <- v);
2472 src#bool "invert colors"
2473 (fun () -> conf.invert)
2474 (fun v -> conf.invert <- v);
2475 src#bool "max fit"
2476 (fun () -> conf.maxhfit)
2477 (fun v -> conf.maxhfit <- v);
2478 src#bool "pax mode"
2479 (fun () -> conf.pax != None)
2480 (fun v ->
2481 if v
2482 then conf.pax <- Some (now ())
2483 else conf.pax <- None);
2484 src#string "uri launcher"
2485 (fun () -> conf.urilauncher)
2486 (fun v -> conf.urilauncher <- v);
2487 src#string "path launcher"
2488 (fun () -> conf.pathlauncher)
2489 (fun v -> conf.pathlauncher <- v);
2490 src#string "tile size"
2491 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2492 (fun v ->
2494 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2495 conf.tilew <- max 64 w;
2496 conf.tileh <- max 64 h;
2497 flushtiles ();
2498 with exn ->
2499 state.text <- Printf.sprintf "bad tile size `%s': %s"
2500 v @@ exntos exn
2502 src#int "texture count"
2503 (fun () -> conf.texcount)
2504 (fun v ->
2505 if realloctexts v
2506 then conf.texcount <- v
2507 else impmsg "failed to set texture count please retry later"
2509 src#int "slice height"
2510 (fun () -> conf.sliceheight)
2511 (fun v ->
2512 conf.sliceheight <- v;
2513 wcmd "sliceh %d" conf.sliceheight;
2515 src#int "anti-aliasing level"
2516 (fun () -> conf.aalevel)
2517 (fun v ->
2518 conf.aalevel <- bound v 0 8;
2519 state.anchor <- getanchor ();
2520 opendoc state.path state.password;
2522 src#string "page scroll scaling factor"
2523 (fun () -> string_of_float conf.pgscale)
2524 (fun v ->
2525 try conf.pgscale <- float_of_string v
2526 with exn ->
2527 state.text <-
2528 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2529 @@ exntos exn
2531 src#int "ui font size"
2532 (fun () -> fstate.fontsize)
2533 (fun v -> setfontsize (bound v 5 100));
2534 src#int "hint font size"
2535 (fun () -> conf.hfsize)
2536 (fun v -> conf.hfsize <- bound v 5 100);
2537 src#string "trim fuzz"
2538 (fun () -> irect_to_string conf.trimfuzz)
2539 (fun v ->
2541 conf.trimfuzz <- irect_of_string v;
2542 if conf.trimmargins
2543 then settrim true conf.trimfuzz;
2544 with exn ->
2545 state.text <- Printf.sprintf "bad irect `%s': %s" v
2546 @@ exntos exn
2548 src#string "selection command"
2549 (fun () -> conf.selcmd)
2550 (fun v -> conf.selcmd <- v);
2551 src#string "synctex command"
2552 (fun () -> conf.stcmd)
2553 (fun v -> conf.stcmd <- v);
2554 src#string "pax command"
2555 (fun () -> conf.paxcmd)
2556 (fun v -> conf.paxcmd <- v);
2557 src#string "ask password command"
2558 (fun () -> conf.passcmd)
2559 (fun v -> conf.passcmd <- v);
2560 src#string "save path command"
2561 (fun () -> conf.savecmd)
2562 (fun v -> conf.savecmd <- v);
2563 src#colorspace "color space"
2564 (fun () -> CSTE.to_string conf.colorspace)
2565 (fun v ->
2566 conf.colorspace <- CSTE.of_int v;
2567 wcmd "cs %d" v;
2568 load state.layout;
2570 src#paxmark "pax mark method"
2571 (fun () -> MTE.to_string conf.paxmark)
2572 (fun v -> conf.paxmark <- MTE.of_int v);
2573 if bousable ()
2574 then
2575 src#bool "use PBO"
2576 (fun () -> conf.usepbo)
2577 (fun v -> conf.usepbo <- v);
2578 src#bool "mouse wheel scrolls pages"
2579 (fun () -> conf.wheelbypage)
2580 (fun v -> conf.wheelbypage <- v);
2581 src#bool "open remote links in a new instance"
2582 (fun () -> conf.riani)
2583 (fun v -> conf.riani <- v);
2584 src#bool "edit annotations inline"
2585 (fun () -> conf.annotinline)
2586 (fun v -> conf.annotinline <- v);
2587 src#bool "coarse positioning in presentation mode"
2588 (fun () -> conf.coarseprespos)
2589 (fun v -> conf.coarseprespos <- v);
2590 src#bool "use document CSS"
2591 (fun () -> conf.usedoccss)
2592 (fun v ->
2593 conf.usedoccss <- v;
2594 state.anchor <- getanchor ();
2595 opendoc state.path state.password;
2597 src#bool ~btos "colors"
2598 (fun () -> !showcolors)
2599 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2600 if !showcolors
2601 then (
2602 colorp " background"
2603 (fun () -> conf.bgcolor)
2604 (fun v -> conf.bgcolor <- v);
2605 rgba " scrollbar"
2606 (fun () -> conf.sbarcolor)
2607 (fun v -> conf.sbarcolor <- v);
2608 rgba " scrollbar handle"
2609 (fun () -> conf.sbarhndlcolor)
2610 (fun v -> conf.sbarhndlcolor <- v);
2611 rgba " texture color"
2612 (fun () -> conf.texturecolor)
2613 (fun v ->
2614 GlTex.env (`color v);
2615 conf.texturecolor <- v;
2620 sep ();
2621 src#caption "Document" 0;
2622 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2623 src#caption2 "Pages" (fun () -> string_of_int state.pagecount) 1;
2624 src#caption2 "Dimensions"
2625 (fun () -> string_of_int (List.length state.pdims)) 1;
2626 if nonemptystr conf.css
2627 then src#caption2 "CSS" (fun () -> conf.css) 1;
2628 if conf.trimmargins
2629 then (
2630 sep ();
2631 src#caption "Trimmed margins" 0;
2632 src#caption2 "Dimensions"
2633 (fun () -> string_of_int (List.length state.pdims)) 1;
2636 sep ();
2637 src#caption "OpenGL" 0;
2638 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2639 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2641 sep ();
2642 src#caption "Location" 0;
2643 if nonemptystr state.origin
2644 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2645 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2647 src#reset prevmode prevuioh;
2649 fun () -> (
2650 state.text <- E.s;
2651 resetmstate ();
2652 let prevmode = state.mode
2653 and prevuioh = state.uioh in
2654 fillsrc prevmode prevuioh;
2655 let source = (src :> lvsource) in
2656 let modehash = findkeyhash conf "info" in
2657 state.uioh <-
2658 coe (object (self)
2659 inherit listview ~zebra:false ~helpmode:false
2660 ~source ~trusted:true ~modehash as super
2661 val mutable m_prevmemused = 0
2662 method! infochanged = function
2663 | Memused ->
2664 if m_prevmemused != state.memused
2665 then (
2666 m_prevmemused <- state.memused;
2667 postRedisplay "memusedchanged";
2669 | Pdim -> postRedisplay "pdimchanged"
2670 | Docinfo -> fillsrc prevmode prevuioh
2672 method! key key mask =
2673 if not (Wsi.withctrl mask)
2674 then
2675 match [@warning "-4"] Wsi.kc2kt key with
2676 | Keys.Left -> coe (self#updownlevel ~-1)
2677 | Keys.Right -> coe (self#updownlevel 1)
2678 | _ -> super#key key mask
2679 else super#key key mask
2680 end);
2681 postRedisplay "info";
2685 let enterhelpmode =
2686 let source =
2687 (object
2688 inherit lvsourcebase
2689 method getitemcount = Array.length state.help
2690 method getitem n =
2691 let s, l, _ = state.help.(n) in
2692 (s, l)
2694 method exit ~uioh ~cancel ~active ~first ~pan =
2695 let optuioh =
2696 if not cancel
2697 then (
2698 match state.help.(active) with
2699 | _, _, Action f -> Some (f uioh)
2700 | _, _, Noaction -> Some uioh
2702 else None
2704 m_active <- active;
2705 m_first <- first;
2706 m_pan <- pan;
2707 optuioh
2709 method hasaction n =
2710 match state.help.(n) with
2711 | _, _, Action _ -> true
2712 | _, _, Noaction -> false
2714 initializer
2715 m_active <- -1
2716 end)
2718 fun () ->
2719 let modehash = findkeyhash conf "help" in
2720 resetmstate ();
2721 state.uioh <- coe (new listview
2722 ~zebra:false ~helpmode:true
2723 ~source ~trusted:true ~modehash);
2724 postRedisplay "help";
2727 let entermsgsmode =
2728 let msgsource =
2729 (object
2730 inherit lvsourcebase
2731 val mutable m_items = E.a
2733 method getitemcount = 1 + Array.length m_items
2735 method getitem n =
2736 if n = 0
2737 then "[Clear]", 0
2738 else m_items.(n-1), 0
2740 method exit ~uioh ~cancel ~active ~first ~pan =
2741 ignore uioh;
2742 if not cancel
2743 then (
2744 if active = 0
2745 then Buffer.clear state.errmsgs;
2747 m_active <- active;
2748 m_first <- first;
2749 m_pan <- pan;
2750 None
2752 method hasaction n =
2753 n = 0
2755 method reset =
2756 state.newerrmsgs <- false;
2757 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2758 m_items <- Array.of_list l
2760 initializer
2761 m_active <- 0
2762 end)
2763 in fun () ->
2764 state.text <- E.s;
2765 resetmstate ();
2766 msgsource#reset;
2767 let source = (msgsource :> lvsource) in
2768 let modehash = findkeyhash conf "listview" in
2769 state.uioh <-
2770 coe (object
2771 inherit listview ~zebra:false ~helpmode:false
2772 ~source ~trusted:false ~modehash as super
2773 method! display =
2774 if state.newerrmsgs
2775 then msgsource#reset;
2776 super#display
2777 end);
2778 postRedisplay "msgs";
2781 let getusertext s =
2782 let editor = getenvdef "EDITOR" E.s in
2783 if emptystr editor
2784 then E.s
2785 else
2786 let tmppath = Filename.temp_file "llpp" "note" in
2787 if nonemptystr s
2788 then (
2789 let oc = open_out tmppath in
2790 output_string oc s;
2791 close_out oc;
2793 let execstr = editor ^ " " ^ tmppath in
2794 let s =
2795 match spawn execstr [] with
2796 | exception exn ->
2797 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2799 | pid ->
2800 match Unix.waitpid [] pid with
2801 | exception exn ->
2802 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2804 | (_pid, status) ->
2805 match status with
2806 | Unix.WEXITED 0 -> filecontents tmppath
2807 | Unix.WEXITED n ->
2808 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2810 | Unix.WSIGNALED n ->
2811 impmsg "editor process(%s) was killed by signal %d" execstr n;
2813 | Unix.WSTOPPED n ->
2814 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2817 match Unix.unlink tmppath with
2818 | exception exn ->
2819 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2821 | () -> s
2824 let enterannotmode opaque slinkindex =
2825 let msgsource =
2826 (object
2827 inherit lvsourcebase
2828 val mutable m_text = E.s
2829 val mutable m_items = E.a
2831 method getitemcount = Array.length m_items
2833 method getitem n =
2834 let label, _func = m_items.(n) in
2835 label, 0
2837 method exit ~uioh ~cancel ~active ~first ~pan =
2838 ignore (uioh, first, pan);
2839 if not cancel
2840 then (
2841 let _label, func = m_items.(active) in
2842 func ()
2844 None
2846 method hasaction n = nonemptystr @@ fst m_items.(n)
2848 method reset s =
2849 let rec split accu b i =
2850 let p = b+i in
2851 if p = String.length s
2852 then (String.sub s b (p-b), fun () -> ()) :: accu
2853 else
2854 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2855 then
2856 let ss = if i = 0 then E.s else String.sub s b i in
2857 split ((ss, fun () -> ())::accu) (p+1) 0
2858 else split accu b (i+1)
2860 let cleanup () =
2861 wcmd "freepage %s" (~> opaque);
2862 let keys =
2863 Hashtbl.fold (fun key opaque' accu ->
2864 if opaque' = opaque'
2865 then key :: accu else accu) state.pagemap []
2867 List.iter (Hashtbl.remove state.pagemap) keys;
2868 flushtiles ();
2869 gotoxy state.x state.y
2871 let dele () =
2872 delannot opaque slinkindex;
2873 cleanup ();
2875 let edit inline () =
2876 let update s =
2877 if emptystr s
2878 then dele ()
2879 else (
2880 modannot opaque slinkindex s;
2881 cleanup ();
2884 if inline
2885 then
2886 let mode = state.mode in
2887 state.mode <-
2888 Textentry (
2889 ("annotation: ", m_text, None, textentry, update, true),
2890 fun _ -> state.mode <- mode
2892 state.text <- E.s;
2893 enttext ();
2894 else
2895 let s = getusertext m_text in
2896 update s
2898 m_text <- s;
2899 m_items <-
2900 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2901 :: ("[Delete]", dele)
2902 :: ("[Edit]", edit conf.annotinline)
2903 :: (E.s, fun () -> ())
2904 :: split [] 0 0 |> List.rev |> Array.of_list
2906 initializer
2907 m_active <- 0
2908 end)
2910 state.text <- E.s;
2911 let s = getannotcontents opaque slinkindex in
2912 resetmstate ();
2913 msgsource#reset s;
2914 let source = (msgsource :> lvsource) in
2915 let modehash = findkeyhash conf "listview" in
2916 state.uioh <- coe (object
2917 inherit listview ~zebra:false ~helpmode:false
2918 ~source ~trusted:false ~modehash
2919 end);
2920 postRedisplay "enterannotmode";
2923 let gotoremote spec =
2924 let filename, dest = splitatchar spec '#' in
2925 let getpath filename =
2926 let path =
2927 if nonemptystr filename
2928 then
2929 if Filename.is_relative filename
2930 then
2931 let dir = Filename.dirname state.path in
2932 let dir =
2933 if Filename.is_implicit dir
2934 then Filename.concat (Sys.getcwd ()) dir
2935 else dir
2937 Filename.concat dir filename
2938 else filename
2939 else E.s
2941 if Sys.file_exists path
2942 then path
2943 else E.s
2945 let path = getpath filename in
2946 let dospawn lcmd =
2947 if conf.riani
2948 then
2949 let cmd = Lazy.force_val lcmd in
2950 match spawn cmd with
2951 | _pid -> ()
2952 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
2953 else
2954 let anchor = getanchor () in
2955 let ranchor = state.path, state.password, anchor, state.origin in
2956 state.origin <- E.s;
2957 state.ranchors <- ranchor :: state.ranchors;
2958 opendoc path E.s;
2960 if substratis spec 0 "page="
2961 then
2962 match Scanf.sscanf spec "page=%d" (fun n -> n) with
2963 | pageno ->
2964 state.anchor <- (pageno, 0.0, 0.0);
2965 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
2966 | exception exn ->
2967 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
2968 else (
2969 state.nameddest <- dest;
2970 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
2974 let gotounder = function
2975 | Ulinkuri s when isexternallink s ->
2976 if substratis s 0 "file://"
2977 then gotoremote @@ String.sub s 7 (String.length s - 7)
2978 else Help.gotouri conf.urilauncher s
2979 | Ulinkuri s ->
2980 let pageno, x, y = uritolocation s in
2981 addnav ();
2982 gotopagexy pageno x y
2983 | Utext _ | Unone -> ()
2984 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
2987 let gotooutline (_, _, kind) =
2988 match kind with
2989 | Onone -> ()
2990 | Oanchor ((pageno, y, _) as anchor) ->
2991 addnav ();
2992 gotoxy state.x @@
2993 getanchory (if conf.presentation then (pageno, y, 1.0) else anchor)
2994 | Ouri uri -> gotounder (Ulinkuri uri)
2995 | Olaunch cmd -> error "gotounder (Ulaunch %S)" cmd
2996 | Oremote (remote, pageno) ->
2997 error "gotounder (Uremote (%S,%d) )" remote pageno
2998 | Ohistory hist -> gotohist hist
2999 | Oremotedest (path, dest) ->
3000 error "gotounder (Uremotedest (%S, %S))" path dest
3003 class outlinesoucebase fetchoutlines = object (self)
3004 inherit lvsourcebase
3005 val mutable m_items = E.a
3006 val mutable m_minfo = E.a
3007 val mutable m_orig_items = E.a
3008 val mutable m_orig_minfo = E.a
3009 val mutable m_narrow_patterns = []
3010 val mutable m_gen = -1
3012 method getitemcount = Array.length m_items
3014 method getitem n =
3015 let s, n, _ = m_items.(n) in
3016 (s, n+0)
3018 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3019 ignore (uioh, first);
3020 let items, minfo =
3021 if m_narrow_patterns = []
3022 then m_orig_items, m_orig_minfo
3023 else m_items, m_minfo
3025 m_pan <- pan;
3026 if not cancel
3027 then (
3028 m_items <- items;
3029 m_minfo <- minfo;
3030 gotooutline m_items.(active);
3032 else (
3033 m_items <- items;
3034 m_minfo <- minfo;
3036 None
3038 method hasaction (_:int) = true
3040 method greetmsg =
3041 if Array.length m_items != Array.length m_orig_items
3042 then
3043 let s =
3044 match m_narrow_patterns with
3045 | one :: [] -> one
3046 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3048 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3049 else E.s
3051 method statestr =
3052 match m_narrow_patterns with
3053 | [] -> E.s
3054 | one :: [] -> one
3055 | head :: _ -> Utf8syms.ellipsis ^ head
3057 method narrow pattern =
3058 match Str.regexp_case_fold pattern with
3059 | exception _ -> ()
3060 | re ->
3061 let rec loop accu minfo n =
3062 if n = -1
3063 then (
3064 m_items <- Array.of_list accu;
3065 m_minfo <- Array.of_list minfo;
3067 else
3068 let (s, _, _) as o = m_items.(n) in
3069 let accu, minfo =
3070 match Str.search_forward re s 0 with
3071 | exception Not_found -> accu, minfo
3072 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3074 loop accu minfo (n-1)
3076 loop [] [] (Array.length m_items - 1)
3078 method! getminfo = m_minfo
3080 method denarrow =
3081 m_orig_items <- fetchoutlines ();
3082 m_minfo <- m_orig_minfo;
3083 m_items <- m_orig_items
3085 method add_narrow_pattern pattern =
3086 m_narrow_patterns <- pattern :: m_narrow_patterns
3088 method del_narrow_pattern =
3089 match m_narrow_patterns with
3090 | _ :: rest -> m_narrow_patterns <- rest
3091 | [] -> ()
3093 method renarrow =
3094 self#denarrow;
3095 match m_narrow_patterns with
3096 | pattern :: [] -> self#narrow pattern; pattern
3097 | list ->
3098 List.fold_left (fun accu pattern ->
3099 self#narrow pattern;
3100 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3102 method calcactive (_:anchor) = 0
3104 method reset anchor items =
3105 if state.gen != m_gen
3106 then (
3107 m_orig_items <- items;
3108 m_items <- items;
3109 m_narrow_patterns <- [];
3110 m_minfo <- E.a;
3111 m_orig_minfo <- E.a;
3112 m_gen <- state.gen;
3114 else (
3115 if items != m_orig_items
3116 then (
3117 m_orig_items <- items;
3118 if m_narrow_patterns == []
3119 then m_items <- items;
3122 let active = self#calcactive anchor in
3123 m_active <- active;
3124 m_first <- firstof m_first active
3128 let outlinesource fetchoutlines =
3129 (object
3130 inherit outlinesoucebase fetchoutlines
3131 method! calcactive anchor =
3132 let rely = getanchory anchor in
3133 let rec loop n best bestd =
3134 if n = Array.length m_items
3135 then best
3136 else
3137 let _, _, kind = m_items.(n) in
3138 match kind with
3139 | Oanchor anchor ->
3140 let orely = getanchory anchor in
3141 let d = abs (orely - rely) in
3142 if d < bestd
3143 then loop (n+1) n d
3144 else loop (n+1) best bestd
3145 | Onone | Oremote _ | Olaunch _
3146 | Oremotedest _ | Ouri _ | Ohistory _ ->
3147 loop (n+1) best bestd
3149 loop 0 ~-1 max_int
3150 end)
3153 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3154 let mkselector sourcetype =
3155 let fetchoutlines () =
3156 match sourcetype with
3157 | `bookmarks -> Array.of_list state.bookmarks
3158 | `outlines -> state.outlines
3159 | `history -> genhistoutlines () |> Array.of_list
3161 let source =
3162 if sourcetype = `history
3163 then new outlinesoucebase fetchoutlines
3164 else outlinesource fetchoutlines
3166 (fun errmsg ->
3167 let outlines = fetchoutlines () in
3168 if Array.length outlines = 0
3169 then showtext ' ' errmsg
3170 else (
3171 resetmstate ();
3172 Wsi.setcursor Wsi.CURSOR_INHERIT;
3173 let anchor = getanchor () in
3174 source#reset anchor outlines;
3175 state.text <- source#greetmsg;
3176 state.uioh <-
3177 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3178 postRedisplay "enter selector";
3182 let mkenter sourcetype errmsg = fun () -> mkselector sourcetype errmsg in
3183 ( mkenter `outlines "document has no outline"
3184 , mkenter `bookmarks "document has no bookmarks (yet)"
3185 , mkenter `history "history is empty" )
3188 let quickbookmark ?title () =
3189 match state.layout with
3190 | [] -> ()
3191 | l :: _ ->
3192 let title =
3193 match title with
3194 | None ->
3195 Unix.(
3196 let tm = localtime (now ()) in
3197 Printf.sprintf
3198 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3199 (l.pageno+1)
3200 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3202 | Some title -> title
3204 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3207 let setautoscrollspeed step goingdown =
3208 let incr = max 1 ((abs step) / 2) in
3209 let incr = if goingdown then incr else -incr in
3210 let astep = boundastep state.winh (step + incr) in
3211 state.autoscroll <- Some astep;
3214 let canpan () =
3215 match conf.columns with
3216 | Csplit _ -> true
3217 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3220 let panbound x = bound x (-state.w) state.winw;;
3222 let existsinrow pageno (columns, coverA, coverB) p =
3223 let last = ((pageno - coverA) mod columns) + columns in
3224 let rec any = function
3225 | [] -> false
3226 | l :: rest ->
3227 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3228 then p l
3229 else (
3230 if not (p l)
3231 then (if l.pageno = last then false else any rest)
3232 else true
3235 any state.layout
3238 let nextpage () =
3239 match state.layout with
3240 | [] ->
3241 let pageno = page_of_y state.y in
3242 gotoxy state.x (getpagey (pageno+1))
3243 | l :: rest ->
3244 match conf.columns with
3245 | Csingle _ ->
3246 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3247 then
3248 let y = clamp (pgscale state.winh) in
3249 gotoxy state.x y
3250 else
3251 let pageno = min (l.pageno+1) (state.pagecount-1) in
3252 gotoxy state.x (getpagey pageno)
3253 | Cmulti ((c, _, _) as cl, _) ->
3254 if conf.presentation
3255 && (existsinrow l.pageno cl
3256 (fun l -> l.pageh > l.pagey + l.pagevh))
3257 then
3258 let y = clamp (pgscale state.winh) in
3259 gotoxy state.x y
3260 else
3261 let pageno = min (l.pageno+c) (state.pagecount-1) in
3262 gotoxy state.x (getpagey pageno)
3263 | Csplit (n, _) ->
3264 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3265 then
3266 let pagey, pageh = getpageyh l.pageno in
3267 let pagey = pagey + pageh * l.pagecol in
3268 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3269 gotoxy state.x (pagey + pageh + ips)
3272 let prevpage () =
3273 match state.layout with
3274 | [] ->
3275 let pageno = page_of_y state.y in
3276 gotoxy state.x (getpagey (pageno-1))
3277 | l :: _ ->
3278 match conf.columns with
3279 | Csingle _ ->
3280 if conf.presentation && l.pagey != 0
3281 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3282 else
3283 let pageno = max 0 (l.pageno-1) in
3284 gotoxy state.x (getpagey pageno)
3285 | Cmulti ((c, _, coverB) as cl, _) ->
3286 if conf.presentation &&
3287 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3288 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3289 else
3290 let decr =
3291 if l.pageno = state.pagecount - coverB
3292 then 1
3293 else c
3295 let pageno = max 0 (l.pageno-decr) in
3296 gotoxy state.x (getpagey pageno)
3297 | Csplit (n, _) ->
3298 let y =
3299 if l.pagecol = 0
3300 then
3301 if l.pageno = 0
3302 then l.pagey
3303 else
3304 let pageno = max 0 (l.pageno-1) in
3305 let pagey, pageh = getpageyh pageno in
3306 pagey + (n-1)*pageh
3307 else
3308 let pagey, pageh = getpageyh l.pageno in
3309 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3311 gotoxy state.x y
3314 let save () =
3315 if emptystr conf.savecmd
3316 then adderrmsg "savepath-command is empty"
3317 "don't know where to save modified document"
3318 else
3319 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3320 let path =
3321 getcmdoutput
3322 (fun exn ->
3323 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3324 savecmd
3326 if nonemptystr path
3327 then
3328 let tmp = path ^ ".tmp" in
3329 savedoc tmp;
3330 Unix.rename tmp path;
3333 let viewkeyboard key mask =
3334 let enttext te =
3335 let mode = state.mode in
3336 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3337 state.text <- E.s;
3338 enttext ();
3339 postRedisplay "view:enttext"
3341 let ctrl = Wsi.withctrl mask in
3342 let open Keys in
3343 match Wsi.kc2kt key with
3344 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3346 | Ascii 'Q' -> exit 0
3348 | Ascii 'z' ->
3349 let yloc f =
3350 match List.rev state.rects with
3351 | [] -> ()
3352 | (pageno, _, (_, y0, _, y1, _, y2, _, y3)) :: _ ->
3353 f pageno (y0, y1, y2, y3)
3354 and yminmax (y0, y1, y2, y3) =
3355 let ym = min y0 y1 |> min y2 |> min y3 |> truncate in
3356 let yM = max y0 y1 |> max y2 |> max y3 |> truncate in
3357 ym, yM
3359 let ondone msg = state.text <- msg
3360 and zmod _ _ k =
3361 match [@warning "-4"] k with
3362 | Keys.Ascii 'z' ->
3363 let f pageno ys =
3364 let ym, yM = yminmax ys in
3365 let hh = (yM - ym)/2 in
3366 gotopage1 pageno (ym + hh - state.winh/2)
3368 yloc f;
3369 TEdone "center"
3370 | Keys.Ascii 't' ->
3371 let f pageno ys =
3372 let ym, _ = yminmax ys in
3373 gotopage1 pageno ym
3375 yloc f;
3376 TEdone "top"
3377 | Keys.Ascii 'b' ->
3378 let f pageno ys =
3379 let _, yM = yminmax ys in
3380 gotopage1 pageno (yM - state.winh)
3382 yloc f;
3383 TEdone "bottom"
3384 | _ -> TEstop
3386 enttext (": ", E.s, None, zmod state.mode, ondone, true)
3388 | Ascii 'W' ->
3389 if hasunsavedchanges ()
3390 then save ()
3392 | Insert ->
3393 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3394 then (
3395 state.mode <- (
3396 match state.lnava with
3397 | None -> LinkNav (Ltgendir 0)
3398 | Some pn -> LinkNav (Ltexact pn)
3400 gotoxy state.x state.y;
3402 else impmsg "keyboard link navigation does not work under rotation"
3404 | Escape | Ascii 'q' ->
3405 begin match state.mstate with
3406 | Mzoomrect _ ->
3407 resetmstate ();
3408 postRedisplay "kill rect";
3409 | Msel _
3410 | Mpan _
3411 | Mscrolly | Mscrollx
3412 | Mzoom _
3413 | Mnone ->
3414 begin match state.mode with
3415 | LinkNav ln ->
3416 begin match ln with
3417 | Ltexact pl -> state.lnava <- Some pl
3418 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3419 end;
3420 state.mode <- View;
3421 postRedisplay "esc leave linknav"
3422 | Birdseye _ | Textentry _ | View ->
3423 match state.ranchors with
3424 | [] -> raise Quit
3425 | (path, password, anchor, origin) :: rest ->
3426 state.ranchors <- rest;
3427 state.anchor <- anchor;
3428 state.origin <- origin;
3429 state.nameddest <- E.s;
3430 opendoc path password
3431 end;
3432 end;
3434 | Backspace ->
3435 addnavnorc ();
3436 gotoxy state.x (getnav ~-1)
3438 | Ascii 'o' -> enteroutlinemode ()
3439 | Ascii 'H' -> enterhistmode ()
3441 | Ascii 'u' ->
3442 state.rects <- [];
3443 state.text <- E.s;
3444 Hashtbl.iter (fun _ opaque ->
3445 clearmark opaque;
3446 Hashtbl.clear state.prects) state.pagemap;
3447 postRedisplay "dehighlight";
3449 | Ascii (('/' | '?') as c) ->
3450 let ondone isforw s =
3451 cbput state.hists.pat s;
3452 state.searchpattern <- s;
3453 search s isforw
3455 let s = String.make 1 c in
3456 enttext (s, E.s, Some (onhist state.hists.pat),
3457 textentry, ondone (c = '/'), true)
3459 | Ascii '+' | Ascii '=' when ctrl ->
3460 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3461 pivotzoom (conf.zoom +. incr)
3463 | Ascii '+' ->
3464 let ondone s =
3465 let n =
3466 try int_of_string s with exn ->
3467 state.text <-
3468 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3469 max_int
3471 if n != max_int
3472 then (
3473 conf.pagebias <- n;
3474 state.text <- "page bias is now " ^ string_of_int n;
3477 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3479 | Ascii '-' when ctrl ->
3480 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3481 pivotzoom (max 0.01 (conf.zoom -. decr))
3483 | Ascii '-' ->
3484 let ondone msg = state.text <- msg in
3485 enttext ("option: ", E.s, None,
3486 optentry state.mode, ondone, true)
3488 | Ascii '0' when ctrl ->
3489 if conf.zoom = 1.0
3490 then gotoxy 0 state.y
3491 else setzoom 1.0
3493 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3494 let cols =
3495 match conf.columns with
3496 | Csingle _ | Cmulti _ -> 1
3497 | Csplit (n, _) -> n
3499 let h = state.winh -
3500 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3502 let zoom = zoomforh state.winw h 0 cols in
3503 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3504 then setzoom zoom
3506 | Ascii '3' when ctrl ->
3507 let fm =
3508 match conf.fitmodel with
3509 | FitWidth -> FitProportional
3510 | FitProportional -> FitPage
3511 | FitPage -> FitWidth
3513 state.text <- "fit model: " ^ FMTE.to_string fm;
3514 reqlayout conf.angle fm
3516 | Ascii '4' when ctrl ->
3517 let zoom = getmaxw () /. float state.winw in
3518 if zoom > 0.0 then setzoom zoom
3520 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3522 | Ascii ('0'..'9' as c) when not ctrl ->
3523 let ondone s =
3524 let n =
3525 try int_of_string s with exn ->
3526 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3529 if n >= 0
3530 then (
3531 addnav ();
3532 cbput state.hists.pag (string_of_int n);
3533 gotopage1 (n + conf.pagebias - 1) 0;
3536 let pageentry text = function [@warning "-4"]
3537 | Keys.Ascii 'g' -> TEdone text
3538 | key -> intentry text key
3540 let text = String.make 1 c in
3541 enttext (":", text, Some (onhist state.hists.pag),
3542 pageentry, ondone, true)
3544 | Ascii 'b' ->
3545 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3546 postRedisplay "toggle scrollbar";
3548 | Ascii 'B' ->
3549 state.bzoom <- not state.bzoom;
3550 state.rects <- [];
3551 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3553 | Ascii 'l' ->
3554 conf.hlinks <- not conf.hlinks;
3555 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3556 postRedisplay "toggle highlightlinks";
3558 | Ascii 'F' ->
3559 if conf.angle mod 360 = 0
3560 then (
3561 state.glinks <- true;
3562 let mode = state.mode in
3563 state.mode <-
3564 Textentry (
3565 (":", E.s, None, linknentry, linknact gotounder, false),
3566 (fun _ ->
3567 state.glinks <- false;
3568 state.mode <- mode)
3570 state.text <- E.s;
3571 postRedisplay "view:linkent(F)"
3573 else impmsg "hint mode does not work under rotation"
3575 | Ascii 'y' ->
3576 state.glinks <- true;
3577 let mode = state.mode in
3578 state.mode <-
3579 Textentry (
3580 (":", E.s, None, linknentry,
3581 linknact (fun under ->
3582 selstring conf.selcmd (undertext under)), false),
3583 (fun _ ->
3584 state.glinks <- false;
3585 state.mode <- mode)
3587 state.text <- E.s;
3588 postRedisplay "view:linkent"
3590 | Ascii 'a' ->
3591 begin match state.autoscroll with
3592 | Some step ->
3593 conf.autoscrollstep <- step;
3594 state.autoscroll <- None
3595 | None ->
3596 state.autoscroll <- Some conf.autoscrollstep;
3597 state.slideshow <- state.slideshow land lnot 2
3600 | Ascii 'p' when ctrl ->
3601 launchpath () (* XXX where do error messages go? *)
3603 | Ascii 'P' ->
3604 setpresentationmode (not conf.presentation);
3605 showtext ' ' ("presentation mode " ^
3606 if conf.presentation then "on" else "off");
3608 | Ascii 'f' ->
3609 if List.mem Wsi.Fullscreen state.winstate
3610 then Wsi.reshape conf.cwinw conf.cwinh
3611 else Wsi.fullscreen ()
3613 | Ascii ('p'|'N') -> search state.searchpattern false
3614 | Ascii 'n' | Fn 3 -> search state.searchpattern true
3616 | Ascii 't' ->
3617 begin match state.layout with
3618 | [] -> ()
3619 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3622 | Ascii ' ' -> nextpage ()
3623 | Delete -> prevpage ()
3624 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3626 | Ascii 'w' ->
3627 begin match state.layout with
3628 | [] -> ()
3629 | l :: _ ->
3630 Wsi.reshape l.pagew l.pageh;
3631 postRedisplay "w"
3634 | Ascii '\'' -> enterbookmarkmode ()
3635 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3636 | Ascii 'i' -> enterinfomode ()
3637 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3639 | Ascii 'm' ->
3640 let ondone s =
3641 match state.layout with
3642 | l :: _ when nonemptystr s ->
3643 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3644 | _ -> ()
3646 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3648 | Ascii '~' ->
3649 quickbookmark ();
3650 showtext ' ' "Quick bookmark added";
3652 | Ascii 'x' -> state.roam ()
3654 | Ascii ('<'|'>' as c) ->
3655 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3657 | Ascii ('['|']' as c) ->
3658 conf.colorscale <-
3659 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3660 postRedisplay "brightness";
3662 | Ascii 'c' when state.mode = View ->
3663 if Wsi.withalt mask
3664 then (
3665 if conf.zoom > 1.0
3666 then
3667 let m = (state.winw - state.w) / 2 in
3668 gotoxy m state.y
3670 else
3671 let (c, a, b), z =
3672 match state.prevcolumns with
3673 | None -> (1, 0, 0), 1.0
3674 | Some (columns, z) ->
3675 let cab =
3676 match columns with
3677 | Csplit (c, _) -> -c, 0, 0
3678 | Cmulti ((c, a, b), _) -> c, a, b
3679 | Csingle _ -> 1, 0, 0
3681 cab, z
3683 setcolumns View c a b;
3684 setzoom z
3686 | Down | Up when ctrl && Wsi.withshift mask ->
3687 let zoom, x = state.prevzoom in
3688 setzoom zoom;
3689 state.x <- x;
3691 | Up ->
3692 begin match state.autoscroll with
3693 | None ->
3694 begin match state.mode with
3695 | Birdseye beye -> upbirdseye 1 beye
3696 | Textentry _ | View | LinkNav _ ->
3697 if ctrl
3698 then gotoxy state.x (clamp ~-(state.winh/2))
3699 else (
3700 if not (Wsi.withshift mask) && conf.presentation
3701 then prevpage ()
3702 else gotoxy state.x (clamp (-conf.scrollstep))
3705 | Some n -> setautoscrollspeed n false
3708 | Down ->
3709 begin match state.autoscroll with
3710 | None ->
3711 begin match state.mode with
3712 | Birdseye beye -> downbirdseye 1 beye
3713 | Textentry _ | View | LinkNav _ ->
3714 if ctrl
3715 then gotoxy state.x (clamp (state.winh/2))
3716 else (
3717 if not (Wsi.withshift mask) && conf.presentation
3718 then nextpage ()
3719 else gotoxy state.x (clamp (conf.scrollstep))
3722 | Some n -> setautoscrollspeed n true
3725 | Left | Right when not (Wsi.withalt mask) ->
3726 if canpan ()
3727 then
3728 let dx =
3729 if ctrl
3730 then state.winw / 2
3731 else conf.hscrollstep
3733 let dx =
3734 let pv = Wsi.kc2kt key in
3735 if pv = Keys.Left then dx else -dx
3737 gotoxy (panbound (state.x + dx)) state.y
3738 else (
3739 state.text <- E.s;
3740 postRedisplay "left/right"
3743 | Prior ->
3744 let y =
3745 if ctrl
3746 then
3747 match state.layout with
3748 | [] -> state.y
3749 | l :: _ -> state.y - l.pagey
3750 else clamp (pgscale (-state.winh))
3752 gotoxy state.x y
3754 | Next ->
3755 let y =
3756 if ctrl
3757 then
3758 match List.rev state.layout with
3759 | [] -> state.y
3760 | l :: _ -> getpagey l.pageno
3761 else clamp (pgscale state.winh)
3763 gotoxy state.x y
3765 | Ascii 'g' | Home ->
3766 addnav ();
3767 gotoxy 0 0
3768 | Ascii 'G' | End ->
3769 addnav ();
3770 gotoxy 0 (clamp state.maxy)
3772 | Right when Wsi.withalt mask ->
3773 addnavnorc ();
3774 gotoxy state.x (getnav 1)
3775 | Left when Wsi.withalt mask ->
3776 addnavnorc ();
3777 gotoxy state.x (getnav ~-1)
3779 | Ascii 'r' ->
3780 reload ()
3782 | Ascii 'v' when conf.debug ->
3783 state.rects <- [];
3784 List.iter (fun l ->
3785 match getopaque l.pageno with
3786 | None -> ()
3787 | Some opaque ->
3788 let x0, y0, x1, y1 = pagebbox opaque in
3789 let rect = (float x0, float y0,
3790 float x1, float y0,
3791 float x1, float y1,
3792 float x0, float y1) in
3793 debugrect rect;
3794 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3795 state.rects <- (l.pageno, color, rect) :: state.rects;
3796 ) state.layout;
3797 postRedisplay "v";
3799 | Ascii '|' ->
3800 let mode = state.mode in
3801 let cmd = ref E.s in
3802 let onleave = function
3803 | Cancel -> state.mode <- mode
3804 | Confirm ->
3805 List.iter (fun l ->
3806 match getopaque l.pageno with
3807 | Some opaque -> pipesel opaque !cmd
3808 | None -> ()) state.layout;
3809 state.mode <- mode
3811 let ondone s =
3812 cbput state.hists.sel s;
3813 cmd := s
3815 let te =
3816 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3818 postRedisplay "|";
3819 state.mode <- Textentry (te, onleave);
3821 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3822 vlog "huh? %s" (Wsi.keyname key)
3825 let linknavkeyboard key mask linknav =
3826 let pv = Wsi.kc2kt key in
3827 let getpage pageno =
3828 let rec loop = function
3829 | [] -> None
3830 | l :: _ when l.pageno = pageno -> Some l
3831 | _ :: rest -> loop rest
3832 in loop state.layout
3834 let doexact (pageno, n) =
3835 match getopaque pageno, getpage pageno with
3836 | Some opaque, Some l ->
3837 if pv = Keys.Enter
3838 then
3839 let under = getlink opaque n in
3840 postRedisplay "link gotounder";
3841 gotounder under;
3842 state.mode <- View;
3843 else
3844 let opt, dir =
3845 let open Keys in
3846 match pv with
3847 | Home -> Some (findlink opaque LDfirst), -1
3848 | End -> Some (findlink opaque LDlast), 1
3849 | Left -> Some (findlink opaque (LDleft n)), -1
3850 | Right -> Some (findlink opaque (LDright n)), 1
3851 | Up -> Some (findlink opaque (LDup n)), -1
3852 | Down -> Some (findlink opaque (LDdown n)), 1
3853 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3854 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3856 let pwl l dir =
3857 begin match findpwl l.pageno dir with
3858 | Pwlnotfound -> ()
3859 | Pwl pageno ->
3860 let notfound dir =
3861 state.mode <- LinkNav (Ltgendir dir);
3862 let y, h = getpageyh pageno in
3863 let y =
3864 if dir < 0
3865 then y + h - state.winh
3866 else y
3868 gotoxy state.x y
3870 begin match getopaque pageno, getpage pageno with
3871 | Some opaque, Some _ ->
3872 let link =
3873 let ld = if dir > 0 then LDfirst else LDlast in
3874 findlink opaque ld
3876 begin match link with
3877 | Lfound m ->
3878 showlinktype (getlink opaque m);
3879 state.mode <- LinkNav (Ltexact (pageno, m));
3880 postRedisplay "linknav jpage";
3881 | Lnotfound -> notfound dir
3882 end;
3883 | _ -> notfound dir
3884 end;
3885 end;
3887 begin match opt with
3888 | Some Lnotfound -> pwl l dir;
3889 | Some (Lfound m) ->
3890 if m = n
3891 then pwl l dir
3892 else (
3893 let _, y0, _, y1 = getlinkrect opaque m in
3894 if y0 < l.pagey
3895 then gotopage1 l.pageno y0
3896 else (
3897 let d = fstate.fontsize + 1 in
3898 if y1 - l.pagey > l.pagevh - d
3899 then gotopage1 l.pageno (y1 - state.winh + d)
3900 else postRedisplay "linknav";
3902 showlinktype (getlink opaque m);
3903 state.mode <- LinkNav (Ltexact (l.pageno, m));
3906 | None -> viewkeyboard key mask
3907 end;
3908 | _ -> viewkeyboard key mask
3910 if pv = Keys.Insert
3911 then (
3912 begin match linknav with
3913 | Ltexact pa -> state.lnava <- Some pa
3914 | Ltgendir _ | Ltnotready _ -> ()
3915 end;
3916 state.mode <- View;
3917 postRedisplay "leave linknav"
3919 else
3920 match linknav with
3921 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3922 | Ltexact exact -> doexact exact
3925 let keyboard key mask =
3926 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3927 then wcmd "interrupt"
3928 else state.uioh <- state.uioh#key key mask
3931 let birdseyekeyboard key mask
3932 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3933 let incr =
3934 match conf.columns with
3935 | Csingle _ -> 1
3936 | Cmulti ((c, _, _), _) -> c
3937 | Csplit _ -> error "bird's eye split mode"
3939 let pgh layout = List.fold_left
3940 (fun m l -> max l.pageh m) state.winh layout in
3941 let open Keys in
3942 match Wsi.kc2kt key with
3943 | Ascii 'l' when Wsi.withctrl mask ->
3944 let y, h = getpageyh pageno in
3945 let top = (state.winh - h) / 2 in
3946 gotoxy state.x (max 0 (y - top))
3947 | Enter -> leavebirdseye beye false
3948 | Escape -> leavebirdseye beye true
3949 | Up -> upbirdseye incr beye
3950 | Down -> downbirdseye incr beye
3951 | Left -> upbirdseye 1 beye
3952 | Right -> downbirdseye 1 beye
3954 | Prior ->
3955 begin match state.layout with
3956 | l :: _ ->
3957 if l.pagey != 0
3958 then (
3959 state.mode <- Birdseye (
3960 oconf, leftx, l.pageno, hooverpageno, anchor
3962 gotopage1 l.pageno 0;
3964 else (
3965 let layout = layout state.x (state.y-state.winh)
3966 state.winw
3967 (pgh state.layout) in
3968 match layout with
3969 | [] -> gotoxy state.x (clamp (-state.winh))
3970 | l :: _ ->
3971 state.mode <- Birdseye (
3972 oconf, leftx, l.pageno, hooverpageno, anchor
3974 gotopage1 l.pageno 0
3977 | [] -> gotoxy state.x (clamp (-state.winh))
3978 end;
3980 | Next ->
3981 begin match List.rev state.layout with
3982 | l :: _ ->
3983 let layout = layout state.x
3984 (state.y + (pgh state.layout))
3985 state.winw state.winh in
3986 begin match layout with
3987 | [] ->
3988 let incr = l.pageh - l.pagevh in
3989 if incr = 0
3990 then (
3991 state.mode <-
3992 Birdseye (
3993 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
3995 postRedisplay "birdseye pagedown";
3997 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
3999 | l :: _ ->
4000 state.mode <-
4001 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4002 gotopage1 l.pageno 0;
4005 | [] -> gotoxy state.x (clamp state.winh)
4006 end;
4008 | Home ->
4009 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4010 gotopage1 0 0
4012 | End ->
4013 let pageno = state.pagecount - 1 in
4014 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4015 if not (pagevisible state.layout pageno)
4016 then
4017 let h =
4018 match List.rev state.pdims with
4019 | [] -> state.winh
4020 | (_, _, h, _) :: _ -> h
4022 gotoxy
4023 state.x
4024 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4025 else postRedisplay "birdseye end";
4027 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4030 let drawpage l =
4031 let color =
4032 match state.mode with
4033 | Textentry _ -> scalecolor 0.4
4034 | LinkNav _ | View -> scalecolor 1.0
4035 | Birdseye (_, _, pageno, hooverpageno, _) ->
4036 if l.pageno = hooverpageno
4037 then scalecolor 0.9
4038 else (
4039 if l.pageno = pageno
4040 then (
4041 let c = scalecolor 1.0 in
4042 GlDraw.color c;
4043 GlDraw.line_width 3.0;
4044 let dispx = l.pagedispx in
4045 linerect
4046 (float (dispx-1)) (float (l.pagedispy-1))
4047 (float (dispx+l.pagevw+1))
4048 (float (l.pagedispy+l.pagevh+1));
4049 GlDraw.line_width 1.0;
4052 else scalecolor 0.8
4055 drawtiles l color;
4058 let postdrawpage l linkindexbase =
4059 match getopaque l.pageno with
4060 | Some opaque ->
4061 if tileready l l.pagex l.pagey
4062 then
4063 let x = l.pagedispx - l.pagex
4064 and y = l.pagedispy - l.pagey in
4065 let hlmask =
4066 match conf.columns with
4067 | Csingle _ | Cmulti _ ->
4068 (if conf.hlinks then 1 else 0)
4069 + (if state.glinks
4070 && not (isbirdseye state.mode) then 2 else 0)
4071 | Csplit _ -> 0
4073 let s =
4074 match state.mode with
4075 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4076 | Textentry _
4077 | Birdseye _
4078 | View
4079 | LinkNav _ -> E.s
4081 Hashtbl.find_all state.prects l.pageno |>
4082 List.iter (fun vals -> drawprect opaque x y vals);
4083 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4084 if n < 0
4085 then (Glutils.redisplay := true; 0)
4086 else n
4087 else 0
4088 | _ -> 0
4091 let scrollindicator () =
4092 let sbw, ph, sh = state.uioh#scrollph in
4093 let sbh, pw, sw = state.uioh#scrollpw in
4095 let x0,x1,hx0 =
4096 if conf.leftscroll
4097 then (0, sbw, sbw)
4098 else ((state.winw - sbw), state.winw, 0)
4101 Gl.enable `blend;
4102 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4103 let (r, g, b, alpha) = conf.sbarcolor in
4104 GlDraw.color (r, g, b) ~alpha;
4105 filledrect (float x0) 0. (float x1) (float state.winh);
4106 filledrect
4107 (float hx0) (float (state.winh - sbh))
4108 (float (hx0 + state.winw)) (float state.winh);
4109 let (r, g, b, alpha) = conf.sbarhndlcolor in
4110 GlDraw.color (r, g, b) ~alpha;
4112 filledrect (float x0) ph (float x1) (ph +. sh);
4113 let pw = pw +. float hx0 in
4114 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4115 Gl.disable `blend;
4118 let showsel () =
4119 match state.mstate with
4120 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4121 | Msel ((x0, y0), (x1, y1)) ->
4122 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4123 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4124 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4125 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4128 let showrects = function
4129 | [] -> ()
4130 | rects ->
4131 Gl.enable `blend;
4132 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4133 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4134 List.iter
4135 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4136 List.iter (fun l ->
4137 if l.pageno = pageno
4138 then
4139 let dx = float (l.pagedispx - l.pagex) in
4140 let dy = float (l.pagedispy - l.pagey) in
4141 let r, g, b, alpha = c in
4142 GlDraw.color (r, g, b) ~alpha;
4143 filledrect2
4144 (x0+.dx) (y0+.dy)
4145 (x1+.dx) (y1+.dy)
4146 (x3+.dx) (y3+.dy)
4147 (x2+.dx) (y2+.dy);
4148 ) state.layout
4149 ) rects;
4150 Gl.disable `blend;
4153 let display () =
4154 GlDraw.color (scalecolor2 conf.bgcolor);
4155 GlClear.color (scalecolor2 conf.bgcolor);
4156 GlClear.clear [`color];
4157 List.iter drawpage state.layout;
4158 let rects =
4159 match state.mode with
4160 | LinkNav (Ltexact (pageno, linkno)) ->
4161 begin match getopaque pageno with
4162 | Some opaque ->
4163 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4164 let color = (0.0, 0.0, 0.5, 0.5) in
4165 (pageno, color,
4166 (float x0, float y0,
4167 float x1, float y0,
4168 float x1, float y1,
4169 float x0, float y1)
4170 ) :: state.rects
4171 | None -> state.rects
4173 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4174 | Birdseye _
4175 | Textentry _
4176 | View -> state.rects
4178 showrects rects;
4179 let rec postloop linkindexbase = function
4180 | l :: rest ->
4181 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4182 postloop linkindexbase rest
4183 | [] -> ()
4185 showsel ();
4186 postloop 0 state.layout;
4187 state.uioh#display;
4188 begin match state.mstate with
4189 | Mzoomrect ((x0, y0), (x1, y1)) ->
4190 Gl.enable `blend;
4191 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4192 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4193 filledrect (float x0) (float y0) (float x1) (float y1);
4194 Gl.disable `blend;
4195 | Msel _
4196 | Mpan _
4197 | Mscrolly | Mscrollx
4198 | Mzoom _
4199 | Mnone -> ()
4200 end;
4201 enttext ();
4202 scrollindicator ();
4203 Wsi.swapb ();
4206 let display () =
4207 match state.reload with
4208 | Some (x, y, t) ->
4209 if x != state.x || y != state.y || abs_float @@ now () -. t > 0.5
4210 || (state.layout != [] && layoutready state.layout)
4211 then (
4212 state.reload <- None;
4213 display ()
4215 | None -> display ()
4218 let zoomrect x y x1 y1 =
4219 let x0 = min x x1
4220 and x1 = max x x1
4221 and y0 = min y y1 in
4222 let zoom = (float state.w) /. float (x1 - x0) in
4223 let margin =
4224 let simple () =
4225 if state.w < state.winw
4226 then (state.winw - state.w) / 2
4227 else 0
4229 match conf.fitmodel with
4230 | FitWidth | FitProportional -> simple ()
4231 | FitPage ->
4232 match conf.columns with
4233 | Csplit _ ->
4234 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4235 | Cmulti _ | Csingle _ -> simple ()
4237 gotoxy ((state.x + margin) - x0) (state.y + y0);
4238 state.anchor <- getanchor ();
4239 setzoom zoom;
4240 resetmstate ();
4243 let annot inline x y =
4244 match unproject x y with
4245 | Some (opaque, n, ux, uy) ->
4246 let add text =
4247 addannot opaque ux uy text;
4248 wcmd "freepage %s" (~> opaque);
4249 Hashtbl.remove state.pagemap (n, state.gen);
4250 flushtiles ();
4251 gotoxy state.x state.y
4253 if inline
4254 then
4255 let ondone s = add s in
4256 let mode = state.mode in
4257 state.mode <- Textentry (
4258 ("annotation: ", E.s, None, textentry, ondone, true),
4259 fun _ -> state.mode <- mode);
4260 state.text <- E.s;
4261 enttext ();
4262 postRedisplay "annot"
4263 else add @@ getusertext E.s
4264 | _ -> ()
4267 let zoomblock x y =
4268 let g opaque l px py =
4269 match rectofblock opaque px py with
4270 | Some a ->
4271 let x0 = a.(0) -. 20. in
4272 let x1 = a.(1) +. 20. in
4273 let y0 = a.(2) -. 20. in
4274 let zoom = (float state.w) /. (x1 -. x0) in
4275 let pagey = getpagey l.pageno in
4276 let margin = (state.w - l.pagew)/2 in
4277 let nx = -truncate x0 - margin in
4278 gotoxy nx (pagey + truncate y0);
4279 state.anchor <- getanchor ();
4280 setzoom zoom;
4281 None
4282 | None -> None
4284 match conf.columns with
4285 | Csplit _ ->
4286 impmsg "block zooming does not work properly in split columns mode"
4287 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4290 let scrollx x =
4291 let winw = state.winw - 1 in
4292 let s = float x /. float winw in
4293 let destx = truncate (float (state.w + winw) *. s) in
4294 gotoxy (winw - destx) state.y;
4295 state.mstate <- Mscrollx;
4298 let scrolly y =
4299 let s = float y /. float state.winh in
4300 let desty = truncate (s *. float (maxy ())) in
4301 gotoxy state.x desty;
4302 state.mstate <- Mscrolly;
4305 let viewmulticlick clicks x y mask =
4306 let g opaque l px py =
4307 let mark =
4308 match clicks with
4309 | 2 -> Mark_word
4310 | 3 -> Mark_line
4311 | 4 -> Mark_block
4312 | _ -> Mark_page
4314 if markunder opaque px py mark
4315 then (
4316 Some (fun () ->
4317 let dopipe cmd =
4318 match getopaque l.pageno with
4319 | None -> ()
4320 | Some opaque -> pipesel opaque cmd
4322 state.roam <- (fun () -> dopipe conf.paxcmd);
4323 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4326 else None
4328 postRedisplay "viewmulticlick";
4329 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4332 let canselect () =
4333 match conf.columns with
4334 | Csplit _ -> false
4335 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4338 let viewmouse button down x y mask =
4339 match button with
4340 | n when (n == 4 || n == 5) && not down ->
4341 if Wsi.withctrl mask
4342 then (
4343 let incr =
4344 if n = 5
4345 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4346 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4348 let fx, fy =
4349 match state.mstate with
4350 | Mzoom (oldn, _, pos) when n = oldn -> pos
4351 | Mzoomrect _ | Mnone | Mpan _
4352 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4354 let zoom = conf.zoom -. incr in
4355 state.mstate <- Mzoom (n, 0, (x, y));
4356 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4357 then pivotzoom ~x ~y zoom
4358 else pivotzoom zoom
4360 else (
4361 match state.autoscroll with
4362 | Some step -> setautoscrollspeed step (n=4)
4363 | None ->
4364 if conf.wheelbypage || conf.presentation
4365 then (
4366 if n = 4
4367 then prevpage ()
4368 else nextpage ()
4370 else
4371 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4372 let incr = incr * 2 in
4373 let y = clamp incr in
4374 gotoxy state.x y
4377 | n when (n = 6 || n = 7) && not down && canpan () ->
4378 let x =
4379 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4380 gotoxy x state.y
4382 | 1 when Wsi.withshift mask ->
4383 state.mstate <- Mnone;
4384 if not down
4385 then (
4386 match unproject x y with
4387 | None -> ()
4388 | Some (_, pageno, ux, uy) ->
4389 let cmd = Printf.sprintf
4390 "%s %s %d %d %d"
4391 conf.stcmd state.path pageno ux uy
4393 match spawn cmd [] with
4394 | exception exn ->
4395 impmsg "execution of synctex command(%S) failed: %S"
4396 conf.stcmd @@ exntos exn
4397 | _pid -> ()
4400 | 1 when Wsi.withctrl mask ->
4401 if down
4402 then (
4403 Wsi.setcursor Wsi.CURSOR_FLEUR;
4404 state.mstate <- Mpan (x, y)
4406 else state.mstate <- Mnone
4408 | 3 ->
4409 if down
4410 then (
4411 if Wsi.withshift mask
4412 then (
4413 annot conf.annotinline x y;
4414 postRedisplay "addannot"
4416 else
4417 let p = (x, y) in
4418 Wsi.setcursor Wsi.CURSOR_CYCLE;
4419 state.mstate <- Mzoomrect (p, p)
4421 else (
4422 match state.mstate with
4423 | Mzoomrect ((x0, y0), _) ->
4424 if abs (x-x0) > 10 && abs (y - y0) > 10
4425 then zoomrect x0 y0 x y
4426 else (
4427 resetmstate ();
4428 postRedisplay "kill accidental zoom rect";
4430 | Msel _
4431 | Mpan _
4432 | Mscrolly | Mscrollx
4433 | Mzoom _
4434 | Mnone -> resetmstate ()
4437 | 1 when vscrollhit x ->
4438 if down
4439 then
4440 let _, position, sh = state.uioh#scrollph in
4441 if y > truncate position && y < truncate (position +. sh)
4442 then state.mstate <- Mscrolly
4443 else scrolly y
4444 else state.mstate <- Mnone
4446 | 1 when y > state.winh - hscrollh () ->
4447 if down
4448 then
4449 let _, position, sw = state.uioh#scrollpw in
4450 if x > truncate position && x < truncate (position +. sw)
4451 then state.mstate <- Mscrollx
4452 else scrollx x
4453 else state.mstate <- Mnone
4455 | 1 when state.bzoom -> if not down then zoomblock x y
4457 | 1 ->
4458 let dest = if down then getunder x y else Unone in
4459 begin match dest with
4460 | Ulinkuri _ -> gotounder dest
4461 | Unone when down ->
4462 Wsi.setcursor Wsi.CURSOR_FLEUR;
4463 state.mstate <- Mpan (x, y);
4464 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4465 | Unone | Utext _ ->
4466 if down
4467 then (
4468 if canselect ()
4469 then (
4470 state.mstate <- Msel ((x, y), (x, y));
4471 postRedisplay "mouse select";
4474 else (
4475 match state.mstate with
4476 | Mnone -> ()
4477 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4478 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4479 | Mpan _ ->
4480 Wsi.setcursor Wsi.CURSOR_INHERIT;
4481 state.mstate <- Mnone
4482 | Msel ((x0, y0), (x1, y1)) ->
4483 let rec loop = function
4484 | [] -> ()
4485 | l :: rest ->
4486 let inside =
4487 let a0 = l.pagedispy in
4488 let a1 = a0 + l.pagevh in
4489 let b0 = l.pagedispx in
4490 let b1 = b0 + l.pagevw in
4491 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4492 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4494 if inside
4495 then
4496 match getopaque l.pageno with
4497 | Some opaque ->
4498 let dosel cmd () =
4499 pipef ~closew:false "Msel"
4500 (fun w ->
4501 copysel w opaque;
4502 postRedisplay "Msel") cmd
4504 dosel conf.selcmd ();
4505 state.roam <- dosel conf.paxcmd;
4506 | None -> ()
4507 else loop rest
4509 loop state.layout;
4510 resetmstate ();
4513 | _ -> ()
4516 let birdseyemouse button down x y mask
4517 (conf, leftx, _, hooverpageno, anchor) =
4518 match button with
4519 | 1 when down ->
4520 let rec loop = function
4521 | [] -> ()
4522 | l :: rest ->
4523 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4524 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4525 then
4526 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false
4527 else loop rest
4529 loop state.layout
4530 | 3 -> ()
4531 | _ -> viewmouse button down x y mask
4534 let uioh = object
4535 method display = ()
4537 method key key mask =
4538 begin match state.mode with
4539 | Textentry textentry -> textentrykeyboard key mask textentry
4540 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4541 | View -> viewkeyboard key mask
4542 | LinkNav linknav -> linknavkeyboard key mask linknav
4543 end;
4544 state.uioh
4546 method button button bstate x y mask =
4547 begin match state.mode with
4548 | LinkNav _ | View -> viewmouse button bstate x y mask
4549 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4550 | Textentry _ -> ()
4551 end;
4552 state.uioh
4554 method multiclick clicks x y mask =
4555 begin match state.mode with
4556 | LinkNav _ | View -> viewmulticlick clicks x y mask
4557 | Birdseye _ | Textentry _ -> ()
4558 end;
4559 state.uioh
4561 method motion x y =
4562 begin match state.mode with
4563 | Textentry _ -> ()
4564 | View | Birdseye _ | LinkNav _ ->
4565 match state.mstate with
4566 | Mzoom _ | Mnone -> ()
4567 | Mpan (x0, y0) ->
4568 let dx = x - x0
4569 and dy = y0 - y in
4570 state.mstate <- Mpan (x, y);
4571 let x = if canpan () then panbound (state.x + dx) else state.x in
4572 let y = clamp dy in
4573 gotoxy x y
4575 | Msel (a, _) ->
4576 state.mstate <- Msel (a, (x, y));
4577 postRedisplay "motion select";
4579 | Mscrolly ->
4580 let y = min state.winh (max 0 y) in
4581 scrolly y
4583 | Mscrollx ->
4584 let x = min state.winw (max 0 x) in
4585 scrollx x
4587 | Mzoomrect (p0, _) ->
4588 state.mstate <- Mzoomrect (p0, (x, y));
4589 postRedisplay "motion zoomrect";
4590 end;
4591 state.uioh
4593 method pmotion x y =
4594 begin match state.mode with
4595 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4596 let rec loop = function
4597 | [] ->
4598 if hooverpageno != -1
4599 then (
4600 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4601 postRedisplay "pmotion birdseye no hoover";
4603 | l :: rest ->
4604 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4605 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4606 then (
4607 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4608 postRedisplay "pmotion birdseye hoover";
4610 else loop rest
4612 loop state.layout
4614 | Textentry _ -> ()
4616 | LinkNav _ | View ->
4617 match state.mstate with
4618 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4619 | Mnone ->
4620 updateunder x y;
4621 if canselect ()
4622 then
4623 match conf.pax with
4624 | None -> ()
4625 | Some past ->
4626 let now = now () in
4627 let delta = now -. past in
4628 if delta > 0.01
4629 then paxunder x y
4630 else conf.pax <- Some now
4631 end;
4632 state.uioh
4634 method infochanged _ = ()
4636 method scrollph =
4637 let maxy = maxy () in
4638 let p, h =
4639 if maxy = 0
4640 then 0.0, float state.winh
4641 else scrollph state.y maxy
4643 vscrollw (), p, h
4645 method scrollpw =
4646 let fwinw = float (state.winw - vscrollw ()) in
4647 let sw =
4648 let sw = fwinw /. float state.w in
4649 let sw = fwinw *. sw in
4650 max sw (float conf.scrollh)
4652 let position =
4653 let maxx = state.w + state.winw in
4654 let x = state.winw - state.x in
4655 let percent = float x /. float maxx in
4656 (fwinw -. sw) *. percent
4658 hscrollh (), position, sw
4660 method modehash =
4661 let modename =
4662 match state.mode with
4663 | LinkNav _ -> "links"
4664 | Textentry _ -> "textentry"
4665 | Birdseye _ -> "birdseye"
4666 | View -> "view"
4668 findkeyhash conf modename
4670 method eformsgs = true
4671 method alwaysscrolly = false
4672 method scroll dx dy =
4673 let x = if canpan () then panbound (state.x + dx) else state.x in
4674 gotoxy x (clamp (2 * dy));
4675 state.uioh
4676 method zoom z x y =
4677 pivotzoom ~x ~y (conf.zoom *. exp z);
4678 end;;
4680 let addrect pageno r g b a x0 y0 x1 y1 =
4681 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4684 let ract cmds =
4685 let cl = splitatchar cmds ' ' in
4686 let scan s fmt f =
4687 try Scanf.sscanf s fmt f
4688 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4689 cmds @@ exntos exn
4691 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4692 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4693 s pageno r g b a x0 y0 x1 y1;
4694 onpagerect
4695 pageno
4696 (fun w h ->
4697 let _,w1,h1,_ = getpagedim pageno in
4698 let sw = float w1 /. float w
4699 and sh = float h1 /. float h in
4700 let x0s = x0 *. sw
4701 and x1s = x1 *. sw
4702 and y0s = y0 *. sh
4703 and y1s = y1 *. sh in
4704 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4705 let color = (r, g, b, a) in
4706 if conf.verbose then debugrect rect;
4707 state.rects <- (pageno, color, rect) :: state.rects;
4708 postRedisplay s;
4711 match cl with
4712 | "reload", "" -> reload ()
4713 | "goto", args ->
4714 scan args "%u %f %f"
4715 (fun pageno x y ->
4716 let cmd, _ = state.geomcmds in
4717 if emptystr cmd
4718 then gotopagexy pageno x y
4719 else
4720 let f prevf () =
4721 gotopagexy pageno x y;
4722 prevf ()
4724 state.reprf <- f state.reprf
4726 | "goto1", args -> scan args "%u %f" gotopage
4727 | "gotor", args -> scan args "%S" gotoremote
4728 | "rect", args ->
4729 scan args "%u %u %f %f %f %f"
4730 (fun pageno c x0 y0 x1 y1 ->
4731 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4732 rectx "rect" pageno color x0 y0 x1 y1;
4734 | "prect", args ->
4735 scan args "%u %f %f %f %f %f %f %f %f"
4736 (fun pageno r g b alpha x0 y0 x1 y1 ->
4737 addrect pageno r g b alpha x0 y0 x1 y1;
4738 postRedisplay "prect"
4740 | "pgoto", args ->
4741 scan args "%u %f %f"
4742 (fun pageno x y ->
4743 let optopaque =
4744 match getopaque pageno with
4745 | Some opaque -> opaque
4746 | None -> ~< E.s
4748 pgoto optopaque pageno x y;
4749 let rec fixx = function
4750 | [] -> ()
4751 | l :: rest ->
4752 if l.pageno = pageno
4753 then gotoxy (state.x - l.pagedispx) state.y
4754 else fixx rest
4756 let layout =
4757 let mult =
4758 match conf.columns with
4759 | Csingle _ | Csplit _ -> 1
4760 | Cmulti ((n, _, _), _) -> n
4762 layout 0 state.y (state.winw * mult) state.winh
4764 fixx layout
4766 | "activatewin", "" -> Wsi.activatewin ()
4767 | "quit", "" -> raise Quit
4768 | "keys", keys ->
4769 begin try
4770 let l = Config.keys_of_string keys in
4771 List.iter (fun (k, m) -> keyboard k m) l
4772 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4773 cmds @@ exntos exn
4775 | "clearrects", "" ->
4776 Hashtbl.clear state.prects;
4777 postRedisplay "clearrects"
4778 | _ ->
4779 adderrfmt "remote command"
4780 "error processing remote command: %S\n" cmds;
4783 let remote =
4784 let scratch = Bytes.create 80 in
4785 let buf = Buffer.create 80 in
4786 fun fd ->
4787 match tempfailureretry (Unix.read fd scratch 0) 80 with
4788 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4789 | 0 ->
4790 Unix.close fd;
4791 if Buffer.length buf > 0
4792 then (
4793 let s = Buffer.contents buf in
4794 Buffer.clear buf;
4795 ract s;
4797 None
4798 | n ->
4799 let rec eat ppos =
4800 let nlpos =
4801 match Bytes.index_from scratch ppos '\n' with
4802 | pos -> if pos >= n then -1 else pos
4803 | exception Not_found -> -1
4805 if nlpos >= 0
4806 then (
4807 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4808 let s = Buffer.contents buf in
4809 Buffer.clear buf;
4810 ract s;
4811 eat (nlpos+1);
4813 else (
4814 Buffer.add_subbytes buf scratch ppos (n-ppos);
4815 Some fd
4817 in eat 0
4820 let remoteopen path =
4821 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4822 with exn ->
4823 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4824 None
4827 let () =
4828 Utils.vlogf := (fun s -> if conf.verbose then prerr_endline s else ignore s);
4829 let gcconfig = ref false in
4830 let trimcachepath = ref E.s in
4831 let rcmdpath = ref E.s in
4832 let pageno = ref None in
4833 let openlast = ref false in
4834 let doreap = ref false in
4835 let csspath = ref None in
4836 selfexec := Sys.executable_name;
4837 Arg.parse
4838 (Arg.align
4839 [("-p", Arg.String (fun s -> state.password <- s),
4840 "<password> Set password");
4842 ("-f", Arg.String
4843 (fun s ->
4844 Config.fontpath := s;
4845 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4847 "<path> Set path to the user interface font");
4849 ("-c", Arg.String
4850 (fun s ->
4851 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4852 Config.confpath := s),
4853 "<path> Set path to the configuration file");
4855 ("-last", Arg.Set openlast, " Open last document");
4857 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4858 "<page-number> Jump to page");
4860 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4861 "<path> Set path to the trim cache file");
4863 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4864 "<named-destination> Set named destination");
4866 ("-remote", Arg.String (fun s -> rcmdpath := s),
4867 "<path> Set path to the source of remote commands");
4869 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4871 ("-v", Arg.Unit (fun () ->
4872 Printf.printf
4873 "%s\nconfiguration file: %s\n"
4874 (Help.version ())
4875 Config.defconfpath;
4876 exit 0), " Print version and exit");
4878 ("-css", Arg.String (fun s -> csspath := Some s),
4879 "<path> Set path to the style sheet to use with EPUB/HTML");
4881 ("-origin", Arg.String (fun s -> state.origin <- s),
4882 "<origin> <undocumented>");
4884 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4885 ("-layout-height", Arg.Set_int layouth,
4886 "<height> layout height html/epub/etc (-1, 0, N)");
4889 (fun s -> state.path <- s)
4890 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4892 let histmode = emptystr state.path && not !openlast in
4894 if not (Config.load !openlast)
4895 then dolog "failed to load configuration";
4897 begin match !pageno with
4898 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4899 | None -> ()
4900 end;
4902 fillhelp ();
4903 if !gcconfig
4904 then (
4905 Config.gc ();
4906 exit 0
4909 let mu =
4910 object (self)
4911 val mutable m_clicks = 0
4912 val mutable m_click_x = 0
4913 val mutable m_click_y = 0
4914 val mutable m_lastclicktime = infinity
4916 method private cleanup =
4917 state.roam <- noroam;
4918 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4919 method expose = postRedisplay "expose"
4920 method visible v =
4921 let name =
4922 match v with
4923 | Wsi.Unobscured -> "unobscured"
4924 | Wsi.PartiallyObscured -> "partiallyobscured"
4925 | Wsi.FullyObscured -> "fullyobscured"
4927 vlog "visibility change %s" name
4928 method display = display ()
4929 method map mapped = vlog "mapped %b" mapped
4930 method reshape w h =
4931 self#cleanup;
4932 reshape w h
4933 method mouse b d x y m =
4934 if d && canselect ()
4935 then (
4937 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4939 m_click_x <- x;
4940 m_click_y <- y;
4941 if b = 1
4942 then (
4943 let t = now () in
4944 if abs x - m_click_x > 10
4945 || abs y - m_click_y > 10
4946 || abs_float (t -. m_lastclicktime) > 0.3
4947 then m_clicks <- 0;
4948 m_clicks <- m_clicks + 1;
4949 m_lastclicktime <- t;
4950 if m_clicks = 1
4951 then (
4952 self#cleanup;
4953 postRedisplay "cleanup";
4954 state.uioh <- state.uioh#button b d x y m;
4956 else state.uioh <- state.uioh#multiclick m_clicks x y m
4958 else (
4959 self#cleanup;
4960 m_clicks <- 0;
4961 m_lastclicktime <- infinity;
4962 state.uioh <- state.uioh#button b d x y m
4965 else state.uioh <- state.uioh#button b d x y m
4966 method motion x y =
4967 state.mpos <- (x, y);
4968 state.uioh <- state.uioh#motion x y
4969 method pmotion x y =
4970 state.mpos <- (x, y);
4971 state.uioh <- state.uioh#pmotion x y
4972 method key k m =
4973 vlog "k=%#x m=%#x" k m;
4974 let mascm = m land (
4975 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
4976 ) in
4977 let keyboard k m =
4978 let x = state.x and y = state.y in
4979 keyboard k m;
4980 if x != state.x || y != state.y then self#cleanup
4982 match state.keystate with
4983 | KSnone ->
4984 let km = k, mascm in
4985 begin
4986 match
4987 let modehash = state.uioh#modehash in
4988 try Hashtbl.find modehash km
4989 with Not_found ->
4990 try Hashtbl.find (findkeyhash conf "global") km
4991 with Not_found -> KMinsrt (k, m)
4992 with
4993 | KMinsrt (k, m) -> keyboard k m
4994 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
4995 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
4997 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
4998 List.iter (fun (k, m) -> keyboard k m) insrt;
4999 state.keystate <- KSnone
5000 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
5001 state.keystate <- KSinto (keys, insrt)
5002 | KSinto _ -> state.keystate <- KSnone
5004 method enter x y =
5005 state.mpos <- (x, y);
5006 state.uioh <- state.uioh#pmotion x y
5007 method leave = state.mpos <- (-1, -1)
5008 method winstate wsl = state.winstate <- wsl
5009 method quit : 'a. 'a = raise Quit
5010 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5011 method zoom z x y = state.uioh#zoom z x y
5012 method opendoc path =
5013 state.mode <- View;
5014 state.uioh <- uioh;
5015 postRedisplay "opendoc";
5016 opendoc path state.password
5019 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5020 state.wsfd <- wsfd;
5022 if not @@ List.exists GlMisc.check_extension
5023 [ "GL_ARB_texture_rectangle"
5024 ; "GL_EXT_texture_recangle"
5025 ; "GL_NV_texture_rectangle" ]
5026 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5028 let cs, ss =
5029 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5030 | exception exn ->
5031 dolog "socketpair failed: %s" @@ exntos exn;
5032 exit 1
5033 | (r, w) ->
5034 cloexec r;
5035 cloexec w;
5036 r, w
5039 setcheckers conf.checkers;
5040 begin match !csspath with
5041 | None -> ()
5042 | Some "" -> conf.css <- E.s
5043 | Some path ->
5044 let css = filecontents path in
5045 let l = String.length css in
5046 conf.css <-
5047 if substratis css (l-2) "\r\n"
5048 then String.sub css 0 (l-2)
5049 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
5050 end;
5051 init cs (
5052 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5053 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5054 !Config.fontpath, !trimcachepath
5056 List.iter GlArray.enable [`texture_coord; `vertex];
5057 GlTex.env (`color conf.texturecolor);
5058 state.ss <- ss;
5059 reshape ~firsttime:true winw winh;
5060 state.uioh <- uioh;
5061 if histmode
5062 then (
5063 Wsi.settitle "llpp (history)";
5064 enterhistmode ();
5066 else (
5067 state.text <- "Opening " ^ (mbtoutf8 state.path);
5068 opendoc state.path state.password;
5070 display ();
5071 Wsi.mapwin ();
5072 Wsi.setcursor Wsi.CURSOR_INHERIT;
5073 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5075 let rec reap () =
5076 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5077 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5078 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5079 | 0, _ -> ()
5080 | _pid, _status -> reap ()
5082 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5084 let optrfd =
5085 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5088 let rec loop deadline =
5089 if !doreap
5090 then (
5091 doreap := false;
5092 reap ()
5094 let r = [state.ss; state.wsfd] in
5095 let r =
5096 match !optrfd with
5097 | None -> r
5098 | Some fd -> fd :: r
5100 if !redisplay
5101 then (
5102 Glutils.redisplay := false;
5103 display ();
5105 let timeout =
5106 let now = now () in
5107 if deadline > now
5108 then (
5109 if deadline = infinity
5110 then ~-.1.0
5111 else max 0.0 (deadline -. now)
5113 else 0.0
5115 let r, _, _ =
5116 try Unix.select r [] [] timeout
5117 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5119 begin match r with
5120 | [] ->
5121 let newdeadline =
5122 match state.autoscroll with
5123 | Some step when step != 0 ->
5124 if state.slideshow land 1 = 1
5125 then (
5126 if state.slideshow land 2 = 0
5127 then state.slideshow <- state.slideshow lor 2
5128 else if step < 0 then prevpage () else nextpage ();
5129 deadline +. (float (abs step))
5131 else
5132 let y = state.y + step in
5133 let fy = if conf.maxhfit then state.winh else 0 in
5134 let y =
5135 if y < 0
5136 then state.maxy - fy
5137 else if y >= state.maxy - fy then 0 else y
5139 gotoxy state.x y;
5140 deadline +. 0.01
5141 | _ -> infinity
5143 loop newdeadline
5145 | l ->
5146 let rec checkfds = function
5147 | [] -> ()
5148 | fd :: rest when fd = state.ss ->
5149 let cmd = rcmd state.ss in
5150 act cmd;
5151 checkfds rest
5153 | fd :: rest when fd = state.wsfd ->
5154 Wsi.readresp fd;
5155 checkfds rest
5157 | fd :: rest when Some fd = !optrfd ->
5158 begin match remote fd with
5159 | None -> optrfd := remoteopen !rcmdpath;
5160 | opt -> optrfd := opt
5161 end;
5162 checkfds rest
5164 | _ :: rest ->
5165 dolog "select returned unknown descriptor";
5166 checkfds rest
5168 checkfds l;
5169 let newdeadline =
5170 let deadline1 =
5171 if deadline = infinity
5172 then now () +. 0.01
5173 else deadline
5175 match state.autoscroll with
5176 | Some step when step != 0 -> deadline1
5177 | _ -> infinity
5179 loop newdeadline
5180 end;
5182 match loop infinity with
5183 | exception Quit ->
5184 Config.save leavebirdseye;
5185 if hasunsavedchanges ()
5186 then save ()
5187 | _ -> error "umpossible - infinity reached"