Fix another issue with page_of_y and use it to bootstrap layouting
[llpp.git] / main.ml
blob521de4364395bf40aa84c85f5371d043695a0f79
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, _, _), b) ->
1031 let rec loop y h n =
1032 if n < 0
1033 then loop y h (n+1)
1034 else (
1035 if n = Array.length b
1036 then y + h
1037 else
1038 let (_, _, y', (_, _, h', _)) = b.(n) in
1039 let y = min y y'
1040 and h = max h h' in
1041 loop y h (n+1)
1044 loop max_int 0 (((Array.length b - 1) / c) * c)
1045 | Csingle b ->
1046 if Array.length b > 0
1047 then
1048 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1049 y + h + (if conf.presentation then calcips h else 0)
1050 else 0
1051 | Csplit (_, b) ->
1052 if Array.length b > 0
1053 then
1054 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1055 y + h
1056 else 0
1059 let getpageyh pageno =
1060 let pageno = bound pageno 0 (state.pagecount-1) in
1061 match conf.columns with
1062 | Csingle b ->
1063 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1064 let y =
1065 if conf.presentation
1066 then y - calcips h
1067 else y
1069 y, h
1070 | Cmulti (_, b) ->
1071 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1072 y, h
1073 | Csplit (c, b) ->
1074 let n = pageno*c in
1075 let (_, _, y, (_, _, h, _)) = b.(n) in
1076 y, h
1079 let getpagedim pageno =
1080 let rec f ppdim l =
1081 match l with
1082 | (n, _, _, _) as pdim :: rest ->
1083 if n >= pageno
1084 then (if n = pageno then pdim else ppdim)
1085 else f pdim rest
1087 | [] -> ppdim
1089 f (-1, -1, -1, -1) state.pdims
1092 let getpagey pageno = fst (getpageyh pageno);;
1094 let nogeomcmds cmds =
1095 match cmds with
1096 | s, [] -> String.length s = 0
1097 | _ -> false
1100 let page_of_y y =
1101 let (c, coverA, coverB), b =
1102 match conf.columns with
1103 | Csingle b -> (1, 0, 0), b
1104 | Cmulti (c, b) -> c, b
1105 | Csplit (_, b) -> (1, 0, 0), b
1107 let rec bsearch nmin nmax =
1108 if nmin > nmax
1109 then -1
1110 else
1111 let n = (nmax + nmin) / 2 in
1112 let _, _, vy, (_, _, h, _) = b.(n) in
1113 let y0, y1 =
1114 if conf.presentation
1115 then
1116 let ips = calcips h in
1117 let y0 = vy - ips in
1118 let y1 = vy + h + ips in
1119 y0, y1
1120 else (
1121 if n = 0
1122 then 0, vy + h + conf.interpagespace
1123 else
1124 let y0 = vy - conf.interpagespace in
1125 y0, y0 + h + conf.interpagespace
1128 if y >= y0 && y < y1
1129 then (
1130 if c = 1
1131 then n
1132 else (
1133 if n > coverA
1134 then
1135 if n < state.pagecount - coverB
1136 then ((n-coverA)/c)*c + coverA
1137 else n
1138 else n
1141 else (
1142 if y > y0
1143 then bsearch (n+1) nmax
1144 else bsearch nmin (n-1)
1147 let r = bsearch 0 (state.pagecount-1) in
1151 let layoutN ((columns, coverA, coverB), b) y sh =
1152 let sh = sh - state.hscrollh in
1153 let rec fold accu n =
1154 if n = Array.length b
1155 then accu
1156 else
1157 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1158 if (vy - y) > sh &&
1159 (n = coverA - 1
1160 || n = state.pagecount - coverB
1161 || (n - coverA) mod columns = columns - 1)
1162 then accu
1163 else
1164 let accu =
1165 if vy + h > y
1166 then
1167 let pagey = max 0 (y - vy) in
1168 let pagedispy = if pagey > 0 then 0 else vy - y in
1169 let pagedispx, pagex =
1170 let pdx =
1171 if n = coverA - 1 || n = state.pagecount - coverB
1172 then state.x + (conf.winw - state.scrollw - w) / 2
1173 else dx + xoff + state.x
1175 if pdx < 0
1176 then 0, -pdx
1177 else pdx, 0
1179 let pagevw =
1180 let vw = conf.winw - state.scrollw - pagedispx in
1181 let pw = w - pagex in
1182 min vw pw
1184 let pagevh = min (h - pagey) (sh - pagedispy) in
1185 if pagevw > 0 && pagevh > 0
1186 then
1187 let e =
1188 { pageno = n
1189 ; pagedimno = pdimno
1190 ; pagew = w
1191 ; pageh = h
1192 ; pagex = pagex
1193 ; pagey = pagey
1194 ; pagevw = pagevw
1195 ; pagevh = pagevh
1196 ; pagedispx = pagedispx
1197 ; pagedispy = pagedispy
1198 ; pagecol = 0
1201 e :: accu
1202 else
1203 accu
1204 else
1205 accu
1207 fold accu (n+1)
1209 List.rev (fold [] (page_of_y y));
1212 let layoutS (columns, b) y sh =
1213 let sh = sh - state.hscrollh in
1214 let rec fold accu n =
1215 if n = Array.length b
1216 then accu
1217 else
1218 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1219 if (vy - y) > sh
1220 then accu
1221 else
1222 let accu =
1223 if vy + pageh > y
1224 then
1225 let x = xoff + state.x in
1226 let pagey = max 0 (y - vy) in
1227 let pagedispy = if pagey > 0 then 0 else vy - y in
1228 let pagedispx, pagex =
1229 if px = 0
1230 then (
1231 if x < 0
1232 then 0, -x
1233 else x, 0
1235 else (
1236 let px = px - x in
1237 if px < 0
1238 then -px, 0
1239 else 0, px
1242 let pagecolw = pagew/columns in
1243 let pagedispx =
1244 if pagecolw < conf.winw
1245 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1246 else pagedispx
1248 let pagevw =
1249 let vw = conf.winw - pagedispx - state.scrollw in
1250 let pw = pagew - pagex in
1251 min vw pw
1253 let pagevw = min pagevw pagecolw in
1254 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1255 if pagevw > 0 && pagevh > 0
1256 then
1257 let e =
1258 { pageno = n/columns
1259 ; pagedimno = pdimno
1260 ; pagew = pagew
1261 ; pageh = pageh
1262 ; pagex = pagex
1263 ; pagey = pagey
1264 ; pagevw = pagevw
1265 ; pagevh = pagevh
1266 ; pagedispx = pagedispx
1267 ; pagedispy = pagedispy
1268 ; pagecol = n mod columns
1271 e :: accu
1272 else
1273 accu
1274 else
1275 accu
1277 fold accu (n+1)
1279 List.rev (fold [] 0)
1282 let layout y sh =
1283 if nogeomcmds state.geomcmds
1284 then
1285 match conf.columns with
1286 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1287 | Cmulti c -> layoutN c y sh
1288 | Csplit s -> layoutS s y sh
1289 else []
1292 let clamp incr =
1293 let y = state.y + incr in
1294 let y = max 0 y in
1295 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1299 let itertiles l f =
1300 let tilex = l.pagex mod conf.tilew in
1301 let tiley = l.pagey mod conf.tileh in
1303 let col = l.pagex / conf.tilew in
1304 let row = l.pagey / conf.tileh in
1306 let rec rowloop row y0 dispy h =
1307 if h = 0
1308 then ()
1309 else (
1310 let dh = conf.tileh - y0 in
1311 let dh = min h dh in
1312 let rec colloop col x0 dispx w =
1313 if w = 0
1314 then ()
1315 else (
1316 let dw = conf.tilew - x0 in
1317 let dw = min w dw in
1319 f col row dispx dispy x0 y0 dw dh;
1320 colloop (col+1) 0 (dispx+dw) (w-dw)
1323 colloop col tilex l.pagedispx l.pagevw;
1324 rowloop (row+1) 0 (dispy+dh) (h-dh)
1327 if l.pagevw > 0 && l.pagevh > 0
1328 then rowloop row tiley l.pagedispy l.pagevh;
1331 let gettileopaque l col row =
1332 let key =
1333 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1335 try Some (Hashtbl.find state.tilemap key)
1336 with Not_found -> None
1339 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1340 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1341 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1344 let drawtiles l color =
1345 GlDraw.color color;
1346 let f col row x y tilex tiley w h =
1347 match gettileopaque l col row with
1348 | Some (opaque, _, t) ->
1349 let params = x, y, w, h, tilex, tiley in
1350 if conf.invert
1351 then (
1352 Gl.enable `blend;
1353 GlFunc.blend_func `zero `one_minus_src_color;
1355 drawtile params opaque;
1356 if conf.invert
1357 then Gl.disable `blend;
1358 if conf.debug
1359 then (
1360 let s = Printf.sprintf
1361 "%d[%d,%d] %f sec"
1362 l.pageno col row t
1364 let w = measurestr fstate.fontsize s in
1365 GlMisc.push_attrib [`current];
1366 GlDraw.color (0.0, 0.0, 0.0);
1367 GlDraw.rect
1368 (float (x-2), float (y-2))
1369 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1370 GlDraw.color (1.0, 1.0, 1.0);
1371 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1372 GlMisc.pop_attrib ();
1375 | _ ->
1376 let w =
1377 let lw = conf.winw - state.scrollw - x in
1378 min lw w
1379 and h =
1380 let lh = conf.winh - y in
1381 min lh h
1383 begin match state.texid with
1384 | Some id ->
1385 Gl.enable `texture_2d;
1386 GlTex.bind_texture `texture_2d id;
1387 let x0 = float x
1388 and y0 = float y
1389 and x1 = float (x+w)
1390 and y1 = float (y+h) in
1392 let tw = float w /. 64.0
1393 and th = float h /. 64.0 in
1394 let tx0 = float tilex /. 64.0
1395 and ty0 = float tiley /. 64.0 in
1396 let tx1 = tx0 +. tw
1397 and ty1 = ty0 +. th in
1398 GlDraw.begins `quads;
1399 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1400 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1401 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1402 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1403 GlDraw.ends ();
1405 Gl.disable `texture_2d;
1406 | None ->
1407 GlDraw.color (1.0, 1.0, 1.0);
1408 GlDraw.rect
1409 (float x, float y)
1410 (float (x+w), float (y+h));
1411 end;
1412 if w > 128 && h > fstate.fontsize + 10
1413 then (
1414 GlDraw.color (0.0, 0.0, 0.0);
1415 let c, r =
1416 if conf.verbose
1417 then (col*conf.tilew, row*conf.tileh)
1418 else col, row
1420 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1422 GlDraw.color color;
1424 itertiles l f
1427 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1429 let tilevisible1 l x y =
1430 let ax0 = l.pagex
1431 and ax1 = l.pagex + l.pagevw
1432 and ay0 = l.pagey
1433 and ay1 = l.pagey + l.pagevh in
1435 let bx0 = x
1436 and by0 = y in
1437 let bx1 = min (bx0 + conf.tilew) l.pagew
1438 and by1 = min (by0 + conf.tileh) l.pageh in
1440 let rx0 = max ax0 bx0
1441 and ry0 = max ay0 by0
1442 and rx1 = min ax1 bx1
1443 and ry1 = min ay1 by1 in
1445 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1446 nonemptyintersection
1449 let tilevisible layout n x y =
1450 let rec findpageinlayout m = function
1451 | l :: rest when l.pageno = n ->
1452 tilevisible1 l x y || (
1453 match conf.columns with
1454 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1455 | _ -> false
1457 | _ :: rest -> findpageinlayout 0 rest
1458 | [] -> false
1460 findpageinlayout 0 layout;
1463 let tileready l x y =
1464 tilevisible1 l x y &&
1465 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1468 let tilepage n p layout =
1469 let rec loop = function
1470 | l :: rest ->
1471 if l.pageno = n
1472 then
1473 let f col row _ _ _ _ _ _ =
1474 if state.currently = Idle
1475 then
1476 match gettileopaque l col row with
1477 | Some _ -> ()
1478 | None ->
1479 let x = col*conf.tilew
1480 and y = row*conf.tileh in
1481 let w =
1482 let w = l.pagew - x in
1483 min w conf.tilew
1485 let h =
1486 let h = l.pageh - y in
1487 min h conf.tileh
1489 wcmd "tile %s %d %d %d %d" p x y w h;
1490 state.currently <-
1491 Tiling (
1492 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1493 conf.tilew, conf.tileh
1496 itertiles l f;
1497 else
1498 loop rest
1500 | [] -> ()
1502 if nogeomcmds state.geomcmds
1503 then loop layout;
1506 let preloadlayout y =
1507 let y = if y < conf.winh then 0 else y - conf.winh in
1508 let h = conf.winh*3 in
1509 layout y h;
1512 let load pages =
1513 let rec loop pages =
1514 if state.currently != Idle
1515 then ()
1516 else
1517 match pages with
1518 | l :: rest ->
1519 begin match getopaque l.pageno with
1520 | None ->
1521 wcmd "page %d %d" l.pageno l.pagedimno;
1522 state.currently <- Loading (l, state.gen);
1523 | Some opaque ->
1524 tilepage l.pageno opaque pages;
1525 loop rest
1526 end;
1527 | _ -> ()
1529 if nogeomcmds state.geomcmds
1530 then loop pages
1533 let preload pages =
1534 load pages;
1535 if conf.preload && state.currently = Idle
1536 then load (preloadlayout state.y);
1539 let layoutready layout =
1540 let rec fold all ls =
1541 all && match ls with
1542 | l :: rest ->
1543 let seen = ref false in
1544 let allvisible = ref true in
1545 let foo col row _ _ _ _ _ _ =
1546 seen := true;
1547 allvisible := !allvisible &&
1548 begin match gettileopaque l col row with
1549 | Some _ -> true
1550 | None -> false
1553 itertiles l foo;
1554 fold (!seen && !allvisible) rest
1555 | [] -> true
1557 let alltilesvisible = fold true layout in
1558 alltilesvisible;
1561 let gotoy y =
1562 let y = bound y 0 state.maxy in
1563 let y, layout, proceed =
1564 match conf.maxwait with
1565 | Some time when state.ghyll == noghyll ->
1566 begin match state.throttle with
1567 | None ->
1568 let layout = layout y conf.winh in
1569 let ready = layoutready layout in
1570 if not ready
1571 then (
1572 load layout;
1573 state.throttle <- Some (layout, y, now ());
1575 else G.postRedisplay "gotoy showall (None)";
1576 y, layout, ready
1577 | Some (_, _, started) ->
1578 let dt = now () -. started in
1579 if dt > time
1580 then (
1581 state.throttle <- None;
1582 let layout = layout y conf.winh in
1583 load layout;
1584 G.postRedisplay "maxwait";
1585 y, layout, true
1587 else -1, [], false
1590 | _ ->
1591 let layout = layout y conf.winh in
1592 if true || layoutready layout
1593 then G.postRedisplay "gotoy ready";
1594 y, layout, true
1596 if proceed
1597 then (
1598 state.y <- y;
1599 state.layout <- layout;
1600 begin match state.mode with
1601 | LinkNav (Ltexact (pageno, linkno)) ->
1602 let rec loop = function
1603 | [] ->
1604 state.mode <- LinkNav (Ltgendir 0)
1605 | l :: _ when l.pageno = pageno ->
1606 begin match getopaque pageno with
1607 | None ->
1608 state.mode <- LinkNav (Ltgendir 0)
1609 | Some opaque ->
1610 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1611 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1612 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1613 then state.mode <- LinkNav (Ltgendir 0)
1615 | _ :: rest -> loop rest
1617 loop layout
1618 | _ -> ()
1619 end;
1620 begin match state.mode with
1621 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1622 if not (pagevisible layout pageno)
1623 then (
1624 match state.layout with
1625 | [] -> ()
1626 | l :: _ ->
1627 state.mode <- Birdseye (
1628 conf, leftx, l.pageno, hooverpageno, anchor
1631 | LinkNav (Ltgendir dir as lt) ->
1632 let linknav =
1633 let rec loop = function
1634 | [] -> lt
1635 | l :: rest ->
1636 match getopaque l.pageno with
1637 | None -> loop rest
1638 | Some opaque ->
1639 let link =
1640 let ld =
1641 if dir = 0
1642 then LDfirstvisible (l.pagex, l.pagey, dir)
1643 else (
1644 if dir > 0 then LDfirst else LDlast
1647 findlink opaque ld
1649 match link with
1650 | Lnotfound -> loop rest
1651 | Lfound n ->
1652 showlinktype (getlink opaque n);
1653 Ltexact (l.pageno, n)
1655 loop state.layout
1657 state.mode <- LinkNav linknav
1658 | _ -> ()
1659 end;
1660 preload layout;
1662 state.ghyll <- noghyll;
1663 if conf.updatecurs
1664 then (
1665 let mx, my = state.mpos in
1666 updateunder mx my;
1670 let conttiling pageno opaque =
1671 tilepage pageno opaque
1672 (if conf.preload then preloadlayout state.y else state.layout)
1675 let gotoy_and_clear_text y =
1676 if not conf.verbose then state.text <- "";
1677 gotoy y;
1680 let getanchor1 l =
1681 let top =
1682 let coloff = l.pagecol * l.pageh in
1683 float (l.pagey + coloff) /. float l.pageh
1685 let dtop =
1686 if l.pagedispy = 0
1687 then
1689 else
1690 if conf.presentation
1691 then float l.pagedispy /. float (calcips l.pageh)
1692 else float l.pagedispy /. float conf.interpagespace
1694 (l.pageno, top, dtop)
1697 let getanchor () =
1698 match state.layout with
1699 | l :: _ -> getanchor1 l
1700 | [] ->
1701 let n = page_of_y state.y in
1702 let y, h = getpageyh n in
1703 let dy = y - state.y in
1704 let dtop =
1705 if conf.presentation
1706 then
1707 let ips = calcips h in
1708 float (dy + ips) /. float ips
1709 else
1710 float dy /. float conf.interpagespace
1712 (n, 0.0, dtop)
1715 let getanchory (n, top, dtop) =
1716 let y, h = getpageyh n in
1717 if conf.presentation
1718 then
1719 let ips = calcips h in
1720 y + truncate (top*.float h -. dtop*.float ips) + ips;
1721 else
1722 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1725 let gotoanchor anchor =
1726 gotoy (getanchory anchor);
1729 let addnav () =
1730 cbput state.hists.nav (getanchor ());
1733 let getnav dir =
1734 let anchor = cbgetc state.hists.nav dir in
1735 getanchory anchor;
1738 let gotoghyll y =
1739 let scroll f n a b =
1740 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1741 let snake f a b =
1742 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1743 if f < a
1744 then s (float f /. float a)
1745 else (
1746 if f > b
1747 then 1.0 -. s ((float (f-b) /. float (n-b)))
1748 else 1.0
1751 snake f a b
1752 and summa f n a b =
1753 (* courtesy:
1754 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1755 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1756 let iv1 = iv f in
1757 let ins = float a *. iv1
1758 and outs = float (n-b) *. iv1 in
1759 let ones = b - a in
1760 ins +. outs +. float ones
1762 let rec set (_N, _A, _B) y sy =
1763 let sum = summa 1.0 _N _A _B in
1764 let dy = float (y - sy) in
1765 state.ghyll <- (
1766 let rec gf n y1 o =
1767 if n >= _N
1768 then state.ghyll <- noghyll
1769 else
1770 let go n =
1771 let s = scroll n _N _A _B in
1772 let y1 = y1 +. ((s *. dy) /. sum) in
1773 gotoy_and_clear_text (truncate y1);
1774 state.ghyll <- gf (n+1) y1;
1776 match o with
1777 | None -> go n
1778 | Some y' -> set (_N/2, 0, 0) y' state.y
1780 gf 0 (float state.y)
1783 match conf.ghyllscroll with
1784 | None ->
1785 gotoy_and_clear_text y
1786 | Some nab ->
1787 if state.ghyll == noghyll
1788 then set nab y state.y
1789 else state.ghyll (Some y)
1792 let gotopage n top =
1793 let y, h = getpageyh n in
1794 let y = y + (truncate (top *. float h)) in
1795 gotoghyll y
1798 let gotopage1 n top =
1799 let y = getpagey n in
1800 let y = y + top in
1801 gotoghyll y
1804 let invalidate s f =
1805 state.layout <- [];
1806 state.pdims <- [];
1807 state.rects <- [];
1808 state.rects1 <- [];
1809 match state.geomcmds with
1810 | ps, [] when String.length ps = 0 ->
1811 f ();
1812 state.geomcmds <- s, [];
1814 | ps, [] ->
1815 state.geomcmds <- ps, [s, f];
1817 | ps, (s', _) :: rest when s' = s ->
1818 state.geomcmds <- ps, ((s, f) :: rest);
1820 | ps, cmds ->
1821 state.geomcmds <- ps, ((s, f) :: cmds);
1824 let opendoc path password =
1825 state.path <- path;
1826 state.password <- password;
1827 state.gen <- state.gen + 1;
1828 state.docinfo <- [];
1830 setaalevel conf.aalevel;
1831 Wsi.settitle ("llpp " ^ Filename.basename path);
1832 wcmd "open %s\000%s\000" path password;
1833 invalidate "reqlayout"
1834 (fun () ->
1835 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1838 let scalecolor c =
1839 let c = c *. conf.colorscale in
1840 (c, c, c);
1843 let scalecolor2 (r, g, b) =
1844 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1847 let docolumns = function
1848 | Csingle _ ->
1849 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1850 let rec loop pageno pdimno pdim y ph pdims =
1851 if pageno = state.pagecount
1852 then ()
1853 else
1854 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1855 match pdims with
1856 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1857 pdimno+1, pdim, rest
1858 | _ ->
1859 pdimno, pdim, pdims
1861 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1862 let y = y +
1863 (if conf.presentation
1864 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1865 else (if pageno = 0 then 0 else calcips h)
1868 a.(pageno) <- (pdimno, x, y, pdim);
1869 loop (pageno+1) pdimno pdim (y + h) h pdims
1871 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1872 conf.columns <- Csingle a;
1874 | Cmulti ((columns, coverA, coverB), _) ->
1875 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1876 let rec loop pageno pdimno pdim x y rowh pdims =
1877 let rec fixrow m = if m = pageno then () else
1878 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1879 if h < rowh
1880 then (
1881 let y = y + (rowh - h) / 2 in
1882 a.(m) <- (pdimno, x, y, pdim);
1884 fixrow (m+1)
1886 if pageno = state.pagecount
1887 then fixrow (((pageno - 1) / columns) * columns)
1888 else
1889 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1890 match pdims with
1891 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1892 pdimno+1, pdim, rest
1893 | _ ->
1894 pdimno, pdim, pdims
1896 let x, y, rowh' =
1897 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1898 then (
1899 (conf.winw - state.scrollw - w) / 2,
1900 y + rowh + conf.interpagespace, h
1902 else (
1903 if (pageno - coverA) mod columns = 0
1904 then (
1905 (if conf.multicenter then
1906 (conf.winw - state.scrollw - state.w) / 2 else 0),
1907 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1909 else x, y, max rowh h
1912 if pageno > 1 && (pageno - coverA) mod columns = 0
1913 then fixrow (pageno - columns);
1914 a.(pageno) <- (pdimno, x, y, pdim);
1915 let x = x + w + xoff*2 + conf.interpagespace in
1916 loop (pageno+1) pdimno pdim x y rowh' pdims
1918 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1919 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1921 | Csplit (c, _) ->
1922 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1923 let rec loop pageno pdimno pdim y pdims =
1924 if pageno = state.pagecount
1925 then ()
1926 else
1927 let pdimno, ((_, w, h, _) as pdim), pdims =
1928 match pdims with
1929 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1930 pdimno+1, pdim, rest
1931 | _ ->
1932 pdimno, pdim, pdims
1934 let cw = w / c in
1935 let rec loop1 n x y =
1936 if n = c then y else (
1937 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1938 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1941 let y = loop1 0 0 y in
1942 loop (pageno+1) pdimno pdim y pdims
1944 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1945 conf.columns <- Csplit (c, a);
1948 let represent () =
1949 docolumns conf.columns;
1950 state.maxy <- calcheight ();
1951 state.hscrollh <-
1952 if state.w <= conf.winw - state.scrollw
1953 then 0
1954 else state.scrollw
1956 match state.mode with
1957 | Birdseye (_, _, pageno, _, _) ->
1958 let y, h = getpageyh pageno in
1959 let top = (conf.winh - h) / 2 in
1960 gotoy (max 0 (y - top))
1961 | _ -> gotoanchor state.anchor
1964 let reshape w h =
1965 GlDraw.viewport 0 0 w h;
1966 let firsttime = state.geomcmds == firstgeomcmds in
1967 if not firsttime && nogeomcmds state.geomcmds
1968 then state.anchor <- getanchor ();
1970 conf.winw <- w;
1971 let w = truncate (float w *. conf.zoom) - state.scrollw in
1972 let w = max w 2 in
1973 conf.winh <- h;
1974 setfontsize fstate.fontsize;
1975 GlMat.mode `modelview;
1976 GlMat.load_identity ();
1978 GlMat.mode `projection;
1979 GlMat.load_identity ();
1980 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1981 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1982 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1984 let relx =
1985 if conf.zoom <= 1.0
1986 then 0.0
1987 else float state.x /. float state.w
1989 invalidate "geometry"
1990 (fun () ->
1991 state.w <- w;
1992 if not firsttime
1993 then state.x <- truncate (relx *. float w);
1994 let w =
1995 match conf.columns with
1996 | Csingle _ -> w
1997 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1998 | Csplit (c, _) -> w * c
2000 wcmd "geometry %d %d" w h);
2003 let enttext () =
2004 let len = String.length state.text in
2005 let drawstring s =
2006 let hscrollh =
2007 match state.mode with
2008 | Textentry _
2009 | View ->
2010 let h, _, _ = state.uioh#scrollpw in
2012 | _ -> 0
2014 let rect x w =
2015 GlDraw.rect
2016 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2017 (x+.w, float (conf.winh - hscrollh))
2020 let w = float (conf.winw - state.scrollw - 1) in
2021 if state.progress >= 0.0 && state.progress < 1.0
2022 then (
2023 GlDraw.color (0.3, 0.3, 0.3);
2024 let w1 = w *. state.progress in
2025 rect 0.0 w1;
2026 GlDraw.color (0.0, 0.0, 0.0);
2027 rect w1 (w-.w1)
2029 else (
2030 GlDraw.color (0.0, 0.0, 0.0);
2031 rect 0.0 w;
2034 GlDraw.color (1.0, 1.0, 1.0);
2035 drawstring fstate.fontsize
2036 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2038 let s =
2039 match state.mode with
2040 | Textentry ((prefix, text, _, _, _, _), _) ->
2041 let s =
2042 if len > 0
2043 then
2044 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2045 else
2046 Printf.sprintf "%s%s_" prefix text
2050 | _ -> state.text
2052 let s =
2053 if state.newerrmsgs
2054 then (
2055 if not (istextentry state.mode)
2056 then
2057 let s1 = "(press 'e' to review error messasges)" in
2058 if String.length s > 0 then s ^ " " ^ s1 else s1
2059 else s
2061 else s
2063 if String.length s > 0
2064 then drawstring s
2067 let gctiles () =
2068 let len = Queue.length state.tilelru in
2069 let layout = lazy (
2070 match state.throttle with
2071 | None ->
2072 if conf.preload
2073 then preloadlayout state.y
2074 else state.layout
2075 | Some (layout, _, _) ->
2076 layout
2077 ) in
2078 let rec loop qpos =
2079 if state.memused <= conf.memlimit
2080 then ()
2081 else (
2082 if qpos < len
2083 then
2084 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2085 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2086 let (_, pw, ph, _) = getpagedim n in
2088 gen = state.gen
2089 && colorspace = conf.colorspace
2090 && angle = conf.angle
2091 && pagew = pw
2092 && pageh = ph
2093 && (
2094 let x = col*conf.tilew
2095 and y = row*conf.tileh in
2096 tilevisible (Lazy.force_val layout) n x y
2098 then Queue.push lruitem state.tilelru
2099 else (
2100 wcmd "freetile %s" p;
2101 state.memused <- state.memused - s;
2102 state.uioh#infochanged Memused;
2103 Hashtbl.remove state.tilemap k;
2105 loop (qpos+1)
2108 loop 0
2111 let flushtiles () =
2112 Queue.iter (fun (k, p, s) ->
2113 wcmd "freetile %s" p;
2114 state.memused <- state.memused - s;
2115 state.uioh#infochanged Memused;
2116 Hashtbl.remove state.tilemap k;
2117 ) state.tilelru;
2118 Queue.clear state.tilelru;
2119 load state.layout;
2122 let logcurrently = function
2123 | Idle -> dolog "Idle"
2124 | Loading (l, gen) ->
2125 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2126 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2127 dolog
2128 "Tiling %d[%d,%d] page=%s cs=%s angle"
2129 l.pageno col row pageopaque
2130 (colorspace_to_string colorspace)
2132 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2133 angle gen conf.angle state.gen
2134 tilew tileh
2135 conf.tilew conf.tileh
2137 | Outlining _ ->
2138 dolog "outlining"
2141 let act cmds =
2142 (* dolog "%S" cmds; *)
2143 let op, args =
2144 let spacepos =
2145 try String.index cmds ' '
2146 with Not_found -> -1
2148 if spacepos = -1
2149 then cmds, ""
2150 else
2151 let l = String.length cmds in
2152 let op = String.sub cmds 0 spacepos in
2153 op, begin
2154 if l - spacepos < 2 then ""
2155 else String.sub cmds (spacepos+1) (l-spacepos-1)
2158 match op with
2159 | "clear" ->
2160 state.uioh#infochanged Pdim;
2161 state.pdims <- [];
2163 | "clearrects" ->
2164 state.rects <- state.rects1;
2165 G.postRedisplay "clearrects";
2167 | "continue" ->
2168 let n =
2169 try Scanf.sscanf args "%u" (fun n -> n)
2170 with exn ->
2171 dolog "error processing 'continue' %S: %s"
2172 cmds (Printexc.to_string exn);
2173 exit 1;
2175 state.pagecount <- n;
2176 begin match state.currently with
2177 | Outlining l ->
2178 state.currently <- Idle;
2179 state.outlines <- Array.of_list (List.rev l)
2180 | _ -> ()
2181 end;
2183 let cur, cmds = state.geomcmds in
2184 if String.length cur = 0
2185 then failwith "umpossible";
2187 begin match List.rev cmds with
2188 | [] ->
2189 state.geomcmds <- "", [];
2190 represent ();
2191 | (s, f) :: rest ->
2192 f ();
2193 state.geomcmds <- s, List.rev rest;
2194 end;
2195 if conf.maxwait = None
2196 then G.postRedisplay "continue";
2198 | "title" ->
2199 Wsi.settitle args
2201 | "msg" ->
2202 showtext ' ' args
2204 | "vmsg" ->
2205 if conf.verbose
2206 then showtext ' ' args
2208 | "progress" ->
2209 let progress, text =
2211 Scanf.sscanf args "%f %n"
2212 (fun f pos ->
2213 f, String.sub args pos (String.length args - pos))
2214 with exn ->
2215 dolog "error processing 'progress' %S: %s"
2216 cmds (Printexc.to_string exn);
2217 exit 1;
2219 state.text <- text;
2220 state.progress <- progress;
2221 G.postRedisplay "progress"
2223 | "firstmatch" ->
2224 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2226 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2227 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2228 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2229 with exn ->
2230 dolog "error processing 'firstmatch' %S: %s"
2231 cmds (Printexc.to_string exn);
2232 exit 1;
2234 let y = (getpagey pageno) + truncate y0 in
2235 addnav ();
2236 gotoy y;
2237 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2239 | "match" ->
2240 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2242 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2243 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2244 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2245 with exn ->
2246 dolog "error processing 'match' %S: %s"
2247 cmds (Printexc.to_string exn);
2248 exit 1;
2250 state.rects1 <-
2251 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2253 | "page" ->
2254 let pageopaque, t =
2256 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2257 with exn ->
2258 dolog "error processing 'page' %S: %s"
2259 cmds (Printexc.to_string exn);
2260 exit 1;
2262 begin match state.currently with
2263 | Loading (l, gen) ->
2264 vlog "page %d took %f sec" l.pageno t;
2265 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2266 begin match state.throttle with
2267 | None ->
2268 let preloadedpages =
2269 if conf.preload
2270 then preloadlayout state.y
2271 else state.layout
2273 let evict () =
2274 let module IntSet =
2275 Set.Make (struct type t = int let compare = (-) end) in
2276 let set =
2277 List.fold_left (fun s l -> IntSet.add l.pageno s)
2278 IntSet.empty preloadedpages
2280 let evictedpages =
2281 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2282 if not (IntSet.mem pageno set)
2283 then (
2284 wcmd "freepage %s" opaque;
2285 key :: accu
2287 else accu
2288 ) state.pagemap []
2290 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2292 evict ();
2293 state.currently <- Idle;
2294 if gen = state.gen
2295 then (
2296 tilepage l.pageno pageopaque state.layout;
2297 load state.layout;
2298 load preloadedpages;
2299 if pagevisible state.layout l.pageno
2300 && layoutready state.layout
2301 then G.postRedisplay "page";
2304 | Some (layout, _, _) ->
2305 state.currently <- Idle;
2306 tilepage l.pageno pageopaque layout;
2307 load state.layout
2308 end;
2310 | _ ->
2311 dolog "Inconsistent loading state";
2312 logcurrently state.currently;
2313 exit 1
2316 | "tile" ->
2317 let (x, y, opaque, size, t) =
2319 Scanf.sscanf args "%u %u %s %u %f"
2320 (fun x y p size t -> (x, y, p, size, t))
2321 with exn ->
2322 dolog "error processing 'tile' %S: %s"
2323 cmds (Printexc.to_string exn);
2324 exit 1;
2326 begin match state.currently with
2327 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2328 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2330 if tilew != conf.tilew || tileh != conf.tileh
2331 then (
2332 wcmd "freetile %s" opaque;
2333 state.currently <- Idle;
2334 load state.layout;
2336 else (
2337 puttileopaque l col row gen cs angle opaque size t;
2338 state.memused <- state.memused + size;
2339 state.uioh#infochanged Memused;
2340 gctiles ();
2341 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2342 opaque, size) state.tilelru;
2344 let layout =
2345 match state.throttle with
2346 | None -> state.layout
2347 | Some (layout, _, _) -> layout
2350 state.currently <- Idle;
2351 if gen = state.gen
2352 && conf.colorspace = cs
2353 && conf.angle = angle
2354 && tilevisible layout l.pageno x y
2355 then conttiling l.pageno pageopaque;
2357 begin match state.throttle with
2358 | None ->
2359 preload state.layout;
2360 if gen = state.gen
2361 && conf.colorspace = cs
2362 && conf.angle = angle
2363 && tilevisible state.layout l.pageno x y
2364 then G.postRedisplay "tile nothrottle";
2366 | Some (layout, y, _) ->
2367 let ready = layoutready layout in
2368 if ready
2369 then (
2370 state.y <- y;
2371 state.layout <- layout;
2372 state.throttle <- None;
2373 G.postRedisplay "throttle";
2375 else load layout;
2376 end;
2379 | _ ->
2380 dolog "Inconsistent tiling state";
2381 logcurrently state.currently;
2382 exit 1
2385 | "pdim" ->
2386 let pdim =
2388 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2389 with exn ->
2390 dolog "error processing 'pdim' %S: %s"
2391 cmds (Printexc.to_string exn);
2392 exit 1;
2394 state.uioh#infochanged Pdim;
2395 state.pdims <- pdim :: state.pdims
2397 | "o" ->
2398 let (l, n, t, h, pos) =
2400 Scanf.sscanf args "%u %u %d %u %n"
2401 (fun l n t h pos -> l, n, t, h, pos)
2402 with exn ->
2403 dolog "error processing 'o' %S: %s"
2404 cmds (Printexc.to_string exn);
2405 exit 1;
2407 let s = String.sub args pos (String.length args - pos) in
2408 let outline = (s, l, (n, float t /. float h, 0.0)) in
2409 begin match state.currently with
2410 | Outlining outlines ->
2411 state.currently <- Outlining (outline :: outlines)
2412 | Idle ->
2413 state.currently <- Outlining [outline]
2414 | currently ->
2415 dolog "invalid outlining state";
2416 logcurrently currently
2419 | "info" ->
2420 state.docinfo <- (1, args) :: state.docinfo
2422 | "infoend" ->
2423 state.uioh#infochanged Docinfo;
2424 state.docinfo <- List.rev state.docinfo
2426 | _ ->
2427 dolog "unknown cmd `%S'" cmds
2430 let onhist cb =
2431 let rc = cb.rc in
2432 let action = function
2433 | HCprev -> cbget cb ~-1
2434 | HCnext -> cbget cb 1
2435 | HCfirst -> cbget cb ~-(cb.rc)
2436 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2437 and cancel () = cb.rc <- rc
2438 in (action, cancel)
2441 let search pattern forward =
2442 if String.length pattern > 0
2443 then
2444 let pn, py =
2445 match state.layout with
2446 | [] -> 0, 0
2447 | l :: _ ->
2448 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2450 wcmd "search %d %d %d %d,%s\000"
2451 (btod conf.icase) pn py (btod forward) pattern;
2454 let intentry text key =
2455 let c =
2456 if key >= 32 && key < 127
2457 then Char.chr key
2458 else '\000'
2460 match c with
2461 | '0' .. '9' ->
2462 let text = addchar text c in
2463 TEcont text
2465 | _ ->
2466 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2467 TEcont text
2470 let linknentry text key =
2471 let c =
2472 if key >= 32 && key < 127
2473 then Char.chr key
2474 else '\000'
2476 match c with
2477 | 'a' .. 'z' ->
2478 let text = addchar text c in
2479 TEcont text
2481 | _ ->
2482 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2483 TEcont text
2486 let linkndone f s =
2487 if String.length s > 0
2488 then (
2489 let n =
2490 let l = String.length s in
2491 let rec loop pos n = if pos = l then n else
2492 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2493 loop (pos+1) (n*26 + m)
2494 in loop 0 0
2496 let rec loop n = function
2497 | [] -> ()
2498 | l :: rest ->
2499 match getopaque l.pageno with
2500 | None -> loop n rest
2501 | Some opaque ->
2502 let m = getlinkcount opaque in
2503 if n < m
2504 then (
2505 let under = getlink opaque n in
2506 f under
2508 else loop (n-m) rest
2510 loop n state.layout;
2514 let textentry text key =
2515 if key land 0xff00 = 0xff00
2516 then TEcont text
2517 else TEcont (text ^ Wsi.toutf8 key)
2520 let reqlayout angle proportional =
2521 match state.throttle with
2522 | None ->
2523 if nogeomcmds state.geomcmds
2524 then state.anchor <- getanchor ();
2525 conf.angle <- angle mod 360;
2526 if conf.angle != 0
2527 then (
2528 match state.mode with
2529 | LinkNav _ -> state.mode <- View
2530 | _ -> ()
2532 conf.proportional <- proportional;
2533 invalidate "reqlayout"
2534 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2535 | _ -> ()
2538 let settrim trimmargins trimfuzz =
2539 if nogeomcmds state.geomcmds
2540 then state.anchor <- getanchor ();
2541 conf.trimmargins <- trimmargins;
2542 conf.trimfuzz <- trimfuzz;
2543 let x0, y0, x1, y1 = trimfuzz in
2544 invalidate "settrim"
2545 (fun () ->
2546 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2547 Hashtbl.iter (fun _ opaque ->
2548 wcmd "freepage %s" opaque;
2549 ) state.pagemap;
2550 Hashtbl.clear state.pagemap;
2553 let setzoom zoom =
2554 match state.throttle with
2555 | None ->
2556 let zoom = max 0.01 zoom in
2557 if zoom <> conf.zoom
2558 then (
2559 state.prevzoom <- conf.zoom;
2560 conf.zoom <- zoom;
2561 reshape conf.winw conf.winh;
2562 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2565 | Some (layout, y, started) ->
2566 let time =
2567 match conf.maxwait with
2568 | None -> 0.0
2569 | Some t -> t
2571 let dt = now () -. started in
2572 if dt > time
2573 then (
2574 state.y <- y;
2575 load layout;
2579 let setcolumns mode columns coverA coverB =
2580 state.prevcolumns <- Some (conf.columns, conf.zoom);
2581 if columns < 0
2582 then (
2583 if isbirdseye mode
2584 then showtext '!' "split mode doesn't work in bird's eye"
2585 else (
2586 conf.columns <- Csplit (-columns, [||]);
2587 state.x <- 0;
2588 conf.zoom <- 1.0;
2591 else (
2592 if columns < 2
2593 then (
2594 conf.columns <- Csingle [||];
2595 state.x <- 0;
2596 setzoom 1.0;
2598 else (
2599 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2600 conf.zoom <- 1.0;
2603 reshape conf.winw conf.winh;
2606 let enterbirdseye () =
2607 let zoom = float conf.thumbw /. float conf.winw in
2608 let birdseyepageno =
2609 let cy = conf.winh / 2 in
2610 let fold = function
2611 | [] -> 0
2612 | l :: rest ->
2613 let rec fold best = function
2614 | [] -> best.pageno
2615 | l :: rest ->
2616 let d = cy - (l.pagedispy + l.pagevh/2)
2617 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2618 if abs d < abs dbest
2619 then fold l rest
2620 else best.pageno
2621 in fold l rest
2623 fold state.layout
2625 state.mode <- Birdseye (
2626 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2628 conf.zoom <- zoom;
2629 conf.presentation <- false;
2630 conf.interpagespace <- 10;
2631 conf.hlinks <- false;
2632 state.x <- 0;
2633 state.mstate <- Mnone;
2634 conf.maxwait <- None;
2635 conf.columns <- (
2636 match conf.beyecolumns with
2637 | Some c ->
2638 conf.zoom <- 1.0;
2639 Cmulti ((c, 0, 0), [||])
2640 | None -> Csingle [||]
2642 Wsi.setcursor Wsi.CURSOR_INHERIT;
2643 if conf.verbose
2644 then
2645 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2646 (100.0*.zoom)
2647 else
2648 state.text <- ""
2650 reshape conf.winw conf.winh;
2653 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2654 state.mode <- View;
2655 conf.zoom <- c.zoom;
2656 conf.presentation <- c.presentation;
2657 conf.interpagespace <- c.interpagespace;
2658 conf.maxwait <- c.maxwait;
2659 conf.hlinks <- c.hlinks;
2660 conf.beyecolumns <- (
2661 match conf.columns with
2662 | Cmulti ((c, _, _), _) -> Some c
2663 | Csingle _ -> None
2664 | Csplit _ -> failwith "leaving bird's eye split mode"
2666 conf.columns <- (
2667 match c.columns with
2668 | Cmulti (c, _) -> Cmulti (c, [||])
2669 | Csingle _ -> Csingle [||]
2670 | Csplit (c, _) -> Csplit (c, [||])
2672 state.x <- leftx;
2673 if conf.verbose
2674 then
2675 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2676 (100.0*.conf.zoom)
2678 reshape conf.winw conf.winh;
2679 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2682 let togglebirdseye () =
2683 match state.mode with
2684 | Birdseye vals -> leavebirdseye vals true
2685 | View -> enterbirdseye ()
2686 | _ -> ()
2689 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2690 let pageno = max 0 (pageno - incr) in
2691 let rec loop = function
2692 | [] -> gotopage1 pageno 0
2693 | l :: _ when l.pageno = pageno ->
2694 if l.pagedispy >= 0 && l.pagey = 0
2695 then G.postRedisplay "upbirdseye"
2696 else gotopage1 pageno 0
2697 | _ :: rest -> loop rest
2699 loop state.layout;
2700 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2703 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2704 let pageno = min (state.pagecount - 1) (pageno + incr) in
2705 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2706 let rec loop = function
2707 | [] ->
2708 let y, h = getpageyh pageno in
2709 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2710 gotoy (clamp dy)
2711 | l :: _ when l.pageno = pageno ->
2712 if l.pagevh != l.pageh
2713 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2714 else G.postRedisplay "downbirdseye"
2715 | _ :: rest -> loop rest
2717 loop state.layout
2720 let optentry mode _ key =
2721 let btos b = if b then "on" else "off" in
2722 if key >= 32 && key < 127
2723 then
2724 let c = Char.chr key in
2725 match c with
2726 | 's' ->
2727 let ondone s =
2728 try conf.scrollstep <- int_of_string s with exc ->
2729 state.text <- Printf.sprintf "bad integer `%s': %s"
2730 s (Printexc.to_string exc)
2732 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2734 | 'A' ->
2735 let ondone s =
2737 conf.autoscrollstep <- int_of_string s;
2738 if state.autoscroll <> None
2739 then state.autoscroll <- Some conf.autoscrollstep
2740 with exc ->
2741 state.text <- Printf.sprintf "bad integer `%s': %s"
2742 s (Printexc.to_string exc)
2744 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2746 | 'C' ->
2747 let ondone s =
2749 let n, a, b = multicolumns_of_string s in
2750 setcolumns mode n a b;
2751 with exc ->
2752 state.text <- Printf.sprintf "bad columns `%s': %s"
2753 s (Printexc.to_string exc)
2755 TEswitch ("columns: ", "", None, textentry, ondone, true)
2757 | 'Z' ->
2758 let ondone s =
2760 let zoom = float (int_of_string s) /. 100.0 in
2761 setzoom zoom
2762 with exc ->
2763 state.text <- Printf.sprintf "bad integer `%s': %s"
2764 s (Printexc.to_string exc)
2766 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2768 | 't' ->
2769 let ondone s =
2771 conf.thumbw <- bound (int_of_string s) 2 4096;
2772 state.text <-
2773 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2774 begin match mode with
2775 | Birdseye beye ->
2776 leavebirdseye beye false;
2777 enterbirdseye ();
2778 | _ -> ();
2780 with exc ->
2781 state.text <- Printf.sprintf "bad integer `%s': %s"
2782 s (Printexc.to_string exc)
2784 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2786 | 'R' ->
2787 let ondone s =
2788 match try
2789 Some (int_of_string s)
2790 with exc ->
2791 state.text <- Printf.sprintf "bad integer `%s': %s"
2792 s (Printexc.to_string exc);
2793 None
2794 with
2795 | Some angle -> reqlayout angle conf.proportional
2796 | None -> ()
2798 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2800 | 'i' ->
2801 conf.icase <- not conf.icase;
2802 TEdone ("case insensitive search " ^ (btos conf.icase))
2804 | 'p' ->
2805 conf.preload <- not conf.preload;
2806 gotoy state.y;
2807 TEdone ("preload " ^ (btos conf.preload))
2809 | 'v' ->
2810 conf.verbose <- not conf.verbose;
2811 TEdone ("verbose " ^ (btos conf.verbose))
2813 | 'd' ->
2814 conf.debug <- not conf.debug;
2815 TEdone ("debug " ^ (btos conf.debug))
2817 | 'h' ->
2818 conf.maxhfit <- not conf.maxhfit;
2819 state.maxy <- calcheight ();
2820 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2822 | 'c' ->
2823 conf.crophack <- not conf.crophack;
2824 TEdone ("crophack " ^ btos conf.crophack)
2826 | 'a' ->
2827 let s =
2828 match conf.maxwait with
2829 | None ->
2830 conf.maxwait <- Some infinity;
2831 "always wait for page to complete"
2832 | Some _ ->
2833 conf.maxwait <- None;
2834 "show placeholder if page is not ready"
2836 TEdone s
2838 | 'f' ->
2839 conf.underinfo <- not conf.underinfo;
2840 TEdone ("underinfo " ^ btos conf.underinfo)
2842 | 'P' ->
2843 conf.savebmarks <- not conf.savebmarks;
2844 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2846 | 'S' ->
2847 let ondone s =
2849 let pageno, py =
2850 match state.layout with
2851 | [] -> 0, 0
2852 | l :: _ ->
2853 l.pageno, l.pagey
2855 conf.interpagespace <- int_of_string s;
2856 docolumns conf.columns;
2857 state.maxy <- calcheight ();
2858 let y = getpagey pageno in
2859 gotoy (y + py)
2860 with exc ->
2861 state.text <- Printf.sprintf "bad integer `%s': %s"
2862 s (Printexc.to_string exc)
2864 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2866 | 'l' ->
2867 reqlayout conf.angle (not conf.proportional);
2868 TEdone ("proportional display " ^ btos conf.proportional)
2870 | 'T' ->
2871 settrim (not conf.trimmargins) conf.trimfuzz;
2872 TEdone ("trim margins " ^ btos conf.trimmargins)
2874 | 'I' ->
2875 conf.invert <- not conf.invert;
2876 TEdone ("invert colors " ^ btos conf.invert)
2878 | 'x' ->
2879 let ondone s =
2880 cbput state.hists.sel s;
2881 conf.selcmd <- s;
2883 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2884 textentry, ondone, true)
2886 | _ ->
2887 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2888 TEstop
2889 else
2890 TEcont state.text
2893 class type lvsource = object
2894 method getitemcount : int
2895 method getitem : int -> (string * int)
2896 method hasaction : int -> bool
2897 method exit :
2898 uioh:uioh ->
2899 cancel:bool ->
2900 active:int ->
2901 first:int ->
2902 pan:int ->
2903 qsearch:string ->
2904 uioh option
2905 method getactive : int
2906 method getfirst : int
2907 method getqsearch : string
2908 method setqsearch : string -> unit
2909 method getpan : int
2910 end;;
2912 class virtual lvsourcebase = object
2913 val mutable m_active = 0
2914 val mutable m_first = 0
2915 val mutable m_qsearch = ""
2916 val mutable m_pan = 0
2917 method getactive = m_active
2918 method getfirst = m_first
2919 method getqsearch = m_qsearch
2920 method getpan = m_pan
2921 method setqsearch s = m_qsearch <- s
2922 end;;
2924 let withoutlastutf8 s =
2925 let len = String.length s in
2926 if len = 0
2927 then s
2928 else
2929 let rec find pos =
2930 if pos = 0
2931 then pos
2932 else
2933 let b = Char.code s.[pos] in
2934 if b land 0b110000 = 0b11000000
2935 then find (pos-1)
2936 else pos-1
2938 let first =
2939 if Char.code s.[len-1] land 0x80 = 0
2940 then len-1
2941 else find (len-1)
2943 String.sub s 0 first;
2946 let textentrykeyboard
2947 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2948 let enttext te =
2949 state.mode <- Textentry (te, onleave);
2950 state.text <- "";
2951 enttext ();
2952 G.postRedisplay "textentrykeyboard enttext";
2954 let histaction cmd =
2955 match opthist with
2956 | None -> ()
2957 | Some (action, _) ->
2958 state.mode <- Textentry (
2959 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2961 G.postRedisplay "textentry histaction"
2963 match key with
2964 | 0xff08 -> (* backspace *)
2965 let s = withoutlastutf8 text in
2966 let len = String.length s in
2967 if cancelonempty && len = 0
2968 then (
2969 onleave Cancel;
2970 G.postRedisplay "textentrykeyboard after cancel";
2972 else (
2973 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2976 | 0xff0d ->
2977 ondone text;
2978 onleave Confirm;
2979 G.postRedisplay "textentrykeyboard after confirm"
2981 | 0xff52 -> histaction HCprev
2982 | 0xff54 -> histaction HCnext
2983 | 0xff50 -> histaction HCfirst
2984 | 0xff57 -> histaction HClast
2986 | 0xff1b -> (* escape*)
2987 if String.length text = 0
2988 then (
2989 begin match opthist with
2990 | None -> ()
2991 | Some (_, onhistcancel) -> onhistcancel ()
2992 end;
2993 onleave Cancel;
2994 state.text <- "";
2995 G.postRedisplay "textentrykeyboard after cancel2"
2997 else (
2998 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3001 | 0xff9f | 0xffff -> () (* delete *)
3003 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3004 begin match onkey text key with
3005 | TEdone text ->
3006 ondone text;
3007 onleave Confirm;
3008 G.postRedisplay "textentrykeyboard after confirm2";
3010 | TEcont text ->
3011 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3013 | TEstop ->
3014 onleave Cancel;
3015 G.postRedisplay "textentrykeyboard after cancel3"
3017 | TEswitch te ->
3018 state.mode <- Textentry (te, onleave);
3019 G.postRedisplay "textentrykeyboard switch";
3020 end;
3022 | _ ->
3023 vlog "unhandled key %s" (Wsi.keyname key)
3026 let firstof first active =
3027 if first > active || abs (first - active) > fstate.maxrows - 1
3028 then max 0 (active - (fstate.maxrows/2))
3029 else first
3032 let calcfirst first active =
3033 if active > first
3034 then
3035 let rows = active - first in
3036 if rows > fstate.maxrows then active - fstate.maxrows else first
3037 else active
3040 let scrollph y maxy =
3041 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3042 let sh = float conf.winh /. sh in
3043 let sh = max sh (float conf.scrollh) in
3045 let percent =
3046 if y = state.maxy
3047 then 1.0
3048 else float y /. float maxy
3050 let position = (float conf.winh -. sh) *. percent in
3052 let position =
3053 if position +. sh > float conf.winh
3054 then float conf.winh -. sh
3055 else position
3057 position, sh;
3060 let coe s = (s :> uioh);;
3062 class listview ~(source:lvsource) ~trusted ~modehash =
3063 object (self)
3064 val m_pan = source#getpan
3065 val m_first = source#getfirst
3066 val m_active = source#getactive
3067 val m_qsearch = source#getqsearch
3068 val m_prev_uioh = state.uioh
3070 method private elemunder y =
3071 let n = y / (fstate.fontsize+1) in
3072 if m_first + n < source#getitemcount
3073 then (
3074 if source#hasaction (m_first + n)
3075 then Some (m_first + n)
3076 else None
3078 else None
3080 method display =
3081 Gl.enable `blend;
3082 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3083 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3084 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3085 GlDraw.color (1., 1., 1.);
3086 Gl.enable `texture_2d;
3087 let fs = fstate.fontsize in
3088 let nfs = fs + 1 in
3089 let ww = fstate.wwidth in
3090 let tabw = 30.0*.ww in
3091 let itemcount = source#getitemcount in
3092 let rec loop row =
3093 if (row - m_first) * nfs > conf.winh
3094 then ()
3095 else (
3096 if row >= 0 && row < itemcount
3097 then (
3098 let (s, level) = source#getitem row in
3099 let y = (row - m_first) * nfs in
3100 let x = 5.0 +. float (level + m_pan) *. ww in
3101 if row = m_active
3102 then (
3103 Gl.disable `texture_2d;
3104 GlDraw.polygon_mode `both `line;
3105 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3106 GlDraw.rect (1., float (y + 1))
3107 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3108 GlDraw.polygon_mode `both `fill;
3109 GlDraw.color (1., 1., 1.);
3110 Gl.enable `texture_2d;
3113 let drawtabularstring s =
3114 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3115 if trusted
3116 then
3117 let tabpos = try String.index s '\t' with Not_found -> -1 in
3118 if tabpos > 0
3119 then
3120 let len = String.length s - tabpos - 1 in
3121 let s1 = String.sub s 0 tabpos
3122 and s2 = String.sub s (tabpos + 1) len in
3123 let nx = drawstr x s1 in
3124 let sw = nx -. x in
3125 let x = x +. (max tabw sw) in
3126 drawstr x s2
3127 else
3128 drawstr x s
3129 else
3130 drawstr x s
3132 let _ = drawtabularstring s in
3133 loop (row+1)
3137 loop m_first;
3138 Gl.disable `blend;
3139 Gl.disable `texture_2d;
3141 method updownlevel incr =
3142 let len = source#getitemcount in
3143 let curlevel =
3144 if m_active >= 0 && m_active < len
3145 then snd (source#getitem m_active)
3146 else -1
3148 let rec flow i =
3149 if i = len then i-1 else if i = -1 then 0 else
3150 let _, l = source#getitem i in
3151 if l != curlevel then i else flow (i+incr)
3153 let active = flow m_active in
3154 let first = calcfirst m_first active in
3155 G.postRedisplay "outline updownlevel";
3156 {< m_active = active; m_first = first >}
3158 method private key1 key mask =
3159 let set1 active first qsearch =
3160 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3162 let search active pattern incr =
3163 let dosearch re =
3164 let rec loop n =
3165 if n >= 0 && n < source#getitemcount
3166 then (
3167 let s, _ = source#getitem n in
3169 (try ignore (Str.search_forward re s 0); true
3170 with Not_found -> false)
3171 then Some n
3172 else loop (n + incr)
3174 else None
3176 loop active
3179 let re = Str.regexp_case_fold pattern in
3180 dosearch re
3181 with Failure s ->
3182 state.text <- s;
3183 None
3185 let itemcount = source#getitemcount in
3186 let find start incr =
3187 let rec find i =
3188 if i = -1 || i = itemcount
3189 then -1
3190 else (
3191 if source#hasaction i
3192 then i
3193 else find (i + incr)
3196 find start
3198 let set active first =
3199 let first = bound first 0 (itemcount - fstate.maxrows) in
3200 state.text <- "";
3201 coe {< m_active = active; m_first = first >}
3203 let navigate incr =
3204 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3205 let active, first =
3206 let incr1 = if incr > 0 then 1 else -1 in
3207 if isvisible m_first m_active
3208 then
3209 let next =
3210 let next = m_active + incr in
3211 let next =
3212 if next < 0 || next >= itemcount
3213 then -1
3214 else find next incr1
3216 if next = -1 || abs (m_active - next) > fstate.maxrows
3217 then -1
3218 else next
3220 if next = -1
3221 then
3222 let first = m_first + incr in
3223 let first = bound first 0 (itemcount - 1) in
3224 let next =
3225 let next = m_active + incr in
3226 let next = bound next 0 (itemcount - 1) in
3227 find next ~-incr1
3229 let active = if next = -1 then m_active else next in
3230 active, first
3231 else
3232 let first = min next m_first in
3233 let first =
3234 if abs (next - first) > fstate.maxrows
3235 then first + incr
3236 else first
3238 next, first
3239 else
3240 let first = m_first + incr in
3241 let first = bound first 0 (itemcount - 1) in
3242 let active =
3243 let next = m_active + incr in
3244 let next = bound next 0 (itemcount - 1) in
3245 let next = find next incr1 in
3246 let active =
3247 if next = -1 || abs (m_active - first) > fstate.maxrows
3248 then (
3249 let active = if m_active = -1 then next else m_active in
3250 active
3252 else next
3254 if isvisible first active
3255 then active
3256 else -1
3258 active, first
3260 G.postRedisplay "listview navigate";
3261 set active first;
3263 match key with
3264 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3265 let incr = if key = 0x72 then -1 else 1 in
3266 let active, first =
3267 match search (m_active + incr) m_qsearch incr with
3268 | None ->
3269 state.text <- m_qsearch ^ " [not found]";
3270 m_active, m_first
3271 | Some active ->
3272 state.text <- m_qsearch;
3273 active, firstof m_first active
3275 G.postRedisplay "listview ctrl-r/s";
3276 set1 active first m_qsearch;
3278 | 0xff08 -> (* backspace *)
3279 if String.length m_qsearch = 0
3280 then coe self
3281 else (
3282 let qsearch = withoutlastutf8 m_qsearch in
3283 let len = String.length qsearch in
3284 if len = 0
3285 then (
3286 state.text <- "";
3287 G.postRedisplay "listview empty qsearch";
3288 set1 m_active m_first "";
3290 else
3291 let active, first =
3292 match search m_active qsearch ~-1 with
3293 | None ->
3294 state.text <- qsearch ^ " [not found]";
3295 m_active, m_first
3296 | Some active ->
3297 state.text <- qsearch;
3298 active, firstof m_first active
3300 G.postRedisplay "listview backspace qsearch";
3301 set1 active first qsearch
3304 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3305 let pattern = m_qsearch ^ Wsi.toutf8 key in
3306 let active, first =
3307 match search m_active pattern 1 with
3308 | None ->
3309 state.text <- pattern ^ " [not found]";
3310 m_active, m_first
3311 | Some active ->
3312 state.text <- pattern;
3313 active, firstof m_first active
3315 G.postRedisplay "listview qsearch add";
3316 set1 active first pattern;
3318 | 0xff1b -> (* escape *)
3319 state.text <- "";
3320 if String.length m_qsearch = 0
3321 then (
3322 G.postRedisplay "list view escape";
3323 begin
3324 match
3325 source#exit (coe self) true m_active m_first m_pan m_qsearch
3326 with
3327 | None -> m_prev_uioh
3328 | Some uioh -> uioh
3331 else (
3332 G.postRedisplay "list view kill qsearch";
3333 source#setqsearch "";
3334 coe {< m_qsearch = "" >}
3337 | 0xff0d -> (* return *)
3338 state.text <- "";
3339 let self = {< m_qsearch = "" >} in
3340 source#setqsearch "";
3341 let opt =
3342 G.postRedisplay "listview enter";
3343 if m_active >= 0 && m_active < source#getitemcount
3344 then (
3345 source#exit (coe self) false m_active m_first m_pan "";
3347 else (
3348 source#exit (coe self) true m_active m_first m_pan "";
3351 begin match opt with
3352 | None -> m_prev_uioh
3353 | Some uioh -> uioh
3356 | 0xff9f | 0xffff -> (* delete *)
3357 coe self
3359 | 0xff52 -> navigate ~-1 (* up *)
3360 | 0xff54 -> navigate 1 (* down *)
3361 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3362 | 0xff56 -> navigate fstate.maxrows (* next *)
3364 | 0xff53 -> (* right *)
3365 state.text <- "";
3366 G.postRedisplay "listview right";
3367 coe {< m_pan = m_pan - 1 >}
3369 | 0xff51 -> (* left *)
3370 state.text <- "";
3371 G.postRedisplay "listview left";
3372 coe {< m_pan = m_pan + 1 >}
3374 | 0xff50 -> (* home *)
3375 let active = find 0 1 in
3376 G.postRedisplay "listview home";
3377 set active 0;
3379 | 0xff57 -> (* end *)
3380 let first = max 0 (itemcount - fstate.maxrows) in
3381 let active = find (itemcount - 1) ~-1 in
3382 G.postRedisplay "listview end";
3383 set active first;
3385 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3386 coe self
3388 | _ ->
3389 dolog "listview unknown key %#x" key; coe self
3391 method key key mask =
3392 match state.mode with
3393 | Textentry te -> textentrykeyboard key mask te; coe self
3394 | _ -> self#key1 key mask
3396 method button button down x y _ =
3397 let opt =
3398 match button with
3399 | 1 when x > conf.winw - conf.scrollbw ->
3400 G.postRedisplay "listview scroll";
3401 if down
3402 then
3403 let _, position, sh = self#scrollph in
3404 if y > truncate position && y < truncate (position +. sh)
3405 then (
3406 state.mstate <- Mscrolly;
3407 Some (coe self)
3409 else
3410 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3411 let first = truncate (s *. float source#getitemcount) in
3412 let first = min source#getitemcount first in
3413 Some (coe {< m_first = first; m_active = first >})
3414 else (
3415 state.mstate <- Mnone;
3416 Some (coe self);
3418 | 1 when not down ->
3419 begin match self#elemunder y with
3420 | Some n ->
3421 G.postRedisplay "listview click";
3422 source#exit
3423 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3424 | _ ->
3425 Some (coe self)
3427 | n when (n == 4 || n == 5) && not down ->
3428 let len = source#getitemcount in
3429 let first =
3430 if n = 5 && m_first + fstate.maxrows >= len
3431 then
3432 m_first
3433 else
3434 let first = m_first + (if n == 4 then -1 else 1) in
3435 bound first 0 (len - 1)
3437 G.postRedisplay "listview wheel";
3438 Some (coe {< m_first = first >})
3439 | n when (n = 6 || n = 7) && not down ->
3440 let inc = m_first + (if n = 7 then -1 else 1) in
3441 G.postRedisplay "listview hwheel";
3442 Some (coe {< m_pan = m_pan + inc >})
3443 | _ ->
3444 Some (coe self)
3446 match opt with
3447 | None -> m_prev_uioh
3448 | Some uioh -> uioh
3450 method motion _ y =
3451 match state.mstate with
3452 | Mscrolly ->
3453 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3454 let first = truncate (s *. float source#getitemcount) in
3455 let first = min source#getitemcount first in
3456 G.postRedisplay "listview motion";
3457 coe {< m_first = first; m_active = first >}
3458 | _ -> coe self
3460 method pmotion x y =
3461 if x < conf.winw - conf.scrollbw
3462 then
3463 let n =
3464 match self#elemunder y with
3465 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3466 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3468 let o =
3469 if n != m_active
3470 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3471 else self
3473 coe o
3474 else (
3475 Wsi.setcursor Wsi.CURSOR_INHERIT;
3476 coe self
3479 method infochanged _ = ()
3481 method scrollpw = (0, 0.0, 0.0)
3482 method scrollph =
3483 let nfs = fstate.fontsize + 1 in
3484 let y = m_first * nfs in
3485 let itemcount = source#getitemcount in
3486 let maxi = max 0 (itemcount - fstate.maxrows) in
3487 let maxy = maxi * nfs in
3488 let p, h = scrollph y maxy in
3489 conf.scrollbw, p, h
3491 method modehash = modehash
3492 end;;
3494 class outlinelistview ~source =
3495 object (self)
3496 inherit listview
3497 ~source:(source :> lvsource)
3498 ~trusted:false
3499 ~modehash:(findkeyhash conf "outline")
3500 as super
3502 method key key mask =
3503 let calcfirst first active =
3504 if active > first
3505 then
3506 let rows = active - first in
3507 let maxrows =
3508 if String.length state.text = 0
3509 then fstate.maxrows
3510 else fstate.maxrows - 2
3512 if rows > maxrows then active - maxrows else first
3513 else active
3515 let navigate incr =
3516 let active = m_active + incr in
3517 let active = bound active 0 (source#getitemcount - 1) in
3518 let first = calcfirst m_first active in
3519 G.postRedisplay "outline navigate";
3520 coe {< m_active = active; m_first = first >}
3522 let ctrl = Wsi.withctrl mask in
3523 match key with
3524 | 110 when ctrl -> (* ctrl-n *)
3525 source#narrow m_qsearch;
3526 G.postRedisplay "outline ctrl-n";
3527 coe {< m_first = 0; m_active = 0 >}
3529 | 117 when ctrl -> (* ctrl-u *)
3530 source#denarrow;
3531 G.postRedisplay "outline ctrl-u";
3532 state.text <- "";
3533 coe {< m_first = 0; m_active = 0 >}
3535 | 108 when ctrl -> (* ctrl-l *)
3536 let first = m_active - (fstate.maxrows / 2) in
3537 G.postRedisplay "outline ctrl-l";
3538 coe {< m_first = first >}
3540 | 0xff9f | 0xffff -> (* delete *)
3541 source#remove m_active;
3542 G.postRedisplay "outline delete";
3543 let active = max 0 (m_active-1) in
3544 coe {< m_first = firstof m_first active;
3545 m_active = active >}
3547 | 0xff52 -> navigate ~-1 (* up *)
3548 | 0xff54 -> navigate 1 (* down *)
3549 | 0xff55 -> (* prior *)
3550 navigate ~-(fstate.maxrows)
3551 | 0xff56 -> (* next *)
3552 navigate fstate.maxrows
3554 | 0xff53 -> (* [ctrl-]right *)
3555 let o =
3556 if ctrl
3557 then (
3558 G.postRedisplay "outline ctrl right";
3559 {< m_pan = m_pan + 1 >}
3561 else self#updownlevel 1
3563 coe o
3565 | 0xff51 -> (* [ctrl-]left *)
3566 let o =
3567 if ctrl
3568 then (
3569 G.postRedisplay "outline ctrl left";
3570 {< m_pan = m_pan - 1 >}
3572 else self#updownlevel ~-1
3574 coe o
3576 | 0xff50 -> (* home *)
3577 G.postRedisplay "outline home";
3578 coe {< m_first = 0; m_active = 0 >}
3580 | 0xff57 -> (* end *)
3581 let active = source#getitemcount - 1 in
3582 let first = max 0 (active - fstate.maxrows) in
3583 G.postRedisplay "outline end";
3584 coe {< m_active = active; m_first = first >}
3586 | _ -> super#key key mask
3589 let outlinesource usebookmarks =
3590 let empty = [||] in
3591 (object
3592 inherit lvsourcebase
3593 val mutable m_items = empty
3594 val mutable m_orig_items = empty
3595 val mutable m_prev_items = empty
3596 val mutable m_narrow_pattern = ""
3597 val mutable m_hadremovals = false
3599 method getitemcount =
3600 Array.length m_items + (if m_hadremovals then 1 else 0)
3602 method getitem n =
3603 if n == Array.length m_items && m_hadremovals
3604 then
3605 ("[Confirm removal]", 0)
3606 else
3607 let s, n, _ = m_items.(n) in
3608 (s, n)
3610 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3611 ignore (uioh, first, qsearch);
3612 let confrimremoval = m_hadremovals && active = Array.length m_items in
3613 let items =
3614 if String.length m_narrow_pattern = 0
3615 then m_orig_items
3616 else m_items
3618 if not cancel
3619 then (
3620 if not confrimremoval
3621 then(
3622 let _, _, anchor = m_items.(active) in
3623 gotoanchor anchor;
3624 m_items <- items;
3626 else (
3627 state.bookmarks <- Array.to_list m_items;
3628 m_orig_items <- m_items;
3631 else m_items <- items;
3632 m_pan <- pan;
3633 None
3635 method hasaction _ = true
3637 method greetmsg =
3638 if Array.length m_items != Array.length m_orig_items
3639 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3640 else ""
3642 method narrow pattern =
3643 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3644 match reopt with
3645 | None -> ()
3646 | Some re ->
3647 let rec loop accu n =
3648 if n = -1
3649 then (
3650 m_narrow_pattern <- pattern;
3651 m_items <- Array.of_list accu
3653 else
3654 let (s, _, _) as o = m_items.(n) in
3655 let accu =
3656 if (try ignore (Str.search_forward re s 0); true
3657 with Not_found -> false)
3658 then o :: accu
3659 else accu
3661 loop accu (n-1)
3663 loop [] (Array.length m_items - 1)
3665 method denarrow =
3666 m_orig_items <- (
3667 if usebookmarks
3668 then Array.of_list state.bookmarks
3669 else state.outlines
3671 m_items <- m_orig_items
3673 method remove m =
3674 if usebookmarks
3675 then
3676 if m >= 0 && m < Array.length m_items
3677 then (
3678 m_hadremovals <- true;
3679 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3680 let n = if n >= m then n+1 else n in
3681 m_items.(n)
3685 method reset anchor items =
3686 m_hadremovals <- false;
3687 if m_orig_items == empty || m_prev_items != items
3688 then (
3689 m_orig_items <- items;
3690 if String.length m_narrow_pattern = 0
3691 then m_items <- items;
3693 m_prev_items <- items;
3694 let rely = getanchory anchor in
3695 let active =
3696 let rec loop n best bestd =
3697 if n = Array.length m_items
3698 then best
3699 else
3700 let (_, _, anchor) = m_items.(n) in
3701 let orely = getanchory anchor in
3702 let d = abs (orely - rely) in
3703 if d < bestd
3704 then loop (n+1) n d
3705 else loop (n+1) best bestd
3707 loop 0 ~-1 max_int
3709 m_active <- active;
3710 m_first <- firstof m_first active
3711 end)
3714 let enterselector usebookmarks =
3715 let source = outlinesource usebookmarks in
3716 fun errmsg ->
3717 let outlines =
3718 if usebookmarks
3719 then Array.of_list state.bookmarks
3720 else state.outlines
3722 if Array.length outlines = 0
3723 then (
3724 showtext ' ' errmsg;
3726 else (
3727 state.text <- source#greetmsg;
3728 Wsi.setcursor Wsi.CURSOR_INHERIT;
3729 let anchor = getanchor () in
3730 source#reset anchor outlines;
3731 state.uioh <- coe (new outlinelistview ~source);
3732 G.postRedisplay "enter selector";
3736 let enteroutlinemode =
3737 let f = enterselector false in
3738 fun ()-> f "Document has no outline";
3741 let enterbookmarkmode =
3742 let f = enterselector true in
3743 fun () -> f "Document has no bookmarks (yet)";
3746 let color_of_string s =
3747 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3748 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3752 let color_to_string (r, g, b) =
3753 let r = truncate (r *. 256.0)
3754 and g = truncate (g *. 256.0)
3755 and b = truncate (b *. 256.0) in
3756 Printf.sprintf "%d/%d/%d" r g b
3759 let irect_of_string s =
3760 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3763 let irect_to_string (x0,y0,x1,y1) =
3764 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3767 let makecheckers () =
3768 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3769 following to say:
3770 converted by Issac Trotts. July 25, 2002 *)
3771 let image_height = 64
3772 and image_width = 64 in
3774 let make_image () =
3775 let image =
3776 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3778 for i = 0 to image_width - 1 do
3779 for j = 0 to image_height - 1 do
3780 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3781 (if (i land 8 ) lxor (j land 8) = 0
3782 then [|255;255;255|] else [|200;200;200|])
3783 done
3784 done;
3785 image
3787 let image = make_image () in
3788 let id = GlTex.gen_texture () in
3789 GlTex.bind_texture `texture_2d id;
3790 GlPix.store (`unpack_alignment 1);
3791 GlTex.image2d image;
3792 List.iter (GlTex.parameter ~target:`texture_2d)
3793 [ `wrap_s `repeat;
3794 `wrap_t `repeat;
3795 `mag_filter `nearest;
3796 `min_filter `nearest ];
3800 let setcheckers enabled =
3801 match state.texid with
3802 | None ->
3803 if enabled then state.texid <- Some (makecheckers ())
3805 | Some texid ->
3806 if not enabled
3807 then (
3808 GlTex.delete_texture texid;
3809 state.texid <- None;
3813 let int_of_string_with_suffix s =
3814 let l = String.length s in
3815 let s1, shift =
3816 if l > 1
3817 then
3818 let suffix = Char.lowercase s.[l-1] in
3819 match suffix with
3820 | 'k' -> String.sub s 0 (l-1), 10
3821 | 'm' -> String.sub s 0 (l-1), 20
3822 | 'g' -> String.sub s 0 (l-1), 30
3823 | _ -> s, 0
3824 else s, 0
3826 let n = int_of_string s1 in
3827 let m = n lsl shift in
3828 if m < 0 || m < n
3829 then raise (Failure "value too large")
3830 else m
3833 let string_with_suffix_of_int n =
3834 if n = 0
3835 then "0"
3836 else
3837 let n, s =
3838 if n land ((1 lsl 20) - 1) = 0
3839 then n lsr 20, "M"
3840 else (
3841 if n land ((1 lsl 10) - 1) = 0
3842 then n lsr 10, "K"
3843 else n, ""
3846 let rec loop s n =
3847 let h = n mod 1000 in
3848 let n = n / 1000 in
3849 if n = 0
3850 then string_of_int h ^ s
3851 else (
3852 let s = Printf.sprintf "_%03d%s" h s in
3853 loop s n
3856 loop "" n ^ s;
3859 let defghyllscroll = (40, 8, 32);;
3860 let ghyllscroll_of_string s =
3861 let (n, a, b) as nab =
3862 if s = "default"
3863 then defghyllscroll
3864 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3866 if n <= a || n <= b || a >= b
3867 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3868 nab;
3871 let ghyllscroll_to_string ((n, a, b) as nab) =
3872 if nab = defghyllscroll
3873 then "default"
3874 else Printf.sprintf "%d,%d,%d" n a b;
3877 let describe_location () =
3878 let f (fn, _) l =
3879 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3881 let fn, ln = List.fold_left f (-1, -1) state.layout in
3882 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3883 let percent =
3884 if maxy <= 0
3885 then 100.
3886 else (100. *. (float state.y /. float maxy))
3888 if fn = ln
3889 then
3890 Printf.sprintf "page %d of %d [%.2f%%]"
3891 (fn+1) state.pagecount percent
3892 else
3893 Printf.sprintf
3894 "pages %d-%d of %d [%.2f%%]"
3895 (fn+1) (ln+1) state.pagecount percent
3898 let setpresentationmode v =
3899 let (n, _, _) = getanchor () in
3900 let _, h = getpageyh n in
3901 state.anchor <- (n, 0.0, float (calcips h));
3902 conf.presentation <- v;
3903 if conf.presentation
3904 then (
3905 if not conf.scrollbarinpm
3906 then state.scrollw <- 0;
3908 else state.scrollw <- conf.scrollbw;
3909 represent ();
3912 let enterinfomode =
3913 let btos b = if b then "\xe2\x88\x9a" else "" in
3914 let showextended = ref false in
3915 let leave mode = function
3916 | Confirm -> state.mode <- mode
3917 | Cancel -> state.mode <- mode in
3918 let src =
3919 (object
3920 val mutable m_first_time = true
3921 val mutable m_l = []
3922 val mutable m_a = [||]
3923 val mutable m_prev_uioh = nouioh
3924 val mutable m_prev_mode = View
3926 inherit lvsourcebase
3928 method reset prev_mode prev_uioh =
3929 m_a <- Array.of_list (List.rev m_l);
3930 m_l <- [];
3931 m_prev_mode <- prev_mode;
3932 m_prev_uioh <- prev_uioh;
3933 if m_first_time
3934 then (
3935 let rec loop n =
3936 if n >= Array.length m_a
3937 then ()
3938 else
3939 match m_a.(n) with
3940 | _, _, _, Action _ -> m_active <- n
3941 | _ -> loop (n+1)
3943 loop 0;
3944 m_first_time <- false;
3947 method int name get set =
3948 m_l <-
3949 (name, `int get, 1, Action (
3950 fun u ->
3951 let ondone s =
3952 try set (int_of_string s)
3953 with exn ->
3954 state.text <- Printf.sprintf "bad integer `%s': %s"
3955 s (Printexc.to_string exn)
3957 state.text <- "";
3958 let te = name ^ ": ", "", None, intentry, ondone, true in
3959 state.mode <- Textentry (te, leave m_prev_mode);
3961 )) :: m_l
3963 method int_with_suffix name get set =
3964 m_l <-
3965 (name, `intws get, 1, Action (
3966 fun u ->
3967 let ondone s =
3968 try set (int_of_string_with_suffix s)
3969 with exn ->
3970 state.text <- Printf.sprintf "bad integer `%s': %s"
3971 s (Printexc.to_string exn)
3973 state.text <- "";
3974 let te =
3975 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3977 state.mode <- Textentry (te, leave m_prev_mode);
3979 )) :: m_l
3981 method bool ?(offset=1) ?(btos=btos) name get set =
3982 m_l <-
3983 (name, `bool (btos, get), offset, Action (
3984 fun u ->
3985 let v = get () in
3986 set (not v);
3988 )) :: m_l
3990 method color name get set =
3991 m_l <-
3992 (name, `color get, 1, Action (
3993 fun u ->
3994 let invalid = (nan, nan, nan) in
3995 let ondone s =
3996 let c =
3997 try color_of_string s
3998 with exn ->
3999 state.text <- Printf.sprintf "bad color `%s': %s"
4000 s (Printexc.to_string exn);
4001 invalid
4003 if c <> invalid
4004 then set c;
4006 let te = name ^ ": ", "", None, textentry, ondone, true in
4007 state.text <- color_to_string (get ());
4008 state.mode <- Textentry (te, leave m_prev_mode);
4010 )) :: m_l
4012 method string name get set =
4013 m_l <-
4014 (name, `string get, 1, Action (
4015 fun u ->
4016 let ondone s = set s in
4017 let te = name ^ ": ", "", None, textentry, ondone, true in
4018 state.mode <- Textentry (te, leave m_prev_mode);
4020 )) :: m_l
4022 method colorspace name get set =
4023 m_l <-
4024 (name, `string get, 1, Action (
4025 fun _ ->
4026 let source =
4027 let vals = [| "rgb"; "bgr"; "gray" |] in
4028 (object
4029 inherit lvsourcebase
4031 initializer
4032 m_active <- int_of_colorspace conf.colorspace;
4033 m_first <- 0;
4035 method getitemcount = Array.length vals
4036 method getitem n = (vals.(n), 0)
4037 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4038 ignore (uioh, first, pan, qsearch);
4039 if not cancel then set active;
4040 None
4041 method hasaction _ = true
4042 end)
4044 state.text <- "";
4045 let modehash = findkeyhash conf "info" in
4046 coe (new listview ~source ~trusted:true ~modehash)
4047 )) :: m_l
4049 method caption s offset =
4050 m_l <- (s, `empty, offset, Noaction) :: m_l
4052 method caption2 s f offset =
4053 m_l <- (s, `string f, offset, Noaction) :: m_l
4055 method getitemcount = Array.length m_a
4057 method getitem n =
4058 let tostr = function
4059 | `int f -> string_of_int (f ())
4060 | `intws f -> string_with_suffix_of_int (f ())
4061 | `string f -> f ()
4062 | `color f -> color_to_string (f ())
4063 | `bool (btos, f) -> btos (f ())
4064 | `empty -> ""
4066 let name, t, offset, _ = m_a.(n) in
4067 ((let s = tostr t in
4068 if String.length s > 0
4069 then Printf.sprintf "%s\t%s" name s
4070 else name),
4071 offset)
4073 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4074 let uiohopt =
4075 if not cancel
4076 then (
4077 m_qsearch <- qsearch;
4078 let uioh =
4079 match m_a.(active) with
4080 | _, _, _, Action f -> f uioh
4081 | _ -> uioh
4083 Some uioh
4085 else None
4087 m_active <- active;
4088 m_first <- first;
4089 m_pan <- pan;
4090 uiohopt
4092 method hasaction n =
4093 match m_a.(n) with
4094 | _, _, _, Action _ -> true
4095 | _ -> false
4096 end)
4098 let rec fillsrc prevmode prevuioh =
4099 let sep () = src#caption "" 0 in
4100 let colorp name get set =
4101 src#string name
4102 (fun () -> color_to_string (get ()))
4103 (fun v ->
4105 let c = color_of_string v in
4106 set c
4107 with exn ->
4108 state.text <- Printf.sprintf "bad color `%s': %s"
4109 v (Printexc.to_string exn);
4112 let oldmode = state.mode in
4113 let birdseye = isbirdseye state.mode in
4115 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4117 src#bool "presentation mode"
4118 (fun () -> conf.presentation)
4119 (fun v -> setpresentationmode v);
4121 src#bool "ignore case in searches"
4122 (fun () -> conf.icase)
4123 (fun v -> conf.icase <- v);
4125 src#bool "preload"
4126 (fun () -> conf.preload)
4127 (fun v -> conf.preload <- v);
4129 src#bool "highlight links"
4130 (fun () -> conf.hlinks)
4131 (fun v -> conf.hlinks <- v);
4133 src#bool "under info"
4134 (fun () -> conf.underinfo)
4135 (fun v -> conf.underinfo <- v);
4137 src#bool "persistent bookmarks"
4138 (fun () -> conf.savebmarks)
4139 (fun v -> conf.savebmarks <- v);
4141 src#bool "proportional display"
4142 (fun () -> conf.proportional)
4143 (fun v -> reqlayout conf.angle v);
4145 src#bool "trim margins"
4146 (fun () -> conf.trimmargins)
4147 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4149 src#bool "persistent location"
4150 (fun () -> conf.jumpback)
4151 (fun v -> conf.jumpback <- v);
4153 sep ();
4154 src#int "inter-page space"
4155 (fun () -> conf.interpagespace)
4156 (fun n ->
4157 conf.interpagespace <- n;
4158 docolumns conf.columns;
4159 let pageno, py =
4160 match state.layout with
4161 | [] -> 0, 0
4162 | l :: _ ->
4163 l.pageno, l.pagey
4165 state.maxy <- calcheight ();
4166 let y = getpagey pageno in
4167 gotoy (y + py)
4170 src#int "page bias"
4171 (fun () -> conf.pagebias)
4172 (fun v -> conf.pagebias <- v);
4174 src#int "scroll step"
4175 (fun () -> conf.scrollstep)
4176 (fun n -> conf.scrollstep <- n);
4178 src#int "horizontal scroll step"
4179 (fun () -> conf.hscrollstep)
4180 (fun v -> conf.hscrollstep <- v);
4182 src#int "auto scroll step"
4183 (fun () ->
4184 match state.autoscroll with
4185 | Some step -> step
4186 | _ -> conf.autoscrollstep)
4187 (fun n ->
4188 if state.autoscroll <> None
4189 then state.autoscroll <- Some n;
4190 conf.autoscrollstep <- n);
4192 src#int "zoom"
4193 (fun () -> truncate (conf.zoom *. 100.))
4194 (fun v -> setzoom ((float v) /. 100.));
4196 src#int "rotation"
4197 (fun () -> conf.angle)
4198 (fun v -> reqlayout v conf.proportional);
4200 src#int "scroll bar width"
4201 (fun () -> state.scrollw)
4202 (fun v ->
4203 state.scrollw <- v;
4204 conf.scrollbw <- v;
4205 reshape conf.winw conf.winh;
4208 src#int "scroll handle height"
4209 (fun () -> conf.scrollh)
4210 (fun v -> conf.scrollh <- v;);
4212 src#int "thumbnail width"
4213 (fun () -> conf.thumbw)
4214 (fun v ->
4215 conf.thumbw <- min 4096 v;
4216 match oldmode with
4217 | Birdseye beye ->
4218 leavebirdseye beye false;
4219 enterbirdseye ()
4220 | _ -> ()
4223 let mode = state.mode in
4224 src#string "columns"
4225 (fun () ->
4226 match conf.columns with
4227 | Csingle _ -> "1"
4228 | Cmulti (multi, _) -> multicolumns_to_string multi
4229 | Csplit (count, _) -> "-" ^ string_of_int count
4231 (fun v ->
4232 let n, a, b = multicolumns_of_string v in
4233 setcolumns mode n a b);
4235 sep ();
4236 src#caption "Presentation mode" 0;
4237 src#bool "scrollbar visible"
4238 (fun () -> conf.scrollbarinpm)
4239 (fun v ->
4240 if v != conf.scrollbarinpm
4241 then (
4242 conf.scrollbarinpm <- v;
4243 if conf.presentation
4244 then (
4245 state.scrollw <- if v then conf.scrollbw else 0;
4246 reshape conf.winw conf.winh;
4251 sep ();
4252 src#caption "Pixmap cache" 0;
4253 src#int_with_suffix "size (advisory)"
4254 (fun () -> conf.memlimit)
4255 (fun v -> conf.memlimit <- v);
4257 src#caption2 "used"
4258 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4259 (string_with_suffix_of_int state.memused)
4260 (Hashtbl.length state.tilemap)) 1;
4262 sep ();
4263 src#caption "Layout" 0;
4264 src#caption2 "Dimension"
4265 (fun () ->
4266 Printf.sprintf "%dx%d (virtual %dx%d)"
4267 conf.winw conf.winh
4268 state.w state.maxy)
4270 if conf.debug
4271 then
4272 src#caption2 "Position" (fun () ->
4273 Printf.sprintf "%dx%d" state.x state.y
4275 else
4276 src#caption2 "Visible" (fun () -> describe_location ()) 1
4279 sep ();
4280 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4281 "Save these parameters as global defaults at exit"
4282 (fun () -> conf.bedefault)
4283 (fun v -> conf.bedefault <- v)
4286 sep ();
4287 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4288 src#bool ~offset:0 ~btos "Extended parameters"
4289 (fun () -> !showextended)
4290 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4291 if !showextended
4292 then (
4293 src#bool "checkers"
4294 (fun () -> conf.checkers)
4295 (fun v -> conf.checkers <- v; setcheckers v);
4296 src#bool "update cursor"
4297 (fun () -> conf.updatecurs)
4298 (fun v -> conf.updatecurs <- v);
4299 src#bool "verbose"
4300 (fun () -> conf.verbose)
4301 (fun v -> conf.verbose <- v);
4302 src#bool "invert colors"
4303 (fun () -> conf.invert)
4304 (fun v -> conf.invert <- v);
4305 src#bool "max fit"
4306 (fun () -> conf.maxhfit)
4307 (fun v -> conf.maxhfit <- v);
4308 src#bool "redirect stderr"
4309 (fun () -> conf.redirectstderr)
4310 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4311 src#string "uri launcher"
4312 (fun () -> conf.urilauncher)
4313 (fun v -> conf.urilauncher <- v);
4314 src#string "path launcher"
4315 (fun () -> conf.pathlauncher)
4316 (fun v -> conf.pathlauncher <- v);
4317 src#string "tile size"
4318 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4319 (fun v ->
4321 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4322 conf.tilew <- max 64 w;
4323 conf.tileh <- max 64 h;
4324 flushtiles ();
4325 with exn ->
4326 state.text <- Printf.sprintf "bad tile size `%s': %s"
4327 v (Printexc.to_string exn));
4328 src#int "texture count"
4329 (fun () -> conf.texcount)
4330 (fun v ->
4331 if realloctexts v
4332 then conf.texcount <- v
4333 else showtext '!' " Failed to set texture count please retry later"
4335 src#int "slice height"
4336 (fun () -> conf.sliceheight)
4337 (fun v ->
4338 conf.sliceheight <- v;
4339 wcmd "sliceh %d" conf.sliceheight;
4341 src#int "anti-aliasing level"
4342 (fun () -> conf.aalevel)
4343 (fun v ->
4344 conf.aalevel <- bound v 0 8;
4345 state.anchor <- getanchor ();
4346 opendoc state.path state.password;
4348 src#string "page scroll scaling factor"
4349 (fun () -> string_of_float conf.pgscale)
4350 (fun v ->
4352 let s = float_of_string v in
4353 conf.pgscale <- s
4354 with exn ->
4355 state.text <- Printf.sprintf
4356 "bad page scroll scaling factor `%s': %s"
4357 v (Printexc.to_string exn)
4360 src#int "ui font size"
4361 (fun () -> fstate.fontsize)
4362 (fun v -> setfontsize (bound v 5 100));
4363 src#int "hint font size"
4364 (fun () -> conf.hfsize)
4365 (fun v -> conf.hfsize <- bound v 5 100);
4366 colorp "background color"
4367 (fun () -> conf.bgcolor)
4368 (fun v -> conf.bgcolor <- v);
4369 src#bool "crop hack"
4370 (fun () -> conf.crophack)
4371 (fun v -> conf.crophack <- v);
4372 src#bool "multi column centering"
4373 (fun () -> conf.multicenter)
4374 (fun v -> conf.multicenter <- v; represent ());
4375 src#string "trim fuzz"
4376 (fun () -> irect_to_string conf.trimfuzz)
4377 (fun v ->
4379 conf.trimfuzz <- irect_of_string v;
4380 if conf.trimmargins
4381 then settrim true conf.trimfuzz;
4382 with exn ->
4383 state.text <- Printf.sprintf "bad irect `%s': %s"
4384 v (Printexc.to_string exn)
4386 src#string "throttle"
4387 (fun () ->
4388 match conf.maxwait with
4389 | None -> "show place holder if page is not ready"
4390 | Some time ->
4391 if time = infinity
4392 then "wait for page to fully render"
4393 else
4394 "wait " ^ string_of_float time
4395 ^ " seconds before showing placeholder"
4397 (fun v ->
4399 let f = float_of_string v in
4400 if f <= 0.0
4401 then conf.maxwait <- None
4402 else conf.maxwait <- Some f
4403 with exn ->
4404 state.text <- Printf.sprintf "bad time `%s': %s"
4405 v (Printexc.to_string exn)
4407 src#string "ghyll scroll"
4408 (fun () ->
4409 match conf.ghyllscroll with
4410 | None -> ""
4411 | Some nab -> ghyllscroll_to_string nab
4413 (fun v ->
4415 let gs =
4416 if String.length v = 0
4417 then None
4418 else Some (ghyllscroll_of_string v)
4420 conf.ghyllscroll <- gs
4421 with exn ->
4422 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4423 v (Printexc.to_string exn)
4425 src#string "selection command"
4426 (fun () -> conf.selcmd)
4427 (fun v -> conf.selcmd <- v);
4428 src#colorspace "color space"
4429 (fun () -> colorspace_to_string conf.colorspace)
4430 (fun v ->
4431 conf.colorspace <- colorspace_of_int v;
4432 wcmd "cs %d" v;
4433 load state.layout;
4437 sep ();
4438 src#caption "Document" 0;
4439 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4440 src#caption2 "Pages"
4441 (fun () -> string_of_int state.pagecount) 1;
4442 src#caption2 "Dimensions"
4443 (fun () -> string_of_int (List.length state.pdims)) 1;
4444 if conf.trimmargins
4445 then (
4446 sep ();
4447 src#caption "Trimmed margins" 0;
4448 src#caption2 "Dimensions"
4449 (fun () -> string_of_int (List.length state.pdims)) 1;
4452 sep ();
4453 src#caption "OpenGL" 0;
4454 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4455 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4456 src#reset prevmode prevuioh;
4458 fun () ->
4459 state.text <- "";
4460 let prevmode = state.mode
4461 and prevuioh = state.uioh in
4462 fillsrc prevmode prevuioh;
4463 let source = (src :> lvsource) in
4464 let modehash = findkeyhash conf "info" in
4465 state.uioh <- coe (object (self)
4466 inherit listview ~source ~trusted:true ~modehash as super
4467 val mutable m_prevmemused = 0
4468 method infochanged = function
4469 | Memused ->
4470 if m_prevmemused != state.memused
4471 then (
4472 m_prevmemused <- state.memused;
4473 G.postRedisplay "memusedchanged";
4475 | Pdim -> G.postRedisplay "pdimchanged"
4476 | Docinfo -> fillsrc prevmode prevuioh
4478 method key key mask =
4479 if not (Wsi.withctrl mask)
4480 then
4481 match key with
4482 | 0xff51 -> coe (self#updownlevel ~-1)
4483 | 0xff53 -> coe (self#updownlevel 1)
4484 | _ -> super#key key mask
4485 else super#key key mask
4486 end);
4487 G.postRedisplay "info";
4490 let enterhelpmode =
4491 let source =
4492 (object
4493 inherit lvsourcebase
4494 method getitemcount = Array.length state.help
4495 method getitem n =
4496 let s, n, _ = state.help.(n) in
4497 (s, n)
4499 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4500 let optuioh =
4501 if not cancel
4502 then (
4503 m_qsearch <- qsearch;
4504 match state.help.(active) with
4505 | _, _, Action f -> Some (f uioh)
4506 | _ -> Some (uioh)
4508 else None
4510 m_active <- active;
4511 m_first <- first;
4512 m_pan <- pan;
4513 optuioh
4515 method hasaction n =
4516 match state.help.(n) with
4517 | _, _, Action _ -> true
4518 | _ -> false
4520 initializer
4521 m_active <- -1
4522 end)
4523 in fun () ->
4524 let modehash = findkeyhash conf "help" in
4525 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4526 G.postRedisplay "help";
4529 let entermsgsmode =
4530 let msgsource =
4531 let re = Str.regexp "[\r\n]" in
4532 (object
4533 inherit lvsourcebase
4534 val mutable m_items = [||]
4536 method getitemcount = 1 + Array.length m_items
4538 method getitem n =
4539 if n = 0
4540 then "[Clear]", 0
4541 else m_items.(n-1), 0
4543 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4544 ignore uioh;
4545 if not cancel
4546 then (
4547 if active = 0
4548 then Buffer.clear state.errmsgs;
4549 m_qsearch <- qsearch;
4551 m_active <- active;
4552 m_first <- first;
4553 m_pan <- pan;
4554 None
4556 method hasaction n =
4557 n = 0
4559 method reset =
4560 state.newerrmsgs <- false;
4561 let l = Str.split re (Buffer.contents state.errmsgs) in
4562 m_items <- Array.of_list l
4564 initializer
4565 m_active <- 0
4566 end)
4567 in fun () ->
4568 state.text <- "";
4569 msgsource#reset;
4570 let source = (msgsource :> lvsource) in
4571 let modehash = findkeyhash conf "listview" in
4572 state.uioh <- coe (object
4573 inherit listview ~source ~trusted:false ~modehash as super
4574 method display =
4575 if state.newerrmsgs
4576 then msgsource#reset;
4577 super#display
4578 end);
4579 G.postRedisplay "msgs";
4582 let quickbookmark ?title () =
4583 match state.layout with
4584 | [] -> ()
4585 | l :: _ ->
4586 let title =
4587 match title with
4588 | None ->
4589 let sec = Unix.gettimeofday () in
4590 let tm = Unix.localtime sec in
4591 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4592 (l.pageno+1)
4593 tm.Unix.tm_mday
4594 tm.Unix.tm_mon
4595 (tm.Unix.tm_year + 1900)
4596 tm.Unix.tm_hour
4597 tm.Unix.tm_min
4598 | Some title -> title
4600 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4603 let doreshape w h =
4604 state.fullscreen <- None;
4605 Wsi.reshape w h;
4608 let setautoscrollspeed step goingdown =
4609 let incr = max 1 ((abs step) / 2) in
4610 let incr = if goingdown then incr else -incr in
4611 let astep = step + incr in
4612 state.autoscroll <- Some astep;
4615 let gotounder = function
4616 | Ulinkgoto (pageno, top) ->
4617 if pageno >= 0
4618 then (
4619 addnav ();
4620 gotopage1 pageno top;
4623 | Ulinkuri s ->
4624 gotouri s
4626 | Uremote (filename, pageno) ->
4627 let path =
4628 if Sys.file_exists filename
4629 then filename
4630 else
4631 let dir = Filename.dirname state.path in
4632 let path = Filename.concat dir filename in
4633 if Sys.file_exists path
4634 then path
4635 else ""
4637 if String.length path > 0
4638 then (
4639 let anchor = getanchor () in
4640 let ranchor = state.path, state.password, anchor in
4641 state.anchor <- (pageno, 0.0, 0.0);
4642 state.ranchors <- ranchor :: state.ranchors;
4643 opendoc path "";
4645 else showtext '!' ("Could not find " ^ filename)
4647 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4650 let canpan () =
4651 match conf.columns with
4652 | Csplit _ -> true
4653 | _ -> conf.zoom > 1.0
4656 let viewkeyboard key mask =
4657 let enttext te =
4658 let mode = state.mode in
4659 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4660 state.text <- "";
4661 enttext ();
4662 G.postRedisplay "view:enttext"
4664 let ctrl = Wsi.withctrl mask in
4665 match key with
4666 | 81 -> (* Q *)
4667 exit 0
4669 | 0xff63 -> (* insert *)
4670 if conf.angle mod 360 = 0
4671 then (
4672 state.mode <- LinkNav (Ltgendir 0);
4673 gotoy state.y;
4675 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4677 | 0xff1b | 113 -> (* escape / q *)
4678 begin match state.mstate with
4679 | Mzoomrect _ ->
4680 state.mstate <- Mnone;
4681 Wsi.setcursor Wsi.CURSOR_INHERIT;
4682 G.postRedisplay "kill zoom rect";
4683 | _ ->
4684 match state.ranchors with
4685 | [] -> raise Quit
4686 | (path, password, anchor) :: rest ->
4687 state.ranchors <- rest;
4688 state.anchor <- anchor;
4689 opendoc path password
4690 end;
4692 | 0xff08 -> (* backspace *)
4693 let y = getnav ~-1 in
4694 gotoy_and_clear_text y
4696 | 111 -> (* o *)
4697 enteroutlinemode ()
4699 | 117 -> (* u *)
4700 state.rects <- [];
4701 state.text <- "";
4702 G.postRedisplay "dehighlight";
4704 | 47 | 63 -> (* / ? *)
4705 let ondone isforw s =
4706 cbput state.hists.pat s;
4707 state.searchpattern <- s;
4708 search s isforw
4710 let s = String.create 1 in
4711 s.[0] <- Char.chr key;
4712 enttext (s, "", Some (onhist state.hists.pat),
4713 textentry, ondone (key = 47), true)
4715 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4716 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4717 setzoom (conf.zoom +. incr)
4719 | 43 | 0xffab -> (* + *)
4720 let ondone s =
4721 let n =
4722 try int_of_string s with exc ->
4723 state.text <- Printf.sprintf "bad integer `%s': %s"
4724 s (Printexc.to_string exc);
4725 max_int
4727 if n != max_int
4728 then (
4729 conf.pagebias <- n;
4730 state.text <- "page bias is now " ^ string_of_int n;
4733 enttext ("page bias: ", "", None, intentry, ondone, true)
4735 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4736 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4737 setzoom (max 0.01 (conf.zoom -. decr))
4739 | 45 | 0xffad -> (* - *)
4740 let ondone msg = state.text <- msg in
4741 enttext (
4742 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4743 optentry state.mode, ondone, true
4746 | 48 when ctrl -> (* ctrl-0 *)
4747 setzoom 1.0
4749 | 49 when ctrl -> (* ctrl-1 *)
4750 let cols =
4751 match conf.columns with
4752 | Csingle _ | Cmulti _ -> 1
4753 | Csplit (n, _) -> n
4755 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4756 if zoom < 1.0
4757 then setzoom zoom
4759 | 0xffc6 -> (* f9 *)
4760 togglebirdseye ()
4762 | 57 when ctrl -> (* ctrl-9 *)
4763 togglebirdseye ()
4765 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4766 when not ctrl -> (* 0..9 *)
4767 let ondone s =
4768 let n =
4769 try int_of_string s with exc ->
4770 state.text <- Printf.sprintf "bad integer `%s': %s"
4771 s (Printexc.to_string exc);
4774 if n >= 0
4775 then (
4776 addnav ();
4777 cbput state.hists.pag (string_of_int n);
4778 gotopage1 (n + conf.pagebias - 1) 0;
4781 let pageentry text key =
4782 match Char.unsafe_chr key with
4783 | 'g' -> TEdone text
4784 | _ -> intentry text key
4786 let text = "x" in text.[0] <- Char.chr key;
4787 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4789 | 98 -> (* b *)
4790 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4791 reshape conf.winw conf.winh;
4793 | 108 -> (* l *)
4794 conf.hlinks <- not conf.hlinks;
4795 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4796 G.postRedisplay "toggle highlightlinks";
4798 | 70 -> (* F *)
4799 state.glinks <- true;
4800 let mode = state.mode in
4801 state.mode <- Textentry (
4802 (":", "", None, linknentry, linkndone (fun under ->
4803 addnav ();
4804 gotounder under
4805 ), false
4806 ), fun _ ->
4807 state.glinks <- false;
4808 state.mode <- mode
4810 state.text <- "";
4811 G.postRedisplay "view:linkent(F)"
4813 | 121 -> (* y *)
4814 state.glinks <- true;
4815 let mode = state.mode in
4816 state.mode <- Textentry (
4817 (":", "", None, linknentry, linkndone (fun under ->
4818 match Ne.pipe () with
4819 | Ne.Exn exn ->
4820 showtext '!' (Printf.sprintf "pipe failed: %s"
4821 (Printexc.to_string exn));
4822 | Ne.Res (r, w) ->
4823 let popened =
4824 try popen conf.selcmd [r, 0; w, -1]; true
4825 with exn ->
4826 showtext '!'
4827 (Printf.sprintf "failed to execute %s: %s"
4828 conf.selcmd (Printexc.to_string exn));
4829 false
4831 let clo cap fd =
4832 Ne.clo fd (fun msg ->
4833 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4836 let s = undertext under in
4837 if popened
4838 then
4839 (try
4840 let l = String.length s in
4841 let n = Unix.write w s 0 l in
4842 if n != l
4843 then
4844 showtext '!'
4845 (Printf.sprintf
4846 "failed to write %d characters to sel pipe, wrote %d"
4849 with exn ->
4850 showtext '!'
4851 (Printf.sprintf "failed to write to sel pipe: %s"
4852 (Printexc.to_string exn)
4855 else dolog "%s" s;
4856 clo "pipe/r" r;
4857 clo "pipe/w" w;
4858 ), false
4860 fun _ ->
4861 state.glinks <- false;
4862 state.mode <- mode
4864 state.text <- "";
4865 G.postRedisplay "view:linkent"
4867 | 97 -> (* a *)
4868 begin match state.autoscroll with
4869 | Some step ->
4870 conf.autoscrollstep <- step;
4871 state.autoscroll <- None
4872 | None ->
4873 if conf.autoscrollstep = 0
4874 then state.autoscroll <- Some 1
4875 else state.autoscroll <- Some conf.autoscrollstep
4878 | 112 when ctrl -> (* ctrl-p *)
4879 launchpath ()
4881 | 80 -> (* P *)
4882 setpresentationmode (not conf.presentation);
4883 showtext ' ' ("presentation mode " ^
4884 if conf.presentation then "on" else "off");
4886 | 102 -> (* f *)
4887 begin match state.fullscreen with
4888 | None ->
4889 state.fullscreen <- Some (conf.winw, conf.winh);
4890 Wsi.fullscreen ()
4891 | Some (w, h) ->
4892 state.fullscreen <- None;
4893 doreshape w h
4896 | 103 -> (* g *)
4897 gotoy_and_clear_text 0
4899 | 71 -> (* G *)
4900 gotopage1 (state.pagecount - 1) 0
4902 | 112 | 78 -> (* p|N *)
4903 search state.searchpattern false
4905 | 110 | 0xffc0 -> (* n|F3 *)
4906 search state.searchpattern true
4908 | 116 -> (* t *)
4909 begin match state.layout with
4910 | [] -> ()
4911 | l :: _ ->
4912 gotoy_and_clear_text (getpagey l.pageno)
4915 | 32 -> (* space *)
4916 begin match state.layout with
4917 | [] -> ()
4918 | l :: rest ->
4919 match conf.columns with
4920 | Csingle _ | Cmulti _ ->
4921 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4922 then
4923 let y = clamp (pgscale conf.winh) in
4924 gotoy_and_clear_text y
4925 else
4926 let pageno = min (l.pageno+1) (state.pagecount-1) in
4927 gotoy_and_clear_text (getpagey pageno)
4928 | Csplit (n, _) ->
4929 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4930 then
4931 let pagey, pageh = getpageyh l.pageno in
4932 let pagey = pagey + pageh * l.pagecol in
4933 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4934 gotoy_and_clear_text (pagey + pageh + ips)
4937 | 0xff9f | 0xffff -> (* delete *)
4938 begin match state.layout with
4939 | [] -> ()
4940 | l :: _ ->
4941 match conf.columns with
4942 | Csingle _ | Cmulti _ ->
4943 if conf.presentation && l.pagey != 0
4944 then
4945 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4946 else
4947 let pageno = max 0 (l.pageno-1) in
4948 gotoy_and_clear_text (getpagey pageno)
4949 | Csplit (n, _) ->
4950 let y =
4951 if l.pagecol = 0
4952 then
4953 if l.pageno = 0
4954 then l.pagey
4955 else
4956 let pageno = max 0 (l.pageno-1) in
4957 let pagey, pageh = getpageyh pageno in
4958 pagey + (n-1)*pageh
4959 else
4960 let pagey, pageh = getpageyh l.pageno in
4961 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4963 gotoy_and_clear_text y
4966 | 61 -> (* = *)
4967 showtext ' ' (describe_location ());
4969 | 119 -> (* w *)
4970 begin match state.layout with
4971 | [] -> ()
4972 | l :: _ ->
4973 doreshape (l.pagew + state.scrollw) l.pageh;
4974 G.postRedisplay "w"
4977 | 39 -> (* ' *)
4978 enterbookmarkmode ()
4980 | 104 | 0xffbe -> (* h|F1 *)
4981 enterhelpmode ()
4983 | 105 -> (* i *)
4984 enterinfomode ()
4986 | 101 when conf.redirectstderr -> (* e *)
4987 entermsgsmode ()
4989 | 109 -> (* m *)
4990 let ondone s =
4991 match state.layout with
4992 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
4993 | _ -> ()
4995 enttext ("bookmark: ", "", None, textentry, ondone, true)
4997 | 126 -> (* ~ *)
4998 quickbookmark ();
4999 showtext ' ' "Quick bookmark added";
5001 | 122 -> (* z *)
5002 begin match state.layout with
5003 | l :: _ ->
5004 let rect = getpdimrect l.pagedimno in
5005 let w, h =
5006 if conf.crophack
5007 then
5008 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5009 truncate (1.2 *. (rect.(3) -. rect.(0))))
5010 else
5011 (truncate (rect.(1) -. rect.(0)),
5012 truncate (rect.(3) -. rect.(0)))
5014 let w = truncate ((float w)*.conf.zoom)
5015 and h = truncate ((float h)*.conf.zoom) in
5016 if w != 0 && h != 0
5017 then (
5018 state.anchor <- getanchor ();
5019 doreshape (w + state.scrollw) (h + conf.interpagespace)
5021 G.postRedisplay "z";
5023 | [] -> ()
5026 | 50 when ctrl -> (* ctrl-2 *)
5027 let maxw = getmaxw () in
5028 if maxw > 0.0
5029 then setzoom (maxw /. float conf.winw)
5031 | 60 | 62 -> (* < > *)
5032 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5034 | 91 | 93 -> (* [ ] *)
5035 conf.colorscale <-
5036 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5038 G.postRedisplay "brightness";
5040 | 99 when state.mode = View -> (* c *)
5041 let (c, a, b), z =
5042 match state.prevcolumns with
5043 | None -> (1, 0, 0), 1.0
5044 | Some (columns, z) ->
5045 let cab =
5046 match columns with
5047 | Csplit (c, _) -> -c, 0, 0
5048 | Cmulti ((c, a, b), _) -> c, a, b
5049 | Csingle _ -> 1, 0, 0
5051 cab, z
5053 setcolumns View c a b;
5054 setzoom z;
5056 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5057 setzoom state.prevzoom
5059 | 107 | 0xff52 -> (* k up *)
5060 begin match state.autoscroll with
5061 | None ->
5062 begin match state.mode with
5063 | Birdseye beye -> upbirdseye 1 beye
5064 | _ ->
5065 if ctrl
5066 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5067 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5069 | Some n ->
5070 setautoscrollspeed n false
5073 | 106 | 0xff54 -> (* j down *)
5074 begin match state.autoscroll with
5075 | None ->
5076 begin match state.mode with
5077 | Birdseye beye -> downbirdseye 1 beye
5078 | _ ->
5079 if ctrl
5080 then gotoy_and_clear_text (clamp (conf.winh/2))
5081 else gotoy_and_clear_text (clamp conf.scrollstep)
5083 | Some n ->
5084 setautoscrollspeed n true
5087 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5088 if canpan ()
5089 then
5090 let dx =
5091 if ctrl
5092 then conf.winw / 2
5093 else 10
5095 let dx = if key = 0xff51 then dx else -dx in
5096 state.x <- state.x + dx;
5097 gotoy_and_clear_text state.y
5098 else (
5099 state.text <- "";
5100 G.postRedisplay "lef/right"
5103 | 0xff55 -> (* prior *)
5104 let y =
5105 if ctrl
5106 then
5107 match state.layout with
5108 | [] -> state.y
5109 | l :: _ -> state.y - l.pagey
5110 else
5111 clamp (pgscale (-conf.winh))
5113 gotoghyll y
5115 | 0xff56 -> (* next *)
5116 let y =
5117 if ctrl
5118 then
5119 match List.rev state.layout with
5120 | [] -> state.y
5121 | l :: _ -> getpagey l.pageno
5122 else
5123 clamp (pgscale conf.winh)
5125 gotoghyll y
5127 | 0xff50 -> (* home *)
5128 gotoghyll 0
5129 | 0xff57 -> (* end *)
5130 gotoghyll (clamp state.maxy)
5131 | 0xff53 when Wsi.withalt mask -> (* right *)
5132 gotoghyll (getnav ~-1)
5133 | 0xff51 when Wsi.withalt mask -> (* left *)
5134 gotoghyll (getnav 1)
5136 | 114 -> (* r *)
5137 state.anchor <- getanchor ();
5138 opendoc state.path state.password
5140 | 118 when conf.debug -> (* v *)
5141 state.rects <- [];
5142 List.iter (fun l ->
5143 match getopaque l.pageno with
5144 | None -> ()
5145 | Some opaque ->
5146 let x0, y0, x1, y1 = pagebbox opaque in
5147 let a,b = float x0, float y0 in
5148 let c,d = float x1, float y0 in
5149 let e,f = float x1, float y1 in
5150 let h,j = float x0, float y1 in
5151 let rect = (a,b,c,d,e,f,h,j) in
5152 debugrect rect;
5153 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5154 ) state.layout;
5155 G.postRedisplay "v";
5157 | _ ->
5158 vlog "huh? %s" (Wsi.keyname key)
5161 let linknavkeyboard key mask linknav =
5162 let getpage pageno =
5163 let rec loop = function
5164 | [] -> None
5165 | l :: _ when l.pageno = pageno -> Some l
5166 | _ :: rest -> loop rest
5167 in loop state.layout
5169 let doexact (pageno, n) =
5170 match getopaque pageno, getpage pageno with
5171 | Some opaque, Some l ->
5172 if key = 0xff0d
5173 then
5174 let under = getlink opaque n in
5175 G.postRedisplay "link gotounder";
5176 gotounder under;
5177 state.mode <- View;
5178 else
5179 let opt, dir =
5180 match key with
5181 | 0xff50 -> (* home *)
5182 Some (findlink opaque LDfirst), -1
5184 | 0xff57 -> (* end *)
5185 Some (findlink opaque LDlast), 1
5187 | 0xff51 -> (* left *)
5188 Some (findlink opaque (LDleft n)), -1
5190 | 0xff53 -> (* right *)
5191 Some (findlink opaque (LDright n)), 1
5193 | 0xff52 -> (* up *)
5194 Some (findlink opaque (LDup n)), -1
5196 | 0xff54 -> (* down *)
5197 Some (findlink opaque (LDdown n)), 1
5199 | _ -> None, 0
5201 let pwl l dir =
5202 begin match findpwl l.pageno dir with
5203 | Pwlnotfound -> ()
5204 | Pwl pageno ->
5205 let notfound dir =
5206 state.mode <- LinkNav (Ltgendir dir);
5207 let y, h = getpageyh pageno in
5208 let y =
5209 if dir < 0
5210 then y + h - conf.winh
5211 else y
5213 gotoy y
5215 begin match getopaque pageno, getpage pageno with
5216 | Some opaque, Some _ ->
5217 let link =
5218 let ld = if dir > 0 then LDfirst else LDlast in
5219 findlink opaque ld
5221 begin match link with
5222 | Lfound m ->
5223 showlinktype (getlink opaque m);
5224 state.mode <- LinkNav (Ltexact (pageno, m));
5225 G.postRedisplay "linknav jpage";
5226 | _ -> notfound dir
5227 end;
5228 | _ -> notfound dir
5229 end;
5230 end;
5232 begin match opt with
5233 | Some Lnotfound -> pwl l dir;
5234 | Some (Lfound m) ->
5235 if m = n
5236 then pwl l dir
5237 else (
5238 let _, y0, _, y1 = getlinkrect opaque m in
5239 if y0 < l.pagey
5240 then gotopage1 l.pageno y0
5241 else (
5242 let d = fstate.fontsize + 1 in
5243 if y1 - l.pagey > l.pagevh - d
5244 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5245 else G.postRedisplay "linknav";
5247 showlinktype (getlink opaque m);
5248 state.mode <- LinkNav (Ltexact (l.pageno, m));
5251 | None -> viewkeyboard key mask
5252 end;
5253 | _ -> viewkeyboard key mask
5255 if key = 0xff63
5256 then (
5257 state.mode <- View;
5258 G.postRedisplay "leave linknav"
5260 else
5261 match linknav with
5262 | Ltgendir _ -> viewkeyboard key mask
5263 | Ltexact exact -> doexact exact
5266 let keyboard key mask =
5267 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5268 then wcmd "interrupt"
5269 else state.uioh <- state.uioh#key key mask
5272 let birdseyekeyboard key mask
5273 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5274 let incr =
5275 match conf.columns with
5276 | Csingle _ -> 1
5277 | Cmulti ((c, _, _), _) -> c
5278 | Csplit _ -> failwith "bird's eye split mode"
5280 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5281 match key with
5282 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5283 let y, h = getpageyh pageno in
5284 let top = (conf.winh - h) / 2 in
5285 gotoy (max 0 (y - top))
5286 | 0xff0d -> leavebirdseye beye false
5287 | 0xff1b -> leavebirdseye beye true (* escape *)
5288 | 0xff52 -> upbirdseye incr beye (* up *)
5289 | 0xff54 -> downbirdseye incr beye (* down *)
5290 | 0xff51 -> upbirdseye 1 beye (* left *)
5291 | 0xff53 -> downbirdseye 1 beye (* right *)
5293 | 0xff55 -> (* prior *)
5294 begin match state.layout with
5295 | l :: _ ->
5296 if l.pagey != 0
5297 then (
5298 state.mode <- Birdseye (
5299 oconf, leftx, l.pageno, hooverpageno, anchor
5301 gotopage1 l.pageno 0;
5303 else (
5304 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5305 match layout with
5306 | [] -> gotoy (clamp (-conf.winh))
5307 | l :: _ ->
5308 state.mode <- Birdseye (
5309 oconf, leftx, l.pageno, hooverpageno, anchor
5311 gotopage1 l.pageno 0
5314 | [] -> gotoy (clamp (-conf.winh))
5315 end;
5317 | 0xff56 -> (* next *)
5318 begin match List.rev state.layout with
5319 | l :: _ ->
5320 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5321 begin match layout with
5322 | [] ->
5323 let incr = l.pageh - l.pagevh in
5324 if incr = 0
5325 then (
5326 state.mode <-
5327 Birdseye (
5328 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5330 G.postRedisplay "birdseye pagedown";
5332 else gotoy (clamp (incr + conf.interpagespace*2));
5334 | l :: _ ->
5335 state.mode <-
5336 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5337 gotopage1 l.pageno 0;
5340 | [] -> gotoy (clamp conf.winh)
5341 end;
5343 | 0xff50 -> (* home *)
5344 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5345 gotopage1 0 0
5347 | 0xff57 -> (* end *)
5348 let pageno = state.pagecount - 1 in
5349 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5350 if not (pagevisible state.layout pageno)
5351 then
5352 let h =
5353 match List.rev state.pdims with
5354 | [] -> conf.winh
5355 | (_, _, h, _) :: _ -> h
5357 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5358 else G.postRedisplay "birdseye end";
5359 | _ -> viewkeyboard key mask
5362 let drawpage l linkindexbase =
5363 let color =
5364 match state.mode with
5365 | Textentry _ -> scalecolor 0.4
5366 | LinkNav _
5367 | View -> scalecolor 1.0
5368 | Birdseye (_, _, pageno, hooverpageno, _) ->
5369 if l.pageno = hooverpageno
5370 then scalecolor 0.9
5371 else (
5372 if l.pageno = pageno
5373 then scalecolor 1.0
5374 else scalecolor 0.8
5377 drawtiles l color;
5378 begin match getopaque l.pageno with
5379 | Some opaque ->
5380 if tileready l l.pagex l.pagey
5381 then
5382 let x = l.pagedispx - l.pagex
5383 and y = l.pagedispy - l.pagey in
5384 let hlmask =
5385 match conf.columns with
5386 | Csingle _ | Cmulti _ ->
5387 (if conf.hlinks then 1 else 0)
5388 + (if state.glinks
5389 && not (isbirdseye state.mode) then 2 else 0)
5390 | _ -> 0
5392 let s =
5393 match state.mode with
5394 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5395 | _ -> ""
5397 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5398 else 0
5400 | _ -> 0
5401 end;
5404 let scrollindicator () =
5405 let sbw, ph, sh = state.uioh#scrollph in
5406 let sbh, pw, sw = state.uioh#scrollpw in
5408 GlDraw.color (0.64, 0.64, 0.64);
5409 GlDraw.rect
5410 (float (conf.winw - sbw), 0.)
5411 (float conf.winw, float conf.winh)
5413 GlDraw.rect
5414 (0., float (conf.winh - sbh))
5415 (float (conf.winw - state.scrollw - 1), float conf.winh)
5417 GlDraw.color (0.0, 0.0, 0.0);
5419 GlDraw.rect
5420 (float (conf.winw - sbw), ph)
5421 (float conf.winw, ph +. sh)
5423 GlDraw.rect
5424 (pw, float (conf.winh - sbh))
5425 (pw +. sw, float conf.winh)
5429 let showsel () =
5430 match state.mstate with
5431 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5434 | Msel ((x0, y0), (x1, y1)) ->
5435 let rec loop = function
5436 | l :: ls ->
5437 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5438 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5439 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5440 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5441 then
5442 match getopaque l.pageno with
5443 | Some opaque ->
5444 let x0, y0 = pagetranslatepoint l x0 y0 in
5445 let x1, y1 = pagetranslatepoint l x1 y1 in
5446 seltext opaque (x0, y0, x1, y1);
5447 | _ -> ()
5448 else loop ls
5449 | [] -> ()
5451 loop state.layout
5454 let showrects rects =
5455 Gl.enable `blend;
5456 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5457 GlDraw.polygon_mode `both `fill;
5458 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5459 List.iter
5460 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5461 List.iter (fun l ->
5462 if l.pageno = pageno
5463 then (
5464 let dx = float (l.pagedispx - l.pagex) in
5465 let dy = float (l.pagedispy - l.pagey) in
5466 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5467 GlDraw.begins `quads;
5469 GlDraw.vertex2 (x0+.dx, y0+.dy);
5470 GlDraw.vertex2 (x1+.dx, y1+.dy);
5471 GlDraw.vertex2 (x2+.dx, y2+.dy);
5472 GlDraw.vertex2 (x3+.dx, y3+.dy);
5474 GlDraw.ends ();
5476 ) state.layout
5477 ) rects
5479 Gl.disable `blend;
5482 let display () =
5483 GlClear.color (scalecolor2 conf.bgcolor);
5484 GlClear.clear [`color];
5485 let rec loop linkindexbase = function
5486 | l :: rest ->
5487 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5488 loop linkindexbase rest
5489 | [] -> ()
5491 loop 0 state.layout;
5492 let rects =
5493 match state.mode with
5494 | LinkNav (Ltexact (pageno, linkno)) ->
5495 begin match getopaque pageno with
5496 | Some opaque ->
5497 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5498 (pageno, 5, (
5499 float x0, float y0,
5500 float x1, float y0,
5501 float x1, float y1,
5502 float x0, float y1)
5503 ) :: state.rects
5504 | None -> state.rects
5506 | _ -> state.rects
5508 showrects rects;
5509 showsel ();
5510 state.uioh#display;
5511 begin match state.mstate with
5512 | Mzoomrect ((x0, y0), (x1, y1)) ->
5513 Gl.enable `blend;
5514 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5515 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5516 GlDraw.rect (float x0, float y0)
5517 (float x1, float y1);
5518 Gl.disable `blend;
5519 | _ -> ()
5520 end;
5521 enttext ();
5522 scrollindicator ();
5523 Wsi.swapb ();
5526 let zoomrect x y x1 y1 =
5527 let x0 = min x x1
5528 and x1 = max x x1
5529 and y0 = min y y1 in
5530 gotoy (state.y + y0);
5531 state.anchor <- getanchor ();
5532 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5533 let margin =
5534 if state.w < conf.winw - state.scrollw
5535 then (conf.winw - state.scrollw - state.w) / 2
5536 else 0
5538 state.x <- (state.x + margin) - x0;
5539 setzoom zoom;
5540 Wsi.setcursor Wsi.CURSOR_INHERIT;
5541 state.mstate <- Mnone;
5544 let scrollx x =
5545 let winw = conf.winw - state.scrollw - 1 in
5546 let s = float x /. float winw in
5547 let destx = truncate (float (state.w + winw) *. s) in
5548 state.x <- winw - destx;
5549 gotoy_and_clear_text state.y;
5550 state.mstate <- Mscrollx;
5553 let scrolly y =
5554 let s = float y /. float conf.winh in
5555 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5556 gotoy_and_clear_text desty;
5557 state.mstate <- Mscrolly;
5560 let viewmouse button down x y mask =
5561 match button with
5562 | n when (n == 4 || n == 5) && not down ->
5563 if Wsi.withctrl mask
5564 then (
5565 match state.mstate with
5566 | Mzoom (oldn, i) ->
5567 if oldn = n
5568 then (
5569 if i = 2
5570 then
5571 let incr =
5572 match n with
5573 | 5 ->
5574 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5575 | _ ->
5576 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5578 let zoom = conf.zoom -. incr in
5579 setzoom zoom;
5580 state.mstate <- Mzoom (n, 0);
5581 else
5582 state.mstate <- Mzoom (n, i+1);
5584 else state.mstate <- Mzoom (n, 0)
5586 | _ -> state.mstate <- Mzoom (n, 0)
5588 else (
5589 match state.autoscroll with
5590 | Some step -> setautoscrollspeed step (n=4)
5591 | None ->
5592 let incr =
5593 if n = 4
5594 then -conf.scrollstep
5595 else conf.scrollstep
5597 let incr = incr * 2 in
5598 let y = clamp incr in
5599 gotoy_and_clear_text y
5602 | n when (n = 6 || n = 7) && not down && canpan () ->
5603 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5604 gotoy_and_clear_text state.y
5606 | 1 when Wsi.withctrl mask ->
5607 if down
5608 then (
5609 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5610 state.mstate <- Mpan (x, y)
5612 else
5613 state.mstate <- Mnone
5615 | 3 ->
5616 if down
5617 then (
5618 Wsi.setcursor Wsi.CURSOR_CYCLE;
5619 let p = (x, y) in
5620 state.mstate <- Mzoomrect (p, p)
5622 else (
5623 match state.mstate with
5624 | Mzoomrect ((x0, y0), _) ->
5625 if abs (x-x0) > 10 && abs (y - y0) > 10
5626 then zoomrect x0 y0 x y
5627 else (
5628 state.mstate <- Mnone;
5629 Wsi.setcursor Wsi.CURSOR_INHERIT;
5630 G.postRedisplay "kill accidental zoom rect";
5632 | _ ->
5633 Wsi.setcursor Wsi.CURSOR_INHERIT;
5634 state.mstate <- Mnone
5637 | 1 when x > conf.winw - state.scrollw ->
5638 if down
5639 then
5640 let _, position, sh = state.uioh#scrollph in
5641 if y > truncate position && y < truncate (position +. sh)
5642 then state.mstate <- Mscrolly
5643 else scrolly y
5644 else
5645 state.mstate <- Mnone
5647 | 1 when y > conf.winh - state.hscrollh ->
5648 if down
5649 then
5650 let _, position, sw = state.uioh#scrollpw in
5651 if x > truncate position && x < truncate (position +. sw)
5652 then state.mstate <- Mscrollx
5653 else scrollx x
5654 else
5655 state.mstate <- Mnone
5657 | 1 ->
5658 let dest = if down then getunder x y else Unone in
5659 begin match dest with
5660 | Ulinkgoto _
5661 | Ulinkuri _
5662 | Uremote _
5663 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5664 gotounder dest
5666 | Unone when down ->
5667 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5668 state.mstate <- Mpan (x, y);
5670 | Unone | Utext _ ->
5671 if down
5672 then (
5673 if conf.angle mod 360 = 0
5674 then (
5675 state.mstate <- Msel ((x, y), (x, y));
5676 G.postRedisplay "mouse select";
5679 else (
5680 match state.mstate with
5681 | Mnone -> ()
5683 | Mzoom _ | Mscrollx | Mscrolly ->
5684 state.mstate <- Mnone
5686 | Mzoomrect ((x0, y0), _) ->
5687 zoomrect x0 y0 x y
5689 | Mpan _ ->
5690 Wsi.setcursor Wsi.CURSOR_INHERIT;
5691 state.mstate <- Mnone
5693 | Msel ((_, y0), (_, y1)) ->
5694 let rec loop = function
5695 | [] -> ()
5696 | l :: rest ->
5697 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5698 || ((y1 >= l.pagedispy
5699 && y1 <= (l.pagedispy + l.pagevh)))
5700 then
5701 match getopaque l.pageno with
5702 | Some opaque ->
5703 begin
5704 match Ne.pipe () with
5705 | Ne.Exn exn ->
5706 showtext '!'
5707 (Printf.sprintf
5708 "can not create sel pipe: %s"
5709 (Printexc.to_string exn));
5710 | Ne.Res (r, w) ->
5711 let doclose what fd =
5712 Ne.clo fd (fun msg ->
5713 dolog "%s close failed: %s" what msg)
5716 popen conf.selcmd [r, 0; w, -1];
5717 copysel w opaque;
5718 doclose "pipe/r" r;
5719 G.postRedisplay "copysel";
5720 with exn ->
5721 dolog "can not execute %S: %s"
5722 conf.selcmd (Printexc.to_string exn);
5723 doclose "pipe/r" r;
5724 doclose "pipe/w" w;
5726 | None -> ()
5727 else loop rest
5729 loop state.layout;
5730 Wsi.setcursor Wsi.CURSOR_INHERIT;
5731 state.mstate <- Mnone;
5735 | _ -> ()
5738 let birdseyemouse button down x y mask
5739 (conf, leftx, _, hooverpageno, anchor) =
5740 match button with
5741 | 1 when down ->
5742 let rec loop = function
5743 | [] -> ()
5744 | l :: rest ->
5745 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5746 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5747 then (
5748 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5750 else loop rest
5752 loop state.layout
5753 | 3 -> ()
5754 | _ -> viewmouse button down x y mask
5757 let mouse button down x y mask =
5758 state.uioh <- state.uioh#button button down x y mask;
5761 let motion ~x ~y =
5762 state.uioh <- state.uioh#motion x y
5765 let pmotion ~x ~y =
5766 state.uioh <- state.uioh#pmotion x y;
5769 let uioh = object
5770 method display = ()
5772 method key key mask =
5773 begin match state.mode with
5774 | Textentry textentry -> textentrykeyboard key mask textentry
5775 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5776 | View -> viewkeyboard key mask
5777 | LinkNav linknav -> linknavkeyboard key mask linknav
5778 end;
5779 state.uioh
5781 method button button bstate x y mask =
5782 begin match state.mode with
5783 | LinkNav _
5784 | View -> viewmouse button bstate x y mask
5785 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5786 | Textentry _ -> ()
5787 end;
5788 state.uioh
5790 method motion x y =
5791 begin match state.mode with
5792 | Textentry _ -> ()
5793 | View | Birdseye _ | LinkNav _ ->
5794 match state.mstate with
5795 | Mzoom _ | Mnone -> ()
5797 | Mpan (x0, y0) ->
5798 let dx = x - x0
5799 and dy = y0 - y in
5800 state.mstate <- Mpan (x, y);
5801 if canpan ()
5802 then state.x <- state.x + dx;
5803 let y = clamp dy in
5804 gotoy_and_clear_text y
5806 | Msel (a, _) ->
5807 state.mstate <- Msel (a, (x, y));
5808 G.postRedisplay "motion select";
5810 | Mscrolly ->
5811 let y = min conf.winh (max 0 y) in
5812 scrolly y
5814 | Mscrollx ->
5815 let x = min conf.winw (max 0 x) in
5816 scrollx x
5818 | Mzoomrect (p0, _) ->
5819 state.mstate <- Mzoomrect (p0, (x, y));
5820 G.postRedisplay "motion zoomrect";
5821 end;
5822 state.uioh
5824 method pmotion x y =
5825 begin match state.mode with
5826 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5827 let rec loop = function
5828 | [] ->
5829 if hooverpageno != -1
5830 then (
5831 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5832 G.postRedisplay "pmotion birdseye no hoover";
5834 | l :: rest ->
5835 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5836 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5837 then (
5838 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5839 G.postRedisplay "pmotion birdseye hoover";
5841 else loop rest
5843 loop state.layout
5845 | Textentry _ -> ()
5847 | LinkNav _
5848 | View ->
5849 match state.mstate with
5850 | Mnone -> updateunder x y
5851 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5853 end;
5854 state.uioh
5856 method infochanged _ = ()
5858 method scrollph =
5859 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5860 let p, h = scrollph state.y maxy in
5861 state.scrollw, p, h
5863 method scrollpw =
5864 let winw = conf.winw - state.scrollw - 1 in
5865 let fwinw = float winw in
5866 let sw =
5867 let sw = fwinw /. float state.w in
5868 let sw = fwinw *. sw in
5869 max sw (float conf.scrollh)
5871 let position, sw =
5872 let f = state.w+winw in
5873 let r = float (winw-state.x) /. float f in
5874 let p = fwinw *. r in
5875 p-.sw/.2., sw
5877 let sw =
5878 if position +. sw > fwinw
5879 then fwinw -. position
5880 else sw
5882 state.hscrollh, position, sw
5884 method modehash =
5885 let modename =
5886 match state.mode with
5887 | LinkNav _ -> "links"
5888 | Textentry _ -> "textentry"
5889 | Birdseye _ -> "birdseye"
5890 | View -> "view"
5892 findkeyhash conf modename
5893 end;;
5895 module Config =
5896 struct
5897 open Parser
5899 let fontpath = ref "";;
5901 module KeyMap =
5902 Map.Make (struct type t = (int * int) let compare = compare end);;
5904 let unent s =
5905 let l = String.length s in
5906 let b = Buffer.create l in
5907 unent b s 0 l;
5908 Buffer.contents b;
5911 let home =
5912 try Sys.getenv "HOME"
5913 with exn ->
5914 prerr_endline
5915 ("Can not determine home directory location: " ^
5916 Printexc.to_string exn);
5920 let modifier_of_string = function
5921 | "alt" -> Wsi.altmask
5922 | "shift" -> Wsi.shiftmask
5923 | "ctrl" | "control" -> Wsi.ctrlmask
5924 | "meta" -> Wsi.metamask
5925 | _ -> 0
5928 let key_of_string =
5929 let r = Str.regexp "-" in
5930 fun s ->
5931 let elems = Str.full_split r s in
5932 let f n k m =
5933 let g s =
5934 let m1 = modifier_of_string s in
5935 if m1 = 0
5936 then (Wsi.namekey s, m)
5937 else (k, m lor m1)
5938 in function
5939 | Str.Delim s when n land 1 = 0 -> g s
5940 | Str.Text s -> g s
5941 | Str.Delim _ -> (k, m)
5943 let rec loop n k m = function
5944 | [] -> (k, m)
5945 | x :: xs ->
5946 let k, m = f n k m x in
5947 loop (n+1) k m xs
5949 loop 0 0 0 elems
5952 let keys_of_string =
5953 let r = Str.regexp "[ \t]" in
5954 fun s ->
5955 let elems = Str.split r s in
5956 List.map key_of_string elems
5959 let copykeyhashes c =
5960 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5963 let config_of c attrs =
5964 let apply c k v =
5966 match k with
5967 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5968 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5969 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5970 | "preload" -> { c with preload = bool_of_string v }
5971 | "page-bias" -> { c with pagebias = int_of_string v }
5972 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5973 | "horizontal-scroll-step" ->
5974 { c with hscrollstep = max (int_of_string v) 1 }
5975 | "auto-scroll-step" ->
5976 { c with autoscrollstep = max 0 (int_of_string v) }
5977 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5978 | "crop-hack" -> { c with crophack = bool_of_string v }
5979 | "throttle" ->
5980 let mw =
5981 match String.lowercase v with
5982 | "true" -> Some infinity
5983 | "false" -> None
5984 | f -> Some (float_of_string f)
5986 { c with maxwait = mw}
5987 | "highlight-links" -> { c with hlinks = bool_of_string v }
5988 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5989 | "vertical-margin" ->
5990 { c with interpagespace = max 0 (int_of_string v) }
5991 | "zoom" ->
5992 let zoom = float_of_string v /. 100. in
5993 let zoom = max zoom 0.0 in
5994 { c with zoom = zoom }
5995 | "presentation" -> { c with presentation = bool_of_string v }
5996 | "rotation-angle" -> { c with angle = int_of_string v }
5997 | "width" -> { c with winw = max 20 (int_of_string v) }
5998 | "height" -> { c with winh = max 20 (int_of_string v) }
5999 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6000 | "proportional-display" -> { c with proportional = bool_of_string v }
6001 | "pixmap-cache-size" ->
6002 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6003 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6004 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6005 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6006 | "persistent-location" -> { c with jumpback = bool_of_string v }
6007 | "background-color" -> { c with bgcolor = color_of_string v }
6008 | "scrollbar-in-presentation" ->
6009 { c with scrollbarinpm = bool_of_string v }
6010 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6011 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6012 | "mupdf-store-size" ->
6013 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6014 | "checkers" -> { c with checkers = bool_of_string v }
6015 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6016 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6017 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6018 | "uri-launcher" -> { c with urilauncher = unent v }
6019 | "path-launcher" -> { c with pathlauncher = unent v }
6020 | "color-space" -> { c with colorspace = colorspace_of_string v }
6021 | "invert-colors" -> { c with invert = bool_of_string v }
6022 | "brightness" -> { c with colorscale = float_of_string v }
6023 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6024 | "ghyllscroll" ->
6025 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6026 | "columns" ->
6027 let (n, _, _) as nab = multicolumns_of_string v in
6028 if n < 0
6029 then { c with columns = Csplit (-n, [||]) }
6030 else { c with columns = Cmulti (nab, [||]) }
6031 | "birds-eye-columns" ->
6032 { c with beyecolumns = Some (max (int_of_string v) 2) }
6033 | "selection-command" -> { c with selcmd = unent v }
6034 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6035 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6036 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6037 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6038 | _ -> c
6039 with exn ->
6040 prerr_endline ("Error processing attribute (`" ^
6041 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6044 let rec fold c = function
6045 | [] -> c
6046 | (k, v) :: rest ->
6047 let c = apply c k v in
6048 fold c rest
6050 fold { c with keyhashes = copykeyhashes c } attrs;
6053 let fromstring f pos n v d =
6054 try f v
6055 with exn ->
6056 dolog "Error processing attribute (%S=%S) at %d\n%s"
6057 n v pos (Printexc.to_string exn)
6062 let bookmark_of attrs =
6063 let rec fold title page rely visy = function
6064 | ("title", v) :: rest -> fold v page rely visy rest
6065 | ("page", v) :: rest -> fold title v rely visy rest
6066 | ("rely", v) :: rest -> fold title page v visy rest
6067 | ("visy", v) :: rest -> fold title page rely v rest
6068 | _ :: rest -> fold title page rely visy rest
6069 | [] -> title, page, rely, visy
6071 fold "invalid" "0" "0" "0" attrs
6074 let doc_of attrs =
6075 let rec fold path page rely pan visy = function
6076 | ("path", v) :: rest -> fold v page rely pan visy rest
6077 | ("page", v) :: rest -> fold path v rely pan visy rest
6078 | ("rely", v) :: rest -> fold path page v pan visy rest
6079 | ("pan", v) :: rest -> fold path page rely v visy rest
6080 | ("visy", v) :: rest -> fold path page rely pan v rest
6081 | _ :: rest -> fold path page rely pan visy rest
6082 | [] -> path, page, rely, pan, visy
6084 fold "" "0" "0" "0" "0" attrs
6087 let map_of attrs =
6088 let rec fold rs ls = function
6089 | ("out", v) :: rest -> fold v ls rest
6090 | ("in", v) :: rest -> fold rs v rest
6091 | _ :: rest -> fold ls rs rest
6092 | [] -> ls, rs
6094 fold "" "" attrs
6097 let setconf dst src =
6098 dst.scrollbw <- src.scrollbw;
6099 dst.scrollh <- src.scrollh;
6100 dst.icase <- src.icase;
6101 dst.preload <- src.preload;
6102 dst.pagebias <- src.pagebias;
6103 dst.verbose <- src.verbose;
6104 dst.scrollstep <- src.scrollstep;
6105 dst.maxhfit <- src.maxhfit;
6106 dst.crophack <- src.crophack;
6107 dst.autoscrollstep <- src.autoscrollstep;
6108 dst.maxwait <- src.maxwait;
6109 dst.hlinks <- src.hlinks;
6110 dst.underinfo <- src.underinfo;
6111 dst.interpagespace <- src.interpagespace;
6112 dst.zoom <- src.zoom;
6113 dst.presentation <- src.presentation;
6114 dst.angle <- src.angle;
6115 dst.winw <- src.winw;
6116 dst.winh <- src.winh;
6117 dst.savebmarks <- src.savebmarks;
6118 dst.memlimit <- src.memlimit;
6119 dst.proportional <- src.proportional;
6120 dst.texcount <- src.texcount;
6121 dst.sliceheight <- src.sliceheight;
6122 dst.thumbw <- src.thumbw;
6123 dst.jumpback <- src.jumpback;
6124 dst.bgcolor <- src.bgcolor;
6125 dst.scrollbarinpm <- src.scrollbarinpm;
6126 dst.tilew <- src.tilew;
6127 dst.tileh <- src.tileh;
6128 dst.mustoresize <- src.mustoresize;
6129 dst.checkers <- src.checkers;
6130 dst.aalevel <- src.aalevel;
6131 dst.trimmargins <- src.trimmargins;
6132 dst.trimfuzz <- src.trimfuzz;
6133 dst.urilauncher <- src.urilauncher;
6134 dst.colorspace <- src.colorspace;
6135 dst.invert <- src.invert;
6136 dst.colorscale <- src.colorscale;
6137 dst.redirectstderr <- src.redirectstderr;
6138 dst.ghyllscroll <- src.ghyllscroll;
6139 dst.columns <- src.columns;
6140 dst.beyecolumns <- src.beyecolumns;
6141 dst.selcmd <- src.selcmd;
6142 dst.updatecurs <- src.updatecurs;
6143 dst.pathlauncher <- src.pathlauncher;
6144 dst.keyhashes <- copykeyhashes src;
6145 dst.hfsize <- src.hfsize;
6146 dst.hscrollstep <- src.hscrollstep;
6147 dst.pgscale <- src.pgscale;
6148 dst.multicenter <- src.multicenter;
6151 let get s =
6152 let h = Hashtbl.create 10 in
6153 let dc = { defconf with angle = defconf.angle } in
6154 let rec toplevel v t spos _ =
6155 match t with
6156 | Vdata | Vcdata | Vend -> v
6157 | Vopen ("llppconfig", _, closed) ->
6158 if closed
6159 then v
6160 else { v with f = llppconfig }
6161 | Vopen _ ->
6162 error "unexpected subelement at top level" s spos
6163 | Vclose _ -> error "unexpected close at top level" s spos
6165 and llppconfig v t spos _ =
6166 match t with
6167 | Vdata | Vcdata -> v
6168 | Vend -> error "unexpected end of input in llppconfig" s spos
6169 | Vopen ("defaults", attrs, closed) ->
6170 let c = config_of dc attrs in
6171 setconf dc c;
6172 if closed
6173 then v
6174 else { v with f = defaults }
6176 | Vopen ("ui-font", attrs, closed) ->
6177 let rec getsize size = function
6178 | [] -> size
6179 | ("size", v) :: rest ->
6180 let size =
6181 fromstring int_of_string spos "size" v fstate.fontsize in
6182 getsize size rest
6183 | l -> getsize size l
6185 fstate.fontsize <- getsize fstate.fontsize attrs;
6186 if closed
6187 then v
6188 else { v with f = uifont (Buffer.create 10) }
6190 | Vopen ("doc", attrs, closed) ->
6191 let pathent, spage, srely, span, svisy = doc_of attrs in
6192 let path = unent pathent
6193 and pageno = fromstring int_of_string spos "page" spage 0
6194 and rely = fromstring float_of_string spos "rely" srely 0.0
6195 and pan = fromstring int_of_string spos "pan" span 0
6196 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6197 let c = config_of dc attrs in
6198 let anchor = (pageno, rely, visy) in
6199 if closed
6200 then (Hashtbl.add h path (c, [], pan, anchor); v)
6201 else { v with f = doc path pan anchor c [] }
6203 | Vopen _ ->
6204 error "unexpected subelement in llppconfig" s spos
6206 | Vclose "llppconfig" -> { v with f = toplevel }
6207 | Vclose _ -> error "unexpected close in llppconfig" s spos
6209 and defaults v t spos _ =
6210 match t with
6211 | Vdata | Vcdata -> v
6212 | Vend -> error "unexpected end of input in defaults" s spos
6213 | Vopen ("keymap", attrs, closed) ->
6214 let modename =
6215 try List.assoc "mode" attrs
6216 with Not_found -> "global" in
6217 if closed
6218 then v
6219 else
6220 let ret keymap =
6221 let h = findkeyhash dc modename in
6222 KeyMap.iter (Hashtbl.replace h) keymap;
6223 defaults
6225 { v with f = pkeymap ret KeyMap.empty }
6227 | Vopen (_, _, _) ->
6228 error "unexpected subelement in defaults" s spos
6230 | Vclose "defaults" ->
6231 { v with f = llppconfig }
6233 | Vclose _ -> error "unexpected close in defaults" s spos
6235 and uifont b v t spos epos =
6236 match t with
6237 | Vdata | Vcdata ->
6238 Buffer.add_substring b s spos (epos - spos);
6240 | Vopen (_, _, _) ->
6241 error "unexpected subelement in ui-font" s spos
6242 | Vclose "ui-font" ->
6243 if String.length !fontpath = 0
6244 then fontpath := Buffer.contents b;
6245 { v with f = llppconfig }
6246 | Vclose _ -> error "unexpected close in ui-font" s spos
6247 | Vend -> error "unexpected end of input in ui-font" s spos
6249 and doc path pan anchor c bookmarks v t spos _ =
6250 match t with
6251 | Vdata | Vcdata -> v
6252 | Vend -> error "unexpected end of input in doc" s spos
6253 | Vopen ("bookmarks", _, closed) ->
6254 if closed
6255 then v
6256 else { v with f = pbookmarks path pan anchor c bookmarks }
6258 | Vopen ("keymap", attrs, closed) ->
6259 let modename =
6260 try List.assoc "mode" attrs
6261 with Not_found -> "global"
6263 if closed
6264 then v
6265 else
6266 let ret keymap =
6267 let h = findkeyhash c modename in
6268 KeyMap.iter (Hashtbl.replace h) keymap;
6269 doc path pan anchor c bookmarks
6271 { v with f = pkeymap ret KeyMap.empty }
6273 | Vopen (_, _, _) ->
6274 error "unexpected subelement in doc" s spos
6276 | Vclose "doc" ->
6277 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6278 { v with f = llppconfig }
6280 | Vclose _ -> error "unexpected close in doc" s spos
6282 and pkeymap ret keymap v t spos _ =
6283 match t with
6284 | Vdata | Vcdata -> v
6285 | Vend -> error "unexpected end of input in keymap" s spos
6286 | Vopen ("map", attrs, closed) ->
6287 let r, l = map_of attrs in
6288 let kss = fromstring keys_of_string spos "in" r [] in
6289 let lss = fromstring keys_of_string spos "out" l [] in
6290 let keymap =
6291 match kss with
6292 | [] -> keymap
6293 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6294 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6296 if closed
6297 then { v with f = pkeymap ret keymap }
6298 else
6299 let f () = v in
6300 { v with f = skip "map" f }
6302 | Vopen _ ->
6303 error "unexpected subelement in keymap" s spos
6305 | Vclose "keymap" ->
6306 { v with f = ret keymap }
6308 | Vclose _ -> error "unexpected close in keymap" s spos
6310 and pbookmarks path pan anchor c bookmarks v t spos _ =
6311 match t with
6312 | Vdata | Vcdata -> v
6313 | Vend -> error "unexpected end of input in bookmarks" s spos
6314 | Vopen ("item", attrs, closed) ->
6315 let titleent, spage, srely, svisy = bookmark_of attrs in
6316 let page = fromstring int_of_string spos "page" spage 0
6317 and rely = fromstring float_of_string spos "rely" srely 0.0
6318 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6319 let bookmarks =
6320 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6322 if closed
6323 then { v with f = pbookmarks path pan anchor c bookmarks }
6324 else
6325 let f () = v in
6326 { v with f = skip "item" f }
6328 | Vopen _ ->
6329 error "unexpected subelement in bookmarks" s spos
6331 | Vclose "bookmarks" ->
6332 { v with f = doc path pan anchor c bookmarks }
6334 | Vclose _ -> error "unexpected close in bookmarks" s spos
6336 and skip tag f v t spos _ =
6337 match t with
6338 | Vdata | Vcdata -> v
6339 | Vend ->
6340 error ("unexpected end of input in skipped " ^ tag) s spos
6341 | Vopen (tag', _, closed) ->
6342 if closed
6343 then v
6344 else
6345 let f' () = { v with f = skip tag f } in
6346 { v with f = skip tag' f' }
6347 | Vclose ctag ->
6348 if tag = ctag
6349 then f ()
6350 else error ("unexpected close in skipped " ^ tag) s spos
6353 parse { f = toplevel; accu = () } s;
6354 h, dc;
6357 let do_load f ic =
6359 let len = in_channel_length ic in
6360 let s = String.create len in
6361 really_input ic s 0 len;
6362 f s;
6363 with
6364 | Parse_error (msg, s, pos) ->
6365 let subs = subs s pos in
6366 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6367 failwith ("parse error: " ^ s)
6369 | exn ->
6370 failwith ("config load error: " ^ Printexc.to_string exn)
6373 let defconfpath =
6374 let dir =
6376 let dir = Filename.concat home ".config" in
6377 if Sys.is_directory dir then dir else home
6378 with _ -> home
6380 Filename.concat dir "llpp.conf"
6383 let confpath = ref defconfpath;;
6385 let load1 f =
6386 if Sys.file_exists !confpath
6387 then
6388 match
6389 (try Some (open_in_bin !confpath)
6390 with exn ->
6391 prerr_endline
6392 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6393 Printexc.to_string exn);
6394 None
6396 with
6397 | Some ic ->
6398 begin try
6399 f (do_load get ic)
6400 with exn ->
6401 prerr_endline
6402 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6403 Printexc.to_string exn);
6404 end;
6405 close_in ic;
6407 | None -> ()
6408 else
6409 f (Hashtbl.create 0, defconf)
6412 let load () =
6413 let f (h, dc) =
6414 let pc, pb, px, pa =
6416 Hashtbl.find h (Filename.basename state.path)
6417 with Not_found -> dc, [], 0, emptyanchor
6419 setconf defconf dc;
6420 setconf conf pc;
6421 state.bookmarks <- pb;
6422 state.x <- px;
6423 state.scrollw <- conf.scrollbw;
6424 if conf.jumpback
6425 then state.anchor <- pa;
6426 cbput state.hists.nav pa;
6428 load1 f
6431 let add_attrs bb always dc c =
6432 let ob s a b =
6433 if always || a != b
6434 then Printf.bprintf bb "\n %s='%b'" s a
6435 and oi s a b =
6436 if always || a != b
6437 then Printf.bprintf bb "\n %s='%d'" s a
6438 and oI s a b =
6439 if always || a != b
6440 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6441 and oz s a b =
6442 if always || a <> b
6443 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6444 and oF s a b =
6445 if always || a <> b
6446 then Printf.bprintf bb "\n %s='%f'" s a
6447 and oc s a b =
6448 if always || a <> b
6449 then
6450 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6451 and oC s a b =
6452 if always || a <> b
6453 then
6454 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6455 and oR s a b =
6456 if always || a <> b
6457 then
6458 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6459 and os s a b =
6460 if always || a <> b
6461 then
6462 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6463 and og s a b =
6464 if always || a <> b
6465 then
6466 match a with
6467 | None -> ()
6468 | Some (_N, _A, _B) ->
6469 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6470 and oW s a b =
6471 if always || a <> b
6472 then
6473 let v =
6474 match a with
6475 | None -> "false"
6476 | Some f ->
6477 if f = infinity
6478 then "true"
6479 else string_of_float f
6481 Printf.bprintf bb "\n %s='%s'" s v
6482 and oco s a b =
6483 if always || a <> b
6484 then
6485 match a with
6486 | Cmulti ((n, a, b), _) when n > 1 ->
6487 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6488 | Csplit (n, _) when n > 1 ->
6489 Printf.bprintf bb "\n %s='%d'" s ~-n
6490 | _ -> ()
6491 and obeco s a b =
6492 if always || a <> b
6493 then
6494 match a with
6495 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6496 | _ -> ()
6498 let w, h =
6499 if always
6500 then dc.winw, dc.winh
6501 else
6502 match state.fullscreen with
6503 | Some wh -> wh
6504 | None -> c.winw, c.winh
6506 oi "width" w dc.winw;
6507 oi "height" h dc.winh;
6508 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6509 oi "scroll-handle-height" c.scrollh dc.scrollh;
6510 ob "case-insensitive-search" c.icase dc.icase;
6511 ob "preload" c.preload dc.preload;
6512 oi "page-bias" c.pagebias dc.pagebias;
6513 oi "scroll-step" c.scrollstep dc.scrollstep;
6514 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6515 ob "max-height-fit" c.maxhfit dc.maxhfit;
6516 ob "crop-hack" c.crophack dc.crophack;
6517 oW "throttle" c.maxwait dc.maxwait;
6518 ob "highlight-links" c.hlinks dc.hlinks;
6519 ob "under-cursor-info" c.underinfo dc.underinfo;
6520 oi "vertical-margin" c.interpagespace dc.interpagespace;
6521 oz "zoom" c.zoom dc.zoom;
6522 ob "presentation" c.presentation dc.presentation;
6523 oi "rotation-angle" c.angle dc.angle;
6524 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6525 ob "proportional-display" c.proportional dc.proportional;
6526 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6527 oi "tex-count" c.texcount dc.texcount;
6528 oi "slice-height" c.sliceheight dc.sliceheight;
6529 oi "thumbnail-width" c.thumbw dc.thumbw;
6530 ob "persistent-location" c.jumpback dc.jumpback;
6531 oc "background-color" c.bgcolor dc.bgcolor;
6532 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6533 oi "tile-width" c.tilew dc.tilew;
6534 oi "tile-height" c.tileh dc.tileh;
6535 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6536 ob "checkers" c.checkers dc.checkers;
6537 oi "aalevel" c.aalevel dc.aalevel;
6538 ob "trim-margins" c.trimmargins dc.trimmargins;
6539 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6540 os "uri-launcher" c.urilauncher dc.urilauncher;
6541 os "path-launcher" c.pathlauncher dc.pathlauncher;
6542 oC "color-space" c.colorspace dc.colorspace;
6543 ob "invert-colors" c.invert dc.invert;
6544 oF "brightness" c.colorscale dc.colorscale;
6545 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6546 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6547 oco "columns" c.columns dc.columns;
6548 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6549 os "selection-command" c.selcmd dc.selcmd;
6550 ob "update-cursor" c.updatecurs dc.updatecurs;
6551 oi "hint-font-size" c.hfsize dc.hfsize;
6552 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6553 oF "page-scroll-scale" c.pgscale dc.pgscale;
6554 ob "multi-column-centering" c.multicenter dc.multicenter;
6557 let keymapsbuf always dc c =
6558 let bb = Buffer.create 16 in
6559 let rec loop = function
6560 | [] -> ()
6561 | (modename, h) :: rest ->
6562 let dh = findkeyhash dc modename in
6563 if always || h <> dh
6564 then (
6565 if Hashtbl.length h > 0
6566 then (
6567 if Buffer.length bb > 0
6568 then Buffer.add_char bb '\n';
6569 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6570 Hashtbl.iter (fun i o ->
6571 let isdifferent = always ||
6573 let dO = Hashtbl.find dh i in
6574 dO <> o
6575 with Not_found -> true
6577 if isdifferent
6578 then
6579 let addkm (k, m) =
6580 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6581 if Wsi.withalt m then Buffer.add_string bb "alt-";
6582 if Wsi.withshift m then Buffer.add_string bb "shift-";
6583 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6584 Buffer.add_string bb (Wsi.keyname k);
6586 let addkms l =
6587 let rec loop = function
6588 | [] -> ()
6589 | km :: [] -> addkm km
6590 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6592 loop l
6594 Buffer.add_string bb "<map in='";
6595 addkm i;
6596 match o with
6597 | KMinsrt km ->
6598 Buffer.add_string bb "' out='";
6599 addkm km;
6600 Buffer.add_string bb "'/>\n"
6602 | KMinsrl kms ->
6603 Buffer.add_string bb "' out='";
6604 addkms kms;
6605 Buffer.add_string bb "'/>\n"
6607 | KMmulti (ins, kms) ->
6608 Buffer.add_char bb ' ';
6609 addkms ins;
6610 Buffer.add_string bb "' out='";
6611 addkms kms;
6612 Buffer.add_string bb "'/>\n"
6613 ) h;
6614 Buffer.add_string bb "</keymap>";
6617 loop rest
6619 loop c.keyhashes;
6623 let save () =
6624 let uifontsize = fstate.fontsize in
6625 let bb = Buffer.create 32768 in
6626 let f (h, dc) =
6627 let dc = if conf.bedefault then conf else dc in
6628 Buffer.add_string bb "<llppconfig>\n";
6630 if String.length !fontpath > 0
6631 then
6632 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6633 uifontsize
6634 !fontpath
6635 else (
6636 if uifontsize <> 14
6637 then
6638 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6641 Buffer.add_string bb "<defaults ";
6642 add_attrs bb true dc dc;
6643 let kb = keymapsbuf true dc dc in
6644 if Buffer.length kb > 0
6645 then (
6646 Buffer.add_string bb ">\n";
6647 Buffer.add_buffer bb kb;
6648 Buffer.add_string bb "\n</defaults>\n";
6650 else Buffer.add_string bb "/>\n";
6652 let adddoc path pan anchor c bookmarks =
6653 if bookmarks == [] && c = dc && anchor = emptyanchor
6654 then ()
6655 else (
6656 Printf.bprintf bb "<doc path='%s'"
6657 (enent path 0 (String.length path));
6659 if anchor <> emptyanchor
6660 then (
6661 let n, rely, visy = anchor in
6662 Printf.bprintf bb " page='%d'" n;
6663 if rely > 1e-6
6664 then
6665 Printf.bprintf bb " rely='%f'" rely
6667 if abs_float visy > 1e-6
6668 then
6669 Printf.bprintf bb " visy='%f'" visy
6673 if pan != 0
6674 then Printf.bprintf bb " pan='%d'" pan;
6676 add_attrs bb false dc c;
6677 let kb = keymapsbuf false dc c in
6679 begin match bookmarks with
6680 | [] ->
6681 if Buffer.length kb > 0
6682 then (
6683 Buffer.add_string bb ">\n";
6684 Buffer.add_buffer bb kb;
6685 Buffer.add_string bb "\n</doc>\n";
6687 else Buffer.add_string bb "/>\n"
6688 | _ ->
6689 Buffer.add_string bb ">\n<bookmarks>\n";
6690 List.iter (fun (title, _level, (page, rely, visy)) ->
6691 Printf.bprintf bb
6692 "<item title='%s' page='%d'"
6693 (enent title 0 (String.length title))
6694 page
6696 if rely > 1e-6
6697 then
6698 Printf.bprintf bb " rely='%f'" rely
6700 if abs_float visy > 1e-6
6701 then
6702 Printf.bprintf bb " visy='%f'" visy
6704 Buffer.add_string bb "/>\n";
6705 ) bookmarks;
6706 Buffer.add_string bb "</bookmarks>";
6707 if Buffer.length kb > 0
6708 then (
6709 Buffer.add_string bb "\n";
6710 Buffer.add_buffer bb kb;
6712 Buffer.add_string bb "\n</doc>\n";
6713 end;
6717 let pan, conf =
6718 match state.mode with
6719 | Birdseye (c, pan, _, _, _) ->
6720 let beyecolumns =
6721 match conf.columns with
6722 | Cmulti ((c, _, _), _) -> Some c
6723 | Csingle _ -> None
6724 | Csplit _ -> None
6725 and columns =
6726 match c.columns with
6727 | Cmulti (c, _) -> Cmulti (c, [||])
6728 | Csingle _ -> Csingle [||]
6729 | Csplit _ -> failwith "quit from bird's eye while split"
6731 pan, { c with beyecolumns = beyecolumns; columns = columns }
6732 | _ -> state.x, conf
6734 let basename = Filename.basename state.path in
6735 adddoc basename pan (getanchor ())
6736 (let conf =
6737 let autoscrollstep =
6738 match state.autoscroll with
6739 | Some step -> step
6740 | None -> conf.autoscrollstep
6742 match state.mode with
6743 | Birdseye (bc, _, _, _, _) ->
6744 { conf with
6745 zoom = bc.zoom;
6746 presentation = bc.presentation;
6747 interpagespace = bc.interpagespace;
6748 maxwait = bc.maxwait;
6749 autoscrollstep = autoscrollstep }
6750 | _ -> { conf with autoscrollstep = autoscrollstep }
6751 in conf)
6752 (if conf.savebmarks then state.bookmarks else []);
6754 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6755 if basename <> path
6756 then adddoc path x anchor c bookmarks
6757 ) h;
6758 Buffer.add_string bb "</llppconfig>";
6760 load1 f;
6761 if Buffer.length bb > 0
6762 then
6764 let tmp = !confpath ^ ".tmp" in
6765 let oc = open_out_bin tmp in
6766 Buffer.output_buffer oc bb;
6767 close_out oc;
6768 Unix.rename tmp !confpath;
6769 with exn ->
6770 prerr_endline
6771 ("error while saving configuration: " ^ Printexc.to_string exn)
6773 end;;
6775 let () =
6776 let trimcachepath = ref "" in
6777 Arg.parse
6778 (Arg.align
6779 [("-p", Arg.String (fun s -> state.password <- s) ,
6780 "<password> Set password");
6782 ("-f", Arg.String (fun s -> Config.fontpath := s),
6783 "<path> Set path to the user interface font");
6785 ("-c", Arg.String (fun s -> Config.confpath := s),
6786 "<path> Set path to the configuration file");
6788 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6789 "<path> Set path to the trim cache file");
6791 ("-v", Arg.Unit (fun () ->
6792 Printf.printf
6793 "%s\nconfiguration path: %s\n"
6794 (version ())
6795 Config.defconfpath
6797 exit 0), " Print version and exit");
6800 (fun s -> state.path <- s)
6801 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6803 if String.length state.path = 0
6804 then (prerr_endline "file name missing"; exit 1);
6806 Config.load ();
6808 let globalkeyhash = findkeyhash conf "global" in
6809 let wsfd, winw, winh = Wsi.init (object
6810 method expose =
6811 if nogeomcmds state.geomcmds || platform == Posx
6812 then display ()
6813 else (
6814 GlClear.color (scalecolor2 conf.bgcolor);
6815 GlClear.clear [`color];
6817 method display = display ()
6818 method reshape w h = reshape w h
6819 method mouse b d x y m = mouse b d x y m
6820 method motion x y = state.mpos <- (x, y); motion x y
6821 method pmotion x y = state.mpos <- (x, y); pmotion x y
6822 method key k m =
6823 let mascm = m land (
6824 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6825 ) in
6826 match state.keystate with
6827 | KSnone ->
6828 let km = k, mascm in
6829 begin
6830 match
6831 let modehash = state.uioh#modehash in
6832 try Hashtbl.find modehash km
6833 with Not_found ->
6834 try Hashtbl.find globalkeyhash km
6835 with Not_found -> KMinsrt (k, m)
6836 with
6837 | KMinsrt (k, m) -> keyboard k m
6838 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6839 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6841 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6842 List.iter (fun (k, m) -> keyboard k m) insrt;
6843 state.keystate <- KSnone
6844 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6845 state.keystate <- KSinto (keys, insrt)
6846 | _ ->
6847 state.keystate <- KSnone
6849 method enter x y = state.mpos <- (x, y); pmotion x y
6850 method leave = state.mpos <- (-1, -1)
6851 method quit = raise Quit
6852 end) conf.winw conf.winh (platform = Posx) in
6854 state.wsfd <- wsfd;
6856 if not (
6857 List.exists GlMisc.check_extension
6858 [ "GL_ARB_texture_rectangle"
6859 ; "GL_EXT_texture_recangle"
6860 ; "GL_NV_texture_rectangle" ]
6862 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6864 let cr, sw =
6865 match Ne.pipe () with
6866 | Ne.Exn exn ->
6867 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6868 exit 1
6869 | Ne.Res rw -> rw
6870 and sr, cw =
6871 match Ne.pipe () with
6872 | Ne.Exn exn ->
6873 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6874 exit 1
6875 | Ne.Res rw -> rw
6878 cloexec cr;
6879 cloexec sw;
6880 cloexec sr;
6881 cloexec cw;
6883 setcheckers conf.checkers;
6884 redirectstderr ();
6886 init (cr, cw) (
6887 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6888 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6889 !Config.fontpath, !trimcachepath
6891 state.sr <- sr;
6892 state.sw <- sw;
6893 state.text <- "Opening " ^ state.path;
6894 reshape winw winh;
6895 opendoc state.path state.password;
6896 state.uioh <- uioh;
6898 let rec loop deadline =
6899 let r =
6900 match state.errfd with
6901 | None -> [state.sr; state.wsfd]
6902 | Some fd -> [state.sr; state.wsfd; fd]
6904 if state.redisplay
6905 then (
6906 state.redisplay <- false;
6907 display ();
6909 let timeout =
6910 let now = now () in
6911 if deadline > now
6912 then (
6913 if deadline = infinity
6914 then ~-.1.0
6915 else max 0.0 (deadline -. now)
6917 else 0.0
6919 let r, _, _ =
6920 try Unix.select r [] [] timeout
6921 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6923 begin match r with
6924 | [] ->
6925 state.ghyll None;
6926 let newdeadline =
6927 if state.ghyll == noghyll
6928 then
6929 match state.autoscroll with
6930 | Some step when step != 0 ->
6931 let y = state.y + step in
6932 let y =
6933 if y < 0
6934 then state.maxy
6935 else if y >= state.maxy then 0 else y
6937 gotoy y;
6938 if state.mode = View
6939 then state.text <- "";
6940 deadline +. 0.01
6941 | _ -> infinity
6942 else deadline +. 0.01
6944 loop newdeadline
6946 | l ->
6947 let rec checkfds = function
6948 | [] -> ()
6949 | fd :: rest when fd = state.sr ->
6950 let cmd = readcmd state.sr in
6951 act cmd;
6952 checkfds rest
6954 | fd :: rest when fd = state.wsfd ->
6955 Wsi.readresp fd;
6956 checkfds rest
6958 | fd :: rest ->
6959 let s = String.create 80 in
6960 let n = Unix.read fd s 0 80 in
6961 if conf.redirectstderr
6962 then (
6963 Buffer.add_substring state.errmsgs s 0 n;
6964 state.newerrmsgs <- true;
6965 state.redisplay <- true;
6967 else (
6968 prerr_string (String.sub s 0 n);
6969 flush stderr;
6971 checkfds rest
6973 checkfds l;
6974 let newdeadline =
6975 let deadline1 =
6976 if deadline = infinity
6977 then now () +. 0.01
6978 else deadline
6980 match state.autoscroll with
6981 | Some step when step != 0 -> deadline1
6982 | _ -> if state.ghyll == noghyll then infinity else deadline1
6984 loop newdeadline
6985 end;
6988 loop infinity;
6989 with Quit ->
6990 Config.save ();