Block zoom
[llpp.git] / main.ml
blobd18f01f3abc673cb12b463b8577086d2e4da34ee
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";;
123 external rectofblock : opaque -> int -> int -> float array option
124 = "ml_rectofblock";;
126 let platform_to_string = function
127 | Punknown -> "unknown"
128 | Plinux -> "Linux"
129 | Posx -> "OSX"
130 | Psun -> "Sun"
131 | Pfreebsd -> "FreeBSD"
132 | Pdragonflybsd -> "DragonflyBSD"
133 | Popenbsd -> "OpenBSD"
134 | Pnetbsd -> "NetBSD"
135 | Pcygwin -> "Cygwin"
138 let platform = platform ();;
140 let now = Unix.gettimeofday;;
142 let selfexec = ref "";;
144 let popen cmd fda =
145 if platform = Pcygwin
146 then (
147 let sh = "/bin/sh" in
148 let args = [|sh; "-c"; cmd|] in
149 let rec std si so se = function
150 | [] -> si, so, se
151 | (fd, 0) :: rest -> std fd so se rest
152 | (fd, -1) :: rest ->
153 Unix.set_close_on_exec fd;
154 std si so se rest
155 | (_, n) :: _ ->
156 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
158 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
159 ignore (Unix.create_process sh args si so se)
161 else popen cmd fda;
164 type mpos = int * int
165 and mstate =
166 | Msel of (mpos * mpos)
167 | Mpan of mpos
168 | Mscrolly | Mscrollx
169 | Mzoom of (int * int)
170 | Mzoomrect of (mpos * mpos)
171 | Mnone
174 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
175 and onkey = string -> int -> te
176 and ondone = string -> unit
177 and histcancel = unit -> unit
178 and onhist = ((histcmd -> string) * histcancel)
179 and histcmd = HCnext | HCprev | HCfirst | HClast
180 and cancelonempty = bool
181 and te =
182 | TEstop
183 | TEdone of string
184 | TEcont of string
185 | TEswitch of textentry
188 type 'a circbuf =
189 { store : 'a array
190 ; mutable rc : int
191 ; mutable wc : int
192 ; mutable len : int
196 let bound v minv maxv =
197 max minv (min maxv v);
200 let cbnew n v =
201 { store = Array.create n v
202 ; rc = 0
203 ; wc = 0
204 ; len = 0
208 let cbcap b = Array.length b.store;;
210 let cbput b v =
211 let cap = cbcap b in
212 b.store.(b.wc) <- v;
213 b.wc <- (b.wc + 1) mod cap;
214 b.rc <- b.wc;
215 b.len <- min (b.len + 1) cap;
218 let cbempty b = b.len = 0;;
220 let cbgetg b circular dir =
221 if cbempty b
222 then b.store.(0)
223 else
224 let rc = b.rc + dir in
225 let rc =
226 if circular
227 then (
228 if rc = -1
229 then b.len-1
230 else (
231 if rc >= b.len
232 then 0
233 else rc
236 else bound rc 0 (b.len-1)
238 b.rc <- rc;
239 b.store.(rc);
242 let cbget b = cbgetg b false;;
243 let cbgetc b = cbgetg b true;;
245 let drawstring size x y s =
246 Gl.enable `blend;
247 Gl.enable `texture_2d;
248 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
249 ignore (drawstr size x y s);
250 Gl.disable `blend;
251 Gl.disable `texture_2d;
254 let drawstring1 size x y s =
255 drawstr size x y s;
258 let drawstring2 size x y fmt =
259 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
262 type page =
263 { pageno : int
264 ; pagedimno : int
265 ; pagew : int
266 ; pageh : int
267 ; pagex : int
268 ; pagey : int
269 ; pagevw : int
270 ; pagevh : int
271 ; pagedispx : int
272 ; pagedispy : int
273 ; pagecol : int
277 let debugl l =
278 dolog "l %d dim=%d {" l.pageno l.pagedimno;
279 dolog " WxH %dx%d" l.pagew l.pageh;
280 dolog " vWxH %dx%d" l.pagevw l.pagevh;
281 dolog " pagex,y %d,%d" l.pagex l.pagey;
282 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
283 dolog " column %d" l.pagecol;
284 dolog "}";
287 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
288 dolog "rect {";
289 dolog " x0,y0=(% f, % f)" x0 y0;
290 dolog " x1,y1=(% f, % f)" x1 y1;
291 dolog " x2,y2=(% f, % f)" x2 y2;
292 dolog " x3,y3=(% f, % f)" x3 y3;
293 dolog "}";
296 type multicolumns = multicol * pagegeom
297 and singlecolumn = pagegeom
298 and splitcolumns = columncount * pagegeom
299 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
300 and multicol = columncount * covercount * covercount
301 and pdimno = int
302 and columncount = int
303 and covercount = int;;
305 type scrollb = int;;
306 let scrollbvv = 1;;
307 let scrollbhv = 2;;
309 type conf =
310 { mutable scrollbw : int
311 ; mutable scrollh : int
312 ; mutable scrollb : scrollb
313 ; mutable icase : bool
314 ; mutable preload : bool
315 ; mutable pagebias : int
316 ; mutable verbose : bool
317 ; mutable debug : bool
318 ; mutable scrollstep : int
319 ; mutable hscrollstep : int
320 ; mutable maxhfit : bool
321 ; mutable crophack : bool
322 ; mutable autoscrollstep : int
323 ; mutable maxwait : float option
324 ; mutable hlinks : bool
325 ; mutable underinfo : bool
326 ; mutable interpagespace : interpagespace
327 ; mutable zoom : float
328 ; mutable presentation : bool
329 ; mutable angle : angle
330 ; mutable cwinw : int
331 ; mutable cwinh : int
332 ; mutable savebmarks : bool
333 ; mutable fitmodel : fitmodel
334 ; mutable trimmargins : trimmargins
335 ; mutable trimfuzz : irect
336 ; mutable memlimit : memsize
337 ; mutable texcount : texcount
338 ; mutable sliceheight : sliceheight
339 ; mutable thumbw : width
340 ; mutable jumpback : bool
341 ; mutable bgcolor : (float * float * float)
342 ; mutable bedefault : bool
343 ; mutable tilew : int
344 ; mutable tileh : int
345 ; mutable mustoresize : memsize
346 ; mutable checkers : bool
347 ; mutable aalevel : int
348 ; mutable urilauncher : string
349 ; mutable pathlauncher : string
350 ; mutable colorspace : colorspace
351 ; mutable invert : bool
352 ; mutable colorscale : float
353 ; mutable redirectstderr : bool
354 ; mutable ghyllscroll : (int * int * int) option
355 ; mutable columns : columns
356 ; mutable beyecolumns : columncount option
357 ; mutable selcmd : string
358 ; mutable paxcmd : string
359 ; mutable updatecurs : bool
360 ; mutable keyhashes : (string * keyhash) list
361 ; mutable hfsize : int
362 ; mutable pgscale : float
363 ; mutable usepbo : bool
364 ; mutable wheelbypage : bool
365 ; mutable stcmd : string
366 ; mutable riani : bool
367 ; mutable pax : (float * int * int) ref option
368 ; mutable paxmark : mark
370 and columns =
371 | Csingle of singlecolumn
372 | Cmulti of multicolumns
373 | Csplit of splitcolumns
376 type anchor = pageno * top * dtop;;
378 type outline = string * int * anchor;;
380 type rect = float * float * float * float * float * float * float * float;;
382 type tile = opaque * pixmapsize * elapsed
383 and elapsed = float;;
384 type pagemapkey = pageno * gen;;
385 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
386 and row = int
387 and col = int;;
389 let emptyanchor = (0, 0.0, 0.0);;
391 type infochange = | Memused | Docinfo | Pdim;;
393 class type uioh = object
394 method display : unit
395 method key : int -> int -> uioh
396 method button : int -> bool -> int -> int -> int -> uioh
397 method motion : int -> int -> uioh
398 method pmotion : int -> int -> uioh
399 method infochanged : infochange -> unit
400 method scrollpw : (int * float * float)
401 method scrollph : (int * float * float)
402 method modehash : keyhash
403 method eformsgs : bool
404 end;;
406 type mode =
407 | Birdseye of (conf * leftx * pageno * pageno * anchor)
408 | Textentry of (textentry * onleave)
409 | View
410 | LinkNav of linktarget
411 and onleave = leavetextentrystatus -> unit
412 and leavetextentrystatus = | Cancel | Confirm
413 and helpitem = string * int * action
414 and action =
415 | Noaction
416 | Action of (uioh -> uioh)
417 and linktarget =
418 | Ltexact of (pageno * int)
419 | Ltgendir of int
422 let isbirdseye = function Birdseye _ -> true | _ -> false;;
423 let istextentry = function Textentry _ -> true | _ -> false;;
425 type currently =
426 | Idle
427 | Loading of (page * gen)
428 | Tiling of (
429 page * opaque * colorspace * angle * gen * col * row * width * height
431 | Outlining of outline list
434 let emptykeyhash = Hashtbl.create 0;;
435 let nouioh : uioh = object (self)
436 method display = ()
437 method key _ _ = self
438 method button _ _ _ _ _ = self
439 method motion _ _ = self
440 method pmotion _ _ = self
441 method infochanged _ = ()
442 method scrollpw = (0, nan, nan)
443 method scrollph = (0, nan, nan)
444 method modehash = emptykeyhash
445 method eformsgs = false
446 end;;
448 type state =
449 { mutable sr : Unix.file_descr
450 ; mutable sw : Unix.file_descr
451 ; mutable wsfd : Unix.file_descr
452 ; mutable errfd : Unix.file_descr option
453 ; mutable stderr : Unix.file_descr
454 ; mutable errmsgs : Buffer.t
455 ; mutable newerrmsgs : bool
456 ; mutable w : int
457 ; mutable x : int
458 ; mutable y : int
459 ; mutable anchor : anchor
460 ; mutable ranchors : (string * string * anchor * string) list
461 ; mutable maxy : int
462 ; mutable layout : page list
463 ; pagemap : (pagemapkey, opaque) Hashtbl.t
464 ; tilemap : (tilemapkey, tile) Hashtbl.t
465 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
466 ; mutable pdims : (pageno * width * height * leftx) list
467 ; mutable pagecount : int
468 ; mutable currently : currently
469 ; mutable mstate : mstate
470 ; mutable searchpattern : string
471 ; mutable rects : (pageno * recttype * rect) list
472 ; mutable rects1 : (pageno * recttype * rect) list
473 ; mutable text : string
474 ; mutable winstate : Wsi.winstate list
475 ; mutable mode : mode
476 ; mutable uioh : uioh
477 ; mutable outlines : outline array
478 ; mutable bookmarks : outline list
479 ; mutable path : string
480 ; mutable password : string
481 ; mutable nameddest : string
482 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
483 ; mutable memused : memsize
484 ; mutable gen : gen
485 ; mutable throttle : (page list * int * float) option
486 ; mutable autoscroll : int option
487 ; mutable ghyll : (int option -> unit)
488 ; mutable help : helpitem array
489 ; mutable docinfo : (int * string) list
490 ; mutable texid : GlTex.texture_id option
491 ; hists : hists
492 ; mutable prevzoom : float
493 ; mutable progress : float
494 ; mutable redisplay : bool
495 ; mutable mpos : mpos
496 ; mutable keystate : keystate
497 ; mutable glinks : bool
498 ; mutable prevcolumns : (columns * float) option
499 ; mutable winw : int
500 ; mutable winh : int
501 ; mutable reprf : (unit -> unit)
502 ; mutable origin : string
503 ; mutable roam : (unit -> unit)
504 ; mutable bzoom : bool
506 and hists =
507 { pat : string circbuf
508 ; pag : string circbuf
509 ; nav : anchor circbuf
510 ; sel : string circbuf
514 let defconf =
515 { scrollbw = 7
516 ; scrollh = 12
517 ; scrollb = scrollbhv lor scrollbvv
518 ; icase = true
519 ; preload = true
520 ; pagebias = 0
521 ; verbose = false
522 ; debug = false
523 ; scrollstep = 24
524 ; hscrollstep = 24
525 ; maxhfit = true
526 ; crophack = false
527 ; autoscrollstep = 2
528 ; maxwait = None
529 ; hlinks = false
530 ; underinfo = false
531 ; interpagespace = 2
532 ; zoom = 1.0
533 ; presentation = false
534 ; angle = 0
535 ; cwinw = 900
536 ; cwinh = 900
537 ; savebmarks = true
538 ; fitmodel = FitProportional
539 ; trimmargins = false
540 ; trimfuzz = (0,0,0,0)
541 ; memlimit = 32 lsl 20
542 ; texcount = 256
543 ; sliceheight = 24
544 ; thumbw = 76
545 ; jumpback = true
546 ; bgcolor = (0.5, 0.5, 0.5)
547 ; bedefault = false
548 ; tilew = 2048
549 ; tileh = 2048
550 ; mustoresize = 256 lsl 20
551 ; checkers = true
552 ; aalevel = 8
553 ; urilauncher =
554 (match platform with
555 | Plinux | Pfreebsd | Pdragonflybsd
556 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
557 | Posx -> "open \"%s\""
558 | Pcygwin -> "cygstart \"%s\""
559 | Punknown -> "echo %s")
560 ; pathlauncher = "lp \"%s\""
561 ; selcmd =
562 (match platform with
563 | Plinux | Pfreebsd | Pdragonflybsd
564 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
565 | Posx -> "pbcopy"
566 | Pcygwin -> "wsel"
567 | Punknown -> "cat")
568 ; paxcmd = "cat"
569 ; colorspace = Rgb
570 ; invert = false
571 ; colorscale = 1.0
572 ; redirectstderr = false
573 ; ghyllscroll = None
574 ; columns = Csingle [||]
575 ; beyecolumns = None
576 ; updatecurs = false
577 ; hfsize = 12
578 ; pgscale = 1.0
579 ; usepbo = false
580 ; wheelbypage = false
581 ; stcmd = "echo SyncTex"
582 ; riani = false
583 ; pax = None
584 ; paxmark = Mark_word
585 ; keyhashes =
586 let mk n = (n, Hashtbl.create 1) in
587 [ mk "global"
588 ; mk "info"
589 ; mk "help"
590 ; mk "outline"
591 ; mk "listview"
592 ; mk "birdseye"
593 ; mk "textentry"
594 ; mk "links"
595 ; mk "view"
600 let wtmode = ref false;;
601 let cxack = ref false;;
603 let findkeyhash c name =
604 try List.assoc name c.keyhashes
605 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
608 let conf = { defconf with angle = defconf.angle };;
610 let pgscale h = truncate (float h *. conf.pgscale);;
612 type fontstate =
613 { mutable fontsize : int
614 ; mutable wwidth : float
615 ; mutable maxrows : int
619 let fstate =
620 { fontsize = 14
621 ; wwidth = nan
622 ; maxrows = -1
626 let geturl s =
627 let colonpos = try String.index s ':' with Not_found -> -1 in
628 let len = String.length s in
629 if colonpos >= 0 && colonpos + 3 < len
630 then (
631 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
632 then
633 let schemestartpos =
634 try String.rindex_from s colonpos ' '
635 with Not_found -> -1
637 let scheme =
638 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
640 match scheme with
641 | "http" | "ftp" | "mailto" ->
642 let epos =
643 try String.index_from s colonpos ' '
644 with Not_found -> len
646 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
647 | _ -> ""
648 else ""
650 else ""
653 let gotouri uri =
654 if String.length conf.urilauncher = 0
655 then print_endline uri
656 else (
657 let url = geturl uri in
658 if String.length url = 0
659 then Printf.eprintf "obtained empty url from uri %S" uri
660 else
661 let re = Str.regexp "%s" in
662 let command = Str.global_replace re url conf.urilauncher in
663 try popen command []
664 with exn ->
665 Printf.eprintf
666 "failed to execute `%s': %s\n" command (exntos exn);
667 flush stderr;
671 let version () =
672 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
673 (platform_to_string platform) Sys.word_size Sys.ocaml_version
676 let makehelp () =
677 let strings = version () :: "" :: Help.keys in
678 Array.of_list (
679 List.map (fun s ->
680 let url = geturl s in
681 if String.length url > 0
682 then (s, 0, Action (fun u -> gotouri url; u))
683 else (s, 0, Noaction)
684 ) strings);
687 let noghyll _ = ();;
688 let firstgeomcmds = "", [];;
689 let noreprf () = ();;
691 let state =
692 { sr = Unix.stdin
693 ; sw = Unix.stdin
694 ; wsfd = Unix.stdin
695 ; errfd = None
696 ; stderr = Unix.stderr
697 ; errmsgs = Buffer.create 0
698 ; newerrmsgs = false
699 ; x = 0
700 ; y = 0
701 ; w = 0
702 ; anchor = emptyanchor
703 ; ranchors = []
704 ; layout = []
705 ; maxy = max_int
706 ; tilelru = Queue.create ()
707 ; pagemap = Hashtbl.create 10
708 ; tilemap = Hashtbl.create 10
709 ; pdims = []
710 ; pagecount = 0
711 ; currently = Idle
712 ; mstate = Mnone
713 ; rects = []
714 ; rects1 = []
715 ; text = ""
716 ; mode = View
717 ; winstate = []
718 ; searchpattern = ""
719 ; outlines = [||]
720 ; bookmarks = []
721 ; path = ""
722 ; password = ""
723 ; nameddest = ""
724 ; geomcmds = firstgeomcmds
725 ; hists =
726 { nav = cbnew 10 emptyanchor
727 ; pat = cbnew 10 ""
728 ; pag = cbnew 10 ""
729 ; sel = cbnew 10 ""
731 ; memused = 0
732 ; gen = 0
733 ; throttle = None
734 ; autoscroll = None
735 ; ghyll = noghyll
736 ; help = makehelp ()
737 ; docinfo = []
738 ; texid = None
739 ; prevzoom = 1.0
740 ; progress = -1.0
741 ; uioh = nouioh
742 ; redisplay = true
743 ; mpos = (-1, -1)
744 ; keystate = KSnone
745 ; glinks = false
746 ; prevcolumns = None
747 ; winw = -1
748 ; winh = -1
749 ; reprf = noreprf
750 ; origin = ""
751 ; roam = (fun () -> ())
752 ; bzoom = false
756 let hscrollh () =
757 if (conf.scrollb land scrollbhv = 0)
758 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
759 then 0
760 else conf.scrollbw
763 let vscrollw () =
764 if (conf.scrollb land scrollbvv = 0)
765 then 0
766 else conf.scrollbw
769 let wadjsb w = w - vscrollw ();;
771 let setfontsize n =
772 fstate.fontsize <- n;
773 fstate.wwidth <- measurestr fstate.fontsize "w";
774 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
777 let vlog fmt =
778 if conf.verbose
779 then
780 Printf.kprintf prerr_endline fmt
781 else
782 Printf.kprintf ignore fmt
785 let launchpath () =
786 if String.length conf.pathlauncher = 0
787 then print_endline state.path
788 else (
789 let re = Str.regexp "%s" in
790 let command = Str.global_replace re state.path conf.pathlauncher in
791 try popen command []
792 with exn ->
793 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
794 flush stderr;
798 module Ne = struct
799 type 'a t = | Res of 'a | Exn of exn;;
801 let pipe () =
802 try Res (Unix.pipe ())
803 with exn -> Exn exn
806 let clo fd f =
807 try tempfailureretry Unix.close fd
808 with exn -> f (exntos exn)
811 let dup fd =
812 try Res (tempfailureretry Unix.dup fd)
813 with exn -> Exn exn
816 let dup2 fd1 fd2 =
817 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
818 with exn -> Exn exn
820 end;;
822 let redirectstderr () =
823 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
824 if conf.redirectstderr
825 then
826 match Ne.pipe () with
827 | Ne.Exn exn ->
828 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
830 | Ne.Res (r, w) ->
831 begin match Ne.dup Unix.stderr with
832 | Ne.Exn exn ->
833 dolog "failed to dup stderr: %s" (exntos exn);
834 Ne.clo r (clofail "pipe/r");
835 Ne.clo w (clofail "pipe/w");
837 | Ne.Res dupstderr ->
838 begin match Ne.dup2 w Unix.stderr with
839 | Ne.Exn exn ->
840 dolog "failed to dup2 to stderr: %s" (exntos exn);
841 Ne.clo dupstderr (clofail "stderr duplicate");
842 Ne.clo r (clofail "redir pipe/r");
843 Ne.clo w (clofail "redir pipe/w");
845 | Ne.Res () ->
846 state.stderr <- dupstderr;
847 state.errfd <- Some r;
848 end;
850 else (
851 state.newerrmsgs <- false;
852 begin match state.errfd with
853 | Some fd ->
854 begin match Ne.dup2 state.stderr Unix.stderr with
855 | Ne.Exn exn ->
856 dolog "failed to dup2 original stderr: %s" (exntos exn)
857 | Ne.Res () ->
858 Ne.clo fd (clofail "dup of stderr");
859 state.errfd <- None;
860 end;
861 | None -> ()
862 end;
863 prerr_string (Buffer.contents state.errmsgs);
864 flush stderr;
865 Buffer.clear state.errmsgs;
869 module G =
870 struct
871 let postRedisplay who =
872 if conf.verbose
873 then prerr_endline ("redisplay for " ^ who);
874 state.redisplay <- true;
876 end;;
878 let getopaque pageno =
879 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
880 with Not_found -> None
883 let putopaque pageno opaque =
884 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
887 let pagetranslatepoint l x y =
888 let dy = y - l.pagedispy in
889 let y = dy + l.pagey in
890 let dx = x - l.pagedispx in
891 let x = dx + l.pagex in
892 (x, y);
895 let onppundermouse g x y d =
896 let rec f = function
897 | l :: rest ->
898 begin match getopaque l.pageno with
899 | Some opaque ->
900 let x0 = l.pagedispx in
901 let x1 = x0 + l.pagevw in
902 let y0 = l.pagedispy in
903 let y1 = y0 + l.pagevh in
904 if y >= y0 && y <= y1 && x >= x0 && x <= x1
905 then
906 let px, py = pagetranslatepoint l x y in
907 match g opaque l px py with
908 | Some res -> res
909 | None -> f rest
910 else f rest
911 | _ ->
912 f rest
914 | [] -> d
916 f state.layout
919 let getunder x y =
920 let g opaque l px py =
921 if state.bzoom
922 then (
923 match rectofblock opaque px py with
924 | Some a ->
925 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
926 state.rects <- [l.pageno, l.pageno mod 3, rect];
927 G.postRedisplay "rectofblock";
928 | None -> ()
930 match whatsunder opaque px py with
931 | Unone -> None
932 | under -> Some under
934 onppundermouse g x y Unone
937 let unproject x y =
938 let g opaque l x y =
939 match unproject opaque x y with
940 | Some (x, y) -> Some (Some (l.pageno, x, y))
941 | None -> None
943 onppundermouse g x y None;
946 let showtext c s =
947 state.text <- Printf.sprintf "%c%s" c s;
948 G.postRedisplay "showtext";
951 let paxunder x y =
952 let g opaque l px py =
953 if markunder opaque px py conf.paxmark
954 then (
955 Some (fun () ->
956 match getopaque l.pageno with
957 | None -> ()
958 | Some opaque ->
959 match Ne.pipe () with
960 | Ne.Exn exn ->
961 showtext '!'
962 (Printf.sprintf
963 "can not create mark pipe: %s"
964 (exntos exn));
965 | Ne.Res (r, w) ->
966 let doclose what fd =
967 Ne.clo fd (fun msg ->
968 dolog "%s close failed: %s" what msg)
971 popen conf.paxcmd [r, 0; w, -1];
972 copysel w opaque false;
973 doclose "pipe/r" r;
974 G.postRedisplay "paxunder";
975 with exn ->
976 dolog "can not execute %S: %s"
977 conf.paxcmd (exntos exn);
978 doclose "pipe/r" r;
979 doclose "pipe/w" w;
982 else None
984 G.postRedisplay "paxunder";
985 state.roam <-
986 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
989 let selstring s =
990 match Ne.pipe () with
991 | Ne.Exn exn ->
992 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
993 | Ne.Res (r, w) ->
994 let popened =
995 try popen conf.selcmd [r, 0; w, -1]; true
996 with exn ->
997 showtext '!'
998 (Printf.sprintf "failed to execute %s: %s"
999 conf.selcmd (exntos exn));
1000 false
1002 let clo cap fd =
1003 Ne.clo fd (fun msg ->
1004 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1007 if popened
1008 then
1009 (try
1010 let l = String.length s in
1011 let n = tempfailureretry (Unix.write w s 0) l in
1012 if n != l
1013 then
1014 showtext '!'
1015 (Printf.sprintf
1016 "failed to write %d characters to sel pipe, wrote %d"
1019 with exn ->
1020 showtext '!'
1021 (Printf.sprintf "failed to write to sel pipe: %s"
1022 (exntos exn)
1025 else dolog "%s" s;
1026 clo "pipe/r" r;
1027 clo "pipe/w" w;
1030 let undertext = function
1031 | Unone -> "none"
1032 | Ulinkuri s -> s
1033 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1034 | Utext s -> "font: " ^ s
1035 | Uunexpected s -> "unexpected: " ^ s
1036 | Ulaunch s -> "launch: " ^ s
1037 | Unamed s -> "named: " ^ s
1038 | Uremote (filename, pageno) ->
1039 Printf.sprintf "%s: page %d" filename (pageno+1)
1042 let updateunder x y =
1043 match getunder x y with
1044 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1045 | Ulinkuri uri ->
1046 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1047 Wsi.setcursor Wsi.CURSOR_INFO
1048 | Ulinkgoto (pageno, _) ->
1049 if conf.underinfo
1050 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1051 Wsi.setcursor Wsi.CURSOR_INFO
1052 | Utext s ->
1053 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1054 Wsi.setcursor Wsi.CURSOR_TEXT
1055 | Uunexpected s ->
1056 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1057 Wsi.setcursor Wsi.CURSOR_INHERIT
1058 | Ulaunch s ->
1059 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1060 Wsi.setcursor Wsi.CURSOR_INHERIT
1061 | Unamed s ->
1062 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1063 Wsi.setcursor Wsi.CURSOR_INHERIT
1064 | Uremote (filename, pageno) ->
1065 if conf.underinfo then showtext 'r'
1066 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1067 Wsi.setcursor Wsi.CURSOR_INFO
1070 let showlinktype under =
1071 if conf.underinfo
1072 then
1073 match under with
1074 | Unone -> ()
1075 | under ->
1076 let s = undertext under in
1077 showtext ' ' s
1080 let addchar s c =
1081 let b = Buffer.create (String.length s + 1) in
1082 Buffer.add_string b s;
1083 Buffer.add_char b c;
1084 Buffer.contents b;
1087 module type TextEnumType =
1089 type t
1090 val name : string
1091 val names : string array
1092 end;;
1094 module TextEnumMake (Ten : TextEnumType) =
1095 struct
1096 let names = Ten.names;;
1097 let to_int (t : Ten.t) = Obj.magic t;;
1098 let to_string t = names.(to_int t);;
1099 let of_int n : Ten.t = Obj.magic n;;
1100 let of_string s =
1101 let rec find i =
1102 if i = Array.length names
1103 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1104 else (
1105 if Ten.names.(i) = s
1106 then of_int i
1107 else find (i+1)
1109 in find 0;;
1110 end;;
1112 module CSTE = TextEnumMake (struct
1113 type t = colorspace;;
1114 let name = "colorspace";;
1115 let names = [|"rgb"; "bgr"; "gray"|];;
1116 end);;
1118 module MTE = TextEnumMake (struct
1119 type t = mark;;
1120 let name = "mark";;
1121 let names = [|"page"; "block"; "line"; "word"|];;
1122 end);;
1124 module FMTE = TextEnumMake (struct
1125 type t= fitmodel;;
1126 let name = "fitmodel";;
1127 let names = [|"width"; "proportional"; "page"|];;
1128 end);;
1130 let intentry_with_suffix text key =
1131 let c =
1132 if key >= 32 && key < 127
1133 then Char.chr key
1134 else '\000'
1136 match Char.lowercase c with
1137 | '0' .. '9' ->
1138 let text = addchar text c in
1139 TEcont text
1141 | 'k' | 'm' | 'g' ->
1142 let text = addchar text c in
1143 TEcont text
1145 | _ ->
1146 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1147 TEcont text
1150 let multicolumns_to_string (n, a, b) =
1151 if a = 0 && b = 0
1152 then Printf.sprintf "%d" n
1153 else Printf.sprintf "%d,%d,%d" n a b;
1156 let multicolumns_of_string s =
1158 (int_of_string s, 0, 0)
1159 with _ ->
1160 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1161 if a > 1 || b > 1
1162 then failwith "subtly broken"; (n, a, b)
1166 let readcmd fd =
1167 let s = "xxxx" in
1168 let n = tempfailureretry (Unix.read fd s 0) 4 in
1169 if n != 4 then failwith "incomplete read(len)";
1170 let len = 0
1171 lor (Char.code s.[0] lsl 24)
1172 lor (Char.code s.[1] lsl 16)
1173 lor (Char.code s.[2] lsl 8)
1174 lor (Char.code s.[3] lsl 0)
1176 let s = String.create len in
1177 let n = tempfailureretry (Unix.read fd s 0) len in
1178 if n != len then failwith "incomplete read(data)";
1182 let btod b = if b then 1 else 0;;
1184 let wcmd fmt =
1185 let b = Buffer.create 16 in
1186 Buffer.add_string b "llll";
1187 Printf.kbprintf
1188 (fun b ->
1189 let s = Buffer.contents b in
1190 let n = String.length s in
1191 let len = n - 4 in
1192 (* dolog "wcmd %S" (String.sub s 4 len); *)
1193 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1194 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1195 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1196 s.[3] <- Char.chr (len land 0xff);
1197 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1198 if n' != n then failwith "write failed";
1199 ) b fmt;
1202 let calcips h =
1203 let d = state.winh - h in
1204 max conf.interpagespace ((d + 1) / 2)
1207 let rowyh (c, coverA, coverB) b n =
1208 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1209 then
1210 let _, _, vy, (_, _, h, _) = b.(n) in
1211 (vy, h)
1212 else
1213 let n' = n - coverA in
1214 let d = n' mod c in
1215 let s = n - d in
1216 let e = min state.pagecount (s + c) in
1217 let rec find m miny maxh = if m = e then miny, maxh else
1218 let _, _, y, (_, _, h, _) = b.(m) in
1219 let miny = min miny y in
1220 let maxh = max maxh h in
1221 find (m+1) miny maxh
1222 in find s max_int 0
1225 let calcheight () =
1226 match conf.columns with
1227 | Cmulti ((_, _, _) as cl, b) ->
1228 if Array.length b > 0
1229 then
1230 let y, h = rowyh cl b (Array.length b - 1) in
1231 y + h + (if conf.presentation then calcips h else 0)
1232 else 0
1233 | Csingle b ->
1234 if Array.length b > 0
1235 then
1236 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1237 y + h + (if conf.presentation then calcips h else 0)
1238 else 0
1239 | Csplit (_, b) ->
1240 if Array.length b > 0
1241 then
1242 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1243 y + h
1244 else 0
1247 let getpageyh pageno =
1248 let pageno = bound pageno 0 (state.pagecount-1) in
1249 match conf.columns with
1250 | Csingle b ->
1251 if Array.length b = 0
1252 then 0, 0
1253 else
1254 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1255 let y =
1256 if conf.presentation
1257 then y - calcips h
1258 else y
1260 y, h
1261 | Cmulti (cl, b) ->
1262 if Array.length b = 0
1263 then 0, 0
1264 else
1265 let y, h = rowyh cl b pageno in
1266 let y =
1267 if conf.presentation
1268 then y - calcips h
1269 else y
1271 y, h
1272 | Csplit (c, b) ->
1273 if Array.length b = 0
1274 then 0, 0
1275 else
1276 let n = pageno*c in
1277 let (_, _, y, (_, _, h, _)) = b.(n) in
1278 y, h
1281 let getpagedim pageno =
1282 let rec f ppdim l =
1283 match l with
1284 | (n, _, _, _) as pdim :: rest ->
1285 if n >= pageno
1286 then (if n = pageno then pdim else ppdim)
1287 else f pdim rest
1289 | [] -> ppdim
1291 f (-1, -1, -1, -1) state.pdims
1294 let getpagey pageno = fst (getpageyh pageno);;
1296 let nogeomcmds cmds =
1297 match cmds with
1298 | s, [] -> String.length s = 0
1299 | _ -> false
1302 let page_of_y y =
1303 let ((c, coverA, coverB) as cl), b =
1304 match conf.columns with
1305 | Csingle b -> (1, 0, 0), b
1306 | Cmulti (c, b) -> c, b
1307 | Csplit (_, b) -> (1, 0, 0), b
1309 if Array.length b = 0
1310 then -1
1311 else
1312 let rec bsearch nmin nmax =
1313 if nmin > nmax
1314 then bound nmin 0 (state.pagecount-1)
1315 else
1316 let n = (nmax + nmin) / 2 in
1317 let vy, h = rowyh cl b n in
1318 let y0, y1 =
1319 if conf.presentation
1320 then
1321 let ips = calcips h in
1322 let y0 = vy - ips in
1323 let y1 = vy + h + ips in
1324 y0, y1
1325 else (
1326 if n = 0
1327 then 0, vy + h + conf.interpagespace
1328 else
1329 let y0 = vy - conf.interpagespace in
1330 y0, y0 + h + conf.interpagespace
1333 if y >= y0 && y < y1
1334 then (
1335 if c = 1
1336 then n
1337 else (
1338 if n > coverA
1339 then
1340 if n < state.pagecount - coverB
1341 then ((n-coverA)/c)*c + coverA
1342 else n
1343 else n
1346 else (
1347 if y > y0
1348 then bsearch (n+1) nmax
1349 else bsearch nmin (n-1)
1352 let r = bsearch 0 (state.pagecount-1) in
1356 let layoutN ((columns, coverA, coverB), b) y sh =
1357 let sh = sh - (hscrollh ()) in
1358 let rec fold accu n =
1359 if n = Array.length b
1360 then accu
1361 else
1362 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1363 if (vy - y) > sh &&
1364 (n = coverA - 1
1365 || n = state.pagecount - coverB
1366 || (n - coverA) mod columns = columns - 1)
1367 then accu
1368 else
1369 let accu =
1370 if vy + h > y
1371 then
1372 let pagey = max 0 (y - vy) in
1373 let pagedispy = if pagey > 0 then 0 else vy - y in
1374 let pagedispx, pagex =
1375 let pdx =
1376 if n = coverA - 1 || n = state.pagecount - coverB
1377 then state.x + (wadjsb state.winw - w) / 2
1378 else dx + xoff + state.x
1380 if pdx < 0
1381 then 0, -pdx
1382 else pdx, 0
1384 let pagevw =
1385 let vw = wadjsb state.winw - pagedispx in
1386 let pw = w - pagex in
1387 min vw pw
1389 let pagevh = min (h - pagey) (sh - pagedispy) in
1390 if pagevw > 0 && pagevh > 0
1391 then
1392 let e =
1393 { pageno = n
1394 ; pagedimno = pdimno
1395 ; pagew = w
1396 ; pageh = h
1397 ; pagex = pagex
1398 ; pagey = pagey
1399 ; pagevw = pagevw
1400 ; pagevh = pagevh
1401 ; pagedispx = pagedispx
1402 ; pagedispy = pagedispy
1403 ; pagecol = 0
1406 e :: accu
1407 else
1408 accu
1409 else
1410 accu
1412 fold accu (n+1)
1414 List.rev (fold [] (page_of_y y));
1417 let layoutS (columns, b) y sh =
1418 let sh = sh - hscrollh () in
1419 let rec fold accu n =
1420 if n = Array.length b
1421 then accu
1422 else
1423 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1424 if (vy - y) > sh
1425 then accu
1426 else
1427 let accu =
1428 if vy + pageh > y
1429 then
1430 let x = xoff + state.x in
1431 let pagey = max 0 (y - vy) in
1432 let pagedispy = if pagey > 0 then 0 else vy - y in
1433 let pagedispx, pagex =
1434 if px = 0
1435 then (
1436 if x < 0
1437 then 0, -x
1438 else x, 0
1440 else (
1441 let px = px - x in
1442 if px < 0
1443 then -px, 0
1444 else 0, px
1447 let pagecolw = pagew/columns in
1448 let pagedispx =
1449 if pagecolw < state.winw
1450 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1451 else pagedispx
1453 let pagevw =
1454 let vw = wadjsb state.winw - pagedispx in
1455 let pw = pagew - pagex in
1456 min vw pw
1458 let pagevw = min pagevw pagecolw in
1459 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1460 if pagevw > 0 && pagevh > 0
1461 then
1462 let e =
1463 { pageno = n/columns
1464 ; pagedimno = pdimno
1465 ; pagew = pagew
1466 ; pageh = pageh
1467 ; pagex = pagex
1468 ; pagey = pagey
1469 ; pagevw = pagevw
1470 ; pagevh = pagevh
1471 ; pagedispx = pagedispx
1472 ; pagedispy = pagedispy
1473 ; pagecol = n mod columns
1476 e :: accu
1477 else
1478 accu
1479 else
1480 accu
1482 fold accu (n+1)
1484 List.rev (fold [] 0)
1487 let layout y sh =
1488 if nogeomcmds state.geomcmds
1489 then
1490 match conf.columns with
1491 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1492 | Cmulti c -> layoutN c y sh
1493 | Csplit s -> layoutS s y sh
1494 else []
1497 let clamp incr =
1498 let y = state.y + incr in
1499 let y = max 0 y in
1500 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1504 let itertiles l f =
1505 let tilex = l.pagex mod conf.tilew in
1506 let tiley = l.pagey mod conf.tileh in
1508 let col = l.pagex / conf.tilew in
1509 let row = l.pagey / conf.tileh in
1511 let rec rowloop row y0 dispy h =
1512 if h = 0
1513 then ()
1514 else (
1515 let dh = conf.tileh - y0 in
1516 let dh = min h dh in
1517 let rec colloop col x0 dispx w =
1518 if w = 0
1519 then ()
1520 else (
1521 let dw = conf.tilew - x0 in
1522 let dw = min w dw in
1524 f col row dispx dispy x0 y0 dw dh;
1525 colloop (col+1) 0 (dispx+dw) (w-dw)
1528 colloop col tilex l.pagedispx l.pagevw;
1529 rowloop (row+1) 0 (dispy+dh) (h-dh)
1532 if l.pagevw > 0 && l.pagevh > 0
1533 then rowloop row tiley l.pagedispy l.pagevh;
1536 let gettileopaque l col row =
1537 let key =
1538 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1540 try Some (Hashtbl.find state.tilemap key)
1541 with Not_found -> None
1544 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1545 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1546 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1549 let drawtiles l color =
1550 GlDraw.color color;
1551 let f col row x y tilex tiley w h =
1552 match gettileopaque l col row with
1553 | Some (opaque, _, t) ->
1554 let params = x, y, w, h, tilex, tiley in
1555 if conf.invert
1556 then (
1557 Gl.enable `blend;
1558 GlFunc.blend_func `zero `one_minus_src_color;
1560 drawtile params opaque;
1561 if conf.invert
1562 then Gl.disable `blend;
1563 if conf.debug
1564 then (
1565 let s = Printf.sprintf
1566 "%d[%d,%d] %f sec"
1567 l.pageno col row t
1569 let w = measurestr fstate.fontsize s in
1570 GlMisc.push_attrib [`current];
1571 GlDraw.color (0.0, 0.0, 0.0);
1572 GlDraw.rect
1573 (float (x-2), float (y-2))
1574 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1575 GlDraw.color (1.0, 1.0, 1.0);
1576 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1577 GlMisc.pop_attrib ();
1580 | _ ->
1581 let w =
1582 let lw = wadjsb state.winw - x in
1583 min lw w
1584 and h =
1585 let lh = state.winh - y in
1586 min lh h
1588 begin match state.texid with
1589 | Some id ->
1590 Gl.enable `texture_2d;
1591 GlTex.bind_texture `texture_2d id;
1592 let x0 = float x
1593 and y0 = float y
1594 and x1 = float (x+w)
1595 and y1 = float (y+h) in
1597 let tw = float w /. 16.0
1598 and th = float h /. 16.0 in
1599 let tx0 = float tilex /. 16.0
1600 and ty0 = float tiley /. 16.0 in
1601 let tx1 = tx0 +. tw
1602 and ty1 = ty0 +. th in
1603 GlDraw.begins `quads;
1604 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1605 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1606 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1607 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1608 GlDraw.ends ();
1610 Gl.disable `texture_2d;
1611 | None ->
1612 GlDraw.color (1.0, 1.0, 1.0);
1613 GlDraw.rect
1614 (float x, float y)
1615 (float (x+w), float (y+h));
1616 end;
1617 if w > 128 && h > fstate.fontsize + 10
1618 then (
1619 GlDraw.color (0.0, 0.0, 0.0);
1620 let c, r =
1621 if conf.verbose
1622 then (col*conf.tilew, row*conf.tileh)
1623 else col, row
1625 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1627 GlDraw.color color;
1629 itertiles l f
1632 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1634 let tilevisible1 l x y =
1635 let ax0 = l.pagex
1636 and ax1 = l.pagex + l.pagevw
1637 and ay0 = l.pagey
1638 and ay1 = l.pagey + l.pagevh in
1640 let bx0 = x
1641 and by0 = y in
1642 let bx1 = min (bx0 + conf.tilew) l.pagew
1643 and by1 = min (by0 + conf.tileh) l.pageh in
1645 let rx0 = max ax0 bx0
1646 and ry0 = max ay0 by0
1647 and rx1 = min ax1 bx1
1648 and ry1 = min ay1 by1 in
1650 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1651 nonemptyintersection
1654 let tilevisible layout n x y =
1655 let rec findpageinlayout m = function
1656 | l :: rest when l.pageno = n ->
1657 tilevisible1 l x y || (
1658 match conf.columns with
1659 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1660 | _ -> false
1662 | _ :: rest -> findpageinlayout 0 rest
1663 | [] -> false
1665 findpageinlayout 0 layout;
1668 let tileready l x y =
1669 tilevisible1 l x y &&
1670 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1673 let tilepage n p layout =
1674 let rec loop = function
1675 | l :: rest ->
1676 if l.pageno = n
1677 then
1678 let f col row _ _ _ _ _ _ =
1679 if state.currently = Idle
1680 then
1681 match gettileopaque l col row with
1682 | Some _ -> ()
1683 | None ->
1684 let x = col*conf.tilew
1685 and y = row*conf.tileh in
1686 let w =
1687 let w = l.pagew - x in
1688 min w conf.tilew
1690 let h =
1691 let h = l.pageh - y in
1692 min h conf.tileh
1694 let pbo =
1695 if conf.usepbo
1696 then getpbo w h conf.colorspace
1697 else "0"
1699 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1700 state.currently <-
1701 Tiling (
1702 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1703 conf.tilew, conf.tileh
1706 itertiles l f;
1707 else
1708 loop rest
1710 | [] -> ()
1712 if nogeomcmds state.geomcmds
1713 then loop layout;
1716 let preloadlayout y =
1717 let y = if y < state.winh then 0 else y - state.winh in
1718 let h = state.winh*3 in
1719 layout y h;
1722 let load pages =
1723 let rec loop pages =
1724 if state.currently != Idle
1725 then ()
1726 else
1727 match pages with
1728 | l :: rest ->
1729 begin match getopaque l.pageno with
1730 | None ->
1731 wcmd "page %d %d" l.pageno l.pagedimno;
1732 state.currently <- Loading (l, state.gen);
1733 | Some opaque ->
1734 tilepage l.pageno opaque pages;
1735 loop rest
1736 end;
1737 | _ -> ()
1739 if nogeomcmds state.geomcmds
1740 then loop pages
1743 let preload pages =
1744 load pages;
1745 if conf.preload && state.currently = Idle
1746 then load (preloadlayout state.y);
1749 let layoutready layout =
1750 let rec fold all ls =
1751 all && match ls with
1752 | l :: rest ->
1753 let seen = ref false in
1754 let allvisible = ref true in
1755 let foo col row _ _ _ _ _ _ =
1756 seen := true;
1757 allvisible := !allvisible &&
1758 begin match gettileopaque l col row with
1759 | Some _ -> true
1760 | None -> false
1763 itertiles l foo;
1764 fold (!seen && !allvisible) rest
1765 | [] -> true
1767 let alltilesvisible = fold true layout in
1768 alltilesvisible;
1771 let gotoy y =
1772 let y = bound y 0 state.maxy in
1773 let y, layout, proceed =
1774 match conf.maxwait with
1775 | Some time when state.ghyll == noghyll ->
1776 begin match state.throttle with
1777 | None ->
1778 let layout = layout y state.winh in
1779 let ready = layoutready layout in
1780 if not ready
1781 then (
1782 load layout;
1783 state.throttle <- Some (layout, y, now ());
1785 else G.postRedisplay "gotoy showall (None)";
1786 y, layout, ready
1787 | Some (_, _, started) ->
1788 let dt = now () -. started in
1789 if dt > time
1790 then (
1791 state.throttle <- None;
1792 let layout = layout y state.winh in
1793 load layout;
1794 G.postRedisplay "maxwait";
1795 y, layout, true
1797 else -1, [], false
1800 | _ ->
1801 let layout = layout y state.winh in
1802 if not !wtmode || layoutready layout
1803 then G.postRedisplay "gotoy ready";
1804 y, layout, true
1806 if proceed
1807 then (
1808 state.y <- y;
1809 state.layout <- layout;
1810 begin match state.mode with
1811 | LinkNav (Ltexact (pageno, linkno)) ->
1812 let rec loop = function
1813 | [] ->
1814 state.mode <- LinkNav (Ltgendir 0)
1815 | l :: _ when l.pageno = pageno ->
1816 begin match getopaque pageno with
1817 | None ->
1818 state.mode <- LinkNav (Ltgendir 0)
1819 | Some opaque ->
1820 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1821 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1822 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1823 then state.mode <- LinkNav (Ltgendir 0)
1825 | _ :: rest -> loop rest
1827 loop layout
1828 | _ -> ()
1829 end;
1830 begin match state.mode with
1831 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1832 if not (pagevisible layout pageno)
1833 then (
1834 match state.layout with
1835 | [] -> ()
1836 | l :: _ ->
1837 state.mode <- Birdseye (
1838 conf, leftx, l.pageno, hooverpageno, anchor
1841 | LinkNav (Ltgendir dir as lt) ->
1842 let linknav =
1843 let rec loop = function
1844 | [] -> lt
1845 | l :: rest ->
1846 match getopaque l.pageno with
1847 | None -> loop rest
1848 | Some opaque ->
1849 let link =
1850 let ld =
1851 if dir = 0
1852 then LDfirstvisible (l.pagex, l.pagey, dir)
1853 else (
1854 if dir > 0 then LDfirst else LDlast
1857 findlink opaque ld
1859 match link with
1860 | Lnotfound -> loop rest
1861 | Lfound n ->
1862 showlinktype (getlink opaque n);
1863 Ltexact (l.pageno, n)
1865 loop state.layout
1867 state.mode <- LinkNav linknav
1868 | _ -> ()
1869 end;
1870 preload layout;
1872 state.ghyll <- noghyll;
1873 if conf.updatecurs
1874 then (
1875 let mx, my = state.mpos in
1876 updateunder mx my;
1880 let conttiling pageno opaque =
1881 tilepage pageno opaque
1882 (if conf.preload then preloadlayout state.y else state.layout)
1885 let gotoy_and_clear_text y =
1886 if not conf.verbose then state.text <- "";
1887 gotoy y;
1890 let getanchor1 l =
1891 let top =
1892 let coloff = l.pagecol * l.pageh in
1893 float (l.pagey + coloff) /. float l.pageh
1895 let dtop =
1896 if l.pagedispy = 0
1897 then
1899 else (
1900 if conf.presentation
1901 then float l.pagedispy /. float (calcips l.pageh)
1902 else float l.pagedispy /. float conf.interpagespace
1905 (l.pageno, top, dtop)
1908 let getanchor () =
1909 match state.layout with
1910 | l :: _ -> getanchor1 l
1911 | [] ->
1912 let n = page_of_y state.y in
1913 if n = -1
1914 then state.anchor
1915 else
1916 let y, h = getpageyh n in
1917 let dy = y - state.y in
1918 let dtop =
1919 if conf.presentation
1920 then
1921 let ips = calcips h in
1922 float (dy + ips) /. float ips
1923 else
1924 float dy /. float conf.interpagespace
1926 (n, 0.0, dtop)
1929 let getanchory (n, top, dtop) =
1930 let y, h = getpageyh n in
1931 if conf.presentation
1932 then
1933 let ips = calcips h in
1934 y + truncate (top*.float h -. dtop*.float ips) + ips;
1935 else
1936 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1939 let gotoanchor anchor =
1940 gotoy (getanchory anchor);
1943 let addnav () =
1944 cbput state.hists.nav (getanchor ());
1947 let getnav dir =
1948 let anchor = cbgetc state.hists.nav dir in
1949 getanchory anchor;
1952 let gotoghyll y =
1953 let scroll f n a b =
1954 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1955 let snake f a b =
1956 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1957 if f < a
1958 then s (float f /. float a)
1959 else (
1960 if f > b
1961 then 1.0 -. s ((float (f-b) /. float (n-b)))
1962 else 1.0
1965 snake f a b
1966 and summa f n a b =
1967 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1968 let iv x = x**3.-.0.5*.x**4. in
1969 let iv1 = iv f in
1970 let ins = float a *. iv1
1971 and outs = float (n-b) *. iv1 in
1972 let ones = b - a in
1973 ins +. outs +. float ones
1975 let rec set (_N, _A, _B) y sy =
1976 let sum = summa 1.0 _N _A _B in
1977 let dy = float (y - sy) in
1978 state.ghyll <- (
1979 let rec gf n y1 o =
1980 if n >= _N
1981 then state.ghyll <- noghyll
1982 else
1983 let go n =
1984 let s = scroll n _N _A _B in
1985 let y1 = y1 +. ((s *. dy) /. sum) in
1986 gotoy_and_clear_text (truncate y1);
1987 state.ghyll <- gf (n+1) y1;
1989 match o with
1990 | None -> go n
1991 | Some y' -> set (_N/2, 1, 1) y' state.y
1993 gf 0 (float state.y)
1996 match conf.ghyllscroll with
1997 | None ->
1998 gotoy_and_clear_text y
1999 | Some nab ->
2000 if state.ghyll == noghyll
2001 then set nab y state.y
2002 else state.ghyll (Some y)
2005 let gotopage n top =
2006 let y, h = getpageyh n in
2007 let y = y + (truncate (top *. float h)) in
2008 gotoghyll y
2011 let gotopage1 n top =
2012 let y = getpagey n in
2013 let y = y + top in
2014 gotoghyll y
2017 let invalidate s f =
2018 state.layout <- [];
2019 state.pdims <- [];
2020 state.rects <- [];
2021 state.rects1 <- [];
2022 match state.geomcmds with
2023 | ps, [] when String.length ps = 0 ->
2024 f ();
2025 state.geomcmds <- s, [];
2027 | ps, [] ->
2028 state.geomcmds <- ps, [s, f];
2030 | ps, (s', _) :: rest when s' = s ->
2031 state.geomcmds <- ps, ((s, f) :: rest);
2033 | ps, cmds ->
2034 state.geomcmds <- ps, ((s, f) :: cmds);
2037 let flushpages () =
2038 Hashtbl.iter (fun _ opaque ->
2039 wcmd "freepage %s" opaque;
2040 ) state.pagemap;
2041 Hashtbl.clear state.pagemap;
2044 let flushtiles () =
2045 if not (Queue.is_empty state.tilelru)
2046 then (
2047 Queue.iter (fun (k, p, s) ->
2048 wcmd "freetile %s" p;
2049 state.memused <- state.memused - s;
2050 Hashtbl.remove state.tilemap k;
2051 ) state.tilelru;
2052 state.uioh#infochanged Memused;
2053 Queue.clear state.tilelru;
2055 load state.layout;
2058 let stateh h =
2059 let h = truncate (float h*.conf.zoom) in
2060 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2061 h - d
2064 let opendoc path password =
2065 state.path <- path;
2066 state.password <- password;
2067 state.gen <- state.gen + 1;
2068 state.docinfo <- [];
2070 flushpages ();
2071 setaalevel conf.aalevel;
2072 let titlepath =
2073 if String.length state.origin = 0
2074 then path
2075 else state.origin
2077 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2078 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2079 invalidate "reqlayout"
2080 (fun () ->
2081 wcmd "reqlayout %d %d %d %s\000"
2082 conf.angle (FMTE.to_int conf.fitmodel)
2083 (stateh state.winh) state.nameddest
2087 let reload () =
2088 state.anchor <- getanchor ();
2089 opendoc state.path state.password;
2092 let scalecolor c =
2093 let c = c *. conf.colorscale in
2094 (c, c, c);
2097 let scalecolor2 (r, g, b) =
2098 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2101 let docolumns = function
2102 | Csingle _ ->
2103 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2104 let rec loop pageno pdimno pdim y ph pdims =
2105 if pageno = state.pagecount
2106 then ()
2107 else
2108 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2109 match pdims with
2110 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2111 pdimno+1, pdim, rest
2112 | _ ->
2113 pdimno, pdim, pdims
2115 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2116 let y = y +
2117 (if conf.presentation
2118 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2119 else (if pageno = 0 then 0 else conf.interpagespace)
2122 a.(pageno) <- (pdimno, x, y, pdim);
2123 loop (pageno+1) pdimno pdim (y + h) h pdims
2125 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2126 conf.columns <- Csingle a;
2128 | Cmulti ((columns, coverA, coverB), _) ->
2129 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2130 let rec loop pageno pdimno pdim x y rowh pdims =
2131 let rec fixrow m = if m = pageno then () else
2132 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2133 if h < rowh
2134 then (
2135 let y = y + (rowh - h) / 2 in
2136 a.(m) <- (pdimno, x, y, pdim);
2138 fixrow (m+1)
2140 if pageno = state.pagecount
2141 then fixrow (((pageno - 1) / columns) * columns)
2142 else
2143 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2144 match pdims with
2145 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2146 pdimno+1, pdim, rest
2147 | _ ->
2148 pdimno, pdim, pdims
2150 let x, y, rowh' =
2151 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2152 then (
2153 let x = (wadjsb state.winw - w) / 2 in
2154 let ips =
2155 if conf.presentation then calcips h else conf.interpagespace in
2156 x, y + ips + rowh, h
2158 else (
2159 if (pageno - coverA) mod columns = 0
2160 then (
2161 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2162 let y =
2163 if conf.presentation
2164 then
2165 let ips = calcips h in
2166 y + (if pageno = 0 then 0 else calcips rowh + ips)
2167 else
2168 y + (if pageno = 0 then 0 else conf.interpagespace)
2170 x, y + rowh, h
2172 else x, y, max rowh h
2175 let y =
2176 if pageno > 1 && (pageno - coverA) mod columns = 0
2177 then (
2178 let y =
2179 if pageno = columns && conf.presentation
2180 then (
2181 let ips = calcips rowh in
2182 for i = 0 to pred columns
2184 let (pdimno, x, y, pdim) = a.(i) in
2185 a.(i) <- (pdimno, x, y+ips, pdim)
2186 done;
2187 y+ips;
2189 else y
2191 fixrow (pageno - columns);
2194 else y
2196 a.(pageno) <- (pdimno, x, y, pdim);
2197 let x = x + w + xoff*2 + conf.interpagespace in
2198 loop (pageno+1) pdimno pdim x y rowh' pdims
2200 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2201 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2203 | Csplit (c, _) ->
2204 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2205 let rec loop pageno pdimno pdim y pdims =
2206 if pageno = state.pagecount
2207 then ()
2208 else
2209 let pdimno, ((_, w, h, _) as pdim), pdims =
2210 match pdims with
2211 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2212 pdimno+1, pdim, rest
2213 | _ ->
2214 pdimno, pdim, pdims
2216 let cw = w / c in
2217 let rec loop1 n x y =
2218 if n = c then y else (
2219 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2220 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2223 let y = loop1 0 0 y in
2224 loop (pageno+1) pdimno pdim y pdims
2226 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2227 conf.columns <- Csplit (c, a);
2230 let represent () =
2231 docolumns conf.columns;
2232 state.maxy <- calcheight ();
2233 if state.reprf == noreprf
2234 then (
2235 match state.mode with
2236 | Birdseye (_, _, pageno, _, _) ->
2237 let y, h = getpageyh pageno in
2238 let top = (state.winh - h) / 2 in
2239 gotoy (max 0 (y - top))
2240 | _ -> gotoanchor state.anchor
2242 else (
2243 state.reprf ();
2244 state.reprf <- noreprf;
2248 let reshape w h =
2249 GlDraw.viewport 0 0 w h;
2250 let firsttime = state.geomcmds == firstgeomcmds in
2251 if not firsttime && nogeomcmds state.geomcmds
2252 then state.anchor <- getanchor ();
2254 state.winw <- w;
2255 let w = wadjsb (truncate (float w *. conf.zoom)) in
2256 let w = max w 2 in
2257 state.winh <- h;
2258 setfontsize fstate.fontsize;
2259 GlMat.mode `modelview;
2260 GlMat.load_identity ();
2262 GlMat.mode `projection;
2263 GlMat.load_identity ();
2264 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2265 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2266 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2268 let relx =
2269 if conf.zoom <= 1.0
2270 then 0.0
2271 else float state.x /. float state.w
2273 invalidate "geometry"
2274 (fun () ->
2275 state.w <- w;
2276 if not firsttime
2277 then state.x <- truncate (relx *. float w);
2278 let w =
2279 match conf.columns with
2280 | Csingle _ -> w
2281 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2282 | Csplit (c, _) -> w * c
2284 wcmd "geometry %d %d %d"
2285 w (stateh h) (FMTE.to_int conf.fitmodel)
2289 let enttext () =
2290 let len = String.length state.text in
2291 let drawstring s =
2292 let hscrollh =
2293 match state.mode with
2294 | Textentry _ | View | LinkNav _ ->
2295 let h, _, _ = state.uioh#scrollpw in
2297 | _ -> 0
2299 let rect x w =
2300 GlDraw.rect
2301 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2302 (x+.w, float (state.winh - hscrollh))
2305 let w = float (wadjsb state.winw - 1) in
2306 if state.progress >= 0.0 && state.progress < 1.0
2307 then (
2308 GlDraw.color (0.3, 0.3, 0.3);
2309 let w1 = w *. state.progress in
2310 rect 0.0 w1;
2311 GlDraw.color (0.0, 0.0, 0.0);
2312 rect w1 (w-.w1)
2314 else (
2315 GlDraw.color (0.0, 0.0, 0.0);
2316 rect 0.0 w;
2319 GlDraw.color (1.0, 1.0, 1.0);
2320 drawstring fstate.fontsize
2321 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2323 let s =
2324 match state.mode with
2325 | Textentry ((prefix, text, _, _, _, _), _) ->
2326 let s =
2327 if len > 0
2328 then
2329 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2330 else
2331 Printf.sprintf "%s%s_" prefix text
2335 | _ -> state.text
2337 let s =
2338 if state.newerrmsgs
2339 then (
2340 if not (istextentry state.mode) && state.uioh#eformsgs
2341 then
2342 let s1 = "(press 'e' to review error messasges)" in
2343 if String.length s > 0 then s ^ " " ^ s1 else s1
2344 else s
2346 else s
2348 if String.length s > 0
2349 then drawstring s
2352 let gctiles () =
2353 let len = Queue.length state.tilelru in
2354 let layout = lazy (
2355 match state.throttle with
2356 | None ->
2357 if conf.preload
2358 then preloadlayout state.y
2359 else state.layout
2360 | Some (layout, _, _) ->
2361 layout
2362 ) in
2363 let rec loop qpos =
2364 if state.memused <= conf.memlimit
2365 then ()
2366 else (
2367 if qpos < len
2368 then
2369 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2370 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2371 let (_, pw, ph, _) = getpagedim n in
2373 gen = state.gen
2374 && colorspace = conf.colorspace
2375 && angle = conf.angle
2376 && pagew = pw
2377 && pageh = ph
2378 && (
2379 let x = col*conf.tilew
2380 and y = row*conf.tileh in
2381 tilevisible (Lazy.force_val layout) n x y
2383 then Queue.push lruitem state.tilelru
2384 else (
2385 freepbo p;
2386 wcmd "freetile %s" p;
2387 state.memused <- state.memused - s;
2388 state.uioh#infochanged Memused;
2389 Hashtbl.remove state.tilemap k;
2391 loop (qpos+1)
2394 loop 0
2397 let logcurrently = function
2398 | Idle -> dolog "Idle"
2399 | Loading (l, gen) ->
2400 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2401 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2402 dolog
2403 "Tiling %d[%d,%d] page=%s cs=%s angle"
2404 l.pageno col row pageopaque
2405 (CSTE.to_string colorspace)
2407 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2408 angle gen conf.angle state.gen
2409 tilew tileh
2410 conf.tilew conf.tileh
2412 | Outlining _ ->
2413 dolog "outlining"
2416 let splitatspace =
2417 let r = Str.regexp " " in
2418 fun s -> Str.bounded_split r s 2;
2421 let onpagerect pageno f =
2422 let b =
2423 match conf.columns with
2424 | Cmulti (_, b) -> b
2425 | Csingle b -> b
2426 | Csplit (_, b) -> b
2428 if pageno >= 0 && pageno < Array.length b
2429 then
2430 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2431 let r = getpdimrect pdimno in
2432 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2435 let gotopagexy1 pageno x y =
2436 onpagerect pageno (fun w h ->
2437 let top = y /. h in
2438 let _,w1,_,leftx = getpagedim pageno in
2439 let wh = state.winh - hscrollh () in
2440 let sw = float w1 /. w in
2441 let x = sw *. x in
2442 let x = leftx + state.x + truncate x in
2443 let sx =
2444 if x < 0 || x >= wadjsb state.winw
2445 then state.x - x
2446 else state.x
2448 let py, h = getpageyh pageno in
2449 let pdy = truncate (top *. float h) in
2450 let y' = py + pdy in
2451 let dy = y' - state.y in
2452 let sy =
2453 if x != state.x || not (dy > 0 && dy < wh)
2454 then (
2455 if conf.presentation
2456 then
2457 if abs (py - y') > wh
2458 then y'
2459 else py
2460 else y';
2462 else state.y
2464 if state.x != sx || state.y != sy
2465 then (
2466 let x, y =
2467 if !wtmode
2468 then (
2469 let ww = wadjsb state.winw in
2470 let qx = sx / ww
2471 and qy = pdy / wh in
2472 let x = qx * ww
2473 and y = py + qy * wh in
2474 let x = if -x + ww > w1 then -(w1-ww) else x
2475 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2476 let y =
2477 if conf.presentation
2478 then
2479 if abs (py - y') > wh
2480 then y'
2481 else py
2482 else y';
2484 (x, y)
2486 else (sx, sy)
2488 state.x <- x;
2489 gotoy_and_clear_text y;
2491 else gotoy_and_clear_text state.y;
2495 let gotopagexy pageno x y =
2496 match state.mode with
2497 | Birdseye _ -> gotopage pageno 0.0
2498 | _ -> gotopagexy1 pageno x y
2501 let act cmds =
2502 (* dolog "%S" cmds; *)
2503 let cl = splitatspace cmds in
2504 let scan s fmt f =
2505 try Scanf.sscanf s fmt f
2506 with exn ->
2507 dolog "error processing '%S': %s" cmds (exntos exn);
2508 exit 1
2510 match cl with
2511 | "clear" :: [] ->
2512 state.uioh#infochanged Pdim;
2513 state.pdims <- [];
2515 | "clearrects" :: [] ->
2516 state.rects <- state.rects1;
2517 G.postRedisplay "clearrects";
2519 | "continue" :: args :: [] ->
2520 let n = scan args "%u" (fun n -> n) in
2521 state.pagecount <- n;
2522 begin match state.currently with
2523 | Outlining l ->
2524 state.currently <- Idle;
2525 state.outlines <- Array.of_list (List.rev l)
2526 | _ -> ()
2527 end;
2529 let cur, cmds = state.geomcmds in
2530 if String.length cur = 0
2531 then failwith "umpossible";
2533 begin match List.rev cmds with
2534 | [] ->
2535 state.geomcmds <- "", [];
2536 represent ();
2537 | (s, f) :: rest ->
2538 f ();
2539 state.geomcmds <- s, List.rev rest;
2540 end;
2541 if conf.maxwait = None && not !wtmode
2542 then G.postRedisplay "continue";
2544 | "title" :: args :: [] ->
2545 Wsi.settitle args
2547 | "msg" :: args :: [] ->
2548 showtext ' ' args
2550 | "vmsg" :: args :: [] ->
2551 if conf.verbose
2552 then showtext ' ' args
2554 | "emsg" :: args :: [] ->
2555 Buffer.add_string state.errmsgs args;
2556 state.newerrmsgs <- true;
2557 G.postRedisplay "error message"
2559 | "progress" :: args :: [] ->
2560 let progress, text =
2561 scan args "%f %n"
2562 (fun f pos ->
2563 f, String.sub args pos (String.length args - pos))
2565 state.text <- text;
2566 state.progress <- progress;
2567 G.postRedisplay "progress"
2569 | "firstmatch" :: args :: [] ->
2570 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2571 scan args "%u %d %f %f %f %f %f %f %f %f"
2572 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2573 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2575 let y = (getpagey pageno) + truncate y0 in
2576 addnav ();
2577 gotoy y;
2578 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2580 | "match" :: args :: [] ->
2581 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2582 scan args "%u %d %f %f %f %f %f %f %f %f"
2583 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2584 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2586 state.rects1 <-
2587 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2589 | "page" :: args :: [] ->
2590 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2591 begin match state.currently with
2592 | Loading (l, gen) ->
2593 vlog "page %d took %f sec" l.pageno t;
2594 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2595 begin match state.throttle with
2596 | None ->
2597 let preloadedpages =
2598 if conf.preload
2599 then preloadlayout state.y
2600 else state.layout
2602 let evict () =
2603 let set =
2604 List.fold_left (fun s l -> IntSet.add l.pageno s)
2605 IntSet.empty preloadedpages
2607 let evictedpages =
2608 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2609 if not (IntSet.mem pageno set)
2610 then (
2611 wcmd "freepage %s" opaque;
2612 key :: accu
2614 else accu
2615 ) state.pagemap []
2617 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2619 evict ();
2620 state.currently <- Idle;
2621 if gen = state.gen
2622 then (
2623 tilepage l.pageno pageopaque state.layout;
2624 load state.layout;
2625 load preloadedpages;
2626 if pagevisible state.layout l.pageno
2627 && layoutready state.layout
2628 then G.postRedisplay "page";
2631 | Some (layout, _, _) ->
2632 state.currently <- Idle;
2633 tilepage l.pageno pageopaque layout;
2634 load state.layout
2635 end;
2637 | _ ->
2638 dolog "Inconsistent loading state";
2639 logcurrently state.currently;
2640 exit 1
2643 | "tile" :: args :: [] ->
2644 let (x, y, opaque, size, t) =
2645 scan args "%u %u %s %u %f"
2646 (fun x y p size t -> (x, y, p, size, t))
2648 begin match state.currently with
2649 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2650 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2652 unmappbo opaque;
2653 if tilew != conf.tilew || tileh != conf.tileh
2654 then (
2655 wcmd "freetile %s" opaque;
2656 state.currently <- Idle;
2657 load state.layout;
2659 else (
2660 puttileopaque l col row gen cs angle opaque size t;
2661 state.memused <- state.memused + size;
2662 state.uioh#infochanged Memused;
2663 gctiles ();
2664 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2665 opaque, size) state.tilelru;
2667 let layout =
2668 match state.throttle with
2669 | None -> state.layout
2670 | Some (layout, _, _) -> layout
2673 state.currently <- Idle;
2674 if gen = state.gen
2675 && conf.colorspace = cs
2676 && conf.angle = angle
2677 && tilevisible layout l.pageno x y
2678 then conttiling l.pageno pageopaque;
2680 begin match state.throttle with
2681 | None ->
2682 preload state.layout;
2683 if gen = state.gen
2684 && conf.colorspace = cs
2685 && conf.angle = angle
2686 && tilevisible state.layout l.pageno x y
2687 && (not !wtmode || layoutready state.layout)
2688 then G.postRedisplay "tile nothrottle";
2690 | Some (layout, y, _) ->
2691 let ready = layoutready layout in
2692 if ready
2693 then (
2694 state.y <- y;
2695 state.layout <- layout;
2696 state.throttle <- None;
2697 G.postRedisplay "throttle";
2699 else load layout;
2700 end;
2703 | _ ->
2704 dolog "Inconsistent tiling state";
2705 logcurrently state.currently;
2706 exit 1
2709 | "pdim" :: args :: [] ->
2710 let (n, w, h, _) as pdim =
2711 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2713 let pdim =
2714 match conf.fitmodel, conf.columns with
2715 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2716 | _ -> pdim
2718 state.uioh#infochanged Pdim;
2719 state.pdims <- pdim :: state.pdims
2721 | "o" :: args :: [] ->
2722 let (l, n, t, h, pos) =
2723 scan args "%u %u %d %u %n"
2724 (fun l n t h pos -> l, n, t, h, pos)
2726 let s = String.sub args pos (String.length args - pos) in
2727 let outline = (s, l, (n, float t /. float h, 0.0)) in
2728 begin match state.currently with
2729 | Outlining outlines ->
2730 state.currently <- Outlining (outline :: outlines)
2731 | Idle ->
2732 state.currently <- Outlining [outline]
2733 | currently ->
2734 dolog "invalid outlining state";
2735 logcurrently currently
2738 | "a" :: args :: [] ->
2739 let (n, l, t) =
2740 scan args "%u %d %d" (fun n l t -> n, l, t)
2742 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2744 | "info" :: args :: [] ->
2745 state.docinfo <- (1, args) :: state.docinfo
2747 | "infoend" :: [] ->
2748 state.uioh#infochanged Docinfo;
2749 state.docinfo <- List.rev state.docinfo
2751 | _ ->
2752 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2755 let onhist cb =
2756 let rc = cb.rc in
2757 let action = function
2758 | HCprev -> cbget cb ~-1
2759 | HCnext -> cbget cb 1
2760 | HCfirst -> cbget cb ~-(cb.rc)
2761 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2762 and cancel () = cb.rc <- rc
2763 in (action, cancel)
2766 let search pattern forward =
2767 match conf.columns with
2768 | Csplit _ ->
2769 showtext '!' "searching does not work properly in split columns mode"
2770 | _ ->
2771 if String.length pattern > 0
2772 then
2773 let pn, py =
2774 match state.layout with
2775 | [] -> 0, 0
2776 | l :: _ ->
2777 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2779 wcmd "search %d %d %d %d,%s\000"
2780 (btod conf.icase) pn py (btod forward) pattern;
2783 let intentry text key =
2784 let c =
2785 if key >= 32 && key < 127
2786 then Char.chr key
2787 else '\000'
2789 match c with
2790 | '0' .. '9' ->
2791 let text = addchar text c in
2792 TEcont text
2794 | _ ->
2795 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2796 TEcont text
2799 let linknentry text key =
2800 let c =
2801 if key >= 32 && key < 127
2802 then Char.chr key
2803 else '\000'
2805 match c with
2806 | 'a' .. 'z' ->
2807 let text = addchar text c in
2808 TEcont text
2810 | _ ->
2811 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2812 TEcont text
2815 let linkndone f s =
2816 if String.length s > 0
2817 then (
2818 let n =
2819 let l = String.length s in
2820 let rec loop pos n = if pos = l then n else
2821 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2822 loop (pos+1) (n*26 + m)
2823 in loop 0 0
2825 let rec loop n = function
2826 | [] -> ()
2827 | l :: rest ->
2828 match getopaque l.pageno with
2829 | None -> loop n rest
2830 | Some opaque ->
2831 let m = getlinkcount opaque in
2832 if n < m
2833 then (
2834 let under = getlink opaque n in
2835 f under
2837 else loop (n-m) rest
2839 loop n state.layout;
2843 let textentry text key =
2844 if key land 0xff00 = 0xff00
2845 then TEcont text
2846 else TEcont (text ^ toutf8 key)
2849 let reqlayout angle fitmodel =
2850 match state.throttle with
2851 | None ->
2852 if nogeomcmds state.geomcmds
2853 then state.anchor <- getanchor ();
2854 conf.angle <- angle mod 360;
2855 if conf.angle != 0
2856 then (
2857 match state.mode with
2858 | LinkNav _ -> state.mode <- View
2859 | _ -> ()
2861 conf.fitmodel <- fitmodel;
2862 invalidate "reqlayout"
2863 (fun () ->
2864 wcmd "reqlayout %d %d %d"
2865 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2867 | _ -> ()
2870 let settrim trimmargins trimfuzz =
2871 if nogeomcmds state.geomcmds
2872 then state.anchor <- getanchor ();
2873 conf.trimmargins <- trimmargins;
2874 conf.trimfuzz <- trimfuzz;
2875 let x0, y0, x1, y1 = trimfuzz in
2876 invalidate "settrim"
2877 (fun () ->
2878 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2879 flushpages ();
2882 let setzoom zoom =
2883 match state.throttle with
2884 | None ->
2885 let zoom = max 0.0001 zoom in
2886 if zoom <> conf.zoom
2887 then (
2888 state.prevzoom <- conf.zoom;
2889 conf.zoom <- zoom;
2890 reshape state.winw state.winh;
2891 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2894 | Some (layout, y, started) ->
2895 let time =
2896 match conf.maxwait with
2897 | None -> 0.0
2898 | Some t -> t
2900 let dt = now () -. started in
2901 if dt > time
2902 then (
2903 state.y <- y;
2904 load layout;
2908 let setcolumns mode columns coverA coverB =
2909 state.prevcolumns <- Some (conf.columns, conf.zoom);
2910 if columns < 0
2911 then (
2912 if isbirdseye mode
2913 then showtext '!' "split mode doesn't work in bird's eye"
2914 else (
2915 conf.columns <- Csplit (-columns, [||]);
2916 state.x <- 0;
2917 conf.zoom <- 1.0;
2920 else (
2921 if columns < 2
2922 then (
2923 conf.columns <- Csingle [||];
2924 state.x <- 0;
2925 setzoom 1.0;
2927 else (
2928 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2929 conf.zoom <- 1.0;
2932 reshape state.winw state.winh;
2935 let enterbirdseye () =
2936 let zoom = float conf.thumbw /. float state.winw in
2937 let birdseyepageno =
2938 let cy = state.winh / 2 in
2939 let fold = function
2940 | [] -> 0
2941 | l :: rest ->
2942 let rec fold best = function
2943 | [] -> best.pageno
2944 | l :: rest ->
2945 let d = cy - (l.pagedispy + l.pagevh/2)
2946 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2947 if abs d < abs dbest
2948 then fold l rest
2949 else best.pageno
2950 in fold l rest
2952 fold state.layout
2954 state.mode <- Birdseye (
2955 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2957 conf.zoom <- zoom;
2958 conf.presentation <- false;
2959 conf.interpagespace <- 10;
2960 conf.hlinks <- false;
2961 conf.fitmodel <- FitProportional;
2962 state.x <- 0;
2963 state.mstate <- Mnone;
2964 conf.maxwait <- None;
2965 conf.columns <- (
2966 match conf.beyecolumns with
2967 | Some c ->
2968 conf.zoom <- 1.0;
2969 Cmulti ((c, 0, 0), [||])
2970 | None -> Csingle [||]
2972 Wsi.setcursor Wsi.CURSOR_INHERIT;
2973 if conf.verbose
2974 then
2975 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2976 (100.0*.zoom)
2977 else
2978 state.text <- ""
2980 reshape state.winw state.winh;
2983 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2984 state.mode <- View;
2985 conf.zoom <- c.zoom;
2986 conf.presentation <- c.presentation;
2987 conf.interpagespace <- c.interpagespace;
2988 conf.maxwait <- c.maxwait;
2989 conf.hlinks <- c.hlinks;
2990 conf.fitmodel <- c.fitmodel;
2991 conf.beyecolumns <- (
2992 match conf.columns with
2993 | Cmulti ((c, _, _), _) -> Some c
2994 | Csingle _ -> None
2995 | Csplit _ -> failwith "leaving bird's eye split mode"
2997 conf.columns <- (
2998 match c.columns with
2999 | Cmulti (c, _) -> Cmulti (c, [||])
3000 | Csingle _ -> Csingle [||]
3001 | Csplit (c, _) -> Csplit (c, [||])
3003 state.x <- leftx;
3004 if conf.verbose
3005 then
3006 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3007 (100.0*.conf.zoom)
3009 reshape state.winw state.winh;
3010 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3013 let togglebirdseye () =
3014 match state.mode with
3015 | Birdseye vals -> leavebirdseye vals true
3016 | View -> enterbirdseye ()
3017 | _ -> ()
3020 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3021 let pageno = max 0 (pageno - incr) in
3022 let rec loop = function
3023 | [] -> gotopage1 pageno 0
3024 | l :: _ when l.pageno = pageno ->
3025 if l.pagedispy >= 0 && l.pagey = 0
3026 then G.postRedisplay "upbirdseye"
3027 else gotopage1 pageno 0
3028 | _ :: rest -> loop rest
3030 loop state.layout;
3031 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3034 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3035 let pageno = min (state.pagecount - 1) (pageno + incr) in
3036 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3037 let rec loop = function
3038 | [] ->
3039 let y, h = getpageyh pageno in
3040 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3041 gotoy (clamp dy)
3042 | l :: _ when l.pageno = pageno ->
3043 if l.pagevh != l.pageh
3044 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3045 else G.postRedisplay "downbirdseye"
3046 | _ :: rest -> loop rest
3048 loop state.layout
3051 let optentry mode _ key =
3052 let btos b = if b then "on" else "off" in
3053 if key >= 32 && key < 127
3054 then
3055 let c = Char.chr key in
3056 match c with
3057 | 's' ->
3058 let ondone s =
3059 try conf.scrollstep <- int_of_string s with exc ->
3060 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3062 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3064 | 'A' ->
3065 let ondone s =
3067 conf.autoscrollstep <- int_of_string s;
3068 if state.autoscroll <> None
3069 then state.autoscroll <- Some conf.autoscrollstep
3070 with exc ->
3071 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3073 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3075 | 'C' ->
3076 let ondone s =
3078 let n, a, b = multicolumns_of_string s in
3079 setcolumns mode n a b;
3080 with exc ->
3081 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3083 TEswitch ("columns: ", "", None, textentry, ondone, true)
3085 | 'Z' ->
3086 let ondone s =
3088 let zoom = float (int_of_string s) /. 100.0 in
3089 setzoom zoom
3090 with exc ->
3091 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3093 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3095 | 't' ->
3096 let ondone s =
3098 conf.thumbw <- bound (int_of_string s) 2 4096;
3099 state.text <-
3100 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3101 begin match mode with
3102 | Birdseye beye ->
3103 leavebirdseye beye false;
3104 enterbirdseye ();
3105 | _ -> ();
3107 with exc ->
3108 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3110 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3112 | 'R' ->
3113 let ondone s =
3114 match try
3115 Some (int_of_string s)
3116 with exc ->
3117 state.text <- Printf.sprintf "bad integer `%s': %s"
3118 s (exntos exc);
3119 None
3120 with
3121 | Some angle -> reqlayout angle conf.fitmodel
3122 | None -> ()
3124 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3126 | 'i' ->
3127 conf.icase <- not conf.icase;
3128 TEdone ("case insensitive search " ^ (btos conf.icase))
3130 | 'p' ->
3131 conf.preload <- not conf.preload;
3132 gotoy state.y;
3133 TEdone ("preload " ^ (btos conf.preload))
3135 | 'v' ->
3136 conf.verbose <- not conf.verbose;
3137 TEdone ("verbose " ^ (btos conf.verbose))
3139 | 'd' ->
3140 conf.debug <- not conf.debug;
3141 TEdone ("debug " ^ (btos conf.debug))
3143 | 'h' ->
3144 conf.maxhfit <- not conf.maxhfit;
3145 state.maxy <- calcheight ();
3146 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3148 | 'c' ->
3149 conf.crophack <- not conf.crophack;
3150 TEdone ("crophack " ^ btos conf.crophack)
3152 | 'a' ->
3153 let s =
3154 match conf.maxwait with
3155 | None ->
3156 conf.maxwait <- Some infinity;
3157 "always wait for page to complete"
3158 | Some _ ->
3159 conf.maxwait <- None;
3160 "show placeholder if page is not ready"
3162 TEdone s
3164 | 'f' ->
3165 conf.underinfo <- not conf.underinfo;
3166 TEdone ("underinfo " ^ btos conf.underinfo)
3168 | 'P' ->
3169 conf.savebmarks <- not conf.savebmarks;
3170 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3172 | 'S' ->
3173 let ondone s =
3175 let pageno, py =
3176 match state.layout with
3177 | [] -> 0, 0
3178 | l :: _ ->
3179 l.pageno, l.pagey
3181 conf.interpagespace <- int_of_string s;
3182 docolumns conf.columns;
3183 state.maxy <- calcheight ();
3184 let y = getpagey pageno in
3185 gotoy (y + py)
3186 with exc ->
3187 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3189 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3191 | 'l' ->
3192 let fm =
3193 match conf.fitmodel with
3194 | FitProportional -> FitWidth
3195 | _ -> FitProportional
3197 reqlayout conf.angle fm;
3198 TEdone ("proportional display " ^ btos (fm == FitProportional))
3200 | 'T' ->
3201 settrim (not conf.trimmargins) conf.trimfuzz;
3202 TEdone ("trim margins " ^ btos conf.trimmargins)
3204 | 'I' ->
3205 conf.invert <- not conf.invert;
3206 TEdone ("invert colors " ^ btos conf.invert)
3208 | 'x' ->
3209 let ondone s =
3210 cbput state.hists.sel s;
3211 conf.selcmd <- s;
3213 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3214 textentry, ondone, true)
3216 | 'M' ->
3217 if conf.pax == None
3218 then conf.pax <- Some (ref (0.0, 0, 0))
3219 else conf.pax <- None;
3220 TEdone ("PAX " ^ btos (conf.pax != None))
3222 | 'B' ->
3223 state.bzoom <- not state.bzoom;
3224 state.rects <- [];
3225 TEdone ("block zoom " ^ btos (state.bzoom))
3227 | _ ->
3228 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3229 TEstop
3230 else
3231 TEcont state.text
3234 class type lvsource = object
3235 method getitemcount : int
3236 method getitem : int -> (string * int)
3237 method hasaction : int -> bool
3238 method exit :
3239 uioh:uioh ->
3240 cancel:bool ->
3241 active:int ->
3242 first:int ->
3243 pan:int ->
3244 qsearch:string ->
3245 uioh option
3246 method getactive : int
3247 method getfirst : int
3248 method getqsearch : string
3249 method setqsearch : string -> unit
3250 method getpan : int
3251 end;;
3253 class virtual lvsourcebase = object
3254 val mutable m_active = 0
3255 val mutable m_first = 0
3256 val mutable m_qsearch = ""
3257 val mutable m_pan = 0
3258 method getactive = m_active
3259 method getfirst = m_first
3260 method getqsearch = m_qsearch
3261 method getpan = m_pan
3262 method setqsearch s = m_qsearch <- s
3263 end;;
3265 let withoutlastutf8 s =
3266 let len = String.length s in
3267 if len = 0
3268 then s
3269 else
3270 let rec find pos =
3271 if pos = 0
3272 then pos
3273 else
3274 let b = Char.code s.[pos] in
3275 if b land 0b11000000 = 0b11000000
3276 then pos
3277 else find (pos-1)
3279 let first =
3280 if Char.code s.[len-1] land 0x80 = 0
3281 then len-1
3282 else find (len-1)
3284 String.sub s 0 first;
3287 let textentrykeyboard
3288 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3289 let key =
3290 if key >= 0xffb0 && key <= 0xffb9
3291 then key - 0xffb0 + 48 else key
3293 let enttext te =
3294 state.mode <- Textentry (te, onleave);
3295 state.text <- "";
3296 enttext ();
3297 G.postRedisplay "textentrykeyboard enttext";
3299 let histaction cmd =
3300 match opthist with
3301 | None -> ()
3302 | Some (action, _) ->
3303 state.mode <- Textentry (
3304 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3306 G.postRedisplay "textentry histaction"
3308 match key with
3309 | 0xff08 -> (* backspace *)
3310 let s = withoutlastutf8 text in
3311 let len = String.length s in
3312 if cancelonempty && len = 0
3313 then (
3314 onleave Cancel;
3315 G.postRedisplay "textentrykeyboard after cancel";
3317 else (
3318 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3321 | 0xff0d | 0xff8d -> (* (kp) enter *)
3322 ondone text;
3323 onleave Confirm;
3324 G.postRedisplay "textentrykeyboard after confirm"
3326 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3327 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3328 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3329 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3331 | 0xff1b -> (* escape*)
3332 if String.length text = 0
3333 then (
3334 begin match opthist with
3335 | None -> ()
3336 | Some (_, onhistcancel) -> onhistcancel ()
3337 end;
3338 onleave Cancel;
3339 state.text <- "";
3340 G.postRedisplay "textentrykeyboard after cancel2"
3342 else (
3343 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3346 | 0xff9f | 0xffff -> () (* delete *)
3348 | _ when key != 0
3349 && key land 0xff00 != 0xff00 (* keyboard *)
3350 && key land 0xfe00 != 0xfe00 (* xkb *)
3351 && key land 0xfd00 != 0xfd00 (* 3270 *)
3353 begin match onkey text key with
3354 | TEdone text ->
3355 ondone text;
3356 onleave Confirm;
3357 G.postRedisplay "textentrykeyboard after confirm2";
3359 | TEcont text ->
3360 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3362 | TEstop ->
3363 onleave Cancel;
3364 G.postRedisplay "textentrykeyboard after cancel3"
3366 | TEswitch te ->
3367 state.mode <- Textentry (te, onleave);
3368 G.postRedisplay "textentrykeyboard switch";
3369 end;
3371 | _ ->
3372 vlog "unhandled key %s" (Wsi.keyname key)
3375 let firstof first active =
3376 if first > active || abs (first - active) > fstate.maxrows - 1
3377 then max 0 (active - (fstate.maxrows/2))
3378 else first
3381 let calcfirst first active =
3382 if active > first
3383 then
3384 let rows = active - first in
3385 if rows > fstate.maxrows then active - fstate.maxrows else first
3386 else active
3389 let scrollph y maxy =
3390 let sh = float (maxy + state.winh) /. float state.winh in
3391 let sh = float state.winh /. sh in
3392 let sh = max sh (float conf.scrollh) in
3394 let percent = float y /. float maxy in
3395 let position = (float state.winh -. sh) *. percent in
3397 let position =
3398 if position +. sh > float state.winh
3399 then float state.winh -. sh
3400 else position
3402 position, sh;
3405 let coe s = (s :> uioh);;
3407 class listview ~(source:lvsource) ~trusted ~modehash =
3408 object (self)
3409 val m_pan = source#getpan
3410 val m_first = source#getfirst
3411 val m_active = source#getactive
3412 val m_qsearch = source#getqsearch
3413 val m_prev_uioh = state.uioh
3415 method private elemunder y =
3416 let n = y / (fstate.fontsize+1) in
3417 if m_first + n < source#getitemcount
3418 then (
3419 if source#hasaction (m_first + n)
3420 then Some (m_first + n)
3421 else None
3423 else None
3425 method display =
3426 Gl.enable `blend;
3427 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3428 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3429 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3430 GlDraw.color (1., 1., 1.);
3431 Gl.enable `texture_2d;
3432 let fs = fstate.fontsize in
3433 let nfs = fs + 1 in
3434 let ww = fstate.wwidth in
3435 let tabw = 30.0*.ww in
3436 let itemcount = source#getitemcount in
3437 let rec loop row =
3438 if (row - m_first) > fstate.maxrows
3439 then ()
3440 else (
3441 if row >= 0 && row < itemcount
3442 then (
3443 let (s, level) = source#getitem row in
3444 let y = (row - m_first) * nfs in
3445 let x = 5.0 +. float (level + m_pan) *. ww in
3446 if row = m_active
3447 then (
3448 Gl.disable `texture_2d;
3449 GlDraw.polygon_mode `both `line;
3450 let alpha = if source#hasaction row then 0.9 else 0.3 in
3451 GlDraw.color (1., 1., 1.) ~alpha;
3452 GlDraw.rect (1., float (y + 1))
3453 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3454 GlDraw.polygon_mode `both `fill;
3455 GlDraw.color (1., 1., 1.);
3456 Gl.enable `texture_2d;
3459 let drawtabularstring s =
3460 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3461 if trusted
3462 then
3463 let tabpos = try String.index s '\t' with Not_found -> -1 in
3464 if tabpos > 0
3465 then
3466 let len = String.length s - tabpos - 1 in
3467 let s1 = String.sub s 0 tabpos
3468 and s2 = String.sub s (tabpos + 1) len in
3469 let nx = drawstr x s1 in
3470 let sw = nx -. x in
3471 let x = x +. (max tabw sw) in
3472 drawstr x s2
3473 else
3474 drawstr x s
3475 else
3476 drawstr x s
3478 let _ = drawtabularstring s in
3479 loop (row+1)
3483 loop m_first;
3484 Gl.disable `blend;
3485 Gl.disable `texture_2d;
3487 method updownlevel incr =
3488 let len = source#getitemcount in
3489 let curlevel =
3490 if m_active >= 0 && m_active < len
3491 then snd (source#getitem m_active)
3492 else -1
3494 let rec flow i =
3495 if i = len then i-1 else if i = -1 then 0 else
3496 let _, l = source#getitem i in
3497 if l != curlevel then i else flow (i+incr)
3499 let active = flow m_active in
3500 let first = calcfirst m_first active in
3501 G.postRedisplay "outline updownlevel";
3502 {< m_active = active; m_first = first >}
3504 method private key1 key mask =
3505 let set1 active first qsearch =
3506 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3508 let search active pattern incr =
3509 let active = if active = -1 then m_first else active in
3510 let dosearch re =
3511 let rec loop n =
3512 if n >= 0 && n < source#getitemcount
3513 then (
3514 let s, _ = source#getitem n in
3516 (try ignore (Str.search_forward re s 0); true
3517 with Not_found -> false)
3518 then Some n
3519 else loop (n + incr)
3521 else None
3523 loop active
3526 let re = Str.regexp_case_fold pattern in
3527 dosearch re
3528 with Failure s ->
3529 state.text <- s;
3530 None
3532 let itemcount = source#getitemcount in
3533 let find start incr =
3534 let rec find i =
3535 if i = -1 || i = itemcount
3536 then -1
3537 else (
3538 if source#hasaction i
3539 then i
3540 else find (i + incr)
3543 find start
3545 let set active first =
3546 let first = bound first 0 (itemcount - fstate.maxrows) in
3547 state.text <- "";
3548 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3550 let navigate incr =
3551 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3552 let active, first =
3553 let incr1 = if incr > 0 then 1 else -1 in
3554 if isvisible m_first m_active
3555 then
3556 let next =
3557 let next = m_active + incr in
3558 let next =
3559 if next < 0 || next >= itemcount
3560 then -1
3561 else find next incr1
3563 if abs (m_active - next) > fstate.maxrows
3564 then -1
3565 else next
3567 if next = -1
3568 then
3569 let first = m_first + incr in
3570 let first = bound first 0 (itemcount - fstate.maxrows) in
3571 let next =
3572 let next = m_active + incr in
3573 let next = bound next 0 (itemcount - 1) in
3574 find next ~-incr1
3576 let active =
3577 if next = -1
3578 then m_active
3579 else (
3580 if isvisible first next
3581 then next
3582 else m_active
3585 active, first
3586 else
3587 let first = min next m_first in
3588 let first =
3589 if abs (next - first) > fstate.maxrows
3590 then first + incr
3591 else first
3593 next, first
3594 else
3595 let first = m_first + incr in
3596 let first = bound first 0 (itemcount - 1) in
3597 let active =
3598 let next = m_active + incr in
3599 let next = bound next 0 (itemcount - 1) in
3600 let next = find next incr1 in
3601 let active =
3602 if next = -1 || abs (m_active - first) > fstate.maxrows
3603 then (
3604 let active = if m_active = -1 then next else m_active in
3605 active
3607 else next
3609 if isvisible first active
3610 then active
3611 else -1
3613 active, first
3615 G.postRedisplay "listview navigate";
3616 set active first;
3618 match key with
3619 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3620 let incr = if key = 0x72 then -1 else 1 in
3621 let active, first =
3622 match search (m_active + incr) m_qsearch incr with
3623 | None ->
3624 state.text <- m_qsearch ^ " [not found]";
3625 m_active, m_first
3626 | Some active ->
3627 state.text <- m_qsearch;
3628 active, firstof m_first active
3630 G.postRedisplay "listview ctrl-r/s";
3631 set1 active first m_qsearch;
3633 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3634 if m_active >= 0 && m_active < source#getitemcount
3635 then (
3636 let s, _ = source#getitem m_active in
3637 selstring s;
3639 coe self
3641 | 0xff08 -> (* backspace *)
3642 if String.length m_qsearch = 0
3643 then coe self
3644 else (
3645 let qsearch = withoutlastutf8 m_qsearch in
3646 let len = String.length qsearch in
3647 if len = 0
3648 then (
3649 state.text <- "";
3650 G.postRedisplay "listview empty qsearch";
3651 set1 m_active m_first "";
3653 else
3654 let active, first =
3655 match search m_active qsearch ~-1 with
3656 | None ->
3657 state.text <- qsearch ^ " [not found]";
3658 m_active, m_first
3659 | Some active ->
3660 state.text <- qsearch;
3661 active, firstof m_first active
3663 G.postRedisplay "listview backspace qsearch";
3664 set1 active first qsearch
3667 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3668 let pattern = m_qsearch ^ toutf8 key in
3669 let active, first =
3670 match search m_active pattern 1 with
3671 | None ->
3672 state.text <- pattern ^ " [not found]";
3673 m_active, m_first
3674 | Some active ->
3675 state.text <- pattern;
3676 active, firstof m_first active
3678 G.postRedisplay "listview qsearch add";
3679 set1 active first pattern;
3681 | 0xff1b -> (* escape *)
3682 state.text <- "";
3683 if String.length m_qsearch = 0
3684 then (
3685 G.postRedisplay "list view escape";
3686 begin
3687 match
3688 source#exit (coe self) true m_active m_first m_pan m_qsearch
3689 with
3690 | None -> m_prev_uioh
3691 | Some uioh -> uioh
3694 else (
3695 G.postRedisplay "list view kill qsearch";
3696 source#setqsearch "";
3697 coe {< m_qsearch = "" >}
3700 | 0xff0d | 0xff8d -> (* (kp) enter *)
3701 state.text <- "";
3702 let self = {< m_qsearch = "" >} in
3703 source#setqsearch "";
3704 let opt =
3705 G.postRedisplay "listview enter";
3706 if m_active >= 0 && m_active < source#getitemcount
3707 then (
3708 source#exit (coe self) false m_active m_first m_pan "";
3710 else (
3711 source#exit (coe self) true m_active m_first m_pan "";
3714 begin match opt with
3715 | None -> m_prev_uioh
3716 | Some uioh -> uioh
3719 | 0xff9f | 0xffff -> (* (kp) delete *)
3720 coe self
3722 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3723 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3724 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3725 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3727 | 0xff53 | 0xff98 -> (* (kp) right *)
3728 state.text <- "";
3729 G.postRedisplay "listview right";
3730 coe {< m_pan = m_pan - 1 >}
3732 | 0xff51 | 0xff96 -> (* (kp) left *)
3733 state.text <- "";
3734 G.postRedisplay "listview left";
3735 coe {< m_pan = m_pan + 1 >}
3737 | 0xff50 | 0xff95 -> (* (kp) home *)
3738 let active = find 0 1 in
3739 G.postRedisplay "listview home";
3740 set active 0;
3742 | 0xff57 | 0xff9c -> (* (kp) end *)
3743 let first = max 0 (itemcount - fstate.maxrows) in
3744 let active = find (itemcount - 1) ~-1 in
3745 G.postRedisplay "listview end";
3746 set active first;
3748 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3749 coe self
3751 | _ ->
3752 dolog "listview unknown key %#x" key; coe self
3754 method key key mask =
3755 match state.mode with
3756 | Textentry te -> textentrykeyboard key mask te; coe self
3757 | _ -> self#key1 key mask
3759 method button button down x y _ =
3760 let opt =
3761 match button with
3762 | 1 when x > state.winw - conf.scrollbw ->
3763 G.postRedisplay "listview scroll";
3764 if down
3765 then
3766 let _, position, sh = self#scrollph in
3767 if y > truncate position && y < truncate (position +. sh)
3768 then (
3769 state.mstate <- Mscrolly;
3770 Some (coe self)
3772 else
3773 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3774 let first = truncate (s *. float source#getitemcount) in
3775 let first = min source#getitemcount first in
3776 Some (coe {< m_first = first; m_active = first >})
3777 else (
3778 state.mstate <- Mnone;
3779 Some (coe self);
3781 | 1 when not down ->
3782 begin match self#elemunder y with
3783 | Some n ->
3784 G.postRedisplay "listview click";
3785 source#exit
3786 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3787 | _ ->
3788 Some (coe self)
3790 | n when (n == 4 || n == 5) && not down ->
3791 let len = source#getitemcount in
3792 let first =
3793 if n = 5 && m_first + fstate.maxrows >= len
3794 then
3795 m_first
3796 else
3797 let first = m_first + (if n == 4 then -1 else 1) in
3798 bound first 0 (len - 1)
3800 G.postRedisplay "listview wheel";
3801 Some (coe {< m_first = first >})
3802 | n when (n = 6 || n = 7) && not down ->
3803 let inc = m_first + (if n = 7 then -1 else 1) in
3804 G.postRedisplay "listview hwheel";
3805 Some (coe {< m_pan = m_pan + inc >})
3806 | _ ->
3807 Some (coe self)
3809 match opt with
3810 | None -> m_prev_uioh
3811 | Some uioh -> uioh
3813 method motion _ y =
3814 match state.mstate with
3815 | Mscrolly ->
3816 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3817 let first = truncate (s *. float source#getitemcount) in
3818 let first = min source#getitemcount first in
3819 G.postRedisplay "listview motion";
3820 coe {< m_first = first; m_active = first >}
3821 | _ -> coe self
3823 method pmotion x y =
3824 if x < state.winw - conf.scrollbw
3825 then
3826 let n =
3827 match self#elemunder y with
3828 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3829 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3831 let o =
3832 if n != m_active
3833 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3834 else self
3836 coe o
3837 else (
3838 Wsi.setcursor Wsi.CURSOR_INHERIT;
3839 coe self
3842 method infochanged _ = ()
3844 method scrollpw = (0, 0.0, 0.0)
3845 method scrollph =
3846 let nfs = fstate.fontsize + 1 in
3847 let y = m_first * nfs in
3848 let itemcount = source#getitemcount in
3849 let maxi = max 0 (itemcount - fstate.maxrows) in
3850 let maxy = maxi * nfs in
3851 let p, h = scrollph y maxy in
3852 conf.scrollbw, p, h
3854 method modehash = modehash
3855 method eformsgs = false
3856 end;;
3858 class outlinelistview ~source =
3859 object (self)
3860 inherit listview
3861 ~source:(source :> lvsource)
3862 ~trusted:false
3863 ~modehash:(findkeyhash conf "outline")
3864 as super
3866 method key key mask =
3867 let calcfirst first active =
3868 if active > first
3869 then
3870 let rows = active - first in
3871 let maxrows =
3872 if String.length state.text = 0
3873 then fstate.maxrows
3874 else fstate.maxrows - 2
3876 if rows > maxrows then active - maxrows else first
3877 else active
3879 let navigate incr =
3880 let active = m_active + incr in
3881 let active = bound active 0 (source#getitemcount - 1) in
3882 let first = calcfirst m_first active in
3883 G.postRedisplay "outline navigate";
3884 coe {< m_active = active; m_first = first >}
3886 let ctrl = Wsi.withctrl mask in
3887 match key with
3888 | 110 when ctrl -> (* ctrl-n *)
3889 source#narrow m_qsearch;
3890 G.postRedisplay "outline ctrl-n";
3891 coe {< m_first = 0; m_active = 0 >}
3893 | 117 when ctrl -> (* ctrl-u *)
3894 source#denarrow;
3895 G.postRedisplay "outline ctrl-u";
3896 state.text <- "";
3897 coe {< m_first = 0; m_active = 0 >}
3899 | 108 when ctrl -> (* ctrl-l *)
3900 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3901 G.postRedisplay "outline ctrl-l";
3902 coe {< m_first = first >}
3904 | 0xff9f | 0xffff -> (* (kp) delete *)
3905 source#remove m_active;
3906 G.postRedisplay "outline delete";
3907 let active = max 0 (m_active-1) in
3908 coe {< m_first = firstof m_first active;
3909 m_active = active >}
3911 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3912 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3913 | 0xff55 | 0xff9a -> (* (kp) prior *)
3914 navigate ~-(fstate.maxrows)
3915 | 0xff56 | 0xff9b -> (* (kp) next *)
3916 navigate fstate.maxrows
3918 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3919 let o =
3920 if ctrl
3921 then (
3922 G.postRedisplay "outline ctrl right";
3923 {< m_pan = m_pan + 1 >}
3925 else self#updownlevel 1
3927 coe o
3929 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3930 let o =
3931 if ctrl
3932 then (
3933 G.postRedisplay "outline ctrl left";
3934 {< m_pan = m_pan - 1 >}
3936 else self#updownlevel ~-1
3938 coe o
3940 | 0xff50 | 0xff95 -> (* (kp) home *)
3941 G.postRedisplay "outline home";
3942 coe {< m_first = 0; m_active = 0 >}
3944 | 0xff57 | 0xff9c -> (* (kp) end *)
3945 let active = source#getitemcount - 1 in
3946 let first = max 0 (active - fstate.maxrows) in
3947 G.postRedisplay "outline end";
3948 coe {< m_active = active; m_first = first >}
3950 | _ -> super#key key mask
3953 let outlinesource usebookmarks =
3954 let empty = [||] in
3955 (object
3956 inherit lvsourcebase
3957 val mutable m_items = empty
3958 val mutable m_orig_items = empty
3959 val mutable m_prev_items = empty
3960 val mutable m_narrow_pattern = ""
3961 val mutable m_hadremovals = false
3963 method getitemcount =
3964 Array.length m_items + (if m_hadremovals then 1 else 0)
3966 method getitem n =
3967 if n == Array.length m_items && m_hadremovals
3968 then
3969 ("[Confirm removal]", 0)
3970 else
3971 let s, n, _ = m_items.(n) in
3972 (s, n)
3974 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3975 ignore (uioh, first, qsearch);
3976 let confrimremoval = m_hadremovals && active = Array.length m_items in
3977 let items =
3978 if String.length m_narrow_pattern = 0
3979 then m_orig_items
3980 else m_items
3982 if not cancel
3983 then (
3984 if not confrimremoval
3985 then(
3986 let _, _, anchor = m_items.(active) in
3987 gotoghyll (getanchory anchor);
3988 m_items <- items;
3990 else (
3991 state.bookmarks <- Array.to_list m_items;
3992 m_orig_items <- m_items;
3995 else m_items <- items;
3996 m_pan <- pan;
3997 None
3999 method hasaction _ = true
4001 method greetmsg =
4002 if Array.length m_items != Array.length m_orig_items
4003 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
4004 else ""
4006 method narrow pattern =
4007 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4008 match reopt with
4009 | None -> ()
4010 | Some re ->
4011 let rec loop accu n =
4012 if n = -1
4013 then (
4014 m_narrow_pattern <- pattern;
4015 m_items <- Array.of_list accu
4017 else
4018 let (s, _, _) as o = m_items.(n) in
4019 let accu =
4020 if (try ignore (Str.search_forward re s 0); true
4021 with Not_found -> false)
4022 then o :: accu
4023 else accu
4025 loop accu (n-1)
4027 loop [] (Array.length m_items - 1)
4029 method denarrow =
4030 m_orig_items <- (
4031 if usebookmarks
4032 then Array.of_list state.bookmarks
4033 else state.outlines
4035 m_items <- m_orig_items
4037 method remove m =
4038 if usebookmarks
4039 then
4040 if m >= 0 && m < Array.length m_items
4041 then (
4042 m_hadremovals <- true;
4043 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4044 let n = if n >= m then n+1 else n in
4045 m_items.(n)
4049 method reset anchor items =
4050 m_hadremovals <- false;
4051 if m_orig_items == empty || m_prev_items != items
4052 then (
4053 m_orig_items <- items;
4054 if String.length m_narrow_pattern = 0
4055 then m_items <- items;
4057 m_prev_items <- items;
4058 let rely = getanchory anchor in
4059 let active =
4060 let rec loop n best bestd =
4061 if n = Array.length m_items
4062 then best
4063 else
4064 let (_, _, anchor) = m_items.(n) in
4065 let orely = getanchory anchor in
4066 let d = abs (orely - rely) in
4067 if d < bestd
4068 then loop (n+1) n d
4069 else loop (n+1) best bestd
4071 loop 0 ~-1 max_int
4073 m_active <- active;
4074 m_first <- firstof m_first active
4075 end)
4078 let enterselector usebookmarks =
4079 let source = outlinesource usebookmarks in
4080 fun errmsg ->
4081 let outlines =
4082 if usebookmarks
4083 then Array.of_list state.bookmarks
4084 else state.outlines
4086 if Array.length outlines = 0
4087 then (
4088 showtext ' ' errmsg;
4090 else (
4091 state.text <- source#greetmsg;
4092 Wsi.setcursor Wsi.CURSOR_INHERIT;
4093 let anchor = getanchor () in
4094 source#reset anchor outlines;
4095 state.uioh <- coe (new outlinelistview ~source);
4096 G.postRedisplay "enter selector";
4100 let enteroutlinemode =
4101 let f = enterselector false in
4102 fun ()-> f "Document has no outline";
4105 let enterbookmarkmode =
4106 let f = enterselector true in
4107 fun () -> f "Document has no bookmarks (yet)";
4110 let color_of_string s =
4111 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4112 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4116 let color_to_string (r, g, b) =
4117 let r = truncate (r *. 256.0)
4118 and g = truncate (g *. 256.0)
4119 and b = truncate (b *. 256.0) in
4120 Printf.sprintf "%d/%d/%d" r g b
4123 let irect_of_string s =
4124 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4127 let irect_to_string (x0,y0,x1,y1) =
4128 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4131 let makecheckers () =
4132 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4133 following to say:
4134 converted by Issac Trotts. July 25, 2002 *)
4135 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4136 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4137 let id = GlTex.gen_texture () in
4138 GlTex.bind_texture `texture_2d id;
4139 GlPix.store (`unpack_alignment 1);
4140 GlTex.image2d image;
4141 List.iter (GlTex.parameter ~target:`texture_2d)
4142 [ `mag_filter `nearest; `min_filter `nearest ];
4146 let setcheckers enabled =
4147 match state.texid with
4148 | None ->
4149 if enabled then state.texid <- Some (makecheckers ())
4151 | Some texid ->
4152 if not enabled
4153 then (
4154 GlTex.delete_texture texid;
4155 state.texid <- None;
4159 let int_of_string_with_suffix s =
4160 let l = String.length s in
4161 let s1, shift =
4162 if l > 1
4163 then
4164 let suffix = Char.lowercase s.[l-1] in
4165 match suffix with
4166 | 'k' -> String.sub s 0 (l-1), 10
4167 | 'm' -> String.sub s 0 (l-1), 20
4168 | 'g' -> String.sub s 0 (l-1), 30
4169 | _ -> s, 0
4170 else s, 0
4172 let n = int_of_string s1 in
4173 let m = n lsl shift in
4174 if m < 0 || m < n
4175 then raise (Failure "value too large")
4176 else m
4179 let string_with_suffix_of_int n =
4180 if n = 0
4181 then "0"
4182 else
4183 let n, s =
4184 if n land ((1 lsl 30) - 1) = 0
4185 then n lsr 30, "G"
4186 else (
4187 if n land ((1 lsl 20) - 1) = 0
4188 then n lsr 20, "M"
4189 else (
4190 if n land ((1 lsl 10) - 1) = 0
4191 then n lsr 10, "K"
4192 else n, ""
4196 let rec loop s n =
4197 let h = n mod 1000 in
4198 let n = n / 1000 in
4199 if n = 0
4200 then string_of_int h ^ s
4201 else (
4202 let s = Printf.sprintf "_%03d%s" h s in
4203 loop s n
4206 loop "" n ^ s;
4209 let defghyllscroll = (40, 8, 32);;
4210 let ghyllscroll_of_string s =
4211 let (n, a, b) as nab =
4212 if s = "default"
4213 then defghyllscroll
4214 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4216 if n <= a || n <= b || a >= b
4217 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4218 nab;
4221 let ghyllscroll_to_string ((n, a, b) as nab) =
4222 if nab = defghyllscroll
4223 then "default"
4224 else Printf.sprintf "%d,%d,%d" n a b;
4227 let describe_location () =
4228 let fn = page_of_y state.y in
4229 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4230 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4231 let percent =
4232 if maxy <= 0
4233 then 100.
4234 else (100. *. (float state.y /. float maxy))
4236 if fn = ln
4237 then
4238 Printf.sprintf "page %d of %d [%.2f%%]"
4239 (fn+1) state.pagecount percent
4240 else
4241 Printf.sprintf
4242 "pages %d-%d of %d [%.2f%%]"
4243 (fn+1) (ln+1) state.pagecount percent
4246 let setpresentationmode v =
4247 let n = page_of_y state.y in
4248 state.anchor <- (n, 0.0, 1.0);
4249 conf.presentation <- v;
4250 if conf.fitmodel = FitPage
4251 then reqlayout conf.angle conf.fitmodel;
4252 represent ();
4255 let enterinfomode =
4256 let btos b = if b then "\xe2\x88\x9a" else "" in
4257 let showextended = ref false in
4258 let leave mode = function
4259 | Confirm -> state.mode <- mode
4260 | Cancel -> state.mode <- mode in
4261 let src =
4262 (object
4263 val mutable m_first_time = true
4264 val mutable m_l = []
4265 val mutable m_a = [||]
4266 val mutable m_prev_uioh = nouioh
4267 val mutable m_prev_mode = View
4269 inherit lvsourcebase
4271 method reset prev_mode prev_uioh =
4272 m_a <- Array.of_list (List.rev m_l);
4273 m_l <- [];
4274 m_prev_mode <- prev_mode;
4275 m_prev_uioh <- prev_uioh;
4276 if m_first_time
4277 then (
4278 let rec loop n =
4279 if n >= Array.length m_a
4280 then ()
4281 else
4282 match m_a.(n) with
4283 | _, _, _, Action _ -> m_active <- n
4284 | _ -> loop (n+1)
4286 loop 0;
4287 m_first_time <- false;
4290 method int name get set =
4291 m_l <-
4292 (name, `int get, 1, Action (
4293 fun u ->
4294 let ondone s =
4295 try set (int_of_string s)
4296 with exn ->
4297 state.text <- Printf.sprintf "bad integer `%s': %s"
4298 s (exntos exn)
4300 state.text <- "";
4301 let te = name ^ ": ", "", None, intentry, ondone, true in
4302 state.mode <- Textentry (te, leave m_prev_mode);
4304 )) :: m_l
4306 method int_with_suffix name get set =
4307 m_l <-
4308 (name, `intws get, 1, Action (
4309 fun u ->
4310 let ondone s =
4311 try set (int_of_string_with_suffix s)
4312 with exn ->
4313 state.text <- Printf.sprintf "bad integer `%s': %s"
4314 s (exntos exn)
4316 state.text <- "";
4317 let te =
4318 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4320 state.mode <- Textentry (te, leave m_prev_mode);
4322 )) :: m_l
4324 method bool ?(offset=1) ?(btos=btos) name get set =
4325 m_l <-
4326 (name, `bool (btos, get), offset, Action (
4327 fun u ->
4328 let v = get () in
4329 set (not v);
4331 )) :: m_l
4333 method color name get set =
4334 m_l <-
4335 (name, `color get, 1, Action (
4336 fun u ->
4337 let invalid = (nan, nan, nan) in
4338 let ondone s =
4339 let c =
4340 try color_of_string s
4341 with exn ->
4342 state.text <- Printf.sprintf "bad color `%s': %s"
4343 s (exntos exn);
4344 invalid
4346 if c <> invalid
4347 then set c;
4349 let te = name ^ ": ", "", None, textentry, ondone, true in
4350 state.text <- color_to_string (get ());
4351 state.mode <- Textentry (te, leave m_prev_mode);
4353 )) :: m_l
4355 method string name get set =
4356 m_l <-
4357 (name, `string get, 1, Action (
4358 fun u ->
4359 let ondone s = set s in
4360 let te = name ^ ": ", "", None, textentry, ondone, true in
4361 state.mode <- Textentry (te, leave m_prev_mode);
4363 )) :: m_l
4365 method colorspace name get set =
4366 m_l <-
4367 (name, `string get, 1, Action (
4368 fun _ ->
4369 let source =
4370 (object
4371 inherit lvsourcebase
4373 initializer
4374 m_active <- CSTE.to_int conf.colorspace;
4375 m_first <- 0;
4377 method getitemcount =
4378 Array.length CSTE.names
4379 method getitem n =
4380 (CSTE.names.(n), 0)
4381 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4382 ignore (uioh, first, pan, qsearch);
4383 if not cancel then set active;
4384 None
4385 method hasaction _ = true
4386 end)
4388 state.text <- "";
4389 let modehash = findkeyhash conf "info" in
4390 coe (new listview ~source ~trusted:true ~modehash)
4391 )) :: m_l
4393 method paxmark name get set =
4394 m_l <-
4395 (name, `string get, 1, Action (
4396 fun _ ->
4397 let source =
4398 (object
4399 inherit lvsourcebase
4401 initializer
4402 m_active <- MTE.to_int conf.paxmark;
4403 m_first <- 0;
4405 method getitemcount = Array.length MTE.names
4406 method getitem n = (MTE.names.(n), 0)
4407 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4408 ignore (uioh, first, pan, qsearch);
4409 if not cancel then set active;
4410 None
4411 method hasaction _ = true
4412 end)
4414 state.text <- "";
4415 let modehash = findkeyhash conf "info" in
4416 coe (new listview ~source ~trusted:true ~modehash)
4417 )) :: m_l
4419 method fitmodel name get set =
4420 m_l <-
4421 (name, `string get, 1, Action (
4422 fun _ ->
4423 let source =
4424 (object
4425 inherit lvsourcebase
4427 initializer
4428 m_active <- FMTE.to_int conf.fitmodel;
4429 m_first <- 0;
4431 method getitemcount = Array.length FMTE.names
4432 method getitem n = (FMTE.names.(n), 0)
4433 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4434 ignore (uioh, first, pan, qsearch);
4435 if not cancel then set active;
4436 None
4437 method hasaction _ = true
4438 end)
4440 state.text <- "";
4441 let modehash = findkeyhash conf "info" in
4442 coe (new listview ~source ~trusted:true ~modehash)
4443 )) :: m_l
4445 method caption s offset =
4446 m_l <- (s, `empty, offset, Noaction) :: m_l
4448 method caption2 s f offset =
4449 m_l <- (s, `string f, offset, Noaction) :: m_l
4451 method getitemcount = Array.length m_a
4453 method getitem n =
4454 let tostr = function
4455 | `int f -> string_of_int (f ())
4456 | `intws f -> string_with_suffix_of_int (f ())
4457 | `string f -> f ()
4458 | `color f -> color_to_string (f ())
4459 | `bool (btos, f) -> btos (f ())
4460 | `empty -> ""
4462 let name, t, offset, _ = m_a.(n) in
4463 ((let s = tostr t in
4464 if String.length s > 0
4465 then Printf.sprintf "%s\t%s" name s
4466 else name),
4467 offset)
4469 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4470 let uiohopt =
4471 if not cancel
4472 then (
4473 m_qsearch <- qsearch;
4474 let uioh =
4475 match m_a.(active) with
4476 | _, _, _, Action f -> f uioh
4477 | _ -> uioh
4479 Some uioh
4481 else None
4483 m_active <- active;
4484 m_first <- first;
4485 m_pan <- pan;
4486 uiohopt
4488 method hasaction n =
4489 match m_a.(n) with
4490 | _, _, _, Action _ -> true
4491 | _ -> false
4492 end)
4494 let rec fillsrc prevmode prevuioh =
4495 let sep () = src#caption "" 0 in
4496 let colorp name get set =
4497 src#string name
4498 (fun () -> color_to_string (get ()))
4499 (fun v ->
4501 let c = color_of_string v in
4502 set c
4503 with exn ->
4504 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4507 let oldmode = state.mode in
4508 let birdseye = isbirdseye state.mode in
4510 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4512 src#bool "presentation mode"
4513 (fun () -> conf.presentation)
4514 (fun v -> setpresentationmode v);
4516 src#bool "ignore case in searches"
4517 (fun () -> conf.icase)
4518 (fun v -> conf.icase <- v);
4520 src#bool "preload"
4521 (fun () -> conf.preload)
4522 (fun v -> conf.preload <- v);
4524 src#bool "highlight links"
4525 (fun () -> conf.hlinks)
4526 (fun v -> conf.hlinks <- v);
4528 src#bool "under info"
4529 (fun () -> conf.underinfo)
4530 (fun v -> conf.underinfo <- v);
4532 src#bool "persistent bookmarks"
4533 (fun () -> conf.savebmarks)
4534 (fun v -> conf.savebmarks <- v);
4536 src#fitmodel "fit model"
4537 (fun () -> FMTE.to_string conf.fitmodel)
4538 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4540 src#bool "trim margins"
4541 (fun () -> conf.trimmargins)
4542 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4544 src#bool "persistent location"
4545 (fun () -> conf.jumpback)
4546 (fun v -> conf.jumpback <- v);
4548 sep ();
4549 src#int "inter-page space"
4550 (fun () -> conf.interpagespace)
4551 (fun n ->
4552 conf.interpagespace <- n;
4553 docolumns conf.columns;
4554 let pageno, py =
4555 match state.layout with
4556 | [] -> 0, 0
4557 | l :: _ ->
4558 l.pageno, l.pagey
4560 state.maxy <- calcheight ();
4561 let y = getpagey pageno in
4562 gotoy (y + py)
4565 src#int "page bias"
4566 (fun () -> conf.pagebias)
4567 (fun v -> conf.pagebias <- v);
4569 src#int "scroll step"
4570 (fun () -> conf.scrollstep)
4571 (fun n -> conf.scrollstep <- n);
4573 src#int "horizontal scroll step"
4574 (fun () -> conf.hscrollstep)
4575 (fun v -> conf.hscrollstep <- v);
4577 src#int "auto scroll step"
4578 (fun () ->
4579 match state.autoscroll with
4580 | Some step -> step
4581 | _ -> conf.autoscrollstep)
4582 (fun n ->
4583 if state.autoscroll <> None
4584 then state.autoscroll <- Some n;
4585 conf.autoscrollstep <- n);
4587 src#int "zoom"
4588 (fun () -> truncate (conf.zoom *. 100.))
4589 (fun v -> setzoom ((float v) /. 100.));
4591 src#int "rotation"
4592 (fun () -> conf.angle)
4593 (fun v -> reqlayout v conf.fitmodel);
4595 src#int "scroll bar width"
4596 (fun () -> conf.scrollbw)
4597 (fun v ->
4598 conf.scrollbw <- v;
4599 reshape state.winw state.winh;
4602 src#int "scroll handle height"
4603 (fun () -> conf.scrollh)
4604 (fun v -> conf.scrollh <- v;);
4606 src#int "thumbnail width"
4607 (fun () -> conf.thumbw)
4608 (fun v ->
4609 conf.thumbw <- min 4096 v;
4610 match oldmode with
4611 | Birdseye beye ->
4612 leavebirdseye beye false;
4613 enterbirdseye ()
4614 | _ -> ()
4617 let mode = state.mode in
4618 src#string "columns"
4619 (fun () ->
4620 match conf.columns with
4621 | Csingle _ -> "1"
4622 | Cmulti (multi, _) -> multicolumns_to_string multi
4623 | Csplit (count, _) -> "-" ^ string_of_int count
4625 (fun v ->
4626 let n, a, b = multicolumns_of_string v in
4627 setcolumns mode n a b);
4629 sep ();
4630 src#caption "Pixmap cache" 0;
4631 src#int_with_suffix "size (advisory)"
4632 (fun () -> conf.memlimit)
4633 (fun v -> conf.memlimit <- v);
4635 src#caption2 "used"
4636 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4637 (string_with_suffix_of_int state.memused)
4638 (Hashtbl.length state.tilemap)) 1;
4640 sep ();
4641 src#caption "Layout" 0;
4642 src#caption2 "Dimension"
4643 (fun () ->
4644 Printf.sprintf "%dx%d (virtual %dx%d)"
4645 state.winw state.winh
4646 state.w state.maxy)
4648 if conf.debug
4649 then
4650 src#caption2 "Position" (fun () ->
4651 Printf.sprintf "%dx%d" state.x state.y
4653 else
4654 src#caption2 "Position" (fun () -> describe_location ()) 1
4657 sep ();
4658 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4659 "Save these parameters as global defaults at exit"
4660 (fun () -> conf.bedefault)
4661 (fun v -> conf.bedefault <- v)
4664 sep ();
4665 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4666 src#bool ~offset:0 ~btos "Extended parameters"
4667 (fun () -> !showextended)
4668 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4669 if !showextended
4670 then (
4671 src#bool "checkers"
4672 (fun () -> conf.checkers)
4673 (fun v -> conf.checkers <- v; setcheckers v);
4674 src#bool "update cursor"
4675 (fun () -> conf.updatecurs)
4676 (fun v -> conf.updatecurs <- v);
4677 src#bool "verbose"
4678 (fun () -> conf.verbose)
4679 (fun v -> conf.verbose <- v);
4680 src#bool "invert colors"
4681 (fun () -> conf.invert)
4682 (fun v -> conf.invert <- v);
4683 src#bool "max fit"
4684 (fun () -> conf.maxhfit)
4685 (fun v -> conf.maxhfit <- v);
4686 src#bool "redirect stderr"
4687 (fun () -> conf.redirectstderr)
4688 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4689 src#bool "pax mode"
4690 (fun () -> conf.pax != None)
4691 (fun v ->
4692 if v
4693 then conf.pax <- Some (ref (now (), 0, 0))
4694 else conf.pax <- None);
4695 src#string "uri launcher"
4696 (fun () -> conf.urilauncher)
4697 (fun v -> conf.urilauncher <- v);
4698 src#string "path launcher"
4699 (fun () -> conf.pathlauncher)
4700 (fun v -> conf.pathlauncher <- v);
4701 src#string "tile size"
4702 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4703 (fun v ->
4705 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4706 conf.tilew <- max 64 w;
4707 conf.tileh <- max 64 h;
4708 flushtiles ();
4709 with exn ->
4710 state.text <- Printf.sprintf "bad tile size `%s': %s"
4711 v (exntos exn)
4713 src#int "texture count"
4714 (fun () -> conf.texcount)
4715 (fun v ->
4716 if realloctexts v
4717 then conf.texcount <- v
4718 else showtext '!' " Failed to set texture count please retry later"
4720 src#int "slice height"
4721 (fun () -> conf.sliceheight)
4722 (fun v ->
4723 conf.sliceheight <- v;
4724 wcmd "sliceh %d" conf.sliceheight;
4726 src#int "anti-aliasing level"
4727 (fun () -> conf.aalevel)
4728 (fun v ->
4729 conf.aalevel <- bound v 0 8;
4730 state.anchor <- getanchor ();
4731 opendoc state.path state.password;
4733 src#string "page scroll scaling factor"
4734 (fun () -> string_of_float conf.pgscale)
4735 (fun v ->
4737 let s = float_of_string v in
4738 conf.pgscale <- s
4739 with exn ->
4740 state.text <- Printf.sprintf
4741 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4744 src#int "ui font size"
4745 (fun () -> fstate.fontsize)
4746 (fun v -> setfontsize (bound v 5 100));
4747 src#int "hint font size"
4748 (fun () -> conf.hfsize)
4749 (fun v -> conf.hfsize <- bound v 5 100);
4750 colorp "background color"
4751 (fun () -> conf.bgcolor)
4752 (fun v -> conf.bgcolor <- v);
4753 src#bool "crop hack"
4754 (fun () -> conf.crophack)
4755 (fun v -> conf.crophack <- v);
4756 src#string "trim fuzz"
4757 (fun () -> irect_to_string conf.trimfuzz)
4758 (fun v ->
4760 conf.trimfuzz <- irect_of_string v;
4761 if conf.trimmargins
4762 then settrim true conf.trimfuzz;
4763 with exn ->
4764 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4766 src#string "throttle"
4767 (fun () ->
4768 match conf.maxwait with
4769 | None -> "show place holder if page is not ready"
4770 | Some time ->
4771 if time = infinity
4772 then "wait for page to fully render"
4773 else
4774 "wait " ^ string_of_float time
4775 ^ " seconds before showing placeholder"
4777 (fun v ->
4779 let f = float_of_string v in
4780 if f <= 0.0
4781 then conf.maxwait <- None
4782 else conf.maxwait <- Some f
4783 with exn ->
4784 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4786 src#string "ghyll scroll"
4787 (fun () ->
4788 match conf.ghyllscroll with
4789 | None -> ""
4790 | Some nab -> ghyllscroll_to_string nab
4792 (fun v ->
4794 let gs =
4795 if String.length v = 0
4796 then None
4797 else Some (ghyllscroll_of_string v)
4799 conf.ghyllscroll <- gs
4800 with exn ->
4801 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4803 src#string "selection command"
4804 (fun () -> conf.selcmd)
4805 (fun v -> conf.selcmd <- v);
4806 src#string "synctex command"
4807 (fun () -> conf.stcmd)
4808 (fun v -> conf.stcmd <- v);
4809 src#string "pax command"
4810 (fun () -> conf.paxcmd)
4811 (fun v -> conf.paxcmd <- v);
4812 src#colorspace "color space"
4813 (fun () -> CSTE.to_string conf.colorspace)
4814 (fun v ->
4815 conf.colorspace <- CSTE.of_int v;
4816 wcmd "cs %d" v;
4817 load state.layout;
4819 src#paxmark "pax mark method"
4820 (fun () -> MTE.to_string conf.paxmark)
4821 (fun v -> conf.paxmark <- MTE.of_int v);
4822 if pbousable ()
4823 then
4824 src#bool "use PBO"
4825 (fun () -> conf.usepbo)
4826 (fun v -> conf.usepbo <- v);
4827 src#bool "mouse wheel scrolls pages"
4828 (fun () -> conf.wheelbypage)
4829 (fun v -> conf.wheelbypage <- v);
4830 src#bool "open remote links in a new instance"
4831 (fun () -> conf.riani)
4832 (fun v -> conf.riani <- v);
4835 sep ();
4836 src#caption "Document" 0;
4837 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4838 src#caption2 "Pages"
4839 (fun () -> string_of_int state.pagecount) 1;
4840 src#caption2 "Dimensions"
4841 (fun () -> string_of_int (List.length state.pdims)) 1;
4842 if conf.trimmargins
4843 then (
4844 sep ();
4845 src#caption "Trimmed margins" 0;
4846 src#caption2 "Dimensions"
4847 (fun () -> string_of_int (List.length state.pdims)) 1;
4850 sep ();
4851 src#caption "OpenGL" 0;
4852 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4853 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4855 sep ();
4856 src#caption "Location" 0;
4857 if String.length state.origin > 0
4858 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4859 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4861 src#reset prevmode prevuioh;
4863 fun () ->
4864 state.text <- "";
4865 let prevmode = state.mode
4866 and prevuioh = state.uioh in
4867 fillsrc prevmode prevuioh;
4868 let source = (src :> lvsource) in
4869 let modehash = findkeyhash conf "info" in
4870 state.uioh <- coe (object (self)
4871 inherit listview ~source ~trusted:true ~modehash as super
4872 val mutable m_prevmemused = 0
4873 method infochanged = function
4874 | Memused ->
4875 if m_prevmemused != state.memused
4876 then (
4877 m_prevmemused <- state.memused;
4878 G.postRedisplay "memusedchanged";
4880 | Pdim -> G.postRedisplay "pdimchanged"
4881 | Docinfo -> fillsrc prevmode prevuioh
4883 method key key mask =
4884 if not (Wsi.withctrl mask)
4885 then
4886 match key with
4887 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4888 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4889 | _ -> super#key key mask
4890 else super#key key mask
4891 end);
4892 G.postRedisplay "info";
4895 let enterhelpmode =
4896 let source =
4897 (object
4898 inherit lvsourcebase
4899 method getitemcount = Array.length state.help
4900 method getitem n =
4901 let s, l, _ = state.help.(n) in
4902 (s, l)
4904 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4905 let optuioh =
4906 if not cancel
4907 then (
4908 m_qsearch <- qsearch;
4909 match state.help.(active) with
4910 | _, _, Action f -> Some (f uioh)
4911 | _ -> Some (uioh)
4913 else None
4915 m_active <- active;
4916 m_first <- first;
4917 m_pan <- pan;
4918 optuioh
4920 method hasaction n =
4921 match state.help.(n) with
4922 | _, _, Action _ -> true
4923 | _ -> false
4925 initializer
4926 m_active <- -1
4927 end)
4928 in fun () ->
4929 let modehash = findkeyhash conf "help" in
4930 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4931 G.postRedisplay "help";
4934 let entermsgsmode =
4935 let msgsource =
4936 let re = Str.regexp "[\r\n]" in
4937 (object
4938 inherit lvsourcebase
4939 val mutable m_items = [||]
4941 method getitemcount = 1 + Array.length m_items
4943 method getitem n =
4944 if n = 0
4945 then "[Clear]", 0
4946 else m_items.(n-1), 0
4948 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4949 ignore uioh;
4950 if not cancel
4951 then (
4952 if active = 0
4953 then Buffer.clear state.errmsgs;
4954 m_qsearch <- qsearch;
4956 m_active <- active;
4957 m_first <- first;
4958 m_pan <- pan;
4959 None
4961 method hasaction n =
4962 n = 0
4964 method reset =
4965 state.newerrmsgs <- false;
4966 let l = Str.split re (Buffer.contents state.errmsgs) in
4967 m_items <- Array.of_list l
4969 initializer
4970 m_active <- 0
4971 end)
4972 in fun () ->
4973 state.text <- "";
4974 msgsource#reset;
4975 let source = (msgsource :> lvsource) in
4976 let modehash = findkeyhash conf "listview" in
4977 state.uioh <- coe (object
4978 inherit listview ~source ~trusted:false ~modehash as super
4979 method display =
4980 if state.newerrmsgs
4981 then msgsource#reset;
4982 super#display
4983 end);
4984 G.postRedisplay "msgs";
4987 let quickbookmark ?title () =
4988 match state.layout with
4989 | [] -> ()
4990 | l :: _ ->
4991 let title =
4992 match title with
4993 | None ->
4994 let sec = Unix.gettimeofday () in
4995 let tm = Unix.localtime sec in
4996 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4997 (l.pageno+1)
4998 tm.Unix.tm_mday
4999 tm.Unix.tm_mon
5000 (tm.Unix.tm_year + 1900)
5001 tm.Unix.tm_hour
5002 tm.Unix.tm_min
5003 | Some title -> title
5005 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5008 let setautoscrollspeed step goingdown =
5009 let incr = max 1 ((abs step) / 2) in
5010 let incr = if goingdown then incr else -incr in
5011 let astep = step + incr in
5012 state.autoscroll <- Some astep;
5015 let gotounder = function
5016 | Ulinkgoto (pageno, top) ->
5017 if pageno >= 0
5018 then (
5019 addnav ();
5020 gotopage1 pageno top;
5023 | Ulinkuri s ->
5024 gotouri s
5026 | Uremote (filename, pageno) ->
5027 let path =
5028 if String.length filename > 0
5029 then
5030 if Filename.is_relative filename
5031 then
5032 let dir = Filename.dirname state.path in
5033 let dir =
5034 if Filename.is_implicit dir
5035 then Filename.concat (Sys.getcwd ()) dir
5036 else dir
5038 Filename.concat dir filename
5039 else filename
5040 else ""
5042 let path =
5043 if Sys.file_exists path
5044 then path
5045 else ""
5047 if String.length path > 0
5048 then (
5049 if conf.riani
5050 then
5051 let command = !selfexec ^ " " ^ path in
5052 try popen command []
5053 with exn ->
5054 Printf.eprintf
5055 "failed to execute `%s': %s\n" command (exntos exn);
5056 flush stderr;
5057 else
5058 let anchor = getanchor () in
5059 let ranchor = state.path, state.password, anchor, state.origin in
5060 state.origin <- "";
5061 state.anchor <- (pageno, 0.0, 0.0);
5062 state.ranchors <- ranchor :: state.ranchors;
5063 opendoc path "";
5065 else showtext '!' ("Could not find " ^ filename)
5067 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5070 let canpan () =
5071 match conf.columns with
5072 | Csplit _ -> true
5073 | _ -> state.x != 0 || conf.zoom > 1.0
5076 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5078 let existsinrow pageno (columns, coverA, coverB) p =
5079 let last = ((pageno - coverA) mod columns) + columns in
5080 let rec any = function
5081 | [] -> false
5082 | l :: rest ->
5083 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5084 then p l
5085 else (
5086 if not (p l)
5087 then (if l.pageno = last then false else any rest)
5088 else true
5091 any state.layout
5094 let nextpage () =
5095 match state.layout with
5096 | [] ->
5097 let pageno = page_of_y state.y in
5098 gotoghyll (getpagey (pageno+1))
5099 | l :: rest ->
5100 match conf.columns with
5101 | Csingle _ ->
5102 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5103 then
5104 let y = clamp (pgscale state.winh) in
5105 gotoghyll y
5106 else
5107 let pageno = min (l.pageno+1) (state.pagecount-1) in
5108 gotoghyll (getpagey pageno)
5109 | Cmulti ((c, _, _) as cl, _) ->
5110 if conf.presentation
5111 && (existsinrow l.pageno cl
5112 (fun l -> l.pageh > l.pagey + l.pagevh))
5113 then
5114 let y = clamp (pgscale state.winh) in
5115 gotoghyll y
5116 else
5117 let pageno = min (l.pageno+c) (state.pagecount-1) in
5118 gotoghyll (getpagey pageno)
5119 | Csplit (n, _) ->
5120 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5121 then
5122 let pagey, pageh = getpageyh l.pageno in
5123 let pagey = pagey + pageh * l.pagecol in
5124 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5125 gotoghyll (pagey + pageh + ips)
5128 let prevpage () =
5129 match state.layout with
5130 | [] ->
5131 let pageno = page_of_y state.y in
5132 gotoghyll (getpagey (pageno-1))
5133 | l :: _ ->
5134 match conf.columns with
5135 | Csingle _ ->
5136 if conf.presentation && l.pagey != 0
5137 then
5138 gotoghyll (clamp (pgscale ~-(state.winh)))
5139 else
5140 let pageno = max 0 (l.pageno-1) in
5141 gotoghyll (getpagey pageno)
5142 | Cmulti ((c, _, coverB) as cl, _) ->
5143 if conf.presentation &&
5144 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5145 then
5146 gotoghyll (clamp (pgscale ~-(state.winh)))
5147 else
5148 let decr =
5149 if l.pageno = state.pagecount - coverB
5150 then 1
5151 else c
5153 let pageno = max 0 (l.pageno-decr) in
5154 gotoghyll (getpagey pageno)
5155 | Csplit (n, _) ->
5156 let y =
5157 if l.pagecol = 0
5158 then
5159 if l.pageno = 0
5160 then l.pagey
5161 else
5162 let pageno = max 0 (l.pageno-1) in
5163 let pagey, pageh = getpageyh pageno in
5164 pagey + (n-1)*pageh
5165 else
5166 let pagey, pageh = getpageyh l.pageno in
5167 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5169 gotoghyll y
5172 let viewkeyboard key mask =
5173 let enttext te =
5174 let mode = state.mode in
5175 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5176 state.text <- "";
5177 enttext ();
5178 G.postRedisplay "view:enttext"
5180 let ctrl = Wsi.withctrl mask in
5181 let key =
5182 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5184 match key with
5185 | 81 -> (* Q *)
5186 exit 0
5188 | 0xff63 -> (* insert *)
5189 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5190 then (
5191 state.mode <- LinkNav (Ltgendir 0);
5192 gotoy state.y;
5194 else showtext '!' "Keyboard link navigation does not work under rotation"
5196 | 0xff1b | 113 -> (* escape / q *)
5197 begin match state.mstate with
5198 | Mzoomrect _ ->
5199 state.mstate <- Mnone;
5200 Wsi.setcursor Wsi.CURSOR_INHERIT;
5201 G.postRedisplay "kill zoom rect";
5202 | _ ->
5203 begin match state.mode with
5204 | LinkNav _ ->
5205 state.mode <- View;
5206 G.postRedisplay "esc leave linknav"
5207 | _ ->
5208 match state.ranchors with
5209 | [] -> raise Quit
5210 | (path, password, anchor, origin) :: rest ->
5211 state.ranchors <- rest;
5212 state.anchor <- anchor;
5213 state.origin <- origin;
5214 opendoc path password
5215 end;
5216 end;
5218 | 0xff08 -> (* backspace *)
5219 gotoghyll (getnav ~-1)
5221 | 111 -> (* o *)
5222 enteroutlinemode ()
5224 | 117 -> (* u *)
5225 state.rects <- [];
5226 state.text <- "";
5227 G.postRedisplay "dehighlight";
5229 | 47 | 63 -> (* / ? *)
5230 let ondone isforw s =
5231 cbput state.hists.pat s;
5232 state.searchpattern <- s;
5233 search s isforw
5235 let s = String.create 1 in
5236 s.[0] <- Char.chr key;
5237 enttext (s, "", Some (onhist state.hists.pat),
5238 textentry, ondone (key = 47), true)
5240 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5241 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5242 setzoom (conf.zoom +. incr)
5244 | 43 | 0xffab -> (* + *)
5245 let ondone s =
5246 let n =
5247 try int_of_string s with exc ->
5248 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5249 max_int
5251 if n != max_int
5252 then (
5253 conf.pagebias <- n;
5254 state.text <- "page bias is now " ^ string_of_int n;
5257 enttext ("page bias: ", "", None, intentry, ondone, true)
5259 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5260 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5261 setzoom (max 0.01 (conf.zoom -. decr))
5263 | 45 | 0xffad -> (* - *)
5264 let ondone msg = state.text <- msg in
5265 enttext (
5266 "option [acfhilpstvxACFPRSZTISMB]: ", "", None,
5267 optentry state.mode, ondone, true
5270 | 48 when ctrl -> (* ctrl-0 *)
5271 if conf.zoom = 1.0
5272 then (
5273 state.x <- 0;
5274 gotoy state.y
5276 else setzoom 1.0
5278 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5279 let cols =
5280 match conf.columns with
5281 | Csingle _ | Cmulti _ -> 1
5282 | Csplit (n, _) -> n
5284 let h = state.winh -
5285 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5287 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5288 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5289 then setzoom zoom
5291 | 51 when ctrl -> (* ctrl-3 *)
5292 let fm =
5293 match conf.fitmodel with
5294 | FitWidth -> FitProportional
5295 | FitProportional -> FitPage
5296 | FitPage -> FitWidth
5298 state.text <- "fit model: " ^ FMTE.to_string fm;
5299 reqlayout conf.angle fm
5301 | 0xffc6 -> (* f9 *)
5302 togglebirdseye ()
5304 | 57 when ctrl -> (* ctrl-9 *)
5305 togglebirdseye ()
5307 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5308 when not ctrl -> (* 0..9 *)
5309 let ondone s =
5310 let n =
5311 try int_of_string s with exc ->
5312 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5315 if n >= 0
5316 then (
5317 addnav ();
5318 cbput state.hists.pag (string_of_int n);
5319 gotopage1 (n + conf.pagebias - 1) 0;
5322 let pageentry text key =
5323 match Char.unsafe_chr key with
5324 | 'g' -> TEdone text
5325 | _ -> intentry text key
5327 let text = "x" in text.[0] <- Char.chr key;
5328 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5330 | 98 -> (* b *)
5331 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5332 reshape state.winw state.winh;
5334 | 108 -> (* l *)
5335 conf.hlinks <- not conf.hlinks;
5336 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5337 G.postRedisplay "toggle highlightlinks";
5339 | 70 -> (* F *)
5340 state.glinks <- true;
5341 let mode = state.mode in
5342 state.mode <- Textentry (
5343 (":", "", None, linknentry, linkndone gotounder, false),
5344 (fun _ ->
5345 state.glinks <- false;
5346 state.mode <- mode)
5348 state.text <- "";
5349 G.postRedisplay "view:linkent(F)"
5351 | 121 -> (* y *)
5352 state.glinks <- true;
5353 let mode = state.mode in
5354 state.mode <- Textentry (
5356 ":", "", None, linknentry, linkndone (fun under ->
5357 selstring (undertext under);
5358 ), false
5360 fun _ ->
5361 state.glinks <- false;
5362 state.mode <- mode
5364 state.text <- "";
5365 G.postRedisplay "view:linkent"
5367 | 97 -> (* a *)
5368 begin match state.autoscroll with
5369 | Some step ->
5370 conf.autoscrollstep <- step;
5371 state.autoscroll <- None
5372 | None ->
5373 if conf.autoscrollstep = 0
5374 then state.autoscroll <- Some 1
5375 else state.autoscroll <- Some conf.autoscrollstep
5378 | 112 when ctrl -> (* ctrl-p *)
5379 launchpath ()
5381 | 80 -> (* P *)
5382 setpresentationmode (not conf.presentation);
5383 showtext ' ' ("presentation mode " ^
5384 if conf.presentation then "on" else "off");
5386 | 102 -> (* f *)
5387 if List.mem Wsi.Fullscreen state.winstate
5388 then Wsi.reshape conf.cwinw conf.cwinh
5389 else Wsi.fullscreen ()
5391 | 112 | 78 -> (* p|N *)
5392 search state.searchpattern false
5394 | 110 | 0xffc0 -> (* n|F3 *)
5395 search state.searchpattern true
5397 | 116 -> (* t *)
5398 begin match state.layout with
5399 | [] -> ()
5400 | l :: _ ->
5401 gotoghyll (getpagey l.pageno)
5404 | 32 -> (* space *)
5405 nextpage ()
5407 | 0xff9f | 0xffff -> (* delete *)
5408 prevpage ()
5410 | 61 -> (* = *)
5411 showtext ' ' (describe_location ());
5413 | 119 -> (* w *)
5414 begin match state.layout with
5415 | [] -> ()
5416 | l :: _ ->
5417 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5418 G.postRedisplay "w"
5421 | 39 -> (* ' *)
5422 enterbookmarkmode ()
5424 | 104 | 0xffbe -> (* h|F1 *)
5425 enterhelpmode ()
5427 | 105 -> (* i *)
5428 enterinfomode ()
5430 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5431 entermsgsmode ()
5433 | 109 -> (* m *)
5434 let ondone s =
5435 match state.layout with
5436 | l :: _ ->
5437 if String.length s > 0
5438 then
5439 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5440 | _ -> ()
5442 enttext ("bookmark: ", "", None, textentry, ondone, true)
5444 | 126 -> (* ~ *)
5445 quickbookmark ();
5446 showtext ' ' "Quick bookmark added";
5448 | 122 -> (* z *)
5449 begin match state.layout with
5450 | l :: _ ->
5451 let rect = getpdimrect l.pagedimno in
5452 let w, h =
5453 if conf.crophack
5454 then
5455 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5456 truncate (1.2 *. (rect.(3) -. rect.(0))))
5457 else
5458 (truncate (rect.(1) -. rect.(0)),
5459 truncate (rect.(3) -. rect.(0)))
5461 let w = truncate ((float w)*.conf.zoom)
5462 and h = truncate ((float h)*.conf.zoom) in
5463 if w != 0 && h != 0
5464 then (
5465 state.anchor <- getanchor ();
5466 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5468 G.postRedisplay "z";
5470 | [] -> ()
5473 | 120 -> state.roam ()
5474 | 60 | 62 -> (* < > *)
5475 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5477 | 91 | 93 -> (* [ ] *)
5478 conf.colorscale <-
5479 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5481 G.postRedisplay "brightness";
5483 | 99 when state.mode = View -> (* [alt-]c *)
5484 if Wsi.withalt mask
5485 then (
5486 if conf.zoom > 1.0
5487 then
5488 let m = (wadjsb state.winw - state.w) / 2 in
5489 state.x <- m;
5490 gotoy_and_clear_text state.y
5492 else
5493 let (c, a, b), z =
5494 match state.prevcolumns with
5495 | None -> (1, 0, 0), 1.0
5496 | Some (columns, z) ->
5497 let cab =
5498 match columns with
5499 | Csplit (c, _) -> -c, 0, 0
5500 | Cmulti ((c, a, b), _) -> c, a, b
5501 | Csingle _ -> 1, 0, 0
5503 cab, z
5505 setcolumns View c a b;
5506 setzoom z
5508 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5509 setzoom state.prevzoom
5511 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5512 begin match state.autoscroll with
5513 | None ->
5514 begin match state.mode with
5515 | Birdseye beye -> upbirdseye 1 beye
5516 | _ ->
5517 if ctrl
5518 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5519 else (
5520 if not (Wsi.withshift mask) && conf.presentation
5521 then prevpage ()
5522 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5525 | Some n ->
5526 setautoscrollspeed n false
5529 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5530 begin match state.autoscroll with
5531 | None ->
5532 begin match state.mode with
5533 | Birdseye beye -> downbirdseye 1 beye
5534 | _ ->
5535 if ctrl
5536 then gotoy_and_clear_text (clamp (state.winh/2))
5537 else (
5538 if not (Wsi.withshift mask) && conf.presentation
5539 then nextpage ()
5540 else gotoy_and_clear_text (clamp conf.scrollstep)
5543 | Some n ->
5544 setautoscrollspeed n true
5547 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5548 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5549 if canpan ()
5550 then
5551 let dx =
5552 if ctrl
5553 then state.winw / 2
5554 else conf.hscrollstep
5556 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5557 state.x <- panbound (state.x + dx);
5558 gotoy_and_clear_text state.y
5559 else (
5560 state.text <- "";
5561 G.postRedisplay "left/right"
5564 | 0xff55 | 0xff9a -> (* (kp) prior *)
5565 let y =
5566 if ctrl
5567 then
5568 match state.layout with
5569 | [] -> state.y
5570 | l :: _ -> state.y - l.pagey
5571 else
5572 clamp (pgscale (-state.winh))
5574 gotoghyll y
5576 | 0xff56 | 0xff9b -> (* (kp) next *)
5577 let y =
5578 if ctrl
5579 then
5580 match List.rev state.layout with
5581 | [] -> state.y
5582 | l :: _ -> getpagey l.pageno
5583 else
5584 clamp (pgscale state.winh)
5586 gotoghyll y
5588 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5589 gotoghyll 0
5590 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5591 gotoghyll (clamp state.maxy)
5593 | 0xff53 | 0xff98
5594 when Wsi.withalt mask -> (* alt-(kp) right *)
5595 gotoghyll (getnav 1)
5596 | 0xff51 | 0xff96
5597 when Wsi.withalt mask -> (* alt-(kp) left *)
5598 gotoghyll (getnav ~-1)
5600 | 114 -> (* r *)
5601 reload ()
5603 | 118 when conf.debug -> (* v *)
5604 state.rects <- [];
5605 List.iter (fun l ->
5606 match getopaque l.pageno with
5607 | None -> ()
5608 | Some opaque ->
5609 let x0, y0, x1, y1 = pagebbox opaque in
5610 let a,b = float x0, float y0 in
5611 let c,d = float x1, float y0 in
5612 let e,f = float x1, float y1 in
5613 let h,j = float x0, float y1 in
5614 let rect = (a,b,c,d,e,f,h,j) in
5615 debugrect rect;
5616 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5617 ) state.layout;
5618 G.postRedisplay "v";
5620 | _ ->
5621 vlog "huh? %s" (Wsi.keyname key)
5624 let linknavkeyboard key mask linknav =
5625 let getpage pageno =
5626 let rec loop = function
5627 | [] -> None
5628 | l :: _ when l.pageno = pageno -> Some l
5629 | _ :: rest -> loop rest
5630 in loop state.layout
5632 let doexact (pageno, n) =
5633 match getopaque pageno, getpage pageno with
5634 | Some opaque, Some l ->
5635 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5636 then
5637 let under = getlink opaque n in
5638 G.postRedisplay "link gotounder";
5639 gotounder under;
5640 state.mode <- View;
5641 else
5642 let opt, dir =
5643 match key with
5644 | 0xff50 -> (* home *)
5645 Some (findlink opaque LDfirst), -1
5647 | 0xff57 -> (* end *)
5648 Some (findlink opaque LDlast), 1
5650 | 0xff51 -> (* left *)
5651 Some (findlink opaque (LDleft n)), -1
5653 | 0xff53 -> (* right *)
5654 Some (findlink opaque (LDright n)), 1
5656 | 0xff52 -> (* up *)
5657 Some (findlink opaque (LDup n)), -1
5659 | 0xff54 -> (* down *)
5660 Some (findlink opaque (LDdown n)), 1
5662 | _ -> None, 0
5664 let pwl l dir =
5665 begin match findpwl l.pageno dir with
5666 | Pwlnotfound -> ()
5667 | Pwl pageno ->
5668 let notfound dir =
5669 state.mode <- LinkNav (Ltgendir dir);
5670 let y, h = getpageyh pageno in
5671 let y =
5672 if dir < 0
5673 then y + h - state.winh
5674 else y
5676 gotoy y
5678 begin match getopaque pageno, getpage pageno with
5679 | Some opaque, Some _ ->
5680 let link =
5681 let ld = if dir > 0 then LDfirst else LDlast in
5682 findlink opaque ld
5684 begin match link with
5685 | Lfound m ->
5686 showlinktype (getlink opaque m);
5687 state.mode <- LinkNav (Ltexact (pageno, m));
5688 G.postRedisplay "linknav jpage";
5689 | _ -> notfound dir
5690 end;
5691 | _ -> notfound dir
5692 end;
5693 end;
5695 begin match opt with
5696 | Some Lnotfound -> pwl l dir;
5697 | Some (Lfound m) ->
5698 if m = n
5699 then pwl l dir
5700 else (
5701 let _, y0, _, y1 = getlinkrect opaque m in
5702 if y0 < l.pagey
5703 then gotopage1 l.pageno y0
5704 else (
5705 let d = fstate.fontsize + 1 in
5706 if y1 - l.pagey > l.pagevh - d
5707 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5708 else G.postRedisplay "linknav";
5710 showlinktype (getlink opaque m);
5711 state.mode <- LinkNav (Ltexact (l.pageno, m));
5714 | None -> viewkeyboard key mask
5715 end;
5716 | _ -> viewkeyboard key mask
5718 if key = 0xff63
5719 then (
5720 state.mode <- View;
5721 G.postRedisplay "leave linknav"
5723 else
5724 match linknav with
5725 | Ltgendir _ -> viewkeyboard key mask
5726 | Ltexact exact -> doexact exact
5729 let keyboard key mask =
5730 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5731 then wcmd "interrupt"
5732 else state.uioh <- state.uioh#key key mask
5735 let birdseyekeyboard key mask
5736 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5737 let incr =
5738 match conf.columns with
5739 | Csingle _ -> 1
5740 | Cmulti ((c, _, _), _) -> c
5741 | Csplit _ -> failwith "bird's eye split mode"
5743 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5744 match key with
5745 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5746 let y, h = getpageyh pageno in
5747 let top = (state.winh - h) / 2 in
5748 gotoy (max 0 (y - top))
5749 | 0xff0d (* enter *)
5750 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5751 | 0xff1b -> leavebirdseye beye true (* escape *)
5752 | 0xff52 -> upbirdseye incr beye (* up *)
5753 | 0xff54 -> downbirdseye incr beye (* down *)
5754 | 0xff51 -> upbirdseye 1 beye (* left *)
5755 | 0xff53 -> downbirdseye 1 beye (* right *)
5757 | 0xff55 -> (* prior *)
5758 begin match state.layout with
5759 | l :: _ ->
5760 if l.pagey != 0
5761 then (
5762 state.mode <- Birdseye (
5763 oconf, leftx, l.pageno, hooverpageno, anchor
5765 gotopage1 l.pageno 0;
5767 else (
5768 let layout = layout (state.y-state.winh) (pgh state.layout) in
5769 match layout with
5770 | [] -> gotoy (clamp (-state.winh))
5771 | l :: _ ->
5772 state.mode <- Birdseye (
5773 oconf, leftx, l.pageno, hooverpageno, anchor
5775 gotopage1 l.pageno 0
5778 | [] -> gotoy (clamp (-state.winh))
5779 end;
5781 | 0xff56 -> (* next *)
5782 begin match List.rev state.layout with
5783 | l :: _ ->
5784 let layout = layout (state.y + (pgh state.layout)) state.winh in
5785 begin match layout with
5786 | [] ->
5787 let incr = l.pageh - l.pagevh in
5788 if incr = 0
5789 then (
5790 state.mode <-
5791 Birdseye (
5792 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5794 G.postRedisplay "birdseye pagedown";
5796 else gotoy (clamp (incr + conf.interpagespace*2));
5798 | l :: _ ->
5799 state.mode <-
5800 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5801 gotopage1 l.pageno 0;
5804 | [] -> gotoy (clamp state.winh)
5805 end;
5807 | 0xff50 -> (* home *)
5808 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5809 gotopage1 0 0
5811 | 0xff57 -> (* end *)
5812 let pageno = state.pagecount - 1 in
5813 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5814 if not (pagevisible state.layout pageno)
5815 then
5816 let h =
5817 match List.rev state.pdims with
5818 | [] -> state.winh
5819 | (_, _, h, _) :: _ -> h
5821 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5822 else G.postRedisplay "birdseye end";
5823 | _ -> viewkeyboard key mask
5826 let drawpage l =
5827 let color =
5828 match state.mode with
5829 | Textentry _ -> scalecolor 0.4
5830 | LinkNav _
5831 | View -> scalecolor 1.0
5832 | Birdseye (_, _, pageno, hooverpageno, _) ->
5833 if l.pageno = hooverpageno
5834 then scalecolor 0.9
5835 else (
5836 if l.pageno = pageno
5837 then scalecolor 1.0
5838 else scalecolor 0.8
5841 drawtiles l color;
5844 let postdrawpage l linkindexbase =
5845 match getopaque l.pageno with
5846 | Some opaque ->
5847 if tileready l l.pagex l.pagey
5848 then
5849 let x = l.pagedispx - l.pagex
5850 and y = l.pagedispy - l.pagey in
5851 let hlmask =
5852 match conf.columns with
5853 | Csingle _ | Cmulti _ ->
5854 (if conf.hlinks then 1 else 0)
5855 + (if state.glinks
5856 && not (isbirdseye state.mode) then 2 else 0)
5857 | _ -> 0
5859 let s =
5860 match state.mode with
5861 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5862 | _ -> ""
5864 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5865 else 0
5866 | _ -> 0
5869 let scrollindicator () =
5870 let sbw, ph, sh = state.uioh#scrollph in
5871 let sbh, pw, sw = state.uioh#scrollpw in
5873 GlDraw.color (0.64, 0.64, 0.64);
5874 GlDraw.rect
5875 (float (state.winw - sbw), 0.)
5876 (float state.winw, float state.winh)
5878 GlDraw.rect
5879 (0., float (state.winh - sbh))
5880 (float (wadjsb state.winw - 1), float state.winh)
5882 GlDraw.color (0.0, 0.0, 0.0);
5884 GlDraw.rect
5885 (float (state.winw - sbw), ph)
5886 (float state.winw, ph +. sh)
5888 GlDraw.rect
5889 (pw, float (state.winh - sbh))
5890 (pw +. sw, float state.winh)
5894 let showsel () =
5895 match state.mstate with
5896 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5899 | Msel ((x0, y0), (x1, y1)) ->
5900 let rec loop = function
5901 | l :: ls ->
5902 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5903 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5904 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5905 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5906 then
5907 match getopaque l.pageno with
5908 | Some opaque ->
5909 let x0, y0 = pagetranslatepoint l x0 y0 in
5910 let x1, y1 = pagetranslatepoint l x1 y1 in
5911 seltext opaque (x0, y0, x1, y1);
5912 | _ -> ()
5913 else loop ls
5914 | [] -> ()
5916 loop state.layout
5919 let showrects rects =
5920 Gl.enable `blend;
5921 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5922 GlDraw.polygon_mode `both `fill;
5923 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5924 List.iter
5925 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5926 List.iter (fun l ->
5927 if l.pageno = pageno
5928 then (
5929 let dx = float (l.pagedispx - l.pagex) in
5930 let dy = float (l.pagedispy - l.pagey) in
5931 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5932 GlDraw.begins `quads;
5934 GlDraw.vertex2 (x0+.dx, y0+.dy);
5935 GlDraw.vertex2 (x1+.dx, y1+.dy);
5936 GlDraw.vertex2 (x2+.dx, y2+.dy);
5937 GlDraw.vertex2 (x3+.dx, y3+.dy);
5939 GlDraw.ends ();
5941 ) state.layout
5942 ) rects
5944 Gl.disable `blend;
5947 let display () =
5948 GlClear.color (scalecolor2 conf.bgcolor);
5949 GlClear.clear [`color];
5950 List.iter drawpage state.layout;
5951 let rects =
5952 match state.mode with
5953 | LinkNav (Ltexact (pageno, linkno)) ->
5954 begin match getopaque pageno with
5955 | Some opaque ->
5956 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5957 (pageno, 5, (
5958 float x0, float y0,
5959 float x1, float y0,
5960 float x1, float y1,
5961 float x0, float y1)
5962 ) :: state.rects
5963 | None -> state.rects
5965 | _ -> state.rects
5967 showrects rects;
5968 let rec postloop linkindexbase = function
5969 | l :: rest ->
5970 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5971 postloop linkindexbase rest
5972 | [] -> ()
5974 showsel ();
5975 postloop 0 state.layout;
5976 state.uioh#display;
5977 begin match state.mstate with
5978 | Mzoomrect ((x0, y0), (x1, y1)) ->
5979 Gl.enable `blend;
5980 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5981 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5982 GlDraw.rect (float x0, float y0)
5983 (float x1, float y1);
5984 Gl.disable `blend;
5985 | _ -> ()
5986 end;
5987 enttext ();
5988 scrollindicator ();
5989 Wsi.swapb ();
5992 let zoomrect x y x1 y1 =
5993 let x0 = min x x1
5994 and x1 = max x x1
5995 and y0 = min y y1 in
5996 gotoy (state.y + y0);
5997 state.anchor <- getanchor ();
5998 let zoom = (float state.w) /. float (x1 - x0) in
5999 let margin =
6000 match conf.fitmodel, conf.columns with
6001 | FitPage, Csplit _ ->
6002 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6004 | _, _ ->
6005 let adjw = wadjsb state.winw in
6006 if state.w < adjw
6007 then (adjw - state.w) / 2
6008 else 0
6010 state.x <- (state.x + margin) - x0;
6011 setzoom zoom;
6012 Wsi.setcursor Wsi.CURSOR_INHERIT;
6013 state.mstate <- Mnone;
6016 let zoomblock x y =
6017 let g opaque l px py =
6018 match rectofblock opaque px py with
6019 | Some a ->
6020 let x0 = a.(0) -. 20. in
6021 let x1 = a.(1) +. 20. in
6022 let y0 = a.(2) -. 20. in
6023 state.rects <- [];
6024 gotoy (getpagey l.pageno + truncate y0);
6025 state.anchor <- getanchor ();
6026 let zoom = (float state.w) /. (x1 -. x0) in
6027 let margin =
6028 match conf.fitmodel, conf.columns with
6029 | FitPage, Csplit _ ->
6030 onppundermouse (fun _ l _ _ -> Some l.pagedispx)
6031 (truncate x0) (truncate y0) 0
6033 | _, _ ->
6034 let adjw = wadjsb state.winw in
6035 if state.w < adjw
6036 then (adjw - state.w) / 2
6037 else 0
6039 state.x <- margin - truncate x0;
6040 setzoom zoom;
6041 state.bzoom <- false;
6042 None
6043 | None -> None
6045 onppundermouse g x y ()
6048 let scrollx x =
6049 let winw = wadjsb state.winw - 1 in
6050 let s = float x /. float winw in
6051 let destx = truncate (float (state.w + winw) *. s) in
6052 state.x <- winw - destx;
6053 gotoy_and_clear_text state.y;
6054 state.mstate <- Mscrollx;
6057 let scrolly y =
6058 let s = float y /. float state.winh in
6059 let desty = truncate (float (state.maxy - state.winh) *. s) in
6060 gotoy_and_clear_text desty;
6061 state.mstate <- Mscrolly;
6064 let viewmouse button down x y mask =
6065 match button with
6066 | n when (n == 4 || n == 5) && not down ->
6067 if Wsi.withctrl mask
6068 then (
6069 match state.mstate with
6070 | Mzoom (oldn, i) ->
6071 if oldn = n
6072 then (
6073 if i = 2
6074 then
6075 let incr =
6076 match n with
6077 | 5 ->
6078 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6079 | _ ->
6080 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6082 let zoom = conf.zoom -. incr in
6083 setzoom zoom;
6084 state.mstate <- Mzoom (n, 0);
6085 else
6086 state.mstate <- Mzoom (n, i+1);
6088 else state.mstate <- Mzoom (n, 0)
6090 | _ -> state.mstate <- Mzoom (n, 0)
6092 else (
6093 match state.autoscroll with
6094 | Some step -> setautoscrollspeed step (n=4)
6095 | None ->
6096 if conf.wheelbypage || conf.presentation
6097 then (
6098 if n = 4
6099 then prevpage ()
6100 else nextpage ()
6102 else
6103 let incr =
6104 if n = 4
6105 then -conf.scrollstep
6106 else conf.scrollstep
6108 let incr = incr * 2 in
6109 let y = clamp incr in
6110 gotoy_and_clear_text y
6113 | n when (n = 6 || n = 7) && not down && canpan () ->
6114 state.x <-
6115 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6116 gotoy_and_clear_text state.y
6118 | 1 when Wsi.withshift mask ->
6119 state.mstate <- Mnone;
6120 if not down
6121 then (
6122 match unproject x y with
6123 | Some (pageno, ux, uy) ->
6124 let cmd = Printf.sprintf
6125 "%s %s %d %d %d"
6126 conf.stcmd state.path pageno ux uy
6128 popen cmd []
6129 | None -> ()
6132 | 1 when Wsi.withctrl mask ->
6133 if down
6134 then (
6135 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6136 state.mstate <- Mpan (x, y)
6138 else
6139 state.mstate <- Mnone
6141 | 3 ->
6142 if down
6143 then (
6144 Wsi.setcursor Wsi.CURSOR_CYCLE;
6145 let p = (x, y) in
6146 state.mstate <- Mzoomrect (p, p)
6148 else (
6149 match state.mstate with
6150 | Mzoomrect ((x0, y0), _) ->
6151 if abs (x-x0) > 10 && abs (y - y0) > 10
6152 then zoomrect x0 y0 x y
6153 else (
6154 state.mstate <- Mnone;
6155 Wsi.setcursor Wsi.CURSOR_INHERIT;
6156 G.postRedisplay "kill accidental zoom rect";
6158 | _ ->
6159 Wsi.setcursor Wsi.CURSOR_INHERIT;
6160 state.mstate <- Mnone
6163 | 1 when x > state.winw - vscrollw () ->
6164 if down
6165 then
6166 let _, position, sh = state.uioh#scrollph in
6167 if y > truncate position && y < truncate (position +. sh)
6168 then state.mstate <- Mscrolly
6169 else scrolly y
6170 else
6171 state.mstate <- Mnone
6173 | 1 when y > state.winh - hscrollh () ->
6174 if down
6175 then
6176 let _, position, sw = state.uioh#scrollpw in
6177 if x > truncate position && x < truncate (position +. sw)
6178 then state.mstate <- Mscrollx
6179 else scrollx x
6180 else
6181 state.mstate <- Mnone
6183 | 1 when state.bzoom -> zoomblock x y
6185 | 1 ->
6186 let dest = if down then getunder x y else Unone in
6187 begin match dest with
6188 | Ulinkgoto _
6189 | Ulinkuri _
6190 | Uremote _
6191 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6192 gotounder dest
6194 | Unone when down ->
6195 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6196 state.mstate <- Mpan (x, y);
6198 | Unone | Utext _ ->
6199 if down
6200 then (
6201 if conf.angle mod 360 = 0
6202 then (
6203 state.mstate <- Msel ((x, y), (x, y));
6204 G.postRedisplay "mouse select";
6207 else (
6208 match state.mstate with
6209 | Mnone -> ()
6211 | Mzoom _ | Mscrollx | Mscrolly ->
6212 state.mstate <- Mnone
6214 | Mzoomrect ((x0, y0), _) ->
6215 zoomrect x0 y0 x y
6217 | Mpan _ ->
6218 Wsi.setcursor Wsi.CURSOR_INHERIT;
6219 state.mstate <- Mnone
6221 | Msel ((x0, y0), (x1, y1)) ->
6222 let rec loop = function
6223 | [] -> ()
6224 | l :: rest ->
6225 let inside =
6226 let a0 = l.pagedispy in
6227 let a1 = a0 + l.pagevh in
6228 let b0 = l.pagedispx in
6229 let b1 = b0 + l.pagevw in
6230 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6231 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6233 if inside
6234 then
6235 match getopaque l.pageno with
6236 | Some opaque ->
6237 begin
6238 match Ne.pipe () with
6239 | Ne.Exn exn ->
6240 showtext '!'
6241 (Printf.sprintf
6242 "can not create sel pipe: %s"
6243 (exntos exn));
6244 | Ne.Res (r, w) ->
6245 let doclose what fd =
6246 Ne.clo fd (fun msg ->
6247 dolog "%s close failed: %s" what msg)
6250 popen conf.selcmd [r, 0; w, -1];
6251 copysel w opaque true;
6252 doclose "pipe/r" r;
6253 G.postRedisplay "copysel";
6254 with exn ->
6255 dolog "can not execute %S: %s"
6256 conf.selcmd (exntos exn);
6257 doclose "pipe/r" r;
6258 doclose "pipe/w" w;
6260 | None -> ()
6261 else loop rest
6263 loop state.layout;
6264 Wsi.setcursor Wsi.CURSOR_INHERIT;
6265 state.mstate <- Mnone;
6269 | _ -> ()
6272 let birdseyemouse button down x y mask
6273 (conf, leftx, _, hooverpageno, anchor) =
6274 match button with
6275 | 1 when down ->
6276 let rec loop = function
6277 | [] -> ()
6278 | l :: rest ->
6279 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6280 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6281 then (
6282 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6284 else loop rest
6286 loop state.layout
6287 | 3 -> ()
6288 | _ -> viewmouse button down x y mask
6291 let mouse button down x y mask =
6292 state.uioh <- state.uioh#button button down x y mask;
6295 let motion ~x ~y =
6296 state.uioh <- state.uioh#motion x y
6299 let pmotion ~x ~y =
6300 state.uioh <- state.uioh#pmotion x y;
6303 let uioh = object
6304 method display = ()
6306 method key key mask =
6307 begin match state.mode with
6308 | Textentry textentry -> textentrykeyboard key mask textentry
6309 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6310 | View -> viewkeyboard key mask
6311 | LinkNav linknav -> linknavkeyboard key mask linknav
6312 end;
6313 state.uioh
6315 method button button bstate x y mask =
6316 begin match state.mode with
6317 | LinkNav _
6318 | View -> viewmouse button bstate x y mask
6319 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6320 | Textentry _ -> ()
6321 end;
6322 state.uioh
6324 method motion x y =
6325 begin match state.mode with
6326 | Textentry _ -> ()
6327 | View | Birdseye _ | LinkNav _ ->
6328 match state.mstate with
6329 | Mzoom _ | Mnone -> ()
6331 | Mpan (x0, y0) ->
6332 let dx = x - x0
6333 and dy = y0 - y in
6334 state.mstate <- Mpan (x, y);
6335 if canpan ()
6336 then state.x <- panbound (state.x + dx);
6337 let y = clamp dy in
6338 gotoy_and_clear_text y
6340 | Msel (a, _) ->
6341 state.mstate <- Msel (a, (x, y));
6342 G.postRedisplay "motion select";
6344 | Mscrolly ->
6345 let y = min state.winh (max 0 y) in
6346 scrolly y
6348 | Mscrollx ->
6349 let x = min state.winw (max 0 x) in
6350 scrollx x
6352 | Mzoomrect (p0, _) ->
6353 state.mstate <- Mzoomrect (p0, (x, y));
6354 G.postRedisplay "motion zoomrect";
6355 end;
6356 state.uioh
6358 method pmotion x y =
6359 begin match state.mode with
6360 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6361 let rec loop = function
6362 | [] ->
6363 if hooverpageno != -1
6364 then (
6365 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6366 G.postRedisplay "pmotion birdseye no hoover";
6368 | l :: rest ->
6369 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6370 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6371 then (
6372 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6373 G.postRedisplay "pmotion birdseye hoover";
6375 else loop rest
6377 loop state.layout
6379 | Textentry _ -> ()
6381 | LinkNav _
6382 | View ->
6383 match state.mstate with
6384 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6386 | Mnone ->
6387 updateunder x y;
6388 match conf.pax with
6389 | None -> ()
6390 | Some r ->
6391 let past, _, _ = !r in
6392 let now = now () in
6393 let delta = now -. past in
6394 if delta > 0.01
6395 then paxunder x y
6396 else r := (now, x, y)
6397 end;
6398 state.uioh
6400 method infochanged _ = ()
6402 method scrollph =
6403 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6404 let p, h =
6405 if maxy = 0
6406 then 0.0, float state.winh
6407 else scrollph state.y maxy
6409 vscrollw (), p, h
6411 method scrollpw =
6412 let winw = wadjsb state.winw in
6413 let fwinw = float winw in
6414 let sw =
6415 let sw = fwinw /. float state.w in
6416 let sw = fwinw *. sw in
6417 max sw (float conf.scrollh)
6419 let position =
6420 let maxx = state.w + winw in
6421 let x = winw - state.x in
6422 let percent = float x /. float maxx in
6423 (fwinw -. sw) *. percent
6425 hscrollh (), position, sw
6427 method modehash =
6428 let modename =
6429 match state.mode with
6430 | LinkNav _ -> "links"
6431 | Textentry _ -> "textentry"
6432 | Birdseye _ -> "birdseye"
6433 | View -> "view"
6435 findkeyhash conf modename
6437 method eformsgs = true
6438 end;;
6440 module Config =
6441 struct
6442 open Parser
6444 let fontpath = ref "";;
6446 module KeyMap =
6447 Map.Make (struct type t = (int * int) let compare = compare end);;
6449 let unent s =
6450 let l = String.length s in
6451 let b = Buffer.create l in
6452 unent b s 0 l;
6453 Buffer.contents b;
6456 let home =
6457 try Sys.getenv "HOME"
6458 with exn ->
6459 prerr_endline
6460 ("Can not determine home directory location: " ^ exntos exn);
6464 let modifier_of_string = function
6465 | "alt" -> Wsi.altmask
6466 | "shift" -> Wsi.shiftmask
6467 | "ctrl" | "control" -> Wsi.ctrlmask
6468 | "meta" -> Wsi.metamask
6469 | _ -> 0
6472 let key_of_string =
6473 let r = Str.regexp "-" in
6474 fun s ->
6475 let elems = Str.full_split r s in
6476 let f n k m =
6477 let g s =
6478 let m1 = modifier_of_string s in
6479 if m1 = 0
6480 then (Wsi.namekey s, m)
6481 else (k, m lor m1)
6482 in function
6483 | Str.Delim s when n land 1 = 0 -> g s
6484 | Str.Text s -> g s
6485 | Str.Delim _ -> (k, m)
6487 let rec loop n k m = function
6488 | [] -> (k, m)
6489 | x :: xs ->
6490 let k, m = f n k m x in
6491 loop (n+1) k m xs
6493 loop 0 0 0 elems
6496 let keys_of_string =
6497 let r = Str.regexp "[ \t]" in
6498 fun s ->
6499 let elems = Str.split r s in
6500 List.map key_of_string elems
6503 let copykeyhashes c =
6504 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6507 let config_of c attrs =
6508 let apply c k v =
6510 match k with
6511 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6512 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6513 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6514 | "preload" -> { c with preload = bool_of_string v }
6515 | "page-bias" -> { c with pagebias = int_of_string v }
6516 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6517 | "horizontal-scroll-step" ->
6518 { c with hscrollstep = max (int_of_string v) 1 }
6519 | "auto-scroll-step" ->
6520 { c with autoscrollstep = max 0 (int_of_string v) }
6521 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6522 | "crop-hack" -> { c with crophack = bool_of_string v }
6523 | "throttle" ->
6524 let mw =
6525 match String.lowercase v with
6526 | "true" -> Some infinity
6527 | "false" -> None
6528 | f -> Some (float_of_string f)
6530 { c with maxwait = mw}
6531 | "highlight-links" -> { c with hlinks = bool_of_string v }
6532 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6533 | "vertical-margin" ->
6534 { c with interpagespace = max 0 (int_of_string v) }
6535 | "zoom" ->
6536 let zoom = float_of_string v /. 100. in
6537 let zoom = max zoom 0.0 in
6538 { c with zoom = zoom }
6539 | "presentation" -> { c with presentation = bool_of_string v }
6540 | "rotation-angle" -> { c with angle = int_of_string v }
6541 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6542 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6543 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6544 | "proportional-display" ->
6545 let fm =
6546 if bool_of_string v
6547 then FitProportional
6548 else FitWidth
6550 { c with fitmodel = fm }
6551 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6552 | "pixmap-cache-size" ->
6553 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6554 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6555 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6556 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6557 | "persistent-location" -> { c with jumpback = bool_of_string v }
6558 | "background-color" -> { c with bgcolor = color_of_string v }
6559 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6560 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6561 | "mupdf-store-size" ->
6562 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6563 | "checkers" -> { c with checkers = bool_of_string v }
6564 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6565 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6566 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6567 | "uri-launcher" -> { c with urilauncher = unent v }
6568 | "path-launcher" -> { c with pathlauncher = unent v }
6569 | "color-space" -> { c with colorspace = CSTE.of_string v }
6570 | "invert-colors" -> { c with invert = bool_of_string v }
6571 | "brightness" -> { c with colorscale = float_of_string v }
6572 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6573 | "ghyllscroll" ->
6574 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6575 | "columns" ->
6576 let (n, _, _) as nab = multicolumns_of_string v in
6577 if n < 0
6578 then { c with columns = Csplit (-n, [||]) }
6579 else { c with columns = Cmulti (nab, [||]) }
6580 | "birds-eye-columns" ->
6581 { c with beyecolumns = Some (max (int_of_string v) 2) }
6582 | "selection-command" -> { c with selcmd = unent v }
6583 | "synctex-command" -> { c with stcmd = unent v }
6584 | "pax-command" -> { c with paxcmd = unent v }
6585 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6586 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6587 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6588 | "use-pbo" -> { c with usepbo = bool_of_string v }
6589 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6590 | "horizontal-scrollbar-visible" ->
6591 let b =
6592 if bool_of_string v
6593 then c.scrollb lor scrollbhv
6594 else c.scrollb land (lnot scrollbhv)
6596 { c with scrollb = b }
6597 | "vertical-scrollbar-visible" ->
6598 let b =
6599 if bool_of_string v
6600 then c.scrollb lor scrollbvv
6601 else c.scrollb land (lnot scrollbvv)
6603 { c with scrollb = b }
6604 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6605 | "point-and-x" ->
6606 { c with pax =
6607 if bool_of_string v
6608 then Some (ref (0.0, 0, 0))
6609 else None }
6610 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6611 | _ -> c
6612 with exn ->
6613 prerr_endline ("Error processing attribute (`" ^
6614 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6617 let rec fold c = function
6618 | [] -> c
6619 | (k, v) :: rest ->
6620 let c = apply c k v in
6621 fold c rest
6623 fold { c with keyhashes = copykeyhashes c } attrs;
6626 let fromstring f pos n v d =
6627 try f v
6628 with exn ->
6629 dolog "Error processing attribute (%S=%S) at %d\n%s"
6630 n v pos (exntos exn)
6635 let bookmark_of attrs =
6636 let rec fold title page rely visy = function
6637 | ("title", v) :: rest -> fold v page rely visy rest
6638 | ("page", v) :: rest -> fold title v rely visy rest
6639 | ("rely", v) :: rest -> fold title page v visy rest
6640 | ("visy", v) :: rest -> fold title page rely v rest
6641 | _ :: rest -> fold title page rely visy rest
6642 | [] -> title, page, rely, visy
6644 fold "invalid" "0" "0" "0" attrs
6647 let doc_of attrs =
6648 let rec fold path page rely pan visy = function
6649 | ("path", v) :: rest -> fold v page rely pan visy rest
6650 | ("page", v) :: rest -> fold path v rely pan visy rest
6651 | ("rely", v) :: rest -> fold path page v pan visy rest
6652 | ("pan", v) :: rest -> fold path page rely v visy rest
6653 | ("visy", v) :: rest -> fold path page rely pan v rest
6654 | _ :: rest -> fold path page rely pan visy rest
6655 | [] -> path, page, rely, pan, visy
6657 fold "" "0" "0" "0" "0" attrs
6660 let map_of attrs =
6661 let rec fold rs ls = function
6662 | ("out", v) :: rest -> fold v ls rest
6663 | ("in", v) :: rest -> fold rs v rest
6664 | _ :: rest -> fold ls rs rest
6665 | [] -> ls, rs
6667 fold "" "" attrs
6670 let setconf dst src =
6671 dst.scrollbw <- src.scrollbw;
6672 dst.scrollh <- src.scrollh;
6673 dst.icase <- src.icase;
6674 dst.preload <- src.preload;
6675 dst.pagebias <- src.pagebias;
6676 dst.verbose <- src.verbose;
6677 dst.scrollstep <- src.scrollstep;
6678 dst.maxhfit <- src.maxhfit;
6679 dst.crophack <- src.crophack;
6680 dst.autoscrollstep <- src.autoscrollstep;
6681 dst.maxwait <- src.maxwait;
6682 dst.hlinks <- src.hlinks;
6683 dst.underinfo <- src.underinfo;
6684 dst.interpagespace <- src.interpagespace;
6685 dst.zoom <- src.zoom;
6686 dst.presentation <- src.presentation;
6687 dst.angle <- src.angle;
6688 dst.cwinw <- src.cwinw;
6689 dst.cwinh <- src.cwinh;
6690 dst.savebmarks <- src.savebmarks;
6691 dst.memlimit <- src.memlimit;
6692 dst.fitmodel <- src.fitmodel;
6693 dst.texcount <- src.texcount;
6694 dst.sliceheight <- src.sliceheight;
6695 dst.thumbw <- src.thumbw;
6696 dst.jumpback <- src.jumpback;
6697 dst.bgcolor <- src.bgcolor;
6698 dst.tilew <- src.tilew;
6699 dst.tileh <- src.tileh;
6700 dst.mustoresize <- src.mustoresize;
6701 dst.checkers <- src.checkers;
6702 dst.aalevel <- src.aalevel;
6703 dst.trimmargins <- src.trimmargins;
6704 dst.trimfuzz <- src.trimfuzz;
6705 dst.urilauncher <- src.urilauncher;
6706 dst.colorspace <- src.colorspace;
6707 dst.invert <- src.invert;
6708 dst.colorscale <- src.colorscale;
6709 dst.redirectstderr <- src.redirectstderr;
6710 dst.ghyllscroll <- src.ghyllscroll;
6711 dst.columns <- src.columns;
6712 dst.beyecolumns <- src.beyecolumns;
6713 dst.selcmd <- src.selcmd;
6714 dst.updatecurs <- src.updatecurs;
6715 dst.pathlauncher <- src.pathlauncher;
6716 dst.keyhashes <- copykeyhashes src;
6717 dst.hfsize <- src.hfsize;
6718 dst.hscrollstep <- src.hscrollstep;
6719 dst.pgscale <- src.pgscale;
6720 dst.usepbo <- src.usepbo;
6721 dst.wheelbypage <- src.wheelbypage;
6722 dst.stcmd <- src.stcmd;
6723 dst.paxcmd <- src.paxcmd;
6724 dst.scrollb <- src.scrollb;
6725 dst.riani <- src.riani;
6726 dst.paxmark <- src.paxmark;
6727 dst.pax <-
6728 if src.pax = None
6729 then None
6730 else Some ((ref (0.0, 0, 0)));
6733 let get s =
6734 let h = Hashtbl.create 10 in
6735 let dc = { defconf with angle = defconf.angle } in
6736 let rec toplevel v t spos _ =
6737 match t with
6738 | Vdata | Vcdata | Vend -> v
6739 | Vopen ("llppconfig", _, closed) ->
6740 if closed
6741 then v
6742 else { v with f = llppconfig }
6743 | Vopen _ ->
6744 error "unexpected subelement at top level" s spos
6745 | Vclose _ -> error "unexpected close at top level" s spos
6747 and llppconfig v t spos _ =
6748 match t with
6749 | Vdata | Vcdata -> v
6750 | Vend -> error "unexpected end of input in llppconfig" s spos
6751 | Vopen ("defaults", attrs, closed) ->
6752 let c = config_of dc attrs in
6753 setconf dc c;
6754 if closed
6755 then v
6756 else { v with f = defaults }
6758 | Vopen ("ui-font", attrs, closed) ->
6759 let rec getsize size = function
6760 | [] -> size
6761 | ("size", v) :: rest ->
6762 let size =
6763 fromstring int_of_string spos "size" v fstate.fontsize in
6764 getsize size rest
6765 | l -> getsize size l
6767 fstate.fontsize <- getsize fstate.fontsize attrs;
6768 if closed
6769 then v
6770 else { v with f = uifont (Buffer.create 10) }
6772 | Vopen ("doc", attrs, closed) ->
6773 let pathent, spage, srely, span, svisy = doc_of attrs in
6774 let path = unent pathent
6775 and pageno = fromstring int_of_string spos "page" spage 0
6776 and rely = fromstring float_of_string spos "rely" srely 0.0
6777 and pan = fromstring int_of_string spos "pan" span 0
6778 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6779 let c = config_of dc attrs in
6780 let anchor = (pageno, rely, visy) in
6781 if closed
6782 then (Hashtbl.add h path (c, [], pan, anchor); v)
6783 else { v with f = doc path pan anchor c [] }
6785 | Vopen _ ->
6786 error "unexpected subelement in llppconfig" s spos
6788 | Vclose "llppconfig" -> { v with f = toplevel }
6789 | Vclose _ -> error "unexpected close in llppconfig" s spos
6791 and defaults v t spos _ =
6792 match t with
6793 | Vdata | Vcdata -> v
6794 | Vend -> error "unexpected end of input in defaults" s spos
6795 | Vopen ("keymap", attrs, closed) ->
6796 let modename =
6797 try List.assoc "mode" attrs
6798 with Not_found -> "global" in
6799 if closed
6800 then v
6801 else
6802 let ret keymap =
6803 let h = findkeyhash dc modename in
6804 KeyMap.iter (Hashtbl.replace h) keymap;
6805 defaults
6807 { v with f = pkeymap ret KeyMap.empty }
6809 | Vopen (_, _, _) ->
6810 error "unexpected subelement in defaults" s spos
6812 | Vclose "defaults" ->
6813 { v with f = llppconfig }
6815 | Vclose _ -> error "unexpected close in defaults" s spos
6817 and uifont b v t spos epos =
6818 match t with
6819 | Vdata | Vcdata ->
6820 Buffer.add_substring b s spos (epos - spos);
6822 | Vopen (_, _, _) ->
6823 error "unexpected subelement in ui-font" s spos
6824 | Vclose "ui-font" ->
6825 if String.length !fontpath = 0
6826 then fontpath := Buffer.contents b;
6827 { v with f = llppconfig }
6828 | Vclose _ -> error "unexpected close in ui-font" s spos
6829 | Vend -> error "unexpected end of input in ui-font" s spos
6831 and doc path pan anchor c bookmarks v t spos _ =
6832 match t with
6833 | Vdata | Vcdata -> v
6834 | Vend -> error "unexpected end of input in doc" s spos
6835 | Vopen ("bookmarks", _, closed) ->
6836 if closed
6837 then v
6838 else { v with f = pbookmarks path pan anchor c bookmarks }
6840 | Vopen ("keymap", attrs, closed) ->
6841 let modename =
6842 try List.assoc "mode" attrs
6843 with Not_found -> "global"
6845 if closed
6846 then v
6847 else
6848 let ret keymap =
6849 let h = findkeyhash c modename in
6850 KeyMap.iter (Hashtbl.replace h) keymap;
6851 doc path pan anchor c bookmarks
6853 { v with f = pkeymap ret KeyMap.empty }
6855 | Vopen (_, _, _) ->
6856 error "unexpected subelement in doc" s spos
6858 | Vclose "doc" ->
6859 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6860 { v with f = llppconfig }
6862 | Vclose _ -> error "unexpected close in doc" s spos
6864 and pkeymap ret keymap v t spos _ =
6865 match t with
6866 | Vdata | Vcdata -> v
6867 | Vend -> error "unexpected end of input in keymap" s spos
6868 | Vopen ("map", attrs, closed) ->
6869 let r, l = map_of attrs in
6870 let kss = fromstring keys_of_string spos "in" r [] in
6871 let lss = fromstring keys_of_string spos "out" l [] in
6872 let keymap =
6873 match kss with
6874 | [] -> keymap
6875 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6876 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6878 if closed
6879 then { v with f = pkeymap ret keymap }
6880 else
6881 let f () = v in
6882 { v with f = skip "map" f }
6884 | Vopen _ ->
6885 error "unexpected subelement in keymap" s spos
6887 | Vclose "keymap" ->
6888 { v with f = ret keymap }
6890 | Vclose _ -> error "unexpected close in keymap" s spos
6892 and pbookmarks path pan anchor c bookmarks v t spos _ =
6893 match t with
6894 | Vdata | Vcdata -> v
6895 | Vend -> error "unexpected end of input in bookmarks" s spos
6896 | Vopen ("item", attrs, closed) ->
6897 let titleent, spage, srely, svisy = bookmark_of attrs in
6898 let page = fromstring int_of_string spos "page" spage 0
6899 and rely = fromstring float_of_string spos "rely" srely 0.0
6900 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6901 let bookmarks =
6902 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6904 if closed
6905 then { v with f = pbookmarks path pan anchor c bookmarks }
6906 else
6907 let f () = v in
6908 { v with f = skip "item" f }
6910 | Vopen _ ->
6911 error "unexpected subelement in bookmarks" s spos
6913 | Vclose "bookmarks" ->
6914 { v with f = doc path pan anchor c bookmarks }
6916 | Vclose _ -> error "unexpected close in bookmarks" s spos
6918 and skip tag f v t spos _ =
6919 match t with
6920 | Vdata | Vcdata -> v
6921 | Vend ->
6922 error ("unexpected end of input in skipped " ^ tag) s spos
6923 | Vopen (tag', _, closed) ->
6924 if closed
6925 then v
6926 else
6927 let f' () = { v with f = skip tag f } in
6928 { v with f = skip tag' f' }
6929 | Vclose ctag ->
6930 if tag = ctag
6931 then f ()
6932 else error ("unexpected close in skipped " ^ tag) s spos
6935 parse { f = toplevel; accu = () } s;
6936 h, dc;
6939 let do_load f ic =
6941 let len = in_channel_length ic in
6942 let s = String.create len in
6943 really_input ic s 0 len;
6944 f s;
6945 with
6946 | Parse_error (msg, s, pos) ->
6947 let subs = subs s pos in
6948 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6949 failwith ("parse error: " ^ s)
6951 | exn ->
6952 failwith ("config load error: " ^ exntos exn)
6955 let defconfpath =
6956 let dir =
6958 let dir = Filename.concat home ".config" in
6959 if Sys.is_directory dir then dir else home
6960 with _ -> home
6962 Filename.concat dir "llpp.conf"
6965 let confpath = ref defconfpath;;
6967 let load1 f =
6968 if Sys.file_exists !confpath
6969 then
6970 match
6971 (try Some (open_in_bin !confpath)
6972 with exn ->
6973 prerr_endline
6974 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6975 exntos exn);
6976 None
6978 with
6979 | Some ic ->
6980 let success =
6982 f (do_load get ic)
6983 with exn ->
6984 prerr_endline
6985 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6986 exntos exn);
6987 false
6989 close_in ic;
6990 success
6992 | None -> false
6993 else
6994 f (Hashtbl.create 0, defconf)
6997 let load () =
6998 let f (h, dc) =
6999 let pc, pb, px, pa =
7001 let key =
7002 if String.length state.origin = 0
7003 then state.path
7004 else state.origin
7006 Hashtbl.find h (Filename.basename key)
7007 with Not_found -> dc, [], 0, emptyanchor
7009 setconf defconf dc;
7010 setconf conf pc;
7011 state.bookmarks <- pb;
7012 state.x <- px;
7013 if conf.jumpback
7014 then state.anchor <- pa;
7015 cbput state.hists.nav pa;
7016 true
7018 load1 f
7021 let add_attrs bb always dc c =
7022 let ob s a b =
7023 if always || a != b
7024 then Printf.bprintf bb "\n %s='%b'" s a
7025 and op s a b =
7026 if always || a <> b
7027 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7028 and oi s a b =
7029 if always || a != b
7030 then Printf.bprintf bb "\n %s='%d'" s a
7031 and oI s a b =
7032 if always || a != b
7033 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7034 and oz s a b =
7035 if always || a <> b
7036 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7037 and oF s a b =
7038 if always || a <> b
7039 then Printf.bprintf bb "\n %s='%f'" s a
7040 and oc s a b =
7041 if always || a <> b
7042 then
7043 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7044 and oC s a b =
7045 if always || a <> b
7046 then
7047 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7048 and oR s a b =
7049 if always || a <> b
7050 then
7051 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7052 and os s a b =
7053 if always || a <> b
7054 then
7055 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7056 and og s a b =
7057 if always || a <> b
7058 then
7059 match a with
7060 | None -> ()
7061 | Some (_N, _A, _B) ->
7062 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7063 and oW s a b =
7064 if always || a <> b
7065 then
7066 let v =
7067 match a with
7068 | None -> "false"
7069 | Some f ->
7070 if f = infinity
7071 then "true"
7072 else string_of_float f
7074 Printf.bprintf bb "\n %s='%s'" s v
7075 and oco s a b =
7076 if always || a <> b
7077 then
7078 match a with
7079 | Cmulti ((n, a, b), _) when n > 1 ->
7080 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7081 | Csplit (n, _) when n > 1 ->
7082 Printf.bprintf bb "\n %s='%d'" s ~-n
7083 | _ -> ()
7084 and obeco s a b =
7085 if always || a <> b
7086 then
7087 match a with
7088 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7089 | _ -> ()
7090 and oFm s a b =
7091 if always || a <> b
7092 then
7093 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7094 and oSv s a b m =
7095 if always || a <> b
7096 then
7097 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7098 and oPm s a b =
7099 if always || a <> b
7100 then
7101 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7103 oi "width" c.cwinw dc.cwinw;
7104 oi "height" c.cwinh dc.cwinh;
7105 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7106 oi "scroll-handle-height" c.scrollh dc.scrollh;
7107 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7108 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7109 ob "case-insensitive-search" c.icase dc.icase;
7110 ob "preload" c.preload dc.preload;
7111 oi "page-bias" c.pagebias dc.pagebias;
7112 oi "scroll-step" c.scrollstep dc.scrollstep;
7113 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7114 ob "max-height-fit" c.maxhfit dc.maxhfit;
7115 ob "crop-hack" c.crophack dc.crophack;
7116 oW "throttle" c.maxwait dc.maxwait;
7117 ob "highlight-links" c.hlinks dc.hlinks;
7118 ob "under-cursor-info" c.underinfo dc.underinfo;
7119 oi "vertical-margin" c.interpagespace dc.interpagespace;
7120 oz "zoom" c.zoom dc.zoom;
7121 ob "presentation" c.presentation dc.presentation;
7122 oi "rotation-angle" c.angle dc.angle;
7123 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7124 oFm "fit-model" c.fitmodel dc.fitmodel;
7125 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7126 oi "tex-count" c.texcount dc.texcount;
7127 oi "slice-height" c.sliceheight dc.sliceheight;
7128 oi "thumbnail-width" c.thumbw dc.thumbw;
7129 ob "persistent-location" c.jumpback dc.jumpback;
7130 oc "background-color" c.bgcolor dc.bgcolor;
7131 oi "tile-width" c.tilew dc.tilew;
7132 oi "tile-height" c.tileh dc.tileh;
7133 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7134 ob "checkers" c.checkers dc.checkers;
7135 oi "aalevel" c.aalevel dc.aalevel;
7136 ob "trim-margins" c.trimmargins dc.trimmargins;
7137 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7138 os "uri-launcher" c.urilauncher dc.urilauncher;
7139 os "path-launcher" c.pathlauncher dc.pathlauncher;
7140 oC "color-space" c.colorspace dc.colorspace;
7141 ob "invert-colors" c.invert dc.invert;
7142 oF "brightness" c.colorscale dc.colorscale;
7143 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7144 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7145 oco "columns" c.columns dc.columns;
7146 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7147 os "selection-command" c.selcmd dc.selcmd;
7148 os "synctex-command" c.stcmd dc.stcmd;
7149 os "pax-command" c.paxcmd dc.paxcmd;
7150 ob "update-cursor" c.updatecurs dc.updatecurs;
7151 oi "hint-font-size" c.hfsize dc.hfsize;
7152 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7153 oF "page-scroll-scale" c.pgscale dc.pgscale;
7154 ob "use-pbo" c.usepbo dc.usepbo;
7155 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7156 ob "remote-in-a-new-instance" c.riani dc.riani;
7157 op "point-and-x" c.pax dc.pax;
7158 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7161 let keymapsbuf always dc c =
7162 let bb = Buffer.create 16 in
7163 let rec loop = function
7164 | [] -> ()
7165 | (modename, h) :: rest ->
7166 let dh = findkeyhash dc modename in
7167 if always || h <> dh
7168 then (
7169 if Hashtbl.length h > 0
7170 then (
7171 if Buffer.length bb > 0
7172 then Buffer.add_char bb '\n';
7173 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7174 Hashtbl.iter (fun i o ->
7175 let isdifferent = always ||
7177 let dO = Hashtbl.find dh i in
7178 dO <> o
7179 with Not_found -> true
7181 if isdifferent
7182 then
7183 let addkm (k, m) =
7184 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7185 if Wsi.withalt m then Buffer.add_string bb "alt-";
7186 if Wsi.withshift m then Buffer.add_string bb "shift-";
7187 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7188 Buffer.add_string bb (Wsi.keyname k);
7190 let addkms l =
7191 let rec loop = function
7192 | [] -> ()
7193 | km :: [] -> addkm km
7194 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7196 loop l
7198 Buffer.add_string bb "<map in='";
7199 addkm i;
7200 match o with
7201 | KMinsrt km ->
7202 Buffer.add_string bb "' out='";
7203 addkm km;
7204 Buffer.add_string bb "'/>\n"
7206 | KMinsrl kms ->
7207 Buffer.add_string bb "' out='";
7208 addkms kms;
7209 Buffer.add_string bb "'/>\n"
7211 | KMmulti (ins, kms) ->
7212 Buffer.add_char bb ' ';
7213 addkms ins;
7214 Buffer.add_string bb "' out='";
7215 addkms kms;
7216 Buffer.add_string bb "'/>\n"
7217 ) h;
7218 Buffer.add_string bb "</keymap>";
7221 loop rest
7223 loop c.keyhashes;
7227 let save () =
7228 let uifontsize = fstate.fontsize in
7229 let bb = Buffer.create 32768 in
7230 let relx = float state.x /. float state.winw in
7231 let w, h, x =
7232 let cx w = truncate (relx *. float w) in
7233 List.fold_left
7234 (fun (w, h, x) ws ->
7235 match ws with
7236 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7237 | Wsi.MaxVert -> (w, conf.cwinh, x)
7238 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7240 (state.winw, state.winh, state.x) state.winstate
7242 conf.cwinw <- w;
7243 conf.cwinh <- h;
7244 let f (h, dc) =
7245 let dc = if conf.bedefault then conf else dc in
7246 Buffer.add_string bb "<llppconfig>\n";
7248 if String.length !fontpath > 0
7249 then
7250 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7251 uifontsize
7252 !fontpath
7253 else (
7254 if uifontsize <> 14
7255 then
7256 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7259 Buffer.add_string bb "<defaults ";
7260 add_attrs bb true dc dc;
7261 let kb = keymapsbuf true dc dc in
7262 if Buffer.length kb > 0
7263 then (
7264 Buffer.add_string bb ">\n";
7265 Buffer.add_buffer bb kb;
7266 Buffer.add_string bb "\n</defaults>\n";
7268 else Buffer.add_string bb "/>\n";
7270 let adddoc path pan anchor c bookmarks =
7271 if bookmarks == [] && c = dc && anchor = emptyanchor
7272 then ()
7273 else (
7274 Printf.bprintf bb "<doc path='%s'"
7275 (enent path 0 (String.length path));
7277 if anchor <> emptyanchor
7278 then (
7279 let n, rely, visy = anchor in
7280 Printf.bprintf bb " page='%d'" n;
7281 if rely > 1e-6
7282 then
7283 Printf.bprintf bb " rely='%f'" rely
7285 if abs_float visy > 1e-6
7286 then
7287 Printf.bprintf bb " visy='%f'" visy
7291 if pan != 0
7292 then Printf.bprintf bb " pan='%d'" pan;
7294 add_attrs bb false dc c;
7295 let kb = keymapsbuf false dc c in
7297 begin match bookmarks with
7298 | [] ->
7299 if Buffer.length kb > 0
7300 then (
7301 Buffer.add_string bb ">\n";
7302 Buffer.add_buffer bb kb;
7303 Buffer.add_string bb "\n</doc>\n";
7305 else Buffer.add_string bb "/>\n"
7306 | _ ->
7307 Buffer.add_string bb ">\n<bookmarks>\n";
7308 List.iter (fun (title, _level, (page, rely, visy)) ->
7309 Printf.bprintf bb
7310 "<item title='%s' page='%d'"
7311 (enent title 0 (String.length title))
7312 page
7314 if rely > 1e-6
7315 then
7316 Printf.bprintf bb " rely='%f'" rely
7318 if abs_float visy > 1e-6
7319 then
7320 Printf.bprintf bb " visy='%f'" visy
7322 Buffer.add_string bb "/>\n";
7323 ) bookmarks;
7324 Buffer.add_string bb "</bookmarks>";
7325 if Buffer.length kb > 0
7326 then (
7327 Buffer.add_string bb "\n";
7328 Buffer.add_buffer bb kb;
7330 Buffer.add_string bb "\n</doc>\n";
7331 end;
7335 let pan, conf =
7336 match state.mode with
7337 | Birdseye (c, pan, _, _, _) ->
7338 let beyecolumns =
7339 match conf.columns with
7340 | Cmulti ((c, _, _), _) -> Some c
7341 | Csingle _ -> None
7342 | Csplit _ -> None
7343 and columns =
7344 match c.columns with
7345 | Cmulti (c, _) -> Cmulti (c, [||])
7346 | Csingle _ -> Csingle [||]
7347 | Csplit _ -> failwith "quit from bird's eye while split"
7349 pan, { c with beyecolumns = beyecolumns; columns = columns }
7350 | _ -> x, conf
7352 let basename = Filename.basename
7353 (if String.length state.origin = 0 then state.path else state.origin)
7355 adddoc basename pan (getanchor ())
7356 (let conf =
7357 let autoscrollstep =
7358 match state.autoscroll with
7359 | Some step -> step
7360 | None -> conf.autoscrollstep
7362 match state.mode with
7363 | Birdseye (bc, _, _, _, _) ->
7364 { conf with
7365 zoom = bc.zoom;
7366 presentation = bc.presentation;
7367 interpagespace = bc.interpagespace;
7368 maxwait = bc.maxwait;
7369 autoscrollstep = autoscrollstep }
7370 | _ -> { conf with autoscrollstep = autoscrollstep }
7371 in conf)
7372 (if conf.savebmarks then state.bookmarks else []);
7374 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7375 if basename <> path
7376 then adddoc path x anchor c bookmarks
7377 ) h;
7378 Buffer.add_string bb "</llppconfig>\n";
7379 true;
7381 if load1 f && Buffer.length bb > 0
7382 then
7384 let tmp = !confpath ^ ".tmp" in
7385 let oc = open_out_bin tmp in
7386 Buffer.output_buffer oc bb;
7387 close_out oc;
7388 Unix.rename tmp !confpath;
7389 with exn ->
7390 prerr_endline
7391 ("error while saving configuration: " ^ exntos exn)
7393 end;;
7395 let adderrmsg src msg =
7396 Buffer.add_string state.errmsgs msg;
7397 state.newerrmsgs <- true;
7398 G.postRedisplay src
7401 let adderrfmt src fmt =
7402 Format.kprintf (fun s -> adderrmsg src s) fmt;
7405 let ract cmds =
7406 let cl = splitatspace cmds in
7407 let scan s fmt f =
7408 try Scanf.sscanf s fmt f
7409 with exn ->
7410 adderrfmt "remote exec"
7411 "error processing '%S': %s\n" cmds (exntos exn)
7413 match cl with
7414 | "reload" :: [] -> reload ()
7415 | "goto" :: args :: [] ->
7416 scan args "%u %f %f"
7417 (fun pageno x y ->
7418 let cmd, _ = state.geomcmds in
7419 if String.length cmd = 0
7420 then gotopagexy pageno x y
7421 else
7422 let f prevf () =
7423 gotopagexy pageno x y;
7424 prevf ()
7426 state.reprf <- f state.reprf
7428 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7429 | "rect" :: args :: [] ->
7430 scan args "%u %u %f %f %f %f"
7431 (fun pageno color x0 y0 x1 y1 ->
7432 onpagerect pageno (fun w h ->
7433 let _,w1,h1,_ = getpagedim pageno in
7434 let sw = float w1 /. w
7435 and sh = float h1 /. h in
7436 let x0s = x0 *. sw
7437 and x1s = x1 *. sw
7438 and y0s = y0 *. sh
7439 and y1s = y1 *. sh in
7440 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7441 debugrect rect;
7442 state.rects <- (pageno, color, rect) :: state.rects;
7443 G.postRedisplay "rect";
7446 | "activatewin" :: [] -> Wsi.activatewin ()
7447 | "quit" :: [] -> raise Quit
7448 | _ ->
7449 adderrfmt "remote command"
7450 "error processing remote command: %S\n" cmds;
7453 let remote =
7454 let scratch = String.create 80 in
7455 let buf = Buffer.create 80 in
7456 fun fd ->
7457 let rec tempfr () =
7458 try Some (Unix.read fd scratch 0 80)
7459 with
7460 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7461 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7462 | exn -> raise exn
7464 match tempfr () with
7465 | None -> Some fd
7466 | Some n ->
7467 if n = 0
7468 then (
7469 Unix.close fd;
7470 if Buffer.length buf > 0
7471 then (
7472 let s = Buffer.contents buf in
7473 Buffer.clear buf;
7474 ract s;
7476 None
7478 else
7479 let rec eat ppos =
7480 let nlpos =
7482 let pos = String.index_from scratch ppos '\n' in
7483 if pos >= n then -1 else pos
7484 with Not_found -> -1
7486 if nlpos >= 0
7487 then (
7488 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7489 let s = Buffer.contents buf in
7490 Buffer.clear buf;
7491 ract s;
7492 eat (nlpos+1);
7494 else (
7495 Buffer.add_substring buf scratch ppos (n-ppos);
7496 Some fd
7498 in eat 0
7501 let remoteopen path =
7502 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7503 with exn ->
7504 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7505 None
7508 let () =
7509 let trimcachepath = ref "" in
7510 let rcmdpath = ref "" in
7511 selfexec := Sys.executable_name;
7512 Arg.parse
7513 (Arg.align
7514 [("-p", Arg.String (fun s -> state.password <- s),
7515 "<password> Set password");
7517 ("-f", Arg.String
7518 (fun s ->
7519 Config.fontpath := s;
7520 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7522 "<path> Set path to the user interface font");
7524 ("-c", Arg.String
7525 (fun s ->
7526 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7527 Config.confpath := s),
7528 "<path> Set path to the configuration file");
7530 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7531 "<path> Set path to the trim cache file");
7533 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7534 "<named-destination> Set named destination");
7536 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7537 ("-cxack", Arg.Set cxack, " Cut corners");
7539 ("-remote", Arg.String (fun s -> rcmdpath := s),
7540 "<path> Set path to the remote commands source");
7542 ("-origin", Arg.String (fun s -> state.origin <- s),
7543 "<original-path> Set original path");
7545 ("-v", Arg.Unit (fun () ->
7546 Printf.printf
7547 "%s\nconfiguration path: %s\n"
7548 (version ())
7549 Config.defconfpath
7551 exit 0), " Print version and exit");
7554 (fun s -> state.path <- s)
7555 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7557 if !wtmode
7558 then selfexec := !selfexec ^ " -wtmode";
7560 if String.length state.path = 0
7561 then (prerr_endline "file name missing"; exit 1);
7563 if not (Config.load ())
7564 then prerr_endline "failed to load configuration";
7566 let wsfd, winw, winh = Wsi.init (object
7567 val mutable m_hack = false
7568 method expose = if not m_hack then G.postRedisplay "expose"
7569 method visible = G.postRedisplay "visible"
7570 method display = m_hack <- false; display ()
7571 method reshape w h =
7572 m_hack <- w < state.winw && h < state.winh;
7573 reshape w h
7574 method mouse b d x y m = mouse b d x y m
7575 method motion x y = state.mpos <- (x, y); motion x y
7576 method pmotion x y = state.mpos <- (x, y); pmotion x y
7577 method key k m =
7578 let mascm = m land (
7579 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7580 ) in
7581 match state.keystate with
7582 | KSnone ->
7583 let km = k, mascm in
7584 begin
7585 match
7586 let modehash = state.uioh#modehash in
7587 try Hashtbl.find modehash km
7588 with Not_found ->
7589 try Hashtbl.find (findkeyhash conf "global") km
7590 with Not_found -> KMinsrt (k, m)
7591 with
7592 | KMinsrt (k, m) -> keyboard k m
7593 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7594 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7596 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7597 List.iter (fun (k, m) -> keyboard k m) insrt;
7598 state.keystate <- KSnone
7599 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7600 state.keystate <- KSinto (keys, insrt)
7601 | _ ->
7602 state.keystate <- KSnone
7604 method enter x y = state.mpos <- (x, y); pmotion x y
7605 method leave = state.mpos <- (-1, -1)
7606 method winstate wsl = state.winstate <- wsl
7607 method quit = raise Quit
7608 end) conf.cwinw conf.cwinh (platform = Posx) in
7610 state.wsfd <- wsfd;
7612 if not (
7613 List.exists GlMisc.check_extension
7614 [ "GL_ARB_texture_rectangle"
7615 ; "GL_EXT_texture_recangle"
7616 ; "GL_NV_texture_rectangle" ]
7618 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7620 if (
7621 let r = GlMisc.get_string `renderer in
7622 let p = "Mesa DRI Intel(" in
7623 let l = String.length p in
7624 String.length r > l && String.sub r 0 l = p
7626 then defconf.sliceheight <- 1024;
7628 let cr, sw =
7629 match Ne.pipe () with
7630 | Ne.Exn exn ->
7631 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7632 exit 1
7633 | Ne.Res rw -> rw
7634 and sr, cw =
7635 match Ne.pipe () with
7636 | Ne.Exn exn ->
7637 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7638 exit 1
7639 | Ne.Res rw -> rw
7642 cloexec cr;
7643 cloexec sw;
7644 cloexec sr;
7645 cloexec cw;
7647 setcheckers conf.checkers;
7648 redirectstderr ();
7649 if conf.redirectstderr
7650 then
7651 at_exit (fun () ->
7652 let s = Buffer.contents state.errmsgs ^
7653 (match state.errfd with
7654 | Some fd ->
7655 let s = String.create (80*24) in
7656 let n =
7658 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7659 if List.mem fd r
7660 then Unix.read fd s 0 (String.length s)
7661 else 0
7662 with _ -> 0
7664 if n = 0
7665 then ""
7666 else String.sub s 0 n
7667 | None -> ""
7670 try ignore (Unix.write state.stderr s 0 (String.length s))
7671 with exn -> print_endline (exntos exn)
7675 init (cr, cw) (
7676 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7677 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7678 !Config.fontpath, !trimcachepath,
7679 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7681 state.sr <- sr;
7682 state.sw <- sw;
7683 state.text <- "Opening " ^ (mbtoutf8 state.path);
7684 reshape winw winh;
7685 opendoc state.path state.password;
7686 state.uioh <- uioh;
7687 display ();
7688 Wsi.mapwin ();
7689 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7690 let optrfd =
7691 ref (
7692 if String.length !rcmdpath > 0
7693 then remoteopen !rcmdpath
7694 else None
7698 let rec loop deadline =
7699 let r =
7700 match state.errfd with
7701 | None -> [state.sr; state.wsfd]
7702 | Some fd -> [state.sr; state.wsfd; fd]
7704 let r =
7705 match !optrfd with
7706 | None -> r
7707 | Some fd -> fd :: r
7709 if state.redisplay
7710 then (
7711 state.redisplay <- false;
7712 display ();
7714 let timeout =
7715 let now = now () in
7716 if deadline > now
7717 then (
7718 if deadline = infinity
7719 then ~-.1.0
7720 else max 0.0 (deadline -. now)
7722 else 0.0
7724 let r, _, _ =
7725 try Unix.select r [] [] timeout
7726 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7728 begin match r with
7729 | [] ->
7730 state.ghyll None;
7731 let newdeadline =
7732 if state.ghyll == noghyll
7733 then
7734 match state.autoscroll with
7735 | Some step when step != 0 ->
7736 let y = state.y + step in
7737 let y =
7738 if y < 0
7739 then state.maxy
7740 else if y >= state.maxy then 0 else y
7742 gotoy y;
7743 if state.mode = View
7744 then state.text <- "";
7745 deadline +. 0.01
7746 | _ -> infinity
7747 else deadline +. 0.01
7749 loop newdeadline
7751 | l ->
7752 let rec checkfds = function
7753 | [] -> ()
7754 | fd :: rest when fd = state.sr ->
7755 let cmd = readcmd state.sr in
7756 act cmd;
7757 checkfds rest
7759 | fd :: rest when fd = state.wsfd ->
7760 Wsi.readresp fd;
7761 checkfds rest
7763 | fd :: rest when Some fd = !optrfd ->
7764 begin match remote fd with
7765 | None -> optrfd := remoteopen !rcmdpath;
7766 | opt -> optrfd := opt
7767 end;
7768 checkfds rest
7770 | fd :: rest ->
7771 let s = String.create 80 in
7772 let n = tempfailureretry (Unix.read fd s 0) 80 in
7773 if conf.redirectstderr
7774 then (
7775 Buffer.add_substring state.errmsgs s 0 n;
7776 state.newerrmsgs <- true;
7777 state.redisplay <- true;
7779 else (
7780 prerr_string (String.sub s 0 n);
7781 flush stderr;
7783 checkfds rest
7785 checkfds l;
7786 let newdeadline =
7787 let deadline1 =
7788 if deadline = infinity
7789 then now () +. 0.01
7790 else deadline
7792 match state.autoscroll with
7793 | Some step when step != 0 -> deadline1
7794 | _ -> if state.ghyll == noghyll then infinity else deadline1
7796 loop newdeadline
7797 end;
7800 loop infinity;
7801 with Quit ->
7802 Config.save ();