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
85 { mutable scrollw
: int
86 ; mutable scrollh
: int
87 ; mutable icase
: bool
88 ; mutable preload
: bool
89 ; mutable pagebias
: int
90 ; mutable verbose
: bool
91 ; mutable scrollincr
: int
92 ; mutable maxhfit
: bool
93 ; mutable crophack
: bool
94 ; mutable autoscroll
: bool
95 ; mutable showall
: bool
96 ; mutable hlinks
: bool
97 ; mutable underinfo
: bool
101 type outline
= string * int * int * float;;
103 | Oarray
of outline array
104 | Olist
of outline list
105 | Onarrow
of outline array
* outline array
108 type rect
= (float * float * float * float * float * float * float * float);;
111 { mutable csock
: Unix.file_descr
112 ; mutable ssock
: Unix.file_descr
115 ; mutable rotate
: int
119 ; mutable layout
: layout list
120 ; pagemap
: ((int * int * int), string) Hashtbl.t
121 ; mutable pages
: (int * int * int) list
122 ; mutable pagecount
: int
123 ; pagecache
: string circbuf
124 ; mutable rendering
: bool
125 ; mutable mstate
: mstate
126 ; mutable searchpattern
: string
127 ; mutable rects
: (int * int * rect
) list
128 ; mutable rects1
: (int * int * rect
) list
129 ; mutable text
: string
130 ; mutable fullscreen
: (int * int) option
131 ; mutable textentry
: textentry
option
132 ; mutable outlines
: outlines
133 ; mutable outline
: (bool * int * int * outline array
* string) option
134 ; mutable bookmarks
: outline list
135 ; mutable path
: string
136 ; mutable invalidated
: int
137 ; mutable colorscale
: float
141 { pat
: string circbuf
142 ; pag
: string circbuf
143 ; nav
: float circbuf
174 ; pagemap
= Hashtbl.create
10
175 ; pagecache
= cbnew 10 ""
186 ; outlines
= Olist
[]
192 { nav
= cbnew 100 0.0
203 Printf.kprintf prerr_endline fmt
205 Printf.kprintf ignore fmt
209 let len = String.length s
in
211 let b = Buffer.create
n in
212 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
213 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
214 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
215 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
216 Buffer.add_string
b s
;
217 let s'
= Buffer.contents
b in
218 let n'
= Unix.write fd
s'
0 n in
219 if n'
!= n then failwith
"write failed";
224 let n = Unix.read fd
s 0 4 in
225 if n != 4 then failwith
"incomplete read(len)";
227 lor (Char.code
s.[0] lsl 24)
228 lor (Char.code
s.[1] lsl 16)
229 lor (Char.code
s.[2] lsl 8)
230 lor (Char.code
s.[3] lsl 0)
232 let s = String.create
len in
233 let n = Unix.read fd
s 0 len in
234 if n != len then failwith
"incomplete read(data)";
239 if y
= state.maxy
then 1.0
240 else float y
/. float state.maxy
244 let b = Buffer.create
10 in
245 Buffer.add_string
b s;
246 let rec combine = function
249 Buffer.add_char
b ' '
;
252 | `
b b -> if b then "1" else "0"
254 | `i i
-> string_of_int i
255 | `f f
-> string_of_float f
256 | `I f
-> string_of_int
(truncate f
)
258 Buffer.add_string
b s;
265 let cmd = Buffer.contents
(makecmd s l
) in
266 writecmd state.csock
cmd;
270 let rec f pn ph fh l
=
272 | (n, _
, h
) :: rest
->
273 let fh = fh + (n - pn
) * ph
in
277 let fh = fh + (ph
* (state.pagecount
- pn
)) in
280 let fh = f 0 0 0 state.pages
in
284 let getpageyh pageno
=
285 let rec f pn ph y l
=
287 | (n, _
, h
) :: rest
->
290 y
+ (pageno
- pn
) * ph
, h
292 let y = y + (n - pn
) * ph
in
296 y + (pageno
- pn
) * ph
, ph
301 let getpagey pageno
= fst
(getpageyh pageno
);;
304 let rec f pageno pdimno prev vy py dy l cacheleft accu
=
305 if pageno
= state.pagecount
|| cacheleft
= 0
308 let ((_
, w
, h
) as curr
), rest
, pdimno
=
310 | ((pageno'
, _
, _
) as curr
) :: rest
when pageno'
= pageno
->
311 curr
, rest
, pdimno
+ 1
315 let pageno'
= pageno + 1 in
349 let accu = e :: accu in
350 f pageno' pdimno curr
351 (vy
+ vh) (py + h
) (dy
+ vh + 2) rest
352 (pred cacheleft
) accu
354 f pageno' pdimno curr vy
(py + h
) dy rest cacheleft
accu
356 if state.invalidated
= 0
358 let accu = f 0 ~
-1 (0,0,0) y 0 0 state.pages
(cblen state.pagecache
) [] in
359 state.maxy
<- calcheight ();
366 let y = state.y + incr
in
368 let y = min
y (state.maxy
- (if conf.maxhfit
then state.h
else 0)) in
372 let getopaque pageno =
373 try Some
(Hashtbl.find
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
375 with Not_found
-> None
378 let cache pageno opaque
=
379 Hashtbl.replace
state.pagemap
(pageno + 1, state.w
- conf.scrollw
,
383 let validopaque opaque
= String.length opaque
> 0;;
386 match getopaque l
.pageno with
387 | None
when not
state.rendering
->
388 state.rendering
<- true;
390 wcmd "render" [`i
(l
.pageno + 1)
398 let loadlayout layout =
399 let rec f all
= function
401 begin match getopaque l
.pageno with
402 | None
-> render l
; f false ls
403 | Some opaque
-> f (all
&& validopaque opaque
) ls
407 f (layout <> []) layout;
413 let evictedopaque = cbpeekw state.pagecache
in
414 List.exists
(fun l
->
415 match getopaque l
.pageno with
416 | Some opaque
when validopaque opaque
->
417 evictedopaque = opaque
421 if not
evictedvisible then
422 let y = if state.y < state.h
then 0 else state.y - state.h
in
423 let pages = layout y (state.h
*3) in
424 List.iter
render pages;
429 let y = min
state.maxy
y in
430 let pages = layout y state.h
in
431 let ready = loadlayout pages in
432 state.ty
<- yratio y;
433 if conf.showall
then (
435 state.layout <- pages;
437 Glut.postRedisplay
();
441 state.layout <- pages;
443 Glut.postRedisplay
();
449 cbput state.hists
.nav
(yratio state.y);
450 cbrfollowlen state.hists
.nav
;
454 let y = cbget state.hists
.nav ~
-1 in
455 truncate
(y *. float state.maxy
)
459 let y, h
= getpageyh n in
461 gotoy (y + (truncate
(top
*. float h
)));
464 let gotopage1 n top
=
465 let y = getpagey n in
475 state.invalidated
<- state.invalidated
+ 1;
479 let c = c *. state.colorscale
in
486 GlDraw.viewport
0 0 w h
;
487 GlMat.mode `modelview
;
488 GlMat.load_identity
();
489 GlMat.mode `projection
;
490 GlMat.load_identity
();
491 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
492 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
493 GlMat.scale3
(2.0 /. float w
, 2.0 /. float state.h
, 1.0);
494 GlClear.color
(scalecolor 1.0);
495 GlClear.clear
[`color
];
498 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i h
];
502 GlDraw.color
(0.0, 0.0, 0.0);
504 (0.0, float (state.h
- 18))
505 (float (state.w
- conf.scrollw
- 1), float state.h
)
507 let font = Glut.BITMAP_8_BY_13
in
508 GlDraw.color
(1.0, 1.0, 1.0);
509 GlPix.raster_pos ~x
:0.0 ~
y:(float (state.h
- 5)) ();
510 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
511 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
515 let len = String.length
state.text
in
516 match state.textentry
with
518 if len > 0 then showtext ' '
state.text
520 | Some
(c, text
, _
, _
, _
) ->
524 text ^
" [" ^
state.text ^
"]"
534 state.text
<- Printf.sprintf
"%c%s" c s;
535 Glut.postRedisplay
();
549 state.rects
<- state.rects1
;
550 Glut.postRedisplay
()
553 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
554 state.pagecount
<- n;
555 state.invalidated
<- state.invalidated
- 1;
556 if state.invalidated
= 0
558 let rely = yratio state.y in
559 state.maxy
<- calcheight ();
560 gotoy (truncate
(float state.maxy
*. rely));
564 let s = Scanf.sscanf
cmd "t %n"
565 (fun n -> String.sub
cmd n (String.length
cmd - n))
567 Glut.setWindowTitle
s
570 let s = Scanf.sscanf
cmd "T %n"
571 (fun n -> String.sub
cmd n (String.length
cmd - n))
573 if state.textentry
= None
580 Glut.postRedisplay
();
586 let s = Scanf.sscanf
cmd "V %n"
587 (fun n -> String.sub
cmd n (String.length
cmd - n))
593 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
594 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
595 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
596 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
598 let y = (getpagey pageno) + truncate y0
in
601 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
604 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
605 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
606 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
607 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
610 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
614 Scanf.sscanf
cmd "r %d %d %d %d %s"
615 (fun n w h r p
-> (n, w
, h
, r
, p
))
617 Hashtbl.replace
state.pagemap
(n, w
, r
) p
;
618 let opaque = cbpeekw state.pagecache
in
619 if validopaque opaque
623 (fun k v a
-> if v
= opaque then k else a
)
624 state.pagemap
(-1, -1, -1)
626 wcmd "free" [`
s opaque];
627 Hashtbl.remove
state.pagemap
k
629 cbput state.pagecache p
;
630 state.rendering
<- false;
632 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy
)))
634 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
635 if visible then gotoy state.y
636 else (ignore
(loadlayout state.layout); preload ())
640 let (n, w
, h
) as pagelayout
=
641 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w
, h
)
643 state.pages <- pagelayout
:: state.pages
646 let (l
, n, t
, h
, pos
) =
647 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
649 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
650 let outline = (s, l
, n, float t
/. float h
) in
652 match state.outlines with
653 | Olist
outlines -> Olist
(outline :: outlines)
654 | Oarray _
-> Olist
[outline]
655 | Onarrow _
-> Olist
[outline]
657 state.outlines <- outlines
660 log "unknown cmd `%S'" cmd
663 let now = Unix.gettimeofday
;;
667 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
670 if conf.autoscroll
then begin
671 let y = state.y + conf.scrollincr
in
672 let y = if y >= state.maxy
then 0 else y in
678 let cmd = readcmd state.csock
in
685 let onhist cb
= function
686 | HCprev
-> cbget cb ~
-1
687 | HCnext
-> cbget cb
1
688 | HCfirst
-> cbget cb ~
-(cb
.rc)
689 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
692 let search pattern forward
=
693 if String.length pattern
> 0
696 match state.layout with
699 l
.pageno, (l
.pagey
+ if forward
then 0 else 0*l
.pagevh
)
702 let b = makecmd "search"
703 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
705 Buffer.add_char
b '
,'
;
706 Buffer.add_string
b pattern
;
707 Buffer.add_char
b '
\000'
;
710 writecmd state.csock
cmd;
713 let intentry text key
=
714 let c = Char.unsafe_chr key
in
717 let s = "x" in s.[0] <- c;
718 let text = text ^
s in
722 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
727 let b = Buffer.create
(String.length
s + 1) in
728 Buffer.add_string
b s;
733 let textentry text key
=
734 let c = Char.unsafe_chr key
in
736 | _
when key
>= 32 && key
< 127 ->
737 let text = addchar text c in
741 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
746 state.rotate <- angle
;
748 wcmd "rotate" [`i angle
];
751 let optentry text key
=
752 let btos b = if b then "on" else "off" in
753 let c = Char.unsafe_chr key
in
757 try conf.scrollincr
<- int_of_string
s with exc
->
758 state.text <- Printf.sprintf
"bad integer `%s': %s"
759 s (Printexc.to_string exc
)
761 TEswitch
('#'
, "", None
, intentry, ondone)
766 Some
(int_of_string
s)
768 state.text <- Printf.sprintf
"bad integer `%s': %s"
769 s (Printexc.to_string exc
);
772 | Some angle
-> rotate angle
775 TEswitch
('^'
, "", None
, intentry, ondone)
778 conf.icase
<- not
conf.icase
;
779 TEdone
("case insensitive search " ^
(btos conf.icase
))
782 conf.preload <- not
conf.preload;
784 TEdone
("preload " ^
(btos conf.preload))
787 conf.verbose
<- not
conf.verbose
;
788 TEdone
("verbose " ^
(btos conf.verbose
))
791 conf.maxhfit
<- not
conf.maxhfit
;
792 state.maxy
<- state.maxy
+ (if conf.maxhfit
then -state.h
else state.h
);
793 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
796 conf.crophack
<- not
conf.crophack
;
797 TEdone
("crophack " ^
btos conf.crophack
)
800 conf.showall
<- not
conf.showall
;
801 TEdone
("showall " ^
btos conf.showall
)
804 conf.underinfo
<- not
conf.underinfo
;
805 TEdone
("underinfo " ^
btos conf.underinfo
)
808 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
812 let maxoutlinerows () = (state.h
- 31) / 16;;
814 let enterselector allowdel
outlines errmsg
=
815 if Array.length
outlines = 0
820 Glut.setCursor
Glut.CURSOR_INHERIT
;
822 match state.layout with
824 | {pageno=pageno} :: rest
-> pageno
828 if n = Array.length
outlines
831 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
832 if outlinepageno
>= pageno then n else loop (n+1)
837 Some
(allowdel
, active,
838 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
839 Glut.postRedisplay
();
843 let enteroutlinemode () =
845 match state.outlines with
848 let a = Array.of_list
(List.rev l
) in
849 state.outlines <- Oarray
a;
851 | Onarrow
(a, b) -> a
853 enterselector false outlines "Document has no outline";
856 let enterbookmarkmode () =
857 let bookmarks = Array.of_list
state.bookmarks in
858 enterselector true bookmarks "Document has no bookmarks (yet)";
862 let quickbookmark ?title
() =
863 match state.layout with
869 let sec = Unix.gettimeofday
() in
870 let tm = Unix.localtime
sec in
871 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
875 (tm.Unix.tm_year
+ 1900)
878 | Some
title -> title
881 (title, 0, l
.pageno, float l
.pagey
/. float l
.pageh
) :: state.bookmarks
885 state.fullscreen
<- None
;
886 Glut.reshapeWindow w h
;
892 Hashtbl.clear
state.pagemap
;
894 writecmd state.csock
("open " ^ path ^
"\000");
895 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
896 wcmd "geometry" [`i
(state.w
- conf.scrollw
); `i
state.h
];
899 let viewkeyboard ~key ~x ~
y =
901 state.textentry <- te
;
904 Glut.postRedisplay
()
906 match state.textentry with
908 let c = Char.chr key
in
923 Glut.postRedisplay
()
926 let ondone isforw
s =
927 cbput state.hists
.pat
s;
928 cbrfollowlen state.hists
.pat
;
929 state.searchpattern
<- s;
932 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
933 textentry, ondone (c ='
/'
)))
938 try int_of_string
s with exc
->
939 state.text <- Printf.sprintf
"bad integer `%s': %s"
940 s (Printexc.to_string exc
);
946 state.text <- "page bias is now " ^ string_of_int
n;
949 enttext (Some
('
+'
, "", None
, intentry, ondone))
955 enttext (Some
('
-'
, "", None
, optentry, ondone))
960 try int_of_string
s with exc
->
961 state.text <- Printf.sprintf
"bad integer `%s': %s"
962 s (Printexc.to_string exc
);
968 cbput state.hists
.pag
(string_of_int
n);
969 cbrfollowlen state.hists
.pag
;
970 gotoy (getpagey (n + conf.pagebias
- 1))
973 let pageentry text key
=
974 match Char.unsafe_chr key
with
976 | _
-> intentry text key
978 let text = "x" in text.[0] <- c;
979 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
983 conf.scrollw
<- if conf.scrollw
> 0 then 0 else 5;
984 reshape state.w
state.h
;
987 conf.hlinks
<- not
conf.hlinks
;
988 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
989 Glut.postRedisplay
()
992 conf.autoscroll
<- not
conf.autoscroll
995 begin match state.fullscreen
with
997 state.fullscreen
<- Some
(state.w
, state.h
);
1000 state.fullscreen
<- None
;
1008 search state.searchpattern
true
1011 search state.searchpattern
false
1014 begin match state.layout with
1017 gotoy (state.y - l
.pagey
);
1021 begin match List.rev
state.layout with
1024 gotoy (clamp (l
.pageh
- l
.pagey
))
1028 begin match state.layout with
1031 gotoy (clamp (-l
.pageh
));
1036 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1038 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1040 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1044 else (100. *. (float state.y /. float maxy)) in
1047 Printf.sprintf
"Page %d of %d %.2f%%"
1048 (fn+1) state.pagecount
percent
1051 "Pages %d-%d of %d %.2f%%"
1052 (fn+1) (ln
+1) state.pagecount
percent
1057 begin match state.layout with
1060 doreshape (l
.pagew
+ conf.scrollw
) l
.pageh
;
1061 Glut.postRedisplay
();
1065 enterbookmarkmode ()
1069 match state.layout with
1072 (s, 0, l
.pageno, float l
.pagey
/. float l
.pageh
)
1076 enttext (Some
('~'
, "", None
, textentry, ondone))
1080 showtext ' '
"Quick bookmark added";
1083 begin match state.layout with
1085 let a = getpagewh l
.pagedimno
in
1089 (truncate
(1.8 *. (a.(1) -. a.(0))),
1090 truncate
(1.2 *. (a.(3) -. a.(0))))
1092 (truncate
(a.(1) -. a.(0)),
1093 truncate
(a.(3) -. a.(0)))
1095 doreshape (w + conf.scrollw
) h
;
1096 Glut.postRedisplay
();
1102 rotate (state.rotate + (if c = '
>'
then 30 else -30));
1107 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1108 Glut.postRedisplay
()
1110 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1111 | 'j'
-> gotoy (clamp conf.scrollincr
)
1113 | '
r'
-> opendoc state.path
1116 vlog "huh? %d %c" key
(Char.chr key
);
1119 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1120 let len = String.length
text in
1123 state.textentry <- None
;
1124 Glut.postRedisplay
();
1127 let s = String.sub
text 0 (len - 1) in
1128 enttext (Some
(c, s, onhist, onkey
, ondone))
1131 | Some
(c, text, onhist, onkey
, ondone) ->
1132 begin match Char.unsafe_chr key
with
1135 state.textentry <- None
;
1136 Glut.postRedisplay
()
1139 state.textentry <- None
;
1140 Glut.postRedisplay
()
1143 begin match onkey
text key
with
1145 state.textentry <- None
;
1147 Glut.postRedisplay
()
1150 enttext (Some
(c, text, onhist, onkey
, ondone));
1153 state.textentry <- None
;
1154 Glut.postRedisplay
()
1157 state.textentry <- Some te
;
1158 Glut.postRedisplay
()
1163 let narrow outlines pattern
=
1164 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1168 let rec fold accu n =
1169 if n = -1 then accu else
1170 let (s, _
, _
, _
) as o
= outlines.(n) in
1172 if (try ignore
(Str.search_forward re
s 0); true
1173 with Not_found
-> false)
1179 let matched = fold [] (Array.length
outlines - 1) in
1180 if matched = [] then None
else Some
(Array.of_list
matched)
1183 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1184 let search active pattern incr
=
1187 if n = Array.length
outlines || n = -1 then None
else
1188 let (s, _
, _
, _
) = outlines.(n) in
1190 (try ignore
(Str.search_forward re
s 0); true
1191 with Not_found
-> false)
1193 else loop (n + incr
)
1198 let re = Str.regexp_case_fold pattern
in
1204 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1207 if String.length qsearch
= 0
1210 state.outline <- None
;
1211 Glut.postRedisplay
();
1215 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1216 Glut.postRedisplay
();
1220 let incr = if key
= 18 then -1 else 1 in
1222 match search (active + incr) qsearch
incr with
1224 state.text <- qsearch ^
" [not found]";
1227 state.text <- qsearch
;
1228 active, firstof active
1230 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1231 Glut.postRedisplay
();
1234 let len = String.length qsearch
in
1241 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1244 let qsearch = String.sub
qsearch 0 (len - 1) in
1246 match search active qsearch ~
-1 with
1248 state.text <- qsearch ^
" [not found]";
1251 state.text <- qsearch;
1252 active, firstof active
1254 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1256 Glut.postRedisplay
()
1259 if active < Array.length
outlines
1261 let (_
, _
, n, t
) = outlines.(active) in
1265 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1266 state.outline <- None
;
1267 Glut.postRedisplay
();
1269 | _
when key
>= 32 && key
< 127 ->
1270 let pattern = addchar qsearch (Char.chr key
) in
1272 match search active pattern 1 with
1274 state.text <- pattern ^
" [not found]";
1277 state.text <- pattern;
1278 active, firstof active
1280 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1281 Glut.postRedisplay
()
1283 | 14 when not allowdel
->
1284 let optoutlines = narrow outlines qsearch in
1285 begin match optoutlines with
1286 | None
-> state.text <- "can't narrow"
1288 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1289 match state.outlines with
1291 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1292 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1294 Glut.postRedisplay
()
1296 | 21 when not allowdel
->
1298 match state.outlines with
1301 let a = Array.of_list
(List.rev l
) in
1302 state.outlines <- Oarray
a;
1305 state.outlines <- Oarray
b;
1308 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1309 Glut.postRedisplay
()
1313 Some
(allowdel
, active, firstof active, outlines, qsearch);
1314 Glut.postRedisplay
()
1316 | 127 when allowdel
->
1317 let len = Array.length
outlines - 1 in
1320 state.outline <- None
;
1321 state.bookmarks <- [];
1324 let bookmarks = Array.init
len
1326 let i = if i >= active then i + 1 else i in
1337 Glut.postRedisplay
()
1339 | _
-> log "unknown key %d" key
1342 let keyboard ~key ~x ~
y =
1347 match state.outline with
1348 | None
-> viewkeyboard ~key ~x ~
y
1349 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1352 let special ~key ~x ~
y =
1353 match state.outline with
1355 begin match state.textentry with
1359 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1360 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1361 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1362 | Glut.KEY_PAGE_UP
->
1363 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1365 match state.layout with
1367 | l
:: _
-> state.y - l
.pagey
1370 | Glut.KEY_PAGE_DOWN
->
1371 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1373 match List.rev
state.layout with
1375 | l
:: _
-> getpagey l
.pageno
1378 | Glut.KEY_HOME
-> addnav (); 0
1381 state.maxy - (if conf.maxhfit
then state.h
else 0)
1387 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1390 | Glut.KEY_UP
-> onhist HCprev
1391 | Glut.KEY_DOWN
-> onhist HCnext
1392 | Glut.KEY_HOME
-> onhist HCfirst
1393 | Glut.KEY_END
-> onhist HClast
1396 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1397 Glut.postRedisplay
()
1402 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1403 let maxrows = maxoutlinerows () in
1405 let active = active + incr in
1406 let active = max
0 (min
active (Array.length
outlines - 1)) in
1410 let rows = active - first in
1411 if rows > maxrows then active - maxrows else first
1414 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1415 Glut.postRedisplay
()
1418 | Glut.KEY_UP
-> navigate ~
-1
1419 | Glut.KEY_DOWN
-> navigate 1
1420 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1421 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1424 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1425 Glut.postRedisplay
()
1428 let active = Array.length
outlines - 1 in
1429 let first = max
0 (active - maxrows) in
1430 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1431 Glut.postRedisplay
()
1436 let drawplaceholder l
=
1437 GlDraw.color
(scalecolor 1.0);
1439 (0.0, float l
.pagedispy
)
1440 (float l
.pagew
, float (l
.pagedispy
+ l
.pagevh
))
1443 and y = float (l
.pagedispy
+ 13) in
1444 let font = Glut.BITMAP_8_BY_13
in
1445 GlDraw.color
(0.0, 0.0, 0.0);
1446 GlPix.raster_pos ~
x ~
y ();
1447 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1448 ("Loading " ^ string_of_int l
.pageno);
1451 let now () = Unix.gettimeofday
();;
1454 begin match getopaque l
.pageno with
1455 | Some
opaque when validopaque opaque ->
1456 if state.textentry = None
1457 then GlDraw.color
(scalecolor 1.0)
1458 else GlDraw.color
(scalecolor 0.4);
1460 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh
, l
.pagey
, conf.hlinks
) opaque;
1463 vlog "draw %f sec" d;
1468 GlDraw.color
(0.5, 0.5, 0.5);
1471 (float (state.w - conf.scrollw
), float (i + (l
.pagedispy
- i)))
1473 l
.pagedispy
+ l
.pagevh
;
1476 let scrollindicator () =
1477 let maxy = state.maxy - (if conf.maxhfit
then state.h
else 0) in
1478 GlDraw.color
(0.64 , 0.64, 0.64);
1480 (float (state.w - conf.scrollw
), 0.)
1481 (float state.w, float state.h
)
1483 GlDraw.color
(0.0, 0.0, 0.0);
1484 let sh = (float (maxy + state.h
) /. float state.h
) in
1485 let sh = float state.h
/. sh in
1486 let sh = max
sh (float conf.scrollh
) in
1489 if state.y = state.maxy
1491 else float state.y /. float maxy
1493 let position = (float state.h
-. sh) *. percent in
1496 if position +. sh > float state.h
1503 (float (state.w - conf.scrollw
), position)
1504 (float state.w, position +. sh)
1509 match state.mstate
with
1513 | Msel
((x0
, y0
), (x1
, y1
)) ->
1514 let rec loop = function
1516 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh
))
1517 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh
)))
1519 match getopaque l
.pageno with
1520 | Some
opaque when validopaque opaque ->
1521 let oy = -l
.pagey
+ l
.pagedispy
in
1522 seltext
opaque (x0
, y0
, x1
, y1
) oy;
1533 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1534 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1536 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1538 if l
.pageno = pageno
1540 let d = float (l
.pagedispy
- l
.pagey
) in
1541 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1542 GlDraw.begins `quads
;
1544 GlDraw.vertex2
(x0
, y0
+.d);
1545 GlDraw.vertex2
(x1
, y1
+.d);
1546 GlDraw.vertex2
(x2
, y2
+.d);
1547 GlDraw.vertex2
(x3
, y3
+.d);
1557 let showoutline = function
1559 | Some
(allowdel
, active, first, outlines, qsearch) ->
1561 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1562 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1563 GlDraw.rect
(0., 0.) (float state.w, float state.h
);
1566 GlDraw.color
(1., 1., 1.);
1567 let font = Glut.BITMAP_9_BY_15
in
1568 let draw_string x y s =
1569 GlPix.raster_pos ~
x ~
y ();
1570 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1573 if row
= Array.length
outlines || (row
- first) * 16 > state.h
1576 let (s, l
, _
, _
) = outlines.(row
) in
1577 let y = (row
- first) * 16 in
1582 GlDraw.polygon_mode `both `line
;
1583 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1584 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1585 GlDraw.rect
(0., float (y + 1))
1586 (float (state.w - conf.scrollw
- 1), float (y + 18));
1587 GlDraw.polygon_mode `both `fill
;
1589 GlDraw.color
(1., 1., 1.);
1591 draw_string (float x) (float (y + 16)) s;
1599 let lasty = List.fold_left
drawpage 0 (state.layout) in
1600 GlDraw.color
(scalecolor 0.5);
1603 (float (state.w - conf.scrollw
), float state.h
)
1608 showoutline state.outline;
1610 Glut.swapBuffers
();
1614 let rec f = function
1616 begin match getopaque l
.pageno with
1617 | Some
opaque when validopaque opaque ->
1618 let y = y - l
.pagedispy
in
1621 let y = l
.pagey
+ y in
1622 match whatsunder
opaque x y with
1635 let mouse ~button ~bstate ~
x ~
y =
1637 | Glut.OTHER_BUTTON
n when n == 3 || n == 4 && bstate
= Glut.UP
->
1645 let incr = incr * 2 in
1646 let y = clamp incr in
1649 | Glut.LEFT_BUTTON
when state.outline = None
->
1650 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1651 begin match dest with
1652 | Ulinkgoto
(pageno, top
) ->
1655 gotopage1 pageno top
1660 | Unone
when bstate
= Glut.DOWN
->
1661 Glut.setCursor
Glut.CURSOR_INHERIT
;
1662 state.mstate
<- Mnone
1664 | Unone
| Utext _
->
1665 if bstate
= Glut.DOWN
1667 if state.rotate mod 360 = 0 then (
1668 state.mstate
<- Msel
((x, y), (x, y));
1669 Glut.postRedisplay
()
1673 match state.mstate
with
1675 | Msel
((x0
, y0
), (x1
, y1
)) ->
1677 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh
))
1678 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh
)))
1680 match getopaque l
.pageno with
1681 | Some
opaque when validopaque opaque ->
1685 List.iter
f state.layout;
1686 copysel
""; (* ugly *)
1687 Glut.setCursor
Glut.CURSOR_INHERIT
;
1688 state.mstate
<- Mnone
;
1695 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1698 if state.outline = None
1700 match state.mstate
with
1703 state.mstate
<- Msel
(a, (x, y));
1704 Glut.postRedisplay
()
1708 if state.outline = None
1710 match state.mstate
with
1712 begin match getunder x y with
1713 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
1715 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
1716 Glut.setCursor
Glut.CURSOR_INFO
1717 | Ulinkgoto
(page
, y) ->
1718 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
1719 Glut.setCursor
Glut.CURSOR_INFO
1721 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
1722 Glut.setCursor
Glut.CURSOR_TEXT
1732 if Sys.os_type
= "Win32"
1734 try Sys.getenv
"HOMEPATH" with Not_found
-> ""
1736 try Filename.concat
(Sys.getenv
"HOME") ".config" with Not_found
-> ""
1738 Filename.concat
home "llpp"
1742 let ic = open_in_bin
statepath in
1743 let hash = input_value
ic in
1749 prerr_endline
("Error loading state " ^
Printexc.to_string exn
)
1756 match state.fullscreen
with
1757 | None
-> state.w, state.h
1760 Hashtbl.replace
pstate state.path
(state.bookmarks, w, h
);
1761 let oc = open_out_bin
statepath in
1762 output_value
oc pstate
1766 prerr_endline
("Error saving state " ^
Printexc.to_string exn
)
1771 let statebookmarks, statew
, stateh
= Hashtbl.find
pstate state.path
in
1774 state.bookmarks <- statebookmarks;
1775 with Not_found
-> ()
1777 prerr_endline
("Error setting state " ^
Printexc.to_string exn
)
1780 Arg.parse
[] (fun s -> state.path
<- s) "options:";
1782 if String.length
state.path
= 0
1783 then (prerr_endline
"filename missing"; exit
1)
1788 let _ = Glut.init
Sys.argv
in
1789 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
1790 let () = Glut.initWindowSize
state.w state.h
in
1791 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
1794 if Sys.os_type
= "Unix"
1796 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
1798 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
1799 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1800 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
1801 Unix.bind
sock addr;
1803 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
1804 Unix.connect
csock addr;
1805 let ssock, _ = Unix.accept
sock in
1808 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
1809 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
1813 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
1817 let () = Glut.displayFunc
display in
1818 let () = Glut.reshapeFunc
reshape in
1819 let () = Glut.keyboardFunc
keyboard in
1820 let () = Glut.specialFunc
special in
1821 let () = Glut.idleFunc
(Some
idle) in
1822 let () = Glut.mouseFunc
mouse in
1823 let () = Glut.motionFunc
motion in
1824 let () = Glut.passiveMotionFunc
pmotion in
1827 state.csock <- csock;
1828 state.ssock <- ssock;
1829 state.text <- "Opening " ^
name;
1830 writecmd csock ("open " ^
name ^
"\000");
1834 let rec handlelablglutbug () =
1837 with Glut.BadEnum
"key in special_of_int" ->
1838 showtext '
!'
" LablGlut bug: special key not recognized";
1839 handlelablglutbug ()
1841 handlelablglutbug ();