754fdec1107f4be659fc6f9337537c79afcc5ae0
[llpp.git] / main.ml
blob754fdec1107f4be659fc6f9337537c79afcc5ae0
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type mark =
17 | Mark_page
18 | Mark_block
19 | Mark_line
20 | Mark_word
23 type params = (angle * fitmodel * trimparams
24 * texcount * sliceheight * memsize
25 * colorspace * fontpath * trimcachepath
26 * haspbo)
27 and pageno = int
28 and width = int
29 and height = int
30 and leftx = int
31 and opaque = string
32 and recttype = int
33 and pixmapsize = int
34 and angle = int
35 and trimmargins = bool
36 and interpagespace = int
37 and texcount = int
38 and sliceheight = int
39 and gen = int
40 and top = float
41 and dtop = float
42 and fontpath = string
43 and trimcachepath = string
44 and memsize = int
45 and aalevel = int
46 and irect = (int * int * int * int)
47 and trimparams = (trimmargins * irect)
48 and colorspace = | Rgb | Bgr | Gray
49 and fitmodel = | FitWidth | FitProportional | FitPage
50 and haspbo = bool
53 type x = int
54 and y = int
55 and tilex = int
56 and tiley = int
57 and tileparams = (x * y * width * height * tilex * tiley)
60 type link =
61 | Lnotfound
62 | Lfound of int
63 and linkdir =
64 | LDfirst
65 | LDlast
66 | LDfirstvisible of (int * int * int)
67 | LDleft of int
68 | LDright of int
69 | LDdown of int
70 | LDup of int
73 type pagewithlinks =
74 | Pwlnotfound
75 | Pwl of int
78 type keymap =
79 | KMinsrt of key
80 | KMinsrl of key list
81 | KMmulti of key list * key list
82 and key = int * int
83 and keyhash = (key, keymap) Hashtbl.t
84 and keystate =
85 | KSnone
86 | KSinto of (key list * key list)
89 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
90 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
92 type pipe = (Unix.file_descr * Unix.file_descr);;
94 external init : pipe -> params -> unit = "ml_init";;
95 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
96 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
97 external getpdimrect : int -> float array = "ml_getpdimrect";;
98 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
99 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
100 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
101 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
102 external measurestr : int -> string -> float = "ml_measure_string";;
103 external postprocess :
104 opaque -> int -> int -> int -> (int * string * int) -> int
105 = "ml_postprocess";;
106 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
107 external platform : unit -> platform = "ml_platform";;
108 external setaalevel : int -> unit = "ml_setaalevel";;
109 external realloctexts : int -> bool = "ml_realloctexts";;
110 external findlink : opaque -> linkdir -> link = "ml_findlink";;
111 external getlink : opaque -> int -> under = "ml_getlink";;
112 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
113 external getlinkcount : opaque -> int = "ml_getlinkcount";;
114 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
115 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
116 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
117 external freepbo : string -> unit = "ml_freepbo";;
118 external unmappbo : string -> unit = "ml_unmappbo";;
119 external pbousable : unit -> bool = "ml_pbo_usable";;
120 external unproject : opaque -> int -> int -> (int * int) option
121 = "ml_unproject";;
122 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
124 let platform_to_string = function
125 | Punknown -> "unknown"
126 | Plinux -> "Linux"
127 | Posx -> "OSX"
128 | Psun -> "Sun"
129 | Pfreebsd -> "FreeBSD"
130 | Pdragonflybsd -> "DragonflyBSD"
131 | Popenbsd -> "OpenBSD"
132 | Pnetbsd -> "NetBSD"
133 | Pcygwin -> "Cygwin"
136 let platform = platform ();;
138 let now = Unix.gettimeofday;;
140 let selfexec = ref "";;
142 let popen cmd fda =
143 if platform = Pcygwin
144 then (
145 let sh = "/bin/sh" in
146 let args = [|sh; "-c"; cmd|] in
147 let rec std si so se = function
148 | [] -> si, so, se
149 | (fd, 0) :: rest -> std fd so se rest
150 | (fd, -1) :: rest ->
151 Unix.set_close_on_exec fd;
152 std si so se rest
153 | (_, n) :: _ ->
154 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
156 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
157 ignore (Unix.create_process sh args si so se)
159 else popen cmd fda;
162 type mpos = int * int
163 and mstate =
164 | Msel of (mpos * mpos)
165 | Mpan of mpos
166 | Mscrolly | Mscrollx
167 | Mzoom of (int * int)
168 | Mzoomrect of (mpos * mpos)
169 | Mnone
172 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
173 and onkey = string -> int -> te
174 and ondone = string -> unit
175 and histcancel = unit -> unit
176 and onhist = ((histcmd -> string) * histcancel)
177 and histcmd = HCnext | HCprev | HCfirst | HClast
178 and cancelonempty = bool
179 and te =
180 | TEstop
181 | TEdone of string
182 | TEcont of string
183 | TEswitch of textentry
186 type 'a circbuf =
187 { store : 'a array
188 ; mutable rc : int
189 ; mutable wc : int
190 ; mutable len : int
194 let bound v minv maxv =
195 max minv (min maxv v);
198 let cbnew n v =
199 { store = Array.create n v
200 ; rc = 0
201 ; wc = 0
202 ; len = 0
206 let cbcap b = Array.length b.store;;
208 let cbput b v =
209 let cap = cbcap b in
210 b.store.(b.wc) <- v;
211 b.wc <- (b.wc + 1) mod cap;
212 b.rc <- b.wc;
213 b.len <- min (b.len + 1) cap;
216 let cbempty b = b.len = 0;;
218 let cbgetg b circular dir =
219 if cbempty b
220 then b.store.(0)
221 else
222 let rc = b.rc + dir in
223 let rc =
224 if circular
225 then (
226 if rc = -1
227 then b.len-1
228 else (
229 if rc >= b.len
230 then 0
231 else rc
234 else bound rc 0 (b.len-1)
236 b.rc <- rc;
237 b.store.(rc);
240 let cbget b = cbgetg b false;;
241 let cbgetc b = cbgetg b true;;
243 let drawstring size x y s =
244 Gl.enable `blend;
245 Gl.enable `texture_2d;
246 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
247 ignore (drawstr size x y s);
248 Gl.disable `blend;
249 Gl.disable `texture_2d;
252 let drawstring1 size x y s =
253 drawstr size x y s;
256 let drawstring2 size x y fmt =
257 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
260 type page =
261 { pageno : int
262 ; pagedimno : int
263 ; pagew : int
264 ; pageh : int
265 ; pagex : int
266 ; pagey : int
267 ; pagevw : int
268 ; pagevh : int
269 ; pagedispx : int
270 ; pagedispy : int
271 ; pagecol : int
275 let debugl l =
276 dolog "l %d dim=%d {" l.pageno l.pagedimno;
277 dolog " WxH %dx%d" l.pagew l.pageh;
278 dolog " vWxH %dx%d" l.pagevw l.pagevh;
279 dolog " pagex,y %d,%d" l.pagex l.pagey;
280 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
281 dolog " column %d" l.pagecol;
282 dolog "}";
285 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
286 dolog "rect {";
287 dolog " x0,y0=(% f, % f)" x0 y0;
288 dolog " x1,y1=(% f, % f)" x1 y1;
289 dolog " x2,y2=(% f, % f)" x2 y2;
290 dolog " x3,y3=(% f, % f)" x3 y3;
291 dolog "}";
294 type multicolumns = multicol * pagegeom
295 and singlecolumn = pagegeom
296 and splitcolumns = columncount * pagegeom
297 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
298 and multicol = columncount * covercount * covercount
299 and pdimno = int
300 and columncount = int
301 and covercount = int;;
303 type scrollb = int;;
304 let scrollbvv = 1;;
305 let scrollbhv = 2;;
307 type conf =
308 { mutable scrollbw : int
309 ; mutable scrollh : int
310 ; mutable scrollb : scrollb
311 ; mutable icase : bool
312 ; mutable preload : bool
313 ; mutable pagebias : int
314 ; mutable verbose : bool
315 ; mutable debug : bool
316 ; mutable scrollstep : int
317 ; mutable hscrollstep : int
318 ; mutable maxhfit : bool
319 ; mutable crophack : bool
320 ; mutable autoscrollstep : int
321 ; mutable maxwait : float option
322 ; mutable hlinks : bool
323 ; mutable underinfo : bool
324 ; mutable interpagespace : interpagespace
325 ; mutable zoom : float
326 ; mutable presentation : bool
327 ; mutable angle : angle
328 ; mutable cwinw : int
329 ; mutable cwinh : int
330 ; mutable savebmarks : bool
331 ; mutable fitmodel : fitmodel
332 ; mutable trimmargins : trimmargins
333 ; mutable trimfuzz : irect
334 ; mutable memlimit : memsize
335 ; mutable texcount : texcount
336 ; mutable sliceheight : sliceheight
337 ; mutable thumbw : width
338 ; mutable jumpback : bool
339 ; mutable bgcolor : (float * float * float)
340 ; mutable bedefault : bool
341 ; mutable tilew : int
342 ; mutable tileh : int
343 ; mutable mustoresize : memsize
344 ; mutable checkers : bool
345 ; mutable aalevel : int
346 ; mutable urilauncher : string
347 ; mutable pathlauncher : string
348 ; mutable colorspace : colorspace
349 ; mutable invert : bool
350 ; mutable colorscale : float
351 ; mutable redirectstderr : bool
352 ; mutable ghyllscroll : (int * int * int) option
353 ; mutable columns : columns
354 ; mutable beyecolumns : columncount option
355 ; mutable selcmd : string
356 ; mutable paxcmd : string
357 ; mutable updatecurs : bool
358 ; mutable keyhashes : (string * keyhash) list
359 ; mutable hfsize : int
360 ; mutable pgscale : float
361 ; mutable usepbo : bool
362 ; mutable wheelbypage : bool
363 ; mutable stcmd : string
364 ; mutable riani : bool
365 ; mutable pax : (float * int * int) ref option
366 ; mutable paxmark : mark
368 and columns =
369 | Csingle of singlecolumn
370 | Cmulti of multicolumns
371 | Csplit of splitcolumns
374 type anchor = pageno * top * dtop;;
376 type outline = string * int * anchor;;
378 type rect = float * float * float * float * float * float * float * float;;
380 type tile = opaque * pixmapsize * elapsed
381 and elapsed = float;;
382 type pagemapkey = pageno * gen;;
383 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
384 and row = int
385 and col = int;;
387 let emptyanchor = (0, 0.0, 0.0);;
389 type infochange = | Memused | Docinfo | Pdim;;
391 class type uioh = object
392 method display : unit
393 method key : int -> int -> uioh
394 method button : int -> bool -> int -> int -> int -> uioh
395 method motion : int -> int -> uioh
396 method pmotion : int -> int -> uioh
397 method infochanged : infochange -> unit
398 method scrollpw : (int * float * float)
399 method scrollph : (int * float * float)
400 method modehash : keyhash
401 method eformsgs : bool
402 end;;
404 type mode =
405 | Birdseye of (conf * leftx * pageno * pageno * anchor)
406 | Textentry of (textentry * onleave)
407 | View
408 | LinkNav of linktarget
409 and onleave = leavetextentrystatus -> unit
410 and leavetextentrystatus = | Cancel | Confirm
411 and helpitem = string * int * action
412 and action =
413 | Noaction
414 | Action of (uioh -> uioh)
415 and linktarget =
416 | Ltexact of (pageno * int)
417 | Ltgendir of int
420 let isbirdseye = function Birdseye _ -> true | _ -> false;;
421 let istextentry = function Textentry _ -> true | _ -> false;;
423 type currently =
424 | Idle
425 | Loading of (page * gen)
426 | Tiling of (
427 page * opaque * colorspace * angle * gen * col * row * width * height
429 | Outlining of outline list
432 let emptykeyhash = Hashtbl.create 0;;
433 let nouioh : uioh = object (self)
434 method display = ()
435 method key _ _ = self
436 method button _ _ _ _ _ = self
437 method motion _ _ = self
438 method pmotion _ _ = self
439 method infochanged _ = ()
440 method scrollpw = (0, nan, nan)
441 method scrollph = (0, nan, nan)
442 method modehash = emptykeyhash
443 method eformsgs = false
444 end;;
446 type state =
447 { mutable sr : Unix.file_descr
448 ; mutable sw : Unix.file_descr
449 ; mutable wsfd : Unix.file_descr
450 ; mutable errfd : Unix.file_descr option
451 ; mutable stderr : Unix.file_descr
452 ; mutable errmsgs : Buffer.t
453 ; mutable newerrmsgs : bool
454 ; mutable w : int
455 ; mutable x : int
456 ; mutable y : int
457 ; mutable anchor : anchor
458 ; mutable ranchors : (string * string * anchor * string) list
459 ; mutable maxy : int
460 ; mutable layout : page list
461 ; pagemap : (pagemapkey, opaque) Hashtbl.t
462 ; tilemap : (tilemapkey, tile) Hashtbl.t
463 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
464 ; mutable pdims : (pageno * width * height * leftx) list
465 ; mutable pagecount : int
466 ; mutable currently : currently
467 ; mutable mstate : mstate
468 ; mutable searchpattern : string
469 ; mutable rects : (pageno * recttype * rect) list
470 ; mutable rects1 : (pageno * recttype * rect) list
471 ; mutable text : string
472 ; mutable winstate : Wsi.winstate list
473 ; mutable mode : mode
474 ; mutable uioh : uioh
475 ; mutable outlines : outline array
476 ; mutable bookmarks : outline list
477 ; mutable path : string
478 ; mutable password : string
479 ; mutable nameddest : string
480 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
481 ; mutable memused : memsize
482 ; mutable gen : gen
483 ; mutable throttle : (page list * int * float) option
484 ; mutable autoscroll : int option
485 ; mutable ghyll : (int option -> unit)
486 ; mutable help : helpitem array
487 ; mutable docinfo : (int * string) list
488 ; mutable texid : GlTex.texture_id option
489 ; hists : hists
490 ; mutable prevzoom : float
491 ; mutable progress : float
492 ; mutable redisplay : bool
493 ; mutable mpos : mpos
494 ; mutable keystate : keystate
495 ; mutable glinks : bool
496 ; mutable prevcolumns : (columns * float) option
497 ; mutable winw : int
498 ; mutable winh : int
499 ; mutable reprf : (unit -> unit)
500 ; mutable origin : string
501 ; mutable roam : (unit -> unit)
503 and hists =
504 { pat : string circbuf
505 ; pag : string circbuf
506 ; nav : anchor circbuf
507 ; sel : string circbuf
511 let defconf =
512 { scrollbw = 7
513 ; scrollh = 12
514 ; scrollb = scrollbhv lor scrollbvv
515 ; icase = true
516 ; preload = true
517 ; pagebias = 0
518 ; verbose = false
519 ; debug = false
520 ; scrollstep = 24
521 ; hscrollstep = 24
522 ; maxhfit = true
523 ; crophack = false
524 ; autoscrollstep = 2
525 ; maxwait = None
526 ; hlinks = false
527 ; underinfo = false
528 ; interpagespace = 2
529 ; zoom = 1.0
530 ; presentation = false
531 ; angle = 0
532 ; cwinw = 900
533 ; cwinh = 900
534 ; savebmarks = true
535 ; fitmodel = FitProportional
536 ; trimmargins = false
537 ; trimfuzz = (0,0,0,0)
538 ; memlimit = 32 lsl 20
539 ; texcount = 256
540 ; sliceheight = 24
541 ; thumbw = 76
542 ; jumpback = true
543 ; bgcolor = (0.5, 0.5, 0.5)
544 ; bedefault = false
545 ; tilew = 2048
546 ; tileh = 2048
547 ; mustoresize = 256 lsl 20
548 ; checkers = true
549 ; aalevel = 8
550 ; urilauncher =
551 (match platform with
552 | Plinux | Pfreebsd | Pdragonflybsd
553 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
554 | Posx -> "open \"%s\""
555 | Pcygwin -> "cygstart \"%s\""
556 | Punknown -> "echo %s")
557 ; pathlauncher = "lp \"%s\""
558 ; selcmd =
559 (match platform with
560 | Plinux | Pfreebsd | Pdragonflybsd
561 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
562 | Posx -> "pbcopy"
563 | Pcygwin -> "wsel"
564 | Punknown -> "cat")
565 ; paxcmd = "cat"
566 ; colorspace = Rgb
567 ; invert = false
568 ; colorscale = 1.0
569 ; redirectstderr = false
570 ; ghyllscroll = None
571 ; columns = Csingle [||]
572 ; beyecolumns = None
573 ; updatecurs = false
574 ; hfsize = 12
575 ; pgscale = 1.0
576 ; usepbo = false
577 ; wheelbypage = false
578 ; stcmd = "echo SyncTex"
579 ; riani = false
580 ; pax = None
581 ; paxmark = Mark_word
582 ; keyhashes =
583 let mk n = (n, Hashtbl.create 1) in
584 [ mk "global"
585 ; mk "info"
586 ; mk "help"
587 ; mk "outline"
588 ; mk "listview"
589 ; mk "birdseye"
590 ; mk "textentry"
591 ; mk "links"
592 ; mk "view"
597 let wtmode = ref false;;
599 let findkeyhash c name =
600 try List.assoc name c.keyhashes
601 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
604 let conf = { defconf with angle = defconf.angle };;
606 let pgscale h = truncate (float h *. conf.pgscale);;
608 type fontstate =
609 { mutable fontsize : int
610 ; mutable wwidth : float
611 ; mutable maxrows : int
615 let fstate =
616 { fontsize = 14
617 ; wwidth = nan
618 ; maxrows = -1
622 let geturl s =
623 let colonpos = try String.index s ':' with Not_found -> -1 in
624 let len = String.length s in
625 if colonpos >= 0 && colonpos + 3 < len
626 then (
627 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
628 then
629 let schemestartpos =
630 try String.rindex_from s colonpos ' '
631 with Not_found -> -1
633 let scheme =
634 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
636 match scheme with
637 | "http" | "ftp" | "mailto" ->
638 let epos =
639 try String.index_from s colonpos ' '
640 with Not_found -> len
642 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
643 | _ -> ""
644 else ""
646 else ""
649 let gotouri uri =
650 if String.length conf.urilauncher = 0
651 then print_endline uri
652 else (
653 let url = geturl uri in
654 if String.length url = 0
655 then Printf.eprintf "obtained empty url from uri %S" uri
656 else
657 let re = Str.regexp "%s" in
658 let command = Str.global_replace re url conf.urilauncher in
659 try popen command []
660 with exn ->
661 Printf.eprintf
662 "failed to execute `%s': %s\n" command (exntos exn);
663 flush stderr;
667 let version () =
668 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
669 (platform_to_string platform) Sys.word_size Sys.ocaml_version
672 let makehelp () =
673 let strings = version () :: "" :: Help.keys in
674 Array.of_list (
675 List.map (fun s ->
676 let url = geturl s in
677 if String.length url > 0
678 then (s, 0, Action (fun u -> gotouri url; u))
679 else (s, 0, Noaction)
680 ) strings);
683 let noghyll _ = ();;
684 let firstgeomcmds = "", [];;
685 let noreprf () = ();;
687 let state =
688 { sr = Unix.stdin
689 ; sw = Unix.stdin
690 ; wsfd = Unix.stdin
691 ; errfd = None
692 ; stderr = Unix.stderr
693 ; errmsgs = Buffer.create 0
694 ; newerrmsgs = false
695 ; x = 0
696 ; y = 0
697 ; w = 0
698 ; anchor = emptyanchor
699 ; ranchors = []
700 ; layout = []
701 ; maxy = max_int
702 ; tilelru = Queue.create ()
703 ; pagemap = Hashtbl.create 10
704 ; tilemap = Hashtbl.create 10
705 ; pdims = []
706 ; pagecount = 0
707 ; currently = Idle
708 ; mstate = Mnone
709 ; rects = []
710 ; rects1 = []
711 ; text = ""
712 ; mode = View
713 ; winstate = []
714 ; searchpattern = ""
715 ; outlines = [||]
716 ; bookmarks = []
717 ; path = ""
718 ; password = ""
719 ; nameddest = ""
720 ; geomcmds = firstgeomcmds
721 ; hists =
722 { nav = cbnew 10 emptyanchor
723 ; pat = cbnew 10 ""
724 ; pag = cbnew 10 ""
725 ; sel = cbnew 10 ""
727 ; memused = 0
728 ; gen = 0
729 ; throttle = None
730 ; autoscroll = None
731 ; ghyll = noghyll
732 ; help = makehelp ()
733 ; docinfo = []
734 ; texid = None
735 ; prevzoom = 1.0
736 ; progress = -1.0
737 ; uioh = nouioh
738 ; redisplay = true
739 ; mpos = (-1, -1)
740 ; keystate = KSnone
741 ; glinks = false
742 ; prevcolumns = None
743 ; winw = -1
744 ; winh = -1
745 ; reprf = noreprf
746 ; origin = ""
747 ; roam = (fun () -> ())
751 let hscrollh () =
752 if (conf.scrollb land scrollbhv = 0)
753 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
754 then 0
755 else conf.scrollbw
758 let vscrollw () =
759 if (conf.scrollb land scrollbvv = 0)
760 then 0
761 else conf.scrollbw
764 let wadjsb w = w - vscrollw ();;
766 let setfontsize n =
767 fstate.fontsize <- n;
768 fstate.wwidth <- measurestr fstate.fontsize "w";
769 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
772 let vlog fmt =
773 if conf.verbose
774 then
775 Printf.kprintf prerr_endline fmt
776 else
777 Printf.kprintf ignore fmt
780 let launchpath () =
781 if String.length conf.pathlauncher = 0
782 then print_endline state.path
783 else (
784 let re = Str.regexp "%s" in
785 let command = Str.global_replace re state.path conf.pathlauncher in
786 try popen command []
787 with exn ->
788 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
789 flush stderr;
793 module Ne = struct
794 type 'a t = | Res of 'a | Exn of exn;;
796 let pipe () =
797 try Res (Unix.pipe ())
798 with exn -> Exn exn
801 let clo fd f =
802 try tempfailureretry Unix.close fd
803 with exn -> f (exntos exn)
806 let dup fd =
807 try Res (tempfailureretry Unix.dup fd)
808 with exn -> Exn exn
811 let dup2 fd1 fd2 =
812 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
813 with exn -> Exn exn
815 end;;
817 let redirectstderr () =
818 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
819 if conf.redirectstderr
820 then
821 match Ne.pipe () with
822 | Ne.Exn exn ->
823 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
825 | Ne.Res (r, w) ->
826 begin match Ne.dup Unix.stderr with
827 | Ne.Exn exn ->
828 dolog "failed to dup stderr: %s" (exntos exn);
829 Ne.clo r (clofail "pipe/r");
830 Ne.clo w (clofail "pipe/w");
832 | Ne.Res dupstderr ->
833 begin match Ne.dup2 w Unix.stderr with
834 | Ne.Exn exn ->
835 dolog "failed to dup2 to stderr: %s" (exntos exn);
836 Ne.clo dupstderr (clofail "stderr duplicate");
837 Ne.clo r (clofail "redir pipe/r");
838 Ne.clo w (clofail "redir pipe/w");
840 | Ne.Res () ->
841 state.stderr <- dupstderr;
842 state.errfd <- Some r;
843 end;
845 else (
846 state.newerrmsgs <- false;
847 begin match state.errfd with
848 | Some fd ->
849 begin match Ne.dup2 state.stderr Unix.stderr with
850 | Ne.Exn exn ->
851 dolog "failed to dup2 original stderr: %s" (exntos exn)
852 | Ne.Res () ->
853 Ne.clo fd (clofail "dup of stderr");
854 state.errfd <- None;
855 end;
856 | None -> ()
857 end;
858 prerr_string (Buffer.contents state.errmsgs);
859 flush stderr;
860 Buffer.clear state.errmsgs;
864 module G =
865 struct
866 let postRedisplay who =
867 if conf.verbose
868 then prerr_endline ("redisplay for " ^ who);
869 state.redisplay <- true;
871 end;;
873 let getopaque pageno =
874 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
875 with Not_found -> None
878 let putopaque pageno opaque =
879 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
882 let pagetranslatepoint l x y =
883 let dy = y - l.pagedispy in
884 let y = dy + l.pagey in
885 let dx = x - l.pagedispx in
886 let x = dx + l.pagex in
887 (x, y);
890 let onppundermouse g x y d =
891 let rec f = function
892 | l :: rest ->
893 begin match getopaque l.pageno with
894 | Some opaque ->
895 let x0 = l.pagedispx in
896 let x1 = x0 + l.pagevw in
897 let y0 = l.pagedispy in
898 let y1 = y0 + l.pagevh in
899 if y >= y0 && y <= y1 && x >= x0 && x <= x1
900 then
901 let px, py = pagetranslatepoint l x y in
902 match g opaque l px py with
903 | Some res -> res
904 | None -> f rest
905 else f rest
906 | _ ->
907 f rest
909 | [] -> d
911 f state.layout
914 let getunder x y =
915 let g opaque _ px py =
916 match whatsunder opaque px py with
917 | Unone -> None
918 | under -> Some under
920 onppundermouse g x y Unone
923 let unproject x y =
924 let g opaque l x y =
925 match unproject opaque x y with
926 | Some (x, y) -> Some (Some (l.pageno, x, y))
927 | None -> None
929 onppundermouse g x y None;
932 let showtext c s =
933 state.text <- Printf.sprintf "%c%s" c s;
934 G.postRedisplay "showtext";
937 let paxunder x y =
938 let g opaque l px py =
939 if markunder opaque px py conf.paxmark
940 then (
941 Some (fun () ->
942 match getopaque l.pageno with
943 | None -> ()
944 | Some opaque ->
945 match Ne.pipe () with
946 | Ne.Exn exn ->
947 showtext '!'
948 (Printf.sprintf
949 "can not create mark pipe: %s"
950 (exntos exn));
951 | Ne.Res (r, w) ->
952 let doclose what fd =
953 Ne.clo fd (fun msg ->
954 dolog "%s close failed: %s" what msg)
957 popen conf.paxcmd [r, 0; w, -1];
958 copysel w opaque false;
959 doclose "pipe/r" r;
960 G.postRedisplay "paxunder";
961 with exn ->
962 dolog "can not execute %S: %s"
963 conf.paxcmd (exntos exn);
964 doclose "pipe/r" r;
965 doclose "pipe/w" w;
968 else None
970 G.postRedisplay "paxunder";
971 state.roam <-
972 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
975 let selstring s =
976 match Ne.pipe () with
977 | Ne.Exn exn ->
978 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
979 | Ne.Res (r, w) ->
980 let popened =
981 try popen conf.selcmd [r, 0; w, -1]; true
982 with exn ->
983 showtext '!'
984 (Printf.sprintf "failed to execute %s: %s"
985 conf.selcmd (exntos exn));
986 false
988 let clo cap fd =
989 Ne.clo fd (fun msg ->
990 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
993 if popened
994 then
995 (try
996 let l = String.length s in
997 let n = tempfailureretry (Unix.write w s 0) l in
998 if n != l
999 then
1000 showtext '!'
1001 (Printf.sprintf
1002 "failed to write %d characters to sel pipe, wrote %d"
1005 with exn ->
1006 showtext '!'
1007 (Printf.sprintf "failed to write to sel pipe: %s"
1008 (exntos exn)
1011 else dolog "%s" s;
1012 clo "pipe/r" r;
1013 clo "pipe/w" w;
1016 let undertext = function
1017 | Unone -> "none"
1018 | Ulinkuri s -> s
1019 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1020 | Utext s -> "font: " ^ s
1021 | Uunexpected s -> "unexpected: " ^ s
1022 | Ulaunch s -> "launch: " ^ s
1023 | Unamed s -> "named: " ^ s
1024 | Uremote (filename, pageno) ->
1025 Printf.sprintf "%s: page %d" filename (pageno+1)
1028 let updateunder x y =
1029 match getunder x y with
1030 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1031 | Ulinkuri uri ->
1032 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1033 Wsi.setcursor Wsi.CURSOR_INFO
1034 | Ulinkgoto (pageno, _) ->
1035 if conf.underinfo
1036 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1037 Wsi.setcursor Wsi.CURSOR_INFO
1038 | Utext s ->
1039 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1040 Wsi.setcursor Wsi.CURSOR_TEXT
1041 | Uunexpected s ->
1042 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1043 Wsi.setcursor Wsi.CURSOR_INHERIT
1044 | Ulaunch s ->
1045 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1046 Wsi.setcursor Wsi.CURSOR_INHERIT
1047 | Unamed s ->
1048 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1049 Wsi.setcursor Wsi.CURSOR_INHERIT
1050 | Uremote (filename, pageno) ->
1051 if conf.underinfo then showtext 'r'
1052 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1053 Wsi.setcursor Wsi.CURSOR_INFO
1056 let showlinktype under =
1057 if conf.underinfo
1058 then
1059 match under with
1060 | Unone -> ()
1061 | under ->
1062 let s = undertext under in
1063 showtext ' ' s
1066 let addchar s c =
1067 let b = Buffer.create (String.length s + 1) in
1068 Buffer.add_string b s;
1069 Buffer.add_char b c;
1070 Buffer.contents b;
1073 module type TextEnumType =
1075 type t
1076 val name : string
1077 val names : string array
1078 end;;
1080 module TextEnumMake (Ten : TextEnumType) =
1081 struct
1082 let names = Ten.names;;
1083 let to_int (t : Ten.t) = Obj.magic t;;
1084 let to_string t = names.(to_int t);;
1085 let of_int n : Ten.t = Obj.magic n;;
1086 let of_string s =
1087 let rec find i =
1088 if i = Array.length names
1089 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1090 else (
1091 if Ten.names.(i) = s
1092 then of_int i
1093 else find (i+1)
1095 in find 0;;
1096 end;;
1098 module CSTE = TextEnumMake (struct
1099 type t = colorspace;;
1100 let name = "colorspace";;
1101 let names = [|"rgb"; "bgr"; "gray"|];;
1102 end);;
1104 module MTE = TextEnumMake (struct
1105 type t = mark;;
1106 let name = "mark";;
1107 let names = [|"page"; "block"; "line"; "word"|];;
1108 end);;
1110 module FMTE = TextEnumMake (struct
1111 type t= fitmodel;;
1112 let name = "fitmodel";;
1113 let names = [|"width"; "proportional"; "page"|];;
1114 end);;
1116 let intentry_with_suffix text key =
1117 let c =
1118 if key >= 32 && key < 127
1119 then Char.chr key
1120 else '\000'
1122 match Char.lowercase c with
1123 | '0' .. '9' ->
1124 let text = addchar text c in
1125 TEcont text
1127 | 'k' | 'm' | 'g' ->
1128 let text = addchar text c in
1129 TEcont text
1131 | _ ->
1132 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1133 TEcont text
1136 let multicolumns_to_string (n, a, b) =
1137 if a = 0 && b = 0
1138 then Printf.sprintf "%d" n
1139 else Printf.sprintf "%d,%d,%d" n a b;
1142 let multicolumns_of_string s =
1144 (int_of_string s, 0, 0)
1145 with _ ->
1146 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1147 if a > 1 || b > 1
1148 then failwith "subtly broken"; (n, a, b)
1152 let readcmd fd =
1153 let s = "xxxx" in
1154 let n = tempfailureretry (Unix.read fd s 0) 4 in
1155 if n != 4 then failwith "incomplete read(len)";
1156 let len = 0
1157 lor (Char.code s.[0] lsl 24)
1158 lor (Char.code s.[1] lsl 16)
1159 lor (Char.code s.[2] lsl 8)
1160 lor (Char.code s.[3] lsl 0)
1162 let s = String.create len in
1163 let n = tempfailureretry (Unix.read fd s 0) len in
1164 if n != len then failwith "incomplete read(data)";
1168 let btod b = if b then 1 else 0;;
1170 let wcmd fmt =
1171 let b = Buffer.create 16 in
1172 Buffer.add_string b "llll";
1173 Printf.kbprintf
1174 (fun b ->
1175 let s = Buffer.contents b in
1176 let n = String.length s in
1177 let len = n - 4 in
1178 (* dolog "wcmd %S" (String.sub s 4 len); *)
1179 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1180 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1181 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1182 s.[3] <- Char.chr (len land 0xff);
1183 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1184 if n' != n then failwith "write failed";
1185 ) b fmt;
1188 let calcips h =
1189 let d = state.winh - h in
1190 max conf.interpagespace ((d + 1) / 2)
1193 let rowyh (c, coverA, coverB) b n =
1194 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1195 then
1196 let _, _, vy, (_, _, h, _) = b.(n) in
1197 (vy, h)
1198 else
1199 let n' = n - coverA in
1200 let d = n' mod c in
1201 let s = n - d in
1202 let e = min state.pagecount (s + c) in
1203 let rec find m miny maxh = if m = e then miny, maxh else
1204 let _, _, y, (_, _, h, _) = b.(m) in
1205 let miny = min miny y in
1206 let maxh = max maxh h in
1207 find (m+1) miny maxh
1208 in find s max_int 0
1211 let calcheight () =
1212 match conf.columns with
1213 | Cmulti ((_, _, _) as cl, b) ->
1214 if Array.length b > 0
1215 then
1216 let y, h = rowyh cl b (Array.length b - 1) in
1217 y + h + (if conf.presentation then calcips h else 0)
1218 else 0
1219 | Csingle b ->
1220 if Array.length b > 0
1221 then
1222 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1223 y + h + (if conf.presentation then calcips h else 0)
1224 else 0
1225 | Csplit (_, b) ->
1226 if Array.length b > 0
1227 then
1228 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1229 y + h
1230 else 0
1233 let getpageyh pageno =
1234 let pageno = bound pageno 0 (state.pagecount-1) in
1235 match conf.columns with
1236 | Csingle b ->
1237 if Array.length b = 0
1238 then 0, 0
1239 else
1240 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1241 let y =
1242 if conf.presentation
1243 then y - calcips h
1244 else y
1246 y, h
1247 | Cmulti (cl, b) ->
1248 if Array.length b = 0
1249 then 0, 0
1250 else
1251 let y, h = rowyh cl b pageno in
1252 let y =
1253 if conf.presentation
1254 then y - calcips h
1255 else y
1257 y, h
1258 | Csplit (c, b) ->
1259 if Array.length b = 0
1260 then 0, 0
1261 else
1262 let n = pageno*c in
1263 let (_, _, y, (_, _, h, _)) = b.(n) in
1264 y, h
1267 let getpagedim pageno =
1268 let rec f ppdim l =
1269 match l with
1270 | (n, _, _, _) as pdim :: rest ->
1271 if n >= pageno
1272 then (if n = pageno then pdim else ppdim)
1273 else f pdim rest
1275 | [] -> ppdim
1277 f (-1, -1, -1, -1) state.pdims
1280 let getpagey pageno = fst (getpageyh pageno);;
1282 let nogeomcmds cmds =
1283 match cmds with
1284 | s, [] -> String.length s = 0
1285 | _ -> false
1288 let page_of_y y =
1289 let ((c, coverA, coverB) as cl), b =
1290 match conf.columns with
1291 | Csingle b -> (1, 0, 0), b
1292 | Cmulti (c, b) -> c, b
1293 | Csplit (_, b) -> (1, 0, 0), b
1295 if Array.length b = 0
1296 then -1
1297 else
1298 let rec bsearch nmin nmax =
1299 if nmin > nmax
1300 then bound nmin 0 (state.pagecount-1)
1301 else
1302 let n = (nmax + nmin) / 2 in
1303 let vy, h = rowyh cl b n in
1304 let y0, y1 =
1305 if conf.presentation
1306 then
1307 let ips = calcips h in
1308 let y0 = vy - ips in
1309 let y1 = vy + h + ips in
1310 y0, y1
1311 else (
1312 if n = 0
1313 then 0, vy + h + conf.interpagespace
1314 else
1315 let y0 = vy - conf.interpagespace in
1316 y0, y0 + h + conf.interpagespace
1319 if y >= y0 && y < y1
1320 then (
1321 if c = 1
1322 then n
1323 else (
1324 if n > coverA
1325 then
1326 if n < state.pagecount - coverB
1327 then ((n-coverA)/c)*c + coverA
1328 else n
1329 else n
1332 else (
1333 if y > y0
1334 then bsearch (n+1) nmax
1335 else bsearch nmin (n-1)
1338 let r = bsearch 0 (state.pagecount-1) in
1342 let layoutN ((columns, coverA, coverB), b) y sh =
1343 let sh = sh - (hscrollh ()) in
1344 let rec fold accu n =
1345 if n = Array.length b
1346 then accu
1347 else
1348 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1349 if (vy - y) > sh &&
1350 (n = coverA - 1
1351 || n = state.pagecount - coverB
1352 || (n - coverA) mod columns = columns - 1)
1353 then accu
1354 else
1355 let accu =
1356 if vy + h > y
1357 then
1358 let pagey = max 0 (y - vy) in
1359 let pagedispy = if pagey > 0 then 0 else vy - y in
1360 let pagedispx, pagex =
1361 let pdx =
1362 if n = coverA - 1 || n = state.pagecount - coverB
1363 then state.x + (wadjsb state.winw - w) / 2
1364 else dx + xoff + state.x
1366 if pdx < 0
1367 then 0, -pdx
1368 else pdx, 0
1370 let pagevw =
1371 let vw = wadjsb state.winw - pagedispx in
1372 let pw = w - pagex in
1373 min vw pw
1375 let pagevh = min (h - pagey) (sh - pagedispy) in
1376 if pagevw > 0 && pagevh > 0
1377 then
1378 let e =
1379 { pageno = n
1380 ; pagedimno = pdimno
1381 ; pagew = w
1382 ; pageh = h
1383 ; pagex = pagex
1384 ; pagey = pagey
1385 ; pagevw = pagevw
1386 ; pagevh = pagevh
1387 ; pagedispx = pagedispx
1388 ; pagedispy = pagedispy
1389 ; pagecol = 0
1392 e :: accu
1393 else
1394 accu
1395 else
1396 accu
1398 fold accu (n+1)
1400 List.rev (fold [] (page_of_y y));
1403 let layoutS (columns, b) y sh =
1404 let sh = sh - hscrollh () in
1405 let rec fold accu n =
1406 if n = Array.length b
1407 then accu
1408 else
1409 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1410 if (vy - y) > sh
1411 then accu
1412 else
1413 let accu =
1414 if vy + pageh > y
1415 then
1416 let x = xoff + state.x in
1417 let pagey = max 0 (y - vy) in
1418 let pagedispy = if pagey > 0 then 0 else vy - y in
1419 let pagedispx, pagex =
1420 if px = 0
1421 then (
1422 if x < 0
1423 then 0, -x
1424 else x, 0
1426 else (
1427 let px = px - x in
1428 if px < 0
1429 then -px, 0
1430 else 0, px
1433 let pagecolw = pagew/columns in
1434 let pagedispx =
1435 if pagecolw < state.winw
1436 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1437 else pagedispx
1439 let pagevw =
1440 let vw = wadjsb state.winw - pagedispx in
1441 let pw = pagew - pagex in
1442 min vw pw
1444 let pagevw = min pagevw pagecolw in
1445 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1446 if pagevw > 0 && pagevh > 0
1447 then
1448 let e =
1449 { pageno = n/columns
1450 ; pagedimno = pdimno
1451 ; pagew = pagew
1452 ; pageh = pageh
1453 ; pagex = pagex
1454 ; pagey = pagey
1455 ; pagevw = pagevw
1456 ; pagevh = pagevh
1457 ; pagedispx = pagedispx
1458 ; pagedispy = pagedispy
1459 ; pagecol = n mod columns
1462 e :: accu
1463 else
1464 accu
1465 else
1466 accu
1468 fold accu (n+1)
1470 List.rev (fold [] 0)
1473 let layout y sh =
1474 if nogeomcmds state.geomcmds
1475 then
1476 match conf.columns with
1477 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1478 | Cmulti c -> layoutN c y sh
1479 | Csplit s -> layoutS s y sh
1480 else []
1483 let clamp incr =
1484 let y = state.y + incr in
1485 let y = max 0 y in
1486 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1490 let itertiles l f =
1491 let tilex = l.pagex mod conf.tilew in
1492 let tiley = l.pagey mod conf.tileh in
1494 let col = l.pagex / conf.tilew in
1495 let row = l.pagey / conf.tileh in
1497 let rec rowloop row y0 dispy h =
1498 if h = 0
1499 then ()
1500 else (
1501 let dh = conf.tileh - y0 in
1502 let dh = min h dh in
1503 let rec colloop col x0 dispx w =
1504 if w = 0
1505 then ()
1506 else (
1507 let dw = conf.tilew - x0 in
1508 let dw = min w dw in
1510 f col row dispx dispy x0 y0 dw dh;
1511 colloop (col+1) 0 (dispx+dw) (w-dw)
1514 colloop col tilex l.pagedispx l.pagevw;
1515 rowloop (row+1) 0 (dispy+dh) (h-dh)
1518 if l.pagevw > 0 && l.pagevh > 0
1519 then rowloop row tiley l.pagedispy l.pagevh;
1522 let gettileopaque l col row =
1523 let key =
1524 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1526 try Some (Hashtbl.find state.tilemap key)
1527 with Not_found -> None
1530 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1531 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1532 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1535 let drawtiles l color =
1536 GlDraw.color color;
1537 let f col row x y tilex tiley w h =
1538 match gettileopaque l col row with
1539 | Some (opaque, _, t) ->
1540 let params = x, y, w, h, tilex, tiley in
1541 if conf.invert
1542 then (
1543 Gl.enable `blend;
1544 GlFunc.blend_func `zero `one_minus_src_color;
1546 drawtile params opaque;
1547 if conf.invert
1548 then Gl.disable `blend;
1549 if conf.debug
1550 then (
1551 let s = Printf.sprintf
1552 "%d[%d,%d] %f sec"
1553 l.pageno col row t
1555 let w = measurestr fstate.fontsize s in
1556 GlMisc.push_attrib [`current];
1557 GlDraw.color (0.0, 0.0, 0.0);
1558 GlDraw.rect
1559 (float (x-2), float (y-2))
1560 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1561 GlDraw.color (1.0, 1.0, 1.0);
1562 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1563 GlMisc.pop_attrib ();
1566 | _ ->
1567 let w =
1568 let lw = wadjsb state.winw - x in
1569 min lw w
1570 and h =
1571 let lh = state.winh - y in
1572 min lh h
1574 begin match state.texid with
1575 | Some id ->
1576 Gl.enable `texture_2d;
1577 GlTex.bind_texture `texture_2d id;
1578 let x0 = float x
1579 and y0 = float y
1580 and x1 = float (x+w)
1581 and y1 = float (y+h) in
1583 let tw = float w /. 16.0
1584 and th = float h /. 16.0 in
1585 let tx0 = float tilex /. 16.0
1586 and ty0 = float tiley /. 16.0 in
1587 let tx1 = tx0 +. tw
1588 and ty1 = ty0 +. th in
1589 GlDraw.begins `quads;
1590 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1591 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1592 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1593 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1594 GlDraw.ends ();
1596 Gl.disable `texture_2d;
1597 | None ->
1598 GlDraw.color (1.0, 1.0, 1.0);
1599 GlDraw.rect
1600 (float x, float y)
1601 (float (x+w), float (y+h));
1602 end;
1603 if w > 128 && h > fstate.fontsize + 10
1604 then (
1605 GlDraw.color (0.0, 0.0, 0.0);
1606 let c, r =
1607 if conf.verbose
1608 then (col*conf.tilew, row*conf.tileh)
1609 else col, row
1611 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1613 GlDraw.color color;
1615 itertiles l f
1618 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1620 let tilevisible1 l x y =
1621 let ax0 = l.pagex
1622 and ax1 = l.pagex + l.pagevw
1623 and ay0 = l.pagey
1624 and ay1 = l.pagey + l.pagevh in
1626 let bx0 = x
1627 and by0 = y in
1628 let bx1 = min (bx0 + conf.tilew) l.pagew
1629 and by1 = min (by0 + conf.tileh) l.pageh in
1631 let rx0 = max ax0 bx0
1632 and ry0 = max ay0 by0
1633 and rx1 = min ax1 bx1
1634 and ry1 = min ay1 by1 in
1636 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1637 nonemptyintersection
1640 let tilevisible layout n x y =
1641 let rec findpageinlayout m = function
1642 | l :: rest when l.pageno = n ->
1643 tilevisible1 l x y || (
1644 match conf.columns with
1645 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1646 | _ -> false
1648 | _ :: rest -> findpageinlayout 0 rest
1649 | [] -> false
1651 findpageinlayout 0 layout;
1654 let tileready l x y =
1655 tilevisible1 l x y &&
1656 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1659 let tilepage n p layout =
1660 let rec loop = function
1661 | l :: rest ->
1662 if l.pageno = n
1663 then
1664 let f col row _ _ _ _ _ _ =
1665 if state.currently = Idle
1666 then
1667 match gettileopaque l col row with
1668 | Some _ -> ()
1669 | None ->
1670 let x = col*conf.tilew
1671 and y = row*conf.tileh in
1672 let w =
1673 let w = l.pagew - x in
1674 min w conf.tilew
1676 let h =
1677 let h = l.pageh - y in
1678 min h conf.tileh
1680 let pbo =
1681 if conf.usepbo
1682 then getpbo w h conf.colorspace
1683 else "0"
1685 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1686 state.currently <-
1687 Tiling (
1688 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1689 conf.tilew, conf.tileh
1692 itertiles l f;
1693 else
1694 loop rest
1696 | [] -> ()
1698 if nogeomcmds state.geomcmds
1699 then loop layout;
1702 let preloadlayout y =
1703 let y = if y < state.winh then 0 else y - state.winh in
1704 let h = state.winh*3 in
1705 layout y h;
1708 let load pages =
1709 let rec loop pages =
1710 if state.currently != Idle
1711 then ()
1712 else
1713 match pages with
1714 | l :: rest ->
1715 begin match getopaque l.pageno with
1716 | None ->
1717 wcmd "page %d %d" l.pageno l.pagedimno;
1718 state.currently <- Loading (l, state.gen);
1719 | Some opaque ->
1720 tilepage l.pageno opaque pages;
1721 loop rest
1722 end;
1723 | _ -> ()
1725 if nogeomcmds state.geomcmds
1726 then loop pages
1729 let preload pages =
1730 load pages;
1731 if conf.preload && state.currently = Idle
1732 then load (preloadlayout state.y);
1735 let layoutready layout =
1736 let rec fold all ls =
1737 all && match ls with
1738 | l :: rest ->
1739 let seen = ref false in
1740 let allvisible = ref true in
1741 let foo col row _ _ _ _ _ _ =
1742 seen := true;
1743 allvisible := !allvisible &&
1744 begin match gettileopaque l col row with
1745 | Some _ -> true
1746 | None -> false
1749 itertiles l foo;
1750 fold (!seen && !allvisible) rest
1751 | [] -> true
1753 let alltilesvisible = fold true layout in
1754 alltilesvisible;
1757 let gotoy y =
1758 let y = bound y 0 state.maxy in
1759 let y, layout, proceed =
1760 match conf.maxwait with
1761 | Some time when state.ghyll == noghyll ->
1762 begin match state.throttle with
1763 | None ->
1764 let layout = layout y state.winh in
1765 let ready = layoutready layout in
1766 if not ready
1767 then (
1768 load layout;
1769 state.throttle <- Some (layout, y, now ());
1771 else G.postRedisplay "gotoy showall (None)";
1772 y, layout, ready
1773 | Some (_, _, started) ->
1774 let dt = now () -. started in
1775 if dt > time
1776 then (
1777 state.throttle <- None;
1778 let layout = layout y state.winh in
1779 load layout;
1780 G.postRedisplay "maxwait";
1781 y, layout, true
1783 else -1, [], false
1786 | _ ->
1787 let layout = layout y state.winh in
1788 if not !wtmode || layoutready layout
1789 then G.postRedisplay "gotoy ready";
1790 y, layout, true
1792 if proceed
1793 then (
1794 state.y <- y;
1795 state.layout <- layout;
1796 begin match state.mode with
1797 | LinkNav (Ltexact (pageno, linkno)) ->
1798 let rec loop = function
1799 | [] ->
1800 state.mode <- LinkNav (Ltgendir 0)
1801 | l :: _ when l.pageno = pageno ->
1802 begin match getopaque pageno with
1803 | None ->
1804 state.mode <- LinkNav (Ltgendir 0)
1805 | Some opaque ->
1806 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1807 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1808 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1809 then state.mode <- LinkNav (Ltgendir 0)
1811 | _ :: rest -> loop rest
1813 loop layout
1814 | _ -> ()
1815 end;
1816 begin match state.mode with
1817 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1818 if not (pagevisible layout pageno)
1819 then (
1820 match state.layout with
1821 | [] -> ()
1822 | l :: _ ->
1823 state.mode <- Birdseye (
1824 conf, leftx, l.pageno, hooverpageno, anchor
1827 | LinkNav (Ltgendir dir as lt) ->
1828 let linknav =
1829 let rec loop = function
1830 | [] -> lt
1831 | l :: rest ->
1832 match getopaque l.pageno with
1833 | None -> loop rest
1834 | Some opaque ->
1835 let link =
1836 let ld =
1837 if dir = 0
1838 then LDfirstvisible (l.pagex, l.pagey, dir)
1839 else (
1840 if dir > 0 then LDfirst else LDlast
1843 findlink opaque ld
1845 match link with
1846 | Lnotfound -> loop rest
1847 | Lfound n ->
1848 showlinktype (getlink opaque n);
1849 Ltexact (l.pageno, n)
1851 loop state.layout
1853 state.mode <- LinkNav linknav
1854 | _ -> ()
1855 end;
1856 preload layout;
1858 state.ghyll <- noghyll;
1859 if conf.updatecurs
1860 then (
1861 let mx, my = state.mpos in
1862 updateunder mx my;
1866 let conttiling pageno opaque =
1867 tilepage pageno opaque
1868 (if conf.preload then preloadlayout state.y else state.layout)
1871 let gotoy_and_clear_text y =
1872 if not conf.verbose then state.text <- "";
1873 gotoy y;
1876 let getanchor1 l =
1877 let top =
1878 let coloff = l.pagecol * l.pageh in
1879 float (l.pagey + coloff) /. float l.pageh
1881 let dtop =
1882 if l.pagedispy = 0
1883 then
1885 else (
1886 if conf.presentation
1887 then float l.pagedispy /. float (calcips l.pageh)
1888 else float l.pagedispy /. float conf.interpagespace
1891 (l.pageno, top, dtop)
1894 let getanchor () =
1895 match state.layout with
1896 | l :: _ -> getanchor1 l
1897 | [] ->
1898 let n = page_of_y state.y in
1899 if n = -1
1900 then state.anchor
1901 else
1902 let y, h = getpageyh n in
1903 let dy = y - state.y in
1904 let dtop =
1905 if conf.presentation
1906 then
1907 let ips = calcips h in
1908 float (dy + ips) /. float ips
1909 else
1910 float dy /. float conf.interpagespace
1912 (n, 0.0, dtop)
1915 let getanchory (n, top, dtop) =
1916 let y, h = getpageyh n in
1917 if conf.presentation
1918 then
1919 let ips = calcips h in
1920 y + truncate (top*.float h -. dtop*.float ips) + ips;
1921 else
1922 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1925 let gotoanchor anchor =
1926 gotoy (getanchory anchor);
1929 let addnav () =
1930 cbput state.hists.nav (getanchor ());
1933 let getnav dir =
1934 let anchor = cbgetc state.hists.nav dir in
1935 getanchory anchor;
1938 let gotoghyll y =
1939 let scroll f n a b =
1940 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1941 let snake f a b =
1942 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1943 if f < a
1944 then s (float f /. float a)
1945 else (
1946 if f > b
1947 then 1.0 -. s ((float (f-b) /. float (n-b)))
1948 else 1.0
1951 snake f a b
1952 and summa f n a b =
1953 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1954 let iv x = x**3.-.0.5*.x**4. in
1955 let iv1 = iv f in
1956 let ins = float a *. iv1
1957 and outs = float (n-b) *. iv1 in
1958 let ones = b - a in
1959 ins +. outs +. float ones
1961 let rec set (_N, _A, _B) y sy =
1962 let sum = summa 1.0 _N _A _B in
1963 let dy = float (y - sy) in
1964 state.ghyll <- (
1965 let rec gf n y1 o =
1966 if n >= _N
1967 then state.ghyll <- noghyll
1968 else
1969 let go n =
1970 let s = scroll n _N _A _B in
1971 let y1 = y1 +. ((s *. dy) /. sum) in
1972 gotoy_and_clear_text (truncate y1);
1973 state.ghyll <- gf (n+1) y1;
1975 match o with
1976 | None -> go n
1977 | Some y' -> set (_N/2, 1, 1) y' state.y
1979 gf 0 (float state.y)
1982 match conf.ghyllscroll with
1983 | None ->
1984 gotoy_and_clear_text y
1985 | Some nab ->
1986 if state.ghyll == noghyll
1987 then set nab y state.y
1988 else state.ghyll (Some y)
1991 let gotopage n top =
1992 let y, h = getpageyh n in
1993 let y = y + (truncate (top *. float h)) in
1994 gotoghyll y
1997 let gotopage1 n top =
1998 let y = getpagey n in
1999 let y = y + top in
2000 gotoghyll y
2003 let invalidate s f =
2004 state.layout <- [];
2005 state.pdims <- [];
2006 state.rects <- [];
2007 state.rects1 <- [];
2008 match state.geomcmds with
2009 | ps, [] when String.length ps = 0 ->
2010 f ();
2011 state.geomcmds <- s, [];
2013 | ps, [] ->
2014 state.geomcmds <- ps, [s, f];
2016 | ps, (s', _) :: rest when s' = s ->
2017 state.geomcmds <- ps, ((s, f) :: rest);
2019 | ps, cmds ->
2020 state.geomcmds <- ps, ((s, f) :: cmds);
2023 let flushpages () =
2024 Hashtbl.iter (fun _ opaque ->
2025 wcmd "freepage %s" opaque;
2026 ) state.pagemap;
2027 Hashtbl.clear state.pagemap;
2030 let flushtiles () =
2031 if not (Queue.is_empty state.tilelru)
2032 then (
2033 Queue.iter (fun (k, p, s) ->
2034 wcmd "freetile %s" p;
2035 state.memused <- state.memused - s;
2036 Hashtbl.remove state.tilemap k;
2037 ) state.tilelru;
2038 state.uioh#infochanged Memused;
2039 Queue.clear state.tilelru;
2041 load state.layout;
2044 let stateh h =
2045 let h = truncate (float h*.conf.zoom) in
2046 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2047 h - d
2050 let opendoc path password =
2051 state.path <- path;
2052 state.password <- password;
2053 state.gen <- state.gen + 1;
2054 state.docinfo <- [];
2056 flushpages ();
2057 setaalevel conf.aalevel;
2058 let titlepath =
2059 if String.length state.origin = 0
2060 then path
2061 else state.origin
2063 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2064 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2065 invalidate "reqlayout"
2066 (fun () ->
2067 wcmd "reqlayout %d %d %d %s\000"
2068 conf.angle (FMTE.to_int conf.fitmodel)
2069 (stateh state.winh) state.nameddest
2073 let reload () =
2074 state.anchor <- getanchor ();
2075 opendoc state.path state.password;
2078 let scalecolor c =
2079 let c = c *. conf.colorscale in
2080 (c, c, c);
2083 let scalecolor2 (r, g, b) =
2084 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2087 let docolumns = function
2088 | Csingle _ ->
2089 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2090 let rec loop pageno pdimno pdim y ph pdims =
2091 if pageno = state.pagecount
2092 then ()
2093 else
2094 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2095 match pdims with
2096 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2097 pdimno+1, pdim, rest
2098 | _ ->
2099 pdimno, pdim, pdims
2101 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2102 let y = y +
2103 (if conf.presentation
2104 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2105 else (if pageno = 0 then 0 else conf.interpagespace)
2108 a.(pageno) <- (pdimno, x, y, pdim);
2109 loop (pageno+1) pdimno pdim (y + h) h pdims
2111 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2112 conf.columns <- Csingle a;
2114 | Cmulti ((columns, coverA, coverB), _) ->
2115 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2116 let rec loop pageno pdimno pdim x y rowh pdims =
2117 let rec fixrow m = if m = pageno then () else
2118 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2119 if h < rowh
2120 then (
2121 let y = y + (rowh - h) / 2 in
2122 a.(m) <- (pdimno, x, y, pdim);
2124 fixrow (m+1)
2126 if pageno = state.pagecount
2127 then fixrow (((pageno - 1) / columns) * columns)
2128 else
2129 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2130 match pdims with
2131 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2132 pdimno+1, pdim, rest
2133 | _ ->
2134 pdimno, pdim, pdims
2136 let x, y, rowh' =
2137 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2138 then (
2139 let x = (wadjsb state.winw - w) / 2 in
2140 let ips =
2141 if conf.presentation then calcips h else conf.interpagespace in
2142 x, y + ips + rowh, h
2144 else (
2145 if (pageno - coverA) mod columns = 0
2146 then (
2147 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2148 let y =
2149 if conf.presentation
2150 then
2151 let ips = calcips h in
2152 y + (if pageno = 0 then 0 else calcips rowh + ips)
2153 else
2154 y + (if pageno = 0 then 0 else conf.interpagespace)
2156 x, y + rowh, h
2158 else x, y, max rowh h
2161 let y =
2162 if pageno > 1 && (pageno - coverA) mod columns = 0
2163 then (
2164 let y =
2165 if pageno = columns && conf.presentation
2166 then (
2167 let ips = calcips rowh in
2168 for i = 0 to pred columns
2170 let (pdimno, x, y, pdim) = a.(i) in
2171 a.(i) <- (pdimno, x, y+ips, pdim)
2172 done;
2173 y+ips;
2175 else y
2177 fixrow (pageno - columns);
2180 else y
2182 a.(pageno) <- (pdimno, x, y, pdim);
2183 let x = x + w + xoff*2 + conf.interpagespace in
2184 loop (pageno+1) pdimno pdim x y rowh' pdims
2186 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2187 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2189 | Csplit (c, _) ->
2190 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2191 let rec loop pageno pdimno pdim y pdims =
2192 if pageno = state.pagecount
2193 then ()
2194 else
2195 let pdimno, ((_, w, h, _) as pdim), pdims =
2196 match pdims with
2197 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2198 pdimno+1, pdim, rest
2199 | _ ->
2200 pdimno, pdim, pdims
2202 let cw = w / c in
2203 let rec loop1 n x y =
2204 if n = c then y else (
2205 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2206 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2209 let y = loop1 0 0 y in
2210 loop (pageno+1) pdimno pdim y pdims
2212 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2213 conf.columns <- Csplit (c, a);
2216 let represent () =
2217 docolumns conf.columns;
2218 state.maxy <- calcheight ();
2219 if state.reprf == noreprf
2220 then (
2221 match state.mode with
2222 | Birdseye (_, _, pageno, _, _) ->
2223 let y, h = getpageyh pageno in
2224 let top = (state.winh - h) / 2 in
2225 gotoy (max 0 (y - top))
2226 | _ -> gotoanchor state.anchor
2228 else (
2229 state.reprf ();
2230 state.reprf <- noreprf;
2234 let reshape w h =
2235 GlDraw.viewport 0 0 w h;
2236 let firsttime = state.geomcmds == firstgeomcmds in
2237 if not firsttime && nogeomcmds state.geomcmds
2238 then state.anchor <- getanchor ();
2240 state.winw <- w;
2241 let w = wadjsb (truncate (float w *. conf.zoom)) in
2242 let w = max w 2 in
2243 state.winh <- h;
2244 setfontsize fstate.fontsize;
2245 GlMat.mode `modelview;
2246 GlMat.load_identity ();
2248 GlMat.mode `projection;
2249 GlMat.load_identity ();
2250 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2251 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2252 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2254 let relx =
2255 if conf.zoom <= 1.0
2256 then 0.0
2257 else float state.x /. float state.w
2259 invalidate "geometry"
2260 (fun () ->
2261 state.w <- w;
2262 if not firsttime
2263 then state.x <- truncate (relx *. float w);
2264 let w =
2265 match conf.columns with
2266 | Csingle _ -> w
2267 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2268 | Csplit (c, _) -> w * c
2270 wcmd "geometry %d %d %d"
2271 w (stateh h) (FMTE.to_int conf.fitmodel)
2275 let enttext () =
2276 let len = String.length state.text in
2277 let drawstring s =
2278 let hscrollh =
2279 match state.mode with
2280 | Textentry _ | View | LinkNav _ ->
2281 let h, _, _ = state.uioh#scrollpw in
2283 | _ -> 0
2285 let rect x w =
2286 GlDraw.rect
2287 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2288 (x+.w, float (state.winh - hscrollh))
2291 let w = float (wadjsb state.winw - 1) in
2292 if state.progress >= 0.0 && state.progress < 1.0
2293 then (
2294 GlDraw.color (0.3, 0.3, 0.3);
2295 let w1 = w *. state.progress in
2296 rect 0.0 w1;
2297 GlDraw.color (0.0, 0.0, 0.0);
2298 rect w1 (w-.w1)
2300 else (
2301 GlDraw.color (0.0, 0.0, 0.0);
2302 rect 0.0 w;
2305 GlDraw.color (1.0, 1.0, 1.0);
2306 drawstring fstate.fontsize
2307 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2309 let s =
2310 match state.mode with
2311 | Textentry ((prefix, text, _, _, _, _), _) ->
2312 let s =
2313 if len > 0
2314 then
2315 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2316 else
2317 Printf.sprintf "%s%s_" prefix text
2321 | _ -> state.text
2323 let s =
2324 if state.newerrmsgs
2325 then (
2326 if not (istextentry state.mode) && state.uioh#eformsgs
2327 then
2328 let s1 = "(press 'e' to review error messasges)" in
2329 if String.length s > 0 then s ^ " " ^ s1 else s1
2330 else s
2332 else s
2334 if String.length s > 0
2335 then drawstring s
2338 let gctiles () =
2339 let len = Queue.length state.tilelru in
2340 let layout = lazy (
2341 match state.throttle with
2342 | None ->
2343 if conf.preload
2344 then preloadlayout state.y
2345 else state.layout
2346 | Some (layout, _, _) ->
2347 layout
2348 ) in
2349 let rec loop qpos =
2350 if state.memused <= conf.memlimit
2351 then ()
2352 else (
2353 if qpos < len
2354 then
2355 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2356 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2357 let (_, pw, ph, _) = getpagedim n in
2359 gen = state.gen
2360 && colorspace = conf.colorspace
2361 && angle = conf.angle
2362 && pagew = pw
2363 && pageh = ph
2364 && (
2365 let x = col*conf.tilew
2366 and y = row*conf.tileh in
2367 tilevisible (Lazy.force_val layout) n x y
2369 then Queue.push lruitem state.tilelru
2370 else (
2371 freepbo p;
2372 wcmd "freetile %s" p;
2373 state.memused <- state.memused - s;
2374 state.uioh#infochanged Memused;
2375 Hashtbl.remove state.tilemap k;
2377 loop (qpos+1)
2380 loop 0
2383 let logcurrently = function
2384 | Idle -> dolog "Idle"
2385 | Loading (l, gen) ->
2386 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2387 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2388 dolog
2389 "Tiling %d[%d,%d] page=%s cs=%s angle"
2390 l.pageno col row pageopaque
2391 (CSTE.to_string colorspace)
2393 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2394 angle gen conf.angle state.gen
2395 tilew tileh
2396 conf.tilew conf.tileh
2398 | Outlining _ ->
2399 dolog "outlining"
2402 let splitatspace =
2403 let r = Str.regexp " " in
2404 fun s -> Str.bounded_split r s 2;
2407 let onpagerect pageno f =
2408 let b =
2409 match conf.columns with
2410 | Cmulti (_, b) -> b
2411 | Csingle b -> b
2412 | Csplit (_, b) -> b
2414 if pageno >= 0 && pageno < Array.length b
2415 then
2416 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2417 let r = getpdimrect pdimno in
2418 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2421 let gotopagexy1 pageno x y =
2422 onpagerect pageno (fun w h ->
2423 let top = y /. h in
2424 let _,w1,_,leftx = getpagedim pageno in
2425 let wh = state.winh - hscrollh () in
2426 let sw = float w1 /. w in
2427 let x = sw *. x in
2428 let x = leftx + state.x + truncate x in
2429 let sx =
2430 if x < 0 || x >= wadjsb state.winw
2431 then state.x - x
2432 else state.x
2434 let py, h = getpageyh pageno in
2435 let pdy = truncate (top *. float h) in
2436 let y' = py + pdy in
2437 let dy = y' - state.y in
2438 let sy =
2439 if x != state.x || not (dy > 0 && dy < wh)
2440 then (
2441 if conf.presentation
2442 then
2443 if abs (py - y') > wh
2444 then y'
2445 else py
2446 else y';
2448 else state.y
2450 if state.x != sx || state.y != sy
2451 then (
2452 let x, y =
2453 if !wtmode
2454 then (
2455 let ww = wadjsb state.winw in
2456 let qx = sx / ww
2457 and qy = pdy / wh in
2458 let x = qx * ww
2459 and y = py + qy * wh in
2460 let x = if -x + ww > w1 then -(w1-ww) else x
2461 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2462 let y =
2463 if conf.presentation
2464 then
2465 if abs (py - y') > wh
2466 then y'
2467 else py
2468 else y';
2470 (x, y)
2472 else (sx, sy)
2474 state.x <- x;
2475 gotoy_and_clear_text y;
2477 else gotoy_and_clear_text state.y;
2481 let gotopagexy pageno x y =
2482 match state.mode with
2483 | Birdseye _ -> gotopage pageno 0.0
2484 | _ -> gotopagexy1 pageno x y
2487 let act cmds =
2488 (* dolog "%S" cmds; *)
2489 let cl = splitatspace cmds in
2490 let scan s fmt f =
2491 try Scanf.sscanf s fmt f
2492 with exn ->
2493 dolog "error processing '%S': %s" cmds (exntos exn);
2494 exit 1
2496 match cl with
2497 | "clear" :: [] ->
2498 state.uioh#infochanged Pdim;
2499 state.pdims <- [];
2501 | "clearrects" :: [] ->
2502 state.rects <- state.rects1;
2503 G.postRedisplay "clearrects";
2505 | "continue" :: args :: [] ->
2506 let n = scan args "%u" (fun n -> n) in
2507 state.pagecount <- n;
2508 begin match state.currently with
2509 | Outlining l ->
2510 state.currently <- Idle;
2511 state.outlines <- Array.of_list (List.rev l)
2512 | _ -> ()
2513 end;
2515 let cur, cmds = state.geomcmds in
2516 if String.length cur = 0
2517 then failwith "umpossible";
2519 begin match List.rev cmds with
2520 | [] ->
2521 state.geomcmds <- "", [];
2522 represent ();
2523 | (s, f) :: rest ->
2524 f ();
2525 state.geomcmds <- s, List.rev rest;
2526 end;
2527 if conf.maxwait = None && not !wtmode
2528 then G.postRedisplay "continue";
2530 | "title" :: args :: [] ->
2531 Wsi.settitle args
2533 | "msg" :: args :: [] ->
2534 showtext ' ' args
2536 | "vmsg" :: args :: [] ->
2537 if conf.verbose
2538 then showtext ' ' args
2540 | "emsg" :: args :: [] ->
2541 Buffer.add_string state.errmsgs args;
2542 state.newerrmsgs <- true;
2543 G.postRedisplay "error message"
2545 | "progress" :: args :: [] ->
2546 let progress, text =
2547 scan args "%f %n"
2548 (fun f pos ->
2549 f, String.sub args pos (String.length args - pos))
2551 state.text <- text;
2552 state.progress <- progress;
2553 G.postRedisplay "progress"
2555 | "firstmatch" :: args :: [] ->
2556 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2557 scan args "%u %d %f %f %f %f %f %f %f %f"
2558 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2559 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2561 let y = (getpagey pageno) + truncate y0 in
2562 addnav ();
2563 gotoy y;
2564 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2566 | "match" :: args :: [] ->
2567 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2568 scan args "%u %d %f %f %f %f %f %f %f %f"
2569 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2570 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2572 state.rects1 <-
2573 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2575 | "page" :: args :: [] ->
2576 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2577 begin match state.currently with
2578 | Loading (l, gen) ->
2579 vlog "page %d took %f sec" l.pageno t;
2580 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2581 begin match state.throttle with
2582 | None ->
2583 let preloadedpages =
2584 if conf.preload
2585 then preloadlayout state.y
2586 else state.layout
2588 let evict () =
2589 let set =
2590 List.fold_left (fun s l -> IntSet.add l.pageno s)
2591 IntSet.empty preloadedpages
2593 let evictedpages =
2594 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2595 if not (IntSet.mem pageno set)
2596 then (
2597 wcmd "freepage %s" opaque;
2598 key :: accu
2600 else accu
2601 ) state.pagemap []
2603 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2605 evict ();
2606 state.currently <- Idle;
2607 if gen = state.gen
2608 then (
2609 tilepage l.pageno pageopaque state.layout;
2610 load state.layout;
2611 load preloadedpages;
2612 if pagevisible state.layout l.pageno
2613 && layoutready state.layout
2614 then G.postRedisplay "page";
2617 | Some (layout, _, _) ->
2618 state.currently <- Idle;
2619 tilepage l.pageno pageopaque layout;
2620 load state.layout
2621 end;
2623 | _ ->
2624 dolog "Inconsistent loading state";
2625 logcurrently state.currently;
2626 exit 1
2629 | "tile" :: args :: [] ->
2630 let (x, y, opaque, size, t) =
2631 scan args "%u %u %s %u %f"
2632 (fun x y p size t -> (x, y, p, size, t))
2634 begin match state.currently with
2635 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2636 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2638 unmappbo opaque;
2639 if tilew != conf.tilew || tileh != conf.tileh
2640 then (
2641 wcmd "freetile %s" opaque;
2642 state.currently <- Idle;
2643 load state.layout;
2645 else (
2646 puttileopaque l col row gen cs angle opaque size t;
2647 state.memused <- state.memused + size;
2648 state.uioh#infochanged Memused;
2649 gctiles ();
2650 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2651 opaque, size) state.tilelru;
2653 let layout =
2654 match state.throttle with
2655 | None -> state.layout
2656 | Some (layout, _, _) -> layout
2659 state.currently <- Idle;
2660 if gen = state.gen
2661 && conf.colorspace = cs
2662 && conf.angle = angle
2663 && tilevisible layout l.pageno x y
2664 then conttiling l.pageno pageopaque;
2666 begin match state.throttle with
2667 | None ->
2668 preload state.layout;
2669 if gen = state.gen
2670 && conf.colorspace = cs
2671 && conf.angle = angle
2672 && tilevisible state.layout l.pageno x y
2673 && (not !wtmode || layoutready state.layout)
2674 then G.postRedisplay "tile nothrottle";
2676 | Some (layout, y, _) ->
2677 let ready = layoutready layout in
2678 if ready
2679 then (
2680 state.y <- y;
2681 state.layout <- layout;
2682 state.throttle <- None;
2683 G.postRedisplay "throttle";
2685 else load layout;
2686 end;
2689 | _ ->
2690 dolog "Inconsistent tiling state";
2691 logcurrently state.currently;
2692 exit 1
2695 | "pdim" :: args :: [] ->
2696 let (n, w, h, _) as pdim =
2697 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2699 let pdim =
2700 match conf.fitmodel, conf.columns with
2701 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2702 | _ -> pdim
2704 state.uioh#infochanged Pdim;
2705 state.pdims <- pdim :: state.pdims
2707 | "o" :: args :: [] ->
2708 let (l, n, t, h, pos) =
2709 scan args "%u %u %d %u %n"
2710 (fun l n t h pos -> l, n, t, h, pos)
2712 let s = String.sub args pos (String.length args - pos) in
2713 let outline = (s, l, (n, float t /. float h, 0.0)) in
2714 begin match state.currently with
2715 | Outlining outlines ->
2716 state.currently <- Outlining (outline :: outlines)
2717 | Idle ->
2718 state.currently <- Outlining [outline]
2719 | currently ->
2720 dolog "invalid outlining state";
2721 logcurrently currently
2724 | "a" :: args :: [] ->
2725 let (n, l, t) =
2726 scan args "%u %d %d" (fun n l t -> n, l, t)
2728 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2730 | "info" :: args :: [] ->
2731 state.docinfo <- (1, args) :: state.docinfo
2733 | "infoend" :: [] ->
2734 state.uioh#infochanged Docinfo;
2735 state.docinfo <- List.rev state.docinfo
2737 | _ ->
2738 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2741 let onhist cb =
2742 let rc = cb.rc in
2743 let action = function
2744 | HCprev -> cbget cb ~-1
2745 | HCnext -> cbget cb 1
2746 | HCfirst -> cbget cb ~-(cb.rc)
2747 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2748 and cancel () = cb.rc <- rc
2749 in (action, cancel)
2752 let search pattern forward =
2753 match conf.columns with
2754 | Csplit _ ->
2755 showtext '!' "searching does not work properly in split columns mode"
2756 | _ ->
2757 if String.length pattern > 0
2758 then
2759 let pn, py =
2760 match state.layout with
2761 | [] -> 0, 0
2762 | l :: _ ->
2763 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2765 wcmd "search %d %d %d %d,%s\000"
2766 (btod conf.icase) pn py (btod forward) pattern;
2769 let intentry text key =
2770 let c =
2771 if key >= 32 && key < 127
2772 then Char.chr key
2773 else '\000'
2775 match c with
2776 | '0' .. '9' ->
2777 let text = addchar text c in
2778 TEcont text
2780 | _ ->
2781 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2782 TEcont text
2785 let linknentry text key =
2786 let c =
2787 if key >= 32 && key < 127
2788 then Char.chr key
2789 else '\000'
2791 match c with
2792 | 'a' .. 'z' ->
2793 let text = addchar text c in
2794 TEcont text
2796 | _ ->
2797 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2798 TEcont text
2801 let linkndone f s =
2802 if String.length s > 0
2803 then (
2804 let n =
2805 let l = String.length s in
2806 let rec loop pos n = if pos = l then n else
2807 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2808 loop (pos+1) (n*26 + m)
2809 in loop 0 0
2811 let rec loop n = function
2812 | [] -> ()
2813 | l :: rest ->
2814 match getopaque l.pageno with
2815 | None -> loop n rest
2816 | Some opaque ->
2817 let m = getlinkcount opaque in
2818 if n < m
2819 then (
2820 let under = getlink opaque n in
2821 f under
2823 else loop (n-m) rest
2825 loop n state.layout;
2829 let textentry text key =
2830 if key land 0xff00 = 0xff00
2831 then TEcont text
2832 else TEcont (text ^ toutf8 key)
2835 let reqlayout angle fitmodel =
2836 match state.throttle with
2837 | None ->
2838 if nogeomcmds state.geomcmds
2839 then state.anchor <- getanchor ();
2840 conf.angle <- angle mod 360;
2841 if conf.angle != 0
2842 then (
2843 match state.mode with
2844 | LinkNav _ -> state.mode <- View
2845 | _ -> ()
2847 conf.fitmodel <- fitmodel;
2848 invalidate "reqlayout"
2849 (fun () ->
2850 wcmd "reqlayout %d %d %d"
2851 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2853 | _ -> ()
2856 let settrim trimmargins trimfuzz =
2857 if nogeomcmds state.geomcmds
2858 then state.anchor <- getanchor ();
2859 conf.trimmargins <- trimmargins;
2860 conf.trimfuzz <- trimfuzz;
2861 let x0, y0, x1, y1 = trimfuzz in
2862 invalidate "settrim"
2863 (fun () ->
2864 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2865 flushpages ();
2868 let setzoom zoom =
2869 match state.throttle with
2870 | None ->
2871 let zoom = max 0.0001 zoom in
2872 if zoom <> conf.zoom
2873 then (
2874 state.prevzoom <- conf.zoom;
2875 conf.zoom <- zoom;
2876 reshape state.winw state.winh;
2877 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2880 | Some (layout, y, started) ->
2881 let time =
2882 match conf.maxwait with
2883 | None -> 0.0
2884 | Some t -> t
2886 let dt = now () -. started in
2887 if dt > time
2888 then (
2889 state.y <- y;
2890 load layout;
2894 let setcolumns mode columns coverA coverB =
2895 state.prevcolumns <- Some (conf.columns, conf.zoom);
2896 if columns < 0
2897 then (
2898 if isbirdseye mode
2899 then showtext '!' "split mode doesn't work in bird's eye"
2900 else (
2901 conf.columns <- Csplit (-columns, [||]);
2902 state.x <- 0;
2903 conf.zoom <- 1.0;
2906 else (
2907 if columns < 2
2908 then (
2909 conf.columns <- Csingle [||];
2910 state.x <- 0;
2911 setzoom 1.0;
2913 else (
2914 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2915 conf.zoom <- 1.0;
2918 reshape state.winw state.winh;
2921 let enterbirdseye () =
2922 let zoom = float conf.thumbw /. float state.winw in
2923 let birdseyepageno =
2924 let cy = state.winh / 2 in
2925 let fold = function
2926 | [] -> 0
2927 | l :: rest ->
2928 let rec fold best = function
2929 | [] -> best.pageno
2930 | l :: rest ->
2931 let d = cy - (l.pagedispy + l.pagevh/2)
2932 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2933 if abs d < abs dbest
2934 then fold l rest
2935 else best.pageno
2936 in fold l rest
2938 fold state.layout
2940 state.mode <- Birdseye (
2941 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2943 conf.zoom <- zoom;
2944 conf.presentation <- false;
2945 conf.interpagespace <- 10;
2946 conf.hlinks <- false;
2947 conf.fitmodel <- FitProportional;
2948 state.x <- 0;
2949 state.mstate <- Mnone;
2950 conf.maxwait <- None;
2951 conf.columns <- (
2952 match conf.beyecolumns with
2953 | Some c ->
2954 conf.zoom <- 1.0;
2955 Cmulti ((c, 0, 0), [||])
2956 | None -> Csingle [||]
2958 Wsi.setcursor Wsi.CURSOR_INHERIT;
2959 if conf.verbose
2960 then
2961 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2962 (100.0*.zoom)
2963 else
2964 state.text <- ""
2966 reshape state.winw state.winh;
2969 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2970 state.mode <- View;
2971 conf.zoom <- c.zoom;
2972 conf.presentation <- c.presentation;
2973 conf.interpagespace <- c.interpagespace;
2974 conf.maxwait <- c.maxwait;
2975 conf.hlinks <- c.hlinks;
2976 conf.fitmodel <- c.fitmodel;
2977 conf.beyecolumns <- (
2978 match conf.columns with
2979 | Cmulti ((c, _, _), _) -> Some c
2980 | Csingle _ -> None
2981 | Csplit _ -> failwith "leaving bird's eye split mode"
2983 conf.columns <- (
2984 match c.columns with
2985 | Cmulti (c, _) -> Cmulti (c, [||])
2986 | Csingle _ -> Csingle [||]
2987 | Csplit (c, _) -> Csplit (c, [||])
2989 state.x <- leftx;
2990 if conf.verbose
2991 then
2992 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2993 (100.0*.conf.zoom)
2995 reshape state.winw state.winh;
2996 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2999 let togglebirdseye () =
3000 match state.mode with
3001 | Birdseye vals -> leavebirdseye vals true
3002 | View -> enterbirdseye ()
3003 | _ -> ()
3006 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3007 let pageno = max 0 (pageno - incr) in
3008 let rec loop = function
3009 | [] -> gotopage1 pageno 0
3010 | l :: _ when l.pageno = pageno ->
3011 if l.pagedispy >= 0 && l.pagey = 0
3012 then G.postRedisplay "upbirdseye"
3013 else gotopage1 pageno 0
3014 | _ :: rest -> loop rest
3016 loop state.layout;
3017 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3020 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3021 let pageno = min (state.pagecount - 1) (pageno + incr) in
3022 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3023 let rec loop = function
3024 | [] ->
3025 let y, h = getpageyh pageno in
3026 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3027 gotoy (clamp dy)
3028 | l :: _ when l.pageno = pageno ->
3029 if l.pagevh != l.pageh
3030 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3031 else G.postRedisplay "downbirdseye"
3032 | _ :: rest -> loop rest
3034 loop state.layout
3037 let optentry mode _ key =
3038 let btos b = if b then "on" else "off" in
3039 if key >= 32 && key < 127
3040 then
3041 let c = Char.chr key in
3042 match c with
3043 | 's' ->
3044 let ondone s =
3045 try conf.scrollstep <- int_of_string s with exc ->
3046 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3048 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3050 | 'A' ->
3051 let ondone s =
3053 conf.autoscrollstep <- int_of_string s;
3054 if state.autoscroll <> None
3055 then state.autoscroll <- Some conf.autoscrollstep
3056 with exc ->
3057 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3059 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3061 | 'C' ->
3062 let ondone s =
3064 let n, a, b = multicolumns_of_string s in
3065 setcolumns mode n a b;
3066 with exc ->
3067 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3069 TEswitch ("columns: ", "", None, textentry, ondone, true)
3071 | 'Z' ->
3072 let ondone s =
3074 let zoom = float (int_of_string s) /. 100.0 in
3075 setzoom zoom
3076 with exc ->
3077 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3079 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3081 | 't' ->
3082 let ondone s =
3084 conf.thumbw <- bound (int_of_string s) 2 4096;
3085 state.text <-
3086 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3087 begin match mode with
3088 | Birdseye beye ->
3089 leavebirdseye beye false;
3090 enterbirdseye ();
3091 | _ -> ();
3093 with exc ->
3094 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3096 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3098 | 'R' ->
3099 let ondone s =
3100 match try
3101 Some (int_of_string s)
3102 with exc ->
3103 state.text <- Printf.sprintf "bad integer `%s': %s"
3104 s (exntos exc);
3105 None
3106 with
3107 | Some angle -> reqlayout angle conf.fitmodel
3108 | None -> ()
3110 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3112 | 'i' ->
3113 conf.icase <- not conf.icase;
3114 TEdone ("case insensitive search " ^ (btos conf.icase))
3116 | 'p' ->
3117 conf.preload <- not conf.preload;
3118 gotoy state.y;
3119 TEdone ("preload " ^ (btos conf.preload))
3121 | 'v' ->
3122 conf.verbose <- not conf.verbose;
3123 TEdone ("verbose " ^ (btos conf.verbose))
3125 | 'd' ->
3126 conf.debug <- not conf.debug;
3127 TEdone ("debug " ^ (btos conf.debug))
3129 | 'h' ->
3130 conf.maxhfit <- not conf.maxhfit;
3131 state.maxy <- calcheight ();
3132 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3134 | 'c' ->
3135 conf.crophack <- not conf.crophack;
3136 TEdone ("crophack " ^ btos conf.crophack)
3138 | 'a' ->
3139 let s =
3140 match conf.maxwait with
3141 | None ->
3142 conf.maxwait <- Some infinity;
3143 "always wait for page to complete"
3144 | Some _ ->
3145 conf.maxwait <- None;
3146 "show placeholder if page is not ready"
3148 TEdone s
3150 | 'f' ->
3151 conf.underinfo <- not conf.underinfo;
3152 TEdone ("underinfo " ^ btos conf.underinfo)
3154 | 'P' ->
3155 conf.savebmarks <- not conf.savebmarks;
3156 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3158 | 'S' ->
3159 let ondone s =
3161 let pageno, py =
3162 match state.layout with
3163 | [] -> 0, 0
3164 | l :: _ ->
3165 l.pageno, l.pagey
3167 conf.interpagespace <- int_of_string s;
3168 docolumns conf.columns;
3169 state.maxy <- calcheight ();
3170 let y = getpagey pageno in
3171 gotoy (y + py)
3172 with exc ->
3173 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3175 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3177 | 'l' ->
3178 let fm =
3179 match conf.fitmodel with
3180 | FitProportional -> FitWidth
3181 | _ -> FitProportional
3183 reqlayout conf.angle fm;
3184 TEdone ("proportional display " ^ btos (fm == FitProportional))
3186 | 'T' ->
3187 settrim (not conf.trimmargins) conf.trimfuzz;
3188 TEdone ("trim margins " ^ btos conf.trimmargins)
3190 | 'I' ->
3191 conf.invert <- not conf.invert;
3192 TEdone ("invert colors " ^ btos conf.invert)
3194 | 'x' ->
3195 let ondone s =
3196 cbput state.hists.sel s;
3197 conf.selcmd <- s;
3199 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3200 textentry, ondone, true)
3202 | 'M' ->
3203 if conf.pax == None
3204 then conf.pax <- Some (ref (0.0, 0, 0))
3205 else conf.pax <- None;
3206 TEdone ("PAX " ^ btos (conf.pax != None))
3208 | _ ->
3209 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3210 TEstop
3211 else
3212 TEcont state.text
3215 class type lvsource = object
3216 method getitemcount : int
3217 method getitem : int -> (string * int)
3218 method hasaction : int -> bool
3219 method exit :
3220 uioh:uioh ->
3221 cancel:bool ->
3222 active:int ->
3223 first:int ->
3224 pan:int ->
3225 qsearch:string ->
3226 uioh option
3227 method getactive : int
3228 method getfirst : int
3229 method getqsearch : string
3230 method setqsearch : string -> unit
3231 method getpan : int
3232 end;;
3234 class virtual lvsourcebase = object
3235 val mutable m_active = 0
3236 val mutable m_first = 0
3237 val mutable m_qsearch = ""
3238 val mutable m_pan = 0
3239 method getactive = m_active
3240 method getfirst = m_first
3241 method getqsearch = m_qsearch
3242 method getpan = m_pan
3243 method setqsearch s = m_qsearch <- s
3244 end;;
3246 let withoutlastutf8 s =
3247 let len = String.length s in
3248 if len = 0
3249 then s
3250 else
3251 let rec find pos =
3252 if pos = 0
3253 then pos
3254 else
3255 let b = Char.code s.[pos] in
3256 if b land 0b11000000 = 0b11000000
3257 then pos
3258 else find (pos-1)
3260 let first =
3261 if Char.code s.[len-1] land 0x80 = 0
3262 then len-1
3263 else find (len-1)
3265 String.sub s 0 first;
3268 let textentrykeyboard
3269 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3270 let key =
3271 if key >= 0xffb0 && key <= 0xffb9
3272 then key - 0xffb0 + 48 else key
3274 let enttext te =
3275 state.mode <- Textentry (te, onleave);
3276 state.text <- "";
3277 enttext ();
3278 G.postRedisplay "textentrykeyboard enttext";
3280 let histaction cmd =
3281 match opthist with
3282 | None -> ()
3283 | Some (action, _) ->
3284 state.mode <- Textentry (
3285 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3287 G.postRedisplay "textentry histaction"
3289 match key with
3290 | 0xff08 -> (* backspace *)
3291 let s = withoutlastutf8 text in
3292 let len = String.length s in
3293 if cancelonempty && len = 0
3294 then (
3295 onleave Cancel;
3296 G.postRedisplay "textentrykeyboard after cancel";
3298 else (
3299 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3302 | 0xff0d | 0xff8d -> (* (kp) enter *)
3303 ondone text;
3304 onleave Confirm;
3305 G.postRedisplay "textentrykeyboard after confirm"
3307 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3308 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3309 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3310 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3312 | 0xff1b -> (* escape*)
3313 if String.length text = 0
3314 then (
3315 begin match opthist with
3316 | None -> ()
3317 | Some (_, onhistcancel) -> onhistcancel ()
3318 end;
3319 onleave Cancel;
3320 state.text <- "";
3321 G.postRedisplay "textentrykeyboard after cancel2"
3323 else (
3324 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3327 | 0xff9f | 0xffff -> () (* delete *)
3329 | _ when key != 0
3330 && key land 0xff00 != 0xff00 (* keyboard *)
3331 && key land 0xfe00 != 0xfe00 (* xkb *)
3332 && key land 0xfd00 != 0xfd00 (* 3270 *)
3334 begin match onkey text key with
3335 | TEdone text ->
3336 ondone text;
3337 onleave Confirm;
3338 G.postRedisplay "textentrykeyboard after confirm2";
3340 | TEcont text ->
3341 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3343 | TEstop ->
3344 onleave Cancel;
3345 G.postRedisplay "textentrykeyboard after cancel3"
3347 | TEswitch te ->
3348 state.mode <- Textentry (te, onleave);
3349 G.postRedisplay "textentrykeyboard switch";
3350 end;
3352 | _ ->
3353 vlog "unhandled key %s" (Wsi.keyname key)
3356 let firstof first active =
3357 if first > active || abs (first - active) > fstate.maxrows - 1
3358 then max 0 (active - (fstate.maxrows/2))
3359 else first
3362 let calcfirst first active =
3363 if active > first
3364 then
3365 let rows = active - first in
3366 if rows > fstate.maxrows then active - fstate.maxrows else first
3367 else active
3370 let scrollph y maxy =
3371 let sh = float (maxy + state.winh) /. float state.winh in
3372 let sh = float state.winh /. sh in
3373 let sh = max sh (float conf.scrollh) in
3375 let percent = float y /. float maxy in
3376 let position = (float state.winh -. sh) *. percent in
3378 let position =
3379 if position +. sh > float state.winh
3380 then float state.winh -. sh
3381 else position
3383 position, sh;
3386 let coe s = (s :> uioh);;
3388 class listview ~(source:lvsource) ~trusted ~modehash =
3389 object (self)
3390 val m_pan = source#getpan
3391 val m_first = source#getfirst
3392 val m_active = source#getactive
3393 val m_qsearch = source#getqsearch
3394 val m_prev_uioh = state.uioh
3396 method private elemunder y =
3397 let n = y / (fstate.fontsize+1) in
3398 if m_first + n < source#getitemcount
3399 then (
3400 if source#hasaction (m_first + n)
3401 then Some (m_first + n)
3402 else None
3404 else None
3406 method display =
3407 Gl.enable `blend;
3408 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3409 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3410 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3411 GlDraw.color (1., 1., 1.);
3412 Gl.enable `texture_2d;
3413 let fs = fstate.fontsize in
3414 let nfs = fs + 1 in
3415 let ww = fstate.wwidth in
3416 let tabw = 30.0*.ww in
3417 let itemcount = source#getitemcount in
3418 let rec loop row =
3419 if (row - m_first) > fstate.maxrows
3420 then ()
3421 else (
3422 if row >= 0 && row < itemcount
3423 then (
3424 let (s, level) = source#getitem row in
3425 let y = (row - m_first) * nfs in
3426 let x = 5.0 +. float (level + m_pan) *. ww in
3427 if row = m_active
3428 then (
3429 Gl.disable `texture_2d;
3430 GlDraw.polygon_mode `both `line;
3431 let alpha = if source#hasaction row then 0.9 else 0.3 in
3432 GlDraw.color (1., 1., 1.) ~alpha;
3433 GlDraw.rect (1., float (y + 1))
3434 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3435 GlDraw.polygon_mode `both `fill;
3436 GlDraw.color (1., 1., 1.);
3437 Gl.enable `texture_2d;
3440 let drawtabularstring s =
3441 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3442 if trusted
3443 then
3444 let tabpos = try String.index s '\t' with Not_found -> -1 in
3445 if tabpos > 0
3446 then
3447 let len = String.length s - tabpos - 1 in
3448 let s1 = String.sub s 0 tabpos
3449 and s2 = String.sub s (tabpos + 1) len in
3450 let nx = drawstr x s1 in
3451 let sw = nx -. x in
3452 let x = x +. (max tabw sw) in
3453 drawstr x s2
3454 else
3455 drawstr x s
3456 else
3457 drawstr x s
3459 let _ = drawtabularstring s in
3460 loop (row+1)
3464 loop m_first;
3465 Gl.disable `blend;
3466 Gl.disable `texture_2d;
3468 method updownlevel incr =
3469 let len = source#getitemcount in
3470 let curlevel =
3471 if m_active >= 0 && m_active < len
3472 then snd (source#getitem m_active)
3473 else -1
3475 let rec flow i =
3476 if i = len then i-1 else if i = -1 then 0 else
3477 let _, l = source#getitem i in
3478 if l != curlevel then i else flow (i+incr)
3480 let active = flow m_active in
3481 let first = calcfirst m_first active in
3482 G.postRedisplay "outline updownlevel";
3483 {< m_active = active; m_first = first >}
3485 method private key1 key mask =
3486 let set1 active first qsearch =
3487 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3489 let search active pattern incr =
3490 let active = if active = -1 then m_first else active in
3491 let dosearch re =
3492 let rec loop n =
3493 if n >= 0 && n < source#getitemcount
3494 then (
3495 let s, _ = source#getitem n in
3497 (try ignore (Str.search_forward re s 0); true
3498 with Not_found -> false)
3499 then Some n
3500 else loop (n + incr)
3502 else None
3504 loop active
3507 let re = Str.regexp_case_fold pattern in
3508 dosearch re
3509 with Failure s ->
3510 state.text <- s;
3511 None
3513 let itemcount = source#getitemcount in
3514 let find start incr =
3515 let rec find i =
3516 if i = -1 || i = itemcount
3517 then -1
3518 else (
3519 if source#hasaction i
3520 then i
3521 else find (i + incr)
3524 find start
3526 let set active first =
3527 let first = bound first 0 (itemcount - fstate.maxrows) in
3528 state.text <- "";
3529 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3531 let navigate incr =
3532 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3533 let active, first =
3534 let incr1 = if incr > 0 then 1 else -1 in
3535 if isvisible m_first m_active
3536 then
3537 let next =
3538 let next = m_active + incr in
3539 let next =
3540 if next < 0 || next >= itemcount
3541 then -1
3542 else find next incr1
3544 if abs (m_active - next) > fstate.maxrows
3545 then -1
3546 else next
3548 if next = -1
3549 then
3550 let first = m_first + incr in
3551 let first = bound first 0 (itemcount - fstate.maxrows) in
3552 let next =
3553 let next = m_active + incr in
3554 let next = bound next 0 (itemcount - 1) in
3555 find next ~-incr1
3557 let active =
3558 if next = -1
3559 then m_active
3560 else (
3561 if isvisible first next
3562 then next
3563 else m_active
3566 active, first
3567 else
3568 let first = min next m_first in
3569 let first =
3570 if abs (next - first) > fstate.maxrows
3571 then first + incr
3572 else first
3574 next, first
3575 else
3576 let first = m_first + incr in
3577 let first = bound first 0 (itemcount - 1) in
3578 let active =
3579 let next = m_active + incr in
3580 let next = bound next 0 (itemcount - 1) in
3581 let next = find next incr1 in
3582 let active =
3583 if next = -1 || abs (m_active - first) > fstate.maxrows
3584 then (
3585 let active = if m_active = -1 then next else m_active in
3586 active
3588 else next
3590 if isvisible first active
3591 then active
3592 else -1
3594 active, first
3596 G.postRedisplay "listview navigate";
3597 set active first;
3599 match key with
3600 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3601 let incr = if key = 0x72 then -1 else 1 in
3602 let active, first =
3603 match search (m_active + incr) m_qsearch incr with
3604 | None ->
3605 state.text <- m_qsearch ^ " [not found]";
3606 m_active, m_first
3607 | Some active ->
3608 state.text <- m_qsearch;
3609 active, firstof m_first active
3611 G.postRedisplay "listview ctrl-r/s";
3612 set1 active first m_qsearch;
3614 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3615 if m_active >= 0 && m_active < source#getitemcount
3616 then (
3617 let s, _ = source#getitem m_active in
3618 selstring s;
3620 coe self
3622 | 0xff08 -> (* backspace *)
3623 if String.length m_qsearch = 0
3624 then coe self
3625 else (
3626 let qsearch = withoutlastutf8 m_qsearch in
3627 let len = String.length qsearch in
3628 if len = 0
3629 then (
3630 state.text <- "";
3631 G.postRedisplay "listview empty qsearch";
3632 set1 m_active m_first "";
3634 else
3635 let active, first =
3636 match search m_active qsearch ~-1 with
3637 | None ->
3638 state.text <- qsearch ^ " [not found]";
3639 m_active, m_first
3640 | Some active ->
3641 state.text <- qsearch;
3642 active, firstof m_first active
3644 G.postRedisplay "listview backspace qsearch";
3645 set1 active first qsearch
3648 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3649 let pattern = m_qsearch ^ toutf8 key in
3650 let active, first =
3651 match search m_active pattern 1 with
3652 | None ->
3653 state.text <- pattern ^ " [not found]";
3654 m_active, m_first
3655 | Some active ->
3656 state.text <- pattern;
3657 active, firstof m_first active
3659 G.postRedisplay "listview qsearch add";
3660 set1 active first pattern;
3662 | 0xff1b -> (* escape *)
3663 state.text <- "";
3664 if String.length m_qsearch = 0
3665 then (
3666 G.postRedisplay "list view escape";
3667 begin
3668 match
3669 source#exit (coe self) true m_active m_first m_pan m_qsearch
3670 with
3671 | None -> m_prev_uioh
3672 | Some uioh -> uioh
3675 else (
3676 G.postRedisplay "list view kill qsearch";
3677 source#setqsearch "";
3678 coe {< m_qsearch = "" >}
3681 | 0xff0d | 0xff8d -> (* (kp) enter *)
3682 state.text <- "";
3683 let self = {< m_qsearch = "" >} in
3684 source#setqsearch "";
3685 let opt =
3686 G.postRedisplay "listview enter";
3687 if m_active >= 0 && m_active < source#getitemcount
3688 then (
3689 source#exit (coe self) false m_active m_first m_pan "";
3691 else (
3692 source#exit (coe self) true m_active m_first m_pan "";
3695 begin match opt with
3696 | None -> m_prev_uioh
3697 | Some uioh -> uioh
3700 | 0xff9f | 0xffff -> (* (kp) delete *)
3701 coe self
3703 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3704 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3705 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3706 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3708 | 0xff53 | 0xff98 -> (* (kp) right *)
3709 state.text <- "";
3710 G.postRedisplay "listview right";
3711 coe {< m_pan = m_pan - 1 >}
3713 | 0xff51 | 0xff96 -> (* (kp) left *)
3714 state.text <- "";
3715 G.postRedisplay "listview left";
3716 coe {< m_pan = m_pan + 1 >}
3718 | 0xff50 | 0xff95 -> (* (kp) home *)
3719 let active = find 0 1 in
3720 G.postRedisplay "listview home";
3721 set active 0;
3723 | 0xff57 | 0xff9c -> (* (kp) end *)
3724 let first = max 0 (itemcount - fstate.maxrows) in
3725 let active = find (itemcount - 1) ~-1 in
3726 G.postRedisplay "listview end";
3727 set active first;
3729 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3730 coe self
3732 | _ ->
3733 dolog "listview unknown key %#x" key; coe self
3735 method key key mask =
3736 match state.mode with
3737 | Textentry te -> textentrykeyboard key mask te; coe self
3738 | _ -> self#key1 key mask
3740 method button button down x y _ =
3741 let opt =
3742 match button with
3743 | 1 when x > state.winw - conf.scrollbw ->
3744 G.postRedisplay "listview scroll";
3745 if down
3746 then
3747 let _, position, sh = self#scrollph in
3748 if y > truncate position && y < truncate (position +. sh)
3749 then (
3750 state.mstate <- Mscrolly;
3751 Some (coe self)
3753 else
3754 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3755 let first = truncate (s *. float source#getitemcount) in
3756 let first = min source#getitemcount first in
3757 Some (coe {< m_first = first; m_active = first >})
3758 else (
3759 state.mstate <- Mnone;
3760 Some (coe self);
3762 | 1 when not down ->
3763 begin match self#elemunder y with
3764 | Some n ->
3765 G.postRedisplay "listview click";
3766 source#exit
3767 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3768 | _ ->
3769 Some (coe self)
3771 | n when (n == 4 || n == 5) && not down ->
3772 let len = source#getitemcount in
3773 let first =
3774 if n = 5 && m_first + fstate.maxrows >= len
3775 then
3776 m_first
3777 else
3778 let first = m_first + (if n == 4 then -1 else 1) in
3779 bound first 0 (len - 1)
3781 G.postRedisplay "listview wheel";
3782 Some (coe {< m_first = first >})
3783 | n when (n = 6 || n = 7) && not down ->
3784 let inc = m_first + (if n = 7 then -1 else 1) in
3785 G.postRedisplay "listview hwheel";
3786 Some (coe {< m_pan = m_pan + inc >})
3787 | _ ->
3788 Some (coe self)
3790 match opt with
3791 | None -> m_prev_uioh
3792 | Some uioh -> uioh
3794 method motion _ y =
3795 match state.mstate with
3796 | Mscrolly ->
3797 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3798 let first = truncate (s *. float source#getitemcount) in
3799 let first = min source#getitemcount first in
3800 G.postRedisplay "listview motion";
3801 coe {< m_first = first; m_active = first >}
3802 | _ -> coe self
3804 method pmotion x y =
3805 if x < state.winw - conf.scrollbw
3806 then
3807 let n =
3808 match self#elemunder y with
3809 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3810 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3812 let o =
3813 if n != m_active
3814 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3815 else self
3817 coe o
3818 else (
3819 Wsi.setcursor Wsi.CURSOR_INHERIT;
3820 coe self
3823 method infochanged _ = ()
3825 method scrollpw = (0, 0.0, 0.0)
3826 method scrollph =
3827 let nfs = fstate.fontsize + 1 in
3828 let y = m_first * nfs in
3829 let itemcount = source#getitemcount in
3830 let maxi = max 0 (itemcount - fstate.maxrows) in
3831 let maxy = maxi * nfs in
3832 let p, h = scrollph y maxy in
3833 conf.scrollbw, p, h
3835 method modehash = modehash
3836 method eformsgs = false
3837 end;;
3839 class outlinelistview ~source =
3840 object (self)
3841 inherit listview
3842 ~source:(source :> lvsource)
3843 ~trusted:false
3844 ~modehash:(findkeyhash conf "outline")
3845 as super
3847 method key key mask =
3848 let calcfirst first active =
3849 if active > first
3850 then
3851 let rows = active - first in
3852 let maxrows =
3853 if String.length state.text = 0
3854 then fstate.maxrows
3855 else fstate.maxrows - 2
3857 if rows > maxrows then active - maxrows else first
3858 else active
3860 let navigate incr =
3861 let active = m_active + incr in
3862 let active = bound active 0 (source#getitemcount - 1) in
3863 let first = calcfirst m_first active in
3864 G.postRedisplay "outline navigate";
3865 coe {< m_active = active; m_first = first >}
3867 let ctrl = Wsi.withctrl mask in
3868 match key with
3869 | 110 when ctrl -> (* ctrl-n *)
3870 source#narrow m_qsearch;
3871 G.postRedisplay "outline ctrl-n";
3872 coe {< m_first = 0; m_active = 0 >}
3874 | 117 when ctrl -> (* ctrl-u *)
3875 source#denarrow;
3876 G.postRedisplay "outline ctrl-u";
3877 state.text <- "";
3878 coe {< m_first = 0; m_active = 0 >}
3880 | 108 when ctrl -> (* ctrl-l *)
3881 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3882 G.postRedisplay "outline ctrl-l";
3883 coe {< m_first = first >}
3885 | 0xff9f | 0xffff -> (* (kp) delete *)
3886 source#remove m_active;
3887 G.postRedisplay "outline delete";
3888 let active = max 0 (m_active-1) in
3889 coe {< m_first = firstof m_first active;
3890 m_active = active >}
3892 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3893 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3894 | 0xff55 | 0xff9a -> (* (kp) prior *)
3895 navigate ~-(fstate.maxrows)
3896 | 0xff56 | 0xff9b -> (* (kp) next *)
3897 navigate fstate.maxrows
3899 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3900 let o =
3901 if ctrl
3902 then (
3903 G.postRedisplay "outline ctrl right";
3904 {< m_pan = m_pan + 1 >}
3906 else self#updownlevel 1
3908 coe o
3910 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3911 let o =
3912 if ctrl
3913 then (
3914 G.postRedisplay "outline ctrl left";
3915 {< m_pan = m_pan - 1 >}
3917 else self#updownlevel ~-1
3919 coe o
3921 | 0xff50 | 0xff95 -> (* (kp) home *)
3922 G.postRedisplay "outline home";
3923 coe {< m_first = 0; m_active = 0 >}
3925 | 0xff57 | 0xff9c -> (* (kp) end *)
3926 let active = source#getitemcount - 1 in
3927 let first = max 0 (active - fstate.maxrows) in
3928 G.postRedisplay "outline end";
3929 coe {< m_active = active; m_first = first >}
3931 | _ -> super#key key mask
3934 let outlinesource usebookmarks =
3935 let empty = [||] in
3936 (object
3937 inherit lvsourcebase
3938 val mutable m_items = empty
3939 val mutable m_orig_items = empty
3940 val mutable m_prev_items = empty
3941 val mutable m_narrow_pattern = ""
3942 val mutable m_hadremovals = false
3944 method getitemcount =
3945 Array.length m_items + (if m_hadremovals then 1 else 0)
3947 method getitem n =
3948 if n == Array.length m_items && m_hadremovals
3949 then
3950 ("[Confirm removal]", 0)
3951 else
3952 let s, n, _ = m_items.(n) in
3953 (s, n)
3955 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3956 ignore (uioh, first, qsearch);
3957 let confrimremoval = m_hadremovals && active = Array.length m_items in
3958 let items =
3959 if String.length m_narrow_pattern = 0
3960 then m_orig_items
3961 else m_items
3963 if not cancel
3964 then (
3965 if not confrimremoval
3966 then(
3967 let _, _, anchor = m_items.(active) in
3968 gotoghyll (getanchory anchor);
3969 m_items <- items;
3971 else (
3972 state.bookmarks <- Array.to_list m_items;
3973 m_orig_items <- m_items;
3976 else m_items <- items;
3977 m_pan <- pan;
3978 None
3980 method hasaction _ = true
3982 method greetmsg =
3983 if Array.length m_items != Array.length m_orig_items
3984 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3985 else ""
3987 method narrow pattern =
3988 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3989 match reopt with
3990 | None -> ()
3991 | Some re ->
3992 let rec loop accu n =
3993 if n = -1
3994 then (
3995 m_narrow_pattern <- pattern;
3996 m_items <- Array.of_list accu
3998 else
3999 let (s, _, _) as o = m_items.(n) in
4000 let accu =
4001 if (try ignore (Str.search_forward re s 0); true
4002 with Not_found -> false)
4003 then o :: accu
4004 else accu
4006 loop accu (n-1)
4008 loop [] (Array.length m_items - 1)
4010 method denarrow =
4011 m_orig_items <- (
4012 if usebookmarks
4013 then Array.of_list state.bookmarks
4014 else state.outlines
4016 m_items <- m_orig_items
4018 method remove m =
4019 if usebookmarks
4020 then
4021 if m >= 0 && m < Array.length m_items
4022 then (
4023 m_hadremovals <- true;
4024 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4025 let n = if n >= m then n+1 else n in
4026 m_items.(n)
4030 method reset anchor items =
4031 m_hadremovals <- false;
4032 if m_orig_items == empty || m_prev_items != items
4033 then (
4034 m_orig_items <- items;
4035 if String.length m_narrow_pattern = 0
4036 then m_items <- items;
4038 m_prev_items <- items;
4039 let rely = getanchory anchor in
4040 let active =
4041 let rec loop n best bestd =
4042 if n = Array.length m_items
4043 then best
4044 else
4045 let (_, _, anchor) = m_items.(n) in
4046 let orely = getanchory anchor in
4047 let d = abs (orely - rely) in
4048 if d < bestd
4049 then loop (n+1) n d
4050 else loop (n+1) best bestd
4052 loop 0 ~-1 max_int
4054 m_active <- active;
4055 m_first <- firstof m_first active
4056 end)
4059 let enterselector usebookmarks =
4060 let source = outlinesource usebookmarks in
4061 fun errmsg ->
4062 let outlines =
4063 if usebookmarks
4064 then Array.of_list state.bookmarks
4065 else state.outlines
4067 if Array.length outlines = 0
4068 then (
4069 showtext ' ' errmsg;
4071 else (
4072 state.text <- source#greetmsg;
4073 Wsi.setcursor Wsi.CURSOR_INHERIT;
4074 let anchor = getanchor () in
4075 source#reset anchor outlines;
4076 state.uioh <- coe (new outlinelistview ~source);
4077 G.postRedisplay "enter selector";
4081 let enteroutlinemode =
4082 let f = enterselector false in
4083 fun ()-> f "Document has no outline";
4086 let enterbookmarkmode =
4087 let f = enterselector true in
4088 fun () -> f "Document has no bookmarks (yet)";
4091 let color_of_string s =
4092 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4093 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4097 let color_to_string (r, g, b) =
4098 let r = truncate (r *. 256.0)
4099 and g = truncate (g *. 256.0)
4100 and b = truncate (b *. 256.0) in
4101 Printf.sprintf "%d/%d/%d" r g b
4104 let irect_of_string s =
4105 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4108 let irect_to_string (x0,y0,x1,y1) =
4109 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4112 let makecheckers () =
4113 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4114 following to say:
4115 converted by Issac Trotts. July 25, 2002 *)
4116 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4117 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4118 let id = GlTex.gen_texture () in
4119 GlTex.bind_texture `texture_2d id;
4120 GlPix.store (`unpack_alignment 1);
4121 GlTex.image2d image;
4122 List.iter (GlTex.parameter ~target:`texture_2d)
4123 [ `mag_filter `nearest; `min_filter `nearest ];
4127 let setcheckers enabled =
4128 match state.texid with
4129 | None ->
4130 if enabled then state.texid <- Some (makecheckers ())
4132 | Some texid ->
4133 if not enabled
4134 then (
4135 GlTex.delete_texture texid;
4136 state.texid <- None;
4140 let int_of_string_with_suffix s =
4141 let l = String.length s in
4142 let s1, shift =
4143 if l > 1
4144 then
4145 let suffix = Char.lowercase s.[l-1] in
4146 match suffix with
4147 | 'k' -> String.sub s 0 (l-1), 10
4148 | 'm' -> String.sub s 0 (l-1), 20
4149 | 'g' -> String.sub s 0 (l-1), 30
4150 | _ -> s, 0
4151 else s, 0
4153 let n = int_of_string s1 in
4154 let m = n lsl shift in
4155 if m < 0 || m < n
4156 then raise (Failure "value too large")
4157 else m
4160 let string_with_suffix_of_int n =
4161 if n = 0
4162 then "0"
4163 else
4164 let n, s =
4165 if n land ((1 lsl 30) - 1) = 0
4166 then n lsr 30, "G"
4167 else (
4168 if n land ((1 lsl 20) - 1) = 0
4169 then n lsr 20, "M"
4170 else (
4171 if n land ((1 lsl 10) - 1) = 0
4172 then n lsr 10, "K"
4173 else n, ""
4177 let rec loop s n =
4178 let h = n mod 1000 in
4179 let n = n / 1000 in
4180 if n = 0
4181 then string_of_int h ^ s
4182 else (
4183 let s = Printf.sprintf "_%03d%s" h s in
4184 loop s n
4187 loop "" n ^ s;
4190 let defghyllscroll = (40, 8, 32);;
4191 let ghyllscroll_of_string s =
4192 let (n, a, b) as nab =
4193 if s = "default"
4194 then defghyllscroll
4195 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4197 if n <= a || n <= b || a >= b
4198 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4199 nab;
4202 let ghyllscroll_to_string ((n, a, b) as nab) =
4203 if nab = defghyllscroll
4204 then "default"
4205 else Printf.sprintf "%d,%d,%d" n a b;
4208 let describe_location () =
4209 let fn = page_of_y state.y in
4210 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4211 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4212 let percent =
4213 if maxy <= 0
4214 then 100.
4215 else (100. *. (float state.y /. float maxy))
4217 if fn = ln
4218 then
4219 Printf.sprintf "page %d of %d [%.2f%%]"
4220 (fn+1) state.pagecount percent
4221 else
4222 Printf.sprintf
4223 "pages %d-%d of %d [%.2f%%]"
4224 (fn+1) (ln+1) state.pagecount percent
4227 let setpresentationmode v =
4228 let n = page_of_y state.y in
4229 state.anchor <- (n, 0.0, 1.0);
4230 conf.presentation <- v;
4231 if conf.fitmodel = FitPage
4232 then reqlayout conf.angle conf.fitmodel;
4233 represent ();
4236 let enterinfomode =
4237 let btos b = if b then "\xe2\x88\x9a" else "" in
4238 let showextended = ref false in
4239 let leave mode = function
4240 | Confirm -> state.mode <- mode
4241 | Cancel -> state.mode <- mode in
4242 let src =
4243 (object
4244 val mutable m_first_time = true
4245 val mutable m_l = []
4246 val mutable m_a = [||]
4247 val mutable m_prev_uioh = nouioh
4248 val mutable m_prev_mode = View
4250 inherit lvsourcebase
4252 method reset prev_mode prev_uioh =
4253 m_a <- Array.of_list (List.rev m_l);
4254 m_l <- [];
4255 m_prev_mode <- prev_mode;
4256 m_prev_uioh <- prev_uioh;
4257 if m_first_time
4258 then (
4259 let rec loop n =
4260 if n >= Array.length m_a
4261 then ()
4262 else
4263 match m_a.(n) with
4264 | _, _, _, Action _ -> m_active <- n
4265 | _ -> loop (n+1)
4267 loop 0;
4268 m_first_time <- false;
4271 method int name get set =
4272 m_l <-
4273 (name, `int get, 1, Action (
4274 fun u ->
4275 let ondone s =
4276 try set (int_of_string s)
4277 with exn ->
4278 state.text <- Printf.sprintf "bad integer `%s': %s"
4279 s (exntos exn)
4281 state.text <- "";
4282 let te = name ^ ": ", "", None, intentry, ondone, true in
4283 state.mode <- Textentry (te, leave m_prev_mode);
4285 )) :: m_l
4287 method int_with_suffix name get set =
4288 m_l <-
4289 (name, `intws get, 1, Action (
4290 fun u ->
4291 let ondone s =
4292 try set (int_of_string_with_suffix s)
4293 with exn ->
4294 state.text <- Printf.sprintf "bad integer `%s': %s"
4295 s (exntos exn)
4297 state.text <- "";
4298 let te =
4299 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4301 state.mode <- Textentry (te, leave m_prev_mode);
4303 )) :: m_l
4305 method bool ?(offset=1) ?(btos=btos) name get set =
4306 m_l <-
4307 (name, `bool (btos, get), offset, Action (
4308 fun u ->
4309 let v = get () in
4310 set (not v);
4312 )) :: m_l
4314 method color name get set =
4315 m_l <-
4316 (name, `color get, 1, Action (
4317 fun u ->
4318 let invalid = (nan, nan, nan) in
4319 let ondone s =
4320 let c =
4321 try color_of_string s
4322 with exn ->
4323 state.text <- Printf.sprintf "bad color `%s': %s"
4324 s (exntos exn);
4325 invalid
4327 if c <> invalid
4328 then set c;
4330 let te = name ^ ": ", "", None, textentry, ondone, true in
4331 state.text <- color_to_string (get ());
4332 state.mode <- Textentry (te, leave m_prev_mode);
4334 )) :: m_l
4336 method string name get set =
4337 m_l <-
4338 (name, `string get, 1, Action (
4339 fun u ->
4340 let ondone s = set s in
4341 let te = name ^ ": ", "", None, textentry, ondone, true in
4342 state.mode <- Textentry (te, leave m_prev_mode);
4344 )) :: m_l
4346 method colorspace name get set =
4347 m_l <-
4348 (name, `string get, 1, Action (
4349 fun _ ->
4350 let source =
4351 (object
4352 inherit lvsourcebase
4354 initializer
4355 m_active <- CSTE.to_int conf.colorspace;
4356 m_first <- 0;
4358 method getitemcount =
4359 Array.length CSTE.names
4360 method getitem n =
4361 (CSTE.names.(n), 0)
4362 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4363 ignore (uioh, first, pan, qsearch);
4364 if not cancel then set active;
4365 None
4366 method hasaction _ = true
4367 end)
4369 state.text <- "";
4370 let modehash = findkeyhash conf "info" in
4371 coe (new listview ~source ~trusted:true ~modehash)
4372 )) :: m_l
4374 method paxmark name get set =
4375 m_l <-
4376 (name, `string get, 1, Action (
4377 fun _ ->
4378 let source =
4379 (object
4380 inherit lvsourcebase
4382 initializer
4383 m_active <- MTE.to_int conf.paxmark;
4384 m_first <- 0;
4386 method getitemcount = Array.length MTE.names
4387 method getitem n = (MTE.names.(n), 0)
4388 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4389 ignore (uioh, first, pan, qsearch);
4390 if not cancel then set active;
4391 None
4392 method hasaction _ = true
4393 end)
4395 state.text <- "";
4396 let modehash = findkeyhash conf "info" in
4397 coe (new listview ~source ~trusted:true ~modehash)
4398 )) :: m_l
4400 method fitmodel name get set =
4401 m_l <-
4402 (name, `string get, 1, Action (
4403 fun _ ->
4404 let source =
4405 (object
4406 inherit lvsourcebase
4408 initializer
4409 m_active <- FMTE.to_int conf.fitmodel;
4410 m_first <- 0;
4412 method getitemcount = Array.length FMTE.names
4413 method getitem n = (FMTE.names.(n), 0)
4414 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4415 ignore (uioh, first, pan, qsearch);
4416 if not cancel then set active;
4417 None
4418 method hasaction _ = true
4419 end)
4421 state.text <- "";
4422 let modehash = findkeyhash conf "info" in
4423 coe (new listview ~source ~trusted:true ~modehash)
4424 )) :: m_l
4426 method caption s offset =
4427 m_l <- (s, `empty, offset, Noaction) :: m_l
4429 method caption2 s f offset =
4430 m_l <- (s, `string f, offset, Noaction) :: m_l
4432 method getitemcount = Array.length m_a
4434 method getitem n =
4435 let tostr = function
4436 | `int f -> string_of_int (f ())
4437 | `intws f -> string_with_suffix_of_int (f ())
4438 | `string f -> f ()
4439 | `color f -> color_to_string (f ())
4440 | `bool (btos, f) -> btos (f ())
4441 | `empty -> ""
4443 let name, t, offset, _ = m_a.(n) in
4444 ((let s = tostr t in
4445 if String.length s > 0
4446 then Printf.sprintf "%s\t%s" name s
4447 else name),
4448 offset)
4450 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4451 let uiohopt =
4452 if not cancel
4453 then (
4454 m_qsearch <- qsearch;
4455 let uioh =
4456 match m_a.(active) with
4457 | _, _, _, Action f -> f uioh
4458 | _ -> uioh
4460 Some uioh
4462 else None
4464 m_active <- active;
4465 m_first <- first;
4466 m_pan <- pan;
4467 uiohopt
4469 method hasaction n =
4470 match m_a.(n) with
4471 | _, _, _, Action _ -> true
4472 | _ -> false
4473 end)
4475 let rec fillsrc prevmode prevuioh =
4476 let sep () = src#caption "" 0 in
4477 let colorp name get set =
4478 src#string name
4479 (fun () -> color_to_string (get ()))
4480 (fun v ->
4482 let c = color_of_string v in
4483 set c
4484 with exn ->
4485 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4488 let oldmode = state.mode in
4489 let birdseye = isbirdseye state.mode in
4491 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4493 src#bool "presentation mode"
4494 (fun () -> conf.presentation)
4495 (fun v -> setpresentationmode v);
4497 src#bool "ignore case in searches"
4498 (fun () -> conf.icase)
4499 (fun v -> conf.icase <- v);
4501 src#bool "preload"
4502 (fun () -> conf.preload)
4503 (fun v -> conf.preload <- v);
4505 src#bool "highlight links"
4506 (fun () -> conf.hlinks)
4507 (fun v -> conf.hlinks <- v);
4509 src#bool "under info"
4510 (fun () -> conf.underinfo)
4511 (fun v -> conf.underinfo <- v);
4513 src#bool "persistent bookmarks"
4514 (fun () -> conf.savebmarks)
4515 (fun v -> conf.savebmarks <- v);
4517 src#fitmodel "fit model"
4518 (fun () -> FMTE.to_string conf.fitmodel)
4519 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4521 src#bool "trim margins"
4522 (fun () -> conf.trimmargins)
4523 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4525 src#bool "persistent location"
4526 (fun () -> conf.jumpback)
4527 (fun v -> conf.jumpback <- v);
4529 sep ();
4530 src#int "inter-page space"
4531 (fun () -> conf.interpagespace)
4532 (fun n ->
4533 conf.interpagespace <- n;
4534 docolumns conf.columns;
4535 let pageno, py =
4536 match state.layout with
4537 | [] -> 0, 0
4538 | l :: _ ->
4539 l.pageno, l.pagey
4541 state.maxy <- calcheight ();
4542 let y = getpagey pageno in
4543 gotoy (y + py)
4546 src#int "page bias"
4547 (fun () -> conf.pagebias)
4548 (fun v -> conf.pagebias <- v);
4550 src#int "scroll step"
4551 (fun () -> conf.scrollstep)
4552 (fun n -> conf.scrollstep <- n);
4554 src#int "horizontal scroll step"
4555 (fun () -> conf.hscrollstep)
4556 (fun v -> conf.hscrollstep <- v);
4558 src#int "auto scroll step"
4559 (fun () ->
4560 match state.autoscroll with
4561 | Some step -> step
4562 | _ -> conf.autoscrollstep)
4563 (fun n ->
4564 if state.autoscroll <> None
4565 then state.autoscroll <- Some n;
4566 conf.autoscrollstep <- n);
4568 src#int "zoom"
4569 (fun () -> truncate (conf.zoom *. 100.))
4570 (fun v -> setzoom ((float v) /. 100.));
4572 src#int "rotation"
4573 (fun () -> conf.angle)
4574 (fun v -> reqlayout v conf.fitmodel);
4576 src#int "scroll bar width"
4577 (fun () -> conf.scrollbw)
4578 (fun v ->
4579 conf.scrollbw <- v;
4580 reshape state.winw state.winh;
4583 src#int "scroll handle height"
4584 (fun () -> conf.scrollh)
4585 (fun v -> conf.scrollh <- v;);
4587 src#int "thumbnail width"
4588 (fun () -> conf.thumbw)
4589 (fun v ->
4590 conf.thumbw <- min 4096 v;
4591 match oldmode with
4592 | Birdseye beye ->
4593 leavebirdseye beye false;
4594 enterbirdseye ()
4595 | _ -> ()
4598 let mode = state.mode in
4599 src#string "columns"
4600 (fun () ->
4601 match conf.columns with
4602 | Csingle _ -> "1"
4603 | Cmulti (multi, _) -> multicolumns_to_string multi
4604 | Csplit (count, _) -> "-" ^ string_of_int count
4606 (fun v ->
4607 let n, a, b = multicolumns_of_string v in
4608 setcolumns mode n a b);
4610 sep ();
4611 src#caption "Pixmap cache" 0;
4612 src#int_with_suffix "size (advisory)"
4613 (fun () -> conf.memlimit)
4614 (fun v -> conf.memlimit <- v);
4616 src#caption2 "used"
4617 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4618 (string_with_suffix_of_int state.memused)
4619 (Hashtbl.length state.tilemap)) 1;
4621 sep ();
4622 src#caption "Layout" 0;
4623 src#caption2 "Dimension"
4624 (fun () ->
4625 Printf.sprintf "%dx%d (virtual %dx%d)"
4626 state.winw state.winh
4627 state.w state.maxy)
4629 if conf.debug
4630 then
4631 src#caption2 "Position" (fun () ->
4632 Printf.sprintf "%dx%d" state.x state.y
4634 else
4635 src#caption2 "Position" (fun () -> describe_location ()) 1
4638 sep ();
4639 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4640 "Save these parameters as global defaults at exit"
4641 (fun () -> conf.bedefault)
4642 (fun v -> conf.bedefault <- v)
4645 sep ();
4646 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4647 src#bool ~offset:0 ~btos "Extended parameters"
4648 (fun () -> !showextended)
4649 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4650 if !showextended
4651 then (
4652 src#bool "checkers"
4653 (fun () -> conf.checkers)
4654 (fun v -> conf.checkers <- v; setcheckers v);
4655 src#bool "update cursor"
4656 (fun () -> conf.updatecurs)
4657 (fun v -> conf.updatecurs <- v);
4658 src#bool "verbose"
4659 (fun () -> conf.verbose)
4660 (fun v -> conf.verbose <- v);
4661 src#bool "invert colors"
4662 (fun () -> conf.invert)
4663 (fun v -> conf.invert <- v);
4664 src#bool "max fit"
4665 (fun () -> conf.maxhfit)
4666 (fun v -> conf.maxhfit <- v);
4667 src#bool "redirect stderr"
4668 (fun () -> conf.redirectstderr)
4669 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4670 src#bool "pax mode"
4671 (fun () -> conf.pax != None)
4672 (fun v ->
4673 if v
4674 then conf.pax <- Some (ref (now (), 0, 0))
4675 else conf.pax <- None);
4676 src#string "uri launcher"
4677 (fun () -> conf.urilauncher)
4678 (fun v -> conf.urilauncher <- v);
4679 src#string "path launcher"
4680 (fun () -> conf.pathlauncher)
4681 (fun v -> conf.pathlauncher <- v);
4682 src#string "tile size"
4683 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4684 (fun v ->
4686 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4687 conf.tilew <- max 64 w;
4688 conf.tileh <- max 64 h;
4689 flushtiles ();
4690 with exn ->
4691 state.text <- Printf.sprintf "bad tile size `%s': %s"
4692 v (exntos exn)
4694 src#int "texture count"
4695 (fun () -> conf.texcount)
4696 (fun v ->
4697 if realloctexts v
4698 then conf.texcount <- v
4699 else showtext '!' " Failed to set texture count please retry later"
4701 src#int "slice height"
4702 (fun () -> conf.sliceheight)
4703 (fun v ->
4704 conf.sliceheight <- v;
4705 wcmd "sliceh %d" conf.sliceheight;
4707 src#int "anti-aliasing level"
4708 (fun () -> conf.aalevel)
4709 (fun v ->
4710 conf.aalevel <- bound v 0 8;
4711 state.anchor <- getanchor ();
4712 opendoc state.path state.password;
4714 src#string "page scroll scaling factor"
4715 (fun () -> string_of_float conf.pgscale)
4716 (fun v ->
4718 let s = float_of_string v in
4719 conf.pgscale <- s
4720 with exn ->
4721 state.text <- Printf.sprintf
4722 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4725 src#int "ui font size"
4726 (fun () -> fstate.fontsize)
4727 (fun v -> setfontsize (bound v 5 100));
4728 src#int "hint font size"
4729 (fun () -> conf.hfsize)
4730 (fun v -> conf.hfsize <- bound v 5 100);
4731 colorp "background color"
4732 (fun () -> conf.bgcolor)
4733 (fun v -> conf.bgcolor <- v);
4734 src#bool "crop hack"
4735 (fun () -> conf.crophack)
4736 (fun v -> conf.crophack <- v);
4737 src#string "trim fuzz"
4738 (fun () -> irect_to_string conf.trimfuzz)
4739 (fun v ->
4741 conf.trimfuzz <- irect_of_string v;
4742 if conf.trimmargins
4743 then settrim true conf.trimfuzz;
4744 with exn ->
4745 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4747 src#string "throttle"
4748 (fun () ->
4749 match conf.maxwait with
4750 | None -> "show place holder if page is not ready"
4751 | Some time ->
4752 if time = infinity
4753 then "wait for page to fully render"
4754 else
4755 "wait " ^ string_of_float time
4756 ^ " seconds before showing placeholder"
4758 (fun v ->
4760 let f = float_of_string v in
4761 if f <= 0.0
4762 then conf.maxwait <- None
4763 else conf.maxwait <- Some f
4764 with exn ->
4765 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4767 src#string "ghyll scroll"
4768 (fun () ->
4769 match conf.ghyllscroll with
4770 | None -> ""
4771 | Some nab -> ghyllscroll_to_string nab
4773 (fun v ->
4775 let gs =
4776 if String.length v = 0
4777 then None
4778 else Some (ghyllscroll_of_string v)
4780 conf.ghyllscroll <- gs
4781 with exn ->
4782 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4784 src#string "selection command"
4785 (fun () -> conf.selcmd)
4786 (fun v -> conf.selcmd <- v);
4787 src#string "synctex command"
4788 (fun () -> conf.stcmd)
4789 (fun v -> conf.stcmd <- v);
4790 src#string "pax command"
4791 (fun () -> conf.paxcmd)
4792 (fun v -> conf.paxcmd <- v);
4793 src#colorspace "color space"
4794 (fun () -> CSTE.to_string conf.colorspace)
4795 (fun v ->
4796 conf.colorspace <- CSTE.of_int v;
4797 wcmd "cs %d" v;
4798 load state.layout;
4800 src#paxmark "pax mark method"
4801 (fun () -> MTE.to_string conf.paxmark)
4802 (fun v -> conf.paxmark <- MTE.of_int v);
4803 if pbousable ()
4804 then
4805 src#bool "use PBO"
4806 (fun () -> conf.usepbo)
4807 (fun v -> conf.usepbo <- v);
4808 src#bool "mouse wheel scrolls pages"
4809 (fun () -> conf.wheelbypage)
4810 (fun v -> conf.wheelbypage <- v);
4811 src#bool "open remote links in a new instance"
4812 (fun () -> conf.riani)
4813 (fun v -> conf.riani <- v);
4816 sep ();
4817 src#caption "Document" 0;
4818 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4819 src#caption2 "Pages"
4820 (fun () -> string_of_int state.pagecount) 1;
4821 src#caption2 "Dimensions"
4822 (fun () -> string_of_int (List.length state.pdims)) 1;
4823 if conf.trimmargins
4824 then (
4825 sep ();
4826 src#caption "Trimmed margins" 0;
4827 src#caption2 "Dimensions"
4828 (fun () -> string_of_int (List.length state.pdims)) 1;
4831 sep ();
4832 src#caption "OpenGL" 0;
4833 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4834 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4836 sep ();
4837 src#caption "Location" 0;
4838 if String.length state.origin > 0
4839 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4840 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4842 src#reset prevmode prevuioh;
4844 fun () ->
4845 state.text <- "";
4846 let prevmode = state.mode
4847 and prevuioh = state.uioh in
4848 fillsrc prevmode prevuioh;
4849 let source = (src :> lvsource) in
4850 let modehash = findkeyhash conf "info" in
4851 state.uioh <- coe (object (self)
4852 inherit listview ~source ~trusted:true ~modehash as super
4853 val mutable m_prevmemused = 0
4854 method infochanged = function
4855 | Memused ->
4856 if m_prevmemused != state.memused
4857 then (
4858 m_prevmemused <- state.memused;
4859 G.postRedisplay "memusedchanged";
4861 | Pdim -> G.postRedisplay "pdimchanged"
4862 | Docinfo -> fillsrc prevmode prevuioh
4864 method key key mask =
4865 if not (Wsi.withctrl mask)
4866 then
4867 match key with
4868 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4869 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4870 | _ -> super#key key mask
4871 else super#key key mask
4872 end);
4873 G.postRedisplay "info";
4876 let enterhelpmode =
4877 let source =
4878 (object
4879 inherit lvsourcebase
4880 method getitemcount = Array.length state.help
4881 method getitem n =
4882 let s, l, _ = state.help.(n) in
4883 (s, l)
4885 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4886 let optuioh =
4887 if not cancel
4888 then (
4889 m_qsearch <- qsearch;
4890 match state.help.(active) with
4891 | _, _, Action f -> Some (f uioh)
4892 | _ -> Some (uioh)
4894 else None
4896 m_active <- active;
4897 m_first <- first;
4898 m_pan <- pan;
4899 optuioh
4901 method hasaction n =
4902 match state.help.(n) with
4903 | _, _, Action _ -> true
4904 | _ -> false
4906 initializer
4907 m_active <- -1
4908 end)
4909 in fun () ->
4910 let modehash = findkeyhash conf "help" in
4911 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4912 G.postRedisplay "help";
4915 let entermsgsmode =
4916 let msgsource =
4917 let re = Str.regexp "[\r\n]" in
4918 (object
4919 inherit lvsourcebase
4920 val mutable m_items = [||]
4922 method getitemcount = 1 + Array.length m_items
4924 method getitem n =
4925 if n = 0
4926 then "[Clear]", 0
4927 else m_items.(n-1), 0
4929 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4930 ignore uioh;
4931 if not cancel
4932 then (
4933 if active = 0
4934 then Buffer.clear state.errmsgs;
4935 m_qsearch <- qsearch;
4937 m_active <- active;
4938 m_first <- first;
4939 m_pan <- pan;
4940 None
4942 method hasaction n =
4943 n = 0
4945 method reset =
4946 state.newerrmsgs <- false;
4947 let l = Str.split re (Buffer.contents state.errmsgs) in
4948 m_items <- Array.of_list l
4950 initializer
4951 m_active <- 0
4952 end)
4953 in fun () ->
4954 state.text <- "";
4955 msgsource#reset;
4956 let source = (msgsource :> lvsource) in
4957 let modehash = findkeyhash conf "listview" in
4958 state.uioh <- coe (object
4959 inherit listview ~source ~trusted:false ~modehash as super
4960 method display =
4961 if state.newerrmsgs
4962 then msgsource#reset;
4963 super#display
4964 end);
4965 G.postRedisplay "msgs";
4968 let quickbookmark ?title () =
4969 match state.layout with
4970 | [] -> ()
4971 | l :: _ ->
4972 let title =
4973 match title with
4974 | None ->
4975 let sec = Unix.gettimeofday () in
4976 let tm = Unix.localtime sec in
4977 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4978 (l.pageno+1)
4979 tm.Unix.tm_mday
4980 tm.Unix.tm_mon
4981 (tm.Unix.tm_year + 1900)
4982 tm.Unix.tm_hour
4983 tm.Unix.tm_min
4984 | Some title -> title
4986 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4989 let setautoscrollspeed step goingdown =
4990 let incr = max 1 ((abs step) / 2) in
4991 let incr = if goingdown then incr else -incr in
4992 let astep = step + incr in
4993 state.autoscroll <- Some astep;
4996 let gotounder = function
4997 | Ulinkgoto (pageno, top) ->
4998 if pageno >= 0
4999 then (
5000 addnav ();
5001 gotopage1 pageno top;
5004 | Ulinkuri s ->
5005 gotouri s
5007 | Uremote (filename, pageno) ->
5008 let path =
5009 if String.length filename > 0
5010 then
5011 if Filename.is_relative filename
5012 then
5013 let dir = Filename.dirname state.path in
5014 let dir =
5015 if Filename.is_implicit dir
5016 then Filename.concat (Sys.getcwd ()) dir
5017 else dir
5019 Filename.concat dir filename
5020 else filename
5021 else ""
5023 let path =
5024 if Sys.file_exists path
5025 then path
5026 else ""
5028 if String.length path > 0
5029 then (
5030 if conf.riani
5031 then
5032 let command = !selfexec ^ " " ^ path in
5033 try popen command []
5034 with exn ->
5035 Printf.eprintf
5036 "failed to execute `%s': %s\n" command (exntos exn);
5037 flush stderr;
5038 else
5039 let anchor = getanchor () in
5040 let ranchor = state.path, state.password, anchor, state.origin in
5041 state.origin <- "";
5042 state.anchor <- (pageno, 0.0, 0.0);
5043 state.ranchors <- ranchor :: state.ranchors;
5044 opendoc path "";
5046 else showtext '!' ("Could not find " ^ filename)
5048 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5051 let canpan () =
5052 match conf.columns with
5053 | Csplit _ -> true
5054 | _ -> state.x != 0 || conf.zoom > 1.0
5057 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5059 let existsinrow pageno (columns, coverA, coverB) p =
5060 let last = ((pageno - coverA) mod columns) + columns in
5061 let rec any = function
5062 | [] -> false
5063 | l :: rest ->
5064 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5065 then p l
5066 else (
5067 if not (p l)
5068 then (if l.pageno = last then false else any rest)
5069 else true
5072 any state.layout
5075 let nextpage () =
5076 match state.layout with
5077 | [] ->
5078 let pageno = page_of_y state.y in
5079 gotoghyll (getpagey (pageno+1))
5080 | l :: rest ->
5081 match conf.columns with
5082 | Csingle _ ->
5083 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5084 then
5085 let y = clamp (pgscale state.winh) in
5086 gotoghyll y
5087 else
5088 let pageno = min (l.pageno+1) (state.pagecount-1) in
5089 gotoghyll (getpagey pageno)
5090 | Cmulti ((c, _, _) as cl, _) ->
5091 if conf.presentation
5092 && (existsinrow l.pageno cl
5093 (fun l -> l.pageh > l.pagey + l.pagevh))
5094 then
5095 let y = clamp (pgscale state.winh) in
5096 gotoghyll y
5097 else
5098 let pageno = min (l.pageno+c) (state.pagecount-1) in
5099 gotoghyll (getpagey pageno)
5100 | Csplit (n, _) ->
5101 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5102 then
5103 let pagey, pageh = getpageyh l.pageno in
5104 let pagey = pagey + pageh * l.pagecol in
5105 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5106 gotoghyll (pagey + pageh + ips)
5109 let prevpage () =
5110 match state.layout with
5111 | [] ->
5112 let pageno = page_of_y state.y in
5113 gotoghyll (getpagey (pageno-1))
5114 | l :: _ ->
5115 match conf.columns with
5116 | Csingle _ ->
5117 if conf.presentation && l.pagey != 0
5118 then
5119 gotoghyll (clamp (pgscale ~-(state.winh)))
5120 else
5121 let pageno = max 0 (l.pageno-1) in
5122 gotoghyll (getpagey pageno)
5123 | Cmulti ((c, _, coverB) as cl, _) ->
5124 if conf.presentation &&
5125 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5126 then
5127 gotoghyll (clamp (pgscale ~-(state.winh)))
5128 else
5129 let decr =
5130 if l.pageno = state.pagecount - coverB
5131 then 1
5132 else c
5134 let pageno = max 0 (l.pageno-decr) in
5135 gotoghyll (getpagey pageno)
5136 | Csplit (n, _) ->
5137 let y =
5138 if l.pagecol = 0
5139 then
5140 if l.pageno = 0
5141 then l.pagey
5142 else
5143 let pageno = max 0 (l.pageno-1) in
5144 let pagey, pageh = getpageyh pageno in
5145 pagey + (n-1)*pageh
5146 else
5147 let pagey, pageh = getpageyh l.pageno in
5148 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5150 gotoghyll y
5153 let viewkeyboard key mask =
5154 let enttext te =
5155 let mode = state.mode in
5156 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5157 state.text <- "";
5158 enttext ();
5159 G.postRedisplay "view:enttext"
5161 let ctrl = Wsi.withctrl mask in
5162 let key =
5163 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5165 match key with
5166 | 81 -> (* Q *)
5167 exit 0
5169 | 0xff63 -> (* insert *)
5170 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5171 then (
5172 state.mode <- LinkNav (Ltgendir 0);
5173 gotoy state.y;
5175 else showtext '!' "Keyboard link navigation does not work under rotation"
5177 | 0xff1b | 113 -> (* escape / q *)
5178 begin match state.mstate with
5179 | Mzoomrect _ ->
5180 state.mstate <- Mnone;
5181 Wsi.setcursor Wsi.CURSOR_INHERIT;
5182 G.postRedisplay "kill zoom rect";
5183 | _ ->
5184 begin match state.mode with
5185 | LinkNav _ ->
5186 state.mode <- View;
5187 G.postRedisplay "esc leave linknav"
5188 | _ ->
5189 match state.ranchors with
5190 | [] -> raise Quit
5191 | (path, password, anchor, origin) :: rest ->
5192 state.ranchors <- rest;
5193 state.anchor <- anchor;
5194 state.origin <- origin;
5195 opendoc path password
5196 end;
5197 end;
5199 | 0xff08 -> (* backspace *)
5200 gotoghyll (getnav ~-1)
5202 | 111 -> (* o *)
5203 enteroutlinemode ()
5205 | 117 -> (* u *)
5206 state.rects <- [];
5207 state.text <- "";
5208 G.postRedisplay "dehighlight";
5210 | 47 | 63 -> (* / ? *)
5211 let ondone isforw s =
5212 cbput state.hists.pat s;
5213 state.searchpattern <- s;
5214 search s isforw
5216 let s = String.create 1 in
5217 s.[0] <- Char.chr key;
5218 enttext (s, "", Some (onhist state.hists.pat),
5219 textentry, ondone (key = 47), true)
5221 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5222 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5223 setzoom (conf.zoom +. incr)
5225 | 43 | 0xffab -> (* + *)
5226 let ondone s =
5227 let n =
5228 try int_of_string s with exc ->
5229 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5230 max_int
5232 if n != max_int
5233 then (
5234 conf.pagebias <- n;
5235 state.text <- "page bias is now " ^ string_of_int n;
5238 enttext ("page bias: ", "", None, intentry, ondone, true)
5240 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5241 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5242 setzoom (max 0.01 (conf.zoom -. decr))
5244 | 45 | 0xffad -> (* - *)
5245 let ondone msg = state.text <- msg in
5246 enttext (
5247 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5248 optentry state.mode, ondone, true
5251 | 48 when ctrl -> (* ctrl-0 *)
5252 if conf.zoom = 1.0
5253 then (
5254 state.x <- 0;
5255 gotoy state.y
5257 else setzoom 1.0
5259 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5260 let cols =
5261 match conf.columns with
5262 | Csingle _ | Cmulti _ -> 1
5263 | Csplit (n, _) -> n
5265 let h = state.winh -
5266 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5268 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5269 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5270 then setzoom zoom
5272 | 51 when ctrl -> (* ctrl-3 *)
5273 let fm =
5274 match conf.fitmodel with
5275 | FitWidth -> FitProportional
5276 | FitProportional -> FitPage
5277 | FitPage -> FitWidth
5279 state.text <- "fit model: " ^ FMTE.to_string fm;
5280 reqlayout conf.angle fm
5282 | 0xffc6 -> (* f9 *)
5283 togglebirdseye ()
5285 | 57 when ctrl -> (* ctrl-9 *)
5286 togglebirdseye ()
5288 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5289 when not ctrl -> (* 0..9 *)
5290 let ondone s =
5291 let n =
5292 try int_of_string s with exc ->
5293 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5296 if n >= 0
5297 then (
5298 addnav ();
5299 cbput state.hists.pag (string_of_int n);
5300 gotopage1 (n + conf.pagebias - 1) 0;
5303 let pageentry text key =
5304 match Char.unsafe_chr key with
5305 | 'g' -> TEdone text
5306 | _ -> intentry text key
5308 let text = "x" in text.[0] <- Char.chr key;
5309 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5311 | 98 -> (* b *)
5312 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5313 reshape state.winw state.winh;
5315 | 108 -> (* l *)
5316 conf.hlinks <- not conf.hlinks;
5317 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5318 G.postRedisplay "toggle highlightlinks";
5320 | 70 -> (* F *)
5321 state.glinks <- true;
5322 let mode = state.mode in
5323 state.mode <- Textentry (
5324 (":", "", None, linknentry, linkndone gotounder, false),
5325 (fun _ ->
5326 state.glinks <- false;
5327 state.mode <- mode)
5329 state.text <- "";
5330 G.postRedisplay "view:linkent(F)"
5332 | 121 -> (* y *)
5333 state.glinks <- true;
5334 let mode = state.mode in
5335 state.mode <- Textentry (
5337 ":", "", None, linknentry, linkndone (fun under ->
5338 selstring (undertext under);
5339 ), false
5341 fun _ ->
5342 state.glinks <- false;
5343 state.mode <- mode
5345 state.text <- "";
5346 G.postRedisplay "view:linkent"
5348 | 97 -> (* a *)
5349 begin match state.autoscroll with
5350 | Some step ->
5351 conf.autoscrollstep <- step;
5352 state.autoscroll <- None
5353 | None ->
5354 if conf.autoscrollstep = 0
5355 then state.autoscroll <- Some 1
5356 else state.autoscroll <- Some conf.autoscrollstep
5359 | 112 when ctrl -> (* ctrl-p *)
5360 launchpath ()
5362 | 80 -> (* P *)
5363 setpresentationmode (not conf.presentation);
5364 showtext ' ' ("presentation mode " ^
5365 if conf.presentation then "on" else "off");
5367 | 102 -> (* f *)
5368 if List.mem Wsi.Fullscreen state.winstate
5369 then Wsi.reshape conf.cwinw conf.cwinh
5370 else Wsi.fullscreen ()
5372 | 112 | 78 -> (* p|N *)
5373 search state.searchpattern false
5375 | 110 | 0xffc0 -> (* n|F3 *)
5376 search state.searchpattern true
5378 | 116 -> (* t *)
5379 begin match state.layout with
5380 | [] -> ()
5381 | l :: _ ->
5382 gotoghyll (getpagey l.pageno)
5385 | 32 -> (* space *)
5386 nextpage ()
5388 | 0xff9f | 0xffff -> (* delete *)
5389 prevpage ()
5391 | 61 -> (* = *)
5392 showtext ' ' (describe_location ());
5394 | 119 -> (* w *)
5395 begin match state.layout with
5396 | [] -> ()
5397 | l :: _ ->
5398 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5399 G.postRedisplay "w"
5402 | 39 -> (* ' *)
5403 enterbookmarkmode ()
5405 | 104 | 0xffbe -> (* h|F1 *)
5406 enterhelpmode ()
5408 | 105 -> (* i *)
5409 enterinfomode ()
5411 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5412 entermsgsmode ()
5414 | 109 -> (* m *)
5415 let ondone s =
5416 match state.layout with
5417 | l :: _ ->
5418 if String.length s > 0
5419 then
5420 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5421 | _ -> ()
5423 enttext ("bookmark: ", "", None, textentry, ondone, true)
5425 | 126 -> (* ~ *)
5426 quickbookmark ();
5427 showtext ' ' "Quick bookmark added";
5429 | 122 -> (* z *)
5430 begin match state.layout with
5431 | l :: _ ->
5432 let rect = getpdimrect l.pagedimno in
5433 let w, h =
5434 if conf.crophack
5435 then
5436 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5437 truncate (1.2 *. (rect.(3) -. rect.(0))))
5438 else
5439 (truncate (rect.(1) -. rect.(0)),
5440 truncate (rect.(3) -. rect.(0)))
5442 let w = truncate ((float w)*.conf.zoom)
5443 and h = truncate ((float h)*.conf.zoom) in
5444 if w != 0 && h != 0
5445 then (
5446 state.anchor <- getanchor ();
5447 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5449 G.postRedisplay "z";
5451 | [] -> ()
5454 | 120 -> state.roam ()
5455 | 60 | 62 -> (* < > *)
5456 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5458 | 91 | 93 -> (* [ ] *)
5459 conf.colorscale <-
5460 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5462 G.postRedisplay "brightness";
5464 | 99 when state.mode = View -> (* [alt]-c *)
5465 if Wsi.withalt mask
5466 then (
5467 if conf.zoom > 1.0
5468 then
5469 let m = (wadjsb state.winw - state.w) / 2 in
5470 state.x <- m;
5471 gotoy_and_clear_text state.y
5473 else
5474 let (c, a, b), z =
5475 match state.prevcolumns with
5476 | None -> (1, 0, 0), 1.0
5477 | Some (columns, z) ->
5478 let cab =
5479 match columns with
5480 | Csplit (c, _) -> -c, 0, 0
5481 | Cmulti ((c, a, b), _) -> c, a, b
5482 | Csingle _ -> 1, 0, 0
5484 cab, z
5486 setcolumns View c a b;
5487 setzoom z
5489 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5490 setzoom state.prevzoom
5492 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5493 begin match state.autoscroll with
5494 | None ->
5495 begin match state.mode with
5496 | Birdseye beye -> upbirdseye 1 beye
5497 | _ ->
5498 if ctrl
5499 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5500 else (
5501 if not (Wsi.withshift mask) && conf.presentation
5502 then prevpage ()
5503 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5506 | Some n ->
5507 setautoscrollspeed n false
5510 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5511 begin match state.autoscroll with
5512 | None ->
5513 begin match state.mode with
5514 | Birdseye beye -> downbirdseye 1 beye
5515 | _ ->
5516 if ctrl
5517 then gotoy_and_clear_text (clamp (state.winh/2))
5518 else (
5519 if not (Wsi.withshift mask) && conf.presentation
5520 then nextpage ()
5521 else gotoy_and_clear_text (clamp conf.scrollstep)
5524 | Some n ->
5525 setautoscrollspeed n true
5528 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5529 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5530 if canpan ()
5531 then
5532 let dx =
5533 if ctrl
5534 then state.winw / 2
5535 else conf.hscrollstep
5537 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5538 state.x <- panbound (state.x + dx);
5539 gotoy_and_clear_text state.y
5540 else (
5541 state.text <- "";
5542 G.postRedisplay "left/right"
5545 | 0xff55 | 0xff9a -> (* (kp) prior *)
5546 let y =
5547 if ctrl
5548 then
5549 match state.layout with
5550 | [] -> state.y
5551 | l :: _ -> state.y - l.pagey
5552 else
5553 clamp (pgscale (-state.winh))
5555 gotoghyll y
5557 | 0xff56 | 0xff9b -> (* (kp) next *)
5558 let y =
5559 if ctrl
5560 then
5561 match List.rev state.layout with
5562 | [] -> state.y
5563 | l :: _ -> getpagey l.pageno
5564 else
5565 clamp (pgscale state.winh)
5567 gotoghyll y
5569 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5570 gotoghyll 0
5571 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5572 gotoghyll (clamp state.maxy)
5574 | 0xff53 | 0xff98
5575 when Wsi.withalt mask -> (* alt-(kp) right *)
5576 gotoghyll (getnav 1)
5577 | 0xff51 | 0xff96
5578 when Wsi.withalt mask -> (* alt-(kp) left *)
5579 gotoghyll (getnav ~-1)
5581 | 114 -> (* r *)
5582 reload ()
5584 | 118 when conf.debug -> (* v *)
5585 state.rects <- [];
5586 List.iter (fun l ->
5587 match getopaque l.pageno with
5588 | None -> ()
5589 | Some opaque ->
5590 let x0, y0, x1, y1 = pagebbox opaque in
5591 let a,b = float x0, float y0 in
5592 let c,d = float x1, float y0 in
5593 let e,f = float x1, float y1 in
5594 let h,j = float x0, float y1 in
5595 let rect = (a,b,c,d,e,f,h,j) in
5596 debugrect rect;
5597 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5598 ) state.layout;
5599 G.postRedisplay "v";
5601 | _ ->
5602 vlog "huh? %s" (Wsi.keyname key)
5605 let linknavkeyboard key mask linknav =
5606 let getpage pageno =
5607 let rec loop = function
5608 | [] -> None
5609 | l :: _ when l.pageno = pageno -> Some l
5610 | _ :: rest -> loop rest
5611 in loop state.layout
5613 let doexact (pageno, n) =
5614 match getopaque pageno, getpage pageno with
5615 | Some opaque, Some l ->
5616 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5617 then
5618 let under = getlink opaque n in
5619 G.postRedisplay "link gotounder";
5620 gotounder under;
5621 state.mode <- View;
5622 else
5623 let opt, dir =
5624 match key with
5625 | 0xff50 -> (* home *)
5626 Some (findlink opaque LDfirst), -1
5628 | 0xff57 -> (* end *)
5629 Some (findlink opaque LDlast), 1
5631 | 0xff51 -> (* left *)
5632 Some (findlink opaque (LDleft n)), -1
5634 | 0xff53 -> (* right *)
5635 Some (findlink opaque (LDright n)), 1
5637 | 0xff52 -> (* up *)
5638 Some (findlink opaque (LDup n)), -1
5640 | 0xff54 -> (* down *)
5641 Some (findlink opaque (LDdown n)), 1
5643 | _ -> None, 0
5645 let pwl l dir =
5646 begin match findpwl l.pageno dir with
5647 | Pwlnotfound -> ()
5648 | Pwl pageno ->
5649 let notfound dir =
5650 state.mode <- LinkNav (Ltgendir dir);
5651 let y, h = getpageyh pageno in
5652 let y =
5653 if dir < 0
5654 then y + h - state.winh
5655 else y
5657 gotoy y
5659 begin match getopaque pageno, getpage pageno with
5660 | Some opaque, Some _ ->
5661 let link =
5662 let ld = if dir > 0 then LDfirst else LDlast in
5663 findlink opaque ld
5665 begin match link with
5666 | Lfound m ->
5667 showlinktype (getlink opaque m);
5668 state.mode <- LinkNav (Ltexact (pageno, m));
5669 G.postRedisplay "linknav jpage";
5670 | _ -> notfound dir
5671 end;
5672 | _ -> notfound dir
5673 end;
5674 end;
5676 begin match opt with
5677 | Some Lnotfound -> pwl l dir;
5678 | Some (Lfound m) ->
5679 if m = n
5680 then pwl l dir
5681 else (
5682 let _, y0, _, y1 = getlinkrect opaque m in
5683 if y0 < l.pagey
5684 then gotopage1 l.pageno y0
5685 else (
5686 let d = fstate.fontsize + 1 in
5687 if y1 - l.pagey > l.pagevh - d
5688 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5689 else G.postRedisplay "linknav";
5691 showlinktype (getlink opaque m);
5692 state.mode <- LinkNav (Ltexact (l.pageno, m));
5695 | None -> viewkeyboard key mask
5696 end;
5697 | _ -> viewkeyboard key mask
5699 if key = 0xff63
5700 then (
5701 state.mode <- View;
5702 G.postRedisplay "leave linknav"
5704 else
5705 match linknav with
5706 | Ltgendir _ -> viewkeyboard key mask
5707 | Ltexact exact -> doexact exact
5710 let keyboard key mask =
5711 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5712 then wcmd "interrupt"
5713 else state.uioh <- state.uioh#key key mask
5716 let birdseyekeyboard key mask
5717 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5718 let incr =
5719 match conf.columns with
5720 | Csingle _ -> 1
5721 | Cmulti ((c, _, _), _) -> c
5722 | Csplit _ -> failwith "bird's eye split mode"
5724 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5725 match key with
5726 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5727 let y, h = getpageyh pageno in
5728 let top = (state.winh - h) / 2 in
5729 gotoy (max 0 (y - top))
5730 | 0xff0d (* enter *)
5731 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5732 | 0xff1b -> leavebirdseye beye true (* escape *)
5733 | 0xff52 -> upbirdseye incr beye (* up *)
5734 | 0xff54 -> downbirdseye incr beye (* down *)
5735 | 0xff51 -> upbirdseye 1 beye (* left *)
5736 | 0xff53 -> downbirdseye 1 beye (* right *)
5738 | 0xff55 -> (* prior *)
5739 begin match state.layout with
5740 | l :: _ ->
5741 if l.pagey != 0
5742 then (
5743 state.mode <- Birdseye (
5744 oconf, leftx, l.pageno, hooverpageno, anchor
5746 gotopage1 l.pageno 0;
5748 else (
5749 let layout = layout (state.y-state.winh) (pgh state.layout) in
5750 match layout with
5751 | [] -> gotoy (clamp (-state.winh))
5752 | l :: _ ->
5753 state.mode <- Birdseye (
5754 oconf, leftx, l.pageno, hooverpageno, anchor
5756 gotopage1 l.pageno 0
5759 | [] -> gotoy (clamp (-state.winh))
5760 end;
5762 | 0xff56 -> (* next *)
5763 begin match List.rev state.layout with
5764 | l :: _ ->
5765 let layout = layout (state.y + (pgh state.layout)) state.winh in
5766 begin match layout with
5767 | [] ->
5768 let incr = l.pageh - l.pagevh in
5769 if incr = 0
5770 then (
5771 state.mode <-
5772 Birdseye (
5773 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5775 G.postRedisplay "birdseye pagedown";
5777 else gotoy (clamp (incr + conf.interpagespace*2));
5779 | l :: _ ->
5780 state.mode <-
5781 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5782 gotopage1 l.pageno 0;
5785 | [] -> gotoy (clamp state.winh)
5786 end;
5788 | 0xff50 -> (* home *)
5789 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5790 gotopage1 0 0
5792 | 0xff57 -> (* end *)
5793 let pageno = state.pagecount - 1 in
5794 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5795 if not (pagevisible state.layout pageno)
5796 then
5797 let h =
5798 match List.rev state.pdims with
5799 | [] -> state.winh
5800 | (_, _, h, _) :: _ -> h
5802 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5803 else G.postRedisplay "birdseye end";
5804 | _ -> viewkeyboard key mask
5807 let drawpage l =
5808 let color =
5809 match state.mode with
5810 | Textentry _ -> scalecolor 0.4
5811 | LinkNav _
5812 | View -> scalecolor 1.0
5813 | Birdseye (_, _, pageno, hooverpageno, _) ->
5814 if l.pageno = hooverpageno
5815 then scalecolor 0.9
5816 else (
5817 if l.pageno = pageno
5818 then scalecolor 1.0
5819 else scalecolor 0.8
5822 drawtiles l color;
5825 let postdrawpage l linkindexbase =
5826 match getopaque l.pageno with
5827 | Some opaque ->
5828 if tileready l l.pagex l.pagey
5829 then
5830 let x = l.pagedispx - l.pagex
5831 and y = l.pagedispy - l.pagey in
5832 let hlmask =
5833 match conf.columns with
5834 | Csingle _ | Cmulti _ ->
5835 (if conf.hlinks then 1 else 0)
5836 + (if state.glinks
5837 && not (isbirdseye state.mode) then 2 else 0)
5838 | _ -> 0
5840 let s =
5841 match state.mode with
5842 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5843 | _ -> ""
5845 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5846 else 0
5847 | _ -> 0
5850 let scrollindicator () =
5851 let sbw, ph, sh = state.uioh#scrollph in
5852 let sbh, pw, sw = state.uioh#scrollpw in
5854 GlDraw.color (0.64, 0.64, 0.64);
5855 GlDraw.rect
5856 (float (state.winw - sbw), 0.)
5857 (float state.winw, float state.winh)
5859 GlDraw.rect
5860 (0., float (state.winh - sbh))
5861 (float (wadjsb state.winw - 1), float state.winh)
5863 GlDraw.color (0.0, 0.0, 0.0);
5865 GlDraw.rect
5866 (float (state.winw - sbw), ph)
5867 (float state.winw, ph +. sh)
5869 GlDraw.rect
5870 (pw, float (state.winh - sbh))
5871 (pw +. sw, float state.winh)
5875 let showsel () =
5876 match state.mstate with
5877 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5880 | Msel ((x0, y0), (x1, y1)) ->
5881 let rec loop = function
5882 | l :: ls ->
5883 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5884 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5885 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5886 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5887 then
5888 match getopaque l.pageno with
5889 | Some opaque ->
5890 let x0, y0 = pagetranslatepoint l x0 y0 in
5891 let x1, y1 = pagetranslatepoint l x1 y1 in
5892 seltext opaque (x0, y0, x1, y1);
5893 | _ -> ()
5894 else loop ls
5895 | [] -> ()
5897 loop state.layout
5900 let showrects rects =
5901 Gl.enable `blend;
5902 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5903 GlDraw.polygon_mode `both `fill;
5904 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5905 List.iter
5906 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5907 List.iter (fun l ->
5908 if l.pageno = pageno
5909 then (
5910 let dx = float (l.pagedispx - l.pagex) in
5911 let dy = float (l.pagedispy - l.pagey) in
5912 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5913 GlDraw.begins `quads;
5915 GlDraw.vertex2 (x0+.dx, y0+.dy);
5916 GlDraw.vertex2 (x1+.dx, y1+.dy);
5917 GlDraw.vertex2 (x2+.dx, y2+.dy);
5918 GlDraw.vertex2 (x3+.dx, y3+.dy);
5920 GlDraw.ends ();
5922 ) state.layout
5923 ) rects
5925 Gl.disable `blend;
5928 let display () =
5929 GlClear.color (scalecolor2 conf.bgcolor);
5930 GlClear.clear [`color];
5931 List.iter drawpage state.layout;
5932 let rects =
5933 match state.mode with
5934 | LinkNav (Ltexact (pageno, linkno)) ->
5935 begin match getopaque pageno with
5936 | Some opaque ->
5937 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5938 (pageno, 5, (
5939 float x0, float y0,
5940 float x1, float y0,
5941 float x1, float y1,
5942 float x0, float y1)
5943 ) :: state.rects
5944 | None -> state.rects
5946 | _ -> state.rects
5948 showrects rects;
5949 let rec postloop linkindexbase = function
5950 | l :: rest ->
5951 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5952 postloop linkindexbase rest
5953 | [] -> ()
5955 showsel ();
5956 postloop 0 state.layout;
5957 state.uioh#display;
5958 begin match state.mstate with
5959 | Mzoomrect ((x0, y0), (x1, y1)) ->
5960 Gl.enable `blend;
5961 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5962 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5963 GlDraw.rect (float x0, float y0)
5964 (float x1, float y1);
5965 Gl.disable `blend;
5966 | _ -> ()
5967 end;
5968 enttext ();
5969 scrollindicator ();
5970 Wsi.swapb ();
5973 let zoomrect x y x1 y1 =
5974 let x0 = min x x1
5975 and x1 = max x x1
5976 and y0 = min y y1 in
5977 gotoy (state.y + y0);
5978 state.anchor <- getanchor ();
5979 let zoom = (float state.w) /. float (x1 - x0) in
5980 let margin =
5981 match conf.fitmodel, conf.columns with
5982 | FitPage, Csplit _ ->
5983 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5985 | _, _ ->
5986 let adjw = wadjsb state.winw in
5987 if state.w < adjw
5988 then (adjw - state.w) / 2
5989 else 0
5991 state.x <- (state.x + margin) - x0;
5992 setzoom zoom;
5993 Wsi.setcursor Wsi.CURSOR_INHERIT;
5994 state.mstate <- Mnone;
5997 let scrollx x =
5998 let winw = wadjsb state.winw - 1 in
5999 let s = float x /. float winw in
6000 let destx = truncate (float (state.w + winw) *. s) in
6001 state.x <- winw - destx;
6002 gotoy_and_clear_text state.y;
6003 state.mstate <- Mscrollx;
6006 let scrolly y =
6007 let s = float y /. float state.winh in
6008 let desty = truncate (float (state.maxy - state.winh) *. s) in
6009 gotoy_and_clear_text desty;
6010 state.mstate <- Mscrolly;
6013 let viewmouse button down x y mask =
6014 match button with
6015 | n when (n == 4 || n == 5) && not down ->
6016 if Wsi.withctrl mask
6017 then (
6018 match state.mstate with
6019 | Mzoom (oldn, i) ->
6020 if oldn = n
6021 then (
6022 if i = 2
6023 then
6024 let incr =
6025 match n with
6026 | 5 ->
6027 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6028 | _ ->
6029 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6031 let zoom = conf.zoom -. incr in
6032 setzoom zoom;
6033 state.mstate <- Mzoom (n, 0);
6034 else
6035 state.mstate <- Mzoom (n, i+1);
6037 else state.mstate <- Mzoom (n, 0)
6039 | _ -> state.mstate <- Mzoom (n, 0)
6041 else (
6042 match state.autoscroll with
6043 | Some step -> setautoscrollspeed step (n=4)
6044 | None ->
6045 if conf.wheelbypage || conf.presentation
6046 then (
6047 if n = 4
6048 then prevpage ()
6049 else nextpage ()
6051 else
6052 let incr =
6053 if n = 4
6054 then -conf.scrollstep
6055 else conf.scrollstep
6057 let incr = incr * 2 in
6058 let y = clamp incr in
6059 gotoy_and_clear_text y
6062 | n when (n = 6 || n = 7) && not down && canpan () ->
6063 state.x <-
6064 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6065 gotoy_and_clear_text state.y
6067 | 1 when Wsi.withshift mask ->
6068 state.mstate <- Mnone;
6069 if not down
6070 then (
6071 match unproject x y with
6072 | Some (pageno, ux, uy) ->
6073 let cmd = Printf.sprintf
6074 "%s %s %d %d %d"
6075 conf.stcmd state.path pageno ux uy
6077 popen cmd []
6078 | None -> ()
6081 | 1 when Wsi.withctrl mask ->
6082 if down
6083 then (
6084 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6085 state.mstate <- Mpan (x, y)
6087 else
6088 state.mstate <- Mnone
6090 | 3 ->
6091 if down
6092 then (
6093 Wsi.setcursor Wsi.CURSOR_CYCLE;
6094 let p = (x, y) in
6095 state.mstate <- Mzoomrect (p, p)
6097 else (
6098 match state.mstate with
6099 | Mzoomrect ((x0, y0), _) ->
6100 if abs (x-x0) > 10 && abs (y - y0) > 10
6101 then zoomrect x0 y0 x y
6102 else (
6103 state.mstate <- Mnone;
6104 Wsi.setcursor Wsi.CURSOR_INHERIT;
6105 G.postRedisplay "kill accidental zoom rect";
6107 | _ ->
6108 Wsi.setcursor Wsi.CURSOR_INHERIT;
6109 state.mstate <- Mnone
6112 | 1 when x > state.winw - vscrollw () ->
6113 if down
6114 then
6115 let _, position, sh = state.uioh#scrollph in
6116 if y > truncate position && y < truncate (position +. sh)
6117 then state.mstate <- Mscrolly
6118 else scrolly y
6119 else
6120 state.mstate <- Mnone
6122 | 1 when y > state.winh - hscrollh () ->
6123 if down
6124 then
6125 let _, position, sw = state.uioh#scrollpw in
6126 if x > truncate position && x < truncate (position +. sw)
6127 then state.mstate <- Mscrollx
6128 else scrollx x
6129 else
6130 state.mstate <- Mnone
6132 | 1 ->
6133 let dest = if down then getunder x y else Unone in
6134 begin match dest with
6135 | Ulinkgoto _
6136 | Ulinkuri _
6137 | Uremote _
6138 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6139 gotounder dest
6141 | Unone when down ->
6142 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6143 state.mstate <- Mpan (x, y);
6145 | Unone | Utext _ ->
6146 if down
6147 then (
6148 if conf.angle mod 360 = 0
6149 then (
6150 state.mstate <- Msel ((x, y), (x, y));
6151 G.postRedisplay "mouse select";
6154 else (
6155 match state.mstate with
6156 | Mnone -> ()
6158 | Mzoom _ | Mscrollx | Mscrolly ->
6159 state.mstate <- Mnone
6161 | Mzoomrect ((x0, y0), _) ->
6162 zoomrect x0 y0 x y
6164 | Mpan _ ->
6165 Wsi.setcursor Wsi.CURSOR_INHERIT;
6166 state.mstate <- Mnone
6168 | Msel ((x0, y0), (x1, y1)) ->
6169 let rec loop = function
6170 | [] -> ()
6171 | l :: rest ->
6172 let inside =
6173 let a0 = l.pagedispy in
6174 let a1 = a0 + l.pagevh in
6175 let b0 = l.pagedispx in
6176 let b1 = b0 + l.pagevw in
6177 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6178 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6180 if inside
6181 then
6182 match getopaque l.pageno with
6183 | Some opaque ->
6184 begin
6185 match Ne.pipe () with
6186 | Ne.Exn exn ->
6187 showtext '!'
6188 (Printf.sprintf
6189 "can not create sel pipe: %s"
6190 (exntos exn));
6191 | Ne.Res (r, w) ->
6192 let doclose what fd =
6193 Ne.clo fd (fun msg ->
6194 dolog "%s close failed: %s" what msg)
6197 popen conf.selcmd [r, 0; w, -1];
6198 copysel w opaque true;
6199 doclose "pipe/r" r;
6200 G.postRedisplay "copysel";
6201 with exn ->
6202 dolog "can not execute %S: %s"
6203 conf.selcmd (exntos exn);
6204 doclose "pipe/r" r;
6205 doclose "pipe/w" w;
6207 | None -> ()
6208 else loop rest
6210 loop state.layout;
6211 Wsi.setcursor Wsi.CURSOR_INHERIT;
6212 state.mstate <- Mnone;
6216 | _ -> ()
6219 let birdseyemouse button down x y mask
6220 (conf, leftx, _, hooverpageno, anchor) =
6221 match button with
6222 | 1 when down ->
6223 let rec loop = function
6224 | [] -> ()
6225 | l :: rest ->
6226 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6227 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6228 then (
6229 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6231 else loop rest
6233 loop state.layout
6234 | 3 -> ()
6235 | _ -> viewmouse button down x y mask
6238 let mouse button down x y mask =
6239 state.uioh <- state.uioh#button button down x y mask;
6242 let motion ~x ~y =
6243 state.uioh <- state.uioh#motion x y
6246 let pmotion ~x ~y =
6247 state.uioh <- state.uioh#pmotion x y;
6250 let uioh = object
6251 method display = ()
6253 method key key mask =
6254 begin match state.mode with
6255 | Textentry textentry -> textentrykeyboard key mask textentry
6256 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6257 | View -> viewkeyboard key mask
6258 | LinkNav linknav -> linknavkeyboard key mask linknav
6259 end;
6260 state.uioh
6262 method button button bstate x y mask =
6263 begin match state.mode with
6264 | LinkNav _
6265 | View -> viewmouse button bstate x y mask
6266 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6267 | Textentry _ -> ()
6268 end;
6269 state.uioh
6271 method motion x y =
6272 begin match state.mode with
6273 | Textentry _ -> ()
6274 | View | Birdseye _ | LinkNav _ ->
6275 match state.mstate with
6276 | Mzoom _ | Mnone -> ()
6278 | Mpan (x0, y0) ->
6279 let dx = x - x0
6280 and dy = y0 - y in
6281 state.mstate <- Mpan (x, y);
6282 if canpan ()
6283 then state.x <- panbound (state.x + dx);
6284 let y = clamp dy in
6285 gotoy_and_clear_text y
6287 | Msel (a, _) ->
6288 state.mstate <- Msel (a, (x, y));
6289 G.postRedisplay "motion select";
6291 | Mscrolly ->
6292 let y = min state.winh (max 0 y) in
6293 scrolly y
6295 | Mscrollx ->
6296 let x = min state.winw (max 0 x) in
6297 scrollx x
6299 | Mzoomrect (p0, _) ->
6300 state.mstate <- Mzoomrect (p0, (x, y));
6301 G.postRedisplay "motion zoomrect";
6302 end;
6303 state.uioh
6305 method pmotion x y =
6306 begin match state.mode with
6307 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6308 let rec loop = function
6309 | [] ->
6310 if hooverpageno != -1
6311 then (
6312 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6313 G.postRedisplay "pmotion birdseye no hoover";
6315 | l :: rest ->
6316 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6317 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6318 then (
6319 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6320 G.postRedisplay "pmotion birdseye hoover";
6322 else loop rest
6324 loop state.layout
6326 | Textentry _ -> ()
6328 | LinkNav _
6329 | View ->
6330 match state.mstate with
6331 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6333 | Mnone ->
6334 updateunder x y;
6335 match conf.pax with
6336 | None -> ()
6337 | Some r ->
6338 let past, _, _ = !r in
6339 let now = now () in
6340 let delta = now -. past in
6341 if delta > 0.01
6342 then paxunder x y
6343 else r := (now, x, y)
6344 end;
6345 state.uioh
6347 method infochanged _ = ()
6349 method scrollph =
6350 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6351 let p, h =
6352 if maxy = 0
6353 then 0.0, float state.winh
6354 else scrollph state.y maxy
6356 vscrollw (), p, h
6358 method scrollpw =
6359 let winw = wadjsb state.winw in
6360 let fwinw = float winw in
6361 let sw =
6362 let sw = fwinw /. float state.w in
6363 let sw = fwinw *. sw in
6364 max sw (float conf.scrollh)
6366 let position =
6367 let maxx = state.w + winw in
6368 let x = winw - state.x in
6369 let percent = float x /. float maxx in
6370 (fwinw -. sw) *. percent
6372 hscrollh (), position, sw
6374 method modehash =
6375 let modename =
6376 match state.mode with
6377 | LinkNav _ -> "links"
6378 | Textentry _ -> "textentry"
6379 | Birdseye _ -> "birdseye"
6380 | View -> "view"
6382 findkeyhash conf modename
6384 method eformsgs = true
6385 end;;
6387 module Config =
6388 struct
6389 open Parser
6391 let fontpath = ref "";;
6393 module KeyMap =
6394 Map.Make (struct type t = (int * int) let compare = compare end);;
6396 let unent s =
6397 let l = String.length s in
6398 let b = Buffer.create l in
6399 unent b s 0 l;
6400 Buffer.contents b;
6403 let home =
6404 try Sys.getenv "HOME"
6405 with exn ->
6406 prerr_endline
6407 ("Can not determine home directory location: " ^ exntos exn);
6411 let modifier_of_string = function
6412 | "alt" -> Wsi.altmask
6413 | "shift" -> Wsi.shiftmask
6414 | "ctrl" | "control" -> Wsi.ctrlmask
6415 | "meta" -> Wsi.metamask
6416 | _ -> 0
6419 let key_of_string =
6420 let r = Str.regexp "-" in
6421 fun s ->
6422 let elems = Str.full_split r s in
6423 let f n k m =
6424 let g s =
6425 let m1 = modifier_of_string s in
6426 if m1 = 0
6427 then (Wsi.namekey s, m)
6428 else (k, m lor m1)
6429 in function
6430 | Str.Delim s when n land 1 = 0 -> g s
6431 | Str.Text s -> g s
6432 | Str.Delim _ -> (k, m)
6434 let rec loop n k m = function
6435 | [] -> (k, m)
6436 | x :: xs ->
6437 let k, m = f n k m x in
6438 loop (n+1) k m xs
6440 loop 0 0 0 elems
6443 let keys_of_string =
6444 let r = Str.regexp "[ \t]" in
6445 fun s ->
6446 let elems = Str.split r s in
6447 List.map key_of_string elems
6450 let copykeyhashes c =
6451 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6454 let config_of c attrs =
6455 let apply c k v =
6457 match k with
6458 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6459 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6460 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6461 | "preload" -> { c with preload = bool_of_string v }
6462 | "page-bias" -> { c with pagebias = int_of_string v }
6463 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6464 | "horizontal-scroll-step" ->
6465 { c with hscrollstep = max (int_of_string v) 1 }
6466 | "auto-scroll-step" ->
6467 { c with autoscrollstep = max 0 (int_of_string v) }
6468 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6469 | "crop-hack" -> { c with crophack = bool_of_string v }
6470 | "throttle" ->
6471 let mw =
6472 match String.lowercase v with
6473 | "true" -> Some infinity
6474 | "false" -> None
6475 | f -> Some (float_of_string f)
6477 { c with maxwait = mw}
6478 | "highlight-links" -> { c with hlinks = bool_of_string v }
6479 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6480 | "vertical-margin" ->
6481 { c with interpagespace = max 0 (int_of_string v) }
6482 | "zoom" ->
6483 let zoom = float_of_string v /. 100. in
6484 let zoom = max zoom 0.0 in
6485 { c with zoom = zoom }
6486 | "presentation" -> { c with presentation = bool_of_string v }
6487 | "rotation-angle" -> { c with angle = int_of_string v }
6488 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6489 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6490 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6491 | "proportional-display" ->
6492 let fm =
6493 if bool_of_string v
6494 then FitProportional
6495 else FitWidth
6497 { c with fitmodel = fm }
6498 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6499 | "pixmap-cache-size" ->
6500 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6501 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6502 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6503 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6504 | "persistent-location" -> { c with jumpback = bool_of_string v }
6505 | "background-color" -> { c with bgcolor = color_of_string v }
6506 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6507 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6508 | "mupdf-store-size" ->
6509 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6510 | "checkers" -> { c with checkers = bool_of_string v }
6511 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6512 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6513 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6514 | "uri-launcher" -> { c with urilauncher = unent v }
6515 | "path-launcher" -> { c with pathlauncher = unent v }
6516 | "color-space" -> { c with colorspace = CSTE.of_string v }
6517 | "invert-colors" -> { c with invert = bool_of_string v }
6518 | "brightness" -> { c with colorscale = float_of_string v }
6519 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6520 | "ghyllscroll" ->
6521 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6522 | "columns" ->
6523 let (n, _, _) as nab = multicolumns_of_string v in
6524 if n < 0
6525 then { c with columns = Csplit (-n, [||]) }
6526 else { c with columns = Cmulti (nab, [||]) }
6527 | "birds-eye-columns" ->
6528 { c with beyecolumns = Some (max (int_of_string v) 2) }
6529 | "selection-command" -> { c with selcmd = unent v }
6530 | "synctex-command" -> { c with stcmd = unent v }
6531 | "pax-command" -> { c with paxcmd = unent v }
6532 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6533 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6534 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6535 | "use-pbo" -> { c with usepbo = bool_of_string v }
6536 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6537 | "horizontal-scrollbar-visible" ->
6538 let b =
6539 if bool_of_string v
6540 then c.scrollb lor scrollbhv
6541 else c.scrollb land (lnot scrollbhv)
6543 { c with scrollb = b }
6544 | "vertical-scrollbar-visible" ->
6545 let b =
6546 if bool_of_string v
6547 then c.scrollb lor scrollbvv
6548 else c.scrollb land (lnot scrollbvv)
6550 { c with scrollb = b }
6551 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6552 | "point-and-x" ->
6553 { c with pax =
6554 if bool_of_string v
6555 then Some (ref (0.0, 0, 0))
6556 else None }
6557 | _ -> c
6558 with exn ->
6559 prerr_endline ("Error processing attribute (`" ^
6560 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6563 let rec fold c = function
6564 | [] -> c
6565 | (k, v) :: rest ->
6566 let c = apply c k v in
6567 fold c rest
6569 fold { c with keyhashes = copykeyhashes c } attrs;
6572 let fromstring f pos n v d =
6573 try f v
6574 with exn ->
6575 dolog "Error processing attribute (%S=%S) at %d\n%s"
6576 n v pos (exntos exn)
6581 let bookmark_of attrs =
6582 let rec fold title page rely visy = function
6583 | ("title", v) :: rest -> fold v page rely visy rest
6584 | ("page", v) :: rest -> fold title v rely visy rest
6585 | ("rely", v) :: rest -> fold title page v visy rest
6586 | ("visy", v) :: rest -> fold title page rely v rest
6587 | _ :: rest -> fold title page rely visy rest
6588 | [] -> title, page, rely, visy
6590 fold "invalid" "0" "0" "0" attrs
6593 let doc_of attrs =
6594 let rec fold path page rely pan visy = function
6595 | ("path", v) :: rest -> fold v page rely pan visy rest
6596 | ("page", v) :: rest -> fold path v rely pan visy rest
6597 | ("rely", v) :: rest -> fold path page v pan visy rest
6598 | ("pan", v) :: rest -> fold path page rely v visy rest
6599 | ("visy", v) :: rest -> fold path page rely pan v rest
6600 | _ :: rest -> fold path page rely pan visy rest
6601 | [] -> path, page, rely, pan, visy
6603 fold "" "0" "0" "0" "0" attrs
6606 let map_of attrs =
6607 let rec fold rs ls = function
6608 | ("out", v) :: rest -> fold v ls rest
6609 | ("in", v) :: rest -> fold rs v rest
6610 | _ :: rest -> fold ls rs rest
6611 | [] -> ls, rs
6613 fold "" "" attrs
6616 let setconf dst src =
6617 dst.scrollbw <- src.scrollbw;
6618 dst.scrollh <- src.scrollh;
6619 dst.icase <- src.icase;
6620 dst.preload <- src.preload;
6621 dst.pagebias <- src.pagebias;
6622 dst.verbose <- src.verbose;
6623 dst.scrollstep <- src.scrollstep;
6624 dst.maxhfit <- src.maxhfit;
6625 dst.crophack <- src.crophack;
6626 dst.autoscrollstep <- src.autoscrollstep;
6627 dst.maxwait <- src.maxwait;
6628 dst.hlinks <- src.hlinks;
6629 dst.underinfo <- src.underinfo;
6630 dst.interpagespace <- src.interpagespace;
6631 dst.zoom <- src.zoom;
6632 dst.presentation <- src.presentation;
6633 dst.angle <- src.angle;
6634 dst.cwinw <- src.cwinw;
6635 dst.cwinh <- src.cwinh;
6636 dst.savebmarks <- src.savebmarks;
6637 dst.memlimit <- src.memlimit;
6638 dst.fitmodel <- src.fitmodel;
6639 dst.texcount <- src.texcount;
6640 dst.sliceheight <- src.sliceheight;
6641 dst.thumbw <- src.thumbw;
6642 dst.jumpback <- src.jumpback;
6643 dst.bgcolor <- src.bgcolor;
6644 dst.tilew <- src.tilew;
6645 dst.tileh <- src.tileh;
6646 dst.mustoresize <- src.mustoresize;
6647 dst.checkers <- src.checkers;
6648 dst.aalevel <- src.aalevel;
6649 dst.trimmargins <- src.trimmargins;
6650 dst.trimfuzz <- src.trimfuzz;
6651 dst.urilauncher <- src.urilauncher;
6652 dst.colorspace <- src.colorspace;
6653 dst.invert <- src.invert;
6654 dst.colorscale <- src.colorscale;
6655 dst.redirectstderr <- src.redirectstderr;
6656 dst.ghyllscroll <- src.ghyllscroll;
6657 dst.columns <- src.columns;
6658 dst.beyecolumns <- src.beyecolumns;
6659 dst.selcmd <- src.selcmd;
6660 dst.updatecurs <- src.updatecurs;
6661 dst.pathlauncher <- src.pathlauncher;
6662 dst.keyhashes <- copykeyhashes src;
6663 dst.hfsize <- src.hfsize;
6664 dst.hscrollstep <- src.hscrollstep;
6665 dst.pgscale <- src.pgscale;
6666 dst.usepbo <- src.usepbo;
6667 dst.wheelbypage <- src.wheelbypage;
6668 dst.stcmd <- src.stcmd;
6669 dst.paxcmd <- src.paxcmd;
6670 dst.scrollb <- src.scrollb;
6671 dst.riani <- src.riani;
6672 dst.pax <-
6673 if src.pax = None
6674 then None
6675 else Some ((ref (0.0, 0, 0)));
6678 let get s =
6679 let h = Hashtbl.create 10 in
6680 let dc = { defconf with angle = defconf.angle } in
6681 let rec toplevel v t spos _ =
6682 match t with
6683 | Vdata | Vcdata | Vend -> v
6684 | Vopen ("llppconfig", _, closed) ->
6685 if closed
6686 then v
6687 else { v with f = llppconfig }
6688 | Vopen _ ->
6689 error "unexpected subelement at top level" s spos
6690 | Vclose _ -> error "unexpected close at top level" s spos
6692 and llppconfig v t spos _ =
6693 match t with
6694 | Vdata | Vcdata -> v
6695 | Vend -> error "unexpected end of input in llppconfig" s spos
6696 | Vopen ("defaults", attrs, closed) ->
6697 let c = config_of dc attrs in
6698 setconf dc c;
6699 if closed
6700 then v
6701 else { v with f = defaults }
6703 | Vopen ("ui-font", attrs, closed) ->
6704 let rec getsize size = function
6705 | [] -> size
6706 | ("size", v) :: rest ->
6707 let size =
6708 fromstring int_of_string spos "size" v fstate.fontsize in
6709 getsize size rest
6710 | l -> getsize size l
6712 fstate.fontsize <- getsize fstate.fontsize attrs;
6713 if closed
6714 then v
6715 else { v with f = uifont (Buffer.create 10) }
6717 | Vopen ("doc", attrs, closed) ->
6718 let pathent, spage, srely, span, svisy = doc_of attrs in
6719 let path = unent pathent
6720 and pageno = fromstring int_of_string spos "page" spage 0
6721 and rely = fromstring float_of_string spos "rely" srely 0.0
6722 and pan = fromstring int_of_string spos "pan" span 0
6723 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6724 let c = config_of dc attrs in
6725 let anchor = (pageno, rely, visy) in
6726 if closed
6727 then (Hashtbl.add h path (c, [], pan, anchor); v)
6728 else { v with f = doc path pan anchor c [] }
6730 | Vopen _ ->
6731 error "unexpected subelement in llppconfig" s spos
6733 | Vclose "llppconfig" -> { v with f = toplevel }
6734 | Vclose _ -> error "unexpected close in llppconfig" s spos
6736 and defaults v t spos _ =
6737 match t with
6738 | Vdata | Vcdata -> v
6739 | Vend -> error "unexpected end of input in defaults" s spos
6740 | Vopen ("keymap", attrs, closed) ->
6741 let modename =
6742 try List.assoc "mode" attrs
6743 with Not_found -> "global" in
6744 if closed
6745 then v
6746 else
6747 let ret keymap =
6748 let h = findkeyhash dc modename in
6749 KeyMap.iter (Hashtbl.replace h) keymap;
6750 defaults
6752 { v with f = pkeymap ret KeyMap.empty }
6754 | Vopen (_, _, _) ->
6755 error "unexpected subelement in defaults" s spos
6757 | Vclose "defaults" ->
6758 { v with f = llppconfig }
6760 | Vclose _ -> error "unexpected close in defaults" s spos
6762 and uifont b v t spos epos =
6763 match t with
6764 | Vdata | Vcdata ->
6765 Buffer.add_substring b s spos (epos - spos);
6767 | Vopen (_, _, _) ->
6768 error "unexpected subelement in ui-font" s spos
6769 | Vclose "ui-font" ->
6770 if String.length !fontpath = 0
6771 then fontpath := Buffer.contents b;
6772 { v with f = llppconfig }
6773 | Vclose _ -> error "unexpected close in ui-font" s spos
6774 | Vend -> error "unexpected end of input in ui-font" s spos
6776 and doc path pan anchor c bookmarks v t spos _ =
6777 match t with
6778 | Vdata | Vcdata -> v
6779 | Vend -> error "unexpected end of input in doc" s spos
6780 | Vopen ("bookmarks", _, closed) ->
6781 if closed
6782 then v
6783 else { v with f = pbookmarks path pan anchor c bookmarks }
6785 | Vopen ("keymap", attrs, closed) ->
6786 let modename =
6787 try List.assoc "mode" attrs
6788 with Not_found -> "global"
6790 if closed
6791 then v
6792 else
6793 let ret keymap =
6794 let h = findkeyhash c modename in
6795 KeyMap.iter (Hashtbl.replace h) keymap;
6796 doc path pan anchor c bookmarks
6798 { v with f = pkeymap ret KeyMap.empty }
6800 | Vopen (_, _, _) ->
6801 error "unexpected subelement in doc" s spos
6803 | Vclose "doc" ->
6804 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6805 { v with f = llppconfig }
6807 | Vclose _ -> error "unexpected close in doc" s spos
6809 and pkeymap ret keymap v t spos _ =
6810 match t with
6811 | Vdata | Vcdata -> v
6812 | Vend -> error "unexpected end of input in keymap" s spos
6813 | Vopen ("map", attrs, closed) ->
6814 let r, l = map_of attrs in
6815 let kss = fromstring keys_of_string spos "in" r [] in
6816 let lss = fromstring keys_of_string spos "out" l [] in
6817 let keymap =
6818 match kss with
6819 | [] -> keymap
6820 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6821 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6823 if closed
6824 then { v with f = pkeymap ret keymap }
6825 else
6826 let f () = v in
6827 { v with f = skip "map" f }
6829 | Vopen _ ->
6830 error "unexpected subelement in keymap" s spos
6832 | Vclose "keymap" ->
6833 { v with f = ret keymap }
6835 | Vclose _ -> error "unexpected close in keymap" s spos
6837 and pbookmarks path pan anchor c bookmarks v t spos _ =
6838 match t with
6839 | Vdata | Vcdata -> v
6840 | Vend -> error "unexpected end of input in bookmarks" s spos
6841 | Vopen ("item", attrs, closed) ->
6842 let titleent, spage, srely, svisy = bookmark_of attrs in
6843 let page = fromstring int_of_string spos "page" spage 0
6844 and rely = fromstring float_of_string spos "rely" srely 0.0
6845 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6846 let bookmarks =
6847 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6849 if closed
6850 then { v with f = pbookmarks path pan anchor c bookmarks }
6851 else
6852 let f () = v in
6853 { v with f = skip "item" f }
6855 | Vopen _ ->
6856 error "unexpected subelement in bookmarks" s spos
6858 | Vclose "bookmarks" ->
6859 { v with f = doc path pan anchor c bookmarks }
6861 | Vclose _ -> error "unexpected close in bookmarks" s spos
6863 and skip tag f v t spos _ =
6864 match t with
6865 | Vdata | Vcdata -> v
6866 | Vend ->
6867 error ("unexpected end of input in skipped " ^ tag) s spos
6868 | Vopen (tag', _, closed) ->
6869 if closed
6870 then v
6871 else
6872 let f' () = { v with f = skip tag f } in
6873 { v with f = skip tag' f' }
6874 | Vclose ctag ->
6875 if tag = ctag
6876 then f ()
6877 else error ("unexpected close in skipped " ^ tag) s spos
6880 parse { f = toplevel; accu = () } s;
6881 h, dc;
6884 let do_load f ic =
6886 let len = in_channel_length ic in
6887 let s = String.create len in
6888 really_input ic s 0 len;
6889 f s;
6890 with
6891 | Parse_error (msg, s, pos) ->
6892 let subs = subs s pos in
6893 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6894 failwith ("parse error: " ^ s)
6896 | exn ->
6897 failwith ("config load error: " ^ exntos exn)
6900 let defconfpath =
6901 let dir =
6903 let dir = Filename.concat home ".config" in
6904 if Sys.is_directory dir then dir else home
6905 with _ -> home
6907 Filename.concat dir "llpp.conf"
6910 let confpath = ref defconfpath;;
6912 let load1 f =
6913 if Sys.file_exists !confpath
6914 then
6915 match
6916 (try Some (open_in_bin !confpath)
6917 with exn ->
6918 prerr_endline
6919 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6920 exntos exn);
6921 None
6923 with
6924 | Some ic ->
6925 let success =
6927 f (do_load get ic)
6928 with exn ->
6929 prerr_endline
6930 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6931 exntos exn);
6932 false
6934 close_in ic;
6935 success
6937 | None -> false
6938 else
6939 f (Hashtbl.create 0, defconf)
6942 let load () =
6943 let f (h, dc) =
6944 let pc, pb, px, pa =
6946 let key =
6947 if String.length state.origin = 0
6948 then state.path
6949 else state.origin
6951 Hashtbl.find h (Filename.basename key)
6952 with Not_found -> dc, [], 0, emptyanchor
6954 setconf defconf dc;
6955 setconf conf pc;
6956 state.bookmarks <- pb;
6957 state.x <- px;
6958 if conf.jumpback
6959 then state.anchor <- pa;
6960 cbput state.hists.nav pa;
6961 true
6963 load1 f
6966 let add_attrs bb always dc c =
6967 let ob s a b =
6968 if always || a != b
6969 then Printf.bprintf bb "\n %s='%b'" s a
6970 and op s a b =
6971 if always || a <> b
6972 then Printf.bprintf bb "\n %s='%b'" s (a != None)
6973 and oi s a b =
6974 if always || a != b
6975 then Printf.bprintf bb "\n %s='%d'" s a
6976 and oI s a b =
6977 if always || a != b
6978 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6979 and oz s a b =
6980 if always || a <> b
6981 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6982 and oF s a b =
6983 if always || a <> b
6984 then Printf.bprintf bb "\n %s='%f'" s a
6985 and oc s a b =
6986 if always || a <> b
6987 then
6988 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6989 and oC s a b =
6990 if always || a <> b
6991 then
6992 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
6993 and oR s a b =
6994 if always || a <> b
6995 then
6996 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6997 and os s a b =
6998 if always || a <> b
6999 then
7000 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7001 and og s a b =
7002 if always || a <> b
7003 then
7004 match a with
7005 | None -> ()
7006 | Some (_N, _A, _B) ->
7007 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7008 and oW s a b =
7009 if always || a <> b
7010 then
7011 let v =
7012 match a with
7013 | None -> "false"
7014 | Some f ->
7015 if f = infinity
7016 then "true"
7017 else string_of_float f
7019 Printf.bprintf bb "\n %s='%s'" s v
7020 and oco s a b =
7021 if always || a <> b
7022 then
7023 match a with
7024 | Cmulti ((n, a, b), _) when n > 1 ->
7025 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7026 | Csplit (n, _) when n > 1 ->
7027 Printf.bprintf bb "\n %s='%d'" s ~-n
7028 | _ -> ()
7029 and obeco s a b =
7030 if always || a <> b
7031 then
7032 match a with
7033 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7034 | _ -> ()
7035 and oFm s a b =
7036 if always || a <> b
7037 then
7038 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7039 and oSv s a b m =
7040 if always || a <> b
7041 then
7042 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7044 oi "width" c.cwinw dc.cwinw;
7045 oi "height" c.cwinh dc.cwinh;
7046 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7047 oi "scroll-handle-height" c.scrollh dc.scrollh;
7048 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7049 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7050 ob "case-insensitive-search" c.icase dc.icase;
7051 ob "preload" c.preload dc.preload;
7052 oi "page-bias" c.pagebias dc.pagebias;
7053 oi "scroll-step" c.scrollstep dc.scrollstep;
7054 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7055 ob "max-height-fit" c.maxhfit dc.maxhfit;
7056 ob "crop-hack" c.crophack dc.crophack;
7057 oW "throttle" c.maxwait dc.maxwait;
7058 ob "highlight-links" c.hlinks dc.hlinks;
7059 ob "under-cursor-info" c.underinfo dc.underinfo;
7060 oi "vertical-margin" c.interpagespace dc.interpagespace;
7061 oz "zoom" c.zoom dc.zoom;
7062 ob "presentation" c.presentation dc.presentation;
7063 oi "rotation-angle" c.angle dc.angle;
7064 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7065 oFm "fit-model" c.fitmodel dc.fitmodel;
7066 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7067 oi "tex-count" c.texcount dc.texcount;
7068 oi "slice-height" c.sliceheight dc.sliceheight;
7069 oi "thumbnail-width" c.thumbw dc.thumbw;
7070 ob "persistent-location" c.jumpback dc.jumpback;
7071 oc "background-color" c.bgcolor dc.bgcolor;
7072 oi "tile-width" c.tilew dc.tilew;
7073 oi "tile-height" c.tileh dc.tileh;
7074 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7075 ob "checkers" c.checkers dc.checkers;
7076 oi "aalevel" c.aalevel dc.aalevel;
7077 ob "trim-margins" c.trimmargins dc.trimmargins;
7078 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7079 os "uri-launcher" c.urilauncher dc.urilauncher;
7080 os "path-launcher" c.pathlauncher dc.pathlauncher;
7081 oC "color-space" c.colorspace dc.colorspace;
7082 ob "invert-colors" c.invert dc.invert;
7083 oF "brightness" c.colorscale dc.colorscale;
7084 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7085 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7086 oco "columns" c.columns dc.columns;
7087 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7088 os "selection-command" c.selcmd dc.selcmd;
7089 os "synctex-command" c.stcmd dc.stcmd;
7090 os "pax-command" c.paxcmd dc.paxcmd;
7091 ob "update-cursor" c.updatecurs dc.updatecurs;
7092 oi "hint-font-size" c.hfsize dc.hfsize;
7093 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7094 oF "page-scroll-scale" c.pgscale dc.pgscale;
7095 ob "use-pbo" c.usepbo dc.usepbo;
7096 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7097 ob "remote-in-a-new-instance" c.riani dc.riani;
7098 op "point-and-x" c.pax dc.pax;
7101 let keymapsbuf always dc c =
7102 let bb = Buffer.create 16 in
7103 let rec loop = function
7104 | [] -> ()
7105 | (modename, h) :: rest ->
7106 let dh = findkeyhash dc modename in
7107 if always || h <> dh
7108 then (
7109 if Hashtbl.length h > 0
7110 then (
7111 if Buffer.length bb > 0
7112 then Buffer.add_char bb '\n';
7113 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7114 Hashtbl.iter (fun i o ->
7115 let isdifferent = always ||
7117 let dO = Hashtbl.find dh i in
7118 dO <> o
7119 with Not_found -> true
7121 if isdifferent
7122 then
7123 let addkm (k, m) =
7124 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7125 if Wsi.withalt m then Buffer.add_string bb "alt-";
7126 if Wsi.withshift m then Buffer.add_string bb "shift-";
7127 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7128 Buffer.add_string bb (Wsi.keyname k);
7130 let addkms l =
7131 let rec loop = function
7132 | [] -> ()
7133 | km :: [] -> addkm km
7134 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7136 loop l
7138 Buffer.add_string bb "<map in='";
7139 addkm i;
7140 match o with
7141 | KMinsrt km ->
7142 Buffer.add_string bb "' out='";
7143 addkm km;
7144 Buffer.add_string bb "'/>\n"
7146 | KMinsrl kms ->
7147 Buffer.add_string bb "' out='";
7148 addkms kms;
7149 Buffer.add_string bb "'/>\n"
7151 | KMmulti (ins, kms) ->
7152 Buffer.add_char bb ' ';
7153 addkms ins;
7154 Buffer.add_string bb "' out='";
7155 addkms kms;
7156 Buffer.add_string bb "'/>\n"
7157 ) h;
7158 Buffer.add_string bb "</keymap>";
7161 loop rest
7163 loop c.keyhashes;
7167 let save () =
7168 let uifontsize = fstate.fontsize in
7169 let bb = Buffer.create 32768 in
7170 let relx = float state.x /. float state.winw in
7171 let w, h, x =
7172 let cx w = truncate (relx *. float w) in
7173 List.fold_left
7174 (fun (w, h, x) ws ->
7175 match ws with
7176 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7177 | Wsi.MaxVert -> (w, conf.cwinh, x)
7178 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7180 (state.winw, state.winh, state.x) state.winstate
7182 conf.cwinw <- w;
7183 conf.cwinh <- h;
7184 let f (h, dc) =
7185 let dc = if conf.bedefault then conf else dc in
7186 Buffer.add_string bb "<llppconfig>\n";
7188 if String.length !fontpath > 0
7189 then
7190 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7191 uifontsize
7192 !fontpath
7193 else (
7194 if uifontsize <> 14
7195 then
7196 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7199 Buffer.add_string bb "<defaults ";
7200 add_attrs bb true dc dc;
7201 let kb = keymapsbuf true dc dc in
7202 if Buffer.length kb > 0
7203 then (
7204 Buffer.add_string bb ">\n";
7205 Buffer.add_buffer bb kb;
7206 Buffer.add_string bb "\n</defaults>\n";
7208 else Buffer.add_string bb "/>\n";
7210 let adddoc path pan anchor c bookmarks =
7211 if bookmarks == [] && c = dc && anchor = emptyanchor
7212 then ()
7213 else (
7214 Printf.bprintf bb "<doc path='%s'"
7215 (enent path 0 (String.length path));
7217 if anchor <> emptyanchor
7218 then (
7219 let n, rely, visy = anchor in
7220 Printf.bprintf bb " page='%d'" n;
7221 if rely > 1e-6
7222 then
7223 Printf.bprintf bb " rely='%f'" rely
7225 if abs_float visy > 1e-6
7226 then
7227 Printf.bprintf bb " visy='%f'" visy
7231 if pan != 0
7232 then Printf.bprintf bb " pan='%d'" pan;
7234 add_attrs bb false dc c;
7235 let kb = keymapsbuf false dc c in
7237 begin match bookmarks with
7238 | [] ->
7239 if Buffer.length kb > 0
7240 then (
7241 Buffer.add_string bb ">\n";
7242 Buffer.add_buffer bb kb;
7243 Buffer.add_string bb "\n</doc>\n";
7245 else Buffer.add_string bb "/>\n"
7246 | _ ->
7247 Buffer.add_string bb ">\n<bookmarks>\n";
7248 List.iter (fun (title, _level, (page, rely, visy)) ->
7249 Printf.bprintf bb
7250 "<item title='%s' page='%d'"
7251 (enent title 0 (String.length title))
7252 page
7254 if rely > 1e-6
7255 then
7256 Printf.bprintf bb " rely='%f'" rely
7258 if abs_float visy > 1e-6
7259 then
7260 Printf.bprintf bb " visy='%f'" visy
7262 Buffer.add_string bb "/>\n";
7263 ) bookmarks;
7264 Buffer.add_string bb "</bookmarks>";
7265 if Buffer.length kb > 0
7266 then (
7267 Buffer.add_string bb "\n";
7268 Buffer.add_buffer bb kb;
7270 Buffer.add_string bb "\n</doc>\n";
7271 end;
7275 let pan, conf =
7276 match state.mode with
7277 | Birdseye (c, pan, _, _, _) ->
7278 let beyecolumns =
7279 match conf.columns with
7280 | Cmulti ((c, _, _), _) -> Some c
7281 | Csingle _ -> None
7282 | Csplit _ -> None
7283 and columns =
7284 match c.columns with
7285 | Cmulti (c, _) -> Cmulti (c, [||])
7286 | Csingle _ -> Csingle [||]
7287 | Csplit _ -> failwith "quit from bird's eye while split"
7289 pan, { c with beyecolumns = beyecolumns; columns = columns }
7290 | _ -> x, conf
7292 let basename = Filename.basename
7293 (if String.length state.origin = 0 then state.path else state.origin)
7295 adddoc basename pan (getanchor ())
7296 (let conf =
7297 let autoscrollstep =
7298 match state.autoscroll with
7299 | Some step -> step
7300 | None -> conf.autoscrollstep
7302 match state.mode with
7303 | Birdseye (bc, _, _, _, _) ->
7304 { conf with
7305 zoom = bc.zoom;
7306 presentation = bc.presentation;
7307 interpagespace = bc.interpagespace;
7308 maxwait = bc.maxwait;
7309 autoscrollstep = autoscrollstep }
7310 | _ -> { conf with autoscrollstep = autoscrollstep }
7311 in conf)
7312 (if conf.savebmarks then state.bookmarks else []);
7314 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7315 if basename <> path
7316 then adddoc path x anchor c bookmarks
7317 ) h;
7318 Buffer.add_string bb "</llppconfig>\n";
7319 true;
7321 if load1 f && Buffer.length bb > 0
7322 then
7324 let tmp = !confpath ^ ".tmp" in
7325 let oc = open_out_bin tmp in
7326 Buffer.output_buffer oc bb;
7327 close_out oc;
7328 Unix.rename tmp !confpath;
7329 with exn ->
7330 prerr_endline
7331 ("error while saving configuration: " ^ exntos exn)
7333 end;;
7335 let adderrmsg src msg =
7336 Buffer.add_string state.errmsgs msg;
7337 state.newerrmsgs <- true;
7338 G.postRedisplay src
7341 let adderrfmt src fmt =
7342 Format.kprintf (fun s -> adderrmsg src s) fmt;
7345 let ract cmds =
7346 let cl = splitatspace cmds in
7347 let scan s fmt f =
7348 try Scanf.sscanf s fmt f
7349 with exn ->
7350 adderrfmt "remote exec"
7351 "error processing '%S': %s\n" cmds (exntos exn)
7353 match cl with
7354 | "reload" :: [] -> reload ()
7355 | "goto" :: args :: [] ->
7356 scan args "%u %f %f"
7357 (fun pageno x y ->
7358 let cmd, _ = state.geomcmds in
7359 if String.length cmd = 0
7360 then gotopagexy pageno x y
7361 else
7362 let f prevf () =
7363 gotopagexy pageno x y;
7364 prevf ()
7366 state.reprf <- f state.reprf
7368 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7369 | "rect" :: args :: [] ->
7370 scan args "%u %u %f %f %f %f"
7371 (fun pageno color x0 y0 x1 y1 ->
7372 onpagerect pageno (fun w h ->
7373 let _,w1,h1,_ = getpagedim pageno in
7374 let sw = float w1 /. w
7375 and sh = float h1 /. h in
7376 let x0s = x0 *. sw
7377 and x1s = x1 *. sw
7378 and y0s = y0 *. sh
7379 and y1s = y1 *. sh in
7380 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7381 debugrect rect;
7382 state.rects <- (pageno, color, rect) :: state.rects;
7383 G.postRedisplay "rect";
7386 | "activatewin" :: [] -> Wsi.activatewin ()
7387 | "quit" :: [] -> raise Quit
7388 | _ ->
7389 adderrfmt "remote command"
7390 "error processing remote command: %S\n" cmds;
7393 let remote =
7394 let scratch = String.create 80 in
7395 let buf = Buffer.create 80 in
7396 fun fd ->
7397 let rec tempfr () =
7398 try Some (Unix.read fd scratch 0 80)
7399 with
7400 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7401 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7402 | exn -> raise exn
7404 match tempfr () with
7405 | None -> Some fd
7406 | Some n ->
7407 if n = 0
7408 then (
7409 Unix.close fd;
7410 if Buffer.length buf > 0
7411 then (
7412 let s = Buffer.contents buf in
7413 Buffer.clear buf;
7414 ract s;
7416 None
7418 else
7419 let rec eat ppos =
7420 let nlpos =
7422 let pos = String.index_from scratch ppos '\n' in
7423 if pos >= n then -1 else pos
7424 with Not_found -> -1
7426 if nlpos >= 0
7427 then (
7428 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7429 let s = Buffer.contents buf in
7430 Buffer.clear buf;
7431 ract s;
7432 eat (nlpos+1);
7434 else (
7435 Buffer.add_substring buf scratch ppos (n-ppos);
7436 Some fd
7438 in eat 0
7441 let remoteopen path =
7442 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7443 with exn ->
7444 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7445 None
7448 let () =
7449 let trimcachepath = ref "" in
7450 let rcmdpath = ref "" in
7451 selfexec := Sys.executable_name;
7452 Arg.parse
7453 (Arg.align
7454 [("-p", Arg.String (fun s -> state.password <- s),
7455 "<password> Set password");
7457 ("-f", Arg.String
7458 (fun s ->
7459 Config.fontpath := s;
7460 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7462 "<path> Set path to the user interface font");
7464 ("-c", Arg.String
7465 (fun s ->
7466 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7467 Config.confpath := s),
7468 "<path> Set path to the configuration file");
7470 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7471 "<path> Set path to the trim cache file");
7473 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7474 "<named-destination> Set named destination");
7476 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7478 ("-remote", Arg.String (fun s -> rcmdpath := s),
7479 "<path> Set path to the remote commands source");
7481 ("-origin", Arg.String (fun s -> state.origin <- s),
7482 "<original-path> Set original path");
7484 ("-v", Arg.Unit (fun () ->
7485 Printf.printf
7486 "%s\nconfiguration path: %s\n"
7487 (version ())
7488 Config.defconfpath
7490 exit 0), " Print version and exit");
7493 (fun s -> state.path <- s)
7494 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7496 if !wtmode
7497 then selfexec := !selfexec ^ " -wtmode";
7499 if String.length state.path = 0
7500 then (prerr_endline "file name missing"; exit 1);
7502 if not (Config.load ())
7503 then prerr_endline "failed to load configuration";
7505 let wsfd, winw, winh = Wsi.init (object
7506 val mutable m_hack = false
7507 method expose = if not m_hack then G.postRedisplay "expose"
7508 method visible = G.postRedisplay "visible"
7509 method display = m_hack <- false; display ()
7510 method reshape w h =
7511 m_hack <- w < state.winw && h < state.winh;
7512 reshape w h
7513 method mouse b d x y m = mouse b d x y m
7514 method motion x y = state.mpos <- (x, y); motion x y
7515 method pmotion x y = state.mpos <- (x, y); pmotion x y
7516 method key k m =
7517 let mascm = m land (
7518 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7519 ) in
7520 match state.keystate with
7521 | KSnone ->
7522 let km = k, mascm in
7523 begin
7524 match
7525 let modehash = state.uioh#modehash in
7526 try Hashtbl.find modehash km
7527 with Not_found ->
7528 try Hashtbl.find (findkeyhash conf "global") km
7529 with Not_found -> KMinsrt (k, m)
7530 with
7531 | KMinsrt (k, m) -> keyboard k m
7532 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7533 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7535 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7536 List.iter (fun (k, m) -> keyboard k m) insrt;
7537 state.keystate <- KSnone
7538 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7539 state.keystate <- KSinto (keys, insrt)
7540 | _ ->
7541 state.keystate <- KSnone
7543 method enter x y = state.mpos <- (x, y); pmotion x y
7544 method leave = state.mpos <- (-1, -1)
7545 method winstate wsl = state.winstate <- wsl
7546 method quit = raise Quit
7547 end) conf.cwinw conf.cwinh (platform = Posx) in
7549 state.wsfd <- wsfd;
7551 if not (
7552 List.exists GlMisc.check_extension
7553 [ "GL_ARB_texture_rectangle"
7554 ; "GL_EXT_texture_recangle"
7555 ; "GL_NV_texture_rectangle" ]
7557 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7559 if (
7560 let r = GlMisc.get_string `renderer in
7561 let p = "Mesa DRI Intel(" in
7562 let l = String.length p in
7563 String.length r > l && String.sub r 0 l = p
7565 then defconf.sliceheight <- 1024;
7567 let cr, sw =
7568 match Ne.pipe () with
7569 | Ne.Exn exn ->
7570 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7571 exit 1
7572 | Ne.Res rw -> rw
7573 and sr, cw =
7574 match Ne.pipe () with
7575 | Ne.Exn exn ->
7576 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7577 exit 1
7578 | Ne.Res rw -> rw
7581 cloexec cr;
7582 cloexec sw;
7583 cloexec sr;
7584 cloexec cw;
7586 setcheckers conf.checkers;
7587 redirectstderr ();
7589 init (cr, cw) (
7590 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7591 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7592 !Config.fontpath, !trimcachepath,
7593 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7595 state.sr <- sr;
7596 state.sw <- sw;
7597 state.text <- "Opening " ^ (mbtoutf8 state.path);
7598 reshape winw winh;
7599 opendoc state.path state.password;
7600 state.uioh <- uioh;
7601 display ();
7602 Wsi.mapwin ();
7603 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7604 let optrfd =
7605 ref (
7606 if String.length !rcmdpath > 0
7607 then remoteopen !rcmdpath
7608 else None
7612 let rec loop deadline =
7613 let r =
7614 match state.errfd with
7615 | None -> [state.sr; state.wsfd]
7616 | Some fd -> [state.sr; state.wsfd; fd]
7618 let r =
7619 match !optrfd with
7620 | None -> r
7621 | Some fd -> fd :: r
7623 if state.redisplay
7624 then (
7625 state.redisplay <- false;
7626 display ();
7628 let timeout =
7629 let now = now () in
7630 if deadline > now
7631 then (
7632 if deadline = infinity
7633 then ~-.1.0
7634 else max 0.0 (deadline -. now)
7636 else 0.0
7638 let r, _, _ =
7639 try Unix.select r [] [] timeout
7640 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7642 begin match r with
7643 | [] ->
7644 state.ghyll None;
7645 let newdeadline =
7646 if state.ghyll == noghyll
7647 then
7648 match state.autoscroll with
7649 | Some step when step != 0 ->
7650 let y = state.y + step in
7651 let y =
7652 if y < 0
7653 then state.maxy
7654 else if y >= state.maxy then 0 else y
7656 gotoy y;
7657 if state.mode = View
7658 then state.text <- "";
7659 deadline +. 0.01
7660 | _ -> infinity
7661 else deadline +. 0.01
7663 loop newdeadline
7665 | l ->
7666 let rec checkfds = function
7667 | [] -> ()
7668 | fd :: rest when fd = state.sr ->
7669 let cmd = readcmd state.sr in
7670 act cmd;
7671 checkfds rest
7673 | fd :: rest when fd = state.wsfd ->
7674 Wsi.readresp fd;
7675 checkfds rest
7677 | fd :: rest when Some fd = !optrfd ->
7678 begin match remote fd with
7679 | None -> optrfd := remoteopen !rcmdpath;
7680 | opt -> optrfd := opt
7681 end;
7682 checkfds rest
7684 | fd :: rest ->
7685 let s = String.create 80 in
7686 let n = tempfailureretry (Unix.read fd s 0) 80 in
7687 if conf.redirectstderr
7688 then (
7689 Buffer.add_substring state.errmsgs s 0 n;
7690 state.newerrmsgs <- true;
7691 state.redisplay <- true;
7693 else (
7694 prerr_string (String.sub s 0 n);
7695 flush stderr;
7697 checkfds rest
7699 checkfds l;
7700 let newdeadline =
7701 let deadline1 =
7702 if deadline = infinity
7703 then now () +. 0.01
7704 else deadline
7706 match state.autoscroll with
7707 | Some step when step != 0 -> deadline1
7708 | _ -> if state.ghyll == noghyll then infinity else deadline1
7710 loop newdeadline
7711 end;
7714 loop infinity;
7715 with Quit ->
7716 Config.save ();