4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let log fmt
= Printf.kprintf prerr_endline fmt
;;
9 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 external init
: Unix.file_descr
-> unit = "ml_init";;
12 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext
: string -> (int * int * int * int) -> int -> unit =
15 external copysel
: string -> unit = "ml_copysel";;
16 external getpagewh
: int -> float array
= "ml_getpagewh";;
17 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
19 type mstate
= Msel
of ((int * int) * (int * int)) | Mnone
;;
29 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
30 and onkey
= string -> int -> te
31 and ondone
= string -> unit
32 and onhist
= histcmd
-> string
33 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
38 | TEswitch
of textentry
42 { store
= Array.create n v
49 let cblen b
= Array.length b
.store
;;
54 b
.wc
<- (b
.wc
+ 1) mod len;
55 b
.len <- min
(b
.len + 1) len;
58 let cbpeekw b
= b
.store
.(b
.wc
);;
61 if b
.len = 0 then b
.store
.(0) else
62 let rc = b
.rc + dir
in
63 let rc = if rc = -1 then b
.len - 1 else rc in
64 let rc = if rc = b
.len then 0 else rc in
75 Array.fill b
.store
0 (Array.length b
.store
) v
;
90 { mutable scrollw
: int
91 ; mutable scrollh
: int
92 ; mutable icase
: bool
93 ; mutable preload
: bool
94 ; mutable pagebias
: int
95 ; mutable verbose
: bool
96 ; mutable scrollincr
: int
97 ; mutable maxhfit
: bool
98 ; mutable crophack
: bool
99 ; mutable autoscroll
: bool
100 ; mutable showall
: bool
101 ; mutable hlinks
: bool
102 ; mutable underinfo
: bool
106 type outline
= string * int * int * float;;
108 | Oarray
of outline array
109 | Olist
of outline list
110 | Onarrow
of outline array
* outline array
113 type rect
= (float * float * float * float * float * float * float * float);;
116 { mutable csock
: Unix.file_descr
117 ; mutable ssock
: Unix.file_descr
120 ; mutable rotate
: int
124 ; mutable layout
: layout list
125 ; pagemap
: ((int * int * int), string) Hashtbl.t
126 ; mutable pages
: (int * int * int) list
127 ; mutable pagecount
: int
128 ; pagecache
: string circbuf
129 ; mutable rendering
: bool
130 ; mutable mstate
: mstate
131 ; mutable searchpattern
: string
132 ; mutable rects
: (int * int * rect
) list
133 ; mutable rects1
: (int * int * rect
) list
134 ; mutable text
: string
135 ; mutable fullscreen
: (int * int) option
136 ; mutable textentry
: textentry
option
137 ; mutable outlines
: outlines
138 ; mutable outline
: (bool * int * int * outline array
* string) option
139 ; mutable bookmarks
: outline list
140 ; mutable path
: string
141 ; mutable invalidated
: int
142 ; mutable colorscale
: float
146 { pat
: string circbuf
147 ; pag
: string circbuf
148 ; nav
: float circbuf
179 ; pagemap
= Hashtbl.create
10
180 ; pagecache
= cbnew 10 ""
191 ; outlines
= Olist
[]
197 { nav
= cbnew 100 0.0
208 Printf.kprintf prerr_endline fmt
210 Printf.kprintf ignore fmt
214 let len = String.length s
in
216 let b = Buffer.create
n in
217 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
218 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
219 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
220 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
221 Buffer.add_string
b s
;
222 let s'
= Buffer.contents
b in
223 let n'
= Unix.write fd
s'
0 n in
224 if n'
!= n then failwith
"write failed";
229 let n = Unix.read fd
s 0 4 in
230 if n != 4 then failwith
"incomplete read(len)";
232 lor (Char.code
s.[0] lsl 24)
233 lor (Char.code
s.[1] lsl 16)
234 lor (Char.code
s.[2] lsl 8)
235 lor (Char.code
s.[3] lsl 0)
237 let s = String.create
len in
238 let n = Unix.read fd
s 0 len in
239 if n != len then failwith
"incomplete read(data)";
244 if y
= state.maxy
then 1.0
245 else float y
/. float state.maxy
249 let b = Buffer.create
10 in
250 Buffer.add_string
b s;
251 let rec combine = function
254 Buffer.add_char
b ' '
;
257 | `
b b -> if b then "1" else "0"
259 | `i i
-> string_of_int i
260 | `f f
-> string_of_float f
261 | `I f
-> string_of_int
(truncate f
)
263 Buffer.add_string
b s;
270 let cmd = Buffer.contents
(makecmd s l
) in
271 writecmd state.csock
cmd;
275 let rec f pn ph fh l
=
277 | (n, _
, h
) :: rest
->
278 let fh = fh + (n - pn
) * ph
in
282 let fh = fh + (ph
* (state.pagecount
- pn
)) in
285 let fh = f 0 0 0 state.pages
in
289 let getpageyh pageno
=
290 let rec f pn ph y l
=
292 | (n, _
, h
) :: rest
->
295 y
+ (pageno
- pn
) * ph
, h
297 let y = y + (n - pn
) * ph
in
301 y + (pageno
- pn
) * ph
, ph
306 let getpagey pageno
= fst
(getpageyh pageno
);;
309 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
310 if pageno
= state.pagecount
|| cacheleft
= 0
313 let ((_
, w
, h
) as curr
), rest
, pdimno
=
315 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
316 curr
, rest
, pdimno
+ 1
320 let pageno'
= pageno + 1 in
354 let accu = e :: accu in
355 f pageno' pdimno curr
356 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
357 (pred cacheleft
) accu
359 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
361 if state.invalidated
= 0
363 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
364 state.maxy
<- calcheight ();
371 let y = state.y + incr
in
373 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
377 let getopaque pageno =
378 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
380 with Not_found
-> None
383 let cache pageno opaque
=
384 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
388 let validopaque opaque
= String.length opaque
> 0;;
391 match getopaque l
.pageno with
392 | None
when not
state.rendering
->
393 state.rendering
<- true;
395 wcmd "render" [`i
(l
.pageno + 1)
403 let loadlayout layout =
404 let rec f all
= function
406 begin match getopaque l
.pageno with
407 | None
-> render l
; f false ls
408 | Some opaque
-> f (all
&& validopaque opaque
) ls
412 f (layout <> []) layout;
418 let evictedopaque = cbpeekw state.pagecache
in
419 List.exists
(fun l
->
420 match getopaque l
.pageno with
421 | Some opaque
when validopaque opaque
->
422 evictedopaque = opaque
426 if not
evictedvisible then
427 let y = if state.y < state.h
then 0 else state.y - state.h
in
428 let pages = layout y (state.h
*3) in
429 List.iter
render pages;
434 let y = min
state.maxy
y in
435 let pages = layout y state.h
in
436 let ready = loadlayout pages in
437 state.ty
<- yratio y;
438 if conf.showall
then (
440 state.layout <- pages;
442 Glut.postRedisplay
();
446 state.layout <- pages;
448 Glut.postRedisplay
();
454 cbput state.hists
.nav
(yratio state.y);
455 cbrfollowlen state.hists
.nav
;
459 let y = cbget state.hists
.nav ~
-1 in
460 truncate
(y *. float state.maxy
)
464 let y, h
= getpageyh n in
466 gotoy (y + (truncate
(top
*. float h
)));
469 let gotopage1 n top
=
470 let y = getpagey n in
480 state.invalidated
<- state.invalidated
+ 1;
484 let c = c *. state.colorscale
in
491 GlDraw.viewport
0 0 w h
;
492 GlMat.mode `modelview
;
493 GlMat.load_identity
();
494 GlMat.mode `projection
;
495 GlMat.load_identity
();
496 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
497 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
498 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
499 GlClear.color
(scalecolor 1.0);
500 GlClear.clear
[`color
];
503 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
507 GlDraw.color
(0.0, 0.0, 0.0);
509 (0.0, float (state.h
- 18))
510 (float (state.w
- conf.scrollw
- 1), float state.h
)
512 let font = Glut.BITMAP_8_BY_13
in
513 GlDraw.color
(1.0, 1.0, 1.0);
514 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
515 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
516 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
520 let len = String.length
state.text
in
521 match state.textentry
with
523 if len > 0 then showtext ' '
state.text
525 | Some
(c, text
, _
, _
, _
) ->
529 text ^
" [" ^
state.text ^
"]"
539 state.text
<- Printf.sprintf
"%c%s" c s;
540 Glut.postRedisplay
();
554 state.rects
<- state.rects1
;
555 Glut.postRedisplay
()
558 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
559 state.pagecount
<- n;
560 state.invalidated
<- state.invalidated
- 1;
561 if state.invalidated
= 0
563 let rely = yratio state.y in
564 state.maxy
<- calcheight ();
565 gotoy (truncate
(float state.maxy
*. rely));
569 let s = Scanf.sscanf
cmd "t %n"
570 (fun n -> String.sub
cmd n (String.length
cmd - n))
572 Glut.setWindowTitle
s
575 let s = Scanf.sscanf
cmd "T %n"
576 (fun n -> String.sub
cmd n (String.length
cmd - n))
578 if state.textentry
= None
585 Glut.postRedisplay
();
591 let s = Scanf.sscanf
cmd "V %n"
592 (fun n -> String.sub
cmd n (String.length
cmd - n))
598 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
599 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
600 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
601 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
603 let y = (getpagey pageno) + truncate y0
in
606 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
609 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
610 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
611 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
612 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
615 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
619 Scanf.sscanf
cmd "r %d %d %d %d %s"
620 (fun n w h r p
-> (n, w
, h
, r
, p
))
622 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
623 let opaque = cbpeekw state.pagecache
in
624 if validopaque opaque
628 (fun k v a
-> if v
= opaque then k else a
)
629 state.pagemap
(-1, -1, -1)
631 wcmd "free" [`
s opaque];
632 Hashtbl.remove
state.pagemap
k
634 cbput state.pagecache p
;
635 state.rendering
<- false;
637 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
639 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
640 if visible then gotoy state.y
641 else (ignore
(loadlayout state.layout); preload ())
645 let (n, w
, h
) as pagelayout
=
646 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
648 state.pages <- pagelayout
:: state.pages
651 let (l
, n, t
, h
, pos
) =
652 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
654 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
656 let l = String.length
s in
657 let b = Buffer.create
(String.length
s) in
659 if i
= l then () else
662 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
665 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
674 let outline = (s, l, n, float t
/. float h
) in
676 match state.outlines with
677 | Olist
outlines -> Olist
(outline :: outlines)
678 | Oarray _
-> Olist
[outline]
679 | Onarrow _
-> Olist
[outline]
681 state.outlines <- outlines
684 log "unknown cmd `%S'" cmd
687 let now = Unix.gettimeofday
;;
691 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
694 if conf.autoscroll
then begin
695 let y = state.y + conf.scrollincr
in
696 let y = if y >= state.maxy
then 0 else y in
702 let cmd = readcmd state.csock
in
709 let onhist cb
= function
710 | HCprev
-> cbget cb ~
-1
711 | HCnext
-> cbget cb
1
712 | HCfirst
-> cbget cb ~
-(cb
.rc)
713 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
716 let search pattern forward
=
717 if String.length pattern
> 0
720 match state.layout with
723 l.pageno, (l.pagey
+ if forward
then 0 else 0*l.pagevh
)
726 let b = makecmd "search"
727 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
729 Buffer.add_char
b '
,'
;
730 Buffer.add_string
b pattern
;
731 Buffer.add_char
b '
\000'
;
734 writecmd state.csock
cmd;
737 let intentry text key
=
738 let c = Char.unsafe_chr key
in
741 let s = "x" in s.[0] <- c;
742 let text = text ^
s in
746 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
751 let b = Buffer.create
(String.length
s + 1) in
752 Buffer.add_string
b s;
757 let textentry text key
=
758 let c = Char.unsafe_chr key
in
760 | _
when key
>= 32 && key
< 127 ->
761 let text = addchar text c in
765 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
770 state.rotate <- angle
;
772 wcmd "rotate" [`i angle
];
775 let optentry text key
=
776 let btos b = if b then "on" else "off" in
777 let c = Char.unsafe_chr key
in
781 try conf.scrollincr
<- int_of_string
s with exc
->
782 state.text <- Printf.sprintf
"bad integer `%s': %s"
783 s (Printexc.to_string exc
)
785 TEswitch
('#'
, "", None
, intentry, ondone)
790 Some
(int_of_string
s)
792 state.text <- Printf.sprintf
"bad integer `%s': %s"
793 s (Printexc.to_string exc
);
796 | Some angle
-> rotate angle
799 TEswitch
('^'
, "", None
, intentry, ondone)
802 conf.icase
<- not
conf.icase
;
803 TEdone
("case insensitive search " ^
(btos conf.icase
))
806 conf.preload <- not
conf.preload;
808 TEdone
("preload " ^
(btos conf.preload))
811 conf.verbose
<- not
conf.verbose
;
812 TEdone
("verbose " ^
(btos conf.verbose
))
815 conf.maxhfit
<- not
conf.maxhfit
;
816 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
817 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
820 conf.crophack
<- not
conf.crophack
;
821 TEdone
("crophack " ^
btos conf.crophack
)
824 conf.showall
<- not
conf.showall
;
825 TEdone
("showall " ^
btos conf.showall
)
828 conf.underinfo
<- not
conf.underinfo
;
829 TEdone
("underinfo " ^
btos conf.underinfo
)
832 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
836 let maxoutlinerows () = (state.h
- 31) / 16;;
838 let enterselector allowdel
outlines errmsg
=
839 if Array.length
outlines = 0
844 Glut.setCursor
Glut.CURSOR_INHERIT
;
846 match state.layout with
848 | {pageno=pageno} :: rest
-> pageno
852 if n = Array.length
outlines
855 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
856 if outlinepageno
>= pageno then n else loop (n+1)
861 Some
(allowdel
, active,
862 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
863 Glut.postRedisplay
();
867 let enteroutlinemode () =
869 match state.outlines with
872 let a = Array.of_list
(List.rev
l) in
873 state.outlines <- Oarray
a;
875 | Onarrow
(a, b) -> a
877 enterselector false outlines "Document has no outline";
880 let enterbookmarkmode () =
881 let bookmarks = Array.of_list
state.bookmarks in
882 enterselector true bookmarks "Document has no bookmarks (yet)";
886 let quickbookmark ?title
() =
887 match state.layout with
893 let sec = Unix.gettimeofday
() in
894 let tm = Unix.localtime
sec in
895 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
899 (tm.Unix.tm_year
+ 1900)
902 | Some
title -> title
905 (title, 0, l.pageno, float l.pagey
/. float l.pageh
) :: state.bookmarks
909 state.fullscreen
<- None
;
910 Glut.reshapeWindow w h
;
917 (fun k v
-> if validopaque v
then wcmd "drop" [`
s v
]) state.pagemap
;
918 Hashtbl.clear
state.pagemap
;
919 cbclear state.pagecache
"";
921 writecmd state.csock
("open " ^ path ^
"\000");
922 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
923 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i
state.h
];
926 let viewkeyboard ~key ~x ~
y =
928 state.textentry <- te
;
931 Glut.postRedisplay
()
933 match state.textentry with
935 let c = Char.chr key
in
950 Glut.postRedisplay
()
953 let ondone isforw
s =
954 cbput state.hists
.pat
s;
955 cbrfollowlen state.hists
.pat
;
956 state.searchpattern
<- s;
959 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
960 textentry, ondone (c ='
/'
)))
965 try int_of_string
s with exc
->
966 state.text <- Printf.sprintf
"bad integer `%s': %s"
967 s (Printexc.to_string exc
);
973 state.text <- "page bias is now " ^ string_of_int
n;
976 enttext (Some
('
+'
, "", None
, intentry, ondone))
982 enttext (Some
('
-'
, "", None
, optentry, ondone))
987 try int_of_string
s with exc
->
988 state.text <- Printf.sprintf
"bad integer `%s': %s"
989 s (Printexc.to_string exc
);
995 cbput state.hists
.pag
(string_of_int
n);
996 cbrfollowlen state.hists
.pag
;
997 gotoy (getpagey (n + conf.pagebias
- 1))
1000 let pageentry text key
=
1001 match Char.unsafe_chr key
with
1002 | 'g'
-> TEdone
text
1003 | _
-> intentry text key
1005 let text = "x" in text.[0] <- c;
1006 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1010 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
1011 reshape state.w
state.h
;
1014 conf.hlinks
<- not
conf.hlinks
;
1015 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1016 Glut.postRedisplay
()
1019 conf.autoscroll
<- not
conf.autoscroll
1022 begin match state.fullscreen
with
1024 state.fullscreen
<- Some
(state.w
, state.h
);
1027 state.fullscreen
<- None
;
1035 search state.searchpattern
true
1038 search state.searchpattern
false
1041 begin match state.layout with
1044 gotoy (state.y - l.pagey
);
1048 begin match List.rev
state.layout with
1051 gotoy (clamp (l.pageh
- l.pagey
))
1055 begin match state.layout with
1058 gotoy (clamp (-l.pageh
));
1063 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1065 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1067 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1071 else (100. *. (float state.y /. float maxy)) in
1074 Printf.sprintf
"Page %d of %d %.2f%%"
1075 (fn+1) state.pagecount
percent
1078 "Pages %d-%d of %d %.2f%%"
1079 (fn+1) (ln
+1) state.pagecount
percent
1084 begin match state.layout with
1087 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1088 Glut.postRedisplay
();
1092 enterbookmarkmode ()
1096 match state.layout with
1099 (s, 0, l.pageno, float l.pagey
/. float l.pageh
)
1103 enttext (Some
('~'
, "", None
, textentry, ondone))
1107 showtext ' '
"Quick bookmark added";
1110 begin match state.layout with
1112 let a = getpagewh
l.pagedimno
in
1116 (truncate
(1.8 *. (a.(1) -. a.(0))),
1117 truncate
(1.2 *. (a.(3) -. a.(0))))
1119 (truncate
(a.(1) -. a.(0)),
1120 truncate
(a.(3) -. a.(0)))
1122 doreshape (w + conf.scrollw
) h
;
1123 Glut.postRedisplay
();
1129 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1134 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1135 Glut.postRedisplay
()
1137 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1138 | 'j'
-> gotoy (clamp conf.scrollincr
)
1140 | '
r'
-> opendoc state.path
1143 vlog "huh? %d %c" key
(Char.chr key
);
1146 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1147 let len = String.length
text in
1150 state.textentry <- None
;
1151 Glut.postRedisplay
();
1154 let s = String.sub
text 0 (len - 1) in
1155 enttext (Some
(c, s, onhist, onkey
, ondone))
1158 | Some
(c, text, onhist, onkey
, ondone) ->
1159 begin match Char.unsafe_chr key
with
1162 state.textentry <- None
;
1163 Glut.postRedisplay
()
1166 state.textentry <- None
;
1167 Glut.postRedisplay
()
1170 begin match onkey
text key
with
1172 state.textentry <- None
;
1174 Glut.postRedisplay
()
1177 enttext (Some
(c, text, onhist, onkey
, ondone));
1180 state.textentry <- None
;
1181 Glut.postRedisplay
()
1184 state.textentry <- Some te
;
1185 Glut.postRedisplay
()
1190 let narrow outlines pattern
=
1191 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1195 let rec fold accu n =
1196 if n = -1 then accu else
1197 let (s, _
, _
, _
) as o
= outlines.(n) in
1199 if (try ignore
(Str.search_forward re
s 0); true
1200 with Not_found
-> false)
1206 let matched = fold [] (Array.length
outlines - 1) in
1207 if matched = [] then None
else Some
(Array.of_list
matched)
1210 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1211 let search active pattern incr
=
1214 if n = Array.length
outlines || n = -1 then None
else
1215 let (s, _
, _
, _
) = outlines.(n) in
1217 (try ignore
(Str.search_forward re
s 0); true
1218 with Not_found
-> false)
1220 else loop (n + incr
)
1225 let re = Str.regexp_case_fold pattern
in
1231 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1234 if String.length qsearch
= 0
1237 state.outline <- None
;
1238 Glut.postRedisplay
();
1242 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1243 Glut.postRedisplay
();
1247 let incr = if key
= 18 then -1 else 1 in
1249 match search (active + incr) qsearch
incr with
1251 state.text <- qsearch ^
" [not found]";
1254 state.text <- qsearch
;
1255 active, firstof active
1257 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1258 Glut.postRedisplay
();
1261 let len = String.length qsearch
in
1268 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1271 let qsearch = String.sub
qsearch 0 (len - 1) in
1273 match search active qsearch ~
-1 with
1275 state.text <- qsearch ^
" [not found]";
1278 state.text <- qsearch;
1279 active, firstof active
1281 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1283 Glut.postRedisplay
()
1286 if active < Array.length
outlines
1288 let (_
, _
, n, t
) = outlines.(active) in
1292 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1293 state.outline <- None
;
1294 Glut.postRedisplay
();
1296 | _
when key
>= 32 && key
< 127 ->
1297 let pattern = addchar qsearch (Char.chr key
) in
1299 match search active pattern 1 with
1301 state.text <- pattern ^
" [not found]";
1304 state.text <- pattern;
1305 active, firstof active
1307 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1308 Glut.postRedisplay
()
1310 | 14 when not allowdel
->
1311 let optoutlines = narrow outlines qsearch in
1312 begin match optoutlines with
1313 | None
-> state.text <- "can't narrow"
1315 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1316 match state.outlines with
1318 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1319 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1321 Glut.postRedisplay
()
1323 | 21 when not allowdel
->
1325 match state.outlines with
1328 let a = Array.of_list
(List.rev
l) in
1329 state.outlines <- Oarray
a;
1332 state.outlines <- Oarray
b;
1335 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1336 Glut.postRedisplay
()
1340 Some
(allowdel
, active, firstof active, outlines, qsearch);
1341 Glut.postRedisplay
()
1343 | 127 when allowdel
->
1344 let len = Array.length
outlines - 1 in
1347 state.outline <- None
;
1348 state.bookmarks <- [];
1351 let bookmarks = Array.init
len
1353 let i = if i >= active then i + 1 else i in
1364 Glut.postRedisplay
()
1366 | _
-> log "unknown key %d" key
1369 let keyboard ~key ~x ~
y =
1374 match state.outline with
1375 | None
-> viewkeyboard ~key ~x ~
y
1376 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1379 let special ~key ~x ~
y =
1380 match state.outline with
1382 begin match state.textentry with
1386 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1387 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1388 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1389 | Glut.KEY_PAGE_UP
->
1390 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1392 match state.layout with
1394 | l :: _
-> state.y - l.pagey
1397 | Glut.KEY_PAGE_DOWN
->
1398 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1400 match List.rev
state.layout with
1402 | l :: _
-> getpagey l.pageno
1405 | Glut.KEY_HOME
-> addnav (); 0
1408 state.maxy - (if conf.maxhfit
then state.h
else 0)
1411 if not
conf.verbose
then state.text <- "";
1414 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1417 | Glut.KEY_UP
-> onhist HCprev
1418 | Glut.KEY_DOWN
-> onhist HCnext
1419 | Glut.KEY_HOME
-> onhist HCfirst
1420 | Glut.KEY_END
-> onhist HClast
1423 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1424 Glut.postRedisplay
()
1429 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1430 let maxrows = maxoutlinerows () in
1432 let active = active + incr in
1433 let active = max
0 (min
active (Array.length
outlines - 1)) in
1437 let rows = active - first in
1438 if rows > maxrows then active - maxrows else first
1441 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1442 Glut.postRedisplay
()
1445 | Glut.KEY_UP
-> navigate ~
-1
1446 | Glut.KEY_DOWN
-> navigate 1
1447 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1448 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1451 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1452 Glut.postRedisplay
()
1455 let active = Array.length
outlines - 1 in
1456 let first = max
0 (active - maxrows) in
1457 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1458 Glut.postRedisplay
()
1463 let drawplaceholder l =
1464 GlDraw.color
(scalecolor 1.0);
1466 (0.0, float l.pagedispy
)
1467 (float l.pagew
, float (l.pagedispy
+ l.pagevh
))
1470 and y = float (l.pagedispy
+ 13) in
1471 let font = Glut.BITMAP_8_BY_13
in
1472 GlDraw.color
(0.0, 0.0, 0.0);
1473 GlPix.raster_pos ~
x ~
y ();
1474 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1475 ("Loading " ^ string_of_int
l.pageno);
1478 let now () = Unix.gettimeofday
();;
1481 begin match getopaque l.pageno with
1482 | Some
opaque when validopaque opaque ->
1483 if state.textentry = None
1484 then GlDraw.color
(scalecolor 1.0)
1485 else GlDraw.color
(scalecolor 0.4);
1487 draw
(l.pagedispy
, l.pagew
, l.pagevh
, l.pagey
, conf.hlinks
) opaque;
1490 vlog "draw %f sec" d;
1495 GlDraw.color
(0.5, 0.5, 0.5);
1498 (float (state.w - conf.scrollw
), float (i + (l.pagedispy
- i)))
1500 l.pagedispy
+ l.pagevh
;
1503 let scrollindicator () =
1504 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1505 GlDraw.color
(0.64 , 0.64, 0.64);
1507 (float (state.w - conf.scrollw
), 0.)
1508 (float state.w, float state.h
)
1510 GlDraw.color
(0.0, 0.0, 0.0);
1511 let sh = (float (maxy + state.h
) /. float state.h
) in
1512 let sh = float state.h
/. sh in
1513 let sh = max
sh (float conf.scrollh
) in
1516 if state.y = state.maxy
1518 else float state.y /. float maxy
1520 let position = (float state.h
-. sh) *. percent in
1523 if position +. sh > float state.h
1530 (float (state.w - conf.scrollw
), position)
1531 (float state.w, position +. sh)
1536 match state.mstate
with
1540 | Msel
((x0
, y0
), (x1
, y1
)) ->
1541 let rec loop = function
1543 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1544 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1546 match getopaque l.pageno with
1547 | Some
opaque when validopaque opaque ->
1548 let oy = -l.pagey
+ l.pagedispy
in
1549 seltext
opaque (x0
, y0
, x1
, y1
) oy;
1560 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1561 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1563 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1565 if l.pageno = pageno
1567 let d = float (l.pagedispy
- l.pagey
) in
1568 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1569 GlDraw.begins `quads
;
1571 GlDraw.vertex2
(x0
, y0
+.d);
1572 GlDraw.vertex2
(x1
, y1
+.d);
1573 GlDraw.vertex2
(x2
, y2
+.d);
1574 GlDraw.vertex2
(x3
, y3
+.d);
1584 let showoutline = function
1586 | Some
(allowdel
, active, first, outlines, qsearch) ->
1588 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1589 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1590 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1593 GlDraw.color
(1., 1., 1.);
1594 let font = Glut.BITMAP_9_BY_15
in
1595 let draw_string x y s =
1596 GlPix.raster_pos ~
x ~
y ();
1597 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1600 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1603 let (s, l, _
, _
) = outlines.(row
) in
1604 let y = (row
- first) * 16 in
1609 GlDraw.polygon_mode `both `line
;
1610 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1611 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1612 GlDraw.rect
(0., float (y + 1))
1613 (float (state.w - conf.scrollw
- 1), float (y + 18));
1614 GlDraw.polygon_mode `both `fill
;
1616 GlDraw.color
(1., 1., 1.);
1618 draw_string (float x) (float (y + 16)) s;
1626 let lasty = List.fold_left
drawpage 0 (state.layout) in
1627 GlDraw.color
(scalecolor 0.5);
1630 (float (state.w - conf.scrollw
), float state.h
)
1635 showoutline state.outline;
1637 Glut.swapBuffers
();
1641 let rec f = function
1643 begin match getopaque l.pageno with
1644 | Some
opaque when validopaque opaque ->
1645 let y = y - l.pagedispy
in
1648 let y = l.pagey
+ y in
1649 match whatsunder
opaque x y with
1662 let mouse ~button ~bstate ~
x ~
y =
1664 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1672 let incr = incr * 2 in
1673 let y = clamp incr in
1676 | Glut.LEFT_BUTTON
when state.outline = None
->
1677 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1678 begin match dest with
1679 | Ulinkgoto
(pageno, top
) ->
1682 gotopage1 pageno top
1687 | Unone
when bstate
= Glut.DOWN
->
1688 Glut.setCursor
Glut.CURSOR_INHERIT
;
1689 state.mstate
<- Mnone
1691 | Unone
| Utext _
->
1692 if bstate
= Glut.DOWN
1694 if state.rotate mod 360 = 0 then (
1695 state.mstate
<- Msel
((x, y), (x, y));
1696 Glut.postRedisplay
()
1700 match state.mstate
with
1702 | Msel
((x0
, y0
), (x1
, y1
)) ->
1704 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh
))
1705 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh
)))
1707 match getopaque l.pageno with
1708 | Some
opaque when validopaque opaque ->
1712 List.iter
f state.layout;
1713 copysel
""; (* ugly *)
1714 Glut.setCursor
Glut.CURSOR_INHERIT
;
1715 state.mstate
<- Mnone
;
1722 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1725 if state.outline = None
1727 match state.mstate
with
1730 state.mstate
<- Msel
(a, (x, y));
1731 Glut.postRedisplay
()
1735 if state.outline = None
1737 match state.mstate
with
1739 begin match getunder x y with
1740 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1742 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1743 Glut.setCursor
Glut.CURSOR_INFO
1744 | Ulinkgoto
(page
, y) ->
1745 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1746 Glut.setCursor
Glut.CURSOR_INFO
1748 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1749 Glut.setCursor
Glut.CURSOR_TEXT
1759 if Sys.os_type
= "Win32"
1761 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1763 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1765 Filename.concat
home "llpp"
1769 let ic = open_in_bin
statepath in
1770 let hash = input_value
ic in
1776 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1783 match state.fullscreen
with
1784 | None
-> state.w, state.h
1787 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1788 let oc = open_out_bin
statepath in
1789 output_value
oc pstate
1793 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1798 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1801 state.bookmarks <- statebookmarks;
1802 with Not_found
-> ()
1804 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1807 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1809 if String.length
state.path
= 0
1810 then (prerr_endline
"filename missing"; exit
1)
1815 let _ = Glut.init
Sys.argv
in
1816 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1817 let () = Glut.initWindowSize
state.w state.h
in
1818 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1821 if Sys.os_type
= "Unix"
1823 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1825 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1826 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1827 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1828 Unix.bind
sock addr;
1830 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1831 Unix.connect
csock addr;
1832 let ssock, _ = Unix.accept
sock in
1835 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1836 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1840 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1844 let () = Glut.displayFunc
display in
1845 let () = Glut.reshapeFunc
reshape in
1846 let () = Glut.keyboardFunc
keyboard in
1847 let () = Glut.specialFunc
special in
1848 let () = Glut.idleFunc
(Some
idle) in
1849 let () = Glut.mouseFunc
mouse in
1850 let () = Glut.motionFunc
motion in
1851 let () = Glut.passiveMotionFunc
pmotion in
1854 state.csock <- csock;
1855 state.ssock <- ssock;
1856 state.text <- "Opening " ^
name;
1857 writecmd csock ("open " ^
name ^
"\000");
1861 let rec handlelablglutbug () =
1864 with Glut.BadEnum
"key in special_of_int" ->
1865 showtext '
!'
" LablGlut bug: special key not recognized";
1866 handlelablglutbug ()
1868 handlelablglutbug ();