Only accept one as cover A/B
[llpp.git] / main.ml
blob4275461934143f207b9e2f30b1b47ab4b77c8abf
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 enterinfomode =
3888 let btos b = if b then "\xe2\x88\x9a" else "" in
3889 let showextended = ref false in
3890 let leave mode = function
3891 | Confirm -> state.mode <- mode
3892 | Cancel -> state.mode <- mode in
3893 let src =
3894 (object
3895 val mutable m_first_time = true
3896 val mutable m_l = []
3897 val mutable m_a = [||]
3898 val mutable m_prev_uioh = nouioh
3899 val mutable m_prev_mode = View
3901 inherit lvsourcebase
3903 method reset prev_mode prev_uioh =
3904 m_a <- Array.of_list (List.rev m_l);
3905 m_l <- [];
3906 m_prev_mode <- prev_mode;
3907 m_prev_uioh <- prev_uioh;
3908 if m_first_time
3909 then (
3910 let rec loop n =
3911 if n >= Array.length m_a
3912 then ()
3913 else
3914 match m_a.(n) with
3915 | _, _, _, Action _ -> m_active <- n
3916 | _ -> loop (n+1)
3918 loop 0;
3919 m_first_time <- false;
3922 method int name get set =
3923 m_l <-
3924 (name, `int get, 1, Action (
3925 fun u ->
3926 let ondone s =
3927 try set (int_of_string s)
3928 with exn ->
3929 state.text <- Printf.sprintf "bad integer `%s': %s"
3930 s (Printexc.to_string exn)
3932 state.text <- "";
3933 let te = name ^ ": ", "", None, intentry, ondone, true in
3934 state.mode <- Textentry (te, leave m_prev_mode);
3936 )) :: m_l
3938 method int_with_suffix name get set =
3939 m_l <-
3940 (name, `intws get, 1, Action (
3941 fun u ->
3942 let ondone s =
3943 try set (int_of_string_with_suffix s)
3944 with exn ->
3945 state.text <- Printf.sprintf "bad integer `%s': %s"
3946 s (Printexc.to_string exn)
3948 state.text <- "";
3949 let te =
3950 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3952 state.mode <- Textentry (te, leave m_prev_mode);
3954 )) :: m_l
3956 method bool ?(offset=1) ?(btos=btos) name get set =
3957 m_l <-
3958 (name, `bool (btos, get), offset, Action (
3959 fun u ->
3960 let v = get () in
3961 set (not v);
3963 )) :: m_l
3965 method color name get set =
3966 m_l <-
3967 (name, `color get, 1, Action (
3968 fun u ->
3969 let invalid = (nan, nan, nan) in
3970 let ondone s =
3971 let c =
3972 try color_of_string s
3973 with exn ->
3974 state.text <- Printf.sprintf "bad color `%s': %s"
3975 s (Printexc.to_string exn);
3976 invalid
3978 if c <> invalid
3979 then set c;
3981 let te = name ^ ": ", "", None, textentry, ondone, true in
3982 state.text <- color_to_string (get ());
3983 state.mode <- Textentry (te, leave m_prev_mode);
3985 )) :: m_l
3987 method string name get set =
3988 m_l <-
3989 (name, `string get, 1, Action (
3990 fun u ->
3991 let ondone s = set s in
3992 let te = name ^ ": ", "", None, textentry, ondone, true in
3993 state.mode <- Textentry (te, leave m_prev_mode);
3995 )) :: m_l
3997 method colorspace name get set =
3998 m_l <-
3999 (name, `string get, 1, Action (
4000 fun _ ->
4001 let source =
4002 let vals = [| "rgb"; "bgr"; "gray" |] in
4003 (object
4004 inherit lvsourcebase
4006 initializer
4007 m_active <- int_of_colorspace conf.colorspace;
4008 m_first <- 0;
4010 method getitemcount = Array.length vals
4011 method getitem n = (vals.(n), 0)
4012 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4013 ignore (uioh, first, pan, qsearch);
4014 if not cancel then set active;
4015 None
4016 method hasaction _ = true
4017 end)
4019 state.text <- "";
4020 let modehash = findkeyhash conf "info" in
4021 coe (new listview ~source ~trusted:true ~modehash)
4022 )) :: m_l
4024 method caption s offset =
4025 m_l <- (s, `empty, offset, Noaction) :: m_l
4027 method caption2 s f offset =
4028 m_l <- (s, `string f, offset, Noaction) :: m_l
4030 method getitemcount = Array.length m_a
4032 method getitem n =
4033 let tostr = function
4034 | `int f -> string_of_int (f ())
4035 | `intws f -> string_with_suffix_of_int (f ())
4036 | `string f -> f ()
4037 | `color f -> color_to_string (f ())
4038 | `bool (btos, f) -> btos (f ())
4039 | `empty -> ""
4041 let name, t, offset, _ = m_a.(n) in
4042 ((let s = tostr t in
4043 if String.length s > 0
4044 then Printf.sprintf "%s\t%s" name s
4045 else name),
4046 offset)
4048 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4049 let uiohopt =
4050 if not cancel
4051 then (
4052 m_qsearch <- qsearch;
4053 let uioh =
4054 match m_a.(active) with
4055 | _, _, _, Action f -> f uioh
4056 | _ -> uioh
4058 Some uioh
4060 else None
4062 m_active <- active;
4063 m_first <- first;
4064 m_pan <- pan;
4065 uiohopt
4067 method hasaction n =
4068 match m_a.(n) with
4069 | _, _, _, Action _ -> true
4070 | _ -> false
4071 end)
4073 let rec fillsrc prevmode prevuioh =
4074 let sep () = src#caption "" 0 in
4075 let colorp name get set =
4076 src#string name
4077 (fun () -> color_to_string (get ()))
4078 (fun v ->
4080 let c = color_of_string v in
4081 set c
4082 with exn ->
4083 state.text <- Printf.sprintf "bad color `%s': %s"
4084 v (Printexc.to_string exn);
4087 let oldmode = state.mode in
4088 let birdseye = isbirdseye state.mode in
4090 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4092 src#bool "presentation mode"
4093 (fun () -> conf.presentation)
4094 (fun v ->
4095 conf.presentation <- v;
4096 state.anchor <- getanchor ();
4097 represent ());
4099 src#bool "ignore case in searches"
4100 (fun () -> conf.icase)
4101 (fun v -> conf.icase <- v);
4103 src#bool "preload"
4104 (fun () -> conf.preload)
4105 (fun v -> conf.preload <- v);
4107 src#bool "highlight links"
4108 (fun () -> conf.hlinks)
4109 (fun v -> conf.hlinks <- v);
4111 src#bool "under info"
4112 (fun () -> conf.underinfo)
4113 (fun v -> conf.underinfo <- v);
4115 src#bool "persistent bookmarks"
4116 (fun () -> conf.savebmarks)
4117 (fun v -> conf.savebmarks <- v);
4119 src#bool "proportional display"
4120 (fun () -> conf.proportional)
4121 (fun v -> reqlayout conf.angle v);
4123 src#bool "trim margins"
4124 (fun () -> conf.trimmargins)
4125 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4127 src#bool "persistent location"
4128 (fun () -> conf.jumpback)
4129 (fun v -> conf.jumpback <- v);
4131 sep ();
4132 src#int "inter-page space"
4133 (fun () -> conf.interpagespace)
4134 (fun n ->
4135 conf.interpagespace <- n;
4136 docolumns conf.columns;
4137 let pageno, py =
4138 match state.layout with
4139 | [] -> 0, 0
4140 | l :: _ ->
4141 l.pageno, l.pagey
4143 state.maxy <- calcheight ();
4144 let y = getpagey pageno in
4145 gotoy (y + py)
4148 src#int "page bias"
4149 (fun () -> conf.pagebias)
4150 (fun v -> conf.pagebias <- v);
4152 src#int "scroll step"
4153 (fun () -> conf.scrollstep)
4154 (fun n -> conf.scrollstep <- n);
4156 src#int "horizontal scroll step"
4157 (fun () -> conf.hscrollstep)
4158 (fun v -> conf.hscrollstep <- v);
4160 src#int "auto scroll step"
4161 (fun () ->
4162 match state.autoscroll with
4163 | Some step -> step
4164 | _ -> conf.autoscrollstep)
4165 (fun n ->
4166 if state.autoscroll <> None
4167 then state.autoscroll <- Some n;
4168 conf.autoscrollstep <- n);
4170 src#int "zoom"
4171 (fun () -> truncate (conf.zoom *. 100.))
4172 (fun v -> setzoom ((float v) /. 100.));
4174 src#int "rotation"
4175 (fun () -> conf.angle)
4176 (fun v -> reqlayout v conf.proportional);
4178 src#int "scroll bar width"
4179 (fun () -> state.scrollw)
4180 (fun v ->
4181 state.scrollw <- v;
4182 conf.scrollbw <- v;
4183 reshape conf.winw conf.winh;
4186 src#int "scroll handle height"
4187 (fun () -> conf.scrollh)
4188 (fun v -> conf.scrollh <- v;);
4190 src#int "thumbnail width"
4191 (fun () -> conf.thumbw)
4192 (fun v ->
4193 conf.thumbw <- min 4096 v;
4194 match oldmode with
4195 | Birdseye beye ->
4196 leavebirdseye beye false;
4197 enterbirdseye ()
4198 | _ -> ()
4201 let mode = state.mode in
4202 src#string "columns"
4203 (fun () ->
4204 match conf.columns with
4205 | Csingle _ -> "1"
4206 | Cmulti (multi, _) -> multicolumns_to_string multi
4207 | Csplit (count, _) -> "-" ^ string_of_int count
4209 (fun v ->
4210 let n, a, b = multicolumns_of_string v in
4211 setcolumns mode n a b);
4213 sep ();
4214 src#caption "Presentation mode" 0;
4215 src#bool "scrollbar visible"
4216 (fun () -> conf.scrollbarinpm)
4217 (fun v ->
4218 if v != conf.scrollbarinpm
4219 then (
4220 conf.scrollbarinpm <- v;
4221 if conf.presentation
4222 then (
4223 state.scrollw <- if v then conf.scrollbw else 0;
4224 reshape conf.winw conf.winh;
4229 sep ();
4230 src#caption "Pixmap cache" 0;
4231 src#int_with_suffix "size (advisory)"
4232 (fun () -> conf.memlimit)
4233 (fun v -> conf.memlimit <- v);
4235 src#caption2 "used"
4236 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4237 (string_with_suffix_of_int state.memused)
4238 (Hashtbl.length state.tilemap)) 1;
4240 sep ();
4241 src#caption "Layout" 0;
4242 src#caption2 "Dimension"
4243 (fun () ->
4244 Printf.sprintf "%dx%d (virtual %dx%d)"
4245 conf.winw conf.winh
4246 state.w state.maxy)
4248 if conf.debug
4249 then
4250 src#caption2 "Position" (fun () ->
4251 Printf.sprintf "%dx%d" state.x state.y
4253 else
4254 src#caption2 "Visible" (fun () -> describe_location ()) 1
4257 sep ();
4258 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4259 "Save these parameters as global defaults at exit"
4260 (fun () -> conf.bedefault)
4261 (fun v -> conf.bedefault <- v)
4264 sep ();
4265 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4266 src#bool ~offset:0 ~btos "Extended parameters"
4267 (fun () -> !showextended)
4268 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4269 if !showextended
4270 then (
4271 src#bool "checkers"
4272 (fun () -> conf.checkers)
4273 (fun v -> conf.checkers <- v; setcheckers v);
4274 src#bool "update cursor"
4275 (fun () -> conf.updatecurs)
4276 (fun v -> conf.updatecurs <- v);
4277 src#bool "verbose"
4278 (fun () -> conf.verbose)
4279 (fun v -> conf.verbose <- v);
4280 src#bool "invert colors"
4281 (fun () -> conf.invert)
4282 (fun v -> conf.invert <- v);
4283 src#bool "max fit"
4284 (fun () -> conf.maxhfit)
4285 (fun v -> conf.maxhfit <- v);
4286 src#bool "redirect stderr"
4287 (fun () -> conf.redirectstderr)
4288 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4289 src#string "uri launcher"
4290 (fun () -> conf.urilauncher)
4291 (fun v -> conf.urilauncher <- v);
4292 src#string "path launcher"
4293 (fun () -> conf.pathlauncher)
4294 (fun v -> conf.pathlauncher <- v);
4295 src#string "tile size"
4296 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4297 (fun v ->
4299 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4300 conf.tilew <- max 64 w;
4301 conf.tileh <- max 64 h;
4302 flushtiles ();
4303 with exn ->
4304 state.text <- Printf.sprintf "bad tile size `%s': %s"
4305 v (Printexc.to_string exn));
4306 src#int "texture count"
4307 (fun () -> conf.texcount)
4308 (fun v ->
4309 if realloctexts v
4310 then conf.texcount <- v
4311 else showtext '!' " Failed to set texture count please retry later"
4313 src#int "slice height"
4314 (fun () -> conf.sliceheight)
4315 (fun v ->
4316 conf.sliceheight <- v;
4317 wcmd "sliceh %d" conf.sliceheight;
4319 src#int "anti-aliasing level"
4320 (fun () -> conf.aalevel)
4321 (fun v ->
4322 conf.aalevel <- bound v 0 8;
4323 state.anchor <- getanchor ();
4324 opendoc state.path state.password;
4326 src#string "page scroll scaling factor"
4327 (fun () -> string_of_float conf.pgscale)
4328 (fun v ->
4330 let s = float_of_string v in
4331 conf.pgscale <- s
4332 with exn ->
4333 state.text <- Printf.sprintf
4334 "bad page scroll scaling factor `%s': %s"
4335 v (Printexc.to_string exn)
4338 src#int "ui font size"
4339 (fun () -> fstate.fontsize)
4340 (fun v -> setfontsize (bound v 5 100));
4341 src#int "hint font size"
4342 (fun () -> conf.hfsize)
4343 (fun v -> conf.hfsize <- bound v 5 100);
4344 colorp "background color"
4345 (fun () -> conf.bgcolor)
4346 (fun v -> conf.bgcolor <- v);
4347 src#bool "crop hack"
4348 (fun () -> conf.crophack)
4349 (fun v -> conf.crophack <- v);
4350 src#bool "multi column centering"
4351 (fun () -> conf.multicenter)
4352 (fun v -> conf.multicenter <- v; represent ());
4353 src#string "trim fuzz"
4354 (fun () -> irect_to_string conf.trimfuzz)
4355 (fun v ->
4357 conf.trimfuzz <- irect_of_string v;
4358 if conf.trimmargins
4359 then settrim true conf.trimfuzz;
4360 with exn ->
4361 state.text <- Printf.sprintf "bad irect `%s': %s"
4362 v (Printexc.to_string exn)
4364 src#string "throttle"
4365 (fun () ->
4366 match conf.maxwait with
4367 | None -> "show place holder if page is not ready"
4368 | Some time ->
4369 if time = infinity
4370 then "wait for page to fully render"
4371 else
4372 "wait " ^ string_of_float time
4373 ^ " seconds before showing placeholder"
4375 (fun v ->
4377 let f = float_of_string v in
4378 if f <= 0.0
4379 then conf.maxwait <- None
4380 else conf.maxwait <- Some f
4381 with exn ->
4382 state.text <- Printf.sprintf "bad time `%s': %s"
4383 v (Printexc.to_string exn)
4385 src#string "ghyll scroll"
4386 (fun () ->
4387 match conf.ghyllscroll with
4388 | None -> ""
4389 | Some nab -> ghyllscroll_to_string nab
4391 (fun v ->
4393 let gs =
4394 if String.length v = 0
4395 then None
4396 else Some (ghyllscroll_of_string v)
4398 conf.ghyllscroll <- gs
4399 with exn ->
4400 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4401 v (Printexc.to_string exn)
4403 src#string "selection command"
4404 (fun () -> conf.selcmd)
4405 (fun v -> conf.selcmd <- v);
4406 src#colorspace "color space"
4407 (fun () -> colorspace_to_string conf.colorspace)
4408 (fun v ->
4409 conf.colorspace <- colorspace_of_int v;
4410 wcmd "cs %d" v;
4411 load state.layout;
4415 sep ();
4416 src#caption "Document" 0;
4417 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4418 src#caption2 "Pages"
4419 (fun () -> string_of_int state.pagecount) 1;
4420 src#caption2 "Dimensions"
4421 (fun () -> string_of_int (List.length state.pdims)) 1;
4422 if conf.trimmargins
4423 then (
4424 sep ();
4425 src#caption "Trimmed margins" 0;
4426 src#caption2 "Dimensions"
4427 (fun () -> string_of_int (List.length state.pdims)) 1;
4430 sep ();
4431 src#caption "OpenGL" 0;
4432 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4433 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4434 src#reset prevmode prevuioh;
4436 fun () ->
4437 state.text <- "";
4438 let prevmode = state.mode
4439 and prevuioh = state.uioh in
4440 fillsrc prevmode prevuioh;
4441 let source = (src :> lvsource) in
4442 let modehash = findkeyhash conf "info" in
4443 state.uioh <- coe (object (self)
4444 inherit listview ~source ~trusted:true ~modehash as super
4445 val mutable m_prevmemused = 0
4446 method infochanged = function
4447 | Memused ->
4448 if m_prevmemused != state.memused
4449 then (
4450 m_prevmemused <- state.memused;
4451 G.postRedisplay "memusedchanged";
4453 | Pdim -> G.postRedisplay "pdimchanged"
4454 | Docinfo -> fillsrc prevmode prevuioh
4456 method key key mask =
4457 if not (Wsi.withctrl mask)
4458 then
4459 match key with
4460 | 0xff51 -> coe (self#updownlevel ~-1)
4461 | 0xff53 -> coe (self#updownlevel 1)
4462 | _ -> super#key key mask
4463 else super#key key mask
4464 end);
4465 G.postRedisplay "info";
4468 let enterhelpmode =
4469 let source =
4470 (object
4471 inherit lvsourcebase
4472 method getitemcount = Array.length state.help
4473 method getitem n =
4474 let s, n, _ = state.help.(n) in
4475 (s, n)
4477 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4478 let optuioh =
4479 if not cancel
4480 then (
4481 m_qsearch <- qsearch;
4482 match state.help.(active) with
4483 | _, _, Action f -> Some (f uioh)
4484 | _ -> Some (uioh)
4486 else None
4488 m_active <- active;
4489 m_first <- first;
4490 m_pan <- pan;
4491 optuioh
4493 method hasaction n =
4494 match state.help.(n) with
4495 | _, _, Action _ -> true
4496 | _ -> false
4498 initializer
4499 m_active <- -1
4500 end)
4501 in fun () ->
4502 let modehash = findkeyhash conf "help" in
4503 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4504 G.postRedisplay "help";
4507 let entermsgsmode =
4508 let msgsource =
4509 let re = Str.regexp "[\r\n]" in
4510 (object
4511 inherit lvsourcebase
4512 val mutable m_items = [||]
4514 method getitemcount = 1 + Array.length m_items
4516 method getitem n =
4517 if n = 0
4518 then "[Clear]", 0
4519 else m_items.(n-1), 0
4521 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4522 ignore uioh;
4523 if not cancel
4524 then (
4525 if active = 0
4526 then Buffer.clear state.errmsgs;
4527 m_qsearch <- qsearch;
4529 m_active <- active;
4530 m_first <- first;
4531 m_pan <- pan;
4532 None
4534 method hasaction n =
4535 n = 0
4537 method reset =
4538 state.newerrmsgs <- false;
4539 let l = Str.split re (Buffer.contents state.errmsgs) in
4540 m_items <- Array.of_list l
4542 initializer
4543 m_active <- 0
4544 end)
4545 in fun () ->
4546 state.text <- "";
4547 msgsource#reset;
4548 let source = (msgsource :> lvsource) in
4549 let modehash = findkeyhash conf "listview" in
4550 state.uioh <- coe (object
4551 inherit listview ~source ~trusted:false ~modehash as super
4552 method display =
4553 if state.newerrmsgs
4554 then msgsource#reset;
4555 super#display
4556 end);
4557 G.postRedisplay "msgs";
4560 let quickbookmark ?title () =
4561 match state.layout with
4562 | [] -> ()
4563 | l :: _ ->
4564 let title =
4565 match title with
4566 | None ->
4567 let sec = Unix.gettimeofday () in
4568 let tm = Unix.localtime sec in
4569 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4570 (l.pageno+1)
4571 tm.Unix.tm_mday
4572 tm.Unix.tm_mon
4573 (tm.Unix.tm_year + 1900)
4574 tm.Unix.tm_hour
4575 tm.Unix.tm_min
4576 | Some title -> title
4578 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4581 let doreshape w h =
4582 state.fullscreen <- None;
4583 Wsi.reshape w h;
4586 let setautoscrollspeed step goingdown =
4587 let incr = max 1 ((abs step) / 2) in
4588 let incr = if goingdown then incr else -incr in
4589 let astep = step + incr in
4590 state.autoscroll <- Some astep;
4593 let gotounder = function
4594 | Ulinkgoto (pageno, top) ->
4595 if pageno >= 0
4596 then (
4597 addnav ();
4598 gotopage1 pageno top;
4601 | Ulinkuri s ->
4602 gotouri s
4604 | Uremote (filename, pageno) ->
4605 let path =
4606 if Sys.file_exists filename
4607 then filename
4608 else
4609 let dir = Filename.dirname state.path in
4610 let path = Filename.concat dir filename in
4611 if Sys.file_exists path
4612 then path
4613 else ""
4615 if String.length path > 0
4616 then (
4617 let anchor = getanchor () in
4618 let ranchor = state.path, state.password, anchor in
4619 state.anchor <- (pageno, 0.0, 0.0);
4620 state.ranchors <- ranchor :: state.ranchors;
4621 opendoc path "";
4623 else showtext '!' ("Could not find " ^ filename)
4625 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4628 let canpan () =
4629 match conf.columns with
4630 | Csplit _ -> true
4631 | _ -> conf.zoom > 1.0
4634 let viewkeyboard key mask =
4635 let enttext te =
4636 let mode = state.mode in
4637 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4638 state.text <- "";
4639 enttext ();
4640 G.postRedisplay "view:enttext"
4642 let ctrl = Wsi.withctrl mask in
4643 match key with
4644 | 81 -> (* Q *)
4645 exit 0
4647 | 0xff63 -> (* insert *)
4648 if conf.angle mod 360 = 0
4649 then (
4650 state.mode <- LinkNav (Ltgendir 0);
4651 gotoy state.y;
4653 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4655 | 0xff1b | 113 -> (* escape / q *)
4656 begin match state.mstate with
4657 | Mzoomrect _ ->
4658 state.mstate <- Mnone;
4659 Wsi.setcursor Wsi.CURSOR_INHERIT;
4660 G.postRedisplay "kill zoom rect";
4661 | _ ->
4662 match state.ranchors with
4663 | [] -> raise Quit
4664 | (path, password, anchor) :: rest ->
4665 state.ranchors <- rest;
4666 state.anchor <- anchor;
4667 opendoc path password
4668 end;
4670 | 0xff08 -> (* backspace *)
4671 let y = getnav ~-1 in
4672 gotoy_and_clear_text y
4674 | 111 -> (* o *)
4675 enteroutlinemode ()
4677 | 117 -> (* u *)
4678 state.rects <- [];
4679 state.text <- "";
4680 G.postRedisplay "dehighlight";
4682 | 47 | 63 -> (* / ? *)
4683 let ondone isforw s =
4684 cbput state.hists.pat s;
4685 state.searchpattern <- s;
4686 search s isforw
4688 let s = String.create 1 in
4689 s.[0] <- Char.chr key;
4690 enttext (s, "", Some (onhist state.hists.pat),
4691 textentry, ondone (key = 47), true)
4693 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4694 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4695 setzoom (conf.zoom +. incr)
4697 | 43 | 0xffab -> (* + *)
4698 let ondone s =
4699 let n =
4700 try int_of_string s with exc ->
4701 state.text <- Printf.sprintf "bad integer `%s': %s"
4702 s (Printexc.to_string exc);
4703 max_int
4705 if n != max_int
4706 then (
4707 conf.pagebias <- n;
4708 state.text <- "page bias is now " ^ string_of_int n;
4711 enttext ("page bias: ", "", None, intentry, ondone, true)
4713 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4714 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4715 setzoom (max 0.01 (conf.zoom -. decr))
4717 | 45 | 0xffad -> (* - *)
4718 let ondone msg = state.text <- msg in
4719 enttext (
4720 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4721 optentry state.mode, ondone, true
4724 | 48 when ctrl -> (* ctrl-0 *)
4725 setzoom 1.0
4727 | 49 when ctrl -> (* ctrl-1 *)
4728 let cols =
4729 match conf.columns with
4730 | Csingle _ | Cmulti _ -> 1
4731 | Csplit (n, _) -> n
4733 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4734 if zoom < 1.0
4735 then setzoom zoom
4737 | 0xffc6 -> (* f9 *)
4738 togglebirdseye ()
4740 | 57 when ctrl -> (* ctrl-9 *)
4741 togglebirdseye ()
4743 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4744 when not ctrl -> (* 0..9 *)
4745 let ondone s =
4746 let n =
4747 try int_of_string s with exc ->
4748 state.text <- Printf.sprintf "bad integer `%s': %s"
4749 s (Printexc.to_string exc);
4752 if n >= 0
4753 then (
4754 addnav ();
4755 cbput state.hists.pag (string_of_int n);
4756 gotopage1 (n + conf.pagebias - 1) 0;
4759 let pageentry text key =
4760 match Char.unsafe_chr key with
4761 | 'g' -> TEdone text
4762 | _ -> intentry text key
4764 let text = "x" in text.[0] <- Char.chr key;
4765 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4767 | 98 -> (* b *)
4768 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4769 reshape conf.winw conf.winh;
4771 | 108 -> (* l *)
4772 conf.hlinks <- not conf.hlinks;
4773 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4774 G.postRedisplay "toggle highlightlinks";
4776 | 70 -> (* F *)
4777 state.glinks <- true;
4778 let mode = state.mode in
4779 state.mode <- Textentry (
4780 (":", "", None, linknentry, linkndone (fun under ->
4781 addnav ();
4782 gotounder under
4783 ), false
4784 ), fun _ ->
4785 state.glinks <- false;
4786 state.mode <- mode
4788 state.text <- "";
4789 G.postRedisplay "view:linkent(F)"
4791 | 121 -> (* y *)
4792 state.glinks <- true;
4793 let mode = state.mode in
4794 state.mode <- Textentry (
4795 (":", "", None, linknentry, linkndone (fun under ->
4796 match Ne.pipe () with
4797 | Ne.Exn exn ->
4798 showtext '!' (Printf.sprintf "pipe failed: %s"
4799 (Printexc.to_string exn));
4800 | Ne.Res (r, w) ->
4801 let popened =
4802 try popen conf.selcmd [r, 0; w, -1]; true
4803 with exn ->
4804 showtext '!'
4805 (Printf.sprintf "failed to execute %s: %s"
4806 conf.selcmd (Printexc.to_string exn));
4807 false
4809 let clo cap fd =
4810 Ne.clo fd (fun msg ->
4811 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4814 let s = undertext under in
4815 if popened
4816 then
4817 (try
4818 let l = String.length s in
4819 let n = Unix.write w s 0 l in
4820 if n != l
4821 then
4822 showtext '!'
4823 (Printf.sprintf
4824 "failed to write %d characters to sel pipe, wrote %d"
4827 with exn ->
4828 showtext '!'
4829 (Printf.sprintf "failed to write to sel pipe: %s"
4830 (Printexc.to_string exn)
4833 else dolog "%s" s;
4834 clo "pipe/r" r;
4835 clo "pipe/w" w;
4836 ), false
4838 fun _ ->
4839 state.glinks <- false;
4840 state.mode <- mode
4842 state.text <- "";
4843 G.postRedisplay "view:linkent"
4845 | 97 -> (* a *)
4846 begin match state.autoscroll with
4847 | Some step ->
4848 conf.autoscrollstep <- step;
4849 state.autoscroll <- None
4850 | None ->
4851 if conf.autoscrollstep = 0
4852 then state.autoscroll <- Some 1
4853 else state.autoscroll <- Some conf.autoscrollstep
4856 | 112 when ctrl -> (* ctrl-p *)
4857 launchpath ()
4859 | 80 -> (* P *)
4860 conf.presentation <- not conf.presentation;
4861 if conf.presentation
4862 then (
4863 if not conf.scrollbarinpm
4864 then state.scrollw <- 0;
4866 else
4867 state.scrollw <- conf.scrollbw;
4869 showtext ' ' ("presentation mode " ^
4870 if conf.presentation then "on" else "off");
4871 state.anchor <- getanchor ();
4872 represent ()
4874 | 102 -> (* f *)
4875 begin match state.fullscreen with
4876 | None ->
4877 state.fullscreen <- Some (conf.winw, conf.winh);
4878 Wsi.fullscreen ()
4879 | Some (w, h) ->
4880 state.fullscreen <- None;
4881 doreshape w h
4884 | 103 -> (* g *)
4885 gotoy_and_clear_text 0
4887 | 71 -> (* G *)
4888 gotopage1 (state.pagecount - 1) 0
4890 | 112 | 78 -> (* p|N *)
4891 search state.searchpattern false
4893 | 110 | 0xffc0 -> (* n|F3 *)
4894 search state.searchpattern true
4896 | 116 -> (* t *)
4897 begin match state.layout with
4898 | [] -> ()
4899 | l :: _ ->
4900 gotoy_and_clear_text (getpagey l.pageno)
4903 | 32 -> (* space *)
4904 begin match state.layout with
4905 | [] -> ()
4906 | l :: rest ->
4907 match conf.columns with
4908 | Csingle _ | Cmulti _ ->
4909 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4910 then
4911 let y = clamp (pgscale conf.winh) in
4912 gotoy_and_clear_text y
4913 else
4914 let pageno = min (l.pageno+1) (state.pagecount-1) in
4915 gotoy_and_clear_text (getpagey pageno)
4916 | Csplit (n, _) ->
4917 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4918 then
4919 let pagey, pageh = getpageyh l.pageno in
4920 let pagey = pagey + pageh * l.pagecol in
4921 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4922 gotoy_and_clear_text (pagey + pageh + ips)
4925 | 0xff9f | 0xffff -> (* delete *)
4926 begin match state.layout with
4927 | [] -> ()
4928 | l :: _ ->
4929 match conf.columns with
4930 | Csingle _ | Cmulti _ ->
4931 if conf.presentation && l.pagey != 0
4932 then
4933 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4934 else
4935 let pageno = max 0 (l.pageno-1) in
4936 gotoy_and_clear_text (getpagey pageno)
4937 | Csplit (n, _) ->
4938 let y =
4939 if l.pagecol = 0
4940 then
4941 if l.pageno = 0
4942 then l.pagey
4943 else
4944 let pageno = max 0 (l.pageno-1) in
4945 let pagey, pageh = getpageyh pageno in
4946 pagey + (n-1)*pageh
4947 else
4948 let pagey, pageh = getpageyh l.pageno in
4949 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4951 gotoy_and_clear_text y
4954 | 61 -> (* = *)
4955 showtext ' ' (describe_location ());
4957 | 119 -> (* w *)
4958 begin match state.layout with
4959 | [] -> ()
4960 | l :: _ ->
4961 doreshape (l.pagew + state.scrollw) l.pageh;
4962 G.postRedisplay "w"
4965 | 39 -> (* ' *)
4966 enterbookmarkmode ()
4968 | 104 | 0xffbe -> (* h|F1 *)
4969 enterhelpmode ()
4971 | 105 -> (* i *)
4972 enterinfomode ()
4974 | 101 when conf.redirectstderr -> (* e *)
4975 entermsgsmode ()
4977 | 109 -> (* m *)
4978 let ondone s =
4979 match state.layout with
4980 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
4981 | _ -> ()
4983 enttext ("bookmark: ", "", None, textentry, ondone, true)
4985 | 126 -> (* ~ *)
4986 quickbookmark ();
4987 showtext ' ' "Quick bookmark added";
4989 | 122 -> (* z *)
4990 begin match state.layout with
4991 | l :: _ ->
4992 let rect = getpdimrect l.pagedimno in
4993 let w, h =
4994 if conf.crophack
4995 then
4996 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4997 truncate (1.2 *. (rect.(3) -. rect.(0))))
4998 else
4999 (truncate (rect.(1) -. rect.(0)),
5000 truncate (rect.(3) -. rect.(0)))
5002 let w = truncate ((float w)*.conf.zoom)
5003 and h = truncate ((float h)*.conf.zoom) in
5004 if w != 0 && h != 0
5005 then (
5006 state.anchor <- getanchor ();
5007 doreshape (w + state.scrollw) (h + conf.interpagespace)
5009 G.postRedisplay "z";
5011 | [] -> ()
5014 | 50 when ctrl -> (* ctrl-2 *)
5015 let maxw = getmaxw () in
5016 if maxw > 0.0
5017 then setzoom (maxw /. float conf.winw)
5019 | 60 | 62 -> (* < > *)
5020 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5022 | 91 | 93 -> (* [ ] *)
5023 conf.colorscale <-
5024 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5026 G.postRedisplay "brightness";
5028 | 99 when state.mode = View -> (* c *)
5029 let (c, a, b), z =
5030 match state.prevcolumns with
5031 | None -> (1, 0, 0), 1.0
5032 | Some (columns, z) ->
5033 let cab =
5034 match columns with
5035 | Csplit (c, _) -> -c, 0, 0
5036 | Cmulti ((c, a, b), _) -> c, a, b
5037 | Csingle _ -> 1, 0, 0
5039 cab, z
5041 setcolumns View c a b;
5042 setzoom z;
5044 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5045 setzoom state.prevzoom
5047 | 107 | 0xff52 -> (* k up *)
5048 begin match state.autoscroll with
5049 | None ->
5050 begin match state.mode with
5051 | Birdseye beye -> upbirdseye 1 beye
5052 | _ ->
5053 if ctrl
5054 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5055 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5057 | Some n ->
5058 setautoscrollspeed n false
5061 | 106 | 0xff54 -> (* j down *)
5062 begin match state.autoscroll with
5063 | None ->
5064 begin match state.mode with
5065 | Birdseye beye -> downbirdseye 1 beye
5066 | _ ->
5067 if ctrl
5068 then gotoy_and_clear_text (clamp (conf.winh/2))
5069 else gotoy_and_clear_text (clamp conf.scrollstep)
5071 | Some n ->
5072 setautoscrollspeed n true
5075 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5076 if canpan ()
5077 then
5078 let dx =
5079 if ctrl
5080 then conf.winw / 2
5081 else 10
5083 let dx = if key = 0xff51 then dx else -dx in
5084 state.x <- state.x + dx;
5085 gotoy_and_clear_text state.y
5086 else (
5087 state.text <- "";
5088 G.postRedisplay "lef/right"
5091 | 0xff55 -> (* prior *)
5092 let y =
5093 if ctrl
5094 then
5095 match state.layout with
5096 | [] -> state.y
5097 | l :: _ -> state.y - l.pagey
5098 else
5099 clamp (pgscale (-conf.winh))
5101 gotoghyll y
5103 | 0xff56 -> (* next *)
5104 let y =
5105 if ctrl
5106 then
5107 match List.rev state.layout with
5108 | [] -> state.y
5109 | l :: _ -> getpagey l.pageno
5110 else
5111 clamp (pgscale conf.winh)
5113 gotoghyll y
5115 | 0xff50 -> (* home *)
5116 gotoghyll 0
5117 | 0xff57 -> (* end *)
5118 gotoghyll (clamp state.maxy)
5119 | 0xff53 when Wsi.withalt mask -> (* right *)
5120 gotoghyll (getnav ~-1)
5121 | 0xff51 when Wsi.withalt mask -> (* left *)
5122 gotoghyll (getnav 1)
5124 | 114 -> (* r *)
5125 state.anchor <- getanchor ();
5126 opendoc state.path state.password
5128 | 118 when conf.debug -> (* v *)
5129 state.rects <- [];
5130 List.iter (fun l ->
5131 match getopaque l.pageno with
5132 | None -> ()
5133 | Some opaque ->
5134 let x0, y0, x1, y1 = pagebbox opaque in
5135 let a,b = float x0, float y0 in
5136 let c,d = float x1, float y0 in
5137 let e,f = float x1, float y1 in
5138 let h,j = float x0, float y1 in
5139 let rect = (a,b,c,d,e,f,h,j) in
5140 debugrect rect;
5141 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5142 ) state.layout;
5143 G.postRedisplay "v";
5145 | _ ->
5146 vlog "huh? %s" (Wsi.keyname key)
5149 let linknavkeyboard key mask linknav =
5150 let getpage pageno =
5151 let rec loop = function
5152 | [] -> None
5153 | l :: _ when l.pageno = pageno -> Some l
5154 | _ :: rest -> loop rest
5155 in loop state.layout
5157 let doexact (pageno, n) =
5158 match getopaque pageno, getpage pageno with
5159 | Some opaque, Some l ->
5160 if key = 0xff0d
5161 then
5162 let under = getlink opaque n in
5163 G.postRedisplay "link gotounder";
5164 gotounder under;
5165 state.mode <- View;
5166 else
5167 let opt, dir =
5168 match key with
5169 | 0xff50 -> (* home *)
5170 Some (findlink opaque LDfirst), -1
5172 | 0xff57 -> (* end *)
5173 Some (findlink opaque LDlast), 1
5175 | 0xff51 -> (* left *)
5176 Some (findlink opaque (LDleft n)), -1
5178 | 0xff53 -> (* right *)
5179 Some (findlink opaque (LDright n)), 1
5181 | 0xff52 -> (* up *)
5182 Some (findlink opaque (LDup n)), -1
5184 | 0xff54 -> (* down *)
5185 Some (findlink opaque (LDdown n)), 1
5187 | _ -> None, 0
5189 let pwl l dir =
5190 begin match findpwl l.pageno dir with
5191 | Pwlnotfound -> ()
5192 | Pwl pageno ->
5193 let notfound dir =
5194 state.mode <- LinkNav (Ltgendir dir);
5195 let y, h = getpageyh pageno in
5196 let y =
5197 if dir < 0
5198 then y + h - conf.winh
5199 else y
5201 gotoy y
5203 begin match getopaque pageno, getpage pageno with
5204 | Some opaque, Some _ ->
5205 let link =
5206 let ld = if dir > 0 then LDfirst else LDlast in
5207 findlink opaque ld
5209 begin match link with
5210 | Lfound m ->
5211 showlinktype (getlink opaque m);
5212 state.mode <- LinkNav (Ltexact (pageno, m));
5213 G.postRedisplay "linknav jpage";
5214 | _ -> notfound dir
5215 end;
5216 | _ -> notfound dir
5217 end;
5218 end;
5220 begin match opt with
5221 | Some Lnotfound -> pwl l dir;
5222 | Some (Lfound m) ->
5223 if m = n
5224 then pwl l dir
5225 else (
5226 let _, y0, _, y1 = getlinkrect opaque m in
5227 if y0 < l.pagey
5228 then gotopage1 l.pageno y0
5229 else (
5230 let d = fstate.fontsize + 1 in
5231 if y1 - l.pagey > l.pagevh - d
5232 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5233 else G.postRedisplay "linknav";
5235 showlinktype (getlink opaque m);
5236 state.mode <- LinkNav (Ltexact (l.pageno, m));
5239 | None -> viewkeyboard key mask
5240 end;
5241 | _ -> viewkeyboard key mask
5243 if key = 0xff63
5244 then (
5245 state.mode <- View;
5246 G.postRedisplay "leave linknav"
5248 else
5249 match linknav with
5250 | Ltgendir _ -> viewkeyboard key mask
5251 | Ltexact exact -> doexact exact
5254 let keyboard key mask =
5255 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5256 then wcmd "interrupt"
5257 else state.uioh <- state.uioh#key key mask
5260 let birdseyekeyboard key mask
5261 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5262 let incr =
5263 match conf.columns with
5264 | Csingle _ -> 1
5265 | Cmulti ((c, _, _), _) -> c
5266 | Csplit _ -> failwith "bird's eye split mode"
5268 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5269 match key with
5270 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5271 let y, h = getpageyh pageno in
5272 let top = (conf.winh - h) / 2 in
5273 gotoy (max 0 (y - top))
5274 | 0xff0d -> leavebirdseye beye false
5275 | 0xff1b -> leavebirdseye beye true (* escape *)
5276 | 0xff52 -> upbirdseye incr beye (* up *)
5277 | 0xff54 -> downbirdseye incr beye (* down *)
5278 | 0xff51 -> upbirdseye 1 beye (* left *)
5279 | 0xff53 -> downbirdseye 1 beye (* right *)
5281 | 0xff55 -> (* prior *)
5282 begin match state.layout with
5283 | l :: _ ->
5284 if l.pagey != 0
5285 then (
5286 state.mode <- Birdseye (
5287 oconf, leftx, l.pageno, hooverpageno, anchor
5289 gotopage1 l.pageno 0;
5291 else (
5292 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5293 match layout with
5294 | [] -> gotoy (clamp (-conf.winh))
5295 | l :: _ ->
5296 state.mode <- Birdseye (
5297 oconf, leftx, l.pageno, hooverpageno, anchor
5299 gotopage1 l.pageno 0
5302 | [] -> gotoy (clamp (-conf.winh))
5303 end;
5305 | 0xff56 -> (* next *)
5306 begin match List.rev state.layout with
5307 | l :: _ ->
5308 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5309 begin match layout with
5310 | [] ->
5311 let incr = l.pageh - l.pagevh in
5312 if incr = 0
5313 then (
5314 state.mode <-
5315 Birdseye (
5316 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5318 G.postRedisplay "birdseye pagedown";
5320 else gotoy (clamp (incr + conf.interpagespace*2));
5322 | l :: _ ->
5323 state.mode <-
5324 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5325 gotopage1 l.pageno 0;
5328 | [] -> gotoy (clamp conf.winh)
5329 end;
5331 | 0xff50 -> (* home *)
5332 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5333 gotopage1 0 0
5335 | 0xff57 -> (* end *)
5336 let pageno = state.pagecount - 1 in
5337 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5338 if not (pagevisible state.layout pageno)
5339 then
5340 let h =
5341 match List.rev state.pdims with
5342 | [] -> conf.winh
5343 | (_, _, h, _) :: _ -> h
5345 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5346 else G.postRedisplay "birdseye end";
5347 | _ -> viewkeyboard key mask
5350 let drawpage l linkindexbase =
5351 let color =
5352 match state.mode with
5353 | Textentry _ -> scalecolor 0.4
5354 | LinkNav _
5355 | View -> scalecolor 1.0
5356 | Birdseye (_, _, pageno, hooverpageno, _) ->
5357 if l.pageno = hooverpageno
5358 then scalecolor 0.9
5359 else (
5360 if l.pageno = pageno
5361 then scalecolor 1.0
5362 else scalecolor 0.8
5365 drawtiles l color;
5366 begin match getopaque l.pageno with
5367 | Some opaque ->
5368 if tileready l l.pagex l.pagey
5369 then
5370 let x = l.pagedispx - l.pagex
5371 and y = l.pagedispy - l.pagey in
5372 let hlmask =
5373 match conf.columns with
5374 | Csingle _ | Cmulti _ ->
5375 (if conf.hlinks then 1 else 0)
5376 + (if state.glinks
5377 && not (isbirdseye state.mode) then 2 else 0)
5378 | _ -> 0
5380 let s =
5381 match state.mode with
5382 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5383 | _ -> ""
5385 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5386 else 0
5388 | _ -> 0
5389 end;
5392 let scrollindicator () =
5393 let sbw, ph, sh = state.uioh#scrollph in
5394 let sbh, pw, sw = state.uioh#scrollpw in
5396 GlDraw.color (0.64, 0.64, 0.64);
5397 GlDraw.rect
5398 (float (conf.winw - sbw), 0.)
5399 (float conf.winw, float conf.winh)
5401 GlDraw.rect
5402 (0., float (conf.winh - sbh))
5403 (float (conf.winw - state.scrollw - 1), float conf.winh)
5405 GlDraw.color (0.0, 0.0, 0.0);
5407 GlDraw.rect
5408 (float (conf.winw - sbw), ph)
5409 (float conf.winw, ph +. sh)
5411 GlDraw.rect
5412 (pw, float (conf.winh - sbh))
5413 (pw +. sw, float conf.winh)
5417 let showsel () =
5418 match state.mstate with
5419 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5422 | Msel ((x0, y0), (x1, y1)) ->
5423 let rec loop = function
5424 | l :: ls ->
5425 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5426 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5427 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5428 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5429 then
5430 match getopaque l.pageno with
5431 | Some opaque ->
5432 let x0, y0 = pagetranslatepoint l x0 y0 in
5433 let x1, y1 = pagetranslatepoint l x1 y1 in
5434 seltext opaque (x0, y0, x1, y1);
5435 | _ -> ()
5436 else loop ls
5437 | [] -> ()
5439 loop state.layout
5442 let showrects rects =
5443 Gl.enable `blend;
5444 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5445 GlDraw.polygon_mode `both `fill;
5446 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5447 List.iter
5448 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5449 List.iter (fun l ->
5450 if l.pageno = pageno
5451 then (
5452 let dx = float (l.pagedispx - l.pagex) in
5453 let dy = float (l.pagedispy - l.pagey) in
5454 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5455 GlDraw.begins `quads;
5457 GlDraw.vertex2 (x0+.dx, y0+.dy);
5458 GlDraw.vertex2 (x1+.dx, y1+.dy);
5459 GlDraw.vertex2 (x2+.dx, y2+.dy);
5460 GlDraw.vertex2 (x3+.dx, y3+.dy);
5462 GlDraw.ends ();
5464 ) state.layout
5465 ) rects
5467 Gl.disable `blend;
5470 let display () =
5471 GlClear.color (scalecolor2 conf.bgcolor);
5472 GlClear.clear [`color];
5473 let rec loop linkindexbase = function
5474 | l :: rest ->
5475 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5476 loop linkindexbase rest
5477 | [] -> ()
5479 loop 0 state.layout;
5480 let rects =
5481 match state.mode with
5482 | LinkNav (Ltexact (pageno, linkno)) ->
5483 begin match getopaque pageno with
5484 | Some opaque ->
5485 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5486 (pageno, 5, (
5487 float x0, float y0,
5488 float x1, float y0,
5489 float x1, float y1,
5490 float x0, float y1)
5491 ) :: state.rects
5492 | None -> state.rects
5494 | _ -> state.rects
5496 showrects rects;
5497 showsel ();
5498 state.uioh#display;
5499 begin match state.mstate with
5500 | Mzoomrect ((x0, y0), (x1, y1)) ->
5501 Gl.enable `blend;
5502 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5503 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5504 GlDraw.rect (float x0, float y0)
5505 (float x1, float y1);
5506 Gl.disable `blend;
5507 | _ -> ()
5508 end;
5509 enttext ();
5510 scrollindicator ();
5511 Wsi.swapb ();
5514 let zoomrect x y x1 y1 =
5515 let x0 = min x x1
5516 and x1 = max x x1
5517 and y0 = min y y1 in
5518 gotoy (state.y + y0);
5519 state.anchor <- getanchor ();
5520 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5521 let margin =
5522 if state.w < conf.winw - state.scrollw
5523 then (conf.winw - state.scrollw - state.w) / 2
5524 else 0
5526 state.x <- (state.x + margin) - x0;
5527 setzoom zoom;
5528 Wsi.setcursor Wsi.CURSOR_INHERIT;
5529 state.mstate <- Mnone;
5532 let scrollx x =
5533 let winw = conf.winw - state.scrollw - 1 in
5534 let s = float x /. float winw in
5535 let destx = truncate (float (state.w + winw) *. s) in
5536 state.x <- winw - destx;
5537 gotoy_and_clear_text state.y;
5538 state.mstate <- Mscrollx;
5541 let scrolly y =
5542 let s = float y /. float conf.winh in
5543 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5544 gotoy_and_clear_text desty;
5545 state.mstate <- Mscrolly;
5548 let viewmouse button down x y mask =
5549 match button with
5550 | n when (n == 4 || n == 5) && not down ->
5551 if Wsi.withctrl mask
5552 then (
5553 match state.mstate with
5554 | Mzoom (oldn, i) ->
5555 if oldn = n
5556 then (
5557 if i = 2
5558 then
5559 let incr =
5560 match n with
5561 | 5 ->
5562 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5563 | _ ->
5564 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5566 let zoom = conf.zoom -. incr in
5567 setzoom zoom;
5568 state.mstate <- Mzoom (n, 0);
5569 else
5570 state.mstate <- Mzoom (n, i+1);
5572 else state.mstate <- Mzoom (n, 0)
5574 | _ -> state.mstate <- Mzoom (n, 0)
5576 else (
5577 match state.autoscroll with
5578 | Some step -> setautoscrollspeed step (n=4)
5579 | None ->
5580 let incr =
5581 if n = 4
5582 then -conf.scrollstep
5583 else conf.scrollstep
5585 let incr = incr * 2 in
5586 let y = clamp incr in
5587 gotoy_and_clear_text y
5590 | n when (n = 6 || n = 7) && not down && canpan () ->
5591 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5592 gotoy_and_clear_text state.y
5594 | 1 when Wsi.withctrl mask ->
5595 if down
5596 then (
5597 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5598 state.mstate <- Mpan (x, y)
5600 else
5601 state.mstate <- Mnone
5603 | 3 ->
5604 if down
5605 then (
5606 Wsi.setcursor Wsi.CURSOR_CYCLE;
5607 let p = (x, y) in
5608 state.mstate <- Mzoomrect (p, p)
5610 else (
5611 match state.mstate with
5612 | Mzoomrect ((x0, y0), _) ->
5613 if abs (x-x0) > 10 && abs (y - y0) > 10
5614 then zoomrect x0 y0 x y
5615 else (
5616 state.mstate <- Mnone;
5617 Wsi.setcursor Wsi.CURSOR_INHERIT;
5618 G.postRedisplay "kill accidental zoom rect";
5620 | _ ->
5621 Wsi.setcursor Wsi.CURSOR_INHERIT;
5622 state.mstate <- Mnone
5625 | 1 when x > conf.winw - state.scrollw ->
5626 if down
5627 then
5628 let _, position, sh = state.uioh#scrollph in
5629 if y > truncate position && y < truncate (position +. sh)
5630 then state.mstate <- Mscrolly
5631 else scrolly y
5632 else
5633 state.mstate <- Mnone
5635 | 1 when y > conf.winh - state.hscrollh ->
5636 if down
5637 then
5638 let _, position, sw = state.uioh#scrollpw in
5639 if x > truncate position && x < truncate (position +. sw)
5640 then state.mstate <- Mscrollx
5641 else scrollx x
5642 else
5643 state.mstate <- Mnone
5645 | 1 ->
5646 let dest = if down then getunder x y else Unone in
5647 begin match dest with
5648 | Ulinkgoto _
5649 | Ulinkuri _
5650 | Uremote _
5651 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5652 gotounder dest
5654 | Unone when down ->
5655 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5656 state.mstate <- Mpan (x, y);
5658 | Unone | Utext _ ->
5659 if down
5660 then (
5661 if conf.angle mod 360 = 0
5662 then (
5663 state.mstate <- Msel ((x, y), (x, y));
5664 G.postRedisplay "mouse select";
5667 else (
5668 match state.mstate with
5669 | Mnone -> ()
5671 | Mzoom _ | Mscrollx | Mscrolly ->
5672 state.mstate <- Mnone
5674 | Mzoomrect ((x0, y0), _) ->
5675 zoomrect x0 y0 x y
5677 | Mpan _ ->
5678 Wsi.setcursor Wsi.CURSOR_INHERIT;
5679 state.mstate <- Mnone
5681 | Msel ((_, y0), (_, y1)) ->
5682 let rec loop = function
5683 | [] -> ()
5684 | l :: rest ->
5685 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5686 || ((y1 >= l.pagedispy
5687 && y1 <= (l.pagedispy + l.pagevh)))
5688 then
5689 match getopaque l.pageno with
5690 | Some opaque ->
5691 begin
5692 match Ne.pipe () with
5693 | Ne.Exn exn ->
5694 showtext '!'
5695 (Printf.sprintf
5696 "can not create sel pipe: %s"
5697 (Printexc.to_string exn));
5698 | Ne.Res (r, w) ->
5699 let doclose what fd =
5700 Ne.clo fd (fun msg ->
5701 dolog "%s close failed: %s" what msg)
5704 popen conf.selcmd [r, 0; w, -1];
5705 copysel w opaque;
5706 doclose "pipe/r" r;
5707 G.postRedisplay "copysel";
5708 with exn ->
5709 dolog "can not execute %S: %s"
5710 conf.selcmd (Printexc.to_string exn);
5711 doclose "pipe/r" r;
5712 doclose "pipe/w" w;
5714 | None -> ()
5715 else loop rest
5717 loop state.layout;
5718 Wsi.setcursor Wsi.CURSOR_INHERIT;
5719 state.mstate <- Mnone;
5723 | _ -> ()
5726 let birdseyemouse button down x y mask
5727 (conf, leftx, _, hooverpageno, anchor) =
5728 match button with
5729 | 1 when down ->
5730 let rec loop = function
5731 | [] -> ()
5732 | l :: rest ->
5733 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5734 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5735 then (
5736 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5738 else loop rest
5740 loop state.layout
5741 | 3 -> ()
5742 | _ -> viewmouse button down x y mask
5745 let mouse button down x y mask =
5746 state.uioh <- state.uioh#button button down x y mask;
5749 let motion ~x ~y =
5750 state.uioh <- state.uioh#motion x y
5753 let pmotion ~x ~y =
5754 state.uioh <- state.uioh#pmotion x y;
5757 let uioh = object
5758 method display = ()
5760 method key key mask =
5761 begin match state.mode with
5762 | Textentry textentry -> textentrykeyboard key mask textentry
5763 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5764 | View -> viewkeyboard key mask
5765 | LinkNav linknav -> linknavkeyboard key mask linknav
5766 end;
5767 state.uioh
5769 method button button bstate x y mask =
5770 begin match state.mode with
5771 | LinkNav _
5772 | View -> viewmouse button bstate x y mask
5773 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5774 | Textentry _ -> ()
5775 end;
5776 state.uioh
5778 method motion x y =
5779 begin match state.mode with
5780 | Textentry _ -> ()
5781 | View | Birdseye _ | LinkNav _ ->
5782 match state.mstate with
5783 | Mzoom _ | Mnone -> ()
5785 | Mpan (x0, y0) ->
5786 let dx = x - x0
5787 and dy = y0 - y in
5788 state.mstate <- Mpan (x, y);
5789 if canpan ()
5790 then state.x <- state.x + dx;
5791 let y = clamp dy in
5792 gotoy_and_clear_text y
5794 | Msel (a, _) ->
5795 state.mstate <- Msel (a, (x, y));
5796 G.postRedisplay "motion select";
5798 | Mscrolly ->
5799 let y = min conf.winh (max 0 y) in
5800 scrolly y
5802 | Mscrollx ->
5803 let x = min conf.winw (max 0 x) in
5804 scrollx x
5806 | Mzoomrect (p0, _) ->
5807 state.mstate <- Mzoomrect (p0, (x, y));
5808 G.postRedisplay "motion zoomrect";
5809 end;
5810 state.uioh
5812 method pmotion x y =
5813 begin match state.mode with
5814 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5815 let rec loop = function
5816 | [] ->
5817 if hooverpageno != -1
5818 then (
5819 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5820 G.postRedisplay "pmotion birdseye no hoover";
5822 | l :: rest ->
5823 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5824 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5825 then (
5826 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5827 G.postRedisplay "pmotion birdseye hoover";
5829 else loop rest
5831 loop state.layout
5833 | Textentry _ -> ()
5835 | LinkNav _
5836 | View ->
5837 match state.mstate with
5838 | Mnone -> updateunder x y
5839 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5841 end;
5842 state.uioh
5844 method infochanged _ = ()
5846 method scrollph =
5847 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5848 let p, h = scrollph state.y maxy in
5849 state.scrollw, p, h
5851 method scrollpw =
5852 let winw = conf.winw - state.scrollw - 1 in
5853 let fwinw = float winw in
5854 let sw =
5855 let sw = fwinw /. float state.w in
5856 let sw = fwinw *. sw in
5857 max sw (float conf.scrollh)
5859 let position, sw =
5860 let f = state.w+winw in
5861 let r = float (winw-state.x) /. float f in
5862 let p = fwinw *. r in
5863 p-.sw/.2., sw
5865 let sw =
5866 if position +. sw > fwinw
5867 then fwinw -. position
5868 else sw
5870 state.hscrollh, position, sw
5872 method modehash =
5873 let modename =
5874 match state.mode with
5875 | LinkNav _ -> "links"
5876 | Textentry _ -> "textentry"
5877 | Birdseye _ -> "birdseye"
5878 | View -> "view"
5880 findkeyhash conf modename
5881 end;;
5883 module Config =
5884 struct
5885 open Parser
5887 let fontpath = ref "";;
5889 module KeyMap =
5890 Map.Make (struct type t = (int * int) let compare = compare end);;
5892 let unent s =
5893 let l = String.length s in
5894 let b = Buffer.create l in
5895 unent b s 0 l;
5896 Buffer.contents b;
5899 let home =
5900 try Sys.getenv "HOME"
5901 with exn ->
5902 prerr_endline
5903 ("Can not determine home directory location: " ^
5904 Printexc.to_string exn);
5908 let modifier_of_string = function
5909 | "alt" -> Wsi.altmask
5910 | "shift" -> Wsi.shiftmask
5911 | "ctrl" | "control" -> Wsi.ctrlmask
5912 | "meta" -> Wsi.metamask
5913 | _ -> 0
5916 let key_of_string =
5917 let r = Str.regexp "-" in
5918 fun s ->
5919 let elems = Str.full_split r s in
5920 let f n k m =
5921 let g s =
5922 let m1 = modifier_of_string s in
5923 if m1 = 0
5924 then (Wsi.namekey s, m)
5925 else (k, m lor m1)
5926 in function
5927 | Str.Delim s when n land 1 = 0 -> g s
5928 | Str.Text s -> g s
5929 | Str.Delim _ -> (k, m)
5931 let rec loop n k m = function
5932 | [] -> (k, m)
5933 | x :: xs ->
5934 let k, m = f n k m x in
5935 loop (n+1) k m xs
5937 loop 0 0 0 elems
5940 let keys_of_string =
5941 let r = Str.regexp "[ \t]" in
5942 fun s ->
5943 let elems = Str.split r s in
5944 List.map key_of_string elems
5947 let copykeyhashes c =
5948 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5951 let config_of c attrs =
5952 let apply c k v =
5954 match k with
5955 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5956 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5957 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5958 | "preload" -> { c with preload = bool_of_string v }
5959 | "page-bias" -> { c with pagebias = int_of_string v }
5960 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5961 | "horizontal-scroll-step" ->
5962 { c with hscrollstep = max (int_of_string v) 1 }
5963 | "auto-scroll-step" ->
5964 { c with autoscrollstep = max 0 (int_of_string v) }
5965 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5966 | "crop-hack" -> { c with crophack = bool_of_string v }
5967 | "throttle" ->
5968 let mw =
5969 match String.lowercase v with
5970 | "true" -> Some infinity
5971 | "false" -> None
5972 | f -> Some (float_of_string f)
5974 { c with maxwait = mw}
5975 | "highlight-links" -> { c with hlinks = bool_of_string v }
5976 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5977 | "vertical-margin" ->
5978 { c with interpagespace = max 0 (int_of_string v) }
5979 | "zoom" ->
5980 let zoom = float_of_string v /. 100. in
5981 let zoom = max zoom 0.0 in
5982 { c with zoom = zoom }
5983 | "presentation" -> { c with presentation = bool_of_string v }
5984 | "rotation-angle" -> { c with angle = int_of_string v }
5985 | "width" -> { c with winw = max 20 (int_of_string v) }
5986 | "height" -> { c with winh = max 20 (int_of_string v) }
5987 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5988 | "proportional-display" -> { c with proportional = bool_of_string v }
5989 | "pixmap-cache-size" ->
5990 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5991 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5992 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5993 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5994 | "persistent-location" -> { c with jumpback = bool_of_string v }
5995 | "background-color" -> { c with bgcolor = color_of_string v }
5996 | "scrollbar-in-presentation" ->
5997 { c with scrollbarinpm = bool_of_string v }
5998 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5999 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6000 | "mupdf-store-size" ->
6001 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6002 | "checkers" -> { c with checkers = bool_of_string v }
6003 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6004 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6005 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6006 | "uri-launcher" -> { c with urilauncher = unent v }
6007 | "path-launcher" -> { c with pathlauncher = unent v }
6008 | "color-space" -> { c with colorspace = colorspace_of_string v }
6009 | "invert-colors" -> { c with invert = bool_of_string v }
6010 | "brightness" -> { c with colorscale = float_of_string v }
6011 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6012 | "ghyllscroll" ->
6013 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6014 | "columns" ->
6015 let (n, _, _) as nab = multicolumns_of_string v in
6016 if n < 0
6017 then { c with columns = Csplit (-n, [||]) }
6018 else { c with columns = Cmulti (nab, [||]) }
6019 | "birds-eye-columns" ->
6020 { c with beyecolumns = Some (max (int_of_string v) 2) }
6021 | "selection-command" -> { c with selcmd = unent v }
6022 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6023 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6024 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6025 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6026 | _ -> c
6027 with exn ->
6028 prerr_endline ("Error processing attribute (`" ^
6029 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6032 let rec fold c = function
6033 | [] -> c
6034 | (k, v) :: rest ->
6035 let c = apply c k v in
6036 fold c rest
6038 fold { c with keyhashes = copykeyhashes c } attrs;
6041 let fromstring f pos n v d =
6042 try f v
6043 with exn ->
6044 dolog "Error processing attribute (%S=%S) at %d\n%s"
6045 n v pos (Printexc.to_string exn)
6050 let bookmark_of attrs =
6051 let rec fold title page rely visy = function
6052 | ("title", v) :: rest -> fold v page rely visy rest
6053 | ("page", v) :: rest -> fold title v rely visy rest
6054 | ("rely", v) :: rest -> fold title page v visy rest
6055 | ("visy", v) :: rest -> fold title page rely v rest
6056 | _ :: rest -> fold title page rely visy rest
6057 | [] -> title, page, rely, visy
6059 fold "invalid" "0" "0" "0" attrs
6062 let doc_of attrs =
6063 let rec fold path page rely pan visy = function
6064 | ("path", v) :: rest -> fold v page rely pan visy rest
6065 | ("page", v) :: rest -> fold path v rely pan visy rest
6066 | ("rely", v) :: rest -> fold path page v pan visy rest
6067 | ("pan", v) :: rest -> fold path page rely v visy rest
6068 | ("visy", v) :: rest -> fold path page rely pan v rest
6069 | _ :: rest -> fold path page rely pan visy rest
6070 | [] -> path, page, rely, pan, visy
6072 fold "" "0" "0" "0" "0" attrs
6075 let map_of attrs =
6076 let rec fold rs ls = function
6077 | ("out", v) :: rest -> fold v ls rest
6078 | ("in", v) :: rest -> fold rs v rest
6079 | _ :: rest -> fold ls rs rest
6080 | [] -> ls, rs
6082 fold "" "" attrs
6085 let setconf dst src =
6086 dst.scrollbw <- src.scrollbw;
6087 dst.scrollh <- src.scrollh;
6088 dst.icase <- src.icase;
6089 dst.preload <- src.preload;
6090 dst.pagebias <- src.pagebias;
6091 dst.verbose <- src.verbose;
6092 dst.scrollstep <- src.scrollstep;
6093 dst.maxhfit <- src.maxhfit;
6094 dst.crophack <- src.crophack;
6095 dst.autoscrollstep <- src.autoscrollstep;
6096 dst.maxwait <- src.maxwait;
6097 dst.hlinks <- src.hlinks;
6098 dst.underinfo <- src.underinfo;
6099 dst.interpagespace <- src.interpagespace;
6100 dst.zoom <- src.zoom;
6101 dst.presentation <- src.presentation;
6102 dst.angle <- src.angle;
6103 dst.winw <- src.winw;
6104 dst.winh <- src.winh;
6105 dst.savebmarks <- src.savebmarks;
6106 dst.memlimit <- src.memlimit;
6107 dst.proportional <- src.proportional;
6108 dst.texcount <- src.texcount;
6109 dst.sliceheight <- src.sliceheight;
6110 dst.thumbw <- src.thumbw;
6111 dst.jumpback <- src.jumpback;
6112 dst.bgcolor <- src.bgcolor;
6113 dst.scrollbarinpm <- src.scrollbarinpm;
6114 dst.tilew <- src.tilew;
6115 dst.tileh <- src.tileh;
6116 dst.mustoresize <- src.mustoresize;
6117 dst.checkers <- src.checkers;
6118 dst.aalevel <- src.aalevel;
6119 dst.trimmargins <- src.trimmargins;
6120 dst.trimfuzz <- src.trimfuzz;
6121 dst.urilauncher <- src.urilauncher;
6122 dst.colorspace <- src.colorspace;
6123 dst.invert <- src.invert;
6124 dst.colorscale <- src.colorscale;
6125 dst.redirectstderr <- src.redirectstderr;
6126 dst.ghyllscroll <- src.ghyllscroll;
6127 dst.columns <- src.columns;
6128 dst.beyecolumns <- src.beyecolumns;
6129 dst.selcmd <- src.selcmd;
6130 dst.updatecurs <- src.updatecurs;
6131 dst.pathlauncher <- src.pathlauncher;
6132 dst.keyhashes <- copykeyhashes src;
6133 dst.hfsize <- src.hfsize;
6134 dst.hscrollstep <- src.hscrollstep;
6135 dst.pgscale <- src.pgscale;
6136 dst.multicenter <- src.multicenter;
6139 let get s =
6140 let h = Hashtbl.create 10 in
6141 let dc = { defconf with angle = defconf.angle } in
6142 let rec toplevel v t spos _ =
6143 match t with
6144 | Vdata | Vcdata | Vend -> v
6145 | Vopen ("llppconfig", _, closed) ->
6146 if closed
6147 then v
6148 else { v with f = llppconfig }
6149 | Vopen _ ->
6150 error "unexpected subelement at top level" s spos
6151 | Vclose _ -> error "unexpected close at top level" s spos
6153 and llppconfig v t spos _ =
6154 match t with
6155 | Vdata | Vcdata -> v
6156 | Vend -> error "unexpected end of input in llppconfig" s spos
6157 | Vopen ("defaults", attrs, closed) ->
6158 let c = config_of dc attrs in
6159 setconf dc c;
6160 if closed
6161 then v
6162 else { v with f = defaults }
6164 | Vopen ("ui-font", attrs, closed) ->
6165 let rec getsize size = function
6166 | [] -> size
6167 | ("size", v) :: rest ->
6168 let size =
6169 fromstring int_of_string spos "size" v fstate.fontsize in
6170 getsize size rest
6171 | l -> getsize size l
6173 fstate.fontsize <- getsize fstate.fontsize attrs;
6174 if closed
6175 then v
6176 else { v with f = uifont (Buffer.create 10) }
6178 | Vopen ("doc", attrs, closed) ->
6179 let pathent, spage, srely, span, svisy = doc_of attrs in
6180 let path = unent pathent
6181 and pageno = fromstring int_of_string spos "page" spage 0
6182 and rely = fromstring float_of_string spos "rely" srely 0.0
6183 and pan = fromstring int_of_string spos "pan" span 0
6184 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6185 let c = config_of dc attrs in
6186 let anchor = (pageno, rely, visy) in
6187 if closed
6188 then (Hashtbl.add h path (c, [], pan, anchor); v)
6189 else { v with f = doc path pan anchor c [] }
6191 | Vopen _ ->
6192 error "unexpected subelement in llppconfig" s spos
6194 | Vclose "llppconfig" -> { v with f = toplevel }
6195 | Vclose _ -> error "unexpected close in llppconfig" s spos
6197 and defaults v t spos _ =
6198 match t with
6199 | Vdata | Vcdata -> v
6200 | Vend -> error "unexpected end of input in defaults" s spos
6201 | Vopen ("keymap", attrs, closed) ->
6202 let modename =
6203 try List.assoc "mode" attrs
6204 with Not_found -> "global" in
6205 if closed
6206 then v
6207 else
6208 let ret keymap =
6209 let h = findkeyhash dc modename in
6210 KeyMap.iter (Hashtbl.replace h) keymap;
6211 defaults
6213 { v with f = pkeymap ret KeyMap.empty }
6215 | Vopen (_, _, _) ->
6216 error "unexpected subelement in defaults" s spos
6218 | Vclose "defaults" ->
6219 { v with f = llppconfig }
6221 | Vclose _ -> error "unexpected close in defaults" s spos
6223 and uifont b v t spos epos =
6224 match t with
6225 | Vdata | Vcdata ->
6226 Buffer.add_substring b s spos (epos - spos);
6228 | Vopen (_, _, _) ->
6229 error "unexpected subelement in ui-font" s spos
6230 | Vclose "ui-font" ->
6231 if String.length !fontpath = 0
6232 then fontpath := Buffer.contents b;
6233 { v with f = llppconfig }
6234 | Vclose _ -> error "unexpected close in ui-font" s spos
6235 | Vend -> error "unexpected end of input in ui-font" s spos
6237 and doc path pan anchor c bookmarks v t spos _ =
6238 match t with
6239 | Vdata | Vcdata -> v
6240 | Vend -> error "unexpected end of input in doc" s spos
6241 | Vopen ("bookmarks", _, closed) ->
6242 if closed
6243 then v
6244 else { v with f = pbookmarks path pan anchor c bookmarks }
6246 | Vopen ("keymap", attrs, closed) ->
6247 let modename =
6248 try List.assoc "mode" attrs
6249 with Not_found -> "global"
6251 if closed
6252 then v
6253 else
6254 let ret keymap =
6255 let h = findkeyhash c modename in
6256 KeyMap.iter (Hashtbl.replace h) keymap;
6257 doc path pan anchor c bookmarks
6259 { v with f = pkeymap ret KeyMap.empty }
6261 | Vopen (_, _, _) ->
6262 error "unexpected subelement in doc" s spos
6264 | Vclose "doc" ->
6265 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6266 { v with f = llppconfig }
6268 | Vclose _ -> error "unexpected close in doc" s spos
6270 and pkeymap ret keymap v t spos _ =
6271 match t with
6272 | Vdata | Vcdata -> v
6273 | Vend -> error "unexpected end of input in keymap" s spos
6274 | Vopen ("map", attrs, closed) ->
6275 let r, l = map_of attrs in
6276 let kss = fromstring keys_of_string spos "in" r [] in
6277 let lss = fromstring keys_of_string spos "out" l [] in
6278 let keymap =
6279 match kss with
6280 | [] -> keymap
6281 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6282 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6284 if closed
6285 then { v with f = pkeymap ret keymap }
6286 else
6287 let f () = v in
6288 { v with f = skip "map" f }
6290 | Vopen _ ->
6291 error "unexpected subelement in keymap" s spos
6293 | Vclose "keymap" ->
6294 { v with f = ret keymap }
6296 | Vclose _ -> error "unexpected close in keymap" s spos
6298 and pbookmarks path pan anchor c bookmarks v t spos _ =
6299 match t with
6300 | Vdata | Vcdata -> v
6301 | Vend -> error "unexpected end of input in bookmarks" s spos
6302 | Vopen ("item", attrs, closed) ->
6303 let titleent, spage, srely, svisy = bookmark_of attrs in
6304 let page = fromstring int_of_string spos "page" spage 0
6305 and rely = fromstring float_of_string spos "rely" srely 0.0
6306 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6307 let bookmarks =
6308 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6310 if closed
6311 then { v with f = pbookmarks path pan anchor c bookmarks }
6312 else
6313 let f () = v in
6314 { v with f = skip "item" f }
6316 | Vopen _ ->
6317 error "unexpected subelement in bookmarks" s spos
6319 | Vclose "bookmarks" ->
6320 { v with f = doc path pan anchor c bookmarks }
6322 | Vclose _ -> error "unexpected close in bookmarks" s spos
6324 and skip tag f v t spos _ =
6325 match t with
6326 | Vdata | Vcdata -> v
6327 | Vend ->
6328 error ("unexpected end of input in skipped " ^ tag) s spos
6329 | Vopen (tag', _, closed) ->
6330 if closed
6331 then v
6332 else
6333 let f' () = { v with f = skip tag f } in
6334 { v with f = skip tag' f' }
6335 | Vclose ctag ->
6336 if tag = ctag
6337 then f ()
6338 else error ("unexpected close in skipped " ^ tag) s spos
6341 parse { f = toplevel; accu = () } s;
6342 h, dc;
6345 let do_load f ic =
6347 let len = in_channel_length ic in
6348 let s = String.create len in
6349 really_input ic s 0 len;
6350 f s;
6351 with
6352 | Parse_error (msg, s, pos) ->
6353 let subs = subs s pos in
6354 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6355 failwith ("parse error: " ^ s)
6357 | exn ->
6358 failwith ("config load error: " ^ Printexc.to_string exn)
6361 let defconfpath =
6362 let dir =
6364 let dir = Filename.concat home ".config" in
6365 if Sys.is_directory dir then dir else home
6366 with _ -> home
6368 Filename.concat dir "llpp.conf"
6371 let confpath = ref defconfpath;;
6373 let load1 f =
6374 if Sys.file_exists !confpath
6375 then
6376 match
6377 (try Some (open_in_bin !confpath)
6378 with exn ->
6379 prerr_endline
6380 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6381 Printexc.to_string exn);
6382 None
6384 with
6385 | Some ic ->
6386 begin try
6387 f (do_load get ic)
6388 with exn ->
6389 prerr_endline
6390 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6391 Printexc.to_string exn);
6392 end;
6393 close_in ic;
6395 | None -> ()
6396 else
6397 f (Hashtbl.create 0, defconf)
6400 let load () =
6401 let f (h, dc) =
6402 let pc, pb, px, pa =
6404 Hashtbl.find h (Filename.basename state.path)
6405 with Not_found -> dc, [], 0, emptyanchor
6407 setconf defconf dc;
6408 setconf conf pc;
6409 state.bookmarks <- pb;
6410 state.x <- px;
6411 state.scrollw <- conf.scrollbw;
6412 if conf.jumpback
6413 then state.anchor <- pa;
6414 cbput state.hists.nav pa;
6416 load1 f
6419 let add_attrs bb always dc c =
6420 let ob s a b =
6421 if always || a != b
6422 then Printf.bprintf bb "\n %s='%b'" s a
6423 and oi s a b =
6424 if always || a != b
6425 then Printf.bprintf bb "\n %s='%d'" s a
6426 and oI s a b =
6427 if always || a != b
6428 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6429 and oz s a b =
6430 if always || a <> b
6431 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6432 and oF s a b =
6433 if always || a <> b
6434 then Printf.bprintf bb "\n %s='%f'" s a
6435 and oc s a b =
6436 if always || a <> b
6437 then
6438 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6439 and oC s a b =
6440 if always || a <> b
6441 then
6442 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6443 and oR s a b =
6444 if always || a <> b
6445 then
6446 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6447 and os s a b =
6448 if always || a <> b
6449 then
6450 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6451 and og s a b =
6452 if always || a <> b
6453 then
6454 match a with
6455 | None -> ()
6456 | Some (_N, _A, _B) ->
6457 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6458 and oW s a b =
6459 if always || a <> b
6460 then
6461 let v =
6462 match a with
6463 | None -> "false"
6464 | Some f ->
6465 if f = infinity
6466 then "true"
6467 else string_of_float f
6469 Printf.bprintf bb "\n %s='%s'" s v
6470 and oco s a b =
6471 if always || a <> b
6472 then
6473 match a with
6474 | Cmulti ((n, a, b), _) when n > 1 ->
6475 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6476 | Csplit (n, _) when n > 1 ->
6477 Printf.bprintf bb "\n %s='%d'" s ~-n
6478 | _ -> ()
6479 and obeco s a b =
6480 if always || a <> b
6481 then
6482 match a with
6483 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6484 | _ -> ()
6486 let w, h =
6487 if always
6488 then dc.winw, dc.winh
6489 else
6490 match state.fullscreen with
6491 | Some wh -> wh
6492 | None -> c.winw, c.winh
6494 oi "width" w dc.winw;
6495 oi "height" h dc.winh;
6496 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6497 oi "scroll-handle-height" c.scrollh dc.scrollh;
6498 ob "case-insensitive-search" c.icase dc.icase;
6499 ob "preload" c.preload dc.preload;
6500 oi "page-bias" c.pagebias dc.pagebias;
6501 oi "scroll-step" c.scrollstep dc.scrollstep;
6502 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6503 ob "max-height-fit" c.maxhfit dc.maxhfit;
6504 ob "crop-hack" c.crophack dc.crophack;
6505 oW "throttle" c.maxwait dc.maxwait;
6506 ob "highlight-links" c.hlinks dc.hlinks;
6507 ob "under-cursor-info" c.underinfo dc.underinfo;
6508 oi "vertical-margin" c.interpagespace dc.interpagespace;
6509 oz "zoom" c.zoom dc.zoom;
6510 ob "presentation" c.presentation dc.presentation;
6511 oi "rotation-angle" c.angle dc.angle;
6512 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6513 ob "proportional-display" c.proportional dc.proportional;
6514 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6515 oi "tex-count" c.texcount dc.texcount;
6516 oi "slice-height" c.sliceheight dc.sliceheight;
6517 oi "thumbnail-width" c.thumbw dc.thumbw;
6518 ob "persistent-location" c.jumpback dc.jumpback;
6519 oc "background-color" c.bgcolor dc.bgcolor;
6520 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6521 oi "tile-width" c.tilew dc.tilew;
6522 oi "tile-height" c.tileh dc.tileh;
6523 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6524 ob "checkers" c.checkers dc.checkers;
6525 oi "aalevel" c.aalevel dc.aalevel;
6526 ob "trim-margins" c.trimmargins dc.trimmargins;
6527 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6528 os "uri-launcher" c.urilauncher dc.urilauncher;
6529 os "path-launcher" c.pathlauncher dc.pathlauncher;
6530 oC "color-space" c.colorspace dc.colorspace;
6531 ob "invert-colors" c.invert dc.invert;
6532 oF "brightness" c.colorscale dc.colorscale;
6533 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6534 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6535 oco "columns" c.columns dc.columns;
6536 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6537 os "selection-command" c.selcmd dc.selcmd;
6538 ob "update-cursor" c.updatecurs dc.updatecurs;
6539 oi "hint-font-size" c.hfsize dc.hfsize;
6540 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6541 oF "page-scroll-scale" c.pgscale dc.pgscale;
6542 ob "multi-column-centering" c.multicenter dc.multicenter;
6545 let keymapsbuf always dc c =
6546 let bb = Buffer.create 16 in
6547 let rec loop = function
6548 | [] -> ()
6549 | (modename, h) :: rest ->
6550 let dh = findkeyhash dc modename in
6551 if always || h <> dh
6552 then (
6553 if Hashtbl.length h > 0
6554 then (
6555 if Buffer.length bb > 0
6556 then Buffer.add_char bb '\n';
6557 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6558 Hashtbl.iter (fun i o ->
6559 let isdifferent = always ||
6561 let dO = Hashtbl.find dh i in
6562 dO <> o
6563 with Not_found -> true
6565 if isdifferent
6566 then
6567 let addkm (k, m) =
6568 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6569 if Wsi.withalt m then Buffer.add_string bb "alt-";
6570 if Wsi.withshift m then Buffer.add_string bb "shift-";
6571 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6572 Buffer.add_string bb (Wsi.keyname k);
6574 let addkms l =
6575 let rec loop = function
6576 | [] -> ()
6577 | km :: [] -> addkm km
6578 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6580 loop l
6582 Buffer.add_string bb "<map in='";
6583 addkm i;
6584 match o with
6585 | KMinsrt km ->
6586 Buffer.add_string bb "' out='";
6587 addkm km;
6588 Buffer.add_string bb "'/>\n"
6590 | KMinsrl kms ->
6591 Buffer.add_string bb "' out='";
6592 addkms kms;
6593 Buffer.add_string bb "'/>\n"
6595 | KMmulti (ins, kms) ->
6596 Buffer.add_char bb ' ';
6597 addkms ins;
6598 Buffer.add_string bb "' out='";
6599 addkms kms;
6600 Buffer.add_string bb "'/>\n"
6601 ) h;
6602 Buffer.add_string bb "</keymap>";
6605 loop rest
6607 loop c.keyhashes;
6611 let save () =
6612 let uifontsize = fstate.fontsize in
6613 let bb = Buffer.create 32768 in
6614 let f (h, dc) =
6615 let dc = if conf.bedefault then conf else dc in
6616 Buffer.add_string bb "<llppconfig>\n";
6618 if String.length !fontpath > 0
6619 then
6620 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6621 uifontsize
6622 !fontpath
6623 else (
6624 if uifontsize <> 14
6625 then
6626 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6629 Buffer.add_string bb "<defaults ";
6630 add_attrs bb true dc dc;
6631 let kb = keymapsbuf true dc dc in
6632 if Buffer.length kb > 0
6633 then (
6634 Buffer.add_string bb ">\n";
6635 Buffer.add_buffer bb kb;
6636 Buffer.add_string bb "\n</defaults>\n";
6638 else Buffer.add_string bb "/>\n";
6640 let adddoc path pan anchor c bookmarks =
6641 if bookmarks == [] && c = dc && anchor = emptyanchor
6642 then ()
6643 else (
6644 Printf.bprintf bb "<doc path='%s'"
6645 (enent path 0 (String.length path));
6647 if anchor <> emptyanchor
6648 then (
6649 let n, rely, visy = anchor in
6650 Printf.bprintf bb " page='%d'" n;
6651 if rely > 1e-6
6652 then
6653 Printf.bprintf bb " rely='%f'" rely
6655 if abs_float visy > 1e-6
6656 then
6657 Printf.bprintf bb " visy='%f'" visy
6661 if pan != 0
6662 then Printf.bprintf bb " pan='%d'" pan;
6664 add_attrs bb false dc c;
6665 let kb = keymapsbuf false dc c in
6667 begin match bookmarks with
6668 | [] ->
6669 if Buffer.length kb > 0
6670 then (
6671 Buffer.add_string bb ">\n";
6672 Buffer.add_buffer bb kb;
6673 Buffer.add_string bb "\n</doc>\n";
6675 else Buffer.add_string bb "/>\n"
6676 | _ ->
6677 Buffer.add_string bb ">\n<bookmarks>\n";
6678 List.iter (fun (title, _level, (page, rely, visy)) ->
6679 Printf.bprintf bb
6680 "<item title='%s' page='%d'"
6681 (enent title 0 (String.length title))
6682 page
6684 if rely > 1e-6
6685 then
6686 Printf.bprintf bb " rely='%f'" rely
6688 if abs_float visy > 1e-6
6689 then
6690 Printf.bprintf bb " visy='%f'" visy
6692 Buffer.add_string bb "/>\n";
6693 ) bookmarks;
6694 Buffer.add_string bb "</bookmarks>";
6695 if Buffer.length kb > 0
6696 then (
6697 Buffer.add_string bb "\n";
6698 Buffer.add_buffer bb kb;
6700 Buffer.add_string bb "\n</doc>\n";
6701 end;
6705 let pan, conf =
6706 match state.mode with
6707 | Birdseye (c, pan, _, _, _) ->
6708 let beyecolumns =
6709 match conf.columns with
6710 | Cmulti ((c, _, _), _) -> Some c
6711 | Csingle _ -> None
6712 | Csplit _ -> None
6713 and columns =
6714 match c.columns with
6715 | Cmulti (c, _) -> Cmulti (c, [||])
6716 | Csingle _ -> Csingle [||]
6717 | Csplit _ -> failwith "quit from bird's eye while split"
6719 pan, { c with beyecolumns = beyecolumns; columns = columns }
6720 | _ -> state.x, conf
6722 let basename = Filename.basename state.path in
6723 adddoc basename pan (getanchor ())
6724 (let conf =
6725 let autoscrollstep =
6726 match state.autoscroll with
6727 | Some step -> step
6728 | None -> conf.autoscrollstep
6730 match state.mode with
6731 | Birdseye (bc, _, _, _, _) ->
6732 { conf with
6733 zoom = bc.zoom;
6734 presentation = bc.presentation;
6735 interpagespace = bc.interpagespace;
6736 maxwait = bc.maxwait;
6737 autoscrollstep = autoscrollstep }
6738 | _ -> { conf with autoscrollstep = autoscrollstep }
6739 in conf)
6740 (if conf.savebmarks then state.bookmarks else []);
6742 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6743 if basename <> path
6744 then adddoc path x anchor c bookmarks
6745 ) h;
6746 Buffer.add_string bb "</llppconfig>";
6748 load1 f;
6749 if Buffer.length bb > 0
6750 then
6752 let tmp = !confpath ^ ".tmp" in
6753 let oc = open_out_bin tmp in
6754 Buffer.output_buffer oc bb;
6755 close_out oc;
6756 Unix.rename tmp !confpath;
6757 with exn ->
6758 prerr_endline
6759 ("error while saving configuration: " ^ Printexc.to_string exn)
6761 end;;
6763 let () =
6764 let trimcachepath = ref "" in
6765 Arg.parse
6766 (Arg.align
6767 [("-p", Arg.String (fun s -> state.password <- s) ,
6768 "<password> Set password");
6770 ("-f", Arg.String (fun s -> Config.fontpath := s),
6771 "<path> Set path to the user interface font");
6773 ("-c", Arg.String (fun s -> Config.confpath := s),
6774 "<path> Set path to the configuration file");
6776 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6777 "<path> Set path to the trim cache file");
6779 ("-v", Arg.Unit (fun () ->
6780 Printf.printf
6781 "%s\nconfiguration path: %s\n"
6782 (version ())
6783 Config.defconfpath
6785 exit 0), " Print version and exit");
6788 (fun s -> state.path <- s)
6789 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6791 if String.length state.path = 0
6792 then (prerr_endline "file name missing"; exit 1);
6794 Config.load ();
6796 let globalkeyhash = findkeyhash conf "global" in
6797 let wsfd, winw, winh = Wsi.init (object
6798 method expose =
6799 if nogeomcmds state.geomcmds || platform == Posx
6800 then display ()
6801 else (
6802 GlClear.color (scalecolor2 conf.bgcolor);
6803 GlClear.clear [`color];
6805 method display = display ()
6806 method reshape w h = reshape w h
6807 method mouse b d x y m = mouse b d x y m
6808 method motion x y = state.mpos <- (x, y); motion x y
6809 method pmotion x y = state.mpos <- (x, y); pmotion x y
6810 method key k m =
6811 let mascm = m land (
6812 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6813 ) in
6814 match state.keystate with
6815 | KSnone ->
6816 let km = k, mascm in
6817 begin
6818 match
6819 let modehash = state.uioh#modehash in
6820 try Hashtbl.find modehash km
6821 with Not_found ->
6822 try Hashtbl.find globalkeyhash km
6823 with Not_found -> KMinsrt (k, m)
6824 with
6825 | KMinsrt (k, m) -> keyboard k m
6826 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6827 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6829 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6830 List.iter (fun (k, m) -> keyboard k m) insrt;
6831 state.keystate <- KSnone
6832 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6833 state.keystate <- KSinto (keys, insrt)
6834 | _ ->
6835 state.keystate <- KSnone
6837 method enter x y = state.mpos <- (x, y); pmotion x y
6838 method leave = state.mpos <- (-1, -1)
6839 method quit = raise Quit
6840 end) conf.winw conf.winh (platform = Posx) in
6842 state.wsfd <- wsfd;
6844 if not (
6845 List.exists GlMisc.check_extension
6846 [ "GL_ARB_texture_rectangle"
6847 ; "GL_EXT_texture_recangle"
6848 ; "GL_NV_texture_rectangle" ]
6850 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6852 let cr, sw =
6853 match Ne.pipe () with
6854 | Ne.Exn exn ->
6855 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6856 exit 1
6857 | Ne.Res rw -> rw
6858 and sr, cw =
6859 match Ne.pipe () with
6860 | Ne.Exn exn ->
6861 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6862 exit 1
6863 | Ne.Res rw -> rw
6866 cloexec cr;
6867 cloexec sw;
6868 cloexec sr;
6869 cloexec cw;
6871 setcheckers conf.checkers;
6872 redirectstderr ();
6874 init (cr, cw) (
6875 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6876 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6877 !Config.fontpath, !trimcachepath
6879 state.sr <- sr;
6880 state.sw <- sw;
6881 state.text <- "Opening " ^ state.path;
6882 reshape winw winh;
6883 opendoc state.path state.password;
6884 state.uioh <- uioh;
6886 let rec loop deadline =
6887 let r =
6888 match state.errfd with
6889 | None -> [state.sr; state.wsfd]
6890 | Some fd -> [state.sr; state.wsfd; fd]
6892 if state.redisplay
6893 then (
6894 state.redisplay <- false;
6895 display ();
6897 let timeout =
6898 let now = now () in
6899 if deadline > now
6900 then (
6901 if deadline = infinity
6902 then ~-.1.0
6903 else max 0.0 (deadline -. now)
6905 else 0.0
6907 let r, _, _ =
6908 try Unix.select r [] [] timeout
6909 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6911 begin match r with
6912 | [] ->
6913 state.ghyll None;
6914 let newdeadline =
6915 if state.ghyll == noghyll
6916 then
6917 match state.autoscroll with
6918 | Some step when step != 0 ->
6919 let y = state.y + step in
6920 let y =
6921 if y < 0
6922 then state.maxy
6923 else if y >= state.maxy then 0 else y
6925 gotoy y;
6926 if state.mode = View
6927 then state.text <- "";
6928 deadline +. 0.01
6929 | _ -> infinity
6930 else deadline +. 0.01
6932 loop newdeadline
6934 | l ->
6935 let rec checkfds = function
6936 | [] -> ()
6937 | fd :: rest when fd = state.sr ->
6938 let cmd = readcmd state.sr in
6939 act cmd;
6940 checkfds rest
6942 | fd :: rest when fd = state.wsfd ->
6943 Wsi.readresp fd;
6944 checkfds rest
6946 | fd :: rest ->
6947 let s = String.create 80 in
6948 let n = Unix.read fd s 0 80 in
6949 if conf.redirectstderr
6950 then (
6951 Buffer.add_substring state.errmsgs s 0 n;
6952 state.newerrmsgs <- true;
6953 state.redisplay <- true;
6955 else (
6956 prerr_string (String.sub s 0 n);
6957 flush stderr;
6959 checkfds rest
6961 checkfds l;
6962 let newdeadline =
6963 let deadline1 =
6964 if deadline = infinity
6965 then now () +. 0.01
6966 else deadline
6968 match state.autoscroll with
6969 | Some step when step != 0 -> deadline1
6970 | _ -> if state.ghyll == noghyll then infinity else deadline1
6972 loop newdeadline
6973 end;
6976 loop infinity;
6977 with Quit ->
6978 Config.save ();