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 getpdimrect
: int -> float array
= "ml_getpdimrect";;
17 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
21 | Msel
of (mpos
* mpos
)
35 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
36 and onkey
= string -> int -> te
37 and ondone
= string -> unit
38 and onhist
= histcmd
-> string
39 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
44 | TEswitch
of textentry
48 { store
= Array.create n v
55 let cblen b
= Array.length b
.store
;;
60 b
.wc
<- (b
.wc
+ 1) mod len;
61 b
.len <- min
(b
.len + 1) len;
64 let cbpeekw b
= b
.store
.(b
.wc
);;
70 let rc = b
.rc + dir
in
71 let rc = if rc = -1 then b
.len - 1 else rc in
72 let rc = if rc = b
.len then 0 else rc in
83 Array.fill b
.store
0 (Array.length b
.store
) v
;
98 { mutable scrollw
: int
99 ; mutable scrollh
: int
100 ; mutable icase
: bool
101 ; mutable preload
: bool
102 ; mutable pagebias
: int
103 ; mutable verbose
: bool
104 ; mutable scrollincr
: int
105 ; mutable maxhfit
: bool
106 ; mutable crophack
: bool
107 ; mutable autoscroll
: bool
108 ; mutable showall
: bool
109 ; mutable hlinks
: bool
110 ; mutable underinfo
: bool
111 ; mutable interpagespace
: int
112 ; mutable zoom
: float
113 ; mutable presentation
: bool
114 ; mutable angle
: int
117 ; mutable savebmarks
: bool
121 type outline
= string * int * int * float;;
123 | Oarray
of outline array
124 | Olist
of outline list
125 | Onarrow
of outline array
* outline array
128 type rect
= (float * float * float * float * float * float * float * float);;
131 { mutable csock
: Unix.file_descr
132 ; mutable ssock
: Unix.file_descr
138 ; mutable layout
: layout list
139 ; pagemap
: ((int * int * int), string) Hashtbl.t
140 ; mutable pdims
: (int * int * int) list
141 ; mutable pagecount
: int
142 ; pagecache
: string circbuf
143 ; mutable rendering
: bool
144 ; mutable mstate
: mstate
145 ; mutable searchpattern
: string
146 ; mutable rects
: (int * int * rect
) list
147 ; mutable rects1
: (int * int * rect
) list
148 ; mutable text
: string
149 ; mutable fullscreen
: (int * int) option
150 ; mutable textentry
: textentry
option
151 ; mutable outlines
: outlines
152 ; mutable outline
: (bool * int * int * outline array
* string) option
153 ; mutable bookmarks
: outline list
154 ; mutable path
: string
155 ; mutable password
: string
156 ; mutable invalidated
: int
157 ; mutable colorscale
: float
161 { pat
: string circbuf
162 ; pag
: string circbuf
163 ; nav
: float circbuf
183 ; presentation
= false
191 let defconf = { conf with angle
=conf.angle
};;
202 ; pagemap
= Hashtbl.create
10
203 ; pagecache
= cbnew 10 ""
214 ; outlines
= Olist
[]
221 { nav
= cbnew 100 0.0
232 Printf.kprintf prerr_endline fmt
234 Printf.kprintf ignore fmt
238 let len = String.length s
in
240 let b = Buffer.create
n in
241 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
242 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
243 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
244 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
245 Buffer.add_string
b s
;
246 let s'
= Buffer.contents
b in
247 let n'
= Unix.write fd
s'
0 n in
248 if n'
!= n then failwith
"write failed";
253 let n = Unix.read fd
s 0 4 in
254 if n != 4 then failwith
"incomplete read(len)";
256 lor (Char.code
s.[0] lsl 24)
257 lor (Char.code
s.[1] lsl 16)
258 lor (Char.code
s.[2] lsl 8)
259 lor (Char.code
s.[3] lsl 0)
261 let s = String.create
len in
262 let n = Unix.read fd
s 0 len in
263 if n != len then failwith
"incomplete read(data)";
270 else float y
/. float state.maxy
274 let b = Buffer.create
10 in
275 Buffer.add_string
b s;
276 let rec combine = function
279 Buffer.add_char
b ' '
;
282 | `
b b -> if b then "1" else "0"
284 | `i i
-> string_of_int i
285 | `f f
-> string_of_float f
286 | `I f
-> string_of_int
(truncate f
)
288 Buffer.add_string
b s;
295 let cmd = Buffer.contents
(makecmd s l
) in
296 writecmd state.csock
cmd;
302 let d = conf.winh
- h
in
309 let rec f pn ph pi fh l
=
311 | (n, _
, h
) :: rest
->
312 let ips = calcips h
in
318 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
327 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
330 let fh = f 0 0 0 0 state.pdims
in
334 let getpageyh pageno
=
335 let rec f pn ph pi y l
=
337 | (n, _
, h
) :: rest
->
338 let ips = calcips h
in
341 if conf.presentation
&& n = pageno
343 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
345 y
+ (pageno
- pn
) * (ph
+ pi
), h
347 let y = y + (if conf.presentation
then pi
else 0) in
348 let y = y + (n - pn
) * (ph
+ pi
) in
352 y + (pageno
- pn
) * (ph
+ pi
), ph
354 f 0 0 0 0 state.pdims
357 let getpagey pageno
= fst
(getpageyh pageno
);;
360 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
361 let ((w
, h
, ips) as curr
), rest
, pdimno
, yinc
=
363 | (pageno'
, w
, h
) :: rest
when pageno'
= pageno
->
364 let ips = calcips h
in
365 let yinc = if conf.presentation
then ips else 0 in
366 (w
, h
, ips), rest
, pdimno
+ 1, yinc
368 prev
, pdims
, pdimno
, 0
370 let dy = dy + yinc in
371 let py = py + yinc in
372 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
377 if py + h
<= vy - yinc
379 let py = py + h
+ ips in
380 let dy = max
0 (py - y) in
390 let pagey = vy - py in
391 let pagevh = h
- pagey in
392 let pagevh = min
(sh
- dy) pagevh in
393 let off = if yinc > 0 then py - vy else 0
395 let py = py + h
+ ips in
401 ; pagedispy
= dy + off
402 ; pagey = pagey + off
403 ; pagevh = pagevh - off
406 let accu = e :: accu in
413 ~cacheleft
:(cacheleft
-1)
416 if state.invalidated
= 0
426 ~cacheleft
:(cblen state.pagecache
)
436 let y = state.y + incr
in
438 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
442 let getopaque pageno
=
443 try Some
(Hashtbl.find
state.pagemap
(pageno
+ 1, state.w
, conf.angle
))
444 with Not_found
-> None
447 let cache pageno opaque
=
448 Hashtbl.replace
state.pagemap
(pageno
+ 1, state.w
, conf.angle
) opaque
451 let validopaque opaque
= String.length opaque
> 0;;
454 match getopaque l
.pageno
with
455 | None
when not
state.rendering
->
456 state.rendering
<- true;
458 wcmd "render" [`i
(l
.pageno
+ 1)
466 let loadlayout layout =
467 let rec f all
= function
469 begin match getopaque l
.pageno
with
470 | None
-> render l
; f false ls
471 | Some opaque
-> f (all
&& validopaque opaque
) ls
475 f (layout <> []) layout;
482 let evictedopaque = cbpeekw state.pagecache
in
483 List.exists
(fun l
->
484 match getopaque l
.pageno
with
485 | Some opaque
when validopaque opaque
->
486 evictedopaque = opaque
490 if not
evictedvisible
492 let rely = yratio state.y in
493 let presentation = conf.presentation in
494 let interpagespace = conf.interpagespace in
495 let maxy = state.maxy in
496 conf.presentation <- false;
497 conf.interpagespace <- 0;
498 state.maxy <- calcheight ();
499 let y = truncate
(float state.maxy *. rely) in
500 let y = if y < conf.winh
then 0 else y - conf.winh
in
501 let pages = layout y (conf.winh
*3) in
502 List.iter
render pages;
503 conf.presentation <- presentation;
504 conf.interpagespace <- interpagespace;
510 let y = min
state.maxy y in
511 let pages = layout y conf.winh
in
512 let ready = loadlayout pages in
513 state.ty
<- yratio y;
518 state.layout <- pages;
520 Glut.postRedisplay
();
524 state.layout <- pages;
526 Glut.postRedisplay
();
531 let gotoy_and_clear_text y =
533 if not
conf.verbose
then state.text
<- "";
537 cbput state.hists
.nav
(yratio state.y);
538 cbrfollowlen state.hists
.nav
;
542 let y = cbget state.hists
.nav ~
-1 in
543 truncate
(y *. float state.maxy)
547 let y, h
= getpageyh n in
549 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
552 let gotopage1 n top
=
553 let y = getpagey n in
555 gotoy_and_clear_text (y + top
);
563 state.invalidated
<- state.invalidated
+ 1;
567 let c = c *. state.colorscale
in
573 match state.layout with
575 let rely = yratio state.y in
576 state.maxy <- calcheight ();
577 truncate
(float state.maxy *. rely)
580 state.maxy <- calcheight ();
587 GlMat.mode `projection
;
588 GlMat.load_identity
();
589 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
590 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
591 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
595 GlMat.mode `projection
;
596 GlMat.load_identity
();
597 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
598 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
599 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
604 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
607 GlMat.mode `modelview
;
608 GlMat.load_identity
();
609 GlClear.color
(scalecolor 1.0);
610 GlClear.clear
[`color
];
613 wcmd "geometry" [`i
w; `i h
];
617 GlDraw.color
(0.0, 0.0, 0.0);
619 (0.0, float (conf.winh
- 18))
620 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
622 let font = Glut.BITMAP_8_BY_13
in
623 GlDraw.color
(1.0, 1.0, 1.0);
624 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
625 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
626 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
630 let len = String.length
state.text
in
631 match state.textentry
with
633 if len > 0 then showtext ' '
state.text
635 | Some
(c, text
, _
, _
, _
) ->
639 text ^
" [" ^
state.text ^
"]"
649 state.text
<- Printf.sprintf
"%c%s" c s;
650 Glut.postRedisplay
();
664 state.rects
<- state.rects1
;
665 Glut.postRedisplay
()
668 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
669 state.pagecount
<- n;
670 state.invalidated
<- state.invalidated
- 1;
671 if state.invalidated
= 0
675 let s = Scanf.sscanf
cmd "t %n"
676 (fun n -> String.sub
cmd n (String.length
cmd - n))
678 Glut.setWindowTitle
s
681 let s = Scanf.sscanf
cmd "T %n"
682 (fun n -> String.sub
cmd n (String.length
cmd - n))
684 if state.textentry
= None
691 Glut.postRedisplay
();
697 let s = Scanf.sscanf
cmd "V %n"
698 (fun n -> String.sub
cmd n (String.length
cmd - n))
704 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
705 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
706 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
707 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
709 let y = (getpagey pageno) + truncate y0
in
712 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
715 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
716 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
717 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
718 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
721 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
725 Scanf.sscanf
cmd "r %d %d %d %d %s"
726 (fun n w h r p
-> (n, w, h
, r
, p
))
728 Hashtbl.replace
state.pagemap
(n, w, r
) p
;
729 let opaque = cbpeekw state.pagecache
in
730 if validopaque opaque
734 (fun k v a
-> if v
= opaque then k else a
)
735 state.pagemap
(-1, -1, -1)
737 wcmd "free" [`
s opaque];
738 Hashtbl.remove
state.pagemap
k
740 cbput state.pagecache p
;
741 state.rendering
<- false;
743 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
745 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
748 else (ignore
(loadlayout state.layout); preload ())
752 let (n, w, h
) as pdim
=
753 Scanf.sscanf
cmd "l %d %d %d" (fun n w h
-> n, w, h
)
755 state.pdims
<- pdim
:: state.pdims
758 let (l
, n, t
, h
, pos
) =
759 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
761 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
763 let l = String.length
s in
764 let b = Buffer.create
(String.length
s) in
771 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
774 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
783 let outline = (s, l, n, float t
/. float h
) in
785 match state.outlines with
786 | Olist
outlines -> Olist
(outline :: outlines)
787 | Oarray _
-> Olist
[outline]
788 | Onarrow _
-> Olist
[outline]
790 state.outlines <- outlines
793 log "unknown cmd `%S'" cmd
796 let now = Unix.gettimeofday
;;
800 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
805 let y = state.y + conf.scrollincr
in
806 let y = if y >= state.maxy then 0 else y in
812 let cmd = readcmd state.csock
in
819 let onhist cb
= function
820 | HCprev
-> cbget cb ~
-1
821 | HCnext
-> cbget cb
1
822 | HCfirst
-> cbget cb ~
-(cb
.rc)
823 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
826 let search pattern forward
=
827 if String.length pattern
> 0
830 match state.layout with
833 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
836 let b = makecmd "search"
837 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
839 Buffer.add_char
b '
,'
;
840 Buffer.add_string
b pattern
;
841 Buffer.add_char
b '
\000'
;
844 writecmd state.csock
cmd;
847 let intentry text key
=
848 let c = Char.unsafe_chr key
in
851 let s = "x" in s.[0] <- c;
852 let text = text ^
s in
856 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
861 let b = Buffer.create
(String.length
s + 1) in
862 Buffer.add_string
b s;
867 let textentry text key
=
868 let c = Char.unsafe_chr key
in
870 | _
when key
>= 32 && key
< 127 ->
871 let text = addchar text c in
875 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
882 wcmd "rotate" [`i angle
];
885 let optentry text key
=
886 let btos b = if b then "on" else "off" in
887 let c = Char.unsafe_chr key
in
891 try conf.scrollincr
<- int_of_string
s with exc
->
892 state.text <- Printf.sprintf
"bad integer `%s': %s"
893 s (Printexc.to_string exc
)
895 TEswitch
('#'
, "", None
, intentry, ondone)
900 Some
(int_of_string
s)
902 state.text <- Printf.sprintf
"bad integer `%s': %s"
903 s (Printexc.to_string exc
);
906 | Some angle
-> rotate angle
909 TEswitch
('^'
, "", None
, intentry, ondone)
912 conf.icase
<- not
conf.icase
;
913 TEdone
("case insensitive search " ^
(btos conf.icase
))
916 conf.preload <- not
conf.preload;
918 TEdone
("preload " ^
(btos conf.preload))
921 conf.verbose
<- not
conf.verbose
;
922 TEdone
("verbose " ^
(btos conf.verbose
))
925 conf.maxhfit
<- not
conf.maxhfit
;
926 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
927 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
930 conf.crophack
<- not
conf.crophack
;
931 TEdone
("crophack " ^
btos conf.crophack
)
934 conf.showall
<- not
conf.showall
;
935 TEdone
("showall " ^
btos conf.showall
)
938 conf.underinfo
<- not
conf.underinfo
;
939 TEdone
("underinfo " ^
btos conf.underinfo
)
942 conf.savebmarks
<- not
conf.savebmarks
;
943 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
948 conf.interpagespace <- int_of_string
s;
949 let rely = yratio state.y in
950 state.maxy <- calcheight ();
951 gotoy (truncate
(float state.maxy *. rely));
953 state.text <- Printf.sprintf
"bad integer `%s': %s"
954 s (Printexc.to_string exc
)
956 TEswitch
('
%'
, "", None
, intentry, ondone)
959 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
963 let maxoutlinerows () = (conf.winh
- 31) / 16;;
965 let enterselector allowdel
outlines errmsg
=
966 if Array.length
outlines = 0
972 Glut.setCursor
Glut.CURSOR_INHERIT
;
974 match state.layout with
976 | {pageno=pageno} :: rest
-> pageno
980 if n = Array.length
outlines
983 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
984 if outlinepageno
>= pageno then n else loop (n+1)
989 Some
(allowdel
, active,
990 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
991 Glut.postRedisplay
();
995 let enteroutlinemode () =
997 match state.outlines with
1000 let a = Array.of_list
(List.rev
l) in
1001 state.outlines <- Oarray
a;
1003 | Onarrow
(a, b) -> a
1005 enterselector false outlines "Document has no outline";
1008 let enterbookmarkmode () =
1009 let bookmarks = Array.of_list
state.bookmarks in
1010 enterselector true bookmarks "Document has no bookmarks (yet)";
1013 let quickbookmark ?title
() =
1014 match state.layout with
1020 let sec = Unix.gettimeofday
() in
1021 let tm = Unix.localtime
sec in
1022 Printf.sprintf
"Quick %d visited (%d/%d/%d %d:%d)"
1026 (tm.Unix.tm_year
+ 1900)
1029 | Some
title -> title
1032 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1036 state.fullscreen
<- None
;
1037 Glut.reshapeWindow
w h
;
1040 let opendoc path password
=
1043 state.password
<- password
;
1044 Hashtbl.clear
state.pagemap
;
1046 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1047 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1048 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1051 let viewkeyboard ~key ~x ~
y =
1053 state.textentry <- te
;
1056 Glut.postRedisplay
()
1058 match state.textentry with
1060 let c = Char.chr key
in
1066 let y = getnav () in
1067 gotoy_and_clear_text y
1075 Glut.postRedisplay
()
1078 let ondone isforw
s =
1079 cbput state.hists
.pat
s;
1080 cbrfollowlen state.hists
.pat
;
1081 state.searchpattern
<- s;
1084 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1085 textentry, ondone (c ='
/'
)))
1087 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1088 conf.zoom
<- min
2.2 (conf.zoom
+. 0.1);
1089 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1090 reshape conf.winw
conf.winh
1095 try int_of_string
s with exc
->
1096 state.text <- Printf.sprintf
"bad integer `%s': %s"
1097 s (Printexc.to_string exc
);
1103 state.text <- "page bias is now " ^ string_of_int
n;
1106 enttext (Some
('
+'
, "", None
, intentry, ondone))
1108 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1109 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1110 if conf.zoom
<= 1.0 then state.x
<- 0;
1111 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1112 reshape conf.winw
conf.winh
;
1118 enttext (Some
('
-'
, "", None
, optentry, ondone))
1120 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1123 state.text <- "zoom is 100%";
1124 reshape conf.winw
conf.winh
1126 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1128 let rec find n maxh nformaxh
= function
1129 | (_
, _
, h
) :: rest
->
1131 then find (n+1) h
n rest
1132 else find (n+1) maxh nformaxh rest
1135 find 0 0 0 state.pdims
1138 let rect = getpdimrect
n in
1139 let pw = rect.(1) -. rect.(0) in
1140 let ph = rect.(3) -. rect.(2) in
1142 let num = (float conf.winh
*. pw) +. (ph *. float conf.scrollw
) in
1143 let den = ph *. float conf.winw
in
1144 let zoom = num /. den in
1150 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1151 reshape conf.winw
conf.winh
;
1157 try int_of_string
s with exc
->
1158 state.text <- Printf.sprintf
"bad integer `%s': %s"
1159 s (Printexc.to_string exc
);
1165 cbput state.hists
.pag
(string_of_int
n);
1166 cbrfollowlen state.hists
.pag
;
1167 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1170 let pageentry text key
=
1171 match Char.unsafe_chr key
with
1172 | 'g'
-> TEdone
text
1173 | _
-> intentry text key
1175 let text = "x" in text.[0] <- c;
1176 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1180 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1181 reshape conf.winw
conf.winh
;
1184 conf.hlinks
<- not
conf.hlinks
;
1185 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1186 Glut.postRedisplay
()
1189 conf.autoscroll
<- not
conf.autoscroll
1192 conf.presentation <- not
conf.presentation;
1193 showtext ' '
("presentation mode " ^
1194 if conf.presentation then "on" else "off");
1198 begin match state.fullscreen
with
1200 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1203 state.fullscreen
<- None
;
1208 gotoy_and_clear_text 0
1211 search state.searchpattern
true
1214 search state.searchpattern
false
1217 begin match state.layout with
1220 gotoy_and_clear_text (getpagey l.pageno)
1224 begin match List.rev
state.layout with
1227 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1228 gotoy_and_clear_text (getpagey pageno)
1232 begin match state.layout with
1235 let pageno = max
0 (l.pageno-1) in
1236 gotoy_and_clear_text (getpagey pageno)
1241 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1243 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1245 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1249 else (100. *. (float state.y /. float maxy)) in
1252 Printf.sprintf
"Page %d of %d %.2f%%"
1253 (fn+1) state.pagecount
percent
1256 "Pages %d-%d of %d %.2f%%"
1257 (fn+1) (ln
+1) state.pagecount
percent
1262 begin match state.layout with
1265 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1266 Glut.postRedisplay
();
1270 enterbookmarkmode ()
1274 match state.layout with
1277 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1281 enttext (Some
('~'
, "", None
, textentry, ondone))
1285 showtext ' '
"Quick bookmark added";
1288 begin match state.layout with
1290 let rect = getpdimrect
l.pagedimno
in
1294 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1295 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1297 (truncate
(rect.(1) -. rect.(0)),
1298 truncate
(rect.(3) -. rect.(0)))
1300 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace);
1301 Glut.postRedisplay
();
1307 rotate (conf.angle
+ (if c = '
>'
then 30 else -30));
1312 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1313 Glut.postRedisplay
()
1315 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1316 | 'j'
-> gotoy (clamp conf.scrollincr
)
1318 | '
r'
-> opendoc state.path
state.password
1321 vlog "huh? %d %c" key
(Char.chr key
);
1324 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1325 let len = String.length
text in
1328 state.textentry <- None
;
1329 Glut.postRedisplay
();
1332 let s = String.sub
text 0 (len - 1) in
1333 enttext (Some
(c, s, onhist, onkey
, ondone))
1336 | Some
(c, text, onhist, onkey
, ondone) ->
1337 begin match Char.unsafe_chr key
with
1340 state.textentry <- None
;
1341 Glut.postRedisplay
()
1344 state.textentry <- None
;
1345 Glut.postRedisplay
()
1348 begin match onkey
text key
with
1350 state.textentry <- None
;
1352 Glut.postRedisplay
()
1355 enttext (Some
(c, text, onhist, onkey
, ondone));
1358 state.textentry <- None
;
1359 Glut.postRedisplay
()
1362 state.textentry <- Some te
;
1363 Glut.postRedisplay
()
1368 let narrow outlines pattern
=
1369 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1373 let rec fold accu n =
1377 let (s, _
, _
, _
) as o
= outlines.(n) in
1379 if (try ignore
(Str.search_forward re
s 0); true
1380 with Not_found
-> false)
1386 let matched = fold [] (Array.length
outlines - 1) in
1387 if matched = [] then None
else Some
(Array.of_list
matched)
1390 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1391 let search active pattern incr
=
1394 if n = Array.length
outlines || n = -1
1397 let (s, _
, _
, _
) = outlines.(n) in
1399 (try ignore
(Str.search_forward re
s 0); true
1400 with Not_found
-> false)
1402 else loop (n + incr
)
1407 let re = Str.regexp_case_fold pattern
in
1413 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1416 if String.length qsearch
= 0
1419 state.outline <- None
;
1420 Glut.postRedisplay
();
1424 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1425 Glut.postRedisplay
();
1429 let incr = if key
= 18 then -1 else 1 in
1431 match search (active + incr) qsearch
incr with
1433 state.text <- qsearch ^
" [not found]";
1436 state.text <- qsearch
;
1437 active, firstof active
1439 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1440 Glut.postRedisplay
();
1443 let len = String.length qsearch
in
1450 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1453 let qsearch = String.sub
qsearch 0 (len - 1) in
1455 match search active qsearch ~
-1 with
1457 state.text <- qsearch ^
" [not found]";
1460 state.text <- qsearch;
1461 active, firstof active
1463 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1465 Glut.postRedisplay
()
1468 if active < Array.length
outlines
1470 let (_
, _
, n, t
) = outlines.(active) in
1474 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1475 state.outline <- None
;
1476 Glut.postRedisplay
();
1478 | _
when key
>= 32 && key
< 127 ->
1479 let pattern = addchar qsearch (Char.chr key
) in
1481 match search active pattern 1 with
1483 state.text <- pattern ^
" [not found]";
1486 state.text <- pattern;
1487 active, firstof active
1489 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1490 Glut.postRedisplay
()
1492 | 14 when not allowdel
->
1493 let optoutlines = narrow outlines qsearch in
1494 begin match optoutlines with
1495 | None
-> state.text <- "can't narrow"
1497 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1498 match state.outlines with
1500 | Oarray
a -> state.outlines <- Onarrow
(outlines, a)
1501 | Onarrow
(a, b) -> state.outlines <- Onarrow
(outlines, b)
1503 Glut.postRedisplay
()
1505 | 21 when not allowdel
->
1507 match state.outlines with
1510 let a = Array.of_list
(List.rev
l) in
1511 state.outlines <- Oarray
a;
1514 state.outlines <- Oarray
b;
1517 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1518 Glut.postRedisplay
()
1522 Some
(allowdel
, active, firstof active, outlines, qsearch);
1523 Glut.postRedisplay
()
1525 | 127 when allowdel
->
1526 let len = Array.length
outlines - 1 in
1529 state.outline <- None
;
1530 state.bookmarks <- [];
1533 let bookmarks = Array.init
len
1535 let i = if i >= active then i + 1 else i in
1546 Glut.postRedisplay
()
1548 | _
-> log "unknown key %d" key
1551 let keyboard ~key ~x ~
y =
1556 match state.outline with
1557 | None
-> viewkeyboard ~key ~x ~
y
1558 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1561 let special ~key ~x ~
y =
1562 match state.outline with
1564 begin match state.textentry with
1568 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1569 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1570 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1571 | Glut.KEY_PAGE_UP
->
1572 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1574 match state.layout with
1576 | l :: _
-> state.y - l.pagey
1579 | Glut.KEY_PAGE_DOWN
->
1580 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1582 match List.rev
state.layout with
1584 | l :: _
-> getpagey l.pageno
1587 | Glut.KEY_HOME
-> addnav (); 0
1590 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1592 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1593 state.x
<- state.x
- 10;
1595 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1596 state.x
<- state.x
+ 10;
1601 gotoy_and_clear_text y
1603 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1606 | Glut.KEY_UP
-> onhist HCprev
1607 | Glut.KEY_DOWN
-> onhist HCnext
1608 | Glut.KEY_HOME
-> onhist HCfirst
1609 | Glut.KEY_END
-> onhist HClast
1612 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1613 Glut.postRedisplay
()
1618 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1619 let maxrows = maxoutlinerows () in
1621 let active = active + incr in
1622 let active = max
0 (min
active (Array.length
outlines - 1)) in
1626 let rows = active - first in
1627 if rows > maxrows then active - maxrows else first
1630 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1631 Glut.postRedisplay
()
1634 | Glut.KEY_UP
-> navigate ~
-1
1635 | Glut.KEY_DOWN
-> navigate 1
1636 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1637 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1640 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1641 Glut.postRedisplay
()
1644 let active = Array.length
outlines - 1 in
1645 let first = max
0 (active - maxrows) in
1646 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1647 Glut.postRedisplay
()
1652 let drawplaceholder l =
1653 GlDraw.color
(scalecolor 1.0);
1655 (0.0, float l.pagedispy
)
1656 (float l.pagew
, float (l.pagedispy
+ l.pagevh))
1659 and y = float (l.pagedispy
+ 13) in
1660 let font = Glut.BITMAP_8_BY_13
in
1661 GlDraw.color
(0.0, 0.0, 0.0);
1662 GlPix.raster_pos ~
x ~
y ();
1663 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1664 ("Loading " ^ string_of_int
(l.pageno + 1));
1667 let now () = Unix.gettimeofday
();;
1670 begin match getopaque l.pageno with
1671 | Some
opaque when validopaque opaque ->
1672 if state.textentry = None
1673 then GlDraw.color
(scalecolor 1.0)
1674 else GlDraw.color
(scalecolor 0.4);
1676 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1680 vlog "draw %d %f sec" l.pageno d;
1685 l.pagedispy
+ l.pagevh;
1689 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1690 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1691 let sh = float conf.winh
/. sh in
1692 let sh = max
sh (float conf.scrollh
) in
1695 if state.y = state.maxy
1697 else float y /. float maxy
1699 let position = (float conf.winh
-. sh) *. percent in
1702 if position +. sh > float conf.winh
1703 then float conf.winh
-. sh
1709 let scrollindicator () =
1710 GlDraw.color
(0.64 , 0.64, 0.64);
1712 (float (conf.winw
- conf.scrollw
), 0.)
1713 (float conf.winw
, float conf.winh
)
1715 GlDraw.color
(0.0, 0.0, 0.0);
1717 let position, sh = scrollph state.y in
1719 (float (conf.winw
- conf.scrollw
), position)
1720 (float conf.winw
, position +. sh)
1724 let showsel margin
=
1725 match state.mstate
with
1726 | Mnone
| Mscroll _
| Mpan _
->
1729 | Msel
((x0
, y0
), (x1
, y1
)) ->
1730 let rec loop = function
1732 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1733 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1735 match getopaque l.pageno with
1736 | Some
opaque when validopaque opaque ->
1737 let oy = -l.pagey + l.pagedispy
in
1739 (x0
- margin
- state.x, y0
,
1740 x1
- margin
- state.x, y1
) oy;
1750 let panx = float state.x in
1752 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1753 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1755 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1757 if l.pageno = pageno
1759 let d = float (l.pagedispy
- l.pagey) in
1760 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1761 GlDraw.begins `quads
;
1763 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1764 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1765 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1766 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1776 let showoutline = function
1778 | Some
(allowdel
, active, first, outlines, qsearch) ->
1780 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1781 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1782 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
1785 GlDraw.color
(1., 1., 1.);
1786 let font = Glut.BITMAP_9_BY_15
in
1787 let draw_string x y s =
1788 GlPix.raster_pos ~
x ~
y ();
1789 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1792 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
1795 let (s, l, _
, _
) = outlines.(row
) in
1796 let y = (row
- first) * 16 in
1801 GlDraw.polygon_mode `both `line
;
1802 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1803 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1804 GlDraw.rect (0., float (y + 1))
1805 (float (conf.winw
- 1), float (y + 18));
1806 GlDraw.polygon_mode `both `fill
;
1808 GlDraw.color
(1., 1., 1.);
1810 draw_string (float x) (float (y + 16)) s;
1818 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1819 GlDraw.viewport
margin 0 state.w conf.winh
;
1821 GlClear.color
(scalecolor 0.5);
1822 GlClear.clear
[`color
];
1825 let x = float state.x in
1826 GlMat.translate ~
x ();
1830 Gl.enable `scissor_test
;
1831 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
1833 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1836 Gl.disable `scissor_test
1840 let x = -.float state.x in
1841 GlMat.translate ~
x ();
1845 GlDraw.viewport
0 0 conf.winw
conf.winh
;
1848 showoutline state.outline;
1850 Glut.swapBuffers
();
1854 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1855 let x = x - margin - state.x in
1856 let rec f = function
1858 begin match getopaque l.pageno with
1859 | Some
opaque when validopaque opaque ->
1860 let y = y - l.pagedispy
in
1863 let y = l.pagey + y in
1864 match whatsunder
opaque x y with
1877 let mouse ~button ~bstate ~
x ~
y =
1879 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1887 let incr = incr * 2 in
1888 let y = clamp incr in
1889 gotoy_and_clear_text y
1891 | Glut.LEFT_BUTTON
when state.outline = None
1892 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1893 if bstate
= Glut.DOWN
1895 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1896 state.mstate
<- Mpan
(x, y)
1899 state.mstate
<- Mnone
1902 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
1903 if bstate
= Glut.DOWN
1905 let position, sh = scrollph state.y in
1906 if y > truncate
position && y < truncate
(position +. sh)
1908 state.mstate
<- Mscroll
1910 let percent = float y /. float conf.winh
in
1911 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
1913 state.mstate
<- Mscroll
1915 state.mstate
<- Mnone
1917 | Glut.LEFT_BUTTON
when state.outline = None
->
1918 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1919 begin match dest with
1920 | Ulinkgoto
(pageno, top
) ->
1923 gotopage1 pageno top
1928 | Unone
when bstate
= Glut.DOWN
->
1929 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1930 state.mstate
<- Mpan
(x, y);
1932 | Unone
| Utext _
->
1933 if bstate
= Glut.DOWN
1935 if conf.angle
mod 360 = 0
1937 state.mstate
<- Msel
((x, y), (x, y));
1938 Glut.postRedisplay
()
1942 match state.mstate
with
1946 state.mstate
<- Mnone
1949 Glut.setCursor
Glut.CURSOR_INHERIT
;
1950 state.mstate
<- Mnone
1952 | Msel
((x0
, y0
), (x1
, y1
)) ->
1954 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1955 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1957 match getopaque l.pageno with
1958 | Some
opaque when validopaque opaque ->
1962 List.iter
f state.layout;
1963 copysel
""; (* ugly *)
1964 Glut.setCursor
Glut.CURSOR_INHERIT
;
1965 state.mstate
<- Mnone
;
1972 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1975 if state.outline = None
1977 match state.mstate
with
1983 state.mstate
<- Mpan
(x, y);
1984 if conf.zoom > 1.0 then state.x <- state.x + dx;
1986 gotoy_and_clear_text y
1989 state.mstate
<- Msel
(a, (x, y));
1990 Glut.postRedisplay
()
1993 let y = min
conf.winh
(max
0 y) in
1994 let percent = float y /. float conf.winh
in
1995 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
1996 gotoy_and_clear_text y
2000 if state.outline = None
2002 match state.mstate
with
2004 begin match getunder x y with
2005 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2007 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2008 Glut.setCursor
Glut.CURSOR_INFO
2009 | Ulinkgoto
(page
, y) ->
2010 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2011 Glut.setCursor
Glut.CURSOR_INFO
2013 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2014 Glut.setCursor
Glut.CURSOR_TEXT
2017 | Mpan _
| Msel _
| Mscroll
->
2027 match Sys.os_type
with
2028 | "Win32" -> Sys.getenv
"HOMEPATH"
2029 | _
-> Sys.getenv
"HOME"
2032 ("Can not determine home directory location: " ^
2033 Printexc.to_string exn
);
2037 let config_of c attrs
=
2041 | "scroll-bar-width" -> { c with scrollw
= int_of_string v
}
2042 | "scroll-handle-height" -> { c with scrollh
= int_of_string v
}
2043 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2044 | "preload" -> { c with preload = bool_of_string v
}
2045 | "page-bias" -> { c with pagebias
= int_of_string v
}
2046 | "scroll-step" -> { c with scrollincr
= int_of_string v
}
2047 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2048 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2049 | "throttle" -> { c with showall
= bool_of_string v
}
2050 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2051 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2052 | "vertical-margin" -> { c with interpagespace = int_of_string v
}
2053 | "zoom" -> { c with zoom = float_of_string v
/. 100. }
2054 | "presentation" -> { c with presentation = bool_of_string v
}
2055 | "rotation-angle" -> { c with angle
= int_of_string v
}
2056 | "width" -> { c with winw
= int_of_string v
}
2057 | "height" -> { c with winh
= int_of_string v
}
2058 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2061 prerr_endline
("Error processing attribute (`" ^
2062 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2065 let rec fold c = function
2068 let c = apply c k v
in
2074 let bookmark_of attrs
=
2075 let rec fold title page
rely = function
2076 | ("title", v
) :: rest
-> fold v page
rely rest
2077 | ("page", v
) :: rest
-> fold title v
rely rest
2078 | ("rely", v
) :: rest
-> fold title page v rest
2079 | _
:: rest
-> fold title page
rely rest
2080 | [] -> title, page
, rely
2082 fold "invalid" "0" "0" attrs
2085 let setconf dst src
=
2086 dst
.scrollw
<- src
.scrollw
;
2087 dst
.scrollh
<- src
.scrollh
;
2088 dst
.icase
<- src
.icase
;
2089 dst
.preload <- src
.preload;
2090 dst
.pagebias
<- src
.pagebias
;
2091 dst
.verbose
<- src
.verbose
;
2092 dst
.scrollincr
<- src
.scrollincr
;
2093 dst
.maxhfit
<- src
.maxhfit
;
2094 dst
.crophack
<- src
.crophack
;
2095 dst
.autoscroll
<- src
.autoscroll
;
2096 dst
.showall
<- src
.showall
;
2097 dst
.hlinks
<- src
.hlinks
;
2098 dst
.underinfo
<- src
.underinfo
;
2099 dst
.interpagespace <- src
.interpagespace;
2100 dst
.zoom <- src
.zoom;
2101 dst
.presentation <- src
.presentation;
2102 dst
.angle
<- src
.angle
;
2103 dst
.winw
<- src
.winw
;
2104 dst
.winh
<- src
.winh
;
2105 dst
.savebmarks
<- src
.savebmarks
;
2109 let h = Hashtbl.create
10 in
2110 let dc = { defconf with angle
= defconf.angle
} in
2111 let rec toplevel v t spos epos
=
2113 | Vdata
| Vcdata
| Vend
-> v
2114 | Vopen
("llppconfig", attrs
, closed
) ->
2117 else { v
with f = llppconfig
}
2119 error
"unexpected subelement at top level" s spos
2120 | Vclose tag
-> error
"unexpected close at toplevel" s spos
2122 and llppconfig v t spos epos
=
2124 | Vdata
| Vcdata
| Vend
-> v
2125 | Vopen
("defaults", attrs
, closed
) ->
2126 let c = config_of dc attrs
in
2130 else { v
with f = skip
"defaults" (fun () -> v
) }
2132 | Vopen
("doc", attrs
, closed
) ->
2135 List.assoc
"path" attrs
2136 with Not_found
-> error
"doc is missing path attribute" s spos
2139 let l = String.length
pathent in
2140 let b = Buffer.create
l in
2141 unent
b pathent 0 l;
2144 let c = config_of dc attrs
in
2147 float_of_string
(List.assoc
"rely" attrs
)
2151 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2156 int_of_string
(List.assoc
"pan" attrs
)
2160 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2164 then (Hashtbl.add
h path (c, [], x, y); v
)
2165 else { v
with f = doc
path x y c [] }
2167 | Vopen
(tag
, _
, closed
) ->
2168 error
"unexpected subelement in llppconfig" s spos
2170 | Vclose
"llppconfig" -> { v
with f = toplevel }
2171 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2173 and doc
path x y c bookmarks v t spos epos
=
2175 | Vdata
| Vcdata
-> v
2176 | Vend
-> error
"unexpected end of input in doc" s spos
2177 | Vopen
("bookmarks", attrs
, closed
) ->
2178 { v
with f = pbookmarks
path x y c [] }
2180 | Vopen
(tag
, _
, _
) ->
2181 error
"unexpected subelement in doc" s spos
2184 Hashtbl.add
h path (c, bookmarks, x, y);
2185 { v
with f = llppconfig
}
2187 | Vclose tag
-> error
"unexpected close in doc" s spos
2189 and pbookmarks
path x y c bookmarks v t spos epos
=
2191 | Vdata
| Vcdata
-> v
2192 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2193 | Vopen
("item", attrs
, closed
) ->
2194 let title, spage
, srely
= bookmark_of attrs
in
2199 dolog "Failed to convert page %S to integer: %s"
2200 spage
(Printexc.to_string exn
);
2205 float_of_string srely
2207 dolog "Failed to convert rely %S to real: %s"
2208 srely
(Printexc.to_string exn
);
2211 let bookmarks = (title, 0, page, rely) :: bookmarks in
2213 then { v
with f = pbookmarks
path x y c bookmarks }
2216 { v
with f = skip
"item" f }
2219 error
"unexpected subelement in bookmarks" s spos
2221 | Vclose
"bookmarks" ->
2222 { v
with f = doc
path x y c bookmarks }
2224 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2226 and skip tag
f v t spos epos
=
2228 | Vdata
| Vcdata
-> v
2230 error
("unexpected end of input in skipped " ^ tag
) s spos
2231 | Vopen
(tag'
, _
, closed
) ->
2235 let f'
() = { v
with f = skip tag
f } in
2236 { v
with f = skip tag'
f'
}
2240 else error
("unexpected close in skipped " ^ tag
) s spos
2243 parse
{ f = toplevel; accu = () } s;
2249 let len = in_channel_length ic
in
2250 let s = String.create
len in
2251 really_input ic
s 0 len;
2254 | Parse_error
(msg
, s, pos
) ->
2255 let subs = subs s pos
in
2256 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2257 failwith
("parse error: " ^
s)
2260 failwith
("config load error: " ^
Printexc.to_string exn
)
2266 let dir = Filename.concat
home ".config" in
2267 if Sys.is_directory
dir then dir else home
2270 Filename.concat
dir "llpp.conf"
2274 if Sys.file_exists
path
2277 (try Some
(open_in_bin
path)
2280 ("Error opening configuation file `" ^
path ^
"': " ^
2281 Printexc.to_string exn
);
2290 ("Error loading configuation from `" ^
path ^
"': " ^
2291 Printexc.to_string exn
);
2297 f (Hashtbl.create
0, defconf)
2302 let pc, pb
, px
, py =
2304 Hashtbl.find h state.path
2305 with Not_found
-> dc, [], 0, 0.0
2309 state.bookmarks <- pb
;
2311 cbput state.hists
.nav
py;
2312 cbrfollowlen state.hists
.nav
;
2317 let add_attrs bb always
dc c =
2320 then Printf.bprintf bb
"\n %s='%b'" s a
2323 then Printf.bprintf bb
"\n %s='%d'" s a
2326 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2328 oi
"width" c.winw
dc.winw
;
2329 oi
"height" c.winh
dc.winh
;
2330 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2331 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2332 ob "case-insensitive-search" c.icase
dc.icase
;
2333 ob "preload" c.preload dc.preload;
2334 oi
"page-bias" c.pagebias
dc.pagebias
;
2335 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2336 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2337 ob "crop-hack" c.crophack
dc.crophack
;
2338 ob "throttle" c.showall
dc.showall
;
2339 ob "highlight-links" c.hlinks
dc.hlinks
;
2340 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2341 oi
"vertical-margin" c.interpagespace dc.interpagespace;
2342 oz
"zoom" c.zoom dc.zoom;
2343 ob "presentation" c.presentation dc.presentation;
2344 oi
"rotation-angle" c.angle
dc.angle
;
2345 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2349 let bb = Buffer.create
32768 in
2351 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2352 add_attrs bb true dc dc;
2353 Buffer.add_string
bb "/>\n";
2355 let adddoc path x y c bookmarks =
2356 if bookmarks == [] && c = dc && y = 0.0
2359 Printf.bprintf
bb "<doc path='%s'"
2360 (enent
path 0 (String.length
path));
2363 then Printf.bprintf
bb " rely='%f'" y;
2366 then Printf.bprintf
bb " pan='%d'" x;
2368 add_attrs bb false dc c;
2370 begin match bookmarks with
2371 | [] -> Buffer.add_string
bb "/>\n"
2373 Buffer.add_string
bb ">\n<bookmarks>\n";
2374 List.iter
(fun (title, _level
, page, rely) ->
2376 "<item title='%s' page='%d' rely='%f'/>\n"
2377 (enent
title 0 (String.length
title))
2381 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2386 adddoc state.path state.x (yratio state.y) conf
2387 (if conf.savebmarks
then state.bookmarks else []);
2389 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2390 if path <> state.path
2392 adddoc path x y c bookmarks
2394 Buffer.add_string
bb "</llppconfig>";
2398 let tmp = path ^
".tmp" in
2399 let oc = open_out_bin
tmp in
2400 Buffer.output_buffer
oc bb;
2402 Sys.rename
tmp path;
2405 ("error while saving configuration: " ^
Printexc.to_string exn
)
2411 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2412 (fun s -> state.path <- s)
2413 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2416 if String.length
state.path = 0
2417 then (prerr_endline
"filename missing"; exit
1)
2423 let _ = Glut.init
Sys.argv
in
2424 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2425 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2426 let _ = Glut.createWindow
("llpp " ^
Filename.basename
name) in
2429 if Sys.os_type
= "Unix"
2431 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2433 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2434 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2435 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2436 Unix.bind
sock addr;
2438 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2439 Unix.connect
csock addr;
2440 let ssock, _ = Unix.accept
sock in
2443 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2444 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2448 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2452 let () = Glut.displayFunc
display in
2453 let () = Glut.reshapeFunc
reshape in
2454 let () = Glut.keyboardFunc
keyboard in
2455 let () = Glut.specialFunc
special in
2456 let () = Glut.idleFunc
(Some
idle) in
2457 let () = Glut.mouseFunc
mouse in
2458 let () = Glut.motionFunc
motion in
2459 let () = Glut.passiveMotionFunc
pmotion in
2462 state.csock <- csock;
2463 state.ssock <- ssock;
2464 state.text <- "Opening " ^
name;
2465 writecmd state.csock ("open " ^
state.path ^
"\000" ^
state.password ^
"\000");
2469 let rec handlelablglutbug () =
2472 with Glut.BadEnum
"key in special_of_int" ->
2473 showtext '
!'
" LablGlut bug: special key not recognized";
2474 handlelablglutbug ()
2476 handlelablglutbug ();