Damnation worthy refactoring
[llpp.git] / main.ml
blob4699f5709d57a9e407ccf39f7056ee19b03e831d
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 = [|"word"; "line"; "block"; "page"|];;
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 roammark name get set =
4375 m_l <-
4376 (name, `string get, 1, Action (
4377 fun _ ->
4378 let source =
4379 let vals = [| "page"; "block"; "line"; "word" |] in
4380 (object
4381 inherit lvsourcebase
4383 initializer
4384 m_active <- MTE.to_int conf.paxmark;
4385 m_first <- 0;
4387 method getitemcount = Array.length vals
4388 method getitem n = (vals.(n), 0)
4389 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4390 ignore (uioh, first, pan, qsearch);
4391 if not cancel then set active;
4392 None
4393 method hasaction _ = true
4394 end)
4396 state.text <- "";
4397 let modehash = findkeyhash conf "info" in
4398 coe (new listview ~source ~trusted:true ~modehash)
4399 )) :: m_l
4401 method fitmodel name get set =
4402 m_l <-
4403 (name, `string get, 1, Action (
4404 fun _ ->
4405 let source =
4406 let vals = [| "fit width"; "proportional"; "fit page" |] in
4407 (object
4408 inherit lvsourcebase
4410 initializer
4411 m_active <- FMTE.to_int conf.fitmodel;
4412 m_first <- 0;
4414 method getitemcount = Array.length vals
4415 method getitem n = (vals.(n), 0)
4416 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4417 ignore (uioh, first, pan, qsearch);
4418 if not cancel then set active;
4419 None
4420 method hasaction _ = true
4421 end)
4423 state.text <- "";
4424 let modehash = findkeyhash conf "info" in
4425 coe (new listview ~source ~trusted:true ~modehash)
4426 )) :: m_l
4428 method caption s offset =
4429 m_l <- (s, `empty, offset, Noaction) :: m_l
4431 method caption2 s f offset =
4432 m_l <- (s, `string f, offset, Noaction) :: m_l
4434 method getitemcount = Array.length m_a
4436 method getitem n =
4437 let tostr = function
4438 | `int f -> string_of_int (f ())
4439 | `intws f -> string_with_suffix_of_int (f ())
4440 | `string f -> f ()
4441 | `color f -> color_to_string (f ())
4442 | `bool (btos, f) -> btos (f ())
4443 | `empty -> ""
4445 let name, t, offset, _ = m_a.(n) in
4446 ((let s = tostr t in
4447 if String.length s > 0
4448 then Printf.sprintf "%s\t%s" name s
4449 else name),
4450 offset)
4452 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4453 let uiohopt =
4454 if not cancel
4455 then (
4456 m_qsearch <- qsearch;
4457 let uioh =
4458 match m_a.(active) with
4459 | _, _, _, Action f -> f uioh
4460 | _ -> uioh
4462 Some uioh
4464 else None
4466 m_active <- active;
4467 m_first <- first;
4468 m_pan <- pan;
4469 uiohopt
4471 method hasaction n =
4472 match m_a.(n) with
4473 | _, _, _, Action _ -> true
4474 | _ -> false
4475 end)
4477 let rec fillsrc prevmode prevuioh =
4478 let sep () = src#caption "" 0 in
4479 let colorp name get set =
4480 src#string name
4481 (fun () -> color_to_string (get ()))
4482 (fun v ->
4484 let c = color_of_string v in
4485 set c
4486 with exn ->
4487 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4490 let oldmode = state.mode in
4491 let birdseye = isbirdseye state.mode in
4493 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4495 src#bool "presentation mode"
4496 (fun () -> conf.presentation)
4497 (fun v -> setpresentationmode v);
4499 src#bool "ignore case in searches"
4500 (fun () -> conf.icase)
4501 (fun v -> conf.icase <- v);
4503 src#bool "preload"
4504 (fun () -> conf.preload)
4505 (fun v -> conf.preload <- v);
4507 src#bool "highlight links"
4508 (fun () -> conf.hlinks)
4509 (fun v -> conf.hlinks <- v);
4511 src#bool "under info"
4512 (fun () -> conf.underinfo)
4513 (fun v -> conf.underinfo <- v);
4515 src#bool "persistent bookmarks"
4516 (fun () -> conf.savebmarks)
4517 (fun v -> conf.savebmarks <- v);
4519 src#fitmodel "fit model"
4520 (fun () -> FMTE.to_string conf.fitmodel)
4521 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4523 src#bool "trim margins"
4524 (fun () -> conf.trimmargins)
4525 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4527 src#bool "persistent location"
4528 (fun () -> conf.jumpback)
4529 (fun v -> conf.jumpback <- v);
4531 sep ();
4532 src#int "inter-page space"
4533 (fun () -> conf.interpagespace)
4534 (fun n ->
4535 conf.interpagespace <- n;
4536 docolumns conf.columns;
4537 let pageno, py =
4538 match state.layout with
4539 | [] -> 0, 0
4540 | l :: _ ->
4541 l.pageno, l.pagey
4543 state.maxy <- calcheight ();
4544 let y = getpagey pageno in
4545 gotoy (y + py)
4548 src#int "page bias"
4549 (fun () -> conf.pagebias)
4550 (fun v -> conf.pagebias <- v);
4552 src#int "scroll step"
4553 (fun () -> conf.scrollstep)
4554 (fun n -> conf.scrollstep <- n);
4556 src#int "horizontal scroll step"
4557 (fun () -> conf.hscrollstep)
4558 (fun v -> conf.hscrollstep <- v);
4560 src#int "auto scroll step"
4561 (fun () ->
4562 match state.autoscroll with
4563 | Some step -> step
4564 | _ -> conf.autoscrollstep)
4565 (fun n ->
4566 if state.autoscroll <> None
4567 then state.autoscroll <- Some n;
4568 conf.autoscrollstep <- n);
4570 src#int "zoom"
4571 (fun () -> truncate (conf.zoom *. 100.))
4572 (fun v -> setzoom ((float v) /. 100.));
4574 src#int "rotation"
4575 (fun () -> conf.angle)
4576 (fun v -> reqlayout v conf.fitmodel);
4578 src#int "scroll bar width"
4579 (fun () -> conf.scrollbw)
4580 (fun v ->
4581 conf.scrollbw <- v;
4582 reshape state.winw state.winh;
4585 src#int "scroll handle height"
4586 (fun () -> conf.scrollh)
4587 (fun v -> conf.scrollh <- v;);
4589 src#int "thumbnail width"
4590 (fun () -> conf.thumbw)
4591 (fun v ->
4592 conf.thumbw <- min 4096 v;
4593 match oldmode with
4594 | Birdseye beye ->
4595 leavebirdseye beye false;
4596 enterbirdseye ()
4597 | _ -> ()
4600 let mode = state.mode in
4601 src#string "columns"
4602 (fun () ->
4603 match conf.columns with
4604 | Csingle _ -> "1"
4605 | Cmulti (multi, _) -> multicolumns_to_string multi
4606 | Csplit (count, _) -> "-" ^ string_of_int count
4608 (fun v ->
4609 let n, a, b = multicolumns_of_string v in
4610 setcolumns mode n a b);
4612 sep ();
4613 src#caption "Pixmap cache" 0;
4614 src#int_with_suffix "size (advisory)"
4615 (fun () -> conf.memlimit)
4616 (fun v -> conf.memlimit <- v);
4618 src#caption2 "used"
4619 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4620 (string_with_suffix_of_int state.memused)
4621 (Hashtbl.length state.tilemap)) 1;
4623 sep ();
4624 src#caption "Layout" 0;
4625 src#caption2 "Dimension"
4626 (fun () ->
4627 Printf.sprintf "%dx%d (virtual %dx%d)"
4628 state.winw state.winh
4629 state.w state.maxy)
4631 if conf.debug
4632 then
4633 src#caption2 "Position" (fun () ->
4634 Printf.sprintf "%dx%d" state.x state.y
4636 else
4637 src#caption2 "Position" (fun () -> describe_location ()) 1
4640 sep ();
4641 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4642 "Save these parameters as global defaults at exit"
4643 (fun () -> conf.bedefault)
4644 (fun v -> conf.bedefault <- v)
4647 sep ();
4648 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4649 src#bool ~offset:0 ~btos "Extended parameters"
4650 (fun () -> !showextended)
4651 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4652 if !showextended
4653 then (
4654 src#bool "checkers"
4655 (fun () -> conf.checkers)
4656 (fun v -> conf.checkers <- v; setcheckers v);
4657 src#bool "update cursor"
4658 (fun () -> conf.updatecurs)
4659 (fun v -> conf.updatecurs <- v);
4660 src#bool "verbose"
4661 (fun () -> conf.verbose)
4662 (fun v -> conf.verbose <- v);
4663 src#bool "invert colors"
4664 (fun () -> conf.invert)
4665 (fun v -> conf.invert <- v);
4666 src#bool "max fit"
4667 (fun () -> conf.maxhfit)
4668 (fun v -> conf.maxhfit <- v);
4669 src#bool "redirect stderr"
4670 (fun () -> conf.redirectstderr)
4671 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4672 src#bool "pax mode"
4673 (fun () -> conf.pax != None)
4674 (fun v ->
4675 if v
4676 then conf.pax <- Some (ref (now (), 0, 0))
4677 else conf.pax <- None);
4678 src#string "uri launcher"
4679 (fun () -> conf.urilauncher)
4680 (fun v -> conf.urilauncher <- v);
4681 src#string "path launcher"
4682 (fun () -> conf.pathlauncher)
4683 (fun v -> conf.pathlauncher <- v);
4684 src#string "tile size"
4685 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4686 (fun v ->
4688 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4689 conf.tilew <- max 64 w;
4690 conf.tileh <- max 64 h;
4691 flushtiles ();
4692 with exn ->
4693 state.text <- Printf.sprintf "bad tile size `%s': %s"
4694 v (exntos exn)
4696 src#int "texture count"
4697 (fun () -> conf.texcount)
4698 (fun v ->
4699 if realloctexts v
4700 then conf.texcount <- v
4701 else showtext '!' " Failed to set texture count please retry later"
4703 src#int "slice height"
4704 (fun () -> conf.sliceheight)
4705 (fun v ->
4706 conf.sliceheight <- v;
4707 wcmd "sliceh %d" conf.sliceheight;
4709 src#int "anti-aliasing level"
4710 (fun () -> conf.aalevel)
4711 (fun v ->
4712 conf.aalevel <- bound v 0 8;
4713 state.anchor <- getanchor ();
4714 opendoc state.path state.password;
4716 src#string "page scroll scaling factor"
4717 (fun () -> string_of_float conf.pgscale)
4718 (fun v ->
4720 let s = float_of_string v in
4721 conf.pgscale <- s
4722 with exn ->
4723 state.text <- Printf.sprintf
4724 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4727 src#int "ui font size"
4728 (fun () -> fstate.fontsize)
4729 (fun v -> setfontsize (bound v 5 100));
4730 src#int "hint font size"
4731 (fun () -> conf.hfsize)
4732 (fun v -> conf.hfsize <- bound v 5 100);
4733 colorp "background color"
4734 (fun () -> conf.bgcolor)
4735 (fun v -> conf.bgcolor <- v);
4736 src#bool "crop hack"
4737 (fun () -> conf.crophack)
4738 (fun v -> conf.crophack <- v);
4739 src#string "trim fuzz"
4740 (fun () -> irect_to_string conf.trimfuzz)
4741 (fun v ->
4743 conf.trimfuzz <- irect_of_string v;
4744 if conf.trimmargins
4745 then settrim true conf.trimfuzz;
4746 with exn ->
4747 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4749 src#string "throttle"
4750 (fun () ->
4751 match conf.maxwait with
4752 | None -> "show place holder if page is not ready"
4753 | Some time ->
4754 if time = infinity
4755 then "wait for page to fully render"
4756 else
4757 "wait " ^ string_of_float time
4758 ^ " seconds before showing placeholder"
4760 (fun v ->
4762 let f = float_of_string v in
4763 if f <= 0.0
4764 then conf.maxwait <- None
4765 else conf.maxwait <- Some f
4766 with exn ->
4767 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4769 src#string "ghyll scroll"
4770 (fun () ->
4771 match conf.ghyllscroll with
4772 | None -> ""
4773 | Some nab -> ghyllscroll_to_string nab
4775 (fun v ->
4777 let gs =
4778 if String.length v = 0
4779 then None
4780 else Some (ghyllscroll_of_string v)
4782 conf.ghyllscroll <- gs
4783 with exn ->
4784 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4786 src#string "selection command"
4787 (fun () -> conf.selcmd)
4788 (fun v -> conf.selcmd <- v);
4789 src#string "synctex command"
4790 (fun () -> conf.stcmd)
4791 (fun v -> conf.stcmd <- v);
4792 src#string "pax command"
4793 (fun () -> conf.paxcmd)
4794 (fun v -> conf.paxcmd <- v);
4795 src#colorspace "color space"
4796 (fun () -> CSTE.to_string conf.colorspace)
4797 (fun v ->
4798 conf.colorspace <- CSTE.of_int v;
4799 wcmd "cs %d" v;
4800 load state.layout;
4802 src#roammark "pax mark method"
4803 (fun () -> MTE.to_string conf.paxmark)
4804 (fun v -> conf.paxmark <- MTE.of_int v);
4805 if pbousable ()
4806 then
4807 src#bool "use PBO"
4808 (fun () -> conf.usepbo)
4809 (fun v -> conf.usepbo <- v);
4810 src#bool "mouse wheel scrolls pages"
4811 (fun () -> conf.wheelbypage)
4812 (fun v -> conf.wheelbypage <- v);
4813 src#bool "open remote links in a new instance"
4814 (fun () -> conf.riani)
4815 (fun v -> conf.riani <- v);
4818 sep ();
4819 src#caption "Document" 0;
4820 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4821 src#caption2 "Pages"
4822 (fun () -> string_of_int state.pagecount) 1;
4823 src#caption2 "Dimensions"
4824 (fun () -> string_of_int (List.length state.pdims)) 1;
4825 if conf.trimmargins
4826 then (
4827 sep ();
4828 src#caption "Trimmed margins" 0;
4829 src#caption2 "Dimensions"
4830 (fun () -> string_of_int (List.length state.pdims)) 1;
4833 sep ();
4834 src#caption "OpenGL" 0;
4835 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4836 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4838 sep ();
4839 src#caption "Location" 0;
4840 if String.length state.origin > 0
4841 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4842 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4844 src#reset prevmode prevuioh;
4846 fun () ->
4847 state.text <- "";
4848 let prevmode = state.mode
4849 and prevuioh = state.uioh in
4850 fillsrc prevmode prevuioh;
4851 let source = (src :> lvsource) in
4852 let modehash = findkeyhash conf "info" in
4853 state.uioh <- coe (object (self)
4854 inherit listview ~source ~trusted:true ~modehash as super
4855 val mutable m_prevmemused = 0
4856 method infochanged = function
4857 | Memused ->
4858 if m_prevmemused != state.memused
4859 then (
4860 m_prevmemused <- state.memused;
4861 G.postRedisplay "memusedchanged";
4863 | Pdim -> G.postRedisplay "pdimchanged"
4864 | Docinfo -> fillsrc prevmode prevuioh
4866 method key key mask =
4867 if not (Wsi.withctrl mask)
4868 then
4869 match key with
4870 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4871 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4872 | _ -> super#key key mask
4873 else super#key key mask
4874 end);
4875 G.postRedisplay "info";
4878 let enterhelpmode =
4879 let source =
4880 (object
4881 inherit lvsourcebase
4882 method getitemcount = Array.length state.help
4883 method getitem n =
4884 let s, l, _ = state.help.(n) in
4885 (s, l)
4887 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4888 let optuioh =
4889 if not cancel
4890 then (
4891 m_qsearch <- qsearch;
4892 match state.help.(active) with
4893 | _, _, Action f -> Some (f uioh)
4894 | _ -> Some (uioh)
4896 else None
4898 m_active <- active;
4899 m_first <- first;
4900 m_pan <- pan;
4901 optuioh
4903 method hasaction n =
4904 match state.help.(n) with
4905 | _, _, Action _ -> true
4906 | _ -> false
4908 initializer
4909 m_active <- -1
4910 end)
4911 in fun () ->
4912 let modehash = findkeyhash conf "help" in
4913 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4914 G.postRedisplay "help";
4917 let entermsgsmode =
4918 let msgsource =
4919 let re = Str.regexp "[\r\n]" in
4920 (object
4921 inherit lvsourcebase
4922 val mutable m_items = [||]
4924 method getitemcount = 1 + Array.length m_items
4926 method getitem n =
4927 if n = 0
4928 then "[Clear]", 0
4929 else m_items.(n-1), 0
4931 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4932 ignore uioh;
4933 if not cancel
4934 then (
4935 if active = 0
4936 then Buffer.clear state.errmsgs;
4937 m_qsearch <- qsearch;
4939 m_active <- active;
4940 m_first <- first;
4941 m_pan <- pan;
4942 None
4944 method hasaction n =
4945 n = 0
4947 method reset =
4948 state.newerrmsgs <- false;
4949 let l = Str.split re (Buffer.contents state.errmsgs) in
4950 m_items <- Array.of_list l
4952 initializer
4953 m_active <- 0
4954 end)
4955 in fun () ->
4956 state.text <- "";
4957 msgsource#reset;
4958 let source = (msgsource :> lvsource) in
4959 let modehash = findkeyhash conf "listview" in
4960 state.uioh <- coe (object
4961 inherit listview ~source ~trusted:false ~modehash as super
4962 method display =
4963 if state.newerrmsgs
4964 then msgsource#reset;
4965 super#display
4966 end);
4967 G.postRedisplay "msgs";
4970 let quickbookmark ?title () =
4971 match state.layout with
4972 | [] -> ()
4973 | l :: _ ->
4974 let title =
4975 match title with
4976 | None ->
4977 let sec = Unix.gettimeofday () in
4978 let tm = Unix.localtime sec in
4979 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4980 (l.pageno+1)
4981 tm.Unix.tm_mday
4982 tm.Unix.tm_mon
4983 (tm.Unix.tm_year + 1900)
4984 tm.Unix.tm_hour
4985 tm.Unix.tm_min
4986 | Some title -> title
4988 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4991 let setautoscrollspeed step goingdown =
4992 let incr = max 1 ((abs step) / 2) in
4993 let incr = if goingdown then incr else -incr in
4994 let astep = step + incr in
4995 state.autoscroll <- Some astep;
4998 let gotounder = function
4999 | Ulinkgoto (pageno, top) ->
5000 if pageno >= 0
5001 then (
5002 addnav ();
5003 gotopage1 pageno top;
5006 | Ulinkuri s ->
5007 gotouri s
5009 | Uremote (filename, pageno) ->
5010 let path =
5011 if String.length filename > 0
5012 then
5013 if Filename.is_relative filename
5014 then
5015 let dir = Filename.dirname state.path in
5016 let dir =
5017 if Filename.is_implicit dir
5018 then Filename.concat (Sys.getcwd ()) dir
5019 else dir
5021 Filename.concat dir filename
5022 else filename
5023 else ""
5025 let path =
5026 if Sys.file_exists path
5027 then path
5028 else ""
5030 if String.length path > 0
5031 then (
5032 if conf.riani
5033 then
5034 let command = !selfexec ^ " " ^ path in
5035 try popen command []
5036 with exn ->
5037 Printf.eprintf
5038 "failed to execute `%s': %s\n" command (exntos exn);
5039 flush stderr;
5040 else
5041 let anchor = getanchor () in
5042 let ranchor = state.path, state.password, anchor, state.origin in
5043 state.origin <- "";
5044 state.anchor <- (pageno, 0.0, 0.0);
5045 state.ranchors <- ranchor :: state.ranchors;
5046 opendoc path "";
5048 else showtext '!' ("Could not find " ^ filename)
5050 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5053 let canpan () =
5054 match conf.columns with
5055 | Csplit _ -> true
5056 | _ -> state.x != 0 || conf.zoom > 1.0
5059 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5061 let existsinrow pageno (columns, coverA, coverB) p =
5062 let last = ((pageno - coverA) mod columns) + columns in
5063 let rec any = function
5064 | [] -> false
5065 | l :: rest ->
5066 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5067 then p l
5068 else (
5069 if not (p l)
5070 then (if l.pageno = last then false else any rest)
5071 else true
5074 any state.layout
5077 let nextpage () =
5078 match state.layout with
5079 | [] ->
5080 let pageno = page_of_y state.y in
5081 gotoghyll (getpagey (pageno+1))
5082 | l :: rest ->
5083 match conf.columns with
5084 | Csingle _ ->
5085 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5086 then
5087 let y = clamp (pgscale state.winh) in
5088 gotoghyll y
5089 else
5090 let pageno = min (l.pageno+1) (state.pagecount-1) in
5091 gotoghyll (getpagey pageno)
5092 | Cmulti ((c, _, _) as cl, _) ->
5093 if conf.presentation
5094 && (existsinrow l.pageno cl
5095 (fun l -> l.pageh > l.pagey + l.pagevh))
5096 then
5097 let y = clamp (pgscale state.winh) in
5098 gotoghyll y
5099 else
5100 let pageno = min (l.pageno+c) (state.pagecount-1) in
5101 gotoghyll (getpagey pageno)
5102 | Csplit (n, _) ->
5103 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5104 then
5105 let pagey, pageh = getpageyh l.pageno in
5106 let pagey = pagey + pageh * l.pagecol in
5107 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5108 gotoghyll (pagey + pageh + ips)
5111 let prevpage () =
5112 match state.layout with
5113 | [] ->
5114 let pageno = page_of_y state.y in
5115 gotoghyll (getpagey (pageno-1))
5116 | l :: _ ->
5117 match conf.columns with
5118 | Csingle _ ->
5119 if conf.presentation && l.pagey != 0
5120 then
5121 gotoghyll (clamp (pgscale ~-(state.winh)))
5122 else
5123 let pageno = max 0 (l.pageno-1) in
5124 gotoghyll (getpagey pageno)
5125 | Cmulti ((c, _, coverB) as cl, _) ->
5126 if conf.presentation &&
5127 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5128 then
5129 gotoghyll (clamp (pgscale ~-(state.winh)))
5130 else
5131 let decr =
5132 if l.pageno = state.pagecount - coverB
5133 then 1
5134 else c
5136 let pageno = max 0 (l.pageno-decr) in
5137 gotoghyll (getpagey pageno)
5138 | Csplit (n, _) ->
5139 let y =
5140 if l.pagecol = 0
5141 then
5142 if l.pageno = 0
5143 then l.pagey
5144 else
5145 let pageno = max 0 (l.pageno-1) in
5146 let pagey, pageh = getpageyh pageno in
5147 pagey + (n-1)*pageh
5148 else
5149 let pagey, pageh = getpageyh l.pageno in
5150 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5152 gotoghyll y
5155 let viewkeyboard key mask =
5156 let enttext te =
5157 let mode = state.mode in
5158 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5159 state.text <- "";
5160 enttext ();
5161 G.postRedisplay "view:enttext"
5163 let ctrl = Wsi.withctrl mask in
5164 let key =
5165 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5167 match key with
5168 | 81 -> (* Q *)
5169 exit 0
5171 | 0xff63 -> (* insert *)
5172 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5173 then (
5174 state.mode <- LinkNav (Ltgendir 0);
5175 gotoy state.y;
5177 else showtext '!' "Keyboard link navigation does not work under rotation"
5179 | 0xff1b | 113 -> (* escape / q *)
5180 begin match state.mstate with
5181 | Mzoomrect _ ->
5182 state.mstate <- Mnone;
5183 Wsi.setcursor Wsi.CURSOR_INHERIT;
5184 G.postRedisplay "kill zoom rect";
5185 | _ ->
5186 begin match state.mode with
5187 | LinkNav _ ->
5188 state.mode <- View;
5189 G.postRedisplay "esc leave linknav"
5190 | _ ->
5191 match state.ranchors with
5192 | [] -> raise Quit
5193 | (path, password, anchor, origin) :: rest ->
5194 state.ranchors <- rest;
5195 state.anchor <- anchor;
5196 state.origin <- origin;
5197 opendoc path password
5198 end;
5199 end;
5201 | 0xff08 -> (* backspace *)
5202 gotoghyll (getnav ~-1)
5204 | 111 -> (* o *)
5205 enteroutlinemode ()
5207 | 117 -> (* u *)
5208 state.rects <- [];
5209 state.text <- "";
5210 G.postRedisplay "dehighlight";
5212 | 47 | 63 -> (* / ? *)
5213 let ondone isforw s =
5214 cbput state.hists.pat s;
5215 state.searchpattern <- s;
5216 search s isforw
5218 let s = String.create 1 in
5219 s.[0] <- Char.chr key;
5220 enttext (s, "", Some (onhist state.hists.pat),
5221 textentry, ondone (key = 47), true)
5223 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5224 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5225 setzoom (conf.zoom +. incr)
5227 | 43 | 0xffab -> (* + *)
5228 let ondone s =
5229 let n =
5230 try int_of_string s with exc ->
5231 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5232 max_int
5234 if n != max_int
5235 then (
5236 conf.pagebias <- n;
5237 state.text <- "page bias is now " ^ string_of_int n;
5240 enttext ("page bias: ", "", None, intentry, ondone, true)
5242 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5243 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5244 setzoom (max 0.01 (conf.zoom -. decr))
5246 | 45 | 0xffad -> (* - *)
5247 let ondone msg = state.text <- msg in
5248 enttext (
5249 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5250 optentry state.mode, ondone, true
5253 | 48 when ctrl -> (* ctrl-0 *)
5254 if conf.zoom = 1.0
5255 then (
5256 state.x <- 0;
5257 gotoy state.y
5259 else setzoom 1.0
5261 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5262 let cols =
5263 match conf.columns with
5264 | Csingle _ | Cmulti _ -> 1
5265 | Csplit (n, _) -> n
5267 let h = state.winh -
5268 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5270 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5271 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5272 then setzoom zoom
5274 | 51 when ctrl -> (* ctrl-3 *)
5275 let fm =
5276 match conf.fitmodel with
5277 | FitWidth -> FitProportional
5278 | FitProportional -> FitPage
5279 | FitPage -> FitWidth
5281 state.text <- "fit model: " ^ FMTE.to_string fm;
5282 reqlayout conf.angle fm
5284 | 0xffc6 -> (* f9 *)
5285 togglebirdseye ()
5287 | 57 when ctrl -> (* ctrl-9 *)
5288 togglebirdseye ()
5290 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5291 when not ctrl -> (* 0..9 *)
5292 let ondone s =
5293 let n =
5294 try int_of_string s with exc ->
5295 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5298 if n >= 0
5299 then (
5300 addnav ();
5301 cbput state.hists.pag (string_of_int n);
5302 gotopage1 (n + conf.pagebias - 1) 0;
5305 let pageentry text key =
5306 match Char.unsafe_chr key with
5307 | 'g' -> TEdone text
5308 | _ -> intentry text key
5310 let text = "x" in text.[0] <- Char.chr key;
5311 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5313 | 98 -> (* b *)
5314 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5315 reshape state.winw state.winh;
5317 | 108 -> (* l *)
5318 conf.hlinks <- not conf.hlinks;
5319 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5320 G.postRedisplay "toggle highlightlinks";
5322 | 70 -> (* F *)
5323 state.glinks <- true;
5324 let mode = state.mode in
5325 state.mode <- Textentry (
5326 (":", "", None, linknentry, linkndone gotounder, false),
5327 (fun _ ->
5328 state.glinks <- false;
5329 state.mode <- mode)
5331 state.text <- "";
5332 G.postRedisplay "view:linkent(F)"
5334 | 121 -> (* y *)
5335 state.glinks <- true;
5336 let mode = state.mode in
5337 state.mode <- Textentry (
5339 ":", "", None, linknentry, linkndone (fun under ->
5340 selstring (undertext under);
5341 ), false
5343 fun _ ->
5344 state.glinks <- false;
5345 state.mode <- mode
5347 state.text <- "";
5348 G.postRedisplay "view:linkent"
5350 | 97 -> (* a *)
5351 begin match state.autoscroll with
5352 | Some step ->
5353 conf.autoscrollstep <- step;
5354 state.autoscroll <- None
5355 | None ->
5356 if conf.autoscrollstep = 0
5357 then state.autoscroll <- Some 1
5358 else state.autoscroll <- Some conf.autoscrollstep
5361 | 112 when ctrl -> (* ctrl-p *)
5362 launchpath ()
5364 | 80 -> (* P *)
5365 setpresentationmode (not conf.presentation);
5366 showtext ' ' ("presentation mode " ^
5367 if conf.presentation then "on" else "off");
5369 | 102 -> (* f *)
5370 if List.mem Wsi.Fullscreen state.winstate
5371 then Wsi.reshape conf.cwinw conf.cwinh
5372 else Wsi.fullscreen ()
5374 | 112 | 78 -> (* p|N *)
5375 search state.searchpattern false
5377 | 110 | 0xffc0 -> (* n|F3 *)
5378 search state.searchpattern true
5380 | 116 -> (* t *)
5381 begin match state.layout with
5382 | [] -> ()
5383 | l :: _ ->
5384 gotoghyll (getpagey l.pageno)
5387 | 32 -> (* space *)
5388 nextpage ()
5390 | 0xff9f | 0xffff -> (* delete *)
5391 prevpage ()
5393 | 61 -> (* = *)
5394 showtext ' ' (describe_location ());
5396 | 119 -> (* w *)
5397 begin match state.layout with
5398 | [] -> ()
5399 | l :: _ ->
5400 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5401 G.postRedisplay "w"
5404 | 39 -> (* ' *)
5405 enterbookmarkmode ()
5407 | 104 | 0xffbe -> (* h|F1 *)
5408 enterhelpmode ()
5410 | 105 -> (* i *)
5411 enterinfomode ()
5413 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5414 entermsgsmode ()
5416 | 109 -> (* m *)
5417 let ondone s =
5418 match state.layout with
5419 | l :: _ ->
5420 if String.length s > 0
5421 then
5422 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5423 | _ -> ()
5425 enttext ("bookmark: ", "", None, textentry, ondone, true)
5427 | 126 -> (* ~ *)
5428 quickbookmark ();
5429 showtext ' ' "Quick bookmark added";
5431 | 122 -> (* z *)
5432 begin match state.layout with
5433 | l :: _ ->
5434 let rect = getpdimrect l.pagedimno in
5435 let w, h =
5436 if conf.crophack
5437 then
5438 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5439 truncate (1.2 *. (rect.(3) -. rect.(0))))
5440 else
5441 (truncate (rect.(1) -. rect.(0)),
5442 truncate (rect.(3) -. rect.(0)))
5444 let w = truncate ((float w)*.conf.zoom)
5445 and h = truncate ((float h)*.conf.zoom) in
5446 if w != 0 && h != 0
5447 then (
5448 state.anchor <- getanchor ();
5449 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5451 G.postRedisplay "z";
5453 | [] -> ()
5456 | 120 -> state.roam ()
5457 | 60 | 62 -> (* < > *)
5458 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5460 | 91 | 93 -> (* [ ] *)
5461 conf.colorscale <-
5462 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5464 G.postRedisplay "brightness";
5466 | 99 when state.mode = View -> (* [alt]-c *)
5467 if Wsi.withalt mask
5468 then (
5469 if conf.zoom > 1.0
5470 then
5471 let m = (wadjsb state.winw - state.w) / 2 in
5472 state.x <- m;
5473 gotoy_and_clear_text state.y
5475 else
5476 let (c, a, b), z =
5477 match state.prevcolumns with
5478 | None -> (1, 0, 0), 1.0
5479 | Some (columns, z) ->
5480 let cab =
5481 match columns with
5482 | Csplit (c, _) -> -c, 0, 0
5483 | Cmulti ((c, a, b), _) -> c, a, b
5484 | Csingle _ -> 1, 0, 0
5486 cab, z
5488 setcolumns View c a b;
5489 setzoom z
5491 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5492 setzoom state.prevzoom
5494 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5495 begin match state.autoscroll with
5496 | None ->
5497 begin match state.mode with
5498 | Birdseye beye -> upbirdseye 1 beye
5499 | _ ->
5500 if ctrl
5501 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5502 else (
5503 if not (Wsi.withshift mask) && conf.presentation
5504 then prevpage ()
5505 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5508 | Some n ->
5509 setautoscrollspeed n false
5512 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5513 begin match state.autoscroll with
5514 | None ->
5515 begin match state.mode with
5516 | Birdseye beye -> downbirdseye 1 beye
5517 | _ ->
5518 if ctrl
5519 then gotoy_and_clear_text (clamp (state.winh/2))
5520 else (
5521 if not (Wsi.withshift mask) && conf.presentation
5522 then nextpage ()
5523 else gotoy_and_clear_text (clamp conf.scrollstep)
5526 | Some n ->
5527 setautoscrollspeed n true
5530 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5531 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5532 if canpan ()
5533 then
5534 let dx =
5535 if ctrl
5536 then state.winw / 2
5537 else conf.hscrollstep
5539 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5540 state.x <- panbound (state.x + dx);
5541 gotoy_and_clear_text state.y
5542 else (
5543 state.text <- "";
5544 G.postRedisplay "left/right"
5547 | 0xff55 | 0xff9a -> (* (kp) prior *)
5548 let y =
5549 if ctrl
5550 then
5551 match state.layout with
5552 | [] -> state.y
5553 | l :: _ -> state.y - l.pagey
5554 else
5555 clamp (pgscale (-state.winh))
5557 gotoghyll y
5559 | 0xff56 | 0xff9b -> (* (kp) next *)
5560 let y =
5561 if ctrl
5562 then
5563 match List.rev state.layout with
5564 | [] -> state.y
5565 | l :: _ -> getpagey l.pageno
5566 else
5567 clamp (pgscale state.winh)
5569 gotoghyll y
5571 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5572 gotoghyll 0
5573 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5574 gotoghyll (clamp state.maxy)
5576 | 0xff53 | 0xff98
5577 when Wsi.withalt mask -> (* alt-(kp) right *)
5578 gotoghyll (getnav 1)
5579 | 0xff51 | 0xff96
5580 when Wsi.withalt mask -> (* alt-(kp) left *)
5581 gotoghyll (getnav ~-1)
5583 | 114 -> (* r *)
5584 reload ()
5586 | 118 when conf.debug -> (* v *)
5587 state.rects <- [];
5588 List.iter (fun l ->
5589 match getopaque l.pageno with
5590 | None -> ()
5591 | Some opaque ->
5592 let x0, y0, x1, y1 = pagebbox opaque in
5593 let a,b = float x0, float y0 in
5594 let c,d = float x1, float y0 in
5595 let e,f = float x1, float y1 in
5596 let h,j = float x0, float y1 in
5597 let rect = (a,b,c,d,e,f,h,j) in
5598 debugrect rect;
5599 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5600 ) state.layout;
5601 G.postRedisplay "v";
5603 | _ ->
5604 vlog "huh? %s" (Wsi.keyname key)
5607 let linknavkeyboard key mask linknav =
5608 let getpage pageno =
5609 let rec loop = function
5610 | [] -> None
5611 | l :: _ when l.pageno = pageno -> Some l
5612 | _ :: rest -> loop rest
5613 in loop state.layout
5615 let doexact (pageno, n) =
5616 match getopaque pageno, getpage pageno with
5617 | Some opaque, Some l ->
5618 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5619 then
5620 let under = getlink opaque n in
5621 G.postRedisplay "link gotounder";
5622 gotounder under;
5623 state.mode <- View;
5624 else
5625 let opt, dir =
5626 match key with
5627 | 0xff50 -> (* home *)
5628 Some (findlink opaque LDfirst), -1
5630 | 0xff57 -> (* end *)
5631 Some (findlink opaque LDlast), 1
5633 | 0xff51 -> (* left *)
5634 Some (findlink opaque (LDleft n)), -1
5636 | 0xff53 -> (* right *)
5637 Some (findlink opaque (LDright n)), 1
5639 | 0xff52 -> (* up *)
5640 Some (findlink opaque (LDup n)), -1
5642 | 0xff54 -> (* down *)
5643 Some (findlink opaque (LDdown n)), 1
5645 | _ -> None, 0
5647 let pwl l dir =
5648 begin match findpwl l.pageno dir with
5649 | Pwlnotfound -> ()
5650 | Pwl pageno ->
5651 let notfound dir =
5652 state.mode <- LinkNav (Ltgendir dir);
5653 let y, h = getpageyh pageno in
5654 let y =
5655 if dir < 0
5656 then y + h - state.winh
5657 else y
5659 gotoy y
5661 begin match getopaque pageno, getpage pageno with
5662 | Some opaque, Some _ ->
5663 let link =
5664 let ld = if dir > 0 then LDfirst else LDlast in
5665 findlink opaque ld
5667 begin match link with
5668 | Lfound m ->
5669 showlinktype (getlink opaque m);
5670 state.mode <- LinkNav (Ltexact (pageno, m));
5671 G.postRedisplay "linknav jpage";
5672 | _ -> notfound dir
5673 end;
5674 | _ -> notfound dir
5675 end;
5676 end;
5678 begin match opt with
5679 | Some Lnotfound -> pwl l dir;
5680 | Some (Lfound m) ->
5681 if m = n
5682 then pwl l dir
5683 else (
5684 let _, y0, _, y1 = getlinkrect opaque m in
5685 if y0 < l.pagey
5686 then gotopage1 l.pageno y0
5687 else (
5688 let d = fstate.fontsize + 1 in
5689 if y1 - l.pagey > l.pagevh - d
5690 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5691 else G.postRedisplay "linknav";
5693 showlinktype (getlink opaque m);
5694 state.mode <- LinkNav (Ltexact (l.pageno, m));
5697 | None -> viewkeyboard key mask
5698 end;
5699 | _ -> viewkeyboard key mask
5701 if key = 0xff63
5702 then (
5703 state.mode <- View;
5704 G.postRedisplay "leave linknav"
5706 else
5707 match linknav with
5708 | Ltgendir _ -> viewkeyboard key mask
5709 | Ltexact exact -> doexact exact
5712 let keyboard key mask =
5713 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5714 then wcmd "interrupt"
5715 else state.uioh <- state.uioh#key key mask
5718 let birdseyekeyboard key mask
5719 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5720 let incr =
5721 match conf.columns with
5722 | Csingle _ -> 1
5723 | Cmulti ((c, _, _), _) -> c
5724 | Csplit _ -> failwith "bird's eye split mode"
5726 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5727 match key with
5728 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5729 let y, h = getpageyh pageno in
5730 let top = (state.winh - h) / 2 in
5731 gotoy (max 0 (y - top))
5732 | 0xff0d (* enter *)
5733 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5734 | 0xff1b -> leavebirdseye beye true (* escape *)
5735 | 0xff52 -> upbirdseye incr beye (* up *)
5736 | 0xff54 -> downbirdseye incr beye (* down *)
5737 | 0xff51 -> upbirdseye 1 beye (* left *)
5738 | 0xff53 -> downbirdseye 1 beye (* right *)
5740 | 0xff55 -> (* prior *)
5741 begin match state.layout with
5742 | l :: _ ->
5743 if l.pagey != 0
5744 then (
5745 state.mode <- Birdseye (
5746 oconf, leftx, l.pageno, hooverpageno, anchor
5748 gotopage1 l.pageno 0;
5750 else (
5751 let layout = layout (state.y-state.winh) (pgh state.layout) in
5752 match layout with
5753 | [] -> gotoy (clamp (-state.winh))
5754 | l :: _ ->
5755 state.mode <- Birdseye (
5756 oconf, leftx, l.pageno, hooverpageno, anchor
5758 gotopage1 l.pageno 0
5761 | [] -> gotoy (clamp (-state.winh))
5762 end;
5764 | 0xff56 -> (* next *)
5765 begin match List.rev state.layout with
5766 | l :: _ ->
5767 let layout = layout (state.y + (pgh state.layout)) state.winh in
5768 begin match layout with
5769 | [] ->
5770 let incr = l.pageh - l.pagevh in
5771 if incr = 0
5772 then (
5773 state.mode <-
5774 Birdseye (
5775 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5777 G.postRedisplay "birdseye pagedown";
5779 else gotoy (clamp (incr + conf.interpagespace*2));
5781 | l :: _ ->
5782 state.mode <-
5783 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5784 gotopage1 l.pageno 0;
5787 | [] -> gotoy (clamp state.winh)
5788 end;
5790 | 0xff50 -> (* home *)
5791 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5792 gotopage1 0 0
5794 | 0xff57 -> (* end *)
5795 let pageno = state.pagecount - 1 in
5796 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5797 if not (pagevisible state.layout pageno)
5798 then
5799 let h =
5800 match List.rev state.pdims with
5801 | [] -> state.winh
5802 | (_, _, h, _) :: _ -> h
5804 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5805 else G.postRedisplay "birdseye end";
5806 | _ -> viewkeyboard key mask
5809 let drawpage l =
5810 let color =
5811 match state.mode with
5812 | Textentry _ -> scalecolor 0.4
5813 | LinkNav _
5814 | View -> scalecolor 1.0
5815 | Birdseye (_, _, pageno, hooverpageno, _) ->
5816 if l.pageno = hooverpageno
5817 then scalecolor 0.9
5818 else (
5819 if l.pageno = pageno
5820 then scalecolor 1.0
5821 else scalecolor 0.8
5824 drawtiles l color;
5827 let postdrawpage l linkindexbase =
5828 match getopaque l.pageno with
5829 | Some opaque ->
5830 if tileready l l.pagex l.pagey
5831 then
5832 let x = l.pagedispx - l.pagex
5833 and y = l.pagedispy - l.pagey in
5834 let hlmask =
5835 match conf.columns with
5836 | Csingle _ | Cmulti _ ->
5837 (if conf.hlinks then 1 else 0)
5838 + (if state.glinks
5839 && not (isbirdseye state.mode) then 2 else 0)
5840 | _ -> 0
5842 let s =
5843 match state.mode with
5844 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5845 | _ -> ""
5847 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5848 else 0
5849 | _ -> 0
5852 let scrollindicator () =
5853 let sbw, ph, sh = state.uioh#scrollph in
5854 let sbh, pw, sw = state.uioh#scrollpw in
5856 GlDraw.color (0.64, 0.64, 0.64);
5857 GlDraw.rect
5858 (float (state.winw - sbw), 0.)
5859 (float state.winw, float state.winh)
5861 GlDraw.rect
5862 (0., float (state.winh - sbh))
5863 (float (wadjsb state.winw - 1), float state.winh)
5865 GlDraw.color (0.0, 0.0, 0.0);
5867 GlDraw.rect
5868 (float (state.winw - sbw), ph)
5869 (float state.winw, ph +. sh)
5871 GlDraw.rect
5872 (pw, float (state.winh - sbh))
5873 (pw +. sw, float state.winh)
5877 let showsel () =
5878 match state.mstate with
5879 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5882 | Msel ((x0, y0), (x1, y1)) ->
5883 let rec loop = function
5884 | l :: ls ->
5885 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5886 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5887 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5888 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5889 then
5890 match getopaque l.pageno with
5891 | Some opaque ->
5892 let x0, y0 = pagetranslatepoint l x0 y0 in
5893 let x1, y1 = pagetranslatepoint l x1 y1 in
5894 seltext opaque (x0, y0, x1, y1);
5895 | _ -> ()
5896 else loop ls
5897 | [] -> ()
5899 loop state.layout
5902 let showrects rects =
5903 Gl.enable `blend;
5904 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5905 GlDraw.polygon_mode `both `fill;
5906 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5907 List.iter
5908 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5909 List.iter (fun l ->
5910 if l.pageno = pageno
5911 then (
5912 let dx = float (l.pagedispx - l.pagex) in
5913 let dy = float (l.pagedispy - l.pagey) in
5914 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5915 GlDraw.begins `quads;
5917 GlDraw.vertex2 (x0+.dx, y0+.dy);
5918 GlDraw.vertex2 (x1+.dx, y1+.dy);
5919 GlDraw.vertex2 (x2+.dx, y2+.dy);
5920 GlDraw.vertex2 (x3+.dx, y3+.dy);
5922 GlDraw.ends ();
5924 ) state.layout
5925 ) rects
5927 Gl.disable `blend;
5930 let display () =
5931 GlClear.color (scalecolor2 conf.bgcolor);
5932 GlClear.clear [`color];
5933 List.iter drawpage state.layout;
5934 let rects =
5935 match state.mode with
5936 | LinkNav (Ltexact (pageno, linkno)) ->
5937 begin match getopaque pageno with
5938 | Some opaque ->
5939 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5940 (pageno, 5, (
5941 float x0, float y0,
5942 float x1, float y0,
5943 float x1, float y1,
5944 float x0, float y1)
5945 ) :: state.rects
5946 | None -> state.rects
5948 | _ -> state.rects
5950 showrects rects;
5951 let rec postloop linkindexbase = function
5952 | l :: rest ->
5953 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5954 postloop linkindexbase rest
5955 | [] -> ()
5957 showsel ();
5958 postloop 0 state.layout;
5959 state.uioh#display;
5960 begin match state.mstate with
5961 | Mzoomrect ((x0, y0), (x1, y1)) ->
5962 Gl.enable `blend;
5963 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5964 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5965 GlDraw.rect (float x0, float y0)
5966 (float x1, float y1);
5967 Gl.disable `blend;
5968 | _ -> ()
5969 end;
5970 enttext ();
5971 scrollindicator ();
5972 Wsi.swapb ();
5975 let zoomrect x y x1 y1 =
5976 let x0 = min x x1
5977 and x1 = max x x1
5978 and y0 = min y y1 in
5979 gotoy (state.y + y0);
5980 state.anchor <- getanchor ();
5981 let zoom = (float state.w) /. float (x1 - x0) in
5982 let margin =
5983 match conf.fitmodel, conf.columns with
5984 | FitPage, Csplit _ ->
5985 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5987 | _, _ ->
5988 let adjw = wadjsb state.winw in
5989 if state.w < adjw
5990 then (adjw - state.w) / 2
5991 else 0
5993 state.x <- (state.x + margin) - x0;
5994 setzoom zoom;
5995 Wsi.setcursor Wsi.CURSOR_INHERIT;
5996 state.mstate <- Mnone;
5999 let scrollx x =
6000 let winw = wadjsb state.winw - 1 in
6001 let s = float x /. float winw in
6002 let destx = truncate (float (state.w + winw) *. s) in
6003 state.x <- winw - destx;
6004 gotoy_and_clear_text state.y;
6005 state.mstate <- Mscrollx;
6008 let scrolly y =
6009 let s = float y /. float state.winh in
6010 let desty = truncate (float (state.maxy - state.winh) *. s) in
6011 gotoy_and_clear_text desty;
6012 state.mstate <- Mscrolly;
6015 let viewmouse button down x y mask =
6016 match button with
6017 | n when (n == 4 || n == 5) && not down ->
6018 if Wsi.withctrl mask
6019 then (
6020 match state.mstate with
6021 | Mzoom (oldn, i) ->
6022 if oldn = n
6023 then (
6024 if i = 2
6025 then
6026 let incr =
6027 match n with
6028 | 5 ->
6029 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6030 | _ ->
6031 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6033 let zoom = conf.zoom -. incr in
6034 setzoom zoom;
6035 state.mstate <- Mzoom (n, 0);
6036 else
6037 state.mstate <- Mzoom (n, i+1);
6039 else state.mstate <- Mzoom (n, 0)
6041 | _ -> state.mstate <- Mzoom (n, 0)
6043 else (
6044 match state.autoscroll with
6045 | Some step -> setautoscrollspeed step (n=4)
6046 | None ->
6047 if conf.wheelbypage || conf.presentation
6048 then (
6049 if n = 4
6050 then prevpage ()
6051 else nextpage ()
6053 else
6054 let incr =
6055 if n = 4
6056 then -conf.scrollstep
6057 else conf.scrollstep
6059 let incr = incr * 2 in
6060 let y = clamp incr in
6061 gotoy_and_clear_text y
6064 | n when (n = 6 || n = 7) && not down && canpan () ->
6065 state.x <-
6066 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6067 gotoy_and_clear_text state.y
6069 | 1 when Wsi.withshift mask ->
6070 state.mstate <- Mnone;
6071 if not down
6072 then (
6073 match unproject x y with
6074 | Some (pageno, ux, uy) ->
6075 let cmd = Printf.sprintf
6076 "%s %s %d %d %d"
6077 conf.stcmd state.path pageno ux uy
6079 popen cmd []
6080 | None -> ()
6083 | 1 when Wsi.withctrl mask ->
6084 if down
6085 then (
6086 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6087 state.mstate <- Mpan (x, y)
6089 else
6090 state.mstate <- Mnone
6092 | 3 ->
6093 if down
6094 then (
6095 Wsi.setcursor Wsi.CURSOR_CYCLE;
6096 let p = (x, y) in
6097 state.mstate <- Mzoomrect (p, p)
6099 else (
6100 match state.mstate with
6101 | Mzoomrect ((x0, y0), _) ->
6102 if abs (x-x0) > 10 && abs (y - y0) > 10
6103 then zoomrect x0 y0 x y
6104 else (
6105 state.mstate <- Mnone;
6106 Wsi.setcursor Wsi.CURSOR_INHERIT;
6107 G.postRedisplay "kill accidental zoom rect";
6109 | _ ->
6110 Wsi.setcursor Wsi.CURSOR_INHERIT;
6111 state.mstate <- Mnone
6114 | 1 when x > state.winw - vscrollw () ->
6115 if down
6116 then
6117 let _, position, sh = state.uioh#scrollph in
6118 if y > truncate position && y < truncate (position +. sh)
6119 then state.mstate <- Mscrolly
6120 else scrolly y
6121 else
6122 state.mstate <- Mnone
6124 | 1 when y > state.winh - hscrollh () ->
6125 if down
6126 then
6127 let _, position, sw = state.uioh#scrollpw in
6128 if x > truncate position && x < truncate (position +. sw)
6129 then state.mstate <- Mscrollx
6130 else scrollx x
6131 else
6132 state.mstate <- Mnone
6134 | 1 ->
6135 let dest = if down then getunder x y else Unone in
6136 begin match dest with
6137 | Ulinkgoto _
6138 | Ulinkuri _
6139 | Uremote _
6140 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6141 gotounder dest
6143 | Unone when down ->
6144 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6145 state.mstate <- Mpan (x, y);
6147 | Unone | Utext _ ->
6148 if down
6149 then (
6150 if conf.angle mod 360 = 0
6151 then (
6152 state.mstate <- Msel ((x, y), (x, y));
6153 G.postRedisplay "mouse select";
6156 else (
6157 match state.mstate with
6158 | Mnone -> ()
6160 | Mzoom _ | Mscrollx | Mscrolly ->
6161 state.mstate <- Mnone
6163 | Mzoomrect ((x0, y0), _) ->
6164 zoomrect x0 y0 x y
6166 | Mpan _ ->
6167 Wsi.setcursor Wsi.CURSOR_INHERIT;
6168 state.mstate <- Mnone
6170 | Msel ((x0, y0), (x1, y1)) ->
6171 let rec loop = function
6172 | [] -> ()
6173 | l :: rest ->
6174 let inside =
6175 let a0 = l.pagedispy in
6176 let a1 = a0 + l.pagevh in
6177 let b0 = l.pagedispx in
6178 let b1 = b0 + l.pagevw in
6179 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6180 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6182 if inside
6183 then
6184 match getopaque l.pageno with
6185 | Some opaque ->
6186 begin
6187 match Ne.pipe () with
6188 | Ne.Exn exn ->
6189 showtext '!'
6190 (Printf.sprintf
6191 "can not create sel pipe: %s"
6192 (exntos exn));
6193 | Ne.Res (r, w) ->
6194 let doclose what fd =
6195 Ne.clo fd (fun msg ->
6196 dolog "%s close failed: %s" what msg)
6199 popen conf.selcmd [r, 0; w, -1];
6200 copysel w opaque true;
6201 doclose "pipe/r" r;
6202 G.postRedisplay "copysel";
6203 with exn ->
6204 dolog "can not execute %S: %s"
6205 conf.selcmd (exntos exn);
6206 doclose "pipe/r" r;
6207 doclose "pipe/w" w;
6209 | None -> ()
6210 else loop rest
6212 loop state.layout;
6213 Wsi.setcursor Wsi.CURSOR_INHERIT;
6214 state.mstate <- Mnone;
6218 | _ -> ()
6221 let birdseyemouse button down x y mask
6222 (conf, leftx, _, hooverpageno, anchor) =
6223 match button with
6224 | 1 when down ->
6225 let rec loop = function
6226 | [] -> ()
6227 | l :: rest ->
6228 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6229 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6230 then (
6231 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6233 else loop rest
6235 loop state.layout
6236 | 3 -> ()
6237 | _ -> viewmouse button down x y mask
6240 let mouse button down x y mask =
6241 state.uioh <- state.uioh#button button down x y mask;
6244 let motion ~x ~y =
6245 state.uioh <- state.uioh#motion x y
6248 let pmotion ~x ~y =
6249 state.uioh <- state.uioh#pmotion x y;
6252 let uioh = object
6253 method display = ()
6255 method key key mask =
6256 begin match state.mode with
6257 | Textentry textentry -> textentrykeyboard key mask textentry
6258 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6259 | View -> viewkeyboard key mask
6260 | LinkNav linknav -> linknavkeyboard key mask linknav
6261 end;
6262 state.uioh
6264 method button button bstate x y mask =
6265 begin match state.mode with
6266 | LinkNav _
6267 | View -> viewmouse button bstate x y mask
6268 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6269 | Textentry _ -> ()
6270 end;
6271 state.uioh
6273 method motion x y =
6274 begin match state.mode with
6275 | Textentry _ -> ()
6276 | View | Birdseye _ | LinkNav _ ->
6277 match state.mstate with
6278 | Mzoom _ | Mnone -> ()
6280 | Mpan (x0, y0) ->
6281 let dx = x - x0
6282 and dy = y0 - y in
6283 state.mstate <- Mpan (x, y);
6284 if canpan ()
6285 then state.x <- panbound (state.x + dx);
6286 let y = clamp dy in
6287 gotoy_and_clear_text y
6289 | Msel (a, _) ->
6290 state.mstate <- Msel (a, (x, y));
6291 G.postRedisplay "motion select";
6293 | Mscrolly ->
6294 let y = min state.winh (max 0 y) in
6295 scrolly y
6297 | Mscrollx ->
6298 let x = min state.winw (max 0 x) in
6299 scrollx x
6301 | Mzoomrect (p0, _) ->
6302 state.mstate <- Mzoomrect (p0, (x, y));
6303 G.postRedisplay "motion zoomrect";
6304 end;
6305 state.uioh
6307 method pmotion x y =
6308 begin match state.mode with
6309 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6310 let rec loop = function
6311 | [] ->
6312 if hooverpageno != -1
6313 then (
6314 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6315 G.postRedisplay "pmotion birdseye no hoover";
6317 | l :: rest ->
6318 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6319 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6320 then (
6321 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6322 G.postRedisplay "pmotion birdseye hoover";
6324 else loop rest
6326 loop state.layout
6328 | Textentry _ -> ()
6330 | LinkNav _
6331 | View ->
6332 match state.mstate with
6333 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6335 | Mnone ->
6336 updateunder x y;
6337 match conf.pax with
6338 | None -> ()
6339 | Some r ->
6340 let past, _, _ = !r in
6341 let now = now () in
6342 let delta = now -. past in
6343 if delta > 0.01
6344 then paxunder x y
6345 else r := (now, x, y)
6346 end;
6347 state.uioh
6349 method infochanged _ = ()
6351 method scrollph =
6352 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6353 let p, h =
6354 if maxy = 0
6355 then 0.0, float state.winh
6356 else scrollph state.y maxy
6358 vscrollw (), p, h
6360 method scrollpw =
6361 let winw = wadjsb state.winw in
6362 let fwinw = float winw in
6363 let sw =
6364 let sw = fwinw /. float state.w in
6365 let sw = fwinw *. sw in
6366 max sw (float conf.scrollh)
6368 let position =
6369 let maxx = state.w + winw in
6370 let x = winw - state.x in
6371 let percent = float x /. float maxx in
6372 (fwinw -. sw) *. percent
6374 hscrollh (), position, sw
6376 method modehash =
6377 let modename =
6378 match state.mode with
6379 | LinkNav _ -> "links"
6380 | Textentry _ -> "textentry"
6381 | Birdseye _ -> "birdseye"
6382 | View -> "view"
6384 findkeyhash conf modename
6386 method eformsgs = true
6387 end;;
6389 module Config =
6390 struct
6391 open Parser
6393 let fontpath = ref "";;
6395 module KeyMap =
6396 Map.Make (struct type t = (int * int) let compare = compare end);;
6398 let unent s =
6399 let l = String.length s in
6400 let b = Buffer.create l in
6401 unent b s 0 l;
6402 Buffer.contents b;
6405 let home =
6406 try Sys.getenv "HOME"
6407 with exn ->
6408 prerr_endline
6409 ("Can not determine home directory location: " ^ exntos exn);
6413 let modifier_of_string = function
6414 | "alt" -> Wsi.altmask
6415 | "shift" -> Wsi.shiftmask
6416 | "ctrl" | "control" -> Wsi.ctrlmask
6417 | "meta" -> Wsi.metamask
6418 | _ -> 0
6421 let key_of_string =
6422 let r = Str.regexp "-" in
6423 fun s ->
6424 let elems = Str.full_split r s in
6425 let f n k m =
6426 let g s =
6427 let m1 = modifier_of_string s in
6428 if m1 = 0
6429 then (Wsi.namekey s, m)
6430 else (k, m lor m1)
6431 in function
6432 | Str.Delim s when n land 1 = 0 -> g s
6433 | Str.Text s -> g s
6434 | Str.Delim _ -> (k, m)
6436 let rec loop n k m = function
6437 | [] -> (k, m)
6438 | x :: xs ->
6439 let k, m = f n k m x in
6440 loop (n+1) k m xs
6442 loop 0 0 0 elems
6445 let keys_of_string =
6446 let r = Str.regexp "[ \t]" in
6447 fun s ->
6448 let elems = Str.split r s in
6449 List.map key_of_string elems
6452 let copykeyhashes c =
6453 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6456 let config_of c attrs =
6457 let apply c k v =
6459 match k with
6460 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6461 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6462 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6463 | "preload" -> { c with preload = bool_of_string v }
6464 | "page-bias" -> { c with pagebias = int_of_string v }
6465 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6466 | "horizontal-scroll-step" ->
6467 { c with hscrollstep = max (int_of_string v) 1 }
6468 | "auto-scroll-step" ->
6469 { c with autoscrollstep = max 0 (int_of_string v) }
6470 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6471 | "crop-hack" -> { c with crophack = bool_of_string v }
6472 | "throttle" ->
6473 let mw =
6474 match String.lowercase v with
6475 | "true" -> Some infinity
6476 | "false" -> None
6477 | f -> Some (float_of_string f)
6479 { c with maxwait = mw}
6480 | "highlight-links" -> { c with hlinks = bool_of_string v }
6481 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6482 | "vertical-margin" ->
6483 { c with interpagespace = max 0 (int_of_string v) }
6484 | "zoom" ->
6485 let zoom = float_of_string v /. 100. in
6486 let zoom = max zoom 0.0 in
6487 { c with zoom = zoom }
6488 | "presentation" -> { c with presentation = bool_of_string v }
6489 | "rotation-angle" -> { c with angle = int_of_string v }
6490 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6491 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6492 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6493 | "proportional-display" ->
6494 let fm =
6495 if bool_of_string v
6496 then FitProportional
6497 else FitWidth
6499 { c with fitmodel = fm }
6500 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6501 | "pixmap-cache-size" ->
6502 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6503 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6504 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6505 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6506 | "persistent-location" -> { c with jumpback = bool_of_string v }
6507 | "background-color" -> { c with bgcolor = color_of_string v }
6508 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6509 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6510 | "mupdf-store-size" ->
6511 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6512 | "checkers" -> { c with checkers = bool_of_string v }
6513 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6514 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6515 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6516 | "uri-launcher" -> { c with urilauncher = unent v }
6517 | "path-launcher" -> { c with pathlauncher = unent v }
6518 | "color-space" -> { c with colorspace = CSTE.of_string v }
6519 | "invert-colors" -> { c with invert = bool_of_string v }
6520 | "brightness" -> { c with colorscale = float_of_string v }
6521 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6522 | "ghyllscroll" ->
6523 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6524 | "columns" ->
6525 let (n, _, _) as nab = multicolumns_of_string v in
6526 if n < 0
6527 then { c with columns = Csplit (-n, [||]) }
6528 else { c with columns = Cmulti (nab, [||]) }
6529 | "birds-eye-columns" ->
6530 { c with beyecolumns = Some (max (int_of_string v) 2) }
6531 | "selection-command" -> { c with selcmd = unent v }
6532 | "synctex-command" -> { c with stcmd = unent v }
6533 | "pax-command" -> { c with paxcmd = unent v }
6534 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6535 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6536 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6537 | "use-pbo" -> { c with usepbo = bool_of_string v }
6538 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6539 | "horizontal-scrollbar-visible" ->
6540 let b =
6541 if bool_of_string v
6542 then c.scrollb lor scrollbhv
6543 else c.scrollb land (lnot scrollbhv)
6545 { c with scrollb = b }
6546 | "vertical-scrollbar-visible" ->
6547 let b =
6548 if bool_of_string v
6549 then c.scrollb lor scrollbvv
6550 else c.scrollb land (lnot scrollbvv)
6552 { c with scrollb = b }
6553 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6554 | "point-and-x" ->
6555 { c with pax =
6556 if bool_of_string v
6557 then Some (ref (0.0, 0, 0))
6558 else None }
6559 | _ -> c
6560 with exn ->
6561 prerr_endline ("Error processing attribute (`" ^
6562 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6565 let rec fold c = function
6566 | [] -> c
6567 | (k, v) :: rest ->
6568 let c = apply c k v in
6569 fold c rest
6571 fold { c with keyhashes = copykeyhashes c } attrs;
6574 let fromstring f pos n v d =
6575 try f v
6576 with exn ->
6577 dolog "Error processing attribute (%S=%S) at %d\n%s"
6578 n v pos (exntos exn)
6583 let bookmark_of attrs =
6584 let rec fold title page rely visy = function
6585 | ("title", v) :: rest -> fold v page rely visy rest
6586 | ("page", v) :: rest -> fold title v rely visy rest
6587 | ("rely", v) :: rest -> fold title page v visy rest
6588 | ("visy", v) :: rest -> fold title page rely v rest
6589 | _ :: rest -> fold title page rely visy rest
6590 | [] -> title, page, rely, visy
6592 fold "invalid" "0" "0" "0" attrs
6595 let doc_of attrs =
6596 let rec fold path page rely pan visy = function
6597 | ("path", v) :: rest -> fold v page rely pan visy rest
6598 | ("page", v) :: rest -> fold path v rely pan visy rest
6599 | ("rely", v) :: rest -> fold path page v pan visy rest
6600 | ("pan", v) :: rest -> fold path page rely v visy rest
6601 | ("visy", v) :: rest -> fold path page rely pan v rest
6602 | _ :: rest -> fold path page rely pan visy rest
6603 | [] -> path, page, rely, pan, visy
6605 fold "" "0" "0" "0" "0" attrs
6608 let map_of attrs =
6609 let rec fold rs ls = function
6610 | ("out", v) :: rest -> fold v ls rest
6611 | ("in", v) :: rest -> fold rs v rest
6612 | _ :: rest -> fold ls rs rest
6613 | [] -> ls, rs
6615 fold "" "" attrs
6618 let setconf dst src =
6619 dst.scrollbw <- src.scrollbw;
6620 dst.scrollh <- src.scrollh;
6621 dst.icase <- src.icase;
6622 dst.preload <- src.preload;
6623 dst.pagebias <- src.pagebias;
6624 dst.verbose <- src.verbose;
6625 dst.scrollstep <- src.scrollstep;
6626 dst.maxhfit <- src.maxhfit;
6627 dst.crophack <- src.crophack;
6628 dst.autoscrollstep <- src.autoscrollstep;
6629 dst.maxwait <- src.maxwait;
6630 dst.hlinks <- src.hlinks;
6631 dst.underinfo <- src.underinfo;
6632 dst.interpagespace <- src.interpagespace;
6633 dst.zoom <- src.zoom;
6634 dst.presentation <- src.presentation;
6635 dst.angle <- src.angle;
6636 dst.cwinw <- src.cwinw;
6637 dst.cwinh <- src.cwinh;
6638 dst.savebmarks <- src.savebmarks;
6639 dst.memlimit <- src.memlimit;
6640 dst.fitmodel <- src.fitmodel;
6641 dst.texcount <- src.texcount;
6642 dst.sliceheight <- src.sliceheight;
6643 dst.thumbw <- src.thumbw;
6644 dst.jumpback <- src.jumpback;
6645 dst.bgcolor <- src.bgcolor;
6646 dst.tilew <- src.tilew;
6647 dst.tileh <- src.tileh;
6648 dst.mustoresize <- src.mustoresize;
6649 dst.checkers <- src.checkers;
6650 dst.aalevel <- src.aalevel;
6651 dst.trimmargins <- src.trimmargins;
6652 dst.trimfuzz <- src.trimfuzz;
6653 dst.urilauncher <- src.urilauncher;
6654 dst.colorspace <- src.colorspace;
6655 dst.invert <- src.invert;
6656 dst.colorscale <- src.colorscale;
6657 dst.redirectstderr <- src.redirectstderr;
6658 dst.ghyllscroll <- src.ghyllscroll;
6659 dst.columns <- src.columns;
6660 dst.beyecolumns <- src.beyecolumns;
6661 dst.selcmd <- src.selcmd;
6662 dst.updatecurs <- src.updatecurs;
6663 dst.pathlauncher <- src.pathlauncher;
6664 dst.keyhashes <- copykeyhashes src;
6665 dst.hfsize <- src.hfsize;
6666 dst.hscrollstep <- src.hscrollstep;
6667 dst.pgscale <- src.pgscale;
6668 dst.usepbo <- src.usepbo;
6669 dst.wheelbypage <- src.wheelbypage;
6670 dst.stcmd <- src.stcmd;
6671 dst.paxcmd <- src.paxcmd;
6672 dst.scrollb <- src.scrollb;
6673 dst.riani <- src.riani;
6674 dst.pax <-
6675 if src.pax = None
6676 then None
6677 else Some ((ref (0.0, 0, 0)));
6680 let get s =
6681 let h = Hashtbl.create 10 in
6682 let dc = { defconf with angle = defconf.angle } in
6683 let rec toplevel v t spos _ =
6684 match t with
6685 | Vdata | Vcdata | Vend -> v
6686 | Vopen ("llppconfig", _, closed) ->
6687 if closed
6688 then v
6689 else { v with f = llppconfig }
6690 | Vopen _ ->
6691 error "unexpected subelement at top level" s spos
6692 | Vclose _ -> error "unexpected close at top level" s spos
6694 and llppconfig v t spos _ =
6695 match t with
6696 | Vdata | Vcdata -> v
6697 | Vend -> error "unexpected end of input in llppconfig" s spos
6698 | Vopen ("defaults", attrs, closed) ->
6699 let c = config_of dc attrs in
6700 setconf dc c;
6701 if closed
6702 then v
6703 else { v with f = defaults }
6705 | Vopen ("ui-font", attrs, closed) ->
6706 let rec getsize size = function
6707 | [] -> size
6708 | ("size", v) :: rest ->
6709 let size =
6710 fromstring int_of_string spos "size" v fstate.fontsize in
6711 getsize size rest
6712 | l -> getsize size l
6714 fstate.fontsize <- getsize fstate.fontsize attrs;
6715 if closed
6716 then v
6717 else { v with f = uifont (Buffer.create 10) }
6719 | Vopen ("doc", attrs, closed) ->
6720 let pathent, spage, srely, span, svisy = doc_of attrs in
6721 let path = unent pathent
6722 and pageno = fromstring int_of_string spos "page" spage 0
6723 and rely = fromstring float_of_string spos "rely" srely 0.0
6724 and pan = fromstring int_of_string spos "pan" span 0
6725 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6726 let c = config_of dc attrs in
6727 let anchor = (pageno, rely, visy) in
6728 if closed
6729 then (Hashtbl.add h path (c, [], pan, anchor); v)
6730 else { v with f = doc path pan anchor c [] }
6732 | Vopen _ ->
6733 error "unexpected subelement in llppconfig" s spos
6735 | Vclose "llppconfig" -> { v with f = toplevel }
6736 | Vclose _ -> error "unexpected close in llppconfig" s spos
6738 and defaults v t spos _ =
6739 match t with
6740 | Vdata | Vcdata -> v
6741 | Vend -> error "unexpected end of input in defaults" s spos
6742 | Vopen ("keymap", attrs, closed) ->
6743 let modename =
6744 try List.assoc "mode" attrs
6745 with Not_found -> "global" in
6746 if closed
6747 then v
6748 else
6749 let ret keymap =
6750 let h = findkeyhash dc modename in
6751 KeyMap.iter (Hashtbl.replace h) keymap;
6752 defaults
6754 { v with f = pkeymap ret KeyMap.empty }
6756 | Vopen (_, _, _) ->
6757 error "unexpected subelement in defaults" s spos
6759 | Vclose "defaults" ->
6760 { v with f = llppconfig }
6762 | Vclose _ -> error "unexpected close in defaults" s spos
6764 and uifont b v t spos epos =
6765 match t with
6766 | Vdata | Vcdata ->
6767 Buffer.add_substring b s spos (epos - spos);
6769 | Vopen (_, _, _) ->
6770 error "unexpected subelement in ui-font" s spos
6771 | Vclose "ui-font" ->
6772 if String.length !fontpath = 0
6773 then fontpath := Buffer.contents b;
6774 { v with f = llppconfig }
6775 | Vclose _ -> error "unexpected close in ui-font" s spos
6776 | Vend -> error "unexpected end of input in ui-font" s spos
6778 and doc path pan anchor c bookmarks v t spos _ =
6779 match t with
6780 | Vdata | Vcdata -> v
6781 | Vend -> error "unexpected end of input in doc" s spos
6782 | Vopen ("bookmarks", _, closed) ->
6783 if closed
6784 then v
6785 else { v with f = pbookmarks path pan anchor c bookmarks }
6787 | Vopen ("keymap", attrs, closed) ->
6788 let modename =
6789 try List.assoc "mode" attrs
6790 with Not_found -> "global"
6792 if closed
6793 then v
6794 else
6795 let ret keymap =
6796 let h = findkeyhash c modename in
6797 KeyMap.iter (Hashtbl.replace h) keymap;
6798 doc path pan anchor c bookmarks
6800 { v with f = pkeymap ret KeyMap.empty }
6802 | Vopen (_, _, _) ->
6803 error "unexpected subelement in doc" s spos
6805 | Vclose "doc" ->
6806 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6807 { v with f = llppconfig }
6809 | Vclose _ -> error "unexpected close in doc" s spos
6811 and pkeymap ret keymap v t spos _ =
6812 match t with
6813 | Vdata | Vcdata -> v
6814 | Vend -> error "unexpected end of input in keymap" s spos
6815 | Vopen ("map", attrs, closed) ->
6816 let r, l = map_of attrs in
6817 let kss = fromstring keys_of_string spos "in" r [] in
6818 let lss = fromstring keys_of_string spos "out" l [] in
6819 let keymap =
6820 match kss with
6821 | [] -> keymap
6822 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6823 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6825 if closed
6826 then { v with f = pkeymap ret keymap }
6827 else
6828 let f () = v in
6829 { v with f = skip "map" f }
6831 | Vopen _ ->
6832 error "unexpected subelement in keymap" s spos
6834 | Vclose "keymap" ->
6835 { v with f = ret keymap }
6837 | Vclose _ -> error "unexpected close in keymap" s spos
6839 and pbookmarks path pan anchor c bookmarks v t spos _ =
6840 match t with
6841 | Vdata | Vcdata -> v
6842 | Vend -> error "unexpected end of input in bookmarks" s spos
6843 | Vopen ("item", attrs, closed) ->
6844 let titleent, spage, srely, svisy = bookmark_of attrs in
6845 let page = fromstring int_of_string spos "page" spage 0
6846 and rely = fromstring float_of_string spos "rely" srely 0.0
6847 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6848 let bookmarks =
6849 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6851 if closed
6852 then { v with f = pbookmarks path pan anchor c bookmarks }
6853 else
6854 let f () = v in
6855 { v with f = skip "item" f }
6857 | Vopen _ ->
6858 error "unexpected subelement in bookmarks" s spos
6860 | Vclose "bookmarks" ->
6861 { v with f = doc path pan anchor c bookmarks }
6863 | Vclose _ -> error "unexpected close in bookmarks" s spos
6865 and skip tag f v t spos _ =
6866 match t with
6867 | Vdata | Vcdata -> v
6868 | Vend ->
6869 error ("unexpected end of input in skipped " ^ tag) s spos
6870 | Vopen (tag', _, closed) ->
6871 if closed
6872 then v
6873 else
6874 let f' () = { v with f = skip tag f } in
6875 { v with f = skip tag' f' }
6876 | Vclose ctag ->
6877 if tag = ctag
6878 then f ()
6879 else error ("unexpected close in skipped " ^ tag) s spos
6882 parse { f = toplevel; accu = () } s;
6883 h, dc;
6886 let do_load f ic =
6888 let len = in_channel_length ic in
6889 let s = String.create len in
6890 really_input ic s 0 len;
6891 f s;
6892 with
6893 | Parse_error (msg, s, pos) ->
6894 let subs = subs s pos in
6895 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6896 failwith ("parse error: " ^ s)
6898 | exn ->
6899 failwith ("config load error: " ^ exntos exn)
6902 let defconfpath =
6903 let dir =
6905 let dir = Filename.concat home ".config" in
6906 if Sys.is_directory dir then dir else home
6907 with _ -> home
6909 Filename.concat dir "llpp.conf"
6912 let confpath = ref defconfpath;;
6914 let load1 f =
6915 if Sys.file_exists !confpath
6916 then
6917 match
6918 (try Some (open_in_bin !confpath)
6919 with exn ->
6920 prerr_endline
6921 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6922 exntos exn);
6923 None
6925 with
6926 | Some ic ->
6927 let success =
6929 f (do_load get ic)
6930 with exn ->
6931 prerr_endline
6932 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6933 exntos exn);
6934 false
6936 close_in ic;
6937 success
6939 | None -> false
6940 else
6941 f (Hashtbl.create 0, defconf)
6944 let load () =
6945 let f (h, dc) =
6946 let pc, pb, px, pa =
6948 let key =
6949 if String.length state.origin = 0
6950 then state.path
6951 else state.origin
6953 Hashtbl.find h (Filename.basename key)
6954 with Not_found -> dc, [], 0, emptyanchor
6956 setconf defconf dc;
6957 setconf conf pc;
6958 state.bookmarks <- pb;
6959 state.x <- px;
6960 if conf.jumpback
6961 then state.anchor <- pa;
6962 cbput state.hists.nav pa;
6963 true
6965 load1 f
6968 let add_attrs bb always dc c =
6969 let ob s a b =
6970 if always || a != b
6971 then Printf.bprintf bb "\n %s='%b'" s a
6972 and op s a b =
6973 if always || a <> b
6974 then Printf.bprintf bb "\n %s='%b'" s (a != None)
6975 and oi s a b =
6976 if always || a != b
6977 then Printf.bprintf bb "\n %s='%d'" s a
6978 and oI s a b =
6979 if always || a != b
6980 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6981 and oz s a b =
6982 if always || a <> b
6983 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6984 and oF s a b =
6985 if always || a <> b
6986 then Printf.bprintf bb "\n %s='%f'" s a
6987 and oc s a b =
6988 if always || a <> b
6989 then
6990 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6991 and oC s a b =
6992 if always || a <> b
6993 then
6994 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
6995 and oR s a b =
6996 if always || a <> b
6997 then
6998 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6999 and os s a b =
7000 if always || a <> b
7001 then
7002 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7003 and og s a b =
7004 if always || a <> b
7005 then
7006 match a with
7007 | None -> ()
7008 | Some (_N, _A, _B) ->
7009 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7010 and oW s a b =
7011 if always || a <> b
7012 then
7013 let v =
7014 match a with
7015 | None -> "false"
7016 | Some f ->
7017 if f = infinity
7018 then "true"
7019 else string_of_float f
7021 Printf.bprintf bb "\n %s='%s'" s v
7022 and oco s a b =
7023 if always || a <> b
7024 then
7025 match a with
7026 | Cmulti ((n, a, b), _) when n > 1 ->
7027 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7028 | Csplit (n, _) when n > 1 ->
7029 Printf.bprintf bb "\n %s='%d'" s ~-n
7030 | _ -> ()
7031 and obeco s a b =
7032 if always || a <> b
7033 then
7034 match a with
7035 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7036 | _ -> ()
7037 and oFm s a b =
7038 if always || a <> b
7039 then
7040 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7041 and oSv s a b m =
7042 if always || a <> b
7043 then
7044 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7046 oi "width" c.cwinw dc.cwinw;
7047 oi "height" c.cwinh dc.cwinh;
7048 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7049 oi "scroll-handle-height" c.scrollh dc.scrollh;
7050 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7051 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7052 ob "case-insensitive-search" c.icase dc.icase;
7053 ob "preload" c.preload dc.preload;
7054 oi "page-bias" c.pagebias dc.pagebias;
7055 oi "scroll-step" c.scrollstep dc.scrollstep;
7056 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7057 ob "max-height-fit" c.maxhfit dc.maxhfit;
7058 ob "crop-hack" c.crophack dc.crophack;
7059 oW "throttle" c.maxwait dc.maxwait;
7060 ob "highlight-links" c.hlinks dc.hlinks;
7061 ob "under-cursor-info" c.underinfo dc.underinfo;
7062 oi "vertical-margin" c.interpagespace dc.interpagespace;
7063 oz "zoom" c.zoom dc.zoom;
7064 ob "presentation" c.presentation dc.presentation;
7065 oi "rotation-angle" c.angle dc.angle;
7066 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7067 oFm "fit-model" c.fitmodel dc.fitmodel;
7068 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7069 oi "tex-count" c.texcount dc.texcount;
7070 oi "slice-height" c.sliceheight dc.sliceheight;
7071 oi "thumbnail-width" c.thumbw dc.thumbw;
7072 ob "persistent-location" c.jumpback dc.jumpback;
7073 oc "background-color" c.bgcolor dc.bgcolor;
7074 oi "tile-width" c.tilew dc.tilew;
7075 oi "tile-height" c.tileh dc.tileh;
7076 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7077 ob "checkers" c.checkers dc.checkers;
7078 oi "aalevel" c.aalevel dc.aalevel;
7079 ob "trim-margins" c.trimmargins dc.trimmargins;
7080 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7081 os "uri-launcher" c.urilauncher dc.urilauncher;
7082 os "path-launcher" c.pathlauncher dc.pathlauncher;
7083 oC "color-space" c.colorspace dc.colorspace;
7084 ob "invert-colors" c.invert dc.invert;
7085 oF "brightness" c.colorscale dc.colorscale;
7086 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7087 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7088 oco "columns" c.columns dc.columns;
7089 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7090 os "selection-command" c.selcmd dc.selcmd;
7091 os "synctex-command" c.stcmd dc.stcmd;
7092 os "pax-command" c.paxcmd dc.paxcmd;
7093 ob "update-cursor" c.updatecurs dc.updatecurs;
7094 oi "hint-font-size" c.hfsize dc.hfsize;
7095 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7096 oF "page-scroll-scale" c.pgscale dc.pgscale;
7097 ob "use-pbo" c.usepbo dc.usepbo;
7098 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7099 ob "remote-in-a-new-instance" c.riani dc.riani;
7100 op "point-and-x" c.pax dc.pax;
7103 let keymapsbuf always dc c =
7104 let bb = Buffer.create 16 in
7105 let rec loop = function
7106 | [] -> ()
7107 | (modename, h) :: rest ->
7108 let dh = findkeyhash dc modename in
7109 if always || h <> dh
7110 then (
7111 if Hashtbl.length h > 0
7112 then (
7113 if Buffer.length bb > 0
7114 then Buffer.add_char bb '\n';
7115 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7116 Hashtbl.iter (fun i o ->
7117 let isdifferent = always ||
7119 let dO = Hashtbl.find dh i in
7120 dO <> o
7121 with Not_found -> true
7123 if isdifferent
7124 then
7125 let addkm (k, m) =
7126 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7127 if Wsi.withalt m then Buffer.add_string bb "alt-";
7128 if Wsi.withshift m then Buffer.add_string bb "shift-";
7129 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7130 Buffer.add_string bb (Wsi.keyname k);
7132 let addkms l =
7133 let rec loop = function
7134 | [] -> ()
7135 | km :: [] -> addkm km
7136 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7138 loop l
7140 Buffer.add_string bb "<map in='";
7141 addkm i;
7142 match o with
7143 | KMinsrt km ->
7144 Buffer.add_string bb "' out='";
7145 addkm km;
7146 Buffer.add_string bb "'/>\n"
7148 | KMinsrl kms ->
7149 Buffer.add_string bb "' out='";
7150 addkms kms;
7151 Buffer.add_string bb "'/>\n"
7153 | KMmulti (ins, kms) ->
7154 Buffer.add_char bb ' ';
7155 addkms ins;
7156 Buffer.add_string bb "' out='";
7157 addkms kms;
7158 Buffer.add_string bb "'/>\n"
7159 ) h;
7160 Buffer.add_string bb "</keymap>";
7163 loop rest
7165 loop c.keyhashes;
7169 let save () =
7170 let uifontsize = fstate.fontsize in
7171 let bb = Buffer.create 32768 in
7172 let relx = float state.x /. float state.winw in
7173 let w, h, x =
7174 let cx w = truncate (relx *. float w) in
7175 List.fold_left
7176 (fun (w, h, x) ws ->
7177 match ws with
7178 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7179 | Wsi.MaxVert -> (w, conf.cwinh, x)
7180 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7182 (state.winw, state.winh, state.x) state.winstate
7184 conf.cwinw <- w;
7185 conf.cwinh <- h;
7186 let f (h, dc) =
7187 let dc = if conf.bedefault then conf else dc in
7188 Buffer.add_string bb "<llppconfig>\n";
7190 if String.length !fontpath > 0
7191 then
7192 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7193 uifontsize
7194 !fontpath
7195 else (
7196 if uifontsize <> 14
7197 then
7198 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7201 Buffer.add_string bb "<defaults ";
7202 add_attrs bb true dc dc;
7203 let kb = keymapsbuf true dc dc in
7204 if Buffer.length kb > 0
7205 then (
7206 Buffer.add_string bb ">\n";
7207 Buffer.add_buffer bb kb;
7208 Buffer.add_string bb "\n</defaults>\n";
7210 else Buffer.add_string bb "/>\n";
7212 let adddoc path pan anchor c bookmarks =
7213 if bookmarks == [] && c = dc && anchor = emptyanchor
7214 then ()
7215 else (
7216 Printf.bprintf bb "<doc path='%s'"
7217 (enent path 0 (String.length path));
7219 if anchor <> emptyanchor
7220 then (
7221 let n, rely, visy = anchor in
7222 Printf.bprintf bb " page='%d'" n;
7223 if rely > 1e-6
7224 then
7225 Printf.bprintf bb " rely='%f'" rely
7227 if abs_float visy > 1e-6
7228 then
7229 Printf.bprintf bb " visy='%f'" visy
7233 if pan != 0
7234 then Printf.bprintf bb " pan='%d'" pan;
7236 add_attrs bb false dc c;
7237 let kb = keymapsbuf false dc c in
7239 begin match bookmarks with
7240 | [] ->
7241 if Buffer.length kb > 0
7242 then (
7243 Buffer.add_string bb ">\n";
7244 Buffer.add_buffer bb kb;
7245 Buffer.add_string bb "\n</doc>\n";
7247 else Buffer.add_string bb "/>\n"
7248 | _ ->
7249 Buffer.add_string bb ">\n<bookmarks>\n";
7250 List.iter (fun (title, _level, (page, rely, visy)) ->
7251 Printf.bprintf bb
7252 "<item title='%s' page='%d'"
7253 (enent title 0 (String.length title))
7254 page
7256 if rely > 1e-6
7257 then
7258 Printf.bprintf bb " rely='%f'" rely
7260 if abs_float visy > 1e-6
7261 then
7262 Printf.bprintf bb " visy='%f'" visy
7264 Buffer.add_string bb "/>\n";
7265 ) bookmarks;
7266 Buffer.add_string bb "</bookmarks>";
7267 if Buffer.length kb > 0
7268 then (
7269 Buffer.add_string bb "\n";
7270 Buffer.add_buffer bb kb;
7272 Buffer.add_string bb "\n</doc>\n";
7273 end;
7277 let pan, conf =
7278 match state.mode with
7279 | Birdseye (c, pan, _, _, _) ->
7280 let beyecolumns =
7281 match conf.columns with
7282 | Cmulti ((c, _, _), _) -> Some c
7283 | Csingle _ -> None
7284 | Csplit _ -> None
7285 and columns =
7286 match c.columns with
7287 | Cmulti (c, _) -> Cmulti (c, [||])
7288 | Csingle _ -> Csingle [||]
7289 | Csplit _ -> failwith "quit from bird's eye while split"
7291 pan, { c with beyecolumns = beyecolumns; columns = columns }
7292 | _ -> x, conf
7294 let basename = Filename.basename
7295 (if String.length state.origin = 0 then state.path else state.origin)
7297 adddoc basename pan (getanchor ())
7298 (let conf =
7299 let autoscrollstep =
7300 match state.autoscroll with
7301 | Some step -> step
7302 | None -> conf.autoscrollstep
7304 match state.mode with
7305 | Birdseye (bc, _, _, _, _) ->
7306 { conf with
7307 zoom = bc.zoom;
7308 presentation = bc.presentation;
7309 interpagespace = bc.interpagespace;
7310 maxwait = bc.maxwait;
7311 autoscrollstep = autoscrollstep }
7312 | _ -> { conf with autoscrollstep = autoscrollstep }
7313 in conf)
7314 (if conf.savebmarks then state.bookmarks else []);
7316 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7317 if basename <> path
7318 then adddoc path x anchor c bookmarks
7319 ) h;
7320 Buffer.add_string bb "</llppconfig>\n";
7321 true;
7323 if load1 f && Buffer.length bb > 0
7324 then
7326 let tmp = !confpath ^ ".tmp" in
7327 let oc = open_out_bin tmp in
7328 Buffer.output_buffer oc bb;
7329 close_out oc;
7330 Unix.rename tmp !confpath;
7331 with exn ->
7332 prerr_endline
7333 ("error while saving configuration: " ^ exntos exn)
7335 end;;
7337 let adderrmsg src msg =
7338 Buffer.add_string state.errmsgs msg;
7339 state.newerrmsgs <- true;
7340 G.postRedisplay src
7343 let adderrfmt src fmt =
7344 Format.kprintf (fun s -> adderrmsg src s) fmt;
7347 let ract cmds =
7348 let cl = splitatspace cmds in
7349 let scan s fmt f =
7350 try Scanf.sscanf s fmt f
7351 with exn ->
7352 adderrfmt "remote exec"
7353 "error processing '%S': %s\n" cmds (exntos exn)
7355 match cl with
7356 | "reload" :: [] -> reload ()
7357 | "goto" :: args :: [] ->
7358 scan args "%u %f %f"
7359 (fun pageno x y ->
7360 let cmd, _ = state.geomcmds in
7361 if String.length cmd = 0
7362 then gotopagexy pageno x y
7363 else
7364 let f prevf () =
7365 gotopagexy pageno x y;
7366 prevf ()
7368 state.reprf <- f state.reprf
7370 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7371 | "rect" :: args :: [] ->
7372 scan args "%u %u %f %f %f %f"
7373 (fun pageno color x0 y0 x1 y1 ->
7374 onpagerect pageno (fun w h ->
7375 let _,w1,h1,_ = getpagedim pageno in
7376 let sw = float w1 /. w
7377 and sh = float h1 /. h in
7378 let x0s = x0 *. sw
7379 and x1s = x1 *. sw
7380 and y0s = y0 *. sh
7381 and y1s = y1 *. sh in
7382 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7383 debugrect rect;
7384 state.rects <- (pageno, color, rect) :: state.rects;
7385 G.postRedisplay "rect";
7388 | "activatewin" :: [] -> Wsi.activatewin ()
7389 | "quit" :: [] -> raise Quit
7390 | _ ->
7391 adderrfmt "remote command"
7392 "error processing remote command: %S\n" cmds;
7395 let remote =
7396 let scratch = String.create 80 in
7397 let buf = Buffer.create 80 in
7398 fun fd ->
7399 let rec tempfr () =
7400 try Some (Unix.read fd scratch 0 80)
7401 with
7402 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7403 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7404 | exn -> raise exn
7406 match tempfr () with
7407 | None -> Some fd
7408 | Some n ->
7409 if n = 0
7410 then (
7411 Unix.close fd;
7412 if Buffer.length buf > 0
7413 then (
7414 let s = Buffer.contents buf in
7415 Buffer.clear buf;
7416 ract s;
7418 None
7420 else
7421 let rec eat ppos =
7422 let nlpos =
7424 let pos = String.index_from scratch ppos '\n' in
7425 if pos >= n then -1 else pos
7426 with Not_found -> -1
7428 if nlpos >= 0
7429 then (
7430 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7431 let s = Buffer.contents buf in
7432 Buffer.clear buf;
7433 ract s;
7434 eat (nlpos+1);
7436 else (
7437 Buffer.add_substring buf scratch ppos (n-ppos);
7438 Some fd
7440 in eat 0
7443 let remoteopen path =
7444 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7445 with exn ->
7446 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7447 None
7450 let () =
7451 let trimcachepath = ref "" in
7452 let rcmdpath = ref "" in
7453 selfexec := Sys.executable_name;
7454 Arg.parse
7455 (Arg.align
7456 [("-p", Arg.String (fun s -> state.password <- s),
7457 "<password> Set password");
7459 ("-f", Arg.String
7460 (fun s ->
7461 Config.fontpath := s;
7462 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7464 "<path> Set path to the user interface font");
7466 ("-c", Arg.String
7467 (fun s ->
7468 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7469 Config.confpath := s),
7470 "<path> Set path to the configuration file");
7472 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7473 "<path> Set path to the trim cache file");
7475 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7476 "<named-destination> Set named destination");
7478 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7480 ("-remote", Arg.String (fun s -> rcmdpath := s),
7481 "<path> Set path to the remote commands source");
7483 ("-origin", Arg.String (fun s -> state.origin <- s),
7484 "<original-path> Set original path");
7486 ("-v", Arg.Unit (fun () ->
7487 Printf.printf
7488 "%s\nconfiguration path: %s\n"
7489 (version ())
7490 Config.defconfpath
7492 exit 0), " Print version and exit");
7495 (fun s -> state.path <- s)
7496 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7498 if !wtmode
7499 then selfexec := !selfexec ^ " -wtmode";
7501 if String.length state.path = 0
7502 then (prerr_endline "file name missing"; exit 1);
7504 if not (Config.load ())
7505 then prerr_endline "failed to load configuration";
7507 let wsfd, winw, winh = Wsi.init (object
7508 val mutable m_hack = false
7509 method expose = if not m_hack then G.postRedisplay "expose"
7510 method visible = G.postRedisplay "visible"
7511 method display = m_hack <- false; display ()
7512 method reshape w h =
7513 m_hack <- w < state.winw && h < state.winh;
7514 reshape w h
7515 method mouse b d x y m = mouse b d x y m
7516 method motion x y = state.mpos <- (x, y); motion x y
7517 method pmotion x y = state.mpos <- (x, y); pmotion x y
7518 method key k m =
7519 let mascm = m land (
7520 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7521 ) in
7522 match state.keystate with
7523 | KSnone ->
7524 let km = k, mascm in
7525 begin
7526 match
7527 let modehash = state.uioh#modehash in
7528 try Hashtbl.find modehash km
7529 with Not_found ->
7530 try Hashtbl.find (findkeyhash conf "global") km
7531 with Not_found -> KMinsrt (k, m)
7532 with
7533 | KMinsrt (k, m) -> keyboard k m
7534 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7535 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7537 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7538 List.iter (fun (k, m) -> keyboard k m) insrt;
7539 state.keystate <- KSnone
7540 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7541 state.keystate <- KSinto (keys, insrt)
7542 | _ ->
7543 state.keystate <- KSnone
7545 method enter x y = state.mpos <- (x, y); pmotion x y
7546 method leave = state.mpos <- (-1, -1)
7547 method winstate wsl = state.winstate <- wsl
7548 method quit = raise Quit
7549 end) conf.cwinw conf.cwinh (platform = Posx) in
7551 state.wsfd <- wsfd;
7553 if not (
7554 List.exists GlMisc.check_extension
7555 [ "GL_ARB_texture_rectangle"
7556 ; "GL_EXT_texture_recangle"
7557 ; "GL_NV_texture_rectangle" ]
7559 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7561 if (
7562 let r = GlMisc.get_string `renderer in
7563 let p = "Mesa DRI Intel(" in
7564 let l = String.length p in
7565 String.length r > l && String.sub r 0 l = p
7567 then defconf.sliceheight <- 1024;
7569 let cr, sw =
7570 match Ne.pipe () with
7571 | Ne.Exn exn ->
7572 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7573 exit 1
7574 | Ne.Res rw -> rw
7575 and sr, cw =
7576 match Ne.pipe () with
7577 | Ne.Exn exn ->
7578 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7579 exit 1
7580 | Ne.Res rw -> rw
7583 cloexec cr;
7584 cloexec sw;
7585 cloexec sr;
7586 cloexec cw;
7588 setcheckers conf.checkers;
7589 redirectstderr ();
7591 init (cr, cw) (
7592 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7593 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7594 !Config.fontpath, !trimcachepath,
7595 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7597 state.sr <- sr;
7598 state.sw <- sw;
7599 state.text <- "Opening " ^ (mbtoutf8 state.path);
7600 reshape winw winh;
7601 opendoc state.path state.password;
7602 state.uioh <- uioh;
7603 display ();
7604 Wsi.mapwin ();
7605 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7606 let optrfd =
7607 ref (
7608 if String.length !rcmdpath > 0
7609 then remoteopen !rcmdpath
7610 else None
7614 let rec loop deadline =
7615 let r =
7616 match state.errfd with
7617 | None -> [state.sr; state.wsfd]
7618 | Some fd -> [state.sr; state.wsfd; fd]
7620 let r =
7621 match !optrfd with
7622 | None -> r
7623 | Some fd -> fd :: r
7625 if state.redisplay
7626 then (
7627 state.redisplay <- false;
7628 display ();
7630 let timeout =
7631 let now = now () in
7632 if deadline > now
7633 then (
7634 if deadline = infinity
7635 then ~-.1.0
7636 else max 0.0 (deadline -. now)
7638 else 0.0
7640 let r, _, _ =
7641 try Unix.select r [] [] timeout
7642 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7644 begin match r with
7645 | [] ->
7646 state.ghyll None;
7647 let newdeadline =
7648 if state.ghyll == noghyll
7649 then
7650 match state.autoscroll with
7651 | Some step when step != 0 ->
7652 let y = state.y + step in
7653 let y =
7654 if y < 0
7655 then state.maxy
7656 else if y >= state.maxy then 0 else y
7658 gotoy y;
7659 if state.mode = View
7660 then state.text <- "";
7661 deadline +. 0.01
7662 | _ -> infinity
7663 else deadline +. 0.01
7665 loop newdeadline
7667 | l ->
7668 let rec checkfds = function
7669 | [] -> ()
7670 | fd :: rest when fd = state.sr ->
7671 let cmd = readcmd state.sr in
7672 act cmd;
7673 checkfds rest
7675 | fd :: rest when fd = state.wsfd ->
7676 Wsi.readresp fd;
7677 checkfds rest
7679 | fd :: rest when Some fd = !optrfd ->
7680 begin match remote fd with
7681 | None -> optrfd := remoteopen !rcmdpath;
7682 | opt -> optrfd := opt
7683 end;
7684 checkfds rest
7686 | fd :: rest ->
7687 let s = String.create 80 in
7688 let n = tempfailureretry (Unix.read fd s 0) 80 in
7689 if conf.redirectstderr
7690 then (
7691 Buffer.add_substring state.errmsgs s 0 n;
7692 state.newerrmsgs <- true;
7693 state.redisplay <- true;
7695 else (
7696 prerr_string (String.sub s 0 n);
7697 flush stderr;
7699 checkfds rest
7701 checkfds l;
7702 let newdeadline =
7703 let deadline1 =
7704 if deadline = infinity
7705 then now () +. 0.01
7706 else deadline
7708 match state.autoscroll with
7709 | Some step when step != 0 -> deadline1
7710 | _ -> if state.ghyll == noghyll then infinity else deadline1
7712 loop newdeadline
7713 end;
7716 loop infinity;
7717 with Quit ->
7718 Config.save ();