Ctrl-up/down in outline mode
[llpp.git] / main.ml
blobb78bd914cdf0a976811b1d769be7ae4f006be295
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 * int)
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, 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 if Array.length b = 0
1415 then []
1416 else List.rev (fold [] (page_of_y y))
1419 let layoutS (columns, b) y sh =
1420 let sh = sh - hscrollh () in
1421 let rec fold accu n =
1422 if n = Array.length b
1423 then accu
1424 else
1425 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1426 if (vy - y) > sh
1427 then accu
1428 else
1429 let accu =
1430 if vy + pageh > y
1431 then
1432 let x = xoff + state.x in
1433 let pagey = max 0 (y - vy) in
1434 let pagedispy = if pagey > 0 then 0 else vy - y in
1435 let pagedispx, pagex =
1436 if px = 0
1437 then (
1438 if x < 0
1439 then 0, -x
1440 else x, 0
1442 else (
1443 let px = px - x in
1444 if px < 0
1445 then -px, 0
1446 else 0, px
1449 let pagecolw = pagew/columns in
1450 let pagedispx =
1451 if pagecolw < state.winw
1452 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1453 else pagedispx
1455 let pagevw =
1456 let vw = wadjsb state.winw - pagedispx in
1457 let pw = pagew - pagex in
1458 min vw pw
1460 let pagevw = min pagevw pagecolw in
1461 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1462 if pagevw > 0 && pagevh > 0
1463 then
1464 let e =
1465 { pageno = n/columns
1466 ; pagedimno = pdimno
1467 ; pagew = pagew
1468 ; pageh = pageh
1469 ; pagex = pagex
1470 ; pagey = pagey
1471 ; pagevw = pagevw
1472 ; pagevh = pagevh
1473 ; pagedispx = pagedispx
1474 ; pagedispy = pagedispy
1475 ; pagecol = n mod columns
1478 e :: accu
1479 else
1480 accu
1481 else
1482 accu
1484 fold accu (n+1)
1486 List.rev (fold [] 0)
1489 let layout y sh =
1490 if nogeomcmds state.geomcmds
1491 then
1492 match conf.columns with
1493 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1494 | Cmulti c -> layoutN c y sh
1495 | Csplit s -> layoutS s y sh
1496 else []
1499 let clamp incr =
1500 let y = state.y + incr in
1501 let y = max 0 y in
1502 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1506 let itertiles l f =
1507 let tilex = l.pagex mod conf.tilew in
1508 let tiley = l.pagey mod conf.tileh in
1510 let col = l.pagex / conf.tilew in
1511 let row = l.pagey / conf.tileh in
1513 let rec rowloop row y0 dispy h =
1514 if h = 0
1515 then ()
1516 else (
1517 let dh = conf.tileh - y0 in
1518 let dh = min h dh in
1519 let rec colloop col x0 dispx w =
1520 if w = 0
1521 then ()
1522 else (
1523 let dw = conf.tilew - x0 in
1524 let dw = min w dw in
1526 f col row dispx dispy x0 y0 dw dh;
1527 colloop (col+1) 0 (dispx+dw) (w-dw)
1530 colloop col tilex l.pagedispx l.pagevw;
1531 rowloop (row+1) 0 (dispy+dh) (h-dh)
1534 if l.pagevw > 0 && l.pagevh > 0
1535 then rowloop row tiley l.pagedispy l.pagevh;
1538 let gettileopaque l col row =
1539 let key =
1540 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1542 try Some (Hashtbl.find state.tilemap key)
1543 with Not_found -> None
1546 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1547 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1548 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1551 let drawtiles l color =
1552 GlDraw.color color;
1553 let f col row x y tilex tiley w h =
1554 match gettileopaque l col row with
1555 | Some (opaque, _, t) ->
1556 let params = x, y, w, h, tilex, tiley in
1557 if conf.invert
1558 then (
1559 Gl.enable `blend;
1560 GlFunc.blend_func `zero `one_minus_src_color;
1562 drawtile params opaque;
1563 if conf.invert
1564 then Gl.disable `blend;
1565 if conf.debug
1566 then (
1567 let s = Printf.sprintf
1568 "%d[%d,%d] %f sec"
1569 l.pageno col row t
1571 let w = measurestr fstate.fontsize s in
1572 GlMisc.push_attrib [`current];
1573 GlDraw.color (0.0, 0.0, 0.0);
1574 GlDraw.rect
1575 (float (x-2), float (y-2))
1576 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1577 GlDraw.color (1.0, 1.0, 1.0);
1578 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1579 GlMisc.pop_attrib ();
1582 | _ ->
1583 let w =
1584 let lw = wadjsb state.winw - x in
1585 min lw w
1586 and h =
1587 let lh = state.winh - y in
1588 min lh h
1590 begin match state.texid with
1591 | Some id ->
1592 Gl.enable `texture_2d;
1593 GlTex.bind_texture `texture_2d id;
1594 let x0 = float x
1595 and y0 = float y
1596 and x1 = float (x+w)
1597 and y1 = float (y+h) in
1599 let tw = float w /. 16.0
1600 and th = float h /. 16.0 in
1601 let tx0 = float tilex /. 16.0
1602 and ty0 = float tiley /. 16.0 in
1603 let tx1 = tx0 +. tw
1604 and ty1 = ty0 +. th in
1605 GlDraw.begins `quads;
1606 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1607 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1608 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1609 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1610 GlDraw.ends ();
1612 Gl.disable `texture_2d;
1613 | None ->
1614 GlDraw.color (1.0, 1.0, 1.0);
1615 GlDraw.rect
1616 (float x, float y)
1617 (float (x+w), float (y+h));
1618 end;
1619 if w > 128 && h > fstate.fontsize + 10
1620 then (
1621 GlDraw.color (0.0, 0.0, 0.0);
1622 let c, r =
1623 if conf.verbose
1624 then (col*conf.tilew, row*conf.tileh)
1625 else col, row
1627 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1629 GlDraw.color color;
1631 itertiles l f
1634 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1636 let tilevisible1 l x y =
1637 let ax0 = l.pagex
1638 and ax1 = l.pagex + l.pagevw
1639 and ay0 = l.pagey
1640 and ay1 = l.pagey + l.pagevh in
1642 let bx0 = x
1643 and by0 = y in
1644 let bx1 = min (bx0 + conf.tilew) l.pagew
1645 and by1 = min (by0 + conf.tileh) l.pageh in
1647 let rx0 = max ax0 bx0
1648 and ry0 = max ay0 by0
1649 and rx1 = min ax1 bx1
1650 and ry1 = min ay1 by1 in
1652 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1653 nonemptyintersection
1656 let tilevisible layout n x y =
1657 let rec findpageinlayout m = function
1658 | l :: rest when l.pageno = n ->
1659 tilevisible1 l x y || (
1660 match conf.columns with
1661 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1662 | _ -> false
1664 | _ :: rest -> findpageinlayout 0 rest
1665 | [] -> false
1667 findpageinlayout 0 layout;
1670 let tileready l x y =
1671 tilevisible1 l x y &&
1672 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1675 let tilepage n p layout =
1676 let rec loop = function
1677 | l :: rest ->
1678 if l.pageno = n
1679 then
1680 let f col row _ _ _ _ _ _ =
1681 if state.currently = Idle
1682 then
1683 match gettileopaque l col row with
1684 | Some _ -> ()
1685 | None ->
1686 let x = col*conf.tilew
1687 and y = row*conf.tileh in
1688 let w =
1689 let w = l.pagew - x in
1690 min w conf.tilew
1692 let h =
1693 let h = l.pageh - y in
1694 min h conf.tileh
1696 let pbo =
1697 if conf.usepbo
1698 then getpbo w h conf.colorspace
1699 else "0"
1701 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1702 state.currently <-
1703 Tiling (
1704 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1705 conf.tilew, conf.tileh
1708 itertiles l f;
1709 else
1710 loop rest
1712 | [] -> ()
1714 if nogeomcmds state.geomcmds
1715 then loop layout;
1718 let preloadlayout y =
1719 let y = if y < state.winh then 0 else y - state.winh in
1720 let h = state.winh*3 in
1721 layout y h;
1724 let load pages =
1725 let rec loop pages =
1726 if state.currently != Idle
1727 then ()
1728 else
1729 match pages with
1730 | l :: rest ->
1731 begin match getopaque l.pageno with
1732 | None ->
1733 wcmd "page %d %d" l.pageno l.pagedimno;
1734 state.currently <- Loading (l, state.gen);
1735 | Some opaque ->
1736 tilepage l.pageno opaque pages;
1737 loop rest
1738 end;
1739 | _ -> ()
1741 if nogeomcmds state.geomcmds
1742 then loop pages
1745 let preload pages =
1746 load pages;
1747 if conf.preload && state.currently = Idle
1748 then load (preloadlayout state.y);
1751 let layoutready layout =
1752 let rec fold all ls =
1753 all && match ls with
1754 | l :: rest ->
1755 let seen = ref false in
1756 let allvisible = ref true in
1757 let foo col row _ _ _ _ _ _ =
1758 seen := true;
1759 allvisible := !allvisible &&
1760 begin match gettileopaque l col row with
1761 | Some _ -> true
1762 | None -> false
1765 itertiles l foo;
1766 fold (!seen && !allvisible) rest
1767 | [] -> true
1769 let alltilesvisible = fold true layout in
1770 alltilesvisible;
1773 let gotoy y =
1774 let y = bound y 0 state.maxy in
1775 let y, layout, proceed =
1776 match conf.maxwait with
1777 | Some time when state.ghyll == noghyll ->
1778 begin match state.throttle with
1779 | None ->
1780 let layout = layout y state.winh in
1781 let ready = layoutready layout in
1782 if not ready
1783 then (
1784 load layout;
1785 state.throttle <- Some (layout, y, now ());
1787 else G.postRedisplay "gotoy showall (None)";
1788 y, layout, ready
1789 | Some (_, _, started) ->
1790 let dt = now () -. started in
1791 if dt > time
1792 then (
1793 state.throttle <- None;
1794 let layout = layout y state.winh in
1795 load layout;
1796 G.postRedisplay "maxwait";
1797 y, layout, true
1799 else -1, [], false
1802 | _ ->
1803 let layout = layout y state.winh in
1804 if not !wtmode || layoutready layout
1805 then G.postRedisplay "gotoy ready";
1806 y, layout, true
1808 if proceed
1809 then (
1810 state.y <- y;
1811 state.layout <- layout;
1812 begin match state.mode with
1813 | LinkNav (Ltexact (pageno, linkno)) ->
1814 let rec loop = function
1815 | [] ->
1816 state.mode <- LinkNav (Ltgendir 0)
1817 | l :: _ when l.pageno = pageno ->
1818 begin match getopaque pageno with
1819 | None ->
1820 state.mode <- LinkNav (Ltgendir 0)
1821 | Some opaque ->
1822 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1823 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1824 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1825 then state.mode <- LinkNav (Ltgendir 0)
1827 | _ :: rest -> loop rest
1829 loop layout
1830 | _ -> ()
1831 end;
1832 begin match state.mode with
1833 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1834 if not (pagevisible layout pageno)
1835 then (
1836 match state.layout with
1837 | [] -> ()
1838 | l :: _ ->
1839 state.mode <- Birdseye (
1840 conf, leftx, l.pageno, hooverpageno, anchor
1843 | LinkNav (Ltgendir dir as lt) ->
1844 let linknav =
1845 let rec loop = function
1846 | [] -> lt
1847 | l :: rest ->
1848 match getopaque l.pageno with
1849 | None -> loop rest
1850 | Some opaque ->
1851 let link =
1852 let ld =
1853 if dir = 0
1854 then LDfirstvisible (l.pagex, l.pagey, dir)
1855 else (
1856 if dir > 0 then LDfirst else LDlast
1859 findlink opaque ld
1861 match link with
1862 | Lnotfound -> loop rest
1863 | Lfound n ->
1864 showlinktype (getlink opaque n);
1865 Ltexact (l.pageno, n)
1867 loop state.layout
1869 state.mode <- LinkNav linknav
1870 | _ -> ()
1871 end;
1872 preload layout;
1874 state.ghyll <- noghyll;
1875 if conf.updatecurs
1876 then (
1877 let mx, my = state.mpos in
1878 updateunder mx my;
1882 let conttiling pageno opaque =
1883 tilepage pageno opaque
1884 (if conf.preload then preloadlayout state.y else state.layout)
1887 let gotoy_and_clear_text y =
1888 if not conf.verbose then state.text <- "";
1889 gotoy y;
1892 let getanchor1 l =
1893 let top =
1894 let coloff = l.pagecol * l.pageh in
1895 float (l.pagey + coloff) /. float l.pageh
1897 let dtop =
1898 if l.pagedispy = 0
1899 then
1901 else (
1902 if conf.presentation
1903 then float l.pagedispy /. float (calcips l.pageh)
1904 else float l.pagedispy /. float conf.interpagespace
1907 (l.pageno, top, dtop)
1910 let getanchor () =
1911 match state.layout with
1912 | l :: _ -> getanchor1 l
1913 | [] ->
1914 let n = page_of_y state.y in
1915 if n = -1
1916 then state.anchor
1917 else
1918 let y, h = getpageyh n in
1919 let dy = y - state.y in
1920 let dtop =
1921 if conf.presentation
1922 then
1923 let ips = calcips h in
1924 float (dy + ips) /. float ips
1925 else
1926 float dy /. float conf.interpagespace
1928 (n, 0.0, dtop)
1931 let getanchory (n, top, dtop) =
1932 let y, h = getpageyh n in
1933 if conf.presentation
1934 then
1935 let ips = calcips h in
1936 y + truncate (top*.float h -. dtop*.float ips) + ips;
1937 else
1938 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1941 let gotoanchor anchor =
1942 gotoy (getanchory anchor);
1945 let addnav () =
1946 cbput state.hists.nav (getanchor ());
1949 let getnav dir =
1950 let anchor = cbgetc state.hists.nav dir in
1951 getanchory anchor;
1954 let gotoghyll y =
1955 let scroll f n a b =
1956 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1957 let snake f a b =
1958 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1959 if f < a
1960 then s (float f /. float a)
1961 else (
1962 if f > b
1963 then 1.0 -. s ((float (f-b) /. float (n-b)))
1964 else 1.0
1967 snake f a b
1968 and summa f n a b =
1969 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1970 let iv x = x**3.-.0.5*.x**4. in
1971 let iv1 = iv f in
1972 let ins = float a *. iv1
1973 and outs = float (n-b) *. iv1 in
1974 let ones = b - a in
1975 ins +. outs +. float ones
1977 let rec set (_N, _A, _B) y sy =
1978 let sum = summa 1.0 _N _A _B in
1979 let dy = float (y - sy) in
1980 state.ghyll <- (
1981 let rec gf n y1 o =
1982 if n >= _N
1983 then state.ghyll <- noghyll
1984 else
1985 let go n =
1986 let s = scroll n _N _A _B in
1987 let y1 = y1 +. ((s *. dy) /. sum) in
1988 gotoy_and_clear_text (truncate y1);
1989 state.ghyll <- gf (n+1) y1;
1991 match o with
1992 | None -> go n
1993 | Some y' -> set (_N/2, 1, 1) y' state.y
1995 gf 0 (float state.y)
1998 match conf.ghyllscroll with
1999 | None ->
2000 gotoy_and_clear_text y
2001 | Some nab ->
2002 if state.ghyll == noghyll
2003 then set nab y state.y
2004 else state.ghyll (Some y)
2007 let gotopage n top =
2008 let y, h = getpageyh n in
2009 let y = y + (truncate (top *. float h)) in
2010 gotoghyll y
2013 let gotopage1 n top =
2014 let y = getpagey n in
2015 let y = y + top in
2016 gotoghyll y
2019 let invalidate s f =
2020 state.layout <- [];
2021 state.pdims <- [];
2022 state.rects <- [];
2023 state.rects1 <- [];
2024 match state.geomcmds with
2025 | ps, [] when String.length ps = 0 ->
2026 f ();
2027 state.geomcmds <- s, [];
2029 | ps, [] ->
2030 state.geomcmds <- ps, [s, f];
2032 | ps, (s', _) :: rest when s' = s ->
2033 state.geomcmds <- ps, ((s, f) :: rest);
2035 | ps, cmds ->
2036 state.geomcmds <- ps, ((s, f) :: cmds);
2039 let flushpages () =
2040 Hashtbl.iter (fun _ opaque ->
2041 wcmd "freepage %s" opaque;
2042 ) state.pagemap;
2043 Hashtbl.clear state.pagemap;
2046 let flushtiles () =
2047 if not (Queue.is_empty state.tilelru)
2048 then (
2049 Queue.iter (fun (k, p, s) ->
2050 wcmd "freetile %s" p;
2051 state.memused <- state.memused - s;
2052 Hashtbl.remove state.tilemap k;
2053 ) state.tilelru;
2054 state.uioh#infochanged Memused;
2055 Queue.clear state.tilelru;
2057 load state.layout;
2060 let stateh h =
2061 let h = truncate (float h*.conf.zoom) in
2062 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2063 h - d
2066 let opendoc path password =
2067 state.path <- path;
2068 state.password <- password;
2069 state.gen <- state.gen + 1;
2070 state.docinfo <- [];
2072 flushpages ();
2073 setaalevel conf.aalevel;
2074 let titlepath =
2075 if String.length state.origin = 0
2076 then path
2077 else state.origin
2079 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2080 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2081 invalidate "reqlayout"
2082 (fun () ->
2083 wcmd "reqlayout %d %d %d %s\000"
2084 conf.angle (FMTE.to_int conf.fitmodel)
2085 (stateh state.winh) state.nameddest
2089 let reload () =
2090 state.anchor <- getanchor ();
2091 opendoc state.path state.password;
2094 let scalecolor c =
2095 let c = c *. conf.colorscale in
2096 (c, c, c);
2099 let scalecolor2 (r, g, b) =
2100 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2103 let docolumns = function
2104 | Csingle _ ->
2105 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2106 let rec loop pageno pdimno pdim y ph pdims =
2107 if pageno = state.pagecount
2108 then ()
2109 else
2110 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2111 match pdims with
2112 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2113 pdimno+1, pdim, rest
2114 | _ ->
2115 pdimno, pdim, pdims
2117 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2118 let y = y +
2119 (if conf.presentation
2120 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2121 else (if pageno = 0 then 0 else conf.interpagespace)
2124 a.(pageno) <- (pdimno, x, y, pdim);
2125 loop (pageno+1) pdimno pdim (y + h) h pdims
2127 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2128 conf.columns <- Csingle a;
2130 | Cmulti ((columns, coverA, coverB), _) ->
2131 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2132 let rec loop pageno pdimno pdim x y rowh pdims =
2133 let rec fixrow m = if m = pageno then () else
2134 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2135 if h < rowh
2136 then (
2137 let y = y + (rowh - h) / 2 in
2138 a.(m) <- (pdimno, x, y, pdim);
2140 fixrow (m+1)
2142 if pageno = state.pagecount
2143 then fixrow (((pageno - 1) / columns) * columns)
2144 else
2145 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2146 match pdims with
2147 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2148 pdimno+1, pdim, rest
2149 | _ ->
2150 pdimno, pdim, pdims
2152 let x, y, rowh' =
2153 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2154 then (
2155 let x = (wadjsb state.winw - w) / 2 in
2156 let ips =
2157 if conf.presentation then calcips h else conf.interpagespace in
2158 x, y + ips + rowh, h
2160 else (
2161 if (pageno - coverA) mod columns = 0
2162 then (
2163 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2164 let y =
2165 if conf.presentation
2166 then
2167 let ips = calcips h in
2168 y + (if pageno = 0 then 0 else calcips rowh + ips)
2169 else
2170 y + (if pageno = 0 then 0 else conf.interpagespace)
2172 x, y + rowh, h
2174 else x, y, max rowh h
2177 let y =
2178 if pageno > 1 && (pageno - coverA) mod columns = 0
2179 then (
2180 let y =
2181 if pageno = columns && conf.presentation
2182 then (
2183 let ips = calcips rowh in
2184 for i = 0 to pred columns
2186 let (pdimno, x, y, pdim) = a.(i) in
2187 a.(i) <- (pdimno, x, y+ips, pdim)
2188 done;
2189 y+ips;
2191 else y
2193 fixrow (pageno - columns);
2196 else y
2198 a.(pageno) <- (pdimno, x, y, pdim);
2199 let x = x + w + xoff*2 + conf.interpagespace in
2200 loop (pageno+1) pdimno pdim x y rowh' pdims
2202 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2203 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2205 | Csplit (c, _) ->
2206 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2207 let rec loop pageno pdimno pdim y pdims =
2208 if pageno = state.pagecount
2209 then ()
2210 else
2211 let pdimno, ((_, w, h, _) as pdim), pdims =
2212 match pdims with
2213 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2214 pdimno+1, pdim, rest
2215 | _ ->
2216 pdimno, pdim, pdims
2218 let cw = w / c in
2219 let rec loop1 n x y =
2220 if n = c then y else (
2221 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2222 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2225 let y = loop1 0 0 y in
2226 loop (pageno+1) pdimno pdim y pdims
2228 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2229 conf.columns <- Csplit (c, a);
2232 let represent () =
2233 docolumns conf.columns;
2234 state.maxy <- calcheight ();
2235 if state.reprf == noreprf
2236 then (
2237 match state.mode with
2238 | Birdseye (_, _, pageno, _, _) ->
2239 let y, h = getpageyh pageno in
2240 let top = (state.winh - h) / 2 in
2241 gotoy (max 0 (y - top))
2242 | _ -> gotoanchor state.anchor
2244 else (
2245 state.reprf ();
2246 state.reprf <- noreprf;
2250 let reshape w h =
2251 GlDraw.viewport 0 0 w h;
2252 let firsttime = state.geomcmds == firstgeomcmds in
2253 if not firsttime && nogeomcmds state.geomcmds
2254 then state.anchor <- getanchor ();
2256 state.winw <- w;
2257 let w = wadjsb (truncate (float w *. conf.zoom)) in
2258 let w = max w 2 in
2259 state.winh <- h;
2260 setfontsize fstate.fontsize;
2261 GlMat.mode `modelview;
2262 GlMat.load_identity ();
2264 GlMat.mode `projection;
2265 GlMat.load_identity ();
2266 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2267 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2268 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2270 let relx =
2271 if conf.zoom <= 1.0
2272 then 0.0
2273 else float state.x /. float state.w
2275 invalidate "geometry"
2276 (fun () ->
2277 state.w <- w;
2278 if not firsttime
2279 then state.x <- truncate (relx *. float w);
2280 let w =
2281 match conf.columns with
2282 | Csingle _ -> w
2283 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2284 | Csplit (c, _) -> w * c
2286 wcmd "geometry %d %d %d"
2287 w (stateh h) (FMTE.to_int conf.fitmodel)
2291 let enttext () =
2292 let len = String.length state.text in
2293 let drawstring s =
2294 let hscrollh =
2295 match state.mode with
2296 | Textentry _ | View | LinkNav _ ->
2297 let h, _, _ = state.uioh#scrollpw in
2299 | _ -> 0
2301 let rect x w =
2302 GlDraw.rect
2303 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2304 (x+.w, float (state.winh - hscrollh))
2307 let w = float (wadjsb state.winw - 1) in
2308 if state.progress >= 0.0 && state.progress < 1.0
2309 then (
2310 GlDraw.color (0.3, 0.3, 0.3);
2311 let w1 = w *. state.progress in
2312 rect 0.0 w1;
2313 GlDraw.color (0.0, 0.0, 0.0);
2314 rect w1 (w-.w1)
2316 else (
2317 GlDraw.color (0.0, 0.0, 0.0);
2318 rect 0.0 w;
2321 GlDraw.color (1.0, 1.0, 1.0);
2322 drawstring fstate.fontsize
2323 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2325 let s =
2326 match state.mode with
2327 | Textentry ((prefix, text, _, _, _, _), _) ->
2328 let s =
2329 if len > 0
2330 then
2331 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2332 else
2333 Printf.sprintf "%s%s_" prefix text
2337 | _ -> state.text
2339 let s =
2340 if state.newerrmsgs
2341 then (
2342 if not (istextentry state.mode) && state.uioh#eformsgs
2343 then
2344 let s1 = "(press 'e' to review error messasges)" in
2345 if String.length s > 0 then s ^ " " ^ s1 else s1
2346 else s
2348 else s
2350 if String.length s > 0
2351 then drawstring s
2354 let gctiles () =
2355 let len = Queue.length state.tilelru in
2356 let layout = lazy (
2357 match state.throttle with
2358 | None ->
2359 if conf.preload
2360 then preloadlayout state.y
2361 else state.layout
2362 | Some (layout, _, _) ->
2363 layout
2364 ) in
2365 let rec loop qpos =
2366 if state.memused <= conf.memlimit
2367 then ()
2368 else (
2369 if qpos < len
2370 then
2371 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2372 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2373 let (_, pw, ph, _) = getpagedim n in
2375 gen = state.gen
2376 && colorspace = conf.colorspace
2377 && angle = conf.angle
2378 && pagew = pw
2379 && pageh = ph
2380 && (
2381 let x = col*conf.tilew
2382 and y = row*conf.tileh in
2383 tilevisible (Lazy.force_val layout) n x y
2385 then Queue.push lruitem state.tilelru
2386 else (
2387 freepbo p;
2388 wcmd "freetile %s" p;
2389 state.memused <- state.memused - s;
2390 state.uioh#infochanged Memused;
2391 Hashtbl.remove state.tilemap k;
2393 loop (qpos+1)
2396 loop 0
2399 let logcurrently = function
2400 | Idle -> dolog "Idle"
2401 | Loading (l, gen) ->
2402 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2403 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2404 dolog
2405 "Tiling %d[%d,%d] page=%s cs=%s angle"
2406 l.pageno col row pageopaque
2407 (CSTE.to_string colorspace)
2409 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2410 angle gen conf.angle state.gen
2411 tilew tileh
2412 conf.tilew conf.tileh
2414 | Outlining _ ->
2415 dolog "outlining"
2418 let splitatspace =
2419 let r = Str.regexp " " in
2420 fun s -> Str.bounded_split r s 2;
2423 let onpagerect pageno f =
2424 let b =
2425 match conf.columns with
2426 | Cmulti (_, b) -> b
2427 | Csingle b -> b
2428 | Csplit (_, b) -> b
2430 if pageno >= 0 && pageno < Array.length b
2431 then
2432 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2433 let r = getpdimrect pdimno in
2434 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2437 let gotopagexy1 pageno x y =
2438 onpagerect pageno (fun w h ->
2439 let top = y /. h in
2440 let _,w1,_,leftx = getpagedim pageno in
2441 let wh = state.winh - hscrollh () in
2442 let sw = float w1 /. w in
2443 let x = sw *. x in
2444 let x = leftx + state.x + truncate x in
2445 let sx =
2446 if x < 0 || x >= wadjsb state.winw
2447 then state.x - x
2448 else state.x
2450 let py, h = getpageyh pageno in
2451 let pdy = truncate (top *. float h) in
2452 let y' = py + pdy in
2453 let dy = y' - state.y in
2454 let sy =
2455 if x != state.x || not (dy > 0 && dy < wh)
2456 then (
2457 if conf.presentation
2458 then
2459 if abs (py - y') > wh
2460 then y'
2461 else py
2462 else y';
2464 else state.y
2466 if state.x != sx || state.y != sy
2467 then (
2468 let x, y =
2469 if !wtmode
2470 then (
2471 let ww = wadjsb state.winw in
2472 let qx = sx / ww
2473 and qy = pdy / wh in
2474 let x = qx * ww
2475 and y = py + qy * wh in
2476 let x = if -x + ww > w1 then -(w1-ww) else x
2477 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2478 let y =
2479 if conf.presentation
2480 then
2481 if abs (py - y') > wh
2482 then y'
2483 else py
2484 else y';
2486 (x, y)
2488 else (sx, sy)
2490 state.x <- x;
2491 gotoy_and_clear_text y;
2493 else gotoy_and_clear_text state.y;
2497 let gotopagexy pageno x y =
2498 match state.mode with
2499 | Birdseye _ -> gotopage pageno 0.0
2500 | _ -> gotopagexy1 pageno x y
2503 let act cmds =
2504 (* dolog "%S" cmds; *)
2505 let cl = splitatspace cmds in
2506 let scan s fmt f =
2507 try Scanf.sscanf s fmt f
2508 with exn ->
2509 dolog "error processing '%S': %s" cmds (exntos exn);
2510 exit 1
2512 match cl with
2513 | "clear" :: [] ->
2514 state.uioh#infochanged Pdim;
2515 state.pdims <- [];
2517 | "clearrects" :: [] ->
2518 state.rects <- state.rects1;
2519 G.postRedisplay "clearrects";
2521 | "continue" :: args :: [] ->
2522 let n = scan args "%u" (fun n -> n) in
2523 state.pagecount <- n;
2524 begin match state.currently with
2525 | Outlining l ->
2526 state.currently <- Idle;
2527 state.outlines <- Array.of_list (List.rev l)
2528 | _ -> ()
2529 end;
2531 let cur, cmds = state.geomcmds in
2532 if String.length cur = 0
2533 then failwith "umpossible";
2535 begin match List.rev cmds with
2536 | [] ->
2537 state.geomcmds <- "", [];
2538 represent ();
2539 | (s, f) :: rest ->
2540 f ();
2541 state.geomcmds <- s, List.rev rest;
2542 end;
2543 if conf.maxwait = None && not !wtmode
2544 then G.postRedisplay "continue";
2546 | "title" :: args :: [] ->
2547 Wsi.settitle args
2549 | "msg" :: args :: [] ->
2550 showtext ' ' args
2552 | "vmsg" :: args :: [] ->
2553 if conf.verbose
2554 then showtext ' ' args
2556 | "emsg" :: args :: [] ->
2557 Buffer.add_string state.errmsgs args;
2558 state.newerrmsgs <- true;
2559 G.postRedisplay "error message"
2561 | "progress" :: args :: [] ->
2562 let progress, text =
2563 scan args "%f %n"
2564 (fun f pos ->
2565 f, String.sub args pos (String.length args - pos))
2567 state.text <- text;
2568 state.progress <- progress;
2569 G.postRedisplay "progress"
2571 | "firstmatch" :: args :: [] ->
2572 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2573 scan args "%u %d %f %f %f %f %f %f %f %f"
2574 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2575 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2577 let y = (getpagey pageno) + truncate y0 in
2578 addnav ();
2579 gotoy y;
2580 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2582 | "match" :: args :: [] ->
2583 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2584 scan args "%u %d %f %f %f %f %f %f %f %f"
2585 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2586 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2588 state.rects1 <-
2589 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2591 | "page" :: args :: [] ->
2592 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2593 begin match state.currently with
2594 | Loading (l, gen) ->
2595 vlog "page %d took %f sec" l.pageno t;
2596 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2597 begin match state.throttle with
2598 | None ->
2599 let preloadedpages =
2600 if conf.preload
2601 then preloadlayout state.y
2602 else state.layout
2604 let evict () =
2605 let set =
2606 List.fold_left (fun s l -> IntSet.add l.pageno s)
2607 IntSet.empty preloadedpages
2609 let evictedpages =
2610 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2611 if not (IntSet.mem pageno set)
2612 then (
2613 wcmd "freepage %s" opaque;
2614 key :: accu
2616 else accu
2617 ) state.pagemap []
2619 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2621 evict ();
2622 state.currently <- Idle;
2623 if gen = state.gen
2624 then (
2625 tilepage l.pageno pageopaque state.layout;
2626 load state.layout;
2627 load preloadedpages;
2628 if pagevisible state.layout l.pageno
2629 && layoutready state.layout
2630 then G.postRedisplay "page";
2633 | Some (layout, _, _) ->
2634 state.currently <- Idle;
2635 tilepage l.pageno pageopaque layout;
2636 load state.layout
2637 end;
2639 | _ ->
2640 dolog "Inconsistent loading state";
2641 logcurrently state.currently;
2642 exit 1
2645 | "tile" :: args :: [] ->
2646 let (x, y, opaque, size, t) =
2647 scan args "%u %u %s %u %f"
2648 (fun x y p size t -> (x, y, p, size, t))
2650 begin match state.currently with
2651 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2652 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2654 unmappbo opaque;
2655 if tilew != conf.tilew || tileh != conf.tileh
2656 then (
2657 wcmd "freetile %s" opaque;
2658 state.currently <- Idle;
2659 load state.layout;
2661 else (
2662 puttileopaque l col row gen cs angle opaque size t;
2663 state.memused <- state.memused + size;
2664 state.uioh#infochanged Memused;
2665 gctiles ();
2666 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2667 opaque, size) state.tilelru;
2669 let layout =
2670 match state.throttle with
2671 | None -> state.layout
2672 | Some (layout, _, _) -> layout
2675 state.currently <- Idle;
2676 if gen = state.gen
2677 && conf.colorspace = cs
2678 && conf.angle = angle
2679 && tilevisible layout l.pageno x y
2680 then conttiling l.pageno pageopaque;
2682 begin match state.throttle with
2683 | None ->
2684 preload state.layout;
2685 if gen = state.gen
2686 && conf.colorspace = cs
2687 && conf.angle = angle
2688 && tilevisible state.layout l.pageno x y
2689 && (not !wtmode || layoutready state.layout)
2690 then G.postRedisplay "tile nothrottle";
2692 | Some (layout, y, _) ->
2693 let ready = layoutready layout in
2694 if ready
2695 then (
2696 state.y <- y;
2697 state.layout <- layout;
2698 state.throttle <- None;
2699 G.postRedisplay "throttle";
2701 else load layout;
2702 end;
2705 | _ ->
2706 dolog "Inconsistent tiling state";
2707 logcurrently state.currently;
2708 exit 1
2711 | "pdim" :: args :: [] ->
2712 let (n, w, h, _) as pdim =
2713 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2715 let pdim =
2716 match conf.fitmodel, conf.columns with
2717 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2718 | _ -> pdim
2720 state.uioh#infochanged Pdim;
2721 state.pdims <- pdim :: state.pdims
2723 | "o" :: args :: [] ->
2724 let (l, n, t, h, pos) =
2725 scan args "%u %u %d %u %n"
2726 (fun l n t h pos -> l, n, t, h, pos)
2728 let s = String.sub args pos (String.length args - pos) in
2729 let outline = (s, l, (n, float t /. float h, 0.0)) in
2730 begin match state.currently with
2731 | Outlining outlines ->
2732 state.currently <- Outlining (outline :: outlines)
2733 | Idle ->
2734 state.currently <- Outlining [outline]
2735 | currently ->
2736 dolog "invalid outlining state";
2737 logcurrently currently
2740 | "a" :: args :: [] ->
2741 let (n, l, t) =
2742 scan args "%u %d %d" (fun n l t -> n, l, t)
2744 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2746 | "info" :: args :: [] ->
2747 state.docinfo <- (1, args) :: state.docinfo
2749 | "infoend" :: [] ->
2750 state.uioh#infochanged Docinfo;
2751 state.docinfo <- List.rev state.docinfo
2753 | _ ->
2754 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2757 let onhist cb =
2758 let rc = cb.rc in
2759 let action = function
2760 | HCprev -> cbget cb ~-1
2761 | HCnext -> cbget cb 1
2762 | HCfirst -> cbget cb ~-(cb.rc)
2763 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2764 and cancel () = cb.rc <- rc
2765 in (action, cancel)
2768 let search pattern forward =
2769 match conf.columns with
2770 | Csplit _ ->
2771 showtext '!' "searching does not work properly in split columns mode"
2772 | _ ->
2773 if String.length pattern > 0
2774 then
2775 let pn, py =
2776 match state.layout with
2777 | [] -> 0, 0
2778 | l :: _ ->
2779 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2781 wcmd "search %d %d %d %d,%s\000"
2782 (btod conf.icase) pn py (btod forward) pattern;
2785 let intentry text key =
2786 let c =
2787 if key >= 32 && key < 127
2788 then Char.chr key
2789 else '\000'
2791 match c with
2792 | '0' .. '9' ->
2793 let text = addchar text c in
2794 TEcont text
2796 | _ ->
2797 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2798 TEcont text
2801 let linknentry text key =
2802 let c =
2803 if key >= 32 && key < 127
2804 then Char.chr key
2805 else '\000'
2807 match c with
2808 | 'a' .. 'z' ->
2809 let text = addchar text c in
2810 TEcont text
2812 | _ ->
2813 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2814 TEcont text
2817 let linkndone f s =
2818 if String.length s > 0
2819 then (
2820 let n =
2821 let l = String.length s in
2822 let rec loop pos n = if pos = l then n else
2823 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2824 loop (pos+1) (n*26 + m)
2825 in loop 0 0
2827 let rec loop n = function
2828 | [] -> ()
2829 | l :: rest ->
2830 match getopaque l.pageno with
2831 | None -> loop n rest
2832 | Some opaque ->
2833 let m = getlinkcount opaque in
2834 if n < m
2835 then (
2836 let under = getlink opaque n in
2837 f under
2839 else loop (n-m) rest
2841 loop n state.layout;
2845 let textentry text key =
2846 if key land 0xff00 = 0xff00
2847 then TEcont text
2848 else TEcont (text ^ toutf8 key)
2851 let reqlayout angle fitmodel =
2852 match state.throttle with
2853 | None ->
2854 if nogeomcmds state.geomcmds
2855 then state.anchor <- getanchor ();
2856 conf.angle <- angle mod 360;
2857 if conf.angle != 0
2858 then (
2859 match state.mode with
2860 | LinkNav _ -> state.mode <- View
2861 | _ -> ()
2863 conf.fitmodel <- fitmodel;
2864 invalidate "reqlayout"
2865 (fun () ->
2866 wcmd "reqlayout %d %d %d"
2867 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2869 | _ -> ()
2872 let settrim trimmargins trimfuzz =
2873 if nogeomcmds state.geomcmds
2874 then state.anchor <- getanchor ();
2875 conf.trimmargins <- trimmargins;
2876 conf.trimfuzz <- trimfuzz;
2877 let x0, y0, x1, y1 = trimfuzz in
2878 invalidate "settrim"
2879 (fun () ->
2880 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2881 flushpages ();
2884 let setzoom zoom =
2885 match state.throttle with
2886 | None ->
2887 let zoom = max 0.0001 zoom in
2888 if zoom <> conf.zoom
2889 then (
2890 state.prevzoom <- (conf.zoom, state.x);
2891 conf.zoom <- zoom;
2892 reshape state.winw state.winh;
2893 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2896 | Some (layout, y, started) ->
2897 let time =
2898 match conf.maxwait with
2899 | None -> 0.0
2900 | Some t -> t
2902 let dt = now () -. started in
2903 if dt > time
2904 then (
2905 state.y <- y;
2906 load layout;
2910 let setcolumns mode columns coverA coverB =
2911 state.prevcolumns <- Some (conf.columns, conf.zoom);
2912 if columns < 0
2913 then (
2914 if isbirdseye mode
2915 then showtext '!' "split mode doesn't work in bird's eye"
2916 else (
2917 conf.columns <- Csplit (-columns, [||]);
2918 state.x <- 0;
2919 conf.zoom <- 1.0;
2922 else (
2923 if columns < 2
2924 then (
2925 conf.columns <- Csingle [||];
2926 state.x <- 0;
2927 setzoom 1.0;
2929 else (
2930 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2931 conf.zoom <- 1.0;
2934 reshape state.winw state.winh;
2937 let enterbirdseye () =
2938 let zoom = float conf.thumbw /. float state.winw in
2939 let birdseyepageno =
2940 let cy = state.winh / 2 in
2941 let fold = function
2942 | [] -> 0
2943 | l :: rest ->
2944 let rec fold best = function
2945 | [] -> best.pageno
2946 | l :: rest ->
2947 let d = cy - (l.pagedispy + l.pagevh/2)
2948 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2949 if abs d < abs dbest
2950 then fold l rest
2951 else best.pageno
2952 in fold l rest
2954 fold state.layout
2956 state.mode <- Birdseye (
2957 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2959 conf.zoom <- zoom;
2960 conf.presentation <- false;
2961 conf.interpagespace <- 10;
2962 conf.hlinks <- false;
2963 conf.fitmodel <- FitProportional;
2964 state.x <- 0;
2965 state.mstate <- Mnone;
2966 conf.maxwait <- None;
2967 conf.columns <- (
2968 match conf.beyecolumns with
2969 | Some c ->
2970 conf.zoom <- 1.0;
2971 Cmulti ((c, 0, 0), [||])
2972 | None -> Csingle [||]
2974 Wsi.setcursor Wsi.CURSOR_INHERIT;
2975 if conf.verbose
2976 then
2977 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2978 (100.0*.zoom)
2979 else
2980 state.text <- ""
2982 reshape state.winw state.winh;
2985 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2986 state.mode <- View;
2987 conf.zoom <- c.zoom;
2988 conf.presentation <- c.presentation;
2989 conf.interpagespace <- c.interpagespace;
2990 conf.maxwait <- c.maxwait;
2991 conf.hlinks <- c.hlinks;
2992 conf.fitmodel <- c.fitmodel;
2993 conf.beyecolumns <- (
2994 match conf.columns with
2995 | Cmulti ((c, _, _), _) -> Some c
2996 | Csingle _ -> None
2997 | Csplit _ -> failwith "leaving bird's eye split mode"
2999 conf.columns <- (
3000 match c.columns with
3001 | Cmulti (c, _) -> Cmulti (c, [||])
3002 | Csingle _ -> Csingle [||]
3003 | Csplit (c, _) -> Csplit (c, [||])
3005 state.x <- leftx;
3006 if conf.verbose
3007 then
3008 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3009 (100.0*.conf.zoom)
3011 reshape state.winw state.winh;
3012 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3015 let togglebirdseye () =
3016 match state.mode with
3017 | Birdseye vals -> leavebirdseye vals true
3018 | View -> enterbirdseye ()
3019 | _ -> ()
3022 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3023 let pageno = max 0 (pageno - incr) in
3024 let rec loop = function
3025 | [] -> gotopage1 pageno 0
3026 | l :: _ when l.pageno = pageno ->
3027 if l.pagedispy >= 0 && l.pagey = 0
3028 then G.postRedisplay "upbirdseye"
3029 else gotopage1 pageno 0
3030 | _ :: rest -> loop rest
3032 loop state.layout;
3033 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3036 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3037 let pageno = min (state.pagecount - 1) (pageno + incr) in
3038 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3039 let rec loop = function
3040 | [] ->
3041 let y, h = getpageyh pageno in
3042 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3043 gotoy (clamp dy)
3044 | l :: _ when l.pageno = pageno ->
3045 if l.pagevh != l.pageh
3046 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3047 else G.postRedisplay "downbirdseye"
3048 | _ :: rest -> loop rest
3050 loop state.layout
3053 let optentry mode _ key =
3054 let btos b = if b then "on" else "off" in
3055 if key >= 32 && key < 127
3056 then
3057 let c = Char.chr key in
3058 match c with
3059 | 's' ->
3060 let ondone s =
3061 try conf.scrollstep <- int_of_string s with exc ->
3062 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3064 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3066 | 'A' ->
3067 let ondone s =
3069 conf.autoscrollstep <- int_of_string s;
3070 if state.autoscroll <> None
3071 then state.autoscroll <- Some conf.autoscrollstep
3072 with exc ->
3073 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3075 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3077 | 'C' ->
3078 let ondone s =
3080 let n, a, b = multicolumns_of_string s in
3081 setcolumns mode n a b;
3082 with exc ->
3083 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3085 TEswitch ("columns: ", "", None, textentry, ondone, true)
3087 | 'Z' ->
3088 let ondone s =
3090 let zoom = float (int_of_string s) /. 100.0 in
3091 setzoom zoom
3092 with exc ->
3093 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3095 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3097 | 't' ->
3098 let ondone s =
3100 conf.thumbw <- bound (int_of_string s) 2 4096;
3101 state.text <-
3102 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3103 begin match mode with
3104 | Birdseye beye ->
3105 leavebirdseye beye false;
3106 enterbirdseye ();
3107 | _ -> ();
3109 with exc ->
3110 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3112 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3114 | 'R' ->
3115 let ondone s =
3116 match try
3117 Some (int_of_string s)
3118 with exc ->
3119 state.text <- Printf.sprintf "bad integer `%s': %s"
3120 s (exntos exc);
3121 None
3122 with
3123 | Some angle -> reqlayout angle conf.fitmodel
3124 | None -> ()
3126 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3128 | 'i' ->
3129 conf.icase <- not conf.icase;
3130 TEdone ("case insensitive search " ^ (btos conf.icase))
3132 | 'p' ->
3133 conf.preload <- not conf.preload;
3134 gotoy state.y;
3135 TEdone ("preload " ^ (btos conf.preload))
3137 | 'v' ->
3138 conf.verbose <- not conf.verbose;
3139 TEdone ("verbose " ^ (btos conf.verbose))
3141 | 'd' ->
3142 conf.debug <- not conf.debug;
3143 TEdone ("debug " ^ (btos conf.debug))
3145 | 'h' ->
3146 conf.maxhfit <- not conf.maxhfit;
3147 state.maxy <- calcheight ();
3148 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3150 | 'c' ->
3151 conf.crophack <- not conf.crophack;
3152 TEdone ("crophack " ^ btos conf.crophack)
3154 | 'a' ->
3155 let s =
3156 match conf.maxwait with
3157 | None ->
3158 conf.maxwait <- Some infinity;
3159 "always wait for page to complete"
3160 | Some _ ->
3161 conf.maxwait <- None;
3162 "show placeholder if page is not ready"
3164 TEdone s
3166 | 'f' ->
3167 conf.underinfo <- not conf.underinfo;
3168 TEdone ("underinfo " ^ btos conf.underinfo)
3170 | 'P' ->
3171 conf.savebmarks <- not conf.savebmarks;
3172 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3174 | 'S' ->
3175 let ondone s =
3177 let pageno, py =
3178 match state.layout with
3179 | [] -> 0, 0
3180 | l :: _ ->
3181 l.pageno, l.pagey
3183 conf.interpagespace <- int_of_string s;
3184 docolumns conf.columns;
3185 state.maxy <- calcheight ();
3186 let y = getpagey pageno in
3187 gotoy (y + py)
3188 with exc ->
3189 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3191 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3193 | 'l' ->
3194 let fm =
3195 match conf.fitmodel with
3196 | FitProportional -> FitWidth
3197 | _ -> FitProportional
3199 reqlayout conf.angle fm;
3200 TEdone ("proportional display " ^ btos (fm == FitProportional))
3202 | 'T' ->
3203 settrim (not conf.trimmargins) conf.trimfuzz;
3204 TEdone ("trim margins " ^ btos conf.trimmargins)
3206 | 'I' ->
3207 conf.invert <- not conf.invert;
3208 TEdone ("invert colors " ^ btos conf.invert)
3210 | 'x' ->
3211 let ondone s =
3212 cbput state.hists.sel s;
3213 conf.selcmd <- s;
3215 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3216 textentry, ondone, true)
3218 | 'M' ->
3219 if conf.pax == None
3220 then conf.pax <- Some (ref (0.0, 0, 0))
3221 else conf.pax <- None;
3222 TEdone ("PAX " ^ btos (conf.pax != None))
3224 | _ ->
3225 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3226 TEstop
3227 else
3228 TEcont state.text
3231 class type lvsource = object
3232 method getitemcount : int
3233 method getitem : int -> (string * int)
3234 method hasaction : int -> bool
3235 method exit :
3236 uioh:uioh ->
3237 cancel:bool ->
3238 active:int ->
3239 first:int ->
3240 pan:int ->
3241 qsearch:string ->
3242 uioh option
3243 method getactive : int
3244 method getfirst : int
3245 method getqsearch : string
3246 method setqsearch : string -> unit
3247 method getpan : int
3248 end;;
3250 class virtual lvsourcebase = object
3251 val mutable m_active = 0
3252 val mutable m_first = 0
3253 val mutable m_qsearch = ""
3254 val mutable m_pan = 0
3255 method getactive = m_active
3256 method getfirst = m_first
3257 method getqsearch = m_qsearch
3258 method getpan = m_pan
3259 method setqsearch s = m_qsearch <- s
3260 end;;
3262 let withoutlastutf8 s =
3263 let len = String.length s in
3264 if len = 0
3265 then s
3266 else
3267 let rec find pos =
3268 if pos = 0
3269 then pos
3270 else
3271 let b = Char.code s.[pos] in
3272 if b land 0b11000000 = 0b11000000
3273 then pos
3274 else find (pos-1)
3276 let first =
3277 if Char.code s.[len-1] land 0x80 = 0
3278 then len-1
3279 else find (len-1)
3281 String.sub s 0 first;
3284 let textentrykeyboard
3285 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3286 let key =
3287 if key >= 0xffb0 && key <= 0xffb9
3288 then key - 0xffb0 + 48 else key
3290 let enttext te =
3291 state.mode <- Textentry (te, onleave);
3292 state.text <- "";
3293 enttext ();
3294 G.postRedisplay "textentrykeyboard enttext";
3296 let histaction cmd =
3297 match opthist with
3298 | None -> ()
3299 | Some (action, _) ->
3300 state.mode <- Textentry (
3301 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3303 G.postRedisplay "textentry histaction"
3305 match key with
3306 | 0xff08 -> (* backspace *)
3307 let s = withoutlastutf8 text in
3308 let len = String.length s in
3309 if cancelonempty && len = 0
3310 then (
3311 onleave Cancel;
3312 G.postRedisplay "textentrykeyboard after cancel";
3314 else (
3315 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3318 | 0xff0d | 0xff8d -> (* (kp) enter *)
3319 ondone text;
3320 onleave Confirm;
3321 G.postRedisplay "textentrykeyboard after confirm"
3323 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3324 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3325 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3326 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3328 | 0xff1b -> (* escape*)
3329 if String.length text = 0
3330 then (
3331 begin match opthist with
3332 | None -> ()
3333 | Some (_, onhistcancel) -> onhistcancel ()
3334 end;
3335 onleave Cancel;
3336 state.text <- "";
3337 G.postRedisplay "textentrykeyboard after cancel2"
3339 else (
3340 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3343 | 0xff9f | 0xffff -> () (* delete *)
3345 | _ when key != 0
3346 && key land 0xff00 != 0xff00 (* keyboard *)
3347 && key land 0xfe00 != 0xfe00 (* xkb *)
3348 && key land 0xfd00 != 0xfd00 (* 3270 *)
3350 begin match onkey text key with
3351 | TEdone text ->
3352 ondone text;
3353 onleave Confirm;
3354 G.postRedisplay "textentrykeyboard after confirm2";
3356 | TEcont text ->
3357 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3359 | TEstop ->
3360 onleave Cancel;
3361 G.postRedisplay "textentrykeyboard after cancel3"
3363 | TEswitch te ->
3364 state.mode <- Textentry (te, onleave);
3365 G.postRedisplay "textentrykeyboard switch";
3366 end;
3368 | _ ->
3369 vlog "unhandled key %s" (Wsi.keyname key)
3372 let firstof first active =
3373 if first > active || abs (first - active) > fstate.maxrows - 1
3374 then max 0 (active - (fstate.maxrows/2))
3375 else first
3378 let calcfirst first active =
3379 if active > first
3380 then
3381 let rows = active - first in
3382 if rows > fstate.maxrows then active - fstate.maxrows else first
3383 else active
3386 let scrollph y maxy =
3387 let sh = float (maxy + state.winh) /. float state.winh in
3388 let sh = float state.winh /. sh in
3389 let sh = max sh (float conf.scrollh) in
3391 let percent = float y /. float maxy in
3392 let position = (float state.winh -. sh) *. percent in
3394 let position =
3395 if position +. sh > float state.winh
3396 then float state.winh -. sh
3397 else position
3399 position, sh;
3402 let coe s = (s :> uioh);;
3404 class listview ~(source:lvsource) ~trusted ~modehash =
3405 object (self)
3406 val m_pan = source#getpan
3407 val m_first = source#getfirst
3408 val m_active = source#getactive
3409 val m_qsearch = source#getqsearch
3410 val m_prev_uioh = state.uioh
3412 method private elemunder y =
3413 let n = y / (fstate.fontsize+1) in
3414 if m_first + n < source#getitemcount
3415 then (
3416 if source#hasaction (m_first + n)
3417 then Some (m_first + n)
3418 else None
3420 else None
3422 method display =
3423 Gl.enable `blend;
3424 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3425 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3426 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3427 GlDraw.color (1., 1., 1.);
3428 Gl.enable `texture_2d;
3429 let fs = fstate.fontsize in
3430 let nfs = fs + 1 in
3431 let ww = fstate.wwidth in
3432 let tabw = 30.0*.ww in
3433 let itemcount = source#getitemcount in
3434 let rec loop row =
3435 if (row - m_first) > fstate.maxrows
3436 then ()
3437 else (
3438 if row >= 0 && row < itemcount
3439 then (
3440 let (s, level) = source#getitem row in
3441 let y = (row - m_first) * nfs in
3442 let x = 5.0 +. float (level + m_pan) *. ww in
3443 if row = m_active
3444 then (
3445 Gl.disable `texture_2d;
3446 GlDraw.polygon_mode `both `line;
3447 let alpha = if source#hasaction row then 0.9 else 0.3 in
3448 GlDraw.color (1., 1., 1.) ~alpha;
3449 GlDraw.rect (1., float (y + 1))
3450 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3451 GlDraw.polygon_mode `both `fill;
3452 GlDraw.color (1., 1., 1.);
3453 Gl.enable `texture_2d;
3456 let drawtabularstring s =
3457 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3458 if trusted
3459 then
3460 let tabpos = try String.index s '\t' with Not_found -> -1 in
3461 if tabpos > 0
3462 then
3463 let len = String.length s - tabpos - 1 in
3464 let s1 = String.sub s 0 tabpos
3465 and s2 = String.sub s (tabpos + 1) len in
3466 let nx = drawstr x s1 in
3467 let sw = nx -. x in
3468 let x = x +. (max tabw sw) in
3469 drawstr x s2
3470 else
3471 drawstr x s
3472 else
3473 drawstr x s
3475 let _ = drawtabularstring s in
3476 loop (row+1)
3480 loop m_first;
3481 Gl.disable `blend;
3482 Gl.disable `texture_2d;
3484 method updownlevel incr =
3485 let len = source#getitemcount in
3486 let curlevel =
3487 if m_active >= 0 && m_active < len
3488 then snd (source#getitem m_active)
3489 else -1
3491 let rec flow i =
3492 if i = len then i-1 else if i = -1 then 0 else
3493 let _, l = source#getitem i in
3494 if l != curlevel then i else flow (i+incr)
3496 let active = flow m_active in
3497 let first = calcfirst m_first active in
3498 G.postRedisplay "outline updownlevel";
3499 {< m_active = active; m_first = first >}
3501 method private key1 key mask =
3502 let set1 active first qsearch =
3503 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3505 let search active pattern incr =
3506 let active = if active = -1 then m_first else active in
3507 let dosearch re =
3508 let rec loop n =
3509 if n >= 0 && n < source#getitemcount
3510 then (
3511 let s, _ = source#getitem n in
3513 (try ignore (Str.search_forward re s 0); true
3514 with Not_found -> false)
3515 then Some n
3516 else loop (n + incr)
3518 else None
3520 loop active
3523 let re = Str.regexp_case_fold pattern in
3524 dosearch re
3525 with Failure s ->
3526 state.text <- s;
3527 None
3529 let itemcount = source#getitemcount in
3530 let find start incr =
3531 let rec find i =
3532 if i = -1 || i = itemcount
3533 then -1
3534 else (
3535 if source#hasaction i
3536 then i
3537 else find (i + incr)
3540 find start
3542 let set active first =
3543 let first = bound first 0 (itemcount - fstate.maxrows) in
3544 state.text <- "";
3545 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3547 let navigate incr =
3548 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3549 let active, first =
3550 let incr1 = if incr > 0 then 1 else -1 in
3551 if isvisible m_first m_active
3552 then
3553 let next =
3554 let next = m_active + incr in
3555 let next =
3556 if next < 0 || next >= itemcount
3557 then -1
3558 else find next incr1
3560 if abs (m_active - next) > fstate.maxrows
3561 then -1
3562 else next
3564 if next = -1
3565 then
3566 let first = m_first + incr in
3567 let first = bound first 0 (itemcount - fstate.maxrows) in
3568 let next =
3569 let next = m_active + incr in
3570 let next = bound next 0 (itemcount - 1) in
3571 find next ~-incr1
3573 let active =
3574 if next = -1
3575 then m_active
3576 else (
3577 if isvisible first next
3578 then next
3579 else m_active
3582 active, first
3583 else
3584 let first = min next m_first in
3585 let first =
3586 if abs (next - first) > fstate.maxrows
3587 then first + incr
3588 else first
3590 next, first
3591 else
3592 let first = m_first + incr in
3593 let first = bound first 0 (itemcount - 1) in
3594 let active =
3595 let next = m_active + incr in
3596 let next = bound next 0 (itemcount - 1) in
3597 let next = find next incr1 in
3598 let active =
3599 if next = -1 || abs (m_active - first) > fstate.maxrows
3600 then (
3601 let active = if m_active = -1 then next else m_active in
3602 active
3604 else next
3606 if isvisible first active
3607 then active
3608 else -1
3610 active, first
3612 G.postRedisplay "listview navigate";
3613 set active first;
3615 match key with
3616 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3617 let incr = if key = 0x72 then -1 else 1 in
3618 let active, first =
3619 match search (m_active + incr) m_qsearch incr with
3620 | None ->
3621 state.text <- m_qsearch ^ " [not found]";
3622 m_active, m_first
3623 | Some active ->
3624 state.text <- m_qsearch;
3625 active, firstof m_first active
3627 G.postRedisplay "listview ctrl-r/s";
3628 set1 active first m_qsearch;
3630 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3631 if m_active >= 0 && m_active < source#getitemcount
3632 then (
3633 let s, _ = source#getitem m_active in
3634 selstring s;
3636 coe self
3638 | 0xff08 -> (* backspace *)
3639 if String.length m_qsearch = 0
3640 then coe self
3641 else (
3642 let qsearch = withoutlastutf8 m_qsearch in
3643 let len = String.length qsearch in
3644 if len = 0
3645 then (
3646 state.text <- "";
3647 G.postRedisplay "listview empty qsearch";
3648 set1 m_active m_first "";
3650 else
3651 let active, first =
3652 match search m_active qsearch ~-1 with
3653 | None ->
3654 state.text <- qsearch ^ " [not found]";
3655 m_active, m_first
3656 | Some active ->
3657 state.text <- qsearch;
3658 active, firstof m_first active
3660 G.postRedisplay "listview backspace qsearch";
3661 set1 active first qsearch
3664 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3665 let pattern = m_qsearch ^ toutf8 key in
3666 let active, first =
3667 match search m_active pattern 1 with
3668 | None ->
3669 state.text <- pattern ^ " [not found]";
3670 m_active, m_first
3671 | Some active ->
3672 state.text <- pattern;
3673 active, firstof m_first active
3675 G.postRedisplay "listview qsearch add";
3676 set1 active first pattern;
3678 | 0xff1b -> (* escape *)
3679 state.text <- "";
3680 if String.length m_qsearch = 0
3681 then (
3682 G.postRedisplay "list view escape";
3683 begin
3684 match
3685 source#exit (coe self) true m_active m_first m_pan m_qsearch
3686 with
3687 | None -> m_prev_uioh
3688 | Some uioh -> uioh
3691 else (
3692 G.postRedisplay "list view kill qsearch";
3693 source#setqsearch "";
3694 coe {< m_qsearch = "" >}
3697 | 0xff0d | 0xff8d -> (* (kp) enter *)
3698 state.text <- "";
3699 let self = {< m_qsearch = "" >} in
3700 source#setqsearch "";
3701 let opt =
3702 G.postRedisplay "listview enter";
3703 if m_active >= 0 && m_active < source#getitemcount
3704 then (
3705 source#exit (coe self) false m_active m_first m_pan "";
3707 else (
3708 source#exit (coe self) true m_active m_first m_pan "";
3711 begin match opt with
3712 | None -> m_prev_uioh
3713 | Some uioh -> uioh
3716 | 0xff9f | 0xffff -> (* (kp) delete *)
3717 coe self
3719 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3720 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3721 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3722 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3724 | 0xff53 | 0xff98 -> (* (kp) right *)
3725 state.text <- "";
3726 G.postRedisplay "listview right";
3727 coe {< m_pan = m_pan - 1 >}
3729 | 0xff51 | 0xff96 -> (* (kp) left *)
3730 state.text <- "";
3731 G.postRedisplay "listview left";
3732 coe {< m_pan = m_pan + 1 >}
3734 | 0xff50 | 0xff95 -> (* (kp) home *)
3735 let active = find 0 1 in
3736 G.postRedisplay "listview home";
3737 set active 0;
3739 | 0xff57 | 0xff9c -> (* (kp) end *)
3740 let first = max 0 (itemcount - fstate.maxrows) in
3741 let active = find (itemcount - 1) ~-1 in
3742 G.postRedisplay "listview end";
3743 set active first;
3745 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3746 coe self
3748 | _ ->
3749 dolog "listview unknown key %#x" key; coe self
3751 method key key mask =
3752 match state.mode with
3753 | Textentry te -> textentrykeyboard key mask te; coe self
3754 | _ -> self#key1 key mask
3756 method button button down x y _ =
3757 let opt =
3758 match button with
3759 | 1 when x > state.winw - conf.scrollbw ->
3760 G.postRedisplay "listview scroll";
3761 if down
3762 then
3763 let _, position, sh = self#scrollph in
3764 if y > truncate position && y < truncate (position +. sh)
3765 then (
3766 state.mstate <- Mscrolly;
3767 Some (coe self)
3769 else
3770 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3771 let first = truncate (s *. float source#getitemcount) in
3772 let first = min source#getitemcount first in
3773 Some (coe {< m_first = first; m_active = first >})
3774 else (
3775 state.mstate <- Mnone;
3776 Some (coe self);
3778 | 1 when not down ->
3779 begin match self#elemunder y with
3780 | Some n ->
3781 G.postRedisplay "listview click";
3782 source#exit
3783 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3784 | _ ->
3785 Some (coe self)
3787 | n when (n == 4 || n == 5) && not down ->
3788 let len = source#getitemcount in
3789 let first =
3790 if n = 5 && m_first + fstate.maxrows >= len
3791 then
3792 m_first
3793 else
3794 let first = m_first + (if n == 4 then -1 else 1) in
3795 bound first 0 (len - 1)
3797 G.postRedisplay "listview wheel";
3798 Some (coe {< m_first = first >})
3799 | n when (n = 6 || n = 7) && not down ->
3800 let inc = if n = 7 then -1 else 1 in
3801 G.postRedisplay "listview hwheel";
3802 Some (coe {< m_pan = m_pan + inc >})
3803 | _ ->
3804 Some (coe self)
3806 match opt with
3807 | None -> m_prev_uioh
3808 | Some uioh -> uioh
3810 method motion _ y =
3811 match state.mstate with
3812 | Mscrolly ->
3813 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3814 let first = truncate (s *. float source#getitemcount) in
3815 let first = min source#getitemcount first in
3816 G.postRedisplay "listview motion";
3817 coe {< m_first = first; m_active = first >}
3818 | _ -> coe self
3820 method pmotion x y =
3821 if x < state.winw - conf.scrollbw
3822 then
3823 let n =
3824 match self#elemunder y with
3825 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3826 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3828 let o =
3829 if n != m_active
3830 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3831 else self
3833 coe o
3834 else (
3835 Wsi.setcursor Wsi.CURSOR_INHERIT;
3836 coe self
3839 method infochanged _ = ()
3841 method scrollpw = (0, 0.0, 0.0)
3842 method scrollph =
3843 let nfs = fstate.fontsize + 1 in
3844 let y = m_first * nfs in
3845 let itemcount = source#getitemcount in
3846 let maxi = max 0 (itemcount - fstate.maxrows) in
3847 let maxy = maxi * nfs in
3848 let p, h = scrollph y maxy in
3849 conf.scrollbw, p, h
3851 method modehash = modehash
3852 method eformsgs = false
3853 end;;
3855 class outlinelistview ~source =
3856 object (self)
3857 inherit listview
3858 ~source:(source :> lvsource)
3859 ~trusted:false
3860 ~modehash:(findkeyhash conf "outline")
3861 as super
3863 method key key mask =
3864 let maxrows =
3865 if String.length state.text = 0
3866 then fstate.maxrows
3867 else fstate.maxrows - 2
3869 let calcfirst first active =
3870 if active > first
3871 then
3872 let rows = active - first in
3873 if rows > maxrows then active - maxrows else first
3874 else active
3876 let navigate incr =
3877 let active = m_active + incr in
3878 let active = bound active 0 (source#getitemcount - 1) in
3879 let first = calcfirst m_first active in
3880 G.postRedisplay "outline navigate";
3881 coe {< m_active = active; m_first = first >}
3883 let navscroll first =
3884 let active =
3885 let dist = m_active - first in
3886 if dist < 0
3887 then first
3888 else (
3889 if dist < maxrows
3890 then m_active
3891 else first + maxrows
3894 G.postRedisplay "outline navscroll";
3895 coe {< m_first = first; m_active = active >}
3897 let ctrl = Wsi.withctrl mask in
3898 match key with
3899 | 110 when ctrl -> (* ctrl-n *)
3900 source#narrow m_qsearch;
3901 G.postRedisplay "outline ctrl-n";
3902 coe {< m_first = 0; m_active = 0 >}
3904 | 117 when ctrl -> (* ctrl-u *)
3905 source#denarrow;
3906 G.postRedisplay "outline ctrl-u";
3907 state.text <- "";
3908 coe {< m_first = 0; m_active = 0 >}
3910 | 108 when ctrl -> (* ctrl-l *)
3911 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3912 G.postRedisplay "outline ctrl-l";
3913 coe {< m_first = first >}
3915 | 0xff9f | 0xffff -> (* (kp) delete *)
3916 source#remove m_active;
3917 G.postRedisplay "outline delete";
3918 let active = max 0 (m_active-1) in
3919 coe {< m_first = firstof m_first active;
3920 m_active = active >}
3922 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3923 navscroll (max 0 (m_first - 1))
3925 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3926 navscroll (min (source#getitemcount - 1) (m_first + 1))
3928 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3929 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3930 | 0xff55 | 0xff9a -> (* (kp) prior *)
3931 navigate ~-(fstate.maxrows)
3932 | 0xff56 | 0xff9b -> (* (kp) next *)
3933 navigate fstate.maxrows
3935 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3936 let o =
3937 if ctrl
3938 then (
3939 G.postRedisplay "outline ctrl right";
3940 {< m_pan = m_pan + 1 >}
3942 else self#updownlevel 1
3944 coe o
3946 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3947 let o =
3948 if ctrl
3949 then (
3950 G.postRedisplay "outline ctrl left";
3951 {< m_pan = m_pan - 1 >}
3953 else self#updownlevel ~-1
3955 coe o
3957 | 0xff50 | 0xff95 -> (* (kp) home *)
3958 G.postRedisplay "outline home";
3959 coe {< m_first = 0; m_active = 0 >}
3961 | 0xff57 | 0xff9c -> (* (kp) end *)
3962 let active = source#getitemcount - 1 in
3963 let first = max 0 (active - fstate.maxrows) in
3964 G.postRedisplay "outline end";
3965 coe {< m_active = active; m_first = first >}
3967 | _ -> super#key key mask
3970 let outlinesource usebookmarks =
3971 let empty = [||] in
3972 (object
3973 inherit lvsourcebase
3974 val mutable m_items = empty
3975 val mutable m_orig_items = empty
3976 val mutable m_prev_items = empty
3977 val mutable m_narrow_pattern = ""
3978 val mutable m_hadremovals = false
3980 method getitemcount =
3981 Array.length m_items + (if m_hadremovals then 1 else 0)
3983 method getitem n =
3984 if n == Array.length m_items && m_hadremovals
3985 then
3986 ("[Confirm removal]", 0)
3987 else
3988 let s, n, _ = m_items.(n) in
3989 (s, n)
3991 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3992 ignore (uioh, first, qsearch);
3993 let confrimremoval = m_hadremovals && active = Array.length m_items in
3994 let items =
3995 if String.length m_narrow_pattern = 0
3996 then m_orig_items
3997 else m_items
3999 if not cancel
4000 then (
4001 if not confrimremoval
4002 then(
4003 let _, _, anchor = m_items.(active) in
4004 gotoghyll (getanchory anchor);
4005 m_items <- items;
4007 else (
4008 state.bookmarks <- Array.to_list m_items;
4009 m_orig_items <- m_items;
4012 else m_items <- items;
4013 m_pan <- pan;
4014 None
4016 method hasaction _ = true
4018 method greetmsg =
4019 if Array.length m_items != Array.length m_orig_items
4020 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
4021 else ""
4023 method narrow pattern =
4024 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4025 match reopt with
4026 | None -> ()
4027 | Some re ->
4028 let rec loop accu n =
4029 if n = -1
4030 then (
4031 m_narrow_pattern <- pattern;
4032 m_items <- Array.of_list accu
4034 else
4035 let (s, _, _) as o = m_items.(n) in
4036 let accu =
4037 if (try ignore (Str.search_forward re s 0); true
4038 with Not_found -> false)
4039 then o :: accu
4040 else accu
4042 loop accu (n-1)
4044 loop [] (Array.length m_items - 1)
4046 method denarrow =
4047 m_orig_items <- (
4048 if usebookmarks
4049 then Array.of_list state.bookmarks
4050 else state.outlines
4052 m_items <- m_orig_items
4054 method remove m =
4055 if usebookmarks
4056 then
4057 if m >= 0 && m < Array.length m_items
4058 then (
4059 m_hadremovals <- true;
4060 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4061 let n = if n >= m then n+1 else n in
4062 m_items.(n)
4066 method reset anchor items =
4067 m_hadremovals <- false;
4068 if m_orig_items == empty || m_prev_items != items
4069 then (
4070 m_orig_items <- items;
4071 if String.length m_narrow_pattern = 0
4072 then m_items <- items;
4074 m_prev_items <- items;
4075 let rely = getanchory anchor in
4076 let active =
4077 let rec loop n best bestd =
4078 if n = Array.length m_items
4079 then best
4080 else
4081 let (_, _, anchor) = m_items.(n) in
4082 let orely = getanchory anchor in
4083 let d = abs (orely - rely) in
4084 if d < bestd
4085 then loop (n+1) n d
4086 else loop (n+1) best bestd
4088 loop 0 ~-1 max_int
4090 m_active <- active;
4091 m_first <- firstof m_first active
4092 end)
4095 let enterselector usebookmarks =
4096 let source = outlinesource usebookmarks in
4097 fun errmsg ->
4098 let outlines =
4099 if usebookmarks
4100 then Array.of_list state.bookmarks
4101 else state.outlines
4103 if Array.length outlines = 0
4104 then (
4105 showtext ' ' errmsg;
4107 else (
4108 state.text <- source#greetmsg;
4109 Wsi.setcursor Wsi.CURSOR_INHERIT;
4110 let anchor = getanchor () in
4111 source#reset anchor outlines;
4112 state.uioh <- coe (new outlinelistview ~source);
4113 G.postRedisplay "enter selector";
4117 let enteroutlinemode =
4118 let f = enterselector false in
4119 fun ()-> f "Document has no outline";
4122 let enterbookmarkmode =
4123 let f = enterselector true in
4124 fun () -> f "Document has no bookmarks (yet)";
4127 let color_of_string s =
4128 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4129 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4133 let color_to_string (r, g, b) =
4134 let r = truncate (r *. 256.0)
4135 and g = truncate (g *. 256.0)
4136 and b = truncate (b *. 256.0) in
4137 Printf.sprintf "%d/%d/%d" r g b
4140 let irect_of_string s =
4141 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4144 let irect_to_string (x0,y0,x1,y1) =
4145 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4148 let makecheckers () =
4149 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4150 following to say:
4151 converted by Issac Trotts. July 25, 2002 *)
4152 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4153 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4154 let id = GlTex.gen_texture () in
4155 GlTex.bind_texture `texture_2d id;
4156 GlPix.store (`unpack_alignment 1);
4157 GlTex.image2d image;
4158 List.iter (GlTex.parameter ~target:`texture_2d)
4159 [ `mag_filter `nearest; `min_filter `nearest ];
4163 let setcheckers enabled =
4164 match state.texid with
4165 | None ->
4166 if enabled then state.texid <- Some (makecheckers ())
4168 | Some texid ->
4169 if not enabled
4170 then (
4171 GlTex.delete_texture texid;
4172 state.texid <- None;
4176 let int_of_string_with_suffix s =
4177 let l = String.length s in
4178 let s1, shift =
4179 if l > 1
4180 then
4181 let suffix = Char.lowercase s.[l-1] in
4182 match suffix with
4183 | 'k' -> String.sub s 0 (l-1), 10
4184 | 'm' -> String.sub s 0 (l-1), 20
4185 | 'g' -> String.sub s 0 (l-1), 30
4186 | _ -> s, 0
4187 else s, 0
4189 let n = int_of_string s1 in
4190 let m = n lsl shift in
4191 if m < 0 || m < n
4192 then raise (Failure "value too large")
4193 else m
4196 let string_with_suffix_of_int n =
4197 if n = 0
4198 then "0"
4199 else
4200 let n, s =
4201 if n land ((1 lsl 30) - 1) = 0
4202 then n lsr 30, "G"
4203 else (
4204 if n land ((1 lsl 20) - 1) = 0
4205 then n lsr 20, "M"
4206 else (
4207 if n land ((1 lsl 10) - 1) = 0
4208 then n lsr 10, "K"
4209 else n, ""
4213 let rec loop s n =
4214 let h = n mod 1000 in
4215 let n = n / 1000 in
4216 if n = 0
4217 then string_of_int h ^ s
4218 else (
4219 let s = Printf.sprintf "_%03d%s" h s in
4220 loop s n
4223 loop "" n ^ s;
4226 let defghyllscroll = (40, 8, 32);;
4227 let ghyllscroll_of_string s =
4228 let (n, a, b) as nab =
4229 if s = "default"
4230 then defghyllscroll
4231 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4233 if n <= a || n <= b || a >= b
4234 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4235 nab;
4238 let ghyllscroll_to_string ((n, a, b) as nab) =
4239 if nab = defghyllscroll
4240 then "default"
4241 else Printf.sprintf "%d,%d,%d" n a b;
4244 let describe_location () =
4245 let fn = page_of_y state.y in
4246 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4247 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4248 let percent =
4249 if maxy <= 0
4250 then 100.
4251 else (100. *. (float state.y /. float maxy))
4253 if fn = ln
4254 then
4255 Printf.sprintf "page %d of %d [%.2f%%]"
4256 (fn+1) state.pagecount percent
4257 else
4258 Printf.sprintf
4259 "pages %d-%d of %d [%.2f%%]"
4260 (fn+1) (ln+1) state.pagecount percent
4263 let setpresentationmode v =
4264 let n = page_of_y state.y in
4265 state.anchor <- (n, 0.0, 1.0);
4266 conf.presentation <- v;
4267 if conf.fitmodel = FitPage
4268 then reqlayout conf.angle conf.fitmodel;
4269 represent ();
4272 let enterinfomode =
4273 let btos b = if b then "\xe2\x88\x9a" else "" in
4274 let showextended = ref false in
4275 let leave mode = function
4276 | Confirm -> state.mode <- mode
4277 | Cancel -> state.mode <- mode in
4278 let src =
4279 (object
4280 val mutable m_first_time = true
4281 val mutable m_l = []
4282 val mutable m_a = [||]
4283 val mutable m_prev_uioh = nouioh
4284 val mutable m_prev_mode = View
4286 inherit lvsourcebase
4288 method reset prev_mode prev_uioh =
4289 m_a <- Array.of_list (List.rev m_l);
4290 m_l <- [];
4291 m_prev_mode <- prev_mode;
4292 m_prev_uioh <- prev_uioh;
4293 if m_first_time
4294 then (
4295 let rec loop n =
4296 if n >= Array.length m_a
4297 then ()
4298 else
4299 match m_a.(n) with
4300 | _, _, _, Action _ -> m_active <- n
4301 | _ -> loop (n+1)
4303 loop 0;
4304 m_first_time <- false;
4307 method int name get set =
4308 m_l <-
4309 (name, `int get, 1, Action (
4310 fun u ->
4311 let ondone s =
4312 try set (int_of_string s)
4313 with exn ->
4314 state.text <- Printf.sprintf "bad integer `%s': %s"
4315 s (exntos exn)
4317 state.text <- "";
4318 let te = name ^ ": ", "", None, intentry, ondone, true in
4319 state.mode <- Textentry (te, leave m_prev_mode);
4321 )) :: m_l
4323 method int_with_suffix name get set =
4324 m_l <-
4325 (name, `intws get, 1, Action (
4326 fun u ->
4327 let ondone s =
4328 try set (int_of_string_with_suffix s)
4329 with exn ->
4330 state.text <- Printf.sprintf "bad integer `%s': %s"
4331 s (exntos exn)
4333 state.text <- "";
4334 let te =
4335 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4337 state.mode <- Textentry (te, leave m_prev_mode);
4339 )) :: m_l
4341 method bool ?(offset=1) ?(btos=btos) name get set =
4342 m_l <-
4343 (name, `bool (btos, get), offset, Action (
4344 fun u ->
4345 let v = get () in
4346 set (not v);
4348 )) :: m_l
4350 method color name get set =
4351 m_l <-
4352 (name, `color get, 1, Action (
4353 fun u ->
4354 let invalid = (nan, nan, nan) in
4355 let ondone s =
4356 let c =
4357 try color_of_string s
4358 with exn ->
4359 state.text <- Printf.sprintf "bad color `%s': %s"
4360 s (exntos exn);
4361 invalid
4363 if c <> invalid
4364 then set c;
4366 let te = name ^ ": ", "", None, textentry, ondone, true in
4367 state.text <- color_to_string (get ());
4368 state.mode <- Textentry (te, leave m_prev_mode);
4370 )) :: m_l
4372 method string name get set =
4373 m_l <-
4374 (name, `string get, 1, Action (
4375 fun u ->
4376 let ondone s = set s in
4377 let te = name ^ ": ", "", None, textentry, ondone, true in
4378 state.mode <- Textentry (te, leave m_prev_mode);
4380 )) :: m_l
4382 method colorspace name get set =
4383 m_l <-
4384 (name, `string get, 1, Action (
4385 fun _ ->
4386 let source =
4387 (object
4388 inherit lvsourcebase
4390 initializer
4391 m_active <- CSTE.to_int conf.colorspace;
4392 m_first <- 0;
4394 method getitemcount =
4395 Array.length CSTE.names
4396 method getitem n =
4397 (CSTE.names.(n), 0)
4398 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4399 ignore (uioh, first, pan, qsearch);
4400 if not cancel then set active;
4401 None
4402 method hasaction _ = true
4403 end)
4405 state.text <- "";
4406 let modehash = findkeyhash conf "info" in
4407 coe (new listview ~source ~trusted:true ~modehash)
4408 )) :: m_l
4410 method paxmark name get set =
4411 m_l <-
4412 (name, `string get, 1, Action (
4413 fun _ ->
4414 let source =
4415 (object
4416 inherit lvsourcebase
4418 initializer
4419 m_active <- MTE.to_int conf.paxmark;
4420 m_first <- 0;
4422 method getitemcount = Array.length MTE.names
4423 method getitem n = (MTE.names.(n), 0)
4424 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4425 ignore (uioh, first, pan, qsearch);
4426 if not cancel then set active;
4427 None
4428 method hasaction _ = true
4429 end)
4431 state.text <- "";
4432 let modehash = findkeyhash conf "info" in
4433 coe (new listview ~source ~trusted:true ~modehash)
4434 )) :: m_l
4436 method fitmodel name get set =
4437 m_l <-
4438 (name, `string get, 1, Action (
4439 fun _ ->
4440 let source =
4441 (object
4442 inherit lvsourcebase
4444 initializer
4445 m_active <- FMTE.to_int conf.fitmodel;
4446 m_first <- 0;
4448 method getitemcount = Array.length FMTE.names
4449 method getitem n = (FMTE.names.(n), 0)
4450 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4451 ignore (uioh, first, pan, qsearch);
4452 if not cancel then set active;
4453 None
4454 method hasaction _ = true
4455 end)
4457 state.text <- "";
4458 let modehash = findkeyhash conf "info" in
4459 coe (new listview ~source ~trusted:true ~modehash)
4460 )) :: m_l
4462 method caption s offset =
4463 m_l <- (s, `empty, offset, Noaction) :: m_l
4465 method caption2 s f offset =
4466 m_l <- (s, `string f, offset, Noaction) :: m_l
4468 method getitemcount = Array.length m_a
4470 method getitem n =
4471 let tostr = function
4472 | `int f -> string_of_int (f ())
4473 | `intws f -> string_with_suffix_of_int (f ())
4474 | `string f -> f ()
4475 | `color f -> color_to_string (f ())
4476 | `bool (btos, f) -> btos (f ())
4477 | `empty -> ""
4479 let name, t, offset, _ = m_a.(n) in
4480 ((let s = tostr t in
4481 if String.length s > 0
4482 then Printf.sprintf "%s\t%s" name s
4483 else name),
4484 offset)
4486 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4487 let uiohopt =
4488 if not cancel
4489 then (
4490 m_qsearch <- qsearch;
4491 let uioh =
4492 match m_a.(active) with
4493 | _, _, _, Action f -> f uioh
4494 | _ -> uioh
4496 Some uioh
4498 else None
4500 m_active <- active;
4501 m_first <- first;
4502 m_pan <- pan;
4503 uiohopt
4505 method hasaction n =
4506 match m_a.(n) with
4507 | _, _, _, Action _ -> true
4508 | _ -> false
4509 end)
4511 let rec fillsrc prevmode prevuioh =
4512 let sep () = src#caption "" 0 in
4513 let colorp name get set =
4514 src#string name
4515 (fun () -> color_to_string (get ()))
4516 (fun v ->
4518 let c = color_of_string v in
4519 set c
4520 with exn ->
4521 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4524 let oldmode = state.mode in
4525 let birdseye = isbirdseye state.mode in
4527 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4529 src#bool "presentation mode"
4530 (fun () -> conf.presentation)
4531 (fun v -> setpresentationmode v);
4533 src#bool "ignore case in searches"
4534 (fun () -> conf.icase)
4535 (fun v -> conf.icase <- v);
4537 src#bool "preload"
4538 (fun () -> conf.preload)
4539 (fun v -> conf.preload <- v);
4541 src#bool "highlight links"
4542 (fun () -> conf.hlinks)
4543 (fun v -> conf.hlinks <- v);
4545 src#bool "under info"
4546 (fun () -> conf.underinfo)
4547 (fun v -> conf.underinfo <- v);
4549 src#bool "persistent bookmarks"
4550 (fun () -> conf.savebmarks)
4551 (fun v -> conf.savebmarks <- v);
4553 src#fitmodel "fit model"
4554 (fun () -> FMTE.to_string conf.fitmodel)
4555 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4557 src#bool "trim margins"
4558 (fun () -> conf.trimmargins)
4559 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4561 src#bool "persistent location"
4562 (fun () -> conf.jumpback)
4563 (fun v -> conf.jumpback <- v);
4565 sep ();
4566 src#int "inter-page space"
4567 (fun () -> conf.interpagespace)
4568 (fun n ->
4569 conf.interpagespace <- n;
4570 docolumns conf.columns;
4571 let pageno, py =
4572 match state.layout with
4573 | [] -> 0, 0
4574 | l :: _ ->
4575 l.pageno, l.pagey
4577 state.maxy <- calcheight ();
4578 let y = getpagey pageno in
4579 gotoy (y + py)
4582 src#int "page bias"
4583 (fun () -> conf.pagebias)
4584 (fun v -> conf.pagebias <- v);
4586 src#int "scroll step"
4587 (fun () -> conf.scrollstep)
4588 (fun n -> conf.scrollstep <- n);
4590 src#int "horizontal scroll step"
4591 (fun () -> conf.hscrollstep)
4592 (fun v -> conf.hscrollstep <- v);
4594 src#int "auto scroll step"
4595 (fun () ->
4596 match state.autoscroll with
4597 | Some step -> step
4598 | _ -> conf.autoscrollstep)
4599 (fun n ->
4600 if state.autoscroll <> None
4601 then state.autoscroll <- Some n;
4602 conf.autoscrollstep <- n);
4604 src#int "zoom"
4605 (fun () -> truncate (conf.zoom *. 100.))
4606 (fun v -> setzoom ((float v) /. 100.));
4608 src#int "rotation"
4609 (fun () -> conf.angle)
4610 (fun v -> reqlayout v conf.fitmodel);
4612 src#int "scroll bar width"
4613 (fun () -> conf.scrollbw)
4614 (fun v ->
4615 conf.scrollbw <- v;
4616 reshape state.winw state.winh;
4619 src#int "scroll handle height"
4620 (fun () -> conf.scrollh)
4621 (fun v -> conf.scrollh <- v;);
4623 src#int "thumbnail width"
4624 (fun () -> conf.thumbw)
4625 (fun v ->
4626 conf.thumbw <- min 4096 v;
4627 match oldmode with
4628 | Birdseye beye ->
4629 leavebirdseye beye false;
4630 enterbirdseye ()
4631 | _ -> ()
4634 let mode = state.mode in
4635 src#string "columns"
4636 (fun () ->
4637 match conf.columns with
4638 | Csingle _ -> "1"
4639 | Cmulti (multi, _) -> multicolumns_to_string multi
4640 | Csplit (count, _) -> "-" ^ string_of_int count
4642 (fun v ->
4643 let n, a, b = multicolumns_of_string v in
4644 setcolumns mode n a b);
4646 sep ();
4647 src#caption "Pixmap cache" 0;
4648 src#int_with_suffix "size (advisory)"
4649 (fun () -> conf.memlimit)
4650 (fun v -> conf.memlimit <- v);
4652 src#caption2 "used"
4653 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4654 (string_with_suffix_of_int state.memused)
4655 (Hashtbl.length state.tilemap)) 1;
4657 sep ();
4658 src#caption "Layout" 0;
4659 src#caption2 "Dimension"
4660 (fun () ->
4661 Printf.sprintf "%dx%d (virtual %dx%d)"
4662 state.winw state.winh
4663 state.w state.maxy)
4665 if conf.debug
4666 then
4667 src#caption2 "Position" (fun () ->
4668 Printf.sprintf "%dx%d" state.x state.y
4670 else
4671 src#caption2 "Position" (fun () -> describe_location ()) 1
4674 sep ();
4675 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4676 "Save these parameters as global defaults at exit"
4677 (fun () -> conf.bedefault)
4678 (fun v -> conf.bedefault <- v)
4681 sep ();
4682 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4683 src#bool ~offset:0 ~btos "Extended parameters"
4684 (fun () -> !showextended)
4685 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4686 if !showextended
4687 then (
4688 src#bool "checkers"
4689 (fun () -> conf.checkers)
4690 (fun v -> conf.checkers <- v; setcheckers v);
4691 src#bool "update cursor"
4692 (fun () -> conf.updatecurs)
4693 (fun v -> conf.updatecurs <- v);
4694 src#bool "verbose"
4695 (fun () -> conf.verbose)
4696 (fun v -> conf.verbose <- v);
4697 src#bool "invert colors"
4698 (fun () -> conf.invert)
4699 (fun v -> conf.invert <- v);
4700 src#bool "max fit"
4701 (fun () -> conf.maxhfit)
4702 (fun v -> conf.maxhfit <- v);
4703 src#bool "redirect stderr"
4704 (fun () -> conf.redirectstderr)
4705 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4706 src#bool "pax mode"
4707 (fun () -> conf.pax != None)
4708 (fun v ->
4709 if v
4710 then conf.pax <- Some (ref (now (), 0, 0))
4711 else conf.pax <- None);
4712 src#string "uri launcher"
4713 (fun () -> conf.urilauncher)
4714 (fun v -> conf.urilauncher <- v);
4715 src#string "path launcher"
4716 (fun () -> conf.pathlauncher)
4717 (fun v -> conf.pathlauncher <- v);
4718 src#string "tile size"
4719 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4720 (fun v ->
4722 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4723 conf.tilew <- max 64 w;
4724 conf.tileh <- max 64 h;
4725 flushtiles ();
4726 with exn ->
4727 state.text <- Printf.sprintf "bad tile size `%s': %s"
4728 v (exntos exn)
4730 src#int "texture count"
4731 (fun () -> conf.texcount)
4732 (fun v ->
4733 if realloctexts v
4734 then conf.texcount <- v
4735 else showtext '!' " Failed to set texture count please retry later"
4737 src#int "slice height"
4738 (fun () -> conf.sliceheight)
4739 (fun v ->
4740 conf.sliceheight <- v;
4741 wcmd "sliceh %d" conf.sliceheight;
4743 src#int "anti-aliasing level"
4744 (fun () -> conf.aalevel)
4745 (fun v ->
4746 conf.aalevel <- bound v 0 8;
4747 state.anchor <- getanchor ();
4748 opendoc state.path state.password;
4750 src#string "page scroll scaling factor"
4751 (fun () -> string_of_float conf.pgscale)
4752 (fun v ->
4754 let s = float_of_string v in
4755 conf.pgscale <- s
4756 with exn ->
4757 state.text <- Printf.sprintf
4758 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4761 src#int "ui font size"
4762 (fun () -> fstate.fontsize)
4763 (fun v -> setfontsize (bound v 5 100));
4764 src#int "hint font size"
4765 (fun () -> conf.hfsize)
4766 (fun v -> conf.hfsize <- bound v 5 100);
4767 colorp "background color"
4768 (fun () -> conf.bgcolor)
4769 (fun v -> conf.bgcolor <- v);
4770 src#bool "crop hack"
4771 (fun () -> conf.crophack)
4772 (fun v -> conf.crophack <- v);
4773 src#string "trim fuzz"
4774 (fun () -> irect_to_string conf.trimfuzz)
4775 (fun v ->
4777 conf.trimfuzz <- irect_of_string v;
4778 if conf.trimmargins
4779 then settrim true conf.trimfuzz;
4780 with exn ->
4781 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4783 src#string "throttle"
4784 (fun () ->
4785 match conf.maxwait with
4786 | None -> "show place holder if page is not ready"
4787 | Some time ->
4788 if time = infinity
4789 then "wait for page to fully render"
4790 else
4791 "wait " ^ string_of_float time
4792 ^ " seconds before showing placeholder"
4794 (fun v ->
4796 let f = float_of_string v in
4797 if f <= 0.0
4798 then conf.maxwait <- None
4799 else conf.maxwait <- Some f
4800 with exn ->
4801 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4803 src#string "ghyll scroll"
4804 (fun () ->
4805 match conf.ghyllscroll with
4806 | None -> ""
4807 | Some nab -> ghyllscroll_to_string nab
4809 (fun v ->
4811 let gs =
4812 if String.length v = 0
4813 then None
4814 else Some (ghyllscroll_of_string v)
4816 conf.ghyllscroll <- gs
4817 with exn ->
4818 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4820 src#string "selection command"
4821 (fun () -> conf.selcmd)
4822 (fun v -> conf.selcmd <- v);
4823 src#string "synctex command"
4824 (fun () -> conf.stcmd)
4825 (fun v -> conf.stcmd <- v);
4826 src#string "pax command"
4827 (fun () -> conf.paxcmd)
4828 (fun v -> conf.paxcmd <- v);
4829 src#colorspace "color space"
4830 (fun () -> CSTE.to_string conf.colorspace)
4831 (fun v ->
4832 conf.colorspace <- CSTE.of_int v;
4833 wcmd "cs %d" v;
4834 load state.layout;
4836 src#paxmark "pax mark method"
4837 (fun () -> MTE.to_string conf.paxmark)
4838 (fun v -> conf.paxmark <- MTE.of_int v);
4839 if pbousable ()
4840 then
4841 src#bool "use PBO"
4842 (fun () -> conf.usepbo)
4843 (fun v -> conf.usepbo <- v);
4844 src#bool "mouse wheel scrolls pages"
4845 (fun () -> conf.wheelbypage)
4846 (fun v -> conf.wheelbypage <- v);
4847 src#bool "open remote links in a new instance"
4848 (fun () -> conf.riani)
4849 (fun v -> conf.riani <- v);
4852 sep ();
4853 src#caption "Document" 0;
4854 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4855 src#caption2 "Pages"
4856 (fun () -> string_of_int state.pagecount) 1;
4857 src#caption2 "Dimensions"
4858 (fun () -> string_of_int (List.length state.pdims)) 1;
4859 if conf.trimmargins
4860 then (
4861 sep ();
4862 src#caption "Trimmed margins" 0;
4863 src#caption2 "Dimensions"
4864 (fun () -> string_of_int (List.length state.pdims)) 1;
4867 sep ();
4868 src#caption "OpenGL" 0;
4869 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4870 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4872 sep ();
4873 src#caption "Location" 0;
4874 if String.length state.origin > 0
4875 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4876 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4878 src#reset prevmode prevuioh;
4880 fun () ->
4881 state.text <- "";
4882 let prevmode = state.mode
4883 and prevuioh = state.uioh in
4884 fillsrc prevmode prevuioh;
4885 let source = (src :> lvsource) in
4886 let modehash = findkeyhash conf "info" in
4887 state.uioh <- coe (object (self)
4888 inherit listview ~source ~trusted:true ~modehash as super
4889 val mutable m_prevmemused = 0
4890 method infochanged = function
4891 | Memused ->
4892 if m_prevmemused != state.memused
4893 then (
4894 m_prevmemused <- state.memused;
4895 G.postRedisplay "memusedchanged";
4897 | Pdim -> G.postRedisplay "pdimchanged"
4898 | Docinfo -> fillsrc prevmode prevuioh
4900 method key key mask =
4901 if not (Wsi.withctrl mask)
4902 then
4903 match key with
4904 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4905 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4906 | _ -> super#key key mask
4907 else super#key key mask
4908 end);
4909 G.postRedisplay "info";
4912 let enterhelpmode =
4913 let source =
4914 (object
4915 inherit lvsourcebase
4916 method getitemcount = Array.length state.help
4917 method getitem n =
4918 let s, l, _ = state.help.(n) in
4919 (s, l)
4921 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4922 let optuioh =
4923 if not cancel
4924 then (
4925 m_qsearch <- qsearch;
4926 match state.help.(active) with
4927 | _, _, Action f -> Some (f uioh)
4928 | _ -> Some (uioh)
4930 else None
4932 m_active <- active;
4933 m_first <- first;
4934 m_pan <- pan;
4935 optuioh
4937 method hasaction n =
4938 match state.help.(n) with
4939 | _, _, Action _ -> true
4940 | _ -> false
4942 initializer
4943 m_active <- -1
4944 end)
4945 in fun () ->
4946 let modehash = findkeyhash conf "help" in
4947 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4948 G.postRedisplay "help";
4951 let entermsgsmode =
4952 let msgsource =
4953 let re = Str.regexp "[\r\n]" in
4954 (object
4955 inherit lvsourcebase
4956 val mutable m_items = [||]
4958 method getitemcount = 1 + Array.length m_items
4960 method getitem n =
4961 if n = 0
4962 then "[Clear]", 0
4963 else m_items.(n-1), 0
4965 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4966 ignore uioh;
4967 if not cancel
4968 then (
4969 if active = 0
4970 then Buffer.clear state.errmsgs;
4971 m_qsearch <- qsearch;
4973 m_active <- active;
4974 m_first <- first;
4975 m_pan <- pan;
4976 None
4978 method hasaction n =
4979 n = 0
4981 method reset =
4982 state.newerrmsgs <- false;
4983 let l = Str.split re (Buffer.contents state.errmsgs) in
4984 m_items <- Array.of_list l
4986 initializer
4987 m_active <- 0
4988 end)
4989 in fun () ->
4990 state.text <- "";
4991 msgsource#reset;
4992 let source = (msgsource :> lvsource) in
4993 let modehash = findkeyhash conf "listview" in
4994 state.uioh <- coe (object
4995 inherit listview ~source ~trusted:false ~modehash as super
4996 method display =
4997 if state.newerrmsgs
4998 then msgsource#reset;
4999 super#display
5000 end);
5001 G.postRedisplay "msgs";
5004 let quickbookmark ?title () =
5005 match state.layout with
5006 | [] -> ()
5007 | l :: _ ->
5008 let title =
5009 match title with
5010 | None ->
5011 let sec = Unix.gettimeofday () in
5012 let tm = Unix.localtime sec in
5013 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5014 (l.pageno+1)
5015 tm.Unix.tm_mday
5016 tm.Unix.tm_mon
5017 (tm.Unix.tm_year + 1900)
5018 tm.Unix.tm_hour
5019 tm.Unix.tm_min
5020 | Some title -> title
5022 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5025 let setautoscrollspeed step goingdown =
5026 let incr = max 1 ((abs step) / 2) in
5027 let incr = if goingdown then incr else -incr in
5028 let astep = step + incr in
5029 state.autoscroll <- Some astep;
5032 let gotounder = function
5033 | Ulinkgoto (pageno, top) ->
5034 if pageno >= 0
5035 then (
5036 addnav ();
5037 gotopage1 pageno top;
5040 | Ulinkuri s ->
5041 gotouri s
5043 | Uremote (filename, pageno) ->
5044 let path =
5045 if String.length filename > 0
5046 then
5047 if Filename.is_relative filename
5048 then
5049 let dir = Filename.dirname state.path in
5050 let dir =
5051 if Filename.is_implicit dir
5052 then Filename.concat (Sys.getcwd ()) dir
5053 else dir
5055 Filename.concat dir filename
5056 else filename
5057 else ""
5059 let path =
5060 if Sys.file_exists path
5061 then path
5062 else ""
5064 if String.length path > 0
5065 then (
5066 if conf.riani
5067 then
5068 let command = !selfexec ^ " " ^ path in
5069 try popen command []
5070 with exn ->
5071 Printf.eprintf
5072 "failed to execute `%s': %s\n" command (exntos exn);
5073 flush stderr;
5074 else
5075 let anchor = getanchor () in
5076 let ranchor = state.path, state.password, anchor, state.origin in
5077 state.origin <- "";
5078 state.anchor <- (pageno, 0.0, 0.0);
5079 state.ranchors <- ranchor :: state.ranchors;
5080 opendoc path "";
5082 else showtext '!' ("Could not find " ^ filename)
5084 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5087 let canpan () =
5088 match conf.columns with
5089 | Csplit _ -> true
5090 | _ -> state.x != 0 || conf.zoom > 1.0
5093 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5095 let existsinrow pageno (columns, coverA, coverB) p =
5096 let last = ((pageno - coverA) mod columns) + columns in
5097 let rec any = function
5098 | [] -> false
5099 | l :: rest ->
5100 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5101 then p l
5102 else (
5103 if not (p l)
5104 then (if l.pageno = last then false else any rest)
5105 else true
5108 any state.layout
5111 let nextpage () =
5112 match state.layout with
5113 | [] ->
5114 let pageno = page_of_y state.y in
5115 gotoghyll (getpagey (pageno+1))
5116 | l :: rest ->
5117 match conf.columns with
5118 | Csingle _ ->
5119 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5120 then
5121 let y = clamp (pgscale state.winh) in
5122 gotoghyll y
5123 else
5124 let pageno = min (l.pageno+1) (state.pagecount-1) in
5125 gotoghyll (getpagey pageno)
5126 | Cmulti ((c, _, _) as cl, _) ->
5127 if conf.presentation
5128 && (existsinrow l.pageno cl
5129 (fun l -> l.pageh > l.pagey + l.pagevh))
5130 then
5131 let y = clamp (pgscale state.winh) in
5132 gotoghyll y
5133 else
5134 let pageno = min (l.pageno+c) (state.pagecount-1) in
5135 gotoghyll (getpagey pageno)
5136 | Csplit (n, _) ->
5137 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5138 then
5139 let pagey, pageh = getpageyh l.pageno in
5140 let pagey = pagey + pageh * l.pagecol in
5141 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5142 gotoghyll (pagey + pageh + ips)
5145 let prevpage () =
5146 match state.layout with
5147 | [] ->
5148 let pageno = page_of_y state.y in
5149 gotoghyll (getpagey (pageno-1))
5150 | l :: _ ->
5151 match conf.columns with
5152 | Csingle _ ->
5153 if conf.presentation && l.pagey != 0
5154 then
5155 gotoghyll (clamp (pgscale ~-(state.winh)))
5156 else
5157 let pageno = max 0 (l.pageno-1) in
5158 gotoghyll (getpagey pageno)
5159 | Cmulti ((c, _, coverB) as cl, _) ->
5160 if conf.presentation &&
5161 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5162 then
5163 gotoghyll (clamp (pgscale ~-(state.winh)))
5164 else
5165 let decr =
5166 if l.pageno = state.pagecount - coverB
5167 then 1
5168 else c
5170 let pageno = max 0 (l.pageno-decr) in
5171 gotoghyll (getpagey pageno)
5172 | Csplit (n, _) ->
5173 let y =
5174 if l.pagecol = 0
5175 then
5176 if l.pageno = 0
5177 then l.pagey
5178 else
5179 let pageno = max 0 (l.pageno-1) in
5180 let pagey, pageh = getpageyh pageno in
5181 pagey + (n-1)*pageh
5182 else
5183 let pagey, pageh = getpageyh l.pageno in
5184 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5186 gotoghyll y
5189 let viewkeyboard key mask =
5190 let enttext te =
5191 let mode = state.mode in
5192 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5193 state.text <- "";
5194 enttext ();
5195 G.postRedisplay "view:enttext"
5197 let ctrl = Wsi.withctrl mask in
5198 let key =
5199 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5201 match key with
5202 | 81 -> (* Q *)
5203 exit 0
5205 | 0xff63 -> (* insert *)
5206 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5207 then (
5208 state.mode <- LinkNav (Ltgendir 0);
5209 gotoy state.y;
5211 else showtext '!' "Keyboard link navigation does not work under rotation"
5213 | 0xff1b | 113 -> (* escape / q *)
5214 begin match state.mstate with
5215 | Mzoomrect _ ->
5216 state.mstate <- Mnone;
5217 Wsi.setcursor Wsi.CURSOR_INHERIT;
5218 G.postRedisplay "kill zoom rect";
5219 | _ ->
5220 begin match state.mode with
5221 | LinkNav _ ->
5222 state.mode <- View;
5223 G.postRedisplay "esc leave linknav"
5224 | _ ->
5225 match state.ranchors with
5226 | [] -> raise Quit
5227 | (path, password, anchor, origin) :: rest ->
5228 state.ranchors <- rest;
5229 state.anchor <- anchor;
5230 state.origin <- origin;
5231 opendoc path password
5232 end;
5233 end;
5235 | 0xff08 -> (* backspace *)
5236 gotoghyll (getnav ~-1)
5238 | 111 -> (* o *)
5239 enteroutlinemode ()
5241 | 117 -> (* u *)
5242 state.rects <- [];
5243 state.text <- "";
5244 G.postRedisplay "dehighlight";
5246 | 47 | 63 -> (* / ? *)
5247 let ondone isforw s =
5248 cbput state.hists.pat s;
5249 state.searchpattern <- s;
5250 search s isforw
5252 let s = String.create 1 in
5253 s.[0] <- Char.chr key;
5254 enttext (s, "", Some (onhist state.hists.pat),
5255 textentry, ondone (key = 47), true)
5257 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5258 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5259 setzoom (conf.zoom +. incr)
5261 | 43 | 0xffab -> (* + *)
5262 let ondone s =
5263 let n =
5264 try int_of_string s with exc ->
5265 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5266 max_int
5268 if n != max_int
5269 then (
5270 conf.pagebias <- n;
5271 state.text <- "page bias is now " ^ string_of_int n;
5274 enttext ("page bias: ", "", None, intentry, ondone, true)
5276 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5277 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5278 setzoom (max 0.01 (conf.zoom -. decr))
5280 | 45 | 0xffad -> (* - *)
5281 let ondone msg = state.text <- msg in
5282 enttext (
5283 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5284 optentry state.mode, ondone, true
5287 | 48 when ctrl -> (* ctrl-0 *)
5288 if conf.zoom = 1.0
5289 then (
5290 state.x <- 0;
5291 gotoy state.y
5293 else setzoom 1.0
5295 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5296 let cols =
5297 match conf.columns with
5298 | Csingle _ | Cmulti _ -> 1
5299 | Csplit (n, _) -> n
5301 let h = state.winh -
5302 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5304 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5305 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5306 then setzoom zoom
5308 | 51 when ctrl -> (* ctrl-3 *)
5309 let fm =
5310 match conf.fitmodel with
5311 | FitWidth -> FitProportional
5312 | FitProportional -> FitPage
5313 | FitPage -> FitWidth
5315 state.text <- "fit model: " ^ FMTE.to_string fm;
5316 reqlayout conf.angle fm
5318 | 0xffc6 -> (* f9 *)
5319 togglebirdseye ()
5321 | 57 when ctrl -> (* ctrl-9 *)
5322 togglebirdseye ()
5324 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5325 when not ctrl -> (* 0..9 *)
5326 let ondone s =
5327 let n =
5328 try int_of_string s with exc ->
5329 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5332 if n >= 0
5333 then (
5334 addnav ();
5335 cbput state.hists.pag (string_of_int n);
5336 gotopage1 (n + conf.pagebias - 1) 0;
5339 let pageentry text key =
5340 match Char.unsafe_chr key with
5341 | 'g' -> TEdone text
5342 | _ -> intentry text key
5344 let text = "x" in text.[0] <- Char.chr key;
5345 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5347 | 98 -> (* b *)
5348 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5349 reshape state.winw state.winh;
5351 | 66 -> (* B *)
5352 state.bzoom <- not state.bzoom;
5353 state.rects <- [];
5354 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5356 | 108 -> (* l *)
5357 conf.hlinks <- not conf.hlinks;
5358 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5359 G.postRedisplay "toggle highlightlinks";
5361 | 70 -> (* F *)
5362 state.glinks <- true;
5363 let mode = state.mode in
5364 state.mode <- Textentry (
5365 (":", "", None, linknentry, linkndone gotounder, false),
5366 (fun _ ->
5367 state.glinks <- false;
5368 state.mode <- mode)
5370 state.text <- "";
5371 G.postRedisplay "view:linkent(F)"
5373 | 121 -> (* y *)
5374 state.glinks <- true;
5375 let mode = state.mode in
5376 state.mode <- Textentry (
5378 ":", "", None, linknentry, linkndone (fun under ->
5379 selstring (undertext under);
5380 ), false
5382 fun _ ->
5383 state.glinks <- false;
5384 state.mode <- mode
5386 state.text <- "";
5387 G.postRedisplay "view:linkent"
5389 | 97 -> (* a *)
5390 begin match state.autoscroll with
5391 | Some step ->
5392 conf.autoscrollstep <- step;
5393 state.autoscroll <- None
5394 | None ->
5395 if conf.autoscrollstep = 0
5396 then state.autoscroll <- Some 1
5397 else state.autoscroll <- Some conf.autoscrollstep
5400 | 112 when ctrl -> (* ctrl-p *)
5401 launchpath ()
5403 | 80 -> (* P *)
5404 setpresentationmode (not conf.presentation);
5405 showtext ' ' ("presentation mode " ^
5406 if conf.presentation then "on" else "off");
5408 | 102 -> (* f *)
5409 if List.mem Wsi.Fullscreen state.winstate
5410 then Wsi.reshape conf.cwinw conf.cwinh
5411 else Wsi.fullscreen ()
5413 | 112 | 78 -> (* p|N *)
5414 search state.searchpattern false
5416 | 110 | 0xffc0 -> (* n|F3 *)
5417 search state.searchpattern true
5419 | 116 -> (* t *)
5420 begin match state.layout with
5421 | [] -> ()
5422 | l :: _ ->
5423 gotoghyll (getpagey l.pageno)
5426 | 32 -> (* space *)
5427 nextpage ()
5429 | 0xff9f | 0xffff -> (* delete *)
5430 prevpage ()
5432 | 61 -> (* = *)
5433 showtext ' ' (describe_location ());
5435 | 119 -> (* w *)
5436 begin match state.layout with
5437 | [] -> ()
5438 | l :: _ ->
5439 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5440 G.postRedisplay "w"
5443 | 39 -> (* ' *)
5444 enterbookmarkmode ()
5446 | 104 | 0xffbe -> (* h|F1 *)
5447 enterhelpmode ()
5449 | 105 -> (* i *)
5450 enterinfomode ()
5452 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5453 entermsgsmode ()
5455 | 109 -> (* m *)
5456 let ondone s =
5457 match state.layout with
5458 | l :: _ ->
5459 if String.length s > 0
5460 then
5461 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5462 | _ -> ()
5464 enttext ("bookmark: ", "", None, textentry, ondone, true)
5466 | 126 -> (* ~ *)
5467 quickbookmark ();
5468 showtext ' ' "Quick bookmark added";
5470 | 122 -> (* z *)
5471 begin match state.layout with
5472 | l :: _ ->
5473 let rect = getpdimrect l.pagedimno in
5474 let w, h =
5475 if conf.crophack
5476 then
5477 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5478 truncate (1.2 *. (rect.(3) -. rect.(0))))
5479 else
5480 (truncate (rect.(1) -. rect.(0)),
5481 truncate (rect.(3) -. rect.(0)))
5483 let w = truncate ((float w)*.conf.zoom)
5484 and h = truncate ((float h)*.conf.zoom) in
5485 if w != 0 && h != 0
5486 then (
5487 state.anchor <- getanchor ();
5488 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5490 G.postRedisplay "z";
5492 | [] -> ()
5495 | 120 -> state.roam ()
5496 | 60 | 62 -> (* < > *)
5497 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5499 | 91 | 93 -> (* [ ] *)
5500 conf.colorscale <-
5501 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5503 G.postRedisplay "brightness";
5505 | 99 when state.mode = View -> (* [alt-]c *)
5506 if Wsi.withalt mask
5507 then (
5508 if conf.zoom > 1.0
5509 then
5510 let m = (wadjsb state.winw - state.w) / 2 in
5511 state.x <- m;
5512 gotoy_and_clear_text state.y
5514 else
5515 let (c, a, b), z =
5516 match state.prevcolumns with
5517 | None -> (1, 0, 0), 1.0
5518 | Some (columns, z) ->
5519 let cab =
5520 match columns with
5521 | Csplit (c, _) -> -c, 0, 0
5522 | Cmulti ((c, a, b), _) -> c, a, b
5523 | Csingle _ -> 1, 0, 0
5525 cab, z
5527 setcolumns View c a b;
5528 setzoom z
5530 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5531 -> (* ctrl-shift- (kp) [up|down] *)
5532 let zoom, x = state.prevzoom in
5533 setzoom zoom;
5534 state.x <- x;
5536 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5537 begin match state.autoscroll with
5538 | None ->
5539 begin match state.mode with
5540 | Birdseye beye -> upbirdseye 1 beye
5541 | _ ->
5542 if ctrl
5543 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5544 else (
5545 if not (Wsi.withshift mask) && conf.presentation
5546 then prevpage ()
5547 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5550 | Some n ->
5551 setautoscrollspeed n false
5554 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5555 begin match state.autoscroll with
5556 | None ->
5557 begin match state.mode with
5558 | Birdseye beye -> downbirdseye 1 beye
5559 | _ ->
5560 if ctrl
5561 then gotoy_and_clear_text (clamp (state.winh/2))
5562 else (
5563 if not (Wsi.withshift mask) && conf.presentation
5564 then nextpage ()
5565 else gotoy_and_clear_text (clamp conf.scrollstep)
5568 | Some n ->
5569 setautoscrollspeed n true
5572 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5573 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5574 if canpan ()
5575 then
5576 let dx =
5577 if ctrl
5578 then state.winw / 2
5579 else conf.hscrollstep
5581 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5582 state.x <- panbound (state.x + dx);
5583 gotoy_and_clear_text state.y
5584 else (
5585 state.text <- "";
5586 G.postRedisplay "left/right"
5589 | 0xff55 | 0xff9a -> (* (kp) prior *)
5590 let y =
5591 if ctrl
5592 then
5593 match state.layout with
5594 | [] -> state.y
5595 | l :: _ -> state.y - l.pagey
5596 else
5597 clamp (pgscale (-state.winh))
5599 gotoghyll y
5601 | 0xff56 | 0xff9b -> (* (kp) next *)
5602 let y =
5603 if ctrl
5604 then
5605 match List.rev state.layout with
5606 | [] -> state.y
5607 | l :: _ -> getpagey l.pageno
5608 else
5609 clamp (pgscale state.winh)
5611 gotoghyll y
5613 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5614 gotoghyll 0
5615 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5616 gotoghyll (clamp state.maxy)
5618 | 0xff53 | 0xff98
5619 when Wsi.withalt mask -> (* alt-(kp) right *)
5620 gotoghyll (getnav 1)
5621 | 0xff51 | 0xff96
5622 when Wsi.withalt mask -> (* alt-(kp) left *)
5623 gotoghyll (getnav ~-1)
5625 | 114 -> (* r *)
5626 reload ()
5628 | 118 when conf.debug -> (* v *)
5629 state.rects <- [];
5630 List.iter (fun l ->
5631 match getopaque l.pageno with
5632 | None -> ()
5633 | Some opaque ->
5634 let x0, y0, x1, y1 = pagebbox opaque in
5635 let a,b = float x0, float y0 in
5636 let c,d = float x1, float y0 in
5637 let e,f = float x1, float y1 in
5638 let h,j = float x0, float y1 in
5639 let rect = (a,b,c,d,e,f,h,j) in
5640 debugrect rect;
5641 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5642 ) state.layout;
5643 G.postRedisplay "v";
5645 | _ ->
5646 vlog "huh? %s" (Wsi.keyname key)
5649 let linknavkeyboard key mask linknav =
5650 let getpage pageno =
5651 let rec loop = function
5652 | [] -> None
5653 | l :: _ when l.pageno = pageno -> Some l
5654 | _ :: rest -> loop rest
5655 in loop state.layout
5657 let doexact (pageno, n) =
5658 match getopaque pageno, getpage pageno with
5659 | Some opaque, Some l ->
5660 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5661 then
5662 let under = getlink opaque n in
5663 G.postRedisplay "link gotounder";
5664 gotounder under;
5665 state.mode <- View;
5666 else
5667 let opt, dir =
5668 match key with
5669 | 0xff50 -> (* home *)
5670 Some (findlink opaque LDfirst), -1
5672 | 0xff57 -> (* end *)
5673 Some (findlink opaque LDlast), 1
5675 | 0xff51 -> (* left *)
5676 Some (findlink opaque (LDleft n)), -1
5678 | 0xff53 -> (* right *)
5679 Some (findlink opaque (LDright n)), 1
5681 | 0xff52 -> (* up *)
5682 Some (findlink opaque (LDup n)), -1
5684 | 0xff54 -> (* down *)
5685 Some (findlink opaque (LDdown n)), 1
5687 | _ -> None, 0
5689 let pwl l dir =
5690 begin match findpwl l.pageno dir with
5691 | Pwlnotfound -> ()
5692 | Pwl pageno ->
5693 let notfound dir =
5694 state.mode <- LinkNav (Ltgendir dir);
5695 let y, h = getpageyh pageno in
5696 let y =
5697 if dir < 0
5698 then y + h - state.winh
5699 else y
5701 gotoy y
5703 begin match getopaque pageno, getpage pageno with
5704 | Some opaque, Some _ ->
5705 let link =
5706 let ld = if dir > 0 then LDfirst else LDlast in
5707 findlink opaque ld
5709 begin match link with
5710 | Lfound m ->
5711 showlinktype (getlink opaque m);
5712 state.mode <- LinkNav (Ltexact (pageno, m));
5713 G.postRedisplay "linknav jpage";
5714 | _ -> notfound dir
5715 end;
5716 | _ -> notfound dir
5717 end;
5718 end;
5720 begin match opt with
5721 | Some Lnotfound -> pwl l dir;
5722 | Some (Lfound m) ->
5723 if m = n
5724 then pwl l dir
5725 else (
5726 let _, y0, _, y1 = getlinkrect opaque m in
5727 if y0 < l.pagey
5728 then gotopage1 l.pageno y0
5729 else (
5730 let d = fstate.fontsize + 1 in
5731 if y1 - l.pagey > l.pagevh - d
5732 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5733 else G.postRedisplay "linknav";
5735 showlinktype (getlink opaque m);
5736 state.mode <- LinkNav (Ltexact (l.pageno, m));
5739 | None -> viewkeyboard key mask
5740 end;
5741 | _ -> viewkeyboard key mask
5743 if key = 0xff63
5744 then (
5745 state.mode <- View;
5746 G.postRedisplay "leave linknav"
5748 else
5749 match linknav with
5750 | Ltgendir _ -> viewkeyboard key mask
5751 | Ltexact exact -> doexact exact
5754 let keyboard key mask =
5755 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5756 then wcmd "interrupt"
5757 else state.uioh <- state.uioh#key key mask
5760 let birdseyekeyboard key mask
5761 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5762 let incr =
5763 match conf.columns with
5764 | Csingle _ -> 1
5765 | Cmulti ((c, _, _), _) -> c
5766 | Csplit _ -> failwith "bird's eye split mode"
5768 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5769 match key with
5770 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5771 let y, h = getpageyh pageno in
5772 let top = (state.winh - h) / 2 in
5773 gotoy (max 0 (y - top))
5774 | 0xff0d (* enter *)
5775 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5776 | 0xff1b -> leavebirdseye beye true (* escape *)
5777 | 0xff52 -> upbirdseye incr beye (* up *)
5778 | 0xff54 -> downbirdseye incr beye (* down *)
5779 | 0xff51 -> upbirdseye 1 beye (* left *)
5780 | 0xff53 -> downbirdseye 1 beye (* right *)
5782 | 0xff55 -> (* prior *)
5783 begin match state.layout with
5784 | l :: _ ->
5785 if l.pagey != 0
5786 then (
5787 state.mode <- Birdseye (
5788 oconf, leftx, l.pageno, hooverpageno, anchor
5790 gotopage1 l.pageno 0;
5792 else (
5793 let layout = layout (state.y-state.winh) (pgh state.layout) in
5794 match layout with
5795 | [] -> gotoy (clamp (-state.winh))
5796 | l :: _ ->
5797 state.mode <- Birdseye (
5798 oconf, leftx, l.pageno, hooverpageno, anchor
5800 gotopage1 l.pageno 0
5803 | [] -> gotoy (clamp (-state.winh))
5804 end;
5806 | 0xff56 -> (* next *)
5807 begin match List.rev state.layout with
5808 | l :: _ ->
5809 let layout = layout (state.y + (pgh state.layout)) state.winh in
5810 begin match layout with
5811 | [] ->
5812 let incr = l.pageh - l.pagevh in
5813 if incr = 0
5814 then (
5815 state.mode <-
5816 Birdseye (
5817 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5819 G.postRedisplay "birdseye pagedown";
5821 else gotoy (clamp (incr + conf.interpagespace*2));
5823 | l :: _ ->
5824 state.mode <-
5825 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5826 gotopage1 l.pageno 0;
5829 | [] -> gotoy (clamp state.winh)
5830 end;
5832 | 0xff50 -> (* home *)
5833 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5834 gotopage1 0 0
5836 | 0xff57 -> (* end *)
5837 let pageno = state.pagecount - 1 in
5838 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5839 if not (pagevisible state.layout pageno)
5840 then
5841 let h =
5842 match List.rev state.pdims with
5843 | [] -> state.winh
5844 | (_, _, h, _) :: _ -> h
5846 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5847 else G.postRedisplay "birdseye end";
5848 | _ -> viewkeyboard key mask
5851 let drawpage l =
5852 let color =
5853 match state.mode with
5854 | Textentry _ -> scalecolor 0.4
5855 | LinkNav _
5856 | View -> scalecolor 1.0
5857 | Birdseye (_, _, pageno, hooverpageno, _) ->
5858 if l.pageno = hooverpageno
5859 then scalecolor 0.9
5860 else (
5861 if l.pageno = pageno
5862 then scalecolor 1.0
5863 else scalecolor 0.8
5866 drawtiles l color;
5869 let postdrawpage l linkindexbase =
5870 match getopaque l.pageno with
5871 | Some opaque ->
5872 if tileready l l.pagex l.pagey
5873 then
5874 let x = l.pagedispx - l.pagex
5875 and y = l.pagedispy - l.pagey in
5876 let hlmask =
5877 match conf.columns with
5878 | Csingle _ | Cmulti _ ->
5879 (if conf.hlinks then 1 else 0)
5880 + (if state.glinks
5881 && not (isbirdseye state.mode) then 2 else 0)
5882 | _ -> 0
5884 let s =
5885 match state.mode with
5886 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5887 | _ -> ""
5889 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5890 else 0
5891 | _ -> 0
5894 let scrollindicator () =
5895 let sbw, ph, sh = state.uioh#scrollph in
5896 let sbh, pw, sw = state.uioh#scrollpw in
5898 GlDraw.color (0.64, 0.64, 0.64);
5899 GlDraw.rect
5900 (float (state.winw - sbw), 0.)
5901 (float state.winw, float state.winh)
5903 GlDraw.rect
5904 (0., float (state.winh - sbh))
5905 (float (wadjsb state.winw - 1), float state.winh)
5907 GlDraw.color (0.0, 0.0, 0.0);
5909 GlDraw.rect
5910 (float (state.winw - sbw), ph)
5911 (float state.winw, ph +. sh)
5913 GlDraw.rect
5914 (pw, float (state.winh - sbh))
5915 (pw +. sw, float state.winh)
5919 let showsel () =
5920 match state.mstate with
5921 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5924 | Msel ((x0, y0), (x1, y1)) ->
5925 let rec loop = function
5926 | l :: ls ->
5927 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5928 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5929 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5930 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5931 then
5932 match getopaque l.pageno with
5933 | Some opaque ->
5934 let x0, y0 = pagetranslatepoint l x0 y0 in
5935 let x1, y1 = pagetranslatepoint l x1 y1 in
5936 seltext opaque (x0, y0, x1, y1);
5937 | _ -> ()
5938 else loop ls
5939 | [] -> ()
5941 loop state.layout
5944 let showrects rects =
5945 Gl.enable `blend;
5946 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5947 GlDraw.polygon_mode `both `fill;
5948 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5949 List.iter
5950 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5951 List.iter (fun l ->
5952 if l.pageno = pageno
5953 then (
5954 let dx = float (l.pagedispx - l.pagex) in
5955 let dy = float (l.pagedispy - l.pagey) in
5956 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5957 GlDraw.begins `quads;
5959 GlDraw.vertex2 (x0+.dx, y0+.dy);
5960 GlDraw.vertex2 (x1+.dx, y1+.dy);
5961 GlDraw.vertex2 (x2+.dx, y2+.dy);
5962 GlDraw.vertex2 (x3+.dx, y3+.dy);
5964 GlDraw.ends ();
5966 ) state.layout
5967 ) rects
5969 Gl.disable `blend;
5972 let display () =
5973 GlClear.color (scalecolor2 conf.bgcolor);
5974 GlClear.clear [`color];
5975 List.iter drawpage state.layout;
5976 let rects =
5977 match state.mode with
5978 | LinkNav (Ltexact (pageno, linkno)) ->
5979 begin match getopaque pageno with
5980 | Some opaque ->
5981 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5982 (pageno, 5, (
5983 float x0, float y0,
5984 float x1, float y0,
5985 float x1, float y1,
5986 float x0, float y1)
5987 ) :: state.rects
5988 | None -> state.rects
5990 | _ -> state.rects
5992 showrects rects;
5993 let rec postloop linkindexbase = function
5994 | l :: rest ->
5995 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5996 postloop linkindexbase rest
5997 | [] -> ()
5999 showsel ();
6000 postloop 0 state.layout;
6001 state.uioh#display;
6002 begin match state.mstate with
6003 | Mzoomrect ((x0, y0), (x1, y1)) ->
6004 Gl.enable `blend;
6005 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6006 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6007 GlDraw.rect (float x0, float y0)
6008 (float x1, float y1);
6009 Gl.disable `blend;
6010 | _ -> ()
6011 end;
6012 enttext ();
6013 scrollindicator ();
6014 Wsi.swapb ();
6017 let zoomrect x y x1 y1 =
6018 let x0 = min x x1
6019 and x1 = max x x1
6020 and y0 = min y y1 in
6021 gotoy (state.y + y0);
6022 state.anchor <- getanchor ();
6023 let zoom = (float state.w) /. float (x1 - x0) in
6024 let margin =
6025 match conf.fitmodel, conf.columns with
6026 | FitPage, Csplit _ ->
6027 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6029 | _, _ ->
6030 let adjw = wadjsb state.winw in
6031 if state.w < adjw
6032 then (adjw - state.w) / 2
6033 else 0
6035 state.x <- (state.x + margin) - x0;
6036 setzoom zoom;
6037 Wsi.setcursor Wsi.CURSOR_INHERIT;
6038 state.mstate <- Mnone;
6041 let zoomblock x y =
6042 let g opaque l px py =
6043 match rectofblock opaque px py with
6044 | Some a ->
6045 let x0 = a.(0) -. 20. in
6046 let x1 = a.(1) +. 20. in
6047 let y0 = a.(2) -. 20. in
6048 let zoom = (float state.w) /. (x1 -. x0) in
6049 let pagey = getpagey l.pageno in
6050 gotoy_and_clear_text (pagey + truncate y0);
6051 state.anchor <- getanchor ();
6052 let margin = (state.w - l.pagew)/2 in
6053 state.x <- -truncate x0 - margin;
6054 setzoom zoom;
6055 None
6056 | None -> None
6058 match conf.columns with
6059 | Csplit _ ->
6060 showtext '!' "block zooming does not work properly in split columns mode"
6061 | _ -> onppundermouse g x y ()
6064 let scrollx x =
6065 let winw = wadjsb state.winw - 1 in
6066 let s = float x /. float winw in
6067 let destx = truncate (float (state.w + winw) *. s) in
6068 state.x <- winw - destx;
6069 gotoy_and_clear_text state.y;
6070 state.mstate <- Mscrollx;
6073 let scrolly y =
6074 let s = float y /. float state.winh in
6075 let desty = truncate (float (state.maxy - state.winh) *. s) in
6076 gotoy_and_clear_text desty;
6077 state.mstate <- Mscrolly;
6080 let viewmouse button down x y mask =
6081 match button with
6082 | n when (n == 4 || n == 5) && not down ->
6083 if Wsi.withctrl mask
6084 then (
6085 match state.mstate with
6086 | Mzoom (oldn, i) ->
6087 if oldn = n
6088 then (
6089 if i = 2
6090 then
6091 let incr =
6092 match n with
6093 | 5 ->
6094 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6095 | _ ->
6096 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6098 let zoom = conf.zoom -. incr in
6099 setzoom zoom;
6100 state.mstate <- Mzoom (n, 0);
6101 else
6102 state.mstate <- Mzoom (n, i+1);
6104 else state.mstate <- Mzoom (n, 0)
6106 | _ -> state.mstate <- Mzoom (n, 0)
6108 else (
6109 match state.autoscroll with
6110 | Some step -> setautoscrollspeed step (n=4)
6111 | None ->
6112 if conf.wheelbypage || conf.presentation
6113 then (
6114 if n = 4
6115 then prevpage ()
6116 else nextpage ()
6118 else
6119 let incr =
6120 if n = 4
6121 then -conf.scrollstep
6122 else conf.scrollstep
6124 let incr = incr * 2 in
6125 let y = clamp incr in
6126 gotoy_and_clear_text y
6129 | n when (n = 6 || n = 7) && not down && canpan () ->
6130 state.x <-
6131 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6132 gotoy_and_clear_text state.y
6134 | 1 when Wsi.withshift mask ->
6135 state.mstate <- Mnone;
6136 if not down
6137 then (
6138 match unproject x y with
6139 | Some (pageno, ux, uy) ->
6140 let cmd = Printf.sprintf
6141 "%s %s %d %d %d"
6142 conf.stcmd state.path pageno ux uy
6144 popen cmd []
6145 | None -> ()
6148 | 1 when Wsi.withctrl mask ->
6149 if down
6150 then (
6151 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6152 state.mstate <- Mpan (x, y)
6154 else
6155 state.mstate <- Mnone
6157 | 3 ->
6158 if down
6159 then (
6160 Wsi.setcursor Wsi.CURSOR_CYCLE;
6161 let p = (x, y) in
6162 state.mstate <- Mzoomrect (p, p)
6164 else (
6165 match state.mstate with
6166 | Mzoomrect ((x0, y0), _) ->
6167 if abs (x-x0) > 10 && abs (y - y0) > 10
6168 then zoomrect x0 y0 x y
6169 else (
6170 state.mstate <- Mnone;
6171 Wsi.setcursor Wsi.CURSOR_INHERIT;
6172 G.postRedisplay "kill accidental zoom rect";
6174 | _ ->
6175 Wsi.setcursor Wsi.CURSOR_INHERIT;
6176 state.mstate <- Mnone
6179 | 1 when x > state.winw - vscrollw () ->
6180 if down
6181 then
6182 let _, position, sh = state.uioh#scrollph in
6183 if y > truncate position && y < truncate (position +. sh)
6184 then state.mstate <- Mscrolly
6185 else scrolly y
6186 else
6187 state.mstate <- Mnone
6189 | 1 when y > state.winh - hscrollh () ->
6190 if down
6191 then
6192 let _, position, sw = state.uioh#scrollpw in
6193 if x > truncate position && x < truncate (position +. sw)
6194 then state.mstate <- Mscrollx
6195 else scrollx x
6196 else
6197 state.mstate <- Mnone
6199 | 1 when state.bzoom -> if not down then zoomblock x y
6201 | 1 ->
6202 let dest = if down then getunder x y else Unone in
6203 begin match dest with
6204 | Ulinkgoto _
6205 | Ulinkuri _
6206 | Uremote _
6207 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6208 gotounder dest
6210 | Unone when down ->
6211 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6212 state.mstate <- Mpan (x, y);
6214 | Unone | Utext _ ->
6215 if down
6216 then (
6217 if conf.angle mod 360 = 0
6218 then (
6219 state.mstate <- Msel ((x, y), (x, y));
6220 G.postRedisplay "mouse select";
6223 else (
6224 match state.mstate with
6225 | Mnone -> ()
6227 | Mzoom _ | Mscrollx | Mscrolly ->
6228 state.mstate <- Mnone
6230 | Mzoomrect ((x0, y0), _) ->
6231 zoomrect x0 y0 x y
6233 | Mpan _ ->
6234 Wsi.setcursor Wsi.CURSOR_INHERIT;
6235 state.mstate <- Mnone
6237 | Msel ((x0, y0), (x1, y1)) ->
6238 let rec loop = function
6239 | [] -> ()
6240 | l :: rest ->
6241 let inside =
6242 let a0 = l.pagedispy in
6243 let a1 = a0 + l.pagevh in
6244 let b0 = l.pagedispx in
6245 let b1 = b0 + l.pagevw in
6246 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6247 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6249 if inside
6250 then
6251 match getopaque l.pageno with
6252 | Some opaque ->
6253 begin
6254 match Ne.pipe () with
6255 | Ne.Exn exn ->
6256 showtext '!'
6257 (Printf.sprintf
6258 "can not create sel pipe: %s"
6259 (exntos exn));
6260 | Ne.Res (r, w) ->
6261 let doclose what fd =
6262 Ne.clo fd (fun msg ->
6263 dolog "%s close failed: %s" what msg)
6266 popen conf.selcmd [r, 0; w, -1];
6267 copysel w opaque true;
6268 doclose "pipe/r" r;
6269 G.postRedisplay "copysel";
6270 with exn ->
6271 dolog "can not execute %S: %s"
6272 conf.selcmd (exntos exn);
6273 doclose "pipe/r" r;
6274 doclose "pipe/w" w;
6276 | None -> ()
6277 else loop rest
6279 loop state.layout;
6280 Wsi.setcursor Wsi.CURSOR_INHERIT;
6281 state.mstate <- Mnone;
6285 | _ -> ()
6288 let birdseyemouse button down x y mask
6289 (conf, leftx, _, hooverpageno, anchor) =
6290 match button with
6291 | 1 when down ->
6292 let rec loop = function
6293 | [] -> ()
6294 | l :: rest ->
6295 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6296 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6297 then (
6298 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6300 else loop rest
6302 loop state.layout
6303 | 3 -> ()
6304 | _ -> viewmouse button down x y mask
6307 let mouse button down x y mask =
6308 state.uioh <- state.uioh#button button down x y mask;
6311 let motion ~x ~y =
6312 state.uioh <- state.uioh#motion x y
6315 let pmotion ~x ~y =
6316 state.uioh <- state.uioh#pmotion x y;
6319 let uioh = object
6320 method display = ()
6322 method key key mask =
6323 begin match state.mode with
6324 | Textentry textentry -> textentrykeyboard key mask textentry
6325 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6326 | View -> viewkeyboard key mask
6327 | LinkNav linknav -> linknavkeyboard key mask linknav
6328 end;
6329 state.uioh
6331 method button button bstate x y mask =
6332 begin match state.mode with
6333 | LinkNav _
6334 | View -> viewmouse button bstate x y mask
6335 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6336 | Textentry _ -> ()
6337 end;
6338 state.uioh
6340 method motion x y =
6341 begin match state.mode with
6342 | Textentry _ -> ()
6343 | View | Birdseye _ | LinkNav _ ->
6344 match state.mstate with
6345 | Mzoom _ | Mnone -> ()
6347 | Mpan (x0, y0) ->
6348 let dx = x - x0
6349 and dy = y0 - y in
6350 state.mstate <- Mpan (x, y);
6351 if canpan ()
6352 then state.x <- panbound (state.x + dx);
6353 let y = clamp dy in
6354 gotoy_and_clear_text y
6356 | Msel (a, _) ->
6357 state.mstate <- Msel (a, (x, y));
6358 G.postRedisplay "motion select";
6360 | Mscrolly ->
6361 let y = min state.winh (max 0 y) in
6362 scrolly y
6364 | Mscrollx ->
6365 let x = min state.winw (max 0 x) in
6366 scrollx x
6368 | Mzoomrect (p0, _) ->
6369 state.mstate <- Mzoomrect (p0, (x, y));
6370 G.postRedisplay "motion zoomrect";
6371 end;
6372 state.uioh
6374 method pmotion x y =
6375 begin match state.mode with
6376 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6377 let rec loop = function
6378 | [] ->
6379 if hooverpageno != -1
6380 then (
6381 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6382 G.postRedisplay "pmotion birdseye no hoover";
6384 | l :: rest ->
6385 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6386 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6387 then (
6388 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6389 G.postRedisplay "pmotion birdseye hoover";
6391 else loop rest
6393 loop state.layout
6395 | Textentry _ -> ()
6397 | LinkNav _
6398 | View ->
6399 match state.mstate with
6400 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6402 | Mnone ->
6403 updateunder x y;
6404 match conf.pax with
6405 | None -> ()
6406 | Some r ->
6407 let past, _, _ = !r in
6408 let now = now () in
6409 let delta = now -. past in
6410 if delta > 0.01
6411 then paxunder x y
6412 else r := (now, x, y)
6413 end;
6414 state.uioh
6416 method infochanged _ = ()
6418 method scrollph =
6419 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6420 let p, h =
6421 if maxy = 0
6422 then 0.0, float state.winh
6423 else scrollph state.y maxy
6425 vscrollw (), p, h
6427 method scrollpw =
6428 let winw = wadjsb state.winw in
6429 let fwinw = float winw in
6430 let sw =
6431 let sw = fwinw /. float state.w in
6432 let sw = fwinw *. sw in
6433 max sw (float conf.scrollh)
6435 let position =
6436 let maxx = state.w + winw in
6437 let x = winw - state.x in
6438 let percent = float x /. float maxx in
6439 (fwinw -. sw) *. percent
6441 hscrollh (), position, sw
6443 method modehash =
6444 let modename =
6445 match state.mode with
6446 | LinkNav _ -> "links"
6447 | Textentry _ -> "textentry"
6448 | Birdseye _ -> "birdseye"
6449 | View -> "view"
6451 findkeyhash conf modename
6453 method eformsgs = true
6454 end;;
6456 module Config =
6457 struct
6458 open Parser
6460 let fontpath = ref "";;
6462 module KeyMap =
6463 Map.Make (struct type t = (int * int) let compare = compare end);;
6465 let unent s =
6466 let l = String.length s in
6467 let b = Buffer.create l in
6468 unent b s 0 l;
6469 Buffer.contents b;
6472 let home =
6473 try Sys.getenv "HOME"
6474 with exn ->
6475 prerr_endline
6476 ("Can not determine home directory location: " ^ exntos exn);
6480 let modifier_of_string = function
6481 | "alt" -> Wsi.altmask
6482 | "shift" -> Wsi.shiftmask
6483 | "ctrl" | "control" -> Wsi.ctrlmask
6484 | "meta" -> Wsi.metamask
6485 | _ -> 0
6488 let key_of_string =
6489 let r = Str.regexp "-" in
6490 fun s ->
6491 let elems = Str.full_split r s in
6492 let f n k m =
6493 let g s =
6494 let m1 = modifier_of_string s in
6495 if m1 = 0
6496 then (Wsi.namekey s, m)
6497 else (k, m lor m1)
6498 in function
6499 | Str.Delim s when n land 1 = 0 -> g s
6500 | Str.Text s -> g s
6501 | Str.Delim _ -> (k, m)
6503 let rec loop n k m = function
6504 | [] -> (k, m)
6505 | x :: xs ->
6506 let k, m = f n k m x in
6507 loop (n+1) k m xs
6509 loop 0 0 0 elems
6512 let keys_of_string =
6513 let r = Str.regexp "[ \t]" in
6514 fun s ->
6515 let elems = Str.split r s in
6516 List.map key_of_string elems
6519 let copykeyhashes c =
6520 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6523 let config_of c attrs =
6524 let apply c k v =
6526 match k with
6527 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6528 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6529 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6530 | "preload" -> { c with preload = bool_of_string v }
6531 | "page-bias" -> { c with pagebias = int_of_string v }
6532 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6533 | "horizontal-scroll-step" ->
6534 { c with hscrollstep = max (int_of_string v) 1 }
6535 | "auto-scroll-step" ->
6536 { c with autoscrollstep = max 0 (int_of_string v) }
6537 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6538 | "crop-hack" -> { c with crophack = bool_of_string v }
6539 | "throttle" ->
6540 let mw =
6541 match String.lowercase v with
6542 | "true" -> Some infinity
6543 | "false" -> None
6544 | f -> Some (float_of_string f)
6546 { c with maxwait = mw}
6547 | "highlight-links" -> { c with hlinks = bool_of_string v }
6548 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6549 | "vertical-margin" ->
6550 { c with interpagespace = max 0 (int_of_string v) }
6551 | "zoom" ->
6552 let zoom = float_of_string v /. 100. in
6553 let zoom = max zoom 0.0 in
6554 { c with zoom = zoom }
6555 | "presentation" -> { c with presentation = bool_of_string v }
6556 | "rotation-angle" -> { c with angle = int_of_string v }
6557 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6558 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6559 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6560 | "proportional-display" ->
6561 let fm =
6562 if bool_of_string v
6563 then FitProportional
6564 else FitWidth
6566 { c with fitmodel = fm }
6567 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6568 | "pixmap-cache-size" ->
6569 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6570 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6571 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6572 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6573 | "persistent-location" -> { c with jumpback = bool_of_string v }
6574 | "background-color" -> { c with bgcolor = color_of_string v }
6575 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6576 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6577 | "mupdf-store-size" ->
6578 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6579 | "checkers" -> { c with checkers = bool_of_string v }
6580 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6581 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6582 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6583 | "uri-launcher" -> { c with urilauncher = unent v }
6584 | "path-launcher" -> { c with pathlauncher = unent v }
6585 | "color-space" -> { c with colorspace = CSTE.of_string v }
6586 | "invert-colors" -> { c with invert = bool_of_string v }
6587 | "brightness" -> { c with colorscale = float_of_string v }
6588 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6589 | "ghyllscroll" ->
6590 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6591 | "columns" ->
6592 let (n, _, _) as nab = multicolumns_of_string v in
6593 if n < 0
6594 then { c with columns = Csplit (-n, [||]) }
6595 else { c with columns = Cmulti (nab, [||]) }
6596 | "birds-eye-columns" ->
6597 { c with beyecolumns = Some (max (int_of_string v) 2) }
6598 | "selection-command" -> { c with selcmd = unent v }
6599 | "synctex-command" -> { c with stcmd = unent v }
6600 | "pax-command" -> { c with paxcmd = unent v }
6601 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6602 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6603 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6604 | "use-pbo" -> { c with usepbo = bool_of_string v }
6605 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6606 | "horizontal-scrollbar-visible" ->
6607 let b =
6608 if bool_of_string v
6609 then c.scrollb lor scrollbhv
6610 else c.scrollb land (lnot scrollbhv)
6612 { c with scrollb = b }
6613 | "vertical-scrollbar-visible" ->
6614 let b =
6615 if bool_of_string v
6616 then c.scrollb lor scrollbvv
6617 else c.scrollb land (lnot scrollbvv)
6619 { c with scrollb = b }
6620 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6621 | "point-and-x" ->
6622 { c with pax =
6623 if bool_of_string v
6624 then Some (ref (0.0, 0, 0))
6625 else None }
6626 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6627 | _ -> c
6628 with exn ->
6629 prerr_endline ("Error processing attribute (`" ^
6630 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6633 let rec fold c = function
6634 | [] -> c
6635 | (k, v) :: rest ->
6636 let c = apply c k v in
6637 fold c rest
6639 fold { c with keyhashes = copykeyhashes c } attrs;
6642 let fromstring f pos n v d =
6643 try f v
6644 with exn ->
6645 dolog "Error processing attribute (%S=%S) at %d\n%s"
6646 n v pos (exntos exn)
6651 let bookmark_of attrs =
6652 let rec fold title page rely visy = function
6653 | ("title", v) :: rest -> fold v page rely visy rest
6654 | ("page", v) :: rest -> fold title v rely visy rest
6655 | ("rely", v) :: rest -> fold title page v visy rest
6656 | ("visy", v) :: rest -> fold title page rely v rest
6657 | _ :: rest -> fold title page rely visy rest
6658 | [] -> title, page, rely, visy
6660 fold "invalid" "0" "0" "0" attrs
6663 let doc_of attrs =
6664 let rec fold path page rely pan visy = function
6665 | ("path", v) :: rest -> fold v page rely pan visy rest
6666 | ("page", v) :: rest -> fold path v rely pan visy rest
6667 | ("rely", v) :: rest -> fold path page v pan visy rest
6668 | ("pan", v) :: rest -> fold path page rely v visy rest
6669 | ("visy", v) :: rest -> fold path page rely pan v rest
6670 | _ :: rest -> fold path page rely pan visy rest
6671 | [] -> path, page, rely, pan, visy
6673 fold "" "0" "0" "0" "0" attrs
6676 let map_of attrs =
6677 let rec fold rs ls = function
6678 | ("out", v) :: rest -> fold v ls rest
6679 | ("in", v) :: rest -> fold rs v rest
6680 | _ :: rest -> fold ls rs rest
6681 | [] -> ls, rs
6683 fold "" "" attrs
6686 let setconf dst src =
6687 dst.scrollbw <- src.scrollbw;
6688 dst.scrollh <- src.scrollh;
6689 dst.icase <- src.icase;
6690 dst.preload <- src.preload;
6691 dst.pagebias <- src.pagebias;
6692 dst.verbose <- src.verbose;
6693 dst.scrollstep <- src.scrollstep;
6694 dst.maxhfit <- src.maxhfit;
6695 dst.crophack <- src.crophack;
6696 dst.autoscrollstep <- src.autoscrollstep;
6697 dst.maxwait <- src.maxwait;
6698 dst.hlinks <- src.hlinks;
6699 dst.underinfo <- src.underinfo;
6700 dst.interpagespace <- src.interpagespace;
6701 dst.zoom <- src.zoom;
6702 dst.presentation <- src.presentation;
6703 dst.angle <- src.angle;
6704 dst.cwinw <- src.cwinw;
6705 dst.cwinh <- src.cwinh;
6706 dst.savebmarks <- src.savebmarks;
6707 dst.memlimit <- src.memlimit;
6708 dst.fitmodel <- src.fitmodel;
6709 dst.texcount <- src.texcount;
6710 dst.sliceheight <- src.sliceheight;
6711 dst.thumbw <- src.thumbw;
6712 dst.jumpback <- src.jumpback;
6713 dst.bgcolor <- src.bgcolor;
6714 dst.tilew <- src.tilew;
6715 dst.tileh <- src.tileh;
6716 dst.mustoresize <- src.mustoresize;
6717 dst.checkers <- src.checkers;
6718 dst.aalevel <- src.aalevel;
6719 dst.trimmargins <- src.trimmargins;
6720 dst.trimfuzz <- src.trimfuzz;
6721 dst.urilauncher <- src.urilauncher;
6722 dst.colorspace <- src.colorspace;
6723 dst.invert <- src.invert;
6724 dst.colorscale <- src.colorscale;
6725 dst.redirectstderr <- src.redirectstderr;
6726 dst.ghyllscroll <- src.ghyllscroll;
6727 dst.columns <- src.columns;
6728 dst.beyecolumns <- src.beyecolumns;
6729 dst.selcmd <- src.selcmd;
6730 dst.updatecurs <- src.updatecurs;
6731 dst.pathlauncher <- src.pathlauncher;
6732 dst.keyhashes <- copykeyhashes src;
6733 dst.hfsize <- src.hfsize;
6734 dst.hscrollstep <- src.hscrollstep;
6735 dst.pgscale <- src.pgscale;
6736 dst.usepbo <- src.usepbo;
6737 dst.wheelbypage <- src.wheelbypage;
6738 dst.stcmd <- src.stcmd;
6739 dst.paxcmd <- src.paxcmd;
6740 dst.scrollb <- src.scrollb;
6741 dst.riani <- src.riani;
6742 dst.paxmark <- src.paxmark;
6743 dst.pax <-
6744 if src.pax = None
6745 then None
6746 else Some ((ref (0.0, 0, 0)));
6749 let get s =
6750 let h = Hashtbl.create 10 in
6751 let dc = { defconf with angle = defconf.angle } in
6752 let rec toplevel v t spos _ =
6753 match t with
6754 | Vdata | Vcdata | Vend -> v
6755 | Vopen ("llppconfig", _, closed) ->
6756 if closed
6757 then v
6758 else { v with f = llppconfig }
6759 | Vopen _ ->
6760 error "unexpected subelement at top level" s spos
6761 | Vclose _ -> error "unexpected close at top level" s spos
6763 and llppconfig v t spos _ =
6764 match t with
6765 | Vdata | Vcdata -> v
6766 | Vend -> error "unexpected end of input in llppconfig" s spos
6767 | Vopen ("defaults", attrs, closed) ->
6768 let c = config_of dc attrs in
6769 setconf dc c;
6770 if closed
6771 then v
6772 else { v with f = defaults }
6774 | Vopen ("ui-font", attrs, closed) ->
6775 let rec getsize size = function
6776 | [] -> size
6777 | ("size", v) :: rest ->
6778 let size =
6779 fromstring int_of_string spos "size" v fstate.fontsize in
6780 getsize size rest
6781 | l -> getsize size l
6783 fstate.fontsize <- getsize fstate.fontsize attrs;
6784 if closed
6785 then v
6786 else { v with f = uifont (Buffer.create 10) }
6788 | Vopen ("doc", attrs, closed) ->
6789 let pathent, spage, srely, span, svisy = doc_of attrs in
6790 let path = unent pathent
6791 and pageno = fromstring int_of_string spos "page" spage 0
6792 and rely = fromstring float_of_string spos "rely" srely 0.0
6793 and pan = fromstring int_of_string spos "pan" span 0
6794 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6795 let c = config_of dc attrs in
6796 let anchor = (pageno, rely, visy) in
6797 if closed
6798 then (Hashtbl.add h path (c, [], pan, anchor); v)
6799 else { v with f = doc path pan anchor c [] }
6801 | Vopen _ ->
6802 error "unexpected subelement in llppconfig" s spos
6804 | Vclose "llppconfig" -> { v with f = toplevel }
6805 | Vclose _ -> error "unexpected close in llppconfig" s spos
6807 and defaults v t spos _ =
6808 match t with
6809 | Vdata | Vcdata -> v
6810 | Vend -> error "unexpected end of input in defaults" s spos
6811 | Vopen ("keymap", attrs, closed) ->
6812 let modename =
6813 try List.assoc "mode" attrs
6814 with Not_found -> "global" in
6815 if closed
6816 then v
6817 else
6818 let ret keymap =
6819 let h = findkeyhash dc modename in
6820 KeyMap.iter (Hashtbl.replace h) keymap;
6821 defaults
6823 { v with f = pkeymap ret KeyMap.empty }
6825 | Vopen (_, _, _) ->
6826 error "unexpected subelement in defaults" s spos
6828 | Vclose "defaults" ->
6829 { v with f = llppconfig }
6831 | Vclose _ -> error "unexpected close in defaults" s spos
6833 and uifont b v t spos epos =
6834 match t with
6835 | Vdata | Vcdata ->
6836 Buffer.add_substring b s spos (epos - spos);
6838 | Vopen (_, _, _) ->
6839 error "unexpected subelement in ui-font" s spos
6840 | Vclose "ui-font" ->
6841 if String.length !fontpath = 0
6842 then fontpath := Buffer.contents b;
6843 { v with f = llppconfig }
6844 | Vclose _ -> error "unexpected close in ui-font" s spos
6845 | Vend -> error "unexpected end of input in ui-font" s spos
6847 and doc path pan anchor c bookmarks v t spos _ =
6848 match t with
6849 | Vdata | Vcdata -> v
6850 | Vend -> error "unexpected end of input in doc" s spos
6851 | Vopen ("bookmarks", _, closed) ->
6852 if closed
6853 then v
6854 else { v with f = pbookmarks path pan anchor c bookmarks }
6856 | Vopen ("keymap", attrs, closed) ->
6857 let modename =
6858 try List.assoc "mode" attrs
6859 with Not_found -> "global"
6861 if closed
6862 then v
6863 else
6864 let ret keymap =
6865 let h = findkeyhash c modename in
6866 KeyMap.iter (Hashtbl.replace h) keymap;
6867 doc path pan anchor c bookmarks
6869 { v with f = pkeymap ret KeyMap.empty }
6871 | Vopen (_, _, _) ->
6872 error "unexpected subelement in doc" s spos
6874 | Vclose "doc" ->
6875 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6876 { v with f = llppconfig }
6878 | Vclose _ -> error "unexpected close in doc" s spos
6880 and pkeymap ret keymap v t spos _ =
6881 match t with
6882 | Vdata | Vcdata -> v
6883 | Vend -> error "unexpected end of input in keymap" s spos
6884 | Vopen ("map", attrs, closed) ->
6885 let r, l = map_of attrs in
6886 let kss = fromstring keys_of_string spos "in" r [] in
6887 let lss = fromstring keys_of_string spos "out" l [] in
6888 let keymap =
6889 match kss with
6890 | [] -> keymap
6891 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6892 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6894 if closed
6895 then { v with f = pkeymap ret keymap }
6896 else
6897 let f () = v in
6898 { v with f = skip "map" f }
6900 | Vopen _ ->
6901 error "unexpected subelement in keymap" s spos
6903 | Vclose "keymap" ->
6904 { v with f = ret keymap }
6906 | Vclose _ -> error "unexpected close in keymap" s spos
6908 and pbookmarks path pan anchor c bookmarks v t spos _ =
6909 match t with
6910 | Vdata | Vcdata -> v
6911 | Vend -> error "unexpected end of input in bookmarks" s spos
6912 | Vopen ("item", attrs, closed) ->
6913 let titleent, spage, srely, svisy = bookmark_of attrs in
6914 let page = fromstring int_of_string spos "page" spage 0
6915 and rely = fromstring float_of_string spos "rely" srely 0.0
6916 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6917 let bookmarks =
6918 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6920 if closed
6921 then { v with f = pbookmarks path pan anchor c bookmarks }
6922 else
6923 let f () = v in
6924 { v with f = skip "item" f }
6926 | Vopen _ ->
6927 error "unexpected subelement in bookmarks" s spos
6929 | Vclose "bookmarks" ->
6930 { v with f = doc path pan anchor c bookmarks }
6932 | Vclose _ -> error "unexpected close in bookmarks" s spos
6934 and skip tag f v t spos _ =
6935 match t with
6936 | Vdata | Vcdata -> v
6937 | Vend ->
6938 error ("unexpected end of input in skipped " ^ tag) s spos
6939 | Vopen (tag', _, closed) ->
6940 if closed
6941 then v
6942 else
6943 let f' () = { v with f = skip tag f } in
6944 { v with f = skip tag' f' }
6945 | Vclose ctag ->
6946 if tag = ctag
6947 then f ()
6948 else error ("unexpected close in skipped " ^ tag) s spos
6951 parse { f = toplevel; accu = () } s;
6952 h, dc;
6955 let do_load f ic =
6957 let len = in_channel_length ic in
6958 let s = String.create len in
6959 really_input ic s 0 len;
6960 f s;
6961 with
6962 | Parse_error (msg, s, pos) ->
6963 let subs = subs s pos in
6964 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6965 failwith ("parse error: " ^ s)
6967 | exn ->
6968 failwith ("config load error: " ^ exntos exn)
6971 let defconfpath =
6972 let dir =
6974 let dir = Filename.concat home ".config" in
6975 if Sys.is_directory dir then dir else home
6976 with _ -> home
6978 Filename.concat dir "llpp.conf"
6981 let confpath = ref defconfpath;;
6983 let load1 f =
6984 if Sys.file_exists !confpath
6985 then
6986 match
6987 (try Some (open_in_bin !confpath)
6988 with exn ->
6989 prerr_endline
6990 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6991 exntos exn);
6992 None
6994 with
6995 | Some ic ->
6996 let success =
6998 f (do_load get ic)
6999 with exn ->
7000 prerr_endline
7001 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7002 exntos exn);
7003 false
7005 close_in ic;
7006 success
7008 | None -> false
7009 else
7010 f (Hashtbl.create 0, defconf)
7013 let load () =
7014 let f (h, dc) =
7015 let pc, pb, px, pa =
7017 let key =
7018 if String.length state.origin = 0
7019 then state.path
7020 else state.origin
7022 Hashtbl.find h (Filename.basename key)
7023 with Not_found -> dc, [], 0, emptyanchor
7025 setconf defconf dc;
7026 setconf conf pc;
7027 state.bookmarks <- pb;
7028 state.x <- px;
7029 if conf.jumpback
7030 then state.anchor <- pa;
7031 cbput state.hists.nav pa;
7032 true
7034 load1 f
7037 let add_attrs bb always dc c =
7038 let ob s a b =
7039 if always || a != b
7040 then Printf.bprintf bb "\n %s='%b'" s a
7041 and op s a b =
7042 if always || a <> b
7043 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7044 and oi s a b =
7045 if always || a != b
7046 then Printf.bprintf bb "\n %s='%d'" s a
7047 and oI s a b =
7048 if always || a != b
7049 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7050 and oz s a b =
7051 if always || a <> b
7052 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7053 and oF s a b =
7054 if always || a <> b
7055 then Printf.bprintf bb "\n %s='%f'" s a
7056 and oc s a b =
7057 if always || a <> b
7058 then
7059 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7060 and oC s a b =
7061 if always || a <> b
7062 then
7063 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7064 and oR s a b =
7065 if always || a <> b
7066 then
7067 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7068 and os s a b =
7069 if always || a <> b
7070 then
7071 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7072 and og s a b =
7073 if always || a <> b
7074 then
7075 match a with
7076 | None -> ()
7077 | Some (_N, _A, _B) ->
7078 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7079 and oW s a b =
7080 if always || a <> b
7081 then
7082 let v =
7083 match a with
7084 | None -> "false"
7085 | Some f ->
7086 if f = infinity
7087 then "true"
7088 else string_of_float f
7090 Printf.bprintf bb "\n %s='%s'" s v
7091 and oco s a b =
7092 if always || a <> b
7093 then
7094 match a with
7095 | Cmulti ((n, a, b), _) when n > 1 ->
7096 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7097 | Csplit (n, _) when n > 1 ->
7098 Printf.bprintf bb "\n %s='%d'" s ~-n
7099 | _ -> ()
7100 and obeco s a b =
7101 if always || a <> b
7102 then
7103 match a with
7104 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7105 | _ -> ()
7106 and oFm s a b =
7107 if always || a <> b
7108 then
7109 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7110 and oSv s a b m =
7111 if always || a <> b
7112 then
7113 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7114 and oPm s a b =
7115 if always || a <> b
7116 then
7117 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7119 oi "width" c.cwinw dc.cwinw;
7120 oi "height" c.cwinh dc.cwinh;
7121 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7122 oi "scroll-handle-height" c.scrollh dc.scrollh;
7123 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7124 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7125 ob "case-insensitive-search" c.icase dc.icase;
7126 ob "preload" c.preload dc.preload;
7127 oi "page-bias" c.pagebias dc.pagebias;
7128 oi "scroll-step" c.scrollstep dc.scrollstep;
7129 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7130 ob "max-height-fit" c.maxhfit dc.maxhfit;
7131 ob "crop-hack" c.crophack dc.crophack;
7132 oW "throttle" c.maxwait dc.maxwait;
7133 ob "highlight-links" c.hlinks dc.hlinks;
7134 ob "under-cursor-info" c.underinfo dc.underinfo;
7135 oi "vertical-margin" c.interpagespace dc.interpagespace;
7136 oz "zoom" c.zoom dc.zoom;
7137 ob "presentation" c.presentation dc.presentation;
7138 oi "rotation-angle" c.angle dc.angle;
7139 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7140 oFm "fit-model" c.fitmodel dc.fitmodel;
7141 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7142 oi "tex-count" c.texcount dc.texcount;
7143 oi "slice-height" c.sliceheight dc.sliceheight;
7144 oi "thumbnail-width" c.thumbw dc.thumbw;
7145 ob "persistent-location" c.jumpback dc.jumpback;
7146 oc "background-color" c.bgcolor dc.bgcolor;
7147 oi "tile-width" c.tilew dc.tilew;
7148 oi "tile-height" c.tileh dc.tileh;
7149 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7150 ob "checkers" c.checkers dc.checkers;
7151 oi "aalevel" c.aalevel dc.aalevel;
7152 ob "trim-margins" c.trimmargins dc.trimmargins;
7153 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7154 os "uri-launcher" c.urilauncher dc.urilauncher;
7155 os "path-launcher" c.pathlauncher dc.pathlauncher;
7156 oC "color-space" c.colorspace dc.colorspace;
7157 ob "invert-colors" c.invert dc.invert;
7158 oF "brightness" c.colorscale dc.colorscale;
7159 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7160 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7161 oco "columns" c.columns dc.columns;
7162 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7163 os "selection-command" c.selcmd dc.selcmd;
7164 os "synctex-command" c.stcmd dc.stcmd;
7165 os "pax-command" c.paxcmd dc.paxcmd;
7166 ob "update-cursor" c.updatecurs dc.updatecurs;
7167 oi "hint-font-size" c.hfsize dc.hfsize;
7168 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7169 oF "page-scroll-scale" c.pgscale dc.pgscale;
7170 ob "use-pbo" c.usepbo dc.usepbo;
7171 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7172 ob "remote-in-a-new-instance" c.riani dc.riani;
7173 op "point-and-x" c.pax dc.pax;
7174 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7177 let keymapsbuf always dc c =
7178 let bb = Buffer.create 16 in
7179 let rec loop = function
7180 | [] -> ()
7181 | (modename, h) :: rest ->
7182 let dh = findkeyhash dc modename in
7183 if always || h <> dh
7184 then (
7185 if Hashtbl.length h > 0
7186 then (
7187 if Buffer.length bb > 0
7188 then Buffer.add_char bb '\n';
7189 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7190 Hashtbl.iter (fun i o ->
7191 let isdifferent = always ||
7193 let dO = Hashtbl.find dh i in
7194 dO <> o
7195 with Not_found -> true
7197 if isdifferent
7198 then
7199 let addkm (k, m) =
7200 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7201 if Wsi.withalt m then Buffer.add_string bb "alt-";
7202 if Wsi.withshift m then Buffer.add_string bb "shift-";
7203 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7204 Buffer.add_string bb (Wsi.keyname k);
7206 let addkms l =
7207 let rec loop = function
7208 | [] -> ()
7209 | km :: [] -> addkm km
7210 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7212 loop l
7214 Buffer.add_string bb "<map in='";
7215 addkm i;
7216 match o with
7217 | KMinsrt km ->
7218 Buffer.add_string bb "' out='";
7219 addkm km;
7220 Buffer.add_string bb "'/>\n"
7222 | KMinsrl kms ->
7223 Buffer.add_string bb "' out='";
7224 addkms kms;
7225 Buffer.add_string bb "'/>\n"
7227 | KMmulti (ins, kms) ->
7228 Buffer.add_char bb ' ';
7229 addkms ins;
7230 Buffer.add_string bb "' out='";
7231 addkms kms;
7232 Buffer.add_string bb "'/>\n"
7233 ) h;
7234 Buffer.add_string bb "</keymap>";
7237 loop rest
7239 loop c.keyhashes;
7243 let save () =
7244 let uifontsize = fstate.fontsize in
7245 let bb = Buffer.create 32768 in
7246 let relx = float state.x /. float state.winw in
7247 let w, h, x =
7248 let cx w = truncate (relx *. float w) in
7249 List.fold_left
7250 (fun (w, h, x) ws ->
7251 match ws with
7252 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7253 | Wsi.MaxVert -> (w, conf.cwinh, x)
7254 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7256 (state.winw, state.winh, state.x) state.winstate
7258 conf.cwinw <- w;
7259 conf.cwinh <- h;
7260 let f (h, dc) =
7261 let dc = if conf.bedefault then conf else dc in
7262 Buffer.add_string bb "<llppconfig>\n";
7264 if String.length !fontpath > 0
7265 then
7266 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7267 uifontsize
7268 !fontpath
7269 else (
7270 if uifontsize <> 14
7271 then
7272 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7275 Buffer.add_string bb "<defaults ";
7276 add_attrs bb true dc dc;
7277 let kb = keymapsbuf true dc dc in
7278 if Buffer.length kb > 0
7279 then (
7280 Buffer.add_string bb ">\n";
7281 Buffer.add_buffer bb kb;
7282 Buffer.add_string bb "\n</defaults>\n";
7284 else Buffer.add_string bb "/>\n";
7286 let adddoc path pan anchor c bookmarks =
7287 if bookmarks == [] && c = dc && anchor = emptyanchor
7288 then ()
7289 else (
7290 Printf.bprintf bb "<doc path='%s'"
7291 (enent path 0 (String.length path));
7293 if anchor <> emptyanchor
7294 then (
7295 let n, rely, visy = anchor in
7296 Printf.bprintf bb " page='%d'" n;
7297 if rely > 1e-6
7298 then
7299 Printf.bprintf bb " rely='%f'" rely
7301 if abs_float visy > 1e-6
7302 then
7303 Printf.bprintf bb " visy='%f'" visy
7307 if pan != 0
7308 then Printf.bprintf bb " pan='%d'" pan;
7310 add_attrs bb false dc c;
7311 let kb = keymapsbuf false dc c in
7313 begin match bookmarks with
7314 | [] ->
7315 if Buffer.length kb > 0
7316 then (
7317 Buffer.add_string bb ">\n";
7318 Buffer.add_buffer bb kb;
7319 Buffer.add_string bb "\n</doc>\n";
7321 else Buffer.add_string bb "/>\n"
7322 | _ ->
7323 Buffer.add_string bb ">\n<bookmarks>\n";
7324 List.iter (fun (title, _level, (page, rely, visy)) ->
7325 Printf.bprintf bb
7326 "<item title='%s' page='%d'"
7327 (enent title 0 (String.length title))
7328 page
7330 if rely > 1e-6
7331 then
7332 Printf.bprintf bb " rely='%f'" rely
7334 if abs_float visy > 1e-6
7335 then
7336 Printf.bprintf bb " visy='%f'" visy
7338 Buffer.add_string bb "/>\n";
7339 ) bookmarks;
7340 Buffer.add_string bb "</bookmarks>";
7341 if Buffer.length kb > 0
7342 then (
7343 Buffer.add_string bb "\n";
7344 Buffer.add_buffer bb kb;
7346 Buffer.add_string bb "\n</doc>\n";
7347 end;
7351 let pan, conf =
7352 match state.mode with
7353 | Birdseye (c, pan, _, _, _) ->
7354 let beyecolumns =
7355 match conf.columns with
7356 | Cmulti ((c, _, _), _) -> Some c
7357 | Csingle _ -> None
7358 | Csplit _ -> None
7359 and columns =
7360 match c.columns with
7361 | Cmulti (c, _) -> Cmulti (c, [||])
7362 | Csingle _ -> Csingle [||]
7363 | Csplit _ -> failwith "quit from bird's eye while split"
7365 pan, { c with beyecolumns = beyecolumns; columns = columns }
7366 | _ -> x, conf
7368 let basename = Filename.basename
7369 (if String.length state.origin = 0 then state.path else state.origin)
7371 adddoc basename pan (getanchor ())
7372 (let conf =
7373 let autoscrollstep =
7374 match state.autoscroll with
7375 | Some step -> step
7376 | None -> conf.autoscrollstep
7378 match state.mode with
7379 | Birdseye (bc, _, _, _, _) ->
7380 { conf with
7381 zoom = bc.zoom;
7382 presentation = bc.presentation;
7383 interpagespace = bc.interpagespace;
7384 maxwait = bc.maxwait;
7385 autoscrollstep = autoscrollstep }
7386 | _ -> { conf with autoscrollstep = autoscrollstep }
7387 in conf)
7388 (if conf.savebmarks then state.bookmarks else []);
7390 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7391 if basename <> path
7392 then adddoc path x anchor c bookmarks
7393 ) h;
7394 Buffer.add_string bb "</llppconfig>\n";
7395 true;
7397 if load1 f && Buffer.length bb > 0
7398 then
7400 let tmp = !confpath ^ ".tmp" in
7401 let oc = open_out_bin tmp in
7402 Buffer.output_buffer oc bb;
7403 close_out oc;
7404 Unix.rename tmp !confpath;
7405 with exn ->
7406 prerr_endline
7407 ("error while saving configuration: " ^ exntos exn)
7409 end;;
7411 let adderrmsg src msg =
7412 Buffer.add_string state.errmsgs msg;
7413 state.newerrmsgs <- true;
7414 G.postRedisplay src
7417 let adderrfmt src fmt =
7418 Format.kprintf (fun s -> adderrmsg src s) fmt;
7421 let ract cmds =
7422 let cl = splitatspace cmds in
7423 let scan s fmt f =
7424 try Scanf.sscanf s fmt f
7425 with exn ->
7426 adderrfmt "remote exec"
7427 "error processing '%S': %s\n" cmds (exntos exn)
7429 match cl with
7430 | "reload" :: [] -> reload ()
7431 | "goto" :: args :: [] ->
7432 scan args "%u %f %f"
7433 (fun pageno x y ->
7434 let cmd, _ = state.geomcmds in
7435 if String.length cmd = 0
7436 then gotopagexy pageno x y
7437 else
7438 let f prevf () =
7439 gotopagexy pageno x y;
7440 prevf ()
7442 state.reprf <- f state.reprf
7444 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7445 | "rect" :: args :: [] ->
7446 scan args "%u %u %f %f %f %f"
7447 (fun pageno color x0 y0 x1 y1 ->
7448 onpagerect pageno (fun w h ->
7449 let _,w1,h1,_ = getpagedim pageno in
7450 let sw = float w1 /. w
7451 and sh = float h1 /. h in
7452 let x0s = x0 *. sw
7453 and x1s = x1 *. sw
7454 and y0s = y0 *. sh
7455 and y1s = y1 *. sh in
7456 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7457 debugrect rect;
7458 state.rects <- (pageno, color, rect) :: state.rects;
7459 G.postRedisplay "rect";
7462 | "activatewin" :: [] -> Wsi.activatewin ()
7463 | "quit" :: [] -> raise Quit
7464 | _ ->
7465 adderrfmt "remote command"
7466 "error processing remote command: %S\n" cmds;
7469 let remote =
7470 let scratch = String.create 80 in
7471 let buf = Buffer.create 80 in
7472 fun fd ->
7473 let rec tempfr () =
7474 try Some (Unix.read fd scratch 0 80)
7475 with
7476 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7477 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7478 | exn -> raise exn
7480 match tempfr () with
7481 | None -> Some fd
7482 | Some n ->
7483 if n = 0
7484 then (
7485 Unix.close fd;
7486 if Buffer.length buf > 0
7487 then (
7488 let s = Buffer.contents buf in
7489 Buffer.clear buf;
7490 ract s;
7492 None
7494 else
7495 let rec eat ppos =
7496 let nlpos =
7498 let pos = String.index_from scratch ppos '\n' in
7499 if pos >= n then -1 else pos
7500 with Not_found -> -1
7502 if nlpos >= 0
7503 then (
7504 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7505 let s = Buffer.contents buf in
7506 Buffer.clear buf;
7507 ract s;
7508 eat (nlpos+1);
7510 else (
7511 Buffer.add_substring buf scratch ppos (n-ppos);
7512 Some fd
7514 in eat 0
7517 let remoteopen path =
7518 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7519 with exn ->
7520 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7521 None
7524 let () =
7525 let trimcachepath = ref "" in
7526 let rcmdpath = ref "" in
7527 selfexec := Sys.executable_name;
7528 Arg.parse
7529 (Arg.align
7530 [("-p", Arg.String (fun s -> state.password <- s),
7531 "<password> Set password");
7533 ("-f", Arg.String
7534 (fun s ->
7535 Config.fontpath := s;
7536 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7538 "<path> Set path to the user interface font");
7540 ("-c", Arg.String
7541 (fun s ->
7542 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7543 Config.confpath := s),
7544 "<path> Set path to the configuration file");
7546 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7547 "<path> Set path to the trim cache file");
7549 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7550 "<named-destination> Set named destination");
7552 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7553 ("-cxack", Arg.Set cxack, " Cut corners");
7555 ("-remote", Arg.String (fun s -> rcmdpath := s),
7556 "<path> Set path to the remote commands source");
7558 ("-origin", Arg.String (fun s -> state.origin <- s),
7559 "<original-path> Set original path");
7561 ("-v", Arg.Unit (fun () ->
7562 Printf.printf
7563 "%s\nconfiguration path: %s\n"
7564 (version ())
7565 Config.defconfpath
7567 exit 0), " Print version and exit");
7570 (fun s -> state.path <- s)
7571 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7573 if !wtmode
7574 then selfexec := !selfexec ^ " -wtmode";
7576 if String.length state.path = 0
7577 then (prerr_endline "file name missing"; exit 1);
7579 if not (Config.load ())
7580 then prerr_endline "failed to load configuration";
7582 let wsfd, winw, winh = Wsi.init (object
7583 val mutable m_hack = false
7584 method expose = if not m_hack then G.postRedisplay "expose"
7585 method visible = G.postRedisplay "visible"
7586 method display = m_hack <- false; display ()
7587 method reshape w h =
7588 m_hack <- w < state.winw && h < state.winh;
7589 reshape w h
7590 method mouse b d x y m = mouse b d x y m
7591 method motion x y = state.mpos <- (x, y); motion x y
7592 method pmotion x y = state.mpos <- (x, y); pmotion x y
7593 method key k m =
7594 let mascm = m land (
7595 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7596 ) in
7597 match state.keystate with
7598 | KSnone ->
7599 let km = k, mascm in
7600 begin
7601 match
7602 let modehash = state.uioh#modehash in
7603 try Hashtbl.find modehash km
7604 with Not_found ->
7605 try Hashtbl.find (findkeyhash conf "global") km
7606 with Not_found -> KMinsrt (k, m)
7607 with
7608 | KMinsrt (k, m) -> keyboard k m
7609 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7610 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7612 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7613 List.iter (fun (k, m) -> keyboard k m) insrt;
7614 state.keystate <- KSnone
7615 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7616 state.keystate <- KSinto (keys, insrt)
7617 | _ ->
7618 state.keystate <- KSnone
7620 method enter x y = state.mpos <- (x, y); pmotion x y
7621 method leave = state.mpos <- (-1, -1)
7622 method winstate wsl = state.winstate <- wsl
7623 method quit = raise Quit
7624 end) conf.cwinw conf.cwinh (platform = Posx) in
7626 state.wsfd <- wsfd;
7628 if not (
7629 List.exists GlMisc.check_extension
7630 [ "GL_ARB_texture_rectangle"
7631 ; "GL_EXT_texture_recangle"
7632 ; "GL_NV_texture_rectangle" ]
7634 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7636 if (
7637 let r = GlMisc.get_string `renderer in
7638 let p = "Mesa DRI Intel(" in
7639 let l = String.length p in
7640 String.length r > l && String.sub r 0 l = p
7642 then (
7643 defconf.sliceheight <- 1024;
7644 defconf.texcount <- 32;
7645 defconf.usepbo <- true;
7648 let cr, sw =
7649 match Ne.pipe () with
7650 | Ne.Exn exn ->
7651 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7652 exit 1
7653 | Ne.Res rw -> rw
7654 and sr, cw =
7655 match Ne.pipe () with
7656 | Ne.Exn exn ->
7657 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7658 exit 1
7659 | Ne.Res rw -> rw
7662 cloexec cr;
7663 cloexec sw;
7664 cloexec sr;
7665 cloexec cw;
7667 setcheckers conf.checkers;
7668 redirectstderr ();
7669 if conf.redirectstderr
7670 then
7671 at_exit (fun () ->
7672 let s = Buffer.contents state.errmsgs ^
7673 (match state.errfd with
7674 | Some fd ->
7675 let s = String.create (80*24) in
7676 let n =
7678 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7679 if List.mem fd r
7680 then Unix.read fd s 0 (String.length s)
7681 else 0
7682 with _ -> 0
7684 if n = 0
7685 then ""
7686 else String.sub s 0 n
7687 | None -> ""
7690 try ignore (Unix.write state.stderr s 0 (String.length s))
7691 with exn -> print_endline (exntos exn)
7695 init (cr, cw) (
7696 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7697 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7698 !Config.fontpath, !trimcachepath,
7699 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7701 state.sr <- sr;
7702 state.sw <- sw;
7703 state.text <- "Opening " ^ (mbtoutf8 state.path);
7704 reshape winw winh;
7705 opendoc state.path state.password;
7706 state.uioh <- uioh;
7707 display ();
7708 Wsi.mapwin ();
7709 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7710 let optrfd =
7711 ref (
7712 if String.length !rcmdpath > 0
7713 then remoteopen !rcmdpath
7714 else None
7718 let rec loop deadline =
7719 let r =
7720 match state.errfd with
7721 | None -> [state.sr; state.wsfd]
7722 | Some fd -> [state.sr; state.wsfd; fd]
7724 let r =
7725 match !optrfd with
7726 | None -> r
7727 | Some fd -> fd :: r
7729 if state.redisplay
7730 then (
7731 state.redisplay <- false;
7732 display ();
7734 let timeout =
7735 let now = now () in
7736 if deadline > now
7737 then (
7738 if deadline = infinity
7739 then ~-.1.0
7740 else max 0.0 (deadline -. now)
7742 else 0.0
7744 let r, _, _ =
7745 try Unix.select r [] [] timeout
7746 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7748 begin match r with
7749 | [] ->
7750 state.ghyll None;
7751 let newdeadline =
7752 if state.ghyll == noghyll
7753 then
7754 match state.autoscroll with
7755 | Some step when step != 0 ->
7756 let y = state.y + step in
7757 let y =
7758 if y < 0
7759 then state.maxy
7760 else if y >= state.maxy then 0 else y
7762 gotoy y;
7763 if state.mode = View
7764 then state.text <- "";
7765 deadline +. 0.01
7766 | _ -> infinity
7767 else deadline +. 0.01
7769 loop newdeadline
7771 | l ->
7772 let rec checkfds = function
7773 | [] -> ()
7774 | fd :: rest when fd = state.sr ->
7775 let cmd = readcmd state.sr in
7776 act cmd;
7777 checkfds rest
7779 | fd :: rest when fd = state.wsfd ->
7780 Wsi.readresp fd;
7781 checkfds rest
7783 | fd :: rest when Some fd = !optrfd ->
7784 begin match remote fd with
7785 | None -> optrfd := remoteopen !rcmdpath;
7786 | opt -> optrfd := opt
7787 end;
7788 checkfds rest
7790 | fd :: rest ->
7791 let s = String.create 80 in
7792 let n = tempfailureretry (Unix.read fd s 0) 80 in
7793 if conf.redirectstderr
7794 then (
7795 Buffer.add_substring state.errmsgs s 0 n;
7796 state.newerrmsgs <- true;
7797 state.redisplay <- true;
7799 else (
7800 prerr_string (String.sub s 0 n);
7801 flush stderr;
7803 checkfds rest
7805 checkfds l;
7806 let newdeadline =
7807 let deadline1 =
7808 if deadline = infinity
7809 then now () +. 0.01
7810 else deadline
7812 match state.autoscroll with
7813 | Some step when step != 0 -> deadline1
7814 | _ -> if state.ghyll == noghyll then infinity else deadline1
7816 loop newdeadline
7817 end;
7820 loop infinity;
7821 with Quit ->
7822 Config.save ();