Save correct pan value when window is maximized upon exit
[llpp.git] / main.ml
bloba70db25067ff324d24d8ca8a0669568bde5c6f4e
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 params = (angle * fitmodel * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and trimmargins = bool
29 and interpagespace = int
30 and texcount = int
31 and sliceheight = int
32 and gen = int
33 and top = float
34 and dtop = float
35 and fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
42 and fitmodel = | FitWidth | FitProportional | FitPage
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable cx : int
316 ; mutable savebmarks : bool
317 ; mutable fitmodel : fitmodel
318 ; mutable trimmargins : trimmargins
319 ; mutable trimfuzz : irect
320 ; mutable memlimit : memsize
321 ; mutable texcount : texcount
322 ; mutable sliceheight : sliceheight
323 ; mutable thumbw : width
324 ; mutable jumpback : bool
325 ; mutable bgcolor : (float * float * float)
326 ; mutable bedefault : bool
327 ; mutable scrollbarinpm : bool
328 ; mutable tilew : int
329 ; mutable tileh : int
330 ; mutable mustoresize : memsize
331 ; mutable checkers : bool
332 ; mutable aalevel : int
333 ; mutable urilauncher : string
334 ; mutable pathlauncher : string
335 ; mutable colorspace : colorspace
336 ; mutable invert : bool
337 ; mutable colorscale : float
338 ; mutable redirectstderr : bool
339 ; mutable ghyllscroll : (int * int * int) option
340 ; mutable columns : columns
341 ; mutable beyecolumns : columncount option
342 ; mutable selcmd : string
343 ; mutable updatecurs : bool
344 ; mutable keyhashes : (string * keyhash) list
345 ; mutable hfsize : int
346 ; mutable pgscale : float
347 ; mutable usepbo : bool
348 ; mutable wheelbypage : bool
349 ; mutable stcmd : string
351 and columns =
352 | Csingle of singlecolumn
353 | Cmulti of multicolumns
354 | Csplit of splitcolumns
357 type anchor = pageno * top * dtop;;
359 type outline = string * int * anchor;;
361 type rect = float * float * float * float * float * float * float * float;;
363 type tile = opaque * pixmapsize * elapsed
364 and elapsed = float;;
365 type pagemapkey = pageno * gen;;
366 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
367 and row = int
368 and col = int;;
370 let emptyanchor = (0, 0.0, 0.0);;
372 type infochange = | Memused | Docinfo | Pdim;;
374 class type uioh = object
375 method display : unit
376 method key : int -> int -> uioh
377 method button : int -> bool -> int -> int -> int -> uioh
378 method motion : int -> int -> uioh
379 method pmotion : int -> int -> uioh
380 method infochanged : infochange -> unit
381 method scrollpw : (int * float * float)
382 method scrollph : (int * float * float)
383 method modehash : keyhash
384 method eformsgs : bool
385 end;;
387 type mode =
388 | Birdseye of (conf * leftx * pageno * pageno * anchor)
389 | Textentry of (textentry * onleave)
390 | View
391 | LinkNav of linktarget
392 and onleave = leavetextentrystatus -> unit
393 and leavetextentrystatus = | Cancel | Confirm
394 and helpitem = string * int * action
395 and action =
396 | Noaction
397 | Action of (uioh -> uioh)
398 and linktarget =
399 | Ltexact of (pageno * int)
400 | Ltgendir of int
403 let isbirdseye = function Birdseye _ -> true | _ -> false;;
404 let istextentry = function Textentry _ -> true | _ -> false;;
406 type currently =
407 | Idle
408 | Loading of (page * gen)
409 | Tiling of (
410 page * opaque * colorspace * angle * gen * col * row * width * height
412 | Outlining of outline list
415 let emptykeyhash = Hashtbl.create 0;;
416 let nouioh : uioh = object (self)
417 method display = ()
418 method key _ _ = self
419 method button _ _ _ _ _ = self
420 method motion _ _ = self
421 method pmotion _ _ = self
422 method infochanged _ = ()
423 method scrollpw = (0, nan, nan)
424 method scrollph = (0, nan, nan)
425 method modehash = emptykeyhash
426 method eformsgs = false
427 end;;
429 type state =
430 { mutable sr : Unix.file_descr
431 ; mutable sw : Unix.file_descr
432 ; mutable wsfd : Unix.file_descr
433 ; mutable errfd : Unix.file_descr option
434 ; mutable stderr : Unix.file_descr
435 ; mutable errmsgs : Buffer.t
436 ; mutable newerrmsgs : bool
437 ; mutable w : int
438 ; mutable x : int
439 ; mutable y : int
440 ; mutable scrollw : int
441 ; mutable hscrollh : int
442 ; mutable anchor : anchor
443 ; mutable ranchors : (string * string * anchor * string) list
444 ; mutable maxy : int
445 ; mutable layout : page list
446 ; pagemap : (pagemapkey, opaque) Hashtbl.t
447 ; tilemap : (tilemapkey, tile) Hashtbl.t
448 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
449 ; mutable pdims : (pageno * width * height * leftx) list
450 ; mutable pagecount : int
451 ; mutable currently : currently
452 ; mutable mstate : mstate
453 ; mutable searchpattern : string
454 ; mutable rects : (pageno * recttype * rect) list
455 ; mutable rects1 : (pageno * recttype * rect) list
456 ; mutable text : string
457 ; mutable winstate : Wsi.winstate list
458 ; mutable mode : mode
459 ; mutable uioh : uioh
460 ; mutable outlines : outline array
461 ; mutable bookmarks : outline list
462 ; mutable path : string
463 ; mutable password : string
464 ; mutable nameddest : string
465 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
466 ; mutable memused : memsize
467 ; mutable gen : gen
468 ; mutable throttle : (page list * int * float) option
469 ; mutable autoscroll : int option
470 ; mutable ghyll : (int option -> unit)
471 ; mutable help : helpitem array
472 ; mutable docinfo : (int * string) list
473 ; mutable texid : GlTex.texture_id option
474 ; hists : hists
475 ; mutable prevzoom : float
476 ; mutable progress : float
477 ; mutable redisplay : bool
478 ; mutable mpos : mpos
479 ; mutable keystate : keystate
480 ; mutable glinks : bool
481 ; mutable prevcolumns : (columns * float) option
482 ; mutable winw : int
483 ; mutable winh : int
484 ; mutable reprf : (unit -> unit)
485 ; mutable origin : string
487 and hists =
488 { pat : string circbuf
489 ; pag : string circbuf
490 ; nav : anchor circbuf
491 ; sel : string circbuf
495 let defconf =
496 { scrollbw = 7
497 ; scrollh = 12
498 ; icase = true
499 ; preload = true
500 ; pagebias = 0
501 ; verbose = false
502 ; debug = false
503 ; scrollstep = 24
504 ; hscrollstep = 24
505 ; maxhfit = true
506 ; crophack = false
507 ; autoscrollstep = 2
508 ; maxwait = None
509 ; hlinks = false
510 ; underinfo = false
511 ; interpagespace = 2
512 ; zoom = 1.0
513 ; presentation = false
514 ; angle = 0
515 ; cwinw = 900
516 ; cwinh = 900
517 ; cx = 0
518 ; savebmarks = true
519 ; fitmodel = FitProportional
520 ; trimmargins = false
521 ; trimfuzz = (0,0,0,0)
522 ; memlimit = 32 lsl 20
523 ; texcount = 256
524 ; sliceheight = 24
525 ; thumbw = 76
526 ; jumpback = true
527 ; bgcolor = (0.5, 0.5, 0.5)
528 ; bedefault = false
529 ; scrollbarinpm = true
530 ; tilew = 2048
531 ; tileh = 2048
532 ; mustoresize = 256 lsl 20
533 ; checkers = true
534 ; aalevel = 8
535 ; urilauncher =
536 (match platform with
537 | Plinux | Pfreebsd | Pdragonflybsd
538 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
539 | Posx -> "open \"%s\""
540 | Pcygwin -> "cygstart \"%s\""
541 | Punknown -> "echo %s")
542 ; pathlauncher = "lp \"%s\""
543 ; selcmd =
544 (match platform with
545 | Plinux | Pfreebsd | Pdragonflybsd
546 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
547 | Posx -> "pbcopy"
548 | Pcygwin -> "wsel"
549 | Punknown -> "cat")
550 ; colorspace = Rgb
551 ; invert = false
552 ; colorscale = 1.0
553 ; redirectstderr = false
554 ; ghyllscroll = None
555 ; columns = Csingle [||]
556 ; beyecolumns = None
557 ; updatecurs = false
558 ; hfsize = 12
559 ; pgscale = 1.0
560 ; usepbo = false
561 ; wheelbypage = false
562 ; stcmd = "echo SyncTex"
563 ; keyhashes =
564 let mk n = (n, Hashtbl.create 1) in
565 [ mk "global"
566 ; mk "info"
567 ; mk "help"
568 ; mk "outline"
569 ; mk "listview"
570 ; mk "birdseye"
571 ; mk "textentry"
572 ; mk "links"
573 ; mk "view"
578 let wtmode = ref false;;
580 let findkeyhash c name =
581 try List.assoc name c.keyhashes
582 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
585 let conf = { defconf with angle = defconf.angle };;
587 let pgscale h = truncate (float h *. conf.pgscale);;
589 type fontstate =
590 { mutable fontsize : int
591 ; mutable wwidth : float
592 ; mutable maxrows : int
596 let fstate =
597 { fontsize = 14
598 ; wwidth = nan
599 ; maxrows = -1
603 let geturl s =
604 let colonpos = try String.index s ':' with Not_found -> -1 in
605 let len = String.length s in
606 if colonpos >= 0 && colonpos + 3 < len
607 then (
608 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
609 then
610 let schemestartpos =
611 try String.rindex_from s colonpos ' '
612 with Not_found -> -1
614 let scheme =
615 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
617 match scheme with
618 | "http" | "ftp" | "mailto" ->
619 let epos =
620 try String.index_from s colonpos ' '
621 with Not_found -> len
623 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
624 | _ -> ""
625 else ""
627 else ""
630 let gotouri uri =
631 if String.length conf.urilauncher = 0
632 then print_endline uri
633 else (
634 let url = geturl uri in
635 if String.length url = 0
636 then print_endline uri
637 else
638 let re = Str.regexp "%s" in
639 let command = Str.global_replace re url conf.urilauncher in
640 try popen command []
641 with exn ->
642 Printf.eprintf
643 "failed to execute `%s': %s\n" command (exntos exn);
644 flush stderr;
648 let version () =
649 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
650 (platform_to_string platform) Sys.word_size Sys.ocaml_version
653 let makehelp () =
654 let strings = version () :: "" :: Help.keys in
655 Array.of_list (
656 List.map (fun s ->
657 let url = geturl s in
658 if String.length url > 0
659 then (s, 0, Action (fun u -> gotouri url; u))
660 else (s, 0, Noaction)
661 ) strings);
664 let noghyll _ = ();;
665 let firstgeomcmds = "", [];;
666 let noreprf () = ();;
668 let state =
669 { sr = Unix.stdin
670 ; sw = Unix.stdin
671 ; wsfd = Unix.stdin
672 ; errfd = None
673 ; stderr = Unix.stderr
674 ; errmsgs = Buffer.create 0
675 ; newerrmsgs = false
676 ; x = 0
677 ; y = 0
678 ; w = 0
679 ; scrollw = 0
680 ; hscrollh = 0
681 ; anchor = emptyanchor
682 ; ranchors = []
683 ; layout = []
684 ; maxy = max_int
685 ; tilelru = Queue.create ()
686 ; pagemap = Hashtbl.create 10
687 ; tilemap = Hashtbl.create 10
688 ; pdims = []
689 ; pagecount = 0
690 ; currently = Idle
691 ; mstate = Mnone
692 ; rects = []
693 ; rects1 = []
694 ; text = ""
695 ; mode = View
696 ; winstate = []
697 ; searchpattern = ""
698 ; outlines = [||]
699 ; bookmarks = []
700 ; path = ""
701 ; password = ""
702 ; nameddest = ""
703 ; geomcmds = firstgeomcmds
704 ; hists =
705 { nav = cbnew 10 emptyanchor
706 ; pat = cbnew 10 ""
707 ; pag = cbnew 10 ""
708 ; sel = cbnew 10 ""
710 ; memused = 0
711 ; gen = 0
712 ; throttle = None
713 ; autoscroll = None
714 ; ghyll = noghyll
715 ; help = makehelp ()
716 ; docinfo = []
717 ; texid = None
718 ; prevzoom = 1.0
719 ; progress = -1.0
720 ; uioh = nouioh
721 ; redisplay = true
722 ; mpos = (-1, -1)
723 ; keystate = KSnone
724 ; glinks = false
725 ; prevcolumns = None
726 ; winw = -1
727 ; winh = -1
728 ; reprf = noreprf
729 ; origin = ""
733 let setfontsize n =
734 fstate.fontsize <- n;
735 fstate.wwidth <- measurestr fstate.fontsize "w";
736 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
739 let vlog fmt =
740 if conf.verbose
741 then
742 Printf.kprintf prerr_endline fmt
743 else
744 Printf.kprintf ignore fmt
747 let launchpath () =
748 if String.length conf.pathlauncher = 0
749 then print_endline state.path
750 else (
751 let re = Str.regexp "%s" in
752 let command = Str.global_replace re state.path conf.pathlauncher in
753 try popen command []
754 with exn ->
755 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
756 flush stderr;
760 module Ne = struct
761 type 'a t = | Res of 'a | Exn of exn;;
763 let pipe () =
764 try Res (Unix.pipe ())
765 with exn -> Exn exn
768 let clo fd f =
769 try tempfailureretry Unix.close fd
770 with exn -> f (exntos exn)
773 let dup fd =
774 try Res (tempfailureretry Unix.dup fd)
775 with exn -> Exn exn
778 let dup2 fd1 fd2 =
779 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
780 with exn -> Exn exn
782 end;;
784 let redirectstderr () =
785 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
786 if conf.redirectstderr
787 then
788 match Ne.pipe () with
789 | Ne.Exn exn ->
790 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
792 | Ne.Res (r, w) ->
793 begin match Ne.dup Unix.stderr with
794 | Ne.Exn exn ->
795 dolog "failed to dup stderr: %s" (exntos exn);
796 Ne.clo r (clofail "pipe/r");
797 Ne.clo w (clofail "pipe/w");
799 | Ne.Res dupstderr ->
800 begin match Ne.dup2 w Unix.stderr with
801 | Ne.Exn exn ->
802 dolog "failed to dup2 to stderr: %s" (exntos exn);
803 Ne.clo dupstderr (clofail "stderr duplicate");
804 Ne.clo r (clofail "redir pipe/r");
805 Ne.clo w (clofail "redir pipe/w");
807 | Ne.Res () ->
808 state.stderr <- dupstderr;
809 state.errfd <- Some r;
810 end;
812 else (
813 state.newerrmsgs <- false;
814 begin match state.errfd with
815 | Some fd ->
816 begin match Ne.dup2 state.stderr Unix.stderr with
817 | Ne.Exn exn ->
818 dolog "failed to dup2 original stderr: %s" (exntos exn)
819 | Ne.Res () ->
820 Ne.clo fd (clofail "dup of stderr");
821 state.errfd <- None;
822 end;
823 | None -> ()
824 end;
825 prerr_string (Buffer.contents state.errmsgs);
826 flush stderr;
827 Buffer.clear state.errmsgs;
831 module G =
832 struct
833 let postRedisplay who =
834 if conf.verbose
835 then prerr_endline ("redisplay for " ^ who);
836 state.redisplay <- true;
838 end;;
840 let getopaque pageno =
841 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
842 with Not_found -> None
845 let putopaque pageno opaque =
846 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
849 let pagetranslatepoint l x y =
850 let dy = y - l.pagedispy in
851 let y = dy + l.pagey in
852 let dx = x - l.pagedispx in
853 let x = dx + l.pagex in
854 (x, y);
857 let onppundermouse g x y d =
858 let rec f = function
859 | l :: rest ->
860 begin match getopaque l.pageno with
861 | Some opaque ->
862 let x0 = l.pagedispx in
863 let x1 = x0 + l.pagevw in
864 let y0 = l.pagedispy in
865 let y1 = y0 + l.pagevh in
866 if y >= y0 && y <= y1 && x >= x0 && x <= x1
867 then
868 let px, py = pagetranslatepoint l x y in
869 match g opaque l px py with
870 | Some res -> res
871 | None -> f rest
872 else f rest
873 | _ ->
874 f rest
876 | [] -> d
878 f state.layout
881 let getunder x y =
882 let g opaque _ px py =
883 match whatsunder opaque px py with
884 | Unone -> None
885 | under -> Some under
887 onppundermouse g x y Unone
890 let unproject x y =
891 let g opaque l x y =
892 match unproject opaque x y with
893 | Some (x, y) -> Some (Some (l.pageno, x, y))
894 | None -> None
896 onppundermouse g x y None;
899 let showtext c s =
900 state.text <- Printf.sprintf "%c%s" c s;
901 G.postRedisplay "showtext";
904 let selstring s =
905 match Ne.pipe () with
906 | Ne.Exn exn ->
907 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
908 | Ne.Res (r, w) ->
909 let popened =
910 try popen conf.selcmd [r, 0; w, -1]; true
911 with exn ->
912 showtext '!'
913 (Printf.sprintf "failed to execute %s: %s"
914 conf.selcmd (exntos exn));
915 false
917 let clo cap fd =
918 Ne.clo fd (fun msg ->
919 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
922 if popened
923 then
924 (try
925 let l = String.length s in
926 let n = tempfailureretry (Unix.write w s 0) l in
927 if n != l
928 then
929 showtext '!'
930 (Printf.sprintf
931 "failed to write %d characters to sel pipe, wrote %d"
934 with exn ->
935 showtext '!'
936 (Printf.sprintf "failed to write to sel pipe: %s"
937 (exntos exn)
940 else dolog "%s" s;
941 clo "pipe/r" r;
942 clo "pipe/w" w;
945 let undertext = function
946 | Unone -> "none"
947 | Ulinkuri s -> s
948 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
949 | Utext s -> "font: " ^ s
950 | Uunexpected s -> "unexpected: " ^ s
951 | Ulaunch s -> "launch: " ^ s
952 | Unamed s -> "named: " ^ s
953 | Uremote (filename, pageno) ->
954 Printf.sprintf "%s: page %d" filename (pageno+1)
957 let updateunder x y =
958 match getunder x y with
959 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
960 | Ulinkuri uri ->
961 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
962 Wsi.setcursor Wsi.CURSOR_INFO
963 | Ulinkgoto (pageno, _) ->
964 if conf.underinfo
965 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
966 Wsi.setcursor Wsi.CURSOR_INFO
967 | Utext s ->
968 if conf.underinfo then showtext 'f' ("ont: " ^ s);
969 Wsi.setcursor Wsi.CURSOR_TEXT
970 | Uunexpected s ->
971 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
972 Wsi.setcursor Wsi.CURSOR_INHERIT
973 | Ulaunch s ->
974 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
975 Wsi.setcursor Wsi.CURSOR_INHERIT
976 | Unamed s ->
977 if conf.underinfo then showtext 'n' ("amed: " ^ s);
978 Wsi.setcursor Wsi.CURSOR_INHERIT
979 | Uremote (filename, pageno) ->
980 if conf.underinfo then showtext 'r'
981 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
982 Wsi.setcursor Wsi.CURSOR_INFO
985 let showlinktype under =
986 if conf.underinfo
987 then
988 match under with
989 | Unone -> ()
990 | under ->
991 let s = undertext under in
992 showtext ' ' s
995 let addchar s c =
996 let b = Buffer.create (String.length s + 1) in
997 Buffer.add_string b s;
998 Buffer.add_char b c;
999 Buffer.contents b;
1002 let colorspace_of_string s =
1003 match String.lowercase s with
1004 | "rgb" -> Rgb
1005 | "bgr" -> Bgr
1006 | "gray" -> Gray
1007 | _ -> failwith "invalid colorspace"
1010 let int_of_colorspace = function
1011 | Rgb -> 0
1012 | Bgr -> 1
1013 | Gray -> 2
1016 let colorspace_of_int = function
1017 | 0 -> Rgb
1018 | 1 -> Bgr
1019 | 2 -> Gray
1020 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1023 let colorspace_to_string = function
1024 | Rgb -> "rgb"
1025 | Bgr -> "bgr"
1026 | Gray -> "gray"
1029 let fitmodel_of_string s =
1030 match String.lowercase s with
1031 | "width" -> FitWidth
1032 | "proportional" -> FitProportional
1033 | "page" -> FitPage
1034 | _ -> failwith "invalid fit model"
1037 let int_of_fitmodel = function
1038 | FitWidth -> 0
1039 | FitProportional -> 1
1040 | FitPage -> 2
1043 let fitmodel_of_int = function
1044 | 0 -> FitWidth
1045 | 1 -> FitProportional
1046 | 2 -> FitPage
1047 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1050 let fitmodel_to_string = function
1051 | FitWidth -> "width"
1052 | FitProportional -> "proportional"
1053 | FitPage -> "page"
1056 let intentry_with_suffix text key =
1057 let c =
1058 if key >= 32 && key < 127
1059 then Char.chr key
1060 else '\000'
1062 match Char.lowercase c with
1063 | '0' .. '9' ->
1064 let text = addchar text c in
1065 TEcont text
1067 | 'k' | 'm' | 'g' ->
1068 let text = addchar text c in
1069 TEcont text
1071 | _ ->
1072 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1073 TEcont text
1076 let multicolumns_to_string (n, a, b) =
1077 if a = 0 && b = 0
1078 then Printf.sprintf "%d" n
1079 else Printf.sprintf "%d,%d,%d" n a b;
1082 let multicolumns_of_string s =
1084 (int_of_string s, 0, 0)
1085 with _ ->
1086 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1087 if a > 1 || b > 1
1088 then failwith "subtly broken"; (n, a, b)
1092 let readcmd fd =
1093 let s = "xxxx" in
1094 let n = tempfailureretry (Unix.read fd s 0) 4 in
1095 if n != 4 then failwith "incomplete read(len)";
1096 let len = 0
1097 lor (Char.code s.[0] lsl 24)
1098 lor (Char.code s.[1] lsl 16)
1099 lor (Char.code s.[2] lsl 8)
1100 lor (Char.code s.[3] lsl 0)
1102 let s = String.create len in
1103 let n = tempfailureretry (Unix.read fd s 0) len in
1104 if n != len then failwith "incomplete read(data)";
1108 let btod b = if b then 1 else 0;;
1110 let wcmd fmt =
1111 let b = Buffer.create 16 in
1112 Buffer.add_string b "llll";
1113 Printf.kbprintf
1114 (fun b ->
1115 let s = Buffer.contents b in
1116 let n = String.length s in
1117 let len = n - 4 in
1118 (* dolog "wcmd %S" (String.sub s 4 len); *)
1119 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1120 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1121 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1122 s.[3] <- Char.chr (len land 0xff);
1123 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1124 if n' != n then failwith "write failed";
1125 ) b fmt;
1128 let calcips h =
1129 let d = state.winh - h in
1130 max conf.interpagespace ((d + 1) / 2)
1133 let rowyh (c, coverA, coverB) b n =
1134 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1135 then
1136 let _, _, vy, (_, _, h, _) = b.(n) in
1137 (vy, h)
1138 else
1139 let n' = n - coverA in
1140 let d = n' mod c in
1141 let s = n - d in
1142 let e = min state.pagecount (s + c) in
1143 let rec find m miny maxh = if m = e then miny, maxh else
1144 let _, _, y, (_, _, h, _) = b.(m) in
1145 let miny = min miny y in
1146 let maxh = max maxh h in
1147 find (m+1) miny maxh
1148 in find s max_int 0
1151 let calcheight () =
1152 match conf.columns with
1153 | Cmulti ((_, _, _) as cl, b) ->
1154 if Array.length b > 0
1155 then
1156 let y, h = rowyh cl b (Array.length b - 1) in
1157 y + h + (if conf.presentation then calcips h else 0)
1158 else 0
1159 | Csingle b ->
1160 if Array.length b > 0
1161 then
1162 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1163 y + h + (if conf.presentation then calcips h else 0)
1164 else 0
1165 | Csplit (_, b) ->
1166 if Array.length b > 0
1167 then
1168 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1169 y + h
1170 else 0
1173 let getpageyh pageno =
1174 let pageno = bound pageno 0 (state.pagecount-1) in
1175 match conf.columns with
1176 | Csingle b ->
1177 if Array.length b = 0
1178 then 0, 0
1179 else
1180 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1181 let y =
1182 if conf.presentation
1183 then y - calcips h
1184 else y
1186 y, h
1187 | Cmulti (cl, b) ->
1188 if Array.length b = 0
1189 then 0, 0
1190 else
1191 let y, h = rowyh cl b pageno in
1192 let y =
1193 if conf.presentation
1194 then y - calcips h
1195 else y
1197 y, h
1198 | Csplit (c, b) ->
1199 if Array.length b = 0
1200 then 0, 0
1201 else
1202 let n = pageno*c in
1203 let (_, _, y, (_, _, h, _)) = b.(n) in
1204 y, h
1207 let getpagedim pageno =
1208 let rec f ppdim l =
1209 match l with
1210 | (n, _, _, _) as pdim :: rest ->
1211 if n >= pageno
1212 then (if n = pageno then pdim else ppdim)
1213 else f pdim rest
1215 | [] -> ppdim
1217 f (-1, -1, -1, -1) state.pdims
1220 let getpagey pageno = fst (getpageyh pageno);;
1222 let nogeomcmds cmds =
1223 match cmds with
1224 | s, [] -> String.length s = 0
1225 | _ -> false
1228 let page_of_y y =
1229 let ((c, coverA, coverB) as cl), b =
1230 match conf.columns with
1231 | Csingle b -> (1, 0, 0), b
1232 | Cmulti (c, b) -> c, b
1233 | Csplit (_, b) -> (1, 0, 0), b
1235 if Array.length b = 0
1236 then -1
1237 else
1238 let rec bsearch nmin nmax =
1239 if nmin > nmax
1240 then bound nmin 0 (state.pagecount-1)
1241 else
1242 let n = (nmax + nmin) / 2 in
1243 let vy, h = rowyh cl b n in
1244 let y0, y1 =
1245 if conf.presentation
1246 then
1247 let ips = calcips h in
1248 let y0 = vy - ips in
1249 let y1 = vy + h + ips in
1250 y0, y1
1251 else (
1252 if n = 0
1253 then 0, vy + h + conf.interpagespace
1254 else
1255 let y0 = vy - conf.interpagespace in
1256 y0, y0 + h + conf.interpagespace
1259 if y >= y0 && y < y1
1260 then (
1261 if c = 1
1262 then n
1263 else (
1264 if n > coverA
1265 then
1266 if n < state.pagecount - coverB
1267 then ((n-coverA)/c)*c + coverA
1268 else n
1269 else n
1272 else (
1273 if y > y0
1274 then bsearch (n+1) nmax
1275 else bsearch nmin (n-1)
1278 let r = bsearch 0 (state.pagecount-1) in
1282 let layoutN ((columns, coverA, coverB), b) y sh =
1283 let sh = sh - state.hscrollh in
1284 let rec fold accu n =
1285 if n = Array.length b
1286 then accu
1287 else
1288 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1289 if (vy - y) > sh &&
1290 (n = coverA - 1
1291 || n = state.pagecount - coverB
1292 || (n - coverA) mod columns = columns - 1)
1293 then accu
1294 else
1295 let accu =
1296 if vy + h > y
1297 then
1298 let pagey = max 0 (y - vy) in
1299 let pagedispy = if pagey > 0 then 0 else vy - y in
1300 let pagedispx, pagex =
1301 let pdx =
1302 if n = coverA - 1 || n = state.pagecount - coverB
1303 then state.x + (state.winw - state.scrollw - w) / 2
1304 else dx + xoff + state.x
1306 if pdx < 0
1307 then 0, -pdx
1308 else pdx, 0
1310 let pagevw =
1311 let vw = state.winw - state.scrollw - pagedispx in
1312 let pw = w - pagex in
1313 min vw pw
1315 let pagevh = min (h - pagey) (sh - pagedispy) in
1316 if pagevw > 0 && pagevh > 0
1317 then
1318 let e =
1319 { pageno = n
1320 ; pagedimno = pdimno
1321 ; pagew = w
1322 ; pageh = h
1323 ; pagex = pagex
1324 ; pagey = pagey
1325 ; pagevw = pagevw
1326 ; pagevh = pagevh
1327 ; pagedispx = pagedispx
1328 ; pagedispy = pagedispy
1329 ; pagecol = 0
1332 e :: accu
1333 else
1334 accu
1335 else
1336 accu
1338 fold accu (n+1)
1340 List.rev (fold [] (page_of_y y));
1343 let layoutS (columns, b) y sh =
1344 let sh = sh - state.hscrollh in
1345 let rec fold accu n =
1346 if n = Array.length b
1347 then accu
1348 else
1349 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1350 if (vy - y) > sh
1351 then accu
1352 else
1353 let accu =
1354 if vy + pageh > y
1355 then
1356 let x = xoff + state.x in
1357 let pagey = max 0 (y - vy) in
1358 let pagedispy = if pagey > 0 then 0 else vy - y in
1359 let pagedispx, pagex =
1360 if px = 0
1361 then (
1362 if x < 0
1363 then 0, -x
1364 else x, 0
1366 else (
1367 let px = px - x in
1368 if px < 0
1369 then -px, 0
1370 else 0, px
1373 let pagecolw = pagew/columns in
1374 let pagedispx =
1375 if pagecolw < state.winw
1376 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1377 else pagedispx
1379 let pagevw =
1380 let vw = state.winw - pagedispx - state.scrollw in
1381 let pw = pagew - pagex in
1382 min vw pw
1384 let pagevw = min pagevw pagecolw in
1385 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1386 if pagevw > 0 && pagevh > 0
1387 then
1388 let e =
1389 { pageno = n/columns
1390 ; pagedimno = pdimno
1391 ; pagew = pagew
1392 ; pageh = pageh
1393 ; pagex = pagex
1394 ; pagey = pagey
1395 ; pagevw = pagevw
1396 ; pagevh = pagevh
1397 ; pagedispx = pagedispx
1398 ; pagedispy = pagedispy
1399 ; pagecol = n mod columns
1402 e :: accu
1403 else
1404 accu
1405 else
1406 accu
1408 fold accu (n+1)
1410 List.rev (fold [] 0)
1413 let layout y sh =
1414 if nogeomcmds state.geomcmds
1415 then
1416 match conf.columns with
1417 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1418 | Cmulti c -> layoutN c y sh
1419 | Csplit s -> layoutS s y sh
1420 else []
1423 let clamp incr =
1424 let y = state.y + incr in
1425 let y = max 0 y in
1426 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1430 let itertiles l f =
1431 let tilex = l.pagex mod conf.tilew in
1432 let tiley = l.pagey mod conf.tileh in
1434 let col = l.pagex / conf.tilew in
1435 let row = l.pagey / conf.tileh in
1437 let rec rowloop row y0 dispy h =
1438 if h = 0
1439 then ()
1440 else (
1441 let dh = conf.tileh - y0 in
1442 let dh = min h dh in
1443 let rec colloop col x0 dispx w =
1444 if w = 0
1445 then ()
1446 else (
1447 let dw = conf.tilew - x0 in
1448 let dw = min w dw in
1450 f col row dispx dispy x0 y0 dw dh;
1451 colloop (col+1) 0 (dispx+dw) (w-dw)
1454 colloop col tilex l.pagedispx l.pagevw;
1455 rowloop (row+1) 0 (dispy+dh) (h-dh)
1458 if l.pagevw > 0 && l.pagevh > 0
1459 then rowloop row tiley l.pagedispy l.pagevh;
1462 let gettileopaque l col row =
1463 let key =
1464 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1466 try Some (Hashtbl.find state.tilemap key)
1467 with Not_found -> None
1470 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1471 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1472 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1475 let drawtiles l color =
1476 GlDraw.color color;
1477 let f col row x y tilex tiley w h =
1478 match gettileopaque l col row with
1479 | Some (opaque, _, t) ->
1480 let params = x, y, w, h, tilex, tiley in
1481 if conf.invert
1482 then (
1483 Gl.enable `blend;
1484 GlFunc.blend_func `zero `one_minus_src_color;
1486 drawtile params opaque;
1487 if conf.invert
1488 then Gl.disable `blend;
1489 if conf.debug
1490 then (
1491 let s = Printf.sprintf
1492 "%d[%d,%d] %f sec"
1493 l.pageno col row t
1495 let w = measurestr fstate.fontsize s in
1496 GlMisc.push_attrib [`current];
1497 GlDraw.color (0.0, 0.0, 0.0);
1498 GlDraw.rect
1499 (float (x-2), float (y-2))
1500 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1501 GlDraw.color (1.0, 1.0, 1.0);
1502 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1503 GlMisc.pop_attrib ();
1506 | _ ->
1507 let w =
1508 let lw = state.winw - state.scrollw - x in
1509 min lw w
1510 and h =
1511 let lh = state.winh - y in
1512 min lh h
1514 begin match state.texid with
1515 | Some id ->
1516 Gl.enable `texture_2d;
1517 GlTex.bind_texture `texture_2d id;
1518 let x0 = float x
1519 and y0 = float y
1520 and x1 = float (x+w)
1521 and y1 = float (y+h) in
1523 let tw = float w /. 16.0
1524 and th = float h /. 16.0 in
1525 let tx0 = float tilex /. 16.0
1526 and ty0 = float tiley /. 16.0 in
1527 let tx1 = tx0 +. tw
1528 and ty1 = ty0 +. th in
1529 GlDraw.begins `quads;
1530 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1531 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1532 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1533 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1534 GlDraw.ends ();
1536 Gl.disable `texture_2d;
1537 | None ->
1538 GlDraw.color (1.0, 1.0, 1.0);
1539 GlDraw.rect
1540 (float x, float y)
1541 (float (x+w), float (y+h));
1542 end;
1543 if w > 128 && h > fstate.fontsize + 10
1544 then (
1545 GlDraw.color (0.0, 0.0, 0.0);
1546 let c, r =
1547 if conf.verbose
1548 then (col*conf.tilew, row*conf.tileh)
1549 else col, row
1551 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1553 GlDraw.color color;
1555 itertiles l f
1558 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1560 let tilevisible1 l x y =
1561 let ax0 = l.pagex
1562 and ax1 = l.pagex + l.pagevw
1563 and ay0 = l.pagey
1564 and ay1 = l.pagey + l.pagevh in
1566 let bx0 = x
1567 and by0 = y in
1568 let bx1 = min (bx0 + conf.tilew) l.pagew
1569 and by1 = min (by0 + conf.tileh) l.pageh in
1571 let rx0 = max ax0 bx0
1572 and ry0 = max ay0 by0
1573 and rx1 = min ax1 bx1
1574 and ry1 = min ay1 by1 in
1576 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1577 nonemptyintersection
1580 let tilevisible layout n x y =
1581 let rec findpageinlayout m = function
1582 | l :: rest when l.pageno = n ->
1583 tilevisible1 l x y || (
1584 match conf.columns with
1585 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1586 | _ -> false
1588 | _ :: rest -> findpageinlayout 0 rest
1589 | [] -> false
1591 findpageinlayout 0 layout;
1594 let tileready l x y =
1595 tilevisible1 l x y &&
1596 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1599 let tilepage n p layout =
1600 let rec loop = function
1601 | l :: rest ->
1602 if l.pageno = n
1603 then
1604 let f col row _ _ _ _ _ _ =
1605 if state.currently = Idle
1606 then
1607 match gettileopaque l col row with
1608 | Some _ -> ()
1609 | None ->
1610 let x = col*conf.tilew
1611 and y = row*conf.tileh in
1612 let w =
1613 let w = l.pagew - x in
1614 min w conf.tilew
1616 let h =
1617 let h = l.pageh - y in
1618 min h conf.tileh
1620 let pbo =
1621 if conf.usepbo
1622 then getpbo w h conf.colorspace
1623 else "0"
1625 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1626 state.currently <-
1627 Tiling (
1628 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1629 conf.tilew, conf.tileh
1632 itertiles l f;
1633 else
1634 loop rest
1636 | [] -> ()
1638 if nogeomcmds state.geomcmds
1639 then loop layout;
1642 let preloadlayout y =
1643 let y = if y < state.winh then 0 else y - state.winh in
1644 let h = state.winh*3 in
1645 layout y h;
1648 let load pages =
1649 let rec loop pages =
1650 if state.currently != Idle
1651 then ()
1652 else
1653 match pages with
1654 | l :: rest ->
1655 begin match getopaque l.pageno with
1656 | None ->
1657 wcmd "page %d %d" l.pageno l.pagedimno;
1658 state.currently <- Loading (l, state.gen);
1659 | Some opaque ->
1660 tilepage l.pageno opaque pages;
1661 loop rest
1662 end;
1663 | _ -> ()
1665 if nogeomcmds state.geomcmds
1666 then loop pages
1669 let preload pages =
1670 load pages;
1671 if conf.preload && state.currently = Idle
1672 then load (preloadlayout state.y);
1675 let layoutready layout =
1676 let rec fold all ls =
1677 all && match ls with
1678 | l :: rest ->
1679 let seen = ref false in
1680 let allvisible = ref true in
1681 let foo col row _ _ _ _ _ _ =
1682 seen := true;
1683 allvisible := !allvisible &&
1684 begin match gettileopaque l col row with
1685 | Some _ -> true
1686 | None -> false
1689 itertiles l foo;
1690 fold (!seen && !allvisible) rest
1691 | [] -> true
1693 let alltilesvisible = fold true layout in
1694 alltilesvisible;
1697 let gotoy y =
1698 let y = bound y 0 state.maxy in
1699 let y, layout, proceed =
1700 match conf.maxwait with
1701 | Some time when state.ghyll == noghyll ->
1702 begin match state.throttle with
1703 | None ->
1704 let layout = layout y state.winh in
1705 let ready = layoutready layout in
1706 if not ready
1707 then (
1708 load layout;
1709 state.throttle <- Some (layout, y, now ());
1711 else G.postRedisplay "gotoy showall (None)";
1712 y, layout, ready
1713 | Some (_, _, started) ->
1714 let dt = now () -. started in
1715 if dt > time
1716 then (
1717 state.throttle <- None;
1718 let layout = layout y state.winh in
1719 load layout;
1720 G.postRedisplay "maxwait";
1721 y, layout, true
1723 else -1, [], false
1726 | _ ->
1727 let layout = layout y state.winh in
1728 if not !wtmode || layoutready layout
1729 then G.postRedisplay "gotoy ready";
1730 y, layout, true
1732 if proceed
1733 then (
1734 state.y <- y;
1735 state.layout <- layout;
1736 begin match state.mode with
1737 | LinkNav (Ltexact (pageno, linkno)) ->
1738 let rec loop = function
1739 | [] ->
1740 state.mode <- LinkNav (Ltgendir 0)
1741 | l :: _ when l.pageno = pageno ->
1742 begin match getopaque pageno with
1743 | None ->
1744 state.mode <- LinkNav (Ltgendir 0)
1745 | Some opaque ->
1746 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1747 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1748 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1749 then state.mode <- LinkNav (Ltgendir 0)
1751 | _ :: rest -> loop rest
1753 loop layout
1754 | _ -> ()
1755 end;
1756 begin match state.mode with
1757 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1758 if not (pagevisible layout pageno)
1759 then (
1760 match state.layout with
1761 | [] -> ()
1762 | l :: _ ->
1763 state.mode <- Birdseye (
1764 conf, leftx, l.pageno, hooverpageno, anchor
1767 | LinkNav (Ltgendir dir as lt) ->
1768 let linknav =
1769 let rec loop = function
1770 | [] -> lt
1771 | l :: rest ->
1772 match getopaque l.pageno with
1773 | None -> loop rest
1774 | Some opaque ->
1775 let link =
1776 let ld =
1777 if dir = 0
1778 then LDfirstvisible (l.pagex, l.pagey, dir)
1779 else (
1780 if dir > 0 then LDfirst else LDlast
1783 findlink opaque ld
1785 match link with
1786 | Lnotfound -> loop rest
1787 | Lfound n ->
1788 showlinktype (getlink opaque n);
1789 Ltexact (l.pageno, n)
1791 loop state.layout
1793 state.mode <- LinkNav linknav
1794 | _ -> ()
1795 end;
1796 preload layout;
1798 state.ghyll <- noghyll;
1799 if conf.updatecurs
1800 then (
1801 let mx, my = state.mpos in
1802 updateunder mx my;
1806 let conttiling pageno opaque =
1807 tilepage pageno opaque
1808 (if conf.preload then preloadlayout state.y else state.layout)
1811 let gotoy_and_clear_text y =
1812 if not conf.verbose then state.text <- "";
1813 gotoy y;
1816 let getanchor1 l =
1817 let top =
1818 let coloff = l.pagecol * l.pageh in
1819 float (l.pagey + coloff) /. float l.pageh
1821 let dtop =
1822 if l.pagedispy = 0
1823 then
1825 else
1826 if conf.presentation
1827 then float l.pagedispy /. float (calcips l.pageh)
1828 else float l.pagedispy /. float conf.interpagespace
1830 (l.pageno, top, dtop)
1833 let getanchor () =
1834 match state.layout with
1835 | l :: _ -> getanchor1 l
1836 | [] ->
1837 let n = page_of_y state.y in
1838 if n = -1
1839 then state.anchor
1840 else
1841 let y, h = getpageyh n in
1842 let dy = y - state.y in
1843 let dtop =
1844 if conf.presentation
1845 then
1846 let ips = calcips h in
1847 float (dy + ips) /. float ips
1848 else
1849 float dy /. float conf.interpagespace
1851 (n, 0.0, dtop)
1854 let getanchory (n, top, dtop) =
1855 let y, h = getpageyh n in
1856 if conf.presentation
1857 then
1858 let ips = calcips h in
1859 y + truncate (top*.float h -. dtop*.float ips) + ips;
1860 else
1861 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1864 let gotoanchor anchor =
1865 gotoy (getanchory anchor);
1868 let addnav () =
1869 cbput state.hists.nav (getanchor ());
1872 let getnav dir =
1873 let anchor = cbgetc state.hists.nav dir in
1874 getanchory anchor;
1877 let gotoghyll y =
1878 let scroll f n a b =
1879 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1880 let snake f a b =
1881 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1882 if f < a
1883 then s (float f /. float a)
1884 else (
1885 if f > b
1886 then 1.0 -. s ((float (f-b) /. float (n-b)))
1887 else 1.0
1890 snake f a b
1891 and summa f n a b =
1892 (* courtesy:
1893 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1894 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1895 let iv1 = iv f in
1896 let ins = float a *. iv1
1897 and outs = float (n-b) *. iv1 in
1898 let ones = b - a in
1899 ins +. outs +. float ones
1901 let rec set (_N, _A, _B) y sy =
1902 let sum = summa 1.0 _N _A _B in
1903 let dy = float (y - sy) in
1904 state.ghyll <- (
1905 let rec gf n y1 o =
1906 if n >= _N
1907 then state.ghyll <- noghyll
1908 else
1909 let go n =
1910 let s = scroll n _N _A _B in
1911 let y1 = y1 +. ((s *. dy) /. sum) in
1912 gotoy_and_clear_text (truncate y1);
1913 state.ghyll <- gf (n+1) y1;
1915 match o with
1916 | None -> go n
1917 | Some y' -> set (_N/2, 1, 1) y' state.y
1919 gf 0 (float state.y)
1922 match conf.ghyllscroll with
1923 | None ->
1924 gotoy_and_clear_text y
1925 | Some nab ->
1926 if state.ghyll == noghyll
1927 then set nab y state.y
1928 else state.ghyll (Some y)
1931 let gotopage n top =
1932 let y, h = getpageyh n in
1933 let y = y + (truncate (top *. float h)) in
1934 gotoghyll y
1937 let gotopage1 n top =
1938 let y = getpagey n in
1939 let y = y + top in
1940 gotoghyll y
1943 let invalidate s f =
1944 state.layout <- [];
1945 state.pdims <- [];
1946 state.rects <- [];
1947 state.rects1 <- [];
1948 match state.geomcmds with
1949 | ps, [] when String.length ps = 0 ->
1950 f ();
1951 state.geomcmds <- s, [];
1953 | ps, [] ->
1954 state.geomcmds <- ps, [s, f];
1956 | ps, (s', _) :: rest when s' = s ->
1957 state.geomcmds <- ps, ((s, f) :: rest);
1959 | ps, cmds ->
1960 state.geomcmds <- ps, ((s, f) :: cmds);
1963 let flushpages () =
1964 Hashtbl.iter (fun _ opaque ->
1965 wcmd "freepage %s" opaque;
1966 ) state.pagemap;
1967 Hashtbl.clear state.pagemap;
1970 let flushtiles () =
1971 if not (Queue.is_empty state.tilelru)
1972 then (
1973 Queue.iter (fun (k, p, s) ->
1974 wcmd "freetile %s" p;
1975 state.memused <- state.memused - s;
1976 Hashtbl.remove state.tilemap k;
1977 ) state.tilelru;
1978 state.uioh#infochanged Memused;
1979 Queue.clear state.tilelru;
1981 load state.layout;
1984 let opendoc path password =
1985 state.path <- path;
1986 state.password <- password;
1987 state.gen <- state.gen + 1;
1988 state.docinfo <- [];
1990 flushpages ();
1991 setaalevel conf.aalevel;
1992 let titlepath =
1993 if String.length state.origin = 0
1994 then path
1995 else state.origin
1997 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1998 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1999 invalidate "reqlayout"
2000 (fun () ->
2001 wcmd "reqlayout %d %d %s\000"
2002 conf.angle (int_of_fitmodel conf.fitmodel) state.nameddest;
2006 let reload () =
2007 state.anchor <- getanchor ();
2008 opendoc state.path state.password;
2011 let scalecolor c =
2012 let c = c *. conf.colorscale in
2013 (c, c, c);
2016 let scalecolor2 (r, g, b) =
2017 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2020 let docolumns = function
2021 | Csingle _ ->
2022 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2023 let rec loop pageno pdimno pdim y ph pdims =
2024 if pageno = state.pagecount
2025 then ()
2026 else
2027 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2028 match pdims with
2029 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2030 pdimno+1, pdim, rest
2031 | _ ->
2032 pdimno, pdim, pdims
2034 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2035 let y = y +
2036 (if conf.presentation
2037 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2038 else (if pageno = 0 then 0 else conf.interpagespace)
2041 a.(pageno) <- (pdimno, x, y, pdim);
2042 loop (pageno+1) pdimno pdim (y + h) h pdims
2044 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2045 conf.columns <- Csingle a;
2047 | Cmulti ((columns, coverA, coverB), _) ->
2048 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2049 let rec loop pageno pdimno pdim x y rowh pdims =
2050 let rec fixrow m = if m = pageno then () else
2051 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2052 if h < rowh
2053 then (
2054 let y = y + (rowh - h) / 2 in
2055 a.(m) <- (pdimno, x, y, pdim);
2057 fixrow (m+1)
2059 if pageno = state.pagecount
2060 then fixrow (((pageno - 1) / columns) * columns)
2061 else
2062 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2063 match pdims with
2064 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2065 pdimno+1, pdim, rest
2066 | _ ->
2067 pdimno, pdim, pdims
2069 let x, y, rowh' =
2070 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2071 then (
2072 let x = (state.winw - state.scrollw - w) / 2 in
2073 let ips =
2074 if conf.presentation then calcips h else conf.interpagespace in
2075 x, y + ips + rowh, h
2077 else (
2078 if (pageno - coverA) mod columns = 0
2079 then (
2080 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2081 let y =
2082 if conf.presentation
2083 then
2084 let ips = calcips h in
2085 y + (if pageno = 0 then 0 else calcips rowh + ips)
2086 else
2087 y + (if pageno = 0 then 0 else conf.interpagespace)
2089 x, y + rowh, h
2091 else x, y, max rowh h
2094 let y =
2095 if pageno > 1 && (pageno - coverA) mod columns = 0
2096 then (
2097 let y =
2098 if pageno = columns && conf.presentation
2099 then (
2100 let ips = calcips rowh in
2101 for i = 0 to pred columns
2103 let (pdimno, x, y, pdim) = a.(i) in
2104 a.(i) <- (pdimno, x, y+ips, pdim)
2105 done;
2106 y+ips;
2108 else y
2110 fixrow (pageno - columns);
2113 else y
2115 a.(pageno) <- (pdimno, x, y, pdim);
2116 let x = x + w + xoff*2 + conf.interpagespace in
2117 loop (pageno+1) pdimno pdim x y rowh' pdims
2119 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2120 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2122 | Csplit (c, _) ->
2123 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2124 let rec loop pageno pdimno pdim y pdims =
2125 if pageno = state.pagecount
2126 then ()
2127 else
2128 let pdimno, ((_, w, h, _) as pdim), pdims =
2129 match pdims with
2130 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2131 pdimno+1, pdim, rest
2132 | _ ->
2133 pdimno, pdim, pdims
2135 let cw = w / c in
2136 let rec loop1 n x y =
2137 if n = c then y else (
2138 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2139 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2142 let y = loop1 0 0 y in
2143 loop (pageno+1) pdimno pdim y pdims
2145 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2146 conf.columns <- Csplit (c, a);
2149 let represent () =
2150 docolumns conf.columns;
2151 state.maxy <- calcheight ();
2152 state.hscrollh <-
2153 if state.x = 0 && state.w <= state.winw - state.scrollw
2154 then 0
2155 else state.scrollw
2157 if state.reprf == noreprf
2158 then (
2159 match state.mode with
2160 | Birdseye (_, _, pageno, _, _) ->
2161 let y, h = getpageyh pageno in
2162 let top = (state.winh - h) / 2 in
2163 gotoy (max 0 (y - top))
2164 | _ -> gotoanchor state.anchor
2166 else (
2167 state.reprf ();
2168 state.reprf <- noreprf;
2172 let reshape w h =
2173 GlDraw.viewport 0 0 w h;
2174 let firsttime = state.geomcmds == firstgeomcmds in
2175 if not firsttime && nogeomcmds state.geomcmds
2176 then state.anchor <- getanchor ();
2178 state.winw <- w;
2179 let w = truncate (float w *. conf.zoom) - state.scrollw in
2180 let w = max w 2 in
2181 state.winh <- h;
2182 setfontsize fstate.fontsize;
2183 GlMat.mode `modelview;
2184 GlMat.load_identity ();
2186 GlMat.mode `projection;
2187 GlMat.load_identity ();
2188 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2189 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2190 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2192 let relx =
2193 if conf.zoom <= 1.0
2194 then 0.0
2195 else float state.x /. float state.w
2197 invalidate "geometry"
2198 (fun () ->
2199 state.w <- w;
2200 if not firsttime
2201 then state.x <- truncate (relx *. float w);
2202 let w =
2203 match conf.columns with
2204 | Csingle _ -> w
2205 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2206 | Csplit (c, _) -> w * c
2208 wcmd "geometry %d %d %d"
2209 w ((truncate (float h*.conf.zoom)) - 2*conf.interpagespace)
2210 (int_of_fitmodel conf.fitmodel)
2214 let enttext () =
2215 let len = String.length state.text in
2216 let drawstring s =
2217 let hscrollh =
2218 match state.mode with
2219 | Textentry _
2220 | View ->
2221 let h, _, _ = state.uioh#scrollpw in
2223 | _ -> 0
2225 let rect x w =
2226 GlDraw.rect
2227 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2228 (x+.w, float (state.winh - hscrollh))
2231 let w = float (state.winw - state.scrollw - 1) in
2232 if state.progress >= 0.0 && state.progress < 1.0
2233 then (
2234 GlDraw.color (0.3, 0.3, 0.3);
2235 let w1 = w *. state.progress in
2236 rect 0.0 w1;
2237 GlDraw.color (0.0, 0.0, 0.0);
2238 rect w1 (w-.w1)
2240 else (
2241 GlDraw.color (0.0, 0.0, 0.0);
2242 rect 0.0 w;
2245 GlDraw.color (1.0, 1.0, 1.0);
2246 drawstring fstate.fontsize
2247 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2249 let s =
2250 match state.mode with
2251 | Textentry ((prefix, text, _, _, _, _), _) ->
2252 let s =
2253 if len > 0
2254 then
2255 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2256 else
2257 Printf.sprintf "%s%s_" prefix text
2261 | _ -> state.text
2263 let s =
2264 if state.newerrmsgs
2265 then (
2266 if not (istextentry state.mode) && state.uioh#eformsgs
2267 then
2268 let s1 = "(press 'e' to review error messasges)" in
2269 if String.length s > 0 then s ^ " " ^ s1 else s1
2270 else s
2272 else s
2274 if String.length s > 0
2275 then drawstring s
2278 let gctiles () =
2279 let len = Queue.length state.tilelru in
2280 let layout = lazy (
2281 match state.throttle with
2282 | None ->
2283 if conf.preload
2284 then preloadlayout state.y
2285 else state.layout
2286 | Some (layout, _, _) ->
2287 layout
2288 ) in
2289 let rec loop qpos =
2290 if state.memused <= conf.memlimit
2291 then ()
2292 else (
2293 if qpos < len
2294 then
2295 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2296 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2297 let (_, pw, ph, _) = getpagedim n in
2299 gen = state.gen
2300 && colorspace = conf.colorspace
2301 && angle = conf.angle
2302 && pagew = pw
2303 && pageh = ph
2304 && (
2305 let x = col*conf.tilew
2306 and y = row*conf.tileh in
2307 tilevisible (Lazy.force_val layout) n x y
2309 then Queue.push lruitem state.tilelru
2310 else (
2311 freepbo p;
2312 wcmd "freetile %s" p;
2313 state.memused <- state.memused - s;
2314 state.uioh#infochanged Memused;
2315 Hashtbl.remove state.tilemap k;
2317 loop (qpos+1)
2320 loop 0
2323 let logcurrently = function
2324 | Idle -> dolog "Idle"
2325 | Loading (l, gen) ->
2326 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2327 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2328 dolog
2329 "Tiling %d[%d,%d] page=%s cs=%s angle"
2330 l.pageno col row pageopaque
2331 (colorspace_to_string colorspace)
2333 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2334 angle gen conf.angle state.gen
2335 tilew tileh
2336 conf.tilew conf.tileh
2338 | Outlining _ ->
2339 dolog "outlining"
2342 let splitatspace =
2343 let r = Str.regexp " " in
2344 fun s -> Str.bounded_split r s 2;
2347 let onpagerect pageno f =
2348 let b =
2349 match conf.columns with
2350 | Cmulti (_, b) -> b
2351 | Csingle b -> b
2352 | Csplit (_, b) -> b
2354 if pageno >= 0 && pageno < Array.length b
2355 then
2356 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2357 let r = getpdimrect pdimno in
2358 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2361 let gotopagexy1 pageno x y =
2362 onpagerect pageno (fun w h ->
2363 let top = y /. h in
2364 let _,w1,_,leftx = getpagedim pageno in
2365 let wh = state.winh - state.hscrollh in
2366 let sw = float w1 /. w in
2367 let x = sw *. x in
2368 let x = leftx + state.x + truncate x in
2369 let sx =
2370 if x < 0 || x >= state.winw - state.scrollw
2371 then state.x - x
2372 else state.x
2374 let py, h = getpageyh pageno in
2375 let pdy = truncate (top *. float h) in
2376 let y' = py + pdy in
2377 let dy = y' - state.y in
2378 let sy =
2379 if x != state.x || not (dy > 0 && dy < wh)
2380 then (
2381 if conf.presentation
2382 then
2383 if abs (py - y') > wh
2384 then y'
2385 else py
2386 else y';
2388 else state.y
2390 if state.x != sx || state.y != sy
2391 then (
2392 let x, y =
2393 if !wtmode
2394 then (
2395 let ww = state.winw - state.scrollw in
2396 let qx = sx / ww
2397 and qy = pdy / wh in
2398 let x = qx * ww
2399 and y = py + qy * wh in
2400 let x = if -x + ww > w1 then -(w1-ww) else x
2401 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2402 let y =
2403 if conf.presentation
2404 then
2405 if abs (py - y') > wh
2406 then y'
2407 else py
2408 else y';
2410 (x, y)
2412 else (sx, sy)
2414 state.x <- x;
2415 state.hscrollh <-
2416 if x = 0 && state.w <= state.winw - state.scrollw
2417 then 0
2418 else state.scrollw
2420 gotoy_and_clear_text y;
2422 else gotoy_and_clear_text state.y;
2426 let gotopagexy pageno x y =
2427 match state.mode with
2428 | Birdseye _ -> gotopage pageno 0.0
2429 | _ -> gotopagexy1 pageno x y
2432 let act cmds =
2433 (* dolog "%S" cmds; *)
2434 let cl = splitatspace cmds in
2435 let scan s fmt f =
2436 try Scanf.sscanf s fmt f
2437 with exn ->
2438 dolog "error processing '%S': %s" cmds (exntos exn);
2439 exit 1
2441 match cl with
2442 | "clear" :: [] ->
2443 state.uioh#infochanged Pdim;
2444 state.pdims <- [];
2446 | "clearrects" :: [] ->
2447 state.rects <- state.rects1;
2448 G.postRedisplay "clearrects";
2450 | "continue" :: args :: [] ->
2451 let n = scan args "%u" (fun n -> n) in
2452 state.pagecount <- n;
2453 begin match state.currently with
2454 | Outlining l ->
2455 state.currently <- Idle;
2456 state.outlines <- Array.of_list (List.rev l)
2457 | _ -> ()
2458 end;
2460 let cur, cmds = state.geomcmds in
2461 if String.length cur = 0
2462 then failwith "umpossible";
2464 begin match List.rev cmds with
2465 | [] ->
2466 state.geomcmds <- "", [];
2467 represent ();
2468 | (s, f) :: rest ->
2469 f ();
2470 state.geomcmds <- s, List.rev rest;
2471 end;
2472 if conf.maxwait = None && not !wtmode
2473 then G.postRedisplay "continue";
2475 | "title" :: args :: [] ->
2476 Wsi.settitle args
2478 | "msg" :: args :: [] ->
2479 showtext ' ' args
2481 | "vmsg" :: args :: [] ->
2482 if conf.verbose
2483 then showtext ' ' args
2485 | "emsg" :: args :: [] ->
2486 Buffer.add_string state.errmsgs args;
2487 state.newerrmsgs <- true;
2488 G.postRedisplay "error message"
2490 | "progress" :: args :: [] ->
2491 let progress, text =
2492 scan args "%f %n"
2493 (fun f pos ->
2494 f, String.sub args pos (String.length args - pos))
2496 state.text <- text;
2497 state.progress <- progress;
2498 G.postRedisplay "progress"
2500 | "firstmatch" :: args :: [] ->
2501 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2502 scan args "%u %d %f %f %f %f %f %f %f %f"
2503 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2504 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2506 let y = (getpagey pageno) + truncate y0 in
2507 addnav ();
2508 gotoy y;
2509 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2511 | "match" :: args :: [] ->
2512 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2513 scan args "%u %d %f %f %f %f %f %f %f %f"
2514 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2515 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2517 state.rects1 <-
2518 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2520 | "page" :: args :: [] ->
2521 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2522 begin match state.currently with
2523 | Loading (l, gen) ->
2524 vlog "page %d took %f sec" l.pageno t;
2525 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2526 begin match state.throttle with
2527 | None ->
2528 let preloadedpages =
2529 if conf.preload
2530 then preloadlayout state.y
2531 else state.layout
2533 let evict () =
2534 let set =
2535 List.fold_left (fun s l -> IntSet.add l.pageno s)
2536 IntSet.empty preloadedpages
2538 let evictedpages =
2539 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2540 if not (IntSet.mem pageno set)
2541 then (
2542 wcmd "freepage %s" opaque;
2543 key :: accu
2545 else accu
2546 ) state.pagemap []
2548 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2550 evict ();
2551 state.currently <- Idle;
2552 if gen = state.gen
2553 then (
2554 tilepage l.pageno pageopaque state.layout;
2555 load state.layout;
2556 load preloadedpages;
2557 if pagevisible state.layout l.pageno
2558 && layoutready state.layout
2559 then G.postRedisplay "page";
2562 | Some (layout, _, _) ->
2563 state.currently <- Idle;
2564 tilepage l.pageno pageopaque layout;
2565 load state.layout
2566 end;
2568 | _ ->
2569 dolog "Inconsistent loading state";
2570 logcurrently state.currently;
2571 exit 1
2574 | "tile" :: args :: [] ->
2575 let (x, y, opaque, size, t) =
2576 scan args "%u %u %s %u %f"
2577 (fun x y p size t -> (x, y, p, size, t))
2579 begin match state.currently with
2580 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2581 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2583 unmappbo opaque;
2584 if tilew != conf.tilew || tileh != conf.tileh
2585 then (
2586 wcmd "freetile %s" opaque;
2587 state.currently <- Idle;
2588 load state.layout;
2590 else (
2591 puttileopaque l col row gen cs angle opaque size t;
2592 state.memused <- state.memused + size;
2593 state.uioh#infochanged Memused;
2594 gctiles ();
2595 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2596 opaque, size) state.tilelru;
2598 let layout =
2599 match state.throttle with
2600 | None -> state.layout
2601 | Some (layout, _, _) -> layout
2604 state.currently <- Idle;
2605 if gen = state.gen
2606 && conf.colorspace = cs
2607 && conf.angle = angle
2608 && tilevisible layout l.pageno x y
2609 then conttiling l.pageno pageopaque;
2611 begin match state.throttle with
2612 | None ->
2613 preload state.layout;
2614 if gen = state.gen
2615 && conf.colorspace = cs
2616 && conf.angle = angle
2617 && tilevisible state.layout l.pageno x y
2618 && (not !wtmode || layoutready state.layout)
2619 then G.postRedisplay "tile nothrottle";
2621 | Some (layout, y, _) ->
2622 let ready = layoutready layout in
2623 if ready
2624 then (
2625 state.y <- y;
2626 state.layout <- layout;
2627 state.throttle <- None;
2628 G.postRedisplay "throttle";
2630 else load layout;
2631 end;
2634 | _ ->
2635 dolog "Inconsistent tiling state";
2636 logcurrently state.currently;
2637 exit 1
2640 | "pdim" :: args :: [] ->
2641 let (n, w, h, _) as pdim =
2642 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2644 let pdim =
2645 match conf.fitmodel, conf.columns with
2646 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2647 | _ -> pdim
2649 state.uioh#infochanged Pdim;
2650 state.pdims <- pdim :: state.pdims
2652 | "o" :: args :: [] ->
2653 let (l, n, t, h, pos) =
2654 scan args "%u %u %d %u %n"
2655 (fun l n t h pos -> l, n, t, h, pos)
2657 let s = String.sub args pos (String.length args - pos) in
2658 let outline = (s, l, (n, float t /. float h, 0.0)) in
2659 begin match state.currently with
2660 | Outlining outlines ->
2661 state.currently <- Outlining (outline :: outlines)
2662 | Idle ->
2663 state.currently <- Outlining [outline]
2664 | currently ->
2665 dolog "invalid outlining state";
2666 logcurrently currently
2669 | "a" :: args :: [] ->
2670 let (n, l, t) =
2671 scan args "%u %d %d" (fun n l t -> n, l, t)
2673 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2675 | "info" :: args :: [] ->
2676 state.docinfo <- (1, args) :: state.docinfo
2678 | "infoend" :: [] ->
2679 state.uioh#infochanged Docinfo;
2680 state.docinfo <- List.rev state.docinfo
2682 | _ ->
2683 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2686 let onhist cb =
2687 let rc = cb.rc in
2688 let action = function
2689 | HCprev -> cbget cb ~-1
2690 | HCnext -> cbget cb 1
2691 | HCfirst -> cbget cb ~-(cb.rc)
2692 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2693 and cancel () = cb.rc <- rc
2694 in (action, cancel)
2697 let search pattern forward =
2698 match conf.columns with
2699 | Csplit _ ->
2700 showtext '!' "searching does not work properly in split columns mode"
2701 | _ ->
2702 if String.length pattern > 0
2703 then
2704 let pn, py =
2705 match state.layout with
2706 | [] -> 0, 0
2707 | l :: _ ->
2708 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2710 wcmd "search %d %d %d %d,%s\000"
2711 (btod conf.icase) pn py (btod forward) pattern;
2714 let intentry text key =
2715 let c =
2716 if key >= 32 && key < 127
2717 then Char.chr key
2718 else '\000'
2720 match c with
2721 | '0' .. '9' ->
2722 let text = addchar text c in
2723 TEcont text
2725 | _ ->
2726 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2727 TEcont text
2730 let linknentry text key =
2731 let c =
2732 if key >= 32 && key < 127
2733 then Char.chr key
2734 else '\000'
2736 match c with
2737 | 'a' .. 'z' ->
2738 let text = addchar text c in
2739 TEcont text
2741 | _ ->
2742 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2743 TEcont text
2746 let linkndone f s =
2747 if String.length s > 0
2748 then (
2749 let n =
2750 let l = String.length s in
2751 let rec loop pos n = if pos = l then n else
2752 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2753 loop (pos+1) (n*26 + m)
2754 in loop 0 0
2756 let rec loop n = function
2757 | [] -> ()
2758 | l :: rest ->
2759 match getopaque l.pageno with
2760 | None -> loop n rest
2761 | Some opaque ->
2762 let m = getlinkcount opaque in
2763 if n < m
2764 then (
2765 let under = getlink opaque n in
2766 f under
2768 else loop (n-m) rest
2770 loop n state.layout;
2774 let textentry text key =
2775 if key land 0xff00 = 0xff00
2776 then TEcont text
2777 else TEcont (text ^ toutf8 key)
2780 let reqlayout angle fitmodel =
2781 match state.throttle with
2782 | None ->
2783 if nogeomcmds state.geomcmds
2784 then state.anchor <- getanchor ();
2785 conf.angle <- angle mod 360;
2786 if conf.angle != 0
2787 then (
2788 match state.mode with
2789 | LinkNav _ -> state.mode <- View
2790 | _ -> ()
2792 conf.fitmodel <- fitmodel;
2793 invalidate "reqlayout"
2794 (fun () ->
2795 wcmd "reqlayout %d %d" conf.angle (int_of_fitmodel conf.fitmodel)
2797 | _ -> ()
2800 let settrim trimmargins trimfuzz =
2801 if nogeomcmds state.geomcmds
2802 then state.anchor <- getanchor ();
2803 conf.trimmargins <- trimmargins;
2804 conf.trimfuzz <- trimfuzz;
2805 let x0, y0, x1, y1 = trimfuzz in
2806 invalidate "settrim"
2807 (fun () ->
2808 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2809 flushpages ();
2812 let setzoom zoom =
2813 match state.throttle with
2814 | None ->
2815 let zoom = max 0.0001 zoom in
2816 if zoom <> conf.zoom
2817 then (
2818 state.prevzoom <- conf.zoom;
2819 conf.zoom <- zoom;
2820 reshape state.winw state.winh;
2821 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2824 | Some (layout, y, started) ->
2825 let time =
2826 match conf.maxwait with
2827 | None -> 0.0
2828 | Some t -> t
2830 let dt = now () -. started in
2831 if dt > time
2832 then (
2833 state.y <- y;
2834 load layout;
2838 let setcolumns mode columns coverA coverB =
2839 state.prevcolumns <- Some (conf.columns, conf.zoom);
2840 if columns < 0
2841 then (
2842 if isbirdseye mode
2843 then showtext '!' "split mode doesn't work in bird's eye"
2844 else (
2845 conf.columns <- Csplit (-columns, [||]);
2846 state.x <- 0;
2847 conf.zoom <- 1.0;
2850 else (
2851 if columns < 2
2852 then (
2853 conf.columns <- Csingle [||];
2854 state.x <- 0;
2855 setzoom 1.0;
2857 else (
2858 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2859 conf.zoom <- 1.0;
2862 reshape state.winw state.winh;
2865 let enterbirdseye () =
2866 let zoom = float conf.thumbw /. float state.winw in
2867 let birdseyepageno =
2868 let cy = state.winh / 2 in
2869 let fold = function
2870 | [] -> 0
2871 | l :: rest ->
2872 let rec fold best = function
2873 | [] -> best.pageno
2874 | l :: rest ->
2875 let d = cy - (l.pagedispy + l.pagevh/2)
2876 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2877 if abs d < abs dbest
2878 then fold l rest
2879 else best.pageno
2880 in fold l rest
2882 fold state.layout
2884 state.mode <- Birdseye (
2885 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2887 conf.zoom <- zoom;
2888 conf.presentation <- false;
2889 conf.interpagespace <- 10;
2890 conf.hlinks <- false;
2891 conf.fitmodel <- FitProportional;
2892 state.x <- 0;
2893 state.mstate <- Mnone;
2894 conf.maxwait <- None;
2895 conf.columns <- (
2896 match conf.beyecolumns with
2897 | Some c ->
2898 conf.zoom <- 1.0;
2899 Cmulti ((c, 0, 0), [||])
2900 | None -> Csingle [||]
2902 Wsi.setcursor Wsi.CURSOR_INHERIT;
2903 if conf.verbose
2904 then
2905 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2906 (100.0*.zoom)
2907 else
2908 state.text <- ""
2910 reshape state.winw state.winh;
2913 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2914 state.mode <- View;
2915 conf.zoom <- c.zoom;
2916 conf.presentation <- c.presentation;
2917 conf.interpagespace <- c.interpagespace;
2918 conf.maxwait <- c.maxwait;
2919 conf.hlinks <- c.hlinks;
2920 conf.fitmodel <- c.fitmodel;
2921 conf.beyecolumns <- (
2922 match conf.columns with
2923 | Cmulti ((c, _, _), _) -> Some c
2924 | Csingle _ -> None
2925 | Csplit _ -> failwith "leaving bird's eye split mode"
2927 conf.columns <- (
2928 match c.columns with
2929 | Cmulti (c, _) -> Cmulti (c, [||])
2930 | Csingle _ -> Csingle [||]
2931 | Csplit (c, _) -> Csplit (c, [||])
2933 state.x <- leftx;
2934 if conf.verbose
2935 then
2936 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2937 (100.0*.conf.zoom)
2939 reshape state.winw state.winh;
2940 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2943 let togglebirdseye () =
2944 match state.mode with
2945 | Birdseye vals -> leavebirdseye vals true
2946 | View -> enterbirdseye ()
2947 | _ -> ()
2950 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2951 let pageno = max 0 (pageno - incr) in
2952 let rec loop = function
2953 | [] -> gotopage1 pageno 0
2954 | l :: _ when l.pageno = pageno ->
2955 if l.pagedispy >= 0 && l.pagey = 0
2956 then G.postRedisplay "upbirdseye"
2957 else gotopage1 pageno 0
2958 | _ :: rest -> loop rest
2960 loop state.layout;
2961 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2964 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2965 let pageno = min (state.pagecount - 1) (pageno + incr) in
2966 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2967 let rec loop = function
2968 | [] ->
2969 let y, h = getpageyh pageno in
2970 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2971 gotoy (clamp dy)
2972 | l :: _ when l.pageno = pageno ->
2973 if l.pagevh != l.pageh
2974 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2975 else G.postRedisplay "downbirdseye"
2976 | _ :: rest -> loop rest
2978 loop state.layout
2981 let optentry mode _ key =
2982 let btos b = if b then "on" else "off" in
2983 if key >= 32 && key < 127
2984 then
2985 let c = Char.chr key in
2986 match c with
2987 | 's' ->
2988 let ondone s =
2989 try conf.scrollstep <- int_of_string s with exc ->
2990 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2992 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2994 | 'A' ->
2995 let ondone s =
2997 conf.autoscrollstep <- int_of_string s;
2998 if state.autoscroll <> None
2999 then state.autoscroll <- Some conf.autoscrollstep
3000 with exc ->
3001 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3003 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3005 | 'C' ->
3006 let ondone s =
3008 let n, a, b = multicolumns_of_string s in
3009 setcolumns mode n a b;
3010 with exc ->
3011 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3013 TEswitch ("columns: ", "", None, textentry, ondone, true)
3015 | 'Z' ->
3016 let ondone s =
3018 let zoom = float (int_of_string s) /. 100.0 in
3019 setzoom zoom
3020 with exc ->
3021 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3023 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3025 | 't' ->
3026 let ondone s =
3028 conf.thumbw <- bound (int_of_string s) 2 4096;
3029 state.text <-
3030 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3031 begin match mode with
3032 | Birdseye beye ->
3033 leavebirdseye beye false;
3034 enterbirdseye ();
3035 | _ -> ();
3037 with exc ->
3038 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3040 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3042 | 'R' ->
3043 let ondone s =
3044 match try
3045 Some (int_of_string s)
3046 with exc ->
3047 state.text <- Printf.sprintf "bad integer `%s': %s"
3048 s (exntos exc);
3049 None
3050 with
3051 | Some angle -> reqlayout angle conf.fitmodel
3052 | None -> ()
3054 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3056 | 'i' ->
3057 conf.icase <- not conf.icase;
3058 TEdone ("case insensitive search " ^ (btos conf.icase))
3060 | 'p' ->
3061 conf.preload <- not conf.preload;
3062 gotoy state.y;
3063 TEdone ("preload " ^ (btos conf.preload))
3065 | 'v' ->
3066 conf.verbose <- not conf.verbose;
3067 TEdone ("verbose " ^ (btos conf.verbose))
3069 | 'd' ->
3070 conf.debug <- not conf.debug;
3071 TEdone ("debug " ^ (btos conf.debug))
3073 | 'h' ->
3074 conf.maxhfit <- not conf.maxhfit;
3075 state.maxy <- calcheight ();
3076 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3078 | 'c' ->
3079 conf.crophack <- not conf.crophack;
3080 TEdone ("crophack " ^ btos conf.crophack)
3082 | 'a' ->
3083 let s =
3084 match conf.maxwait with
3085 | None ->
3086 conf.maxwait <- Some infinity;
3087 "always wait for page to complete"
3088 | Some _ ->
3089 conf.maxwait <- None;
3090 "show placeholder if page is not ready"
3092 TEdone s
3094 | 'f' ->
3095 conf.underinfo <- not conf.underinfo;
3096 TEdone ("underinfo " ^ btos conf.underinfo)
3098 | 'P' ->
3099 conf.savebmarks <- not conf.savebmarks;
3100 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3102 | 'S' ->
3103 let ondone s =
3105 let pageno, py =
3106 match state.layout with
3107 | [] -> 0, 0
3108 | l :: _ ->
3109 l.pageno, l.pagey
3111 conf.interpagespace <- int_of_string s;
3112 docolumns conf.columns;
3113 state.maxy <- calcheight ();
3114 let y = getpagey pageno in
3115 gotoy (y + py)
3116 with exc ->
3117 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3119 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3121 | 'l' ->
3122 let fm =
3123 match conf.fitmodel with
3124 | FitProportional -> FitWidth
3125 | _ -> FitProportional
3127 reqlayout conf.angle fm;
3128 TEdone ("proportional display " ^ btos (fm == FitProportional))
3130 | 'T' ->
3131 settrim (not conf.trimmargins) conf.trimfuzz;
3132 TEdone ("trim margins " ^ btos conf.trimmargins)
3134 | 'I' ->
3135 conf.invert <- not conf.invert;
3136 TEdone ("invert colors " ^ btos conf.invert)
3138 | 'x' ->
3139 let ondone s =
3140 cbput state.hists.sel s;
3141 conf.selcmd <- s;
3143 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3144 textentry, ondone, true)
3146 | _ ->
3147 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3148 TEstop
3149 else
3150 TEcont state.text
3153 class type lvsource = object
3154 method getitemcount : int
3155 method getitem : int -> (string * int)
3156 method hasaction : int -> bool
3157 method exit :
3158 uioh:uioh ->
3159 cancel:bool ->
3160 active:int ->
3161 first:int ->
3162 pan:int ->
3163 qsearch:string ->
3164 uioh option
3165 method getactive : int
3166 method getfirst : int
3167 method getqsearch : string
3168 method setqsearch : string -> unit
3169 method getpan : int
3170 end;;
3172 class virtual lvsourcebase = object
3173 val mutable m_active = 0
3174 val mutable m_first = 0
3175 val mutable m_qsearch = ""
3176 val mutable m_pan = 0
3177 method getactive = m_active
3178 method getfirst = m_first
3179 method getqsearch = m_qsearch
3180 method getpan = m_pan
3181 method setqsearch s = m_qsearch <- s
3182 end;;
3184 let withoutlastutf8 s =
3185 let len = String.length s in
3186 if len = 0
3187 then s
3188 else
3189 let rec find pos =
3190 if pos = 0
3191 then pos
3192 else
3193 let b = Char.code s.[pos] in
3194 if b land 0b11000000 = 0b11000000
3195 then pos
3196 else find (pos-1)
3198 let first =
3199 if Char.code s.[len-1] land 0x80 = 0
3200 then len-1
3201 else find (len-1)
3203 String.sub s 0 first;
3206 let textentrykeyboard
3207 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3208 let key =
3209 if key >= 0xffb0 && key <= 0xffb9
3210 then key - 0xffb0 + 48 else key
3212 let enttext te =
3213 state.mode <- Textentry (te, onleave);
3214 state.text <- "";
3215 enttext ();
3216 G.postRedisplay "textentrykeyboard enttext";
3218 let histaction cmd =
3219 match opthist with
3220 | None -> ()
3221 | Some (action, _) ->
3222 state.mode <- Textentry (
3223 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3225 G.postRedisplay "textentry histaction"
3227 match key with
3228 | 0xff08 -> (* backspace *)
3229 let s = withoutlastutf8 text in
3230 let len = String.length s in
3231 if cancelonempty && len = 0
3232 then (
3233 onleave Cancel;
3234 G.postRedisplay "textentrykeyboard after cancel";
3236 else (
3237 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3240 | 0xff0d | 0xff8d -> (* (kp) enter *)
3241 ondone text;
3242 onleave Confirm;
3243 G.postRedisplay "textentrykeyboard after confirm"
3245 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3246 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3247 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3248 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3250 | 0xff1b -> (* escape*)
3251 if String.length text = 0
3252 then (
3253 begin match opthist with
3254 | None -> ()
3255 | Some (_, onhistcancel) -> onhistcancel ()
3256 end;
3257 onleave Cancel;
3258 state.text <- "";
3259 G.postRedisplay "textentrykeyboard after cancel2"
3261 else (
3262 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3265 | 0xff9f | 0xffff -> () (* delete *)
3267 | _ when key != 0
3268 && key land 0xff00 != 0xff00 (* keyboard *)
3269 && key land 0xfe00 != 0xfe00 (* xkb *)
3270 && key land 0xfd00 != 0xfd00 (* 3270 *)
3272 begin match onkey text key with
3273 | TEdone text ->
3274 ondone text;
3275 onleave Confirm;
3276 G.postRedisplay "textentrykeyboard after confirm2";
3278 | TEcont text ->
3279 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3281 | TEstop ->
3282 onleave Cancel;
3283 G.postRedisplay "textentrykeyboard after cancel3"
3285 | TEswitch te ->
3286 state.mode <- Textentry (te, onleave);
3287 G.postRedisplay "textentrykeyboard switch";
3288 end;
3290 | _ ->
3291 vlog "unhandled key %s" (Wsi.keyname key)
3294 let firstof first active =
3295 if first > active || abs (first - active) > fstate.maxrows - 1
3296 then max 0 (active - (fstate.maxrows/2))
3297 else first
3300 let calcfirst first active =
3301 if active > first
3302 then
3303 let rows = active - first in
3304 if rows > fstate.maxrows then active - fstate.maxrows else first
3305 else active
3308 let scrollph y maxy =
3309 let sh = float (maxy + state.winh) /. float state.winh in
3310 let sh = float state.winh /. sh in
3311 let sh = max sh (float conf.scrollh) in
3313 let percent = float y /. float maxy in
3314 let position = (float state.winh -. sh) *. percent in
3316 let position =
3317 if position +. sh > float state.winh
3318 then float state.winh -. sh
3319 else position
3321 position, sh;
3324 let coe s = (s :> uioh);;
3326 class listview ~(source:lvsource) ~trusted ~modehash =
3327 object (self)
3328 val m_pan = source#getpan
3329 val m_first = source#getfirst
3330 val m_active = source#getactive
3331 val m_qsearch = source#getqsearch
3332 val m_prev_uioh = state.uioh
3334 method private elemunder y =
3335 let n = y / (fstate.fontsize+1) in
3336 if m_first + n < source#getitemcount
3337 then (
3338 if source#hasaction (m_first + n)
3339 then Some (m_first + n)
3340 else None
3342 else None
3344 method display =
3345 Gl.enable `blend;
3346 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3347 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3348 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3349 GlDraw.color (1., 1., 1.);
3350 Gl.enable `texture_2d;
3351 let fs = fstate.fontsize in
3352 let nfs = fs + 1 in
3353 let ww = fstate.wwidth in
3354 let tabw = 30.0*.ww in
3355 let itemcount = source#getitemcount in
3356 let rec loop row =
3357 if (row - m_first) > fstate.maxrows
3358 then ()
3359 else (
3360 if row >= 0 && row < itemcount
3361 then (
3362 let (s, level) = source#getitem row in
3363 let y = (row - m_first) * nfs in
3364 let x = 5.0 +. float (level + m_pan) *. ww in
3365 if row = m_active
3366 then (
3367 Gl.disable `texture_2d;
3368 GlDraw.polygon_mode `both `line;
3369 let alpha = if source#hasaction row then 0.9 else 0.3 in
3370 GlDraw.color (1., 1., 1.) ~alpha;
3371 GlDraw.rect (1., float (y + 1))
3372 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3373 GlDraw.polygon_mode `both `fill;
3374 GlDraw.color (1., 1., 1.);
3375 Gl.enable `texture_2d;
3378 let drawtabularstring s =
3379 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3380 if trusted
3381 then
3382 let tabpos = try String.index s '\t' with Not_found -> -1 in
3383 if tabpos > 0
3384 then
3385 let len = String.length s - tabpos - 1 in
3386 let s1 = String.sub s 0 tabpos
3387 and s2 = String.sub s (tabpos + 1) len in
3388 let nx = drawstr x s1 in
3389 let sw = nx -. x in
3390 let x = x +. (max tabw sw) in
3391 drawstr x s2
3392 else
3393 drawstr x s
3394 else
3395 drawstr x s
3397 let _ = drawtabularstring s in
3398 loop (row+1)
3402 loop m_first;
3403 Gl.disable `blend;
3404 Gl.disable `texture_2d;
3406 method updownlevel incr =
3407 let len = source#getitemcount in
3408 let curlevel =
3409 if m_active >= 0 && m_active < len
3410 then snd (source#getitem m_active)
3411 else -1
3413 let rec flow i =
3414 if i = len then i-1 else if i = -1 then 0 else
3415 let _, l = source#getitem i in
3416 if l != curlevel then i else flow (i+incr)
3418 let active = flow m_active in
3419 let first = calcfirst m_first active in
3420 G.postRedisplay "outline updownlevel";
3421 {< m_active = active; m_first = first >}
3423 method private key1 key mask =
3424 let set1 active first qsearch =
3425 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3427 let search active pattern incr =
3428 let active = if active = -1 then m_first else active in
3429 let dosearch re =
3430 let rec loop n =
3431 if n >= 0 && n < source#getitemcount
3432 then (
3433 let s, _ = source#getitem n in
3435 (try ignore (Str.search_forward re s 0); true
3436 with Not_found -> false)
3437 then Some n
3438 else loop (n + incr)
3440 else None
3442 loop active
3445 let re = Str.regexp_case_fold pattern in
3446 dosearch re
3447 with Failure s ->
3448 state.text <- s;
3449 None
3451 let itemcount = source#getitemcount in
3452 let find start incr =
3453 let rec find i =
3454 if i = -1 || i = itemcount
3455 then -1
3456 else (
3457 if source#hasaction i
3458 then i
3459 else find (i + incr)
3462 find start
3464 let set active first =
3465 let first = bound first 0 (itemcount - fstate.maxrows) in
3466 state.text <- "";
3467 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3469 let navigate incr =
3470 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3471 let active, first =
3472 let incr1 = if incr > 0 then 1 else -1 in
3473 if isvisible m_first m_active
3474 then
3475 let next =
3476 let next = m_active + incr in
3477 let next =
3478 if next < 0 || next >= itemcount
3479 then -1
3480 else find next incr1
3482 if abs (m_active - next) > fstate.maxrows
3483 then -1
3484 else next
3486 if next = -1
3487 then
3488 let first = m_first + incr in
3489 let first = bound first 0 (itemcount - 1) in
3490 let next =
3491 let next = m_active + incr in
3492 let next = bound next 0 (itemcount - 1) in
3493 find next ~-incr1
3495 let active =
3496 if next = -1
3497 then m_active
3498 else (
3499 if isvisible first next
3500 then next
3501 else m_active
3504 active, first
3505 else
3506 let first = min next m_first in
3507 let first =
3508 if abs (next - first) > fstate.maxrows
3509 then first + incr
3510 else first
3512 next, first
3513 else
3514 let first = m_first + incr in
3515 let first = bound first 0 (itemcount - 1) in
3516 let active =
3517 let next = m_active + incr in
3518 let next = bound next 0 (itemcount - 1) in
3519 let next = find next incr1 in
3520 let active =
3521 if next = -1 || abs (m_active - first) > fstate.maxrows
3522 then (
3523 let active = if m_active = -1 then next else m_active in
3524 active
3526 else next
3528 if isvisible first active
3529 then active
3530 else -1
3532 active, first
3534 G.postRedisplay "listview navigate";
3535 set active first;
3537 match key with
3538 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3539 let incr = if key = 0x72 then -1 else 1 in
3540 let active, first =
3541 match search (m_active + incr) m_qsearch incr with
3542 | None ->
3543 state.text <- m_qsearch ^ " [not found]";
3544 m_active, m_first
3545 | Some active ->
3546 state.text <- m_qsearch;
3547 active, firstof m_first active
3549 G.postRedisplay "listview ctrl-r/s";
3550 set1 active first m_qsearch;
3552 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3553 if m_active >= 0 && m_active < source#getitemcount
3554 then (
3555 let s, _ = source#getitem m_active in
3556 selstring s;
3558 coe self
3560 | 0xff08 -> (* backspace *)
3561 if String.length m_qsearch = 0
3562 then coe self
3563 else (
3564 let qsearch = withoutlastutf8 m_qsearch in
3565 let len = String.length qsearch in
3566 if len = 0
3567 then (
3568 state.text <- "";
3569 G.postRedisplay "listview empty qsearch";
3570 set1 m_active m_first "";
3572 else
3573 let active, first =
3574 match search m_active qsearch ~-1 with
3575 | None ->
3576 state.text <- qsearch ^ " [not found]";
3577 m_active, m_first
3578 | Some active ->
3579 state.text <- qsearch;
3580 active, firstof m_first active
3582 G.postRedisplay "listview backspace qsearch";
3583 set1 active first qsearch
3586 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3587 let pattern = m_qsearch ^ toutf8 key in
3588 let active, first =
3589 match search m_active pattern 1 with
3590 | None ->
3591 state.text <- pattern ^ " [not found]";
3592 m_active, m_first
3593 | Some active ->
3594 state.text <- pattern;
3595 active, firstof m_first active
3597 G.postRedisplay "listview qsearch add";
3598 set1 active first pattern;
3600 | 0xff1b -> (* escape *)
3601 state.text <- "";
3602 if String.length m_qsearch = 0
3603 then (
3604 G.postRedisplay "list view escape";
3605 begin
3606 match
3607 source#exit (coe self) true m_active m_first m_pan m_qsearch
3608 with
3609 | None -> m_prev_uioh
3610 | Some uioh -> uioh
3613 else (
3614 G.postRedisplay "list view kill qsearch";
3615 source#setqsearch "";
3616 coe {< m_qsearch = "" >}
3619 | 0xff0d | 0xff8d -> (* (kp) enter *)
3620 state.text <- "";
3621 let self = {< m_qsearch = "" >} in
3622 source#setqsearch "";
3623 let opt =
3624 G.postRedisplay "listview enter";
3625 if m_active >= 0 && m_active < source#getitemcount
3626 then (
3627 source#exit (coe self) false m_active m_first m_pan "";
3629 else (
3630 source#exit (coe self) true m_active m_first m_pan "";
3633 begin match opt with
3634 | None -> m_prev_uioh
3635 | Some uioh -> uioh
3638 | 0xff9f | 0xffff -> (* (kp) delete *)
3639 coe self
3641 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3642 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3643 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3644 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3646 | 0xff53 | 0xff98 -> (* (kp) right *)
3647 state.text <- "";
3648 G.postRedisplay "listview right";
3649 coe {< m_pan = m_pan - 1 >}
3651 | 0xff51 | 0xff96 -> (* (kp) left *)
3652 state.text <- "";
3653 G.postRedisplay "listview left";
3654 coe {< m_pan = m_pan + 1 >}
3656 | 0xff50 | 0xff95 -> (* (kp) home *)
3657 let active = find 0 1 in
3658 G.postRedisplay "listview home";
3659 set active 0;
3661 | 0xff57 | 0xff9c -> (* (kp) end *)
3662 let first = max 0 (itemcount - fstate.maxrows) in
3663 let active = find (itemcount - 1) ~-1 in
3664 G.postRedisplay "listview end";
3665 set active first;
3667 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3668 coe self
3670 | _ ->
3671 dolog "listview unknown key %#x" key; coe self
3673 method key key mask =
3674 match state.mode with
3675 | Textentry te -> textentrykeyboard key mask te; coe self
3676 | _ -> self#key1 key mask
3678 method button button down x y _ =
3679 let opt =
3680 match button with
3681 | 1 when x > state.winw - conf.scrollbw ->
3682 G.postRedisplay "listview scroll";
3683 if down
3684 then
3685 let _, position, sh = self#scrollph in
3686 if y > truncate position && y < truncate (position +. sh)
3687 then (
3688 state.mstate <- Mscrolly;
3689 Some (coe self)
3691 else
3692 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3693 let first = truncate (s *. float source#getitemcount) in
3694 let first = min source#getitemcount first in
3695 Some (coe {< m_first = first; m_active = first >})
3696 else (
3697 state.mstate <- Mnone;
3698 Some (coe self);
3700 | 1 when not down ->
3701 begin match self#elemunder y with
3702 | Some n ->
3703 G.postRedisplay "listview click";
3704 source#exit
3705 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3706 | _ ->
3707 Some (coe self)
3709 | n when (n == 4 || n == 5) && not down ->
3710 let len = source#getitemcount in
3711 let first =
3712 if n = 5 && m_first + fstate.maxrows >= len
3713 then
3714 m_first
3715 else
3716 let first = m_first + (if n == 4 then -1 else 1) in
3717 bound first 0 (len - 1)
3719 G.postRedisplay "listview wheel";
3720 Some (coe {< m_first = first >})
3721 | n when (n = 6 || n = 7) && not down ->
3722 let inc = m_first + (if n = 7 then -1 else 1) in
3723 G.postRedisplay "listview hwheel";
3724 Some (coe {< m_pan = m_pan + inc >})
3725 | _ ->
3726 Some (coe self)
3728 match opt with
3729 | None -> m_prev_uioh
3730 | Some uioh -> uioh
3732 method motion _ y =
3733 match state.mstate with
3734 | Mscrolly ->
3735 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3736 let first = truncate (s *. float source#getitemcount) in
3737 let first = min source#getitemcount first in
3738 G.postRedisplay "listview motion";
3739 coe {< m_first = first; m_active = first >}
3740 | _ -> coe self
3742 method pmotion x y =
3743 if x < state.winw - conf.scrollbw
3744 then
3745 let n =
3746 match self#elemunder y with
3747 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3748 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3750 let o =
3751 if n != m_active
3752 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3753 else self
3755 coe o
3756 else (
3757 Wsi.setcursor Wsi.CURSOR_INHERIT;
3758 coe self
3761 method infochanged _ = ()
3763 method scrollpw = (0, 0.0, 0.0)
3764 method scrollph =
3765 let nfs = fstate.fontsize + 1 in
3766 let y = m_first * nfs in
3767 let itemcount = source#getitemcount in
3768 let maxi = max 0 (itemcount - fstate.maxrows) in
3769 let maxy = maxi * nfs in
3770 let p, h = scrollph y maxy in
3771 conf.scrollbw, p, h
3773 method modehash = modehash
3774 method eformsgs = false
3775 end;;
3777 class outlinelistview ~source =
3778 object (self)
3779 inherit listview
3780 ~source:(source :> lvsource)
3781 ~trusted:false
3782 ~modehash:(findkeyhash conf "outline")
3783 as super
3785 method key key mask =
3786 let calcfirst first active =
3787 if active > first
3788 then
3789 let rows = active - first in
3790 let maxrows =
3791 if String.length state.text = 0
3792 then fstate.maxrows
3793 else fstate.maxrows - 2
3795 if rows > maxrows then active - maxrows else first
3796 else active
3798 let navigate incr =
3799 let active = m_active + incr in
3800 let active = bound active 0 (source#getitemcount - 1) in
3801 let first = calcfirst m_first active in
3802 G.postRedisplay "outline navigate";
3803 coe {< m_active = active; m_first = first >}
3805 let ctrl = Wsi.withctrl mask in
3806 match key with
3807 | 110 when ctrl -> (* ctrl-n *)
3808 source#narrow m_qsearch;
3809 G.postRedisplay "outline ctrl-n";
3810 coe {< m_first = 0; m_active = 0 >}
3812 | 117 when ctrl -> (* ctrl-u *)
3813 source#denarrow;
3814 G.postRedisplay "outline ctrl-u";
3815 state.text <- "";
3816 coe {< m_first = 0; m_active = 0 >}
3818 | 108 when ctrl -> (* ctrl-l *)
3819 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3820 G.postRedisplay "outline ctrl-l";
3821 coe {< m_first = first >}
3823 | 0xff9f | 0xffff -> (* (kp) delete *)
3824 source#remove m_active;
3825 G.postRedisplay "outline delete";
3826 let active = max 0 (m_active-1) in
3827 coe {< m_first = firstof m_first active;
3828 m_active = active >}
3830 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3831 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3832 | 0xff55 | 0xff9a -> (* (kp) prior *)
3833 navigate ~-(fstate.maxrows)
3834 | 0xff56 | 0xff9b -> (* (kp) next *)
3835 navigate fstate.maxrows
3837 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3838 let o =
3839 if ctrl
3840 then (
3841 G.postRedisplay "outline ctrl right";
3842 {< m_pan = m_pan + 1 >}
3844 else self#updownlevel 1
3846 coe o
3848 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3849 let o =
3850 if ctrl
3851 then (
3852 G.postRedisplay "outline ctrl left";
3853 {< m_pan = m_pan - 1 >}
3855 else self#updownlevel ~-1
3857 coe o
3859 | 0xff50 | 0xff95 -> (* (kp) home *)
3860 G.postRedisplay "outline home";
3861 coe {< m_first = 0; m_active = 0 >}
3863 | 0xff57 | 0xff9c -> (* (kp) end *)
3864 let active = source#getitemcount - 1 in
3865 let first = max 0 (active - fstate.maxrows) in
3866 G.postRedisplay "outline end";
3867 coe {< m_active = active; m_first = first >}
3869 | _ -> super#key key mask
3872 let outlinesource usebookmarks =
3873 let empty = [||] in
3874 (object
3875 inherit lvsourcebase
3876 val mutable m_items = empty
3877 val mutable m_orig_items = empty
3878 val mutable m_prev_items = empty
3879 val mutable m_narrow_pattern = ""
3880 val mutable m_hadremovals = false
3882 method getitemcount =
3883 Array.length m_items + (if m_hadremovals then 1 else 0)
3885 method getitem n =
3886 if n == Array.length m_items && m_hadremovals
3887 then
3888 ("[Confirm removal]", 0)
3889 else
3890 let s, n, _ = m_items.(n) in
3891 (s, n)
3893 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3894 ignore (uioh, first, qsearch);
3895 let confrimremoval = m_hadremovals && active = Array.length m_items in
3896 let items =
3897 if String.length m_narrow_pattern = 0
3898 then m_orig_items
3899 else m_items
3901 if not cancel
3902 then (
3903 if not confrimremoval
3904 then(
3905 let _, _, anchor = m_items.(active) in
3906 gotoghyll (getanchory anchor);
3907 m_items <- items;
3909 else (
3910 state.bookmarks <- Array.to_list m_items;
3911 m_orig_items <- m_items;
3914 else m_items <- items;
3915 m_pan <- pan;
3916 None
3918 method hasaction _ = true
3920 method greetmsg =
3921 if Array.length m_items != Array.length m_orig_items
3922 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3923 else ""
3925 method narrow pattern =
3926 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3927 match reopt with
3928 | None -> ()
3929 | Some re ->
3930 let rec loop accu n =
3931 if n = -1
3932 then (
3933 m_narrow_pattern <- pattern;
3934 m_items <- Array.of_list accu
3936 else
3937 let (s, _, _) as o = m_items.(n) in
3938 let accu =
3939 if (try ignore (Str.search_forward re s 0); true
3940 with Not_found -> false)
3941 then o :: accu
3942 else accu
3944 loop accu (n-1)
3946 loop [] (Array.length m_items - 1)
3948 method denarrow =
3949 m_orig_items <- (
3950 if usebookmarks
3951 then Array.of_list state.bookmarks
3952 else state.outlines
3954 m_items <- m_orig_items
3956 method remove m =
3957 if usebookmarks
3958 then
3959 if m >= 0 && m < Array.length m_items
3960 then (
3961 m_hadremovals <- true;
3962 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3963 let n = if n >= m then n+1 else n in
3964 m_items.(n)
3968 method reset anchor items =
3969 m_hadremovals <- false;
3970 if m_orig_items == empty || m_prev_items != items
3971 then (
3972 m_orig_items <- items;
3973 if String.length m_narrow_pattern = 0
3974 then m_items <- items;
3976 m_prev_items <- items;
3977 let rely = getanchory anchor in
3978 let active =
3979 let rec loop n best bestd =
3980 if n = Array.length m_items
3981 then best
3982 else
3983 let (_, _, anchor) = m_items.(n) in
3984 let orely = getanchory anchor in
3985 let d = abs (orely - rely) in
3986 if d < bestd
3987 then loop (n+1) n d
3988 else loop (n+1) best bestd
3990 loop 0 ~-1 max_int
3992 m_active <- active;
3993 m_first <- firstof m_first active
3994 end)
3997 let enterselector usebookmarks =
3998 let source = outlinesource usebookmarks in
3999 fun errmsg ->
4000 let outlines =
4001 if usebookmarks
4002 then Array.of_list state.bookmarks
4003 else state.outlines
4005 if Array.length outlines = 0
4006 then (
4007 showtext ' ' errmsg;
4009 else (
4010 state.text <- source#greetmsg;
4011 Wsi.setcursor Wsi.CURSOR_INHERIT;
4012 let anchor = getanchor () in
4013 source#reset anchor outlines;
4014 state.uioh <- coe (new outlinelistview ~source);
4015 G.postRedisplay "enter selector";
4019 let enteroutlinemode =
4020 let f = enterselector false in
4021 fun ()-> f "Document has no outline";
4024 let enterbookmarkmode =
4025 let f = enterselector true in
4026 fun () -> f "Document has no bookmarks (yet)";
4029 let color_of_string s =
4030 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4031 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4035 let color_to_string (r, g, b) =
4036 let r = truncate (r *. 256.0)
4037 and g = truncate (g *. 256.0)
4038 and b = truncate (b *. 256.0) in
4039 Printf.sprintf "%d/%d/%d" r g b
4042 let irect_of_string s =
4043 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4046 let irect_to_string (x0,y0,x1,y1) =
4047 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4050 let makecheckers () =
4051 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4052 following to say:
4053 converted by Issac Trotts. July 25, 2002 *)
4054 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4055 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4056 let id = GlTex.gen_texture () in
4057 GlTex.bind_texture `texture_2d id;
4058 GlPix.store (`unpack_alignment 1);
4059 GlTex.image2d image;
4060 List.iter (GlTex.parameter ~target:`texture_2d)
4061 [ `mag_filter `nearest; `min_filter `nearest ];
4065 let setcheckers enabled =
4066 match state.texid with
4067 | None ->
4068 if enabled then state.texid <- Some (makecheckers ())
4070 | Some texid ->
4071 if not enabled
4072 then (
4073 GlTex.delete_texture texid;
4074 state.texid <- None;
4078 let int_of_string_with_suffix s =
4079 let l = String.length s in
4080 let s1, shift =
4081 if l > 1
4082 then
4083 let suffix = Char.lowercase s.[l-1] in
4084 match suffix with
4085 | 'k' -> String.sub s 0 (l-1), 10
4086 | 'm' -> String.sub s 0 (l-1), 20
4087 | 'g' -> String.sub s 0 (l-1), 30
4088 | _ -> s, 0
4089 else s, 0
4091 let n = int_of_string s1 in
4092 let m = n lsl shift in
4093 if m < 0 || m < n
4094 then raise (Failure "value too large")
4095 else m
4098 let string_with_suffix_of_int n =
4099 if n = 0
4100 then "0"
4101 else
4102 let n, s =
4103 if n land ((1 lsl 30) - 1) = 0
4104 then n lsr 30, "G"
4105 else (
4106 if n land ((1 lsl 20) - 1) = 0
4107 then n lsr 20, "M"
4108 else (
4109 if n land ((1 lsl 10) - 1) = 0
4110 then n lsr 10, "K"
4111 else n, ""
4115 let rec loop s n =
4116 let h = n mod 1000 in
4117 let n = n / 1000 in
4118 if n = 0
4119 then string_of_int h ^ s
4120 else (
4121 let s = Printf.sprintf "_%03d%s" h s in
4122 loop s n
4125 loop "" n ^ s;
4128 let defghyllscroll = (40, 8, 32);;
4129 let ghyllscroll_of_string s =
4130 let (n, a, b) as nab =
4131 if s = "default"
4132 then defghyllscroll
4133 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4135 if n <= a || n <= b || a >= b
4136 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4137 nab;
4140 let ghyllscroll_to_string ((n, a, b) as nab) =
4141 if nab = defghyllscroll
4142 then "default"
4143 else Printf.sprintf "%d,%d,%d" n a b;
4146 let describe_location () =
4147 let fn = page_of_y state.y in
4148 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4149 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4150 let percent =
4151 if maxy <= 0
4152 then 100.
4153 else (100. *. (float state.y /. float maxy))
4155 if fn = ln
4156 then
4157 Printf.sprintf "page %d of %d [%.2f%%]"
4158 (fn+1) state.pagecount percent
4159 else
4160 Printf.sprintf
4161 "pages %d-%d of %d [%.2f%%]"
4162 (fn+1) (ln+1) state.pagecount percent
4165 let setpresentationmode v =
4166 let n = page_of_y state.y in
4167 state.anchor <- (n, 0.0, 1.0);
4168 conf.presentation <- v;
4169 if conf.presentation
4170 then (
4171 if not conf.scrollbarinpm
4172 then state.scrollw <- 0;
4174 else state.scrollw <- conf.scrollbw;
4175 represent ();
4178 let enterinfomode =
4179 let btos b = if b then "\xe2\x88\x9a" else "" in
4180 let showextended = ref false in
4181 let leave mode = function
4182 | Confirm -> state.mode <- mode
4183 | Cancel -> state.mode <- mode in
4184 let src =
4185 (object
4186 val mutable m_first_time = true
4187 val mutable m_l = []
4188 val mutable m_a = [||]
4189 val mutable m_prev_uioh = nouioh
4190 val mutable m_prev_mode = View
4192 inherit lvsourcebase
4194 method reset prev_mode prev_uioh =
4195 m_a <- Array.of_list (List.rev m_l);
4196 m_l <- [];
4197 m_prev_mode <- prev_mode;
4198 m_prev_uioh <- prev_uioh;
4199 if m_first_time
4200 then (
4201 let rec loop n =
4202 if n >= Array.length m_a
4203 then ()
4204 else
4205 match m_a.(n) with
4206 | _, _, _, Action _ -> m_active <- n
4207 | _ -> loop (n+1)
4209 loop 0;
4210 m_first_time <- false;
4213 method int name get set =
4214 m_l <-
4215 (name, `int get, 1, Action (
4216 fun u ->
4217 let ondone s =
4218 try set (int_of_string s)
4219 with exn ->
4220 state.text <- Printf.sprintf "bad integer `%s': %s"
4221 s (exntos exn)
4223 state.text <- "";
4224 let te = name ^ ": ", "", None, intentry, ondone, true in
4225 state.mode <- Textentry (te, leave m_prev_mode);
4227 )) :: m_l
4229 method int_with_suffix name get set =
4230 m_l <-
4231 (name, `intws get, 1, Action (
4232 fun u ->
4233 let ondone s =
4234 try set (int_of_string_with_suffix s)
4235 with exn ->
4236 state.text <- Printf.sprintf "bad integer `%s': %s"
4237 s (exntos exn)
4239 state.text <- "";
4240 let te =
4241 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4243 state.mode <- Textentry (te, leave m_prev_mode);
4245 )) :: m_l
4247 method bool ?(offset=1) ?(btos=btos) name get set =
4248 m_l <-
4249 (name, `bool (btos, get), offset, Action (
4250 fun u ->
4251 let v = get () in
4252 set (not v);
4254 )) :: m_l
4256 method color name get set =
4257 m_l <-
4258 (name, `color get, 1, Action (
4259 fun u ->
4260 let invalid = (nan, nan, nan) in
4261 let ondone s =
4262 let c =
4263 try color_of_string s
4264 with exn ->
4265 state.text <- Printf.sprintf "bad color `%s': %s"
4266 s (exntos exn);
4267 invalid
4269 if c <> invalid
4270 then set c;
4272 let te = name ^ ": ", "", None, textentry, ondone, true in
4273 state.text <- color_to_string (get ());
4274 state.mode <- Textentry (te, leave m_prev_mode);
4276 )) :: m_l
4278 method string name get set =
4279 m_l <-
4280 (name, `string get, 1, Action (
4281 fun u ->
4282 let ondone s = set s in
4283 let te = name ^ ": ", "", None, textentry, ondone, true in
4284 state.mode <- Textentry (te, leave m_prev_mode);
4286 )) :: m_l
4288 method colorspace name get set =
4289 m_l <-
4290 (name, `string get, 1, Action (
4291 fun _ ->
4292 let source =
4293 let vals = [| "rgb"; "bgr"; "gray" |] in
4294 (object
4295 inherit lvsourcebase
4297 initializer
4298 m_active <- int_of_colorspace conf.colorspace;
4299 m_first <- 0;
4301 method getitemcount = Array.length vals
4302 method getitem n = (vals.(n), 0)
4303 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4304 ignore (uioh, first, pan, qsearch);
4305 if not cancel then set active;
4306 None
4307 method hasaction _ = true
4308 end)
4310 state.text <- "";
4311 let modehash = findkeyhash conf "info" in
4312 coe (new listview ~source ~trusted:true ~modehash)
4313 )) :: m_l
4315 method fitmodel name get set =
4316 m_l <-
4317 (name, `string get, 1, Action (
4318 fun _ ->
4319 let source =
4320 let vals = [| "fit width"; "proportional"; "fit page" |] in
4321 (object
4322 inherit lvsourcebase
4324 initializer
4325 m_active <- int_of_fitmodel conf.fitmodel;
4326 m_first <- 0;
4328 method getitemcount = Array.length vals
4329 method getitem n = (vals.(n), 0)
4330 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4331 ignore (uioh, first, pan, qsearch);
4332 if not cancel then set active;
4333 None
4334 method hasaction _ = true
4335 end)
4337 state.text <- "";
4338 let modehash = findkeyhash conf "info" in
4339 coe (new listview ~source ~trusted:true ~modehash)
4340 )) :: m_l
4342 method caption s offset =
4343 m_l <- (s, `empty, offset, Noaction) :: m_l
4345 method caption2 s f offset =
4346 m_l <- (s, `string f, offset, Noaction) :: m_l
4348 method getitemcount = Array.length m_a
4350 method getitem n =
4351 let tostr = function
4352 | `int f -> string_of_int (f ())
4353 | `intws f -> string_with_suffix_of_int (f ())
4354 | `string f -> f ()
4355 | `color f -> color_to_string (f ())
4356 | `bool (btos, f) -> btos (f ())
4357 | `empty -> ""
4359 let name, t, offset, _ = m_a.(n) in
4360 ((let s = tostr t in
4361 if String.length s > 0
4362 then Printf.sprintf "%s\t%s" name s
4363 else name),
4364 offset)
4366 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4367 let uiohopt =
4368 if not cancel
4369 then (
4370 m_qsearch <- qsearch;
4371 let uioh =
4372 match m_a.(active) with
4373 | _, _, _, Action f -> f uioh
4374 | _ -> uioh
4376 Some uioh
4378 else None
4380 m_active <- active;
4381 m_first <- first;
4382 m_pan <- pan;
4383 uiohopt
4385 method hasaction n =
4386 match m_a.(n) with
4387 | _, _, _, Action _ -> true
4388 | _ -> false
4389 end)
4391 let rec fillsrc prevmode prevuioh =
4392 let sep () = src#caption "" 0 in
4393 let colorp name get set =
4394 src#string name
4395 (fun () -> color_to_string (get ()))
4396 (fun v ->
4398 let c = color_of_string v in
4399 set c
4400 with exn ->
4401 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4404 let oldmode = state.mode in
4405 let birdseye = isbirdseye state.mode in
4407 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4409 src#bool "presentation mode"
4410 (fun () -> conf.presentation)
4411 (fun v -> setpresentationmode v);
4413 src#bool "ignore case in searches"
4414 (fun () -> conf.icase)
4415 (fun v -> conf.icase <- v);
4417 src#bool "preload"
4418 (fun () -> conf.preload)
4419 (fun v -> conf.preload <- v);
4421 src#bool "highlight links"
4422 (fun () -> conf.hlinks)
4423 (fun v -> conf.hlinks <- v);
4425 src#bool "under info"
4426 (fun () -> conf.underinfo)
4427 (fun v -> conf.underinfo <- v);
4429 src#bool "persistent bookmarks"
4430 (fun () -> conf.savebmarks)
4431 (fun v -> conf.savebmarks <- v);
4433 src#fitmodel "fit model"
4434 (fun () -> fitmodel_to_string conf.fitmodel)
4435 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4437 src#bool "trim margins"
4438 (fun () -> conf.trimmargins)
4439 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4441 src#bool "persistent location"
4442 (fun () -> conf.jumpback)
4443 (fun v -> conf.jumpback <- v);
4445 sep ();
4446 src#int "inter-page space"
4447 (fun () -> conf.interpagespace)
4448 (fun n ->
4449 conf.interpagespace <- n;
4450 docolumns conf.columns;
4451 let pageno, py =
4452 match state.layout with
4453 | [] -> 0, 0
4454 | l :: _ ->
4455 l.pageno, l.pagey
4457 state.maxy <- calcheight ();
4458 let y = getpagey pageno in
4459 gotoy (y + py)
4462 src#int "page bias"
4463 (fun () -> conf.pagebias)
4464 (fun v -> conf.pagebias <- v);
4466 src#int "scroll step"
4467 (fun () -> conf.scrollstep)
4468 (fun n -> conf.scrollstep <- n);
4470 src#int "horizontal scroll step"
4471 (fun () -> conf.hscrollstep)
4472 (fun v -> conf.hscrollstep <- v);
4474 src#int "auto scroll step"
4475 (fun () ->
4476 match state.autoscroll with
4477 | Some step -> step
4478 | _ -> conf.autoscrollstep)
4479 (fun n ->
4480 if state.autoscroll <> None
4481 then state.autoscroll <- Some n;
4482 conf.autoscrollstep <- n);
4484 src#int "zoom"
4485 (fun () -> truncate (conf.zoom *. 100.))
4486 (fun v -> setzoom ((float v) /. 100.));
4488 src#int "rotation"
4489 (fun () -> conf.angle)
4490 (fun v -> reqlayout v conf.fitmodel);
4492 src#int "scroll bar width"
4493 (fun () -> state.scrollw)
4494 (fun v ->
4495 state.scrollw <- v;
4496 conf.scrollbw <- v;
4497 reshape state.winw state.winh;
4500 src#int "scroll handle height"
4501 (fun () -> conf.scrollh)
4502 (fun v -> conf.scrollh <- v;);
4504 src#int "thumbnail width"
4505 (fun () -> conf.thumbw)
4506 (fun v ->
4507 conf.thumbw <- min 4096 v;
4508 match oldmode with
4509 | Birdseye beye ->
4510 leavebirdseye beye false;
4511 enterbirdseye ()
4512 | _ -> ()
4515 let mode = state.mode in
4516 src#string "columns"
4517 (fun () ->
4518 match conf.columns with
4519 | Csingle _ -> "1"
4520 | Cmulti (multi, _) -> multicolumns_to_string multi
4521 | Csplit (count, _) -> "-" ^ string_of_int count
4523 (fun v ->
4524 let n, a, b = multicolumns_of_string v in
4525 setcolumns mode n a b);
4527 sep ();
4528 src#caption "Presentation mode" 0;
4529 src#bool "scrollbar visible"
4530 (fun () -> conf.scrollbarinpm)
4531 (fun v ->
4532 if v != conf.scrollbarinpm
4533 then (
4534 conf.scrollbarinpm <- v;
4535 if conf.presentation
4536 then (
4537 state.scrollw <- if v then conf.scrollbw else 0;
4538 reshape state.winw state.winh;
4543 sep ();
4544 src#caption "Pixmap cache" 0;
4545 src#int_with_suffix "size (advisory)"
4546 (fun () -> conf.memlimit)
4547 (fun v -> conf.memlimit <- v);
4549 src#caption2 "used"
4550 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4551 (string_with_suffix_of_int state.memused)
4552 (Hashtbl.length state.tilemap)) 1;
4554 sep ();
4555 src#caption "Layout" 0;
4556 src#caption2 "Dimension"
4557 (fun () ->
4558 Printf.sprintf "%dx%d (virtual %dx%d)"
4559 state.winw state.winh
4560 state.w state.maxy)
4562 if conf.debug
4563 then
4564 src#caption2 "Position" (fun () ->
4565 Printf.sprintf "%dx%d" state.x state.y
4567 else
4568 src#caption2 "Position" (fun () -> describe_location ()) 1
4571 sep ();
4572 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4573 "Save these parameters as global defaults at exit"
4574 (fun () -> conf.bedefault)
4575 (fun v -> conf.bedefault <- v)
4578 sep ();
4579 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4580 src#bool ~offset:0 ~btos "Extended parameters"
4581 (fun () -> !showextended)
4582 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4583 if !showextended
4584 then (
4585 src#bool "checkers"
4586 (fun () -> conf.checkers)
4587 (fun v -> conf.checkers <- v; setcheckers v);
4588 src#bool "update cursor"
4589 (fun () -> conf.updatecurs)
4590 (fun v -> conf.updatecurs <- v);
4591 src#bool "verbose"
4592 (fun () -> conf.verbose)
4593 (fun v -> conf.verbose <- v);
4594 src#bool "invert colors"
4595 (fun () -> conf.invert)
4596 (fun v -> conf.invert <- v);
4597 src#bool "max fit"
4598 (fun () -> conf.maxhfit)
4599 (fun v -> conf.maxhfit <- v);
4600 src#bool "redirect stderr"
4601 (fun () -> conf.redirectstderr)
4602 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4603 src#string "uri launcher"
4604 (fun () -> conf.urilauncher)
4605 (fun v -> conf.urilauncher <- v);
4606 src#string "path launcher"
4607 (fun () -> conf.pathlauncher)
4608 (fun v -> conf.pathlauncher <- v);
4609 src#string "tile size"
4610 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4611 (fun v ->
4613 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4614 conf.tilew <- max 64 w;
4615 conf.tileh <- max 64 h;
4616 flushtiles ();
4617 with exn ->
4618 state.text <- Printf.sprintf "bad tile size `%s': %s"
4619 v (exntos exn)
4621 src#int "texture count"
4622 (fun () -> conf.texcount)
4623 (fun v ->
4624 if realloctexts v
4625 then conf.texcount <- v
4626 else showtext '!' " Failed to set texture count please retry later"
4628 src#int "slice height"
4629 (fun () -> conf.sliceheight)
4630 (fun v ->
4631 conf.sliceheight <- v;
4632 wcmd "sliceh %d" conf.sliceheight;
4634 src#int "anti-aliasing level"
4635 (fun () -> conf.aalevel)
4636 (fun v ->
4637 conf.aalevel <- bound v 0 8;
4638 state.anchor <- getanchor ();
4639 opendoc state.path state.password;
4641 src#string "page scroll scaling factor"
4642 (fun () -> string_of_float conf.pgscale)
4643 (fun v ->
4645 let s = float_of_string v in
4646 conf.pgscale <- s
4647 with exn ->
4648 state.text <- Printf.sprintf
4649 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4652 src#int "ui font size"
4653 (fun () -> fstate.fontsize)
4654 (fun v -> setfontsize (bound v 5 100));
4655 src#int "hint font size"
4656 (fun () -> conf.hfsize)
4657 (fun v -> conf.hfsize <- bound v 5 100);
4658 colorp "background color"
4659 (fun () -> conf.bgcolor)
4660 (fun v -> conf.bgcolor <- v);
4661 src#bool "crop hack"
4662 (fun () -> conf.crophack)
4663 (fun v -> conf.crophack <- v);
4664 src#string "trim fuzz"
4665 (fun () -> irect_to_string conf.trimfuzz)
4666 (fun v ->
4668 conf.trimfuzz <- irect_of_string v;
4669 if conf.trimmargins
4670 then settrim true conf.trimfuzz;
4671 with exn ->
4672 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4674 src#string "throttle"
4675 (fun () ->
4676 match conf.maxwait with
4677 | None -> "show place holder if page is not ready"
4678 | Some time ->
4679 if time = infinity
4680 then "wait for page to fully render"
4681 else
4682 "wait " ^ string_of_float time
4683 ^ " seconds before showing placeholder"
4685 (fun v ->
4687 let f = float_of_string v in
4688 if f <= 0.0
4689 then conf.maxwait <- None
4690 else conf.maxwait <- Some f
4691 with exn ->
4692 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4694 src#string "ghyll scroll"
4695 (fun () ->
4696 match conf.ghyllscroll with
4697 | None -> ""
4698 | Some nab -> ghyllscroll_to_string nab
4700 (fun v ->
4702 let gs =
4703 if String.length v = 0
4704 then None
4705 else Some (ghyllscroll_of_string v)
4707 conf.ghyllscroll <- gs
4708 with exn ->
4709 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4711 src#string "selection command"
4712 (fun () -> conf.selcmd)
4713 (fun v -> conf.selcmd <- v);
4714 src#string "synctex command"
4715 (fun () -> conf.stcmd)
4716 (fun v -> conf.stcmd <- v);
4717 src#colorspace "color space"
4718 (fun () -> colorspace_to_string conf.colorspace)
4719 (fun v ->
4720 conf.colorspace <- colorspace_of_int v;
4721 wcmd "cs %d" v;
4722 load state.layout;
4724 if pbousable ()
4725 then
4726 src#bool "use PBO"
4727 (fun () -> conf.usepbo)
4728 (fun v -> conf.usepbo <- v);
4729 src#bool "mouse wheel scrolls pages"
4730 (fun () -> conf.wheelbypage)
4731 (fun v -> conf.wheelbypage <- v);
4734 sep ();
4735 src#caption "Document" 0;
4736 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4737 src#caption2 "Pages"
4738 (fun () -> string_of_int state.pagecount) 1;
4739 src#caption2 "Dimensions"
4740 (fun () -> string_of_int (List.length state.pdims)) 1;
4741 if conf.trimmargins
4742 then (
4743 sep ();
4744 src#caption "Trimmed margins" 0;
4745 src#caption2 "Dimensions"
4746 (fun () -> string_of_int (List.length state.pdims)) 1;
4749 sep ();
4750 src#caption "OpenGL" 0;
4751 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4752 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4754 sep ();
4755 src#caption "Location" 0;
4756 if String.length state.origin > 0
4757 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4758 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4760 src#reset prevmode prevuioh;
4762 fun () ->
4763 state.text <- "";
4764 let prevmode = state.mode
4765 and prevuioh = state.uioh in
4766 fillsrc prevmode prevuioh;
4767 let source = (src :> lvsource) in
4768 let modehash = findkeyhash conf "info" in
4769 state.uioh <- coe (object (self)
4770 inherit listview ~source ~trusted:true ~modehash as super
4771 val mutable m_prevmemused = 0
4772 method infochanged = function
4773 | Memused ->
4774 if m_prevmemused != state.memused
4775 then (
4776 m_prevmemused <- state.memused;
4777 G.postRedisplay "memusedchanged";
4779 | Pdim -> G.postRedisplay "pdimchanged"
4780 | Docinfo -> fillsrc prevmode prevuioh
4782 method key key mask =
4783 if not (Wsi.withctrl mask)
4784 then
4785 match key with
4786 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4787 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4788 | _ -> super#key key mask
4789 else super#key key mask
4790 end);
4791 G.postRedisplay "info";
4794 let enterhelpmode =
4795 let source =
4796 (object
4797 inherit lvsourcebase
4798 method getitemcount = Array.length state.help
4799 method getitem n =
4800 let s, l, _ = state.help.(n) in
4801 (s, l)
4803 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4804 let optuioh =
4805 if not cancel
4806 then (
4807 m_qsearch <- qsearch;
4808 match state.help.(active) with
4809 | _, _, Action f -> Some (f uioh)
4810 | _ -> Some (uioh)
4812 else None
4814 m_active <- active;
4815 m_first <- first;
4816 m_pan <- pan;
4817 optuioh
4819 method hasaction n =
4820 match state.help.(n) with
4821 | _, _, Action _ -> true
4822 | _ -> false
4824 initializer
4825 m_active <- -1
4826 end)
4827 in fun () ->
4828 let modehash = findkeyhash conf "help" in
4829 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4830 G.postRedisplay "help";
4833 let entermsgsmode =
4834 let msgsource =
4835 let re = Str.regexp "[\r\n]" in
4836 (object
4837 inherit lvsourcebase
4838 val mutable m_items = [||]
4840 method getitemcount = 1 + Array.length m_items
4842 method getitem n =
4843 if n = 0
4844 then "[Clear]", 0
4845 else m_items.(n-1), 0
4847 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4848 ignore uioh;
4849 if not cancel
4850 then (
4851 if active = 0
4852 then Buffer.clear state.errmsgs;
4853 m_qsearch <- qsearch;
4855 m_active <- active;
4856 m_first <- first;
4857 m_pan <- pan;
4858 None
4860 method hasaction n =
4861 n = 0
4863 method reset =
4864 state.newerrmsgs <- false;
4865 let l = Str.split re (Buffer.contents state.errmsgs) in
4866 m_items <- Array.of_list l
4868 initializer
4869 m_active <- 0
4870 end)
4871 in fun () ->
4872 state.text <- "";
4873 msgsource#reset;
4874 let source = (msgsource :> lvsource) in
4875 let modehash = findkeyhash conf "listview" in
4876 state.uioh <- coe (object
4877 inherit listview ~source ~trusted:false ~modehash as super
4878 method display =
4879 if state.newerrmsgs
4880 then msgsource#reset;
4881 super#display
4882 end);
4883 G.postRedisplay "msgs";
4886 let quickbookmark ?title () =
4887 match state.layout with
4888 | [] -> ()
4889 | l :: _ ->
4890 let title =
4891 match title with
4892 | None ->
4893 let sec = Unix.gettimeofday () in
4894 let tm = Unix.localtime sec in
4895 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4896 (l.pageno+1)
4897 tm.Unix.tm_mday
4898 tm.Unix.tm_mon
4899 (tm.Unix.tm_year + 1900)
4900 tm.Unix.tm_hour
4901 tm.Unix.tm_min
4902 | Some title -> title
4904 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4907 let setautoscrollspeed step goingdown =
4908 let incr = max 1 ((abs step) / 2) in
4909 let incr = if goingdown then incr else -incr in
4910 let astep = step + incr in
4911 state.autoscroll <- Some astep;
4914 let gotounder = function
4915 | Ulinkgoto (pageno, top) ->
4916 if pageno >= 0
4917 then (
4918 addnav ();
4919 gotopage1 pageno top;
4922 | Ulinkuri s ->
4923 gotouri s
4925 | Uremote (filename, pageno) ->
4926 let path =
4927 if Sys.file_exists filename
4928 then filename
4929 else
4930 let dir = Filename.dirname state.path in
4931 let path = Filename.concat dir filename in
4932 if Sys.file_exists path
4933 then path
4934 else ""
4936 if String.length path > 0
4937 then (
4938 let anchor = getanchor () in
4939 let ranchor = state.path, state.password, anchor, state.origin in
4940 state.origin <- "";
4941 state.anchor <- (pageno, 0.0, 0.0);
4942 state.ranchors <- ranchor :: state.ranchors;
4943 opendoc path "";
4945 else showtext '!' ("Could not find " ^ filename)
4947 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4950 let canpan () =
4951 match conf.columns with
4952 | Csplit _ -> true
4953 | _ -> state.x != 0 || conf.zoom > 1.0
4956 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4958 let existsinrow pageno (columns, coverA, coverB) p =
4959 let last = ((pageno - coverA) mod columns) + columns in
4960 let rec any = function
4961 | [] -> false
4962 | l :: rest ->
4963 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4964 then p l
4965 else (
4966 if not (p l)
4967 then (if l.pageno = last then false else any rest)
4968 else true
4971 any state.layout
4974 let nextpage () =
4975 match state.layout with
4976 | [] ->
4977 let pageno = page_of_y state.y in
4978 gotoghyll (getpagey (pageno+1))
4979 | l :: rest ->
4980 match conf.columns with
4981 | Csingle _ ->
4982 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4983 then
4984 let y = clamp (pgscale state.winh) in
4985 gotoghyll y
4986 else
4987 let pageno = min (l.pageno+1) (state.pagecount-1) in
4988 gotoghyll (getpagey pageno)
4989 | Cmulti ((c, _, _) as cl, _) ->
4990 if conf.presentation
4991 && (existsinrow l.pageno cl
4992 (fun l -> l.pageh > l.pagey + l.pagevh))
4993 then
4994 let y = clamp (pgscale state.winh) in
4995 gotoghyll y
4996 else
4997 let pageno = min (l.pageno+c) (state.pagecount-1) in
4998 gotoghyll (getpagey pageno)
4999 | Csplit (n, _) ->
5000 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5001 then
5002 let pagey, pageh = getpageyh l.pageno in
5003 let pagey = pagey + pageh * l.pagecol in
5004 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5005 gotoghyll (pagey + pageh + ips)
5008 let prevpage () =
5009 match state.layout with
5010 | [] ->
5011 let pageno = page_of_y state.y in
5012 gotoghyll (getpagey (pageno-1))
5013 | l :: _ ->
5014 match conf.columns with
5015 | Csingle _ ->
5016 if conf.presentation && l.pagey != 0
5017 then
5018 gotoghyll (clamp (pgscale ~-(state.winh)))
5019 else
5020 let pageno = max 0 (l.pageno-1) in
5021 gotoghyll (getpagey pageno)
5022 | Cmulti ((c, _, coverB) as cl, _) ->
5023 if conf.presentation &&
5024 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5025 then
5026 gotoghyll (clamp (pgscale ~-(state.winh)))
5027 else
5028 let decr =
5029 if l.pageno = state.pagecount - coverB
5030 then 1
5031 else c
5033 let pageno = max 0 (l.pageno-decr) in
5034 gotoghyll (getpagey pageno)
5035 | Csplit (n, _) ->
5036 let y =
5037 if l.pagecol = 0
5038 then
5039 if l.pageno = 0
5040 then l.pagey
5041 else
5042 let pageno = max 0 (l.pageno-1) in
5043 let pagey, pageh = getpageyh pageno in
5044 pagey + (n-1)*pageh
5045 else
5046 let pagey, pageh = getpageyh l.pageno in
5047 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5049 gotoghyll y
5052 let viewkeyboard key mask =
5053 let enttext te =
5054 let mode = state.mode in
5055 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5056 state.text <- "";
5057 enttext ();
5058 G.postRedisplay "view:enttext"
5060 let ctrl = Wsi.withctrl mask in
5061 let key =
5062 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5064 match key with
5065 | 81 -> (* Q *)
5066 exit 0
5068 | 0xff63 -> (* insert *)
5069 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5070 then (
5071 state.mode <- LinkNav (Ltgendir 0);
5072 gotoy state.y;
5074 else showtext '!' "Keyboard link navigation does not work under rotation"
5076 | 0xff1b | 113 -> (* escape / q *)
5077 begin match state.mstate with
5078 | Mzoomrect _ ->
5079 state.mstate <- Mnone;
5080 Wsi.setcursor Wsi.CURSOR_INHERIT;
5081 G.postRedisplay "kill zoom rect";
5082 | _ ->
5083 begin match state.mode with
5084 | LinkNav _ ->
5085 state.mode <- View;
5086 G.postRedisplay "esc leave linknav"
5087 | _ ->
5088 match state.ranchors with
5089 | [] -> raise Quit
5090 | (path, password, anchor, origin) :: rest ->
5091 state.ranchors <- rest;
5092 state.anchor <- anchor;
5093 state.origin <- origin;
5094 opendoc path password
5095 end;
5096 end;
5098 | 0xff08 -> (* backspace *)
5099 gotoghyll (getnav ~-1)
5101 | 111 -> (* o *)
5102 enteroutlinemode ()
5104 | 117 -> (* u *)
5105 state.rects <- [];
5106 state.text <- "";
5107 G.postRedisplay "dehighlight";
5109 | 47 | 63 -> (* / ? *)
5110 let ondone isforw s =
5111 cbput state.hists.pat s;
5112 state.searchpattern <- s;
5113 search s isforw
5115 let s = String.create 1 in
5116 s.[0] <- Char.chr key;
5117 enttext (s, "", Some (onhist state.hists.pat),
5118 textentry, ondone (key = 47), true)
5120 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5121 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5122 setzoom (conf.zoom +. incr)
5124 | 43 | 0xffab -> (* + *)
5125 let ondone s =
5126 let n =
5127 try int_of_string s with exc ->
5128 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5129 max_int
5131 if n != max_int
5132 then (
5133 conf.pagebias <- n;
5134 state.text <- "page bias is now " ^ string_of_int n;
5137 enttext ("page bias: ", "", None, intentry, ondone, true)
5139 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5140 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5141 setzoom (max 0.01 (conf.zoom -. decr))
5143 | 45 | 0xffad -> (* - *)
5144 let ondone msg = state.text <- msg in
5145 enttext (
5146 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5147 optentry state.mode, ondone, true
5150 | 48 when ctrl -> (* ctrl-0 *)
5151 if conf.zoom = 1.0
5152 then (
5153 state.x <- 0;
5154 state.hscrollh <-
5155 if state.w <= state.winw - state.scrollw
5156 then 0
5157 else state.scrollw
5159 gotoy state.y
5161 else setzoom 1.0
5163 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5164 let cols =
5165 match conf.columns with
5166 | Csingle _ | Cmulti _ -> 1
5167 | Csplit (n, _) -> n
5169 let h = state.winh -
5170 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5172 let zoom = zoomforh state.winw h state.scrollw cols in
5173 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5174 then setzoom zoom
5176 | 51 when ctrl -> (* ctrl-3 *)
5177 let fm =
5178 match conf.fitmodel with
5179 | FitWidth -> FitProportional
5180 | FitProportional -> FitPage
5181 | FitPage -> FitWidth
5183 state.text <- "fit model: " ^ fitmodel_to_string fm;
5184 reqlayout conf.angle fm
5186 | 0xffc6 -> (* f9 *)
5187 togglebirdseye ()
5189 | 57 when ctrl -> (* ctrl-9 *)
5190 togglebirdseye ()
5192 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5193 when not ctrl -> (* 0..9 *)
5194 let ondone s =
5195 let n =
5196 try int_of_string s with exc ->
5197 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5200 if n >= 0
5201 then (
5202 addnav ();
5203 cbput state.hists.pag (string_of_int n);
5204 gotopage1 (n + conf.pagebias - 1) 0;
5207 let pageentry text key =
5208 match Char.unsafe_chr key with
5209 | 'g' -> TEdone text
5210 | _ -> intentry text key
5212 let text = "x" in text.[0] <- Char.chr key;
5213 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5215 | 98 -> (* b *)
5216 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5217 reshape state.winw state.winh;
5219 | 108 -> (* l *)
5220 conf.hlinks <- not conf.hlinks;
5221 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5222 G.postRedisplay "toggle highlightlinks";
5224 | 70 -> (* F *)
5225 state.glinks <- true;
5226 let mode = state.mode in
5227 state.mode <- Textentry (
5228 (":", "", None, linknentry, linkndone gotounder, false),
5229 (fun _ ->
5230 state.glinks <- false;
5231 state.mode <- mode)
5233 state.text <- "";
5234 G.postRedisplay "view:linkent(F)"
5236 | 121 -> (* y *)
5237 state.glinks <- true;
5238 let mode = state.mode in
5239 state.mode <- Textentry (
5241 ":", "", None, linknentry, linkndone (fun under ->
5242 selstring (undertext under);
5243 ), false
5245 fun _ ->
5246 state.glinks <- false;
5247 state.mode <- mode
5249 state.text <- "";
5250 G.postRedisplay "view:linkent"
5252 | 97 -> (* a *)
5253 begin match state.autoscroll with
5254 | Some step ->
5255 conf.autoscrollstep <- step;
5256 state.autoscroll <- None
5257 | None ->
5258 if conf.autoscrollstep = 0
5259 then state.autoscroll <- Some 1
5260 else state.autoscroll <- Some conf.autoscrollstep
5263 | 112 when ctrl -> (* ctrl-p *)
5264 launchpath ()
5266 | 80 -> (* P *)
5267 setpresentationmode (not conf.presentation);
5268 showtext ' ' ("presentation mode " ^
5269 if conf.presentation then "on" else "off");
5271 | 102 -> (* f *)
5272 if List.mem Wsi.Fullscreen state.winstate
5273 then Wsi.reshape conf.cwinw conf.cwinh
5274 else Wsi.fullscreen ()
5276 | 112 | 78 -> (* p|N *)
5277 search state.searchpattern false
5279 | 110 | 0xffc0 -> (* n|F3 *)
5280 search state.searchpattern true
5282 | 116 -> (* t *)
5283 begin match state.layout with
5284 | [] -> ()
5285 | l :: _ ->
5286 gotoghyll (getpagey l.pageno)
5289 | 32 -> (* space *)
5290 nextpage ()
5292 | 0xff9f | 0xffff -> (* delete *)
5293 prevpage ()
5295 | 61 -> (* = *)
5296 showtext ' ' (describe_location ());
5298 | 119 -> (* w *)
5299 begin match state.layout with
5300 | [] -> ()
5301 | l :: _ ->
5302 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5303 G.postRedisplay "w"
5306 | 39 -> (* ' *)
5307 enterbookmarkmode ()
5309 | 104 | 0xffbe -> (* h|F1 *)
5310 enterhelpmode ()
5312 | 105 -> (* i *)
5313 enterinfomode ()
5315 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5316 entermsgsmode ()
5318 | 109 -> (* m *)
5319 let ondone s =
5320 match state.layout with
5321 | l :: _ ->
5322 if String.length s > 0
5323 then
5324 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5325 | _ -> ()
5327 enttext ("bookmark: ", "", None, textentry, ondone, true)
5329 | 126 -> (* ~ *)
5330 quickbookmark ();
5331 showtext ' ' "Quick bookmark added";
5333 | 122 -> (* z *)
5334 begin match state.layout with
5335 | l :: _ ->
5336 let rect = getpdimrect l.pagedimno in
5337 let w, h =
5338 if conf.crophack
5339 then
5340 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5341 truncate (1.2 *. (rect.(3) -. rect.(0))))
5342 else
5343 (truncate (rect.(1) -. rect.(0)),
5344 truncate (rect.(3) -. rect.(0)))
5346 let w = truncate ((float w)*.conf.zoom)
5347 and h = truncate ((float h)*.conf.zoom) in
5348 if w != 0 && h != 0
5349 then (
5350 state.anchor <- getanchor ();
5351 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5353 G.postRedisplay "z";
5355 | [] -> ()
5358 | 60 | 62 -> (* < > *)
5359 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5361 | 91 | 93 -> (* [ ] *)
5362 conf.colorscale <-
5363 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5365 G.postRedisplay "brightness";
5367 | 99 when state.mode = View -> (* c *)
5368 let (c, a, b), z =
5369 match state.prevcolumns with
5370 | None -> (1, 0, 0), 1.0
5371 | Some (columns, z) ->
5372 let cab =
5373 match columns with
5374 | Csplit (c, _) -> -c, 0, 0
5375 | Cmulti ((c, a, b), _) -> c, a, b
5376 | Csingle _ -> 1, 0, 0
5378 cab, z
5380 setcolumns View c a b;
5381 setzoom z;
5383 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5384 setzoom state.prevzoom
5386 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5387 begin match state.autoscroll with
5388 | None ->
5389 begin match state.mode with
5390 | Birdseye beye -> upbirdseye 1 beye
5391 | _ ->
5392 if ctrl
5393 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5394 else (
5395 if not (Wsi.withshift mask) && conf.presentation
5396 then prevpage ()
5397 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5400 | Some n ->
5401 setautoscrollspeed n false
5404 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5405 begin match state.autoscroll with
5406 | None ->
5407 begin match state.mode with
5408 | Birdseye beye -> downbirdseye 1 beye
5409 | _ ->
5410 if ctrl
5411 then gotoy_and_clear_text (clamp (state.winh/2))
5412 else (
5413 if not (Wsi.withshift mask) && conf.presentation
5414 then nextpage ()
5415 else gotoy_and_clear_text (clamp conf.scrollstep)
5418 | Some n ->
5419 setautoscrollspeed n true
5422 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5423 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5424 if canpan ()
5425 then
5426 let dx =
5427 if ctrl
5428 then state.winw / 2
5429 else conf.hscrollstep
5431 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5432 state.x <- panbound (state.x + dx);
5433 gotoy_and_clear_text state.y
5434 else (
5435 state.text <- "";
5436 G.postRedisplay "left/right"
5439 | 0xff55 | 0xff9a -> (* (kp) prior *)
5440 let y =
5441 if ctrl
5442 then
5443 match state.layout with
5444 | [] -> state.y
5445 | l :: _ -> state.y - l.pagey
5446 else
5447 clamp (pgscale (-state.winh))
5449 gotoghyll y
5451 | 0xff56 | 0xff9b -> (* (kp) next *)
5452 let y =
5453 if ctrl
5454 then
5455 match List.rev state.layout with
5456 | [] -> state.y
5457 | l :: _ -> getpagey l.pageno
5458 else
5459 clamp (pgscale state.winh)
5461 gotoghyll y
5463 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5464 gotoghyll 0
5465 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5466 gotoghyll (clamp state.maxy)
5468 | 0xff53 | 0xff98
5469 when Wsi.withalt mask -> (* alt-(kp) right *)
5470 gotoghyll (getnav 1)
5471 | 0xff51 | 0xff96
5472 when Wsi.withalt mask -> (* alt-(kp) left *)
5473 gotoghyll (getnav ~-1)
5475 | 114 -> (* r *)
5476 reload ()
5478 | 118 when conf.debug -> (* v *)
5479 state.rects <- [];
5480 List.iter (fun l ->
5481 match getopaque l.pageno with
5482 | None -> ()
5483 | Some opaque ->
5484 let x0, y0, x1, y1 = pagebbox opaque in
5485 let a,b = float x0, float y0 in
5486 let c,d = float x1, float y0 in
5487 let e,f = float x1, float y1 in
5488 let h,j = float x0, float y1 in
5489 let rect = (a,b,c,d,e,f,h,j) in
5490 debugrect rect;
5491 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5492 ) state.layout;
5493 G.postRedisplay "v";
5495 | _ ->
5496 vlog "huh? %s" (Wsi.keyname key)
5499 let linknavkeyboard key mask linknav =
5500 let getpage pageno =
5501 let rec loop = function
5502 | [] -> None
5503 | l :: _ when l.pageno = pageno -> Some l
5504 | _ :: rest -> loop rest
5505 in loop state.layout
5507 let doexact (pageno, n) =
5508 match getopaque pageno, getpage pageno with
5509 | Some opaque, Some l ->
5510 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5511 then
5512 let under = getlink opaque n in
5513 G.postRedisplay "link gotounder";
5514 gotounder under;
5515 state.mode <- View;
5516 else
5517 let opt, dir =
5518 match key with
5519 | 0xff50 -> (* home *)
5520 Some (findlink opaque LDfirst), -1
5522 | 0xff57 -> (* end *)
5523 Some (findlink opaque LDlast), 1
5525 | 0xff51 -> (* left *)
5526 Some (findlink opaque (LDleft n)), -1
5528 | 0xff53 -> (* right *)
5529 Some (findlink opaque (LDright n)), 1
5531 | 0xff52 -> (* up *)
5532 Some (findlink opaque (LDup n)), -1
5534 | 0xff54 -> (* down *)
5535 Some (findlink opaque (LDdown n)), 1
5537 | _ -> None, 0
5539 let pwl l dir =
5540 begin match findpwl l.pageno dir with
5541 | Pwlnotfound -> ()
5542 | Pwl pageno ->
5543 let notfound dir =
5544 state.mode <- LinkNav (Ltgendir dir);
5545 let y, h = getpageyh pageno in
5546 let y =
5547 if dir < 0
5548 then y + h - state.winh
5549 else y
5551 gotoy y
5553 begin match getopaque pageno, getpage pageno with
5554 | Some opaque, Some _ ->
5555 let link =
5556 let ld = if dir > 0 then LDfirst else LDlast in
5557 findlink opaque ld
5559 begin match link with
5560 | Lfound m ->
5561 showlinktype (getlink opaque m);
5562 state.mode <- LinkNav (Ltexact (pageno, m));
5563 G.postRedisplay "linknav jpage";
5564 | _ -> notfound dir
5565 end;
5566 | _ -> notfound dir
5567 end;
5568 end;
5570 begin match opt with
5571 | Some Lnotfound -> pwl l dir;
5572 | Some (Lfound m) ->
5573 if m = n
5574 then pwl l dir
5575 else (
5576 let _, y0, _, y1 = getlinkrect opaque m in
5577 if y0 < l.pagey
5578 then gotopage1 l.pageno y0
5579 else (
5580 let d = fstate.fontsize + 1 in
5581 if y1 - l.pagey > l.pagevh - d
5582 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5583 else G.postRedisplay "linknav";
5585 showlinktype (getlink opaque m);
5586 state.mode <- LinkNav (Ltexact (l.pageno, m));
5589 | None -> viewkeyboard key mask
5590 end;
5591 | _ -> viewkeyboard key mask
5593 if key = 0xff63
5594 then (
5595 state.mode <- View;
5596 G.postRedisplay "leave linknav"
5598 else
5599 match linknav with
5600 | Ltgendir _ -> viewkeyboard key mask
5601 | Ltexact exact -> doexact exact
5604 let keyboard key mask =
5605 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5606 then wcmd "interrupt"
5607 else state.uioh <- state.uioh#key key mask
5610 let birdseyekeyboard key mask
5611 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5612 let incr =
5613 match conf.columns with
5614 | Csingle _ -> 1
5615 | Cmulti ((c, _, _), _) -> c
5616 | Csplit _ -> failwith "bird's eye split mode"
5618 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5619 match key with
5620 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5621 let y, h = getpageyh pageno in
5622 let top = (state.winh - h) / 2 in
5623 gotoy (max 0 (y - top))
5624 | 0xff0d (* enter *)
5625 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5626 | 0xff1b -> leavebirdseye beye true (* escape *)
5627 | 0xff52 -> upbirdseye incr beye (* up *)
5628 | 0xff54 -> downbirdseye incr beye (* down *)
5629 | 0xff51 -> upbirdseye 1 beye (* left *)
5630 | 0xff53 -> downbirdseye 1 beye (* right *)
5632 | 0xff55 -> (* prior *)
5633 begin match state.layout with
5634 | l :: _ ->
5635 if l.pagey != 0
5636 then (
5637 state.mode <- Birdseye (
5638 oconf, leftx, l.pageno, hooverpageno, anchor
5640 gotopage1 l.pageno 0;
5642 else (
5643 let layout = layout (state.y-state.winh) (pgh state.layout) in
5644 match layout with
5645 | [] -> gotoy (clamp (-state.winh))
5646 | l :: _ ->
5647 state.mode <- Birdseye (
5648 oconf, leftx, l.pageno, hooverpageno, anchor
5650 gotopage1 l.pageno 0
5653 | [] -> gotoy (clamp (-state.winh))
5654 end;
5656 | 0xff56 -> (* next *)
5657 begin match List.rev state.layout with
5658 | l :: _ ->
5659 let layout = layout (state.y + (pgh state.layout)) state.winh in
5660 begin match layout with
5661 | [] ->
5662 let incr = l.pageh - l.pagevh in
5663 if incr = 0
5664 then (
5665 state.mode <-
5666 Birdseye (
5667 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5669 G.postRedisplay "birdseye pagedown";
5671 else gotoy (clamp (incr + conf.interpagespace*2));
5673 | l :: _ ->
5674 state.mode <-
5675 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5676 gotopage1 l.pageno 0;
5679 | [] -> gotoy (clamp state.winh)
5680 end;
5682 | 0xff50 -> (* home *)
5683 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5684 gotopage1 0 0
5686 | 0xff57 -> (* end *)
5687 let pageno = state.pagecount - 1 in
5688 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5689 if not (pagevisible state.layout pageno)
5690 then
5691 let h =
5692 match List.rev state.pdims with
5693 | [] -> state.winh
5694 | (_, _, h, _) :: _ -> h
5696 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5697 else G.postRedisplay "birdseye end";
5698 | _ -> viewkeyboard key mask
5701 let drawpage l =
5702 let color =
5703 match state.mode with
5704 | Textentry _ -> scalecolor 0.4
5705 | LinkNav _
5706 | View -> scalecolor 1.0
5707 | Birdseye (_, _, pageno, hooverpageno, _) ->
5708 if l.pageno = hooverpageno
5709 then scalecolor 0.9
5710 else (
5711 if l.pageno = pageno
5712 then scalecolor 1.0
5713 else scalecolor 0.8
5716 drawtiles l color;
5719 let postdrawpage l linkindexbase =
5720 match getopaque l.pageno with
5721 | Some opaque ->
5722 if tileready l l.pagex l.pagey
5723 then
5724 let x = l.pagedispx - l.pagex
5725 and y = l.pagedispy - l.pagey in
5726 let hlmask =
5727 match conf.columns with
5728 | Csingle _ | Cmulti _ ->
5729 (if conf.hlinks then 1 else 0)
5730 + (if state.glinks
5731 && not (isbirdseye state.mode) then 2 else 0)
5732 | _ -> 0
5734 let s =
5735 match state.mode with
5736 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5737 | _ -> ""
5739 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5740 else 0
5741 | _ -> 0
5744 let scrollindicator () =
5745 let sbw, ph, sh = state.uioh#scrollph in
5746 let sbh, pw, sw = state.uioh#scrollpw in
5748 GlDraw.color (0.64, 0.64, 0.64);
5749 GlDraw.rect
5750 (float (state.winw - sbw), 0.)
5751 (float state.winw, float state.winh)
5753 GlDraw.rect
5754 (0., float (state.winh - sbh))
5755 (float (state.winw - state.scrollw - 1), float state.winh)
5757 GlDraw.color (0.0, 0.0, 0.0);
5759 GlDraw.rect
5760 (float (state.winw - sbw), ph)
5761 (float state.winw, ph +. sh)
5763 GlDraw.rect
5764 (pw, float (state.winh - sbh))
5765 (pw +. sw, float state.winh)
5769 let showsel () =
5770 match state.mstate with
5771 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5774 | Msel ((x0, y0), (x1, y1)) ->
5775 let rec loop = function
5776 | l :: ls ->
5777 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5778 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5779 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5780 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5781 then
5782 match getopaque l.pageno with
5783 | Some opaque ->
5784 let x0, y0 = pagetranslatepoint l x0 y0 in
5785 let x1, y1 = pagetranslatepoint l x1 y1 in
5786 seltext opaque (x0, y0, x1, y1);
5787 | _ -> ()
5788 else loop ls
5789 | [] -> ()
5791 loop state.layout
5794 let showrects rects =
5795 Gl.enable `blend;
5796 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5797 GlDraw.polygon_mode `both `fill;
5798 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5799 List.iter
5800 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5801 List.iter (fun l ->
5802 if l.pageno = pageno
5803 then (
5804 let dx = float (l.pagedispx - l.pagex) in
5805 let dy = float (l.pagedispy - l.pagey) in
5806 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5807 GlDraw.begins `quads;
5809 GlDraw.vertex2 (x0+.dx, y0+.dy);
5810 GlDraw.vertex2 (x1+.dx, y1+.dy);
5811 GlDraw.vertex2 (x2+.dx, y2+.dy);
5812 GlDraw.vertex2 (x3+.dx, y3+.dy);
5814 GlDraw.ends ();
5816 ) state.layout
5817 ) rects
5819 Gl.disable `blend;
5822 let display () =
5823 GlClear.color (scalecolor2 conf.bgcolor);
5824 GlClear.clear [`color];
5825 List.iter drawpage state.layout;
5826 let rects =
5827 match state.mode with
5828 | LinkNav (Ltexact (pageno, linkno)) ->
5829 begin match getopaque pageno with
5830 | Some opaque ->
5831 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5832 (pageno, 5, (
5833 float x0, float y0,
5834 float x1, float y0,
5835 float x1, float y1,
5836 float x0, float y1)
5837 ) :: state.rects
5838 | None -> state.rects
5840 | _ -> state.rects
5842 showrects rects;
5843 let rec postloop linkindexbase = function
5844 | l :: rest ->
5845 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5846 postloop linkindexbase rest
5847 | [] -> ()
5849 showsel ();
5850 postloop 0 state.layout;
5851 state.uioh#display;
5852 begin match state.mstate with
5853 | Mzoomrect ((x0, y0), (x1, y1)) ->
5854 Gl.enable `blend;
5855 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5856 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5857 GlDraw.rect (float x0, float y0)
5858 (float x1, float y1);
5859 Gl.disable `blend;
5860 | _ -> ()
5861 end;
5862 enttext ();
5863 scrollindicator ();
5864 Wsi.swapb ();
5867 let zoomrect x y x1 y1 =
5868 let x0 = min x x1
5869 and x1 = max x x1
5870 and y0 = min y y1 in
5871 gotoy (state.y + y0);
5872 state.anchor <- getanchor ();
5873 let zoom = (float state.w) /. float (x1 - x0) in
5874 let margin =
5875 match conf.fitmodel, conf.columns with
5876 | FitPage, Csplit _ ->
5877 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5879 | _, _ ->
5880 if state.w < state.winw - state.scrollw
5881 then (state.winw - state.scrollw - state.w) / 2
5882 else 0
5884 state.x <- (state.x + margin) - x0;
5885 setzoom zoom;
5886 Wsi.setcursor Wsi.CURSOR_INHERIT;
5887 state.mstate <- Mnone;
5890 let scrollx x =
5891 let winw = state.winw - state.scrollw - 1 in
5892 let s = float x /. float winw in
5893 let destx = truncate (float (state.w + winw) *. s) in
5894 state.x <- winw - destx;
5895 gotoy_and_clear_text state.y;
5896 state.mstate <- Mscrollx;
5899 let scrolly y =
5900 let s = float y /. float state.winh in
5901 let desty = truncate (float (state.maxy - state.winh) *. s) in
5902 gotoy_and_clear_text desty;
5903 state.mstate <- Mscrolly;
5906 let viewmouse button down x y mask =
5907 match button with
5908 | n when (n == 4 || n == 5) && not down ->
5909 if Wsi.withctrl mask
5910 then (
5911 match state.mstate with
5912 | Mzoom (oldn, i) ->
5913 if oldn = n
5914 then (
5915 if i = 2
5916 then
5917 let incr =
5918 match n with
5919 | 5 ->
5920 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5921 | _ ->
5922 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5924 let zoom = conf.zoom -. incr in
5925 setzoom zoom;
5926 state.mstate <- Mzoom (n, 0);
5927 else
5928 state.mstate <- Mzoom (n, i+1);
5930 else state.mstate <- Mzoom (n, 0)
5932 | _ -> state.mstate <- Mzoom (n, 0)
5934 else (
5935 match state.autoscroll with
5936 | Some step -> setautoscrollspeed step (n=4)
5937 | None ->
5938 if conf.wheelbypage || conf.presentation
5939 then (
5940 if n = 4
5941 then prevpage ()
5942 else nextpage ()
5944 else
5945 let incr =
5946 if n = 4
5947 then -conf.scrollstep
5948 else conf.scrollstep
5950 let incr = incr * 2 in
5951 let y = clamp incr in
5952 gotoy_and_clear_text y
5955 | n when (n = 6 || n = 7) && not down && canpan () ->
5956 state.x <-
5957 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5958 gotoy_and_clear_text state.y
5960 | 1 when Wsi.withshift mask ->
5961 state.mstate <- Mnone;
5962 if not down
5963 then (
5964 match unproject x y with
5965 | Some (pageno, ux, uy) ->
5966 let cmd = Printf.sprintf
5967 "%s %s %d %d %d"
5968 conf.stcmd state.path pageno ux uy
5970 popen cmd []
5971 | None -> ()
5974 | 1 when Wsi.withctrl mask ->
5975 if down
5976 then (
5977 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5978 state.mstate <- Mpan (x, y)
5980 else
5981 state.mstate <- Mnone
5983 | 3 ->
5984 if down
5985 then (
5986 Wsi.setcursor Wsi.CURSOR_CYCLE;
5987 let p = (x, y) in
5988 state.mstate <- Mzoomrect (p, p)
5990 else (
5991 match state.mstate with
5992 | Mzoomrect ((x0, y0), _) ->
5993 if abs (x-x0) > 10 && abs (y - y0) > 10
5994 then zoomrect x0 y0 x y
5995 else (
5996 state.mstate <- Mnone;
5997 Wsi.setcursor Wsi.CURSOR_INHERIT;
5998 G.postRedisplay "kill accidental zoom rect";
6000 | _ ->
6001 Wsi.setcursor Wsi.CURSOR_INHERIT;
6002 state.mstate <- Mnone
6005 | 1 when x > state.winw - state.scrollw ->
6006 if down
6007 then
6008 let _, position, sh = state.uioh#scrollph in
6009 if y > truncate position && y < truncate (position +. sh)
6010 then state.mstate <- Mscrolly
6011 else scrolly y
6012 else
6013 state.mstate <- Mnone
6015 | 1 when y > state.winh - state.hscrollh ->
6016 if down
6017 then
6018 let _, position, sw = state.uioh#scrollpw in
6019 if x > truncate position && x < truncate (position +. sw)
6020 then state.mstate <- Mscrollx
6021 else scrollx x
6022 else
6023 state.mstate <- Mnone
6025 | 1 ->
6026 let dest = if down then getunder x y else Unone in
6027 begin match dest with
6028 | Ulinkgoto _
6029 | Ulinkuri _
6030 | Uremote _
6031 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6032 gotounder dest
6034 | Unone when down ->
6035 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6036 state.mstate <- Mpan (x, y);
6038 | Unone | Utext _ ->
6039 if down
6040 then (
6041 if conf.angle mod 360 = 0
6042 then (
6043 state.mstate <- Msel ((x, y), (x, y));
6044 G.postRedisplay "mouse select";
6047 else (
6048 match state.mstate with
6049 | Mnone -> ()
6051 | Mzoom _ | Mscrollx | Mscrolly ->
6052 state.mstate <- Mnone
6054 | Mzoomrect ((x0, y0), _) ->
6055 zoomrect x0 y0 x y
6057 | Mpan _ ->
6058 Wsi.setcursor Wsi.CURSOR_INHERIT;
6059 state.mstate <- Mnone
6061 | Msel ((x0, y0), (x1, y1)) ->
6062 let rec loop = function
6063 | [] -> ()
6064 | l :: rest ->
6065 let inside =
6066 let a0 = l.pagedispy in
6067 let a1 = a0 + l.pagevh in
6068 let b0 = l.pagedispx in
6069 let b1 = b0 + l.pagevw in
6070 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6071 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6073 if inside
6074 then
6075 match getopaque l.pageno with
6076 | Some opaque ->
6077 begin
6078 match Ne.pipe () with
6079 | Ne.Exn exn ->
6080 showtext '!'
6081 (Printf.sprintf
6082 "can not create sel pipe: %s"
6083 (exntos exn));
6084 | Ne.Res (r, w) ->
6085 let doclose what fd =
6086 Ne.clo fd (fun msg ->
6087 dolog "%s close failed: %s" what msg)
6090 popen conf.selcmd [r, 0; w, -1];
6091 copysel w opaque;
6092 doclose "pipe/r" r;
6093 G.postRedisplay "copysel";
6094 with exn ->
6095 dolog "can not execute %S: %s"
6096 conf.selcmd (exntos exn);
6097 doclose "pipe/r" r;
6098 doclose "pipe/w" w;
6100 | None -> ()
6101 else loop rest
6103 loop state.layout;
6104 Wsi.setcursor Wsi.CURSOR_INHERIT;
6105 state.mstate <- Mnone;
6109 | _ -> ()
6112 let birdseyemouse button down x y mask
6113 (conf, leftx, _, hooverpageno, anchor) =
6114 match button with
6115 | 1 when down ->
6116 let rec loop = function
6117 | [] -> ()
6118 | l :: rest ->
6119 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6120 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6121 then (
6122 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6124 else loop rest
6126 loop state.layout
6127 | 3 -> ()
6128 | _ -> viewmouse button down x y mask
6131 let mouse button down x y mask =
6132 state.uioh <- state.uioh#button button down x y mask;
6135 let motion ~x ~y =
6136 state.uioh <- state.uioh#motion x y
6139 let pmotion ~x ~y =
6140 state.uioh <- state.uioh#pmotion x y;
6143 let uioh = object
6144 method display = ()
6146 method key key mask =
6147 begin match state.mode with
6148 | Textentry textentry -> textentrykeyboard key mask textentry
6149 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6150 | View -> viewkeyboard key mask
6151 | LinkNav linknav -> linknavkeyboard key mask linknav
6152 end;
6153 state.uioh
6155 method button button bstate x y mask =
6156 begin match state.mode with
6157 | LinkNav _
6158 | View -> viewmouse button bstate x y mask
6159 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6160 | Textentry _ -> ()
6161 end;
6162 state.uioh
6164 method motion x y =
6165 begin match state.mode with
6166 | Textentry _ -> ()
6167 | View | Birdseye _ | LinkNav _ ->
6168 match state.mstate with
6169 | Mzoom _ | Mnone -> ()
6171 | Mpan (x0, y0) ->
6172 let dx = x - x0
6173 and dy = y0 - y in
6174 state.mstate <- Mpan (x, y);
6175 if canpan ()
6176 then state.x <- panbound (state.x + dx);
6177 let y = clamp dy in
6178 gotoy_and_clear_text y
6180 | Msel (a, _) ->
6181 state.mstate <- Msel (a, (x, y));
6182 G.postRedisplay "motion select";
6184 | Mscrolly ->
6185 let y = min state.winh (max 0 y) in
6186 scrolly y
6188 | Mscrollx ->
6189 let x = min state.winw (max 0 x) in
6190 scrollx x
6192 | Mzoomrect (p0, _) ->
6193 state.mstate <- Mzoomrect (p0, (x, y));
6194 G.postRedisplay "motion zoomrect";
6195 end;
6196 state.uioh
6198 method pmotion x y =
6199 begin match state.mode with
6200 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6201 let rec loop = function
6202 | [] ->
6203 if hooverpageno != -1
6204 then (
6205 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6206 G.postRedisplay "pmotion birdseye no hoover";
6208 | l :: rest ->
6209 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6210 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6211 then (
6212 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6213 G.postRedisplay "pmotion birdseye hoover";
6215 else loop rest
6217 loop state.layout
6219 | Textentry _ -> ()
6221 | LinkNav _
6222 | View ->
6223 match state.mstate with
6224 | Mnone -> updateunder x y
6225 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6227 end;
6228 state.uioh
6230 method infochanged _ = ()
6232 method scrollph =
6233 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6234 let p, h =
6235 if maxy = 0
6236 then 0.0, float state.winh
6237 else scrollph state.y maxy
6239 state.scrollw, p, h
6241 method scrollpw =
6242 let winw = state.winw - state.scrollw in
6243 let fwinw = float winw in
6244 let sw =
6245 let sw = fwinw /. float state.w in
6246 let sw = fwinw *. sw in
6247 max sw (float conf.scrollh)
6249 let position =
6250 let maxx = state.w + winw in
6251 let x = winw - state.x in
6252 let percent = float x /. float maxx in
6253 (fwinw -. sw) *. percent
6255 state.hscrollh, position, sw
6257 method modehash =
6258 let modename =
6259 match state.mode with
6260 | LinkNav _ -> "links"
6261 | Textentry _ -> "textentry"
6262 | Birdseye _ -> "birdseye"
6263 | View -> "view"
6265 findkeyhash conf modename
6267 method eformsgs = true
6268 end;;
6270 module Config =
6271 struct
6272 open Parser
6274 let fontpath = ref "";;
6276 module KeyMap =
6277 Map.Make (struct type t = (int * int) let compare = compare end);;
6279 let unent s =
6280 let l = String.length s in
6281 let b = Buffer.create l in
6282 unent b s 0 l;
6283 Buffer.contents b;
6286 let home =
6287 try Sys.getenv "HOME"
6288 with exn ->
6289 prerr_endline
6290 ("Can not determine home directory location: " ^ exntos exn);
6294 let modifier_of_string = function
6295 | "alt" -> Wsi.altmask
6296 | "shift" -> Wsi.shiftmask
6297 | "ctrl" | "control" -> Wsi.ctrlmask
6298 | "meta" -> Wsi.metamask
6299 | _ -> 0
6302 let key_of_string =
6303 let r = Str.regexp "-" in
6304 fun s ->
6305 let elems = Str.full_split r s in
6306 let f n k m =
6307 let g s =
6308 let m1 = modifier_of_string s in
6309 if m1 = 0
6310 then (Wsi.namekey s, m)
6311 else (k, m lor m1)
6312 in function
6313 | Str.Delim s when n land 1 = 0 -> g s
6314 | Str.Text s -> g s
6315 | Str.Delim _ -> (k, m)
6317 let rec loop n k m = function
6318 | [] -> (k, m)
6319 | x :: xs ->
6320 let k, m = f n k m x in
6321 loop (n+1) k m xs
6323 loop 0 0 0 elems
6326 let keys_of_string =
6327 let r = Str.regexp "[ \t]" in
6328 fun s ->
6329 let elems = Str.split r s in
6330 List.map key_of_string elems
6333 let copykeyhashes c =
6334 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6337 let config_of c attrs =
6338 let apply c k v =
6340 match k with
6341 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6342 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6343 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6344 | "preload" -> { c with preload = bool_of_string v }
6345 | "page-bias" -> { c with pagebias = int_of_string v }
6346 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6347 | "horizontal-scroll-step" ->
6348 { c with hscrollstep = max (int_of_string v) 1 }
6349 | "auto-scroll-step" ->
6350 { c with autoscrollstep = max 0 (int_of_string v) }
6351 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6352 | "crop-hack" -> { c with crophack = bool_of_string v }
6353 | "throttle" ->
6354 let mw =
6355 match String.lowercase v with
6356 | "true" -> Some infinity
6357 | "false" -> None
6358 | f -> Some (float_of_string f)
6360 { c with maxwait = mw}
6361 | "highlight-links" -> { c with hlinks = bool_of_string v }
6362 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6363 | "vertical-margin" ->
6364 { c with interpagespace = max 0 (int_of_string v) }
6365 | "zoom" ->
6366 let zoom = float_of_string v /. 100. in
6367 let zoom = max zoom 0.0 in
6368 { c with zoom = zoom }
6369 | "presentation" -> { c with presentation = bool_of_string v }
6370 | "rotation-angle" -> { c with angle = int_of_string v }
6371 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6372 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6373 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6374 | "proportional-display" ->
6375 let fm =
6376 if bool_of_string v
6377 then FitProportional
6378 else FitWidth
6380 { c with fitmodel = fm }
6381 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6382 | "pixmap-cache-size" ->
6383 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6384 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6385 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6386 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6387 | "persistent-location" -> { c with jumpback = bool_of_string v }
6388 | "background-color" -> { c with bgcolor = color_of_string v }
6389 | "scrollbar-in-presentation" ->
6390 { c with scrollbarinpm = bool_of_string v }
6391 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6392 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6393 | "mupdf-store-size" ->
6394 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6395 | "checkers" -> { c with checkers = bool_of_string v }
6396 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6397 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6398 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6399 | "uri-launcher" -> { c with urilauncher = unent v }
6400 | "path-launcher" -> { c with pathlauncher = unent v }
6401 | "color-space" -> { c with colorspace = colorspace_of_string v }
6402 | "invert-colors" -> { c with invert = bool_of_string v }
6403 | "brightness" -> { c with colorscale = float_of_string v }
6404 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6405 | "ghyllscroll" ->
6406 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6407 | "columns" ->
6408 let (n, _, _) as nab = multicolumns_of_string v in
6409 if n < 0
6410 then { c with columns = Csplit (-n, [||]) }
6411 else { c with columns = Cmulti (nab, [||]) }
6412 | "birds-eye-columns" ->
6413 { c with beyecolumns = Some (max (int_of_string v) 2) }
6414 | "selection-command" -> { c with selcmd = unent v }
6415 | "synctex-command" -> { c with stcmd = unent v }
6416 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6417 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6418 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6419 | "use-pbo" -> { c with usepbo = bool_of_string v }
6420 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6421 | _ -> c
6422 with exn ->
6423 prerr_endline ("Error processing attribute (`" ^
6424 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6427 let rec fold c = function
6428 | [] -> c
6429 | (k, v) :: rest ->
6430 let c = apply c k v in
6431 fold c rest
6433 fold { c with keyhashes = copykeyhashes c } attrs;
6436 let fromstring f pos n v d =
6437 try f v
6438 with exn ->
6439 dolog "Error processing attribute (%S=%S) at %d\n%s"
6440 n v pos (exntos exn)
6445 let bookmark_of attrs =
6446 let rec fold title page rely visy = function
6447 | ("title", v) :: rest -> fold v page rely visy rest
6448 | ("page", v) :: rest -> fold title v rely visy rest
6449 | ("rely", v) :: rest -> fold title page v visy rest
6450 | ("visy", v) :: rest -> fold title page rely v rest
6451 | _ :: rest -> fold title page rely visy rest
6452 | [] -> title, page, rely, visy
6454 fold "invalid" "0" "0" "0" attrs
6457 let doc_of attrs =
6458 let rec fold path page rely pan visy = function
6459 | ("path", v) :: rest -> fold v page rely pan visy rest
6460 | ("page", v) :: rest -> fold path v rely pan visy rest
6461 | ("rely", v) :: rest -> fold path page v pan visy rest
6462 | ("pan", v) :: rest -> fold path page rely v visy rest
6463 | ("visy", v) :: rest -> fold path page rely pan v rest
6464 | _ :: rest -> fold path page rely pan visy rest
6465 | [] -> path, page, rely, pan, visy
6467 fold "" "0" "0" "0" "0" attrs
6470 let map_of attrs =
6471 let rec fold rs ls = function
6472 | ("out", v) :: rest -> fold v ls rest
6473 | ("in", v) :: rest -> fold rs v rest
6474 | _ :: rest -> fold ls rs rest
6475 | [] -> ls, rs
6477 fold "" "" attrs
6480 let setconf dst src =
6481 dst.scrollbw <- src.scrollbw;
6482 dst.scrollh <- src.scrollh;
6483 dst.icase <- src.icase;
6484 dst.preload <- src.preload;
6485 dst.pagebias <- src.pagebias;
6486 dst.verbose <- src.verbose;
6487 dst.scrollstep <- src.scrollstep;
6488 dst.maxhfit <- src.maxhfit;
6489 dst.crophack <- src.crophack;
6490 dst.autoscrollstep <- src.autoscrollstep;
6491 dst.maxwait <- src.maxwait;
6492 dst.hlinks <- src.hlinks;
6493 dst.underinfo <- src.underinfo;
6494 dst.interpagespace <- src.interpagespace;
6495 dst.zoom <- src.zoom;
6496 dst.presentation <- src.presentation;
6497 dst.angle <- src.angle;
6498 dst.cwinw <- src.cwinw;
6499 dst.cwinh <- src.cwinh;
6500 dst.savebmarks <- src.savebmarks;
6501 dst.memlimit <- src.memlimit;
6502 dst.fitmodel <- src.fitmodel;
6503 dst.texcount <- src.texcount;
6504 dst.sliceheight <- src.sliceheight;
6505 dst.thumbw <- src.thumbw;
6506 dst.jumpback <- src.jumpback;
6507 dst.bgcolor <- src.bgcolor;
6508 dst.scrollbarinpm <- src.scrollbarinpm;
6509 dst.tilew <- src.tilew;
6510 dst.tileh <- src.tileh;
6511 dst.mustoresize <- src.mustoresize;
6512 dst.checkers <- src.checkers;
6513 dst.aalevel <- src.aalevel;
6514 dst.trimmargins <- src.trimmargins;
6515 dst.trimfuzz <- src.trimfuzz;
6516 dst.urilauncher <- src.urilauncher;
6517 dst.colorspace <- src.colorspace;
6518 dst.invert <- src.invert;
6519 dst.colorscale <- src.colorscale;
6520 dst.redirectstderr <- src.redirectstderr;
6521 dst.ghyllscroll <- src.ghyllscroll;
6522 dst.columns <- src.columns;
6523 dst.beyecolumns <- src.beyecolumns;
6524 dst.selcmd <- src.selcmd;
6525 dst.updatecurs <- src.updatecurs;
6526 dst.pathlauncher <- src.pathlauncher;
6527 dst.keyhashes <- copykeyhashes src;
6528 dst.hfsize <- src.hfsize;
6529 dst.hscrollstep <- src.hscrollstep;
6530 dst.pgscale <- src.pgscale;
6531 dst.usepbo <- src.usepbo;
6532 dst.wheelbypage <- src.wheelbypage;
6533 dst.stcmd <- src.stcmd;
6536 let get s =
6537 let h = Hashtbl.create 10 in
6538 let dc = { defconf with angle = defconf.angle } in
6539 let rec toplevel v t spos _ =
6540 match t with
6541 | Vdata | Vcdata | Vend -> v
6542 | Vopen ("llppconfig", _, closed) ->
6543 if closed
6544 then v
6545 else { v with f = llppconfig }
6546 | Vopen _ ->
6547 error "unexpected subelement at top level" s spos
6548 | Vclose _ -> error "unexpected close at top level" s spos
6550 and llppconfig v t spos _ =
6551 match t with
6552 | Vdata | Vcdata -> v
6553 | Vend -> error "unexpected end of input in llppconfig" s spos
6554 | Vopen ("defaults", attrs, closed) ->
6555 let c = config_of dc attrs in
6556 setconf dc c;
6557 if closed
6558 then v
6559 else { v with f = defaults }
6561 | Vopen ("ui-font", attrs, closed) ->
6562 let rec getsize size = function
6563 | [] -> size
6564 | ("size", v) :: rest ->
6565 let size =
6566 fromstring int_of_string spos "size" v fstate.fontsize in
6567 getsize size rest
6568 | l -> getsize size l
6570 fstate.fontsize <- getsize fstate.fontsize attrs;
6571 if closed
6572 then v
6573 else { v with f = uifont (Buffer.create 10) }
6575 | Vopen ("doc", attrs, closed) ->
6576 let pathent, spage, srely, span, svisy = doc_of attrs in
6577 let path = unent pathent
6578 and pageno = fromstring int_of_string spos "page" spage 0
6579 and rely = fromstring float_of_string spos "rely" srely 0.0
6580 and pan = fromstring int_of_string spos "pan" span 0
6581 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6582 let c = config_of dc attrs in
6583 let anchor = (pageno, rely, visy) in
6584 if closed
6585 then (Hashtbl.add h path (c, [], pan, anchor); v)
6586 else { v with f = doc path pan anchor c [] }
6588 | Vopen _ ->
6589 error "unexpected subelement in llppconfig" s spos
6591 | Vclose "llppconfig" -> { v with f = toplevel }
6592 | Vclose _ -> error "unexpected close in llppconfig" s spos
6594 and defaults v t spos _ =
6595 match t with
6596 | Vdata | Vcdata -> v
6597 | Vend -> error "unexpected end of input in defaults" s spos
6598 | Vopen ("keymap", attrs, closed) ->
6599 let modename =
6600 try List.assoc "mode" attrs
6601 with Not_found -> "global" in
6602 if closed
6603 then v
6604 else
6605 let ret keymap =
6606 let h = findkeyhash dc modename in
6607 KeyMap.iter (Hashtbl.replace h) keymap;
6608 defaults
6610 { v with f = pkeymap ret KeyMap.empty }
6612 | Vopen (_, _, _) ->
6613 error "unexpected subelement in defaults" s spos
6615 | Vclose "defaults" ->
6616 { v with f = llppconfig }
6618 | Vclose _ -> error "unexpected close in defaults" s spos
6620 and uifont b v t spos epos =
6621 match t with
6622 | Vdata | Vcdata ->
6623 Buffer.add_substring b s spos (epos - spos);
6625 | Vopen (_, _, _) ->
6626 error "unexpected subelement in ui-font" s spos
6627 | Vclose "ui-font" ->
6628 if String.length !fontpath = 0
6629 then fontpath := Buffer.contents b;
6630 { v with f = llppconfig }
6631 | Vclose _ -> error "unexpected close in ui-font" s spos
6632 | Vend -> error "unexpected end of input in ui-font" s spos
6634 and doc path pan anchor c bookmarks v t spos _ =
6635 match t with
6636 | Vdata | Vcdata -> v
6637 | Vend -> error "unexpected end of input in doc" s spos
6638 | Vopen ("bookmarks", _, closed) ->
6639 if closed
6640 then v
6641 else { v with f = pbookmarks path pan anchor c bookmarks }
6643 | Vopen ("keymap", attrs, closed) ->
6644 let modename =
6645 try List.assoc "mode" attrs
6646 with Not_found -> "global"
6648 if closed
6649 then v
6650 else
6651 let ret keymap =
6652 let h = findkeyhash c modename in
6653 KeyMap.iter (Hashtbl.replace h) keymap;
6654 doc path pan anchor c bookmarks
6656 { v with f = pkeymap ret KeyMap.empty }
6658 | Vopen (_, _, _) ->
6659 error "unexpected subelement in doc" s spos
6661 | Vclose "doc" ->
6662 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6663 { v with f = llppconfig }
6665 | Vclose _ -> error "unexpected close in doc" s spos
6667 and pkeymap ret keymap v t spos _ =
6668 match t with
6669 | Vdata | Vcdata -> v
6670 | Vend -> error "unexpected end of input in keymap" s spos
6671 | Vopen ("map", attrs, closed) ->
6672 let r, l = map_of attrs in
6673 let kss = fromstring keys_of_string spos "in" r [] in
6674 let lss = fromstring keys_of_string spos "out" l [] in
6675 let keymap =
6676 match kss with
6677 | [] -> keymap
6678 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6679 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6681 if closed
6682 then { v with f = pkeymap ret keymap }
6683 else
6684 let f () = v in
6685 { v with f = skip "map" f }
6687 | Vopen _ ->
6688 error "unexpected subelement in keymap" s spos
6690 | Vclose "keymap" ->
6691 { v with f = ret keymap }
6693 | Vclose _ -> error "unexpected close in keymap" s spos
6695 and pbookmarks path pan anchor c bookmarks v t spos _ =
6696 match t with
6697 | Vdata | Vcdata -> v
6698 | Vend -> error "unexpected end of input in bookmarks" s spos
6699 | Vopen ("item", attrs, closed) ->
6700 let titleent, spage, srely, svisy = bookmark_of attrs in
6701 let page = fromstring int_of_string spos "page" spage 0
6702 and rely = fromstring float_of_string spos "rely" srely 0.0
6703 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6704 let bookmarks =
6705 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6707 if closed
6708 then { v with f = pbookmarks path pan anchor c bookmarks }
6709 else
6710 let f () = v in
6711 { v with f = skip "item" f }
6713 | Vopen _ ->
6714 error "unexpected subelement in bookmarks" s spos
6716 | Vclose "bookmarks" ->
6717 { v with f = doc path pan anchor c bookmarks }
6719 | Vclose _ -> error "unexpected close in bookmarks" s spos
6721 and skip tag f v t spos _ =
6722 match t with
6723 | Vdata | Vcdata -> v
6724 | Vend ->
6725 error ("unexpected end of input in skipped " ^ tag) s spos
6726 | Vopen (tag', _, closed) ->
6727 if closed
6728 then v
6729 else
6730 let f' () = { v with f = skip tag f } in
6731 { v with f = skip tag' f' }
6732 | Vclose ctag ->
6733 if tag = ctag
6734 then f ()
6735 else error ("unexpected close in skipped " ^ tag) s spos
6738 parse { f = toplevel; accu = () } s;
6739 h, dc;
6742 let do_load f ic =
6744 let len = in_channel_length ic in
6745 let s = String.create len in
6746 really_input ic s 0 len;
6747 f s;
6748 with
6749 | Parse_error (msg, s, pos) ->
6750 let subs = subs s pos in
6751 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6752 failwith ("parse error: " ^ s)
6754 | exn ->
6755 failwith ("config load error: " ^ exntos exn)
6758 let defconfpath =
6759 let dir =
6761 let dir = Filename.concat home ".config" in
6762 if Sys.is_directory dir then dir else home
6763 with _ -> home
6765 Filename.concat dir "llpp.conf"
6768 let confpath = ref defconfpath;;
6770 let load1 f =
6771 if Sys.file_exists !confpath
6772 then
6773 match
6774 (try Some (open_in_bin !confpath)
6775 with exn ->
6776 prerr_endline
6777 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6778 exntos exn);
6779 None
6781 with
6782 | Some ic ->
6783 let success =
6785 f (do_load get ic)
6786 with exn ->
6787 prerr_endline
6788 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6789 exntos exn);
6790 false
6792 close_in ic;
6793 success
6795 | None -> false
6796 else
6797 f (Hashtbl.create 0, defconf)
6800 let load () =
6801 let f (h, dc) =
6802 let pc, pb, px, pa =
6804 let key =
6805 if String.length state.origin = 0
6806 then state.path
6807 else state.origin
6809 Hashtbl.find h (Filename.basename key)
6810 with Not_found -> dc, [], 0, emptyanchor
6812 setconf defconf dc;
6813 setconf conf pc;
6814 state.bookmarks <- pb;
6815 state.x <- px;
6816 state.scrollw <- conf.scrollbw;
6817 if conf.jumpback
6818 then state.anchor <- pa;
6819 cbput state.hists.nav pa;
6820 true
6822 load1 f
6825 let add_attrs bb always dc c =
6826 let ob s a b =
6827 if always || a != b
6828 then Printf.bprintf bb "\n %s='%b'" s a
6829 and oi s a b =
6830 if always || a != b
6831 then Printf.bprintf bb "\n %s='%d'" s a
6832 and oI s a b =
6833 if always || a != b
6834 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6835 and oz s a b =
6836 if always || a <> b
6837 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6838 and oF s a b =
6839 if always || a <> b
6840 then Printf.bprintf bb "\n %s='%f'" s a
6841 and oc s a b =
6842 if always || a <> b
6843 then
6844 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6845 and oC s a b =
6846 if always || a <> b
6847 then
6848 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6849 and oR s a b =
6850 if always || a <> b
6851 then
6852 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6853 and os s a b =
6854 if always || a <> b
6855 then
6856 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6857 and og s a b =
6858 if always || a <> b
6859 then
6860 match a with
6861 | None -> ()
6862 | Some (_N, _A, _B) ->
6863 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6864 and oW s a b =
6865 if always || a <> b
6866 then
6867 let v =
6868 match a with
6869 | None -> "false"
6870 | Some f ->
6871 if f = infinity
6872 then "true"
6873 else string_of_float f
6875 Printf.bprintf bb "\n %s='%s'" s v
6876 and oco s a b =
6877 if always || a <> b
6878 then
6879 match a with
6880 | Cmulti ((n, a, b), _) when n > 1 ->
6881 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6882 | Csplit (n, _) when n > 1 ->
6883 Printf.bprintf bb "\n %s='%d'" s ~-n
6884 | _ -> ()
6885 and obeco s a b =
6886 if always || a <> b
6887 then
6888 match a with
6889 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6890 | _ -> ()
6891 and oFm s a b =
6892 if always || a <> b
6893 then
6894 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6896 oi "width" c.cwinw dc.cwinw;
6897 oi "height" c.cwinh dc.cwinh;
6898 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6899 oi "scroll-handle-height" c.scrollh dc.scrollh;
6900 ob "case-insensitive-search" c.icase dc.icase;
6901 ob "preload" c.preload dc.preload;
6902 oi "page-bias" c.pagebias dc.pagebias;
6903 oi "scroll-step" c.scrollstep dc.scrollstep;
6904 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6905 ob "max-height-fit" c.maxhfit dc.maxhfit;
6906 ob "crop-hack" c.crophack dc.crophack;
6907 oW "throttle" c.maxwait dc.maxwait;
6908 ob "highlight-links" c.hlinks dc.hlinks;
6909 ob "under-cursor-info" c.underinfo dc.underinfo;
6910 oi "vertical-margin" c.interpagespace dc.interpagespace;
6911 oz "zoom" c.zoom dc.zoom;
6912 ob "presentation" c.presentation dc.presentation;
6913 oi "rotation-angle" c.angle dc.angle;
6914 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6915 oFm "fit-model" c.fitmodel dc.fitmodel;
6916 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6917 oi "tex-count" c.texcount dc.texcount;
6918 oi "slice-height" c.sliceheight dc.sliceheight;
6919 oi "thumbnail-width" c.thumbw dc.thumbw;
6920 ob "persistent-location" c.jumpback dc.jumpback;
6921 oc "background-color" c.bgcolor dc.bgcolor;
6922 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6923 oi "tile-width" c.tilew dc.tilew;
6924 oi "tile-height" c.tileh dc.tileh;
6925 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6926 ob "checkers" c.checkers dc.checkers;
6927 oi "aalevel" c.aalevel dc.aalevel;
6928 ob "trim-margins" c.trimmargins dc.trimmargins;
6929 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6930 os "uri-launcher" c.urilauncher dc.urilauncher;
6931 os "path-launcher" c.pathlauncher dc.pathlauncher;
6932 oC "color-space" c.colorspace dc.colorspace;
6933 ob "invert-colors" c.invert dc.invert;
6934 oF "brightness" c.colorscale dc.colorscale;
6935 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6936 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6937 oco "columns" c.columns dc.columns;
6938 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6939 os "selection-command" c.selcmd dc.selcmd;
6940 os "synctex-command" c.stcmd dc.stcmd;
6941 ob "update-cursor" c.updatecurs dc.updatecurs;
6942 oi "hint-font-size" c.hfsize dc.hfsize;
6943 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6944 oF "page-scroll-scale" c.pgscale dc.pgscale;
6945 ob "use-pbo" c.usepbo dc.usepbo;
6946 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6949 let keymapsbuf always dc c =
6950 let bb = Buffer.create 16 in
6951 let rec loop = function
6952 | [] -> ()
6953 | (modename, h) :: rest ->
6954 let dh = findkeyhash dc modename in
6955 if always || h <> dh
6956 then (
6957 if Hashtbl.length h > 0
6958 then (
6959 if Buffer.length bb > 0
6960 then Buffer.add_char bb '\n';
6961 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6962 Hashtbl.iter (fun i o ->
6963 let isdifferent = always ||
6965 let dO = Hashtbl.find dh i in
6966 dO <> o
6967 with Not_found -> true
6969 if isdifferent
6970 then
6971 let addkm (k, m) =
6972 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6973 if Wsi.withalt m then Buffer.add_string bb "alt-";
6974 if Wsi.withshift m then Buffer.add_string bb "shift-";
6975 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6976 Buffer.add_string bb (Wsi.keyname k);
6978 let addkms l =
6979 let rec loop = function
6980 | [] -> ()
6981 | km :: [] -> addkm km
6982 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6984 loop l
6986 Buffer.add_string bb "<map in='";
6987 addkm i;
6988 match o with
6989 | KMinsrt km ->
6990 Buffer.add_string bb "' out='";
6991 addkm km;
6992 Buffer.add_string bb "'/>\n"
6994 | KMinsrl kms ->
6995 Buffer.add_string bb "' out='";
6996 addkms kms;
6997 Buffer.add_string bb "'/>\n"
6999 | KMmulti (ins, kms) ->
7000 Buffer.add_char bb ' ';
7001 addkms ins;
7002 Buffer.add_string bb "' out='";
7003 addkms kms;
7004 Buffer.add_string bb "'/>\n"
7005 ) h;
7006 Buffer.add_string bb "</keymap>";
7009 loop rest
7011 loop c.keyhashes;
7015 let save () =
7016 let uifontsize = fstate.fontsize in
7017 let bb = Buffer.create 32768 in
7018 let w, h, cx =
7019 List.fold_left
7020 (fun (w, h, _) ws ->
7021 match ws with
7022 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, conf.cx)
7023 | Wsi.MaxVert -> (w, conf.cwinh, conf.cx)
7024 | Wsi.MaxHorz -> (conf.cwinw, h, conf.cx)
7026 (state.winw, state.winh, state.x) state.winstate
7028 conf.cwinw <- w;
7029 conf.cwinh <- h;
7030 let f (h, dc) =
7031 let dc = if conf.bedefault then conf else dc in
7032 Buffer.add_string bb "<llppconfig>\n";
7034 if String.length !fontpath > 0
7035 then
7036 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7037 uifontsize
7038 !fontpath
7039 else (
7040 if uifontsize <> 14
7041 then
7042 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7045 Buffer.add_string bb "<defaults ";
7046 add_attrs bb true dc dc;
7047 let kb = keymapsbuf true dc dc in
7048 if Buffer.length kb > 0
7049 then (
7050 Buffer.add_string bb ">\n";
7051 Buffer.add_buffer bb kb;
7052 Buffer.add_string bb "\n</defaults>\n";
7054 else Buffer.add_string bb "/>\n";
7056 let adddoc path pan anchor c bookmarks =
7057 if bookmarks == [] && c = dc && anchor = emptyanchor
7058 then ()
7059 else (
7060 Printf.bprintf bb "<doc path='%s'"
7061 (enent path 0 (String.length path));
7063 if anchor <> emptyanchor
7064 then (
7065 let n, rely, visy = anchor in
7066 Printf.bprintf bb " page='%d'" n;
7067 if rely > 1e-6
7068 then
7069 Printf.bprintf bb " rely='%f'" rely
7071 if abs_float visy > 1e-6
7072 then
7073 Printf.bprintf bb " visy='%f'" visy
7077 if pan != 0
7078 then Printf.bprintf bb " pan='%d'" pan;
7080 add_attrs bb false dc c;
7081 let kb = keymapsbuf false dc c in
7083 begin match bookmarks with
7084 | [] ->
7085 if Buffer.length kb > 0
7086 then (
7087 Buffer.add_string bb ">\n";
7088 Buffer.add_buffer bb kb;
7089 Buffer.add_string bb "\n</doc>\n";
7091 else Buffer.add_string bb "/>\n"
7092 | _ ->
7093 Buffer.add_string bb ">\n<bookmarks>\n";
7094 List.iter (fun (title, _level, (page, rely, visy)) ->
7095 Printf.bprintf bb
7096 "<item title='%s' page='%d'"
7097 (enent title 0 (String.length title))
7098 page
7100 if rely > 1e-6
7101 then
7102 Printf.bprintf bb " rely='%f'" rely
7104 if abs_float visy > 1e-6
7105 then
7106 Printf.bprintf bb " visy='%f'" visy
7108 Buffer.add_string bb "/>\n";
7109 ) bookmarks;
7110 Buffer.add_string bb "</bookmarks>";
7111 if Buffer.length kb > 0
7112 then (
7113 Buffer.add_string bb "\n";
7114 Buffer.add_buffer bb kb;
7116 Buffer.add_string bb "\n</doc>\n";
7117 end;
7121 let pan, conf =
7122 match state.mode with
7123 | Birdseye (c, pan, _, _, _) ->
7124 let beyecolumns =
7125 match conf.columns with
7126 | Cmulti ((c, _, _), _) -> Some c
7127 | Csingle _ -> None
7128 | Csplit _ -> None
7129 and columns =
7130 match c.columns with
7131 | Cmulti (c, _) -> Cmulti (c, [||])
7132 | Csingle _ -> Csingle [||]
7133 | Csplit _ -> failwith "quit from bird's eye while split"
7135 pan, { c with beyecolumns = beyecolumns; columns = columns }
7136 | _ -> cx, conf
7138 let basename = Filename.basename
7139 (if String.length state.origin = 0 then state.path else state.origin)
7141 adddoc basename pan (getanchor ())
7142 (let conf =
7143 let autoscrollstep =
7144 match state.autoscroll with
7145 | Some step -> step
7146 | None -> conf.autoscrollstep
7148 match state.mode with
7149 | Birdseye (bc, _, _, _, _) ->
7150 { conf with
7151 zoom = bc.zoom;
7152 presentation = bc.presentation;
7153 interpagespace = bc.interpagespace;
7154 maxwait = bc.maxwait;
7155 autoscrollstep = autoscrollstep }
7156 | _ -> { conf with autoscrollstep = autoscrollstep }
7157 in conf)
7158 (if conf.savebmarks then state.bookmarks else []);
7160 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7161 if basename <> path
7162 then adddoc path x anchor c bookmarks
7163 ) h;
7164 Buffer.add_string bb "</llppconfig>\n";
7165 true;
7167 if load1 f && Buffer.length bb > 0
7168 then
7170 let tmp = !confpath ^ ".tmp" in
7171 let oc = open_out_bin tmp in
7172 Buffer.output_buffer oc bb;
7173 close_out oc;
7174 Unix.rename tmp !confpath;
7175 with exn ->
7176 prerr_endline
7177 ("error while saving configuration: " ^ exntos exn)
7179 end;;
7181 let adderrmsg src msg =
7182 Buffer.add_string state.errmsgs msg;
7183 state.newerrmsgs <- true;
7184 G.postRedisplay src
7187 let adderrfmt src fmt =
7188 Format.kprintf (fun s -> adderrmsg src s) fmt;
7191 let ract cmds =
7192 let cl = splitatspace cmds in
7193 let scan s fmt f =
7194 try Scanf.sscanf s fmt f
7195 with exn ->
7196 adderrfmt "remote exec"
7197 "error processing '%S': %s\n" cmds (exntos exn)
7199 match cl with
7200 | "reload" :: [] -> reload ()
7201 | "goto" :: args :: [] ->
7202 scan args "%u %f %f"
7203 (fun pageno x y ->
7204 let cmd, _ = state.geomcmds in
7205 if String.length cmd = 0
7206 then gotopagexy pageno x y
7207 else
7208 let f prevf () =
7209 gotopagexy pageno x y;
7210 prevf ()
7212 state.reprf <- f state.reprf
7214 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7215 | "rect" :: args :: [] ->
7216 scan args "%u %u %f %f %f %f"
7217 (fun pageno color x0 y0 x1 y1 ->
7218 onpagerect pageno (fun w h ->
7219 let _,w1,h1,_ = getpagedim pageno in
7220 let sw = float w1 /. w
7221 and sh = float h1 /. h in
7222 let x0s = x0 *. sw
7223 and x1s = x1 *. sw
7224 and y0s = y0 *. sh
7225 and y1s = y1 *. sh in
7226 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7227 debugrect rect;
7228 state.rects <- (pageno, color, rect) :: state.rects;
7229 G.postRedisplay "rect";
7232 | "activatewin" :: [] -> Wsi.activatewin ()
7233 | "quit" :: [] -> raise Quit
7234 | _ ->
7235 adderrfmt "remote command"
7236 "error processing remote command: %S\n" cmds;
7239 let remote =
7240 let scratch = String.create 80 in
7241 let buf = Buffer.create 80 in
7242 fun fd ->
7243 let rec tempfr () =
7244 try Some (Unix.read fd scratch 0 80)
7245 with
7246 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7247 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7248 | exn -> raise exn
7250 match tempfr () with
7251 | None -> Some fd
7252 | Some n ->
7253 if n = 0
7254 then (
7255 Unix.close fd;
7256 if Buffer.length buf > 0
7257 then (
7258 let s = Buffer.contents buf in
7259 Buffer.clear buf;
7260 ract s;
7262 None
7264 else
7265 let rec eat ppos =
7266 let nlpos =
7268 let pos = String.index_from scratch ppos '\n' in
7269 if pos >= n then -1 else pos
7270 with Not_found -> -1
7272 if nlpos >= 0
7273 then (
7274 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7275 let s = Buffer.contents buf in
7276 Buffer.clear buf;
7277 ract s;
7278 eat (nlpos+1);
7280 else (
7281 Buffer.add_substring buf scratch ppos (n-ppos);
7282 Some fd
7284 in eat 0
7287 let remoteopen path =
7288 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7289 with exn ->
7290 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7291 None
7294 let () =
7295 let trimcachepath = ref "" in
7296 let rcmdpath = ref "" in
7297 Arg.parse
7298 (Arg.align
7299 [("-p", Arg.String (fun s -> state.password <- s),
7300 "<password> Set password");
7302 ("-f", Arg.String (fun s -> Config.fontpath := s),
7303 "<path> Set path to the user interface font");
7305 ("-c", Arg.String (fun s -> Config.confpath := s),
7306 "<path> Set path to the configuration file");
7308 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7309 "<path> Set path to the trim cache file");
7311 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7312 "<named-destination> Set named destination");
7314 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7316 ("-remote", Arg.String (fun s -> rcmdpath := s),
7317 "<path> Set path to the remote commands source");
7319 ("-origin", Arg.String (fun s -> state.origin <- s),
7320 "<original path> Set original path");
7322 ("-v", Arg.Unit (fun () ->
7323 Printf.printf
7324 "%s\nconfiguration path: %s\n"
7325 (version ())
7326 Config.defconfpath
7328 exit 0), " Print version and exit");
7331 (fun s -> state.path <- s)
7332 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7334 if String.length state.path = 0
7335 then (prerr_endline "file name missing"; exit 1);
7337 if not (Config.load ())
7338 then prerr_endline "failed to load configuration";
7340 let globalkeyhash = findkeyhash conf "global" in
7341 let wsfd, winw, winh = Wsi.init (object
7342 method expose =
7343 if nogeomcmds state.geomcmds || platform == Posx
7344 then display ()
7345 else (
7346 GlClear.color (scalecolor2 conf.bgcolor);
7347 GlClear.clear [`color];
7349 method display = display ()
7350 method reshape w h = reshape w h
7351 method mouse b d x y m = mouse b d x y m
7352 method motion x y = state.mpos <- (x, y); motion x y
7353 method pmotion x y = state.mpos <- (x, y); pmotion x y
7354 method key k m =
7355 let mascm = m land (
7356 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7357 ) in
7358 match state.keystate with
7359 | KSnone ->
7360 let km = k, mascm in
7361 begin
7362 match
7363 let modehash = state.uioh#modehash in
7364 try Hashtbl.find modehash km
7365 with Not_found ->
7366 try Hashtbl.find globalkeyhash km
7367 with Not_found -> KMinsrt (k, m)
7368 with
7369 | KMinsrt (k, m) -> keyboard k m
7370 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7371 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7373 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7374 List.iter (fun (k, m) -> keyboard k m) insrt;
7375 state.keystate <- KSnone
7376 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7377 state.keystate <- KSinto (keys, insrt)
7378 | _ ->
7379 state.keystate <- KSnone
7381 method enter x y = state.mpos <- (x, y); pmotion x y
7382 method leave = state.mpos <- (-1, -1)
7383 method winstate wsl =
7384 if List.exists
7385 (function | Wsi.MaxVert | Wsi.MaxHorz | Wsi.Fullscreen -> true) wsl
7386 then conf.cx <- state.x;
7387 state.winstate <- wsl
7388 method quit = raise Quit
7389 end) conf.cwinw conf.cwinh (platform = Posx) in
7391 state.wsfd <- wsfd;
7393 if not (
7394 List.exists GlMisc.check_extension
7395 [ "GL_ARB_texture_rectangle"
7396 ; "GL_EXT_texture_recangle"
7397 ; "GL_NV_texture_rectangle" ]
7399 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7401 let cr, sw =
7402 match Ne.pipe () with
7403 | Ne.Exn exn ->
7404 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7405 exit 1
7406 | Ne.Res rw -> rw
7407 and sr, cw =
7408 match Ne.pipe () with
7409 | Ne.Exn exn ->
7410 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7411 exit 1
7412 | Ne.Res rw -> rw
7415 cloexec cr;
7416 cloexec sw;
7417 cloexec sr;
7418 cloexec cw;
7420 setcheckers conf.checkers;
7421 redirectstderr ();
7423 init (cr, cw) (
7424 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7425 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7426 !Config.fontpath, !trimcachepath,
7427 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7429 state.sr <- sr;
7430 state.sw <- sw;
7431 state.text <- "Opening " ^ (mbtoutf8 state.path);
7432 reshape winw winh;
7433 opendoc state.path state.password;
7434 state.uioh <- uioh;
7436 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7437 let optrfd =
7438 ref (
7439 if String.length !rcmdpath > 0
7440 then remoteopen !rcmdpath
7441 else None
7445 let rec loop deadline =
7446 let r =
7447 match state.errfd with
7448 | None -> [state.sr; state.wsfd]
7449 | Some fd -> [state.sr; state.wsfd; fd]
7451 let r =
7452 match !optrfd with
7453 | None -> r
7454 | Some fd -> fd :: r
7456 if state.redisplay
7457 then (
7458 state.redisplay <- false;
7459 display ();
7461 let timeout =
7462 let now = now () in
7463 if deadline > now
7464 then (
7465 if deadline = infinity
7466 then ~-.1.0
7467 else max 0.0 (deadline -. now)
7469 else 0.0
7471 let r, _, _ =
7472 try Unix.select r [] [] timeout
7473 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7475 begin match r with
7476 | [] ->
7477 state.ghyll None;
7478 let newdeadline =
7479 if state.ghyll == noghyll
7480 then
7481 match state.autoscroll with
7482 | Some step when step != 0 ->
7483 let y = state.y + step in
7484 let y =
7485 if y < 0
7486 then state.maxy
7487 else if y >= state.maxy then 0 else y
7489 gotoy y;
7490 if state.mode = View
7491 then state.text <- "";
7492 deadline +. 0.01
7493 | _ -> infinity
7494 else deadline +. 0.01
7496 loop newdeadline
7498 | l ->
7499 let rec checkfds = function
7500 | [] -> ()
7501 | fd :: rest when fd = state.sr ->
7502 let cmd = readcmd state.sr in
7503 act cmd;
7504 checkfds rest
7506 | fd :: rest when fd = state.wsfd ->
7507 Wsi.readresp fd;
7508 checkfds rest
7510 | fd :: rest when Some fd = !optrfd ->
7511 begin match remote fd with
7512 | None -> optrfd := remoteopen !rcmdpath;
7513 | opt -> optrfd := opt
7514 end;
7515 checkfds rest
7517 | fd :: rest ->
7518 let s = String.create 80 in
7519 let n = tempfailureretry (Unix.read fd s 0) 80 in
7520 if conf.redirectstderr
7521 then (
7522 Buffer.add_substring state.errmsgs s 0 n;
7523 state.newerrmsgs <- true;
7524 state.redisplay <- true;
7526 else (
7527 prerr_string (String.sub s 0 n);
7528 flush stderr;
7530 checkfds rest
7532 checkfds l;
7533 let newdeadline =
7534 let deadline1 =
7535 if deadline = infinity
7536 then now () +. 0.01
7537 else deadline
7539 match state.autoscroll with
7540 | Some step when step != 0 -> deadline1
7541 | _ -> if state.ghyll == noghyll then infinity else deadline1
7543 loop newdeadline
7544 end;
7547 loop infinity;
7548 with Quit ->
7549 Config.save ();