Hard to document so i wont
[llpp.git] / main.ml
blobb6cf543c624462bf621498cc6e8e6547037191bc
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;;
598 let cxack = ref false;;
600 let findkeyhash c name =
601 try List.assoc name c.keyhashes
602 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
605 let conf = { defconf with angle = defconf.angle };;
607 let pgscale h = truncate (float h *. conf.pgscale);;
609 type fontstate =
610 { mutable fontsize : int
611 ; mutable wwidth : float
612 ; mutable maxrows : int
616 let fstate =
617 { fontsize = 14
618 ; wwidth = nan
619 ; maxrows = -1
623 let geturl s =
624 let colonpos = try String.index s ':' with Not_found -> -1 in
625 let len = String.length s in
626 if colonpos >= 0 && colonpos + 3 < len
627 then (
628 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
629 then
630 let schemestartpos =
631 try String.rindex_from s colonpos ' '
632 with Not_found -> -1
634 let scheme =
635 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
637 match scheme with
638 | "http" | "ftp" | "mailto" ->
639 let epos =
640 try String.index_from s colonpos ' '
641 with Not_found -> len
643 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
644 | _ -> ""
645 else ""
647 else ""
650 let gotouri uri =
651 if String.length conf.urilauncher = 0
652 then print_endline uri
653 else (
654 let url = geturl uri in
655 if String.length url = 0
656 then Printf.eprintf "obtained empty url from uri %S" uri
657 else
658 let re = Str.regexp "%s" in
659 let command = Str.global_replace re url conf.urilauncher in
660 try popen command []
661 with exn ->
662 Printf.eprintf
663 "failed to execute `%s': %s\n" command (exntos exn);
664 flush stderr;
668 let version () =
669 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
670 (platform_to_string platform) Sys.word_size Sys.ocaml_version
673 let makehelp () =
674 let strings = version () :: "" :: Help.keys in
675 Array.of_list (
676 List.map (fun s ->
677 let url = geturl s in
678 if String.length url > 0
679 then (s, 0, Action (fun u -> gotouri url; u))
680 else (s, 0, Noaction)
681 ) strings);
684 let noghyll _ = ();;
685 let firstgeomcmds = "", [];;
686 let noreprf () = ();;
688 let state =
689 { sr = Unix.stdin
690 ; sw = Unix.stdin
691 ; wsfd = Unix.stdin
692 ; errfd = None
693 ; stderr = Unix.stderr
694 ; errmsgs = Buffer.create 0
695 ; newerrmsgs = false
696 ; x = 0
697 ; y = 0
698 ; w = 0
699 ; anchor = emptyanchor
700 ; ranchors = []
701 ; layout = []
702 ; maxy = max_int
703 ; tilelru = Queue.create ()
704 ; pagemap = Hashtbl.create 10
705 ; tilemap = Hashtbl.create 10
706 ; pdims = []
707 ; pagecount = 0
708 ; currently = Idle
709 ; mstate = Mnone
710 ; rects = []
711 ; rects1 = []
712 ; text = ""
713 ; mode = View
714 ; winstate = []
715 ; searchpattern = ""
716 ; outlines = [||]
717 ; bookmarks = []
718 ; path = ""
719 ; password = ""
720 ; nameddest = ""
721 ; geomcmds = firstgeomcmds
722 ; hists =
723 { nav = cbnew 10 emptyanchor
724 ; pat = cbnew 10 ""
725 ; pag = cbnew 10 ""
726 ; sel = cbnew 10 ""
728 ; memused = 0
729 ; gen = 0
730 ; throttle = None
731 ; autoscroll = None
732 ; ghyll = noghyll
733 ; help = makehelp ()
734 ; docinfo = []
735 ; texid = None
736 ; prevzoom = 1.0
737 ; progress = -1.0
738 ; uioh = nouioh
739 ; redisplay = true
740 ; mpos = (-1, -1)
741 ; keystate = KSnone
742 ; glinks = false
743 ; prevcolumns = None
744 ; winw = -1
745 ; winh = -1
746 ; reprf = noreprf
747 ; origin = ""
748 ; roam = (fun () -> ())
752 let hscrollh () =
753 if (conf.scrollb land scrollbhv = 0)
754 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
755 then 0
756 else conf.scrollbw
759 let vscrollw () =
760 if (conf.scrollb land scrollbvv = 0)
761 then 0
762 else conf.scrollbw
765 let wadjsb w = w - vscrollw ();;
767 let setfontsize n =
768 fstate.fontsize <- n;
769 fstate.wwidth <- measurestr fstate.fontsize "w";
770 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
773 let vlog fmt =
774 if conf.verbose
775 then
776 Printf.kprintf prerr_endline fmt
777 else
778 Printf.kprintf ignore fmt
781 let launchpath () =
782 if String.length conf.pathlauncher = 0
783 then print_endline state.path
784 else (
785 let re = Str.regexp "%s" in
786 let command = Str.global_replace re state.path conf.pathlauncher in
787 try popen command []
788 with exn ->
789 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
790 flush stderr;
794 module Ne = struct
795 type 'a t = | Res of 'a | Exn of exn;;
797 let pipe () =
798 try Res (Unix.pipe ())
799 with exn -> Exn exn
802 let clo fd f =
803 try tempfailureretry Unix.close fd
804 with exn -> f (exntos exn)
807 let dup fd =
808 try Res (tempfailureretry Unix.dup fd)
809 with exn -> Exn exn
812 let dup2 fd1 fd2 =
813 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
814 with exn -> Exn exn
816 end;;
818 let redirectstderr () =
819 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
820 if conf.redirectstderr
821 then
822 match Ne.pipe () with
823 | Ne.Exn exn ->
824 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
826 | Ne.Res (r, w) ->
827 begin match Ne.dup Unix.stderr with
828 | Ne.Exn exn ->
829 dolog "failed to dup stderr: %s" (exntos exn);
830 Ne.clo r (clofail "pipe/r");
831 Ne.clo w (clofail "pipe/w");
833 | Ne.Res dupstderr ->
834 begin match Ne.dup2 w Unix.stderr with
835 | Ne.Exn exn ->
836 dolog "failed to dup2 to stderr: %s" (exntos exn);
837 Ne.clo dupstderr (clofail "stderr duplicate");
838 Ne.clo r (clofail "redir pipe/r");
839 Ne.clo w (clofail "redir pipe/w");
841 | Ne.Res () ->
842 state.stderr <- dupstderr;
843 state.errfd <- Some r;
844 end;
846 else (
847 state.newerrmsgs <- false;
848 begin match state.errfd with
849 | Some fd ->
850 begin match Ne.dup2 state.stderr Unix.stderr with
851 | Ne.Exn exn ->
852 dolog "failed to dup2 original stderr: %s" (exntos exn)
853 | Ne.Res () ->
854 Ne.clo fd (clofail "dup of stderr");
855 state.errfd <- None;
856 end;
857 | None -> ()
858 end;
859 prerr_string (Buffer.contents state.errmsgs);
860 flush stderr;
861 Buffer.clear state.errmsgs;
865 module G =
866 struct
867 let postRedisplay who =
868 if conf.verbose
869 then prerr_endline ("redisplay for " ^ who);
870 state.redisplay <- true;
872 end;;
874 let getopaque pageno =
875 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
876 with Not_found -> None
879 let putopaque pageno opaque =
880 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
883 let pagetranslatepoint l x y =
884 let dy = y - l.pagedispy in
885 let y = dy + l.pagey in
886 let dx = x - l.pagedispx in
887 let x = dx + l.pagex in
888 (x, y);
891 let onppundermouse g x y d =
892 let rec f = function
893 | l :: rest ->
894 begin match getopaque l.pageno with
895 | Some opaque ->
896 let x0 = l.pagedispx in
897 let x1 = x0 + l.pagevw in
898 let y0 = l.pagedispy in
899 let y1 = y0 + l.pagevh in
900 if y >= y0 && y <= y1 && x >= x0 && x <= x1
901 then
902 let px, py = pagetranslatepoint l x y in
903 match g opaque l px py with
904 | Some res -> res
905 | None -> f rest
906 else f rest
907 | _ ->
908 f rest
910 | [] -> d
912 f state.layout
915 let getunder x y =
916 let g opaque _ px py =
917 match whatsunder opaque px py with
918 | Unone -> None
919 | under -> Some under
921 onppundermouse g x y Unone
924 let unproject x y =
925 let g opaque l x y =
926 match unproject opaque x y with
927 | Some (x, y) -> Some (Some (l.pageno, x, y))
928 | None -> None
930 onppundermouse g x y None;
933 let showtext c s =
934 state.text <- Printf.sprintf "%c%s" c s;
935 G.postRedisplay "showtext";
938 let paxunder x y =
939 let g opaque l px py =
940 if markunder opaque px py conf.paxmark
941 then (
942 Some (fun () ->
943 match getopaque l.pageno with
944 | None -> ()
945 | Some opaque ->
946 match Ne.pipe () with
947 | Ne.Exn exn ->
948 showtext '!'
949 (Printf.sprintf
950 "can not create mark pipe: %s"
951 (exntos exn));
952 | Ne.Res (r, w) ->
953 let doclose what fd =
954 Ne.clo fd (fun msg ->
955 dolog "%s close failed: %s" what msg)
958 popen conf.paxcmd [r, 0; w, -1];
959 copysel w opaque false;
960 doclose "pipe/r" r;
961 G.postRedisplay "paxunder";
962 with exn ->
963 dolog "can not execute %S: %s"
964 conf.paxcmd (exntos exn);
965 doclose "pipe/r" r;
966 doclose "pipe/w" w;
969 else None
971 G.postRedisplay "paxunder";
972 state.roam <-
973 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
976 let selstring s =
977 match Ne.pipe () with
978 | Ne.Exn exn ->
979 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
980 | Ne.Res (r, w) ->
981 let popened =
982 try popen conf.selcmd [r, 0; w, -1]; true
983 with exn ->
984 showtext '!'
985 (Printf.sprintf "failed to execute %s: %s"
986 conf.selcmd (exntos exn));
987 false
989 let clo cap fd =
990 Ne.clo fd (fun msg ->
991 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
994 if popened
995 then
996 (try
997 let l = String.length s in
998 let n = tempfailureretry (Unix.write w s 0) l in
999 if n != l
1000 then
1001 showtext '!'
1002 (Printf.sprintf
1003 "failed to write %d characters to sel pipe, wrote %d"
1006 with exn ->
1007 showtext '!'
1008 (Printf.sprintf "failed to write to sel pipe: %s"
1009 (exntos exn)
1012 else dolog "%s" s;
1013 clo "pipe/r" r;
1014 clo "pipe/w" w;
1017 let undertext = function
1018 | Unone -> "none"
1019 | Ulinkuri s -> s
1020 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1021 | Utext s -> "font: " ^ s
1022 | Uunexpected s -> "unexpected: " ^ s
1023 | Ulaunch s -> "launch: " ^ s
1024 | Unamed s -> "named: " ^ s
1025 | Uremote (filename, pageno) ->
1026 Printf.sprintf "%s: page %d" filename (pageno+1)
1029 let updateunder x y =
1030 match getunder x y with
1031 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1032 | Ulinkuri uri ->
1033 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1034 Wsi.setcursor Wsi.CURSOR_INFO
1035 | Ulinkgoto (pageno, _) ->
1036 if conf.underinfo
1037 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1038 Wsi.setcursor Wsi.CURSOR_INFO
1039 | Utext s ->
1040 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1041 Wsi.setcursor Wsi.CURSOR_TEXT
1042 | Uunexpected s ->
1043 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1044 Wsi.setcursor Wsi.CURSOR_INHERIT
1045 | Ulaunch s ->
1046 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1047 Wsi.setcursor Wsi.CURSOR_INHERIT
1048 | Unamed s ->
1049 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1050 Wsi.setcursor Wsi.CURSOR_INHERIT
1051 | Uremote (filename, pageno) ->
1052 if conf.underinfo then showtext 'r'
1053 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1054 Wsi.setcursor Wsi.CURSOR_INFO
1057 let showlinktype under =
1058 if conf.underinfo
1059 then
1060 match under with
1061 | Unone -> ()
1062 | under ->
1063 let s = undertext under in
1064 showtext ' ' s
1067 let addchar s c =
1068 let b = Buffer.create (String.length s + 1) in
1069 Buffer.add_string b s;
1070 Buffer.add_char b c;
1071 Buffer.contents b;
1074 module type TextEnumType =
1076 type t
1077 val name : string
1078 val names : string array
1079 end;;
1081 module TextEnumMake (Ten : TextEnumType) =
1082 struct
1083 let names = Ten.names;;
1084 let to_int (t : Ten.t) = Obj.magic t;;
1085 let to_string t = names.(to_int t);;
1086 let of_int n : Ten.t = Obj.magic n;;
1087 let of_string s =
1088 let rec find i =
1089 if i = Array.length names
1090 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1091 else (
1092 if Ten.names.(i) = s
1093 then of_int i
1094 else find (i+1)
1096 in find 0;;
1097 end;;
1099 module CSTE = TextEnumMake (struct
1100 type t = colorspace;;
1101 let name = "colorspace";;
1102 let names = [|"rgb"; "bgr"; "gray"|];;
1103 end);;
1105 module MTE = TextEnumMake (struct
1106 type t = mark;;
1107 let name = "mark";;
1108 let names = [|"page"; "block"; "line"; "word"|];;
1109 end);;
1111 module FMTE = TextEnumMake (struct
1112 type t= fitmodel;;
1113 let name = "fitmodel";;
1114 let names = [|"width"; "proportional"; "page"|];;
1115 end);;
1117 let intentry_with_suffix text key =
1118 let c =
1119 if key >= 32 && key < 127
1120 then Char.chr key
1121 else '\000'
1123 match Char.lowercase c with
1124 | '0' .. '9' ->
1125 let text = addchar text c in
1126 TEcont text
1128 | 'k' | 'm' | 'g' ->
1129 let text = addchar text c in
1130 TEcont text
1132 | _ ->
1133 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1134 TEcont text
1137 let multicolumns_to_string (n, a, b) =
1138 if a = 0 && b = 0
1139 then Printf.sprintf "%d" n
1140 else Printf.sprintf "%d,%d,%d" n a b;
1143 let multicolumns_of_string s =
1145 (int_of_string s, 0, 0)
1146 with _ ->
1147 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1148 if a > 1 || b > 1
1149 then failwith "subtly broken"; (n, a, b)
1153 let readcmd fd =
1154 let s = "xxxx" in
1155 let n = tempfailureretry (Unix.read fd s 0) 4 in
1156 if n != 4 then failwith "incomplete read(len)";
1157 let len = 0
1158 lor (Char.code s.[0] lsl 24)
1159 lor (Char.code s.[1] lsl 16)
1160 lor (Char.code s.[2] lsl 8)
1161 lor (Char.code s.[3] lsl 0)
1163 let s = String.create len in
1164 let n = tempfailureretry (Unix.read fd s 0) len in
1165 if n != len then failwith "incomplete read(data)";
1169 let btod b = if b then 1 else 0;;
1171 let wcmd fmt =
1172 let b = Buffer.create 16 in
1173 Buffer.add_string b "llll";
1174 Printf.kbprintf
1175 (fun b ->
1176 let s = Buffer.contents b in
1177 let n = String.length s in
1178 let len = n - 4 in
1179 (* dolog "wcmd %S" (String.sub s 4 len); *)
1180 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1181 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1182 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1183 s.[3] <- Char.chr (len land 0xff);
1184 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1185 if n' != n then failwith "write failed";
1186 ) b fmt;
1189 let calcips h =
1190 let d = state.winh - h in
1191 max conf.interpagespace ((d + 1) / 2)
1194 let rowyh (c, coverA, coverB) b n =
1195 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1196 then
1197 let _, _, vy, (_, _, h, _) = b.(n) in
1198 (vy, h)
1199 else
1200 let n' = n - coverA in
1201 let d = n' mod c in
1202 let s = n - d in
1203 let e = min state.pagecount (s + c) in
1204 let rec find m miny maxh = if m = e then miny, maxh else
1205 let _, _, y, (_, _, h, _) = b.(m) in
1206 let miny = min miny y in
1207 let maxh = max maxh h in
1208 find (m+1) miny maxh
1209 in find s max_int 0
1212 let calcheight () =
1213 match conf.columns with
1214 | Cmulti ((_, _, _) as cl, b) ->
1215 if Array.length b > 0
1216 then
1217 let y, h = rowyh cl b (Array.length b - 1) in
1218 y + h + (if conf.presentation then calcips h else 0)
1219 else 0
1220 | Csingle b ->
1221 if Array.length b > 0
1222 then
1223 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1224 y + h + (if conf.presentation then calcips h else 0)
1225 else 0
1226 | Csplit (_, b) ->
1227 if Array.length b > 0
1228 then
1229 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1230 y + h
1231 else 0
1234 let getpageyh pageno =
1235 let pageno = bound pageno 0 (state.pagecount-1) in
1236 match conf.columns with
1237 | Csingle b ->
1238 if Array.length b = 0
1239 then 0, 0
1240 else
1241 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1242 let y =
1243 if conf.presentation
1244 then y - calcips h
1245 else y
1247 y, h
1248 | Cmulti (cl, b) ->
1249 if Array.length b = 0
1250 then 0, 0
1251 else
1252 let y, h = rowyh cl b pageno in
1253 let y =
1254 if conf.presentation
1255 then y - calcips h
1256 else y
1258 y, h
1259 | Csplit (c, b) ->
1260 if Array.length b = 0
1261 then 0, 0
1262 else
1263 let n = pageno*c in
1264 let (_, _, y, (_, _, h, _)) = b.(n) in
1265 y, h
1268 let getpagedim pageno =
1269 let rec f ppdim l =
1270 match l with
1271 | (n, _, _, _) as pdim :: rest ->
1272 if n >= pageno
1273 then (if n = pageno then pdim else ppdim)
1274 else f pdim rest
1276 | [] -> ppdim
1278 f (-1, -1, -1, -1) state.pdims
1281 let getpagey pageno = fst (getpageyh pageno);;
1283 let nogeomcmds cmds =
1284 match cmds with
1285 | s, [] -> String.length s = 0
1286 | _ -> false
1289 let page_of_y y =
1290 let ((c, coverA, coverB) as cl), b =
1291 match conf.columns with
1292 | Csingle b -> (1, 0, 0), b
1293 | Cmulti (c, b) -> c, b
1294 | Csplit (_, b) -> (1, 0, 0), b
1296 if Array.length b = 0
1297 then -1
1298 else
1299 let rec bsearch nmin nmax =
1300 if nmin > nmax
1301 then bound nmin 0 (state.pagecount-1)
1302 else
1303 let n = (nmax + nmin) / 2 in
1304 let vy, h = rowyh cl b n in
1305 let y0, y1 =
1306 if conf.presentation
1307 then
1308 let ips = calcips h in
1309 let y0 = vy - ips in
1310 let y1 = vy + h + ips in
1311 y0, y1
1312 else (
1313 if n = 0
1314 then 0, vy + h + conf.interpagespace
1315 else
1316 let y0 = vy - conf.interpagespace in
1317 y0, y0 + h + conf.interpagespace
1320 if y >= y0 && y < y1
1321 then (
1322 if c = 1
1323 then n
1324 else (
1325 if n > coverA
1326 then
1327 if n < state.pagecount - coverB
1328 then ((n-coverA)/c)*c + coverA
1329 else n
1330 else n
1333 else (
1334 if y > y0
1335 then bsearch (n+1) nmax
1336 else bsearch nmin (n-1)
1339 let r = bsearch 0 (state.pagecount-1) in
1343 let layoutN ((columns, coverA, coverB), b) y sh =
1344 let sh = sh - (hscrollh ()) in
1345 let rec fold accu n =
1346 if n = Array.length b
1347 then accu
1348 else
1349 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1350 if (vy - y) > sh &&
1351 (n = coverA - 1
1352 || n = state.pagecount - coverB
1353 || (n - coverA) mod columns = columns - 1)
1354 then accu
1355 else
1356 let accu =
1357 if vy + h > y
1358 then
1359 let pagey = max 0 (y - vy) in
1360 let pagedispy = if pagey > 0 then 0 else vy - y in
1361 let pagedispx, pagex =
1362 let pdx =
1363 if n = coverA - 1 || n = state.pagecount - coverB
1364 then state.x + (wadjsb state.winw - w) / 2
1365 else dx + xoff + state.x
1367 if pdx < 0
1368 then 0, -pdx
1369 else pdx, 0
1371 let pagevw =
1372 let vw = wadjsb state.winw - pagedispx in
1373 let pw = w - pagex in
1374 min vw pw
1376 let pagevh = min (h - pagey) (sh - pagedispy) in
1377 if pagevw > 0 && pagevh > 0
1378 then
1379 let e =
1380 { pageno = n
1381 ; pagedimno = pdimno
1382 ; pagew = w
1383 ; pageh = h
1384 ; pagex = pagex
1385 ; pagey = pagey
1386 ; pagevw = pagevw
1387 ; pagevh = pagevh
1388 ; pagedispx = pagedispx
1389 ; pagedispy = pagedispy
1390 ; pagecol = 0
1393 e :: accu
1394 else
1395 accu
1396 else
1397 accu
1399 fold accu (n+1)
1401 List.rev (fold [] (page_of_y y));
1404 let layoutS (columns, b) y sh =
1405 let sh = sh - hscrollh () in
1406 let rec fold accu n =
1407 if n = Array.length b
1408 then accu
1409 else
1410 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1411 if (vy - y) > sh
1412 then accu
1413 else
1414 let accu =
1415 if vy + pageh > y
1416 then
1417 let x = xoff + state.x in
1418 let pagey = max 0 (y - vy) in
1419 let pagedispy = if pagey > 0 then 0 else vy - y in
1420 let pagedispx, pagex =
1421 if px = 0
1422 then (
1423 if x < 0
1424 then 0, -x
1425 else x, 0
1427 else (
1428 let px = px - x in
1429 if px < 0
1430 then -px, 0
1431 else 0, px
1434 let pagecolw = pagew/columns in
1435 let pagedispx =
1436 if pagecolw < state.winw
1437 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1438 else pagedispx
1440 let pagevw =
1441 let vw = wadjsb state.winw - pagedispx in
1442 let pw = pagew - pagex in
1443 min vw pw
1445 let pagevw = min pagevw pagecolw in
1446 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1447 if pagevw > 0 && pagevh > 0
1448 then
1449 let e =
1450 { pageno = n/columns
1451 ; pagedimno = pdimno
1452 ; pagew = pagew
1453 ; pageh = pageh
1454 ; pagex = pagex
1455 ; pagey = pagey
1456 ; pagevw = pagevw
1457 ; pagevh = pagevh
1458 ; pagedispx = pagedispx
1459 ; pagedispy = pagedispy
1460 ; pagecol = n mod columns
1463 e :: accu
1464 else
1465 accu
1466 else
1467 accu
1469 fold accu (n+1)
1471 List.rev (fold [] 0)
1474 let layout y sh =
1475 if nogeomcmds state.geomcmds
1476 then
1477 match conf.columns with
1478 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1479 | Cmulti c -> layoutN c y sh
1480 | Csplit s -> layoutS s y sh
1481 else []
1484 let clamp incr =
1485 let y = state.y + incr in
1486 let y = max 0 y in
1487 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1491 let itertiles l f =
1492 let tilex = l.pagex mod conf.tilew in
1493 let tiley = l.pagey mod conf.tileh in
1495 let col = l.pagex / conf.tilew in
1496 let row = l.pagey / conf.tileh in
1498 let rec rowloop row y0 dispy h =
1499 if h = 0
1500 then ()
1501 else (
1502 let dh = conf.tileh - y0 in
1503 let dh = min h dh in
1504 let rec colloop col x0 dispx w =
1505 if w = 0
1506 then ()
1507 else (
1508 let dw = conf.tilew - x0 in
1509 let dw = min w dw in
1511 f col row dispx dispy x0 y0 dw dh;
1512 colloop (col+1) 0 (dispx+dw) (w-dw)
1515 colloop col tilex l.pagedispx l.pagevw;
1516 rowloop (row+1) 0 (dispy+dh) (h-dh)
1519 if l.pagevw > 0 && l.pagevh > 0
1520 then rowloop row tiley l.pagedispy l.pagevh;
1523 let gettileopaque l col row =
1524 let key =
1525 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1527 try Some (Hashtbl.find state.tilemap key)
1528 with Not_found -> None
1531 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1532 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1533 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1536 let drawtiles l color =
1537 GlDraw.color color;
1538 let f col row x y tilex tiley w h =
1539 match gettileopaque l col row with
1540 | Some (opaque, _, t) ->
1541 let params = x, y, w, h, tilex, tiley in
1542 if conf.invert
1543 then (
1544 Gl.enable `blend;
1545 GlFunc.blend_func `zero `one_minus_src_color;
1547 drawtile params opaque;
1548 if conf.invert
1549 then Gl.disable `blend;
1550 if conf.debug
1551 then (
1552 let s = Printf.sprintf
1553 "%d[%d,%d] %f sec"
1554 l.pageno col row t
1556 let w = measurestr fstate.fontsize s in
1557 GlMisc.push_attrib [`current];
1558 GlDraw.color (0.0, 0.0, 0.0);
1559 GlDraw.rect
1560 (float (x-2), float (y-2))
1561 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1562 GlDraw.color (1.0, 1.0, 1.0);
1563 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1564 GlMisc.pop_attrib ();
1567 | _ ->
1568 let w =
1569 let lw = wadjsb state.winw - x in
1570 min lw w
1571 and h =
1572 let lh = state.winh - y in
1573 min lh h
1575 begin match state.texid with
1576 | Some id ->
1577 Gl.enable `texture_2d;
1578 GlTex.bind_texture `texture_2d id;
1579 let x0 = float x
1580 and y0 = float y
1581 and x1 = float (x+w)
1582 and y1 = float (y+h) in
1584 let tw = float w /. 16.0
1585 and th = float h /. 16.0 in
1586 let tx0 = float tilex /. 16.0
1587 and ty0 = float tiley /. 16.0 in
1588 let tx1 = tx0 +. tw
1589 and ty1 = ty0 +. th in
1590 GlDraw.begins `quads;
1591 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1592 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1593 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1594 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1595 GlDraw.ends ();
1597 Gl.disable `texture_2d;
1598 | None ->
1599 GlDraw.color (1.0, 1.0, 1.0);
1600 GlDraw.rect
1601 (float x, float y)
1602 (float (x+w), float (y+h));
1603 end;
1604 if w > 128 && h > fstate.fontsize + 10
1605 then (
1606 GlDraw.color (0.0, 0.0, 0.0);
1607 let c, r =
1608 if conf.verbose
1609 then (col*conf.tilew, row*conf.tileh)
1610 else col, row
1612 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1614 GlDraw.color color;
1616 itertiles l f
1619 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1621 let tilevisible1 l x y =
1622 let ax0 = l.pagex
1623 and ax1 = l.pagex + l.pagevw
1624 and ay0 = l.pagey
1625 and ay1 = l.pagey + l.pagevh in
1627 let bx0 = x
1628 and by0 = y in
1629 let bx1 = min (bx0 + conf.tilew) l.pagew
1630 and by1 = min (by0 + conf.tileh) l.pageh in
1632 let rx0 = max ax0 bx0
1633 and ry0 = max ay0 by0
1634 and rx1 = min ax1 bx1
1635 and ry1 = min ay1 by1 in
1637 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1638 nonemptyintersection
1641 let tilevisible layout n x y =
1642 let rec findpageinlayout m = function
1643 | l :: rest when l.pageno = n ->
1644 tilevisible1 l x y || (
1645 match conf.columns with
1646 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1647 | _ -> false
1649 | _ :: rest -> findpageinlayout 0 rest
1650 | [] -> false
1652 findpageinlayout 0 layout;
1655 let tileready l x y =
1656 tilevisible1 l x y &&
1657 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1660 let tilepage n p layout =
1661 let rec loop = function
1662 | l :: rest ->
1663 if l.pageno = n
1664 then
1665 let f col row _ _ _ _ _ _ =
1666 if state.currently = Idle
1667 then
1668 match gettileopaque l col row with
1669 | Some _ -> ()
1670 | None ->
1671 let x = col*conf.tilew
1672 and y = row*conf.tileh in
1673 let w =
1674 let w = l.pagew - x in
1675 min w conf.tilew
1677 let h =
1678 let h = l.pageh - y in
1679 min h conf.tileh
1681 let pbo =
1682 if conf.usepbo
1683 then getpbo w h conf.colorspace
1684 else "0"
1686 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1687 state.currently <-
1688 Tiling (
1689 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1690 conf.tilew, conf.tileh
1693 itertiles l f;
1694 else
1695 loop rest
1697 | [] -> ()
1699 if nogeomcmds state.geomcmds
1700 then loop layout;
1703 let preloadlayout y =
1704 let y = if y < state.winh then 0 else y - state.winh in
1705 let h = state.winh*3 in
1706 layout y h;
1709 let load pages =
1710 let rec loop pages =
1711 if state.currently != Idle
1712 then ()
1713 else
1714 match pages with
1715 | l :: rest ->
1716 begin match getopaque l.pageno with
1717 | None ->
1718 wcmd "page %d %d" l.pageno l.pagedimno;
1719 state.currently <- Loading (l, state.gen);
1720 | Some opaque ->
1721 tilepage l.pageno opaque pages;
1722 loop rest
1723 end;
1724 | _ -> ()
1726 if nogeomcmds state.geomcmds
1727 then loop pages
1730 let preload pages =
1731 load pages;
1732 if conf.preload && state.currently = Idle
1733 then load (preloadlayout state.y);
1736 let layoutready layout =
1737 let rec fold all ls =
1738 all && match ls with
1739 | l :: rest ->
1740 let seen = ref false in
1741 let allvisible = ref true in
1742 let foo col row _ _ _ _ _ _ =
1743 seen := true;
1744 allvisible := !allvisible &&
1745 begin match gettileopaque l col row with
1746 | Some _ -> true
1747 | None -> false
1750 itertiles l foo;
1751 fold (!seen && !allvisible) rest
1752 | [] -> true
1754 let alltilesvisible = fold true layout in
1755 alltilesvisible;
1758 let gotoy y =
1759 let y = bound y 0 state.maxy in
1760 let y, layout, proceed =
1761 match conf.maxwait with
1762 | Some time when state.ghyll == noghyll ->
1763 begin match state.throttle with
1764 | None ->
1765 let layout = layout y state.winh in
1766 let ready = layoutready layout in
1767 if not ready
1768 then (
1769 load layout;
1770 state.throttle <- Some (layout, y, now ());
1772 else G.postRedisplay "gotoy showall (None)";
1773 y, layout, ready
1774 | Some (_, _, started) ->
1775 let dt = now () -. started in
1776 if dt > time
1777 then (
1778 state.throttle <- None;
1779 let layout = layout y state.winh in
1780 load layout;
1781 G.postRedisplay "maxwait";
1782 y, layout, true
1784 else -1, [], false
1787 | _ ->
1788 let layout = layout y state.winh in
1789 if not !wtmode || layoutready layout
1790 then G.postRedisplay "gotoy ready";
1791 y, layout, true
1793 if proceed
1794 then (
1795 state.y <- y;
1796 state.layout <- layout;
1797 begin match state.mode with
1798 | LinkNav (Ltexact (pageno, linkno)) ->
1799 let rec loop = function
1800 | [] ->
1801 state.mode <- LinkNav (Ltgendir 0)
1802 | l :: _ when l.pageno = pageno ->
1803 begin match getopaque pageno with
1804 | None ->
1805 state.mode <- LinkNav (Ltgendir 0)
1806 | Some opaque ->
1807 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1808 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1809 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1810 then state.mode <- LinkNav (Ltgendir 0)
1812 | _ :: rest -> loop rest
1814 loop layout
1815 | _ -> ()
1816 end;
1817 begin match state.mode with
1818 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1819 if not (pagevisible layout pageno)
1820 then (
1821 match state.layout with
1822 | [] -> ()
1823 | l :: _ ->
1824 state.mode <- Birdseye (
1825 conf, leftx, l.pageno, hooverpageno, anchor
1828 | LinkNav (Ltgendir dir as lt) ->
1829 let linknav =
1830 let rec loop = function
1831 | [] -> lt
1832 | l :: rest ->
1833 match getopaque l.pageno with
1834 | None -> loop rest
1835 | Some opaque ->
1836 let link =
1837 let ld =
1838 if dir = 0
1839 then LDfirstvisible (l.pagex, l.pagey, dir)
1840 else (
1841 if dir > 0 then LDfirst else LDlast
1844 findlink opaque ld
1846 match link with
1847 | Lnotfound -> loop rest
1848 | Lfound n ->
1849 showlinktype (getlink opaque n);
1850 Ltexact (l.pageno, n)
1852 loop state.layout
1854 state.mode <- LinkNav linknav
1855 | _ -> ()
1856 end;
1857 preload layout;
1859 state.ghyll <- noghyll;
1860 if conf.updatecurs
1861 then (
1862 let mx, my = state.mpos in
1863 updateunder mx my;
1867 let conttiling pageno opaque =
1868 tilepage pageno opaque
1869 (if conf.preload then preloadlayout state.y else state.layout)
1872 let gotoy_and_clear_text y =
1873 if not conf.verbose then state.text <- "";
1874 gotoy y;
1877 let getanchor1 l =
1878 let top =
1879 let coloff = l.pagecol * l.pageh in
1880 float (l.pagey + coloff) /. float l.pageh
1882 let dtop =
1883 if l.pagedispy = 0
1884 then
1886 else (
1887 if conf.presentation
1888 then float l.pagedispy /. float (calcips l.pageh)
1889 else float l.pagedispy /. float conf.interpagespace
1892 (l.pageno, top, dtop)
1895 let getanchor () =
1896 match state.layout with
1897 | l :: _ -> getanchor1 l
1898 | [] ->
1899 let n = page_of_y state.y in
1900 if n = -1
1901 then state.anchor
1902 else
1903 let y, h = getpageyh n in
1904 let dy = y - state.y in
1905 let dtop =
1906 if conf.presentation
1907 then
1908 let ips = calcips h in
1909 float (dy + ips) /. float ips
1910 else
1911 float dy /. float conf.interpagespace
1913 (n, 0.0, dtop)
1916 let getanchory (n, top, dtop) =
1917 let y, h = getpageyh n in
1918 if conf.presentation
1919 then
1920 let ips = calcips h in
1921 y + truncate (top*.float h -. dtop*.float ips) + ips;
1922 else
1923 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1926 let gotoanchor anchor =
1927 gotoy (getanchory anchor);
1930 let addnav () =
1931 cbput state.hists.nav (getanchor ());
1934 let getnav dir =
1935 let anchor = cbgetc state.hists.nav dir in
1936 getanchory anchor;
1939 let gotoghyll y =
1940 let scroll f n a b =
1941 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1942 let snake f a b =
1943 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1944 if f < a
1945 then s (float f /. float a)
1946 else (
1947 if f > b
1948 then 1.0 -. s ((float (f-b) /. float (n-b)))
1949 else 1.0
1952 snake f a b
1953 and summa f n a b =
1954 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1955 let iv x = x**3.-.0.5*.x**4. in
1956 let iv1 = iv f in
1957 let ins = float a *. iv1
1958 and outs = float (n-b) *. iv1 in
1959 let ones = b - a in
1960 ins +. outs +. float ones
1962 let rec set (_N, _A, _B) y sy =
1963 let sum = summa 1.0 _N _A _B in
1964 let dy = float (y - sy) in
1965 state.ghyll <- (
1966 let rec gf n y1 o =
1967 if n >= _N
1968 then state.ghyll <- noghyll
1969 else
1970 let go n =
1971 let s = scroll n _N _A _B in
1972 let y1 = y1 +. ((s *. dy) /. sum) in
1973 gotoy_and_clear_text (truncate y1);
1974 state.ghyll <- gf (n+1) y1;
1976 match o with
1977 | None -> go n
1978 | Some y' -> set (_N/2, 1, 1) y' state.y
1980 gf 0 (float state.y)
1983 match conf.ghyllscroll with
1984 | None ->
1985 gotoy_and_clear_text y
1986 | Some nab ->
1987 if state.ghyll == noghyll
1988 then set nab y state.y
1989 else state.ghyll (Some y)
1992 let gotopage n top =
1993 let y, h = getpageyh n in
1994 let y = y + (truncate (top *. float h)) in
1995 gotoghyll y
1998 let gotopage1 n top =
1999 let y = getpagey n in
2000 let y = y + top in
2001 gotoghyll y
2004 let invalidate s f =
2005 state.layout <- [];
2006 state.pdims <- [];
2007 state.rects <- [];
2008 state.rects1 <- [];
2009 match state.geomcmds with
2010 | ps, [] when String.length ps = 0 ->
2011 f ();
2012 state.geomcmds <- s, [];
2014 | ps, [] ->
2015 state.geomcmds <- ps, [s, f];
2017 | ps, (s', _) :: rest when s' = s ->
2018 state.geomcmds <- ps, ((s, f) :: rest);
2020 | ps, cmds ->
2021 state.geomcmds <- ps, ((s, f) :: cmds);
2024 let flushpages () =
2025 Hashtbl.iter (fun _ opaque ->
2026 wcmd "freepage %s" opaque;
2027 ) state.pagemap;
2028 Hashtbl.clear state.pagemap;
2031 let flushtiles () =
2032 if not (Queue.is_empty state.tilelru)
2033 then (
2034 Queue.iter (fun (k, p, s) ->
2035 wcmd "freetile %s" p;
2036 state.memused <- state.memused - s;
2037 Hashtbl.remove state.tilemap k;
2038 ) state.tilelru;
2039 state.uioh#infochanged Memused;
2040 Queue.clear state.tilelru;
2042 load state.layout;
2045 let stateh h =
2046 let h = truncate (float h*.conf.zoom) in
2047 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2048 h - d
2051 let opendoc path password =
2052 state.path <- path;
2053 state.password <- password;
2054 state.gen <- state.gen + 1;
2055 state.docinfo <- [];
2057 flushpages ();
2058 setaalevel conf.aalevel;
2059 let titlepath =
2060 if String.length state.origin = 0
2061 then path
2062 else state.origin
2064 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2065 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2066 invalidate "reqlayout"
2067 (fun () ->
2068 wcmd "reqlayout %d %d %d %s\000"
2069 conf.angle (FMTE.to_int conf.fitmodel)
2070 (stateh state.winh) state.nameddest
2074 let reload () =
2075 state.anchor <- getanchor ();
2076 opendoc state.path state.password;
2079 let scalecolor c =
2080 let c = c *. conf.colorscale in
2081 (c, c, c);
2084 let scalecolor2 (r, g, b) =
2085 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2088 let docolumns = function
2089 | Csingle _ ->
2090 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2091 let rec loop pageno pdimno pdim y ph pdims =
2092 if pageno = state.pagecount
2093 then ()
2094 else
2095 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2096 match pdims with
2097 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2098 pdimno+1, pdim, rest
2099 | _ ->
2100 pdimno, pdim, pdims
2102 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2103 let y = y +
2104 (if conf.presentation
2105 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2106 else (if pageno = 0 then 0 else conf.interpagespace)
2109 a.(pageno) <- (pdimno, x, y, pdim);
2110 loop (pageno+1) pdimno pdim (y + h) h pdims
2112 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2113 conf.columns <- Csingle a;
2115 | Cmulti ((columns, coverA, coverB), _) ->
2116 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2117 let rec loop pageno pdimno pdim x y rowh pdims =
2118 let rec fixrow m = if m = pageno then () else
2119 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2120 if h < rowh
2121 then (
2122 let y = y + (rowh - h) / 2 in
2123 a.(m) <- (pdimno, x, y, pdim);
2125 fixrow (m+1)
2127 if pageno = state.pagecount
2128 then fixrow (((pageno - 1) / columns) * columns)
2129 else
2130 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2131 match pdims with
2132 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2133 pdimno+1, pdim, rest
2134 | _ ->
2135 pdimno, pdim, pdims
2137 let x, y, rowh' =
2138 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2139 then (
2140 let x = (wadjsb state.winw - w) / 2 in
2141 let ips =
2142 if conf.presentation then calcips h else conf.interpagespace in
2143 x, y + ips + rowh, h
2145 else (
2146 if (pageno - coverA) mod columns = 0
2147 then (
2148 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2149 let y =
2150 if conf.presentation
2151 then
2152 let ips = calcips h in
2153 y + (if pageno = 0 then 0 else calcips rowh + ips)
2154 else
2155 y + (if pageno = 0 then 0 else conf.interpagespace)
2157 x, y + rowh, h
2159 else x, y, max rowh h
2162 let y =
2163 if pageno > 1 && (pageno - coverA) mod columns = 0
2164 then (
2165 let y =
2166 if pageno = columns && conf.presentation
2167 then (
2168 let ips = calcips rowh in
2169 for i = 0 to pred columns
2171 let (pdimno, x, y, pdim) = a.(i) in
2172 a.(i) <- (pdimno, x, y+ips, pdim)
2173 done;
2174 y+ips;
2176 else y
2178 fixrow (pageno - columns);
2181 else y
2183 a.(pageno) <- (pdimno, x, y, pdim);
2184 let x = x + w + xoff*2 + conf.interpagespace in
2185 loop (pageno+1) pdimno pdim x y rowh' pdims
2187 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2188 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2190 | Csplit (c, _) ->
2191 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2192 let rec loop pageno pdimno pdim y pdims =
2193 if pageno = state.pagecount
2194 then ()
2195 else
2196 let pdimno, ((_, w, h, _) as pdim), pdims =
2197 match pdims with
2198 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2199 pdimno+1, pdim, rest
2200 | _ ->
2201 pdimno, pdim, pdims
2203 let cw = w / c in
2204 let rec loop1 n x y =
2205 if n = c then y else (
2206 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2207 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2210 let y = loop1 0 0 y in
2211 loop (pageno+1) pdimno pdim y pdims
2213 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2214 conf.columns <- Csplit (c, a);
2217 let represent () =
2218 docolumns conf.columns;
2219 state.maxy <- calcheight ();
2220 if state.reprf == noreprf
2221 then (
2222 match state.mode with
2223 | Birdseye (_, _, pageno, _, _) ->
2224 let y, h = getpageyh pageno in
2225 let top = (state.winh - h) / 2 in
2226 gotoy (max 0 (y - top))
2227 | _ -> gotoanchor state.anchor
2229 else (
2230 state.reprf ();
2231 state.reprf <- noreprf;
2235 let reshape w h =
2236 GlDraw.viewport 0 0 w h;
2237 let firsttime = state.geomcmds == firstgeomcmds in
2238 if not firsttime && nogeomcmds state.geomcmds
2239 then state.anchor <- getanchor ();
2241 state.winw <- w;
2242 let w = wadjsb (truncate (float w *. conf.zoom)) in
2243 let w = max w 2 in
2244 state.winh <- h;
2245 setfontsize fstate.fontsize;
2246 GlMat.mode `modelview;
2247 GlMat.load_identity ();
2249 GlMat.mode `projection;
2250 GlMat.load_identity ();
2251 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2252 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2253 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2255 let relx =
2256 if conf.zoom <= 1.0
2257 then 0.0
2258 else float state.x /. float state.w
2260 invalidate "geometry"
2261 (fun () ->
2262 state.w <- w;
2263 if not firsttime
2264 then state.x <- truncate (relx *. float w);
2265 let w =
2266 match conf.columns with
2267 | Csingle _ -> w
2268 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2269 | Csplit (c, _) -> w * c
2271 wcmd "geometry %d %d %d"
2272 w (stateh h) (FMTE.to_int conf.fitmodel)
2276 let enttext () =
2277 let len = String.length state.text in
2278 let drawstring s =
2279 let hscrollh =
2280 match state.mode with
2281 | Textentry _ | View | LinkNav _ ->
2282 let h, _, _ = state.uioh#scrollpw in
2284 | _ -> 0
2286 let rect x w =
2287 GlDraw.rect
2288 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2289 (x+.w, float (state.winh - hscrollh))
2292 let w = float (wadjsb state.winw - 1) in
2293 if state.progress >= 0.0 && state.progress < 1.0
2294 then (
2295 GlDraw.color (0.3, 0.3, 0.3);
2296 let w1 = w *. state.progress in
2297 rect 0.0 w1;
2298 GlDraw.color (0.0, 0.0, 0.0);
2299 rect w1 (w-.w1)
2301 else (
2302 GlDraw.color (0.0, 0.0, 0.0);
2303 rect 0.0 w;
2306 GlDraw.color (1.0, 1.0, 1.0);
2307 drawstring fstate.fontsize
2308 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2310 let s =
2311 match state.mode with
2312 | Textentry ((prefix, text, _, _, _, _), _) ->
2313 let s =
2314 if len > 0
2315 then
2316 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2317 else
2318 Printf.sprintf "%s%s_" prefix text
2322 | _ -> state.text
2324 let s =
2325 if state.newerrmsgs
2326 then (
2327 if not (istextentry state.mode) && state.uioh#eformsgs
2328 then
2329 let s1 = "(press 'e' to review error messasges)" in
2330 if String.length s > 0 then s ^ " " ^ s1 else s1
2331 else s
2333 else s
2335 if String.length s > 0
2336 then drawstring s
2339 let gctiles () =
2340 let len = Queue.length state.tilelru in
2341 let layout = lazy (
2342 match state.throttle with
2343 | None ->
2344 if conf.preload
2345 then preloadlayout state.y
2346 else state.layout
2347 | Some (layout, _, _) ->
2348 layout
2349 ) in
2350 let rec loop qpos =
2351 if state.memused <= conf.memlimit
2352 then ()
2353 else (
2354 if qpos < len
2355 then
2356 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2357 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2358 let (_, pw, ph, _) = getpagedim n in
2360 gen = state.gen
2361 && colorspace = conf.colorspace
2362 && angle = conf.angle
2363 && pagew = pw
2364 && pageh = ph
2365 && (
2366 let x = col*conf.tilew
2367 and y = row*conf.tileh in
2368 tilevisible (Lazy.force_val layout) n x y
2370 then Queue.push lruitem state.tilelru
2371 else (
2372 freepbo p;
2373 wcmd "freetile %s" p;
2374 state.memused <- state.memused - s;
2375 state.uioh#infochanged Memused;
2376 Hashtbl.remove state.tilemap k;
2378 loop (qpos+1)
2381 loop 0
2384 let logcurrently = function
2385 | Idle -> dolog "Idle"
2386 | Loading (l, gen) ->
2387 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2388 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2389 dolog
2390 "Tiling %d[%d,%d] page=%s cs=%s angle"
2391 l.pageno col row pageopaque
2392 (CSTE.to_string colorspace)
2394 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2395 angle gen conf.angle state.gen
2396 tilew tileh
2397 conf.tilew conf.tileh
2399 | Outlining _ ->
2400 dolog "outlining"
2403 let splitatspace =
2404 let r = Str.regexp " " in
2405 fun s -> Str.bounded_split r s 2;
2408 let onpagerect pageno f =
2409 let b =
2410 match conf.columns with
2411 | Cmulti (_, b) -> b
2412 | Csingle b -> b
2413 | Csplit (_, b) -> b
2415 if pageno >= 0 && pageno < Array.length b
2416 then
2417 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2418 let r = getpdimrect pdimno in
2419 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2422 let gotopagexy1 pageno x y =
2423 onpagerect pageno (fun w h ->
2424 let top = y /. h in
2425 let _,w1,_,leftx = getpagedim pageno in
2426 let wh = state.winh - hscrollh () in
2427 let sw = float w1 /. w in
2428 let x = sw *. x in
2429 let x = leftx + state.x + truncate x in
2430 let sx =
2431 if x < 0 || x >= wadjsb state.winw
2432 then state.x - x
2433 else state.x
2435 let py, h = getpageyh pageno in
2436 let pdy = truncate (top *. float h) in
2437 let y' = py + pdy in
2438 let dy = y' - state.y in
2439 let sy =
2440 if x != state.x || not (dy > 0 && dy < wh)
2441 then (
2442 if conf.presentation
2443 then
2444 if abs (py - y') > wh
2445 then y'
2446 else py
2447 else y';
2449 else state.y
2451 if state.x != sx || state.y != sy
2452 then (
2453 let x, y =
2454 if !wtmode
2455 then (
2456 let ww = wadjsb state.winw in
2457 let qx = sx / ww
2458 and qy = pdy / wh in
2459 let x = qx * ww
2460 and y = py + qy * wh in
2461 let x = if -x + ww > w1 then -(w1-ww) else x
2462 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2463 let y =
2464 if conf.presentation
2465 then
2466 if abs (py - y') > wh
2467 then y'
2468 else py
2469 else y';
2471 (x, y)
2473 else (sx, sy)
2475 state.x <- x;
2476 gotoy_and_clear_text y;
2478 else gotoy_and_clear_text state.y;
2482 let gotopagexy pageno x y =
2483 match state.mode with
2484 | Birdseye _ -> gotopage pageno 0.0
2485 | _ -> gotopagexy1 pageno x y
2488 let act cmds =
2489 (* dolog "%S" cmds; *)
2490 let cl = splitatspace cmds in
2491 let scan s fmt f =
2492 try Scanf.sscanf s fmt f
2493 with exn ->
2494 dolog "error processing '%S': %s" cmds (exntos exn);
2495 exit 1
2497 match cl with
2498 | "clear" :: [] ->
2499 state.uioh#infochanged Pdim;
2500 state.pdims <- [];
2502 | "clearrects" :: [] ->
2503 state.rects <- state.rects1;
2504 G.postRedisplay "clearrects";
2506 | "continue" :: args :: [] ->
2507 let n = scan args "%u" (fun n -> n) in
2508 state.pagecount <- n;
2509 begin match state.currently with
2510 | Outlining l ->
2511 state.currently <- Idle;
2512 state.outlines <- Array.of_list (List.rev l)
2513 | _ -> ()
2514 end;
2516 let cur, cmds = state.geomcmds in
2517 if String.length cur = 0
2518 then failwith "umpossible";
2520 begin match List.rev cmds with
2521 | [] ->
2522 state.geomcmds <- "", [];
2523 represent ();
2524 | (s, f) :: rest ->
2525 f ();
2526 state.geomcmds <- s, List.rev rest;
2527 end;
2528 if conf.maxwait = None && not !wtmode
2529 then G.postRedisplay "continue";
2531 | "title" :: args :: [] ->
2532 Wsi.settitle args
2534 | "msg" :: args :: [] ->
2535 showtext ' ' args
2537 | "vmsg" :: args :: [] ->
2538 if conf.verbose
2539 then showtext ' ' args
2541 | "emsg" :: args :: [] ->
2542 Buffer.add_string state.errmsgs args;
2543 state.newerrmsgs <- true;
2544 G.postRedisplay "error message"
2546 | "progress" :: args :: [] ->
2547 let progress, text =
2548 scan args "%f %n"
2549 (fun f pos ->
2550 f, String.sub args pos (String.length args - pos))
2552 state.text <- text;
2553 state.progress <- progress;
2554 G.postRedisplay "progress"
2556 | "firstmatch" :: args :: [] ->
2557 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2558 scan args "%u %d %f %f %f %f %f %f %f %f"
2559 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2560 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2562 let y = (getpagey pageno) + truncate y0 in
2563 addnav ();
2564 gotoy y;
2565 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2567 | "match" :: args :: [] ->
2568 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2569 scan args "%u %d %f %f %f %f %f %f %f %f"
2570 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2571 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2573 state.rects1 <-
2574 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2576 | "page" :: args :: [] ->
2577 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2578 begin match state.currently with
2579 | Loading (l, gen) ->
2580 vlog "page %d took %f sec" l.pageno t;
2581 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2582 begin match state.throttle with
2583 | None ->
2584 let preloadedpages =
2585 if conf.preload
2586 then preloadlayout state.y
2587 else state.layout
2589 let evict () =
2590 let set =
2591 List.fold_left (fun s l -> IntSet.add l.pageno s)
2592 IntSet.empty preloadedpages
2594 let evictedpages =
2595 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2596 if not (IntSet.mem pageno set)
2597 then (
2598 wcmd "freepage %s" opaque;
2599 key :: accu
2601 else accu
2602 ) state.pagemap []
2604 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2606 evict ();
2607 state.currently <- Idle;
2608 if gen = state.gen
2609 then (
2610 tilepage l.pageno pageopaque state.layout;
2611 load state.layout;
2612 load preloadedpages;
2613 if pagevisible state.layout l.pageno
2614 && layoutready state.layout
2615 then G.postRedisplay "page";
2618 | Some (layout, _, _) ->
2619 state.currently <- Idle;
2620 tilepage l.pageno pageopaque layout;
2621 load state.layout
2622 end;
2624 | _ ->
2625 dolog "Inconsistent loading state";
2626 logcurrently state.currently;
2627 exit 1
2630 | "tile" :: args :: [] ->
2631 let (x, y, opaque, size, t) =
2632 scan args "%u %u %s %u %f"
2633 (fun x y p size t -> (x, y, p, size, t))
2635 begin match state.currently with
2636 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2637 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2639 unmappbo opaque;
2640 if tilew != conf.tilew || tileh != conf.tileh
2641 then (
2642 wcmd "freetile %s" opaque;
2643 state.currently <- Idle;
2644 load state.layout;
2646 else (
2647 puttileopaque l col row gen cs angle opaque size t;
2648 state.memused <- state.memused + size;
2649 state.uioh#infochanged Memused;
2650 gctiles ();
2651 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2652 opaque, size) state.tilelru;
2654 let layout =
2655 match state.throttle with
2656 | None -> state.layout
2657 | Some (layout, _, _) -> layout
2660 state.currently <- Idle;
2661 if gen = state.gen
2662 && conf.colorspace = cs
2663 && conf.angle = angle
2664 && tilevisible layout l.pageno x y
2665 then conttiling l.pageno pageopaque;
2667 begin match state.throttle with
2668 | None ->
2669 preload state.layout;
2670 if gen = state.gen
2671 && conf.colorspace = cs
2672 && conf.angle = angle
2673 && tilevisible state.layout l.pageno x y
2674 && (not !wtmode || layoutready state.layout)
2675 then G.postRedisplay "tile nothrottle";
2677 | Some (layout, y, _) ->
2678 let ready = layoutready layout in
2679 if ready
2680 then (
2681 state.y <- y;
2682 state.layout <- layout;
2683 state.throttle <- None;
2684 G.postRedisplay "throttle";
2686 else load layout;
2687 end;
2690 | _ ->
2691 dolog "Inconsistent tiling state";
2692 logcurrently state.currently;
2693 exit 1
2696 | "pdim" :: args :: [] ->
2697 let (n, w, h, _) as pdim =
2698 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2700 let pdim =
2701 match conf.fitmodel, conf.columns with
2702 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2703 | _ -> pdim
2705 state.uioh#infochanged Pdim;
2706 state.pdims <- pdim :: state.pdims
2708 | "o" :: args :: [] ->
2709 let (l, n, t, h, pos) =
2710 scan args "%u %u %d %u %n"
2711 (fun l n t h pos -> l, n, t, h, pos)
2713 let s = String.sub args pos (String.length args - pos) in
2714 let outline = (s, l, (n, float t /. float h, 0.0)) in
2715 begin match state.currently with
2716 | Outlining outlines ->
2717 state.currently <- Outlining (outline :: outlines)
2718 | Idle ->
2719 state.currently <- Outlining [outline]
2720 | currently ->
2721 dolog "invalid outlining state";
2722 logcurrently currently
2725 | "a" :: args :: [] ->
2726 let (n, l, t) =
2727 scan args "%u %d %d" (fun n l t -> n, l, t)
2729 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2731 | "info" :: args :: [] ->
2732 state.docinfo <- (1, args) :: state.docinfo
2734 | "infoend" :: [] ->
2735 state.uioh#infochanged Docinfo;
2736 state.docinfo <- List.rev state.docinfo
2738 | _ ->
2739 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2742 let onhist cb =
2743 let rc = cb.rc in
2744 let action = function
2745 | HCprev -> cbget cb ~-1
2746 | HCnext -> cbget cb 1
2747 | HCfirst -> cbget cb ~-(cb.rc)
2748 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2749 and cancel () = cb.rc <- rc
2750 in (action, cancel)
2753 let search pattern forward =
2754 match conf.columns with
2755 | Csplit _ ->
2756 showtext '!' "searching does not work properly in split columns mode"
2757 | _ ->
2758 if String.length pattern > 0
2759 then
2760 let pn, py =
2761 match state.layout with
2762 | [] -> 0, 0
2763 | l :: _ ->
2764 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2766 wcmd "search %d %d %d %d,%s\000"
2767 (btod conf.icase) pn py (btod forward) pattern;
2770 let intentry text key =
2771 let c =
2772 if key >= 32 && key < 127
2773 then Char.chr key
2774 else '\000'
2776 match c with
2777 | '0' .. '9' ->
2778 let text = addchar text c in
2779 TEcont text
2781 | _ ->
2782 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2783 TEcont text
2786 let linknentry text key =
2787 let c =
2788 if key >= 32 && key < 127
2789 then Char.chr key
2790 else '\000'
2792 match c with
2793 | 'a' .. 'z' ->
2794 let text = addchar text c in
2795 TEcont text
2797 | _ ->
2798 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2799 TEcont text
2802 let linkndone f s =
2803 if String.length s > 0
2804 then (
2805 let n =
2806 let l = String.length s in
2807 let rec loop pos n = if pos = l then n else
2808 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2809 loop (pos+1) (n*26 + m)
2810 in loop 0 0
2812 let rec loop n = function
2813 | [] -> ()
2814 | l :: rest ->
2815 match getopaque l.pageno with
2816 | None -> loop n rest
2817 | Some opaque ->
2818 let m = getlinkcount opaque in
2819 if n < m
2820 then (
2821 let under = getlink opaque n in
2822 f under
2824 else loop (n-m) rest
2826 loop n state.layout;
2830 let textentry text key =
2831 if key land 0xff00 = 0xff00
2832 then TEcont text
2833 else TEcont (text ^ toutf8 key)
2836 let reqlayout angle fitmodel =
2837 match state.throttle with
2838 | None ->
2839 if nogeomcmds state.geomcmds
2840 then state.anchor <- getanchor ();
2841 conf.angle <- angle mod 360;
2842 if conf.angle != 0
2843 then (
2844 match state.mode with
2845 | LinkNav _ -> state.mode <- View
2846 | _ -> ()
2848 conf.fitmodel <- fitmodel;
2849 invalidate "reqlayout"
2850 (fun () ->
2851 wcmd "reqlayout %d %d %d"
2852 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2854 | _ -> ()
2857 let settrim trimmargins trimfuzz =
2858 if nogeomcmds state.geomcmds
2859 then state.anchor <- getanchor ();
2860 conf.trimmargins <- trimmargins;
2861 conf.trimfuzz <- trimfuzz;
2862 let x0, y0, x1, y1 = trimfuzz in
2863 invalidate "settrim"
2864 (fun () ->
2865 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2866 flushpages ();
2869 let setzoom zoom =
2870 match state.throttle with
2871 | None ->
2872 let zoom = max 0.0001 zoom in
2873 if zoom <> conf.zoom
2874 then (
2875 state.prevzoom <- conf.zoom;
2876 conf.zoom <- zoom;
2877 reshape state.winw state.winh;
2878 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2881 | Some (layout, y, started) ->
2882 let time =
2883 match conf.maxwait with
2884 | None -> 0.0
2885 | Some t -> t
2887 let dt = now () -. started in
2888 if dt > time
2889 then (
2890 state.y <- y;
2891 load layout;
2895 let setcolumns mode columns coverA coverB =
2896 state.prevcolumns <- Some (conf.columns, conf.zoom);
2897 if columns < 0
2898 then (
2899 if isbirdseye mode
2900 then showtext '!' "split mode doesn't work in bird's eye"
2901 else (
2902 conf.columns <- Csplit (-columns, [||]);
2903 state.x <- 0;
2904 conf.zoom <- 1.0;
2907 else (
2908 if columns < 2
2909 then (
2910 conf.columns <- Csingle [||];
2911 state.x <- 0;
2912 setzoom 1.0;
2914 else (
2915 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2916 conf.zoom <- 1.0;
2919 reshape state.winw state.winh;
2922 let enterbirdseye () =
2923 let zoom = float conf.thumbw /. float state.winw in
2924 let birdseyepageno =
2925 let cy = state.winh / 2 in
2926 let fold = function
2927 | [] -> 0
2928 | l :: rest ->
2929 let rec fold best = function
2930 | [] -> best.pageno
2931 | l :: rest ->
2932 let d = cy - (l.pagedispy + l.pagevh/2)
2933 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2934 if abs d < abs dbest
2935 then fold l rest
2936 else best.pageno
2937 in fold l rest
2939 fold state.layout
2941 state.mode <- Birdseye (
2942 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2944 conf.zoom <- zoom;
2945 conf.presentation <- false;
2946 conf.interpagespace <- 10;
2947 conf.hlinks <- false;
2948 conf.fitmodel <- FitProportional;
2949 state.x <- 0;
2950 state.mstate <- Mnone;
2951 conf.maxwait <- None;
2952 conf.columns <- (
2953 match conf.beyecolumns with
2954 | Some c ->
2955 conf.zoom <- 1.0;
2956 Cmulti ((c, 0, 0), [||])
2957 | None -> Csingle [||]
2959 Wsi.setcursor Wsi.CURSOR_INHERIT;
2960 if conf.verbose
2961 then
2962 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2963 (100.0*.zoom)
2964 else
2965 state.text <- ""
2967 reshape state.winw state.winh;
2970 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2971 state.mode <- View;
2972 conf.zoom <- c.zoom;
2973 conf.presentation <- c.presentation;
2974 conf.interpagespace <- c.interpagespace;
2975 conf.maxwait <- c.maxwait;
2976 conf.hlinks <- c.hlinks;
2977 conf.fitmodel <- c.fitmodel;
2978 conf.beyecolumns <- (
2979 match conf.columns with
2980 | Cmulti ((c, _, _), _) -> Some c
2981 | Csingle _ -> None
2982 | Csplit _ -> failwith "leaving bird's eye split mode"
2984 conf.columns <- (
2985 match c.columns with
2986 | Cmulti (c, _) -> Cmulti (c, [||])
2987 | Csingle _ -> Csingle [||]
2988 | Csplit (c, _) -> Csplit (c, [||])
2990 state.x <- leftx;
2991 if conf.verbose
2992 then
2993 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2994 (100.0*.conf.zoom)
2996 reshape state.winw state.winh;
2997 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3000 let togglebirdseye () =
3001 match state.mode with
3002 | Birdseye vals -> leavebirdseye vals true
3003 | View -> enterbirdseye ()
3004 | _ -> ()
3007 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3008 let pageno = max 0 (pageno - incr) in
3009 let rec loop = function
3010 | [] -> gotopage1 pageno 0
3011 | l :: _ when l.pageno = pageno ->
3012 if l.pagedispy >= 0 && l.pagey = 0
3013 then G.postRedisplay "upbirdseye"
3014 else gotopage1 pageno 0
3015 | _ :: rest -> loop rest
3017 loop state.layout;
3018 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3021 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3022 let pageno = min (state.pagecount - 1) (pageno + incr) in
3023 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3024 let rec loop = function
3025 | [] ->
3026 let y, h = getpageyh pageno in
3027 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3028 gotoy (clamp dy)
3029 | l :: _ when l.pageno = pageno ->
3030 if l.pagevh != l.pageh
3031 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3032 else G.postRedisplay "downbirdseye"
3033 | _ :: rest -> loop rest
3035 loop state.layout
3038 let optentry mode _ key =
3039 let btos b = if b then "on" else "off" in
3040 if key >= 32 && key < 127
3041 then
3042 let c = Char.chr key in
3043 match c with
3044 | 's' ->
3045 let ondone s =
3046 try conf.scrollstep <- int_of_string s with exc ->
3047 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3049 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3051 | 'A' ->
3052 let ondone s =
3054 conf.autoscrollstep <- int_of_string s;
3055 if state.autoscroll <> None
3056 then state.autoscroll <- Some conf.autoscrollstep
3057 with exc ->
3058 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3060 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3062 | 'C' ->
3063 let ondone s =
3065 let n, a, b = multicolumns_of_string s in
3066 setcolumns mode n a b;
3067 with exc ->
3068 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3070 TEswitch ("columns: ", "", None, textentry, ondone, true)
3072 | 'Z' ->
3073 let ondone s =
3075 let zoom = float (int_of_string s) /. 100.0 in
3076 setzoom zoom
3077 with exc ->
3078 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3080 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3082 | 't' ->
3083 let ondone s =
3085 conf.thumbw <- bound (int_of_string s) 2 4096;
3086 state.text <-
3087 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3088 begin match mode with
3089 | Birdseye beye ->
3090 leavebirdseye beye false;
3091 enterbirdseye ();
3092 | _ -> ();
3094 with exc ->
3095 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3097 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3099 | 'R' ->
3100 let ondone s =
3101 match try
3102 Some (int_of_string s)
3103 with exc ->
3104 state.text <- Printf.sprintf "bad integer `%s': %s"
3105 s (exntos exc);
3106 None
3107 with
3108 | Some angle -> reqlayout angle conf.fitmodel
3109 | None -> ()
3111 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3113 | 'i' ->
3114 conf.icase <- not conf.icase;
3115 TEdone ("case insensitive search " ^ (btos conf.icase))
3117 | 'p' ->
3118 conf.preload <- not conf.preload;
3119 gotoy state.y;
3120 TEdone ("preload " ^ (btos conf.preload))
3122 | 'v' ->
3123 conf.verbose <- not conf.verbose;
3124 TEdone ("verbose " ^ (btos conf.verbose))
3126 | 'd' ->
3127 conf.debug <- not conf.debug;
3128 TEdone ("debug " ^ (btos conf.debug))
3130 | 'h' ->
3131 conf.maxhfit <- not conf.maxhfit;
3132 state.maxy <- calcheight ();
3133 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3135 | 'c' ->
3136 conf.crophack <- not conf.crophack;
3137 TEdone ("crophack " ^ btos conf.crophack)
3139 | 'a' ->
3140 let s =
3141 match conf.maxwait with
3142 | None ->
3143 conf.maxwait <- Some infinity;
3144 "always wait for page to complete"
3145 | Some _ ->
3146 conf.maxwait <- None;
3147 "show placeholder if page is not ready"
3149 TEdone s
3151 | 'f' ->
3152 conf.underinfo <- not conf.underinfo;
3153 TEdone ("underinfo " ^ btos conf.underinfo)
3155 | 'P' ->
3156 conf.savebmarks <- not conf.savebmarks;
3157 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3159 | 'S' ->
3160 let ondone s =
3162 let pageno, py =
3163 match state.layout with
3164 | [] -> 0, 0
3165 | l :: _ ->
3166 l.pageno, l.pagey
3168 conf.interpagespace <- int_of_string s;
3169 docolumns conf.columns;
3170 state.maxy <- calcheight ();
3171 let y = getpagey pageno in
3172 gotoy (y + py)
3173 with exc ->
3174 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3176 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3178 | 'l' ->
3179 let fm =
3180 match conf.fitmodel with
3181 | FitProportional -> FitWidth
3182 | _ -> FitProportional
3184 reqlayout conf.angle fm;
3185 TEdone ("proportional display " ^ btos (fm == FitProportional))
3187 | 'T' ->
3188 settrim (not conf.trimmargins) conf.trimfuzz;
3189 TEdone ("trim margins " ^ btos conf.trimmargins)
3191 | 'I' ->
3192 conf.invert <- not conf.invert;
3193 TEdone ("invert colors " ^ btos conf.invert)
3195 | 'x' ->
3196 let ondone s =
3197 cbput state.hists.sel s;
3198 conf.selcmd <- s;
3200 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3201 textentry, ondone, true)
3203 | 'M' ->
3204 if conf.pax == None
3205 then conf.pax <- Some (ref (0.0, 0, 0))
3206 else conf.pax <- None;
3207 TEdone ("PAX " ^ btos (conf.pax != None))
3209 | _ ->
3210 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3211 TEstop
3212 else
3213 TEcont state.text
3216 class type lvsource = object
3217 method getitemcount : int
3218 method getitem : int -> (string * int)
3219 method hasaction : int -> bool
3220 method exit :
3221 uioh:uioh ->
3222 cancel:bool ->
3223 active:int ->
3224 first:int ->
3225 pan:int ->
3226 qsearch:string ->
3227 uioh option
3228 method getactive : int
3229 method getfirst : int
3230 method getqsearch : string
3231 method setqsearch : string -> unit
3232 method getpan : int
3233 end;;
3235 class virtual lvsourcebase = object
3236 val mutable m_active = 0
3237 val mutable m_first = 0
3238 val mutable m_qsearch = ""
3239 val mutable m_pan = 0
3240 method getactive = m_active
3241 method getfirst = m_first
3242 method getqsearch = m_qsearch
3243 method getpan = m_pan
3244 method setqsearch s = m_qsearch <- s
3245 end;;
3247 let withoutlastutf8 s =
3248 let len = String.length s in
3249 if len = 0
3250 then s
3251 else
3252 let rec find pos =
3253 if pos = 0
3254 then pos
3255 else
3256 let b = Char.code s.[pos] in
3257 if b land 0b11000000 = 0b11000000
3258 then pos
3259 else find (pos-1)
3261 let first =
3262 if Char.code s.[len-1] land 0x80 = 0
3263 then len-1
3264 else find (len-1)
3266 String.sub s 0 first;
3269 let textentrykeyboard
3270 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3271 let key =
3272 if key >= 0xffb0 && key <= 0xffb9
3273 then key - 0xffb0 + 48 else key
3275 let enttext te =
3276 state.mode <- Textentry (te, onleave);
3277 state.text <- "";
3278 enttext ();
3279 G.postRedisplay "textentrykeyboard enttext";
3281 let histaction cmd =
3282 match opthist with
3283 | None -> ()
3284 | Some (action, _) ->
3285 state.mode <- Textentry (
3286 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3288 G.postRedisplay "textentry histaction"
3290 match key with
3291 | 0xff08 -> (* backspace *)
3292 let s = withoutlastutf8 text in
3293 let len = String.length s in
3294 if cancelonempty && len = 0
3295 then (
3296 onleave Cancel;
3297 G.postRedisplay "textentrykeyboard after cancel";
3299 else (
3300 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3303 | 0xff0d | 0xff8d -> (* (kp) enter *)
3304 ondone text;
3305 onleave Confirm;
3306 G.postRedisplay "textentrykeyboard after confirm"
3308 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3309 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3310 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3311 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3313 | 0xff1b -> (* escape*)
3314 if String.length text = 0
3315 then (
3316 begin match opthist with
3317 | None -> ()
3318 | Some (_, onhistcancel) -> onhistcancel ()
3319 end;
3320 onleave Cancel;
3321 state.text <- "";
3322 G.postRedisplay "textentrykeyboard after cancel2"
3324 else (
3325 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3328 | 0xff9f | 0xffff -> () (* delete *)
3330 | _ when key != 0
3331 && key land 0xff00 != 0xff00 (* keyboard *)
3332 && key land 0xfe00 != 0xfe00 (* xkb *)
3333 && key land 0xfd00 != 0xfd00 (* 3270 *)
3335 begin match onkey text key with
3336 | TEdone text ->
3337 ondone text;
3338 onleave Confirm;
3339 G.postRedisplay "textentrykeyboard after confirm2";
3341 | TEcont text ->
3342 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3344 | TEstop ->
3345 onleave Cancel;
3346 G.postRedisplay "textentrykeyboard after cancel3"
3348 | TEswitch te ->
3349 state.mode <- Textentry (te, onleave);
3350 G.postRedisplay "textentrykeyboard switch";
3351 end;
3353 | _ ->
3354 vlog "unhandled key %s" (Wsi.keyname key)
3357 let firstof first active =
3358 if first > active || abs (first - active) > fstate.maxrows - 1
3359 then max 0 (active - (fstate.maxrows/2))
3360 else first
3363 let calcfirst first active =
3364 if active > first
3365 then
3366 let rows = active - first in
3367 if rows > fstate.maxrows then active - fstate.maxrows else first
3368 else active
3371 let scrollph y maxy =
3372 let sh = float (maxy + state.winh) /. float state.winh in
3373 let sh = float state.winh /. sh in
3374 let sh = max sh (float conf.scrollh) in
3376 let percent = float y /. float maxy in
3377 let position = (float state.winh -. sh) *. percent in
3379 let position =
3380 if position +. sh > float state.winh
3381 then float state.winh -. sh
3382 else position
3384 position, sh;
3387 let coe s = (s :> uioh);;
3389 class listview ~(source:lvsource) ~trusted ~modehash =
3390 object (self)
3391 val m_pan = source#getpan
3392 val m_first = source#getfirst
3393 val m_active = source#getactive
3394 val m_qsearch = source#getqsearch
3395 val m_prev_uioh = state.uioh
3397 method private elemunder y =
3398 let n = y / (fstate.fontsize+1) in
3399 if m_first + n < source#getitemcount
3400 then (
3401 if source#hasaction (m_first + n)
3402 then Some (m_first + n)
3403 else None
3405 else None
3407 method display =
3408 Gl.enable `blend;
3409 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3410 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3411 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3412 GlDraw.color (1., 1., 1.);
3413 Gl.enable `texture_2d;
3414 let fs = fstate.fontsize in
3415 let nfs = fs + 1 in
3416 let ww = fstate.wwidth in
3417 let tabw = 30.0*.ww in
3418 let itemcount = source#getitemcount in
3419 let rec loop row =
3420 if (row - m_first) > fstate.maxrows
3421 then ()
3422 else (
3423 if row >= 0 && row < itemcount
3424 then (
3425 let (s, level) = source#getitem row in
3426 let y = (row - m_first) * nfs in
3427 let x = 5.0 +. float (level + m_pan) *. ww in
3428 if row = m_active
3429 then (
3430 Gl.disable `texture_2d;
3431 GlDraw.polygon_mode `both `line;
3432 let alpha = if source#hasaction row then 0.9 else 0.3 in
3433 GlDraw.color (1., 1., 1.) ~alpha;
3434 GlDraw.rect (1., float (y + 1))
3435 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3436 GlDraw.polygon_mode `both `fill;
3437 GlDraw.color (1., 1., 1.);
3438 Gl.enable `texture_2d;
3441 let drawtabularstring s =
3442 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3443 if trusted
3444 then
3445 let tabpos = try String.index s '\t' with Not_found -> -1 in
3446 if tabpos > 0
3447 then
3448 let len = String.length s - tabpos - 1 in
3449 let s1 = String.sub s 0 tabpos
3450 and s2 = String.sub s (tabpos + 1) len in
3451 let nx = drawstr x s1 in
3452 let sw = nx -. x in
3453 let x = x +. (max tabw sw) in
3454 drawstr x s2
3455 else
3456 drawstr x s
3457 else
3458 drawstr x s
3460 let _ = drawtabularstring s in
3461 loop (row+1)
3465 loop m_first;
3466 Gl.disable `blend;
3467 Gl.disable `texture_2d;
3469 method updownlevel incr =
3470 let len = source#getitemcount in
3471 let curlevel =
3472 if m_active >= 0 && m_active < len
3473 then snd (source#getitem m_active)
3474 else -1
3476 let rec flow i =
3477 if i = len then i-1 else if i = -1 then 0 else
3478 let _, l = source#getitem i in
3479 if l != curlevel then i else flow (i+incr)
3481 let active = flow m_active in
3482 let first = calcfirst m_first active in
3483 G.postRedisplay "outline updownlevel";
3484 {< m_active = active; m_first = first >}
3486 method private key1 key mask =
3487 let set1 active first qsearch =
3488 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3490 let search active pattern incr =
3491 let active = if active = -1 then m_first else active in
3492 let dosearch re =
3493 let rec loop n =
3494 if n >= 0 && n < source#getitemcount
3495 then (
3496 let s, _ = source#getitem n in
3498 (try ignore (Str.search_forward re s 0); true
3499 with Not_found -> false)
3500 then Some n
3501 else loop (n + incr)
3503 else None
3505 loop active
3508 let re = Str.regexp_case_fold pattern in
3509 dosearch re
3510 with Failure s ->
3511 state.text <- s;
3512 None
3514 let itemcount = source#getitemcount in
3515 let find start incr =
3516 let rec find i =
3517 if i = -1 || i = itemcount
3518 then -1
3519 else (
3520 if source#hasaction i
3521 then i
3522 else find (i + incr)
3525 find start
3527 let set active first =
3528 let first = bound first 0 (itemcount - fstate.maxrows) in
3529 state.text <- "";
3530 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3532 let navigate incr =
3533 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3534 let active, first =
3535 let incr1 = if incr > 0 then 1 else -1 in
3536 if isvisible m_first m_active
3537 then
3538 let next =
3539 let next = m_active + incr in
3540 let next =
3541 if next < 0 || next >= itemcount
3542 then -1
3543 else find next incr1
3545 if abs (m_active - next) > fstate.maxrows
3546 then -1
3547 else next
3549 if next = -1
3550 then
3551 let first = m_first + incr in
3552 let first = bound first 0 (itemcount - fstate.maxrows) in
3553 let next =
3554 let next = m_active + incr in
3555 let next = bound next 0 (itemcount - 1) in
3556 find next ~-incr1
3558 let active =
3559 if next = -1
3560 then m_active
3561 else (
3562 if isvisible first next
3563 then next
3564 else m_active
3567 active, first
3568 else
3569 let first = min next m_first in
3570 let first =
3571 if abs (next - first) > fstate.maxrows
3572 then first + incr
3573 else first
3575 next, first
3576 else
3577 let first = m_first + incr in
3578 let first = bound first 0 (itemcount - 1) in
3579 let active =
3580 let next = m_active + incr in
3581 let next = bound next 0 (itemcount - 1) in
3582 let next = find next incr1 in
3583 let active =
3584 if next = -1 || abs (m_active - first) > fstate.maxrows
3585 then (
3586 let active = if m_active = -1 then next else m_active in
3587 active
3589 else next
3591 if isvisible first active
3592 then active
3593 else -1
3595 active, first
3597 G.postRedisplay "listview navigate";
3598 set active first;
3600 match key with
3601 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3602 let incr = if key = 0x72 then -1 else 1 in
3603 let active, first =
3604 match search (m_active + incr) m_qsearch incr with
3605 | None ->
3606 state.text <- m_qsearch ^ " [not found]";
3607 m_active, m_first
3608 | Some active ->
3609 state.text <- m_qsearch;
3610 active, firstof m_first active
3612 G.postRedisplay "listview ctrl-r/s";
3613 set1 active first m_qsearch;
3615 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3616 if m_active >= 0 && m_active < source#getitemcount
3617 then (
3618 let s, _ = source#getitem m_active in
3619 selstring s;
3621 coe self
3623 | 0xff08 -> (* backspace *)
3624 if String.length m_qsearch = 0
3625 then coe self
3626 else (
3627 let qsearch = withoutlastutf8 m_qsearch in
3628 let len = String.length qsearch in
3629 if len = 0
3630 then (
3631 state.text <- "";
3632 G.postRedisplay "listview empty qsearch";
3633 set1 m_active m_first "";
3635 else
3636 let active, first =
3637 match search m_active qsearch ~-1 with
3638 | None ->
3639 state.text <- qsearch ^ " [not found]";
3640 m_active, m_first
3641 | Some active ->
3642 state.text <- qsearch;
3643 active, firstof m_first active
3645 G.postRedisplay "listview backspace qsearch";
3646 set1 active first qsearch
3649 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3650 let pattern = m_qsearch ^ toutf8 key in
3651 let active, first =
3652 match search m_active pattern 1 with
3653 | None ->
3654 state.text <- pattern ^ " [not found]";
3655 m_active, m_first
3656 | Some active ->
3657 state.text <- pattern;
3658 active, firstof m_first active
3660 G.postRedisplay "listview qsearch add";
3661 set1 active first pattern;
3663 | 0xff1b -> (* escape *)
3664 state.text <- "";
3665 if String.length m_qsearch = 0
3666 then (
3667 G.postRedisplay "list view escape";
3668 begin
3669 match
3670 source#exit (coe self) true m_active m_first m_pan m_qsearch
3671 with
3672 | None -> m_prev_uioh
3673 | Some uioh -> uioh
3676 else (
3677 G.postRedisplay "list view kill qsearch";
3678 source#setqsearch "";
3679 coe {< m_qsearch = "" >}
3682 | 0xff0d | 0xff8d -> (* (kp) enter *)
3683 state.text <- "";
3684 let self = {< m_qsearch = "" >} in
3685 source#setqsearch "";
3686 let opt =
3687 G.postRedisplay "listview enter";
3688 if m_active >= 0 && m_active < source#getitemcount
3689 then (
3690 source#exit (coe self) false m_active m_first m_pan "";
3692 else (
3693 source#exit (coe self) true m_active m_first m_pan "";
3696 begin match opt with
3697 | None -> m_prev_uioh
3698 | Some uioh -> uioh
3701 | 0xff9f | 0xffff -> (* (kp) delete *)
3702 coe self
3704 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3705 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3706 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3707 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3709 | 0xff53 | 0xff98 -> (* (kp) right *)
3710 state.text <- "";
3711 G.postRedisplay "listview right";
3712 coe {< m_pan = m_pan - 1 >}
3714 | 0xff51 | 0xff96 -> (* (kp) left *)
3715 state.text <- "";
3716 G.postRedisplay "listview left";
3717 coe {< m_pan = m_pan + 1 >}
3719 | 0xff50 | 0xff95 -> (* (kp) home *)
3720 let active = find 0 1 in
3721 G.postRedisplay "listview home";
3722 set active 0;
3724 | 0xff57 | 0xff9c -> (* (kp) end *)
3725 let first = max 0 (itemcount - fstate.maxrows) in
3726 let active = find (itemcount - 1) ~-1 in
3727 G.postRedisplay "listview end";
3728 set active first;
3730 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3731 coe self
3733 | _ ->
3734 dolog "listview unknown key %#x" key; coe self
3736 method key key mask =
3737 match state.mode with
3738 | Textentry te -> textentrykeyboard key mask te; coe self
3739 | _ -> self#key1 key mask
3741 method button button down x y _ =
3742 let opt =
3743 match button with
3744 | 1 when x > state.winw - conf.scrollbw ->
3745 G.postRedisplay "listview scroll";
3746 if down
3747 then
3748 let _, position, sh = self#scrollph in
3749 if y > truncate position && y < truncate (position +. sh)
3750 then (
3751 state.mstate <- Mscrolly;
3752 Some (coe self)
3754 else
3755 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3756 let first = truncate (s *. float source#getitemcount) in
3757 let first = min source#getitemcount first in
3758 Some (coe {< m_first = first; m_active = first >})
3759 else (
3760 state.mstate <- Mnone;
3761 Some (coe self);
3763 | 1 when not down ->
3764 begin match self#elemunder y with
3765 | Some n ->
3766 G.postRedisplay "listview click";
3767 source#exit
3768 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3769 | _ ->
3770 Some (coe self)
3772 | n when (n == 4 || n == 5) && not down ->
3773 let len = source#getitemcount in
3774 let first =
3775 if n = 5 && m_first + fstate.maxrows >= len
3776 then
3777 m_first
3778 else
3779 let first = m_first + (if n == 4 then -1 else 1) in
3780 bound first 0 (len - 1)
3782 G.postRedisplay "listview wheel";
3783 Some (coe {< m_first = first >})
3784 | n when (n = 6 || n = 7) && not down ->
3785 let inc = m_first + (if n = 7 then -1 else 1) in
3786 G.postRedisplay "listview hwheel";
3787 Some (coe {< m_pan = m_pan + inc >})
3788 | _ ->
3789 Some (coe self)
3791 match opt with
3792 | None -> m_prev_uioh
3793 | Some uioh -> uioh
3795 method motion _ y =
3796 match state.mstate with
3797 | Mscrolly ->
3798 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3799 let first = truncate (s *. float source#getitemcount) in
3800 let first = min source#getitemcount first in
3801 G.postRedisplay "listview motion";
3802 coe {< m_first = first; m_active = first >}
3803 | _ -> coe self
3805 method pmotion x y =
3806 if x < state.winw - conf.scrollbw
3807 then
3808 let n =
3809 match self#elemunder y with
3810 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3811 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3813 let o =
3814 if n != m_active
3815 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3816 else self
3818 coe o
3819 else (
3820 Wsi.setcursor Wsi.CURSOR_INHERIT;
3821 coe self
3824 method infochanged _ = ()
3826 method scrollpw = (0, 0.0, 0.0)
3827 method scrollph =
3828 let nfs = fstate.fontsize + 1 in
3829 let y = m_first * nfs in
3830 let itemcount = source#getitemcount in
3831 let maxi = max 0 (itemcount - fstate.maxrows) in
3832 let maxy = maxi * nfs in
3833 let p, h = scrollph y maxy in
3834 conf.scrollbw, p, h
3836 method modehash = modehash
3837 method eformsgs = false
3838 end;;
3840 class outlinelistview ~source =
3841 object (self)
3842 inherit listview
3843 ~source:(source :> lvsource)
3844 ~trusted:false
3845 ~modehash:(findkeyhash conf "outline")
3846 as super
3848 method key key mask =
3849 let calcfirst first active =
3850 if active > first
3851 then
3852 let rows = active - first in
3853 let maxrows =
3854 if String.length state.text = 0
3855 then fstate.maxrows
3856 else fstate.maxrows - 2
3858 if rows > maxrows then active - maxrows else first
3859 else active
3861 let navigate incr =
3862 let active = m_active + incr in
3863 let active = bound active 0 (source#getitemcount - 1) in
3864 let first = calcfirst m_first active in
3865 G.postRedisplay "outline navigate";
3866 coe {< m_active = active; m_first = first >}
3868 let ctrl = Wsi.withctrl mask in
3869 match key with
3870 | 110 when ctrl -> (* ctrl-n *)
3871 source#narrow m_qsearch;
3872 G.postRedisplay "outline ctrl-n";
3873 coe {< m_first = 0; m_active = 0 >}
3875 | 117 when ctrl -> (* ctrl-u *)
3876 source#denarrow;
3877 G.postRedisplay "outline ctrl-u";
3878 state.text <- "";
3879 coe {< m_first = 0; m_active = 0 >}
3881 | 108 when ctrl -> (* ctrl-l *)
3882 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3883 G.postRedisplay "outline ctrl-l";
3884 coe {< m_first = first >}
3886 | 0xff9f | 0xffff -> (* (kp) delete *)
3887 source#remove m_active;
3888 G.postRedisplay "outline delete";
3889 let active = max 0 (m_active-1) in
3890 coe {< m_first = firstof m_first active;
3891 m_active = active >}
3893 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3894 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3895 | 0xff55 | 0xff9a -> (* (kp) prior *)
3896 navigate ~-(fstate.maxrows)
3897 | 0xff56 | 0xff9b -> (* (kp) next *)
3898 navigate fstate.maxrows
3900 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3901 let o =
3902 if ctrl
3903 then (
3904 G.postRedisplay "outline ctrl right";
3905 {< m_pan = m_pan + 1 >}
3907 else self#updownlevel 1
3909 coe o
3911 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3912 let o =
3913 if ctrl
3914 then (
3915 G.postRedisplay "outline ctrl left";
3916 {< m_pan = m_pan - 1 >}
3918 else self#updownlevel ~-1
3920 coe o
3922 | 0xff50 | 0xff95 -> (* (kp) home *)
3923 G.postRedisplay "outline home";
3924 coe {< m_first = 0; m_active = 0 >}
3926 | 0xff57 | 0xff9c -> (* (kp) end *)
3927 let active = source#getitemcount - 1 in
3928 let first = max 0 (active - fstate.maxrows) in
3929 G.postRedisplay "outline end";
3930 coe {< m_active = active; m_first = first >}
3932 | _ -> super#key key mask
3935 let outlinesource usebookmarks =
3936 let empty = [||] in
3937 (object
3938 inherit lvsourcebase
3939 val mutable m_items = empty
3940 val mutable m_orig_items = empty
3941 val mutable m_prev_items = empty
3942 val mutable m_narrow_pattern = ""
3943 val mutable m_hadremovals = false
3945 method getitemcount =
3946 Array.length m_items + (if m_hadremovals then 1 else 0)
3948 method getitem n =
3949 if n == Array.length m_items && m_hadremovals
3950 then
3951 ("[Confirm removal]", 0)
3952 else
3953 let s, n, _ = m_items.(n) in
3954 (s, n)
3956 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3957 ignore (uioh, first, qsearch);
3958 let confrimremoval = m_hadremovals && active = Array.length m_items in
3959 let items =
3960 if String.length m_narrow_pattern = 0
3961 then m_orig_items
3962 else m_items
3964 if not cancel
3965 then (
3966 if not confrimremoval
3967 then(
3968 let _, _, anchor = m_items.(active) in
3969 gotoghyll (getanchory anchor);
3970 m_items <- items;
3972 else (
3973 state.bookmarks <- Array.to_list m_items;
3974 m_orig_items <- m_items;
3977 else m_items <- items;
3978 m_pan <- pan;
3979 None
3981 method hasaction _ = true
3983 method greetmsg =
3984 if Array.length m_items != Array.length m_orig_items
3985 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3986 else ""
3988 method narrow pattern =
3989 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3990 match reopt with
3991 | None -> ()
3992 | Some re ->
3993 let rec loop accu n =
3994 if n = -1
3995 then (
3996 m_narrow_pattern <- pattern;
3997 m_items <- Array.of_list accu
3999 else
4000 let (s, _, _) as o = m_items.(n) in
4001 let accu =
4002 if (try ignore (Str.search_forward re s 0); true
4003 with Not_found -> false)
4004 then o :: accu
4005 else accu
4007 loop accu (n-1)
4009 loop [] (Array.length m_items - 1)
4011 method denarrow =
4012 m_orig_items <- (
4013 if usebookmarks
4014 then Array.of_list state.bookmarks
4015 else state.outlines
4017 m_items <- m_orig_items
4019 method remove m =
4020 if usebookmarks
4021 then
4022 if m >= 0 && m < Array.length m_items
4023 then (
4024 m_hadremovals <- true;
4025 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4026 let n = if n >= m then n+1 else n in
4027 m_items.(n)
4031 method reset anchor items =
4032 m_hadremovals <- false;
4033 if m_orig_items == empty || m_prev_items != items
4034 then (
4035 m_orig_items <- items;
4036 if String.length m_narrow_pattern = 0
4037 then m_items <- items;
4039 m_prev_items <- items;
4040 let rely = getanchory anchor in
4041 let active =
4042 let rec loop n best bestd =
4043 if n = Array.length m_items
4044 then best
4045 else
4046 let (_, _, anchor) = m_items.(n) in
4047 let orely = getanchory anchor in
4048 let d = abs (orely - rely) in
4049 if d < bestd
4050 then loop (n+1) n d
4051 else loop (n+1) best bestd
4053 loop 0 ~-1 max_int
4055 m_active <- active;
4056 m_first <- firstof m_first active
4057 end)
4060 let enterselector usebookmarks =
4061 let source = outlinesource usebookmarks in
4062 fun errmsg ->
4063 let outlines =
4064 if usebookmarks
4065 then Array.of_list state.bookmarks
4066 else state.outlines
4068 if Array.length outlines = 0
4069 then (
4070 showtext ' ' errmsg;
4072 else (
4073 state.text <- source#greetmsg;
4074 Wsi.setcursor Wsi.CURSOR_INHERIT;
4075 let anchor = getanchor () in
4076 source#reset anchor outlines;
4077 state.uioh <- coe (new outlinelistview ~source);
4078 G.postRedisplay "enter selector";
4082 let enteroutlinemode =
4083 let f = enterselector false in
4084 fun ()-> f "Document has no outline";
4087 let enterbookmarkmode =
4088 let f = enterselector true in
4089 fun () -> f "Document has no bookmarks (yet)";
4092 let color_of_string s =
4093 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4094 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4098 let color_to_string (r, g, b) =
4099 let r = truncate (r *. 256.0)
4100 and g = truncate (g *. 256.0)
4101 and b = truncate (b *. 256.0) in
4102 Printf.sprintf "%d/%d/%d" r g b
4105 let irect_of_string s =
4106 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4109 let irect_to_string (x0,y0,x1,y1) =
4110 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4113 let makecheckers () =
4114 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4115 following to say:
4116 converted by Issac Trotts. July 25, 2002 *)
4117 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4118 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4119 let id = GlTex.gen_texture () in
4120 GlTex.bind_texture `texture_2d id;
4121 GlPix.store (`unpack_alignment 1);
4122 GlTex.image2d image;
4123 List.iter (GlTex.parameter ~target:`texture_2d)
4124 [ `mag_filter `nearest; `min_filter `nearest ];
4128 let setcheckers enabled =
4129 match state.texid with
4130 | None ->
4131 if enabled then state.texid <- Some (makecheckers ())
4133 | Some texid ->
4134 if not enabled
4135 then (
4136 GlTex.delete_texture texid;
4137 state.texid <- None;
4141 let int_of_string_with_suffix s =
4142 let l = String.length s in
4143 let s1, shift =
4144 if l > 1
4145 then
4146 let suffix = Char.lowercase s.[l-1] in
4147 match suffix with
4148 | 'k' -> String.sub s 0 (l-1), 10
4149 | 'm' -> String.sub s 0 (l-1), 20
4150 | 'g' -> String.sub s 0 (l-1), 30
4151 | _ -> s, 0
4152 else s, 0
4154 let n = int_of_string s1 in
4155 let m = n lsl shift in
4156 if m < 0 || m < n
4157 then raise (Failure "value too large")
4158 else m
4161 let string_with_suffix_of_int n =
4162 if n = 0
4163 then "0"
4164 else
4165 let n, s =
4166 if n land ((1 lsl 30) - 1) = 0
4167 then n lsr 30, "G"
4168 else (
4169 if n land ((1 lsl 20) - 1) = 0
4170 then n lsr 20, "M"
4171 else (
4172 if n land ((1 lsl 10) - 1) = 0
4173 then n lsr 10, "K"
4174 else n, ""
4178 let rec loop s n =
4179 let h = n mod 1000 in
4180 let n = n / 1000 in
4181 if n = 0
4182 then string_of_int h ^ s
4183 else (
4184 let s = Printf.sprintf "_%03d%s" h s in
4185 loop s n
4188 loop "" n ^ s;
4191 let defghyllscroll = (40, 8, 32);;
4192 let ghyllscroll_of_string s =
4193 let (n, a, b) as nab =
4194 if s = "default"
4195 then defghyllscroll
4196 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4198 if n <= a || n <= b || a >= b
4199 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4200 nab;
4203 let ghyllscroll_to_string ((n, a, b) as nab) =
4204 if nab = defghyllscroll
4205 then "default"
4206 else Printf.sprintf "%d,%d,%d" n a b;
4209 let describe_location () =
4210 let fn = page_of_y state.y in
4211 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4212 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4213 let percent =
4214 if maxy <= 0
4215 then 100.
4216 else (100. *. (float state.y /. float maxy))
4218 if fn = ln
4219 then
4220 Printf.sprintf "page %d of %d [%.2f%%]"
4221 (fn+1) state.pagecount percent
4222 else
4223 Printf.sprintf
4224 "pages %d-%d of %d [%.2f%%]"
4225 (fn+1) (ln+1) state.pagecount percent
4228 let setpresentationmode v =
4229 let n = page_of_y state.y in
4230 state.anchor <- (n, 0.0, 1.0);
4231 conf.presentation <- v;
4232 if conf.fitmodel = FitPage
4233 then reqlayout conf.angle conf.fitmodel;
4234 represent ();
4237 let enterinfomode =
4238 let btos b = if b then "\xe2\x88\x9a" else "" in
4239 let showextended = ref false in
4240 let leave mode = function
4241 | Confirm -> state.mode <- mode
4242 | Cancel -> state.mode <- mode in
4243 let src =
4244 (object
4245 val mutable m_first_time = true
4246 val mutable m_l = []
4247 val mutable m_a = [||]
4248 val mutable m_prev_uioh = nouioh
4249 val mutable m_prev_mode = View
4251 inherit lvsourcebase
4253 method reset prev_mode prev_uioh =
4254 m_a <- Array.of_list (List.rev m_l);
4255 m_l <- [];
4256 m_prev_mode <- prev_mode;
4257 m_prev_uioh <- prev_uioh;
4258 if m_first_time
4259 then (
4260 let rec loop n =
4261 if n >= Array.length m_a
4262 then ()
4263 else
4264 match m_a.(n) with
4265 | _, _, _, Action _ -> m_active <- n
4266 | _ -> loop (n+1)
4268 loop 0;
4269 m_first_time <- false;
4272 method int name get set =
4273 m_l <-
4274 (name, `int get, 1, Action (
4275 fun u ->
4276 let ondone s =
4277 try set (int_of_string s)
4278 with exn ->
4279 state.text <- Printf.sprintf "bad integer `%s': %s"
4280 s (exntos exn)
4282 state.text <- "";
4283 let te = name ^ ": ", "", None, intentry, ondone, true in
4284 state.mode <- Textentry (te, leave m_prev_mode);
4286 )) :: m_l
4288 method int_with_suffix name get set =
4289 m_l <-
4290 (name, `intws get, 1, Action (
4291 fun u ->
4292 let ondone s =
4293 try set (int_of_string_with_suffix s)
4294 with exn ->
4295 state.text <- Printf.sprintf "bad integer `%s': %s"
4296 s (exntos exn)
4298 state.text <- "";
4299 let te =
4300 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4302 state.mode <- Textentry (te, leave m_prev_mode);
4304 )) :: m_l
4306 method bool ?(offset=1) ?(btos=btos) name get set =
4307 m_l <-
4308 (name, `bool (btos, get), offset, Action (
4309 fun u ->
4310 let v = get () in
4311 set (not v);
4313 )) :: m_l
4315 method color name get set =
4316 m_l <-
4317 (name, `color get, 1, Action (
4318 fun u ->
4319 let invalid = (nan, nan, nan) in
4320 let ondone s =
4321 let c =
4322 try color_of_string s
4323 with exn ->
4324 state.text <- Printf.sprintf "bad color `%s': %s"
4325 s (exntos exn);
4326 invalid
4328 if c <> invalid
4329 then set c;
4331 let te = name ^ ": ", "", None, textentry, ondone, true in
4332 state.text <- color_to_string (get ());
4333 state.mode <- Textentry (te, leave m_prev_mode);
4335 )) :: m_l
4337 method string name get set =
4338 m_l <-
4339 (name, `string get, 1, Action (
4340 fun u ->
4341 let ondone s = set s in
4342 let te = name ^ ": ", "", None, textentry, ondone, true in
4343 state.mode <- Textentry (te, leave m_prev_mode);
4345 )) :: m_l
4347 method colorspace name get set =
4348 m_l <-
4349 (name, `string get, 1, Action (
4350 fun _ ->
4351 let source =
4352 (object
4353 inherit lvsourcebase
4355 initializer
4356 m_active <- CSTE.to_int conf.colorspace;
4357 m_first <- 0;
4359 method getitemcount =
4360 Array.length CSTE.names
4361 method getitem n =
4362 (CSTE.names.(n), 0)
4363 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4364 ignore (uioh, first, pan, qsearch);
4365 if not cancel then set active;
4366 None
4367 method hasaction _ = true
4368 end)
4370 state.text <- "";
4371 let modehash = findkeyhash conf "info" in
4372 coe (new listview ~source ~trusted:true ~modehash)
4373 )) :: m_l
4375 method paxmark name get set =
4376 m_l <-
4377 (name, `string get, 1, Action (
4378 fun _ ->
4379 let source =
4380 (object
4381 inherit lvsourcebase
4383 initializer
4384 m_active <- MTE.to_int conf.paxmark;
4385 m_first <- 0;
4387 method getitemcount = Array.length MTE.names
4388 method getitem n = (MTE.names.(n), 0)
4389 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4390 ignore (uioh, first, pan, qsearch);
4391 if not cancel then set active;
4392 None
4393 method hasaction _ = true
4394 end)
4396 state.text <- "";
4397 let modehash = findkeyhash conf "info" in
4398 coe (new listview ~source ~trusted:true ~modehash)
4399 )) :: m_l
4401 method fitmodel name get set =
4402 m_l <-
4403 (name, `string get, 1, Action (
4404 fun _ ->
4405 let source =
4406 (object
4407 inherit lvsourcebase
4409 initializer
4410 m_active <- FMTE.to_int conf.fitmodel;
4411 m_first <- 0;
4413 method getitemcount = Array.length FMTE.names
4414 method getitem n = (FMTE.names.(n), 0)
4415 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4416 ignore (uioh, first, pan, qsearch);
4417 if not cancel then set active;
4418 None
4419 method hasaction _ = true
4420 end)
4422 state.text <- "";
4423 let modehash = findkeyhash conf "info" in
4424 coe (new listview ~source ~trusted:true ~modehash)
4425 )) :: m_l
4427 method caption s offset =
4428 m_l <- (s, `empty, offset, Noaction) :: m_l
4430 method caption2 s f offset =
4431 m_l <- (s, `string f, offset, Noaction) :: m_l
4433 method getitemcount = Array.length m_a
4435 method getitem n =
4436 let tostr = function
4437 | `int f -> string_of_int (f ())
4438 | `intws f -> string_with_suffix_of_int (f ())
4439 | `string f -> f ()
4440 | `color f -> color_to_string (f ())
4441 | `bool (btos, f) -> btos (f ())
4442 | `empty -> ""
4444 let name, t, offset, _ = m_a.(n) in
4445 ((let s = tostr t in
4446 if String.length s > 0
4447 then Printf.sprintf "%s\t%s" name s
4448 else name),
4449 offset)
4451 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4452 let uiohopt =
4453 if not cancel
4454 then (
4455 m_qsearch <- qsearch;
4456 let uioh =
4457 match m_a.(active) with
4458 | _, _, _, Action f -> f uioh
4459 | _ -> uioh
4461 Some uioh
4463 else None
4465 m_active <- active;
4466 m_first <- first;
4467 m_pan <- pan;
4468 uiohopt
4470 method hasaction n =
4471 match m_a.(n) with
4472 | _, _, _, Action _ -> true
4473 | _ -> false
4474 end)
4476 let rec fillsrc prevmode prevuioh =
4477 let sep () = src#caption "" 0 in
4478 let colorp name get set =
4479 src#string name
4480 (fun () -> color_to_string (get ()))
4481 (fun v ->
4483 let c = color_of_string v in
4484 set c
4485 with exn ->
4486 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4489 let oldmode = state.mode in
4490 let birdseye = isbirdseye state.mode in
4492 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4494 src#bool "presentation mode"
4495 (fun () -> conf.presentation)
4496 (fun v -> setpresentationmode v);
4498 src#bool "ignore case in searches"
4499 (fun () -> conf.icase)
4500 (fun v -> conf.icase <- v);
4502 src#bool "preload"
4503 (fun () -> conf.preload)
4504 (fun v -> conf.preload <- v);
4506 src#bool "highlight links"
4507 (fun () -> conf.hlinks)
4508 (fun v -> conf.hlinks <- v);
4510 src#bool "under info"
4511 (fun () -> conf.underinfo)
4512 (fun v -> conf.underinfo <- v);
4514 src#bool "persistent bookmarks"
4515 (fun () -> conf.savebmarks)
4516 (fun v -> conf.savebmarks <- v);
4518 src#fitmodel "fit model"
4519 (fun () -> FMTE.to_string conf.fitmodel)
4520 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4522 src#bool "trim margins"
4523 (fun () -> conf.trimmargins)
4524 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4526 src#bool "persistent location"
4527 (fun () -> conf.jumpback)
4528 (fun v -> conf.jumpback <- v);
4530 sep ();
4531 src#int "inter-page space"
4532 (fun () -> conf.interpagespace)
4533 (fun n ->
4534 conf.interpagespace <- n;
4535 docolumns conf.columns;
4536 let pageno, py =
4537 match state.layout with
4538 | [] -> 0, 0
4539 | l :: _ ->
4540 l.pageno, l.pagey
4542 state.maxy <- calcheight ();
4543 let y = getpagey pageno in
4544 gotoy (y + py)
4547 src#int "page bias"
4548 (fun () -> conf.pagebias)
4549 (fun v -> conf.pagebias <- v);
4551 src#int "scroll step"
4552 (fun () -> conf.scrollstep)
4553 (fun n -> conf.scrollstep <- n);
4555 src#int "horizontal scroll step"
4556 (fun () -> conf.hscrollstep)
4557 (fun v -> conf.hscrollstep <- v);
4559 src#int "auto scroll step"
4560 (fun () ->
4561 match state.autoscroll with
4562 | Some step -> step
4563 | _ -> conf.autoscrollstep)
4564 (fun n ->
4565 if state.autoscroll <> None
4566 then state.autoscroll <- Some n;
4567 conf.autoscrollstep <- n);
4569 src#int "zoom"
4570 (fun () -> truncate (conf.zoom *. 100.))
4571 (fun v -> setzoom ((float v) /. 100.));
4573 src#int "rotation"
4574 (fun () -> conf.angle)
4575 (fun v -> reqlayout v conf.fitmodel);
4577 src#int "scroll bar width"
4578 (fun () -> conf.scrollbw)
4579 (fun v ->
4580 conf.scrollbw <- v;
4581 reshape state.winw state.winh;
4584 src#int "scroll handle height"
4585 (fun () -> conf.scrollh)
4586 (fun v -> conf.scrollh <- v;);
4588 src#int "thumbnail width"
4589 (fun () -> conf.thumbw)
4590 (fun v ->
4591 conf.thumbw <- min 4096 v;
4592 match oldmode with
4593 | Birdseye beye ->
4594 leavebirdseye beye false;
4595 enterbirdseye ()
4596 | _ -> ()
4599 let mode = state.mode in
4600 src#string "columns"
4601 (fun () ->
4602 match conf.columns with
4603 | Csingle _ -> "1"
4604 | Cmulti (multi, _) -> multicolumns_to_string multi
4605 | Csplit (count, _) -> "-" ^ string_of_int count
4607 (fun v ->
4608 let n, a, b = multicolumns_of_string v in
4609 setcolumns mode n a b);
4611 sep ();
4612 src#caption "Pixmap cache" 0;
4613 src#int_with_suffix "size (advisory)"
4614 (fun () -> conf.memlimit)
4615 (fun v -> conf.memlimit <- v);
4617 src#caption2 "used"
4618 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4619 (string_with_suffix_of_int state.memused)
4620 (Hashtbl.length state.tilemap)) 1;
4622 sep ();
4623 src#caption "Layout" 0;
4624 src#caption2 "Dimension"
4625 (fun () ->
4626 Printf.sprintf "%dx%d (virtual %dx%d)"
4627 state.winw state.winh
4628 state.w state.maxy)
4630 if conf.debug
4631 then
4632 src#caption2 "Position" (fun () ->
4633 Printf.sprintf "%dx%d" state.x state.y
4635 else
4636 src#caption2 "Position" (fun () -> describe_location ()) 1
4639 sep ();
4640 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4641 "Save these parameters as global defaults at exit"
4642 (fun () -> conf.bedefault)
4643 (fun v -> conf.bedefault <- v)
4646 sep ();
4647 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4648 src#bool ~offset:0 ~btos "Extended parameters"
4649 (fun () -> !showextended)
4650 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4651 if !showextended
4652 then (
4653 src#bool "checkers"
4654 (fun () -> conf.checkers)
4655 (fun v -> conf.checkers <- v; setcheckers v);
4656 src#bool "update cursor"
4657 (fun () -> conf.updatecurs)
4658 (fun v -> conf.updatecurs <- v);
4659 src#bool "verbose"
4660 (fun () -> conf.verbose)
4661 (fun v -> conf.verbose <- v);
4662 src#bool "invert colors"
4663 (fun () -> conf.invert)
4664 (fun v -> conf.invert <- v);
4665 src#bool "max fit"
4666 (fun () -> conf.maxhfit)
4667 (fun v -> conf.maxhfit <- v);
4668 src#bool "redirect stderr"
4669 (fun () -> conf.redirectstderr)
4670 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4671 src#bool "pax mode"
4672 (fun () -> conf.pax != None)
4673 (fun v ->
4674 if v
4675 then conf.pax <- Some (ref (now (), 0, 0))
4676 else conf.pax <- None);
4677 src#string "uri launcher"
4678 (fun () -> conf.urilauncher)
4679 (fun v -> conf.urilauncher <- v);
4680 src#string "path launcher"
4681 (fun () -> conf.pathlauncher)
4682 (fun v -> conf.pathlauncher <- v);
4683 src#string "tile size"
4684 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4685 (fun v ->
4687 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4688 conf.tilew <- max 64 w;
4689 conf.tileh <- max 64 h;
4690 flushtiles ();
4691 with exn ->
4692 state.text <- Printf.sprintf "bad tile size `%s': %s"
4693 v (exntos exn)
4695 src#int "texture count"
4696 (fun () -> conf.texcount)
4697 (fun v ->
4698 if realloctexts v
4699 then conf.texcount <- v
4700 else showtext '!' " Failed to set texture count please retry later"
4702 src#int "slice height"
4703 (fun () -> conf.sliceheight)
4704 (fun v ->
4705 conf.sliceheight <- v;
4706 wcmd "sliceh %d" conf.sliceheight;
4708 src#int "anti-aliasing level"
4709 (fun () -> conf.aalevel)
4710 (fun v ->
4711 conf.aalevel <- bound v 0 8;
4712 state.anchor <- getanchor ();
4713 opendoc state.path state.password;
4715 src#string "page scroll scaling factor"
4716 (fun () -> string_of_float conf.pgscale)
4717 (fun v ->
4719 let s = float_of_string v in
4720 conf.pgscale <- s
4721 with exn ->
4722 state.text <- Printf.sprintf
4723 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4726 src#int "ui font size"
4727 (fun () -> fstate.fontsize)
4728 (fun v -> setfontsize (bound v 5 100));
4729 src#int "hint font size"
4730 (fun () -> conf.hfsize)
4731 (fun v -> conf.hfsize <- bound v 5 100);
4732 colorp "background color"
4733 (fun () -> conf.bgcolor)
4734 (fun v -> conf.bgcolor <- v);
4735 src#bool "crop hack"
4736 (fun () -> conf.crophack)
4737 (fun v -> conf.crophack <- v);
4738 src#string "trim fuzz"
4739 (fun () -> irect_to_string conf.trimfuzz)
4740 (fun v ->
4742 conf.trimfuzz <- irect_of_string v;
4743 if conf.trimmargins
4744 then settrim true conf.trimfuzz;
4745 with exn ->
4746 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4748 src#string "throttle"
4749 (fun () ->
4750 match conf.maxwait with
4751 | None -> "show place holder if page is not ready"
4752 | Some time ->
4753 if time = infinity
4754 then "wait for page to fully render"
4755 else
4756 "wait " ^ string_of_float time
4757 ^ " seconds before showing placeholder"
4759 (fun v ->
4761 let f = float_of_string v in
4762 if f <= 0.0
4763 then conf.maxwait <- None
4764 else conf.maxwait <- Some f
4765 with exn ->
4766 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4768 src#string "ghyll scroll"
4769 (fun () ->
4770 match conf.ghyllscroll with
4771 | None -> ""
4772 | Some nab -> ghyllscroll_to_string nab
4774 (fun v ->
4776 let gs =
4777 if String.length v = 0
4778 then None
4779 else Some (ghyllscroll_of_string v)
4781 conf.ghyllscroll <- gs
4782 with exn ->
4783 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4785 src#string "selection command"
4786 (fun () -> conf.selcmd)
4787 (fun v -> conf.selcmd <- v);
4788 src#string "synctex command"
4789 (fun () -> conf.stcmd)
4790 (fun v -> conf.stcmd <- v);
4791 src#string "pax command"
4792 (fun () -> conf.paxcmd)
4793 (fun v -> conf.paxcmd <- v);
4794 src#colorspace "color space"
4795 (fun () -> CSTE.to_string conf.colorspace)
4796 (fun v ->
4797 conf.colorspace <- CSTE.of_int v;
4798 wcmd "cs %d" v;
4799 load state.layout;
4801 src#paxmark "pax mark method"
4802 (fun () -> MTE.to_string conf.paxmark)
4803 (fun v -> conf.paxmark <- MTE.of_int v);
4804 if pbousable ()
4805 then
4806 src#bool "use PBO"
4807 (fun () -> conf.usepbo)
4808 (fun v -> conf.usepbo <- v);
4809 src#bool "mouse wheel scrolls pages"
4810 (fun () -> conf.wheelbypage)
4811 (fun v -> conf.wheelbypage <- v);
4812 src#bool "open remote links in a new instance"
4813 (fun () -> conf.riani)
4814 (fun v -> conf.riani <- v);
4817 sep ();
4818 src#caption "Document" 0;
4819 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4820 src#caption2 "Pages"
4821 (fun () -> string_of_int state.pagecount) 1;
4822 src#caption2 "Dimensions"
4823 (fun () -> string_of_int (List.length state.pdims)) 1;
4824 if conf.trimmargins
4825 then (
4826 sep ();
4827 src#caption "Trimmed margins" 0;
4828 src#caption2 "Dimensions"
4829 (fun () -> string_of_int (List.length state.pdims)) 1;
4832 sep ();
4833 src#caption "OpenGL" 0;
4834 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4835 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4837 sep ();
4838 src#caption "Location" 0;
4839 if String.length state.origin > 0
4840 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4841 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4843 src#reset prevmode prevuioh;
4845 fun () ->
4846 state.text <- "";
4847 let prevmode = state.mode
4848 and prevuioh = state.uioh in
4849 fillsrc prevmode prevuioh;
4850 let source = (src :> lvsource) in
4851 let modehash = findkeyhash conf "info" in
4852 state.uioh <- coe (object (self)
4853 inherit listview ~source ~trusted:true ~modehash as super
4854 val mutable m_prevmemused = 0
4855 method infochanged = function
4856 | Memused ->
4857 if m_prevmemused != state.memused
4858 then (
4859 m_prevmemused <- state.memused;
4860 G.postRedisplay "memusedchanged";
4862 | Pdim -> G.postRedisplay "pdimchanged"
4863 | Docinfo -> fillsrc prevmode prevuioh
4865 method key key mask =
4866 if not (Wsi.withctrl mask)
4867 then
4868 match key with
4869 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4870 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4871 | _ -> super#key key mask
4872 else super#key key mask
4873 end);
4874 G.postRedisplay "info";
4877 let enterhelpmode =
4878 let source =
4879 (object
4880 inherit lvsourcebase
4881 method getitemcount = Array.length state.help
4882 method getitem n =
4883 let s, l, _ = state.help.(n) in
4884 (s, l)
4886 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4887 let optuioh =
4888 if not cancel
4889 then (
4890 m_qsearch <- qsearch;
4891 match state.help.(active) with
4892 | _, _, Action f -> Some (f uioh)
4893 | _ -> Some (uioh)
4895 else None
4897 m_active <- active;
4898 m_first <- first;
4899 m_pan <- pan;
4900 optuioh
4902 method hasaction n =
4903 match state.help.(n) with
4904 | _, _, Action _ -> true
4905 | _ -> false
4907 initializer
4908 m_active <- -1
4909 end)
4910 in fun () ->
4911 let modehash = findkeyhash conf "help" in
4912 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4913 G.postRedisplay "help";
4916 let entermsgsmode =
4917 let msgsource =
4918 let re = Str.regexp "[\r\n]" in
4919 (object
4920 inherit lvsourcebase
4921 val mutable m_items = [||]
4923 method getitemcount = 1 + Array.length m_items
4925 method getitem n =
4926 if n = 0
4927 then "[Clear]", 0
4928 else m_items.(n-1), 0
4930 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4931 ignore uioh;
4932 if not cancel
4933 then (
4934 if active = 0
4935 then Buffer.clear state.errmsgs;
4936 m_qsearch <- qsearch;
4938 m_active <- active;
4939 m_first <- first;
4940 m_pan <- pan;
4941 None
4943 method hasaction n =
4944 n = 0
4946 method reset =
4947 state.newerrmsgs <- false;
4948 let l = Str.split re (Buffer.contents state.errmsgs) in
4949 m_items <- Array.of_list l
4951 initializer
4952 m_active <- 0
4953 end)
4954 in fun () ->
4955 state.text <- "";
4956 msgsource#reset;
4957 let source = (msgsource :> lvsource) in
4958 let modehash = findkeyhash conf "listview" in
4959 state.uioh <- coe (object
4960 inherit listview ~source ~trusted:false ~modehash as super
4961 method display =
4962 if state.newerrmsgs
4963 then msgsource#reset;
4964 super#display
4965 end);
4966 G.postRedisplay "msgs";
4969 let quickbookmark ?title () =
4970 match state.layout with
4971 | [] -> ()
4972 | l :: _ ->
4973 let title =
4974 match title with
4975 | None ->
4976 let sec = Unix.gettimeofday () in
4977 let tm = Unix.localtime sec in
4978 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4979 (l.pageno+1)
4980 tm.Unix.tm_mday
4981 tm.Unix.tm_mon
4982 (tm.Unix.tm_year + 1900)
4983 tm.Unix.tm_hour
4984 tm.Unix.tm_min
4985 | Some title -> title
4987 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4990 let setautoscrollspeed step goingdown =
4991 let incr = max 1 ((abs step) / 2) in
4992 let incr = if goingdown then incr else -incr in
4993 let astep = step + incr in
4994 state.autoscroll <- Some astep;
4997 let gotounder = function
4998 | Ulinkgoto (pageno, top) ->
4999 if pageno >= 0
5000 then (
5001 addnav ();
5002 gotopage1 pageno top;
5005 | Ulinkuri s ->
5006 gotouri s
5008 | Uremote (filename, pageno) ->
5009 let path =
5010 if String.length filename > 0
5011 then
5012 if Filename.is_relative filename
5013 then
5014 let dir = Filename.dirname state.path in
5015 let dir =
5016 if Filename.is_implicit dir
5017 then Filename.concat (Sys.getcwd ()) dir
5018 else dir
5020 Filename.concat dir filename
5021 else filename
5022 else ""
5024 let path =
5025 if Sys.file_exists path
5026 then path
5027 else ""
5029 if String.length path > 0
5030 then (
5031 if conf.riani
5032 then
5033 let command = !selfexec ^ " " ^ path in
5034 try popen command []
5035 with exn ->
5036 Printf.eprintf
5037 "failed to execute `%s': %s\n" command (exntos exn);
5038 flush stderr;
5039 else
5040 let anchor = getanchor () in
5041 let ranchor = state.path, state.password, anchor, state.origin in
5042 state.origin <- "";
5043 state.anchor <- (pageno, 0.0, 0.0);
5044 state.ranchors <- ranchor :: state.ranchors;
5045 opendoc path "";
5047 else showtext '!' ("Could not find " ^ filename)
5049 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5052 let canpan () =
5053 match conf.columns with
5054 | Csplit _ -> true
5055 | _ -> state.x != 0 || conf.zoom > 1.0
5058 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5060 let existsinrow pageno (columns, coverA, coverB) p =
5061 let last = ((pageno - coverA) mod columns) + columns in
5062 let rec any = function
5063 | [] -> false
5064 | l :: rest ->
5065 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5066 then p l
5067 else (
5068 if not (p l)
5069 then (if l.pageno = last then false else any rest)
5070 else true
5073 any state.layout
5076 let nextpage () =
5077 match state.layout with
5078 | [] ->
5079 let pageno = page_of_y state.y in
5080 gotoghyll (getpagey (pageno+1))
5081 | l :: rest ->
5082 match conf.columns with
5083 | Csingle _ ->
5084 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5085 then
5086 let y = clamp (pgscale state.winh) in
5087 gotoghyll y
5088 else
5089 let pageno = min (l.pageno+1) (state.pagecount-1) in
5090 gotoghyll (getpagey pageno)
5091 | Cmulti ((c, _, _) as cl, _) ->
5092 if conf.presentation
5093 && (existsinrow l.pageno cl
5094 (fun l -> l.pageh > l.pagey + l.pagevh))
5095 then
5096 let y = clamp (pgscale state.winh) in
5097 gotoghyll y
5098 else
5099 let pageno = min (l.pageno+c) (state.pagecount-1) in
5100 gotoghyll (getpagey pageno)
5101 | Csplit (n, _) ->
5102 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5103 then
5104 let pagey, pageh = getpageyh l.pageno in
5105 let pagey = pagey + pageh * l.pagecol in
5106 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5107 gotoghyll (pagey + pageh + ips)
5110 let prevpage () =
5111 match state.layout with
5112 | [] ->
5113 let pageno = page_of_y state.y in
5114 gotoghyll (getpagey (pageno-1))
5115 | l :: _ ->
5116 match conf.columns with
5117 | Csingle _ ->
5118 if conf.presentation && l.pagey != 0
5119 then
5120 gotoghyll (clamp (pgscale ~-(state.winh)))
5121 else
5122 let pageno = max 0 (l.pageno-1) in
5123 gotoghyll (getpagey pageno)
5124 | Cmulti ((c, _, coverB) as cl, _) ->
5125 if conf.presentation &&
5126 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5127 then
5128 gotoghyll (clamp (pgscale ~-(state.winh)))
5129 else
5130 let decr =
5131 if l.pageno = state.pagecount - coverB
5132 then 1
5133 else c
5135 let pageno = max 0 (l.pageno-decr) in
5136 gotoghyll (getpagey pageno)
5137 | Csplit (n, _) ->
5138 let y =
5139 if l.pagecol = 0
5140 then
5141 if l.pageno = 0
5142 then l.pagey
5143 else
5144 let pageno = max 0 (l.pageno-1) in
5145 let pagey, pageh = getpageyh pageno in
5146 pagey + (n-1)*pageh
5147 else
5148 let pagey, pageh = getpageyh l.pageno in
5149 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5151 gotoghyll y
5154 let viewkeyboard key mask =
5155 let enttext te =
5156 let mode = state.mode in
5157 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5158 state.text <- "";
5159 enttext ();
5160 G.postRedisplay "view:enttext"
5162 let ctrl = Wsi.withctrl mask in
5163 let key =
5164 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5166 match key with
5167 | 81 -> (* Q *)
5168 exit 0
5170 | 0xff63 -> (* insert *)
5171 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5172 then (
5173 state.mode <- LinkNav (Ltgendir 0);
5174 gotoy state.y;
5176 else showtext '!' "Keyboard link navigation does not work under rotation"
5178 | 0xff1b | 113 -> (* escape / q *)
5179 begin match state.mstate with
5180 | Mzoomrect _ ->
5181 state.mstate <- Mnone;
5182 Wsi.setcursor Wsi.CURSOR_INHERIT;
5183 G.postRedisplay "kill zoom rect";
5184 | _ ->
5185 begin match state.mode with
5186 | LinkNav _ ->
5187 state.mode <- View;
5188 G.postRedisplay "esc leave linknav"
5189 | _ ->
5190 match state.ranchors with
5191 | [] -> raise Quit
5192 | (path, password, anchor, origin) :: rest ->
5193 state.ranchors <- rest;
5194 state.anchor <- anchor;
5195 state.origin <- origin;
5196 opendoc path password
5197 end;
5198 end;
5200 | 0xff08 -> (* backspace *)
5201 gotoghyll (getnav ~-1)
5203 | 111 -> (* o *)
5204 enteroutlinemode ()
5206 | 117 -> (* u *)
5207 state.rects <- [];
5208 state.text <- "";
5209 G.postRedisplay "dehighlight";
5211 | 47 | 63 -> (* / ? *)
5212 let ondone isforw s =
5213 cbput state.hists.pat s;
5214 state.searchpattern <- s;
5215 search s isforw
5217 let s = String.create 1 in
5218 s.[0] <- Char.chr key;
5219 enttext (s, "", Some (onhist state.hists.pat),
5220 textentry, ondone (key = 47), true)
5222 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5223 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5224 setzoom (conf.zoom +. incr)
5226 | 43 | 0xffab -> (* + *)
5227 let ondone s =
5228 let n =
5229 try int_of_string s with exc ->
5230 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5231 max_int
5233 if n != max_int
5234 then (
5235 conf.pagebias <- n;
5236 state.text <- "page bias is now " ^ string_of_int n;
5239 enttext ("page bias: ", "", None, intentry, ondone, true)
5241 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5242 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5243 setzoom (max 0.01 (conf.zoom -. decr))
5245 | 45 | 0xffad -> (* - *)
5246 let ondone msg = state.text <- msg in
5247 enttext (
5248 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5249 optentry state.mode, ondone, true
5252 | 48 when ctrl -> (* ctrl-0 *)
5253 if conf.zoom = 1.0
5254 then (
5255 state.x <- 0;
5256 gotoy state.y
5258 else setzoom 1.0
5260 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5261 let cols =
5262 match conf.columns with
5263 | Csingle _ | Cmulti _ -> 1
5264 | Csplit (n, _) -> n
5266 let h = state.winh -
5267 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5269 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5270 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5271 then setzoom zoom
5273 | 51 when ctrl -> (* ctrl-3 *)
5274 let fm =
5275 match conf.fitmodel with
5276 | FitWidth -> FitProportional
5277 | FitProportional -> FitPage
5278 | FitPage -> FitWidth
5280 state.text <- "fit model: " ^ FMTE.to_string fm;
5281 reqlayout conf.angle fm
5283 | 0xffc6 -> (* f9 *)
5284 togglebirdseye ()
5286 | 57 when ctrl -> (* ctrl-9 *)
5287 togglebirdseye ()
5289 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5290 when not ctrl -> (* 0..9 *)
5291 let ondone s =
5292 let n =
5293 try int_of_string s with exc ->
5294 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5297 if n >= 0
5298 then (
5299 addnav ();
5300 cbput state.hists.pag (string_of_int n);
5301 gotopage1 (n + conf.pagebias - 1) 0;
5304 let pageentry text key =
5305 match Char.unsafe_chr key with
5306 | 'g' -> TEdone text
5307 | _ -> intentry text key
5309 let text = "x" in text.[0] <- Char.chr key;
5310 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5312 | 98 -> (* b *)
5313 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5314 reshape state.winw state.winh;
5316 | 108 -> (* l *)
5317 conf.hlinks <- not conf.hlinks;
5318 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5319 G.postRedisplay "toggle highlightlinks";
5321 | 70 -> (* F *)
5322 state.glinks <- true;
5323 let mode = state.mode in
5324 state.mode <- Textentry (
5325 (":", "", None, linknentry, linkndone gotounder, false),
5326 (fun _ ->
5327 state.glinks <- false;
5328 state.mode <- mode)
5330 state.text <- "";
5331 G.postRedisplay "view:linkent(F)"
5333 | 121 -> (* y *)
5334 state.glinks <- true;
5335 let mode = state.mode in
5336 state.mode <- Textentry (
5338 ":", "", None, linknentry, linkndone (fun under ->
5339 selstring (undertext under);
5340 ), false
5342 fun _ ->
5343 state.glinks <- false;
5344 state.mode <- mode
5346 state.text <- "";
5347 G.postRedisplay "view:linkent"
5349 | 97 -> (* a *)
5350 begin match state.autoscroll with
5351 | Some step ->
5352 conf.autoscrollstep <- step;
5353 state.autoscroll <- None
5354 | None ->
5355 if conf.autoscrollstep = 0
5356 then state.autoscroll <- Some 1
5357 else state.autoscroll <- Some conf.autoscrollstep
5360 | 112 when ctrl -> (* ctrl-p *)
5361 launchpath ()
5363 | 80 -> (* P *)
5364 setpresentationmode (not conf.presentation);
5365 showtext ' ' ("presentation mode " ^
5366 if conf.presentation then "on" else "off");
5368 | 102 -> (* f *)
5369 if List.mem Wsi.Fullscreen state.winstate
5370 then Wsi.reshape conf.cwinw conf.cwinh
5371 else Wsi.fullscreen ()
5373 | 112 | 78 -> (* p|N *)
5374 search state.searchpattern false
5376 | 110 | 0xffc0 -> (* n|F3 *)
5377 search state.searchpattern true
5379 | 116 -> (* t *)
5380 begin match state.layout with
5381 | [] -> ()
5382 | l :: _ ->
5383 gotoghyll (getpagey l.pageno)
5386 | 32 -> (* space *)
5387 nextpage ()
5389 | 0xff9f | 0xffff -> (* delete *)
5390 prevpage ()
5392 | 61 -> (* = *)
5393 showtext ' ' (describe_location ());
5395 | 119 -> (* w *)
5396 begin match state.layout with
5397 | [] -> ()
5398 | l :: _ ->
5399 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5400 G.postRedisplay "w"
5403 | 39 -> (* ' *)
5404 enterbookmarkmode ()
5406 | 104 | 0xffbe -> (* h|F1 *)
5407 enterhelpmode ()
5409 | 105 -> (* i *)
5410 enterinfomode ()
5412 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5413 entermsgsmode ()
5415 | 109 -> (* m *)
5416 let ondone s =
5417 match state.layout with
5418 | l :: _ ->
5419 if String.length s > 0
5420 then
5421 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5422 | _ -> ()
5424 enttext ("bookmark: ", "", None, textentry, ondone, true)
5426 | 126 -> (* ~ *)
5427 quickbookmark ();
5428 showtext ' ' "Quick bookmark added";
5430 | 122 -> (* z *)
5431 begin match state.layout with
5432 | l :: _ ->
5433 let rect = getpdimrect l.pagedimno in
5434 let w, h =
5435 if conf.crophack
5436 then
5437 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5438 truncate (1.2 *. (rect.(3) -. rect.(0))))
5439 else
5440 (truncate (rect.(1) -. rect.(0)),
5441 truncate (rect.(3) -. rect.(0)))
5443 let w = truncate ((float w)*.conf.zoom)
5444 and h = truncate ((float h)*.conf.zoom) in
5445 if w != 0 && h != 0
5446 then (
5447 state.anchor <- getanchor ();
5448 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5450 G.postRedisplay "z";
5452 | [] -> ()
5455 | 120 -> state.roam ()
5456 | 60 | 62 -> (* < > *)
5457 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5459 | 91 | 93 -> (* [ ] *)
5460 conf.colorscale <-
5461 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5463 G.postRedisplay "brightness";
5465 | 99 when state.mode = View -> (* [alt-]c *)
5466 if Wsi.withalt mask
5467 then (
5468 if conf.zoom > 1.0
5469 then
5470 let m = (wadjsb state.winw - state.w) / 2 in
5471 state.x <- m;
5472 gotoy_and_clear_text state.y
5474 else
5475 let (c, a, b), z =
5476 match state.prevcolumns with
5477 | None -> (1, 0, 0), 1.0
5478 | Some (columns, z) ->
5479 let cab =
5480 match columns with
5481 | Csplit (c, _) -> -c, 0, 0
5482 | Cmulti ((c, a, b), _) -> c, a, b
5483 | Csingle _ -> 1, 0, 0
5485 cab, z
5487 setcolumns View c a b;
5488 setzoom z
5490 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5491 setzoom state.prevzoom
5493 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5494 begin match state.autoscroll with
5495 | None ->
5496 begin match state.mode with
5497 | Birdseye beye -> upbirdseye 1 beye
5498 | _ ->
5499 if ctrl
5500 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5501 else (
5502 if not (Wsi.withshift mask) && conf.presentation
5503 then prevpage ()
5504 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5507 | Some n ->
5508 setautoscrollspeed n false
5511 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5512 begin match state.autoscroll with
5513 | None ->
5514 begin match state.mode with
5515 | Birdseye beye -> downbirdseye 1 beye
5516 | _ ->
5517 if ctrl
5518 then gotoy_and_clear_text (clamp (state.winh/2))
5519 else (
5520 if not (Wsi.withshift mask) && conf.presentation
5521 then nextpage ()
5522 else gotoy_and_clear_text (clamp conf.scrollstep)
5525 | Some n ->
5526 setautoscrollspeed n true
5529 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5530 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5531 if canpan ()
5532 then
5533 let dx =
5534 if ctrl
5535 then state.winw / 2
5536 else conf.hscrollstep
5538 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5539 state.x <- panbound (state.x + dx);
5540 gotoy_and_clear_text state.y
5541 else (
5542 state.text <- "";
5543 G.postRedisplay "left/right"
5546 | 0xff55 | 0xff9a -> (* (kp) prior *)
5547 let y =
5548 if ctrl
5549 then
5550 match state.layout with
5551 | [] -> state.y
5552 | l :: _ -> state.y - l.pagey
5553 else
5554 clamp (pgscale (-state.winh))
5556 gotoghyll y
5558 | 0xff56 | 0xff9b -> (* (kp) next *)
5559 let y =
5560 if ctrl
5561 then
5562 match List.rev state.layout with
5563 | [] -> state.y
5564 | l :: _ -> getpagey l.pageno
5565 else
5566 clamp (pgscale state.winh)
5568 gotoghyll y
5570 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5571 gotoghyll 0
5572 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5573 gotoghyll (clamp state.maxy)
5575 | 0xff53 | 0xff98
5576 when Wsi.withalt mask -> (* alt-(kp) right *)
5577 gotoghyll (getnav 1)
5578 | 0xff51 | 0xff96
5579 when Wsi.withalt mask -> (* alt-(kp) left *)
5580 gotoghyll (getnav ~-1)
5582 | 114 -> (* r *)
5583 reload ()
5585 | 118 when conf.debug -> (* v *)
5586 state.rects <- [];
5587 List.iter (fun l ->
5588 match getopaque l.pageno with
5589 | None -> ()
5590 | Some opaque ->
5591 let x0, y0, x1, y1 = pagebbox opaque in
5592 let a,b = float x0, float y0 in
5593 let c,d = float x1, float y0 in
5594 let e,f = float x1, float y1 in
5595 let h,j = float x0, float y1 in
5596 let rect = (a,b,c,d,e,f,h,j) in
5597 debugrect rect;
5598 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5599 ) state.layout;
5600 G.postRedisplay "v";
5602 | _ ->
5603 vlog "huh? %s" (Wsi.keyname key)
5606 let linknavkeyboard key mask linknav =
5607 let getpage pageno =
5608 let rec loop = function
5609 | [] -> None
5610 | l :: _ when l.pageno = pageno -> Some l
5611 | _ :: rest -> loop rest
5612 in loop state.layout
5614 let doexact (pageno, n) =
5615 match getopaque pageno, getpage pageno with
5616 | Some opaque, Some l ->
5617 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5618 then
5619 let under = getlink opaque n in
5620 G.postRedisplay "link gotounder";
5621 gotounder under;
5622 state.mode <- View;
5623 else
5624 let opt, dir =
5625 match key with
5626 | 0xff50 -> (* home *)
5627 Some (findlink opaque LDfirst), -1
5629 | 0xff57 -> (* end *)
5630 Some (findlink opaque LDlast), 1
5632 | 0xff51 -> (* left *)
5633 Some (findlink opaque (LDleft n)), -1
5635 | 0xff53 -> (* right *)
5636 Some (findlink opaque (LDright n)), 1
5638 | 0xff52 -> (* up *)
5639 Some (findlink opaque (LDup n)), -1
5641 | 0xff54 -> (* down *)
5642 Some (findlink opaque (LDdown n)), 1
5644 | _ -> None, 0
5646 let pwl l dir =
5647 begin match findpwl l.pageno dir with
5648 | Pwlnotfound -> ()
5649 | Pwl pageno ->
5650 let notfound dir =
5651 state.mode <- LinkNav (Ltgendir dir);
5652 let y, h = getpageyh pageno in
5653 let y =
5654 if dir < 0
5655 then y + h - state.winh
5656 else y
5658 gotoy y
5660 begin match getopaque pageno, getpage pageno with
5661 | Some opaque, Some _ ->
5662 let link =
5663 let ld = if dir > 0 then LDfirst else LDlast in
5664 findlink opaque ld
5666 begin match link with
5667 | Lfound m ->
5668 showlinktype (getlink opaque m);
5669 state.mode <- LinkNav (Ltexact (pageno, m));
5670 G.postRedisplay "linknav jpage";
5671 | _ -> notfound dir
5672 end;
5673 | _ -> notfound dir
5674 end;
5675 end;
5677 begin match opt with
5678 | Some Lnotfound -> pwl l dir;
5679 | Some (Lfound m) ->
5680 if m = n
5681 then pwl l dir
5682 else (
5683 let _, y0, _, y1 = getlinkrect opaque m in
5684 if y0 < l.pagey
5685 then gotopage1 l.pageno y0
5686 else (
5687 let d = fstate.fontsize + 1 in
5688 if y1 - l.pagey > l.pagevh - d
5689 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5690 else G.postRedisplay "linknav";
5692 showlinktype (getlink opaque m);
5693 state.mode <- LinkNav (Ltexact (l.pageno, m));
5696 | None -> viewkeyboard key mask
5697 end;
5698 | _ -> viewkeyboard key mask
5700 if key = 0xff63
5701 then (
5702 state.mode <- View;
5703 G.postRedisplay "leave linknav"
5705 else
5706 match linknav with
5707 | Ltgendir _ -> viewkeyboard key mask
5708 | Ltexact exact -> doexact exact
5711 let keyboard key mask =
5712 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5713 then wcmd "interrupt"
5714 else state.uioh <- state.uioh#key key mask
5717 let birdseyekeyboard key mask
5718 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5719 let incr =
5720 match conf.columns with
5721 | Csingle _ -> 1
5722 | Cmulti ((c, _, _), _) -> c
5723 | Csplit _ -> failwith "bird's eye split mode"
5725 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5726 match key with
5727 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5728 let y, h = getpageyh pageno in
5729 let top = (state.winh - h) / 2 in
5730 gotoy (max 0 (y - top))
5731 | 0xff0d (* enter *)
5732 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5733 | 0xff1b -> leavebirdseye beye true (* escape *)
5734 | 0xff52 -> upbirdseye incr beye (* up *)
5735 | 0xff54 -> downbirdseye incr beye (* down *)
5736 | 0xff51 -> upbirdseye 1 beye (* left *)
5737 | 0xff53 -> downbirdseye 1 beye (* right *)
5739 | 0xff55 -> (* prior *)
5740 begin match state.layout with
5741 | l :: _ ->
5742 if l.pagey != 0
5743 then (
5744 state.mode <- Birdseye (
5745 oconf, leftx, l.pageno, hooverpageno, anchor
5747 gotopage1 l.pageno 0;
5749 else (
5750 let layout = layout (state.y-state.winh) (pgh state.layout) in
5751 match layout with
5752 | [] -> gotoy (clamp (-state.winh))
5753 | l :: _ ->
5754 state.mode <- Birdseye (
5755 oconf, leftx, l.pageno, hooverpageno, anchor
5757 gotopage1 l.pageno 0
5760 | [] -> gotoy (clamp (-state.winh))
5761 end;
5763 | 0xff56 -> (* next *)
5764 begin match List.rev state.layout with
5765 | l :: _ ->
5766 let layout = layout (state.y + (pgh state.layout)) state.winh in
5767 begin match layout with
5768 | [] ->
5769 let incr = l.pageh - l.pagevh in
5770 if incr = 0
5771 then (
5772 state.mode <-
5773 Birdseye (
5774 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5776 G.postRedisplay "birdseye pagedown";
5778 else gotoy (clamp (incr + conf.interpagespace*2));
5780 | l :: _ ->
5781 state.mode <-
5782 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5783 gotopage1 l.pageno 0;
5786 | [] -> gotoy (clamp state.winh)
5787 end;
5789 | 0xff50 -> (* home *)
5790 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5791 gotopage1 0 0
5793 | 0xff57 -> (* end *)
5794 let pageno = state.pagecount - 1 in
5795 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5796 if not (pagevisible state.layout pageno)
5797 then
5798 let h =
5799 match List.rev state.pdims with
5800 | [] -> state.winh
5801 | (_, _, h, _) :: _ -> h
5803 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5804 else G.postRedisplay "birdseye end";
5805 | _ -> viewkeyboard key mask
5808 let drawpage l =
5809 let color =
5810 match state.mode with
5811 | Textentry _ -> scalecolor 0.4
5812 | LinkNav _
5813 | View -> scalecolor 1.0
5814 | Birdseye (_, _, pageno, hooverpageno, _) ->
5815 if l.pageno = hooverpageno
5816 then scalecolor 0.9
5817 else (
5818 if l.pageno = pageno
5819 then scalecolor 1.0
5820 else scalecolor 0.8
5823 drawtiles l color;
5826 let postdrawpage l linkindexbase =
5827 match getopaque l.pageno with
5828 | Some opaque ->
5829 if tileready l l.pagex l.pagey
5830 then
5831 let x = l.pagedispx - l.pagex
5832 and y = l.pagedispy - l.pagey in
5833 let hlmask =
5834 match conf.columns with
5835 | Csingle _ | Cmulti _ ->
5836 (if conf.hlinks then 1 else 0)
5837 + (if state.glinks
5838 && not (isbirdseye state.mode) then 2 else 0)
5839 | _ -> 0
5841 let s =
5842 match state.mode with
5843 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5844 | _ -> ""
5846 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5847 else 0
5848 | _ -> 0
5851 let scrollindicator () =
5852 let sbw, ph, sh = state.uioh#scrollph in
5853 let sbh, pw, sw = state.uioh#scrollpw in
5855 GlDraw.color (0.64, 0.64, 0.64);
5856 GlDraw.rect
5857 (float (state.winw - sbw), 0.)
5858 (float state.winw, float state.winh)
5860 GlDraw.rect
5861 (0., float (state.winh - sbh))
5862 (float (wadjsb state.winw - 1), float state.winh)
5864 GlDraw.color (0.0, 0.0, 0.0);
5866 GlDraw.rect
5867 (float (state.winw - sbw), ph)
5868 (float state.winw, ph +. sh)
5870 GlDraw.rect
5871 (pw, float (state.winh - sbh))
5872 (pw +. sw, float state.winh)
5876 let showsel () =
5877 match state.mstate with
5878 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5881 | Msel ((x0, y0), (x1, y1)) ->
5882 let rec loop = function
5883 | l :: ls ->
5884 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5885 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5886 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5887 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5888 then
5889 match getopaque l.pageno with
5890 | Some opaque ->
5891 let x0, y0 = pagetranslatepoint l x0 y0 in
5892 let x1, y1 = pagetranslatepoint l x1 y1 in
5893 seltext opaque (x0, y0, x1, y1);
5894 | _ -> ()
5895 else loop ls
5896 | [] -> ()
5898 loop state.layout
5901 let showrects rects =
5902 Gl.enable `blend;
5903 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5904 GlDraw.polygon_mode `both `fill;
5905 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5906 List.iter
5907 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5908 List.iter (fun l ->
5909 if l.pageno = pageno
5910 then (
5911 let dx = float (l.pagedispx - l.pagex) in
5912 let dy = float (l.pagedispy - l.pagey) in
5913 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5914 GlDraw.begins `quads;
5916 GlDraw.vertex2 (x0+.dx, y0+.dy);
5917 GlDraw.vertex2 (x1+.dx, y1+.dy);
5918 GlDraw.vertex2 (x2+.dx, y2+.dy);
5919 GlDraw.vertex2 (x3+.dx, y3+.dy);
5921 GlDraw.ends ();
5923 ) state.layout
5924 ) rects
5926 Gl.disable `blend;
5929 let display () =
5930 GlClear.color (scalecolor2 conf.bgcolor);
5931 GlClear.clear [`color];
5932 List.iter drawpage state.layout;
5933 let rects =
5934 match state.mode with
5935 | LinkNav (Ltexact (pageno, linkno)) ->
5936 begin match getopaque pageno with
5937 | Some opaque ->
5938 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5939 (pageno, 5, (
5940 float x0, float y0,
5941 float x1, float y0,
5942 float x1, float y1,
5943 float x0, float y1)
5944 ) :: state.rects
5945 | None -> state.rects
5947 | _ -> state.rects
5949 showrects rects;
5950 let rec postloop linkindexbase = function
5951 | l :: rest ->
5952 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5953 postloop linkindexbase rest
5954 | [] -> ()
5956 showsel ();
5957 postloop 0 state.layout;
5958 state.uioh#display;
5959 begin match state.mstate with
5960 | Mzoomrect ((x0, y0), (x1, y1)) ->
5961 Gl.enable `blend;
5962 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5963 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5964 GlDraw.rect (float x0, float y0)
5965 (float x1, float y1);
5966 Gl.disable `blend;
5967 | _ -> ()
5968 end;
5969 enttext ();
5970 scrollindicator ();
5971 Wsi.swapb ();
5974 let zoomrect x y x1 y1 =
5975 let x0 = min x x1
5976 and x1 = max x x1
5977 and y0 = min y y1 in
5978 gotoy (state.y + y0);
5979 state.anchor <- getanchor ();
5980 let zoom = (float state.w) /. float (x1 - x0) in
5981 let margin =
5982 match conf.fitmodel, conf.columns with
5983 | FitPage, Csplit _ ->
5984 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5986 | _, _ ->
5987 let adjw = wadjsb state.winw in
5988 if state.w < adjw
5989 then (adjw - state.w) / 2
5990 else 0
5992 state.x <- (state.x + margin) - x0;
5993 setzoom zoom;
5994 Wsi.setcursor Wsi.CURSOR_INHERIT;
5995 state.mstate <- Mnone;
5998 let scrollx x =
5999 let winw = wadjsb state.winw - 1 in
6000 let s = float x /. float winw in
6001 let destx = truncate (float (state.w + winw) *. s) in
6002 state.x <- winw - destx;
6003 gotoy_and_clear_text state.y;
6004 state.mstate <- Mscrollx;
6007 let scrolly y =
6008 let s = float y /. float state.winh in
6009 let desty = truncate (float (state.maxy - state.winh) *. s) in
6010 gotoy_and_clear_text desty;
6011 state.mstate <- Mscrolly;
6014 let viewmouse button down x y mask =
6015 match button with
6016 | n when (n == 4 || n == 5) && not down ->
6017 if Wsi.withctrl mask
6018 then (
6019 match state.mstate with
6020 | Mzoom (oldn, i) ->
6021 if oldn = n
6022 then (
6023 if i = 2
6024 then
6025 let incr =
6026 match n with
6027 | 5 ->
6028 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6029 | _ ->
6030 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6032 let zoom = conf.zoom -. incr in
6033 setzoom zoom;
6034 state.mstate <- Mzoom (n, 0);
6035 else
6036 state.mstate <- Mzoom (n, i+1);
6038 else state.mstate <- Mzoom (n, 0)
6040 | _ -> state.mstate <- Mzoom (n, 0)
6042 else (
6043 match state.autoscroll with
6044 | Some step -> setautoscrollspeed step (n=4)
6045 | None ->
6046 if conf.wheelbypage || conf.presentation
6047 then (
6048 if n = 4
6049 then prevpage ()
6050 else nextpage ()
6052 else
6053 let incr =
6054 if n = 4
6055 then -conf.scrollstep
6056 else conf.scrollstep
6058 let incr = incr * 2 in
6059 let y = clamp incr in
6060 gotoy_and_clear_text y
6063 | n when (n = 6 || n = 7) && not down && canpan () ->
6064 state.x <-
6065 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6066 gotoy_and_clear_text state.y
6068 | 1 when Wsi.withshift mask ->
6069 state.mstate <- Mnone;
6070 if not down
6071 then (
6072 match unproject x y with
6073 | Some (pageno, ux, uy) ->
6074 let cmd = Printf.sprintf
6075 "%s %s %d %d %d"
6076 conf.stcmd state.path pageno ux uy
6078 popen cmd []
6079 | None -> ()
6082 | 1 when Wsi.withctrl mask ->
6083 if down
6084 then (
6085 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6086 state.mstate <- Mpan (x, y)
6088 else
6089 state.mstate <- Mnone
6091 | 3 ->
6092 if down
6093 then (
6094 Wsi.setcursor Wsi.CURSOR_CYCLE;
6095 let p = (x, y) in
6096 state.mstate <- Mzoomrect (p, p)
6098 else (
6099 match state.mstate with
6100 | Mzoomrect ((x0, y0), _) ->
6101 if abs (x-x0) > 10 && abs (y - y0) > 10
6102 then zoomrect x0 y0 x y
6103 else (
6104 state.mstate <- Mnone;
6105 Wsi.setcursor Wsi.CURSOR_INHERIT;
6106 G.postRedisplay "kill accidental zoom rect";
6108 | _ ->
6109 Wsi.setcursor Wsi.CURSOR_INHERIT;
6110 state.mstate <- Mnone
6113 | 1 when x > state.winw - vscrollw () ->
6114 if down
6115 then
6116 let _, position, sh = state.uioh#scrollph in
6117 if y > truncate position && y < truncate (position +. sh)
6118 then state.mstate <- Mscrolly
6119 else scrolly y
6120 else
6121 state.mstate <- Mnone
6123 | 1 when y > state.winh - hscrollh () ->
6124 if down
6125 then
6126 let _, position, sw = state.uioh#scrollpw in
6127 if x > truncate position && x < truncate (position +. sw)
6128 then state.mstate <- Mscrollx
6129 else scrollx x
6130 else
6131 state.mstate <- Mnone
6133 | 1 ->
6134 let dest = if down then getunder x y else Unone in
6135 begin match dest with
6136 | Ulinkgoto _
6137 | Ulinkuri _
6138 | Uremote _
6139 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6140 gotounder dest
6142 | Unone when down ->
6143 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6144 state.mstate <- Mpan (x, y);
6146 | Unone | Utext _ ->
6147 if down
6148 then (
6149 if conf.angle mod 360 = 0
6150 then (
6151 state.mstate <- Msel ((x, y), (x, y));
6152 G.postRedisplay "mouse select";
6155 else (
6156 match state.mstate with
6157 | Mnone -> ()
6159 | Mzoom _ | Mscrollx | Mscrolly ->
6160 state.mstate <- Mnone
6162 | Mzoomrect ((x0, y0), _) ->
6163 zoomrect x0 y0 x y
6165 | Mpan _ ->
6166 Wsi.setcursor Wsi.CURSOR_INHERIT;
6167 state.mstate <- Mnone
6169 | Msel ((x0, y0), (x1, y1)) ->
6170 let rec loop = function
6171 | [] -> ()
6172 | l :: rest ->
6173 let inside =
6174 let a0 = l.pagedispy in
6175 let a1 = a0 + l.pagevh in
6176 let b0 = l.pagedispx in
6177 let b1 = b0 + l.pagevw in
6178 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6179 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6181 if inside
6182 then
6183 match getopaque l.pageno with
6184 | Some opaque ->
6185 begin
6186 match Ne.pipe () with
6187 | Ne.Exn exn ->
6188 showtext '!'
6189 (Printf.sprintf
6190 "can not create sel pipe: %s"
6191 (exntos exn));
6192 | Ne.Res (r, w) ->
6193 let doclose what fd =
6194 Ne.clo fd (fun msg ->
6195 dolog "%s close failed: %s" what msg)
6198 popen conf.selcmd [r, 0; w, -1];
6199 copysel w opaque true;
6200 doclose "pipe/r" r;
6201 G.postRedisplay "copysel";
6202 with exn ->
6203 dolog "can not execute %S: %s"
6204 conf.selcmd (exntos exn);
6205 doclose "pipe/r" r;
6206 doclose "pipe/w" w;
6208 | None -> ()
6209 else loop rest
6211 loop state.layout;
6212 Wsi.setcursor Wsi.CURSOR_INHERIT;
6213 state.mstate <- Mnone;
6217 | _ -> ()
6220 let birdseyemouse button down x y mask
6221 (conf, leftx, _, hooverpageno, anchor) =
6222 match button with
6223 | 1 when down ->
6224 let rec loop = function
6225 | [] -> ()
6226 | l :: rest ->
6227 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6228 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6229 then (
6230 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6232 else loop rest
6234 loop state.layout
6235 | 3 -> ()
6236 | _ -> viewmouse button down x y mask
6239 let mouse button down x y mask =
6240 state.uioh <- state.uioh#button button down x y mask;
6243 let motion ~x ~y =
6244 state.uioh <- state.uioh#motion x y
6247 let pmotion ~x ~y =
6248 state.uioh <- state.uioh#pmotion x y;
6251 let uioh = object
6252 method display = ()
6254 method key key mask =
6255 begin match state.mode with
6256 | Textentry textentry -> textentrykeyboard key mask textentry
6257 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6258 | View -> viewkeyboard key mask
6259 | LinkNav linknav -> linknavkeyboard key mask linknav
6260 end;
6261 state.uioh
6263 method button button bstate x y mask =
6264 begin match state.mode with
6265 | LinkNav _
6266 | View -> viewmouse button bstate x y mask
6267 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6268 | Textentry _ -> ()
6269 end;
6270 state.uioh
6272 method motion x y =
6273 begin match state.mode with
6274 | Textentry _ -> ()
6275 | View | Birdseye _ | LinkNav _ ->
6276 match state.mstate with
6277 | Mzoom _ | Mnone -> ()
6279 | Mpan (x0, y0) ->
6280 let dx = x - x0
6281 and dy = y0 - y in
6282 state.mstate <- Mpan (x, y);
6283 if canpan ()
6284 then state.x <- panbound (state.x + dx);
6285 let y = clamp dy in
6286 gotoy_and_clear_text y
6288 | Msel (a, _) ->
6289 state.mstate <- Msel (a, (x, y));
6290 G.postRedisplay "motion select";
6292 | Mscrolly ->
6293 let y = min state.winh (max 0 y) in
6294 scrolly y
6296 | Mscrollx ->
6297 let x = min state.winw (max 0 x) in
6298 scrollx x
6300 | Mzoomrect (p0, _) ->
6301 state.mstate <- Mzoomrect (p0, (x, y));
6302 G.postRedisplay "motion zoomrect";
6303 end;
6304 state.uioh
6306 method pmotion x y =
6307 begin match state.mode with
6308 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6309 let rec loop = function
6310 | [] ->
6311 if hooverpageno != -1
6312 then (
6313 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6314 G.postRedisplay "pmotion birdseye no hoover";
6316 | l :: rest ->
6317 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6318 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6319 then (
6320 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6321 G.postRedisplay "pmotion birdseye hoover";
6323 else loop rest
6325 loop state.layout
6327 | Textentry _ -> ()
6329 | LinkNav _
6330 | View ->
6331 match state.mstate with
6332 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6334 | Mnone ->
6335 updateunder x y;
6336 match conf.pax with
6337 | None -> ()
6338 | Some r ->
6339 let past, _, _ = !r in
6340 let now = now () in
6341 let delta = now -. past in
6342 if delta > 0.01
6343 then paxunder x y
6344 else r := (now, x, y)
6345 end;
6346 state.uioh
6348 method infochanged _ = ()
6350 method scrollph =
6351 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6352 let p, h =
6353 if maxy = 0
6354 then 0.0, float state.winh
6355 else scrollph state.y maxy
6357 vscrollw (), p, h
6359 method scrollpw =
6360 let winw = wadjsb state.winw in
6361 let fwinw = float winw in
6362 let sw =
6363 let sw = fwinw /. float state.w in
6364 let sw = fwinw *. sw in
6365 max sw (float conf.scrollh)
6367 let position =
6368 let maxx = state.w + winw in
6369 let x = winw - state.x in
6370 let percent = float x /. float maxx in
6371 (fwinw -. sw) *. percent
6373 hscrollh (), position, sw
6375 method modehash =
6376 let modename =
6377 match state.mode with
6378 | LinkNav _ -> "links"
6379 | Textentry _ -> "textentry"
6380 | Birdseye _ -> "birdseye"
6381 | View -> "view"
6383 findkeyhash conf modename
6385 method eformsgs = true
6386 end;;
6388 module Config =
6389 struct
6390 open Parser
6392 let fontpath = ref "";;
6394 module KeyMap =
6395 Map.Make (struct type t = (int * int) let compare = compare end);;
6397 let unent s =
6398 let l = String.length s in
6399 let b = Buffer.create l in
6400 unent b s 0 l;
6401 Buffer.contents b;
6404 let home =
6405 try Sys.getenv "HOME"
6406 with exn ->
6407 prerr_endline
6408 ("Can not determine home directory location: " ^ exntos exn);
6412 let modifier_of_string = function
6413 | "alt" -> Wsi.altmask
6414 | "shift" -> Wsi.shiftmask
6415 | "ctrl" | "control" -> Wsi.ctrlmask
6416 | "meta" -> Wsi.metamask
6417 | _ -> 0
6420 let key_of_string =
6421 let r = Str.regexp "-" in
6422 fun s ->
6423 let elems = Str.full_split r s in
6424 let f n k m =
6425 let g s =
6426 let m1 = modifier_of_string s in
6427 if m1 = 0
6428 then (Wsi.namekey s, m)
6429 else (k, m lor m1)
6430 in function
6431 | Str.Delim s when n land 1 = 0 -> g s
6432 | Str.Text s -> g s
6433 | Str.Delim _ -> (k, m)
6435 let rec loop n k m = function
6436 | [] -> (k, m)
6437 | x :: xs ->
6438 let k, m = f n k m x in
6439 loop (n+1) k m xs
6441 loop 0 0 0 elems
6444 let keys_of_string =
6445 let r = Str.regexp "[ \t]" in
6446 fun s ->
6447 let elems = Str.split r s in
6448 List.map key_of_string elems
6451 let copykeyhashes c =
6452 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6455 let config_of c attrs =
6456 let apply c k v =
6458 match k with
6459 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6460 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6461 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6462 | "preload" -> { c with preload = bool_of_string v }
6463 | "page-bias" -> { c with pagebias = int_of_string v }
6464 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6465 | "horizontal-scroll-step" ->
6466 { c with hscrollstep = max (int_of_string v) 1 }
6467 | "auto-scroll-step" ->
6468 { c with autoscrollstep = max 0 (int_of_string v) }
6469 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6470 | "crop-hack" -> { c with crophack = bool_of_string v }
6471 | "throttle" ->
6472 let mw =
6473 match String.lowercase v with
6474 | "true" -> Some infinity
6475 | "false" -> None
6476 | f -> Some (float_of_string f)
6478 { c with maxwait = mw}
6479 | "highlight-links" -> { c with hlinks = bool_of_string v }
6480 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6481 | "vertical-margin" ->
6482 { c with interpagespace = max 0 (int_of_string v) }
6483 | "zoom" ->
6484 let zoom = float_of_string v /. 100. in
6485 let zoom = max zoom 0.0 in
6486 { c with zoom = zoom }
6487 | "presentation" -> { c with presentation = bool_of_string v }
6488 | "rotation-angle" -> { c with angle = int_of_string v }
6489 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6490 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6491 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6492 | "proportional-display" ->
6493 let fm =
6494 if bool_of_string v
6495 then FitProportional
6496 else FitWidth
6498 { c with fitmodel = fm }
6499 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6500 | "pixmap-cache-size" ->
6501 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6502 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6503 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6504 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6505 | "persistent-location" -> { c with jumpback = bool_of_string v }
6506 | "background-color" -> { c with bgcolor = color_of_string v }
6507 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6508 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6509 | "mupdf-store-size" ->
6510 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6511 | "checkers" -> { c with checkers = bool_of_string v }
6512 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6513 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6514 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6515 | "uri-launcher" -> { c with urilauncher = unent v }
6516 | "path-launcher" -> { c with pathlauncher = unent v }
6517 | "color-space" -> { c with colorspace = CSTE.of_string v }
6518 | "invert-colors" -> { c with invert = bool_of_string v }
6519 | "brightness" -> { c with colorscale = float_of_string v }
6520 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6521 | "ghyllscroll" ->
6522 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6523 | "columns" ->
6524 let (n, _, _) as nab = multicolumns_of_string v in
6525 if n < 0
6526 then { c with columns = Csplit (-n, [||]) }
6527 else { c with columns = Cmulti (nab, [||]) }
6528 | "birds-eye-columns" ->
6529 { c with beyecolumns = Some (max (int_of_string v) 2) }
6530 | "selection-command" -> { c with selcmd = unent v }
6531 | "synctex-command" -> { c with stcmd = unent v }
6532 | "pax-command" -> { c with paxcmd = unent v }
6533 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6534 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6535 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6536 | "use-pbo" -> { c with usepbo = bool_of_string v }
6537 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6538 | "horizontal-scrollbar-visible" ->
6539 let b =
6540 if bool_of_string v
6541 then c.scrollb lor scrollbhv
6542 else c.scrollb land (lnot scrollbhv)
6544 { c with scrollb = b }
6545 | "vertical-scrollbar-visible" ->
6546 let b =
6547 if bool_of_string v
6548 then c.scrollb lor scrollbvv
6549 else c.scrollb land (lnot scrollbvv)
6551 { c with scrollb = b }
6552 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6553 | "point-and-x" ->
6554 { c with pax =
6555 if bool_of_string v
6556 then Some (ref (0.0, 0, 0))
6557 else None }
6558 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6559 | _ -> c
6560 with exn ->
6561 prerr_endline ("Error processing attribute (`" ^
6562 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6565 let rec fold c = function
6566 | [] -> c
6567 | (k, v) :: rest ->
6568 let c = apply c k v in
6569 fold c rest
6571 fold { c with keyhashes = copykeyhashes c } attrs;
6574 let fromstring f pos n v d =
6575 try f v
6576 with exn ->
6577 dolog "Error processing attribute (%S=%S) at %d\n%s"
6578 n v pos (exntos exn)
6583 let bookmark_of attrs =
6584 let rec fold title page rely visy = function
6585 | ("title", v) :: rest -> fold v page rely visy rest
6586 | ("page", v) :: rest -> fold title v rely visy rest
6587 | ("rely", v) :: rest -> fold title page v visy rest
6588 | ("visy", v) :: rest -> fold title page rely v rest
6589 | _ :: rest -> fold title page rely visy rest
6590 | [] -> title, page, rely, visy
6592 fold "invalid" "0" "0" "0" attrs
6595 let doc_of attrs =
6596 let rec fold path page rely pan visy = function
6597 | ("path", v) :: rest -> fold v page rely pan visy rest
6598 | ("page", v) :: rest -> fold path v rely pan visy rest
6599 | ("rely", v) :: rest -> fold path page v pan visy rest
6600 | ("pan", v) :: rest -> fold path page rely v visy rest
6601 | ("visy", v) :: rest -> fold path page rely pan v rest
6602 | _ :: rest -> fold path page rely pan visy rest
6603 | [] -> path, page, rely, pan, visy
6605 fold "" "0" "0" "0" "0" attrs
6608 let map_of attrs =
6609 let rec fold rs ls = function
6610 | ("out", v) :: rest -> fold v ls rest
6611 | ("in", v) :: rest -> fold rs v rest
6612 | _ :: rest -> fold ls rs rest
6613 | [] -> ls, rs
6615 fold "" "" attrs
6618 let setconf dst src =
6619 dst.scrollbw <- src.scrollbw;
6620 dst.scrollh <- src.scrollh;
6621 dst.icase <- src.icase;
6622 dst.preload <- src.preload;
6623 dst.pagebias <- src.pagebias;
6624 dst.verbose <- src.verbose;
6625 dst.scrollstep <- src.scrollstep;
6626 dst.maxhfit <- src.maxhfit;
6627 dst.crophack <- src.crophack;
6628 dst.autoscrollstep <- src.autoscrollstep;
6629 dst.maxwait <- src.maxwait;
6630 dst.hlinks <- src.hlinks;
6631 dst.underinfo <- src.underinfo;
6632 dst.interpagespace <- src.interpagespace;
6633 dst.zoom <- src.zoom;
6634 dst.presentation <- src.presentation;
6635 dst.angle <- src.angle;
6636 dst.cwinw <- src.cwinw;
6637 dst.cwinh <- src.cwinh;
6638 dst.savebmarks <- src.savebmarks;
6639 dst.memlimit <- src.memlimit;
6640 dst.fitmodel <- src.fitmodel;
6641 dst.texcount <- src.texcount;
6642 dst.sliceheight <- src.sliceheight;
6643 dst.thumbw <- src.thumbw;
6644 dst.jumpback <- src.jumpback;
6645 dst.bgcolor <- src.bgcolor;
6646 dst.tilew <- src.tilew;
6647 dst.tileh <- src.tileh;
6648 dst.mustoresize <- src.mustoresize;
6649 dst.checkers <- src.checkers;
6650 dst.aalevel <- src.aalevel;
6651 dst.trimmargins <- src.trimmargins;
6652 dst.trimfuzz <- src.trimfuzz;
6653 dst.urilauncher <- src.urilauncher;
6654 dst.colorspace <- src.colorspace;
6655 dst.invert <- src.invert;
6656 dst.colorscale <- src.colorscale;
6657 dst.redirectstderr <- src.redirectstderr;
6658 dst.ghyllscroll <- src.ghyllscroll;
6659 dst.columns <- src.columns;
6660 dst.beyecolumns <- src.beyecolumns;
6661 dst.selcmd <- src.selcmd;
6662 dst.updatecurs <- src.updatecurs;
6663 dst.pathlauncher <- src.pathlauncher;
6664 dst.keyhashes <- copykeyhashes src;
6665 dst.hfsize <- src.hfsize;
6666 dst.hscrollstep <- src.hscrollstep;
6667 dst.pgscale <- src.pgscale;
6668 dst.usepbo <- src.usepbo;
6669 dst.wheelbypage <- src.wheelbypage;
6670 dst.stcmd <- src.stcmd;
6671 dst.paxcmd <- src.paxcmd;
6672 dst.scrollb <- src.scrollb;
6673 dst.riani <- src.riani;
6674 dst.paxmark <- src.paxmark;
6675 dst.pax <-
6676 if src.pax = None
6677 then None
6678 else Some ((ref (0.0, 0, 0)));
6681 let get s =
6682 let h = Hashtbl.create 10 in
6683 let dc = { defconf with angle = defconf.angle } in
6684 let rec toplevel v t spos _ =
6685 match t with
6686 | Vdata | Vcdata | Vend -> v
6687 | Vopen ("llppconfig", _, closed) ->
6688 if closed
6689 then v
6690 else { v with f = llppconfig }
6691 | Vopen _ ->
6692 error "unexpected subelement at top level" s spos
6693 | Vclose _ -> error "unexpected close at top level" s spos
6695 and llppconfig v t spos _ =
6696 match t with
6697 | Vdata | Vcdata -> v
6698 | Vend -> error "unexpected end of input in llppconfig" s spos
6699 | Vopen ("defaults", attrs, closed) ->
6700 let c = config_of dc attrs in
6701 setconf dc c;
6702 if closed
6703 then v
6704 else { v with f = defaults }
6706 | Vopen ("ui-font", attrs, closed) ->
6707 let rec getsize size = function
6708 | [] -> size
6709 | ("size", v) :: rest ->
6710 let size =
6711 fromstring int_of_string spos "size" v fstate.fontsize in
6712 getsize size rest
6713 | l -> getsize size l
6715 fstate.fontsize <- getsize fstate.fontsize attrs;
6716 if closed
6717 then v
6718 else { v with f = uifont (Buffer.create 10) }
6720 | Vopen ("doc", attrs, closed) ->
6721 let pathent, spage, srely, span, svisy = doc_of attrs in
6722 let path = unent pathent
6723 and pageno = fromstring int_of_string spos "page" spage 0
6724 and rely = fromstring float_of_string spos "rely" srely 0.0
6725 and pan = fromstring int_of_string spos "pan" span 0
6726 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6727 let c = config_of dc attrs in
6728 let anchor = (pageno, rely, visy) in
6729 if closed
6730 then (Hashtbl.add h path (c, [], pan, anchor); v)
6731 else { v with f = doc path pan anchor c [] }
6733 | Vopen _ ->
6734 error "unexpected subelement in llppconfig" s spos
6736 | Vclose "llppconfig" -> { v with f = toplevel }
6737 | Vclose _ -> error "unexpected close in llppconfig" s spos
6739 and defaults v t spos _ =
6740 match t with
6741 | Vdata | Vcdata -> v
6742 | Vend -> error "unexpected end of input in defaults" s spos
6743 | Vopen ("keymap", attrs, closed) ->
6744 let modename =
6745 try List.assoc "mode" attrs
6746 with Not_found -> "global" in
6747 if closed
6748 then v
6749 else
6750 let ret keymap =
6751 let h = findkeyhash dc modename in
6752 KeyMap.iter (Hashtbl.replace h) keymap;
6753 defaults
6755 { v with f = pkeymap ret KeyMap.empty }
6757 | Vopen (_, _, _) ->
6758 error "unexpected subelement in defaults" s spos
6760 | Vclose "defaults" ->
6761 { v with f = llppconfig }
6763 | Vclose _ -> error "unexpected close in defaults" s spos
6765 and uifont b v t spos epos =
6766 match t with
6767 | Vdata | Vcdata ->
6768 Buffer.add_substring b s spos (epos - spos);
6770 | Vopen (_, _, _) ->
6771 error "unexpected subelement in ui-font" s spos
6772 | Vclose "ui-font" ->
6773 if String.length !fontpath = 0
6774 then fontpath := Buffer.contents b;
6775 { v with f = llppconfig }
6776 | Vclose _ -> error "unexpected close in ui-font" s spos
6777 | Vend -> error "unexpected end of input in ui-font" s spos
6779 and doc path pan anchor c bookmarks v t spos _ =
6780 match t with
6781 | Vdata | Vcdata -> v
6782 | Vend -> error "unexpected end of input in doc" s spos
6783 | Vopen ("bookmarks", _, closed) ->
6784 if closed
6785 then v
6786 else { v with f = pbookmarks path pan anchor c bookmarks }
6788 | Vopen ("keymap", attrs, closed) ->
6789 let modename =
6790 try List.assoc "mode" attrs
6791 with Not_found -> "global"
6793 if closed
6794 then v
6795 else
6796 let ret keymap =
6797 let h = findkeyhash c modename in
6798 KeyMap.iter (Hashtbl.replace h) keymap;
6799 doc path pan anchor c bookmarks
6801 { v with f = pkeymap ret KeyMap.empty }
6803 | Vopen (_, _, _) ->
6804 error "unexpected subelement in doc" s spos
6806 | Vclose "doc" ->
6807 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6808 { v with f = llppconfig }
6810 | Vclose _ -> error "unexpected close in doc" s spos
6812 and pkeymap ret keymap v t spos _ =
6813 match t with
6814 | Vdata | Vcdata -> v
6815 | Vend -> error "unexpected end of input in keymap" s spos
6816 | Vopen ("map", attrs, closed) ->
6817 let r, l = map_of attrs in
6818 let kss = fromstring keys_of_string spos "in" r [] in
6819 let lss = fromstring keys_of_string spos "out" l [] in
6820 let keymap =
6821 match kss with
6822 | [] -> keymap
6823 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6824 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6826 if closed
6827 then { v with f = pkeymap ret keymap }
6828 else
6829 let f () = v in
6830 { v with f = skip "map" f }
6832 | Vopen _ ->
6833 error "unexpected subelement in keymap" s spos
6835 | Vclose "keymap" ->
6836 { v with f = ret keymap }
6838 | Vclose _ -> error "unexpected close in keymap" s spos
6840 and pbookmarks path pan anchor c bookmarks v t spos _ =
6841 match t with
6842 | Vdata | Vcdata -> v
6843 | Vend -> error "unexpected end of input in bookmarks" s spos
6844 | Vopen ("item", attrs, closed) ->
6845 let titleent, spage, srely, svisy = bookmark_of attrs in
6846 let page = fromstring int_of_string spos "page" spage 0
6847 and rely = fromstring float_of_string spos "rely" srely 0.0
6848 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6849 let bookmarks =
6850 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6852 if closed
6853 then { v with f = pbookmarks path pan anchor c bookmarks }
6854 else
6855 let f () = v in
6856 { v with f = skip "item" f }
6858 | Vopen _ ->
6859 error "unexpected subelement in bookmarks" s spos
6861 | Vclose "bookmarks" ->
6862 { v with f = doc path pan anchor c bookmarks }
6864 | Vclose _ -> error "unexpected close in bookmarks" s spos
6866 and skip tag f v t spos _ =
6867 match t with
6868 | Vdata | Vcdata -> v
6869 | Vend ->
6870 error ("unexpected end of input in skipped " ^ tag) s spos
6871 | Vopen (tag', _, closed) ->
6872 if closed
6873 then v
6874 else
6875 let f' () = { v with f = skip tag f } in
6876 { v with f = skip tag' f' }
6877 | Vclose ctag ->
6878 if tag = ctag
6879 then f ()
6880 else error ("unexpected close in skipped " ^ tag) s spos
6883 parse { f = toplevel; accu = () } s;
6884 h, dc;
6887 let do_load f ic =
6889 let len = in_channel_length ic in
6890 let s = String.create len in
6891 really_input ic s 0 len;
6892 f s;
6893 with
6894 | Parse_error (msg, s, pos) ->
6895 let subs = subs s pos in
6896 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6897 failwith ("parse error: " ^ s)
6899 | exn ->
6900 failwith ("config load error: " ^ exntos exn)
6903 let defconfpath =
6904 let dir =
6906 let dir = Filename.concat home ".config" in
6907 if Sys.is_directory dir then dir else home
6908 with _ -> home
6910 Filename.concat dir "llpp.conf"
6913 let confpath = ref defconfpath;;
6915 let load1 f =
6916 if Sys.file_exists !confpath
6917 then
6918 match
6919 (try Some (open_in_bin !confpath)
6920 with exn ->
6921 prerr_endline
6922 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6923 exntos exn);
6924 None
6926 with
6927 | Some ic ->
6928 let success =
6930 f (do_load get ic)
6931 with exn ->
6932 prerr_endline
6933 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6934 exntos exn);
6935 false
6937 close_in ic;
6938 success
6940 | None -> false
6941 else
6942 f (Hashtbl.create 0, defconf)
6945 let load () =
6946 let f (h, dc) =
6947 let pc, pb, px, pa =
6949 let key =
6950 if String.length state.origin = 0
6951 then state.path
6952 else state.origin
6954 Hashtbl.find h (Filename.basename key)
6955 with Not_found -> dc, [], 0, emptyanchor
6957 setconf defconf dc;
6958 setconf conf pc;
6959 state.bookmarks <- pb;
6960 state.x <- px;
6961 if conf.jumpback
6962 then state.anchor <- pa;
6963 cbput state.hists.nav pa;
6964 true
6966 load1 f
6969 let add_attrs bb always dc c =
6970 let ob s a b =
6971 if always || a != b
6972 then Printf.bprintf bb "\n %s='%b'" s a
6973 and op s a b =
6974 if always || a <> b
6975 then Printf.bprintf bb "\n %s='%b'" s (a != None)
6976 and oi s a b =
6977 if always || a != b
6978 then Printf.bprintf bb "\n %s='%d'" s a
6979 and oI s a b =
6980 if always || a != b
6981 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6982 and oz s a b =
6983 if always || a <> b
6984 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6985 and oF s a b =
6986 if always || a <> b
6987 then Printf.bprintf bb "\n %s='%f'" s a
6988 and oc s a b =
6989 if always || a <> b
6990 then
6991 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6992 and oC s a b =
6993 if always || a <> b
6994 then
6995 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
6996 and oR s a b =
6997 if always || a <> b
6998 then
6999 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7000 and os s a b =
7001 if always || a <> b
7002 then
7003 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7004 and og s a b =
7005 if always || a <> b
7006 then
7007 match a with
7008 | None -> ()
7009 | Some (_N, _A, _B) ->
7010 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7011 and oW s a b =
7012 if always || a <> b
7013 then
7014 let v =
7015 match a with
7016 | None -> "false"
7017 | Some f ->
7018 if f = infinity
7019 then "true"
7020 else string_of_float f
7022 Printf.bprintf bb "\n %s='%s'" s v
7023 and oco s a b =
7024 if always || a <> b
7025 then
7026 match a with
7027 | Cmulti ((n, a, b), _) when n > 1 ->
7028 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7029 | Csplit (n, _) when n > 1 ->
7030 Printf.bprintf bb "\n %s='%d'" s ~-n
7031 | _ -> ()
7032 and obeco s a b =
7033 if always || a <> b
7034 then
7035 match a with
7036 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7037 | _ -> ()
7038 and oFm s a b =
7039 if always || a <> b
7040 then
7041 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7042 and oSv s a b m =
7043 if always || a <> b
7044 then
7045 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7046 and oPm s a b =
7047 if always || a <> b
7048 then
7049 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7051 oi "width" c.cwinw dc.cwinw;
7052 oi "height" c.cwinh dc.cwinh;
7053 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7054 oi "scroll-handle-height" c.scrollh dc.scrollh;
7055 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7056 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7057 ob "case-insensitive-search" c.icase dc.icase;
7058 ob "preload" c.preload dc.preload;
7059 oi "page-bias" c.pagebias dc.pagebias;
7060 oi "scroll-step" c.scrollstep dc.scrollstep;
7061 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7062 ob "max-height-fit" c.maxhfit dc.maxhfit;
7063 ob "crop-hack" c.crophack dc.crophack;
7064 oW "throttle" c.maxwait dc.maxwait;
7065 ob "highlight-links" c.hlinks dc.hlinks;
7066 ob "under-cursor-info" c.underinfo dc.underinfo;
7067 oi "vertical-margin" c.interpagespace dc.interpagespace;
7068 oz "zoom" c.zoom dc.zoom;
7069 ob "presentation" c.presentation dc.presentation;
7070 oi "rotation-angle" c.angle dc.angle;
7071 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7072 oFm "fit-model" c.fitmodel dc.fitmodel;
7073 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7074 oi "tex-count" c.texcount dc.texcount;
7075 oi "slice-height" c.sliceheight dc.sliceheight;
7076 oi "thumbnail-width" c.thumbw dc.thumbw;
7077 ob "persistent-location" c.jumpback dc.jumpback;
7078 oc "background-color" c.bgcolor dc.bgcolor;
7079 oi "tile-width" c.tilew dc.tilew;
7080 oi "tile-height" c.tileh dc.tileh;
7081 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7082 ob "checkers" c.checkers dc.checkers;
7083 oi "aalevel" c.aalevel dc.aalevel;
7084 ob "trim-margins" c.trimmargins dc.trimmargins;
7085 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7086 os "uri-launcher" c.urilauncher dc.urilauncher;
7087 os "path-launcher" c.pathlauncher dc.pathlauncher;
7088 oC "color-space" c.colorspace dc.colorspace;
7089 ob "invert-colors" c.invert dc.invert;
7090 oF "brightness" c.colorscale dc.colorscale;
7091 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7092 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7093 oco "columns" c.columns dc.columns;
7094 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7095 os "selection-command" c.selcmd dc.selcmd;
7096 os "synctex-command" c.stcmd dc.stcmd;
7097 os "pax-command" c.paxcmd dc.paxcmd;
7098 ob "update-cursor" c.updatecurs dc.updatecurs;
7099 oi "hint-font-size" c.hfsize dc.hfsize;
7100 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7101 oF "page-scroll-scale" c.pgscale dc.pgscale;
7102 ob "use-pbo" c.usepbo dc.usepbo;
7103 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7104 ob "remote-in-a-new-instance" c.riani dc.riani;
7105 op "point-and-x" c.pax dc.pax;
7106 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7109 let keymapsbuf always dc c =
7110 let bb = Buffer.create 16 in
7111 let rec loop = function
7112 | [] -> ()
7113 | (modename, h) :: rest ->
7114 let dh = findkeyhash dc modename in
7115 if always || h <> dh
7116 then (
7117 if Hashtbl.length h > 0
7118 then (
7119 if Buffer.length bb > 0
7120 then Buffer.add_char bb '\n';
7121 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7122 Hashtbl.iter (fun i o ->
7123 let isdifferent = always ||
7125 let dO = Hashtbl.find dh i in
7126 dO <> o
7127 with Not_found -> true
7129 if isdifferent
7130 then
7131 let addkm (k, m) =
7132 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7133 if Wsi.withalt m then Buffer.add_string bb "alt-";
7134 if Wsi.withshift m then Buffer.add_string bb "shift-";
7135 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7136 Buffer.add_string bb (Wsi.keyname k);
7138 let addkms l =
7139 let rec loop = function
7140 | [] -> ()
7141 | km :: [] -> addkm km
7142 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7144 loop l
7146 Buffer.add_string bb "<map in='";
7147 addkm i;
7148 match o with
7149 | KMinsrt km ->
7150 Buffer.add_string bb "' out='";
7151 addkm km;
7152 Buffer.add_string bb "'/>\n"
7154 | KMinsrl kms ->
7155 Buffer.add_string bb "' out='";
7156 addkms kms;
7157 Buffer.add_string bb "'/>\n"
7159 | KMmulti (ins, kms) ->
7160 Buffer.add_char bb ' ';
7161 addkms ins;
7162 Buffer.add_string bb "' out='";
7163 addkms kms;
7164 Buffer.add_string bb "'/>\n"
7165 ) h;
7166 Buffer.add_string bb "</keymap>";
7169 loop rest
7171 loop c.keyhashes;
7175 let save () =
7176 let uifontsize = fstate.fontsize in
7177 let bb = Buffer.create 32768 in
7178 let relx = float state.x /. float state.winw in
7179 let w, h, x =
7180 let cx w = truncate (relx *. float w) in
7181 List.fold_left
7182 (fun (w, h, x) ws ->
7183 match ws with
7184 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7185 | Wsi.MaxVert -> (w, conf.cwinh, x)
7186 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7188 (state.winw, state.winh, state.x) state.winstate
7190 conf.cwinw <- w;
7191 conf.cwinh <- h;
7192 let f (h, dc) =
7193 let dc = if conf.bedefault then conf else dc in
7194 Buffer.add_string bb "<llppconfig>\n";
7196 if String.length !fontpath > 0
7197 then
7198 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7199 uifontsize
7200 !fontpath
7201 else (
7202 if uifontsize <> 14
7203 then
7204 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7207 Buffer.add_string bb "<defaults ";
7208 add_attrs bb true dc dc;
7209 let kb = keymapsbuf true dc dc in
7210 if Buffer.length kb > 0
7211 then (
7212 Buffer.add_string bb ">\n";
7213 Buffer.add_buffer bb kb;
7214 Buffer.add_string bb "\n</defaults>\n";
7216 else Buffer.add_string bb "/>\n";
7218 let adddoc path pan anchor c bookmarks =
7219 if bookmarks == [] && c = dc && anchor = emptyanchor
7220 then ()
7221 else (
7222 Printf.bprintf bb "<doc path='%s'"
7223 (enent path 0 (String.length path));
7225 if anchor <> emptyanchor
7226 then (
7227 let n, rely, visy = anchor in
7228 Printf.bprintf bb " page='%d'" n;
7229 if rely > 1e-6
7230 then
7231 Printf.bprintf bb " rely='%f'" rely
7233 if abs_float visy > 1e-6
7234 then
7235 Printf.bprintf bb " visy='%f'" visy
7239 if pan != 0
7240 then Printf.bprintf bb " pan='%d'" pan;
7242 add_attrs bb false dc c;
7243 let kb = keymapsbuf false dc c in
7245 begin match bookmarks with
7246 | [] ->
7247 if Buffer.length kb > 0
7248 then (
7249 Buffer.add_string bb ">\n";
7250 Buffer.add_buffer bb kb;
7251 Buffer.add_string bb "\n</doc>\n";
7253 else Buffer.add_string bb "/>\n"
7254 | _ ->
7255 Buffer.add_string bb ">\n<bookmarks>\n";
7256 List.iter (fun (title, _level, (page, rely, visy)) ->
7257 Printf.bprintf bb
7258 "<item title='%s' page='%d'"
7259 (enent title 0 (String.length title))
7260 page
7262 if rely > 1e-6
7263 then
7264 Printf.bprintf bb " rely='%f'" rely
7266 if abs_float visy > 1e-6
7267 then
7268 Printf.bprintf bb " visy='%f'" visy
7270 Buffer.add_string bb "/>\n";
7271 ) bookmarks;
7272 Buffer.add_string bb "</bookmarks>";
7273 if Buffer.length kb > 0
7274 then (
7275 Buffer.add_string bb "\n";
7276 Buffer.add_buffer bb kb;
7278 Buffer.add_string bb "\n</doc>\n";
7279 end;
7283 let pan, conf =
7284 match state.mode with
7285 | Birdseye (c, pan, _, _, _) ->
7286 let beyecolumns =
7287 match conf.columns with
7288 | Cmulti ((c, _, _), _) -> Some c
7289 | Csingle _ -> None
7290 | Csplit _ -> None
7291 and columns =
7292 match c.columns with
7293 | Cmulti (c, _) -> Cmulti (c, [||])
7294 | Csingle _ -> Csingle [||]
7295 | Csplit _ -> failwith "quit from bird's eye while split"
7297 pan, { c with beyecolumns = beyecolumns; columns = columns }
7298 | _ -> x, conf
7300 let basename = Filename.basename
7301 (if String.length state.origin = 0 then state.path else state.origin)
7303 adddoc basename pan (getanchor ())
7304 (let conf =
7305 let autoscrollstep =
7306 match state.autoscroll with
7307 | Some step -> step
7308 | None -> conf.autoscrollstep
7310 match state.mode with
7311 | Birdseye (bc, _, _, _, _) ->
7312 { conf with
7313 zoom = bc.zoom;
7314 presentation = bc.presentation;
7315 interpagespace = bc.interpagespace;
7316 maxwait = bc.maxwait;
7317 autoscrollstep = autoscrollstep }
7318 | _ -> { conf with autoscrollstep = autoscrollstep }
7319 in conf)
7320 (if conf.savebmarks then state.bookmarks else []);
7322 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7323 if basename <> path
7324 then adddoc path x anchor c bookmarks
7325 ) h;
7326 Buffer.add_string bb "</llppconfig>\n";
7327 true;
7329 if load1 f && Buffer.length bb > 0
7330 then
7332 let tmp = !confpath ^ ".tmp" in
7333 let oc = open_out_bin tmp in
7334 Buffer.output_buffer oc bb;
7335 close_out oc;
7336 Unix.rename tmp !confpath;
7337 with exn ->
7338 prerr_endline
7339 ("error while saving configuration: " ^ exntos exn)
7341 end;;
7343 let adderrmsg src msg =
7344 Buffer.add_string state.errmsgs msg;
7345 state.newerrmsgs <- true;
7346 G.postRedisplay src
7349 let adderrfmt src fmt =
7350 Format.kprintf (fun s -> adderrmsg src s) fmt;
7353 let ract cmds =
7354 let cl = splitatspace cmds in
7355 let scan s fmt f =
7356 try Scanf.sscanf s fmt f
7357 with exn ->
7358 adderrfmt "remote exec"
7359 "error processing '%S': %s\n" cmds (exntos exn)
7361 match cl with
7362 | "reload" :: [] -> reload ()
7363 | "goto" :: args :: [] ->
7364 scan args "%u %f %f"
7365 (fun pageno x y ->
7366 let cmd, _ = state.geomcmds in
7367 if String.length cmd = 0
7368 then gotopagexy pageno x y
7369 else
7370 let f prevf () =
7371 gotopagexy pageno x y;
7372 prevf ()
7374 state.reprf <- f state.reprf
7376 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7377 | "rect" :: args :: [] ->
7378 scan args "%u %u %f %f %f %f"
7379 (fun pageno color x0 y0 x1 y1 ->
7380 onpagerect pageno (fun w h ->
7381 let _,w1,h1,_ = getpagedim pageno in
7382 let sw = float w1 /. w
7383 and sh = float h1 /. h in
7384 let x0s = x0 *. sw
7385 and x1s = x1 *. sw
7386 and y0s = y0 *. sh
7387 and y1s = y1 *. sh in
7388 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7389 debugrect rect;
7390 state.rects <- (pageno, color, rect) :: state.rects;
7391 G.postRedisplay "rect";
7394 | "activatewin" :: [] -> Wsi.activatewin ()
7395 | "quit" :: [] -> raise Quit
7396 | _ ->
7397 adderrfmt "remote command"
7398 "error processing remote command: %S\n" cmds;
7401 let remote =
7402 let scratch = String.create 80 in
7403 let buf = Buffer.create 80 in
7404 fun fd ->
7405 let rec tempfr () =
7406 try Some (Unix.read fd scratch 0 80)
7407 with
7408 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7409 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7410 | exn -> raise exn
7412 match tempfr () with
7413 | None -> Some fd
7414 | Some n ->
7415 if n = 0
7416 then (
7417 Unix.close fd;
7418 if Buffer.length buf > 0
7419 then (
7420 let s = Buffer.contents buf in
7421 Buffer.clear buf;
7422 ract s;
7424 None
7426 else
7427 let rec eat ppos =
7428 let nlpos =
7430 let pos = String.index_from scratch ppos '\n' in
7431 if pos >= n then -1 else pos
7432 with Not_found -> -1
7434 if nlpos >= 0
7435 then (
7436 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7437 let s = Buffer.contents buf in
7438 Buffer.clear buf;
7439 ract s;
7440 eat (nlpos+1);
7442 else (
7443 Buffer.add_substring buf scratch ppos (n-ppos);
7444 Some fd
7446 in eat 0
7449 let remoteopen path =
7450 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7451 with exn ->
7452 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7453 None
7456 let () =
7457 let trimcachepath = ref "" in
7458 let rcmdpath = ref "" in
7459 selfexec := Sys.executable_name;
7460 Arg.parse
7461 (Arg.align
7462 [("-p", Arg.String (fun s -> state.password <- s),
7463 "<password> Set password");
7465 ("-f", Arg.String
7466 (fun s ->
7467 Config.fontpath := s;
7468 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7470 "<path> Set path to the user interface font");
7472 ("-c", Arg.String
7473 (fun s ->
7474 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7475 Config.confpath := s),
7476 "<path> Set path to the configuration file");
7478 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7479 "<path> Set path to the trim cache file");
7481 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7482 "<named-destination> Set named destination");
7484 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7485 ("-cxack", Arg.Set cxack, " Cut corners");
7487 ("-remote", Arg.String (fun s -> rcmdpath := s),
7488 "<path> Set path to the remote commands source");
7490 ("-origin", Arg.String (fun s -> state.origin <- s),
7491 "<original-path> Set original path");
7493 ("-v", Arg.Unit (fun () ->
7494 Printf.printf
7495 "%s\nconfiguration path: %s\n"
7496 (version ())
7497 Config.defconfpath
7499 exit 0), " Print version and exit");
7502 (fun s -> state.path <- s)
7503 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7505 if !wtmode
7506 then selfexec := !selfexec ^ " -wtmode";
7508 if String.length state.path = 0
7509 then (prerr_endline "file name missing"; exit 1);
7511 if not (Config.load ())
7512 then prerr_endline "failed to load configuration";
7514 let wsfd, winw, winh = Wsi.init (object
7515 val mutable m_hack = false
7516 method expose = if not m_hack then G.postRedisplay "expose"
7517 method visible = G.postRedisplay "visible"
7518 method display = m_hack <- false; display ()
7519 method reshape w h =
7520 m_hack <- w < state.winw && h < state.winh;
7521 reshape w h
7522 method mouse b d x y m = mouse b d x y m
7523 method motion x y = state.mpos <- (x, y); motion x y
7524 method pmotion x y = state.mpos <- (x, y); pmotion x y
7525 method key k m =
7526 let mascm = m land (
7527 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7528 ) in
7529 match state.keystate with
7530 | KSnone ->
7531 let km = k, mascm in
7532 begin
7533 match
7534 let modehash = state.uioh#modehash in
7535 try Hashtbl.find modehash km
7536 with Not_found ->
7537 try Hashtbl.find (findkeyhash conf "global") km
7538 with Not_found -> KMinsrt (k, m)
7539 with
7540 | KMinsrt (k, m) -> keyboard k m
7541 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7542 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7544 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7545 List.iter (fun (k, m) -> keyboard k m) insrt;
7546 state.keystate <- KSnone
7547 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7548 state.keystate <- KSinto (keys, insrt)
7549 | _ ->
7550 state.keystate <- KSnone
7552 method enter x y = state.mpos <- (x, y); pmotion x y
7553 method leave = state.mpos <- (-1, -1)
7554 method winstate wsl = state.winstate <- wsl
7555 method quit = raise Quit
7556 end) conf.cwinw conf.cwinh (platform = Posx) in
7558 state.wsfd <- wsfd;
7560 if not (
7561 List.exists GlMisc.check_extension
7562 [ "GL_ARB_texture_rectangle"
7563 ; "GL_EXT_texture_recangle"
7564 ; "GL_NV_texture_rectangle" ]
7566 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7568 if (
7569 let r = GlMisc.get_string `renderer in
7570 let p = "Mesa DRI Intel(" in
7571 let l = String.length p in
7572 String.length r > l && String.sub r 0 l = p
7574 then defconf.sliceheight <- 1024;
7576 let cr, sw =
7577 match Ne.pipe () with
7578 | Ne.Exn exn ->
7579 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7580 exit 1
7581 | Ne.Res rw -> rw
7582 and sr, cw =
7583 match Ne.pipe () with
7584 | Ne.Exn exn ->
7585 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7586 exit 1
7587 | Ne.Res rw -> rw
7590 cloexec cr;
7591 cloexec sw;
7592 cloexec sr;
7593 cloexec cw;
7595 setcheckers conf.checkers;
7596 redirectstderr ();
7597 if conf.redirectstderr
7598 then
7599 at_exit (fun () ->
7600 let s = Buffer.contents state.errmsgs ^
7601 (match state.errfd with
7602 | Some fd ->
7603 let s = String.create (80*24) in
7604 let n =
7606 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7607 if List.mem fd r
7608 then Unix.read fd s 0 (String.length s)
7609 else 0
7610 with _ -> 0
7612 if n = 0
7613 then ""
7614 else String.sub s 0 n
7615 | None -> ""
7618 try ignore (Unix.write state.stderr s 0 (String.length s))
7619 with exn -> print_endline (exntos exn)
7623 init (cr, cw) (
7624 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7625 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7626 !Config.fontpath, !trimcachepath,
7627 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7629 state.sr <- sr;
7630 state.sw <- sw;
7631 state.text <- "Opening " ^ (mbtoutf8 state.path);
7632 reshape winw winh;
7633 opendoc state.path state.password;
7634 state.uioh <- uioh;
7635 display ();
7636 Wsi.mapwin ();
7637 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7638 let optrfd =
7639 ref (
7640 if String.length !rcmdpath > 0
7641 then remoteopen !rcmdpath
7642 else None
7646 let rec loop deadline =
7647 let r =
7648 match state.errfd with
7649 | None -> [state.sr; state.wsfd]
7650 | Some fd -> [state.sr; state.wsfd; fd]
7652 let r =
7653 match !optrfd with
7654 | None -> r
7655 | Some fd -> fd :: r
7657 if state.redisplay
7658 then (
7659 state.redisplay <- false;
7660 display ();
7662 let timeout =
7663 let now = now () in
7664 if deadline > now
7665 then (
7666 if deadline = infinity
7667 then ~-.1.0
7668 else max 0.0 (deadline -. now)
7670 else 0.0
7672 let r, _, _ =
7673 try Unix.select r [] [] timeout
7674 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7676 begin match r with
7677 | [] ->
7678 state.ghyll None;
7679 let newdeadline =
7680 if state.ghyll == noghyll
7681 then
7682 match state.autoscroll with
7683 | Some step when step != 0 ->
7684 let y = state.y + step in
7685 let y =
7686 if y < 0
7687 then state.maxy
7688 else if y >= state.maxy then 0 else y
7690 gotoy y;
7691 if state.mode = View
7692 then state.text <- "";
7693 deadline +. 0.01
7694 | _ -> infinity
7695 else deadline +. 0.01
7697 loop newdeadline
7699 | l ->
7700 let rec checkfds = function
7701 | [] -> ()
7702 | fd :: rest when fd = state.sr ->
7703 let cmd = readcmd state.sr in
7704 act cmd;
7705 checkfds rest
7707 | fd :: rest when fd = state.wsfd ->
7708 Wsi.readresp fd;
7709 checkfds rest
7711 | fd :: rest when Some fd = !optrfd ->
7712 begin match remote fd with
7713 | None -> optrfd := remoteopen !rcmdpath;
7714 | opt -> optrfd := opt
7715 end;
7716 checkfds rest
7718 | fd :: rest ->
7719 let s = String.create 80 in
7720 let n = tempfailureretry (Unix.read fd s 0) 80 in
7721 if conf.redirectstderr
7722 then (
7723 Buffer.add_substring state.errmsgs s 0 n;
7724 state.newerrmsgs <- true;
7725 state.redisplay <- true;
7727 else (
7728 prerr_string (String.sub s 0 n);
7729 flush stderr;
7731 checkfds rest
7733 checkfds l;
7734 let newdeadline =
7735 let deadline1 =
7736 if deadline = infinity
7737 then now () +. 0.01
7738 else deadline
7740 match state.autoscroll with
7741 | Some step when step != 0 -> deadline1
7742 | _ -> if state.ghyll == noghyll then infinity else deadline1
7744 loop newdeadline
7745 end;
7748 loop infinity;
7749 with Quit ->
7750 Config.save ();