Layouting/preloading/anchoring fixes
[llpp.git] / main.ml
blob54be7e2e68cc4eb513c080bcc309ac008daae72d
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
45 type link =
46 | Lnotfound
47 | Lfound of int
48 and linkdir =
49 | LDfirst
50 | LDlast
51 | LDfirstvisible of (int * int * int)
52 | LDleft of int
53 | LDright of int
54 | LDdown of int
55 | LDup of int
58 type pagewithlinks =
59 | Pwlnotfound
60 | Pwl of int
63 type keymap =
64 | KMinsrt of key
65 | KMinsrl of key list
66 | KMmulti of key list * key list
67 and key = int * int
68 and keyhash = (key, keymap) Hashtbl.t
69 and keystate =
70 | KSnone
71 | KSinto of (key list * key list)
74 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
75 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
77 type pipe = (Unix.file_descr * Unix.file_descr);;
79 external init : pipe -> params -> unit = "ml_init";;
80 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
81 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
82 external getpdimrect : int -> float array = "ml_getpdimrect";;
83 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
84 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
85 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
86 external measurestr : int -> string -> float = "ml_measure_string";;
87 external getmaxw : unit -> float = "ml_getmaxw";;
88 external postprocess :
89 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
90 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
91 external platform : unit -> platform = "ml_platform";;
92 external setaalevel : int -> unit = "ml_setaalevel";;
93 external realloctexts : int -> bool = "ml_realloctexts";;
94 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
95 external findlink : opaque -> linkdir -> link = "ml_findlink";;
96 external getlink : opaque -> int -> under = "ml_getlink";;
97 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
98 external getlinkcount : opaque -> int = "ml_getlinkcount";;
99 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
100 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
102 let platform_to_string = function
103 | Punknown -> "unknown"
104 | Plinux -> "Linux"
105 | Posx -> "OSX"
106 | Psun -> "Sun"
107 | Pfreebsd -> "FreeBSD"
108 | Pdragonflybsd -> "DragonflyBSD"
109 | Popenbsd -> "OpenBSD"
110 | Pnetbsd -> "NetBSD"
111 | Pcygwin -> "Cygwin"
114 let platform = platform ();;
116 let popen cmd fda =
117 if platform = Pcygwin
118 then (
119 let sh = "/bin/sh" in
120 let args = [|sh; "-c"; cmd|] in
121 let rec std si so se = function
122 | [] -> si, so, se
123 | (fd, 0) :: rest -> std fd so se rest
124 | (fd, -1) :: rest ->
125 Unix.set_close_on_exec fd;
126 std si so se rest
127 | (_, n) :: _ ->
128 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
130 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
131 ignore (Unix.create_process sh args si so se)
133 else popen cmd fda;
136 type x = int
137 and y = int
138 and tilex = int
139 and tiley = int
140 and tileparams = (x * y * width * height * tilex * tiley)
143 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
145 type mpos = int * int
146 and mstate =
147 | Msel of (mpos * mpos)
148 | Mpan of mpos
149 | Mscrolly | Mscrollx
150 | Mzoom of (int * int)
151 | Mzoomrect of (mpos * mpos)
152 | Mnone
155 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
156 and onkey = string -> int -> te
157 and ondone = string -> unit
158 and histcancel = unit -> unit
159 and onhist = ((histcmd -> string) * histcancel)
160 and histcmd = HCnext | HCprev | HCfirst | HClast
161 and cancelonempty = bool
162 and te =
163 | TEstop
164 | TEdone of string
165 | TEcont of string
166 | TEswitch of textentry
169 type 'a circbuf =
170 { store : 'a array
171 ; mutable rc : int
172 ; mutable wc : int
173 ; mutable len : int
177 let bound v minv maxv =
178 max minv (min maxv v);
181 let cbnew n v =
182 { store = Array.create n v
183 ; rc = 0
184 ; wc = 0
185 ; len = 0
189 let drawstring size x y s =
190 Gl.enable `blend;
191 Gl.enable `texture_2d;
192 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
193 ignore (drawstr size x y s);
194 Gl.disable `blend;
195 Gl.disable `texture_2d;
198 let drawstring1 size x y s =
199 drawstr size x y s;
202 let drawstring2 size x y fmt =
203 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
206 let cbcap b = Array.length b.store;;
208 let cbput b v =
209 let cap = cbcap b in
210 b.store.(b.wc) <- v;
211 b.wc <- (b.wc + 1) mod cap;
212 b.rc <- b.wc;
213 b.len <- min (b.len + 1) cap;
216 let cbempty b = b.len = 0;;
218 let cbgetg b circular dir =
219 if cbempty b
220 then b.store.(0)
221 else
222 let rc = b.rc + dir in
223 let rc =
224 if circular
225 then (
226 if rc = -1
227 then b.len-1
228 else (
229 if rc = b.len
230 then 0
231 else rc
234 else max 0 (min rc (b.len-1))
236 b.rc <- rc;
237 b.store.(rc);
240 let cbget b = cbgetg b false;;
241 let cbgetc b = cbgetg b true;;
243 type page =
244 { pageno : int
245 ; pagedimno : int
246 ; pagew : int
247 ; pageh : int
248 ; pagex : int
249 ; pagey : int
250 ; pagevw : int
251 ; pagevh : int
252 ; pagedispx : int
253 ; pagedispy : int
254 ; pagecol : int
258 let debugl l =
259 dolog "l %d dim=%d {" l.pageno l.pagedimno;
260 dolog " WxH %dx%d" l.pagew l.pageh;
261 dolog " vWxH %dx%d" l.pagevw l.pagevh;
262 dolog " pagex,y %d,%d" l.pagex l.pagey;
263 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
264 dolog " column %d" l.pagecol;
265 dolog "}";
268 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
269 dolog "rect {";
270 dolog " x0,y0=(% f, % f)" x0 y0;
271 dolog " x1,y1=(% f, % f)" x1 y1;
272 dolog " x2,y2=(% f, % f)" x2 y2;
273 dolog " x3,y3=(% f, % f)" x3 y3;
274 dolog "}";
277 type multicolumns = multicol * pagegeom
278 and singlecolumn = pagegeom
279 and splitcolumns = columncount * pagegeom
280 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
281 and multicol = columncount * covercount * covercount
282 and pdimno = int
283 and columncount = int
284 and covercount = int;;
286 type conf =
287 { mutable scrollbw : int
288 ; mutable scrollh : int
289 ; mutable icase : bool
290 ; mutable preload : bool
291 ; mutable pagebias : int
292 ; mutable verbose : bool
293 ; mutable debug : bool
294 ; mutable scrollstep : int
295 ; mutable hscrollstep : int
296 ; mutable maxhfit : bool
297 ; mutable crophack : bool
298 ; mutable autoscrollstep : int
299 ; mutable maxwait : float option
300 ; mutable hlinks : bool
301 ; mutable underinfo : bool
302 ; mutable interpagespace : interpagespace
303 ; mutable zoom : float
304 ; mutable presentation : bool
305 ; mutable angle : angle
306 ; mutable winw : int
307 ; mutable winh : int
308 ; mutable savebmarks : bool
309 ; mutable proportional : proportional
310 ; mutable trimmargins : trimmargins
311 ; mutable trimfuzz : irect
312 ; mutable memlimit : memsize
313 ; mutable texcount : texcount
314 ; mutable sliceheight : sliceheight
315 ; mutable thumbw : width
316 ; mutable jumpback : bool
317 ; mutable bgcolor : float * float * float
318 ; mutable bedefault : bool
319 ; mutable scrollbarinpm : bool
320 ; mutable tilew : int
321 ; mutable tileh : int
322 ; mutable mustoresize : memsize
323 ; mutable checkers : bool
324 ; mutable aalevel : int
325 ; mutable urilauncher : string
326 ; mutable pathlauncher : string
327 ; mutable colorspace : colorspace
328 ; mutable invert : bool
329 ; mutable colorscale : float
330 ; mutable redirectstderr : bool
331 ; mutable ghyllscroll : (int * int * int) option
332 ; mutable columns : columns
333 ; mutable beyecolumns : columncount option
334 ; mutable selcmd : string
335 ; mutable updatecurs : bool
336 ; mutable keyhashes : (string * keyhash) list
337 ; mutable hfsize : int
338 ; mutable pgscale : float
340 and columns =
341 | Csingle of singlecolumn
342 | Cmulti of multicolumns
343 | Csplit of splitcolumns
346 type anchor = pageno * top * dtop;;
348 type outline = string * int * anchor;;
350 type rect = float * float * float * float * float * float * float * float;;
352 type tile = opaque * pixmapsize * elapsed
353 and elapsed = float;;
354 type pagemapkey = pageno * gen;;
355 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
356 and row = int
357 and col = int;;
359 let emptyanchor = (0, 0.0, 0.0);;
361 type infochange = | Memused | Docinfo | Pdim;;
363 class type uioh = object
364 method display : unit
365 method key : int -> int -> uioh
366 method button : int -> bool -> int -> int -> int -> uioh
367 method motion : int -> int -> uioh
368 method pmotion : int -> int -> uioh
369 method infochanged : infochange -> unit
370 method scrollpw : (int * float * float)
371 method scrollph : (int * float * float)
372 method modehash : keyhash
373 end;;
375 type mode =
376 | Birdseye of (conf * leftx * pageno * pageno * anchor)
377 | Textentry of (textentry * onleave)
378 | View
379 | LinkNav of linktarget
380 and onleave = leavetextentrystatus -> unit
381 and leavetextentrystatus = | Cancel | Confirm
382 and helpitem = string * int * action
383 and action =
384 | Noaction
385 | Action of (uioh -> uioh)
386 and linktarget =
387 | Ltexact of (pageno * int)
388 | Ltgendir of int
391 let isbirdseye = function Birdseye _ -> true | _ -> false;;
392 let istextentry = function Textentry _ -> true | _ -> false;;
394 type currently =
395 | Idle
396 | Loading of (page * gen)
397 | Tiling of (
398 page * opaque * colorspace * angle * gen * col * row * width * height
400 | Outlining of outline list
403 let emptykeyhash = Hashtbl.create 0;;
404 let nouioh : uioh = object (self)
405 method display = ()
406 method key _ _ = self
407 method button _ _ _ _ _ = self
408 method motion _ _ = self
409 method pmotion _ _ = self
410 method infochanged _ = ()
411 method scrollpw = (0, nan, nan)
412 method scrollph = (0, nan, nan)
413 method modehash = emptykeyhash
414 end;;
416 type state =
417 { mutable sr : Unix.file_descr
418 ; mutable sw : Unix.file_descr
419 ; mutable wsfd : Unix.file_descr
420 ; mutable errfd : Unix.file_descr option
421 ; mutable stderr : Unix.file_descr
422 ; mutable errmsgs : Buffer.t
423 ; mutable newerrmsgs : bool
424 ; mutable w : int
425 ; mutable x : int
426 ; mutable y : int
427 ; mutable scrollw : int
428 ; mutable hscrollh : int
429 ; mutable anchor : anchor
430 ; mutable ranchors : (string * string * anchor) list
431 ; mutable maxy : int
432 ; mutable layout : page list
433 ; pagemap : (pagemapkey, opaque) Hashtbl.t
434 ; tilemap : (tilemapkey, tile) Hashtbl.t
435 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
436 ; mutable pdims : (pageno * width * height * leftx) list
437 ; mutable pagecount : int
438 ; mutable currently : currently
439 ; mutable mstate : mstate
440 ; mutable searchpattern : string
441 ; mutable rects : (pageno * recttype * rect) list
442 ; mutable rects1 : (pageno * recttype * rect) list
443 ; mutable text : string
444 ; mutable fullscreen : (width * height) option
445 ; mutable mode : mode
446 ; mutable uioh : uioh
447 ; mutable outlines : outline array
448 ; mutable bookmarks : outline list
449 ; mutable path : string
450 ; mutable password : string
451 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
452 ; mutable memused : memsize
453 ; mutable gen : gen
454 ; mutable throttle : (page list * int * float) option
455 ; mutable autoscroll : int option
456 ; mutable ghyll : (int option -> unit)
457 ; mutable help : helpitem array
458 ; mutable docinfo : (int * string) list
459 ; mutable texid : GlTex.texture_id option
460 ; hists : hists
461 ; mutable prevzoom : float
462 ; mutable progress : float
463 ; mutable redisplay : bool
464 ; mutable mpos : mpos
465 ; mutable keystate : keystate
466 ; mutable glinks : bool
467 ; mutable prevcolumns : (columns * float) option
469 and hists =
470 { pat : string circbuf
471 ; pag : string circbuf
472 ; nav : anchor circbuf
473 ; sel : string circbuf
477 let defconf =
478 { scrollbw = 7
479 ; scrollh = 12
480 ; icase = true
481 ; preload = true
482 ; pagebias = 0
483 ; verbose = false
484 ; debug = false
485 ; scrollstep = 24
486 ; hscrollstep = 24
487 ; maxhfit = true
488 ; crophack = false
489 ; autoscrollstep = 2
490 ; maxwait = None
491 ; hlinks = false
492 ; underinfo = false
493 ; interpagespace = 2
494 ; zoom = 1.0
495 ; presentation = false
496 ; angle = 0
497 ; winw = 900
498 ; winh = 900
499 ; savebmarks = true
500 ; proportional = true
501 ; trimmargins = false
502 ; trimfuzz = (0,0,0,0)
503 ; memlimit = 32 lsl 20
504 ; texcount = 256
505 ; sliceheight = 24
506 ; thumbw = 76
507 ; jumpback = true
508 ; bgcolor = (0.5, 0.5, 0.5)
509 ; bedefault = false
510 ; scrollbarinpm = true
511 ; tilew = 2048
512 ; tileh = 2048
513 ; mustoresize = 256 lsl 20
514 ; checkers = true
515 ; aalevel = 8
516 ; urilauncher =
517 (match platform with
518 | Plinux | Pfreebsd | Pdragonflybsd
519 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
520 | Posx -> "open \"%s\""
521 | Pcygwin -> "cygstart \"%s\""
522 | Punknown -> "echo %s")
523 ; pathlauncher = "lp \"%s\""
524 ; selcmd =
525 (match platform with
526 | Plinux | Pfreebsd | Pdragonflybsd
527 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
528 | Posx -> "pbcopy"
529 | Pcygwin -> "wsel"
530 | Punknown -> "cat")
531 ; colorspace = Rgb
532 ; invert = false
533 ; colorscale = 1.0
534 ; redirectstderr = false
535 ; ghyllscroll = None
536 ; columns = Csingle [||]
537 ; beyecolumns = None
538 ; updatecurs = false
539 ; hfsize = 12
540 ; pgscale = 1.0
541 ; keyhashes =
542 let mk n = (n, Hashtbl.create 1) in
543 [ mk "global"
544 ; mk "info"
545 ; mk "help"
546 ; mk "outline"
547 ; mk "listview"
548 ; mk "birdseye"
549 ; mk "textentry"
550 ; mk "links"
551 ; mk "view"
556 let findkeyhash c name =
557 try List.assoc name c.keyhashes
558 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
561 let conf = { defconf with angle = defconf.angle };;
563 let pgscale h = truncate (float h *. conf.pgscale);;
565 type fontstate =
566 { mutable fontsize : int
567 ; mutable wwidth : float
568 ; mutable maxrows : int
572 let fstate =
573 { fontsize = 14
574 ; wwidth = nan
575 ; maxrows = -1
579 let setfontsize n =
580 fstate.fontsize <- n;
581 fstate.wwidth <- measurestr fstate.fontsize "w";
582 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
585 let geturl s =
586 let colonpos = try String.index s ':' with Not_found -> -1 in
587 let len = String.length s in
588 if colonpos >= 0 && colonpos + 3 < len
589 then (
590 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
591 then
592 let schemestartpos =
593 try String.rindex_from s colonpos ' '
594 with Not_found -> -1
596 let scheme =
597 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
599 match scheme with
600 | "http" | "ftp" | "mailto" ->
601 let epos =
602 try String.index_from s colonpos ' '
603 with Not_found -> len
605 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
606 | _ -> ""
607 else ""
609 else ""
612 let gotouri uri =
613 if String.length conf.urilauncher = 0
614 then print_endline uri
615 else (
616 let url = geturl uri in
617 if String.length url = 0
618 then print_endline uri
619 else
620 let re = Str.regexp "%s" in
621 let command = Str.global_replace re url conf.urilauncher in
622 try popen command []
623 with exn ->
624 Printf.eprintf
625 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
626 flush stderr;
630 let version () =
631 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
632 (platform_to_string platform) Sys.word_size Sys.ocaml_version
635 let makehelp () =
636 let strings = version () :: "" :: Help.keys in
637 Array.of_list (
638 List.map (fun s ->
639 let url = geturl s in
640 if String.length url > 0
641 then (s, 0, Action (fun u -> gotouri url; u))
642 else (s, 0, Noaction)
643 ) strings);
646 let noghyll _ = ();;
647 let firstgeomcmds = "", [];;
649 let state =
650 { sr = Unix.stdin
651 ; sw = Unix.stdin
652 ; wsfd = Unix.stdin
653 ; errfd = None
654 ; stderr = Unix.stderr
655 ; errmsgs = Buffer.create 0
656 ; newerrmsgs = false
657 ; x = 0
658 ; y = 0
659 ; w = 0
660 ; scrollw = 0
661 ; hscrollh = 0
662 ; anchor = emptyanchor
663 ; ranchors = []
664 ; layout = []
665 ; maxy = max_int
666 ; tilelru = Queue.create ()
667 ; pagemap = Hashtbl.create 10
668 ; tilemap = Hashtbl.create 10
669 ; pdims = []
670 ; pagecount = 0
671 ; currently = Idle
672 ; mstate = Mnone
673 ; rects = []
674 ; rects1 = []
675 ; text = ""
676 ; mode = View
677 ; fullscreen = None
678 ; searchpattern = ""
679 ; outlines = [||]
680 ; bookmarks = []
681 ; path = ""
682 ; password = ""
683 ; geomcmds = firstgeomcmds
684 ; hists =
685 { nav = cbnew 10 emptyanchor
686 ; pat = cbnew 10 ""
687 ; pag = cbnew 10 ""
688 ; sel = cbnew 10 ""
690 ; memused = 0
691 ; gen = 0
692 ; throttle = None
693 ; autoscroll = None
694 ; ghyll = noghyll
695 ; help = makehelp ()
696 ; docinfo = []
697 ; texid = None
698 ; prevzoom = 1.0
699 ; progress = -1.0
700 ; uioh = nouioh
701 ; redisplay = true
702 ; mpos = (-1, -1)
703 ; keystate = KSnone
704 ; glinks = false
705 ; prevcolumns = None
709 let vlog fmt =
710 if conf.verbose
711 then
712 Printf.kprintf prerr_endline fmt
713 else
714 Printf.kprintf ignore fmt
717 let launchpath () =
718 if String.length conf.pathlauncher = 0
719 then print_endline state.path
720 else (
721 let re = Str.regexp "%s" in
722 let command = Str.global_replace re state.path conf.pathlauncher in
723 try popen command []
724 with exn ->
725 Printf.eprintf
726 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
727 flush stderr;
731 module Ne = struct
732 type 'a t = | Res of 'a | Exn of exn;;
734 let pipe () =
735 try Res (Unix.pipe ())
736 with exn -> Exn exn
739 let clo fd f =
740 try Unix.close fd
741 with exn -> f (Printexc.to_string exn)
744 let dup fd =
745 try Res (Unix.dup fd)
746 with exn -> Exn exn
749 let dup2 fd1 fd2 =
750 try Res (Unix.dup2 fd1 fd2)
751 with exn -> Exn exn
753 end;;
755 let redirectstderr () =
756 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
757 if conf.redirectstderr
758 then
759 match Ne.pipe () with
760 | Ne.Exn exn ->
761 dolog "failed to create stderr redirection pipes: %s"
762 (Printexc.to_string exn)
764 | Ne.Res (r, w) ->
765 begin match Ne.dup Unix.stderr with
766 | Ne.Exn exn ->
767 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
768 Ne.clo r (clofail "pipe/r");
769 Ne.clo w (clofail "pipe/w");
771 | Ne.Res dupstderr ->
772 begin match Ne.dup2 w Unix.stderr with
773 | Ne.Exn exn ->
774 dolog "failed to dup2 to stderr: %s"
775 (Printexc.to_string exn);
776 Ne.clo dupstderr (clofail "stderr duplicate");
777 Ne.clo r (clofail "redir pipe/r");
778 Ne.clo w (clofail "redir pipe/w");
780 | Ne.Res () ->
781 state.stderr <- dupstderr;
782 state.errfd <- Some r;
783 end;
785 else (
786 state.newerrmsgs <- false;
787 begin match state.errfd with
788 | Some fd ->
789 begin match Ne.dup2 state.stderr Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup2 original stderr: %s"
792 (Printexc.to_string exn)
793 | Ne.Res () ->
794 Ne.clo fd (clofail "dup of stderr");
795 Unix.dup2 state.stderr Unix.stderr;
796 state.errfd <- None;
797 end;
798 | None -> ()
799 end;
800 prerr_string (Buffer.contents state.errmsgs);
801 flush stderr;
802 Buffer.clear state.errmsgs;
806 module G =
807 struct
808 let postRedisplay who =
809 if conf.verbose
810 then prerr_endline ("redisplay for " ^ who);
811 state.redisplay <- true;
813 end;;
815 let getopaque pageno =
816 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
817 with Not_found -> None
820 let putopaque pageno opaque =
821 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
824 let pagetranslatepoint l x y =
825 let dy = y - l.pagedispy in
826 let y = dy + l.pagey in
827 let dx = x - l.pagedispx in
828 let x = dx + l.pagex in
829 (x, y);
832 let getunder x y =
833 let rec f = function
834 | l :: rest ->
835 begin match getopaque l.pageno with
836 | Some opaque ->
837 let x0 = l.pagedispx in
838 let x1 = x0 + l.pagevw in
839 let y0 = l.pagedispy in
840 let y1 = y0 + l.pagevh in
841 if y >= y0 && y <= y1 && x >= x0 && x <= x1
842 then
843 let px, py = pagetranslatepoint l x y in
844 match whatsunder opaque px py with
845 | Unone -> f rest
846 | under -> under
847 else f rest
848 | _ ->
849 f rest
851 | [] -> Unone
853 f state.layout
856 let showtext c s =
857 state.text <- Printf.sprintf "%c%s" c s;
858 G.postRedisplay "showtext";
861 let undertext = function
862 | Unone -> "none"
863 | Ulinkuri s -> s
864 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
865 | Utext s -> "font: " ^ s
866 | Uunexpected s -> "unexpected: " ^ s
867 | Ulaunch s -> "launch: " ^ s
868 | Unamed s -> "named: " ^ s
869 | Uremote (filename, pageno) ->
870 Printf.sprintf "%s: page %d" filename (pageno+1)
873 let updateunder x y =
874 match getunder x y with
875 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
876 | Ulinkuri uri ->
877 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
878 Wsi.setcursor Wsi.CURSOR_INFO
879 | Ulinkgoto (pageno, _) ->
880 if conf.underinfo
881 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
882 Wsi.setcursor Wsi.CURSOR_INFO
883 | Utext s ->
884 if conf.underinfo then showtext 'f' ("ont: " ^ s);
885 Wsi.setcursor Wsi.CURSOR_TEXT
886 | Uunexpected s ->
887 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
888 Wsi.setcursor Wsi.CURSOR_INHERIT
889 | Ulaunch s ->
890 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
891 Wsi.setcursor Wsi.CURSOR_INHERIT
892 | Unamed s ->
893 if conf.underinfo then showtext 'n' ("amed: " ^ s);
894 Wsi.setcursor Wsi.CURSOR_INHERIT
895 | Uremote (filename, pageno) ->
896 if conf.underinfo then showtext 'r'
897 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
898 Wsi.setcursor Wsi.CURSOR_INFO
901 let showlinktype under =
902 if conf.underinfo
903 then
904 match under with
905 | Unone -> ()
906 | under ->
907 let s = undertext under in
908 showtext ' ' s
911 let addchar s c =
912 let b = Buffer.create (String.length s + 1) in
913 Buffer.add_string b s;
914 Buffer.add_char b c;
915 Buffer.contents b;
918 let colorspace_of_string s =
919 match String.lowercase s with
920 | "rgb" -> Rgb
921 | "bgr" -> Bgr
922 | "gray" -> Gray
923 | _ -> failwith "invalid colorspace"
926 let int_of_colorspace = function
927 | Rgb -> 0
928 | Bgr -> 1
929 | Gray -> 2
932 let colorspace_of_int = function
933 | 0 -> Rgb
934 | 1 -> Bgr
935 | 2 -> Gray
936 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
939 let colorspace_to_string = function
940 | Rgb -> "rgb"
941 | Bgr -> "bgr"
942 | Gray -> "gray"
945 let intentry_with_suffix text key =
946 let c =
947 if key >= 32 && key < 127
948 then Char.chr key
949 else '\000'
951 match Char.lowercase c with
952 | '0' .. '9' ->
953 let text = addchar text c in
954 TEcont text
956 | 'k' | 'm' | 'g' ->
957 let text = addchar text c in
958 TEcont text
960 | _ ->
961 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
962 TEcont text
965 let multicolumns_to_string (n, a, b) =
966 if a = 0 && b = 0
967 then Printf.sprintf "%d" n
968 else Printf.sprintf "%d,%d,%d" n a b;
971 let multicolumns_of_string s =
973 (int_of_string s, 0, 0)
974 with _ ->
975 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
978 let readcmd fd =
979 let s = "xxxx" in
980 let n = Unix.read fd s 0 4 in
981 if n != 4 then failwith "incomplete read(len)";
982 let len = 0
983 lor (Char.code s.[0] lsl 24)
984 lor (Char.code s.[1] lsl 16)
985 lor (Char.code s.[2] lsl 8)
986 lor (Char.code s.[3] lsl 0)
988 let s = String.create len in
989 let n = Unix.read fd s 0 len in
990 if n != len then failwith "incomplete read(data)";
994 let btod b = if b then 1 else 0;;
996 let wcmd fmt =
997 let b = Buffer.create 16 in
998 Buffer.add_string b "llll";
999 Printf.kbprintf
1000 (fun b ->
1001 let s = Buffer.contents b in
1002 let n = String.length s in
1003 let len = n - 4 in
1004 (* dolog "wcmd %S" (String.sub s 4 len); *)
1005 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1006 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1007 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1008 s.[3] <- Char.chr (len land 0xff);
1009 let n' = Unix.write state.sw s 0 n in
1010 if n' != n then failwith "write failed";
1011 ) b fmt;
1014 let calcips h =
1015 if conf.presentation
1016 then
1017 let d = conf.winh - h in
1018 max conf.interpagespace ((d + 1) / 2)
1019 else
1020 conf.interpagespace
1023 let calcheight () =
1024 match conf.columns with
1025 | Cmulti ((c, _, _), b) ->
1026 let rec loop y h n =
1027 if n < 0
1028 then loop y h (n+1)
1029 else (
1030 if n = Array.length b
1031 then y + h
1032 else
1033 let (_, _, y', (_, _, h', _)) = b.(n) in
1034 let y = min y y'
1035 and h = max h h' in
1036 loop y h (n+1)
1039 loop max_int 0 (((Array.length b - 1) / c) * c)
1040 | Csingle b ->
1041 if Array.length b > 0
1042 then
1043 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1044 y + h + (if conf.presentation then calcips h else 0)
1045 else 0
1046 | Csplit (_, b) ->
1047 if Array.length b > 0
1048 then
1049 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1050 y + h
1051 else 0
1054 let getpageyh pageno =
1055 let pageno = bound pageno 0 (state.pagecount-1) in
1056 match conf.columns with
1057 | Csingle b ->
1058 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1059 let y =
1060 if conf.presentation
1061 then y - calcips h
1062 else y
1064 y, h
1065 | Cmulti (_, b) ->
1066 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1067 y, h
1068 | Csplit (c, b) ->
1069 let n = pageno*c in
1070 let (_, _, y, (_, _, h, _)) = b.(n) in
1071 y, h
1074 let getpagedim pageno =
1075 let rec f ppdim l =
1076 match l with
1077 | (n, _, _, _) as pdim :: rest ->
1078 if n >= pageno
1079 then (if n = pageno then pdim else ppdim)
1080 else f pdim rest
1082 | [] -> ppdim
1084 f (-1, -1, -1, -1) state.pdims
1087 let getpagey pageno = fst (getpageyh pageno);;
1089 let nogeomcmds cmds =
1090 match cmds with
1091 | s, [] -> String.length s = 0
1092 | _ -> false
1095 let layoutN ((columns, coverA, coverB), b) y sh =
1096 let sh = sh - state.hscrollh in
1097 let rec fold accu n =
1098 if n = Array.length b
1099 then accu
1100 else
1101 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1102 if (vy - y) > sh &&
1103 (n = coverA - 1
1104 || n = state.pagecount - coverB
1105 || (n - coverA) mod columns = columns - 1)
1106 then accu
1107 else
1108 let accu =
1109 if vy + h > y
1110 then
1111 let pagey = max 0 (y - vy) in
1112 let pagedispy = if pagey > 0 then 0 else vy - y in
1113 let pagedispx, pagex =
1114 let pdx =
1115 if n = coverA - 1 || n = state.pagecount - coverB
1116 then state.x + (conf.winw - state.scrollw - w) / 2
1117 else dx + xoff + state.x
1119 if pdx < 0
1120 then 0, -pdx
1121 else pdx, 0
1123 let pagevw =
1124 let vw = conf.winw - state.scrollw - pagedispx in
1125 let pw = w - pagex in
1126 min vw pw
1128 let pagevh = min (h - pagey) (sh - pagedispy) in
1129 if pagevw > 0 && pagevh > 0
1130 then
1131 let e =
1132 { pageno = n
1133 ; pagedimno = pdimno
1134 ; pagew = w
1135 ; pageh = h
1136 ; pagex = pagex
1137 ; pagey = pagey
1138 ; pagevw = pagevw
1139 ; pagevh = pagevh
1140 ; pagedispx = pagedispx
1141 ; pagedispy = pagedispy
1142 ; pagecol = 0
1145 e :: accu
1146 else
1147 accu
1148 else
1149 accu
1151 fold accu (n+1)
1153 List.rev (fold [] 0)
1156 let layoutS (columns, b) y sh =
1157 let sh = sh - state.hscrollh in
1158 let rec fold accu n =
1159 if n = Array.length b
1160 then accu
1161 else
1162 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1163 if (vy - y) > sh
1164 then accu
1165 else
1166 let accu =
1167 if vy + pageh > y
1168 then
1169 let x = xoff + state.x in
1170 let pagey = max 0 (y - vy) in
1171 let pagedispy = if pagey > 0 then 0 else vy - y in
1172 let pagedispx, pagex =
1173 if px = 0
1174 then (
1175 if x < 0
1176 then 0, -x
1177 else x, 0
1179 else (
1180 let px = px - x in
1181 if px < 0
1182 then -px, 0
1183 else 0, px
1186 let pagecolw = pagew/columns in
1187 let pagedispx =
1188 if pagecolw < conf.winw
1189 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1190 else pagedispx
1192 let pagevw =
1193 let vw = conf.winw - pagedispx - state.scrollw in
1194 let pw = pagew - pagex in
1195 min vw pw
1197 let pagevw = min pagevw pagecolw in
1198 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1199 if pagevw > 0 && pagevh > 0
1200 then
1201 let e =
1202 { pageno = n/columns
1203 ; pagedimno = pdimno
1204 ; pagew = pagew
1205 ; pageh = pageh
1206 ; pagex = pagex
1207 ; pagey = pagey
1208 ; pagevw = pagevw
1209 ; pagevh = pagevh
1210 ; pagedispx = pagedispx
1211 ; pagedispy = pagedispy
1212 ; pagecol = n mod columns
1215 e :: accu
1216 else
1217 accu
1218 else
1219 accu
1221 fold accu (n+1)
1223 List.rev (fold [] 0)
1226 let layout y sh =
1227 if nogeomcmds state.geomcmds
1228 then
1229 match conf.columns with
1230 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1231 | Cmulti c -> layoutN c y sh
1232 | Csplit s -> layoutS s y sh
1233 else []
1236 let clamp incr =
1237 let y = state.y + incr in
1238 let y = max 0 y in
1239 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1243 let itertiles l f =
1244 let tilex = l.pagex mod conf.tilew in
1245 let tiley = l.pagey mod conf.tileh in
1247 let col = l.pagex / conf.tilew in
1248 let row = l.pagey / conf.tileh in
1250 let rec rowloop row y0 dispy h =
1251 if h = 0
1252 then ()
1253 else (
1254 let dh = conf.tileh - y0 in
1255 let dh = min h dh in
1256 let rec colloop col x0 dispx w =
1257 if w = 0
1258 then ()
1259 else (
1260 let dw = conf.tilew - x0 in
1261 let dw = min w dw in
1263 f col row dispx dispy x0 y0 dw dh;
1264 colloop (col+1) 0 (dispx+dw) (w-dw)
1267 colloop col tilex l.pagedispx l.pagevw;
1268 rowloop (row+1) 0 (dispy+dh) (h-dh)
1271 if l.pagevw > 0 && l.pagevh > 0
1272 then rowloop row tiley l.pagedispy l.pagevh;
1275 let gettileopaque l col row =
1276 let key =
1277 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1279 try Some (Hashtbl.find state.tilemap key)
1280 with Not_found -> None
1283 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1284 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1285 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1288 let drawtiles l color =
1289 GlDraw.color color;
1290 let f col row x y tilex tiley w h =
1291 match gettileopaque l col row with
1292 | Some (opaque, _, t) ->
1293 let params = x, y, w, h, tilex, tiley in
1294 if conf.invert
1295 then (
1296 Gl.enable `blend;
1297 GlFunc.blend_func `zero `one_minus_src_color;
1299 drawtile params opaque;
1300 if conf.invert
1301 then Gl.disable `blend;
1302 if conf.debug
1303 then (
1304 let s = Printf.sprintf
1305 "%d[%d,%d] %f sec"
1306 l.pageno col row t
1308 let w = measurestr fstate.fontsize s in
1309 GlMisc.push_attrib [`current];
1310 GlDraw.color (0.0, 0.0, 0.0);
1311 GlDraw.rect
1312 (float (x-2), float (y-2))
1313 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1314 GlDraw.color (1.0, 1.0, 1.0);
1315 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1316 GlMisc.pop_attrib ();
1319 | _ ->
1320 let w =
1321 let lw = conf.winw - state.scrollw - x in
1322 min lw w
1323 and h =
1324 let lh = conf.winh - y in
1325 min lh h
1327 begin match state.texid with
1328 | Some id ->
1329 Gl.enable `texture_2d;
1330 GlTex.bind_texture `texture_2d id;
1331 let x0 = float x
1332 and y0 = float y
1333 and x1 = float (x+w)
1334 and y1 = float (y+h) in
1336 let tw = float w /. 64.0
1337 and th = float h /. 64.0 in
1338 let tx0 = float tilex /. 64.0
1339 and ty0 = float tiley /. 64.0 in
1340 let tx1 = tx0 +. tw
1341 and ty1 = ty0 +. th in
1342 GlDraw.begins `quads;
1343 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1344 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1345 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1346 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1347 GlDraw.ends ();
1349 Gl.disable `texture_2d;
1350 | None ->
1351 GlDraw.color (1.0, 1.0, 1.0);
1352 GlDraw.rect
1353 (float x, float y)
1354 (float (x+w), float (y+h));
1355 end;
1356 if w > 128 && h > fstate.fontsize + 10
1357 then (
1358 GlDraw.color (0.0, 0.0, 0.0);
1359 let c, r =
1360 if conf.verbose
1361 then (col*conf.tilew, row*conf.tileh)
1362 else col, row
1364 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1366 GlDraw.color color;
1368 itertiles l f
1371 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1373 let tilevisible1 l x y =
1374 let ax0 = l.pagex
1375 and ax1 = l.pagex + l.pagevw
1376 and ay0 = l.pagey
1377 and ay1 = l.pagey + l.pagevh in
1379 let bx0 = x
1380 and by0 = y in
1381 let bx1 = min (bx0 + conf.tilew) l.pagew
1382 and by1 = min (by0 + conf.tileh) l.pageh in
1384 let rx0 = max ax0 bx0
1385 and ry0 = max ay0 by0
1386 and rx1 = min ax1 bx1
1387 and ry1 = min ay1 by1 in
1389 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1390 nonemptyintersection
1393 let tilevisible layout n x y =
1394 let rec findpageinlayout m = function
1395 | l :: rest when l.pageno = n ->
1396 tilevisible1 l x y || (
1397 match conf.columns with
1398 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1399 | _ -> false
1401 | _ :: rest -> findpageinlayout 0 rest
1402 | [] -> false
1404 findpageinlayout 0 layout;
1407 let tileready l x y =
1408 tilevisible1 l x y &&
1409 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1412 let tilepage n p layout =
1413 let rec loop = function
1414 | l :: rest ->
1415 if l.pageno = n
1416 then
1417 let f col row _ _ _ _ _ _ =
1418 if state.currently = Idle
1419 then
1420 match gettileopaque l col row with
1421 | Some _ -> ()
1422 | None ->
1423 let x = col*conf.tilew
1424 and y = row*conf.tileh in
1425 let w =
1426 let w = l.pagew - x in
1427 min w conf.tilew
1429 let h =
1430 let h = l.pageh - y in
1431 min h conf.tileh
1433 wcmd "tile %s %d %d %d %d" p x y w h;
1434 state.currently <-
1435 Tiling (
1436 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1437 conf.tilew, conf.tileh
1440 itertiles l f;
1441 else
1442 loop rest
1444 | [] -> ()
1446 if nogeomcmds state.geomcmds
1447 then loop layout;
1450 let page_of_y y =
1451 let b =
1452 match conf.columns with
1453 | Csingle b -> b
1454 | Cmulti (_, b) -> b
1455 | Csplit (_, b) -> b
1457 let rec bsearch nmin nmax =
1458 if nmin > nmax
1459 then -1
1460 else
1461 let n = (nmax + nmin) / 2 in
1462 let _, _, vy, (_, h, _, _) = b.(n) in
1463 let y0, y1 =
1464 if conf.presentation
1465 then
1466 let ips = calcips h in
1467 let y0 = vy - ips in
1468 let y1 = vy + h + ips in
1469 y0, y1
1470 else (
1471 if n = 0
1472 then 0, vy + h + conf.interpagespace
1473 else
1474 let y0 = vy - conf.interpagespace in
1475 y0, y0 + h
1478 if y >= y0 && y < y1
1479 then n
1480 else (
1481 if y > y0
1482 then bsearch (n+1) nmax
1483 else bsearch nmin (n-1)
1486 let r = bsearch 0 (state.pagecount-1) in
1490 let preloadlayout _ =
1491 let y = if state.y < conf.winh then 0 else state.y - conf.winh in
1492 let h = conf.winh*3 in
1493 layout y h;
1496 let load pages =
1497 let rec loop pages =
1498 if state.currently != Idle
1499 then ()
1500 else
1501 match pages with
1502 | l :: rest ->
1503 begin match getopaque l.pageno with
1504 | None ->
1505 wcmd "page %d %d" l.pageno l.pagedimno;
1506 state.currently <- Loading (l, state.gen);
1507 | Some opaque ->
1508 tilepage l.pageno opaque pages;
1509 loop rest
1510 end;
1511 | _ -> ()
1513 if nogeomcmds state.geomcmds
1514 then loop pages
1517 let preload pages =
1518 load pages;
1519 if conf.preload && state.currently = Idle
1520 then load (preloadlayout pages);
1523 let layoutready layout =
1524 let rec fold all ls =
1525 all && match ls with
1526 | l :: rest ->
1527 let seen = ref false in
1528 let allvisible = ref true in
1529 let foo col row _ _ _ _ _ _ =
1530 seen := true;
1531 allvisible := !allvisible &&
1532 begin match gettileopaque l col row with
1533 | Some _ -> true
1534 | None -> false
1537 itertiles l foo;
1538 fold (!seen && !allvisible) rest
1539 | [] -> true
1541 let alltilesvisible = fold true layout in
1542 alltilesvisible;
1545 let gotoy y =
1546 let y = bound y 0 state.maxy in
1547 let y, layout, proceed =
1548 match conf.maxwait with
1549 | Some time when state.ghyll == noghyll ->
1550 begin match state.throttle with
1551 | None ->
1552 let layout = layout y conf.winh in
1553 let ready = layoutready layout in
1554 if not ready
1555 then (
1556 load layout;
1557 state.throttle <- Some (layout, y, now ());
1559 else G.postRedisplay "gotoy showall (None)";
1560 y, layout, ready
1561 | Some (_, _, started) ->
1562 let dt = now () -. started in
1563 if dt > time
1564 then (
1565 state.throttle <- None;
1566 let layout = layout y conf.winh in
1567 load layout;
1568 G.postRedisplay "maxwait";
1569 y, layout, true
1571 else -1, [], false
1574 | _ ->
1575 let layout = layout y conf.winh in
1576 if true || layoutready layout
1577 then G.postRedisplay "gotoy ready";
1578 y, layout, true
1580 if proceed
1581 then (
1582 state.y <- y;
1583 state.layout <- layout;
1584 begin match state.mode with
1585 | LinkNav (Ltexact (pageno, linkno)) ->
1586 let rec loop = function
1587 | [] ->
1588 state.mode <- LinkNav (Ltgendir 0)
1589 | l :: _ when l.pageno = pageno ->
1590 begin match getopaque pageno with
1591 | None ->
1592 state.mode <- LinkNav (Ltgendir 0)
1593 | Some opaque ->
1594 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1595 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1596 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1597 then state.mode <- LinkNav (Ltgendir 0)
1599 | _ :: rest -> loop rest
1601 loop layout
1602 | _ -> ()
1603 end;
1604 begin match state.mode with
1605 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1606 if not (pagevisible layout pageno)
1607 then (
1608 match state.layout with
1609 | [] -> ()
1610 | l :: _ ->
1611 state.mode <- Birdseye (
1612 conf, leftx, l.pageno, hooverpageno, anchor
1615 | LinkNav (Ltgendir dir as lt) ->
1616 let linknav =
1617 let rec loop = function
1618 | [] -> lt
1619 | l :: rest ->
1620 match getopaque l.pageno with
1621 | None -> loop rest
1622 | Some opaque ->
1623 let link =
1624 let ld =
1625 if dir = 0
1626 then LDfirstvisible (l.pagex, l.pagey, dir)
1627 else (
1628 if dir > 0 then LDfirst else LDlast
1631 findlink opaque ld
1633 match link with
1634 | Lnotfound -> loop rest
1635 | Lfound n ->
1636 showlinktype (getlink opaque n);
1637 Ltexact (l.pageno, n)
1639 loop state.layout
1641 state.mode <- LinkNav linknav
1642 | _ -> ()
1643 end;
1644 preload layout;
1646 state.ghyll <- noghyll;
1647 if conf.updatecurs
1648 then (
1649 let mx, my = state.mpos in
1650 updateunder mx my;
1654 let conttiling pageno opaque =
1655 tilepage pageno opaque
1656 (if conf.preload then preloadlayout state.layout else state.layout)
1659 let gotoy_and_clear_text y =
1660 if not conf.verbose then state.text <- "";
1661 gotoy y;
1664 let getanchor1 l =
1665 let top =
1666 let coloff = l.pagecol * l.pageh in
1667 float (l.pagey + coloff) /. float l.pageh
1669 let dtop =
1670 if l.pagedispy = 0
1671 then
1673 else
1674 if conf.presentation
1675 then float l.pagedispy /. float (calcips l.pageh)
1676 else float l.pagedispy /. float conf.interpagespace
1678 (l.pageno, top, dtop)
1681 let getanchor () =
1682 match state.layout with
1683 | l :: _ -> getanchor1 l
1684 | [] ->
1685 let n = page_of_y state.y in
1686 let y, h = getpageyh n in
1687 let dy = y - state.y in
1688 let dtop =
1689 if conf.presentation
1690 then
1691 let ips = calcips h in
1692 float (dy + ips) /. float ips
1693 else
1694 float dy /. float conf.interpagespace
1696 (n, 0.0, dtop)
1699 let getanchory (n, top, dtop) =
1700 let y, h = getpageyh n in
1701 if conf.presentation
1702 then
1703 let ips = calcips h in
1704 y + truncate (top*.float h -. dtop*.float ips) + ips;
1705 else
1706 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1709 let gotoanchor anchor =
1710 gotoy (getanchory anchor);
1713 let addnav () =
1714 cbput state.hists.nav (getanchor ());
1717 let getnav dir =
1718 let anchor = cbgetc state.hists.nav dir in
1719 getanchory anchor;
1722 let gotoghyll y =
1723 let scroll f n a b =
1724 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1725 let snake f a b =
1726 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1727 if f < a
1728 then s (float f /. float a)
1729 else (
1730 if f > b
1731 then 1.0 -. s ((float (f-b) /. float (n-b)))
1732 else 1.0
1735 snake f a b
1736 and summa f n a b =
1737 (* courtesy:
1738 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1739 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1740 let iv1 = iv f in
1741 let ins = float a *. iv1
1742 and outs = float (n-b) *. iv1 in
1743 let ones = b - a in
1744 ins +. outs +. float ones
1746 let rec set (_N, _A, _B) y sy =
1747 let sum = summa 1.0 _N _A _B in
1748 let dy = float (y - sy) in
1749 state.ghyll <- (
1750 let rec gf n y1 o =
1751 if n >= _N
1752 then state.ghyll <- noghyll
1753 else
1754 let go n =
1755 let s = scroll n _N _A _B in
1756 let y1 = y1 +. ((s *. dy) /. sum) in
1757 gotoy_and_clear_text (truncate y1);
1758 state.ghyll <- gf (n+1) y1;
1760 match o with
1761 | None -> go n
1762 | Some y' -> set (_N/2, 0, 0) y' state.y
1764 gf 0 (float state.y)
1767 match conf.ghyllscroll with
1768 | None ->
1769 gotoy_and_clear_text y
1770 | Some nab ->
1771 if state.ghyll == noghyll
1772 then set nab y state.y
1773 else state.ghyll (Some y)
1776 let gotopage n top =
1777 let y, h = getpageyh n in
1778 let y = y + (truncate (top *. float h)) in
1779 gotoghyll y
1782 let gotopage1 n top =
1783 let y = getpagey n in
1784 let y = y + top in
1785 gotoghyll y
1788 let invalidate s f =
1789 state.layout <- [];
1790 state.pdims <- [];
1791 state.rects <- [];
1792 state.rects1 <- [];
1793 match state.geomcmds with
1794 | ps, [] when String.length ps = 0 ->
1795 f ();
1796 state.geomcmds <- s, [];
1798 | ps, [] ->
1799 state.geomcmds <- ps, [s, f];
1801 | ps, (s', _) :: rest when s' = s ->
1802 state.geomcmds <- ps, ((s, f) :: rest);
1804 | ps, cmds ->
1805 state.geomcmds <- ps, ((s, f) :: cmds);
1808 let opendoc path password =
1809 state.path <- path;
1810 state.password <- password;
1811 state.gen <- state.gen + 1;
1812 state.docinfo <- [];
1814 setaalevel conf.aalevel;
1815 Wsi.settitle ("llpp " ^ Filename.basename path);
1816 wcmd "open %s\000%s\000" path password;
1817 invalidate "reqlayout"
1818 (fun () ->
1819 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1822 let scalecolor c =
1823 let c = c *. conf.colorscale in
1824 (c, c, c);
1827 let scalecolor2 (r, g, b) =
1828 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1831 let docolumns = function
1832 | Csingle _ ->
1833 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1834 let rec loop pageno pdimno pdim y ph pdims =
1835 if pageno = state.pagecount
1836 then ()
1837 else
1838 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1839 match pdims with
1840 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1841 pdimno+1, pdim, rest
1842 | _ ->
1843 pdimno, pdim, pdims
1845 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1846 let y = y +
1847 (if conf.presentation
1848 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1849 else (if pageno = 0 then 0 else calcips h)
1852 a.(pageno) <- (pdimno, x, y, pdim);
1853 loop (pageno+1) pdimno pdim (y + h) h pdims
1855 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1856 conf.columns <- Csingle a;
1858 | Cmulti ((columns, coverA, coverB), _) ->
1859 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1860 let rec loop pageno pdimno pdim x y rowh pdims =
1861 let rec fixrow m = if m = pageno then () else
1862 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1863 if h < rowh
1864 then (
1865 let y = y + (rowh - h) / 2 in
1866 a.(m) <- (pdimno, x, y, pdim);
1868 fixrow (m+1)
1870 if pageno = state.pagecount
1871 then fixrow (((pageno - 1) / columns) * columns)
1872 else
1873 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1874 match pdims with
1875 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1876 pdimno+1, pdim, rest
1877 | _ ->
1878 pdimno, pdim, pdims
1880 let x, y, rowh' =
1881 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1882 then (
1883 (conf.winw - state.scrollw - w) / 2,
1884 y + rowh + conf.interpagespace, h
1886 else (
1887 if (pageno - coverA) mod columns = 0
1888 then (
1889 (conf.winw - state.scrollw - state.w) / 2,
1890 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1892 else x, y, max rowh h
1895 if pageno > 1 && (pageno - coverA) mod columns = 0
1896 then fixrow (pageno - columns);
1897 a.(pageno) <- (pdimno, x, y, pdim);
1898 let x = x + w + xoff*2 + conf.interpagespace in
1899 loop (pageno+1) pdimno pdim x y rowh' pdims
1901 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1902 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1904 | Csplit (c, _) ->
1905 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1906 let rec loop pageno pdimno pdim y pdims =
1907 if pageno = state.pagecount
1908 then ()
1909 else
1910 let pdimno, ((_, w, h, _) as pdim), pdims =
1911 match pdims with
1912 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1913 pdimno+1, pdim, rest
1914 | _ ->
1915 pdimno, pdim, pdims
1917 let cw = w / c in
1918 let rec loop1 n x y =
1919 if n = c then y else (
1920 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1921 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1924 let y = loop1 0 0 y in
1925 loop (pageno+1) pdimno pdim y pdims
1927 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1928 conf.columns <- Csplit (c, a);
1931 let represent () =
1932 docolumns conf.columns;
1933 state.maxy <- calcheight ();
1934 state.hscrollh <-
1935 if state.w <= conf.winw - state.scrollw
1936 then 0
1937 else state.scrollw
1939 match state.mode with
1940 | Birdseye (_, _, pageno, _, _) ->
1941 let y, h = getpageyh pageno in
1942 let top = (conf.winh - h) / 2 in
1943 gotoy (max 0 (y - top))
1944 | _ -> gotoanchor state.anchor
1947 let reshape w h =
1948 GlDraw.viewport 0 0 w h;
1949 let firsttime = state.geomcmds == firstgeomcmds in
1950 if not firsttime && nogeomcmds state.geomcmds
1951 then state.anchor <- getanchor ();
1953 conf.winw <- w;
1954 let w = truncate (float w *. conf.zoom) - state.scrollw in
1955 let w = max w 2 in
1956 conf.winh <- h;
1957 setfontsize fstate.fontsize;
1958 GlMat.mode `modelview;
1959 GlMat.load_identity ();
1961 GlMat.mode `projection;
1962 GlMat.load_identity ();
1963 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1964 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1965 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1967 let relx =
1968 if conf.zoom <= 1.0
1969 then 0.0
1970 else float state.x /. float state.w
1972 invalidate "geometry"
1973 (fun () ->
1974 state.w <- w;
1975 if not firsttime
1976 then state.x <- truncate (relx *. float w);
1977 let w =
1978 match conf.columns with
1979 | Csingle _ -> w
1980 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1981 | Csplit (c, _) -> w * c
1983 wcmd "geometry %d %d" w h);
1986 let enttext () =
1987 let len = String.length state.text in
1988 let drawstring s =
1989 let hscrollh =
1990 match state.mode with
1991 | Textentry _
1992 | View ->
1993 let h, _, _ = state.uioh#scrollpw in
1995 | _ -> 0
1997 let rect x w =
1998 GlDraw.rect
1999 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2000 (x+.w, float (conf.winh - hscrollh))
2003 let w = float (conf.winw - state.scrollw - 1) in
2004 if state.progress >= 0.0 && state.progress < 1.0
2005 then (
2006 GlDraw.color (0.3, 0.3, 0.3);
2007 let w1 = w *. state.progress in
2008 rect 0.0 w1;
2009 GlDraw.color (0.0, 0.0, 0.0);
2010 rect w1 (w-.w1)
2012 else (
2013 GlDraw.color (0.0, 0.0, 0.0);
2014 rect 0.0 w;
2017 GlDraw.color (1.0, 1.0, 1.0);
2018 drawstring fstate.fontsize
2019 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2021 let s =
2022 match state.mode with
2023 | Textentry ((prefix, text, _, _, _, _), _) ->
2024 let s =
2025 if len > 0
2026 then
2027 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2028 else
2029 Printf.sprintf "%s%s_" prefix text
2033 | _ -> state.text
2035 let s =
2036 if state.newerrmsgs
2037 then (
2038 if not (istextentry state.mode)
2039 then
2040 let s1 = "(press 'e' to review error messasges)" in
2041 if String.length s > 0 then s ^ " " ^ s1 else s1
2042 else s
2044 else s
2046 if String.length s > 0
2047 then drawstring s
2050 let gctiles () =
2051 let len = Queue.length state.tilelru in
2052 let layout = lazy (
2053 match state.throttle with
2054 | None ->
2055 if conf.preload
2056 then preloadlayout state.layout
2057 else state.layout
2058 | Some (layout, _, _) ->
2059 layout
2060 ) in
2061 let rec loop qpos =
2062 if state.memused <= conf.memlimit
2063 then ()
2064 else (
2065 if qpos < len
2066 then
2067 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2068 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2069 let (_, pw, ph, _) = getpagedim n in
2071 gen = state.gen
2072 && colorspace = conf.colorspace
2073 && angle = conf.angle
2074 && pagew = pw
2075 && pageh = ph
2076 && (
2077 let x = col*conf.tilew
2078 and y = row*conf.tileh in
2079 tilevisible (Lazy.force_val layout) n x y
2081 then Queue.push lruitem state.tilelru
2082 else (
2083 wcmd "freetile %s" p;
2084 state.memused <- state.memused - s;
2085 state.uioh#infochanged Memused;
2086 Hashtbl.remove state.tilemap k;
2088 loop (qpos+1)
2091 loop 0
2094 let flushtiles () =
2095 Queue.iter (fun (k, p, s) ->
2096 wcmd "freetile %s" p;
2097 state.memused <- state.memused - s;
2098 state.uioh#infochanged Memused;
2099 Hashtbl.remove state.tilemap k;
2100 ) state.tilelru;
2101 Queue.clear state.tilelru;
2102 load state.layout;
2105 let logcurrently = function
2106 | Idle -> dolog "Idle"
2107 | Loading (l, gen) ->
2108 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2109 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2110 dolog
2111 "Tiling %d[%d,%d] page=%s cs=%s angle"
2112 l.pageno col row pageopaque
2113 (colorspace_to_string colorspace)
2115 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2116 angle gen conf.angle state.gen
2117 tilew tileh
2118 conf.tilew conf.tileh
2120 | Outlining _ ->
2121 dolog "outlining"
2124 let act cmds =
2125 (* dolog "%S" cmds; *)
2126 let op, args =
2127 let spacepos =
2128 try String.index cmds ' '
2129 with Not_found -> -1
2131 if spacepos = -1
2132 then cmds, ""
2133 else
2134 let l = String.length cmds in
2135 let op = String.sub cmds 0 spacepos in
2136 op, begin
2137 if l - spacepos < 2 then ""
2138 else String.sub cmds (spacepos+1) (l-spacepos-1)
2141 match op with
2142 | "clear" ->
2143 state.uioh#infochanged Pdim;
2144 state.pdims <- [];
2146 | "clearrects" ->
2147 state.rects <- state.rects1;
2148 G.postRedisplay "clearrects";
2150 | "continue" ->
2151 let n =
2152 try Scanf.sscanf args "%u" (fun n -> n)
2153 with exn ->
2154 dolog "error processing 'continue' %S: %s"
2155 cmds (Printexc.to_string exn);
2156 exit 1;
2158 state.pagecount <- n;
2159 begin match state.currently with
2160 | Outlining l ->
2161 state.currently <- Idle;
2162 state.outlines <- Array.of_list (List.rev l)
2163 | _ -> ()
2164 end;
2166 let cur, cmds = state.geomcmds in
2167 if String.length cur = 0
2168 then failwith "umpossible";
2170 begin match List.rev cmds with
2171 | [] ->
2172 state.geomcmds <- "", [];
2173 represent ();
2174 | (s, f) :: rest ->
2175 f ();
2176 state.geomcmds <- s, List.rev rest;
2177 end;
2178 if conf.maxwait = None
2179 then G.postRedisplay "continue";
2181 | "title" ->
2182 Wsi.settitle args
2184 | "msg" ->
2185 showtext ' ' args
2187 | "vmsg" ->
2188 if conf.verbose
2189 then showtext ' ' args
2191 | "progress" ->
2192 let progress, text =
2194 Scanf.sscanf args "%f %n"
2195 (fun f pos ->
2196 f, String.sub args pos (String.length args - pos))
2197 with exn ->
2198 dolog "error processing 'progress' %S: %s"
2199 cmds (Printexc.to_string exn);
2200 exit 1;
2202 state.text <- text;
2203 state.progress <- progress;
2204 G.postRedisplay "progress"
2206 | "firstmatch" ->
2207 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2209 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2210 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2211 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2212 with exn ->
2213 dolog "error processing 'firstmatch' %S: %s"
2214 cmds (Printexc.to_string exn);
2215 exit 1;
2217 let y = (getpagey pageno) + truncate y0 in
2218 addnav ();
2219 gotoy y;
2220 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2222 | "match" ->
2223 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2225 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2226 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2227 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2228 with exn ->
2229 dolog "error processing 'match' %S: %s"
2230 cmds (Printexc.to_string exn);
2231 exit 1;
2233 state.rects1 <-
2234 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2236 | "page" ->
2237 let pageopaque, t =
2239 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2240 with exn ->
2241 dolog "error processing 'page' %S: %s"
2242 cmds (Printexc.to_string exn);
2243 exit 1;
2245 begin match state.currently with
2246 | Loading (l, gen) ->
2247 vlog "page %d took %f sec" l.pageno t;
2248 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2249 begin match state.throttle with
2250 | None ->
2251 let preloadedpages =
2252 if conf.preload
2253 then preloadlayout state.layout
2254 else state.layout
2256 let evict () =
2257 let module IntSet =
2258 Set.Make (struct type t = int let compare = (-) end) in
2259 let set =
2260 List.fold_left (fun s l -> IntSet.add l.pageno s)
2261 IntSet.empty preloadedpages
2263 let evictedpages =
2264 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2265 if not (IntSet.mem pageno set)
2266 then (
2267 wcmd "freepage %s" opaque;
2268 key :: accu
2270 else accu
2271 ) state.pagemap []
2273 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2275 evict ();
2276 state.currently <- Idle;
2277 if gen = state.gen
2278 then (
2279 tilepage l.pageno pageopaque state.layout;
2280 load state.layout;
2281 load preloadedpages;
2282 if pagevisible state.layout l.pageno
2283 && layoutready state.layout
2284 then G.postRedisplay "page";
2287 | Some (layout, _, _) ->
2288 state.currently <- Idle;
2289 tilepage l.pageno pageopaque layout;
2290 load state.layout
2291 end;
2293 | _ ->
2294 dolog "Inconsistent loading state";
2295 logcurrently state.currently;
2296 exit 1
2299 | "tile" ->
2300 let (x, y, opaque, size, t) =
2302 Scanf.sscanf args "%u %u %s %u %f"
2303 (fun x y p size t -> (x, y, p, size, t))
2304 with exn ->
2305 dolog "error processing 'tile' %S: %s"
2306 cmds (Printexc.to_string exn);
2307 exit 1;
2309 begin match state.currently with
2310 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2311 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2313 if tilew != conf.tilew || tileh != conf.tileh
2314 then (
2315 wcmd "freetile %s" opaque;
2316 state.currently <- Idle;
2317 load state.layout;
2319 else (
2320 puttileopaque l col row gen cs angle opaque size t;
2321 state.memused <- state.memused + size;
2322 state.uioh#infochanged Memused;
2323 gctiles ();
2324 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2325 opaque, size) state.tilelru;
2327 let layout =
2328 match state.throttle with
2329 | None -> state.layout
2330 | Some (layout, _, _) -> layout
2333 state.currently <- Idle;
2334 if gen = state.gen
2335 && conf.colorspace = cs
2336 && conf.angle = angle
2337 && tilevisible layout l.pageno x y
2338 then conttiling l.pageno pageopaque;
2340 begin match state.throttle with
2341 | None ->
2342 preload state.layout;
2343 if gen = state.gen
2344 && conf.colorspace = cs
2345 && conf.angle = angle
2346 && tilevisible state.layout l.pageno x y
2347 then G.postRedisplay "tile nothrottle";
2349 | Some (layout, y, _) ->
2350 let ready = layoutready layout in
2351 if ready
2352 then (
2353 state.y <- y;
2354 state.layout <- layout;
2355 state.throttle <- None;
2356 G.postRedisplay "throttle";
2358 else load layout;
2359 end;
2362 | _ ->
2363 dolog "Inconsistent tiling state";
2364 logcurrently state.currently;
2365 exit 1
2368 | "pdim" ->
2369 let pdim =
2371 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2372 with exn ->
2373 dolog "error processing 'pdim' %S: %s"
2374 cmds (Printexc.to_string exn);
2375 exit 1;
2377 state.uioh#infochanged Pdim;
2378 state.pdims <- pdim :: state.pdims
2380 | "o" ->
2381 let (l, n, t, h, pos) =
2383 Scanf.sscanf args "%u %u %d %u %n"
2384 (fun l n t h pos -> l, n, t, h, pos)
2385 with exn ->
2386 dolog "error processing 'o' %S: %s"
2387 cmds (Printexc.to_string exn);
2388 exit 1;
2390 let s = String.sub args pos (String.length args - pos) in
2391 let outline = (s, l, (n, float t /. float h, 0.0)) in
2392 begin match state.currently with
2393 | Outlining outlines ->
2394 state.currently <- Outlining (outline :: outlines)
2395 | Idle ->
2396 state.currently <- Outlining [outline]
2397 | currently ->
2398 dolog "invalid outlining state";
2399 logcurrently currently
2402 | "info" ->
2403 state.docinfo <- (1, args) :: state.docinfo
2405 | "infoend" ->
2406 state.uioh#infochanged Docinfo;
2407 state.docinfo <- List.rev state.docinfo
2409 | _ ->
2410 dolog "unknown cmd `%S'" cmds
2413 let onhist cb =
2414 let rc = cb.rc in
2415 let action = function
2416 | HCprev -> cbget cb ~-1
2417 | HCnext -> cbget cb 1
2418 | HCfirst -> cbget cb ~-(cb.rc)
2419 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2420 and cancel () = cb.rc <- rc
2421 in (action, cancel)
2424 let search pattern forward =
2425 if String.length pattern > 0
2426 then
2427 let pn, py =
2428 match state.layout with
2429 | [] -> 0, 0
2430 | l :: _ ->
2431 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2433 wcmd "search %d %d %d %d,%s\000"
2434 (btod conf.icase) pn py (btod forward) pattern;
2437 let intentry text key =
2438 let c =
2439 if key >= 32 && key < 127
2440 then Char.chr key
2441 else '\000'
2443 match c with
2444 | '0' .. '9' ->
2445 let text = addchar text c in
2446 TEcont text
2448 | _ ->
2449 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2450 TEcont text
2453 let linknentry text key =
2454 let c =
2455 if key >= 32 && key < 127
2456 then Char.chr key
2457 else '\000'
2459 match c with
2460 | 'a' .. 'z' ->
2461 let text = addchar text c in
2462 TEcont text
2464 | _ ->
2465 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2466 TEcont text
2469 let linkndone f s =
2470 if String.length s > 0
2471 then (
2472 let n =
2473 let l = String.length s in
2474 let rec loop pos n = if pos = l then n else
2475 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2476 loop (pos+1) (n*26 + m)
2477 in loop 0 0
2479 let rec loop n = function
2480 | [] -> ()
2481 | l :: rest ->
2482 match getopaque l.pageno with
2483 | None -> loop n rest
2484 | Some opaque ->
2485 let m = getlinkcount opaque in
2486 if n < m
2487 then (
2488 let under = getlink opaque n in
2489 f under
2491 else loop (n-m) rest
2493 loop n state.layout;
2497 let textentry text key =
2498 if key land 0xff00 = 0xff00
2499 then TEcont text
2500 else TEcont (text ^ Wsi.toutf8 key)
2503 let reqlayout angle proportional =
2504 match state.throttle with
2505 | None ->
2506 if nogeomcmds state.geomcmds
2507 then state.anchor <- getanchor ();
2508 conf.angle <- angle mod 360;
2509 if conf.angle != 0
2510 then (
2511 match state.mode with
2512 | LinkNav _ -> state.mode <- View
2513 | _ -> ()
2515 conf.proportional <- proportional;
2516 invalidate "reqlayout"
2517 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2518 | _ -> ()
2521 let settrim trimmargins trimfuzz =
2522 if nogeomcmds state.geomcmds
2523 then state.anchor <- getanchor ();
2524 conf.trimmargins <- trimmargins;
2525 conf.trimfuzz <- trimfuzz;
2526 let x0, y0, x1, y1 = trimfuzz in
2527 invalidate "settrim"
2528 (fun () ->
2529 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2530 Hashtbl.iter (fun _ opaque ->
2531 wcmd "freepage %s" opaque;
2532 ) state.pagemap;
2533 Hashtbl.clear state.pagemap;
2536 let setzoom zoom =
2537 match state.throttle with
2538 | None ->
2539 let zoom = max 0.01 zoom in
2540 if zoom <> conf.zoom
2541 then (
2542 state.prevzoom <- conf.zoom;
2543 conf.zoom <- zoom;
2544 reshape conf.winw conf.winh;
2545 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2548 | Some (layout, y, started) ->
2549 let time =
2550 match conf.maxwait with
2551 | None -> 0.0
2552 | Some t -> t
2554 let dt = now () -. started in
2555 if dt > time
2556 then (
2557 state.y <- y;
2558 load layout;
2562 let setcolumns mode columns coverA coverB =
2563 state.prevcolumns <- Some (conf.columns, conf.zoom);
2564 if columns < 0
2565 then (
2566 if isbirdseye mode
2567 then showtext '!' "split mode doesn't work in bird's eye"
2568 else (
2569 conf.columns <- Csplit (-columns, [||]);
2570 state.x <- 0;
2571 conf.zoom <- 1.0;
2574 else (
2575 if columns < 2
2576 then (
2577 conf.columns <- Csingle [||];
2578 state.x <- 0;
2579 setzoom 1.0;
2581 else (
2582 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2583 conf.zoom <- 1.0;
2586 reshape conf.winw conf.winh;
2589 let enterbirdseye () =
2590 let zoom = float conf.thumbw /. float conf.winw in
2591 let birdseyepageno =
2592 let cy = conf.winh / 2 in
2593 let fold = function
2594 | [] -> 0
2595 | l :: rest ->
2596 let rec fold best = function
2597 | [] -> best.pageno
2598 | l :: rest ->
2599 let d = cy - (l.pagedispy + l.pagevh/2)
2600 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2601 if abs d < abs dbest
2602 then fold l rest
2603 else best.pageno
2604 in fold l rest
2606 fold state.layout
2608 state.mode <- Birdseye (
2609 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2611 conf.zoom <- zoom;
2612 conf.presentation <- false;
2613 conf.interpagespace <- 10;
2614 conf.hlinks <- false;
2615 state.x <- 0;
2616 state.mstate <- Mnone;
2617 conf.maxwait <- None;
2618 conf.columns <- (
2619 match conf.beyecolumns with
2620 | Some c ->
2621 conf.zoom <- 1.0;
2622 Cmulti ((c, 0, 0), [||])
2623 | None -> Csingle [||]
2625 Wsi.setcursor Wsi.CURSOR_INHERIT;
2626 if conf.verbose
2627 then
2628 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2629 (100.0*.zoom)
2630 else
2631 state.text <- ""
2633 reshape conf.winw conf.winh;
2636 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2637 state.mode <- View;
2638 conf.zoom <- c.zoom;
2639 conf.presentation <- c.presentation;
2640 conf.interpagespace <- c.interpagespace;
2641 conf.maxwait <- c.maxwait;
2642 conf.hlinks <- c.hlinks;
2643 conf.beyecolumns <- (
2644 match conf.columns with
2645 | Cmulti ((c, _, _), _) -> Some c
2646 | Csingle _ -> None
2647 | Csplit _ -> failwith "leaving bird's eye split mode"
2649 conf.columns <- (
2650 match c.columns with
2651 | Cmulti (c, _) -> Cmulti (c, [||])
2652 | Csingle _ -> Csingle [||]
2653 | Csplit (c, _) -> Csplit (c, [||])
2655 state.x <- leftx;
2656 if conf.verbose
2657 then
2658 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2659 (100.0*.conf.zoom)
2661 reshape conf.winw conf.winh;
2662 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2665 let togglebirdseye () =
2666 match state.mode with
2667 | Birdseye vals -> leavebirdseye vals true
2668 | View -> enterbirdseye ()
2669 | _ -> ()
2672 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2673 let pageno = max 0 (pageno - incr) in
2674 let rec loop = function
2675 | [] -> gotopage1 pageno 0
2676 | l :: _ when l.pageno = pageno ->
2677 if l.pagedispy >= 0 && l.pagey = 0
2678 then G.postRedisplay "upbirdseye"
2679 else gotopage1 pageno 0
2680 | _ :: rest -> loop rest
2682 loop state.layout;
2683 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2686 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2687 let pageno = min (state.pagecount - 1) (pageno + incr) in
2688 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2689 let rec loop = function
2690 | [] ->
2691 let y, h = getpageyh pageno in
2692 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2693 gotoy (clamp dy)
2694 | l :: _ when l.pageno = pageno ->
2695 if l.pagevh != l.pageh
2696 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2697 else G.postRedisplay "downbirdseye"
2698 | _ :: rest -> loop rest
2700 loop state.layout
2703 let optentry mode _ key =
2704 let btos b = if b then "on" else "off" in
2705 if key >= 32 && key < 127
2706 then
2707 let c = Char.chr key in
2708 match c with
2709 | 's' ->
2710 let ondone s =
2711 try conf.scrollstep <- int_of_string s with exc ->
2712 state.text <- Printf.sprintf "bad integer `%s': %s"
2713 s (Printexc.to_string exc)
2715 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2717 | 'A' ->
2718 let ondone s =
2720 conf.autoscrollstep <- int_of_string s;
2721 if state.autoscroll <> None
2722 then state.autoscroll <- Some conf.autoscrollstep
2723 with exc ->
2724 state.text <- Printf.sprintf "bad integer `%s': %s"
2725 s (Printexc.to_string exc)
2727 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2729 | 'C' ->
2730 let ondone s =
2732 let n, a, b = multicolumns_of_string s in
2733 setcolumns mode n a b;
2734 with exc ->
2735 state.text <- Printf.sprintf "bad columns `%s': %s"
2736 s (Printexc.to_string exc)
2738 TEswitch ("columns: ", "", None, textentry, ondone, true)
2740 | 'Z' ->
2741 let ondone s =
2743 let zoom = float (int_of_string s) /. 100.0 in
2744 setzoom zoom
2745 with exc ->
2746 state.text <- Printf.sprintf "bad integer `%s': %s"
2747 s (Printexc.to_string exc)
2749 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2751 | 't' ->
2752 let ondone s =
2754 conf.thumbw <- bound (int_of_string s) 2 4096;
2755 state.text <-
2756 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2757 begin match mode with
2758 | Birdseye beye ->
2759 leavebirdseye beye false;
2760 enterbirdseye ();
2761 | _ -> ();
2763 with exc ->
2764 state.text <- Printf.sprintf "bad integer `%s': %s"
2765 s (Printexc.to_string exc)
2767 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2769 | 'R' ->
2770 let ondone s =
2771 match try
2772 Some (int_of_string s)
2773 with exc ->
2774 state.text <- Printf.sprintf "bad integer `%s': %s"
2775 s (Printexc.to_string exc);
2776 None
2777 with
2778 | Some angle -> reqlayout angle conf.proportional
2779 | None -> ()
2781 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2783 | 'i' ->
2784 conf.icase <- not conf.icase;
2785 TEdone ("case insensitive search " ^ (btos conf.icase))
2787 | 'p' ->
2788 conf.preload <- not conf.preload;
2789 gotoy state.y;
2790 TEdone ("preload " ^ (btos conf.preload))
2792 | 'v' ->
2793 conf.verbose <- not conf.verbose;
2794 TEdone ("verbose " ^ (btos conf.verbose))
2796 | 'd' ->
2797 conf.debug <- not conf.debug;
2798 TEdone ("debug " ^ (btos conf.debug))
2800 | 'h' ->
2801 conf.maxhfit <- not conf.maxhfit;
2802 state.maxy <- calcheight ();
2803 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2805 | 'c' ->
2806 conf.crophack <- not conf.crophack;
2807 TEdone ("crophack " ^ btos conf.crophack)
2809 | 'a' ->
2810 let s =
2811 match conf.maxwait with
2812 | None ->
2813 conf.maxwait <- Some infinity;
2814 "always wait for page to complete"
2815 | Some _ ->
2816 conf.maxwait <- None;
2817 "show placeholder if page is not ready"
2819 TEdone s
2821 | 'f' ->
2822 conf.underinfo <- not conf.underinfo;
2823 TEdone ("underinfo " ^ btos conf.underinfo)
2825 | 'P' ->
2826 conf.savebmarks <- not conf.savebmarks;
2827 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2829 | 'S' ->
2830 let ondone s =
2832 let pageno, py =
2833 match state.layout with
2834 | [] -> 0, 0
2835 | l :: _ ->
2836 l.pageno, l.pagey
2838 conf.interpagespace <- int_of_string s;
2839 docolumns conf.columns;
2840 state.maxy <- calcheight ();
2841 let y = getpagey pageno in
2842 gotoy (y + py)
2843 with exc ->
2844 state.text <- Printf.sprintf "bad integer `%s': %s"
2845 s (Printexc.to_string exc)
2847 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2849 | 'l' ->
2850 reqlayout conf.angle (not conf.proportional);
2851 TEdone ("proportional display " ^ btos conf.proportional)
2853 | 'T' ->
2854 settrim (not conf.trimmargins) conf.trimfuzz;
2855 TEdone ("trim margins " ^ btos conf.trimmargins)
2857 | 'I' ->
2858 conf.invert <- not conf.invert;
2859 TEdone ("invert colors " ^ btos conf.invert)
2861 | 'x' ->
2862 let ondone s =
2863 cbput state.hists.sel s;
2864 conf.selcmd <- s;
2866 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2867 textentry, ondone, true)
2869 | _ ->
2870 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2871 TEstop
2872 else
2873 TEcont state.text
2876 class type lvsource = object
2877 method getitemcount : int
2878 method getitem : int -> (string * int)
2879 method hasaction : int -> bool
2880 method exit :
2881 uioh:uioh ->
2882 cancel:bool ->
2883 active:int ->
2884 first:int ->
2885 pan:int ->
2886 qsearch:string ->
2887 uioh option
2888 method getactive : int
2889 method getfirst : int
2890 method getqsearch : string
2891 method setqsearch : string -> unit
2892 method getpan : int
2893 end;;
2895 class virtual lvsourcebase = object
2896 val mutable m_active = 0
2897 val mutable m_first = 0
2898 val mutable m_qsearch = ""
2899 val mutable m_pan = 0
2900 method getactive = m_active
2901 method getfirst = m_first
2902 method getqsearch = m_qsearch
2903 method getpan = m_pan
2904 method setqsearch s = m_qsearch <- s
2905 end;;
2907 let withoutlastutf8 s =
2908 let len = String.length s in
2909 if len = 0
2910 then s
2911 else
2912 let rec find pos =
2913 if pos = 0
2914 then pos
2915 else
2916 let b = Char.code s.[pos] in
2917 if b land 0b110000 = 0b11000000
2918 then find (pos-1)
2919 else pos-1
2921 let first =
2922 if Char.code s.[len-1] land 0x80 = 0
2923 then len-1
2924 else find (len-1)
2926 String.sub s 0 first;
2929 let textentrykeyboard
2930 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2931 let enttext te =
2932 state.mode <- Textentry (te, onleave);
2933 state.text <- "";
2934 enttext ();
2935 G.postRedisplay "textentrykeyboard enttext";
2937 let histaction cmd =
2938 match opthist with
2939 | None -> ()
2940 | Some (action, _) ->
2941 state.mode <- Textentry (
2942 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2944 G.postRedisplay "textentry histaction"
2946 match key with
2947 | 0xff08 -> (* backspace *)
2948 let s = withoutlastutf8 text in
2949 let len = String.length s in
2950 if cancelonempty && len = 0
2951 then (
2952 onleave Cancel;
2953 G.postRedisplay "textentrykeyboard after cancel";
2955 else (
2956 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2959 | 0xff0d ->
2960 ondone text;
2961 onleave Confirm;
2962 G.postRedisplay "textentrykeyboard after confirm"
2964 | 0xff52 -> histaction HCprev
2965 | 0xff54 -> histaction HCnext
2966 | 0xff50 -> histaction HCfirst
2967 | 0xff57 -> histaction HClast
2969 | 0xff1b -> (* escape*)
2970 if String.length text = 0
2971 then (
2972 begin match opthist with
2973 | None -> ()
2974 | Some (_, onhistcancel) -> onhistcancel ()
2975 end;
2976 onleave Cancel;
2977 state.text <- "";
2978 G.postRedisplay "textentrykeyboard after cancel2"
2980 else (
2981 enttext (c, "", opthist, onkey, ondone, cancelonempty)
2984 | 0xff9f | 0xffff -> () (* delete *)
2986 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2987 begin match onkey text key with
2988 | TEdone text ->
2989 ondone text;
2990 onleave Confirm;
2991 G.postRedisplay "textentrykeyboard after confirm2";
2993 | TEcont text ->
2994 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2996 | TEstop ->
2997 onleave Cancel;
2998 G.postRedisplay "textentrykeyboard after cancel3"
3000 | TEswitch te ->
3001 state.mode <- Textentry (te, onleave);
3002 G.postRedisplay "textentrykeyboard switch";
3003 end;
3005 | _ ->
3006 vlog "unhandled key %s" (Wsi.keyname key)
3009 let firstof first active =
3010 if first > active || abs (first - active) > fstate.maxrows - 1
3011 then max 0 (active - (fstate.maxrows/2))
3012 else first
3015 let calcfirst first active =
3016 if active > first
3017 then
3018 let rows = active - first in
3019 if rows > fstate.maxrows then active - fstate.maxrows else first
3020 else active
3023 let scrollph y maxy =
3024 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3025 let sh = float conf.winh /. sh in
3026 let sh = max sh (float conf.scrollh) in
3028 let percent =
3029 if y = state.maxy
3030 then 1.0
3031 else float y /. float maxy
3033 let position = (float conf.winh -. sh) *. percent in
3035 let position =
3036 if position +. sh > float conf.winh
3037 then float conf.winh -. sh
3038 else position
3040 position, sh;
3043 let coe s = (s :> uioh);;
3045 class listview ~(source:lvsource) ~trusted ~modehash =
3046 object (self)
3047 val m_pan = source#getpan
3048 val m_first = source#getfirst
3049 val m_active = source#getactive
3050 val m_qsearch = source#getqsearch
3051 val m_prev_uioh = state.uioh
3053 method private elemunder y =
3054 let n = y / (fstate.fontsize+1) in
3055 if m_first + n < source#getitemcount
3056 then (
3057 if source#hasaction (m_first + n)
3058 then Some (m_first + n)
3059 else None
3061 else None
3063 method display =
3064 Gl.enable `blend;
3065 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3066 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3067 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3068 GlDraw.color (1., 1., 1.);
3069 Gl.enable `texture_2d;
3070 let fs = fstate.fontsize in
3071 let nfs = fs + 1 in
3072 let ww = fstate.wwidth in
3073 let tabw = 30.0*.ww in
3074 let itemcount = source#getitemcount in
3075 let rec loop row =
3076 if (row - m_first) * nfs > conf.winh
3077 then ()
3078 else (
3079 if row >= 0 && row < itemcount
3080 then (
3081 let (s, level) = source#getitem row in
3082 let y = (row - m_first) * nfs in
3083 let x = 5.0 +. float (level + m_pan) *. ww in
3084 if row = m_active
3085 then (
3086 Gl.disable `texture_2d;
3087 GlDraw.polygon_mode `both `line;
3088 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3089 GlDraw.rect (1., float (y + 1))
3090 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3091 GlDraw.polygon_mode `both `fill;
3092 GlDraw.color (1., 1., 1.);
3093 Gl.enable `texture_2d;
3096 let drawtabularstring s =
3097 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3098 if trusted
3099 then
3100 let tabpos = try String.index s '\t' with Not_found -> -1 in
3101 if tabpos > 0
3102 then
3103 let len = String.length s - tabpos - 1 in
3104 let s1 = String.sub s 0 tabpos
3105 and s2 = String.sub s (tabpos + 1) len in
3106 let nx = drawstr x s1 in
3107 let sw = nx -. x in
3108 let x = x +. (max tabw sw) in
3109 drawstr x s2
3110 else
3111 drawstr x s
3112 else
3113 drawstr x s
3115 let _ = drawtabularstring s in
3116 loop (row+1)
3120 loop m_first;
3121 Gl.disable `blend;
3122 Gl.disable `texture_2d;
3124 method updownlevel incr =
3125 let len = source#getitemcount in
3126 let curlevel =
3127 if m_active >= 0 && m_active < len
3128 then snd (source#getitem m_active)
3129 else -1
3131 let rec flow i =
3132 if i = len then i-1 else if i = -1 then 0 else
3133 let _, l = source#getitem i in
3134 if l != curlevel then i else flow (i+incr)
3136 let active = flow m_active in
3137 let first = calcfirst m_first active in
3138 G.postRedisplay "outline updownlevel";
3139 {< m_active = active; m_first = first >}
3141 method private key1 key mask =
3142 let set1 active first qsearch =
3143 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3145 let search active pattern incr =
3146 let dosearch re =
3147 let rec loop n =
3148 if n >= 0 && n < source#getitemcount
3149 then (
3150 let s, _ = source#getitem n in
3152 (try ignore (Str.search_forward re s 0); true
3153 with Not_found -> false)
3154 then Some n
3155 else loop (n + incr)
3157 else None
3159 loop active
3162 let re = Str.regexp_case_fold pattern in
3163 dosearch re
3164 with Failure s ->
3165 state.text <- s;
3166 None
3168 let itemcount = source#getitemcount in
3169 let find start incr =
3170 let rec find i =
3171 if i = -1 || i = itemcount
3172 then -1
3173 else (
3174 if source#hasaction i
3175 then i
3176 else find (i + incr)
3179 find start
3181 let set active first =
3182 let first = bound first 0 (itemcount - fstate.maxrows) in
3183 state.text <- "";
3184 coe {< m_active = active; m_first = first >}
3186 let navigate incr =
3187 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3188 let active, first =
3189 let incr1 = if incr > 0 then 1 else -1 in
3190 if isvisible m_first m_active
3191 then
3192 let next =
3193 let next = m_active + incr in
3194 let next =
3195 if next < 0 || next >= itemcount
3196 then -1
3197 else find next incr1
3199 if next = -1 || abs (m_active - next) > fstate.maxrows
3200 then -1
3201 else next
3203 if next = -1
3204 then
3205 let first = m_first + incr in
3206 let first = bound first 0 (itemcount - 1) in
3207 let next =
3208 let next = m_active + incr in
3209 let next = bound next 0 (itemcount - 1) in
3210 find next ~-incr1
3212 let active = if next = -1 then m_active else next in
3213 active, first
3214 else
3215 let first = min next m_first in
3216 let first =
3217 if abs (next - first) > fstate.maxrows
3218 then first + incr
3219 else first
3221 next, first
3222 else
3223 let first = m_first + incr in
3224 let first = bound first 0 (itemcount - 1) in
3225 let active =
3226 let next = m_active + incr in
3227 let next = bound next 0 (itemcount - 1) in
3228 let next = find next incr1 in
3229 let active =
3230 if next = -1 || abs (m_active - first) > fstate.maxrows
3231 then (
3232 let active = if m_active = -1 then next else m_active in
3233 active
3235 else next
3237 if isvisible first active
3238 then active
3239 else -1
3241 active, first
3243 G.postRedisplay "listview navigate";
3244 set active first;
3246 match key with
3247 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3248 let incr = if key = 0x72 then -1 else 1 in
3249 let active, first =
3250 match search (m_active + incr) m_qsearch incr with
3251 | None ->
3252 state.text <- m_qsearch ^ " [not found]";
3253 m_active, m_first
3254 | Some active ->
3255 state.text <- m_qsearch;
3256 active, firstof m_first active
3258 G.postRedisplay "listview ctrl-r/s";
3259 set1 active first m_qsearch;
3261 | 0xff08 -> (* backspace *)
3262 if String.length m_qsearch = 0
3263 then coe self
3264 else (
3265 let qsearch = withoutlastutf8 m_qsearch in
3266 let len = String.length qsearch in
3267 if len = 0
3268 then (
3269 state.text <- "";
3270 G.postRedisplay "listview empty qsearch";
3271 set1 m_active m_first "";
3273 else
3274 let active, first =
3275 match search m_active qsearch ~-1 with
3276 | None ->
3277 state.text <- qsearch ^ " [not found]";
3278 m_active, m_first
3279 | Some active ->
3280 state.text <- qsearch;
3281 active, firstof m_first active
3283 G.postRedisplay "listview backspace qsearch";
3284 set1 active first qsearch
3287 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3288 let pattern = m_qsearch ^ Wsi.toutf8 key in
3289 let active, first =
3290 match search m_active pattern 1 with
3291 | None ->
3292 state.text <- pattern ^ " [not found]";
3293 m_active, m_first
3294 | Some active ->
3295 state.text <- pattern;
3296 active, firstof m_first active
3298 G.postRedisplay "listview qsearch add";
3299 set1 active first pattern;
3301 | 0xff1b -> (* escape *)
3302 state.text <- "";
3303 if String.length m_qsearch = 0
3304 then (
3305 G.postRedisplay "list view escape";
3306 begin
3307 match
3308 source#exit (coe self) true m_active m_first m_pan m_qsearch
3309 with
3310 | None -> m_prev_uioh
3311 | Some uioh -> uioh
3314 else (
3315 G.postRedisplay "list view kill qsearch";
3316 source#setqsearch "";
3317 coe {< m_qsearch = "" >}
3320 | 0xff0d -> (* return *)
3321 state.text <- "";
3322 let self = {< m_qsearch = "" >} in
3323 source#setqsearch "";
3324 let opt =
3325 G.postRedisplay "listview enter";
3326 if m_active >= 0 && m_active < source#getitemcount
3327 then (
3328 source#exit (coe self) false m_active m_first m_pan "";
3330 else (
3331 source#exit (coe self) true m_active m_first m_pan "";
3334 begin match opt with
3335 | None -> m_prev_uioh
3336 | Some uioh -> uioh
3339 | 0xff9f | 0xffff -> (* delete *)
3340 coe self
3342 | 0xff52 -> navigate ~-1 (* up *)
3343 | 0xff54 -> navigate 1 (* down *)
3344 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3345 | 0xff56 -> navigate fstate.maxrows (* next *)
3347 | 0xff53 -> (* right *)
3348 state.text <- "";
3349 G.postRedisplay "listview right";
3350 coe {< m_pan = m_pan - 1 >}
3352 | 0xff51 -> (* left *)
3353 state.text <- "";
3354 G.postRedisplay "listview left";
3355 coe {< m_pan = m_pan + 1 >}
3357 | 0xff50 -> (* home *)
3358 let active = find 0 1 in
3359 G.postRedisplay "listview home";
3360 set active 0;
3362 | 0xff57 -> (* end *)
3363 let first = max 0 (itemcount - fstate.maxrows) in
3364 let active = find (itemcount - 1) ~-1 in
3365 G.postRedisplay "listview end";
3366 set active first;
3368 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3369 coe self
3371 | _ ->
3372 dolog "listview unknown key %#x" key; coe self
3374 method key key mask =
3375 match state.mode with
3376 | Textentry te -> textentrykeyboard key mask te; coe self
3377 | _ -> self#key1 key mask
3379 method button button down x y _ =
3380 let opt =
3381 match button with
3382 | 1 when x > conf.winw - conf.scrollbw ->
3383 G.postRedisplay "listview scroll";
3384 if down
3385 then
3386 let _, position, sh = self#scrollph in
3387 if y > truncate position && y < truncate (position +. sh)
3388 then (
3389 state.mstate <- Mscrolly;
3390 Some (coe self)
3392 else
3393 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3394 let first = truncate (s *. float source#getitemcount) in
3395 let first = min source#getitemcount first in
3396 Some (coe {< m_first = first; m_active = first >})
3397 else (
3398 state.mstate <- Mnone;
3399 Some (coe self);
3401 | 1 when not down ->
3402 begin match self#elemunder y with
3403 | Some n ->
3404 G.postRedisplay "listview click";
3405 source#exit
3406 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3407 | _ ->
3408 Some (coe self)
3410 | n when (n == 4 || n == 5) && not down ->
3411 let len = source#getitemcount in
3412 let first =
3413 if n = 5 && m_first + fstate.maxrows >= len
3414 then
3415 m_first
3416 else
3417 let first = m_first + (if n == 4 then -1 else 1) in
3418 bound first 0 (len - 1)
3420 G.postRedisplay "listview wheel";
3421 Some (coe {< m_first = first >})
3422 | n when (n = 6 || n = 7) && not down ->
3423 let inc = m_first + (if n = 7 then -1 else 1) in
3424 G.postRedisplay "listview hwheel";
3425 Some (coe {< m_pan = m_pan + inc >})
3426 | _ ->
3427 Some (coe self)
3429 match opt with
3430 | None -> m_prev_uioh
3431 | Some uioh -> uioh
3433 method motion _ y =
3434 match state.mstate with
3435 | Mscrolly ->
3436 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3437 let first = truncate (s *. float source#getitemcount) in
3438 let first = min source#getitemcount first in
3439 G.postRedisplay "listview motion";
3440 coe {< m_first = first; m_active = first >}
3441 | _ -> coe self
3443 method pmotion x y =
3444 if x < conf.winw - conf.scrollbw
3445 then
3446 let n =
3447 match self#elemunder y with
3448 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3449 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3451 let o =
3452 if n != m_active
3453 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3454 else self
3456 coe o
3457 else (
3458 Wsi.setcursor Wsi.CURSOR_INHERIT;
3459 coe self
3462 method infochanged _ = ()
3464 method scrollpw = (0, 0.0, 0.0)
3465 method scrollph =
3466 let nfs = fstate.fontsize + 1 in
3467 let y = m_first * nfs in
3468 let itemcount = source#getitemcount in
3469 let maxi = max 0 (itemcount - fstate.maxrows) in
3470 let maxy = maxi * nfs in
3471 let p, h = scrollph y maxy in
3472 conf.scrollbw, p, h
3474 method modehash = modehash
3475 end;;
3477 class outlinelistview ~source =
3478 object (self)
3479 inherit listview
3480 ~source:(source :> lvsource)
3481 ~trusted:false
3482 ~modehash:(findkeyhash conf "outline")
3483 as super
3485 method key key mask =
3486 let calcfirst first active =
3487 if active > first
3488 then
3489 let rows = active - first in
3490 let maxrows =
3491 if String.length state.text = 0
3492 then fstate.maxrows
3493 else fstate.maxrows - 2
3495 if rows > maxrows then active - maxrows else first
3496 else active
3498 let navigate incr =
3499 let active = m_active + incr in
3500 let active = bound active 0 (source#getitemcount - 1) in
3501 let first = calcfirst m_first active in
3502 G.postRedisplay "outline navigate";
3503 coe {< m_active = active; m_first = first >}
3505 let ctrl = Wsi.withctrl mask in
3506 match key with
3507 | 110 when ctrl -> (* ctrl-n *)
3508 source#narrow m_qsearch;
3509 G.postRedisplay "outline ctrl-n";
3510 coe {< m_first = 0; m_active = 0 >}
3512 | 117 when ctrl -> (* ctrl-u *)
3513 source#denarrow;
3514 G.postRedisplay "outline ctrl-u";
3515 state.text <- "";
3516 coe {< m_first = 0; m_active = 0 >}
3518 | 108 when ctrl -> (* ctrl-l *)
3519 let first = m_active - (fstate.maxrows / 2) in
3520 G.postRedisplay "outline ctrl-l";
3521 coe {< m_first = first >}
3523 | 0xff9f | 0xffff -> (* delete *)
3524 source#remove m_active;
3525 G.postRedisplay "outline delete";
3526 let active = max 0 (m_active-1) in
3527 coe {< m_first = firstof m_first active;
3528 m_active = active >}
3530 | 0xff52 -> navigate ~-1 (* up *)
3531 | 0xff54 -> navigate 1 (* down *)
3532 | 0xff55 -> (* prior *)
3533 navigate ~-(fstate.maxrows)
3534 | 0xff56 -> (* next *)
3535 navigate fstate.maxrows
3537 | 0xff53 -> (* [ctrl-]right *)
3538 let o =
3539 if ctrl
3540 then (
3541 G.postRedisplay "outline ctrl right";
3542 {< m_pan = m_pan + 1 >}
3544 else self#updownlevel 1
3546 coe o
3548 | 0xff51 -> (* [ctrl-]left *)
3549 let o =
3550 if ctrl
3551 then (
3552 G.postRedisplay "outline ctrl left";
3553 {< m_pan = m_pan - 1 >}
3555 else self#updownlevel ~-1
3557 coe o
3559 | 0xff50 -> (* home *)
3560 G.postRedisplay "outline home";
3561 coe {< m_first = 0; m_active = 0 >}
3563 | 0xff57 -> (* end *)
3564 let active = source#getitemcount - 1 in
3565 let first = max 0 (active - fstate.maxrows) in
3566 G.postRedisplay "outline end";
3567 coe {< m_active = active; m_first = first >}
3569 | _ -> super#key key mask
3572 let outlinesource usebookmarks =
3573 let empty = [||] in
3574 (object
3575 inherit lvsourcebase
3576 val mutable m_items = empty
3577 val mutable m_orig_items = empty
3578 val mutable m_prev_items = empty
3579 val mutable m_narrow_pattern = ""
3580 val mutable m_hadremovals = false
3582 method getitemcount =
3583 Array.length m_items + (if m_hadremovals then 1 else 0)
3585 method getitem n =
3586 if n == Array.length m_items && m_hadremovals
3587 then
3588 ("[Confirm removal]", 0)
3589 else
3590 let s, n, _ = m_items.(n) in
3591 (s, n)
3593 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3594 ignore (uioh, first, qsearch);
3595 let confrimremoval = m_hadremovals && active = Array.length m_items in
3596 let items =
3597 if String.length m_narrow_pattern = 0
3598 then m_orig_items
3599 else m_items
3601 if not cancel
3602 then (
3603 if not confrimremoval
3604 then(
3605 let _, _, anchor = m_items.(active) in
3606 gotoanchor anchor;
3607 m_items <- items;
3609 else (
3610 state.bookmarks <- Array.to_list m_items;
3611 m_orig_items <- m_items;
3614 else m_items <- items;
3615 m_pan <- pan;
3616 None
3618 method hasaction _ = true
3620 method greetmsg =
3621 if Array.length m_items != Array.length m_orig_items
3622 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3623 else ""
3625 method narrow pattern =
3626 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3627 match reopt with
3628 | None -> ()
3629 | Some re ->
3630 let rec loop accu n =
3631 if n = -1
3632 then (
3633 m_narrow_pattern <- pattern;
3634 m_items <- Array.of_list accu
3636 else
3637 let (s, _, _) as o = m_items.(n) in
3638 let accu =
3639 if (try ignore (Str.search_forward re s 0); true
3640 with Not_found -> false)
3641 then o :: accu
3642 else accu
3644 loop accu (n-1)
3646 loop [] (Array.length m_items - 1)
3648 method denarrow =
3649 m_orig_items <- (
3650 if usebookmarks
3651 then Array.of_list state.bookmarks
3652 else state.outlines
3654 m_items <- m_orig_items
3656 method remove m =
3657 if usebookmarks
3658 then
3659 if m >= 0 && m < Array.length m_items
3660 then (
3661 m_hadremovals <- true;
3662 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3663 let n = if n >= m then n+1 else n in
3664 m_items.(n)
3668 method reset anchor items =
3669 m_hadremovals <- false;
3670 if m_orig_items == empty || m_prev_items != items
3671 then (
3672 m_orig_items <- items;
3673 if String.length m_narrow_pattern = 0
3674 then m_items <- items;
3676 m_prev_items <- items;
3677 let rely = getanchory anchor in
3678 let active =
3679 let rec loop n best bestd =
3680 if n = Array.length m_items
3681 then best
3682 else
3683 let (_, _, anchor) = m_items.(n) in
3684 let orely = getanchory anchor in
3685 let d = abs (orely - rely) in
3686 if d < bestd
3687 then loop (n+1) n d
3688 else loop (n+1) best bestd
3690 loop 0 ~-1 max_int
3692 m_active <- active;
3693 m_first <- firstof m_first active
3694 end)
3697 let enterselector usebookmarks =
3698 let source = outlinesource usebookmarks in
3699 fun errmsg ->
3700 let outlines =
3701 if usebookmarks
3702 then Array.of_list state.bookmarks
3703 else state.outlines
3705 if Array.length outlines = 0
3706 then (
3707 showtext ' ' errmsg;
3709 else (
3710 state.text <- source#greetmsg;
3711 Wsi.setcursor Wsi.CURSOR_INHERIT;
3712 let anchor = getanchor () in
3713 source#reset anchor outlines;
3714 state.uioh <- coe (new outlinelistview ~source);
3715 G.postRedisplay "enter selector";
3719 let enteroutlinemode =
3720 let f = enterselector false in
3721 fun ()-> f "Document has no outline";
3724 let enterbookmarkmode =
3725 let f = enterselector true in
3726 fun () -> f "Document has no bookmarks (yet)";
3729 let color_of_string s =
3730 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3731 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3735 let color_to_string (r, g, b) =
3736 let r = truncate (r *. 256.0)
3737 and g = truncate (g *. 256.0)
3738 and b = truncate (b *. 256.0) in
3739 Printf.sprintf "%d/%d/%d" r g b
3742 let irect_of_string s =
3743 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3746 let irect_to_string (x0,y0,x1,y1) =
3747 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3750 let makecheckers () =
3751 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3752 following to say:
3753 converted by Issac Trotts. July 25, 2002 *)
3754 let image_height = 64
3755 and image_width = 64 in
3757 let make_image () =
3758 let image =
3759 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3761 for i = 0 to image_width - 1 do
3762 for j = 0 to image_height - 1 do
3763 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3764 (if (i land 8 ) lxor (j land 8) = 0
3765 then [|255;255;255|] else [|200;200;200|])
3766 done
3767 done;
3768 image
3770 let image = make_image () in
3771 let id = GlTex.gen_texture () in
3772 GlTex.bind_texture `texture_2d id;
3773 GlPix.store (`unpack_alignment 1);
3774 GlTex.image2d image;
3775 List.iter (GlTex.parameter ~target:`texture_2d)
3776 [ `wrap_s `repeat;
3777 `wrap_t `repeat;
3778 `mag_filter `nearest;
3779 `min_filter `nearest ];
3783 let setcheckers enabled =
3784 match state.texid with
3785 | None ->
3786 if enabled then state.texid <- Some (makecheckers ())
3788 | Some texid ->
3789 if not enabled
3790 then (
3791 GlTex.delete_texture texid;
3792 state.texid <- None;
3796 let int_of_string_with_suffix s =
3797 let l = String.length s in
3798 let s1, shift =
3799 if l > 1
3800 then
3801 let suffix = Char.lowercase s.[l-1] in
3802 match suffix with
3803 | 'k' -> String.sub s 0 (l-1), 10
3804 | 'm' -> String.sub s 0 (l-1), 20
3805 | 'g' -> String.sub s 0 (l-1), 30
3806 | _ -> s, 0
3807 else s, 0
3809 let n = int_of_string s1 in
3810 let m = n lsl shift in
3811 if m < 0 || m < n
3812 then raise (Failure "value too large")
3813 else m
3816 let string_with_suffix_of_int n =
3817 if n = 0
3818 then "0"
3819 else
3820 let n, s =
3821 if n land ((1 lsl 20) - 1) = 0
3822 then n lsr 20, "M"
3823 else (
3824 if n land ((1 lsl 10) - 1) = 0
3825 then n lsr 10, "K"
3826 else n, ""
3829 let rec loop s n =
3830 let h = n mod 1000 in
3831 let n = n / 1000 in
3832 if n = 0
3833 then string_of_int h ^ s
3834 else (
3835 let s = Printf.sprintf "_%03d%s" h s in
3836 loop s n
3839 loop "" n ^ s;
3842 let defghyllscroll = (40, 8, 32);;
3843 let ghyllscroll_of_string s =
3844 let (n, a, b) as nab =
3845 if s = "default"
3846 then defghyllscroll
3847 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3849 if n <= a || n <= b || a >= b
3850 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3851 nab;
3854 let ghyllscroll_to_string ((n, a, b) as nab) =
3855 if nab = defghyllscroll
3856 then "default"
3857 else Printf.sprintf "%d,%d,%d" n a b;
3860 let describe_location () =
3861 let f (fn, _) l =
3862 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3864 let fn, ln = List.fold_left f (-1, -1) state.layout in
3865 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3866 let percent =
3867 if maxy <= 0
3868 then 100.
3869 else (100. *. (float state.y /. float maxy))
3871 if fn = ln
3872 then
3873 Printf.sprintf "page %d of %d [%.2f%%]"
3874 (fn+1) state.pagecount percent
3875 else
3876 Printf.sprintf
3877 "pages %d-%d of %d [%.2f%%]"
3878 (fn+1) (ln+1) state.pagecount percent
3881 let enterinfomode =
3882 let btos b = if b then "\xe2\x88\x9a" else "" in
3883 let showextended = ref false in
3884 let leave mode = function
3885 | Confirm -> state.mode <- mode
3886 | Cancel -> state.mode <- mode in
3887 let src =
3888 (object
3889 val mutable m_first_time = true
3890 val mutable m_l = []
3891 val mutable m_a = [||]
3892 val mutable m_prev_uioh = nouioh
3893 val mutable m_prev_mode = View
3895 inherit lvsourcebase
3897 method reset prev_mode prev_uioh =
3898 m_a <- Array.of_list (List.rev m_l);
3899 m_l <- [];
3900 m_prev_mode <- prev_mode;
3901 m_prev_uioh <- prev_uioh;
3902 if m_first_time
3903 then (
3904 let rec loop n =
3905 if n >= Array.length m_a
3906 then ()
3907 else
3908 match m_a.(n) with
3909 | _, _, _, Action _ -> m_active <- n
3910 | _ -> loop (n+1)
3912 loop 0;
3913 m_first_time <- false;
3916 method int name get set =
3917 m_l <-
3918 (name, `int get, 1, Action (
3919 fun u ->
3920 let ondone s =
3921 try set (int_of_string s)
3922 with exn ->
3923 state.text <- Printf.sprintf "bad integer `%s': %s"
3924 s (Printexc.to_string exn)
3926 state.text <- "";
3927 let te = name ^ ": ", "", None, intentry, ondone, true in
3928 state.mode <- Textentry (te, leave m_prev_mode);
3930 )) :: m_l
3932 method int_with_suffix name get set =
3933 m_l <-
3934 (name, `intws get, 1, Action (
3935 fun u ->
3936 let ondone s =
3937 try set (int_of_string_with_suffix s)
3938 with exn ->
3939 state.text <- Printf.sprintf "bad integer `%s': %s"
3940 s (Printexc.to_string exn)
3942 state.text <- "";
3943 let te =
3944 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3946 state.mode <- Textentry (te, leave m_prev_mode);
3948 )) :: m_l
3950 method bool ?(offset=1) ?(btos=btos) name get set =
3951 m_l <-
3952 (name, `bool (btos, get), offset, Action (
3953 fun u ->
3954 let v = get () in
3955 set (not v);
3957 )) :: m_l
3959 method color name get set =
3960 m_l <-
3961 (name, `color get, 1, Action (
3962 fun u ->
3963 let invalid = (nan, nan, nan) in
3964 let ondone s =
3965 let c =
3966 try color_of_string s
3967 with exn ->
3968 state.text <- Printf.sprintf "bad color `%s': %s"
3969 s (Printexc.to_string exn);
3970 invalid
3972 if c <> invalid
3973 then set c;
3975 let te = name ^ ": ", "", None, textentry, ondone, true in
3976 state.text <- color_to_string (get ());
3977 state.mode <- Textentry (te, leave m_prev_mode);
3979 )) :: m_l
3981 method string name get set =
3982 m_l <-
3983 (name, `string get, 1, Action (
3984 fun u ->
3985 let ondone s = set s in
3986 let te = name ^ ": ", "", None, textentry, ondone, true in
3987 state.mode <- Textentry (te, leave m_prev_mode);
3989 )) :: m_l
3991 method colorspace name get set =
3992 m_l <-
3993 (name, `string get, 1, Action (
3994 fun _ ->
3995 let source =
3996 let vals = [| "rgb"; "bgr"; "gray" |] in
3997 (object
3998 inherit lvsourcebase
4000 initializer
4001 m_active <- int_of_colorspace conf.colorspace;
4002 m_first <- 0;
4004 method getitemcount = Array.length vals
4005 method getitem n = (vals.(n), 0)
4006 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4007 ignore (uioh, first, pan, qsearch);
4008 if not cancel then set active;
4009 None
4010 method hasaction _ = true
4011 end)
4013 state.text <- "";
4014 let modehash = findkeyhash conf "info" in
4015 coe (new listview ~source ~trusted:true ~modehash)
4016 )) :: m_l
4018 method caption s offset =
4019 m_l <- (s, `empty, offset, Noaction) :: m_l
4021 method caption2 s f offset =
4022 m_l <- (s, `string f, offset, Noaction) :: m_l
4024 method getitemcount = Array.length m_a
4026 method getitem n =
4027 let tostr = function
4028 | `int f -> string_of_int (f ())
4029 | `intws f -> string_with_suffix_of_int (f ())
4030 | `string f -> f ()
4031 | `color f -> color_to_string (f ())
4032 | `bool (btos, f) -> btos (f ())
4033 | `empty -> ""
4035 let name, t, offset, _ = m_a.(n) in
4036 ((let s = tostr t in
4037 if String.length s > 0
4038 then Printf.sprintf "%s\t%s" name s
4039 else name),
4040 offset)
4042 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4043 let uiohopt =
4044 if not cancel
4045 then (
4046 m_qsearch <- qsearch;
4047 let uioh =
4048 match m_a.(active) with
4049 | _, _, _, Action f -> f uioh
4050 | _ -> uioh
4052 Some uioh
4054 else None
4056 m_active <- active;
4057 m_first <- first;
4058 m_pan <- pan;
4059 uiohopt
4061 method hasaction n =
4062 match m_a.(n) with
4063 | _, _, _, Action _ -> true
4064 | _ -> false
4065 end)
4067 let rec fillsrc prevmode prevuioh =
4068 let sep () = src#caption "" 0 in
4069 let colorp name get set =
4070 src#string name
4071 (fun () -> color_to_string (get ()))
4072 (fun v ->
4074 let c = color_of_string v in
4075 set c
4076 with exn ->
4077 state.text <- Printf.sprintf "bad color `%s': %s"
4078 v (Printexc.to_string exn);
4081 let oldmode = state.mode in
4082 let birdseye = isbirdseye state.mode in
4084 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4086 src#bool "presentation mode"
4087 (fun () -> conf.presentation)
4088 (fun v ->
4089 conf.presentation <- v;
4090 state.anchor <- getanchor ();
4091 represent ());
4093 src#bool "ignore case in searches"
4094 (fun () -> conf.icase)
4095 (fun v -> conf.icase <- v);
4097 src#bool "preload"
4098 (fun () -> conf.preload)
4099 (fun v -> conf.preload <- v);
4101 src#bool "highlight links"
4102 (fun () -> conf.hlinks)
4103 (fun v -> conf.hlinks <- v);
4105 src#bool "under info"
4106 (fun () -> conf.underinfo)
4107 (fun v -> conf.underinfo <- v);
4109 src#bool "persistent bookmarks"
4110 (fun () -> conf.savebmarks)
4111 (fun v -> conf.savebmarks <- v);
4113 src#bool "proportional display"
4114 (fun () -> conf.proportional)
4115 (fun v -> reqlayout conf.angle v);
4117 src#bool "trim margins"
4118 (fun () -> conf.trimmargins)
4119 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4121 src#bool "persistent location"
4122 (fun () -> conf.jumpback)
4123 (fun v -> conf.jumpback <- v);
4125 sep ();
4126 src#int "inter-page space"
4127 (fun () -> conf.interpagespace)
4128 (fun n ->
4129 conf.interpagespace <- n;
4130 docolumns conf.columns;
4131 let pageno, py =
4132 match state.layout with
4133 | [] -> 0, 0
4134 | l :: _ ->
4135 l.pageno, l.pagey
4137 state.maxy <- calcheight ();
4138 let y = getpagey pageno in
4139 gotoy (y + py)
4142 src#int "page bias"
4143 (fun () -> conf.pagebias)
4144 (fun v -> conf.pagebias <- v);
4146 src#int "scroll step"
4147 (fun () -> conf.scrollstep)
4148 (fun n -> conf.scrollstep <- n);
4150 src#int "horizontal scroll step"
4151 (fun () -> conf.hscrollstep)
4152 (fun v -> conf.hscrollstep <- v);
4154 src#int "auto scroll step"
4155 (fun () ->
4156 match state.autoscroll with
4157 | Some step -> step
4158 | _ -> conf.autoscrollstep)
4159 (fun n ->
4160 if state.autoscroll <> None
4161 then state.autoscroll <- Some n;
4162 conf.autoscrollstep <- n);
4164 src#int "zoom"
4165 (fun () -> truncate (conf.zoom *. 100.))
4166 (fun v -> setzoom ((float v) /. 100.));
4168 src#int "rotation"
4169 (fun () -> conf.angle)
4170 (fun v -> reqlayout v conf.proportional);
4172 src#int "scroll bar width"
4173 (fun () -> state.scrollw)
4174 (fun v ->
4175 state.scrollw <- v;
4176 conf.scrollbw <- v;
4177 reshape conf.winw conf.winh;
4180 src#int "scroll handle height"
4181 (fun () -> conf.scrollh)
4182 (fun v -> conf.scrollh <- v;);
4184 src#int "thumbnail width"
4185 (fun () -> conf.thumbw)
4186 (fun v ->
4187 conf.thumbw <- min 4096 v;
4188 match oldmode with
4189 | Birdseye beye ->
4190 leavebirdseye beye false;
4191 enterbirdseye ()
4192 | _ -> ()
4195 let mode = state.mode in
4196 src#string "columns"
4197 (fun () ->
4198 match conf.columns with
4199 | Csingle _ -> "1"
4200 | Cmulti (multi, _) -> multicolumns_to_string multi
4201 | Csplit (count, _) -> "-" ^ string_of_int count
4203 (fun v ->
4204 let n, a, b = multicolumns_of_string v in
4205 setcolumns mode n a b);
4207 sep ();
4208 src#caption "Presentation mode" 0;
4209 src#bool "scrollbar visible"
4210 (fun () -> conf.scrollbarinpm)
4211 (fun v ->
4212 if v != conf.scrollbarinpm
4213 then (
4214 conf.scrollbarinpm <- v;
4215 if conf.presentation
4216 then (
4217 state.scrollw <- if v then conf.scrollbw else 0;
4218 reshape conf.winw conf.winh;
4223 sep ();
4224 src#caption "Pixmap cache" 0;
4225 src#int_with_suffix "size (advisory)"
4226 (fun () -> conf.memlimit)
4227 (fun v -> conf.memlimit <- v);
4229 src#caption2 "used"
4230 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4231 (string_with_suffix_of_int state.memused)
4232 (Hashtbl.length state.tilemap)) 1;
4234 sep ();
4235 src#caption "Layout" 0;
4236 src#caption2 "Dimension"
4237 (fun () ->
4238 Printf.sprintf "%dx%d (virtual %dx%d)"
4239 conf.winw conf.winh
4240 state.w state.maxy)
4242 if conf.debug
4243 then
4244 src#caption2 "Position" (fun () ->
4245 Printf.sprintf "%dx%d" state.x state.y
4247 else
4248 src#caption2 "Visible" (fun () -> describe_location ()) 1
4251 sep ();
4252 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4253 "Save these parameters as global defaults at exit"
4254 (fun () -> conf.bedefault)
4255 (fun v -> conf.bedefault <- v)
4258 sep ();
4259 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4260 src#bool ~offset:0 ~btos "Extended parameters"
4261 (fun () -> !showextended)
4262 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4263 if !showextended
4264 then (
4265 src#bool "checkers"
4266 (fun () -> conf.checkers)
4267 (fun v -> conf.checkers <- v; setcheckers v);
4268 src#bool "update cursor"
4269 (fun () -> conf.updatecurs)
4270 (fun v -> conf.updatecurs <- v);
4271 src#bool "verbose"
4272 (fun () -> conf.verbose)
4273 (fun v -> conf.verbose <- v);
4274 src#bool "invert colors"
4275 (fun () -> conf.invert)
4276 (fun v -> conf.invert <- v);
4277 src#bool "max fit"
4278 (fun () -> conf.maxhfit)
4279 (fun v -> conf.maxhfit <- v);
4280 src#bool "redirect stderr"
4281 (fun () -> conf.redirectstderr)
4282 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4283 src#string "uri launcher"
4284 (fun () -> conf.urilauncher)
4285 (fun v -> conf.urilauncher <- v);
4286 src#string "path launcher"
4287 (fun () -> conf.pathlauncher)
4288 (fun v -> conf.pathlauncher <- v);
4289 src#string "tile size"
4290 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4291 (fun v ->
4293 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4294 conf.tilew <- max 64 w;
4295 conf.tileh <- max 64 h;
4296 flushtiles ();
4297 with exn ->
4298 state.text <- Printf.sprintf "bad tile size `%s': %s"
4299 v (Printexc.to_string exn));
4300 src#int "texture count"
4301 (fun () -> conf.texcount)
4302 (fun v ->
4303 if realloctexts v
4304 then conf.texcount <- v
4305 else showtext '!' " Failed to set texture count please retry later"
4307 src#int "slice height"
4308 (fun () -> conf.sliceheight)
4309 (fun v ->
4310 conf.sliceheight <- v;
4311 wcmd "sliceh %d" conf.sliceheight;
4313 src#int "anti-aliasing level"
4314 (fun () -> conf.aalevel)
4315 (fun v ->
4316 conf.aalevel <- bound v 0 8;
4317 state.anchor <- getanchor ();
4318 opendoc state.path state.password;
4320 src#string "page scroll scaling factor"
4321 (fun () -> string_of_float conf.pgscale)
4322 (fun v ->
4324 let s = float_of_string v in
4325 conf.pgscale <- s
4326 with exn ->
4327 state.text <- Printf.sprintf
4328 "bad page scroll scaling factor `%s': %s"
4329 v (Printexc.to_string exn)
4332 src#int "ui font size"
4333 (fun () -> fstate.fontsize)
4334 (fun v -> setfontsize (bound v 5 100));
4335 src#int "hint font size"
4336 (fun () -> conf.hfsize)
4337 (fun v -> conf.hfsize <- bound v 5 100);
4338 colorp "background color"
4339 (fun () -> conf.bgcolor)
4340 (fun v -> conf.bgcolor <- v);
4341 src#bool "crop hack"
4342 (fun () -> conf.crophack)
4343 (fun v -> conf.crophack <- v);
4344 src#string "trim fuzz"
4345 (fun () -> irect_to_string conf.trimfuzz)
4346 (fun v ->
4348 conf.trimfuzz <- irect_of_string v;
4349 if conf.trimmargins
4350 then settrim true conf.trimfuzz;
4351 with exn ->
4352 state.text <- Printf.sprintf "bad irect `%s': %s"
4353 v (Printexc.to_string exn)
4355 src#string "throttle"
4356 (fun () ->
4357 match conf.maxwait with
4358 | None -> "show place holder if page is not ready"
4359 | Some time ->
4360 if time = infinity
4361 then "wait for page to fully render"
4362 else
4363 "wait " ^ string_of_float time
4364 ^ " seconds before showing placeholder"
4366 (fun v ->
4368 let f = float_of_string v in
4369 if f <= 0.0
4370 then conf.maxwait <- None
4371 else conf.maxwait <- Some f
4372 with exn ->
4373 state.text <- Printf.sprintf "bad time `%s': %s"
4374 v (Printexc.to_string exn)
4376 src#string "ghyll scroll"
4377 (fun () ->
4378 match conf.ghyllscroll with
4379 | None -> ""
4380 | Some nab -> ghyllscroll_to_string nab
4382 (fun v ->
4384 let gs =
4385 if String.length v = 0
4386 then None
4387 else Some (ghyllscroll_of_string v)
4389 conf.ghyllscroll <- gs
4390 with exn ->
4391 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4392 v (Printexc.to_string exn)
4394 src#string "selection command"
4395 (fun () -> conf.selcmd)
4396 (fun v -> conf.selcmd <- v);
4397 src#colorspace "color space"
4398 (fun () -> colorspace_to_string conf.colorspace)
4399 (fun v ->
4400 conf.colorspace <- colorspace_of_int v;
4401 wcmd "cs %d" v;
4402 load state.layout;
4406 sep ();
4407 src#caption "Document" 0;
4408 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4409 src#caption2 "Pages"
4410 (fun () -> string_of_int state.pagecount) 1;
4411 src#caption2 "Dimensions"
4412 (fun () -> string_of_int (List.length state.pdims)) 1;
4413 if conf.trimmargins
4414 then (
4415 sep ();
4416 src#caption "Trimmed margins" 0;
4417 src#caption2 "Dimensions"
4418 (fun () -> string_of_int (List.length state.pdims)) 1;
4421 sep ();
4422 src#caption "OpenGL" 0;
4423 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4424 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4425 src#reset prevmode prevuioh;
4427 fun () ->
4428 state.text <- "";
4429 let prevmode = state.mode
4430 and prevuioh = state.uioh in
4431 fillsrc prevmode prevuioh;
4432 let source = (src :> lvsource) in
4433 let modehash = findkeyhash conf "info" in
4434 state.uioh <- coe (object (self)
4435 inherit listview ~source ~trusted:true ~modehash as super
4436 val mutable m_prevmemused = 0
4437 method infochanged = function
4438 | Memused ->
4439 if m_prevmemused != state.memused
4440 then (
4441 m_prevmemused <- state.memused;
4442 G.postRedisplay "memusedchanged";
4444 | Pdim -> G.postRedisplay "pdimchanged"
4445 | Docinfo -> fillsrc prevmode prevuioh
4447 method key key mask =
4448 if not (Wsi.withctrl mask)
4449 then
4450 match key with
4451 | 0xff51 -> coe (self#updownlevel ~-1)
4452 | 0xff53 -> coe (self#updownlevel 1)
4453 | _ -> super#key key mask
4454 else super#key key mask
4455 end);
4456 G.postRedisplay "info";
4459 let enterhelpmode =
4460 let source =
4461 (object
4462 inherit lvsourcebase
4463 method getitemcount = Array.length state.help
4464 method getitem n =
4465 let s, n, _ = state.help.(n) in
4466 (s, n)
4468 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4469 let optuioh =
4470 if not cancel
4471 then (
4472 m_qsearch <- qsearch;
4473 match state.help.(active) with
4474 | _, _, Action f -> Some (f uioh)
4475 | _ -> Some (uioh)
4477 else None
4479 m_active <- active;
4480 m_first <- first;
4481 m_pan <- pan;
4482 optuioh
4484 method hasaction n =
4485 match state.help.(n) with
4486 | _, _, Action _ -> true
4487 | _ -> false
4489 initializer
4490 m_active <- -1
4491 end)
4492 in fun () ->
4493 let modehash = findkeyhash conf "help" in
4494 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4495 G.postRedisplay "help";
4498 let entermsgsmode =
4499 let msgsource =
4500 let re = Str.regexp "[\r\n]" in
4501 (object
4502 inherit lvsourcebase
4503 val mutable m_items = [||]
4505 method getitemcount = 1 + Array.length m_items
4507 method getitem n =
4508 if n = 0
4509 then "[Clear]", 0
4510 else m_items.(n-1), 0
4512 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4513 ignore uioh;
4514 if not cancel
4515 then (
4516 if active = 0
4517 then Buffer.clear state.errmsgs;
4518 m_qsearch <- qsearch;
4520 m_active <- active;
4521 m_first <- first;
4522 m_pan <- pan;
4523 None
4525 method hasaction n =
4526 n = 0
4528 method reset =
4529 state.newerrmsgs <- false;
4530 let l = Str.split re (Buffer.contents state.errmsgs) in
4531 m_items <- Array.of_list l
4533 initializer
4534 m_active <- 0
4535 end)
4536 in fun () ->
4537 state.text <- "";
4538 msgsource#reset;
4539 let source = (msgsource :> lvsource) in
4540 let modehash = findkeyhash conf "listview" in
4541 state.uioh <- coe (object
4542 inherit listview ~source ~trusted:false ~modehash as super
4543 method display =
4544 if state.newerrmsgs
4545 then msgsource#reset;
4546 super#display
4547 end);
4548 G.postRedisplay "msgs";
4551 let quickbookmark ?title () =
4552 match state.layout with
4553 | [] -> ()
4554 | l :: _ ->
4555 let title =
4556 match title with
4557 | None ->
4558 let sec = Unix.gettimeofday () in
4559 let tm = Unix.localtime sec in
4560 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4561 (l.pageno+1)
4562 tm.Unix.tm_mday
4563 tm.Unix.tm_mon
4564 (tm.Unix.tm_year + 1900)
4565 tm.Unix.tm_hour
4566 tm.Unix.tm_min
4567 | Some title -> title
4569 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4572 let doreshape w h =
4573 state.fullscreen <- None;
4574 Wsi.reshape w h;
4577 let setautoscrollspeed step goingdown =
4578 let incr = max 1 ((abs step) / 2) in
4579 let incr = if goingdown then incr else -incr in
4580 let astep = step + incr in
4581 state.autoscroll <- Some astep;
4584 let gotounder = function
4585 | Ulinkgoto (pageno, top) ->
4586 if pageno >= 0
4587 then (
4588 addnav ();
4589 gotopage1 pageno top;
4592 | Ulinkuri s ->
4593 gotouri s
4595 | Uremote (filename, pageno) ->
4596 let path =
4597 if Sys.file_exists filename
4598 then filename
4599 else
4600 let dir = Filename.dirname state.path in
4601 let path = Filename.concat dir filename in
4602 if Sys.file_exists path
4603 then path
4604 else ""
4606 if String.length path > 0
4607 then (
4608 let anchor = getanchor () in
4609 let ranchor = state.path, state.password, anchor in
4610 state.anchor <- (pageno, 0.0, 0.0);
4611 state.ranchors <- ranchor :: state.ranchors;
4612 opendoc path "";
4614 else showtext '!' ("Could not find " ^ filename)
4616 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4619 let canpan () =
4620 match conf.columns with
4621 | Csplit _ -> true
4622 | _ -> conf.zoom > 1.0
4625 let viewkeyboard key mask =
4626 let enttext te =
4627 let mode = state.mode in
4628 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4629 state.text <- "";
4630 enttext ();
4631 G.postRedisplay "view:enttext"
4633 let ctrl = Wsi.withctrl mask in
4634 match key with
4635 | 81 -> (* Q *)
4636 exit 0
4638 | 0xff63 -> (* insert *)
4639 if conf.angle mod 360 = 0
4640 then (
4641 state.mode <- LinkNav (Ltgendir 0);
4642 gotoy state.y;
4644 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4646 | 0xff1b | 113 -> (* escape / q *)
4647 begin match state.mstate with
4648 | Mzoomrect _ ->
4649 state.mstate <- Mnone;
4650 Wsi.setcursor Wsi.CURSOR_INHERIT;
4651 G.postRedisplay "kill zoom rect";
4652 | _ ->
4653 match state.ranchors with
4654 | [] -> raise Quit
4655 | (path, password, anchor) :: rest ->
4656 state.ranchors <- rest;
4657 state.anchor <- anchor;
4658 opendoc path password
4659 end;
4661 | 0xff08 -> (* backspace *)
4662 let y = getnav ~-1 in
4663 gotoy_and_clear_text y
4665 | 111 -> (* o *)
4666 enteroutlinemode ()
4668 | 117 -> (* u *)
4669 state.rects <- [];
4670 state.text <- "";
4671 G.postRedisplay "dehighlight";
4673 | 47 | 63 -> (* / ? *)
4674 let ondone isforw s =
4675 cbput state.hists.pat s;
4676 state.searchpattern <- s;
4677 search s isforw
4679 let s = String.create 1 in
4680 s.[0] <- Char.chr key;
4681 enttext (s, "", Some (onhist state.hists.pat),
4682 textentry, ondone (key = 47), true)
4684 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4685 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4686 setzoom (conf.zoom +. incr)
4688 | 43 | 0xffab -> (* + *)
4689 let ondone s =
4690 let n =
4691 try int_of_string s with exc ->
4692 state.text <- Printf.sprintf "bad integer `%s': %s"
4693 s (Printexc.to_string exc);
4694 max_int
4696 if n != max_int
4697 then (
4698 conf.pagebias <- n;
4699 state.text <- "page bias is now " ^ string_of_int n;
4702 enttext ("page bias: ", "", None, intentry, ondone, true)
4704 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4705 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4706 setzoom (max 0.01 (conf.zoom -. decr))
4708 | 45 | 0xffad -> (* - *)
4709 let ondone msg = state.text <- msg in
4710 enttext (
4711 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4712 optentry state.mode, ondone, true
4715 | 48 when ctrl -> (* ctrl-0 *)
4716 setzoom 1.0
4718 | 49 when ctrl -> (* ctrl-1 *)
4719 let cols =
4720 match conf.columns with
4721 | Csingle _ | Cmulti _ -> 1
4722 | Csplit (n, _) -> n
4724 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4725 if zoom < 1.0
4726 then setzoom zoom
4728 | 0xffc6 -> (* f9 *)
4729 togglebirdseye ()
4731 | 57 when ctrl -> (* ctrl-9 *)
4732 togglebirdseye ()
4734 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4735 when not ctrl -> (* 0..9 *)
4736 let ondone s =
4737 let n =
4738 try int_of_string s with exc ->
4739 state.text <- Printf.sprintf "bad integer `%s': %s"
4740 s (Printexc.to_string exc);
4743 if n >= 0
4744 then (
4745 addnav ();
4746 cbput state.hists.pag (string_of_int n);
4747 gotopage1 (n + conf.pagebias - 1) 0;
4750 let pageentry text key =
4751 match Char.unsafe_chr key with
4752 | 'g' -> TEdone text
4753 | _ -> intentry text key
4755 let text = "x" in text.[0] <- Char.chr key;
4756 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4758 | 98 -> (* b *)
4759 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4760 reshape conf.winw conf.winh;
4762 | 108 -> (* l *)
4763 conf.hlinks <- not conf.hlinks;
4764 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4765 G.postRedisplay "toggle highlightlinks";
4767 | 70 -> (* F *)
4768 state.glinks <- true;
4769 let mode = state.mode in
4770 state.mode <- Textentry (
4771 (":", "", None, linknentry, linkndone (fun under ->
4772 addnav ();
4773 gotounder under
4774 ), false
4775 ), fun _ ->
4776 state.glinks <- false;
4777 state.mode <- mode
4779 state.text <- "";
4780 G.postRedisplay "view:linkent(F)"
4782 | 121 -> (* y *)
4783 state.glinks <- true;
4784 let mode = state.mode in
4785 state.mode <- Textentry (
4786 (":", "", None, linknentry, linkndone (fun under ->
4787 match Ne.pipe () with
4788 | Ne.Exn exn ->
4789 showtext '!' (Printf.sprintf "pipe failed: %s"
4790 (Printexc.to_string exn));
4791 | Ne.Res (r, w) ->
4792 let popened =
4793 try popen conf.selcmd [r, 0; w, -1]; true
4794 with exn ->
4795 showtext '!'
4796 (Printf.sprintf "failed to execute %s: %s"
4797 conf.selcmd (Printexc.to_string exn));
4798 false
4800 let clo cap fd =
4801 Ne.clo fd (fun msg ->
4802 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4805 let s = undertext under in
4806 if popened
4807 then
4808 (try
4809 let l = String.length s in
4810 let n = Unix.write w s 0 l in
4811 if n != l
4812 then
4813 showtext '!'
4814 (Printf.sprintf
4815 "failed to write %d characters to sel pipe, wrote %d"
4818 with exn ->
4819 showtext '!'
4820 (Printf.sprintf "failed to write to sel pipe: %s"
4821 (Printexc.to_string exn)
4824 else dolog "%s" s;
4825 clo "pipe/r" r;
4826 clo "pipe/w" w;
4827 ), false
4829 fun _ ->
4830 state.glinks <- false;
4831 state.mode <- mode
4833 state.text <- "";
4834 G.postRedisplay "view:linkent"
4836 | 97 -> (* a *)
4837 begin match state.autoscroll with
4838 | Some step ->
4839 conf.autoscrollstep <- step;
4840 state.autoscroll <- None
4841 | None ->
4842 if conf.autoscrollstep = 0
4843 then state.autoscroll <- Some 1
4844 else state.autoscroll <- Some conf.autoscrollstep
4847 | 112 when ctrl -> (* ctrl-p *)
4848 launchpath ()
4850 | 80 -> (* P *)
4851 conf.presentation <- not conf.presentation;
4852 if conf.presentation
4853 then (
4854 if not conf.scrollbarinpm
4855 then state.scrollw <- 0;
4857 else
4858 state.scrollw <- conf.scrollbw;
4860 showtext ' ' ("presentation mode " ^
4861 if conf.presentation then "on" else "off");
4862 state.anchor <- getanchor ();
4863 represent ()
4865 | 102 -> (* f *)
4866 begin match state.fullscreen with
4867 | None ->
4868 state.fullscreen <- Some (conf.winw, conf.winh);
4869 Wsi.fullscreen ()
4870 | Some (w, h) ->
4871 state.fullscreen <- None;
4872 doreshape w h
4875 | 103 -> (* g *)
4876 gotoy_and_clear_text 0
4878 | 71 -> (* G *)
4879 gotopage1 (state.pagecount - 1) 0
4881 | 112 | 78 -> (* p|N *)
4882 search state.searchpattern false
4884 | 110 | 0xffc0 -> (* n|F3 *)
4885 search state.searchpattern true
4887 | 116 -> (* t *)
4888 begin match state.layout with
4889 | [] -> ()
4890 | l :: _ ->
4891 gotoy_and_clear_text (getpagey l.pageno)
4894 | 32 -> (* space *)
4895 begin match state.layout with
4896 | [] -> ()
4897 | l :: rest ->
4898 match conf.columns with
4899 | Csingle _ | Cmulti _ ->
4900 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4901 then
4902 let y = clamp (pgscale conf.winh) in
4903 gotoy_and_clear_text y
4904 else
4905 let pageno = min (l.pageno+1) (state.pagecount-1) in
4906 gotoy_and_clear_text (getpagey pageno)
4907 | Csplit (n, _) ->
4908 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4909 then
4910 let pagey, pageh = getpageyh l.pageno in
4911 let pagey = pagey + pageh * l.pagecol in
4912 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4913 gotoy_and_clear_text (pagey + pageh + ips)
4916 | 0xff9f | 0xffff -> (* delete *)
4917 begin match state.layout with
4918 | [] -> ()
4919 | l :: _ ->
4920 match conf.columns with
4921 | Csingle _ | Cmulti _ ->
4922 if conf.presentation && l.pagey != 0
4923 then
4924 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4925 else
4926 let pageno = max 0 (l.pageno-1) in
4927 gotoy_and_clear_text (getpagey pageno)
4928 | Csplit (n, _) ->
4929 let y =
4930 if l.pagecol = 0
4931 then
4932 if l.pageno = 0
4933 then l.pagey
4934 else
4935 let pageno = max 0 (l.pageno-1) in
4936 let pagey, pageh = getpageyh pageno in
4937 pagey + (n-1)*pageh
4938 else
4939 let pagey, pageh = getpageyh l.pageno in
4940 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4942 gotoy_and_clear_text y
4945 | 61 -> (* = *)
4946 showtext ' ' (describe_location ());
4948 | 119 -> (* w *)
4949 begin match state.layout with
4950 | [] -> ()
4951 | l :: _ ->
4952 doreshape (l.pagew + state.scrollw) l.pageh;
4953 G.postRedisplay "w"
4956 | 39 -> (* ' *)
4957 enterbookmarkmode ()
4959 | 104 | 0xffbe -> (* h|F1 *)
4960 enterhelpmode ()
4962 | 105 -> (* i *)
4963 enterinfomode ()
4965 | 101 when conf.redirectstderr -> (* e *)
4966 entermsgsmode ()
4968 | 109 -> (* m *)
4969 let ondone s =
4970 match state.layout with
4971 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
4972 | _ -> ()
4974 enttext ("bookmark: ", "", None, textentry, ondone, true)
4976 | 126 -> (* ~ *)
4977 quickbookmark ();
4978 showtext ' ' "Quick bookmark added";
4980 | 122 -> (* z *)
4981 begin match state.layout with
4982 | l :: _ ->
4983 let rect = getpdimrect l.pagedimno in
4984 let w, h =
4985 if conf.crophack
4986 then
4987 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4988 truncate (1.2 *. (rect.(3) -. rect.(0))))
4989 else
4990 (truncate (rect.(1) -. rect.(0)),
4991 truncate (rect.(3) -. rect.(0)))
4993 let w = truncate ((float w)*.conf.zoom)
4994 and h = truncate ((float h)*.conf.zoom) in
4995 if w != 0 && h != 0
4996 then (
4997 state.anchor <- getanchor ();
4998 doreshape (w + state.scrollw) (h + conf.interpagespace)
5000 G.postRedisplay "z";
5002 | [] -> ()
5005 | 50 when ctrl -> (* ctrl-2 *)
5006 let maxw = getmaxw () in
5007 if maxw > 0.0
5008 then setzoom (maxw /. float conf.winw)
5010 | 60 | 62 -> (* < > *)
5011 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5013 | 91 | 93 -> (* [ ] *)
5014 conf.colorscale <-
5015 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5017 G.postRedisplay "brightness";
5019 | 99 when state.mode = View -> (* c *)
5020 let (c, a, b), z =
5021 match state.prevcolumns with
5022 | None -> (1, 0, 0), 1.0
5023 | Some (columns, z) ->
5024 let cab =
5025 match columns with
5026 | Csplit (c, _) -> -c, 0, 0
5027 | Cmulti ((c, a, b), _) -> c, a, b
5028 | Csingle _ -> 1, 0, 0
5030 cab, z
5032 setcolumns View c a b;
5033 setzoom z;
5035 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5036 setzoom state.prevzoom
5038 | 107 | 0xff52 -> (* k up *)
5039 begin match state.autoscroll with
5040 | None ->
5041 begin match state.mode with
5042 | Birdseye beye -> upbirdseye 1 beye
5043 | _ ->
5044 if ctrl
5045 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5046 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5048 | Some n ->
5049 setautoscrollspeed n false
5052 | 106 | 0xff54 -> (* j down *)
5053 begin match state.autoscroll with
5054 | None ->
5055 begin match state.mode with
5056 | Birdseye beye -> downbirdseye 1 beye
5057 | _ ->
5058 if ctrl
5059 then gotoy_and_clear_text (clamp (conf.winh/2))
5060 else gotoy_and_clear_text (clamp conf.scrollstep)
5062 | Some n ->
5063 setautoscrollspeed n true
5066 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5067 if canpan ()
5068 then
5069 let dx =
5070 if ctrl
5071 then conf.winw / 2
5072 else 10
5074 let dx = if key = 0xff51 then dx else -dx in
5075 state.x <- state.x + dx;
5076 gotoy_and_clear_text state.y
5077 else (
5078 state.text <- "";
5079 G.postRedisplay "lef/right"
5082 | 0xff55 -> (* prior *)
5083 let y =
5084 if ctrl
5085 then
5086 match state.layout with
5087 | [] -> state.y
5088 | l :: _ -> state.y - l.pagey
5089 else
5090 clamp (pgscale (-conf.winh))
5092 gotoghyll y
5094 | 0xff56 -> (* next *)
5095 let y =
5096 if ctrl
5097 then
5098 match List.rev state.layout with
5099 | [] -> state.y
5100 | l :: _ -> getpagey l.pageno
5101 else
5102 clamp (pgscale conf.winh)
5104 gotoghyll y
5106 | 0xff50 -> (* home *)
5107 gotoghyll 0
5108 | 0xff57 -> (* end *)
5109 gotoghyll (clamp state.maxy)
5110 | 0xff53 when Wsi.withalt mask -> (* right *)
5111 gotoghyll (getnav ~-1)
5112 | 0xff51 when Wsi.withalt mask -> (* left *)
5113 gotoghyll (getnav 1)
5115 | 114 -> (* r *)
5116 state.anchor <- getanchor ();
5117 opendoc state.path state.password
5119 | 118 when conf.debug -> (* v *)
5120 state.rects <- [];
5121 List.iter (fun l ->
5122 match getopaque l.pageno with
5123 | None -> ()
5124 | Some opaque ->
5125 let x0, y0, x1, y1 = pagebbox opaque in
5126 let a,b = float x0, float y0 in
5127 let c,d = float x1, float y0 in
5128 let e,f = float x1, float y1 in
5129 let h,j = float x0, float y1 in
5130 let rect = (a,b,c,d,e,f,h,j) in
5131 debugrect rect;
5132 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5133 ) state.layout;
5134 G.postRedisplay "v";
5136 | _ ->
5137 vlog "huh? %s" (Wsi.keyname key)
5140 let linknavkeyboard key mask linknav =
5141 let getpage pageno =
5142 let rec loop = function
5143 | [] -> None
5144 | l :: _ when l.pageno = pageno -> Some l
5145 | _ :: rest -> loop rest
5146 in loop state.layout
5148 let doexact (pageno, n) =
5149 match getopaque pageno, getpage pageno with
5150 | Some opaque, Some l ->
5151 if key = 0xff0d
5152 then
5153 let under = getlink opaque n in
5154 G.postRedisplay "link gotounder";
5155 gotounder under;
5156 state.mode <- View;
5157 else
5158 let opt, dir =
5159 match key with
5160 | 0xff50 -> (* home *)
5161 Some (findlink opaque LDfirst), -1
5163 | 0xff57 -> (* end *)
5164 Some (findlink opaque LDlast), 1
5166 | 0xff51 -> (* left *)
5167 Some (findlink opaque (LDleft n)), -1
5169 | 0xff53 -> (* right *)
5170 Some (findlink opaque (LDright n)), 1
5172 | 0xff52 -> (* up *)
5173 Some (findlink opaque (LDup n)), -1
5175 | 0xff54 -> (* down *)
5176 Some (findlink opaque (LDdown n)), 1
5178 | _ -> None, 0
5180 let pwl l dir =
5181 begin match findpwl l.pageno dir with
5182 | Pwlnotfound -> ()
5183 | Pwl pageno ->
5184 let notfound dir =
5185 state.mode <- LinkNav (Ltgendir dir);
5186 let y, h = getpageyh pageno in
5187 let y =
5188 if dir < 0
5189 then y + h - conf.winh
5190 else y
5192 gotoy y
5194 begin match getopaque pageno, getpage pageno with
5195 | Some opaque, Some _ ->
5196 let link =
5197 let ld = if dir > 0 then LDfirst else LDlast in
5198 findlink opaque ld
5200 begin match link with
5201 | Lfound m ->
5202 showlinktype (getlink opaque m);
5203 state.mode <- LinkNav (Ltexact (pageno, m));
5204 G.postRedisplay "linknav jpage";
5205 | _ -> notfound dir
5206 end;
5207 | _ -> notfound dir
5208 end;
5209 end;
5211 begin match opt with
5212 | Some Lnotfound -> pwl l dir;
5213 | Some (Lfound m) ->
5214 if m = n
5215 then pwl l dir
5216 else (
5217 let _, y0, _, y1 = getlinkrect opaque m in
5218 if y0 < l.pagey
5219 then gotopage1 l.pageno y0
5220 else (
5221 let d = fstate.fontsize + 1 in
5222 if y1 - l.pagey > l.pagevh - d
5223 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5224 else G.postRedisplay "linknav";
5226 showlinktype (getlink opaque m);
5227 state.mode <- LinkNav (Ltexact (l.pageno, m));
5230 | None -> viewkeyboard key mask
5231 end;
5232 | _ -> viewkeyboard key mask
5234 if key = 0xff63
5235 then (
5236 state.mode <- View;
5237 G.postRedisplay "leave linknav"
5239 else
5240 match linknav with
5241 | Ltgendir _ -> viewkeyboard key mask
5242 | Ltexact exact -> doexact exact
5245 let keyboard key mask =
5246 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5247 then wcmd "interrupt"
5248 else state.uioh <- state.uioh#key key mask
5251 let birdseyekeyboard key mask
5252 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5253 let incr =
5254 match conf.columns with
5255 | Csingle _ -> 1
5256 | Cmulti ((c, _, _), _) -> c
5257 | Csplit _ -> failwith "bird's eye split mode"
5259 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5260 match key with
5261 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5262 let y, h = getpageyh pageno in
5263 let top = (conf.winh - h) / 2 in
5264 gotoy (max 0 (y - top))
5265 | 0xff0d -> leavebirdseye beye false
5266 | 0xff1b -> leavebirdseye beye true (* escape *)
5267 | 0xff52 -> upbirdseye incr beye (* up *)
5268 | 0xff54 -> downbirdseye incr beye (* down *)
5269 | 0xff51 -> upbirdseye 1 beye (* left *)
5270 | 0xff53 -> downbirdseye 1 beye (* right *)
5272 | 0xff55 -> (* prior *)
5273 begin match state.layout with
5274 | l :: _ ->
5275 if l.pagey != 0
5276 then (
5277 state.mode <- Birdseye (
5278 oconf, leftx, l.pageno, hooverpageno, anchor
5280 gotopage1 l.pageno 0;
5282 else (
5283 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5284 match layout with
5285 | [] -> gotoy (clamp (-conf.winh))
5286 | l :: _ ->
5287 state.mode <- Birdseye (
5288 oconf, leftx, l.pageno, hooverpageno, anchor
5290 gotopage1 l.pageno 0
5293 | [] -> gotoy (clamp (-conf.winh))
5294 end;
5296 | 0xff56 -> (* next *)
5297 begin match List.rev state.layout with
5298 | l :: _ ->
5299 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5300 begin match layout with
5301 | [] ->
5302 let incr = l.pageh - l.pagevh in
5303 if incr = 0
5304 then (
5305 state.mode <-
5306 Birdseye (
5307 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5309 G.postRedisplay "birdseye pagedown";
5311 else gotoy (clamp (incr + conf.interpagespace*2));
5313 | l :: _ ->
5314 state.mode <-
5315 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5316 gotopage1 l.pageno 0;
5319 | [] -> gotoy (clamp conf.winh)
5320 end;
5322 | 0xff50 -> (* home *)
5323 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5324 gotopage1 0 0
5326 | 0xff57 -> (* end *)
5327 let pageno = state.pagecount - 1 in
5328 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5329 if not (pagevisible state.layout pageno)
5330 then
5331 let h =
5332 match List.rev state.pdims with
5333 | [] -> conf.winh
5334 | (_, _, h, _) :: _ -> h
5336 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5337 else G.postRedisplay "birdseye end";
5338 | _ -> viewkeyboard key mask
5341 let drawpage l linkindexbase =
5342 let color =
5343 match state.mode with
5344 | Textentry _ -> scalecolor 0.4
5345 | LinkNav _
5346 | View -> scalecolor 1.0
5347 | Birdseye (_, _, pageno, hooverpageno, _) ->
5348 if l.pageno = hooverpageno
5349 then scalecolor 0.9
5350 else (
5351 if l.pageno = pageno
5352 then scalecolor 1.0
5353 else scalecolor 0.8
5356 drawtiles l color;
5357 begin match getopaque l.pageno with
5358 | Some opaque ->
5359 if tileready l l.pagex l.pagey
5360 then
5361 let x = l.pagedispx - l.pagex
5362 and y = l.pagedispy - l.pagey in
5363 let hlmask =
5364 match conf.columns with
5365 | Csingle _ | Cmulti _ ->
5366 (if conf.hlinks then 1 else 0)
5367 + (if state.glinks
5368 && not (isbirdseye state.mode) then 2 else 0)
5369 | _ -> 0
5371 let s =
5372 match state.mode with
5373 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5374 | _ -> ""
5376 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5377 else 0
5379 | _ -> 0
5380 end;
5383 let scrollindicator () =
5384 let sbw, ph, sh = state.uioh#scrollph in
5385 let sbh, pw, sw = state.uioh#scrollpw in
5387 GlDraw.color (0.64, 0.64, 0.64);
5388 GlDraw.rect
5389 (float (conf.winw - sbw), 0.)
5390 (float conf.winw, float conf.winh)
5392 GlDraw.rect
5393 (0., float (conf.winh - sbh))
5394 (float (conf.winw - state.scrollw - 1), float conf.winh)
5396 GlDraw.color (0.0, 0.0, 0.0);
5398 GlDraw.rect
5399 (float (conf.winw - sbw), ph)
5400 (float conf.winw, ph +. sh)
5402 GlDraw.rect
5403 (pw, float (conf.winh - sbh))
5404 (pw +. sw, float conf.winh)
5408 let showsel () =
5409 match state.mstate with
5410 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5413 | Msel ((x0, y0), (x1, y1)) ->
5414 let rec loop = function
5415 | l :: ls ->
5416 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5417 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5418 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5419 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5420 then
5421 match getopaque l.pageno with
5422 | Some opaque ->
5423 let x0, y0 = pagetranslatepoint l x0 y0 in
5424 let x1, y1 = pagetranslatepoint l x1 y1 in
5425 seltext opaque (x0, y0, x1, y1);
5426 | _ -> ()
5427 else loop ls
5428 | [] -> ()
5430 loop state.layout
5433 let showrects rects =
5434 Gl.enable `blend;
5435 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5436 GlDraw.polygon_mode `both `fill;
5437 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5438 List.iter
5439 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5440 List.iter (fun l ->
5441 if l.pageno = pageno
5442 then (
5443 let dx = float (l.pagedispx - l.pagex) in
5444 let dy = float (l.pagedispy - l.pagey) in
5445 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5446 GlDraw.begins `quads;
5448 GlDraw.vertex2 (x0+.dx, y0+.dy);
5449 GlDraw.vertex2 (x1+.dx, y1+.dy);
5450 GlDraw.vertex2 (x2+.dx, y2+.dy);
5451 GlDraw.vertex2 (x3+.dx, y3+.dy);
5453 GlDraw.ends ();
5455 ) state.layout
5456 ) rects
5458 Gl.disable `blend;
5461 let display () =
5462 GlClear.color (scalecolor2 conf.bgcolor);
5463 GlClear.clear [`color];
5464 let rec loop linkindexbase = function
5465 | l :: rest ->
5466 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5467 loop linkindexbase rest
5468 | [] -> ()
5470 loop 0 state.layout;
5471 let rects =
5472 match state.mode with
5473 | LinkNav (Ltexact (pageno, linkno)) ->
5474 begin match getopaque pageno with
5475 | Some opaque ->
5476 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5477 (pageno, 5, (
5478 float x0, float y0,
5479 float x1, float y0,
5480 float x1, float y1,
5481 float x0, float y1)
5482 ) :: state.rects
5483 | None -> state.rects
5485 | _ -> state.rects
5487 showrects rects;
5488 showsel ();
5489 state.uioh#display;
5490 begin match state.mstate with
5491 | Mzoomrect ((x0, y0), (x1, y1)) ->
5492 Gl.enable `blend;
5493 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5494 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5495 GlDraw.rect (float x0, float y0)
5496 (float x1, float y1);
5497 Gl.disable `blend;
5498 | _ -> ()
5499 end;
5500 enttext ();
5501 scrollindicator ();
5502 Wsi.swapb ();
5505 let zoomrect x y x1 y1 =
5506 let x0 = min x x1
5507 and x1 = max x x1
5508 and y0 = min y y1 in
5509 gotoy (state.y + y0);
5510 state.anchor <- getanchor ();
5511 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5512 let margin =
5513 if state.w < conf.winw - state.scrollw
5514 then (conf.winw - state.scrollw - state.w) / 2
5515 else 0
5517 state.x <- (state.x + margin) - x0;
5518 setzoom zoom;
5519 Wsi.setcursor Wsi.CURSOR_INHERIT;
5520 state.mstate <- Mnone;
5523 let scrollx x =
5524 let winw = conf.winw - state.scrollw - 1 in
5525 let s = float x /. float winw in
5526 let destx = truncate (float (state.w + winw) *. s) in
5527 state.x <- winw - destx;
5528 gotoy_and_clear_text state.y;
5529 state.mstate <- Mscrollx;
5532 let scrolly y =
5533 let s = float y /. float conf.winh in
5534 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5535 gotoy_and_clear_text desty;
5536 state.mstate <- Mscrolly;
5539 let viewmouse button down x y mask =
5540 match button with
5541 | n when (n == 4 || n == 5) && not down ->
5542 if Wsi.withctrl mask
5543 then (
5544 match state.mstate with
5545 | Mzoom (oldn, i) ->
5546 if oldn = n
5547 then (
5548 if i = 2
5549 then
5550 let incr =
5551 match n with
5552 | 5 ->
5553 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5554 | _ ->
5555 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5557 let zoom = conf.zoom -. incr in
5558 setzoom zoom;
5559 state.mstate <- Mzoom (n, 0);
5560 else
5561 state.mstate <- Mzoom (n, i+1);
5563 else state.mstate <- Mzoom (n, 0)
5565 | _ -> state.mstate <- Mzoom (n, 0)
5567 else (
5568 match state.autoscroll with
5569 | Some step -> setautoscrollspeed step (n=4)
5570 | None ->
5571 let incr =
5572 if n = 4
5573 then -conf.scrollstep
5574 else conf.scrollstep
5576 let incr = incr * 2 in
5577 let y = clamp incr in
5578 gotoy_and_clear_text y
5581 | n when (n = 6 || n = 7) && not down && canpan () ->
5582 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5583 gotoy_and_clear_text state.y
5585 | 1 when Wsi.withctrl mask ->
5586 if down
5587 then (
5588 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5589 state.mstate <- Mpan (x, y)
5591 else
5592 state.mstate <- Mnone
5594 | 3 ->
5595 if down
5596 then (
5597 Wsi.setcursor Wsi.CURSOR_CYCLE;
5598 let p = (x, y) in
5599 state.mstate <- Mzoomrect (p, p)
5601 else (
5602 match state.mstate with
5603 | Mzoomrect ((x0, y0), _) ->
5604 if abs (x-x0) > 10 && abs (y - y0) > 10
5605 then zoomrect x0 y0 x y
5606 else (
5607 state.mstate <- Mnone;
5608 Wsi.setcursor Wsi.CURSOR_INHERIT;
5609 G.postRedisplay "kill accidental zoom rect";
5611 | _ ->
5612 Wsi.setcursor Wsi.CURSOR_INHERIT;
5613 state.mstate <- Mnone
5616 | 1 when x > conf.winw - state.scrollw ->
5617 if down
5618 then
5619 let _, position, sh = state.uioh#scrollph in
5620 if y > truncate position && y < truncate (position +. sh)
5621 then state.mstate <- Mscrolly
5622 else scrolly y
5623 else
5624 state.mstate <- Mnone
5626 | 1 when y > conf.winh - state.hscrollh ->
5627 if down
5628 then
5629 let _, position, sw = state.uioh#scrollpw in
5630 if x > truncate position && x < truncate (position +. sw)
5631 then state.mstate <- Mscrollx
5632 else scrollx x
5633 else
5634 state.mstate <- Mnone
5636 | 1 ->
5637 let dest = if down then getunder x y else Unone in
5638 begin match dest with
5639 | Ulinkgoto _
5640 | Ulinkuri _
5641 | Uremote _
5642 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5643 gotounder dest
5645 | Unone when down ->
5646 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5647 state.mstate <- Mpan (x, y);
5649 | Unone | Utext _ ->
5650 if down
5651 then (
5652 if conf.angle mod 360 = 0
5653 then (
5654 state.mstate <- Msel ((x, y), (x, y));
5655 G.postRedisplay "mouse select";
5658 else (
5659 match state.mstate with
5660 | Mnone -> ()
5662 | Mzoom _ | Mscrollx | Mscrolly ->
5663 state.mstate <- Mnone
5665 | Mzoomrect ((x0, y0), _) ->
5666 zoomrect x0 y0 x y
5668 | Mpan _ ->
5669 Wsi.setcursor Wsi.CURSOR_INHERIT;
5670 state.mstate <- Mnone
5672 | Msel ((_, y0), (_, y1)) ->
5673 let rec loop = function
5674 | [] -> ()
5675 | l :: rest ->
5676 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5677 || ((y1 >= l.pagedispy
5678 && y1 <= (l.pagedispy + l.pagevh)))
5679 then
5680 match getopaque l.pageno with
5681 | Some opaque ->
5682 begin
5683 match Ne.pipe () with
5684 | Ne.Exn exn ->
5685 showtext '!'
5686 (Printf.sprintf
5687 "can not create sel pipe: %s"
5688 (Printexc.to_string exn));
5689 | Ne.Res (r, w) ->
5690 let doclose what fd =
5691 Ne.clo fd (fun msg ->
5692 dolog "%s close failed: %s" what msg)
5695 popen conf.selcmd [r, 0; w, -1];
5696 copysel w opaque;
5697 doclose "pipe/r" r;
5698 G.postRedisplay "copysel";
5699 with exn ->
5700 dolog "can not execute %S: %s"
5701 conf.selcmd (Printexc.to_string exn);
5702 doclose "pipe/r" r;
5703 doclose "pipe/w" w;
5705 | None -> ()
5706 else loop rest
5708 loop state.layout;
5709 Wsi.setcursor Wsi.CURSOR_INHERIT;
5710 state.mstate <- Mnone;
5714 | _ -> ()
5717 let birdseyemouse button down x y mask
5718 (conf, leftx, _, hooverpageno, anchor) =
5719 match button with
5720 | 1 when down ->
5721 let rec loop = function
5722 | [] -> ()
5723 | l :: rest ->
5724 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5725 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5726 then (
5727 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5729 else loop rest
5731 loop state.layout
5732 | 3 -> ()
5733 | _ -> viewmouse button down x y mask
5736 let mouse button down x y mask =
5737 state.uioh <- state.uioh#button button down x y mask;
5740 let motion ~x ~y =
5741 state.uioh <- state.uioh#motion x y
5744 let pmotion ~x ~y =
5745 state.uioh <- state.uioh#pmotion x y;
5748 let uioh = object
5749 method display = ()
5751 method key key mask =
5752 begin match state.mode with
5753 | Textentry textentry -> textentrykeyboard key mask textentry
5754 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5755 | View -> viewkeyboard key mask
5756 | LinkNav linknav -> linknavkeyboard key mask linknav
5757 end;
5758 state.uioh
5760 method button button bstate x y mask =
5761 begin match state.mode with
5762 | LinkNav _
5763 | View -> viewmouse button bstate x y mask
5764 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5765 | Textentry _ -> ()
5766 end;
5767 state.uioh
5769 method motion x y =
5770 begin match state.mode with
5771 | Textentry _ -> ()
5772 | View | Birdseye _ | LinkNav _ ->
5773 match state.mstate with
5774 | Mzoom _ | Mnone -> ()
5776 | Mpan (x0, y0) ->
5777 let dx = x - x0
5778 and dy = y0 - y in
5779 state.mstate <- Mpan (x, y);
5780 if canpan ()
5781 then state.x <- state.x + dx;
5782 let y = clamp dy in
5783 gotoy_and_clear_text y
5785 | Msel (a, _) ->
5786 state.mstate <- Msel (a, (x, y));
5787 G.postRedisplay "motion select";
5789 | Mscrolly ->
5790 let y = min conf.winh (max 0 y) in
5791 scrolly y
5793 | Mscrollx ->
5794 let x = min conf.winw (max 0 x) in
5795 scrollx x
5797 | Mzoomrect (p0, _) ->
5798 state.mstate <- Mzoomrect (p0, (x, y));
5799 G.postRedisplay "motion zoomrect";
5800 end;
5801 state.uioh
5803 method pmotion x y =
5804 begin match state.mode with
5805 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5806 let rec loop = function
5807 | [] ->
5808 if hooverpageno != -1
5809 then (
5810 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5811 G.postRedisplay "pmotion birdseye no hoover";
5813 | l :: rest ->
5814 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5815 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5816 then (
5817 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5818 G.postRedisplay "pmotion birdseye hoover";
5820 else loop rest
5822 loop state.layout
5824 | Textentry _ -> ()
5826 | LinkNav _
5827 | View ->
5828 match state.mstate with
5829 | Mnone -> updateunder x y
5830 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5832 end;
5833 state.uioh
5835 method infochanged _ = ()
5837 method scrollph =
5838 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5839 let p, h = scrollph state.y maxy in
5840 state.scrollw, p, h
5842 method scrollpw =
5843 let winw = conf.winw - state.scrollw - 1 in
5844 let fwinw = float winw in
5845 let sw =
5846 let sw = fwinw /. float state.w in
5847 let sw = fwinw *. sw in
5848 max sw (float conf.scrollh)
5850 let position, sw =
5851 let f = state.w+winw in
5852 let r = float (winw-state.x) /. float f in
5853 let p = fwinw *. r in
5854 p-.sw/.2., sw
5856 let sw =
5857 if position +. sw > fwinw
5858 then fwinw -. position
5859 else sw
5861 state.hscrollh, position, sw
5863 method modehash =
5864 let modename =
5865 match state.mode with
5866 | LinkNav _ -> "links"
5867 | Textentry _ -> "textentry"
5868 | Birdseye _ -> "birdseye"
5869 | View -> "view"
5871 findkeyhash conf modename
5872 end;;
5874 module Config =
5875 struct
5876 open Parser
5878 let fontpath = ref "";;
5880 module KeyMap =
5881 Map.Make (struct type t = (int * int) let compare = compare end);;
5883 let unent s =
5884 let l = String.length s in
5885 let b = Buffer.create l in
5886 unent b s 0 l;
5887 Buffer.contents b;
5890 let home =
5891 try Sys.getenv "HOME"
5892 with exn ->
5893 prerr_endline
5894 ("Can not determine home directory location: " ^
5895 Printexc.to_string exn);
5899 let modifier_of_string = function
5900 | "alt" -> Wsi.altmask
5901 | "shift" -> Wsi.shiftmask
5902 | "ctrl" | "control" -> Wsi.ctrlmask
5903 | "meta" -> Wsi.metamask
5904 | _ -> 0
5907 let key_of_string =
5908 let r = Str.regexp "-" in
5909 fun s ->
5910 let elems = Str.full_split r s in
5911 let f n k m =
5912 let g s =
5913 let m1 = modifier_of_string s in
5914 if m1 = 0
5915 then (Wsi.namekey s, m)
5916 else (k, m lor m1)
5917 in function
5918 | Str.Delim s when n land 1 = 0 -> g s
5919 | Str.Text s -> g s
5920 | Str.Delim _ -> (k, m)
5922 let rec loop n k m = function
5923 | [] -> (k, m)
5924 | x :: xs ->
5925 let k, m = f n k m x in
5926 loop (n+1) k m xs
5928 loop 0 0 0 elems
5931 let keys_of_string =
5932 let r = Str.regexp "[ \t]" in
5933 fun s ->
5934 let elems = Str.split r s in
5935 List.map key_of_string elems
5938 let copykeyhashes c =
5939 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5942 let config_of c attrs =
5943 let apply c k v =
5945 match k with
5946 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5947 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5948 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5949 | "preload" -> { c with preload = bool_of_string v }
5950 | "page-bias" -> { c with pagebias = int_of_string v }
5951 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5952 | "horizontal-scroll-step" ->
5953 { c with hscrollstep = max (int_of_string v) 1 }
5954 | "auto-scroll-step" ->
5955 { c with autoscrollstep = max 0 (int_of_string v) }
5956 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5957 | "crop-hack" -> { c with crophack = bool_of_string v }
5958 | "throttle" ->
5959 let mw =
5960 match String.lowercase v with
5961 | "true" -> Some infinity
5962 | "false" -> None
5963 | f -> Some (float_of_string f)
5965 { c with maxwait = mw}
5966 | "highlight-links" -> { c with hlinks = bool_of_string v }
5967 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5968 | "vertical-margin" ->
5969 { c with interpagespace = max 0 (int_of_string v) }
5970 | "zoom" ->
5971 let zoom = float_of_string v /. 100. in
5972 let zoom = max zoom 0.0 in
5973 { c with zoom = zoom }
5974 | "presentation" -> { c with presentation = bool_of_string v }
5975 | "rotation-angle" -> { c with angle = int_of_string v }
5976 | "width" -> { c with winw = max 20 (int_of_string v) }
5977 | "height" -> { c with winh = max 20 (int_of_string v) }
5978 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5979 | "proportional-display" -> { c with proportional = bool_of_string v }
5980 | "pixmap-cache-size" ->
5981 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5982 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5983 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5984 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5985 | "persistent-location" -> { c with jumpback = bool_of_string v }
5986 | "background-color" -> { c with bgcolor = color_of_string v }
5987 | "scrollbar-in-presentation" ->
5988 { c with scrollbarinpm = bool_of_string v }
5989 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5990 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5991 | "mupdf-store-size" ->
5992 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5993 | "checkers" -> { c with checkers = bool_of_string v }
5994 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5995 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5996 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5997 | "uri-launcher" -> { c with urilauncher = unent v }
5998 | "path-launcher" -> { c with pathlauncher = unent v }
5999 | "color-space" -> { c with colorspace = colorspace_of_string v }
6000 | "invert-colors" -> { c with invert = bool_of_string v }
6001 | "brightness" -> { c with colorscale = float_of_string v }
6002 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6003 | "ghyllscroll" ->
6004 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6005 | "columns" ->
6006 let (n, _, _) as nab = multicolumns_of_string v in
6007 if n < 0
6008 then { c with columns = Csplit (-n, [||]) }
6009 else { c with columns = Cmulti (nab, [||]) }
6010 | "birds-eye-columns" ->
6011 { c with beyecolumns = Some (max (int_of_string v) 2) }
6012 | "selection-command" -> { c with selcmd = unent v }
6013 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6014 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6015 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6016 | _ -> c
6017 with exn ->
6018 prerr_endline ("Error processing attribute (`" ^
6019 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6022 let rec fold c = function
6023 | [] -> c
6024 | (k, v) :: rest ->
6025 let c = apply c k v in
6026 fold c rest
6028 fold { c with keyhashes = copykeyhashes c } attrs;
6031 let fromstring f pos n v d =
6032 try f v
6033 with exn ->
6034 dolog "Error processing attribute (%S=%S) at %d\n%s"
6035 n v pos (Printexc.to_string exn)
6040 let bookmark_of attrs =
6041 let rec fold title page rely visy = function
6042 | ("title", v) :: rest -> fold v page rely visy rest
6043 | ("page", v) :: rest -> fold title v rely visy rest
6044 | ("rely", v) :: rest -> fold title page v visy rest
6045 | ("visy", v) :: rest -> fold title page rely v rest
6046 | _ :: rest -> fold title page rely visy rest
6047 | [] -> title, page, rely, visy
6049 fold "invalid" "0" "0" "0" attrs
6052 let doc_of attrs =
6053 let rec fold path page rely pan visy = function
6054 | ("path", v) :: rest -> fold v page rely pan visy rest
6055 | ("page", v) :: rest -> fold path v rely pan visy rest
6056 | ("rely", v) :: rest -> fold path page v pan visy rest
6057 | ("pan", v) :: rest -> fold path page rely v visy rest
6058 | ("visy", v) :: rest -> fold path page rely pan v rest
6059 | _ :: rest -> fold path page rely pan visy rest
6060 | [] -> path, page, rely, pan, visy
6062 fold "" "0" "0" "0" "0" attrs
6065 let map_of attrs =
6066 let rec fold rs ls = function
6067 | ("out", v) :: rest -> fold v ls rest
6068 | ("in", v) :: rest -> fold rs v rest
6069 | _ :: rest -> fold ls rs rest
6070 | [] -> ls, rs
6072 fold "" "" attrs
6075 let setconf dst src =
6076 dst.scrollbw <- src.scrollbw;
6077 dst.scrollh <- src.scrollh;
6078 dst.icase <- src.icase;
6079 dst.preload <- src.preload;
6080 dst.pagebias <- src.pagebias;
6081 dst.verbose <- src.verbose;
6082 dst.scrollstep <- src.scrollstep;
6083 dst.maxhfit <- src.maxhfit;
6084 dst.crophack <- src.crophack;
6085 dst.autoscrollstep <- src.autoscrollstep;
6086 dst.maxwait <- src.maxwait;
6087 dst.hlinks <- src.hlinks;
6088 dst.underinfo <- src.underinfo;
6089 dst.interpagespace <- src.interpagespace;
6090 dst.zoom <- src.zoom;
6091 dst.presentation <- src.presentation;
6092 dst.angle <- src.angle;
6093 dst.winw <- src.winw;
6094 dst.winh <- src.winh;
6095 dst.savebmarks <- src.savebmarks;
6096 dst.memlimit <- src.memlimit;
6097 dst.proportional <- src.proportional;
6098 dst.texcount <- src.texcount;
6099 dst.sliceheight <- src.sliceheight;
6100 dst.thumbw <- src.thumbw;
6101 dst.jumpback <- src.jumpback;
6102 dst.bgcolor <- src.bgcolor;
6103 dst.scrollbarinpm <- src.scrollbarinpm;
6104 dst.tilew <- src.tilew;
6105 dst.tileh <- src.tileh;
6106 dst.mustoresize <- src.mustoresize;
6107 dst.checkers <- src.checkers;
6108 dst.aalevel <- src.aalevel;
6109 dst.trimmargins <- src.trimmargins;
6110 dst.trimfuzz <- src.trimfuzz;
6111 dst.urilauncher <- src.urilauncher;
6112 dst.colorspace <- src.colorspace;
6113 dst.invert <- src.invert;
6114 dst.colorscale <- src.colorscale;
6115 dst.redirectstderr <- src.redirectstderr;
6116 dst.ghyllscroll <- src.ghyllscroll;
6117 dst.columns <- src.columns;
6118 dst.beyecolumns <- src.beyecolumns;
6119 dst.selcmd <- src.selcmd;
6120 dst.updatecurs <- src.updatecurs;
6121 dst.pathlauncher <- src.pathlauncher;
6122 dst.keyhashes <- copykeyhashes src;
6123 dst.hfsize <- src.hfsize;
6124 dst.hscrollstep <- src.hscrollstep;
6125 dst.pgscale <- src.pgscale;
6128 let get s =
6129 let h = Hashtbl.create 10 in
6130 let dc = { defconf with angle = defconf.angle } in
6131 let rec toplevel v t spos _ =
6132 match t with
6133 | Vdata | Vcdata | Vend -> v
6134 | Vopen ("llppconfig", _, closed) ->
6135 if closed
6136 then v
6137 else { v with f = llppconfig }
6138 | Vopen _ ->
6139 error "unexpected subelement at top level" s spos
6140 | Vclose _ -> error "unexpected close at top level" s spos
6142 and llppconfig v t spos _ =
6143 match t with
6144 | Vdata | Vcdata -> v
6145 | Vend -> error "unexpected end of input in llppconfig" s spos
6146 | Vopen ("defaults", attrs, closed) ->
6147 let c = config_of dc attrs in
6148 setconf dc c;
6149 if closed
6150 then v
6151 else { v with f = defaults }
6153 | Vopen ("ui-font", attrs, closed) ->
6154 let rec getsize size = function
6155 | [] -> size
6156 | ("size", v) :: rest ->
6157 let size =
6158 fromstring int_of_string spos "size" v fstate.fontsize in
6159 getsize size rest
6160 | l -> getsize size l
6162 fstate.fontsize <- getsize fstate.fontsize attrs;
6163 if closed
6164 then v
6165 else { v with f = uifont (Buffer.create 10) }
6167 | Vopen ("doc", attrs, closed) ->
6168 let pathent, spage, srely, span, svisy = doc_of attrs in
6169 let path = unent pathent
6170 and pageno = fromstring int_of_string spos "page" spage 0
6171 and rely = fromstring float_of_string spos "rely" srely 0.0
6172 and pan = fromstring int_of_string spos "pan" span 0
6173 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6174 let c = config_of dc attrs in
6175 let anchor = (pageno, rely, visy) in
6176 if closed
6177 then (Hashtbl.add h path (c, [], pan, anchor); v)
6178 else { v with f = doc path pan anchor c [] }
6180 | Vopen _ ->
6181 error "unexpected subelement in llppconfig" s spos
6183 | Vclose "llppconfig" -> { v with f = toplevel }
6184 | Vclose _ -> error "unexpected close in llppconfig" s spos
6186 and defaults v t spos _ =
6187 match t with
6188 | Vdata | Vcdata -> v
6189 | Vend -> error "unexpected end of input in defaults" s spos
6190 | Vopen ("keymap", attrs, closed) ->
6191 let modename =
6192 try List.assoc "mode" attrs
6193 with Not_found -> "global" in
6194 if closed
6195 then v
6196 else
6197 let ret keymap =
6198 let h = findkeyhash dc modename in
6199 KeyMap.iter (Hashtbl.replace h) keymap;
6200 defaults
6202 { v with f = pkeymap ret KeyMap.empty }
6204 | Vopen (_, _, _) ->
6205 error "unexpected subelement in defaults" s spos
6207 | Vclose "defaults" ->
6208 { v with f = llppconfig }
6210 | Vclose _ -> error "unexpected close in defaults" s spos
6212 and uifont b v t spos epos =
6213 match t with
6214 | Vdata | Vcdata ->
6215 Buffer.add_substring b s spos (epos - spos);
6217 | Vopen (_, _, _) ->
6218 error "unexpected subelement in ui-font" s spos
6219 | Vclose "ui-font" ->
6220 if String.length !fontpath = 0
6221 then fontpath := Buffer.contents b;
6222 { v with f = llppconfig }
6223 | Vclose _ -> error "unexpected close in ui-font" s spos
6224 | Vend -> error "unexpected end of input in ui-font" s spos
6226 and doc path pan anchor c bookmarks v t spos _ =
6227 match t with
6228 | Vdata | Vcdata -> v
6229 | Vend -> error "unexpected end of input in doc" s spos
6230 | Vopen ("bookmarks", _, closed) ->
6231 if closed
6232 then v
6233 else { v with f = pbookmarks path pan anchor c bookmarks }
6235 | Vopen ("keymap", attrs, closed) ->
6236 let modename =
6237 try List.assoc "mode" attrs
6238 with Not_found -> "global"
6240 if closed
6241 then v
6242 else
6243 let ret keymap =
6244 let h = findkeyhash c modename in
6245 KeyMap.iter (Hashtbl.replace h) keymap;
6246 doc path pan anchor c bookmarks
6248 { v with f = pkeymap ret KeyMap.empty }
6250 | Vopen (_, _, _) ->
6251 error "unexpected subelement in doc" s spos
6253 | Vclose "doc" ->
6254 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6255 { v with f = llppconfig }
6257 | Vclose _ -> error "unexpected close in doc" s spos
6259 and pkeymap ret keymap v t spos _ =
6260 match t with
6261 | Vdata | Vcdata -> v
6262 | Vend -> error "unexpected end of input in keymap" s spos
6263 | Vopen ("map", attrs, closed) ->
6264 let r, l = map_of attrs in
6265 let kss = fromstring keys_of_string spos "in" r [] in
6266 let lss = fromstring keys_of_string spos "out" l [] in
6267 let keymap =
6268 match kss with
6269 | [] -> keymap
6270 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6271 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6273 if closed
6274 then { v with f = pkeymap ret keymap }
6275 else
6276 let f () = v in
6277 { v with f = skip "map" f }
6279 | Vopen _ ->
6280 error "unexpected subelement in keymap" s spos
6282 | Vclose "keymap" ->
6283 { v with f = ret keymap }
6285 | Vclose _ -> error "unexpected close in keymap" s spos
6287 and pbookmarks path pan anchor c bookmarks v t spos _ =
6288 match t with
6289 | Vdata | Vcdata -> v
6290 | Vend -> error "unexpected end of input in bookmarks" s spos
6291 | Vopen ("item", attrs, closed) ->
6292 let titleent, spage, srely, svisy = bookmark_of attrs in
6293 let page = fromstring int_of_string spos "page" spage 0
6294 and rely = fromstring float_of_string spos "rely" srely 0.0
6295 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6296 let bookmarks =
6297 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6299 if closed
6300 then { v with f = pbookmarks path pan anchor c bookmarks }
6301 else
6302 let f () = v in
6303 { v with f = skip "item" f }
6305 | Vopen _ ->
6306 error "unexpected subelement in bookmarks" s spos
6308 | Vclose "bookmarks" ->
6309 { v with f = doc path pan anchor c bookmarks }
6311 | Vclose _ -> error "unexpected close in bookmarks" s spos
6313 and skip tag f v t spos _ =
6314 match t with
6315 | Vdata | Vcdata -> v
6316 | Vend ->
6317 error ("unexpected end of input in skipped " ^ tag) s spos
6318 | Vopen (tag', _, closed) ->
6319 if closed
6320 then v
6321 else
6322 let f' () = { v with f = skip tag f } in
6323 { v with f = skip tag' f' }
6324 | Vclose ctag ->
6325 if tag = ctag
6326 then f ()
6327 else error ("unexpected close in skipped " ^ tag) s spos
6330 parse { f = toplevel; accu = () } s;
6331 h, dc;
6334 let do_load f ic =
6336 let len = in_channel_length ic in
6337 let s = String.create len in
6338 really_input ic s 0 len;
6339 f s;
6340 with
6341 | Parse_error (msg, s, pos) ->
6342 let subs = subs s pos in
6343 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6344 failwith ("parse error: " ^ s)
6346 | exn ->
6347 failwith ("config load error: " ^ Printexc.to_string exn)
6350 let defconfpath =
6351 let dir =
6353 let dir = Filename.concat home ".config" in
6354 if Sys.is_directory dir then dir else home
6355 with _ -> home
6357 Filename.concat dir "llpp.conf"
6360 let confpath = ref defconfpath;;
6362 let load1 f =
6363 if Sys.file_exists !confpath
6364 then
6365 match
6366 (try Some (open_in_bin !confpath)
6367 with exn ->
6368 prerr_endline
6369 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6370 Printexc.to_string exn);
6371 None
6373 with
6374 | Some ic ->
6375 begin try
6376 f (do_load get ic)
6377 with exn ->
6378 prerr_endline
6379 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6380 Printexc.to_string exn);
6381 end;
6382 close_in ic;
6384 | None -> ()
6385 else
6386 f (Hashtbl.create 0, defconf)
6389 let load () =
6390 let f (h, dc) =
6391 let pc, pb, px, pa =
6393 Hashtbl.find h (Filename.basename state.path)
6394 with Not_found -> dc, [], 0, emptyanchor
6396 setconf defconf dc;
6397 setconf conf pc;
6398 state.bookmarks <- pb;
6399 state.x <- px;
6400 state.scrollw <- conf.scrollbw;
6401 if conf.jumpback
6402 then state.anchor <- pa;
6403 cbput state.hists.nav pa;
6405 load1 f
6408 let add_attrs bb always dc c =
6409 let ob s a b =
6410 if always || a != b
6411 then Printf.bprintf bb "\n %s='%b'" s a
6412 and oi s a b =
6413 if always || a != b
6414 then Printf.bprintf bb "\n %s='%d'" s a
6415 and oI s a b =
6416 if always || a != b
6417 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6418 and oz s a b =
6419 if always || a <> b
6420 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6421 and oF s a b =
6422 if always || a <> b
6423 then Printf.bprintf bb "\n %s='%f'" s a
6424 and oc s a b =
6425 if always || a <> b
6426 then
6427 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6428 and oC s a b =
6429 if always || a <> b
6430 then
6431 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6432 and oR s a b =
6433 if always || a <> b
6434 then
6435 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6436 and os s a b =
6437 if always || a <> b
6438 then
6439 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6440 and og s a b =
6441 if always || a <> b
6442 then
6443 match a with
6444 | None -> ()
6445 | Some (_N, _A, _B) ->
6446 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6447 and oW s a b =
6448 if always || a <> b
6449 then
6450 let v =
6451 match a with
6452 | None -> "false"
6453 | Some f ->
6454 if f = infinity
6455 then "true"
6456 else string_of_float f
6458 Printf.bprintf bb "\n %s='%s'" s v
6459 and oco s a b =
6460 if always || a <> b
6461 then
6462 match a with
6463 | Cmulti ((n, a, b), _) when n > 1 ->
6464 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6465 | Csplit (n, _) when n > 1 ->
6466 Printf.bprintf bb "\n %s='%d'" s ~-n
6467 | _ -> ()
6468 and obeco s a b =
6469 if always || a <> b
6470 then
6471 match a with
6472 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6473 | _ -> ()
6475 let w, h =
6476 if always
6477 then dc.winw, dc.winh
6478 else
6479 match state.fullscreen with
6480 | Some wh -> wh
6481 | None -> c.winw, c.winh
6483 oi "width" w dc.winw;
6484 oi "height" h dc.winh;
6485 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6486 oi "scroll-handle-height" c.scrollh dc.scrollh;
6487 ob "case-insensitive-search" c.icase dc.icase;
6488 ob "preload" c.preload dc.preload;
6489 oi "page-bias" c.pagebias dc.pagebias;
6490 oi "scroll-step" c.scrollstep dc.scrollstep;
6491 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6492 ob "max-height-fit" c.maxhfit dc.maxhfit;
6493 ob "crop-hack" c.crophack dc.crophack;
6494 oW "throttle" c.maxwait dc.maxwait;
6495 ob "highlight-links" c.hlinks dc.hlinks;
6496 ob "under-cursor-info" c.underinfo dc.underinfo;
6497 oi "vertical-margin" c.interpagespace dc.interpagespace;
6498 oz "zoom" c.zoom dc.zoom;
6499 ob "presentation" c.presentation dc.presentation;
6500 oi "rotation-angle" c.angle dc.angle;
6501 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6502 ob "proportional-display" c.proportional dc.proportional;
6503 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6504 oi "tex-count" c.texcount dc.texcount;
6505 oi "slice-height" c.sliceheight dc.sliceheight;
6506 oi "thumbnail-width" c.thumbw dc.thumbw;
6507 ob "persistent-location" c.jumpback dc.jumpback;
6508 oc "background-color" c.bgcolor dc.bgcolor;
6509 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6510 oi "tile-width" c.tilew dc.tilew;
6511 oi "tile-height" c.tileh dc.tileh;
6512 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6513 ob "checkers" c.checkers dc.checkers;
6514 oi "aalevel" c.aalevel dc.aalevel;
6515 ob "trim-margins" c.trimmargins dc.trimmargins;
6516 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6517 os "uri-launcher" c.urilauncher dc.urilauncher;
6518 os "path-launcher" c.pathlauncher dc.pathlauncher;
6519 oC "color-space" c.colorspace dc.colorspace;
6520 ob "invert-colors" c.invert dc.invert;
6521 oF "brightness" c.colorscale dc.colorscale;
6522 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6523 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6524 oco "columns" c.columns dc.columns;
6525 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6526 os "selection-command" c.selcmd dc.selcmd;
6527 ob "update-cursor" c.updatecurs dc.updatecurs;
6528 oi "hint-font-size" c.hfsize dc.hfsize;
6529 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6530 oF "page-scroll-scale" c.pgscale dc.pgscale;
6533 let keymapsbuf always dc c =
6534 let bb = Buffer.create 16 in
6535 let rec loop = function
6536 | [] -> ()
6537 | (modename, h) :: rest ->
6538 let dh = findkeyhash dc modename in
6539 if always || h <> dh
6540 then (
6541 if Hashtbl.length h > 0
6542 then (
6543 if Buffer.length bb > 0
6544 then Buffer.add_char bb '\n';
6545 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6546 Hashtbl.iter (fun i o ->
6547 let isdifferent = always ||
6549 let dO = Hashtbl.find dh i in
6550 dO <> o
6551 with Not_found -> true
6553 if isdifferent
6554 then
6555 let addkm (k, m) =
6556 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6557 if Wsi.withalt m then Buffer.add_string bb "alt-";
6558 if Wsi.withshift m then Buffer.add_string bb "shift-";
6559 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6560 Buffer.add_string bb (Wsi.keyname k);
6562 let addkms l =
6563 let rec loop = function
6564 | [] -> ()
6565 | km :: [] -> addkm km
6566 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6568 loop l
6570 Buffer.add_string bb "<map in='";
6571 addkm i;
6572 match o with
6573 | KMinsrt km ->
6574 Buffer.add_string bb "' out='";
6575 addkm km;
6576 Buffer.add_string bb "'/>\n"
6578 | KMinsrl kms ->
6579 Buffer.add_string bb "' out='";
6580 addkms kms;
6581 Buffer.add_string bb "'/>\n"
6583 | KMmulti (ins, kms) ->
6584 Buffer.add_char bb ' ';
6585 addkms ins;
6586 Buffer.add_string bb "' out='";
6587 addkms kms;
6588 Buffer.add_string bb "'/>\n"
6589 ) h;
6590 Buffer.add_string bb "</keymap>";
6593 loop rest
6595 loop c.keyhashes;
6599 let save () =
6600 let uifontsize = fstate.fontsize in
6601 let bb = Buffer.create 32768 in
6602 let f (h, dc) =
6603 let dc = if conf.bedefault then conf else dc in
6604 Buffer.add_string bb "<llppconfig>\n";
6606 if String.length !fontpath > 0
6607 then
6608 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6609 uifontsize
6610 !fontpath
6611 else (
6612 if uifontsize <> 14
6613 then
6614 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6617 Buffer.add_string bb "<defaults ";
6618 add_attrs bb true dc dc;
6619 let kb = keymapsbuf true dc dc in
6620 if Buffer.length kb > 0
6621 then (
6622 Buffer.add_string bb ">\n";
6623 Buffer.add_buffer bb kb;
6624 Buffer.add_string bb "\n</defaults>\n";
6626 else Buffer.add_string bb "/>\n";
6628 let adddoc path pan anchor c bookmarks =
6629 if bookmarks == [] && c = dc && anchor = emptyanchor
6630 then ()
6631 else (
6632 Printf.bprintf bb "<doc path='%s'"
6633 (enent path 0 (String.length path));
6635 if anchor <> emptyanchor
6636 then (
6637 let n, rely, visy = anchor in
6638 Printf.bprintf bb " page='%d'" n;
6639 if rely > 1e-6
6640 then
6641 Printf.bprintf bb " rely='%f'" rely
6643 if abs_float visy > 1e-6
6644 then
6645 Printf.bprintf bb " visy='%f'" visy
6649 if pan != 0
6650 then Printf.bprintf bb " pan='%d'" pan;
6652 add_attrs bb false dc c;
6653 let kb = keymapsbuf false dc c in
6655 begin match bookmarks with
6656 | [] ->
6657 if Buffer.length kb > 0
6658 then (
6659 Buffer.add_string bb ">\n";
6660 Buffer.add_buffer bb kb;
6661 Buffer.add_string bb "\n</doc>\n";
6663 else Buffer.add_string bb "/>\n"
6664 | _ ->
6665 Buffer.add_string bb ">\n<bookmarks>\n";
6666 List.iter (fun (title, _level, (page, rely, visy)) ->
6667 Printf.bprintf bb
6668 "<item title='%s' page='%d'"
6669 (enent title 0 (String.length title))
6670 page
6672 if rely > 1e-6
6673 then
6674 Printf.bprintf bb " rely='%f'" rely
6676 if abs_float visy > 1e-6
6677 then
6678 Printf.bprintf bb " visy='%f'" visy
6680 Buffer.add_string bb "/>\n";
6681 ) bookmarks;
6682 Buffer.add_string bb "</bookmarks>";
6683 if Buffer.length kb > 0
6684 then (
6685 Buffer.add_string bb "\n";
6686 Buffer.add_buffer bb kb;
6688 Buffer.add_string bb "\n</doc>\n";
6689 end;
6693 let pan, conf =
6694 match state.mode with
6695 | Birdseye (c, pan, _, _, _) ->
6696 let beyecolumns =
6697 match conf.columns with
6698 | Cmulti ((c, _, _), _) -> Some c
6699 | Csingle _ -> None
6700 | Csplit _ -> None
6701 and columns =
6702 match c.columns with
6703 | Cmulti (c, _) -> Cmulti (c, [||])
6704 | Csingle _ -> Csingle [||]
6705 | Csplit _ -> failwith "quit from bird's eye while split"
6707 pan, { c with beyecolumns = beyecolumns; columns = columns }
6708 | _ -> state.x, conf
6710 let basename = Filename.basename state.path in
6711 adddoc basename pan (getanchor ())
6712 (let conf =
6713 let autoscrollstep =
6714 match state.autoscroll with
6715 | Some step -> step
6716 | None -> conf.autoscrollstep
6718 match state.mode with
6719 | Birdseye (bc, _, _, _, _) ->
6720 { conf with
6721 zoom = bc.zoom;
6722 presentation = bc.presentation;
6723 interpagespace = bc.interpagespace;
6724 maxwait = bc.maxwait;
6725 autoscrollstep = autoscrollstep }
6726 | _ -> { conf with autoscrollstep = autoscrollstep }
6727 in conf)
6728 (if conf.savebmarks then state.bookmarks else []);
6730 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6731 if basename <> path
6732 then adddoc path x anchor c bookmarks
6733 ) h;
6734 Buffer.add_string bb "</llppconfig>";
6736 load1 f;
6737 if Buffer.length bb > 0
6738 then
6740 let tmp = !confpath ^ ".tmp" in
6741 let oc = open_out_bin tmp in
6742 Buffer.output_buffer oc bb;
6743 close_out oc;
6744 Unix.rename tmp !confpath;
6745 with exn ->
6746 prerr_endline
6747 ("error while saving configuration: " ^ Printexc.to_string exn)
6749 end;;
6751 let () =
6752 let trimcachepath = ref "" in
6753 Arg.parse
6754 (Arg.align
6755 [("-p", Arg.String (fun s -> state.password <- s) ,
6756 "<password> Set password");
6758 ("-f", Arg.String (fun s -> Config.fontpath := s),
6759 "<path> Set path to the user interface font");
6761 ("-c", Arg.String (fun s -> Config.confpath := s),
6762 "<path> Set path to the configuration file");
6764 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6765 "<path> Set path to the trim cache file");
6767 ("-v", Arg.Unit (fun () ->
6768 Printf.printf
6769 "%s\nconfiguration path: %s\n"
6770 (version ())
6771 Config.defconfpath
6773 exit 0), " Print version and exit");
6776 (fun s -> state.path <- s)
6777 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6779 if String.length state.path = 0
6780 then (prerr_endline "file name missing"; exit 1);
6782 Config.load ();
6784 let globalkeyhash = findkeyhash conf "global" in
6785 let wsfd, winw, winh = Wsi.init (object
6786 method expose =
6787 if nogeomcmds state.geomcmds || platform == Posx
6788 then display ()
6789 else (
6790 GlClear.color (scalecolor2 conf.bgcolor);
6791 GlClear.clear [`color];
6793 method display = display ()
6794 method reshape w h = reshape w h
6795 method mouse b d x y m = mouse b d x y m
6796 method motion x y = state.mpos <- (x, y); motion x y
6797 method pmotion x y = state.mpos <- (x, y); pmotion x y
6798 method key k m =
6799 let mascm = m land (
6800 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6801 ) in
6802 match state.keystate with
6803 | KSnone ->
6804 let km = k, mascm in
6805 begin
6806 match
6807 let modehash = state.uioh#modehash in
6808 try Hashtbl.find modehash km
6809 with Not_found ->
6810 try Hashtbl.find globalkeyhash km
6811 with Not_found -> KMinsrt (k, m)
6812 with
6813 | KMinsrt (k, m) -> keyboard k m
6814 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6815 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6817 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6818 List.iter (fun (k, m) -> keyboard k m) insrt;
6819 state.keystate <- KSnone
6820 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6821 state.keystate <- KSinto (keys, insrt)
6822 | _ ->
6823 state.keystate <- KSnone
6825 method enter x y = state.mpos <- (x, y); pmotion x y
6826 method leave = state.mpos <- (-1, -1)
6827 method quit = raise Quit
6828 end) conf.winw conf.winh (platform = Posx) in
6830 state.wsfd <- wsfd;
6832 if not (
6833 List.exists GlMisc.check_extension
6834 [ "GL_ARB_texture_rectangle"
6835 ; "GL_EXT_texture_recangle"
6836 ; "GL_NV_texture_rectangle" ]
6838 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6840 let cr, sw =
6841 match Ne.pipe () with
6842 | Ne.Exn exn ->
6843 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6844 exit 1
6845 | Ne.Res rw -> rw
6846 and sr, cw =
6847 match Ne.pipe () with
6848 | Ne.Exn exn ->
6849 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6850 exit 1
6851 | Ne.Res rw -> rw
6854 cloexec cr;
6855 cloexec sw;
6856 cloexec sr;
6857 cloexec cw;
6859 setcheckers conf.checkers;
6860 redirectstderr ();
6862 init (cr, cw) (
6863 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6864 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6865 !Config.fontpath, !trimcachepath
6867 state.sr <- sr;
6868 state.sw <- sw;
6869 state.text <- "Opening " ^ state.path;
6870 reshape winw winh;
6871 opendoc state.path state.password;
6872 state.uioh <- uioh;
6874 let rec loop deadline =
6875 let r =
6876 match state.errfd with
6877 | None -> [state.sr; state.wsfd]
6878 | Some fd -> [state.sr; state.wsfd; fd]
6880 if state.redisplay
6881 then (
6882 state.redisplay <- false;
6883 display ();
6885 let timeout =
6886 let now = now () in
6887 if deadline > now
6888 then (
6889 if deadline = infinity
6890 then ~-.1.0
6891 else max 0.0 (deadline -. now)
6893 else 0.0
6895 let r, _, _ =
6896 try Unix.select r [] [] timeout
6897 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6899 begin match r with
6900 | [] ->
6901 state.ghyll None;
6902 let newdeadline =
6903 if state.ghyll == noghyll
6904 then
6905 match state.autoscroll with
6906 | Some step when step != 0 ->
6907 let y = state.y + step in
6908 let y =
6909 if y < 0
6910 then state.maxy
6911 else if y >= state.maxy then 0 else y
6913 gotoy y;
6914 if state.mode = View
6915 then state.text <- "";
6916 deadline +. 0.01
6917 | _ -> infinity
6918 else deadline +. 0.01
6920 loop newdeadline
6922 | l ->
6923 let rec checkfds = function
6924 | [] -> ()
6925 | fd :: rest when fd = state.sr ->
6926 let cmd = readcmd state.sr in
6927 act cmd;
6928 checkfds rest
6930 | fd :: rest when fd = state.wsfd ->
6931 Wsi.readresp fd;
6932 checkfds rest
6934 | fd :: rest ->
6935 let s = String.create 80 in
6936 let n = Unix.read fd s 0 80 in
6937 if conf.redirectstderr
6938 then (
6939 Buffer.add_substring state.errmsgs s 0 n;
6940 state.newerrmsgs <- true;
6941 state.redisplay <- true;
6943 else (
6944 prerr_string (String.sub s 0 n);
6945 flush stderr;
6947 checkfds rest
6949 checkfds l;
6950 let newdeadline =
6951 let deadline1 =
6952 if deadline = infinity
6953 then now () +. 0.01
6954 else deadline
6956 match state.autoscroll with
6957 | Some step when step != 0 -> deadline1
6958 | _ -> if state.ghyll == noghyll then infinity else deadline1
6960 loop newdeadline
6961 end;
6964 loop infinity;
6965 with Quit ->
6966 Config.save ();