CBZ trim caching
[llpp.git] / main.ml
blobafba5ecc13134af968950176c35f0fc0d12e8b63
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 fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
44 type link =
45 | Lnotfound
46 | Lfound of int
47 and linkdir =
48 | LDfirst
49 | LDlast
50 | LDfirstvisible of (int * int * int)
51 | LDleft of int
52 | LDright of int
53 | LDdown of int
54 | LDup of int
57 type pagewithlinks =
58 | Pwlnotfound
59 | Pwl of int
62 type keymap =
63 | KMinsrt of key
64 | KMinsrl of key list
65 | KMmulti of key list * key list
66 and key = int * int
67 and keyhash = (key, keymap) Hashtbl.t
68 and keystate =
69 | KSnone
70 | KSinto of (key list * key list)
73 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
74 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
76 type pipe = (Unix.file_descr * Unix.file_descr);;
78 external init : pipe -> params -> unit = "ml_init";;
79 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
80 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
81 external getpdimrect : int -> float array = "ml_getpdimrect";;
82 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
83 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
84 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
85 external measurestr : int -> string -> float = "ml_measure_string";;
86 external getmaxw : unit -> float = "ml_getmaxw";;
87 external postprocess :
88 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
89 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
90 external platform : unit -> platform = "ml_platform";;
91 external setaalevel : int -> unit = "ml_setaalevel";;
92 external realloctexts : int -> bool = "ml_realloctexts";;
93 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
94 external findlink : opaque -> linkdir -> link = "ml_findlink";;
95 external getlink : opaque -> int -> under = "ml_getlink";;
96 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
97 external getlinkcount : opaque -> int = "ml_getlinkcount";;
98 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
99 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
101 let platform_to_string = function
102 | Punknown -> "unknown"
103 | Plinux -> "Linux"
104 | Posx -> "OSX"
105 | Psun -> "Sun"
106 | Pfreebsd -> "FreeBSD"
107 | Pdragonflybsd -> "DragonflyBSD"
108 | Popenbsd -> "OpenBSD"
109 | Pnetbsd -> "NetBSD"
110 | Pcygwin -> "Cygwin"
113 let platform = platform ();;
115 let popen cmd fda =
116 if platform = Pcygwin
117 then (
118 let sh = "/bin/sh" in
119 let args = [|sh; "-c"; cmd|] in
120 let rec std si so se = function
121 | [] -> si, so, se
122 | (fd, 0) :: rest -> std fd so se rest
123 | (fd, -1) :: rest ->
124 Unix.set_close_on_exec fd;
125 std si so se rest
126 | (_, n) :: _ ->
127 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
129 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
130 ignore (Unix.create_process sh args si so se)
132 else popen cmd fda;
135 type x = int
136 and y = int
137 and tilex = int
138 and tiley = int
139 and tileparams = (x * y * width * height * tilex * tiley)
142 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
144 type mpos = int * int
145 and mstate =
146 | Msel of (mpos * mpos)
147 | Mpan of mpos
148 | Mscrolly | Mscrollx
149 | Mzoom of (int * int)
150 | Mzoomrect of (mpos * mpos)
151 | Mnone
154 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
155 and onkey = string -> int -> te
156 and ondone = string -> unit
157 and histcancel = unit -> unit
158 and onhist = ((histcmd -> string) * histcancel)
159 and histcmd = HCnext | HCprev | HCfirst | HClast
160 and cancelonempty = bool
161 and te =
162 | TEstop
163 | TEdone of string
164 | TEcont of string
165 | TEswitch of textentry
168 type 'a circbuf =
169 { store : 'a array
170 ; mutable rc : int
171 ; mutable wc : int
172 ; mutable len : int
176 let bound v minv maxv =
177 max minv (min maxv v);
180 let cbnew n v =
181 { store = Array.create n v
182 ; rc = 0
183 ; wc = 0
184 ; len = 0
188 let drawstring size x y s =
189 Gl.enable `blend;
190 Gl.enable `texture_2d;
191 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
192 ignore (drawstr size x y s);
193 Gl.disable `blend;
194 Gl.disable `texture_2d;
197 let drawstring1 size x y s =
198 drawstr size x y s;
201 let drawstring2 size x y fmt =
202 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
205 let cbcap b = Array.length b.store;;
207 let cbput b v =
208 let cap = cbcap b in
209 b.store.(b.wc) <- v;
210 b.wc <- (b.wc + 1) mod cap;
211 b.rc <- b.wc;
212 b.len <- min (b.len + 1) cap;
215 let cbempty b = b.len = 0;;
217 let cbgetg b circular dir =
218 if cbempty b
219 then b.store.(0)
220 else
221 let rc = b.rc + dir in
222 let rc =
223 if circular
224 then (
225 if rc = -1
226 then b.len-1
227 else (
228 if rc = b.len
229 then 0
230 else rc
233 else max 0 (min rc (b.len-1))
235 b.rc <- rc;
236 b.store.(rc);
239 let cbget b = cbgetg b false;;
240 let cbgetc b = cbgetg b true;;
242 type page =
243 { pageno : int
244 ; pagedimno : int
245 ; pagew : int
246 ; pageh : int
247 ; pagex : int
248 ; pagey : int
249 ; pagevw : int
250 ; pagevh : int
251 ; pagedispx : int
252 ; pagedispy : int
253 ; pagecol : int
257 let debugl l =
258 dolog "l %d dim=%d {" l.pageno l.pagedimno;
259 dolog " WxH %dx%d" l.pagew l.pageh;
260 dolog " vWxH %dx%d" l.pagevw l.pagevh;
261 dolog " pagex,y %d,%d" l.pagex l.pagey;
262 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
263 dolog " column %d" l.pagecol;
264 dolog "}";
267 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
268 dolog "rect {";
269 dolog " x0,y0=(% f, % f)" x0 y0;
270 dolog " x1,y1=(% f, % f)" x1 y1;
271 dolog " x2,y2=(% f, % f)" x2 y2;
272 dolog " x3,y3=(% f, % f)" x3 y3;
273 dolog "}";
276 type multicolumns = multicol * pagegeom
277 and splitcolumns = columncount * pagegeom
278 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
279 and multicol = columncount * covercount * covercount
280 and pdimno = int
281 and columncount = int
282 and covercount = int;;
284 type conf =
285 { mutable scrollbw : int
286 ; mutable scrollh : int
287 ; mutable icase : bool
288 ; mutable preload : bool
289 ; mutable pagebias : int
290 ; mutable verbose : bool
291 ; mutable debug : bool
292 ; mutable scrollstep : int
293 ; mutable hscrollstep : int
294 ; mutable maxhfit : bool
295 ; mutable crophack : bool
296 ; mutable autoscrollstep : int
297 ; mutable maxwait : float option
298 ; mutable hlinks : bool
299 ; mutable underinfo : bool
300 ; mutable interpagespace : interpagespace
301 ; mutable zoom : float
302 ; mutable presentation : bool
303 ; mutable angle : angle
304 ; mutable winw : int
305 ; mutable winh : int
306 ; mutable savebmarks : bool
307 ; mutable proportional : proportional
308 ; mutable trimmargins : trimmargins
309 ; mutable trimfuzz : irect
310 ; mutable memlimit : memsize
311 ; mutable texcount : texcount
312 ; mutable sliceheight : sliceheight
313 ; mutable thumbw : width
314 ; mutable jumpback : bool
315 ; mutable bgcolor : float * float * float
316 ; mutable bedefault : bool
317 ; mutable scrollbarinpm : bool
318 ; mutable tilew : int
319 ; mutable tileh : int
320 ; mutable mustoresize : memsize
321 ; mutable checkers : bool
322 ; mutable aalevel : int
323 ; mutable urilauncher : string
324 ; mutable pathlauncher : string
325 ; mutable colorspace : colorspace
326 ; mutable invert : bool
327 ; mutable colorscale : float
328 ; mutable redirectstderr : bool
329 ; mutable ghyllscroll : (int * int * int) option
330 ; mutable columns : columns
331 ; mutable beyecolumns : columncount option
332 ; mutable selcmd : string
333 ; mutable updatecurs : bool
334 ; mutable keyhashes : (string * keyhash) list
335 ; mutable hfsize : int
336 ; mutable pgscale : float
338 and columns =
339 | Csingle
340 | Cmulti of multicolumns
341 | Csplit of splitcolumns
344 type anchor = pageno * top;;
346 type outline = string * int * anchor;;
348 type rect = float * float * float * float * float * float * float * float;;
350 type tile = opaque * pixmapsize * elapsed
351 and elapsed = float;;
352 type pagemapkey = pageno * gen;;
353 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
354 and row = int
355 and col = int;;
357 let emptyanchor = (0, 0.0);;
359 type infochange = | Memused | Docinfo | Pdim;;
361 class type uioh = object
362 method display : unit
363 method key : int -> int -> uioh
364 method button : int -> bool -> int -> int -> int -> uioh
365 method motion : int -> int -> uioh
366 method pmotion : int -> int -> uioh
367 method infochanged : infochange -> unit
368 method scrollpw : (int * float * float)
369 method scrollph : (int * float * float)
370 method modehash : keyhash
371 end;;
373 type mode =
374 | Birdseye of (conf * leftx * pageno * pageno * anchor)
375 | Textentry of (textentry * onleave)
376 | View
377 | LinkNav of linktarget
378 and onleave = leavetextentrystatus -> unit
379 and leavetextentrystatus = | Cancel | Confirm
380 and helpitem = string * int * action
381 and action =
382 | Noaction
383 | Action of (uioh -> uioh)
384 and linktarget =
385 | Ltexact of (pageno * int)
386 | Ltgendir of int
389 let isbirdseye = function Birdseye _ -> true | _ -> false;;
390 let istextentry = function Textentry _ -> true | _ -> false;;
392 type currently =
393 | Idle
394 | Loading of (page * gen)
395 | Tiling of (
396 page * opaque * colorspace * angle * gen * col * row * width * height
398 | Outlining of outline list
401 let emptykeyhash = Hashtbl.create 0;;
402 let nouioh : uioh = object (self)
403 method display = ()
404 method key _ _ = self
405 method button _ _ _ _ _ = self
406 method motion _ _ = self
407 method pmotion _ _ = self
408 method infochanged _ = ()
409 method scrollpw = (0, nan, nan)
410 method scrollph = (0, nan, nan)
411 method modehash = emptykeyhash
412 end;;
414 type state =
415 { mutable sr : Unix.file_descr
416 ; mutable sw : Unix.file_descr
417 ; mutable wsfd : Unix.file_descr
418 ; mutable errfd : Unix.file_descr option
419 ; mutable stderr : Unix.file_descr
420 ; mutable errmsgs : Buffer.t
421 ; mutable newerrmsgs : bool
422 ; mutable w : int
423 ; mutable x : int
424 ; mutable y : int
425 ; mutable scrollw : int
426 ; mutable hscrollh : int
427 ; mutable anchor : anchor
428 ; mutable ranchors : (string * string * anchor) list
429 ; mutable maxy : int
430 ; mutable layout : page list
431 ; pagemap : (pagemapkey, opaque) Hashtbl.t
432 ; tilemap : (tilemapkey, tile) Hashtbl.t
433 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
434 ; mutable pdims : (pageno * width * height * leftx) list
435 ; mutable pagecount : int
436 ; mutable currently : currently
437 ; mutable mstate : mstate
438 ; mutable searchpattern : string
439 ; mutable rects : (pageno * recttype * rect) list
440 ; mutable rects1 : (pageno * recttype * rect) list
441 ; mutable text : string
442 ; mutable fullscreen : (width * height) option
443 ; mutable mode : mode
444 ; mutable uioh : uioh
445 ; mutable outlines : outline array
446 ; mutable bookmarks : outline list
447 ; mutable path : string
448 ; mutable password : string
449 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
450 ; mutable memused : memsize
451 ; mutable gen : gen
452 ; mutable throttle : (page list * int * float) option
453 ; mutable autoscroll : int option
454 ; mutable ghyll : (int option -> unit)
455 ; mutable help : helpitem array
456 ; mutable docinfo : (int * string) list
457 ; mutable texid : GlTex.texture_id option
458 ; hists : hists
459 ; mutable prevzoom : float
460 ; mutable progress : float
461 ; mutable redisplay : bool
462 ; mutable mpos : mpos
463 ; mutable keystate : keystate
464 ; mutable glinks : bool
465 ; mutable prevcolumns : (columns * float) option
467 and hists =
468 { pat : string circbuf
469 ; pag : string circbuf
470 ; nav : anchor circbuf
471 ; sel : string circbuf
475 let defconf =
476 { scrollbw = 7
477 ; scrollh = 12
478 ; icase = true
479 ; preload = true
480 ; pagebias = 0
481 ; verbose = false
482 ; debug = false
483 ; scrollstep = 24
484 ; hscrollstep = 24
485 ; maxhfit = true
486 ; crophack = false
487 ; autoscrollstep = 2
488 ; maxwait = None
489 ; hlinks = false
490 ; underinfo = false
491 ; interpagespace = 2
492 ; zoom = 1.0
493 ; presentation = false
494 ; angle = 0
495 ; winw = 900
496 ; winh = 900
497 ; savebmarks = true
498 ; proportional = true
499 ; trimmargins = false
500 ; trimfuzz = (0,0,0,0)
501 ; memlimit = 32 lsl 20
502 ; texcount = 256
503 ; sliceheight = 24
504 ; thumbw = 76
505 ; jumpback = true
506 ; bgcolor = (0.5, 0.5, 0.5)
507 ; bedefault = false
508 ; scrollbarinpm = true
509 ; tilew = 2048
510 ; tileh = 2048
511 ; mustoresize = 256 lsl 20
512 ; checkers = true
513 ; aalevel = 8
514 ; urilauncher =
515 (match platform with
516 | Plinux | Pfreebsd | Pdragonflybsd
517 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
518 | Posx -> "open \"%s\""
519 | Pcygwin -> "cygstart \"%s\""
520 | Punknown -> "echo %s")
521 ; pathlauncher = "lp \"%s\""
522 ; selcmd =
523 (match platform with
524 | Plinux | Pfreebsd | Pdragonflybsd
525 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
526 | Posx -> "pbcopy"
527 | Pcygwin -> "wsel"
528 | Punknown -> "cat")
529 ; colorspace = Rgb
530 ; invert = false
531 ; colorscale = 1.0
532 ; redirectstderr = false
533 ; ghyllscroll = None
534 ; columns = Csingle
535 ; beyecolumns = None
536 ; updatecurs = false
537 ; hfsize = 12
538 ; pgscale = 1.0
539 ; keyhashes =
540 let mk n = (n, Hashtbl.create 1) in
541 [ mk "global"
542 ; mk "info"
543 ; mk "help"
544 ; mk "outline"
545 ; mk "listview"
546 ; mk "birdseye"
547 ; mk "textentry"
548 ; mk "links"
549 ; mk "view"
554 let findkeyhash c name =
555 try List.assoc name c.keyhashes
556 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
559 let conf = { defconf with angle = defconf.angle };;
561 let pgscale h = truncate (float h *. conf.pgscale);;
563 type fontstate =
564 { mutable fontsize : int
565 ; mutable wwidth : float
566 ; mutable maxrows : int
570 let fstate =
571 { fontsize = 14
572 ; wwidth = nan
573 ; maxrows = -1
577 let setfontsize n =
578 fstate.fontsize <- n;
579 fstate.wwidth <- measurestr fstate.fontsize "w";
580 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
583 let geturl s =
584 let colonpos = try String.index s ':' with Not_found -> -1 in
585 let len = String.length s in
586 if colonpos >= 0 && colonpos + 3 < len
587 then (
588 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
589 then
590 let schemestartpos =
591 try String.rindex_from s colonpos ' '
592 with Not_found -> -1
594 let scheme =
595 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
597 match scheme with
598 | "http" | "ftp" | "mailto" ->
599 let epos =
600 try String.index_from s colonpos ' '
601 with Not_found -> len
603 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
604 | _ -> ""
605 else ""
607 else ""
610 let gotouri uri =
611 if String.length conf.urilauncher = 0
612 then print_endline uri
613 else (
614 let url = geturl uri in
615 if String.length url = 0
616 then print_endline uri
617 else
618 let re = Str.regexp "%s" in
619 let command = Str.global_replace re url conf.urilauncher in
620 try popen command []
621 with exn ->
622 Printf.eprintf
623 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
624 flush stderr;
628 let version () =
629 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
630 (platform_to_string platform) Sys.word_size Sys.ocaml_version
633 let makehelp () =
634 let strings = version () :: "" :: Help.keys in
635 Array.of_list (
636 List.map (fun s ->
637 let url = geturl s in
638 if String.length url > 0
639 then (s, 0, Action (fun u -> gotouri url; u))
640 else (s, 0, Noaction)
641 ) strings);
644 let noghyll _ = ();;
645 let firstgeomcmds = "", [];;
647 let state =
648 { sr = Unix.stdin
649 ; sw = Unix.stdin
650 ; wsfd = Unix.stdin
651 ; errfd = None
652 ; stderr = Unix.stderr
653 ; errmsgs = Buffer.create 0
654 ; newerrmsgs = false
655 ; x = 0
656 ; y = 0
657 ; w = 0
658 ; scrollw = 0
659 ; hscrollh = 0
660 ; anchor = emptyanchor
661 ; ranchors = []
662 ; layout = []
663 ; maxy = max_int
664 ; tilelru = Queue.create ()
665 ; pagemap = Hashtbl.create 10
666 ; tilemap = Hashtbl.create 10
667 ; pdims = []
668 ; pagecount = 0
669 ; currently = Idle
670 ; mstate = Mnone
671 ; rects = []
672 ; rects1 = []
673 ; text = ""
674 ; mode = View
675 ; fullscreen = None
676 ; searchpattern = ""
677 ; outlines = [||]
678 ; bookmarks = []
679 ; path = ""
680 ; password = ""
681 ; geomcmds = firstgeomcmds
682 ; hists =
683 { nav = cbnew 10 (0, 0.0)
684 ; pat = cbnew 10 ""
685 ; pag = cbnew 10 ""
686 ; sel = cbnew 10 ""
688 ; memused = 0
689 ; gen = 0
690 ; throttle = None
691 ; autoscroll = None
692 ; ghyll = noghyll
693 ; help = makehelp ()
694 ; docinfo = []
695 ; texid = None
696 ; prevzoom = 1.0
697 ; progress = -1.0
698 ; uioh = nouioh
699 ; redisplay = true
700 ; mpos = (-1, -1)
701 ; keystate = KSnone
702 ; glinks = false
703 ; prevcolumns = None
707 let vlog fmt =
708 if conf.verbose
709 then
710 Printf.kprintf prerr_endline fmt
711 else
712 Printf.kprintf ignore fmt
715 let launchpath () =
716 if String.length conf.pathlauncher = 0
717 then print_endline state.path
718 else (
719 let re = Str.regexp "%s" in
720 let command = Str.global_replace re state.path conf.pathlauncher in
721 try popen command []
722 with exn ->
723 Printf.eprintf
724 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
725 flush stderr;
729 module Ne = struct
730 type 'a t = | Res of 'a | Exn of exn;;
732 let pipe () =
733 try Res (Unix.pipe ())
734 with exn -> Exn exn
737 let clo fd f =
738 try Unix.close fd
739 with exn -> f (Printexc.to_string exn)
742 let dup fd =
743 try Res (Unix.dup fd)
744 with exn -> Exn exn
747 let dup2 fd1 fd2 =
748 try Res (Unix.dup2 fd1 fd2)
749 with exn -> Exn exn
751 end;;
753 let redirectstderr () =
754 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
755 if conf.redirectstderr
756 then
757 match Ne.pipe () with
758 | Ne.Exn exn ->
759 dolog "failed to create stderr redirection pipes: %s"
760 (Printexc.to_string exn)
762 | Ne.Res (r, w) ->
763 begin match Ne.dup Unix.stderr with
764 | Ne.Exn exn ->
765 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
766 Ne.clo r (clofail "pipe/r");
767 Ne.clo w (clofail "pipe/w");
769 | Ne.Res dupstderr ->
770 begin match Ne.dup2 w Unix.stderr with
771 | Ne.Exn exn ->
772 dolog "failed to dup2 to stderr: %s"
773 (Printexc.to_string exn);
774 Ne.clo dupstderr (clofail "stderr duplicate");
775 Ne.clo r (clofail "redir pipe/r");
776 Ne.clo w (clofail "redir pipe/w");
778 | Ne.Res () ->
779 state.stderr <- dupstderr;
780 state.errfd <- Some r;
781 end;
783 else (
784 state.newerrmsgs <- false;
785 begin match state.errfd with
786 | Some fd ->
787 begin match Ne.dup2 state.stderr Unix.stderr with
788 | Ne.Exn exn ->
789 dolog "failed to dup2 original stderr: %s"
790 (Printexc.to_string exn)
791 | Ne.Res () ->
792 Ne.clo fd (clofail "dup of stderr");
793 Unix.dup2 state.stderr Unix.stderr;
794 state.errfd <- None;
795 end;
796 | None -> ()
797 end;
798 prerr_string (Buffer.contents state.errmsgs);
799 flush stderr;
800 Buffer.clear state.errmsgs;
804 module G =
805 struct
806 let postRedisplay who =
807 if conf.verbose
808 then prerr_endline ("redisplay for " ^ who);
809 state.redisplay <- true;
811 end;;
813 let getopaque pageno =
814 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
815 with Not_found -> None
818 let putopaque pageno opaque =
819 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
822 let pagetranslatepoint l x y =
823 let dy = y - l.pagedispy in
824 let y = dy + l.pagey in
825 let dx = x - l.pagedispx in
826 let x = dx + l.pagex in
827 (x, y);
830 let getunder x y =
831 let rec f = function
832 | l :: rest ->
833 begin match getopaque l.pageno with
834 | Some opaque ->
835 let x0 = l.pagedispx in
836 let x1 = x0 + l.pagevw in
837 let y0 = l.pagedispy in
838 let y1 = y0 + l.pagevh in
839 if y >= y0 && y <= y1 && x >= x0 && x <= x1
840 then
841 let px, py = pagetranslatepoint l x y in
842 match whatsunder opaque px py with
843 | Unone -> f rest
844 | under -> under
845 else f rest
846 | _ ->
847 f rest
849 | [] -> Unone
851 f state.layout
854 let showtext c s =
855 state.text <- Printf.sprintf "%c%s" c s;
856 G.postRedisplay "showtext";
859 let undertext = function
860 | Unone -> "none"
861 | Ulinkuri s -> s
862 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
863 | Utext s -> "font: " ^ s
864 | Uunexpected s -> "unexpected: " ^ s
865 | Ulaunch s -> "launch: " ^ s
866 | Unamed s -> "named: " ^ s
867 | Uremote (filename, pageno) ->
868 Printf.sprintf "%s: page %d" filename (pageno+1)
871 let updateunder x y =
872 match getunder x y with
873 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
874 | Ulinkuri uri ->
875 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
876 Wsi.setcursor Wsi.CURSOR_INFO
877 | Ulinkgoto (pageno, _) ->
878 if conf.underinfo
879 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
880 Wsi.setcursor Wsi.CURSOR_INFO
881 | Utext s ->
882 if conf.underinfo then showtext 'f' ("ont: " ^ s);
883 Wsi.setcursor Wsi.CURSOR_TEXT
884 | Uunexpected s ->
885 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
886 Wsi.setcursor Wsi.CURSOR_INHERIT
887 | Ulaunch s ->
888 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
889 Wsi.setcursor Wsi.CURSOR_INHERIT
890 | Unamed s ->
891 if conf.underinfo then showtext 'n' ("amed: " ^ s);
892 Wsi.setcursor Wsi.CURSOR_INHERIT
893 | Uremote (filename, pageno) ->
894 if conf.underinfo then showtext 'r'
895 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
896 Wsi.setcursor Wsi.CURSOR_INFO
899 let showlinktype under =
900 if conf.underinfo
901 then
902 match under with
903 | Unone -> ()
904 | under ->
905 let s = undertext under in
906 showtext ' ' s
909 let addchar s c =
910 let b = Buffer.create (String.length s + 1) in
911 Buffer.add_string b s;
912 Buffer.add_char b c;
913 Buffer.contents b;
916 let colorspace_of_string s =
917 match String.lowercase s with
918 | "rgb" -> Rgb
919 | "bgr" -> Bgr
920 | "gray" -> Gray
921 | _ -> failwith "invalid colorspace"
924 let int_of_colorspace = function
925 | Rgb -> 0
926 | Bgr -> 1
927 | Gray -> 2
930 let colorspace_of_int = function
931 | 0 -> Rgb
932 | 1 -> Bgr
933 | 2 -> Gray
934 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
937 let colorspace_to_string = function
938 | Rgb -> "rgb"
939 | Bgr -> "bgr"
940 | Gray -> "gray"
943 let intentry_with_suffix text key =
944 let c =
945 if key >= 32 && key < 127
946 then Char.chr key
947 else '\000'
949 match Char.lowercase c with
950 | '0' .. '9' ->
951 let text = addchar text c in
952 TEcont text
954 | 'k' | 'm' | 'g' ->
955 let text = addchar text c in
956 TEcont text
958 | _ ->
959 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
960 TEcont text
963 let multicolumns_to_string (n, a, b) =
964 if a = 0 && b = 0
965 then Printf.sprintf "%d" n
966 else Printf.sprintf "%d,%d,%d" n a b;
969 let multicolumns_of_string s =
971 (int_of_string s, 0, 0)
972 with _ ->
973 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
976 let readcmd fd =
977 let s = "xxxx" in
978 let n = Unix.read fd s 0 4 in
979 if n != 4 then failwith "incomplete read(len)";
980 let len = 0
981 lor (Char.code s.[0] lsl 24)
982 lor (Char.code s.[1] lsl 16)
983 lor (Char.code s.[2] lsl 8)
984 lor (Char.code s.[3] lsl 0)
986 let s = String.create len in
987 let n = Unix.read fd s 0 len in
988 if n != len then failwith "incomplete read(data)";
992 let btod b = if b then 1 else 0;;
994 let wcmd fmt =
995 let b = Buffer.create 16 in
996 Buffer.add_string b "llll";
997 Printf.kbprintf
998 (fun b ->
999 let s = Buffer.contents b in
1000 let n = String.length s in
1001 let len = n - 4 in
1002 (* dolog "wcmd %S" (String.sub s 4 len); *)
1003 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1004 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1005 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1006 s.[3] <- Char.chr (len land 0xff);
1007 let n' = Unix.write state.sw s 0 n in
1008 if n' != n then failwith "write failed";
1009 ) b fmt;
1012 let calcips h =
1013 if conf.presentation
1014 then
1015 let d = conf.winh - h in
1016 max conf.interpagespace ((d + 1) / 2)
1017 else
1018 conf.interpagespace
1021 let calcheight () =
1022 let rec f pn ph pi fh l =
1023 match l with
1024 | (n, _, h, _) :: rest ->
1025 let ips = calcips h in
1026 let fh =
1027 if conf.presentation
1028 then fh+ips
1029 else (
1030 if isbirdseye state.mode && pn = 0
1031 then fh + ips
1032 else fh
1035 let fh = fh + ((n - pn) * (ph + pi)) in
1036 f n h ips fh rest;
1038 | [] ->
1039 let inc =
1040 if conf.presentation || (isbirdseye state.mode && pn = 0)
1041 then 0
1042 else -pi
1044 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1045 max 0 fh
1047 let fh = f 0 0 0 0 state.pdims in
1051 let calcheight () =
1052 match conf.columns with
1053 | Csingle -> calcheight ()
1054 | Cmulti ((c, _, _), b) ->
1055 let rec loop y h n =
1056 if n < 0
1057 then loop y h (n+1)
1058 else (
1059 if n = Array.length b
1060 then y + h
1061 else
1062 let (_, _, y', (_, _, h', _)) = b.(n) in
1063 let y = min y y'
1064 and h = max h h' in
1065 loop y h (n+1)
1068 loop max_int 0 (((Array.length b - 1) / c) * c)
1069 | Csplit (_, b) ->
1070 if Array.length b > 0
1071 then
1072 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1073 y + h
1074 else 0
1077 let getpageyh pageno =
1078 let rec f pn ph pi y l =
1079 match l with
1080 | (n, _, h, _) :: rest ->
1081 let ips = calcips h in
1082 if n >= pageno
1083 then
1084 let h = if n = pageno then h else ph in
1085 if conf.presentation && n = pageno
1086 then
1087 y + (pageno - pn) * (ph + pi) + pi, h
1088 else
1089 y + (pageno - pn) * (ph + pi), h
1090 else
1091 let y = y + (if conf.presentation then pi else 0) in
1092 let y = y + (n - pn) * (ph + pi) in
1093 f n h ips y rest
1095 | [] ->
1096 y + (pageno - pn) * (ph + pi), ph
1098 f 0 0 0 0 state.pdims
1101 let getpageyh pageno =
1102 match conf.columns with
1103 | Csingle -> getpageyh pageno
1104 | Cmulti (_, b) ->
1105 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1106 y, h
1107 | Csplit (c, b) ->
1108 let n = pageno*c in
1109 let (_, _, y, (_, _, h, _)) = b.(n) in
1110 y, h
1113 let getpagedim pageno =
1114 let rec f ppdim l =
1115 match l with
1116 | (n, _, _, _) as pdim :: rest ->
1117 if n >= pageno
1118 then (if n = pageno then pdim else ppdim)
1119 else f pdim rest
1121 | [] -> ppdim
1123 f (-1, -1, -1, -1) state.pdims
1126 let getpagey pageno = fst (getpageyh pageno);;
1128 let nogeomcmds cmds =
1129 match cmds with
1130 | s, [] -> String.length s = 0
1131 | _ -> false
1134 let layout1 y sh =
1135 let sh = sh - state.hscrollh in
1136 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1137 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1138 match pdims with
1139 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1140 let ips = calcips h in
1141 let yinc =
1142 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1143 then ips
1144 else 0
1146 (w, h, ips, xoff), rest, pdimno + 1, yinc
1147 | _ ->
1148 prev, pdims, pdimno, 0
1150 let dy = dy + yinc in
1151 let py = py + yinc in
1152 if pageno = state.pagecount || dy >= sh
1153 then
1154 accu
1155 else
1156 let vy = y + dy in
1157 if py + h <= vy - yinc
1158 then
1159 let py = py + h + ips in
1160 let dy = max 0 (py - y) in
1161 f ~pageno:(pageno+1)
1162 ~pdimno
1163 ~prev:curr
1166 ~pdims:rest
1167 ~accu
1168 else
1169 let pagey = vy - py in
1170 let pagevh = h - pagey in
1171 let pagevh = min (sh - dy) pagevh in
1172 let off = if yinc > 0 then py - vy else 0 in
1173 let py = py + h + ips in
1174 let pagex, dx =
1175 let xoff = xoff +
1176 if state.w < conf.winw - state.scrollw
1177 then (conf.winw - state.scrollw - state.w) / 2
1178 else 0
1180 let dispx = xoff + state.x in
1181 if dispx < 0
1182 then (-dispx, 0)
1183 else (0, dispx)
1185 let pagevw =
1186 let lw = w - pagex in
1187 min lw (conf.winw - state.scrollw)
1189 let e =
1190 { pageno = pageno
1191 ; pagedimno = pdimno
1192 ; pagew = w
1193 ; pageh = h
1194 ; pagex = pagex
1195 ; pagey = pagey + off
1196 ; pagevw = pagevw
1197 ; pagevh = pagevh - off
1198 ; pagedispx = dx
1199 ; pagedispy = dy + off
1200 ; pagecol = 0
1203 let accu = e :: accu in
1204 f ~pageno:(pageno+1)
1205 ~pdimno
1206 ~prev:curr
1208 ~dy:(dy+pagevh+ips)
1209 ~pdims:rest
1210 ~accu
1212 let accu =
1214 ~pageno:0
1215 ~pdimno:~-1
1216 ~prev:(0,0,0,0)
1217 ~py:0
1218 ~dy:0
1219 ~pdims:state.pdims
1220 ~accu:[]
1222 List.rev accu
1225 let layoutN ((columns, coverA, coverB), b) y sh =
1226 let sh = sh - state.hscrollh in
1227 let rec fold accu n =
1228 if n = Array.length b
1229 then accu
1230 else
1231 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1232 if (vy - y) > sh &&
1233 (n = coverA - 1
1234 || n = state.pagecount - coverB
1235 || (n - coverA) mod columns = columns - 1)
1236 then accu
1237 else
1238 let accu =
1239 if vy + h > y
1240 then
1241 let pagey = max 0 (y - vy) in
1242 let pagedispy = if pagey > 0 then 0 else vy - y in
1243 let pagedispx, pagex =
1244 let pdx =
1245 if n = coverA - 1 || n = state.pagecount - coverB
1246 then state.x + (conf.winw - state.scrollw - w) / 2
1247 else dx + xoff + state.x
1249 if pdx < 0
1250 then 0, -pdx
1251 else pdx, 0
1253 let pagevw =
1254 let vw = conf.winw - state.scrollw - pagedispx in
1255 let pw = w - pagex in
1256 min vw pw
1258 let pagevh = min (h - pagey) (sh - pagedispy) in
1259 if pagevw > 0 && pagevh > 0
1260 then
1261 let e =
1262 { pageno = n
1263 ; pagedimno = pdimno
1264 ; pagew = w
1265 ; pageh = h
1266 ; pagex = pagex
1267 ; pagey = pagey
1268 ; pagevw = pagevw
1269 ; pagevh = pagevh
1270 ; pagedispx = pagedispx
1271 ; pagedispy = pagedispy
1272 ; pagecol = 0
1275 e :: accu
1276 else
1277 accu
1278 else
1279 accu
1281 fold accu (n+1)
1283 List.rev (fold [] 0)
1286 let layoutS (columns, b) y sh =
1287 let sh = sh - state.hscrollh in
1288 let rec fold accu n =
1289 if n = Array.length b
1290 then accu
1291 else
1292 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1293 if (vy - y) > sh
1294 then accu
1295 else
1296 let accu =
1297 if vy + pageh > y
1298 then
1299 let x = xoff + state.x in
1300 let pagey = max 0 (y - vy) in
1301 let pagedispy = if pagey > 0 then 0 else vy - y in
1302 let pagedispx, pagex =
1303 if px = 0
1304 then (
1305 if x < 0
1306 then 0, -x
1307 else x, 0
1309 else (
1310 let px = px - x in
1311 if px < 0
1312 then -px, 0
1313 else 0, px
1316 let pagecolw = pagew/columns in
1317 let pagedispx =
1318 if pagecolw < conf.winw
1319 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1320 else pagedispx
1322 let pagevw =
1323 let vw = conf.winw - pagedispx - state.scrollw in
1324 let pw = pagew - pagex in
1325 min vw pw
1327 let pagevw = min pagevw pagecolw in
1328 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1329 if pagevw > 0 && pagevh > 0
1330 then
1331 let e =
1332 { pageno = n/columns
1333 ; pagedimno = pdimno
1334 ; pagew = pagew
1335 ; pageh = pageh
1336 ; pagex = pagex
1337 ; pagey = pagey
1338 ; pagevw = pagevw
1339 ; pagevh = pagevh
1340 ; pagedispx = pagedispx
1341 ; pagedispy = pagedispy
1342 ; pagecol = n mod columns
1345 e :: accu
1346 else
1347 accu
1348 else
1349 accu
1351 fold accu (n+1)
1353 List.rev (fold [] 0)
1356 let layout y sh =
1357 if nogeomcmds state.geomcmds
1358 then
1359 match conf.columns with
1360 | Csingle -> layout1 y sh
1361 | Cmulti c -> layoutN c y sh
1362 | Csplit s -> layoutS s y sh
1363 else []
1366 let clamp incr =
1367 let y = state.y + incr in
1368 let y = max 0 y in
1369 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1373 let itertiles l f =
1374 let tilex = l.pagex mod conf.tilew in
1375 let tiley = l.pagey mod conf.tileh in
1377 let col = l.pagex / conf.tilew in
1378 let row = l.pagey / conf.tileh in
1380 let rec rowloop row y0 dispy h =
1381 if h = 0
1382 then ()
1383 else (
1384 let dh = conf.tileh - y0 in
1385 let dh = min h dh in
1386 let rec colloop col x0 dispx w =
1387 if w = 0
1388 then ()
1389 else (
1390 let dw = conf.tilew - x0 in
1391 let dw = min w dw in
1393 f col row dispx dispy x0 y0 dw dh;
1394 colloop (col+1) 0 (dispx+dw) (w-dw)
1397 colloop col tilex l.pagedispx l.pagevw;
1398 rowloop (row+1) 0 (dispy+dh) (h-dh)
1401 if l.pagevw > 0 && l.pagevh > 0
1402 then rowloop row tiley l.pagedispy l.pagevh;
1405 let gettileopaque l col row =
1406 let key =
1407 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1409 try Some (Hashtbl.find state.tilemap key)
1410 with Not_found -> None
1413 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1414 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1415 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1418 let drawtiles l color =
1419 GlDraw.color color;
1420 let f col row x y tilex tiley w h =
1421 match gettileopaque l col row with
1422 | Some (opaque, _, t) ->
1423 let params = x, y, w, h, tilex, tiley in
1424 if conf.invert
1425 then (
1426 Gl.enable `blend;
1427 GlFunc.blend_func `zero `one_minus_src_color;
1429 drawtile params opaque;
1430 if conf.invert
1431 then Gl.disable `blend;
1432 if conf.debug
1433 then (
1434 let s = Printf.sprintf
1435 "%d[%d,%d] %f sec"
1436 l.pageno col row t
1438 let w = measurestr fstate.fontsize s in
1439 GlMisc.push_attrib [`current];
1440 GlDraw.color (0.0, 0.0, 0.0);
1441 GlDraw.rect
1442 (float (x-2), float (y-2))
1443 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1444 GlDraw.color (1.0, 1.0, 1.0);
1445 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1446 GlMisc.pop_attrib ();
1449 | _ ->
1450 let w =
1451 let lw = conf.winw - state.scrollw - x in
1452 min lw w
1453 and h =
1454 let lh = conf.winh - y in
1455 min lh h
1457 begin match state.texid with
1458 | Some id ->
1459 Gl.enable `texture_2d;
1460 GlTex.bind_texture `texture_2d id;
1461 let x0 = float x
1462 and y0 = float y
1463 and x1 = float (x+w)
1464 and y1 = float (y+h) in
1466 let tw = float w /. 64.0
1467 and th = float h /. 64.0 in
1468 let tx0 = float tilex /. 64.0
1469 and ty0 = float tiley /. 64.0 in
1470 let tx1 = tx0 +. tw
1471 and ty1 = ty0 +. th in
1472 GlDraw.begins `quads;
1473 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1474 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1475 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1476 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1477 GlDraw.ends ();
1479 Gl.disable `texture_2d;
1480 | None ->
1481 GlDraw.color (1.0, 1.0, 1.0);
1482 GlDraw.rect
1483 (float x, float y)
1484 (float (x+w), float (y+h));
1485 end;
1486 if w > 128 && h > fstate.fontsize + 10
1487 then (
1488 GlDraw.color (0.0, 0.0, 0.0);
1489 let c, r =
1490 if conf.verbose
1491 then (col*conf.tilew, row*conf.tileh)
1492 else col, row
1494 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1496 GlDraw.color color;
1498 itertiles l f
1501 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1503 let tilevisible1 l x y =
1504 let ax0 = l.pagex
1505 and ax1 = l.pagex + l.pagevw
1506 and ay0 = l.pagey
1507 and ay1 = l.pagey + l.pagevh in
1509 let bx0 = x
1510 and by0 = y in
1511 let bx1 = min (bx0 + conf.tilew) l.pagew
1512 and by1 = min (by0 + conf.tileh) l.pageh in
1514 let rx0 = max ax0 bx0
1515 and ry0 = max ay0 by0
1516 and rx1 = min ax1 bx1
1517 and ry1 = min ay1 by1 in
1519 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1520 nonemptyintersection
1523 let tilevisible layout n x y =
1524 let rec findpageinlayout m = function
1525 | l :: rest when l.pageno = n ->
1526 tilevisible1 l x y || (
1527 match conf.columns with
1528 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1529 | _ -> false
1531 | _ :: rest -> findpageinlayout 0 rest
1532 | [] -> false
1534 findpageinlayout 0 layout;
1537 let tileready l x y =
1538 tilevisible1 l x y &&
1539 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1542 let tilepage n p layout =
1543 let rec loop = function
1544 | l :: rest ->
1545 if l.pageno = n
1546 then
1547 let f col row _ _ _ _ _ _ =
1548 if state.currently = Idle
1549 then
1550 match gettileopaque l col row with
1551 | Some _ -> ()
1552 | None ->
1553 let x = col*conf.tilew
1554 and y = row*conf.tileh in
1555 let w =
1556 let w = l.pagew - x in
1557 min w conf.tilew
1559 let h =
1560 let h = l.pageh - y in
1561 min h conf.tileh
1563 wcmd "tile %s %d %d %d %d" p x y w h;
1564 state.currently <-
1565 Tiling (
1566 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1567 conf.tilew, conf.tileh
1570 itertiles l f;
1571 else
1572 loop rest
1574 | [] -> ()
1576 if nogeomcmds state.geomcmds
1577 then loop layout;
1580 let preloadlayout visiblepages =
1581 let presentation = conf.presentation in
1582 let interpagespace = conf.interpagespace in
1583 conf.presentation <- false;
1584 conf.interpagespace <- 0;
1585 let y =
1586 match visiblepages with
1587 | [] -> if state.y >= state.maxy then state.maxy else 0
1588 | l :: _ -> getpagey l.pageno + (l.pagey - min 0 l.pagedispy)
1590 let y = if y < conf.winh then 0 else y - conf.winh in
1591 let h = conf.winh*3 in
1592 let pages = layout y h in
1593 conf.presentation <- presentation;
1594 conf.interpagespace <- interpagespace;
1595 pages;
1598 let load pages =
1599 let rec loop pages =
1600 if state.currently != Idle
1601 then ()
1602 else
1603 match pages with
1604 | l :: rest ->
1605 begin match getopaque l.pageno with
1606 | None ->
1607 wcmd "page %d %d" l.pageno l.pagedimno;
1608 state.currently <- Loading (l, state.gen);
1609 | Some opaque ->
1610 tilepage l.pageno opaque pages;
1611 loop rest
1612 end;
1613 | _ -> ()
1615 if nogeomcmds state.geomcmds
1616 then loop pages
1619 let preload pages =
1620 load pages;
1621 if conf.preload && state.currently = Idle
1622 then load (preloadlayout pages);
1625 let layoutready layout =
1626 let rec fold all ls =
1627 all && match ls with
1628 | l :: rest ->
1629 let seen = ref false in
1630 let allvisible = ref true in
1631 let foo col row _ _ _ _ _ _ =
1632 seen := true;
1633 allvisible := !allvisible &&
1634 begin match gettileopaque l col row with
1635 | Some _ -> true
1636 | None -> false
1639 itertiles l foo;
1640 fold (!seen && !allvisible) rest
1641 | [] -> true
1643 let alltilesvisible = fold true layout in
1644 alltilesvisible;
1647 let gotoy y =
1648 let y = bound y 0 state.maxy in
1649 let y, layout, proceed =
1650 match conf.maxwait with
1651 | Some time when state.ghyll == noghyll ->
1652 begin match state.throttle with
1653 | None ->
1654 let layout = layout y conf.winh in
1655 let ready = layoutready layout in
1656 if not ready
1657 then (
1658 load layout;
1659 state.throttle <- Some (layout, y, now ());
1661 else G.postRedisplay "gotoy showall (None)";
1662 y, layout, ready
1663 | Some (_, _, started) ->
1664 let dt = now () -. started in
1665 if dt > time
1666 then (
1667 state.throttle <- None;
1668 let layout = layout y conf.winh in
1669 load layout;
1670 G.postRedisplay "maxwait";
1671 y, layout, true
1673 else -1, [], false
1676 | _ ->
1677 let layout = layout y conf.winh in
1678 if true || layoutready layout
1679 then G.postRedisplay "gotoy ready";
1680 y, layout, true
1682 if proceed
1683 then (
1684 state.y <- y;
1685 state.layout <- layout;
1686 begin match state.mode with
1687 | LinkNav (Ltexact (pageno, linkno)) ->
1688 let rec loop = function
1689 | [] ->
1690 state.mode <- LinkNav (Ltgendir 0)
1691 | l :: _ when l.pageno = pageno ->
1692 begin match getopaque pageno with
1693 | None ->
1694 state.mode <- LinkNav (Ltgendir 0)
1695 | Some opaque ->
1696 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1697 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1698 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1699 then state.mode <- LinkNav (Ltgendir 0)
1701 | _ :: rest -> loop rest
1703 loop layout
1704 | _ -> ()
1705 end;
1706 begin match state.mode with
1707 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1708 if not (pagevisible layout pageno)
1709 then (
1710 match state.layout with
1711 | [] -> ()
1712 | l :: _ ->
1713 state.mode <- Birdseye (
1714 conf, leftx, l.pageno, hooverpageno, anchor
1717 | LinkNav (Ltgendir dir as lt) ->
1718 let linknav =
1719 let rec loop = function
1720 | [] -> lt
1721 | l :: rest ->
1722 match getopaque l.pageno with
1723 | None -> loop rest
1724 | Some opaque ->
1725 let link =
1726 let ld =
1727 if dir = 0
1728 then LDfirstvisible (l.pagex, l.pagey, dir)
1729 else (
1730 if dir > 0 then LDfirst else LDlast
1733 findlink opaque ld
1735 match link with
1736 | Lnotfound -> loop rest
1737 | Lfound n ->
1738 showlinktype (getlink opaque n);
1739 Ltexact (l.pageno, n)
1741 loop state.layout
1743 state.mode <- LinkNav linknav
1744 | _ -> ()
1745 end;
1746 preload layout;
1748 state.ghyll <- noghyll;
1749 if conf.updatecurs
1750 then (
1751 let mx, my = state.mpos in
1752 updateunder mx my;
1756 let conttiling pageno opaque =
1757 tilepage pageno opaque
1758 (if conf.preload then preloadlayout state.layout else state.layout)
1761 let gotoy_and_clear_text y =
1762 if not conf.verbose then state.text <- "";
1763 gotoy y;
1766 let getanchor () =
1767 match state.layout with
1768 | [] -> emptyanchor
1769 | l :: _ ->
1770 let coloff = l.pagecol * l.pageh in
1771 (l.pageno,
1772 (float (l.pagey - l.pagedispy) +. float coloff) /. float l.pageh)
1775 let getanchory (n, top) =
1776 let y, h = getpageyh n in
1777 y + (truncate (top *. float h));
1780 let gotoanchor anchor =
1781 gotoy (getanchory anchor);
1784 let addnav () =
1785 cbput state.hists.nav (getanchor ());
1788 let getnav dir =
1789 let anchor = cbgetc state.hists.nav dir in
1790 getanchory anchor;
1793 let gotoghyll y =
1794 let scroll f n a b =
1795 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1796 let snake f a b =
1797 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1798 if f < a
1799 then s (float f /. float a)
1800 else (
1801 if f > b
1802 then 1.0 -. s ((float (f-b) /. float (n-b)))
1803 else 1.0
1806 snake f a b
1807 and summa f n a b =
1808 (* courtesy:
1809 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1810 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1811 let iv1 = iv f in
1812 let ins = float a *. iv1
1813 and outs = float (n-b) *. iv1 in
1814 let ones = b - a in
1815 ins +. outs +. float ones
1817 let rec set (_N, _A, _B) y sy =
1818 let sum = summa 1.0 _N _A _B in
1819 let dy = float (y - sy) in
1820 state.ghyll <- (
1821 let rec gf n y1 o =
1822 if n >= _N
1823 then state.ghyll <- noghyll
1824 else
1825 let go n =
1826 let s = scroll n _N _A _B in
1827 let y1 = y1 +. ((s *. dy) /. sum) in
1828 gotoy_and_clear_text (truncate y1);
1829 state.ghyll <- gf (n+1) y1;
1831 match o with
1832 | None -> go n
1833 | Some y' -> set (_N/2, 0, 0) y' state.y
1835 gf 0 (float state.y)
1838 match conf.ghyllscroll with
1839 | None ->
1840 gotoy_and_clear_text y
1841 | Some nab ->
1842 if state.ghyll == noghyll
1843 then set nab y state.y
1844 else state.ghyll (Some y)
1847 let gotopage n top =
1848 let y, h = getpageyh n in
1849 let y = y + (truncate (top *. float h)) in
1850 gotoghyll y
1853 let gotopage1 n top =
1854 let y = getpagey n in
1855 let y = y + top in
1856 gotoghyll y
1859 let invalidate s f =
1860 state.layout <- [];
1861 state.pdims <- [];
1862 state.rects <- [];
1863 state.rects1 <- [];
1864 match state.geomcmds with
1865 | ps, [] when String.length ps = 0 ->
1866 f ();
1867 state.geomcmds <- s, [];
1869 | ps, [] ->
1870 state.geomcmds <- ps, [s, f];
1872 | ps, (s', _) :: rest when s' = s ->
1873 state.geomcmds <- ps, ((s, f) :: rest);
1875 | ps, cmds ->
1876 state.geomcmds <- ps, ((s, f) :: cmds);
1879 let opendoc path password =
1880 state.path <- path;
1881 state.password <- password;
1882 state.gen <- state.gen + 1;
1883 state.docinfo <- [];
1885 setaalevel conf.aalevel;
1886 Wsi.settitle ("llpp " ^ Filename.basename path);
1887 wcmd "open %s\000%s\000" path password;
1888 invalidate "reqlayout"
1889 (fun () ->
1890 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1893 let scalecolor c =
1894 let c = c *. conf.colorscale in
1895 (c, c, c);
1898 let scalecolor2 (r, g, b) =
1899 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1902 let docolumns = function
1903 | Csingle -> ()
1905 | Cmulti ((columns, coverA, coverB), _) ->
1906 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1907 let rec loop pageno pdimno pdim x y rowh pdims =
1908 let rec fixrow m = if m = pageno then () else
1909 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1910 if h < rowh
1911 then (
1912 let y = y + (rowh - h) / 2 in
1913 a.(m) <- (pdimno, x, y, pdim);
1915 fixrow (m+1)
1917 if pageno = state.pagecount
1918 then fixrow (((pageno - 1) / columns) * columns)
1919 else
1920 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1921 match pdims with
1922 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1923 pdimno+1, pdim, rest
1924 | _ ->
1925 pdimno, pdim, pdims
1927 let x, y, rowh' =
1928 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1929 then (
1930 (conf.winw - state.scrollw - w) / 2,
1931 y + rowh + conf.interpagespace, h
1933 else (
1934 if (pageno - coverA) mod columns = 0
1935 then 0, y + rowh + conf.interpagespace, h
1936 else x, y, max rowh h
1939 if pageno > 1 && (pageno - coverA) mod columns = 0
1940 then fixrow (pageno - columns);
1941 a.(pageno) <- (pdimno, x, y, pdim);
1942 let x = x + w + xoff*2 + conf.interpagespace in
1943 loop (pageno+1) pdimno pdim x y rowh' pdims
1945 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1946 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1948 | Csplit (c, _) ->
1949 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1950 let rec loop pageno pdimno pdim y pdims =
1951 if pageno = state.pagecount
1952 then ()
1953 else
1954 let pdimno, ((_, w, h, _) as pdim), pdims =
1955 match pdims with
1956 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1957 pdimno+1, pdim, rest
1958 | _ ->
1959 pdimno, pdim, pdims
1961 let cw = w / c in
1962 let rec loop1 n x y =
1963 if n = c then y else (
1964 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1965 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1968 let y = loop1 0 0 y in
1969 loop (pageno+1) pdimno pdim y pdims
1971 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1972 conf.columns <- Csplit (c, a);
1975 let represent () =
1976 docolumns conf.columns;
1977 state.maxy <- calcheight ();
1978 state.hscrollh <-
1979 if state.w <= conf.winw - state.scrollw
1980 then 0
1981 else state.scrollw
1983 match state.mode with
1984 | Birdseye (_, _, pageno, _, _) ->
1985 let y, h = getpageyh pageno in
1986 let top = (conf.winh - h) / 2 in
1987 gotoy (max 0 (y - top))
1988 | _ -> gotoanchor state.anchor
1991 let reshape w h =
1992 GlDraw.viewport 0 0 w h;
1993 let firsttime = state.geomcmds == firstgeomcmds in
1994 if not firsttime && nogeomcmds state.geomcmds
1995 then state.anchor <- getanchor ();
1997 conf.winw <- w;
1998 let w = truncate (float w *. conf.zoom) - state.scrollw in
1999 let w = max w 2 in
2000 conf.winh <- h;
2001 setfontsize fstate.fontsize;
2002 GlMat.mode `modelview;
2003 GlMat.load_identity ();
2005 GlMat.mode `projection;
2006 GlMat.load_identity ();
2007 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2008 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2009 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2011 let relx =
2012 if conf.zoom <= 1.0
2013 then 0.0
2014 else float state.x /. float state.w
2016 invalidate "geometry"
2017 (fun () ->
2018 state.w <- w;
2019 if not firsttime
2020 then state.x <- truncate (relx *. float w);
2021 let w =
2022 match conf.columns with
2023 | Csingle -> w
2024 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2025 | Csplit (c, _) -> w * c
2027 wcmd "geometry %d %d" w h);
2030 let enttext () =
2031 let len = String.length state.text in
2032 let drawstring s =
2033 let hscrollh =
2034 match state.mode with
2035 | Textentry _
2036 | View ->
2037 let h, _, _ = state.uioh#scrollpw in
2039 | _ -> 0
2041 let rect x w =
2042 GlDraw.rect
2043 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2044 (x+.w, float (conf.winh - hscrollh))
2047 let w = float (conf.winw - state.scrollw - 1) in
2048 if state.progress >= 0.0 && state.progress < 1.0
2049 then (
2050 GlDraw.color (0.3, 0.3, 0.3);
2051 let w1 = w *. state.progress in
2052 rect 0.0 w1;
2053 GlDraw.color (0.0, 0.0, 0.0);
2054 rect w1 (w-.w1)
2056 else (
2057 GlDraw.color (0.0, 0.0, 0.0);
2058 rect 0.0 w;
2061 GlDraw.color (1.0, 1.0, 1.0);
2062 drawstring fstate.fontsize
2063 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2065 let s =
2066 match state.mode with
2067 | Textentry ((prefix, text, _, _, _, _), _) ->
2068 let s =
2069 if len > 0
2070 then
2071 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2072 else
2073 Printf.sprintf "%s%s_" prefix text
2077 | _ -> state.text
2079 let s =
2080 if state.newerrmsgs
2081 then (
2082 if not (istextentry state.mode)
2083 then
2084 let s1 = "(press 'e' to review error messasges)" in
2085 if String.length s > 0 then s ^ " " ^ s1 else s1
2086 else s
2088 else s
2090 if String.length s > 0
2091 then drawstring s
2094 let gctiles () =
2095 let len = Queue.length state.tilelru in
2096 let rec loop qpos =
2097 if state.memused <= conf.memlimit
2098 then ()
2099 else (
2100 if qpos < len
2101 then
2102 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2103 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2104 let (_, pw, ph, _) = getpagedim n in
2106 gen = state.gen
2107 && colorspace = conf.colorspace
2108 && angle = conf.angle
2109 && pagew = pw
2110 && pageh = ph
2111 && (
2112 let layout =
2113 match state.throttle with
2114 | None ->
2115 if conf.preload
2116 then preloadlayout state.layout
2117 else state.layout
2118 | Some (layout, _, _) ->
2119 layout
2121 let x = col*conf.tilew
2122 and y = row*conf.tileh in
2123 tilevisible layout n x y
2125 then Queue.push lruitem state.tilelru
2126 else (
2127 wcmd "freetile %s" p;
2128 state.memused <- state.memused - s;
2129 state.uioh#infochanged Memused;
2130 Hashtbl.remove state.tilemap k;
2132 loop (qpos+1)
2135 loop 0
2138 let flushtiles () =
2139 Queue.iter (fun (k, p, s) ->
2140 wcmd "freetile %s" p;
2141 state.memused <- state.memused - s;
2142 state.uioh#infochanged Memused;
2143 Hashtbl.remove state.tilemap k;
2144 ) state.tilelru;
2145 Queue.clear state.tilelru;
2146 load state.layout;
2149 let logcurrently = function
2150 | Idle -> dolog "Idle"
2151 | Loading (l, gen) ->
2152 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2153 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2154 dolog
2155 "Tiling %d[%d,%d] page=%s cs=%s angle"
2156 l.pageno col row pageopaque
2157 (colorspace_to_string colorspace)
2159 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2160 angle gen conf.angle state.gen
2161 tilew tileh
2162 conf.tilew conf.tileh
2164 | Outlining _ ->
2165 dolog "outlining"
2168 let act cmds =
2169 (* dolog "%S" cmds; *)
2170 let op, args =
2171 let spacepos =
2172 try String.index cmds ' '
2173 with Not_found -> -1
2175 if spacepos = -1
2176 then cmds, ""
2177 else
2178 let l = String.length cmds in
2179 let op = String.sub cmds 0 spacepos in
2180 op, begin
2181 if l - spacepos < 2 then ""
2182 else String.sub cmds (spacepos+1) (l-spacepos-1)
2185 match op with
2186 | "clear" ->
2187 state.uioh#infochanged Pdim;
2188 state.pdims <- [];
2190 | "clearrects" ->
2191 state.rects <- state.rects1;
2192 G.postRedisplay "clearrects";
2194 | "continue" ->
2195 let n =
2196 try Scanf.sscanf args "%u" (fun n -> n)
2197 with exn ->
2198 dolog "error processing 'continue' %S: %s"
2199 cmds (Printexc.to_string exn);
2200 exit 1;
2202 state.pagecount <- n;
2203 begin match state.currently with
2204 | Outlining l ->
2205 state.currently <- Idle;
2206 state.outlines <- Array.of_list (List.rev l)
2207 | _ -> ()
2208 end;
2210 let cur, cmds = state.geomcmds in
2211 if String.length cur = 0
2212 then failwith "umpossible";
2214 begin match List.rev cmds with
2215 | [] ->
2216 state.geomcmds <- "", [];
2217 represent ();
2218 | (s, f) :: rest ->
2219 f ();
2220 state.geomcmds <- s, List.rev rest;
2221 end;
2222 if conf.maxwait = None
2223 then G.postRedisplay "continue";
2225 | "title" ->
2226 Wsi.settitle args
2228 | "msg" ->
2229 showtext ' ' args
2231 | "vmsg" ->
2232 if conf.verbose
2233 then showtext ' ' args
2235 | "progress" ->
2236 let progress, text =
2238 Scanf.sscanf args "%f %n"
2239 (fun f pos ->
2240 f, String.sub args pos (String.length args - pos))
2241 with exn ->
2242 dolog "error processing 'progress' %S: %s"
2243 cmds (Printexc.to_string exn);
2244 exit 1;
2246 state.text <- text;
2247 state.progress <- progress;
2248 G.postRedisplay "progress"
2250 | "firstmatch" ->
2251 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2253 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2254 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2255 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2256 with exn ->
2257 dolog "error processing 'firstmatch' %S: %s"
2258 cmds (Printexc.to_string exn);
2259 exit 1;
2261 let y = (getpagey pageno) + truncate y0 in
2262 addnav ();
2263 gotoy y;
2264 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2266 | "match" ->
2267 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2269 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2270 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2271 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2272 with exn ->
2273 dolog "error processing 'match' %S: %s"
2274 cmds (Printexc.to_string exn);
2275 exit 1;
2277 state.rects1 <-
2278 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2280 | "page" ->
2281 let pageopaque, t =
2283 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2284 with exn ->
2285 dolog "error processing 'page' %S: %s"
2286 cmds (Printexc.to_string exn);
2287 exit 1;
2289 begin match state.currently with
2290 | Loading (l, gen) ->
2291 vlog "page %d took %f sec" l.pageno t;
2292 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2293 begin match state.throttle with
2294 | None ->
2295 let preloadedpages =
2296 if conf.preload
2297 then preloadlayout state.layout
2298 else state.layout
2300 let evict () =
2301 let module IntSet =
2302 Set.Make (struct type t = int let compare = (-) end) in
2303 let set =
2304 List.fold_left (fun s l -> IntSet.add l.pageno s)
2305 IntSet.empty preloadedpages
2307 let evictedpages =
2308 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2309 if not (IntSet.mem pageno set)
2310 then (
2311 wcmd "freepage %s" opaque;
2312 key :: accu
2314 else accu
2315 ) state.pagemap []
2317 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2319 evict ();
2320 state.currently <- Idle;
2321 if gen = state.gen
2322 then (
2323 tilepage l.pageno pageopaque state.layout;
2324 load state.layout;
2325 load preloadedpages;
2326 if pagevisible state.layout l.pageno
2327 && layoutready state.layout
2328 then G.postRedisplay "page";
2331 | Some (layout, _, _) ->
2332 state.currently <- Idle;
2333 tilepage l.pageno pageopaque layout;
2334 load state.layout
2335 end;
2337 | _ ->
2338 dolog "Inconsistent loading state";
2339 logcurrently state.currently;
2340 exit 1
2343 | "tile" ->
2344 let (x, y, opaque, size, t) =
2346 Scanf.sscanf args "%u %u %s %u %f"
2347 (fun x y p size t -> (x, y, p, size, t))
2348 with exn ->
2349 dolog "error processing 'tile' %S: %s"
2350 cmds (Printexc.to_string exn);
2351 exit 1;
2353 begin match state.currently with
2354 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2355 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2357 if tilew != conf.tilew || tileh != conf.tileh
2358 then (
2359 wcmd "freetile %s" opaque;
2360 state.currently <- Idle;
2361 load state.layout;
2363 else (
2364 puttileopaque l col row gen cs angle opaque size t;
2365 state.memused <- state.memused + size;
2366 state.uioh#infochanged Memused;
2367 gctiles ();
2368 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2369 opaque, size) state.tilelru;
2371 let layout =
2372 match state.throttle with
2373 | None -> state.layout
2374 | Some (layout, _, _) -> layout
2377 state.currently <- Idle;
2378 if gen = state.gen
2379 && conf.colorspace = cs
2380 && conf.angle = angle
2381 && tilevisible layout l.pageno x y
2382 then conttiling l.pageno pageopaque;
2384 begin match state.throttle with
2385 | None ->
2386 preload state.layout;
2387 if gen = state.gen
2388 && conf.colorspace = cs
2389 && conf.angle = angle
2390 && tilevisible state.layout l.pageno x y
2391 then G.postRedisplay "tile nothrottle";
2393 | Some (layout, y, _) ->
2394 let ready = layoutready layout in
2395 if ready
2396 then (
2397 state.y <- y;
2398 state.layout <- layout;
2399 state.throttle <- None;
2400 G.postRedisplay "throttle";
2402 else load layout;
2403 end;
2406 | _ ->
2407 dolog "Inconsistent tiling state";
2408 logcurrently state.currently;
2409 exit 1
2412 | "pdim" ->
2413 let pdim =
2415 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2416 with exn ->
2417 dolog "error processing 'pdim' %S: %s"
2418 cmds (Printexc.to_string exn);
2419 exit 1;
2421 state.uioh#infochanged Pdim;
2422 state.pdims <- pdim :: state.pdims
2424 | "o" ->
2425 let (l, n, t, h, pos) =
2427 Scanf.sscanf args "%u %u %d %u %n"
2428 (fun l n t h pos -> l, n, t, h, pos)
2429 with exn ->
2430 dolog "error processing 'o' %S: %s"
2431 cmds (Printexc.to_string exn);
2432 exit 1;
2434 let s = String.sub args pos (String.length args - pos) in
2435 let outline = (s, l, (n, float t /. float h)) in
2436 begin match state.currently with
2437 | Outlining outlines ->
2438 state.currently <- Outlining (outline :: outlines)
2439 | Idle ->
2440 state.currently <- Outlining [outline]
2441 | currently ->
2442 dolog "invalid outlining state";
2443 logcurrently currently
2446 | "info" ->
2447 state.docinfo <- (1, args) :: state.docinfo
2449 | "infoend" ->
2450 state.uioh#infochanged Docinfo;
2451 state.docinfo <- List.rev state.docinfo
2453 | _ ->
2454 dolog "unknown cmd `%S'" cmds
2457 let onhist cb =
2458 let rc = cb.rc in
2459 let action = function
2460 | HCprev -> cbget cb ~-1
2461 | HCnext -> cbget cb 1
2462 | HCfirst -> cbget cb ~-(cb.rc)
2463 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2464 and cancel () = cb.rc <- rc
2465 in (action, cancel)
2468 let search pattern forward =
2469 if String.length pattern > 0
2470 then
2471 let pn, py =
2472 match state.layout with
2473 | [] -> 0, 0
2474 | l :: _ ->
2475 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2477 wcmd "search %d %d %d %d,%s\000"
2478 (btod conf.icase) pn py (btod forward) pattern;
2481 let intentry text key =
2482 let c =
2483 if key >= 32 && key < 127
2484 then Char.chr key
2485 else '\000'
2487 match c with
2488 | '0' .. '9' ->
2489 let text = addchar text c in
2490 TEcont text
2492 | _ ->
2493 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2494 TEcont text
2497 let linknentry text key =
2498 let c =
2499 if key >= 32 && key < 127
2500 then Char.chr key
2501 else '\000'
2503 match c with
2504 | 'a' .. 'z' ->
2505 let text = addchar text c in
2506 TEcont text
2508 | _ ->
2509 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2510 TEcont text
2513 let linkndone f s =
2514 if String.length s > 0
2515 then (
2516 let n =
2517 let l = String.length s in
2518 let rec loop pos n = if pos = l then n else
2519 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2520 loop (pos+1) (n*26 + m)
2521 in loop 0 0
2523 let rec loop n = function
2524 | [] -> ()
2525 | l :: rest ->
2526 match getopaque l.pageno with
2527 | None -> loop n rest
2528 | Some opaque ->
2529 let m = getlinkcount opaque in
2530 if n < m
2531 then (
2532 let under = getlink opaque n in
2533 f under
2535 else loop (n-m) rest
2537 loop n state.layout;
2541 let textentry text key =
2542 if key land 0xff00 = 0xff00
2543 then TEcont text
2544 else TEcont (text ^ Wsi.toutf8 key)
2547 let reqlayout angle proportional =
2548 match state.throttle with
2549 | None ->
2550 if nogeomcmds state.geomcmds
2551 then state.anchor <- getanchor ();
2552 conf.angle <- angle mod 360;
2553 if conf.angle != 0
2554 then (
2555 match state.mode with
2556 | LinkNav _ -> state.mode <- View
2557 | _ -> ()
2559 conf.proportional <- proportional;
2560 invalidate "reqlayout"
2561 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2562 | _ -> ()
2565 let settrim trimmargins trimfuzz =
2566 if nogeomcmds state.geomcmds
2567 then state.anchor <- getanchor ();
2568 conf.trimmargins <- trimmargins;
2569 conf.trimfuzz <- trimfuzz;
2570 let x0, y0, x1, y1 = trimfuzz in
2571 invalidate "settrim"
2572 (fun () ->
2573 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2574 Hashtbl.iter (fun _ opaque ->
2575 wcmd "freepage %s" opaque;
2576 ) state.pagemap;
2577 Hashtbl.clear state.pagemap;
2580 let setzoom zoom =
2581 match state.throttle with
2582 | None ->
2583 let zoom = max 0.01 zoom in
2584 if zoom <> conf.zoom
2585 then (
2586 state.prevzoom <- conf.zoom;
2587 conf.zoom <- zoom;
2588 reshape conf.winw conf.winh;
2589 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2592 | Some (layout, y, started) ->
2593 let time =
2594 match conf.maxwait with
2595 | None -> 0.0
2596 | Some t -> t
2598 let dt = now () -. started in
2599 if dt > time
2600 then (
2601 state.y <- y;
2602 load layout;
2606 let setcolumns mode columns coverA coverB =
2607 state.prevcolumns <- Some (conf.columns, conf.zoom);
2608 if columns < 0
2609 then (
2610 if isbirdseye mode
2611 then showtext '!' "split mode doesn't work in bird's eye"
2612 else (
2613 conf.columns <- Csplit (-columns, [||]);
2614 state.x <- 0;
2615 conf.zoom <- 1.0;
2618 else (
2619 if columns < 2
2620 then (
2621 conf.columns <- Csingle;
2622 state.x <- 0;
2623 setzoom 1.0;
2625 else (
2626 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2627 conf.zoom <- 1.0;
2630 reshape conf.winw conf.winh;
2633 let enterbirdseye () =
2634 let zoom = float conf.thumbw /. float conf.winw in
2635 let birdseyepageno =
2636 let cy = conf.winh / 2 in
2637 let fold = function
2638 | [] -> 0
2639 | l :: rest ->
2640 let rec fold best = function
2641 | [] -> best.pageno
2642 | l :: rest ->
2643 let d = cy - (l.pagedispy + l.pagevh/2)
2644 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2645 if abs d < abs dbest
2646 then fold l rest
2647 else best.pageno
2648 in fold l rest
2650 fold state.layout
2652 state.mode <- Birdseye (
2653 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2655 conf.zoom <- zoom;
2656 conf.presentation <- false;
2657 conf.interpagespace <- 10;
2658 conf.hlinks <- false;
2659 state.x <- 0;
2660 state.mstate <- Mnone;
2661 conf.maxwait <- None;
2662 conf.columns <- (
2663 match conf.beyecolumns with
2664 | Some c ->
2665 conf.zoom <- 1.0;
2666 Cmulti ((c, 0, 0), [||])
2667 | None -> Csingle
2669 Wsi.setcursor Wsi.CURSOR_INHERIT;
2670 if conf.verbose
2671 then
2672 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2673 (100.0*.zoom)
2674 else
2675 state.text <- ""
2677 reshape conf.winw conf.winh;
2680 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2681 state.mode <- View;
2682 conf.zoom <- c.zoom;
2683 conf.presentation <- c.presentation;
2684 conf.interpagespace <- c.interpagespace;
2685 conf.maxwait <- c.maxwait;
2686 conf.hlinks <- c.hlinks;
2687 conf.beyecolumns <- (
2688 match conf.columns with
2689 | Cmulti ((c, _, _), _) -> Some c
2690 | Csingle -> None
2691 | Csplit _ -> failwith "leaving bird's eye split mode"
2693 conf.columns <- (
2694 match c.columns with
2695 | Cmulti (c, _) -> Cmulti (c, [||])
2696 | Csingle -> Csingle
2697 | Csplit (c, _) -> Csplit (c, [||])
2699 state.x <- leftx;
2700 if conf.verbose
2701 then
2702 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2703 (100.0*.conf.zoom)
2705 reshape conf.winw conf.winh;
2706 state.anchor <- if goback then anchor else (pageno, 0.0);
2709 let togglebirdseye () =
2710 match state.mode with
2711 | Birdseye vals -> leavebirdseye vals true
2712 | View -> enterbirdseye ()
2713 | _ -> ()
2716 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2717 let pageno = max 0 (pageno - incr) in
2718 let rec loop = function
2719 | [] -> gotopage1 pageno 0
2720 | l :: _ when l.pageno = pageno ->
2721 if l.pagedispy >= 0 && l.pagey = 0
2722 then G.postRedisplay "upbirdseye"
2723 else gotopage1 pageno 0
2724 | _ :: rest -> loop rest
2726 loop state.layout;
2727 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2730 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2731 let pageno = min (state.pagecount - 1) (pageno + incr) in
2732 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2733 let rec loop = function
2734 | [] ->
2735 let y, h = getpageyh pageno in
2736 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2737 gotoy (clamp dy)
2738 | l :: _ when l.pageno = pageno ->
2739 if l.pagevh != l.pageh
2740 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2741 else G.postRedisplay "downbirdseye"
2742 | _ :: rest -> loop rest
2744 loop state.layout
2747 let optentry mode _ key =
2748 let btos b = if b then "on" else "off" in
2749 if key >= 32 && key < 127
2750 then
2751 let c = Char.chr key in
2752 match c with
2753 | 's' ->
2754 let ondone s =
2755 try conf.scrollstep <- int_of_string s with exc ->
2756 state.text <- Printf.sprintf "bad integer `%s': %s"
2757 s (Printexc.to_string exc)
2759 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2761 | 'A' ->
2762 let ondone s =
2764 conf.autoscrollstep <- int_of_string s;
2765 if state.autoscroll <> None
2766 then state.autoscroll <- Some conf.autoscrollstep
2767 with exc ->
2768 state.text <- Printf.sprintf "bad integer `%s': %s"
2769 s (Printexc.to_string exc)
2771 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2773 | 'C' ->
2774 let ondone s =
2776 let n, a, b = multicolumns_of_string s in
2777 setcolumns mode n a b;
2778 with exc ->
2779 state.text <- Printf.sprintf "bad columns `%s': %s"
2780 s (Printexc.to_string exc)
2782 TEswitch ("columns: ", "", None, textentry, ondone, true)
2784 | 'Z' ->
2785 let ondone s =
2787 let zoom = float (int_of_string s) /. 100.0 in
2788 setzoom zoom
2789 with exc ->
2790 state.text <- Printf.sprintf "bad integer `%s': %s"
2791 s (Printexc.to_string exc)
2793 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2795 | 't' ->
2796 let ondone s =
2798 conf.thumbw <- bound (int_of_string s) 2 4096;
2799 state.text <-
2800 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2801 begin match mode with
2802 | Birdseye beye ->
2803 leavebirdseye beye false;
2804 enterbirdseye ();
2805 | _ -> ();
2807 with exc ->
2808 state.text <- Printf.sprintf "bad integer `%s': %s"
2809 s (Printexc.to_string exc)
2811 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2813 | 'R' ->
2814 let ondone s =
2815 match try
2816 Some (int_of_string s)
2817 with exc ->
2818 state.text <- Printf.sprintf "bad integer `%s': %s"
2819 s (Printexc.to_string exc);
2820 None
2821 with
2822 | Some angle -> reqlayout angle conf.proportional
2823 | None -> ()
2825 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2827 | 'i' ->
2828 conf.icase <- not conf.icase;
2829 TEdone ("case insensitive search " ^ (btos conf.icase))
2831 | 'p' ->
2832 conf.preload <- not conf.preload;
2833 gotoy state.y;
2834 TEdone ("preload " ^ (btos conf.preload))
2836 | 'v' ->
2837 conf.verbose <- not conf.verbose;
2838 TEdone ("verbose " ^ (btos conf.verbose))
2840 | 'd' ->
2841 conf.debug <- not conf.debug;
2842 TEdone ("debug " ^ (btos conf.debug))
2844 | 'h' ->
2845 conf.maxhfit <- not conf.maxhfit;
2846 state.maxy <- calcheight ();
2847 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2849 | 'c' ->
2850 conf.crophack <- not conf.crophack;
2851 TEdone ("crophack " ^ btos conf.crophack)
2853 | 'a' ->
2854 let s =
2855 match conf.maxwait with
2856 | None ->
2857 conf.maxwait <- Some infinity;
2858 "always wait for page to complete"
2859 | Some _ ->
2860 conf.maxwait <- None;
2861 "show placeholder if page is not ready"
2863 TEdone s
2865 | 'f' ->
2866 conf.underinfo <- not conf.underinfo;
2867 TEdone ("underinfo " ^ btos conf.underinfo)
2869 | 'P' ->
2870 conf.savebmarks <- not conf.savebmarks;
2871 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2873 | 'S' ->
2874 let ondone s =
2876 let pageno, py =
2877 match state.layout with
2878 | [] -> 0, 0
2879 | l :: _ ->
2880 l.pageno, l.pagey
2882 conf.interpagespace <- int_of_string s;
2883 docolumns conf.columns;
2884 state.maxy <- calcheight ();
2885 let y = getpagey pageno in
2886 gotoy (y + py)
2887 with exc ->
2888 state.text <- Printf.sprintf "bad integer `%s': %s"
2889 s (Printexc.to_string exc)
2891 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2893 | 'l' ->
2894 reqlayout conf.angle (not conf.proportional);
2895 TEdone ("proportional display " ^ btos conf.proportional)
2897 | 'T' ->
2898 settrim (not conf.trimmargins) conf.trimfuzz;
2899 TEdone ("trim margins " ^ btos conf.trimmargins)
2901 | 'I' ->
2902 conf.invert <- not conf.invert;
2903 TEdone ("invert colors " ^ btos conf.invert)
2905 | 'x' ->
2906 let ondone s =
2907 cbput state.hists.sel s;
2908 conf.selcmd <- s;
2910 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2911 textentry, ondone, true)
2913 | _ ->
2914 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2915 TEstop
2916 else
2917 TEcont state.text
2920 class type lvsource = object
2921 method getitemcount : int
2922 method getitem : int -> (string * int)
2923 method hasaction : int -> bool
2924 method exit :
2925 uioh:uioh ->
2926 cancel:bool ->
2927 active:int ->
2928 first:int ->
2929 pan:int ->
2930 qsearch:string ->
2931 uioh option
2932 method getactive : int
2933 method getfirst : int
2934 method getqsearch : string
2935 method setqsearch : string -> unit
2936 method getpan : int
2937 end;;
2939 class virtual lvsourcebase = object
2940 val mutable m_active = 0
2941 val mutable m_first = 0
2942 val mutable m_qsearch = ""
2943 val mutable m_pan = 0
2944 method getactive = m_active
2945 method getfirst = m_first
2946 method getqsearch = m_qsearch
2947 method getpan = m_pan
2948 method setqsearch s = m_qsearch <- s
2949 end;;
2951 let withoutlastutf8 s =
2952 let len = String.length s in
2953 if len = 0
2954 then s
2955 else
2956 let rec find pos =
2957 if pos = 0
2958 then pos
2959 else
2960 let b = Char.code s.[pos] in
2961 if b land 0b110000 = 0b11000000
2962 then find (pos-1)
2963 else pos-1
2965 let first =
2966 if Char.code s.[len-1] land 0x80 = 0
2967 then len-1
2968 else find (len-1)
2970 String.sub s 0 first;
2973 let textentrykeyboard
2974 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2975 let enttext te =
2976 state.mode <- Textentry (te, onleave);
2977 state.text <- "";
2978 enttext ();
2979 G.postRedisplay "textentrykeyboard enttext";
2981 let histaction cmd =
2982 match opthist with
2983 | None -> ()
2984 | Some (action, _) ->
2985 state.mode <- Textentry (
2986 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2988 G.postRedisplay "textentry histaction"
2990 match key with
2991 | 0xff08 -> (* backspace *)
2992 let s = withoutlastutf8 text in
2993 let len = String.length s in
2994 if cancelonempty && len = 0
2995 then (
2996 onleave Cancel;
2997 G.postRedisplay "textentrykeyboard after cancel";
2999 else (
3000 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3003 | 0xff0d ->
3004 ondone text;
3005 onleave Confirm;
3006 G.postRedisplay "textentrykeyboard after confirm"
3008 | 0xff52 -> histaction HCprev
3009 | 0xff54 -> histaction HCnext
3010 | 0xff50 -> histaction HCfirst
3011 | 0xff57 -> histaction HClast
3013 | 0xff1b -> (* escape*)
3014 if String.length text = 0
3015 then (
3016 begin match opthist with
3017 | None -> ()
3018 | Some (_, onhistcancel) -> onhistcancel ()
3019 end;
3020 onleave Cancel;
3021 state.text <- "";
3022 G.postRedisplay "textentrykeyboard after cancel2"
3024 else (
3025 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3028 | 0xff9f | 0xffff -> () (* delete *)
3030 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3031 begin match onkey text key with
3032 | TEdone text ->
3033 ondone text;
3034 onleave Confirm;
3035 G.postRedisplay "textentrykeyboard after confirm2";
3037 | TEcont text ->
3038 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3040 | TEstop ->
3041 onleave Cancel;
3042 G.postRedisplay "textentrykeyboard after cancel3"
3044 | TEswitch te ->
3045 state.mode <- Textentry (te, onleave);
3046 G.postRedisplay "textentrykeyboard switch";
3047 end;
3049 | _ ->
3050 vlog "unhandled key %s" (Wsi.keyname key)
3053 let firstof first active =
3054 if first > active || abs (first - active) > fstate.maxrows - 1
3055 then max 0 (active - (fstate.maxrows/2))
3056 else first
3059 let calcfirst first active =
3060 if active > first
3061 then
3062 let rows = active - first in
3063 if rows > fstate.maxrows then active - fstate.maxrows else first
3064 else active
3067 let scrollph y maxy =
3068 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3069 let sh = float conf.winh /. sh in
3070 let sh = max sh (float conf.scrollh) in
3072 let percent =
3073 if y = state.maxy
3074 then 1.0
3075 else float y /. float maxy
3077 let position = (float conf.winh -. sh) *. percent in
3079 let position =
3080 if position +. sh > float conf.winh
3081 then float conf.winh -. sh
3082 else position
3084 position, sh;
3087 let coe s = (s :> uioh);;
3089 class listview ~(source:lvsource) ~trusted ~modehash =
3090 object (self)
3091 val m_pan = source#getpan
3092 val m_first = source#getfirst
3093 val m_active = source#getactive
3094 val m_qsearch = source#getqsearch
3095 val m_prev_uioh = state.uioh
3097 method private elemunder y =
3098 let n = y / (fstate.fontsize+1) in
3099 if m_first + n < source#getitemcount
3100 then (
3101 if source#hasaction (m_first + n)
3102 then Some (m_first + n)
3103 else None
3105 else None
3107 method display =
3108 Gl.enable `blend;
3109 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3110 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3111 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3112 GlDraw.color (1., 1., 1.);
3113 Gl.enable `texture_2d;
3114 let fs = fstate.fontsize in
3115 let nfs = fs + 1 in
3116 let ww = fstate.wwidth in
3117 let tabw = 30.0*.ww in
3118 let itemcount = source#getitemcount in
3119 let rec loop row =
3120 if (row - m_first) * nfs > conf.winh
3121 then ()
3122 else (
3123 if row >= 0 && row < itemcount
3124 then (
3125 let (s, level) = source#getitem row in
3126 let y = (row - m_first) * nfs in
3127 let x = 5.0 +. float (level + m_pan) *. ww in
3128 if row = m_active
3129 then (
3130 Gl.disable `texture_2d;
3131 GlDraw.polygon_mode `both `line;
3132 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3133 GlDraw.rect (1., float (y + 1))
3134 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3135 GlDraw.polygon_mode `both `fill;
3136 GlDraw.color (1., 1., 1.);
3137 Gl.enable `texture_2d;
3140 let drawtabularstring s =
3141 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3142 if trusted
3143 then
3144 let tabpos = try String.index s '\t' with Not_found -> -1 in
3145 if tabpos > 0
3146 then
3147 let len = String.length s - tabpos - 1 in
3148 let s1 = String.sub s 0 tabpos
3149 and s2 = String.sub s (tabpos + 1) len in
3150 let nx = drawstr x s1 in
3151 let sw = nx -. x in
3152 let x = x +. (max tabw sw) in
3153 drawstr x s2
3154 else
3155 drawstr x s
3156 else
3157 drawstr x s
3159 let _ = drawtabularstring s in
3160 loop (row+1)
3164 loop m_first;
3165 Gl.disable `blend;
3166 Gl.disable `texture_2d;
3168 method updownlevel incr =
3169 let len = source#getitemcount in
3170 let curlevel =
3171 if m_active >= 0 && m_active < len
3172 then snd (source#getitem m_active)
3173 else -1
3175 let rec flow i =
3176 if i = len then i-1 else if i = -1 then 0 else
3177 let _, l = source#getitem i in
3178 if l != curlevel then i else flow (i+incr)
3180 let active = flow m_active in
3181 let first = calcfirst m_first active in
3182 G.postRedisplay "outline updownlevel";
3183 {< m_active = active; m_first = first >}
3185 method private key1 key mask =
3186 let set1 active first qsearch =
3187 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3189 let search active pattern incr =
3190 let dosearch re =
3191 let rec loop n =
3192 if n >= 0 && n < source#getitemcount
3193 then (
3194 let s, _ = source#getitem n in
3196 (try ignore (Str.search_forward re s 0); true
3197 with Not_found -> false)
3198 then Some n
3199 else loop (n + incr)
3201 else None
3203 loop active
3206 let re = Str.regexp_case_fold pattern in
3207 dosearch re
3208 with Failure s ->
3209 state.text <- s;
3210 None
3212 let itemcount = source#getitemcount in
3213 let find start incr =
3214 let rec find i =
3215 if i = -1 || i = itemcount
3216 then -1
3217 else (
3218 if source#hasaction i
3219 then i
3220 else find (i + incr)
3223 find start
3225 let set active first =
3226 let first = bound first 0 (itemcount - fstate.maxrows) in
3227 state.text <- "";
3228 coe {< m_active = active; m_first = first >}
3230 let navigate incr =
3231 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3232 let active, first =
3233 let incr1 = if incr > 0 then 1 else -1 in
3234 if isvisible m_first m_active
3235 then
3236 let next =
3237 let next = m_active + incr in
3238 let next =
3239 if next < 0 || next >= itemcount
3240 then -1
3241 else find next incr1
3243 if next = -1 || abs (m_active - next) > fstate.maxrows
3244 then -1
3245 else next
3247 if next = -1
3248 then
3249 let first = m_first + incr in
3250 let first = bound first 0 (itemcount - 1) in
3251 let next =
3252 let next = m_active + incr in
3253 let next = bound next 0 (itemcount - 1) in
3254 find next ~-incr1
3256 let active = if next = -1 then m_active else next in
3257 active, first
3258 else
3259 let first = min next m_first in
3260 let first =
3261 if abs (next - first) > fstate.maxrows
3262 then first + incr
3263 else first
3265 next, first
3266 else
3267 let first = m_first + incr in
3268 let first = bound first 0 (itemcount - 1) in
3269 let active =
3270 let next = m_active + incr in
3271 let next = bound next 0 (itemcount - 1) in
3272 let next = find next incr1 in
3273 let active =
3274 if next = -1 || abs (m_active - first) > fstate.maxrows
3275 then (
3276 let active = if m_active = -1 then next else m_active in
3277 active
3279 else next
3281 if isvisible first active
3282 then active
3283 else -1
3285 active, first
3287 G.postRedisplay "listview navigate";
3288 set active first;
3290 match key with
3291 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3292 let incr = if key = 0x72 then -1 else 1 in
3293 let active, first =
3294 match search (m_active + incr) m_qsearch incr with
3295 | None ->
3296 state.text <- m_qsearch ^ " [not found]";
3297 m_active, m_first
3298 | Some active ->
3299 state.text <- m_qsearch;
3300 active, firstof m_first active
3302 G.postRedisplay "listview ctrl-r/s";
3303 set1 active first m_qsearch;
3305 | 0xff08 -> (* backspace *)
3306 if String.length m_qsearch = 0
3307 then coe self
3308 else (
3309 let qsearch = withoutlastutf8 m_qsearch in
3310 let len = String.length qsearch in
3311 if len = 0
3312 then (
3313 state.text <- "";
3314 G.postRedisplay "listview empty qsearch";
3315 set1 m_active m_first "";
3317 else
3318 let active, first =
3319 match search m_active qsearch ~-1 with
3320 | None ->
3321 state.text <- qsearch ^ " [not found]";
3322 m_active, m_first
3323 | Some active ->
3324 state.text <- qsearch;
3325 active, firstof m_first active
3327 G.postRedisplay "listview backspace qsearch";
3328 set1 active first qsearch
3331 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3332 let pattern = m_qsearch ^ Wsi.toutf8 key in
3333 let active, first =
3334 match search m_active pattern 1 with
3335 | None ->
3336 state.text <- pattern ^ " [not found]";
3337 m_active, m_first
3338 | Some active ->
3339 state.text <- pattern;
3340 active, firstof m_first active
3342 G.postRedisplay "listview qsearch add";
3343 set1 active first pattern;
3345 | 0xff1b -> (* escape *)
3346 state.text <- "";
3347 if String.length m_qsearch = 0
3348 then (
3349 G.postRedisplay "list view escape";
3350 begin
3351 match
3352 source#exit (coe self) true m_active m_first m_pan m_qsearch
3353 with
3354 | None -> m_prev_uioh
3355 | Some uioh -> uioh
3358 else (
3359 G.postRedisplay "list view kill qsearch";
3360 source#setqsearch "";
3361 coe {< m_qsearch = "" >}
3364 | 0xff0d -> (* return *)
3365 state.text <- "";
3366 let self = {< m_qsearch = "" >} in
3367 source#setqsearch "";
3368 let opt =
3369 G.postRedisplay "listview enter";
3370 if m_active >= 0 && m_active < source#getitemcount
3371 then (
3372 source#exit (coe self) false m_active m_first m_pan "";
3374 else (
3375 source#exit (coe self) true m_active m_first m_pan "";
3378 begin match opt with
3379 | None -> m_prev_uioh
3380 | Some uioh -> uioh
3383 | 0xff9f | 0xffff -> (* delete *)
3384 coe self
3386 | 0xff52 -> navigate ~-1 (* up *)
3387 | 0xff54 -> navigate 1 (* down *)
3388 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3389 | 0xff56 -> navigate fstate.maxrows (* next *)
3391 | 0xff53 -> (* right *)
3392 state.text <- "";
3393 G.postRedisplay "listview right";
3394 coe {< m_pan = m_pan - 1 >}
3396 | 0xff51 -> (* left *)
3397 state.text <- "";
3398 G.postRedisplay "listview left";
3399 coe {< m_pan = m_pan + 1 >}
3401 | 0xff50 -> (* home *)
3402 let active = find 0 1 in
3403 G.postRedisplay "listview home";
3404 set active 0;
3406 | 0xff57 -> (* end *)
3407 let first = max 0 (itemcount - fstate.maxrows) in
3408 let active = find (itemcount - 1) ~-1 in
3409 G.postRedisplay "listview end";
3410 set active first;
3412 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3413 coe self
3415 | _ ->
3416 dolog "listview unknown key %#x" key; coe self
3418 method key key mask =
3419 match state.mode with
3420 | Textentry te -> textentrykeyboard key mask te; coe self
3421 | _ -> self#key1 key mask
3423 method button button down x y _ =
3424 let opt =
3425 match button with
3426 | 1 when x > conf.winw - conf.scrollbw ->
3427 G.postRedisplay "listview scroll";
3428 if down
3429 then
3430 let _, position, sh = self#scrollph in
3431 if y > truncate position && y < truncate (position +. sh)
3432 then (
3433 state.mstate <- Mscrolly;
3434 Some (coe self)
3436 else
3437 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3438 let first = truncate (s *. float source#getitemcount) in
3439 let first = min source#getitemcount first in
3440 Some (coe {< m_first = first; m_active = first >})
3441 else (
3442 state.mstate <- Mnone;
3443 Some (coe self);
3445 | 1 when not down ->
3446 begin match self#elemunder y with
3447 | Some n ->
3448 G.postRedisplay "listview click";
3449 source#exit
3450 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3451 | _ ->
3452 Some (coe self)
3454 | n when (n == 4 || n == 5) && not down ->
3455 let len = source#getitemcount in
3456 let first =
3457 if n = 5 && m_first + fstate.maxrows >= len
3458 then
3459 m_first
3460 else
3461 let first = m_first + (if n == 4 then -1 else 1) in
3462 bound first 0 (len - 1)
3464 G.postRedisplay "listview wheel";
3465 Some (coe {< m_first = first >})
3466 | n when (n = 6 || n = 7) && not down ->
3467 let inc = m_first + (if n = 7 then -1 else 1) in
3468 G.postRedisplay "listview hwheel";
3469 Some (coe {< m_pan = m_pan + inc >})
3470 | _ ->
3471 Some (coe self)
3473 match opt with
3474 | None -> m_prev_uioh
3475 | Some uioh -> uioh
3477 method motion _ y =
3478 match state.mstate with
3479 | Mscrolly ->
3480 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3481 let first = truncate (s *. float source#getitemcount) in
3482 let first = min source#getitemcount first in
3483 G.postRedisplay "listview motion";
3484 coe {< m_first = first; m_active = first >}
3485 | _ -> coe self
3487 method pmotion x y =
3488 if x < conf.winw - conf.scrollbw
3489 then
3490 let n =
3491 match self#elemunder y with
3492 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3493 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3495 let o =
3496 if n != m_active
3497 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3498 else self
3500 coe o
3501 else (
3502 Wsi.setcursor Wsi.CURSOR_INHERIT;
3503 coe self
3506 method infochanged _ = ()
3508 method scrollpw = (0, 0.0, 0.0)
3509 method scrollph =
3510 let nfs = fstate.fontsize + 1 in
3511 let y = m_first * nfs in
3512 let itemcount = source#getitemcount in
3513 let maxi = max 0 (itemcount - fstate.maxrows) in
3514 let maxy = maxi * nfs in
3515 let p, h = scrollph y maxy in
3516 conf.scrollbw, p, h
3518 method modehash = modehash
3519 end;;
3521 class outlinelistview ~source =
3522 object (self)
3523 inherit listview
3524 ~source:(source :> lvsource)
3525 ~trusted:false
3526 ~modehash:(findkeyhash conf "outline")
3527 as super
3529 method key key mask =
3530 let calcfirst first active =
3531 if active > first
3532 then
3533 let rows = active - first in
3534 let maxrows =
3535 if String.length state.text = 0
3536 then fstate.maxrows
3537 else fstate.maxrows - 2
3539 if rows > maxrows then active - maxrows else first
3540 else active
3542 let navigate incr =
3543 let active = m_active + incr in
3544 let active = bound active 0 (source#getitemcount - 1) in
3545 let first = calcfirst m_first active in
3546 G.postRedisplay "outline navigate";
3547 coe {< m_active = active; m_first = first >}
3549 let ctrl = Wsi.withctrl mask in
3550 match key with
3551 | 110 when ctrl -> (* ctrl-n *)
3552 source#narrow m_qsearch;
3553 G.postRedisplay "outline ctrl-n";
3554 coe {< m_first = 0; m_active = 0 >}
3556 | 117 when ctrl -> (* ctrl-u *)
3557 source#denarrow;
3558 G.postRedisplay "outline ctrl-u";
3559 state.text <- "";
3560 coe {< m_first = 0; m_active = 0 >}
3562 | 108 when ctrl -> (* ctrl-l *)
3563 let first = m_active - (fstate.maxrows / 2) in
3564 G.postRedisplay "outline ctrl-l";
3565 coe {< m_first = first >}
3567 | 0xff9f | 0xffff -> (* delete *)
3568 source#remove m_active;
3569 G.postRedisplay "outline delete";
3570 let active = max 0 (m_active-1) in
3571 coe {< m_first = firstof m_first active;
3572 m_active = active >}
3574 | 0xff52 -> navigate ~-1 (* up *)
3575 | 0xff54 -> navigate 1 (* down *)
3576 | 0xff55 -> (* prior *)
3577 navigate ~-(fstate.maxrows)
3578 | 0xff56 -> (* next *)
3579 navigate fstate.maxrows
3581 | 0xff53 -> (* [ctrl-]right *)
3582 let o =
3583 if ctrl
3584 then (
3585 G.postRedisplay "outline ctrl right";
3586 {< m_pan = m_pan + 1 >}
3588 else self#updownlevel 1
3590 coe o
3592 | 0xff51 -> (* [ctrl-]left *)
3593 let o =
3594 if ctrl
3595 then (
3596 G.postRedisplay "outline ctrl left";
3597 {< m_pan = m_pan - 1 >}
3599 else self#updownlevel ~-1
3601 coe o
3603 | 0xff50 -> (* home *)
3604 G.postRedisplay "outline home";
3605 coe {< m_first = 0; m_active = 0 >}
3607 | 0xff57 -> (* end *)
3608 let active = source#getitemcount - 1 in
3609 let first = max 0 (active - fstate.maxrows) in
3610 G.postRedisplay "outline end";
3611 coe {< m_active = active; m_first = first >}
3613 | _ -> super#key key mask
3616 let outlinesource usebookmarks =
3617 let empty = [||] in
3618 (object
3619 inherit lvsourcebase
3620 val mutable m_items = empty
3621 val mutable m_orig_items = empty
3622 val mutable m_prev_items = empty
3623 val mutable m_narrow_pattern = ""
3624 val mutable m_hadremovals = false
3626 method getitemcount =
3627 Array.length m_items + (if m_hadremovals then 1 else 0)
3629 method getitem n =
3630 if n == Array.length m_items && m_hadremovals
3631 then
3632 ("[Confirm removal]", 0)
3633 else
3634 let s, n, _ = m_items.(n) in
3635 (s, n)
3637 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3638 ignore (uioh, first, qsearch);
3639 let confrimremoval = m_hadremovals && active = Array.length m_items in
3640 let items =
3641 if String.length m_narrow_pattern = 0
3642 then m_orig_items
3643 else m_items
3645 if not cancel
3646 then (
3647 if not confrimremoval
3648 then(
3649 let _, _, anchor = m_items.(active) in
3650 gotoanchor anchor;
3651 m_items <- items;
3653 else (
3654 state.bookmarks <- Array.to_list m_items;
3655 m_orig_items <- m_items;
3658 else m_items <- items;
3659 m_pan <- pan;
3660 None
3662 method hasaction _ = true
3664 method greetmsg =
3665 if Array.length m_items != Array.length m_orig_items
3666 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3667 else ""
3669 method narrow pattern =
3670 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3671 match reopt with
3672 | None -> ()
3673 | Some re ->
3674 let rec loop accu n =
3675 if n = -1
3676 then (
3677 m_narrow_pattern <- pattern;
3678 m_items <- Array.of_list accu
3680 else
3681 let (s, _, _) as o = m_items.(n) in
3682 let accu =
3683 if (try ignore (Str.search_forward re s 0); true
3684 with Not_found -> false)
3685 then o :: accu
3686 else accu
3688 loop accu (n-1)
3690 loop [] (Array.length m_items - 1)
3692 method denarrow =
3693 m_orig_items <- (
3694 if usebookmarks
3695 then Array.of_list state.bookmarks
3696 else state.outlines
3698 m_items <- m_orig_items
3700 method remove m =
3701 if usebookmarks
3702 then
3703 if m >= 0 && m < Array.length m_items
3704 then (
3705 m_hadremovals <- true;
3706 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3707 let n = if n >= m then n+1 else n in
3708 m_items.(n)
3712 method reset anchor items =
3713 m_hadremovals <- false;
3714 if m_orig_items == empty || m_prev_items != items
3715 then (
3716 m_orig_items <- items;
3717 if String.length m_narrow_pattern = 0
3718 then m_items <- items;
3720 m_prev_items <- items;
3721 let rely = getanchory anchor in
3722 let active =
3723 let rec loop n best bestd =
3724 if n = Array.length m_items
3725 then best
3726 else
3727 let (_, _, anchor) = m_items.(n) in
3728 let orely = getanchory anchor in
3729 let d = abs (orely - rely) in
3730 if d < bestd
3731 then loop (n+1) n d
3732 else loop (n+1) best bestd
3734 loop 0 ~-1 max_int
3736 m_active <- active;
3737 m_first <- firstof m_first active
3738 end)
3741 let enterselector usebookmarks =
3742 let source = outlinesource usebookmarks in
3743 fun errmsg ->
3744 let outlines =
3745 if usebookmarks
3746 then Array.of_list state.bookmarks
3747 else state.outlines
3749 if Array.length outlines = 0
3750 then (
3751 showtext ' ' errmsg;
3753 else (
3754 state.text <- source#greetmsg;
3755 Wsi.setcursor Wsi.CURSOR_INHERIT;
3756 let anchor = getanchor () in
3757 source#reset anchor outlines;
3758 state.uioh <- coe (new outlinelistview ~source);
3759 G.postRedisplay "enter selector";
3763 let enteroutlinemode =
3764 let f = enterselector false in
3765 fun ()-> f "Document has no outline";
3768 let enterbookmarkmode =
3769 let f = enterselector true in
3770 fun () -> f "Document has no bookmarks (yet)";
3773 let color_of_string s =
3774 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3775 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3779 let color_to_string (r, g, b) =
3780 let r = truncate (r *. 256.0)
3781 and g = truncate (g *. 256.0)
3782 and b = truncate (b *. 256.0) in
3783 Printf.sprintf "%d/%d/%d" r g b
3786 let irect_of_string s =
3787 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3790 let irect_to_string (x0,y0,x1,y1) =
3791 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3794 let makecheckers () =
3795 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3796 following to say:
3797 converted by Issac Trotts. July 25, 2002 *)
3798 let image_height = 64
3799 and image_width = 64 in
3801 let make_image () =
3802 let image =
3803 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3805 for i = 0 to image_width - 1 do
3806 for j = 0 to image_height - 1 do
3807 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3808 (if (i land 8 ) lxor (j land 8) = 0
3809 then [|255;255;255|] else [|200;200;200|])
3810 done
3811 done;
3812 image
3814 let image = make_image () in
3815 let id = GlTex.gen_texture () in
3816 GlTex.bind_texture `texture_2d id;
3817 GlPix.store (`unpack_alignment 1);
3818 GlTex.image2d image;
3819 List.iter (GlTex.parameter ~target:`texture_2d)
3820 [ `wrap_s `repeat;
3821 `wrap_t `repeat;
3822 `mag_filter `nearest;
3823 `min_filter `nearest ];
3827 let setcheckers enabled =
3828 match state.texid with
3829 | None ->
3830 if enabled then state.texid <- Some (makecheckers ())
3832 | Some texid ->
3833 if not enabled
3834 then (
3835 GlTex.delete_texture texid;
3836 state.texid <- None;
3840 let int_of_string_with_suffix s =
3841 let l = String.length s in
3842 let s1, shift =
3843 if l > 1
3844 then
3845 let suffix = Char.lowercase s.[l-1] in
3846 match suffix with
3847 | 'k' -> String.sub s 0 (l-1), 10
3848 | 'm' -> String.sub s 0 (l-1), 20
3849 | 'g' -> String.sub s 0 (l-1), 30
3850 | _ -> s, 0
3851 else s, 0
3853 let n = int_of_string s1 in
3854 let m = n lsl shift in
3855 if m < 0 || m < n
3856 then raise (Failure "value too large")
3857 else m
3860 let string_with_suffix_of_int n =
3861 if n = 0
3862 then "0"
3863 else
3864 let n, s =
3865 if n land ((1 lsl 20) - 1) = 0
3866 then n lsr 20, "M"
3867 else (
3868 if n land ((1 lsl 10) - 1) = 0
3869 then n lsr 10, "K"
3870 else n, ""
3873 let rec loop s n =
3874 let h = n mod 1000 in
3875 let n = n / 1000 in
3876 if n = 0
3877 then string_of_int h ^ s
3878 else (
3879 let s = Printf.sprintf "_%03d%s" h s in
3880 loop s n
3883 loop "" n ^ s;
3886 let defghyllscroll = (40, 8, 32);;
3887 let ghyllscroll_of_string s =
3888 let (n, a, b) as nab =
3889 if s = "default"
3890 then defghyllscroll
3891 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3893 if n <= a || n <= b || a >= b
3894 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3895 nab;
3898 let ghyllscroll_to_string ((n, a, b) as nab) =
3899 if nab = defghyllscroll
3900 then "default"
3901 else Printf.sprintf "%d,%d,%d" n a b;
3904 let describe_location () =
3905 let f (fn, _) l =
3906 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3908 let fn, ln = List.fold_left f (-1, -1) state.layout in
3909 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3910 let percent =
3911 if maxy <= 0
3912 then 100.
3913 else (100. *. (float state.y /. float maxy))
3915 if fn = ln
3916 then
3917 Printf.sprintf "page %d of %d [%.2f%%]"
3918 (fn+1) state.pagecount percent
3919 else
3920 Printf.sprintf
3921 "pages %d-%d of %d [%.2f%%]"
3922 (fn+1) (ln+1) state.pagecount percent
3925 let enterinfomode =
3926 let btos b = if b then "\xe2\x88\x9a" else "" in
3927 let showextended = ref false in
3928 let leave mode = function
3929 | Confirm -> state.mode <- mode
3930 | Cancel -> state.mode <- mode in
3931 let src =
3932 (object
3933 val mutable m_first_time = true
3934 val mutable m_l = []
3935 val mutable m_a = [||]
3936 val mutable m_prev_uioh = nouioh
3937 val mutable m_prev_mode = View
3939 inherit lvsourcebase
3941 method reset prev_mode prev_uioh =
3942 m_a <- Array.of_list (List.rev m_l);
3943 m_l <- [];
3944 m_prev_mode <- prev_mode;
3945 m_prev_uioh <- prev_uioh;
3946 if m_first_time
3947 then (
3948 let rec loop n =
3949 if n >= Array.length m_a
3950 then ()
3951 else
3952 match m_a.(n) with
3953 | _, _, _, Action _ -> m_active <- n
3954 | _ -> loop (n+1)
3956 loop 0;
3957 m_first_time <- false;
3960 method int name get set =
3961 m_l <-
3962 (name, `int get, 1, Action (
3963 fun u ->
3964 let ondone s =
3965 try set (int_of_string s)
3966 with exn ->
3967 state.text <- Printf.sprintf "bad integer `%s': %s"
3968 s (Printexc.to_string exn)
3970 state.text <- "";
3971 let te = name ^ ": ", "", None, intentry, ondone, true in
3972 state.mode <- Textentry (te, leave m_prev_mode);
3974 )) :: m_l
3976 method int_with_suffix name get set =
3977 m_l <-
3978 (name, `intws get, 1, Action (
3979 fun u ->
3980 let ondone s =
3981 try set (int_of_string_with_suffix s)
3982 with exn ->
3983 state.text <- Printf.sprintf "bad integer `%s': %s"
3984 s (Printexc.to_string exn)
3986 state.text <- "";
3987 let te =
3988 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3990 state.mode <- Textentry (te, leave m_prev_mode);
3992 )) :: m_l
3994 method bool ?(offset=1) ?(btos=btos) name get set =
3995 m_l <-
3996 (name, `bool (btos, get), offset, Action (
3997 fun u ->
3998 let v = get () in
3999 set (not v);
4001 )) :: m_l
4003 method color name get set =
4004 m_l <-
4005 (name, `color get, 1, Action (
4006 fun u ->
4007 let invalid = (nan, nan, nan) in
4008 let ondone s =
4009 let c =
4010 try color_of_string s
4011 with exn ->
4012 state.text <- Printf.sprintf "bad color `%s': %s"
4013 s (Printexc.to_string exn);
4014 invalid
4016 if c <> invalid
4017 then set c;
4019 let te = name ^ ": ", "", None, textentry, ondone, true in
4020 state.text <- color_to_string (get ());
4021 state.mode <- Textentry (te, leave m_prev_mode);
4023 )) :: m_l
4025 method string name get set =
4026 m_l <-
4027 (name, `string get, 1, Action (
4028 fun u ->
4029 let ondone s = set s in
4030 let te = name ^ ": ", "", None, textentry, ondone, true in
4031 state.mode <- Textentry (te, leave m_prev_mode);
4033 )) :: m_l
4035 method colorspace name get set =
4036 m_l <-
4037 (name, `string get, 1, Action (
4038 fun _ ->
4039 let source =
4040 let vals = [| "rgb"; "bgr"; "gray" |] in
4041 (object
4042 inherit lvsourcebase
4044 initializer
4045 m_active <- int_of_colorspace conf.colorspace;
4046 m_first <- 0;
4048 method getitemcount = Array.length vals
4049 method getitem n = (vals.(n), 0)
4050 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4051 ignore (uioh, first, pan, qsearch);
4052 if not cancel then set active;
4053 None
4054 method hasaction _ = true
4055 end)
4057 state.text <- "";
4058 let modehash = findkeyhash conf "info" in
4059 coe (new listview ~source ~trusted:true ~modehash)
4060 )) :: m_l
4062 method caption s offset =
4063 m_l <- (s, `empty, offset, Noaction) :: m_l
4065 method caption2 s f offset =
4066 m_l <- (s, `string f, offset, Noaction) :: m_l
4068 method getitemcount = Array.length m_a
4070 method getitem n =
4071 let tostr = function
4072 | `int f -> string_of_int (f ())
4073 | `intws f -> string_with_suffix_of_int (f ())
4074 | `string f -> f ()
4075 | `color f -> color_to_string (f ())
4076 | `bool (btos, f) -> btos (f ())
4077 | `empty -> ""
4079 let name, t, offset, _ = m_a.(n) in
4080 ((let s = tostr t in
4081 if String.length s > 0
4082 then Printf.sprintf "%s\t%s" name s
4083 else name),
4084 offset)
4086 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4087 let uiohopt =
4088 if not cancel
4089 then (
4090 m_qsearch <- qsearch;
4091 let uioh =
4092 match m_a.(active) with
4093 | _, _, _, Action f -> f uioh
4094 | _ -> uioh
4096 Some uioh
4098 else None
4100 m_active <- active;
4101 m_first <- first;
4102 m_pan <- pan;
4103 uiohopt
4105 method hasaction n =
4106 match m_a.(n) with
4107 | _, _, _, Action _ -> true
4108 | _ -> false
4109 end)
4111 let rec fillsrc prevmode prevuioh =
4112 let sep () = src#caption "" 0 in
4113 let colorp name get set =
4114 src#string name
4115 (fun () -> color_to_string (get ()))
4116 (fun v ->
4118 let c = color_of_string v in
4119 set c
4120 with exn ->
4121 state.text <- Printf.sprintf "bad color `%s': %s"
4122 v (Printexc.to_string exn);
4125 let oldmode = state.mode in
4126 let birdseye = isbirdseye state.mode in
4128 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4130 src#bool "presentation mode"
4131 (fun () -> conf.presentation)
4132 (fun v ->
4133 conf.presentation <- v;
4134 state.anchor <- getanchor ();
4135 represent ());
4137 src#bool "ignore case in searches"
4138 (fun () -> conf.icase)
4139 (fun v -> conf.icase <- v);
4141 src#bool "preload"
4142 (fun () -> conf.preload)
4143 (fun v -> conf.preload <- v);
4145 src#bool "highlight links"
4146 (fun () -> conf.hlinks)
4147 (fun v -> conf.hlinks <- v);
4149 src#bool "under info"
4150 (fun () -> conf.underinfo)
4151 (fun v -> conf.underinfo <- v);
4153 src#bool "persistent bookmarks"
4154 (fun () -> conf.savebmarks)
4155 (fun v -> conf.savebmarks <- v);
4157 src#bool "proportional display"
4158 (fun () -> conf.proportional)
4159 (fun v -> reqlayout conf.angle v);
4161 src#bool "trim margins"
4162 (fun () -> conf.trimmargins)
4163 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4165 src#bool "persistent location"
4166 (fun () -> conf.jumpback)
4167 (fun v -> conf.jumpback <- v);
4169 sep ();
4170 src#int "inter-page space"
4171 (fun () -> conf.interpagespace)
4172 (fun n ->
4173 conf.interpagespace <- n;
4174 docolumns conf.columns;
4175 let pageno, py =
4176 match state.layout with
4177 | [] -> 0, 0
4178 | l :: _ ->
4179 l.pageno, l.pagey
4181 state.maxy <- calcheight ();
4182 let y = getpagey pageno in
4183 gotoy (y + py)
4186 src#int "page bias"
4187 (fun () -> conf.pagebias)
4188 (fun v -> conf.pagebias <- v);
4190 src#int "scroll step"
4191 (fun () -> conf.scrollstep)
4192 (fun n -> conf.scrollstep <- n);
4194 src#int "horizontal scroll step"
4195 (fun () -> conf.hscrollstep)
4196 (fun v -> conf.hscrollstep <- v);
4198 src#int "auto scroll step"
4199 (fun () ->
4200 match state.autoscroll with
4201 | Some step -> step
4202 | _ -> conf.autoscrollstep)
4203 (fun n ->
4204 if state.autoscroll <> None
4205 then state.autoscroll <- Some n;
4206 conf.autoscrollstep <- n);
4208 src#int "zoom"
4209 (fun () -> truncate (conf.zoom *. 100.))
4210 (fun v -> setzoom ((float v) /. 100.));
4212 src#int "rotation"
4213 (fun () -> conf.angle)
4214 (fun v -> reqlayout v conf.proportional);
4216 src#int "scroll bar width"
4217 (fun () -> state.scrollw)
4218 (fun v ->
4219 state.scrollw <- v;
4220 conf.scrollbw <- v;
4221 reshape conf.winw conf.winh;
4224 src#int "scroll handle height"
4225 (fun () -> conf.scrollh)
4226 (fun v -> conf.scrollh <- v;);
4228 src#int "thumbnail width"
4229 (fun () -> conf.thumbw)
4230 (fun v ->
4231 conf.thumbw <- min 4096 v;
4232 match oldmode with
4233 | Birdseye beye ->
4234 leavebirdseye beye false;
4235 enterbirdseye ()
4236 | _ -> ()
4239 let mode = state.mode in
4240 src#string "columns"
4241 (fun () ->
4242 match conf.columns with
4243 | Csingle -> "1"
4244 | Cmulti (multi, _) -> multicolumns_to_string multi
4245 | Csplit (count, _) -> "-" ^ string_of_int count
4247 (fun v ->
4248 let n, a, b = multicolumns_of_string v in
4249 setcolumns mode n a b);
4251 sep ();
4252 src#caption "Presentation mode" 0;
4253 src#bool "scrollbar visible"
4254 (fun () -> conf.scrollbarinpm)
4255 (fun v ->
4256 if v != conf.scrollbarinpm
4257 then (
4258 conf.scrollbarinpm <- v;
4259 if conf.presentation
4260 then (
4261 state.scrollw <- if v then conf.scrollbw else 0;
4262 reshape conf.winw conf.winh;
4267 sep ();
4268 src#caption "Pixmap cache" 0;
4269 src#int_with_suffix "size (advisory)"
4270 (fun () -> conf.memlimit)
4271 (fun v -> conf.memlimit <- v);
4273 src#caption2 "used"
4274 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4275 (string_with_suffix_of_int state.memused)
4276 (Hashtbl.length state.tilemap)) 1;
4278 sep ();
4279 src#caption "Layout" 0;
4280 src#caption2 "Dimension"
4281 (fun () ->
4282 Printf.sprintf "%dx%d (virtual %dx%d)"
4283 conf.winw conf.winh
4284 state.w state.maxy)
4286 if conf.debug
4287 then
4288 src#caption2 "Position" (fun () ->
4289 Printf.sprintf "%dx%d" state.x state.y
4291 else
4292 src#caption2 "Visible" (fun () -> describe_location ()) 1
4295 sep ();
4296 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4297 "Save these parameters as global defaults at exit"
4298 (fun () -> conf.bedefault)
4299 (fun v -> conf.bedefault <- v)
4302 sep ();
4303 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4304 src#bool ~offset:0 ~btos "Extended parameters"
4305 (fun () -> !showextended)
4306 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4307 if !showextended
4308 then (
4309 src#bool "checkers"
4310 (fun () -> conf.checkers)
4311 (fun v -> conf.checkers <- v; setcheckers v);
4312 src#bool "update cursor"
4313 (fun () -> conf.updatecurs)
4314 (fun v -> conf.updatecurs <- v);
4315 src#bool "verbose"
4316 (fun () -> conf.verbose)
4317 (fun v -> conf.verbose <- v);
4318 src#bool "invert colors"
4319 (fun () -> conf.invert)
4320 (fun v -> conf.invert <- v);
4321 src#bool "max fit"
4322 (fun () -> conf.maxhfit)
4323 (fun v -> conf.maxhfit <- v);
4324 src#bool "redirect stderr"
4325 (fun () -> conf.redirectstderr)
4326 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4327 src#string "uri launcher"
4328 (fun () -> conf.urilauncher)
4329 (fun v -> conf.urilauncher <- v);
4330 src#string "path launcher"
4331 (fun () -> conf.pathlauncher)
4332 (fun v -> conf.pathlauncher <- v);
4333 src#string "tile size"
4334 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4335 (fun v ->
4337 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4338 conf.tilew <- max 64 w;
4339 conf.tileh <- max 64 h;
4340 flushtiles ();
4341 with exn ->
4342 state.text <- Printf.sprintf "bad tile size `%s': %s"
4343 v (Printexc.to_string exn));
4344 src#int "texture count"
4345 (fun () -> conf.texcount)
4346 (fun v ->
4347 if realloctexts v
4348 then conf.texcount <- v
4349 else showtext '!' " Failed to set texture count please retry later"
4351 src#int "slice height"
4352 (fun () -> conf.sliceheight)
4353 (fun v ->
4354 conf.sliceheight <- v;
4355 wcmd "sliceh %d" conf.sliceheight;
4357 src#int "anti-aliasing level"
4358 (fun () -> conf.aalevel)
4359 (fun v ->
4360 conf.aalevel <- bound v 0 8;
4361 state.anchor <- getanchor ();
4362 opendoc state.path state.password;
4364 src#string "page scroll scaling factor"
4365 (fun () -> string_of_float conf.pgscale)
4366 (fun v ->
4368 let s = float_of_string v in
4369 conf.pgscale <- s
4370 with exn ->
4371 state.text <- Printf.sprintf
4372 "bad page scroll scaling factor `%s': %s"
4373 v (Printexc.to_string exn)
4376 src#int "ui font size"
4377 (fun () -> fstate.fontsize)
4378 (fun v -> setfontsize (bound v 5 100));
4379 src#int "hint font size"
4380 (fun () -> conf.hfsize)
4381 (fun v -> conf.hfsize <- bound v 5 100);
4382 colorp "background color"
4383 (fun () -> conf.bgcolor)
4384 (fun v -> conf.bgcolor <- v);
4385 src#bool "crop hack"
4386 (fun () -> conf.crophack)
4387 (fun v -> conf.crophack <- v);
4388 src#string "trim fuzz"
4389 (fun () -> irect_to_string conf.trimfuzz)
4390 (fun v ->
4392 conf.trimfuzz <- irect_of_string v;
4393 if conf.trimmargins
4394 then settrim true conf.trimfuzz;
4395 with exn ->
4396 state.text <- Printf.sprintf "bad irect `%s': %s"
4397 v (Printexc.to_string exn)
4399 src#string "throttle"
4400 (fun () ->
4401 match conf.maxwait with
4402 | None -> "show place holder if page is not ready"
4403 | Some time ->
4404 if time = infinity
4405 then "wait for page to fully render"
4406 else
4407 "wait " ^ string_of_float time
4408 ^ " seconds before showing placeholder"
4410 (fun v ->
4412 let f = float_of_string v in
4413 if f <= 0.0
4414 then conf.maxwait <- None
4415 else conf.maxwait <- Some f
4416 with exn ->
4417 state.text <- Printf.sprintf "bad time `%s': %s"
4418 v (Printexc.to_string exn)
4420 src#string "ghyll scroll"
4421 (fun () ->
4422 match conf.ghyllscroll with
4423 | None -> ""
4424 | Some nab -> ghyllscroll_to_string nab
4426 (fun v ->
4428 let gs =
4429 if String.length v = 0
4430 then None
4431 else Some (ghyllscroll_of_string v)
4433 conf.ghyllscroll <- gs
4434 with exn ->
4435 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4436 v (Printexc.to_string exn)
4438 src#string "selection command"
4439 (fun () -> conf.selcmd)
4440 (fun v -> conf.selcmd <- v);
4441 src#colorspace "color space"
4442 (fun () -> colorspace_to_string conf.colorspace)
4443 (fun v ->
4444 conf.colorspace <- colorspace_of_int v;
4445 wcmd "cs %d" v;
4446 load state.layout;
4450 sep ();
4451 src#caption "Document" 0;
4452 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4453 src#caption2 "Pages"
4454 (fun () -> string_of_int state.pagecount) 1;
4455 src#caption2 "Dimensions"
4456 (fun () -> string_of_int (List.length state.pdims)) 1;
4457 if conf.trimmargins
4458 then (
4459 sep ();
4460 src#caption "Trimmed margins" 0;
4461 src#caption2 "Dimensions"
4462 (fun () -> string_of_int (List.length state.pdims)) 1;
4465 sep ();
4466 src#caption "OpenGL" 0;
4467 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4468 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4469 src#reset prevmode prevuioh;
4471 fun () ->
4472 state.text <- "";
4473 let prevmode = state.mode
4474 and prevuioh = state.uioh in
4475 fillsrc prevmode prevuioh;
4476 let source = (src :> lvsource) in
4477 let modehash = findkeyhash conf "info" in
4478 state.uioh <- coe (object (self)
4479 inherit listview ~source ~trusted:true ~modehash as super
4480 val mutable m_prevmemused = 0
4481 method infochanged = function
4482 | Memused ->
4483 if m_prevmemused != state.memused
4484 then (
4485 m_prevmemused <- state.memused;
4486 G.postRedisplay "memusedchanged";
4488 | Pdim -> G.postRedisplay "pdimchanged"
4489 | Docinfo -> fillsrc prevmode prevuioh
4491 method key key mask =
4492 if not (Wsi.withctrl mask)
4493 then
4494 match key with
4495 | 0xff51 -> coe (self#updownlevel ~-1)
4496 | 0xff53 -> coe (self#updownlevel 1)
4497 | _ -> super#key key mask
4498 else super#key key mask
4499 end);
4500 G.postRedisplay "info";
4503 let enterhelpmode =
4504 let source =
4505 (object
4506 inherit lvsourcebase
4507 method getitemcount = Array.length state.help
4508 method getitem n =
4509 let s, n, _ = state.help.(n) in
4510 (s, n)
4512 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4513 let optuioh =
4514 if not cancel
4515 then (
4516 m_qsearch <- qsearch;
4517 match state.help.(active) with
4518 | _, _, Action f -> Some (f uioh)
4519 | _ -> Some (uioh)
4521 else None
4523 m_active <- active;
4524 m_first <- first;
4525 m_pan <- pan;
4526 optuioh
4528 method hasaction n =
4529 match state.help.(n) with
4530 | _, _, Action _ -> true
4531 | _ -> false
4533 initializer
4534 m_active <- -1
4535 end)
4536 in fun () ->
4537 let modehash = findkeyhash conf "help" in
4538 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4539 G.postRedisplay "help";
4542 let entermsgsmode =
4543 let msgsource =
4544 let re = Str.regexp "[\r\n]" in
4545 (object
4546 inherit lvsourcebase
4547 val mutable m_items = [||]
4549 method getitemcount = 1 + Array.length m_items
4551 method getitem n =
4552 if n = 0
4553 then "[Clear]", 0
4554 else m_items.(n-1), 0
4556 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4557 ignore uioh;
4558 if not cancel
4559 then (
4560 if active = 0
4561 then Buffer.clear state.errmsgs;
4562 m_qsearch <- qsearch;
4564 m_active <- active;
4565 m_first <- first;
4566 m_pan <- pan;
4567 None
4569 method hasaction n =
4570 n = 0
4572 method reset =
4573 state.newerrmsgs <- false;
4574 let l = Str.split re (Buffer.contents state.errmsgs) in
4575 m_items <- Array.of_list l
4577 initializer
4578 m_active <- 0
4579 end)
4580 in fun () ->
4581 state.text <- "";
4582 msgsource#reset;
4583 let source = (msgsource :> lvsource) in
4584 let modehash = findkeyhash conf "listview" in
4585 state.uioh <- coe (object
4586 inherit listview ~source ~trusted:false ~modehash as super
4587 method display =
4588 if state.newerrmsgs
4589 then msgsource#reset;
4590 super#display
4591 end);
4592 G.postRedisplay "msgs";
4595 let quickbookmark ?title () =
4596 match state.layout with
4597 | [] -> ()
4598 | l :: _ ->
4599 let title =
4600 match title with
4601 | None ->
4602 let sec = Unix.gettimeofday () in
4603 let tm = Unix.localtime sec in
4604 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4605 (l.pageno+1)
4606 tm.Unix.tm_mday
4607 tm.Unix.tm_mon
4608 (tm.Unix.tm_year + 1900)
4609 tm.Unix.tm_hour
4610 tm.Unix.tm_min
4611 | Some title -> title
4613 state.bookmarks <-
4614 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4615 :: state.bookmarks
4618 let doreshape w h =
4619 state.fullscreen <- None;
4620 Wsi.reshape w h;
4623 let setautoscrollspeed step goingdown =
4624 let incr = max 1 ((abs step) / 2) in
4625 let incr = if goingdown then incr else -incr in
4626 let astep = step + incr in
4627 state.autoscroll <- Some astep;
4630 let gotounder = function
4631 | Ulinkgoto (pageno, top) ->
4632 if pageno >= 0
4633 then (
4634 addnav ();
4635 gotopage1 pageno top;
4638 | Ulinkuri s ->
4639 gotouri s
4641 | Uremote (filename, pageno) ->
4642 let path =
4643 if Sys.file_exists filename
4644 then filename
4645 else
4646 let dir = Filename.dirname state.path in
4647 let path = Filename.concat dir filename in
4648 if Sys.file_exists path
4649 then path
4650 else ""
4652 if String.length path > 0
4653 then (
4654 let anchor = getanchor () in
4655 let ranchor = state.path, state.password, anchor in
4656 state.anchor <- (pageno, 0.0);
4657 state.ranchors <- ranchor :: state.ranchors;
4658 opendoc path "";
4660 else showtext '!' ("Could not find " ^ filename)
4662 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4665 let canpan () =
4666 match conf.columns with
4667 | Csplit _ -> true
4668 | _ -> conf.zoom > 1.0
4671 let viewkeyboard key mask =
4672 let enttext te =
4673 let mode = state.mode in
4674 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4675 state.text <- "";
4676 enttext ();
4677 G.postRedisplay "view:enttext"
4679 let ctrl = Wsi.withctrl mask in
4680 match key with
4681 | 81 -> (* Q *)
4682 exit 0
4684 | 0xff63 -> (* insert *)
4685 if conf.angle mod 360 = 0
4686 then (
4687 state.mode <- LinkNav (Ltgendir 0);
4688 gotoy state.y;
4690 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4692 | 0xff1b | 113 -> (* escape / q *)
4693 begin match state.mstate with
4694 | Mzoomrect _ ->
4695 state.mstate <- Mnone;
4696 Wsi.setcursor Wsi.CURSOR_INHERIT;
4697 G.postRedisplay "kill zoom rect";
4698 | _ ->
4699 match state.ranchors with
4700 | [] -> raise Quit
4701 | (path, password, anchor) :: rest ->
4702 state.ranchors <- rest;
4703 state.anchor <- anchor;
4704 opendoc path password
4705 end;
4707 | 0xff08 -> (* backspace *)
4708 let y = getnav ~-1 in
4709 gotoy_and_clear_text y
4711 | 111 -> (* o *)
4712 enteroutlinemode ()
4714 | 117 -> (* u *)
4715 state.rects <- [];
4716 state.text <- "";
4717 G.postRedisplay "dehighlight";
4719 | 47 | 63 -> (* / ? *)
4720 let ondone isforw s =
4721 cbput state.hists.pat s;
4722 state.searchpattern <- s;
4723 search s isforw
4725 let s = String.create 1 in
4726 s.[0] <- Char.chr key;
4727 enttext (s, "", Some (onhist state.hists.pat),
4728 textentry, ondone (key = 47), true)
4730 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-=*)
4731 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4732 setzoom (conf.zoom +. incr)
4734 | 43 | 0xffab -> (* + *)
4735 let ondone s =
4736 let n =
4737 try int_of_string s with exc ->
4738 state.text <- Printf.sprintf "bad integer `%s': %s"
4739 s (Printexc.to_string exc);
4740 max_int
4742 if n != max_int
4743 then (
4744 conf.pagebias <- n;
4745 state.text <- "page bias is now " ^ string_of_int n;
4748 enttext ("page bias: ", "", None, intentry, ondone, true)
4750 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4751 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4752 setzoom (max 0.01 (conf.zoom -. decr))
4754 | 45 | 0xffad -> (* - *)
4755 let ondone msg = state.text <- msg in
4756 enttext (
4757 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4758 optentry state.mode, ondone, true
4761 | 48 when ctrl -> (* ctrl-0 *)
4762 setzoom 1.0
4764 | 49 when ctrl -> (* ctrl-1 *)
4765 let cols =
4766 match conf.columns with
4767 | Csingle | Cmulti _ -> 1
4768 | Csplit (n, _) -> n
4770 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4771 if zoom < 1.0
4772 then setzoom zoom
4774 | 0xffc6 -> (* f9 *)
4775 togglebirdseye ()
4777 | 57 when ctrl -> (* ctrl-9 *)
4778 togglebirdseye ()
4780 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4781 when not ctrl -> (* 0..9 *)
4782 let ondone s =
4783 let n =
4784 try int_of_string s with exc ->
4785 state.text <- Printf.sprintf "bad integer `%s': %s"
4786 s (Printexc.to_string exc);
4789 if n >= 0
4790 then (
4791 addnav ();
4792 cbput state.hists.pag (string_of_int n);
4793 gotopage1 (n + conf.pagebias - 1) 0;
4796 let pageentry text key =
4797 match Char.unsafe_chr key with
4798 | 'g' -> TEdone text
4799 | _ -> intentry text key
4801 let text = "x" in text.[0] <- Char.chr key;
4802 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4804 | 98 -> (* b *)
4805 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4806 reshape conf.winw conf.winh;
4808 | 108 -> (* l *)
4809 conf.hlinks <- not conf.hlinks;
4810 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4811 G.postRedisplay "toggle highlightlinks";
4813 | 70 -> (* F *)
4814 state.glinks <- true;
4815 let mode = state.mode in
4816 state.mode <- Textentry (
4817 (":", "", None, linknentry, linkndone (fun under ->
4818 addnav ();
4819 gotounder under
4820 ), false
4821 ), fun _ ->
4822 state.glinks <- false;
4823 state.mode <- mode
4825 state.text <- "";
4826 G.postRedisplay "view:linkent(F)"
4828 | 121 -> (* y *)
4829 state.glinks <- true;
4830 let mode = state.mode in
4831 state.mode <- Textentry (
4832 (":", "", None, linknentry, linkndone (fun under ->
4833 match Ne.pipe () with
4834 | Ne.Exn exn ->
4835 showtext '!' (Printf.sprintf "pipe failed: %s"
4836 (Printexc.to_string exn));
4837 | Ne.Res (r, w) ->
4838 let popened =
4839 try popen conf.selcmd [r, 0; w, -1]; true
4840 with exn ->
4841 showtext '!'
4842 (Printf.sprintf "failed to execute %s: %s"
4843 conf.selcmd (Printexc.to_string exn));
4844 false
4846 let clo cap fd =
4847 Ne.clo fd (fun msg ->
4848 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4851 let s = undertext under in
4852 if popened
4853 then
4854 (try
4855 let l = String.length s in
4856 let n = Unix.write w s 0 l in
4857 if n != l
4858 then
4859 showtext '!'
4860 (Printf.sprintf
4861 "failed to write %d characters to sel pipe, wrote %d"
4864 with exn ->
4865 showtext '!'
4866 (Printf.sprintf "failed to write to sel pipe: %s"
4867 (Printexc.to_string exn)
4870 else dolog "%s" s;
4871 clo "pipe/r" r;
4872 clo "pipe/w" w;
4873 ), false
4875 fun _ ->
4876 state.glinks <- false;
4877 state.mode <- mode
4879 state.text <- "";
4880 G.postRedisplay "view:linkent"
4882 | 97 -> (* a *)
4883 begin match state.autoscroll with
4884 | Some step ->
4885 conf.autoscrollstep <- step;
4886 state.autoscroll <- None
4887 | None ->
4888 if conf.autoscrollstep = 0
4889 then state.autoscroll <- Some 1
4890 else state.autoscroll <- Some conf.autoscrollstep
4893 | 112 when ctrl -> (* ctrl-p *)
4894 launchpath ()
4896 | 80 -> (* P *)
4897 conf.presentation <- not conf.presentation;
4898 if conf.presentation
4899 then (
4900 if not conf.scrollbarinpm
4901 then state.scrollw <- 0;
4903 else
4904 state.scrollw <- conf.scrollbw;
4906 showtext ' ' ("presentation mode " ^
4907 if conf.presentation then "on" else "off");
4908 state.anchor <- getanchor ();
4909 represent ()
4911 | 102 -> (* f *)
4912 begin match state.fullscreen with
4913 | None ->
4914 state.fullscreen <- Some (conf.winw, conf.winh);
4915 Wsi.fullscreen ()
4916 | Some (w, h) ->
4917 state.fullscreen <- None;
4918 doreshape w h
4921 | 103 -> (* g *)
4922 gotoy_and_clear_text 0
4924 | 71 -> (* G *)
4925 gotopage1 (state.pagecount - 1) 0
4927 | 112 | 78 -> (* p|N *)
4928 search state.searchpattern false
4930 | 110 | 0xffc0 -> (* n|F3 *)
4931 search state.searchpattern true
4933 | 116 -> (* t *)
4934 begin match state.layout with
4935 | [] -> ()
4936 | l :: _ ->
4937 gotoy_and_clear_text (getpagey l.pageno)
4940 | 32 -> (* space *)
4941 begin match state.layout with
4942 | [] -> ()
4943 | l :: rest ->
4944 match conf.columns with
4945 | Csingle | Cmulti _ ->
4946 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4947 then
4948 let y = clamp (pgscale conf.winh) in
4949 gotoy_and_clear_text y
4950 else
4951 let pageno = min (l.pageno+1) (state.pagecount-1) in
4952 gotoy_and_clear_text (getpagey pageno)
4953 | Csplit (n, _) ->
4954 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4955 then
4956 let pagey, pageh = getpageyh l.pageno in
4957 let pagey = pagey + pageh * l.pagecol in
4958 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4959 gotoy_and_clear_text (pagey + pageh + ips)
4962 | 0xff9f | 0xffff -> (* delete *)
4963 begin match state.layout with
4964 | [] -> ()
4965 | l :: _ ->
4966 match conf.columns with
4967 | Csingle | Cmulti _ ->
4968 if conf.presentation && (l.pagey != 0 || l.pagedispy < 0)
4969 then
4970 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4971 else
4972 let pageno = max 0 (l.pageno-1) in
4973 gotoy_and_clear_text (getpagey pageno)
4974 | Csplit (n, _) ->
4975 let y =
4976 if l.pagecol = 0
4977 then
4978 if l.pageno = 0
4979 then l.pagey
4980 else
4981 let pageno = max 0 (l.pageno-1) in
4982 let pagey, pageh = getpageyh pageno in
4983 pagey + (n-1)*pageh
4984 else
4985 let pagey, pageh = getpageyh l.pageno in
4986 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4988 gotoy_and_clear_text y
4991 | 61 -> (* = *)
4992 showtext ' ' (describe_location ());
4994 | 119 -> (* w *)
4995 begin match state.layout with
4996 | [] -> ()
4997 | l :: _ ->
4998 doreshape (l.pagew + state.scrollw) l.pageh;
4999 G.postRedisplay "w"
5002 | 39 -> (* ' *)
5003 enterbookmarkmode ()
5005 | 104 | 0xffbe -> (* h|F1 *)
5006 enterhelpmode ()
5008 | 105 -> (* i *)
5009 enterinfomode ()
5011 | 101 when conf.redirectstderr -> (* e *)
5012 entermsgsmode ()
5014 | 109 -> (* m *)
5015 let ondone s =
5016 match state.layout with
5017 | l :: _ ->
5018 state.bookmarks <-
5019 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
5020 :: state.bookmarks
5021 | _ -> ()
5023 enttext ("bookmark: ", "", None, textentry, ondone, true)
5025 | 126 -> (* ~ *)
5026 quickbookmark ();
5027 showtext ' ' "Quick bookmark added";
5029 | 122 -> (* z *)
5030 begin match state.layout with
5031 | l :: _ ->
5032 let rect = getpdimrect l.pagedimno in
5033 let w, h =
5034 if conf.crophack
5035 then
5036 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5037 truncate (1.2 *. (rect.(3) -. rect.(0))))
5038 else
5039 (truncate (rect.(1) -. rect.(0)),
5040 truncate (rect.(3) -. rect.(0)))
5042 let w = truncate ((float w)*.conf.zoom)
5043 and h = truncate ((float h)*.conf.zoom) in
5044 if w != 0 && h != 0
5045 then (
5046 state.anchor <- getanchor ();
5047 doreshape (w + state.scrollw) (h + conf.interpagespace)
5049 G.postRedisplay "z";
5051 | [] -> ()
5054 | 50 when ctrl -> (* ctrl-2 *)
5055 let maxw = getmaxw () in
5056 if maxw > 0.0
5057 then setzoom (maxw /. float conf.winw)
5059 | 60 | 62 -> (* < > *)
5060 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5062 | 91 | 93 -> (* [ ] *)
5063 conf.colorscale <-
5064 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5066 G.postRedisplay "brightness";
5068 | 99 when state.mode = View -> (* c *)
5069 let (c, a, b), z =
5070 match state.prevcolumns with
5071 | None -> (1, 0, 0), 1.0
5072 | Some (columns, z) ->
5073 let cab =
5074 match columns with
5075 | Csplit (c, _) -> -c, 0, 0
5076 | Cmulti ((c, a, b), _) -> c, a, b
5077 | Csingle -> 1, 0, 0
5079 cab, z
5081 setcolumns View c a b;
5082 setzoom z;
5084 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5085 setzoom state.prevzoom
5087 | 107 | 0xff52 -> (* k up *)
5088 begin match state.autoscroll with
5089 | None ->
5090 begin match state.mode with
5091 | Birdseye beye -> upbirdseye 1 beye
5092 | _ ->
5093 if ctrl
5094 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5095 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5097 | Some n ->
5098 setautoscrollspeed n false
5101 | 106 | 0xff54 -> (* j down *)
5102 begin match state.autoscroll with
5103 | None ->
5104 begin match state.mode with
5105 | Birdseye beye -> downbirdseye 1 beye
5106 | _ ->
5107 if ctrl
5108 then gotoy_and_clear_text (clamp (conf.winh/2))
5109 else gotoy_and_clear_text (clamp conf.scrollstep)
5111 | Some n ->
5112 setautoscrollspeed n true
5115 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5116 if canpan ()
5117 then
5118 let dx =
5119 if ctrl
5120 then conf.winw / 2
5121 else 10
5123 let dx = if key = 0xff51 then dx else -dx in
5124 state.x <- state.x + dx;
5125 gotoy_and_clear_text state.y
5126 else (
5127 state.text <- "";
5128 G.postRedisplay "lef/right"
5131 | 0xff55 -> (* prior *)
5132 let y =
5133 if ctrl
5134 then
5135 match state.layout with
5136 | [] -> state.y
5137 | l :: _ -> state.y - l.pagey
5138 else
5139 clamp (pgscale (-conf.winh))
5141 gotoghyll y
5143 | 0xff56 -> (* next *)
5144 let y =
5145 if ctrl
5146 then
5147 match List.rev state.layout with
5148 | [] -> state.y
5149 | l :: _ -> getpagey l.pageno
5150 else
5151 clamp (pgscale conf.winh)
5153 gotoghyll y
5155 | 0xff50 -> gotoghyll 0
5156 | 0xff57 -> gotoghyll (clamp state.maxy)
5157 | 0xff53 when Wsi.withalt mask ->
5158 gotoghyll (getnav ~-1)
5159 | 0xff51 when Wsi.withalt mask ->
5160 gotoghyll (getnav 1)
5162 | 114 -> (* r *)
5163 state.anchor <- getanchor ();
5164 opendoc state.path state.password
5166 | 118 when conf.debug -> (* v *)
5167 state.rects <- [];
5168 List.iter (fun l ->
5169 match getopaque l.pageno with
5170 | None -> ()
5171 | Some opaque ->
5172 let x0, y0, x1, y1 = pagebbox opaque in
5173 let a,b = float x0, float y0 in
5174 let c,d = float x1, float y0 in
5175 let e,f = float x1, float y1 in
5176 let h,j = float x0, float y1 in
5177 let rect = (a,b,c,d,e,f,h,j) in
5178 debugrect rect;
5179 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5180 ) state.layout;
5181 G.postRedisplay "v";
5183 | _ ->
5184 vlog "huh? %s" (Wsi.keyname key)
5187 let linknavkeyboard key mask linknav =
5188 let getpage pageno =
5189 let rec loop = function
5190 | [] -> None
5191 | l :: _ when l.pageno = pageno -> Some l
5192 | _ :: rest -> loop rest
5193 in loop state.layout
5195 let doexact (pageno, n) =
5196 match getopaque pageno, getpage pageno with
5197 | Some opaque, Some l ->
5198 if key = 0xff0d
5199 then
5200 let under = getlink opaque n in
5201 G.postRedisplay "link gotounder";
5202 gotounder under;
5203 state.mode <- View;
5204 else
5205 let opt, dir =
5206 match key with
5207 | 0xff50 -> (* home *)
5208 Some (findlink opaque LDfirst), -1
5210 | 0xff57 -> (* end *)
5211 Some (findlink opaque LDlast), 1
5213 | 0xff51 -> (* left *)
5214 Some (findlink opaque (LDleft n)), -1
5216 | 0xff53 -> (* right *)
5217 Some (findlink opaque (LDright n)), 1
5219 | 0xff52 -> (* up *)
5220 Some (findlink opaque (LDup n)), -1
5222 | 0xff54 -> (* down *)
5223 Some (findlink opaque (LDdown n)), 1
5225 | _ -> None, 0
5227 let pwl l dir =
5228 begin match findpwl l.pageno dir with
5229 | Pwlnotfound -> ()
5230 | Pwl pageno ->
5231 let notfound dir =
5232 state.mode <- LinkNav (Ltgendir dir);
5233 let y, h = getpageyh pageno in
5234 let y =
5235 if dir < 0
5236 then y + h - conf.winh
5237 else y
5239 gotoy y
5241 begin match getopaque pageno, getpage pageno with
5242 | Some opaque, Some _ ->
5243 let link =
5244 let ld = if dir > 0 then LDfirst else LDlast in
5245 findlink opaque ld
5247 begin match link with
5248 | Lfound m ->
5249 showlinktype (getlink opaque m);
5250 state.mode <- LinkNav (Ltexact (pageno, m));
5251 G.postRedisplay "linknav jpage";
5252 | _ -> notfound dir
5253 end;
5254 | _ -> notfound dir
5255 end;
5256 end;
5258 begin match opt with
5259 | Some Lnotfound -> pwl l dir;
5260 | Some (Lfound m) ->
5261 if m = n
5262 then pwl l dir
5263 else (
5264 let _, y0, _, y1 = getlinkrect opaque m in
5265 if y0 < l.pagey
5266 then gotopage1 l.pageno y0
5267 else (
5268 let d = fstate.fontsize + 1 in
5269 if y1 - l.pagey > l.pagevh - d
5270 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5271 else G.postRedisplay "linknav";
5273 showlinktype (getlink opaque m);
5274 state.mode <- LinkNav (Ltexact (l.pageno, m));
5277 | None -> viewkeyboard key mask
5278 end;
5279 | _ -> viewkeyboard key mask
5281 if key = 0xff63
5282 then (
5283 state.mode <- View;
5284 G.postRedisplay "leave linknav"
5286 else
5287 match linknav with
5288 | Ltgendir _ -> viewkeyboard key mask
5289 | Ltexact exact -> doexact exact
5292 let keyboard key mask =
5293 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5294 then wcmd "interrupt"
5295 else state.uioh <- state.uioh#key key mask
5298 let birdseyekeyboard key mask
5299 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5300 let incr =
5301 match conf.columns with
5302 | Csingle -> 1
5303 | Cmulti ((c, _, _), _) -> c
5304 | Csplit _ -> failwith "bird's eye split mode"
5306 match key with
5307 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5308 let y, h = getpageyh pageno in
5309 let top = (conf.winh - h) / 2 in
5310 gotoy (max 0 (y - top))
5311 | 0xff0d -> leavebirdseye beye false
5312 | 0xff1b -> leavebirdseye beye true (* escape *)
5313 | 0xff52 -> upbirdseye incr beye (* prior *)
5314 | 0xff54 -> downbirdseye incr beye (* next *)
5315 | 0xff51 -> upbirdseye 1 beye (* up *)
5316 | 0xff53 -> downbirdseye 1 beye (* down *)
5318 | 0xff55 ->
5319 begin match state.layout with
5320 | l :: _ ->
5321 if l.pagey != 0
5322 then (
5323 state.mode <- Birdseye (
5324 oconf, leftx, l.pageno, hooverpageno, anchor
5326 gotopage1 l.pageno 0;
5328 else (
5329 let layout = layout (state.y-conf.winh) conf.winh in
5330 match layout with
5331 | [] -> gotoy (clamp (-conf.winh))
5332 | l :: _ ->
5333 state.mode <- Birdseye (
5334 oconf, leftx, l.pageno, hooverpageno, anchor
5336 gotopage1 l.pageno 0
5339 | [] -> gotoy (clamp (-conf.winh))
5340 end;
5342 | 0xff56 ->
5343 begin match List.rev state.layout with
5344 | l :: _ ->
5345 let layout = layout (state.y + conf.winh) conf.winh in
5346 begin match layout with
5347 | [] ->
5348 let incr = l.pageh - l.pagevh in
5349 if incr = 0
5350 then (
5351 state.mode <-
5352 Birdseye (
5353 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5355 G.postRedisplay "birdseye pagedown";
5357 else gotoy (clamp (incr + conf.interpagespace*2));
5359 | l :: _ ->
5360 state.mode <-
5361 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5362 gotopage1 l.pageno 0;
5365 | [] -> gotoy (clamp conf.winh)
5366 end;
5368 | 0xff50 ->
5369 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5370 gotopage1 0 0
5372 | 0xff57 ->
5373 let pageno = state.pagecount - 1 in
5374 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5375 if not (pagevisible state.layout pageno)
5376 then
5377 let h =
5378 match List.rev state.pdims with
5379 | [] -> conf.winh
5380 | (_, _, h, _) :: _ -> h
5382 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5383 else G.postRedisplay "birdseye end";
5384 | _ -> viewkeyboard key mask
5387 let drawpage l linkindexbase =
5388 let color =
5389 match state.mode with
5390 | Textentry _ -> scalecolor 0.4
5391 | LinkNav _
5392 | View -> scalecolor 1.0
5393 | Birdseye (_, _, pageno, hooverpageno, _) ->
5394 if l.pageno = hooverpageno
5395 then scalecolor 0.9
5396 else (
5397 if l.pageno = pageno
5398 then scalecolor 1.0
5399 else scalecolor 0.8
5402 drawtiles l color;
5403 begin match getopaque l.pageno with
5404 | Some opaque ->
5405 if tileready l l.pagex l.pagey
5406 then
5407 let x = l.pagedispx - l.pagex
5408 and y = l.pagedispy - l.pagey in
5409 let hlmask =
5410 match conf.columns with
5411 | Csingle | Cmulti _ ->
5412 (if conf.hlinks then 1 else 0)
5413 + (if state.glinks
5414 && not (isbirdseye state.mode) then 2 else 0)
5415 | _ -> 0
5417 let s =
5418 match state.mode with
5419 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5420 | _ -> ""
5422 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5423 else 0
5425 | _ -> 0
5426 end;
5429 let scrollindicator () =
5430 let sbw, ph, sh = state.uioh#scrollph in
5431 let sbh, pw, sw = state.uioh#scrollpw in
5433 GlDraw.color (0.64, 0.64, 0.64);
5434 GlDraw.rect
5435 (float (conf.winw - sbw), 0.)
5436 (float conf.winw, float conf.winh)
5438 GlDraw.rect
5439 (0., float (conf.winh - sbh))
5440 (float (conf.winw - state.scrollw - 1), float conf.winh)
5442 GlDraw.color (0.0, 0.0, 0.0);
5444 GlDraw.rect
5445 (float (conf.winw - sbw), ph)
5446 (float conf.winw, ph +. sh)
5448 GlDraw.rect
5449 (pw, float (conf.winh - sbh))
5450 (pw +. sw, float conf.winh)
5454 let showsel () =
5455 match state.mstate with
5456 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5459 | Msel ((x0, y0), (x1, y1)) ->
5460 let rec loop = function
5461 | l :: ls ->
5462 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5463 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5464 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5465 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5466 then
5467 match getopaque l.pageno with
5468 | Some opaque ->
5469 let x0, y0 = pagetranslatepoint l x0 y0 in
5470 let x1, y1 = pagetranslatepoint l x1 y1 in
5471 seltext opaque (x0, y0, x1, y1);
5472 | _ -> ()
5473 else loop ls
5474 | [] -> ()
5476 loop state.layout
5479 let showrects rects =
5480 Gl.enable `blend;
5481 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5482 GlDraw.polygon_mode `both `fill;
5483 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5484 List.iter
5485 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5486 List.iter (fun l ->
5487 if l.pageno = pageno
5488 then (
5489 let dx = float (l.pagedispx - l.pagex) in
5490 let dy = float (l.pagedispy - l.pagey) in
5491 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5492 GlDraw.begins `quads;
5494 GlDraw.vertex2 (x0+.dx, y0+.dy);
5495 GlDraw.vertex2 (x1+.dx, y1+.dy);
5496 GlDraw.vertex2 (x2+.dx, y2+.dy);
5497 GlDraw.vertex2 (x3+.dx, y3+.dy);
5499 GlDraw.ends ();
5501 ) state.layout
5502 ) rects
5504 Gl.disable `blend;
5507 let display () =
5508 GlClear.color (scalecolor2 conf.bgcolor);
5509 GlClear.clear [`color];
5510 let rec loop linkindexbase = function
5511 | l :: rest ->
5512 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5513 loop linkindexbase rest
5514 | [] -> ()
5516 loop 0 state.layout;
5517 let rects =
5518 match state.mode with
5519 | LinkNav (Ltexact (pageno, linkno)) ->
5520 begin match getopaque pageno with
5521 | Some opaque ->
5522 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5523 (pageno, 5, (
5524 float x0, float y0,
5525 float x1, float y0,
5526 float x1, float y1,
5527 float x0, float y1)
5528 ) :: state.rects
5529 | None -> state.rects
5531 | _ -> state.rects
5533 showrects rects;
5534 showsel ();
5535 state.uioh#display;
5536 begin match state.mstate with
5537 | Mzoomrect ((x0, y0), (x1, y1)) ->
5538 Gl.enable `blend;
5539 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5540 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5541 GlDraw.rect (float x0, float y0)
5542 (float x1, float y1);
5543 Gl.disable `blend;
5544 | _ -> ()
5545 end;
5546 enttext ();
5547 scrollindicator ();
5548 Wsi.swapb ();
5551 let zoomrect x y x1 y1 =
5552 let x0 = min x x1
5553 and x1 = max x x1
5554 and y0 = min y y1 in
5555 gotoy (state.y + y0);
5556 state.anchor <- getanchor ();
5557 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5558 let margin =
5559 if state.w < conf.winw - state.scrollw
5560 then (conf.winw - state.scrollw - state.w) / 2
5561 else 0
5563 state.x <- (state.x + margin) - x0;
5564 setzoom zoom;
5565 Wsi.setcursor Wsi.CURSOR_INHERIT;
5566 state.mstate <- Mnone;
5569 let scrollx x =
5570 let winw = conf.winw - state.scrollw - 1 in
5571 let s = float x /. float winw in
5572 let destx = truncate (float (state.w + winw) *. s) in
5573 state.x <- winw - destx;
5574 gotoy_and_clear_text state.y;
5575 state.mstate <- Mscrollx;
5578 let scrolly y =
5579 let s = float y /. float conf.winh in
5580 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5581 gotoy_and_clear_text desty;
5582 state.mstate <- Mscrolly;
5585 let viewmouse button down x y mask =
5586 match button with
5587 | n when (n == 4 || n == 5) && not down ->
5588 if Wsi.withctrl mask
5589 then (
5590 match state.mstate with
5591 | Mzoom (oldn, i) ->
5592 if oldn = n
5593 then (
5594 if i = 2
5595 then
5596 let incr =
5597 match n with
5598 | 5 ->
5599 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5600 | _ ->
5601 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5603 let zoom = conf.zoom -. incr in
5604 setzoom zoom;
5605 state.mstate <- Mzoom (n, 0);
5606 else
5607 state.mstate <- Mzoom (n, i+1);
5609 else state.mstate <- Mzoom (n, 0)
5611 | _ -> state.mstate <- Mzoom (n, 0)
5613 else (
5614 match state.autoscroll with
5615 | Some step -> setautoscrollspeed step (n=4)
5616 | None ->
5617 let incr =
5618 if n = 4
5619 then -conf.scrollstep
5620 else conf.scrollstep
5622 let incr = incr * 2 in
5623 let y = clamp incr in
5624 gotoy_and_clear_text y
5627 | n when (n = 6 || n = 7) && not down && canpan () ->
5628 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5629 gotoy_and_clear_text state.y
5631 | 1 when Wsi.withctrl mask ->
5632 if down
5633 then (
5634 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5635 state.mstate <- Mpan (x, y)
5637 else
5638 state.mstate <- Mnone
5640 | 3 ->
5641 if down
5642 then (
5643 Wsi.setcursor Wsi.CURSOR_CYCLE;
5644 let p = (x, y) in
5645 state.mstate <- Mzoomrect (p, p)
5647 else (
5648 match state.mstate with
5649 | Mzoomrect ((x0, y0), _) ->
5650 if abs (x-x0) > 10 && abs (y - y0) > 10
5651 then zoomrect x0 y0 x y
5652 else (
5653 state.mstate <- Mnone;
5654 Wsi.setcursor Wsi.CURSOR_INHERIT;
5655 G.postRedisplay "kill accidental zoom rect";
5657 | _ ->
5658 Wsi.setcursor Wsi.CURSOR_INHERIT;
5659 state.mstate <- Mnone
5662 | 1 when x > conf.winw - state.scrollw ->
5663 if down
5664 then
5665 let _, position, sh = state.uioh#scrollph in
5666 if y > truncate position && y < truncate (position +. sh)
5667 then state.mstate <- Mscrolly
5668 else scrolly y
5669 else
5670 state.mstate <- Mnone
5672 | 1 when y > conf.winh - state.hscrollh ->
5673 if down
5674 then
5675 let _, position, sw = state.uioh#scrollpw in
5676 if x > truncate position && x < truncate (position +. sw)
5677 then state.mstate <- Mscrollx
5678 else scrollx x
5679 else
5680 state.mstate <- Mnone
5682 | 1 ->
5683 let dest = if down then getunder x y else Unone in
5684 begin match dest with
5685 | Ulinkgoto _
5686 | Ulinkuri _
5687 | Uremote _
5688 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5689 gotounder dest
5691 | Unone when down ->
5692 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5693 state.mstate <- Mpan (x, y);
5695 | Unone | Utext _ ->
5696 if down
5697 then (
5698 if conf.angle mod 360 = 0
5699 then (
5700 state.mstate <- Msel ((x, y), (x, y));
5701 G.postRedisplay "mouse select";
5704 else (
5705 match state.mstate with
5706 | Mnone -> ()
5708 | Mzoom _ | Mscrollx | Mscrolly ->
5709 state.mstate <- Mnone
5711 | Mzoomrect ((x0, y0), _) ->
5712 zoomrect x0 y0 x y
5714 | Mpan _ ->
5715 Wsi.setcursor Wsi.CURSOR_INHERIT;
5716 state.mstate <- Mnone
5718 | Msel ((_, y0), (_, y1)) ->
5719 let rec loop = function
5720 | [] -> ()
5721 | l :: rest ->
5722 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5723 || ((y1 >= l.pagedispy
5724 && y1 <= (l.pagedispy + l.pagevh)))
5725 then
5726 match getopaque l.pageno with
5727 | Some opaque ->
5728 begin
5729 match Ne.pipe () with
5730 | Ne.Exn exn ->
5731 showtext '!'
5732 (Printf.sprintf
5733 "can not create sel pipe: %s"
5734 (Printexc.to_string exn));
5735 | Ne.Res (r, w) ->
5736 let doclose what fd =
5737 Ne.clo fd (fun msg ->
5738 dolog "%s close failed: %s" what msg)
5741 popen conf.selcmd [r, 0; w, -1];
5742 copysel w opaque;
5743 doclose "pipe/r" r;
5744 G.postRedisplay "copysel";
5745 with exn ->
5746 dolog "can not execute %S: %s"
5747 conf.selcmd (Printexc.to_string exn);
5748 doclose "pipe/r" r;
5749 doclose "pipe/w" w;
5751 | None -> ()
5752 else loop rest
5754 loop state.layout;
5755 Wsi.setcursor Wsi.CURSOR_INHERIT;
5756 state.mstate <- Mnone;
5760 | _ -> ()
5763 let birdseyemouse button down x y mask
5764 (conf, leftx, _, hooverpageno, anchor) =
5765 match button with
5766 | 1 when down ->
5767 let rec loop = function
5768 | [] -> ()
5769 | l :: rest ->
5770 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5771 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5772 then (
5773 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5775 else loop rest
5777 loop state.layout
5778 | 3 -> ()
5779 | _ -> viewmouse button down x y mask
5782 let mouse button down x y mask =
5783 state.uioh <- state.uioh#button button down x y mask;
5786 let motion ~x ~y =
5787 state.uioh <- state.uioh#motion x y
5790 let pmotion ~x ~y =
5791 state.uioh <- state.uioh#pmotion x y;
5794 let uioh = object
5795 method display = ()
5797 method key key mask =
5798 begin match state.mode with
5799 | Textentry textentry -> textentrykeyboard key mask textentry
5800 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5801 | View -> viewkeyboard key mask
5802 | LinkNav linknav -> linknavkeyboard key mask linknav
5803 end;
5804 state.uioh
5806 method button button bstate x y mask =
5807 begin match state.mode with
5808 | LinkNav _
5809 | View -> viewmouse button bstate x y mask
5810 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5811 | Textentry _ -> ()
5812 end;
5813 state.uioh
5815 method motion x y =
5816 begin match state.mode with
5817 | Textentry _ -> ()
5818 | View | Birdseye _ | LinkNav _ ->
5819 match state.mstate with
5820 | Mzoom _ | Mnone -> ()
5822 | Mpan (x0, y0) ->
5823 let dx = x - x0
5824 and dy = y0 - y in
5825 state.mstate <- Mpan (x, y);
5826 if canpan ()
5827 then state.x <- state.x + dx;
5828 let y = clamp dy in
5829 gotoy_and_clear_text y
5831 | Msel (a, _) ->
5832 state.mstate <- Msel (a, (x, y));
5833 G.postRedisplay "motion select";
5835 | Mscrolly ->
5836 let y = min conf.winh (max 0 y) in
5837 scrolly y
5839 | Mscrollx ->
5840 let x = min conf.winw (max 0 x) in
5841 scrollx x
5843 | Mzoomrect (p0, _) ->
5844 state.mstate <- Mzoomrect (p0, (x, y));
5845 G.postRedisplay "motion zoomrect";
5846 end;
5847 state.uioh
5849 method pmotion x y =
5850 begin match state.mode with
5851 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5852 let rec loop = function
5853 | [] ->
5854 if hooverpageno != -1
5855 then (
5856 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5857 G.postRedisplay "pmotion birdseye no hoover";
5859 | l :: rest ->
5860 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5861 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5862 then (
5863 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5864 G.postRedisplay "pmotion birdseye hoover";
5866 else loop rest
5868 loop state.layout
5870 | Textentry _ -> ()
5872 | LinkNav _
5873 | View ->
5874 match state.mstate with
5875 | Mnone -> updateunder x y
5876 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5878 end;
5879 state.uioh
5881 method infochanged _ = ()
5883 method scrollph =
5884 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5885 let p, h = scrollph state.y maxy in
5886 state.scrollw, p, h
5888 method scrollpw =
5889 let winw = conf.winw - state.scrollw - 1 in
5890 let fwinw = float winw in
5891 let sw =
5892 let sw = fwinw /. float state.w in
5893 let sw = fwinw *. sw in
5894 max sw (float conf.scrollh)
5896 let position, sw =
5897 let f = state.w+winw in
5898 let r = float (winw-state.x) /. float f in
5899 let p = fwinw *. r in
5900 p-.sw/.2., sw
5902 let sw =
5903 if position +. sw > fwinw
5904 then fwinw -. position
5905 else sw
5907 state.hscrollh, position, sw
5909 method modehash =
5910 let modename =
5911 match state.mode with
5912 | LinkNav _ -> "links"
5913 | Textentry _ -> "textentry"
5914 | Birdseye _ -> "birdseye"
5915 | View -> "view"
5917 findkeyhash conf modename
5918 end;;
5920 module Config =
5921 struct
5922 open Parser
5924 let fontpath = ref "";;
5926 module KeyMap =
5927 Map.Make (struct type t = (int * int) let compare = compare end);;
5929 let unent s =
5930 let l = String.length s in
5931 let b = Buffer.create l in
5932 unent b s 0 l;
5933 Buffer.contents b;
5936 let home =
5937 try Sys.getenv "HOME"
5938 with exn ->
5939 prerr_endline
5940 ("Can not determine home directory location: " ^
5941 Printexc.to_string exn);
5945 let modifier_of_string = function
5946 | "alt" -> Wsi.altmask
5947 | "shift" -> Wsi.shiftmask
5948 | "ctrl" | "control" -> Wsi.ctrlmask
5949 | "meta" -> Wsi.metamask
5950 | _ -> 0
5953 let key_of_string =
5954 let r = Str.regexp "-" in
5955 fun s ->
5956 let elems = Str.full_split r s in
5957 let f n k m =
5958 let g s =
5959 let m1 = modifier_of_string s in
5960 if m1 = 0
5961 then (Wsi.namekey s, m)
5962 else (k, m lor m1)
5963 in function
5964 | Str.Delim s when n land 1 = 0 -> g s
5965 | Str.Text s -> g s
5966 | Str.Delim _ -> (k, m)
5968 let rec loop n k m = function
5969 | [] -> (k, m)
5970 | x :: xs ->
5971 let k, m = f n k m x in
5972 loop (n+1) k m xs
5974 loop 0 0 0 elems
5977 let keys_of_string =
5978 let r = Str.regexp "[ \t]" in
5979 fun s ->
5980 let elems = Str.split r s in
5981 List.map key_of_string elems
5984 let copykeyhashes c =
5985 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5988 let config_of c attrs =
5989 let apply c k v =
5991 match k with
5992 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5993 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5994 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5995 | "preload" -> { c with preload = bool_of_string v }
5996 | "page-bias" -> { c with pagebias = int_of_string v }
5997 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5998 | "horizontal-scroll-step" ->
5999 { c with hscrollstep = max (int_of_string v) 1 }
6000 | "auto-scroll-step" ->
6001 { c with autoscrollstep = max 0 (int_of_string v) }
6002 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6003 | "crop-hack" -> { c with crophack = bool_of_string v }
6004 | "throttle" ->
6005 let mw =
6006 match String.lowercase v with
6007 | "true" -> Some infinity
6008 | "false" -> None
6009 | f -> Some (float_of_string f)
6011 { c with maxwait = mw}
6012 | "highlight-links" -> { c with hlinks = bool_of_string v }
6013 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6014 | "vertical-margin" ->
6015 { c with interpagespace = max 0 (int_of_string v) }
6016 | "zoom" ->
6017 let zoom = float_of_string v /. 100. in
6018 let zoom = max zoom 0.0 in
6019 { c with zoom = zoom }
6020 | "presentation" -> { c with presentation = bool_of_string v }
6021 | "rotation-angle" -> { c with angle = int_of_string v }
6022 | "width" -> { c with winw = max 20 (int_of_string v) }
6023 | "height" -> { c with winh = max 20 (int_of_string v) }
6024 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6025 | "proportional-display" -> { c with proportional = bool_of_string v }
6026 | "pixmap-cache-size" ->
6027 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6028 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6029 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6030 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6031 | "persistent-location" -> { c with jumpback = bool_of_string v }
6032 | "background-color" -> { c with bgcolor = color_of_string v }
6033 | "scrollbar-in-presentation" ->
6034 { c with scrollbarinpm = bool_of_string v }
6035 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6036 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6037 | "mupdf-store-size" ->
6038 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6039 | "checkers" -> { c with checkers = bool_of_string v }
6040 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6041 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6042 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6043 | "uri-launcher" -> { c with urilauncher = unent v }
6044 | "path-launcher" -> { c with pathlauncher = unent v }
6045 | "color-space" -> { c with colorspace = colorspace_of_string v }
6046 | "invert-colors" -> { c with invert = bool_of_string v }
6047 | "brightness" -> { c with colorscale = float_of_string v }
6048 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6049 | "ghyllscroll" ->
6050 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6051 | "columns" ->
6052 let (n, _, _) as nab = multicolumns_of_string v in
6053 if n < 0
6054 then { c with columns = Csplit (-n, [||]) }
6055 else { c with columns = Cmulti (nab, [||]) }
6056 | "birds-eye-columns" ->
6057 { c with beyecolumns = Some (max (int_of_string v) 2) }
6058 | "selection-command" -> { c with selcmd = unent v }
6059 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6060 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6061 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6062 | _ -> c
6063 with exn ->
6064 prerr_endline ("Error processing attribute (`" ^
6065 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6068 let rec fold c = function
6069 | [] -> c
6070 | (k, v) :: rest ->
6071 let c = apply c k v in
6072 fold c rest
6074 fold { c with keyhashes = copykeyhashes c } attrs;
6077 let fromstring f pos n v d =
6078 try f v
6079 with exn ->
6080 dolog "Error processing attribute (%S=%S) at %d\n%s"
6081 n v pos (Printexc.to_string exn)
6086 let bookmark_of attrs =
6087 let rec fold title page rely = function
6088 | ("title", v) :: rest -> fold v page rely rest
6089 | ("page", v) :: rest -> fold title v rely rest
6090 | ("rely", v) :: rest -> fold title page v rest
6091 | _ :: rest -> fold title page rely rest
6092 | [] -> title, page, rely
6094 fold "invalid" "0" "0" attrs
6097 let doc_of attrs =
6098 let rec fold path page rely pan = function
6099 | ("path", v) :: rest -> fold v page rely pan rest
6100 | ("page", v) :: rest -> fold path v rely pan rest
6101 | ("rely", v) :: rest -> fold path page v pan rest
6102 | ("pan", v) :: rest -> fold path page rely v rest
6103 | _ :: rest -> fold path page rely pan rest
6104 | [] -> path, page, rely, pan
6106 fold "" "0" "0" "0" attrs
6109 let map_of attrs =
6110 let rec fold rs ls = function
6111 | ("out", v) :: rest -> fold v ls rest
6112 | ("in", v) :: rest -> fold rs v rest
6113 | _ :: rest -> fold ls rs rest
6114 | [] -> ls, rs
6116 fold "" "" attrs
6119 let setconf dst src =
6120 dst.scrollbw <- src.scrollbw;
6121 dst.scrollh <- src.scrollh;
6122 dst.icase <- src.icase;
6123 dst.preload <- src.preload;
6124 dst.pagebias <- src.pagebias;
6125 dst.verbose <- src.verbose;
6126 dst.scrollstep <- src.scrollstep;
6127 dst.maxhfit <- src.maxhfit;
6128 dst.crophack <- src.crophack;
6129 dst.autoscrollstep <- src.autoscrollstep;
6130 dst.maxwait <- src.maxwait;
6131 dst.hlinks <- src.hlinks;
6132 dst.underinfo <- src.underinfo;
6133 dst.interpagespace <- src.interpagespace;
6134 dst.zoom <- src.zoom;
6135 dst.presentation <- src.presentation;
6136 dst.angle <- src.angle;
6137 dst.winw <- src.winw;
6138 dst.winh <- src.winh;
6139 dst.savebmarks <- src.savebmarks;
6140 dst.memlimit <- src.memlimit;
6141 dst.proportional <- src.proportional;
6142 dst.texcount <- src.texcount;
6143 dst.sliceheight <- src.sliceheight;
6144 dst.thumbw <- src.thumbw;
6145 dst.jumpback <- src.jumpback;
6146 dst.bgcolor <- src.bgcolor;
6147 dst.scrollbarinpm <- src.scrollbarinpm;
6148 dst.tilew <- src.tilew;
6149 dst.tileh <- src.tileh;
6150 dst.mustoresize <- src.mustoresize;
6151 dst.checkers <- src.checkers;
6152 dst.aalevel <- src.aalevel;
6153 dst.trimmargins <- src.trimmargins;
6154 dst.trimfuzz <- src.trimfuzz;
6155 dst.urilauncher <- src.urilauncher;
6156 dst.colorspace <- src.colorspace;
6157 dst.invert <- src.invert;
6158 dst.colorscale <- src.colorscale;
6159 dst.redirectstderr <- src.redirectstderr;
6160 dst.ghyllscroll <- src.ghyllscroll;
6161 dst.columns <- src.columns;
6162 dst.beyecolumns <- src.beyecolumns;
6163 dst.selcmd <- src.selcmd;
6164 dst.updatecurs <- src.updatecurs;
6165 dst.pathlauncher <- src.pathlauncher;
6166 dst.keyhashes <- copykeyhashes src;
6167 dst.hfsize <- src.hfsize;
6168 dst.hscrollstep <- src.hscrollstep;
6169 dst.pgscale <- src.pgscale;
6172 let get s =
6173 let h = Hashtbl.create 10 in
6174 let dc = { defconf with angle = defconf.angle } in
6175 let rec toplevel v t spos _ =
6176 match t with
6177 | Vdata | Vcdata | Vend -> v
6178 | Vopen ("llppconfig", _, closed) ->
6179 if closed
6180 then v
6181 else { v with f = llppconfig }
6182 | Vopen _ ->
6183 error "unexpected subelement at top level" s spos
6184 | Vclose _ -> error "unexpected close at top level" s spos
6186 and llppconfig v t spos _ =
6187 match t with
6188 | Vdata | Vcdata -> v
6189 | Vend -> error "unexpected end of input in llppconfig" s spos
6190 | Vopen ("defaults", attrs, closed) ->
6191 let c = config_of dc attrs in
6192 setconf dc c;
6193 if closed
6194 then v
6195 else { v with f = defaults }
6197 | Vopen ("ui-font", attrs, closed) ->
6198 let rec getsize size = function
6199 | [] -> size
6200 | ("size", v) :: rest ->
6201 let size =
6202 fromstring int_of_string spos "size" v fstate.fontsize in
6203 getsize size rest
6204 | l -> getsize size l
6206 fstate.fontsize <- getsize fstate.fontsize attrs;
6207 if closed
6208 then v
6209 else { v with f = uifont (Buffer.create 10) }
6211 | Vopen ("doc", attrs, closed) ->
6212 let pathent, spage, srely, span = doc_of attrs in
6213 let path = unent pathent
6214 and pageno = fromstring int_of_string spos "page" spage 0
6215 and rely = fromstring float_of_string spos "rely" srely 0.0
6216 and pan = fromstring int_of_string spos "pan" span 0 in
6217 let c = config_of dc attrs in
6218 let anchor = (pageno, rely) in
6219 if closed
6220 then (Hashtbl.add h path (c, [], pan, anchor); v)
6221 else { v with f = doc path pan anchor c [] }
6223 | Vopen _ ->
6224 error "unexpected subelement in llppconfig" s spos
6226 | Vclose "llppconfig" -> { v with f = toplevel }
6227 | Vclose _ -> error "unexpected close in llppconfig" s spos
6229 and defaults v t spos _ =
6230 match t with
6231 | Vdata | Vcdata -> v
6232 | Vend -> error "unexpected end of input in defaults" s spos
6233 | Vopen ("keymap", attrs, closed) ->
6234 let modename =
6235 try List.assoc "mode" attrs
6236 with Not_found -> "global" in
6237 if closed
6238 then v
6239 else
6240 let ret keymap =
6241 let h = findkeyhash dc modename in
6242 KeyMap.iter (Hashtbl.replace h) keymap;
6243 defaults
6245 { v with f = pkeymap ret KeyMap.empty }
6247 | Vopen (_, _, _) ->
6248 error "unexpected subelement in defaults" s spos
6250 | Vclose "defaults" ->
6251 { v with f = llppconfig }
6253 | Vclose _ -> error "unexpected close in defaults" s spos
6255 and uifont b v t spos epos =
6256 match t with
6257 | Vdata | Vcdata ->
6258 Buffer.add_substring b s spos (epos - spos);
6260 | Vopen (_, _, _) ->
6261 error "unexpected subelement in ui-font" s spos
6262 | Vclose "ui-font" ->
6263 if String.length !fontpath = 0
6264 then fontpath := Buffer.contents b;
6265 { v with f = llppconfig }
6266 | Vclose _ -> error "unexpected close in ui-font" s spos
6267 | Vend -> error "unexpected end of input in ui-font" s spos
6269 and doc path pan anchor c bookmarks v t spos _ =
6270 match t with
6271 | Vdata | Vcdata -> v
6272 | Vend -> error "unexpected end of input in doc" s spos
6273 | Vopen ("bookmarks", _, closed) ->
6274 if closed
6275 then v
6276 else { v with f = pbookmarks path pan anchor c bookmarks }
6278 | Vopen ("keymap", attrs, closed) ->
6279 let modename =
6280 try List.assoc "mode" attrs
6281 with Not_found -> "global"
6283 if closed
6284 then v
6285 else
6286 let ret keymap =
6287 let h = findkeyhash c modename in
6288 KeyMap.iter (Hashtbl.replace h) keymap;
6289 doc path pan anchor c bookmarks
6291 { v with f = pkeymap ret KeyMap.empty }
6293 | Vopen (_, _, _) ->
6294 error "unexpected subelement in doc" s spos
6296 | Vclose "doc" ->
6297 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6298 { v with f = llppconfig }
6300 | Vclose _ -> error "unexpected close in doc" s spos
6302 and pkeymap ret keymap v t spos _ =
6303 match t with
6304 | Vdata | Vcdata -> v
6305 | Vend -> error "unexpected end of input in keymap" s spos
6306 | Vopen ("map", attrs, closed) ->
6307 let r, l = map_of attrs in
6308 let kss = fromstring keys_of_string spos "in" r [] in
6309 let lss = fromstring keys_of_string spos "out" l [] in
6310 let keymap =
6311 match kss with
6312 | [] -> keymap
6313 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6314 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6316 if closed
6317 then { v with f = pkeymap ret keymap }
6318 else
6319 let f () = v in
6320 { v with f = skip "map" f }
6322 | Vopen _ ->
6323 error "unexpected subelement in keymap" s spos
6325 | Vclose "keymap" ->
6326 { v with f = ret keymap }
6328 | Vclose _ -> error "unexpected close in keymap" s spos
6330 and pbookmarks path pan anchor c bookmarks v t spos _ =
6331 match t with
6332 | Vdata | Vcdata -> v
6333 | Vend -> error "unexpected end of input in bookmarks" s spos
6334 | Vopen ("item", attrs, closed) ->
6335 let titleent, spage, srely = bookmark_of attrs in
6336 let page = fromstring int_of_string spos "page" spage 0
6337 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6338 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6339 if closed
6340 then { v with f = pbookmarks path pan anchor c bookmarks }
6341 else
6342 let f () = v in
6343 { v with f = skip "item" f }
6345 | Vopen _ ->
6346 error "unexpected subelement in bookmarks" s spos
6348 | Vclose "bookmarks" ->
6349 { v with f = doc path pan anchor c bookmarks }
6351 | Vclose _ -> error "unexpected close in bookmarks" s spos
6353 and skip tag f v t spos _ =
6354 match t with
6355 | Vdata | Vcdata -> v
6356 | Vend ->
6357 error ("unexpected end of input in skipped " ^ tag) s spos
6358 | Vopen (tag', _, closed) ->
6359 if closed
6360 then v
6361 else
6362 let f' () = { v with f = skip tag f } in
6363 { v with f = skip tag' f' }
6364 | Vclose ctag ->
6365 if tag = ctag
6366 then f ()
6367 else error ("unexpected close in skipped " ^ tag) s spos
6370 parse { f = toplevel; accu = () } s;
6371 h, dc;
6374 let do_load f ic =
6376 let len = in_channel_length ic in
6377 let s = String.create len in
6378 really_input ic s 0 len;
6379 f s;
6380 with
6381 | Parse_error (msg, s, pos) ->
6382 let subs = subs s pos in
6383 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6384 failwith ("parse error: " ^ s)
6386 | exn ->
6387 failwith ("config load error: " ^ Printexc.to_string exn)
6390 let defconfpath =
6391 let dir =
6393 let dir = Filename.concat home ".config" in
6394 if Sys.is_directory dir then dir else home
6395 with _ -> home
6397 Filename.concat dir "llpp.conf"
6400 let confpath = ref defconfpath;;
6402 let load1 f =
6403 if Sys.file_exists !confpath
6404 then
6405 match
6406 (try Some (open_in_bin !confpath)
6407 with exn ->
6408 prerr_endline
6409 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6410 Printexc.to_string exn);
6411 None
6413 with
6414 | Some ic ->
6415 begin try
6416 f (do_load get ic)
6417 with exn ->
6418 prerr_endline
6419 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6420 Printexc.to_string exn);
6421 end;
6422 close_in ic;
6424 | None -> ()
6425 else
6426 f (Hashtbl.create 0, defconf)
6429 let load () =
6430 let f (h, dc) =
6431 let pc, pb, px, pa =
6433 Hashtbl.find h (Filename.basename state.path)
6434 with Not_found -> dc, [], 0, (0, 0.0)
6436 setconf defconf dc;
6437 setconf conf pc;
6438 state.bookmarks <- pb;
6439 state.x <- px;
6440 state.scrollw <- conf.scrollbw;
6441 if conf.jumpback
6442 then state.anchor <- pa;
6443 cbput state.hists.nav pa;
6445 load1 f
6448 let add_attrs bb always dc c =
6449 let ob s a b =
6450 if always || a != b
6451 then Printf.bprintf bb "\n %s='%b'" s a
6452 and oi s a b =
6453 if always || a != b
6454 then Printf.bprintf bb "\n %s='%d'" s a
6455 and oI s a b =
6456 if always || a != b
6457 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6458 and oz s a b =
6459 if always || a <> b
6460 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6461 and oF s a b =
6462 if always || a <> b
6463 then Printf.bprintf bb "\n %s='%f'" s a
6464 and oc s a b =
6465 if always || a <> b
6466 then
6467 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6468 and oC s a b =
6469 if always || a <> b
6470 then
6471 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6472 and oR s a b =
6473 if always || a <> b
6474 then
6475 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6476 and os s a b =
6477 if always || a <> b
6478 then
6479 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6480 and og s a b =
6481 if always || a <> b
6482 then
6483 match a with
6484 | None -> ()
6485 | Some (_N, _A, _B) ->
6486 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6487 and oW s a b =
6488 if always || a <> b
6489 then
6490 let v =
6491 match a with
6492 | None -> "false"
6493 | Some f ->
6494 if f = infinity
6495 then "true"
6496 else string_of_float f
6498 Printf.bprintf bb "\n %s='%s'" s v
6499 and oco s a b =
6500 if always || a <> b
6501 then
6502 match a with
6503 | Cmulti ((n, a, b), _) when n > 1 ->
6504 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6505 | Csplit (n, _) when n > 1 ->
6506 Printf.bprintf bb "\n %s='%d'" s ~-n
6507 | _ -> ()
6508 and obeco s a b =
6509 if always || a <> b
6510 then
6511 match a with
6512 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6513 | _ -> ()
6515 let w, h =
6516 if always
6517 then dc.winw, dc.winh
6518 else
6519 match state.fullscreen with
6520 | Some wh -> wh
6521 | None -> c.winw, c.winh
6523 let zoom, presentation, interpagespace, maxwait =
6524 if always
6525 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6526 else
6527 match state.mode with
6528 | Birdseye (bc, _, _, _, _) ->
6529 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6530 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6532 oi "width" w dc.winw;
6533 oi "height" h dc.winh;
6534 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6535 oi "scroll-handle-height" c.scrollh dc.scrollh;
6536 ob "case-insensitive-search" c.icase dc.icase;
6537 ob "preload" c.preload dc.preload;
6538 oi "page-bias" c.pagebias dc.pagebias;
6539 oi "scroll-step" c.scrollstep dc.scrollstep;
6540 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6541 ob "max-height-fit" c.maxhfit dc.maxhfit;
6542 ob "crop-hack" c.crophack dc.crophack;
6543 oW "throttle" maxwait dc.maxwait;
6544 ob "highlight-links" c.hlinks dc.hlinks;
6545 ob "under-cursor-info" c.underinfo dc.underinfo;
6546 oi "vertical-margin" interpagespace dc.interpagespace;
6547 oz "zoom" zoom dc.zoom;
6548 ob "presentation" presentation dc.presentation;
6549 oi "rotation-angle" c.angle dc.angle;
6550 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6551 ob "proportional-display" c.proportional dc.proportional;
6552 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6553 oi "tex-count" c.texcount dc.texcount;
6554 oi "slice-height" c.sliceheight dc.sliceheight;
6555 oi "thumbnail-width" c.thumbw dc.thumbw;
6556 ob "persistent-location" c.jumpback dc.jumpback;
6557 oc "background-color" c.bgcolor dc.bgcolor;
6558 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6559 oi "tile-width" c.tilew dc.tilew;
6560 oi "tile-height" c.tileh dc.tileh;
6561 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6562 ob "checkers" c.checkers dc.checkers;
6563 oi "aalevel" c.aalevel dc.aalevel;
6564 ob "trim-margins" c.trimmargins dc.trimmargins;
6565 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6566 os "uri-launcher" c.urilauncher dc.urilauncher;
6567 os "path-launcher" c.pathlauncher dc.pathlauncher;
6568 oC "color-space" c.colorspace dc.colorspace;
6569 ob "invert-colors" c.invert dc.invert;
6570 oF "brightness" c.colorscale dc.colorscale;
6571 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6572 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6573 oco "columns" c.columns dc.columns;
6574 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6575 os "selection-command" c.selcmd dc.selcmd;
6576 ob "update-cursor" c.updatecurs dc.updatecurs;
6577 oi "hint-font-size" c.hfsize dc.hfsize;
6578 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6579 oF "page-scroll-scale" c.pgscale dc.pgscale;
6582 let keymapsbuf always dc c =
6583 let bb = Buffer.create 16 in
6584 let rec loop = function
6585 | [] -> ()
6586 | (modename, h) :: rest ->
6587 let dh = findkeyhash dc modename in
6588 if always || h <> dh
6589 then (
6590 if Hashtbl.length h > 0
6591 then (
6592 if Buffer.length bb > 0
6593 then Buffer.add_char bb '\n';
6594 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6595 Hashtbl.iter (fun i o ->
6596 let isdifferent = always ||
6598 let dO = Hashtbl.find dh i in
6599 dO <> o
6600 with Not_found -> true
6602 if isdifferent
6603 then
6604 let addkm (k, m) =
6605 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6606 if Wsi.withalt m then Buffer.add_string bb "alt-";
6607 if Wsi.withshift m then Buffer.add_string bb "shift-";
6608 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6609 Buffer.add_string bb (Wsi.keyname k);
6611 let addkms l =
6612 let rec loop = function
6613 | [] -> ()
6614 | km :: [] -> addkm km
6615 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6617 loop l
6619 Buffer.add_string bb "<map in='";
6620 addkm i;
6621 match o with
6622 | KMinsrt km ->
6623 Buffer.add_string bb "' out='";
6624 addkm km;
6625 Buffer.add_string bb "'/>\n"
6627 | KMinsrl kms ->
6628 Buffer.add_string bb "' out='";
6629 addkms kms;
6630 Buffer.add_string bb "'/>\n"
6632 | KMmulti (ins, kms) ->
6633 Buffer.add_char bb ' ';
6634 addkms ins;
6635 Buffer.add_string bb "' out='";
6636 addkms kms;
6637 Buffer.add_string bb "'/>\n"
6638 ) h;
6639 Buffer.add_string bb "</keymap>";
6642 loop rest
6644 loop c.keyhashes;
6648 let save () =
6649 let uifontsize = fstate.fontsize in
6650 let bb = Buffer.create 32768 in
6651 let f (h, dc) =
6652 let dc = if conf.bedefault then conf else dc in
6653 Buffer.add_string bb "<llppconfig>\n";
6655 if String.length !fontpath > 0
6656 then
6657 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6658 uifontsize
6659 !fontpath
6660 else (
6661 if uifontsize <> 14
6662 then
6663 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6666 Buffer.add_string bb "<defaults ";
6667 add_attrs bb true dc dc;
6668 let kb = keymapsbuf true dc dc in
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</defaults>\n";
6675 else Buffer.add_string bb "/>\n";
6677 let adddoc path pan anchor c bookmarks =
6678 if bookmarks == [] && c = dc && anchor = emptyanchor
6679 then ()
6680 else (
6681 Printf.bprintf bb "<doc path='%s'"
6682 (enent path 0 (String.length path));
6684 if anchor <> emptyanchor
6685 then (
6686 let n, y = anchor in
6687 Printf.bprintf bb " page='%d'" n;
6688 if y > 1e-6
6689 then
6690 Printf.bprintf bb " rely='%f'" y
6694 if pan != 0
6695 then Printf.bprintf bb " pan='%d'" pan;
6697 add_attrs bb false dc c;
6698 let kb = keymapsbuf false dc c in
6700 begin match bookmarks with
6701 | [] ->
6702 if Buffer.length kb > 0
6703 then (
6704 Buffer.add_string bb ">\n";
6705 Buffer.add_buffer bb kb;
6706 Buffer.add_string bb "\n</doc>\n";
6708 else Buffer.add_string bb "/>\n"
6709 | _ ->
6710 Buffer.add_string bb ">\n<bookmarks>\n";
6711 List.iter (fun (title, _level, (page, rely)) ->
6712 Printf.bprintf bb
6713 "<item title='%s' page='%d'"
6714 (enent title 0 (String.length title))
6715 page
6717 if rely > 1e-6
6718 then
6719 Printf.bprintf bb " rely='%f'" rely
6721 Buffer.add_string bb "/>\n";
6722 ) bookmarks;
6723 Buffer.add_string bb "</bookmarks>";
6724 if Buffer.length kb > 0
6725 then (
6726 Buffer.add_string bb "\n";
6727 Buffer.add_buffer bb kb;
6729 Buffer.add_string bb "\n</doc>\n";
6730 end;
6734 let pan, conf =
6735 match state.mode with
6736 | Birdseye (c, pan, _, _, _) ->
6737 let beyecolumns =
6738 match conf.columns with
6739 | Cmulti ((c, _, _), _) -> Some c
6740 | Csingle -> None
6741 | Csplit _ -> None
6742 and columns =
6743 match c.columns with
6744 | Cmulti (c, _) -> Cmulti (c, [||])
6745 | Csingle -> Csingle
6746 | Csplit _ -> failwith "quit from bird's eye while split"
6748 pan, { c with beyecolumns = beyecolumns; columns = columns }
6749 | _ -> state.x, conf
6751 let basename = Filename.basename state.path in
6752 adddoc basename pan (getanchor ())
6753 { conf with
6754 autoscrollstep =
6755 match state.autoscroll with
6756 | Some step -> step
6757 | None -> conf.autoscrollstep }
6758 (if conf.savebmarks then state.bookmarks else []);
6760 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6761 if basename <> path
6762 then adddoc path x y c bookmarks
6763 ) h;
6764 Buffer.add_string bb "</llppconfig>";
6766 load1 f;
6767 if Buffer.length bb > 0
6768 then
6770 let tmp = !confpath ^ ".tmp" in
6771 let oc = open_out_bin tmp in
6772 Buffer.output_buffer oc bb;
6773 close_out oc;
6774 Unix.rename tmp !confpath;
6775 with exn ->
6776 prerr_endline
6777 ("error while saving configuration: " ^ Printexc.to_string exn)
6779 end;;
6781 let () =
6782 let trimcachepath = ref "" in
6783 Arg.parse
6784 (Arg.align
6785 [("-p", Arg.String (fun s -> state.password <- s) ,
6786 "<password> Set password");
6788 ("-f", Arg.String (fun s -> Config.fontpath := s),
6789 "<path> Set path to the user interface font");
6791 ("-c", Arg.String (fun s -> Config.confpath := s),
6792 "<path> Set path to the configuration file");
6794 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6795 "<path> Set path to the trim cache file");
6797 ("-v", Arg.Unit (fun () ->
6798 Printf.printf
6799 "%s\nconfiguration path: %s\n"
6800 (version ())
6801 Config.defconfpath
6803 exit 0), " Print version and exit");
6806 (fun s -> state.path <- s)
6807 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6809 if String.length state.path = 0
6810 then (prerr_endline "file name missing"; exit 1);
6812 Config.load ();
6814 let globalkeyhash = findkeyhash conf "global" in
6815 let wsfd, winw, winh = Wsi.init (object
6816 method expose =
6817 if nogeomcmds state.geomcmds || platform == Posx
6818 then display ()
6819 else (
6820 GlClear.color (scalecolor2 conf.bgcolor);
6821 GlClear.clear [`color];
6823 method display = display ()
6824 method reshape w h = reshape w h
6825 method mouse b d x y m = mouse b d x y m
6826 method motion x y = state.mpos <- (x, y); motion x y
6827 method pmotion x y = state.mpos <- (x, y); pmotion x y
6828 method key k m =
6829 let mascm = m land (
6830 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6831 ) in
6832 match state.keystate with
6833 | KSnone ->
6834 let km = k, mascm in
6835 begin
6836 match
6837 let modehash = state.uioh#modehash in
6838 try Hashtbl.find modehash km
6839 with Not_found ->
6840 try Hashtbl.find globalkeyhash km
6841 with Not_found -> KMinsrt (k, m)
6842 with
6843 | KMinsrt (k, m) -> keyboard k m
6844 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6845 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6847 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6848 List.iter (fun (k, m) -> keyboard k m) insrt;
6849 state.keystate <- KSnone
6850 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6851 state.keystate <- KSinto (keys, insrt)
6852 | _ ->
6853 state.keystate <- KSnone
6855 method enter x y = state.mpos <- (x, y); pmotion x y
6856 method leave = state.mpos <- (-1, -1)
6857 method quit = raise Quit
6858 end) conf.winw conf.winh (platform = Posx) in
6860 state.wsfd <- wsfd;
6862 if not (
6863 List.exists GlMisc.check_extension
6864 [ "GL_ARB_texture_rectangle"
6865 ; "GL_EXT_texture_recangle"
6866 ; "GL_NV_texture_rectangle" ]
6868 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6870 let cr, sw =
6871 match Ne.pipe () with
6872 | Ne.Exn exn ->
6873 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6874 exit 1
6875 | Ne.Res rw -> rw
6876 and sr, cw =
6877 match Ne.pipe () with
6878 | Ne.Exn exn ->
6879 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6880 exit 1
6881 | Ne.Res rw -> rw
6884 cloexec cr;
6885 cloexec sw;
6886 cloexec sr;
6887 cloexec cw;
6889 setcheckers conf.checkers;
6890 redirectstderr ();
6892 init (cr, cw) (
6893 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6894 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6895 !Config.fontpath, !trimcachepath
6897 state.sr <- sr;
6898 state.sw <- sw;
6899 state.text <- "Opening " ^ state.path;
6900 reshape winw winh;
6901 opendoc state.path state.password;
6902 state.uioh <- uioh;
6904 let rec loop deadline =
6905 let r =
6906 match state.errfd with
6907 | None -> [state.sr; state.wsfd]
6908 | Some fd -> [state.sr; state.wsfd; fd]
6910 if state.redisplay
6911 then (
6912 state.redisplay <- false;
6913 display ();
6915 let timeout =
6916 let now = now () in
6917 if deadline > now
6918 then (
6919 if deadline = infinity
6920 then ~-.1.0
6921 else max 0.0 (deadline -. now)
6923 else 0.0
6925 let r, _, _ =
6926 try Unix.select r [] [] timeout
6927 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6929 begin match r with
6930 | [] ->
6931 state.ghyll None;
6932 let newdeadline =
6933 if state.ghyll == noghyll
6934 then
6935 match state.autoscroll with
6936 | Some step when step != 0 ->
6937 let y = state.y + step in
6938 let y =
6939 if y < 0
6940 then state.maxy
6941 else if y >= state.maxy then 0 else y
6943 gotoy y;
6944 if state.mode = View
6945 then state.text <- "";
6946 deadline +. 0.01
6947 | _ -> infinity
6948 else deadline +. 0.01
6950 loop newdeadline
6952 | l ->
6953 let rec checkfds = function
6954 | [] -> ()
6955 | fd :: rest when fd = state.sr ->
6956 let cmd = readcmd state.sr in
6957 act cmd;
6958 checkfds rest
6960 | fd :: rest when fd = state.wsfd ->
6961 Wsi.readresp fd;
6962 checkfds rest
6964 | fd :: rest ->
6965 let s = String.create 80 in
6966 let n = Unix.read fd s 0 80 in
6967 if conf.redirectstderr
6968 then (
6969 Buffer.add_substring state.errmsgs s 0 n;
6970 state.newerrmsgs <- true;
6971 state.redisplay <- true;
6973 else (
6974 prerr_string (String.sub s 0 n);
6975 flush stderr;
6977 checkfds rest
6979 checkfds l;
6980 let newdeadline =
6981 let deadline1 =
6982 if deadline = infinity
6983 then now () +. 0.01
6984 else deadline
6986 match state.autoscroll with
6987 | Some step when step != 0 -> deadline1
6988 | _ -> if state.ghyll == noghyll then infinity else deadline1
6990 loop newdeadline
6991 end;
6994 loop infinity;
6995 with Quit ->
6996 Config.save ();