Beautify
[llpp.git] / main.ml
blob42a50f46d8e1e8e2b7c4600247c33d42ad95a602
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mzoom of (int * int)
42 | Mnone
45 type textentry = string * string * onhist * onkey * ondone
46 and onkey = string -> int -> te
47 and ondone = string -> unit
48 and histcancel = unit -> unit
49 and onhist = ((histcmd -> string) * histcancel) option
50 and histcmd = HCnext | HCprev | HCfirst | HClast
51 and te =
52 | TEstop
53 | TEdone of string
54 | TEcont of string
55 | TEswitch of textentry
58 type 'a circbuf =
59 { store : 'a array
60 ; mutable rc : int
61 ; mutable wc : int
62 ; mutable len : int
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollstep : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscrollstep : int
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
157 ; mutable jumpback : bool
158 ; mutable bgcolor : float * float * float
159 ; mutable bedefault : bool
163 type outline = string * int * int * float;;
164 type outlines =
165 | Oarray of outline array
166 | Olist of outline list
167 | Onarrow of string * outline array * outline array
170 type rect = float * float * float * float * float * float * float * float;;
172 type pagemapkey = pageno * width * angle * proportional * gen;;
174 type anchor = pageno * top;;
176 let emptyanchor = (0, 0.0);;
177 let initialanchor = (-1, nan);;
179 type mode =
180 | Birdseye of (conf * leftx * pageno * pageno * anchor)
181 | Outline of (bool * int * int * outline array * string * int * mode)
182 | Items of (int * int * item array * string * int * mode)
183 | Textentry of (textentry * onleave)
184 | View
185 and onleave = leavetextentrystatus -> unit
186 and leavetextentrystatus = | Cancel | Confirm
187 and item = string * int * action
188 and action =
189 | Noaction
190 | Action of (int -> int -> string -> int -> mode)
193 let isbirdseye = function Birdseye _ -> true | _ -> false;;
194 let istextentry = function Textentry _ -> true | _ -> false;;
196 type state =
197 { mutable csock : Unix.file_descr
198 ; mutable ssock : Unix.file_descr
199 ; mutable w : int
200 ; mutable x : int
201 ; mutable y : int
202 ; mutable anchor : anchor
203 ; mutable maxy : int
204 ; mutable layout : layout list
205 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
206 ; mutable pdims : (pageno * width * height * leftx) list
207 ; mutable pagecount : int
208 ; pagecache : string circbuf
209 ; mutable rendering : bool
210 ; mutable mstate : mstate
211 ; mutable searchpattern : string
212 ; mutable rects : (pageno * recttype * rect) list
213 ; mutable rects1 : (pageno * recttype * rect) list
214 ; mutable text : string
215 ; mutable fullscreen : (width * height) option
216 ; mutable mode : mode
217 ; mutable outlines : outlines
218 ; mutable bookmarks : outline list
219 ; mutable path : string
220 ; mutable password : string
221 ; mutable invalidated : int
222 ; mutable colorscale : float
223 ; mutable memused : int
224 ; mutable gen : gen
225 ; mutable throttle : layout list option
226 ; mutable ascrollstep : int
227 ; mutable help : item array
228 ; mutable docinfo : (int * string) list
229 ; hists : hists
231 and hists =
232 { pat : string circbuf
233 ; pag : string circbuf
234 ; nav : anchor circbuf
238 let defconf =
239 { scrollw = 7
240 ; scrollh = 12
241 ; icase = true
242 ; preload = true
243 ; pagebias = 0
244 ; verbose = false
245 ; scrollstep = 24
246 ; maxhfit = true
247 ; crophack = false
248 ; autoscrollstep = 24
249 ; showall = false
250 ; hlinks = false
251 ; underinfo = false
252 ; interpagespace = 2
253 ; zoom = 1.0
254 ; presentation = false
255 ; angle = 0
256 ; winw = 900
257 ; winh = 900
258 ; savebmarks = true
259 ; proportional = true
260 ; memlimit = 32*1024*1024
261 ; texcount = 256
262 ; sliceheight = 24
263 ; thumbw = 76
264 ; jumpback = false
265 ; bgcolor = (0.5, 0.5, 0.5)
266 ; bedefault = false
270 let conf = { defconf with angle = defconf.angle };;
272 let makehelp () =
273 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
274 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
277 let state =
278 { csock = Unix.stdin
279 ; ssock = Unix.stdin
280 ; x = 0
281 ; y = 0
282 ; anchor = initialanchor
283 ; w = 0
284 ; layout = []
285 ; maxy = max_int
286 ; pagemap = Hashtbl.create 10
287 ; pagecache = cbnew 100 ""
288 ; pdims = []
289 ; pagecount = 0
290 ; rendering = false
291 ; mstate = Mnone
292 ; rects = []
293 ; rects1 = []
294 ; text = ""
295 ; mode = View
296 ; fullscreen = None
297 ; searchpattern = ""
298 ; outlines = Olist []
299 ; bookmarks = []
300 ; path = ""
301 ; password = ""
302 ; invalidated = 0
303 ; hists =
304 { nav = cbnew 100 (0, 0.0)
305 ; pat = cbnew 20 ""
306 ; pag = cbnew 10 ""
308 ; colorscale = 1.0
309 ; memused = 0
310 ; gen = 0
311 ; throttle = None
312 ; ascrollstep = 0
313 ; help = makehelp ()
314 ; docinfo = []
318 let vlog fmt =
319 if conf.verbose
320 then
321 Printf.kprintf prerr_endline fmt
322 else
323 Printf.kprintf ignore fmt
326 let writecmd fd s =
327 let len = String.length s in
328 let n = 4 + len in
329 let b = Buffer.create n in
330 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
331 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
332 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
333 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
334 Buffer.add_string b s;
335 let s' = Buffer.contents b in
336 let n' = Unix.write fd s' 0 n in
337 if n' != n then failwith "write failed";
340 let readcmd fd =
341 let s = "xxxx" in
342 let n = Unix.read fd s 0 4 in
343 if n != 4 then failwith "incomplete read(len)";
344 let len = 0
345 lor (Char.code s.[0] lsl 24)
346 lor (Char.code s.[1] lsl 16)
347 lor (Char.code s.[2] lsl 8)
348 lor (Char.code s.[3] lsl 0)
350 let s = String.create len in
351 let n = Unix.read fd s 0 len in
352 if n != len then failwith "incomplete read(data)";
356 let makecmd s l =
357 let b = Buffer.create 10 in
358 Buffer.add_string b s;
359 let rec combine = function
360 | [] -> b
361 | x :: xs ->
362 Buffer.add_char b ' ';
363 let s =
364 match x with
365 | `b b -> if b then "1" else "0"
366 | `s s -> s
367 | `i i -> string_of_int i
368 | `f f -> string_of_float f
369 | `I f -> string_of_int (truncate f)
371 Buffer.add_string b s;
372 combine xs;
374 combine l;
377 let wcmd s l =
378 let cmd = Buffer.contents (makecmd s l) in
379 writecmd state.csock cmd;
382 let calcips h =
383 if conf.presentation
384 then
385 let d = conf.winh - h in
386 max 0 ((d + 1) / 2)
387 else
388 conf.interpagespace
391 let calcheight () =
392 let rec f pn ph pi fh l =
393 match l with
394 | (n, _, h, _) :: rest ->
395 let ips = calcips h in
396 let fh =
397 if conf.presentation
398 then fh+ips
399 else (
400 if isbirdseye state.mode && pn = 0
401 then fh + ips
402 else fh
405 let fh = fh + ((n - pn) * (ph + pi)) in
406 f n h ips fh rest;
408 | [] ->
409 let inc =
410 if conf.presentation || (isbirdseye state.mode && pn = 0)
411 then 0
412 else -pi
414 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
415 max 0 fh
417 let fh = f 0 0 0 0 state.pdims in
421 let getpageyh pageno =
422 let rec f pn ph pi y l =
423 match l with
424 | (n, _, h, _) :: rest ->
425 let ips = calcips h in
426 if n >= pageno
427 then
428 let h = if n = pageno then h else ph in
429 if conf.presentation && n = pageno
430 then
431 y + (pageno - pn) * (ph + pi) + pi, h
432 else
433 y + (pageno - pn) * (ph + pi), h
434 else
435 let y = y + (if conf.presentation then pi else 0) in
436 let y = y + (n - pn) * (ph + pi) in
437 f n h ips y rest
439 | [] ->
440 y + (pageno - pn) * (ph + pi), ph
442 f 0 0 0 0 state.pdims
445 let getpagey pageno = fst (getpageyh pageno);;
447 let layout y sh =
448 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
449 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
450 match pdims with
451 | (pageno', w, h, x) :: rest when pageno' = pageno ->
452 let ips = calcips h in
453 let yinc =
454 if conf.presentation || (isbirdseye state.mode && pageno = 0)
455 then ips
456 else 0
458 (w, h, ips, x), rest, pdimno + 1, yinc
459 | _ ->
460 prev, pdims, pdimno, 0
462 let dy = dy + yinc in
463 let py = py + yinc in
464 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
465 then
466 accu
467 else
468 let vy = y + dy in
469 if py + h <= vy - yinc
470 then
471 let py = py + h + ips in
472 let dy = max 0 (py - y) in
473 f ~pageno:(pageno+1)
474 ~pdimno
475 ~prev:curr
478 ~pdims:rest
479 ~cacheleft
480 ~accu
481 else
482 let pagey = vy - py in
483 let pagevh = h - pagey in
484 let pagevh = min (sh - dy) pagevh in
485 let off = if yinc > 0 then py - vy else 0 in
486 let py = py + h + ips in
487 let e =
488 { pageno = pageno
489 ; pagedimno = pdimno
490 ; pagew = w
491 ; pageh = h
492 ; pagedispy = dy + off
493 ; pagey = pagey + off
494 ; pagevh = pagevh - off
495 ; pagex = x
498 let accu = e :: accu in
499 f ~pageno:(pageno+1)
500 ~pdimno
501 ~prev:curr
503 ~dy:(dy+pagevh+ips)
504 ~pdims:rest
505 ~cacheleft:(cacheleft-1)
506 ~accu
508 if state.invalidated = 0
509 then (
510 let accu =
512 ~pageno:0
513 ~pdimno:~-1
514 ~prev:(0,0,0,0)
515 ~py:0
516 ~dy:0
517 ~pdims:state.pdims
518 ~cacheleft:(cbcap state.pagecache)
519 ~accu:[]
521 List.rev accu
523 else
527 let clamp incr =
528 let y = state.y + incr in
529 let y = max 0 y in
530 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
534 let getopaque pageno =
535 try Some (Hashtbl.find state.pagemap
536 (pageno, state.w, conf.angle, conf.proportional, state.gen))
537 with Not_found -> None
540 let cache pageno opaque =
541 Hashtbl.replace state.pagemap
542 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
545 let validopaque opaque = String.length opaque > 0;;
547 let render l =
548 match getopaque l.pageno with
549 | None when not state.rendering ->
550 state.rendering <- true;
551 cache l.pageno ("", -1);
552 wcmd "render" [`i (l.pageno + 1)
553 ;`i l.pagedimno
554 ;`i l.pagew
555 ;`i l.pageh];
556 | _ -> ()
559 let loadlayout layout =
560 let rec f all = function
561 | l :: ls ->
562 begin match getopaque l.pageno with
563 | None -> render l; f false ls
564 | Some (opaque, _) -> f (all && validopaque opaque) ls
566 | [] -> all
568 f (layout <> []) layout;
571 let findpageforopaque opaque =
572 Hashtbl.fold
573 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
574 state.pagemap None
577 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
579 let preload () =
580 let oktopreload =
581 if conf.preload
582 then
583 let memleft = conf.memlimit - state.memused in
584 if memleft < 0
585 then
586 let opaque = cbpeek state.pagecache in
587 match findpageforopaque opaque with
588 | Some ((n, _, _, _, _), size) ->
589 memleft + size >= 0 && not (pagevisible state.layout n)
590 | None -> false
591 else true
592 else false
594 if oktopreload
595 then
596 let presentation = conf.presentation in
597 let interpagespace = conf.interpagespace in
598 let maxy = state.maxy in
599 conf.presentation <- false;
600 conf.interpagespace <- 0;
601 state.maxy <- calcheight ();
602 let y =
603 match state.layout with
604 | [] -> 0
605 | l :: _ -> getpagey l.pageno + l.pagey
607 let y = if y < conf.winh then 0 else y - conf.winh in
608 let pages = layout y (conf.winh*3) in
609 List.iter render pages;
610 conf.presentation <- presentation;
611 conf.interpagespace <- interpagespace;
612 state.maxy <- maxy;
615 let gotoy y =
616 let y = max 0 y in
617 let y = min state.maxy y in
618 let pages = layout y conf.winh in
619 let ready = loadlayout pages in
620 if conf.showall
621 then (
622 if ready
623 then (
624 state.y <- y;
625 state.layout <- pages;
626 state.throttle <- None;
627 Glut.postRedisplay ();
629 else (
630 state.throttle <- Some pages;
633 else (
634 state.y <- y;
635 state.layout <- pages;
636 state.throttle <- None;
637 Glut.postRedisplay ();
639 begin match state.mode with
640 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
641 if not (pagevisible pages pageno)
642 then (
643 match state.layout with
644 | [] -> ()
645 | l :: _ ->
646 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
648 | _ -> ()
649 end;
650 preload ();
653 let gotoy_and_clear_text y =
654 gotoy y;
655 if not conf.verbose then state.text <- "";
658 let getanchor () =
659 match state.layout with
660 | [] -> emptyanchor
661 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
664 let getanchory (n, top) =
665 let y, h = getpageyh n in
666 y + (truncate (top *. float h));
669 let gotoanchor anchor =
670 gotoy (getanchory anchor);
673 let addnav () =
674 cbput state.hists.nav (getanchor ());
677 let getnav () =
678 let anchor = cbgetc state.hists.nav ~-1 in
679 getanchory anchor;
682 let gotopage n top =
683 let y, h = getpageyh n in
684 gotoy_and_clear_text (y + (truncate (top *. float h)));
687 let gotopage1 n top =
688 let y = getpagey n in
689 gotoy_and_clear_text (y + top);
692 let invalidate () =
693 state.layout <- [];
694 state.pdims <- [];
695 state.rects <- [];
696 state.rects1 <- [];
697 state.invalidated <- state.invalidated + 1;
700 let scalecolor c =
701 let c = c *. state.colorscale in
702 (c, c, c);
705 let scalecolor2 (r, g, b) =
706 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
709 let represent () =
710 state.maxy <- calcheight ();
711 match state.mode with
712 | Birdseye (_, _, pageno, _, _) ->
713 let y, h = getpageyh pageno in
714 let top = (conf.winh - h) / 2 in
715 gotoy (max 0 (y - top))
716 | _ -> gotoanchor state.anchor
719 let pagematrix () =
720 GlMat.mode `projection;
721 GlMat.load_identity ();
722 GlMat.rotate ~x:1.0 ~angle:180.0 ();
723 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
724 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
725 if state.x != 0
726 then (
727 GlMat.translate ~x:(float state.x) ();
731 let winmatrix () =
732 GlMat.mode `projection;
733 GlMat.load_identity ();
734 GlMat.rotate ~x:1.0 ~angle:180.0 ();
735 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
736 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
739 let reshape ~w ~h =
740 if state.invalidated = 0 && state.anchor == initialanchor
741 then state.anchor <- getanchor ();
743 conf.winw <- w;
744 let w = truncate (float w *. conf.zoom) - conf.scrollw in
745 let w = max w 2 in
746 state.w <- w;
747 conf.winh <- h;
748 GlMat.mode `modelview;
749 GlMat.load_identity ();
750 GlClear.color (scalecolor 1.0);
751 GlClear.clear [`color];
753 invalidate ();
754 wcmd "geometry" [`i w; `i h];
757 let showtext c s =
758 GlDraw.color (0.0, 0.0, 0.0);
759 GlDraw.rect
760 (0.0, float (conf.winh - 18))
761 (float (conf.winw - conf.scrollw - 1), float conf.winh)
763 let font = Glut.BITMAP_8_BY_13 in
764 GlDraw.color (1.0, 1.0, 1.0);
765 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
766 Glut.bitmapCharacter ~font ~c:(Char.code ' ');
767 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
770 let enttext () =
771 let len = String.length state.text in
772 match state.mode with
773 | Textentry ((prefix, text, _, _, _), _) ->
774 let s =
775 match String.length prefix with
776 | 0 | 1 ->
777 if len > 0
778 then
779 Printf.sprintf "%s%s^ [%s]" prefix text state.text
780 else
781 Printf.sprintf "%s%s^" prefix text
783 | _ ->
784 if len > 0
785 then
786 Printf.sprintf "%s: %s^ [%s]" prefix text state.text
787 else
788 Printf.sprintf "%s: %s^" prefix text
790 showtext ' ' s;
792 | _ ->
793 if len > 0 then showtext ' ' state.text
796 let showtext c s =
797 state.text <- Printf.sprintf "%c%s" c s;
798 Glut.postRedisplay ();
801 let act cmd =
802 match cmd.[0] with
803 | 'c' ->
804 state.pdims <- [];
806 | 'D' ->
807 state.rects <- state.rects1;
808 Glut.postRedisplay ()
810 | 'C' ->
811 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
812 state.pagecount <- n;
813 state.invalidated <- state.invalidated - 1;
814 if state.invalidated = 0
815 then represent ()
817 | 't' ->
818 let s = Scanf.sscanf cmd "t %n"
819 (fun n -> String.sub cmd n (String.length cmd - n))
821 Glut.setWindowTitle s
823 | 'T' ->
824 let s = Scanf.sscanf cmd "T %n"
825 (fun n -> String.sub cmd n (String.length cmd - n))
827 if istextentry state.mode
828 then (
829 state.text <- s;
830 showtext ' ' s;
832 else (
833 state.text <- s;
834 Glut.postRedisplay ();
837 | 'V' ->
838 if conf.verbose
839 then
840 let s = Scanf.sscanf cmd "V %n"
841 (fun n -> String.sub cmd n (String.length cmd - n))
843 state.text <- s;
844 showtext ' ' s;
846 | 'F' ->
847 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
848 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
849 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
850 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
852 let y = (getpagey pageno) + truncate y0 in
853 addnav ();
854 gotoy y;
855 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
857 | 'R' ->
858 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
859 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
860 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
861 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
863 state.rects1 <-
864 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
866 | 'r' ->
867 let n, w, h, r, l, s, p =
868 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
869 (fun n w h r l s p ->
870 (n-1, w, h, r, l != 0, s, p))
873 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
874 state.memused <- state.memused + s;
876 let layout =
877 match state.throttle with
878 | None -> state.layout
879 | Some layout -> layout
882 let rec gc () =
883 if (state.memused <= conf.memlimit) || cbempty state.pagecache
884 then ()
885 else (
886 let evictedopaque = cbpeek state.pagecache in
887 match findpageforopaque evictedopaque with
888 | None -> failwith "bug in gc"
889 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
890 if state.gen != gen || not (pagevisible layout evictedn)
891 then (
892 wcmd "free" [`s evictedopaque];
893 state.memused <- state.memused - evictedsize;
894 Hashtbl.remove state.pagemap k;
895 cbdecr state.pagecache;
896 gc ();
900 gc ();
902 cbput state.pagecache p;
903 state.rendering <- false;
905 begin match state.throttle with
906 | None ->
907 if pagevisible state.layout n
908 then gotoy state.y
909 else (
910 let allvisible = loadlayout state.layout in
911 if allvisible then preload ();
914 | Some layout ->
915 match layout with
916 | [] -> ()
917 | l :: _ ->
918 let y = getpagey l.pageno + l.pagey in
919 gotoy y
922 | 'l' ->
923 let (n, w, h, x) as pdim =
924 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
926 state.pdims <- pdim :: state.pdims
928 | 'o' ->
929 let (l, n, t, h, pos) =
930 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
932 let s = String.sub cmd pos (String.length cmd - pos) in
933 let s =
934 let l = String.length s in
935 let b = Buffer.create (String.length s) in
936 let rec loop pc2 i =
937 if i = l
938 then ()
939 else
940 let pc2 =
941 match s.[i] with
942 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
943 | '\xc2' -> true
944 | c ->
945 let c = if Char.code c land 0x80 = 0 then c else '?' in
946 Buffer.add_char b c;
947 false
949 loop pc2 (i+1)
951 loop false 0;
952 Buffer.contents b
954 let outline = (s, l, n, float t /. float h) in
955 let outlines =
956 match state.outlines with
957 | Olist outlines -> Olist (outline :: outlines)
958 | Oarray _ -> Olist [outline]
959 | Onarrow _ -> Olist [outline]
961 state.outlines <- outlines
964 | 'i' ->
965 let s = Scanf.sscanf cmd "i %n"
966 (fun n -> String.sub cmd n (String.length cmd - n))
968 let len = String.length s in
969 let rec fold accu pos =
970 let eolpos =
971 try String.index_from s pos '\n' with Not_found -> len
973 if eolpos = len
974 then List.rev accu
975 else
976 let line = String.sub s pos (eolpos - pos) in
977 fold ((1, line)::accu) (eolpos+1)
979 state.docinfo <- fold state.docinfo 0
981 | _ ->
982 dolog "unknown cmd `%S'" cmd
985 let now = Unix.gettimeofday;;
987 let idle () =
988 let rec loop delay =
989 let r, _, _ = Unix.select [state.csock] [] [] delay in
990 begin match r with
991 | [] ->
992 if state.ascrollstep > 0
993 then begin
994 let y = state.y + state.ascrollstep in
995 let y = if y >= state.maxy then 0 else y in
996 gotoy y;
997 state.text <- "";
998 end;
1000 | _ ->
1001 let cmd = readcmd state.csock in
1002 act cmd;
1003 loop 0.0
1004 end;
1005 in loop 0.001
1008 let onhist cb =
1009 let rc = cb.rc in
1010 let action = function
1011 | HCprev -> cbget cb ~-1
1012 | HCnext -> cbget cb 1
1013 | HCfirst -> cbget cb ~-(cb.rc)
1014 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1015 and cancel () = cb.rc <- rc
1016 in (action, cancel)
1019 let search pattern forward =
1020 if String.length pattern > 0
1021 then
1022 let pn, py =
1023 match state.layout with
1024 | [] -> 0, 0
1025 | l :: _ ->
1026 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1028 let cmd =
1029 let b = makecmd "search"
1030 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1032 Buffer.add_char b ',';
1033 Buffer.add_string b pattern;
1034 Buffer.add_char b '\000';
1035 Buffer.contents b;
1037 writecmd state.csock cmd;
1040 let intentry text key =
1041 let c = Char.unsafe_chr key in
1042 match c with
1043 | '0' .. '9' ->
1044 let s = "x" in s.[0] <- c;
1045 let text = text ^ s in
1046 TEcont text
1048 | _ ->
1049 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1050 TEcont text
1053 let addchar s c =
1054 let b = Buffer.create (String.length s + 1) in
1055 Buffer.add_string b s;
1056 Buffer.add_char b c;
1057 Buffer.contents b;
1060 let textentry text key =
1061 let c = Char.unsafe_chr key in
1062 match c with
1063 | _ when key >= 32 && key < 127 ->
1064 let text = addchar text c in
1065 TEcont text
1067 | _ ->
1068 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1069 TEcont text
1072 let reinit angle proportional =
1073 conf.angle <- angle;
1074 conf.proportional <- proportional;
1075 invalidate ();
1076 wcmd "reinit" [`i angle; `b proportional];
1079 let setzoom zoom =
1080 let zoom = max 0.01 (min 2.2 zoom) in
1081 if zoom <> conf.zoom
1082 then (
1083 if zoom <= 1.0
1084 then state.x <- 0;
1085 conf.zoom <- zoom;
1086 reshape conf.winw conf.winh;
1087 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1091 let enterbirdseye () =
1092 let zoom = float conf.thumbw /. float conf.winw in
1093 let birdseyepageno =
1094 let rec fold candidate = function
1095 | [] -> candidate
1096 | l :: _ when l.pagey = 0 -> l.pageno
1097 | l :: rest -> fold l.pageno rest
1099 fold 0 state.layout
1101 state.mode <- Birdseye (
1102 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1104 conf.zoom <- zoom;
1105 conf.presentation <- false;
1106 conf.interpagespace <- 10;
1107 conf.hlinks <- false;
1108 state.x <- 0;
1109 state.mstate <- Mnone;
1110 conf.showall <- false;
1111 Glut.setCursor Glut.CURSOR_INHERIT;
1112 if conf.verbose
1113 then
1114 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1115 (100.0*.zoom)
1116 else
1117 state.text <- ""
1119 reshape conf.winw conf.winh;
1122 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1123 state.mode <- View;
1124 conf.zoom <- c.zoom;
1125 conf.presentation <- c.presentation;
1126 conf.interpagespace <- c.interpagespace;
1127 conf.showall <- c.showall;
1128 conf.hlinks <- c.hlinks;
1129 state.x <- leftx;
1130 if conf.verbose
1131 then
1132 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1133 (100.0*.conf.zoom)
1135 reshape conf.winw conf.winh;
1136 state.anchor <- if goback then anchor else (pageno, 0.0);
1139 let togglebirdseye () =
1140 match state.mode with
1141 | Birdseye vals -> leavebirdseye vals true
1142 | View | Outline _ -> enterbirdseye ()
1143 | _ -> ()
1146 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1147 let pageno = max 0 (pageno - 1) in
1148 let rec loop = function
1149 | [] -> gotopage1 pageno 0
1150 | l :: _ when l.pageno = pageno ->
1151 if l.pagedispy >= 0 && l.pagey = 0
1152 then Glut.postRedisplay ()
1153 else gotopage1 pageno 0
1154 | _ :: rest -> loop rest
1156 loop state.layout;
1157 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1160 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1161 let pageno = min (state.pagecount - 1) (pageno + 1) in
1162 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1163 let rec loop = function
1164 | [] ->
1165 let y, h = getpageyh pageno in
1166 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1167 gotoy (clamp dy)
1168 | l :: rest when l.pageno = pageno ->
1169 if l.pagevh != l.pageh
1170 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1171 else Glut.postRedisplay ()
1172 | l :: rest -> loop rest
1174 loop state.layout
1177 let optentry mode text key =
1178 let btos b = if b then "on" else "off" in
1179 let c = Char.unsafe_chr key in
1180 match c with
1181 | 's' ->
1182 let ondone s =
1183 try conf.scrollstep <- int_of_string s with exc ->
1184 state.text <- Printf.sprintf "bad integer `%s': %s"
1185 s (Printexc.to_string exc)
1187 TEswitch ("scroll step", "", None, intentry, ondone)
1189 | 'A' ->
1190 let ondone s =
1192 conf.autoscrollstep <- int_of_string s;
1193 if state.ascrollstep > 0
1194 then state.ascrollstep <- conf.autoscrollstep;
1195 with exc ->
1196 state.text <- Printf.sprintf "bad integer `%s': %s"
1197 s (Printexc.to_string exc)
1199 TEswitch ("auto scroll step", "", None, intentry, ondone)
1201 | 'Z' ->
1202 let ondone s =
1204 let zoom = float (int_of_string s) /. 100.0 in
1205 setzoom zoom
1206 with exc ->
1207 state.text <- Printf.sprintf "bad integer `%s': %s"
1208 s (Printexc.to_string exc)
1210 TEswitch ("zoom", "", None, intentry, ondone)
1212 | 't' ->
1213 let ondone s =
1215 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1216 state.text <-
1217 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1218 begin match mode with
1219 | Birdseye beye ->
1220 leavebirdseye beye false;
1221 enterbirdseye ();
1222 | _ -> ();
1224 with exc ->
1225 state.text <- Printf.sprintf "bad integer `%s': %s"
1226 s (Printexc.to_string exc)
1228 TEswitch ("thumbnail width", "", None, intentry, ondone)
1230 | 'R' ->
1231 let ondone s =
1232 match try
1233 Some (int_of_string s)
1234 with exc ->
1235 state.text <- Printf.sprintf "bad integer `%s': %s"
1236 s (Printexc.to_string exc);
1237 None
1238 with
1239 | Some angle -> reinit angle conf.proportional
1240 | None -> ()
1242 TEswitch ("rotation", "", None, intentry, ondone)
1244 | 'i' ->
1245 conf.icase <- not conf.icase;
1246 TEdone ("case insensitive search " ^ (btos conf.icase))
1248 | 'p' ->
1249 conf.preload <- not conf.preload;
1250 gotoy state.y;
1251 TEdone ("preload " ^ (btos conf.preload))
1253 | 'v' ->
1254 conf.verbose <- not conf.verbose;
1255 TEdone ("verbose " ^ (btos conf.verbose))
1257 | 'h' ->
1258 conf.maxhfit <- not conf.maxhfit;
1259 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1260 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1262 | 'c' ->
1263 conf.crophack <- not conf.crophack;
1264 TEdone ("crophack " ^ btos conf.crophack)
1266 | 'a' ->
1267 conf.showall <- not conf.showall;
1268 TEdone ("throttle " ^ btos conf.showall)
1270 | 'f' ->
1271 conf.underinfo <- not conf.underinfo;
1272 TEdone ("underinfo " ^ btos conf.underinfo)
1274 | 'P' ->
1275 conf.savebmarks <- not conf.savebmarks;
1276 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1278 | 'S' ->
1279 let ondone s =
1281 let pageno, py =
1282 match state.layout with
1283 | [] -> 0, 0
1284 | l :: _ ->
1285 l.pageno, l.pagey
1287 conf.interpagespace <- int_of_string s;
1288 state.maxy <- calcheight ();
1289 let y = getpagey pageno in
1290 gotoy (y + py)
1291 with exc ->
1292 state.text <- Printf.sprintf "bad integer `%s': %s"
1293 s (Printexc.to_string exc)
1295 TEswitch ("vertical margin", "", None, intentry, ondone)
1297 | 'l' ->
1298 reinit conf.angle (not conf.proportional);
1299 TEdone ("proprortional display " ^ btos conf.proportional)
1301 | _ ->
1302 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1303 TEstop
1306 let maxoutlinerows () = (conf.winh - 31) / 16;;
1308 let enterselector allowdel outlines errmsg msg =
1309 if Array.length outlines = 0
1310 then (
1311 showtext ' ' errmsg;
1313 else (
1314 state.text <- msg;
1315 Glut.setCursor Glut.CURSOR_INHERIT;
1316 let pageno =
1317 match state.layout with
1318 | [] -> -1
1319 | {pageno=pageno} :: rest -> pageno
1321 let active =
1322 let rec loop n =
1323 if n = Array.length outlines
1324 then 0
1325 else
1326 let (_, _, outlinepageno, _) = outlines.(n) in
1327 if outlinepageno >= pageno then n else loop (n+1)
1329 loop 0
1331 state.mode <- Outline
1332 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1333 state.mode);
1334 Glut.postRedisplay ();
1338 let enteroutlinemode () =
1339 let outlines, msg =
1340 match state.outlines with
1341 | Oarray a -> a, ""
1342 | Olist l ->
1343 let a = Array.of_list (List.rev l) in
1344 state.outlines <- Oarray a;
1345 a, ""
1346 | Onarrow (pat, a, b) ->
1347 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1349 enterselector false outlines "Document has no outline" msg;
1352 let enterbookmarkmode () =
1353 let bookmarks = Array.of_list state.bookmarks in
1354 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1357 let mode_to_string mode =
1358 let b = Buffer.create 10 in
1359 let rec f = function
1360 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1361 | View -> Buffer.add_string b "View"
1362 | Birdseye _ -> Buffer.add_string b "Birdseye"
1363 | Items _ -> Buffer.add_string b "Items"
1364 | Outline _ -> Buffer.add_string b "Outline"
1366 f mode;
1367 Buffer.contents b;
1370 let enterinfomode () =
1371 let btos = function true -> "on" | _ -> "off" in
1372 let mode = state.mode in
1373 let rec makeitems () =
1374 let intp name get set =
1375 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1376 fun active first qsearch pan ->
1377 let ondone s =
1378 let n =
1379 try int_of_string s
1380 with exn ->
1381 state.text <- Printf.sprintf "bad integer `%s': %s"
1382 s (Printexc.to_string exn);
1383 max_int;
1385 if n != max_int then set n;
1387 let te = name, "", None, intentry, ondone in
1388 state.text <- "";
1389 Textentry (
1391 fun _ ->
1392 state.mode <- Items (active, first, makeitems (), "", 0, mode)
1395 and boolp name get set =
1396 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1397 fun active first qsearch pan ->
1398 let v = get () in
1399 set (not v);
1400 Items (active, first, makeitems (), qsearch, pan, mode);
1404 let items = [
1405 "Setup", 0, Noaction;
1407 boolp "presentation"
1408 (fun () -> conf.presentation)
1409 (fun v ->
1410 conf.presentation <- v;
1411 state.anchor <- getanchor ();
1412 represent ());
1414 boolp "ignore case in searches"
1415 (fun () -> conf.icase)
1416 (fun v -> conf.icase <- v);
1418 boolp "preload"
1419 (fun () -> conf.preload)
1420 (fun v -> conf.preload <- v);
1422 boolp "verbose"
1423 (fun () -> conf.verbose)
1424 (fun v -> conf.verbose <- v);
1426 boolp "max fit"
1427 (fun () -> conf.maxhfit)
1428 (fun v -> conf.maxhfit <- v);
1430 boolp "crop hack"
1431 (fun () -> conf.crophack)
1432 (fun v -> conf.crophack <- v);
1434 boolp "throttle"
1435 (fun () -> conf.showall)
1436 (fun v -> conf.showall <- v);
1438 boolp "highlight links"
1439 (fun () -> conf.hlinks)
1440 (fun v -> conf.hlinks <- v);
1442 boolp "under info"
1443 (fun () -> conf.underinfo)
1444 (fun v -> conf.underinfo <- v);
1445 boolp "persistent bookmarks"
1446 (fun () -> conf.savebmarks)
1447 (fun v -> conf.savebmarks <- v);
1449 boolp "proportional display"
1450 (fun () -> conf.proportional)
1451 (fun v -> reinit conf.angle (not conf.proportional));
1453 boolp "persistent location"
1454 (fun () -> conf.jumpback)
1455 (fun v -> conf.jumpback <- v);
1457 "", 0, Noaction;
1459 intp "vertical margin"
1460 (fun () -> conf.interpagespace)
1461 (fun n ->
1462 conf.interpagespace <- n;
1463 let pageno, py =
1464 match state.layout with
1465 | [] -> 0, 0
1466 | l :: _ ->
1467 l.pageno, l.pagey
1469 state.maxy <- calcheight ();
1470 let y = getpagey pageno in
1471 gotoy (y + py)
1474 intp "page bias"
1475 (fun () -> conf.pagebias)
1476 (fun v -> conf.pagebias <- v);
1478 intp "scroll step"
1479 (fun () -> conf.scrollstep)
1480 (fun n -> conf.scrollstep <- n);
1482 intp "auto scroll step"
1483 (fun () ->
1484 if state.ascrollstep > 0
1485 then state.ascrollstep
1486 else conf.autoscrollstep)
1487 (fun n ->
1488 if state.ascrollstep > 0
1489 then state.ascrollstep <- n
1490 else conf.autoscrollstep <- n);
1492 intp "zoom"
1493 (fun () -> truncate (conf.zoom *. 100.))
1494 (fun v -> setzoom ((float v) /. 100.));
1496 intp "rotation"
1497 (fun () -> conf.angle)
1498 (fun v -> reinit v conf.proportional);
1500 intp "scroll bar width"
1501 (fun () -> conf.scrollw)
1502 (fun v ->
1503 conf.scrollw <- v;
1504 reshape conf.winw conf.winh;
1507 intp "scroll handle height"
1508 (fun () -> conf.scrollh)
1509 (fun v -> conf.scrollh <- v;);
1511 intp "thumbnail width"
1512 (fun () -> conf.thumbw)
1513 (fun v ->
1514 conf.thumbw <- min 1920 v;
1515 match mode with
1516 | Birdseye beye ->
1517 leavebirdseye beye false;
1518 enterbirdseye ()
1519 | _ -> ()
1522 "", 0, Noaction;
1523 "Pixmap Cache", 0, Noaction;
1525 intp "size (advisory)"
1526 (fun () -> conf.memlimit)
1527 (fun v -> conf.memlimit <- v);
1528 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1530 "", 0, Noaction;
1531 "Window", 0, Noaction;
1532 Printf.sprintf "dimensions %dx%d" conf.winw conf.winh, 1, Noaction;
1534 "", 0, Noaction;
1536 Printf.sprintf "Save these parameters as defaults at exit (%s)"
1537 (btos conf.bedefault),
1539 Action (
1540 fun active first qsearch pan ->
1541 conf.bedefault <- not conf.bedefault;
1542 Items (active, first, makeitems (), qsearch, pan, mode);
1546 "", 0, Noaction;
1547 "Document", 0, Noaction;
1550 Array.of_list
1551 (items @ List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo);
1553 state.text <- "";
1554 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1555 Glut.postRedisplay ();
1558 let enterhelpmode () =
1559 state.mode <- Items (0, 0, state.help, "", 0, state.mode);
1560 Glut.postRedisplay ();
1563 let quickbookmark ?title () =
1564 match state.layout with
1565 | [] -> ()
1566 | l :: _ ->
1567 let title =
1568 match title with
1569 | None ->
1570 let sec = Unix.gettimeofday () in
1571 let tm = Unix.localtime sec in
1572 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1573 (l.pageno+1)
1574 tm.Unix.tm_mday
1575 tm.Unix.tm_mon
1576 (tm.Unix.tm_year + 1900)
1577 tm.Unix.tm_hour
1578 tm.Unix.tm_min
1579 | Some title -> title
1581 state.bookmarks <-
1582 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1585 let doreshape w h =
1586 state.fullscreen <- None;
1587 Glut.reshapeWindow w h;
1590 let writeopen path password =
1591 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1592 writecmd state.csock "info";
1595 let opendoc path password =
1596 invalidate ();
1597 state.path <- path;
1598 state.password <- password;
1599 state.gen <- state.gen + 1;
1601 writeopen path password;
1602 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1603 wcmd "geometry" [`i state.w; `i conf.winh];
1606 let viewkeyboard ~key ~x ~y =
1607 let enttext te =
1608 let mode = state.mode in
1609 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1610 state.text <- "";
1611 enttext ();
1612 Glut.postRedisplay ()
1614 let c = Char.chr key in
1615 match c with
1616 | '\027' | 'q' ->
1617 exit 0
1619 | '\008' ->
1620 let y = getnav () in
1621 gotoy_and_clear_text y
1623 | 'o' ->
1624 enteroutlinemode ()
1626 | 'u' ->
1627 state.rects <- [];
1628 state.text <- "";
1629 Glut.postRedisplay ()
1631 | '/' | '?' ->
1632 let ondone isforw s =
1633 cbput state.hists.pat s;
1634 state.searchpattern <- s;
1635 search s isforw
1637 let s = String.create 1 in
1638 s.[0] <- c;
1639 enttext (s, "", Some (onhist state.hists.pat),
1640 textentry, ondone (c ='/'))
1642 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1643 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1644 setzoom (min 2.2 (conf.zoom +. incr))
1646 | '+' ->
1647 let ondone s =
1648 let n =
1649 try int_of_string s with exc ->
1650 state.text <- Printf.sprintf "bad integer `%s': %s"
1651 s (Printexc.to_string exc);
1652 max_int
1654 if n != max_int
1655 then (
1656 conf.pagebias <- n;
1657 state.text <- "page bias is now " ^ string_of_int n;
1660 enttext ("page bias", "", None, intentry, ondone)
1662 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1663 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1664 setzoom (max 0.01 (conf.zoom -. decr))
1666 | '-' ->
1667 let ondone msg =
1668 state.text <- msg;
1670 enttext (
1671 "option [acfhilpstvAPRSZ]", "", None,
1672 optentry state.mode, ondone
1675 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1676 setzoom 1.0
1678 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1679 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1680 if zoom < 1.0
1681 then setzoom zoom
1683 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1684 togglebirdseye ()
1686 | '0' .. '9' ->
1687 let ondone s =
1688 let n =
1689 try int_of_string s with exc ->
1690 state.text <- Printf.sprintf "bad integer `%s': %s"
1691 s (Printexc.to_string exc);
1694 if n >= 0
1695 then (
1696 addnav ();
1697 cbput state.hists.pag (string_of_int n);
1698 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1701 let pageentry text key =
1702 match Char.unsafe_chr key with
1703 | 'g' -> TEdone text
1704 | _ -> intentry text key
1706 let text = "x" in text.[0] <- c;
1707 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1709 | 'b' ->
1710 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1711 reshape conf.winw conf.winh;
1713 | 'l' ->
1714 conf.hlinks <- not conf.hlinks;
1715 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1716 Glut.postRedisplay ()
1718 | 'a' ->
1719 if state.ascrollstep = 0
1720 then state.ascrollstep <- conf.autoscrollstep
1721 else (
1722 conf.autoscrollstep <- state.ascrollstep;
1723 state.ascrollstep <- 0;
1726 | 'P' ->
1727 conf.presentation <- not conf.presentation;
1728 showtext ' ' ("presentation mode " ^
1729 if conf.presentation then "on" else "off");
1730 state.anchor <- getanchor ();
1731 represent ()
1733 | 'f' ->
1734 begin match state.fullscreen with
1735 | None ->
1736 state.fullscreen <- Some (conf.winw, conf.winh);
1737 Glut.fullScreen ()
1738 | Some (w, h) ->
1739 state.fullscreen <- None;
1740 doreshape w h
1743 | 'g' ->
1744 gotoy_and_clear_text 0
1746 | 'n' ->
1747 search state.searchpattern true
1749 | 'p' | 'N' ->
1750 search state.searchpattern false
1752 | 't' ->
1753 begin match state.layout with
1754 | [] -> ()
1755 | l :: _ ->
1756 gotoy_and_clear_text (getpagey l.pageno)
1759 | ' ' ->
1760 begin match List.rev state.layout with
1761 | [] -> ()
1762 | l :: _ ->
1763 let pageno = min (l.pageno+1) (state.pagecount-1) in
1764 gotoy_and_clear_text (getpagey pageno)
1767 | '\127' ->
1768 begin match state.layout with
1769 | [] -> ()
1770 | l :: _ ->
1771 let pageno = max 0 (l.pageno-1) in
1772 gotoy_and_clear_text (getpagey pageno)
1775 | '=' ->
1776 let f (fn, ln) l =
1777 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1779 let fn, ln = List.fold_left f (-1, -1) state.layout in
1780 let s =
1781 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1782 let percent =
1783 if maxy <= 0
1784 then 100.
1785 else (100. *. (float state.y /. float maxy)) in
1786 if fn = ln
1787 then
1788 Printf.sprintf "Page %d of %d %.2f%%"
1789 (fn+1) state.pagecount percent
1790 else
1791 Printf.sprintf
1792 "Pages %d-%d of %d %.2f%%"
1793 (fn+1) (ln+1) state.pagecount percent
1795 showtext ' ' s;
1797 | 'w' ->
1798 begin match state.layout with
1799 | [] -> ()
1800 | l :: _ ->
1801 doreshape (l.pagew + conf.scrollw) l.pageh;
1802 Glut.postRedisplay ();
1805 | '\'' ->
1806 enterbookmarkmode ()
1808 | 'h' ->
1809 enterhelpmode ()
1811 | 'i' ->
1812 enterinfomode ()
1814 | 'm' ->
1815 let ondone s =
1816 match state.layout with
1817 | l :: _ ->
1818 state.bookmarks <-
1819 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1820 :: state.bookmarks
1821 | _ -> ()
1823 enttext ("bookmark", "", None, textentry, ondone)
1825 | '~' ->
1826 quickbookmark ();
1827 showtext ' ' "Quick bookmark added";
1829 | 'z' ->
1830 begin match state.layout with
1831 | l :: _ ->
1832 let rect = getpdimrect l.pagedimno in
1833 let w, h =
1834 if conf.crophack
1835 then
1836 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1837 truncate (1.2 *. (rect.(3) -. rect.(0))))
1838 else
1839 (truncate (rect.(1) -. rect.(0)),
1840 truncate (rect.(3) -. rect.(0)))
1842 if w != 0 && h != 0
1843 then
1844 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1846 Glut.postRedisplay ();
1848 | [] -> ()
1851 | '<' | '>' ->
1852 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1854 | '[' | ']' ->
1855 state.colorscale <-
1856 max 0.0
1857 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1858 Glut.postRedisplay ()
1860 | 'k' ->
1861 begin match state.mode with
1862 | Birdseye beye -> upbirdseye beye
1863 | _ -> gotoy (clamp (-conf.scrollstep))
1866 | 'j' ->
1867 begin match state.mode with
1868 | Birdseye beye -> downbirdseye beye
1869 | _ -> gotoy (clamp conf.scrollstep)
1872 | 'r' -> opendoc state.path state.password
1874 | _ ->
1875 vlog "huh? %d %c" key (Char.chr key);
1878 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), onleave) =
1879 let enttext te =
1880 state.mode <- Textentry (te, onleave);
1881 state.text <- "";
1882 enttext ();
1883 Glut.postRedisplay ()
1885 match Char.unsafe_chr key with
1886 | '\008' ->
1887 let len = String.length text in
1888 if len = 0
1889 then (
1890 onleave Cancel;
1891 Glut.postRedisplay ();
1893 else (
1894 let s = String.sub text 0 (len - 1) in
1895 enttext (c, s, opthist, onkey, ondone)
1898 | '\r' | '\n' ->
1899 ondone text;
1900 onleave Confirm;
1901 Glut.postRedisplay ()
1903 | '\027' ->
1904 begin match opthist with
1905 | None -> ()
1906 | Some (_, onhistcancel) -> onhistcancel ()
1907 end;
1908 onleave Cancel;
1909 Glut.postRedisplay ()
1911 | _ ->
1912 begin match onkey text key with
1913 | TEdone text ->
1914 onleave Confirm;
1915 ondone text;
1916 Glut.postRedisplay ()
1918 | TEcont text ->
1919 enttext (c, text, opthist, onkey, ondone);
1921 | TEstop ->
1922 onleave Cancel;
1923 Glut.postRedisplay ()
1925 | TEswitch te ->
1926 state.mode <- Textentry (te, onleave);
1927 Glut.postRedisplay ()
1928 end;
1931 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1932 match key with
1933 | 27 ->
1934 leavebirdseye beye true
1936 | 12 ->
1937 let y, h = getpageyh pageno in
1938 let top = (conf.winh - h) / 2 in
1939 gotoy (max 0 (y - top))
1941 | 13 ->
1942 leavebirdseye beye false
1944 | _ ->
1945 viewkeyboard ~key ~x ~y
1948 let itemskeyboard ~key ~x ~y (active, first, items, qsearch, pan, oldmode) =
1949 let set active first qsearch =
1950 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
1952 let search active pattern incr =
1953 let dosearch re =
1954 let rec loop n =
1955 if n = Array.length items || n = -1
1956 then None
1957 else
1958 let (s, _, _) = items.(n) in
1960 (try ignore (Str.search_forward re s 0); true
1961 with Not_found -> false)
1962 then Some n
1963 else loop (n + incr)
1965 loop active
1968 let re = Str.regexp_case_fold pattern in
1969 dosearch re
1970 with Failure s ->
1971 state.text <- s;
1972 None
1974 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1975 match key with
1976 | 18 | 19 ->
1977 let incr = if key = 18 then -1 else 1 in
1978 let active, first =
1979 match search (active + incr) qsearch incr with
1980 | None ->
1981 state.text <- qsearch ^ " [not found]";
1982 active, first
1983 | Some active ->
1984 state.text <- qsearch;
1985 active, firstof active
1987 set active first qsearch;
1988 Glut.postRedisplay ();
1990 | 8 ->
1991 let len = String.length qsearch in
1992 if len = 0
1993 then ()
1994 else (
1995 if len = 1
1996 then (
1997 state.text <- "";
1998 set active first "";
2000 else
2001 let qsearch = String.sub qsearch 0 (len - 1) in
2002 let active, first =
2003 match search active qsearch ~-1 with
2004 | None ->
2005 state.text <- qsearch ^ " [not found]";
2006 active, first
2007 | Some active ->
2008 state.text <- qsearch;
2009 active, firstof active
2011 set active first qsearch
2013 Glut.postRedisplay ()
2015 | _ when key >= 32 && key < 127 ->
2016 let pattern = addchar qsearch (Char.chr key) in
2017 let active, first =
2018 match search active pattern 1 with
2019 | None ->
2020 state.text <- pattern ^ " [not found]";
2021 active, first
2022 | Some active ->
2023 state.text <- pattern;
2024 active, firstof active
2026 set active first pattern;
2027 Glut.postRedisplay ()
2029 | 27 ->
2030 state.text <- "";
2031 state.mode <- oldmode;
2032 Glut.postRedisplay ();
2034 | 13 ->
2035 if active < Array.length items
2036 then (
2037 match items.(active) with
2038 | _, _, Action f ->
2039 state.mode <- f active first qsearch pan
2041 | _, _, Noaction ->
2042 state.text <- "";
2043 state.mode <- oldmode
2045 Glut.postRedisplay ();
2047 | _ -> dolog "unknown key %d" key
2050 let outlinekeyboard ~key ~x ~y
2051 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2052 let narrow outlines pattern =
2053 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2054 match reopt with
2055 | None -> None
2056 | Some re ->
2057 let rec fold accu n =
2058 if n = -1
2059 then accu
2060 else
2061 let (s, _, _, _) as o = outlines.(n) in
2062 let accu =
2063 if (try ignore (Str.search_forward re s 0); true
2064 with Not_found -> false)
2065 then (o :: accu)
2066 else accu
2068 fold accu (n-1)
2070 let matched = fold [] (Array.length outlines - 1) in
2071 if matched = [] then None else Some (Array.of_list matched)
2073 let search active pattern incr =
2074 let dosearch re =
2075 let rec loop n =
2076 if n = Array.length outlines || n = -1
2077 then None
2078 else
2079 let (s, _, _, _) = outlines.(n) in
2081 (try ignore (Str.search_forward re s 0); true
2082 with Not_found -> false)
2083 then Some n
2084 else loop (n + incr)
2086 loop active
2089 let re = Str.regexp_case_fold pattern in
2090 dosearch re
2091 with Failure s ->
2092 state.text <- s;
2093 None
2095 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2096 match key with
2097 | 27 ->
2098 if String.length qsearch = 0
2099 then (
2100 state.text <- "";
2101 state.mode <- oldmode;
2102 Glut.postRedisplay ();
2104 else (
2105 state.text <- "";
2106 state.mode <- Outline (
2107 allowdel, active, first, outlines, "", pan, oldmode
2109 Glut.postRedisplay ();
2112 | 18 | 19 ->
2113 let incr = if key = 18 then -1 else 1 in
2114 let active, first =
2115 match search (active + incr) qsearch incr with
2116 | None ->
2117 state.text <- qsearch ^ " [not found]";
2118 active, first
2119 | Some active ->
2120 state.text <- qsearch;
2121 active, firstof active
2123 state.mode <- Outline (
2124 allowdel, active, first, outlines, qsearch, pan, oldmode
2126 Glut.postRedisplay ();
2128 | 8 ->
2129 let len = String.length qsearch in
2130 if len = 0
2131 then ()
2132 else (
2133 if len = 1
2134 then (
2135 state.text <- "";
2136 state.mode <- Outline (
2137 allowdel, active, first, outlines, "", pan, oldmode
2140 else
2141 let qsearch = String.sub qsearch 0 (len - 1) in
2142 let active, first =
2143 match search active qsearch ~-1 with
2144 | None ->
2145 state.text <- qsearch ^ " [not found]";
2146 active, first
2147 | Some active ->
2148 state.text <- qsearch;
2149 active, firstof active
2151 state.mode <- Outline (
2152 allowdel, active, first, outlines, qsearch, pan, oldmode
2155 Glut.postRedisplay ()
2157 | 13 ->
2158 if active < Array.length outlines
2159 then (
2160 let (_, _, n, t) = outlines.(active) in
2161 addnav ();
2162 gotopage n t;
2164 state.text <- "";
2165 if allowdel then state.bookmarks <- Array.to_list outlines;
2166 state.mode <- oldmode;
2167 Glut.postRedisplay ();
2169 | _ when key >= 32 && key < 127 ->
2170 let pattern = addchar qsearch (Char.chr key) in
2171 let active, first =
2172 match search active pattern 1 with
2173 | None ->
2174 state.text <- pattern ^ " [not found]";
2175 active, first
2176 | Some active ->
2177 state.text <- pattern;
2178 active, firstof active
2180 state.mode <- Outline (
2181 allowdel, active, first, outlines, pattern, pan, oldmode
2183 Glut.postRedisplay ()
2185 | 14 when not allowdel -> (* ctrl-n *)
2186 if String.length qsearch > 0
2187 then (
2188 let optoutlines = narrow outlines qsearch in
2189 begin match optoutlines with
2190 | None -> state.text <- "can't narrow"
2191 | Some outlines ->
2192 state.mode <- Outline (
2193 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2195 match state.outlines with
2196 | Olist l -> ()
2197 | Oarray a ->
2198 state.outlines <- Onarrow (qsearch, outlines, a)
2199 | Onarrow (pat, a, b) ->
2200 state.outlines <- Onarrow (qsearch, outlines, b)
2201 end;
2203 Glut.postRedisplay ()
2205 | 21 when not allowdel -> (* ctrl-u *)
2206 let outline =
2207 match state.outlines with
2208 | Oarray a -> a
2209 | Olist l ->
2210 let a = Array.of_list (List.rev l) in
2211 state.outlines <- Oarray a;
2213 | Onarrow (pat, a, b) ->
2214 state.outlines <- Oarray b;
2215 state.text <- "";
2218 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2219 Glut.postRedisplay ()
2221 | 12 ->
2222 state.mode <- Outline
2223 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2224 Glut.postRedisplay ()
2226 | 127 when allowdel ->
2227 let len = Array.length outlines - 1 in
2228 if len = 0
2229 then (
2230 state.mode <- View;
2231 state.bookmarks <- [];
2233 else (
2234 let bookmarks = Array.init len
2235 (fun i ->
2236 let i = if i >= active then i + 1 else i in
2237 outlines.(i)
2240 state.mode <-
2241 Outline (
2242 allowdel,
2243 min active (len-1),
2244 min first (len-1),
2245 bookmarks, qsearch,
2247 oldmode
2250 Glut.postRedisplay ()
2252 | _ -> dolog "unknown key %d" key
2255 let keyboard ~key ~x ~y =
2256 if key = 7
2257 then
2258 wcmd "interrupt" []
2259 else
2260 match state.mode with
2261 | Outline outline -> outlinekeyboard ~key ~x ~y outline
2262 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
2263 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
2264 | View -> viewkeyboard ~key ~x ~y
2265 | Items items -> itemskeyboard ~key ~x ~y items
2268 let birdseyespecial key x y
2269 ((conf, leftx, pageno, hooverpageno, anchor) as beye) =
2270 match key with
2271 | Glut.KEY_UP -> upbirdseye beye
2272 | Glut.KEY_DOWN -> downbirdseye beye
2274 | Glut.KEY_PAGE_UP ->
2275 begin match state.layout with
2276 | l :: _ ->
2277 if l.pagey != 0
2278 then (
2279 state.mode <- Birdseye (
2280 conf, leftx, l.pageno, hooverpageno, anchor
2282 gotopage1 l.pageno 0;
2284 else (
2285 let layout = layout (state.y-conf.winh) conf.winh in
2286 match layout with
2287 | [] -> gotoy (clamp (-conf.winh))
2288 | l :: _ ->
2289 state.mode <- Birdseye (
2290 conf, leftx, l.pageno, hooverpageno, anchor
2292 gotopage1 l.pageno 0
2295 | [] -> gotoy (clamp (-conf.winh))
2296 end;
2298 | Glut.KEY_PAGE_DOWN ->
2299 begin match List.rev state.layout with
2300 | l :: _ ->
2301 let layout = layout (state.y + conf.winh) conf.winh in
2302 begin match layout with
2303 | [] ->
2304 let incr = l.pageh - l.pagevh in
2305 if incr = 0
2306 then (
2307 state.mode <-
2308 Birdseye (
2309 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2311 Glut.postRedisplay ();
2313 else gotoy (clamp (incr + conf.interpagespace*2));
2315 | l :: _ ->
2316 state.mode <-
2317 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2318 gotopage1 l.pageno 0;
2321 | [] -> gotoy (clamp conf.winh)
2322 end;
2324 | Glut.KEY_HOME ->
2325 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2326 gotopage1 0 0
2328 | Glut.KEY_END ->
2329 let pageno = state.pagecount - 1 in
2330 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2331 if not (pagevisible state.layout pageno)
2332 then
2333 let h =
2334 match List.rev state.pdims with
2335 | [] -> conf.winh
2336 | (_, _, h, _) :: _ -> h
2338 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2339 else Glut.postRedisplay ();
2340 | _ -> ()
2343 let setautoscrollspeed goingdown =
2344 let incr = max 1 (state.ascrollstep / 2) in
2345 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2346 state.ascrollstep <- astep;
2349 let special ~key ~x ~y =
2350 match state.mode with
2351 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2352 togglebirdseye ()
2354 | Birdseye vals ->
2355 birdseyespecial key x y vals
2357 | View when key = Glut.KEY_F1 ->
2358 enterhelpmode ()
2360 | View ->
2361 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2362 then setautoscrollspeed (key = Glut.KEY_DOWN)
2363 else
2364 let y =
2365 match key with
2366 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2367 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2368 | Glut.KEY_DOWN -> clamp conf.scrollstep
2369 | Glut.KEY_PAGE_UP ->
2370 if Glut.getModifiers () land Glut.active_ctrl != 0
2371 then
2372 match state.layout with
2373 | [] -> state.y
2374 | l :: _ -> state.y - l.pagey
2375 else
2376 clamp (-conf.winh)
2377 | Glut.KEY_PAGE_DOWN ->
2378 if Glut.getModifiers () land Glut.active_ctrl != 0
2379 then
2380 match List.rev state.layout with
2381 | [] -> state.y
2382 | l :: _ -> getpagey l.pageno
2383 else
2384 clamp conf.winh
2385 | Glut.KEY_HOME -> addnav (); 0
2386 | Glut.KEY_END ->
2387 addnav ();
2388 state.maxy - (if conf.maxhfit then conf.winh else 0)
2390 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2391 state.x <- state.x - 10;
2392 state.y
2393 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2394 state.x <- state.x + 10;
2395 state.y
2397 | _ -> state.y
2399 gotoy_and_clear_text y
2401 | Textentry
2402 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
2403 let s =
2404 match key with
2405 | Glut.KEY_UP -> action HCprev
2406 | Glut.KEY_DOWN -> action HCnext
2407 | Glut.KEY_HOME -> action HCfirst
2408 | Glut.KEY_END -> action HClast
2409 | _ -> state.text
2411 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2412 Glut.postRedisplay ()
2414 | Textentry _ -> ()
2416 | Items (active, first, items, qsearch, pan, oldmode) ->
2417 let maxrows = maxoutlinerows () in
2418 let itemcount = Array.length items in
2419 let hasaction = function
2420 | (_, _, Noaction) -> false
2421 | _ -> true
2423 let find start incr =
2424 let rec find i =
2425 if i = -1 || i = itemcount
2426 then -1
2427 else (
2428 if hasaction items.(i)
2429 then i
2430 else find (i + incr)
2433 find start
2435 let set active first =
2436 let first = max 0 (min first (itemcount - maxrows)) in
2437 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2439 let navigate incr =
2440 let isvisible first n = n >= first && n - first <= maxrows in
2441 let active, first =
2442 let incr1 = if incr > 0 then 1 else -1 in
2443 if isvisible first active
2444 then
2445 let next =
2446 let next = active + incr in
2447 let next =
2448 if next < 0 || next >= itemcount
2449 then -1
2450 else find next incr1
2452 if next = -1 || abs (active - next) > maxrows
2453 then -1
2454 else next
2456 if next = -1
2457 then
2458 let first = first + incr in
2459 let first = max 0 (min first (itemcount - 1)) in
2460 let next =
2461 let next = active + incr in
2462 let next = max 0 (min next (itemcount - 1)) in
2463 find next ~-incr1
2465 let active = if next = -1 then active else next in
2466 active, first
2467 else
2468 let first = min next first in
2469 next, first
2470 else
2471 let first = first + incr in
2472 let first = max 0 (min first (itemcount - 1)) in
2473 let active =
2474 let next = active + incr in
2475 let next = max 0 (min next (itemcount - 1)) in
2476 let next = find next incr1 in
2477 if next = -1 || abs (active - first) > maxrows
2478 then active
2479 else next
2481 active, first
2483 set active first;
2484 Glut.postRedisplay ()
2486 begin match key with
2487 | Glut.KEY_UP -> navigate ~-1
2488 | Glut.KEY_DOWN -> navigate 1
2489 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2490 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2492 | Glut.KEY_RIGHT ->
2493 state.mode <- Items (
2494 active, first, items, qsearch, min 0 (pan - 1), oldmode
2496 Glut.postRedisplay ()
2498 | Glut.KEY_LEFT ->
2499 state.mode <- Items (
2500 active, first, items, qsearch, min 0 (pan + 1), oldmode
2502 Glut.postRedisplay ()
2504 | Glut.KEY_HOME ->
2505 let active = find 0 1 in
2506 set active 0;
2507 Glut.postRedisplay ()
2509 | Glut.KEY_END ->
2510 let first = max 0 (itemcount - maxrows) in
2511 let active = find (itemcount - 1) ~-1 in
2512 set active first;
2513 Glut.postRedisplay ()
2515 | _ -> ()
2516 end;
2518 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2519 let maxrows = maxoutlinerows () in
2520 let calcfirst first active =
2521 if active > first
2522 then
2523 let rows = active - first in
2524 if rows > maxrows then active - maxrows else first
2525 else active
2527 let navigate incr =
2528 let active = active + incr in
2529 let active = max 0 (min active (Array.length outlines - 1)) in
2530 let first = calcfirst first active in
2531 state.mode <- Outline (
2532 allowdel, active, first, outlines, qsearch, pan, oldmode
2534 Glut.postRedisplay ()
2536 let updownlevel incr =
2537 let len = Array.length outlines in
2538 let (_, curlevel, _, _) = outlines.(active) in
2539 let rec flow i =
2540 if i = len then i-1 else if i = -1 then 0 else
2541 let (_, l, _, _) = outlines.(i) in
2542 if l != curlevel then i else flow (i+incr)
2544 let active = flow active in
2545 let first = calcfirst first active in
2546 state.mode <- Outline (
2547 allowdel, active, first, outlines, qsearch, pan, oldmode
2549 Glut.postRedisplay ()
2551 match key with
2552 | Glut.KEY_UP -> navigate ~-1
2553 | Glut.KEY_DOWN -> navigate 1
2554 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2555 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2557 | Glut.KEY_RIGHT ->
2558 if Glut.getModifiers () land Glut.active_ctrl != 0
2559 then (
2560 state.mode <- Outline (
2561 allowdel, active, first, outlines,
2562 qsearch, min 0 (pan - 1), oldmode
2564 Glut.postRedisplay ();
2566 else (
2567 if not allowdel
2568 then updownlevel 1
2571 | Glut.KEY_LEFT ->
2572 if Glut.getModifiers () land Glut.active_ctrl != 0
2573 then (
2574 state.mode <- Outline (
2575 allowdel, active, first, outlines, qsearch, pan + 1, oldmode
2577 Glut.postRedisplay ();
2579 else (
2580 if not allowdel
2581 then updownlevel ~-1
2584 | Glut.KEY_HOME ->
2585 state.mode <- Outline (
2586 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2588 Glut.postRedisplay ()
2590 | Glut.KEY_END ->
2591 let active = Array.length outlines - 1 in
2592 let first = max 0 (active - maxrows) in
2593 state.mode <- Outline (
2594 allowdel, active, first, outlines, qsearch, pan, oldmode
2596 Glut.postRedisplay ()
2598 | _ -> ()
2601 let drawplaceholder l =
2602 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2603 GlDraw.rect
2604 (float l.pagex, float l.pagedispy)
2605 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2607 let x = float (if margin < 0 then -margin else l.pagex)
2608 and y = float (l.pagedispy + 13) in
2609 let font = Glut.BITMAP_8_BY_13 in
2610 GlDraw.color (0.0, 0.0, 0.0);
2611 GlPix.raster_pos ~x ~y ();
2612 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2613 ("Loading " ^ string_of_int (l.pageno + 1));
2616 let now () = Unix.gettimeofday ();;
2618 let drawpage l =
2619 let color =
2620 match state.mode with
2621 | Textentry _ -> scalecolor 0.4
2622 | View | Outline _ | Items _ -> scalecolor 1.0
2623 | Birdseye (_, _, pageno, hooverpageno, _) ->
2624 if l.pageno = hooverpageno
2625 then scalecolor 0.9
2626 else (
2627 if l.pageno = pageno
2628 then scalecolor 1.0
2629 else scalecolor 0.8
2632 GlDraw.color color;
2633 begin match getopaque l.pageno with
2634 | Some (opaque, _) when validopaque opaque ->
2635 let a = now () in
2636 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2637 opaque;
2638 let b = now () in
2639 let d = b-.a in
2640 vlog "draw %d %f sec" l.pageno d;
2642 | _ ->
2643 drawplaceholder l;
2644 end;
2647 let scrollph y =
2648 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2649 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2650 let sh = float conf.winh /. sh in
2651 let sh = max sh (float conf.scrollh) in
2653 let percent =
2654 if state.y = state.maxy
2655 then 1.0
2656 else float y /. float maxy
2658 let position = (float conf.winh -. sh) *. percent in
2660 let position =
2661 if position +. sh > float conf.winh
2662 then float conf.winh -. sh
2663 else position
2665 position, sh;
2668 let scrollindicator () =
2669 GlDraw.color (0.64 , 0.64, 0.64);
2670 GlDraw.rect
2671 (float (conf.winw - conf.scrollw), 0.)
2672 (float conf.winw, float conf.winh)
2674 GlDraw.color (0.0, 0.0, 0.0);
2676 let position, sh = scrollph state.y in
2677 GlDraw.rect
2678 (float (conf.winw - conf.scrollw), position)
2679 (float conf.winw, position +. sh)
2683 let showsel margin =
2684 match state.mstate with
2685 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2688 | Msel ((x0, y0), (x1, y1)) ->
2689 let rec loop = function
2690 | l :: ls ->
2691 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2692 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2693 then
2694 match getopaque l.pageno with
2695 | Some (opaque, _) when validopaque opaque ->
2696 let oy = -l.pagey + l.pagedispy in
2697 seltext opaque
2698 (x0 - margin - state.x, y0,
2699 x1 - margin - state.x, y1) oy;
2701 | _ -> ()
2702 else loop ls
2703 | [] -> ()
2705 loop state.layout
2708 let showrects () =
2709 let panx = float state.x in
2710 Gl.enable `blend;
2711 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2712 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2713 List.iter
2714 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2715 List.iter (fun l ->
2716 if l.pageno = pageno
2717 then (
2718 let d = float (l.pagedispy - l.pagey) in
2719 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2720 GlDraw.begins `quads;
2722 GlDraw.vertex2 (x0+.panx, y0+.d);
2723 GlDraw.vertex2 (x1+.panx, y1+.d);
2724 GlDraw.vertex2 (x2+.panx, y2+.d);
2725 GlDraw.vertex2 (x3+.panx, y3+.d);
2727 GlDraw.ends ();
2729 ) state.layout
2730 ) state.rects
2732 Gl.disable `blend;
2735 let showoutline (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2736 Gl.enable `blend;
2737 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2738 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2739 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2740 Gl.disable `blend;
2742 GlDraw.color (1., 1., 1.);
2743 let font = Glut.BITMAP_9_BY_15 in
2744 let draw_string x y s =
2745 GlPix.raster_pos ~x ~y ();
2746 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2748 let rec loop row =
2749 if row = Array.length outlines || (row - first) * 16 > conf.winh
2750 then ()
2751 else (
2752 let (s, level, _, _) = outlines.(row) in
2753 let y = (row - first) * 16 in
2754 let x = 5 + 15*(max 0 (level+pan)) in
2755 if row = active
2756 then (
2757 Gl.enable `blend;
2758 GlDraw.polygon_mode `both `line;
2759 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2760 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2761 GlDraw.rect (0., float (y + 1))
2762 (float (conf.winw - 1), float (y + 18));
2763 GlDraw.polygon_mode `both `fill;
2764 Gl.disable `blend;
2765 GlDraw.color (1., 1., 1.);
2767 let draw_string s =
2768 let l = String.length s in
2769 if pan < 0
2770 then (
2771 let pan = pan * 2 in
2772 let left = l + pan in
2773 if left > 0
2774 then
2775 let s = String.sub s (-pan) left in
2776 draw_string (float x) (float (y + 16)) s
2778 else
2779 draw_string (float (x + pan*15)) (float (y + 16)) s
2781 draw_string s;
2782 loop (row+1)
2785 loop first
2788 let showitems (active, first, items, qsearch, pan, oldmode) =
2789 Gl.enable `blend;
2790 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2791 GlDraw.color (0., 0., 0.) ~alpha:0.90;
2792 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2793 Gl.disable `blend;
2795 GlDraw.color (1., 1., 1.);
2796 let font = Glut.BITMAP_9_BY_15 in
2797 let draw_string x y s =
2798 GlPix.raster_pos ~x ~y ();
2799 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2801 let rec loop row =
2802 if row = Array.length items || (row - first) * 16 > conf.winh
2803 then ()
2804 else (
2805 let (s, l, a) = items.(row) in
2806 let y = (row - first) * 16 in
2807 let x = 5 + (max 0 (l+pan))*15 in
2808 if row = active && a <> Noaction
2809 then (
2810 Gl.enable `blend;
2811 GlDraw.polygon_mode `both `line;
2812 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2813 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2814 GlDraw.rect (0., float (y + 1))
2815 (float (conf.winw - 1), float (y + 18));
2816 GlDraw.polygon_mode `both `fill;
2817 Gl.disable `blend;
2818 GlDraw.color (1., 1., 1.);
2820 let draw_string s =
2821 let l = String.length s in
2822 if pan < 0
2823 then (
2824 let pan = pan * 2 in
2825 let left = l + pan in
2826 if left > 0
2827 then
2828 let s = String.sub s (-pan) left in
2829 draw_string (float x) (float (y + 16)) s
2831 else
2832 draw_string (float (x + pan*15)) (float (y + 16)) s
2834 draw_string s;
2835 loop (row+1)
2838 loop first
2841 let display () =
2842 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2843 GlDraw.viewport margin 0 state.w conf.winh;
2844 pagematrix ();
2845 GlClear.color (scalecolor2 conf.bgcolor);
2846 GlClear.clear [`color];
2847 if conf.zoom > 1.0
2848 then (
2849 Gl.enable `scissor_test;
2850 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2852 List.iter drawpage state.layout;
2853 if conf.zoom > 1.0
2854 then
2855 Gl.disable `scissor_test
2857 if state.x != 0
2858 then (
2859 let x = -.float state.x in
2860 GlMat.translate ~x ();
2862 showrects ();
2863 showsel margin;
2864 GlDraw.viewport 0 0 conf.winw conf.winh;
2865 winmatrix ();
2866 scrollindicator ();
2867 begin match state.mode with
2868 | Items items -> showitems items
2869 | Outline outline -> showoutline outline
2870 | _ -> ()
2871 end;
2872 enttext ();
2873 Glut.swapBuffers ();
2876 let getunder x y =
2877 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2878 let x = x - margin - state.x in
2879 let rec f = function
2880 | l :: rest ->
2881 begin match getopaque l.pageno with
2882 | Some (opaque, _) when validopaque opaque ->
2883 let y = y - l.pagedispy in
2884 if y > 0
2885 then
2886 let y = l.pagey + y in
2887 let x = x - l.pagex in
2888 match whatsunder opaque x y with
2889 | Unone -> f rest
2890 | under -> under
2891 else
2892 f rest
2893 | _ ->
2894 f rest
2896 | [] -> Unone
2898 f state.layout
2901 let viewmouse button bstate x y =
2902 match button with
2903 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2904 if Glut.getModifiers () land Glut.active_ctrl != 0
2905 then (
2906 match state.mstate with
2907 | Mzoom (oldn, i) ->
2908 if oldn = n
2909 then (
2910 if i = 2
2911 then
2912 let incr =
2913 match n with
2914 | 4 ->
2915 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2916 | _ ->
2917 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2919 let zoom = conf.zoom +. incr in
2920 setzoom zoom;
2921 state.mstate <- Mzoom (n, 0);
2922 else
2923 state.mstate <- Mzoom (n, i+1);
2925 else state.mstate <- Mzoom (n, 0)
2927 | _ -> state.mstate <- Mzoom (n, 0)
2929 else (
2930 if state.ascrollstep > 0
2931 then
2932 setautoscrollspeed (n=4)
2933 else
2934 let incr =
2935 if n = 3
2936 then -conf.scrollstep
2937 else conf.scrollstep
2939 let incr = incr * 2 in
2940 let y = clamp incr in
2941 gotoy_and_clear_text y
2944 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2945 if bstate = Glut.DOWN
2946 then (
2947 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2948 state.mstate <- Mpan (x, y)
2950 else
2951 state.mstate <- Mnone
2953 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2954 if bstate = Glut.DOWN
2955 then
2956 let position, sh = scrollph state.y in
2957 if y > truncate position && y < truncate (position +. sh)
2958 then
2959 state.mstate <- Mscroll
2960 else
2961 let percent = float y /. float conf.winh in
2962 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2963 gotoy desty;
2964 state.mstate <- Mscroll
2965 else
2966 state.mstate <- Mnone
2968 | Glut.LEFT_BUTTON ->
2969 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2970 begin match dest with
2971 | Ulinkgoto (pageno, top) ->
2972 if pageno >= 0
2973 then (
2974 addnav ();
2975 gotopage1 pageno top;
2978 | Ulinkuri s ->
2979 print_endline s
2981 | Unone when bstate = Glut.DOWN ->
2982 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2983 state.mstate <- Mpan (x, y);
2985 | Unone | Utext _ ->
2986 if bstate = Glut.DOWN
2987 then (
2988 if conf.angle mod 360 = 0
2989 then (
2990 state.mstate <- Msel ((x, y), (x, y));
2991 Glut.postRedisplay ()
2994 else (
2995 match state.mstate with
2996 | Mnone -> ()
2998 | Mzoom _ | Mscroll ->
2999 state.mstate <- Mnone
3001 | Mpan _ ->
3002 Glut.setCursor Glut.CURSOR_INHERIT;
3003 state.mstate <- Mnone
3005 | Msel ((x0, y0), (x1, y1)) ->
3006 let f l =
3007 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3008 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3009 then
3010 match getopaque l.pageno with
3011 | Some (opaque, _) when validopaque opaque ->
3012 copysel opaque
3013 | _ -> ()
3015 List.iter f state.layout;
3016 copysel ""; (* ugly *)
3017 Glut.setCursor Glut.CURSOR_INHERIT;
3018 state.mstate <- Mnone;
3022 | _ -> ()
3025 let birdseyemouse button bstate x y
3026 (conf, leftx, pageno, hooverpageno, anchor) =
3027 match button with
3028 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3029 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
3030 let rec loop = function
3031 | [] -> ()
3032 | l :: rest ->
3033 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3034 && x > margin && x < margin + l.pagew
3035 then (
3036 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3038 else loop rest
3040 loop state.layout
3041 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3042 | _ -> ()
3045 let mouse bstate button x y =
3046 match state.mode with
3047 | View -> viewmouse button bstate x y
3048 | Birdseye beye -> birdseyemouse button bstate x y beye
3049 | Textentry _ | Outline _ | Items _ -> ()
3052 let mouse ~button ~state ~x ~y = mouse state button x y;;
3054 let motion ~x ~y =
3055 match state.mode with
3056 | Outline _ -> ()
3057 | _ ->
3058 match state.mstate with
3059 | Mzoom _ | Mnone -> ()
3061 | Mpan (x0, y0) ->
3062 let dx = x - x0
3063 and dy = y0 - y in
3064 state.mstate <- Mpan (x, y);
3065 if conf.zoom > 1.0 then state.x <- state.x + dx;
3066 let y = clamp dy in
3067 gotoy_and_clear_text y
3069 | Msel (a, _) ->
3070 state.mstate <- Msel (a, (x, y));
3071 Glut.postRedisplay ()
3073 | Mscroll ->
3074 let y = min conf.winh (max 0 y) in
3075 let percent = float y /. float conf.winh in
3076 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3077 gotoy_and_clear_text y
3080 let pmotion ~x ~y =
3081 match state.mode with
3082 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3083 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
3084 let rec loop = function
3085 | [] ->
3086 if hooverpageno != -1
3087 then (
3088 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3089 Glut.postRedisplay ();
3091 | l :: rest ->
3092 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3093 && x > margin && x < margin + l.pagew
3094 then (
3095 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3096 Glut.postRedisplay ();
3098 else loop rest
3100 loop state.layout
3102 | Outline _ -> ()
3103 | _ ->
3104 match state.mstate with
3105 | Mnone ->
3106 begin match getunder x y with
3107 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3108 | Ulinkuri uri ->
3109 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3110 Glut.setCursor Glut.CURSOR_INFO
3111 | Ulinkgoto (page, y) ->
3112 if conf.underinfo
3113 then showtext 'p' ("age: " ^ string_of_int page);
3114 Glut.setCursor Glut.CURSOR_INFO
3115 | Utext s ->
3116 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3117 Glut.setCursor Glut.CURSOR_TEXT
3120 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3125 module State =
3126 struct
3127 open Parser
3129 let home =
3131 match Sys.os_type with
3132 | "Win32" -> Sys.getenv "HOMEPATH"
3133 | _ -> Sys.getenv "HOME"
3134 with exn ->
3135 prerr_endline
3136 ("Can not determine home directory location: " ^
3137 Printexc.to_string exn);
3141 let color_of_string s =
3142 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3143 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3147 let config_of c attrs =
3148 let apply c k v =
3150 match k with
3151 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
3152 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3153 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3154 | "preload" -> { c with preload = bool_of_string v }
3155 | "page-bias" -> { c with pagebias = int_of_string v }
3156 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3157 | "auto-scroll-step" ->
3158 { c with autoscrollstep = max 0 (int_of_string v) }
3159 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3160 | "crop-hack" -> { c with crophack = bool_of_string v }
3161 | "throttle" -> { c with showall = bool_of_string v }
3162 | "highlight-links" -> { c with hlinks = bool_of_string v }
3163 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3164 | "vertical-margin" ->
3165 { c with interpagespace = max 0 (int_of_string v) }
3166 | "zoom" ->
3167 let zoom = float_of_string v /. 100. in
3168 let zoom = max 0.01 (min 2.2 zoom) in
3169 { c with zoom = zoom }
3170 | "presentation" -> { c with presentation = bool_of_string v }
3171 | "rotation-angle" -> { c with angle = int_of_string v }
3172 | "width" -> { c with winw = max 20 (int_of_string v) }
3173 | "height" -> { c with winh = max 20 (int_of_string v) }
3174 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3175 | "proportional-display" -> { c with proportional = bool_of_string v }
3176 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3177 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3178 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3179 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3180 | "persistent-location" -> { c with jumpback = bool_of_string v }
3181 | "background-color" -> { c with bgcolor = color_of_string v }
3182 | _ -> c
3183 with exn ->
3184 prerr_endline ("Error processing attribute (`" ^
3185 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3188 let rec fold c = function
3189 | [] -> c
3190 | (k, v) :: rest ->
3191 let c = apply c k v in
3192 fold c rest
3194 fold c attrs;
3197 let fromstring f pos n v d =
3198 try f v
3199 with exn ->
3200 dolog "Error processing attribute (%S=%S) at %d\n%s"
3201 n v pos (Printexc.to_string exn)
3206 let bookmark_of attrs =
3207 let rec fold title page rely = function
3208 | ("title", v) :: rest -> fold v page rely rest
3209 | ("page", v) :: rest -> fold title v rely rest
3210 | ("rely", v) :: rest -> fold title page v rest
3211 | _ :: rest -> fold title page rely rest
3212 | [] -> title, page, rely
3214 fold "invalid" "0" "0" attrs
3217 let doc_of attrs =
3218 let rec fold path page rely pan = function
3219 | ("path", v) :: rest -> fold v page rely pan rest
3220 | ("page", v) :: rest -> fold path v rely pan rest
3221 | ("rely", v) :: rest -> fold path page v pan rest
3222 | ("pan", v) :: rest -> fold path page rely v rest
3223 | _ :: rest -> fold path page rely pan rest
3224 | [] -> path, page, rely, pan
3226 fold "" "0" "0" "0" attrs
3229 let setconf dst src =
3230 dst.scrollw <- src.scrollw;
3231 dst.scrollh <- src.scrollh;
3232 dst.icase <- src.icase;
3233 dst.preload <- src.preload;
3234 dst.pagebias <- src.pagebias;
3235 dst.verbose <- src.verbose;
3236 dst.scrollstep <- src.scrollstep;
3237 dst.maxhfit <- src.maxhfit;
3238 dst.crophack <- src.crophack;
3239 dst.autoscrollstep <- src.autoscrollstep;
3240 dst.showall <- src.showall;
3241 dst.hlinks <- src.hlinks;
3242 dst.underinfo <- src.underinfo;
3243 dst.interpagespace <- src.interpagespace;
3244 dst.zoom <- src.zoom;
3245 dst.presentation <- src.presentation;
3246 dst.angle <- src.angle;
3247 dst.winw <- src.winw;
3248 dst.winh <- src.winh;
3249 dst.savebmarks <- src.savebmarks;
3250 dst.memlimit <- src.memlimit;
3251 dst.proportional <- src.proportional;
3252 dst.texcount <- src.texcount;
3253 dst.sliceheight <- src.sliceheight;
3254 dst.thumbw <- src.thumbw;
3255 dst.jumpback <- src.jumpback;
3256 dst.bgcolor <- src.bgcolor;
3259 let unent s =
3260 let l = String.length s in
3261 let b = Buffer.create l in
3262 unent b s 0 l;
3263 Buffer.contents b;
3266 let get s =
3267 let h = Hashtbl.create 10 in
3268 let dc = { defconf with angle = defconf.angle } in
3269 let rec toplevel v t spos epos =
3270 match t with
3271 | Vdata | Vcdata | Vend -> v
3272 | Vopen ("llppconfig", attrs, closed) ->
3273 if closed
3274 then v
3275 else { v with f = llppconfig }
3276 | Vopen _ ->
3277 error "unexpected subelement at top level" s spos
3278 | Vclose tag -> error "unexpected close at top level" s spos
3280 and llppconfig v t spos epos =
3281 match t with
3282 | Vdata | Vcdata | Vend -> v
3283 | Vopen ("defaults", attrs, closed) ->
3284 let c = config_of dc attrs in
3285 setconf dc c;
3286 if closed
3287 then v
3288 else { v with f = skip "defaults" (fun () -> v) }
3290 | Vopen ("doc", attrs, closed) ->
3291 let pathent, spage, srely, span = doc_of attrs in
3292 let path = unent pathent
3293 and pageno = fromstring int_of_string spos "page" spage 0
3294 and rely = fromstring float_of_string spos "rely" srely 0.0
3295 and pan = fromstring int_of_string spos "pan" span 0 in
3296 let c = config_of dc attrs in
3297 let anchor = (pageno, rely) in
3298 if closed
3299 then (Hashtbl.add h path (c, [], pan, anchor); v)
3300 else { v with f = doc path pan anchor c [] }
3302 | Vopen (tag, _, closed) ->
3303 error "unexpected subelement in llppconfig" s spos
3305 | Vclose "llppconfig" -> { v with f = toplevel }
3306 | Vclose tag -> error "unexpected close in llppconfig" s spos
3308 and doc path pan anchor c bookmarks v t spos epos =
3309 match t with
3310 | Vdata | Vcdata -> v
3311 | Vend -> error "unexpected end of input in doc" s spos
3312 | Vopen ("bookmarks", attrs, closed) ->
3313 { v with f = pbookmarks path pan anchor c bookmarks }
3315 | Vopen (tag, _, _) ->
3316 error "unexpected subelement in doc" s spos
3318 | Vclose "doc" ->
3319 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3320 { v with f = llppconfig }
3322 | Vclose tag -> error "unexpected close in doc" s spos
3324 and pbookmarks path pan anchor c bookmarks v t spos epos =
3325 match t with
3326 | Vdata | Vcdata -> v
3327 | Vend -> error "unexpected end of input in bookmarks" s spos
3328 | Vopen ("item", attrs, closed) ->
3329 let titleent, spage, srely = bookmark_of attrs in
3330 let page = fromstring int_of_string spos "page" spage 0
3331 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3332 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
3333 if closed
3334 then { v with f = pbookmarks path pan anchor c bookmarks }
3335 else
3336 let f () = v in
3337 { v with f = skip "item" f }
3339 | Vopen _ ->
3340 error "unexpected subelement in bookmarks" s spos
3342 | Vclose "bookmarks" ->
3343 { v with f = doc path pan anchor c bookmarks }
3345 | Vclose tag -> error "unexpected close in bookmarks" s spos
3347 and skip tag f v t spos epos =
3348 match t with
3349 | Vdata | Vcdata -> v
3350 | Vend ->
3351 error ("unexpected end of input in skipped " ^ tag) s spos
3352 | Vopen (tag', _, closed) ->
3353 if closed
3354 then v
3355 else
3356 let f' () = { v with f = skip tag f } in
3357 { v with f = skip tag' f' }
3358 | Vclose ctag ->
3359 if tag = ctag
3360 then f ()
3361 else error ("unexpected close in skipped " ^ tag) s spos
3364 parse { f = toplevel; accu = () } s;
3365 h, dc;
3368 let do_load f ic =
3370 let len = in_channel_length ic in
3371 let s = String.create len in
3372 really_input ic s 0 len;
3373 f s;
3374 with
3375 | Parse_error (msg, s, pos) ->
3376 let subs = subs s pos in
3377 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3378 failwith ("parse error: " ^ s)
3380 | exn ->
3381 failwith ("config load error: " ^ Printexc.to_string exn)
3384 let path =
3385 let dir =
3387 let dir = Filename.concat home ".config" in
3388 if Sys.is_directory dir then dir else home
3389 with _ -> home
3391 Filename.concat dir "llpp.conf"
3394 let load1 f =
3395 if Sys.file_exists path
3396 then
3397 match
3398 (try Some (open_in_bin path)
3399 with exn ->
3400 prerr_endline
3401 ("Error opening configuation file `" ^ path ^ "': " ^
3402 Printexc.to_string exn);
3403 None
3405 with
3406 | Some ic ->
3407 begin try
3408 f (do_load get ic)
3409 with exn ->
3410 prerr_endline
3411 ("Error loading configuation from `" ^ path ^ "': " ^
3412 Printexc.to_string exn);
3413 end;
3414 close_in ic;
3416 | None -> ()
3417 else
3418 f (Hashtbl.create 0, defconf)
3421 let load () =
3422 let f (h, dc) =
3423 let pc, pb, px, pa =
3425 Hashtbl.find h (Filename.basename state.path)
3426 with Not_found -> dc, [], 0, (0, 0.0)
3428 setconf defconf dc;
3429 setconf conf pc;
3430 state.bookmarks <- pb;
3431 state.x <- px;
3432 if conf.jumpback
3433 then state.anchor <- pa;
3434 cbput state.hists.nav pa;
3436 load1 f
3439 let add_attrs bb always dc c =
3440 let ob s a b =
3441 if always || a != b
3442 then Printf.bprintf bb "\n %s='%b'" s a
3443 and oi s a b =
3444 if always || a != b
3445 then Printf.bprintf bb "\n %s='%d'" s a
3446 and oz s a b =
3447 if always || a <> b
3448 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3449 and oc s a b =
3450 if always || a <> b
3451 then
3452 let r, g, b = a in
3453 let r = truncate (r *. 256.0)
3454 and g = truncate (r *. 256.0)
3455 and b = truncate (r *. 256.0) in
3456 Printf.bprintf bb "\n %s='%d/%d/%d'" s r g b
3458 let w, h =
3459 if always
3460 then dc.winw, dc.winh
3461 else
3462 match state.fullscreen with
3463 | Some wh -> wh
3464 | None -> c.winw, c.winh
3466 let zoom, presentation, interpagespace, showall=
3467 if always
3468 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3469 else
3470 match state.mode with
3471 | Birdseye (bc, _, _, _, _) ->
3472 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3473 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3475 oi "width" w dc.winw;
3476 oi "height" h dc.winh;
3477 oi "scroll-bar-width" c.scrollw dc.scrollw;
3478 oi "scroll-handle-height" c.scrollh dc.scrollh;
3479 ob "case-insensitive-search" c.icase dc.icase;
3480 ob "preload" c.preload dc.preload;
3481 oi "page-bias" c.pagebias dc.pagebias;
3482 oi "scroll-step" c.scrollstep dc.scrollstep;
3483 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3484 ob "max-height-fit" c.maxhfit dc.maxhfit;
3485 ob "crop-hack" c.crophack dc.crophack;
3486 ob "throttle" showall dc.showall;
3487 ob "highlight-links" c.hlinks dc.hlinks;
3488 ob "under-cursor-info" c.underinfo dc.underinfo;
3489 oi "vertical-margin" interpagespace dc.interpagespace;
3490 oz "zoom" zoom dc.zoom;
3491 ob "presentation" presentation dc.presentation;
3492 oi "rotation-angle" c.angle dc.angle;
3493 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3494 ob "proportional-display" c.proportional dc.proportional;
3495 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3496 oi "texcount" c.texcount dc.texcount;
3497 oi "slice-height" c.sliceheight dc.sliceheight;
3498 oi "thumbnail-width" c.thumbw dc.thumbw;
3499 ob "persistent-location" c.jumpback dc.jumpback;
3500 oc "background-color" c.bgcolor dc.bgcolor;
3503 let save () =
3504 let bb = Buffer.create 32768 in
3505 let f (h, dc) =
3506 let dc = if conf.bedefault then conf else dc in
3507 Buffer.add_string bb "<llppconfig>\n<defaults ";
3508 add_attrs bb true dc dc;
3509 Buffer.add_string bb "/>\n";
3511 let adddoc path pan anchor c bookmarks =
3512 if bookmarks == [] && c = dc && anchor = emptyanchor
3513 then ()
3514 else (
3515 Printf.bprintf bb "<doc path='%s'"
3516 (enent path 0 (String.length path));
3518 if anchor <> emptyanchor
3519 then (
3520 let n, y = anchor in
3521 Printf.bprintf bb " page='%d'" n;
3522 if y > 1e-6
3523 then
3524 Printf.bprintf bb " rely='%f'" y
3528 if pan != 0
3529 then Printf.bprintf bb " pan='%d'" pan;
3531 add_attrs bb false dc c;
3533 begin match bookmarks with
3534 | [] -> Buffer.add_string bb "/>\n"
3535 | _ ->
3536 Buffer.add_string bb ">\n<bookmarks>\n";
3537 List.iter (fun (title, _level, page, rely) ->
3538 Printf.bprintf bb
3539 "<item title='%s' page='%d'"
3540 (enent title 0 (String.length title))
3541 page
3543 if rely > 1e-6
3544 then
3545 Printf.bprintf bb " rely='%f'" rely
3547 Buffer.add_string bb "/>\n";
3548 ) bookmarks;
3549 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3550 end;
3554 let pan =
3555 match state.mode with
3556 | Birdseye (_, pan, _, _, _) -> pan
3557 | _ -> state.x
3559 let basename = Filename.basename state.path in
3560 adddoc basename pan (getanchor ())
3561 { conf with
3562 autoscrollstep =
3563 if state.ascrollstep > 0
3564 then state.ascrollstep
3565 else conf.autoscrollstep }
3566 (if conf.savebmarks then state.bookmarks else []);
3568 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3569 if basename <> path
3570 then adddoc path x y c bookmarks
3571 ) h;
3572 Buffer.add_string bb "</llppconfig>";
3574 load1 f;
3575 if Buffer.length bb > 0
3576 then
3578 let tmp = path ^ ".tmp" in
3579 let oc = open_out_bin tmp in
3580 Buffer.output_buffer oc bb;
3581 close_out oc;
3582 Sys.rename tmp path;
3583 with exn ->
3584 prerr_endline
3585 ("error while saving configuration: " ^ Printexc.to_string exn)
3587 end;;
3589 let () =
3590 Arg.parse
3591 (Arg.align
3592 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3593 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3594 " Print version and exit")]
3596 (fun s -> state.path <- s)
3597 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3599 if String.length state.path = 0
3600 then (prerr_endline "file name missing"; exit 1);
3602 State.load ();
3604 let _ = Glut.init Sys.argv in
3605 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3606 let () = Glut.initWindowSize conf.winw conf.winh in
3607 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3609 let csock, ssock =
3610 if Sys.os_type = "Unix"
3611 then
3612 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3613 else
3614 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3615 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3616 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3617 Unix.bind sock addr;
3618 Unix.listen sock 1;
3619 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3620 Unix.connect csock addr;
3621 let ssock, _ = Unix.accept sock in
3622 Unix.close sock;
3623 let opts sock =
3624 Unix.setsockopt sock Unix.TCP_NODELAY true;
3625 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3627 opts ssock;
3628 opts csock;
3629 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3630 ssock, csock
3633 let () = Glut.displayFunc display in
3634 let () = Glut.reshapeFunc reshape in
3635 let () = Glut.keyboardFunc keyboard in
3636 let () = Glut.specialFunc special in
3637 let () = Glut.idleFunc (Some idle) in
3638 let () = Glut.mouseFunc mouse in
3639 let () = Glut.motionFunc motion in
3640 let () = Glut.passiveMotionFunc pmotion in
3642 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3643 state.csock <- csock;
3644 state.ssock <- ssock;
3645 state.text <- "Opening " ^ state.path;
3646 writeopen state.path state.password;
3648 at_exit State.save;
3650 let rec handlelablglutbug () =
3652 Glut.mainLoop ();
3653 with Glut.BadEnum "key in special_of_int" ->
3654 showtext '!' " LablGlut bug: special key not recognized";
3655 handlelablglutbug ()
3657 handlelablglutbug ();