4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
10 type params
= angle
* proportional
* texcount
* sliceheight
19 and proportional
= bool
20 and interpagespace
= int
27 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
28 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext
: string -> (int * int * int * int) -> int -> unit =
31 external copysel
: string -> unit = "ml_copysel";;
32 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
33 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
34 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
38 | Msel
of (mpos
* mpos
)
41 | Mzoom
of (int * int)
45 type textentry
= string * string * onhist
option * onkey
* ondone
46 and onkey
= string -> int -> te
47 and ondone
= string -> unit
48 and histcancel
= unit -> unit
49 and onhist
= ((histcmd
-> string) * histcancel
)
50 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
55 | TEswitch
of textentry
67 { store
= Array.create n v
74 let cbcap b
= Array.length b
.store
;;
79 b
.wc
<- (b
.wc
+ 1) mod cap;
81 b
.len
<- min
(b
.len
+ 1) cap;
84 let cbempty b
= b
.len
= 0;;
86 let cbgetg b circular dir
=
90 let rc = b
.rc + dir
in
102 else max
0 (min
rc (b
.len
-1))
108 let cbget b
= cbgetg b
false;;
109 let cbgetc b
= cbgetg b
true;;
112 let rc = b
.wc
- b
.len
in
113 let rc = if rc < 0 then cbcap b
+ rc else rc in
117 let cbdecr b
= b
.len
<- b
.len
- 1;;
132 { mutable scrollw
: int
133 ; mutable scrollh
: int
134 ; mutable icase
: bool
135 ; mutable preload
: bool
136 ; mutable pagebias
: int
137 ; mutable verbose
: bool
138 ; mutable scrollstep
: int
139 ; mutable maxhfit
: bool
140 ; mutable crophack
: bool
141 ; mutable autoscrollstep
: int
142 ; mutable showall
: bool
143 ; mutable hlinks
: bool
144 ; mutable underinfo
: bool
145 ; mutable interpagespace
: interpagespace
146 ; mutable zoom
: float
147 ; mutable presentation
: bool
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
156 ; mutable thumbw
: width
157 ; mutable jumpback
: bool
158 ; mutable bgcolor
: float * float * float
159 ; mutable bedefault
: bool
163 type anchor
= pageno
* top
;;
165 type outline
= string * int * anchor
167 | Oarray
of outline array
168 | Olist
of outline list
169 | Onarrow
of string * outline array
* outline array
172 type rect
= float * float * float * float * float * float * float * float;;
174 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
176 let emptyanchor = (0, 0.0);;
177 let initialanchor = (-1, nan
);;
180 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
181 | Outline
of (bool * int * int * outline array
* string * int * mode
)
182 | Items
of (int * int * item array
* string * int * mode
)
183 | Textentry
of (textentry
* onleave
)
185 and onleave
= leavetextentrystatus
-> unit
186 and leavetextentrystatus
= | Cancel
| Confirm
187 and item
= string * int * action
190 | Action
of (int -> int -> string -> int -> mode
)
193 let isbirdseye = function Birdseye _
-> true | _
-> false;;
194 let istextentry = function Textentry _
-> true | _
-> false;;
197 { mutable csock
: Unix.file_descr
198 ; mutable ssock
: Unix.file_descr
202 ; mutable anchor
: anchor
204 ; mutable layout
: layout list
205 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
206 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
207 ; mutable pagecount
: int
208 ; pagecache
: string circbuf
209 ; mutable rendering
: bool
210 ; mutable mstate
: mstate
211 ; mutable searchpattern
: string
212 ; mutable rects
: (pageno
* recttype
* rect
) list
213 ; mutable rects1
: (pageno
* recttype
* rect
) list
214 ; mutable text
: string
215 ; mutable fullscreen
: (width
* height
) option
216 ; mutable mode
: mode
217 ; mutable outlines
: outlines
218 ; mutable bookmarks
: outline list
219 ; mutable path
: string
220 ; mutable password
: string
221 ; mutable invalidated
: int
222 ; mutable colorscale
: float
223 ; mutable memused
: int
225 ; mutable throttle
: layout list
option
226 ; mutable ascrollstep
: int
227 ; mutable help
: item array
228 ; mutable docinfo
: (int * string) list
232 { pat
: string circbuf
233 ; pag
: string circbuf
234 ; nav
: anchor circbuf
248 ; autoscrollstep
= 24
254 ; presentation
= false
259 ; proportional
= true
260 ; memlimit
= 32*1024*1024
265 ; bgcolor
= (0.5, 0.5, 0.5)
270 let conf = { defconf with angle
= defconf.angle
};;
273 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
274 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
282 ; anchor
= initialanchor
286 ; pagemap
= Hashtbl.create
10
287 ; pagecache
= cbnew 100 ""
298 ; outlines
= Olist
[]
304 { nav
= cbnew 100 (0, 0.0)
321 Printf.kprintf prerr_endline fmt
323 Printf.kprintf ignore fmt
327 let len = String.length s
in
329 let b = Buffer.create
n in
330 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
331 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
332 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
333 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
334 Buffer.add_string
b s
;
335 let s'
= Buffer.contents
b in
336 let n'
= Unix.write fd
s'
0 n in
337 if n'
!= n then failwith
"write failed";
342 let n = Unix.read fd
s 0 4 in
343 if n != 4 then failwith
"incomplete read(len)";
345 lor (Char.code
s.[0] lsl 24)
346 lor (Char.code
s.[1] lsl 16)
347 lor (Char.code
s.[2] lsl 8)
348 lor (Char.code
s.[3] lsl 0)
350 let s = String.create
len in
351 let n = Unix.read fd
s 0 len in
352 if n != len then failwith
"incomplete read(data)";
357 let b = Buffer.create
10 in
358 Buffer.add_string
b s;
359 let rec combine = function
362 Buffer.add_char
b ' '
;
365 | `
b b -> if b then "1" else "0"
367 | `i i
-> string_of_int i
368 | `f f
-> string_of_float f
369 | `I f
-> string_of_int
(truncate f
)
371 Buffer.add_string
b s;
378 let cmd = Buffer.contents
(makecmd s l
) in
379 writecmd state.csock
cmd;
385 let d = conf.winh
- h
in
392 let rec f pn ph pi fh l
=
394 | (n, _
, h
, _
) :: rest
->
395 let ips = calcips h
in
400 if isbirdseye state.mode
&& pn
= 0
405 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
410 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
414 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
417 let fh = f 0 0 0 0 state.pdims
in
421 let getpageyh pageno
=
422 let rec f pn ph pi y l
=
424 | (n, _
, h
, _
) :: rest
->
425 let ips = calcips h
in
428 let h = if n = pageno
then h else ph
in
429 if conf.presentation
&& n = pageno
431 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
433 y
+ (pageno
- pn
) * (ph
+ pi
), h
435 let y = y + (if conf.presentation
then pi
else 0) in
436 let y = y + (n - pn
) * (ph
+ pi
) in
440 y + (pageno
- pn
) * (ph
+ pi
), ph
442 f 0 0 0 0 state.pdims
445 let getpagey pageno
= fst
(getpageyh pageno
);;
448 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
449 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
451 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
452 let ips = calcips h in
454 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
458 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
460 prev
, pdims
, pdimno
, 0
462 let dy = dy + yinc in
463 let py = py + yinc in
464 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
469 if py + h <= vy - yinc
471 let py = py + h + ips in
472 let dy = max
0 (py - y) in
482 let pagey = vy - py in
483 let pagevh = h - pagey in
484 let pagevh = min
(sh
- dy) pagevh in
485 let off = if yinc > 0 then py - vy else 0 in
486 let py = py + h + ips in
492 ; pagedispy
= dy + off
493 ; pagey = pagey + off
494 ; pagevh = pagevh - off
498 let accu = e :: accu in
505 ~cacheleft
:(cacheleft
-1)
508 if state.invalidated
= 0
518 ~cacheleft
:(cbcap state.pagecache
)
528 let y = state.y + incr
in
530 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
534 let getopaque pageno
=
535 try Some
(Hashtbl.find
state.pagemap
536 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
537 with Not_found
-> None
540 let cache pageno opaque
=
541 Hashtbl.replace
state.pagemap
542 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
545 let validopaque opaque
= String.length opaque
> 0;;
548 match getopaque l
.pageno
with
549 | None
when not
state.rendering
->
550 state.rendering
<- true;
551 cache l
.pageno
("", -1);
552 wcmd "render" [`i
(l
.pageno
+ 1)
559 let loadlayout layout =
560 let rec f all
= function
562 begin match getopaque l
.pageno
with
563 | None
-> render l
; f false ls
564 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
568 f (layout <> []) layout;
571 let findpageforopaque opaque
=
573 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
577 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
583 let memleft = conf.memlimit
- state.memused
in
586 let opaque = cbpeek state.pagecache
in
587 match findpageforopaque opaque with
588 | Some
((n, _
, _
, _
, _
), size
) ->
589 memleft + size
>= 0 && not
(pagevisible state.layout n)
596 let presentation = conf.presentation in
597 let interpagespace = conf.interpagespace in
598 let maxy = state.maxy in
599 conf.presentation <- false;
600 conf.interpagespace <- 0;
601 state.maxy <- calcheight ();
603 match state.layout with
605 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
607 let y = if y < conf.winh
then 0 else y - conf.winh
in
608 let pages = layout y (conf.winh
*3) in
609 List.iter
render pages;
610 conf.presentation <- presentation;
611 conf.interpagespace <- interpagespace;
617 let y = min
state.maxy y in
618 let pages = layout y conf.winh
in
619 let ready = loadlayout pages in
625 state.layout <- pages;
626 state.throttle
<- None
;
627 Glut.postRedisplay
();
630 state.throttle
<- Some
pages;
635 state.layout <- pages;
636 state.throttle
<- None
;
637 Glut.postRedisplay
();
639 begin match state.mode
with
640 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
641 if not
(pagevisible pages pageno
)
643 match state.layout with
646 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
653 let gotoy_and_clear_text y =
655 if not
conf.verbose
then state.text
<- "";
659 match state.layout with
661 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
664 let getanchory (n, top
) =
665 let y, h = getpageyh n in
666 y + (truncate
(top
*. float h));
669 let gotoanchor anchor
=
670 gotoy (getanchory anchor
);
674 cbput state.hists
.nav
(getanchor ());
678 let anchor = cbgetc state.hists
.nav dir
in
683 let y, h = getpageyh n in
684 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
687 let gotopage1 n top
=
688 let y = getpagey n in
689 gotoy_and_clear_text (y + top
);
697 state.invalidated
<- state.invalidated
+ 1;
701 let c = c *. state.colorscale
in
705 let scalecolor2 (r
, g
, b) =
706 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
710 state.maxy <- calcheight ();
711 match state.mode
with
712 | Birdseye
(_
, _
, pageno
, _
, _
) ->
713 let y, h = getpageyh pageno
in
714 let top = (conf.winh
- h) / 2 in
715 gotoy (max
0 (y - top))
716 | _
-> gotoanchor state.anchor
720 GlMat.mode `projection
;
721 GlMat.load_identity
();
722 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
723 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
724 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
727 GlMat.translate ~x
:(float state.x
) ();
732 GlMat.mode `projection
;
733 GlMat.load_identity
();
734 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
735 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
736 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
740 if state.invalidated
= 0 && state.anchor == initialanchor
741 then state.anchor <- getanchor ();
744 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
748 GlMat.mode `modelview
;
749 GlMat.load_identity
();
750 GlClear.color
(scalecolor 1.0);
751 GlClear.clear
[`color
];
754 wcmd "geometry" [`i
w; `i
h];
758 GlDraw.color
(0.0, 0.0, 0.0);
760 (0.0, float (conf.winh
- 18))
761 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
763 let font = Glut.BITMAP_8_BY_13
in
764 GlDraw.color
(1.0, 1.0, 1.0);
765 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
766 Glut.bitmapCharacter ~
font ~
c:(Char.code ' '
);
767 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
771 let len = String.length
state.text
in
772 match state.mode
with
773 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
775 match String.length prefix
with
779 Printf.sprintf
"%s%s^ [%s]" prefix text
state.text
781 Printf.sprintf
"%s%s^" prefix text
786 Printf.sprintf
"%s: %s^ [%s]" prefix text
state.text
788 Printf.sprintf
"%s: %s^" prefix text
793 if len > 0 then showtext state.text
797 state.text
<- Printf.sprintf
"%c%s" c s;
798 Glut.postRedisplay
();
807 state.rects
<- state.rects1
;
808 Glut.postRedisplay
()
811 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
812 state.pagecount
<- n;
813 state.invalidated
<- state.invalidated
- 1;
814 if state.invalidated
= 0
818 let s = Scanf.sscanf
cmd "t %n"
819 (fun n -> String.sub
cmd n (String.length
cmd - n))
821 Glut.setWindowTitle
s
824 let s = Scanf.sscanf
cmd "T %n"
825 (fun n -> String.sub
cmd n (String.length
cmd - n))
827 if istextentry state.mode
834 Glut.postRedisplay
();
840 let s = Scanf.sscanf
cmd "V %n"
841 (fun n -> String.sub
cmd n (String.length
cmd - n))
847 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
848 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
849 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
850 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
852 let y = (getpagey pageno) + truncate y0
in
855 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
858 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
859 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
860 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
861 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
864 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
867 let n, w, _h
, r
, l
, s, p
=
868 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
869 (fun n w h r l
s p
->
870 (n-1, w, h, r
, l
!= 0, s, p
))
873 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
874 state.memused
<- state.memused
+ s;
877 match state.throttle
with
878 | None
-> state.layout
879 | Some
layout -> layout
883 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
886 let evictedopaque = cbpeek state.pagecache
in
887 match findpageforopaque evictedopaque with
888 | None
-> failwith
"bug in gc"
889 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
890 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
892 wcmd "free" [`
s evictedopaque];
893 state.memused
<- state.memused
- evictedsize
;
894 Hashtbl.remove
state.pagemap k
;
895 cbdecr state.pagecache
;
902 cbput state.pagecache p
;
903 state.rendering
<- false;
905 begin match state.throttle
with
907 if pagevisible state.layout n
910 let allvisible = loadlayout state.layout in
911 if allvisible then preload ();
918 let y = getpagey l
.pageno + l
.pagey in
924 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
926 state.pdims
<- pdim :: state.pdims
929 let (l
, n, t
, h, pos
) =
930 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
932 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
934 let l = String.length
s in
935 let b = Buffer.create
(String.length
s) in
942 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
945 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
954 let outline = (s, l, (n, float t
/. float h)) in
956 match state.outlines with
957 | Olist
outlines -> Olist
(outline :: outlines)
958 | Oarray _
-> Olist
[outline]
959 | Onarrow _
-> Olist
[outline]
961 state.outlines <- outlines
965 let s = Scanf.sscanf
cmd "i %n"
966 (fun n -> String.sub
cmd n (String.length
cmd - n))
968 let len = String.length
s in
969 let rec fold accu pos
=
971 try String.index_from
s pos '
\n'
with Not_found
-> len
976 let line = String.sub
s pos
(eolpos - pos
) in
977 fold ((1, line)::accu) (eolpos+1)
979 state.docinfo
<- fold state.docinfo
0
982 dolog "unknown cmd `%S'" cmd
985 let now = Unix.gettimeofday
;;
989 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
992 if state.ascrollstep
> 0
994 let y = state.y + state.ascrollstep
in
995 let y = if y >= state.maxy then 0 else y in
1001 let cmd = readcmd state.csock
in
1010 let action = function
1011 | HCprev
-> cbget cb ~
-1
1012 | HCnext
-> cbget cb
1
1013 | HCfirst
-> cbget cb ~
-(cb
.rc)
1014 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1015 and cancel
() = cb
.rc <- rc
1019 let search pattern forward
=
1020 if String.length pattern
> 0
1023 match state.layout with
1026 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
1029 let b = makecmd "search"
1030 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1032 Buffer.add_char
b '
,'
;
1033 Buffer.add_string
b pattern
;
1034 Buffer.add_char
b '
\000'
;
1037 writecmd state.csock
cmd;
1040 let intentry text key
=
1041 let c = Char.unsafe_chr key
in
1044 let s = "x" in s.[0] <- c;
1045 let text = text ^
s in
1049 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1054 let b = Buffer.create
(String.length
s + 1) in
1055 Buffer.add_string
b s;
1056 Buffer.add_char
b c;
1060 let textentry text key
=
1061 let c = Char.unsafe_chr key
in
1063 | _
when key
>= 32 && key
< 127 ->
1064 let text = addchar text c in
1068 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1072 let reinit angle proportional
=
1073 conf.angle
<- angle
;
1074 conf.proportional
<- proportional
;
1076 wcmd "reinit" [`i angle
; `
b proportional
];
1080 let zoom = max
0.01 (min
2.2 zoom) in
1081 if zoom <> conf.zoom
1086 state.anchor <- getanchor ();
1087 reshape conf.winw
conf.winh
;
1088 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1092 let enterbirdseye () =
1093 let zoom = float conf.thumbw
/. float conf.winw
in
1094 let birdseyepageno =
1095 let rec fold candidate
= function
1097 | l :: _
when l.pagey = 0 -> l.pageno
1098 | l :: rest
-> fold l.pageno rest
1102 state.mode
<- Birdseye
(
1103 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1106 conf.presentation <- false;
1107 conf.interpagespace <- 10;
1108 conf.hlinks
<- false;
1110 state.mstate
<- Mnone
;
1111 conf.showall
<- false;
1112 Glut.setCursor
Glut.CURSOR_INHERIT
;
1115 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1120 reshape conf.winw
conf.winh
;
1123 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1125 conf.zoom <- c.zoom;
1126 conf.presentation <- c.presentation;
1127 conf.interpagespace <- c.interpagespace;
1128 conf.showall
<- c.showall
;
1129 conf.hlinks
<- c.hlinks
;
1133 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1136 reshape conf.winw
conf.winh
;
1137 state.anchor <- if goback
then anchor else (pageno, 0.0);
1140 let togglebirdseye () =
1141 match state.mode
with
1142 | Birdseye vals
-> leavebirdseye vals
true
1143 | View
| Outline _
-> enterbirdseye ()
1147 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1148 let pageno = max
0 (pageno - 1) in
1149 let rec loop = function
1150 | [] -> gotopage1 pageno 0
1151 | l :: _
when l.pageno = pageno ->
1152 if l.pagedispy
>= 0 && l.pagey = 0
1153 then Glut.postRedisplay
()
1154 else gotopage1 pageno 0
1155 | _
:: rest
-> loop rest
1158 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1161 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1162 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1163 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1164 let rec loop = function
1166 let y, h = getpageyh pageno in
1167 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1169 | l :: _
when l.pageno = pageno ->
1170 if l.pagevh != l.pageh
1171 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1172 else Glut.postRedisplay
()
1173 | _
:: rest
-> loop rest
1178 let optentry mode _ key
=
1179 let btos b = if b then "on" else "off" in
1180 let c = Char.unsafe_chr key
in
1184 try conf.scrollstep
<- int_of_string
s with exc
->
1185 state.text <- Printf.sprintf
"bad integer `%s': %s"
1186 s (Printexc.to_string exc
)
1188 TEswitch
("scroll step", "", None
, intentry, ondone)
1193 conf.autoscrollstep
<- int_of_string
s;
1194 if state.ascrollstep
> 0
1195 then state.ascrollstep
<- conf.autoscrollstep
;
1197 state.text <- Printf.sprintf
"bad integer `%s': %s"
1198 s (Printexc.to_string exc
)
1200 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1205 let zoom = float (int_of_string
s) /. 100.0 in
1208 state.text <- Printf.sprintf
"bad integer `%s': %s"
1209 s (Printexc.to_string exc
)
1211 TEswitch
("zoom", "", None
, intentry, ondone)
1216 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1218 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1219 begin match mode
with
1221 leavebirdseye beye
false;
1226 state.text <- Printf.sprintf
"bad integer `%s': %s"
1227 s (Printexc.to_string exc
)
1229 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1234 Some
(int_of_string
s)
1236 state.text <- Printf.sprintf
"bad integer `%s': %s"
1237 s (Printexc.to_string exc
);
1240 | Some angle
-> reinit angle
conf.proportional
1243 TEswitch
("rotation", "", None
, intentry, ondone)
1246 conf.icase
<- not
conf.icase
;
1247 TEdone
("case insensitive search " ^
(btos conf.icase
))
1250 conf.preload <- not
conf.preload;
1252 TEdone
("preload " ^
(btos conf.preload))
1255 conf.verbose
<- not
conf.verbose
;
1256 TEdone
("verbose " ^
(btos conf.verbose
))
1259 conf.maxhfit
<- not
conf.maxhfit
;
1260 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1261 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1264 conf.crophack
<- not
conf.crophack
;
1265 TEdone
("crophack " ^
btos conf.crophack
)
1268 conf.showall
<- not
conf.showall
;
1269 TEdone
("throttle " ^
btos conf.showall
)
1272 conf.underinfo
<- not
conf.underinfo
;
1273 TEdone
("underinfo " ^
btos conf.underinfo
)
1276 conf.savebmarks
<- not
conf.savebmarks
;
1277 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1283 match state.layout with
1288 conf.interpagespace <- int_of_string
s;
1289 state.maxy <- calcheight ();
1290 let y = getpagey pageno in
1293 state.text <- Printf.sprintf
"bad integer `%s': %s"
1294 s (Printexc.to_string exc
)
1296 TEswitch
("vertical margin", "", None
, intentry, ondone)
1299 reinit conf.angle
(not
conf.proportional
);
1300 TEdone
("proprortional display " ^
btos conf.proportional
)
1303 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1307 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1309 let enterselector allowdel
outlines errmsg msg
=
1310 if Array.length
outlines = 0
1312 showtext ' ' errmsg
;
1316 Glut.setCursor
Glut.CURSOR_INHERIT
;
1318 match state.layout with
1320 | {pageno=pageno} :: _
-> pageno
1324 if n = Array.length
outlines
1327 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1328 if outlinepageno
>= pageno then n else loop (n+1)
1332 state.mode
<- Outline
1333 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1335 Glut.postRedisplay
();
1339 let enteroutlinemode () =
1341 match state.outlines with
1344 let a = Array.of_list
(List.rev
l) in
1345 state.outlines <- Oarray
a;
1347 | Onarrow
(pat
, a, _
) ->
1348 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1350 enterselector false outlines "Document has no outline" msg
;
1353 let enterbookmarkmode () =
1354 let bookmarks = Array.of_list
state.bookmarks in
1355 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1358 let mode_to_string mode
=
1359 let b = Buffer.create
10 in
1360 let rec f = function
1361 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1362 | View
-> Buffer.add_string
b "View"
1363 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1364 | Items _
-> Buffer.add_string
b "Items"
1365 | Outline _
-> Buffer.add_string
b "Outline"
1371 let enterinfomode () =
1372 let btos = function true -> "on" | _
-> "off" in
1373 let mode = state.mode in
1374 let rec makeitems () =
1375 let intp name get set
=
1376 Printf.sprintf
"%-24s %d" name
(get
()), 1, Action
(
1377 fun active first _qsearch pan
->
1382 state.text <- Printf.sprintf
"bad integer `%s': %s"
1383 s (Printexc.to_string exn
);
1386 if n != max_int
then set
n;
1388 let te = name
, "", None
, intentry, ondone in
1393 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1396 and boolp name get set
=
1397 Printf.sprintf
"%-24s %s" name
(btos (get
())), 1, Action
(
1398 fun active first qsearch pan
->
1401 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1405 let birdseye = isbirdseye mode in
1407 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1409 boolp
"presentation"
1410 (fun () -> conf.presentation)
1412 conf.presentation <- v;
1413 state.anchor <- getanchor ();
1416 boolp
"ignore case in searches"
1417 (fun () -> conf.icase
)
1418 (fun v -> conf.icase
<- v);
1421 (fun () -> conf.preload)
1422 (fun v -> conf.preload <- v);
1425 (fun () -> conf.verbose
)
1426 (fun v -> conf.verbose
<- v);
1429 (fun () -> conf.maxhfit
)
1430 (fun v -> conf.maxhfit
<- v);
1433 (fun () -> conf.crophack
)
1434 (fun v -> conf.crophack
<- v);
1437 (fun () -> conf.showall
)
1438 (fun v -> conf.showall
<- v);
1440 boolp
"highlight links"
1441 (fun () -> conf.hlinks
)
1442 (fun v -> conf.hlinks
<- v);
1445 (fun () -> conf.underinfo
)
1446 (fun v -> conf.underinfo
<- v);
1447 boolp
"persistent bookmarks"
1448 (fun () -> conf.savebmarks
)
1449 (fun v -> conf.savebmarks
<- v);
1451 boolp
"proportional display"
1452 (fun () -> conf.proportional
)
1453 (fun v -> reinit conf.angle
v);
1455 boolp
"persistent location"
1456 (fun () -> conf.jumpback
)
1457 (fun v -> conf.jumpback
<- v);
1461 intp "vertical margin"
1462 (fun () -> conf.interpagespace)
1464 conf.interpagespace <- n;
1466 match state.layout with
1471 state.maxy <- calcheight ();
1472 let y = getpagey pageno in
1477 (fun () -> conf.pagebias
)
1478 (fun v -> conf.pagebias
<- v);
1481 (fun () -> conf.scrollstep
)
1482 (fun n -> conf.scrollstep
<- n);
1484 intp "auto scroll step"
1486 if state.ascrollstep
> 0
1487 then state.ascrollstep
1488 else conf.autoscrollstep
)
1490 if state.ascrollstep
> 0
1491 then state.ascrollstep
<- n
1492 else conf.autoscrollstep
<- n);
1495 (fun () -> truncate
(conf.zoom *. 100.))
1496 (fun v -> setzoom ((float v) /. 100.));
1499 (fun () -> conf.angle
)
1500 (fun v -> reinit v conf.proportional
);
1502 intp "scroll bar width"
1503 (fun () -> conf.scrollw
)
1506 reshape conf.winw
conf.winh
;
1509 intp "scroll handle height"
1510 (fun () -> conf.scrollh
)
1511 (fun v -> conf.scrollh
<- v;);
1513 intp "thumbnail width"
1514 (fun () -> conf.thumbw
)
1516 conf.thumbw
<- min
1920 v;
1519 leavebirdseye beye
false;
1525 "Pixmap Cache", 0, Noaction
;
1527 intp "size (advisory)"
1528 (fun () -> conf.memlimit
)
1529 (fun v -> conf.memlimit
<- v);
1530 Printf.sprintf
"%-24s %d" "used" state.memused
, 1, Noaction
;
1537 :: ("Document", 0, Noaction
)
1538 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1544 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1545 (btos conf.bedefault
),
1548 fun active first qsearch pan
->
1549 conf.bedefault
<- not
conf.bedefault
;
1550 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1555 Array.of_list
(items @ tailer)
1558 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1559 Glut.postRedisplay
();
1562 let enterhelpmode () =
1563 state.mode <- Items
(0, 0, state.help
, "", 0, state.mode);
1564 Glut.postRedisplay
();
1567 let quickbookmark ?title
() =
1568 match state.layout with
1574 let sec = Unix.gettimeofday
() in
1575 let tm = Unix.localtime
sec in
1576 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1580 (tm.Unix.tm_year
+ 1900)
1583 | Some
title -> title
1586 (title, 0, (l.pageno, float l.pagey /. float l.pageh
))
1591 state.fullscreen
<- None
;
1592 Glut.reshapeWindow
w h;
1595 let writeopen path password
=
1596 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1597 writecmd state.csock
"info";
1600 let opendoc path password
=
1603 state.password
<- password
;
1604 state.gen
<- state.gen
+ 1;
1606 writeopen path password
;
1607 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1608 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1611 let viewkeyboard ~key ~x ~
y =
1615 let mode = state.mode in
1616 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1619 Glut.postRedisplay
()
1621 let c = Char.chr key
in
1623 | '
\027'
| 'q'
-> (* escape *)
1626 | '
\008'
-> (* backspace *)
1627 let y = getnav ~
-1 in
1628 gotoy_and_clear_text y
1636 Glut.postRedisplay
()
1639 let ondone isforw
s =
1640 cbput state.hists
.pat
s;
1641 state.searchpattern
<- s;
1644 let s = String.create
1 in
1646 enttext (s, "", Some
(onhist state.hists
.pat
),
1647 textentry, ondone (c ='
/'
))
1649 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1650 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1651 setzoom (min
2.2 (conf.zoom +. incr))
1656 try int_of_string
s with exc
->
1657 state.text <- Printf.sprintf
"bad integer `%s': %s"
1658 s (Printexc.to_string exc
);
1664 state.text <- "page bias is now " ^ string_of_int
n;
1667 enttext ("page bias", "", None
, intentry, ondone)
1669 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1670 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1671 setzoom (max
0.01 (conf.zoom -. decr))
1674 let ondone msg
= state.text <- msg
in
1676 "option [acfhilpstvAPRSZ]", "", None
,
1677 optentry state.mode, ondone
1680 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1683 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1684 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1688 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1694 try int_of_string
s with exc
->
1695 state.text <- Printf.sprintf
"bad integer `%s': %s"
1696 s (Printexc.to_string exc
);
1702 cbput state.hists
.pag
(string_of_int
n);
1703 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1706 let pageentry text key
=
1707 match Char.unsafe_chr key
with
1708 | 'g'
-> TEdone
text
1709 | _
-> intentry text key
1711 let text = "x" in text.[0] <- c;
1712 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1715 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1716 reshape conf.winw
conf.winh
;
1719 conf.hlinks
<- not
conf.hlinks
;
1720 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1721 Glut.postRedisplay
()
1724 if state.ascrollstep
= 0
1725 then state.ascrollstep
<- conf.autoscrollstep
1727 conf.autoscrollstep
<- state.ascrollstep
;
1728 state.ascrollstep
<- 0;
1732 conf.presentation <- not
conf.presentation;
1733 showtext ' '
("presentation mode " ^
1734 if conf.presentation then "on" else "off");
1735 state.anchor <- getanchor ();
1739 begin match state.fullscreen
with
1741 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1744 state.fullscreen
<- None
;
1749 gotoy_and_clear_text 0
1752 search state.searchpattern
true
1755 search state.searchpattern
false
1758 begin match state.layout with
1761 gotoy_and_clear_text (getpagey l.pageno)
1765 begin match List.rev
state.layout with
1768 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1769 gotoy_and_clear_text (getpagey pageno)
1772 | '
\127'
-> (* delte *)
1773 begin match state.layout with
1776 let pageno = max
0 (l.pageno-1) in
1777 gotoy_and_clear_text (getpagey pageno)
1782 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1784 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1786 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1790 else (100. *. (float state.y /. float maxy)) in
1793 Printf.sprintf
"Page %d of %d %.2f%%"
1794 (fn+1) state.pagecount
percent
1797 "Pages %d-%d of %d %.2f%%"
1798 (fn+1) (ln
+1) state.pagecount
percent
1803 begin match state.layout with
1806 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1807 Glut.postRedisplay
();
1811 enterbookmarkmode ()
1821 match state.layout with
1824 (s, 0, (l.pageno, float l.pagey /. float l.pageh
))
1828 enttext ("bookmark", "", None
, textentry, ondone)
1832 showtext ' '
"Quick bookmark added";
1835 begin match state.layout with
1837 let rect = getpdimrect
l.pagedimno
in
1841 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1842 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1844 (truncate
(rect.(1) -. rect.(0)),
1845 truncate
(rect.(3) -. rect.(0)))
1849 doreshape (w + conf.scrollw
) (h + conf.interpagespace)
1851 Glut.postRedisplay
();
1857 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1862 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1863 Glut.postRedisplay
()
1866 begin match state.mode with
1867 | Birdseye beye
-> upbirdseye beye
1868 | _
-> gotoy (clamp (-conf.scrollstep
))
1872 begin match state.mode with
1873 | Birdseye beye
-> downbirdseye beye
1874 | _
-> gotoy (clamp conf.scrollstep
)
1877 | '
r'
-> opendoc state.path
state.password
1880 vlog "huh? %d %c" key
(Char.chr key
);
1883 let textentrykeyboard ~key ~x ~
y ((c, text, opthist
, onkey
, ondone), onleave
) =
1887 state.mode <- Textentry
(te, onleave
);
1890 Glut.postRedisplay
()
1892 match Char.unsafe_chr key
with
1893 | '
\008'
-> (* backspace *)
1894 let len = String.length
text in
1898 Glut.postRedisplay
();
1901 let s = String.sub
text 0 (len - 1) in
1902 enttext (c, s, opthist
, onkey
, ondone)
1908 Glut.postRedisplay
()
1910 | '
\027'
-> (* escape *)
1911 begin match opthist
with
1913 | Some
(_
, onhistcancel
) -> onhistcancel
()
1916 Glut.postRedisplay
()
1919 begin match onkey
text key
with
1923 Glut.postRedisplay
()
1926 enttext (c, text, opthist
, onkey
, ondone);
1930 Glut.postRedisplay
()
1933 state.mode <- Textentry
(te, onleave
);
1934 Glut.postRedisplay
()
1938 let birdseyekeyboard ~key ~x ~
y ((_
, _
, pageno, _
, _
) as beye
) =
1940 | 27 -> (* escape *)
1941 leavebirdseye beye
true
1943 | 12 -> (* ctrl-l *)
1944 let y, h = getpageyh pageno in
1945 let top = (conf.winh
- h) / 2 in
1946 gotoy (max
0 (y - top))
1949 leavebirdseye beye
false
1952 viewkeyboard ~key ~x ~
y
1955 let itemskeyboard ~key ~x ~
y (active, first
, items, qsearch
, pan
, oldmode
) =
1958 let set active first qsearch
=
1959 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
1961 let search active pattern
incr =
1964 if n = Array.length
items || n = -1
1967 let (s, _
, _
) = items.(n) in
1969 (try ignore
(Str.search_forward re
s 0); true
1970 with Not_found
-> false)
1972 else loop (n + incr)
1977 let re = Str.regexp_case_fold pattern
in
1983 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1985 | 18 | 19 -> (* ctrl-r/ctlr-s *)
1986 let incr = if key
= 18 then -1 else 1 in
1988 match search (active + incr) qsearch
incr with
1990 state.text <- qsearch ^
" [not found]";
1993 state.text <- qsearch
;
1994 active, firstof active
1996 set active first qsearch
;
1997 Glut.postRedisplay
();
1999 | 8 -> (* backspace *)
2000 let len = String.length qsearch
in
2007 set active first
"";
2010 let qsearch = String.sub
qsearch 0 (len - 1) in
2012 match search active qsearch ~
-1 with
2014 state.text <- qsearch ^
" [not found]";
2017 state.text <- qsearch;
2018 active, firstof active
2020 set active first
qsearch
2022 Glut.postRedisplay
()
2024 | _
when key
>= 32 && key
< 127 ->
2025 let pattern = addchar qsearch (Char.chr key
) in
2027 match search active pattern 1 with
2029 state.text <- pattern ^
" [not found]";
2032 state.text <- pattern;
2033 active, firstof active
2035 set active first
pattern;
2036 Glut.postRedisplay
()
2038 | 27 -> (* escape *)
2040 state.mode <- oldmode
;
2041 Glut.postRedisplay
();
2044 if active < Array.length
items
2046 match items.(active) with
2048 state.mode <- f active first
qsearch pan
2052 state.mode <- oldmode
2054 Glut.postRedisplay
();
2056 | _
-> dolog "unknown key %d" key
2059 let outlinekeyboard ~key ~x ~
y
2060 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2063 let narrow outlines pattern =
2064 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2068 let rec fold accu n =
2072 let (s, _
, _
) as o
= outlines.(n) in
2074 if (try ignore
(Str.search_forward
re s 0); true
2075 with Not_found
-> false)
2081 let matched = fold [] (Array.length
outlines - 1) in
2082 if matched = [] then None
else Some
(Array.of_list
matched)
2084 let search active pattern incr =
2087 if n = Array.length
outlines || n = -1
2090 let (s, _
, _
) = outlines.(n) in
2092 (try ignore
(Str.search_forward
re s 0); true
2093 with Not_found
-> false)
2095 else loop (n + incr)
2100 let re = Str.regexp_case_fold
pattern in
2106 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2108 | 27 -> (* escape *)
2109 if String.length
qsearch = 0
2112 state.mode <- oldmode
;
2113 Glut.postRedisplay
();
2117 state.mode <- Outline
(
2118 allowdel
, active, first
, outlines, "", pan
, oldmode
2120 Glut.postRedisplay
();
2123 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2124 let incr = if key
= 18 then -1 else 1 in
2126 match search (active + incr) qsearch incr with
2128 state.text <- qsearch ^
" [not found]";
2131 state.text <- qsearch;
2132 active, firstof active
2134 state.mode <- Outline
(
2135 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2137 Glut.postRedisplay
();
2139 | 8 -> (* backspace *)
2140 let len = String.length
qsearch in
2147 state.mode <- Outline
(
2148 allowdel
, active, first
, outlines, "", pan
, oldmode
2152 let qsearch = String.sub
qsearch 0 (len - 1) in
2154 match search active qsearch ~
-1 with
2156 state.text <- qsearch ^
" [not found]";
2159 state.text <- qsearch;
2160 active, firstof active
2162 state.mode <- Outline
(
2163 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2166 Glut.postRedisplay
()
2169 if active < Array.length
outlines
2171 let (_
, _
, anchor) = outlines.(active) in
2176 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2177 state.mode <- oldmode
;
2178 Glut.postRedisplay
();
2180 | _
when key
>= 32 && key
< 127 ->
2181 let pattern = addchar qsearch (Char.chr key
) in
2183 match search active pattern 1 with
2185 state.text <- pattern ^
" [not found]";
2188 state.text <- pattern;
2189 active, firstof active
2191 state.mode <- Outline
(
2192 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2194 Glut.postRedisplay
()
2196 | 14 when not allowdel
-> (* ctrl-n *)
2197 if String.length
qsearch > 0
2199 let optoutlines = narrow outlines qsearch in
2200 begin match optoutlines with
2201 | None
-> state.text <- "can't narrow"
2203 state.mode <- Outline
(
2204 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2206 match state.outlines with
2209 state.outlines <- Onarrow
(qsearch, outlines, a)
2210 | Onarrow
(_
, _
, b) ->
2211 state.outlines <- Onarrow
(qsearch, outlines, b)
2214 Glut.postRedisplay
()
2216 | 21 when not allowdel
-> (* ctrl-u *)
2218 match state.outlines with
2221 let a = Array.of_list
(List.rev
l) in
2222 state.outlines <- Oarray
a;
2224 | Onarrow
(_
, _
, b) ->
2225 state.outlines <- Oarray
b;
2229 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2230 Glut.postRedisplay
()
2232 | 12 -> (* ctrl-l *)
2233 state.mode <- Outline
2234 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2235 Glut.postRedisplay
()
2237 | 127 when allowdel
-> (* delete *)
2238 let len = Array.length
outlines - 1 in
2242 state.bookmarks <- [];
2245 let bookmarks = Array.init
len
2247 let i = if i >= active then i + 1 else i in
2261 Glut.postRedisplay
()
2263 | _
-> dolog "unknown key %d" key
2266 let keyboard ~key ~x ~
y =
2271 match state.mode with
2272 | Outline
outline -> outlinekeyboard ~key ~x ~
y outline
2273 | Textentry
textentry -> textentrykeyboard ~key ~x ~
y textentry
2274 | Birdseye
birdseye -> birdseyekeyboard ~key ~x ~
y birdseye
2275 | View
-> viewkeyboard ~key ~x ~
y
2276 | Items
items -> itemskeyboard ~key ~x ~
y items
2279 let birdseyespecial key _ _
2280 ((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2282 | Glut.KEY_UP
-> upbirdseye beye
2283 | Glut.KEY_DOWN
-> downbirdseye beye
2285 | Glut.KEY_PAGE_UP
->
2286 begin match state.layout with
2290 state.mode <- Birdseye
(
2291 conf, leftx
, l.pageno, hooverpageno
, anchor
2293 gotopage1 l.pageno 0;
2296 let layout = layout (state.y-conf.winh
) conf.winh
in
2298 | [] -> gotoy (clamp (-conf.winh
))
2300 state.mode <- Birdseye
(
2301 conf, leftx
, l.pageno, hooverpageno
, anchor
2303 gotopage1 l.pageno 0
2306 | [] -> gotoy (clamp (-conf.winh
))
2309 | Glut.KEY_PAGE_DOWN
->
2310 begin match List.rev
state.layout with
2312 let layout = layout (state.y + conf.winh
) conf.winh
in
2313 begin match layout with
2315 let incr = l.pageh
- l.pagevh in
2320 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2322 Glut.postRedisplay
();
2324 else gotoy (clamp (incr + conf.interpagespace*2));
2328 Birdseye
(conf, leftx
, l.pageno, hooverpageno
, anchor);
2329 gotopage1 l.pageno 0;
2332 | [] -> gotoy (clamp conf.winh
)
2336 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2340 let pageno = state.pagecount
- 1 in
2341 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2342 if not
(pagevisible state.layout pageno)
2345 match List.rev
state.pdims
with
2347 | (_
, _
, h, _
) :: _
-> h
2349 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2350 else Glut.postRedisplay
();
2354 let setautoscrollspeed goingdown
=
2355 let incr = max
1 (state.ascrollstep
/ 2) in
2356 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2357 state.ascrollstep
<- astep;
2360 let special ~key ~x ~
y =
2361 match state.mode with
2362 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2366 birdseyespecial key x
y vals
2368 | View
when key
= Glut.KEY_F1
->
2372 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2373 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2377 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2378 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2379 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2380 | Glut.KEY_PAGE_UP
->
2381 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2383 match state.layout with
2385 | l :: _
-> state.y - l.pagey
2388 | Glut.KEY_PAGE_DOWN
->
2389 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2391 match List.rev
state.layout with
2393 | l :: _
-> getpagey l.pageno
2396 | Glut.KEY_HOME
-> addnav (); 0
2399 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2401 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2402 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2403 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2405 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2406 state.x
<- state.x
- 10;
2408 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2409 state.x
<- state.x
+ 10;
2414 gotoy_and_clear_text y
2417 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2420 | Glut.KEY_UP
-> action HCprev
2421 | Glut.KEY_DOWN
-> action HCnext
2422 | Glut.KEY_HOME
-> action HCfirst
2423 | Glut.KEY_END
-> action HClast
2426 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2427 Glut.postRedisplay
()
2431 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2432 let maxrows = maxoutlinerows () in
2433 let itemcount = Array.length
items in
2434 let hasaction = function
2435 | (_
, _
, Noaction
) -> false
2438 let find start
incr =
2440 if i = -1 || i = itemcount
2443 if hasaction items.(i)
2445 else find (i + incr)
2450 let set active first
=
2451 let first = max
0 (min
first (itemcount - maxrows)) in
2452 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2455 let isvisible first n = n >= first && n - first <= maxrows in
2457 let incr1 = if incr > 0 then 1 else -1 in
2458 if isvisible first active
2461 let next = active + incr in
2463 if next < 0 || next >= itemcount
2465 else find next incr1
2467 if next = -1 || abs
(active - next) > maxrows
2473 let first = first + incr in
2474 let first = max
0 (min
first (itemcount - 1)) in
2476 let next = active + incr in
2477 let next = max
0 (min
next (itemcount - 1)) in
2480 let active = if next = -1 then active else next in
2483 let first = min
next first in
2486 let first = first + incr in
2487 let first = max
0 (min
first (itemcount - 1)) in
2489 let next = active + incr in
2490 let next = max
0 (min
next (itemcount - 1)) in
2491 let next = find next incr1 in
2492 if next = -1 || abs
(active - first) > maxrows
2499 Glut.postRedisplay
()
2501 begin match key
with
2502 | Glut.KEY_UP
-> navigate ~
-1
2503 | Glut.KEY_DOWN
-> navigate 1
2504 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2505 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2508 state.mode <- Items
(
2509 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2511 Glut.postRedisplay
()
2514 state.mode <- Items
(
2515 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2517 Glut.postRedisplay
()
2520 let active = find 0 1 in
2522 Glut.postRedisplay
()
2525 let first = max
0 (itemcount - maxrows) in
2526 let active = find (itemcount - 1) ~
-1 in
2528 Glut.postRedisplay
()
2533 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2534 let maxrows = maxoutlinerows () in
2535 let calcfirst first active =
2538 let rows = active - first in
2539 if rows > maxrows then active - maxrows else first
2543 let active = active + incr in
2544 let active = max
0 (min
active (Array.length
outlines - 1)) in
2545 let first = calcfirst first active in
2546 state.mode <- Outline
(
2547 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2549 Glut.postRedisplay
()
2551 let updownlevel incr =
2552 let len = Array.length
outlines in
2553 let (_
, curlevel
, _
) = outlines.(active) in
2555 if i = len then i-1 else if i = -1 then 0 else
2556 let (_
, l, _
) = outlines.(i) in
2557 if l != curlevel
then i else flow (i+incr)
2559 let active = flow active in
2560 let first = calcfirst first active in
2561 state.mode <- Outline
(
2562 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2564 Glut.postRedisplay
()
2567 | Glut.KEY_UP
-> navigate ~
-1
2568 | Glut.KEY_DOWN
-> navigate 1
2569 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2570 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2573 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2575 state.mode <- Outline
(
2576 allowdel
, active, first, outlines,
2577 qsearch, min
0 (pan
+ 1), oldmode
2579 Glut.postRedisplay
();
2587 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2589 state.mode <- Outline
(
2590 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2592 Glut.postRedisplay
();
2596 then updownlevel ~
-1
2600 state.mode <- Outline
(
2601 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2603 Glut.postRedisplay
()
2606 let active = Array.length
outlines - 1 in
2607 let first = max
0 (active - maxrows) in
2608 state.mode <- Outline
(
2609 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2611 Glut.postRedisplay
()
2616 let drawplaceholder l =
2617 let margin = state.x
+ (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2619 (float l.pagex
, float l.pagedispy
)
2620 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
2622 let x = float (if margin < 0 then -margin else l.pagex
)
2623 and y = float (l.pagedispy
+ 13) in
2624 let font = Glut.BITMAP_8_BY_13
in
2625 GlDraw.color
(0.0, 0.0, 0.0);
2626 GlPix.raster_pos ~
x ~
y ();
2627 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
2628 ("Loading " ^ string_of_int
(l.pageno + 1));
2631 let now () = Unix.gettimeofday
();;
2635 match state.mode with
2636 | Textentry _
-> scalecolor 0.4
2637 | View
| Outline _
| Items _
-> scalecolor 1.0
2638 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2639 if l.pageno = hooverpageno
2642 if l.pageno = pageno
2648 begin match getopaque l.pageno with
2649 | Some
(opaque, _
) when validopaque opaque ->
2651 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2655 vlog "draw %d %f sec" l.pageno d;
2663 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2664 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2665 let sh = float conf.winh
/. sh in
2666 let sh = max
sh (float conf.scrollh
) in
2669 if state.y = state.maxy
2671 else float y /. float maxy
2673 let position = (float conf.winh
-. sh) *. percent in
2676 if position +. sh > float conf.winh
2677 then float conf.winh
-. sh
2683 let scrollindicator () =
2684 GlDraw.color (0.64 , 0.64, 0.64);
2686 (float (conf.winw
- conf.scrollw
), 0.)
2687 (float conf.winw
, float conf.winh
)
2689 GlDraw.color (0.0, 0.0, 0.0);
2691 let position, sh = scrollph state.y in
2693 (float (conf.winw
- conf.scrollw
), position)
2694 (float conf.winw
, position +. sh)
2698 let showsel margin =
2699 match state.mstate
with
2700 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2703 | Msel
((x0
, y0
), (x1
, y1
)) ->
2704 let rec loop = function
2706 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2707 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2709 match getopaque l.pageno with
2710 | Some
(opaque, _
) when validopaque opaque ->
2711 let oy = -l.pagey + l.pagedispy
in
2713 (x0
- margin - state.x, y0
,
2714 x1
- margin - state.x, y1
) oy;
2724 let panx = float state.x in
2726 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2727 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2729 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2731 if l.pageno = pageno
2733 let d = float (l.pagedispy
- l.pagey) in
2734 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2735 GlDraw.begins `quads
;
2737 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2738 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2739 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2740 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2750 let showstrings active first pan
strings =
2752 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2753 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2754 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2757 GlDraw.color (1., 1., 1.);
2758 let font = Glut.BITMAP_9_BY_15
in
2759 let draw_string x y s =
2760 GlPix.raster_pos ~
x ~
y ();
2761 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2764 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2767 let (s, level
, _
) = strings.(row
) in
2768 let y = (row
- first) * 16 in
2769 let x = 5 + 15*(max
0 (level
+pan
)) in
2773 GlDraw.polygon_mode `both `
line;
2774 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2775 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2776 GlDraw.rect (0., float (y + 1))
2777 (float (conf.winw
- 1), float (y + 18));
2778 GlDraw.polygon_mode `both `fill
;
2780 GlDraw.color (1., 1., 1.);
2783 let l = String.length
s in
2786 let pan = pan * 2 in
2787 let pos = pan + level
in
2788 let left = l + pos in
2794 else String.sub
s (-pos) left
2796 draw_string (float x) (float (y + 16)) s
2799 draw_string (float (x + pan*15)) (float (y + 16)) s
2808 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2809 showstrings active first pan outlines;
2812 let showitems (active, first, items, _
, pan, _
) =
2813 showstrings active first pan items;
2817 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2818 GlDraw.viewport
margin 0 state.w conf.winh
;
2820 GlClear.color (scalecolor2 conf.bgcolor
);
2821 GlClear.clear
[`
color];
2824 Gl.enable `scissor_test
;
2825 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
2827 List.iter
drawpage state.layout;
2830 Gl.disable `scissor_test
2834 let x = -.float state.x in
2835 GlMat.translate ~
x ();
2839 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2842 begin match state.mode with
2843 | Items
items -> showitems items
2844 | Outline
outline -> showoutline outline
2848 Glut.swapBuffers
();
2852 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
2853 let x = x - margin - state.x in
2854 let rec f = function
2856 begin match getopaque l.pageno with
2857 | Some
(opaque, _
) when validopaque opaque ->
2858 let y = y - l.pagedispy
in
2861 let y = l.pagey + y in
2862 let x = x - l.pagex
in
2863 match whatsunder
opaque x y with
2876 let viewmouse button bstate
x y =
2878 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2879 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2881 match state.mstate
with
2882 | Mzoom
(oldn
, i) ->
2890 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2892 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2894 let zoom = conf.zoom +. incr in
2896 state.mstate
<- Mzoom
(n, 0);
2898 state.mstate
<- Mzoom
(n, i+1);
2900 else state.mstate
<- Mzoom
(n, 0)
2902 | _
-> state.mstate
<- Mzoom
(n, 0)
2905 if state.ascrollstep
> 0
2907 setautoscrollspeed (n=4)
2911 then -conf.scrollstep
2912 else conf.scrollstep
2914 let incr = incr * 2 in
2915 let y = clamp incr in
2916 gotoy_and_clear_text y
2919 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2920 if bstate
= Glut.DOWN
2922 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2923 state.mstate
<- Mpan
(x, y)
2926 state.mstate
<- Mnone
2928 | Glut.LEFT_BUTTON
when x > conf.winw
- conf.scrollw
->
2929 if bstate
= Glut.DOWN
2931 let position, sh = scrollph state.y in
2932 if y > truncate
position && y < truncate
(position +. sh)
2934 state.mstate
<- Mscroll
2936 let percent = float y /. float conf.winh
in
2937 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2939 state.mstate
<- Mscroll
2941 state.mstate
<- Mnone
2943 | Glut.LEFT_BUTTON
->
2944 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2945 begin match dest with
2946 | Ulinkgoto
(pageno, top) ->
2950 gotopage1 pageno top;
2956 | Unone
when bstate
= Glut.DOWN
->
2957 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2958 state.mstate
<- Mpan
(x, y);
2960 | Unone
| Utext _
->
2961 if bstate
= Glut.DOWN
2963 if conf.angle
mod 360 = 0
2965 state.mstate
<- Msel
((x, y), (x, y));
2966 Glut.postRedisplay
()
2970 match state.mstate
with
2973 | Mzoom _
| Mscroll
->
2974 state.mstate
<- Mnone
2977 Glut.setCursor
Glut.CURSOR_INHERIT
;
2978 state.mstate
<- Mnone
2980 | Msel
((_
, y0
), (_
, y1
)) ->
2982 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2983 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2985 match getopaque l.pageno with
2986 | Some
(opaque, _
) when validopaque opaque ->
2990 List.iter
f state.layout;
2991 copysel
""; (* ugly *)
2992 Glut.setCursor
Glut.CURSOR_INHERIT
;
2993 state.mstate
<- Mnone
;
3000 let birdseyemouse button bstate
x y
3001 (conf, leftx
, _
, hooverpageno
, anchor) =
3003 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3004 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
3005 let rec loop = function
3008 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3009 && x > margin && x < margin + l.pagew
3011 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3016 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3020 let mouse bstate button
x y =
3021 match state.mode with
3022 | View
-> viewmouse button bstate
x y
3023 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3024 | Textentry _
| Outline _
| Items _
-> ()
3027 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3030 match state.mode with
3033 match state.mstate
with
3034 | Mzoom _
| Mnone
-> ()
3039 state.mstate
<- Mpan
(x, y);
3040 if conf.zoom > 1.0 then state.x <- state.x + dx;
3042 gotoy_and_clear_text y
3045 state.mstate
<- Msel
(a, (x, y));
3046 Glut.postRedisplay
()
3049 let y = min
conf.winh
(max
0 y) in
3050 let percent = float y /. float conf.winh
in
3051 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3052 gotoy_and_clear_text y
3056 match state.mode with
3057 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3058 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
3059 let rec loop = function
3061 if hooverpageno
!= -1
3063 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3064 Glut.postRedisplay
();
3067 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3068 && x > margin && x < margin + l.pagew
3070 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3071 Glut.postRedisplay
();
3077 | Outline _
| Items _
| Textentry _
-> ()
3079 match state.mstate
with
3081 begin match getunder x y with
3082 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3084 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3085 Glut.setCursor
Glut.CURSOR_INFO
3086 | Ulinkgoto
(page
, _
) ->
3088 then showtext 'p'
("age: " ^ string_of_int page
);
3089 Glut.setCursor
Glut.CURSOR_INFO
3091 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3092 Glut.setCursor
Glut.CURSOR_TEXT
3095 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3106 match Sys.os_type
with
3107 | "Win32" -> Sys.getenv
"HOMEPATH"
3108 | _
-> Sys.getenv
"HOME"
3111 ("Can not determine home directory location: " ^
3112 Printexc.to_string exn
);
3116 let color_of_string s =
3117 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
3118 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
3122 let config_of c attrs
=
3126 | "scroll-bar-width" -> { c with scrollw
= max
0 (int_of_string
v) }
3127 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3128 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3129 | "preload" -> { c with preload = bool_of_string
v }
3130 | "page-bias" -> { c with pagebias
= int_of_string
v }
3131 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3132 | "auto-scroll-step" ->
3133 { c with autoscrollstep
= max
0 (int_of_string
v) }
3134 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3135 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3136 | "throttle" -> { c with showall
= bool_of_string
v }
3137 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3138 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3139 | "vertical-margin" ->
3140 { c with interpagespace = max
0 (int_of_string
v) }
3142 let zoom = float_of_string
v /. 100. in
3143 let zoom = max
0.01 (min
2.2 zoom) in
3144 { c with zoom = zoom }
3145 | "presentation" -> { c with presentation = bool_of_string
v }
3146 | "rotation-angle" -> { c with angle
= int_of_string
v }
3147 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3148 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3149 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3150 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3151 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3152 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3153 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3154 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3155 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3156 | "background-color" -> { c with bgcolor
= color_of_string v }
3159 prerr_endline
("Error processing attribute (`" ^
3160 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3163 let rec fold c = function
3166 let c = apply c k
v in
3172 let fromstring f pos n v d =
3175 dolog "Error processing attribute (%S=%S) at %d\n%s"
3176 n v pos (Printexc.to_string exn
)
3181 let bookmark_of attrs
=
3182 let rec fold title page rely
= function
3183 | ("title", v) :: rest
-> fold v page rely rest
3184 | ("page", v) :: rest
-> fold title v rely rest
3185 | ("rely", v) :: rest
-> fold title page
v rest
3186 | _
:: rest
-> fold title page rely rest
3187 | [] -> title, page
, rely
3189 fold "invalid" "0" "0" attrs
3193 let rec fold path page rely
pan = function
3194 | ("path", v) :: rest
-> fold v page rely
pan rest
3195 | ("page", v) :: rest
-> fold path
v rely
pan rest
3196 | ("rely", v) :: rest
-> fold path page
v pan rest
3197 | ("pan", v) :: rest
-> fold path page rely
v rest
3198 | _
:: rest
-> fold path page rely
pan rest
3199 | [] -> path
, page
, rely
, pan
3201 fold "" "0" "0" "0" attrs
3204 let setconf dst src
=
3205 dst
.scrollw
<- src
.scrollw
;
3206 dst
.scrollh
<- src
.scrollh
;
3207 dst
.icase
<- src
.icase
;
3208 dst
.preload <- src
.preload;
3209 dst
.pagebias
<- src
.pagebias
;
3210 dst
.verbose
<- src
.verbose
;
3211 dst
.scrollstep
<- src
.scrollstep
;
3212 dst
.maxhfit
<- src
.maxhfit
;
3213 dst
.crophack
<- src
.crophack
;
3214 dst
.autoscrollstep
<- src
.autoscrollstep
;
3215 dst
.showall
<- src
.showall
;
3216 dst
.hlinks
<- src
.hlinks
;
3217 dst
.underinfo
<- src
.underinfo
;
3218 dst
.interpagespace <- src
.interpagespace;
3219 dst
.zoom <- src
.zoom;
3220 dst
.presentation <- src
.presentation;
3221 dst
.angle
<- src
.angle
;
3222 dst
.winw
<- src
.winw
;
3223 dst
.winh
<- src
.winh
;
3224 dst
.savebmarks
<- src
.savebmarks
;
3225 dst
.memlimit
<- src
.memlimit
;
3226 dst
.proportional
<- src
.proportional
;
3227 dst
.texcount
<- src
.texcount
;
3228 dst
.sliceheight
<- src
.sliceheight
;
3229 dst
.thumbw
<- src
.thumbw
;
3230 dst
.jumpback
<- src
.jumpback
;
3231 dst
.bgcolor
<- src
.bgcolor
;
3235 let l = String.length
s in
3236 let b = Buffer.create
l in
3242 let h = Hashtbl.create
10 in
3243 let dc = { defconf with angle
= defconf.angle
} in
3244 let rec toplevel v t spos _
=
3246 | Vdata
| Vcdata
| Vend
-> v
3247 | Vopen
("llppconfig", _
, closed
) ->
3250 else { v with f = llppconfig
}
3252 error
"unexpected subelement at top level" s spos
3253 | Vclose _
-> error
"unexpected close at top level" s spos
3255 and llppconfig
v t spos _
=
3257 | Vdata
| Vcdata
| Vend
-> v
3258 | Vopen
("defaults", attrs
, closed
) ->
3259 let c = config_of dc attrs
in
3263 else { v with f = skip
"defaults" (fun () -> v) }
3265 | Vopen
("doc", attrs
, closed
) ->
3266 let pathent, spage
, srely
, span
= doc_of attrs
in
3267 let path = unent pathent
3268 and pageno = fromstring int_of_string spos
"page" spage
0
3269 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3270 and pan = fromstring int_of_string spos
"pan" span
0 in
3271 let c = config_of dc attrs
in
3272 let anchor = (pageno, rely
) in
3274 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3275 else { v with f = doc
path pan anchor c [] }
3278 error
"unexpected subelement in llppconfig" s spos
3280 | Vclose
"llppconfig" -> { v with f = toplevel }
3281 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3283 and doc
path pan anchor c bookmarks v t spos _
=
3285 | Vdata
| Vcdata
-> v
3286 | Vend
-> error
"unexpected end of input in doc" s spos
3287 | Vopen
("bookmarks", _
, closed
) ->
3290 else { v with f = pbookmarks
path pan anchor c bookmarks }
3292 | Vopen
(_
, _
, _
) ->
3293 error
"unexpected subelement in doc" s spos
3296 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3297 { v with f = llppconfig
}
3299 | Vclose _
-> error
"unexpected close in doc" s spos
3301 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3303 | Vdata
| Vcdata
-> v
3304 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3305 | Vopen
("item", attrs
, closed
) ->
3306 let titleent, spage
, srely
= bookmark_of attrs
in
3307 let page = fromstring int_of_string spos
"page" spage
0
3308 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3309 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3311 then { v with f = pbookmarks
path pan anchor c bookmarks }
3314 { v with f = skip
"item" f }
3317 error
"unexpected subelement in bookmarks" s spos
3319 | Vclose
"bookmarks" ->
3320 { v with f = doc
path pan anchor c bookmarks }
3322 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3324 and skip tag
f v t spos _
=
3326 | Vdata
| Vcdata
-> v
3328 error
("unexpected end of input in skipped " ^ tag
) s spos
3329 | Vopen
(tag'
, _
, closed
) ->
3333 let f'
() = { v with f = skip tag
f } in
3334 { v with f = skip tag'
f'
}
3338 else error
("unexpected close in skipped " ^ tag
) s spos
3341 parse
{ f = toplevel; accu = () } s;
3347 let len = in_channel_length ic
in
3348 let s = String.create
len in
3349 really_input ic
s 0 len;
3352 | Parse_error
(msg
, s, pos) ->
3353 let subs = subs s pos in
3354 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3355 failwith
("parse error: " ^
s)
3358 failwith
("config load error: " ^
Printexc.to_string exn
)
3364 let dir = Filename.concat
home ".config" in
3365 if Sys.is_directory
dir then dir else home
3368 Filename.concat
dir "llpp.conf"
3372 if Sys.file_exists
path
3375 (try Some
(open_in_bin
path)
3378 ("Error opening configuation file `" ^
path ^
"': " ^
3379 Printexc.to_string exn
);
3388 ("Error loading configuation from `" ^
path ^
"': " ^
3389 Printexc.to_string exn
);
3395 f (Hashtbl.create
0, defconf)
3400 let pc, pb
, px
, pa
=
3402 Hashtbl.find h (Filename.basename
state.path)
3403 with Not_found
-> dc, [], 0, (0, 0.0)
3407 state.bookmarks <- pb
;
3410 then state.anchor <- pa
;
3411 cbput state.hists
.nav pa
;
3416 let add_attrs bb always
dc c =
3419 then Printf.bprintf bb
"\n %s='%b'" s a
3422 then Printf.bprintf bb
"\n %s='%d'" s a
3425 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3430 let r = truncate
(r *. 256.0)
3431 and g
= truncate
(g
*. 256.0)
3432 and b = truncate
(b *. 256.0) in
3433 Printf.bprintf bb
"\n %s='%d/%d/%d'" s r g
b
3437 then dc.winw
, dc.winh
3439 match state.fullscreen
with
3441 | None
-> c.winw
, c.winh
3443 let zoom, presentation, interpagespace, showall
=
3445 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3447 match state.mode with
3448 | Birdseye
(bc
, _
, _
, _
, _
) ->
3449 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3450 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3452 oi
"width" w dc.winw
;
3453 oi
"height" h dc.winh
;
3454 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
3455 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3456 ob "case-insensitive-search" c.icase
dc.icase
;
3457 ob "preload" c.preload dc.preload;
3458 oi
"page-bias" c.pagebias
dc.pagebias
;
3459 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3460 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3461 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3462 ob "crop-hack" c.crophack
dc.crophack
;
3463 ob "throttle" showall
dc.showall
;
3464 ob "highlight-links" c.hlinks
dc.hlinks
;
3465 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3466 oi
"vertical-margin" interpagespace dc.interpagespace;
3467 oz
"zoom" zoom dc.zoom;
3468 ob "presentation" presentation dc.presentation;
3469 oi
"rotation-angle" c.angle
dc.angle
;
3470 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3471 ob "proportional-display" c.proportional
dc.proportional
;
3472 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3473 oi
"texcount" c.texcount
dc.texcount
;
3474 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3475 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3476 ob "persistent-location" c.jumpback
dc.jumpback
;
3477 oc
"background-color" c.bgcolor
dc.bgcolor
;
3481 let bb = Buffer.create
32768 in
3483 let dc = if conf.bedefault
then conf else dc in
3484 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3485 add_attrs bb true dc dc;
3486 Buffer.add_string
bb "/>\n";
3488 let adddoc path pan anchor c bookmarks =
3489 if bookmarks == [] && c = dc && anchor = emptyanchor
3492 Printf.bprintf
bb "<doc path='%s'"
3493 (enent
path 0 (String.length
path));
3495 if anchor <> emptyanchor
3497 let n, y = anchor in
3498 Printf.bprintf
bb " page='%d'" n;
3501 Printf.bprintf
bb " rely='%f'" y
3506 then Printf.bprintf
bb " pan='%d'" pan;
3508 add_attrs bb false dc c;
3510 begin match bookmarks with
3511 | [] -> Buffer.add_string
bb "/>\n"
3513 Buffer.add_string
bb ">\n<bookmarks>\n";
3514 List.iter
(fun (title, _level
, (page, rely
)) ->
3516 "<item title='%s' page='%d'"
3517 (enent
title 0 (String.length
title))
3522 Printf.bprintf
bb " rely='%f'" rely
3524 Buffer.add_string
bb "/>\n";
3526 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3532 match state.mode with
3533 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3536 let basename = Filename.basename state.path in
3537 adddoc basename pan (getanchor ())
3540 if state.ascrollstep
> 0
3541 then state.ascrollstep
3542 else conf.autoscrollstep
}
3543 (if conf.savebmarks
then state.bookmarks else []);
3545 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3547 then adddoc path x y c bookmarks
3549 Buffer.add_string
bb "</llppconfig>";
3552 if Buffer.length
bb > 0
3555 let tmp = path ^
".tmp" in
3556 let oc = open_out_bin
tmp in
3557 Buffer.output_buffer
oc bb;
3559 Sys.rename
tmp path;
3562 ("error while saving configuration: " ^
Printexc.to_string exn
)
3569 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3570 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3571 " Print version and exit")]
3573 (fun s -> state.path <- s)
3574 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3576 if String.length
state.path = 0
3577 then (prerr_endline
"file name missing"; exit
1);
3581 let _ = Glut.init
Sys.argv
in
3582 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3583 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3584 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3587 if Sys.os_type
= "Unix"
3589 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3591 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3592 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3593 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3594 Unix.bind
sock addr;
3596 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3597 Unix.connect
csock addr;
3598 let ssock, _ = Unix.accept
sock in
3601 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3602 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3606 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3610 let () = Glut.displayFunc
display in
3611 let () = Glut.reshapeFunc
reshape in
3612 let () = Glut.keyboardFunc
keyboard in
3613 let () = Glut.specialFunc
special in
3614 let () = Glut.idleFunc
(Some
idle) in
3615 let () = Glut.mouseFunc
mouse in
3616 let () = Glut.motionFunc
motion in
3617 let () = Glut.passiveMotionFunc
pmotion in
3619 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
3620 state.csock <- csock;
3621 state.ssock <- ssock;
3622 state.text <- "Opening " ^
state.path;
3623 writeopen state.path state.password
;
3627 let rec handlelablglutbug () =
3630 with Glut.BadEnum
"key in special_of_int" ->
3631 showtext '
!'
" LablGlut bug: special key not recognized";
3632 handlelablglutbug ()
3634 handlelablglutbug ();