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