4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
13 angle
* proportional
* texcount
* sliceheight
* blockwidth
* fontpath
22 and proportional
= bool
23 and interpagespace
= int
32 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
33 external draw
: (int * int * int * bool) -> string -> unit = "ml_draw";;
34 external seltext
: string -> (int * int * int * int) -> int -> unit =
36 external copysel
: string -> unit = "ml_copysel";;
37 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
38 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
39 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
40 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
41 external measurestr
: int -> string -> float = "ml_measure_string";;
42 external getmaxw
: unit -> float = "ml_getmaxw";;
46 | Msel
of (mpos
* mpos
)
49 | Mzoom
of (int * int)
53 type textentry
= string * string * onhist
option * onkey
* ondone
54 and onkey
= string -> int -> te
55 and ondone
= string -> unit
56 and histcancel
= unit -> unit
57 and onhist
= ((histcmd
-> string) * histcancel
)
58 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
63 | TEswitch
of textentry
75 { store
= Array.create n v
82 let cbcap b
= Array.length b
.store
;;
87 b
.wc
<- (b
.wc
+ 1) mod cap;
89 b
.len
<- min
(b
.len
+ 1) cap;
92 let cbempty b
= b
.len
= 0;;
94 let cbgetg b circular dir
=
98 let rc = b
.rc + dir
in
110 else max
0 (min
rc (b
.len
-1))
116 let cbget b
= cbgetg b
false;;
117 let cbgetc b
= cbgetg b
true;;
120 let rc = b
.wc
- b
.len
in
121 let rc = if rc < 0 then cbcap b
+ rc else rc in
125 let cbdecr b
= b
.len
<- b
.len
- 1;;
140 { mutable scrollbw
: int
141 ; mutable scrollh
: int
142 ; mutable icase
: bool
143 ; mutable preload
: bool
144 ; mutable pagebias
: int
145 ; mutable verbose
: bool
146 ; mutable scrollstep
: int
147 ; mutable maxhfit
: bool
148 ; mutable crophack
: bool
149 ; mutable autoscrollstep
: int
150 ; mutable showall
: bool
151 ; mutable hlinks
: bool
152 ; mutable underinfo
: bool
153 ; mutable interpagespace
: interpagespace
154 ; mutable zoom
: float
155 ; mutable presentation
: bool
156 ; mutable angle
: angle
159 ; mutable savebmarks
: bool
160 ; mutable proportional
: proportional
161 ; mutable memlimit
: int
162 ; mutable texcount
: texcount
163 ; mutable sliceheight
: sliceheight
164 ; mutable blockwidth
: blockwidth
165 ; mutable thumbw
: width
166 ; mutable jumpback
: bool
167 ; mutable bgcolor
: float * float * float
168 ; mutable bedefault
: bool
169 ; mutable scrollbarinpm
: bool
173 type anchor
= pageno
* top
;;
175 type outline
= string * int * anchor
177 | Oarray
of outline array
178 | Olist
of outline list
179 | Onarrow
of string * outline array
* outline array
182 type rect
= float * float * float * float * float * float * float * float;;
184 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
186 let emptyanchor = (0, 0.0);;
189 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
190 | Outline
of (bool * int * int * outline array
* string * int * mode
)
191 | Items
of (int * int * item array
* string * int * mode
)
192 | Textentry
of (textentry
* onleave
)
194 and onleave
= leavetextentrystatus
-> unit
195 and leavetextentrystatus
= | Cancel
| Confirm
196 and item
= string * int * action
199 | Action
of (int -> int -> string -> int -> mode
)
202 let isbirdseye = function Birdseye _
-> true | _
-> false;;
203 let istextentry = function Textentry _
-> true | _
-> false;;
206 { mutable csock
: Unix.file_descr
207 ; mutable ssock
: Unix.file_descr
211 ; mutable scrollw
: int
212 ; mutable anchor
: anchor
214 ; mutable layout
: layout list
215 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
216 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
217 ; mutable pagecount
: int
218 ; pagecache
: string circbuf
219 ; mutable rendering
: bool
220 ; mutable mstate
: mstate
221 ; mutable searchpattern
: string
222 ; mutable rects
: (pageno
* recttype
* rect
) list
223 ; mutable rects1
: (pageno
* recttype
* rect
) list
224 ; mutable text
: string
225 ; mutable fullscreen
: (width
* height
) option
226 ; mutable mode
: mode
227 ; mutable outlines
: outlines
228 ; mutable bookmarks
: outline list
229 ; mutable path
: string
230 ; mutable password
: string
231 ; mutable invalidated
: int
232 ; mutable colorscale
: float
233 ; mutable memused
: int
235 ; mutable throttle
: layout list
option
236 ; mutable autoscroll
:int option
237 ; mutable help
: item array
238 ; mutable docinfo
: (int * string) list
239 ; mutable deadline
: float
243 { pat
: string circbuf
244 ; pag
: string circbuf
245 ; nav
: anchor circbuf
265 ; presentation
= false
270 ; proportional
= true
271 ; memlimit
= 32*1024*1024
277 ; bgcolor
= (0.5, 0.5, 0.5)
279 ; scrollbarinpm
= true
283 let conf = { defconf with angle
= defconf.angle
};;
286 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
287 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
297 ; anchor
= emptyanchor
300 ; pagemap
= Hashtbl.create
10
301 ; pagecache
= cbnew 100 ""
312 ; outlines
= Olist
[]
318 { nav
= cbnew 100 (0, 0.0)
336 Printf.kprintf prerr_endline fmt
338 Printf.kprintf ignore fmt
342 let len = String.length s
in
344 let b = Buffer.create
n in
345 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
346 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
347 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
348 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
349 Buffer.add_string
b s
;
350 let s'
= Buffer.contents
b in
351 let n'
= Unix.write fd
s'
0 n in
352 if n'
!= n then failwith
"write failed";
357 let n = Unix.read fd
s 0 4 in
358 if n != 4 then failwith
"incomplete read(len)";
360 lor (Char.code
s.[0] lsl 24)
361 lor (Char.code
s.[1] lsl 16)
362 lor (Char.code
s.[2] lsl 8)
363 lor (Char.code
s.[3] lsl 0)
365 let s = String.create
len in
366 let n = Unix.read fd
s 0 len in
367 if n != len then failwith
"incomplete read(data)";
372 let b = Buffer.create
10 in
373 Buffer.add_string
b s;
374 let rec combine = function
377 Buffer.add_char
b ' '
;
380 | `
b b -> if b then "1" else "0"
382 | `i i
-> string_of_int i
383 | `f f
-> string_of_float f
384 | `I f
-> string_of_int
(truncate f
)
386 Buffer.add_string
b s;
393 let cmd = Buffer.contents
(makecmd s l
) in
394 writecmd state.csock
cmd;
400 let d = conf.winh
- h
in
407 let rec f pn ph pi fh l
=
409 | (n, _
, h
, _
) :: rest
->
410 let ips = calcips h
in
415 if isbirdseye state.mode
&& pn
= 0
420 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
425 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
429 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
432 let fh = f 0 0 0 0 state.pdims
in
436 let getpageyh pageno
=
437 let rec f pn ph pi y l
=
439 | (n, _
, h
, _
) :: rest
->
440 let ips = calcips h
in
443 let h = if n = pageno
then h else ph
in
444 if conf.presentation
&& n = pageno
446 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
448 y
+ (pageno
- pn
) * (ph
+ pi
), h
450 let y = y + (if conf.presentation
then pi
else 0) in
451 let y = y + (n - pn
) * (ph
+ pi
) in
455 y + (pageno
- pn
) * (ph
+ pi
), ph
457 f 0 0 0 0 state.pdims
460 let getpagey pageno
= fst
(getpageyh pageno
);;
463 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
464 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
466 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
467 let ips = calcips h in
469 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
473 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
475 prev
, pdims
, pdimno
, 0
477 let dy = dy + yinc in
478 let py = py + yinc in
479 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
484 if py + h <= vy - yinc
486 let py = py + h + ips in
487 let dy = max
0 (py - y) in
497 let pagey = vy - py in
498 let pagevh = h - pagey in
499 let pagevh = min
(sh
- dy) pagevh in
500 let off = if yinc > 0 then py - vy else 0 in
501 let py = py + h + ips in
507 ; pagedispy
= dy + off
508 ; pagey = pagey + off
509 ; pagevh = pagevh - off
513 let accu = e :: accu in
520 ~cacheleft
:(cacheleft
-1)
523 if state.invalidated
= 0
533 ~cacheleft
:(cbcap state.pagecache
)
543 let y = state.y + incr
in
545 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
549 let getopaque pageno
=
550 try Some
(Hashtbl.find
state.pagemap
551 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
552 with Not_found
-> None
555 let cache pageno opaque
=
556 Hashtbl.replace
state.pagemap
557 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
560 let validopaque opaque
= String.length opaque
> 0;;
563 match getopaque l
.pageno
with
564 | None
when not
state.rendering
->
565 state.rendering
<- true;
566 cache l
.pageno
("", -1);
567 wcmd "render" [`i
(l
.pageno
+ 1)
574 let loadlayout layout =
575 let rec f all
= function
577 begin match getopaque l
.pageno
with
578 | None
-> render l
; f false ls
579 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
583 f (layout <> []) layout;
586 let findpageforopaque opaque
=
588 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
592 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
596 if conf.preload && not
state.rendering
598 let memleft = conf.memlimit
- state.memused
in
601 let opaque = cbpeek state.pagecache
in
602 match findpageforopaque opaque with
603 | Some
((n, _
, _
, _
, _
), size
) ->
604 memleft + size
>= 0 && not
(pagevisible state.layout n)
611 let presentation = conf.presentation in
612 let interpagespace = conf.interpagespace in
613 let maxy = state.maxy in
614 conf.presentation <- false;
615 conf.interpagespace <- 0;
616 state.maxy <- calcheight ();
618 match state.layout with
620 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
622 let y = if y < conf.winh
then 0 else y - conf.winh
in
623 let h = state.y - y + conf.winh
*3 in
624 let pages = layout y h in
625 List.iter
render pages;
626 conf.presentation <- presentation;
627 conf.interpagespace <- interpagespace;
633 let y = min
state.maxy y in
634 let pages = layout y conf.winh
in
635 let ready = loadlayout pages in
641 state.layout <- pages;
642 state.throttle
<- None
;
643 Glut.postRedisplay
();
646 state.throttle
<- Some
pages;
651 state.layout <- pages;
652 state.throttle
<- None
;
653 Glut.postRedisplay
();
655 begin match state.mode
with
656 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
657 if not
(pagevisible pages pageno
)
659 match state.layout with
662 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
669 let gotoy_and_clear_text y =
671 if not
conf.verbose
then state.text
<- "";
675 match state.layout with
677 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
680 let getanchory (n, top
) =
681 let y, h = getpageyh n in
682 y + (truncate
(top
*. float h));
685 let gotoanchor anchor
=
686 gotoy (getanchory anchor
);
690 cbput state.hists
.nav
(getanchor ());
694 let anchor = cbgetc state.hists
.nav dir
in
699 let y, h = getpageyh n in
700 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
703 let gotopage1 n top
=
704 let y = getpagey n in
705 gotoy_and_clear_text (y + top
);
713 state.invalidated
<- state.invalidated
+ 1;
717 let c = c *. state.colorscale
in
721 let scalecolor2 (r
, g
, b) =
722 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
726 state.maxy <- calcheight ();
727 match state.mode
with
728 | Birdseye
(_
, _
, pageno
, _
, _
) ->
729 let y, h = getpageyh pageno
in
730 let top = (conf.winh
- h) / 2 in
731 gotoy (max
0 (y - top))
732 | _
-> gotoanchor state.anchor
736 GlMat.mode `projection
;
737 GlMat.load_identity
();
738 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
739 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
740 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
743 GlMat.translate ~x
:(float state.x
) ();
748 GlMat.mode `projection
;
749 GlMat.load_identity
();
750 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
751 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
752 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
756 let firsttime = ref true in
758 if state.invalidated
= 0 && not
!firsttime
759 then state.anchor <- getanchor ();
763 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
767 GlMat.mode `modelview
;
768 GlMat.load_identity
();
769 GlClear.color
(scalecolor 1.0);
770 GlClear.clear
[`color
];
773 wcmd "geometry" [`i
w; `i
h];
776 let drawstring size x
y s =
778 Gl.enable `texture_2d
;
779 ignore
(drawstr size x
y s);
781 Gl.disable `texture_2d
;
784 let drawstring1 size x
y s =
789 let len = String.length
state.text
in
791 GlDraw.color
(0.0, 0.0, 0.0);
793 (0.0, float (conf.winh
- 18))
794 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
796 GlDraw.color
(1.0, 1.0, 1.0);
797 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
799 match state.mode
with
800 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
804 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
806 Printf.sprintf
"%s%s_" prefix text
811 if len > 0 then drawstring state.text
815 state.text
<- Printf.sprintf
"%c%s" c s;
816 Glut.postRedisplay
();
825 state.rects
<- state.rects1
;
826 Glut.postRedisplay
()
829 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
830 state.pagecount
<- n;
831 state.invalidated
<- state.invalidated
- 1;
832 if state.invalidated
= 0
836 let s = Scanf.sscanf
cmd "t %n"
837 (fun n -> String.sub
cmd n (String.length
cmd - n))
839 Glut.setWindowTitle
s
842 let s = Scanf.sscanf
cmd "T %n"
843 (fun n -> String.sub
cmd n (String.length
cmd - n))
845 if istextentry state.mode
852 Glut.postRedisplay
();
858 let s = Scanf.sscanf
cmd "V %n"
859 (fun n -> String.sub
cmd n (String.length
cmd - n))
865 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
866 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
867 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
868 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
870 let y = (getpagey pageno) + truncate y0
in
873 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
876 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
877 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
878 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
879 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
882 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
885 let n, w, _h
, r
, l
, s, p
=
886 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
887 (fun n w h r l
s p
->
888 (n-1, w, h, r
, l
!= 0, s, p
))
891 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
892 state.memused
<- state.memused
+ s;
895 match state.throttle
with
896 | None
-> state.layout
897 | Some
layout -> layout
901 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
904 let evictedopaque = cbpeek state.pagecache
in
905 match findpageforopaque evictedopaque with
906 | None
-> failwith
"bug in gc"
907 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
908 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
910 wcmd "free" [`
s evictedopaque];
911 state.memused
<- state.memused
- evictedsize
;
912 Hashtbl.remove
state.pagemap k
;
913 cbdecr state.pagecache
;
920 cbput state.pagecache p
;
921 state.rendering
<- false;
923 begin match state.throttle
with
925 if pagevisible state.layout n
928 let allvisible = loadlayout state.layout in
929 if allvisible then preload ();
936 let y = getpagey l
.pageno + l
.pagey in
942 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
944 state.pdims
<- pdim :: state.pdims
947 let (l
, n, t
, h, pos
) =
948 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
950 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
951 let outline = (s, l
, (n, float t
/. float h)) in
953 match state.outlines with
954 | Olist
outlines -> Olist
(outline :: outlines)
955 | Oarray _
-> Olist
[outline]
956 | Onarrow _
-> Olist
[outline]
958 state.outlines <- outlines
962 if String.length
cmd > 1 && cmd.[1] = '
e'
964 state.docinfo
<- List.rev
state.docinfo
966 let s = Scanf.sscanf
cmd "i %n"
967 (fun n -> String.sub
cmd n (String.length
cmd - n))
969 state.docinfo
<- (1, s) :: state.docinfo
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday
;;
978 if state.deadline
== nan
then state.deadline
<- now ();
982 then max
0.0 (state.deadline
-. now ())
985 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
986 state.deadline
<- state.deadline
+. delay
;
989 begin match state.autoscroll
with
990 | Some step
when step
!= 0 ->
991 let y = state.y + step
in
995 else if y >= state.maxy then 0 else y
999 then state.text
<- "";
1004 let cmd = readcmd state.csock
in
1013 let action = function
1014 | HCprev
-> cbget cb ~
-1
1015 | HCnext
-> cbget cb
1
1016 | HCfirst
-> cbget cb ~
-(cb
.rc)
1017 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1018 and cancel
() = cb
.rc <- rc
1022 let search pattern forward
=
1023 if String.length pattern
> 0
1026 match state.layout with
1029 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1032 let b = makecmd "search"
1033 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1035 Buffer.add_char
b '
,'
;
1036 Buffer.add_string
b pattern
;
1037 Buffer.add_char
b '
\000'
;
1040 writecmd state.csock
cmd;
1043 let intentry text key
=
1044 let c = Char.unsafe_chr key
in
1047 let s = "x" in s.[0] <- c;
1048 let text = text ^
s in
1052 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1057 let b = Buffer.create
(String.length
s + 1) in
1058 Buffer.add_string
b s;
1059 Buffer.add_char
b c;
1063 let textentry text key
=
1064 let c = Char.unsafe_chr key
in
1066 | _
when key
>= 32 && key
< 127 ->
1067 let text = addchar text c in
1071 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1075 let reinit angle proportional
=
1076 state.anchor <- getanchor ();
1077 conf.angle
<- angle
;
1078 conf.proportional
<- proportional
;
1080 wcmd "reinit" [`i angle
; `
b proportional
];
1084 let zoom = max
0.01 zoom in
1085 if zoom <> conf.zoom
1090 reshape conf.winw
conf.winh
;
1091 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1095 let enterbirdseye () =
1096 let zoom = float conf.thumbw
/. float conf.winw
in
1097 let birdseyepageno =
1098 let cy = conf.winh
/ 2 in
1102 let rec fold best
= function
1105 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1106 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1107 if abs
d < abs dbest
1114 state.mode
<- Birdseye
(
1115 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1118 conf.presentation <- false;
1119 conf.interpagespace <- 10;
1120 conf.hlinks
<- false;
1122 state.mstate
<- Mnone
;
1123 conf.showall
<- false;
1124 Glut.setCursor
Glut.CURSOR_INHERIT
;
1127 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1132 reshape conf.winw
conf.winh
;
1135 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1137 conf.zoom <- c.zoom;
1138 conf.presentation <- c.presentation;
1139 conf.interpagespace <- c.interpagespace;
1140 conf.showall
<- c.showall
;
1141 conf.hlinks
<- c.hlinks
;
1145 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1148 reshape conf.winw
conf.winh
;
1149 state.anchor <- if goback
then anchor else (pageno, 0.0);
1152 let togglebirdseye () =
1153 match state.mode
with
1154 | Birdseye vals
-> leavebirdseye vals
true
1155 | View
| Outline _
-> enterbirdseye ()
1159 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1160 let pageno = max
0 (pageno - 1) in
1161 let rec loop = function
1162 | [] -> gotopage1 pageno 0
1163 | l
:: _
when l
.pageno = pageno ->
1164 if l
.pagedispy
>= 0 && l
.pagey = 0
1165 then Glut.postRedisplay
()
1166 else gotopage1 pageno 0
1167 | _
:: rest
-> loop rest
1170 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1173 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1174 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1175 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1176 let rec loop = function
1178 let y, h = getpageyh pageno in
1179 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1181 | l
:: _
when l
.pageno = pageno ->
1182 if l
.pagevh != l
.pageh
1183 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1184 else Glut.postRedisplay
()
1185 | _
:: rest
-> loop rest
1190 let optentry mode _ key
=
1191 let btos b = if b then "on" else "off" in
1192 let c = Char.unsafe_chr key
in
1196 try conf.scrollstep
<- int_of_string
s with exc
->
1197 state.text <- Printf.sprintf
"bad integer `%s': %s"
1198 s (Printexc.to_string exc
)
1200 TEswitch
("scroll step: ", "", None
, intentry, ondone)
1205 conf.autoscrollstep
<- int_of_string
s;
1206 if state.autoscroll
<> None
1207 then state.autoscroll
<- Some
conf.autoscrollstep
1209 state.text <- Printf.sprintf
"bad integer `%s': %s"
1210 s (Printexc.to_string exc
)
1212 TEswitch
("auto scroll step: ", "", None
, intentry, ondone)
1217 let zoom = float (int_of_string
s) /. 100.0 in
1220 state.text <- Printf.sprintf
"bad integer `%s': %s"
1221 s (Printexc.to_string exc
)
1223 TEswitch
("zoom: ", "", None
, intentry, ondone)
1228 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1230 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1231 begin match mode
with
1233 leavebirdseye beye
false;
1238 state.text <- Printf.sprintf
"bad integer `%s': %s"
1239 s (Printexc.to_string exc
)
1241 TEswitch
("thumbnail width: ", "", None
, intentry, ondone)
1246 Some
(int_of_string
s)
1248 state.text <- Printf.sprintf
"bad integer `%s': %s"
1249 s (Printexc.to_string exc
);
1252 | Some angle
-> reinit angle
conf.proportional
1255 TEswitch
("rotation: ", "", None
, intentry, ondone)
1258 conf.icase
<- not
conf.icase
;
1259 TEdone
("case insensitive search " ^
(btos conf.icase
))
1262 conf.preload <- not
conf.preload;
1264 TEdone
("preload " ^
(btos conf.preload))
1267 conf.verbose
<- not
conf.verbose
;
1268 TEdone
("verbose " ^
(btos conf.verbose
))
1271 conf.maxhfit
<- not
conf.maxhfit
;
1272 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1273 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1276 conf.crophack
<- not
conf.crophack
;
1277 TEdone
("crophack " ^
btos conf.crophack
)
1280 conf.showall
<- not
conf.showall
;
1281 TEdone
("throttle " ^
btos conf.showall
)
1284 conf.underinfo
<- not
conf.underinfo
;
1285 TEdone
("underinfo " ^
btos conf.underinfo
)
1288 conf.savebmarks
<- not
conf.savebmarks
;
1289 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1295 match state.layout with
1300 conf.interpagespace <- int_of_string
s;
1301 state.maxy <- calcheight ();
1302 let y = getpagey pageno in
1305 state.text <- Printf.sprintf
"bad integer `%s': %s"
1306 s (Printexc.to_string exc
)
1308 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1311 reinit conf.angle
(not
conf.proportional
);
1312 TEdone
("proprortional display " ^
btos conf.proportional
)
1315 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1319 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1321 let enterselector allowdel
outlines errmsg msg
=
1322 if Array.length
outlines = 0
1324 showtext ' ' errmsg
;
1328 Glut.setCursor
Glut.CURSOR_INHERIT
;
1330 match state.layout with
1332 | {pageno=pageno} :: _
-> pageno
1336 if n = Array.length
outlines
1339 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1340 if outlinepageno
>= pageno then n else loop (n+1)
1344 state.mode
<- Outline
1345 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1347 Glut.postRedisplay
();
1351 let enteroutlinemode () =
1353 match state.outlines with
1356 let a = Array.of_list
(List.rev l
) in
1357 state.outlines <- Oarray
a;
1359 | Onarrow
(pat
, a, _
) ->
1360 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1362 enterselector false outlines "Document has no outline" msg
;
1365 let enterbookmarkmode () =
1366 let bookmarks = Array.of_list
state.bookmarks in
1367 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1370 let mode_to_string mode
=
1371 let b = Buffer.create
10 in
1372 let rec f = function
1373 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1374 | View
-> Buffer.add_string
b "View"
1375 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1376 | Items _
-> Buffer.add_string
b "Items"
1377 | Outline _
-> Buffer.add_string
b "Outline"
1383 let color_of_string s =
1384 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1385 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1389 let color_to_string (r, g
, b) =
1390 let r = truncate
(r *. 256.0)
1391 and g
= truncate
(g
*. 256.0)
1392 and b = truncate
(b *. 256.0) in
1393 Printf.sprintf
"%d/%d/%d" r g
b
1396 let enterinfomode () =
1397 let btos = function true -> "on" | _
-> "off" in
1398 let mode = state.mode in
1399 let rec makeitems () =
1400 let intp name get set
=
1401 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1402 fun active first _ pan
->
1407 state.text <- Printf.sprintf
"bad integer `%s': %s"
1408 s (Printexc.to_string exn
);
1411 if n != max_int
then set
n;
1413 let te = name ^
": ", "", None
, intentry, ondone in
1418 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1421 and boolp name get set
=
1422 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1423 fun active first qsearch pan
->
1426 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1428 and colorp name get set
=
1429 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1430 fun active first _ pan
->
1431 let invalid = (nan
, nan
, nan
) in
1434 try color_of_string s
1436 state.text <- Printf.sprintf
"bad color `%s': %s"
1437 s (Printexc.to_string exn
);
1443 let te = name ^
": ", "", None
, textentry, ondone in
1448 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1453 let birdseye = isbirdseye mode in
1455 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1457 boolp
"presentation"
1458 (fun () -> conf.presentation)
1460 conf.presentation <- v;
1461 state.anchor <- getanchor ();
1464 boolp
"ignore case in searches"
1465 (fun () -> conf.icase
)
1466 (fun v -> conf.icase
<- v);
1469 (fun () -> conf.preload)
1470 (fun v -> conf.preload <- v);
1473 (fun () -> conf.verbose
)
1474 (fun v -> conf.verbose
<- v);
1477 (fun () -> conf.maxhfit
)
1478 (fun v -> conf.maxhfit
<- v);
1481 (fun () -> conf.crophack
)
1482 (fun v -> conf.crophack
<- v);
1485 (fun () -> conf.showall
)
1486 (fun v -> conf.showall
<- v);
1488 boolp
"highlight links"
1489 (fun () -> conf.hlinks
)
1490 (fun v -> conf.hlinks
<- v);
1493 (fun () -> conf.underinfo
)
1494 (fun v -> conf.underinfo
<- v);
1495 boolp
"persistent bookmarks"
1496 (fun () -> conf.savebmarks
)
1497 (fun v -> conf.savebmarks
<- v);
1499 boolp
"proportional display"
1500 (fun () -> conf.proportional
)
1501 (fun v -> reinit conf.angle
v);
1503 boolp
"persistent location"
1504 (fun () -> conf.jumpback
)
1505 (fun v -> conf.jumpback
<- v);
1509 intp "vertical margin"
1510 (fun () -> conf.interpagespace)
1512 conf.interpagespace <- n;
1514 match state.layout with
1519 state.maxy <- calcheight ();
1520 let y = getpagey pageno in
1525 (fun () -> conf.pagebias
)
1526 (fun v -> conf.pagebias
<- v);
1529 (fun () -> conf.scrollstep
)
1530 (fun n -> conf.scrollstep
<- n);
1532 intp "auto scroll step"
1534 match state.autoscroll
with
1536 | _
-> conf.autoscrollstep
)
1538 if state.autoscroll
<> None
1539 then state.autoscroll
<- Some
n;
1540 conf.autoscrollstep
<- n);
1543 (fun () -> truncate
(conf.zoom *. 100.))
1544 (fun v -> setzoom ((float v) /. 100.));
1547 (fun () -> conf.angle
)
1548 (fun v -> reinit v conf.proportional
);
1550 intp "scroll bar width"
1551 (fun () -> state.scrollw
)
1555 reshape conf.winw
conf.winh
;
1558 intp "scroll handle height"
1559 (fun () -> conf.scrollh
)
1560 (fun v -> conf.scrollh
<- v;);
1562 intp "thumbnail width"
1563 (fun () -> conf.thumbw
)
1565 conf.thumbw
<- min
1920 v;
1568 leavebirdseye beye
false;
1573 colorp
"background color"
1574 (fun () -> conf.bgcolor
)
1575 (fun v -> conf.bgcolor
<- v);
1578 "Presentation mode", 0, Noaction
;
1580 boolp
"scrollbar visible"
1581 (fun () -> conf.scrollbarinpm
)
1583 if v != conf.scrollbarinpm
1585 conf.scrollbarinpm
<- v;
1586 if conf.presentation
1588 state.scrollw
<- if v then conf.scrollbw
else 0;
1589 reshape conf.winw
conf.winh
;
1595 "Pixmap Cache", 0, Noaction
;
1597 intp "size (advisory)"
1598 (fun () -> conf.memlimit
)
1599 (fun v -> conf.memlimit
<- v);
1600 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1607 :: ("Document", 0, Noaction
)
1608 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1614 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1615 (btos conf.bedefault
),
1618 fun active first qsearch pan
->
1619 conf.bedefault
<- not
conf.bedefault
;
1620 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1625 Array.of_list
(items @ tailer)
1628 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1629 Glut.postRedisplay
();
1632 let enterhelpmode () =
1633 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1634 Glut.postRedisplay
();
1637 let quickbookmark ?title
() =
1638 match state.layout with
1644 let sec = Unix.gettimeofday
() in
1645 let tm = Unix.localtime
sec in
1646 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1650 (tm.Unix.tm_year
+ 1900)
1653 | Some
title -> title
1656 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1661 state.fullscreen
<- None
;
1662 Glut.reshapeWindow
w h;
1665 let writeopen path password
=
1666 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1669 let opendoc path password
=
1672 state.password
<- password
;
1673 state.gen
<- state.gen
+ 1;
1674 state.docinfo
<- [];
1676 writeopen path password
;
1677 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1678 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1681 let viewkeyboard key
=
1683 let mode = state.mode in
1684 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1687 Glut.postRedisplay
()
1689 let c = Char.chr key
in
1691 | '
\027'
| 'q'
-> (* escape *)
1694 | '
\008'
-> (* backspace *)
1695 let y = getnav ~
-1 in
1696 gotoy_and_clear_text y
1704 Glut.postRedisplay
()
1707 let ondone isforw
s =
1708 cbput state.hists
.pat
s;
1709 state.searchpattern
<- s;
1712 let s = String.create
1 in
1714 enttext (s, "", Some
(onhist state.hists
.pat
),
1715 textentry, ondone (c ='
/'
))
1717 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1718 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1719 setzoom (conf.zoom +. incr)
1724 try int_of_string
s with exc
->
1725 state.text <- Printf.sprintf
"bad integer `%s': %s"
1726 s (Printexc.to_string exc
);
1732 state.text <- "page bias is now " ^ string_of_int
n;
1735 enttext ("page bias: ", "", None
, intentry, ondone)
1737 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1738 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1739 setzoom (max
0.01 (conf.zoom -. decr))
1742 let ondone msg
= state.text <- msg
in
1744 "option [acfhilpstvAPRSZ]: ", "", None
,
1745 optentry state.mode, ondone
1748 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1751 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1752 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1756 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1762 try int_of_string
s with exc
->
1763 state.text <- Printf.sprintf
"bad integer `%s': %s"
1764 s (Printexc.to_string exc
);
1770 cbput state.hists
.pag
(string_of_int
n);
1771 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1774 let pageentry text key
=
1775 match Char.unsafe_chr key
with
1776 | 'g'
-> TEdone
text
1777 | _
-> intentry text key
1779 let text = "x" in text.[0] <- c;
1780 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1783 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1784 reshape conf.winw
conf.winh
;
1787 conf.hlinks
<- not
conf.hlinks
;
1788 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1789 Glut.postRedisplay
()
1792 begin match state.autoscroll
with
1794 conf.autoscrollstep
<- step
;
1795 state.autoscroll
<- None
1797 if conf.autoscrollstep
= 0
1798 then state.autoscroll
<- Some
1
1799 else state.autoscroll
<- Some
conf.autoscrollstep
1803 conf.presentation <- not
conf.presentation;
1804 if conf.presentation
1806 if not
conf.scrollbarinpm
1807 then state.scrollw
<- 0;
1810 state.scrollw
<- conf.scrollbw
;
1812 showtext ' '
("presentation mode " ^
1813 if conf.presentation then "on" else "off");
1814 state.anchor <- getanchor ();
1818 begin match state.fullscreen
with
1820 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1823 state.fullscreen
<- None
;
1828 gotoy_and_clear_text 0
1831 gotopage1 (state.pagecount
- 1) 0
1834 search state.searchpattern
true
1837 search state.searchpattern
false
1840 begin match state.layout with
1843 gotoy_and_clear_text (getpagey l
.pageno)
1847 begin match List.rev
state.layout with
1850 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1851 gotoy_and_clear_text (getpagey pageno)
1854 | '
\127'
-> (* del *)
1855 begin match state.layout with
1858 let pageno = max
0 (l
.pageno-1) in
1859 gotoy_and_clear_text (getpagey pageno)
1864 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1866 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1868 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1872 else (100. *. (float state.y /. float maxy)) in
1875 Printf.sprintf
"Page %d of %d %.2f%%"
1876 (fn+1) state.pagecount
percent
1879 "Pages %d-%d of %d %.2f%%"
1880 (fn+1) (ln
+1) state.pagecount
percent
1885 begin match state.layout with
1888 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1889 Glut.postRedisplay
();
1893 enterbookmarkmode ()
1903 match state.layout with
1906 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1910 enttext ("bookmark: ", "", None
, textentry, ondone)
1914 showtext ' '
"Quick bookmark added";
1917 begin match state.layout with
1919 let rect = getpdimrect l
.pagedimno
in
1923 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1924 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1926 (truncate
(rect.(1) -. rect.(0)),
1927 truncate
(rect.(3) -. rect.(0)))
1931 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1933 Glut.postRedisplay
();
1938 | '
\000'
-> (* ctrl-2 *)
1939 let maxw = getmaxw
() in
1941 then setzoom (maxw /. float conf.winw
)
1944 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1949 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1950 Glut.postRedisplay
()
1953 begin match state.mode with
1954 | Birdseye beye
-> upbirdseye beye
1955 | _
-> gotoy (clamp (-conf.scrollstep
))
1959 begin match state.mode with
1960 | Birdseye beye
-> downbirdseye beye
1961 | _
-> gotoy (clamp conf.scrollstep
)
1965 state.anchor <- getanchor ();
1966 opendoc state.path
state.password
1969 vlog "huh? %d %c" key
(Char.chr key
);
1972 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1974 state.mode <- Textentry
(te, onleave
);
1977 Glut.postRedisplay
()
1979 match Char.unsafe_chr key
with
1980 | '
\008'
-> (* backspace *)
1981 let len = String.length
text in
1985 Glut.postRedisplay
();
1988 let s = String.sub
text 0 (len - 1) in
1989 enttext (c, s, opthist
, onkey
, ondone)
1995 Glut.postRedisplay
()
1997 | '
\007'
(* ctrl-g *)
1998 | '
\027'
-> (* escape *)
1999 begin match opthist
with
2001 | Some
(_
, onhistcancel
) -> onhistcancel
()
2004 Glut.postRedisplay
()
2007 begin match onkey
text key
with
2011 Glut.postRedisplay
()
2014 enttext (c, text, opthist
, onkey
, ondone);
2018 Glut.postRedisplay
()
2021 state.mode <- Textentry
(te, onleave
);
2022 Glut.postRedisplay
()
2026 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2028 | 27 -> (* escape *)
2029 leavebirdseye beye
true
2031 | 12 -> (* ctrl-l *)
2032 let y, h = getpageyh pageno in
2033 let top = (conf.winh
- h) / 2 in
2034 gotoy (max
0 (y - top))
2037 leavebirdseye beye
false
2043 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2044 let set active first qsearch
=
2045 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2047 let search active pattern
incr =
2050 if n >= Array.length
items || n < 0
2053 let (s, _
, _
) = items.(n) in
2055 (try ignore
(Str.search_forward re
s 0); true
2056 with Not_found
-> false)
2058 else loop (n + incr)
2063 let re = Str.regexp_case_fold pattern
in
2069 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2071 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2072 let incr = if key
= 18 then -1 else 1 in
2074 match search (active + incr) qsearch
incr with
2076 state.text <- qsearch ^
" [not found]";
2079 state.text <- qsearch
;
2080 active, firstof active
2082 set active first qsearch
;
2083 Glut.postRedisplay
();
2085 | 8 -> (* backspace *)
2086 let len = String.length qsearch
in
2093 set active first
"";
2096 let qsearch = String.sub
qsearch 0 (len - 1) in
2098 match search active qsearch ~
-1 with
2100 state.text <- qsearch ^
" [not found]";
2103 state.text <- qsearch;
2104 active, firstof active
2106 set active first
qsearch
2108 Glut.postRedisplay
()
2110 | _
when key
>= 32 && key
< 127 ->
2111 let pattern = addchar qsearch (Char.chr key
) in
2113 match search active pattern 1 with
2115 state.text <- pattern ^
" [not found]";
2118 state.text <- pattern;
2119 active, firstof active
2121 set active first
pattern;
2122 Glut.postRedisplay
()
2124 | 27 -> (* escape *)
2126 if String.length
qsearch = 0
2128 state.mode <- oldmode
;
2131 set active first
"";
2133 Glut.postRedisplay
()
2136 if active >= 0 && active < Array.length
items
2138 match items.(active) with
2140 state.mode <- f active first
qsearch pan
2144 state.mode <- oldmode
2148 state.mode <- oldmode
2150 Glut.postRedisplay
();
2152 | _
-> dolog "unknown key %d" key
2155 let outlinekeyboard key
2156 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2157 let narrow outlines pattern =
2158 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2162 let rec fold accu n =
2166 let (s, _
, _
) as o
= outlines.(n) in
2168 if (try ignore
(Str.search_forward
re s 0); true
2169 with Not_found
-> false)
2175 let matched = fold [] (Array.length
outlines - 1) in
2176 if matched = [] then None
else Some
(Array.of_list
matched)
2178 let search active pattern incr =
2181 if n = Array.length
outlines || n = -1
2184 let (s, _
, _
) = outlines.(n) in
2186 (try ignore
(Str.search_forward
re s 0); true
2187 with Not_found
-> false)
2189 else loop (n + incr)
2194 let re = Str.regexp_case_fold
pattern in
2200 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2202 | 27 -> (* escape *)
2204 if String.length
qsearch = 0
2206 state.mode <- oldmode
;
2209 state.mode <- Outline
(
2210 allowdel
, active, first
, outlines, "", pan
, oldmode
2213 Glut.postRedisplay
();
2215 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2216 let incr = if key
= 18 then -1 else 1 in
2218 match search (active + incr) qsearch incr with
2220 state.text <- qsearch ^
" [not found]";
2223 state.text <- qsearch;
2224 active, firstof active
2226 state.mode <- Outline
(
2227 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2229 Glut.postRedisplay
();
2231 | 8 -> (* backspace *)
2232 let len = String.length
qsearch in
2239 state.mode <- Outline
(
2240 allowdel
, active, first
, outlines, "", pan
, oldmode
2244 let qsearch = String.sub
qsearch 0 (len - 1) in
2246 match search active qsearch ~
-1 with
2248 state.text <- qsearch ^
" [not found]";
2251 state.text <- qsearch;
2252 active, firstof active
2254 state.mode <- Outline
(
2255 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2258 Glut.postRedisplay
()
2261 if active < Array.length
outlines
2263 let (_
, _
, anchor) = outlines.(active) in
2268 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2269 state.mode <- oldmode
;
2270 Glut.postRedisplay
();
2272 | _
when key
>= 32 && key
< 127 ->
2273 let pattern = addchar qsearch (Char.chr key
) in
2275 match search active pattern 1 with
2277 state.text <- pattern ^
" [not found]";
2280 state.text <- pattern;
2281 active, firstof active
2283 state.mode <- Outline
(
2284 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2286 Glut.postRedisplay
()
2288 | 14 when not allowdel
-> (* ctrl-n *)
2289 if String.length
qsearch > 0
2291 let optoutlines = narrow outlines qsearch in
2292 begin match optoutlines with
2293 | None
-> state.text <- "can't narrow"
2295 state.mode <- Outline
(
2296 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2298 match state.outlines with
2301 state.outlines <- Onarrow
(qsearch, outlines, a)
2302 | Onarrow
(_
, _
, b) ->
2303 state.outlines <- Onarrow
(qsearch, outlines, b)
2306 Glut.postRedisplay
()
2308 | 21 when not allowdel
-> (* ctrl-u *)
2310 match state.outlines with
2313 let a = Array.of_list
(List.rev l
) in
2314 state.outlines <- Oarray
a;
2316 | Onarrow
(_
, _
, b) ->
2317 state.outlines <- Oarray
b;
2321 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2322 Glut.postRedisplay
()
2324 | 12 -> (* ctrl-l *)
2325 state.mode <- Outline
2326 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2327 Glut.postRedisplay
()
2329 | 127 when allowdel
-> (* delete *)
2330 let len = Array.length
outlines - 1 in
2334 state.bookmarks <- [];
2337 let bookmarks = Array.init
len
2339 let i = if i >= active then i + 1 else i in
2353 Glut.postRedisplay
()
2355 | _
-> dolog "unknown key %d" key
2358 let keyboard ~key ~x ~
y =
2361 if key
= 7 && not
(istextentry state.mode) (* ctrl-g *)
2365 match state.mode with
2366 | Outline
outline -> outlinekeyboard key
outline
2367 | Textentry
textentry -> textentrykeyboard key
textentry
2368 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2369 | View
-> viewkeyboard key
2370 | Items
items -> itemskeyboard key
items
2373 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2375 | Glut.KEY_UP
-> upbirdseye beye
2376 | Glut.KEY_DOWN
-> downbirdseye beye
2378 | Glut.KEY_PAGE_UP
->
2379 begin match state.layout with
2383 state.mode <- Birdseye
(
2384 conf, leftx
, l
.pageno, hooverpageno
, anchor
2386 gotopage1 l
.pageno 0;
2389 let layout = layout (state.y-conf.winh
) conf.winh
in
2391 | [] -> gotoy (clamp (-conf.winh
))
2393 state.mode <- Birdseye
(
2394 conf, leftx
, l
.pageno, hooverpageno
, anchor
2396 gotopage1 l
.pageno 0
2399 | [] -> gotoy (clamp (-conf.winh
))
2402 | Glut.KEY_PAGE_DOWN
->
2403 begin match List.rev
state.layout with
2405 let layout = layout (state.y + conf.winh
) conf.winh
in
2406 begin match layout with
2408 let incr = l
.pageh
- l
.pagevh in
2413 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2415 Glut.postRedisplay
();
2417 else gotoy (clamp (incr + conf.interpagespace*2));
2421 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2422 gotopage1 l
.pageno 0;
2425 | [] -> gotoy (clamp conf.winh
)
2429 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2433 let pageno = state.pagecount
- 1 in
2434 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2435 if not
(pagevisible state.layout pageno)
2438 match List.rev
state.pdims
with
2440 | (_
, _
, h, _
) :: _
-> h
2442 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2443 else Glut.postRedisplay
();
2447 let setautoscrollspeed step goingdown
=
2448 let incr = max
1 ((abs step
) / 2) in
2449 let incr = if goingdown
then incr else -incr in
2450 let astep = step
+ incr in
2451 state.autoscroll
<- Some
astep;
2454 let special ~key ~x ~
y =
2457 match state.mode with
2458 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2462 birdseyespecial key vals
2464 | View
when key
= Glut.KEY_F1
->
2468 begin match state.autoscroll
with
2469 | Some step
when key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
->
2470 setautoscrollspeed step
(key
= Glut.KEY_DOWN
)
2475 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2476 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2477 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2478 | Glut.KEY_PAGE_UP
->
2479 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2481 match state.layout with
2483 | l
:: _
-> state.y - l
.pagey
2486 | Glut.KEY_PAGE_DOWN
->
2487 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2489 match List.rev
state.layout with
2491 | l
:: _
-> getpagey l
.pageno
2494 | Glut.KEY_HOME
-> addnav (); 0
2497 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2499 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2500 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2501 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2503 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2504 state.x
<- state.x
- 10;
2506 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2507 state.x
<- state.x
+ 10;
2512 gotoy_and_clear_text y
2516 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2519 | Glut.KEY_UP
-> action HCprev
2520 | Glut.KEY_DOWN
-> action HCnext
2521 | Glut.KEY_HOME
-> action HCfirst
2522 | Glut.KEY_END
-> action HClast
2525 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2526 Glut.postRedisplay
()
2530 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2531 let maxrows = maxoutlinerows () in
2532 let itemcount = Array.length
items in
2533 let hasaction = function
2534 | (_
, _
, Noaction
) -> false
2537 let find start
incr =
2539 if i = -1 || i = itemcount
2542 if hasaction items.(i)
2544 else find (i + incr)
2549 let set active first
=
2550 let first = max
0 (min
first (itemcount - maxrows)) in
2551 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2554 let isvisible first n = n >= first && n - first <= maxrows in
2556 let incr1 = if incr > 0 then 1 else -1 in
2557 if isvisible first active
2560 let next = active + incr in
2562 if next < 0 || next >= itemcount
2564 else find next incr1
2566 if next = -1 || abs
(active - next) > maxrows
2572 let first = first + incr in
2573 let first = max
0 (min
first (itemcount - 1)) in
2575 let next = active + incr in
2576 let next = max
0 (min
next (itemcount - 1)) in
2579 let active = if next = -1 then active else next in
2582 let first = min
next first in
2585 let first = first + incr in
2586 let first = max
0 (min
first (itemcount - 1)) in
2588 let next = active + incr in
2589 let next = max
0 (min
next (itemcount - 1)) in
2590 let next = find next incr1 in
2591 if next = -1 || abs
(active - first) > maxrows
2598 Glut.postRedisplay
()
2600 begin match key
with
2601 | Glut.KEY_UP
-> navigate ~
-1
2602 | Glut.KEY_DOWN
-> navigate 1
2603 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2604 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2607 state.mode <- Items
(
2608 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2610 Glut.postRedisplay
()
2613 state.mode <- Items
(
2614 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2616 Glut.postRedisplay
()
2619 let active = find 0 1 in
2621 Glut.postRedisplay
()
2624 let first = max
0 (itemcount - maxrows) in
2625 let active = find (itemcount - 1) ~
-1 in
2627 Glut.postRedisplay
()
2632 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2633 let maxrows = maxoutlinerows () in
2634 let calcfirst first active =
2637 let rows = active - first in
2638 if rows > maxrows then active - maxrows else first
2642 let active = active + incr in
2643 let active = max
0 (min
active (Array.length
outlines - 1)) in
2644 let first = calcfirst first active in
2645 state.mode <- Outline
(
2646 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2648 Glut.postRedisplay
()
2650 let updownlevel incr =
2651 let len = Array.length
outlines in
2652 let (_
, curlevel
, _
) = outlines.(active) in
2654 if i = len then i-1 else if i = -1 then 0 else
2655 let (_
, l
, _
) = outlines.(i) in
2656 if l
!= curlevel
then i else flow (i+incr)
2658 let active = flow active in
2659 let first = calcfirst first active in
2660 state.mode <- Outline
(
2661 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2663 Glut.postRedisplay
()
2666 | Glut.KEY_UP
-> navigate ~
-1
2667 | Glut.KEY_DOWN
-> navigate 1
2668 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2669 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2672 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2674 state.mode <- Outline
(
2675 allowdel
, active, first, outlines,
2676 qsearch, min
0 (pan
+ 1), oldmode
2678 Glut.postRedisplay
();
2686 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2688 state.mode <- Outline
(
2689 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2691 Glut.postRedisplay
();
2695 then updownlevel ~
-1
2699 state.mode <- Outline
(
2700 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2702 Glut.postRedisplay
()
2705 let active = Array.length
outlines - 1 in
2706 let first = max
0 (active - maxrows) in
2707 state.mode <- Outline
(
2708 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2710 Glut.postRedisplay
()
2715 let drawplaceholder l
=
2716 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2718 (float l
.pagex
, float l
.pagedispy
)
2719 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2721 let x = if margin < 0 then -margin else l
.pagex
2722 and y = l
.pagedispy
+ 13 in
2723 GlDraw.color
(0.0, 0.0, 0.0);
2724 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2727 let now () = Unix.gettimeofday
();;
2731 match state.mode with
2732 | Textentry _
-> scalecolor 0.4
2733 | View
| Outline _
| Items _
-> scalecolor 1.0
2734 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2735 if l
.pageno = hooverpageno
2738 if l
.pageno = pageno
2744 begin match getopaque l
.pageno with
2745 | Some
(opaque, _
) when validopaque opaque ->
2747 draw
(l
.pagedispy
, l
.pagevh, l
.pagey, conf.hlinks
) opaque;
2750 vlog "draw %d %f sec" l
.pageno d;
2758 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2759 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2760 let sh = float conf.winh
/. sh in
2761 let sh = max
sh (float conf.scrollh
) in
2764 if state.y = state.maxy
2766 else float y /. float maxy
2768 let position = (float conf.winh
-. sh) *. percent in
2771 if position +. sh > float conf.winh
2772 then float conf.winh
-. sh
2778 let scrollindicator () =
2779 GlDraw.color (0.64 , 0.64, 0.64);
2781 (float (conf.winw
- state.scrollw
), 0.)
2782 (float conf.winw
, float conf.winh
)
2784 GlDraw.color (0.0, 0.0, 0.0);
2786 let position, sh = scrollph state.y in
2788 (float (conf.winw
- state.scrollw
), position)
2789 (float conf.winw
, position +. sh)
2793 let showsel margin =
2794 match state.mstate
with
2795 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2798 | Msel
((x0
, y0
), (x1
, y1
)) ->
2799 let rec loop = function
2801 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2802 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2804 match getopaque l
.pageno with
2805 | Some
(opaque, _
) when validopaque opaque ->
2806 let oy = -l
.pagey + l
.pagedispy
in
2808 (x0
- margin - state.x, y0
,
2809 x1
- margin - state.x, y1
) oy;
2819 let panx = float state.x in
2821 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2822 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2824 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2826 if l
.pageno = pageno
2828 let d = float (l
.pagedispy
- l
.pagey) in
2829 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2830 GlDraw.begins `quads
;
2832 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2833 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2834 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2835 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2845 let showstrings trusted
active first pan
strings =
2847 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2848 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2849 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2850 GlDraw.color (1., 1., 1.);
2851 Gl.enable `texture_2d
;
2853 let wx = measurestr
15 "w" in
2854 let tabx = 30.0*.wx +. float (pan
*15) in
2856 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2859 let (s, level
, _
) = strings.(row
) in
2860 let y = (row
- first) * 16 in
2861 let x = 5 + 15*(max
0 (level
+pan
)) in
2864 Gl.disable `texture_2d
;
2865 GlDraw.polygon_mode `both `line
;
2866 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2867 GlDraw.rect (0., float (y + 1))
2868 (float (conf.winw
- 1), float (y + 18));
2869 GlDraw.polygon_mode `both `fill
;
2870 GlDraw.color (1., 1., 1.);
2871 Gl.enable `texture_2d
;
2874 let drawtabularstring x s =
2878 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2881 let len = String.length
s - tabpos - 1 in
2882 let s1 = String.sub
s 0 tabpos
2883 and s2
= String.sub
s (tabpos + 1) len in
2884 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2885 let x = truncate
(max
xx tabx) in
2886 drawstring1 15 x (y + 16) s2
2888 drawstring1 15 x (y + 16) s
2890 drawstring1 15 x (y + 16) s
2894 drawtabularstring (x + pan
*15) s;
2900 Gl.disable `texture_2d
;
2903 let showoutline (_, active, first, outlines, _, pan
, _) =
2904 showstrings false active first pan
outlines;
2907 let showitems (active, first, items, _, pan
, _) =
2908 showstrings true active first pan
items;
2912 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2913 GlDraw.viewport
margin 0 state.w conf.winh
;
2915 GlClear.color (scalecolor2 conf.bgcolor
);
2916 GlClear.clear
[`
color];
2919 Gl.enable `scissor_test
;
2920 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2922 List.iter
drawpage state.layout;
2925 Gl.disable `scissor_test
2929 let x = -.float state.x in
2930 GlMat.translate ~
x ();
2934 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2937 begin match state.mode with
2938 | Items
items -> showitems items
2939 | Outline
outline -> showoutline outline
2943 Glut.swapBuffers
();
2947 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2948 let x = x - margin - state.x in
2949 let rec f = function
2951 begin match getopaque l
.pageno with
2952 | Some
(opaque, _) when validopaque opaque ->
2953 let y = y - l
.pagedispy
in
2956 let y = l
.pagey + y in
2957 let x = x - l
.pagex
in
2958 match whatsunder
opaque x y with
2971 let viewmouse button bstate
x y =
2973 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2974 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2976 match state.mstate
with
2977 | Mzoom
(oldn
, i) ->
2985 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2987 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2989 let zoom = conf.zoom +. incr in
2991 state.mstate
<- Mzoom
(n, 0);
2993 state.mstate
<- Mzoom
(n, i+1);
2995 else state.mstate
<- Mzoom
(n, 0)
2997 | _ -> state.mstate
<- Mzoom
(n, 0)
3000 match state.autoscroll
with
3001 | Some step
-> setautoscrollspeed step
(n=4)
3005 then -conf.scrollstep
3006 else conf.scrollstep
3008 let incr = incr * 2 in
3009 let y = clamp incr in
3010 gotoy_and_clear_text y
3013 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3014 if bstate
= Glut.DOWN
3016 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3017 state.mstate
<- Mpan
(x, y)
3020 state.mstate
<- Mnone
3022 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3023 if bstate
= Glut.DOWN
3025 let position, sh = scrollph state.y in
3026 if y > truncate
position && y < truncate
(position +. sh)
3028 state.mstate
<- Mscroll
3030 let percent = float y /. float conf.winh
in
3031 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3033 state.mstate
<- Mscroll
3035 state.mstate
<- Mnone
3037 | Glut.LEFT_BUTTON
->
3038 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3039 begin match dest with
3040 | Ulinkgoto
(pageno, top) ->
3044 gotopage1 pageno top;
3050 | Unone
when bstate
= Glut.DOWN
->
3051 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3052 state.mstate
<- Mpan
(x, y);
3054 | Unone
| Utext
_ ->
3055 if bstate
= Glut.DOWN
3057 if conf.angle
mod 360 = 0
3059 state.mstate
<- Msel
((x, y), (x, y));
3060 Glut.postRedisplay
()
3064 match state.mstate
with
3067 | Mzoom
_ | Mscroll
->
3068 state.mstate
<- Mnone
3071 Glut.setCursor
Glut.CURSOR_INHERIT
;
3072 state.mstate
<- Mnone
3074 | Msel
((_, y0
), (_, y1
)) ->
3076 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3077 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3079 match getopaque l
.pageno with
3080 | Some
(opaque, _) when validopaque opaque ->
3084 List.iter
f state.layout;
3085 copysel
""; (* ugly *)
3086 Glut.setCursor
Glut.CURSOR_INHERIT
;
3087 state.mstate
<- Mnone
;
3094 let birdseyemouse button bstate
x y
3095 (conf, leftx
, _, hooverpageno
, anchor) =
3097 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3098 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3099 let rec loop = function
3102 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3103 && x > margin && x < margin + l
.pagew
3105 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3110 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3114 let mouse bstate button
x y =
3115 match state.mode with
3116 | View
-> viewmouse button bstate
x y
3117 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3118 | Textentry
_ | Outline
_ | Items
_ -> ()
3121 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3124 match state.mode with
3127 match state.mstate
with
3128 | Mzoom
_ | Mnone
-> ()
3133 state.mstate
<- Mpan
(x, y);
3134 if conf.zoom > 1.0 then state.x <- state.x + dx;
3136 gotoy_and_clear_text y
3139 state.mstate
<- Msel
(a, (x, y));
3140 Glut.postRedisplay
()
3143 let y = min
conf.winh
(max
0 y) in
3144 let percent = float y /. float conf.winh
in
3145 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3146 gotoy_and_clear_text y
3150 match state.mode with
3151 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3152 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3153 let rec loop = function
3155 if hooverpageno
!= -1
3157 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3158 Glut.postRedisplay
();
3161 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3162 && x > margin && x < margin + l
.pagew
3164 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3165 Glut.postRedisplay
();
3171 | Outline
_ | Items
_ | Textentry
_ -> ()
3173 match state.mstate
with
3175 begin match getunder x y with
3176 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3178 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3179 Glut.setCursor
Glut.CURSOR_INFO
3180 | Ulinkgoto
(page
, _) ->
3182 then showtext 'p'
("age: " ^ string_of_int page
);
3183 Glut.setCursor
Glut.CURSOR_INFO
3185 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3186 Glut.setCursor
Glut.CURSOR_TEXT
3189 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3197 let fontpath = ref "";;
3201 match Sys.os_type
with
3202 | "Win32" -> Sys.getenv
"HOMEPATH"
3203 | _ -> Sys.getenv
"HOME"
3206 ("Can not determine home directory location: " ^
3207 Printexc.to_string exn
);
3211 let config_of c attrs
=
3215 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3216 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3217 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3218 | "preload" -> { c with preload = bool_of_string
v }
3219 | "page-bias" -> { c with pagebias
= int_of_string
v }
3220 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3221 | "auto-scroll-step" ->
3222 { c with autoscrollstep
= max
0 (int_of_string
v) }
3223 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3224 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3225 | "throttle" -> { c with showall
= bool_of_string
v }
3226 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3227 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3228 | "vertical-margin" ->
3229 { c with interpagespace = max
0 (int_of_string
v) }
3231 let zoom = float_of_string
v /. 100. in
3232 let zoom = max
0.01 zoom in
3233 { c with zoom = zoom }
3234 | "presentation" -> { c with presentation = bool_of_string
v }
3235 | "rotation-angle" -> { c with angle
= int_of_string
v }
3236 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3237 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3238 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3239 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3240 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3241 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3242 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3243 | "block-width" -> { c with blockwidth
= max
2 (int_of_string
v) }
3244 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3245 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3246 | "background-color" -> { c with bgcolor
= color_of_string v }
3247 | "scrollbar-in-presentation" ->
3248 { c with scrollbarinpm
= bool_of_string
v }
3251 prerr_endline
("Error processing attribute (`" ^
3252 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3255 let rec fold c = function
3258 let c = apply c k
v in
3264 let fromstring f pos
n v d =
3267 dolog "Error processing attribute (%S=%S) at %d\n%s"
3268 n v pos
(Printexc.to_string exn
)
3273 let bookmark_of attrs
=
3274 let rec fold title page rely
= function
3275 | ("title", v) :: rest
-> fold v page rely rest
3276 | ("page", v) :: rest
-> fold title v rely rest
3277 | ("rely", v) :: rest
-> fold title page
v rest
3278 | _ :: rest
-> fold title page rely rest
3279 | [] -> title, page
, rely
3281 fold "invalid" "0" "0" attrs
3285 let rec fold path page rely pan
= function
3286 | ("path", v) :: rest
-> fold v page rely pan rest
3287 | ("page", v) :: rest
-> fold path
v rely pan rest
3288 | ("rely", v) :: rest
-> fold path page
v pan rest
3289 | ("pan", v) :: rest
-> fold path page rely
v rest
3290 | _ :: rest
-> fold path page rely pan rest
3291 | [] -> path
, page
, rely
, pan
3293 fold "" "0" "0" "0" attrs
3296 let setconf dst src
=
3297 dst
.scrollbw
<- src
.scrollbw
;
3298 dst
.scrollh
<- src
.scrollh
;
3299 dst
.icase
<- src
.icase
;
3300 dst
.preload <- src
.preload;
3301 dst
.pagebias
<- src
.pagebias
;
3302 dst
.verbose
<- src
.verbose
;
3303 dst
.scrollstep
<- src
.scrollstep
;
3304 dst
.maxhfit
<- src
.maxhfit
;
3305 dst
.crophack
<- src
.crophack
;
3306 dst
.autoscrollstep
<- src
.autoscrollstep
;
3307 dst
.showall
<- src
.showall
;
3308 dst
.hlinks
<- src
.hlinks
;
3309 dst
.underinfo
<- src
.underinfo
;
3310 dst
.interpagespace <- src
.interpagespace;
3311 dst
.zoom <- src
.zoom;
3312 dst
.presentation <- src
.presentation;
3313 dst
.angle
<- src
.angle
;
3314 dst
.winw
<- src
.winw
;
3315 dst
.winh
<- src
.winh
;
3316 dst
.savebmarks
<- src
.savebmarks
;
3317 dst
.memlimit
<- src
.memlimit
;
3318 dst
.proportional
<- src
.proportional
;
3319 dst
.texcount
<- src
.texcount
;
3320 dst
.sliceheight
<- src
.sliceheight
;
3321 dst
.blockwidth
<- src
.blockwidth
;
3322 dst
.thumbw
<- src
.thumbw
;
3323 dst
.jumpback
<- src
.jumpback
;
3324 dst
.bgcolor
<- src
.bgcolor
;
3325 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3329 let l = String.length
s in
3330 let b = Buffer.create
l in
3336 let h = Hashtbl.create
10 in
3337 let dc = { defconf with angle
= defconf.angle
} in
3338 let rec toplevel v t spos
_ =
3340 | Vdata
| Vcdata
| Vend
-> v
3341 | Vopen
("llppconfig", _, closed
) ->
3344 else { v with f = llppconfig
}
3346 error
"unexpected subelement at top level" s spos
3347 | Vclose
_ -> error
"unexpected close at top level" s spos
3349 and llppconfig
v t spos
_ =
3351 | Vdata
| Vcdata
-> v
3352 | Vend
-> error
"unexpected end of input in llppconfig" s spos
3353 | Vopen
("defaults", attrs
, closed
) ->
3354 let c = config_of dc attrs
in
3358 else { v with f = skip
"defaults" (fun () -> v) }
3360 | Vopen
("ui-font", _, closed
) ->
3363 else { v with f = uifont
(Buffer.create
10) }
3365 | Vopen
("doc", attrs
, closed
) ->
3366 let pathent, spage
, srely
, span
= doc_of attrs
in
3367 let path = unent pathent
3368 and pageno = fromstring int_of_string spos
"page" spage
0
3369 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3370 and pan
= fromstring int_of_string spos
"pan" span
0 in
3371 let c = config_of dc attrs
in
3372 let anchor = (pageno, rely
) in
3374 then (Hashtbl.add
h path (c, [], pan
, anchor); v)
3375 else { v with f = doc
path pan
anchor c [] }
3378 error
"unexpected subelement in llppconfig" s spos
3380 | Vclose
"llppconfig" -> { v with f = toplevel }
3381 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3383 and uifont
b v t spos epos
=
3386 Buffer.add_substring
b s spos
(epos
- spos
);
3388 | Vopen
(_, _, _) ->
3389 error
"unexpected subelement in ui-font" s spos
3390 | Vclose
"ui-font" ->
3391 if String.length
!fontpath = 0
3392 then fontpath := Buffer.contents
b;
3393 { v with f = llppconfig
}
3394 | Vclose
_ -> error
"unexpected close in ui-font" s spos
3395 | Vend
-> error
"unexpected end of input in ui-font" s spos
3397 and doc
path pan
anchor c bookmarks v t spos
_ =
3399 | Vdata
| Vcdata
-> v
3400 | Vend
-> error
"unexpected end of input in doc" s spos
3401 | Vopen
("bookmarks", _, closed
) ->
3404 else { v with f = pbookmarks
path pan
anchor c bookmarks }
3406 | Vopen
(_, _, _) ->
3407 error
"unexpected subelement in doc" s spos
3410 Hashtbl.add
h path (c, List.rev
bookmarks, pan
, anchor);
3411 { v with f = llppconfig
}
3413 | Vclose
_ -> error
"unexpected close in doc" s spos
3415 and pbookmarks
path pan
anchor c bookmarks v t spos
_ =
3417 | Vdata
| Vcdata
-> v
3418 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3419 | Vopen
("item", attrs
, closed
) ->
3420 let titleent, spage
, srely
= bookmark_of attrs
in
3421 let page = fromstring int_of_string spos
"page" spage
0
3422 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3423 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3425 then { v with f = pbookmarks
path pan
anchor c bookmarks }
3428 { v with f = skip
"item" f }
3431 error
"unexpected subelement in bookmarks" s spos
3433 | Vclose
"bookmarks" ->
3434 { v with f = doc
path pan
anchor c bookmarks }
3436 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3438 and skip tag
f v t spos
_ =
3440 | Vdata
| Vcdata
-> v
3442 error
("unexpected end of input in skipped " ^ tag
) s spos
3443 | Vopen
(tag'
, _, closed
) ->
3447 let f'
() = { v with f = skip tag
f } in
3448 { v with f = skip tag'
f'
}
3452 else error
("unexpected close in skipped " ^ tag
) s spos
3455 parse
{ f = toplevel; accu = () } s;
3461 let len = in_channel_length ic
in
3462 let s = String.create
len in
3463 really_input ic
s 0 len;
3466 | Parse_error
(msg
, s, pos
) ->
3467 let subs = subs s pos
in
3468 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
3469 failwith
("parse error: " ^
s)
3472 failwith
("config load error: " ^
Printexc.to_string exn
)
3478 let dir = Filename.concat
home ".config" in
3479 if Sys.is_directory
dir then dir else home
3482 Filename.concat
dir "llpp.conf"
3485 let confpath = ref defconfpath;;
3488 if Sys.file_exists
!confpath
3491 (try Some
(open_in_bin
!confpath)
3494 ("Error opening configuation file `" ^
!confpath ^
"': " ^
3495 Printexc.to_string exn
);
3504 ("Error loading configuation from `" ^
!confpath ^
"': " ^
3505 Printexc.to_string exn
);
3511 f (Hashtbl.create
0, defconf)
3516 let pc, pb
, px
, pa
=
3518 Hashtbl.find h (Filename.basename
state.path)
3519 with Not_found
-> dc, [], 0, (0, 0.0)
3523 state.bookmarks <- pb
;
3525 state.scrollw
<- conf.scrollbw
;
3527 then state.anchor <- pa
;
3528 cbput state.hists
.nav pa
;
3533 let add_attrs bb always
dc c =
3536 then Printf.bprintf bb
"\n %s='%b'" s a
3539 then Printf.bprintf bb
"\n %s='%d'" s a
3542 then Printf.bprintf bb
"\n %s='%d'" s (truncate
(a*.100.))
3546 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3550 then dc.winw
, dc.winh
3552 match state.fullscreen
with
3554 | None
-> c.winw
, c.winh
3556 let zoom, presentation, interpagespace, showall
=
3558 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3560 match state.mode with
3561 | Birdseye
(bc
, _, _, _, _) ->
3562 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3563 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3565 oi
"width" w dc.winw
;
3566 oi
"height" h dc.winh
;
3567 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3568 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3569 ob "case-insensitive-search" c.icase
dc.icase
;
3570 ob "preload" c.preload dc.preload;
3571 oi
"page-bias" c.pagebias
dc.pagebias
;
3572 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3573 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3574 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3575 ob "crop-hack" c.crophack
dc.crophack
;
3576 ob "throttle" showall
dc.showall
;
3577 ob "highlight-links" c.hlinks
dc.hlinks
;
3578 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3579 oi
"vertical-margin" interpagespace dc.interpagespace;
3580 oz
"zoom" zoom dc.zoom;
3581 ob "presentation" presentation dc.presentation;
3582 oi
"rotation-angle" c.angle
dc.angle
;
3583 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3584 ob "proportional-display" c.proportional
dc.proportional
;
3585 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3586 oi
"tex-count" c.texcount
dc.texcount
;
3587 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3588 oi
"block-width" c.blockwidth
dc.blockwidth
;
3589 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3590 ob "persistent-location" c.jumpback
dc.jumpback
;
3591 oc
"background-color" c.bgcolor
dc.bgcolor
;
3592 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3596 let bb = Buffer.create
32768 in
3598 let dc = if conf.bedefault
then conf else dc in
3599 Buffer.add_string
bb "<llppconfig>\n";
3601 if String.length
!fontpath > 0
3602 then Printf.bprintf
bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3604 Buffer.add_string
bb "<defaults ";
3605 add_attrs bb true dc dc;
3606 Buffer.add_string
bb "/>\n";
3608 let adddoc path pan
anchor c bookmarks =
3609 if bookmarks == [] && c = dc && anchor = emptyanchor
3612 Printf.bprintf
bb "<doc path='%s'"
3613 (enent
path 0 (String.length
path));
3615 if anchor <> emptyanchor
3617 let n, y = anchor in
3618 Printf.bprintf
bb " page='%d'" n;
3621 Printf.bprintf
bb " rely='%f'" y
3626 then Printf.bprintf
bb " pan='%d'" pan
;
3628 add_attrs bb false dc c;
3630 begin match bookmarks with
3631 | [] -> Buffer.add_string
bb "/>\n"
3633 Buffer.add_string
bb ">\n<bookmarks>\n";
3634 List.iter
(fun (title, _level
, (page, rely
)) ->
3636 "<item title='%s' page='%d'"
3637 (enent
title 0 (String.length
title))
3642 Printf.bprintf
bb " rely='%f'" rely
3644 Buffer.add_string
bb "/>\n";
3646 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3652 match state.mode with
3653 | Birdseye
(_, pan, _, _, _) -> pan
3656 let basename = Filename.basename state.path in
3657 adddoc basename pan (getanchor ())
3660 match state.autoscroll
with
3662 | None
-> conf.autoscrollstep
}
3663 (if conf.savebmarks
then state.bookmarks else []);
3665 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3667 then adddoc path x y c bookmarks
3669 Buffer.add_string
bb "</llppconfig>";
3672 if Buffer.length
bb > 0
3675 let tmp = !confpath ^
".tmp" in
3676 let oc = open_out_bin
tmp in
3677 Buffer.output_buffer
oc bb;
3679 Sys.rename
tmp !confpath;
3682 ("error while saving configuration: " ^
Printexc.to_string exn
)
3689 [("-p", Arg.String
(fun s -> state.password
<- s) ,
3690 "<password> Set password");
3692 ("-f", Arg.String
(fun s -> State.fontpath := s),
3693 "<path> Set path to the user interface font");
3695 ("-c", Arg.String
(fun s -> State.confpath := s),
3696 "<path> Set path to the configuration file");
3698 ("-v", Arg.Unit
(fun () ->
3700 "%s\nconfiguration path: %s\n"
3704 exit
0), " Print version and exit");
3707 (fun s -> state.path <- s)
3708 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3710 if String.length
state.path = 0
3711 then (prerr_endline
"file name missing"; exit
1);
3715 let _ = Glut.init
Sys.argv
in
3716 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3717 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3718 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3721 if Sys.os_type
= "Unix"
3723 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3725 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3726 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3727 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3728 Unix.bind
sock addr;
3730 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3731 Unix.connect
csock addr;
3732 let ssock, _ = Unix.accept
sock in
3735 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3736 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3743 let () = Glut.displayFunc
display in
3744 let () = Glut.reshapeFunc
reshape in
3745 let () = Glut.keyboardFunc
keyboard in
3746 let () = Glut.specialFunc
special in
3747 let () = Glut.idleFunc
(Some
idle) in
3748 let () = Glut.mouseFunc
mouse in
3749 let () = Glut.motionFunc
motion in
3750 let () = Glut.passiveMotionFunc
pmotion in
3753 conf.angle
, conf.proportional
, conf.texcount
,
3754 conf.sliceheight
, conf.blockwidth
, !State.fontpath
3756 state.csock <- csock;
3757 state.ssock <- ssock;
3758 state.text <- "Opening " ^
state.path;
3759 writeopen state.path state.password
;
3765 | Glut.BadEnum
"key in special_of_int" ->
3766 showtext '
!'
" LablGlut bug: special key not recognized";
3769 if Sys.os_type
<> "Unix"
3770 then Unix.shutdown
ssock Unix.SHUTDOWN_ALL
;