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";;
18 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
22 | Msel
of (mpos
* mpos
)
36 type textentry
= (char
* string * onhist
option * onkey
* ondone
)
37 and onkey
= string -> int -> te
38 and ondone
= string -> unit
39 and onhist
= histcmd
-> string
40 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
45 | TEswitch
of textentry
49 { store
= Array.create n v
56 let cblen b
= Array.length b
.store
;;
61 b
.wc
<- (b
.wc
+ 1) mod len;
62 b
.len <- min
(b
.len + 1) len;
65 let cbpeekw b
= b
.store
.(b
.wc
);;
71 let rc = b
.rc + dir
in
72 let rc = if rc = -1 then b
.len - 1 else rc in
73 let rc = if rc = b
.len then 0 else rc in
84 Array.fill b
.store
0 (Array.length b
.store
) v
;
100 { mutable scrollw
: int
101 ; mutable scrollh
: int
102 ; mutable icase
: bool
103 ; mutable preload
: bool
104 ; mutable pagebias
: int
105 ; mutable verbose
: bool
106 ; mutable scrollincr
: int
107 ; mutable maxhfit
: bool
108 ; mutable crophack
: bool
109 ; mutable autoscroll
: bool
110 ; mutable showall
: bool
111 ; mutable hlinks
: bool
112 ; mutable underinfo
: bool
113 ; mutable interpagespace
: int
114 ; mutable zoom
: float
115 ; mutable presentation
: bool
116 ; mutable angle
: int
119 ; mutable savebmarks
: bool
120 ; mutable proportional
: bool
124 type outline
= string * int * int * float;;
126 | Oarray
of outline array
127 | Olist
of outline list
128 | Onarrow
of string * outline array
* outline array
131 type rect
= (float * float * float * float * float * float * float * float);;
134 { mutable csock
: Unix.file_descr
135 ; mutable ssock
: Unix.file_descr
141 ; mutable layout
: layout list
142 ; pagemap
: ((int * int * int * bool), string) Hashtbl.t
143 ; mutable pdims
: (int * int * int * int) list
144 ; mutable pagecount
: int
145 ; pagecache
: string circbuf
146 ; mutable rendering
: bool
147 ; mutable mstate
: mstate
148 ; mutable searchpattern
: string
149 ; mutable rects
: (int * int * rect
) list
150 ; mutable rects1
: (int * int * rect
) list
151 ; mutable text
: string
152 ; mutable fullscreen
: (int * int) option
153 ; mutable textentry
: textentry
option
154 ; mutable outlines
: outlines
155 ; mutable outline
: (bool * int * int * outline array
* string) option
156 ; mutable bookmarks
: outline list
157 ; mutable path
: string
158 ; mutable password
: string
159 ; mutable invalidated
: int
160 ; mutable colorscale
: float
164 { pat
: string circbuf
165 ; pag
: string circbuf
166 ; nav
: float circbuf
186 ; presentation
= false
191 ; proportional
= true
195 let defconf = { conf with angle
=conf.angle
};;
206 ; pagemap
= Hashtbl.create
10
207 ; pagecache
= cbnew 10 ""
218 ; outlines
= Olist
[]
225 { nav
= cbnew 100 0.0
236 Printf.kprintf prerr_endline fmt
238 Printf.kprintf ignore fmt
242 let len = String.length s
in
244 let b = Buffer.create
n in
245 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
246 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
247 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
248 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
249 Buffer.add_string
b s
;
250 let s'
= Buffer.contents
b in
251 let n'
= Unix.write fd
s'
0 n in
252 if n'
!= n then failwith
"write failed";
257 let n = Unix.read fd
s 0 4 in
258 if n != 4 then failwith
"incomplete read(len)";
260 lor (Char.code
s.[0] lsl 24)
261 lor (Char.code
s.[1] lsl 16)
262 lor (Char.code
s.[2] lsl 8)
263 lor (Char.code
s.[3] lsl 0)
265 let s = String.create
len in
266 let n = Unix.read fd
s 0 len in
267 if n != len then failwith
"incomplete read(data)";
274 else float y
/. float state.maxy
278 let b = Buffer.create
10 in
279 Buffer.add_string
b s;
280 let rec combine = function
283 Buffer.add_char
b ' '
;
286 | `
b b -> if b then "1" else "0"
288 | `i i
-> string_of_int i
289 | `f f
-> string_of_float f
290 | `I f
-> string_of_int
(truncate f
)
292 Buffer.add_string
b s;
299 let cmd = Buffer.contents
(makecmd s l
) in
300 writecmd state.csock
cmd;
306 let d = conf.winh
- h
in
313 let rec f pn ph pi fh l
=
315 | (n, _
, h
, _
) :: rest
->
316 let ips = calcips h
in
322 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
331 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
334 let fh = f 0 0 0 0 state.pdims
in
338 let getpageyh pageno
=
339 let rec f pn ph pi y l
=
341 | (n, _
, h
, _
) :: rest
->
342 let ips = calcips h
in
345 if conf.presentation
&& n = pageno
347 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
349 y
+ (pageno
- pn
) * (ph
+ pi
), h
351 let y = y + (if conf.presentation
then pi
else 0) in
352 let y = y + (n - pn
) * (ph
+ pi
) in
356 y + (pageno
- pn
) * (ph
+ pi
), ph
358 f 0 0 0 0 state.pdims
361 let getpagey pageno
= fst
(getpageyh pageno
);;
364 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
365 let ((w
, h
, ips, x
) as curr
), rest
, pdimno
, yinc
=
367 | (pageno'
, w
, h
, x
) :: rest
when pageno'
= pageno
->
368 let ips = calcips h
in
369 let yinc = if conf.presentation
then ips else 0 in
370 (w
, h
, ips, x
), rest
, pdimno
+ 1, yinc
372 prev
, pdims
, pdimno
, 0
374 let dy = dy + yinc in
375 let py = py + yinc in
376 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
381 if py + h
<= vy - yinc
383 let py = py + h
+ ips in
384 let dy = max
0 (py - y) in
394 let pagey = vy - py in
395 let pagevh = h
- pagey in
396 let pagevh = min
(sh
- dy) pagevh in
397 let off = if yinc > 0 then py - vy else 0
399 let py = py + h
+ ips in
405 ; pagedispy
= dy + off
406 ; pagey = pagey + off
407 ; pagevh = pagevh - off
411 let accu = e :: accu in
418 ~cacheleft
:(cacheleft
-1)
421 if state.invalidated
= 0
431 ~cacheleft
:(cblen state.pagecache
)
441 let y = state.y + incr
in
443 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
447 let getopaque pageno
=
448 try Some
(Hashtbl.find
state.pagemap
449 (pageno
+ 1, state.w
, conf.angle
, conf.proportional
))
450 with Not_found
-> None
453 let cache pageno opaque
=
454 Hashtbl.replace
state.pagemap
455 (pageno
+ 1, state.w
, conf.angle
, conf.proportional
) opaque
458 let validopaque opaque
= String.length opaque
> 0;;
461 match getopaque l
.pageno
with
462 | None
when not
state.rendering
->
463 state.rendering
<- true;
465 wcmd "render" [`i
(l
.pageno
+ 1)
473 let loadlayout layout =
474 let rec f all
= function
476 begin match getopaque l
.pageno
with
477 | None
-> render l
; f false ls
478 | Some opaque
-> f (all
&& validopaque opaque
) ls
482 f (layout <> []) layout;
489 let evictedopaque = cbpeekw state.pagecache
in
490 List.exists
(fun l
->
491 match getopaque l
.pageno
with
492 | Some opaque
when validopaque opaque
->
493 evictedopaque = opaque
497 if not
evictedvisible
499 let rely = yratio state.y in
500 let presentation = conf.presentation in
501 let interpagespace = conf.interpagespace in
502 let maxy = state.maxy in
503 conf.presentation <- false;
504 conf.interpagespace <- 0;
505 state.maxy <- calcheight ();
506 let y = truncate
(float state.maxy *. rely) in
507 let y = if y < conf.winh
then 0 else y - conf.winh
in
508 let pages = layout y (conf.winh
*3) in
509 List.iter
render pages;
510 conf.presentation <- presentation;
511 conf.interpagespace <- interpagespace;
517 let y = min
state.maxy y in
518 let pages = layout y conf.winh
in
519 let ready = loadlayout pages in
520 state.ty
<- yratio y;
525 state.layout <- pages;
527 Glut.postRedisplay
();
531 state.layout <- pages;
533 Glut.postRedisplay
();
538 let gotoy_and_clear_text y =
540 if not
conf.verbose
then state.text
<- "";
544 cbput state.hists
.nav
(yratio state.y);
545 cbrfollowlen state.hists
.nav
;
549 let y = cbget state.hists
.nav ~
-1 in
550 truncate
(y *. float state.maxy)
554 let y, h
= getpageyh n in
556 gotoy_and_clear_text (y + (truncate
(top
*. float h
)));
559 let gotopage1 n top
=
560 let y = getpagey n in
562 gotoy_and_clear_text (y + top
);
570 state.invalidated
<- state.invalidated
+ 1;
574 let c = c *. state.colorscale
in
580 match state.layout with
582 let rely = yratio state.y in
583 state.maxy <- calcheight ();
584 truncate
(float state.maxy *. rely)
587 state.maxy <- calcheight ();
594 GlMat.mode `projection
;
595 GlMat.load_identity
();
596 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
597 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
598 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
602 GlMat.mode `projection
;
603 GlMat.load_identity
();
604 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
605 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
606 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
611 let w = truncate
(float w *. conf.zoom
) - conf.scrollw
in
614 GlMat.mode `modelview
;
615 GlMat.load_identity
();
616 GlClear.color
(scalecolor 1.0);
617 GlClear.clear
[`color
];
620 wcmd "geometry" [`i
w; `i h
];
624 GlDraw.color
(0.0, 0.0, 0.0);
626 (0.0, float (conf.winh
- 18))
627 (float (conf.winw
- conf.scrollw
- 1), float conf.winh
)
629 let font = Glut.BITMAP_8_BY_13
in
630 GlDraw.color
(1.0, 1.0, 1.0);
631 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
632 Glut.bitmapCharacter ~
font ~
c:(Char.code
c);
633 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
637 let len = String.length
state.text
in
638 match state.textentry
with
640 if len > 0 then showtext ' '
state.text
642 | Some
(c, text
, _
, _
, _
) ->
646 text ^
" [" ^
state.text ^
"]"
656 state.text
<- Printf.sprintf
"%c%s" c s;
657 Glut.postRedisplay
();
671 state.rects
<- state.rects1
;
672 Glut.postRedisplay
()
675 let n = Scanf.sscanf
cmd "C %d" (fun n -> n) in
676 state.pagecount
<- n;
677 state.invalidated
<- state.invalidated
- 1;
678 if state.invalidated
= 0
682 let s = Scanf.sscanf
cmd "t %n"
683 (fun n -> String.sub
cmd n (String.length
cmd - n))
685 Glut.setWindowTitle
s
688 let s = Scanf.sscanf
cmd "T %n"
689 (fun n -> String.sub
cmd n (String.length
cmd - n))
691 if state.textentry
= None
698 Glut.postRedisplay
();
704 let s = Scanf.sscanf
cmd "V %n"
705 (fun n -> String.sub
cmd n (String.length
cmd - n))
711 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
712 Scanf.sscanf
cmd "F %d %d %f %f %f %f %f %f %f %f"
713 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
714 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
716 let y = (getpagey pageno) + truncate y0
in
719 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
722 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
723 Scanf.sscanf
cmd "R %d %d %f %f %f %f %f %f %f %f"
724 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
725 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
728 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
731 let n, w, h
, r
, l
, p
=
732 Scanf.sscanf
cmd "r %d %d %d %d %d %s"
733 (fun n w h r l p
-> (n, w, h
, r
, l
!= 0, p
))
735 Hashtbl.replace
state.pagemap
(n, w, r
, l
) p
;
736 let opaque = cbpeekw state.pagecache
in
737 if validopaque opaque
741 (fun k v a
-> if v
= opaque then k else a
)
742 state.pagemap
(-1, -1, -1, false)
744 wcmd "free" [`
s opaque];
745 Hashtbl.remove
state.pagemap
k
747 cbput state.pagecache p
;
748 state.rendering
<- false;
750 then gotoy (truncate
(ceil
(state.ty
*. float state.maxy)))
752 let visible = List.exists
(fun l
-> l
.pageno + 1 = n) state.layout in
755 else (ignore
(loadlayout state.layout); preload ())
759 let (n, w, h
, x
) as pdim
=
760 Scanf.sscanf
cmd "l %d %d %d %d" (fun n w h x
-> n, w, h
, x
)
762 state.pdims
<- pdim
:: state.pdims
765 let (l
, n, t
, h
, pos
) =
766 Scanf.sscanf
cmd "o %d %d %d %d %n" (fun l
n t h pos
-> l
, n, t
, h
, pos
)
768 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
770 let l = String.length
s in
771 let b = Buffer.create
(String.length
s) in
778 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
781 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
790 let outline = (s, l, n, float t
/. float h
) in
792 match state.outlines with
793 | Olist
outlines -> Olist
(outline :: outlines)
794 | Oarray _
-> Olist
[outline]
795 | Onarrow _
-> Olist
[outline]
797 state.outlines <- outlines
800 log "unknown cmd `%S'" cmd
803 let now = Unix.gettimeofday
;;
807 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
812 let y = state.y + conf.scrollincr
in
813 let y = if y >= state.maxy then 0 else y in
819 let cmd = readcmd state.csock
in
826 let onhist cb
= function
827 | HCprev
-> cbget cb ~
-1
828 | HCnext
-> cbget cb
1
829 | HCfirst
-> cbget cb ~
-(cb
.rc)
830 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
833 let search pattern forward
=
834 if String.length pattern
> 0
837 match state.layout with
840 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
843 let b = makecmd "search"
844 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
846 Buffer.add_char
b '
,'
;
847 Buffer.add_string
b pattern
;
848 Buffer.add_char
b '
\000'
;
851 writecmd state.csock
cmd;
854 let intentry text key
=
855 let c = Char.unsafe_chr key
in
858 let s = "x" in s.[0] <- c;
859 let text = text ^
s in
863 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
868 let b = Buffer.create
(String.length
s + 1) in
869 Buffer.add_string
b s;
874 let textentry text key
=
875 let c = Char.unsafe_chr key
in
877 | _
when key
>= 32 && key
< 127 ->
878 let text = addchar text c in
882 log "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
886 let reinit angle proportional
=
888 conf.proportional
<- proportional
;
890 wcmd "reinit" [`i angle
; `
b proportional
];
893 let optentry text key
=
894 let btos b = if b then "on" else "off" in
895 let c = Char.unsafe_chr key
in
899 try conf.scrollincr
<- int_of_string
s with exc
->
900 state.text <- Printf.sprintf
"bad integer `%s': %s"
901 s (Printexc.to_string exc
)
903 TEswitch
('#'
, "", None
, intentry, ondone)
908 Some
(int_of_string
s)
910 state.text <- Printf.sprintf
"bad integer `%s': %s"
911 s (Printexc.to_string exc
);
914 | Some angle
-> reinit angle
conf.proportional
917 TEswitch
('^'
, "", None
, intentry, ondone)
920 conf.icase
<- not
conf.icase
;
921 TEdone
("case insensitive search " ^
(btos conf.icase
))
924 conf.preload <- not
conf.preload;
926 TEdone
("preload " ^
(btos conf.preload))
929 conf.verbose
<- not
conf.verbose
;
930 TEdone
("verbose " ^
(btos conf.verbose
))
933 conf.maxhfit
<- not
conf.maxhfit
;
934 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
935 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
938 conf.crophack
<- not
conf.crophack
;
939 TEdone
("crophack " ^
btos conf.crophack
)
942 conf.showall
<- not
conf.showall
;
943 TEdone
("showall " ^
btos conf.showall
)
946 conf.underinfo
<- not
conf.underinfo
;
947 TEdone
("underinfo " ^
btos conf.underinfo
)
950 conf.savebmarks
<- not
conf.savebmarks
;
951 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
956 conf.interpagespace <- int_of_string
s;
957 let rely = yratio state.y in
958 state.maxy <- calcheight ();
959 gotoy (truncate
(float state.maxy *. rely));
961 state.text <- Printf.sprintf
"bad integer `%s': %s"
962 s (Printexc.to_string exc
)
964 TEswitch
('
%'
, "", None
, intentry, ondone)
967 reinit conf.angle
(not
conf.proportional
);
968 TEdone
("proprortional display " ^
btos conf.proportional
)
971 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
975 let maxoutlinerows () = (conf.winh
- 31) / 16;;
977 let enterselector allowdel
outlines errmsg msg
=
978 if Array.length
outlines = 0
984 Glut.setCursor
Glut.CURSOR_INHERIT
;
986 match state.layout with
988 | {pageno=pageno} :: rest
-> pageno
992 if n = Array.length
outlines
995 let (_
, _
, outlinepageno
, _
) = outlines.(n) in
996 if outlinepageno
>= pageno then n else loop (n+1)
1001 Some
(allowdel
, active,
1002 max
0 ((active - maxoutlinerows () / 2)), outlines, "");
1003 Glut.postRedisplay
();
1007 let enteroutlinemode () =
1009 match state.outlines with
1012 let a = Array.of_list
(List.rev
l) in
1013 state.outlines <- Oarray
a;
1015 | Onarrow
(pat
, a, b) ->
1016 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1018 enterselector false outlines "Document has no outline" msg
;
1021 let enterbookmarkmode () =
1022 let bookmarks = Array.of_list
state.bookmarks in
1023 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1026 let quickbookmark ?title
() =
1027 match state.layout with
1033 let sec = Unix.gettimeofday
() in
1034 let tm = Unix.localtime
sec in
1035 Printf.sprintf
"Quick (page %d) visited (%d/%d/%d %d:%d)"
1039 (tm.Unix.tm_year
+ 1900)
1042 | Some
title -> title
1045 (title, 0, l.pageno, float l.pagey /. float l.pageh
) :: state.bookmarks
1049 state.fullscreen
<- None
;
1050 Glut.reshapeWindow
w h
;
1053 let writeopen path password
=
1054 writecmd state.csock
1055 ("open " ^ path ^
"\000"
1056 ^
state.password ^
"\000"
1057 ^ string_of_int
conf.angle ^
" "
1058 ^
(if conf.proportional
then "1" else "0"))
1062 let opendoc path password
=
1065 state.password
<- password
;
1066 Hashtbl.clear
state.pagemap
;
1068 writeopen path password
;
1069 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1070 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1073 let viewkeyboard ~key ~x ~
y =
1075 state.textentry <- te
;
1078 Glut.postRedisplay
()
1080 match state.textentry with
1082 let c = Char.chr key
in
1088 let y = getnav () in
1089 gotoy_and_clear_text y
1097 Glut.postRedisplay
()
1100 let ondone isforw
s =
1101 cbput state.hists
.pat
s;
1102 cbrfollowlen state.hists
.pat
;
1103 state.searchpattern
<- s;
1106 enttext (Some
(c, "", Some
(onhist state.hists
.pat
),
1107 textentry, ondone (c ='
/'
)))
1109 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1110 conf.zoom
<- min
2.2 (conf.zoom
+. 0.1);
1111 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1112 reshape conf.winw
conf.winh
1117 try int_of_string
s with exc
->
1118 state.text <- Printf.sprintf
"bad integer `%s': %s"
1119 s (Printexc.to_string exc
);
1125 state.text <- "page bias is now " ^ string_of_int
n;
1128 enttext (Some
('
+'
, "", None
, intentry, ondone))
1130 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1131 conf.zoom
<- max
0.1 (conf.zoom
-. 0.1);
1132 if conf.zoom
<= 1.0 then state.x
<- 0;
1133 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom
);
1134 reshape conf.winw
conf.winh
;
1140 enttext (Some
('
-'
, "", None
, optentry, ondone))
1142 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1145 state.text <- "zoom is 100%";
1146 reshape conf.winw
conf.winh
1148 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1149 let zoom = zoomforh
conf.winw
conf.winh
conf.scrollw
in
1154 state.text <- Printf.sprintf
"zoom is %3.1f%%" (100.0*.conf.zoom);
1155 reshape conf.winw
conf.winh
;
1161 try int_of_string
s with exc
->
1162 state.text <- Printf.sprintf
"bad integer `%s': %s"
1163 s (Printexc.to_string exc
);
1169 cbput state.hists
.pag
(string_of_int
n);
1170 cbrfollowlen state.hists
.pag
;
1171 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1174 let pageentry text key
=
1175 match Char.unsafe_chr key
with
1176 | 'g'
-> TEdone
text
1177 | _
-> intentry text key
1179 let text = "x" in text.[0] <- c;
1180 enttext (Some
('
:'
, text, Some
(onhist state.hists
.pag
),
1184 conf.scrollw
<- if conf.scrollw
> 0 then 0 else defconf.scrollw
;
1185 reshape conf.winw
conf.winh
;
1188 conf.hlinks
<- not
conf.hlinks
;
1189 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1190 Glut.postRedisplay
()
1193 conf.autoscroll
<- not
conf.autoscroll
1196 conf.presentation <- not
conf.presentation;
1197 showtext ' '
("presentation mode " ^
1198 if conf.presentation then "on" else "off");
1202 begin match state.fullscreen
with
1204 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1207 state.fullscreen
<- None
;
1212 gotoy_and_clear_text 0
1215 search state.searchpattern
true
1218 search state.searchpattern
false
1221 begin match state.layout with
1224 gotoy_and_clear_text (getpagey l.pageno)
1228 begin match List.rev
state.layout with
1231 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1232 gotoy_and_clear_text (getpagey pageno)
1236 begin match state.layout with
1239 let pageno = max
0 (l.pageno-1) in
1240 gotoy_and_clear_text (getpagey pageno)
1245 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1247 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1249 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1253 else (100. *. (float state.y /. float maxy)) in
1256 Printf.sprintf
"Page %d of %d %.2f%%"
1257 (fn+1) state.pagecount
percent
1260 "Pages %d-%d of %d %.2f%%"
1261 (fn+1) (ln
+1) state.pagecount
percent
1266 begin match state.layout with
1269 doreshape (l.pagew
+ conf.scrollw
) l.pageh
;
1270 Glut.postRedisplay
();
1274 enterbookmarkmode ()
1278 match state.layout with
1281 (s, 0, l.pageno, float l.pagey /. float l.pageh
)
1285 enttext (Some
('~'
, "", None
, textentry, ondone))
1289 showtext ' '
"Quick bookmark added";
1292 begin match state.layout with
1294 let rect = getpdimrect
l.pagedimno
in
1298 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1299 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1301 (truncate
(rect.(1) -. rect.(0)),
1302 truncate
(rect.(3) -. rect.(0)))
1306 doreshape (w + conf.scrollw
) (h
+ conf.interpagespace)
1308 Glut.postRedisplay
();
1314 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1319 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1320 Glut.postRedisplay
()
1322 | '
k'
-> gotoy (clamp (-conf.scrollincr
))
1323 | 'j'
-> gotoy (clamp conf.scrollincr
)
1325 | '
r'
-> opendoc state.path
state.password
1328 vlog "huh? %d %c" key
(Char.chr key
);
1331 | Some
(c, text, onhist, onkey
, ondone) when key
= 8 ->
1332 let len = String.length
text in
1335 state.textentry <- None
;
1336 Glut.postRedisplay
();
1339 let s = String.sub
text 0 (len - 1) in
1340 enttext (Some
(c, s, onhist, onkey
, ondone))
1343 | Some
(c, text, onhist, onkey
, ondone) ->
1344 begin match Char.unsafe_chr key
with
1347 state.textentry <- None
;
1348 Glut.postRedisplay
()
1351 state.textentry <- None
;
1352 Glut.postRedisplay
()
1355 begin match onkey
text key
with
1357 state.textentry <- None
;
1359 Glut.postRedisplay
()
1362 enttext (Some
(c, text, onhist, onkey
, ondone));
1365 state.textentry <- None
;
1366 Glut.postRedisplay
()
1369 state.textentry <- Some te
;
1370 Glut.postRedisplay
()
1375 let narrow outlines pattern
=
1376 let reopt = try Some
(Str.regexp_case_fold pattern
) with _
-> None
in
1380 let rec fold accu n =
1384 let (s, _
, _
, _
) as o
= outlines.(n) in
1386 if (try ignore
(Str.search_forward re
s 0); true
1387 with Not_found
-> false)
1393 let matched = fold [] (Array.length
outlines - 1) in
1394 if matched = [] then None
else Some
(Array.of_list
matched)
1397 let outlinekeyboard ~key ~x ~
y (allowdel
, active, first
, outlines, qsearch
) =
1398 let search active pattern incr
=
1401 if n = Array.length
outlines || n = -1
1404 let (s, _
, _
, _
) = outlines.(n) in
1406 (try ignore
(Str.search_forward re
s 0); true
1407 with Not_found
-> false)
1409 else loop (n + incr
)
1414 let re = Str.regexp_case_fold pattern
in
1420 let firstof active = max
0 (active - maxoutlinerows () / 2) in
1423 if String.length qsearch
= 0
1426 state.outline <- None
;
1427 Glut.postRedisplay
();
1431 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1432 Glut.postRedisplay
();
1436 let incr = if key
= 18 then -1 else 1 in
1438 match search (active + incr) qsearch
incr with
1440 state.text <- qsearch ^
" [not found]";
1443 state.text <- qsearch
;
1444 active, firstof active
1446 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch
);
1447 Glut.postRedisplay
();
1450 let len = String.length qsearch
in
1457 state.outline <- Some
(allowdel
, active, first
, outlines, "");
1460 let qsearch = String.sub
qsearch 0 (len - 1) in
1462 match search active qsearch ~
-1 with
1464 state.text <- qsearch ^
" [not found]";
1467 state.text <- qsearch;
1468 active, firstof active
1470 state.outline <- Some
(allowdel
, active, first
, outlines, qsearch);
1472 Glut.postRedisplay
()
1475 if active < Array.length
outlines
1477 let (_
, _
, n, t
) = outlines.(active) in
1481 if allowdel
then state.bookmarks <- Array.to_list
outlines;
1482 state.outline <- None
;
1483 Glut.postRedisplay
();
1485 | _
when key
>= 32 && key
< 127 ->
1486 let pattern = addchar qsearch (Char.chr key
) in
1488 match search active pattern 1 with
1490 state.text <- pattern ^
" [not found]";
1493 state.text <- pattern;
1494 active, firstof active
1496 state.outline <- Some
(allowdel
, active, first
, outlines, pattern);
1497 Glut.postRedisplay
()
1499 | 14 when not allowdel
-> (* ctrl-n *)
1500 if String.length
qsearch > 0
1502 let optoutlines = narrow outlines qsearch in
1503 begin match optoutlines with
1504 | None
-> state.text <- "can't narrow"
1506 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1507 match state.outlines with
1510 state.outlines <- Onarrow
(qsearch, outlines, a)
1511 | Onarrow
(pat
, a, b) ->
1512 state.outlines <- Onarrow
(qsearch, outlines, b)
1515 Glut.postRedisplay
()
1517 | 21 when not allowdel
-> (* ctrl-u *)
1519 match state.outlines with
1522 let a = Array.of_list
(List.rev
l) in
1523 state.outlines <- Oarray
a;
1525 | Onarrow
(pat
, a, b) ->
1526 state.outlines <- Oarray
b;
1530 state.outline <- Some
(allowdel
, 0, 0, outline, qsearch);
1531 Glut.postRedisplay
()
1535 Some
(allowdel
, active, firstof active, outlines, qsearch);
1536 Glut.postRedisplay
()
1538 | 127 when allowdel
->
1539 let len = Array.length
outlines - 1 in
1542 state.outline <- None
;
1543 state.bookmarks <- [];
1546 let bookmarks = Array.init
len
1548 let i = if i >= active then i + 1 else i in
1559 Glut.postRedisplay
()
1561 | _
-> log "unknown key %d" key
1564 let keyboard ~key ~x ~
y =
1569 match state.outline with
1570 | None
-> viewkeyboard ~key ~x ~
y
1571 | Some
outline -> outlinekeyboard ~key ~x ~
y outline
1574 let special ~key ~x ~
y =
1575 match state.outline with
1577 begin match state.textentry with
1581 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
1582 | Glut.KEY_UP
-> clamp (-conf.scrollincr
)
1583 | Glut.KEY_DOWN
-> clamp conf.scrollincr
1584 | Glut.KEY_PAGE_UP
->
1585 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1587 match state.layout with
1589 | l :: _
-> state.y - l.pagey
1592 | Glut.KEY_PAGE_DOWN
->
1593 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
1595 match List.rev
state.layout with
1597 | l :: _
-> getpagey l.pageno
1600 | Glut.KEY_HOME
-> addnav (); 0
1603 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
1605 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
1606 state.x
<- state.x
- 10;
1608 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
1609 state.x
<- state.x
+ 10;
1614 gotoy_and_clear_text y
1616 | Some
(c, s, Some
onhist, onkey
, ondone) ->
1619 | Glut.KEY_UP
-> onhist HCprev
1620 | Glut.KEY_DOWN
-> onhist HCnext
1621 | Glut.KEY_HOME
-> onhist HCfirst
1622 | Glut.KEY_END
-> onhist HClast
1625 state.textentry <- Some
(c, s, Some
onhist, onkey
, ondone);
1626 Glut.postRedisplay
()
1631 | Some
(allowdel
, active, first
, outlines, qsearch) ->
1632 let maxrows = maxoutlinerows () in
1634 let active = active + incr in
1635 let active = max
0 (min
active (Array.length
outlines - 1)) in
1639 let rows = active - first in
1640 if rows > maxrows then active - maxrows else first
1643 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1644 Glut.postRedisplay
()
1647 | Glut.KEY_UP
-> navigate ~
-1
1648 | Glut.KEY_DOWN
-> navigate 1
1649 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
1650 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
1653 state.outline <- Some
(allowdel
, 0, 0, outlines, qsearch);
1654 Glut.postRedisplay
()
1657 let active = Array.length
outlines - 1 in
1658 let first = max
0 (active - maxrows) in
1659 state.outline <- Some
(allowdel
, active, first, outlines, qsearch);
1660 Glut.postRedisplay
()
1665 let drawplaceholder l =
1666 GlDraw.color
(scalecolor 1.0);
1668 (float l.pagex
, float l.pagedispy
)
1669 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
1671 let x = float l.pagex
1672 and y = float (l.pagedispy
+ 13) in
1673 let font = Glut.BITMAP_8_BY_13
in
1674 GlDraw.color
(0.0, 0.0, 0.0);
1675 GlPix.raster_pos ~
x ~
y ();
1676 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
1677 ("Loading " ^ string_of_int
(l.pageno + 1));
1680 let now () = Unix.gettimeofday
();;
1683 begin match getopaque l.pageno with
1684 | Some
opaque when validopaque opaque ->
1685 if state.textentry = None
1686 then GlDraw.color
(scalecolor 1.0)
1687 else GlDraw.color
(scalecolor 0.4);
1689 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
1693 vlog "draw %d %f sec" l.pageno d;
1698 l.pagedispy
+ l.pagevh;
1702 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1703 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
1704 let sh = float conf.winh
/. sh in
1705 let sh = max
sh (float conf.scrollh
) in
1708 if state.y = state.maxy
1710 else float y /. float maxy
1712 let position = (float conf.winh
-. sh) *. percent in
1715 if position +. sh > float conf.winh
1716 then float conf.winh
-. sh
1722 let scrollindicator () =
1723 GlDraw.color
(0.64 , 0.64, 0.64);
1725 (float (conf.winw
- conf.scrollw
), 0.)
1726 (float conf.winw
, float conf.winh
)
1728 GlDraw.color
(0.0, 0.0, 0.0);
1730 let position, sh = scrollph state.y in
1732 (float (conf.winw
- conf.scrollw
), position)
1733 (float conf.winw
, position +. sh)
1737 let showsel margin
=
1738 match state.mstate
with
1739 | Mnone
| Mscroll _
| Mpan _
->
1742 | Msel
((x0
, y0
), (x1
, y1
)) ->
1743 let rec loop = function
1745 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1746 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1748 match getopaque l.pageno with
1749 | Some
opaque when validopaque opaque ->
1750 let oy = -l.pagey + l.pagedispy
in
1752 (x0
- margin
- state.x, y0
,
1753 x1
- margin
- state.x, y1
) oy;
1763 let panx = float state.x in
1765 GlDraw.color
(0.0, 0.0, 1.0) ~alpha
:0.5;
1766 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1768 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
1770 if l.pageno = pageno
1772 let d = float (l.pagedispy
- l.pagey) in
1773 GlDraw.color
(0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
1774 GlDraw.begins `quads
;
1776 GlDraw.vertex2
(x0
+.panx, y0
+.d);
1777 GlDraw.vertex2
(x1
+.panx, y1
+.d);
1778 GlDraw.vertex2
(x2
+.panx, y2
+.d);
1779 GlDraw.vertex2
(x3
+.panx, y3
+.d);
1789 let showoutline = function
1791 | Some
(allowdel
, active, first, outlines, qsearch) ->
1793 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1794 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
1795 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
1798 GlDraw.color
(1., 1., 1.);
1799 let font = Glut.BITMAP_9_BY_15
in
1800 let draw_string x y s =
1801 GlPix.raster_pos ~
x ~
y ();
1802 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
1805 if row
= Array.length
outlines || (row
- first) * 16 > conf.winh
1808 let (s, l, _
, _
) = outlines.(row
) in
1809 let y = (row
- first) * 16 in
1814 GlDraw.polygon_mode `both `line
;
1815 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
1816 GlDraw.color
(1., 1., 1.) ~alpha
:0.9;
1817 GlDraw.rect (0., float (y + 1))
1818 (float (conf.winw
- 1), float (y + 18));
1819 GlDraw.polygon_mode `both `fill
;
1821 GlDraw.color
(1., 1., 1.);
1823 draw_string (float x) (float (y + 16)) s;
1831 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1832 GlDraw.viewport
margin 0 state.w conf.winh
;
1834 GlClear.color
(scalecolor 0.5);
1835 GlClear.clear
[`color
];
1838 let x = float state.x in
1839 GlMat.translate ~
x ();
1843 Gl.enable `scissor_test
;
1844 GlMisc.scissor
0 0 (conf.winw
- conf.scrollw
) conf.winh
;
1846 let _lasty = List.fold_left
drawpage 0 (state.layout) in
1849 Gl.disable `scissor_test
1853 let x = -.float state.x in
1854 GlMat.translate ~
x ();
1858 GlDraw.viewport
0 0 conf.winw
conf.winh
;
1861 showoutline state.outline;
1863 Glut.swapBuffers
();
1867 let margin = (conf.winw
- (state.w + conf.scrollw
)) / 2 in
1868 let x = x - margin - state.x in
1869 let rec f = function
1871 begin match getopaque l.pageno with
1872 | Some
opaque when validopaque opaque ->
1873 let y = y - l.pagedispy
in
1876 let y = l.pagey + y in
1877 let x = x - l.pagex
in
1878 match whatsunder
opaque x y with
1891 let mouse ~button ~bstate ~
x ~
y =
1893 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
1901 let incr = incr * 2 in
1902 let y = clamp incr in
1903 gotoy_and_clear_text y
1905 | Glut.LEFT_BUTTON
when state.outline = None
1906 && Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1907 if bstate
= Glut.DOWN
1909 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1910 state.mstate
<- Mpan
(x, y)
1913 state.mstate
<- Mnone
1916 when state.outline = None
&& x > conf.winw
- conf.scrollw
->
1917 if bstate
= Glut.DOWN
1919 let position, sh = scrollph state.y in
1920 if y > truncate
position && y < truncate
(position +. sh)
1922 state.mstate
<- Mscroll
1924 let percent = float y /. float conf.winh
in
1925 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
1927 state.mstate
<- Mscroll
1929 state.mstate
<- Mnone
1931 | Glut.LEFT_BUTTON
when state.outline = None
->
1932 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
1933 begin match dest with
1934 | Ulinkgoto
(pageno, top
) ->
1937 gotopage1 pageno top
1942 | Unone
when bstate
= Glut.DOWN
->
1943 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
1944 state.mstate
<- Mpan
(x, y);
1946 | Unone
| Utext _
->
1947 if bstate
= Glut.DOWN
1949 if conf.angle
mod 360 = 0
1951 state.mstate
<- Msel
((x, y), (x, y));
1952 Glut.postRedisplay
()
1956 match state.mstate
with
1960 state.mstate
<- Mnone
1963 Glut.setCursor
Glut.CURSOR_INHERIT
;
1964 state.mstate
<- Mnone
1966 | Msel
((x0
, y0
), (x1
, y1
)) ->
1968 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
1969 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
1971 match getopaque l.pageno with
1972 | Some
opaque when validopaque opaque ->
1976 List.iter
f state.layout;
1977 copysel
""; (* ugly *)
1978 Glut.setCursor
Glut.CURSOR_INHERIT
;
1979 state.mstate
<- Mnone
;
1986 let mouse ~button ~
state ~
x ~
y = mouse button
state x y;;
1989 if state.outline = None
1991 match state.mstate
with
1997 state.mstate
<- Mpan
(x, y);
1998 if conf.zoom > 1.0 then state.x <- state.x + dx;
2000 gotoy_and_clear_text y
2003 state.mstate
<- Msel
(a, (x, y));
2004 Glut.postRedisplay
()
2007 let y = min
conf.winh
(max
0 y) in
2008 let percent = float y /. float conf.winh
in
2009 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
2010 gotoy_and_clear_text y
2014 if state.outline = None
2016 match state.mstate
with
2018 begin match getunder x y with
2019 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
2021 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
2022 Glut.setCursor
Glut.CURSOR_INFO
2023 | Ulinkgoto
(page
, y) ->
2024 if conf.underinfo
then showtext 'p'
("age: " ^ string_of_int page
);
2025 Glut.setCursor
Glut.CURSOR_INFO
2027 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
2028 Glut.setCursor
Glut.CURSOR_TEXT
2031 | Mpan _
| Msel _
| Mscroll
->
2041 match Sys.os_type
with
2042 | "Win32" -> Sys.getenv
"HOMEPATH"
2043 | _
-> Sys.getenv
"HOME"
2046 ("Can not determine home directory location: " ^
2047 Printexc.to_string exn
);
2051 let config_of c attrs
=
2055 | "scroll-bar-width" -> { c with scrollw
= int_of_string v
}
2056 | "scroll-handle-height" -> { c with scrollh
= int_of_string v
}
2057 | "case-insensitive-search" -> { c with icase
= bool_of_string v
}
2058 | "preload" -> { c with preload = bool_of_string v
}
2059 | "page-bias" -> { c with pagebias
= int_of_string v
}
2060 | "scroll-step" -> { c with scrollincr
= int_of_string v
}
2061 | "max-height-fit" -> { c with maxhfit
= bool_of_string v
}
2062 | "crop-hack" -> { c with crophack
= bool_of_string v
}
2063 | "throttle" -> { c with showall
= bool_of_string v
}
2064 | "highlight-links" -> { c with hlinks
= bool_of_string v
}
2065 | "under-cursor-info" -> { c with underinfo
= bool_of_string v
}
2066 | "vertical-margin" -> { c with interpagespace = int_of_string v
}
2067 | "zoom" -> { c with zoom = float_of_string v
/. 100. }
2068 | "presentation" -> { c with presentation = bool_of_string v
}
2069 | "rotation-angle" -> { c with angle
= int_of_string v
}
2070 | "width" -> { c with winw
= int_of_string v
}
2071 | "height" -> { c with winh
= int_of_string v
}
2072 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string v
}
2073 | "proportional-display" -> { c with proportional
= bool_of_string v
}
2076 prerr_endline
("Error processing attribute (`" ^
2077 k ^
"'=`" ^ v ^
"'): " ^
Printexc.to_string exn
);
2080 let rec fold c = function
2083 let c = apply c k v
in
2089 let bookmark_of attrs
=
2090 let rec fold title page
rely = function
2091 | ("title", v
) :: rest
-> fold v page
rely rest
2092 | ("page", v
) :: rest
-> fold title v
rely rest
2093 | ("rely", v
) :: rest
-> fold title page v rest
2094 | _
:: rest
-> fold title page
rely rest
2095 | [] -> title, page
, rely
2097 fold "invalid" "0" "0" attrs
2100 let setconf dst src
=
2101 dst
.scrollw
<- src
.scrollw
;
2102 dst
.scrollh
<- src
.scrollh
;
2103 dst
.icase
<- src
.icase
;
2104 dst
.preload <- src
.preload;
2105 dst
.pagebias
<- src
.pagebias
;
2106 dst
.verbose
<- src
.verbose
;
2107 dst
.scrollincr
<- src
.scrollincr
;
2108 dst
.maxhfit
<- src
.maxhfit
;
2109 dst
.crophack
<- src
.crophack
;
2110 dst
.autoscroll
<- src
.autoscroll
;
2111 dst
.showall
<- src
.showall
;
2112 dst
.hlinks
<- src
.hlinks
;
2113 dst
.underinfo
<- src
.underinfo
;
2114 dst
.interpagespace <- src
.interpagespace;
2115 dst
.zoom <- src
.zoom;
2116 dst
.presentation <- src
.presentation;
2117 dst
.angle
<- src
.angle
;
2118 dst
.winw
<- src
.winw
;
2119 dst
.winh
<- src
.winh
;
2120 dst
.savebmarks
<- src
.savebmarks
;
2121 dst
.proportional
<- src
.proportional
;
2125 let l = String.length
s in
2126 let b = Buffer.create
l in
2132 let h = Hashtbl.create
10 in
2133 let dc = { defconf with angle
= defconf.angle
} in
2134 let rec toplevel v t spos epos
=
2136 | Vdata
| Vcdata
| Vend
-> v
2137 | Vopen
("llppconfig", attrs
, closed
) ->
2140 else { v
with f = llppconfig
}
2142 error
"unexpected subelement at top level" s spos
2143 | Vclose tag
-> error
"unexpected close at toplevel" s spos
2145 and llppconfig v t spos epos
=
2147 | Vdata
| Vcdata
| Vend
-> v
2148 | Vopen
("defaults", attrs
, closed
) ->
2149 let c = config_of dc attrs
in
2153 else { v
with f = skip
"defaults" (fun () -> v
) }
2155 | Vopen
("doc", attrs
, closed
) ->
2158 List.assoc
"path" attrs
2159 with Not_found
-> error
"doc is missing path attribute" s spos
2161 let path = unent pathent in
2162 let c = config_of dc attrs
in
2165 float_of_string
(List.assoc
"rely" attrs
)
2169 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2174 int_of_string
(List.assoc
"pan" attrs
)
2178 dolog "error while accesing rely: %s" (Printexc.to_string exn
);
2182 then (Hashtbl.add
h path (c, [], x, y); v
)
2183 else { v
with f = doc
path x y c [] }
2185 | Vopen
(tag
, _
, closed
) ->
2186 error
"unexpected subelement in llppconfig" s spos
2188 | Vclose
"llppconfig" -> { v
with f = toplevel }
2189 | Vclose tag
-> error
"unexpected close in llppconfig" s spos
2191 and doc
path x y c bookmarks v t spos epos
=
2193 | Vdata
| Vcdata
-> v
2194 | Vend
-> error
"unexpected end of input in doc" s spos
2195 | Vopen
("bookmarks", attrs
, closed
) ->
2196 { v
with f = pbookmarks
path x y c bookmarks }
2198 | Vopen
(tag
, _
, _
) ->
2199 error
"unexpected subelement in doc" s spos
2202 Hashtbl.add
h path (c, List.rev
bookmarks, x, y);
2203 { v
with f = llppconfig
}
2205 | Vclose tag
-> error
"unexpected close in doc" s spos
2207 and pbookmarks
path x y c bookmarks v t spos epos
=
2209 | Vdata
| Vcdata
-> v
2210 | Vend
-> error
"unexpected end of input in bookmarks" s spos
2211 | Vopen
("item", attrs
, closed
) ->
2212 let titleent, spage
, srely
= bookmark_of attrs
in
2217 dolog "Failed to convert page %S to integer: %s"
2218 spage
(Printexc.to_string exn
);
2223 float_of_string srely
2225 dolog "Failed to convert rely %S to real: %s"
2226 srely
(Printexc.to_string exn
);
2229 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2231 then { v
with f = pbookmarks
path x y c bookmarks }
2234 { v
with f = skip
"item" f }
2237 error
"unexpected subelement in bookmarks" s spos
2239 | Vclose
"bookmarks" ->
2240 { v
with f = doc
path x y c bookmarks }
2242 | Vclose tag
-> error
"unexpected close in bookmarks" s spos
2244 and skip tag
f v t spos epos
=
2246 | Vdata
| Vcdata
-> v
2248 error
("unexpected end of input in skipped " ^ tag
) s spos
2249 | Vopen
(tag'
, _
, closed
) ->
2253 let f'
() = { v
with f = skip tag
f } in
2254 { v
with f = skip tag'
f'
}
2258 else error
("unexpected close in skipped " ^ tag
) s spos
2261 parse
{ f = toplevel; accu = () } s;
2267 let len = in_channel_length ic
in
2268 let s = String.create
len in
2269 really_input ic
s 0 len;
2272 | Parse_error
(msg
, s, pos
) ->
2273 let subs = subs s pos
in
2274 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
2275 failwith
("parse error: " ^
s)
2278 failwith
("config load error: " ^
Printexc.to_string exn
)
2284 let dir = Filename.concat
home ".config" in
2285 if Sys.is_directory
dir then dir else home
2288 Filename.concat
dir "llpp.conf"
2292 if Sys.file_exists
path
2295 (try Some
(open_in_bin
path)
2298 ("Error opening configuation file `" ^
path ^
"': " ^
2299 Printexc.to_string exn
);
2308 ("Error loading configuation from `" ^
path ^
"': " ^
2309 Printexc.to_string exn
);
2315 f (Hashtbl.create
0, defconf)
2320 let pc, pb
, px
, py =
2322 Hashtbl.find
h state.path
2323 with Not_found
-> dc, [], 0, 0.0
2327 state.bookmarks <- pb
;
2329 cbput state.hists
.nav
py;
2330 cbrfollowlen state.hists
.nav
;
2335 let add_attrs bb always
dc c =
2338 then Printf.bprintf bb
"\n %s='%b'" s a
2341 then Printf.bprintf bb
"\n %s='%d'" s a
2344 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
2348 then dc.winw
, dc.winh
2350 match state.fullscreen
with
2352 | None
-> c.winw
, c.winh
2354 oi
"width" w dc.winw
;
2355 oi
"height" h dc.winh
;
2356 oi
"scroll-bar-width" c.scrollw
dc.scrollw
;
2357 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
2358 ob "case-insensitive-search" c.icase
dc.icase
;
2359 ob "preload" c.preload dc.preload;
2360 oi
"page-bias" c.pagebias
dc.pagebias
;
2361 oi
"scroll-step" c.scrollincr
dc.scrollincr
;
2362 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
2363 ob "crop-hack" c.crophack
dc.crophack
;
2364 ob "throttle" c.showall
dc.showall
;
2365 ob "highlight-links" c.hlinks
dc.hlinks
;
2366 ob "under-cursor-info" c.underinfo
dc.underinfo
;
2367 oi
"vertical-margin" c.interpagespace dc.interpagespace;
2368 oz
"zoom" c.zoom dc.zoom;
2369 ob "presentation" c.presentation dc.presentation;
2370 oi
"rotation-angle" c.angle
dc.angle
;
2371 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
2372 ob "proportional-display" c.proportional
dc.proportional
;
2376 let bb = Buffer.create
32768 in
2378 Buffer.add_string
bb "<llppconfig>\n<defaults ";
2379 add_attrs bb true dc dc;
2380 Buffer.add_string
bb "/>\n";
2382 let adddoc path x y c bookmarks =
2383 if bookmarks == [] && c = dc && y = 0.0
2386 Printf.bprintf
bb "<doc path='%s'"
2387 (enent
path 0 (String.length
path));
2390 then Printf.bprintf
bb " rely='%f'" y;
2393 then Printf.bprintf
bb " pan='%d'" x;
2395 add_attrs bb false dc c;
2397 begin match bookmarks with
2398 | [] -> Buffer.add_string
bb "/>\n"
2400 Buffer.add_string
bb ">\n<bookmarks>\n";
2401 List.iter
(fun (title, _level
, page, rely) ->
2403 "<item title='%s' page='%d' rely='%f'/>\n"
2404 (enent
title 0 (String.length
title))
2408 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
2413 adddoc state.path state.x (yratio state.y) conf
2414 (if conf.savebmarks
then state.bookmarks else []);
2416 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
2417 if path <> state.path
2419 adddoc path x y c bookmarks
2421 Buffer.add_string
bb "</llppconfig>";
2424 if Buffer.length
bb > 0
2427 let tmp = path ^
".tmp" in
2428 let oc = open_out_bin
tmp in
2429 Buffer.output_buffer
oc bb;
2431 Sys.rename
tmp path;
2434 ("error while saving configuration: " ^
Printexc.to_string exn
)
2440 ["-p", Arg.String
(fun s -> state.password
<- s) , "password"]
2441 (fun s -> state.path <- s)
2442 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\noptions:")
2445 if String.length
state.path = 0
2446 then (prerr_endline
"filename missing"; exit
1)
2448 if Filename.is_relative
state.path
2449 then Filename.concat
(Sys.getcwd
()) state.path
2457 let _ = Glut.init
Sys.argv
in
2458 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
2459 let () = Glut.initWindowSize
conf.winw
conf.winh
in
2460 let _ = Glut.createWindow
("llpp " ^
Filename.basename
path) in
2463 if Sys.os_type
= "Unix"
2465 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
2467 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
2468 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2469 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
2470 Unix.bind
sock addr;
2472 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
2473 Unix.connect
csock addr;
2474 let ssock, _ = Unix.accept
sock in
2477 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
2478 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
2482 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
2486 let () = Glut.displayFunc
display in
2487 let () = Glut.reshapeFunc
reshape in
2488 let () = Glut.keyboardFunc
keyboard in
2489 let () = Glut.specialFunc
special in
2490 let () = Glut.idleFunc
(Some
idle) in
2491 let () = Glut.mouseFunc
mouse in
2492 let () = Glut.motionFunc
motion in
2493 let () = Glut.passiveMotionFunc
pmotion in
2496 state.csock <- csock;
2497 state.ssock <- ssock;
2498 state.text <- "Opening " ^
path;
2499 writeopen state.path state.password
;
2503 let rec handlelablglutbug () =
2506 with Glut.BadEnum
"key in special_of_int" ->
2507 showtext '
!'
" LablGlut bug: special key not recognized";
2508 handlelablglutbug ()
2510 handlelablglutbug ();