pax wip
[llpp.git] / main.ml
blobc4a873f2113bdd626e9679e3fe3d2fba983b6687
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type mark =
17 | Mark_page
18 | Mark_block
19 | Mark_line
20 | Mark_word
23 type params = (angle * fitmodel * trimparams
24 * texcount * sliceheight * memsize
25 * colorspace * fontpath * trimcachepath
26 * haspbo)
27 and pageno = int
28 and width = int
29 and height = int
30 and leftx = int
31 and opaque = string
32 and recttype = int
33 and pixmapsize = int
34 and angle = int
35 and trimmargins = bool
36 and interpagespace = int
37 and texcount = int
38 and sliceheight = int
39 and gen = int
40 and top = float
41 and dtop = float
42 and fontpath = string
43 and trimcachepath = string
44 and memsize = int
45 and aalevel = int
46 and irect = (int * int * int * int)
47 and trimparams = (trimmargins * irect)
48 and colorspace = | Rgb | Bgr | Gray
49 and fitmodel = | FitWidth | FitProportional | FitPage
50 and haspbo = bool
53 type x = int
54 and y = int
55 and tilex = int
56 and tiley = int
57 and tileparams = (x * y * width * height * tilex * tiley)
60 type link =
61 | Lnotfound
62 | Lfound of int
63 and linkdir =
64 | LDfirst
65 | LDlast
66 | LDfirstvisible of (int * int * int)
67 | LDleft of int
68 | LDright of int
69 | LDdown of int
70 | LDup of int
73 type pagewithlinks =
74 | Pwlnotfound
75 | Pwl of int
78 type keymap =
79 | KMinsrt of key
80 | KMinsrl of key list
81 | KMmulti of key list * key list
82 and key = int * int
83 and keyhash = (key, keymap) Hashtbl.t
84 and keystate =
85 | KSnone
86 | KSinto of (key list * key list)
89 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
90 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
92 type pipe = (Unix.file_descr * Unix.file_descr);;
94 external init : pipe -> params -> unit = "ml_init";;
95 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
96 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
97 external getpdimrect : int -> float array = "ml_getpdimrect";;
98 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
99 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
100 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
101 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
102 external measurestr : int -> string -> float = "ml_measure_string";;
103 external postprocess :
104 opaque -> int -> int -> int -> (int * string * int) -> int
105 = "ml_postprocess";;
106 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
107 external platform : unit -> platform = "ml_platform";;
108 external setaalevel : int -> unit = "ml_setaalevel";;
109 external realloctexts : int -> bool = "ml_realloctexts";;
110 external findlink : opaque -> linkdir -> link = "ml_findlink";;
111 external getlink : opaque -> int -> under = "ml_getlink";;
112 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
113 external getlinkcount : opaque -> int = "ml_getlinkcount";;
114 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
115 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
116 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
117 external freepbo : string -> unit = "ml_freepbo";;
118 external unmappbo : string -> unit = "ml_unmappbo";;
119 external pbousable : unit -> bool = "ml_pbo_usable";;
120 external unproject : opaque -> int -> int -> (int * int) option
121 = "ml_unproject";;
122 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
124 let platform_to_string = function
125 | Punknown -> "unknown"
126 | Plinux -> "Linux"
127 | Posx -> "OSX"
128 | Psun -> "Sun"
129 | Pfreebsd -> "FreeBSD"
130 | Pdragonflybsd -> "DragonflyBSD"
131 | Popenbsd -> "OpenBSD"
132 | Pnetbsd -> "NetBSD"
133 | Pcygwin -> "Cygwin"
136 let platform = platform ();;
138 let now = Unix.gettimeofday;;
140 let selfexec = ref "";;
142 let popen cmd fda =
143 if platform = Pcygwin
144 then (
145 let sh = "/bin/sh" in
146 let args = [|sh; "-c"; cmd|] in
147 let rec std si so se = function
148 | [] -> si, so, se
149 | (fd, 0) :: rest -> std fd so se rest
150 | (fd, -1) :: rest ->
151 Unix.set_close_on_exec fd;
152 std si so se rest
153 | (_, n) :: _ ->
154 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
156 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
157 ignore (Unix.create_process sh args si so se)
159 else popen cmd fda;
162 type mpos = int * int
163 and mstate =
164 | Msel of (mpos * mpos)
165 | Mpan of mpos
166 | Mscrolly | Mscrollx
167 | Mzoom of (int * int)
168 | Mzoomrect of (mpos * mpos)
169 | Mnone
172 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
173 and onkey = string -> int -> te
174 and ondone = string -> unit
175 and histcancel = unit -> unit
176 and onhist = ((histcmd -> string) * histcancel)
177 and histcmd = HCnext | HCprev | HCfirst | HClast
178 and cancelonempty = bool
179 and te =
180 | TEstop
181 | TEdone of string
182 | TEcont of string
183 | TEswitch of textentry
186 type 'a circbuf =
187 { store : 'a array
188 ; mutable rc : int
189 ; mutable wc : int
190 ; mutable len : int
194 let bound v minv maxv =
195 max minv (min maxv v);
198 let cbnew n v =
199 { store = Array.create n v
200 ; rc = 0
201 ; wc = 0
202 ; len = 0
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 bound rc 0 (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 let drawstring size x y s =
244 Gl.enable `blend;
245 Gl.enable `texture_2d;
246 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
247 ignore (drawstr size x y s);
248 Gl.disable `blend;
249 Gl.disable `texture_2d;
252 let drawstring1 size x y s =
253 drawstr size x y s;
256 let drawstring2 size x y fmt =
257 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
260 type page =
261 { pageno : int
262 ; pagedimno : int
263 ; pagew : int
264 ; pageh : int
265 ; pagex : int
266 ; pagey : int
267 ; pagevw : int
268 ; pagevh : int
269 ; pagedispx : int
270 ; pagedispy : int
271 ; pagecol : int
275 let debugl l =
276 dolog "l %d dim=%d {" l.pageno l.pagedimno;
277 dolog " WxH %dx%d" l.pagew l.pageh;
278 dolog " vWxH %dx%d" l.pagevw l.pagevh;
279 dolog " pagex,y %d,%d" l.pagex l.pagey;
280 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
281 dolog " column %d" l.pagecol;
282 dolog "}";
285 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
286 dolog "rect {";
287 dolog " x0,y0=(% f, % f)" x0 y0;
288 dolog " x1,y1=(% f, % f)" x1 y1;
289 dolog " x2,y2=(% f, % f)" x2 y2;
290 dolog " x3,y3=(% f, % f)" x3 y3;
291 dolog "}";
294 type multicolumns = multicol * pagegeom
295 and singlecolumn = pagegeom
296 and splitcolumns = columncount * pagegeom
297 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
298 and multicol = columncount * covercount * covercount
299 and pdimno = int
300 and columncount = int
301 and covercount = int;;
303 type scrollb = int;;
304 let scrollbvv = 1;;
305 let scrollbhv = 2;;
307 type conf =
308 { mutable scrollbw : int
309 ; mutable scrollh : int
310 ; mutable scrollb : scrollb
311 ; mutable icase : bool
312 ; mutable preload : bool
313 ; mutable pagebias : int
314 ; mutable verbose : bool
315 ; mutable debug : bool
316 ; mutable scrollstep : int
317 ; mutable hscrollstep : int
318 ; mutable maxhfit : bool
319 ; mutable crophack : bool
320 ; mutable autoscrollstep : int
321 ; mutable maxwait : float option
322 ; mutable hlinks : bool
323 ; mutable underinfo : bool
324 ; mutable interpagespace : interpagespace
325 ; mutable zoom : float
326 ; mutable presentation : bool
327 ; mutable angle : angle
328 ; mutable cwinw : int
329 ; mutable cwinh : int
330 ; mutable savebmarks : bool
331 ; mutable fitmodel : fitmodel
332 ; mutable trimmargins : trimmargins
333 ; mutable trimfuzz : irect
334 ; mutable memlimit : memsize
335 ; mutable texcount : texcount
336 ; mutable sliceheight : sliceheight
337 ; mutable thumbw : width
338 ; mutable jumpback : bool
339 ; mutable bgcolor : (float * float * float)
340 ; mutable bedefault : bool
341 ; mutable tilew : int
342 ; mutable tileh : int
343 ; mutable mustoresize : memsize
344 ; mutable checkers : bool
345 ; mutable aalevel : int
346 ; mutable urilauncher : string
347 ; mutable pathlauncher : string
348 ; mutable colorspace : colorspace
349 ; mutable invert : bool
350 ; mutable colorscale : float
351 ; mutable redirectstderr : bool
352 ; mutable ghyllscroll : (int * int * int) option
353 ; mutable columns : columns
354 ; mutable beyecolumns : columncount option
355 ; mutable selcmd : string
356 ; mutable paxcmd : string
357 ; mutable updatecurs : bool
358 ; mutable keyhashes : (string * keyhash) list
359 ; mutable hfsize : int
360 ; mutable pgscale : float
361 ; mutable usepbo : bool
362 ; mutable wheelbypage : bool
363 ; mutable stcmd : string
364 ; mutable riani : bool
365 ; mutable pax : (float * int * int) ref option
366 ; mutable paxmark : mark
368 and columns =
369 | Csingle of singlecolumn
370 | Cmulti of multicolumns
371 | Csplit of splitcolumns
374 type anchor = pageno * top * dtop;;
376 type outline = string * int * anchor;;
378 type rect = float * float * float * float * float * float * float * float;;
380 type tile = opaque * pixmapsize * elapsed
381 and elapsed = float;;
382 type pagemapkey = pageno * gen;;
383 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
384 and row = int
385 and col = int;;
387 let emptyanchor = (0, 0.0, 0.0);;
389 type infochange = | Memused | Docinfo | Pdim;;
391 class type uioh = object
392 method display : unit
393 method key : int -> int -> uioh
394 method button : int -> bool -> int -> int -> int -> uioh
395 method motion : int -> int -> uioh
396 method pmotion : int -> int -> uioh
397 method infochanged : infochange -> unit
398 method scrollpw : (int * float * float)
399 method scrollph : (int * float * float)
400 method modehash : keyhash
401 method eformsgs : bool
402 end;;
404 type mode =
405 | Birdseye of (conf * leftx * pageno * pageno * anchor)
406 | Textentry of (textentry * onleave)
407 | View
408 | LinkNav of linktarget
409 and onleave = leavetextentrystatus -> unit
410 and leavetextentrystatus = | Cancel | Confirm
411 and helpitem = string * int * action
412 and action =
413 | Noaction
414 | Action of (uioh -> uioh)
415 and linktarget =
416 | Ltexact of (pageno * int)
417 | Ltgendir of int
420 let isbirdseye = function Birdseye _ -> true | _ -> false;;
421 let istextentry = function Textentry _ -> true | _ -> false;;
423 type currently =
424 | Idle
425 | Loading of (page * gen)
426 | Tiling of (
427 page * opaque * colorspace * angle * gen * col * row * width * height
429 | Outlining of outline list
432 let emptykeyhash = Hashtbl.create 0;;
433 let nouioh : uioh = object (self)
434 method display = ()
435 method key _ _ = self
436 method button _ _ _ _ _ = self
437 method motion _ _ = self
438 method pmotion _ _ = self
439 method infochanged _ = ()
440 method scrollpw = (0, nan, nan)
441 method scrollph = (0, nan, nan)
442 method modehash = emptykeyhash
443 method eformsgs = false
444 end;;
446 type state =
447 { mutable sr : Unix.file_descr
448 ; mutable sw : Unix.file_descr
449 ; mutable wsfd : Unix.file_descr
450 ; mutable errfd : Unix.file_descr option
451 ; mutable stderr : Unix.file_descr
452 ; mutable errmsgs : Buffer.t
453 ; mutable newerrmsgs : bool
454 ; mutable w : int
455 ; mutable x : int
456 ; mutable y : int
457 ; mutable anchor : anchor
458 ; mutable ranchors : (string * string * anchor * string) list
459 ; mutable maxy : int
460 ; mutable layout : page list
461 ; pagemap : (pagemapkey, opaque) Hashtbl.t
462 ; tilemap : (tilemapkey, tile) Hashtbl.t
463 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
464 ; mutable pdims : (pageno * width * height * leftx) list
465 ; mutable pagecount : int
466 ; mutable currently : currently
467 ; mutable mstate : mstate
468 ; mutable searchpattern : string
469 ; mutable rects : (pageno * recttype * rect) list
470 ; mutable rects1 : (pageno * recttype * rect) list
471 ; mutable text : string
472 ; mutable winstate : Wsi.winstate list
473 ; mutable mode : mode
474 ; mutable uioh : uioh
475 ; mutable outlines : outline array
476 ; mutable bookmarks : outline list
477 ; mutable path : string
478 ; mutable password : string
479 ; mutable nameddest : string
480 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
481 ; mutable memused : memsize
482 ; mutable gen : gen
483 ; mutable throttle : (page list * int * float) option
484 ; mutable autoscroll : int option
485 ; mutable ghyll : (int option -> unit)
486 ; mutable help : helpitem array
487 ; mutable docinfo : (int * string) list
488 ; mutable texid : GlTex.texture_id option
489 ; hists : hists
490 ; mutable prevzoom : float
491 ; mutable progress : float
492 ; mutable redisplay : bool
493 ; mutable mpos : mpos
494 ; mutable keystate : keystate
495 ; mutable glinks : bool
496 ; mutable prevcolumns : (columns * float) option
497 ; mutable winw : int
498 ; mutable winh : int
499 ; mutable reprf : (unit -> unit)
500 ; mutable origin : string
501 ; mutable roam : (unit -> unit)
503 and hists =
504 { pat : string circbuf
505 ; pag : string circbuf
506 ; nav : anchor circbuf
507 ; sel : string circbuf
511 let defconf =
512 { scrollbw = 7
513 ; scrollh = 12
514 ; scrollb = scrollbhv lor scrollbvv
515 ; icase = true
516 ; preload = true
517 ; pagebias = 0
518 ; verbose = false
519 ; debug = false
520 ; scrollstep = 24
521 ; hscrollstep = 24
522 ; maxhfit = true
523 ; crophack = false
524 ; autoscrollstep = 2
525 ; maxwait = None
526 ; hlinks = false
527 ; underinfo = false
528 ; interpagespace = 2
529 ; zoom = 1.0
530 ; presentation = false
531 ; angle = 0
532 ; cwinw = 900
533 ; cwinh = 900
534 ; savebmarks = true
535 ; fitmodel = FitProportional
536 ; trimmargins = false
537 ; trimfuzz = (0,0,0,0)
538 ; memlimit = 32 lsl 20
539 ; texcount = 256
540 ; sliceheight = 24
541 ; thumbw = 76
542 ; jumpback = true
543 ; bgcolor = (0.5, 0.5, 0.5)
544 ; bedefault = false
545 ; tilew = 2048
546 ; tileh = 2048
547 ; mustoresize = 256 lsl 20
548 ; checkers = true
549 ; aalevel = 8
550 ; urilauncher =
551 (match platform with
552 | Plinux | Pfreebsd | Pdragonflybsd
553 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
554 | Posx -> "open \"%s\""
555 | Pcygwin -> "cygstart \"%s\""
556 | Punknown -> "echo %s")
557 ; pathlauncher = "lp \"%s\""
558 ; selcmd =
559 (match platform with
560 | Plinux | Pfreebsd | Pdragonflybsd
561 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
562 | Posx -> "pbcopy"
563 | Pcygwin -> "wsel"
564 | Punknown -> "cat")
565 ; paxcmd = "cat"
566 ; colorspace = Rgb
567 ; invert = false
568 ; colorscale = 1.0
569 ; redirectstderr = false
570 ; ghyllscroll = None
571 ; columns = Csingle [||]
572 ; beyecolumns = None
573 ; updatecurs = false
574 ; hfsize = 12
575 ; pgscale = 1.0
576 ; usepbo = false
577 ; wheelbypage = false
578 ; stcmd = "echo SyncTex"
579 ; riani = false
580 ; pax = None
581 ; paxmark = Mark_word
582 ; keyhashes =
583 let mk n = (n, Hashtbl.create 1) in
584 [ mk "global"
585 ; mk "info"
586 ; mk "help"
587 ; mk "outline"
588 ; mk "listview"
589 ; mk "birdseye"
590 ; mk "textentry"
591 ; mk "links"
592 ; mk "view"
597 let wtmode = ref false;;
599 let findkeyhash c name =
600 try List.assoc name c.keyhashes
601 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
604 let conf = { defconf with angle = defconf.angle };;
606 let pgscale h = truncate (float h *. conf.pgscale);;
608 type fontstate =
609 { mutable fontsize : int
610 ; mutable wwidth : float
611 ; mutable maxrows : int
615 let fstate =
616 { fontsize = 14
617 ; wwidth = nan
618 ; maxrows = -1
622 let geturl s =
623 let colonpos = try String.index s ':' with Not_found -> -1 in
624 let len = String.length s in
625 if colonpos >= 0 && colonpos + 3 < len
626 then (
627 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
628 then
629 let schemestartpos =
630 try String.rindex_from s colonpos ' '
631 with Not_found -> -1
633 let scheme =
634 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
636 match scheme with
637 | "http" | "ftp" | "mailto" ->
638 let epos =
639 try String.index_from s colonpos ' '
640 with Not_found -> len
642 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
643 | _ -> ""
644 else ""
646 else ""
649 let gotouri uri =
650 if String.length conf.urilauncher = 0
651 then print_endline uri
652 else (
653 let url = geturl uri in
654 if String.length url = 0
655 then Printf.eprintf "obtained empty url from uri %S" uri
656 else
657 let re = Str.regexp "%s" in
658 let command = Str.global_replace re url conf.urilauncher in
659 try popen command []
660 with exn ->
661 Printf.eprintf
662 "failed to execute `%s': %s\n" command (exntos exn);
663 flush stderr;
667 let version () =
668 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
669 (platform_to_string platform) Sys.word_size Sys.ocaml_version
672 let makehelp () =
673 let strings = version () :: "" :: Help.keys in
674 Array.of_list (
675 List.map (fun s ->
676 let url = geturl s in
677 if String.length url > 0
678 then (s, 0, Action (fun u -> gotouri url; u))
679 else (s, 0, Noaction)
680 ) strings);
683 let noghyll _ = ();;
684 let firstgeomcmds = "", [];;
685 let noreprf () = ();;
687 let state =
688 { sr = Unix.stdin
689 ; sw = Unix.stdin
690 ; wsfd = Unix.stdin
691 ; errfd = None
692 ; stderr = Unix.stderr
693 ; errmsgs = Buffer.create 0
694 ; newerrmsgs = false
695 ; x = 0
696 ; y = 0
697 ; w = 0
698 ; anchor = emptyanchor
699 ; ranchors = []
700 ; layout = []
701 ; maxy = max_int
702 ; tilelru = Queue.create ()
703 ; pagemap = Hashtbl.create 10
704 ; tilemap = Hashtbl.create 10
705 ; pdims = []
706 ; pagecount = 0
707 ; currently = Idle
708 ; mstate = Mnone
709 ; rects = []
710 ; rects1 = []
711 ; text = ""
712 ; mode = View
713 ; winstate = []
714 ; searchpattern = ""
715 ; outlines = [||]
716 ; bookmarks = []
717 ; path = ""
718 ; password = ""
719 ; nameddest = ""
720 ; geomcmds = firstgeomcmds
721 ; hists =
722 { nav = cbnew 10 emptyanchor
723 ; pat = cbnew 10 ""
724 ; pag = cbnew 10 ""
725 ; sel = cbnew 10 ""
727 ; memused = 0
728 ; gen = 0
729 ; throttle = None
730 ; autoscroll = None
731 ; ghyll = noghyll
732 ; help = makehelp ()
733 ; docinfo = []
734 ; texid = None
735 ; prevzoom = 1.0
736 ; progress = -1.0
737 ; uioh = nouioh
738 ; redisplay = true
739 ; mpos = (-1, -1)
740 ; keystate = KSnone
741 ; glinks = false
742 ; prevcolumns = None
743 ; winw = -1
744 ; winh = -1
745 ; reprf = noreprf
746 ; origin = ""
747 ; roam = (fun () -> ())
751 let hscrollh () =
752 if (conf.scrollb land scrollbhv = 0)
753 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
754 then 0
755 else conf.scrollbw
758 let vscrollw () =
759 if (conf.scrollb land scrollbvv = 0)
760 then 0
761 else conf.scrollbw
764 let wadjsb w = w - vscrollw ();;
766 let setfontsize n =
767 fstate.fontsize <- n;
768 fstate.wwidth <- measurestr fstate.fontsize "w";
769 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
772 let vlog fmt =
773 if conf.verbose
774 then
775 Printf.kprintf prerr_endline fmt
776 else
777 Printf.kprintf ignore fmt
780 let launchpath () =
781 if String.length conf.pathlauncher = 0
782 then print_endline state.path
783 else (
784 let re = Str.regexp "%s" in
785 let command = Str.global_replace re state.path conf.pathlauncher in
786 try popen command []
787 with exn ->
788 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
789 flush stderr;
793 module Ne = struct
794 type 'a t = | Res of 'a | Exn of exn;;
796 let pipe () =
797 try Res (Unix.pipe ())
798 with exn -> Exn exn
801 let clo fd f =
802 try tempfailureretry Unix.close fd
803 with exn -> f (exntos exn)
806 let dup fd =
807 try Res (tempfailureretry Unix.dup fd)
808 with exn -> Exn exn
811 let dup2 fd1 fd2 =
812 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
813 with exn -> Exn exn
815 end;;
817 let redirectstderr () =
818 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
819 if conf.redirectstderr
820 then
821 match Ne.pipe () with
822 | Ne.Exn exn ->
823 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
825 | Ne.Res (r, w) ->
826 begin match Ne.dup Unix.stderr with
827 | Ne.Exn exn ->
828 dolog "failed to dup stderr: %s" (exntos exn);
829 Ne.clo r (clofail "pipe/r");
830 Ne.clo w (clofail "pipe/w");
832 | Ne.Res dupstderr ->
833 begin match Ne.dup2 w Unix.stderr with
834 | Ne.Exn exn ->
835 dolog "failed to dup2 to stderr: %s" (exntos exn);
836 Ne.clo dupstderr (clofail "stderr duplicate");
837 Ne.clo r (clofail "redir pipe/r");
838 Ne.clo w (clofail "redir pipe/w");
840 | Ne.Res () ->
841 state.stderr <- dupstderr;
842 state.errfd <- Some r;
843 end;
845 else (
846 state.newerrmsgs <- false;
847 begin match state.errfd with
848 | Some fd ->
849 begin match Ne.dup2 state.stderr Unix.stderr with
850 | Ne.Exn exn ->
851 dolog "failed to dup2 original stderr: %s" (exntos exn)
852 | Ne.Res () ->
853 Ne.clo fd (clofail "dup of stderr");
854 state.errfd <- None;
855 end;
856 | None -> ()
857 end;
858 prerr_string (Buffer.contents state.errmsgs);
859 flush stderr;
860 Buffer.clear state.errmsgs;
864 module G =
865 struct
866 let postRedisplay who =
867 if conf.verbose
868 then prerr_endline ("redisplay for " ^ who);
869 state.redisplay <- true;
871 end;;
873 let getopaque pageno =
874 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
875 with Not_found -> None
878 let putopaque pageno opaque =
879 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
882 let pagetranslatepoint l x y =
883 let dy = y - l.pagedispy in
884 let y = dy + l.pagey in
885 let dx = x - l.pagedispx in
886 let x = dx + l.pagex in
887 (x, y);
890 let onppundermouse g x y d =
891 let rec f = function
892 | l :: rest ->
893 begin match getopaque l.pageno with
894 | Some opaque ->
895 let x0 = l.pagedispx in
896 let x1 = x0 + l.pagevw in
897 let y0 = l.pagedispy in
898 let y1 = y0 + l.pagevh in
899 if y >= y0 && y <= y1 && x >= x0 && x <= x1
900 then
901 let px, py = pagetranslatepoint l x y in
902 match g opaque l px py with
903 | Some res -> res
904 | None -> f rest
905 else f rest
906 | _ ->
907 f rest
909 | [] -> d
911 f state.layout
914 let getunder x y =
915 let g opaque _ px py =
916 match whatsunder opaque px py with
917 | Unone -> None
918 | under -> Some under
920 onppundermouse g x y Unone
923 let unproject x y =
924 let g opaque l x y =
925 match unproject opaque x y with
926 | Some (x, y) -> Some (Some (l.pageno, x, y))
927 | None -> None
929 onppundermouse g x y None;
932 let showtext c s =
933 state.text <- Printf.sprintf "%c%s" c s;
934 G.postRedisplay "showtext";
937 let paxunder x y =
938 let g opaque l px py =
939 if markunder opaque px py conf.paxmark
940 then (
941 Some (fun () ->
942 match getopaque l.pageno with
943 | None -> ()
944 | Some opaque ->
945 match Ne.pipe () with
946 | Ne.Exn exn ->
947 showtext '!'
948 (Printf.sprintf
949 "can not create mark pipe: %s"
950 (exntos exn));
951 | Ne.Res (r, w) ->
952 let doclose what fd =
953 Ne.clo fd (fun msg ->
954 dolog "%s close failed: %s" what msg)
957 popen conf.paxcmd [r, 0; w, -1];
958 copysel w opaque false;
959 doclose "pipe/r" r;
960 G.postRedisplay "paxunder";
961 with exn ->
962 dolog "can not execute %S: %s"
963 conf.paxcmd (exntos exn);
964 doclose "pipe/r" r;
965 doclose "pipe/w" w;
968 else None
970 G.postRedisplay "paxunder";
971 state.roam <-
972 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
975 let selstring s =
976 match Ne.pipe () with
977 | Ne.Exn exn ->
978 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
979 | Ne.Res (r, w) ->
980 let popened =
981 try popen conf.selcmd [r, 0; w, -1]; true
982 with exn ->
983 showtext '!'
984 (Printf.sprintf "failed to execute %s: %s"
985 conf.selcmd (exntos exn));
986 false
988 let clo cap fd =
989 Ne.clo fd (fun msg ->
990 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
993 if popened
994 then
995 (try
996 let l = String.length s in
997 let n = tempfailureretry (Unix.write w s 0) l in
998 if n != l
999 then
1000 showtext '!'
1001 (Printf.sprintf
1002 "failed to write %d characters to sel pipe, wrote %d"
1005 with exn ->
1006 showtext '!'
1007 (Printf.sprintf "failed to write to sel pipe: %s"
1008 (exntos exn)
1011 else dolog "%s" s;
1012 clo "pipe/r" r;
1013 clo "pipe/w" w;
1016 let undertext = function
1017 | Unone -> "none"
1018 | Ulinkuri s -> s
1019 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1020 | Utext s -> "font: " ^ s
1021 | Uunexpected s -> "unexpected: " ^ s
1022 | Ulaunch s -> "launch: " ^ s
1023 | Unamed s -> "named: " ^ s
1024 | Uremote (filename, pageno) ->
1025 Printf.sprintf "%s: page %d" filename (pageno+1)
1028 let updateunder x y =
1029 match getunder x y with
1030 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1031 | Ulinkuri uri ->
1032 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1033 Wsi.setcursor Wsi.CURSOR_INFO
1034 | Ulinkgoto (pageno, _) ->
1035 if conf.underinfo
1036 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1037 Wsi.setcursor Wsi.CURSOR_INFO
1038 | Utext s ->
1039 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1040 Wsi.setcursor Wsi.CURSOR_TEXT
1041 | Uunexpected s ->
1042 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1043 Wsi.setcursor Wsi.CURSOR_INHERIT
1044 | Ulaunch s ->
1045 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1046 Wsi.setcursor Wsi.CURSOR_INHERIT
1047 | Unamed s ->
1048 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1049 Wsi.setcursor Wsi.CURSOR_INHERIT
1050 | Uremote (filename, pageno) ->
1051 if conf.underinfo then showtext 'r'
1052 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1053 Wsi.setcursor Wsi.CURSOR_INFO
1056 let showlinktype under =
1057 if conf.underinfo
1058 then
1059 match under with
1060 | Unone -> ()
1061 | under ->
1062 let s = undertext under in
1063 showtext ' ' s
1066 let addchar s c =
1067 let b = Buffer.create (String.length s + 1) in
1068 Buffer.add_string b s;
1069 Buffer.add_char b c;
1070 Buffer.contents b;
1073 let colorspace_of_string s =
1074 match String.lowercase s with
1075 | "rgb" -> Rgb
1076 | "bgr" -> Bgr
1077 | "gray" -> Gray
1078 | _ -> failwith "invalid colorspace"
1081 let int_of_colorspace = function
1082 | Rgb -> 0
1083 | Bgr -> 1
1084 | Gray -> 2
1087 let colorspace_of_int = function
1088 | 0 -> Rgb
1089 | 1 -> Bgr
1090 | 2 -> Gray
1091 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1094 let colorspace_to_string = function
1095 | Rgb -> "rgb"
1096 | Bgr -> "bgr"
1097 | Gray -> "gray"
1100 let mark_of_string s =
1101 match String.lowercase s with
1102 | "word" -> Mark_word
1103 | "line" -> Mark_line
1104 | "block" -> Mark_block
1105 | "page" -> Mark_page
1106 | _ -> failwith "invalid colorspace"
1109 let int_of_mark = function
1110 | Mark_page -> 0
1111 | Mark_block -> 1
1112 | Mark_line -> 2
1113 | Mark_word -> 3
1116 let mark_of_int = function
1117 | 0 -> Mark_page
1118 | 1 -> Mark_block
1119 | 2 -> Mark_line
1120 | 3 -> Mark_word
1121 | n -> failwith ("invalid mark index " ^ string_of_int n)
1124 let mark_to_string = function
1125 | Mark_word -> "word"
1126 | Mark_line -> "line"
1127 | Mark_block -> "block"
1128 | Mark_page -> "page"
1131 let fitmodel_of_string s =
1132 match String.lowercase s with
1133 | "width" -> FitWidth
1134 | "proportional" -> FitProportional
1135 | "page" -> FitPage
1136 | _ -> failwith "invalid fit model"
1139 let int_of_fitmodel = function
1140 | FitWidth -> 0
1141 | FitProportional -> 1
1142 | FitPage -> 2
1145 let fitmodel_of_int = function
1146 | 0 -> FitWidth
1147 | 1 -> FitProportional
1148 | 2 -> FitPage
1149 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1152 let fitmodel_to_string = function
1153 | FitWidth -> "width"
1154 | FitProportional -> "proportional"
1155 | FitPage -> "page"
1158 let intentry_with_suffix text key =
1159 let c =
1160 if key >= 32 && key < 127
1161 then Char.chr key
1162 else '\000'
1164 match Char.lowercase c with
1165 | '0' .. '9' ->
1166 let text = addchar text c in
1167 TEcont text
1169 | 'k' | 'm' | 'g' ->
1170 let text = addchar text c in
1171 TEcont text
1173 | _ ->
1174 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1175 TEcont text
1178 let multicolumns_to_string (n, a, b) =
1179 if a = 0 && b = 0
1180 then Printf.sprintf "%d" n
1181 else Printf.sprintf "%d,%d,%d" n a b;
1184 let multicolumns_of_string s =
1186 (int_of_string s, 0, 0)
1187 with _ ->
1188 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1189 if a > 1 || b > 1
1190 then failwith "subtly broken"; (n, a, b)
1194 let readcmd fd =
1195 let s = "xxxx" in
1196 let n = tempfailureretry (Unix.read fd s 0) 4 in
1197 if n != 4 then failwith "incomplete read(len)";
1198 let len = 0
1199 lor (Char.code s.[0] lsl 24)
1200 lor (Char.code s.[1] lsl 16)
1201 lor (Char.code s.[2] lsl 8)
1202 lor (Char.code s.[3] lsl 0)
1204 let s = String.create len in
1205 let n = tempfailureretry (Unix.read fd s 0) len in
1206 if n != len then failwith "incomplete read(data)";
1210 let btod b = if b then 1 else 0;;
1212 let wcmd fmt =
1213 let b = Buffer.create 16 in
1214 Buffer.add_string b "llll";
1215 Printf.kbprintf
1216 (fun b ->
1217 let s = Buffer.contents b in
1218 let n = String.length s in
1219 let len = n - 4 in
1220 (* dolog "wcmd %S" (String.sub s 4 len); *)
1221 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1222 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1223 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1224 s.[3] <- Char.chr (len land 0xff);
1225 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1226 if n' != n then failwith "write failed";
1227 ) b fmt;
1230 let calcips h =
1231 let d = state.winh - h in
1232 max conf.interpagespace ((d + 1) / 2)
1235 let rowyh (c, coverA, coverB) b n =
1236 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1237 then
1238 let _, _, vy, (_, _, h, _) = b.(n) in
1239 (vy, h)
1240 else
1241 let n' = n - coverA in
1242 let d = n' mod c in
1243 let s = n - d in
1244 let e = min state.pagecount (s + c) in
1245 let rec find m miny maxh = if m = e then miny, maxh else
1246 let _, _, y, (_, _, h, _) = b.(m) in
1247 let miny = min miny y in
1248 let maxh = max maxh h in
1249 find (m+1) miny maxh
1250 in find s max_int 0
1253 let calcheight () =
1254 match conf.columns with
1255 | Cmulti ((_, _, _) as cl, b) ->
1256 if Array.length b > 0
1257 then
1258 let y, h = rowyh cl b (Array.length b - 1) in
1259 y + h + (if conf.presentation then calcips h else 0)
1260 else 0
1261 | Csingle b ->
1262 if Array.length b > 0
1263 then
1264 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1265 y + h + (if conf.presentation then calcips h else 0)
1266 else 0
1267 | Csplit (_, b) ->
1268 if Array.length b > 0
1269 then
1270 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1271 y + h
1272 else 0
1275 let getpageyh pageno =
1276 let pageno = bound pageno 0 (state.pagecount-1) in
1277 match conf.columns with
1278 | Csingle b ->
1279 if Array.length b = 0
1280 then 0, 0
1281 else
1282 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1283 let y =
1284 if conf.presentation
1285 then y - calcips h
1286 else y
1288 y, h
1289 | Cmulti (cl, b) ->
1290 if Array.length b = 0
1291 then 0, 0
1292 else
1293 let y, h = rowyh cl b pageno in
1294 let y =
1295 if conf.presentation
1296 then y - calcips h
1297 else y
1299 y, h
1300 | Csplit (c, b) ->
1301 if Array.length b = 0
1302 then 0, 0
1303 else
1304 let n = pageno*c in
1305 let (_, _, y, (_, _, h, _)) = b.(n) in
1306 y, h
1309 let getpagedim pageno =
1310 let rec f ppdim l =
1311 match l with
1312 | (n, _, _, _) as pdim :: rest ->
1313 if n >= pageno
1314 then (if n = pageno then pdim else ppdim)
1315 else f pdim rest
1317 | [] -> ppdim
1319 f (-1, -1, -1, -1) state.pdims
1322 let getpagey pageno = fst (getpageyh pageno);;
1324 let nogeomcmds cmds =
1325 match cmds with
1326 | s, [] -> String.length s = 0
1327 | _ -> false
1330 let page_of_y y =
1331 let ((c, coverA, coverB) as cl), b =
1332 match conf.columns with
1333 | Csingle b -> (1, 0, 0), b
1334 | Cmulti (c, b) -> c, b
1335 | Csplit (_, b) -> (1, 0, 0), b
1337 if Array.length b = 0
1338 then -1
1339 else
1340 let rec bsearch nmin nmax =
1341 if nmin > nmax
1342 then bound nmin 0 (state.pagecount-1)
1343 else
1344 let n = (nmax + nmin) / 2 in
1345 let vy, h = rowyh cl b n in
1346 let y0, y1 =
1347 if conf.presentation
1348 then
1349 let ips = calcips h in
1350 let y0 = vy - ips in
1351 let y1 = vy + h + ips in
1352 y0, y1
1353 else (
1354 if n = 0
1355 then 0, vy + h + conf.interpagespace
1356 else
1357 let y0 = vy - conf.interpagespace in
1358 y0, y0 + h + conf.interpagespace
1361 if y >= y0 && y < y1
1362 then (
1363 if c = 1
1364 then n
1365 else (
1366 if n > coverA
1367 then
1368 if n < state.pagecount - coverB
1369 then ((n-coverA)/c)*c + coverA
1370 else n
1371 else n
1374 else (
1375 if y > y0
1376 then bsearch (n+1) nmax
1377 else bsearch nmin (n-1)
1380 let r = bsearch 0 (state.pagecount-1) in
1384 let layoutN ((columns, coverA, coverB), b) y sh =
1385 let sh = sh - (hscrollh ()) in
1386 let rec fold accu n =
1387 if n = Array.length b
1388 then accu
1389 else
1390 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1391 if (vy - y) > sh &&
1392 (n = coverA - 1
1393 || n = state.pagecount - coverB
1394 || (n - coverA) mod columns = columns - 1)
1395 then accu
1396 else
1397 let accu =
1398 if vy + h > y
1399 then
1400 let pagey = max 0 (y - vy) in
1401 let pagedispy = if pagey > 0 then 0 else vy - y in
1402 let pagedispx, pagex =
1403 let pdx =
1404 if n = coverA - 1 || n = state.pagecount - coverB
1405 then state.x + (wadjsb state.winw - w) / 2
1406 else dx + xoff + state.x
1408 if pdx < 0
1409 then 0, -pdx
1410 else pdx, 0
1412 let pagevw =
1413 let vw = wadjsb state.winw - pagedispx in
1414 let pw = w - pagex in
1415 min vw pw
1417 let pagevh = min (h - pagey) (sh - pagedispy) in
1418 if pagevw > 0 && pagevh > 0
1419 then
1420 let e =
1421 { pageno = n
1422 ; pagedimno = pdimno
1423 ; pagew = w
1424 ; pageh = h
1425 ; pagex = pagex
1426 ; pagey = pagey
1427 ; pagevw = pagevw
1428 ; pagevh = pagevh
1429 ; pagedispx = pagedispx
1430 ; pagedispy = pagedispy
1431 ; pagecol = 0
1434 e :: accu
1435 else
1436 accu
1437 else
1438 accu
1440 fold accu (n+1)
1442 List.rev (fold [] (page_of_y y));
1445 let layoutS (columns, b) y sh =
1446 let sh = sh - hscrollh () in
1447 let rec fold accu n =
1448 if n = Array.length b
1449 then accu
1450 else
1451 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1452 if (vy - y) > sh
1453 then accu
1454 else
1455 let accu =
1456 if vy + pageh > y
1457 then
1458 let x = xoff + state.x in
1459 let pagey = max 0 (y - vy) in
1460 let pagedispy = if pagey > 0 then 0 else vy - y in
1461 let pagedispx, pagex =
1462 if px = 0
1463 then (
1464 if x < 0
1465 then 0, -x
1466 else x, 0
1468 else (
1469 let px = px - x in
1470 if px < 0
1471 then -px, 0
1472 else 0, px
1475 let pagecolw = pagew/columns in
1476 let pagedispx =
1477 if pagecolw < state.winw
1478 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1479 else pagedispx
1481 let pagevw =
1482 let vw = wadjsb state.winw - pagedispx in
1483 let pw = pagew - pagex in
1484 min vw pw
1486 let pagevw = min pagevw pagecolw in
1487 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1488 if pagevw > 0 && pagevh > 0
1489 then
1490 let e =
1491 { pageno = n/columns
1492 ; pagedimno = pdimno
1493 ; pagew = pagew
1494 ; pageh = pageh
1495 ; pagex = pagex
1496 ; pagey = pagey
1497 ; pagevw = pagevw
1498 ; pagevh = pagevh
1499 ; pagedispx = pagedispx
1500 ; pagedispy = pagedispy
1501 ; pagecol = n mod columns
1504 e :: accu
1505 else
1506 accu
1507 else
1508 accu
1510 fold accu (n+1)
1512 List.rev (fold [] 0)
1515 let layout y sh =
1516 if nogeomcmds state.geomcmds
1517 then
1518 match conf.columns with
1519 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1520 | Cmulti c -> layoutN c y sh
1521 | Csplit s -> layoutS s y sh
1522 else []
1525 let clamp incr =
1526 let y = state.y + incr in
1527 let y = max 0 y in
1528 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1532 let itertiles l f =
1533 let tilex = l.pagex mod conf.tilew in
1534 let tiley = l.pagey mod conf.tileh in
1536 let col = l.pagex / conf.tilew in
1537 let row = l.pagey / conf.tileh in
1539 let rec rowloop row y0 dispy h =
1540 if h = 0
1541 then ()
1542 else (
1543 let dh = conf.tileh - y0 in
1544 let dh = min h dh in
1545 let rec colloop col x0 dispx w =
1546 if w = 0
1547 then ()
1548 else (
1549 let dw = conf.tilew - x0 in
1550 let dw = min w dw in
1552 f col row dispx dispy x0 y0 dw dh;
1553 colloop (col+1) 0 (dispx+dw) (w-dw)
1556 colloop col tilex l.pagedispx l.pagevw;
1557 rowloop (row+1) 0 (dispy+dh) (h-dh)
1560 if l.pagevw > 0 && l.pagevh > 0
1561 then rowloop row tiley l.pagedispy l.pagevh;
1564 let gettileopaque l col row =
1565 let key =
1566 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1568 try Some (Hashtbl.find state.tilemap key)
1569 with Not_found -> None
1572 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1573 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1574 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1577 let drawtiles l color =
1578 GlDraw.color color;
1579 let f col row x y tilex tiley w h =
1580 match gettileopaque l col row with
1581 | Some (opaque, _, t) ->
1582 let params = x, y, w, h, tilex, tiley in
1583 if conf.invert
1584 then (
1585 Gl.enable `blend;
1586 GlFunc.blend_func `zero `one_minus_src_color;
1588 drawtile params opaque;
1589 if conf.invert
1590 then Gl.disable `blend;
1591 if conf.debug
1592 then (
1593 let s = Printf.sprintf
1594 "%d[%d,%d] %f sec"
1595 l.pageno col row t
1597 let w = measurestr fstate.fontsize s in
1598 GlMisc.push_attrib [`current];
1599 GlDraw.color (0.0, 0.0, 0.0);
1600 GlDraw.rect
1601 (float (x-2), float (y-2))
1602 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1603 GlDraw.color (1.0, 1.0, 1.0);
1604 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1605 GlMisc.pop_attrib ();
1608 | _ ->
1609 let w =
1610 let lw = wadjsb state.winw - x in
1611 min lw w
1612 and h =
1613 let lh = state.winh - y in
1614 min lh h
1616 begin match state.texid with
1617 | Some id ->
1618 Gl.enable `texture_2d;
1619 GlTex.bind_texture `texture_2d id;
1620 let x0 = float x
1621 and y0 = float y
1622 and x1 = float (x+w)
1623 and y1 = float (y+h) in
1625 let tw = float w /. 16.0
1626 and th = float h /. 16.0 in
1627 let tx0 = float tilex /. 16.0
1628 and ty0 = float tiley /. 16.0 in
1629 let tx1 = tx0 +. tw
1630 and ty1 = ty0 +. th in
1631 GlDraw.begins `quads;
1632 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1633 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1634 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1635 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1636 GlDraw.ends ();
1638 Gl.disable `texture_2d;
1639 | None ->
1640 GlDraw.color (1.0, 1.0, 1.0);
1641 GlDraw.rect
1642 (float x, float y)
1643 (float (x+w), float (y+h));
1644 end;
1645 if w > 128 && h > fstate.fontsize + 10
1646 then (
1647 GlDraw.color (0.0, 0.0, 0.0);
1648 let c, r =
1649 if conf.verbose
1650 then (col*conf.tilew, row*conf.tileh)
1651 else col, row
1653 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1655 GlDraw.color color;
1657 itertiles l f
1660 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1662 let tilevisible1 l x y =
1663 let ax0 = l.pagex
1664 and ax1 = l.pagex + l.pagevw
1665 and ay0 = l.pagey
1666 and ay1 = l.pagey + l.pagevh in
1668 let bx0 = x
1669 and by0 = y in
1670 let bx1 = min (bx0 + conf.tilew) l.pagew
1671 and by1 = min (by0 + conf.tileh) l.pageh in
1673 let rx0 = max ax0 bx0
1674 and ry0 = max ay0 by0
1675 and rx1 = min ax1 bx1
1676 and ry1 = min ay1 by1 in
1678 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1679 nonemptyintersection
1682 let tilevisible layout n x y =
1683 let rec findpageinlayout m = function
1684 | l :: rest when l.pageno = n ->
1685 tilevisible1 l x y || (
1686 match conf.columns with
1687 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1688 | _ -> false
1690 | _ :: rest -> findpageinlayout 0 rest
1691 | [] -> false
1693 findpageinlayout 0 layout;
1696 let tileready l x y =
1697 tilevisible1 l x y &&
1698 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1701 let tilepage n p layout =
1702 let rec loop = function
1703 | l :: rest ->
1704 if l.pageno = n
1705 then
1706 let f col row _ _ _ _ _ _ =
1707 if state.currently = Idle
1708 then
1709 match gettileopaque l col row with
1710 | Some _ -> ()
1711 | None ->
1712 let x = col*conf.tilew
1713 and y = row*conf.tileh in
1714 let w =
1715 let w = l.pagew - x in
1716 min w conf.tilew
1718 let h =
1719 let h = l.pageh - y in
1720 min h conf.tileh
1722 let pbo =
1723 if conf.usepbo
1724 then getpbo w h conf.colorspace
1725 else "0"
1727 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1728 state.currently <-
1729 Tiling (
1730 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1731 conf.tilew, conf.tileh
1734 itertiles l f;
1735 else
1736 loop rest
1738 | [] -> ()
1740 if nogeomcmds state.geomcmds
1741 then loop layout;
1744 let preloadlayout y =
1745 let y = if y < state.winh then 0 else y - state.winh in
1746 let h = state.winh*3 in
1747 layout y h;
1750 let load pages =
1751 let rec loop pages =
1752 if state.currently != Idle
1753 then ()
1754 else
1755 match pages with
1756 | l :: rest ->
1757 begin match getopaque l.pageno with
1758 | None ->
1759 wcmd "page %d %d" l.pageno l.pagedimno;
1760 state.currently <- Loading (l, state.gen);
1761 | Some opaque ->
1762 tilepage l.pageno opaque pages;
1763 loop rest
1764 end;
1765 | _ -> ()
1767 if nogeomcmds state.geomcmds
1768 then loop pages
1771 let preload pages =
1772 load pages;
1773 if conf.preload && state.currently = Idle
1774 then load (preloadlayout state.y);
1777 let layoutready layout =
1778 let rec fold all ls =
1779 all && match ls with
1780 | l :: rest ->
1781 let seen = ref false in
1782 let allvisible = ref true in
1783 let foo col row _ _ _ _ _ _ =
1784 seen := true;
1785 allvisible := !allvisible &&
1786 begin match gettileopaque l col row with
1787 | Some _ -> true
1788 | None -> false
1791 itertiles l foo;
1792 fold (!seen && !allvisible) rest
1793 | [] -> true
1795 let alltilesvisible = fold true layout in
1796 alltilesvisible;
1799 let gotoy y =
1800 let y = bound y 0 state.maxy in
1801 let y, layout, proceed =
1802 match conf.maxwait with
1803 | Some time when state.ghyll == noghyll ->
1804 begin match state.throttle with
1805 | None ->
1806 let layout = layout y state.winh in
1807 let ready = layoutready layout in
1808 if not ready
1809 then (
1810 load layout;
1811 state.throttle <- Some (layout, y, now ());
1813 else G.postRedisplay "gotoy showall (None)";
1814 y, layout, ready
1815 | Some (_, _, started) ->
1816 let dt = now () -. started in
1817 if dt > time
1818 then (
1819 state.throttle <- None;
1820 let layout = layout y state.winh in
1821 load layout;
1822 G.postRedisplay "maxwait";
1823 y, layout, true
1825 else -1, [], false
1828 | _ ->
1829 let layout = layout y state.winh in
1830 if not !wtmode || layoutready layout
1831 then G.postRedisplay "gotoy ready";
1832 y, layout, true
1834 if proceed
1835 then (
1836 state.y <- y;
1837 state.layout <- layout;
1838 begin match state.mode with
1839 | LinkNav (Ltexact (pageno, linkno)) ->
1840 let rec loop = function
1841 | [] ->
1842 state.mode <- LinkNav (Ltgendir 0)
1843 | l :: _ when l.pageno = pageno ->
1844 begin match getopaque pageno with
1845 | None ->
1846 state.mode <- LinkNav (Ltgendir 0)
1847 | Some opaque ->
1848 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1849 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1850 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1851 then state.mode <- LinkNav (Ltgendir 0)
1853 | _ :: rest -> loop rest
1855 loop layout
1856 | _ -> ()
1857 end;
1858 begin match state.mode with
1859 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1860 if not (pagevisible layout pageno)
1861 then (
1862 match state.layout with
1863 | [] -> ()
1864 | l :: _ ->
1865 state.mode <- Birdseye (
1866 conf, leftx, l.pageno, hooverpageno, anchor
1869 | LinkNav (Ltgendir dir as lt) ->
1870 let linknav =
1871 let rec loop = function
1872 | [] -> lt
1873 | l :: rest ->
1874 match getopaque l.pageno with
1875 | None -> loop rest
1876 | Some opaque ->
1877 let link =
1878 let ld =
1879 if dir = 0
1880 then LDfirstvisible (l.pagex, l.pagey, dir)
1881 else (
1882 if dir > 0 then LDfirst else LDlast
1885 findlink opaque ld
1887 match link with
1888 | Lnotfound -> loop rest
1889 | Lfound n ->
1890 showlinktype (getlink opaque n);
1891 Ltexact (l.pageno, n)
1893 loop state.layout
1895 state.mode <- LinkNav linknav
1896 | _ -> ()
1897 end;
1898 preload layout;
1900 state.ghyll <- noghyll;
1901 if conf.updatecurs
1902 then (
1903 let mx, my = state.mpos in
1904 updateunder mx my;
1908 let conttiling pageno opaque =
1909 tilepage pageno opaque
1910 (if conf.preload then preloadlayout state.y else state.layout)
1913 let gotoy_and_clear_text y =
1914 if not conf.verbose then state.text <- "";
1915 gotoy y;
1918 let getanchor1 l =
1919 let top =
1920 let coloff = l.pagecol * l.pageh in
1921 float (l.pagey + coloff) /. float l.pageh
1923 let dtop =
1924 if l.pagedispy = 0
1925 then
1927 else (
1928 if conf.presentation
1929 then float l.pagedispy /. float (calcips l.pageh)
1930 else float l.pagedispy /. float conf.interpagespace
1933 (l.pageno, top, dtop)
1936 let getanchor () =
1937 match state.layout with
1938 | l :: _ -> getanchor1 l
1939 | [] ->
1940 let n = page_of_y state.y in
1941 if n = -1
1942 then state.anchor
1943 else
1944 let y, h = getpageyh n in
1945 let dy = y - state.y in
1946 let dtop =
1947 if conf.presentation
1948 then
1949 let ips = calcips h in
1950 float (dy + ips) /. float ips
1951 else
1952 float dy /. float conf.interpagespace
1954 (n, 0.0, dtop)
1957 let getanchory (n, top, dtop) =
1958 let y, h = getpageyh n in
1959 if conf.presentation
1960 then
1961 let ips = calcips h in
1962 y + truncate (top*.float h -. dtop*.float ips) + ips;
1963 else
1964 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1967 let gotoanchor anchor =
1968 gotoy (getanchory anchor);
1971 let addnav () =
1972 cbput state.hists.nav (getanchor ());
1975 let getnav dir =
1976 let anchor = cbgetc state.hists.nav dir in
1977 getanchory anchor;
1980 let gotoghyll y =
1981 let scroll f n a b =
1982 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1983 let snake f a b =
1984 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1985 if f < a
1986 then s (float f /. float a)
1987 else (
1988 if f > b
1989 then 1.0 -. s ((float (f-b) /. float (n-b)))
1990 else 1.0
1993 snake f a b
1994 and summa f n a b =
1995 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1996 let iv x = x**3.-.0.5*.x**4. in
1997 let iv1 = iv f in
1998 let ins = float a *. iv1
1999 and outs = float (n-b) *. iv1 in
2000 let ones = b - a in
2001 ins +. outs +. float ones
2003 let rec set (_N, _A, _B) y sy =
2004 let sum = summa 1.0 _N _A _B in
2005 let dy = float (y - sy) in
2006 state.ghyll <- (
2007 let rec gf n y1 o =
2008 if n >= _N
2009 then state.ghyll <- noghyll
2010 else
2011 let go n =
2012 let s = scroll n _N _A _B in
2013 let y1 = y1 +. ((s *. dy) /. sum) in
2014 gotoy_and_clear_text (truncate y1);
2015 state.ghyll <- gf (n+1) y1;
2017 match o with
2018 | None -> go n
2019 | Some y' -> set (_N/2, 1, 1) y' state.y
2021 gf 0 (float state.y)
2024 match conf.ghyllscroll with
2025 | None ->
2026 gotoy_and_clear_text y
2027 | Some nab ->
2028 if state.ghyll == noghyll
2029 then set nab y state.y
2030 else state.ghyll (Some y)
2033 let gotopage n top =
2034 let y, h = getpageyh n in
2035 let y = y + (truncate (top *. float h)) in
2036 gotoghyll y
2039 let gotopage1 n top =
2040 let y = getpagey n in
2041 let y = y + top in
2042 gotoghyll y
2045 let invalidate s f =
2046 state.layout <- [];
2047 state.pdims <- [];
2048 state.rects <- [];
2049 state.rects1 <- [];
2050 match state.geomcmds with
2051 | ps, [] when String.length ps = 0 ->
2052 f ();
2053 state.geomcmds <- s, [];
2055 | ps, [] ->
2056 state.geomcmds <- ps, [s, f];
2058 | ps, (s', _) :: rest when s' = s ->
2059 state.geomcmds <- ps, ((s, f) :: rest);
2061 | ps, cmds ->
2062 state.geomcmds <- ps, ((s, f) :: cmds);
2065 let flushpages () =
2066 Hashtbl.iter (fun _ opaque ->
2067 wcmd "freepage %s" opaque;
2068 ) state.pagemap;
2069 Hashtbl.clear state.pagemap;
2072 let flushtiles () =
2073 if not (Queue.is_empty state.tilelru)
2074 then (
2075 Queue.iter (fun (k, p, s) ->
2076 wcmd "freetile %s" p;
2077 state.memused <- state.memused - s;
2078 Hashtbl.remove state.tilemap k;
2079 ) state.tilelru;
2080 state.uioh#infochanged Memused;
2081 Queue.clear state.tilelru;
2083 load state.layout;
2086 let stateh h =
2087 let h = truncate (float h*.conf.zoom) in
2088 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2089 h - d
2092 let opendoc path password =
2093 state.path <- path;
2094 state.password <- password;
2095 state.gen <- state.gen + 1;
2096 state.docinfo <- [];
2098 flushpages ();
2099 setaalevel conf.aalevel;
2100 let titlepath =
2101 if String.length state.origin = 0
2102 then path
2103 else state.origin
2105 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2106 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2107 invalidate "reqlayout"
2108 (fun () ->
2109 wcmd "reqlayout %d %d %d %s\000"
2110 conf.angle (int_of_fitmodel conf.fitmodel)
2111 (stateh state.winh) state.nameddest
2115 let reload () =
2116 state.anchor <- getanchor ();
2117 opendoc state.path state.password;
2120 let scalecolor c =
2121 let c = c *. conf.colorscale in
2122 (c, c, c);
2125 let scalecolor2 (r, g, b) =
2126 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2129 let docolumns = function
2130 | Csingle _ ->
2131 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2132 let rec loop pageno pdimno pdim y ph pdims =
2133 if pageno = state.pagecount
2134 then ()
2135 else
2136 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2137 match pdims with
2138 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2139 pdimno+1, pdim, rest
2140 | _ ->
2141 pdimno, pdim, pdims
2143 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2144 let y = y +
2145 (if conf.presentation
2146 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2147 else (if pageno = 0 then 0 else conf.interpagespace)
2150 a.(pageno) <- (pdimno, x, y, pdim);
2151 loop (pageno+1) pdimno pdim (y + h) h pdims
2153 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2154 conf.columns <- Csingle a;
2156 | Cmulti ((columns, coverA, coverB), _) ->
2157 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2158 let rec loop pageno pdimno pdim x y rowh pdims =
2159 let rec fixrow m = if m = pageno then () else
2160 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2161 if h < rowh
2162 then (
2163 let y = y + (rowh - h) / 2 in
2164 a.(m) <- (pdimno, x, y, pdim);
2166 fixrow (m+1)
2168 if pageno = state.pagecount
2169 then fixrow (((pageno - 1) / columns) * columns)
2170 else
2171 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2172 match pdims with
2173 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2174 pdimno+1, pdim, rest
2175 | _ ->
2176 pdimno, pdim, pdims
2178 let x, y, rowh' =
2179 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2180 then (
2181 let x = (wadjsb state.winw - w) / 2 in
2182 let ips =
2183 if conf.presentation then calcips h else conf.interpagespace in
2184 x, y + ips + rowh, h
2186 else (
2187 if (pageno - coverA) mod columns = 0
2188 then (
2189 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2190 let y =
2191 if conf.presentation
2192 then
2193 let ips = calcips h in
2194 y + (if pageno = 0 then 0 else calcips rowh + ips)
2195 else
2196 y + (if pageno = 0 then 0 else conf.interpagespace)
2198 x, y + rowh, h
2200 else x, y, max rowh h
2203 let y =
2204 if pageno > 1 && (pageno - coverA) mod columns = 0
2205 then (
2206 let y =
2207 if pageno = columns && conf.presentation
2208 then (
2209 let ips = calcips rowh in
2210 for i = 0 to pred columns
2212 let (pdimno, x, y, pdim) = a.(i) in
2213 a.(i) <- (pdimno, x, y+ips, pdim)
2214 done;
2215 y+ips;
2217 else y
2219 fixrow (pageno - columns);
2222 else y
2224 a.(pageno) <- (pdimno, x, y, pdim);
2225 let x = x + w + xoff*2 + conf.interpagespace in
2226 loop (pageno+1) pdimno pdim x y rowh' pdims
2228 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2229 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2231 | Csplit (c, _) ->
2232 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2233 let rec loop pageno pdimno pdim y pdims =
2234 if pageno = state.pagecount
2235 then ()
2236 else
2237 let pdimno, ((_, w, h, _) as pdim), pdims =
2238 match pdims with
2239 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2240 pdimno+1, pdim, rest
2241 | _ ->
2242 pdimno, pdim, pdims
2244 let cw = w / c in
2245 let rec loop1 n x y =
2246 if n = c then y else (
2247 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2248 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2251 let y = loop1 0 0 y in
2252 loop (pageno+1) pdimno pdim y pdims
2254 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2255 conf.columns <- Csplit (c, a);
2258 let represent () =
2259 docolumns conf.columns;
2260 state.maxy <- calcheight ();
2261 if state.reprf == noreprf
2262 then (
2263 match state.mode with
2264 | Birdseye (_, _, pageno, _, _) ->
2265 let y, h = getpageyh pageno in
2266 let top = (state.winh - h) / 2 in
2267 gotoy (max 0 (y - top))
2268 | _ -> gotoanchor state.anchor
2270 else (
2271 state.reprf ();
2272 state.reprf <- noreprf;
2276 let reshape w h =
2277 GlDraw.viewport 0 0 w h;
2278 let firsttime = state.geomcmds == firstgeomcmds in
2279 if not firsttime && nogeomcmds state.geomcmds
2280 then state.anchor <- getanchor ();
2282 state.winw <- w;
2283 let w = wadjsb (truncate (float w *. conf.zoom)) in
2284 let w = max w 2 in
2285 state.winh <- h;
2286 setfontsize fstate.fontsize;
2287 GlMat.mode `modelview;
2288 GlMat.load_identity ();
2290 GlMat.mode `projection;
2291 GlMat.load_identity ();
2292 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2293 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2294 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2296 let relx =
2297 if conf.zoom <= 1.0
2298 then 0.0
2299 else float state.x /. float state.w
2301 invalidate "geometry"
2302 (fun () ->
2303 state.w <- w;
2304 if not firsttime
2305 then state.x <- truncate (relx *. float w);
2306 let w =
2307 match conf.columns with
2308 | Csingle _ -> w
2309 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2310 | Csplit (c, _) -> w * c
2312 wcmd "geometry %d %d %d"
2313 w (stateh h) (int_of_fitmodel conf.fitmodel)
2317 let enttext () =
2318 let len = String.length state.text in
2319 let drawstring s =
2320 let hscrollh =
2321 match state.mode with
2322 | Textentry _ | View | LinkNav _ ->
2323 let h, _, _ = state.uioh#scrollpw in
2325 | _ -> 0
2327 let rect x w =
2328 GlDraw.rect
2329 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2330 (x+.w, float (state.winh - hscrollh))
2333 let w = float (wadjsb state.winw - 1) in
2334 if state.progress >= 0.0 && state.progress < 1.0
2335 then (
2336 GlDraw.color (0.3, 0.3, 0.3);
2337 let w1 = w *. state.progress in
2338 rect 0.0 w1;
2339 GlDraw.color (0.0, 0.0, 0.0);
2340 rect w1 (w-.w1)
2342 else (
2343 GlDraw.color (0.0, 0.0, 0.0);
2344 rect 0.0 w;
2347 GlDraw.color (1.0, 1.0, 1.0);
2348 drawstring fstate.fontsize
2349 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2351 let s =
2352 match state.mode with
2353 | Textentry ((prefix, text, _, _, _, _), _) ->
2354 let s =
2355 if len > 0
2356 then
2357 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2358 else
2359 Printf.sprintf "%s%s_" prefix text
2363 | _ -> state.text
2365 let s =
2366 if state.newerrmsgs
2367 then (
2368 if not (istextentry state.mode) && state.uioh#eformsgs
2369 then
2370 let s1 = "(press 'e' to review error messasges)" in
2371 if String.length s > 0 then s ^ " " ^ s1 else s1
2372 else s
2374 else s
2376 if String.length s > 0
2377 then drawstring s
2380 let gctiles () =
2381 let len = Queue.length state.tilelru in
2382 let layout = lazy (
2383 match state.throttle with
2384 | None ->
2385 if conf.preload
2386 then preloadlayout state.y
2387 else state.layout
2388 | Some (layout, _, _) ->
2389 layout
2390 ) in
2391 let rec loop qpos =
2392 if state.memused <= conf.memlimit
2393 then ()
2394 else (
2395 if qpos < len
2396 then
2397 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2398 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2399 let (_, pw, ph, _) = getpagedim n in
2401 gen = state.gen
2402 && colorspace = conf.colorspace
2403 && angle = conf.angle
2404 && pagew = pw
2405 && pageh = ph
2406 && (
2407 let x = col*conf.tilew
2408 and y = row*conf.tileh in
2409 tilevisible (Lazy.force_val layout) n x y
2411 then Queue.push lruitem state.tilelru
2412 else (
2413 freepbo p;
2414 wcmd "freetile %s" p;
2415 state.memused <- state.memused - s;
2416 state.uioh#infochanged Memused;
2417 Hashtbl.remove state.tilemap k;
2419 loop (qpos+1)
2422 loop 0
2425 let logcurrently = function
2426 | Idle -> dolog "Idle"
2427 | Loading (l, gen) ->
2428 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2429 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2430 dolog
2431 "Tiling %d[%d,%d] page=%s cs=%s angle"
2432 l.pageno col row pageopaque
2433 (colorspace_to_string colorspace)
2435 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2436 angle gen conf.angle state.gen
2437 tilew tileh
2438 conf.tilew conf.tileh
2440 | Outlining _ ->
2441 dolog "outlining"
2444 let splitatspace =
2445 let r = Str.regexp " " in
2446 fun s -> Str.bounded_split r s 2;
2449 let onpagerect pageno f =
2450 let b =
2451 match conf.columns with
2452 | Cmulti (_, b) -> b
2453 | Csingle b -> b
2454 | Csplit (_, b) -> b
2456 if pageno >= 0 && pageno < Array.length b
2457 then
2458 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2459 let r = getpdimrect pdimno in
2460 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2463 let gotopagexy1 pageno x y =
2464 onpagerect pageno (fun w h ->
2465 let top = y /. h in
2466 let _,w1,_,leftx = getpagedim pageno in
2467 let wh = state.winh - hscrollh () in
2468 let sw = float w1 /. w in
2469 let x = sw *. x in
2470 let x = leftx + state.x + truncate x in
2471 let sx =
2472 if x < 0 || x >= wadjsb state.winw
2473 then state.x - x
2474 else state.x
2476 let py, h = getpageyh pageno in
2477 let pdy = truncate (top *. float h) in
2478 let y' = py + pdy in
2479 let dy = y' - state.y in
2480 let sy =
2481 if x != state.x || not (dy > 0 && dy < wh)
2482 then (
2483 if conf.presentation
2484 then
2485 if abs (py - y') > wh
2486 then y'
2487 else py
2488 else y';
2490 else state.y
2492 if state.x != sx || state.y != sy
2493 then (
2494 let x, y =
2495 if !wtmode
2496 then (
2497 let ww = wadjsb state.winw in
2498 let qx = sx / ww
2499 and qy = pdy / wh in
2500 let x = qx * ww
2501 and y = py + qy * wh in
2502 let x = if -x + ww > w1 then -(w1-ww) else x
2503 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2504 let y =
2505 if conf.presentation
2506 then
2507 if abs (py - y') > wh
2508 then y'
2509 else py
2510 else y';
2512 (x, y)
2514 else (sx, sy)
2516 state.x <- x;
2517 gotoy_and_clear_text y;
2519 else gotoy_and_clear_text state.y;
2523 let gotopagexy pageno x y =
2524 match state.mode with
2525 | Birdseye _ -> gotopage pageno 0.0
2526 | _ -> gotopagexy1 pageno x y
2529 let act cmds =
2530 (* dolog "%S" cmds; *)
2531 let cl = splitatspace cmds in
2532 let scan s fmt f =
2533 try Scanf.sscanf s fmt f
2534 with exn ->
2535 dolog "error processing '%S': %s" cmds (exntos exn);
2536 exit 1
2538 match cl with
2539 | "clear" :: [] ->
2540 state.uioh#infochanged Pdim;
2541 state.pdims <- [];
2543 | "clearrects" :: [] ->
2544 state.rects <- state.rects1;
2545 G.postRedisplay "clearrects";
2547 | "continue" :: args :: [] ->
2548 let n = scan args "%u" (fun n -> n) in
2549 state.pagecount <- n;
2550 begin match state.currently with
2551 | Outlining l ->
2552 state.currently <- Idle;
2553 state.outlines <- Array.of_list (List.rev l)
2554 | _ -> ()
2555 end;
2557 let cur, cmds = state.geomcmds in
2558 if String.length cur = 0
2559 then failwith "umpossible";
2561 begin match List.rev cmds with
2562 | [] ->
2563 state.geomcmds <- "", [];
2564 represent ();
2565 | (s, f) :: rest ->
2566 f ();
2567 state.geomcmds <- s, List.rev rest;
2568 end;
2569 if conf.maxwait = None && not !wtmode
2570 then G.postRedisplay "continue";
2572 | "title" :: args :: [] ->
2573 Wsi.settitle args
2575 | "msg" :: args :: [] ->
2576 showtext ' ' args
2578 | "vmsg" :: args :: [] ->
2579 if conf.verbose
2580 then showtext ' ' args
2582 | "emsg" :: args :: [] ->
2583 Buffer.add_string state.errmsgs args;
2584 state.newerrmsgs <- true;
2585 G.postRedisplay "error message"
2587 | "progress" :: args :: [] ->
2588 let progress, text =
2589 scan args "%f %n"
2590 (fun f pos ->
2591 f, String.sub args pos (String.length args - pos))
2593 state.text <- text;
2594 state.progress <- progress;
2595 G.postRedisplay "progress"
2597 | "firstmatch" :: args :: [] ->
2598 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2599 scan args "%u %d %f %f %f %f %f %f %f %f"
2600 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2601 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2603 let y = (getpagey pageno) + truncate y0 in
2604 addnav ();
2605 gotoy y;
2606 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2608 | "match" :: args :: [] ->
2609 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2610 scan args "%u %d %f %f %f %f %f %f %f %f"
2611 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2612 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2614 state.rects1 <-
2615 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2617 | "page" :: args :: [] ->
2618 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2619 begin match state.currently with
2620 | Loading (l, gen) ->
2621 vlog "page %d took %f sec" l.pageno t;
2622 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2623 begin match state.throttle with
2624 | None ->
2625 let preloadedpages =
2626 if conf.preload
2627 then preloadlayout state.y
2628 else state.layout
2630 let evict () =
2631 let set =
2632 List.fold_left (fun s l -> IntSet.add l.pageno s)
2633 IntSet.empty preloadedpages
2635 let evictedpages =
2636 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2637 if not (IntSet.mem pageno set)
2638 then (
2639 wcmd "freepage %s" opaque;
2640 key :: accu
2642 else accu
2643 ) state.pagemap []
2645 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2647 evict ();
2648 state.currently <- Idle;
2649 if gen = state.gen
2650 then (
2651 tilepage l.pageno pageopaque state.layout;
2652 load state.layout;
2653 load preloadedpages;
2654 if pagevisible state.layout l.pageno
2655 && layoutready state.layout
2656 then G.postRedisplay "page";
2659 | Some (layout, _, _) ->
2660 state.currently <- Idle;
2661 tilepage l.pageno pageopaque layout;
2662 load state.layout
2663 end;
2665 | _ ->
2666 dolog "Inconsistent loading state";
2667 logcurrently state.currently;
2668 exit 1
2671 | "tile" :: args :: [] ->
2672 let (x, y, opaque, size, t) =
2673 scan args "%u %u %s %u %f"
2674 (fun x y p size t -> (x, y, p, size, t))
2676 begin match state.currently with
2677 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2678 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2680 unmappbo opaque;
2681 if tilew != conf.tilew || tileh != conf.tileh
2682 then (
2683 wcmd "freetile %s" opaque;
2684 state.currently <- Idle;
2685 load state.layout;
2687 else (
2688 puttileopaque l col row gen cs angle opaque size t;
2689 state.memused <- state.memused + size;
2690 state.uioh#infochanged Memused;
2691 gctiles ();
2692 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2693 opaque, size) state.tilelru;
2695 let layout =
2696 match state.throttle with
2697 | None -> state.layout
2698 | Some (layout, _, _) -> layout
2701 state.currently <- Idle;
2702 if gen = state.gen
2703 && conf.colorspace = cs
2704 && conf.angle = angle
2705 && tilevisible layout l.pageno x y
2706 then conttiling l.pageno pageopaque;
2708 begin match state.throttle with
2709 | None ->
2710 preload state.layout;
2711 if gen = state.gen
2712 && conf.colorspace = cs
2713 && conf.angle = angle
2714 && tilevisible state.layout l.pageno x y
2715 && (not !wtmode || layoutready state.layout)
2716 then G.postRedisplay "tile nothrottle";
2718 | Some (layout, y, _) ->
2719 let ready = layoutready layout in
2720 if ready
2721 then (
2722 state.y <- y;
2723 state.layout <- layout;
2724 state.throttle <- None;
2725 G.postRedisplay "throttle";
2727 else load layout;
2728 end;
2731 | _ ->
2732 dolog "Inconsistent tiling state";
2733 logcurrently state.currently;
2734 exit 1
2737 | "pdim" :: args :: [] ->
2738 let (n, w, h, _) as pdim =
2739 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2741 let pdim =
2742 match conf.fitmodel, conf.columns with
2743 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2744 | _ -> pdim
2746 state.uioh#infochanged Pdim;
2747 state.pdims <- pdim :: state.pdims
2749 | "o" :: args :: [] ->
2750 let (l, n, t, h, pos) =
2751 scan args "%u %u %d %u %n"
2752 (fun l n t h pos -> l, n, t, h, pos)
2754 let s = String.sub args pos (String.length args - pos) in
2755 let outline = (s, l, (n, float t /. float h, 0.0)) in
2756 begin match state.currently with
2757 | Outlining outlines ->
2758 state.currently <- Outlining (outline :: outlines)
2759 | Idle ->
2760 state.currently <- Outlining [outline]
2761 | currently ->
2762 dolog "invalid outlining state";
2763 logcurrently currently
2766 | "a" :: args :: [] ->
2767 let (n, l, t) =
2768 scan args "%u %d %d" (fun n l t -> n, l, t)
2770 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2772 | "info" :: args :: [] ->
2773 state.docinfo <- (1, args) :: state.docinfo
2775 | "infoend" :: [] ->
2776 state.uioh#infochanged Docinfo;
2777 state.docinfo <- List.rev state.docinfo
2779 | _ ->
2780 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2783 let onhist cb =
2784 let rc = cb.rc in
2785 let action = function
2786 | HCprev -> cbget cb ~-1
2787 | HCnext -> cbget cb 1
2788 | HCfirst -> cbget cb ~-(cb.rc)
2789 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2790 and cancel () = cb.rc <- rc
2791 in (action, cancel)
2794 let search pattern forward =
2795 match conf.columns with
2796 | Csplit _ ->
2797 showtext '!' "searching does not work properly in split columns mode"
2798 | _ ->
2799 if String.length pattern > 0
2800 then
2801 let pn, py =
2802 match state.layout with
2803 | [] -> 0, 0
2804 | l :: _ ->
2805 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2807 wcmd "search %d %d %d %d,%s\000"
2808 (btod conf.icase) pn py (btod forward) pattern;
2811 let intentry text key =
2812 let c =
2813 if key >= 32 && key < 127
2814 then Char.chr key
2815 else '\000'
2817 match c with
2818 | '0' .. '9' ->
2819 let text = addchar text c in
2820 TEcont text
2822 | _ ->
2823 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2824 TEcont text
2827 let linknentry text key =
2828 let c =
2829 if key >= 32 && key < 127
2830 then Char.chr key
2831 else '\000'
2833 match c with
2834 | 'a' .. 'z' ->
2835 let text = addchar text c in
2836 TEcont text
2838 | _ ->
2839 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2840 TEcont text
2843 let linkndone f s =
2844 if String.length s > 0
2845 then (
2846 let n =
2847 let l = String.length s in
2848 let rec loop pos n = if pos = l then n else
2849 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2850 loop (pos+1) (n*26 + m)
2851 in loop 0 0
2853 let rec loop n = function
2854 | [] -> ()
2855 | l :: rest ->
2856 match getopaque l.pageno with
2857 | None -> loop n rest
2858 | Some opaque ->
2859 let m = getlinkcount opaque in
2860 if n < m
2861 then (
2862 let under = getlink opaque n in
2863 f under
2865 else loop (n-m) rest
2867 loop n state.layout;
2871 let textentry text key =
2872 if key land 0xff00 = 0xff00
2873 then TEcont text
2874 else TEcont (text ^ toutf8 key)
2877 let reqlayout angle fitmodel =
2878 match state.throttle with
2879 | None ->
2880 if nogeomcmds state.geomcmds
2881 then state.anchor <- getanchor ();
2882 conf.angle <- angle mod 360;
2883 if conf.angle != 0
2884 then (
2885 match state.mode with
2886 | LinkNav _ -> state.mode <- View
2887 | _ -> ()
2889 conf.fitmodel <- fitmodel;
2890 invalidate "reqlayout"
2891 (fun () ->
2892 wcmd "reqlayout %d %d %d"
2893 conf.angle (int_of_fitmodel conf.fitmodel) (stateh state.winh)
2895 | _ -> ()
2898 let settrim trimmargins trimfuzz =
2899 if nogeomcmds state.geomcmds
2900 then state.anchor <- getanchor ();
2901 conf.trimmargins <- trimmargins;
2902 conf.trimfuzz <- trimfuzz;
2903 let x0, y0, x1, y1 = trimfuzz in
2904 invalidate "settrim"
2905 (fun () ->
2906 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2907 flushpages ();
2910 let setzoom zoom =
2911 match state.throttle with
2912 | None ->
2913 let zoom = max 0.0001 zoom in
2914 if zoom <> conf.zoom
2915 then (
2916 state.prevzoom <- conf.zoom;
2917 conf.zoom <- zoom;
2918 reshape state.winw state.winh;
2919 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2922 | Some (layout, y, started) ->
2923 let time =
2924 match conf.maxwait with
2925 | None -> 0.0
2926 | Some t -> t
2928 let dt = now () -. started in
2929 if dt > time
2930 then (
2931 state.y <- y;
2932 load layout;
2936 let setcolumns mode columns coverA coverB =
2937 state.prevcolumns <- Some (conf.columns, conf.zoom);
2938 if columns < 0
2939 then (
2940 if isbirdseye mode
2941 then showtext '!' "split mode doesn't work in bird's eye"
2942 else (
2943 conf.columns <- Csplit (-columns, [||]);
2944 state.x <- 0;
2945 conf.zoom <- 1.0;
2948 else (
2949 if columns < 2
2950 then (
2951 conf.columns <- Csingle [||];
2952 state.x <- 0;
2953 setzoom 1.0;
2955 else (
2956 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2957 conf.zoom <- 1.0;
2960 reshape state.winw state.winh;
2963 let enterbirdseye () =
2964 let zoom = float conf.thumbw /. float state.winw in
2965 let birdseyepageno =
2966 let cy = state.winh / 2 in
2967 let fold = function
2968 | [] -> 0
2969 | l :: rest ->
2970 let rec fold best = function
2971 | [] -> best.pageno
2972 | l :: rest ->
2973 let d = cy - (l.pagedispy + l.pagevh/2)
2974 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2975 if abs d < abs dbest
2976 then fold l rest
2977 else best.pageno
2978 in fold l rest
2980 fold state.layout
2982 state.mode <- Birdseye (
2983 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2985 conf.zoom <- zoom;
2986 conf.presentation <- false;
2987 conf.interpagespace <- 10;
2988 conf.hlinks <- false;
2989 conf.fitmodel <- FitProportional;
2990 state.x <- 0;
2991 state.mstate <- Mnone;
2992 conf.maxwait <- None;
2993 conf.columns <- (
2994 match conf.beyecolumns with
2995 | Some c ->
2996 conf.zoom <- 1.0;
2997 Cmulti ((c, 0, 0), [||])
2998 | None -> Csingle [||]
3000 Wsi.setcursor Wsi.CURSOR_INHERIT;
3001 if conf.verbose
3002 then
3003 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3004 (100.0*.zoom)
3005 else
3006 state.text <- ""
3008 reshape state.winw state.winh;
3011 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3012 state.mode <- View;
3013 conf.zoom <- c.zoom;
3014 conf.presentation <- c.presentation;
3015 conf.interpagespace <- c.interpagespace;
3016 conf.maxwait <- c.maxwait;
3017 conf.hlinks <- c.hlinks;
3018 conf.fitmodel <- c.fitmodel;
3019 conf.beyecolumns <- (
3020 match conf.columns with
3021 | Cmulti ((c, _, _), _) -> Some c
3022 | Csingle _ -> None
3023 | Csplit _ -> failwith "leaving bird's eye split mode"
3025 conf.columns <- (
3026 match c.columns with
3027 | Cmulti (c, _) -> Cmulti (c, [||])
3028 | Csingle _ -> Csingle [||]
3029 | Csplit (c, _) -> Csplit (c, [||])
3031 state.x <- leftx;
3032 if conf.verbose
3033 then
3034 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3035 (100.0*.conf.zoom)
3037 reshape state.winw state.winh;
3038 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3041 let togglebirdseye () =
3042 match state.mode with
3043 | Birdseye vals -> leavebirdseye vals true
3044 | View -> enterbirdseye ()
3045 | _ -> ()
3048 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3049 let pageno = max 0 (pageno - incr) in
3050 let rec loop = function
3051 | [] -> gotopage1 pageno 0
3052 | l :: _ when l.pageno = pageno ->
3053 if l.pagedispy >= 0 && l.pagey = 0
3054 then G.postRedisplay "upbirdseye"
3055 else gotopage1 pageno 0
3056 | _ :: rest -> loop rest
3058 loop state.layout;
3059 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3062 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3063 let pageno = min (state.pagecount - 1) (pageno + incr) in
3064 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3065 let rec loop = function
3066 | [] ->
3067 let y, h = getpageyh pageno in
3068 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3069 gotoy (clamp dy)
3070 | l :: _ when l.pageno = pageno ->
3071 if l.pagevh != l.pageh
3072 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3073 else G.postRedisplay "downbirdseye"
3074 | _ :: rest -> loop rest
3076 loop state.layout
3079 let optentry mode _ key =
3080 let btos b = if b then "on" else "off" in
3081 if key >= 32 && key < 127
3082 then
3083 let c = Char.chr key in
3084 match c with
3085 | 's' ->
3086 let ondone s =
3087 try conf.scrollstep <- int_of_string s with exc ->
3088 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3090 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3092 | 'A' ->
3093 let ondone s =
3095 conf.autoscrollstep <- int_of_string s;
3096 if state.autoscroll <> None
3097 then state.autoscroll <- Some conf.autoscrollstep
3098 with exc ->
3099 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3101 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3103 | 'C' ->
3104 let ondone s =
3106 let n, a, b = multicolumns_of_string s in
3107 setcolumns mode n a b;
3108 with exc ->
3109 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3111 TEswitch ("columns: ", "", None, textentry, ondone, true)
3113 | 'Z' ->
3114 let ondone s =
3116 let zoom = float (int_of_string s) /. 100.0 in
3117 setzoom zoom
3118 with exc ->
3119 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3121 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3123 | 't' ->
3124 let ondone s =
3126 conf.thumbw <- bound (int_of_string s) 2 4096;
3127 state.text <-
3128 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3129 begin match mode with
3130 | Birdseye beye ->
3131 leavebirdseye beye false;
3132 enterbirdseye ();
3133 | _ -> ();
3135 with exc ->
3136 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3138 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3140 | 'R' ->
3141 let ondone s =
3142 match try
3143 Some (int_of_string s)
3144 with exc ->
3145 state.text <- Printf.sprintf "bad integer `%s': %s"
3146 s (exntos exc);
3147 None
3148 with
3149 | Some angle -> reqlayout angle conf.fitmodel
3150 | None -> ()
3152 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3154 | 'i' ->
3155 conf.icase <- not conf.icase;
3156 TEdone ("case insensitive search " ^ (btos conf.icase))
3158 | 'p' ->
3159 conf.preload <- not conf.preload;
3160 gotoy state.y;
3161 TEdone ("preload " ^ (btos conf.preload))
3163 | 'v' ->
3164 conf.verbose <- not conf.verbose;
3165 TEdone ("verbose " ^ (btos conf.verbose))
3167 | 'd' ->
3168 conf.debug <- not conf.debug;
3169 TEdone ("debug " ^ (btos conf.debug))
3171 | 'h' ->
3172 conf.maxhfit <- not conf.maxhfit;
3173 state.maxy <- calcheight ();
3174 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3176 | 'c' ->
3177 conf.crophack <- not conf.crophack;
3178 TEdone ("crophack " ^ btos conf.crophack)
3180 | 'a' ->
3181 let s =
3182 match conf.maxwait with
3183 | None ->
3184 conf.maxwait <- Some infinity;
3185 "always wait for page to complete"
3186 | Some _ ->
3187 conf.maxwait <- None;
3188 "show placeholder if page is not ready"
3190 TEdone s
3192 | 'f' ->
3193 conf.underinfo <- not conf.underinfo;
3194 TEdone ("underinfo " ^ btos conf.underinfo)
3196 | 'P' ->
3197 conf.savebmarks <- not conf.savebmarks;
3198 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3200 | 'S' ->
3201 let ondone s =
3203 let pageno, py =
3204 match state.layout with
3205 | [] -> 0, 0
3206 | l :: _ ->
3207 l.pageno, l.pagey
3209 conf.interpagespace <- int_of_string s;
3210 docolumns conf.columns;
3211 state.maxy <- calcheight ();
3212 let y = getpagey pageno in
3213 gotoy (y + py)
3214 with exc ->
3215 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3217 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3219 | 'l' ->
3220 let fm =
3221 match conf.fitmodel with
3222 | FitProportional -> FitWidth
3223 | _ -> FitProportional
3225 reqlayout conf.angle fm;
3226 TEdone ("proportional display " ^ btos (fm == FitProportional))
3228 | 'T' ->
3229 settrim (not conf.trimmargins) conf.trimfuzz;
3230 TEdone ("trim margins " ^ btos conf.trimmargins)
3232 | 'I' ->
3233 conf.invert <- not conf.invert;
3234 TEdone ("invert colors " ^ btos conf.invert)
3236 | 'x' ->
3237 let ondone s =
3238 cbput state.hists.sel s;
3239 conf.selcmd <- s;
3241 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3242 textentry, ondone, true)
3244 | 'M' ->
3245 if conf.pax == None
3246 then conf.pax <- Some (ref (0.0, 0, 0))
3247 else conf.pax <- None;
3248 TEdone ("PAX " ^ btos (conf.pax != None))
3250 | _ ->
3251 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3252 TEstop
3253 else
3254 TEcont state.text
3257 class type lvsource = object
3258 method getitemcount : int
3259 method getitem : int -> (string * int)
3260 method hasaction : int -> bool
3261 method exit :
3262 uioh:uioh ->
3263 cancel:bool ->
3264 active:int ->
3265 first:int ->
3266 pan:int ->
3267 qsearch:string ->
3268 uioh option
3269 method getactive : int
3270 method getfirst : int
3271 method getqsearch : string
3272 method setqsearch : string -> unit
3273 method getpan : int
3274 end;;
3276 class virtual lvsourcebase = object
3277 val mutable m_active = 0
3278 val mutable m_first = 0
3279 val mutable m_qsearch = ""
3280 val mutable m_pan = 0
3281 method getactive = m_active
3282 method getfirst = m_first
3283 method getqsearch = m_qsearch
3284 method getpan = m_pan
3285 method setqsearch s = m_qsearch <- s
3286 end;;
3288 let withoutlastutf8 s =
3289 let len = String.length s in
3290 if len = 0
3291 then s
3292 else
3293 let rec find pos =
3294 if pos = 0
3295 then pos
3296 else
3297 let b = Char.code s.[pos] in
3298 if b land 0b11000000 = 0b11000000
3299 then pos
3300 else find (pos-1)
3302 let first =
3303 if Char.code s.[len-1] land 0x80 = 0
3304 then len-1
3305 else find (len-1)
3307 String.sub s 0 first;
3310 let textentrykeyboard
3311 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3312 let key =
3313 if key >= 0xffb0 && key <= 0xffb9
3314 then key - 0xffb0 + 48 else key
3316 let enttext te =
3317 state.mode <- Textentry (te, onleave);
3318 state.text <- "";
3319 enttext ();
3320 G.postRedisplay "textentrykeyboard enttext";
3322 let histaction cmd =
3323 match opthist with
3324 | None -> ()
3325 | Some (action, _) ->
3326 state.mode <- Textentry (
3327 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3329 G.postRedisplay "textentry histaction"
3331 match key with
3332 | 0xff08 -> (* backspace *)
3333 let s = withoutlastutf8 text in
3334 let len = String.length s in
3335 if cancelonempty && len = 0
3336 then (
3337 onleave Cancel;
3338 G.postRedisplay "textentrykeyboard after cancel";
3340 else (
3341 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3344 | 0xff0d | 0xff8d -> (* (kp) enter *)
3345 ondone text;
3346 onleave Confirm;
3347 G.postRedisplay "textentrykeyboard after confirm"
3349 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3350 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3351 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3352 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3354 | 0xff1b -> (* escape*)
3355 if String.length text = 0
3356 then (
3357 begin match opthist with
3358 | None -> ()
3359 | Some (_, onhistcancel) -> onhistcancel ()
3360 end;
3361 onleave Cancel;
3362 state.text <- "";
3363 G.postRedisplay "textentrykeyboard after cancel2"
3365 else (
3366 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3369 | 0xff9f | 0xffff -> () (* delete *)
3371 | _ when key != 0
3372 && key land 0xff00 != 0xff00 (* keyboard *)
3373 && key land 0xfe00 != 0xfe00 (* xkb *)
3374 && key land 0xfd00 != 0xfd00 (* 3270 *)
3376 begin match onkey text key with
3377 | TEdone text ->
3378 ondone text;
3379 onleave Confirm;
3380 G.postRedisplay "textentrykeyboard after confirm2";
3382 | TEcont text ->
3383 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3385 | TEstop ->
3386 onleave Cancel;
3387 G.postRedisplay "textentrykeyboard after cancel3"
3389 | TEswitch te ->
3390 state.mode <- Textentry (te, onleave);
3391 G.postRedisplay "textentrykeyboard switch";
3392 end;
3394 | _ ->
3395 vlog "unhandled key %s" (Wsi.keyname key)
3398 let firstof first active =
3399 if first > active || abs (first - active) > fstate.maxrows - 1
3400 then max 0 (active - (fstate.maxrows/2))
3401 else first
3404 let calcfirst first active =
3405 if active > first
3406 then
3407 let rows = active - first in
3408 if rows > fstate.maxrows then active - fstate.maxrows else first
3409 else active
3412 let scrollph y maxy =
3413 let sh = float (maxy + state.winh) /. float state.winh in
3414 let sh = float state.winh /. sh in
3415 let sh = max sh (float conf.scrollh) in
3417 let percent = float y /. float maxy in
3418 let position = (float state.winh -. sh) *. percent in
3420 let position =
3421 if position +. sh > float state.winh
3422 then float state.winh -. sh
3423 else position
3425 position, sh;
3428 let coe s = (s :> uioh);;
3430 class listview ~(source:lvsource) ~trusted ~modehash =
3431 object (self)
3432 val m_pan = source#getpan
3433 val m_first = source#getfirst
3434 val m_active = source#getactive
3435 val m_qsearch = source#getqsearch
3436 val m_prev_uioh = state.uioh
3438 method private elemunder y =
3439 let n = y / (fstate.fontsize+1) in
3440 if m_first + n < source#getitemcount
3441 then (
3442 if source#hasaction (m_first + n)
3443 then Some (m_first + n)
3444 else None
3446 else None
3448 method display =
3449 Gl.enable `blend;
3450 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3451 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3452 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3453 GlDraw.color (1., 1., 1.);
3454 Gl.enable `texture_2d;
3455 let fs = fstate.fontsize in
3456 let nfs = fs + 1 in
3457 let ww = fstate.wwidth in
3458 let tabw = 30.0*.ww in
3459 let itemcount = source#getitemcount in
3460 let rec loop row =
3461 if (row - m_first) > fstate.maxrows
3462 then ()
3463 else (
3464 if row >= 0 && row < itemcount
3465 then (
3466 let (s, level) = source#getitem row in
3467 let y = (row - m_first) * nfs in
3468 let x = 5.0 +. float (level + m_pan) *. ww in
3469 if row = m_active
3470 then (
3471 Gl.disable `texture_2d;
3472 GlDraw.polygon_mode `both `line;
3473 let alpha = if source#hasaction row then 0.9 else 0.3 in
3474 GlDraw.color (1., 1., 1.) ~alpha;
3475 GlDraw.rect (1., float (y + 1))
3476 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3477 GlDraw.polygon_mode `both `fill;
3478 GlDraw.color (1., 1., 1.);
3479 Gl.enable `texture_2d;
3482 let drawtabularstring s =
3483 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3484 if trusted
3485 then
3486 let tabpos = try String.index s '\t' with Not_found -> -1 in
3487 if tabpos > 0
3488 then
3489 let len = String.length s - tabpos - 1 in
3490 let s1 = String.sub s 0 tabpos
3491 and s2 = String.sub s (tabpos + 1) len in
3492 let nx = drawstr x s1 in
3493 let sw = nx -. x in
3494 let x = x +. (max tabw sw) in
3495 drawstr x s2
3496 else
3497 drawstr x s
3498 else
3499 drawstr x s
3501 let _ = drawtabularstring s in
3502 loop (row+1)
3506 loop m_first;
3507 Gl.disable `blend;
3508 Gl.disable `texture_2d;
3510 method updownlevel incr =
3511 let len = source#getitemcount in
3512 let curlevel =
3513 if m_active >= 0 && m_active < len
3514 then snd (source#getitem m_active)
3515 else -1
3517 let rec flow i =
3518 if i = len then i-1 else if i = -1 then 0 else
3519 let _, l = source#getitem i in
3520 if l != curlevel then i else flow (i+incr)
3522 let active = flow m_active in
3523 let first = calcfirst m_first active in
3524 G.postRedisplay "outline updownlevel";
3525 {< m_active = active; m_first = first >}
3527 method private key1 key mask =
3528 let set1 active first qsearch =
3529 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3531 let search active pattern incr =
3532 let active = if active = -1 then m_first else active in
3533 let dosearch re =
3534 let rec loop n =
3535 if n >= 0 && n < source#getitemcount
3536 then (
3537 let s, _ = source#getitem n in
3539 (try ignore (Str.search_forward re s 0); true
3540 with Not_found -> false)
3541 then Some n
3542 else loop (n + incr)
3544 else None
3546 loop active
3549 let re = Str.regexp_case_fold pattern in
3550 dosearch re
3551 with Failure s ->
3552 state.text <- s;
3553 None
3555 let itemcount = source#getitemcount in
3556 let find start incr =
3557 let rec find i =
3558 if i = -1 || i = itemcount
3559 then -1
3560 else (
3561 if source#hasaction i
3562 then i
3563 else find (i + incr)
3566 find start
3568 let set active first =
3569 let first = bound first 0 (itemcount - fstate.maxrows) in
3570 state.text <- "";
3571 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3573 let navigate incr =
3574 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3575 let active, first =
3576 let incr1 = if incr > 0 then 1 else -1 in
3577 if isvisible m_first m_active
3578 then
3579 let next =
3580 let next = m_active + incr in
3581 let next =
3582 if next < 0 || next >= itemcount
3583 then -1
3584 else find next incr1
3586 if abs (m_active - next) > fstate.maxrows
3587 then -1
3588 else next
3590 if next = -1
3591 then
3592 let first = m_first + incr in
3593 let first = bound first 0 (itemcount - fstate.maxrows) in
3594 let next =
3595 let next = m_active + incr in
3596 let next = bound next 0 (itemcount - 1) in
3597 find next ~-incr1
3599 let active =
3600 if next = -1
3601 then m_active
3602 else (
3603 if isvisible first next
3604 then next
3605 else m_active
3608 active, first
3609 else
3610 let first = min next m_first in
3611 let first =
3612 if abs (next - first) > fstate.maxrows
3613 then first + incr
3614 else first
3616 next, first
3617 else
3618 let first = m_first + incr in
3619 let first = bound first 0 (itemcount - 1) in
3620 let active =
3621 let next = m_active + incr in
3622 let next = bound next 0 (itemcount - 1) in
3623 let next = find next incr1 in
3624 let active =
3625 if next = -1 || abs (m_active - first) > fstate.maxrows
3626 then (
3627 let active = if m_active = -1 then next else m_active in
3628 active
3630 else next
3632 if isvisible first active
3633 then active
3634 else -1
3636 active, first
3638 G.postRedisplay "listview navigate";
3639 set active first;
3641 match key with
3642 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3643 let incr = if key = 0x72 then -1 else 1 in
3644 let active, first =
3645 match search (m_active + incr) m_qsearch incr with
3646 | None ->
3647 state.text <- m_qsearch ^ " [not found]";
3648 m_active, m_first
3649 | Some active ->
3650 state.text <- m_qsearch;
3651 active, firstof m_first active
3653 G.postRedisplay "listview ctrl-r/s";
3654 set1 active first m_qsearch;
3656 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3657 if m_active >= 0 && m_active < source#getitemcount
3658 then (
3659 let s, _ = source#getitem m_active in
3660 selstring s;
3662 coe self
3664 | 0xff08 -> (* backspace *)
3665 if String.length m_qsearch = 0
3666 then coe self
3667 else (
3668 let qsearch = withoutlastutf8 m_qsearch in
3669 let len = String.length qsearch in
3670 if len = 0
3671 then (
3672 state.text <- "";
3673 G.postRedisplay "listview empty qsearch";
3674 set1 m_active m_first "";
3676 else
3677 let active, first =
3678 match search m_active qsearch ~-1 with
3679 | None ->
3680 state.text <- qsearch ^ " [not found]";
3681 m_active, m_first
3682 | Some active ->
3683 state.text <- qsearch;
3684 active, firstof m_first active
3686 G.postRedisplay "listview backspace qsearch";
3687 set1 active first qsearch
3690 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3691 let pattern = m_qsearch ^ toutf8 key in
3692 let active, first =
3693 match search m_active pattern 1 with
3694 | None ->
3695 state.text <- pattern ^ " [not found]";
3696 m_active, m_first
3697 | Some active ->
3698 state.text <- pattern;
3699 active, firstof m_first active
3701 G.postRedisplay "listview qsearch add";
3702 set1 active first pattern;
3704 | 0xff1b -> (* escape *)
3705 state.text <- "";
3706 if String.length m_qsearch = 0
3707 then (
3708 G.postRedisplay "list view escape";
3709 begin
3710 match
3711 source#exit (coe self) true m_active m_first m_pan m_qsearch
3712 with
3713 | None -> m_prev_uioh
3714 | Some uioh -> uioh
3717 else (
3718 G.postRedisplay "list view kill qsearch";
3719 source#setqsearch "";
3720 coe {< m_qsearch = "" >}
3723 | 0xff0d | 0xff8d -> (* (kp) enter *)
3724 state.text <- "";
3725 let self = {< m_qsearch = "" >} in
3726 source#setqsearch "";
3727 let opt =
3728 G.postRedisplay "listview enter";
3729 if m_active >= 0 && m_active < source#getitemcount
3730 then (
3731 source#exit (coe self) false m_active m_first m_pan "";
3733 else (
3734 source#exit (coe self) true m_active m_first m_pan "";
3737 begin match opt with
3738 | None -> m_prev_uioh
3739 | Some uioh -> uioh
3742 | 0xff9f | 0xffff -> (* (kp) delete *)
3743 coe self
3745 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3746 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3747 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3748 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3750 | 0xff53 | 0xff98 -> (* (kp) right *)
3751 state.text <- "";
3752 G.postRedisplay "listview right";
3753 coe {< m_pan = m_pan - 1 >}
3755 | 0xff51 | 0xff96 -> (* (kp) left *)
3756 state.text <- "";
3757 G.postRedisplay "listview left";
3758 coe {< m_pan = m_pan + 1 >}
3760 | 0xff50 | 0xff95 -> (* (kp) home *)
3761 let active = find 0 1 in
3762 G.postRedisplay "listview home";
3763 set active 0;
3765 | 0xff57 | 0xff9c -> (* (kp) end *)
3766 let first = max 0 (itemcount - fstate.maxrows) in
3767 let active = find (itemcount - 1) ~-1 in
3768 G.postRedisplay "listview end";
3769 set active first;
3771 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3772 coe self
3774 | _ ->
3775 dolog "listview unknown key %#x" key; coe self
3777 method key key mask =
3778 match state.mode with
3779 | Textentry te -> textentrykeyboard key mask te; coe self
3780 | _ -> self#key1 key mask
3782 method button button down x y _ =
3783 let opt =
3784 match button with
3785 | 1 when x > state.winw - conf.scrollbw ->
3786 G.postRedisplay "listview scroll";
3787 if down
3788 then
3789 let _, position, sh = self#scrollph in
3790 if y > truncate position && y < truncate (position +. sh)
3791 then (
3792 state.mstate <- Mscrolly;
3793 Some (coe self)
3795 else
3796 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3797 let first = truncate (s *. float source#getitemcount) in
3798 let first = min source#getitemcount first in
3799 Some (coe {< m_first = first; m_active = first >})
3800 else (
3801 state.mstate <- Mnone;
3802 Some (coe self);
3804 | 1 when not down ->
3805 begin match self#elemunder y with
3806 | Some n ->
3807 G.postRedisplay "listview click";
3808 source#exit
3809 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3810 | _ ->
3811 Some (coe self)
3813 | n when (n == 4 || n == 5) && not down ->
3814 let len = source#getitemcount in
3815 let first =
3816 if n = 5 && m_first + fstate.maxrows >= len
3817 then
3818 m_first
3819 else
3820 let first = m_first + (if n == 4 then -1 else 1) in
3821 bound first 0 (len - 1)
3823 G.postRedisplay "listview wheel";
3824 Some (coe {< m_first = first >})
3825 | n when (n = 6 || n = 7) && not down ->
3826 let inc = m_first + (if n = 7 then -1 else 1) in
3827 G.postRedisplay "listview hwheel";
3828 Some (coe {< m_pan = m_pan + inc >})
3829 | _ ->
3830 Some (coe self)
3832 match opt with
3833 | None -> m_prev_uioh
3834 | Some uioh -> uioh
3836 method motion _ y =
3837 match state.mstate with
3838 | Mscrolly ->
3839 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3840 let first = truncate (s *. float source#getitemcount) in
3841 let first = min source#getitemcount first in
3842 G.postRedisplay "listview motion";
3843 coe {< m_first = first; m_active = first >}
3844 | _ -> coe self
3846 method pmotion x y =
3847 if x < state.winw - conf.scrollbw
3848 then
3849 let n =
3850 match self#elemunder y with
3851 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3852 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3854 let o =
3855 if n != m_active
3856 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3857 else self
3859 coe o
3860 else (
3861 Wsi.setcursor Wsi.CURSOR_INHERIT;
3862 coe self
3865 method infochanged _ = ()
3867 method scrollpw = (0, 0.0, 0.0)
3868 method scrollph =
3869 let nfs = fstate.fontsize + 1 in
3870 let y = m_first * nfs in
3871 let itemcount = source#getitemcount in
3872 let maxi = max 0 (itemcount - fstate.maxrows) in
3873 let maxy = maxi * nfs in
3874 let p, h = scrollph y maxy in
3875 conf.scrollbw, p, h
3877 method modehash = modehash
3878 method eformsgs = false
3879 end;;
3881 class outlinelistview ~source =
3882 object (self)
3883 inherit listview
3884 ~source:(source :> lvsource)
3885 ~trusted:false
3886 ~modehash:(findkeyhash conf "outline")
3887 as super
3889 method key key mask =
3890 let calcfirst first active =
3891 if active > first
3892 then
3893 let rows = active - first in
3894 let maxrows =
3895 if String.length state.text = 0
3896 then fstate.maxrows
3897 else fstate.maxrows - 2
3899 if rows > maxrows then active - maxrows else first
3900 else active
3902 let navigate incr =
3903 let active = m_active + incr in
3904 let active = bound active 0 (source#getitemcount - 1) in
3905 let first = calcfirst m_first active in
3906 G.postRedisplay "outline navigate";
3907 coe {< m_active = active; m_first = first >}
3909 let ctrl = Wsi.withctrl mask in
3910 match key with
3911 | 110 when ctrl -> (* ctrl-n *)
3912 source#narrow m_qsearch;
3913 G.postRedisplay "outline ctrl-n";
3914 coe {< m_first = 0; m_active = 0 >}
3916 | 117 when ctrl -> (* ctrl-u *)
3917 source#denarrow;
3918 G.postRedisplay "outline ctrl-u";
3919 state.text <- "";
3920 coe {< m_first = 0; m_active = 0 >}
3922 | 108 when ctrl -> (* ctrl-l *)
3923 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3924 G.postRedisplay "outline ctrl-l";
3925 coe {< m_first = first >}
3927 | 0xff9f | 0xffff -> (* (kp) delete *)
3928 source#remove m_active;
3929 G.postRedisplay "outline delete";
3930 let active = max 0 (m_active-1) in
3931 coe {< m_first = firstof m_first active;
3932 m_active = active >}
3934 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3935 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3936 | 0xff55 | 0xff9a -> (* (kp) prior *)
3937 navigate ~-(fstate.maxrows)
3938 | 0xff56 | 0xff9b -> (* (kp) next *)
3939 navigate fstate.maxrows
3941 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3942 let o =
3943 if ctrl
3944 then (
3945 G.postRedisplay "outline ctrl right";
3946 {< m_pan = m_pan + 1 >}
3948 else self#updownlevel 1
3950 coe o
3952 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3953 let o =
3954 if ctrl
3955 then (
3956 G.postRedisplay "outline ctrl left";
3957 {< m_pan = m_pan - 1 >}
3959 else self#updownlevel ~-1
3961 coe o
3963 | 0xff50 | 0xff95 -> (* (kp) home *)
3964 G.postRedisplay "outline home";
3965 coe {< m_first = 0; m_active = 0 >}
3967 | 0xff57 | 0xff9c -> (* (kp) end *)
3968 let active = source#getitemcount - 1 in
3969 let first = max 0 (active - fstate.maxrows) in
3970 G.postRedisplay "outline end";
3971 coe {< m_active = active; m_first = first >}
3973 | _ -> super#key key mask
3976 let outlinesource usebookmarks =
3977 let empty = [||] in
3978 (object
3979 inherit lvsourcebase
3980 val mutable m_items = empty
3981 val mutable m_orig_items = empty
3982 val mutable m_prev_items = empty
3983 val mutable m_narrow_pattern = ""
3984 val mutable m_hadremovals = false
3986 method getitemcount =
3987 Array.length m_items + (if m_hadremovals then 1 else 0)
3989 method getitem n =
3990 if n == Array.length m_items && m_hadremovals
3991 then
3992 ("[Confirm removal]", 0)
3993 else
3994 let s, n, _ = m_items.(n) in
3995 (s, n)
3997 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3998 ignore (uioh, first, qsearch);
3999 let confrimremoval = m_hadremovals && active = Array.length m_items in
4000 let items =
4001 if String.length m_narrow_pattern = 0
4002 then m_orig_items
4003 else m_items
4005 if not cancel
4006 then (
4007 if not confrimremoval
4008 then(
4009 let _, _, anchor = m_items.(active) in
4010 gotoghyll (getanchory anchor);
4011 m_items <- items;
4013 else (
4014 state.bookmarks <- Array.to_list m_items;
4015 m_orig_items <- m_items;
4018 else m_items <- items;
4019 m_pan <- pan;
4020 None
4022 method hasaction _ = true
4024 method greetmsg =
4025 if Array.length m_items != Array.length m_orig_items
4026 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
4027 else ""
4029 method narrow pattern =
4030 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4031 match reopt with
4032 | None -> ()
4033 | Some re ->
4034 let rec loop accu n =
4035 if n = -1
4036 then (
4037 m_narrow_pattern <- pattern;
4038 m_items <- Array.of_list accu
4040 else
4041 let (s, _, _) as o = m_items.(n) in
4042 let accu =
4043 if (try ignore (Str.search_forward re s 0); true
4044 with Not_found -> false)
4045 then o :: accu
4046 else accu
4048 loop accu (n-1)
4050 loop [] (Array.length m_items - 1)
4052 method denarrow =
4053 m_orig_items <- (
4054 if usebookmarks
4055 then Array.of_list state.bookmarks
4056 else state.outlines
4058 m_items <- m_orig_items
4060 method remove m =
4061 if usebookmarks
4062 then
4063 if m >= 0 && m < Array.length m_items
4064 then (
4065 m_hadremovals <- true;
4066 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4067 let n = if n >= m then n+1 else n in
4068 m_items.(n)
4072 method reset anchor items =
4073 m_hadremovals <- false;
4074 if m_orig_items == empty || m_prev_items != items
4075 then (
4076 m_orig_items <- items;
4077 if String.length m_narrow_pattern = 0
4078 then m_items <- items;
4080 m_prev_items <- items;
4081 let rely = getanchory anchor in
4082 let active =
4083 let rec loop n best bestd =
4084 if n = Array.length m_items
4085 then best
4086 else
4087 let (_, _, anchor) = m_items.(n) in
4088 let orely = getanchory anchor in
4089 let d = abs (orely - rely) in
4090 if d < bestd
4091 then loop (n+1) n d
4092 else loop (n+1) best bestd
4094 loop 0 ~-1 max_int
4096 m_active <- active;
4097 m_first <- firstof m_first active
4098 end)
4101 let enterselector usebookmarks =
4102 let source = outlinesource usebookmarks in
4103 fun errmsg ->
4104 let outlines =
4105 if usebookmarks
4106 then Array.of_list state.bookmarks
4107 else state.outlines
4109 if Array.length outlines = 0
4110 then (
4111 showtext ' ' errmsg;
4113 else (
4114 state.text <- source#greetmsg;
4115 Wsi.setcursor Wsi.CURSOR_INHERIT;
4116 let anchor = getanchor () in
4117 source#reset anchor outlines;
4118 state.uioh <- coe (new outlinelistview ~source);
4119 G.postRedisplay "enter selector";
4123 let enteroutlinemode =
4124 let f = enterselector false in
4125 fun ()-> f "Document has no outline";
4128 let enterbookmarkmode =
4129 let f = enterselector true in
4130 fun () -> f "Document has no bookmarks (yet)";
4133 let color_of_string s =
4134 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4135 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4139 let color_to_string (r, g, b) =
4140 let r = truncate (r *. 256.0)
4141 and g = truncate (g *. 256.0)
4142 and b = truncate (b *. 256.0) in
4143 Printf.sprintf "%d/%d/%d" r g b
4146 let irect_of_string s =
4147 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4150 let irect_to_string (x0,y0,x1,y1) =
4151 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4154 let makecheckers () =
4155 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4156 following to say:
4157 converted by Issac Trotts. July 25, 2002 *)
4158 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4159 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4160 let id = GlTex.gen_texture () in
4161 GlTex.bind_texture `texture_2d id;
4162 GlPix.store (`unpack_alignment 1);
4163 GlTex.image2d image;
4164 List.iter (GlTex.parameter ~target:`texture_2d)
4165 [ `mag_filter `nearest; `min_filter `nearest ];
4169 let setcheckers enabled =
4170 match state.texid with
4171 | None ->
4172 if enabled then state.texid <- Some (makecheckers ())
4174 | Some texid ->
4175 if not enabled
4176 then (
4177 GlTex.delete_texture texid;
4178 state.texid <- None;
4182 let int_of_string_with_suffix s =
4183 let l = String.length s in
4184 let s1, shift =
4185 if l > 1
4186 then
4187 let suffix = Char.lowercase s.[l-1] in
4188 match suffix with
4189 | 'k' -> String.sub s 0 (l-1), 10
4190 | 'm' -> String.sub s 0 (l-1), 20
4191 | 'g' -> String.sub s 0 (l-1), 30
4192 | _ -> s, 0
4193 else s, 0
4195 let n = int_of_string s1 in
4196 let m = n lsl shift in
4197 if m < 0 || m < n
4198 then raise (Failure "value too large")
4199 else m
4202 let string_with_suffix_of_int n =
4203 if n = 0
4204 then "0"
4205 else
4206 let n, s =
4207 if n land ((1 lsl 30) - 1) = 0
4208 then n lsr 30, "G"
4209 else (
4210 if n land ((1 lsl 20) - 1) = 0
4211 then n lsr 20, "M"
4212 else (
4213 if n land ((1 lsl 10) - 1) = 0
4214 then n lsr 10, "K"
4215 else n, ""
4219 let rec loop s n =
4220 let h = n mod 1000 in
4221 let n = n / 1000 in
4222 if n = 0
4223 then string_of_int h ^ s
4224 else (
4225 let s = Printf.sprintf "_%03d%s" h s in
4226 loop s n
4229 loop "" n ^ s;
4232 let defghyllscroll = (40, 8, 32);;
4233 let ghyllscroll_of_string s =
4234 let (n, a, b) as nab =
4235 if s = "default"
4236 then defghyllscroll
4237 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4239 if n <= a || n <= b || a >= b
4240 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4241 nab;
4244 let ghyllscroll_to_string ((n, a, b) as nab) =
4245 if nab = defghyllscroll
4246 then "default"
4247 else Printf.sprintf "%d,%d,%d" n a b;
4250 let describe_location () =
4251 let fn = page_of_y state.y in
4252 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4253 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4254 let percent =
4255 if maxy <= 0
4256 then 100.
4257 else (100. *. (float state.y /. float maxy))
4259 if fn = ln
4260 then
4261 Printf.sprintf "page %d of %d [%.2f%%]"
4262 (fn+1) state.pagecount percent
4263 else
4264 Printf.sprintf
4265 "pages %d-%d of %d [%.2f%%]"
4266 (fn+1) (ln+1) state.pagecount percent
4269 let setpresentationmode v =
4270 let n = page_of_y state.y in
4271 state.anchor <- (n, 0.0, 1.0);
4272 conf.presentation <- v;
4273 if conf.fitmodel = FitPage
4274 then reqlayout conf.angle conf.fitmodel;
4275 represent ();
4278 let enterinfomode =
4279 let btos b = if b then "\xe2\x88\x9a" else "" in
4280 let showextended = ref false in
4281 let leave mode = function
4282 | Confirm -> state.mode <- mode
4283 | Cancel -> state.mode <- mode in
4284 let src =
4285 (object
4286 val mutable m_first_time = true
4287 val mutable m_l = []
4288 val mutable m_a = [||]
4289 val mutable m_prev_uioh = nouioh
4290 val mutable m_prev_mode = View
4292 inherit lvsourcebase
4294 method reset prev_mode prev_uioh =
4295 m_a <- Array.of_list (List.rev m_l);
4296 m_l <- [];
4297 m_prev_mode <- prev_mode;
4298 m_prev_uioh <- prev_uioh;
4299 if m_first_time
4300 then (
4301 let rec loop n =
4302 if n >= Array.length m_a
4303 then ()
4304 else
4305 match m_a.(n) with
4306 | _, _, _, Action _ -> m_active <- n
4307 | _ -> loop (n+1)
4309 loop 0;
4310 m_first_time <- false;
4313 method int name get set =
4314 m_l <-
4315 (name, `int get, 1, Action (
4316 fun u ->
4317 let ondone s =
4318 try set (int_of_string s)
4319 with exn ->
4320 state.text <- Printf.sprintf "bad integer `%s': %s"
4321 s (exntos exn)
4323 state.text <- "";
4324 let te = name ^ ": ", "", None, intentry, ondone, true in
4325 state.mode <- Textentry (te, leave m_prev_mode);
4327 )) :: m_l
4329 method int_with_suffix name get set =
4330 m_l <-
4331 (name, `intws get, 1, Action (
4332 fun u ->
4333 let ondone s =
4334 try set (int_of_string_with_suffix s)
4335 with exn ->
4336 state.text <- Printf.sprintf "bad integer `%s': %s"
4337 s (exntos exn)
4339 state.text <- "";
4340 let te =
4341 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4343 state.mode <- Textentry (te, leave m_prev_mode);
4345 )) :: m_l
4347 method bool ?(offset=1) ?(btos=btos) name get set =
4348 m_l <-
4349 (name, `bool (btos, get), offset, Action (
4350 fun u ->
4351 let v = get () in
4352 set (not v);
4354 )) :: m_l
4356 method color name get set =
4357 m_l <-
4358 (name, `color get, 1, Action (
4359 fun u ->
4360 let invalid = (nan, nan, nan) in
4361 let ondone s =
4362 let c =
4363 try color_of_string s
4364 with exn ->
4365 state.text <- Printf.sprintf "bad color `%s': %s"
4366 s (exntos exn);
4367 invalid
4369 if c <> invalid
4370 then set c;
4372 let te = name ^ ": ", "", None, textentry, ondone, true in
4373 state.text <- color_to_string (get ());
4374 state.mode <- Textentry (te, leave m_prev_mode);
4376 )) :: m_l
4378 method string name get set =
4379 m_l <-
4380 (name, `string get, 1, Action (
4381 fun u ->
4382 let ondone s = set s in
4383 let te = name ^ ": ", "", None, textentry, ondone, true in
4384 state.mode <- Textentry (te, leave m_prev_mode);
4386 )) :: m_l
4388 method colorspace name get set =
4389 m_l <-
4390 (name, `string get, 1, Action (
4391 fun _ ->
4392 let source =
4393 let vals = [| "rgb"; "bgr"; "gray" |] in
4394 (object
4395 inherit lvsourcebase
4397 initializer
4398 m_active <- int_of_colorspace conf.colorspace;
4399 m_first <- 0;
4401 method getitemcount = Array.length vals
4402 method getitem n = (vals.(n), 0)
4403 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4404 ignore (uioh, first, pan, qsearch);
4405 if not cancel then set active;
4406 None
4407 method hasaction _ = true
4408 end)
4410 state.text <- "";
4411 let modehash = findkeyhash conf "info" in
4412 coe (new listview ~source ~trusted:true ~modehash)
4413 )) :: m_l
4415 method roammark name get set =
4416 m_l <-
4417 (name, `string get, 1, Action (
4418 fun _ ->
4419 let source =
4420 let vals = [| "page"; "block"; "line"; "word" |] in
4421 (object
4422 inherit lvsourcebase
4424 initializer
4425 m_active <- int_of_mark conf.paxmark;
4426 m_first <- 0;
4428 method getitemcount = Array.length vals
4429 method getitem n = (vals.(n), 0)
4430 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4431 ignore (uioh, first, pan, qsearch);
4432 if not cancel then set active;
4433 None
4434 method hasaction _ = true
4435 end)
4437 state.text <- "";
4438 let modehash = findkeyhash conf "info" in
4439 coe (new listview ~source ~trusted:true ~modehash)
4440 )) :: m_l
4442 method fitmodel name get set =
4443 m_l <-
4444 (name, `string get, 1, Action (
4445 fun _ ->
4446 let source =
4447 let vals = [| "fit width"; "proportional"; "fit page" |] in
4448 (object
4449 inherit lvsourcebase
4451 initializer
4452 m_active <- int_of_fitmodel conf.fitmodel;
4453 m_first <- 0;
4455 method getitemcount = Array.length vals
4456 method getitem n = (vals.(n), 0)
4457 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4458 ignore (uioh, first, pan, qsearch);
4459 if not cancel then set active;
4460 None
4461 method hasaction _ = true
4462 end)
4464 state.text <- "";
4465 let modehash = findkeyhash conf "info" in
4466 coe (new listview ~source ~trusted:true ~modehash)
4467 )) :: m_l
4469 method caption s offset =
4470 m_l <- (s, `empty, offset, Noaction) :: m_l
4472 method caption2 s f offset =
4473 m_l <- (s, `string f, offset, Noaction) :: m_l
4475 method getitemcount = Array.length m_a
4477 method getitem n =
4478 let tostr = function
4479 | `int f -> string_of_int (f ())
4480 | `intws f -> string_with_suffix_of_int (f ())
4481 | `string f -> f ()
4482 | `color f -> color_to_string (f ())
4483 | `bool (btos, f) -> btos (f ())
4484 | `empty -> ""
4486 let name, t, offset, _ = m_a.(n) in
4487 ((let s = tostr t in
4488 if String.length s > 0
4489 then Printf.sprintf "%s\t%s" name s
4490 else name),
4491 offset)
4493 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4494 let uiohopt =
4495 if not cancel
4496 then (
4497 m_qsearch <- qsearch;
4498 let uioh =
4499 match m_a.(active) with
4500 | _, _, _, Action f -> f uioh
4501 | _ -> uioh
4503 Some uioh
4505 else None
4507 m_active <- active;
4508 m_first <- first;
4509 m_pan <- pan;
4510 uiohopt
4512 method hasaction n =
4513 match m_a.(n) with
4514 | _, _, _, Action _ -> true
4515 | _ -> false
4516 end)
4518 let rec fillsrc prevmode prevuioh =
4519 let sep () = src#caption "" 0 in
4520 let colorp name get set =
4521 src#string name
4522 (fun () -> color_to_string (get ()))
4523 (fun v ->
4525 let c = color_of_string v in
4526 set c
4527 with exn ->
4528 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4531 let oldmode = state.mode in
4532 let birdseye = isbirdseye state.mode in
4534 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4536 src#bool "presentation mode"
4537 (fun () -> conf.presentation)
4538 (fun v -> setpresentationmode v);
4540 src#bool "ignore case in searches"
4541 (fun () -> conf.icase)
4542 (fun v -> conf.icase <- v);
4544 src#bool "preload"
4545 (fun () -> conf.preload)
4546 (fun v -> conf.preload <- v);
4548 src#bool "highlight links"
4549 (fun () -> conf.hlinks)
4550 (fun v -> conf.hlinks <- v);
4552 src#bool "under info"
4553 (fun () -> conf.underinfo)
4554 (fun v -> conf.underinfo <- v);
4556 src#bool "persistent bookmarks"
4557 (fun () -> conf.savebmarks)
4558 (fun v -> conf.savebmarks <- v);
4560 src#fitmodel "fit model"
4561 (fun () -> fitmodel_to_string conf.fitmodel)
4562 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4564 src#bool "trim margins"
4565 (fun () -> conf.trimmargins)
4566 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4568 src#bool "persistent location"
4569 (fun () -> conf.jumpback)
4570 (fun v -> conf.jumpback <- v);
4572 sep ();
4573 src#int "inter-page space"
4574 (fun () -> conf.interpagespace)
4575 (fun n ->
4576 conf.interpagespace <- n;
4577 docolumns conf.columns;
4578 let pageno, py =
4579 match state.layout with
4580 | [] -> 0, 0
4581 | l :: _ ->
4582 l.pageno, l.pagey
4584 state.maxy <- calcheight ();
4585 let y = getpagey pageno in
4586 gotoy (y + py)
4589 src#int "page bias"
4590 (fun () -> conf.pagebias)
4591 (fun v -> conf.pagebias <- v);
4593 src#int "scroll step"
4594 (fun () -> conf.scrollstep)
4595 (fun n -> conf.scrollstep <- n);
4597 src#int "horizontal scroll step"
4598 (fun () -> conf.hscrollstep)
4599 (fun v -> conf.hscrollstep <- v);
4601 src#int "auto scroll step"
4602 (fun () ->
4603 match state.autoscroll with
4604 | Some step -> step
4605 | _ -> conf.autoscrollstep)
4606 (fun n ->
4607 if state.autoscroll <> None
4608 then state.autoscroll <- Some n;
4609 conf.autoscrollstep <- n);
4611 src#int "zoom"
4612 (fun () -> truncate (conf.zoom *. 100.))
4613 (fun v -> setzoom ((float v) /. 100.));
4615 src#int "rotation"
4616 (fun () -> conf.angle)
4617 (fun v -> reqlayout v conf.fitmodel);
4619 src#int "scroll bar width"
4620 (fun () -> conf.scrollbw)
4621 (fun v ->
4622 conf.scrollbw <- v;
4623 reshape state.winw state.winh;
4626 src#int "scroll handle height"
4627 (fun () -> conf.scrollh)
4628 (fun v -> conf.scrollh <- v;);
4630 src#int "thumbnail width"
4631 (fun () -> conf.thumbw)
4632 (fun v ->
4633 conf.thumbw <- min 4096 v;
4634 match oldmode with
4635 | Birdseye beye ->
4636 leavebirdseye beye false;
4637 enterbirdseye ()
4638 | _ -> ()
4641 let mode = state.mode in
4642 src#string "columns"
4643 (fun () ->
4644 match conf.columns with
4645 | Csingle _ -> "1"
4646 | Cmulti (multi, _) -> multicolumns_to_string multi
4647 | Csplit (count, _) -> "-" ^ string_of_int count
4649 (fun v ->
4650 let n, a, b = multicolumns_of_string v in
4651 setcolumns mode n a b);
4653 sep ();
4654 src#caption "Pixmap cache" 0;
4655 src#int_with_suffix "size (advisory)"
4656 (fun () -> conf.memlimit)
4657 (fun v -> conf.memlimit <- v);
4659 src#caption2 "used"
4660 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4661 (string_with_suffix_of_int state.memused)
4662 (Hashtbl.length state.tilemap)) 1;
4664 sep ();
4665 src#caption "Layout" 0;
4666 src#caption2 "Dimension"
4667 (fun () ->
4668 Printf.sprintf "%dx%d (virtual %dx%d)"
4669 state.winw state.winh
4670 state.w state.maxy)
4672 if conf.debug
4673 then
4674 src#caption2 "Position" (fun () ->
4675 Printf.sprintf "%dx%d" state.x state.y
4677 else
4678 src#caption2 "Position" (fun () -> describe_location ()) 1
4681 sep ();
4682 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4683 "Save these parameters as global defaults at exit"
4684 (fun () -> conf.bedefault)
4685 (fun v -> conf.bedefault <- v)
4688 sep ();
4689 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4690 src#bool ~offset:0 ~btos "Extended parameters"
4691 (fun () -> !showextended)
4692 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4693 if !showextended
4694 then (
4695 src#bool "checkers"
4696 (fun () -> conf.checkers)
4697 (fun v -> conf.checkers <- v; setcheckers v);
4698 src#bool "update cursor"
4699 (fun () -> conf.updatecurs)
4700 (fun v -> conf.updatecurs <- v);
4701 src#bool "verbose"
4702 (fun () -> conf.verbose)
4703 (fun v -> conf.verbose <- v);
4704 src#bool "invert colors"
4705 (fun () -> conf.invert)
4706 (fun v -> conf.invert <- v);
4707 src#bool "max fit"
4708 (fun () -> conf.maxhfit)
4709 (fun v -> conf.maxhfit <- v);
4710 src#bool "redirect stderr"
4711 (fun () -> conf.redirectstderr)
4712 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4713 src#bool "pax mode"
4714 (fun () -> conf.pax != None)
4715 (fun v ->
4716 if v
4717 then conf.pax <- Some (ref (now (), 0, 0))
4718 else conf.pax <- None);
4719 src#string "uri launcher"
4720 (fun () -> conf.urilauncher)
4721 (fun v -> conf.urilauncher <- v);
4722 src#string "path launcher"
4723 (fun () -> conf.pathlauncher)
4724 (fun v -> conf.pathlauncher <- v);
4725 src#string "tile size"
4726 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4727 (fun v ->
4729 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4730 conf.tilew <- max 64 w;
4731 conf.tileh <- max 64 h;
4732 flushtiles ();
4733 with exn ->
4734 state.text <- Printf.sprintf "bad tile size `%s': %s"
4735 v (exntos exn)
4737 src#int "texture count"
4738 (fun () -> conf.texcount)
4739 (fun v ->
4740 if realloctexts v
4741 then conf.texcount <- v
4742 else showtext '!' " Failed to set texture count please retry later"
4744 src#int "slice height"
4745 (fun () -> conf.sliceheight)
4746 (fun v ->
4747 conf.sliceheight <- v;
4748 wcmd "sliceh %d" conf.sliceheight;
4750 src#int "anti-aliasing level"
4751 (fun () -> conf.aalevel)
4752 (fun v ->
4753 conf.aalevel <- bound v 0 8;
4754 state.anchor <- getanchor ();
4755 opendoc state.path state.password;
4757 src#string "page scroll scaling factor"
4758 (fun () -> string_of_float conf.pgscale)
4759 (fun v ->
4761 let s = float_of_string v in
4762 conf.pgscale <- s
4763 with exn ->
4764 state.text <- Printf.sprintf
4765 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4768 src#int "ui font size"
4769 (fun () -> fstate.fontsize)
4770 (fun v -> setfontsize (bound v 5 100));
4771 src#int "hint font size"
4772 (fun () -> conf.hfsize)
4773 (fun v -> conf.hfsize <- bound v 5 100);
4774 colorp "background color"
4775 (fun () -> conf.bgcolor)
4776 (fun v -> conf.bgcolor <- v);
4777 src#bool "crop hack"
4778 (fun () -> conf.crophack)
4779 (fun v -> conf.crophack <- v);
4780 src#string "trim fuzz"
4781 (fun () -> irect_to_string conf.trimfuzz)
4782 (fun v ->
4784 conf.trimfuzz <- irect_of_string v;
4785 if conf.trimmargins
4786 then settrim true conf.trimfuzz;
4787 with exn ->
4788 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4790 src#string "throttle"
4791 (fun () ->
4792 match conf.maxwait with
4793 | None -> "show place holder if page is not ready"
4794 | Some time ->
4795 if time = infinity
4796 then "wait for page to fully render"
4797 else
4798 "wait " ^ string_of_float time
4799 ^ " seconds before showing placeholder"
4801 (fun v ->
4803 let f = float_of_string v in
4804 if f <= 0.0
4805 then conf.maxwait <- None
4806 else conf.maxwait <- Some f
4807 with exn ->
4808 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4810 src#string "ghyll scroll"
4811 (fun () ->
4812 match conf.ghyllscroll with
4813 | None -> ""
4814 | Some nab -> ghyllscroll_to_string nab
4816 (fun v ->
4818 let gs =
4819 if String.length v = 0
4820 then None
4821 else Some (ghyllscroll_of_string v)
4823 conf.ghyllscroll <- gs
4824 with exn ->
4825 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4827 src#string "selection command"
4828 (fun () -> conf.selcmd)
4829 (fun v -> conf.selcmd <- v);
4830 src#string "synctex command"
4831 (fun () -> conf.stcmd)
4832 (fun v -> conf.stcmd <- v);
4833 src#string "pax command"
4834 (fun () -> conf.paxcmd)
4835 (fun v -> conf.paxcmd <- v);
4836 src#colorspace "color space"
4837 (fun () -> colorspace_to_string conf.colorspace)
4838 (fun v ->
4839 conf.colorspace <- colorspace_of_int v;
4840 wcmd "cs %d" v;
4841 load state.layout;
4843 src#roammark "pax mark method"
4844 (fun () -> mark_to_string conf.paxmark)
4845 (fun v -> conf.paxmark <- mark_of_int v);
4846 if pbousable ()
4847 then
4848 src#bool "use PBO"
4849 (fun () -> conf.usepbo)
4850 (fun v -> conf.usepbo <- v);
4851 src#bool "mouse wheel scrolls pages"
4852 (fun () -> conf.wheelbypage)
4853 (fun v -> conf.wheelbypage <- v);
4854 src#bool "open remote links in a new instance"
4855 (fun () -> conf.riani)
4856 (fun v -> conf.riani <- v);
4859 sep ();
4860 src#caption "Document" 0;
4861 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4862 src#caption2 "Pages"
4863 (fun () -> string_of_int state.pagecount) 1;
4864 src#caption2 "Dimensions"
4865 (fun () -> string_of_int (List.length state.pdims)) 1;
4866 if conf.trimmargins
4867 then (
4868 sep ();
4869 src#caption "Trimmed margins" 0;
4870 src#caption2 "Dimensions"
4871 (fun () -> string_of_int (List.length state.pdims)) 1;
4874 sep ();
4875 src#caption "OpenGL" 0;
4876 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4877 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4879 sep ();
4880 src#caption "Location" 0;
4881 if String.length state.origin > 0
4882 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4883 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4885 src#reset prevmode prevuioh;
4887 fun () ->
4888 state.text <- "";
4889 let prevmode = state.mode
4890 and prevuioh = state.uioh in
4891 fillsrc prevmode prevuioh;
4892 let source = (src :> lvsource) in
4893 let modehash = findkeyhash conf "info" in
4894 state.uioh <- coe (object (self)
4895 inherit listview ~source ~trusted:true ~modehash as super
4896 val mutable m_prevmemused = 0
4897 method infochanged = function
4898 | Memused ->
4899 if m_prevmemused != state.memused
4900 then (
4901 m_prevmemused <- state.memused;
4902 G.postRedisplay "memusedchanged";
4904 | Pdim -> G.postRedisplay "pdimchanged"
4905 | Docinfo -> fillsrc prevmode prevuioh
4907 method key key mask =
4908 if not (Wsi.withctrl mask)
4909 then
4910 match key with
4911 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4912 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4913 | _ -> super#key key mask
4914 else super#key key mask
4915 end);
4916 G.postRedisplay "info";
4919 let enterhelpmode =
4920 let source =
4921 (object
4922 inherit lvsourcebase
4923 method getitemcount = Array.length state.help
4924 method getitem n =
4925 let s, l, _ = state.help.(n) in
4926 (s, l)
4928 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4929 let optuioh =
4930 if not cancel
4931 then (
4932 m_qsearch <- qsearch;
4933 match state.help.(active) with
4934 | _, _, Action f -> Some (f uioh)
4935 | _ -> Some (uioh)
4937 else None
4939 m_active <- active;
4940 m_first <- first;
4941 m_pan <- pan;
4942 optuioh
4944 method hasaction n =
4945 match state.help.(n) with
4946 | _, _, Action _ -> true
4947 | _ -> false
4949 initializer
4950 m_active <- -1
4951 end)
4952 in fun () ->
4953 let modehash = findkeyhash conf "help" in
4954 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4955 G.postRedisplay "help";
4958 let entermsgsmode =
4959 let msgsource =
4960 let re = Str.regexp "[\r\n]" in
4961 (object
4962 inherit lvsourcebase
4963 val mutable m_items = [||]
4965 method getitemcount = 1 + Array.length m_items
4967 method getitem n =
4968 if n = 0
4969 then "[Clear]", 0
4970 else m_items.(n-1), 0
4972 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4973 ignore uioh;
4974 if not cancel
4975 then (
4976 if active = 0
4977 then Buffer.clear state.errmsgs;
4978 m_qsearch <- qsearch;
4980 m_active <- active;
4981 m_first <- first;
4982 m_pan <- pan;
4983 None
4985 method hasaction n =
4986 n = 0
4988 method reset =
4989 state.newerrmsgs <- false;
4990 let l = Str.split re (Buffer.contents state.errmsgs) in
4991 m_items <- Array.of_list l
4993 initializer
4994 m_active <- 0
4995 end)
4996 in fun () ->
4997 state.text <- "";
4998 msgsource#reset;
4999 let source = (msgsource :> lvsource) in
5000 let modehash = findkeyhash conf "listview" in
5001 state.uioh <- coe (object
5002 inherit listview ~source ~trusted:false ~modehash as super
5003 method display =
5004 if state.newerrmsgs
5005 then msgsource#reset;
5006 super#display
5007 end);
5008 G.postRedisplay "msgs";
5011 let quickbookmark ?title () =
5012 match state.layout with
5013 | [] -> ()
5014 | l :: _ ->
5015 let title =
5016 match title with
5017 | None ->
5018 let sec = Unix.gettimeofday () in
5019 let tm = Unix.localtime sec in
5020 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5021 (l.pageno+1)
5022 tm.Unix.tm_mday
5023 tm.Unix.tm_mon
5024 (tm.Unix.tm_year + 1900)
5025 tm.Unix.tm_hour
5026 tm.Unix.tm_min
5027 | Some title -> title
5029 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5032 let setautoscrollspeed step goingdown =
5033 let incr = max 1 ((abs step) / 2) in
5034 let incr = if goingdown then incr else -incr in
5035 let astep = step + incr in
5036 state.autoscroll <- Some astep;
5039 let gotounder = function
5040 | Ulinkgoto (pageno, top) ->
5041 if pageno >= 0
5042 then (
5043 addnav ();
5044 gotopage1 pageno top;
5047 | Ulinkuri s ->
5048 gotouri s
5050 | Uremote (filename, pageno) ->
5051 let path =
5052 if String.length filename > 0
5053 then
5054 if Filename.is_relative filename
5055 then
5056 let dir = Filename.dirname state.path in
5057 let dir =
5058 if Filename.is_implicit dir
5059 then Filename.concat (Sys.getcwd ()) dir
5060 else dir
5062 Filename.concat dir filename
5063 else filename
5064 else ""
5066 let path =
5067 if Sys.file_exists path
5068 then path
5069 else ""
5071 if String.length path > 0
5072 then (
5073 if conf.riani
5074 then
5075 let command = !selfexec ^ " " ^ path in
5076 try popen command []
5077 with exn ->
5078 Printf.eprintf
5079 "failed to execute `%s': %s\n" command (exntos exn);
5080 flush stderr;
5081 else
5082 let anchor = getanchor () in
5083 let ranchor = state.path, state.password, anchor, state.origin in
5084 state.origin <- "";
5085 state.anchor <- (pageno, 0.0, 0.0);
5086 state.ranchors <- ranchor :: state.ranchors;
5087 opendoc path "";
5089 else showtext '!' ("Could not find " ^ filename)
5091 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5094 let canpan () =
5095 match conf.columns with
5096 | Csplit _ -> true
5097 | _ -> state.x != 0 || conf.zoom > 1.0
5100 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5102 let existsinrow pageno (columns, coverA, coverB) p =
5103 let last = ((pageno - coverA) mod columns) + columns in
5104 let rec any = function
5105 | [] -> false
5106 | l :: rest ->
5107 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5108 then p l
5109 else (
5110 if not (p l)
5111 then (if l.pageno = last then false else any rest)
5112 else true
5115 any state.layout
5118 let nextpage () =
5119 match state.layout with
5120 | [] ->
5121 let pageno = page_of_y state.y in
5122 gotoghyll (getpagey (pageno+1))
5123 | l :: rest ->
5124 match conf.columns with
5125 | Csingle _ ->
5126 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5127 then
5128 let y = clamp (pgscale state.winh) in
5129 gotoghyll y
5130 else
5131 let pageno = min (l.pageno+1) (state.pagecount-1) in
5132 gotoghyll (getpagey pageno)
5133 | Cmulti ((c, _, _) as cl, _) ->
5134 if conf.presentation
5135 && (existsinrow l.pageno cl
5136 (fun l -> l.pageh > l.pagey + l.pagevh))
5137 then
5138 let y = clamp (pgscale state.winh) in
5139 gotoghyll y
5140 else
5141 let pageno = min (l.pageno+c) (state.pagecount-1) in
5142 gotoghyll (getpagey pageno)
5143 | Csplit (n, _) ->
5144 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5145 then
5146 let pagey, pageh = getpageyh l.pageno in
5147 let pagey = pagey + pageh * l.pagecol in
5148 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5149 gotoghyll (pagey + pageh + ips)
5152 let prevpage () =
5153 match state.layout with
5154 | [] ->
5155 let pageno = page_of_y state.y in
5156 gotoghyll (getpagey (pageno-1))
5157 | l :: _ ->
5158 match conf.columns with
5159 | Csingle _ ->
5160 if conf.presentation && l.pagey != 0
5161 then
5162 gotoghyll (clamp (pgscale ~-(state.winh)))
5163 else
5164 let pageno = max 0 (l.pageno-1) in
5165 gotoghyll (getpagey pageno)
5166 | Cmulti ((c, _, coverB) as cl, _) ->
5167 if conf.presentation &&
5168 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5169 then
5170 gotoghyll (clamp (pgscale ~-(state.winh)))
5171 else
5172 let decr =
5173 if l.pageno = state.pagecount - coverB
5174 then 1
5175 else c
5177 let pageno = max 0 (l.pageno-decr) in
5178 gotoghyll (getpagey pageno)
5179 | Csplit (n, _) ->
5180 let y =
5181 if l.pagecol = 0
5182 then
5183 if l.pageno = 0
5184 then l.pagey
5185 else
5186 let pageno = max 0 (l.pageno-1) in
5187 let pagey, pageh = getpageyh pageno in
5188 pagey + (n-1)*pageh
5189 else
5190 let pagey, pageh = getpageyh l.pageno in
5191 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5193 gotoghyll y
5196 let viewkeyboard key mask =
5197 let enttext te =
5198 let mode = state.mode in
5199 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5200 state.text <- "";
5201 enttext ();
5202 G.postRedisplay "view:enttext"
5204 let ctrl = Wsi.withctrl mask in
5205 let key =
5206 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5208 match key with
5209 | 81 -> (* Q *)
5210 exit 0
5212 | 0xff63 -> (* insert *)
5213 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5214 then (
5215 state.mode <- LinkNav (Ltgendir 0);
5216 gotoy state.y;
5218 else showtext '!' "Keyboard link navigation does not work under rotation"
5220 | 0xff1b | 113 -> (* escape / q *)
5221 begin match state.mstate with
5222 | Mzoomrect _ ->
5223 state.mstate <- Mnone;
5224 Wsi.setcursor Wsi.CURSOR_INHERIT;
5225 G.postRedisplay "kill zoom rect";
5226 | _ ->
5227 begin match state.mode with
5228 | LinkNav _ ->
5229 state.mode <- View;
5230 G.postRedisplay "esc leave linknav"
5231 | _ ->
5232 match state.ranchors with
5233 | [] -> raise Quit
5234 | (path, password, anchor, origin) :: rest ->
5235 state.ranchors <- rest;
5236 state.anchor <- anchor;
5237 state.origin <- origin;
5238 opendoc path password
5239 end;
5240 end;
5242 | 0xff08 -> (* backspace *)
5243 gotoghyll (getnav ~-1)
5245 | 111 -> (* o *)
5246 enteroutlinemode ()
5248 | 117 -> (* u *)
5249 state.rects <- [];
5250 state.text <- "";
5251 G.postRedisplay "dehighlight";
5253 | 47 | 63 -> (* / ? *)
5254 let ondone isforw s =
5255 cbput state.hists.pat s;
5256 state.searchpattern <- s;
5257 search s isforw
5259 let s = String.create 1 in
5260 s.[0] <- Char.chr key;
5261 enttext (s, "", Some (onhist state.hists.pat),
5262 textentry, ondone (key = 47), true)
5264 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5265 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5266 setzoom (conf.zoom +. incr)
5268 | 43 | 0xffab -> (* + *)
5269 let ondone s =
5270 let n =
5271 try int_of_string s with exc ->
5272 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5273 max_int
5275 if n != max_int
5276 then (
5277 conf.pagebias <- n;
5278 state.text <- "page bias is now " ^ string_of_int n;
5281 enttext ("page bias: ", "", None, intentry, ondone, true)
5283 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5284 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5285 setzoom (max 0.01 (conf.zoom -. decr))
5287 | 45 | 0xffad -> (* - *)
5288 let ondone msg = state.text <- msg in
5289 enttext (
5290 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5291 optentry state.mode, ondone, true
5294 | 48 when ctrl -> (* ctrl-0 *)
5295 if conf.zoom = 1.0
5296 then (
5297 state.x <- 0;
5298 gotoy state.y
5300 else setzoom 1.0
5302 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5303 let cols =
5304 match conf.columns with
5305 | Csingle _ | Cmulti _ -> 1
5306 | Csplit (n, _) -> n
5308 let h = state.winh -
5309 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5311 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5312 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5313 then setzoom zoom
5315 | 51 when ctrl -> (* ctrl-3 *)
5316 let fm =
5317 match conf.fitmodel with
5318 | FitWidth -> FitProportional
5319 | FitProportional -> FitPage
5320 | FitPage -> FitWidth
5322 state.text <- "fit model: " ^ fitmodel_to_string fm;
5323 reqlayout conf.angle fm
5325 | 0xffc6 -> (* f9 *)
5326 togglebirdseye ()
5328 | 57 when ctrl -> (* ctrl-9 *)
5329 togglebirdseye ()
5331 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5332 when not ctrl -> (* 0..9 *)
5333 let ondone s =
5334 let n =
5335 try int_of_string s with exc ->
5336 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5339 if n >= 0
5340 then (
5341 addnav ();
5342 cbput state.hists.pag (string_of_int n);
5343 gotopage1 (n + conf.pagebias - 1) 0;
5346 let pageentry text key =
5347 match Char.unsafe_chr key with
5348 | 'g' -> TEdone text
5349 | _ -> intentry text key
5351 let text = "x" in text.[0] <- Char.chr key;
5352 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5354 | 98 -> (* b *)
5355 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5356 reshape state.winw state.winh;
5358 | 108 -> (* l *)
5359 conf.hlinks <- not conf.hlinks;
5360 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5361 G.postRedisplay "toggle highlightlinks";
5363 | 70 -> (* F *)
5364 state.glinks <- true;
5365 let mode = state.mode in
5366 state.mode <- Textentry (
5367 (":", "", None, linknentry, linkndone gotounder, false),
5368 (fun _ ->
5369 state.glinks <- false;
5370 state.mode <- mode)
5372 state.text <- "";
5373 G.postRedisplay "view:linkent(F)"
5375 | 121 -> (* y *)
5376 state.glinks <- true;
5377 let mode = state.mode in
5378 state.mode <- Textentry (
5380 ":", "", None, linknentry, linkndone (fun under ->
5381 selstring (undertext under);
5382 ), false
5384 fun _ ->
5385 state.glinks <- false;
5386 state.mode <- mode
5388 state.text <- "";
5389 G.postRedisplay "view:linkent"
5391 | 97 -> (* a *)
5392 begin match state.autoscroll with
5393 | Some step ->
5394 conf.autoscrollstep <- step;
5395 state.autoscroll <- None
5396 | None ->
5397 if conf.autoscrollstep = 0
5398 then state.autoscroll <- Some 1
5399 else state.autoscroll <- Some conf.autoscrollstep
5402 | 112 when ctrl -> (* ctrl-p *)
5403 launchpath ()
5405 | 80 -> (* P *)
5406 setpresentationmode (not conf.presentation);
5407 showtext ' ' ("presentation mode " ^
5408 if conf.presentation then "on" else "off");
5410 | 102 -> (* f *)
5411 if List.mem Wsi.Fullscreen state.winstate
5412 then Wsi.reshape conf.cwinw conf.cwinh
5413 else Wsi.fullscreen ()
5415 | 112 | 78 -> (* p|N *)
5416 search state.searchpattern false
5418 | 110 | 0xffc0 -> (* n|F3 *)
5419 search state.searchpattern true
5421 | 116 -> (* t *)
5422 begin match state.layout with
5423 | [] -> ()
5424 | l :: _ ->
5425 gotoghyll (getpagey l.pageno)
5428 | 32 -> (* space *)
5429 nextpage ()
5431 | 0xff9f | 0xffff -> (* delete *)
5432 prevpage ()
5434 | 61 -> (* = *)
5435 showtext ' ' (describe_location ());
5437 | 119 -> (* w *)
5438 begin match state.layout with
5439 | [] -> ()
5440 | l :: _ ->
5441 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5442 G.postRedisplay "w"
5445 | 39 -> (* ' *)
5446 enterbookmarkmode ()
5448 | 104 | 0xffbe -> (* h|F1 *)
5449 enterhelpmode ()
5451 | 105 -> (* i *)
5452 enterinfomode ()
5454 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5455 entermsgsmode ()
5457 | 109 -> (* m *)
5458 let ondone s =
5459 match state.layout with
5460 | l :: _ ->
5461 if String.length s > 0
5462 then
5463 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5464 | _ -> ()
5466 enttext ("bookmark: ", "", None, textentry, ondone, true)
5468 | 126 -> (* ~ *)
5469 quickbookmark ();
5470 showtext ' ' "Quick bookmark added";
5472 | 122 -> (* z *)
5473 begin match state.layout with
5474 | l :: _ ->
5475 let rect = getpdimrect l.pagedimno in
5476 let w, h =
5477 if conf.crophack
5478 then
5479 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5480 truncate (1.2 *. (rect.(3) -. rect.(0))))
5481 else
5482 (truncate (rect.(1) -. rect.(0)),
5483 truncate (rect.(3) -. rect.(0)))
5485 let w = truncate ((float w)*.conf.zoom)
5486 and h = truncate ((float h)*.conf.zoom) in
5487 if w != 0 && h != 0
5488 then (
5489 state.anchor <- getanchor ();
5490 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5492 G.postRedisplay "z";
5494 | [] -> ()
5497 | 120 -> state.roam ()
5498 | 60 | 62 -> (* < > *)
5499 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5501 | 91 | 93 -> (* [ ] *)
5502 conf.colorscale <-
5503 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5505 G.postRedisplay "brightness";
5507 | 99 when state.mode = View -> (* [alt]-c *)
5508 if Wsi.withalt mask
5509 then (
5510 if conf.zoom > 1.0
5511 then
5512 let m = (wadjsb state.winw - state.w) / 2 in
5513 state.x <- m;
5514 gotoy_and_clear_text state.y
5516 else
5517 let (c, a, b), z =
5518 match state.prevcolumns with
5519 | None -> (1, 0, 0), 1.0
5520 | Some (columns, z) ->
5521 let cab =
5522 match columns with
5523 | Csplit (c, _) -> -c, 0, 0
5524 | Cmulti ((c, a, b), _) -> c, a, b
5525 | Csingle _ -> 1, 0, 0
5527 cab, z
5529 setcolumns View c a b;
5530 setzoom z
5532 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5533 setzoom state.prevzoom
5535 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5536 begin match state.autoscroll with
5537 | None ->
5538 begin match state.mode with
5539 | Birdseye beye -> upbirdseye 1 beye
5540 | _ ->
5541 if ctrl
5542 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5543 else (
5544 if not (Wsi.withshift mask) && conf.presentation
5545 then prevpage ()
5546 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5549 | Some n ->
5550 setautoscrollspeed n false
5553 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5554 begin match state.autoscroll with
5555 | None ->
5556 begin match state.mode with
5557 | Birdseye beye -> downbirdseye 1 beye
5558 | _ ->
5559 if ctrl
5560 then gotoy_and_clear_text (clamp (state.winh/2))
5561 else (
5562 if not (Wsi.withshift mask) && conf.presentation
5563 then nextpage ()
5564 else gotoy_and_clear_text (clamp conf.scrollstep)
5567 | Some n ->
5568 setautoscrollspeed n true
5571 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5572 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5573 if canpan ()
5574 then
5575 let dx =
5576 if ctrl
5577 then state.winw / 2
5578 else conf.hscrollstep
5580 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5581 state.x <- panbound (state.x + dx);
5582 gotoy_and_clear_text state.y
5583 else (
5584 state.text <- "";
5585 G.postRedisplay "left/right"
5588 | 0xff55 | 0xff9a -> (* (kp) prior *)
5589 let y =
5590 if ctrl
5591 then
5592 match state.layout with
5593 | [] -> state.y
5594 | l :: _ -> state.y - l.pagey
5595 else
5596 clamp (pgscale (-state.winh))
5598 gotoghyll y
5600 | 0xff56 | 0xff9b -> (* (kp) next *)
5601 let y =
5602 if ctrl
5603 then
5604 match List.rev state.layout with
5605 | [] -> state.y
5606 | l :: _ -> getpagey l.pageno
5607 else
5608 clamp (pgscale state.winh)
5610 gotoghyll y
5612 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5613 gotoghyll 0
5614 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5615 gotoghyll (clamp state.maxy)
5617 | 0xff53 | 0xff98
5618 when Wsi.withalt mask -> (* alt-(kp) right *)
5619 gotoghyll (getnav 1)
5620 | 0xff51 | 0xff96
5621 when Wsi.withalt mask -> (* alt-(kp) left *)
5622 gotoghyll (getnav ~-1)
5624 | 114 -> (* r *)
5625 reload ()
5627 | 118 when conf.debug -> (* v *)
5628 state.rects <- [];
5629 List.iter (fun l ->
5630 match getopaque l.pageno with
5631 | None -> ()
5632 | Some opaque ->
5633 let x0, y0, x1, y1 = pagebbox opaque in
5634 let a,b = float x0, float y0 in
5635 let c,d = float x1, float y0 in
5636 let e,f = float x1, float y1 in
5637 let h,j = float x0, float y1 in
5638 let rect = (a,b,c,d,e,f,h,j) in
5639 debugrect rect;
5640 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5641 ) state.layout;
5642 G.postRedisplay "v";
5644 | _ ->
5645 vlog "huh? %s" (Wsi.keyname key)
5648 let linknavkeyboard key mask linknav =
5649 let getpage pageno =
5650 let rec loop = function
5651 | [] -> None
5652 | l :: _ when l.pageno = pageno -> Some l
5653 | _ :: rest -> loop rest
5654 in loop state.layout
5656 let doexact (pageno, n) =
5657 match getopaque pageno, getpage pageno with
5658 | Some opaque, Some l ->
5659 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5660 then
5661 let under = getlink opaque n in
5662 G.postRedisplay "link gotounder";
5663 gotounder under;
5664 state.mode <- View;
5665 else
5666 let opt, dir =
5667 match key with
5668 | 0xff50 -> (* home *)
5669 Some (findlink opaque LDfirst), -1
5671 | 0xff57 -> (* end *)
5672 Some (findlink opaque LDlast), 1
5674 | 0xff51 -> (* left *)
5675 Some (findlink opaque (LDleft n)), -1
5677 | 0xff53 -> (* right *)
5678 Some (findlink opaque (LDright n)), 1
5680 | 0xff52 -> (* up *)
5681 Some (findlink opaque (LDup n)), -1
5683 | 0xff54 -> (* down *)
5684 Some (findlink opaque (LDdown n)), 1
5686 | _ -> None, 0
5688 let pwl l dir =
5689 begin match findpwl l.pageno dir with
5690 | Pwlnotfound -> ()
5691 | Pwl pageno ->
5692 let notfound dir =
5693 state.mode <- LinkNav (Ltgendir dir);
5694 let y, h = getpageyh pageno in
5695 let y =
5696 if dir < 0
5697 then y + h - state.winh
5698 else y
5700 gotoy y
5702 begin match getopaque pageno, getpage pageno with
5703 | Some opaque, Some _ ->
5704 let link =
5705 let ld = if dir > 0 then LDfirst else LDlast in
5706 findlink opaque ld
5708 begin match link with
5709 | Lfound m ->
5710 showlinktype (getlink opaque m);
5711 state.mode <- LinkNav (Ltexact (pageno, m));
5712 G.postRedisplay "linknav jpage";
5713 | _ -> notfound dir
5714 end;
5715 | _ -> notfound dir
5716 end;
5717 end;
5719 begin match opt with
5720 | Some Lnotfound -> pwl l dir;
5721 | Some (Lfound m) ->
5722 if m = n
5723 then pwl l dir
5724 else (
5725 let _, y0, _, y1 = getlinkrect opaque m in
5726 if y0 < l.pagey
5727 then gotopage1 l.pageno y0
5728 else (
5729 let d = fstate.fontsize + 1 in
5730 if y1 - l.pagey > l.pagevh - d
5731 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5732 else G.postRedisplay "linknav";
5734 showlinktype (getlink opaque m);
5735 state.mode <- LinkNav (Ltexact (l.pageno, m));
5738 | None -> viewkeyboard key mask
5739 end;
5740 | _ -> viewkeyboard key mask
5742 if key = 0xff63
5743 then (
5744 state.mode <- View;
5745 G.postRedisplay "leave linknav"
5747 else
5748 match linknav with
5749 | Ltgendir _ -> viewkeyboard key mask
5750 | Ltexact exact -> doexact exact
5753 let keyboard key mask =
5754 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5755 then wcmd "interrupt"
5756 else state.uioh <- state.uioh#key key mask
5759 let birdseyekeyboard key mask
5760 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5761 let incr =
5762 match conf.columns with
5763 | Csingle _ -> 1
5764 | Cmulti ((c, _, _), _) -> c
5765 | Csplit _ -> failwith "bird's eye split mode"
5767 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5768 match key with
5769 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5770 let y, h = getpageyh pageno in
5771 let top = (state.winh - h) / 2 in
5772 gotoy (max 0 (y - top))
5773 | 0xff0d (* enter *)
5774 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5775 | 0xff1b -> leavebirdseye beye true (* escape *)
5776 | 0xff52 -> upbirdseye incr beye (* up *)
5777 | 0xff54 -> downbirdseye incr beye (* down *)
5778 | 0xff51 -> upbirdseye 1 beye (* left *)
5779 | 0xff53 -> downbirdseye 1 beye (* right *)
5781 | 0xff55 -> (* prior *)
5782 begin match state.layout with
5783 | l :: _ ->
5784 if l.pagey != 0
5785 then (
5786 state.mode <- Birdseye (
5787 oconf, leftx, l.pageno, hooverpageno, anchor
5789 gotopage1 l.pageno 0;
5791 else (
5792 let layout = layout (state.y-state.winh) (pgh state.layout) in
5793 match layout with
5794 | [] -> gotoy (clamp (-state.winh))
5795 | l :: _ ->
5796 state.mode <- Birdseye (
5797 oconf, leftx, l.pageno, hooverpageno, anchor
5799 gotopage1 l.pageno 0
5802 | [] -> gotoy (clamp (-state.winh))
5803 end;
5805 | 0xff56 -> (* next *)
5806 begin match List.rev state.layout with
5807 | l :: _ ->
5808 let layout = layout (state.y + (pgh state.layout)) state.winh in
5809 begin match layout with
5810 | [] ->
5811 let incr = l.pageh - l.pagevh in
5812 if incr = 0
5813 then (
5814 state.mode <-
5815 Birdseye (
5816 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5818 G.postRedisplay "birdseye pagedown";
5820 else gotoy (clamp (incr + conf.interpagespace*2));
5822 | l :: _ ->
5823 state.mode <-
5824 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5825 gotopage1 l.pageno 0;
5828 | [] -> gotoy (clamp state.winh)
5829 end;
5831 | 0xff50 -> (* home *)
5832 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5833 gotopage1 0 0
5835 | 0xff57 -> (* end *)
5836 let pageno = state.pagecount - 1 in
5837 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5838 if not (pagevisible state.layout pageno)
5839 then
5840 let h =
5841 match List.rev state.pdims with
5842 | [] -> state.winh
5843 | (_, _, h, _) :: _ -> h
5845 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5846 else G.postRedisplay "birdseye end";
5847 | _ -> viewkeyboard key mask
5850 let drawpage l =
5851 let color =
5852 match state.mode with
5853 | Textentry _ -> scalecolor 0.4
5854 | LinkNav _
5855 | View -> scalecolor 1.0
5856 | Birdseye (_, _, pageno, hooverpageno, _) ->
5857 if l.pageno = hooverpageno
5858 then scalecolor 0.9
5859 else (
5860 if l.pageno = pageno
5861 then scalecolor 1.0
5862 else scalecolor 0.8
5865 drawtiles l color;
5868 let postdrawpage l linkindexbase =
5869 match getopaque l.pageno with
5870 | Some opaque ->
5871 if tileready l l.pagex l.pagey
5872 then
5873 let x = l.pagedispx - l.pagex
5874 and y = l.pagedispy - l.pagey in
5875 let hlmask =
5876 match conf.columns with
5877 | Csingle _ | Cmulti _ ->
5878 (if conf.hlinks then 1 else 0)
5879 + (if state.glinks
5880 && not (isbirdseye state.mode) then 2 else 0)
5881 | _ -> 0
5883 let s =
5884 match state.mode with
5885 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5886 | _ -> ""
5888 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5889 else 0
5890 | _ -> 0
5893 let scrollindicator () =
5894 let sbw, ph, sh = state.uioh#scrollph in
5895 let sbh, pw, sw = state.uioh#scrollpw in
5897 GlDraw.color (0.64, 0.64, 0.64);
5898 GlDraw.rect
5899 (float (state.winw - sbw), 0.)
5900 (float state.winw, float state.winh)
5902 GlDraw.rect
5903 (0., float (state.winh - sbh))
5904 (float (wadjsb state.winw - 1), float state.winh)
5906 GlDraw.color (0.0, 0.0, 0.0);
5908 GlDraw.rect
5909 (float (state.winw - sbw), ph)
5910 (float state.winw, ph +. sh)
5912 GlDraw.rect
5913 (pw, float (state.winh - sbh))
5914 (pw +. sw, float state.winh)
5918 let showsel () =
5919 match state.mstate with
5920 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5923 | Msel ((x0, y0), (x1, y1)) ->
5924 let rec loop = function
5925 | l :: ls ->
5926 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5927 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5928 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5929 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5930 then
5931 match getopaque l.pageno with
5932 | Some opaque ->
5933 let x0, y0 = pagetranslatepoint l x0 y0 in
5934 let x1, y1 = pagetranslatepoint l x1 y1 in
5935 seltext opaque (x0, y0, x1, y1);
5936 | _ -> ()
5937 else loop ls
5938 | [] -> ()
5940 loop state.layout
5943 let showrects rects =
5944 Gl.enable `blend;
5945 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5946 GlDraw.polygon_mode `both `fill;
5947 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5948 List.iter
5949 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5950 List.iter (fun l ->
5951 if l.pageno = pageno
5952 then (
5953 let dx = float (l.pagedispx - l.pagex) in
5954 let dy = float (l.pagedispy - l.pagey) in
5955 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5956 GlDraw.begins `quads;
5958 GlDraw.vertex2 (x0+.dx, y0+.dy);
5959 GlDraw.vertex2 (x1+.dx, y1+.dy);
5960 GlDraw.vertex2 (x2+.dx, y2+.dy);
5961 GlDraw.vertex2 (x3+.dx, y3+.dy);
5963 GlDraw.ends ();
5965 ) state.layout
5966 ) rects
5968 Gl.disable `blend;
5971 let display () =
5972 GlClear.color (scalecolor2 conf.bgcolor);
5973 GlClear.clear [`color];
5974 List.iter drawpage state.layout;
5975 let rects =
5976 match state.mode with
5977 | LinkNav (Ltexact (pageno, linkno)) ->
5978 begin match getopaque pageno with
5979 | Some opaque ->
5980 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5981 (pageno, 5, (
5982 float x0, float y0,
5983 float x1, float y0,
5984 float x1, float y1,
5985 float x0, float y1)
5986 ) :: state.rects
5987 | None -> state.rects
5989 | _ -> state.rects
5991 showrects rects;
5992 let rec postloop linkindexbase = function
5993 | l :: rest ->
5994 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5995 postloop linkindexbase rest
5996 | [] -> ()
5998 showsel ();
5999 postloop 0 state.layout;
6000 state.uioh#display;
6001 begin match state.mstate with
6002 | Mzoomrect ((x0, y0), (x1, y1)) ->
6003 Gl.enable `blend;
6004 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6005 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6006 GlDraw.rect (float x0, float y0)
6007 (float x1, float y1);
6008 Gl.disable `blend;
6009 | _ -> ()
6010 end;
6011 enttext ();
6012 scrollindicator ();
6013 Wsi.swapb ();
6016 let zoomrect x y x1 y1 =
6017 let x0 = min x x1
6018 and x1 = max x x1
6019 and y0 = min y y1 in
6020 gotoy (state.y + y0);
6021 state.anchor <- getanchor ();
6022 let zoom = (float state.w) /. float (x1 - x0) in
6023 let margin =
6024 match conf.fitmodel, conf.columns with
6025 | FitPage, Csplit _ ->
6026 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6028 | _, _ ->
6029 let adjw = wadjsb state.winw in
6030 if state.w < adjw
6031 then (adjw - state.w) / 2
6032 else 0
6034 state.x <- (state.x + margin) - x0;
6035 setzoom zoom;
6036 Wsi.setcursor Wsi.CURSOR_INHERIT;
6037 state.mstate <- Mnone;
6040 let scrollx x =
6041 let winw = wadjsb state.winw - 1 in
6042 let s = float x /. float winw in
6043 let destx = truncate (float (state.w + winw) *. s) in
6044 state.x <- winw - destx;
6045 gotoy_and_clear_text state.y;
6046 state.mstate <- Mscrollx;
6049 let scrolly y =
6050 let s = float y /. float state.winh in
6051 let desty = truncate (float (state.maxy - state.winh) *. s) in
6052 gotoy_and_clear_text desty;
6053 state.mstate <- Mscrolly;
6056 let viewmouse button down x y mask =
6057 match button with
6058 | n when (n == 4 || n == 5) && not down ->
6059 if Wsi.withctrl mask
6060 then (
6061 match state.mstate with
6062 | Mzoom (oldn, i) ->
6063 if oldn = n
6064 then (
6065 if i = 2
6066 then
6067 let incr =
6068 match n with
6069 | 5 ->
6070 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6071 | _ ->
6072 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6074 let zoom = conf.zoom -. incr in
6075 setzoom zoom;
6076 state.mstate <- Mzoom (n, 0);
6077 else
6078 state.mstate <- Mzoom (n, i+1);
6080 else state.mstate <- Mzoom (n, 0)
6082 | _ -> state.mstate <- Mzoom (n, 0)
6084 else (
6085 match state.autoscroll with
6086 | Some step -> setautoscrollspeed step (n=4)
6087 | None ->
6088 if conf.wheelbypage || conf.presentation
6089 then (
6090 if n = 4
6091 then prevpage ()
6092 else nextpage ()
6094 else
6095 let incr =
6096 if n = 4
6097 then -conf.scrollstep
6098 else conf.scrollstep
6100 let incr = incr * 2 in
6101 let y = clamp incr in
6102 gotoy_and_clear_text y
6105 | n when (n = 6 || n = 7) && not down && canpan () ->
6106 state.x <-
6107 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6108 gotoy_and_clear_text state.y
6110 | 1 when Wsi.withshift mask ->
6111 state.mstate <- Mnone;
6112 if not down
6113 then (
6114 match unproject x y with
6115 | Some (pageno, ux, uy) ->
6116 let cmd = Printf.sprintf
6117 "%s %s %d %d %d"
6118 conf.stcmd state.path pageno ux uy
6120 popen cmd []
6121 | None -> ()
6124 | 1 when Wsi.withctrl mask ->
6125 if down
6126 then (
6127 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6128 state.mstate <- Mpan (x, y)
6130 else
6131 state.mstate <- Mnone
6133 | 3 ->
6134 if down
6135 then (
6136 Wsi.setcursor Wsi.CURSOR_CYCLE;
6137 let p = (x, y) in
6138 state.mstate <- Mzoomrect (p, p)
6140 else (
6141 match state.mstate with
6142 | Mzoomrect ((x0, y0), _) ->
6143 if abs (x-x0) > 10 && abs (y - y0) > 10
6144 then zoomrect x0 y0 x y
6145 else (
6146 state.mstate <- Mnone;
6147 Wsi.setcursor Wsi.CURSOR_INHERIT;
6148 G.postRedisplay "kill accidental zoom rect";
6150 | _ ->
6151 Wsi.setcursor Wsi.CURSOR_INHERIT;
6152 state.mstate <- Mnone
6155 | 1 when x > state.winw - vscrollw () ->
6156 if down
6157 then
6158 let _, position, sh = state.uioh#scrollph in
6159 if y > truncate position && y < truncate (position +. sh)
6160 then state.mstate <- Mscrolly
6161 else scrolly y
6162 else
6163 state.mstate <- Mnone
6165 | 1 when y > state.winh - hscrollh () ->
6166 if down
6167 then
6168 let _, position, sw = state.uioh#scrollpw in
6169 if x > truncate position && x < truncate (position +. sw)
6170 then state.mstate <- Mscrollx
6171 else scrollx x
6172 else
6173 state.mstate <- Mnone
6175 | 1 ->
6176 let dest = if down then getunder x y else Unone in
6177 begin match dest with
6178 | Ulinkgoto _
6179 | Ulinkuri _
6180 | Uremote _
6181 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6182 gotounder dest
6184 | Unone when down ->
6185 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6186 state.mstate <- Mpan (x, y);
6188 | Unone | Utext _ ->
6189 if down
6190 then (
6191 if conf.angle mod 360 = 0
6192 then (
6193 state.mstate <- Msel ((x, y), (x, y));
6194 G.postRedisplay "mouse select";
6197 else (
6198 match state.mstate with
6199 | Mnone -> ()
6201 | Mzoom _ | Mscrollx | Mscrolly ->
6202 state.mstate <- Mnone
6204 | Mzoomrect ((x0, y0), _) ->
6205 zoomrect x0 y0 x y
6207 | Mpan _ ->
6208 Wsi.setcursor Wsi.CURSOR_INHERIT;
6209 state.mstate <- Mnone
6211 | Msel ((x0, y0), (x1, y1)) ->
6212 let rec loop = function
6213 | [] -> ()
6214 | l :: rest ->
6215 let inside =
6216 let a0 = l.pagedispy in
6217 let a1 = a0 + l.pagevh in
6218 let b0 = l.pagedispx in
6219 let b1 = b0 + l.pagevw in
6220 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6221 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6223 if inside
6224 then
6225 match getopaque l.pageno with
6226 | Some opaque ->
6227 begin
6228 match Ne.pipe () with
6229 | Ne.Exn exn ->
6230 showtext '!'
6231 (Printf.sprintf
6232 "can not create sel pipe: %s"
6233 (exntos exn));
6234 | Ne.Res (r, w) ->
6235 let doclose what fd =
6236 Ne.clo fd (fun msg ->
6237 dolog "%s close failed: %s" what msg)
6240 popen conf.selcmd [r, 0; w, -1];
6241 copysel w opaque true;
6242 doclose "pipe/r" r;
6243 G.postRedisplay "copysel";
6244 with exn ->
6245 dolog "can not execute %S: %s"
6246 conf.selcmd (exntos exn);
6247 doclose "pipe/r" r;
6248 doclose "pipe/w" w;
6250 | None -> ()
6251 else loop rest
6253 loop state.layout;
6254 Wsi.setcursor Wsi.CURSOR_INHERIT;
6255 state.mstate <- Mnone;
6259 | _ -> ()
6262 let birdseyemouse button down x y mask
6263 (conf, leftx, _, hooverpageno, anchor) =
6264 match button with
6265 | 1 when down ->
6266 let rec loop = function
6267 | [] -> ()
6268 | l :: rest ->
6269 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6270 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6271 then (
6272 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6274 else loop rest
6276 loop state.layout
6277 | 3 -> ()
6278 | _ -> viewmouse button down x y mask
6281 let mouse button down x y mask =
6282 state.uioh <- state.uioh#button button down x y mask;
6285 let motion ~x ~y =
6286 state.uioh <- state.uioh#motion x y
6289 let pmotion ~x ~y =
6290 state.uioh <- state.uioh#pmotion x y;
6293 let uioh = object
6294 method display = ()
6296 method key key mask =
6297 begin match state.mode with
6298 | Textentry textentry -> textentrykeyboard key mask textentry
6299 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6300 | View -> viewkeyboard key mask
6301 | LinkNav linknav -> linknavkeyboard key mask linknav
6302 end;
6303 state.uioh
6305 method button button bstate x y mask =
6306 begin match state.mode with
6307 | LinkNav _
6308 | View -> viewmouse button bstate x y mask
6309 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6310 | Textentry _ -> ()
6311 end;
6312 state.uioh
6314 method motion x y =
6315 begin match state.mode with
6316 | Textentry _ -> ()
6317 | View | Birdseye _ | LinkNav _ ->
6318 match state.mstate with
6319 | Mzoom _ | Mnone -> ()
6321 | Mpan (x0, y0) ->
6322 let dx = x - x0
6323 and dy = y0 - y in
6324 state.mstate <- Mpan (x, y);
6325 if canpan ()
6326 then state.x <- panbound (state.x + dx);
6327 let y = clamp dy in
6328 gotoy_and_clear_text y
6330 | Msel (a, _) ->
6331 state.mstate <- Msel (a, (x, y));
6332 G.postRedisplay "motion select";
6334 | Mscrolly ->
6335 let y = min state.winh (max 0 y) in
6336 scrolly y
6338 | Mscrollx ->
6339 let x = min state.winw (max 0 x) in
6340 scrollx x
6342 | Mzoomrect (p0, _) ->
6343 state.mstate <- Mzoomrect (p0, (x, y));
6344 G.postRedisplay "motion zoomrect";
6345 end;
6346 state.uioh
6348 method pmotion x y =
6349 begin match state.mode with
6350 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6351 let rec loop = function
6352 | [] ->
6353 if hooverpageno != -1
6354 then (
6355 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6356 G.postRedisplay "pmotion birdseye no hoover";
6358 | l :: rest ->
6359 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6360 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6361 then (
6362 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6363 G.postRedisplay "pmotion birdseye hoover";
6365 else loop rest
6367 loop state.layout
6369 | Textentry _ -> ()
6371 | LinkNav _
6372 | View ->
6373 match state.mstate with
6374 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6376 | Mnone ->
6377 updateunder x y;
6378 match conf.pax with
6379 | None -> ()
6380 | Some r ->
6381 let past, _, _ = !r in
6382 let now = now () in
6383 let delta = now -. past in
6384 if delta > 0.01
6385 then paxunder x y
6386 else r := (now, x, y)
6387 end;
6388 state.uioh
6390 method infochanged _ = ()
6392 method scrollph =
6393 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6394 let p, h =
6395 if maxy = 0
6396 then 0.0, float state.winh
6397 else scrollph state.y maxy
6399 vscrollw (), p, h
6401 method scrollpw =
6402 let winw = wadjsb state.winw in
6403 let fwinw = float winw in
6404 let sw =
6405 let sw = fwinw /. float state.w in
6406 let sw = fwinw *. sw in
6407 max sw (float conf.scrollh)
6409 let position =
6410 let maxx = state.w + winw in
6411 let x = winw - state.x in
6412 let percent = float x /. float maxx in
6413 (fwinw -. sw) *. percent
6415 hscrollh (), position, sw
6417 method modehash =
6418 let modename =
6419 match state.mode with
6420 | LinkNav _ -> "links"
6421 | Textentry _ -> "textentry"
6422 | Birdseye _ -> "birdseye"
6423 | View -> "view"
6425 findkeyhash conf modename
6427 method eformsgs = true
6428 end;;
6430 module Config =
6431 struct
6432 open Parser
6434 let fontpath = ref "";;
6436 module KeyMap =
6437 Map.Make (struct type t = (int * int) let compare = compare end);;
6439 let unent s =
6440 let l = String.length s in
6441 let b = Buffer.create l in
6442 unent b s 0 l;
6443 Buffer.contents b;
6446 let home =
6447 try Sys.getenv "HOME"
6448 with exn ->
6449 prerr_endline
6450 ("Can not determine home directory location: " ^ exntos exn);
6454 let modifier_of_string = function
6455 | "alt" -> Wsi.altmask
6456 | "shift" -> Wsi.shiftmask
6457 | "ctrl" | "control" -> Wsi.ctrlmask
6458 | "meta" -> Wsi.metamask
6459 | _ -> 0
6462 let key_of_string =
6463 let r = Str.regexp "-" in
6464 fun s ->
6465 let elems = Str.full_split r s in
6466 let f n k m =
6467 let g s =
6468 let m1 = modifier_of_string s in
6469 if m1 = 0
6470 then (Wsi.namekey s, m)
6471 else (k, m lor m1)
6472 in function
6473 | Str.Delim s when n land 1 = 0 -> g s
6474 | Str.Text s -> g s
6475 | Str.Delim _ -> (k, m)
6477 let rec loop n k m = function
6478 | [] -> (k, m)
6479 | x :: xs ->
6480 let k, m = f n k m x in
6481 loop (n+1) k m xs
6483 loop 0 0 0 elems
6486 let keys_of_string =
6487 let r = Str.regexp "[ \t]" in
6488 fun s ->
6489 let elems = Str.split r s in
6490 List.map key_of_string elems
6493 let copykeyhashes c =
6494 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6497 let config_of c attrs =
6498 let apply c k v =
6500 match k with
6501 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6502 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6503 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6504 | "preload" -> { c with preload = bool_of_string v }
6505 | "page-bias" -> { c with pagebias = int_of_string v }
6506 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6507 | "horizontal-scroll-step" ->
6508 { c with hscrollstep = max (int_of_string v) 1 }
6509 | "auto-scroll-step" ->
6510 { c with autoscrollstep = max 0 (int_of_string v) }
6511 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6512 | "crop-hack" -> { c with crophack = bool_of_string v }
6513 | "throttle" ->
6514 let mw =
6515 match String.lowercase v with
6516 | "true" -> Some infinity
6517 | "false" -> None
6518 | f -> Some (float_of_string f)
6520 { c with maxwait = mw}
6521 | "highlight-links" -> { c with hlinks = bool_of_string v }
6522 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6523 | "vertical-margin" ->
6524 { c with interpagespace = max 0 (int_of_string v) }
6525 | "zoom" ->
6526 let zoom = float_of_string v /. 100. in
6527 let zoom = max zoom 0.0 in
6528 { c with zoom = zoom }
6529 | "presentation" -> { c with presentation = bool_of_string v }
6530 | "rotation-angle" -> { c with angle = int_of_string v }
6531 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6532 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6533 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6534 | "proportional-display" ->
6535 let fm =
6536 if bool_of_string v
6537 then FitProportional
6538 else FitWidth
6540 { c with fitmodel = fm }
6541 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6542 | "pixmap-cache-size" ->
6543 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6544 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6545 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6546 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6547 | "persistent-location" -> { c with jumpback = bool_of_string v }
6548 | "background-color" -> { c with bgcolor = color_of_string v }
6549 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6550 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6551 | "mupdf-store-size" ->
6552 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6553 | "checkers" -> { c with checkers = bool_of_string v }
6554 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6555 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6556 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6557 | "uri-launcher" -> { c with urilauncher = unent v }
6558 | "path-launcher" -> { c with pathlauncher = unent v }
6559 | "color-space" -> { c with colorspace = colorspace_of_string v }
6560 | "invert-colors" -> { c with invert = bool_of_string v }
6561 | "brightness" -> { c with colorscale = float_of_string v }
6562 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6563 | "ghyllscroll" ->
6564 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6565 | "columns" ->
6566 let (n, _, _) as nab = multicolumns_of_string v in
6567 if n < 0
6568 then { c with columns = Csplit (-n, [||]) }
6569 else { c with columns = Cmulti (nab, [||]) }
6570 | "birds-eye-columns" ->
6571 { c with beyecolumns = Some (max (int_of_string v) 2) }
6572 | "selection-command" -> { c with selcmd = unent v }
6573 | "synctex-command" -> { c with stcmd = unent v }
6574 | "pax-command" -> { c with paxcmd = unent v }
6575 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6576 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6577 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6578 | "use-pbo" -> { c with usepbo = bool_of_string v }
6579 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6580 | "horizontal-scrollbar-visible" ->
6581 let b =
6582 if bool_of_string v
6583 then c.scrollb lor scrollbhv
6584 else c.scrollb land (lnot scrollbhv)
6586 { c with scrollb = b }
6587 | "vertical-scrollbar-visible" ->
6588 let b =
6589 if bool_of_string v
6590 then c.scrollb lor scrollbvv
6591 else c.scrollb land (lnot scrollbvv)
6593 { c with scrollb = b }
6594 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6595 | "point-and-x" ->
6596 { c with pax =
6597 if bool_of_string v
6598 then Some (ref (0.0, 0, 0))
6599 else None }
6600 | _ -> c
6601 with exn ->
6602 prerr_endline ("Error processing attribute (`" ^
6603 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6606 let rec fold c = function
6607 | [] -> c
6608 | (k, v) :: rest ->
6609 let c = apply c k v in
6610 fold c rest
6612 fold { c with keyhashes = copykeyhashes c } attrs;
6615 let fromstring f pos n v d =
6616 try f v
6617 with exn ->
6618 dolog "Error processing attribute (%S=%S) at %d\n%s"
6619 n v pos (exntos exn)
6624 let bookmark_of attrs =
6625 let rec fold title page rely visy = function
6626 | ("title", v) :: rest -> fold v page rely visy rest
6627 | ("page", v) :: rest -> fold title v rely visy rest
6628 | ("rely", v) :: rest -> fold title page v visy rest
6629 | ("visy", v) :: rest -> fold title page rely v rest
6630 | _ :: rest -> fold title page rely visy rest
6631 | [] -> title, page, rely, visy
6633 fold "invalid" "0" "0" "0" attrs
6636 let doc_of attrs =
6637 let rec fold path page rely pan visy = function
6638 | ("path", v) :: rest -> fold v page rely pan visy rest
6639 | ("page", v) :: rest -> fold path v rely pan visy rest
6640 | ("rely", v) :: rest -> fold path page v pan visy rest
6641 | ("pan", v) :: rest -> fold path page rely v visy rest
6642 | ("visy", v) :: rest -> fold path page rely pan v rest
6643 | _ :: rest -> fold path page rely pan visy rest
6644 | [] -> path, page, rely, pan, visy
6646 fold "" "0" "0" "0" "0" attrs
6649 let map_of attrs =
6650 let rec fold rs ls = function
6651 | ("out", v) :: rest -> fold v ls rest
6652 | ("in", v) :: rest -> fold rs v rest
6653 | _ :: rest -> fold ls rs rest
6654 | [] -> ls, rs
6656 fold "" "" attrs
6659 let setconf dst src =
6660 dst.scrollbw <- src.scrollbw;
6661 dst.scrollh <- src.scrollh;
6662 dst.icase <- src.icase;
6663 dst.preload <- src.preload;
6664 dst.pagebias <- src.pagebias;
6665 dst.verbose <- src.verbose;
6666 dst.scrollstep <- src.scrollstep;
6667 dst.maxhfit <- src.maxhfit;
6668 dst.crophack <- src.crophack;
6669 dst.autoscrollstep <- src.autoscrollstep;
6670 dst.maxwait <- src.maxwait;
6671 dst.hlinks <- src.hlinks;
6672 dst.underinfo <- src.underinfo;
6673 dst.interpagespace <- src.interpagespace;
6674 dst.zoom <- src.zoom;
6675 dst.presentation <- src.presentation;
6676 dst.angle <- src.angle;
6677 dst.cwinw <- src.cwinw;
6678 dst.cwinh <- src.cwinh;
6679 dst.savebmarks <- src.savebmarks;
6680 dst.memlimit <- src.memlimit;
6681 dst.fitmodel <- src.fitmodel;
6682 dst.texcount <- src.texcount;
6683 dst.sliceheight <- src.sliceheight;
6684 dst.thumbw <- src.thumbw;
6685 dst.jumpback <- src.jumpback;
6686 dst.bgcolor <- src.bgcolor;
6687 dst.tilew <- src.tilew;
6688 dst.tileh <- src.tileh;
6689 dst.mustoresize <- src.mustoresize;
6690 dst.checkers <- src.checkers;
6691 dst.aalevel <- src.aalevel;
6692 dst.trimmargins <- src.trimmargins;
6693 dst.trimfuzz <- src.trimfuzz;
6694 dst.urilauncher <- src.urilauncher;
6695 dst.colorspace <- src.colorspace;
6696 dst.invert <- src.invert;
6697 dst.colorscale <- src.colorscale;
6698 dst.redirectstderr <- src.redirectstderr;
6699 dst.ghyllscroll <- src.ghyllscroll;
6700 dst.columns <- src.columns;
6701 dst.beyecolumns <- src.beyecolumns;
6702 dst.selcmd <- src.selcmd;
6703 dst.updatecurs <- src.updatecurs;
6704 dst.pathlauncher <- src.pathlauncher;
6705 dst.keyhashes <- copykeyhashes src;
6706 dst.hfsize <- src.hfsize;
6707 dst.hscrollstep <- src.hscrollstep;
6708 dst.pgscale <- src.pgscale;
6709 dst.usepbo <- src.usepbo;
6710 dst.wheelbypage <- src.wheelbypage;
6711 dst.stcmd <- src.stcmd;
6712 dst.paxcmd <- src.paxcmd;
6713 dst.scrollb <- src.scrollb;
6714 dst.riani <- src.riani;
6715 dst.pax <-
6716 if src.pax = None
6717 then None
6718 else Some ((ref (0.0, 0, 0)));
6721 let get s =
6722 let h = Hashtbl.create 10 in
6723 let dc = { defconf with angle = defconf.angle } in
6724 let rec toplevel v t spos _ =
6725 match t with
6726 | Vdata | Vcdata | Vend -> v
6727 | Vopen ("llppconfig", _, closed) ->
6728 if closed
6729 then v
6730 else { v with f = llppconfig }
6731 | Vopen _ ->
6732 error "unexpected subelement at top level" s spos
6733 | Vclose _ -> error "unexpected close at top level" s spos
6735 and llppconfig v t spos _ =
6736 match t with
6737 | Vdata | Vcdata -> v
6738 | Vend -> error "unexpected end of input in llppconfig" s spos
6739 | Vopen ("defaults", attrs, closed) ->
6740 let c = config_of dc attrs in
6741 setconf dc c;
6742 if closed
6743 then v
6744 else { v with f = defaults }
6746 | Vopen ("ui-font", attrs, closed) ->
6747 let rec getsize size = function
6748 | [] -> size
6749 | ("size", v) :: rest ->
6750 let size =
6751 fromstring int_of_string spos "size" v fstate.fontsize in
6752 getsize size rest
6753 | l -> getsize size l
6755 fstate.fontsize <- getsize fstate.fontsize attrs;
6756 if closed
6757 then v
6758 else { v with f = uifont (Buffer.create 10) }
6760 | Vopen ("doc", attrs, closed) ->
6761 let pathent, spage, srely, span, svisy = doc_of attrs in
6762 let path = unent pathent
6763 and pageno = fromstring int_of_string spos "page" spage 0
6764 and rely = fromstring float_of_string spos "rely" srely 0.0
6765 and pan = fromstring int_of_string spos "pan" span 0
6766 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6767 let c = config_of dc attrs in
6768 let anchor = (pageno, rely, visy) in
6769 if closed
6770 then (Hashtbl.add h path (c, [], pan, anchor); v)
6771 else { v with f = doc path pan anchor c [] }
6773 | Vopen _ ->
6774 error "unexpected subelement in llppconfig" s spos
6776 | Vclose "llppconfig" -> { v with f = toplevel }
6777 | Vclose _ -> error "unexpected close in llppconfig" s spos
6779 and defaults v t spos _ =
6780 match t with
6781 | Vdata | Vcdata -> v
6782 | Vend -> error "unexpected end of input in defaults" s spos
6783 | Vopen ("keymap", attrs, closed) ->
6784 let modename =
6785 try List.assoc "mode" attrs
6786 with Not_found -> "global" in
6787 if closed
6788 then v
6789 else
6790 let ret keymap =
6791 let h = findkeyhash dc modename in
6792 KeyMap.iter (Hashtbl.replace h) keymap;
6793 defaults
6795 { v with f = pkeymap ret KeyMap.empty }
6797 | Vopen (_, _, _) ->
6798 error "unexpected subelement in defaults" s spos
6800 | Vclose "defaults" ->
6801 { v with f = llppconfig }
6803 | Vclose _ -> error "unexpected close in defaults" s spos
6805 and uifont b v t spos epos =
6806 match t with
6807 | Vdata | Vcdata ->
6808 Buffer.add_substring b s spos (epos - spos);
6810 | Vopen (_, _, _) ->
6811 error "unexpected subelement in ui-font" s spos
6812 | Vclose "ui-font" ->
6813 if String.length !fontpath = 0
6814 then fontpath := Buffer.contents b;
6815 { v with f = llppconfig }
6816 | Vclose _ -> error "unexpected close in ui-font" s spos
6817 | Vend -> error "unexpected end of input in ui-font" s spos
6819 and doc path pan anchor c bookmarks v t spos _ =
6820 match t with
6821 | Vdata | Vcdata -> v
6822 | Vend -> error "unexpected end of input in doc" s spos
6823 | Vopen ("bookmarks", _, closed) ->
6824 if closed
6825 then v
6826 else { v with f = pbookmarks path pan anchor c bookmarks }
6828 | Vopen ("keymap", attrs, closed) ->
6829 let modename =
6830 try List.assoc "mode" attrs
6831 with Not_found -> "global"
6833 if closed
6834 then v
6835 else
6836 let ret keymap =
6837 let h = findkeyhash c modename in
6838 KeyMap.iter (Hashtbl.replace h) keymap;
6839 doc path pan anchor c bookmarks
6841 { v with f = pkeymap ret KeyMap.empty }
6843 | Vopen (_, _, _) ->
6844 error "unexpected subelement in doc" s spos
6846 | Vclose "doc" ->
6847 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6848 { v with f = llppconfig }
6850 | Vclose _ -> error "unexpected close in doc" s spos
6852 and pkeymap ret keymap v t spos _ =
6853 match t with
6854 | Vdata | Vcdata -> v
6855 | Vend -> error "unexpected end of input in keymap" s spos
6856 | Vopen ("map", attrs, closed) ->
6857 let r, l = map_of attrs in
6858 let kss = fromstring keys_of_string spos "in" r [] in
6859 let lss = fromstring keys_of_string spos "out" l [] in
6860 let keymap =
6861 match kss with
6862 | [] -> keymap
6863 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6864 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6866 if closed
6867 then { v with f = pkeymap ret keymap }
6868 else
6869 let f () = v in
6870 { v with f = skip "map" f }
6872 | Vopen _ ->
6873 error "unexpected subelement in keymap" s spos
6875 | Vclose "keymap" ->
6876 { v with f = ret keymap }
6878 | Vclose _ -> error "unexpected close in keymap" s spos
6880 and pbookmarks path pan anchor c bookmarks v t spos _ =
6881 match t with
6882 | Vdata | Vcdata -> v
6883 | Vend -> error "unexpected end of input in bookmarks" s spos
6884 | Vopen ("item", attrs, closed) ->
6885 let titleent, spage, srely, svisy = bookmark_of attrs in
6886 let page = fromstring int_of_string spos "page" spage 0
6887 and rely = fromstring float_of_string spos "rely" srely 0.0
6888 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6889 let bookmarks =
6890 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6892 if closed
6893 then { v with f = pbookmarks path pan anchor c bookmarks }
6894 else
6895 let f () = v in
6896 { v with f = skip "item" f }
6898 | Vopen _ ->
6899 error "unexpected subelement in bookmarks" s spos
6901 | Vclose "bookmarks" ->
6902 { v with f = doc path pan anchor c bookmarks }
6904 | Vclose _ -> error "unexpected close in bookmarks" s spos
6906 and skip tag f v t spos _ =
6907 match t with
6908 | Vdata | Vcdata -> v
6909 | Vend ->
6910 error ("unexpected end of input in skipped " ^ tag) s spos
6911 | Vopen (tag', _, closed) ->
6912 if closed
6913 then v
6914 else
6915 let f' () = { v with f = skip tag f } in
6916 { v with f = skip tag' f' }
6917 | Vclose ctag ->
6918 if tag = ctag
6919 then f ()
6920 else error ("unexpected close in skipped " ^ tag) s spos
6923 parse { f = toplevel; accu = () } s;
6924 h, dc;
6927 let do_load f ic =
6929 let len = in_channel_length ic in
6930 let s = String.create len in
6931 really_input ic s 0 len;
6932 f s;
6933 with
6934 | Parse_error (msg, s, pos) ->
6935 let subs = subs s pos in
6936 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6937 failwith ("parse error: " ^ s)
6939 | exn ->
6940 failwith ("config load error: " ^ exntos exn)
6943 let defconfpath =
6944 let dir =
6946 let dir = Filename.concat home ".config" in
6947 if Sys.is_directory dir then dir else home
6948 with _ -> home
6950 Filename.concat dir "llpp.conf"
6953 let confpath = ref defconfpath;;
6955 let load1 f =
6956 if Sys.file_exists !confpath
6957 then
6958 match
6959 (try Some (open_in_bin !confpath)
6960 with exn ->
6961 prerr_endline
6962 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6963 exntos exn);
6964 None
6966 with
6967 | Some ic ->
6968 let success =
6970 f (do_load get ic)
6971 with exn ->
6972 prerr_endline
6973 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6974 exntos exn);
6975 false
6977 close_in ic;
6978 success
6980 | None -> false
6981 else
6982 f (Hashtbl.create 0, defconf)
6985 let load () =
6986 let f (h, dc) =
6987 let pc, pb, px, pa =
6989 let key =
6990 if String.length state.origin = 0
6991 then state.path
6992 else state.origin
6994 Hashtbl.find h (Filename.basename key)
6995 with Not_found -> dc, [], 0, emptyanchor
6997 setconf defconf dc;
6998 setconf conf pc;
6999 state.bookmarks <- pb;
7000 state.x <- px;
7001 if conf.jumpback
7002 then state.anchor <- pa;
7003 cbput state.hists.nav pa;
7004 true
7006 load1 f
7009 let add_attrs bb always dc c =
7010 let ob s a b =
7011 if always || a != b
7012 then Printf.bprintf bb "\n %s='%b'" s a
7013 and op s a b =
7014 if always || a <> b
7015 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7016 and oi s a b =
7017 if always || a != b
7018 then Printf.bprintf bb "\n %s='%d'" s a
7019 and oI s a b =
7020 if always || a != b
7021 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7022 and oz s a b =
7023 if always || a <> b
7024 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7025 and oF s a b =
7026 if always || a <> b
7027 then Printf.bprintf bb "\n %s='%f'" s a
7028 and oc s a b =
7029 if always || a <> b
7030 then
7031 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7032 and oC s a b =
7033 if always || a <> b
7034 then
7035 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
7036 and oR s a b =
7037 if always || a <> b
7038 then
7039 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7040 and os s a b =
7041 if always || a <> b
7042 then
7043 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7044 and og s a b =
7045 if always || a <> b
7046 then
7047 match a with
7048 | None -> ()
7049 | Some (_N, _A, _B) ->
7050 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7051 and oW s a b =
7052 if always || a <> b
7053 then
7054 let v =
7055 match a with
7056 | None -> "false"
7057 | Some f ->
7058 if f = infinity
7059 then "true"
7060 else string_of_float f
7062 Printf.bprintf bb "\n %s='%s'" s v
7063 and oco s a b =
7064 if always || a <> b
7065 then
7066 match a with
7067 | Cmulti ((n, a, b), _) when n > 1 ->
7068 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7069 | Csplit (n, _) when n > 1 ->
7070 Printf.bprintf bb "\n %s='%d'" s ~-n
7071 | _ -> ()
7072 and obeco s a b =
7073 if always || a <> b
7074 then
7075 match a with
7076 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7077 | _ -> ()
7078 and oFm s a b =
7079 if always || a <> b
7080 then
7081 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
7082 and oSv s a b m =
7083 if always || a <> b
7084 then
7085 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7087 oi "width" c.cwinw dc.cwinw;
7088 oi "height" c.cwinh dc.cwinh;
7089 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7090 oi "scroll-handle-height" c.scrollh dc.scrollh;
7091 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7092 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7093 ob "case-insensitive-search" c.icase dc.icase;
7094 ob "preload" c.preload dc.preload;
7095 oi "page-bias" c.pagebias dc.pagebias;
7096 oi "scroll-step" c.scrollstep dc.scrollstep;
7097 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7098 ob "max-height-fit" c.maxhfit dc.maxhfit;
7099 ob "crop-hack" c.crophack dc.crophack;
7100 oW "throttle" c.maxwait dc.maxwait;
7101 ob "highlight-links" c.hlinks dc.hlinks;
7102 ob "under-cursor-info" c.underinfo dc.underinfo;
7103 oi "vertical-margin" c.interpagespace dc.interpagespace;
7104 oz "zoom" c.zoom dc.zoom;
7105 ob "presentation" c.presentation dc.presentation;
7106 oi "rotation-angle" c.angle dc.angle;
7107 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7108 oFm "fit-model" c.fitmodel dc.fitmodel;
7109 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7110 oi "tex-count" c.texcount dc.texcount;
7111 oi "slice-height" c.sliceheight dc.sliceheight;
7112 oi "thumbnail-width" c.thumbw dc.thumbw;
7113 ob "persistent-location" c.jumpback dc.jumpback;
7114 oc "background-color" c.bgcolor dc.bgcolor;
7115 oi "tile-width" c.tilew dc.tilew;
7116 oi "tile-height" c.tileh dc.tileh;
7117 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7118 ob "checkers" c.checkers dc.checkers;
7119 oi "aalevel" c.aalevel dc.aalevel;
7120 ob "trim-margins" c.trimmargins dc.trimmargins;
7121 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7122 os "uri-launcher" c.urilauncher dc.urilauncher;
7123 os "path-launcher" c.pathlauncher dc.pathlauncher;
7124 oC "color-space" c.colorspace dc.colorspace;
7125 ob "invert-colors" c.invert dc.invert;
7126 oF "brightness" c.colorscale dc.colorscale;
7127 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7128 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7129 oco "columns" c.columns dc.columns;
7130 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7131 os "selection-command" c.selcmd dc.selcmd;
7132 os "synctex-command" c.stcmd dc.stcmd;
7133 os "pax-command" c.paxcmd dc.paxcmd;
7134 ob "update-cursor" c.updatecurs dc.updatecurs;
7135 oi "hint-font-size" c.hfsize dc.hfsize;
7136 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7137 oF "page-scroll-scale" c.pgscale dc.pgscale;
7138 ob "use-pbo" c.usepbo dc.usepbo;
7139 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7140 ob "remote-in-a-new-instance" c.riani dc.riani;
7141 op "point-and-x" c.pax dc.pax;
7144 let keymapsbuf always dc c =
7145 let bb = Buffer.create 16 in
7146 let rec loop = function
7147 | [] -> ()
7148 | (modename, h) :: rest ->
7149 let dh = findkeyhash dc modename in
7150 if always || h <> dh
7151 then (
7152 if Hashtbl.length h > 0
7153 then (
7154 if Buffer.length bb > 0
7155 then Buffer.add_char bb '\n';
7156 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7157 Hashtbl.iter (fun i o ->
7158 let isdifferent = always ||
7160 let dO = Hashtbl.find dh i in
7161 dO <> o
7162 with Not_found -> true
7164 if isdifferent
7165 then
7166 let addkm (k, m) =
7167 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7168 if Wsi.withalt m then Buffer.add_string bb "alt-";
7169 if Wsi.withshift m then Buffer.add_string bb "shift-";
7170 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7171 Buffer.add_string bb (Wsi.keyname k);
7173 let addkms l =
7174 let rec loop = function
7175 | [] -> ()
7176 | km :: [] -> addkm km
7177 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7179 loop l
7181 Buffer.add_string bb "<map in='";
7182 addkm i;
7183 match o with
7184 | KMinsrt km ->
7185 Buffer.add_string bb "' out='";
7186 addkm km;
7187 Buffer.add_string bb "'/>\n"
7189 | KMinsrl kms ->
7190 Buffer.add_string bb "' out='";
7191 addkms kms;
7192 Buffer.add_string bb "'/>\n"
7194 | KMmulti (ins, kms) ->
7195 Buffer.add_char bb ' ';
7196 addkms ins;
7197 Buffer.add_string bb "' out='";
7198 addkms kms;
7199 Buffer.add_string bb "'/>\n"
7200 ) h;
7201 Buffer.add_string bb "</keymap>";
7204 loop rest
7206 loop c.keyhashes;
7210 let save () =
7211 let uifontsize = fstate.fontsize in
7212 let bb = Buffer.create 32768 in
7213 let relx = float state.x /. float state.winw in
7214 let w, h, x =
7215 let cx w = truncate (relx *. float w) in
7216 List.fold_left
7217 (fun (w, h, x) ws ->
7218 match ws with
7219 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7220 | Wsi.MaxVert -> (w, conf.cwinh, x)
7221 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7223 (state.winw, state.winh, state.x) state.winstate
7225 conf.cwinw <- w;
7226 conf.cwinh <- h;
7227 let f (h, dc) =
7228 let dc = if conf.bedefault then conf else dc in
7229 Buffer.add_string bb "<llppconfig>\n";
7231 if String.length !fontpath > 0
7232 then
7233 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7234 uifontsize
7235 !fontpath
7236 else (
7237 if uifontsize <> 14
7238 then
7239 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7242 Buffer.add_string bb "<defaults ";
7243 add_attrs bb true dc dc;
7244 let kb = keymapsbuf true dc dc in
7245 if Buffer.length kb > 0
7246 then (
7247 Buffer.add_string bb ">\n";
7248 Buffer.add_buffer bb kb;
7249 Buffer.add_string bb "\n</defaults>\n";
7251 else Buffer.add_string bb "/>\n";
7253 let adddoc path pan anchor c bookmarks =
7254 if bookmarks == [] && c = dc && anchor = emptyanchor
7255 then ()
7256 else (
7257 Printf.bprintf bb "<doc path='%s'"
7258 (enent path 0 (String.length path));
7260 if anchor <> emptyanchor
7261 then (
7262 let n, rely, visy = anchor in
7263 Printf.bprintf bb " page='%d'" n;
7264 if rely > 1e-6
7265 then
7266 Printf.bprintf bb " rely='%f'" rely
7268 if abs_float visy > 1e-6
7269 then
7270 Printf.bprintf bb " visy='%f'" visy
7274 if pan != 0
7275 then Printf.bprintf bb " pan='%d'" pan;
7277 add_attrs bb false dc c;
7278 let kb = keymapsbuf false dc c in
7280 begin match bookmarks with
7281 | [] ->
7282 if Buffer.length kb > 0
7283 then (
7284 Buffer.add_string bb ">\n";
7285 Buffer.add_buffer bb kb;
7286 Buffer.add_string bb "\n</doc>\n";
7288 else Buffer.add_string bb "/>\n"
7289 | _ ->
7290 Buffer.add_string bb ">\n<bookmarks>\n";
7291 List.iter (fun (title, _level, (page, rely, visy)) ->
7292 Printf.bprintf bb
7293 "<item title='%s' page='%d'"
7294 (enent title 0 (String.length title))
7295 page
7297 if rely > 1e-6
7298 then
7299 Printf.bprintf bb " rely='%f'" rely
7301 if abs_float visy > 1e-6
7302 then
7303 Printf.bprintf bb " visy='%f'" visy
7305 Buffer.add_string bb "/>\n";
7306 ) bookmarks;
7307 Buffer.add_string bb "</bookmarks>";
7308 if Buffer.length kb > 0
7309 then (
7310 Buffer.add_string bb "\n";
7311 Buffer.add_buffer bb kb;
7313 Buffer.add_string bb "\n</doc>\n";
7314 end;
7318 let pan, conf =
7319 match state.mode with
7320 | Birdseye (c, pan, _, _, _) ->
7321 let beyecolumns =
7322 match conf.columns with
7323 | Cmulti ((c, _, _), _) -> Some c
7324 | Csingle _ -> None
7325 | Csplit _ -> None
7326 and columns =
7327 match c.columns with
7328 | Cmulti (c, _) -> Cmulti (c, [||])
7329 | Csingle _ -> Csingle [||]
7330 | Csplit _ -> failwith "quit from bird's eye while split"
7332 pan, { c with beyecolumns = beyecolumns; columns = columns }
7333 | _ -> x, conf
7335 let basename = Filename.basename
7336 (if String.length state.origin = 0 then state.path else state.origin)
7338 adddoc basename pan (getanchor ())
7339 (let conf =
7340 let autoscrollstep =
7341 match state.autoscroll with
7342 | Some step -> step
7343 | None -> conf.autoscrollstep
7345 match state.mode with
7346 | Birdseye (bc, _, _, _, _) ->
7347 { conf with
7348 zoom = bc.zoom;
7349 presentation = bc.presentation;
7350 interpagespace = bc.interpagespace;
7351 maxwait = bc.maxwait;
7352 autoscrollstep = autoscrollstep }
7353 | _ -> { conf with autoscrollstep = autoscrollstep }
7354 in conf)
7355 (if conf.savebmarks then state.bookmarks else []);
7357 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7358 if basename <> path
7359 then adddoc path x anchor c bookmarks
7360 ) h;
7361 Buffer.add_string bb "</llppconfig>\n";
7362 true;
7364 if load1 f && Buffer.length bb > 0
7365 then
7367 let tmp = !confpath ^ ".tmp" in
7368 let oc = open_out_bin tmp in
7369 Buffer.output_buffer oc bb;
7370 close_out oc;
7371 Unix.rename tmp !confpath;
7372 with exn ->
7373 prerr_endline
7374 ("error while saving configuration: " ^ exntos exn)
7376 end;;
7378 let adderrmsg src msg =
7379 Buffer.add_string state.errmsgs msg;
7380 state.newerrmsgs <- true;
7381 G.postRedisplay src
7384 let adderrfmt src fmt =
7385 Format.kprintf (fun s -> adderrmsg src s) fmt;
7388 let ract cmds =
7389 let cl = splitatspace cmds in
7390 let scan s fmt f =
7391 try Scanf.sscanf s fmt f
7392 with exn ->
7393 adderrfmt "remote exec"
7394 "error processing '%S': %s\n" cmds (exntos exn)
7396 match cl with
7397 | "reload" :: [] -> reload ()
7398 | "goto" :: args :: [] ->
7399 scan args "%u %f %f"
7400 (fun pageno x y ->
7401 let cmd, _ = state.geomcmds in
7402 if String.length cmd = 0
7403 then gotopagexy pageno x y
7404 else
7405 let f prevf () =
7406 gotopagexy pageno x y;
7407 prevf ()
7409 state.reprf <- f state.reprf
7411 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7412 | "rect" :: args :: [] ->
7413 scan args "%u %u %f %f %f %f"
7414 (fun pageno color x0 y0 x1 y1 ->
7415 onpagerect pageno (fun w h ->
7416 let _,w1,h1,_ = getpagedim pageno in
7417 let sw = float w1 /. w
7418 and sh = float h1 /. h in
7419 let x0s = x0 *. sw
7420 and x1s = x1 *. sw
7421 and y0s = y0 *. sh
7422 and y1s = y1 *. sh in
7423 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7424 debugrect rect;
7425 state.rects <- (pageno, color, rect) :: state.rects;
7426 G.postRedisplay "rect";
7429 | "activatewin" :: [] -> Wsi.activatewin ()
7430 | "quit" :: [] -> raise Quit
7431 | _ ->
7432 adderrfmt "remote command"
7433 "error processing remote command: %S\n" cmds;
7436 let remote =
7437 let scratch = String.create 80 in
7438 let buf = Buffer.create 80 in
7439 fun fd ->
7440 let rec tempfr () =
7441 try Some (Unix.read fd scratch 0 80)
7442 with
7443 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7444 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7445 | exn -> raise exn
7447 match tempfr () with
7448 | None -> Some fd
7449 | Some n ->
7450 if n = 0
7451 then (
7452 Unix.close fd;
7453 if Buffer.length buf > 0
7454 then (
7455 let s = Buffer.contents buf in
7456 Buffer.clear buf;
7457 ract s;
7459 None
7461 else
7462 let rec eat ppos =
7463 let nlpos =
7465 let pos = String.index_from scratch ppos '\n' in
7466 if pos >= n then -1 else pos
7467 with Not_found -> -1
7469 if nlpos >= 0
7470 then (
7471 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7472 let s = Buffer.contents buf in
7473 Buffer.clear buf;
7474 ract s;
7475 eat (nlpos+1);
7477 else (
7478 Buffer.add_substring buf scratch ppos (n-ppos);
7479 Some fd
7481 in eat 0
7484 let remoteopen path =
7485 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7486 with exn ->
7487 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7488 None
7491 let () =
7492 let trimcachepath = ref "" in
7493 let rcmdpath = ref "" in
7494 selfexec := Sys.executable_name;
7495 Arg.parse
7496 (Arg.align
7497 [("-p", Arg.String (fun s -> state.password <- s),
7498 "<password> Set password");
7500 ("-f", Arg.String
7501 (fun s ->
7502 Config.fontpath := s;
7503 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7505 "<path> Set path to the user interface font");
7507 ("-c", Arg.String
7508 (fun s ->
7509 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7510 Config.confpath := s),
7511 "<path> Set path to the configuration file");
7513 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7514 "<path> Set path to the trim cache file");
7516 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7517 "<named-destination> Set named destination");
7519 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7521 ("-remote", Arg.String (fun s -> rcmdpath := s),
7522 "<path> Set path to the remote commands source");
7524 ("-origin", Arg.String (fun s -> state.origin <- s),
7525 "<original-path> Set original path");
7527 ("-v", Arg.Unit (fun () ->
7528 Printf.printf
7529 "%s\nconfiguration path: %s\n"
7530 (version ())
7531 Config.defconfpath
7533 exit 0), " Print version and exit");
7536 (fun s -> state.path <- s)
7537 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7539 if !wtmode
7540 then selfexec := !selfexec ^ " -wtmode";
7542 if String.length state.path = 0
7543 then (prerr_endline "file name missing"; exit 1);
7545 if not (Config.load ())
7546 then prerr_endline "failed to load configuration";
7548 let wsfd, winw, winh = Wsi.init (object
7549 val mutable m_hack = false
7550 method expose = if not m_hack then G.postRedisplay "expose"
7551 method visible = G.postRedisplay "visible"
7552 method display = m_hack <- false; display ()
7553 method reshape w h =
7554 m_hack <- w < state.winw && h < state.winh;
7555 reshape w h
7556 method mouse b d x y m = mouse b d x y m
7557 method motion x y = state.mpos <- (x, y); motion x y
7558 method pmotion x y = state.mpos <- (x, y); pmotion x y
7559 method key k m =
7560 let mascm = m land (
7561 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7562 ) in
7563 match state.keystate with
7564 | KSnone ->
7565 let km = k, mascm in
7566 begin
7567 match
7568 let modehash = state.uioh#modehash in
7569 try Hashtbl.find modehash km
7570 with Not_found ->
7571 try Hashtbl.find (findkeyhash conf "global") km
7572 with Not_found -> KMinsrt (k, m)
7573 with
7574 | KMinsrt (k, m) -> keyboard k m
7575 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7576 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7578 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7579 List.iter (fun (k, m) -> keyboard k m) insrt;
7580 state.keystate <- KSnone
7581 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7582 state.keystate <- KSinto (keys, insrt)
7583 | _ ->
7584 state.keystate <- KSnone
7586 method enter x y = state.mpos <- (x, y); pmotion x y
7587 method leave = state.mpos <- (-1, -1)
7588 method winstate wsl = state.winstate <- wsl
7589 method quit = raise Quit
7590 end) conf.cwinw conf.cwinh (platform = Posx) in
7592 state.wsfd <- wsfd;
7594 if not (
7595 List.exists GlMisc.check_extension
7596 [ "GL_ARB_texture_rectangle"
7597 ; "GL_EXT_texture_recangle"
7598 ; "GL_NV_texture_rectangle" ]
7600 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7602 if (
7603 let r = GlMisc.get_string `renderer in
7604 let p = "Mesa DRI Intel(" in
7605 let l = String.length p in
7606 String.length r > l && String.sub r 0 l = p
7608 then defconf.sliceheight <- 1024;
7610 let cr, sw =
7611 match Ne.pipe () with
7612 | Ne.Exn exn ->
7613 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7614 exit 1
7615 | Ne.Res rw -> rw
7616 and sr, cw =
7617 match Ne.pipe () with
7618 | Ne.Exn exn ->
7619 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7620 exit 1
7621 | Ne.Res rw -> rw
7624 cloexec cr;
7625 cloexec sw;
7626 cloexec sr;
7627 cloexec cw;
7629 setcheckers conf.checkers;
7630 redirectstderr ();
7632 init (cr, cw) (
7633 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7634 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7635 !Config.fontpath, !trimcachepath,
7636 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7638 state.sr <- sr;
7639 state.sw <- sw;
7640 state.text <- "Opening " ^ (mbtoutf8 state.path);
7641 reshape winw winh;
7642 opendoc state.path state.password;
7643 state.uioh <- uioh;
7644 display ();
7645 Wsi.mapwin ();
7646 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7647 let optrfd =
7648 ref (
7649 if String.length !rcmdpath > 0
7650 then remoteopen !rcmdpath
7651 else None
7655 let rec loop deadline =
7656 let r =
7657 match state.errfd with
7658 | None -> [state.sr; state.wsfd]
7659 | Some fd -> [state.sr; state.wsfd; fd]
7661 let r =
7662 match !optrfd with
7663 | None -> r
7664 | Some fd -> fd :: r
7666 if state.redisplay
7667 then (
7668 state.redisplay <- false;
7669 display ();
7671 let timeout =
7672 let now = now () in
7673 if deadline > now
7674 then (
7675 if deadline = infinity
7676 then ~-.1.0
7677 else max 0.0 (deadline -. now)
7679 else 0.0
7681 let r, _, _ =
7682 try Unix.select r [] [] timeout
7683 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7685 begin match r with
7686 | [] ->
7687 state.ghyll None;
7688 let newdeadline =
7689 if state.ghyll == noghyll
7690 then
7691 match state.autoscroll with
7692 | Some step when step != 0 ->
7693 let y = state.y + step in
7694 let y =
7695 if y < 0
7696 then state.maxy
7697 else if y >= state.maxy then 0 else y
7699 gotoy y;
7700 if state.mode = View
7701 then state.text <- "";
7702 deadline +. 0.01
7703 | _ -> infinity
7704 else deadline +. 0.01
7706 loop newdeadline
7708 | l ->
7709 let rec checkfds = function
7710 | [] -> ()
7711 | fd :: rest when fd = state.sr ->
7712 let cmd = readcmd state.sr in
7713 act cmd;
7714 checkfds rest
7716 | fd :: rest when fd = state.wsfd ->
7717 Wsi.readresp fd;
7718 checkfds rest
7720 | fd :: rest when Some fd = !optrfd ->
7721 begin match remote fd with
7722 | None -> optrfd := remoteopen !rcmdpath;
7723 | opt -> optrfd := opt
7724 end;
7725 checkfds rest
7727 | fd :: rest ->
7728 let s = String.create 80 in
7729 let n = tempfailureretry (Unix.read fd s 0) 80 in
7730 if conf.redirectstderr
7731 then (
7732 Buffer.add_substring state.errmsgs s 0 n;
7733 state.newerrmsgs <- true;
7734 state.redisplay <- true;
7736 else (
7737 prerr_string (String.sub s 0 n);
7738 flush stderr;
7740 checkfds rest
7742 checkfds l;
7743 let newdeadline =
7744 let deadline1 =
7745 if deadline = infinity
7746 then now () +. 0.01
7747 else deadline
7749 match state.autoscroll with
7750 | Some step when step != 0 -> deadline1
7751 | _ -> if state.ghyll == noghyll then infinity else deadline1
7753 loop newdeadline
7754 end;
7757 loop infinity;
7758 with Quit ->
7759 Config.save ();