Fix selstring
[llpp.git] / main.ml
blobc30fbf7f11d37d6546a1d2f5473095612dac55d5
1 open Utils;;
2 open Config;;
4 exception Quit;;
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 copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess : opaque -> int -> int -> int -> (int * string * int)
19 -> int = "ml_postprocess";;
20 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
21 external setaalevel : int -> unit = "ml_setaalevel";;
22 external realloctexts : int -> bool = "ml_realloctexts";;
23 external findlink : opaque -> linkdir -> link = "ml_findlink";;
24 external getlink : opaque -> int -> under = "ml_getlink";;
25 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
26 external getlinkcount : opaque -> int = "ml_getlinkcount";;
27 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
28 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
29 external freepbo : opaque -> unit = "ml_freepbo";;
30 external unmappbo : opaque -> unit = "ml_unmappbo";;
31 external bousable : unit -> bool = "ml_bo_usable";;
32 external unproject : opaque -> int -> int
33 -> (int * int) option = "ml_unproject";;
34 external project : opaque -> int -> int -> float -> float
35 -> (float * float) = "ml_project";;
36 external drawtile : tileparams -> opaque
37 -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int
39 -> float array option = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex
48 -> string = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array
50 -> unit = "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external uritolocation : string
54 -> (pageno * float * float) = "ml_uritolocation";;
55 external isexternallink : string -> bool = "ml_isexternallink";;
57 let selfexec = ref E.s;;
58 let opengl_has_pbo = ref false;;
60 let drawstring size x y s =
61 Gl.enable `blend;
62 Gl.enable `texture_2d;
63 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
64 ignore (drawstr size x y s);
65 Gl.disable `blend;
66 Gl.disable `texture_2d;
69 let drawstring1 size x y s =
70 drawstr size x y s;
73 let drawstring2 size x y fmt =
74 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
77 module UniSyms = struct
78 let ellipsis = "\xe2\x80\xa6";;
79 let radical = "\xe2\x88\x9a";;
80 let lguillemet = "\xc2\xab";;
81 let rguillemet = "\xc2\xbb";;
82 end;;
84 let _debugl l =
85 dolog {|l %d dim=%d {
86 WxH %dx%d
87 vWxH %dx%d
88 pagex,y %d,%d
89 dispx,y %d,%d
90 column %d
91 }|}
92 l.pageno l.pagedimno
93 l.pagew l.pageh
94 l.pagevw l.pagevh
95 l.pagex l.pagey
96 l.pagedispx l.pagedispy
97 l.pagecol
100 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
101 dolog {|rect {
102 x0,y0=(% f, % f)
103 x1,y1=(% f, % f)
104 x2,y2=(% f, % f)
105 x3,y3=(% f, % f)
106 }|} x0 y0 x1 y1 x2 y2 x3 y3;
109 let isbirdseye = function
110 | Birdseye _ -> true
111 | Textentry _ | View | LinkNav _ -> false
114 let istextentry = function
115 | Textentry _ -> true
116 | Birdseye _ | View | LinkNav _ -> false
119 let wtmode = ref false;;
120 let cxack = ref false;;
122 let pgscale h = truncate (float h *. conf.pgscale);;
124 let hscrollh () =
125 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
126 && (state.w > state.winw))
127 then conf.scrollbw
128 else 0
131 let vscrollw () =
132 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
133 && (state.maxy > state.winh))
134 then conf.scrollbw
135 else 0
138 let vscrollhit x =
139 if conf.leftscroll
140 then x < vscrollw ()
141 else x > state.winw - vscrollw ()
144 let setfontsize n =
145 fstate.fontsize <- n;
146 fstate.wwidth <- measurestr fstate.fontsize "w";
147 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
150 let vlog fmt =
151 if conf.verbose
152 then dolog fmt
153 else Printf.kprintf ignore fmt
156 let launchpath () =
157 if emptystr conf.pathlauncher
158 then dolog "%s" state.path
159 else (
160 let command = Str.global_replace percentsre state.path conf.pathlauncher in
161 match spawn command [] with
162 | _pid -> ()
163 | exception exn ->
164 dolog "failed to execute `%s': %s" command @@ exntos exn
168 module G =
169 struct
170 let postRedisplay who =
171 vlog "redisplay for [%S]" who;
172 state.redisplay <- true;
174 end;;
176 let getopaque pageno =
177 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
178 with Not_found -> None
181 let pagetranslatepoint l x y =
182 let dy = y - l.pagedispy in
183 let y = dy + l.pagey in
184 let dx = x - l.pagedispx in
185 let x = dx + l.pagex in
186 (x, y);
189 let onppundermouse g x y d =
190 let rec f = function
191 | l :: rest ->
192 begin match getopaque l.pageno with
193 | Some opaque ->
194 let x0 = l.pagedispx in
195 let x1 = x0 + l.pagevw in
196 let y0 = l.pagedispy in
197 let y1 = y0 + l.pagevh in
198 if y >= y0 && y <= y1 && x >= x0 && x <= x1
199 then
200 let px, py = pagetranslatepoint l x y in
201 match g opaque l px py with
202 | Some res -> res
203 | None -> f rest
204 else f rest
205 | _ ->
206 f rest
208 | [] -> d
210 f state.layout
213 let getunder x y =
214 let g opaque l px py =
215 if state.bzoom
216 then (
217 match rectofblock opaque px py with
218 | Some [|x0;x1;y0;y1|] ->
219 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
220 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
221 state.rects <- [l.pageno, color, rect];
222 G.postRedisplay "getunder";
223 | _ -> ()
225 let under = whatsunder opaque px py in
226 if under = Unone then None else Some under
228 onppundermouse g x y Unone
231 let unproject x y =
232 let g opaque l x y =
233 match unproject opaque x y with
234 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
235 | None -> None
237 onppundermouse g x y None;
240 let showtext c s =
241 state.text <- Printf.sprintf "%c%s" c s;
242 G.postRedisplay "showtext";
245 let impmsg fmt =
246 Format.ksprintf (fun s -> showtext '!' s) fmt;
249 let pipesel opaque cmd =
250 if hassel opaque
251 then
252 match Unix.pipe () with
253 | exception exn -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
254 | (r, w) ->
255 let doclose what fd =
256 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
258 begin match spawn cmd [r, 0; w, -1] with
259 | exception exn ->
260 doclose "pipesel pipe/w" w;
261 dolog "cannot execute %S: %s" cmd @@ exntos exn
262 | _pid ->
263 copysel w opaque;
264 G.postRedisplay "pipesel";
265 end;
266 doclose "pipesel pipe/r" r;
269 let paxunder x y =
270 let g opaque l px py =
271 if markunder opaque px py conf.paxmark
272 then (
273 Some (fun () ->
274 match getopaque l.pageno with
275 | None -> ()
276 | Some opaque -> pipesel opaque conf.paxcmd
279 else None
281 G.postRedisplay "paxunder";
282 if conf.paxmark = Mark_page
283 then
284 List.iter (fun l ->
285 match getopaque l.pageno with
286 | None -> ()
287 | Some opaque -> clearmark opaque) state.layout;
288 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
291 let selstring s =
292 match Unix.pipe () with
293 | exception exn -> impmsg "pipe failed: %s" @@ exntos exn
294 | (r, w) ->
295 let clo cap fd =
296 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
298 begin match spawn conf.selcmd [r, 0; w, -1] with
299 | exception exn ->
300 clo "selstring pipe/w" w;
301 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn
302 | _pid ->
304 let l = String.length s in
305 let bytes = Bytes.unsafe_of_string s in
306 let n = tempfailureretry (Unix.write w bytes 0) l in
307 if n != l
308 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n;
309 clo "selstring pipe/w" w;
310 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
311 end;
312 clo "selstring pipe/r" r;
315 let undertext = function
316 | Unone -> "none"
317 | Ulinkuri s -> s
318 | Utext s -> "font: " ^ s
319 | Uannotation (opaque, slinkindex) ->
320 "annotation: " ^ getannotcontents opaque slinkindex
323 let updateunder x y =
324 match getunder x y with
325 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
326 | Ulinkuri uri ->
327 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
328 Wsi.setcursor Wsi.CURSOR_INFO
329 | Utext s ->
330 if conf.underinfo then showtext 'f' ("ont: " ^ s);
331 Wsi.setcursor Wsi.CURSOR_TEXT
332 | Uannotation _ ->
333 if conf.underinfo then showtext 'a' "nnotation";
334 Wsi.setcursor Wsi.CURSOR_INFO
337 let showlinktype under =
338 if conf.underinfo && under != Unone
339 then showtext ' ' @@ undertext under
342 let [@warning "-4"] intentry_with_suffix text key =
343 let text =
344 match key with
345 | Keys.Ascii ('0'..'9' as c) -> addchar text c
346 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
347 addchar text @@ asciilower c
348 | _ ->
349 state.text <- Printf.sprintf "invalid key";
350 text
352 TEcont text
355 let wcmd fmt =
356 let b = Buffer.create 16 in
357 Printf.kbprintf
358 (fun b ->
359 let b = Buffer.to_bytes b in
360 wcmd state.ss b @@ Bytes.length b
361 ) b fmt
364 let nogeomcmds cmds =
365 match cmds with
366 | s, [] -> emptystr s
367 | _ -> false
370 let layoutN ((columns, coverA, coverB), b) x y sw sh =
371 let rec fold accu n =
372 if n = Array.length b
373 then accu
374 else
375 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
376 if (vy - y) > sh &&
377 (n = coverA - 1
378 || n = state.pagecount - coverB
379 || (n - coverA) mod columns = columns - 1)
380 then accu
381 else
382 let accu =
383 if vy + h > y
384 then
385 let pagey = max 0 (y - vy) in
386 let pagedispy = if pagey > 0 then 0 else vy - y in
387 let pagedispx, pagex =
388 let pdx =
389 if n = coverA - 1 || n = state.pagecount - coverB
390 then x + (sw - w) / 2
391 else dx + xoff + x
393 if pdx < 0
394 then 0, -pdx
395 else pdx, 0
397 let pagevw =
398 let vw = sw - pagedispx in
399 let pw = w - pagex in
400 min vw pw
402 let pagevh = min (h - pagey) (sh - pagedispy) in
403 if pagevw > 0 && pagevh > 0
404 then
405 let e =
406 { pageno = n
407 ; pagedimno = pdimno
408 ; pagew = w
409 ; pageh = h
410 ; pagex = pagex
411 ; pagey = pagey
412 ; pagevw = pagevw
413 ; pagevh = pagevh
414 ; pagedispx = pagedispx
415 ; pagedispy = pagedispy
416 ; pagecol = 0
419 e :: accu
420 else
421 accu
422 else
423 accu
425 fold accu (n+1)
427 if Array.length b = 0
428 then []
429 else List.rev (fold [] (page_of_y y))
432 let layoutS (columns, b) x y sw sh =
433 let rec fold accu n =
434 if n = Array.length b
435 then accu
436 else
437 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
438 if (vy - y) > sh
439 then accu
440 else
441 let accu =
442 if vy + pageh > y
443 then
444 let x = xoff + x in
445 let pagey = max 0 (y - vy) in
446 let pagedispy = if pagey > 0 then 0 else vy - y in
447 let pagedispx, pagex =
448 if px = 0
449 then (
450 if x < 0
451 then 0, -x
452 else x, 0
454 else (
455 let px = px - x in
456 if px < 0
457 then -px, 0
458 else 0, px
461 let pagecolw = pagew/columns in
462 let pagedispx =
463 if pagecolw < sw
464 then pagedispx + ((sw - pagecolw) / 2)
465 else pagedispx
467 let pagevw =
468 let vw = sw - pagedispx in
469 let pw = pagew - pagex in
470 min vw pw
472 let pagevw = min pagevw pagecolw in
473 let pagevh = min (pageh - pagey) (sh - pagedispy) in
474 if pagevw > 0 && pagevh > 0
475 then
476 let e =
477 { pageno = n/columns
478 ; pagedimno = pdimno
479 ; pagew = pagew
480 ; pageh = pageh
481 ; pagex = pagex
482 ; pagey = pagey
483 ; pagevw = pagevw
484 ; pagevh = pagevh
485 ; pagedispx = pagedispx
486 ; pagedispy = pagedispy
487 ; pagecol = n mod columns
490 e :: accu
491 else
492 accu
493 else
494 accu
496 fold accu (n+1)
498 List.rev (fold [] 0)
501 let layout x y sw sh =
502 if nogeomcmds state.geomcmds
503 then
504 match conf.columns with
505 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
506 | Cmulti c -> layoutN c x y sw sh
507 | Csplit s -> layoutS s x y sw sh
508 else []
511 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
513 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
515 let itertiles l f =
516 let tilex = l.pagex mod conf.tilew in
517 let tiley = l.pagey mod conf.tileh in
519 let col = l.pagex / conf.tilew in
520 let row = l.pagey / conf.tileh in
522 let rec rowloop row y0 dispy h =
523 if h = 0
524 then ()
525 else (
526 let dh = conf.tileh - y0 in
527 let dh = min h dh in
528 let rec colloop col x0 dispx w =
529 if w = 0
530 then ()
531 else (
532 let dw = conf.tilew - x0 in
533 let dw = min w dw in
534 f col row dispx dispy x0 y0 dw dh;
535 colloop (col+1) 0 (dispx+dw) (w-dw)
538 colloop col tilex l.pagedispx l.pagevw;
539 rowloop (row+1) 0 (dispy+dh) (h-dh)
542 if l.pagevw > 0 && l.pagevh > 0
543 then rowloop row tiley l.pagedispy l.pagevh;
546 let gettileopaque l col row =
547 let key =
548 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
550 try Some (Hashtbl.find state.tilemap key)
551 with Not_found -> None
554 let puttileopaque l col row gen colorspace angle opaque size elapsed =
555 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
556 Hashtbl.add state.tilemap key (opaque, size, elapsed)
559 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
560 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
561 GlArray.vertex `two state.vraw;
562 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
565 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
567 let filledrect x0 y0 x1 y1 =
568 GlArray.disable `texture_coord;
569 filledrect1 x0 y0 x1 y1;
570 GlArray.enable `texture_coord;
573 let linerect x0 y0 x1 y1 =
574 GlArray.disable `texture_coord;
575 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
576 GlArray.vertex `two state.vraw;
577 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
578 GlArray.enable `texture_coord;
581 let drawtiles l color =
582 GlDraw.color color;
583 begintiles ();
584 let f col row x y tilex tiley w h =
585 match gettileopaque l col row with
586 | Some (opaque, _, t) ->
587 let params = x, y, w, h, tilex, tiley in
588 if conf.invert
589 then GlTex.env (`mode `blend);
590 drawtile params opaque;
591 if conf.invert
592 then GlTex.env (`mode `modulate);
593 if conf.debug
594 then (
595 endtiles ();
596 let s = Printf.sprintf
597 "%d[%d,%d] %f sec"
598 l.pageno col row t
600 let w = measurestr fstate.fontsize s in
601 GlDraw.color (0.0, 0.0, 0.0);
602 filledrect (float (x-2))
603 (float (y-2))
604 (float (x+2) +. w)
605 (float (y + fstate.fontsize + 2));
606 GlDraw.color color;
607 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
608 begintiles ();
611 | None ->
612 endtiles ();
613 let w =
614 let lw = state.winw - x in
615 min lw w
616 and h =
617 let lh = state.winh - y in
618 min lh h
620 if conf.invert
621 then GlTex.env (`mode `blend);
622 begin match state.checkerstexid with
623 | Some id ->
624 Gl.enable `texture_2d;
625 GlTex.bind_texture ~target:`texture_2d id;
626 let x0 = float x
627 and y0 = float y
628 and x1 = float (x+w)
629 and y1 = float (y+h) in
631 let tw = float w /. 16.0
632 and th = float h /. 16.0 in
633 let tx0 = float tilex /. 16.0
634 and ty0 = float tiley /. 16.0 in
635 let tx1 = tx0 +. tw
636 and ty1 = ty0 +. th in
637 Raw.sets_float state.vraw ~pos:0
638 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
639 Raw.sets_float state.traw ~pos:0
640 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
641 GlArray.vertex `two state.vraw;
642 GlArray.tex_coord `two state.traw;
643 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
644 Gl.disable `texture_2d;
646 | None ->
647 GlDraw.color (1.0, 1.0, 1.0);
648 filledrect (float x) (float y) (float (x+w)) (float (y+h));
649 end;
650 if conf.invert
651 then GlTex.env (`mode `modulate);
652 if w > 128 && h > fstate.fontsize + 10
653 then (
654 let c = if conf.invert then 1.0 else 0.0 in
655 GlDraw.color (c, c, c);
656 let c, r =
657 if conf.verbose
658 then (col*conf.tilew, row*conf.tileh)
659 else col, row
661 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
663 GlDraw.color color;
664 begintiles ();
666 itertiles l f;
667 endtiles ();
670 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
672 let tilevisible1 l x y =
673 let ax0 = l.pagex
674 and ax1 = l.pagex + l.pagevw
675 and ay0 = l.pagey
676 and ay1 = l.pagey + l.pagevh in
678 let bx0 = x
679 and by0 = y in
680 let bx1 = min (bx0 + conf.tilew) l.pagew
681 and by1 = min (by0 + conf.tileh) l.pageh in
683 let rx0 = max ax0 bx0
684 and ry0 = max ay0 by0
685 and rx1 = min ax1 bx1
686 and ry1 = min ay1 by1 in
688 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
689 nonemptyintersection
692 let tilevisible layout n x y =
693 let rec findpageinlayout m = function
694 | l :: rest when l.pageno = n ->
695 tilevisible1 l x y || (
696 match conf.columns with
697 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
698 | Csplit _ | Csingle _ | Cmulti _ -> false
700 | _ :: rest -> findpageinlayout 0 rest
701 | [] -> false
703 findpageinlayout 0 layout;
706 let tileready l x y =
707 tilevisible1 l x y &&
708 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
711 let tilepage n p layout =
712 let rec loop = function
713 | l :: rest ->
714 if l.pageno = n
715 then
716 let f col row _ _ _ _ _ _ =
717 if state.currently = Idle
718 then
719 match gettileopaque l col row with
720 | Some _ -> ()
721 | None ->
722 let x = col*conf.tilew
723 and y = row*conf.tileh in
724 let w =
725 let w = l.pagew - x in
726 min w conf.tilew
728 let h =
729 let h = l.pageh - y in
730 min h conf.tileh
732 let pbo =
733 if conf.usepbo
734 then getpbo w h conf.colorspace
735 else ~< "0"
737 wcmd "tile %s %d %d %d %d %s"
738 (~> p) x y w h (~> pbo);
739 state.currently <-
740 Tiling (
741 l, p, conf.colorspace, conf.angle,
742 state.gen, col, row, conf.tilew, conf.tileh
745 itertiles l f;
746 else
747 loop rest
749 | [] -> ()
751 if nogeomcmds state.geomcmds
752 then loop layout;
755 let preloadlayout x y sw sh =
756 let y = if y < sh then 0 else y - sh in
757 let x = min 0 (x + sw) in
758 let h = sh*3 in
759 let w = sw*3 in
760 layout x y w h;
763 let load pages =
764 let rec loop pages =
765 if state.currently != Idle
766 then ()
767 else
768 match pages with
769 | l :: rest ->
770 begin match getopaque l.pageno with
771 | None ->
772 wcmd "page %d %d" l.pageno l.pagedimno;
773 state.currently <- Loading (l, state.gen);
774 | Some opaque ->
775 tilepage l.pageno opaque pages;
776 loop rest
777 end;
778 | _ -> ()
780 if nogeomcmds state.geomcmds
781 then loop pages
784 let preload pages =
785 load pages;
786 if conf.preload && state.currently = Idle
787 then load (preloadlayout state.x state.y state.winw state.winh);
790 let layoutready layout =
791 let rec fold all ls =
792 all && match ls with
793 | l :: rest ->
794 let seen = ref false in
795 let allvisible = ref true in
796 let foo col row _ _ _ _ _ _ =
797 seen := true;
798 allvisible := !allvisible &&
799 begin match gettileopaque l col row with
800 | Some _ -> true
801 | None -> false
804 itertiles l foo;
805 fold (!seen && !allvisible) rest
806 | [] -> true
808 let alltilesvisible = fold true layout in
809 alltilesvisible;
812 let gotoxy x y =
813 let y = bound y 0 state.maxy in
814 let y, layout, proceed =
815 match conf.maxwait with
816 | Some time when state.ghyll == noghyll ->
817 begin match state.throttle with
818 | None ->
819 let layout = layout x y state.winw state.winh in
820 let ready = layoutready layout in
821 if not ready
822 then (
823 load layout;
824 state.throttle <- Some (layout, y, now ());
826 else G.postRedisplay "gotoxy showall (None)";
827 y, layout, ready
828 | Some (_, _, started) ->
829 let dt = now () -. started in
830 if dt > time
831 then (
832 state.throttle <- None;
833 let layout = layout x y state.winw state.winh in
834 load layout;
835 G.postRedisplay "maxwait";
836 y, layout, true
838 else -1, [], false
841 | _ ->
842 let layout = layout x y state.winw state.winh in
843 if not !wtmode || layoutready layout
844 then G.postRedisplay "gotoxy ready";
845 y, layout, true
847 if proceed
848 then (
849 state.x <- x;
850 state.y <- y;
851 state.layout <- layout;
852 begin match state.mode with
853 | LinkNav ln ->
854 begin match ln with
855 | Ltexact (pageno, linkno) ->
856 let rec loop = function
857 | [] ->
858 state.lnava <- Some (pageno, linkno);
859 state.mode <- LinkNav (Ltgendir 0)
860 | l :: _ when l.pageno = pageno ->
861 begin match getopaque pageno with
862 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
863 | Some opaque ->
864 let x0, y0, x1, y1 = getlinkrect opaque linkno in
865 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
866 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
867 then state.mode <- LinkNav (Ltgendir 0)
869 | _ :: rest -> loop rest
871 loop layout
872 | Ltnotready _ | Ltgendir _ -> ()
874 | Birdseye _ | Textentry _ | View -> ()
875 end;
876 begin match state.mode with
877 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
878 if not (pagevisible layout pageno)
879 then (
880 match state.layout with
881 | [] -> ()
882 | l :: _ ->
883 state.mode <- Birdseye (
884 conf, leftx, l.pageno, hooverpageno, anchor
887 | LinkNav lt ->
888 begin match lt with
889 | Ltnotready (_, dir)
890 | Ltgendir dir ->
891 let linknav =
892 let rec loop = function
893 | [] -> lt
894 | l :: rest ->
895 match getopaque l.pageno with
896 | None -> Ltnotready (l.pageno, dir)
897 | Some opaque ->
898 let link =
899 let ld =
900 if dir = 0
901 then LDfirstvisible (l.pagex, l.pagey, dir)
902 else (
903 if dir > 0 then LDfirst else LDlast
906 findlink opaque ld
908 match link with
909 | Lnotfound -> loop rest
910 | Lfound n ->
911 showlinktype (getlink opaque n);
912 Ltexact (l.pageno, n)
914 loop state.layout
916 state.mode <- LinkNav linknav
917 | Ltexact _ -> ()
919 | Textentry _ | View -> ()
920 end;
921 preload layout;
923 state.ghyll <- noghyll;
924 if conf.updatecurs
925 then (
926 let mx, my = state.mpos in
927 updateunder mx my;
931 let conttiling pageno opaque =
932 tilepage pageno opaque
933 (if conf.preload
934 then preloadlayout state.x state.y state.winw state.winh
935 else state.layout)
938 let gotoxy_and_clear_text x y =
939 if not conf.verbose then state.text <- E.s;
940 gotoxy x y;
943 let getanchory (n, top, dtop) =
944 let y, h = getpageyh n in
945 if conf.presentation
946 then
947 let ips = calcips h in
948 y + truncate (top*.float h -. dtop*.float ips) + ips;
949 else
950 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
953 let gotoanchor anchor =
954 gotoxy state.x (getanchory anchor);
957 let addnav () =
958 cbput state.hists.nav (getanchor ());
961 let getnav dir =
962 let anchor = cbgetc state.hists.nav dir in
963 getanchory anchor;
966 let gotoghyll1 single y =
967 let scroll f n a b =
968 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
969 let snake f a b =
970 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
971 if f < a
972 then s (float f /. float a)
973 else (
974 if f > b
975 then 1.0 -. s ((float (f-b) /. float (n-b)))
976 else 1.0
979 snake f a b
980 and summa n a b =
981 let ins = float a *. 0.5
982 and outs = float (n-b) *. 0.5 in
983 let ones = b - a in
984 ins +. outs +. float ones
986 let rec set nab y sy =
987 let (_N, _A, _B), y =
988 if single
989 then
990 let scl = if y > sy then 2 else -2 in
991 let _N, _, _ = nab in
992 (_N,0,_N), y+conf.scrollstep*scl
993 else nab,y in
994 let sum = summa _N _A _B in
995 let dy = float (y - sy) in
996 state.ghyll <- (
997 let rec gf n y1 o =
998 if n >= _N
999 then state.ghyll <- noghyll
1000 else
1001 let go n =
1002 let s = scroll n _N _A _B in
1003 let y1 = y1 +. ((s *. dy) /. sum) in
1004 gotoxy_and_clear_text state.x (truncate y1);
1005 state.ghyll <- gf (n+1) y1;
1007 match o with
1008 | None -> go n
1009 | Some y' when single -> set nab y' state.y
1010 | Some y' -> set (_N/2, 1, 1) y' state.y
1012 gf 0 (float state.y)
1015 match conf.ghyllscroll with
1016 | Some nab when not conf.presentation ->
1017 if state.ghyll == noghyll
1018 then set nab y state.y
1019 else state.ghyll (Some y)
1020 | _ ->
1021 gotoxy_and_clear_text state.x y
1024 let gotoghyll = gotoghyll1 false;;
1026 let gotopage n top =
1027 let y, h = getpageyh n in
1028 let y = y + (truncate (top *. float h)) in
1029 gotoghyll y
1032 let gotopage1 n top =
1033 let y = getpagey n in
1034 let y = y + top in
1035 gotoghyll y
1038 let invalidate ?(redisplay=false) s f =
1039 state.redisplay <- redisplay;
1040 state.layout <- [];
1041 state.pdims <- [];
1042 state.rects <- [];
1043 state.rects1 <- [];
1044 match state.geomcmds with
1045 | ps, [] when emptystr ps ->
1046 f ();
1047 state.geomcmds <- s, [];
1049 | ps, [] ->
1050 state.geomcmds <- ps, [s, f];
1052 | ps, (s', _) :: rest when s' = s ->
1053 state.geomcmds <- ps, ((s, f) :: rest);
1055 | ps, cmds ->
1056 state.geomcmds <- ps, ((s, f) :: cmds);
1059 let flushpages () =
1060 Hashtbl.iter (fun _ opaque ->
1061 wcmd "freepage %s" (~> opaque);
1062 ) state.pagemap;
1063 Hashtbl.clear state.pagemap;
1066 let flushtiles () =
1067 if not (Queue.is_empty state.tilelru)
1068 then (
1069 Queue.iter (fun (k, p, s) ->
1070 wcmd "freetile %s" (~> p);
1071 state.memused <- state.memused - s;
1072 Hashtbl.remove state.tilemap k;
1073 ) state.tilelru;
1074 state.uioh#infochanged Memused;
1075 Queue.clear state.tilelru;
1077 load state.layout;
1080 let stateh h =
1081 let h = truncate (float h*.conf.zoom) in
1082 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1083 h - d
1086 let opendoc path password =
1087 state.path <- path;
1088 state.password <- password;
1089 state.gen <- state.gen + 1;
1090 state.docinfo <- [];
1091 state.outlines <- [||];
1093 flushpages ();
1094 setaalevel conf.aalevel;
1095 let titlepath =
1096 if emptystr state.origin
1097 then path
1098 else state.origin
1100 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1101 wcmd "open %d %d %d %s\000%s\000%s\000"
1102 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1103 path password conf.css;
1104 invalidate "reqlayout"
1105 (fun () ->
1106 wcmd "reqlayout %d %d %d %s\000"
1107 conf.angle (FMTE.to_int conf.fitmodel)
1108 (stateh state.winh) state.nameddest
1110 state.help <-
1111 let sl = keystostrlist conf in
1112 let rec loop accu =
1113 function | [] -> accu
1114 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1115 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1118 let reload () =
1119 state.anchor <- getanchor ();
1120 opendoc state.path state.password;
1123 let scalecolor c =
1124 let c = c *. conf.colorscale in
1125 (c, c, c);
1128 let scalecolor2 (r, g, b) =
1129 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1132 let docolumns columns =
1133 match columns with
1134 | Csingle _ ->
1135 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1136 let rec loop pageno pdimno pdim y ph pdims =
1137 if pageno = state.pagecount
1138 then ()
1139 else
1140 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1141 match pdims with
1142 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1143 pdimno+1, pdim, rest
1144 | _ ->
1145 pdimno, pdim, pdims
1147 let x = max 0 (((state.winw - w) / 2) - xoff) in
1148 let y =
1149 y + (if conf.presentation
1150 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1151 else (if pageno = 0 then 0 else conf.interpagespace)
1154 a.(pageno) <- (pdimno, x, y, pdim);
1155 loop (pageno+1) pdimno pdim (y + h) h pdims
1157 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1158 conf.columns <- Csingle a;
1160 | Cmulti ((columns, coverA, coverB), _) ->
1161 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1162 let rec loop pageno pdimno pdim x y rowh pdims =
1163 let rec fixrow m =
1164 if m = pageno then () else
1165 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1166 if h < rowh
1167 then (
1168 let y = y + (rowh - h) / 2 in
1169 a.(m) <- (pdimno, x, y, pdim);
1171 fixrow (m+1)
1173 if pageno = state.pagecount
1174 then fixrow (((pageno - 1) / columns) * columns)
1175 else
1176 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1177 match pdims with
1178 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1179 pdimno+1, pdim, rest
1180 | _ ->
1181 pdimno, pdim, pdims
1183 let x, y, rowh' =
1184 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1185 then (
1186 let x = (state.winw - w) / 2 in
1187 let ips =
1188 if conf.presentation then calcips h else conf.interpagespace in
1189 x, y + ips + rowh, h
1191 else (
1192 if (pageno - coverA) mod columns = 0
1193 then (
1194 let x = max 0 (state.winw - state.w) / 2 in
1195 let y =
1196 if conf.presentation
1197 then
1198 let ips = calcips h in
1199 y + (if pageno = 0 then 0 else calcips rowh + ips)
1200 else
1201 y + (if pageno = 0 then 0 else conf.interpagespace)
1203 x, y + rowh, h
1205 else x, y, max rowh h
1208 let y =
1209 if pageno > 1 && (pageno - coverA) mod columns = 0
1210 then (
1211 let y =
1212 if pageno = columns && conf.presentation
1213 then (
1214 let ips = calcips rowh in
1215 for i = 0 to pred columns
1217 let (pdimno, x, y, pdim) = a.(i) in
1218 a.(i) <- (pdimno, x, y+ips, pdim)
1219 done;
1220 y+ips;
1222 else y
1224 fixrow (pageno - columns);
1227 else y
1229 a.(pageno) <- (pdimno, x, y, pdim);
1230 let x = x + w + xoff*2 + conf.interpagespace in
1231 loop (pageno+1) pdimno pdim x y rowh' pdims
1233 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1234 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1236 | Csplit (c, _) ->
1237 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1238 let rec loop pageno pdimno pdim y pdims =
1239 if pageno = state.pagecount
1240 then ()
1241 else
1242 let pdimno, ((_, w, h, _) as pdim), pdims =
1243 match pdims with
1244 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1245 pdimno+1, pdim, rest
1246 | _ ->
1247 pdimno, pdim, pdims
1249 let cw = w / c in
1250 let rec loop1 n x y =
1251 if n = c then y else (
1252 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1253 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1256 let y = loop1 0 0 y in
1257 loop (pageno+1) pdimno pdim y pdims
1259 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1260 conf.columns <- Csplit (c, a);
1263 let represent () =
1264 docolumns conf.columns;
1265 state.maxy <- calcheight ();
1266 if state.reprf == noreprf
1267 then (
1268 match state.mode with
1269 | Birdseye (_, _, pageno, _, _) ->
1270 let y, h = getpageyh pageno in
1271 let top = (state.winh - h) / 2 in
1272 gotoxy state.x (max 0 (y - top))
1273 | Textentry _ | View | LinkNav _ ->
1274 let y = getanchory state.anchor in
1275 let y = min y (state.maxy - state.winh) in
1276 gotoxy state.x y;
1278 else (
1279 state.reprf ();
1280 state.reprf <- noreprf;
1284 let reshape ?(firsttime=false) w h =
1285 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1286 if not firsttime && nogeomcmds state.geomcmds
1287 then state.anchor <- getanchor ();
1289 state.winw <- w;
1290 let w = truncate (float w *. conf.zoom) in
1291 let w = max w 2 in
1292 state.winh <- h;
1293 setfontsize fstate.fontsize;
1294 GlMat.mode `modelview;
1295 GlMat.load_identity ();
1297 GlMat.mode `projection;
1298 GlMat.load_identity ();
1299 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1300 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1301 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1303 let relx =
1304 if conf.zoom <= 1.0
1305 then 0.0
1306 else float state.x /. float state.w
1308 invalidate ~redisplay:true "geometry"
1309 (fun () ->
1310 state.w <- w;
1311 if not firsttime
1312 then state.x <- truncate (relx *. float w);
1313 let w =
1314 match conf.columns with
1315 | Csingle _ -> w
1316 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1317 | Csplit (c, _) -> w * c
1319 wcmd "geometry %d %d %d"
1320 w (stateh h) (FMTE.to_int conf.fitmodel)
1324 let enttext () =
1325 let len = String.length state.text in
1326 let x0 = if conf.leftscroll then vscrollw () else 0 in
1327 let drawstring s =
1328 let hscrollh =
1329 match state.mode with
1330 | Textentry _ | View | LinkNav _ ->
1331 let h, _, _ = state.uioh#scrollpw in
1333 | Birdseye _ -> 0
1335 let rect x w =
1336 filledrect
1337 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1338 (x+.w) (float (state.winh - hscrollh))
1341 let w = float (state.winw - 1 - vscrollw ()) in
1342 if state.progress >= 0.0 && state.progress < 1.0
1343 then (
1344 GlDraw.color (0.3, 0.3, 0.3);
1345 let w1 = w *. state.progress in
1346 rect (float x0) w1;
1347 GlDraw.color (0.0, 0.0, 0.0);
1348 rect (float x0+.w1) (float x0+.w-.w1)
1350 else (
1351 GlDraw.color (0.0, 0.0, 0.0);
1352 rect (float x0) w;
1355 GlDraw.color (1.0, 1.0, 1.0);
1356 drawstring
1357 fstate.fontsize
1358 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1359 (state.winh - hscrollh - 5) s;
1361 let s =
1362 match state.mode with
1363 | Textentry ((prefix, text, _, _, _, _), _) ->
1364 let s =
1365 if len > 0
1366 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1367 else Printf.sprintf "%s%s_" prefix text
1371 | Birdseye _ | View | LinkNav _ -> state.text
1373 let s =
1374 if state.newerrmsgs
1375 then (
1376 if not (istextentry state.mode) && state.uioh#eformsgs
1377 then
1378 let s1 = "(press 'e' to review error messasges)" in
1379 if nonemptystr s then s ^ " " ^ s1 else s1
1380 else s
1382 else s
1384 if nonemptystr s
1385 then drawstring s
1388 let gctiles () =
1389 let len = Queue.length state.tilelru in
1390 let layout = lazy (
1391 match state.throttle with
1392 | None ->
1393 if conf.preload
1394 then preloadlayout state.x state.y state.winw state.winh
1395 else state.layout
1396 | Some (layout, _, _) ->
1397 layout
1398 ) in
1399 let rec loop qpos =
1400 if state.memused > conf.memlimit
1401 then (
1402 if qpos < len
1403 then
1404 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1405 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1406 let (_, pw, ph, _) = getpagedim n in
1407 if gen = state.gen
1408 && colorspace = conf.colorspace
1409 && angle = conf.angle
1410 && pagew = pw
1411 && pageh = ph
1412 && (
1413 let x = col*conf.tilew
1414 and y = row*conf.tileh in
1415 tilevisible (Lazy.force_val layout) n x y
1417 then Queue.push lruitem state.tilelru
1418 else (
1419 freepbo p;
1420 wcmd "freetile %s" (~> p);
1421 state.memused <- state.memused - s;
1422 state.uioh#infochanged Memused;
1423 Hashtbl.remove state.tilemap k;
1425 loop (qpos+1)
1428 loop 0
1431 let onpagerect pageno f =
1432 let b =
1433 match conf.columns with
1434 | Cmulti (_, b) -> b
1435 | Csingle b -> b
1436 | Csplit (_, b) -> b
1438 if pageno >= 0 && pageno < Array.length b
1439 then
1440 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1441 f w h
1444 let gotopagexy1 wtmode pageno x y =
1445 let _,w1,h1,leftx = getpagedim pageno in
1446 let top = y /. (float h1) in
1447 let left = x /. (float w1) in
1448 let py, w, h = getpageywh pageno in
1449 let wh = state.winh in
1450 let x = left *. (float w) in
1451 let x = leftx + state.x + truncate x in
1452 let sx =
1453 if x < 0 || x >= state.winw
1454 then state.x - x
1455 else state.x
1457 let pdy = truncate (top *. float h) in
1458 let y' = py + pdy in
1459 let dy = y' - state.y in
1460 let sy =
1461 if x != state.x || not (dy > 0 && dy < wh)
1462 then (
1463 if conf.presentation
1464 then
1465 if abs (py - y') > wh
1466 then y'
1467 else py
1468 else y';
1470 else state.y
1472 if state.x != sx || state.y != sy
1473 then (
1474 let x, y =
1475 if wtmode
1476 then (
1477 let ww = state.winw in
1478 let qx = sx / ww
1479 and qy = pdy / wh in
1480 let x = qx * ww
1481 and y = py + qy * wh in
1482 let x = if -x + ww > w1 then -(w1-ww) else x
1483 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1484 let y =
1485 if conf.presentation
1486 then
1487 if abs (py - y') > wh
1488 then y'
1489 else py
1490 else y';
1492 (x, y)
1494 else (sx, sy)
1496 gotoxy_and_clear_text x y;
1498 else gotoxy_and_clear_text state.x state.y;
1501 let gotopagexy wtmode pageno x y =
1502 match state.mode with
1503 | Birdseye _ -> gotopage pageno 0.0
1504 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1507 let getpassword () =
1508 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1509 if emptystr passcmd
1510 then E.s
1511 else getcmdoutput
1512 (fun s ->
1513 impmsg "error getting password: %s" s;
1514 dolog "%s" s) passcmd;
1517 let pgoto opaque pageno x y =
1518 let pdimno = getpdimno pageno in
1519 let x, y = project opaque pageno pdimno x y in
1520 gotopagexy false pageno x y;
1523 let act cmds =
1524 (* dolog "%S" cmds; *)
1525 let spl = splitatchar cmds ' ' in
1526 let scan s fmt f =
1527 try Scanf.sscanf s fmt f
1528 with exn ->
1529 dolog "error processing '%S': %s" cmds @@ exntos exn;
1530 exit 1
1532 let addoutline outline =
1533 match state.currently with
1534 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1535 | Idle -> state.currently <- Outlining [outline]
1536 | Loading _ | Tiling _ ->
1537 dolog "invalid outlining state";
1538 logcurrently state.currently
1540 match spl with
1541 | "clear", "" ->
1542 state.pdims <- [];
1543 state.uioh#infochanged Pdim;
1545 | "clearrects", "" ->
1546 state.rects <- state.rects1;
1547 G.postRedisplay "clearrects";
1549 | "continue", args ->
1550 let n = scan args "%u" (fun n -> n) in
1551 state.pagecount <- n;
1552 begin match state.currently with
1553 | Outlining l ->
1554 state.currently <- Idle;
1555 state.outlines <- Array.of_list (List.rev l)
1556 | Idle | Loading _ | Tiling _ -> ()
1557 end;
1559 let cur, cmds = state.geomcmds in
1560 if emptystr cur
1561 then failwith "umpossible";
1563 begin match List.rev cmds with
1564 | [] ->
1565 state.geomcmds <- E.s, [];
1566 state.throttle <- None;
1567 represent ();
1568 | (s, f) :: rest ->
1569 f ();
1570 state.geomcmds <- s, List.rev rest;
1571 end;
1572 if conf.maxwait = None && not !wtmode
1573 then G.postRedisplay "continue";
1575 | "msg", args ->
1576 showtext ' ' args
1578 | "vmsg", args ->
1579 if conf.verbose
1580 then showtext ' ' args
1582 | "emsg", args ->
1583 Buffer.add_string state.errmsgs args;
1584 state.newerrmsgs <- true;
1585 G.postRedisplay "error message"
1587 | "progress", args ->
1588 let progress, text =
1589 scan args "%f %n"
1590 (fun f pos ->
1591 f, String.sub args pos (String.length args - pos))
1593 state.text <- text;
1594 state.progress <- progress;
1595 G.postRedisplay "progress"
1597 | "firstmatch", args ->
1598 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1599 scan args "%u %d %f %f %f %f %f %f %f %f"
1600 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1601 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1603 let y = (getpagey pageno) + truncate y0 in
1604 let x =
1605 if conf.zoom > 1.0
1606 then state.winw/2
1607 else state.x
1609 addnav ();
1610 gotoxy x y;
1611 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1612 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1614 | "match", args ->
1615 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1616 scan args "%u %d %f %f %f %f %f %f %f %f"
1617 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1618 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1620 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1621 state.rects1 <-
1622 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1624 | "page", args ->
1625 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1626 let pageopaque = ~< pageopaques in
1627 begin match state.currently with
1628 | Loading (l, gen) ->
1629 vlog "page %d took %f sec" l.pageno t;
1630 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1631 begin match state.throttle with
1632 | None ->
1633 let preloadedpages =
1634 if conf.preload
1635 then preloadlayout state.x state.y state.winw state.winh
1636 else state.layout
1638 let evict () =
1639 let set =
1640 List.fold_left (fun s l -> IntSet.add l.pageno s)
1641 IntSet.empty preloadedpages
1643 let evictedpages =
1644 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1645 if not (IntSet.mem pageno set)
1646 then (
1647 wcmd "freepage %s" (~> opaque);
1648 key :: accu
1650 else accu
1651 ) state.pagemap []
1653 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1655 evict ();
1656 state.currently <- Idle;
1657 if gen = state.gen
1658 then (
1659 tilepage l.pageno pageopaque state.layout;
1660 load state.layout;
1661 load preloadedpages;
1662 let visible = pagevisible state.layout l.pageno in
1663 if visible
1664 then (
1665 match state.mode with
1666 | LinkNav (Ltnotready (pageno, dir)) ->
1667 if pageno = l.pageno
1668 then (
1669 let link =
1670 let ld =
1671 if dir = 0
1672 then LDfirstvisible (l.pagex, l.pagey, dir)
1673 else (
1674 if dir > 0 then LDfirst else LDlast
1677 findlink pageopaque ld
1679 match link with
1680 | Lnotfound -> ()
1681 | Lfound n ->
1682 showlinktype (getlink pageopaque n);
1683 state.mode <- LinkNav (Ltexact (l.pageno, n))
1685 | LinkNav (Ltgendir _)
1686 | LinkNav (Ltexact _)
1687 | View
1688 | Birdseye _
1689 | Textentry _ -> ()
1692 if visible && layoutready state.layout
1693 then (
1694 G.postRedisplay "page";
1698 | Some (layout, _, _) ->
1699 state.currently <- Idle;
1700 tilepage l.pageno pageopaque layout;
1701 load state.layout
1702 end;
1704 | Idle | Tiling _ | Outlining _ ->
1705 dolog "Inconsistent loading state";
1706 logcurrently state.currently;
1707 exit 1
1710 | "tile" , args ->
1711 let (x, y, opaques, size, t) =
1712 scan args "%u %u %s %u %f"
1713 (fun x y p size t -> (x, y, p, size, t))
1715 let opaque = ~< opaques in
1716 begin match state.currently with
1717 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1718 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1720 unmappbo opaque;
1721 if tilew != conf.tilew || tileh != conf.tileh
1722 then (
1723 wcmd "freetile %s" (~> opaque);
1724 state.currently <- Idle;
1725 load state.layout;
1727 else (
1728 puttileopaque l col row gen cs angle opaque size t;
1729 state.memused <- state.memused + size;
1730 state.uioh#infochanged Memused;
1731 gctiles ();
1732 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1733 opaque, size) state.tilelru;
1735 let layout =
1736 match state.throttle with
1737 | None -> state.layout
1738 | Some (layout, _, _) -> layout
1741 state.currently <- Idle;
1742 if gen = state.gen
1743 && conf.colorspace = cs
1744 && conf.angle = angle
1745 && tilevisible layout l.pageno x y
1746 then conttiling l.pageno pageopaque;
1748 begin match state.throttle with
1749 | None ->
1750 preload state.layout;
1751 if gen = state.gen
1752 && conf.colorspace = cs
1753 && conf.angle = angle
1754 && tilevisible state.layout l.pageno x y
1755 && (not !wtmode || layoutready state.layout)
1756 then G.postRedisplay "tile nothrottle";
1758 | Some (layout, y, _) ->
1759 let ready = layoutready layout in
1760 if ready
1761 then (
1762 state.y <- y;
1763 state.layout <- layout;
1764 state.throttle <- None;
1765 G.postRedisplay "throttle";
1767 else load layout;
1768 end;
1771 | Idle | Loading _ | Outlining _ ->
1772 dolog "Inconsistent tiling state";
1773 logcurrently state.currently;
1774 exit 1
1777 | "pdim", args ->
1778 let (n, w, h, _) as pdim =
1779 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1781 let pdim =
1782 match conf.fitmodel with
1783 | FitWidth -> pdim
1784 | FitPage | FitProportional ->
1785 match conf.columns with
1786 | Csplit _ -> (n, w, h, 0)
1787 | Csingle _ | Cmulti _ -> pdim
1789 state.pdims <- pdim :: state.pdims;
1790 state.uioh#infochanged Pdim
1792 | "o", args ->
1793 let (l, n, t, h, pos) =
1794 scan args "%u %u %d %u %n"
1795 (fun l n t h pos -> l, n, t, h, pos)
1797 let s = String.sub args pos (String.length args - pos) in
1798 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1800 | "ou", args ->
1801 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1802 let s = String.sub args pos len in
1803 let pos2 = pos + len + 1 in
1804 let uri = String.sub args pos2 (String.length args - pos2) in
1805 addoutline (s, l, Ouri uri)
1807 | "on", args ->
1808 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1809 let s = String.sub args pos (String.length args - pos) in
1810 addoutline (s, l, Onone)
1812 | "a", args ->
1813 let (n, l, t) =
1814 scan args "%u %d %d" (fun n l t -> n, l, t)
1816 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1818 | "info", args ->
1819 let c, v = splitatchar args '\t' in
1820 let s =
1821 if nonemptystr v
1822 then
1823 if c = "Title"
1824 then (
1825 conf.title <- v;
1826 Wsi.settitle v;
1827 args
1829 else
1830 if let len = String.length c in
1831 len > 6 && ((String.sub c (len-4) 4) = "date")
1832 then (
1833 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1834 then
1835 let b = Buffer.create 10 in
1836 Printf.bprintf b "%s\t" c;
1837 let sub p l c =
1839 Buffer.add_substring b v p l;
1840 Buffer.add_char b c;
1841 with exn -> Buffer.add_string b @@ exntos exn
1843 sub 2 4 '/';
1844 sub 6 2 '/';
1845 sub 8 2 ' ';
1846 sub 10 2 ':';
1847 sub 12 2 ':';
1848 sub 14 2 ' ';
1849 Buffer.add_char b '[';
1850 Buffer.add_string b v;
1851 Buffer.add_char b ']';
1852 Buffer.contents b
1853 else args
1855 else args
1856 else args
1858 state.docinfo <- (1, s) :: state.docinfo
1860 | "infoend", "" ->
1861 state.docinfo <- List.rev state.docinfo;
1862 state.uioh#infochanged Docinfo
1864 | "pass", args ->
1865 if args = "fail"
1866 then Wsi.settitle "Wrong password";
1867 let password = getpassword () in
1868 if emptystr password
1869 then error "document is password protected"
1870 else opendoc state.path password
1872 | _ ->
1873 error "unknown cmd `%S'" cmds
1876 let onhist cb =
1877 let rc = cb.rc in
1878 let action = function
1879 | HCprev -> cbget cb ~-1
1880 | HCnext -> cbget cb 1
1881 | HCfirst -> cbget cb ~-(cb.rc)
1882 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1883 and cancel () = cb.rc <- rc
1884 in (action, cancel)
1887 let search pattern forward =
1888 match conf.columns with
1889 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1890 | Csingle _ | Cmulti _ ->
1891 if nonemptystr pattern
1892 then
1893 let pn, py =
1894 match state.layout with
1895 | [] -> 0, 0
1896 | l :: _ ->
1897 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1899 wcmd "search %d %d %d %d,%s\000"
1900 (btod conf.icase) pn py (btod forward) pattern;
1903 let [@warning "-4"] intentry text key =
1904 let text =
1905 match key with
1906 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1907 | _ ->
1908 state.text <- "invalid key";
1909 text
1911 TEcont text
1914 let linknact f s =
1915 if nonemptystr s
1916 then (
1917 let n =
1918 let l = String.length s in
1919 let rec loop pos n =
1920 if pos = l
1921 then n
1922 else
1923 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1924 loop (pos+1) (n*26 + m)
1925 in loop 0 0
1927 let rec loop n = function
1928 | [] -> ()
1929 | l :: rest ->
1930 match getopaque l.pageno with
1931 | None -> loop n rest
1932 | Some opaque ->
1933 let m = getlinkcount opaque in
1934 if n < m
1935 then (
1936 let under = getlink opaque n in
1937 f under
1939 else loop (n-m) rest
1941 loop n state.layout;
1945 let [@warning "-4"] linknentry text = function
1946 | Keys.Ascii c ->
1947 let text = addchar text c in
1948 linknact (fun under -> state.text <- undertext under) text;
1949 TEcont text
1950 | _ ->
1951 state.text <- Printf.sprintf "invalid key";
1952 TEcont text
1955 let [@warning "-4"] textentry text = function
1956 | Keys.Ascii c -> TEcont (addchar text c)
1957 | Keys.Code c -> TEcont (text ^ toutf8 c)
1958 | _ -> TEcont text
1961 let reqlayout angle fitmodel =
1962 match state.throttle with
1963 | None ->
1964 if nogeomcmds state.geomcmds
1965 then state.anchor <- getanchor ();
1966 conf.angle <- angle mod 360;
1967 if conf.angle != 0
1968 then (
1969 match state.mode with
1970 | LinkNav _ -> state.mode <- View
1971 | Birdseye _ | Textentry _ | View -> ()
1973 conf.fitmodel <- fitmodel;
1974 invalidate
1975 "reqlayout"
1976 (fun () ->
1977 wcmd "reqlayout %d %d %d"
1978 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1980 | _ -> ()
1983 let settrim trimmargins trimfuzz =
1984 if nogeomcmds state.geomcmds
1985 then state.anchor <- getanchor ();
1986 conf.trimmargins <- trimmargins;
1987 conf.trimfuzz <- trimfuzz;
1988 let x0, y0, x1, y1 = trimfuzz in
1989 invalidate
1990 "settrim" (fun () ->
1991 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1992 flushpages ();
1995 let setzoom zoom =
1996 match state.throttle with
1997 | None ->
1998 let zoom = max 0.0001 zoom in
1999 if zoom <> conf.zoom
2000 then (
2001 state.prevzoom <- (conf.zoom, state.x);
2002 conf.zoom <- zoom;
2003 reshape state.winw state.winh;
2004 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2007 | Some (layout, y, started) ->
2008 let time =
2009 match conf.maxwait with
2010 | None -> 0.0
2011 | Some t -> t
2013 let dt = now () -. started in
2014 if dt > time
2015 then (
2016 state.y <- y;
2017 load layout;
2021 let pivotzoom ?(vw=min state.w state.winw)
2022 ?(vh=min (state.maxy-state.y) state.winh)
2023 ?(x=vw/2) ?(y=vh/2) zoom =
2024 let w = float state.w /. zoom in
2025 let hw = w /. 2.0 in
2026 let ratio = float vh /. float vw in
2027 let hh = hw *. ratio in
2028 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2029 let y0 = float y -. hh in
2030 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2031 setzoom zoom;
2034 let pivotzoom ?vw ?vh ?x ?y zoom =
2035 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2038 let setcolumns mode columns coverA coverB =
2039 state.prevcolumns <- Some (conf.columns, conf.zoom);
2040 if columns < 0
2041 then (
2042 if isbirdseye mode
2043 then impmsg "split mode doesn't work in bird's eye"
2044 else (
2045 conf.columns <- Csplit (-columns, E.a);
2046 state.x <- 0;
2047 conf.zoom <- 1.0;
2050 else (
2051 if columns < 2
2052 then (
2053 conf.columns <- Csingle E.a;
2054 state.x <- 0;
2055 setzoom 1.0;
2057 else (
2058 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2059 conf.zoom <- 1.0;
2062 reshape state.winw state.winh;
2065 let resetmstate () =
2066 state.mstate <- Mnone;
2067 Wsi.setcursor Wsi.CURSOR_INHERIT;
2070 let enterbirdseye () =
2071 let zoom = float conf.thumbw /. float state.winw in
2072 let birdseyepageno =
2073 let cy = state.winh / 2 in
2074 let fold = function
2075 | [] -> 0
2076 | l :: rest ->
2077 let rec fold best = function
2078 | [] -> best.pageno
2079 | l :: rest ->
2080 let d = cy - (l.pagedispy + l.pagevh/2)
2081 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2082 if abs d < abs dbest
2083 then fold l rest
2084 else best.pageno
2085 in fold l rest
2087 fold state.layout
2089 state.mode <-
2090 Birdseye (
2091 { conf with zoom = conf.zoom },
2092 state.x, birdseyepageno, -1, getanchor ()
2094 resetmstate ();
2095 conf.zoom <- zoom;
2096 conf.presentation <- false;
2097 conf.interpagespace <- 10;
2098 conf.hlinks <- false;
2099 conf.fitmodel <- FitPage;
2100 state.x <- 0;
2101 conf.maxwait <- None;
2102 conf.columns <- (
2103 match conf.beyecolumns with
2104 | Some c ->
2105 conf.zoom <- 1.0;
2106 Cmulti ((c, 0, 0), E.a)
2107 | None -> Csingle E.a
2109 if conf.verbose
2110 then
2111 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2112 (100.0*.zoom)
2113 else
2114 state.text <- E.s
2116 reshape state.winw state.winh;
2119 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2120 state.mode <- View;
2121 conf.zoom <- c.zoom;
2122 conf.presentation <- c.presentation;
2123 conf.interpagespace <- c.interpagespace;
2124 conf.maxwait <- c.maxwait;
2125 conf.hlinks <- c.hlinks;
2126 conf.fitmodel <- c.fitmodel;
2127 conf.beyecolumns <- (
2128 match conf.columns with
2129 | Cmulti ((c, _, _), _) -> Some c
2130 | Csingle _ -> None
2131 | Csplit _ -> failwith "leaving bird's eye split mode"
2133 conf.columns <- (
2134 match c.columns with
2135 | Cmulti (c, _) -> Cmulti (c, E.a)
2136 | Csingle _ -> Csingle E.a
2137 | Csplit (c, _) -> Csplit (c, E.a)
2139 if conf.verbose
2140 then
2141 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2142 (100.0*.conf.zoom)
2144 reshape state.winw state.winh;
2145 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2146 state.x <- leftx;
2149 let togglebirdseye () =
2150 match state.mode with
2151 | Birdseye vals -> leavebirdseye vals true
2152 | View -> enterbirdseye ()
2153 | Textentry _ | LinkNav _ -> ()
2156 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2157 let pageno = max 0 (pageno - incr) in
2158 let rec loop = function
2159 | [] -> gotopage1 pageno 0
2160 | l :: _ when l.pageno = pageno ->
2161 if l.pagedispy >= 0 && l.pagey = 0
2162 then G.postRedisplay "upbirdseye"
2163 else gotopage1 pageno 0
2164 | _ :: rest -> loop rest
2166 loop state.layout;
2167 state.text <- E.s;
2168 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2171 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2172 let pageno = min (state.pagecount - 1) (pageno + incr) in
2173 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2174 let rec loop = function
2175 | [] ->
2176 let y, h = getpageyh pageno in
2177 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2178 gotoxy state.x (clamp dy)
2179 | l :: _ when l.pageno = pageno ->
2180 if l.pagevh != l.pageh
2181 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2182 else G.postRedisplay "downbirdseye"
2183 | _ :: rest -> loop rest
2185 loop state.layout;
2186 state.text <- E.s;
2189 let [@warning "-4"] optentry mode _ key =
2190 let btos b = if b then "on" else "off" in
2191 match key with
2192 | Keys.Ascii 's' ->
2193 let ondone s =
2194 try conf.scrollstep <- int_of_string s with exn ->
2195 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2197 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2199 | Keys.Ascii 'A' ->
2200 let ondone s =
2202 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2203 if state.autoscroll <> None
2204 then state.autoscroll <- Some conf.autoscrollstep
2205 with exn ->
2206 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2208 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2210 | Keys.Ascii 'C' ->
2211 let ondone s =
2213 let n, a, b = multicolumns_of_string s in
2214 setcolumns mode n a b;
2215 with exn ->
2216 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2218 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2220 | Keys.Ascii 'Z' ->
2221 let ondone s =
2223 let zoom = float (int_of_string s) /. 100.0 in
2224 pivotzoom zoom
2225 with exn ->
2226 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2228 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2230 | Keys.Ascii 't' ->
2231 let ondone s =
2233 conf.thumbw <- bound (int_of_string s) 2 4096;
2234 state.text <-
2235 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2236 begin match mode with
2237 | Birdseye beye ->
2238 leavebirdseye beye false;
2239 enterbirdseye ();
2240 | Textentry _ | View | LinkNav _ -> ();
2242 with exn ->
2243 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2245 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2247 | Keys.Ascii 'R' ->
2248 let ondone s =
2249 match int_of_string s with
2250 | angle -> reqlayout angle conf.fitmodel
2251 | exception exn ->
2252 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2254 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2256 | Keys.Ascii 'i' ->
2257 conf.icase <- not conf.icase;
2258 TEdone ("case insensitive search " ^ (btos conf.icase))
2260 | Keys.Ascii 'p' ->
2261 conf.preload <- not conf.preload;
2262 gotoxy state.x state.y;
2263 TEdone ("preload " ^ (btos conf.preload))
2265 | Keys.Ascii 'v' ->
2266 conf.verbose <- not conf.verbose;
2267 TEdone ("verbose " ^ (btos conf.verbose))
2269 | Keys.Ascii 'd' ->
2270 conf.debug <- not conf.debug;
2271 TEdone ("debug " ^ (btos conf.debug))
2273 | Keys.Ascii 'h' ->
2274 conf.maxhfit <- not conf.maxhfit;
2275 state.maxy <- calcheight ();
2276 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2278 | Keys.Ascii 'c' ->
2279 conf.crophack <- not conf.crophack;
2280 TEdone ("crophack " ^ btos conf.crophack)
2282 | Keys.Ascii 'a' ->
2283 let s =
2284 match conf.maxwait with
2285 | None ->
2286 conf.maxwait <- Some infinity;
2287 "always wait for page to complete"
2288 | Some _ ->
2289 conf.maxwait <- None;
2290 "show placeholder if page is not ready"
2292 TEdone s
2294 | Keys.Ascii 'f' ->
2295 conf.underinfo <- not conf.underinfo;
2296 TEdone ("underinfo " ^ btos conf.underinfo)
2298 | Keys.Ascii 'P' ->
2299 conf.savebmarks <- not conf.savebmarks;
2300 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2302 | Keys.Ascii 'S' ->
2303 let ondone s =
2305 let pageno, py =
2306 match state.layout with
2307 | [] -> 0, 0
2308 | l :: _ ->
2309 l.pageno, l.pagey
2311 conf.interpagespace <- int_of_string s;
2312 docolumns conf.columns;
2313 state.maxy <- calcheight ();
2314 let y = getpagey pageno in
2315 gotoxy state.x (y + py)
2316 with exn ->
2317 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2319 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2321 | Keys.Ascii 'l' ->
2322 let fm =
2323 match conf.fitmodel with
2324 | FitProportional -> FitWidth
2325 | FitWidth | FitPage -> FitProportional
2327 reqlayout conf.angle fm;
2328 TEdone ("proportional display " ^ btos (fm == FitProportional))
2330 | Keys.Ascii 'T' ->
2331 settrim (not conf.trimmargins) conf.trimfuzz;
2332 TEdone ("trim margins " ^ btos conf.trimmargins)
2334 | Keys.Ascii 'I' ->
2335 conf.invert <- not conf.invert;
2336 TEdone ("invert colors " ^ btos conf.invert)
2338 | Keys.Ascii 'x' ->
2339 let ondone s =
2340 cbput state.hists.sel s;
2341 conf.selcmd <- s;
2343 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2344 textentry, ondone, true)
2346 | Keys.Ascii 'M' ->
2347 if conf.pax == None
2348 then conf.pax <- Some (ref (0.0, 0, 0))
2349 else conf.pax <- None;
2350 TEdone ("PAX " ^ btos (conf.pax != None))
2352 | (Keys.Ascii c) ->
2353 state.text <- Printf.sprintf "bad option %d `%c'"
2354 (Char.code c) c;
2355 TEstop
2357 | _ ->
2358 TEcont state.text
2361 class type lvsource =
2362 object
2363 method getitemcount : int
2364 method getitem : int -> (string * int)
2365 method hasaction : int -> bool
2366 method exit : uioh:uioh ->
2367 cancel:bool ->
2368 active:int ->
2369 first:int ->
2370 pan:int ->
2371 uioh option
2372 method getactive : int
2373 method getfirst : int
2374 method getpan : int
2375 method getminfo : (int * int) array
2376 end;;
2378 class virtual lvsourcebase = object
2379 val mutable m_active = 0
2380 val mutable m_first = 0
2381 val mutable m_pan = 0
2382 method getactive = m_active
2383 method getfirst = m_first
2384 method getpan = m_pan
2385 method getminfo : (int * int) array = E.a
2386 end;;
2388 let [@warning "-4"] textentrykeyboard
2389 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2390 state.text <- E.s;
2391 let enttext te =
2392 state.mode <- Textentry (te, onleave);
2393 enttext ();
2394 G.postRedisplay "textentrykeyboard enttext";
2396 let histaction cmd =
2397 match opthist with
2398 | None -> ()
2399 | Some (action, _) ->
2400 state.mode <-
2401 Textentry (
2402 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2404 G.postRedisplay "textentry histaction"
2406 let open Keys in let kt = Wsi.kc2kt key in
2407 match kt with
2408 | Backspace ->
2409 if emptystr text && cancelonempty
2410 then (
2411 onleave Cancel;
2412 G.postRedisplay "textentrykeyboard after cancel";
2414 else
2415 let s = withoutlastutf8 text in
2416 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2418 | Enter ->
2419 ondone text;
2420 onleave Confirm;
2421 G.postRedisplay "textentrykeyboard after confirm"
2423 | Up -> histaction HCprev
2424 | Down -> histaction HCnext
2425 | Home -> histaction HCfirst
2426 | End -> histaction HClast
2428 | Escape ->
2429 if emptystr text
2430 then (
2431 begin match opthist with
2432 | None -> ()
2433 | Some (_, onhistcancel) -> onhistcancel ()
2434 end;
2435 onleave Cancel;
2436 state.text <- E.s;
2437 G.postRedisplay "textentrykeyboard after cancel2"
2439 else (
2440 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2443 | Delete -> ()
2445 | Code _ | Ascii _ ->
2446 begin match onkey text kt with
2447 | TEdone text ->
2448 ondone text;
2449 onleave Confirm;
2450 G.postRedisplay "textentrykeyboard after confirm2";
2452 | TEcont text ->
2453 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2455 | TEstop ->
2456 onleave Cancel;
2457 G.postRedisplay "textentrykeyboard after cancel3"
2459 | TEswitch te ->
2460 state.mode <- Textentry (te, onleave);
2461 G.postRedisplay "textentrykeyboard switch";
2463 | _ -> vlog "unhandled key"
2466 let firstof first active =
2467 if first > active || abs (first - active) > fstate.maxrows - 1
2468 then max 0 (active - (fstate.maxrows/2))
2469 else first
2472 let calcfirst first active =
2473 if active > first
2474 then
2475 let rows = active - first in
2476 if rows > fstate.maxrows then active - fstate.maxrows else first
2477 else active
2480 let scrollph y maxy =
2481 let sh = float (maxy + state.winh) /. float state.winh in
2482 let sh = float state.winh /. sh in
2483 let sh = max sh (float conf.scrollh) in
2485 let percent = float y /. float maxy in
2486 let position = (float state.winh -. sh) *. percent in
2488 let position =
2489 if position +. sh > float state.winh
2490 then float state.winh -. sh
2491 else position
2493 position, sh;
2496 let adderrmsg src msg =
2497 Buffer.add_string state.errmsgs msg;
2498 state.newerrmsgs <- true;
2499 G.postRedisplay src
2502 let adderrfmt src fmt =
2503 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2506 let coe s = (s :> uioh);;
2508 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2509 object (self)
2510 val m_pan = source#getpan
2511 val m_first = source#getfirst
2512 val m_active = source#getactive
2513 val m_qsearch = E.s
2514 val m_prev_uioh = state.uioh
2516 method private elemunder y =
2517 if y < 0
2518 then None
2519 else
2520 let n = y / (fstate.fontsize+1) in
2521 if m_first + n < source#getitemcount
2522 then (
2523 if source#hasaction (m_first + n)
2524 then Some (m_first + n)
2525 else None
2527 else None
2529 method display =
2530 Gl.enable `blend;
2531 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2532 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2533 filledrect 0. 0. (float state.winw) (float state.winh);
2534 GlDraw.color (1., 1., 1.);
2535 Gl.enable `texture_2d;
2536 let fs = fstate.fontsize in
2537 let nfs = fs + 1 in
2538 let hw = state.winw/3 in
2539 let ww = fstate.wwidth in
2540 let tabw = 17.0*.ww in
2541 let itemcount = source#getitemcount in
2542 let minfo = source#getminfo in
2543 if conf.leftscroll
2544 then (
2545 GlMat.push ();
2546 GlMat.translate ~x:(float conf.scrollbw) ();
2548 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2549 let rec loop row =
2550 if (row - m_first) > fstate.maxrows
2551 then ()
2552 else (
2553 if row >= 0 && row < itemcount
2554 then (
2555 let (s, level) = source#getitem row in
2556 let y = (row - m_first) * nfs in
2557 let x = 5.0 +. (float (level + m_pan)) *. ww in
2558 if helpmode
2559 then GlDraw.color
2560 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2562 if row = m_active
2563 then (
2564 Gl.disable `texture_2d;
2565 let alpha = if source#hasaction row then 0.9 else 0.3 in
2566 GlDraw.color (1., 1., 1.) ~alpha;
2567 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2568 Gl.enable `texture_2d;
2570 let c =
2571 if zebra && row land 1 = 1
2572 then 0.8
2573 else 1.0
2575 GlDraw.color (c,c,c);
2576 let drawtabularstring s =
2577 let drawstr x s =
2578 let x' = truncate (x0 +. x) in
2579 let s1, s2 = splitatchar s '\000' in
2580 if emptystr s2
2581 then drawstring1 fs x' (y+nfs) s
2582 else
2583 let rec e s =
2584 if emptystr s
2585 then s
2586 else
2587 let s' = withoutlastutf8 s in
2588 let s = s' ^ UniSyms.ellipsis in
2589 let w = measurestr fs s in
2590 if float x' +. w +. ww < float (hw + x')
2591 then s
2592 else e s'
2594 let s1 =
2595 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2596 then e s1
2597 else s1
2599 ignore (drawstring1 fs x' (y+nfs) s1);
2600 drawstring1 fs (hw + x') (y+nfs) s2
2602 if trusted
2603 then
2604 let x = if helpmode && row > 0 then x +. ww else x in
2605 let s1, s2 = splitatchar s '\t' in
2606 if nonemptystr s2
2607 then
2608 let nx = drawstr x s1 in
2609 let sw = nx -. x in
2610 let x = x +. (max tabw sw) in
2611 drawstr x s2
2612 else
2613 let len = String.length s - 2 in
2614 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2615 then
2616 let s = String.sub s 2 len in
2617 let x = if not helpmode then x +. ww else x in
2618 GlDraw.color (1.2, 1.2, 1.2);
2619 let vinc = drawstring1 (fs+fs/4)
2620 (truncate (x -. ww)) (y+nfs) s in
2621 GlDraw.color (1., 1., 1.);
2622 vinc +. (float fs *. 0.8)
2623 else
2624 drawstr x s
2625 else
2626 drawstr x s
2628 ignore (drawtabularstring s);
2629 loop (row+1)
2633 loop m_first;
2634 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2635 let xadj = 5.0 in
2636 let rec loop row =
2637 if (row - m_first) <= fstate.maxrows
2638 then
2639 if row >= 0 && row < itemcount
2640 then (
2641 let (s, level) = source#getitem row in
2642 let pos0 = nindex s '\000' in
2643 let y = (row - m_first) * nfs in
2644 let x = float (level + m_pan) *. ww in
2645 let (first, last) = minfo.(row) in
2646 let prefix =
2647 if pos0 > 0 && first > pos0
2648 then String.sub s (pos0+1) (first-pos0-1)
2649 else String.sub s 0 first
2651 let suffix = String.sub s first (last - first) in
2652 let w1 = measurestr fstate.fontsize prefix in
2653 let w2 = measurestr fstate.fontsize suffix in
2654 let x = x +. if conf.leftscroll then xadj else 5.0 in
2655 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2656 let x0 = x +. w1
2657 and y0 = float (y+2) in
2658 let x1 = x0 +. w2
2659 and y1 = float (y+fs+3) in
2660 filledrect x0 y0 x1 y1;
2661 loop (row+1)
2664 Gl.disable `texture_2d;
2665 if Array.length minfo > 0 then loop m_first;
2666 Gl.disable `blend;
2667 if conf.leftscroll
2668 then GlMat.pop ()
2670 method updownlevel incr =
2671 let len = source#getitemcount in
2672 let curlevel =
2673 if m_active >= 0 && m_active < len
2674 then snd (source#getitem m_active)
2675 else -1
2677 let rec flow i =
2678 if i = len then i-1 else if i = -1 then 0 else
2679 let _, l = source#getitem i in
2680 if l != curlevel then i else flow (i+incr)
2682 let active = flow m_active in
2683 let first = calcfirst m_first active in
2684 G.postRedisplay "outline updownlevel";
2685 {< m_active = active; m_first = first >}
2687 method private key1 key mask =
2688 let set1 active first qsearch =
2689 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2691 let search active pattern incr =
2692 let active = if active = -1 then m_first else active in
2693 let dosearch re =
2694 let rec loop n =
2695 if n >= 0 && n < source#getitemcount
2696 then (
2697 let s, _ = source#getitem n in
2698 match Str.search_forward re s 0 with
2699 | exception Not_found -> loop (n + incr)
2700 | _ -> Some n
2702 else None
2704 loop active
2706 let qpat = Str.quote pattern in
2707 match Str.regexp_case_fold qpat with
2708 | s -> dosearch s
2709 | exception exn ->
2710 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2711 qpat @@ Printexc.to_string exn;
2712 None
2714 let itemcount = source#getitemcount in
2715 let find start incr =
2716 let rec find i =
2717 if i = -1 || i = itemcount
2718 then -1
2719 else (
2720 if source#hasaction i
2721 then i
2722 else find (i + incr)
2725 find start
2727 let set active first =
2728 let first = bound first 0 (itemcount - fstate.maxrows) in
2729 state.text <- E.s;
2730 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2732 let navigate incr =
2733 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2734 let active, first =
2735 let incr1 = if incr > 0 then 1 else -1 in
2736 if isvisible m_first m_active
2737 then
2738 let next =
2739 let next = m_active + incr in
2740 let next =
2741 if next < 0 || next >= itemcount
2742 then -1
2743 else find next incr1
2745 if abs (m_active - next) > fstate.maxrows
2746 then -1
2747 else next
2749 if next = -1
2750 then
2751 let first = m_first + incr in
2752 let first = bound first 0 (itemcount - fstate.maxrows) in
2753 let next =
2754 let next = m_active + incr in
2755 let next = bound next 0 (itemcount - 1) in
2756 find next ~-incr1
2758 let active =
2759 if next = -1
2760 then m_active
2761 else (
2762 if isvisible first next
2763 then next
2764 else m_active
2767 active, first
2768 else
2769 let first = min next m_first in
2770 let first =
2771 if abs (next - first) > fstate.maxrows
2772 then first + incr
2773 else first
2775 next, first
2776 else
2777 let first = m_first + incr in
2778 let first = bound first 0 (itemcount - 1) in
2779 let active =
2780 let next = m_active + incr in
2781 let next = bound next 0 (itemcount - 1) in
2782 let next = find next incr1 in
2783 let active =
2784 if next = -1 || abs (m_active - first) > fstate.maxrows
2785 then (
2786 let active = if m_active = -1 then next else m_active in
2787 active
2789 else next
2791 if isvisible first active
2792 then active
2793 else -1
2795 active, first
2797 G.postRedisplay "listview navigate";
2798 set active first;
2800 let open Keys in
2801 let kt = Wsi.kc2kt key in
2802 match [@warning "-4"] kt with
2803 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2804 let incr = if c = 'r' then -1 else 1 in
2805 let active, first =
2806 match search (m_active + incr) m_qsearch incr with
2807 | None ->
2808 state.text <- m_qsearch ^ " [not found]";
2809 m_active, m_first
2810 | Some active ->
2811 state.text <- m_qsearch;
2812 active, firstof m_first active
2814 G.postRedisplay "listview ctrl-r/s";
2815 set1 active first m_qsearch;
2817 | Insert when Wsi.withctrl mask ->
2818 if m_active >= 0 && m_active < source#getitemcount
2819 then (
2820 let s, _ = source#getitem m_active in
2821 selstring s;
2823 coe self
2825 | Backspace ->
2826 if emptystr m_qsearch
2827 then coe self
2828 else (
2829 let qsearch = withoutlastutf8 m_qsearch in
2830 if emptystr qsearch
2831 then (
2832 state.text <- E.s;
2833 G.postRedisplay "listview empty qsearch";
2834 set1 m_active m_first E.s;
2836 else
2837 let active, first =
2838 match search m_active qsearch ~-1 with
2839 | None ->
2840 state.text <- qsearch ^ " [not found]";
2841 m_active, m_first
2842 | Some active ->
2843 state.text <- qsearch;
2844 active, firstof m_first active
2846 G.postRedisplay "listview backspace qsearch";
2847 set1 active first qsearch
2850 | Ascii _ | Code _ ->
2851 let utf8 =
2852 match [@warning "-4"] kt with
2853 | Ascii c -> String.make 1 c
2854 | Code code -> toutf8 code
2855 | _ -> assert false
2857 let pattern = m_qsearch ^ utf8 in
2858 let active, first =
2859 match search m_active pattern 1 with
2860 | None ->
2861 state.text <- pattern ^ " [not found]";
2862 m_active, m_first
2863 | Some active ->
2864 state.text <- pattern;
2865 active, firstof m_first active
2867 G.postRedisplay "listview qsearch add";
2868 set1 active first pattern;
2870 | Escape ->
2871 state.text <- E.s;
2872 if emptystr m_qsearch
2873 then (
2874 G.postRedisplay "list view escape";
2875 let mx, my = state.mpos in
2876 updateunder mx my;
2877 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2878 ~first:m_first ~pan:m_pan with
2879 | None -> m_prev_uioh
2880 | Some uioh -> uioh
2882 else (
2883 G.postRedisplay "list view kill qsearch";
2884 coe {< m_qsearch = E.s >}
2887 | Enter ->
2888 state.text <- E.s;
2889 let self = {< m_qsearch = E.s >} in
2890 let opt =
2891 G.postRedisplay "listview enter";
2892 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2893 source#exit ~uioh:(coe self) ~cancel
2894 ~active:m_active ~first:m_first ~pan:m_pan;
2896 begin match opt with
2897 | None -> m_prev_uioh
2898 | Some uioh -> uioh
2901 | Delete ->
2902 coe self
2904 | Up -> navigate ~-1
2905 | Down -> navigate 1
2906 | Prior -> navigate ~-(fstate.maxrows)
2907 | Next -> navigate fstate.maxrows
2909 | Right ->
2910 state.text <- E.s;
2911 G.postRedisplay "listview right";
2912 coe {< m_pan = m_pan - 1 >}
2914 | Left ->
2915 state.text <- E.s;
2916 G.postRedisplay "listview left";
2917 coe {< m_pan = m_pan + 1 >}
2919 | Home ->
2920 let active = find 0 1 in
2921 G.postRedisplay "listview home";
2922 set active 0;
2924 | End ->
2925 let first = max 0 (itemcount - fstate.maxrows) in
2926 let active = find (itemcount - 1) ~-1 in
2927 G.postRedisplay "listview end";
2928 set active first;
2930 | _ -> coe self
2932 method key key mask =
2933 match state.mode with
2934 | Textentry te ->
2935 textentrykeyboard key mask te;
2936 coe self
2937 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2939 method button button down x y _ =
2940 let opt =
2941 match button with
2942 | 1 when vscrollhit x ->
2943 G.postRedisplay "listview scroll";
2944 if down
2945 then
2946 let _, position, sh = self#scrollph in
2947 if y > truncate position && y < truncate (position +. sh)
2948 then (
2949 state.mstate <- Mscrolly;
2950 Some (coe self)
2952 else
2953 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2954 let first = truncate (s *. float source#getitemcount) in
2955 let first = min source#getitemcount first in
2956 Some (coe {< m_first = first; m_active = first >})
2957 else (
2958 state.mstate <- Mnone;
2959 Some (coe self);
2961 | 1 when down ->
2962 begin match self#elemunder y with
2963 | Some n ->
2964 G.postRedisplay "listview click";
2965 source#exit ~uioh:(coe {< m_active = n >})
2966 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2967 | _ ->
2968 Some (coe self)
2970 | n when (n == 4 || n == 5) && not down ->
2971 let len = source#getitemcount in
2972 let first =
2973 if n = 5 && m_first + fstate.maxrows >= len
2974 then
2975 m_first
2976 else
2977 let first = m_first + (if n == 4 then -1 else 1) in
2978 bound first 0 (len - 1)
2980 G.postRedisplay "listview wheel";
2981 Some (coe {< m_first = first >})
2982 | n when (n = 6 || n = 7) && not down ->
2983 let inc = if n = 7 then -1 else 1 in
2984 G.postRedisplay "listview hwheel";
2985 Some (coe {< m_pan = m_pan + inc >})
2986 | _ ->
2987 Some (coe self)
2989 match opt with
2990 | None -> m_prev_uioh
2991 | Some uioh -> uioh
2993 method multiclick _ x y = self#button 1 true x y
2995 method motion _ y =
2996 match state.mstate with
2997 | Mscrolly ->
2998 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2999 let first = truncate (s *. float source#getitemcount) in
3000 let first = min source#getitemcount first in
3001 G.postRedisplay "listview motion";
3002 coe {< m_first = first; m_active = first >}
3003 | Msel _
3004 | Mpan _
3005 | Mscrollx
3006 | Mzoom _
3007 | Mzoomrect _
3008 | Mnone -> coe self
3010 method pmotion x y =
3011 if x < state.winw - conf.scrollbw
3012 then
3013 let n =
3014 match self#elemunder y with
3015 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3016 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3018 let o =
3019 if n != m_active
3020 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3021 else self
3023 coe o
3024 else (
3025 Wsi.setcursor Wsi.CURSOR_INHERIT;
3026 coe self
3029 method infochanged _ = ()
3031 method scrollpw = (0, 0.0, 0.0)
3032 method scrollph =
3033 let nfs = fstate.fontsize + 1 in
3034 let y = m_first * nfs in
3035 let itemcount = source#getitemcount in
3036 let maxi = max 0 (itemcount - fstate.maxrows) in
3037 let maxy = maxi * nfs in
3038 let p, h = scrollph y maxy in
3039 conf.scrollbw, p, h
3041 method modehash = modehash
3042 method eformsgs = false
3043 method alwaysscrolly = true
3044 end;;
3046 class outlinelistview ~zebra ~source =
3047 let settext autonarrow s =
3048 if autonarrow
3049 then
3050 let ss = source#statestr in
3051 state.text <-
3052 if emptystr ss
3053 then "[" ^ s ^ "]"
3054 else "{" ^ ss ^ "} [" ^ s ^ "]"
3055 else state.text <- s
3057 object (self)
3058 inherit listview
3059 ~zebra
3060 ~helpmode:false
3061 ~source:(source :> lvsource)
3062 ~trusted:false
3063 ~modehash:(findkeyhash conf "outline")
3064 as super
3066 val m_autonarrow = false
3068 method! key key mask =
3069 let maxrows =
3070 if emptystr state.text
3071 then fstate.maxrows
3072 else fstate.maxrows - 2
3074 let calcfirst first active =
3075 if active > first
3076 then
3077 let rows = active - first in
3078 if rows > maxrows then active - maxrows else first
3079 else active
3081 let navigate incr =
3082 let active = m_active + incr in
3083 let active = bound active 0 (source#getitemcount - 1) in
3084 let first = calcfirst m_first active in
3085 G.postRedisplay "outline navigate";
3086 coe {< m_active = active; m_first = first >}
3088 let navscroll first =
3089 let active =
3090 let dist = m_active - first in
3091 if dist < 0
3092 then first
3093 else (
3094 if dist < maxrows
3095 then m_active
3096 else first + maxrows
3099 G.postRedisplay "outline navscroll";
3100 coe {< m_first = first; m_active = active >}
3102 let ctrl = Wsi.withctrl mask in
3103 let open Keys in
3104 match Wsi.kc2kt key with
3105 | Ascii 'a' when ctrl ->
3106 let text =
3107 if m_autonarrow
3108 then (source#denarrow; E.s)
3109 else (
3110 let pattern = source#renarrow in
3111 if nonemptystr m_qsearch
3112 then (source#narrow m_qsearch; m_qsearch)
3113 else pattern
3116 settext (not m_autonarrow) text;
3117 G.postRedisplay "toggle auto narrowing";
3118 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3120 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3121 settext true E.s;
3122 G.postRedisplay "toggle auto narrowing";
3123 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3125 | Ascii 'n' when ctrl ->
3126 source#narrow m_qsearch;
3127 if not m_autonarrow
3128 then source#add_narrow_pattern m_qsearch;
3129 G.postRedisplay "outline ctrl-n";
3130 coe {< m_first = 0; m_active = 0 >}
3132 | Ascii 'S' when ctrl ->
3133 let active = source#calcactive (getanchor ()) in
3134 let first = firstof m_first active in
3135 G.postRedisplay "outline ctrl-s";
3136 coe {< m_first = first; m_active = active >}
3138 | Ascii 'u' when ctrl ->
3139 G.postRedisplay "outline ctrl-u";
3140 if m_autonarrow && nonemptystr m_qsearch
3141 then (
3142 ignore (source#renarrow);
3143 settext m_autonarrow E.s;
3144 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3146 else (
3147 source#del_narrow_pattern;
3148 let pattern = source#renarrow in
3149 let text =
3150 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3152 settext m_autonarrow text;
3153 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3156 | Ascii 'l' when ctrl ->
3157 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3158 G.postRedisplay "outline ctrl-l";
3159 coe {< m_first = first >}
3161 | Ascii '\t' when m_autonarrow ->
3162 if nonemptystr m_qsearch
3163 then (
3164 G.postRedisplay "outline list view tab";
3165 source#add_narrow_pattern m_qsearch;
3166 settext true E.s;
3167 coe {< m_qsearch = E.s >}
3169 else coe self
3171 | Escape when m_autonarrow ->
3172 if nonemptystr m_qsearch
3173 then source#add_narrow_pattern m_qsearch;
3174 super#key key mask
3176 | Enter when m_autonarrow ->
3177 if nonemptystr m_qsearch
3178 then source#add_narrow_pattern m_qsearch;
3179 super#key key mask
3181 | (Ascii _ | Code _) when m_autonarrow ->
3182 let pattern = m_qsearch ^ toutf8 key in
3183 G.postRedisplay "outlinelistview autonarrow add";
3184 source#narrow pattern;
3185 settext true pattern;
3186 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3188 | Backspace when m_autonarrow ->
3189 if emptystr m_qsearch
3190 then coe self
3191 else
3192 let pattern = withoutlastutf8 m_qsearch in
3193 G.postRedisplay "outlinelistview autonarrow backspace";
3194 ignore (source#renarrow);
3195 source#narrow pattern;
3196 settext true pattern;
3197 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3199 | Up when ctrl ->
3200 navscroll (max 0 (m_first - 1))
3202 | Down when ctrl ->
3203 navscroll (min (source#getitemcount - 1) (m_first + 1))
3205 | Up -> navigate ~-1
3206 | Down -> navigate 1
3207 | Prior -> navigate ~-(fstate.maxrows)
3208 | Next -> navigate fstate.maxrows
3210 | Right ->
3211 let o =
3212 if ctrl
3213 then (
3214 G.postRedisplay "outline ctrl right";
3215 {< m_pan = m_pan + 1 >}
3217 else self#updownlevel 1
3219 coe o
3221 | Left ->
3222 let o =
3223 if ctrl
3224 then (
3225 G.postRedisplay "outline ctrl left";
3226 {< m_pan = m_pan - 1 >}
3228 else self#updownlevel ~-1
3230 coe o
3232 | Home ->
3233 G.postRedisplay "outline home";
3234 coe {< m_first = 0; m_active = 0 >}
3236 | End ->
3237 let active = source#getitemcount - 1 in
3238 let first = max 0 (active - fstate.maxrows) in
3239 G.postRedisplay "outline end";
3240 coe {< m_active = active; m_first = first >}
3242 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3243 super#key key mask
3244 end;;
3246 let genhistoutlines () =
3247 Config.gethist ()
3248 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3249 compare c2.lastvisit c1.lastvisit)
3250 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3251 let path = if nonemptystr origin then origin else path in
3252 let base = mbtoutf8 @@ Filename.basename path in
3253 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3257 let gotohist (path, c, bookmarks, x, anchor, origin) =
3258 Config.save leavebirdseye;
3259 state.anchor <- anchor;
3260 state.bookmarks <- bookmarks;
3261 state.origin <- origin;
3262 state.x <- x;
3263 setconf conf c;
3264 let x0, y0, x1, y1 = conf.trimfuzz in
3265 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3266 reshape ~firsttime:true state.winw state.winh;
3267 opendoc path origin;
3268 setzoom c.zoom;
3271 let makecheckers () =
3272 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3273 following to say:
3274 converted by Issac Trotts. July 25, 2002 *)
3275 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3276 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3277 let id = GlTex.gen_texture () in
3278 GlTex.bind_texture ~target:`texture_2d id;
3279 GlPix.store (`unpack_alignment 1);
3280 GlTex.image2d image;
3281 List.iter (GlTex.parameter ~target:`texture_2d)
3282 [ `mag_filter `nearest; `min_filter `nearest ];
3286 let setcheckers enabled =
3287 match state.checkerstexid with
3288 | None ->
3289 if enabled then state.checkerstexid <- Some (makecheckers ())
3291 | Some checkerstexid ->
3292 if not enabled
3293 then (
3294 GlTex.delete_texture checkerstexid;
3295 state.checkerstexid <- None;
3299 let describe_location () =
3300 let fn = page_of_y state.y in
3301 let ln = page_of_y (state.y + state.winh - 1) in
3302 let maxy = maxy () in
3303 let percent =
3304 if maxy <= 0
3305 then 100.
3306 else 100. *. (float state.y /. float maxy)
3308 if fn = ln
3309 then
3310 Printf.sprintf "page %d of %d [%.2f%%]"
3311 (fn+1) state.pagecount percent
3312 else
3313 Printf.sprintf
3314 "pages %d-%d of %d [%.2f%%]"
3315 (fn+1) (ln+1) state.pagecount percent
3318 let setpresentationmode v =
3319 let n = page_of_y state.y in
3320 state.anchor <- (n, 0.0, 1.0);
3321 conf.presentation <- v;
3322 if conf.fitmodel = FitPage
3323 then reqlayout conf.angle conf.fitmodel;
3324 represent ();
3327 let setbgcol (r, g, b) =
3328 let col =
3329 let r = r *. 255.0 |> truncate
3330 and g = g *. 255.0 |> truncate
3331 and b = b *. 255.0 |> truncate in
3332 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3334 Wsi.setwinbgcol col;
3337 let enterinfomode =
3338 let btos b = if b then UniSyms.radical else E.s in
3339 let showextended = ref false in
3340 let leave mode _ = state.mode <- mode in
3341 let src =
3342 (object
3343 val mutable m_l = []
3344 val mutable m_a = E.a
3345 val mutable m_prev_uioh = nouioh
3346 val mutable m_prev_mode = View
3348 inherit lvsourcebase
3350 method reset prev_mode prev_uioh =
3351 m_a <- Array.of_list (List.rev m_l);
3352 m_l <- [];
3353 m_prev_mode <- prev_mode;
3354 m_prev_uioh <- prev_uioh;
3356 method int name get set =
3357 m_l <-
3358 (name, `int get, 1,
3359 Action (
3360 fun u ->
3361 let ondone s =
3362 try set (int_of_string s)
3363 with exn ->
3364 state.text <- Printf.sprintf "bad integer `%s': %s"
3365 s @@ exntos exn
3367 state.text <- E.s;
3368 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3369 state.mode <- Textentry (te, leave m_prev_mode);
3371 )) :: m_l
3373 method int_with_suffix name get set =
3374 m_l <-
3375 (name, `intws get, 1,
3376 Action (
3377 fun u ->
3378 let ondone s =
3379 try set (int_of_string_with_suffix s)
3380 with exn ->
3381 state.text <- Printf.sprintf "bad integer `%s': %s"
3382 s @@ exntos exn
3384 state.text <- E.s;
3385 let te =
3386 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3388 state.mode <- Textentry (te, leave m_prev_mode);
3390 )) :: m_l
3392 method bool ?(offset=1) ?(btos=btos) name get set =
3393 m_l <-
3394 (name, `bool (btos, get), offset, Action (
3395 fun u ->
3396 let v = get () in
3397 set (not v);
3399 )) :: m_l
3401 method color name get set =
3402 m_l <-
3403 (name, `color get, 1,
3404 Action (
3405 fun u ->
3406 let invalid = (nan, nan, nan) in
3407 let ondone s =
3408 let c =
3409 try color_of_string s
3410 with exn ->
3411 state.text <- Printf.sprintf "bad color `%s': %s"
3412 s @@ exntos exn;
3413 invalid
3415 if c <> invalid
3416 then set c;
3418 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3419 state.text <- color_to_string (get ());
3420 state.mode <- Textentry (te, leave m_prev_mode);
3422 )) :: m_l
3424 method string name get set =
3425 m_l <-
3426 (name, `string get, 1,
3427 Action (
3428 fun u ->
3429 let ondone s = set s in
3430 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3431 state.mode <- Textentry (te, leave m_prev_mode);
3433 )) :: m_l
3435 method colorspace name get set =
3436 m_l <-
3437 (name, `string get, 1,
3438 Action (
3439 fun _ ->
3440 let source =
3441 (object
3442 inherit lvsourcebase
3444 initializer
3445 m_active <- CSTE.to_int conf.colorspace;
3446 m_first <- 0;
3448 method getitemcount =
3449 Array.length CSTE.names
3450 method getitem n =
3451 (CSTE.names.(n), 0)
3452 method exit ~uioh ~cancel ~active ~first ~pan =
3453 ignore (uioh, first, pan);
3454 if not cancel then set active;
3455 None
3456 method hasaction _ = true
3457 end)
3459 state.text <- E.s;
3460 let modehash = findkeyhash conf "info" in
3461 coe (new listview ~zebra:false ~helpmode:false
3462 ~source ~trusted:true ~modehash)
3463 )) :: m_l
3465 method paxmark name get set =
3466 m_l <-
3467 (name, `string get, 1,
3468 Action (
3469 fun _ ->
3470 let source =
3471 (object
3472 inherit lvsourcebase
3474 initializer
3475 m_active <- MTE.to_int conf.paxmark;
3476 m_first <- 0;
3478 method getitemcount = Array.length MTE.names
3479 method getitem n = (MTE.names.(n), 0)
3480 method exit ~uioh ~cancel ~active ~first ~pan =
3481 ignore (uioh, first, pan);
3482 if not cancel then set active;
3483 None
3484 method hasaction _ = true
3485 end)
3487 state.text <- E.s;
3488 let modehash = findkeyhash conf "info" in
3489 coe (new listview ~zebra:false ~helpmode:false
3490 ~source ~trusted:true ~modehash)
3491 )) :: m_l
3493 method fitmodel name get set =
3494 m_l <-
3495 (name, `string get, 1,
3496 Action (
3497 fun _ ->
3498 let source =
3499 (object
3500 inherit lvsourcebase
3502 initializer
3503 m_active <- FMTE.to_int conf.fitmodel;
3504 m_first <- 0;
3506 method getitemcount = Array.length FMTE.names
3507 method getitem n = (FMTE.names.(n), 0)
3508 method exit ~uioh ~cancel ~active ~first ~pan =
3509 ignore (uioh, first, pan);
3510 if not cancel then set active;
3511 None
3512 method hasaction _ = true
3513 end)
3515 state.text <- E.s;
3516 let modehash = findkeyhash conf "info" in
3517 coe (new listview ~zebra:false ~helpmode:false
3518 ~source ~trusted:true ~modehash)
3519 )) :: m_l
3521 method caption s offset =
3522 m_l <- (s, `empty, offset, Noaction) :: m_l
3524 method caption2 s f offset =
3525 m_l <- (s, `string f, offset, Noaction) :: m_l
3527 method getitemcount = Array.length m_a
3529 method getitem n =
3530 let tostr = function
3531 | `int f -> string_of_int (f ())
3532 | `intws f -> string_with_suffix_of_int (f ())
3533 | `string f -> f ()
3534 | `color f -> color_to_string (f ())
3535 | `bool (btos, f) -> btos (f ())
3536 | `empty -> E.s
3538 let name, t, offset, _ = m_a.(n) in
3539 ((let s = tostr t in
3540 if nonemptystr s
3541 then Printf.sprintf "%s\t%s" name s
3542 else name),
3543 offset)
3545 method exit ~uioh ~cancel ~active ~first ~pan =
3546 let uiohopt =
3547 if not cancel
3548 then (
3549 let uioh =
3550 match m_a.(active) with
3551 | _, _, _, Action f -> f uioh
3552 | _, _, _, Noaction -> uioh
3554 Some uioh
3556 else None
3558 m_active <- active;
3559 m_first <- first;
3560 m_pan <- pan;
3561 uiohopt
3563 method hasaction n =
3564 match m_a.(n) with
3565 | _, _, _, Action _ -> true
3566 | _, _, _, Noaction -> false
3568 initializer m_active <- 1
3569 end)
3571 let rec fillsrc prevmode prevuioh =
3572 let sep () = src#caption E.s 0 in
3573 let colorp name get set =
3574 src#string name
3575 (fun () -> color_to_string (get ()))
3576 (fun v ->
3578 let c = color_of_string v in
3579 set c
3580 with exn ->
3581 state.text <-
3582 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3585 let oldmode = state.mode in
3586 let birdseye = isbirdseye state.mode in
3588 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3590 src#bool "presentation mode"
3591 (fun () -> conf.presentation)
3592 (fun v -> setpresentationmode v);
3594 src#bool "ignore case in searches"
3595 (fun () -> conf.icase)
3596 (fun v -> conf.icase <- v);
3598 src#bool "preload"
3599 (fun () -> conf.preload)
3600 (fun v -> conf.preload <- v);
3602 src#bool "highlight links"
3603 (fun () -> conf.hlinks)
3604 (fun v -> conf.hlinks <- v);
3606 src#bool "under info"
3607 (fun () -> conf.underinfo)
3608 (fun v -> conf.underinfo <- v);
3610 src#bool "persistent bookmarks"
3611 (fun () -> conf.savebmarks)
3612 (fun v -> conf.savebmarks <- v);
3614 src#fitmodel "fit model"
3615 (fun () -> FMTE.to_string conf.fitmodel)
3616 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3618 src#bool "trim margins"
3619 (fun () -> conf.trimmargins)
3620 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3622 src#bool "persistent location"
3623 (fun () -> conf.jumpback)
3624 (fun v -> conf.jumpback <- v);
3626 sep ();
3627 src#int "inter-page space"
3628 (fun () -> conf.interpagespace)
3629 (fun n ->
3630 conf.interpagespace <- n;
3631 docolumns conf.columns;
3632 let pageno, py =
3633 match state.layout with
3634 | [] -> 0, 0
3635 | l :: _ ->
3636 l.pageno, l.pagey
3638 state.maxy <- calcheight ();
3639 let y = getpagey pageno in
3640 gotoxy state.x (y + py)
3643 src#int "page bias"
3644 (fun () -> conf.pagebias)
3645 (fun v -> conf.pagebias <- v);
3647 src#int "scroll step"
3648 (fun () -> conf.scrollstep)
3649 (fun n -> conf.scrollstep <- n);
3651 src#int "horizontal scroll step"
3652 (fun () -> conf.hscrollstep)
3653 (fun v -> conf.hscrollstep <- v);
3655 src#int "auto scroll step"
3656 (fun () ->
3657 match state.autoscroll with
3658 | Some step -> step
3659 | _ -> conf.autoscrollstep)
3660 (fun n ->
3661 let n = boundastep state.winh n in
3662 if state.autoscroll <> None
3663 then state.autoscroll <- Some n;
3664 conf.autoscrollstep <- n);
3666 src#int "zoom"
3667 (fun () -> truncate (conf.zoom *. 100.))
3668 (fun v -> pivotzoom ((float v) /. 100.));
3670 src#int "rotation"
3671 (fun () -> conf.angle)
3672 (fun v -> reqlayout v conf.fitmodel);
3674 src#int "scroll bar width"
3675 (fun () -> conf.scrollbw)
3676 (fun v ->
3677 conf.scrollbw <- v;
3678 reshape state.winw state.winh;
3681 src#int "scroll handle height"
3682 (fun () -> conf.scrollh)
3683 (fun v -> conf.scrollh <- v;);
3685 src#int "thumbnail width"
3686 (fun () -> conf.thumbw)
3687 (fun v ->
3688 conf.thumbw <- min 4096 v;
3689 match oldmode with
3690 | Birdseye beye ->
3691 leavebirdseye beye false;
3692 enterbirdseye ()
3693 | Textentry _
3694 | View
3695 | LinkNav _ -> ()
3698 let mode = state.mode in
3699 src#string "columns"
3700 (fun () ->
3701 match conf.columns with
3702 | Csingle _ -> "1"
3703 | Cmulti (multi, _) -> multicolumns_to_string multi
3704 | Csplit (count, _) -> "-" ^ string_of_int count
3706 (fun v ->
3707 let n, a, b = multicolumns_of_string v in
3708 setcolumns mode n a b);
3710 sep ();
3711 src#caption "Pixmap cache" 0;
3712 src#int_with_suffix "size (advisory)"
3713 (fun () -> conf.memlimit)
3714 (fun v -> conf.memlimit <- v);
3716 src#caption2 "used"
3717 (fun () ->
3718 Printf.sprintf "%s bytes, %d tiles"
3719 (string_with_suffix_of_int state.memused)
3720 (Hashtbl.length state.tilemap)) 1;
3722 sep ();
3723 src#caption "Layout" 0;
3724 src#caption2 "Dimension"
3725 (fun () ->
3726 Printf.sprintf "%dx%d (virtual %dx%d)"
3727 state.winw state.winh
3728 state.w state.maxy)
3730 if conf.debug
3731 then
3732 src#caption2 "Position" (fun () ->
3733 Printf.sprintf "%dx%d" state.x state.y
3735 else
3736 src#caption2 "Position" (fun () -> describe_location ()) 1
3739 sep ();
3740 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3741 "Save these parameters as global defaults at exit"
3742 (fun () -> conf.bedefault)
3743 (fun v -> conf.bedefault <- v)
3746 sep ();
3747 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3748 src#bool ~offset:0 ~btos "Extended parameters"
3749 (fun () -> !showextended)
3750 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3751 if !showextended
3752 then (
3753 src#bool "checkers"
3754 (fun () -> conf.checkers)
3755 (fun v -> conf.checkers <- v; setcheckers v);
3756 src#bool "update cursor"
3757 (fun () -> conf.updatecurs)
3758 (fun v -> conf.updatecurs <- v);
3759 src#bool "scroll-bar on the left"
3760 (fun () -> conf.leftscroll)
3761 (fun v -> conf.leftscroll <- v);
3762 src#bool "verbose"
3763 (fun () -> conf.verbose)
3764 (fun v -> conf.verbose <- v);
3765 src#bool "invert colors"
3766 (fun () -> conf.invert)
3767 (fun v -> conf.invert <- v);
3768 src#bool "max fit"
3769 (fun () -> conf.maxhfit)
3770 (fun v -> conf.maxhfit <- v);
3771 src#bool "pax mode"
3772 (fun () -> conf.pax != None)
3773 (fun v ->
3774 if v
3775 then conf.pax <- Some (ref (now (), 0, 0))
3776 else conf.pax <- None);
3777 src#string "uri launcher"
3778 (fun () -> conf.urilauncher)
3779 (fun v -> conf.urilauncher <- v);
3780 src#string "path launcher"
3781 (fun () -> conf.pathlauncher)
3782 (fun v -> conf.pathlauncher <- v);
3783 src#string "tile size"
3784 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3785 (fun v ->
3787 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3788 conf.tilew <- max 64 w;
3789 conf.tileh <- max 64 h;
3790 flushtiles ();
3791 with exn ->
3792 state.text <- Printf.sprintf "bad tile size `%s': %s"
3793 v @@ exntos exn
3795 src#int "texture count"
3796 (fun () -> conf.texcount)
3797 (fun v ->
3798 if realloctexts v
3799 then conf.texcount <- v
3800 else impmsg "failed to set texture count please retry later"
3802 src#int "slice height"
3803 (fun () -> conf.sliceheight)
3804 (fun v ->
3805 conf.sliceheight <- v;
3806 wcmd "sliceh %d" conf.sliceheight;
3808 src#int "anti-aliasing level"
3809 (fun () -> conf.aalevel)
3810 (fun v ->
3811 conf.aalevel <- bound v 0 8;
3812 state.anchor <- getanchor ();
3813 opendoc state.path state.password;
3815 src#string "page scroll scaling factor"
3816 (fun () -> string_of_float conf.pgscale)
3817 (fun v ->
3819 let s = float_of_string v in
3820 conf.pgscale <- s
3821 with exn ->
3822 state.text <- Printf.sprintf
3823 "bad page scroll scaling factor `%s': %s" v
3824 @@ exntos exn
3827 src#int "ui font size"
3828 (fun () -> fstate.fontsize)
3829 (fun v -> setfontsize (bound v 5 100));
3830 src#int "hint font size"
3831 (fun () -> conf.hfsize)
3832 (fun v -> conf.hfsize <- bound v 5 100);
3833 colorp "background color"
3834 (fun () -> conf.bgcolor)
3835 (fun v -> conf.bgcolor <- v; setbgcol v);
3836 src#bool "crop hack"
3837 (fun () -> conf.crophack)
3838 (fun v -> conf.crophack <- v);
3839 src#string "trim fuzz"
3840 (fun () -> irect_to_string conf.trimfuzz)
3841 (fun v ->
3843 conf.trimfuzz <- irect_of_string v;
3844 if conf.trimmargins
3845 then settrim true conf.trimfuzz;
3846 with exn ->
3847 state.text <- Printf.sprintf "bad irect `%s': %s" v
3848 @@ exntos exn
3850 src#string "throttle"
3851 (fun () ->
3852 match conf.maxwait with
3853 | None -> "show place holder if page is not ready"
3854 | Some time ->
3855 if time = infinity
3856 then "wait for page to fully render"
3857 else
3858 "wait " ^ string_of_float time
3859 ^ " seconds before showing placeholder"
3861 (fun v ->
3863 let f = float_of_string v in
3864 if f <= 0.0
3865 then conf.maxwait <- None
3866 else conf.maxwait <- Some f
3867 with exn ->
3868 state.text <- Printf.sprintf "bad time `%s': %s" v
3869 @@ exntos exn
3871 src#string "ghyll scroll"
3872 (fun () ->
3873 match conf.ghyllscroll with
3874 | None -> E.s
3875 | Some nab -> ghyllscroll_to_string nab
3877 (fun v ->
3878 try conf.ghyllscroll <- ghyllscroll_of_string v
3879 with
3880 | Failure msg ->
3881 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3882 | exn ->
3883 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3884 @@ exntos exn
3886 src#string "selection command"
3887 (fun () -> conf.selcmd)
3888 (fun v -> conf.selcmd <- v);
3889 src#string "synctex command"
3890 (fun () -> conf.stcmd)
3891 (fun v -> conf.stcmd <- v);
3892 src#string "pax command"
3893 (fun () -> conf.paxcmd)
3894 (fun v -> conf.paxcmd <- v);
3895 src#string "ask password command"
3896 (fun () -> conf.passcmd)
3897 (fun v -> conf.passcmd <- v);
3898 src#string "save path command"
3899 (fun () -> conf.savecmd)
3900 (fun v -> conf.savecmd <- v);
3901 src#colorspace "color space"
3902 (fun () -> CSTE.to_string conf.colorspace)
3903 (fun v ->
3904 conf.colorspace <- CSTE.of_int v;
3905 wcmd "cs %d" v;
3906 load state.layout;
3908 src#paxmark "pax mark method"
3909 (fun () -> MTE.to_string conf.paxmark)
3910 (fun v -> conf.paxmark <- MTE.of_int v);
3911 if bousable () && !opengl_has_pbo
3912 then
3913 src#bool "use PBO"
3914 (fun () -> conf.usepbo)
3915 (fun v -> conf.usepbo <- v);
3916 src#bool "mouse wheel scrolls pages"
3917 (fun () -> conf.wheelbypage)
3918 (fun v -> conf.wheelbypage <- v);
3919 src#bool "open remote links in a new instance"
3920 (fun () -> conf.riani)
3921 (fun v -> conf.riani <- v);
3922 src#bool "edit annotations inline"
3923 (fun () -> conf.annotinline)
3924 (fun v -> conf.annotinline <- v);
3925 src#bool "coarse positioning in presentation mode"
3926 (fun () -> conf.coarseprespos)
3927 (fun v -> conf.coarseprespos <- v);
3928 src#bool "use document CSS"
3929 (fun () -> conf.usedoccss)
3930 (fun v ->
3931 conf.usedoccss <- v;
3932 state.anchor <- getanchor ();
3933 opendoc state.path state.password;
3937 sep ();
3938 src#caption "Document" 0;
3939 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3940 src#caption2 "Pages"
3941 (fun () -> string_of_int state.pagecount) 1;
3942 src#caption2 "Dimensions"
3943 (fun () -> string_of_int (List.length state.pdims)) 1;
3944 if nonemptystr conf.css
3945 then src#caption2 "CSS" (fun () -> conf.css) 1;
3946 if conf.trimmargins
3947 then (
3948 sep ();
3949 src#caption "Trimmed margins" 0;
3950 src#caption2 "Dimensions"
3951 (fun () -> string_of_int (List.length state.pdims)) 1;
3954 sep ();
3955 src#caption "OpenGL" 0;
3956 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3957 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3959 sep ();
3960 src#caption "Location" 0;
3961 if nonemptystr state.origin
3962 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3963 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3965 src#reset prevmode prevuioh;
3967 fun () ->
3968 state.text <- E.s;
3969 resetmstate ();
3970 let prevmode = state.mode
3971 and prevuioh = state.uioh in
3972 fillsrc prevmode prevuioh;
3973 let source = (src :> lvsource) in
3974 let modehash = findkeyhash conf "info" in
3975 state.uioh <-
3976 coe (object (self)
3977 inherit listview ~zebra:false ~helpmode:false
3978 ~source ~trusted:true ~modehash as super
3979 val mutable m_prevmemused = 0
3980 method! infochanged = function
3981 | Memused ->
3982 if m_prevmemused != state.memused
3983 then (
3984 m_prevmemused <- state.memused;
3985 G.postRedisplay "memusedchanged";
3987 | Pdim -> G.postRedisplay "pdimchanged"
3988 | Docinfo -> fillsrc prevmode prevuioh
3990 method! key key mask =
3991 if not (Wsi.withctrl mask)
3992 then
3993 match [@warning "-4"] Wsi.kc2kt key with
3994 | Keys.Left -> coe (self#updownlevel ~-1)
3995 | Keys.Right -> coe (self#updownlevel 1)
3996 | _ -> super#key key mask
3997 else super#key key mask
3998 end);
3999 G.postRedisplay "info";
4002 let enterhelpmode =
4003 let source =
4004 (object
4005 inherit lvsourcebase
4006 method getitemcount = Array.length state.help
4007 method getitem n =
4008 let s, l, _ = state.help.(n) in
4009 (s, l)
4011 method exit ~uioh ~cancel ~active ~first ~pan =
4012 let optuioh =
4013 if not cancel
4014 then (
4015 match state.help.(active) with
4016 | _, _, Action f -> Some (f uioh)
4017 | _, _, Noaction -> Some uioh
4019 else None
4021 m_active <- active;
4022 m_first <- first;
4023 m_pan <- pan;
4024 optuioh
4026 method hasaction n =
4027 match state.help.(n) with
4028 | _, _, Action _ -> true
4029 | _, _, Noaction -> false
4031 initializer
4032 m_active <- -1
4033 end)
4034 in fun () ->
4035 let modehash = findkeyhash conf "help" in
4036 resetmstate ();
4037 state.uioh <- coe (new listview
4038 ~zebra:false ~helpmode:true
4039 ~source ~trusted:true ~modehash);
4040 G.postRedisplay "help";
4043 let entermsgsmode =
4044 let msgsource =
4045 (object
4046 inherit lvsourcebase
4047 val mutable m_items = E.a
4049 method getitemcount = 1 + Array.length m_items
4051 method getitem n =
4052 if n = 0
4053 then "[Clear]", 0
4054 else m_items.(n-1), 0
4056 method exit ~uioh ~cancel ~active ~first ~pan =
4057 ignore uioh;
4058 if not cancel
4059 then (
4060 if active = 0
4061 then Buffer.clear state.errmsgs;
4063 m_active <- active;
4064 m_first <- first;
4065 m_pan <- pan;
4066 None
4068 method hasaction n =
4069 n = 0
4071 method reset =
4072 state.newerrmsgs <- false;
4073 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4074 m_items <- Array.of_list l
4076 initializer
4077 m_active <- 0
4078 end)
4079 in fun () ->
4080 state.text <- E.s;
4081 resetmstate ();
4082 msgsource#reset;
4083 let source = (msgsource :> lvsource) in
4084 let modehash = findkeyhash conf "listview" in
4085 state.uioh <-
4086 coe (object
4087 inherit listview ~zebra:false ~helpmode:false
4088 ~source ~trusted:false ~modehash as super
4089 method! display =
4090 if state.newerrmsgs
4091 then msgsource#reset;
4092 super#display
4093 end);
4094 G.postRedisplay "msgs";
4097 let getusertext s =
4098 let editor = getenvwithdef "EDITOR" E.s in
4099 if emptystr editor
4100 then E.s
4101 else
4102 let tmppath = Filename.temp_file "llpp" "note" in
4103 if nonemptystr s
4104 then (
4105 let oc = open_out tmppath in
4106 output_string oc s;
4107 close_out oc;
4109 let execstr = editor ^ " " ^ tmppath in
4110 let s =
4111 match spawn execstr [] with
4112 | exception exn ->
4113 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4115 | pid ->
4116 match Unix.waitpid [] pid with
4117 | exception exn ->
4118 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4120 | (_pid, status) ->
4121 match status with
4122 | Unix.WEXITED 0 -> filecontents tmppath
4123 | Unix.WEXITED n ->
4124 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4126 | Unix.WSIGNALED n ->
4127 impmsg "editor process(%s) was killed by signal %d" execstr n;
4129 | Unix.WSTOPPED n ->
4130 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4133 match Unix.unlink tmppath with
4134 | exception exn ->
4135 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4137 | () -> s
4140 let enterannotmode opaque slinkindex =
4141 let msgsource =
4142 (object
4143 inherit lvsourcebase
4144 val mutable m_text = E.s
4145 val mutable m_items = E.a
4147 method getitemcount = Array.length m_items
4149 method getitem n =
4150 let label, _func = m_items.(n) in
4151 label, 0
4153 method exit ~uioh ~cancel ~active ~first ~pan =
4154 ignore (uioh, first, pan);
4155 if not cancel
4156 then (
4157 let _label, func = m_items.(active) in
4158 func ()
4160 None
4162 method hasaction n = nonemptystr @@ fst m_items.(n)
4164 method reset s =
4165 let rec split accu b i =
4166 let p = b+i in
4167 if p = String.length s
4168 then (String.sub s b (p-b), unit) :: accu
4169 else
4170 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4171 then
4172 let ss = if i = 0 then E.s else String.sub s b i in
4173 split ((ss, unit)::accu) (p+1) 0
4174 else
4175 split accu b (i+1)
4177 let cleanup () =
4178 wcmd "freepage %s" (~> opaque);
4179 let keys =
4180 Hashtbl.fold (fun key opaque' accu ->
4181 if opaque' = opaque'
4182 then key :: accu else accu) state.pagemap []
4184 List.iter (Hashtbl.remove state.pagemap) keys;
4185 flushtiles ();
4186 gotoxy state.x state.y
4188 let dele () =
4189 delannot opaque slinkindex;
4190 cleanup ();
4192 let edit inline () =
4193 let update s =
4194 if emptystr s
4195 then dele ()
4196 else (
4197 modannot opaque slinkindex s;
4198 cleanup ();
4201 if inline
4202 then
4203 let mode = state.mode in
4204 state.mode <-
4205 Textentry (
4206 ("annotation: ", m_text, None, textentry, update, true),
4207 fun _ -> state.mode <- mode);
4208 state.text <- E.s;
4209 enttext ();
4210 else
4211 let s = getusertext m_text in
4212 update s
4214 m_text <- s;
4215 m_items <-
4216 ( "[Copy]", fun () -> selstring m_text)
4217 :: ("[Delete]", dele)
4218 :: ("[Edit]", edit conf.annotinline)
4219 :: (E.s, unit)
4220 :: split [] 0 0 |> List.rev |> Array.of_list
4222 initializer
4223 m_active <- 0
4224 end)
4226 state.text <- E.s;
4227 let s = getannotcontents opaque slinkindex in
4228 resetmstate ();
4229 msgsource#reset s;
4230 let source = (msgsource :> lvsource) in
4231 let modehash = findkeyhash conf "listview" in
4232 state.uioh <- coe (object
4233 inherit listview ~zebra:false ~helpmode:false
4234 ~source ~trusted:false ~modehash
4235 end);
4236 G.postRedisplay "enterannotmode";
4239 let gotoremote spec =
4240 let filename, dest = splitatchar spec '#' in
4241 let getpath filename =
4242 let path =
4243 if nonemptystr filename
4244 then
4245 if Filename.is_relative filename
4246 then
4247 let dir = Filename.dirname state.path in
4248 let dir =
4249 if Filename.is_implicit dir
4250 then Filename.concat (Sys.getcwd ()) dir
4251 else dir
4253 Filename.concat dir filename
4254 else filename
4255 else E.s
4257 if Sys.file_exists path
4258 then path
4259 else E.s
4261 let path = getpath filename in
4262 let dospawn lcmd =
4263 if conf.riani
4264 then
4265 let cmd = Lazy.force_val lcmd in
4266 match spawn cmd with
4267 | _pid -> ()
4268 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4269 else
4270 let anchor = getanchor () in
4271 let ranchor = state.path, state.password, anchor, state.origin in
4272 state.origin <- E.s;
4273 state.ranchors <- ranchor :: state.ranchors;
4274 opendoc path E.s;
4276 if substratis spec 0 "page="
4277 then
4278 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4279 | pageno ->
4280 state.anchor <- (pageno, 0.0, 0.0);
4281 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4282 | exception exn ->
4283 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4284 else (
4285 state.nameddest <- dest;
4286 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4290 let gotounder = function
4291 | Ulinkuri s when isexternallink s ->
4292 if substratis s 0 "file://"
4293 then gotoremote @@ String.sub s 7 (String.length s - 7)
4294 else gotouri s
4295 | Ulinkuri s ->
4296 let pageno, x, y = uritolocation s in
4297 addnav ();
4298 gotopagexy !wtmode pageno x y
4299 | Utext _ | Unone -> ()
4300 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4303 let gotooutline (_, _, kind) =
4304 match kind with
4305 | Onone -> ()
4306 | Oanchor anchor ->
4307 let (pageno, y, _) = anchor in
4308 let y = getanchory
4309 (if conf.presentation then (pageno, y, 1.0) else anchor)
4311 addnav ();
4312 gotoghyll y
4313 | Ouri uri -> gotounder (Ulinkuri uri)
4314 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4315 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4316 | Ohistory hist -> gotohist hist
4317 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4320 class outlinesoucebase fetchoutlines = object (self)
4321 inherit lvsourcebase
4322 val mutable m_items = E.a
4323 val mutable m_minfo = E.a
4324 val mutable m_orig_items = E.a
4325 val mutable m_orig_minfo = E.a
4326 val mutable m_narrow_patterns = []
4327 val mutable m_gen = -1
4329 method getitemcount = Array.length m_items
4331 method getitem n =
4332 let s, n, _ = m_items.(n) in
4333 (s, n+0)
4335 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4336 ignore (uioh, first);
4337 let items, minfo =
4338 if m_narrow_patterns = []
4339 then m_orig_items, m_orig_minfo
4340 else m_items, m_minfo
4342 m_pan <- pan;
4343 if not cancel
4344 then (
4345 m_items <- items;
4346 m_minfo <- minfo;
4347 gotooutline m_items.(active);
4349 else (
4350 m_items <- items;
4351 m_minfo <- minfo;
4353 None
4355 method hasaction (_:int) = true
4357 method greetmsg =
4358 if Array.length m_items != Array.length m_orig_items
4359 then
4360 let s =
4361 match m_narrow_patterns with
4362 | one :: [] -> one
4363 | many -> String.concat UniSyms.ellipsis (List.rev many)
4365 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4366 else E.s
4368 method statestr =
4369 match m_narrow_patterns with
4370 | [] -> E.s
4371 | one :: [] -> one
4372 | head :: _ -> UniSyms.ellipsis ^ head
4374 method narrow pattern =
4375 match Str.regexp_case_fold pattern with
4376 | exception _ -> ()
4377 | re ->
4378 let rec loop accu minfo n =
4379 if n = -1
4380 then (
4381 m_items <- Array.of_list accu;
4382 m_minfo <- Array.of_list minfo;
4384 else
4385 let (s, _, _) as o = m_items.(n) in
4386 let accu, minfo =
4387 match Str.search_forward re s 0 with
4388 | exception Not_found -> accu, minfo
4389 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4391 loop accu minfo (n-1)
4393 loop [] [] (Array.length m_items - 1)
4395 method! getminfo = m_minfo
4397 method denarrow =
4398 m_orig_items <- fetchoutlines ();
4399 m_minfo <- m_orig_minfo;
4400 m_items <- m_orig_items
4402 method add_narrow_pattern pattern =
4403 m_narrow_patterns <- pattern :: m_narrow_patterns
4405 method del_narrow_pattern =
4406 match m_narrow_patterns with
4407 | _ :: rest -> m_narrow_patterns <- rest
4408 | [] -> ()
4410 method renarrow =
4411 self#denarrow;
4412 match m_narrow_patterns with
4413 | pattern :: [] -> self#narrow pattern; pattern
4414 | list ->
4415 List.fold_left (fun accu pattern ->
4416 self#narrow pattern;
4417 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4419 method calcactive (_:anchor) = 0
4421 method reset anchor items =
4422 if state.gen != m_gen
4423 then (
4424 m_orig_items <- items;
4425 m_items <- items;
4426 m_narrow_patterns <- [];
4427 m_minfo <- E.a;
4428 m_orig_minfo <- E.a;
4429 m_gen <- state.gen;
4431 else (
4432 if items != m_orig_items
4433 then (
4434 m_orig_items <- items;
4435 if m_narrow_patterns == []
4436 then m_items <- items;
4439 let active = self#calcactive anchor in
4440 m_active <- active;
4441 m_first <- firstof m_first active
4445 let outlinesource fetchoutlines =
4446 (object
4447 inherit outlinesoucebase fetchoutlines
4448 method! calcactive anchor =
4449 let rely = getanchory anchor in
4450 let rec loop n best bestd =
4451 if n = Array.length m_items
4452 then best
4453 else
4454 let _, _, kind = m_items.(n) in
4455 match kind with
4456 | Oanchor anchor ->
4457 let orely = getanchory anchor in
4458 let d = abs (orely - rely) in
4459 if d < bestd
4460 then loop (n+1) n d
4461 else loop (n+1) best bestd
4462 | Onone | Oremote _ | Olaunch _
4463 | Oremotedest _ | Ouri _ | Ohistory _ ->
4464 loop (n+1) best bestd
4466 loop 0 ~-1 max_int
4467 end)
4470 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4471 let mkselector sourcetype =
4472 let fetchoutlines () =
4473 match sourcetype with
4474 | `bookmarks -> Array.of_list state.bookmarks
4475 | `outlines -> state.outlines
4476 | `history -> genhistoutlines () |> Array.of_list
4478 let source =
4479 if sourcetype = `history
4480 then new outlinesoucebase fetchoutlines
4481 else outlinesource fetchoutlines
4483 (fun errmsg ->
4484 let outlines = fetchoutlines () in
4485 if Array.length outlines = 0
4486 then showtext ' ' errmsg
4487 else (
4488 resetmstate ();
4489 Wsi.setcursor Wsi.CURSOR_INHERIT;
4490 let anchor = getanchor () in
4491 source#reset anchor outlines;
4492 state.text <- source#greetmsg;
4493 state.uioh <-
4494 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4495 G.postRedisplay "enter selector";
4499 let mkenter sourcetype errmsg =
4500 let enter = mkselector sourcetype in
4501 fun () -> enter errmsg
4503 ( mkenter `outlines "document has no outline"
4504 , mkenter `bookmarks "document has no bookmarks (yet)"
4505 , mkenter `history "history is empty" )
4508 let quickbookmark ?title () =
4509 match state.layout with
4510 | [] -> ()
4511 | l :: _ ->
4512 let title =
4513 match title with
4514 | None ->
4515 Unix.(
4516 let tm = localtime (now ()) in
4517 Printf.sprintf
4518 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4519 (l.pageno+1)
4520 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4522 | Some title -> title
4524 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4527 let setautoscrollspeed step goingdown =
4528 let incr = max 1 ((abs step) / 2) in
4529 let incr = if goingdown then incr else -incr in
4530 let astep = boundastep state.winh (step + incr) in
4531 state.autoscroll <- Some astep;
4534 let canpan () =
4535 match conf.columns with
4536 | Csplit _ -> true
4537 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4540 let panbound x = bound x (-state.w) state.winw;;
4542 let existsinrow pageno (columns, coverA, coverB) p =
4543 let last = ((pageno - coverA) mod columns) + columns in
4544 let rec any = function
4545 | [] -> false
4546 | l :: rest ->
4547 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4548 then p l
4549 else (
4550 if not (p l)
4551 then (if l.pageno = last then false else any rest)
4552 else true
4555 any state.layout
4558 let nextpage () =
4559 match state.layout with
4560 | [] ->
4561 let pageno = page_of_y state.y in
4562 gotoghyll (getpagey (pageno+1))
4563 | l :: rest ->
4564 match conf.columns with
4565 | Csingle _ ->
4566 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4567 then
4568 let y = clamp (pgscale state.winh) in
4569 gotoghyll y
4570 else
4571 let pageno = min (l.pageno+1) (state.pagecount-1) in
4572 gotoghyll (getpagey pageno)
4573 | Cmulti ((c, _, _) as cl, _) ->
4574 if conf.presentation
4575 && (existsinrow l.pageno cl
4576 (fun l -> l.pageh > l.pagey + l.pagevh))
4577 then
4578 let y = clamp (pgscale state.winh) in
4579 gotoghyll y
4580 else
4581 let pageno = min (l.pageno+c) (state.pagecount-1) in
4582 gotoghyll (getpagey pageno)
4583 | Csplit (n, _) ->
4584 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4585 then
4586 let pagey, pageh = getpageyh l.pageno in
4587 let pagey = pagey + pageh * l.pagecol in
4588 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4589 gotoghyll (pagey + pageh + ips)
4592 let prevpage () =
4593 match state.layout with
4594 | [] ->
4595 let pageno = page_of_y state.y in
4596 gotoghyll (getpagey (pageno-1))
4597 | l :: _ ->
4598 match conf.columns with
4599 | Csingle _ ->
4600 if conf.presentation && l.pagey != 0
4601 then
4602 gotoghyll (clamp (pgscale ~-(state.winh)))
4603 else
4604 let pageno = max 0 (l.pageno-1) in
4605 gotoghyll (getpagey pageno)
4606 | Cmulti ((c, _, coverB) as cl, _) ->
4607 if conf.presentation &&
4608 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4609 then
4610 gotoghyll (clamp (pgscale ~-(state.winh)))
4611 else
4612 let decr =
4613 if l.pageno = state.pagecount - coverB
4614 then 1
4615 else c
4617 let pageno = max 0 (l.pageno-decr) in
4618 gotoghyll (getpagey pageno)
4619 | Csplit (n, _) ->
4620 let y =
4621 if l.pagecol = 0
4622 then
4623 if l.pageno = 0
4624 then l.pagey
4625 else
4626 let pageno = max 0 (l.pageno-1) in
4627 let pagey, pageh = getpageyh pageno in
4628 pagey + (n-1)*pageh
4629 else
4630 let pagey, pageh = getpageyh l.pageno in
4631 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4633 gotoghyll y
4636 let save () =
4637 if emptystr conf.savecmd
4638 then adderrmsg "savepath-command is empty"
4639 "don't know where to save modified document"
4640 else
4641 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4642 let path =
4643 getcmdoutput
4644 (fun exn ->
4645 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4646 savecmd
4648 if nonemptystr path
4649 then
4650 let tmp = path ^ ".tmp" in
4651 savedoc tmp;
4652 Unix.rename tmp path;
4655 let viewkeyboard key mask =
4656 let enttext te =
4657 let mode = state.mode in
4658 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4659 state.text <- E.s;
4660 enttext ();
4661 G.postRedisplay "view:enttext"
4663 let ctrl = Wsi.withctrl mask in
4664 let open Keys in
4665 match Wsi.kc2kt key with
4666 | Ascii 'Q' -> exit 0
4668 | Ascii 'W' ->
4669 if hasunsavedchanges ()
4670 then save ()
4672 | Insert ->
4673 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4674 then (
4675 state.mode <- (
4676 match state.lnava with
4677 | None -> LinkNav (Ltgendir 0)
4678 | Some pn -> LinkNav (Ltexact pn)
4680 gotoxy state.x state.y;
4682 else impmsg "keyboard link navigation does not work under rotation"
4684 | Escape | Ascii 'q' ->
4685 begin match state.mstate with
4686 | Mzoomrect _ ->
4687 resetmstate ();
4688 G.postRedisplay "kill rect";
4689 | Msel _
4690 | Mpan _
4691 | Mscrolly | Mscrollx
4692 | Mzoom _
4693 | Mnone ->
4694 begin match state.mode with
4695 | LinkNav ln ->
4696 begin match ln with
4697 | Ltexact pl -> state.lnava <- Some pl
4698 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4699 end;
4700 state.mode <- View;
4701 G.postRedisplay "esc leave linknav"
4702 | Birdseye _ | Textentry _ | View ->
4703 match state.ranchors with
4704 | [] -> raise Quit
4705 | (path, password, anchor, origin) :: rest ->
4706 state.ranchors <- rest;
4707 state.anchor <- anchor;
4708 state.origin <- origin;
4709 state.nameddest <- E.s;
4710 opendoc path password
4711 end;
4712 end;
4714 | Backspace ->
4715 gotoghyll (getnav ~-1)
4717 | Ascii 'o' ->
4718 enteroutlinemode ()
4720 | Ascii 'H' ->
4721 enterhistmode ()
4723 | Ascii 'u' ->
4724 state.rects <- [];
4725 state.text <- E.s;
4726 Hashtbl.iter (fun _ opaque ->
4727 clearmark opaque;
4728 Hashtbl.clear state.prects) state.pagemap;
4729 G.postRedisplay "dehighlight";
4731 | (Ascii '/' | Ascii '?') as pv ->
4732 let ondone isforw s =
4733 cbput state.hists.pat s;
4734 state.searchpattern <- s;
4735 search s isforw
4737 let s = String.make 1 (Char.chr key) in
4738 enttext (s, E.s, Some (onhist state.hists.pat),
4739 textentry, ondone (pv = Ascii '/'), true)
4741 | Ascii '+' | Ascii '=' when ctrl ->
4742 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4743 pivotzoom (conf.zoom +. incr)
4745 | Ascii '+' ->
4746 let ondone s =
4747 let n =
4748 try int_of_string s with exn ->
4749 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4750 max_int
4752 if n != max_int
4753 then (
4754 conf.pagebias <- n;
4755 state.text <- "page bias is now " ^ string_of_int n;
4758 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4760 | Ascii '-' when ctrl ->
4761 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4762 pivotzoom (max 0.01 (conf.zoom -. decr))
4764 | Ascii '-' ->
4765 let ondone msg = state.text <- msg in
4766 enttext (
4767 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4768 optentry state.mode, ondone, true
4771 | Ascii '0' when ctrl ->
4772 if conf.zoom = 1.0
4773 then gotoxy 0 state.y
4774 else setzoom 1.0
4776 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4777 let cols =
4778 match conf.columns with
4779 | Csingle _ | Cmulti _ -> 1
4780 | Csplit (n, _) -> n
4782 let h = state.winh -
4783 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4785 let zoom = zoomforh state.winw h 0 cols in
4786 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4787 then setzoom zoom
4789 | Ascii '3' when ctrl ->
4790 let fm =
4791 match conf.fitmodel with
4792 | FitWidth -> FitProportional
4793 | FitProportional -> FitPage
4794 | FitPage -> FitWidth
4796 state.text <- "fit model: " ^ FMTE.to_string fm;
4797 reqlayout conf.angle fm
4799 | Ascii '4' when ctrl ->
4800 let zoom = getmaxw () /. float state.winw in
4801 if zoom > 0.0 then setzoom zoom
4803 | Fn 9 ->
4804 togglebirdseye ()
4806 | Ascii '9' when ctrl ->
4807 togglebirdseye ()
4809 | Ascii ('0'..'9')
4810 when not ctrl ->
4811 let ondone s =
4812 let n =
4813 try int_of_string s with exn ->
4814 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4817 if n >= 0
4818 then (
4819 addnav ();
4820 cbput state.hists.pag (string_of_int n);
4821 gotopage1 (n + conf.pagebias - 1) 0;
4824 let [@warning "-4"] pageentry text = function
4825 | Keys.Ascii 'g' -> TEdone text
4826 | key -> intentry text key
4828 let text = String.make 1 (Char.chr key) in
4829 enttext (":", text, Some (onhist state.hists.pag),
4830 pageentry, ondone, true)
4832 | Ascii 'b' ->
4833 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4834 G.postRedisplay "toggle scrollbar";
4836 | Ascii 'B' ->
4837 state.bzoom <- not state.bzoom;
4838 state.rects <- [];
4839 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4841 | Ascii 'l' ->
4842 conf.hlinks <- not conf.hlinks;
4843 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4844 G.postRedisplay "toggle highlightlinks";
4846 | Ascii 'F' ->
4847 if conf.angle mod 360 = 0
4848 then (
4849 state.glinks <- true;
4850 let mode = state.mode in
4851 state.mode <-
4852 Textentry (
4853 (":", E.s, None, linknentry, linknact gotounder, false),
4854 (fun _ ->
4855 state.glinks <- false;
4856 state.mode <- mode)
4858 state.text <- E.s;
4859 G.postRedisplay "view:linkent(F)"
4861 else impmsg "hint mode does not work under rotation"
4863 | Ascii 'y' ->
4864 state.glinks <- true;
4865 let mode = state.mode in
4866 state.mode <-
4867 Textentry (
4868 (":", E.s, None, linknentry,
4869 linknact (fun under -> selstring (undertext under)), false),
4870 (fun _ ->
4871 state.glinks <- false;
4872 state.mode <- mode)
4874 state.text <- E.s;
4875 G.postRedisplay "view:linkent"
4877 | Ascii 'a' ->
4878 begin match state.autoscroll with
4879 | Some step ->
4880 conf.autoscrollstep <- step;
4881 state.autoscroll <- None
4882 | None ->
4883 if conf.autoscrollstep = 0
4884 then state.autoscroll <- Some 1
4885 else state.autoscroll <- Some conf.autoscrollstep
4888 | Ascii 'p' when ctrl ->
4889 launchpath () (* XXX where do error messages go? *)
4891 | Ascii 'P' ->
4892 setpresentationmode (not conf.presentation);
4893 showtext ' ' ("presentation mode " ^
4894 if conf.presentation then "on" else "off");
4896 | Ascii 'f' ->
4897 if List.mem Wsi.Fullscreen state.winstate
4898 then Wsi.reshape conf.cwinw conf.cwinh
4899 else Wsi.fullscreen ()
4901 | Ascii ('p'|'N') ->
4902 search state.searchpattern false
4904 | Ascii 'n' | Fn 3 ->
4905 search state.searchpattern true
4907 | Ascii 't' ->
4908 begin match state.layout with
4909 | [] -> ()
4910 | l :: _ ->
4911 gotoghyll (getpagey l.pageno)
4914 | Ascii ' ' ->
4915 nextpage ()
4917 | Delete ->
4918 prevpage ()
4920 | Ascii '=' ->
4921 showtext ' ' (describe_location ());
4923 | Ascii 'w' ->
4924 begin match state.layout with
4925 | [] -> ()
4926 | l :: _ ->
4927 Wsi.reshape l.pagew l.pageh;
4928 G.postRedisplay "w"
4931 | Ascii '\'' ->
4932 enterbookmarkmode ()
4934 | Ascii 'h' | Fn 1 ->
4935 enterhelpmode ()
4937 | Ascii 'i' ->
4938 enterinfomode ()
4940 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4941 entermsgsmode ()
4943 | Ascii 'm' ->
4944 let ondone s =
4945 match state.layout with
4946 | l :: _ ->
4947 if nonemptystr s
4948 then
4949 state.bookmarks <-
4950 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4951 | _ -> ()
4953 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4955 | Ascii '~' ->
4956 quickbookmark ();
4957 showtext ' ' "Quick bookmark added";
4959 | Ascii 'z' ->
4960 begin match state.layout with
4961 | l :: _ ->
4962 let rect = getpdimrect l.pagedimno in
4963 let w, h =
4964 if conf.crophack
4965 then
4966 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4967 truncate (1.2 *. (rect.(3) -. rect.(0))))
4968 else
4969 (truncate (rect.(1) -. rect.(0)),
4970 truncate (rect.(3) -. rect.(0)))
4972 let w = truncate ((float w)*.conf.zoom)
4973 and h = truncate ((float h)*.conf.zoom) in
4974 if w != 0 && h != 0
4975 then (
4976 state.anchor <- getanchor ();
4977 Wsi.reshape w (h + conf.interpagespace)
4979 G.postRedisplay "z";
4981 | [] -> ()
4984 | Ascii 'x' -> state.roam ()
4986 | Ascii ('<'|'>' as c) ->
4987 reqlayout
4988 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
4990 | Ascii ('['|']' as c) ->
4991 conf.colorscale <-
4992 bound (conf.colorscale +. (if c = '>' then 0.1 else -0.1)) 0.0 1.0;
4993 G.postRedisplay "brightness";
4995 | Ascii 'c' when state.mode = View ->
4996 if Wsi.withalt mask
4997 then (
4998 if conf.zoom > 1.0
4999 then
5000 let m = (state.winw - state.w) / 2 in
5001 gotoxy_and_clear_text m state.y
5003 else
5004 let (c, a, b), z =
5005 match state.prevcolumns with
5006 | None -> (1, 0, 0), 1.0
5007 | Some (columns, z) ->
5008 let cab =
5009 match columns with
5010 | Csplit (c, _) -> -c, 0, 0
5011 | Cmulti ((c, a, b), _) -> c, a, b
5012 | Csingle _ -> 1, 0, 0
5014 cab, z
5016 setcolumns View c a b;
5017 setzoom z
5019 | Down | Up when ctrl && Wsi.withshift mask ->
5020 let zoom, x = state.prevzoom in
5021 setzoom zoom;
5022 state.x <- x;
5024 | Ascii 'k' | Up ->
5025 begin match state.autoscroll with
5026 | None ->
5027 begin match state.mode with
5028 | Birdseye beye -> upbirdseye 1 beye
5029 | Textentry _ | View | LinkNav _ ->
5030 if ctrl
5031 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5032 else (
5033 if not (Wsi.withshift mask) && conf.presentation
5034 then prevpage ()
5035 else gotoghyll1 true (clamp (-conf.scrollstep))
5038 | Some n ->
5039 setautoscrollspeed n false
5042 | Ascii 'j' | Down ->
5043 begin match state.autoscroll with
5044 | None ->
5045 begin match state.mode with
5046 | Birdseye beye -> downbirdseye 1 beye
5047 | Textentry _ | View | LinkNav _ ->
5048 if ctrl
5049 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5050 else (
5051 if not (Wsi.withshift mask) && conf.presentation
5052 then nextpage ()
5053 else gotoghyll1 true (clamp (conf.scrollstep))
5056 | Some n ->
5057 setautoscrollspeed n true
5060 | Left | Right when not (Wsi.withalt mask) ->
5061 if canpan ()
5062 then
5063 let dx =
5064 if ctrl
5065 then state.winw / 2
5066 else conf.hscrollstep
5068 let dx =
5069 let pv = Wsi.kc2kt key in
5070 if pv = Keys.Left then dx else -dx
5072 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5073 else (
5074 state.text <- E.s;
5075 G.postRedisplay "left/right"
5078 | Prior ->
5079 let y =
5080 if ctrl
5081 then
5082 match state.layout with
5083 | [] -> state.y
5084 | l :: _ -> state.y - l.pagey
5085 else
5086 clamp (pgscale (-state.winh))
5088 gotoghyll y
5090 | Next ->
5091 let y =
5092 if ctrl
5093 then
5094 match List.rev state.layout with
5095 | [] -> state.y
5096 | l :: _ -> getpagey l.pageno
5097 else
5098 clamp (pgscale state.winh)
5100 gotoghyll y
5102 | Ascii 'g' | Home ->
5103 addnav ();
5104 gotoghyll 0
5105 | Ascii 'G' | End ->
5106 addnav ();
5107 gotoghyll (clamp state.maxy)
5109 | Right when Wsi.withalt mask ->
5110 gotoghyll (getnav 1)
5111 | Left when Wsi.withalt mask ->
5112 gotoghyll (getnav ~-1)
5114 | Ascii 'r' ->
5115 reload ()
5117 | Ascii 'v' when conf.debug ->
5118 state.rects <- [];
5119 List.iter (fun l ->
5120 match getopaque l.pageno with
5121 | None -> ()
5122 | Some opaque ->
5123 let x0, y0, x1, y1 = pagebbox opaque in
5124 let rect = (float x0, float y0,
5125 float x1, float y0,
5126 float x1, float y1,
5127 float x0, float y1) in
5128 debugrect rect;
5129 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5130 state.rects <- (l.pageno, color, rect) :: state.rects;
5131 ) state.layout;
5132 G.postRedisplay "v";
5134 | Ascii '|' ->
5135 let mode = state.mode in
5136 let cmd = ref E.s in
5137 let onleave = function
5138 | Cancel -> state.mode <- mode
5139 | Confirm ->
5140 List.iter (fun l ->
5141 match getopaque l.pageno with
5142 | Some opaque -> pipesel opaque !cmd
5143 | None -> ()) state.layout;
5144 state.mode <- mode
5146 let ondone s =
5147 cbput state.hists.sel s;
5148 cmd := s
5150 let te =
5151 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5153 G.postRedisplay "|";
5154 state.mode <- Textentry (te, onleave);
5156 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5157 vlog "huh? %s" (Wsi.keyname key)
5160 let linknavkeyboard key mask linknav =
5161 let pv = Wsi.kc2kt key in
5162 let getpage pageno =
5163 let rec loop = function
5164 | [] -> None
5165 | l :: _ when l.pageno = pageno -> Some l
5166 | _ :: rest -> loop rest
5167 in loop state.layout
5169 let doexact (pageno, n) =
5170 match getopaque pageno, getpage pageno with
5171 | Some opaque, Some l ->
5172 if pv = Keys.Enter
5173 then
5174 let under = getlink opaque n in
5175 G.postRedisplay "link gotounder";
5176 gotounder under;
5177 state.mode <- View;
5178 else
5179 let opt, dir =
5180 let open Keys in
5181 match pv with
5182 | Home ->
5183 Some (findlink opaque LDfirst), -1
5185 | End ->
5186 Some (findlink opaque LDlast), 1
5188 | Left ->
5189 Some (findlink opaque (LDleft n)), -1
5191 | Right ->
5192 Some (findlink opaque (LDright n)), 1
5194 | Up ->
5195 Some (findlink opaque (LDup n)), -1
5197 | Down ->
5198 Some (findlink opaque (LDdown n)), 1
5200 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5201 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5203 let pwl l dir =
5204 begin match findpwl l.pageno dir with
5205 | Pwlnotfound -> ()
5206 | Pwl pageno ->
5207 let notfound dir =
5208 state.mode <- LinkNav (Ltgendir dir);
5209 let y, h = getpageyh pageno in
5210 let y =
5211 if dir < 0
5212 then y + h - state.winh
5213 else y
5215 gotoxy state.x y
5217 begin match getopaque pageno, getpage pageno with
5218 | Some opaque, Some _ ->
5219 let link =
5220 let ld = if dir > 0 then LDfirst else LDlast in
5221 findlink opaque ld
5223 begin match link with
5224 | Lfound m ->
5225 showlinktype (getlink opaque m);
5226 state.mode <- LinkNav (Ltexact (pageno, m));
5227 G.postRedisplay "linknav jpage";
5228 | Lnotfound -> notfound dir
5229 end;
5230 | _ -> notfound dir
5231 end;
5232 end;
5234 begin match opt with
5235 | Some Lnotfound -> pwl l dir;
5236 | Some (Lfound m) ->
5237 if m = n
5238 then pwl l dir
5239 else (
5240 let _, y0, _, y1 = getlinkrect opaque m in
5241 if y0 < l.pagey
5242 then gotopage1 l.pageno y0
5243 else (
5244 let d = fstate.fontsize + 1 in
5245 if y1 - l.pagey > l.pagevh - d
5246 then gotopage1 l.pageno (y1 - state.winh + d)
5247 else G.postRedisplay "linknav";
5249 showlinktype (getlink opaque m);
5250 state.mode <- LinkNav (Ltexact (l.pageno, m));
5253 | None -> viewkeyboard key mask
5254 end;
5255 | _ -> viewkeyboard key mask
5257 if pv = Keys.Insert
5258 then (
5259 begin match linknav with
5260 | Ltexact pa -> state.lnava <- Some pa
5261 | Ltgendir _ | Ltnotready _ -> ()
5262 end;
5263 state.mode <- View;
5264 G.postRedisplay "leave linknav"
5266 else
5267 match linknav with
5268 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5269 | Ltexact exact -> doexact exact
5272 let keyboard key mask =
5273 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5274 then wcmd "interrupt"
5275 else state.uioh <- state.uioh#key key mask
5278 let birdseyekeyboard key mask
5279 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5280 let incr =
5281 match conf.columns with
5282 | Csingle _ -> 1
5283 | Cmulti ((c, _, _), _) -> c
5284 | Csplit _ -> failwith "bird's eye split mode"
5286 let pgh layout = List.fold_left
5287 (fun m l -> max l.pageh m) state.winh layout in
5288 let open Keys in
5289 match Wsi.kc2kt key with
5290 | Ascii 'l' when Wsi.withctrl mask ->
5291 let y, h = getpageyh pageno in
5292 let top = (state.winh - h) / 2 in
5293 gotoxy state.x (max 0 (y - top))
5294 | Enter -> leavebirdseye beye false
5295 | Escape -> leavebirdseye beye true
5296 | Up -> upbirdseye incr beye
5297 | Down -> downbirdseye incr beye
5298 | Left -> upbirdseye 1 beye
5299 | Right -> downbirdseye 1 beye
5301 | Prior ->
5302 begin match state.layout with
5303 | l :: _ ->
5304 if l.pagey != 0
5305 then (
5306 state.mode <- Birdseye (
5307 oconf, leftx, l.pageno, hooverpageno, anchor
5309 gotopage1 l.pageno 0;
5311 else (
5312 let layout = layout state.x (state.y-state.winh)
5313 state.winw
5314 (pgh state.layout) in
5315 match layout with
5316 | [] -> gotoxy state.x (clamp (-state.winh))
5317 | l :: _ ->
5318 state.mode <- Birdseye (
5319 oconf, leftx, l.pageno, hooverpageno, anchor
5321 gotopage1 l.pageno 0
5324 | [] -> gotoxy state.x (clamp (-state.winh))
5325 end;
5327 | Next ->
5328 begin match List.rev state.layout with
5329 | l :: _ ->
5330 let layout = layout state.x
5331 (state.y + (pgh state.layout))
5332 state.winw state.winh in
5333 begin match layout with
5334 | [] ->
5335 let incr = l.pageh - l.pagevh in
5336 if incr = 0
5337 then (
5338 state.mode <-
5339 Birdseye (
5340 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5342 G.postRedisplay "birdseye pagedown";
5344 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5346 | l :: _ ->
5347 state.mode <-
5348 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5349 gotopage1 l.pageno 0;
5352 | [] -> gotoxy state.x (clamp state.winh)
5353 end;
5355 | Home ->
5356 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5357 gotopage1 0 0
5359 | End ->
5360 let pageno = state.pagecount - 1 in
5361 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5362 if not (pagevisible state.layout pageno)
5363 then
5364 let h =
5365 match List.rev state.pdims with
5366 | [] -> state.winh
5367 | (_, _, h, _) :: _ -> h
5369 gotoxy
5370 state.x
5371 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5372 else G.postRedisplay "birdseye end";
5374 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5377 let drawpage l =
5378 let color =
5379 match state.mode with
5380 | Textentry _ -> scalecolor 0.4
5381 | LinkNav _ | View -> scalecolor 1.0
5382 | Birdseye (_, _, pageno, hooverpageno, _) ->
5383 if l.pageno = hooverpageno
5384 then scalecolor 0.9
5385 else (
5386 if l.pageno = pageno
5387 then (
5388 let c = scalecolor 1.0 in
5389 GlDraw.color c;
5390 GlDraw.line_width 3.0;
5391 let dispx = l.pagedispx in
5392 linerect
5393 (float (dispx-1)) (float (l.pagedispy-1))
5394 (float (dispx+l.pagevw+1))
5395 (float (l.pagedispy+l.pagevh+1))
5397 GlDraw.line_width 1.0;
5400 else scalecolor 0.8
5403 drawtiles l color;
5406 let postdrawpage l linkindexbase =
5407 match getopaque l.pageno with
5408 | Some opaque ->
5409 if tileready l l.pagex l.pagey
5410 then
5411 let x = l.pagedispx - l.pagex
5412 and y = l.pagedispy - l.pagey in
5413 let hlmask =
5414 match conf.columns with
5415 | Csingle _ | Cmulti _ ->
5416 (if conf.hlinks then 1 else 0)
5417 + (if state.glinks
5418 && not (isbirdseye state.mode) then 2 else 0)
5419 | Csplit _ -> 0
5421 let s =
5422 match state.mode with
5423 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5424 | Textentry _
5425 | Birdseye _
5426 | View
5427 | LinkNav _ -> E.s
5429 Hashtbl.find_all state.prects l.pageno |>
5430 List.iter (fun vals -> drawprect opaque x y vals);
5431 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5432 if n < 0
5433 then (state.redisplay <- true; 0)
5434 else n
5435 else 0
5436 | _ -> 0
5439 let scrollindicator () =
5440 let sbw, ph, sh = state.uioh#scrollph in
5441 let sbh, pw, sw = state.uioh#scrollpw in
5443 let x0,x1,hx0 =
5444 if conf.leftscroll
5445 then (0, sbw, sbw)
5446 else ((state.winw - sbw), state.winw, 0)
5449 Gl.enable `blend;
5450 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5451 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5452 filledrect (float x0) 0. (float x1) (float state.winh);
5453 filledrect
5454 (float hx0) (float (state.winh - sbh))
5455 (float (hx0 + state.winw)) (float state.winh)
5457 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5459 filledrect (float x0) ph (float x1) (ph +. sh);
5460 let pw = pw +. float hx0 in
5461 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5462 Gl.disable `blend;
5465 let showsel () =
5466 match state.mstate with
5467 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5470 | Msel ((x0, y0), (x1, y1)) ->
5471 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5472 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5473 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5474 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5477 let showrects =
5478 function [] -> ()
5479 | rects ->
5480 Gl.enable `blend;
5481 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5482 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5483 List.iter
5484 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5485 List.iter (fun l ->
5486 if l.pageno = pageno
5487 then (
5488 let dx = float (l.pagedispx - l.pagex) in
5489 let dy = float (l.pagedispy - l.pagey) in
5490 let r, g, b, alpha = c in
5491 GlDraw.color (r, g, b) ~alpha;
5492 filledrect2 (x0+.dx) (y0+.dy)
5493 (x1+.dx) (y1+.dy)
5494 (x3+.dx) (y3+.dy)
5495 (x2+.dx) (y2+.dy);
5497 ) state.layout
5498 ) rects
5500 Gl.disable `blend;
5503 let display () =
5504 GlDraw.color (scalecolor2 conf.bgcolor);
5505 GlClear.color (scalecolor2 conf.bgcolor);
5506 GlClear.clear [`color];
5507 List.iter drawpage state.layout;
5508 let rects =
5509 match state.mode with
5510 | LinkNav (Ltexact (pageno, linkno)) ->
5511 begin match getopaque pageno with
5512 | Some opaque ->
5513 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5514 let color = (0.0, 0.0, 0.5, 0.5) in
5515 (pageno, color, (
5516 float x0, float y0,
5517 float x1, float y0,
5518 float x1, float y1,
5519 float x0, float y1)
5520 ) :: state.rects
5521 | None -> state.rects
5523 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5524 | Birdseye _
5525 | Textentry _
5526 | View -> state.rects
5528 showrects rects;
5529 let rec postloop linkindexbase = function
5530 | l :: rest ->
5531 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5532 postloop linkindexbase rest
5533 | [] -> ()
5535 showsel ();
5536 postloop 0 state.layout;
5537 state.uioh#display;
5538 begin match state.mstate with
5539 | Mzoomrect ((x0, y0), (x1, y1)) ->
5540 Gl.enable `blend;
5541 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5542 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5543 filledrect (float x0) (float y0) (float x1) (float y1);
5544 Gl.disable `blend;
5545 | Msel _
5546 | Mpan _
5547 | Mscrolly | Mscrollx
5548 | Mzoom _
5549 | Mnone -> ()
5550 end;
5551 enttext ();
5552 scrollindicator ();
5553 Wsi.swapb ();
5556 let zoomrect x y x1 y1 =
5557 let x0 = min x x1
5558 and x1 = max x x1
5559 and y0 = min y y1 in
5560 let zoom = (float state.w) /. float (x1 - x0) in
5561 let margin =
5562 let simple () =
5563 if state.w < state.winw
5564 then (state.winw - state.w) / 2
5565 else 0
5567 match conf.fitmodel with
5568 | FitWidth | FitProportional -> simple ()
5569 | FitPage ->
5570 match conf.columns with
5571 | Csplit _ ->
5572 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5573 | Cmulti _ | Csingle _ -> simple ()
5575 gotoxy ((state.x + margin) - x0) (state.y + y0);
5576 state.anchor <- getanchor ();
5577 setzoom zoom;
5578 resetmstate ();
5581 let annot inline x y =
5582 match unproject x y with
5583 | Some (opaque, n, ux, uy) ->
5584 let add text =
5585 addannot opaque ux uy text;
5586 wcmd "freepage %s" (~> opaque);
5587 Hashtbl.remove state.pagemap (n, state.gen);
5588 flushtiles ();
5589 gotoxy state.x state.y
5591 if inline
5592 then
5593 let ondone s = add s in
5594 let mode = state.mode in
5595 state.mode <- Textentry (
5596 ("annotation: ", E.s, None, textentry, ondone, true),
5597 fun _ -> state.mode <- mode);
5598 state.text <- E.s;
5599 enttext ();
5600 G.postRedisplay "annot"
5601 else
5602 add @@ getusertext E.s
5603 | _ -> ()
5606 let zoomblock x y =
5607 let g opaque l px py =
5608 match rectofblock opaque px py with
5609 | Some a ->
5610 let x0 = a.(0) -. 20. in
5611 let x1 = a.(1) +. 20. in
5612 let y0 = a.(2) -. 20. in
5613 let zoom = (float state.w) /. (x1 -. x0) in
5614 let pagey = getpagey l.pageno in
5615 let margin = (state.w - l.pagew)/2 in
5616 let nx = -truncate x0 - margin in
5617 gotoxy_and_clear_text nx (pagey + truncate y0);
5618 state.anchor <- getanchor ();
5619 setzoom zoom;
5620 None
5621 | None -> None
5623 match conf.columns with
5624 | Csplit _ ->
5625 impmsg "block zooming does not work properly in split columns mode"
5626 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5629 let scrollx x =
5630 let winw = state.winw - 1 in
5631 let s = float x /. float winw in
5632 let destx = truncate (float (state.w + winw) *. s) in
5633 gotoxy_and_clear_text (winw - destx) state.y;
5634 state.mstate <- Mscrollx;
5637 let scrolly y =
5638 let s = float y /. float state.winh in
5639 let desty = truncate (s *. float (maxy ())) in
5640 gotoxy_and_clear_text state.x desty;
5641 state.mstate <- Mscrolly;
5644 let viewmulticlick clicks x y mask =
5645 let g opaque l px py =
5646 let mark =
5647 match clicks with
5648 | 2 -> Mark_word
5649 | 3 -> Mark_line
5650 | 4 -> Mark_block
5651 | _ -> Mark_page
5653 if markunder opaque px py mark
5654 then (
5655 Some (fun () ->
5656 let dopipe cmd =
5657 match getopaque l.pageno with
5658 | None -> ()
5659 | Some opaque -> pipesel opaque cmd
5661 state.roam <- (fun () -> dopipe conf.paxcmd);
5662 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5665 else None
5667 G.postRedisplay "viewmulticlick";
5668 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5671 let canselect () =
5672 match conf.columns with
5673 | Csplit _ -> false
5674 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5677 let viewmouse button down x y mask =
5678 match button with
5679 | n when (n == 4 || n == 5) && not down ->
5680 if Wsi.withctrl mask
5681 then (
5682 match state.mstate with
5683 | Mzoom (oldn, i, (ftx, fty)) ->
5684 let recenter =
5685 if false
5686 then abs (ftx - x) > 5 || abs (fty - y) > 5
5687 else false
5689 if oldn = n
5690 then (
5691 if i = 2
5692 then
5693 let incr =
5694 match n with
5695 | 5 ->
5696 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5697 | _ ->
5698 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5700 let zoom = conf.zoom -. incr in
5701 if recenter
5702 then pivotzoom ~x ~y zoom
5703 else pivotzoom zoom;
5704 state.mstate <- Mzoom (n, 0, (x, y));
5705 else
5706 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5708 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5710 | Msel _
5711 | Mpan _
5712 | Mscrolly | Mscrollx
5713 | Mzoomrect _
5714 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5716 else (
5717 match state.autoscroll with
5718 | Some step -> setautoscrollspeed step (n=4)
5719 | None ->
5720 if conf.wheelbypage || conf.presentation
5721 then (
5722 if n = 4
5723 then prevpage ()
5724 else nextpage ()
5726 else
5727 let incr =
5728 if n = 4
5729 then -conf.scrollstep
5730 else conf.scrollstep
5732 let incr = incr * 2 in
5733 let y = clamp incr in
5734 gotoxy_and_clear_text state.x y
5737 | n when (n = 6 || n = 7) && not down && canpan () ->
5738 let x =
5739 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5740 gotoxy_and_clear_text x state.y
5742 | 1 when Wsi.withshift mask ->
5743 state.mstate <- Mnone;
5744 if not down
5745 then (
5746 match unproject x y with
5747 | None -> ()
5748 | Some (_, pageno, ux, uy) ->
5749 let cmd = Printf.sprintf
5750 "%s %s %d %d %d"
5751 conf.stcmd state.path pageno ux uy
5753 match spawn cmd [] with
5754 | exception exn ->
5755 impmsg "execution of synctex command(%S) failed: %S"
5756 conf.stcmd @@ exntos exn
5757 | _pid -> ()
5760 | 1 when Wsi.withctrl mask ->
5761 if down
5762 then (
5763 Wsi.setcursor Wsi.CURSOR_FLEUR;
5764 state.mstate <- Mpan (x, y)
5766 else
5767 state.mstate <- Mnone
5769 | 3 ->
5770 if down
5771 then (
5772 if Wsi.withshift mask
5773 then (
5774 annot conf.annotinline x y;
5775 G.postRedisplay "addannot"
5777 else
5778 let p = (x, y) in
5779 Wsi.setcursor Wsi.CURSOR_CYCLE;
5780 state.mstate <- Mzoomrect (p, p)
5782 else (
5783 match state.mstate with
5784 | Mzoomrect ((x0, y0), _) ->
5785 if abs (x-x0) > 10 && abs (y - y0) > 10
5786 then zoomrect x0 y0 x y
5787 else (
5788 resetmstate ();
5789 G.postRedisplay "kill accidental zoom rect";
5791 | Msel _
5792 | Mpan _
5793 | Mscrolly | Mscrollx
5794 | Mzoom _
5795 | Mnone -> resetmstate ()
5798 | 1 when vscrollhit x ->
5799 if down
5800 then
5801 let _, position, sh = state.uioh#scrollph in
5802 if y > truncate position && y < truncate (position +. sh)
5803 then state.mstate <- Mscrolly
5804 else scrolly y
5805 else
5806 state.mstate <- Mnone
5808 | 1 when y > state.winh - hscrollh () ->
5809 if down
5810 then
5811 let _, position, sw = state.uioh#scrollpw in
5812 if x > truncate position && x < truncate (position +. sw)
5813 then state.mstate <- Mscrollx
5814 else scrollx x
5815 else
5816 state.mstate <- Mnone
5818 | 1 when state.bzoom -> if not down then zoomblock x y
5820 | 1 ->
5821 let dest = if down then getunder x y else Unone in
5822 begin match dest with
5823 | Ulinkuri _ ->
5824 gotounder dest
5826 | Unone when down ->
5827 Wsi.setcursor Wsi.CURSOR_FLEUR;
5828 state.mstate <- Mpan (x, y);
5830 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5832 | Unone | Utext _ ->
5833 if down
5834 then (
5835 if canselect ()
5836 then (
5837 state.mstate <- Msel ((x, y), (x, y));
5838 G.postRedisplay "mouse select";
5841 else (
5842 match state.mstate with
5843 | Mnone -> ()
5845 | Mzoom _ | Mscrollx | Mscrolly ->
5846 state.mstate <- Mnone
5848 | Mzoomrect ((x0, y0), _) ->
5849 zoomrect x0 y0 x y
5851 | Mpan _ ->
5852 Wsi.setcursor Wsi.CURSOR_INHERIT;
5853 state.mstate <- Mnone
5855 | Msel ((x0, y0), (x1, y1)) ->
5856 let rec loop = function
5857 | [] -> ()
5858 | l :: rest ->
5859 let inside =
5860 let a0 = l.pagedispy in
5861 let a1 = a0 + l.pagevh in
5862 let b0 = l.pagedispx in
5863 let b1 = b0 + l.pagevw in
5864 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5865 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5867 if inside
5868 then
5869 match getopaque l.pageno with
5870 | Some opaque ->
5871 let dosel cmd () =
5872 match Unix.pipe () with
5873 | exception exn ->
5874 impmsg "cannot create sel pipe: %s" @@
5875 exntos exn;
5876 | (r, w) ->
5877 let clo what fd =
5878 Ne.clo fd (fun msg ->
5879 dolog "%s close failed: %s" what msg)
5881 begin match spawn cmd [r, 0; w, -1] with
5882 | exception exn ->
5883 clo "Msel pipe/w" w;
5884 dolog "cannot execute %S: %s" cmd @@ exntos exn
5885 | _pid ->
5886 copysel w opaque;
5887 G.postRedisplay "copysel";
5888 end;
5889 clo "Msel pipe/r" r;
5891 dosel conf.selcmd ();
5892 state.roam <- dosel conf.paxcmd;
5893 | None -> ()
5894 else loop rest
5896 loop state.layout;
5897 resetmstate ();
5901 | _ -> ()
5904 let birdseyemouse button down x y mask
5905 (conf, leftx, _, hooverpageno, anchor) =
5906 match button with
5907 | 1 when down ->
5908 let rec loop = function
5909 | [] -> ()
5910 | l :: rest ->
5911 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5912 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5913 then (
5914 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5916 else loop rest
5918 loop state.layout
5919 | 3 -> ()
5920 | _ -> viewmouse button down x y mask
5923 let uioh = object
5924 method display = ()
5926 method key key mask =
5927 begin match state.mode with
5928 | Textentry textentry -> textentrykeyboard key mask textentry
5929 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5930 | View -> viewkeyboard key mask
5931 | LinkNav linknav -> linknavkeyboard key mask linknav
5932 end;
5933 state.uioh
5935 method button button bstate x y mask =
5936 begin match state.mode with
5937 | LinkNav _ | View -> viewmouse button bstate x y mask
5938 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5939 | Textentry _ -> ()
5940 end;
5941 state.uioh
5943 method multiclick clicks x y mask =
5944 begin match state.mode with
5945 | LinkNav _ | View -> viewmulticlick clicks x y mask
5946 | Birdseye _ | Textentry _ -> ()
5947 end;
5948 state.uioh
5950 method motion x y =
5951 begin match state.mode with
5952 | Textentry _ -> ()
5953 | View | Birdseye _ | LinkNav _ ->
5954 match state.mstate with
5955 | Mzoom _ | Mnone -> ()
5957 | Mpan (x0, y0) ->
5958 let dx = x - x0
5959 and dy = y0 - y in
5960 state.mstate <- Mpan (x, y);
5961 let x = if canpan () then panbound (state.x + dx) else state.x in
5962 let y = clamp dy in
5963 gotoxy_and_clear_text x y
5965 | Msel (a, _) ->
5966 state.mstate <- Msel (a, (x, y));
5967 G.postRedisplay "motion select";
5969 | Mscrolly ->
5970 let y = min state.winh (max 0 y) in
5971 scrolly y
5973 | Mscrollx ->
5974 let x = min state.winw (max 0 x) in
5975 scrollx x
5977 | Mzoomrect (p0, _) ->
5978 state.mstate <- Mzoomrect (p0, (x, y));
5979 G.postRedisplay "motion zoomrect";
5980 end;
5981 state.uioh
5983 method pmotion x y =
5984 begin match state.mode with
5985 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5986 let rec loop = function
5987 | [] ->
5988 if hooverpageno != -1
5989 then (
5990 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5991 G.postRedisplay "pmotion birdseye no hoover";
5993 | l :: rest ->
5994 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5995 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5996 then (
5997 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5998 G.postRedisplay "pmotion birdseye hoover";
6000 else loop rest
6002 loop state.layout
6004 | Textentry _ -> ()
6006 | LinkNav _ | View ->
6007 match state.mstate with
6008 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6009 | Mnone ->
6010 updateunder x y;
6011 if canselect ()
6012 then
6013 match conf.pax with
6014 | None -> ()
6015 | Some r ->
6016 let past, _, _ = !r in
6017 let now = now () in
6018 let delta = now -. past in
6019 if delta > 0.01
6020 then paxunder x y
6021 else r := (now, x, y)
6022 end;
6023 state.uioh
6025 method infochanged _ = ()
6027 method scrollph =
6028 let maxy = maxy () in
6029 let p, h =
6030 if maxy = 0
6031 then 0.0, float state.winh
6032 else scrollph state.y maxy
6034 vscrollw (), p, h
6036 method scrollpw =
6037 let fwinw = float (state.winw - vscrollw ()) in
6038 let sw =
6039 let sw = fwinw /. float state.w in
6040 let sw = fwinw *. sw in
6041 max sw (float conf.scrollh)
6043 let position =
6044 let maxx = state.w + state.winw in
6045 let x = state.winw - state.x in
6046 let percent = float x /. float maxx in
6047 (fwinw -. sw) *. percent
6049 hscrollh (), position, sw
6051 method modehash =
6052 let modename =
6053 match state.mode with
6054 | LinkNav _ -> "links"
6055 | Textentry _ -> "textentry"
6056 | Birdseye _ -> "birdseye"
6057 | View -> "view"
6059 findkeyhash conf modename
6061 method eformsgs = true
6062 method alwaysscrolly = false
6063 end;;
6065 let addrect pageno r g b a x0 y0 x1 y1 =
6066 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6069 let ract cmds =
6070 let cl = splitatchar cmds ' ' in
6071 let scan s fmt f =
6072 try Scanf.sscanf s fmt f
6073 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6074 cmds @@ exntos exn
6076 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6077 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6078 s pageno r g b a x0 y0 x1 y1;
6079 onpagerect
6080 pageno
6081 (fun w h ->
6082 let _,w1,h1,_ = getpagedim pageno in
6083 let sw = float w1 /. float w
6084 and sh = float h1 /. float h in
6085 let x0s = x0 *. sw
6086 and x1s = x1 *. sw
6087 and y0s = y0 *. sh
6088 and y1s = y1 *. sh in
6089 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6090 let color = (r, g, b, a) in
6091 if conf.verbose then debugrect rect;
6092 state.rects <- (pageno, color, rect) :: state.rects;
6093 G.postRedisplay s;
6096 match cl with
6097 | "reload", "" -> reload ()
6098 | "goto", args ->
6099 scan args "%u %f %f"
6100 (fun pageno x y ->
6101 let cmd, _ = state.geomcmds in
6102 if emptystr cmd
6103 then gotopagexy !wtmode pageno x y
6104 else
6105 let f prevf () =
6106 gotopagexy !wtmode pageno x y;
6107 prevf ()
6109 state.reprf <- f state.reprf
6111 | "goto1", args -> scan args "%u %f" gotopage
6112 | "gotor", args ->
6113 scan args "%S %u"
6114 (fun _filename _pageno ->
6115 failwith "gotounder (Uremote (filename, pageno))")
6116 | "gotord", args ->
6117 scan args "%S %S"
6118 (fun _filename _dest ->
6119 failwith "gotounder (Uremotedest (filename, dest))")
6120 | "rect", args ->
6121 scan args "%u %u %f %f %f %f"
6122 (fun pageno c x0 y0 x1 y1 ->
6123 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6124 rectx "rect" pageno color x0 y0 x1 y1;
6126 | "prect", args ->
6127 scan args "%u %f %f %f %f %f %f %f %f"
6128 (fun pageno r g b alpha x0 y0 x1 y1 ->
6129 addrect pageno r g b alpha x0 y0 x1 y1;
6130 G.postRedisplay "prect"
6132 | "pgoto", args ->
6133 scan args "%u %f %f"
6134 (fun pageno x y ->
6135 let optopaque =
6136 match getopaque pageno with
6137 | Some opaque -> opaque
6138 | None -> ~< E.s
6140 pgoto optopaque pageno x y;
6141 let rec fixx = function
6142 | [] -> ()
6143 | l :: rest ->
6144 if l.pageno = pageno
6145 then gotoxy (state.x - l.pagedispx) state.y
6146 else fixx rest
6148 let layout =
6149 let mult =
6150 match conf.columns with
6151 | Csingle _ | Csplit _ -> 1
6152 | Cmulti ((n, _, _), _) -> n
6154 layout 0 state.y (state.winw * mult) state.winh
6156 fixx layout
6158 | "activatewin", "" -> Wsi.activatewin ()
6159 | "quit", "" -> raise Quit
6160 | "keys", keys ->
6161 begin try
6162 let l = Config.keys_of_string keys in
6163 List.iter (fun (k, m) -> keyboard k m) l
6164 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6165 cmds @@ exntos exn
6167 | "clearrects", "" ->
6168 Hashtbl.clear state.prects;
6169 G.postRedisplay "clearrects"
6170 | _ ->
6171 adderrfmt "remote command"
6172 "error processing remote command: %S\n" cmds;
6175 let remote =
6176 let scratch = Bytes.create 80 in
6177 let buf = Buffer.create 80 in
6178 fun fd ->
6179 match tempfailureretry (Unix.read fd scratch 0) 80 with
6180 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6181 | 0 ->
6182 Unix.close fd;
6183 if Buffer.length buf > 0
6184 then (
6185 let s = Buffer.contents buf in
6186 Buffer.clear buf;
6187 ract s;
6189 None
6190 | n ->
6191 let rec eat ppos =
6192 let nlpos =
6193 match Bytes.index_from scratch ppos '\n' with
6194 | pos -> if pos >= n then -1 else pos
6195 | exception Not_found -> -1
6197 if nlpos >= 0
6198 then (
6199 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6200 let s = Buffer.contents buf in
6201 Buffer.clear buf;
6202 ract s;
6203 eat (nlpos+1);
6205 else (
6206 Buffer.add_subbytes buf scratch ppos (n-ppos);
6207 Some fd
6209 in eat 0
6212 let remoteopen path =
6213 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6214 with exn ->
6215 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6216 None
6219 let () =
6220 let gcconfig = ref E.s in
6221 let trimcachepath = ref E.s in
6222 let rcmdpath = ref E.s in
6223 let pageno = ref None in
6224 let rootwid = ref 0 in
6225 let openlast = ref false in
6226 let doreap = ref false in
6227 let csspath = ref None in
6228 selfexec := Sys.executable_name;
6229 Arg.parse
6230 (Arg.align
6231 [("-p", Arg.String (fun s -> state.password <- s),
6232 "<password> Set password");
6234 ("-f", Arg.String
6235 (fun s ->
6236 Config.fontpath := s;
6237 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6239 "<path> Set path to the user interface font");
6241 ("-c", Arg.String
6242 (fun s ->
6243 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6244 Config.confpath := s),
6245 "<path> Set path to the configuration file");
6247 ("-last", Arg.Set openlast, " Open last document");
6249 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6250 "<page-number> Jump to page");
6252 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6253 "<path> Set path to the trim cache file");
6255 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6256 "<named-destination> Set named destination");
6258 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6259 ("-cxack", Arg.Set cxack, " Cut corners");
6261 ("-remote", Arg.String (fun s -> rcmdpath := s),
6262 "<path> Set path to the source of remote commands");
6264 ("-gc", Arg.Set_string gcconfig,
6265 "<path> Collect garbage with the help of a script");
6267 ("-v", Arg.Unit (fun () ->
6268 Printf.printf
6269 "%s\nconfiguration path: %s\n"
6270 (version ())
6271 Config.defconfpath;
6272 exit 0), " Print version and exit");
6274 ("-css", Arg.String (fun s -> csspath := Some s),
6275 "<path> Set path to the style sheet to use with EPUB/HTML");
6277 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6279 ("-origin", Arg.String (fun s -> state.origin <- s),
6280 "<origin> <undocumented>");
6283 (fun s -> state.path <- s)
6284 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6286 if !wtmode
6287 then selfexec := !selfexec ^ " -wtmode";
6289 let histmode = emptystr state.path && not !openlast in
6291 if not (Config.load !openlast)
6292 then dolog "failed to load configuration";
6294 begin match !pageno with
6295 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6296 | None -> ()
6297 end;
6299 if nonemptystr !gcconfig
6300 then (
6301 let (c, s) =
6302 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6303 | exception exn -> error "socketpair for gc failed: %s" @@ exntos exn
6304 | fds -> fds
6306 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6307 | exception exn -> error "failed to execute gc script: %s" @@ exntos exn
6308 | _pid ->
6309 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6310 Config.gc s;
6311 exit 0
6314 let mu =
6315 object (self)
6316 val mutable m_clicks = 0
6317 val mutable m_click_x = 0
6318 val mutable m_click_y = 0
6319 val mutable m_lastclicktime = infinity
6321 method private cleanup =
6322 state.roam <- noroam;
6323 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6324 method expose = G.postRedisplay "expose"
6325 method visible v =
6326 let name =
6327 match v with
6328 | Wsi.Unobscured -> "unobscured"
6329 | Wsi.PartiallyObscured -> "partiallyobscured"
6330 | Wsi.FullyObscured -> "fullyobscured"
6332 vlog "visibility change %s" name
6333 method display = display ()
6334 method map mapped = vlog "mapped %b" mapped
6335 method reshape w h =
6336 self#cleanup;
6337 reshape w h
6338 method mouse b d x y m =
6339 if d && canselect ()
6340 then (
6342 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6344 m_click_x <- x;
6345 m_click_y <- y;
6346 if b = 1
6347 then (
6348 let t = now () in
6349 if abs x - m_click_x > 10
6350 || abs y - m_click_y > 10
6351 || abs_float (t -. m_lastclicktime) > 0.3
6352 then m_clicks <- 0;
6353 m_clicks <- m_clicks + 1;
6354 m_lastclicktime <- t;
6355 if m_clicks = 1
6356 then (
6357 self#cleanup;
6358 G.postRedisplay "cleanup";
6359 state.uioh <- state.uioh#button b d x y m;
6361 else state.uioh <- state.uioh#multiclick m_clicks x y m
6363 else (
6364 self#cleanup;
6365 m_clicks <- 0;
6366 m_lastclicktime <- infinity;
6367 state.uioh <- state.uioh#button b d x y m
6370 else (
6371 state.uioh <- state.uioh#button b d x y m
6373 method motion x y =
6374 state.mpos <- (x, y);
6375 state.uioh <- state.uioh#motion x y
6376 method pmotion x y =
6377 state.mpos <- (x, y);
6378 state.uioh <- state.uioh#pmotion x y
6379 method key k m =
6380 vlog "k=%#x m=%#x" k m;
6381 let mascm = m land (
6382 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6383 ) in
6384 let keyboard k m =
6385 let x = state.x and y = state.y in
6386 keyboard k m;
6387 if x != state.x || y != state.y then self#cleanup
6389 match state.keystate with
6390 | KSnone ->
6391 let km = k, mascm in
6392 begin
6393 match
6394 let modehash = state.uioh#modehash in
6395 try Hashtbl.find modehash km
6396 with Not_found ->
6397 try Hashtbl.find (findkeyhash conf "global") km
6398 with Not_found -> KMinsrt (k, m)
6399 with
6400 | KMinsrt (k, m) -> keyboard k m
6401 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6402 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6404 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6405 List.iter (fun (k, m) -> keyboard k m) insrt;
6406 state.keystate <- KSnone
6407 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6408 state.keystate <- KSinto (keys, insrt)
6409 | KSinto _ -> state.keystate <- KSnone
6411 method enter x y =
6412 state.mpos <- (x, y);
6413 state.uioh <- state.uioh#pmotion x y
6414 method leave = state.mpos <- (-1, -1)
6415 method winstate wsl = state.winstate <- wsl
6416 method quit = raise Quit
6419 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6421 setbgcol conf.bgcolor;
6422 state.wsfd <- wsfd;
6424 if not @@ List.exists GlMisc.check_extension
6425 [ "GL_ARB_texture_rectangle"
6426 ; "GL_EXT_texture_recangle"
6427 ; "GL_NV_texture_rectangle" ]
6428 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6430 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6431 then (
6432 defconf.sliceheight <- 1024;
6433 defconf.texcount <- 32;
6434 defconf.usepbo <- true;
6437 let cs, ss =
6438 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6439 | exception exn ->
6440 dolog "socketpair failed: %s" @@ exntos exn;
6441 exit 1
6442 | (r, w) ->
6443 cloexec r;
6444 cloexec w;
6445 r, w
6448 setcheckers conf.checkers;
6450 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6452 begin match !csspath with
6453 | None -> ()
6454 | Some "" -> conf.css <- E.s
6455 | Some path ->
6456 let css = filecontents path in
6457 let l = String.length css in
6458 conf.css <-
6459 if substratis css (l-2) "\r\n"
6460 then String.sub css 0 (l-2)
6461 else (if css.[l-1] = '\n'
6462 then String.sub css 0 (l-1)
6463 else css);
6464 end;
6465 init cs (
6466 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6467 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6468 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6470 List.iter GlArray.enable [`texture_coord; `vertex];
6471 state.ss <- ss;
6472 reshape ~firsttime:true winw winh;
6473 state.uioh <- uioh;
6474 if histmode
6475 then (
6476 Wsi.settitle "llpp (history)";
6477 enterhistmode ();
6479 else (
6480 state.text <- "Opening " ^ (mbtoutf8 state.path);
6481 opendoc state.path state.password;
6483 display ();
6484 Wsi.mapwin ();
6485 Wsi.setcursor Wsi.CURSOR_INHERIT;
6486 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6488 let rec reap () =
6489 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6490 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6491 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6492 | 0, _ -> ()
6493 | _pid, _status -> reap ()
6495 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6497 let optrfd =
6498 ref (
6499 if nonemptystr !rcmdpath
6500 then remoteopen !rcmdpath
6501 else None
6505 let rec loop deadline =
6506 if !doreap
6507 then (
6508 doreap := false;
6509 reap ()
6511 let r = [state.ss; state.wsfd] in
6512 let r =
6513 match !optrfd with
6514 | None -> r
6515 | Some fd -> fd :: r
6517 if state.redisplay
6518 then (
6519 state.redisplay <- false;
6520 display ();
6522 let timeout =
6523 let now = now () in
6524 if deadline > now
6525 then (
6526 if deadline = infinity
6527 then ~-.1.0
6528 else max 0.0 (deadline -. now)
6530 else 0.0
6532 let r, _, _ =
6533 try Unix.select r [] [] timeout
6534 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6536 begin match r with
6537 | [] ->
6538 state.ghyll None;
6539 let newdeadline =
6540 if state.ghyll == noghyll
6541 then
6542 match state.autoscroll with
6543 | Some step when step != 0 ->
6544 let y = state.y + step in
6545 let fy = if conf.maxhfit then state.winh else 0 in
6546 let y =
6547 if y < 0
6548 then state.maxy - fy
6549 else if y >= state.maxy - fy then 0 else y
6551 if state.mode = View
6552 then gotoxy_and_clear_text state.x y
6553 else gotoxy state.x y;
6554 deadline +. 0.01
6555 | _ -> infinity
6556 else deadline +. 0.01
6558 loop newdeadline
6560 | l ->
6561 let rec checkfds = function
6562 | [] -> ()
6563 | fd :: rest when fd = state.ss ->
6564 let cmd = rcmd state.ss in
6565 act cmd;
6566 checkfds rest
6568 | fd :: rest when fd = state.wsfd ->
6569 Wsi.readresp fd;
6570 checkfds rest
6572 | fd :: rest when Some fd = !optrfd ->
6573 begin match remote fd with
6574 | None -> optrfd := remoteopen !rcmdpath;
6575 | opt -> optrfd := opt
6576 end;
6577 checkfds rest
6579 | _ :: rest ->
6580 dolog "select returned unknown descriptor";
6581 checkfds rest
6583 checkfds l;
6584 let newdeadline =
6585 let deadline1 =
6586 if deadline = infinity
6587 then now () +. 0.01
6588 else deadline
6590 match state.autoscroll with
6591 | Some step when step != 0 -> deadline1
6592 | _ -> if state.ghyll == noghyll then infinity else deadline1
6594 loop newdeadline
6595 end;
6597 match loop infinity with
6598 | exception Quit ->
6599 Config.save leavebirdseye;
6600 if hasunsavedchanges ()
6601 then save ()
6602 | _ -> error "umpossible - infinity reached"