Correct handling of enter when nothing is active
[llpp.git] / main.ml
blobc5e3d1618eaeca1c6a8861fc05d15d0eefac8f45
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 >= 0 && 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 else (
2108 state.text <- "";
2109 state.mode <- oldmode
2111 Glut.postRedisplay ();
2113 | _ -> dolog "unknown key %d" key
2116 let outlinekeyboard key
2117 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2118 let narrow outlines pattern =
2119 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2120 match reopt with
2121 | None -> None
2122 | Some re ->
2123 let rec fold accu n =
2124 if n = -1
2125 then accu
2126 else
2127 let (s, _, _) as o = outlines.(n) in
2128 let accu =
2129 if (try ignore (Str.search_forward re s 0); true
2130 with Not_found -> false)
2131 then (o :: accu)
2132 else accu
2134 fold accu (n-1)
2136 let matched = fold [] (Array.length outlines - 1) in
2137 if matched = [] then None else Some (Array.of_list matched)
2139 let search active pattern incr =
2140 let dosearch re =
2141 let rec loop n =
2142 if n = Array.length outlines || n = -1
2143 then None
2144 else
2145 let (s, _, _) = outlines.(n) in
2147 (try ignore (Str.search_forward re s 0); true
2148 with Not_found -> false)
2149 then Some n
2150 else loop (n + incr)
2152 loop active
2155 let re = Str.regexp_case_fold pattern in
2156 dosearch re
2157 with Failure s ->
2158 state.text <- s;
2159 None
2161 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2162 match key with
2163 | 27 -> (* escape *)
2164 if String.length qsearch = 0
2165 then (
2166 state.text <- "";
2167 state.mode <- oldmode;
2168 Glut.postRedisplay ();
2170 else (
2171 state.text <- "";
2172 state.mode <- Outline (
2173 allowdel, active, first, outlines, "", pan, oldmode
2175 Glut.postRedisplay ();
2178 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2179 let incr = if key = 18 then -1 else 1 in
2180 let active, first =
2181 match search (active + incr) qsearch incr with
2182 | None ->
2183 state.text <- qsearch ^ " [not found]";
2184 active, first
2185 | Some active ->
2186 state.text <- qsearch;
2187 active, firstof active
2189 state.mode <- Outline (
2190 allowdel, active, first, outlines, qsearch, pan, oldmode
2192 Glut.postRedisplay ();
2194 | 8 -> (* backspace *)
2195 let len = String.length qsearch in
2196 if len = 0
2197 then ()
2198 else (
2199 if len = 1
2200 then (
2201 state.text <- "";
2202 state.mode <- Outline (
2203 allowdel, active, first, outlines, "", pan, oldmode
2206 else
2207 let qsearch = String.sub qsearch 0 (len - 1) in
2208 let active, first =
2209 match search active qsearch ~-1 with
2210 | None ->
2211 state.text <- qsearch ^ " [not found]";
2212 active, first
2213 | Some active ->
2214 state.text <- qsearch;
2215 active, firstof active
2217 state.mode <- Outline (
2218 allowdel, active, first, outlines, qsearch, pan, oldmode
2221 Glut.postRedisplay ()
2223 | 13 -> (* enter *)
2224 if active < Array.length outlines
2225 then (
2226 let (_, _, anchor) = outlines.(active) in
2227 addnav ();
2228 gotoanchor anchor;
2230 state.text <- "";
2231 if allowdel then state.bookmarks <- Array.to_list outlines;
2232 state.mode <- oldmode;
2233 Glut.postRedisplay ();
2235 | _ when key >= 32 && key < 127 ->
2236 let pattern = addchar qsearch (Char.chr key) in
2237 let active, first =
2238 match search active pattern 1 with
2239 | None ->
2240 state.text <- pattern ^ " [not found]";
2241 active, first
2242 | Some active ->
2243 state.text <- pattern;
2244 active, firstof active
2246 state.mode <- Outline (
2247 allowdel, active, first, outlines, pattern, pan, oldmode
2249 Glut.postRedisplay ()
2251 | 14 when not allowdel -> (* ctrl-n *)
2252 if String.length qsearch > 0
2253 then (
2254 let optoutlines = narrow outlines qsearch in
2255 begin match optoutlines with
2256 | None -> state.text <- "can't narrow"
2257 | Some outlines ->
2258 state.mode <- Outline (
2259 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2261 match state.outlines with
2262 | Olist _ -> ()
2263 | Oarray a ->
2264 state.outlines <- Onarrow (qsearch, outlines, a)
2265 | Onarrow (_, _, b) ->
2266 state.outlines <- Onarrow (qsearch, outlines, b)
2267 end;
2269 Glut.postRedisplay ()
2271 | 21 when not allowdel -> (* ctrl-u *)
2272 let outline =
2273 match state.outlines with
2274 | Oarray a -> a
2275 | Olist l ->
2276 let a = Array.of_list (List.rev l) in
2277 state.outlines <- Oarray a;
2279 | Onarrow (_, _, b) ->
2280 state.outlines <- Oarray b;
2281 state.text <- "";
2284 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2285 Glut.postRedisplay ()
2287 | 12 -> (* ctrl-l *)
2288 state.mode <- Outline
2289 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2290 Glut.postRedisplay ()
2292 | 127 when allowdel -> (* delete *)
2293 let len = Array.length outlines - 1 in
2294 if len = 0
2295 then (
2296 state.mode <- View;
2297 state.bookmarks <- [];
2299 else (
2300 let bookmarks = Array.init len
2301 (fun i ->
2302 let i = if i >= active then i + 1 else i in
2303 outlines.(i)
2306 state.mode <-
2307 Outline (
2308 allowdel,
2309 min active (len-1),
2310 min first (len-1),
2311 bookmarks, qsearch,
2313 oldmode
2316 Glut.postRedisplay ()
2318 | _ -> dolog "unknown key %d" key
2321 let keyboard ~key ~x ~y =
2322 ignore x;
2323 ignore y;
2324 if key = 7 (* ctrl-g *)
2325 then
2326 wcmd "interrupt" []
2327 else
2328 match state.mode with
2329 | Outline outline -> outlinekeyboard key outline
2330 | Textentry textentry -> textentrykeyboard key textentry
2331 | Birdseye birdseye -> birdseyekeyboard key birdseye
2332 | View -> viewkeyboard key
2333 | Items items -> itemskeyboard key items
2336 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2337 match key with
2338 | Glut.KEY_UP -> upbirdseye beye
2339 | Glut.KEY_DOWN -> downbirdseye beye
2341 | Glut.KEY_PAGE_UP ->
2342 begin match state.layout with
2343 | l :: _ ->
2344 if l.pagey != 0
2345 then (
2346 state.mode <- Birdseye (
2347 conf, leftx, l.pageno, hooverpageno, anchor
2349 gotopage1 l.pageno 0;
2351 else (
2352 let layout = layout (state.y-conf.winh) conf.winh in
2353 match layout with
2354 | [] -> gotoy (clamp (-conf.winh))
2355 | l :: _ ->
2356 state.mode <- Birdseye (
2357 conf, leftx, l.pageno, hooverpageno, anchor
2359 gotopage1 l.pageno 0
2362 | [] -> gotoy (clamp (-conf.winh))
2363 end;
2365 | Glut.KEY_PAGE_DOWN ->
2366 begin match List.rev state.layout with
2367 | l :: _ ->
2368 let layout = layout (state.y + conf.winh) conf.winh in
2369 begin match layout with
2370 | [] ->
2371 let incr = l.pageh - l.pagevh in
2372 if incr = 0
2373 then (
2374 state.mode <-
2375 Birdseye (
2376 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2378 Glut.postRedisplay ();
2380 else gotoy (clamp (incr + conf.interpagespace*2));
2382 | l :: _ ->
2383 state.mode <-
2384 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2385 gotopage1 l.pageno 0;
2388 | [] -> gotoy (clamp conf.winh)
2389 end;
2391 | Glut.KEY_HOME ->
2392 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2393 gotopage1 0 0
2395 | Glut.KEY_END ->
2396 let pageno = state.pagecount - 1 in
2397 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2398 if not (pagevisible state.layout pageno)
2399 then
2400 let h =
2401 match List.rev state.pdims with
2402 | [] -> conf.winh
2403 | (_, _, h, _) :: _ -> h
2405 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2406 else Glut.postRedisplay ();
2407 | _ -> ()
2410 let setautoscrollspeed goingdown =
2411 let incr = max 1 (state.ascrollstep / 2) in
2412 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2413 state.ascrollstep <- astep;
2416 let special ~key ~x ~y =
2417 ignore x;
2418 ignore y;
2419 match state.mode with
2420 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2421 togglebirdseye ()
2423 | Birdseye vals ->
2424 birdseyespecial key vals
2426 | View when key = Glut.KEY_F1 ->
2427 enterhelpmode ()
2429 | View ->
2430 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2431 then setautoscrollspeed (key = Glut.KEY_DOWN)
2432 else
2433 let y =
2434 match key with
2435 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2436 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2437 | Glut.KEY_DOWN -> clamp conf.scrollstep
2438 | Glut.KEY_PAGE_UP ->
2439 if Glut.getModifiers () land Glut.active_ctrl != 0
2440 then
2441 match state.layout with
2442 | [] -> state.y
2443 | l :: _ -> state.y - l.pagey
2444 else
2445 clamp (-conf.winh)
2446 | Glut.KEY_PAGE_DOWN ->
2447 if Glut.getModifiers () land Glut.active_ctrl != 0
2448 then
2449 match List.rev state.layout with
2450 | [] -> state.y
2451 | l :: _ -> getpagey l.pageno
2452 else
2453 clamp conf.winh
2454 | Glut.KEY_HOME -> addnav (); 0
2455 | Glut.KEY_END ->
2456 addnav ();
2457 state.maxy - (if conf.maxhfit then conf.winh else 0)
2459 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2460 Glut.getModifiers () land Glut.active_alt != 0 ->
2461 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2463 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2464 state.x <- state.x - 10;
2465 state.y
2466 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2467 state.x <- state.x + 10;
2468 state.y
2470 | _ -> state.y
2472 gotoy_and_clear_text y
2474 | Textentry
2475 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2476 let s =
2477 match key with
2478 | Glut.KEY_UP -> action HCprev
2479 | Glut.KEY_DOWN -> action HCnext
2480 | Glut.KEY_HOME -> action HCfirst
2481 | Glut.KEY_END -> action HClast
2482 | _ -> state.text
2484 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2485 Glut.postRedisplay ()
2487 | Textentry _ -> ()
2489 | Items (active, first, items, qsearch, pan, oldmode) ->
2490 let maxrows = maxoutlinerows () in
2491 let itemcount = Array.length items in
2492 let hasaction = function
2493 | (_, _, Noaction) -> false
2494 | _ -> true
2496 let find start incr =
2497 let rec find i =
2498 if i = -1 || i = itemcount
2499 then -1
2500 else (
2501 if hasaction items.(i)
2502 then i
2503 else find (i + incr)
2506 find start
2508 let set active first =
2509 let first = max 0 (min first (itemcount - maxrows)) in
2510 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2512 let navigate incr =
2513 let isvisible first n = n >= first && n - first <= maxrows in
2514 let active, first =
2515 let incr1 = if incr > 0 then 1 else -1 in
2516 if isvisible first active
2517 then
2518 let next =
2519 let next = active + incr in
2520 let next =
2521 if next < 0 || next >= itemcount
2522 then -1
2523 else find next incr1
2525 if next = -1 || abs (active - next) > maxrows
2526 then -1
2527 else next
2529 if next = -1
2530 then
2531 let first = first + incr in
2532 let first = max 0 (min first (itemcount - 1)) in
2533 let next =
2534 let next = active + incr in
2535 let next = max 0 (min next (itemcount - 1)) in
2536 find next ~-incr1
2538 let active = if next = -1 then active else next in
2539 active, first
2540 else
2541 let first = min next first in
2542 next, first
2543 else
2544 let first = first + incr in
2545 let first = max 0 (min first (itemcount - 1)) in
2546 let active =
2547 let next = active + incr in
2548 let next = max 0 (min next (itemcount - 1)) in
2549 let next = find next incr1 in
2550 if next = -1 || abs (active - first) > maxrows
2551 then active
2552 else next
2554 active, first
2556 set active first;
2557 Glut.postRedisplay ()
2559 begin match key with
2560 | Glut.KEY_UP -> navigate ~-1
2561 | Glut.KEY_DOWN -> navigate 1
2562 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2563 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2565 | Glut.KEY_RIGHT ->
2566 state.mode <- Items (
2567 active, first, items, qsearch, min 0 (pan - 1), oldmode
2569 Glut.postRedisplay ()
2571 | Glut.KEY_LEFT ->
2572 state.mode <- Items (
2573 active, first, items, qsearch, min 0 (pan + 1), oldmode
2575 Glut.postRedisplay ()
2577 | Glut.KEY_HOME ->
2578 let active = find 0 1 in
2579 set active 0;
2580 Glut.postRedisplay ()
2582 | Glut.KEY_END ->
2583 let first = max 0 (itemcount - maxrows) in
2584 let active = find (itemcount - 1) ~-1 in
2585 set active first;
2586 Glut.postRedisplay ()
2588 | _ -> ()
2589 end;
2591 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2592 let maxrows = maxoutlinerows () in
2593 let calcfirst first active =
2594 if active > first
2595 then
2596 let rows = active - first in
2597 if rows > maxrows then active - maxrows else first
2598 else active
2600 let navigate incr =
2601 let active = active + incr in
2602 let active = max 0 (min active (Array.length outlines - 1)) in
2603 let first = calcfirst first active in
2604 state.mode <- Outline (
2605 allowdel, active, first, outlines, qsearch, pan, oldmode
2607 Glut.postRedisplay ()
2609 let updownlevel incr =
2610 let len = Array.length outlines in
2611 let (_, curlevel, _) = outlines.(active) in
2612 let rec flow i =
2613 if i = len then i-1 else if i = -1 then 0 else
2614 let (_, l, _) = outlines.(i) in
2615 if l != curlevel then i else flow (i+incr)
2617 let active = flow active in
2618 let first = calcfirst first active in
2619 state.mode <- Outline (
2620 allowdel, active, first, outlines, qsearch, pan, oldmode
2622 Glut.postRedisplay ()
2624 match key with
2625 | Glut.KEY_UP -> navigate ~-1
2626 | Glut.KEY_DOWN -> navigate 1
2627 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2628 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2630 | Glut.KEY_RIGHT ->
2631 if Glut.getModifiers () land Glut.active_ctrl != 0
2632 then (
2633 state.mode <- Outline (
2634 allowdel, active, first, outlines,
2635 qsearch, min 0 (pan + 1), oldmode
2637 Glut.postRedisplay ();
2639 else (
2640 if not allowdel
2641 then updownlevel 1
2644 | Glut.KEY_LEFT ->
2645 if Glut.getModifiers () land Glut.active_ctrl != 0
2646 then (
2647 state.mode <- Outline (
2648 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2650 Glut.postRedisplay ();
2652 else (
2653 if not allowdel
2654 then updownlevel ~-1
2657 | Glut.KEY_HOME ->
2658 state.mode <- Outline (
2659 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2661 Glut.postRedisplay ()
2663 | Glut.KEY_END ->
2664 let active = Array.length outlines - 1 in
2665 let first = max 0 (active - maxrows) in
2666 state.mode <- Outline (
2667 allowdel, active, first, outlines, qsearch, pan, oldmode
2669 Glut.postRedisplay ()
2671 | _ -> ()
2674 let drawplaceholder l =
2675 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2676 GlDraw.rect
2677 (float l.pagex, float l.pagedispy)
2678 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2680 let x = if margin < 0 then -margin else l.pagex
2681 and y = l.pagedispy + 13 in
2682 GlDraw.color (0.0, 0.0, 0.0);
2683 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2686 let now () = Unix.gettimeofday ();;
2688 let drawpage l =
2689 let color =
2690 match state.mode with
2691 | Textentry _ -> scalecolor 0.4
2692 | View | Outline _ | Items _ -> scalecolor 1.0
2693 | Birdseye (_, _, pageno, hooverpageno, _) ->
2694 if l.pageno = hooverpageno
2695 then scalecolor 0.9
2696 else (
2697 if l.pageno = pageno
2698 then scalecolor 1.0
2699 else scalecolor 0.8
2702 GlDraw.color color;
2703 begin match getopaque l.pageno with
2704 | Some (opaque, _) when validopaque opaque ->
2705 let a = now () in
2706 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2707 opaque;
2708 let b = now () in
2709 let d = b-.a in
2710 vlog "draw %d %f sec" l.pageno d;
2712 | _ ->
2713 drawplaceholder l;
2714 end;
2717 let scrollph y =
2718 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2719 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2720 let sh = float conf.winh /. sh in
2721 let sh = max sh (float conf.scrollh) in
2723 let percent =
2724 if state.y = state.maxy
2725 then 1.0
2726 else float y /. float maxy
2728 let position = (float conf.winh -. sh) *. percent in
2730 let position =
2731 if position +. sh > float conf.winh
2732 then float conf.winh -. sh
2733 else position
2735 position, sh;
2738 let scrollindicator () =
2739 GlDraw.color (0.64 , 0.64, 0.64);
2740 GlDraw.rect
2741 (float (conf.winw - state.scrollw), 0.)
2742 (float conf.winw, float conf.winh)
2744 GlDraw.color (0.0, 0.0, 0.0);
2746 let position, sh = scrollph state.y in
2747 GlDraw.rect
2748 (float (conf.winw - state.scrollw), position)
2749 (float conf.winw, position +. sh)
2753 let showsel margin =
2754 match state.mstate with
2755 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2758 | Msel ((x0, y0), (x1, y1)) ->
2759 let rec loop = function
2760 | l :: ls ->
2761 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2762 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2763 then
2764 match getopaque l.pageno with
2765 | Some (opaque, _) when validopaque opaque ->
2766 let oy = -l.pagey + l.pagedispy in
2767 seltext opaque
2768 (x0 - margin - state.x, y0,
2769 x1 - margin - state.x, y1) oy;
2771 | _ -> ()
2772 else loop ls
2773 | [] -> ()
2775 loop state.layout
2778 let showrects () =
2779 let panx = float state.x in
2780 Gl.enable `blend;
2781 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2782 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2783 List.iter
2784 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2785 List.iter (fun l ->
2786 if l.pageno = pageno
2787 then (
2788 let d = float (l.pagedispy - l.pagey) in
2789 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2790 GlDraw.begins `quads;
2792 GlDraw.vertex2 (x0+.panx, y0+.d);
2793 GlDraw.vertex2 (x1+.panx, y1+.d);
2794 GlDraw.vertex2 (x2+.panx, y2+.d);
2795 GlDraw.vertex2 (x3+.panx, y3+.d);
2797 GlDraw.ends ();
2799 ) state.layout
2800 ) state.rects
2802 Gl.disable `blend;
2805 let showstrings active first pan strings =
2806 Gl.enable `blend;
2807 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2808 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2809 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2810 Gl.disable `blend;
2812 GlDraw.color (1., 1., 1.);
2813 let rec loop row =
2814 if row = Array.length strings || (row - first) * 16 > conf.winh
2815 then ()
2816 else (
2817 let (s, level, _) = strings.(row) in
2818 let y = (row - first) * 16 in
2819 let x = 5 + 15*(max 0 (level+pan)) in
2820 if row = active
2821 then (
2822 Gl.enable `blend;
2823 GlDraw.polygon_mode `both `line;
2824 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2825 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2826 GlDraw.rect (0., float (y + 1))
2827 (float (conf.winw - 1), float (y + 18));
2828 GlDraw.polygon_mode `both `fill;
2829 Gl.disable `blend;
2830 GlDraw.color (1., 1., 1.);
2832 let draw_string s =
2833 let l = String.length s in
2834 if pan < 0
2835 then (
2836 let pan = pan * 2 in
2837 let pos = pan + level in
2838 let left = l + pos in
2839 if left > 0
2840 then
2841 let s =
2842 if left > l
2843 then s
2844 else String.sub s (-pos) left
2846 drawstring 14 x (y + 16) s
2848 else
2849 drawstring 14 (x + pan*15) (y + 16) s
2851 draw_string s;
2852 loop (row+1)
2855 loop first
2858 let showoutline (_, active, first, outlines, _, pan, _) =
2859 showstrings active first pan outlines;
2862 let showitems (active, first, items, _, pan, _) =
2863 showstrings active first pan items;
2866 let display () =
2867 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2868 GlDraw.viewport margin 0 state.w conf.winh;
2869 pagematrix ();
2870 GlClear.color (scalecolor2 conf.bgcolor);
2871 GlClear.clear [`color];
2872 if conf.zoom > 1.0
2873 then (
2874 Gl.enable `scissor_test;
2875 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2877 List.iter drawpage state.layout;
2878 if conf.zoom > 1.0
2879 then
2880 Gl.disable `scissor_test
2882 if state.x != 0
2883 then (
2884 let x = -.float state.x in
2885 GlMat.translate ~x ();
2887 showrects ();
2888 showsel margin;
2889 GlDraw.viewport 0 0 conf.winw conf.winh;
2890 winmatrix ();
2891 scrollindicator ();
2892 begin match state.mode with
2893 | Items items -> showitems items
2894 | Outline outline -> showoutline outline
2895 | _ -> ()
2896 end;
2897 enttext ();
2898 Glut.swapBuffers ();
2901 let getunder x y =
2902 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2903 let x = x - margin - state.x in
2904 let rec f = function
2905 | l :: rest ->
2906 begin match getopaque l.pageno with
2907 | Some (opaque, _) when validopaque opaque ->
2908 let y = y - l.pagedispy in
2909 if y > 0
2910 then
2911 let y = l.pagey + y in
2912 let x = x - l.pagex in
2913 match whatsunder opaque x y with
2914 | Unone -> f rest
2915 | under -> under
2916 else
2917 f rest
2918 | _ ->
2919 f rest
2921 | [] -> Unone
2923 f state.layout
2926 let viewmouse button bstate x y =
2927 match button with
2928 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2929 if Glut.getModifiers () land Glut.active_ctrl != 0
2930 then (
2931 match state.mstate with
2932 | Mzoom (oldn, i) ->
2933 if oldn = n
2934 then (
2935 if i = 2
2936 then
2937 let incr =
2938 match n with
2939 | 4 ->
2940 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2941 | _ ->
2942 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2944 let zoom = conf.zoom +. incr in
2945 setzoom zoom;
2946 state.mstate <- Mzoom (n, 0);
2947 else
2948 state.mstate <- Mzoom (n, i+1);
2950 else state.mstate <- Mzoom (n, 0)
2952 | _ -> state.mstate <- Mzoom (n, 0)
2954 else (
2955 if state.ascrollstep > 0
2956 then
2957 setautoscrollspeed (n=4)
2958 else
2959 let incr =
2960 if n = 3
2961 then -conf.scrollstep
2962 else conf.scrollstep
2964 let incr = incr * 2 in
2965 let y = clamp incr in
2966 gotoy_and_clear_text y
2969 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2970 if bstate = Glut.DOWN
2971 then (
2972 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2973 state.mstate <- Mpan (x, y)
2975 else
2976 state.mstate <- Mnone
2978 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
2979 if bstate = Glut.DOWN
2980 then
2981 let position, sh = scrollph state.y in
2982 if y > truncate position && y < truncate (position +. sh)
2983 then
2984 state.mstate <- Mscroll
2985 else
2986 let percent = float y /. float conf.winh in
2987 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2988 gotoy desty;
2989 state.mstate <- Mscroll
2990 else
2991 state.mstate <- Mnone
2993 | Glut.LEFT_BUTTON ->
2994 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2995 begin match dest with
2996 | Ulinkgoto (pageno, top) ->
2997 if pageno >= 0
2998 then (
2999 addnav ();
3000 gotopage1 pageno top;
3003 | Ulinkuri s ->
3004 print_endline s
3006 | Unone when bstate = Glut.DOWN ->
3007 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3008 state.mstate <- Mpan (x, y);
3010 | Unone | Utext _ ->
3011 if bstate = Glut.DOWN
3012 then (
3013 if conf.angle mod 360 = 0
3014 then (
3015 state.mstate <- Msel ((x, y), (x, y));
3016 Glut.postRedisplay ()
3019 else (
3020 match state.mstate with
3021 | Mnone -> ()
3023 | Mzoom _ | Mscroll ->
3024 state.mstate <- Mnone
3026 | Mpan _ ->
3027 Glut.setCursor Glut.CURSOR_INHERIT;
3028 state.mstate <- Mnone
3030 | Msel ((_, y0), (_, y1)) ->
3031 let f l =
3032 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3033 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3034 then
3035 match getopaque l.pageno with
3036 | Some (opaque, _) when validopaque opaque ->
3037 copysel opaque
3038 | _ -> ()
3040 List.iter f state.layout;
3041 copysel ""; (* ugly *)
3042 Glut.setCursor Glut.CURSOR_INHERIT;
3043 state.mstate <- Mnone;
3047 | _ -> ()
3050 let birdseyemouse button bstate x y
3051 (conf, leftx, _, hooverpageno, anchor) =
3052 match button with
3053 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3054 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3055 let rec loop = function
3056 | [] -> ()
3057 | l :: rest ->
3058 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3059 && x > margin && x < margin + l.pagew
3060 then (
3061 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3063 else loop rest
3065 loop state.layout
3066 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3067 | _ -> ()
3070 let mouse bstate button x y =
3071 match state.mode with
3072 | View -> viewmouse button bstate x y
3073 | Birdseye beye -> birdseyemouse button bstate x y beye
3074 | Textentry _ | Outline _ | Items _ -> ()
3077 let mouse ~button ~state ~x ~y = mouse state button x y;;
3079 let motion ~x ~y =
3080 match state.mode with
3081 | Outline _ -> ()
3082 | _ ->
3083 match state.mstate with
3084 | Mzoom _ | Mnone -> ()
3086 | Mpan (x0, y0) ->
3087 let dx = x - x0
3088 and dy = y0 - y in
3089 state.mstate <- Mpan (x, y);
3090 if conf.zoom > 1.0 then state.x <- state.x + dx;
3091 let y = clamp dy in
3092 gotoy_and_clear_text y
3094 | Msel (a, _) ->
3095 state.mstate <- Msel (a, (x, y));
3096 Glut.postRedisplay ()
3098 | Mscroll ->
3099 let y = min conf.winh (max 0 y) in
3100 let percent = float y /. float conf.winh in
3101 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3102 gotoy_and_clear_text y
3105 let pmotion ~x ~y =
3106 match state.mode with
3107 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3108 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3109 let rec loop = function
3110 | [] ->
3111 if hooverpageno != -1
3112 then (
3113 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3114 Glut.postRedisplay ();
3116 | l :: rest ->
3117 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3118 && x > margin && x < margin + l.pagew
3119 then (
3120 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3121 Glut.postRedisplay ();
3123 else loop rest
3125 loop state.layout
3127 | Outline _ | Items _ | Textentry _ -> ()
3128 | View ->
3129 match state.mstate with
3130 | Mnone ->
3131 begin match getunder x y with
3132 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3133 | Ulinkuri uri ->
3134 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3135 Glut.setCursor Glut.CURSOR_INFO
3136 | Ulinkgoto (page, _) ->
3137 if conf.underinfo
3138 then showtext 'p' ("age: " ^ string_of_int page);
3139 Glut.setCursor Glut.CURSOR_INFO
3140 | Utext s ->
3141 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3142 Glut.setCursor Glut.CURSOR_TEXT
3145 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3150 module State =
3151 struct
3152 open Parser
3154 let home =
3156 match Sys.os_type with
3157 | "Win32" -> Sys.getenv "HOMEPATH"
3158 | _ -> Sys.getenv "HOME"
3159 with exn ->
3160 prerr_endline
3161 ("Can not determine home directory location: " ^
3162 Printexc.to_string exn);
3166 let config_of c attrs =
3167 let apply c k v =
3169 match k with
3170 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3171 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3172 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3173 | "preload" -> { c with preload = bool_of_string v }
3174 | "page-bias" -> { c with pagebias = int_of_string v }
3175 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3176 | "auto-scroll-step" ->
3177 { c with autoscrollstep = max 0 (int_of_string v) }
3178 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3179 | "crop-hack" -> { c with crophack = bool_of_string v }
3180 | "throttle" -> { c with showall = bool_of_string v }
3181 | "highlight-links" -> { c with hlinks = bool_of_string v }
3182 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3183 | "vertical-margin" ->
3184 { c with interpagespace = max 0 (int_of_string v) }
3185 | "zoom" ->
3186 let zoom = float_of_string v /. 100. in
3187 let zoom = max 0.01 (min 2.2 zoom) in
3188 { c with zoom = zoom }
3189 | "presentation" -> { c with presentation = bool_of_string v }
3190 | "rotation-angle" -> { c with angle = int_of_string v }
3191 | "width" -> { c with winw = max 20 (int_of_string v) }
3192 | "height" -> { c with winh = max 20 (int_of_string v) }
3193 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3194 | "proportional-display" -> { c with proportional = bool_of_string v }
3195 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3196 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3197 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3198 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3199 | "persistent-location" -> { c with jumpback = bool_of_string v }
3200 | "background-color" -> { c with bgcolor = color_of_string v }
3201 | "scrollbar-in-presentation" ->
3202 { c with scrollbarinpm = bool_of_string v }
3203 | "ui-font" -> { c with uifont = v }
3204 | _ -> c
3205 with exn ->
3206 prerr_endline ("Error processing attribute (`" ^
3207 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3210 let rec fold c = function
3211 | [] -> c
3212 | (k, v) :: rest ->
3213 let c = apply c k v in
3214 fold c rest
3216 fold c attrs;
3219 let fromstring f pos n v d =
3220 try f v
3221 with exn ->
3222 dolog "Error processing attribute (%S=%S) at %d\n%s"
3223 n v pos (Printexc.to_string exn)
3228 let bookmark_of attrs =
3229 let rec fold title page rely = function
3230 | ("title", v) :: rest -> fold v page rely rest
3231 | ("page", v) :: rest -> fold title v rely rest
3232 | ("rely", v) :: rest -> fold title page v rest
3233 | _ :: rest -> fold title page rely rest
3234 | [] -> title, page, rely
3236 fold "invalid" "0" "0" attrs
3239 let doc_of attrs =
3240 let rec fold path page rely pan = function
3241 | ("path", v) :: rest -> fold v page rely pan rest
3242 | ("page", v) :: rest -> fold path v rely pan rest
3243 | ("rely", v) :: rest -> fold path page v pan rest
3244 | ("pan", v) :: rest -> fold path page rely v rest
3245 | _ :: rest -> fold path page rely pan rest
3246 | [] -> path, page, rely, pan
3248 fold "" "0" "0" "0" attrs
3251 let setconf dst src =
3252 dst.scrollbw <- src.scrollbw;
3253 dst.scrollh <- src.scrollh;
3254 dst.icase <- src.icase;
3255 dst.preload <- src.preload;
3256 dst.pagebias <- src.pagebias;
3257 dst.verbose <- src.verbose;
3258 dst.scrollstep <- src.scrollstep;
3259 dst.maxhfit <- src.maxhfit;
3260 dst.crophack <- src.crophack;
3261 dst.autoscrollstep <- src.autoscrollstep;
3262 dst.showall <- src.showall;
3263 dst.hlinks <- src.hlinks;
3264 dst.underinfo <- src.underinfo;
3265 dst.interpagespace <- src.interpagespace;
3266 dst.zoom <- src.zoom;
3267 dst.presentation <- src.presentation;
3268 dst.angle <- src.angle;
3269 dst.winw <- src.winw;
3270 dst.winh <- src.winh;
3271 dst.savebmarks <- src.savebmarks;
3272 dst.memlimit <- src.memlimit;
3273 dst.proportional <- src.proportional;
3274 dst.texcount <- src.texcount;
3275 dst.sliceheight <- src.sliceheight;
3276 dst.thumbw <- src.thumbw;
3277 dst.jumpback <- src.jumpback;
3278 dst.bgcolor <- src.bgcolor;
3279 dst.scrollbarinpm <- src.scrollbarinpm;
3280 dst.uifont <- src.uifont;
3283 let unent s =
3284 let l = String.length s in
3285 let b = Buffer.create l in
3286 unent b s 0 l;
3287 Buffer.contents b;
3290 let get s =
3291 let h = Hashtbl.create 10 in
3292 let dc = { defconf with angle = defconf.angle } in
3293 let rec toplevel v t spos _ =
3294 match t with
3295 | Vdata | Vcdata | Vend -> v
3296 | Vopen ("llppconfig", _, closed) ->
3297 if closed
3298 then v
3299 else { v with f = llppconfig }
3300 | Vopen _ ->
3301 error "unexpected subelement at top level" s spos
3302 | Vclose _ -> error "unexpected close at top level" s spos
3304 and llppconfig v t spos _ =
3305 match t with
3306 | Vdata | Vcdata | Vend -> v
3307 | Vopen ("defaults", attrs, closed) ->
3308 let c = config_of dc attrs in
3309 setconf dc c;
3310 if closed
3311 then v
3312 else { v with f = skip "defaults" (fun () -> v) }
3314 | Vopen ("doc", attrs, closed) ->
3315 let pathent, spage, srely, span = doc_of attrs in
3316 let path = unent pathent
3317 and pageno = fromstring int_of_string spos "page" spage 0
3318 and rely = fromstring float_of_string spos "rely" srely 0.0
3319 and pan = fromstring int_of_string spos "pan" span 0 in
3320 let c = config_of dc attrs in
3321 let anchor = (pageno, rely) in
3322 if closed
3323 then (Hashtbl.add h path (c, [], pan, anchor); v)
3324 else { v with f = doc path pan anchor c [] }
3326 | Vopen _ ->
3327 error "unexpected subelement in llppconfig" s spos
3329 | Vclose "llppconfig" -> { v with f = toplevel }
3330 | Vclose _ -> error "unexpected close in llppconfig" s spos
3332 and doc path pan anchor c bookmarks v t spos _ =
3333 match t with
3334 | Vdata | Vcdata -> v
3335 | Vend -> error "unexpected end of input in doc" s spos
3336 | Vopen ("bookmarks", _, closed) ->
3337 if closed
3338 then v
3339 else { v with f = pbookmarks path pan anchor c bookmarks }
3341 | Vopen (_, _, _) ->
3342 error "unexpected subelement in doc" s spos
3344 | Vclose "doc" ->
3345 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3346 { v with f = llppconfig }
3348 | Vclose _ -> error "unexpected close in doc" s spos
3350 and pbookmarks path pan anchor c bookmarks v t spos _ =
3351 match t with
3352 | Vdata | Vcdata -> v
3353 | Vend -> error "unexpected end of input in bookmarks" s spos
3354 | Vopen ("item", attrs, closed) ->
3355 let titleent, spage, srely = bookmark_of attrs in
3356 let page = fromstring int_of_string spos "page" spage 0
3357 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3358 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3359 if closed
3360 then { v with f = pbookmarks path pan anchor c bookmarks }
3361 else
3362 let f () = v in
3363 { v with f = skip "item" f }
3365 | Vopen _ ->
3366 error "unexpected subelement in bookmarks" s spos
3368 | Vclose "bookmarks" ->
3369 { v with f = doc path pan anchor c bookmarks }
3371 | Vclose _ -> error "unexpected close in bookmarks" s spos
3373 and skip tag f v t spos _ =
3374 match t with
3375 | Vdata | Vcdata -> v
3376 | Vend ->
3377 error ("unexpected end of input in skipped " ^ tag) s spos
3378 | Vopen (tag', _, closed) ->
3379 if closed
3380 then v
3381 else
3382 let f' () = { v with f = skip tag f } in
3383 { v with f = skip tag' f' }
3384 | Vclose ctag ->
3385 if tag = ctag
3386 then f ()
3387 else error ("unexpected close in skipped " ^ tag) s spos
3390 parse { f = toplevel; accu = () } s;
3391 h, dc;
3394 let do_load f ic =
3396 let len = in_channel_length ic in
3397 let s = String.create len in
3398 really_input ic s 0 len;
3399 f s;
3400 with
3401 | Parse_error (msg, s, pos) ->
3402 let subs = subs s pos in
3403 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3404 failwith ("parse error: " ^ s)
3406 | exn ->
3407 failwith ("config load error: " ^ Printexc.to_string exn)
3410 let path =
3411 let dir =
3413 let dir = Filename.concat home ".config" in
3414 if Sys.is_directory dir then dir else home
3415 with _ -> home
3417 Filename.concat dir "llpp.conf"
3420 let load1 f =
3421 if Sys.file_exists path
3422 then
3423 match
3424 (try Some (open_in_bin path)
3425 with exn ->
3426 prerr_endline
3427 ("Error opening configuation file `" ^ path ^ "': " ^
3428 Printexc.to_string exn);
3429 None
3431 with
3432 | Some ic ->
3433 begin try
3434 f (do_load get ic)
3435 with exn ->
3436 prerr_endline
3437 ("Error loading configuation from `" ^ path ^ "': " ^
3438 Printexc.to_string exn);
3439 end;
3440 close_in ic;
3442 | None -> ()
3443 else
3444 f (Hashtbl.create 0, defconf)
3447 let load () =
3448 let f (h, dc) =
3449 let pc, pb, px, pa =
3451 Hashtbl.find h (Filename.basename state.path)
3452 with Not_found -> dc, [], 0, (0, 0.0)
3454 setconf defconf dc;
3455 setconf conf pc;
3456 state.bookmarks <- pb;
3457 state.x <- px;
3458 state.scrollw <- conf.scrollbw;
3459 if conf.jumpback
3460 then state.anchor <- pa;
3461 cbput state.hists.nav pa;
3463 load1 f
3466 let add_attrs bb always dc c =
3467 let ob s a b =
3468 if always || a != b
3469 then Printf.bprintf bb "\n %s='%b'" s a
3470 and oi s a b =
3471 if always || a != b
3472 then Printf.bprintf bb "\n %s='%d'" s a
3473 and oz s a b =
3474 if always || a <> b
3475 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3476 and oc s a b =
3477 if always || a <> b
3478 then
3479 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3480 and os s a b =
3481 if always || a <> b
3482 then
3483 Printf.bprintf bb "\n %s='%s'" s a
3485 let w, h =
3486 if always
3487 then dc.winw, dc.winh
3488 else
3489 match state.fullscreen with
3490 | Some wh -> wh
3491 | None -> c.winw, c.winh
3493 let zoom, presentation, interpagespace, showall=
3494 if always
3495 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3496 else
3497 match state.mode with
3498 | Birdseye (bc, _, _, _, _) ->
3499 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3500 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3502 oi "width" w dc.winw;
3503 oi "height" h dc.winh;
3504 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3505 oi "scroll-handle-height" c.scrollh dc.scrollh;
3506 ob "case-insensitive-search" c.icase dc.icase;
3507 ob "preload" c.preload dc.preload;
3508 oi "page-bias" c.pagebias dc.pagebias;
3509 oi "scroll-step" c.scrollstep dc.scrollstep;
3510 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3511 ob "max-height-fit" c.maxhfit dc.maxhfit;
3512 ob "crop-hack" c.crophack dc.crophack;
3513 ob "throttle" showall dc.showall;
3514 ob "highlight-links" c.hlinks dc.hlinks;
3515 ob "under-cursor-info" c.underinfo dc.underinfo;
3516 oi "vertical-margin" interpagespace dc.interpagespace;
3517 oz "zoom" zoom dc.zoom;
3518 ob "presentation" presentation dc.presentation;
3519 oi "rotation-angle" c.angle dc.angle;
3520 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3521 ob "proportional-display" c.proportional dc.proportional;
3522 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3523 oi "texcount" c.texcount dc.texcount;
3524 oi "slice-height" c.sliceheight dc.sliceheight;
3525 oi "thumbnail-width" c.thumbw dc.thumbw;
3526 ob "persistent-location" c.jumpback dc.jumpback;
3527 oc "background-color" c.bgcolor dc.bgcolor;
3528 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3529 os "ui-font" c.uifont dc.uifont;
3532 let save () =
3533 let bb = Buffer.create 32768 in
3534 let f (h, dc) =
3535 let dc = if conf.bedefault then conf else dc in
3536 Buffer.add_string bb "<llppconfig>\n<defaults ";
3537 add_attrs bb true dc dc;
3538 Buffer.add_string bb "/>\n";
3540 let adddoc path pan anchor c bookmarks =
3541 if bookmarks == [] && c = dc && anchor = emptyanchor
3542 then ()
3543 else (
3544 Printf.bprintf bb "<doc path='%s'"
3545 (enent path 0 (String.length path));
3547 if anchor <> emptyanchor
3548 then (
3549 let n, y = anchor in
3550 Printf.bprintf bb " page='%d'" n;
3551 if y > 1e-6
3552 then
3553 Printf.bprintf bb " rely='%f'" y
3557 if pan != 0
3558 then Printf.bprintf bb " pan='%d'" pan;
3560 add_attrs bb false dc c;
3562 begin match bookmarks with
3563 | [] -> Buffer.add_string bb "/>\n"
3564 | _ ->
3565 Buffer.add_string bb ">\n<bookmarks>\n";
3566 List.iter (fun (title, _level, (page, rely)) ->
3567 Printf.bprintf bb
3568 "<item title='%s' page='%d'"
3569 (enent title 0 (String.length title))
3570 page
3572 if rely > 1e-6
3573 then
3574 Printf.bprintf bb " rely='%f'" rely
3576 Buffer.add_string bb "/>\n";
3577 ) bookmarks;
3578 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3579 end;
3583 let pan =
3584 match state.mode with
3585 | Birdseye (_, pan, _, _, _) -> pan
3586 | _ -> state.x
3588 let basename = Filename.basename state.path in
3589 adddoc basename pan (getanchor ())
3590 { conf with
3591 autoscrollstep =
3592 if state.ascrollstep > 0
3593 then state.ascrollstep
3594 else conf.autoscrollstep }
3595 (if conf.savebmarks then state.bookmarks else []);
3597 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3598 if basename <> path
3599 then adddoc path x y c bookmarks
3600 ) h;
3601 Buffer.add_string bb "</llppconfig>";
3603 load1 f;
3604 if Buffer.length bb > 0
3605 then
3607 let tmp = path ^ ".tmp" in
3608 let oc = open_out_bin tmp in
3609 Buffer.output_buffer oc bb;
3610 close_out oc;
3611 Sys.rename tmp path;
3612 with exn ->
3613 prerr_endline
3614 ("error while saving configuration: " ^ Printexc.to_string exn)
3616 end;;
3618 let () =
3619 Arg.parse
3620 (Arg.align
3621 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3622 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3623 " Print version and exit")]
3625 (fun s -> state.path <- s)
3626 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3628 if String.length state.path = 0
3629 then (prerr_endline "file name missing"; exit 1);
3631 State.load ();
3633 let _ = Glut.init Sys.argv in
3634 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3635 let () = Glut.initWindowSize conf.winw conf.winh in
3636 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3638 let csock, ssock =
3639 if Sys.os_type = "Unix"
3640 then
3641 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3642 else
3643 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3644 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3645 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3646 Unix.bind sock addr;
3647 Unix.listen sock 1;
3648 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3649 Unix.connect csock addr;
3650 let ssock, _ = Unix.accept sock in
3651 Unix.close sock;
3652 let opts sock =
3653 Unix.setsockopt sock Unix.TCP_NODELAY true;
3654 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3656 opts ssock;
3657 opts csock;
3658 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3659 ssock, csock
3662 let () = Glut.displayFunc display in
3663 let () = Glut.reshapeFunc reshape in
3664 let () = Glut.keyboardFunc keyboard in
3665 let () = Glut.specialFunc special in
3666 let () = Glut.idleFunc (Some idle) in
3667 let () = Glut.mouseFunc mouse in
3668 let () = Glut.motionFunc motion in
3669 let () = Glut.passiveMotionFunc pmotion in
3671 init ssock (
3672 conf.angle, conf.proportional, conf.texcount,
3673 conf.sliceheight, conf.uifont
3675 state.csock <- csock;
3676 state.ssock <- ssock;
3677 state.text <- "Opening " ^ state.path;
3678 writeopen state.path state.password;
3680 at_exit State.save;
3682 let rec handlelablglutbug () =
3684 Glut.mainLoop ();
3685 with Glut.BadEnum "key in special_of_int" ->
3686 showtext '!' " LablGlut bug: special key not recognized";
3687 handlelablglutbug ()
3689 handlelablglutbug ();