Account for cover page in rowyh
[llpp.git] / main.ml
bloba5340b486e25bac3a9d53929565533c9ecb629dd
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
45 type link =
46 | Lnotfound
47 | Lfound of int
48 and linkdir =
49 | LDfirst
50 | LDlast
51 | LDfirstvisible of (int * int * int)
52 | LDleft of int
53 | LDright of int
54 | LDdown of int
55 | LDup of int
58 type pagewithlinks =
59 | Pwlnotfound
60 | Pwl of int
63 type keymap =
64 | KMinsrt of key
65 | KMinsrl of key list
66 | KMmulti of key list * key list
67 and key = int * int
68 and keyhash = (key, keymap) Hashtbl.t
69 and keystate =
70 | KSnone
71 | KSinto of (key list * key list)
74 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
75 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
77 type pipe = (Unix.file_descr * Unix.file_descr);;
79 external init : pipe -> params -> unit = "ml_init";;
80 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
81 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
82 external getpdimrect : int -> float array = "ml_getpdimrect";;
83 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
84 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
85 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
86 external measurestr : int -> string -> float = "ml_measure_string";;
87 external getmaxw : unit -> float = "ml_getmaxw";;
88 external postprocess :
89 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
90 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
91 external platform : unit -> platform = "ml_platform";;
92 external setaalevel : int -> unit = "ml_setaalevel";;
93 external realloctexts : int -> bool = "ml_realloctexts";;
94 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
95 external findlink : opaque -> linkdir -> link = "ml_findlink";;
96 external getlink : opaque -> int -> under = "ml_getlink";;
97 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
98 external getlinkcount : opaque -> int = "ml_getlinkcount";;
99 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
100 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
102 let platform_to_string = function
103 | Punknown -> "unknown"
104 | Plinux -> "Linux"
105 | Posx -> "OSX"
106 | Psun -> "Sun"
107 | Pfreebsd -> "FreeBSD"
108 | Pdragonflybsd -> "DragonflyBSD"
109 | Popenbsd -> "OpenBSD"
110 | Pnetbsd -> "NetBSD"
111 | Pcygwin -> "Cygwin"
114 let platform = platform ();;
116 let popen cmd fda =
117 if platform = Pcygwin
118 then (
119 let sh = "/bin/sh" in
120 let args = [|sh; "-c"; cmd|] in
121 let rec std si so se = function
122 | [] -> si, so, se
123 | (fd, 0) :: rest -> std fd so se rest
124 | (fd, -1) :: rest ->
125 Unix.set_close_on_exec fd;
126 std si so se rest
127 | (_, n) :: _ ->
128 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
130 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
131 ignore (Unix.create_process sh args si so se)
133 else popen cmd fda;
136 type x = int
137 and y = int
138 and tilex = int
139 and tiley = int
140 and tileparams = (x * y * width * height * tilex * tiley)
143 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
145 type mpos = int * int
146 and mstate =
147 | Msel of (mpos * mpos)
148 | Mpan of mpos
149 | Mscrolly | Mscrollx
150 | Mzoom of (int * int)
151 | Mzoomrect of (mpos * mpos)
152 | Mnone
155 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
156 and onkey = string -> int -> te
157 and ondone = string -> unit
158 and histcancel = unit -> unit
159 and onhist = ((histcmd -> string) * histcancel)
160 and histcmd = HCnext | HCprev | HCfirst | HClast
161 and cancelonempty = bool
162 and te =
163 | TEstop
164 | TEdone of string
165 | TEcont of string
166 | TEswitch of textentry
169 type 'a circbuf =
170 { store : 'a array
171 ; mutable rc : int
172 ; mutable wc : int
173 ; mutable len : int
177 let bound v minv maxv =
178 max minv (min maxv v);
181 let cbnew n v =
182 { store = Array.create n v
183 ; rc = 0
184 ; wc = 0
185 ; len = 0
189 let drawstring size x y s =
190 Gl.enable `blend;
191 Gl.enable `texture_2d;
192 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
193 ignore (drawstr size x y s);
194 Gl.disable `blend;
195 Gl.disable `texture_2d;
198 let drawstring1 size x y s =
199 drawstr size x y s;
202 let drawstring2 size x y fmt =
203 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
206 let cbcap b = Array.length b.store;;
208 let cbput b v =
209 let cap = cbcap b in
210 b.store.(b.wc) <- v;
211 b.wc <- (b.wc + 1) mod cap;
212 b.rc <- b.wc;
213 b.len <- min (b.len + 1) cap;
216 let cbempty b = b.len = 0;;
218 let cbgetg b circular dir =
219 if cbempty b
220 then b.store.(0)
221 else
222 let rc = b.rc + dir in
223 let rc =
224 if circular
225 then (
226 if rc = -1
227 then b.len-1
228 else (
229 if rc = b.len
230 then 0
231 else rc
234 else max 0 (min rc (b.len-1))
236 b.rc <- rc;
237 b.store.(rc);
240 let cbget b = cbgetg b false;;
241 let cbgetc b = cbgetg b true;;
243 type page =
244 { pageno : int
245 ; pagedimno : int
246 ; pagew : int
247 ; pageh : int
248 ; pagex : int
249 ; pagey : int
250 ; pagevw : int
251 ; pagevh : int
252 ; pagedispx : int
253 ; pagedispy : int
254 ; pagecol : int
258 let debugl l =
259 dolog "l %d dim=%d {" l.pageno l.pagedimno;
260 dolog " WxH %dx%d" l.pagew l.pageh;
261 dolog " vWxH %dx%d" l.pagevw l.pagevh;
262 dolog " pagex,y %d,%d" l.pagex l.pagey;
263 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
264 dolog " column %d" l.pagecol;
265 dolog "}";
268 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
269 dolog "rect {";
270 dolog " x0,y0=(% f, % f)" x0 y0;
271 dolog " x1,y1=(% f, % f)" x1 y1;
272 dolog " x2,y2=(% f, % f)" x2 y2;
273 dolog " x3,y3=(% f, % f)" x3 y3;
274 dolog "}";
277 type multicolumns = multicol * pagegeom
278 and singlecolumn = pagegeom
279 and splitcolumns = columncount * pagegeom
280 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
281 and multicol = columncount * covercount * covercount
282 and pdimno = int
283 and columncount = int
284 and covercount = int;;
286 type conf =
287 { mutable scrollbw : int
288 ; mutable scrollh : int
289 ; mutable icase : bool
290 ; mutable preload : bool
291 ; mutable pagebias : int
292 ; mutable verbose : bool
293 ; mutable debug : bool
294 ; mutable scrollstep : int
295 ; mutable hscrollstep : int
296 ; mutable maxhfit : bool
297 ; mutable crophack : bool
298 ; mutable autoscrollstep : int
299 ; mutable maxwait : float option
300 ; mutable hlinks : bool
301 ; mutable underinfo : bool
302 ; mutable interpagespace : interpagespace
303 ; mutable zoom : float
304 ; mutable presentation : bool
305 ; mutable angle : angle
306 ; mutable winw : int
307 ; mutable winh : int
308 ; mutable savebmarks : bool
309 ; mutable proportional : proportional
310 ; mutable trimmargins : trimmargins
311 ; mutable trimfuzz : irect
312 ; mutable memlimit : memsize
313 ; mutable texcount : texcount
314 ; mutable sliceheight : sliceheight
315 ; mutable thumbw : width
316 ; mutable jumpback : bool
317 ; mutable bgcolor : float * float * float
318 ; mutable bedefault : bool
319 ; mutable scrollbarinpm : bool
320 ; mutable tilew : int
321 ; mutable tileh : int
322 ; mutable mustoresize : memsize
323 ; mutable checkers : bool
324 ; mutable aalevel : int
325 ; mutable urilauncher : string
326 ; mutable pathlauncher : string
327 ; mutable colorspace : colorspace
328 ; mutable invert : bool
329 ; mutable colorscale : float
330 ; mutable redirectstderr : bool
331 ; mutable ghyllscroll : (int * int * int) option
332 ; mutable columns : columns
333 ; mutable beyecolumns : columncount option
334 ; mutable selcmd : string
335 ; mutable updatecurs : bool
336 ; mutable keyhashes : (string * keyhash) list
337 ; mutable hfsize : int
338 ; mutable pgscale : float
339 ; mutable multicenter : bool
341 and columns =
342 | Csingle of singlecolumn
343 | Cmulti of multicolumns
344 | Csplit of splitcolumns
347 type anchor = pageno * top * dtop;;
349 type outline = string * int * anchor;;
351 type rect = float * float * float * float * float * float * float * float;;
353 type tile = opaque * pixmapsize * elapsed
354 and elapsed = float;;
355 type pagemapkey = pageno * gen;;
356 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
357 and row = int
358 and col = int;;
360 let emptyanchor = (0, 0.0, 0.0);;
362 type infochange = | Memused | Docinfo | Pdim;;
364 class type uioh = object
365 method display : unit
366 method key : int -> int -> uioh
367 method button : int -> bool -> int -> int -> int -> uioh
368 method motion : int -> int -> uioh
369 method pmotion : int -> int -> uioh
370 method infochanged : infochange -> unit
371 method scrollpw : (int * float * float)
372 method scrollph : (int * float * float)
373 method modehash : keyhash
374 end;;
376 type mode =
377 | Birdseye of (conf * leftx * pageno * pageno * anchor)
378 | Textentry of (textentry * onleave)
379 | View
380 | LinkNav of linktarget
381 and onleave = leavetextentrystatus -> unit
382 and leavetextentrystatus = | Cancel | Confirm
383 and helpitem = string * int * action
384 and action =
385 | Noaction
386 | Action of (uioh -> uioh)
387 and linktarget =
388 | Ltexact of (pageno * int)
389 | Ltgendir of int
392 let isbirdseye = function Birdseye _ -> true | _ -> false;;
393 let istextentry = function Textentry _ -> true | _ -> false;;
395 type currently =
396 | Idle
397 | Loading of (page * gen)
398 | Tiling of (
399 page * opaque * colorspace * angle * gen * col * row * width * height
401 | Outlining of outline list
404 let emptykeyhash = Hashtbl.create 0;;
405 let nouioh : uioh = object (self)
406 method display = ()
407 method key _ _ = self
408 method button _ _ _ _ _ = self
409 method motion _ _ = self
410 method pmotion _ _ = self
411 method infochanged _ = ()
412 method scrollpw = (0, nan, nan)
413 method scrollph = (0, nan, nan)
414 method modehash = emptykeyhash
415 end;;
417 type state =
418 { mutable sr : Unix.file_descr
419 ; mutable sw : Unix.file_descr
420 ; mutable wsfd : Unix.file_descr
421 ; mutable errfd : Unix.file_descr option
422 ; mutable stderr : Unix.file_descr
423 ; mutable errmsgs : Buffer.t
424 ; mutable newerrmsgs : bool
425 ; mutable w : int
426 ; mutable x : int
427 ; mutable y : int
428 ; mutable scrollw : int
429 ; mutable hscrollh : int
430 ; mutable anchor : anchor
431 ; mutable ranchors : (string * string * anchor) list
432 ; mutable maxy : int
433 ; mutable layout : page list
434 ; pagemap : (pagemapkey, opaque) Hashtbl.t
435 ; tilemap : (tilemapkey, tile) Hashtbl.t
436 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
437 ; mutable pdims : (pageno * width * height * leftx) list
438 ; mutable pagecount : int
439 ; mutable currently : currently
440 ; mutable mstate : mstate
441 ; mutable searchpattern : string
442 ; mutable rects : (pageno * recttype * rect) list
443 ; mutable rects1 : (pageno * recttype * rect) list
444 ; mutable text : string
445 ; mutable fullscreen : (width * height) option
446 ; mutable mode : mode
447 ; mutable uioh : uioh
448 ; mutable outlines : outline array
449 ; mutable bookmarks : outline list
450 ; mutable path : string
451 ; mutable password : string
452 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
453 ; mutable memused : memsize
454 ; mutable gen : gen
455 ; mutable throttle : (page list * int * float) option
456 ; mutable autoscroll : int option
457 ; mutable ghyll : (int option -> unit)
458 ; mutable help : helpitem array
459 ; mutable docinfo : (int * string) list
460 ; mutable texid : GlTex.texture_id option
461 ; hists : hists
462 ; mutable prevzoom : float
463 ; mutable progress : float
464 ; mutable redisplay : bool
465 ; mutable mpos : mpos
466 ; mutable keystate : keystate
467 ; mutable glinks : bool
468 ; mutable prevcolumns : (columns * float) option
470 and hists =
471 { pat : string circbuf
472 ; pag : string circbuf
473 ; nav : anchor circbuf
474 ; sel : string circbuf
478 let defconf =
479 { scrollbw = 7
480 ; scrollh = 12
481 ; icase = true
482 ; preload = true
483 ; pagebias = 0
484 ; verbose = false
485 ; debug = false
486 ; scrollstep = 24
487 ; hscrollstep = 24
488 ; maxhfit = true
489 ; crophack = false
490 ; autoscrollstep = 2
491 ; maxwait = None
492 ; hlinks = false
493 ; underinfo = false
494 ; interpagespace = 2
495 ; zoom = 1.0
496 ; presentation = false
497 ; angle = 0
498 ; winw = 900
499 ; winh = 900
500 ; savebmarks = true
501 ; proportional = true
502 ; trimmargins = false
503 ; trimfuzz = (0,0,0,0)
504 ; memlimit = 32 lsl 20
505 ; texcount = 256
506 ; sliceheight = 24
507 ; thumbw = 76
508 ; jumpback = true
509 ; bgcolor = (0.5, 0.5, 0.5)
510 ; bedefault = false
511 ; scrollbarinpm = true
512 ; tilew = 2048
513 ; tileh = 2048
514 ; mustoresize = 256 lsl 20
515 ; checkers = true
516 ; aalevel = 8
517 ; urilauncher =
518 (match platform with
519 | Plinux | Pfreebsd | Pdragonflybsd
520 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
521 | Posx -> "open \"%s\""
522 | Pcygwin -> "cygstart \"%s\""
523 | Punknown -> "echo %s")
524 ; pathlauncher = "lp \"%s\""
525 ; selcmd =
526 (match platform with
527 | Plinux | Pfreebsd | Pdragonflybsd
528 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
529 | Posx -> "pbcopy"
530 | Pcygwin -> "wsel"
531 | Punknown -> "cat")
532 ; colorspace = Rgb
533 ; invert = false
534 ; colorscale = 1.0
535 ; redirectstderr = false
536 ; ghyllscroll = None
537 ; columns = Csingle [||]
538 ; beyecolumns = None
539 ; updatecurs = false
540 ; hfsize = 12
541 ; pgscale = 1.0
542 ; multicenter = false
543 ; keyhashes =
544 let mk n = (n, Hashtbl.create 1) in
545 [ mk "global"
546 ; mk "info"
547 ; mk "help"
548 ; mk "outline"
549 ; mk "listview"
550 ; mk "birdseye"
551 ; mk "textentry"
552 ; mk "links"
553 ; mk "view"
558 let findkeyhash c name =
559 try List.assoc name c.keyhashes
560 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
563 let conf = { defconf with angle = defconf.angle };;
565 let pgscale h = truncate (float h *. conf.pgscale);;
567 type fontstate =
568 { mutable fontsize : int
569 ; mutable wwidth : float
570 ; mutable maxrows : int
574 let fstate =
575 { fontsize = 14
576 ; wwidth = nan
577 ; maxrows = -1
581 let setfontsize n =
582 fstate.fontsize <- n;
583 fstate.wwidth <- measurestr fstate.fontsize "w";
584 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
587 let geturl s =
588 let colonpos = try String.index s ':' with Not_found -> -1 in
589 let len = String.length s in
590 if colonpos >= 0 && colonpos + 3 < len
591 then (
592 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
593 then
594 let schemestartpos =
595 try String.rindex_from s colonpos ' '
596 with Not_found -> -1
598 let scheme =
599 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
601 match scheme with
602 | "http" | "ftp" | "mailto" ->
603 let epos =
604 try String.index_from s colonpos ' '
605 with Not_found -> len
607 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
608 | _ -> ""
609 else ""
611 else ""
614 let gotouri uri =
615 if String.length conf.urilauncher = 0
616 then print_endline uri
617 else (
618 let url = geturl uri in
619 if String.length url = 0
620 then print_endline uri
621 else
622 let re = Str.regexp "%s" in
623 let command = Str.global_replace re url conf.urilauncher in
624 try popen command []
625 with exn ->
626 Printf.eprintf
627 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
628 flush stderr;
632 let version () =
633 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
634 (platform_to_string platform) Sys.word_size Sys.ocaml_version
637 let makehelp () =
638 let strings = version () :: "" :: Help.keys in
639 Array.of_list (
640 List.map (fun s ->
641 let url = geturl s in
642 if String.length url > 0
643 then (s, 0, Action (fun u -> gotouri url; u))
644 else (s, 0, Noaction)
645 ) strings);
648 let noghyll _ = ();;
649 let firstgeomcmds = "", [];;
651 let state =
652 { sr = Unix.stdin
653 ; sw = Unix.stdin
654 ; wsfd = Unix.stdin
655 ; errfd = None
656 ; stderr = Unix.stderr
657 ; errmsgs = Buffer.create 0
658 ; newerrmsgs = false
659 ; x = 0
660 ; y = 0
661 ; w = 0
662 ; scrollw = 0
663 ; hscrollh = 0
664 ; anchor = emptyanchor
665 ; ranchors = []
666 ; layout = []
667 ; maxy = max_int
668 ; tilelru = Queue.create ()
669 ; pagemap = Hashtbl.create 10
670 ; tilemap = Hashtbl.create 10
671 ; pdims = []
672 ; pagecount = 0
673 ; currently = Idle
674 ; mstate = Mnone
675 ; rects = []
676 ; rects1 = []
677 ; text = ""
678 ; mode = View
679 ; fullscreen = None
680 ; searchpattern = ""
681 ; outlines = [||]
682 ; bookmarks = []
683 ; path = ""
684 ; password = ""
685 ; geomcmds = firstgeomcmds
686 ; hists =
687 { nav = cbnew 10 emptyanchor
688 ; pat = cbnew 10 ""
689 ; pag = cbnew 10 ""
690 ; sel = cbnew 10 ""
692 ; memused = 0
693 ; gen = 0
694 ; throttle = None
695 ; autoscroll = None
696 ; ghyll = noghyll
697 ; help = makehelp ()
698 ; docinfo = []
699 ; texid = None
700 ; prevzoom = 1.0
701 ; progress = -1.0
702 ; uioh = nouioh
703 ; redisplay = true
704 ; mpos = (-1, -1)
705 ; keystate = KSnone
706 ; glinks = false
707 ; prevcolumns = None
711 let vlog fmt =
712 if conf.verbose
713 then
714 Printf.kprintf prerr_endline fmt
715 else
716 Printf.kprintf ignore fmt
719 let launchpath () =
720 if String.length conf.pathlauncher = 0
721 then print_endline state.path
722 else (
723 let re = Str.regexp "%s" in
724 let command = Str.global_replace re state.path conf.pathlauncher in
725 try popen command []
726 with exn ->
727 Printf.eprintf
728 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
729 flush stderr;
733 module Ne = struct
734 type 'a t = | Res of 'a | Exn of exn;;
736 let pipe () =
737 try Res (Unix.pipe ())
738 with exn -> Exn exn
741 let clo fd f =
742 try Unix.close fd
743 with exn -> f (Printexc.to_string exn)
746 let dup fd =
747 try Res (Unix.dup fd)
748 with exn -> Exn exn
751 let dup2 fd1 fd2 =
752 try Res (Unix.dup2 fd1 fd2)
753 with exn -> Exn exn
755 end;;
757 let redirectstderr () =
758 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
759 if conf.redirectstderr
760 then
761 match Ne.pipe () with
762 | Ne.Exn exn ->
763 dolog "failed to create stderr redirection pipes: %s"
764 (Printexc.to_string exn)
766 | Ne.Res (r, w) ->
767 begin match Ne.dup Unix.stderr with
768 | Ne.Exn exn ->
769 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
770 Ne.clo r (clofail "pipe/r");
771 Ne.clo w (clofail "pipe/w");
773 | Ne.Res dupstderr ->
774 begin match Ne.dup2 w Unix.stderr with
775 | Ne.Exn exn ->
776 dolog "failed to dup2 to stderr: %s"
777 (Printexc.to_string exn);
778 Ne.clo dupstderr (clofail "stderr duplicate");
779 Ne.clo r (clofail "redir pipe/r");
780 Ne.clo w (clofail "redir pipe/w");
782 | Ne.Res () ->
783 state.stderr <- dupstderr;
784 state.errfd <- Some r;
785 end;
787 else (
788 state.newerrmsgs <- false;
789 begin match state.errfd with
790 | Some fd ->
791 begin match Ne.dup2 state.stderr Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup2 original stderr: %s"
794 (Printexc.to_string exn)
795 | Ne.Res () ->
796 Ne.clo fd (clofail "dup of stderr");
797 Unix.dup2 state.stderr Unix.stderr;
798 state.errfd <- None;
799 end;
800 | None -> ()
801 end;
802 prerr_string (Buffer.contents state.errmsgs);
803 flush stderr;
804 Buffer.clear state.errmsgs;
808 module G =
809 struct
810 let postRedisplay who =
811 if conf.verbose
812 then prerr_endline ("redisplay for " ^ who);
813 state.redisplay <- true;
815 end;;
817 let getopaque pageno =
818 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
819 with Not_found -> None
822 let putopaque pageno opaque =
823 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
826 let pagetranslatepoint l x y =
827 let dy = y - l.pagedispy in
828 let y = dy + l.pagey in
829 let dx = x - l.pagedispx in
830 let x = dx + l.pagex in
831 (x, y);
834 let getunder x y =
835 let rec f = function
836 | l :: rest ->
837 begin match getopaque l.pageno with
838 | Some opaque ->
839 let x0 = l.pagedispx in
840 let x1 = x0 + l.pagevw in
841 let y0 = l.pagedispy in
842 let y1 = y0 + l.pagevh in
843 if y >= y0 && y <= y1 && x >= x0 && x <= x1
844 then
845 let px, py = pagetranslatepoint l x y in
846 match whatsunder opaque px py with
847 | Unone -> f rest
848 | under -> under
849 else f rest
850 | _ ->
851 f rest
853 | [] -> Unone
855 f state.layout
858 let showtext c s =
859 state.text <- Printf.sprintf "%c%s" c s;
860 G.postRedisplay "showtext";
863 let undertext = function
864 | Unone -> "none"
865 | Ulinkuri s -> s
866 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
867 | Utext s -> "font: " ^ s
868 | Uunexpected s -> "unexpected: " ^ s
869 | Ulaunch s -> "launch: " ^ s
870 | Unamed s -> "named: " ^ s
871 | Uremote (filename, pageno) ->
872 Printf.sprintf "%s: page %d" filename (pageno+1)
875 let updateunder x y =
876 match getunder x y with
877 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
878 | Ulinkuri uri ->
879 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
880 Wsi.setcursor Wsi.CURSOR_INFO
881 | Ulinkgoto (pageno, _) ->
882 if conf.underinfo
883 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
884 Wsi.setcursor Wsi.CURSOR_INFO
885 | Utext s ->
886 if conf.underinfo then showtext 'f' ("ont: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_TEXT
888 | Uunexpected s ->
889 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
890 Wsi.setcursor Wsi.CURSOR_INHERIT
891 | Ulaunch s ->
892 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
893 Wsi.setcursor Wsi.CURSOR_INHERIT
894 | Unamed s ->
895 if conf.underinfo then showtext 'n' ("amed: " ^ s);
896 Wsi.setcursor Wsi.CURSOR_INHERIT
897 | Uremote (filename, pageno) ->
898 if conf.underinfo then showtext 'r'
899 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
900 Wsi.setcursor Wsi.CURSOR_INFO
903 let showlinktype under =
904 if conf.underinfo
905 then
906 match under with
907 | Unone -> ()
908 | under ->
909 let s = undertext under in
910 showtext ' ' s
913 let addchar s c =
914 let b = Buffer.create (String.length s + 1) in
915 Buffer.add_string b s;
916 Buffer.add_char b c;
917 Buffer.contents b;
920 let colorspace_of_string s =
921 match String.lowercase s with
922 | "rgb" -> Rgb
923 | "bgr" -> Bgr
924 | "gray" -> Gray
925 | _ -> failwith "invalid colorspace"
928 let int_of_colorspace = function
929 | Rgb -> 0
930 | Bgr -> 1
931 | Gray -> 2
934 let colorspace_of_int = function
935 | 0 -> Rgb
936 | 1 -> Bgr
937 | 2 -> Gray
938 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
941 let colorspace_to_string = function
942 | Rgb -> "rgb"
943 | Bgr -> "bgr"
944 | Gray -> "gray"
947 let intentry_with_suffix text key =
948 let c =
949 if key >= 32 && key < 127
950 then Char.chr key
951 else '\000'
953 match Char.lowercase c with
954 | '0' .. '9' ->
955 let text = addchar text c in
956 TEcont text
958 | 'k' | 'm' | 'g' ->
959 let text = addchar text c in
960 TEcont text
962 | _ ->
963 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
964 TEcont text
967 let multicolumns_to_string (n, a, b) =
968 if a = 0 && b = 0
969 then Printf.sprintf "%d" n
970 else Printf.sprintf "%d,%d,%d" n a b;
973 let multicolumns_of_string s =
975 (int_of_string s, 0, 0)
976 with _ ->
977 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
978 if a > 1 || b > 1
979 then failwith "subtly broken"; (n, a, b)
983 let readcmd fd =
984 let s = "xxxx" in
985 let n = Unix.read fd s 0 4 in
986 if n != 4 then failwith "incomplete read(len)";
987 let len = 0
988 lor (Char.code s.[0] lsl 24)
989 lor (Char.code s.[1] lsl 16)
990 lor (Char.code s.[2] lsl 8)
991 lor (Char.code s.[3] lsl 0)
993 let s = String.create len in
994 let n = Unix.read fd s 0 len in
995 if n != len then failwith "incomplete read(data)";
999 let btod b = if b then 1 else 0;;
1001 let wcmd fmt =
1002 let b = Buffer.create 16 in
1003 Buffer.add_string b "llll";
1004 Printf.kbprintf
1005 (fun b ->
1006 let s = Buffer.contents b in
1007 let n = String.length s in
1008 let len = n - 4 in
1009 (* dolog "wcmd %S" (String.sub s 4 len); *)
1010 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1011 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1012 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1013 s.[3] <- Char.chr (len land 0xff);
1014 let n' = Unix.write state.sw s 0 n in
1015 if n' != n then failwith "write failed";
1016 ) b fmt;
1019 let calcips h =
1020 if conf.presentation
1021 then
1022 let d = conf.winh - h in
1023 max conf.interpagespace ((d + 1) / 2)
1024 else
1025 conf.interpagespace
1028 let calcheight () =
1029 match conf.columns with
1030 | Cmulti ((c, _, coverB), b) ->
1031 if coverB > 0
1032 then
1033 if Array.length b > 0
1034 then
1035 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1036 y + h
1037 else 0
1038 else
1039 let rec loop y h n =
1040 if n < 0
1041 then loop y h (n+1)
1042 else (
1043 if n = Array.length b
1044 then y + h
1045 else
1046 let (_, _, y', (_, _, h', _)) = b.(n) in
1047 let y = min y y'
1048 and h = max h h' in
1049 loop y h (n+1)
1052 loop max_int 0 (((Array.length b - 1) / c) * c)
1053 | Csingle b ->
1054 if Array.length b > 0
1055 then
1056 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1057 y + h + (if conf.presentation then calcips h else 0)
1058 else 0
1059 | Csplit (_, b) ->
1060 if Array.length b > 0
1061 then
1062 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1063 y + h
1064 else 0
1067 let rowyh (c, coverA, coverB) b n =
1068 if c = 1 || (n < coverA || n > state.pagecount - coverB)
1069 then
1070 let _, _, vy, (_, _, h, _) = b.(n) in
1071 (vy, h)
1072 else
1073 let n' = n - coverA in
1074 let d = n' mod c in
1075 let s = n - d in
1076 let e = s + c in
1077 let rec find m miny maxh = if m = e then miny, maxh else
1078 let _, _, y, (_, _, h, _) = b.(m) in
1079 let miny = min miny y in
1080 let maxh = max maxh h in
1081 find (m+1) miny maxh
1082 in find s max_int 0
1085 let getpageyh pageno =
1086 let pageno = bound pageno 0 (state.pagecount-1) in
1087 match conf.columns with
1088 | Csingle b ->
1089 if Array.length b = 0
1090 then 0, 0
1091 else
1092 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1093 let y =
1094 if conf.presentation
1095 then y - calcips h
1096 else y
1098 y, h
1099 | Cmulti (cl, b) ->
1100 if Array.length b = 0
1101 then 0, 0
1102 else rowyh cl b pageno
1103 | Csplit (c, b) ->
1104 if Array.length b = 0
1105 then 0, 0
1106 else
1107 let n = pageno*c in
1108 let (_, _, y, (_, _, h, _)) = b.(n) in
1109 y, h
1112 let getpagedim pageno =
1113 let rec f ppdim l =
1114 match l with
1115 | (n, _, _, _) as pdim :: rest ->
1116 if n >= pageno
1117 then (if n = pageno then pdim else ppdim)
1118 else f pdim rest
1120 | [] -> ppdim
1122 f (-1, -1, -1, -1) state.pdims
1125 let getpagey pageno = fst (getpageyh pageno);;
1127 let nogeomcmds cmds =
1128 match cmds with
1129 | s, [] -> String.length s = 0
1130 | _ -> false
1133 let page_of_y y =
1134 let ((c, coverA, coverB) as cl), b =
1135 match conf.columns with
1136 | Csingle b -> (1, 0, 0), b
1137 | Cmulti (c, b) -> c, b
1138 | Csplit (_, b) -> (1, 0, 0), b
1140 let rec bsearch nmin nmax =
1141 if nmin > nmax
1142 then bound nmin 0 (state.pagecount-1)
1143 else
1144 let n = (nmax + nmin) / 2 in
1145 let vy, h = rowyh cl b n in
1146 let y0, y1 =
1147 if c = 1 && conf.presentation
1148 then
1149 let ips = calcips h in
1150 let y0 = vy - ips in
1151 let y1 = vy + h + ips in
1152 y0, y1
1153 else (
1154 if n = 0
1155 then 0, vy + h + conf.interpagespace
1156 else
1157 let y0 = vy - conf.interpagespace in
1158 y0, y0 + h + conf.interpagespace
1161 if y >= y0 && y < y1
1162 then (
1163 if c = 1
1164 then n
1165 else (
1166 if n > coverA
1167 then
1168 if n < state.pagecount - coverB
1169 then ((n-coverA)/c)*c + coverA
1170 else n
1171 else n
1174 else (
1175 if y > y0
1176 then bsearch (n+1) nmax
1177 else bsearch nmin (n-1)
1180 let r = bsearch 0 (state.pagecount-1) in
1184 let layoutN ((columns, coverA, coverB), b) y sh =
1185 let sh = sh - state.hscrollh in
1186 let rec fold accu n =
1187 if n = Array.length b
1188 then accu
1189 else
1190 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1191 if (vy - y) > sh &&
1192 (n = coverA - 1
1193 || n = state.pagecount - coverB
1194 || (n - coverA) mod columns = columns - 1)
1195 then accu
1196 else
1197 let accu =
1198 if vy + h > y
1199 then
1200 let pagey = max 0 (y - vy) in
1201 let pagedispy = if pagey > 0 then 0 else vy - y in
1202 let pagedispx, pagex =
1203 let pdx =
1204 if n = coverA - 1 || n = state.pagecount - coverB
1205 then state.x + (conf.winw - state.scrollw - w) / 2
1206 else dx + xoff + state.x
1208 if pdx < 0
1209 then 0, -pdx
1210 else pdx, 0
1212 let pagevw =
1213 let vw = conf.winw - state.scrollw - pagedispx in
1214 let pw = w - pagex in
1215 min vw pw
1217 let pagevh = min (h - pagey) (sh - pagedispy) in
1218 if pagevw > 0 && pagevh > 0
1219 then
1220 let e =
1221 { pageno = n
1222 ; pagedimno = pdimno
1223 ; pagew = w
1224 ; pageh = h
1225 ; pagex = pagex
1226 ; pagey = pagey
1227 ; pagevw = pagevw
1228 ; pagevh = pagevh
1229 ; pagedispx = pagedispx
1230 ; pagedispy = pagedispy
1231 ; pagecol = 0
1234 e :: accu
1235 else
1236 accu
1237 else
1238 accu
1240 fold accu (n+1)
1242 List.rev (fold [] (page_of_y y));
1245 let layoutS (columns, b) y sh =
1246 let sh = sh - state.hscrollh in
1247 let rec fold accu n =
1248 if n = Array.length b
1249 then accu
1250 else
1251 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1252 if (vy - y) > sh
1253 then accu
1254 else
1255 let accu =
1256 if vy + pageh > y
1257 then
1258 let x = xoff + state.x in
1259 let pagey = max 0 (y - vy) in
1260 let pagedispy = if pagey > 0 then 0 else vy - y in
1261 let pagedispx, pagex =
1262 if px = 0
1263 then (
1264 if x < 0
1265 then 0, -x
1266 else x, 0
1268 else (
1269 let px = px - x in
1270 if px < 0
1271 then -px, 0
1272 else 0, px
1275 let pagecolw = pagew/columns in
1276 let pagedispx =
1277 if pagecolw < conf.winw
1278 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1279 else pagedispx
1281 let pagevw =
1282 let vw = conf.winw - pagedispx - state.scrollw in
1283 let pw = pagew - pagex in
1284 min vw pw
1286 let pagevw = min pagevw pagecolw in
1287 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1288 if pagevw > 0 && pagevh > 0
1289 then
1290 let e =
1291 { pageno = n/columns
1292 ; pagedimno = pdimno
1293 ; pagew = pagew
1294 ; pageh = pageh
1295 ; pagex = pagex
1296 ; pagey = pagey
1297 ; pagevw = pagevw
1298 ; pagevh = pagevh
1299 ; pagedispx = pagedispx
1300 ; pagedispy = pagedispy
1301 ; pagecol = n mod columns
1304 e :: accu
1305 else
1306 accu
1307 else
1308 accu
1310 fold accu (n+1)
1312 List.rev (fold [] 0)
1315 let layout y sh =
1316 if nogeomcmds state.geomcmds
1317 then
1318 match conf.columns with
1319 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1320 | Cmulti c -> layoutN c y sh
1321 | Csplit s -> layoutS s y sh
1322 else []
1325 let clamp incr =
1326 let y = state.y + incr in
1327 let y = max 0 y in
1328 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1332 let itertiles l f =
1333 let tilex = l.pagex mod conf.tilew in
1334 let tiley = l.pagey mod conf.tileh in
1336 let col = l.pagex / conf.tilew in
1337 let row = l.pagey / conf.tileh in
1339 let rec rowloop row y0 dispy h =
1340 if h = 0
1341 then ()
1342 else (
1343 let dh = conf.tileh - y0 in
1344 let dh = min h dh in
1345 let rec colloop col x0 dispx w =
1346 if w = 0
1347 then ()
1348 else (
1349 let dw = conf.tilew - x0 in
1350 let dw = min w dw in
1352 f col row dispx dispy x0 y0 dw dh;
1353 colloop (col+1) 0 (dispx+dw) (w-dw)
1356 colloop col tilex l.pagedispx l.pagevw;
1357 rowloop (row+1) 0 (dispy+dh) (h-dh)
1360 if l.pagevw > 0 && l.pagevh > 0
1361 then rowloop row tiley l.pagedispy l.pagevh;
1364 let gettileopaque l col row =
1365 let key =
1366 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1368 try Some (Hashtbl.find state.tilemap key)
1369 with Not_found -> None
1372 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1373 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1374 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1377 let drawtiles l color =
1378 GlDraw.color color;
1379 let f col row x y tilex tiley w h =
1380 match gettileopaque l col row with
1381 | Some (opaque, _, t) ->
1382 let params = x, y, w, h, tilex, tiley in
1383 if conf.invert
1384 then (
1385 Gl.enable `blend;
1386 GlFunc.blend_func `zero `one_minus_src_color;
1388 drawtile params opaque;
1389 if conf.invert
1390 then Gl.disable `blend;
1391 if conf.debug
1392 then (
1393 let s = Printf.sprintf
1394 "%d[%d,%d] %f sec"
1395 l.pageno col row t
1397 let w = measurestr fstate.fontsize s in
1398 GlMisc.push_attrib [`current];
1399 GlDraw.color (0.0, 0.0, 0.0);
1400 GlDraw.rect
1401 (float (x-2), float (y-2))
1402 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1403 GlDraw.color (1.0, 1.0, 1.0);
1404 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1405 GlMisc.pop_attrib ();
1408 | _ ->
1409 let w =
1410 let lw = conf.winw - state.scrollw - x in
1411 min lw w
1412 and h =
1413 let lh = conf.winh - y in
1414 min lh h
1416 begin match state.texid with
1417 | Some id ->
1418 Gl.enable `texture_2d;
1419 GlTex.bind_texture `texture_2d id;
1420 let x0 = float x
1421 and y0 = float y
1422 and x1 = float (x+w)
1423 and y1 = float (y+h) in
1425 let tw = float w /. 64.0
1426 and th = float h /. 64.0 in
1427 let tx0 = float tilex /. 64.0
1428 and ty0 = float tiley /. 64.0 in
1429 let tx1 = tx0 +. tw
1430 and ty1 = ty0 +. th in
1431 GlDraw.begins `quads;
1432 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1433 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1434 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1435 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1436 GlDraw.ends ();
1438 Gl.disable `texture_2d;
1439 | None ->
1440 GlDraw.color (1.0, 1.0, 1.0);
1441 GlDraw.rect
1442 (float x, float y)
1443 (float (x+w), float (y+h));
1444 end;
1445 if w > 128 && h > fstate.fontsize + 10
1446 then (
1447 GlDraw.color (0.0, 0.0, 0.0);
1448 let c, r =
1449 if conf.verbose
1450 then (col*conf.tilew, row*conf.tileh)
1451 else col, row
1453 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1455 GlDraw.color color;
1457 itertiles l f
1460 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1462 let tilevisible1 l x y =
1463 let ax0 = l.pagex
1464 and ax1 = l.pagex + l.pagevw
1465 and ay0 = l.pagey
1466 and ay1 = l.pagey + l.pagevh in
1468 let bx0 = x
1469 and by0 = y in
1470 let bx1 = min (bx0 + conf.tilew) l.pagew
1471 and by1 = min (by0 + conf.tileh) l.pageh in
1473 let rx0 = max ax0 bx0
1474 and ry0 = max ay0 by0
1475 and rx1 = min ax1 bx1
1476 and ry1 = min ay1 by1 in
1478 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1479 nonemptyintersection
1482 let tilevisible layout n x y =
1483 let rec findpageinlayout m = function
1484 | l :: rest when l.pageno = n ->
1485 tilevisible1 l x y || (
1486 match conf.columns with
1487 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1488 | _ -> false
1490 | _ :: rest -> findpageinlayout 0 rest
1491 | [] -> false
1493 findpageinlayout 0 layout;
1496 let tileready l x y =
1497 tilevisible1 l x y &&
1498 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1501 let tilepage n p layout =
1502 let rec loop = function
1503 | l :: rest ->
1504 if l.pageno = n
1505 then
1506 let f col row _ _ _ _ _ _ =
1507 if state.currently = Idle
1508 then
1509 match gettileopaque l col row with
1510 | Some _ -> ()
1511 | None ->
1512 let x = col*conf.tilew
1513 and y = row*conf.tileh in
1514 let w =
1515 let w = l.pagew - x in
1516 min w conf.tilew
1518 let h =
1519 let h = l.pageh - y in
1520 min h conf.tileh
1522 wcmd "tile %s %d %d %d %d" p x y w h;
1523 state.currently <-
1524 Tiling (
1525 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1526 conf.tilew, conf.tileh
1529 itertiles l f;
1530 else
1531 loop rest
1533 | [] -> ()
1535 if nogeomcmds state.geomcmds
1536 then loop layout;
1539 let preloadlayout y =
1540 let y = if y < conf.winh then 0 else y - conf.winh in
1541 let h = conf.winh*3 in
1542 layout y h;
1545 let load pages =
1546 let rec loop pages =
1547 if state.currently != Idle
1548 then ()
1549 else
1550 match pages with
1551 | l :: rest ->
1552 begin match getopaque l.pageno with
1553 | None ->
1554 wcmd "page %d %d" l.pageno l.pagedimno;
1555 state.currently <- Loading (l, state.gen);
1556 | Some opaque ->
1557 tilepage l.pageno opaque pages;
1558 loop rest
1559 end;
1560 | _ -> ()
1562 if nogeomcmds state.geomcmds
1563 then loop pages
1566 let preload pages =
1567 load pages;
1568 if conf.preload && state.currently = Idle
1569 then load (preloadlayout state.y);
1572 let layoutready layout =
1573 let rec fold all ls =
1574 all && match ls with
1575 | l :: rest ->
1576 let seen = ref false in
1577 let allvisible = ref true in
1578 let foo col row _ _ _ _ _ _ =
1579 seen := true;
1580 allvisible := !allvisible &&
1581 begin match gettileopaque l col row with
1582 | Some _ -> true
1583 | None -> false
1586 itertiles l foo;
1587 fold (!seen && !allvisible) rest
1588 | [] -> true
1590 let alltilesvisible = fold true layout in
1591 alltilesvisible;
1594 let gotoy y =
1595 let y = bound y 0 state.maxy in
1596 let y, layout, proceed =
1597 match conf.maxwait with
1598 | Some time when state.ghyll == noghyll ->
1599 begin match state.throttle with
1600 | None ->
1601 let layout = layout y conf.winh in
1602 let ready = layoutready layout in
1603 if not ready
1604 then (
1605 load layout;
1606 state.throttle <- Some (layout, y, now ());
1608 else G.postRedisplay "gotoy showall (None)";
1609 y, layout, ready
1610 | Some (_, _, started) ->
1611 let dt = now () -. started in
1612 if dt > time
1613 then (
1614 state.throttle <- None;
1615 let layout = layout y conf.winh in
1616 load layout;
1617 G.postRedisplay "maxwait";
1618 y, layout, true
1620 else -1, [], false
1623 | _ ->
1624 let layout = layout y conf.winh in
1625 if true || layoutready layout
1626 then G.postRedisplay "gotoy ready";
1627 y, layout, true
1629 if proceed
1630 then (
1631 state.y <- y;
1632 state.layout <- layout;
1633 begin match state.mode with
1634 | LinkNav (Ltexact (pageno, linkno)) ->
1635 let rec loop = function
1636 | [] ->
1637 state.mode <- LinkNav (Ltgendir 0)
1638 | l :: _ when l.pageno = pageno ->
1639 begin match getopaque pageno with
1640 | None ->
1641 state.mode <- LinkNav (Ltgendir 0)
1642 | Some opaque ->
1643 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1644 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1645 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1646 then state.mode <- LinkNav (Ltgendir 0)
1648 | _ :: rest -> loop rest
1650 loop layout
1651 | _ -> ()
1652 end;
1653 begin match state.mode with
1654 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1655 if not (pagevisible layout pageno)
1656 then (
1657 match state.layout with
1658 | [] -> ()
1659 | l :: _ ->
1660 state.mode <- Birdseye (
1661 conf, leftx, l.pageno, hooverpageno, anchor
1664 | LinkNav (Ltgendir dir as lt) ->
1665 let linknav =
1666 let rec loop = function
1667 | [] -> lt
1668 | l :: rest ->
1669 match getopaque l.pageno with
1670 | None -> loop rest
1671 | Some opaque ->
1672 let link =
1673 let ld =
1674 if dir = 0
1675 then LDfirstvisible (l.pagex, l.pagey, dir)
1676 else (
1677 if dir > 0 then LDfirst else LDlast
1680 findlink opaque ld
1682 match link with
1683 | Lnotfound -> loop rest
1684 | Lfound n ->
1685 showlinktype (getlink opaque n);
1686 Ltexact (l.pageno, n)
1688 loop state.layout
1690 state.mode <- LinkNav linknav
1691 | _ -> ()
1692 end;
1693 preload layout;
1695 state.ghyll <- noghyll;
1696 if conf.updatecurs
1697 then (
1698 let mx, my = state.mpos in
1699 updateunder mx my;
1703 let conttiling pageno opaque =
1704 tilepage pageno opaque
1705 (if conf.preload then preloadlayout state.y else state.layout)
1708 let gotoy_and_clear_text y =
1709 if not conf.verbose then state.text <- "";
1710 gotoy y;
1713 let getanchor1 l =
1714 let top =
1715 let coloff = l.pagecol * l.pageh in
1716 float (l.pagey + coloff) /. float l.pageh
1718 let dtop =
1719 if l.pagedispy = 0
1720 then
1722 else
1723 if conf.presentation
1724 then float l.pagedispy /. float (calcips l.pageh)
1725 else float l.pagedispy /. float conf.interpagespace
1727 (l.pageno, top, dtop)
1730 let getanchor () =
1731 match state.layout with
1732 | l :: _ -> getanchor1 l
1733 | [] ->
1734 let n = page_of_y state.y in
1735 let y, h = getpageyh n in
1736 let dy = y - state.y in
1737 let dtop =
1738 if conf.presentation
1739 then
1740 let ips = calcips h in
1741 float (dy + ips) /. float ips
1742 else
1743 float dy /. float conf.interpagespace
1745 (n, 0.0, dtop)
1748 let getanchory (n, top, dtop) =
1749 let y, h = getpageyh n in
1750 if conf.presentation
1751 then
1752 let ips = calcips h in
1753 y + truncate (top*.float h -. dtop*.float ips) + ips;
1754 else
1755 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1758 let gotoanchor anchor =
1759 gotoy (getanchory anchor);
1762 let addnav () =
1763 cbput state.hists.nav (getanchor ());
1766 let getnav dir =
1767 let anchor = cbgetc state.hists.nav dir in
1768 getanchory anchor;
1771 let gotoghyll y =
1772 let scroll f n a b =
1773 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1774 let snake f a b =
1775 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1776 if f < a
1777 then s (float f /. float a)
1778 else (
1779 if f > b
1780 then 1.0 -. s ((float (f-b) /. float (n-b)))
1781 else 1.0
1784 snake f a b
1785 and summa f n a b =
1786 (* courtesy:
1787 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1788 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1789 let iv1 = iv f in
1790 let ins = float a *. iv1
1791 and outs = float (n-b) *. iv1 in
1792 let ones = b - a in
1793 ins +. outs +. float ones
1795 let rec set (_N, _A, _B) y sy =
1796 let sum = summa 1.0 _N _A _B in
1797 let dy = float (y - sy) in
1798 state.ghyll <- (
1799 let rec gf n y1 o =
1800 if n >= _N
1801 then state.ghyll <- noghyll
1802 else
1803 let go n =
1804 let s = scroll n _N _A _B in
1805 let y1 = y1 +. ((s *. dy) /. sum) in
1806 gotoy_and_clear_text (truncate y1);
1807 state.ghyll <- gf (n+1) y1;
1809 match o with
1810 | None -> go n
1811 | Some y' -> set (_N/2, 0, 0) y' state.y
1813 gf 0 (float state.y)
1816 match conf.ghyllscroll with
1817 | None ->
1818 gotoy_and_clear_text y
1819 | Some nab ->
1820 if state.ghyll == noghyll
1821 then set nab y state.y
1822 else state.ghyll (Some y)
1825 let gotopage n top =
1826 let y, h = getpageyh n in
1827 let y = y + (truncate (top *. float h)) in
1828 gotoghyll y
1831 let gotopage1 n top =
1832 let y = getpagey n in
1833 let y = y + top in
1834 gotoghyll y
1837 let invalidate s f =
1838 state.layout <- [];
1839 state.pdims <- [];
1840 state.rects <- [];
1841 state.rects1 <- [];
1842 match state.geomcmds with
1843 | ps, [] when String.length ps = 0 ->
1844 f ();
1845 state.geomcmds <- s, [];
1847 | ps, [] ->
1848 state.geomcmds <- ps, [s, f];
1850 | ps, (s', _) :: rest when s' = s ->
1851 state.geomcmds <- ps, ((s, f) :: rest);
1853 | ps, cmds ->
1854 state.geomcmds <- ps, ((s, f) :: cmds);
1857 let opendoc path password =
1858 state.path <- path;
1859 state.password <- password;
1860 state.gen <- state.gen + 1;
1861 state.docinfo <- [];
1863 setaalevel conf.aalevel;
1864 Wsi.settitle ("llpp " ^ Filename.basename path);
1865 wcmd "open %s\000%s\000" path password;
1866 invalidate "reqlayout"
1867 (fun () ->
1868 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1871 let scalecolor c =
1872 let c = c *. conf.colorscale in
1873 (c, c, c);
1876 let scalecolor2 (r, g, b) =
1877 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1880 let docolumns = function
1881 | Csingle _ ->
1882 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1883 let rec loop pageno pdimno pdim y ph pdims =
1884 if pageno = state.pagecount
1885 then ()
1886 else
1887 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1888 match pdims with
1889 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1890 pdimno+1, pdim, rest
1891 | _ ->
1892 pdimno, pdim, pdims
1894 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1895 let y = y +
1896 (if conf.presentation
1897 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1898 else (if pageno = 0 then 0 else calcips h)
1901 a.(pageno) <- (pdimno, x, y, pdim);
1902 loop (pageno+1) pdimno pdim (y + h) h pdims
1904 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1905 conf.columns <- Csingle a;
1907 | Cmulti ((columns, coverA, coverB), _) ->
1908 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1909 let rec loop pageno pdimno pdim x y rowh pdims =
1910 let rec fixrow m = if m = pageno then () else
1911 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1912 if h < rowh
1913 then (
1914 let y = y + (rowh - h) / 2 in
1915 a.(m) <- (pdimno, x, y, pdim);
1917 fixrow (m+1)
1919 if pageno = state.pagecount
1920 then fixrow (((pageno - 1) / columns) * columns)
1921 else
1922 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1923 match pdims with
1924 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1925 pdimno+1, pdim, rest
1926 | _ ->
1927 pdimno, pdim, pdims
1929 let x, y, rowh' =
1930 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1931 then (
1932 (conf.winw - state.scrollw - w) / 2,
1933 y + rowh + conf.interpagespace, h
1935 else (
1936 if (pageno - coverA) mod columns = 0
1937 then (
1938 (if conf.multicenter then
1939 (conf.winw - state.scrollw - state.w) / 2 else 0),
1940 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1942 else x, y, max rowh h
1945 if pageno > 1 && (pageno - coverA) mod columns = 0
1946 then fixrow (pageno - columns);
1947 a.(pageno) <- (pdimno, x, y, pdim);
1948 let x = x + w + xoff*2 + conf.interpagespace in
1949 loop (pageno+1) pdimno pdim x y rowh' pdims
1951 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1952 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1954 | Csplit (c, _) ->
1955 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1956 let rec loop pageno pdimno pdim y pdims =
1957 if pageno = state.pagecount
1958 then ()
1959 else
1960 let pdimno, ((_, w, h, _) as pdim), pdims =
1961 match pdims with
1962 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1963 pdimno+1, pdim, rest
1964 | _ ->
1965 pdimno, pdim, pdims
1967 let cw = w / c in
1968 let rec loop1 n x y =
1969 if n = c then y else (
1970 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1971 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1974 let y = loop1 0 0 y in
1975 loop (pageno+1) pdimno pdim y pdims
1977 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1978 conf.columns <- Csplit (c, a);
1981 let represent () =
1982 docolumns conf.columns;
1983 state.maxy <- calcheight ();
1984 state.hscrollh <-
1985 if state.w <= conf.winw - state.scrollw
1986 then 0
1987 else state.scrollw
1989 match state.mode with
1990 | Birdseye (_, _, pageno, _, _) ->
1991 let y, h = getpageyh pageno in
1992 let top = (conf.winh - h) / 2 in
1993 gotoy (max 0 (y - top))
1994 | _ -> gotoanchor state.anchor
1997 let reshape w h =
1998 GlDraw.viewport 0 0 w h;
1999 let firsttime = state.geomcmds == firstgeomcmds in
2000 if not firsttime && nogeomcmds state.geomcmds
2001 then state.anchor <- getanchor ();
2003 conf.winw <- w;
2004 let w = truncate (float w *. conf.zoom) - state.scrollw in
2005 let w = max w 2 in
2006 conf.winh <- h;
2007 setfontsize fstate.fontsize;
2008 GlMat.mode `modelview;
2009 GlMat.load_identity ();
2011 GlMat.mode `projection;
2012 GlMat.load_identity ();
2013 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2014 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2015 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2017 let relx =
2018 if conf.zoom <= 1.0
2019 then 0.0
2020 else float state.x /. float state.w
2022 invalidate "geometry"
2023 (fun () ->
2024 state.w <- w;
2025 if not firsttime
2026 then state.x <- truncate (relx *. float w);
2027 let w =
2028 match conf.columns with
2029 | Csingle _ -> w
2030 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2031 | Csplit (c, _) -> w * c
2033 wcmd "geometry %d %d" w h);
2036 let enttext () =
2037 let len = String.length state.text in
2038 let drawstring s =
2039 let hscrollh =
2040 match state.mode with
2041 | Textentry _
2042 | View ->
2043 let h, _, _ = state.uioh#scrollpw in
2045 | _ -> 0
2047 let rect x w =
2048 GlDraw.rect
2049 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2050 (x+.w, float (conf.winh - hscrollh))
2053 let w = float (conf.winw - state.scrollw - 1) in
2054 if state.progress >= 0.0 && state.progress < 1.0
2055 then (
2056 GlDraw.color (0.3, 0.3, 0.3);
2057 let w1 = w *. state.progress in
2058 rect 0.0 w1;
2059 GlDraw.color (0.0, 0.0, 0.0);
2060 rect w1 (w-.w1)
2062 else (
2063 GlDraw.color (0.0, 0.0, 0.0);
2064 rect 0.0 w;
2067 GlDraw.color (1.0, 1.0, 1.0);
2068 drawstring fstate.fontsize
2069 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2071 let s =
2072 match state.mode with
2073 | Textentry ((prefix, text, _, _, _, _), _) ->
2074 let s =
2075 if len > 0
2076 then
2077 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2078 else
2079 Printf.sprintf "%s%s_" prefix text
2083 | _ -> state.text
2085 let s =
2086 if state.newerrmsgs
2087 then (
2088 if not (istextentry state.mode)
2089 then
2090 let s1 = "(press 'e' to review error messasges)" in
2091 if String.length s > 0 then s ^ " " ^ s1 else s1
2092 else s
2094 else s
2096 if String.length s > 0
2097 then drawstring s
2100 let gctiles () =
2101 let len = Queue.length state.tilelru in
2102 let layout = lazy (
2103 match state.throttle with
2104 | None ->
2105 if conf.preload
2106 then preloadlayout state.y
2107 else state.layout
2108 | Some (layout, _, _) ->
2109 layout
2110 ) in
2111 let rec loop qpos =
2112 if state.memused <= conf.memlimit
2113 then ()
2114 else (
2115 if qpos < len
2116 then
2117 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2118 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2119 let (_, pw, ph, _) = getpagedim n in
2121 gen = state.gen
2122 && colorspace = conf.colorspace
2123 && angle = conf.angle
2124 && pagew = pw
2125 && pageh = ph
2126 && (
2127 let x = col*conf.tilew
2128 and y = row*conf.tileh in
2129 tilevisible (Lazy.force_val layout) n x y
2131 then Queue.push lruitem state.tilelru
2132 else (
2133 wcmd "freetile %s" p;
2134 state.memused <- state.memused - s;
2135 state.uioh#infochanged Memused;
2136 Hashtbl.remove state.tilemap k;
2138 loop (qpos+1)
2141 loop 0
2144 let flushtiles () =
2145 Queue.iter (fun (k, p, s) ->
2146 wcmd "freetile %s" p;
2147 state.memused <- state.memused - s;
2148 state.uioh#infochanged Memused;
2149 Hashtbl.remove state.tilemap k;
2150 ) state.tilelru;
2151 Queue.clear state.tilelru;
2152 load state.layout;
2155 let logcurrently = function
2156 | Idle -> dolog "Idle"
2157 | Loading (l, gen) ->
2158 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2159 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2160 dolog
2161 "Tiling %d[%d,%d] page=%s cs=%s angle"
2162 l.pageno col row pageopaque
2163 (colorspace_to_string colorspace)
2165 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2166 angle gen conf.angle state.gen
2167 tilew tileh
2168 conf.tilew conf.tileh
2170 | Outlining _ ->
2171 dolog "outlining"
2174 let act cmds =
2175 (* dolog "%S" cmds; *)
2176 let op, args =
2177 let spacepos =
2178 try String.index cmds ' '
2179 with Not_found -> -1
2181 if spacepos = -1
2182 then cmds, ""
2183 else
2184 let l = String.length cmds in
2185 let op = String.sub cmds 0 spacepos in
2186 op, begin
2187 if l - spacepos < 2 then ""
2188 else String.sub cmds (spacepos+1) (l-spacepos-1)
2191 match op with
2192 | "clear" ->
2193 state.uioh#infochanged Pdim;
2194 state.pdims <- [];
2196 | "clearrects" ->
2197 state.rects <- state.rects1;
2198 G.postRedisplay "clearrects";
2200 | "continue" ->
2201 let n =
2202 try Scanf.sscanf args "%u" (fun n -> n)
2203 with exn ->
2204 dolog "error processing 'continue' %S: %s"
2205 cmds (Printexc.to_string exn);
2206 exit 1;
2208 state.pagecount <- n;
2209 begin match state.currently with
2210 | Outlining l ->
2211 state.currently <- Idle;
2212 state.outlines <- Array.of_list (List.rev l)
2213 | _ -> ()
2214 end;
2216 let cur, cmds = state.geomcmds in
2217 if String.length cur = 0
2218 then failwith "umpossible";
2220 begin match List.rev cmds with
2221 | [] ->
2222 state.geomcmds <- "", [];
2223 represent ();
2224 | (s, f) :: rest ->
2225 f ();
2226 state.geomcmds <- s, List.rev rest;
2227 end;
2228 if conf.maxwait = None
2229 then G.postRedisplay "continue";
2231 | "title" ->
2232 Wsi.settitle args
2234 | "msg" ->
2235 showtext ' ' args
2237 | "vmsg" ->
2238 if conf.verbose
2239 then showtext ' ' args
2241 | "progress" ->
2242 let progress, text =
2244 Scanf.sscanf args "%f %n"
2245 (fun f pos ->
2246 f, String.sub args pos (String.length args - pos))
2247 with exn ->
2248 dolog "error processing 'progress' %S: %s"
2249 cmds (Printexc.to_string exn);
2250 exit 1;
2252 state.text <- text;
2253 state.progress <- progress;
2254 G.postRedisplay "progress"
2256 | "firstmatch" ->
2257 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2259 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2260 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2261 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2262 with exn ->
2263 dolog "error processing 'firstmatch' %S: %s"
2264 cmds (Printexc.to_string exn);
2265 exit 1;
2267 let y = (getpagey pageno) + truncate y0 in
2268 addnav ();
2269 gotoy y;
2270 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2272 | "match" ->
2273 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2275 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2276 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2277 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2278 with exn ->
2279 dolog "error processing 'match' %S: %s"
2280 cmds (Printexc.to_string exn);
2281 exit 1;
2283 state.rects1 <-
2284 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2286 | "page" ->
2287 let pageopaque, t =
2289 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2290 with exn ->
2291 dolog "error processing 'page' %S: %s"
2292 cmds (Printexc.to_string exn);
2293 exit 1;
2295 begin match state.currently with
2296 | Loading (l, gen) ->
2297 vlog "page %d took %f sec" l.pageno t;
2298 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2299 begin match state.throttle with
2300 | None ->
2301 let preloadedpages =
2302 if conf.preload
2303 then preloadlayout state.y
2304 else state.layout
2306 let evict () =
2307 let module IntSet =
2308 Set.Make (struct type t = int let compare = (-) end) in
2309 let set =
2310 List.fold_left (fun s l -> IntSet.add l.pageno s)
2311 IntSet.empty preloadedpages
2313 let evictedpages =
2314 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2315 if not (IntSet.mem pageno set)
2316 then (
2317 wcmd "freepage %s" opaque;
2318 key :: accu
2320 else accu
2321 ) state.pagemap []
2323 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2325 evict ();
2326 state.currently <- Idle;
2327 if gen = state.gen
2328 then (
2329 tilepage l.pageno pageopaque state.layout;
2330 load state.layout;
2331 load preloadedpages;
2332 if pagevisible state.layout l.pageno
2333 && layoutready state.layout
2334 then G.postRedisplay "page";
2337 | Some (layout, _, _) ->
2338 state.currently <- Idle;
2339 tilepage l.pageno pageopaque layout;
2340 load state.layout
2341 end;
2343 | _ ->
2344 dolog "Inconsistent loading state";
2345 logcurrently state.currently;
2346 exit 1
2349 | "tile" ->
2350 let (x, y, opaque, size, t) =
2352 Scanf.sscanf args "%u %u %s %u %f"
2353 (fun x y p size t -> (x, y, p, size, t))
2354 with exn ->
2355 dolog "error processing 'tile' %S: %s"
2356 cmds (Printexc.to_string exn);
2357 exit 1;
2359 begin match state.currently with
2360 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2361 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2363 if tilew != conf.tilew || tileh != conf.tileh
2364 then (
2365 wcmd "freetile %s" opaque;
2366 state.currently <- Idle;
2367 load state.layout;
2369 else (
2370 puttileopaque l col row gen cs angle opaque size t;
2371 state.memused <- state.memused + size;
2372 state.uioh#infochanged Memused;
2373 gctiles ();
2374 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2375 opaque, size) state.tilelru;
2377 let layout =
2378 match state.throttle with
2379 | None -> state.layout
2380 | Some (layout, _, _) -> layout
2383 state.currently <- Idle;
2384 if gen = state.gen
2385 && conf.colorspace = cs
2386 && conf.angle = angle
2387 && tilevisible layout l.pageno x y
2388 then conttiling l.pageno pageopaque;
2390 begin match state.throttle with
2391 | None ->
2392 preload state.layout;
2393 if gen = state.gen
2394 && conf.colorspace = cs
2395 && conf.angle = angle
2396 && tilevisible state.layout l.pageno x y
2397 then G.postRedisplay "tile nothrottle";
2399 | Some (layout, y, _) ->
2400 let ready = layoutready layout in
2401 if ready
2402 then (
2403 state.y <- y;
2404 state.layout <- layout;
2405 state.throttle <- None;
2406 G.postRedisplay "throttle";
2408 else load layout;
2409 end;
2412 | _ ->
2413 dolog "Inconsistent tiling state";
2414 logcurrently state.currently;
2415 exit 1
2418 | "pdim" ->
2419 let pdim =
2421 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2422 with exn ->
2423 dolog "error processing 'pdim' %S: %s"
2424 cmds (Printexc.to_string exn);
2425 exit 1;
2427 state.uioh#infochanged Pdim;
2428 state.pdims <- pdim :: state.pdims
2430 | "o" ->
2431 let (l, n, t, h, pos) =
2433 Scanf.sscanf args "%u %u %d %u %n"
2434 (fun l n t h pos -> l, n, t, h, pos)
2435 with exn ->
2436 dolog "error processing 'o' %S: %s"
2437 cmds (Printexc.to_string exn);
2438 exit 1;
2440 let s = String.sub args pos (String.length args - pos) in
2441 let outline = (s, l, (n, float t /. float h, 0.0)) in
2442 begin match state.currently with
2443 | Outlining outlines ->
2444 state.currently <- Outlining (outline :: outlines)
2445 | Idle ->
2446 state.currently <- Outlining [outline]
2447 | currently ->
2448 dolog "invalid outlining state";
2449 logcurrently currently
2452 | "info" ->
2453 state.docinfo <- (1, args) :: state.docinfo
2455 | "infoend" ->
2456 state.uioh#infochanged Docinfo;
2457 state.docinfo <- List.rev state.docinfo
2459 | _ ->
2460 dolog "unknown cmd `%S'" cmds
2463 let onhist cb =
2464 let rc = cb.rc in
2465 let action = function
2466 | HCprev -> cbget cb ~-1
2467 | HCnext -> cbget cb 1
2468 | HCfirst -> cbget cb ~-(cb.rc)
2469 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2470 and cancel () = cb.rc <- rc
2471 in (action, cancel)
2474 let search pattern forward =
2475 if String.length pattern > 0
2476 then
2477 let pn, py =
2478 match state.layout with
2479 | [] -> 0, 0
2480 | l :: _ ->
2481 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2483 wcmd "search %d %d %d %d,%s\000"
2484 (btod conf.icase) pn py (btod forward) pattern;
2487 let intentry text key =
2488 let c =
2489 if key >= 32 && key < 127
2490 then Char.chr key
2491 else '\000'
2493 match c with
2494 | '0' .. '9' ->
2495 let text = addchar text c in
2496 TEcont text
2498 | _ ->
2499 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2500 TEcont text
2503 let linknentry text key =
2504 let c =
2505 if key >= 32 && key < 127
2506 then Char.chr key
2507 else '\000'
2509 match c with
2510 | 'a' .. 'z' ->
2511 let text = addchar text c in
2512 TEcont text
2514 | _ ->
2515 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2516 TEcont text
2519 let linkndone f s =
2520 if String.length s > 0
2521 then (
2522 let n =
2523 let l = String.length s in
2524 let rec loop pos n = if pos = l then n else
2525 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2526 loop (pos+1) (n*26 + m)
2527 in loop 0 0
2529 let rec loop n = function
2530 | [] -> ()
2531 | l :: rest ->
2532 match getopaque l.pageno with
2533 | None -> loop n rest
2534 | Some opaque ->
2535 let m = getlinkcount opaque in
2536 if n < m
2537 then (
2538 let under = getlink opaque n in
2539 f under
2541 else loop (n-m) rest
2543 loop n state.layout;
2547 let textentry text key =
2548 if key land 0xff00 = 0xff00
2549 then TEcont text
2550 else TEcont (text ^ Wsi.toutf8 key)
2553 let reqlayout angle proportional =
2554 match state.throttle with
2555 | None ->
2556 if nogeomcmds state.geomcmds
2557 then state.anchor <- getanchor ();
2558 conf.angle <- angle mod 360;
2559 if conf.angle != 0
2560 then (
2561 match state.mode with
2562 | LinkNav _ -> state.mode <- View
2563 | _ -> ()
2565 conf.proportional <- proportional;
2566 invalidate "reqlayout"
2567 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2568 | _ -> ()
2571 let settrim trimmargins trimfuzz =
2572 if nogeomcmds state.geomcmds
2573 then state.anchor <- getanchor ();
2574 conf.trimmargins <- trimmargins;
2575 conf.trimfuzz <- trimfuzz;
2576 let x0, y0, x1, y1 = trimfuzz in
2577 invalidate "settrim"
2578 (fun () ->
2579 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2580 Hashtbl.iter (fun _ opaque ->
2581 wcmd "freepage %s" opaque;
2582 ) state.pagemap;
2583 Hashtbl.clear state.pagemap;
2586 let setzoom zoom =
2587 match state.throttle with
2588 | None ->
2589 let zoom = max 0.01 zoom in
2590 if zoom <> conf.zoom
2591 then (
2592 state.prevzoom <- conf.zoom;
2593 conf.zoom <- zoom;
2594 reshape conf.winw conf.winh;
2595 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2598 | Some (layout, y, started) ->
2599 let time =
2600 match conf.maxwait with
2601 | None -> 0.0
2602 | Some t -> t
2604 let dt = now () -. started in
2605 if dt > time
2606 then (
2607 state.y <- y;
2608 load layout;
2612 let setcolumns mode columns coverA coverB =
2613 state.prevcolumns <- Some (conf.columns, conf.zoom);
2614 if columns < 0
2615 then (
2616 if isbirdseye mode
2617 then showtext '!' "split mode doesn't work in bird's eye"
2618 else (
2619 conf.columns <- Csplit (-columns, [||]);
2620 state.x <- 0;
2621 conf.zoom <- 1.0;
2624 else (
2625 if columns < 2
2626 then (
2627 conf.columns <- Csingle [||];
2628 state.x <- 0;
2629 setzoom 1.0;
2631 else (
2632 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2633 conf.zoom <- 1.0;
2636 reshape conf.winw conf.winh;
2639 let enterbirdseye () =
2640 let zoom = float conf.thumbw /. float conf.winw in
2641 let birdseyepageno =
2642 let cy = conf.winh / 2 in
2643 let fold = function
2644 | [] -> 0
2645 | l :: rest ->
2646 let rec fold best = function
2647 | [] -> best.pageno
2648 | l :: rest ->
2649 let d = cy - (l.pagedispy + l.pagevh/2)
2650 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2651 if abs d < abs dbest
2652 then fold l rest
2653 else best.pageno
2654 in fold l rest
2656 fold state.layout
2658 state.mode <- Birdseye (
2659 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2661 conf.zoom <- zoom;
2662 conf.presentation <- false;
2663 conf.interpagespace <- 10;
2664 conf.hlinks <- false;
2665 state.x <- 0;
2666 state.mstate <- Mnone;
2667 conf.maxwait <- None;
2668 conf.columns <- (
2669 match conf.beyecolumns with
2670 | Some c ->
2671 conf.zoom <- 1.0;
2672 Cmulti ((c, 0, 0), [||])
2673 | None -> Csingle [||]
2675 Wsi.setcursor Wsi.CURSOR_INHERIT;
2676 if conf.verbose
2677 then
2678 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2679 (100.0*.zoom)
2680 else
2681 state.text <- ""
2683 reshape conf.winw conf.winh;
2686 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2687 state.mode <- View;
2688 conf.zoom <- c.zoom;
2689 conf.presentation <- c.presentation;
2690 conf.interpagespace <- c.interpagespace;
2691 conf.maxwait <- c.maxwait;
2692 conf.hlinks <- c.hlinks;
2693 conf.beyecolumns <- (
2694 match conf.columns with
2695 | Cmulti ((c, _, _), _) -> Some c
2696 | Csingle _ -> None
2697 | Csplit _ -> failwith "leaving bird's eye split mode"
2699 conf.columns <- (
2700 match c.columns with
2701 | Cmulti (c, _) -> Cmulti (c, [||])
2702 | Csingle _ -> Csingle [||]
2703 | Csplit (c, _) -> Csplit (c, [||])
2705 state.x <- leftx;
2706 if conf.verbose
2707 then
2708 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2709 (100.0*.conf.zoom)
2711 reshape conf.winw conf.winh;
2712 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2715 let togglebirdseye () =
2716 match state.mode with
2717 | Birdseye vals -> leavebirdseye vals true
2718 | View -> enterbirdseye ()
2719 | _ -> ()
2722 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2723 let pageno = max 0 (pageno - incr) in
2724 let rec loop = function
2725 | [] -> gotopage1 pageno 0
2726 | l :: _ when l.pageno = pageno ->
2727 if l.pagedispy >= 0 && l.pagey = 0
2728 then G.postRedisplay "upbirdseye"
2729 else gotopage1 pageno 0
2730 | _ :: rest -> loop rest
2732 loop state.layout;
2733 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2736 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2737 let pageno = min (state.pagecount - 1) (pageno + incr) in
2738 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2739 let rec loop = function
2740 | [] ->
2741 let y, h = getpageyh pageno in
2742 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2743 gotoy (clamp dy)
2744 | l :: _ when l.pageno = pageno ->
2745 if l.pagevh != l.pageh
2746 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2747 else G.postRedisplay "downbirdseye"
2748 | _ :: rest -> loop rest
2750 loop state.layout
2753 let optentry mode _ key =
2754 let btos b = if b then "on" else "off" in
2755 if key >= 32 && key < 127
2756 then
2757 let c = Char.chr key in
2758 match c with
2759 | 's' ->
2760 let ondone s =
2761 try conf.scrollstep <- int_of_string s with exc ->
2762 state.text <- Printf.sprintf "bad integer `%s': %s"
2763 s (Printexc.to_string exc)
2765 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2767 | 'A' ->
2768 let ondone s =
2770 conf.autoscrollstep <- int_of_string s;
2771 if state.autoscroll <> None
2772 then state.autoscroll <- Some conf.autoscrollstep
2773 with exc ->
2774 state.text <- Printf.sprintf "bad integer `%s': %s"
2775 s (Printexc.to_string exc)
2777 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2779 | 'C' ->
2780 let ondone s =
2782 let n, a, b = multicolumns_of_string s in
2783 setcolumns mode n a b;
2784 with exc ->
2785 state.text <- Printf.sprintf "bad columns `%s': %s"
2786 s (Printexc.to_string exc)
2788 TEswitch ("columns: ", "", None, textentry, ondone, true)
2790 | 'Z' ->
2791 let ondone s =
2793 let zoom = float (int_of_string s) /. 100.0 in
2794 setzoom zoom
2795 with exc ->
2796 state.text <- Printf.sprintf "bad integer `%s': %s"
2797 s (Printexc.to_string exc)
2799 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2801 | 't' ->
2802 let ondone s =
2804 conf.thumbw <- bound (int_of_string s) 2 4096;
2805 state.text <-
2806 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2807 begin match mode with
2808 | Birdseye beye ->
2809 leavebirdseye beye false;
2810 enterbirdseye ();
2811 | _ -> ();
2813 with exc ->
2814 state.text <- Printf.sprintf "bad integer `%s': %s"
2815 s (Printexc.to_string exc)
2817 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2819 | 'R' ->
2820 let ondone s =
2821 match try
2822 Some (int_of_string s)
2823 with exc ->
2824 state.text <- Printf.sprintf "bad integer `%s': %s"
2825 s (Printexc.to_string exc);
2826 None
2827 with
2828 | Some angle -> reqlayout angle conf.proportional
2829 | None -> ()
2831 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2833 | 'i' ->
2834 conf.icase <- not conf.icase;
2835 TEdone ("case insensitive search " ^ (btos conf.icase))
2837 | 'p' ->
2838 conf.preload <- not conf.preload;
2839 gotoy state.y;
2840 TEdone ("preload " ^ (btos conf.preload))
2842 | 'v' ->
2843 conf.verbose <- not conf.verbose;
2844 TEdone ("verbose " ^ (btos conf.verbose))
2846 | 'd' ->
2847 conf.debug <- not conf.debug;
2848 TEdone ("debug " ^ (btos conf.debug))
2850 | 'h' ->
2851 conf.maxhfit <- not conf.maxhfit;
2852 state.maxy <- calcheight ();
2853 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2855 | 'c' ->
2856 conf.crophack <- not conf.crophack;
2857 TEdone ("crophack " ^ btos conf.crophack)
2859 | 'a' ->
2860 let s =
2861 match conf.maxwait with
2862 | None ->
2863 conf.maxwait <- Some infinity;
2864 "always wait for page to complete"
2865 | Some _ ->
2866 conf.maxwait <- None;
2867 "show placeholder if page is not ready"
2869 TEdone s
2871 | 'f' ->
2872 conf.underinfo <- not conf.underinfo;
2873 TEdone ("underinfo " ^ btos conf.underinfo)
2875 | 'P' ->
2876 conf.savebmarks <- not conf.savebmarks;
2877 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2879 | 'S' ->
2880 let ondone s =
2882 let pageno, py =
2883 match state.layout with
2884 | [] -> 0, 0
2885 | l :: _ ->
2886 l.pageno, l.pagey
2888 conf.interpagespace <- int_of_string s;
2889 docolumns conf.columns;
2890 state.maxy <- calcheight ();
2891 let y = getpagey pageno in
2892 gotoy (y + py)
2893 with exc ->
2894 state.text <- Printf.sprintf "bad integer `%s': %s"
2895 s (Printexc.to_string exc)
2897 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2899 | 'l' ->
2900 reqlayout conf.angle (not conf.proportional);
2901 TEdone ("proportional display " ^ btos conf.proportional)
2903 | 'T' ->
2904 settrim (not conf.trimmargins) conf.trimfuzz;
2905 TEdone ("trim margins " ^ btos conf.trimmargins)
2907 | 'I' ->
2908 conf.invert <- not conf.invert;
2909 TEdone ("invert colors " ^ btos conf.invert)
2911 | 'x' ->
2912 let ondone s =
2913 cbput state.hists.sel s;
2914 conf.selcmd <- s;
2916 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2917 textentry, ondone, true)
2919 | _ ->
2920 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2921 TEstop
2922 else
2923 TEcont state.text
2926 class type lvsource = object
2927 method getitemcount : int
2928 method getitem : int -> (string * int)
2929 method hasaction : int -> bool
2930 method exit :
2931 uioh:uioh ->
2932 cancel:bool ->
2933 active:int ->
2934 first:int ->
2935 pan:int ->
2936 qsearch:string ->
2937 uioh option
2938 method getactive : int
2939 method getfirst : int
2940 method getqsearch : string
2941 method setqsearch : string -> unit
2942 method getpan : int
2943 end;;
2945 class virtual lvsourcebase = object
2946 val mutable m_active = 0
2947 val mutable m_first = 0
2948 val mutable m_qsearch = ""
2949 val mutable m_pan = 0
2950 method getactive = m_active
2951 method getfirst = m_first
2952 method getqsearch = m_qsearch
2953 method getpan = m_pan
2954 method setqsearch s = m_qsearch <- s
2955 end;;
2957 let withoutlastutf8 s =
2958 let len = String.length s in
2959 if len = 0
2960 then s
2961 else
2962 let rec find pos =
2963 if pos = 0
2964 then pos
2965 else
2966 let b = Char.code s.[pos] in
2967 if b land 0b110000 = 0b11000000
2968 then find (pos-1)
2969 else pos-1
2971 let first =
2972 if Char.code s.[len-1] land 0x80 = 0
2973 then len-1
2974 else find (len-1)
2976 String.sub s 0 first;
2979 let textentrykeyboard
2980 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2981 let enttext te =
2982 state.mode <- Textentry (te, onleave);
2983 state.text <- "";
2984 enttext ();
2985 G.postRedisplay "textentrykeyboard enttext";
2987 let histaction cmd =
2988 match opthist with
2989 | None -> ()
2990 | Some (action, _) ->
2991 state.mode <- Textentry (
2992 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2994 G.postRedisplay "textentry histaction"
2996 match key with
2997 | 0xff08 -> (* backspace *)
2998 let s = withoutlastutf8 text in
2999 let len = String.length s in
3000 if cancelonempty && len = 0
3001 then (
3002 onleave Cancel;
3003 G.postRedisplay "textentrykeyboard after cancel";
3005 else (
3006 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3009 | 0xff0d ->
3010 ondone text;
3011 onleave Confirm;
3012 G.postRedisplay "textentrykeyboard after confirm"
3014 | 0xff52 -> histaction HCprev
3015 | 0xff54 -> histaction HCnext
3016 | 0xff50 -> histaction HCfirst
3017 | 0xff57 -> histaction HClast
3019 | 0xff1b -> (* escape*)
3020 if String.length text = 0
3021 then (
3022 begin match opthist with
3023 | None -> ()
3024 | Some (_, onhistcancel) -> onhistcancel ()
3025 end;
3026 onleave Cancel;
3027 state.text <- "";
3028 G.postRedisplay "textentrykeyboard after cancel2"
3030 else (
3031 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3034 | 0xff9f | 0xffff -> () (* delete *)
3036 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3037 begin match onkey text key with
3038 | TEdone text ->
3039 ondone text;
3040 onleave Confirm;
3041 G.postRedisplay "textentrykeyboard after confirm2";
3043 | TEcont text ->
3044 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3046 | TEstop ->
3047 onleave Cancel;
3048 G.postRedisplay "textentrykeyboard after cancel3"
3050 | TEswitch te ->
3051 state.mode <- Textentry (te, onleave);
3052 G.postRedisplay "textentrykeyboard switch";
3053 end;
3055 | _ ->
3056 vlog "unhandled key %s" (Wsi.keyname key)
3059 let firstof first active =
3060 if first > active || abs (first - active) > fstate.maxrows - 1
3061 then max 0 (active - (fstate.maxrows/2))
3062 else first
3065 let calcfirst first active =
3066 if active > first
3067 then
3068 let rows = active - first in
3069 if rows > fstate.maxrows then active - fstate.maxrows else first
3070 else active
3073 let scrollph y maxy =
3074 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3075 let sh = float conf.winh /. sh in
3076 let sh = max sh (float conf.scrollh) in
3078 let percent =
3079 if y = state.maxy
3080 then 1.0
3081 else float y /. float maxy
3083 let position = (float conf.winh -. sh) *. percent in
3085 let position =
3086 if position +. sh > float conf.winh
3087 then float conf.winh -. sh
3088 else position
3090 position, sh;
3093 let coe s = (s :> uioh);;
3095 class listview ~(source:lvsource) ~trusted ~modehash =
3096 object (self)
3097 val m_pan = source#getpan
3098 val m_first = source#getfirst
3099 val m_active = source#getactive
3100 val m_qsearch = source#getqsearch
3101 val m_prev_uioh = state.uioh
3103 method private elemunder y =
3104 let n = y / (fstate.fontsize+1) in
3105 if m_first + n < source#getitemcount
3106 then (
3107 if source#hasaction (m_first + n)
3108 then Some (m_first + n)
3109 else None
3111 else None
3113 method display =
3114 Gl.enable `blend;
3115 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3116 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3117 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3118 GlDraw.color (1., 1., 1.);
3119 Gl.enable `texture_2d;
3120 let fs = fstate.fontsize in
3121 let nfs = fs + 1 in
3122 let ww = fstate.wwidth in
3123 let tabw = 30.0*.ww in
3124 let itemcount = source#getitemcount in
3125 let rec loop row =
3126 if (row - m_first) * nfs > conf.winh
3127 then ()
3128 else (
3129 if row >= 0 && row < itemcount
3130 then (
3131 let (s, level) = source#getitem row in
3132 let y = (row - m_first) * nfs in
3133 let x = 5.0 +. float (level + m_pan) *. ww in
3134 if row = m_active
3135 then (
3136 Gl.disable `texture_2d;
3137 GlDraw.polygon_mode `both `line;
3138 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3139 GlDraw.rect (1., float (y + 1))
3140 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3141 GlDraw.polygon_mode `both `fill;
3142 GlDraw.color (1., 1., 1.);
3143 Gl.enable `texture_2d;
3146 let drawtabularstring s =
3147 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3148 if trusted
3149 then
3150 let tabpos = try String.index s '\t' with Not_found -> -1 in
3151 if tabpos > 0
3152 then
3153 let len = String.length s - tabpos - 1 in
3154 let s1 = String.sub s 0 tabpos
3155 and s2 = String.sub s (tabpos + 1) len in
3156 let nx = drawstr x s1 in
3157 let sw = nx -. x in
3158 let x = x +. (max tabw sw) in
3159 drawstr x s2
3160 else
3161 drawstr x s
3162 else
3163 drawstr x s
3165 let _ = drawtabularstring s in
3166 loop (row+1)
3170 loop m_first;
3171 Gl.disable `blend;
3172 Gl.disable `texture_2d;
3174 method updownlevel incr =
3175 let len = source#getitemcount in
3176 let curlevel =
3177 if m_active >= 0 && m_active < len
3178 then snd (source#getitem m_active)
3179 else -1
3181 let rec flow i =
3182 if i = len then i-1 else if i = -1 then 0 else
3183 let _, l = source#getitem i in
3184 if l != curlevel then i else flow (i+incr)
3186 let active = flow m_active in
3187 let first = calcfirst m_first active in
3188 G.postRedisplay "outline updownlevel";
3189 {< m_active = active; m_first = first >}
3191 method private key1 key mask =
3192 let set1 active first qsearch =
3193 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3195 let search active pattern incr =
3196 let dosearch re =
3197 let rec loop n =
3198 if n >= 0 && n < source#getitemcount
3199 then (
3200 let s, _ = source#getitem n in
3202 (try ignore (Str.search_forward re s 0); true
3203 with Not_found -> false)
3204 then Some n
3205 else loop (n + incr)
3207 else None
3209 loop active
3212 let re = Str.regexp_case_fold pattern in
3213 dosearch re
3214 with Failure s ->
3215 state.text <- s;
3216 None
3218 let itemcount = source#getitemcount in
3219 let find start incr =
3220 let rec find i =
3221 if i = -1 || i = itemcount
3222 then -1
3223 else (
3224 if source#hasaction i
3225 then i
3226 else find (i + incr)
3229 find start
3231 let set active first =
3232 let first = bound first 0 (itemcount - fstate.maxrows) in
3233 state.text <- "";
3234 coe {< m_active = active; m_first = first >}
3236 let navigate incr =
3237 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3238 let active, first =
3239 let incr1 = if incr > 0 then 1 else -1 in
3240 if isvisible m_first m_active
3241 then
3242 let next =
3243 let next = m_active + incr in
3244 let next =
3245 if next < 0 || next >= itemcount
3246 then -1
3247 else find next incr1
3249 if next = -1 || abs (m_active - next) > fstate.maxrows
3250 then -1
3251 else next
3253 if next = -1
3254 then
3255 let first = m_first + incr in
3256 let first = bound first 0 (itemcount - 1) in
3257 let next =
3258 let next = m_active + incr in
3259 let next = bound next 0 (itemcount - 1) in
3260 find next ~-incr1
3262 let active = if next = -1 then m_active else next in
3263 active, first
3264 else
3265 let first = min next m_first in
3266 let first =
3267 if abs (next - first) > fstate.maxrows
3268 then first + incr
3269 else first
3271 next, first
3272 else
3273 let first = m_first + incr in
3274 let first = bound first 0 (itemcount - 1) in
3275 let active =
3276 let next = m_active + incr in
3277 let next = bound next 0 (itemcount - 1) in
3278 let next = find next incr1 in
3279 let active =
3280 if next = -1 || abs (m_active - first) > fstate.maxrows
3281 then (
3282 let active = if m_active = -1 then next else m_active in
3283 active
3285 else next
3287 if isvisible first active
3288 then active
3289 else -1
3291 active, first
3293 G.postRedisplay "listview navigate";
3294 set active first;
3296 match key with
3297 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3298 let incr = if key = 0x72 then -1 else 1 in
3299 let active, first =
3300 match search (m_active + incr) m_qsearch incr with
3301 | None ->
3302 state.text <- m_qsearch ^ " [not found]";
3303 m_active, m_first
3304 | Some active ->
3305 state.text <- m_qsearch;
3306 active, firstof m_first active
3308 G.postRedisplay "listview ctrl-r/s";
3309 set1 active first m_qsearch;
3311 | 0xff08 -> (* backspace *)
3312 if String.length m_qsearch = 0
3313 then coe self
3314 else (
3315 let qsearch = withoutlastutf8 m_qsearch in
3316 let len = String.length qsearch in
3317 if len = 0
3318 then (
3319 state.text <- "";
3320 G.postRedisplay "listview empty qsearch";
3321 set1 m_active m_first "";
3323 else
3324 let active, first =
3325 match search m_active qsearch ~-1 with
3326 | None ->
3327 state.text <- qsearch ^ " [not found]";
3328 m_active, m_first
3329 | Some active ->
3330 state.text <- qsearch;
3331 active, firstof m_first active
3333 G.postRedisplay "listview backspace qsearch";
3334 set1 active first qsearch
3337 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3338 let pattern = m_qsearch ^ Wsi.toutf8 key in
3339 let active, first =
3340 match search m_active pattern 1 with
3341 | None ->
3342 state.text <- pattern ^ " [not found]";
3343 m_active, m_first
3344 | Some active ->
3345 state.text <- pattern;
3346 active, firstof m_first active
3348 G.postRedisplay "listview qsearch add";
3349 set1 active first pattern;
3351 | 0xff1b -> (* escape *)
3352 state.text <- "";
3353 if String.length m_qsearch = 0
3354 then (
3355 G.postRedisplay "list view escape";
3356 begin
3357 match
3358 source#exit (coe self) true m_active m_first m_pan m_qsearch
3359 with
3360 | None -> m_prev_uioh
3361 | Some uioh -> uioh
3364 else (
3365 G.postRedisplay "list view kill qsearch";
3366 source#setqsearch "";
3367 coe {< m_qsearch = "" >}
3370 | 0xff0d -> (* return *)
3371 state.text <- "";
3372 let self = {< m_qsearch = "" >} in
3373 source#setqsearch "";
3374 let opt =
3375 G.postRedisplay "listview enter";
3376 if m_active >= 0 && m_active < source#getitemcount
3377 then (
3378 source#exit (coe self) false m_active m_first m_pan "";
3380 else (
3381 source#exit (coe self) true m_active m_first m_pan "";
3384 begin match opt with
3385 | None -> m_prev_uioh
3386 | Some uioh -> uioh
3389 | 0xff9f | 0xffff -> (* delete *)
3390 coe self
3392 | 0xff52 -> navigate ~-1 (* up *)
3393 | 0xff54 -> navigate 1 (* down *)
3394 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3395 | 0xff56 -> navigate fstate.maxrows (* next *)
3397 | 0xff53 -> (* right *)
3398 state.text <- "";
3399 G.postRedisplay "listview right";
3400 coe {< m_pan = m_pan - 1 >}
3402 | 0xff51 -> (* left *)
3403 state.text <- "";
3404 G.postRedisplay "listview left";
3405 coe {< m_pan = m_pan + 1 >}
3407 | 0xff50 -> (* home *)
3408 let active = find 0 1 in
3409 G.postRedisplay "listview home";
3410 set active 0;
3412 | 0xff57 -> (* end *)
3413 let first = max 0 (itemcount - fstate.maxrows) in
3414 let active = find (itemcount - 1) ~-1 in
3415 G.postRedisplay "listview end";
3416 set active first;
3418 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3419 coe self
3421 | _ ->
3422 dolog "listview unknown key %#x" key; coe self
3424 method key key mask =
3425 match state.mode with
3426 | Textentry te -> textentrykeyboard key mask te; coe self
3427 | _ -> self#key1 key mask
3429 method button button down x y _ =
3430 let opt =
3431 match button with
3432 | 1 when x > conf.winw - conf.scrollbw ->
3433 G.postRedisplay "listview scroll";
3434 if down
3435 then
3436 let _, position, sh = self#scrollph in
3437 if y > truncate position && y < truncate (position +. sh)
3438 then (
3439 state.mstate <- Mscrolly;
3440 Some (coe self)
3442 else
3443 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3444 let first = truncate (s *. float source#getitemcount) in
3445 let first = min source#getitemcount first in
3446 Some (coe {< m_first = first; m_active = first >})
3447 else (
3448 state.mstate <- Mnone;
3449 Some (coe self);
3451 | 1 when not down ->
3452 begin match self#elemunder y with
3453 | Some n ->
3454 G.postRedisplay "listview click";
3455 source#exit
3456 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3457 | _ ->
3458 Some (coe self)
3460 | n when (n == 4 || n == 5) && not down ->
3461 let len = source#getitemcount in
3462 let first =
3463 if n = 5 && m_first + fstate.maxrows >= len
3464 then
3465 m_first
3466 else
3467 let first = m_first + (if n == 4 then -1 else 1) in
3468 bound first 0 (len - 1)
3470 G.postRedisplay "listview wheel";
3471 Some (coe {< m_first = first >})
3472 | n when (n = 6 || n = 7) && not down ->
3473 let inc = m_first + (if n = 7 then -1 else 1) in
3474 G.postRedisplay "listview hwheel";
3475 Some (coe {< m_pan = m_pan + inc >})
3476 | _ ->
3477 Some (coe self)
3479 match opt with
3480 | None -> m_prev_uioh
3481 | Some uioh -> uioh
3483 method motion _ y =
3484 match state.mstate with
3485 | Mscrolly ->
3486 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3487 let first = truncate (s *. float source#getitemcount) in
3488 let first = min source#getitemcount first in
3489 G.postRedisplay "listview motion";
3490 coe {< m_first = first; m_active = first >}
3491 | _ -> coe self
3493 method pmotion x y =
3494 if x < conf.winw - conf.scrollbw
3495 then
3496 let n =
3497 match self#elemunder y with
3498 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3499 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3501 let o =
3502 if n != m_active
3503 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3504 else self
3506 coe o
3507 else (
3508 Wsi.setcursor Wsi.CURSOR_INHERIT;
3509 coe self
3512 method infochanged _ = ()
3514 method scrollpw = (0, 0.0, 0.0)
3515 method scrollph =
3516 let nfs = fstate.fontsize + 1 in
3517 let y = m_first * nfs in
3518 let itemcount = source#getitemcount in
3519 let maxi = max 0 (itemcount - fstate.maxrows) in
3520 let maxy = maxi * nfs in
3521 let p, h = scrollph y maxy in
3522 conf.scrollbw, p, h
3524 method modehash = modehash
3525 end;;
3527 class outlinelistview ~source =
3528 object (self)
3529 inherit listview
3530 ~source:(source :> lvsource)
3531 ~trusted:false
3532 ~modehash:(findkeyhash conf "outline")
3533 as super
3535 method key key mask =
3536 let calcfirst first active =
3537 if active > first
3538 then
3539 let rows = active - first in
3540 let maxrows =
3541 if String.length state.text = 0
3542 then fstate.maxrows
3543 else fstate.maxrows - 2
3545 if rows > maxrows then active - maxrows else first
3546 else active
3548 let navigate incr =
3549 let active = m_active + incr in
3550 let active = bound active 0 (source#getitemcount - 1) in
3551 let first = calcfirst m_first active in
3552 G.postRedisplay "outline navigate";
3553 coe {< m_active = active; m_first = first >}
3555 let ctrl = Wsi.withctrl mask in
3556 match key with
3557 | 110 when ctrl -> (* ctrl-n *)
3558 source#narrow m_qsearch;
3559 G.postRedisplay "outline ctrl-n";
3560 coe {< m_first = 0; m_active = 0 >}
3562 | 117 when ctrl -> (* ctrl-u *)
3563 source#denarrow;
3564 G.postRedisplay "outline ctrl-u";
3565 state.text <- "";
3566 coe {< m_first = 0; m_active = 0 >}
3568 | 108 when ctrl -> (* ctrl-l *)
3569 let first = m_active - (fstate.maxrows / 2) in
3570 G.postRedisplay "outline ctrl-l";
3571 coe {< m_first = first >}
3573 | 0xff9f | 0xffff -> (* delete *)
3574 source#remove m_active;
3575 G.postRedisplay "outline delete";
3576 let active = max 0 (m_active-1) in
3577 coe {< m_first = firstof m_first active;
3578 m_active = active >}
3580 | 0xff52 -> navigate ~-1 (* up *)
3581 | 0xff54 -> navigate 1 (* down *)
3582 | 0xff55 -> (* prior *)
3583 navigate ~-(fstate.maxrows)
3584 | 0xff56 -> (* next *)
3585 navigate fstate.maxrows
3587 | 0xff53 -> (* [ctrl-]right *)
3588 let o =
3589 if ctrl
3590 then (
3591 G.postRedisplay "outline ctrl right";
3592 {< m_pan = m_pan + 1 >}
3594 else self#updownlevel 1
3596 coe o
3598 | 0xff51 -> (* [ctrl-]left *)
3599 let o =
3600 if ctrl
3601 then (
3602 G.postRedisplay "outline ctrl left";
3603 {< m_pan = m_pan - 1 >}
3605 else self#updownlevel ~-1
3607 coe o
3609 | 0xff50 -> (* home *)
3610 G.postRedisplay "outline home";
3611 coe {< m_first = 0; m_active = 0 >}
3613 | 0xff57 -> (* end *)
3614 let active = source#getitemcount - 1 in
3615 let first = max 0 (active - fstate.maxrows) in
3616 G.postRedisplay "outline end";
3617 coe {< m_active = active; m_first = first >}
3619 | _ -> super#key key mask
3622 let outlinesource usebookmarks =
3623 let empty = [||] in
3624 (object
3625 inherit lvsourcebase
3626 val mutable m_items = empty
3627 val mutable m_orig_items = empty
3628 val mutable m_prev_items = empty
3629 val mutable m_narrow_pattern = ""
3630 val mutable m_hadremovals = false
3632 method getitemcount =
3633 Array.length m_items + (if m_hadremovals then 1 else 0)
3635 method getitem n =
3636 if n == Array.length m_items && m_hadremovals
3637 then
3638 ("[Confirm removal]", 0)
3639 else
3640 let s, n, _ = m_items.(n) in
3641 (s, n)
3643 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3644 ignore (uioh, first, qsearch);
3645 let confrimremoval = m_hadremovals && active = Array.length m_items in
3646 let items =
3647 if String.length m_narrow_pattern = 0
3648 then m_orig_items
3649 else m_items
3651 if not cancel
3652 then (
3653 if not confrimremoval
3654 then(
3655 let _, _, anchor = m_items.(active) in
3656 gotoanchor anchor;
3657 m_items <- items;
3659 else (
3660 state.bookmarks <- Array.to_list m_items;
3661 m_orig_items <- m_items;
3664 else m_items <- items;
3665 m_pan <- pan;
3666 None
3668 method hasaction _ = true
3670 method greetmsg =
3671 if Array.length m_items != Array.length m_orig_items
3672 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3673 else ""
3675 method narrow pattern =
3676 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3677 match reopt with
3678 | None -> ()
3679 | Some re ->
3680 let rec loop accu n =
3681 if n = -1
3682 then (
3683 m_narrow_pattern <- pattern;
3684 m_items <- Array.of_list accu
3686 else
3687 let (s, _, _) as o = m_items.(n) in
3688 let accu =
3689 if (try ignore (Str.search_forward re s 0); true
3690 with Not_found -> false)
3691 then o :: accu
3692 else accu
3694 loop accu (n-1)
3696 loop [] (Array.length m_items - 1)
3698 method denarrow =
3699 m_orig_items <- (
3700 if usebookmarks
3701 then Array.of_list state.bookmarks
3702 else state.outlines
3704 m_items <- m_orig_items
3706 method remove m =
3707 if usebookmarks
3708 then
3709 if m >= 0 && m < Array.length m_items
3710 then (
3711 m_hadremovals <- true;
3712 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3713 let n = if n >= m then n+1 else n in
3714 m_items.(n)
3718 method reset anchor items =
3719 m_hadremovals <- false;
3720 if m_orig_items == empty || m_prev_items != items
3721 then (
3722 m_orig_items <- items;
3723 if String.length m_narrow_pattern = 0
3724 then m_items <- items;
3726 m_prev_items <- items;
3727 let rely = getanchory anchor in
3728 let active =
3729 let rec loop n best bestd =
3730 if n = Array.length m_items
3731 then best
3732 else
3733 let (_, _, anchor) = m_items.(n) in
3734 let orely = getanchory anchor in
3735 let d = abs (orely - rely) in
3736 if d < bestd
3737 then loop (n+1) n d
3738 else loop (n+1) best bestd
3740 loop 0 ~-1 max_int
3742 m_active <- active;
3743 m_first <- firstof m_first active
3744 end)
3747 let enterselector usebookmarks =
3748 let source = outlinesource usebookmarks in
3749 fun errmsg ->
3750 let outlines =
3751 if usebookmarks
3752 then Array.of_list state.bookmarks
3753 else state.outlines
3755 if Array.length outlines = 0
3756 then (
3757 showtext ' ' errmsg;
3759 else (
3760 state.text <- source#greetmsg;
3761 Wsi.setcursor Wsi.CURSOR_INHERIT;
3762 let anchor = getanchor () in
3763 source#reset anchor outlines;
3764 state.uioh <- coe (new outlinelistview ~source);
3765 G.postRedisplay "enter selector";
3769 let enteroutlinemode =
3770 let f = enterselector false in
3771 fun ()-> f "Document has no outline";
3774 let enterbookmarkmode =
3775 let f = enterselector true in
3776 fun () -> f "Document has no bookmarks (yet)";
3779 let color_of_string s =
3780 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3781 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3785 let color_to_string (r, g, b) =
3786 let r = truncate (r *. 256.0)
3787 and g = truncate (g *. 256.0)
3788 and b = truncate (b *. 256.0) in
3789 Printf.sprintf "%d/%d/%d" r g b
3792 let irect_of_string s =
3793 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3796 let irect_to_string (x0,y0,x1,y1) =
3797 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3800 let makecheckers () =
3801 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3802 following to say:
3803 converted by Issac Trotts. July 25, 2002 *)
3804 let image_height = 64
3805 and image_width = 64 in
3807 let make_image () =
3808 let image =
3809 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3811 for i = 0 to image_width - 1 do
3812 for j = 0 to image_height - 1 do
3813 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3814 (if (i land 8 ) lxor (j land 8) = 0
3815 then [|255;255;255|] else [|200;200;200|])
3816 done
3817 done;
3818 image
3820 let image = make_image () in
3821 let id = GlTex.gen_texture () in
3822 GlTex.bind_texture `texture_2d id;
3823 GlPix.store (`unpack_alignment 1);
3824 GlTex.image2d image;
3825 List.iter (GlTex.parameter ~target:`texture_2d)
3826 [ `wrap_s `repeat;
3827 `wrap_t `repeat;
3828 `mag_filter `nearest;
3829 `min_filter `nearest ];
3833 let setcheckers enabled =
3834 match state.texid with
3835 | None ->
3836 if enabled then state.texid <- Some (makecheckers ())
3838 | Some texid ->
3839 if not enabled
3840 then (
3841 GlTex.delete_texture texid;
3842 state.texid <- None;
3846 let int_of_string_with_suffix s =
3847 let l = String.length s in
3848 let s1, shift =
3849 if l > 1
3850 then
3851 let suffix = Char.lowercase s.[l-1] in
3852 match suffix with
3853 | 'k' -> String.sub s 0 (l-1), 10
3854 | 'm' -> String.sub s 0 (l-1), 20
3855 | 'g' -> String.sub s 0 (l-1), 30
3856 | _ -> s, 0
3857 else s, 0
3859 let n = int_of_string s1 in
3860 let m = n lsl shift in
3861 if m < 0 || m < n
3862 then raise (Failure "value too large")
3863 else m
3866 let string_with_suffix_of_int n =
3867 if n = 0
3868 then "0"
3869 else
3870 let n, s =
3871 if n land ((1 lsl 20) - 1) = 0
3872 then n lsr 20, "M"
3873 else (
3874 if n land ((1 lsl 10) - 1) = 0
3875 then n lsr 10, "K"
3876 else n, ""
3879 let rec loop s n =
3880 let h = n mod 1000 in
3881 let n = n / 1000 in
3882 if n = 0
3883 then string_of_int h ^ s
3884 else (
3885 let s = Printf.sprintf "_%03d%s" h s in
3886 loop s n
3889 loop "" n ^ s;
3892 let defghyllscroll = (40, 8, 32);;
3893 let ghyllscroll_of_string s =
3894 let (n, a, b) as nab =
3895 if s = "default"
3896 then defghyllscroll
3897 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3899 if n <= a || n <= b || a >= b
3900 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3901 nab;
3904 let ghyllscroll_to_string ((n, a, b) as nab) =
3905 if nab = defghyllscroll
3906 then "default"
3907 else Printf.sprintf "%d,%d,%d" n a b;
3910 let describe_location () =
3911 let f (fn, _) l =
3912 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3914 let fn, ln = List.fold_left f (-1, -1) state.layout in
3915 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3916 let percent =
3917 if maxy <= 0
3918 then 100.
3919 else (100. *. (float state.y /. float maxy))
3921 if fn = ln
3922 then
3923 Printf.sprintf "page %d of %d [%.2f%%]"
3924 (fn+1) state.pagecount percent
3925 else
3926 Printf.sprintf
3927 "pages %d-%d of %d [%.2f%%]"
3928 (fn+1) (ln+1) state.pagecount percent
3931 let setpresentationmode v =
3932 let (n, _, _) = getanchor () in
3933 let _, h = getpageyh n in
3934 state.anchor <- (n, 0.0, float (calcips h));
3935 conf.presentation <- v;
3936 if conf.presentation
3937 then (
3938 if not conf.scrollbarinpm
3939 then state.scrollw <- 0;
3941 else state.scrollw <- conf.scrollbw;
3942 represent ();
3945 let enterinfomode =
3946 let btos b = if b then "\xe2\x88\x9a" else "" in
3947 let showextended = ref false in
3948 let leave mode = function
3949 | Confirm -> state.mode <- mode
3950 | Cancel -> state.mode <- mode in
3951 let src =
3952 (object
3953 val mutable m_first_time = true
3954 val mutable m_l = []
3955 val mutable m_a = [||]
3956 val mutable m_prev_uioh = nouioh
3957 val mutable m_prev_mode = View
3959 inherit lvsourcebase
3961 method reset prev_mode prev_uioh =
3962 m_a <- Array.of_list (List.rev m_l);
3963 m_l <- [];
3964 m_prev_mode <- prev_mode;
3965 m_prev_uioh <- prev_uioh;
3966 if m_first_time
3967 then (
3968 let rec loop n =
3969 if n >= Array.length m_a
3970 then ()
3971 else
3972 match m_a.(n) with
3973 | _, _, _, Action _ -> m_active <- n
3974 | _ -> loop (n+1)
3976 loop 0;
3977 m_first_time <- false;
3980 method int name get set =
3981 m_l <-
3982 (name, `int get, 1, Action (
3983 fun u ->
3984 let ondone s =
3985 try set (int_of_string s)
3986 with exn ->
3987 state.text <- Printf.sprintf "bad integer `%s': %s"
3988 s (Printexc.to_string exn)
3990 state.text <- "";
3991 let te = name ^ ": ", "", None, intentry, ondone, true in
3992 state.mode <- Textentry (te, leave m_prev_mode);
3994 )) :: m_l
3996 method int_with_suffix name get set =
3997 m_l <-
3998 (name, `intws get, 1, Action (
3999 fun u ->
4000 let ondone s =
4001 try set (int_of_string_with_suffix s)
4002 with exn ->
4003 state.text <- Printf.sprintf "bad integer `%s': %s"
4004 s (Printexc.to_string exn)
4006 state.text <- "";
4007 let te =
4008 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4010 state.mode <- Textentry (te, leave m_prev_mode);
4012 )) :: m_l
4014 method bool ?(offset=1) ?(btos=btos) name get set =
4015 m_l <-
4016 (name, `bool (btos, get), offset, Action (
4017 fun u ->
4018 let v = get () in
4019 set (not v);
4021 )) :: m_l
4023 method color name get set =
4024 m_l <-
4025 (name, `color get, 1, Action (
4026 fun u ->
4027 let invalid = (nan, nan, nan) in
4028 let ondone s =
4029 let c =
4030 try color_of_string s
4031 with exn ->
4032 state.text <- Printf.sprintf "bad color `%s': %s"
4033 s (Printexc.to_string exn);
4034 invalid
4036 if c <> invalid
4037 then set c;
4039 let te = name ^ ": ", "", None, textentry, ondone, true in
4040 state.text <- color_to_string (get ());
4041 state.mode <- Textentry (te, leave m_prev_mode);
4043 )) :: m_l
4045 method string name get set =
4046 m_l <-
4047 (name, `string get, 1, Action (
4048 fun u ->
4049 let ondone s = set s in
4050 let te = name ^ ": ", "", None, textentry, ondone, true in
4051 state.mode <- Textentry (te, leave m_prev_mode);
4053 )) :: m_l
4055 method colorspace name get set =
4056 m_l <-
4057 (name, `string get, 1, Action (
4058 fun _ ->
4059 let source =
4060 let vals = [| "rgb"; "bgr"; "gray" |] in
4061 (object
4062 inherit lvsourcebase
4064 initializer
4065 m_active <- int_of_colorspace conf.colorspace;
4066 m_first <- 0;
4068 method getitemcount = Array.length vals
4069 method getitem n = (vals.(n), 0)
4070 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4071 ignore (uioh, first, pan, qsearch);
4072 if not cancel then set active;
4073 None
4074 method hasaction _ = true
4075 end)
4077 state.text <- "";
4078 let modehash = findkeyhash conf "info" in
4079 coe (new listview ~source ~trusted:true ~modehash)
4080 )) :: m_l
4082 method caption s offset =
4083 m_l <- (s, `empty, offset, Noaction) :: m_l
4085 method caption2 s f offset =
4086 m_l <- (s, `string f, offset, Noaction) :: m_l
4088 method getitemcount = Array.length m_a
4090 method getitem n =
4091 let tostr = function
4092 | `int f -> string_of_int (f ())
4093 | `intws f -> string_with_suffix_of_int (f ())
4094 | `string f -> f ()
4095 | `color f -> color_to_string (f ())
4096 | `bool (btos, f) -> btos (f ())
4097 | `empty -> ""
4099 let name, t, offset, _ = m_a.(n) in
4100 ((let s = tostr t in
4101 if String.length s > 0
4102 then Printf.sprintf "%s\t%s" name s
4103 else name),
4104 offset)
4106 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4107 let uiohopt =
4108 if not cancel
4109 then (
4110 m_qsearch <- qsearch;
4111 let uioh =
4112 match m_a.(active) with
4113 | _, _, _, Action f -> f uioh
4114 | _ -> uioh
4116 Some uioh
4118 else None
4120 m_active <- active;
4121 m_first <- first;
4122 m_pan <- pan;
4123 uiohopt
4125 method hasaction n =
4126 match m_a.(n) with
4127 | _, _, _, Action _ -> true
4128 | _ -> false
4129 end)
4131 let rec fillsrc prevmode prevuioh =
4132 let sep () = src#caption "" 0 in
4133 let colorp name get set =
4134 src#string name
4135 (fun () -> color_to_string (get ()))
4136 (fun v ->
4138 let c = color_of_string v in
4139 set c
4140 with exn ->
4141 state.text <- Printf.sprintf "bad color `%s': %s"
4142 v (Printexc.to_string exn);
4145 let oldmode = state.mode in
4146 let birdseye = isbirdseye state.mode in
4148 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4150 src#bool "presentation mode"
4151 (fun () -> conf.presentation)
4152 (fun v -> setpresentationmode v);
4154 src#bool "ignore case in searches"
4155 (fun () -> conf.icase)
4156 (fun v -> conf.icase <- v);
4158 src#bool "preload"
4159 (fun () -> conf.preload)
4160 (fun v -> conf.preload <- v);
4162 src#bool "highlight links"
4163 (fun () -> conf.hlinks)
4164 (fun v -> conf.hlinks <- v);
4166 src#bool "under info"
4167 (fun () -> conf.underinfo)
4168 (fun v -> conf.underinfo <- v);
4170 src#bool "persistent bookmarks"
4171 (fun () -> conf.savebmarks)
4172 (fun v -> conf.savebmarks <- v);
4174 src#bool "proportional display"
4175 (fun () -> conf.proportional)
4176 (fun v -> reqlayout conf.angle v);
4178 src#bool "trim margins"
4179 (fun () -> conf.trimmargins)
4180 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4182 src#bool "persistent location"
4183 (fun () -> conf.jumpback)
4184 (fun v -> conf.jumpback <- v);
4186 sep ();
4187 src#int "inter-page space"
4188 (fun () -> conf.interpagespace)
4189 (fun n ->
4190 conf.interpagespace <- n;
4191 docolumns conf.columns;
4192 let pageno, py =
4193 match state.layout with
4194 | [] -> 0, 0
4195 | l :: _ ->
4196 l.pageno, l.pagey
4198 state.maxy <- calcheight ();
4199 let y = getpagey pageno in
4200 gotoy (y + py)
4203 src#int "page bias"
4204 (fun () -> conf.pagebias)
4205 (fun v -> conf.pagebias <- v);
4207 src#int "scroll step"
4208 (fun () -> conf.scrollstep)
4209 (fun n -> conf.scrollstep <- n);
4211 src#int "horizontal scroll step"
4212 (fun () -> conf.hscrollstep)
4213 (fun v -> conf.hscrollstep <- v);
4215 src#int "auto scroll step"
4216 (fun () ->
4217 match state.autoscroll with
4218 | Some step -> step
4219 | _ -> conf.autoscrollstep)
4220 (fun n ->
4221 if state.autoscroll <> None
4222 then state.autoscroll <- Some n;
4223 conf.autoscrollstep <- n);
4225 src#int "zoom"
4226 (fun () -> truncate (conf.zoom *. 100.))
4227 (fun v -> setzoom ((float v) /. 100.));
4229 src#int "rotation"
4230 (fun () -> conf.angle)
4231 (fun v -> reqlayout v conf.proportional);
4233 src#int "scroll bar width"
4234 (fun () -> state.scrollw)
4235 (fun v ->
4236 state.scrollw <- v;
4237 conf.scrollbw <- v;
4238 reshape conf.winw conf.winh;
4241 src#int "scroll handle height"
4242 (fun () -> conf.scrollh)
4243 (fun v -> conf.scrollh <- v;);
4245 src#int "thumbnail width"
4246 (fun () -> conf.thumbw)
4247 (fun v ->
4248 conf.thumbw <- min 4096 v;
4249 match oldmode with
4250 | Birdseye beye ->
4251 leavebirdseye beye false;
4252 enterbirdseye ()
4253 | _ -> ()
4256 let mode = state.mode in
4257 src#string "columns"
4258 (fun () ->
4259 match conf.columns with
4260 | Csingle _ -> "1"
4261 | Cmulti (multi, _) -> multicolumns_to_string multi
4262 | Csplit (count, _) -> "-" ^ string_of_int count
4264 (fun v ->
4265 let n, a, b = multicolumns_of_string v in
4266 setcolumns mode n a b);
4268 sep ();
4269 src#caption "Presentation mode" 0;
4270 src#bool "scrollbar visible"
4271 (fun () -> conf.scrollbarinpm)
4272 (fun v ->
4273 if v != conf.scrollbarinpm
4274 then (
4275 conf.scrollbarinpm <- v;
4276 if conf.presentation
4277 then (
4278 state.scrollw <- if v then conf.scrollbw else 0;
4279 reshape conf.winw conf.winh;
4284 sep ();
4285 src#caption "Pixmap cache" 0;
4286 src#int_with_suffix "size (advisory)"
4287 (fun () -> conf.memlimit)
4288 (fun v -> conf.memlimit <- v);
4290 src#caption2 "used"
4291 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4292 (string_with_suffix_of_int state.memused)
4293 (Hashtbl.length state.tilemap)) 1;
4295 sep ();
4296 src#caption "Layout" 0;
4297 src#caption2 "Dimension"
4298 (fun () ->
4299 Printf.sprintf "%dx%d (virtual %dx%d)"
4300 conf.winw conf.winh
4301 state.w state.maxy)
4303 if conf.debug
4304 then
4305 src#caption2 "Position" (fun () ->
4306 Printf.sprintf "%dx%d" state.x state.y
4308 else
4309 src#caption2 "Visible" (fun () -> describe_location ()) 1
4312 sep ();
4313 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4314 "Save these parameters as global defaults at exit"
4315 (fun () -> conf.bedefault)
4316 (fun v -> conf.bedefault <- v)
4319 sep ();
4320 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4321 src#bool ~offset:0 ~btos "Extended parameters"
4322 (fun () -> !showextended)
4323 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4324 if !showextended
4325 then (
4326 src#bool "checkers"
4327 (fun () -> conf.checkers)
4328 (fun v -> conf.checkers <- v; setcheckers v);
4329 src#bool "update cursor"
4330 (fun () -> conf.updatecurs)
4331 (fun v -> conf.updatecurs <- v);
4332 src#bool "verbose"
4333 (fun () -> conf.verbose)
4334 (fun v -> conf.verbose <- v);
4335 src#bool "invert colors"
4336 (fun () -> conf.invert)
4337 (fun v -> conf.invert <- v);
4338 src#bool "max fit"
4339 (fun () -> conf.maxhfit)
4340 (fun v -> conf.maxhfit <- v);
4341 src#bool "redirect stderr"
4342 (fun () -> conf.redirectstderr)
4343 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4344 src#string "uri launcher"
4345 (fun () -> conf.urilauncher)
4346 (fun v -> conf.urilauncher <- v);
4347 src#string "path launcher"
4348 (fun () -> conf.pathlauncher)
4349 (fun v -> conf.pathlauncher <- v);
4350 src#string "tile size"
4351 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4352 (fun v ->
4354 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4355 conf.tilew <- max 64 w;
4356 conf.tileh <- max 64 h;
4357 flushtiles ();
4358 with exn ->
4359 state.text <- Printf.sprintf "bad tile size `%s': %s"
4360 v (Printexc.to_string exn));
4361 src#int "texture count"
4362 (fun () -> conf.texcount)
4363 (fun v ->
4364 if realloctexts v
4365 then conf.texcount <- v
4366 else showtext '!' " Failed to set texture count please retry later"
4368 src#int "slice height"
4369 (fun () -> conf.sliceheight)
4370 (fun v ->
4371 conf.sliceheight <- v;
4372 wcmd "sliceh %d" conf.sliceheight;
4374 src#int "anti-aliasing level"
4375 (fun () -> conf.aalevel)
4376 (fun v ->
4377 conf.aalevel <- bound v 0 8;
4378 state.anchor <- getanchor ();
4379 opendoc state.path state.password;
4381 src#string "page scroll scaling factor"
4382 (fun () -> string_of_float conf.pgscale)
4383 (fun v ->
4385 let s = float_of_string v in
4386 conf.pgscale <- s
4387 with exn ->
4388 state.text <- Printf.sprintf
4389 "bad page scroll scaling factor `%s': %s"
4390 v (Printexc.to_string exn)
4393 src#int "ui font size"
4394 (fun () -> fstate.fontsize)
4395 (fun v -> setfontsize (bound v 5 100));
4396 src#int "hint font size"
4397 (fun () -> conf.hfsize)
4398 (fun v -> conf.hfsize <- bound v 5 100);
4399 colorp "background color"
4400 (fun () -> conf.bgcolor)
4401 (fun v -> conf.bgcolor <- v);
4402 src#bool "crop hack"
4403 (fun () -> conf.crophack)
4404 (fun v -> conf.crophack <- v);
4405 src#bool "multi column centering"
4406 (fun () -> conf.multicenter)
4407 (fun v -> conf.multicenter <- v; represent ());
4408 src#string "trim fuzz"
4409 (fun () -> irect_to_string conf.trimfuzz)
4410 (fun v ->
4412 conf.trimfuzz <- irect_of_string v;
4413 if conf.trimmargins
4414 then settrim true conf.trimfuzz;
4415 with exn ->
4416 state.text <- Printf.sprintf "bad irect `%s': %s"
4417 v (Printexc.to_string exn)
4419 src#string "throttle"
4420 (fun () ->
4421 match conf.maxwait with
4422 | None -> "show place holder if page is not ready"
4423 | Some time ->
4424 if time = infinity
4425 then "wait for page to fully render"
4426 else
4427 "wait " ^ string_of_float time
4428 ^ " seconds before showing placeholder"
4430 (fun v ->
4432 let f = float_of_string v in
4433 if f <= 0.0
4434 then conf.maxwait <- None
4435 else conf.maxwait <- Some f
4436 with exn ->
4437 state.text <- Printf.sprintf "bad time `%s': %s"
4438 v (Printexc.to_string exn)
4440 src#string "ghyll scroll"
4441 (fun () ->
4442 match conf.ghyllscroll with
4443 | None -> ""
4444 | Some nab -> ghyllscroll_to_string nab
4446 (fun v ->
4448 let gs =
4449 if String.length v = 0
4450 then None
4451 else Some (ghyllscroll_of_string v)
4453 conf.ghyllscroll <- gs
4454 with exn ->
4455 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4456 v (Printexc.to_string exn)
4458 src#string "selection command"
4459 (fun () -> conf.selcmd)
4460 (fun v -> conf.selcmd <- v);
4461 src#colorspace "color space"
4462 (fun () -> colorspace_to_string conf.colorspace)
4463 (fun v ->
4464 conf.colorspace <- colorspace_of_int v;
4465 wcmd "cs %d" v;
4466 load state.layout;
4470 sep ();
4471 src#caption "Document" 0;
4472 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4473 src#caption2 "Pages"
4474 (fun () -> string_of_int state.pagecount) 1;
4475 src#caption2 "Dimensions"
4476 (fun () -> string_of_int (List.length state.pdims)) 1;
4477 if conf.trimmargins
4478 then (
4479 sep ();
4480 src#caption "Trimmed margins" 0;
4481 src#caption2 "Dimensions"
4482 (fun () -> string_of_int (List.length state.pdims)) 1;
4485 sep ();
4486 src#caption "OpenGL" 0;
4487 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4488 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4489 src#reset prevmode prevuioh;
4491 fun () ->
4492 state.text <- "";
4493 let prevmode = state.mode
4494 and prevuioh = state.uioh in
4495 fillsrc prevmode prevuioh;
4496 let source = (src :> lvsource) in
4497 let modehash = findkeyhash conf "info" in
4498 state.uioh <- coe (object (self)
4499 inherit listview ~source ~trusted:true ~modehash as super
4500 val mutable m_prevmemused = 0
4501 method infochanged = function
4502 | Memused ->
4503 if m_prevmemused != state.memused
4504 then (
4505 m_prevmemused <- state.memused;
4506 G.postRedisplay "memusedchanged";
4508 | Pdim -> G.postRedisplay "pdimchanged"
4509 | Docinfo -> fillsrc prevmode prevuioh
4511 method key key mask =
4512 if not (Wsi.withctrl mask)
4513 then
4514 match key with
4515 | 0xff51 -> coe (self#updownlevel ~-1)
4516 | 0xff53 -> coe (self#updownlevel 1)
4517 | _ -> super#key key mask
4518 else super#key key mask
4519 end);
4520 G.postRedisplay "info";
4523 let enterhelpmode =
4524 let source =
4525 (object
4526 inherit lvsourcebase
4527 method getitemcount = Array.length state.help
4528 method getitem n =
4529 let s, n, _ = state.help.(n) in
4530 (s, n)
4532 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4533 let optuioh =
4534 if not cancel
4535 then (
4536 m_qsearch <- qsearch;
4537 match state.help.(active) with
4538 | _, _, Action f -> Some (f uioh)
4539 | _ -> Some (uioh)
4541 else None
4543 m_active <- active;
4544 m_first <- first;
4545 m_pan <- pan;
4546 optuioh
4548 method hasaction n =
4549 match state.help.(n) with
4550 | _, _, Action _ -> true
4551 | _ -> false
4553 initializer
4554 m_active <- -1
4555 end)
4556 in fun () ->
4557 let modehash = findkeyhash conf "help" in
4558 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4559 G.postRedisplay "help";
4562 let entermsgsmode =
4563 let msgsource =
4564 let re = Str.regexp "[\r\n]" in
4565 (object
4566 inherit lvsourcebase
4567 val mutable m_items = [||]
4569 method getitemcount = 1 + Array.length m_items
4571 method getitem n =
4572 if n = 0
4573 then "[Clear]", 0
4574 else m_items.(n-1), 0
4576 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4577 ignore uioh;
4578 if not cancel
4579 then (
4580 if active = 0
4581 then Buffer.clear state.errmsgs;
4582 m_qsearch <- qsearch;
4584 m_active <- active;
4585 m_first <- first;
4586 m_pan <- pan;
4587 None
4589 method hasaction n =
4590 n = 0
4592 method reset =
4593 state.newerrmsgs <- false;
4594 let l = Str.split re (Buffer.contents state.errmsgs) in
4595 m_items <- Array.of_list l
4597 initializer
4598 m_active <- 0
4599 end)
4600 in fun () ->
4601 state.text <- "";
4602 msgsource#reset;
4603 let source = (msgsource :> lvsource) in
4604 let modehash = findkeyhash conf "listview" in
4605 state.uioh <- coe (object
4606 inherit listview ~source ~trusted:false ~modehash as super
4607 method display =
4608 if state.newerrmsgs
4609 then msgsource#reset;
4610 super#display
4611 end);
4612 G.postRedisplay "msgs";
4615 let quickbookmark ?title () =
4616 match state.layout with
4617 | [] -> ()
4618 | l :: _ ->
4619 let title =
4620 match title with
4621 | None ->
4622 let sec = Unix.gettimeofday () in
4623 let tm = Unix.localtime sec in
4624 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4625 (l.pageno+1)
4626 tm.Unix.tm_mday
4627 tm.Unix.tm_mon
4628 (tm.Unix.tm_year + 1900)
4629 tm.Unix.tm_hour
4630 tm.Unix.tm_min
4631 | Some title -> title
4633 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4636 let doreshape w h =
4637 state.fullscreen <- None;
4638 Wsi.reshape w h;
4641 let setautoscrollspeed step goingdown =
4642 let incr = max 1 ((abs step) / 2) in
4643 let incr = if goingdown then incr else -incr in
4644 let astep = step + incr in
4645 state.autoscroll <- Some astep;
4648 let gotounder = function
4649 | Ulinkgoto (pageno, top) ->
4650 if pageno >= 0
4651 then (
4652 addnav ();
4653 gotopage1 pageno top;
4656 | Ulinkuri s ->
4657 gotouri s
4659 | Uremote (filename, pageno) ->
4660 let path =
4661 if Sys.file_exists filename
4662 then filename
4663 else
4664 let dir = Filename.dirname state.path in
4665 let path = Filename.concat dir filename in
4666 if Sys.file_exists path
4667 then path
4668 else ""
4670 if String.length path > 0
4671 then (
4672 let anchor = getanchor () in
4673 let ranchor = state.path, state.password, anchor in
4674 state.anchor <- (pageno, 0.0, 0.0);
4675 state.ranchors <- ranchor :: state.ranchors;
4676 opendoc path "";
4678 else showtext '!' ("Could not find " ^ filename)
4680 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4683 let canpan () =
4684 match conf.columns with
4685 | Csplit _ -> true
4686 | _ -> conf.zoom > 1.0
4689 let viewkeyboard key mask =
4690 let enttext te =
4691 let mode = state.mode in
4692 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4693 state.text <- "";
4694 enttext ();
4695 G.postRedisplay "view:enttext"
4697 let ctrl = Wsi.withctrl mask in
4698 match key with
4699 | 81 -> (* Q *)
4700 exit 0
4702 | 0xff63 -> (* insert *)
4703 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4704 then (
4705 state.mode <- LinkNav (Ltgendir 0);
4706 gotoy state.y;
4708 else showtext '!' "Keyboard link navigation does not work under rotation"
4710 | 0xff1b | 113 -> (* escape / q *)
4711 begin match state.mstate with
4712 | Mzoomrect _ ->
4713 state.mstate <- Mnone;
4714 Wsi.setcursor Wsi.CURSOR_INHERIT;
4715 G.postRedisplay "kill zoom rect";
4716 | _ ->
4717 match state.ranchors with
4718 | [] -> raise Quit
4719 | (path, password, anchor) :: rest ->
4720 state.ranchors <- rest;
4721 state.anchor <- anchor;
4722 opendoc path password
4723 end;
4725 | 0xff08 -> (* backspace *)
4726 let y = getnav ~-1 in
4727 gotoy_and_clear_text y
4729 | 111 -> (* o *)
4730 enteroutlinemode ()
4732 | 117 -> (* u *)
4733 state.rects <- [];
4734 state.text <- "";
4735 G.postRedisplay "dehighlight";
4737 | 47 | 63 -> (* / ? *)
4738 let ondone isforw s =
4739 cbput state.hists.pat s;
4740 state.searchpattern <- s;
4741 search s isforw
4743 let s = String.create 1 in
4744 s.[0] <- Char.chr key;
4745 enttext (s, "", Some (onhist state.hists.pat),
4746 textentry, ondone (key = 47), true)
4748 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4749 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4750 setzoom (conf.zoom +. incr)
4752 | 43 | 0xffab -> (* + *)
4753 let ondone s =
4754 let n =
4755 try int_of_string s with exc ->
4756 state.text <- Printf.sprintf "bad integer `%s': %s"
4757 s (Printexc.to_string exc);
4758 max_int
4760 if n != max_int
4761 then (
4762 conf.pagebias <- n;
4763 state.text <- "page bias is now " ^ string_of_int n;
4766 enttext ("page bias: ", "", None, intentry, ondone, true)
4768 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4769 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4770 setzoom (max 0.01 (conf.zoom -. decr))
4772 | 45 | 0xffad -> (* - *)
4773 let ondone msg = state.text <- msg in
4774 enttext (
4775 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4776 optentry state.mode, ondone, true
4779 | 48 when ctrl -> (* ctrl-0 *)
4780 setzoom 1.0
4782 | 49 when ctrl -> (* ctrl-1 *)
4783 let cols =
4784 match conf.columns with
4785 | Csingle _ | Cmulti _ -> 1
4786 | Csplit (n, _) -> n
4788 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4789 if zoom < 1.0
4790 then setzoom zoom
4792 | 0xffc6 -> (* f9 *)
4793 togglebirdseye ()
4795 | 57 when ctrl -> (* ctrl-9 *)
4796 togglebirdseye ()
4798 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4799 when not ctrl -> (* 0..9 *)
4800 let ondone s =
4801 let n =
4802 try int_of_string s with exc ->
4803 state.text <- Printf.sprintf "bad integer `%s': %s"
4804 s (Printexc.to_string exc);
4807 if n >= 0
4808 then (
4809 addnav ();
4810 cbput state.hists.pag (string_of_int n);
4811 gotopage1 (n + conf.pagebias - 1) 0;
4814 let pageentry text key =
4815 match Char.unsafe_chr key with
4816 | 'g' -> TEdone text
4817 | _ -> intentry text key
4819 let text = "x" in text.[0] <- Char.chr key;
4820 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4822 | 98 -> (* b *)
4823 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4824 reshape conf.winw conf.winh;
4826 | 108 -> (* l *)
4827 conf.hlinks <- not conf.hlinks;
4828 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4829 G.postRedisplay "toggle highlightlinks";
4831 | 70 -> (* F *)
4832 state.glinks <- true;
4833 let mode = state.mode in
4834 state.mode <- Textentry (
4835 (":", "", None, linknentry, linkndone (fun under ->
4836 addnav ();
4837 gotounder under
4838 ), false
4839 ), fun _ ->
4840 state.glinks <- false;
4841 state.mode <- mode
4843 state.text <- "";
4844 G.postRedisplay "view:linkent(F)"
4846 | 121 -> (* y *)
4847 state.glinks <- true;
4848 let mode = state.mode in
4849 state.mode <- Textentry (
4850 (":", "", None, linknentry, linkndone (fun under ->
4851 match Ne.pipe () with
4852 | Ne.Exn exn ->
4853 showtext '!' (Printf.sprintf "pipe failed: %s"
4854 (Printexc.to_string exn));
4855 | Ne.Res (r, w) ->
4856 let popened =
4857 try popen conf.selcmd [r, 0; w, -1]; true
4858 with exn ->
4859 showtext '!'
4860 (Printf.sprintf "failed to execute %s: %s"
4861 conf.selcmd (Printexc.to_string exn));
4862 false
4864 let clo cap fd =
4865 Ne.clo fd (fun msg ->
4866 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4869 let s = undertext under in
4870 if popened
4871 then
4872 (try
4873 let l = String.length s in
4874 let n = Unix.write w s 0 l in
4875 if n != l
4876 then
4877 showtext '!'
4878 (Printf.sprintf
4879 "failed to write %d characters to sel pipe, wrote %d"
4882 with exn ->
4883 showtext '!'
4884 (Printf.sprintf "failed to write to sel pipe: %s"
4885 (Printexc.to_string exn)
4888 else dolog "%s" s;
4889 clo "pipe/r" r;
4890 clo "pipe/w" w;
4891 ), false
4893 fun _ ->
4894 state.glinks <- false;
4895 state.mode <- mode
4897 state.text <- "";
4898 G.postRedisplay "view:linkent"
4900 | 97 -> (* a *)
4901 begin match state.autoscroll with
4902 | Some step ->
4903 conf.autoscrollstep <- step;
4904 state.autoscroll <- None
4905 | None ->
4906 if conf.autoscrollstep = 0
4907 then state.autoscroll <- Some 1
4908 else state.autoscroll <- Some conf.autoscrollstep
4911 | 112 when ctrl -> (* ctrl-p *)
4912 launchpath ()
4914 | 80 -> (* P *)
4915 setpresentationmode (not conf.presentation);
4916 showtext ' ' ("presentation mode " ^
4917 if conf.presentation then "on" else "off");
4919 | 102 -> (* f *)
4920 begin match state.fullscreen with
4921 | None ->
4922 state.fullscreen <- Some (conf.winw, conf.winh);
4923 Wsi.fullscreen ()
4924 | Some (w, h) ->
4925 state.fullscreen <- None;
4926 doreshape w h
4929 | 103 -> (* g *)
4930 gotoy_and_clear_text 0
4932 | 71 -> (* G *)
4933 gotopage1 (state.pagecount - 1) 0
4935 | 112 | 78 -> (* p|N *)
4936 search state.searchpattern false
4938 | 110 | 0xffc0 -> (* n|F3 *)
4939 search state.searchpattern true
4941 | 116 -> (* t *)
4942 begin match state.layout with
4943 | [] -> ()
4944 | l :: _ ->
4945 gotoy_and_clear_text (getpagey l.pageno)
4948 | 32 -> (* space *)
4949 begin match state.layout with
4950 | [] -> ()
4951 | l :: rest ->
4952 match conf.columns with
4953 | Csingle _ ->
4954 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4955 then
4956 let y = clamp (pgscale conf.winh) in
4957 gotoy_and_clear_text y
4958 else
4959 let pageno = min (l.pageno+1) (state.pagecount-1) in
4960 gotoy_and_clear_text (getpagey pageno)
4961 | Cmulti ((c, _, _), _) ->
4962 let pageno = min (l.pageno+c) (state.pagecount-1) in
4963 gotoy_and_clear_text (getpagey pageno)
4964 | Csplit (n, _) ->
4965 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4966 then
4967 let pagey, pageh = getpageyh l.pageno in
4968 let pagey = pagey + pageh * l.pagecol in
4969 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4970 gotoy_and_clear_text (pagey + pageh + ips)
4973 | 0xff9f | 0xffff -> (* delete *)
4974 begin match state.layout with
4975 | [] -> ()
4976 | l :: _ ->
4977 match conf.columns with
4978 | Csingle _ ->
4979 if conf.presentation && l.pagey != 0
4980 then
4981 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4982 else
4983 let pageno = max 0 (l.pageno-1) in
4984 gotoy_and_clear_text (getpagey pageno)
4985 | Cmulti ((c, _, _), _) ->
4986 let pageno = max 0 (l.pageno-c) in
4987 gotoy_and_clear_text (getpagey pageno)
4988 | Csplit (n, _) ->
4989 let y =
4990 if l.pagecol = 0
4991 then
4992 if l.pageno = 0
4993 then l.pagey
4994 else
4995 let pageno = max 0 (l.pageno-1) in
4996 let pagey, pageh = getpageyh pageno in
4997 pagey + (n-1)*pageh
4998 else
4999 let pagey, pageh = getpageyh l.pageno in
5000 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5002 gotoy_and_clear_text y
5005 | 61 -> (* = *)
5006 showtext ' ' (describe_location ());
5008 | 119 -> (* w *)
5009 begin match state.layout with
5010 | [] -> ()
5011 | l :: _ ->
5012 doreshape (l.pagew + state.scrollw) l.pageh;
5013 G.postRedisplay "w"
5016 | 39 -> (* ' *)
5017 enterbookmarkmode ()
5019 | 104 | 0xffbe -> (* h|F1 *)
5020 enterhelpmode ()
5022 | 105 -> (* i *)
5023 enterinfomode ()
5025 | 101 when conf.redirectstderr -> (* e *)
5026 entermsgsmode ()
5028 | 109 -> (* m *)
5029 let ondone s =
5030 match state.layout with
5031 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5032 | _ -> ()
5034 enttext ("bookmark: ", "", None, textentry, ondone, true)
5036 | 126 -> (* ~ *)
5037 quickbookmark ();
5038 showtext ' ' "Quick bookmark added";
5040 | 122 -> (* z *)
5041 begin match state.layout with
5042 | l :: _ ->
5043 let rect = getpdimrect l.pagedimno in
5044 let w, h =
5045 if conf.crophack
5046 then
5047 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5048 truncate (1.2 *. (rect.(3) -. rect.(0))))
5049 else
5050 (truncate (rect.(1) -. rect.(0)),
5051 truncate (rect.(3) -. rect.(0)))
5053 let w = truncate ((float w)*.conf.zoom)
5054 and h = truncate ((float h)*.conf.zoom) in
5055 if w != 0 && h != 0
5056 then (
5057 state.anchor <- getanchor ();
5058 doreshape (w + state.scrollw) (h + conf.interpagespace)
5060 G.postRedisplay "z";
5062 | [] -> ()
5065 | 50 when ctrl -> (* ctrl-2 *)
5066 let maxw = getmaxw () in
5067 if maxw > 0.0
5068 then setzoom (maxw /. float conf.winw)
5070 | 60 | 62 -> (* < > *)
5071 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5073 | 91 | 93 -> (* [ ] *)
5074 conf.colorscale <-
5075 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5077 G.postRedisplay "brightness";
5079 | 99 when state.mode = View -> (* c *)
5080 let (c, a, b), z =
5081 match state.prevcolumns with
5082 | None -> (1, 0, 0), 1.0
5083 | Some (columns, z) ->
5084 let cab =
5085 match columns with
5086 | Csplit (c, _) -> -c, 0, 0
5087 | Cmulti ((c, a, b), _) -> c, a, b
5088 | Csingle _ -> 1, 0, 0
5090 cab, z
5092 setcolumns View c a b;
5093 setzoom z;
5095 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5096 setzoom state.prevzoom
5098 | 107 | 0xff52 -> (* k up *)
5099 begin match state.autoscroll with
5100 | None ->
5101 begin match state.mode with
5102 | Birdseye beye -> upbirdseye 1 beye
5103 | _ ->
5104 if ctrl
5105 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5106 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5108 | Some n ->
5109 setautoscrollspeed n false
5112 | 106 | 0xff54 -> (* j down *)
5113 begin match state.autoscroll with
5114 | None ->
5115 begin match state.mode with
5116 | Birdseye beye -> downbirdseye 1 beye
5117 | _ ->
5118 if ctrl
5119 then gotoy_and_clear_text (clamp (conf.winh/2))
5120 else gotoy_and_clear_text (clamp conf.scrollstep)
5122 | Some n ->
5123 setautoscrollspeed n true
5126 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5127 if canpan ()
5128 then
5129 let dx =
5130 if ctrl
5131 then conf.winw / 2
5132 else 10
5134 let dx = if key = 0xff51 then dx else -dx in
5135 state.x <- state.x + dx;
5136 gotoy_and_clear_text state.y
5137 else (
5138 state.text <- "";
5139 G.postRedisplay "lef/right"
5142 | 0xff55 -> (* prior *)
5143 let y =
5144 if ctrl
5145 then
5146 match state.layout with
5147 | [] -> state.y
5148 | l :: _ -> state.y - l.pagey
5149 else
5150 clamp (pgscale (-conf.winh))
5152 gotoghyll y
5154 | 0xff56 -> (* next *)
5155 let y =
5156 if ctrl
5157 then
5158 match List.rev state.layout with
5159 | [] -> state.y
5160 | l :: _ -> getpagey l.pageno
5161 else
5162 clamp (pgscale conf.winh)
5164 gotoghyll y
5166 | 0xff50 -> (* home *)
5167 gotoghyll 0
5168 | 0xff57 -> (* end *)
5169 gotoghyll (clamp state.maxy)
5170 | 0xff53 when Wsi.withalt mask -> (* right *)
5171 gotoghyll (getnav ~-1)
5172 | 0xff51 when Wsi.withalt mask -> (* left *)
5173 gotoghyll (getnav 1)
5175 | 114 -> (* r *)
5176 state.anchor <- getanchor ();
5177 opendoc state.path state.password
5179 | 118 when conf.debug -> (* v *)
5180 state.rects <- [];
5181 List.iter (fun l ->
5182 match getopaque l.pageno with
5183 | None -> ()
5184 | Some opaque ->
5185 let x0, y0, x1, y1 = pagebbox opaque in
5186 let a,b = float x0, float y0 in
5187 let c,d = float x1, float y0 in
5188 let e,f = float x1, float y1 in
5189 let h,j = float x0, float y1 in
5190 let rect = (a,b,c,d,e,f,h,j) in
5191 debugrect rect;
5192 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5193 ) state.layout;
5194 G.postRedisplay "v";
5196 | _ ->
5197 vlog "huh? %s" (Wsi.keyname key)
5200 let linknavkeyboard key mask linknav =
5201 let getpage pageno =
5202 let rec loop = function
5203 | [] -> None
5204 | l :: _ when l.pageno = pageno -> Some l
5205 | _ :: rest -> loop rest
5206 in loop state.layout
5208 let doexact (pageno, n) =
5209 match getopaque pageno, getpage pageno with
5210 | Some opaque, Some l ->
5211 if key = 0xff0d
5212 then
5213 let under = getlink opaque n in
5214 G.postRedisplay "link gotounder";
5215 gotounder under;
5216 state.mode <- View;
5217 else
5218 let opt, dir =
5219 match key with
5220 | 0xff50 -> (* home *)
5221 Some (findlink opaque LDfirst), -1
5223 | 0xff57 -> (* end *)
5224 Some (findlink opaque LDlast), 1
5226 | 0xff51 -> (* left *)
5227 Some (findlink opaque (LDleft n)), -1
5229 | 0xff53 -> (* right *)
5230 Some (findlink opaque (LDright n)), 1
5232 | 0xff52 -> (* up *)
5233 Some (findlink opaque (LDup n)), -1
5235 | 0xff54 -> (* down *)
5236 Some (findlink opaque (LDdown n)), 1
5238 | _ -> None, 0
5240 let pwl l dir =
5241 begin match findpwl l.pageno dir with
5242 | Pwlnotfound -> ()
5243 | Pwl pageno ->
5244 let notfound dir =
5245 state.mode <- LinkNav (Ltgendir dir);
5246 let y, h = getpageyh pageno in
5247 let y =
5248 if dir < 0
5249 then y + h - conf.winh
5250 else y
5252 gotoy y
5254 begin match getopaque pageno, getpage pageno with
5255 | Some opaque, Some _ ->
5256 let link =
5257 let ld = if dir > 0 then LDfirst else LDlast in
5258 findlink opaque ld
5260 begin match link with
5261 | Lfound m ->
5262 showlinktype (getlink opaque m);
5263 state.mode <- LinkNav (Ltexact (pageno, m));
5264 G.postRedisplay "linknav jpage";
5265 | _ -> notfound dir
5266 end;
5267 | _ -> notfound dir
5268 end;
5269 end;
5271 begin match opt with
5272 | Some Lnotfound -> pwl l dir;
5273 | Some (Lfound m) ->
5274 if m = n
5275 then pwl l dir
5276 else (
5277 let _, y0, _, y1 = getlinkrect opaque m in
5278 if y0 < l.pagey
5279 then gotopage1 l.pageno y0
5280 else (
5281 let d = fstate.fontsize + 1 in
5282 if y1 - l.pagey > l.pagevh - d
5283 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5284 else G.postRedisplay "linknav";
5286 showlinktype (getlink opaque m);
5287 state.mode <- LinkNav (Ltexact (l.pageno, m));
5290 | None -> viewkeyboard key mask
5291 end;
5292 | _ -> viewkeyboard key mask
5294 if key = 0xff63
5295 then (
5296 state.mode <- View;
5297 G.postRedisplay "leave linknav"
5299 else
5300 match linknav with
5301 | Ltgendir _ -> viewkeyboard key mask
5302 | Ltexact exact -> doexact exact
5305 let keyboard key mask =
5306 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5307 then wcmd "interrupt"
5308 else state.uioh <- state.uioh#key key mask
5311 let birdseyekeyboard key mask
5312 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5313 let incr =
5314 match conf.columns with
5315 | Csingle _ -> 1
5316 | Cmulti ((c, _, _), _) -> c
5317 | Csplit _ -> failwith "bird's eye split mode"
5319 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5320 match key with
5321 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5322 let y, h = getpageyh pageno in
5323 let top = (conf.winh - h) / 2 in
5324 gotoy (max 0 (y - top))
5325 | 0xff0d -> leavebirdseye beye false
5326 | 0xff1b -> leavebirdseye beye true (* escape *)
5327 | 0xff52 -> upbirdseye incr beye (* up *)
5328 | 0xff54 -> downbirdseye incr beye (* down *)
5329 | 0xff51 -> upbirdseye 1 beye (* left *)
5330 | 0xff53 -> downbirdseye 1 beye (* right *)
5332 | 0xff55 -> (* prior *)
5333 begin match state.layout with
5334 | l :: _ ->
5335 if l.pagey != 0
5336 then (
5337 state.mode <- Birdseye (
5338 oconf, leftx, l.pageno, hooverpageno, anchor
5340 gotopage1 l.pageno 0;
5342 else (
5343 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5344 match layout with
5345 | [] -> gotoy (clamp (-conf.winh))
5346 | l :: _ ->
5347 state.mode <- Birdseye (
5348 oconf, leftx, l.pageno, hooverpageno, anchor
5350 gotopage1 l.pageno 0
5353 | [] -> gotoy (clamp (-conf.winh))
5354 end;
5356 | 0xff56 -> (* next *)
5357 begin match List.rev state.layout with
5358 | l :: _ ->
5359 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5360 begin match layout with
5361 | [] ->
5362 let incr = l.pageh - l.pagevh in
5363 if incr = 0
5364 then (
5365 state.mode <-
5366 Birdseye (
5367 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5369 G.postRedisplay "birdseye pagedown";
5371 else gotoy (clamp (incr + conf.interpagespace*2));
5373 | l :: _ ->
5374 state.mode <-
5375 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5376 gotopage1 l.pageno 0;
5379 | [] -> gotoy (clamp conf.winh)
5380 end;
5382 | 0xff50 -> (* home *)
5383 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5384 gotopage1 0 0
5386 | 0xff57 -> (* end *)
5387 let pageno = state.pagecount - 1 in
5388 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5389 if not (pagevisible state.layout pageno)
5390 then
5391 let h =
5392 match List.rev state.pdims with
5393 | [] -> conf.winh
5394 | (_, _, h, _) :: _ -> h
5396 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5397 else G.postRedisplay "birdseye end";
5398 | _ -> viewkeyboard key mask
5401 let drawpage l linkindexbase =
5402 let color =
5403 match state.mode with
5404 | Textentry _ -> scalecolor 0.4
5405 | LinkNav _
5406 | View -> scalecolor 1.0
5407 | Birdseye (_, _, pageno, hooverpageno, _) ->
5408 if l.pageno = hooverpageno
5409 then scalecolor 0.9
5410 else (
5411 if l.pageno = pageno
5412 then scalecolor 1.0
5413 else scalecolor 0.8
5416 drawtiles l color;
5417 begin match getopaque l.pageno with
5418 | Some opaque ->
5419 if tileready l l.pagex l.pagey
5420 then
5421 let x = l.pagedispx - l.pagex
5422 and y = l.pagedispy - l.pagey in
5423 let hlmask =
5424 match conf.columns with
5425 | Csingle _ | Cmulti _ ->
5426 (if conf.hlinks then 1 else 0)
5427 + (if state.glinks
5428 && not (isbirdseye state.mode) then 2 else 0)
5429 | _ -> 0
5431 let s =
5432 match state.mode with
5433 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5434 | _ -> ""
5436 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5437 else 0
5439 | _ -> 0
5440 end;
5443 let scrollindicator () =
5444 let sbw, ph, sh = state.uioh#scrollph in
5445 let sbh, pw, sw = state.uioh#scrollpw in
5447 GlDraw.color (0.64, 0.64, 0.64);
5448 GlDraw.rect
5449 (float (conf.winw - sbw), 0.)
5450 (float conf.winw, float conf.winh)
5452 GlDraw.rect
5453 (0., float (conf.winh - sbh))
5454 (float (conf.winw - state.scrollw - 1), float conf.winh)
5456 GlDraw.color (0.0, 0.0, 0.0);
5458 GlDraw.rect
5459 (float (conf.winw - sbw), ph)
5460 (float conf.winw, ph +. sh)
5462 GlDraw.rect
5463 (pw, float (conf.winh - sbh))
5464 (pw +. sw, float conf.winh)
5468 let showsel () =
5469 match state.mstate with
5470 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5473 | Msel ((x0, y0), (x1, y1)) ->
5474 let rec loop = function
5475 | l :: ls ->
5476 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5477 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5478 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5479 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5480 then
5481 match getopaque l.pageno with
5482 | Some opaque ->
5483 let x0, y0 = pagetranslatepoint l x0 y0 in
5484 let x1, y1 = pagetranslatepoint l x1 y1 in
5485 seltext opaque (x0, y0, x1, y1);
5486 | _ -> ()
5487 else loop ls
5488 | [] -> ()
5490 loop state.layout
5493 let showrects rects =
5494 Gl.enable `blend;
5495 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5496 GlDraw.polygon_mode `both `fill;
5497 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5498 List.iter
5499 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5500 List.iter (fun l ->
5501 if l.pageno = pageno
5502 then (
5503 let dx = float (l.pagedispx - l.pagex) in
5504 let dy = float (l.pagedispy - l.pagey) in
5505 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5506 GlDraw.begins `quads;
5508 GlDraw.vertex2 (x0+.dx, y0+.dy);
5509 GlDraw.vertex2 (x1+.dx, y1+.dy);
5510 GlDraw.vertex2 (x2+.dx, y2+.dy);
5511 GlDraw.vertex2 (x3+.dx, y3+.dy);
5513 GlDraw.ends ();
5515 ) state.layout
5516 ) rects
5518 Gl.disable `blend;
5521 let display () =
5522 GlClear.color (scalecolor2 conf.bgcolor);
5523 GlClear.clear [`color];
5524 let rec loop linkindexbase = function
5525 | l :: rest ->
5526 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5527 loop linkindexbase rest
5528 | [] -> ()
5530 loop 0 state.layout;
5531 let rects =
5532 match state.mode with
5533 | LinkNav (Ltexact (pageno, linkno)) ->
5534 begin match getopaque pageno with
5535 | Some opaque ->
5536 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5537 (pageno, 5, (
5538 float x0, float y0,
5539 float x1, float y0,
5540 float x1, float y1,
5541 float x0, float y1)
5542 ) :: state.rects
5543 | None -> state.rects
5545 | _ -> state.rects
5547 showrects rects;
5548 showsel ();
5549 state.uioh#display;
5550 begin match state.mstate with
5551 | Mzoomrect ((x0, y0), (x1, y1)) ->
5552 Gl.enable `blend;
5553 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5554 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5555 GlDraw.rect (float x0, float y0)
5556 (float x1, float y1);
5557 Gl.disable `blend;
5558 | _ -> ()
5559 end;
5560 enttext ();
5561 scrollindicator ();
5562 Wsi.swapb ();
5565 let zoomrect x y x1 y1 =
5566 let x0 = min x x1
5567 and x1 = max x x1
5568 and y0 = min y y1 in
5569 gotoy (state.y + y0);
5570 state.anchor <- getanchor ();
5571 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5572 let margin =
5573 if state.w < conf.winw - state.scrollw
5574 then (conf.winw - state.scrollw - state.w) / 2
5575 else 0
5577 state.x <- (state.x + margin) - x0;
5578 setzoom zoom;
5579 Wsi.setcursor Wsi.CURSOR_INHERIT;
5580 state.mstate <- Mnone;
5583 let scrollx x =
5584 let winw = conf.winw - state.scrollw - 1 in
5585 let s = float x /. float winw in
5586 let destx = truncate (float (state.w + winw) *. s) in
5587 state.x <- winw - destx;
5588 gotoy_and_clear_text state.y;
5589 state.mstate <- Mscrollx;
5592 let scrolly y =
5593 let s = float y /. float conf.winh in
5594 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5595 gotoy_and_clear_text desty;
5596 state.mstate <- Mscrolly;
5599 let viewmouse button down x y mask =
5600 match button with
5601 | n when (n == 4 || n == 5) && not down ->
5602 if Wsi.withctrl mask
5603 then (
5604 match state.mstate with
5605 | Mzoom (oldn, i) ->
5606 if oldn = n
5607 then (
5608 if i = 2
5609 then
5610 let incr =
5611 match n with
5612 | 5 ->
5613 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5614 | _ ->
5615 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5617 let zoom = conf.zoom -. incr in
5618 setzoom zoom;
5619 state.mstate <- Mzoom (n, 0);
5620 else
5621 state.mstate <- Mzoom (n, i+1);
5623 else state.mstate <- Mzoom (n, 0)
5625 | _ -> state.mstate <- Mzoom (n, 0)
5627 else (
5628 match state.autoscroll with
5629 | Some step -> setautoscrollspeed step (n=4)
5630 | None ->
5631 let incr =
5632 if n = 4
5633 then -conf.scrollstep
5634 else conf.scrollstep
5636 let incr = incr * 2 in
5637 let y = clamp incr in
5638 gotoy_and_clear_text y
5641 | n when (n = 6 || n = 7) && not down && canpan () ->
5642 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5643 gotoy_and_clear_text state.y
5645 | 1 when Wsi.withctrl mask ->
5646 if down
5647 then (
5648 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5649 state.mstate <- Mpan (x, y)
5651 else
5652 state.mstate <- Mnone
5654 | 3 ->
5655 if down
5656 then (
5657 Wsi.setcursor Wsi.CURSOR_CYCLE;
5658 let p = (x, y) in
5659 state.mstate <- Mzoomrect (p, p)
5661 else (
5662 match state.mstate with
5663 | Mzoomrect ((x0, y0), _) ->
5664 if abs (x-x0) > 10 && abs (y - y0) > 10
5665 then zoomrect x0 y0 x y
5666 else (
5667 state.mstate <- Mnone;
5668 Wsi.setcursor Wsi.CURSOR_INHERIT;
5669 G.postRedisplay "kill accidental zoom rect";
5671 | _ ->
5672 Wsi.setcursor Wsi.CURSOR_INHERIT;
5673 state.mstate <- Mnone
5676 | 1 when x > conf.winw - state.scrollw ->
5677 if down
5678 then
5679 let _, position, sh = state.uioh#scrollph in
5680 if y > truncate position && y < truncate (position +. sh)
5681 then state.mstate <- Mscrolly
5682 else scrolly y
5683 else
5684 state.mstate <- Mnone
5686 | 1 when y > conf.winh - state.hscrollh ->
5687 if down
5688 then
5689 let _, position, sw = state.uioh#scrollpw in
5690 if x > truncate position && x < truncate (position +. sw)
5691 then state.mstate <- Mscrollx
5692 else scrollx x
5693 else
5694 state.mstate <- Mnone
5696 | 1 ->
5697 let dest = if down then getunder x y else Unone in
5698 begin match dest with
5699 | Ulinkgoto _
5700 | Ulinkuri _
5701 | Uremote _
5702 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5703 gotounder dest
5705 | Unone when down ->
5706 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5707 state.mstate <- Mpan (x, y);
5709 | Unone | Utext _ ->
5710 if down
5711 then (
5712 if conf.angle mod 360 = 0
5713 then (
5714 state.mstate <- Msel ((x, y), (x, y));
5715 G.postRedisplay "mouse select";
5718 else (
5719 match state.mstate with
5720 | Mnone -> ()
5722 | Mzoom _ | Mscrollx | Mscrolly ->
5723 state.mstate <- Mnone
5725 | Mzoomrect ((x0, y0), _) ->
5726 zoomrect x0 y0 x y
5728 | Mpan _ ->
5729 Wsi.setcursor Wsi.CURSOR_INHERIT;
5730 state.mstate <- Mnone
5732 | Msel ((_, y0), (_, y1)) ->
5733 let rec loop = function
5734 | [] -> ()
5735 | l :: rest ->
5736 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5737 || ((y1 >= l.pagedispy
5738 && y1 <= (l.pagedispy + l.pagevh)))
5739 then
5740 match getopaque l.pageno with
5741 | Some opaque ->
5742 begin
5743 match Ne.pipe () with
5744 | Ne.Exn exn ->
5745 showtext '!'
5746 (Printf.sprintf
5747 "can not create sel pipe: %s"
5748 (Printexc.to_string exn));
5749 | Ne.Res (r, w) ->
5750 let doclose what fd =
5751 Ne.clo fd (fun msg ->
5752 dolog "%s close failed: %s" what msg)
5755 popen conf.selcmd [r, 0; w, -1];
5756 copysel w opaque;
5757 doclose "pipe/r" r;
5758 G.postRedisplay "copysel";
5759 with exn ->
5760 dolog "can not execute %S: %s"
5761 conf.selcmd (Printexc.to_string exn);
5762 doclose "pipe/r" r;
5763 doclose "pipe/w" w;
5765 | None -> ()
5766 else loop rest
5768 loop state.layout;
5769 Wsi.setcursor Wsi.CURSOR_INHERIT;
5770 state.mstate <- Mnone;
5774 | _ -> ()
5777 let birdseyemouse button down x y mask
5778 (conf, leftx, _, hooverpageno, anchor) =
5779 match button with
5780 | 1 when down ->
5781 let rec loop = function
5782 | [] -> ()
5783 | l :: rest ->
5784 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5785 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5786 then (
5787 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5789 else loop rest
5791 loop state.layout
5792 | 3 -> ()
5793 | _ -> viewmouse button down x y mask
5796 let mouse button down x y mask =
5797 state.uioh <- state.uioh#button button down x y mask;
5800 let motion ~x ~y =
5801 state.uioh <- state.uioh#motion x y
5804 let pmotion ~x ~y =
5805 state.uioh <- state.uioh#pmotion x y;
5808 let uioh = object
5809 method display = ()
5811 method key key mask =
5812 begin match state.mode with
5813 | Textentry textentry -> textentrykeyboard key mask textentry
5814 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5815 | View -> viewkeyboard key mask
5816 | LinkNav linknav -> linknavkeyboard key mask linknav
5817 end;
5818 state.uioh
5820 method button button bstate x y mask =
5821 begin match state.mode with
5822 | LinkNav _
5823 | View -> viewmouse button bstate x y mask
5824 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5825 | Textentry _ -> ()
5826 end;
5827 state.uioh
5829 method motion x y =
5830 begin match state.mode with
5831 | Textentry _ -> ()
5832 | View | Birdseye _ | LinkNav _ ->
5833 match state.mstate with
5834 | Mzoom _ | Mnone -> ()
5836 | Mpan (x0, y0) ->
5837 let dx = x - x0
5838 and dy = y0 - y in
5839 state.mstate <- Mpan (x, y);
5840 if canpan ()
5841 then state.x <- state.x + dx;
5842 let y = clamp dy in
5843 gotoy_and_clear_text y
5845 | Msel (a, _) ->
5846 state.mstate <- Msel (a, (x, y));
5847 G.postRedisplay "motion select";
5849 | Mscrolly ->
5850 let y = min conf.winh (max 0 y) in
5851 scrolly y
5853 | Mscrollx ->
5854 let x = min conf.winw (max 0 x) in
5855 scrollx x
5857 | Mzoomrect (p0, _) ->
5858 state.mstate <- Mzoomrect (p0, (x, y));
5859 G.postRedisplay "motion zoomrect";
5860 end;
5861 state.uioh
5863 method pmotion x y =
5864 begin match state.mode with
5865 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5866 let rec loop = function
5867 | [] ->
5868 if hooverpageno != -1
5869 then (
5870 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5871 G.postRedisplay "pmotion birdseye no hoover";
5873 | l :: rest ->
5874 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5875 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5876 then (
5877 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5878 G.postRedisplay "pmotion birdseye hoover";
5880 else loop rest
5882 loop state.layout
5884 | Textentry _ -> ()
5886 | LinkNav _
5887 | View ->
5888 match state.mstate with
5889 | Mnone -> updateunder x y
5890 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5892 end;
5893 state.uioh
5895 method infochanged _ = ()
5897 method scrollph =
5898 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5899 let p, h = scrollph state.y maxy in
5900 state.scrollw, p, h
5902 method scrollpw =
5903 let winw = conf.winw - state.scrollw - 1 in
5904 let fwinw = float winw in
5905 let sw =
5906 let sw = fwinw /. float state.w in
5907 let sw = fwinw *. sw in
5908 max sw (float conf.scrollh)
5910 let position, sw =
5911 let f = state.w+winw in
5912 let r = float (winw-state.x) /. float f in
5913 let p = fwinw *. r in
5914 p-.sw/.2., sw
5916 let sw =
5917 if position +. sw > fwinw
5918 then fwinw -. position
5919 else sw
5921 state.hscrollh, position, sw
5923 method modehash =
5924 let modename =
5925 match state.mode with
5926 | LinkNav _ -> "links"
5927 | Textentry _ -> "textentry"
5928 | Birdseye _ -> "birdseye"
5929 | View -> "view"
5931 findkeyhash conf modename
5932 end;;
5934 module Config =
5935 struct
5936 open Parser
5938 let fontpath = ref "";;
5940 module KeyMap =
5941 Map.Make (struct type t = (int * int) let compare = compare end);;
5943 let unent s =
5944 let l = String.length s in
5945 let b = Buffer.create l in
5946 unent b s 0 l;
5947 Buffer.contents b;
5950 let home =
5951 try Sys.getenv "HOME"
5952 with exn ->
5953 prerr_endline
5954 ("Can not determine home directory location: " ^
5955 Printexc.to_string exn);
5959 let modifier_of_string = function
5960 | "alt" -> Wsi.altmask
5961 | "shift" -> Wsi.shiftmask
5962 | "ctrl" | "control" -> Wsi.ctrlmask
5963 | "meta" -> Wsi.metamask
5964 | _ -> 0
5967 let key_of_string =
5968 let r = Str.regexp "-" in
5969 fun s ->
5970 let elems = Str.full_split r s in
5971 let f n k m =
5972 let g s =
5973 let m1 = modifier_of_string s in
5974 if m1 = 0
5975 then (Wsi.namekey s, m)
5976 else (k, m lor m1)
5977 in function
5978 | Str.Delim s when n land 1 = 0 -> g s
5979 | Str.Text s -> g s
5980 | Str.Delim _ -> (k, m)
5982 let rec loop n k m = function
5983 | [] -> (k, m)
5984 | x :: xs ->
5985 let k, m = f n k m x in
5986 loop (n+1) k m xs
5988 loop 0 0 0 elems
5991 let keys_of_string =
5992 let r = Str.regexp "[ \t]" in
5993 fun s ->
5994 let elems = Str.split r s in
5995 List.map key_of_string elems
5998 let copykeyhashes c =
5999 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6002 let config_of c attrs =
6003 let apply c k v =
6005 match k with
6006 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6007 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6008 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6009 | "preload" -> { c with preload = bool_of_string v }
6010 | "page-bias" -> { c with pagebias = int_of_string v }
6011 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6012 | "horizontal-scroll-step" ->
6013 { c with hscrollstep = max (int_of_string v) 1 }
6014 | "auto-scroll-step" ->
6015 { c with autoscrollstep = max 0 (int_of_string v) }
6016 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6017 | "crop-hack" -> { c with crophack = bool_of_string v }
6018 | "throttle" ->
6019 let mw =
6020 match String.lowercase v with
6021 | "true" -> Some infinity
6022 | "false" -> None
6023 | f -> Some (float_of_string f)
6025 { c with maxwait = mw}
6026 | "highlight-links" -> { c with hlinks = bool_of_string v }
6027 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6028 | "vertical-margin" ->
6029 { c with interpagespace = max 0 (int_of_string v) }
6030 | "zoom" ->
6031 let zoom = float_of_string v /. 100. in
6032 let zoom = max zoom 0.0 in
6033 { c with zoom = zoom }
6034 | "presentation" -> { c with presentation = bool_of_string v }
6035 | "rotation-angle" -> { c with angle = int_of_string v }
6036 | "width" -> { c with winw = max 20 (int_of_string v) }
6037 | "height" -> { c with winh = max 20 (int_of_string v) }
6038 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6039 | "proportional-display" -> { c with proportional = bool_of_string v }
6040 | "pixmap-cache-size" ->
6041 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6042 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6043 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6044 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6045 | "persistent-location" -> { c with jumpback = bool_of_string v }
6046 | "background-color" -> { c with bgcolor = color_of_string v }
6047 | "scrollbar-in-presentation" ->
6048 { c with scrollbarinpm = bool_of_string v }
6049 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6050 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6051 | "mupdf-store-size" ->
6052 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6053 | "checkers" -> { c with checkers = bool_of_string v }
6054 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6055 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6056 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6057 | "uri-launcher" -> { c with urilauncher = unent v }
6058 | "path-launcher" -> { c with pathlauncher = unent v }
6059 | "color-space" -> { c with colorspace = colorspace_of_string v }
6060 | "invert-colors" -> { c with invert = bool_of_string v }
6061 | "brightness" -> { c with colorscale = float_of_string v }
6062 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6063 | "ghyllscroll" ->
6064 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6065 | "columns" ->
6066 let (n, _, _) as nab = multicolumns_of_string v in
6067 if n < 0
6068 then { c with columns = Csplit (-n, [||]) }
6069 else { c with columns = Cmulti (nab, [||]) }
6070 | "birds-eye-columns" ->
6071 { c with beyecolumns = Some (max (int_of_string v) 2) }
6072 | "selection-command" -> { c with selcmd = unent v }
6073 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6074 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6075 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6076 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6077 | _ -> c
6078 with exn ->
6079 prerr_endline ("Error processing attribute (`" ^
6080 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6083 let rec fold c = function
6084 | [] -> c
6085 | (k, v) :: rest ->
6086 let c = apply c k v in
6087 fold c rest
6089 fold { c with keyhashes = copykeyhashes c } attrs;
6092 let fromstring f pos n v d =
6093 try f v
6094 with exn ->
6095 dolog "Error processing attribute (%S=%S) at %d\n%s"
6096 n v pos (Printexc.to_string exn)
6101 let bookmark_of attrs =
6102 let rec fold title page rely visy = function
6103 | ("title", v) :: rest -> fold v page rely visy rest
6104 | ("page", v) :: rest -> fold title v rely visy rest
6105 | ("rely", v) :: rest -> fold title page v visy rest
6106 | ("visy", v) :: rest -> fold title page rely v rest
6107 | _ :: rest -> fold title page rely visy rest
6108 | [] -> title, page, rely, visy
6110 fold "invalid" "0" "0" "0" attrs
6113 let doc_of attrs =
6114 let rec fold path page rely pan visy = function
6115 | ("path", v) :: rest -> fold v page rely pan visy rest
6116 | ("page", v) :: rest -> fold path v rely pan visy rest
6117 | ("rely", v) :: rest -> fold path page v pan visy rest
6118 | ("pan", v) :: rest -> fold path page rely v visy rest
6119 | ("visy", v) :: rest -> fold path page rely pan v rest
6120 | _ :: rest -> fold path page rely pan visy rest
6121 | [] -> path, page, rely, pan, visy
6123 fold "" "0" "0" "0" "0" attrs
6126 let map_of attrs =
6127 let rec fold rs ls = function
6128 | ("out", v) :: rest -> fold v ls rest
6129 | ("in", v) :: rest -> fold rs v rest
6130 | _ :: rest -> fold ls rs rest
6131 | [] -> ls, rs
6133 fold "" "" attrs
6136 let setconf dst src =
6137 dst.scrollbw <- src.scrollbw;
6138 dst.scrollh <- src.scrollh;
6139 dst.icase <- src.icase;
6140 dst.preload <- src.preload;
6141 dst.pagebias <- src.pagebias;
6142 dst.verbose <- src.verbose;
6143 dst.scrollstep <- src.scrollstep;
6144 dst.maxhfit <- src.maxhfit;
6145 dst.crophack <- src.crophack;
6146 dst.autoscrollstep <- src.autoscrollstep;
6147 dst.maxwait <- src.maxwait;
6148 dst.hlinks <- src.hlinks;
6149 dst.underinfo <- src.underinfo;
6150 dst.interpagespace <- src.interpagespace;
6151 dst.zoom <- src.zoom;
6152 dst.presentation <- src.presentation;
6153 dst.angle <- src.angle;
6154 dst.winw <- src.winw;
6155 dst.winh <- src.winh;
6156 dst.savebmarks <- src.savebmarks;
6157 dst.memlimit <- src.memlimit;
6158 dst.proportional <- src.proportional;
6159 dst.texcount <- src.texcount;
6160 dst.sliceheight <- src.sliceheight;
6161 dst.thumbw <- src.thumbw;
6162 dst.jumpback <- src.jumpback;
6163 dst.bgcolor <- src.bgcolor;
6164 dst.scrollbarinpm <- src.scrollbarinpm;
6165 dst.tilew <- src.tilew;
6166 dst.tileh <- src.tileh;
6167 dst.mustoresize <- src.mustoresize;
6168 dst.checkers <- src.checkers;
6169 dst.aalevel <- src.aalevel;
6170 dst.trimmargins <- src.trimmargins;
6171 dst.trimfuzz <- src.trimfuzz;
6172 dst.urilauncher <- src.urilauncher;
6173 dst.colorspace <- src.colorspace;
6174 dst.invert <- src.invert;
6175 dst.colorscale <- src.colorscale;
6176 dst.redirectstderr <- src.redirectstderr;
6177 dst.ghyllscroll <- src.ghyllscroll;
6178 dst.columns <- src.columns;
6179 dst.beyecolumns <- src.beyecolumns;
6180 dst.selcmd <- src.selcmd;
6181 dst.updatecurs <- src.updatecurs;
6182 dst.pathlauncher <- src.pathlauncher;
6183 dst.keyhashes <- copykeyhashes src;
6184 dst.hfsize <- src.hfsize;
6185 dst.hscrollstep <- src.hscrollstep;
6186 dst.pgscale <- src.pgscale;
6187 dst.multicenter <- src.multicenter;
6190 let get s =
6191 let h = Hashtbl.create 10 in
6192 let dc = { defconf with angle = defconf.angle } in
6193 let rec toplevel v t spos _ =
6194 match t with
6195 | Vdata | Vcdata | Vend -> v
6196 | Vopen ("llppconfig", _, closed) ->
6197 if closed
6198 then v
6199 else { v with f = llppconfig }
6200 | Vopen _ ->
6201 error "unexpected subelement at top level" s spos
6202 | Vclose _ -> error "unexpected close at top level" s spos
6204 and llppconfig v t spos _ =
6205 match t with
6206 | Vdata | Vcdata -> v
6207 | Vend -> error "unexpected end of input in llppconfig" s spos
6208 | Vopen ("defaults", attrs, closed) ->
6209 let c = config_of dc attrs in
6210 setconf dc c;
6211 if closed
6212 then v
6213 else { v with f = defaults }
6215 | Vopen ("ui-font", attrs, closed) ->
6216 let rec getsize size = function
6217 | [] -> size
6218 | ("size", v) :: rest ->
6219 let size =
6220 fromstring int_of_string spos "size" v fstate.fontsize in
6221 getsize size rest
6222 | l -> getsize size l
6224 fstate.fontsize <- getsize fstate.fontsize attrs;
6225 if closed
6226 then v
6227 else { v with f = uifont (Buffer.create 10) }
6229 | Vopen ("doc", attrs, closed) ->
6230 let pathent, spage, srely, span, svisy = doc_of attrs in
6231 let path = unent pathent
6232 and pageno = fromstring int_of_string spos "page" spage 0
6233 and rely = fromstring float_of_string spos "rely" srely 0.0
6234 and pan = fromstring int_of_string spos "pan" span 0
6235 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6236 let c = config_of dc attrs in
6237 let anchor = (pageno, rely, visy) in
6238 if closed
6239 then (Hashtbl.add h path (c, [], pan, anchor); v)
6240 else { v with f = doc path pan anchor c [] }
6242 | Vopen _ ->
6243 error "unexpected subelement in llppconfig" s spos
6245 | Vclose "llppconfig" -> { v with f = toplevel }
6246 | Vclose _ -> error "unexpected close in llppconfig" s spos
6248 and defaults v t spos _ =
6249 match t with
6250 | Vdata | Vcdata -> v
6251 | Vend -> error "unexpected end of input in defaults" s spos
6252 | Vopen ("keymap", attrs, closed) ->
6253 let modename =
6254 try List.assoc "mode" attrs
6255 with Not_found -> "global" in
6256 if closed
6257 then v
6258 else
6259 let ret keymap =
6260 let h = findkeyhash dc modename in
6261 KeyMap.iter (Hashtbl.replace h) keymap;
6262 defaults
6264 { v with f = pkeymap ret KeyMap.empty }
6266 | Vopen (_, _, _) ->
6267 error "unexpected subelement in defaults" s spos
6269 | Vclose "defaults" ->
6270 { v with f = llppconfig }
6272 | Vclose _ -> error "unexpected close in defaults" s spos
6274 and uifont b v t spos epos =
6275 match t with
6276 | Vdata | Vcdata ->
6277 Buffer.add_substring b s spos (epos - spos);
6279 | Vopen (_, _, _) ->
6280 error "unexpected subelement in ui-font" s spos
6281 | Vclose "ui-font" ->
6282 if String.length !fontpath = 0
6283 then fontpath := Buffer.contents b;
6284 { v with f = llppconfig }
6285 | Vclose _ -> error "unexpected close in ui-font" s spos
6286 | Vend -> error "unexpected end of input in ui-font" s spos
6288 and doc path pan anchor c bookmarks v t spos _ =
6289 match t with
6290 | Vdata | Vcdata -> v
6291 | Vend -> error "unexpected end of input in doc" s spos
6292 | Vopen ("bookmarks", _, closed) ->
6293 if closed
6294 then v
6295 else { v with f = pbookmarks path pan anchor c bookmarks }
6297 | Vopen ("keymap", attrs, closed) ->
6298 let modename =
6299 try List.assoc "mode" attrs
6300 with Not_found -> "global"
6302 if closed
6303 then v
6304 else
6305 let ret keymap =
6306 let h = findkeyhash c modename in
6307 KeyMap.iter (Hashtbl.replace h) keymap;
6308 doc path pan anchor c bookmarks
6310 { v with f = pkeymap ret KeyMap.empty }
6312 | Vopen (_, _, _) ->
6313 error "unexpected subelement in doc" s spos
6315 | Vclose "doc" ->
6316 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6317 { v with f = llppconfig }
6319 | Vclose _ -> error "unexpected close in doc" s spos
6321 and pkeymap ret keymap v t spos _ =
6322 match t with
6323 | Vdata | Vcdata -> v
6324 | Vend -> error "unexpected end of input in keymap" s spos
6325 | Vopen ("map", attrs, closed) ->
6326 let r, l = map_of attrs in
6327 let kss = fromstring keys_of_string spos "in" r [] in
6328 let lss = fromstring keys_of_string spos "out" l [] in
6329 let keymap =
6330 match kss with
6331 | [] -> keymap
6332 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6333 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6335 if closed
6336 then { v with f = pkeymap ret keymap }
6337 else
6338 let f () = v in
6339 { v with f = skip "map" f }
6341 | Vopen _ ->
6342 error "unexpected subelement in keymap" s spos
6344 | Vclose "keymap" ->
6345 { v with f = ret keymap }
6347 | Vclose _ -> error "unexpected close in keymap" s spos
6349 and pbookmarks path pan anchor c bookmarks v t spos _ =
6350 match t with
6351 | Vdata | Vcdata -> v
6352 | Vend -> error "unexpected end of input in bookmarks" s spos
6353 | Vopen ("item", attrs, closed) ->
6354 let titleent, spage, srely, svisy = bookmark_of attrs in
6355 let page = fromstring int_of_string spos "page" spage 0
6356 and rely = fromstring float_of_string spos "rely" srely 0.0
6357 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6358 let bookmarks =
6359 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6361 if closed
6362 then { v with f = pbookmarks path pan anchor c bookmarks }
6363 else
6364 let f () = v in
6365 { v with f = skip "item" f }
6367 | Vopen _ ->
6368 error "unexpected subelement in bookmarks" s spos
6370 | Vclose "bookmarks" ->
6371 { v with f = doc path pan anchor c bookmarks }
6373 | Vclose _ -> error "unexpected close in bookmarks" s spos
6375 and skip tag f v t spos _ =
6376 match t with
6377 | Vdata | Vcdata -> v
6378 | Vend ->
6379 error ("unexpected end of input in skipped " ^ tag) s spos
6380 | Vopen (tag', _, closed) ->
6381 if closed
6382 then v
6383 else
6384 let f' () = { v with f = skip tag f } in
6385 { v with f = skip tag' f' }
6386 | Vclose ctag ->
6387 if tag = ctag
6388 then f ()
6389 else error ("unexpected close in skipped " ^ tag) s spos
6392 parse { f = toplevel; accu = () } s;
6393 h, dc;
6396 let do_load f ic =
6398 let len = in_channel_length ic in
6399 let s = String.create len in
6400 really_input ic s 0 len;
6401 f s;
6402 with
6403 | Parse_error (msg, s, pos) ->
6404 let subs = subs s pos in
6405 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6406 failwith ("parse error: " ^ s)
6408 | exn ->
6409 failwith ("config load error: " ^ Printexc.to_string exn)
6412 let defconfpath =
6413 let dir =
6415 let dir = Filename.concat home ".config" in
6416 if Sys.is_directory dir then dir else home
6417 with _ -> home
6419 Filename.concat dir "llpp.conf"
6422 let confpath = ref defconfpath;;
6424 let load1 f =
6425 if Sys.file_exists !confpath
6426 then
6427 match
6428 (try Some (open_in_bin !confpath)
6429 with exn ->
6430 prerr_endline
6431 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6432 Printexc.to_string exn);
6433 None
6435 with
6436 | Some ic ->
6437 let success =
6439 f (do_load get ic)
6440 with exn ->
6441 prerr_endline
6442 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6443 Printexc.to_string exn);
6444 false
6446 close_in ic;
6447 success
6449 | None -> false
6450 else
6451 f (Hashtbl.create 0, defconf)
6454 let load () =
6455 let f (h, dc) =
6456 let pc, pb, px, pa =
6458 Hashtbl.find h (Filename.basename state.path)
6459 with Not_found -> dc, [], 0, emptyanchor
6461 setconf defconf dc;
6462 setconf conf pc;
6463 state.bookmarks <- pb;
6464 state.x <- px;
6465 state.scrollw <- conf.scrollbw;
6466 if conf.jumpback
6467 then state.anchor <- pa;
6468 cbput state.hists.nav pa;
6469 true
6471 load1 f
6474 let add_attrs bb always dc c =
6475 let ob s a b =
6476 if always || a != b
6477 then Printf.bprintf bb "\n %s='%b'" s a
6478 and oi s a b =
6479 if always || a != b
6480 then Printf.bprintf bb "\n %s='%d'" s a
6481 and oI s a b =
6482 if always || a != b
6483 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6484 and oz s a b =
6485 if always || a <> b
6486 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6487 and oF s a b =
6488 if always || a <> b
6489 then Printf.bprintf bb "\n %s='%f'" s a
6490 and oc s a b =
6491 if always || a <> b
6492 then
6493 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6494 and oC s a b =
6495 if always || a <> b
6496 then
6497 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6498 and oR s a b =
6499 if always || a <> b
6500 then
6501 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6502 and os s a b =
6503 if always || a <> b
6504 then
6505 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6506 and og s a b =
6507 if always || a <> b
6508 then
6509 match a with
6510 | None -> ()
6511 | Some (_N, _A, _B) ->
6512 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6513 and oW s a b =
6514 if always || a <> b
6515 then
6516 let v =
6517 match a with
6518 | None -> "false"
6519 | Some f ->
6520 if f = infinity
6521 then "true"
6522 else string_of_float f
6524 Printf.bprintf bb "\n %s='%s'" s v
6525 and oco s a b =
6526 if always || a <> b
6527 then
6528 match a with
6529 | Cmulti ((n, a, b), _) when n > 1 ->
6530 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6531 | Csplit (n, _) when n > 1 ->
6532 Printf.bprintf bb "\n %s='%d'" s ~-n
6533 | _ -> ()
6534 and obeco s a b =
6535 if always || a <> b
6536 then
6537 match a with
6538 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6539 | _ -> ()
6541 let w, h =
6542 if always
6543 then dc.winw, dc.winh
6544 else
6545 match state.fullscreen with
6546 | Some wh -> wh
6547 | None -> c.winw, c.winh
6549 oi "width" w dc.winw;
6550 oi "height" h dc.winh;
6551 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6552 oi "scroll-handle-height" c.scrollh dc.scrollh;
6553 ob "case-insensitive-search" c.icase dc.icase;
6554 ob "preload" c.preload dc.preload;
6555 oi "page-bias" c.pagebias dc.pagebias;
6556 oi "scroll-step" c.scrollstep dc.scrollstep;
6557 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6558 ob "max-height-fit" c.maxhfit dc.maxhfit;
6559 ob "crop-hack" c.crophack dc.crophack;
6560 oW "throttle" c.maxwait dc.maxwait;
6561 ob "highlight-links" c.hlinks dc.hlinks;
6562 ob "under-cursor-info" c.underinfo dc.underinfo;
6563 oi "vertical-margin" c.interpagespace dc.interpagespace;
6564 oz "zoom" c.zoom dc.zoom;
6565 ob "presentation" c.presentation dc.presentation;
6566 oi "rotation-angle" c.angle dc.angle;
6567 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6568 ob "proportional-display" c.proportional dc.proportional;
6569 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6570 oi "tex-count" c.texcount dc.texcount;
6571 oi "slice-height" c.sliceheight dc.sliceheight;
6572 oi "thumbnail-width" c.thumbw dc.thumbw;
6573 ob "persistent-location" c.jumpback dc.jumpback;
6574 oc "background-color" c.bgcolor dc.bgcolor;
6575 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6576 oi "tile-width" c.tilew dc.tilew;
6577 oi "tile-height" c.tileh dc.tileh;
6578 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6579 ob "checkers" c.checkers dc.checkers;
6580 oi "aalevel" c.aalevel dc.aalevel;
6581 ob "trim-margins" c.trimmargins dc.trimmargins;
6582 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6583 os "uri-launcher" c.urilauncher dc.urilauncher;
6584 os "path-launcher" c.pathlauncher dc.pathlauncher;
6585 oC "color-space" c.colorspace dc.colorspace;
6586 ob "invert-colors" c.invert dc.invert;
6587 oF "brightness" c.colorscale dc.colorscale;
6588 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6589 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6590 oco "columns" c.columns dc.columns;
6591 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6592 os "selection-command" c.selcmd dc.selcmd;
6593 ob "update-cursor" c.updatecurs dc.updatecurs;
6594 oi "hint-font-size" c.hfsize dc.hfsize;
6595 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6596 oF "page-scroll-scale" c.pgscale dc.pgscale;
6597 ob "multi-column-centering" c.multicenter dc.multicenter;
6600 let keymapsbuf always dc c =
6601 let bb = Buffer.create 16 in
6602 let rec loop = function
6603 | [] -> ()
6604 | (modename, h) :: rest ->
6605 let dh = findkeyhash dc modename in
6606 if always || h <> dh
6607 then (
6608 if Hashtbl.length h > 0
6609 then (
6610 if Buffer.length bb > 0
6611 then Buffer.add_char bb '\n';
6612 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6613 Hashtbl.iter (fun i o ->
6614 let isdifferent = always ||
6616 let dO = Hashtbl.find dh i in
6617 dO <> o
6618 with Not_found -> true
6620 if isdifferent
6621 then
6622 let addkm (k, m) =
6623 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6624 if Wsi.withalt m then Buffer.add_string bb "alt-";
6625 if Wsi.withshift m then Buffer.add_string bb "shift-";
6626 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6627 Buffer.add_string bb (Wsi.keyname k);
6629 let addkms l =
6630 let rec loop = function
6631 | [] -> ()
6632 | km :: [] -> addkm km
6633 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6635 loop l
6637 Buffer.add_string bb "<map in='";
6638 addkm i;
6639 match o with
6640 | KMinsrt km ->
6641 Buffer.add_string bb "' out='";
6642 addkm km;
6643 Buffer.add_string bb "'/>\n"
6645 | KMinsrl kms ->
6646 Buffer.add_string bb "' out='";
6647 addkms kms;
6648 Buffer.add_string bb "'/>\n"
6650 | KMmulti (ins, kms) ->
6651 Buffer.add_char bb ' ';
6652 addkms ins;
6653 Buffer.add_string bb "' out='";
6654 addkms kms;
6655 Buffer.add_string bb "'/>\n"
6656 ) h;
6657 Buffer.add_string bb "</keymap>";
6660 loop rest
6662 loop c.keyhashes;
6666 let save () =
6667 let uifontsize = fstate.fontsize in
6668 let bb = Buffer.create 32768 in
6669 let f (h, dc) =
6670 let dc = if conf.bedefault then conf else dc in
6671 Buffer.add_string bb "<llppconfig>\n";
6673 if String.length !fontpath > 0
6674 then
6675 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6676 uifontsize
6677 !fontpath
6678 else (
6679 if uifontsize <> 14
6680 then
6681 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6684 Buffer.add_string bb "<defaults ";
6685 add_attrs bb true dc dc;
6686 let kb = keymapsbuf true dc dc in
6687 if Buffer.length kb > 0
6688 then (
6689 Buffer.add_string bb ">\n";
6690 Buffer.add_buffer bb kb;
6691 Buffer.add_string bb "\n</defaults>\n";
6693 else Buffer.add_string bb "/>\n";
6695 let adddoc path pan anchor c bookmarks =
6696 if bookmarks == [] && c = dc && anchor = emptyanchor
6697 then ()
6698 else (
6699 Printf.bprintf bb "<doc path='%s'"
6700 (enent path 0 (String.length path));
6702 if anchor <> emptyanchor
6703 then (
6704 let n, rely, visy = anchor in
6705 Printf.bprintf bb " page='%d'" n;
6706 if rely > 1e-6
6707 then
6708 Printf.bprintf bb " rely='%f'" rely
6710 if abs_float visy > 1e-6
6711 then
6712 Printf.bprintf bb " visy='%f'" visy
6716 if pan != 0
6717 then Printf.bprintf bb " pan='%d'" pan;
6719 add_attrs bb false dc c;
6720 let kb = keymapsbuf false dc c in
6722 begin match bookmarks with
6723 | [] ->
6724 if Buffer.length kb > 0
6725 then (
6726 Buffer.add_string bb ">\n";
6727 Buffer.add_buffer bb kb;
6728 Buffer.add_string bb "\n</doc>\n";
6730 else Buffer.add_string bb "/>\n"
6731 | _ ->
6732 Buffer.add_string bb ">\n<bookmarks>\n";
6733 List.iter (fun (title, _level, (page, rely, visy)) ->
6734 Printf.bprintf bb
6735 "<item title='%s' page='%d'"
6736 (enent title 0 (String.length title))
6737 page
6739 if rely > 1e-6
6740 then
6741 Printf.bprintf bb " rely='%f'" rely
6743 if abs_float visy > 1e-6
6744 then
6745 Printf.bprintf bb " visy='%f'" visy
6747 Buffer.add_string bb "/>\n";
6748 ) bookmarks;
6749 Buffer.add_string bb "</bookmarks>";
6750 if Buffer.length kb > 0
6751 then (
6752 Buffer.add_string bb "\n";
6753 Buffer.add_buffer bb kb;
6755 Buffer.add_string bb "\n</doc>\n";
6756 end;
6760 let pan, conf =
6761 match state.mode with
6762 | Birdseye (c, pan, _, _, _) ->
6763 let beyecolumns =
6764 match conf.columns with
6765 | Cmulti ((c, _, _), _) -> Some c
6766 | Csingle _ -> None
6767 | Csplit _ -> None
6768 and columns =
6769 match c.columns with
6770 | Cmulti (c, _) -> Cmulti (c, [||])
6771 | Csingle _ -> Csingle [||]
6772 | Csplit _ -> failwith "quit from bird's eye while split"
6774 pan, { c with beyecolumns = beyecolumns; columns = columns }
6775 | _ -> state.x, conf
6777 let basename = Filename.basename state.path in
6778 adddoc basename pan (getanchor ())
6779 (let conf =
6780 let autoscrollstep =
6781 match state.autoscroll with
6782 | Some step -> step
6783 | None -> conf.autoscrollstep
6785 match state.mode with
6786 | Birdseye (bc, _, _, _, _) ->
6787 { conf with
6788 zoom = bc.zoom;
6789 presentation = bc.presentation;
6790 interpagespace = bc.interpagespace;
6791 maxwait = bc.maxwait;
6792 autoscrollstep = autoscrollstep }
6793 | _ -> { conf with autoscrollstep = autoscrollstep }
6794 in conf)
6795 (if conf.savebmarks then state.bookmarks else []);
6797 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6798 if basename <> path
6799 then adddoc path x anchor c bookmarks
6800 ) h;
6801 Buffer.add_string bb "</llppconfig>\n";
6802 true;
6804 if load1 f && Buffer.length bb > 0
6805 then
6807 let tmp = !confpath ^ ".tmp" in
6808 let oc = open_out_bin tmp in
6809 Buffer.output_buffer oc bb;
6810 close_out oc;
6811 Unix.rename tmp !confpath;
6812 with exn ->
6813 prerr_endline
6814 ("error while saving configuration: " ^ Printexc.to_string exn)
6816 end;;
6818 let () =
6819 let trimcachepath = ref "" in
6820 Arg.parse
6821 (Arg.align
6822 [("-p", Arg.String (fun s -> state.password <- s) ,
6823 "<password> Set password");
6825 ("-f", Arg.String (fun s -> Config.fontpath := s),
6826 "<path> Set path to the user interface font");
6828 ("-c", Arg.String (fun s -> Config.confpath := s),
6829 "<path> Set path to the configuration file");
6831 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6832 "<path> Set path to the trim cache file");
6834 ("-v", Arg.Unit (fun () ->
6835 Printf.printf
6836 "%s\nconfiguration path: %s\n"
6837 (version ())
6838 Config.defconfpath
6840 exit 0), " Print version and exit");
6843 (fun s -> state.path <- s)
6844 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6846 if String.length state.path = 0
6847 then (prerr_endline "file name missing"; exit 1);
6849 if not (Config.load ())
6850 then prerr_endline "failed to load configuration";
6852 let globalkeyhash = findkeyhash conf "global" in
6853 let wsfd, winw, winh = Wsi.init (object
6854 method expose =
6855 if nogeomcmds state.geomcmds || platform == Posx
6856 then display ()
6857 else (
6858 GlClear.color (scalecolor2 conf.bgcolor);
6859 GlClear.clear [`color];
6861 method display = display ()
6862 method reshape w h = reshape w h
6863 method mouse b d x y m = mouse b d x y m
6864 method motion x y = state.mpos <- (x, y); motion x y
6865 method pmotion x y = state.mpos <- (x, y); pmotion x y
6866 method key k m =
6867 let mascm = m land (
6868 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6869 ) in
6870 match state.keystate with
6871 | KSnone ->
6872 let km = k, mascm in
6873 begin
6874 match
6875 let modehash = state.uioh#modehash in
6876 try Hashtbl.find modehash km
6877 with Not_found ->
6878 try Hashtbl.find globalkeyhash km
6879 with Not_found -> KMinsrt (k, m)
6880 with
6881 | KMinsrt (k, m) -> keyboard k m
6882 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6883 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6885 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6886 List.iter (fun (k, m) -> keyboard k m) insrt;
6887 state.keystate <- KSnone
6888 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6889 state.keystate <- KSinto (keys, insrt)
6890 | _ ->
6891 state.keystate <- KSnone
6893 method enter x y = state.mpos <- (x, y); pmotion x y
6894 method leave = state.mpos <- (-1, -1)
6895 method quit = raise Quit
6896 end) conf.winw conf.winh (platform = Posx) in
6898 state.wsfd <- wsfd;
6900 if not (
6901 List.exists GlMisc.check_extension
6902 [ "GL_ARB_texture_rectangle"
6903 ; "GL_EXT_texture_recangle"
6904 ; "GL_NV_texture_rectangle" ]
6906 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6908 let cr, sw =
6909 match Ne.pipe () with
6910 | Ne.Exn exn ->
6911 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6912 exit 1
6913 | Ne.Res rw -> rw
6914 and sr, cw =
6915 match Ne.pipe () with
6916 | Ne.Exn exn ->
6917 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6918 exit 1
6919 | Ne.Res rw -> rw
6922 cloexec cr;
6923 cloexec sw;
6924 cloexec sr;
6925 cloexec cw;
6927 setcheckers conf.checkers;
6928 redirectstderr ();
6930 init (cr, cw) (
6931 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6932 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6933 !Config.fontpath, !trimcachepath
6935 state.sr <- sr;
6936 state.sw <- sw;
6937 state.text <- "Opening " ^ state.path;
6938 reshape winw winh;
6939 opendoc state.path state.password;
6940 state.uioh <- uioh;
6942 let rec loop deadline =
6943 let r =
6944 match state.errfd with
6945 | None -> [state.sr; state.wsfd]
6946 | Some fd -> [state.sr; state.wsfd; fd]
6948 if state.redisplay
6949 then (
6950 state.redisplay <- false;
6951 display ();
6953 let timeout =
6954 let now = now () in
6955 if deadline > now
6956 then (
6957 if deadline = infinity
6958 then ~-.1.0
6959 else max 0.0 (deadline -. now)
6961 else 0.0
6963 let r, _, _ =
6964 try Unix.select r [] [] timeout
6965 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6967 begin match r with
6968 | [] ->
6969 state.ghyll None;
6970 let newdeadline =
6971 if state.ghyll == noghyll
6972 then
6973 match state.autoscroll with
6974 | Some step when step != 0 ->
6975 let y = state.y + step in
6976 let y =
6977 if y < 0
6978 then state.maxy
6979 else if y >= state.maxy then 0 else y
6981 gotoy y;
6982 if state.mode = View
6983 then state.text <- "";
6984 deadline +. 0.01
6985 | _ -> infinity
6986 else deadline +. 0.01
6988 loop newdeadline
6990 | l ->
6991 let rec checkfds = function
6992 | [] -> ()
6993 | fd :: rest when fd = state.sr ->
6994 let cmd = readcmd state.sr in
6995 act cmd;
6996 checkfds rest
6998 | fd :: rest when fd = state.wsfd ->
6999 Wsi.readresp fd;
7000 checkfds rest
7002 | fd :: rest ->
7003 let s = String.create 80 in
7004 let n = Unix.read fd s 0 80 in
7005 if conf.redirectstderr
7006 then (
7007 Buffer.add_substring state.errmsgs s 0 n;
7008 state.newerrmsgs <- true;
7009 state.redisplay <- true;
7011 else (
7012 prerr_string (String.sub s 0 n);
7013 flush stderr;
7015 checkfds rest
7017 checkfds l;
7018 let newdeadline =
7019 let deadline1 =
7020 if deadline = infinity
7021 then now () +. 0.01
7022 else deadline
7024 match state.autoscroll with
7025 | Some step when step != 0 -> deadline1
7026 | _ -> if state.ghyll == noghyll then infinity else deadline1
7028 loop newdeadline
7029 end;
7032 loop infinity;
7033 with Quit ->
7034 Config.save ();