Fix width/height position in pdim tuple mixup
[llpp.git] / main.ml
blob52800addd4fd838b7c8a4aa7250ec9eb96aa91fa
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 layoutN ((columns, coverA, coverB), b) y sh =
1101 let sh = sh - state.hscrollh in
1102 let rec fold accu n =
1103 if n = Array.length b
1104 then accu
1105 else
1106 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1107 if (vy - y) > sh &&
1108 (n = coverA - 1
1109 || n = state.pagecount - coverB
1110 || (n - coverA) mod columns = columns - 1)
1111 then accu
1112 else
1113 let accu =
1114 if vy + h > y
1115 then
1116 let pagey = max 0 (y - vy) in
1117 let pagedispy = if pagey > 0 then 0 else vy - y in
1118 let pagedispx, pagex =
1119 let pdx =
1120 if n = coverA - 1 || n = state.pagecount - coverB
1121 then state.x + (conf.winw - state.scrollw - w) / 2
1122 else dx + xoff + state.x
1124 if pdx < 0
1125 then 0, -pdx
1126 else pdx, 0
1128 let pagevw =
1129 let vw = conf.winw - state.scrollw - pagedispx in
1130 let pw = w - pagex in
1131 min vw pw
1133 let pagevh = min (h - pagey) (sh - pagedispy) in
1134 if pagevw > 0 && pagevh > 0
1135 then
1136 let e =
1137 { pageno = n
1138 ; pagedimno = pdimno
1139 ; pagew = w
1140 ; pageh = h
1141 ; pagex = pagex
1142 ; pagey = pagey
1143 ; pagevw = pagevw
1144 ; pagevh = pagevh
1145 ; pagedispx = pagedispx
1146 ; pagedispy = pagedispy
1147 ; pagecol = 0
1150 e :: accu
1151 else
1152 accu
1153 else
1154 accu
1156 fold accu (n+1)
1158 List.rev (fold [] 0);
1161 let layoutS (columns, b) y sh =
1162 let sh = sh - state.hscrollh in
1163 let rec fold accu n =
1164 if n = Array.length b
1165 then accu
1166 else
1167 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1168 if (vy - y) > sh
1169 then accu
1170 else
1171 let accu =
1172 if vy + pageh > y
1173 then
1174 let x = xoff + state.x in
1175 let pagey = max 0 (y - vy) in
1176 let pagedispy = if pagey > 0 then 0 else vy - y in
1177 let pagedispx, pagex =
1178 if px = 0
1179 then (
1180 if x < 0
1181 then 0, -x
1182 else x, 0
1184 else (
1185 let px = px - x in
1186 if px < 0
1187 then -px, 0
1188 else 0, px
1191 let pagecolw = pagew/columns in
1192 let pagedispx =
1193 if pagecolw < conf.winw
1194 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1195 else pagedispx
1197 let pagevw =
1198 let vw = conf.winw - pagedispx - state.scrollw in
1199 let pw = pagew - pagex in
1200 min vw pw
1202 let pagevw = min pagevw pagecolw in
1203 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1204 if pagevw > 0 && pagevh > 0
1205 then
1206 let e =
1207 { pageno = n/columns
1208 ; pagedimno = pdimno
1209 ; pagew = pagew
1210 ; pageh = pageh
1211 ; pagex = pagex
1212 ; pagey = pagey
1213 ; pagevw = pagevw
1214 ; pagevh = pagevh
1215 ; pagedispx = pagedispx
1216 ; pagedispy = pagedispy
1217 ; pagecol = n mod columns
1220 e :: accu
1221 else
1222 accu
1223 else
1224 accu
1226 fold accu (n+1)
1228 List.rev (fold [] 0)
1231 let layout y sh =
1232 if nogeomcmds state.geomcmds
1233 then
1234 match conf.columns with
1235 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1236 | Cmulti c -> layoutN c y sh
1237 | Csplit s -> layoutS s y sh
1238 else []
1241 let clamp incr =
1242 let y = state.y + incr in
1243 let y = max 0 y in
1244 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1248 let itertiles l f =
1249 let tilex = l.pagex mod conf.tilew in
1250 let tiley = l.pagey mod conf.tileh in
1252 let col = l.pagex / conf.tilew in
1253 let row = l.pagey / conf.tileh in
1255 let rec rowloop row y0 dispy h =
1256 if h = 0
1257 then ()
1258 else (
1259 let dh = conf.tileh - y0 in
1260 let dh = min h dh in
1261 let rec colloop col x0 dispx w =
1262 if w = 0
1263 then ()
1264 else (
1265 let dw = conf.tilew - x0 in
1266 let dw = min w dw in
1268 f col row dispx dispy x0 y0 dw dh;
1269 colloop (col+1) 0 (dispx+dw) (w-dw)
1272 colloop col tilex l.pagedispx l.pagevw;
1273 rowloop (row+1) 0 (dispy+dh) (h-dh)
1276 if l.pagevw > 0 && l.pagevh > 0
1277 then rowloop row tiley l.pagedispy l.pagevh;
1280 let gettileopaque l col row =
1281 let key =
1282 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1284 try Some (Hashtbl.find state.tilemap key)
1285 with Not_found -> None
1288 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1289 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1290 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1293 let drawtiles l color =
1294 GlDraw.color color;
1295 let f col row x y tilex tiley w h =
1296 match gettileopaque l col row with
1297 | Some (opaque, _, t) ->
1298 let params = x, y, w, h, tilex, tiley in
1299 if conf.invert
1300 then (
1301 Gl.enable `blend;
1302 GlFunc.blend_func `zero `one_minus_src_color;
1304 drawtile params opaque;
1305 if conf.invert
1306 then Gl.disable `blend;
1307 if conf.debug
1308 then (
1309 let s = Printf.sprintf
1310 "%d[%d,%d] %f sec"
1311 l.pageno col row t
1313 let w = measurestr fstate.fontsize s in
1314 GlMisc.push_attrib [`current];
1315 GlDraw.color (0.0, 0.0, 0.0);
1316 GlDraw.rect
1317 (float (x-2), float (y-2))
1318 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1319 GlDraw.color (1.0, 1.0, 1.0);
1320 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1321 GlMisc.pop_attrib ();
1324 | _ ->
1325 let w =
1326 let lw = conf.winw - state.scrollw - x in
1327 min lw w
1328 and h =
1329 let lh = conf.winh - y in
1330 min lh h
1332 begin match state.texid with
1333 | Some id ->
1334 Gl.enable `texture_2d;
1335 GlTex.bind_texture `texture_2d id;
1336 let x0 = float x
1337 and y0 = float y
1338 and x1 = float (x+w)
1339 and y1 = float (y+h) in
1341 let tw = float w /. 64.0
1342 and th = float h /. 64.0 in
1343 let tx0 = float tilex /. 64.0
1344 and ty0 = float tiley /. 64.0 in
1345 let tx1 = tx0 +. tw
1346 and ty1 = ty0 +. th in
1347 GlDraw.begins `quads;
1348 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1349 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1350 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1351 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1352 GlDraw.ends ();
1354 Gl.disable `texture_2d;
1355 | None ->
1356 GlDraw.color (1.0, 1.0, 1.0);
1357 GlDraw.rect
1358 (float x, float y)
1359 (float (x+w), float (y+h));
1360 end;
1361 if w > 128 && h > fstate.fontsize + 10
1362 then (
1363 GlDraw.color (0.0, 0.0, 0.0);
1364 let c, r =
1365 if conf.verbose
1366 then (col*conf.tilew, row*conf.tileh)
1367 else col, row
1369 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1371 GlDraw.color color;
1373 itertiles l f
1376 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1378 let tilevisible1 l x y =
1379 let ax0 = l.pagex
1380 and ax1 = l.pagex + l.pagevw
1381 and ay0 = l.pagey
1382 and ay1 = l.pagey + l.pagevh in
1384 let bx0 = x
1385 and by0 = y in
1386 let bx1 = min (bx0 + conf.tilew) l.pagew
1387 and by1 = min (by0 + conf.tileh) l.pageh in
1389 let rx0 = max ax0 bx0
1390 and ry0 = max ay0 by0
1391 and rx1 = min ax1 bx1
1392 and ry1 = min ay1 by1 in
1394 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1395 nonemptyintersection
1398 let tilevisible layout n x y =
1399 let rec findpageinlayout m = function
1400 | l :: rest when l.pageno = n ->
1401 tilevisible1 l x y || (
1402 match conf.columns with
1403 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1404 | _ -> false
1406 | _ :: rest -> findpageinlayout 0 rest
1407 | [] -> false
1409 findpageinlayout 0 layout;
1412 let tileready l x y =
1413 tilevisible1 l x y &&
1414 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1417 let tilepage n p layout =
1418 let rec loop = function
1419 | l :: rest ->
1420 if l.pageno = n
1421 then
1422 let f col row _ _ _ _ _ _ =
1423 if state.currently = Idle
1424 then
1425 match gettileopaque l col row with
1426 | Some _ -> ()
1427 | None ->
1428 let x = col*conf.tilew
1429 and y = row*conf.tileh in
1430 let w =
1431 let w = l.pagew - x in
1432 min w conf.tilew
1434 let h =
1435 let h = l.pageh - y in
1436 min h conf.tileh
1438 wcmd "tile %s %d %d %d %d" p x y w h;
1439 state.currently <-
1440 Tiling (
1441 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1442 conf.tilew, conf.tileh
1445 itertiles l f;
1446 else
1447 loop rest
1449 | [] -> ()
1451 if nogeomcmds state.geomcmds
1452 then loop layout;
1455 let page_of_y y =
1456 let b =
1457 match conf.columns with
1458 | Csingle b -> b
1459 | Cmulti (_, b) -> b
1460 | Csplit (_, b) -> b
1462 let rec bsearch nmin nmax =
1463 if nmin > nmax
1464 then -1
1465 else
1466 let n = (nmax + nmin) / 2 in
1467 let _, _, vy, (_, _, h, _) = b.(n) in
1468 let y0, y1 =
1469 if conf.presentation
1470 then
1471 let ips = calcips h in
1472 let y0 = vy - ips in
1473 let y1 = vy + h + ips in
1474 y0, y1
1475 else (
1476 if n = 0
1477 then 0, vy + h + conf.interpagespace
1478 else
1479 let y0 = vy - conf.interpagespace in
1480 y0, y0 + h
1483 if y >= y0 && y < y1
1484 then n
1485 else (
1486 if y > y0
1487 then bsearch (n+1) nmax
1488 else bsearch nmin (n-1)
1491 let r = bsearch 0 (state.pagecount-1) in
1495 let preloadlayout y =
1496 let y = if y < conf.winh then 0 else y - conf.winh in
1497 let h = conf.winh*3 in
1498 layout y h;
1501 let load pages =
1502 let rec loop pages =
1503 if state.currently != Idle
1504 then ()
1505 else
1506 match pages with
1507 | l :: rest ->
1508 begin match getopaque l.pageno with
1509 | None ->
1510 wcmd "page %d %d" l.pageno l.pagedimno;
1511 state.currently <- Loading (l, state.gen);
1512 | Some opaque ->
1513 tilepage l.pageno opaque pages;
1514 loop rest
1515 end;
1516 | _ -> ()
1518 if nogeomcmds state.geomcmds
1519 then loop pages
1522 let preload pages =
1523 load pages;
1524 if conf.preload && state.currently = Idle
1525 then load (preloadlayout state.y);
1528 let layoutready layout =
1529 let rec fold all ls =
1530 all && match ls with
1531 | l :: rest ->
1532 let seen = ref false in
1533 let allvisible = ref true in
1534 let foo col row _ _ _ _ _ _ =
1535 seen := true;
1536 allvisible := !allvisible &&
1537 begin match gettileopaque l col row with
1538 | Some _ -> true
1539 | None -> false
1542 itertiles l foo;
1543 fold (!seen && !allvisible) rest
1544 | [] -> true
1546 let alltilesvisible = fold true layout in
1547 alltilesvisible;
1550 let gotoy y =
1551 let y = bound y 0 state.maxy in
1552 let y, layout, proceed =
1553 match conf.maxwait with
1554 | Some time when state.ghyll == noghyll ->
1555 begin match state.throttle with
1556 | None ->
1557 let layout = layout y conf.winh in
1558 let ready = layoutready layout in
1559 if not ready
1560 then (
1561 load layout;
1562 state.throttle <- Some (layout, y, now ());
1564 else G.postRedisplay "gotoy showall (None)";
1565 y, layout, ready
1566 | Some (_, _, started) ->
1567 let dt = now () -. started in
1568 if dt > time
1569 then (
1570 state.throttle <- None;
1571 let layout = layout y conf.winh in
1572 load layout;
1573 G.postRedisplay "maxwait";
1574 y, layout, true
1576 else -1, [], false
1579 | _ ->
1580 let layout = layout y conf.winh in
1581 if true || layoutready layout
1582 then G.postRedisplay "gotoy ready";
1583 y, layout, true
1585 if proceed
1586 then (
1587 state.y <- y;
1588 state.layout <- layout;
1589 begin match state.mode with
1590 | LinkNav (Ltexact (pageno, linkno)) ->
1591 let rec loop = function
1592 | [] ->
1593 state.mode <- LinkNav (Ltgendir 0)
1594 | l :: _ when l.pageno = pageno ->
1595 begin match getopaque pageno with
1596 | None ->
1597 state.mode <- LinkNav (Ltgendir 0)
1598 | Some opaque ->
1599 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1600 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1601 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1602 then state.mode <- LinkNav (Ltgendir 0)
1604 | _ :: rest -> loop rest
1606 loop layout
1607 | _ -> ()
1608 end;
1609 begin match state.mode with
1610 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1611 if not (pagevisible layout pageno)
1612 then (
1613 match state.layout with
1614 | [] -> ()
1615 | l :: _ ->
1616 state.mode <- Birdseye (
1617 conf, leftx, l.pageno, hooverpageno, anchor
1620 | LinkNav (Ltgendir dir as lt) ->
1621 let linknav =
1622 let rec loop = function
1623 | [] -> lt
1624 | l :: rest ->
1625 match getopaque l.pageno with
1626 | None -> loop rest
1627 | Some opaque ->
1628 let link =
1629 let ld =
1630 if dir = 0
1631 then LDfirstvisible (l.pagex, l.pagey, dir)
1632 else (
1633 if dir > 0 then LDfirst else LDlast
1636 findlink opaque ld
1638 match link with
1639 | Lnotfound -> loop rest
1640 | Lfound n ->
1641 showlinktype (getlink opaque n);
1642 Ltexact (l.pageno, n)
1644 loop state.layout
1646 state.mode <- LinkNav linknav
1647 | _ -> ()
1648 end;
1649 preload layout;
1651 state.ghyll <- noghyll;
1652 if conf.updatecurs
1653 then (
1654 let mx, my = state.mpos in
1655 updateunder mx my;
1659 let conttiling pageno opaque =
1660 tilepage pageno opaque
1661 (if conf.preload then preloadlayout state.y else state.layout)
1664 let gotoy_and_clear_text y =
1665 if not conf.verbose then state.text <- "";
1666 gotoy y;
1669 let getanchor1 l =
1670 let top =
1671 let coloff = l.pagecol * l.pageh in
1672 float (l.pagey + coloff) /. float l.pageh
1674 let dtop =
1675 if l.pagedispy = 0
1676 then
1678 else
1679 if conf.presentation
1680 then float l.pagedispy /. float (calcips l.pageh)
1681 else float l.pagedispy /. float conf.interpagespace
1683 (l.pageno, top, dtop)
1686 let getanchor () =
1687 match state.layout with
1688 | l :: _ -> getanchor1 l
1689 | [] ->
1690 let n = page_of_y state.y in
1691 let y, h = getpageyh n in
1692 let dy = y - state.y in
1693 let dtop =
1694 if conf.presentation
1695 then
1696 let ips = calcips h in
1697 float (dy + ips) /. float ips
1698 else
1699 float dy /. float conf.interpagespace
1701 (n, 0.0, dtop)
1704 let getanchory (n, top, dtop) =
1705 let y, h = getpageyh n in
1706 if conf.presentation
1707 then
1708 let ips = calcips h in
1709 y + truncate (top*.float h -. dtop*.float ips) + ips;
1710 else
1711 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1714 let gotoanchor anchor =
1715 gotoy (getanchory anchor);
1718 let addnav () =
1719 cbput state.hists.nav (getanchor ());
1722 let getnav dir =
1723 let anchor = cbgetc state.hists.nav dir in
1724 getanchory anchor;
1727 let gotoghyll y =
1728 let scroll f n a b =
1729 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1730 let snake f a b =
1731 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1732 if f < a
1733 then s (float f /. float a)
1734 else (
1735 if f > b
1736 then 1.0 -. s ((float (f-b) /. float (n-b)))
1737 else 1.0
1740 snake f a b
1741 and summa f n a b =
1742 (* courtesy:
1743 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1744 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1745 let iv1 = iv f in
1746 let ins = float a *. iv1
1747 and outs = float (n-b) *. iv1 in
1748 let ones = b - a in
1749 ins +. outs +. float ones
1751 let rec set (_N, _A, _B) y sy =
1752 let sum = summa 1.0 _N _A _B in
1753 let dy = float (y - sy) in
1754 state.ghyll <- (
1755 let rec gf n y1 o =
1756 if n >= _N
1757 then state.ghyll <- noghyll
1758 else
1759 let go n =
1760 let s = scroll n _N _A _B in
1761 let y1 = y1 +. ((s *. dy) /. sum) in
1762 gotoy_and_clear_text (truncate y1);
1763 state.ghyll <- gf (n+1) y1;
1765 match o with
1766 | None -> go n
1767 | Some y' -> set (_N/2, 0, 0) y' state.y
1769 gf 0 (float state.y)
1772 match conf.ghyllscroll with
1773 | None ->
1774 gotoy_and_clear_text y
1775 | Some nab ->
1776 if state.ghyll == noghyll
1777 then set nab y state.y
1778 else state.ghyll (Some y)
1781 let gotopage n top =
1782 let y, h = getpageyh n in
1783 let y = y + (truncate (top *. float h)) in
1784 gotoghyll y
1787 let gotopage1 n top =
1788 let y = getpagey n in
1789 let y = y + top in
1790 gotoghyll y
1793 let invalidate s f =
1794 state.layout <- [];
1795 state.pdims <- [];
1796 state.rects <- [];
1797 state.rects1 <- [];
1798 match state.geomcmds with
1799 | ps, [] when String.length ps = 0 ->
1800 f ();
1801 state.geomcmds <- s, [];
1803 | ps, [] ->
1804 state.geomcmds <- ps, [s, f];
1806 | ps, (s', _) :: rest when s' = s ->
1807 state.geomcmds <- ps, ((s, f) :: rest);
1809 | ps, cmds ->
1810 state.geomcmds <- ps, ((s, f) :: cmds);
1813 let opendoc path password =
1814 state.path <- path;
1815 state.password <- password;
1816 state.gen <- state.gen + 1;
1817 state.docinfo <- [];
1819 setaalevel conf.aalevel;
1820 Wsi.settitle ("llpp " ^ Filename.basename path);
1821 wcmd "open %s\000%s\000" path password;
1822 invalidate "reqlayout"
1823 (fun () ->
1824 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1827 let scalecolor c =
1828 let c = c *. conf.colorscale in
1829 (c, c, c);
1832 let scalecolor2 (r, g, b) =
1833 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1836 let docolumns = function
1837 | Csingle _ ->
1838 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1839 let rec loop pageno pdimno pdim y ph pdims =
1840 if pageno = state.pagecount
1841 then ()
1842 else
1843 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1844 match pdims with
1845 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1846 pdimno+1, pdim, rest
1847 | _ ->
1848 pdimno, pdim, pdims
1850 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1851 let y = y +
1852 (if conf.presentation
1853 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1854 else (if pageno = 0 then 0 else calcips h)
1857 a.(pageno) <- (pdimno, x, y, pdim);
1858 loop (pageno+1) pdimno pdim (y + h) h pdims
1860 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1861 conf.columns <- Csingle a;
1863 | Cmulti ((columns, coverA, coverB), _) ->
1864 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1865 let rec loop pageno pdimno pdim x y rowh pdims =
1866 let rec fixrow m = if m = pageno then () else
1867 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1868 if h < rowh
1869 then (
1870 let y = y + (rowh - h) / 2 in
1871 a.(m) <- (pdimno, x, y, pdim);
1873 fixrow (m+1)
1875 if pageno = state.pagecount
1876 then fixrow (((pageno - 1) / columns) * columns)
1877 else
1878 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1879 match pdims with
1880 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1881 pdimno+1, pdim, rest
1882 | _ ->
1883 pdimno, pdim, pdims
1885 let x, y, rowh' =
1886 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1887 then (
1888 (conf.winw - state.scrollw - w) / 2,
1889 y + rowh + conf.interpagespace, h
1891 else (
1892 if (pageno - coverA) mod columns = 0
1893 then (
1894 (if conf.multicenter then
1895 (conf.winw - state.scrollw - state.w) / 2 else 0),
1896 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1898 else x, y, max rowh h
1901 if pageno > 1 && (pageno - coverA) mod columns = 0
1902 then fixrow (pageno - columns);
1903 a.(pageno) <- (pdimno, x, y, pdim);
1904 let x = x + w + xoff*2 + conf.interpagespace in
1905 loop (pageno+1) pdimno pdim x y rowh' pdims
1907 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1908 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1910 | Csplit (c, _) ->
1911 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1912 let rec loop pageno pdimno pdim y pdims =
1913 if pageno = state.pagecount
1914 then ()
1915 else
1916 let pdimno, ((_, w, h, _) as pdim), pdims =
1917 match pdims with
1918 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1919 pdimno+1, pdim, rest
1920 | _ ->
1921 pdimno, pdim, pdims
1923 let cw = w / c in
1924 let rec loop1 n x y =
1925 if n = c then y else (
1926 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1927 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1930 let y = loop1 0 0 y in
1931 loop (pageno+1) pdimno pdim y pdims
1933 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1934 conf.columns <- Csplit (c, a);
1937 let represent () =
1938 docolumns conf.columns;
1939 state.maxy <- calcheight ();
1940 state.hscrollh <-
1941 if state.w <= conf.winw - state.scrollw
1942 then 0
1943 else state.scrollw
1945 match state.mode with
1946 | Birdseye (_, _, pageno, _, _) ->
1947 let y, h = getpageyh pageno in
1948 let top = (conf.winh - h) / 2 in
1949 gotoy (max 0 (y - top))
1950 | _ -> gotoanchor state.anchor
1953 let reshape w h =
1954 GlDraw.viewport 0 0 w h;
1955 let firsttime = state.geomcmds == firstgeomcmds in
1956 if not firsttime && nogeomcmds state.geomcmds
1957 then state.anchor <- getanchor ();
1959 conf.winw <- w;
1960 let w = truncate (float w *. conf.zoom) - state.scrollw in
1961 let w = max w 2 in
1962 conf.winh <- h;
1963 setfontsize fstate.fontsize;
1964 GlMat.mode `modelview;
1965 GlMat.load_identity ();
1967 GlMat.mode `projection;
1968 GlMat.load_identity ();
1969 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1970 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1971 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1973 let relx =
1974 if conf.zoom <= 1.0
1975 then 0.0
1976 else float state.x /. float state.w
1978 invalidate "geometry"
1979 (fun () ->
1980 state.w <- w;
1981 if not firsttime
1982 then state.x <- truncate (relx *. float w);
1983 let w =
1984 match conf.columns with
1985 | Csingle _ -> w
1986 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1987 | Csplit (c, _) -> w * c
1989 wcmd "geometry %d %d" w h);
1992 let enttext () =
1993 let len = String.length state.text in
1994 let drawstring s =
1995 let hscrollh =
1996 match state.mode with
1997 | Textentry _
1998 | View ->
1999 let h, _, _ = state.uioh#scrollpw in
2001 | _ -> 0
2003 let rect x w =
2004 GlDraw.rect
2005 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2006 (x+.w, float (conf.winh - hscrollh))
2009 let w = float (conf.winw - state.scrollw - 1) in
2010 if state.progress >= 0.0 && state.progress < 1.0
2011 then (
2012 GlDraw.color (0.3, 0.3, 0.3);
2013 let w1 = w *. state.progress in
2014 rect 0.0 w1;
2015 GlDraw.color (0.0, 0.0, 0.0);
2016 rect w1 (w-.w1)
2018 else (
2019 GlDraw.color (0.0, 0.0, 0.0);
2020 rect 0.0 w;
2023 GlDraw.color (1.0, 1.0, 1.0);
2024 drawstring fstate.fontsize
2025 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2027 let s =
2028 match state.mode with
2029 | Textentry ((prefix, text, _, _, _, _), _) ->
2030 let s =
2031 if len > 0
2032 then
2033 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2034 else
2035 Printf.sprintf "%s%s_" prefix text
2039 | _ -> state.text
2041 let s =
2042 if state.newerrmsgs
2043 then (
2044 if not (istextentry state.mode)
2045 then
2046 let s1 = "(press 'e' to review error messasges)" in
2047 if String.length s > 0 then s ^ " " ^ s1 else s1
2048 else s
2050 else s
2052 if String.length s > 0
2053 then drawstring s
2056 let gctiles () =
2057 let len = Queue.length state.tilelru in
2058 let layout = lazy (
2059 match state.throttle with
2060 | None ->
2061 if conf.preload
2062 then preloadlayout state.y
2063 else state.layout
2064 | Some (layout, _, _) ->
2065 layout
2066 ) in
2067 let rec loop qpos =
2068 if state.memused <= conf.memlimit
2069 then ()
2070 else (
2071 if qpos < len
2072 then
2073 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2074 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2075 let (_, pw, ph, _) = getpagedim n in
2077 gen = state.gen
2078 && colorspace = conf.colorspace
2079 && angle = conf.angle
2080 && pagew = pw
2081 && pageh = ph
2082 && (
2083 let x = col*conf.tilew
2084 and y = row*conf.tileh in
2085 tilevisible (Lazy.force_val layout) n x y
2087 then Queue.push lruitem state.tilelru
2088 else (
2089 wcmd "freetile %s" p;
2090 state.memused <- state.memused - s;
2091 state.uioh#infochanged Memused;
2092 Hashtbl.remove state.tilemap k;
2094 loop (qpos+1)
2097 loop 0
2100 let flushtiles () =
2101 Queue.iter (fun (k, p, s) ->
2102 wcmd "freetile %s" p;
2103 state.memused <- state.memused - s;
2104 state.uioh#infochanged Memused;
2105 Hashtbl.remove state.tilemap k;
2106 ) state.tilelru;
2107 Queue.clear state.tilelru;
2108 load state.layout;
2111 let logcurrently = function
2112 | Idle -> dolog "Idle"
2113 | Loading (l, gen) ->
2114 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2115 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2116 dolog
2117 "Tiling %d[%d,%d] page=%s cs=%s angle"
2118 l.pageno col row pageopaque
2119 (colorspace_to_string colorspace)
2121 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2122 angle gen conf.angle state.gen
2123 tilew tileh
2124 conf.tilew conf.tileh
2126 | Outlining _ ->
2127 dolog "outlining"
2130 let act cmds =
2131 (* dolog "%S" cmds; *)
2132 let op, args =
2133 let spacepos =
2134 try String.index cmds ' '
2135 with Not_found -> -1
2137 if spacepos = -1
2138 then cmds, ""
2139 else
2140 let l = String.length cmds in
2141 let op = String.sub cmds 0 spacepos in
2142 op, begin
2143 if l - spacepos < 2 then ""
2144 else String.sub cmds (spacepos+1) (l-spacepos-1)
2147 match op with
2148 | "clear" ->
2149 state.uioh#infochanged Pdim;
2150 state.pdims <- [];
2152 | "clearrects" ->
2153 state.rects <- state.rects1;
2154 G.postRedisplay "clearrects";
2156 | "continue" ->
2157 let n =
2158 try Scanf.sscanf args "%u" (fun n -> n)
2159 with exn ->
2160 dolog "error processing 'continue' %S: %s"
2161 cmds (Printexc.to_string exn);
2162 exit 1;
2164 state.pagecount <- n;
2165 begin match state.currently with
2166 | Outlining l ->
2167 state.currently <- Idle;
2168 state.outlines <- Array.of_list (List.rev l)
2169 | _ -> ()
2170 end;
2172 let cur, cmds = state.geomcmds in
2173 if String.length cur = 0
2174 then failwith "umpossible";
2176 begin match List.rev cmds with
2177 | [] ->
2178 state.geomcmds <- "", [];
2179 represent ();
2180 | (s, f) :: rest ->
2181 f ();
2182 state.geomcmds <- s, List.rev rest;
2183 end;
2184 if conf.maxwait = None
2185 then G.postRedisplay "continue";
2187 | "title" ->
2188 Wsi.settitle args
2190 | "msg" ->
2191 showtext ' ' args
2193 | "vmsg" ->
2194 if conf.verbose
2195 then showtext ' ' args
2197 | "progress" ->
2198 let progress, text =
2200 Scanf.sscanf args "%f %n"
2201 (fun f pos ->
2202 f, String.sub args pos (String.length args - pos))
2203 with exn ->
2204 dolog "error processing 'progress' %S: %s"
2205 cmds (Printexc.to_string exn);
2206 exit 1;
2208 state.text <- text;
2209 state.progress <- progress;
2210 G.postRedisplay "progress"
2212 | "firstmatch" ->
2213 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2215 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2216 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2217 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2218 with exn ->
2219 dolog "error processing 'firstmatch' %S: %s"
2220 cmds (Printexc.to_string exn);
2221 exit 1;
2223 let y = (getpagey pageno) + truncate y0 in
2224 addnav ();
2225 gotoy y;
2226 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2228 | "match" ->
2229 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2231 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2232 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2233 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2234 with exn ->
2235 dolog "error processing 'match' %S: %s"
2236 cmds (Printexc.to_string exn);
2237 exit 1;
2239 state.rects1 <-
2240 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2242 | "page" ->
2243 let pageopaque, t =
2245 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2246 with exn ->
2247 dolog "error processing 'page' %S: %s"
2248 cmds (Printexc.to_string exn);
2249 exit 1;
2251 begin match state.currently with
2252 | Loading (l, gen) ->
2253 vlog "page %d took %f sec" l.pageno t;
2254 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2255 begin match state.throttle with
2256 | None ->
2257 let preloadedpages =
2258 if conf.preload
2259 then preloadlayout state.y
2260 else state.layout
2262 let evict () =
2263 let module IntSet =
2264 Set.Make (struct type t = int let compare = (-) end) in
2265 let set =
2266 List.fold_left (fun s l -> IntSet.add l.pageno s)
2267 IntSet.empty preloadedpages
2269 let evictedpages =
2270 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2271 if not (IntSet.mem pageno set)
2272 then (
2273 wcmd "freepage %s" opaque;
2274 key :: accu
2276 else accu
2277 ) state.pagemap []
2279 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2281 evict ();
2282 state.currently <- Idle;
2283 if gen = state.gen
2284 then (
2285 tilepage l.pageno pageopaque state.layout;
2286 load state.layout;
2287 load preloadedpages;
2288 if pagevisible state.layout l.pageno
2289 && layoutready state.layout
2290 then G.postRedisplay "page";
2293 | Some (layout, _, _) ->
2294 state.currently <- Idle;
2295 tilepage l.pageno pageopaque layout;
2296 load state.layout
2297 end;
2299 | _ ->
2300 dolog "Inconsistent loading state";
2301 logcurrently state.currently;
2302 exit 1
2305 | "tile" ->
2306 let (x, y, opaque, size, t) =
2308 Scanf.sscanf args "%u %u %s %u %f"
2309 (fun x y p size t -> (x, y, p, size, t))
2310 with exn ->
2311 dolog "error processing 'tile' %S: %s"
2312 cmds (Printexc.to_string exn);
2313 exit 1;
2315 begin match state.currently with
2316 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2317 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2319 if tilew != conf.tilew || tileh != conf.tileh
2320 then (
2321 wcmd "freetile %s" opaque;
2322 state.currently <- Idle;
2323 load state.layout;
2325 else (
2326 puttileopaque l col row gen cs angle opaque size t;
2327 state.memused <- state.memused + size;
2328 state.uioh#infochanged Memused;
2329 gctiles ();
2330 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2331 opaque, size) state.tilelru;
2333 let layout =
2334 match state.throttle with
2335 | None -> state.layout
2336 | Some (layout, _, _) -> layout
2339 state.currently <- Idle;
2340 if gen = state.gen
2341 && conf.colorspace = cs
2342 && conf.angle = angle
2343 && tilevisible layout l.pageno x y
2344 then conttiling l.pageno pageopaque;
2346 begin match state.throttle with
2347 | None ->
2348 preload state.layout;
2349 if gen = state.gen
2350 && conf.colorspace = cs
2351 && conf.angle = angle
2352 && tilevisible state.layout l.pageno x y
2353 then G.postRedisplay "tile nothrottle";
2355 | Some (layout, y, _) ->
2356 let ready = layoutready layout in
2357 if ready
2358 then (
2359 state.y <- y;
2360 state.layout <- layout;
2361 state.throttle <- None;
2362 G.postRedisplay "throttle";
2364 else load layout;
2365 end;
2368 | _ ->
2369 dolog "Inconsistent tiling state";
2370 logcurrently state.currently;
2371 exit 1
2374 | "pdim" ->
2375 let pdim =
2377 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2378 with exn ->
2379 dolog "error processing 'pdim' %S: %s"
2380 cmds (Printexc.to_string exn);
2381 exit 1;
2383 state.uioh#infochanged Pdim;
2384 state.pdims <- pdim :: state.pdims
2386 | "o" ->
2387 let (l, n, t, h, pos) =
2389 Scanf.sscanf args "%u %u %d %u %n"
2390 (fun l n t h pos -> l, n, t, h, pos)
2391 with exn ->
2392 dolog "error processing 'o' %S: %s"
2393 cmds (Printexc.to_string exn);
2394 exit 1;
2396 let s = String.sub args pos (String.length args - pos) in
2397 let outline = (s, l, (n, float t /. float h, 0.0)) in
2398 begin match state.currently with
2399 | Outlining outlines ->
2400 state.currently <- Outlining (outline :: outlines)
2401 | Idle ->
2402 state.currently <- Outlining [outline]
2403 | currently ->
2404 dolog "invalid outlining state";
2405 logcurrently currently
2408 | "info" ->
2409 state.docinfo <- (1, args) :: state.docinfo
2411 | "infoend" ->
2412 state.uioh#infochanged Docinfo;
2413 state.docinfo <- List.rev state.docinfo
2415 | _ ->
2416 dolog "unknown cmd `%S'" cmds
2419 let onhist cb =
2420 let rc = cb.rc in
2421 let action = function
2422 | HCprev -> cbget cb ~-1
2423 | HCnext -> cbget cb 1
2424 | HCfirst -> cbget cb ~-(cb.rc)
2425 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2426 and cancel () = cb.rc <- rc
2427 in (action, cancel)
2430 let search pattern forward =
2431 if String.length pattern > 0
2432 then
2433 let pn, py =
2434 match state.layout with
2435 | [] -> 0, 0
2436 | l :: _ ->
2437 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2439 wcmd "search %d %d %d %d,%s\000"
2440 (btod conf.icase) pn py (btod forward) pattern;
2443 let intentry text key =
2444 let c =
2445 if key >= 32 && key < 127
2446 then Char.chr key
2447 else '\000'
2449 match c with
2450 | '0' .. '9' ->
2451 let text = addchar text c in
2452 TEcont text
2454 | _ ->
2455 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2456 TEcont text
2459 let linknentry text key =
2460 let c =
2461 if key >= 32 && key < 127
2462 then Char.chr key
2463 else '\000'
2465 match c with
2466 | 'a' .. 'z' ->
2467 let text = addchar text c in
2468 TEcont text
2470 | _ ->
2471 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2472 TEcont text
2475 let linkndone f s =
2476 if String.length s > 0
2477 then (
2478 let n =
2479 let l = String.length s in
2480 let rec loop pos n = if pos = l then n else
2481 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2482 loop (pos+1) (n*26 + m)
2483 in loop 0 0
2485 let rec loop n = function
2486 | [] -> ()
2487 | l :: rest ->
2488 match getopaque l.pageno with
2489 | None -> loop n rest
2490 | Some opaque ->
2491 let m = getlinkcount opaque in
2492 if n < m
2493 then (
2494 let under = getlink opaque n in
2495 f under
2497 else loop (n-m) rest
2499 loop n state.layout;
2503 let textentry text key =
2504 if key land 0xff00 = 0xff00
2505 then TEcont text
2506 else TEcont (text ^ Wsi.toutf8 key)
2509 let reqlayout angle proportional =
2510 match state.throttle with
2511 | None ->
2512 if nogeomcmds state.geomcmds
2513 then state.anchor <- getanchor ();
2514 conf.angle <- angle mod 360;
2515 if conf.angle != 0
2516 then (
2517 match state.mode with
2518 | LinkNav _ -> state.mode <- View
2519 | _ -> ()
2521 conf.proportional <- proportional;
2522 invalidate "reqlayout"
2523 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2524 | _ -> ()
2527 let settrim trimmargins trimfuzz =
2528 if nogeomcmds state.geomcmds
2529 then state.anchor <- getanchor ();
2530 conf.trimmargins <- trimmargins;
2531 conf.trimfuzz <- trimfuzz;
2532 let x0, y0, x1, y1 = trimfuzz in
2533 invalidate "settrim"
2534 (fun () ->
2535 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2536 Hashtbl.iter (fun _ opaque ->
2537 wcmd "freepage %s" opaque;
2538 ) state.pagemap;
2539 Hashtbl.clear state.pagemap;
2542 let setzoom zoom =
2543 match state.throttle with
2544 | None ->
2545 let zoom = max 0.01 zoom in
2546 if zoom <> conf.zoom
2547 then (
2548 state.prevzoom <- conf.zoom;
2549 conf.zoom <- zoom;
2550 reshape conf.winw conf.winh;
2551 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2554 | Some (layout, y, started) ->
2555 let time =
2556 match conf.maxwait with
2557 | None -> 0.0
2558 | Some t -> t
2560 let dt = now () -. started in
2561 if dt > time
2562 then (
2563 state.y <- y;
2564 load layout;
2568 let setcolumns mode columns coverA coverB =
2569 state.prevcolumns <- Some (conf.columns, conf.zoom);
2570 if columns < 0
2571 then (
2572 if isbirdseye mode
2573 then showtext '!' "split mode doesn't work in bird's eye"
2574 else (
2575 conf.columns <- Csplit (-columns, [||]);
2576 state.x <- 0;
2577 conf.zoom <- 1.0;
2580 else (
2581 if columns < 2
2582 then (
2583 conf.columns <- Csingle [||];
2584 state.x <- 0;
2585 setzoom 1.0;
2587 else (
2588 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2589 conf.zoom <- 1.0;
2592 reshape conf.winw conf.winh;
2595 let enterbirdseye () =
2596 let zoom = float conf.thumbw /. float conf.winw in
2597 let birdseyepageno =
2598 let cy = conf.winh / 2 in
2599 let fold = function
2600 | [] -> 0
2601 | l :: rest ->
2602 let rec fold best = function
2603 | [] -> best.pageno
2604 | l :: rest ->
2605 let d = cy - (l.pagedispy + l.pagevh/2)
2606 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2607 if abs d < abs dbest
2608 then fold l rest
2609 else best.pageno
2610 in fold l rest
2612 fold state.layout
2614 state.mode <- Birdseye (
2615 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2617 conf.zoom <- zoom;
2618 conf.presentation <- false;
2619 conf.interpagespace <- 10;
2620 conf.hlinks <- false;
2621 state.x <- 0;
2622 state.mstate <- Mnone;
2623 conf.maxwait <- None;
2624 conf.columns <- (
2625 match conf.beyecolumns with
2626 | Some c ->
2627 conf.zoom <- 1.0;
2628 Cmulti ((c, 0, 0), [||])
2629 | None -> Csingle [||]
2631 Wsi.setcursor Wsi.CURSOR_INHERIT;
2632 if conf.verbose
2633 then
2634 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2635 (100.0*.zoom)
2636 else
2637 state.text <- ""
2639 reshape conf.winw conf.winh;
2642 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2643 state.mode <- View;
2644 conf.zoom <- c.zoom;
2645 conf.presentation <- c.presentation;
2646 conf.interpagespace <- c.interpagespace;
2647 conf.maxwait <- c.maxwait;
2648 conf.hlinks <- c.hlinks;
2649 conf.beyecolumns <- (
2650 match conf.columns with
2651 | Cmulti ((c, _, _), _) -> Some c
2652 | Csingle _ -> None
2653 | Csplit _ -> failwith "leaving bird's eye split mode"
2655 conf.columns <- (
2656 match c.columns with
2657 | Cmulti (c, _) -> Cmulti (c, [||])
2658 | Csingle _ -> Csingle [||]
2659 | Csplit (c, _) -> Csplit (c, [||])
2661 state.x <- leftx;
2662 if conf.verbose
2663 then
2664 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2665 (100.0*.conf.zoom)
2667 reshape conf.winw conf.winh;
2668 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2671 let togglebirdseye () =
2672 match state.mode with
2673 | Birdseye vals -> leavebirdseye vals true
2674 | View -> enterbirdseye ()
2675 | _ -> ()
2678 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2679 let pageno = max 0 (pageno - incr) in
2680 let rec loop = function
2681 | [] -> gotopage1 pageno 0
2682 | l :: _ when l.pageno = pageno ->
2683 if l.pagedispy >= 0 && l.pagey = 0
2684 then G.postRedisplay "upbirdseye"
2685 else gotopage1 pageno 0
2686 | _ :: rest -> loop rest
2688 loop state.layout;
2689 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2692 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2693 let pageno = min (state.pagecount - 1) (pageno + incr) in
2694 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2695 let rec loop = function
2696 | [] ->
2697 let y, h = getpageyh pageno in
2698 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2699 gotoy (clamp dy)
2700 | l :: _ when l.pageno = pageno ->
2701 if l.pagevh != l.pageh
2702 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2703 else G.postRedisplay "downbirdseye"
2704 | _ :: rest -> loop rest
2706 loop state.layout
2709 let optentry mode _ key =
2710 let btos b = if b then "on" else "off" in
2711 if key >= 32 && key < 127
2712 then
2713 let c = Char.chr key in
2714 match c with
2715 | 's' ->
2716 let ondone s =
2717 try conf.scrollstep <- int_of_string s with exc ->
2718 state.text <- Printf.sprintf "bad integer `%s': %s"
2719 s (Printexc.to_string exc)
2721 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2723 | 'A' ->
2724 let ondone s =
2726 conf.autoscrollstep <- int_of_string s;
2727 if state.autoscroll <> None
2728 then state.autoscroll <- Some conf.autoscrollstep
2729 with exc ->
2730 state.text <- Printf.sprintf "bad integer `%s': %s"
2731 s (Printexc.to_string exc)
2733 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2735 | 'C' ->
2736 let ondone s =
2738 let n, a, b = multicolumns_of_string s in
2739 setcolumns mode n a b;
2740 with exc ->
2741 state.text <- Printf.sprintf "bad columns `%s': %s"
2742 s (Printexc.to_string exc)
2744 TEswitch ("columns: ", "", None, textentry, ondone, true)
2746 | 'Z' ->
2747 let ondone s =
2749 let zoom = float (int_of_string s) /. 100.0 in
2750 setzoom zoom
2751 with exc ->
2752 state.text <- Printf.sprintf "bad integer `%s': %s"
2753 s (Printexc.to_string exc)
2755 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2757 | 't' ->
2758 let ondone s =
2760 conf.thumbw <- bound (int_of_string s) 2 4096;
2761 state.text <-
2762 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2763 begin match mode with
2764 | Birdseye beye ->
2765 leavebirdseye beye false;
2766 enterbirdseye ();
2767 | _ -> ();
2769 with exc ->
2770 state.text <- Printf.sprintf "bad integer `%s': %s"
2771 s (Printexc.to_string exc)
2773 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2775 | 'R' ->
2776 let ondone s =
2777 match try
2778 Some (int_of_string s)
2779 with exc ->
2780 state.text <- Printf.sprintf "bad integer `%s': %s"
2781 s (Printexc.to_string exc);
2782 None
2783 with
2784 | Some angle -> reqlayout angle conf.proportional
2785 | None -> ()
2787 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2789 | 'i' ->
2790 conf.icase <- not conf.icase;
2791 TEdone ("case insensitive search " ^ (btos conf.icase))
2793 | 'p' ->
2794 conf.preload <- not conf.preload;
2795 gotoy state.y;
2796 TEdone ("preload " ^ (btos conf.preload))
2798 | 'v' ->
2799 conf.verbose <- not conf.verbose;
2800 TEdone ("verbose " ^ (btos conf.verbose))
2802 | 'd' ->
2803 conf.debug <- not conf.debug;
2804 TEdone ("debug " ^ (btos conf.debug))
2806 | 'h' ->
2807 conf.maxhfit <- not conf.maxhfit;
2808 state.maxy <- calcheight ();
2809 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2811 | 'c' ->
2812 conf.crophack <- not conf.crophack;
2813 TEdone ("crophack " ^ btos conf.crophack)
2815 | 'a' ->
2816 let s =
2817 match conf.maxwait with
2818 | None ->
2819 conf.maxwait <- Some infinity;
2820 "always wait for page to complete"
2821 | Some _ ->
2822 conf.maxwait <- None;
2823 "show placeholder if page is not ready"
2825 TEdone s
2827 | 'f' ->
2828 conf.underinfo <- not conf.underinfo;
2829 TEdone ("underinfo " ^ btos conf.underinfo)
2831 | 'P' ->
2832 conf.savebmarks <- not conf.savebmarks;
2833 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2835 | 'S' ->
2836 let ondone s =
2838 let pageno, py =
2839 match state.layout with
2840 | [] -> 0, 0
2841 | l :: _ ->
2842 l.pageno, l.pagey
2844 conf.interpagespace <- int_of_string s;
2845 docolumns conf.columns;
2846 state.maxy <- calcheight ();
2847 let y = getpagey pageno in
2848 gotoy (y + py)
2849 with exc ->
2850 state.text <- Printf.sprintf "bad integer `%s': %s"
2851 s (Printexc.to_string exc)
2853 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2855 | 'l' ->
2856 reqlayout conf.angle (not conf.proportional);
2857 TEdone ("proportional display " ^ btos conf.proportional)
2859 | 'T' ->
2860 settrim (not conf.trimmargins) conf.trimfuzz;
2861 TEdone ("trim margins " ^ btos conf.trimmargins)
2863 | 'I' ->
2864 conf.invert <- not conf.invert;
2865 TEdone ("invert colors " ^ btos conf.invert)
2867 | 'x' ->
2868 let ondone s =
2869 cbput state.hists.sel s;
2870 conf.selcmd <- s;
2872 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2873 textentry, ondone, true)
2875 | _ ->
2876 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2877 TEstop
2878 else
2879 TEcont state.text
2882 class type lvsource = object
2883 method getitemcount : int
2884 method getitem : int -> (string * int)
2885 method hasaction : int -> bool
2886 method exit :
2887 uioh:uioh ->
2888 cancel:bool ->
2889 active:int ->
2890 first:int ->
2891 pan:int ->
2892 qsearch:string ->
2893 uioh option
2894 method getactive : int
2895 method getfirst : int
2896 method getqsearch : string
2897 method setqsearch : string -> unit
2898 method getpan : int
2899 end;;
2901 class virtual lvsourcebase = object
2902 val mutable m_active = 0
2903 val mutable m_first = 0
2904 val mutable m_qsearch = ""
2905 val mutable m_pan = 0
2906 method getactive = m_active
2907 method getfirst = m_first
2908 method getqsearch = m_qsearch
2909 method getpan = m_pan
2910 method setqsearch s = m_qsearch <- s
2911 end;;
2913 let withoutlastutf8 s =
2914 let len = String.length s in
2915 if len = 0
2916 then s
2917 else
2918 let rec find pos =
2919 if pos = 0
2920 then pos
2921 else
2922 let b = Char.code s.[pos] in
2923 if b land 0b110000 = 0b11000000
2924 then find (pos-1)
2925 else pos-1
2927 let first =
2928 if Char.code s.[len-1] land 0x80 = 0
2929 then len-1
2930 else find (len-1)
2932 String.sub s 0 first;
2935 let textentrykeyboard
2936 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2937 let enttext te =
2938 state.mode <- Textentry (te, onleave);
2939 state.text <- "";
2940 enttext ();
2941 G.postRedisplay "textentrykeyboard enttext";
2943 let histaction cmd =
2944 match opthist with
2945 | None -> ()
2946 | Some (action, _) ->
2947 state.mode <- Textentry (
2948 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2950 G.postRedisplay "textentry histaction"
2952 match key with
2953 | 0xff08 -> (* backspace *)
2954 let s = withoutlastutf8 text in
2955 let len = String.length s in
2956 if cancelonempty && len = 0
2957 then (
2958 onleave Cancel;
2959 G.postRedisplay "textentrykeyboard after cancel";
2961 else (
2962 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2965 | 0xff0d ->
2966 ondone text;
2967 onleave Confirm;
2968 G.postRedisplay "textentrykeyboard after confirm"
2970 | 0xff52 -> histaction HCprev
2971 | 0xff54 -> histaction HCnext
2972 | 0xff50 -> histaction HCfirst
2973 | 0xff57 -> histaction HClast
2975 | 0xff1b -> (* escape*)
2976 if String.length text = 0
2977 then (
2978 begin match opthist with
2979 | None -> ()
2980 | Some (_, onhistcancel) -> onhistcancel ()
2981 end;
2982 onleave Cancel;
2983 state.text <- "";
2984 G.postRedisplay "textentrykeyboard after cancel2"
2986 else (
2987 enttext (c, "", opthist, onkey, ondone, cancelonempty)
2990 | 0xff9f | 0xffff -> () (* delete *)
2992 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2993 begin match onkey text key with
2994 | TEdone text ->
2995 ondone text;
2996 onleave Confirm;
2997 G.postRedisplay "textentrykeyboard after confirm2";
2999 | TEcont text ->
3000 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3002 | TEstop ->
3003 onleave Cancel;
3004 G.postRedisplay "textentrykeyboard after cancel3"
3006 | TEswitch te ->
3007 state.mode <- Textentry (te, onleave);
3008 G.postRedisplay "textentrykeyboard switch";
3009 end;
3011 | _ ->
3012 vlog "unhandled key %s" (Wsi.keyname key)
3015 let firstof first active =
3016 if first > active || abs (first - active) > fstate.maxrows - 1
3017 then max 0 (active - (fstate.maxrows/2))
3018 else first
3021 let calcfirst first active =
3022 if active > first
3023 then
3024 let rows = active - first in
3025 if rows > fstate.maxrows then active - fstate.maxrows else first
3026 else active
3029 let scrollph y maxy =
3030 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3031 let sh = float conf.winh /. sh in
3032 let sh = max sh (float conf.scrollh) in
3034 let percent =
3035 if y = state.maxy
3036 then 1.0
3037 else float y /. float maxy
3039 let position = (float conf.winh -. sh) *. percent in
3041 let position =
3042 if position +. sh > float conf.winh
3043 then float conf.winh -. sh
3044 else position
3046 position, sh;
3049 let coe s = (s :> uioh);;
3051 class listview ~(source:lvsource) ~trusted ~modehash =
3052 object (self)
3053 val m_pan = source#getpan
3054 val m_first = source#getfirst
3055 val m_active = source#getactive
3056 val m_qsearch = source#getqsearch
3057 val m_prev_uioh = state.uioh
3059 method private elemunder y =
3060 let n = y / (fstate.fontsize+1) in
3061 if m_first + n < source#getitemcount
3062 then (
3063 if source#hasaction (m_first + n)
3064 then Some (m_first + n)
3065 else None
3067 else None
3069 method display =
3070 Gl.enable `blend;
3071 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3072 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3073 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3074 GlDraw.color (1., 1., 1.);
3075 Gl.enable `texture_2d;
3076 let fs = fstate.fontsize in
3077 let nfs = fs + 1 in
3078 let ww = fstate.wwidth in
3079 let tabw = 30.0*.ww in
3080 let itemcount = source#getitemcount in
3081 let rec loop row =
3082 if (row - m_first) * nfs > conf.winh
3083 then ()
3084 else (
3085 if row >= 0 && row < itemcount
3086 then (
3087 let (s, level) = source#getitem row in
3088 let y = (row - m_first) * nfs in
3089 let x = 5.0 +. float (level + m_pan) *. ww in
3090 if row = m_active
3091 then (
3092 Gl.disable `texture_2d;
3093 GlDraw.polygon_mode `both `line;
3094 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3095 GlDraw.rect (1., float (y + 1))
3096 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3097 GlDraw.polygon_mode `both `fill;
3098 GlDraw.color (1., 1., 1.);
3099 Gl.enable `texture_2d;
3102 let drawtabularstring s =
3103 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3104 if trusted
3105 then
3106 let tabpos = try String.index s '\t' with Not_found -> -1 in
3107 if tabpos > 0
3108 then
3109 let len = String.length s - tabpos - 1 in
3110 let s1 = String.sub s 0 tabpos
3111 and s2 = String.sub s (tabpos + 1) len in
3112 let nx = drawstr x s1 in
3113 let sw = nx -. x in
3114 let x = x +. (max tabw sw) in
3115 drawstr x s2
3116 else
3117 drawstr x s
3118 else
3119 drawstr x s
3121 let _ = drawtabularstring s in
3122 loop (row+1)
3126 loop m_first;
3127 Gl.disable `blend;
3128 Gl.disable `texture_2d;
3130 method updownlevel incr =
3131 let len = source#getitemcount in
3132 let curlevel =
3133 if m_active >= 0 && m_active < len
3134 then snd (source#getitem m_active)
3135 else -1
3137 let rec flow i =
3138 if i = len then i-1 else if i = -1 then 0 else
3139 let _, l = source#getitem i in
3140 if l != curlevel then i else flow (i+incr)
3142 let active = flow m_active in
3143 let first = calcfirst m_first active in
3144 G.postRedisplay "outline updownlevel";
3145 {< m_active = active; m_first = first >}
3147 method private key1 key mask =
3148 let set1 active first qsearch =
3149 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3151 let search active pattern incr =
3152 let dosearch re =
3153 let rec loop n =
3154 if n >= 0 && n < source#getitemcount
3155 then (
3156 let s, _ = source#getitem n in
3158 (try ignore (Str.search_forward re s 0); true
3159 with Not_found -> false)
3160 then Some n
3161 else loop (n + incr)
3163 else None
3165 loop active
3168 let re = Str.regexp_case_fold pattern in
3169 dosearch re
3170 with Failure s ->
3171 state.text <- s;
3172 None
3174 let itemcount = source#getitemcount in
3175 let find start incr =
3176 let rec find i =
3177 if i = -1 || i = itemcount
3178 then -1
3179 else (
3180 if source#hasaction i
3181 then i
3182 else find (i + incr)
3185 find start
3187 let set active first =
3188 let first = bound first 0 (itemcount - fstate.maxrows) in
3189 state.text <- "";
3190 coe {< m_active = active; m_first = first >}
3192 let navigate incr =
3193 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3194 let active, first =
3195 let incr1 = if incr > 0 then 1 else -1 in
3196 if isvisible m_first m_active
3197 then
3198 let next =
3199 let next = m_active + incr in
3200 let next =
3201 if next < 0 || next >= itemcount
3202 then -1
3203 else find next incr1
3205 if next = -1 || abs (m_active - next) > fstate.maxrows
3206 then -1
3207 else next
3209 if next = -1
3210 then
3211 let first = m_first + incr in
3212 let first = bound first 0 (itemcount - 1) in
3213 let next =
3214 let next = m_active + incr in
3215 let next = bound next 0 (itemcount - 1) in
3216 find next ~-incr1
3218 let active = if next = -1 then m_active else next in
3219 active, first
3220 else
3221 let first = min next m_first in
3222 let first =
3223 if abs (next - first) > fstate.maxrows
3224 then first + incr
3225 else first
3227 next, first
3228 else
3229 let first = m_first + incr in
3230 let first = bound first 0 (itemcount - 1) in
3231 let active =
3232 let next = m_active + incr in
3233 let next = bound next 0 (itemcount - 1) in
3234 let next = find next incr1 in
3235 let active =
3236 if next = -1 || abs (m_active - first) > fstate.maxrows
3237 then (
3238 let active = if m_active = -1 then next else m_active in
3239 active
3241 else next
3243 if isvisible first active
3244 then active
3245 else -1
3247 active, first
3249 G.postRedisplay "listview navigate";
3250 set active first;
3252 match key with
3253 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3254 let incr = if key = 0x72 then -1 else 1 in
3255 let active, first =
3256 match search (m_active + incr) m_qsearch incr with
3257 | None ->
3258 state.text <- m_qsearch ^ " [not found]";
3259 m_active, m_first
3260 | Some active ->
3261 state.text <- m_qsearch;
3262 active, firstof m_first active
3264 G.postRedisplay "listview ctrl-r/s";
3265 set1 active first m_qsearch;
3267 | 0xff08 -> (* backspace *)
3268 if String.length m_qsearch = 0
3269 then coe self
3270 else (
3271 let qsearch = withoutlastutf8 m_qsearch in
3272 let len = String.length qsearch in
3273 if len = 0
3274 then (
3275 state.text <- "";
3276 G.postRedisplay "listview empty qsearch";
3277 set1 m_active m_first "";
3279 else
3280 let active, first =
3281 match search m_active qsearch ~-1 with
3282 | None ->
3283 state.text <- qsearch ^ " [not found]";
3284 m_active, m_first
3285 | Some active ->
3286 state.text <- qsearch;
3287 active, firstof m_first active
3289 G.postRedisplay "listview backspace qsearch";
3290 set1 active first qsearch
3293 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3294 let pattern = m_qsearch ^ Wsi.toutf8 key in
3295 let active, first =
3296 match search m_active pattern 1 with
3297 | None ->
3298 state.text <- pattern ^ " [not found]";
3299 m_active, m_first
3300 | Some active ->
3301 state.text <- pattern;
3302 active, firstof m_first active
3304 G.postRedisplay "listview qsearch add";
3305 set1 active first pattern;
3307 | 0xff1b -> (* escape *)
3308 state.text <- "";
3309 if String.length m_qsearch = 0
3310 then (
3311 G.postRedisplay "list view escape";
3312 begin
3313 match
3314 source#exit (coe self) true m_active m_first m_pan m_qsearch
3315 with
3316 | None -> m_prev_uioh
3317 | Some uioh -> uioh
3320 else (
3321 G.postRedisplay "list view kill qsearch";
3322 source#setqsearch "";
3323 coe {< m_qsearch = "" >}
3326 | 0xff0d -> (* return *)
3327 state.text <- "";
3328 let self = {< m_qsearch = "" >} in
3329 source#setqsearch "";
3330 let opt =
3331 G.postRedisplay "listview enter";
3332 if m_active >= 0 && m_active < source#getitemcount
3333 then (
3334 source#exit (coe self) false m_active m_first m_pan "";
3336 else (
3337 source#exit (coe self) true m_active m_first m_pan "";
3340 begin match opt with
3341 | None -> m_prev_uioh
3342 | Some uioh -> uioh
3345 | 0xff9f | 0xffff -> (* delete *)
3346 coe self
3348 | 0xff52 -> navigate ~-1 (* up *)
3349 | 0xff54 -> navigate 1 (* down *)
3350 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3351 | 0xff56 -> navigate fstate.maxrows (* next *)
3353 | 0xff53 -> (* right *)
3354 state.text <- "";
3355 G.postRedisplay "listview right";
3356 coe {< m_pan = m_pan - 1 >}
3358 | 0xff51 -> (* left *)
3359 state.text <- "";
3360 G.postRedisplay "listview left";
3361 coe {< m_pan = m_pan + 1 >}
3363 | 0xff50 -> (* home *)
3364 let active = find 0 1 in
3365 G.postRedisplay "listview home";
3366 set active 0;
3368 | 0xff57 -> (* end *)
3369 let first = max 0 (itemcount - fstate.maxrows) in
3370 let active = find (itemcount - 1) ~-1 in
3371 G.postRedisplay "listview end";
3372 set active first;
3374 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3375 coe self
3377 | _ ->
3378 dolog "listview unknown key %#x" key; coe self
3380 method key key mask =
3381 match state.mode with
3382 | Textentry te -> textentrykeyboard key mask te; coe self
3383 | _ -> self#key1 key mask
3385 method button button down x y _ =
3386 let opt =
3387 match button with
3388 | 1 when x > conf.winw - conf.scrollbw ->
3389 G.postRedisplay "listview scroll";
3390 if down
3391 then
3392 let _, position, sh = self#scrollph in
3393 if y > truncate position && y < truncate (position +. sh)
3394 then (
3395 state.mstate <- Mscrolly;
3396 Some (coe self)
3398 else
3399 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3400 let first = truncate (s *. float source#getitemcount) in
3401 let first = min source#getitemcount first in
3402 Some (coe {< m_first = first; m_active = first >})
3403 else (
3404 state.mstate <- Mnone;
3405 Some (coe self);
3407 | 1 when not down ->
3408 begin match self#elemunder y with
3409 | Some n ->
3410 G.postRedisplay "listview click";
3411 source#exit
3412 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3413 | _ ->
3414 Some (coe self)
3416 | n when (n == 4 || n == 5) && not down ->
3417 let len = source#getitemcount in
3418 let first =
3419 if n = 5 && m_first + fstate.maxrows >= len
3420 then
3421 m_first
3422 else
3423 let first = m_first + (if n == 4 then -1 else 1) in
3424 bound first 0 (len - 1)
3426 G.postRedisplay "listview wheel";
3427 Some (coe {< m_first = first >})
3428 | n when (n = 6 || n = 7) && not down ->
3429 let inc = m_first + (if n = 7 then -1 else 1) in
3430 G.postRedisplay "listview hwheel";
3431 Some (coe {< m_pan = m_pan + inc >})
3432 | _ ->
3433 Some (coe self)
3435 match opt with
3436 | None -> m_prev_uioh
3437 | Some uioh -> uioh
3439 method motion _ y =
3440 match state.mstate with
3441 | Mscrolly ->
3442 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3443 let first = truncate (s *. float source#getitemcount) in
3444 let first = min source#getitemcount first in
3445 G.postRedisplay "listview motion";
3446 coe {< m_first = first; m_active = first >}
3447 | _ -> coe self
3449 method pmotion x y =
3450 if x < conf.winw - conf.scrollbw
3451 then
3452 let n =
3453 match self#elemunder y with
3454 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3455 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3457 let o =
3458 if n != m_active
3459 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3460 else self
3462 coe o
3463 else (
3464 Wsi.setcursor Wsi.CURSOR_INHERIT;
3465 coe self
3468 method infochanged _ = ()
3470 method scrollpw = (0, 0.0, 0.0)
3471 method scrollph =
3472 let nfs = fstate.fontsize + 1 in
3473 let y = m_first * nfs in
3474 let itemcount = source#getitemcount in
3475 let maxi = max 0 (itemcount - fstate.maxrows) in
3476 let maxy = maxi * nfs in
3477 let p, h = scrollph y maxy in
3478 conf.scrollbw, p, h
3480 method modehash = modehash
3481 end;;
3483 class outlinelistview ~source =
3484 object (self)
3485 inherit listview
3486 ~source:(source :> lvsource)
3487 ~trusted:false
3488 ~modehash:(findkeyhash conf "outline")
3489 as super
3491 method key key mask =
3492 let calcfirst first active =
3493 if active > first
3494 then
3495 let rows = active - first in
3496 let maxrows =
3497 if String.length state.text = 0
3498 then fstate.maxrows
3499 else fstate.maxrows - 2
3501 if rows > maxrows then active - maxrows else first
3502 else active
3504 let navigate incr =
3505 let active = m_active + incr in
3506 let active = bound active 0 (source#getitemcount - 1) in
3507 let first = calcfirst m_first active in
3508 G.postRedisplay "outline navigate";
3509 coe {< m_active = active; m_first = first >}
3511 let ctrl = Wsi.withctrl mask in
3512 match key with
3513 | 110 when ctrl -> (* ctrl-n *)
3514 source#narrow m_qsearch;
3515 G.postRedisplay "outline ctrl-n";
3516 coe {< m_first = 0; m_active = 0 >}
3518 | 117 when ctrl -> (* ctrl-u *)
3519 source#denarrow;
3520 G.postRedisplay "outline ctrl-u";
3521 state.text <- "";
3522 coe {< m_first = 0; m_active = 0 >}
3524 | 108 when ctrl -> (* ctrl-l *)
3525 let first = m_active - (fstate.maxrows / 2) in
3526 G.postRedisplay "outline ctrl-l";
3527 coe {< m_first = first >}
3529 | 0xff9f | 0xffff -> (* delete *)
3530 source#remove m_active;
3531 G.postRedisplay "outline delete";
3532 let active = max 0 (m_active-1) in
3533 coe {< m_first = firstof m_first active;
3534 m_active = active >}
3536 | 0xff52 -> navigate ~-1 (* up *)
3537 | 0xff54 -> navigate 1 (* down *)
3538 | 0xff55 -> (* prior *)
3539 navigate ~-(fstate.maxrows)
3540 | 0xff56 -> (* next *)
3541 navigate fstate.maxrows
3543 | 0xff53 -> (* [ctrl-]right *)
3544 let o =
3545 if ctrl
3546 then (
3547 G.postRedisplay "outline ctrl right";
3548 {< m_pan = m_pan + 1 >}
3550 else self#updownlevel 1
3552 coe o
3554 | 0xff51 -> (* [ctrl-]left *)
3555 let o =
3556 if ctrl
3557 then (
3558 G.postRedisplay "outline ctrl left";
3559 {< m_pan = m_pan - 1 >}
3561 else self#updownlevel ~-1
3563 coe o
3565 | 0xff50 -> (* home *)
3566 G.postRedisplay "outline home";
3567 coe {< m_first = 0; m_active = 0 >}
3569 | 0xff57 -> (* end *)
3570 let active = source#getitemcount - 1 in
3571 let first = max 0 (active - fstate.maxrows) in
3572 G.postRedisplay "outline end";
3573 coe {< m_active = active; m_first = first >}
3575 | _ -> super#key key mask
3578 let outlinesource usebookmarks =
3579 let empty = [||] in
3580 (object
3581 inherit lvsourcebase
3582 val mutable m_items = empty
3583 val mutable m_orig_items = empty
3584 val mutable m_prev_items = empty
3585 val mutable m_narrow_pattern = ""
3586 val mutable m_hadremovals = false
3588 method getitemcount =
3589 Array.length m_items + (if m_hadremovals then 1 else 0)
3591 method getitem n =
3592 if n == Array.length m_items && m_hadremovals
3593 then
3594 ("[Confirm removal]", 0)
3595 else
3596 let s, n, _ = m_items.(n) in
3597 (s, n)
3599 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3600 ignore (uioh, first, qsearch);
3601 let confrimremoval = m_hadremovals && active = Array.length m_items in
3602 let items =
3603 if String.length m_narrow_pattern = 0
3604 then m_orig_items
3605 else m_items
3607 if not cancel
3608 then (
3609 if not confrimremoval
3610 then(
3611 let _, _, anchor = m_items.(active) in
3612 gotoanchor anchor;
3613 m_items <- items;
3615 else (
3616 state.bookmarks <- Array.to_list m_items;
3617 m_orig_items <- m_items;
3620 else m_items <- items;
3621 m_pan <- pan;
3622 None
3624 method hasaction _ = true
3626 method greetmsg =
3627 if Array.length m_items != Array.length m_orig_items
3628 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3629 else ""
3631 method narrow pattern =
3632 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3633 match reopt with
3634 | None -> ()
3635 | Some re ->
3636 let rec loop accu n =
3637 if n = -1
3638 then (
3639 m_narrow_pattern <- pattern;
3640 m_items <- Array.of_list accu
3642 else
3643 let (s, _, _) as o = m_items.(n) in
3644 let accu =
3645 if (try ignore (Str.search_forward re s 0); true
3646 with Not_found -> false)
3647 then o :: accu
3648 else accu
3650 loop accu (n-1)
3652 loop [] (Array.length m_items - 1)
3654 method denarrow =
3655 m_orig_items <- (
3656 if usebookmarks
3657 then Array.of_list state.bookmarks
3658 else state.outlines
3660 m_items <- m_orig_items
3662 method remove m =
3663 if usebookmarks
3664 then
3665 if m >= 0 && m < Array.length m_items
3666 then (
3667 m_hadremovals <- true;
3668 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3669 let n = if n >= m then n+1 else n in
3670 m_items.(n)
3674 method reset anchor items =
3675 m_hadremovals <- false;
3676 if m_orig_items == empty || m_prev_items != items
3677 then (
3678 m_orig_items <- items;
3679 if String.length m_narrow_pattern = 0
3680 then m_items <- items;
3682 m_prev_items <- items;
3683 let rely = getanchory anchor in
3684 let active =
3685 let rec loop n best bestd =
3686 if n = Array.length m_items
3687 then best
3688 else
3689 let (_, _, anchor) = m_items.(n) in
3690 let orely = getanchory anchor in
3691 let d = abs (orely - rely) in
3692 if d < bestd
3693 then loop (n+1) n d
3694 else loop (n+1) best bestd
3696 loop 0 ~-1 max_int
3698 m_active <- active;
3699 m_first <- firstof m_first active
3700 end)
3703 let enterselector usebookmarks =
3704 let source = outlinesource usebookmarks in
3705 fun errmsg ->
3706 let outlines =
3707 if usebookmarks
3708 then Array.of_list state.bookmarks
3709 else state.outlines
3711 if Array.length outlines = 0
3712 then (
3713 showtext ' ' errmsg;
3715 else (
3716 state.text <- source#greetmsg;
3717 Wsi.setcursor Wsi.CURSOR_INHERIT;
3718 let anchor = getanchor () in
3719 source#reset anchor outlines;
3720 state.uioh <- coe (new outlinelistview ~source);
3721 G.postRedisplay "enter selector";
3725 let enteroutlinemode =
3726 let f = enterselector false in
3727 fun ()-> f "Document has no outline";
3730 let enterbookmarkmode =
3731 let f = enterselector true in
3732 fun () -> f "Document has no bookmarks (yet)";
3735 let color_of_string s =
3736 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3737 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3741 let color_to_string (r, g, b) =
3742 let r = truncate (r *. 256.0)
3743 and g = truncate (g *. 256.0)
3744 and b = truncate (b *. 256.0) in
3745 Printf.sprintf "%d/%d/%d" r g b
3748 let irect_of_string s =
3749 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3752 let irect_to_string (x0,y0,x1,y1) =
3753 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3756 let makecheckers () =
3757 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3758 following to say:
3759 converted by Issac Trotts. July 25, 2002 *)
3760 let image_height = 64
3761 and image_width = 64 in
3763 let make_image () =
3764 let image =
3765 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3767 for i = 0 to image_width - 1 do
3768 for j = 0 to image_height - 1 do
3769 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3770 (if (i land 8 ) lxor (j land 8) = 0
3771 then [|255;255;255|] else [|200;200;200|])
3772 done
3773 done;
3774 image
3776 let image = make_image () in
3777 let id = GlTex.gen_texture () in
3778 GlTex.bind_texture `texture_2d id;
3779 GlPix.store (`unpack_alignment 1);
3780 GlTex.image2d image;
3781 List.iter (GlTex.parameter ~target:`texture_2d)
3782 [ `wrap_s `repeat;
3783 `wrap_t `repeat;
3784 `mag_filter `nearest;
3785 `min_filter `nearest ];
3789 let setcheckers enabled =
3790 match state.texid with
3791 | None ->
3792 if enabled then state.texid <- Some (makecheckers ())
3794 | Some texid ->
3795 if not enabled
3796 then (
3797 GlTex.delete_texture texid;
3798 state.texid <- None;
3802 let int_of_string_with_suffix s =
3803 let l = String.length s in
3804 let s1, shift =
3805 if l > 1
3806 then
3807 let suffix = Char.lowercase s.[l-1] in
3808 match suffix with
3809 | 'k' -> String.sub s 0 (l-1), 10
3810 | 'm' -> String.sub s 0 (l-1), 20
3811 | 'g' -> String.sub s 0 (l-1), 30
3812 | _ -> s, 0
3813 else s, 0
3815 let n = int_of_string s1 in
3816 let m = n lsl shift in
3817 if m < 0 || m < n
3818 then raise (Failure "value too large")
3819 else m
3822 let string_with_suffix_of_int n =
3823 if n = 0
3824 then "0"
3825 else
3826 let n, s =
3827 if n land ((1 lsl 20) - 1) = 0
3828 then n lsr 20, "M"
3829 else (
3830 if n land ((1 lsl 10) - 1) = 0
3831 then n lsr 10, "K"
3832 else n, ""
3835 let rec loop s n =
3836 let h = n mod 1000 in
3837 let n = n / 1000 in
3838 if n = 0
3839 then string_of_int h ^ s
3840 else (
3841 let s = Printf.sprintf "_%03d%s" h s in
3842 loop s n
3845 loop "" n ^ s;
3848 let defghyllscroll = (40, 8, 32);;
3849 let ghyllscroll_of_string s =
3850 let (n, a, b) as nab =
3851 if s = "default"
3852 then defghyllscroll
3853 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3855 if n <= a || n <= b || a >= b
3856 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3857 nab;
3860 let ghyllscroll_to_string ((n, a, b) as nab) =
3861 if nab = defghyllscroll
3862 then "default"
3863 else Printf.sprintf "%d,%d,%d" n a b;
3866 let describe_location () =
3867 let f (fn, _) l =
3868 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3870 let fn, ln = List.fold_left f (-1, -1) state.layout in
3871 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3872 let percent =
3873 if maxy <= 0
3874 then 100.
3875 else (100. *. (float state.y /. float maxy))
3877 if fn = ln
3878 then
3879 Printf.sprintf "page %d of %d [%.2f%%]"
3880 (fn+1) state.pagecount percent
3881 else
3882 Printf.sprintf
3883 "pages %d-%d of %d [%.2f%%]"
3884 (fn+1) (ln+1) state.pagecount percent
3887 let setpresentationmode v =
3888 let (n, _, _) = getanchor () in
3889 let _, h = getpageyh n in
3890 state.anchor <- (n, 0.0, float (calcips h));
3891 conf.presentation <- v;
3892 if conf.presentation
3893 then (
3894 if not conf.scrollbarinpm
3895 then state.scrollw <- 0;
3897 else state.scrollw <- conf.scrollbw;
3898 represent ();
3901 let enterinfomode =
3902 let btos b = if b then "\xe2\x88\x9a" else "" in
3903 let showextended = ref false in
3904 let leave mode = function
3905 | Confirm -> state.mode <- mode
3906 | Cancel -> state.mode <- mode in
3907 let src =
3908 (object
3909 val mutable m_first_time = true
3910 val mutable m_l = []
3911 val mutable m_a = [||]
3912 val mutable m_prev_uioh = nouioh
3913 val mutable m_prev_mode = View
3915 inherit lvsourcebase
3917 method reset prev_mode prev_uioh =
3918 m_a <- Array.of_list (List.rev m_l);
3919 m_l <- [];
3920 m_prev_mode <- prev_mode;
3921 m_prev_uioh <- prev_uioh;
3922 if m_first_time
3923 then (
3924 let rec loop n =
3925 if n >= Array.length m_a
3926 then ()
3927 else
3928 match m_a.(n) with
3929 | _, _, _, Action _ -> m_active <- n
3930 | _ -> loop (n+1)
3932 loop 0;
3933 m_first_time <- false;
3936 method int name get set =
3937 m_l <-
3938 (name, `int get, 1, Action (
3939 fun u ->
3940 let ondone s =
3941 try set (int_of_string s)
3942 with exn ->
3943 state.text <- Printf.sprintf "bad integer `%s': %s"
3944 s (Printexc.to_string exn)
3946 state.text <- "";
3947 let te = name ^ ": ", "", None, intentry, ondone, true in
3948 state.mode <- Textentry (te, leave m_prev_mode);
3950 )) :: m_l
3952 method int_with_suffix name get set =
3953 m_l <-
3954 (name, `intws get, 1, Action (
3955 fun u ->
3956 let ondone s =
3957 try set (int_of_string_with_suffix s)
3958 with exn ->
3959 state.text <- Printf.sprintf "bad integer `%s': %s"
3960 s (Printexc.to_string exn)
3962 state.text <- "";
3963 let te =
3964 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3966 state.mode <- Textentry (te, leave m_prev_mode);
3968 )) :: m_l
3970 method bool ?(offset=1) ?(btos=btos) name get set =
3971 m_l <-
3972 (name, `bool (btos, get), offset, Action (
3973 fun u ->
3974 let v = get () in
3975 set (not v);
3977 )) :: m_l
3979 method color name get set =
3980 m_l <-
3981 (name, `color get, 1, Action (
3982 fun u ->
3983 let invalid = (nan, nan, nan) in
3984 let ondone s =
3985 let c =
3986 try color_of_string s
3987 with exn ->
3988 state.text <- Printf.sprintf "bad color `%s': %s"
3989 s (Printexc.to_string exn);
3990 invalid
3992 if c <> invalid
3993 then set c;
3995 let te = name ^ ": ", "", None, textentry, ondone, true in
3996 state.text <- color_to_string (get ());
3997 state.mode <- Textentry (te, leave m_prev_mode);
3999 )) :: m_l
4001 method string name get set =
4002 m_l <-
4003 (name, `string get, 1, Action (
4004 fun u ->
4005 let ondone s = set s in
4006 let te = name ^ ": ", "", None, textentry, ondone, true in
4007 state.mode <- Textentry (te, leave m_prev_mode);
4009 )) :: m_l
4011 method colorspace name get set =
4012 m_l <-
4013 (name, `string get, 1, Action (
4014 fun _ ->
4015 let source =
4016 let vals = [| "rgb"; "bgr"; "gray" |] in
4017 (object
4018 inherit lvsourcebase
4020 initializer
4021 m_active <- int_of_colorspace conf.colorspace;
4022 m_first <- 0;
4024 method getitemcount = Array.length vals
4025 method getitem n = (vals.(n), 0)
4026 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4027 ignore (uioh, first, pan, qsearch);
4028 if not cancel then set active;
4029 None
4030 method hasaction _ = true
4031 end)
4033 state.text <- "";
4034 let modehash = findkeyhash conf "info" in
4035 coe (new listview ~source ~trusted:true ~modehash)
4036 )) :: m_l
4038 method caption s offset =
4039 m_l <- (s, `empty, offset, Noaction) :: m_l
4041 method caption2 s f offset =
4042 m_l <- (s, `string f, offset, Noaction) :: m_l
4044 method getitemcount = Array.length m_a
4046 method getitem n =
4047 let tostr = function
4048 | `int f -> string_of_int (f ())
4049 | `intws f -> string_with_suffix_of_int (f ())
4050 | `string f -> f ()
4051 | `color f -> color_to_string (f ())
4052 | `bool (btos, f) -> btos (f ())
4053 | `empty -> ""
4055 let name, t, offset, _ = m_a.(n) in
4056 ((let s = tostr t in
4057 if String.length s > 0
4058 then Printf.sprintf "%s\t%s" name s
4059 else name),
4060 offset)
4062 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4063 let uiohopt =
4064 if not cancel
4065 then (
4066 m_qsearch <- qsearch;
4067 let uioh =
4068 match m_a.(active) with
4069 | _, _, _, Action f -> f uioh
4070 | _ -> uioh
4072 Some uioh
4074 else None
4076 m_active <- active;
4077 m_first <- first;
4078 m_pan <- pan;
4079 uiohopt
4081 method hasaction n =
4082 match m_a.(n) with
4083 | _, _, _, Action _ -> true
4084 | _ -> false
4085 end)
4087 let rec fillsrc prevmode prevuioh =
4088 let sep () = src#caption "" 0 in
4089 let colorp name get set =
4090 src#string name
4091 (fun () -> color_to_string (get ()))
4092 (fun v ->
4094 let c = color_of_string v in
4095 set c
4096 with exn ->
4097 state.text <- Printf.sprintf "bad color `%s': %s"
4098 v (Printexc.to_string exn);
4101 let oldmode = state.mode in
4102 let birdseye = isbirdseye state.mode in
4104 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4106 src#bool "presentation mode"
4107 (fun () -> conf.presentation)
4108 (fun v -> setpresentationmode v);
4110 src#bool "ignore case in searches"
4111 (fun () -> conf.icase)
4112 (fun v -> conf.icase <- v);
4114 src#bool "preload"
4115 (fun () -> conf.preload)
4116 (fun v -> conf.preload <- v);
4118 src#bool "highlight links"
4119 (fun () -> conf.hlinks)
4120 (fun v -> conf.hlinks <- v);
4122 src#bool "under info"
4123 (fun () -> conf.underinfo)
4124 (fun v -> conf.underinfo <- v);
4126 src#bool "persistent bookmarks"
4127 (fun () -> conf.savebmarks)
4128 (fun v -> conf.savebmarks <- v);
4130 src#bool "proportional display"
4131 (fun () -> conf.proportional)
4132 (fun v -> reqlayout conf.angle v);
4134 src#bool "trim margins"
4135 (fun () -> conf.trimmargins)
4136 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4138 src#bool "persistent location"
4139 (fun () -> conf.jumpback)
4140 (fun v -> conf.jumpback <- v);
4142 sep ();
4143 src#int "inter-page space"
4144 (fun () -> conf.interpagespace)
4145 (fun n ->
4146 conf.interpagespace <- n;
4147 docolumns conf.columns;
4148 let pageno, py =
4149 match state.layout with
4150 | [] -> 0, 0
4151 | l :: _ ->
4152 l.pageno, l.pagey
4154 state.maxy <- calcheight ();
4155 let y = getpagey pageno in
4156 gotoy (y + py)
4159 src#int "page bias"
4160 (fun () -> conf.pagebias)
4161 (fun v -> conf.pagebias <- v);
4163 src#int "scroll step"
4164 (fun () -> conf.scrollstep)
4165 (fun n -> conf.scrollstep <- n);
4167 src#int "horizontal scroll step"
4168 (fun () -> conf.hscrollstep)
4169 (fun v -> conf.hscrollstep <- v);
4171 src#int "auto scroll step"
4172 (fun () ->
4173 match state.autoscroll with
4174 | Some step -> step
4175 | _ -> conf.autoscrollstep)
4176 (fun n ->
4177 if state.autoscroll <> None
4178 then state.autoscroll <- Some n;
4179 conf.autoscrollstep <- n);
4181 src#int "zoom"
4182 (fun () -> truncate (conf.zoom *. 100.))
4183 (fun v -> setzoom ((float v) /. 100.));
4185 src#int "rotation"
4186 (fun () -> conf.angle)
4187 (fun v -> reqlayout v conf.proportional);
4189 src#int "scroll bar width"
4190 (fun () -> state.scrollw)
4191 (fun v ->
4192 state.scrollw <- v;
4193 conf.scrollbw <- v;
4194 reshape conf.winw conf.winh;
4197 src#int "scroll handle height"
4198 (fun () -> conf.scrollh)
4199 (fun v -> conf.scrollh <- v;);
4201 src#int "thumbnail width"
4202 (fun () -> conf.thumbw)
4203 (fun v ->
4204 conf.thumbw <- min 4096 v;
4205 match oldmode with
4206 | Birdseye beye ->
4207 leavebirdseye beye false;
4208 enterbirdseye ()
4209 | _ -> ()
4212 let mode = state.mode in
4213 src#string "columns"
4214 (fun () ->
4215 match conf.columns with
4216 | Csingle _ -> "1"
4217 | Cmulti (multi, _) -> multicolumns_to_string multi
4218 | Csplit (count, _) -> "-" ^ string_of_int count
4220 (fun v ->
4221 let n, a, b = multicolumns_of_string v in
4222 setcolumns mode n a b);
4224 sep ();
4225 src#caption "Presentation mode" 0;
4226 src#bool "scrollbar visible"
4227 (fun () -> conf.scrollbarinpm)
4228 (fun v ->
4229 if v != conf.scrollbarinpm
4230 then (
4231 conf.scrollbarinpm <- v;
4232 if conf.presentation
4233 then (
4234 state.scrollw <- if v then conf.scrollbw else 0;
4235 reshape conf.winw conf.winh;
4240 sep ();
4241 src#caption "Pixmap cache" 0;
4242 src#int_with_suffix "size (advisory)"
4243 (fun () -> conf.memlimit)
4244 (fun v -> conf.memlimit <- v);
4246 src#caption2 "used"
4247 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4248 (string_with_suffix_of_int state.memused)
4249 (Hashtbl.length state.tilemap)) 1;
4251 sep ();
4252 src#caption "Layout" 0;
4253 src#caption2 "Dimension"
4254 (fun () ->
4255 Printf.sprintf "%dx%d (virtual %dx%d)"
4256 conf.winw conf.winh
4257 state.w state.maxy)
4259 if conf.debug
4260 then
4261 src#caption2 "Position" (fun () ->
4262 Printf.sprintf "%dx%d" state.x state.y
4264 else
4265 src#caption2 "Visible" (fun () -> describe_location ()) 1
4268 sep ();
4269 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4270 "Save these parameters as global defaults at exit"
4271 (fun () -> conf.bedefault)
4272 (fun v -> conf.bedefault <- v)
4275 sep ();
4276 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4277 src#bool ~offset:0 ~btos "Extended parameters"
4278 (fun () -> !showextended)
4279 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4280 if !showextended
4281 then (
4282 src#bool "checkers"
4283 (fun () -> conf.checkers)
4284 (fun v -> conf.checkers <- v; setcheckers v);
4285 src#bool "update cursor"
4286 (fun () -> conf.updatecurs)
4287 (fun v -> conf.updatecurs <- v);
4288 src#bool "verbose"
4289 (fun () -> conf.verbose)
4290 (fun v -> conf.verbose <- v);
4291 src#bool "invert colors"
4292 (fun () -> conf.invert)
4293 (fun v -> conf.invert <- v);
4294 src#bool "max fit"
4295 (fun () -> conf.maxhfit)
4296 (fun v -> conf.maxhfit <- v);
4297 src#bool "redirect stderr"
4298 (fun () -> conf.redirectstderr)
4299 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4300 src#string "uri launcher"
4301 (fun () -> conf.urilauncher)
4302 (fun v -> conf.urilauncher <- v);
4303 src#string "path launcher"
4304 (fun () -> conf.pathlauncher)
4305 (fun v -> conf.pathlauncher <- v);
4306 src#string "tile size"
4307 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4308 (fun v ->
4310 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4311 conf.tilew <- max 64 w;
4312 conf.tileh <- max 64 h;
4313 flushtiles ();
4314 with exn ->
4315 state.text <- Printf.sprintf "bad tile size `%s': %s"
4316 v (Printexc.to_string exn));
4317 src#int "texture count"
4318 (fun () -> conf.texcount)
4319 (fun v ->
4320 if realloctexts v
4321 then conf.texcount <- v
4322 else showtext '!' " Failed to set texture count please retry later"
4324 src#int "slice height"
4325 (fun () -> conf.sliceheight)
4326 (fun v ->
4327 conf.sliceheight <- v;
4328 wcmd "sliceh %d" conf.sliceheight;
4330 src#int "anti-aliasing level"
4331 (fun () -> conf.aalevel)
4332 (fun v ->
4333 conf.aalevel <- bound v 0 8;
4334 state.anchor <- getanchor ();
4335 opendoc state.path state.password;
4337 src#string "page scroll scaling factor"
4338 (fun () -> string_of_float conf.pgscale)
4339 (fun v ->
4341 let s = float_of_string v in
4342 conf.pgscale <- s
4343 with exn ->
4344 state.text <- Printf.sprintf
4345 "bad page scroll scaling factor `%s': %s"
4346 v (Printexc.to_string exn)
4349 src#int "ui font size"
4350 (fun () -> fstate.fontsize)
4351 (fun v -> setfontsize (bound v 5 100));
4352 src#int "hint font size"
4353 (fun () -> conf.hfsize)
4354 (fun v -> conf.hfsize <- bound v 5 100);
4355 colorp "background color"
4356 (fun () -> conf.bgcolor)
4357 (fun v -> conf.bgcolor <- v);
4358 src#bool "crop hack"
4359 (fun () -> conf.crophack)
4360 (fun v -> conf.crophack <- v);
4361 src#bool "multi column centering"
4362 (fun () -> conf.multicenter)
4363 (fun v -> conf.multicenter <- v; represent ());
4364 src#string "trim fuzz"
4365 (fun () -> irect_to_string conf.trimfuzz)
4366 (fun v ->
4368 conf.trimfuzz <- irect_of_string v;
4369 if conf.trimmargins
4370 then settrim true conf.trimfuzz;
4371 with exn ->
4372 state.text <- Printf.sprintf "bad irect `%s': %s"
4373 v (Printexc.to_string exn)
4375 src#string "throttle"
4376 (fun () ->
4377 match conf.maxwait with
4378 | None -> "show place holder if page is not ready"
4379 | Some time ->
4380 if time = infinity
4381 then "wait for page to fully render"
4382 else
4383 "wait " ^ string_of_float time
4384 ^ " seconds before showing placeholder"
4386 (fun v ->
4388 let f = float_of_string v in
4389 if f <= 0.0
4390 then conf.maxwait <- None
4391 else conf.maxwait <- Some f
4392 with exn ->
4393 state.text <- Printf.sprintf "bad time `%s': %s"
4394 v (Printexc.to_string exn)
4396 src#string "ghyll scroll"
4397 (fun () ->
4398 match conf.ghyllscroll with
4399 | None -> ""
4400 | Some nab -> ghyllscroll_to_string nab
4402 (fun v ->
4404 let gs =
4405 if String.length v = 0
4406 then None
4407 else Some (ghyllscroll_of_string v)
4409 conf.ghyllscroll <- gs
4410 with exn ->
4411 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4412 v (Printexc.to_string exn)
4414 src#string "selection command"
4415 (fun () -> conf.selcmd)
4416 (fun v -> conf.selcmd <- v);
4417 src#colorspace "color space"
4418 (fun () -> colorspace_to_string conf.colorspace)
4419 (fun v ->
4420 conf.colorspace <- colorspace_of_int v;
4421 wcmd "cs %d" v;
4422 load state.layout;
4426 sep ();
4427 src#caption "Document" 0;
4428 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4429 src#caption2 "Pages"
4430 (fun () -> string_of_int state.pagecount) 1;
4431 src#caption2 "Dimensions"
4432 (fun () -> string_of_int (List.length state.pdims)) 1;
4433 if conf.trimmargins
4434 then (
4435 sep ();
4436 src#caption "Trimmed margins" 0;
4437 src#caption2 "Dimensions"
4438 (fun () -> string_of_int (List.length state.pdims)) 1;
4441 sep ();
4442 src#caption "OpenGL" 0;
4443 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4444 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4445 src#reset prevmode prevuioh;
4447 fun () ->
4448 state.text <- "";
4449 let prevmode = state.mode
4450 and prevuioh = state.uioh in
4451 fillsrc prevmode prevuioh;
4452 let source = (src :> lvsource) in
4453 let modehash = findkeyhash conf "info" in
4454 state.uioh <- coe (object (self)
4455 inherit listview ~source ~trusted:true ~modehash as super
4456 val mutable m_prevmemused = 0
4457 method infochanged = function
4458 | Memused ->
4459 if m_prevmemused != state.memused
4460 then (
4461 m_prevmemused <- state.memused;
4462 G.postRedisplay "memusedchanged";
4464 | Pdim -> G.postRedisplay "pdimchanged"
4465 | Docinfo -> fillsrc prevmode prevuioh
4467 method key key mask =
4468 if not (Wsi.withctrl mask)
4469 then
4470 match key with
4471 | 0xff51 -> coe (self#updownlevel ~-1)
4472 | 0xff53 -> coe (self#updownlevel 1)
4473 | _ -> super#key key mask
4474 else super#key key mask
4475 end);
4476 G.postRedisplay "info";
4479 let enterhelpmode =
4480 let source =
4481 (object
4482 inherit lvsourcebase
4483 method getitemcount = Array.length state.help
4484 method getitem n =
4485 let s, n, _ = state.help.(n) in
4486 (s, n)
4488 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4489 let optuioh =
4490 if not cancel
4491 then (
4492 m_qsearch <- qsearch;
4493 match state.help.(active) with
4494 | _, _, Action f -> Some (f uioh)
4495 | _ -> Some (uioh)
4497 else None
4499 m_active <- active;
4500 m_first <- first;
4501 m_pan <- pan;
4502 optuioh
4504 method hasaction n =
4505 match state.help.(n) with
4506 | _, _, Action _ -> true
4507 | _ -> false
4509 initializer
4510 m_active <- -1
4511 end)
4512 in fun () ->
4513 let modehash = findkeyhash conf "help" in
4514 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4515 G.postRedisplay "help";
4518 let entermsgsmode =
4519 let msgsource =
4520 let re = Str.regexp "[\r\n]" in
4521 (object
4522 inherit lvsourcebase
4523 val mutable m_items = [||]
4525 method getitemcount = 1 + Array.length m_items
4527 method getitem n =
4528 if n = 0
4529 then "[Clear]", 0
4530 else m_items.(n-1), 0
4532 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4533 ignore uioh;
4534 if not cancel
4535 then (
4536 if active = 0
4537 then Buffer.clear state.errmsgs;
4538 m_qsearch <- qsearch;
4540 m_active <- active;
4541 m_first <- first;
4542 m_pan <- pan;
4543 None
4545 method hasaction n =
4546 n = 0
4548 method reset =
4549 state.newerrmsgs <- false;
4550 let l = Str.split re (Buffer.contents state.errmsgs) in
4551 m_items <- Array.of_list l
4553 initializer
4554 m_active <- 0
4555 end)
4556 in fun () ->
4557 state.text <- "";
4558 msgsource#reset;
4559 let source = (msgsource :> lvsource) in
4560 let modehash = findkeyhash conf "listview" in
4561 state.uioh <- coe (object
4562 inherit listview ~source ~trusted:false ~modehash as super
4563 method display =
4564 if state.newerrmsgs
4565 then msgsource#reset;
4566 super#display
4567 end);
4568 G.postRedisplay "msgs";
4571 let quickbookmark ?title () =
4572 match state.layout with
4573 | [] -> ()
4574 | l :: _ ->
4575 let title =
4576 match title with
4577 | None ->
4578 let sec = Unix.gettimeofday () in
4579 let tm = Unix.localtime sec in
4580 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4581 (l.pageno+1)
4582 tm.Unix.tm_mday
4583 tm.Unix.tm_mon
4584 (tm.Unix.tm_year + 1900)
4585 tm.Unix.tm_hour
4586 tm.Unix.tm_min
4587 | Some title -> title
4589 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4592 let doreshape w h =
4593 state.fullscreen <- None;
4594 Wsi.reshape w h;
4597 let setautoscrollspeed step goingdown =
4598 let incr = max 1 ((abs step) / 2) in
4599 let incr = if goingdown then incr else -incr in
4600 let astep = step + incr in
4601 state.autoscroll <- Some astep;
4604 let gotounder = function
4605 | Ulinkgoto (pageno, top) ->
4606 if pageno >= 0
4607 then (
4608 addnav ();
4609 gotopage1 pageno top;
4612 | Ulinkuri s ->
4613 gotouri s
4615 | Uremote (filename, pageno) ->
4616 let path =
4617 if Sys.file_exists filename
4618 then filename
4619 else
4620 let dir = Filename.dirname state.path in
4621 let path = Filename.concat dir filename in
4622 if Sys.file_exists path
4623 then path
4624 else ""
4626 if String.length path > 0
4627 then (
4628 let anchor = getanchor () in
4629 let ranchor = state.path, state.password, anchor in
4630 state.anchor <- (pageno, 0.0, 0.0);
4631 state.ranchors <- ranchor :: state.ranchors;
4632 opendoc path "";
4634 else showtext '!' ("Could not find " ^ filename)
4636 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4639 let canpan () =
4640 match conf.columns with
4641 | Csplit _ -> true
4642 | _ -> conf.zoom > 1.0
4645 let viewkeyboard key mask =
4646 let enttext te =
4647 let mode = state.mode in
4648 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4649 state.text <- "";
4650 enttext ();
4651 G.postRedisplay "view:enttext"
4653 let ctrl = Wsi.withctrl mask in
4654 match key with
4655 | 81 -> (* Q *)
4656 exit 0
4658 | 0xff63 -> (* insert *)
4659 if conf.angle mod 360 = 0
4660 then (
4661 state.mode <- LinkNav (Ltgendir 0);
4662 gotoy state.y;
4664 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4666 | 0xff1b | 113 -> (* escape / q *)
4667 begin match state.mstate with
4668 | Mzoomrect _ ->
4669 state.mstate <- Mnone;
4670 Wsi.setcursor Wsi.CURSOR_INHERIT;
4671 G.postRedisplay "kill zoom rect";
4672 | _ ->
4673 match state.ranchors with
4674 | [] -> raise Quit
4675 | (path, password, anchor) :: rest ->
4676 state.ranchors <- rest;
4677 state.anchor <- anchor;
4678 opendoc path password
4679 end;
4681 | 0xff08 -> (* backspace *)
4682 let y = getnav ~-1 in
4683 gotoy_and_clear_text y
4685 | 111 -> (* o *)
4686 enteroutlinemode ()
4688 | 117 -> (* u *)
4689 state.rects <- [];
4690 state.text <- "";
4691 G.postRedisplay "dehighlight";
4693 | 47 | 63 -> (* / ? *)
4694 let ondone isforw s =
4695 cbput state.hists.pat s;
4696 state.searchpattern <- s;
4697 search s isforw
4699 let s = String.create 1 in
4700 s.[0] <- Char.chr key;
4701 enttext (s, "", Some (onhist state.hists.pat),
4702 textentry, ondone (key = 47), true)
4704 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4705 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4706 setzoom (conf.zoom +. incr)
4708 | 43 | 0xffab -> (* + *)
4709 let ondone s =
4710 let n =
4711 try int_of_string s with exc ->
4712 state.text <- Printf.sprintf "bad integer `%s': %s"
4713 s (Printexc.to_string exc);
4714 max_int
4716 if n != max_int
4717 then (
4718 conf.pagebias <- n;
4719 state.text <- "page bias is now " ^ string_of_int n;
4722 enttext ("page bias: ", "", None, intentry, ondone, true)
4724 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4725 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4726 setzoom (max 0.01 (conf.zoom -. decr))
4728 | 45 | 0xffad -> (* - *)
4729 let ondone msg = state.text <- msg in
4730 enttext (
4731 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4732 optentry state.mode, ondone, true
4735 | 48 when ctrl -> (* ctrl-0 *)
4736 setzoom 1.0
4738 | 49 when ctrl -> (* ctrl-1 *)
4739 let cols =
4740 match conf.columns with
4741 | Csingle _ | Cmulti _ -> 1
4742 | Csplit (n, _) -> n
4744 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4745 if zoom < 1.0
4746 then setzoom zoom
4748 | 0xffc6 -> (* f9 *)
4749 togglebirdseye ()
4751 | 57 when ctrl -> (* ctrl-9 *)
4752 togglebirdseye ()
4754 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4755 when not ctrl -> (* 0..9 *)
4756 let ondone s =
4757 let n =
4758 try int_of_string s with exc ->
4759 state.text <- Printf.sprintf "bad integer `%s': %s"
4760 s (Printexc.to_string exc);
4763 if n >= 0
4764 then (
4765 addnav ();
4766 cbput state.hists.pag (string_of_int n);
4767 gotopage1 (n + conf.pagebias - 1) 0;
4770 let pageentry text key =
4771 match Char.unsafe_chr key with
4772 | 'g' -> TEdone text
4773 | _ -> intentry text key
4775 let text = "x" in text.[0] <- Char.chr key;
4776 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4778 | 98 -> (* b *)
4779 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4780 reshape conf.winw conf.winh;
4782 | 108 -> (* l *)
4783 conf.hlinks <- not conf.hlinks;
4784 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4785 G.postRedisplay "toggle highlightlinks";
4787 | 70 -> (* F *)
4788 state.glinks <- true;
4789 let mode = state.mode in
4790 state.mode <- Textentry (
4791 (":", "", None, linknentry, linkndone (fun under ->
4792 addnav ();
4793 gotounder under
4794 ), false
4795 ), fun _ ->
4796 state.glinks <- false;
4797 state.mode <- mode
4799 state.text <- "";
4800 G.postRedisplay "view:linkent(F)"
4802 | 121 -> (* y *)
4803 state.glinks <- true;
4804 let mode = state.mode in
4805 state.mode <- Textentry (
4806 (":", "", None, linknentry, linkndone (fun under ->
4807 match Ne.pipe () with
4808 | Ne.Exn exn ->
4809 showtext '!' (Printf.sprintf "pipe failed: %s"
4810 (Printexc.to_string exn));
4811 | Ne.Res (r, w) ->
4812 let popened =
4813 try popen conf.selcmd [r, 0; w, -1]; true
4814 with exn ->
4815 showtext '!'
4816 (Printf.sprintf "failed to execute %s: %s"
4817 conf.selcmd (Printexc.to_string exn));
4818 false
4820 let clo cap fd =
4821 Ne.clo fd (fun msg ->
4822 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4825 let s = undertext under in
4826 if popened
4827 then
4828 (try
4829 let l = String.length s in
4830 let n = Unix.write w s 0 l in
4831 if n != l
4832 then
4833 showtext '!'
4834 (Printf.sprintf
4835 "failed to write %d characters to sel pipe, wrote %d"
4838 with exn ->
4839 showtext '!'
4840 (Printf.sprintf "failed to write to sel pipe: %s"
4841 (Printexc.to_string exn)
4844 else dolog "%s" s;
4845 clo "pipe/r" r;
4846 clo "pipe/w" w;
4847 ), false
4849 fun _ ->
4850 state.glinks <- false;
4851 state.mode <- mode
4853 state.text <- "";
4854 G.postRedisplay "view:linkent"
4856 | 97 -> (* a *)
4857 begin match state.autoscroll with
4858 | Some step ->
4859 conf.autoscrollstep <- step;
4860 state.autoscroll <- None
4861 | None ->
4862 if conf.autoscrollstep = 0
4863 then state.autoscroll <- Some 1
4864 else state.autoscroll <- Some conf.autoscrollstep
4867 | 112 when ctrl -> (* ctrl-p *)
4868 launchpath ()
4870 | 80 -> (* P *)
4871 setpresentationmode (not conf.presentation);
4872 showtext ' ' ("presentation mode " ^
4873 if conf.presentation then "on" else "off");
4875 | 102 -> (* f *)
4876 begin match state.fullscreen with
4877 | None ->
4878 state.fullscreen <- Some (conf.winw, conf.winh);
4879 Wsi.fullscreen ()
4880 | Some (w, h) ->
4881 state.fullscreen <- None;
4882 doreshape w h
4885 | 103 -> (* g *)
4886 gotoy_and_clear_text 0
4888 | 71 -> (* G *)
4889 gotopage1 (state.pagecount - 1) 0
4891 | 112 | 78 -> (* p|N *)
4892 search state.searchpattern false
4894 | 110 | 0xffc0 -> (* n|F3 *)
4895 search state.searchpattern true
4897 | 116 -> (* t *)
4898 begin match state.layout with
4899 | [] -> ()
4900 | l :: _ ->
4901 gotoy_and_clear_text (getpagey l.pageno)
4904 | 32 -> (* space *)
4905 begin match state.layout with
4906 | [] -> ()
4907 | l :: rest ->
4908 match conf.columns with
4909 | Csingle _ | Cmulti _ ->
4910 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4911 then
4912 let y = clamp (pgscale conf.winh) in
4913 gotoy_and_clear_text y
4914 else
4915 let pageno = min (l.pageno+1) (state.pagecount-1) in
4916 gotoy_and_clear_text (getpagey pageno)
4917 | Csplit (n, _) ->
4918 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4919 then
4920 let pagey, pageh = getpageyh l.pageno in
4921 let pagey = pagey + pageh * l.pagecol in
4922 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4923 gotoy_and_clear_text (pagey + pageh + ips)
4926 | 0xff9f | 0xffff -> (* delete *)
4927 begin match state.layout with
4928 | [] -> ()
4929 | l :: _ ->
4930 match conf.columns with
4931 | Csingle _ | Cmulti _ ->
4932 if conf.presentation && l.pagey != 0
4933 then
4934 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4935 else
4936 let pageno = max 0 (l.pageno-1) in
4937 gotoy_and_clear_text (getpagey pageno)
4938 | Csplit (n, _) ->
4939 let y =
4940 if l.pagecol = 0
4941 then
4942 if l.pageno = 0
4943 then l.pagey
4944 else
4945 let pageno = max 0 (l.pageno-1) in
4946 let pagey, pageh = getpageyh pageno in
4947 pagey + (n-1)*pageh
4948 else
4949 let pagey, pageh = getpageyh l.pageno in
4950 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4952 gotoy_and_clear_text y
4955 | 61 -> (* = *)
4956 showtext ' ' (describe_location ());
4958 | 119 -> (* w *)
4959 begin match state.layout with
4960 | [] -> ()
4961 | l :: _ ->
4962 doreshape (l.pagew + state.scrollw) l.pageh;
4963 G.postRedisplay "w"
4966 | 39 -> (* ' *)
4967 enterbookmarkmode ()
4969 | 104 | 0xffbe -> (* h|F1 *)
4970 enterhelpmode ()
4972 | 105 -> (* i *)
4973 enterinfomode ()
4975 | 101 when conf.redirectstderr -> (* e *)
4976 entermsgsmode ()
4978 | 109 -> (* m *)
4979 let ondone s =
4980 match state.layout with
4981 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
4982 | _ -> ()
4984 enttext ("bookmark: ", "", None, textentry, ondone, true)
4986 | 126 -> (* ~ *)
4987 quickbookmark ();
4988 showtext ' ' "Quick bookmark added";
4990 | 122 -> (* z *)
4991 begin match state.layout with
4992 | l :: _ ->
4993 let rect = getpdimrect l.pagedimno in
4994 let w, h =
4995 if conf.crophack
4996 then
4997 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4998 truncate (1.2 *. (rect.(3) -. rect.(0))))
4999 else
5000 (truncate (rect.(1) -. rect.(0)),
5001 truncate (rect.(3) -. rect.(0)))
5003 let w = truncate ((float w)*.conf.zoom)
5004 and h = truncate ((float h)*.conf.zoom) in
5005 if w != 0 && h != 0
5006 then (
5007 state.anchor <- getanchor ();
5008 doreshape (w + state.scrollw) (h + conf.interpagespace)
5010 G.postRedisplay "z";
5012 | [] -> ()
5015 | 50 when ctrl -> (* ctrl-2 *)
5016 let maxw = getmaxw () in
5017 if maxw > 0.0
5018 then setzoom (maxw /. float conf.winw)
5020 | 60 | 62 -> (* < > *)
5021 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5023 | 91 | 93 -> (* [ ] *)
5024 conf.colorscale <-
5025 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5027 G.postRedisplay "brightness";
5029 | 99 when state.mode = View -> (* c *)
5030 let (c, a, b), z =
5031 match state.prevcolumns with
5032 | None -> (1, 0, 0), 1.0
5033 | Some (columns, z) ->
5034 let cab =
5035 match columns with
5036 | Csplit (c, _) -> -c, 0, 0
5037 | Cmulti ((c, a, b), _) -> c, a, b
5038 | Csingle _ -> 1, 0, 0
5040 cab, z
5042 setcolumns View c a b;
5043 setzoom z;
5045 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5046 setzoom state.prevzoom
5048 | 107 | 0xff52 -> (* k up *)
5049 begin match state.autoscroll with
5050 | None ->
5051 begin match state.mode with
5052 | Birdseye beye -> upbirdseye 1 beye
5053 | _ ->
5054 if ctrl
5055 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5056 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5058 | Some n ->
5059 setautoscrollspeed n false
5062 | 106 | 0xff54 -> (* j down *)
5063 begin match state.autoscroll with
5064 | None ->
5065 begin match state.mode with
5066 | Birdseye beye -> downbirdseye 1 beye
5067 | _ ->
5068 if ctrl
5069 then gotoy_and_clear_text (clamp (conf.winh/2))
5070 else gotoy_and_clear_text (clamp conf.scrollstep)
5072 | Some n ->
5073 setautoscrollspeed n true
5076 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5077 if canpan ()
5078 then
5079 let dx =
5080 if ctrl
5081 then conf.winw / 2
5082 else 10
5084 let dx = if key = 0xff51 then dx else -dx in
5085 state.x <- state.x + dx;
5086 gotoy_and_clear_text state.y
5087 else (
5088 state.text <- "";
5089 G.postRedisplay "lef/right"
5092 | 0xff55 -> (* prior *)
5093 let y =
5094 if ctrl
5095 then
5096 match state.layout with
5097 | [] -> state.y
5098 | l :: _ -> state.y - l.pagey
5099 else
5100 clamp (pgscale (-conf.winh))
5102 gotoghyll y
5104 | 0xff56 -> (* next *)
5105 let y =
5106 if ctrl
5107 then
5108 match List.rev state.layout with
5109 | [] -> state.y
5110 | l :: _ -> getpagey l.pageno
5111 else
5112 clamp (pgscale conf.winh)
5114 gotoghyll y
5116 | 0xff50 -> (* home *)
5117 gotoghyll 0
5118 | 0xff57 -> (* end *)
5119 gotoghyll (clamp state.maxy)
5120 | 0xff53 when Wsi.withalt mask -> (* right *)
5121 gotoghyll (getnav ~-1)
5122 | 0xff51 when Wsi.withalt mask -> (* left *)
5123 gotoghyll (getnav 1)
5125 | 114 -> (* r *)
5126 state.anchor <- getanchor ();
5127 opendoc state.path state.password
5129 | 118 when conf.debug -> (* v *)
5130 state.rects <- [];
5131 List.iter (fun l ->
5132 match getopaque l.pageno with
5133 | None -> ()
5134 | Some opaque ->
5135 let x0, y0, x1, y1 = pagebbox opaque in
5136 let a,b = float x0, float y0 in
5137 let c,d = float x1, float y0 in
5138 let e,f = float x1, float y1 in
5139 let h,j = float x0, float y1 in
5140 let rect = (a,b,c,d,e,f,h,j) in
5141 debugrect rect;
5142 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5143 ) state.layout;
5144 G.postRedisplay "v";
5146 | _ ->
5147 vlog "huh? %s" (Wsi.keyname key)
5150 let linknavkeyboard key mask linknav =
5151 let getpage pageno =
5152 let rec loop = function
5153 | [] -> None
5154 | l :: _ when l.pageno = pageno -> Some l
5155 | _ :: rest -> loop rest
5156 in loop state.layout
5158 let doexact (pageno, n) =
5159 match getopaque pageno, getpage pageno with
5160 | Some opaque, Some l ->
5161 if key = 0xff0d
5162 then
5163 let under = getlink opaque n in
5164 G.postRedisplay "link gotounder";
5165 gotounder under;
5166 state.mode <- View;
5167 else
5168 let opt, dir =
5169 match key with
5170 | 0xff50 -> (* home *)
5171 Some (findlink opaque LDfirst), -1
5173 | 0xff57 -> (* end *)
5174 Some (findlink opaque LDlast), 1
5176 | 0xff51 -> (* left *)
5177 Some (findlink opaque (LDleft n)), -1
5179 | 0xff53 -> (* right *)
5180 Some (findlink opaque (LDright n)), 1
5182 | 0xff52 -> (* up *)
5183 Some (findlink opaque (LDup n)), -1
5185 | 0xff54 -> (* down *)
5186 Some (findlink opaque (LDdown n)), 1
5188 | _ -> None, 0
5190 let pwl l dir =
5191 begin match findpwl l.pageno dir with
5192 | Pwlnotfound -> ()
5193 | Pwl pageno ->
5194 let notfound dir =
5195 state.mode <- LinkNav (Ltgendir dir);
5196 let y, h = getpageyh pageno in
5197 let y =
5198 if dir < 0
5199 then y + h - conf.winh
5200 else y
5202 gotoy y
5204 begin match getopaque pageno, getpage pageno with
5205 | Some opaque, Some _ ->
5206 let link =
5207 let ld = if dir > 0 then LDfirst else LDlast in
5208 findlink opaque ld
5210 begin match link with
5211 | Lfound m ->
5212 showlinktype (getlink opaque m);
5213 state.mode <- LinkNav (Ltexact (pageno, m));
5214 G.postRedisplay "linknav jpage";
5215 | _ -> notfound dir
5216 end;
5217 | _ -> notfound dir
5218 end;
5219 end;
5221 begin match opt with
5222 | Some Lnotfound -> pwl l dir;
5223 | Some (Lfound m) ->
5224 if m = n
5225 then pwl l dir
5226 else (
5227 let _, y0, _, y1 = getlinkrect opaque m in
5228 if y0 < l.pagey
5229 then gotopage1 l.pageno y0
5230 else (
5231 let d = fstate.fontsize + 1 in
5232 if y1 - l.pagey > l.pagevh - d
5233 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5234 else G.postRedisplay "linknav";
5236 showlinktype (getlink opaque m);
5237 state.mode <- LinkNav (Ltexact (l.pageno, m));
5240 | None -> viewkeyboard key mask
5241 end;
5242 | _ -> viewkeyboard key mask
5244 if key = 0xff63
5245 then (
5246 state.mode <- View;
5247 G.postRedisplay "leave linknav"
5249 else
5250 match linknav with
5251 | Ltgendir _ -> viewkeyboard key mask
5252 | Ltexact exact -> doexact exact
5255 let keyboard key mask =
5256 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5257 then wcmd "interrupt"
5258 else state.uioh <- state.uioh#key key mask
5261 let birdseyekeyboard key mask
5262 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5263 let incr =
5264 match conf.columns with
5265 | Csingle _ -> 1
5266 | Cmulti ((c, _, _), _) -> c
5267 | Csplit _ -> failwith "bird's eye split mode"
5269 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5270 match key with
5271 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5272 let y, h = getpageyh pageno in
5273 let top = (conf.winh - h) / 2 in
5274 gotoy (max 0 (y - top))
5275 | 0xff0d -> leavebirdseye beye false
5276 | 0xff1b -> leavebirdseye beye true (* escape *)
5277 | 0xff52 -> upbirdseye incr beye (* up *)
5278 | 0xff54 -> downbirdseye incr beye (* down *)
5279 | 0xff51 -> upbirdseye 1 beye (* left *)
5280 | 0xff53 -> downbirdseye 1 beye (* right *)
5282 | 0xff55 -> (* prior *)
5283 begin match state.layout with
5284 | l :: _ ->
5285 if l.pagey != 0
5286 then (
5287 state.mode <- Birdseye (
5288 oconf, leftx, l.pageno, hooverpageno, anchor
5290 gotopage1 l.pageno 0;
5292 else (
5293 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5294 match layout with
5295 | [] -> gotoy (clamp (-conf.winh))
5296 | l :: _ ->
5297 state.mode <- Birdseye (
5298 oconf, leftx, l.pageno, hooverpageno, anchor
5300 gotopage1 l.pageno 0
5303 | [] -> gotoy (clamp (-conf.winh))
5304 end;
5306 | 0xff56 -> (* next *)
5307 begin match List.rev state.layout with
5308 | l :: _ ->
5309 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5310 begin match layout with
5311 | [] ->
5312 let incr = l.pageh - l.pagevh in
5313 if incr = 0
5314 then (
5315 state.mode <-
5316 Birdseye (
5317 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5319 G.postRedisplay "birdseye pagedown";
5321 else gotoy (clamp (incr + conf.interpagespace*2));
5323 | l :: _ ->
5324 state.mode <-
5325 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5326 gotopage1 l.pageno 0;
5329 | [] -> gotoy (clamp conf.winh)
5330 end;
5332 | 0xff50 -> (* home *)
5333 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5334 gotopage1 0 0
5336 | 0xff57 -> (* end *)
5337 let pageno = state.pagecount - 1 in
5338 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5339 if not (pagevisible state.layout pageno)
5340 then
5341 let h =
5342 match List.rev state.pdims with
5343 | [] -> conf.winh
5344 | (_, _, h, _) :: _ -> h
5346 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5347 else G.postRedisplay "birdseye end";
5348 | _ -> viewkeyboard key mask
5351 let drawpage l linkindexbase =
5352 let color =
5353 match state.mode with
5354 | Textentry _ -> scalecolor 0.4
5355 | LinkNav _
5356 | View -> scalecolor 1.0
5357 | Birdseye (_, _, pageno, hooverpageno, _) ->
5358 if l.pageno = hooverpageno
5359 then scalecolor 0.9
5360 else (
5361 if l.pageno = pageno
5362 then scalecolor 1.0
5363 else scalecolor 0.8
5366 drawtiles l color;
5367 begin match getopaque l.pageno with
5368 | Some opaque ->
5369 if tileready l l.pagex l.pagey
5370 then
5371 let x = l.pagedispx - l.pagex
5372 and y = l.pagedispy - l.pagey in
5373 let hlmask =
5374 match conf.columns with
5375 | Csingle _ | Cmulti _ ->
5376 (if conf.hlinks then 1 else 0)
5377 + (if state.glinks
5378 && not (isbirdseye state.mode) then 2 else 0)
5379 | _ -> 0
5381 let s =
5382 match state.mode with
5383 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5384 | _ -> ""
5386 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5387 else 0
5389 | _ -> 0
5390 end;
5393 let scrollindicator () =
5394 let sbw, ph, sh = state.uioh#scrollph in
5395 let sbh, pw, sw = state.uioh#scrollpw in
5397 GlDraw.color (0.64, 0.64, 0.64);
5398 GlDraw.rect
5399 (float (conf.winw - sbw), 0.)
5400 (float conf.winw, float conf.winh)
5402 GlDraw.rect
5403 (0., float (conf.winh - sbh))
5404 (float (conf.winw - state.scrollw - 1), float conf.winh)
5406 GlDraw.color (0.0, 0.0, 0.0);
5408 GlDraw.rect
5409 (float (conf.winw - sbw), ph)
5410 (float conf.winw, ph +. sh)
5412 GlDraw.rect
5413 (pw, float (conf.winh - sbh))
5414 (pw +. sw, float conf.winh)
5418 let showsel () =
5419 match state.mstate with
5420 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5423 | Msel ((x0, y0), (x1, y1)) ->
5424 let rec loop = function
5425 | l :: ls ->
5426 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5427 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5428 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5429 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5430 then
5431 match getopaque l.pageno with
5432 | Some opaque ->
5433 let x0, y0 = pagetranslatepoint l x0 y0 in
5434 let x1, y1 = pagetranslatepoint l x1 y1 in
5435 seltext opaque (x0, y0, x1, y1);
5436 | _ -> ()
5437 else loop ls
5438 | [] -> ()
5440 loop state.layout
5443 let showrects rects =
5444 Gl.enable `blend;
5445 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5446 GlDraw.polygon_mode `both `fill;
5447 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5448 List.iter
5449 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5450 List.iter (fun l ->
5451 if l.pageno = pageno
5452 then (
5453 let dx = float (l.pagedispx - l.pagex) in
5454 let dy = float (l.pagedispy - l.pagey) in
5455 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5456 GlDraw.begins `quads;
5458 GlDraw.vertex2 (x0+.dx, y0+.dy);
5459 GlDraw.vertex2 (x1+.dx, y1+.dy);
5460 GlDraw.vertex2 (x2+.dx, y2+.dy);
5461 GlDraw.vertex2 (x3+.dx, y3+.dy);
5463 GlDraw.ends ();
5465 ) state.layout
5466 ) rects
5468 Gl.disable `blend;
5471 let display () =
5472 GlClear.color (scalecolor2 conf.bgcolor);
5473 GlClear.clear [`color];
5474 let rec loop linkindexbase = function
5475 | l :: rest ->
5476 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5477 loop linkindexbase rest
5478 | [] -> ()
5480 loop 0 state.layout;
5481 let rects =
5482 match state.mode with
5483 | LinkNav (Ltexact (pageno, linkno)) ->
5484 begin match getopaque pageno with
5485 | Some opaque ->
5486 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5487 (pageno, 5, (
5488 float x0, float y0,
5489 float x1, float y0,
5490 float x1, float y1,
5491 float x0, float y1)
5492 ) :: state.rects
5493 | None -> state.rects
5495 | _ -> state.rects
5497 showrects rects;
5498 showsel ();
5499 state.uioh#display;
5500 begin match state.mstate with
5501 | Mzoomrect ((x0, y0), (x1, y1)) ->
5502 Gl.enable `blend;
5503 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5504 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5505 GlDraw.rect (float x0, float y0)
5506 (float x1, float y1);
5507 Gl.disable `blend;
5508 | _ -> ()
5509 end;
5510 enttext ();
5511 scrollindicator ();
5512 Wsi.swapb ();
5515 let zoomrect x y x1 y1 =
5516 let x0 = min x x1
5517 and x1 = max x x1
5518 and y0 = min y y1 in
5519 gotoy (state.y + y0);
5520 state.anchor <- getanchor ();
5521 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5522 let margin =
5523 if state.w < conf.winw - state.scrollw
5524 then (conf.winw - state.scrollw - state.w) / 2
5525 else 0
5527 state.x <- (state.x + margin) - x0;
5528 setzoom zoom;
5529 Wsi.setcursor Wsi.CURSOR_INHERIT;
5530 state.mstate <- Mnone;
5533 let scrollx x =
5534 let winw = conf.winw - state.scrollw - 1 in
5535 let s = float x /. float winw in
5536 let destx = truncate (float (state.w + winw) *. s) in
5537 state.x <- winw - destx;
5538 gotoy_and_clear_text state.y;
5539 state.mstate <- Mscrollx;
5542 let scrolly y =
5543 let s = float y /. float conf.winh in
5544 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5545 gotoy_and_clear_text desty;
5546 state.mstate <- Mscrolly;
5549 let viewmouse button down x y mask =
5550 match button with
5551 | n when (n == 4 || n == 5) && not down ->
5552 if Wsi.withctrl mask
5553 then (
5554 match state.mstate with
5555 | Mzoom (oldn, i) ->
5556 if oldn = n
5557 then (
5558 if i = 2
5559 then
5560 let incr =
5561 match n with
5562 | 5 ->
5563 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5564 | _ ->
5565 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5567 let zoom = conf.zoom -. incr in
5568 setzoom zoom;
5569 state.mstate <- Mzoom (n, 0);
5570 else
5571 state.mstate <- Mzoom (n, i+1);
5573 else state.mstate <- Mzoom (n, 0)
5575 | _ -> state.mstate <- Mzoom (n, 0)
5577 else (
5578 match state.autoscroll with
5579 | Some step -> setautoscrollspeed step (n=4)
5580 | None ->
5581 let incr =
5582 if n = 4
5583 then -conf.scrollstep
5584 else conf.scrollstep
5586 let incr = incr * 2 in
5587 let y = clamp incr in
5588 gotoy_and_clear_text y
5591 | n when (n = 6 || n = 7) && not down && canpan () ->
5592 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5593 gotoy_and_clear_text state.y
5595 | 1 when Wsi.withctrl mask ->
5596 if down
5597 then (
5598 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5599 state.mstate <- Mpan (x, y)
5601 else
5602 state.mstate <- Mnone
5604 | 3 ->
5605 if down
5606 then (
5607 Wsi.setcursor Wsi.CURSOR_CYCLE;
5608 let p = (x, y) in
5609 state.mstate <- Mzoomrect (p, p)
5611 else (
5612 match state.mstate with
5613 | Mzoomrect ((x0, y0), _) ->
5614 if abs (x-x0) > 10 && abs (y - y0) > 10
5615 then zoomrect x0 y0 x y
5616 else (
5617 state.mstate <- Mnone;
5618 Wsi.setcursor Wsi.CURSOR_INHERIT;
5619 G.postRedisplay "kill accidental zoom rect";
5621 | _ ->
5622 Wsi.setcursor Wsi.CURSOR_INHERIT;
5623 state.mstate <- Mnone
5626 | 1 when x > conf.winw - state.scrollw ->
5627 if down
5628 then
5629 let _, position, sh = state.uioh#scrollph in
5630 if y > truncate position && y < truncate (position +. sh)
5631 then state.mstate <- Mscrolly
5632 else scrolly y
5633 else
5634 state.mstate <- Mnone
5636 | 1 when y > conf.winh - state.hscrollh ->
5637 if down
5638 then
5639 let _, position, sw = state.uioh#scrollpw in
5640 if x > truncate position && x < truncate (position +. sw)
5641 then state.mstate <- Mscrollx
5642 else scrollx x
5643 else
5644 state.mstate <- Mnone
5646 | 1 ->
5647 let dest = if down then getunder x y else Unone in
5648 begin match dest with
5649 | Ulinkgoto _
5650 | Ulinkuri _
5651 | Uremote _
5652 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5653 gotounder dest
5655 | Unone when down ->
5656 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5657 state.mstate <- Mpan (x, y);
5659 | Unone | Utext _ ->
5660 if down
5661 then (
5662 if conf.angle mod 360 = 0
5663 then (
5664 state.mstate <- Msel ((x, y), (x, y));
5665 G.postRedisplay "mouse select";
5668 else (
5669 match state.mstate with
5670 | Mnone -> ()
5672 | Mzoom _ | Mscrollx | Mscrolly ->
5673 state.mstate <- Mnone
5675 | Mzoomrect ((x0, y0), _) ->
5676 zoomrect x0 y0 x y
5678 | Mpan _ ->
5679 Wsi.setcursor Wsi.CURSOR_INHERIT;
5680 state.mstate <- Mnone
5682 | Msel ((_, y0), (_, y1)) ->
5683 let rec loop = function
5684 | [] -> ()
5685 | l :: rest ->
5686 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5687 || ((y1 >= l.pagedispy
5688 && y1 <= (l.pagedispy + l.pagevh)))
5689 then
5690 match getopaque l.pageno with
5691 | Some opaque ->
5692 begin
5693 match Ne.pipe () with
5694 | Ne.Exn exn ->
5695 showtext '!'
5696 (Printf.sprintf
5697 "can not create sel pipe: %s"
5698 (Printexc.to_string exn));
5699 | Ne.Res (r, w) ->
5700 let doclose what fd =
5701 Ne.clo fd (fun msg ->
5702 dolog "%s close failed: %s" what msg)
5705 popen conf.selcmd [r, 0; w, -1];
5706 copysel w opaque;
5707 doclose "pipe/r" r;
5708 G.postRedisplay "copysel";
5709 with exn ->
5710 dolog "can not execute %S: %s"
5711 conf.selcmd (Printexc.to_string exn);
5712 doclose "pipe/r" r;
5713 doclose "pipe/w" w;
5715 | None -> ()
5716 else loop rest
5718 loop state.layout;
5719 Wsi.setcursor Wsi.CURSOR_INHERIT;
5720 state.mstate <- Mnone;
5724 | _ -> ()
5727 let birdseyemouse button down x y mask
5728 (conf, leftx, _, hooverpageno, anchor) =
5729 match button with
5730 | 1 when down ->
5731 let rec loop = function
5732 | [] -> ()
5733 | l :: rest ->
5734 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5735 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5736 then (
5737 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5739 else loop rest
5741 loop state.layout
5742 | 3 -> ()
5743 | _ -> viewmouse button down x y mask
5746 let mouse button down x y mask =
5747 state.uioh <- state.uioh#button button down x y mask;
5750 let motion ~x ~y =
5751 state.uioh <- state.uioh#motion x y
5754 let pmotion ~x ~y =
5755 state.uioh <- state.uioh#pmotion x y;
5758 let uioh = object
5759 method display = ()
5761 method key key mask =
5762 begin match state.mode with
5763 | Textentry textentry -> textentrykeyboard key mask textentry
5764 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5765 | View -> viewkeyboard key mask
5766 | LinkNav linknav -> linknavkeyboard key mask linknav
5767 end;
5768 state.uioh
5770 method button button bstate x y mask =
5771 begin match state.mode with
5772 | LinkNav _
5773 | View -> viewmouse button bstate x y mask
5774 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5775 | Textentry _ -> ()
5776 end;
5777 state.uioh
5779 method motion x y =
5780 begin match state.mode with
5781 | Textentry _ -> ()
5782 | View | Birdseye _ | LinkNav _ ->
5783 match state.mstate with
5784 | Mzoom _ | Mnone -> ()
5786 | Mpan (x0, y0) ->
5787 let dx = x - x0
5788 and dy = y0 - y in
5789 state.mstate <- Mpan (x, y);
5790 if canpan ()
5791 then state.x <- state.x + dx;
5792 let y = clamp dy in
5793 gotoy_and_clear_text y
5795 | Msel (a, _) ->
5796 state.mstate <- Msel (a, (x, y));
5797 G.postRedisplay "motion select";
5799 | Mscrolly ->
5800 let y = min conf.winh (max 0 y) in
5801 scrolly y
5803 | Mscrollx ->
5804 let x = min conf.winw (max 0 x) in
5805 scrollx x
5807 | Mzoomrect (p0, _) ->
5808 state.mstate <- Mzoomrect (p0, (x, y));
5809 G.postRedisplay "motion zoomrect";
5810 end;
5811 state.uioh
5813 method pmotion x y =
5814 begin match state.mode with
5815 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5816 let rec loop = function
5817 | [] ->
5818 if hooverpageno != -1
5819 then (
5820 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5821 G.postRedisplay "pmotion birdseye no hoover";
5823 | l :: rest ->
5824 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5825 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5826 then (
5827 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5828 G.postRedisplay "pmotion birdseye hoover";
5830 else loop rest
5832 loop state.layout
5834 | Textentry _ -> ()
5836 | LinkNav _
5837 | View ->
5838 match state.mstate with
5839 | Mnone -> updateunder x y
5840 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5842 end;
5843 state.uioh
5845 method infochanged _ = ()
5847 method scrollph =
5848 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5849 let p, h = scrollph state.y maxy in
5850 state.scrollw, p, h
5852 method scrollpw =
5853 let winw = conf.winw - state.scrollw - 1 in
5854 let fwinw = float winw in
5855 let sw =
5856 let sw = fwinw /. float state.w in
5857 let sw = fwinw *. sw in
5858 max sw (float conf.scrollh)
5860 let position, sw =
5861 let f = state.w+winw in
5862 let r = float (winw-state.x) /. float f in
5863 let p = fwinw *. r in
5864 p-.sw/.2., sw
5866 let sw =
5867 if position +. sw > fwinw
5868 then fwinw -. position
5869 else sw
5871 state.hscrollh, position, sw
5873 method modehash =
5874 let modename =
5875 match state.mode with
5876 | LinkNav _ -> "links"
5877 | Textentry _ -> "textentry"
5878 | Birdseye _ -> "birdseye"
5879 | View -> "view"
5881 findkeyhash conf modename
5882 end;;
5884 module Config =
5885 struct
5886 open Parser
5888 let fontpath = ref "";;
5890 module KeyMap =
5891 Map.Make (struct type t = (int * int) let compare = compare end);;
5893 let unent s =
5894 let l = String.length s in
5895 let b = Buffer.create l in
5896 unent b s 0 l;
5897 Buffer.contents b;
5900 let home =
5901 try Sys.getenv "HOME"
5902 with exn ->
5903 prerr_endline
5904 ("Can not determine home directory location: " ^
5905 Printexc.to_string exn);
5909 let modifier_of_string = function
5910 | "alt" -> Wsi.altmask
5911 | "shift" -> Wsi.shiftmask
5912 | "ctrl" | "control" -> Wsi.ctrlmask
5913 | "meta" -> Wsi.metamask
5914 | _ -> 0
5917 let key_of_string =
5918 let r = Str.regexp "-" in
5919 fun s ->
5920 let elems = Str.full_split r s in
5921 let f n k m =
5922 let g s =
5923 let m1 = modifier_of_string s in
5924 if m1 = 0
5925 then (Wsi.namekey s, m)
5926 else (k, m lor m1)
5927 in function
5928 | Str.Delim s when n land 1 = 0 -> g s
5929 | Str.Text s -> g s
5930 | Str.Delim _ -> (k, m)
5932 let rec loop n k m = function
5933 | [] -> (k, m)
5934 | x :: xs ->
5935 let k, m = f n k m x in
5936 loop (n+1) k m xs
5938 loop 0 0 0 elems
5941 let keys_of_string =
5942 let r = Str.regexp "[ \t]" in
5943 fun s ->
5944 let elems = Str.split r s in
5945 List.map key_of_string elems
5948 let copykeyhashes c =
5949 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5952 let config_of c attrs =
5953 let apply c k v =
5955 match k with
5956 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5957 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5958 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5959 | "preload" -> { c with preload = bool_of_string v }
5960 | "page-bias" -> { c with pagebias = int_of_string v }
5961 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5962 | "horizontal-scroll-step" ->
5963 { c with hscrollstep = max (int_of_string v) 1 }
5964 | "auto-scroll-step" ->
5965 { c with autoscrollstep = max 0 (int_of_string v) }
5966 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5967 | "crop-hack" -> { c with crophack = bool_of_string v }
5968 | "throttle" ->
5969 let mw =
5970 match String.lowercase v with
5971 | "true" -> Some infinity
5972 | "false" -> None
5973 | f -> Some (float_of_string f)
5975 { c with maxwait = mw}
5976 | "highlight-links" -> { c with hlinks = bool_of_string v }
5977 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5978 | "vertical-margin" ->
5979 { c with interpagespace = max 0 (int_of_string v) }
5980 | "zoom" ->
5981 let zoom = float_of_string v /. 100. in
5982 let zoom = max zoom 0.0 in
5983 { c with zoom = zoom }
5984 | "presentation" -> { c with presentation = bool_of_string v }
5985 | "rotation-angle" -> { c with angle = int_of_string v }
5986 | "width" -> { c with winw = max 20 (int_of_string v) }
5987 | "height" -> { c with winh = max 20 (int_of_string v) }
5988 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5989 | "proportional-display" -> { c with proportional = bool_of_string v }
5990 | "pixmap-cache-size" ->
5991 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5992 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5993 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5994 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5995 | "persistent-location" -> { c with jumpback = bool_of_string v }
5996 | "background-color" -> { c with bgcolor = color_of_string v }
5997 | "scrollbar-in-presentation" ->
5998 { c with scrollbarinpm = bool_of_string v }
5999 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6000 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6001 | "mupdf-store-size" ->
6002 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6003 | "checkers" -> { c with checkers = bool_of_string v }
6004 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6005 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6006 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6007 | "uri-launcher" -> { c with urilauncher = unent v }
6008 | "path-launcher" -> { c with pathlauncher = unent v }
6009 | "color-space" -> { c with colorspace = colorspace_of_string v }
6010 | "invert-colors" -> { c with invert = bool_of_string v }
6011 | "brightness" -> { c with colorscale = float_of_string v }
6012 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6013 | "ghyllscroll" ->
6014 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6015 | "columns" ->
6016 let (n, _, _) as nab = multicolumns_of_string v in
6017 if n < 0
6018 then { c with columns = Csplit (-n, [||]) }
6019 else { c with columns = Cmulti (nab, [||]) }
6020 | "birds-eye-columns" ->
6021 { c with beyecolumns = Some (max (int_of_string v) 2) }
6022 | "selection-command" -> { c with selcmd = unent v }
6023 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6024 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6025 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6026 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6027 | _ -> c
6028 with exn ->
6029 prerr_endline ("Error processing attribute (`" ^
6030 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6033 let rec fold c = function
6034 | [] -> c
6035 | (k, v) :: rest ->
6036 let c = apply c k v in
6037 fold c rest
6039 fold { c with keyhashes = copykeyhashes c } attrs;
6042 let fromstring f pos n v d =
6043 try f v
6044 with exn ->
6045 dolog "Error processing attribute (%S=%S) at %d\n%s"
6046 n v pos (Printexc.to_string exn)
6051 let bookmark_of attrs =
6052 let rec fold title page rely visy = function
6053 | ("title", v) :: rest -> fold v page rely visy rest
6054 | ("page", v) :: rest -> fold title v rely visy rest
6055 | ("rely", v) :: rest -> fold title page v visy rest
6056 | ("visy", v) :: rest -> fold title page rely v rest
6057 | _ :: rest -> fold title page rely visy rest
6058 | [] -> title, page, rely, visy
6060 fold "invalid" "0" "0" "0" attrs
6063 let doc_of attrs =
6064 let rec fold path page rely pan visy = function
6065 | ("path", v) :: rest -> fold v page rely pan visy rest
6066 | ("page", v) :: rest -> fold path v rely pan visy rest
6067 | ("rely", v) :: rest -> fold path page v pan visy rest
6068 | ("pan", v) :: rest -> fold path page rely v visy rest
6069 | ("visy", v) :: rest -> fold path page rely pan v rest
6070 | _ :: rest -> fold path page rely pan visy rest
6071 | [] -> path, page, rely, pan, visy
6073 fold "" "0" "0" "0" "0" attrs
6076 let map_of attrs =
6077 let rec fold rs ls = function
6078 | ("out", v) :: rest -> fold v ls rest
6079 | ("in", v) :: rest -> fold rs v rest
6080 | _ :: rest -> fold ls rs rest
6081 | [] -> ls, rs
6083 fold "" "" attrs
6086 let setconf dst src =
6087 dst.scrollbw <- src.scrollbw;
6088 dst.scrollh <- src.scrollh;
6089 dst.icase <- src.icase;
6090 dst.preload <- src.preload;
6091 dst.pagebias <- src.pagebias;
6092 dst.verbose <- src.verbose;
6093 dst.scrollstep <- src.scrollstep;
6094 dst.maxhfit <- src.maxhfit;
6095 dst.crophack <- src.crophack;
6096 dst.autoscrollstep <- src.autoscrollstep;
6097 dst.maxwait <- src.maxwait;
6098 dst.hlinks <- src.hlinks;
6099 dst.underinfo <- src.underinfo;
6100 dst.interpagespace <- src.interpagespace;
6101 dst.zoom <- src.zoom;
6102 dst.presentation <- src.presentation;
6103 dst.angle <- src.angle;
6104 dst.winw <- src.winw;
6105 dst.winh <- src.winh;
6106 dst.savebmarks <- src.savebmarks;
6107 dst.memlimit <- src.memlimit;
6108 dst.proportional <- src.proportional;
6109 dst.texcount <- src.texcount;
6110 dst.sliceheight <- src.sliceheight;
6111 dst.thumbw <- src.thumbw;
6112 dst.jumpback <- src.jumpback;
6113 dst.bgcolor <- src.bgcolor;
6114 dst.scrollbarinpm <- src.scrollbarinpm;
6115 dst.tilew <- src.tilew;
6116 dst.tileh <- src.tileh;
6117 dst.mustoresize <- src.mustoresize;
6118 dst.checkers <- src.checkers;
6119 dst.aalevel <- src.aalevel;
6120 dst.trimmargins <- src.trimmargins;
6121 dst.trimfuzz <- src.trimfuzz;
6122 dst.urilauncher <- src.urilauncher;
6123 dst.colorspace <- src.colorspace;
6124 dst.invert <- src.invert;
6125 dst.colorscale <- src.colorscale;
6126 dst.redirectstderr <- src.redirectstderr;
6127 dst.ghyllscroll <- src.ghyllscroll;
6128 dst.columns <- src.columns;
6129 dst.beyecolumns <- src.beyecolumns;
6130 dst.selcmd <- src.selcmd;
6131 dst.updatecurs <- src.updatecurs;
6132 dst.pathlauncher <- src.pathlauncher;
6133 dst.keyhashes <- copykeyhashes src;
6134 dst.hfsize <- src.hfsize;
6135 dst.hscrollstep <- src.hscrollstep;
6136 dst.pgscale <- src.pgscale;
6137 dst.multicenter <- src.multicenter;
6140 let get s =
6141 let h = Hashtbl.create 10 in
6142 let dc = { defconf with angle = defconf.angle } in
6143 let rec toplevel v t spos _ =
6144 match t with
6145 | Vdata | Vcdata | Vend -> v
6146 | Vopen ("llppconfig", _, closed) ->
6147 if closed
6148 then v
6149 else { v with f = llppconfig }
6150 | Vopen _ ->
6151 error "unexpected subelement at top level" s spos
6152 | Vclose _ -> error "unexpected close at top level" s spos
6154 and llppconfig v t spos _ =
6155 match t with
6156 | Vdata | Vcdata -> v
6157 | Vend -> error "unexpected end of input in llppconfig" s spos
6158 | Vopen ("defaults", attrs, closed) ->
6159 let c = config_of dc attrs in
6160 setconf dc c;
6161 if closed
6162 then v
6163 else { v with f = defaults }
6165 | Vopen ("ui-font", attrs, closed) ->
6166 let rec getsize size = function
6167 | [] -> size
6168 | ("size", v) :: rest ->
6169 let size =
6170 fromstring int_of_string spos "size" v fstate.fontsize in
6171 getsize size rest
6172 | l -> getsize size l
6174 fstate.fontsize <- getsize fstate.fontsize attrs;
6175 if closed
6176 then v
6177 else { v with f = uifont (Buffer.create 10) }
6179 | Vopen ("doc", attrs, closed) ->
6180 let pathent, spage, srely, span, svisy = doc_of attrs in
6181 let path = unent pathent
6182 and pageno = fromstring int_of_string spos "page" spage 0
6183 and rely = fromstring float_of_string spos "rely" srely 0.0
6184 and pan = fromstring int_of_string spos "pan" span 0
6185 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6186 let c = config_of dc attrs in
6187 let anchor = (pageno, rely, visy) in
6188 if closed
6189 then (Hashtbl.add h path (c, [], pan, anchor); v)
6190 else { v with f = doc path pan anchor c [] }
6192 | Vopen _ ->
6193 error "unexpected subelement in llppconfig" s spos
6195 | Vclose "llppconfig" -> { v with f = toplevel }
6196 | Vclose _ -> error "unexpected close in llppconfig" s spos
6198 and defaults v t spos _ =
6199 match t with
6200 | Vdata | Vcdata -> v
6201 | Vend -> error "unexpected end of input in defaults" s spos
6202 | Vopen ("keymap", attrs, closed) ->
6203 let modename =
6204 try List.assoc "mode" attrs
6205 with Not_found -> "global" in
6206 if closed
6207 then v
6208 else
6209 let ret keymap =
6210 let h = findkeyhash dc modename in
6211 KeyMap.iter (Hashtbl.replace h) keymap;
6212 defaults
6214 { v with f = pkeymap ret KeyMap.empty }
6216 | Vopen (_, _, _) ->
6217 error "unexpected subelement in defaults" s spos
6219 | Vclose "defaults" ->
6220 { v with f = llppconfig }
6222 | Vclose _ -> error "unexpected close in defaults" s spos
6224 and uifont b v t spos epos =
6225 match t with
6226 | Vdata | Vcdata ->
6227 Buffer.add_substring b s spos (epos - spos);
6229 | Vopen (_, _, _) ->
6230 error "unexpected subelement in ui-font" s spos
6231 | Vclose "ui-font" ->
6232 if String.length !fontpath = 0
6233 then fontpath := Buffer.contents b;
6234 { v with f = llppconfig }
6235 | Vclose _ -> error "unexpected close in ui-font" s spos
6236 | Vend -> error "unexpected end of input in ui-font" s spos
6238 and doc path pan anchor c bookmarks v t spos _ =
6239 match t with
6240 | Vdata | Vcdata -> v
6241 | Vend -> error "unexpected end of input in doc" s spos
6242 | Vopen ("bookmarks", _, closed) ->
6243 if closed
6244 then v
6245 else { v with f = pbookmarks path pan anchor c bookmarks }
6247 | Vopen ("keymap", attrs, closed) ->
6248 let modename =
6249 try List.assoc "mode" attrs
6250 with Not_found -> "global"
6252 if closed
6253 then v
6254 else
6255 let ret keymap =
6256 let h = findkeyhash c modename in
6257 KeyMap.iter (Hashtbl.replace h) keymap;
6258 doc path pan anchor c bookmarks
6260 { v with f = pkeymap ret KeyMap.empty }
6262 | Vopen (_, _, _) ->
6263 error "unexpected subelement in doc" s spos
6265 | Vclose "doc" ->
6266 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6267 { v with f = llppconfig }
6269 | Vclose _ -> error "unexpected close in doc" s spos
6271 and pkeymap ret keymap v t spos _ =
6272 match t with
6273 | Vdata | Vcdata -> v
6274 | Vend -> error "unexpected end of input in keymap" s spos
6275 | Vopen ("map", attrs, closed) ->
6276 let r, l = map_of attrs in
6277 let kss = fromstring keys_of_string spos "in" r [] in
6278 let lss = fromstring keys_of_string spos "out" l [] in
6279 let keymap =
6280 match kss with
6281 | [] -> keymap
6282 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6283 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6285 if closed
6286 then { v with f = pkeymap ret keymap }
6287 else
6288 let f () = v in
6289 { v with f = skip "map" f }
6291 | Vopen _ ->
6292 error "unexpected subelement in keymap" s spos
6294 | Vclose "keymap" ->
6295 { v with f = ret keymap }
6297 | Vclose _ -> error "unexpected close in keymap" s spos
6299 and pbookmarks path pan anchor c bookmarks v t spos _ =
6300 match t with
6301 | Vdata | Vcdata -> v
6302 | Vend -> error "unexpected end of input in bookmarks" s spos
6303 | Vopen ("item", attrs, closed) ->
6304 let titleent, spage, srely, svisy = bookmark_of attrs in
6305 let page = fromstring int_of_string spos "page" spage 0
6306 and rely = fromstring float_of_string spos "rely" srely 0.0
6307 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6308 let bookmarks =
6309 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6311 if closed
6312 then { v with f = pbookmarks path pan anchor c bookmarks }
6313 else
6314 let f () = v in
6315 { v with f = skip "item" f }
6317 | Vopen _ ->
6318 error "unexpected subelement in bookmarks" s spos
6320 | Vclose "bookmarks" ->
6321 { v with f = doc path pan anchor c bookmarks }
6323 | Vclose _ -> error "unexpected close in bookmarks" s spos
6325 and skip tag f v t spos _ =
6326 match t with
6327 | Vdata | Vcdata -> v
6328 | Vend ->
6329 error ("unexpected end of input in skipped " ^ tag) s spos
6330 | Vopen (tag', _, closed) ->
6331 if closed
6332 then v
6333 else
6334 let f' () = { v with f = skip tag f } in
6335 { v with f = skip tag' f' }
6336 | Vclose ctag ->
6337 if tag = ctag
6338 then f ()
6339 else error ("unexpected close in skipped " ^ tag) s spos
6342 parse { f = toplevel; accu = () } s;
6343 h, dc;
6346 let do_load f ic =
6348 let len = in_channel_length ic in
6349 let s = String.create len in
6350 really_input ic s 0 len;
6351 f s;
6352 with
6353 | Parse_error (msg, s, pos) ->
6354 let subs = subs s pos in
6355 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6356 failwith ("parse error: " ^ s)
6358 | exn ->
6359 failwith ("config load error: " ^ Printexc.to_string exn)
6362 let defconfpath =
6363 let dir =
6365 let dir = Filename.concat home ".config" in
6366 if Sys.is_directory dir then dir else home
6367 with _ -> home
6369 Filename.concat dir "llpp.conf"
6372 let confpath = ref defconfpath;;
6374 let load1 f =
6375 if Sys.file_exists !confpath
6376 then
6377 match
6378 (try Some (open_in_bin !confpath)
6379 with exn ->
6380 prerr_endline
6381 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6382 Printexc.to_string exn);
6383 None
6385 with
6386 | Some ic ->
6387 begin try
6388 f (do_load get ic)
6389 with exn ->
6390 prerr_endline
6391 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6392 Printexc.to_string exn);
6393 end;
6394 close_in ic;
6396 | None -> ()
6397 else
6398 f (Hashtbl.create 0, defconf)
6401 let load () =
6402 let f (h, dc) =
6403 let pc, pb, px, pa =
6405 Hashtbl.find h (Filename.basename state.path)
6406 with Not_found -> dc, [], 0, emptyanchor
6408 setconf defconf dc;
6409 setconf conf pc;
6410 state.bookmarks <- pb;
6411 state.x <- px;
6412 state.scrollw <- conf.scrollbw;
6413 if conf.jumpback
6414 then state.anchor <- pa;
6415 cbput state.hists.nav pa;
6417 load1 f
6420 let add_attrs bb always dc c =
6421 let ob s a b =
6422 if always || a != b
6423 then Printf.bprintf bb "\n %s='%b'" s a
6424 and oi s a b =
6425 if always || a != b
6426 then Printf.bprintf bb "\n %s='%d'" s a
6427 and oI s a b =
6428 if always || a != b
6429 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6430 and oz s a b =
6431 if always || a <> b
6432 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6433 and oF s a b =
6434 if always || a <> b
6435 then Printf.bprintf bb "\n %s='%f'" s a
6436 and oc s a b =
6437 if always || a <> b
6438 then
6439 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6440 and oC s a b =
6441 if always || a <> b
6442 then
6443 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6444 and oR s a b =
6445 if always || a <> b
6446 then
6447 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6448 and os s a b =
6449 if always || a <> b
6450 then
6451 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6452 and og s a b =
6453 if always || a <> b
6454 then
6455 match a with
6456 | None -> ()
6457 | Some (_N, _A, _B) ->
6458 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6459 and oW s a b =
6460 if always || a <> b
6461 then
6462 let v =
6463 match a with
6464 | None -> "false"
6465 | Some f ->
6466 if f = infinity
6467 then "true"
6468 else string_of_float f
6470 Printf.bprintf bb "\n %s='%s'" s v
6471 and oco s a b =
6472 if always || a <> b
6473 then
6474 match a with
6475 | Cmulti ((n, a, b), _) when n > 1 ->
6476 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6477 | Csplit (n, _) when n > 1 ->
6478 Printf.bprintf bb "\n %s='%d'" s ~-n
6479 | _ -> ()
6480 and obeco s a b =
6481 if always || a <> b
6482 then
6483 match a with
6484 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6485 | _ -> ()
6487 let w, h =
6488 if always
6489 then dc.winw, dc.winh
6490 else
6491 match state.fullscreen with
6492 | Some wh -> wh
6493 | None -> c.winw, c.winh
6495 oi "width" w dc.winw;
6496 oi "height" h dc.winh;
6497 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6498 oi "scroll-handle-height" c.scrollh dc.scrollh;
6499 ob "case-insensitive-search" c.icase dc.icase;
6500 ob "preload" c.preload dc.preload;
6501 oi "page-bias" c.pagebias dc.pagebias;
6502 oi "scroll-step" c.scrollstep dc.scrollstep;
6503 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6504 ob "max-height-fit" c.maxhfit dc.maxhfit;
6505 ob "crop-hack" c.crophack dc.crophack;
6506 oW "throttle" c.maxwait dc.maxwait;
6507 ob "highlight-links" c.hlinks dc.hlinks;
6508 ob "under-cursor-info" c.underinfo dc.underinfo;
6509 oi "vertical-margin" c.interpagespace dc.interpagespace;
6510 oz "zoom" c.zoom dc.zoom;
6511 ob "presentation" c.presentation dc.presentation;
6512 oi "rotation-angle" c.angle dc.angle;
6513 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6514 ob "proportional-display" c.proportional dc.proportional;
6515 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6516 oi "tex-count" c.texcount dc.texcount;
6517 oi "slice-height" c.sliceheight dc.sliceheight;
6518 oi "thumbnail-width" c.thumbw dc.thumbw;
6519 ob "persistent-location" c.jumpback dc.jumpback;
6520 oc "background-color" c.bgcolor dc.bgcolor;
6521 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6522 oi "tile-width" c.tilew dc.tilew;
6523 oi "tile-height" c.tileh dc.tileh;
6524 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6525 ob "checkers" c.checkers dc.checkers;
6526 oi "aalevel" c.aalevel dc.aalevel;
6527 ob "trim-margins" c.trimmargins dc.trimmargins;
6528 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6529 os "uri-launcher" c.urilauncher dc.urilauncher;
6530 os "path-launcher" c.pathlauncher dc.pathlauncher;
6531 oC "color-space" c.colorspace dc.colorspace;
6532 ob "invert-colors" c.invert dc.invert;
6533 oF "brightness" c.colorscale dc.colorscale;
6534 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6535 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6536 oco "columns" c.columns dc.columns;
6537 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6538 os "selection-command" c.selcmd dc.selcmd;
6539 ob "update-cursor" c.updatecurs dc.updatecurs;
6540 oi "hint-font-size" c.hfsize dc.hfsize;
6541 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6542 oF "page-scroll-scale" c.pgscale dc.pgscale;
6543 ob "multi-column-centering" c.multicenter dc.multicenter;
6546 let keymapsbuf always dc c =
6547 let bb = Buffer.create 16 in
6548 let rec loop = function
6549 | [] -> ()
6550 | (modename, h) :: rest ->
6551 let dh = findkeyhash dc modename in
6552 if always || h <> dh
6553 then (
6554 if Hashtbl.length h > 0
6555 then (
6556 if Buffer.length bb > 0
6557 then Buffer.add_char bb '\n';
6558 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6559 Hashtbl.iter (fun i o ->
6560 let isdifferent = always ||
6562 let dO = Hashtbl.find dh i in
6563 dO <> o
6564 with Not_found -> true
6566 if isdifferent
6567 then
6568 let addkm (k, m) =
6569 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6570 if Wsi.withalt m then Buffer.add_string bb "alt-";
6571 if Wsi.withshift m then Buffer.add_string bb "shift-";
6572 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6573 Buffer.add_string bb (Wsi.keyname k);
6575 let addkms l =
6576 let rec loop = function
6577 | [] -> ()
6578 | km :: [] -> addkm km
6579 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6581 loop l
6583 Buffer.add_string bb "<map in='";
6584 addkm i;
6585 match o with
6586 | KMinsrt km ->
6587 Buffer.add_string bb "' out='";
6588 addkm km;
6589 Buffer.add_string bb "'/>\n"
6591 | KMinsrl kms ->
6592 Buffer.add_string bb "' out='";
6593 addkms kms;
6594 Buffer.add_string bb "'/>\n"
6596 | KMmulti (ins, kms) ->
6597 Buffer.add_char bb ' ';
6598 addkms ins;
6599 Buffer.add_string bb "' out='";
6600 addkms kms;
6601 Buffer.add_string bb "'/>\n"
6602 ) h;
6603 Buffer.add_string bb "</keymap>";
6606 loop rest
6608 loop c.keyhashes;
6612 let save () =
6613 let uifontsize = fstate.fontsize in
6614 let bb = Buffer.create 32768 in
6615 let f (h, dc) =
6616 let dc = if conf.bedefault then conf else dc in
6617 Buffer.add_string bb "<llppconfig>\n";
6619 if String.length !fontpath > 0
6620 then
6621 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6622 uifontsize
6623 !fontpath
6624 else (
6625 if uifontsize <> 14
6626 then
6627 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6630 Buffer.add_string bb "<defaults ";
6631 add_attrs bb true dc dc;
6632 let kb = keymapsbuf true dc dc in
6633 if Buffer.length kb > 0
6634 then (
6635 Buffer.add_string bb ">\n";
6636 Buffer.add_buffer bb kb;
6637 Buffer.add_string bb "\n</defaults>\n";
6639 else Buffer.add_string bb "/>\n";
6641 let adddoc path pan anchor c bookmarks =
6642 if bookmarks == [] && c = dc && anchor = emptyanchor
6643 then ()
6644 else (
6645 Printf.bprintf bb "<doc path='%s'"
6646 (enent path 0 (String.length path));
6648 if anchor <> emptyanchor
6649 then (
6650 let n, rely, visy = anchor in
6651 Printf.bprintf bb " page='%d'" n;
6652 if rely > 1e-6
6653 then
6654 Printf.bprintf bb " rely='%f'" rely
6656 if abs_float visy > 1e-6
6657 then
6658 Printf.bprintf bb " visy='%f'" visy
6662 if pan != 0
6663 then Printf.bprintf bb " pan='%d'" pan;
6665 add_attrs bb false dc c;
6666 let kb = keymapsbuf false dc c in
6668 begin match bookmarks with
6669 | [] ->
6670 if Buffer.length kb > 0
6671 then (
6672 Buffer.add_string bb ">\n";
6673 Buffer.add_buffer bb kb;
6674 Buffer.add_string bb "\n</doc>\n";
6676 else Buffer.add_string bb "/>\n"
6677 | _ ->
6678 Buffer.add_string bb ">\n<bookmarks>\n";
6679 List.iter (fun (title, _level, (page, rely, visy)) ->
6680 Printf.bprintf bb
6681 "<item title='%s' page='%d'"
6682 (enent title 0 (String.length title))
6683 page
6685 if rely > 1e-6
6686 then
6687 Printf.bprintf bb " rely='%f'" rely
6689 if abs_float visy > 1e-6
6690 then
6691 Printf.bprintf bb " visy='%f'" visy
6693 Buffer.add_string bb "/>\n";
6694 ) bookmarks;
6695 Buffer.add_string bb "</bookmarks>";
6696 if Buffer.length kb > 0
6697 then (
6698 Buffer.add_string bb "\n";
6699 Buffer.add_buffer bb kb;
6701 Buffer.add_string bb "\n</doc>\n";
6702 end;
6706 let pan, conf =
6707 match state.mode with
6708 | Birdseye (c, pan, _, _, _) ->
6709 let beyecolumns =
6710 match conf.columns with
6711 | Cmulti ((c, _, _), _) -> Some c
6712 | Csingle _ -> None
6713 | Csplit _ -> None
6714 and columns =
6715 match c.columns with
6716 | Cmulti (c, _) -> Cmulti (c, [||])
6717 | Csingle _ -> Csingle [||]
6718 | Csplit _ -> failwith "quit from bird's eye while split"
6720 pan, { c with beyecolumns = beyecolumns; columns = columns }
6721 | _ -> state.x, conf
6723 let basename = Filename.basename state.path in
6724 adddoc basename pan (getanchor ())
6725 (let conf =
6726 let autoscrollstep =
6727 match state.autoscroll with
6728 | Some step -> step
6729 | None -> conf.autoscrollstep
6731 match state.mode with
6732 | Birdseye (bc, _, _, _, _) ->
6733 { conf with
6734 zoom = bc.zoom;
6735 presentation = bc.presentation;
6736 interpagespace = bc.interpagespace;
6737 maxwait = bc.maxwait;
6738 autoscrollstep = autoscrollstep }
6739 | _ -> { conf with autoscrollstep = autoscrollstep }
6740 in conf)
6741 (if conf.savebmarks then state.bookmarks else []);
6743 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6744 if basename <> path
6745 then adddoc path x anchor c bookmarks
6746 ) h;
6747 Buffer.add_string bb "</llppconfig>";
6749 load1 f;
6750 if Buffer.length bb > 0
6751 then
6753 let tmp = !confpath ^ ".tmp" in
6754 let oc = open_out_bin tmp in
6755 Buffer.output_buffer oc bb;
6756 close_out oc;
6757 Unix.rename tmp !confpath;
6758 with exn ->
6759 prerr_endline
6760 ("error while saving configuration: " ^ Printexc.to_string exn)
6762 end;;
6764 let () =
6765 let trimcachepath = ref "" in
6766 Arg.parse
6767 (Arg.align
6768 [("-p", Arg.String (fun s -> state.password <- s) ,
6769 "<password> Set password");
6771 ("-f", Arg.String (fun s -> Config.fontpath := s),
6772 "<path> Set path to the user interface font");
6774 ("-c", Arg.String (fun s -> Config.confpath := s),
6775 "<path> Set path to the configuration file");
6777 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6778 "<path> Set path to the trim cache file");
6780 ("-v", Arg.Unit (fun () ->
6781 Printf.printf
6782 "%s\nconfiguration path: %s\n"
6783 (version ())
6784 Config.defconfpath
6786 exit 0), " Print version and exit");
6789 (fun s -> state.path <- s)
6790 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6792 if String.length state.path = 0
6793 then (prerr_endline "file name missing"; exit 1);
6795 Config.load ();
6797 let globalkeyhash = findkeyhash conf "global" in
6798 let wsfd, winw, winh = Wsi.init (object
6799 method expose =
6800 if nogeomcmds state.geomcmds || platform == Posx
6801 then display ()
6802 else (
6803 GlClear.color (scalecolor2 conf.bgcolor);
6804 GlClear.clear [`color];
6806 method display = display ()
6807 method reshape w h = reshape w h
6808 method mouse b d x y m = mouse b d x y m
6809 method motion x y = state.mpos <- (x, y); motion x y
6810 method pmotion x y = state.mpos <- (x, y); pmotion x y
6811 method key k m =
6812 let mascm = m land (
6813 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6814 ) in
6815 match state.keystate with
6816 | KSnone ->
6817 let km = k, mascm in
6818 begin
6819 match
6820 let modehash = state.uioh#modehash in
6821 try Hashtbl.find modehash km
6822 with Not_found ->
6823 try Hashtbl.find globalkeyhash km
6824 with Not_found -> KMinsrt (k, m)
6825 with
6826 | KMinsrt (k, m) -> keyboard k m
6827 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6828 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6830 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6831 List.iter (fun (k, m) -> keyboard k m) insrt;
6832 state.keystate <- KSnone
6833 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6834 state.keystate <- KSinto (keys, insrt)
6835 | _ ->
6836 state.keystate <- KSnone
6838 method enter x y = state.mpos <- (x, y); pmotion x y
6839 method leave = state.mpos <- (-1, -1)
6840 method quit = raise Quit
6841 end) conf.winw conf.winh (platform = Posx) in
6843 state.wsfd <- wsfd;
6845 if not (
6846 List.exists GlMisc.check_extension
6847 [ "GL_ARB_texture_rectangle"
6848 ; "GL_EXT_texture_recangle"
6849 ; "GL_NV_texture_rectangle" ]
6851 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6853 let cr, sw =
6854 match Ne.pipe () with
6855 | Ne.Exn exn ->
6856 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6857 exit 1
6858 | Ne.Res rw -> rw
6859 and sr, cw =
6860 match Ne.pipe () with
6861 | Ne.Exn exn ->
6862 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6863 exit 1
6864 | Ne.Res rw -> rw
6867 cloexec cr;
6868 cloexec sw;
6869 cloexec sr;
6870 cloexec cw;
6872 setcheckers conf.checkers;
6873 redirectstderr ();
6875 init (cr, cw) (
6876 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6877 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6878 !Config.fontpath, !trimcachepath
6880 state.sr <- sr;
6881 state.sw <- sw;
6882 state.text <- "Opening " ^ state.path;
6883 reshape winw winh;
6884 opendoc state.path state.password;
6885 state.uioh <- uioh;
6887 let rec loop deadline =
6888 let r =
6889 match state.errfd with
6890 | None -> [state.sr; state.wsfd]
6891 | Some fd -> [state.sr; state.wsfd; fd]
6893 if state.redisplay
6894 then (
6895 state.redisplay <- false;
6896 display ();
6898 let timeout =
6899 let now = now () in
6900 if deadline > now
6901 then (
6902 if deadline = infinity
6903 then ~-.1.0
6904 else max 0.0 (deadline -. now)
6906 else 0.0
6908 let r, _, _ =
6909 try Unix.select r [] [] timeout
6910 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6912 begin match r with
6913 | [] ->
6914 state.ghyll None;
6915 let newdeadline =
6916 if state.ghyll == noghyll
6917 then
6918 match state.autoscroll with
6919 | Some step when step != 0 ->
6920 let y = state.y + step in
6921 let y =
6922 if y < 0
6923 then state.maxy
6924 else if y >= state.maxy then 0 else y
6926 gotoy y;
6927 if state.mode = View
6928 then state.text <- "";
6929 deadline +. 0.01
6930 | _ -> infinity
6931 else deadline +. 0.01
6933 loop newdeadline
6935 | l ->
6936 let rec checkfds = function
6937 | [] -> ()
6938 | fd :: rest when fd = state.sr ->
6939 let cmd = readcmd state.sr in
6940 act cmd;
6941 checkfds rest
6943 | fd :: rest when fd = state.wsfd ->
6944 Wsi.readresp fd;
6945 checkfds rest
6947 | fd :: rest ->
6948 let s = String.create 80 in
6949 let n = Unix.read fd s 0 80 in
6950 if conf.redirectstderr
6951 then (
6952 Buffer.add_substring state.errmsgs s 0 n;
6953 state.newerrmsgs <- true;
6954 state.redisplay <- true;
6956 else (
6957 prerr_string (String.sub s 0 n);
6958 flush stderr;
6960 checkfds rest
6962 checkfds l;
6963 let newdeadline =
6964 let deadline1 =
6965 if deadline = infinity
6966 then now () +. 0.01
6967 else deadline
6969 match state.autoscroll with
6970 | Some step when step != 0 -> deadline1
6971 | _ -> if state.ghyll == noghyll then infinity else deadline1
6973 loop newdeadline
6974 end;
6977 loop infinity;
6978 with Quit ->
6979 Config.save ();