Refactor
[llpp.git] / main.ml
blobf334bb8706a9fe2b36e61a8ea83184a14ecf3f6d
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 * fontpath
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
25 and fontpath = string
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstring : int -> int -> int -> string -> unit = "ml_draw_string";;
38 type mpos = int * int
39 and mstate =
40 | Msel of (mpos * mpos)
41 | Mpan of mpos
42 | Mscroll
43 | Mzoom of (int * int)
44 | Mnone
47 type textentry = string * string * onhist option * onkey * ondone
48 and onkey = string -> int -> te
49 and ondone = string -> unit
50 and histcancel = unit -> unit
51 and onhist = ((histcmd -> string) * histcancel)
52 and histcmd = HCnext | HCprev | HCfirst | HClast
53 and te =
54 | TEstop
55 | TEdone of string
56 | TEcont of string
57 | TEswitch of textentry
60 type 'a circbuf =
61 { store : 'a array
62 ; mutable rc : int
63 ; mutable wc : int
64 ; mutable len : int
68 let cbnew n v =
69 { store = Array.create n v
70 ; rc = 0
71 ; wc = 0
72 ; len = 0
76 let cbcap b = Array.length b.store;;
78 let cbput b v =
79 let cap = cbcap b in
80 b.store.(b.wc) <- v;
81 b.wc <- (b.wc + 1) mod cap;
82 b.rc <- b.wc;
83 b.len <- min (b.len + 1) cap;
86 let cbempty b = b.len = 0;;
88 let cbgetg b circular dir =
89 if cbempty b
90 then b.store.(0)
91 else
92 let rc = b.rc + dir in
93 let rc =
94 if circular
95 then (
96 if rc = -1
97 then b.len-1
98 else (
99 if rc = b.len
100 then 0
101 else rc
104 else max 0 (min rc (b.len-1))
106 b.rc <- rc;
107 b.store.(rc);
110 let cbget b = cbgetg b false;;
111 let cbgetc b = cbgetg b true;;
113 let cbpeek b =
114 let rc = b.wc - b.len in
115 let rc = if rc < 0 then cbcap b + rc else rc in
116 b.store.(rc);
119 let cbdecr b = b.len <- b.len - 1;;
121 type layout =
122 { pageno : int
123 ; pagedimno : int
124 ; pagew : int
125 ; pageh : int
126 ; pagedispy : int
127 ; pagey : int
128 ; pagevh : int
129 ; pagex : int
133 type conf =
134 { mutable scrollbw : int
135 ; mutable scrollh : int
136 ; mutable icase : bool
137 ; mutable preload : bool
138 ; mutable pagebias : int
139 ; mutable verbose : bool
140 ; mutable scrollstep : int
141 ; mutable maxhfit : bool
142 ; mutable crophack : bool
143 ; mutable autoscrollstep : int
144 ; mutable showall : bool
145 ; mutable hlinks : bool
146 ; mutable underinfo : bool
147 ; mutable interpagespace : interpagespace
148 ; mutable zoom : float
149 ; mutable presentation : bool
150 ; mutable angle : angle
151 ; mutable winw : int
152 ; mutable winh : int
153 ; mutable savebmarks : bool
154 ; mutable proportional : proportional
155 ; mutable memlimit : int
156 ; mutable texcount : texcount
157 ; mutable sliceheight : sliceheight
158 ; mutable thumbw : width
159 ; mutable jumpback : bool
160 ; mutable bgcolor : float * float * float
161 ; mutable bedefault : bool
162 ; mutable scrollbarinpm : bool
163 ; mutable uifont : string
167 type anchor = pageno * top;;
169 type outline = string * int * anchor
170 and outlines =
171 | Oarray of outline array
172 | Olist of outline list
173 | Onarrow of string * outline array * outline array
176 type rect = float * float * float * float * float * float * float * float;;
178 type pagemapkey = pageno * width * angle * proportional * gen;;
180 let emptyanchor = (0, 0.0);;
181 let initialanchor = (-1, nan);;
183 type mode =
184 | Birdseye of (conf * leftx * pageno * pageno * anchor)
185 | Outline of (bool * int * int * outline array * string * int * mode)
186 | Items of (int * int * item array * string * int * mode)
187 | Textentry of (textentry * onleave)
188 | View
189 and onleave = leavetextentrystatus -> unit
190 and leavetextentrystatus = | Cancel | Confirm
191 and item = string * int * action
192 and action =
193 | Noaction
194 | Action of (int -> int -> string -> int -> mode)
197 let isbirdseye = function Birdseye _ -> true | _ -> false;;
198 let istextentry = function Textentry _ -> true | _ -> false;;
200 type state =
201 { mutable csock : Unix.file_descr
202 ; mutable ssock : Unix.file_descr
203 ; mutable w : int
204 ; mutable x : int
205 ; mutable y : int
206 ; mutable scrollw : int
207 ; mutable anchor : anchor
208 ; mutable maxy : int
209 ; mutable layout : layout list
210 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
211 ; mutable pdims : (pageno * width * height * leftx) list
212 ; mutable pagecount : int
213 ; pagecache : string circbuf
214 ; mutable rendering : bool
215 ; mutable mstate : mstate
216 ; mutable searchpattern : string
217 ; mutable rects : (pageno * recttype * rect) list
218 ; mutable rects1 : (pageno * recttype * rect) list
219 ; mutable text : string
220 ; mutable fullscreen : (width * height) option
221 ; mutable mode : mode
222 ; mutable outlines : outlines
223 ; mutable bookmarks : outline list
224 ; mutable path : string
225 ; mutable password : string
226 ; mutable invalidated : int
227 ; mutable colorscale : float
228 ; mutable memused : int
229 ; mutable gen : gen
230 ; mutable throttle : layout list option
231 ; mutable ascrollstep : int
232 ; mutable help : item array
233 ; mutable docinfo : (int * string) list
234 ; hists : hists
236 and hists =
237 { pat : string circbuf
238 ; pag : string circbuf
239 ; nav : anchor circbuf
243 let defconf =
244 { scrollbw = 7
245 ; scrollh = 12
246 ; icase = true
247 ; preload = true
248 ; pagebias = 0
249 ; verbose = false
250 ; scrollstep = 24
251 ; maxhfit = true
252 ; crophack = false
253 ; autoscrollstep = 24
254 ; showall = false
255 ; hlinks = false
256 ; underinfo = false
257 ; interpagespace = 2
258 ; zoom = 1.0
259 ; presentation = false
260 ; angle = 0
261 ; winw = 900
262 ; winh = 900
263 ; savebmarks = true
264 ; proportional = true
265 ; memlimit = 32*1024*1024
266 ; texcount = 256
267 ; sliceheight = 24
268 ; thumbw = 76
269 ; jumpback = false
270 ; bgcolor = (0.5, 0.5, 0.5)
271 ; bedefault = false
272 ; scrollbarinpm = true
273 ; uifont = ""
277 let conf = { defconf with angle = defconf.angle };;
279 let makehelp () =
280 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
281 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
284 let state =
285 { csock = Unix.stdin
286 ; ssock = Unix.stdin
287 ; x = 0
288 ; y = 0
289 ; w = 0
290 ; scrollw = 0
291 ; anchor = initialanchor
292 ; layout = []
293 ; maxy = max_int
294 ; pagemap = Hashtbl.create 10
295 ; pagecache = cbnew 100 ""
296 ; pdims = []
297 ; pagecount = 0
298 ; rendering = false
299 ; mstate = Mnone
300 ; rects = []
301 ; rects1 = []
302 ; text = ""
303 ; mode = View
304 ; fullscreen = None
305 ; searchpattern = ""
306 ; outlines = Olist []
307 ; bookmarks = []
308 ; path = ""
309 ; password = ""
310 ; invalidated = 0
311 ; hists =
312 { nav = cbnew 100 (0, 0.0)
313 ; pat = cbnew 20 ""
314 ; pag = cbnew 10 ""
316 ; colorscale = 1.0
317 ; memused = 0
318 ; gen = 0
319 ; throttle = None
320 ; ascrollstep = 0
321 ; help = makehelp ()
322 ; docinfo = []
326 let vlog fmt =
327 if conf.verbose
328 then
329 Printf.kprintf prerr_endline fmt
330 else
331 Printf.kprintf ignore fmt
334 let writecmd fd s =
335 let len = String.length s in
336 let n = 4 + len in
337 let b = Buffer.create n in
338 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
339 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
340 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
341 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
342 Buffer.add_string b s;
343 let s' = Buffer.contents b in
344 let n' = Unix.write fd s' 0 n in
345 if n' != n then failwith "write failed";
348 let readcmd fd =
349 let s = "xxxx" in
350 let n = Unix.read fd s 0 4 in
351 if n != 4 then failwith "incomplete read(len)";
352 let len = 0
353 lor (Char.code s.[0] lsl 24)
354 lor (Char.code s.[1] lsl 16)
355 lor (Char.code s.[2] lsl 8)
356 lor (Char.code s.[3] lsl 0)
358 let s = String.create len in
359 let n = Unix.read fd s 0 len in
360 if n != len then failwith "incomplete read(data)";
364 let makecmd s l =
365 let b = Buffer.create 10 in
366 Buffer.add_string b s;
367 let rec combine = function
368 | [] -> b
369 | x :: xs ->
370 Buffer.add_char b ' ';
371 let s =
372 match x with
373 | `b b -> if b then "1" else "0"
374 | `s s -> s
375 | `i i -> string_of_int i
376 | `f f -> string_of_float f
377 | `I f -> string_of_int (truncate f)
379 Buffer.add_string b s;
380 combine xs;
382 combine l;
385 let wcmd s l =
386 let cmd = Buffer.contents (makecmd s l) in
387 writecmd state.csock cmd;
390 let calcips h =
391 if conf.presentation
392 then
393 let d = conf.winh - h in
394 max 0 ((d + 1) / 2)
395 else
396 conf.interpagespace
399 let calcheight () =
400 let rec f pn ph pi fh l =
401 match l with
402 | (n, _, h, _) :: rest ->
403 let ips = calcips h in
404 let fh =
405 if conf.presentation
406 then fh+ips
407 else (
408 if isbirdseye state.mode && pn = 0
409 then fh + ips
410 else fh
413 let fh = fh + ((n - pn) * (ph + pi)) in
414 f n h ips fh rest;
416 | [] ->
417 let inc =
418 if conf.presentation || (isbirdseye state.mode && pn = 0)
419 then 0
420 else -pi
422 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
423 max 0 fh
425 let fh = f 0 0 0 0 state.pdims in
429 let getpageyh pageno =
430 let rec f pn ph pi y l =
431 match l with
432 | (n, _, h, _) :: rest ->
433 let ips = calcips h in
434 if n >= pageno
435 then
436 let h = if n = pageno then h else ph in
437 if conf.presentation && n = pageno
438 then
439 y + (pageno - pn) * (ph + pi) + pi, h
440 else
441 y + (pageno - pn) * (ph + pi), h
442 else
443 let y = y + (if conf.presentation then pi else 0) in
444 let y = y + (n - pn) * (ph + pi) in
445 f n h ips y rest
447 | [] ->
448 y + (pageno - pn) * (ph + pi), ph
450 f 0 0 0 0 state.pdims
453 let getpagey pageno = fst (getpageyh pageno);;
455 let layout y sh =
456 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
457 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
458 match pdims with
459 | (pageno', w, h, x) :: rest when pageno' = pageno ->
460 let ips = calcips h in
461 let yinc =
462 if conf.presentation || (isbirdseye state.mode && pageno = 0)
463 then ips
464 else 0
466 (w, h, ips, x), rest, pdimno + 1, yinc
467 | _ ->
468 prev, pdims, pdimno, 0
470 let dy = dy + yinc in
471 let py = py + yinc in
472 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
473 then
474 accu
475 else
476 let vy = y + dy in
477 if py + h <= vy - yinc
478 then
479 let py = py + h + ips in
480 let dy = max 0 (py - y) in
481 f ~pageno:(pageno+1)
482 ~pdimno
483 ~prev:curr
486 ~pdims:rest
487 ~cacheleft
488 ~accu
489 else
490 let pagey = vy - py in
491 let pagevh = h - pagey in
492 let pagevh = min (sh - dy) pagevh in
493 let off = if yinc > 0 then py - vy else 0 in
494 let py = py + h + ips in
495 let e =
496 { pageno = pageno
497 ; pagedimno = pdimno
498 ; pagew = w
499 ; pageh = h
500 ; pagedispy = dy + off
501 ; pagey = pagey + off
502 ; pagevh = pagevh - off
503 ; pagex = x
506 let accu = e :: accu in
507 f ~pageno:(pageno+1)
508 ~pdimno
509 ~prev:curr
511 ~dy:(dy+pagevh+ips)
512 ~pdims:rest
513 ~cacheleft:(cacheleft-1)
514 ~accu
516 if state.invalidated = 0
517 then (
518 let accu =
520 ~pageno:0
521 ~pdimno:~-1
522 ~prev:(0,0,0,0)
523 ~py:0
524 ~dy:0
525 ~pdims:state.pdims
526 ~cacheleft:(cbcap state.pagecache)
527 ~accu:[]
529 List.rev accu
531 else
535 let clamp incr =
536 let y = state.y + incr in
537 let y = max 0 y in
538 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
542 let getopaque pageno =
543 try Some (Hashtbl.find state.pagemap
544 (pageno, state.w, conf.angle, conf.proportional, state.gen))
545 with Not_found -> None
548 let cache pageno opaque =
549 Hashtbl.replace state.pagemap
550 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
553 let validopaque opaque = String.length opaque > 0;;
555 let render l =
556 match getopaque l.pageno with
557 | None when not state.rendering ->
558 state.rendering <- true;
559 cache l.pageno ("", -1);
560 wcmd "render" [`i (l.pageno + 1)
561 ;`i l.pagedimno
562 ;`i l.pagew
563 ;`i l.pageh];
564 | _ -> ()
567 let loadlayout layout =
568 let rec f all = function
569 | l :: ls ->
570 begin match getopaque l.pageno with
571 | None -> render l; f false ls
572 | Some (opaque, _) -> f (all && validopaque opaque) ls
574 | [] -> all
576 f (layout <> []) layout;
579 let findpageforopaque opaque =
580 Hashtbl.fold
581 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
582 state.pagemap None
585 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
587 let preload () =
588 let oktopreload =
589 if conf.preload
590 then
591 let memleft = conf.memlimit - state.memused in
592 if memleft < 0
593 then
594 let opaque = cbpeek state.pagecache in
595 match findpageforopaque opaque with
596 | Some ((n, _, _, _, _), size) ->
597 memleft + size >= 0 && not (pagevisible state.layout n)
598 | None -> false
599 else true
600 else false
602 if oktopreload
603 then
604 let presentation = conf.presentation in
605 let interpagespace = conf.interpagespace in
606 let maxy = state.maxy in
607 conf.presentation <- false;
608 conf.interpagespace <- 0;
609 state.maxy <- calcheight ();
610 let y =
611 match state.layout with
612 | [] -> 0
613 | l :: _ -> getpagey l.pageno + l.pagey
615 let y = if y < conf.winh then 0 else y - conf.winh in
616 let pages = layout y (conf.winh*3) in
617 List.iter render pages;
618 conf.presentation <- presentation;
619 conf.interpagespace <- interpagespace;
620 state.maxy <- maxy;
623 let gotoy y =
624 let y = max 0 y in
625 let y = min state.maxy y in
626 let pages = layout y conf.winh in
627 let ready = loadlayout pages in
628 if conf.showall
629 then (
630 if ready
631 then (
632 state.y <- y;
633 state.layout <- pages;
634 state.throttle <- None;
635 Glut.postRedisplay ();
637 else (
638 state.throttle <- Some pages;
641 else (
642 state.y <- y;
643 state.layout <- pages;
644 state.throttle <- None;
645 Glut.postRedisplay ();
647 begin match state.mode with
648 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
649 if not (pagevisible pages pageno)
650 then (
651 match state.layout with
652 | [] -> ()
653 | l :: _ ->
654 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
656 | _ -> ()
657 end;
658 preload ();
661 let gotoy_and_clear_text y =
662 gotoy y;
663 if not conf.verbose then state.text <- "";
666 let getanchor () =
667 match state.layout with
668 | [] -> emptyanchor
669 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
672 let getanchory (n, top) =
673 let y, h = getpageyh n in
674 y + (truncate (top *. float h));
677 let gotoanchor anchor =
678 gotoy (getanchory anchor);
681 let addnav () =
682 cbput state.hists.nav (getanchor ());
685 let getnav dir =
686 let anchor = cbgetc state.hists.nav dir in
687 getanchory anchor;
690 let gotopage n top =
691 let y, h = getpageyh n in
692 gotoy_and_clear_text (y + (truncate (top *. float h)));
695 let gotopage1 n top =
696 let y = getpagey n in
697 gotoy_and_clear_text (y + top);
700 let invalidate () =
701 state.layout <- [];
702 state.pdims <- [];
703 state.rects <- [];
704 state.rects1 <- [];
705 state.invalidated <- state.invalidated + 1;
708 let scalecolor c =
709 let c = c *. state.colorscale in
710 (c, c, c);
713 let scalecolor2 (r, g, b) =
714 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
717 let represent () =
718 state.maxy <- calcheight ();
719 match state.mode with
720 | Birdseye (_, _, pageno, _, _) ->
721 let y, h = getpageyh pageno in
722 let top = (conf.winh - h) / 2 in
723 gotoy (max 0 (y - top))
724 | _ -> gotoanchor state.anchor
727 let pagematrix () =
728 GlMat.mode `projection;
729 GlMat.load_identity ();
730 GlMat.rotate ~x:1.0 ~angle:180.0 ();
731 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
732 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
733 if state.x != 0
734 then (
735 GlMat.translate ~x:(float state.x) ();
739 let winmatrix () =
740 GlMat.mode `projection;
741 GlMat.load_identity ();
742 GlMat.rotate ~x:1.0 ~angle:180.0 ();
743 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
744 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
747 let reshape ~w ~h =
748 if state.invalidated = 0 && state.anchor == initialanchor
749 then state.anchor <- getanchor ();
751 conf.winw <- w;
752 let w = truncate (float w *. conf.zoom) - state.scrollw in
753 let w = max w 2 in
754 state.w <- w;
755 conf.winh <- h;
756 GlMat.mode `modelview;
757 GlMat.load_identity ();
758 GlClear.color (scalecolor 1.0);
759 GlClear.clear [`color];
761 invalidate ();
762 wcmd "geometry" [`i w; `i h];
765 let enttext () =
766 let drawstring s =
767 GlDraw.color (0.0, 0.0, 0.0);
768 GlDraw.rect
769 (0.0, float (conf.winh - 18))
770 (float (conf.winw - state.scrollw - 1), float conf.winh)
772 GlDraw.color (1.0, 1.0, 1.0);
773 drawstring 14 8 (conf.winh - 5) s;
775 let len = String.length state.text in
776 match state.mode with
777 | Textentry ((prefix, text, _, _, _), _) ->
778 let s =
779 match String.length prefix with
780 | 0 | 1 ->
781 if len > 0
782 then
783 Printf.sprintf "%s%s\226–� [%s]" prefix text state.text
784 else
785 Printf.sprintf "%s%s\226–�" prefix text
787 | _ ->
788 if len > 0
789 then
790 Printf.sprintf "%s: %s\226–� [%s]" prefix text state.text
791 else
792 Printf.sprintf "%s: %s\226–�" prefix text
794 drawstring s
796 | _ ->
797 if len > 0 then drawstring state.text
800 let showtext c s =
801 state.text <- Printf.sprintf "%c%s" c s;
802 Glut.postRedisplay ();
805 let act cmd =
806 match cmd.[0] with
807 | 'c' ->
808 state.pdims <- [];
810 | 'D' ->
811 state.rects <- state.rects1;
812 Glut.postRedisplay ()
814 | 'C' ->
815 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
816 state.pagecount <- n;
817 state.invalidated <- state.invalidated - 1;
818 if state.invalidated = 0
819 then represent ()
821 | 't' ->
822 let s = Scanf.sscanf cmd "t %n"
823 (fun n -> String.sub cmd n (String.length cmd - n))
825 Glut.setWindowTitle s
827 | 'T' ->
828 let s = Scanf.sscanf cmd "T %n"
829 (fun n -> String.sub cmd n (String.length cmd - n))
831 if istextentry state.mode
832 then (
833 state.text <- s;
834 showtext ' ' s;
836 else (
837 state.text <- s;
838 Glut.postRedisplay ();
841 | 'V' ->
842 if conf.verbose
843 then
844 let s = Scanf.sscanf cmd "V %n"
845 (fun n -> String.sub cmd n (String.length cmd - n))
847 state.text <- s;
848 showtext ' ' s;
850 | 'F' ->
851 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
852 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
853 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
854 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
856 let y = (getpagey pageno) + truncate y0 in
857 addnav ();
858 gotoy y;
859 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
861 | 'R' ->
862 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
863 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
864 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
865 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
867 state.rects1 <-
868 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
870 | 'r' ->
871 let n, w, _h, r, l, s, p =
872 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
873 (fun n w h r l s p ->
874 (n-1, w, h, r, l != 0, s, p))
877 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
878 state.memused <- state.memused + s;
880 let layout =
881 match state.throttle with
882 | None -> state.layout
883 | Some layout -> layout
886 let rec gc () =
887 if (state.memused <= conf.memlimit) || cbempty state.pagecache
888 then ()
889 else (
890 let evictedopaque = cbpeek state.pagecache in
891 match findpageforopaque evictedopaque with
892 | None -> failwith "bug in gc"
893 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
894 if state.gen != gen || not (pagevisible layout evictedn)
895 then (
896 wcmd "free" [`s evictedopaque];
897 state.memused <- state.memused - evictedsize;
898 Hashtbl.remove state.pagemap k;
899 cbdecr state.pagecache;
900 gc ();
904 gc ();
906 cbput state.pagecache p;
907 state.rendering <- false;
909 begin match state.throttle with
910 | None ->
911 if pagevisible state.layout n
912 then gotoy state.y
913 else (
914 let allvisible = loadlayout state.layout in
915 if allvisible then preload ();
918 | Some layout ->
919 match layout with
920 | [] -> ()
921 | l :: _ ->
922 let y = getpagey l.pageno + l.pagey in
923 gotoy y
926 | 'l' ->
927 let pdim =
928 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
930 state.pdims <- pdim :: state.pdims
932 | 'o' ->
933 let (l, n, t, h, pos) =
934 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
936 let s = String.sub cmd pos (String.length cmd - pos) in
937 let outline = (s, l, (n, float t /. float h)) in
938 let outlines =
939 match state.outlines with
940 | Olist outlines -> Olist (outline :: outlines)
941 | Oarray _ -> Olist [outline]
942 | Onarrow _ -> Olist [outline]
944 state.outlines <- outlines
947 | 'i' ->
948 let s = Scanf.sscanf cmd "i %n"
949 (fun n -> String.sub cmd n (String.length cmd - n))
951 let len = String.length s in
952 let rec fold accu pos =
953 let eolpos =
954 try String.index_from s pos '\n' with Not_found -> len
956 if eolpos = len
957 then List.rev accu
958 else
959 let line = String.sub s pos (eolpos - pos) in
960 fold ((1, line)::accu) (eolpos+1)
962 state.docinfo <- fold state.docinfo 0
964 | _ ->
965 dolog "unknown cmd `%S'" cmd
968 let now = Unix.gettimeofday;;
970 let idle () =
971 let rec loop delay =
972 let r, _, _ = Unix.select [state.csock] [] [] delay in
973 begin match r with
974 | [] ->
975 if state.ascrollstep > 0
976 then begin
977 let y = state.y + state.ascrollstep in
978 let y = if y >= state.maxy then 0 else y in
979 gotoy y;
980 state.text <- "";
981 end;
983 | _ ->
984 let cmd = readcmd state.csock in
985 act cmd;
986 loop 0.0
987 end;
988 in loop 0.001
991 let onhist cb =
992 let rc = cb.rc in
993 let action = function
994 | HCprev -> cbget cb ~-1
995 | HCnext -> cbget cb 1
996 | HCfirst -> cbget cb ~-(cb.rc)
997 | HClast -> cbget cb (cb.len - 1 - cb.rc)
998 and cancel () = cb.rc <- rc
999 in (action, cancel)
1002 let search pattern forward =
1003 if String.length pattern > 0
1004 then
1005 let pn, py =
1006 match state.layout with
1007 | [] -> 0, 0
1008 | l :: _ ->
1009 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1011 let cmd =
1012 let b = makecmd "search"
1013 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1015 Buffer.add_char b ',';
1016 Buffer.add_string b pattern;
1017 Buffer.add_char b '\000';
1018 Buffer.contents b;
1020 writecmd state.csock cmd;
1023 let intentry text key =
1024 let c = Char.unsafe_chr key in
1025 match c with
1026 | '0' .. '9' ->
1027 let s = "x" in s.[0] <- c;
1028 let text = text ^ s in
1029 TEcont text
1031 | _ ->
1032 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1033 TEcont text
1036 let addchar s c =
1037 let b = Buffer.create (String.length s + 1) in
1038 Buffer.add_string b s;
1039 Buffer.add_char b c;
1040 Buffer.contents b;
1043 let textentry text key =
1044 let c = Char.unsafe_chr key in
1045 match c with
1046 | _ when key >= 32 && key < 127 ->
1047 let text = addchar text c in
1048 TEcont text
1050 | _ ->
1051 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1052 TEcont text
1055 let reinit angle proportional =
1056 conf.angle <- angle;
1057 conf.proportional <- proportional;
1058 invalidate ();
1059 wcmd "reinit" [`i angle; `b proportional];
1062 let setzoom zoom =
1063 let zoom = max 0.01 (min 2.2 zoom) in
1064 if zoom <> conf.zoom
1065 then (
1066 if zoom <= 1.0
1067 then state.x <- 0;
1068 conf.zoom <- zoom;
1069 state.anchor <- getanchor ();
1070 reshape conf.winw conf.winh;
1071 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1075 let enterbirdseye () =
1076 let zoom = float conf.thumbw /. float conf.winw in
1077 let birdseyepageno =
1078 let cy = conf.winh / 2 in
1079 let fold = function
1080 | [] -> 0
1081 | l :: rest ->
1082 let rec fold best = function
1083 | [] -> best.pageno
1084 | l :: rest ->
1085 let d = cy - (l.pagedispy + l.pagevh/2)
1086 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1087 if abs d < abs dbest
1088 then fold l rest
1089 else best.pageno
1090 in fold l rest
1092 fold state.layout
1094 state.mode <- Birdseye (
1095 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1097 conf.zoom <- zoom;
1098 conf.presentation <- false;
1099 conf.interpagespace <- 10;
1100 conf.hlinks <- false;
1101 state.x <- 0;
1102 state.mstate <- Mnone;
1103 conf.showall <- false;
1104 Glut.setCursor Glut.CURSOR_INHERIT;
1105 if conf.verbose
1106 then
1107 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1108 (100.0*.zoom)
1109 else
1110 state.text <- ""
1112 reshape conf.winw conf.winh;
1115 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1116 state.mode <- View;
1117 conf.zoom <- c.zoom;
1118 conf.presentation <- c.presentation;
1119 conf.interpagespace <- c.interpagespace;
1120 conf.showall <- c.showall;
1121 conf.hlinks <- c.hlinks;
1122 state.x <- leftx;
1123 if conf.verbose
1124 then
1125 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1126 (100.0*.conf.zoom)
1128 reshape conf.winw conf.winh;
1129 state.anchor <- if goback then anchor else (pageno, 0.0);
1132 let togglebirdseye () =
1133 match state.mode with
1134 | Birdseye vals -> leavebirdseye vals true
1135 | View | Outline _ -> enterbirdseye ()
1136 | _ -> ()
1139 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1140 let pageno = max 0 (pageno - 1) in
1141 let rec loop = function
1142 | [] -> gotopage1 pageno 0
1143 | l :: _ when l.pageno = pageno ->
1144 if l.pagedispy >= 0 && l.pagey = 0
1145 then Glut.postRedisplay ()
1146 else gotopage1 pageno 0
1147 | _ :: rest -> loop rest
1149 loop state.layout;
1150 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1153 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1154 let pageno = min (state.pagecount - 1) (pageno + 1) in
1155 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1156 let rec loop = function
1157 | [] ->
1158 let y, h = getpageyh pageno in
1159 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1160 gotoy (clamp dy)
1161 | l :: _ when l.pageno = pageno ->
1162 if l.pagevh != l.pageh
1163 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1164 else Glut.postRedisplay ()
1165 | _ :: rest -> loop rest
1167 loop state.layout
1170 let optentry mode _ key =
1171 let btos b = if b then "on" else "off" in
1172 let c = Char.unsafe_chr key in
1173 match c with
1174 | 's' ->
1175 let ondone s =
1176 try conf.scrollstep <- int_of_string s with exc ->
1177 state.text <- Printf.sprintf "bad integer `%s': %s"
1178 s (Printexc.to_string exc)
1180 TEswitch ("scroll step", "", None, intentry, ondone)
1182 | 'A' ->
1183 let ondone s =
1185 conf.autoscrollstep <- int_of_string s;
1186 if state.ascrollstep > 0
1187 then state.ascrollstep <- conf.autoscrollstep;
1188 with exc ->
1189 state.text <- Printf.sprintf "bad integer `%s': %s"
1190 s (Printexc.to_string exc)
1192 TEswitch ("auto scroll step", "", None, intentry, ondone)
1194 | 'Z' ->
1195 let ondone s =
1197 let zoom = float (int_of_string s) /. 100.0 in
1198 setzoom zoom
1199 with exc ->
1200 state.text <- Printf.sprintf "bad integer `%s': %s"
1201 s (Printexc.to_string exc)
1203 TEswitch ("zoom", "", None, intentry, ondone)
1205 | 't' ->
1206 let ondone s =
1208 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1209 state.text <-
1210 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1211 begin match mode with
1212 | Birdseye beye ->
1213 leavebirdseye beye false;
1214 enterbirdseye ();
1215 | _ -> ();
1217 with exc ->
1218 state.text <- Printf.sprintf "bad integer `%s': %s"
1219 s (Printexc.to_string exc)
1221 TEswitch ("thumbnail width", "", None, intentry, ondone)
1223 | 'R' ->
1224 let ondone s =
1225 match try
1226 Some (int_of_string s)
1227 with exc ->
1228 state.text <- Printf.sprintf "bad integer `%s': %s"
1229 s (Printexc.to_string exc);
1230 None
1231 with
1232 | Some angle -> reinit angle conf.proportional
1233 | None -> ()
1235 TEswitch ("rotation", "", None, intentry, ondone)
1237 | 'i' ->
1238 conf.icase <- not conf.icase;
1239 TEdone ("case insensitive search " ^ (btos conf.icase))
1241 | 'p' ->
1242 conf.preload <- not conf.preload;
1243 gotoy state.y;
1244 TEdone ("preload " ^ (btos conf.preload))
1246 | 'v' ->
1247 conf.verbose <- not conf.verbose;
1248 TEdone ("verbose " ^ (btos conf.verbose))
1250 | 'h' ->
1251 conf.maxhfit <- not conf.maxhfit;
1252 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1253 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1255 | 'c' ->
1256 conf.crophack <- not conf.crophack;
1257 TEdone ("crophack " ^ btos conf.crophack)
1259 | 'a' ->
1260 conf.showall <- not conf.showall;
1261 TEdone ("throttle " ^ btos conf.showall)
1263 | 'f' ->
1264 conf.underinfo <- not conf.underinfo;
1265 TEdone ("underinfo " ^ btos conf.underinfo)
1267 | 'P' ->
1268 conf.savebmarks <- not conf.savebmarks;
1269 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1271 | 'S' ->
1272 let ondone s =
1274 let pageno, py =
1275 match state.layout with
1276 | [] -> 0, 0
1277 | l :: _ ->
1278 l.pageno, l.pagey
1280 conf.interpagespace <- int_of_string s;
1281 state.maxy <- calcheight ();
1282 let y = getpagey pageno in
1283 gotoy (y + py)
1284 with exc ->
1285 state.text <- Printf.sprintf "bad integer `%s': %s"
1286 s (Printexc.to_string exc)
1288 TEswitch ("vertical margin", "", None, intentry, ondone)
1290 | 'l' ->
1291 reinit conf.angle (not conf.proportional);
1292 TEdone ("proprortional display " ^ btos conf.proportional)
1294 | _ ->
1295 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1296 TEstop
1299 let maxoutlinerows () = (conf.winh - 31) / 16;;
1301 let enterselector allowdel outlines errmsg msg =
1302 if Array.length outlines = 0
1303 then (
1304 showtext ' ' errmsg;
1306 else (
1307 state.text <- msg;
1308 Glut.setCursor Glut.CURSOR_INHERIT;
1309 let pageno =
1310 match state.layout with
1311 | [] -> -1
1312 | {pageno=pageno} :: _ -> pageno
1314 let active =
1315 let rec loop n =
1316 if n = Array.length outlines
1317 then 0
1318 else
1319 let (_, _, (outlinepageno, _)) = outlines.(n) in
1320 if outlinepageno >= pageno then n else loop (n+1)
1322 loop 0
1324 state.mode <- Outline
1325 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1326 state.mode);
1327 Glut.postRedisplay ();
1331 let enteroutlinemode () =
1332 let outlines, msg =
1333 match state.outlines with
1334 | Oarray a -> a, ""
1335 | Olist l ->
1336 let a = Array.of_list (List.rev l) in
1337 state.outlines <- Oarray a;
1338 a, ""
1339 | Onarrow (pat, a, _) ->
1340 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1342 enterselector false outlines "Document has no outline" msg;
1345 let enterbookmarkmode () =
1346 let bookmarks = Array.of_list state.bookmarks in
1347 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1350 let mode_to_string mode =
1351 let b = Buffer.create 10 in
1352 let rec f = function
1353 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1354 | View -> Buffer.add_string b "View"
1355 | Birdseye _ -> Buffer.add_string b "Birdseye"
1356 | Items _ -> Buffer.add_string b "Items"
1357 | Outline _ -> Buffer.add_string b "Outline"
1359 f mode;
1360 Buffer.contents b;
1363 let color_of_string s =
1364 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1365 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1369 let color_to_string (r, g, b) =
1370 let r = truncate (r *. 256.0)
1371 and g = truncate (g *. 256.0)
1372 and b = truncate (b *. 256.0) in
1373 Printf.sprintf "%d/%d/%d" r g b
1376 let enterinfomode () =
1377 let btos = function true -> "on" | _ -> "off" in
1378 let mode = state.mode in
1379 let rec makeitems () =
1380 let intp name get set =
1381 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1382 fun active first _ pan ->
1383 let ondone s =
1384 let n =
1385 try int_of_string s
1386 with exn ->
1387 state.text <- Printf.sprintf "bad integer `%s': %s"
1388 s (Printexc.to_string exn);
1389 max_int;
1391 if n != max_int then set n;
1393 let te = name, "", None, intentry, ondone in
1394 state.text <- "";
1395 Textentry (
1397 fun _ ->
1398 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1401 and boolp name get set =
1402 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1403 fun active first qsearch pan ->
1404 let v = get () in
1405 set (not v);
1406 Items (active, first, makeitems (), qsearch, pan, mode);
1408 and colorp name get set =
1409 Printf.sprintf "%-24s %s" name (color_to_string (get ())), 1, Action (
1410 fun active first _ pan ->
1411 let invalid = (nan, nan, nan) in
1412 let ondone s =
1413 let c =
1414 try color_of_string s
1415 with exn ->
1416 state.text <- Printf.sprintf "bad color `%s': %s"
1417 s (Printexc.to_string exn);
1418 invalid
1420 if c <> invalid
1421 then set c;
1423 let te = name, "", None, textentry, ondone in
1424 state.text <- "";
1425 Textentry (
1427 fun _ ->
1428 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1433 let birdseye = isbirdseye mode in
1434 let items = [
1435 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1437 boolp "presentation"
1438 (fun () -> conf.presentation)
1439 (fun v ->
1440 conf.presentation <- v;
1441 state.anchor <- getanchor ();
1442 represent ());
1444 boolp "ignore case in searches"
1445 (fun () -> conf.icase)
1446 (fun v -> conf.icase <- v);
1448 boolp "preload"
1449 (fun () -> conf.preload)
1450 (fun v -> conf.preload <- v);
1452 boolp "verbose"
1453 (fun () -> conf.verbose)
1454 (fun v -> conf.verbose <- v);
1456 boolp "max fit"
1457 (fun () -> conf.maxhfit)
1458 (fun v -> conf.maxhfit <- v);
1460 boolp "crop hack"
1461 (fun () -> conf.crophack)
1462 (fun v -> conf.crophack <- v);
1464 boolp "throttle"
1465 (fun () -> conf.showall)
1466 (fun v -> conf.showall <- v);
1468 boolp "highlight links"
1469 (fun () -> conf.hlinks)
1470 (fun v -> conf.hlinks <- v);
1472 boolp "under info"
1473 (fun () -> conf.underinfo)
1474 (fun v -> conf.underinfo <- v);
1475 boolp "persistent bookmarks"
1476 (fun () -> conf.savebmarks)
1477 (fun v -> conf.savebmarks <- v);
1479 boolp "proportional display"
1480 (fun () -> conf.proportional)
1481 (fun v -> reinit conf.angle v);
1483 boolp "persistent location"
1484 (fun () -> conf.jumpback)
1485 (fun v -> conf.jumpback <- v);
1487 "", 0, Noaction;
1489 intp "vertical margin"
1490 (fun () -> conf.interpagespace)
1491 (fun n ->
1492 conf.interpagespace <- n;
1493 let pageno, py =
1494 match state.layout with
1495 | [] -> 0, 0
1496 | l :: _ ->
1497 l.pageno, l.pagey
1499 state.maxy <- calcheight ();
1500 let y = getpagey pageno in
1501 gotoy (y + py)
1504 intp "page bias"
1505 (fun () -> conf.pagebias)
1506 (fun v -> conf.pagebias <- v);
1508 intp "scroll step"
1509 (fun () -> conf.scrollstep)
1510 (fun n -> conf.scrollstep <- n);
1512 intp "auto scroll step"
1513 (fun () ->
1514 if state.ascrollstep > 0
1515 then state.ascrollstep
1516 else conf.autoscrollstep)
1517 (fun n ->
1518 if state.ascrollstep > 0
1519 then state.ascrollstep <- n
1520 else conf.autoscrollstep <- n);
1522 intp "zoom"
1523 (fun () -> truncate (conf.zoom *. 100.))
1524 (fun v -> setzoom ((float v) /. 100.));
1526 intp "rotation"
1527 (fun () -> conf.angle)
1528 (fun v -> reinit v conf.proportional);
1530 intp "scroll bar width"
1531 (fun () -> state.scrollw)
1532 (fun v ->
1533 state.scrollw <- v;
1534 reshape conf.winw conf.winh;
1537 intp "scroll handle height"
1538 (fun () -> conf.scrollh)
1539 (fun v -> conf.scrollh <- v;);
1541 intp "thumbnail width"
1542 (fun () -> conf.thumbw)
1543 (fun v ->
1544 conf.thumbw <- min 1920 v;
1545 match mode with
1546 | Birdseye beye ->
1547 leavebirdseye beye false;
1548 enterbirdseye ()
1549 | _ -> ()
1552 colorp "background color"
1553 (fun () -> conf.bgcolor)
1554 (fun v -> conf.bgcolor <- v);
1556 "", 0, Noaction;
1557 "Presentation mode", 0, Noaction;
1559 boolp "scrollbar visible"
1560 (fun () -> conf.scrollbarinpm)
1561 (fun v ->
1562 if v != conf.scrollbarinpm
1563 then (
1564 conf.scrollbarinpm <- v;
1565 if conf.presentation
1566 then (
1567 state.scrollw <- if v then conf.scrollbw else 0;
1568 reshape conf.winw conf.winh;
1573 "", 0, Noaction;
1574 "Pixmap Cache", 0, Noaction;
1576 intp "size (advisory)"
1577 (fun () -> conf.memlimit)
1578 (fun v -> conf.memlimit <- v);
1579 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1583 let tailer =
1584 let trailer =
1585 ("", 0, Noaction)
1586 :: ("Document", 0, Noaction)
1587 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1589 if birdseye
1590 then trailer
1591 else (
1592 ("", 0, Noaction)
1593 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1594 (btos conf.bedefault),
1596 Action (
1597 fun active first qsearch pan ->
1598 conf.bedefault <- not conf.bedefault;
1599 Items (active, first, makeitems (), qsearch, pan, mode);
1601 ) :: trailer
1604 Array.of_list (items @ tailer)
1606 state.text <- "";
1607 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1608 Glut.postRedisplay ();
1611 let enterhelpmode () =
1612 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1613 Glut.postRedisplay ();
1616 let quickbookmark ?title () =
1617 match state.layout with
1618 | [] -> ()
1619 | l :: _ ->
1620 let title =
1621 match title with
1622 | None ->
1623 let sec = Unix.gettimeofday () in
1624 let tm = Unix.localtime sec in
1625 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1626 (l.pageno+1)
1627 tm.Unix.tm_mday
1628 tm.Unix.tm_mon
1629 (tm.Unix.tm_year + 1900)
1630 tm.Unix.tm_hour
1631 tm.Unix.tm_min
1632 | Some title -> title
1634 state.bookmarks <-
1635 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1636 :: state.bookmarks
1639 let doreshape w h =
1640 state.fullscreen <- None;
1641 Glut.reshapeWindow w h;
1644 let writeopen path password =
1645 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1646 writecmd state.csock "info";
1649 let opendoc path password =
1650 invalidate ();
1651 state.path <- path;
1652 state.password <- password;
1653 state.gen <- state.gen + 1;
1655 writeopen path password;
1656 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1657 wcmd "geometry" [`i state.w; `i conf.winh];
1660 let viewkeyboard key =
1661 let enttext te =
1662 let mode = state.mode in
1663 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1664 state.text <- "";
1665 enttext ();
1666 Glut.postRedisplay ()
1668 let c = Char.chr key in
1669 match c with
1670 | '\027' | 'q' -> (* escape *)
1671 exit 0
1673 | '\008' -> (* backspace *)
1674 let y = getnav ~-1 in
1675 gotoy_and_clear_text y
1677 | 'o' ->
1678 enteroutlinemode ()
1680 | 'u' ->
1681 state.rects <- [];
1682 state.text <- "";
1683 Glut.postRedisplay ()
1685 | '/' | '?' ->
1686 let ondone isforw s =
1687 cbput state.hists.pat s;
1688 state.searchpattern <- s;
1689 search s isforw
1691 let s = String.create 1 in
1692 s.[0] <- c;
1693 enttext (s, "", Some (onhist state.hists.pat),
1694 textentry, ondone (c ='/'))
1696 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1697 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1698 setzoom (min 2.2 (conf.zoom +. incr))
1700 | '+' ->
1701 let ondone s =
1702 let n =
1703 try int_of_string s with exc ->
1704 state.text <- Printf.sprintf "bad integer `%s': %s"
1705 s (Printexc.to_string exc);
1706 max_int
1708 if n != max_int
1709 then (
1710 conf.pagebias <- n;
1711 state.text <- "page bias is now " ^ string_of_int n;
1714 enttext ("page bias", "", None, intentry, ondone)
1716 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1717 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1718 setzoom (max 0.01 (conf.zoom -. decr))
1720 | '-' ->
1721 let ondone msg = state.text <- msg in
1722 enttext (
1723 "option [acfhilpstvAPRSZ]", "", None,
1724 optentry state.mode, ondone
1727 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1728 setzoom 1.0
1730 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1731 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1732 if zoom < 1.0
1733 then setzoom zoom
1735 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1736 togglebirdseye ()
1738 | '0' .. '9' ->
1739 let ondone s =
1740 let n =
1741 try int_of_string s with exc ->
1742 state.text <- Printf.sprintf "bad integer `%s': %s"
1743 s (Printexc.to_string exc);
1746 if n >= 0
1747 then (
1748 addnav ();
1749 cbput state.hists.pag (string_of_int n);
1750 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1753 let pageentry text key =
1754 match Char.unsafe_chr key with
1755 | 'g' -> TEdone text
1756 | _ -> intentry text key
1758 let text = "x" in text.[0] <- c;
1759 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1761 | 'b' ->
1762 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1763 reshape conf.winw conf.winh;
1765 | 'l' ->
1766 conf.hlinks <- not conf.hlinks;
1767 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1768 Glut.postRedisplay ()
1770 | 'a' ->
1771 if state.ascrollstep = 0
1772 then state.ascrollstep <- conf.autoscrollstep
1773 else (
1774 conf.autoscrollstep <- state.ascrollstep;
1775 state.ascrollstep <- 0;
1778 | 'P' ->
1779 conf.presentation <- not conf.presentation;
1780 if conf.presentation
1781 then (
1782 if not conf.scrollbarinpm
1783 then state.scrollw <- 0;
1785 else
1786 state.scrollw <- conf.scrollbw;
1788 showtext ' ' ("presentation mode " ^
1789 if conf.presentation then "on" else "off");
1790 state.anchor <- getanchor ();
1791 represent ()
1793 | 'f' ->
1794 begin match state.fullscreen with
1795 | None ->
1796 state.fullscreen <- Some (conf.winw, conf.winh);
1797 Glut.fullScreen ()
1798 | Some (w, h) ->
1799 state.fullscreen <- None;
1800 doreshape w h
1803 | 'g' ->
1804 gotoy_and_clear_text 0
1806 | 'n' ->
1807 search state.searchpattern true
1809 | 'p' | 'N' ->
1810 search state.searchpattern false
1812 | 't' ->
1813 begin match state.layout with
1814 | [] -> ()
1815 | l :: _ ->
1816 gotoy_and_clear_text (getpagey l.pageno)
1819 | ' ' ->
1820 begin match List.rev state.layout with
1821 | [] -> ()
1822 | l :: _ ->
1823 let pageno = min (l.pageno+1) (state.pagecount-1) in
1824 gotoy_and_clear_text (getpagey pageno)
1827 | '\127' -> (* delte *)
1828 begin match state.layout with
1829 | [] -> ()
1830 | l :: _ ->
1831 let pageno = max 0 (l.pageno-1) in
1832 gotoy_and_clear_text (getpagey pageno)
1835 | '=' ->
1836 let f (fn, _) l =
1837 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1839 let fn, ln = List.fold_left f (-1, -1) state.layout in
1840 let s =
1841 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1842 let percent =
1843 if maxy <= 0
1844 then 100.
1845 else (100. *. (float state.y /. float maxy)) in
1846 if fn = ln
1847 then
1848 Printf.sprintf "Page %d of %d %.2f%%"
1849 (fn+1) state.pagecount percent
1850 else
1851 Printf.sprintf
1852 "Pages %d-%d of %d %.2f%%"
1853 (fn+1) (ln+1) state.pagecount percent
1855 showtext ' ' s;
1857 | 'w' ->
1858 begin match state.layout with
1859 | [] -> ()
1860 | l :: _ ->
1861 doreshape (l.pagew + state.scrollw) l.pageh;
1862 Glut.postRedisplay ();
1865 | '\'' ->
1866 enterbookmarkmode ()
1868 | 'h' ->
1869 enterhelpmode ()
1871 | 'i' ->
1872 enterinfomode ()
1874 | 'm' ->
1875 let ondone s =
1876 match state.layout with
1877 | l :: _ ->
1878 state.bookmarks <-
1879 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1880 :: state.bookmarks
1881 | _ -> ()
1883 enttext ("bookmark", "", None, textentry, ondone)
1885 | '~' ->
1886 quickbookmark ();
1887 showtext ' ' "Quick bookmark added";
1889 | 'z' ->
1890 begin match state.layout with
1891 | l :: _ ->
1892 let rect = getpdimrect l.pagedimno in
1893 let w, h =
1894 if conf.crophack
1895 then
1896 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1897 truncate (1.2 *. (rect.(3) -. rect.(0))))
1898 else
1899 (truncate (rect.(1) -. rect.(0)),
1900 truncate (rect.(3) -. rect.(0)))
1902 if w != 0 && h != 0
1903 then
1904 doreshape (w + state.scrollw) (h + conf.interpagespace)
1906 Glut.postRedisplay ();
1908 | [] -> ()
1911 | '<' | '>' ->
1912 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1914 | '[' | ']' ->
1915 state.colorscale <-
1916 max 0.0
1917 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1918 Glut.postRedisplay ()
1920 | 'k' ->
1921 begin match state.mode with
1922 | Birdseye beye -> upbirdseye beye
1923 | _ -> gotoy (clamp (-conf.scrollstep))
1926 | 'j' ->
1927 begin match state.mode with
1928 | Birdseye beye -> downbirdseye beye
1929 | _ -> gotoy (clamp conf.scrollstep)
1932 | 'r' ->
1933 state.anchor <- getanchor ();
1934 opendoc state.path state.password
1936 | _ ->
1937 vlog "huh? %d %c" key (Char.chr key);
1940 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1941 let enttext te =
1942 state.mode <- Textentry (te, onleave);
1943 state.text <- "";
1944 enttext ();
1945 Glut.postRedisplay ()
1947 match Char.unsafe_chr key with
1948 | '\008' -> (* backspace *)
1949 let len = String.length text in
1950 if len = 0
1951 then (
1952 onleave Cancel;
1953 Glut.postRedisplay ();
1955 else (
1956 let s = String.sub text 0 (len - 1) in
1957 enttext (c, s, opthist, onkey, ondone)
1960 | '\r' | '\n' ->
1961 ondone text;
1962 onleave Confirm;
1963 Glut.postRedisplay ()
1965 | '\027' -> (* escape *)
1966 begin match opthist with
1967 | None -> ()
1968 | Some (_, onhistcancel) -> onhistcancel ()
1969 end;
1970 onleave Cancel;
1971 Glut.postRedisplay ()
1973 | _ ->
1974 begin match onkey text key with
1975 | TEdone text ->
1976 onleave Confirm;
1977 ondone text;
1978 Glut.postRedisplay ()
1980 | TEcont text ->
1981 enttext (c, text, opthist, onkey, ondone);
1983 | TEstop ->
1984 onleave Cancel;
1985 Glut.postRedisplay ()
1987 | TEswitch te ->
1988 state.mode <- Textentry (te, onleave);
1989 Glut.postRedisplay ()
1990 end;
1993 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
1994 match key with
1995 | 27 -> (* escape *)
1996 leavebirdseye beye true
1998 | 12 -> (* ctrl-l *)
1999 let y, h = getpageyh pageno in
2000 let top = (conf.winh - h) / 2 in
2001 gotoy (max 0 (y - top))
2003 | 13 -> (* enter *)
2004 leavebirdseye beye false
2006 | _ ->
2007 viewkeyboard key
2010 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2011 let set active first qsearch =
2012 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2014 let search active pattern incr =
2015 let dosearch re =
2016 let rec loop n =
2017 if n = Array.length items || n = -1
2018 then None
2019 else
2020 let (s, _, _) = items.(n) in
2022 (try ignore (Str.search_forward re s 0); true
2023 with Not_found -> false)
2024 then Some n
2025 else loop (n + incr)
2027 loop active
2030 let re = Str.regexp_case_fold pattern in
2031 dosearch re
2032 with Failure s ->
2033 state.text <- s;
2034 None
2036 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2037 match key with
2038 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2039 let incr = if key = 18 then -1 else 1 in
2040 let active, first =
2041 match search (active + incr) qsearch incr with
2042 | None ->
2043 state.text <- qsearch ^ " [not found]";
2044 active, first
2045 | Some active ->
2046 state.text <- qsearch;
2047 active, firstof active
2049 set active first qsearch;
2050 Glut.postRedisplay ();
2052 | 8 -> (* backspace *)
2053 let len = String.length qsearch in
2054 if len = 0
2055 then ()
2056 else (
2057 if len = 1
2058 then (
2059 state.text <- "";
2060 set active first "";
2062 else
2063 let qsearch = String.sub qsearch 0 (len - 1) in
2064 let active, first =
2065 match search active qsearch ~-1 with
2066 | None ->
2067 state.text <- qsearch ^ " [not found]";
2068 active, first
2069 | Some active ->
2070 state.text <- qsearch;
2071 active, firstof active
2073 set active first qsearch
2075 Glut.postRedisplay ()
2077 | _ when key >= 32 && key < 127 ->
2078 let pattern = addchar qsearch (Char.chr key) in
2079 let active, first =
2080 match search active pattern 1 with
2081 | None ->
2082 state.text <- pattern ^ " [not found]";
2083 active, first
2084 | Some active ->
2085 state.text <- pattern;
2086 active, firstof active
2088 set active first pattern;
2089 Glut.postRedisplay ()
2091 | 27 -> (* escape *)
2092 state.text <- "";
2093 state.mode <- oldmode;
2094 Glut.postRedisplay ();
2096 | 13 -> (* enter *)
2097 if active < Array.length items
2098 then (
2099 match items.(active) with
2100 | _, _, Action f ->
2101 state.mode <- f active first qsearch pan
2103 | _, _, Noaction ->
2104 state.text <- "";
2105 state.mode <- oldmode
2107 Glut.postRedisplay ();
2109 | _ -> dolog "unknown key %d" key
2112 let outlinekeyboard key
2113 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2114 let narrow outlines pattern =
2115 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2116 match reopt with
2117 | None -> None
2118 | Some re ->
2119 let rec fold accu n =
2120 if n = -1
2121 then accu
2122 else
2123 let (s, _, _) as o = outlines.(n) in
2124 let accu =
2125 if (try ignore (Str.search_forward re s 0); true
2126 with Not_found -> false)
2127 then (o :: accu)
2128 else accu
2130 fold accu (n-1)
2132 let matched = fold [] (Array.length outlines - 1) in
2133 if matched = [] then None else Some (Array.of_list matched)
2135 let search active pattern incr =
2136 let dosearch re =
2137 let rec loop n =
2138 if n = Array.length outlines || n = -1
2139 then None
2140 else
2141 let (s, _, _) = outlines.(n) in
2143 (try ignore (Str.search_forward re s 0); true
2144 with Not_found -> false)
2145 then Some n
2146 else loop (n + incr)
2148 loop active
2151 let re = Str.regexp_case_fold pattern in
2152 dosearch re
2153 with Failure s ->
2154 state.text <- s;
2155 None
2157 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2158 match key with
2159 | 27 -> (* escape *)
2160 if String.length qsearch = 0
2161 then (
2162 state.text <- "";
2163 state.mode <- oldmode;
2164 Glut.postRedisplay ();
2166 else (
2167 state.text <- "";
2168 state.mode <- Outline (
2169 allowdel, active, first, outlines, "", pan, oldmode
2171 Glut.postRedisplay ();
2174 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2175 let incr = if key = 18 then -1 else 1 in
2176 let active, first =
2177 match search (active + incr) qsearch incr with
2178 | None ->
2179 state.text <- qsearch ^ " [not found]";
2180 active, first
2181 | Some active ->
2182 state.text <- qsearch;
2183 active, firstof active
2185 state.mode <- Outline (
2186 allowdel, active, first, outlines, qsearch, pan, oldmode
2188 Glut.postRedisplay ();
2190 | 8 -> (* backspace *)
2191 let len = String.length qsearch in
2192 if len = 0
2193 then ()
2194 else (
2195 if len = 1
2196 then (
2197 state.text <- "";
2198 state.mode <- Outline (
2199 allowdel, active, first, outlines, "", pan, oldmode
2202 else
2203 let qsearch = String.sub qsearch 0 (len - 1) in
2204 let active, first =
2205 match search active qsearch ~-1 with
2206 | None ->
2207 state.text <- qsearch ^ " [not found]";
2208 active, first
2209 | Some active ->
2210 state.text <- qsearch;
2211 active, firstof active
2213 state.mode <- Outline (
2214 allowdel, active, first, outlines, qsearch, pan, oldmode
2217 Glut.postRedisplay ()
2219 | 13 -> (* enter *)
2220 if active < Array.length outlines
2221 then (
2222 let (_, _, anchor) = outlines.(active) in
2223 addnav ();
2224 gotoanchor anchor;
2226 state.text <- "";
2227 if allowdel then state.bookmarks <- Array.to_list outlines;
2228 state.mode <- oldmode;
2229 Glut.postRedisplay ();
2231 | _ when key >= 32 && key < 127 ->
2232 let pattern = addchar qsearch (Char.chr key) in
2233 let active, first =
2234 match search active pattern 1 with
2235 | None ->
2236 state.text <- pattern ^ " [not found]";
2237 active, first
2238 | Some active ->
2239 state.text <- pattern;
2240 active, firstof active
2242 state.mode <- Outline (
2243 allowdel, active, first, outlines, pattern, pan, oldmode
2245 Glut.postRedisplay ()
2247 | 14 when not allowdel -> (* ctrl-n *)
2248 if String.length qsearch > 0
2249 then (
2250 let optoutlines = narrow outlines qsearch in
2251 begin match optoutlines with
2252 | None -> state.text <- "can't narrow"
2253 | Some outlines ->
2254 state.mode <- Outline (
2255 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2257 match state.outlines with
2258 | Olist _ -> ()
2259 | Oarray a ->
2260 state.outlines <- Onarrow (qsearch, outlines, a)
2261 | Onarrow (_, _, b) ->
2262 state.outlines <- Onarrow (qsearch, outlines, b)
2263 end;
2265 Glut.postRedisplay ()
2267 | 21 when not allowdel -> (* ctrl-u *)
2268 let outline =
2269 match state.outlines with
2270 | Oarray a -> a
2271 | Olist l ->
2272 let a = Array.of_list (List.rev l) in
2273 state.outlines <- Oarray a;
2275 | Onarrow (_, _, b) ->
2276 state.outlines <- Oarray b;
2277 state.text <- "";
2280 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2281 Glut.postRedisplay ()
2283 | 12 -> (* ctrl-l *)
2284 state.mode <- Outline
2285 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2286 Glut.postRedisplay ()
2288 | 127 when allowdel -> (* delete *)
2289 let len = Array.length outlines - 1 in
2290 if len = 0
2291 then (
2292 state.mode <- View;
2293 state.bookmarks <- [];
2295 else (
2296 let bookmarks = Array.init len
2297 (fun i ->
2298 let i = if i >= active then i + 1 else i in
2299 outlines.(i)
2302 state.mode <-
2303 Outline (
2304 allowdel,
2305 min active (len-1),
2306 min first (len-1),
2307 bookmarks, qsearch,
2309 oldmode
2312 Glut.postRedisplay ()
2314 | _ -> dolog "unknown key %d" key
2317 let keyboard ~key ~x ~y =
2318 ignore x;
2319 ignore y;
2320 if key = 7 (* ctrl-g *)
2321 then
2322 wcmd "interrupt" []
2323 else
2324 match state.mode with
2325 | Outline outline -> outlinekeyboard key outline
2326 | Textentry textentry -> textentrykeyboard key textentry
2327 | Birdseye birdseye -> birdseyekeyboard key birdseye
2328 | View -> viewkeyboard key
2329 | Items items -> itemskeyboard key items
2332 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2333 match key with
2334 | Glut.KEY_UP -> upbirdseye beye
2335 | Glut.KEY_DOWN -> downbirdseye beye
2337 | Glut.KEY_PAGE_UP ->
2338 begin match state.layout with
2339 | l :: _ ->
2340 if l.pagey != 0
2341 then (
2342 state.mode <- Birdseye (
2343 conf, leftx, l.pageno, hooverpageno, anchor
2345 gotopage1 l.pageno 0;
2347 else (
2348 let layout = layout (state.y-conf.winh) conf.winh in
2349 match layout with
2350 | [] -> gotoy (clamp (-conf.winh))
2351 | l :: _ ->
2352 state.mode <- Birdseye (
2353 conf, leftx, l.pageno, hooverpageno, anchor
2355 gotopage1 l.pageno 0
2358 | [] -> gotoy (clamp (-conf.winh))
2359 end;
2361 | Glut.KEY_PAGE_DOWN ->
2362 begin match List.rev state.layout with
2363 | l :: _ ->
2364 let layout = layout (state.y + conf.winh) conf.winh in
2365 begin match layout with
2366 | [] ->
2367 let incr = l.pageh - l.pagevh in
2368 if incr = 0
2369 then (
2370 state.mode <-
2371 Birdseye (
2372 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2374 Glut.postRedisplay ();
2376 else gotoy (clamp (incr + conf.interpagespace*2));
2378 | l :: _ ->
2379 state.mode <-
2380 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2381 gotopage1 l.pageno 0;
2384 | [] -> gotoy (clamp conf.winh)
2385 end;
2387 | Glut.KEY_HOME ->
2388 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2389 gotopage1 0 0
2391 | Glut.KEY_END ->
2392 let pageno = state.pagecount - 1 in
2393 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2394 if not (pagevisible state.layout pageno)
2395 then
2396 let h =
2397 match List.rev state.pdims with
2398 | [] -> conf.winh
2399 | (_, _, h, _) :: _ -> h
2401 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2402 else Glut.postRedisplay ();
2403 | _ -> ()
2406 let setautoscrollspeed goingdown =
2407 let incr = max 1 (state.ascrollstep / 2) in
2408 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2409 state.ascrollstep <- astep;
2412 let special ~key ~x ~y =
2413 ignore x;
2414 ignore y;
2415 match state.mode with
2416 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2417 togglebirdseye ()
2419 | Birdseye vals ->
2420 birdseyespecial key vals
2422 | View when key = Glut.KEY_F1 ->
2423 enterhelpmode ()
2425 | View ->
2426 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2427 then setautoscrollspeed (key = Glut.KEY_DOWN)
2428 else
2429 let y =
2430 match key with
2431 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2432 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2433 | Glut.KEY_DOWN -> clamp conf.scrollstep
2434 | Glut.KEY_PAGE_UP ->
2435 if Glut.getModifiers () land Glut.active_ctrl != 0
2436 then
2437 match state.layout with
2438 | [] -> state.y
2439 | l :: _ -> state.y - l.pagey
2440 else
2441 clamp (-conf.winh)
2442 | Glut.KEY_PAGE_DOWN ->
2443 if Glut.getModifiers () land Glut.active_ctrl != 0
2444 then
2445 match List.rev state.layout with
2446 | [] -> state.y
2447 | l :: _ -> getpagey l.pageno
2448 else
2449 clamp conf.winh
2450 | Glut.KEY_HOME -> addnav (); 0
2451 | Glut.KEY_END ->
2452 addnav ();
2453 state.maxy - (if conf.maxhfit then conf.winh else 0)
2455 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2456 Glut.getModifiers () land Glut.active_alt != 0 ->
2457 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2459 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2460 state.x <- state.x - 10;
2461 state.y
2462 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2463 state.x <- state.x + 10;
2464 state.y
2466 | _ -> state.y
2468 gotoy_and_clear_text y
2470 | Textentry
2471 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2472 let s =
2473 match key with
2474 | Glut.KEY_UP -> action HCprev
2475 | Glut.KEY_DOWN -> action HCnext
2476 | Glut.KEY_HOME -> action HCfirst
2477 | Glut.KEY_END -> action HClast
2478 | _ -> state.text
2480 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2481 Glut.postRedisplay ()
2483 | Textentry _ -> ()
2485 | Items (active, first, items, qsearch, pan, oldmode) ->
2486 let maxrows = maxoutlinerows () in
2487 let itemcount = Array.length items in
2488 let hasaction = function
2489 | (_, _, Noaction) -> false
2490 | _ -> true
2492 let find start incr =
2493 let rec find i =
2494 if i = -1 || i = itemcount
2495 then -1
2496 else (
2497 if hasaction items.(i)
2498 then i
2499 else find (i + incr)
2502 find start
2504 let set active first =
2505 let first = max 0 (min first (itemcount - maxrows)) in
2506 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2508 let navigate incr =
2509 let isvisible first n = n >= first && n - first <= maxrows in
2510 let active, first =
2511 let incr1 = if incr > 0 then 1 else -1 in
2512 if isvisible first active
2513 then
2514 let next =
2515 let next = active + incr in
2516 let next =
2517 if next < 0 || next >= itemcount
2518 then -1
2519 else find next incr1
2521 if next = -1 || abs (active - next) > maxrows
2522 then -1
2523 else next
2525 if next = -1
2526 then
2527 let first = first + incr in
2528 let first = max 0 (min first (itemcount - 1)) in
2529 let next =
2530 let next = active + incr in
2531 let next = max 0 (min next (itemcount - 1)) in
2532 find next ~-incr1
2534 let active = if next = -1 then active else next in
2535 active, first
2536 else
2537 let first = min next first in
2538 next, first
2539 else
2540 let first = first + incr in
2541 let first = max 0 (min first (itemcount - 1)) in
2542 let active =
2543 let next = active + incr in
2544 let next = max 0 (min next (itemcount - 1)) in
2545 let next = find next incr1 in
2546 if next = -1 || abs (active - first) > maxrows
2547 then active
2548 else next
2550 active, first
2552 set active first;
2553 Glut.postRedisplay ()
2555 begin match key with
2556 | Glut.KEY_UP -> navigate ~-1
2557 | Glut.KEY_DOWN -> navigate 1
2558 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2559 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2561 | Glut.KEY_RIGHT ->
2562 state.mode <- Items (
2563 active, first, items, qsearch, min 0 (pan - 1), oldmode
2565 Glut.postRedisplay ()
2567 | Glut.KEY_LEFT ->
2568 state.mode <- Items (
2569 active, first, items, qsearch, min 0 (pan + 1), oldmode
2571 Glut.postRedisplay ()
2573 | Glut.KEY_HOME ->
2574 let active = find 0 1 in
2575 set active 0;
2576 Glut.postRedisplay ()
2578 | Glut.KEY_END ->
2579 let first = max 0 (itemcount - maxrows) in
2580 let active = find (itemcount - 1) ~-1 in
2581 set active first;
2582 Glut.postRedisplay ()
2584 | _ -> ()
2585 end;
2587 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2588 let maxrows = maxoutlinerows () in
2589 let calcfirst first active =
2590 if active > first
2591 then
2592 let rows = active - first in
2593 if rows > maxrows then active - maxrows else first
2594 else active
2596 let navigate incr =
2597 let active = active + incr in
2598 let active = max 0 (min active (Array.length outlines - 1)) in
2599 let first = calcfirst first active in
2600 state.mode <- Outline (
2601 allowdel, active, first, outlines, qsearch, pan, oldmode
2603 Glut.postRedisplay ()
2605 let updownlevel incr =
2606 let len = Array.length outlines in
2607 let (_, curlevel, _) = outlines.(active) in
2608 let rec flow i =
2609 if i = len then i-1 else if i = -1 then 0 else
2610 let (_, l, _) = outlines.(i) in
2611 if l != curlevel then i else flow (i+incr)
2613 let active = flow active in
2614 let first = calcfirst first active in
2615 state.mode <- Outline (
2616 allowdel, active, first, outlines, qsearch, pan, oldmode
2618 Glut.postRedisplay ()
2620 match key with
2621 | Glut.KEY_UP -> navigate ~-1
2622 | Glut.KEY_DOWN -> navigate 1
2623 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2624 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2626 | Glut.KEY_RIGHT ->
2627 if Glut.getModifiers () land Glut.active_ctrl != 0
2628 then (
2629 state.mode <- Outline (
2630 allowdel, active, first, outlines,
2631 qsearch, min 0 (pan + 1), oldmode
2633 Glut.postRedisplay ();
2635 else (
2636 if not allowdel
2637 then updownlevel 1
2640 | Glut.KEY_LEFT ->
2641 if Glut.getModifiers () land Glut.active_ctrl != 0
2642 then (
2643 state.mode <- Outline (
2644 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2646 Glut.postRedisplay ();
2648 else (
2649 if not allowdel
2650 then updownlevel ~-1
2653 | Glut.KEY_HOME ->
2654 state.mode <- Outline (
2655 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2657 Glut.postRedisplay ()
2659 | Glut.KEY_END ->
2660 let active = Array.length outlines - 1 in
2661 let first = max 0 (active - maxrows) in
2662 state.mode <- Outline (
2663 allowdel, active, first, outlines, qsearch, pan, oldmode
2665 Glut.postRedisplay ()
2667 | _ -> ()
2670 let drawplaceholder l =
2671 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2672 GlDraw.rect
2673 (float l.pagex, float l.pagedispy)
2674 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2676 let x = if margin < 0 then -margin else l.pagex
2677 and y = l.pagedispy + 13 in
2678 GlDraw.color (0.0, 0.0, 0.0);
2679 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2682 let now () = Unix.gettimeofday ();;
2684 let drawpage l =
2685 let color =
2686 match state.mode with
2687 | Textentry _ -> scalecolor 0.4
2688 | View | Outline _ | Items _ -> scalecolor 1.0
2689 | Birdseye (_, _, pageno, hooverpageno, _) ->
2690 if l.pageno = hooverpageno
2691 then scalecolor 0.9
2692 else (
2693 if l.pageno = pageno
2694 then scalecolor 1.0
2695 else scalecolor 0.8
2698 GlDraw.color color;
2699 begin match getopaque l.pageno with
2700 | Some (opaque, _) when validopaque opaque ->
2701 let a = now () in
2702 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2703 opaque;
2704 let b = now () in
2705 let d = b-.a in
2706 vlog "draw %d %f sec" l.pageno d;
2708 | _ ->
2709 drawplaceholder l;
2710 end;
2713 let scrollph y =
2714 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2715 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2716 let sh = float conf.winh /. sh in
2717 let sh = max sh (float conf.scrollh) in
2719 let percent =
2720 if state.y = state.maxy
2721 then 1.0
2722 else float y /. float maxy
2724 let position = (float conf.winh -. sh) *. percent in
2726 let position =
2727 if position +. sh > float conf.winh
2728 then float conf.winh -. sh
2729 else position
2731 position, sh;
2734 let scrollindicator () =
2735 GlDraw.color (0.64 , 0.64, 0.64);
2736 GlDraw.rect
2737 (float (conf.winw - state.scrollw), 0.)
2738 (float conf.winw, float conf.winh)
2740 GlDraw.color (0.0, 0.0, 0.0);
2742 let position, sh = scrollph state.y in
2743 GlDraw.rect
2744 (float (conf.winw - state.scrollw), position)
2745 (float conf.winw, position +. sh)
2749 let showsel margin =
2750 match state.mstate with
2751 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2754 | Msel ((x0, y0), (x1, y1)) ->
2755 let rec loop = function
2756 | l :: ls ->
2757 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2758 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2759 then
2760 match getopaque l.pageno with
2761 | Some (opaque, _) when validopaque opaque ->
2762 let oy = -l.pagey + l.pagedispy in
2763 seltext opaque
2764 (x0 - margin - state.x, y0,
2765 x1 - margin - state.x, y1) oy;
2767 | _ -> ()
2768 else loop ls
2769 | [] -> ()
2771 loop state.layout
2774 let showrects () =
2775 let panx = float state.x in
2776 Gl.enable `blend;
2777 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2778 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2779 List.iter
2780 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2781 List.iter (fun l ->
2782 if l.pageno = pageno
2783 then (
2784 let d = float (l.pagedispy - l.pagey) in
2785 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2786 GlDraw.begins `quads;
2788 GlDraw.vertex2 (x0+.panx, y0+.d);
2789 GlDraw.vertex2 (x1+.panx, y1+.d);
2790 GlDraw.vertex2 (x2+.panx, y2+.d);
2791 GlDraw.vertex2 (x3+.panx, y3+.d);
2793 GlDraw.ends ();
2795 ) state.layout
2796 ) state.rects
2798 Gl.disable `blend;
2801 let showstrings active first pan strings =
2802 Gl.enable `blend;
2803 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2804 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2805 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2806 Gl.disable `blend;
2808 GlDraw.color (1., 1., 1.);
2809 let rec loop row =
2810 if row = Array.length strings || (row - first) * 16 > conf.winh
2811 then ()
2812 else (
2813 let (s, level, _) = strings.(row) in
2814 let y = (row - first) * 16 in
2815 let x = 5 + 15*(max 0 (level+pan)) in
2816 if row = active
2817 then (
2818 Gl.enable `blend;
2819 GlDraw.polygon_mode `both `line;
2820 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2821 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2822 GlDraw.rect (0., float (y + 1))
2823 (float (conf.winw - 1), float (y + 18));
2824 GlDraw.polygon_mode `both `fill;
2825 Gl.disable `blend;
2826 GlDraw.color (1., 1., 1.);
2828 let draw_string s =
2829 let l = String.length s in
2830 if pan < 0
2831 then (
2832 let pan = pan * 2 in
2833 let pos = pan + level in
2834 let left = l + pos in
2835 if left > 0
2836 then
2837 let s =
2838 if left > l
2839 then s
2840 else String.sub s (-pos) left
2842 drawstring 14 x (y + 16) s
2844 else
2845 drawstring 14 (x + pan*15) (y + 16) s
2847 draw_string s;
2848 loop (row+1)
2851 loop first
2854 let showoutline (_, active, first, outlines, _, pan, _) =
2855 showstrings active first pan outlines;
2858 let showitems (active, first, items, _, pan, _) =
2859 showstrings active first pan items;
2862 let display () =
2863 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2864 GlDraw.viewport margin 0 state.w conf.winh;
2865 pagematrix ();
2866 GlClear.color (scalecolor2 conf.bgcolor);
2867 GlClear.clear [`color];
2868 if conf.zoom > 1.0
2869 then (
2870 Gl.enable `scissor_test;
2871 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2873 List.iter drawpage state.layout;
2874 if conf.zoom > 1.0
2875 then
2876 Gl.disable `scissor_test
2878 if state.x != 0
2879 then (
2880 let x = -.float state.x in
2881 GlMat.translate ~x ();
2883 showrects ();
2884 showsel margin;
2885 GlDraw.viewport 0 0 conf.winw conf.winh;
2886 winmatrix ();
2887 scrollindicator ();
2888 begin match state.mode with
2889 | Items items -> showitems items
2890 | Outline outline -> showoutline outline
2891 | _ -> ()
2892 end;
2893 enttext ();
2894 Glut.swapBuffers ();
2897 let getunder x y =
2898 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2899 let x = x - margin - state.x in
2900 let rec f = function
2901 | l :: rest ->
2902 begin match getopaque l.pageno with
2903 | Some (opaque, _) when validopaque opaque ->
2904 let y = y - l.pagedispy in
2905 if y > 0
2906 then
2907 let y = l.pagey + y in
2908 let x = x - l.pagex in
2909 match whatsunder opaque x y with
2910 | Unone -> f rest
2911 | under -> under
2912 else
2913 f rest
2914 | _ ->
2915 f rest
2917 | [] -> Unone
2919 f state.layout
2922 let viewmouse button bstate x y =
2923 match button with
2924 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2925 if Glut.getModifiers () land Glut.active_ctrl != 0
2926 then (
2927 match state.mstate with
2928 | Mzoom (oldn, i) ->
2929 if oldn = n
2930 then (
2931 if i = 2
2932 then
2933 let incr =
2934 match n with
2935 | 4 ->
2936 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2937 | _ ->
2938 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2940 let zoom = conf.zoom +. incr in
2941 setzoom zoom;
2942 state.mstate <- Mzoom (n, 0);
2943 else
2944 state.mstate <- Mzoom (n, i+1);
2946 else state.mstate <- Mzoom (n, 0)
2948 | _ -> state.mstate <- Mzoom (n, 0)
2950 else (
2951 if state.ascrollstep > 0
2952 then
2953 setautoscrollspeed (n=4)
2954 else
2955 let incr =
2956 if n = 3
2957 then -conf.scrollstep
2958 else conf.scrollstep
2960 let incr = incr * 2 in
2961 let y = clamp incr in
2962 gotoy_and_clear_text y
2965 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2966 if bstate = Glut.DOWN
2967 then (
2968 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2969 state.mstate <- Mpan (x, y)
2971 else
2972 state.mstate <- Mnone
2974 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
2975 if bstate = Glut.DOWN
2976 then
2977 let position, sh = scrollph state.y in
2978 if y > truncate position && y < truncate (position +. sh)
2979 then
2980 state.mstate <- Mscroll
2981 else
2982 let percent = float y /. float conf.winh in
2983 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2984 gotoy desty;
2985 state.mstate <- Mscroll
2986 else
2987 state.mstate <- Mnone
2989 | Glut.LEFT_BUTTON ->
2990 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2991 begin match dest with
2992 | Ulinkgoto (pageno, top) ->
2993 if pageno >= 0
2994 then (
2995 addnav ();
2996 gotopage1 pageno top;
2999 | Ulinkuri s ->
3000 print_endline s
3002 | Unone when bstate = Glut.DOWN ->
3003 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3004 state.mstate <- Mpan (x, y);
3006 | Unone | Utext _ ->
3007 if bstate = Glut.DOWN
3008 then (
3009 if conf.angle mod 360 = 0
3010 then (
3011 state.mstate <- Msel ((x, y), (x, y));
3012 Glut.postRedisplay ()
3015 else (
3016 match state.mstate with
3017 | Mnone -> ()
3019 | Mzoom _ | Mscroll ->
3020 state.mstate <- Mnone
3022 | Mpan _ ->
3023 Glut.setCursor Glut.CURSOR_INHERIT;
3024 state.mstate <- Mnone
3026 | Msel ((_, y0), (_, y1)) ->
3027 let f l =
3028 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3029 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3030 then
3031 match getopaque l.pageno with
3032 | Some (opaque, _) when validopaque opaque ->
3033 copysel opaque
3034 | _ -> ()
3036 List.iter f state.layout;
3037 copysel ""; (* ugly *)
3038 Glut.setCursor Glut.CURSOR_INHERIT;
3039 state.mstate <- Mnone;
3043 | _ -> ()
3046 let birdseyemouse button bstate x y
3047 (conf, leftx, _, hooverpageno, anchor) =
3048 match button with
3049 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3050 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3051 let rec loop = function
3052 | [] -> ()
3053 | l :: rest ->
3054 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3055 && x > margin && x < margin + l.pagew
3056 then (
3057 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3059 else loop rest
3061 loop state.layout
3062 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3063 | _ -> ()
3066 let mouse bstate button x y =
3067 match state.mode with
3068 | View -> viewmouse button bstate x y
3069 | Birdseye beye -> birdseyemouse button bstate x y beye
3070 | Textentry _ | Outline _ | Items _ -> ()
3073 let mouse ~button ~state ~x ~y = mouse state button x y;;
3075 let motion ~x ~y =
3076 match state.mode with
3077 | Outline _ -> ()
3078 | _ ->
3079 match state.mstate with
3080 | Mzoom _ | Mnone -> ()
3082 | Mpan (x0, y0) ->
3083 let dx = x - x0
3084 and dy = y0 - y in
3085 state.mstate <- Mpan (x, y);
3086 if conf.zoom > 1.0 then state.x <- state.x + dx;
3087 let y = clamp dy in
3088 gotoy_and_clear_text y
3090 | Msel (a, _) ->
3091 state.mstate <- Msel (a, (x, y));
3092 Glut.postRedisplay ()
3094 | Mscroll ->
3095 let y = min conf.winh (max 0 y) in
3096 let percent = float y /. float conf.winh in
3097 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3098 gotoy_and_clear_text y
3101 let pmotion ~x ~y =
3102 match state.mode with
3103 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3104 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3105 let rec loop = function
3106 | [] ->
3107 if hooverpageno != -1
3108 then (
3109 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3110 Glut.postRedisplay ();
3112 | l :: rest ->
3113 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3114 && x > margin && x < margin + l.pagew
3115 then (
3116 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3117 Glut.postRedisplay ();
3119 else loop rest
3121 loop state.layout
3123 | Outline _ | Items _ | Textentry _ -> ()
3124 | View ->
3125 match state.mstate with
3126 | Mnone ->
3127 begin match getunder x y with
3128 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3129 | Ulinkuri uri ->
3130 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3131 Glut.setCursor Glut.CURSOR_INFO
3132 | Ulinkgoto (page, _) ->
3133 if conf.underinfo
3134 then showtext 'p' ("age: " ^ string_of_int page);
3135 Glut.setCursor Glut.CURSOR_INFO
3136 | Utext s ->
3137 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3138 Glut.setCursor Glut.CURSOR_TEXT
3141 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3146 module State =
3147 struct
3148 open Parser
3150 let home =
3152 match Sys.os_type with
3153 | "Win32" -> Sys.getenv "HOMEPATH"
3154 | _ -> Sys.getenv "HOME"
3155 with exn ->
3156 prerr_endline
3157 ("Can not determine home directory location: " ^
3158 Printexc.to_string exn);
3162 let config_of c attrs =
3163 let apply c k v =
3165 match k with
3166 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3167 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3168 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3169 | "preload" -> { c with preload = bool_of_string v }
3170 | "page-bias" -> { c with pagebias = int_of_string v }
3171 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3172 | "auto-scroll-step" ->
3173 { c with autoscrollstep = max 0 (int_of_string v) }
3174 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3175 | "crop-hack" -> { c with crophack = bool_of_string v }
3176 | "throttle" -> { c with showall = bool_of_string v }
3177 | "highlight-links" -> { c with hlinks = bool_of_string v }
3178 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3179 | "vertical-margin" ->
3180 { c with interpagespace = max 0 (int_of_string v) }
3181 | "zoom" ->
3182 let zoom = float_of_string v /. 100. in
3183 let zoom = max 0.01 (min 2.2 zoom) in
3184 { c with zoom = zoom }
3185 | "presentation" -> { c with presentation = bool_of_string v }
3186 | "rotation-angle" -> { c with angle = int_of_string v }
3187 | "width" -> { c with winw = max 20 (int_of_string v) }
3188 | "height" -> { c with winh = max 20 (int_of_string v) }
3189 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3190 | "proportional-display" -> { c with proportional = bool_of_string v }
3191 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3192 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3193 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3194 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3195 | "persistent-location" -> { c with jumpback = bool_of_string v }
3196 | "background-color" -> { c with bgcolor = color_of_string v }
3197 | "scrollbar-in-presentation" ->
3198 { c with scrollbarinpm = bool_of_string v }
3199 | "ui-font" -> { c with uifont = v }
3200 | _ -> c
3201 with exn ->
3202 prerr_endline ("Error processing attribute (`" ^
3203 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3206 let rec fold c = function
3207 | [] -> c
3208 | (k, v) :: rest ->
3209 let c = apply c k v in
3210 fold c rest
3212 fold c attrs;
3215 let fromstring f pos n v d =
3216 try f v
3217 with exn ->
3218 dolog "Error processing attribute (%S=%S) at %d\n%s"
3219 n v pos (Printexc.to_string exn)
3224 let bookmark_of attrs =
3225 let rec fold title page rely = function
3226 | ("title", v) :: rest -> fold v page rely rest
3227 | ("page", v) :: rest -> fold title v rely rest
3228 | ("rely", v) :: rest -> fold title page v rest
3229 | _ :: rest -> fold title page rely rest
3230 | [] -> title, page, rely
3232 fold "invalid" "0" "0" attrs
3235 let doc_of attrs =
3236 let rec fold path page rely pan = function
3237 | ("path", v) :: rest -> fold v page rely pan rest
3238 | ("page", v) :: rest -> fold path v rely pan rest
3239 | ("rely", v) :: rest -> fold path page v pan rest
3240 | ("pan", v) :: rest -> fold path page rely v rest
3241 | _ :: rest -> fold path page rely pan rest
3242 | [] -> path, page, rely, pan
3244 fold "" "0" "0" "0" attrs
3247 let setconf dst src =
3248 dst.scrollbw <- src.scrollbw;
3249 dst.scrollh <- src.scrollh;
3250 dst.icase <- src.icase;
3251 dst.preload <- src.preload;
3252 dst.pagebias <- src.pagebias;
3253 dst.verbose <- src.verbose;
3254 dst.scrollstep <- src.scrollstep;
3255 dst.maxhfit <- src.maxhfit;
3256 dst.crophack <- src.crophack;
3257 dst.autoscrollstep <- src.autoscrollstep;
3258 dst.showall <- src.showall;
3259 dst.hlinks <- src.hlinks;
3260 dst.underinfo <- src.underinfo;
3261 dst.interpagespace <- src.interpagespace;
3262 dst.zoom <- src.zoom;
3263 dst.presentation <- src.presentation;
3264 dst.angle <- src.angle;
3265 dst.winw <- src.winw;
3266 dst.winh <- src.winh;
3267 dst.savebmarks <- src.savebmarks;
3268 dst.memlimit <- src.memlimit;
3269 dst.proportional <- src.proportional;
3270 dst.texcount <- src.texcount;
3271 dst.sliceheight <- src.sliceheight;
3272 dst.thumbw <- src.thumbw;
3273 dst.jumpback <- src.jumpback;
3274 dst.bgcolor <- src.bgcolor;
3275 dst.scrollbarinpm <- src.scrollbarinpm;
3276 dst.uifont <- src.uifont;
3279 let unent s =
3280 let l = String.length s in
3281 let b = Buffer.create l in
3282 unent b s 0 l;
3283 Buffer.contents b;
3286 let get s =
3287 let h = Hashtbl.create 10 in
3288 let dc = { defconf with angle = defconf.angle } in
3289 let rec toplevel v t spos _ =
3290 match t with
3291 | Vdata | Vcdata | Vend -> v
3292 | Vopen ("llppconfig", _, closed) ->
3293 if closed
3294 then v
3295 else { v with f = llppconfig }
3296 | Vopen _ ->
3297 error "unexpected subelement at top level" s spos
3298 | Vclose _ -> error "unexpected close at top level" s spos
3300 and llppconfig v t spos _ =
3301 match t with
3302 | Vdata | Vcdata | Vend -> v
3303 | Vopen ("defaults", attrs, closed) ->
3304 let c = config_of dc attrs in
3305 setconf dc c;
3306 if closed
3307 then v
3308 else { v with f = skip "defaults" (fun () -> v) }
3310 | Vopen ("doc", attrs, closed) ->
3311 let pathent, spage, srely, span = doc_of attrs in
3312 let path = unent pathent
3313 and pageno = fromstring int_of_string spos "page" spage 0
3314 and rely = fromstring float_of_string spos "rely" srely 0.0
3315 and pan = fromstring int_of_string spos "pan" span 0 in
3316 let c = config_of dc attrs in
3317 let anchor = (pageno, rely) in
3318 if closed
3319 then (Hashtbl.add h path (c, [], pan, anchor); v)
3320 else { v with f = doc path pan anchor c [] }
3322 | Vopen _ ->
3323 error "unexpected subelement in llppconfig" s spos
3325 | Vclose "llppconfig" -> { v with f = toplevel }
3326 | Vclose _ -> error "unexpected close in llppconfig" s spos
3328 and doc path pan anchor c bookmarks v t spos _ =
3329 match t with
3330 | Vdata | Vcdata -> v
3331 | Vend -> error "unexpected end of input in doc" s spos
3332 | Vopen ("bookmarks", _, closed) ->
3333 if closed
3334 then v
3335 else { v with f = pbookmarks path pan anchor c bookmarks }
3337 | Vopen (_, _, _) ->
3338 error "unexpected subelement in doc" s spos
3340 | Vclose "doc" ->
3341 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3342 { v with f = llppconfig }
3344 | Vclose _ -> error "unexpected close in doc" s spos
3346 and pbookmarks path pan anchor c bookmarks v t spos _ =
3347 match t with
3348 | Vdata | Vcdata -> v
3349 | Vend -> error "unexpected end of input in bookmarks" s spos
3350 | Vopen ("item", attrs, closed) ->
3351 let titleent, spage, srely = bookmark_of attrs in
3352 let page = fromstring int_of_string spos "page" spage 0
3353 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3354 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3355 if closed
3356 then { v with f = pbookmarks path pan anchor c bookmarks }
3357 else
3358 let f () = v in
3359 { v with f = skip "item" f }
3361 | Vopen _ ->
3362 error "unexpected subelement in bookmarks" s spos
3364 | Vclose "bookmarks" ->
3365 { v with f = doc path pan anchor c bookmarks }
3367 | Vclose _ -> error "unexpected close in bookmarks" s spos
3369 and skip tag f v t spos _ =
3370 match t with
3371 | Vdata | Vcdata -> v
3372 | Vend ->
3373 error ("unexpected end of input in skipped " ^ tag) s spos
3374 | Vopen (tag', _, closed) ->
3375 if closed
3376 then v
3377 else
3378 let f' () = { v with f = skip tag f } in
3379 { v with f = skip tag' f' }
3380 | Vclose ctag ->
3381 if tag = ctag
3382 then f ()
3383 else error ("unexpected close in skipped " ^ tag) s spos
3386 parse { f = toplevel; accu = () } s;
3387 h, dc;
3390 let do_load f ic =
3392 let len = in_channel_length ic in
3393 let s = String.create len in
3394 really_input ic s 0 len;
3395 f s;
3396 with
3397 | Parse_error (msg, s, pos) ->
3398 let subs = subs s pos in
3399 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3400 failwith ("parse error: " ^ s)
3402 | exn ->
3403 failwith ("config load error: " ^ Printexc.to_string exn)
3406 let path =
3407 let dir =
3409 let dir = Filename.concat home ".config" in
3410 if Sys.is_directory dir then dir else home
3411 with _ -> home
3413 Filename.concat dir "llpp.conf"
3416 let load1 f =
3417 if Sys.file_exists path
3418 then
3419 match
3420 (try Some (open_in_bin path)
3421 with exn ->
3422 prerr_endline
3423 ("Error opening configuation file `" ^ path ^ "': " ^
3424 Printexc.to_string exn);
3425 None
3427 with
3428 | Some ic ->
3429 begin try
3430 f (do_load get ic)
3431 with exn ->
3432 prerr_endline
3433 ("Error loading configuation from `" ^ path ^ "': " ^
3434 Printexc.to_string exn);
3435 end;
3436 close_in ic;
3438 | None -> ()
3439 else
3440 f (Hashtbl.create 0, defconf)
3443 let load () =
3444 let f (h, dc) =
3445 let pc, pb, px, pa =
3447 Hashtbl.find h (Filename.basename state.path)
3448 with Not_found -> dc, [], 0, (0, 0.0)
3450 setconf defconf dc;
3451 setconf conf pc;
3452 state.bookmarks <- pb;
3453 state.x <- px;
3454 state.scrollw <- conf.scrollbw;
3455 if conf.jumpback
3456 then state.anchor <- pa;
3457 cbput state.hists.nav pa;
3459 load1 f
3462 let add_attrs bb always dc c =
3463 let ob s a b =
3464 if always || a != b
3465 then Printf.bprintf bb "\n %s='%b'" s a
3466 and oi s a b =
3467 if always || a != b
3468 then Printf.bprintf bb "\n %s='%d'" s a
3469 and oz s a b =
3470 if always || a <> b
3471 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3472 and oc s a b =
3473 if always || a <> b
3474 then
3475 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3476 and os s a b =
3477 if always || a <> b
3478 then
3479 Printf.bprintf bb "\n %s='%s'" s a
3481 let w, h =
3482 if always
3483 then dc.winw, dc.winh
3484 else
3485 match state.fullscreen with
3486 | Some wh -> wh
3487 | None -> c.winw, c.winh
3489 let zoom, presentation, interpagespace, showall=
3490 if always
3491 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3492 else
3493 match state.mode with
3494 | Birdseye (bc, _, _, _, _) ->
3495 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3496 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3498 oi "width" w dc.winw;
3499 oi "height" h dc.winh;
3500 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3501 oi "scroll-handle-height" c.scrollh dc.scrollh;
3502 ob "case-insensitive-search" c.icase dc.icase;
3503 ob "preload" c.preload dc.preload;
3504 oi "page-bias" c.pagebias dc.pagebias;
3505 oi "scroll-step" c.scrollstep dc.scrollstep;
3506 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3507 ob "max-height-fit" c.maxhfit dc.maxhfit;
3508 ob "crop-hack" c.crophack dc.crophack;
3509 ob "throttle" showall dc.showall;
3510 ob "highlight-links" c.hlinks dc.hlinks;
3511 ob "under-cursor-info" c.underinfo dc.underinfo;
3512 oi "vertical-margin" interpagespace dc.interpagespace;
3513 oz "zoom" zoom dc.zoom;
3514 ob "presentation" presentation dc.presentation;
3515 oi "rotation-angle" c.angle dc.angle;
3516 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3517 ob "proportional-display" c.proportional dc.proportional;
3518 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3519 oi "texcount" c.texcount dc.texcount;
3520 oi "slice-height" c.sliceheight dc.sliceheight;
3521 oi "thumbnail-width" c.thumbw dc.thumbw;
3522 ob "persistent-location" c.jumpback dc.jumpback;
3523 oc "background-color" c.bgcolor dc.bgcolor;
3524 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3525 os "ui-font" c.uifont dc.uifont;
3528 let save () =
3529 let bb = Buffer.create 32768 in
3530 let f (h, dc) =
3531 let dc = if conf.bedefault then conf else dc in
3532 Buffer.add_string bb "<llppconfig>\n<defaults ";
3533 add_attrs bb true dc dc;
3534 Buffer.add_string bb "/>\n";
3536 let adddoc path pan anchor c bookmarks =
3537 if bookmarks == [] && c = dc && anchor = emptyanchor
3538 then ()
3539 else (
3540 Printf.bprintf bb "<doc path='%s'"
3541 (enent path 0 (String.length path));
3543 if anchor <> emptyanchor
3544 then (
3545 let n, y = anchor in
3546 Printf.bprintf bb " page='%d'" n;
3547 if y > 1e-6
3548 then
3549 Printf.bprintf bb " rely='%f'" y
3553 if pan != 0
3554 then Printf.bprintf bb " pan='%d'" pan;
3556 add_attrs bb false dc c;
3558 begin match bookmarks with
3559 | [] -> Buffer.add_string bb "/>\n"
3560 | _ ->
3561 Buffer.add_string bb ">\n<bookmarks>\n";
3562 List.iter (fun (title, _level, (page, rely)) ->
3563 Printf.bprintf bb
3564 "<item title='%s' page='%d'"
3565 (enent title 0 (String.length title))
3566 page
3568 if rely > 1e-6
3569 then
3570 Printf.bprintf bb " rely='%f'" rely
3572 Buffer.add_string bb "/>\n";
3573 ) bookmarks;
3574 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3575 end;
3579 let pan =
3580 match state.mode with
3581 | Birdseye (_, pan, _, _, _) -> pan
3582 | _ -> state.x
3584 let basename = Filename.basename state.path in
3585 adddoc basename pan (getanchor ())
3586 { conf with
3587 autoscrollstep =
3588 if state.ascrollstep > 0
3589 then state.ascrollstep
3590 else conf.autoscrollstep }
3591 (if conf.savebmarks then state.bookmarks else []);
3593 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3594 if basename <> path
3595 then adddoc path x y c bookmarks
3596 ) h;
3597 Buffer.add_string bb "</llppconfig>";
3599 load1 f;
3600 if Buffer.length bb > 0
3601 then
3603 let tmp = path ^ ".tmp" in
3604 let oc = open_out_bin tmp in
3605 Buffer.output_buffer oc bb;
3606 close_out oc;
3607 Sys.rename tmp path;
3608 with exn ->
3609 prerr_endline
3610 ("error while saving configuration: " ^ Printexc.to_string exn)
3612 end;;
3614 let () =
3615 Arg.parse
3616 (Arg.align
3617 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3618 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3619 " Print version and exit")]
3621 (fun s -> state.path <- s)
3622 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3624 if String.length state.path = 0
3625 then (prerr_endline "file name missing"; exit 1);
3627 State.load ();
3629 let _ = Glut.init Sys.argv in
3630 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3631 let () = Glut.initWindowSize conf.winw conf.winh in
3632 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3634 let csock, ssock =
3635 if Sys.os_type = "Unix"
3636 then
3637 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3638 else
3639 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3640 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3641 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3642 Unix.bind sock addr;
3643 Unix.listen sock 1;
3644 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3645 Unix.connect csock addr;
3646 let ssock, _ = Unix.accept sock in
3647 Unix.close sock;
3648 let opts sock =
3649 Unix.setsockopt sock Unix.TCP_NODELAY true;
3650 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3652 opts ssock;
3653 opts csock;
3654 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3655 ssock, csock
3658 let () = Glut.displayFunc display in
3659 let () = Glut.reshapeFunc reshape in
3660 let () = Glut.keyboardFunc keyboard in
3661 let () = Glut.specialFunc special in
3662 let () = Glut.idleFunc (Some idle) in
3663 let () = Glut.mouseFunc mouse in
3664 let () = Glut.motionFunc motion in
3665 let () = Glut.passiveMotionFunc pmotion in
3667 init ssock (
3668 conf.angle, conf.proportional, conf.texcount,
3669 conf.sliceheight, conf.uifont
3671 state.csock <- csock;
3672 state.ssock <- ssock;
3673 state.text <- "Opening " ^ state.path;
3674 writeopen state.path state.password;
3676 at_exit State.save;
3678 let rec handlelablglutbug () =
3680 Glut.mainLoop ();
3681 with Glut.BadEnum "key in special_of_int" ->
3682 showtext '!' " LablGlut bug: special key not recognized";
3683 handlelablglutbug ()
3685 handlelablglutbug ();