Use preprocessor to save the lines
[llpp.git] / main.ml
blob7e2d4c963b3a218fa49622eaa340ceb4a830175f
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 addnav () = getanchor () |> cbput state.hists.nav;;
703 let addnavnorc () = getanchor () |> cbput_dont_update_rc state.hists.nav;;
705 let getnav dir =
706 let anchor = cbgetc state.hists.nav dir in
707 getanchory anchor;
710 let gotopage n top =
711 let y, h = getpageyh n in
712 let y = y + (truncate (top *. float h)) in
713 gotoxy state.x y
716 let gotopage1 n top =
717 let y = getpagey n in
718 let y = y + top in
719 gotoxy state.x y
722 let invalidate s f =
723 Glutils.redisplay := false;
724 state.layout <- [];
725 state.pdims <- [];
726 state.rects <- [];
727 state.rects1 <- [];
728 match state.geomcmds with
729 | ps, [] when emptystr ps ->
730 f ();
731 state.geomcmds <- s, [];
732 | ps, [] -> state.geomcmds <- ps, [s, f];
733 | ps, (s', _) :: rest when s' = s -> state.geomcmds <- ps, ((s, f) :: rest);
734 | ps, cmds -> state.geomcmds <- ps, ((s, f) :: cmds);
737 let flushpages () =
738 Hashtbl.iter (fun _ opaque -> wcmd "freepage %s" (~> opaque)) state.pagemap;
739 Hashtbl.clear state.pagemap;
742 let flushtiles () =
743 if not (Queue.is_empty state.tilelru)
744 then (
745 Queue.iter (fun (k, p, s) ->
746 wcmd "freetile %s" (~> p);
747 state.memused <- state.memused - s;
748 Hashtbl.remove state.tilemap k;
749 ) state.tilelru;
750 state.uioh#infochanged Memused;
751 Queue.clear state.tilelru;
753 load state.layout;
756 let stateh h =
757 let h = truncate (float h*.conf.zoom) in
758 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
759 h - d
762 let fillhelp () =
763 state.help <-
764 let sl = keystostrlist conf in
765 let rec loop accu =
766 function | [] -> accu
767 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
768 in Help.makehelp conf.urilauncher
769 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
772 let opendoc path password =
773 state.path <- path;
774 state.password <- password;
775 state.gen <- state.gen + 1;
776 state.docinfo <- [];
777 state.outlines <- [||];
779 flushpages ();
780 setaalevel conf.aalevel;
781 setpapercolor conf.papercolor;
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 Buffer.add_char state.errmsgs '\n';
1158 state.newerrmsgs <- true;
1159 postRedisplay "error message"
1161 | "progress", args ->
1162 let progress, text =
1163 scan args "%f %n"
1164 (fun f pos -> f, String.sub args pos (String.length args - pos))
1166 state.text <- text;
1167 state.progress <- progress;
1168 postRedisplay "progress"
1170 | "firstmatch", args ->
1171 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1172 scan args "%u %d %f %f %f %f %f %f %f %f"
1173 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1174 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1176 let y = (getpagey pageno) + truncate y0 in
1177 let x =
1178 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1179 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1180 else state.x
1182 addnav ();
1183 gotoxy x y;
1184 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1185 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1187 | "match", args ->
1188 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1189 scan args "%u %d %f %f %f %f %f %f %f %f"
1190 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1191 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1193 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1194 state.rects1 <-
1195 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1197 | "page", args ->
1198 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1199 let pageopaque = ~< pageopaques in
1200 begin match state.currently with
1201 | Loading (l, gen) ->
1202 vlog "page %d took %f sec" l.pageno t;
1203 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1204 let preloadedpages =
1205 if conf.preload
1206 then preloadlayout state.x state.y state.winw state.winh
1207 else state.layout
1209 let evict () =
1210 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1211 IntSet.empty preloadedpages
1213 let evictedpages =
1214 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1215 if not (IntSet.mem pageno set)
1216 then (
1217 wcmd "freepage %s" (~> opaque);
1218 key :: accu
1220 else accu
1221 ) state.pagemap []
1223 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1225 evict ();
1226 state.currently <- Idle;
1227 if gen = state.gen
1228 then (
1229 tilepage l.pageno pageopaque state.layout;
1230 load state.layout;
1231 load preloadedpages;
1232 let visible = pagevisible state.layout l.pageno in
1233 if visible
1234 then (
1235 match state.mode with
1236 | LinkNav (Ltnotready (pageno, dir)) ->
1237 if pageno = l.pageno
1238 then (
1239 let link =
1240 let ld =
1241 if dir = 0
1242 then LDfirstvisible (l.pagex, l.pagey, dir)
1243 else if dir > 0 then LDfirst else LDlast
1245 findlink pageopaque ld
1247 match link with
1248 | Lnotfound -> ()
1249 | Lfound n ->
1250 showlinktype (getlink pageopaque n);
1251 state.mode <- LinkNav (Ltexact (l.pageno, n))
1253 | LinkNav (Ltgendir _)
1254 | LinkNav (Ltexact _)
1255 | View
1256 | Birdseye _
1257 | Textentry _ -> ()
1260 if visible && layoutready state.layout
1261 then postRedisplay "page";
1264 | Idle | Tiling _ | Outlining _ ->
1265 dolog "Inconsistent loading state";
1266 logcurrently state.currently;
1267 exit 1
1270 | "tile" , args ->
1271 let (x, y, opaques, size, t) =
1272 scan args "%u %u %s %u %f" (fun x y p size t -> (x, y, p, size, t))
1274 let opaque = ~< opaques in
1275 begin match state.currently with
1276 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1277 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1279 unmappbo opaque;
1280 if tilew != conf.tilew || tileh != conf.tileh
1281 then (
1282 wcmd "freetile %s" (~> opaque);
1283 state.currently <- Idle;
1284 load state.layout;
1286 else (
1287 puttileopaque l col row gen cs angle opaque size t;
1288 state.memused <- state.memused + size;
1289 state.uioh#infochanged Memused;
1290 gctiles ();
1291 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1292 opaque, size) state.tilelru;
1294 state.currently <- Idle;
1295 if gen = state.gen
1296 && conf.colorspace = cs
1297 && conf.angle = angle
1298 && tilevisible state.layout l.pageno x y
1299 then conttiling l.pageno pageopaque;
1301 preload state.layout;
1302 if gen = state.gen
1303 && conf.colorspace = cs
1304 && conf.angle = angle
1305 && tilevisible state.layout l.pageno x y
1306 && layoutready state.layout
1307 then postRedisplay "tile nothrottle";
1310 | Idle | Loading _ | Outlining _ ->
1311 dolog "Inconsistent tiling state";
1312 logcurrently state.currently;
1313 exit 1
1316 | "pdim", args ->
1317 let (n, w, h, _) as pdim =
1318 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1320 let pdim =
1321 match conf.fitmodel with
1322 | FitWidth -> pdim
1323 | FitPage | FitProportional ->
1324 match conf.columns with
1325 | Csplit _ -> (n, w, h, 0)
1326 | Csingle _ | Cmulti _ -> pdim
1328 state.pdims <- pdim :: state.pdims;
1329 state.uioh#infochanged Pdim
1331 | "o", args ->
1332 let (l, n, t, h, pos) =
1333 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1335 let s = String.sub args pos (String.length args - pos) in
1336 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1338 | "ou", args ->
1339 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1340 let s = String.sub args pos len in
1341 let pos2 = pos + len + 1 in
1342 let uri = String.sub args pos2 (String.length args - pos2) in
1343 addoutline (s, l, Ouri uri)
1345 | "on", args ->
1346 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1347 let s = String.sub args pos (String.length args - pos) in
1348 addoutline (s, l, Onone)
1350 | "a", args ->
1351 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1352 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1354 | "info", args ->
1355 let c, v = splitatchar args '\t' in
1356 let s =
1357 if nonemptystr v
1358 then
1359 if c = "Title"
1360 then (
1361 conf.title <- v;
1362 if not !ignoredoctitlte then Wsi.settitle v;
1363 args
1365 else
1366 if let len = String.length c in
1367 len > 6 && ((String.sub c (len-4) 4) = "date")
1368 then (
1369 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1370 then
1371 let b = Buffer.create 10 in
1372 Printf.bprintf b "%s\t" c;
1373 let sub p l c =
1375 Buffer.add_substring b v p l;
1376 Buffer.add_char b c;
1377 with exn -> Buffer.add_string b @@ exntos exn
1379 sub 2 4 '/';
1380 sub 6 2 '/';
1381 sub 8 2 ' ';
1382 sub 10 2 ':';
1383 sub 12 2 ':';
1384 sub 14 2 ' ';
1385 Printf.bprintf b "[%s]" v;
1386 Buffer.contents b
1387 else args
1389 else args
1390 else args
1392 state.docinfo <- (1, s) :: state.docinfo
1394 | "infoend", "" ->
1395 state.docinfo <- List.rev state.docinfo;
1396 state.uioh#infochanged Docinfo
1398 | "pass", args ->
1399 if args = "fail"
1400 then Wsi.settitle "Wrong password";
1401 let password = getpassword () in
1402 if emptystr password
1403 then error "document is password protected"
1404 else opendoc state.path password
1406 | _ -> error "unknown cmd `%S'" cmds
1409 let onhist cb =
1410 let rc = cb.rc in
1411 let action = function
1412 | HCprev -> cbget cb ~-1
1413 | HCnext -> cbget cb 1
1414 | HCfirst -> cbget cb ~-(cb.rc)
1415 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1416 and cancel () = cb.rc <- rc
1417 in (action, cancel)
1420 let search pattern forward =
1421 match conf.columns with
1422 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1423 | Csingle _ | Cmulti _ ->
1424 if nonemptystr pattern
1425 then
1426 let pn, py =
1427 match state.layout with
1428 | [] -> 0, 0
1429 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1431 wcmd "search %d %d %d %d,%s\000"
1432 (btod conf.icase) pn py (btod forward) pattern;
1435 let intentry text key =
1436 let text =
1437 if emptystr text && key = Keys.Ascii '-'
1438 then addchar text '-'
1439 else
1440 match [@warning "-4"] key with
1441 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1442 | _ ->
1443 state.text <- "invalid key";
1444 text
1446 TEcont text
1449 let linknact f s =
1450 if nonemptystr s
1451 then
1452 let n =
1453 let l = String.length s in
1454 let rec loop pos n =
1455 if pos = l
1456 then n
1457 else
1458 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1459 loop (pos+1) (n*26 + m)
1460 in loop 0 0
1462 let rec loop n = function
1463 | [] -> ()
1464 | l :: rest ->
1465 match getopaque l.pageno with
1466 | None -> loop n rest
1467 | Some opaque ->
1468 let m = getlinkcount opaque in
1469 if n < m
1470 then
1471 let under = getlink opaque n in
1472 f under
1473 else loop (n-m) rest
1475 loop n state.layout;
1478 let linknentry text key = match [@warning "-4"] key with
1479 | Keys.Ascii ('a' .. 'z' as c) ->
1480 let text = addchar text c in
1481 linknact (fun under -> state.text <- undertext under) text;
1482 TEcont text
1483 | _ ->
1484 state.text <- Printf.sprintf "invalid key %s" @@ Keys.to_string key;
1485 TEcont text
1488 let textentry text key = match [@warning "-4"] key with
1489 | Keys.Ascii c -> TEcont (addchar text c)
1490 | Keys.Code c -> TEcont (text ^ toutf8 c)
1491 | _ -> TEcont text
1494 let reqlayout angle fitmodel =
1495 if nogeomcmds state.geomcmds
1496 then state.anchor <- getanchor ();
1497 conf.angle <- angle mod 360;
1498 if conf.angle != 0
1499 then (
1500 match state.mode with
1501 | LinkNav _ -> state.mode <- View
1502 | Birdseye _ | Textentry _ | View -> ()
1504 conf.fitmodel <- fitmodel;
1505 invalidate "reqlayout"
1506 (fun () -> wcmd "reqlayout %d %d %d"
1507 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh));
1510 let settrim trimmargins trimfuzz =
1511 if nogeomcmds state.geomcmds
1512 then state.anchor <- getanchor ();
1513 conf.trimmargins <- trimmargins;
1514 conf.trimfuzz <- trimfuzz;
1515 let x0, y0, x1, y1 = trimfuzz in
1516 invalidate "settrim"
1517 (fun () -> wcmd "settrim %d %d %d %d %d"
1518 (btod conf.trimmargins) x0 y0 x1 y1);
1519 flushpages ();
1522 let setzoom zoom =
1523 let zoom = max 0.0001 zoom in
1524 if zoom <> conf.zoom
1525 then (
1526 state.prevzoom <- (conf.zoom, state.x);
1527 conf.zoom <- zoom;
1528 reshape state.winw state.winh;
1529 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1533 let pivotzoom ?(vw=min state.w state.winw)
1534 ?(vh=min (state.maxy-state.y) state.winh)
1535 ?(x=vw/2) ?(y=vh/2) zoom =
1536 let w = float state.w /. zoom in
1537 let hw = w /. 2.0 in
1538 let ratio = float vh /. float vw in
1539 let hh = hw *. ratio in
1540 let x0 = float x -. hw
1541 and y0 = float y -. hh in
1542 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1543 setzoom zoom;
1546 let pivotzoom ?vw ?vh ?x ?y zoom =
1547 if nogeomcmds state.geomcmds
1548 then
1549 if zoom > 1.0
1550 then pivotzoom ?vw ?vh ?x ?y zoom
1551 else setzoom zoom
1554 let setcolumns mode columns coverA coverB =
1555 state.prevcolumns <- Some (conf.columns, conf.zoom);
1556 if columns < 0
1557 then (
1558 if isbirdseye mode
1559 then impmsg "split mode doesn't work in bird's eye"
1560 else (
1561 conf.columns <- Csplit (-columns, E.a);
1562 state.x <- 0;
1563 conf.zoom <- 1.0;
1566 else (
1567 if columns < 2
1568 then (
1569 conf.columns <- Csingle E.a;
1570 state.x <- 0;
1571 setzoom 1.0;
1573 else (
1574 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1575 conf.zoom <- 1.0;
1578 reshape state.winw state.winh;
1581 let resetmstate () =
1582 state.mstate <- Mnone;
1583 Wsi.setcursor Wsi.CURSOR_INHERIT;
1586 let enterbirdseye () =
1587 let zoom = float conf.thumbw /. float state.winw in
1588 let birdseyepageno =
1589 let cy = state.winh / 2 in
1590 let fold = function
1591 | [] -> 0
1592 | l :: rest ->
1593 let rec fold best = function
1594 | [] -> best.pageno
1595 | l :: rest ->
1596 let d = cy - (l.pagedispy + l.pagevh/2)
1597 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1598 if abs d < abs dbest
1599 then fold l rest
1600 else best.pageno
1601 in fold l rest
1603 fold state.layout
1605 state.mode <-
1606 Birdseye (
1607 { conf with zoom = conf.zoom },
1608 state.x, birdseyepageno, -1, getanchor ()
1610 resetmstate ();
1611 conf.zoom <- zoom;
1612 conf.presentation <- false;
1613 conf.interpagespace <- 10;
1614 conf.hlinks <- false;
1615 conf.fitmodel <- FitPage;
1616 state.x <- 0;
1617 conf.columns <- (
1618 match conf.beyecolumns with
1619 | Some c ->
1620 conf.zoom <- 1.0;
1621 Cmulti ((c, 0, 0), E.a)
1622 | None -> Csingle E.a
1624 if conf.verbose
1625 then state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1626 (100.0*.zoom)
1627 else state.text <- E.s;
1628 reshape state.winw state.winh;
1631 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1632 state.mode <- View;
1633 conf.zoom <- c.zoom;
1634 conf.presentation <- c.presentation;
1635 conf.interpagespace <- c.interpagespace;
1636 conf.hlinks <- c.hlinks;
1637 conf.fitmodel <- c.fitmodel;
1638 conf.beyecolumns <- (
1639 match conf.columns with
1640 | Cmulti ((c, _, _), _) -> Some c
1641 | Csingle _ -> None
1642 | Csplit _ -> error "leaving bird's eye split mode"
1644 conf.columns <- (
1645 match c.columns with
1646 | Cmulti (c, _) -> Cmulti (c, E.a)
1647 | Csingle _ -> Csingle E.a
1648 | Csplit (c, _) -> Csplit (c, E.a)
1650 if conf.verbose
1651 then state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1652 (100.0*.conf.zoom);
1653 reshape state.winw state.winh;
1654 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1655 state.x <- leftx;
1658 let togglebirdseye () =
1659 match state.mode with
1660 | Birdseye vals -> leavebirdseye vals true
1661 | View -> enterbirdseye ()
1662 | Textentry _ | LinkNav _ -> ()
1665 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1666 let pageno = max 0 (pageno - incr) in
1667 let rec loop = function
1668 | [] -> gotopage1 pageno 0
1669 | l :: _ when l.pageno = pageno ->
1670 if l.pagedispy >= 0 && l.pagey = 0
1671 then postRedisplay "upbirdseye"
1672 else gotopage1 pageno 0
1673 | _ :: rest -> loop rest
1675 loop state.layout;
1676 state.text <- E.s;
1677 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1680 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1681 let pageno = min (state.pagecount - 1) (pageno + incr) in
1682 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1683 let rec loop = function
1684 | [] ->
1685 let y, h = getpageyh pageno in
1686 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1687 gotoxy state.x (clamp dy)
1688 | l :: _ when l.pageno = pageno ->
1689 if l.pagevh != l.pageh
1690 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1691 else postRedisplay "downbirdseye"
1692 | _ :: rest -> loop rest
1694 loop state.layout;
1695 state.text <- E.s;
1698 let optentry mode _ key =
1699 let btos b = if b then "on" else "off" in
1700 match [@warning "-4"] key with
1701 | Keys.Ascii 'C' ->
1702 let ondone s =
1704 let n, a, b = multicolumns_of_string s in
1705 setcolumns mode n a b;
1706 with exn ->
1707 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1709 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1711 | Keys.Ascii 'Z' ->
1712 let ondone s =
1714 let zoom = float (int_of_string s) /. 100.0 in
1715 pivotzoom zoom
1716 with exn ->
1717 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1719 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1721 | Keys.Ascii 'i' ->
1722 conf.icase <- not conf.icase;
1723 TEdone ("case insensitive search " ^ (btos conf.icase))
1725 | Keys.Ascii 'v' ->
1726 conf.verbose <- not conf.verbose;
1727 TEdone ("verbose " ^ (btos conf.verbose))
1729 | Keys.Ascii 'd' ->
1730 conf.debug <- not conf.debug;
1731 TEdone ("debug " ^ (btos conf.debug))
1733 | Keys.Ascii 'f' ->
1734 conf.underinfo <- not conf.underinfo;
1735 TEdone ("underinfo " ^ btos conf.underinfo)
1737 | Keys.Ascii 'T' ->
1738 settrim (not conf.trimmargins) conf.trimfuzz;
1739 TEdone ("trim margins " ^ btos conf.trimmargins)
1741 | Keys.Ascii 'I' ->
1742 conf.invert <- not conf.invert;
1743 TEdone ("invert colors " ^ btos conf.invert)
1745 | Keys.Ascii 'x' ->
1746 let ondone s =
1747 cbput state.hists.sel s;
1748 conf.selcmd <- s;
1750 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1751 textentry, ondone, true)
1753 | Keys.Ascii 'M' ->
1754 if conf.pax == None
1755 then conf.pax <- Some 0.0
1756 else conf.pax <- None;
1757 TEdone ("PAX " ^ btos (conf.pax != None))
1759 | (Keys.Ascii c) ->
1760 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1761 TEstop
1763 | _ -> TEcont state.text
1766 let adderrmsg src msg =
1767 Buffer.add_string state.errmsgs msg;
1768 state.newerrmsgs <- true;
1769 postRedisplay src
1772 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1774 class outlinelistview ~zebra ~source =
1775 let settext autonarrow s =
1776 if autonarrow
1777 then
1778 let ss = source#statestr in
1779 state.text <- if emptystr ss
1780 then "[" ^ s ^ "]"
1781 else "{" ^ ss ^ "} [" ^ s ^ "]"
1782 else state.text <- s
1784 object (self)
1785 inherit listview
1786 ~zebra
1787 ~helpmode:false
1788 ~source:(source :> lvsource)
1789 ~trusted:false
1790 ~modehash:(findkeyhash conf "outline")
1791 as super
1793 val m_autonarrow = false
1795 method! key key mask =
1796 let maxrows =
1797 if emptystr state.text
1798 then fstate.maxrows
1799 else fstate.maxrows - 2
1801 let calcfirst first active =
1802 if active > first
1803 then
1804 let rows = active - first in
1805 if rows > maxrows then active - maxrows else first
1806 else active
1808 let navigate incr =
1809 let active = m_active + incr in
1810 let active = bound active 0 (source#getitemcount - 1) in
1811 let first = calcfirst m_first active in
1812 postRedisplay "outline navigate";
1813 coe {< m_active = active; m_first = first >}
1815 let navscroll first =
1816 let active =
1817 let dist = m_active - first in
1818 if dist < 0
1819 then first
1820 else (
1821 if dist < maxrows
1822 then m_active
1823 else first + maxrows
1826 postRedisplay "outline navscroll";
1827 coe {< m_first = first; m_active = active >}
1829 let ctrl = Wsi.withctrl mask in
1830 let open Keys in
1831 match Wsi.kc2kt key with
1832 | Ascii 'a' when ctrl ->
1833 let text =
1834 if m_autonarrow
1835 then (
1836 source#denarrow;
1839 else (
1840 let pattern = source#renarrow in
1841 if nonemptystr m_qsearch
1842 then (source#narrow m_qsearch; m_qsearch)
1843 else pattern
1846 settext (not m_autonarrow) text;
1847 postRedisplay "toggle auto narrowing";
1848 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1850 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1851 settext true E.s;
1852 postRedisplay "toggle auto narrowing";
1853 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1855 | Ascii 'n' when ctrl ->
1856 source#narrow m_qsearch;
1857 if not m_autonarrow
1858 then source#add_narrow_pattern m_qsearch;
1859 postRedisplay "outline ctrl-n";
1860 coe {< m_first = 0; m_active = 0 >}
1862 | Ascii 'S' when ctrl ->
1863 let active = source#calcactive (getanchor ()) in
1864 let first = firstof m_first active in
1865 postRedisplay "outline ctrl-s";
1866 coe {< m_first = first; m_active = active >}
1868 | Ascii 'u' when ctrl ->
1869 postRedisplay "outline ctrl-u";
1870 if m_autonarrow && nonemptystr m_qsearch
1871 then (
1872 ignore (source#renarrow);
1873 settext m_autonarrow E.s;
1874 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1876 else (
1877 source#del_narrow_pattern;
1878 let pattern = source#renarrow in
1879 let text =
1880 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1882 settext m_autonarrow text;
1883 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1886 | Ascii 'l' when ctrl ->
1887 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1888 postRedisplay "outline ctrl-l";
1889 coe {< m_first = first >}
1891 | Ascii '\t' when m_autonarrow ->
1892 if nonemptystr m_qsearch
1893 then (
1894 postRedisplay "outline list view tab";
1895 source#add_narrow_pattern m_qsearch;
1896 settext true E.s;
1897 coe {< m_qsearch = E.s >}
1899 else coe self
1901 | Escape when m_autonarrow ->
1902 if nonemptystr m_qsearch
1903 then source#add_narrow_pattern m_qsearch;
1904 super#key key mask
1906 | Enter when m_autonarrow ->
1907 if nonemptystr m_qsearch
1908 then source#add_narrow_pattern m_qsearch;
1909 super#key key mask
1911 | (Ascii _ | Code _) when m_autonarrow ->
1912 let pattern = m_qsearch ^ toutf8 key in
1913 postRedisplay "outlinelistview autonarrow add";
1914 source#narrow pattern;
1915 settext true pattern;
1916 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1918 | Backspace when m_autonarrow ->
1919 if emptystr m_qsearch
1920 then coe self
1921 else
1922 let pattern = withoutlastutf8 m_qsearch in
1923 postRedisplay "outlinelistview autonarrow backspace";
1924 ignore (source#renarrow);
1925 source#narrow pattern;
1926 settext true pattern;
1927 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1929 | Up when ctrl -> navscroll (max 0 (m_first - 1))
1931 | Down when ctrl ->
1932 navscroll (min (source#getitemcount - 1) (m_first + 1))
1934 | Up -> navigate ~-1
1935 | Down -> navigate 1
1936 | Prior -> navigate ~-(fstate.maxrows)
1937 | Next -> navigate fstate.maxrows
1939 | Right ->
1940 let o =
1941 if ctrl
1942 then (
1943 postRedisplay "outline ctrl right";
1944 {< m_pan = m_pan + 1 >}
1946 else (
1947 if Wsi.withshift mask
1948 then self#nextcurlevel 1
1949 else self#updownlevel 1
1952 coe o
1954 | Left ->
1955 let o =
1956 if ctrl
1957 then (
1958 postRedisplay "outline ctrl left";
1959 {< m_pan = m_pan - 1 >}
1961 else (
1962 if Wsi.withshift mask
1963 then self#nextcurlevel ~-1
1964 else self#updownlevel ~-1
1967 coe o
1969 | Home ->
1970 postRedisplay "outline home";
1971 coe {< m_first = 0; m_active = 0 >}
1973 | End ->
1974 let active = source#getitemcount - 1 in
1975 let first = max 0 (active - fstate.maxrows) in
1976 postRedisplay "outline end";
1977 coe {< m_active = active; m_first = first >}
1979 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
1980 super#key key mask
1981 end;;
1983 let genhistoutlines () =
1984 Config.gethist ()
1985 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
1986 compare c2.lastvisit c1.lastvisit)
1987 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
1988 let path = if nonemptystr origin then origin else path in
1989 let base = mbtoutf8 @@ Filename.basename path in
1990 (base ^ "\000" ^ c.title, 1, Ohistory hist)
1994 let gotohist (path, c, bookmarks, x, anchor, origin) =
1995 Config.save leavebirdseye;
1996 state.anchor <- anchor;
1997 state.bookmarks <- bookmarks;
1998 state.origin <- origin;
1999 state.x <- x;
2000 setconf conf c;
2001 let x0, y0, x1, y1 = conf.trimfuzz in
2002 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2003 reshape ~firsttime:true state.winw state.winh;
2004 opendoc path origin;
2005 setzoom c.zoom;
2008 let setcheckers enabled =
2009 match !checkerstexid with
2010 | None -> if enabled then checkerstexid := Some (makecheckers ())
2011 | Some id ->
2012 if not enabled
2013 then (
2014 GlTex.delete_texture id;
2015 checkerstexid := None;
2019 let describe_layout layout =
2020 let d =
2021 match layout with
2022 | [] -> "Page 0"
2023 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2024 | l :: rest ->
2025 let rangestr a b =
2026 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2027 else Printf.sprintf "%d%s%d" (a.pageno+1)
2028 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2029 (b.pageno+1)
2031 let rec fold s la lb = function
2032 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2033 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2034 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2036 fold "Pages" l l rest
2038 let percent =
2039 let maxy = maxy () in
2040 if maxy <= 0
2041 then 100.
2042 else 100. *. (float state.y /. float maxy)
2044 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2047 let setpresentationmode v =
2048 let n = page_of_y state.y in
2049 state.anchor <- (n, 0.0, 1.0);
2050 conf.presentation <- v;
2051 if conf.fitmodel = FitPage
2052 then reqlayout conf.angle conf.fitmodel;
2053 represent ();
2056 let enterinfomode =
2057 let btos b = if b then Utf8syms.radical else E.s in
2058 let showextended = ref false in
2059 let showcolors = ref false in
2060 let leave mode _ = state.mode <- mode in
2061 let src =
2062 (object
2063 val mutable m_l = []
2064 val mutable m_a = E.a
2065 val mutable m_prev_uioh = nouioh
2066 val mutable m_prev_mode = View
2068 inherit lvsourcebase
2070 method reset prev_mode prev_uioh =
2071 m_a <- Array.of_list (List.rev m_l);
2072 m_l <- [];
2073 m_prev_mode <- prev_mode;
2074 m_prev_uioh <- prev_uioh;
2076 method int name get set =
2077 m_l <-
2078 (name, `int get, 1,
2079 Action (
2080 fun u ->
2081 let ondone s =
2082 try set (int_of_string s)
2083 with exn ->
2084 state.text <- Printf.sprintf "bad integer `%s': %s"
2085 s @@ exntos exn
2087 state.text <- E.s;
2088 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2089 state.mode <- Textentry (te, leave m_prev_mode);
2091 )) :: m_l
2093 method int_with_suffix name get set =
2094 m_l <-
2095 (name, `intws get, 1,
2096 Action (
2097 fun u ->
2098 let ondone s =
2099 try set (int_of_string_with_suffix s)
2100 with exn ->
2101 state.text <- Printf.sprintf "bad integer `%s': %s"
2102 s @@ exntos exn
2104 state.text <- E.s;
2105 let te =
2106 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2108 state.mode <- Textentry (te, leave m_prev_mode);
2110 )) :: m_l
2112 method bool ?(offset=1) ?(btos=btos) name get set =
2113 m_l <-
2114 (name, `bool (btos, get), offset, Action (
2115 fun u ->
2116 let v = get () in
2117 set (not v);
2119 )) :: m_l
2121 method color name get set =
2122 m_l <-
2123 (name, `color get, 1,
2124 Action (
2125 fun u ->
2126 let invalid = (nan, nan, nan) in
2127 let ondone s =
2128 let c =
2129 try color_of_string s
2130 with exn ->
2131 state.text <- Printf.sprintf "bad color `%s': %s"
2132 s @@ exntos exn;
2133 invalid
2135 if c <> invalid
2136 then set c;
2138 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2139 state.text <- color_to_string (get ());
2140 state.mode <- Textentry (te, leave m_prev_mode);
2142 )) :: m_l
2144 method string name get set =
2145 m_l <-
2146 (name, `string get, 1,
2147 Action (
2148 fun u ->
2149 let ondone s = set s in
2150 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2151 state.mode <- Textentry (te, leave m_prev_mode);
2153 )) :: m_l
2155 method colorspace name get set =
2156 m_l <-
2157 (name, `string get, 1,
2158 Action (
2159 fun _ ->
2160 let source =
2161 (object
2162 inherit lvsourcebase
2164 initializer
2165 m_active <- CSTE.to_int conf.colorspace;
2166 m_first <- 0;
2168 method getitemcount =
2169 Array.length CSTE.names
2170 method getitem n =
2171 (CSTE.names.(n), 0)
2172 method exit ~uioh ~cancel ~active ~first ~pan =
2173 ignore (uioh, first, pan);
2174 if not cancel then set active;
2175 None
2176 method hasaction _ = true
2177 end)
2179 state.text <- E.s;
2180 let modehash = findkeyhash conf "info" in
2181 coe (new listview ~zebra:false ~helpmode:false
2182 ~source ~trusted:true ~modehash)
2183 )) :: m_l
2185 method paxmark name get set =
2186 m_l <-
2187 (name, `string get, 1,
2188 Action (
2189 fun _ ->
2190 let source =
2191 (object
2192 inherit lvsourcebase
2194 initializer
2195 m_active <- MTE.to_int conf.paxmark;
2196 m_first <- 0;
2198 method getitemcount = Array.length MTE.names
2199 method getitem n = (MTE.names.(n), 0)
2200 method exit ~uioh ~cancel ~active ~first ~pan =
2201 ignore (uioh, first, pan);
2202 if not cancel then set active;
2203 None
2204 method hasaction _ = true
2205 end)
2207 state.text <- E.s;
2208 let modehash = findkeyhash conf "info" in
2209 coe (new listview ~zebra:false ~helpmode:false
2210 ~source ~trusted:true ~modehash)
2211 )) :: m_l
2213 method fitmodel name get set =
2214 m_l <-
2215 (name, `string get, 1,
2216 Action (
2217 fun _ ->
2218 let source =
2219 (object
2220 inherit lvsourcebase
2222 initializer
2223 m_active <- FMTE.to_int conf.fitmodel;
2224 m_first <- 0;
2226 method getitemcount = Array.length FMTE.names
2227 method getitem n = (FMTE.names.(n), 0)
2228 method exit ~uioh ~cancel ~active ~first ~pan =
2229 ignore (uioh, first, pan);
2230 if not cancel then set active;
2231 None
2232 method hasaction _ = true
2233 end)
2235 state.text <- E.s;
2236 let modehash = findkeyhash conf "info" in
2237 coe (new listview ~zebra:false ~helpmode:false
2238 ~source ~trusted:true ~modehash)
2239 )) :: m_l
2241 method caption s offset =
2242 m_l <- (s, `empty, offset, Noaction) :: m_l
2244 method caption2 s f offset =
2245 m_l <- (s, `string f, offset, Noaction) :: m_l
2247 method getitemcount = Array.length m_a
2249 method getitem n =
2250 let tostr = function
2251 | `int f -> string_of_int (f ())
2252 | `intws f -> string_with_suffix_of_int (f ())
2253 | `string f -> f ()
2254 | `color f -> color_to_string (f ())
2255 | `bool (btos, f) -> btos (f ())
2256 | `empty -> E.s
2258 let name, t, offset, _ = m_a.(n) in
2259 ((let s = tostr t in
2260 if nonemptystr s
2261 then Printf.sprintf "%s\t%s" name s
2262 else name),
2263 offset)
2265 method exit ~uioh ~cancel ~active ~first ~pan =
2266 let uiohopt =
2267 if not cancel
2268 then (
2269 let uioh =
2270 match m_a.(active) with
2271 | _, _, _, Action f -> f uioh
2272 | _, _, _, Noaction -> uioh
2274 Some uioh
2276 else None
2278 m_active <- active;
2279 m_first <- first;
2280 m_pan <- pan;
2281 uiohopt
2283 method hasaction n =
2284 match m_a.(n) with
2285 | _, _, _, Action _ -> true
2286 | _, _, _, Noaction -> false
2288 initializer m_active <- 1
2289 end)
2291 let rec fillsrc prevmode prevuioh =
2292 let sep () = src#caption E.s 0 in
2293 let colorp name get set =
2294 src#string name
2295 (fun () -> color_to_string (get ()))
2296 (fun v ->
2297 try set @@ color_of_string v
2298 with exn ->
2299 state.text <-
2300 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2303 let rgba name get set =
2304 src#string name
2305 (fun () -> get () |> rgba_to_string)
2306 (fun v ->
2307 try set @@ rgba_of_string v
2308 with exn ->
2309 state.text <-
2310 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2313 let oldmode = state.mode in
2314 let birdseye = isbirdseye state.mode in
2316 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2318 src#bool "presentation mode"
2319 (fun () -> conf.presentation)
2320 (fun v -> setpresentationmode v);
2322 src#bool "ignore case in searches"
2323 (fun () -> conf.icase)
2324 (fun v -> conf.icase <- v);
2326 src#bool "preload"
2327 (fun () -> conf.preload)
2328 (fun v -> conf.preload <- v);
2330 src#bool "highlight links"
2331 (fun () -> conf.hlinks)
2332 (fun v -> conf.hlinks <- v);
2334 src#bool "under info"
2335 (fun () -> conf.underinfo)
2336 (fun v -> conf.underinfo <- v);
2338 src#fitmodel "fit model"
2339 (fun () -> FMTE.to_string conf.fitmodel)
2340 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2342 src#bool "trim margins"
2343 (fun () -> conf.trimmargins)
2344 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2346 sep ();
2347 src#int "inter-page space"
2348 (fun () -> conf.interpagespace)
2349 (fun n ->
2350 conf.interpagespace <- n;
2351 docolumns conf.columns;
2352 let pageno, py =
2353 match state.layout with
2354 | [] -> 0, 0
2355 | l :: _ -> l.pageno, l.pagey
2357 state.maxy <- calcheight ();
2358 let y = getpagey pageno in
2359 gotoxy state.x (y + py)
2362 src#int "page bias"
2363 (fun () -> conf.pagebias)
2364 (fun v -> conf.pagebias <- v);
2366 src#int "scroll step"
2367 (fun () -> conf.scrollstep)
2368 (fun n -> conf.scrollstep <- n);
2370 src#int "horizontal scroll step"
2371 (fun () -> conf.hscrollstep)
2372 (fun v -> conf.hscrollstep <- v);
2374 src#int "auto scroll step"
2375 (fun () ->
2376 match state.autoscroll with
2377 | Some step -> step
2378 | _ -> conf.autoscrollstep)
2379 (fun n ->
2380 let n = boundastep state.winh n in
2381 if state.autoscroll <> None
2382 then state.autoscroll <- Some n;
2383 conf.autoscrollstep <- n);
2385 src#int "zoom"
2386 (fun () -> truncate (conf.zoom *. 100.))
2387 (fun v -> pivotzoom ((float v) /. 100.));
2389 src#int "rotation"
2390 (fun () -> conf.angle)
2391 (fun v -> reqlayout v conf.fitmodel);
2393 src#int "scroll bar width"
2394 (fun () -> conf.scrollbw)
2395 (fun v ->
2396 conf.scrollbw <- v;
2397 reshape state.winw state.winh;
2400 src#int "scroll handle height"
2401 (fun () -> conf.scrollh)
2402 (fun v -> conf.scrollh <- v;);
2404 src#int "thumbnail width"
2405 (fun () -> conf.thumbw)
2406 (fun v ->
2407 conf.thumbw <- min 4096 v;
2408 match oldmode with
2409 | Birdseye beye ->
2410 leavebirdseye beye false;
2411 enterbirdseye ()
2412 | Textentry _
2413 | View
2414 | LinkNav _ -> ()
2417 let mode = state.mode in
2418 src#string "columns"
2419 (fun () ->
2420 match conf.columns with
2421 | Csingle _ -> "1"
2422 | Cmulti (multi, _) -> multicolumns_to_string multi
2423 | Csplit (count, _) -> "-" ^ string_of_int count
2425 (fun v ->
2426 let n, a, b = multicolumns_of_string v in
2427 setcolumns mode n a b);
2429 sep ();
2430 src#caption "Pixmap cache" 0;
2431 src#int_with_suffix "size (advisory)"
2432 (fun () -> conf.memlimit)
2433 (fun v -> conf.memlimit <- v);
2435 src#caption2 "used"
2436 (fun () ->
2437 Printf.sprintf "%s bytes, %d tiles"
2438 (string_with_suffix_of_int state.memused)
2439 (Hashtbl.length state.tilemap)) 1;
2441 sep ();
2442 src#caption "Layout" 0;
2443 src#caption2 "Dimension"
2444 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2445 state.winw state.winh
2446 state.w state.maxy)
2448 if conf.debug
2449 then src#caption2 "Position" (fun () ->
2450 Printf.sprintf "%dx%d" state.x state.y
2452 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2454 sep ();
2455 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2456 "Save these parameters as global defaults at exit"
2457 (fun () -> conf.bedefault)
2458 (fun v -> conf.bedefault <- v);
2460 sep ();
2461 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2462 src#bool ~offset:0 ~btos "Extended parameters"
2463 (fun () -> !showextended)
2464 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2465 if !showextended
2466 then (
2467 src#bool "checkers"
2468 (fun () -> conf.checkers)
2469 (fun v -> conf.checkers <- v; setcheckers v);
2470 src#bool "update cursor"
2471 (fun () -> conf.updatecurs)
2472 (fun v -> conf.updatecurs <- v);
2473 src#bool "scroll-bar on the left"
2474 (fun () -> conf.leftscroll)
2475 (fun v -> conf.leftscroll <- v);
2476 src#bool "verbose"
2477 (fun () -> conf.verbose)
2478 (fun v -> conf.verbose <- v);
2479 src#bool "invert colors"
2480 (fun () -> conf.invert)
2481 (fun v -> conf.invert <- v);
2482 src#bool "max fit"
2483 (fun () -> conf.maxhfit)
2484 (fun v -> conf.maxhfit <- v);
2485 src#bool "pax mode"
2486 (fun () -> conf.pax != None)
2487 (fun v ->
2488 if v
2489 then conf.pax <- Some (now ())
2490 else conf.pax <- None);
2491 src#string "uri launcher"
2492 (fun () -> conf.urilauncher)
2493 (fun v -> conf.urilauncher <- v);
2494 src#string "path launcher"
2495 (fun () -> conf.pathlauncher)
2496 (fun v -> conf.pathlauncher <- v);
2497 src#string "tile size"
2498 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2499 (fun v ->
2501 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2502 conf.tilew <- max 64 w;
2503 conf.tileh <- max 64 h;
2504 flushtiles ();
2505 with exn ->
2506 state.text <- Printf.sprintf "bad tile size `%s': %s"
2507 v @@ exntos exn
2509 src#int "texture count"
2510 (fun () -> conf.texcount)
2511 (fun v ->
2512 if realloctexts v
2513 then conf.texcount <- v
2514 else impmsg "failed to set texture count please retry later"
2516 src#int "slice height"
2517 (fun () -> conf.sliceheight)
2518 (fun v ->
2519 conf.sliceheight <- v;
2520 wcmd "sliceh %d" conf.sliceheight;
2522 src#int "anti-aliasing level"
2523 (fun () -> conf.aalevel)
2524 (fun v ->
2525 conf.aalevel <- bound v 0 8;
2526 state.anchor <- getanchor ();
2527 opendoc state.path state.password;
2529 src#string "page scroll scaling factor"
2530 (fun () -> string_of_float conf.pgscale)
2531 (fun v ->
2532 try conf.pgscale <- float_of_string v
2533 with exn ->
2534 state.text <-
2535 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2536 @@ exntos exn
2538 src#int "ui font size"
2539 (fun () -> fstate.fontsize)
2540 (fun v -> setfontsize (bound v 5 100));
2541 src#int "hint font size"
2542 (fun () -> conf.hfsize)
2543 (fun v -> conf.hfsize <- bound v 5 100);
2544 src#string "trim fuzz"
2545 (fun () -> irect_to_string conf.trimfuzz)
2546 (fun v ->
2548 conf.trimfuzz <- irect_of_string v;
2549 if conf.trimmargins
2550 then settrim true conf.trimfuzz;
2551 with exn ->
2552 state.text <- Printf.sprintf "bad irect `%s': %s" v
2553 @@ exntos exn
2555 src#string "selection command"
2556 (fun () -> conf.selcmd)
2557 (fun v -> conf.selcmd <- v);
2558 src#string "synctex command"
2559 (fun () -> conf.stcmd)
2560 (fun v -> conf.stcmd <- v);
2561 src#string "pax command"
2562 (fun () -> conf.paxcmd)
2563 (fun v -> conf.paxcmd <- v);
2564 src#string "ask password command"
2565 (fun () -> conf.passcmd)
2566 (fun v -> conf.passcmd <- v);
2567 src#string "save path command"
2568 (fun () -> conf.savecmd)
2569 (fun v -> conf.savecmd <- v);
2570 src#colorspace "color space"
2571 (fun () -> CSTE.to_string conf.colorspace)
2572 (fun v ->
2573 conf.colorspace <- CSTE.of_int v;
2574 wcmd "cs %d" v;
2575 load state.layout;
2577 src#paxmark "pax mark method"
2578 (fun () -> MTE.to_string conf.paxmark)
2579 (fun v -> conf.paxmark <- MTE.of_int v);
2580 if bousable ()
2581 then
2582 src#bool "use PBO"
2583 (fun () -> conf.usepbo)
2584 (fun v -> conf.usepbo <- v);
2585 src#bool "mouse wheel scrolls pages"
2586 (fun () -> conf.wheelbypage)
2587 (fun v -> conf.wheelbypage <- v);
2588 src#bool "open remote links in a new instance"
2589 (fun () -> conf.riani)
2590 (fun v -> conf.riani <- v);
2591 src#bool "edit annotations inline"
2592 (fun () -> conf.annotinline)
2593 (fun v -> conf.annotinline <- v);
2594 src#bool "coarse positioning in presentation mode"
2595 (fun () -> conf.coarseprespos)
2596 (fun v -> conf.coarseprespos <- v);
2597 src#bool "use document CSS"
2598 (fun () -> conf.usedoccss)
2599 (fun v ->
2600 conf.usedoccss <- v;
2601 state.anchor <- getanchor ();
2602 opendoc state.path state.password;
2604 src#bool ~btos "colors"
2605 (fun () -> !showcolors)
2606 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2607 if !showcolors
2608 then (
2609 colorp " background"
2610 (fun () -> conf.bgcolor)
2611 (fun v -> conf.bgcolor <- v);
2613 rgba " paper color"
2614 (fun () -> conf.papercolor)
2615 (fun v ->
2616 conf.papercolor <- v;
2617 setpapercolor conf.papercolor;
2618 flushtiles ();
2620 rgba " scrollbar"
2621 (fun () -> conf.sbarcolor)
2622 (fun v -> conf.sbarcolor <- v);
2623 rgba " scrollbar handle"
2624 (fun () -> conf.sbarhndlcolor)
2625 (fun v -> conf.sbarhndlcolor <- v);
2626 rgba " texture color"
2627 (fun () -> conf.texturecolor)
2628 (fun v ->
2629 GlTex.env (`color v);
2630 conf.texturecolor <- v;
2635 sep ();
2636 src#caption "Document" 0;
2637 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2638 src#caption2 "Pages" (fun () -> string_of_int state.pagecount) 1;
2639 src#caption2 "Dimensions"
2640 (fun () -> string_of_int (List.length state.pdims)) 1;
2641 if nonemptystr conf.css
2642 then src#caption2 "CSS" (fun () -> conf.css) 1;
2643 if conf.trimmargins
2644 then (
2645 sep ();
2646 src#caption "Trimmed margins" 0;
2647 src#caption2 "Dimensions"
2648 (fun () -> string_of_int (List.length state.pdims)) 1;
2651 sep ();
2652 src#caption "OpenGL" 0;
2653 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2654 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2656 sep ();
2657 src#caption "Location" 0;
2658 if nonemptystr state.origin
2659 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2660 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2662 src#reset prevmode prevuioh;
2664 fun () -> (
2665 state.text <- E.s;
2666 resetmstate ();
2667 let prevmode = state.mode
2668 and prevuioh = state.uioh in
2669 fillsrc prevmode prevuioh;
2670 let source = (src :> lvsource) in
2671 let modehash = findkeyhash conf "info" in
2672 state.uioh <-
2673 coe (object (self)
2674 inherit listview ~zebra:false ~helpmode:false
2675 ~source ~trusted:true ~modehash as super
2676 val mutable m_prevmemused = 0
2677 method! infochanged = function
2678 | Memused ->
2679 if m_prevmemused != state.memused
2680 then (
2681 m_prevmemused <- state.memused;
2682 postRedisplay "memusedchanged";
2684 | Pdim -> postRedisplay "pdimchanged"
2685 | Docinfo -> fillsrc prevmode prevuioh
2687 method! key key mask =
2688 if not (Wsi.withctrl mask)
2689 then
2690 match [@warning "-4"] Wsi.kc2kt key with
2691 | Keys.Left -> coe (self#updownlevel ~-1)
2692 | Keys.Right -> coe (self#updownlevel 1)
2693 | _ -> super#key key mask
2694 else super#key key mask
2695 end);
2696 postRedisplay "info";
2700 let enterhelpmode =
2701 let source =
2702 (object
2703 inherit lvsourcebase
2704 method getitemcount = Array.length state.help
2705 method getitem n =
2706 let s, l, _ = state.help.(n) in
2707 (s, l)
2709 method exit ~uioh ~cancel ~active ~first ~pan =
2710 let optuioh =
2711 if not cancel
2712 then (
2713 match state.help.(active) with
2714 | _, _, Action f -> Some (f uioh)
2715 | _, _, Noaction -> Some uioh
2717 else None
2719 m_active <- active;
2720 m_first <- first;
2721 m_pan <- pan;
2722 optuioh
2724 method hasaction n =
2725 match state.help.(n) with
2726 | _, _, Action _ -> true
2727 | _, _, Noaction -> false
2729 initializer
2730 m_active <- -1
2731 end)
2733 fun () ->
2734 let modehash = findkeyhash conf "help" in
2735 resetmstate ();
2736 state.uioh <- coe (new listview
2737 ~zebra:false ~helpmode:true
2738 ~source ~trusted:true ~modehash);
2739 postRedisplay "help";
2742 let entermsgsmode =
2743 let msgsource =
2744 (object
2745 inherit lvsourcebase
2746 val mutable m_items = E.a
2748 method getitemcount = 1 + Array.length m_items
2750 method getitem n =
2751 if n = 0
2752 then "[Clear]", 0
2753 else m_items.(n-1), 0
2755 method exit ~uioh ~cancel ~active ~first ~pan =
2756 ignore uioh;
2757 if not cancel
2758 then (
2759 if active = 0
2760 then Buffer.clear state.errmsgs;
2762 m_active <- active;
2763 m_first <- first;
2764 m_pan <- pan;
2765 None
2767 method hasaction n =
2768 n = 0
2770 method reset =
2771 state.newerrmsgs <- false;
2772 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2773 m_items <- Array.of_list l
2775 initializer
2776 m_active <- 0
2777 end)
2778 in fun () ->
2779 state.text <- E.s;
2780 resetmstate ();
2781 msgsource#reset;
2782 let source = (msgsource :> lvsource) in
2783 let modehash = findkeyhash conf "listview" in
2784 state.uioh <-
2785 coe (object
2786 inherit listview ~zebra:false ~helpmode:false
2787 ~source ~trusted:false ~modehash as super
2788 method! display =
2789 if state.newerrmsgs
2790 then msgsource#reset;
2791 super#display
2792 end);
2793 postRedisplay "msgs";
2796 let getusertext s =
2797 let editor = getenvdef "EDITOR" E.s in
2798 if emptystr editor
2799 then E.s
2800 else
2801 let tmppath = Filename.temp_file "llpp" "note" in
2802 if nonemptystr s
2803 then (
2804 let oc = open_out tmppath in
2805 output_string oc s;
2806 close_out oc;
2808 let execstr = editor ^ " " ^ tmppath in
2809 let s =
2810 match spawn execstr [] with
2811 | exception exn ->
2812 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2814 | pid ->
2815 match Unix.waitpid [] pid with
2816 | exception exn ->
2817 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2819 | (_pid, status) ->
2820 match status with
2821 | Unix.WEXITED 0 -> filecontents tmppath
2822 | Unix.WEXITED n ->
2823 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2825 | Unix.WSIGNALED n ->
2826 impmsg "editor process(%s) was killed by signal %d" execstr n;
2828 | Unix.WSTOPPED n ->
2829 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2832 match Unix.unlink tmppath with
2833 | exception exn ->
2834 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2836 | () -> s
2839 let enterannotmode opaque slinkindex =
2840 let msgsource =
2841 (object
2842 inherit lvsourcebase
2843 val mutable m_text = E.s
2844 val mutable m_items = E.a
2846 method getitemcount = Array.length m_items
2848 method getitem n =
2849 let label, _func = m_items.(n) in
2850 label, 0
2852 method exit ~uioh ~cancel ~active ~first ~pan =
2853 ignore (uioh, first, pan);
2854 if not cancel
2855 then (
2856 let _label, func = m_items.(active) in
2857 func ()
2859 None
2861 method hasaction n = nonemptystr @@ fst m_items.(n)
2863 method reset s =
2864 let rec split accu b i =
2865 let p = b+i in
2866 if p = String.length s
2867 then (String.sub s b (p-b), fun () -> ()) :: accu
2868 else
2869 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2870 then
2871 let ss = if i = 0 then E.s else String.sub s b i in
2872 split ((ss, fun () -> ())::accu) (p+1) 0
2873 else split accu b (i+1)
2875 let cleanup () =
2876 wcmd "freepage %s" (~> opaque);
2877 let keys =
2878 Hashtbl.fold (fun key opaque' accu ->
2879 if opaque' = opaque'
2880 then key :: accu else accu) state.pagemap []
2882 List.iter (Hashtbl.remove state.pagemap) keys;
2883 flushtiles ();
2884 gotoxy state.x state.y
2886 let dele () =
2887 delannot opaque slinkindex;
2888 cleanup ();
2890 let edit inline () =
2891 let update s =
2892 if emptystr s
2893 then dele ()
2894 else (
2895 modannot opaque slinkindex s;
2896 cleanup ();
2899 if inline
2900 then
2901 let mode = state.mode in
2902 state.mode <-
2903 Textentry (
2904 ("annotation: ", m_text, None, textentry, update, true),
2905 fun _ -> state.mode <- mode
2907 state.text <- E.s;
2908 enttext ();
2909 else
2910 let s = getusertext m_text in
2911 update s
2913 m_text <- s;
2914 m_items <-
2915 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2916 :: ("[Delete]", dele)
2917 :: ("[Edit]", edit conf.annotinline)
2918 :: (E.s, fun () -> ())
2919 :: split [] 0 0 |> List.rev |> Array.of_list
2921 initializer
2922 m_active <- 0
2923 end)
2925 state.text <- E.s;
2926 let s = getannotcontents opaque slinkindex in
2927 resetmstate ();
2928 msgsource#reset s;
2929 let source = (msgsource :> lvsource) in
2930 let modehash = findkeyhash conf "listview" in
2931 state.uioh <- coe (object
2932 inherit listview ~zebra:false ~helpmode:false
2933 ~source ~trusted:false ~modehash
2934 end);
2935 postRedisplay "enterannotmode";
2938 let gotoremote spec =
2939 let filename, dest = splitatchar spec '#' in
2940 let getpath filename =
2941 let path =
2942 if nonemptystr filename
2943 then
2944 if Filename.is_relative filename
2945 then
2946 let dir = Filename.dirname state.path in
2947 let dir =
2948 if Filename.is_implicit dir
2949 then Filename.concat (Sys.getcwd ()) dir
2950 else dir
2952 Filename.concat dir filename
2953 else filename
2954 else E.s
2956 if Sys.file_exists path
2957 then path
2958 else E.s
2960 let path = getpath filename in
2961 let dospawn lcmd =
2962 if conf.riani
2963 then
2964 let cmd = Lazy.force_val lcmd in
2965 match spawn cmd with
2966 | _pid -> ()
2967 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
2968 else
2969 let anchor = getanchor () in
2970 let ranchor = state.path, state.password, anchor, state.origin in
2971 state.origin <- E.s;
2972 state.ranchors <- ranchor :: state.ranchors;
2973 opendoc path E.s;
2975 if substratis spec 0 "page="
2976 then
2977 match Scanf.sscanf spec "page=%d" (fun n -> n) with
2978 | pageno ->
2979 state.anchor <- (pageno, 0.0, 0.0);
2980 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
2981 | exception exn ->
2982 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
2983 else (
2984 state.nameddest <- dest;
2985 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
2989 let gotounder = function
2990 | Ulinkuri s when isexternallink s ->
2991 if substratis s 0 "file://"
2992 then gotoremote @@ String.sub s 7 (String.length s - 7)
2993 else Help.gotouri conf.urilauncher s
2994 | Ulinkuri s ->
2995 let pageno, x, y = uritolocation s in
2996 addnav ();
2997 gotopagexy pageno x y
2998 | Utext _ | Unone -> ()
2999 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
3002 let gotooutline (_, _, kind) =
3003 match kind with
3004 | Onone -> ()
3005 | Oanchor ((pageno, y, _) as anchor) ->
3006 addnav ();
3007 gotoxy state.x @@
3008 getanchory (if conf.presentation then (pageno, y, 1.0) else anchor)
3009 | Ouri uri -> gotounder (Ulinkuri uri)
3010 | Olaunch cmd -> error "gotounder (Ulaunch %S)" cmd
3011 | Oremote (remote, pageno) ->
3012 error "gotounder (Uremote (%S,%d) )" remote pageno
3013 | Ohistory hist -> gotohist hist
3014 | Oremotedest (path, dest) ->
3015 error "gotounder (Uremotedest (%S, %S))" path dest
3018 class outlinesoucebase fetchoutlines = object (self)
3019 inherit lvsourcebase
3020 val mutable m_items = E.a
3021 val mutable m_minfo = E.a
3022 val mutable m_orig_items = E.a
3023 val mutable m_orig_minfo = E.a
3024 val mutable m_narrow_patterns = []
3025 val mutable m_gen = -1
3027 method getitemcount = Array.length m_items
3029 method getitem n =
3030 let s, n, _ = m_items.(n) in
3031 (s, n+0)
3033 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3034 ignore (uioh, first);
3035 let items, minfo =
3036 if m_narrow_patterns = []
3037 then m_orig_items, m_orig_minfo
3038 else m_items, m_minfo
3040 m_pan <- pan;
3041 if not cancel
3042 then (
3043 m_items <- items;
3044 m_minfo <- minfo;
3045 gotooutline m_items.(active);
3047 else (
3048 m_items <- items;
3049 m_minfo <- minfo;
3051 None
3053 method hasaction (_:int) = true
3055 method greetmsg =
3056 if Array.length m_items != Array.length m_orig_items
3057 then
3058 let s =
3059 match m_narrow_patterns with
3060 | one :: [] -> one
3061 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3063 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3064 else E.s
3066 method statestr =
3067 match m_narrow_patterns with
3068 | [] -> E.s
3069 | one :: [] -> one
3070 | head :: _ -> Utf8syms.ellipsis ^ head
3072 method narrow pattern =
3073 match Str.regexp_case_fold pattern with
3074 | exception _ -> ()
3075 | re ->
3076 let rec loop accu minfo n =
3077 if n = -1
3078 then (
3079 m_items <- Array.of_list accu;
3080 m_minfo <- Array.of_list minfo;
3082 else
3083 let (s, _, _) as o = m_items.(n) in
3084 let accu, minfo =
3085 match Str.search_forward re s 0 with
3086 | exception Not_found -> accu, minfo
3087 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3089 loop accu minfo (n-1)
3091 loop [] [] (Array.length m_items - 1)
3093 method! getminfo = m_minfo
3095 method denarrow =
3096 m_orig_items <- fetchoutlines ();
3097 m_minfo <- m_orig_minfo;
3098 m_items <- m_orig_items
3100 method add_narrow_pattern pattern =
3101 m_narrow_patterns <- pattern :: m_narrow_patterns
3103 method del_narrow_pattern =
3104 match m_narrow_patterns with
3105 | _ :: rest -> m_narrow_patterns <- rest
3106 | [] -> ()
3108 method renarrow =
3109 self#denarrow;
3110 match m_narrow_patterns with
3111 | pattern :: [] -> self#narrow pattern; pattern
3112 | list ->
3113 List.fold_left (fun accu pattern ->
3114 self#narrow pattern;
3115 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3117 method calcactive (_:anchor) = 0
3119 method reset anchor items =
3120 if state.gen != m_gen
3121 then (
3122 m_orig_items <- items;
3123 m_items <- items;
3124 m_narrow_patterns <- [];
3125 m_minfo <- E.a;
3126 m_orig_minfo <- E.a;
3127 m_gen <- state.gen;
3129 else (
3130 if items != m_orig_items
3131 then (
3132 m_orig_items <- items;
3133 if m_narrow_patterns == []
3134 then m_items <- items;
3137 let active = self#calcactive anchor in
3138 m_active <- active;
3139 m_first <- firstof m_first active
3143 let outlinesource fetchoutlines =
3144 (object
3145 inherit outlinesoucebase fetchoutlines
3146 method! calcactive anchor =
3147 let rely = getanchory anchor in
3148 let rec loop n best bestd =
3149 if n = Array.length m_items
3150 then best
3151 else
3152 let _, _, kind = m_items.(n) in
3153 match kind with
3154 | Oanchor anchor ->
3155 let orely = getanchory anchor in
3156 let d = abs (orely - rely) in
3157 if d < bestd
3158 then loop (n+1) n d
3159 else loop (n+1) best bestd
3160 | Onone | Oremote _ | Olaunch _
3161 | Oremotedest _ | Ouri _ | Ohistory _ ->
3162 loop (n+1) best bestd
3164 loop 0 ~-1 max_int
3165 end)
3168 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3169 let fetchoutlines sourcetype () =
3170 match sourcetype with
3171 | `bookmarks -> Array.of_list state.bookmarks
3172 | `outlines -> state.outlines
3173 | `history -> genhistoutlines () |> Array.of_list
3175 let so = outlinesource (fetchoutlines `outlines) in
3176 let sb = outlinesource (fetchoutlines `bookmarks) in
3177 let sh = outlinesource (fetchoutlines `history) in
3178 let mkselector sourcetype source =
3179 (fun errmsg ->
3180 let outlines = fetchoutlines sourcetype () in
3181 if Array.length outlines = 0
3182 then showtext ' ' errmsg
3183 else (
3184 resetmstate ();
3185 Wsi.setcursor Wsi.CURSOR_INHERIT;
3186 let anchor = getanchor () in
3187 source#reset anchor outlines;
3188 state.text <- source#greetmsg;
3189 state.uioh <-
3190 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3191 postRedisplay "enter selector";
3195 let mkenter sourcetype errmsg s = fun () -> mkselector sourcetype s errmsg in
3196 ( mkenter `outlines "document has no outline" so
3197 , mkenter `bookmarks "document has no bookmarks (yet)" sb
3198 , mkenter `history "history is empty" sh )
3201 let quickbookmark ?title () =
3202 match state.layout with
3203 | [] -> ()
3204 | l :: _ ->
3205 let title =
3206 match title with
3207 | None ->
3208 Unix.(
3209 let tm = localtime (now ()) in
3210 Printf.sprintf
3211 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3212 (l.pageno+1)
3213 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3215 | Some title -> title
3217 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3220 let setautoscrollspeed step goingdown =
3221 let incr = max 1 ((abs step) / 2) in
3222 let incr = if goingdown then incr else -incr in
3223 let astep = boundastep state.winh (step + incr) in
3224 state.autoscroll <- Some astep;
3227 let canpan () =
3228 match conf.columns with
3229 | Csplit _ -> true
3230 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3233 let panbound x = bound x (-state.w) state.winw;;
3235 let existsinrow pageno (columns, coverA, coverB) p =
3236 let last = ((pageno - coverA) mod columns) + columns in
3237 let rec any = function
3238 | [] -> false
3239 | l :: rest ->
3240 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3241 then p l
3242 else (
3243 if not (p l)
3244 then (if l.pageno = last then false else any rest)
3245 else true
3248 any state.layout
3251 let nextpage () =
3252 match state.layout with
3253 | [] ->
3254 let pageno = page_of_y state.y in
3255 gotoxy state.x (getpagey (pageno+1))
3256 | l :: rest ->
3257 match conf.columns with
3258 | Csingle _ ->
3259 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3260 then
3261 let y = clamp (pgscale state.winh) in
3262 gotoxy state.x y
3263 else
3264 let pageno = min (l.pageno+1) (state.pagecount-1) in
3265 gotoxy state.x (getpagey pageno)
3266 | Cmulti ((c, _, _) as cl, _) ->
3267 if conf.presentation
3268 && (existsinrow l.pageno cl
3269 (fun l -> l.pageh > l.pagey + l.pagevh))
3270 then
3271 let y = clamp (pgscale state.winh) in
3272 gotoxy state.x y
3273 else
3274 let pageno = min (l.pageno+c) (state.pagecount-1) in
3275 gotoxy state.x (getpagey pageno)
3276 | Csplit (n, _) ->
3277 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3278 then
3279 let pagey, pageh = getpageyh l.pageno in
3280 let pagey = pagey + pageh * l.pagecol in
3281 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3282 gotoxy state.x (pagey + pageh + ips)
3285 let prevpage () =
3286 match state.layout with
3287 | [] ->
3288 let pageno = page_of_y state.y in
3289 gotoxy state.x (getpagey (pageno-1))
3290 | l :: _ ->
3291 match conf.columns with
3292 | Csingle _ ->
3293 if conf.presentation && l.pagey != 0
3294 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3295 else
3296 let pageno = max 0 (l.pageno-1) in
3297 gotoxy state.x (getpagey pageno)
3298 | Cmulti ((c, _, coverB) as cl, _) ->
3299 if conf.presentation &&
3300 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3301 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3302 else
3303 let decr =
3304 if l.pageno = state.pagecount - coverB
3305 then 1
3306 else c
3308 let pageno = max 0 (l.pageno-decr) in
3309 gotoxy state.x (getpagey pageno)
3310 | Csplit (n, _) ->
3311 let y =
3312 if l.pagecol = 0
3313 then
3314 if l.pageno = 0
3315 then l.pagey
3316 else
3317 let pageno = max 0 (l.pageno-1) in
3318 let pagey, pageh = getpageyh pageno in
3319 pagey + (n-1)*pageh
3320 else
3321 let pagey, pageh = getpageyh l.pageno in
3322 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3324 gotoxy state.x y
3327 let save () =
3328 if emptystr conf.savecmd
3329 then adderrmsg "savepath-command is empty"
3330 "don't know where to save modified document"
3331 else
3332 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3333 let path =
3334 getcmdoutput
3335 (fun exn ->
3336 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3337 savecmd
3339 if nonemptystr path
3340 then
3341 let tmp = path ^ ".tmp" in
3342 savedoc tmp;
3343 Unix.rename tmp path;
3346 let viewkeyboard key mask =
3347 let enttext te =
3348 let mode = state.mode in
3349 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3350 state.text <- E.s;
3351 enttext ();
3352 postRedisplay "view:enttext"
3354 let ctrl = Wsi.withctrl mask in
3355 let open Keys in
3356 match Wsi.kc2kt key with
3357 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3359 | Ascii 'Q' -> exit 0
3361 | Ascii 'z' ->
3362 let yloc f =
3363 match List.rev state.rects with
3364 | [] -> ()
3365 | (pageno, _, (_, y0, _, y1, _, y2, _, y3)) :: _ ->
3366 f pageno (y0, y1, y2, y3)
3367 and yminmax (y0, y1, y2, y3) =
3368 let ym = min y0 y1 |> min y2 |> min y3 |> truncate in
3369 let yM = max y0 y1 |> max y2 |> max y3 |> truncate in
3370 ym, yM
3372 let ondone msg = state.text <- msg
3373 and zmod _ _ k =
3374 match [@warning "-4"] k with
3375 | Keys.Ascii 'z' ->
3376 let f pageno ys =
3377 let ym, yM = yminmax ys in
3378 let hh = (yM - ym)/2 in
3379 gotopage1 pageno (ym + hh - state.winh/2)
3381 yloc f;
3382 TEdone "center"
3383 | Keys.Ascii 't' ->
3384 let f pageno ys =
3385 let ym, _ = yminmax ys in
3386 gotopage1 pageno ym
3388 yloc f;
3389 TEdone "top"
3390 | Keys.Ascii 'b' ->
3391 let f pageno ys =
3392 let _, yM = yminmax ys in
3393 gotopage1 pageno (yM - state.winh)
3395 yloc f;
3396 TEdone "bottom"
3397 | _ -> TEstop
3399 enttext (": ", E.s, None, zmod state.mode, ondone, true)
3401 | Ascii 'W' ->
3402 if hasunsavedchanges ()
3403 then save ()
3405 | Insert ->
3406 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3407 then (
3408 state.mode <- (
3409 match state.lnava with
3410 | None -> LinkNav (Ltgendir 0)
3411 | Some pn -> LinkNav (Ltexact pn)
3413 gotoxy state.x state.y;
3415 else impmsg "keyboard link navigation does not work under rotation"
3417 | Escape | Ascii 'q' ->
3418 begin match state.mstate with
3419 | Mzoomrect _ ->
3420 resetmstate ();
3421 postRedisplay "kill rect";
3422 | Msel _
3423 | Mpan _
3424 | Mscrolly | Mscrollx
3425 | Mzoom _
3426 | Mnone ->
3427 begin match state.mode with
3428 | LinkNav ln ->
3429 begin match ln with
3430 | Ltexact pl -> state.lnava <- Some pl
3431 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3432 end;
3433 state.mode <- View;
3434 postRedisplay "esc leave linknav"
3435 | Birdseye _ | Textentry _ | View ->
3436 match state.ranchors with
3437 | [] -> raise Quit
3438 | (path, password, anchor, origin) :: rest ->
3439 state.ranchors <- rest;
3440 state.anchor <- anchor;
3441 state.origin <- origin;
3442 state.nameddest <- E.s;
3443 opendoc path password
3444 end;
3445 end;
3447 | Backspace ->
3448 addnavnorc ();
3449 gotoxy state.x (getnav ~-1)
3451 | Ascii 'o' -> enteroutlinemode ()
3452 | Ascii 'H' -> enterhistmode ()
3454 | Ascii 'u' ->
3455 state.rects <- [];
3456 state.text <- E.s;
3457 Hashtbl.iter (fun _ opaque ->
3458 clearmark opaque;
3459 Hashtbl.clear state.prects) state.pagemap;
3460 postRedisplay "dehighlight";
3462 | Ascii (('/' | '?') as c) ->
3463 let ondone isforw s =
3464 cbput state.hists.pat s;
3465 state.searchpattern <- s;
3466 search s isforw
3468 let s = String.make 1 c in
3469 enttext (s, E.s, Some (onhist state.hists.pat),
3470 textentry, ondone (c = '/'), true)
3472 | Ascii '+' | Ascii '=' when ctrl ->
3473 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3474 pivotzoom (conf.zoom +. incr)
3476 | Ascii '+' ->
3477 let ondone s =
3478 let n =
3479 try int_of_string s with exn ->
3480 state.text <-
3481 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3482 max_int
3484 if n != max_int
3485 then (
3486 conf.pagebias <- n;
3487 state.text <- "page bias is now " ^ string_of_int n;
3490 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3492 | Ascii '-' when ctrl ->
3493 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3494 pivotzoom (max 0.01 (conf.zoom -. decr))
3496 | Ascii '-' ->
3497 let ondone msg = state.text <- msg in
3498 enttext ("option: ", E.s, None,
3499 optentry state.mode, ondone, true)
3501 | Ascii '0' when ctrl ->
3502 if conf.zoom = 1.0
3503 then gotoxy 0 state.y
3504 else setzoom 1.0
3506 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3507 let cols =
3508 match conf.columns with
3509 | Csingle _ | Cmulti _ -> 1
3510 | Csplit (n, _) -> n
3512 let h = state.winh -
3513 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3515 let zoom = zoomforh state.winw h 0 cols in
3516 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3517 then setzoom zoom
3519 | Ascii '3' when ctrl ->
3520 let fm =
3521 match conf.fitmodel with
3522 | FitWidth -> FitProportional
3523 | FitProportional -> FitPage
3524 | FitPage -> FitWidth
3526 state.text <- "fit model: " ^ FMTE.to_string fm;
3527 reqlayout conf.angle fm
3529 | Ascii '4' when ctrl ->
3530 let zoom = getmaxw () /. float state.winw in
3531 if zoom > 0.0 then setzoom zoom
3533 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3535 | Ascii ('0'..'9' as c) when not ctrl ->
3536 let ondone s =
3537 let n =
3538 try int_of_string s with exn ->
3539 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3542 if n >= 0
3543 then (
3544 addnav ();
3545 cbput state.hists.pag (string_of_int n);
3546 gotopage1 (n + conf.pagebias - 1) 0;
3549 let pageentry text = function [@warning "-4"]
3550 | Keys.Ascii 'g' -> TEdone text
3551 | key -> intentry text key
3553 let text = String.make 1 c in
3554 enttext (":", text, Some (onhist state.hists.pag),
3555 pageentry, ondone, true)
3557 | Ascii 'b' ->
3558 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3559 postRedisplay "toggle scrollbar";
3561 | Ascii 'B' ->
3562 state.bzoom <- not state.bzoom;
3563 state.rects <- [];
3564 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3566 | Ascii 'l' ->
3567 conf.hlinks <- not conf.hlinks;
3568 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3569 postRedisplay "toggle highlightlinks";
3571 | Ascii 'F' ->
3572 if conf.angle mod 360 = 0
3573 then (
3574 state.glinks <- true;
3575 let mode = state.mode in
3576 state.mode <-
3577 Textentry (
3578 ("goto: ", E.s, None, linknentry, linknact gotounder, false),
3579 (fun _ ->
3580 state.glinks <- false;
3581 state.mode <- mode)
3583 state.text <- E.s;
3584 postRedisplay "view:linkent(F)"
3586 else impmsg "hint mode does not work under rotation"
3588 | Ascii 'y' ->
3589 state.glinks <- true;
3590 let mode = state.mode in
3591 state.mode <-
3592 Textentry (
3593 ("copy: ", E.s, None, linknentry,
3594 linknact (fun under ->
3595 selstring conf.selcmd (undertext under)), false),
3596 (fun _ ->
3597 state.glinks <- false;
3598 state.mode <- mode)
3600 state.text <- E.s;
3601 postRedisplay "view:linkent"
3603 | Ascii 'a' ->
3604 begin match state.autoscroll with
3605 | Some step ->
3606 conf.autoscrollstep <- step;
3607 state.autoscroll <- None
3608 | None ->
3609 state.autoscroll <- Some conf.autoscrollstep;
3610 state.slideshow <- state.slideshow land lnot 2
3613 | Ascii 'p' when ctrl ->
3614 launchpath () (* XXX where do error messages go? *)
3616 | Ascii 'P' ->
3617 setpresentationmode (not conf.presentation);
3618 showtext ' ' ("presentation mode " ^
3619 if conf.presentation then "on" else "off");
3621 | Ascii 'f' ->
3622 if List.mem Wsi.Fullscreen state.winstate
3623 then Wsi.reshape conf.cwinw conf.cwinh
3624 else Wsi.fullscreen ()
3626 | Ascii ('p'|'N') -> search state.searchpattern false
3627 | Ascii 'n' | Fn 3 -> search state.searchpattern true
3629 | Ascii 't' ->
3630 begin match state.layout with
3631 | [] -> ()
3632 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3635 | Ascii ' ' -> nextpage ()
3636 | Delete -> prevpage ()
3637 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3639 | Ascii 'w' ->
3640 begin match state.layout with
3641 | [] -> ()
3642 | l :: _ ->
3643 Wsi.reshape l.pagew l.pageh;
3644 postRedisplay "w"
3647 | Ascii '\'' -> enterbookmarkmode ()
3648 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3649 | Ascii 'i' -> enterinfomode ()
3650 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3652 | Ascii 'm' ->
3653 let ondone s =
3654 match state.layout with
3655 | l :: _ when nonemptystr s ->
3656 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3657 | _ -> ()
3659 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3661 | Ascii '~' ->
3662 quickbookmark ();
3663 showtext ' ' "Quick bookmark added";
3665 | Ascii 'x' -> state.roam ()
3667 | Ascii ('<'|'>' as c) ->
3668 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3670 | Ascii ('['|']' as c) ->
3671 conf.colorscale <-
3672 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3673 postRedisplay "brightness";
3675 | Ascii 'c' when state.mode = View ->
3676 if Wsi.withalt mask
3677 then (
3678 if conf.zoom > 1.0
3679 then
3680 let m = (state.winw - state.w) / 2 in
3681 gotoxy m state.y
3683 else
3684 let (c, a, b), z =
3685 match state.prevcolumns with
3686 | None -> (1, 0, 0), 1.0
3687 | Some (columns, z) ->
3688 let cab =
3689 match columns with
3690 | Csplit (c, _) -> -c, 0, 0
3691 | Cmulti ((c, a, b), _) -> c, a, b
3692 | Csingle _ -> 1, 0, 0
3694 cab, z
3696 setcolumns View c a b;
3697 setzoom z
3699 | Down | Up when ctrl && Wsi.withshift mask ->
3700 let zoom, x = state.prevzoom in
3701 setzoom zoom;
3702 state.x <- x;
3704 | Up ->
3705 begin match state.autoscroll with
3706 | None ->
3707 begin match state.mode with
3708 | Birdseye beye -> upbirdseye 1 beye
3709 | Textentry _ | View | LinkNav _ ->
3710 if ctrl
3711 then gotoxy state.x (clamp ~-(state.winh/2))
3712 else (
3713 if not (Wsi.withshift mask) && conf.presentation
3714 then prevpage ()
3715 else gotoxy state.x (clamp (-conf.scrollstep))
3718 | Some n -> setautoscrollspeed n false
3721 | Down ->
3722 begin match state.autoscroll with
3723 | None ->
3724 begin match state.mode with
3725 | Birdseye beye -> downbirdseye 1 beye
3726 | Textentry _ | View | LinkNav _ ->
3727 if ctrl
3728 then gotoxy state.x (clamp (state.winh/2))
3729 else (
3730 if not (Wsi.withshift mask) && conf.presentation
3731 then nextpage ()
3732 else gotoxy state.x (clamp (conf.scrollstep))
3735 | Some n -> setautoscrollspeed n true
3738 | Left | Right when not (Wsi.withalt mask) ->
3739 if canpan ()
3740 then
3741 let dx =
3742 if ctrl
3743 then state.winw / 2
3744 else conf.hscrollstep
3746 let dx =
3747 let pv = Wsi.kc2kt key in
3748 if pv = Keys.Left then dx else -dx
3750 gotoxy (panbound (state.x + dx)) state.y
3751 else (
3752 state.text <- E.s;
3753 postRedisplay "left/right"
3756 | Prior ->
3757 let y =
3758 if ctrl
3759 then
3760 match state.layout with
3761 | [] -> state.y
3762 | l :: _ -> state.y - l.pagey
3763 else clamp (pgscale (-state.winh))
3765 gotoxy state.x y
3767 | Next ->
3768 let y =
3769 if ctrl
3770 then
3771 match List.rev state.layout with
3772 | [] -> state.y
3773 | l :: _ -> getpagey l.pageno
3774 else clamp (pgscale state.winh)
3776 gotoxy state.x y
3778 | Ascii 'g' | Home ->
3779 addnav ();
3780 gotoxy 0 0
3781 | Ascii 'G' | End ->
3782 addnav ();
3783 gotoxy 0 (clamp state.maxy)
3785 | Right when Wsi.withalt mask ->
3786 addnavnorc ();
3787 gotoxy state.x (getnav 1)
3788 | Left when Wsi.withalt mask ->
3789 addnavnorc ();
3790 gotoxy state.x (getnav ~-1)
3792 | Ascii 'r' ->
3793 reload ()
3795 | Ascii 'v' when conf.debug ->
3796 state.rects <- [];
3797 List.iter (fun l ->
3798 match getopaque l.pageno with
3799 | None -> ()
3800 | Some opaque ->
3801 let x0, y0, x1, y1 = pagebbox opaque in
3802 let rect = (float x0, float y0,
3803 float x1, float y0,
3804 float x1, float y1,
3805 float x0, float y1) in
3806 debugrect rect;
3807 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3808 state.rects <- (l.pageno, color, rect) :: state.rects;
3809 ) state.layout;
3810 postRedisplay "v";
3812 | Ascii '|' ->
3813 let mode = state.mode in
3814 let cmd = ref E.s in
3815 let onleave = function
3816 | Cancel -> state.mode <- mode
3817 | Confirm ->
3818 List.iter (fun l ->
3819 match getopaque l.pageno with
3820 | Some opaque -> pipesel opaque !cmd
3821 | None -> ()) state.layout;
3822 state.mode <- mode
3824 let ondone s =
3825 cbput state.hists.sel s;
3826 cmd := s
3828 let te =
3829 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3831 postRedisplay "|";
3832 state.mode <- Textentry (te, onleave);
3834 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3835 vlog "huh? %s" (Wsi.keyname key)
3838 let linknavkeyboard key mask linknav =
3839 let pv = Wsi.kc2kt key in
3840 let getpage pageno =
3841 let rec loop = function
3842 | [] -> None
3843 | l :: _ when l.pageno = pageno -> Some l
3844 | _ :: rest -> loop rest
3845 in loop state.layout
3847 let doexact (pageno, n) =
3848 match getopaque pageno, getpage pageno with
3849 | Some opaque, Some l ->
3850 if pv = Keys.Enter
3851 then
3852 let under = getlink opaque n in
3853 postRedisplay "link gotounder";
3854 gotounder under;
3855 state.mode <- View;
3856 else
3857 let opt, dir =
3858 let open Keys in
3859 match pv with
3860 | Home -> Some (findlink opaque LDfirst), -1
3861 | End -> Some (findlink opaque LDlast), 1
3862 | Left -> Some (findlink opaque (LDleft n)), -1
3863 | Right -> Some (findlink opaque (LDright n)), 1
3864 | Up -> Some (findlink opaque (LDup n)), -1
3865 | Down -> Some (findlink opaque (LDdown n)), 1
3866 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3867 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3869 let pwl l dir =
3870 begin match findpwl l.pageno dir with
3871 | Pwlnotfound -> ()
3872 | Pwl pageno ->
3873 let notfound dir =
3874 state.mode <- LinkNav (Ltgendir dir);
3875 let y, h = getpageyh pageno in
3876 let y =
3877 if dir < 0
3878 then y + h - state.winh
3879 else y
3881 gotoxy state.x y
3883 begin match getopaque pageno, getpage pageno with
3884 | Some opaque, Some _ ->
3885 let link =
3886 let ld = if dir > 0 then LDfirst else LDlast in
3887 findlink opaque ld
3889 begin match link with
3890 | Lfound m ->
3891 showlinktype (getlink opaque m);
3892 state.mode <- LinkNav (Ltexact (pageno, m));
3893 postRedisplay "linknav jpage";
3894 | Lnotfound -> notfound dir
3895 end;
3896 | _ -> notfound dir
3897 end;
3898 end;
3900 begin match opt with
3901 | Some Lnotfound -> pwl l dir;
3902 | Some (Lfound m) ->
3903 if m = n
3904 then pwl l dir
3905 else (
3906 let _, y0, _, y1 = getlinkrect opaque m in
3907 if y0 < l.pagey
3908 then gotopage1 l.pageno y0
3909 else (
3910 let d = fstate.fontsize + 1 in
3911 if y1 - l.pagey > l.pagevh - d
3912 then gotopage1 l.pageno (y1 - state.winh + d)
3913 else postRedisplay "linknav";
3915 showlinktype (getlink opaque m);
3916 state.mode <- LinkNav (Ltexact (l.pageno, m));
3919 | None -> viewkeyboard key mask
3920 end;
3921 | _ -> viewkeyboard key mask
3923 if pv = Keys.Insert
3924 then (
3925 begin match linknav with
3926 | Ltexact pa -> state.lnava <- Some pa
3927 | Ltgendir _ | Ltnotready _ -> ()
3928 end;
3929 state.mode <- View;
3930 postRedisplay "leave linknav"
3932 else
3933 match linknav with
3934 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3935 | Ltexact exact -> doexact exact
3938 let keyboard key mask =
3939 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3940 then wcmd "interrupt"
3941 else state.uioh <- state.uioh#key key mask
3944 let birdseyekeyboard key mask
3945 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3946 let incr =
3947 match conf.columns with
3948 | Csingle _ -> 1
3949 | Cmulti ((c, _, _), _) -> c
3950 | Csplit _ -> error "bird's eye split mode"
3952 let pgh layout = List.fold_left
3953 (fun m l -> max l.pageh m) state.winh layout in
3954 let open Keys in
3955 match Wsi.kc2kt key with
3956 | Ascii 'l' when Wsi.withctrl mask ->
3957 let y, h = getpageyh pageno in
3958 let top = (state.winh - h) / 2 in
3959 gotoxy state.x (max 0 (y - top))
3960 | Enter -> leavebirdseye beye false
3961 | Escape -> leavebirdseye beye true
3962 | Up -> upbirdseye incr beye
3963 | Down -> downbirdseye incr beye
3964 | Left -> upbirdseye 1 beye
3965 | Right -> downbirdseye 1 beye
3967 | Prior ->
3968 begin match state.layout with
3969 | l :: _ ->
3970 if l.pagey != 0
3971 then (
3972 state.mode <- Birdseye (
3973 oconf, leftx, l.pageno, hooverpageno, anchor
3975 gotopage1 l.pageno 0;
3977 else (
3978 let layout = layout state.x (state.y-state.winh)
3979 state.winw
3980 (pgh state.layout) in
3981 match layout with
3982 | [] -> gotoxy state.x (clamp (-state.winh))
3983 | l :: _ ->
3984 state.mode <- Birdseye (
3985 oconf, leftx, l.pageno, hooverpageno, anchor
3987 gotopage1 l.pageno 0
3990 | [] -> gotoxy state.x (clamp (-state.winh))
3991 end;
3993 | Next ->
3994 begin match List.rev state.layout with
3995 | l :: _ ->
3996 let layout = layout state.x
3997 (state.y + (pgh state.layout))
3998 state.winw state.winh in
3999 begin match layout with
4000 | [] ->
4001 let incr = l.pageh - l.pagevh in
4002 if incr = 0
4003 then (
4004 state.mode <-
4005 Birdseye (
4006 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4008 postRedisplay "birdseye pagedown";
4010 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
4012 | l :: _ ->
4013 state.mode <-
4014 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4015 gotopage1 l.pageno 0;
4018 | [] -> gotoxy state.x (clamp state.winh)
4019 end;
4021 | Home ->
4022 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4023 gotopage1 0 0
4025 | End ->
4026 let pageno = state.pagecount - 1 in
4027 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4028 if not (pagevisible state.layout pageno)
4029 then
4030 let h =
4031 match List.rev state.pdims with
4032 | [] -> state.winh
4033 | (_, _, h, _) :: _ -> h
4035 gotoxy
4036 state.x
4037 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4038 else postRedisplay "birdseye end";
4040 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4043 let drawpage l =
4044 let color =
4045 match state.mode with
4046 | Textentry _ -> scalecolor 0.4
4047 | LinkNav _ | View -> scalecolor 1.0
4048 | Birdseye (_, _, pageno, hooverpageno, _) ->
4049 if l.pageno = hooverpageno
4050 then scalecolor 0.9
4051 else (
4052 if l.pageno = pageno
4053 then (
4054 let c = scalecolor 1.0 in
4055 GlDraw.color c;
4056 GlDraw.line_width 3.0;
4057 let dispx = l.pagedispx in
4058 linerect
4059 (float (dispx-1)) (float (l.pagedispy-1))
4060 (float (dispx+l.pagevw+1))
4061 (float (l.pagedispy+l.pagevh+1));
4062 GlDraw.line_width 1.0;
4065 else scalecolor 0.8
4068 drawtiles l color;
4071 let postdrawpage l linkindexbase =
4072 match getopaque l.pageno with
4073 | Some opaque ->
4074 if tileready l l.pagex l.pagey
4075 then
4076 let x = l.pagedispx - l.pagex
4077 and y = l.pagedispy - l.pagey in
4078 let hlmask =
4079 match conf.columns with
4080 | Csingle _ | Cmulti _ ->
4081 (if conf.hlinks then 1 else 0)
4082 + (if state.glinks
4083 && not (isbirdseye state.mode) then 2 else 0)
4084 | Csplit _ -> 0
4086 let s =
4087 match state.mode with
4088 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4089 | Textentry _
4090 | Birdseye _
4091 | View
4092 | LinkNav _ -> E.s
4094 Hashtbl.find_all state.prects l.pageno |>
4095 List.iter (fun vals -> drawprect opaque x y vals);
4096 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4097 if n < 0
4098 then (Glutils.redisplay := true; 0)
4099 else n
4100 else 0
4101 | _ -> 0
4104 let scrollindicator () =
4105 let sbw, ph, sh = state.uioh#scrollph in
4106 let sbh, pw, sw = state.uioh#scrollpw in
4108 let x0,x1,hx0 =
4109 if conf.leftscroll
4110 then (0, sbw, sbw)
4111 else ((state.winw - sbw), state.winw, 0)
4114 Gl.enable `blend;
4115 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4116 let (r, g, b, alpha) = conf.sbarcolor in
4117 GlDraw.color (r, g, b) ~alpha;
4118 filledrect (float x0) 0. (float x1) (float state.winh);
4119 filledrect
4120 (float hx0) (float (state.winh - sbh))
4121 (float (hx0 + state.winw)) (float state.winh);
4122 let (r, g, b, alpha) = conf.sbarhndlcolor in
4123 GlDraw.color (r, g, b) ~alpha;
4125 filledrect (float x0) ph (float x1) (ph +. sh);
4126 let pw = pw +. float hx0 in
4127 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4128 Gl.disable `blend;
4131 let showsel () =
4132 match state.mstate with
4133 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4134 | Msel ((x0, y0), (x1, y1)) ->
4135 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4136 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4137 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4138 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4141 let showrects = function
4142 | [] -> ()
4143 | rects ->
4144 Gl.enable `blend;
4145 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4146 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4147 List.iter
4148 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4149 List.iter (fun l ->
4150 if l.pageno = pageno
4151 then
4152 let dx = float (l.pagedispx - l.pagex) in
4153 let dy = float (l.pagedispy - l.pagey) in
4154 let r, g, b, alpha = c in
4155 GlDraw.color (r, g, b) ~alpha;
4156 filledrect2
4157 (x0+.dx) (y0+.dy)
4158 (x1+.dx) (y1+.dy)
4159 (x3+.dx) (y3+.dy)
4160 (x2+.dx) (y2+.dy);
4161 ) state.layout
4162 ) rects;
4163 Gl.disable `blend;
4166 let display () =
4167 GlDraw.color (scalecolor2 conf.bgcolor);
4168 GlClear.color (scalecolor2 conf.bgcolor);
4169 GlClear.clear [`color];
4170 List.iter drawpage state.layout;
4171 let rects =
4172 match state.mode with
4173 | LinkNav (Ltexact (pageno, linkno)) ->
4174 begin match getopaque pageno with
4175 | Some opaque ->
4176 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4177 let color = (0.0, 0.0, 0.5, 0.5) in
4178 (pageno, color,
4179 (float x0, float y0,
4180 float x1, float y0,
4181 float x1, float y1,
4182 float x0, float y1)
4183 ) :: state.rects
4184 | None -> state.rects
4186 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4187 | Birdseye _
4188 | Textentry _
4189 | View -> state.rects
4191 showrects rects;
4192 let rec postloop linkindexbase = function
4193 | l :: rest ->
4194 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4195 postloop linkindexbase rest
4196 | [] -> ()
4198 showsel ();
4199 postloop 0 state.layout;
4200 state.uioh#display;
4201 begin match state.mstate with
4202 | Mzoomrect ((x0, y0), (x1, y1)) ->
4203 Gl.enable `blend;
4204 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4205 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4206 filledrect (float x0) (float y0) (float x1) (float y1);
4207 Gl.disable `blend;
4208 | Msel _
4209 | Mpan _
4210 | Mscrolly | Mscrollx
4211 | Mzoom _
4212 | Mnone -> ()
4213 end;
4214 enttext ();
4215 scrollindicator ();
4216 Wsi.swapb ();
4219 let display () =
4220 match state.reload with
4221 | Some (x, y, t) ->
4222 if x != state.x || y != state.y || abs_float @@ now () -. t > 0.5
4223 || (state.layout != [] && layoutready state.layout)
4224 then (
4225 state.reload <- None;
4226 display ()
4228 | None -> display ()
4231 let zoomrect x y x1 y1 =
4232 let x0 = min x x1
4233 and x1 = max x x1
4234 and y0 = min y y1 in
4235 let zoom = (float state.w) /. float (x1 - x0) in
4236 let margin =
4237 let simple () =
4238 if state.w < state.winw
4239 then (state.winw - state.w) / 2
4240 else 0
4242 match conf.fitmodel with
4243 | FitWidth | FitProportional -> simple ()
4244 | FitPage ->
4245 match conf.columns with
4246 | Csplit _ ->
4247 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4248 | Cmulti _ | Csingle _ -> simple ()
4250 gotoxy ((state.x + margin) - x0) (state.y + y0);
4251 state.anchor <- getanchor ();
4252 setzoom zoom;
4253 resetmstate ();
4256 let annot inline x y =
4257 match unproject x y with
4258 | Some (opaque, n, ux, uy) ->
4259 let add text =
4260 addannot opaque ux uy text;
4261 wcmd "freepage %s" (~> opaque);
4262 Hashtbl.remove state.pagemap (n, state.gen);
4263 flushtiles ();
4264 gotoxy state.x state.y
4266 if inline
4267 then
4268 let ondone s = add s in
4269 let mode = state.mode in
4270 state.mode <- Textentry (
4271 ("annotation: ", E.s, None, textentry, ondone, true),
4272 fun _ -> state.mode <- mode);
4273 state.text <- E.s;
4274 enttext ();
4275 postRedisplay "annot"
4276 else add @@ getusertext E.s
4277 | _ -> ()
4280 let zoomblock x y =
4281 let g opaque l px py =
4282 match rectofblock opaque px py with
4283 | Some a ->
4284 let x0 = a.(0) -. 20. in
4285 let x1 = a.(1) +. 20. in
4286 let y0 = a.(2) -. 20. in
4287 let zoom = (float state.w) /. (x1 -. x0) in
4288 let pagey = getpagey l.pageno in
4289 let margin = (state.w - l.pagew)/2 in
4290 let nx = -truncate x0 - margin in
4291 gotoxy nx (pagey + truncate y0);
4292 state.anchor <- getanchor ();
4293 setzoom zoom;
4294 None
4295 | None -> None
4297 match conf.columns with
4298 | Csplit _ ->
4299 impmsg "block zooming does not work properly in split columns mode"
4300 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4303 let scrollx x =
4304 let winw = state.winw - 1 in
4305 let s = float x /. float winw in
4306 let destx = truncate (float (state.w + winw) *. s) in
4307 gotoxy (winw - destx) state.y;
4308 state.mstate <- Mscrollx;
4311 let scrolly y =
4312 let s = float y /. float state.winh in
4313 let desty = truncate (s *. float (maxy ())) in
4314 gotoxy state.x desty;
4315 state.mstate <- Mscrolly;
4318 let viewmulticlick clicks x y mask =
4319 let g opaque l px py =
4320 let mark =
4321 match clicks with
4322 | 2 -> Mark_word
4323 | 3 -> Mark_line
4324 | 4 -> Mark_block
4325 | _ -> Mark_page
4327 if markunder opaque px py mark
4328 then (
4329 Some (fun () ->
4330 let dopipe cmd =
4331 match getopaque l.pageno with
4332 | None -> ()
4333 | Some opaque -> pipesel opaque cmd
4335 state.roam <- (fun () -> dopipe conf.paxcmd);
4336 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4339 else None
4341 postRedisplay "viewmulticlick";
4342 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4345 let canselect () =
4346 match conf.columns with
4347 | Csplit _ -> false
4348 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4351 let viewmouse button down x y mask =
4352 match button with
4353 | n when (n == 4 || n == 5) && not down ->
4354 if Wsi.withctrl mask
4355 then (
4356 let incr =
4357 if n = 5
4358 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4359 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4361 let fx, fy =
4362 match state.mstate with
4363 | Mzoom (oldn, _, pos) when n = oldn -> pos
4364 | Mzoomrect _ | Mnone | Mpan _
4365 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4367 let zoom = conf.zoom -. incr in
4368 state.mstate <- Mzoom (n, 0, (x, y));
4369 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4370 then pivotzoom ~x ~y zoom
4371 else pivotzoom zoom
4373 else (
4374 match state.autoscroll with
4375 | Some step -> setautoscrollspeed step (n=4)
4376 | None ->
4377 if conf.wheelbypage || conf.presentation
4378 then (
4379 if n = 4
4380 then prevpage ()
4381 else nextpage ()
4383 else
4384 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4385 let incr = incr * 2 in
4386 let y = clamp incr in
4387 gotoxy state.x y
4390 | n when (n = 6 || n = 7) && not down && canpan () ->
4391 let x =
4392 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4393 gotoxy x state.y
4395 | 1 when Wsi.withshift mask ->
4396 state.mstate <- Mnone;
4397 if not down
4398 then (
4399 match unproject x y with
4400 | None -> ()
4401 | Some (_, pageno, ux, uy) ->
4402 let cmd = Printf.sprintf
4403 "%s %s %d %d %d"
4404 conf.stcmd state.path pageno ux uy
4406 match spawn cmd [] with
4407 | exception exn ->
4408 impmsg "execution of synctex command(%S) failed: %S"
4409 conf.stcmd @@ exntos exn
4410 | _pid -> ()
4413 | 1 when Wsi.withctrl mask ->
4414 if down
4415 then (
4416 Wsi.setcursor Wsi.CURSOR_FLEUR;
4417 state.mstate <- Mpan (x, y)
4419 else state.mstate <- Mnone
4421 | 3 ->
4422 if down
4423 then (
4424 if Wsi.withshift mask
4425 then (
4426 annot conf.annotinline x y;
4427 postRedisplay "addannot"
4429 else
4430 let p = (x, y) in
4431 Wsi.setcursor Wsi.CURSOR_CYCLE;
4432 state.mstate <- Mzoomrect (p, p)
4434 else (
4435 match state.mstate with
4436 | Mzoomrect ((x0, y0), _) ->
4437 if abs (x-x0) > 10 && abs (y - y0) > 10
4438 then zoomrect x0 y0 x y
4439 else (
4440 resetmstate ();
4441 postRedisplay "kill accidental zoom rect";
4443 | Msel _
4444 | Mpan _
4445 | Mscrolly | Mscrollx
4446 | Mzoom _
4447 | Mnone -> resetmstate ()
4450 | 1 when vscrollhit x ->
4451 if down
4452 then
4453 let _, position, sh = state.uioh#scrollph in
4454 if y > truncate position && y < truncate (position +. sh)
4455 then state.mstate <- Mscrolly
4456 else scrolly y
4457 else state.mstate <- Mnone
4459 | 1 when y > state.winh - hscrollh () ->
4460 if down
4461 then
4462 let _, position, sw = state.uioh#scrollpw in
4463 if x > truncate position && x < truncate (position +. sw)
4464 then state.mstate <- Mscrollx
4465 else scrollx x
4466 else state.mstate <- Mnone
4468 | 1 when state.bzoom -> if not down then zoomblock x y
4470 | 1 ->
4471 let dest = if down then getunder x y else Unone in
4472 begin match dest with
4473 | Ulinkuri _ -> gotounder dest
4474 | Unone when down ->
4475 Wsi.setcursor Wsi.CURSOR_FLEUR;
4476 state.mstate <- Mpan (x, y);
4477 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4478 | Unone | Utext _ ->
4479 if down
4480 then (
4481 if canselect ()
4482 then (
4483 state.mstate <- Msel ((x, y), (x, y));
4484 postRedisplay "mouse select";
4487 else (
4488 match state.mstate with
4489 | Mnone -> ()
4490 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4491 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4492 | Mpan _ ->
4493 Wsi.setcursor Wsi.CURSOR_INHERIT;
4494 state.mstate <- Mnone
4495 | Msel ((x0, y0), (x1, y1)) ->
4496 let rec loop = function
4497 | [] -> ()
4498 | l :: rest ->
4499 let inside =
4500 let a0 = l.pagedispy in
4501 let a1 = a0 + l.pagevh in
4502 let b0 = l.pagedispx in
4503 let b1 = b0 + l.pagevw in
4504 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4505 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4507 if inside
4508 then
4509 match getopaque l.pageno with
4510 | Some opaque ->
4511 let dosel cmd () =
4512 pipef ~closew:false "Msel"
4513 (fun w ->
4514 copysel w opaque;
4515 postRedisplay "Msel") cmd
4517 dosel conf.selcmd ();
4518 state.roam <- dosel conf.paxcmd;
4519 | None -> ()
4520 else loop rest
4522 loop state.layout;
4523 resetmstate ();
4526 | _ -> ()
4529 let birdseyemouse button down x y mask
4530 (conf, leftx, _, hooverpageno, anchor) =
4531 match button with
4532 | 1 when down ->
4533 let rec loop = function
4534 | [] -> ()
4535 | l :: rest ->
4536 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4537 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4538 then
4539 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false
4540 else loop rest
4542 loop state.layout
4543 | 3 -> ()
4544 | _ -> viewmouse button down x y mask
4547 let uioh = object
4548 method display = ()
4550 method key key mask =
4551 begin match state.mode with
4552 | Textentry textentry -> textentrykeyboard key mask textentry
4553 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4554 | View -> viewkeyboard key mask
4555 | LinkNav linknav -> linknavkeyboard key mask linknav
4556 end;
4557 state.uioh
4559 method button button bstate x y mask =
4560 begin match state.mode with
4561 | LinkNav _ | View -> viewmouse button bstate x y mask
4562 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4563 | Textentry _ -> ()
4564 end;
4565 state.uioh
4567 method multiclick clicks x y mask =
4568 begin match state.mode with
4569 | LinkNav _ | View -> viewmulticlick clicks x y mask
4570 | Birdseye _ | Textentry _ -> ()
4571 end;
4572 state.uioh
4574 method motion x y =
4575 begin match state.mode with
4576 | Textentry _ -> ()
4577 | View | Birdseye _ | LinkNav _ ->
4578 match state.mstate with
4579 | Mzoom _ | Mnone -> ()
4580 | Mpan (x0, y0) ->
4581 let dx = x - x0
4582 and dy = y0 - y in
4583 state.mstate <- Mpan (x, y);
4584 let x = if canpan () then panbound (state.x + dx) else state.x in
4585 let y = clamp dy in
4586 gotoxy x y
4588 | Msel (a, _) ->
4589 state.mstate <- Msel (a, (x, y));
4590 postRedisplay "motion select";
4592 | Mscrolly ->
4593 let y = min state.winh (max 0 y) in
4594 scrolly y
4596 | Mscrollx ->
4597 let x = min state.winw (max 0 x) in
4598 scrollx x
4600 | Mzoomrect (p0, _) ->
4601 state.mstate <- Mzoomrect (p0, (x, y));
4602 postRedisplay "motion zoomrect";
4603 end;
4604 state.uioh
4606 method pmotion x y =
4607 begin match state.mode with
4608 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4609 let rec loop = function
4610 | [] ->
4611 if hooverpageno != -1
4612 then (
4613 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4614 postRedisplay "pmotion birdseye no hoover";
4616 | l :: rest ->
4617 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4618 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4619 then (
4620 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4621 postRedisplay "pmotion birdseye hoover";
4623 else loop rest
4625 loop state.layout
4627 | Textentry _ -> ()
4629 | LinkNav _ | View ->
4630 match state.mstate with
4631 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4632 | Mnone ->
4633 updateunder x y;
4634 if canselect ()
4635 then
4636 match conf.pax with
4637 | None -> ()
4638 | Some past ->
4639 let now = now () in
4640 let delta = now -. past in
4641 if delta > 0.01
4642 then paxunder x y
4643 else conf.pax <- Some now
4644 end;
4645 state.uioh
4647 method infochanged _ = ()
4649 method scrollph =
4650 let maxy = maxy () in
4651 let p, h =
4652 if maxy = 0
4653 then 0.0, float state.winh
4654 else scrollph state.y maxy
4656 vscrollw (), p, h
4658 method scrollpw =
4659 let fwinw = float (state.winw - vscrollw ()) in
4660 let sw =
4661 let sw = fwinw /. float state.w in
4662 let sw = fwinw *. sw in
4663 max sw (float conf.scrollh)
4665 let position =
4666 let maxx = state.w + state.winw in
4667 let x = state.winw - state.x in
4668 let percent = float x /. float maxx in
4669 (fwinw -. sw) *. percent
4671 hscrollh (), position, sw
4673 method modehash =
4674 let modename =
4675 match state.mode with
4676 | LinkNav _ -> "links"
4677 | Textentry _ -> "textentry"
4678 | Birdseye _ -> "birdseye"
4679 | View -> "view"
4681 findkeyhash conf modename
4683 method eformsgs = true
4684 method alwaysscrolly = false
4685 method scroll dx dy =
4686 let x = if canpan () then panbound (state.x + dx) else state.x in
4687 gotoxy x (clamp (2 * dy));
4688 state.uioh
4689 method zoom z x y =
4690 pivotzoom ~x ~y (conf.zoom *. exp z);
4691 end;;
4693 let addrect pageno r g b a x0 y0 x1 y1 =
4694 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4697 let ract cmds =
4698 let cl = splitatchar cmds ' ' in
4699 let scan s fmt f =
4700 try Scanf.sscanf s fmt f
4701 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4702 cmds @@ exntos exn
4704 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4705 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4706 s pageno r g b a x0 y0 x1 y1;
4707 onpagerect
4708 pageno
4709 (fun w h ->
4710 let _,w1,h1,_ = getpagedim pageno in
4711 let sw = float w1 /. float w
4712 and sh = float h1 /. float h in
4713 let x0s = x0 *. sw
4714 and x1s = x1 *. sw
4715 and y0s = y0 *. sh
4716 and y1s = y1 *. sh in
4717 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4718 let color = (r, g, b, a) in
4719 if conf.verbose then debugrect rect;
4720 state.rects <- (pageno, color, rect) :: state.rects;
4721 postRedisplay s;
4724 match cl with
4725 | "reload", "" -> reload ()
4726 | "goto", args ->
4727 scan args "%u %f %f"
4728 (fun pageno x y ->
4729 let cmd, _ = state.geomcmds in
4730 if emptystr cmd
4731 then gotopagexy pageno x y
4732 else
4733 let f prevf () =
4734 gotopagexy pageno x y;
4735 prevf ()
4737 state.reprf <- f state.reprf
4739 | "goto1", args -> scan args "%u %f" gotopage
4740 | "gotor", args -> scan args "%S" gotoremote
4741 | "rect", args ->
4742 scan args "%u %u %f %f %f %f"
4743 (fun pageno c x0 y0 x1 y1 ->
4744 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4745 rectx "rect" pageno color x0 y0 x1 y1;
4747 | "prect", args ->
4748 scan args "%u %f %f %f %f %f %f %f %f"
4749 (fun pageno r g b alpha x0 y0 x1 y1 ->
4750 addrect pageno r g b alpha x0 y0 x1 y1;
4751 postRedisplay "prect"
4753 | "pgoto", args ->
4754 scan args "%u %f %f"
4755 (fun pageno x y ->
4756 let optopaque =
4757 match getopaque pageno with
4758 | Some opaque -> opaque
4759 | None -> ~< E.s
4761 pgoto optopaque pageno x y;
4762 let rec fixx = function
4763 | [] -> ()
4764 | l :: rest ->
4765 if l.pageno = pageno
4766 then gotoxy (state.x - l.pagedispx) state.y
4767 else fixx rest
4769 let layout =
4770 let mult =
4771 match conf.columns with
4772 | Csingle _ | Csplit _ -> 1
4773 | Cmulti ((n, _, _), _) -> n
4775 layout 0 state.y (state.winw * mult) state.winh
4777 fixx layout
4779 | "activatewin", "" -> Wsi.activatewin ()
4780 | "quit", "" -> raise Quit
4781 | "keys", keys ->
4782 begin try
4783 let l = Config.keys_of_string keys in
4784 List.iter (fun (k, m) -> keyboard k m) l
4785 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4786 cmds @@ exntos exn
4788 | "clearrects", "" ->
4789 Hashtbl.clear state.prects;
4790 postRedisplay "clearrects"
4791 | _ ->
4792 adderrfmt "remote command"
4793 "error processing remote command: %S\n" cmds;
4796 let remote =
4797 let scratch = Bytes.create 80 in
4798 let buf = Buffer.create 80 in
4799 fun fd ->
4800 match tempfailureretry (Unix.read fd scratch 0) 80 with
4801 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4802 | 0 ->
4803 Unix.close fd;
4804 if Buffer.length buf > 0
4805 then (
4806 let s = Buffer.contents buf in
4807 Buffer.clear buf;
4808 ract s;
4810 None
4811 | n ->
4812 let rec eat ppos =
4813 let nlpos =
4814 match Bytes.index_from scratch ppos '\n' with
4815 | pos -> if pos >= n then -1 else pos
4816 | exception Not_found -> -1
4818 if nlpos >= 0
4819 then (
4820 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4821 let s = Buffer.contents buf in
4822 Buffer.clear buf;
4823 ract s;
4824 eat (nlpos+1);
4826 else (
4827 Buffer.add_subbytes buf scratch ppos (n-ppos);
4828 Some fd
4830 in eat 0
4833 let remoteopen path =
4834 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4835 with exn ->
4836 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4837 None
4840 let () =
4841 Utils.vlogf := (fun s -> if conf.verbose then prerr_endline s else ignore s);
4842 let gcconfig = ref false in
4843 let trimcachepath = ref E.s in
4844 let rcmdpath = ref E.s in
4845 let pageno = ref None in
4846 let openlast = ref false in
4847 let doreap = ref false in
4848 let csspath = ref None in
4849 selfexec := Sys.executable_name;
4850 Arg.parse
4851 (Arg.align
4852 [("-p", Arg.String (fun s -> state.password <- s),
4853 "<password> Set password");
4855 ("-f", Arg.String
4856 (fun s ->
4857 Config.fontpath := s;
4858 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4860 "<path> Set path to the user interface font");
4862 ("-c", Arg.String
4863 (fun s ->
4864 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4865 Config.confpath := s),
4866 "<path> Set path to the configuration file");
4868 ("-last", Arg.Set openlast, " Open last document");
4870 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4871 "<page-number> Jump to page");
4873 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4874 "<path> Set path to the trim cache file");
4876 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4877 "<named-destination> Set named destination");
4879 ("-remote", Arg.String (fun s -> rcmdpath := s),
4880 "<path> Set path to the source of remote commands");
4882 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4884 ("-v", Arg.Unit (fun () ->
4885 Printf.printf
4886 "%s\nconfiguration file: %s\n"
4887 (Help.version ())
4888 Config.defconfpath;
4889 exit 0), " Print version and exit");
4891 ("-css", Arg.String (fun s -> csspath := Some s),
4892 "<path> Set path to the style sheet to use with EPUB/HTML");
4894 ("-origin", Arg.String (fun s -> state.origin <- s),
4895 "<origin> <undocumented>");
4897 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4898 ("-layout-height", Arg.Set_int layouth,
4899 "<height> layout height html/epub/etc (-1, 0, N)");
4902 (fun s -> state.path <- s)
4903 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4905 let histmode = emptystr state.path && not !openlast in
4907 if not (Config.load !openlast)
4908 then dolog "failed to load configuration";
4910 begin match !pageno with
4911 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4912 | None -> ()
4913 end;
4915 fillhelp ();
4916 if !gcconfig
4917 then (
4918 Config.gc ();
4919 exit 0
4922 let mu =
4923 object (self)
4924 val mutable m_clicks = 0
4925 val mutable m_click_x = 0
4926 val mutable m_click_y = 0
4927 val mutable m_lastclicktime = infinity
4929 method private cleanup =
4930 state.roam <- noroam;
4931 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4932 method expose = postRedisplay "expose"
4933 method visible v =
4934 let name =
4935 match v with
4936 | Wsi.Unobscured -> "unobscured"
4937 | Wsi.PartiallyObscured -> "partiallyobscured"
4938 | Wsi.FullyObscured -> "fullyobscured"
4940 vlog "visibility change %s" name
4941 method display = display ()
4942 method map mapped = vlog "mapped %b" mapped
4943 method reshape w h =
4944 self#cleanup;
4945 reshape w h
4946 method mouse b d x y m =
4947 if d && canselect ()
4948 then (
4950 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4952 m_click_x <- x;
4953 m_click_y <- y;
4954 if b = 1
4955 then (
4956 let t = now () in
4957 if abs x - m_click_x > 10
4958 || abs y - m_click_y > 10
4959 || abs_float (t -. m_lastclicktime) > 0.3
4960 then m_clicks <- 0;
4961 m_clicks <- m_clicks + 1;
4962 m_lastclicktime <- t;
4963 if m_clicks = 1
4964 then (
4965 self#cleanup;
4966 postRedisplay "cleanup";
4967 state.uioh <- state.uioh#button b d x y m;
4969 else state.uioh <- state.uioh#multiclick m_clicks x y m
4971 else (
4972 self#cleanup;
4973 m_clicks <- 0;
4974 m_lastclicktime <- infinity;
4975 state.uioh <- state.uioh#button b d x y m
4978 else state.uioh <- state.uioh#button b d x y m
4979 method motion x y =
4980 state.mpos <- (x, y);
4981 state.uioh <- state.uioh#motion x y
4982 method pmotion x y =
4983 state.mpos <- (x, y);
4984 state.uioh <- state.uioh#pmotion x y
4985 method key k m =
4986 vlog "k=%#x m=%#x" k m;
4987 let mascm = m land (
4988 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
4989 ) in
4990 let keyboard k m =
4991 let x = state.x and y = state.y in
4992 keyboard k m;
4993 if x != state.x || y != state.y then self#cleanup
4995 match state.keystate with
4996 | KSnone ->
4997 let km = k, mascm in
4998 begin
4999 match
5000 let modehash = state.uioh#modehash in
5001 try Hashtbl.find modehash km
5002 with Not_found ->
5003 try Hashtbl.find (findkeyhash conf "global") km
5004 with Not_found -> KMinsrt (k, m)
5005 with
5006 | KMinsrt (k, m) -> keyboard k m
5007 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
5008 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
5010 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
5011 List.iter (fun (k, m) -> keyboard k m) insrt;
5012 state.keystate <- KSnone
5013 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
5014 state.keystate <- KSinto (keys, insrt)
5015 | KSinto _ -> state.keystate <- KSnone
5017 method enter x y =
5018 state.mpos <- (x, y);
5019 state.uioh <- state.uioh#pmotion x y
5020 method leave = state.mpos <- (-1, -1)
5021 method winstate wsl = state.winstate <- wsl
5022 method quit : 'a. 'a = raise Quit
5023 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5024 method zoom z x y = state.uioh#zoom z x y
5025 method opendoc path =
5026 state.mode <- View;
5027 state.uioh <- uioh;
5028 postRedisplay "opendoc";
5029 opendoc path state.password
5032 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5033 state.wsfd <- wsfd;
5035 if not @@ List.exists GlMisc.check_extension
5036 [ "GL_ARB_texture_rectangle"
5037 ; "GL_EXT_texture_recangle"
5038 ; "GL_NV_texture_rectangle" ]
5039 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5041 let cs, ss =
5042 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5043 | exception exn ->
5044 dolog "socketpair failed: %s" @@ exntos exn;
5045 exit 1
5046 | (r, w) ->
5047 cloexec r;
5048 cloexec w;
5049 r, w
5052 setcheckers conf.checkers;
5053 begin match !csspath with
5054 | None -> ()
5055 | Some "" -> conf.css <- E.s
5056 | Some path ->
5057 let css = filecontents path in
5058 let l = String.length css in
5059 conf.css <-
5060 if substratis css (l-2) "\r\n"
5061 then String.sub css 0 (l-2)
5062 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
5063 end;
5064 init cs (
5065 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5066 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5067 !Config.fontpath, !trimcachepath
5069 List.iter GlArray.enable [`texture_coord; `vertex];
5070 GlTex.env (`color conf.texturecolor);
5071 state.ss <- ss;
5072 reshape ~firsttime:true winw winh;
5073 state.uioh <- uioh;
5074 if histmode
5075 then (
5076 Wsi.settitle "llpp (history)";
5077 enterhistmode ();
5079 else (
5080 state.text <- "Opening " ^ (mbtoutf8 state.path);
5081 opendoc state.path state.password;
5083 display ();
5084 Wsi.mapwin ();
5085 Wsi.setcursor Wsi.CURSOR_INHERIT;
5086 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5088 let rec reap () =
5089 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5090 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5091 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5092 | 0, _ -> ()
5093 | _pid, _status -> reap ()
5095 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5097 let optrfd =
5098 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5101 let rec loop deadline =
5102 if !doreap
5103 then (
5104 doreap := false;
5105 reap ()
5107 let r = [state.ss; state.wsfd] in
5108 let r =
5109 match !optrfd with
5110 | None -> r
5111 | Some fd -> fd :: r
5113 if !redisplay
5114 then (
5115 Glutils.redisplay := false;
5116 display ();
5118 let timeout =
5119 let now = now () in
5120 if deadline > now
5121 then (
5122 if deadline = infinity
5123 then ~-.1.0
5124 else max 0.0 (deadline -. now)
5126 else 0.0
5128 let r, _, _ =
5129 try Unix.select r [] [] timeout
5130 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5132 begin match r with
5133 | [] ->
5134 let newdeadline =
5135 match state.autoscroll with
5136 | Some step when step != 0 ->
5137 if state.slideshow land 1 = 1
5138 then (
5139 if state.slideshow land 2 = 0
5140 then state.slideshow <- state.slideshow lor 2
5141 else if step < 0 then prevpage () else nextpage ();
5142 deadline +. (float (abs step))
5144 else
5145 let y = state.y + step in
5146 let fy = if conf.maxhfit then state.winh else 0 in
5147 let y =
5148 if y < 0
5149 then state.maxy - fy
5150 else if y >= state.maxy - fy then 0 else y
5152 gotoxy state.x y;
5153 deadline +. 0.01
5154 | _ -> infinity
5156 loop newdeadline
5158 | l ->
5159 let rec checkfds = function
5160 | [] -> ()
5161 | fd :: rest when fd = state.ss ->
5162 let cmd = rcmd state.ss in
5163 act cmd;
5164 checkfds rest
5166 | fd :: rest when fd = state.wsfd ->
5167 Wsi.readresp fd;
5168 checkfds rest
5170 | fd :: rest when Some fd = !optrfd ->
5171 begin match remote fd with
5172 | None -> optrfd := remoteopen !rcmdpath;
5173 | opt -> optrfd := opt
5174 end;
5175 checkfds rest
5177 | _ :: rest ->
5178 dolog "select returned unknown descriptor";
5179 checkfds rest
5181 checkfds l;
5182 let newdeadline =
5183 let deadline1 =
5184 if deadline = infinity
5185 then now () +. 0.01
5186 else deadline
5188 match state.autoscroll with
5189 | Some step when step != 0 -> deadline1
5190 | _ -> infinity
5192 loop newdeadline
5193 end;
5195 match loop infinity with
5196 | exception Quit ->
5197 Config.save leavebirdseye;
5198 if hasunsavedchanges ()
5199 then save ()
5200 | _ -> error "umpossible - infinity reached"