Special zoomrect handling for page fitmodel + split columns
[llpp.git] / main.ml
blob9292b3075d6bf96b2fecfbaeb2dfc88a5fdd16fe
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 savebmarks : bool
316 ; mutable fitmodel : fitmodel
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 method eformsgs : bool
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 method eformsgs = false
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor * string) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
484 ; mutable origin : string
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; cwinw = 900
515 ; cwinh = 900
516 ; savebmarks = true
517 ; fitmodel = FitProportional
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let geturl s =
602 let colonpos = try String.index s ':' with Not_found -> -1 in
603 let len = String.length s in
604 if colonpos >= 0 && colonpos + 3 < len
605 then (
606 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
607 then
608 let schemestartpos =
609 try String.rindex_from s colonpos ' '
610 with Not_found -> -1
612 let scheme =
613 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
615 match scheme with
616 | "http" | "ftp" | "mailto" ->
617 let epos =
618 try String.index_from s colonpos ' '
619 with Not_found -> len
621 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
622 | _ -> ""
623 else ""
625 else ""
628 let gotouri uri =
629 if String.length conf.urilauncher = 0
630 then print_endline uri
631 else (
632 let url = geturl uri in
633 if String.length url = 0
634 then print_endline uri
635 else
636 let re = Str.regexp "%s" in
637 let command = Str.global_replace re url conf.urilauncher in
638 try popen command []
639 with exn ->
640 Printf.eprintf
641 "failed to execute `%s': %s\n" command (exntos exn);
642 flush stderr;
646 let version () =
647 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
648 (platform_to_string platform) Sys.word_size Sys.ocaml_version
651 let makehelp () =
652 let strings = version () :: "" :: Help.keys in
653 Array.of_list (
654 List.map (fun s ->
655 let url = geturl s in
656 if String.length url > 0
657 then (s, 0, Action (fun u -> gotouri url; u))
658 else (s, 0, Noaction)
659 ) strings);
662 let noghyll _ = ();;
663 let firstgeomcmds = "", [];;
664 let noreprf () = ();;
666 let state =
667 { sr = Unix.stdin
668 ; sw = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; errfd = None
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; scrollw = 0
678 ; hscrollh = 0
679 ; anchor = emptyanchor
680 ; ranchors = []
681 ; layout = []
682 ; maxy = max_int
683 ; tilelru = Queue.create ()
684 ; pagemap = Hashtbl.create 10
685 ; tilemap = Hashtbl.create 10
686 ; pdims = []
687 ; pagecount = 0
688 ; currently = Idle
689 ; mstate = Mnone
690 ; rects = []
691 ; rects1 = []
692 ; text = ""
693 ; mode = View
694 ; winstate = []
695 ; searchpattern = ""
696 ; outlines = [||]
697 ; bookmarks = []
698 ; path = ""
699 ; password = ""
700 ; nameddest = ""
701 ; geomcmds = firstgeomcmds
702 ; hists =
703 { nav = cbnew 10 emptyanchor
704 ; pat = cbnew 10 ""
705 ; pag = cbnew 10 ""
706 ; sel = cbnew 10 ""
708 ; memused = 0
709 ; gen = 0
710 ; throttle = None
711 ; autoscroll = None
712 ; ghyll = noghyll
713 ; help = makehelp ()
714 ; docinfo = []
715 ; texid = None
716 ; prevzoom = 1.0
717 ; progress = -1.0
718 ; uioh = nouioh
719 ; redisplay = true
720 ; mpos = (-1, -1)
721 ; keystate = KSnone
722 ; glinks = false
723 ; prevcolumns = None
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
727 ; origin = ""
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
754 flush stderr;
758 module Ne = struct
759 type 'a t = | Res of 'a | Exn of exn;;
761 let pipe () =
762 try Res (Unix.pipe ())
763 with exn -> Exn exn
766 let clo fd f =
767 try tempfailureretry Unix.close fd
768 with exn -> f (exntos exn)
771 let dup fd =
772 try Res (tempfailureretry Unix.dup fd)
773 with exn -> Exn exn
776 let dup2 fd1 fd2 =
777 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
778 with exn -> Exn exn
780 end;;
782 let redirectstderr () =
783 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
784 if conf.redirectstderr
785 then
786 match Ne.pipe () with
787 | Ne.Exn exn ->
788 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
790 | Ne.Res (r, w) ->
791 begin match Ne.dup Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup stderr: %s" (exntos exn);
794 Ne.clo r (clofail "pipe/r");
795 Ne.clo w (clofail "pipe/w");
797 | Ne.Res dupstderr ->
798 begin match Ne.dup2 w Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 to stderr: %s" (exntos exn);
801 Ne.clo dupstderr (clofail "stderr duplicate");
802 Ne.clo r (clofail "redir pipe/r");
803 Ne.clo w (clofail "redir pipe/w");
805 | Ne.Res () ->
806 state.stderr <- dupstderr;
807 state.errfd <- Some r;
808 end;
810 else (
811 state.newerrmsgs <- false;
812 begin match state.errfd with
813 | Some fd ->
814 begin match Ne.dup2 state.stderr Unix.stderr with
815 | Ne.Exn exn ->
816 dolog "failed to dup2 original stderr: %s" (exntos exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let selstring s =
903 match Ne.pipe () with
904 | Ne.Exn exn ->
905 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
906 | Ne.Res (r, w) ->
907 let popened =
908 try popen conf.selcmd [r, 0; w, -1]; true
909 with exn ->
910 showtext '!'
911 (Printf.sprintf "failed to execute %s: %s"
912 conf.selcmd (exntos exn));
913 false
915 let clo cap fd =
916 Ne.clo fd (fun msg ->
917 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
920 if popened
921 then
922 (try
923 let l = String.length s in
924 let n = tempfailureretry (Unix.write w s 0) l in
925 if n != l
926 then
927 showtext '!'
928 (Printf.sprintf
929 "failed to write %d characters to sel pipe, wrote %d"
932 with exn ->
933 showtext '!'
934 (Printf.sprintf "failed to write to sel pipe: %s"
935 (exntos exn)
938 else dolog "%s" s;
939 clo "pipe/r" r;
940 clo "pipe/w" w;
943 let undertext = function
944 | Unone -> "none"
945 | Ulinkuri s -> s
946 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
947 | Utext s -> "font: " ^ s
948 | Uunexpected s -> "unexpected: " ^ s
949 | Ulaunch s -> "launch: " ^ s
950 | Unamed s -> "named: " ^ s
951 | Uremote (filename, pageno) ->
952 Printf.sprintf "%s: page %d" filename (pageno+1)
955 let updateunder x y =
956 match getunder x y with
957 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
958 | Ulinkuri uri ->
959 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
960 Wsi.setcursor Wsi.CURSOR_INFO
961 | Ulinkgoto (pageno, _) ->
962 if conf.underinfo
963 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
964 Wsi.setcursor Wsi.CURSOR_INFO
965 | Utext s ->
966 if conf.underinfo then showtext 'f' ("ont: " ^ s);
967 Wsi.setcursor Wsi.CURSOR_TEXT
968 | Uunexpected s ->
969 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
970 Wsi.setcursor Wsi.CURSOR_INHERIT
971 | Ulaunch s ->
972 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
973 Wsi.setcursor Wsi.CURSOR_INHERIT
974 | Unamed s ->
975 if conf.underinfo then showtext 'n' ("amed: " ^ s);
976 Wsi.setcursor Wsi.CURSOR_INHERIT
977 | Uremote (filename, pageno) ->
978 if conf.underinfo then showtext 'r'
979 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
980 Wsi.setcursor Wsi.CURSOR_INFO
983 let showlinktype under =
984 if conf.underinfo
985 then
986 match under with
987 | Unone -> ()
988 | under ->
989 let s = undertext under in
990 showtext ' ' s
993 let addchar s c =
994 let b = Buffer.create (String.length s + 1) in
995 Buffer.add_string b s;
996 Buffer.add_char b c;
997 Buffer.contents b;
1000 let colorspace_of_string s =
1001 match String.lowercase s with
1002 | "rgb" -> Rgb
1003 | "bgr" -> Bgr
1004 | "gray" -> Gray
1005 | _ -> failwith "invalid colorspace"
1008 let int_of_colorspace = function
1009 | Rgb -> 0
1010 | Bgr -> 1
1011 | Gray -> 2
1014 let colorspace_of_int = function
1015 | 0 -> Rgb
1016 | 1 -> Bgr
1017 | 2 -> Gray
1018 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1021 let colorspace_to_string = function
1022 | Rgb -> "rgb"
1023 | Bgr -> "bgr"
1024 | Gray -> "gray"
1027 let fitmodel_of_string s =
1028 match String.lowercase s with
1029 | "width" -> FitWidth
1030 | "proportional" -> FitProportional
1031 | "page" -> FitPage
1032 | _ -> failwith "invalid fit model"
1035 let int_of_fitmodel = function
1036 | FitWidth -> 0
1037 | FitProportional -> 1
1038 | FitPage -> 2
1041 let fitmodel_of_int = function
1042 | 0 -> FitWidth
1043 | 1 -> FitProportional
1044 | 2 -> FitPage
1045 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1048 let fitmodel_to_string = function
1049 | FitWidth -> "width"
1050 | FitProportional -> "proportional"
1051 | FitPage -> "page"
1054 let intentry_with_suffix text key =
1055 let c =
1056 if key >= 32 && key < 127
1057 then Char.chr key
1058 else '\000'
1060 match Char.lowercase c with
1061 | '0' .. '9' ->
1062 let text = addchar text c in
1063 TEcont text
1065 | 'k' | 'm' | 'g' ->
1066 let text = addchar text c in
1067 TEcont text
1069 | _ ->
1070 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1071 TEcont text
1074 let multicolumns_to_string (n, a, b) =
1075 if a = 0 && b = 0
1076 then Printf.sprintf "%d" n
1077 else Printf.sprintf "%d,%d,%d" n a b;
1080 let multicolumns_of_string s =
1082 (int_of_string s, 0, 0)
1083 with _ ->
1084 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1085 if a > 1 || b > 1
1086 then failwith "subtly broken"; (n, a, b)
1090 let readcmd fd =
1091 let s = "xxxx" in
1092 let n = tempfailureretry (Unix.read fd s 0) 4 in
1093 if n != 4 then failwith "incomplete read(len)";
1094 let len = 0
1095 lor (Char.code s.[0] lsl 24)
1096 lor (Char.code s.[1] lsl 16)
1097 lor (Char.code s.[2] lsl 8)
1098 lor (Char.code s.[3] lsl 0)
1100 let s = String.create len in
1101 let n = tempfailureretry (Unix.read fd s 0) len in
1102 if n != len then failwith "incomplete read(data)";
1106 let btod b = if b then 1 else 0;;
1108 let wcmd fmt =
1109 let b = Buffer.create 16 in
1110 Buffer.add_string b "llll";
1111 Printf.kbprintf
1112 (fun b ->
1113 let s = Buffer.contents b in
1114 let n = String.length s in
1115 let len = n - 4 in
1116 (* dolog "wcmd %S" (String.sub s 4 len); *)
1117 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1118 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1119 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1120 s.[3] <- Char.chr (len land 0xff);
1121 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1122 if n' != n then failwith "write failed";
1123 ) b fmt;
1126 let calcips h =
1127 let d = state.winh - h in
1128 max conf.interpagespace ((d + 1) / 2)
1131 let rowyh (c, coverA, coverB) b n =
1132 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1133 then
1134 let _, _, vy, (_, _, h, _) = b.(n) in
1135 (vy, h)
1136 else
1137 let n' = n - coverA in
1138 let d = n' mod c in
1139 let s = n - d in
1140 let e = min state.pagecount (s + c) in
1141 let rec find m miny maxh = if m = e then miny, maxh else
1142 let _, _, y, (_, _, h, _) = b.(m) in
1143 let miny = min miny y in
1144 let maxh = max maxh h in
1145 find (m+1) miny maxh
1146 in find s max_int 0
1149 let calcheight () =
1150 match conf.columns with
1151 | Cmulti ((_, _, _) as cl, b) ->
1152 if Array.length b > 0
1153 then
1154 let y, h = rowyh cl b (Array.length b - 1) in
1155 y + h + (if conf.presentation then calcips h else 0)
1156 else 0
1157 | Csingle b ->
1158 if Array.length b > 0
1159 then
1160 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1161 y + h + (if conf.presentation then calcips h else 0)
1162 else 0
1163 | Csplit (_, b) ->
1164 if Array.length b > 0
1165 then
1166 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1167 y + h
1168 else 0
1171 let getpageyh pageno =
1172 let pageno = bound pageno 0 (state.pagecount-1) in
1173 match conf.columns with
1174 | Csingle b ->
1175 if Array.length b = 0
1176 then 0, 0
1177 else
1178 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1179 let y =
1180 if conf.presentation
1181 then y - calcips h
1182 else y
1184 y, h
1185 | Cmulti (cl, b) ->
1186 if Array.length b = 0
1187 then 0, 0
1188 else
1189 let y, h = rowyh cl b pageno in
1190 let y =
1191 if conf.presentation
1192 then y - calcips h
1193 else y
1195 y, h
1196 | Csplit (c, b) ->
1197 if Array.length b = 0
1198 then 0, 0
1199 else
1200 let n = pageno*c in
1201 let (_, _, y, (_, _, h, _)) = b.(n) in
1202 y, h
1205 let getpagedim pageno =
1206 let rec f ppdim l =
1207 match l with
1208 | (n, _, _, _) as pdim :: rest ->
1209 if n >= pageno
1210 then (if n = pageno then pdim else ppdim)
1211 else f pdim rest
1213 | [] -> ppdim
1215 f (-1, -1, -1, -1) state.pdims
1218 let getpagey pageno = fst (getpageyh pageno);;
1220 let nogeomcmds cmds =
1221 match cmds with
1222 | s, [] -> String.length s = 0
1223 | _ -> false
1226 let page_of_y y =
1227 let ((c, coverA, coverB) as cl), b =
1228 match conf.columns with
1229 | Csingle b -> (1, 0, 0), b
1230 | Cmulti (c, b) -> c, b
1231 | Csplit (_, b) -> (1, 0, 0), b
1233 if Array.length b = 0
1234 then -1
1235 else
1236 let rec bsearch nmin nmax =
1237 if nmin > nmax
1238 then bound nmin 0 (state.pagecount-1)
1239 else
1240 let n = (nmax + nmin) / 2 in
1241 let vy, h = rowyh cl b n in
1242 let y0, y1 =
1243 if conf.presentation
1244 then
1245 let ips = calcips h in
1246 let y0 = vy - ips in
1247 let y1 = vy + h + ips in
1248 y0, y1
1249 else (
1250 if n = 0
1251 then 0, vy + h + conf.interpagespace
1252 else
1253 let y0 = vy - conf.interpagespace in
1254 y0, y0 + h + conf.interpagespace
1257 if y >= y0 && y < y1
1258 then (
1259 if c = 1
1260 then n
1261 else (
1262 if n > coverA
1263 then
1264 if n < state.pagecount - coverB
1265 then ((n-coverA)/c)*c + coverA
1266 else n
1267 else n
1270 else (
1271 if y > y0
1272 then bsearch (n+1) nmax
1273 else bsearch nmin (n-1)
1276 let r = bsearch 0 (state.pagecount-1) in
1280 let layoutN ((columns, coverA, coverB), b) y sh =
1281 let sh = sh - state.hscrollh in
1282 let rec fold accu n =
1283 if n = Array.length b
1284 then accu
1285 else
1286 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1287 if (vy - y) > sh &&
1288 (n = coverA - 1
1289 || n = state.pagecount - coverB
1290 || (n - coverA) mod columns = columns - 1)
1291 then accu
1292 else
1293 let accu =
1294 if vy + h > y
1295 then
1296 let pagey = max 0 (y - vy) in
1297 let pagedispy = if pagey > 0 then 0 else vy - y in
1298 let pagedispx, pagex =
1299 let pdx =
1300 if n = coverA - 1 || n = state.pagecount - coverB
1301 then state.x + (state.winw - state.scrollw - w) / 2
1302 else dx + xoff + state.x
1304 if pdx < 0
1305 then 0, -pdx
1306 else pdx, 0
1308 let pagevw =
1309 let vw = state.winw - state.scrollw - pagedispx in
1310 let pw = w - pagex in
1311 min vw pw
1313 let pagevh = min (h - pagey) (sh - pagedispy) in
1314 if pagevw > 0 && pagevh > 0
1315 then
1316 let e =
1317 { pageno = n
1318 ; pagedimno = pdimno
1319 ; pagew = w
1320 ; pageh = h
1321 ; pagex = pagex
1322 ; pagey = pagey
1323 ; pagevw = pagevw
1324 ; pagevh = pagevh
1325 ; pagedispx = pagedispx
1326 ; pagedispy = pagedispy
1327 ; pagecol = 0
1330 e :: accu
1331 else
1332 accu
1333 else
1334 accu
1336 fold accu (n+1)
1338 List.rev (fold [] (page_of_y y));
1341 let layoutS (columns, b) y sh =
1342 let sh = sh - state.hscrollh in
1343 let rec fold accu n =
1344 if n = Array.length b
1345 then accu
1346 else
1347 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1348 if (vy - y) > sh
1349 then accu
1350 else
1351 let accu =
1352 if vy + pageh > y
1353 then
1354 let x = xoff + state.x in
1355 let pagey = max 0 (y - vy) in
1356 let pagedispy = if pagey > 0 then 0 else vy - y in
1357 let pagedispx, pagex =
1358 if px = 0
1359 then (
1360 if x < 0
1361 then 0, -x
1362 else x, 0
1364 else (
1365 let px = px - x in
1366 if px < 0
1367 then -px, 0
1368 else 0, px
1371 let pagecolw = pagew/columns in
1372 let pagedispx =
1373 if pagecolw < state.winw
1374 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1375 else pagedispx
1377 let pagevw =
1378 let vw = state.winw - pagedispx - state.scrollw in
1379 let pw = pagew - pagex in
1380 min vw pw
1382 let pagevw = min pagevw pagecolw in
1383 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1384 if pagevw > 0 && pagevh > 0
1385 then
1386 let e =
1387 { pageno = n/columns
1388 ; pagedimno = pdimno
1389 ; pagew = pagew
1390 ; pageh = pageh
1391 ; pagex = pagex
1392 ; pagey = pagey
1393 ; pagevw = pagevw
1394 ; pagevh = pagevh
1395 ; pagedispx = pagedispx
1396 ; pagedispy = pagedispy
1397 ; pagecol = n mod columns
1400 e :: accu
1401 else
1402 accu
1403 else
1404 accu
1406 fold accu (n+1)
1408 List.rev (fold [] 0)
1411 let layout y sh =
1412 if nogeomcmds state.geomcmds
1413 then
1414 match conf.columns with
1415 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1416 | Cmulti c -> layoutN c y sh
1417 | Csplit s -> layoutS s y sh
1418 else []
1421 let clamp incr =
1422 let y = state.y + incr in
1423 let y = max 0 y in
1424 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1428 let itertiles l f =
1429 let tilex = l.pagex mod conf.tilew in
1430 let tiley = l.pagey mod conf.tileh in
1432 let col = l.pagex / conf.tilew in
1433 let row = l.pagey / conf.tileh in
1435 let rec rowloop row y0 dispy h =
1436 if h = 0
1437 then ()
1438 else (
1439 let dh = conf.tileh - y0 in
1440 let dh = min h dh in
1441 let rec colloop col x0 dispx w =
1442 if w = 0
1443 then ()
1444 else (
1445 let dw = conf.tilew - x0 in
1446 let dw = min w dw in
1448 f col row dispx dispy x0 y0 dw dh;
1449 colloop (col+1) 0 (dispx+dw) (w-dw)
1452 colloop col tilex l.pagedispx l.pagevw;
1453 rowloop (row+1) 0 (dispy+dh) (h-dh)
1456 if l.pagevw > 0 && l.pagevh > 0
1457 then rowloop row tiley l.pagedispy l.pagevh;
1460 let gettileopaque l col row =
1461 let key =
1462 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1464 try Some (Hashtbl.find state.tilemap key)
1465 with Not_found -> None
1468 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1469 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1470 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1473 let drawtiles l color =
1474 GlDraw.color color;
1475 let f col row x y tilex tiley w h =
1476 match gettileopaque l col row with
1477 | Some (opaque, _, t) ->
1478 let params = x, y, w, h, tilex, tiley in
1479 if conf.invert
1480 then (
1481 Gl.enable `blend;
1482 GlFunc.blend_func `zero `one_minus_src_color;
1484 drawtile params opaque;
1485 if conf.invert
1486 then Gl.disable `blend;
1487 if conf.debug
1488 then (
1489 let s = Printf.sprintf
1490 "%d[%d,%d] %f sec"
1491 l.pageno col row t
1493 let w = measurestr fstate.fontsize s in
1494 GlMisc.push_attrib [`current];
1495 GlDraw.color (0.0, 0.0, 0.0);
1496 GlDraw.rect
1497 (float (x-2), float (y-2))
1498 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1499 GlDraw.color (1.0, 1.0, 1.0);
1500 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1501 GlMisc.pop_attrib ();
1504 | _ ->
1505 let w =
1506 let lw = state.winw - state.scrollw - x in
1507 min lw w
1508 and h =
1509 let lh = state.winh - y in
1510 min lh h
1512 begin match state.texid with
1513 | Some id ->
1514 Gl.enable `texture_2d;
1515 GlTex.bind_texture `texture_2d id;
1516 let x0 = float x
1517 and y0 = float y
1518 and x1 = float (x+w)
1519 and y1 = float (y+h) in
1521 let tw = float w /. 16.0
1522 and th = float h /. 16.0 in
1523 let tx0 = float tilex /. 16.0
1524 and ty0 = float tiley /. 16.0 in
1525 let tx1 = tx0 +. tw
1526 and ty1 = ty0 +. th in
1527 GlDraw.begins `quads;
1528 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1529 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1530 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1531 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1532 GlDraw.ends ();
1534 Gl.disable `texture_2d;
1535 | None ->
1536 GlDraw.color (1.0, 1.0, 1.0);
1537 GlDraw.rect
1538 (float x, float y)
1539 (float (x+w), float (y+h));
1540 end;
1541 if w > 128 && h > fstate.fontsize + 10
1542 then (
1543 GlDraw.color (0.0, 0.0, 0.0);
1544 let c, r =
1545 if conf.verbose
1546 then (col*conf.tilew, row*conf.tileh)
1547 else col, row
1549 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1551 GlDraw.color color;
1553 itertiles l f
1556 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1558 let tilevisible1 l x y =
1559 let ax0 = l.pagex
1560 and ax1 = l.pagex + l.pagevw
1561 and ay0 = l.pagey
1562 and ay1 = l.pagey + l.pagevh in
1564 let bx0 = x
1565 and by0 = y in
1566 let bx1 = min (bx0 + conf.tilew) l.pagew
1567 and by1 = min (by0 + conf.tileh) l.pageh in
1569 let rx0 = max ax0 bx0
1570 and ry0 = max ay0 by0
1571 and rx1 = min ax1 bx1
1572 and ry1 = min ay1 by1 in
1574 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1575 nonemptyintersection
1578 let tilevisible layout n x y =
1579 let rec findpageinlayout m = function
1580 | l :: rest when l.pageno = n ->
1581 tilevisible1 l x y || (
1582 match conf.columns with
1583 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1584 | _ -> false
1586 | _ :: rest -> findpageinlayout 0 rest
1587 | [] -> false
1589 findpageinlayout 0 layout;
1592 let tileready l x y =
1593 tilevisible1 l x y &&
1594 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1597 let tilepage n p layout =
1598 let rec loop = function
1599 | l :: rest ->
1600 if l.pageno = n
1601 then
1602 let f col row _ _ _ _ _ _ =
1603 if state.currently = Idle
1604 then
1605 match gettileopaque l col row with
1606 | Some _ -> ()
1607 | None ->
1608 let x = col*conf.tilew
1609 and y = row*conf.tileh in
1610 let w =
1611 let w = l.pagew - x in
1612 min w conf.tilew
1614 let h =
1615 let h = l.pageh - y in
1616 min h conf.tileh
1618 let pbo =
1619 if conf.usepbo
1620 then getpbo w h conf.colorspace
1621 else "0"
1623 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1624 state.currently <-
1625 Tiling (
1626 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1627 conf.tilew, conf.tileh
1630 itertiles l f;
1631 else
1632 loop rest
1634 | [] -> ()
1636 if nogeomcmds state.geomcmds
1637 then loop layout;
1640 let preloadlayout y =
1641 let y = if y < state.winh then 0 else y - state.winh in
1642 let h = state.winh*3 in
1643 layout y h;
1646 let load pages =
1647 let rec loop pages =
1648 if state.currently != Idle
1649 then ()
1650 else
1651 match pages with
1652 | l :: rest ->
1653 begin match getopaque l.pageno with
1654 | None ->
1655 wcmd "page %d %d" l.pageno l.pagedimno;
1656 state.currently <- Loading (l, state.gen);
1657 | Some opaque ->
1658 tilepage l.pageno opaque pages;
1659 loop rest
1660 end;
1661 | _ -> ()
1663 if nogeomcmds state.geomcmds
1664 then loop pages
1667 let preload pages =
1668 load pages;
1669 if conf.preload && state.currently = Idle
1670 then load (preloadlayout state.y);
1673 let layoutready layout =
1674 let rec fold all ls =
1675 all && match ls with
1676 | l :: rest ->
1677 let seen = ref false in
1678 let allvisible = ref true in
1679 let foo col row _ _ _ _ _ _ =
1680 seen := true;
1681 allvisible := !allvisible &&
1682 begin match gettileopaque l col row with
1683 | Some _ -> true
1684 | None -> false
1687 itertiles l foo;
1688 fold (!seen && !allvisible) rest
1689 | [] -> true
1691 let alltilesvisible = fold true layout in
1692 alltilesvisible;
1695 let gotoy y =
1696 let y = bound y 0 state.maxy in
1697 let y, layout, proceed =
1698 match conf.maxwait with
1699 | Some time when state.ghyll == noghyll ->
1700 begin match state.throttle with
1701 | None ->
1702 let layout = layout y state.winh in
1703 let ready = layoutready layout in
1704 if not ready
1705 then (
1706 load layout;
1707 state.throttle <- Some (layout, y, now ());
1709 else G.postRedisplay "gotoy showall (None)";
1710 y, layout, ready
1711 | Some (_, _, started) ->
1712 let dt = now () -. started in
1713 if dt > time
1714 then (
1715 state.throttle <- None;
1716 let layout = layout y state.winh in
1717 load layout;
1718 G.postRedisplay "maxwait";
1719 y, layout, true
1721 else -1, [], false
1724 | _ ->
1725 let layout = layout y state.winh in
1726 if not !wtmode || layoutready layout
1727 then G.postRedisplay "gotoy ready";
1728 y, layout, true
1730 if proceed
1731 then (
1732 state.y <- y;
1733 state.layout <- layout;
1734 begin match state.mode with
1735 | LinkNav (Ltexact (pageno, linkno)) ->
1736 let rec loop = function
1737 | [] ->
1738 state.mode <- LinkNav (Ltgendir 0)
1739 | l :: _ when l.pageno = pageno ->
1740 begin match getopaque pageno with
1741 | None ->
1742 state.mode <- LinkNav (Ltgendir 0)
1743 | Some opaque ->
1744 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1745 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1746 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1747 then state.mode <- LinkNav (Ltgendir 0)
1749 | _ :: rest -> loop rest
1751 loop layout
1752 | _ -> ()
1753 end;
1754 begin match state.mode with
1755 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1756 if not (pagevisible layout pageno)
1757 then (
1758 match state.layout with
1759 | [] -> ()
1760 | l :: _ ->
1761 state.mode <- Birdseye (
1762 conf, leftx, l.pageno, hooverpageno, anchor
1765 | LinkNav (Ltgendir dir as lt) ->
1766 let linknav =
1767 let rec loop = function
1768 | [] -> lt
1769 | l :: rest ->
1770 match getopaque l.pageno with
1771 | None -> loop rest
1772 | Some opaque ->
1773 let link =
1774 let ld =
1775 if dir = 0
1776 then LDfirstvisible (l.pagex, l.pagey, dir)
1777 else (
1778 if dir > 0 then LDfirst else LDlast
1781 findlink opaque ld
1783 match link with
1784 | Lnotfound -> loop rest
1785 | Lfound n ->
1786 showlinktype (getlink opaque n);
1787 Ltexact (l.pageno, n)
1789 loop state.layout
1791 state.mode <- LinkNav linknav
1792 | _ -> ()
1793 end;
1794 preload layout;
1796 state.ghyll <- noghyll;
1797 if conf.updatecurs
1798 then (
1799 let mx, my = state.mpos in
1800 updateunder mx my;
1804 let conttiling pageno opaque =
1805 tilepage pageno opaque
1806 (if conf.preload then preloadlayout state.y else state.layout)
1809 let gotoy_and_clear_text y =
1810 if not conf.verbose then state.text <- "";
1811 gotoy y;
1814 let getanchor1 l =
1815 let top =
1816 let coloff = l.pagecol * l.pageh in
1817 float (l.pagey + coloff) /. float l.pageh
1819 let dtop =
1820 if l.pagedispy = 0
1821 then
1823 else
1824 if conf.presentation
1825 then float l.pagedispy /. float (calcips l.pageh)
1826 else float l.pagedispy /. float conf.interpagespace
1828 (l.pageno, top, dtop)
1831 let getanchor () =
1832 match state.layout with
1833 | l :: _ -> getanchor1 l
1834 | [] ->
1835 let n = page_of_y state.y in
1836 if n = -1
1837 then state.anchor
1838 else
1839 let y, h = getpageyh n in
1840 let dy = y - state.y in
1841 let dtop =
1842 if conf.presentation
1843 then
1844 let ips = calcips h in
1845 float (dy + ips) /. float ips
1846 else
1847 float dy /. float conf.interpagespace
1849 (n, 0.0, dtop)
1852 let getanchory (n, top, dtop) =
1853 let y, h = getpageyh n in
1854 if conf.presentation
1855 then
1856 let ips = calcips h in
1857 y + truncate (top*.float h -. dtop*.float ips) + ips;
1858 else
1859 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1862 let gotoanchor anchor =
1863 gotoy (getanchory anchor);
1866 let addnav () =
1867 cbput state.hists.nav (getanchor ());
1870 let getnav dir =
1871 let anchor = cbgetc state.hists.nav dir in
1872 getanchory anchor;
1875 let gotoghyll y =
1876 let scroll f n a b =
1877 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1878 let snake f a b =
1879 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1880 if f < a
1881 then s (float f /. float a)
1882 else (
1883 if f > b
1884 then 1.0 -. s ((float (f-b) /. float (n-b)))
1885 else 1.0
1888 snake f a b
1889 and summa f n a b =
1890 (* courtesy:
1891 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1892 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1893 let iv1 = iv f in
1894 let ins = float a *. iv1
1895 and outs = float (n-b) *. iv1 in
1896 let ones = b - a in
1897 ins +. outs +. float ones
1899 let rec set (_N, _A, _B) y sy =
1900 let sum = summa 1.0 _N _A _B in
1901 let dy = float (y - sy) in
1902 state.ghyll <- (
1903 let rec gf n y1 o =
1904 if n >= _N
1905 then state.ghyll <- noghyll
1906 else
1907 let go n =
1908 let s = scroll n _N _A _B in
1909 let y1 = y1 +. ((s *. dy) /. sum) in
1910 gotoy_and_clear_text (truncate y1);
1911 state.ghyll <- gf (n+1) y1;
1913 match o with
1914 | None -> go n
1915 | Some y' -> set (_N/2, 1, 1) y' state.y
1917 gf 0 (float state.y)
1920 match conf.ghyllscroll with
1921 | None ->
1922 gotoy_and_clear_text y
1923 | Some nab ->
1924 if state.ghyll == noghyll
1925 then set nab y state.y
1926 else state.ghyll (Some y)
1929 let gotopage n top =
1930 let y, h = getpageyh n in
1931 let y = y + (truncate (top *. float h)) in
1932 gotoghyll y
1935 let gotopage1 n top =
1936 let y = getpagey n in
1937 let y = y + top in
1938 gotoghyll y
1941 let invalidate s f =
1942 state.layout <- [];
1943 state.pdims <- [];
1944 state.rects <- [];
1945 state.rects1 <- [];
1946 match state.geomcmds with
1947 | ps, [] when String.length ps = 0 ->
1948 f ();
1949 state.geomcmds <- s, [];
1951 | ps, [] ->
1952 state.geomcmds <- ps, [s, f];
1954 | ps, (s', _) :: rest when s' = s ->
1955 state.geomcmds <- ps, ((s, f) :: rest);
1957 | ps, cmds ->
1958 state.geomcmds <- ps, ((s, f) :: cmds);
1961 let flushpages () =
1962 Hashtbl.iter (fun _ opaque ->
1963 wcmd "freepage %s" opaque;
1964 ) state.pagemap;
1965 Hashtbl.clear state.pagemap;
1968 let flushtiles () =
1969 if not (Queue.is_empty state.tilelru)
1970 then (
1971 Queue.iter (fun (k, p, s) ->
1972 wcmd "freetile %s" p;
1973 state.memused <- state.memused - s;
1974 Hashtbl.remove state.tilemap k;
1975 ) state.tilelru;
1976 state.uioh#infochanged Memused;
1977 Queue.clear state.tilelru;
1979 load state.layout;
1982 let opendoc path password =
1983 state.path <- path;
1984 state.password <- password;
1985 state.gen <- state.gen + 1;
1986 state.docinfo <- [];
1988 flushpages ();
1989 setaalevel conf.aalevel;
1990 let titlepath =
1991 if String.length state.origin = 0
1992 then path
1993 else state.origin
1995 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1996 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1997 invalidate "reqlayout"
1998 (fun () ->
1999 wcmd "reqlayout %d %d %s\000"
2000 conf.angle (int_of_fitmodel conf.fitmodel) state.nameddest;
2004 let reload () =
2005 state.anchor <- getanchor ();
2006 opendoc state.path state.password;
2009 let scalecolor c =
2010 let c = c *. conf.colorscale in
2011 (c, c, c);
2014 let scalecolor2 (r, g, b) =
2015 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2018 let docolumns = function
2019 | Csingle _ ->
2020 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2021 let rec loop pageno pdimno pdim y ph pdims =
2022 if pageno = state.pagecount
2023 then ()
2024 else
2025 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2026 match pdims with
2027 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2028 pdimno+1, pdim, rest
2029 | _ ->
2030 pdimno, pdim, pdims
2032 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2033 let y = y +
2034 (if conf.presentation
2035 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2036 else (if pageno = 0 then 0 else conf.interpagespace)
2039 a.(pageno) <- (pdimno, x, y, pdim);
2040 loop (pageno+1) pdimno pdim (y + h) h pdims
2042 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2043 conf.columns <- Csingle a;
2045 | Cmulti ((columns, coverA, coverB), _) ->
2046 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2047 let rec loop pageno pdimno pdim x y rowh pdims =
2048 let rec fixrow m = if m = pageno then () else
2049 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2050 if h < rowh
2051 then (
2052 let y = y + (rowh - h) / 2 in
2053 a.(m) <- (pdimno, x, y, pdim);
2055 fixrow (m+1)
2057 if pageno = state.pagecount
2058 then fixrow (((pageno - 1) / columns) * columns)
2059 else
2060 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2061 match pdims with
2062 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2063 pdimno+1, pdim, rest
2064 | _ ->
2065 pdimno, pdim, pdims
2067 let x, y, rowh' =
2068 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2069 then (
2070 let x = (state.winw - state.scrollw - w) / 2 in
2071 let ips =
2072 if conf.presentation then calcips h else conf.interpagespace in
2073 x, y + ips + rowh, h
2075 else (
2076 if (pageno - coverA) mod columns = 0
2077 then (
2078 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2079 let y =
2080 if conf.presentation
2081 then
2082 let ips = calcips h in
2083 y + (if pageno = 0 then 0 else calcips rowh + ips)
2084 else
2085 y + (if pageno = 0 then 0 else conf.interpagespace)
2087 x, y + rowh, h
2089 else x, y, max rowh h
2092 let y =
2093 if pageno > 1 && (pageno - coverA) mod columns = 0
2094 then (
2095 let y =
2096 if pageno = columns && conf.presentation
2097 then (
2098 let ips = calcips rowh in
2099 for i = 0 to pred columns
2101 let (pdimno, x, y, pdim) = a.(i) in
2102 a.(i) <- (pdimno, x, y+ips, pdim)
2103 done;
2104 y+ips;
2106 else y
2108 fixrow (pageno - columns);
2111 else y
2113 a.(pageno) <- (pdimno, x, y, pdim);
2114 let x = x + w + xoff*2 + conf.interpagespace in
2115 loop (pageno+1) pdimno pdim x y rowh' pdims
2117 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2118 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2120 | Csplit (c, _) ->
2121 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2122 let rec loop pageno pdimno pdim y pdims =
2123 if pageno = state.pagecount
2124 then ()
2125 else
2126 let pdimno, ((_, w, h, _) as pdim), pdims =
2127 match pdims with
2128 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2129 pdimno+1, pdim, rest
2130 | _ ->
2131 pdimno, pdim, pdims
2133 let cw = w / c in
2134 let rec loop1 n x y =
2135 if n = c then y else (
2136 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2137 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2140 let y = loop1 0 0 y in
2141 loop (pageno+1) pdimno pdim y pdims
2143 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2144 conf.columns <- Csplit (c, a);
2147 let represent () =
2148 docolumns conf.columns;
2149 state.maxy <- calcheight ();
2150 state.hscrollh <-
2151 if state.x = 0 && state.w <= state.winw - state.scrollw
2152 then 0
2153 else state.scrollw
2155 if state.reprf == noreprf
2156 then (
2157 match state.mode with
2158 | Birdseye (_, _, pageno, _, _) ->
2159 let y, h = getpageyh pageno in
2160 let top = (state.winh - h) / 2 in
2161 gotoy (max 0 (y - top))
2162 | _ -> gotoanchor state.anchor
2164 else (
2165 state.reprf ();
2166 state.reprf <- noreprf;
2170 let reshape w h =
2171 GlDraw.viewport 0 0 w h;
2172 let firsttime = state.geomcmds == firstgeomcmds in
2173 if not firsttime && nogeomcmds state.geomcmds
2174 then state.anchor <- getanchor ();
2176 state.winw <- w;
2177 let w = truncate (float w *. conf.zoom) - state.scrollw in
2178 let w = max w 2 in
2179 state.winh <- h;
2180 setfontsize fstate.fontsize;
2181 GlMat.mode `modelview;
2182 GlMat.load_identity ();
2184 GlMat.mode `projection;
2185 GlMat.load_identity ();
2186 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2187 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2188 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2190 let relx =
2191 if conf.zoom <= 1.0
2192 then 0.0
2193 else float state.x /. float state.w
2195 invalidate "geometry"
2196 (fun () ->
2197 state.w <- w;
2198 if not firsttime
2199 then state.x <- truncate (relx *. float w);
2200 let w =
2201 match conf.columns with
2202 | Csingle _ -> w
2203 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2204 | Csplit (c, _) -> w * c
2206 wcmd "geometry %d %d %d"
2207 w ((truncate (float h*.conf.zoom)) - 2*conf.interpagespace)
2208 (int_of_fitmodel conf.fitmodel)
2212 let enttext () =
2213 let len = String.length state.text in
2214 let drawstring s =
2215 let hscrollh =
2216 match state.mode with
2217 | Textentry _
2218 | View ->
2219 let h, _, _ = state.uioh#scrollpw in
2221 | _ -> 0
2223 let rect x w =
2224 GlDraw.rect
2225 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2226 (x+.w, float (state.winh - hscrollh))
2229 let w = float (state.winw - state.scrollw - 1) in
2230 if state.progress >= 0.0 && state.progress < 1.0
2231 then (
2232 GlDraw.color (0.3, 0.3, 0.3);
2233 let w1 = w *. state.progress in
2234 rect 0.0 w1;
2235 GlDraw.color (0.0, 0.0, 0.0);
2236 rect w1 (w-.w1)
2238 else (
2239 GlDraw.color (0.0, 0.0, 0.0);
2240 rect 0.0 w;
2243 GlDraw.color (1.0, 1.0, 1.0);
2244 drawstring fstate.fontsize
2245 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2247 let s =
2248 match state.mode with
2249 | Textentry ((prefix, text, _, _, _, _), _) ->
2250 let s =
2251 if len > 0
2252 then
2253 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2254 else
2255 Printf.sprintf "%s%s_" prefix text
2259 | _ -> state.text
2261 let s =
2262 if state.newerrmsgs
2263 then (
2264 if not (istextentry state.mode) && state.uioh#eformsgs
2265 then
2266 let s1 = "(press 'e' to review error messasges)" in
2267 if String.length s > 0 then s ^ " " ^ s1 else s1
2268 else s
2270 else s
2272 if String.length s > 0
2273 then drawstring s
2276 let gctiles () =
2277 let len = Queue.length state.tilelru in
2278 let layout = lazy (
2279 match state.throttle with
2280 | None ->
2281 if conf.preload
2282 then preloadlayout state.y
2283 else state.layout
2284 | Some (layout, _, _) ->
2285 layout
2286 ) in
2287 let rec loop qpos =
2288 if state.memused <= conf.memlimit
2289 then ()
2290 else (
2291 if qpos < len
2292 then
2293 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2294 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2295 let (_, pw, ph, _) = getpagedim n in
2297 gen = state.gen
2298 && colorspace = conf.colorspace
2299 && angle = conf.angle
2300 && pagew = pw
2301 && pageh = ph
2302 && (
2303 let x = col*conf.tilew
2304 and y = row*conf.tileh in
2305 tilevisible (Lazy.force_val layout) n x y
2307 then Queue.push lruitem state.tilelru
2308 else (
2309 freepbo p;
2310 wcmd "freetile %s" p;
2311 state.memused <- state.memused - s;
2312 state.uioh#infochanged Memused;
2313 Hashtbl.remove state.tilemap k;
2315 loop (qpos+1)
2318 loop 0
2321 let logcurrently = function
2322 | Idle -> dolog "Idle"
2323 | Loading (l, gen) ->
2324 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2325 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2326 dolog
2327 "Tiling %d[%d,%d] page=%s cs=%s angle"
2328 l.pageno col row pageopaque
2329 (colorspace_to_string colorspace)
2331 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2332 angle gen conf.angle state.gen
2333 tilew tileh
2334 conf.tilew conf.tileh
2336 | Outlining _ ->
2337 dolog "outlining"
2340 let splitatspace =
2341 let r = Str.regexp " " in
2342 fun s -> Str.bounded_split r s 2;
2345 let onpagerect pageno f =
2346 let b =
2347 match conf.columns with
2348 | Cmulti (_, b) -> b
2349 | Csingle b -> b
2350 | Csplit (_, b) -> b
2352 if pageno >= 0 && pageno < Array.length b
2353 then
2354 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2355 let r = getpdimrect pdimno in
2356 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2359 let gotopagexy1 pageno x y =
2360 onpagerect pageno (fun w h ->
2361 let top = y /. h in
2362 let _,w1,_,leftx = getpagedim pageno in
2363 let wh = state.winh - state.hscrollh in
2364 let sw = float w1 /. w in
2365 let x = sw *. x in
2366 let x = leftx + state.x + truncate x in
2367 let sx =
2368 if x < 0 || x >= state.winw - state.scrollw
2369 then state.x - x
2370 else state.x
2372 let py, h = getpageyh pageno in
2373 let pdy = truncate (top *. float h) in
2374 let y' = py + pdy in
2375 let dy = y' - state.y in
2376 let sy =
2377 if x != state.x || not (dy > 0 && dy < wh)
2378 then (
2379 if conf.presentation
2380 then
2381 if abs (py - y') > wh
2382 then y'
2383 else py
2384 else y';
2386 else state.y
2388 if state.x != sx || state.y != sy
2389 then (
2390 let x, y =
2391 if !wtmode
2392 then (
2393 let ww = state.winw - state.scrollw in
2394 let qx = sx / ww
2395 and qy = pdy / wh in
2396 let x = qx * ww
2397 and y = py + qy * wh in
2398 let x = if -x + ww > w1 then -(w1-ww) else x
2399 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2400 let y =
2401 if conf.presentation
2402 then
2403 if abs (py - y') > wh
2404 then y'
2405 else py
2406 else y';
2408 (x, y)
2410 else (sx, sy)
2412 state.x <- x;
2413 state.hscrollh <-
2414 if x = 0 && state.w <= state.winw - state.scrollw
2415 then 0
2416 else state.scrollw
2418 gotoy_and_clear_text y;
2420 else gotoy_and_clear_text state.y;
2424 let gotopagexy pageno x y =
2425 match state.mode with
2426 | Birdseye _ -> gotopage pageno 0.0
2427 | _ -> gotopagexy1 pageno x y
2430 let act cmds =
2431 (* dolog "%S" cmds; *)
2432 let cl = splitatspace cmds in
2433 let scan s fmt f =
2434 try Scanf.sscanf s fmt f
2435 with exn ->
2436 dolog "error processing '%S': %s" cmds (exntos exn);
2437 exit 1
2439 match cl with
2440 | "clear" :: [] ->
2441 state.uioh#infochanged Pdim;
2442 state.pdims <- [];
2444 | "clearrects" :: [] ->
2445 state.rects <- state.rects1;
2446 G.postRedisplay "clearrects";
2448 | "continue" :: args :: [] ->
2449 let n = scan args "%u" (fun n -> n) in
2450 state.pagecount <- n;
2451 begin match state.currently with
2452 | Outlining l ->
2453 state.currently <- Idle;
2454 state.outlines <- Array.of_list (List.rev l)
2455 | _ -> ()
2456 end;
2458 let cur, cmds = state.geomcmds in
2459 if String.length cur = 0
2460 then failwith "umpossible";
2462 begin match List.rev cmds with
2463 | [] ->
2464 state.geomcmds <- "", [];
2465 represent ();
2466 | (s, f) :: rest ->
2467 f ();
2468 state.geomcmds <- s, List.rev rest;
2469 end;
2470 if conf.maxwait = None && not !wtmode
2471 then G.postRedisplay "continue";
2473 | "title" :: args :: [] ->
2474 Wsi.settitle args
2476 | "msg" :: args :: [] ->
2477 showtext ' ' args
2479 | "vmsg" :: args :: [] ->
2480 if conf.verbose
2481 then showtext ' ' args
2483 | "emsg" :: args :: [] ->
2484 Buffer.add_string state.errmsgs args;
2485 state.newerrmsgs <- true;
2486 G.postRedisplay "error message"
2488 | "progress" :: args :: [] ->
2489 let progress, text =
2490 scan args "%f %n"
2491 (fun f pos ->
2492 f, String.sub args pos (String.length args - pos))
2494 state.text <- text;
2495 state.progress <- progress;
2496 G.postRedisplay "progress"
2498 | "firstmatch" :: args :: [] ->
2499 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2500 scan args "%u %d %f %f %f %f %f %f %f %f"
2501 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2502 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2504 let y = (getpagey pageno) + truncate y0 in
2505 addnav ();
2506 gotoy y;
2507 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2509 | "match" :: args :: [] ->
2510 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2511 scan args "%u %d %f %f %f %f %f %f %f %f"
2512 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2513 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2515 state.rects1 <-
2516 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2518 | "page" :: args :: [] ->
2519 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2520 begin match state.currently with
2521 | Loading (l, gen) ->
2522 vlog "page %d took %f sec" l.pageno t;
2523 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2524 begin match state.throttle with
2525 | None ->
2526 let preloadedpages =
2527 if conf.preload
2528 then preloadlayout state.y
2529 else state.layout
2531 let evict () =
2532 let set =
2533 List.fold_left (fun s l -> IntSet.add l.pageno s)
2534 IntSet.empty preloadedpages
2536 let evictedpages =
2537 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2538 if not (IntSet.mem pageno set)
2539 then (
2540 wcmd "freepage %s" opaque;
2541 key :: accu
2543 else accu
2544 ) state.pagemap []
2546 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2548 evict ();
2549 state.currently <- Idle;
2550 if gen = state.gen
2551 then (
2552 tilepage l.pageno pageopaque state.layout;
2553 load state.layout;
2554 load preloadedpages;
2555 if pagevisible state.layout l.pageno
2556 && layoutready state.layout
2557 then G.postRedisplay "page";
2560 | Some (layout, _, _) ->
2561 state.currently <- Idle;
2562 tilepage l.pageno pageopaque layout;
2563 load state.layout
2564 end;
2566 | _ ->
2567 dolog "Inconsistent loading state";
2568 logcurrently state.currently;
2569 exit 1
2572 | "tile" :: args :: [] ->
2573 let (x, y, opaque, size, t) =
2574 scan args "%u %u %s %u %f"
2575 (fun x y p size t -> (x, y, p, size, t))
2577 begin match state.currently with
2578 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2579 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2581 unmappbo opaque;
2582 if tilew != conf.tilew || tileh != conf.tileh
2583 then (
2584 wcmd "freetile %s" opaque;
2585 state.currently <- Idle;
2586 load state.layout;
2588 else (
2589 puttileopaque l col row gen cs angle opaque size t;
2590 state.memused <- state.memused + size;
2591 state.uioh#infochanged Memused;
2592 gctiles ();
2593 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2594 opaque, size) state.tilelru;
2596 let layout =
2597 match state.throttle with
2598 | None -> state.layout
2599 | Some (layout, _, _) -> layout
2602 state.currently <- Idle;
2603 if gen = state.gen
2604 && conf.colorspace = cs
2605 && conf.angle = angle
2606 && tilevisible layout l.pageno x y
2607 then conttiling l.pageno pageopaque;
2609 begin match state.throttle with
2610 | None ->
2611 preload state.layout;
2612 if gen = state.gen
2613 && conf.colorspace = cs
2614 && conf.angle = angle
2615 && tilevisible state.layout l.pageno x y
2616 && (not !wtmode || layoutready state.layout)
2617 then G.postRedisplay "tile nothrottle";
2619 | Some (layout, y, _) ->
2620 let ready = layoutready layout in
2621 if ready
2622 then (
2623 state.y <- y;
2624 state.layout <- layout;
2625 state.throttle <- None;
2626 G.postRedisplay "throttle";
2628 else load layout;
2629 end;
2632 | _ ->
2633 dolog "Inconsistent tiling state";
2634 logcurrently state.currently;
2635 exit 1
2638 | "pdim" :: args :: [] ->
2639 let (n, w, h, _) as pdim =
2640 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2642 let pdim =
2643 match conf.fitmodel, conf.columns with
2644 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2645 | _ -> pdim
2647 state.uioh#infochanged Pdim;
2648 state.pdims <- pdim :: state.pdims
2650 | "o" :: args :: [] ->
2651 let (l, n, t, h, pos) =
2652 scan args "%u %u %d %u %n"
2653 (fun l n t h pos -> l, n, t, h, pos)
2655 let s = String.sub args pos (String.length args - pos) in
2656 let outline = (s, l, (n, float t /. float h, 0.0)) in
2657 begin match state.currently with
2658 | Outlining outlines ->
2659 state.currently <- Outlining (outline :: outlines)
2660 | Idle ->
2661 state.currently <- Outlining [outline]
2662 | currently ->
2663 dolog "invalid outlining state";
2664 logcurrently currently
2667 | "a" :: args :: [] ->
2668 let (n, l, t) =
2669 scan args "%u %d %d" (fun n l t -> n, l, t)
2671 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2673 | "info" :: args :: [] ->
2674 state.docinfo <- (1, args) :: state.docinfo
2676 | "infoend" :: [] ->
2677 state.uioh#infochanged Docinfo;
2678 state.docinfo <- List.rev state.docinfo
2680 | _ ->
2681 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2684 let onhist cb =
2685 let rc = cb.rc in
2686 let action = function
2687 | HCprev -> cbget cb ~-1
2688 | HCnext -> cbget cb 1
2689 | HCfirst -> cbget cb ~-(cb.rc)
2690 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2691 and cancel () = cb.rc <- rc
2692 in (action, cancel)
2695 let search pattern forward =
2696 match conf.columns with
2697 | Csplit _ ->
2698 showtext '!' "searching does not work properly in split columns mode"
2699 | _ ->
2700 if String.length pattern > 0
2701 then
2702 let pn, py =
2703 match state.layout with
2704 | [] -> 0, 0
2705 | l :: _ ->
2706 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2708 wcmd "search %d %d %d %d,%s\000"
2709 (btod conf.icase) pn py (btod forward) pattern;
2712 let intentry text key =
2713 let c =
2714 if key >= 32 && key < 127
2715 then Char.chr key
2716 else '\000'
2718 match c with
2719 | '0' .. '9' ->
2720 let text = addchar text c in
2721 TEcont text
2723 | _ ->
2724 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2725 TEcont text
2728 let linknentry text key =
2729 let c =
2730 if key >= 32 && key < 127
2731 then Char.chr key
2732 else '\000'
2734 match c with
2735 | 'a' .. 'z' ->
2736 let text = addchar text c in
2737 TEcont text
2739 | _ ->
2740 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2741 TEcont text
2744 let linkndone f s =
2745 if String.length s > 0
2746 then (
2747 let n =
2748 let l = String.length s in
2749 let rec loop pos n = if pos = l then n else
2750 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2751 loop (pos+1) (n*26 + m)
2752 in loop 0 0
2754 let rec loop n = function
2755 | [] -> ()
2756 | l :: rest ->
2757 match getopaque l.pageno with
2758 | None -> loop n rest
2759 | Some opaque ->
2760 let m = getlinkcount opaque in
2761 if n < m
2762 then (
2763 let under = getlink opaque n in
2764 f under
2766 else loop (n-m) rest
2768 loop n state.layout;
2772 let textentry text key =
2773 if key land 0xff00 = 0xff00
2774 then TEcont text
2775 else TEcont (text ^ toutf8 key)
2778 let reqlayout angle fitmodel =
2779 match state.throttle with
2780 | None ->
2781 if nogeomcmds state.geomcmds
2782 then state.anchor <- getanchor ();
2783 conf.angle <- angle mod 360;
2784 if conf.angle != 0
2785 then (
2786 match state.mode with
2787 | LinkNav _ -> state.mode <- View
2788 | _ -> ()
2790 conf.fitmodel <- fitmodel;
2791 invalidate "reqlayout"
2792 (fun () ->
2793 wcmd "reqlayout %d %d" conf.angle (int_of_fitmodel conf.fitmodel)
2795 | _ -> ()
2798 let settrim trimmargins trimfuzz =
2799 if nogeomcmds state.geomcmds
2800 then state.anchor <- getanchor ();
2801 conf.trimmargins <- trimmargins;
2802 conf.trimfuzz <- trimfuzz;
2803 let x0, y0, x1, y1 = trimfuzz in
2804 invalidate "settrim"
2805 (fun () ->
2806 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2807 flushpages ();
2810 let setzoom zoom =
2811 match state.throttle with
2812 | None ->
2813 let zoom = max 0.0001 zoom in
2814 if zoom <> conf.zoom
2815 then (
2816 state.prevzoom <- conf.zoom;
2817 conf.zoom <- zoom;
2818 reshape state.winw state.winh;
2819 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2822 | Some (layout, y, started) ->
2823 let time =
2824 match conf.maxwait with
2825 | None -> 0.0
2826 | Some t -> t
2828 let dt = now () -. started in
2829 if dt > time
2830 then (
2831 state.y <- y;
2832 load layout;
2836 let setcolumns mode columns coverA coverB =
2837 state.prevcolumns <- Some (conf.columns, conf.zoom);
2838 if columns < 0
2839 then (
2840 if isbirdseye mode
2841 then showtext '!' "split mode doesn't work in bird's eye"
2842 else (
2843 conf.columns <- Csplit (-columns, [||]);
2844 state.x <- 0;
2845 conf.zoom <- 1.0;
2848 else (
2849 if columns < 2
2850 then (
2851 conf.columns <- Csingle [||];
2852 state.x <- 0;
2853 setzoom 1.0;
2855 else (
2856 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2857 conf.zoom <- 1.0;
2860 reshape state.winw state.winh;
2863 let enterbirdseye () =
2864 let zoom = float conf.thumbw /. float state.winw in
2865 let birdseyepageno =
2866 let cy = state.winh / 2 in
2867 let fold = function
2868 | [] -> 0
2869 | l :: rest ->
2870 let rec fold best = function
2871 | [] -> best.pageno
2872 | l :: rest ->
2873 let d = cy - (l.pagedispy + l.pagevh/2)
2874 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2875 if abs d < abs dbest
2876 then fold l rest
2877 else best.pageno
2878 in fold l rest
2880 fold state.layout
2882 state.mode <- Birdseye (
2883 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2885 conf.zoom <- zoom;
2886 conf.presentation <- false;
2887 conf.interpagespace <- 10;
2888 conf.hlinks <- false;
2889 conf.fitmodel <- FitProportional;
2890 state.x <- 0;
2891 state.mstate <- Mnone;
2892 conf.maxwait <- None;
2893 conf.columns <- (
2894 match conf.beyecolumns with
2895 | Some c ->
2896 conf.zoom <- 1.0;
2897 Cmulti ((c, 0, 0), [||])
2898 | None -> Csingle [||]
2900 Wsi.setcursor Wsi.CURSOR_INHERIT;
2901 if conf.verbose
2902 then
2903 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2904 (100.0*.zoom)
2905 else
2906 state.text <- ""
2908 reshape state.winw state.winh;
2911 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2912 state.mode <- View;
2913 conf.zoom <- c.zoom;
2914 conf.presentation <- c.presentation;
2915 conf.interpagespace <- c.interpagespace;
2916 conf.maxwait <- c.maxwait;
2917 conf.hlinks <- c.hlinks;
2918 conf.fitmodel <- c.fitmodel;
2919 conf.beyecolumns <- (
2920 match conf.columns with
2921 | Cmulti ((c, _, _), _) -> Some c
2922 | Csingle _ -> None
2923 | Csplit _ -> failwith "leaving bird's eye split mode"
2925 conf.columns <- (
2926 match c.columns with
2927 | Cmulti (c, _) -> Cmulti (c, [||])
2928 | Csingle _ -> Csingle [||]
2929 | Csplit (c, _) -> Csplit (c, [||])
2931 state.x <- leftx;
2932 if conf.verbose
2933 then
2934 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2935 (100.0*.conf.zoom)
2937 reshape state.winw state.winh;
2938 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2941 let togglebirdseye () =
2942 match state.mode with
2943 | Birdseye vals -> leavebirdseye vals true
2944 | View -> enterbirdseye ()
2945 | _ -> ()
2948 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2949 let pageno = max 0 (pageno - incr) in
2950 let rec loop = function
2951 | [] -> gotopage1 pageno 0
2952 | l :: _ when l.pageno = pageno ->
2953 if l.pagedispy >= 0 && l.pagey = 0
2954 then G.postRedisplay "upbirdseye"
2955 else gotopage1 pageno 0
2956 | _ :: rest -> loop rest
2958 loop state.layout;
2959 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2962 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2963 let pageno = min (state.pagecount - 1) (pageno + incr) in
2964 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2965 let rec loop = function
2966 | [] ->
2967 let y, h = getpageyh pageno in
2968 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2969 gotoy (clamp dy)
2970 | l :: _ when l.pageno = pageno ->
2971 if l.pagevh != l.pageh
2972 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2973 else G.postRedisplay "downbirdseye"
2974 | _ :: rest -> loop rest
2976 loop state.layout
2979 let optentry mode _ key =
2980 let btos b = if b then "on" else "off" in
2981 if key >= 32 && key < 127
2982 then
2983 let c = Char.chr key in
2984 match c with
2985 | 's' ->
2986 let ondone s =
2987 try conf.scrollstep <- int_of_string s with exc ->
2988 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2990 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2992 | 'A' ->
2993 let ondone s =
2995 conf.autoscrollstep <- int_of_string s;
2996 if state.autoscroll <> None
2997 then state.autoscroll <- Some conf.autoscrollstep
2998 with exc ->
2999 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3001 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3003 | 'C' ->
3004 let ondone s =
3006 let n, a, b = multicolumns_of_string s in
3007 setcolumns mode n a b;
3008 with exc ->
3009 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3011 TEswitch ("columns: ", "", None, textentry, ondone, true)
3013 | 'Z' ->
3014 let ondone s =
3016 let zoom = float (int_of_string s) /. 100.0 in
3017 setzoom zoom
3018 with exc ->
3019 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3021 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3023 | 't' ->
3024 let ondone s =
3026 conf.thumbw <- bound (int_of_string s) 2 4096;
3027 state.text <-
3028 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3029 begin match mode with
3030 | Birdseye beye ->
3031 leavebirdseye beye false;
3032 enterbirdseye ();
3033 | _ -> ();
3035 with exc ->
3036 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3038 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3040 | 'R' ->
3041 let ondone s =
3042 match try
3043 Some (int_of_string s)
3044 with exc ->
3045 state.text <- Printf.sprintf "bad integer `%s': %s"
3046 s (exntos exc);
3047 None
3048 with
3049 | Some angle -> reqlayout angle conf.fitmodel
3050 | None -> ()
3052 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3054 | 'i' ->
3055 conf.icase <- not conf.icase;
3056 TEdone ("case insensitive search " ^ (btos conf.icase))
3058 | 'p' ->
3059 conf.preload <- not conf.preload;
3060 gotoy state.y;
3061 TEdone ("preload " ^ (btos conf.preload))
3063 | 'v' ->
3064 conf.verbose <- not conf.verbose;
3065 TEdone ("verbose " ^ (btos conf.verbose))
3067 | 'd' ->
3068 conf.debug <- not conf.debug;
3069 TEdone ("debug " ^ (btos conf.debug))
3071 | 'h' ->
3072 conf.maxhfit <- not conf.maxhfit;
3073 state.maxy <- calcheight ();
3074 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3076 | 'c' ->
3077 conf.crophack <- not conf.crophack;
3078 TEdone ("crophack " ^ btos conf.crophack)
3080 | 'a' ->
3081 let s =
3082 match conf.maxwait with
3083 | None ->
3084 conf.maxwait <- Some infinity;
3085 "always wait for page to complete"
3086 | Some _ ->
3087 conf.maxwait <- None;
3088 "show placeholder if page is not ready"
3090 TEdone s
3092 | 'f' ->
3093 conf.underinfo <- not conf.underinfo;
3094 TEdone ("underinfo " ^ btos conf.underinfo)
3096 | 'P' ->
3097 conf.savebmarks <- not conf.savebmarks;
3098 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3100 | 'S' ->
3101 let ondone s =
3103 let pageno, py =
3104 match state.layout with
3105 | [] -> 0, 0
3106 | l :: _ ->
3107 l.pageno, l.pagey
3109 conf.interpagespace <- int_of_string s;
3110 docolumns conf.columns;
3111 state.maxy <- calcheight ();
3112 let y = getpagey pageno in
3113 gotoy (y + py)
3114 with exc ->
3115 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3117 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3119 | 'l' ->
3120 let fm =
3121 match conf.fitmodel with
3122 | FitProportional -> FitWidth
3123 | _ -> FitProportional
3125 reqlayout conf.angle fm;
3126 TEdone ("proportional display " ^ btos (fm == FitProportional))
3128 | 'T' ->
3129 settrim (not conf.trimmargins) conf.trimfuzz;
3130 TEdone ("trim margins " ^ btos conf.trimmargins)
3132 | 'I' ->
3133 conf.invert <- not conf.invert;
3134 TEdone ("invert colors " ^ btos conf.invert)
3136 | 'x' ->
3137 let ondone s =
3138 cbput state.hists.sel s;
3139 conf.selcmd <- s;
3141 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3142 textentry, ondone, true)
3144 | _ ->
3145 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3146 TEstop
3147 else
3148 TEcont state.text
3151 class type lvsource = object
3152 method getitemcount : int
3153 method getitem : int -> (string * int)
3154 method hasaction : int -> bool
3155 method exit :
3156 uioh:uioh ->
3157 cancel:bool ->
3158 active:int ->
3159 first:int ->
3160 pan:int ->
3161 qsearch:string ->
3162 uioh option
3163 method getactive : int
3164 method getfirst : int
3165 method getqsearch : string
3166 method setqsearch : string -> unit
3167 method getpan : int
3168 end;;
3170 class virtual lvsourcebase = object
3171 val mutable m_active = 0
3172 val mutable m_first = 0
3173 val mutable m_qsearch = ""
3174 val mutable m_pan = 0
3175 method getactive = m_active
3176 method getfirst = m_first
3177 method getqsearch = m_qsearch
3178 method getpan = m_pan
3179 method setqsearch s = m_qsearch <- s
3180 end;;
3182 let withoutlastutf8 s =
3183 let len = String.length s in
3184 if len = 0
3185 then s
3186 else
3187 let rec find pos =
3188 if pos = 0
3189 then pos
3190 else
3191 let b = Char.code s.[pos] in
3192 if b land 0b11000000 = 0b11000000
3193 then pos
3194 else find (pos-1)
3196 let first =
3197 if Char.code s.[len-1] land 0x80 = 0
3198 then len-1
3199 else find (len-1)
3201 String.sub s 0 first;
3204 let textentrykeyboard
3205 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3206 let key =
3207 if key >= 0xffb0 && key <= 0xffb9
3208 then key - 0xffb0 + 48 else key
3210 let enttext te =
3211 state.mode <- Textentry (te, onleave);
3212 state.text <- "";
3213 enttext ();
3214 G.postRedisplay "textentrykeyboard enttext";
3216 let histaction cmd =
3217 match opthist with
3218 | None -> ()
3219 | Some (action, _) ->
3220 state.mode <- Textentry (
3221 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3223 G.postRedisplay "textentry histaction"
3225 match key with
3226 | 0xff08 -> (* backspace *)
3227 let s = withoutlastutf8 text in
3228 let len = String.length s in
3229 if cancelonempty && len = 0
3230 then (
3231 onleave Cancel;
3232 G.postRedisplay "textentrykeyboard after cancel";
3234 else (
3235 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3238 | 0xff0d | 0xff8d -> (* (kp) enter *)
3239 ondone text;
3240 onleave Confirm;
3241 G.postRedisplay "textentrykeyboard after confirm"
3243 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3244 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3245 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3246 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3248 | 0xff1b -> (* escape*)
3249 if String.length text = 0
3250 then (
3251 begin match opthist with
3252 | None -> ()
3253 | Some (_, onhistcancel) -> onhistcancel ()
3254 end;
3255 onleave Cancel;
3256 state.text <- "";
3257 G.postRedisplay "textentrykeyboard after cancel2"
3259 else (
3260 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3263 | 0xff9f | 0xffff -> () (* delete *)
3265 | _ when key != 0
3266 && key land 0xff00 != 0xff00 (* keyboard *)
3267 && key land 0xfe00 != 0xfe00 (* xkb *)
3268 && key land 0xfd00 != 0xfd00 (* 3270 *)
3270 begin match onkey text key with
3271 | TEdone text ->
3272 ondone text;
3273 onleave Confirm;
3274 G.postRedisplay "textentrykeyboard after confirm2";
3276 | TEcont text ->
3277 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3279 | TEstop ->
3280 onleave Cancel;
3281 G.postRedisplay "textentrykeyboard after cancel3"
3283 | TEswitch te ->
3284 state.mode <- Textentry (te, onleave);
3285 G.postRedisplay "textentrykeyboard switch";
3286 end;
3288 | _ ->
3289 vlog "unhandled key %s" (Wsi.keyname key)
3292 let firstof first active =
3293 if first > active || abs (first - active) > fstate.maxrows - 1
3294 then max 0 (active - (fstate.maxrows/2))
3295 else first
3298 let calcfirst first active =
3299 if active > first
3300 then
3301 let rows = active - first in
3302 if rows > fstate.maxrows then active - fstate.maxrows else first
3303 else active
3306 let scrollph y maxy =
3307 let sh = float (maxy + state.winh) /. float state.winh in
3308 let sh = float state.winh /. sh in
3309 let sh = max sh (float conf.scrollh) in
3311 let percent = float y /. float maxy in
3312 let position = (float state.winh -. sh) *. percent in
3314 let position =
3315 if position +. sh > float state.winh
3316 then float state.winh -. sh
3317 else position
3319 position, sh;
3322 let coe s = (s :> uioh);;
3324 class listview ~(source:lvsource) ~trusted ~modehash =
3325 object (self)
3326 val m_pan = source#getpan
3327 val m_first = source#getfirst
3328 val m_active = source#getactive
3329 val m_qsearch = source#getqsearch
3330 val m_prev_uioh = state.uioh
3332 method private elemunder y =
3333 let n = y / (fstate.fontsize+1) in
3334 if m_first + n < source#getitemcount
3335 then (
3336 if source#hasaction (m_first + n)
3337 then Some (m_first + n)
3338 else None
3340 else None
3342 method display =
3343 Gl.enable `blend;
3344 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3345 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3346 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3347 GlDraw.color (1., 1., 1.);
3348 Gl.enable `texture_2d;
3349 let fs = fstate.fontsize in
3350 let nfs = fs + 1 in
3351 let ww = fstate.wwidth in
3352 let tabw = 30.0*.ww in
3353 let itemcount = source#getitemcount in
3354 let rec loop row =
3355 if (row - m_first) > fstate.maxrows
3356 then ()
3357 else (
3358 if row >= 0 && row < itemcount
3359 then (
3360 let (s, level) = source#getitem row in
3361 let y = (row - m_first) * nfs in
3362 let x = 5.0 +. float (level + m_pan) *. ww in
3363 if row = m_active
3364 then (
3365 Gl.disable `texture_2d;
3366 GlDraw.polygon_mode `both `line;
3367 let alpha = if source#hasaction row then 0.9 else 0.3 in
3368 GlDraw.color (1., 1., 1.) ~alpha;
3369 GlDraw.rect (1., float (y + 1))
3370 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3371 GlDraw.polygon_mode `both `fill;
3372 GlDraw.color (1., 1., 1.);
3373 Gl.enable `texture_2d;
3376 let drawtabularstring s =
3377 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3378 if trusted
3379 then
3380 let tabpos = try String.index s '\t' with Not_found -> -1 in
3381 if tabpos > 0
3382 then
3383 let len = String.length s - tabpos - 1 in
3384 let s1 = String.sub s 0 tabpos
3385 and s2 = String.sub s (tabpos + 1) len in
3386 let nx = drawstr x s1 in
3387 let sw = nx -. x in
3388 let x = x +. (max tabw sw) in
3389 drawstr x s2
3390 else
3391 drawstr x s
3392 else
3393 drawstr x s
3395 let _ = drawtabularstring s in
3396 loop (row+1)
3400 loop m_first;
3401 Gl.disable `blend;
3402 Gl.disable `texture_2d;
3404 method updownlevel incr =
3405 let len = source#getitemcount in
3406 let curlevel =
3407 if m_active >= 0 && m_active < len
3408 then snd (source#getitem m_active)
3409 else -1
3411 let rec flow i =
3412 if i = len then i-1 else if i = -1 then 0 else
3413 let _, l = source#getitem i in
3414 if l != curlevel then i else flow (i+incr)
3416 let active = flow m_active in
3417 let first = calcfirst m_first active in
3418 G.postRedisplay "outline updownlevel";
3419 {< m_active = active; m_first = first >}
3421 method private key1 key mask =
3422 let set1 active first qsearch =
3423 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3425 let search active pattern incr =
3426 let active = if active = -1 then m_first else active in
3427 let dosearch re =
3428 let rec loop n =
3429 if n >= 0 && n < source#getitemcount
3430 then (
3431 let s, _ = source#getitem n in
3433 (try ignore (Str.search_forward re s 0); true
3434 with Not_found -> false)
3435 then Some n
3436 else loop (n + incr)
3438 else None
3440 loop active
3443 let re = Str.regexp_case_fold pattern in
3444 dosearch re
3445 with Failure s ->
3446 state.text <- s;
3447 None
3449 let itemcount = source#getitemcount in
3450 let find start incr =
3451 let rec find i =
3452 if i = -1 || i = itemcount
3453 then -1
3454 else (
3455 if source#hasaction i
3456 then i
3457 else find (i + incr)
3460 find start
3462 let set active first =
3463 let first = bound first 0 (itemcount - fstate.maxrows) in
3464 state.text <- "";
3465 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3467 let navigate incr =
3468 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3469 let active, first =
3470 let incr1 = if incr > 0 then 1 else -1 in
3471 if isvisible m_first m_active
3472 then
3473 let next =
3474 let next = m_active + incr in
3475 let next =
3476 if next < 0 || next >= itemcount
3477 then -1
3478 else find next incr1
3480 if abs (m_active - next) > fstate.maxrows
3481 then -1
3482 else next
3484 if next = -1
3485 then
3486 let first = m_first + incr in
3487 let first = bound first 0 (itemcount - 1) in
3488 let next =
3489 let next = m_active + incr in
3490 let next = bound next 0 (itemcount - 1) in
3491 find next ~-incr1
3493 let active =
3494 if next = -1
3495 then m_active
3496 else (
3497 if isvisible first next
3498 then next
3499 else m_active
3502 active, first
3503 else
3504 let first = min next m_first in
3505 let first =
3506 if abs (next - first) > fstate.maxrows
3507 then first + incr
3508 else first
3510 next, first
3511 else
3512 let first = m_first + incr in
3513 let first = bound first 0 (itemcount - 1) in
3514 let active =
3515 let next = m_active + incr in
3516 let next = bound next 0 (itemcount - 1) in
3517 let next = find next incr1 in
3518 let active =
3519 if next = -1 || abs (m_active - first) > fstate.maxrows
3520 then (
3521 let active = if m_active = -1 then next else m_active in
3522 active
3524 else next
3526 if isvisible first active
3527 then active
3528 else -1
3530 active, first
3532 G.postRedisplay "listview navigate";
3533 set active first;
3535 match key with
3536 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3537 let incr = if key = 0x72 then -1 else 1 in
3538 let active, first =
3539 match search (m_active + incr) m_qsearch incr with
3540 | None ->
3541 state.text <- m_qsearch ^ " [not found]";
3542 m_active, m_first
3543 | Some active ->
3544 state.text <- m_qsearch;
3545 active, firstof m_first active
3547 G.postRedisplay "listview ctrl-r/s";
3548 set1 active first m_qsearch;
3550 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3551 if m_active >= 0 && m_active < source#getitemcount
3552 then (
3553 let s, _ = source#getitem m_active in
3554 selstring s;
3556 coe self
3558 | 0xff08 -> (* backspace *)
3559 if String.length m_qsearch = 0
3560 then coe self
3561 else (
3562 let qsearch = withoutlastutf8 m_qsearch in
3563 let len = String.length qsearch in
3564 if len = 0
3565 then (
3566 state.text <- "";
3567 G.postRedisplay "listview empty qsearch";
3568 set1 m_active m_first "";
3570 else
3571 let active, first =
3572 match search m_active qsearch ~-1 with
3573 | None ->
3574 state.text <- qsearch ^ " [not found]";
3575 m_active, m_first
3576 | Some active ->
3577 state.text <- qsearch;
3578 active, firstof m_first active
3580 G.postRedisplay "listview backspace qsearch";
3581 set1 active first qsearch
3584 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3585 let pattern = m_qsearch ^ toutf8 key in
3586 let active, first =
3587 match search m_active pattern 1 with
3588 | None ->
3589 state.text <- pattern ^ " [not found]";
3590 m_active, m_first
3591 | Some active ->
3592 state.text <- pattern;
3593 active, firstof m_first active
3595 G.postRedisplay "listview qsearch add";
3596 set1 active first pattern;
3598 | 0xff1b -> (* escape *)
3599 state.text <- "";
3600 if String.length m_qsearch = 0
3601 then (
3602 G.postRedisplay "list view escape";
3603 begin
3604 match
3605 source#exit (coe self) true m_active m_first m_pan m_qsearch
3606 with
3607 | None -> m_prev_uioh
3608 | Some uioh -> uioh
3611 else (
3612 G.postRedisplay "list view kill qsearch";
3613 source#setqsearch "";
3614 coe {< m_qsearch = "" >}
3617 | 0xff0d | 0xff8d -> (* (kp) enter *)
3618 state.text <- "";
3619 let self = {< m_qsearch = "" >} in
3620 source#setqsearch "";
3621 let opt =
3622 G.postRedisplay "listview enter";
3623 if m_active >= 0 && m_active < source#getitemcount
3624 then (
3625 source#exit (coe self) false m_active m_first m_pan "";
3627 else (
3628 source#exit (coe self) true m_active m_first m_pan "";
3631 begin match opt with
3632 | None -> m_prev_uioh
3633 | Some uioh -> uioh
3636 | 0xff9f | 0xffff -> (* (kp) delete *)
3637 coe self
3639 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3640 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3641 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3642 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3644 | 0xff53 | 0xff98 -> (* (kp) right *)
3645 state.text <- "";
3646 G.postRedisplay "listview right";
3647 coe {< m_pan = m_pan - 1 >}
3649 | 0xff51 | 0xff96 -> (* (kp) left *)
3650 state.text <- "";
3651 G.postRedisplay "listview left";
3652 coe {< m_pan = m_pan + 1 >}
3654 | 0xff50 | 0xff95 -> (* (kp) home *)
3655 let active = find 0 1 in
3656 G.postRedisplay "listview home";
3657 set active 0;
3659 | 0xff57 | 0xff9c -> (* (kp) end *)
3660 let first = max 0 (itemcount - fstate.maxrows) in
3661 let active = find (itemcount - 1) ~-1 in
3662 G.postRedisplay "listview end";
3663 set active first;
3665 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3666 coe self
3668 | _ ->
3669 dolog "listview unknown key %#x" key; coe self
3671 method key key mask =
3672 match state.mode with
3673 | Textentry te -> textentrykeyboard key mask te; coe self
3674 | _ -> self#key1 key mask
3676 method button button down x y _ =
3677 let opt =
3678 match button with
3679 | 1 when x > state.winw - conf.scrollbw ->
3680 G.postRedisplay "listview scroll";
3681 if down
3682 then
3683 let _, position, sh = self#scrollph in
3684 if y > truncate position && y < truncate (position +. sh)
3685 then (
3686 state.mstate <- Mscrolly;
3687 Some (coe self)
3689 else
3690 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3691 let first = truncate (s *. float source#getitemcount) in
3692 let first = min source#getitemcount first in
3693 Some (coe {< m_first = first; m_active = first >})
3694 else (
3695 state.mstate <- Mnone;
3696 Some (coe self);
3698 | 1 when not down ->
3699 begin match self#elemunder y with
3700 | Some n ->
3701 G.postRedisplay "listview click";
3702 source#exit
3703 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3704 | _ ->
3705 Some (coe self)
3707 | n when (n == 4 || n == 5) && not down ->
3708 let len = source#getitemcount in
3709 let first =
3710 if n = 5 && m_first + fstate.maxrows >= len
3711 then
3712 m_first
3713 else
3714 let first = m_first + (if n == 4 then -1 else 1) in
3715 bound first 0 (len - 1)
3717 G.postRedisplay "listview wheel";
3718 Some (coe {< m_first = first >})
3719 | n when (n = 6 || n = 7) && not down ->
3720 let inc = m_first + (if n = 7 then -1 else 1) in
3721 G.postRedisplay "listview hwheel";
3722 Some (coe {< m_pan = m_pan + inc >})
3723 | _ ->
3724 Some (coe self)
3726 match opt with
3727 | None -> m_prev_uioh
3728 | Some uioh -> uioh
3730 method motion _ y =
3731 match state.mstate with
3732 | Mscrolly ->
3733 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3734 let first = truncate (s *. float source#getitemcount) in
3735 let first = min source#getitemcount first in
3736 G.postRedisplay "listview motion";
3737 coe {< m_first = first; m_active = first >}
3738 | _ -> coe self
3740 method pmotion x y =
3741 if x < state.winw - conf.scrollbw
3742 then
3743 let n =
3744 match self#elemunder y with
3745 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3746 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3748 let o =
3749 if n != m_active
3750 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3751 else self
3753 coe o
3754 else (
3755 Wsi.setcursor Wsi.CURSOR_INHERIT;
3756 coe self
3759 method infochanged _ = ()
3761 method scrollpw = (0, 0.0, 0.0)
3762 method scrollph =
3763 let nfs = fstate.fontsize + 1 in
3764 let y = m_first * nfs in
3765 let itemcount = source#getitemcount in
3766 let maxi = max 0 (itemcount - fstate.maxrows) in
3767 let maxy = maxi * nfs in
3768 let p, h = scrollph y maxy in
3769 conf.scrollbw, p, h
3771 method modehash = modehash
3772 method eformsgs = false
3773 end;;
3775 class outlinelistview ~source =
3776 object (self)
3777 inherit listview
3778 ~source:(source :> lvsource)
3779 ~trusted:false
3780 ~modehash:(findkeyhash conf "outline")
3781 as super
3783 method key key mask =
3784 let calcfirst first active =
3785 if active > first
3786 then
3787 let rows = active - first in
3788 let maxrows =
3789 if String.length state.text = 0
3790 then fstate.maxrows
3791 else fstate.maxrows - 2
3793 if rows > maxrows then active - maxrows else first
3794 else active
3796 let navigate incr =
3797 let active = m_active + incr in
3798 let active = bound active 0 (source#getitemcount - 1) in
3799 let first = calcfirst m_first active in
3800 G.postRedisplay "outline navigate";
3801 coe {< m_active = active; m_first = first >}
3803 let ctrl = Wsi.withctrl mask in
3804 match key with
3805 | 110 when ctrl -> (* ctrl-n *)
3806 source#narrow m_qsearch;
3807 G.postRedisplay "outline ctrl-n";
3808 coe {< m_first = 0; m_active = 0 >}
3810 | 117 when ctrl -> (* ctrl-u *)
3811 source#denarrow;
3812 G.postRedisplay "outline ctrl-u";
3813 state.text <- "";
3814 coe {< m_first = 0; m_active = 0 >}
3816 | 108 when ctrl -> (* ctrl-l *)
3817 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3818 G.postRedisplay "outline ctrl-l";
3819 coe {< m_first = first >}
3821 | 0xff9f | 0xffff -> (* (kp) delete *)
3822 source#remove m_active;
3823 G.postRedisplay "outline delete";
3824 let active = max 0 (m_active-1) in
3825 coe {< m_first = firstof m_first active;
3826 m_active = active >}
3828 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3829 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3830 | 0xff55 | 0xff9a -> (* (kp) prior *)
3831 navigate ~-(fstate.maxrows)
3832 | 0xff56 | 0xff9b -> (* (kp) next *)
3833 navigate fstate.maxrows
3835 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3836 let o =
3837 if ctrl
3838 then (
3839 G.postRedisplay "outline ctrl right";
3840 {< m_pan = m_pan + 1 >}
3842 else self#updownlevel 1
3844 coe o
3846 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3847 let o =
3848 if ctrl
3849 then (
3850 G.postRedisplay "outline ctrl left";
3851 {< m_pan = m_pan - 1 >}
3853 else self#updownlevel ~-1
3855 coe o
3857 | 0xff50 | 0xff95 -> (* (kp) home *)
3858 G.postRedisplay "outline home";
3859 coe {< m_first = 0; m_active = 0 >}
3861 | 0xff57 | 0xff9c -> (* (kp) end *)
3862 let active = source#getitemcount - 1 in
3863 let first = max 0 (active - fstate.maxrows) in
3864 G.postRedisplay "outline end";
3865 coe {< m_active = active; m_first = first >}
3867 | _ -> super#key key mask
3870 let outlinesource usebookmarks =
3871 let empty = [||] in
3872 (object
3873 inherit lvsourcebase
3874 val mutable m_items = empty
3875 val mutable m_orig_items = empty
3876 val mutable m_prev_items = empty
3877 val mutable m_narrow_pattern = ""
3878 val mutable m_hadremovals = false
3880 method getitemcount =
3881 Array.length m_items + (if m_hadremovals then 1 else 0)
3883 method getitem n =
3884 if n == Array.length m_items && m_hadremovals
3885 then
3886 ("[Confirm removal]", 0)
3887 else
3888 let s, n, _ = m_items.(n) in
3889 (s, n)
3891 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3892 ignore (uioh, first, qsearch);
3893 let confrimremoval = m_hadremovals && active = Array.length m_items in
3894 let items =
3895 if String.length m_narrow_pattern = 0
3896 then m_orig_items
3897 else m_items
3899 if not cancel
3900 then (
3901 if not confrimremoval
3902 then(
3903 let _, _, anchor = m_items.(active) in
3904 gotoghyll (getanchory anchor);
3905 m_items <- items;
3907 else (
3908 state.bookmarks <- Array.to_list m_items;
3909 m_orig_items <- m_items;
3912 else m_items <- items;
3913 m_pan <- pan;
3914 None
3916 method hasaction _ = true
3918 method greetmsg =
3919 if Array.length m_items != Array.length m_orig_items
3920 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3921 else ""
3923 method narrow pattern =
3924 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3925 match reopt with
3926 | None -> ()
3927 | Some re ->
3928 let rec loop accu n =
3929 if n = -1
3930 then (
3931 m_narrow_pattern <- pattern;
3932 m_items <- Array.of_list accu
3934 else
3935 let (s, _, _) as o = m_items.(n) in
3936 let accu =
3937 if (try ignore (Str.search_forward re s 0); true
3938 with Not_found -> false)
3939 then o :: accu
3940 else accu
3942 loop accu (n-1)
3944 loop [] (Array.length m_items - 1)
3946 method denarrow =
3947 m_orig_items <- (
3948 if usebookmarks
3949 then Array.of_list state.bookmarks
3950 else state.outlines
3952 m_items <- m_orig_items
3954 method remove m =
3955 if usebookmarks
3956 then
3957 if m >= 0 && m < Array.length m_items
3958 then (
3959 m_hadremovals <- true;
3960 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3961 let n = if n >= m then n+1 else n in
3962 m_items.(n)
3966 method reset anchor items =
3967 m_hadremovals <- false;
3968 if m_orig_items == empty || m_prev_items != items
3969 then (
3970 m_orig_items <- items;
3971 if String.length m_narrow_pattern = 0
3972 then m_items <- items;
3974 m_prev_items <- items;
3975 let rely = getanchory anchor in
3976 let active =
3977 let rec loop n best bestd =
3978 if n = Array.length m_items
3979 then best
3980 else
3981 let (_, _, anchor) = m_items.(n) in
3982 let orely = getanchory anchor in
3983 let d = abs (orely - rely) in
3984 if d < bestd
3985 then loop (n+1) n d
3986 else loop (n+1) best bestd
3988 loop 0 ~-1 max_int
3990 m_active <- active;
3991 m_first <- firstof m_first active
3992 end)
3995 let enterselector usebookmarks =
3996 let source = outlinesource usebookmarks in
3997 fun errmsg ->
3998 let outlines =
3999 if usebookmarks
4000 then Array.of_list state.bookmarks
4001 else state.outlines
4003 if Array.length outlines = 0
4004 then (
4005 showtext ' ' errmsg;
4007 else (
4008 state.text <- source#greetmsg;
4009 Wsi.setcursor Wsi.CURSOR_INHERIT;
4010 let anchor = getanchor () in
4011 source#reset anchor outlines;
4012 state.uioh <- coe (new outlinelistview ~source);
4013 G.postRedisplay "enter selector";
4017 let enteroutlinemode =
4018 let f = enterselector false in
4019 fun ()-> f "Document has no outline";
4022 let enterbookmarkmode =
4023 let f = enterselector true in
4024 fun () -> f "Document has no bookmarks (yet)";
4027 let color_of_string s =
4028 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4029 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4033 let color_to_string (r, g, b) =
4034 let r = truncate (r *. 256.0)
4035 and g = truncate (g *. 256.0)
4036 and b = truncate (b *. 256.0) in
4037 Printf.sprintf "%d/%d/%d" r g b
4040 let irect_of_string s =
4041 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4044 let irect_to_string (x0,y0,x1,y1) =
4045 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4048 let makecheckers () =
4049 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4050 following to say:
4051 converted by Issac Trotts. July 25, 2002 *)
4052 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4053 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4054 let id = GlTex.gen_texture () in
4055 GlTex.bind_texture `texture_2d id;
4056 GlPix.store (`unpack_alignment 1);
4057 GlTex.image2d image;
4058 List.iter (GlTex.parameter ~target:`texture_2d)
4059 [ `mag_filter `nearest; `min_filter `nearest ];
4063 let setcheckers enabled =
4064 match state.texid with
4065 | None ->
4066 if enabled then state.texid <- Some (makecheckers ())
4068 | Some texid ->
4069 if not enabled
4070 then (
4071 GlTex.delete_texture texid;
4072 state.texid <- None;
4076 let int_of_string_with_suffix s =
4077 let l = String.length s in
4078 let s1, shift =
4079 if l > 1
4080 then
4081 let suffix = Char.lowercase s.[l-1] in
4082 match suffix with
4083 | 'k' -> String.sub s 0 (l-1), 10
4084 | 'm' -> String.sub s 0 (l-1), 20
4085 | 'g' -> String.sub s 0 (l-1), 30
4086 | _ -> s, 0
4087 else s, 0
4089 let n = int_of_string s1 in
4090 let m = n lsl shift in
4091 if m < 0 || m < n
4092 then raise (Failure "value too large")
4093 else m
4096 let string_with_suffix_of_int n =
4097 if n = 0
4098 then "0"
4099 else
4100 let n, s =
4101 if n land ((1 lsl 30) - 1) = 0
4102 then n lsr 30, "G"
4103 else (
4104 if n land ((1 lsl 20) - 1) = 0
4105 then n lsr 20, "M"
4106 else (
4107 if n land ((1 lsl 10) - 1) = 0
4108 then n lsr 10, "K"
4109 else n, ""
4113 let rec loop s n =
4114 let h = n mod 1000 in
4115 let n = n / 1000 in
4116 if n = 0
4117 then string_of_int h ^ s
4118 else (
4119 let s = Printf.sprintf "_%03d%s" h s in
4120 loop s n
4123 loop "" n ^ s;
4126 let defghyllscroll = (40, 8, 32);;
4127 let ghyllscroll_of_string s =
4128 let (n, a, b) as nab =
4129 if s = "default"
4130 then defghyllscroll
4131 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4133 if n <= a || n <= b || a >= b
4134 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4135 nab;
4138 let ghyllscroll_to_string ((n, a, b) as nab) =
4139 if nab = defghyllscroll
4140 then "default"
4141 else Printf.sprintf "%d,%d,%d" n a b;
4144 let describe_location () =
4145 let fn = page_of_y state.y in
4146 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4147 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4148 let percent =
4149 if maxy <= 0
4150 then 100.
4151 else (100. *. (float state.y /. float maxy))
4153 if fn = ln
4154 then
4155 Printf.sprintf "page %d of %d [%.2f%%]"
4156 (fn+1) state.pagecount percent
4157 else
4158 Printf.sprintf
4159 "pages %d-%d of %d [%.2f%%]"
4160 (fn+1) (ln+1) state.pagecount percent
4163 let setpresentationmode v =
4164 let n = page_of_y state.y in
4165 state.anchor <- (n, 0.0, 1.0);
4166 conf.presentation <- v;
4167 if conf.presentation
4168 then (
4169 if not conf.scrollbarinpm
4170 then state.scrollw <- 0;
4172 else state.scrollw <- conf.scrollbw;
4173 represent ();
4176 let enterinfomode =
4177 let btos b = if b then "\xe2\x88\x9a" else "" in
4178 let showextended = ref false in
4179 let leave mode = function
4180 | Confirm -> state.mode <- mode
4181 | Cancel -> state.mode <- mode in
4182 let src =
4183 (object
4184 val mutable m_first_time = true
4185 val mutable m_l = []
4186 val mutable m_a = [||]
4187 val mutable m_prev_uioh = nouioh
4188 val mutable m_prev_mode = View
4190 inherit lvsourcebase
4192 method reset prev_mode prev_uioh =
4193 m_a <- Array.of_list (List.rev m_l);
4194 m_l <- [];
4195 m_prev_mode <- prev_mode;
4196 m_prev_uioh <- prev_uioh;
4197 if m_first_time
4198 then (
4199 let rec loop n =
4200 if n >= Array.length m_a
4201 then ()
4202 else
4203 match m_a.(n) with
4204 | _, _, _, Action _ -> m_active <- n
4205 | _ -> loop (n+1)
4207 loop 0;
4208 m_first_time <- false;
4211 method int name get set =
4212 m_l <-
4213 (name, `int get, 1, Action (
4214 fun u ->
4215 let ondone s =
4216 try set (int_of_string s)
4217 with exn ->
4218 state.text <- Printf.sprintf "bad integer `%s': %s"
4219 s (exntos exn)
4221 state.text <- "";
4222 let te = name ^ ": ", "", None, intentry, ondone, true in
4223 state.mode <- Textentry (te, leave m_prev_mode);
4225 )) :: m_l
4227 method int_with_suffix name get set =
4228 m_l <-
4229 (name, `intws get, 1, Action (
4230 fun u ->
4231 let ondone s =
4232 try set (int_of_string_with_suffix s)
4233 with exn ->
4234 state.text <- Printf.sprintf "bad integer `%s': %s"
4235 s (exntos exn)
4237 state.text <- "";
4238 let te =
4239 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4241 state.mode <- Textentry (te, leave m_prev_mode);
4243 )) :: m_l
4245 method bool ?(offset=1) ?(btos=btos) name get set =
4246 m_l <-
4247 (name, `bool (btos, get), offset, Action (
4248 fun u ->
4249 let v = get () in
4250 set (not v);
4252 )) :: m_l
4254 method color name get set =
4255 m_l <-
4256 (name, `color get, 1, Action (
4257 fun u ->
4258 let invalid = (nan, nan, nan) in
4259 let ondone s =
4260 let c =
4261 try color_of_string s
4262 with exn ->
4263 state.text <- Printf.sprintf "bad color `%s': %s"
4264 s (exntos exn);
4265 invalid
4267 if c <> invalid
4268 then set c;
4270 let te = name ^ ": ", "", None, textentry, ondone, true in
4271 state.text <- color_to_string (get ());
4272 state.mode <- Textentry (te, leave m_prev_mode);
4274 )) :: m_l
4276 method string name get set =
4277 m_l <-
4278 (name, `string get, 1, Action (
4279 fun u ->
4280 let ondone s = set s in
4281 let te = name ^ ": ", "", None, textentry, ondone, true in
4282 state.mode <- Textentry (te, leave m_prev_mode);
4284 )) :: m_l
4286 method colorspace name get set =
4287 m_l <-
4288 (name, `string get, 1, Action (
4289 fun _ ->
4290 let source =
4291 let vals = [| "rgb"; "bgr"; "gray" |] in
4292 (object
4293 inherit lvsourcebase
4295 initializer
4296 m_active <- int_of_colorspace conf.colorspace;
4297 m_first <- 0;
4299 method getitemcount = Array.length vals
4300 method getitem n = (vals.(n), 0)
4301 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4302 ignore (uioh, first, pan, qsearch);
4303 if not cancel then set active;
4304 None
4305 method hasaction _ = true
4306 end)
4308 state.text <- "";
4309 let modehash = findkeyhash conf "info" in
4310 coe (new listview ~source ~trusted:true ~modehash)
4311 )) :: m_l
4313 method fitmodel name get set =
4314 m_l <-
4315 (name, `string get, 1, Action (
4316 fun _ ->
4317 let source =
4318 let vals = [| "fit width"; "proportional"; "fit page" |] in
4319 (object
4320 inherit lvsourcebase
4322 initializer
4323 m_active <- int_of_fitmodel conf.fitmodel;
4324 m_first <- 0;
4326 method getitemcount = Array.length vals
4327 method getitem n = (vals.(n), 0)
4328 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4329 ignore (uioh, first, pan, qsearch);
4330 if not cancel then set active;
4331 None
4332 method hasaction _ = true
4333 end)
4335 state.text <- "";
4336 let modehash = findkeyhash conf "info" in
4337 coe (new listview ~source ~trusted:true ~modehash)
4338 )) :: m_l
4340 method caption s offset =
4341 m_l <- (s, `empty, offset, Noaction) :: m_l
4343 method caption2 s f offset =
4344 m_l <- (s, `string f, offset, Noaction) :: m_l
4346 method getitemcount = Array.length m_a
4348 method getitem n =
4349 let tostr = function
4350 | `int f -> string_of_int (f ())
4351 | `intws f -> string_with_suffix_of_int (f ())
4352 | `string f -> f ()
4353 | `color f -> color_to_string (f ())
4354 | `bool (btos, f) -> btos (f ())
4355 | `empty -> ""
4357 let name, t, offset, _ = m_a.(n) in
4358 ((let s = tostr t in
4359 if String.length s > 0
4360 then Printf.sprintf "%s\t%s" name s
4361 else name),
4362 offset)
4364 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4365 let uiohopt =
4366 if not cancel
4367 then (
4368 m_qsearch <- qsearch;
4369 let uioh =
4370 match m_a.(active) with
4371 | _, _, _, Action f -> f uioh
4372 | _ -> uioh
4374 Some uioh
4376 else None
4378 m_active <- active;
4379 m_first <- first;
4380 m_pan <- pan;
4381 uiohopt
4383 method hasaction n =
4384 match m_a.(n) with
4385 | _, _, _, Action _ -> true
4386 | _ -> false
4387 end)
4389 let rec fillsrc prevmode prevuioh =
4390 let sep () = src#caption "" 0 in
4391 let colorp name get set =
4392 src#string name
4393 (fun () -> color_to_string (get ()))
4394 (fun v ->
4396 let c = color_of_string v in
4397 set c
4398 with exn ->
4399 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4402 let oldmode = state.mode in
4403 let birdseye = isbirdseye state.mode in
4405 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4407 src#bool "presentation mode"
4408 (fun () -> conf.presentation)
4409 (fun v -> setpresentationmode v);
4411 src#bool "ignore case in searches"
4412 (fun () -> conf.icase)
4413 (fun v -> conf.icase <- v);
4415 src#bool "preload"
4416 (fun () -> conf.preload)
4417 (fun v -> conf.preload <- v);
4419 src#bool "highlight links"
4420 (fun () -> conf.hlinks)
4421 (fun v -> conf.hlinks <- v);
4423 src#bool "under info"
4424 (fun () -> conf.underinfo)
4425 (fun v -> conf.underinfo <- v);
4427 src#bool "persistent bookmarks"
4428 (fun () -> conf.savebmarks)
4429 (fun v -> conf.savebmarks <- v);
4431 src#fitmodel "fit model"
4432 (fun () -> fitmodel_to_string conf.fitmodel)
4433 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4435 src#bool "trim margins"
4436 (fun () -> conf.trimmargins)
4437 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4439 src#bool "persistent location"
4440 (fun () -> conf.jumpback)
4441 (fun v -> conf.jumpback <- v);
4443 sep ();
4444 src#int "inter-page space"
4445 (fun () -> conf.interpagespace)
4446 (fun n ->
4447 conf.interpagespace <- n;
4448 docolumns conf.columns;
4449 let pageno, py =
4450 match state.layout with
4451 | [] -> 0, 0
4452 | l :: _ ->
4453 l.pageno, l.pagey
4455 state.maxy <- calcheight ();
4456 let y = getpagey pageno in
4457 gotoy (y + py)
4460 src#int "page bias"
4461 (fun () -> conf.pagebias)
4462 (fun v -> conf.pagebias <- v);
4464 src#int "scroll step"
4465 (fun () -> conf.scrollstep)
4466 (fun n -> conf.scrollstep <- n);
4468 src#int "horizontal scroll step"
4469 (fun () -> conf.hscrollstep)
4470 (fun v -> conf.hscrollstep <- v);
4472 src#int "auto scroll step"
4473 (fun () ->
4474 match state.autoscroll with
4475 | Some step -> step
4476 | _ -> conf.autoscrollstep)
4477 (fun n ->
4478 if state.autoscroll <> None
4479 then state.autoscroll <- Some n;
4480 conf.autoscrollstep <- n);
4482 src#int "zoom"
4483 (fun () -> truncate (conf.zoom *. 100.))
4484 (fun v -> setzoom ((float v) /. 100.));
4486 src#int "rotation"
4487 (fun () -> conf.angle)
4488 (fun v -> reqlayout v conf.fitmodel);
4490 src#int "scroll bar width"
4491 (fun () -> state.scrollw)
4492 (fun v ->
4493 state.scrollw <- v;
4494 conf.scrollbw <- v;
4495 reshape state.winw state.winh;
4498 src#int "scroll handle height"
4499 (fun () -> conf.scrollh)
4500 (fun v -> conf.scrollh <- v;);
4502 src#int "thumbnail width"
4503 (fun () -> conf.thumbw)
4504 (fun v ->
4505 conf.thumbw <- min 4096 v;
4506 match oldmode with
4507 | Birdseye beye ->
4508 leavebirdseye beye false;
4509 enterbirdseye ()
4510 | _ -> ()
4513 let mode = state.mode in
4514 src#string "columns"
4515 (fun () ->
4516 match conf.columns with
4517 | Csingle _ -> "1"
4518 | Cmulti (multi, _) -> multicolumns_to_string multi
4519 | Csplit (count, _) -> "-" ^ string_of_int count
4521 (fun v ->
4522 let n, a, b = multicolumns_of_string v in
4523 setcolumns mode n a b);
4525 sep ();
4526 src#caption "Presentation mode" 0;
4527 src#bool "scrollbar visible"
4528 (fun () -> conf.scrollbarinpm)
4529 (fun v ->
4530 if v != conf.scrollbarinpm
4531 then (
4532 conf.scrollbarinpm <- v;
4533 if conf.presentation
4534 then (
4535 state.scrollw <- if v then conf.scrollbw else 0;
4536 reshape state.winw state.winh;
4541 sep ();
4542 src#caption "Pixmap cache" 0;
4543 src#int_with_suffix "size (advisory)"
4544 (fun () -> conf.memlimit)
4545 (fun v -> conf.memlimit <- v);
4547 src#caption2 "used"
4548 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4549 (string_with_suffix_of_int state.memused)
4550 (Hashtbl.length state.tilemap)) 1;
4552 sep ();
4553 src#caption "Layout" 0;
4554 src#caption2 "Dimension"
4555 (fun () ->
4556 Printf.sprintf "%dx%d (virtual %dx%d)"
4557 state.winw state.winh
4558 state.w state.maxy)
4560 if conf.debug
4561 then
4562 src#caption2 "Position" (fun () ->
4563 Printf.sprintf "%dx%d" state.x state.y
4565 else
4566 src#caption2 "Position" (fun () -> describe_location ()) 1
4569 sep ();
4570 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4571 "Save these parameters as global defaults at exit"
4572 (fun () -> conf.bedefault)
4573 (fun v -> conf.bedefault <- v)
4576 sep ();
4577 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4578 src#bool ~offset:0 ~btos "Extended parameters"
4579 (fun () -> !showextended)
4580 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4581 if !showextended
4582 then (
4583 src#bool "checkers"
4584 (fun () -> conf.checkers)
4585 (fun v -> conf.checkers <- v; setcheckers v);
4586 src#bool "update cursor"
4587 (fun () -> conf.updatecurs)
4588 (fun v -> conf.updatecurs <- v);
4589 src#bool "verbose"
4590 (fun () -> conf.verbose)
4591 (fun v -> conf.verbose <- v);
4592 src#bool "invert colors"
4593 (fun () -> conf.invert)
4594 (fun v -> conf.invert <- v);
4595 src#bool "max fit"
4596 (fun () -> conf.maxhfit)
4597 (fun v -> conf.maxhfit <- v);
4598 src#bool "redirect stderr"
4599 (fun () -> conf.redirectstderr)
4600 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4601 src#string "uri launcher"
4602 (fun () -> conf.urilauncher)
4603 (fun v -> conf.urilauncher <- v);
4604 src#string "path launcher"
4605 (fun () -> conf.pathlauncher)
4606 (fun v -> conf.pathlauncher <- v);
4607 src#string "tile size"
4608 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4609 (fun v ->
4611 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4612 conf.tilew <- max 64 w;
4613 conf.tileh <- max 64 h;
4614 flushtiles ();
4615 with exn ->
4616 state.text <- Printf.sprintf "bad tile size `%s': %s"
4617 v (exntos exn)
4619 src#int "texture count"
4620 (fun () -> conf.texcount)
4621 (fun v ->
4622 if realloctexts v
4623 then conf.texcount <- v
4624 else showtext '!' " Failed to set texture count please retry later"
4626 src#int "slice height"
4627 (fun () -> conf.sliceheight)
4628 (fun v ->
4629 conf.sliceheight <- v;
4630 wcmd "sliceh %d" conf.sliceheight;
4632 src#int "anti-aliasing level"
4633 (fun () -> conf.aalevel)
4634 (fun v ->
4635 conf.aalevel <- bound v 0 8;
4636 state.anchor <- getanchor ();
4637 opendoc state.path state.password;
4639 src#string "page scroll scaling factor"
4640 (fun () -> string_of_float conf.pgscale)
4641 (fun v ->
4643 let s = float_of_string v in
4644 conf.pgscale <- s
4645 with exn ->
4646 state.text <- Printf.sprintf
4647 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4650 src#int "ui font size"
4651 (fun () -> fstate.fontsize)
4652 (fun v -> setfontsize (bound v 5 100));
4653 src#int "hint font size"
4654 (fun () -> conf.hfsize)
4655 (fun v -> conf.hfsize <- bound v 5 100);
4656 colorp "background color"
4657 (fun () -> conf.bgcolor)
4658 (fun v -> conf.bgcolor <- v);
4659 src#bool "crop hack"
4660 (fun () -> conf.crophack)
4661 (fun v -> conf.crophack <- v);
4662 src#string "trim fuzz"
4663 (fun () -> irect_to_string conf.trimfuzz)
4664 (fun v ->
4666 conf.trimfuzz <- irect_of_string v;
4667 if conf.trimmargins
4668 then settrim true conf.trimfuzz;
4669 with exn ->
4670 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4672 src#string "throttle"
4673 (fun () ->
4674 match conf.maxwait with
4675 | None -> "show place holder if page is not ready"
4676 | Some time ->
4677 if time = infinity
4678 then "wait for page to fully render"
4679 else
4680 "wait " ^ string_of_float time
4681 ^ " seconds before showing placeholder"
4683 (fun v ->
4685 let f = float_of_string v in
4686 if f <= 0.0
4687 then conf.maxwait <- None
4688 else conf.maxwait <- Some f
4689 with exn ->
4690 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4692 src#string "ghyll scroll"
4693 (fun () ->
4694 match conf.ghyllscroll with
4695 | None -> ""
4696 | Some nab -> ghyllscroll_to_string nab
4698 (fun v ->
4700 let gs =
4701 if String.length v = 0
4702 then None
4703 else Some (ghyllscroll_of_string v)
4705 conf.ghyllscroll <- gs
4706 with exn ->
4707 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4709 src#string "selection command"
4710 (fun () -> conf.selcmd)
4711 (fun v -> conf.selcmd <- v);
4712 src#string "synctex command"
4713 (fun () -> conf.stcmd)
4714 (fun v -> conf.stcmd <- v);
4715 src#colorspace "color space"
4716 (fun () -> colorspace_to_string conf.colorspace)
4717 (fun v ->
4718 conf.colorspace <- colorspace_of_int v;
4719 wcmd "cs %d" v;
4720 load state.layout;
4722 if pbousable ()
4723 then
4724 src#bool "use PBO"
4725 (fun () -> conf.usepbo)
4726 (fun v -> conf.usepbo <- v);
4727 src#bool "mouse wheel scrolls pages"
4728 (fun () -> conf.wheelbypage)
4729 (fun v -> conf.wheelbypage <- v);
4732 sep ();
4733 src#caption "Document" 0;
4734 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4735 src#caption2 "Pages"
4736 (fun () -> string_of_int state.pagecount) 1;
4737 src#caption2 "Dimensions"
4738 (fun () -> string_of_int (List.length state.pdims)) 1;
4739 if conf.trimmargins
4740 then (
4741 sep ();
4742 src#caption "Trimmed margins" 0;
4743 src#caption2 "Dimensions"
4744 (fun () -> string_of_int (List.length state.pdims)) 1;
4747 sep ();
4748 src#caption "OpenGL" 0;
4749 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4750 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4752 sep ();
4753 src#caption "Location" 0;
4754 if String.length state.origin > 0
4755 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4756 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4758 src#reset prevmode prevuioh;
4760 fun () ->
4761 state.text <- "";
4762 let prevmode = state.mode
4763 and prevuioh = state.uioh in
4764 fillsrc prevmode prevuioh;
4765 let source = (src :> lvsource) in
4766 let modehash = findkeyhash conf "info" in
4767 state.uioh <- coe (object (self)
4768 inherit listview ~source ~trusted:true ~modehash as super
4769 val mutable m_prevmemused = 0
4770 method infochanged = function
4771 | Memused ->
4772 if m_prevmemused != state.memused
4773 then (
4774 m_prevmemused <- state.memused;
4775 G.postRedisplay "memusedchanged";
4777 | Pdim -> G.postRedisplay "pdimchanged"
4778 | Docinfo -> fillsrc prevmode prevuioh
4780 method key key mask =
4781 if not (Wsi.withctrl mask)
4782 then
4783 match key with
4784 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4785 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4786 | _ -> super#key key mask
4787 else super#key key mask
4788 end);
4789 G.postRedisplay "info";
4792 let enterhelpmode =
4793 let source =
4794 (object
4795 inherit lvsourcebase
4796 method getitemcount = Array.length state.help
4797 method getitem n =
4798 let s, l, _ = state.help.(n) in
4799 (s, l)
4801 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4802 let optuioh =
4803 if not cancel
4804 then (
4805 m_qsearch <- qsearch;
4806 match state.help.(active) with
4807 | _, _, Action f -> Some (f uioh)
4808 | _ -> Some (uioh)
4810 else None
4812 m_active <- active;
4813 m_first <- first;
4814 m_pan <- pan;
4815 optuioh
4817 method hasaction n =
4818 match state.help.(n) with
4819 | _, _, Action _ -> true
4820 | _ -> false
4822 initializer
4823 m_active <- -1
4824 end)
4825 in fun () ->
4826 let modehash = findkeyhash conf "help" in
4827 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4828 G.postRedisplay "help";
4831 let entermsgsmode =
4832 let msgsource =
4833 let re = Str.regexp "[\r\n]" in
4834 (object
4835 inherit lvsourcebase
4836 val mutable m_items = [||]
4838 method getitemcount = 1 + Array.length m_items
4840 method getitem n =
4841 if n = 0
4842 then "[Clear]", 0
4843 else m_items.(n-1), 0
4845 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4846 ignore uioh;
4847 if not cancel
4848 then (
4849 if active = 0
4850 then Buffer.clear state.errmsgs;
4851 m_qsearch <- qsearch;
4853 m_active <- active;
4854 m_first <- first;
4855 m_pan <- pan;
4856 None
4858 method hasaction n =
4859 n = 0
4861 method reset =
4862 state.newerrmsgs <- false;
4863 let l = Str.split re (Buffer.contents state.errmsgs) in
4864 m_items <- Array.of_list l
4866 initializer
4867 m_active <- 0
4868 end)
4869 in fun () ->
4870 state.text <- "";
4871 msgsource#reset;
4872 let source = (msgsource :> lvsource) in
4873 let modehash = findkeyhash conf "listview" in
4874 state.uioh <- coe (object
4875 inherit listview ~source ~trusted:false ~modehash as super
4876 method display =
4877 if state.newerrmsgs
4878 then msgsource#reset;
4879 super#display
4880 end);
4881 G.postRedisplay "msgs";
4884 let quickbookmark ?title () =
4885 match state.layout with
4886 | [] -> ()
4887 | l :: _ ->
4888 let title =
4889 match title with
4890 | None ->
4891 let sec = Unix.gettimeofday () in
4892 let tm = Unix.localtime sec in
4893 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4894 (l.pageno+1)
4895 tm.Unix.tm_mday
4896 tm.Unix.tm_mon
4897 (tm.Unix.tm_year + 1900)
4898 tm.Unix.tm_hour
4899 tm.Unix.tm_min
4900 | Some title -> title
4902 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4905 let setautoscrollspeed step goingdown =
4906 let incr = max 1 ((abs step) / 2) in
4907 let incr = if goingdown then incr else -incr in
4908 let astep = step + incr in
4909 state.autoscroll <- Some astep;
4912 let gotounder = function
4913 | Ulinkgoto (pageno, top) ->
4914 if pageno >= 0
4915 then (
4916 addnav ();
4917 gotopage1 pageno top;
4920 | Ulinkuri s ->
4921 gotouri s
4923 | Uremote (filename, pageno) ->
4924 let path =
4925 if Sys.file_exists filename
4926 then filename
4927 else
4928 let dir = Filename.dirname state.path in
4929 let path = Filename.concat dir filename in
4930 if Sys.file_exists path
4931 then path
4932 else ""
4934 if String.length path > 0
4935 then (
4936 let anchor = getanchor () in
4937 let ranchor = state.path, state.password, anchor, state.origin in
4938 state.origin <- "";
4939 state.anchor <- (pageno, 0.0, 0.0);
4940 state.ranchors <- ranchor :: state.ranchors;
4941 opendoc path "";
4943 else showtext '!' ("Could not find " ^ filename)
4945 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4948 let canpan () =
4949 match conf.columns with
4950 | Csplit _ -> true
4951 | _ -> state.x != 0 || conf.zoom > 1.0
4954 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4956 let existsinrow pageno (columns, coverA, coverB) p =
4957 let last = ((pageno - coverA) mod columns) + columns in
4958 let rec any = function
4959 | [] -> false
4960 | l :: rest ->
4961 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4962 then p l
4963 else (
4964 if not (p l)
4965 then (if l.pageno = last then false else any rest)
4966 else true
4969 any state.layout
4972 let nextpage () =
4973 match state.layout with
4974 | [] ->
4975 let pageno = page_of_y state.y in
4976 gotoghyll (getpagey (pageno+1))
4977 | l :: rest ->
4978 match conf.columns with
4979 | Csingle _ ->
4980 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4981 then
4982 let y = clamp (pgscale state.winh) in
4983 gotoghyll y
4984 else
4985 let pageno = min (l.pageno+1) (state.pagecount-1) in
4986 gotoghyll (getpagey pageno)
4987 | Cmulti ((c, _, _) as cl, _) ->
4988 if conf.presentation
4989 && (existsinrow l.pageno cl
4990 (fun l -> l.pageh > l.pagey + l.pagevh))
4991 then
4992 let y = clamp (pgscale state.winh) in
4993 gotoghyll y
4994 else
4995 let pageno = min (l.pageno+c) (state.pagecount-1) in
4996 gotoghyll (getpagey pageno)
4997 | Csplit (n, _) ->
4998 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4999 then
5000 let pagey, pageh = getpageyh l.pageno in
5001 let pagey = pagey + pageh * l.pagecol in
5002 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5003 gotoghyll (pagey + pageh + ips)
5006 let prevpage () =
5007 match state.layout with
5008 | [] ->
5009 let pageno = page_of_y state.y in
5010 gotoghyll (getpagey (pageno-1))
5011 | l :: _ ->
5012 match conf.columns with
5013 | Csingle _ ->
5014 if conf.presentation && l.pagey != 0
5015 then
5016 gotoghyll (clamp (pgscale ~-(state.winh)))
5017 else
5018 let pageno = max 0 (l.pageno-1) in
5019 gotoghyll (getpagey pageno)
5020 | Cmulti ((c, _, coverB) as cl, _) ->
5021 if conf.presentation &&
5022 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5023 then
5024 gotoghyll (clamp (pgscale ~-(state.winh)))
5025 else
5026 let decr =
5027 if l.pageno = state.pagecount - coverB
5028 then 1
5029 else c
5031 let pageno = max 0 (l.pageno-decr) in
5032 gotoghyll (getpagey pageno)
5033 | Csplit (n, _) ->
5034 let y =
5035 if l.pagecol = 0
5036 then
5037 if l.pageno = 0
5038 then l.pagey
5039 else
5040 let pageno = max 0 (l.pageno-1) in
5041 let pagey, pageh = getpageyh pageno in
5042 pagey + (n-1)*pageh
5043 else
5044 let pagey, pageh = getpageyh l.pageno in
5045 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5047 gotoghyll y
5050 let viewkeyboard key mask =
5051 let enttext te =
5052 let mode = state.mode in
5053 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5054 state.text <- "";
5055 enttext ();
5056 G.postRedisplay "view:enttext"
5058 let ctrl = Wsi.withctrl mask in
5059 let key =
5060 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5062 match key with
5063 | 81 -> (* Q *)
5064 exit 0
5066 | 0xff63 -> (* insert *)
5067 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5068 then (
5069 state.mode <- LinkNav (Ltgendir 0);
5070 gotoy state.y;
5072 else showtext '!' "Keyboard link navigation does not work under rotation"
5074 | 0xff1b | 113 -> (* escape / q *)
5075 begin match state.mstate with
5076 | Mzoomrect _ ->
5077 state.mstate <- Mnone;
5078 Wsi.setcursor Wsi.CURSOR_INHERIT;
5079 G.postRedisplay "kill zoom rect";
5080 | _ ->
5081 begin match state.mode with
5082 | LinkNav _ ->
5083 state.mode <- View;
5084 G.postRedisplay "esc leave linknav"
5085 | _ ->
5086 match state.ranchors with
5087 | [] -> raise Quit
5088 | (path, password, anchor, origin) :: rest ->
5089 state.ranchors <- rest;
5090 state.anchor <- anchor;
5091 state.origin <- origin;
5092 opendoc path password
5093 end;
5094 end;
5096 | 0xff08 -> (* backspace *)
5097 gotoghyll (getnav ~-1)
5099 | 111 -> (* o *)
5100 enteroutlinemode ()
5102 | 117 -> (* u *)
5103 state.rects <- [];
5104 state.text <- "";
5105 G.postRedisplay "dehighlight";
5107 | 47 | 63 -> (* / ? *)
5108 let ondone isforw s =
5109 cbput state.hists.pat s;
5110 state.searchpattern <- s;
5111 search s isforw
5113 let s = String.create 1 in
5114 s.[0] <- Char.chr key;
5115 enttext (s, "", Some (onhist state.hists.pat),
5116 textentry, ondone (key = 47), true)
5118 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5119 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5120 setzoom (conf.zoom +. incr)
5122 | 43 | 0xffab -> (* + *)
5123 let ondone s =
5124 let n =
5125 try int_of_string s with exc ->
5126 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5127 max_int
5129 if n != max_int
5130 then (
5131 conf.pagebias <- n;
5132 state.text <- "page bias is now " ^ string_of_int n;
5135 enttext ("page bias: ", "", None, intentry, ondone, true)
5137 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5138 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5139 setzoom (max 0.01 (conf.zoom -. decr))
5141 | 45 | 0xffad -> (* - *)
5142 let ondone msg = state.text <- msg in
5143 enttext (
5144 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5145 optentry state.mode, ondone, true
5148 | 48 when ctrl -> (* ctrl-0 *)
5149 if conf.zoom = 1.0
5150 then (
5151 state.x <- 0;
5152 state.hscrollh <-
5153 if state.w <= state.winw - state.scrollw
5154 then 0
5155 else state.scrollw
5157 gotoy state.y
5159 else setzoom 1.0
5161 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5162 let cols =
5163 match conf.columns with
5164 | Csingle _ | Cmulti _ -> 1
5165 | Csplit (n, _) -> n
5167 let h = state.winh -
5168 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5170 let zoom = zoomforh state.winw h state.scrollw cols in
5171 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5172 then setzoom zoom
5174 | 51 when ctrl -> (* ctrl-3 *)
5175 let fm =
5176 match conf.fitmodel with
5177 | FitWidth -> FitProportional
5178 | FitProportional -> FitPage
5179 | FitPage -> FitWidth
5181 state.text <- "fit model: " ^ fitmodel_to_string fm;
5182 reqlayout conf.angle fm
5184 | 0xffc6 -> (* f9 *)
5185 togglebirdseye ()
5187 | 57 when ctrl -> (* ctrl-9 *)
5188 togglebirdseye ()
5190 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5191 when not ctrl -> (* 0..9 *)
5192 let ondone s =
5193 let n =
5194 try int_of_string s with exc ->
5195 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5198 if n >= 0
5199 then (
5200 addnav ();
5201 cbput state.hists.pag (string_of_int n);
5202 gotopage1 (n + conf.pagebias - 1) 0;
5205 let pageentry text key =
5206 match Char.unsafe_chr key with
5207 | 'g' -> TEdone text
5208 | _ -> intentry text key
5210 let text = "x" in text.[0] <- Char.chr key;
5211 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5213 | 98 -> (* b *)
5214 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5215 reshape state.winw state.winh;
5217 | 108 -> (* l *)
5218 conf.hlinks <- not conf.hlinks;
5219 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5220 G.postRedisplay "toggle highlightlinks";
5222 | 70 -> (* F *)
5223 state.glinks <- true;
5224 let mode = state.mode in
5225 state.mode <- Textentry (
5226 (":", "", None, linknentry, linkndone gotounder, false),
5227 (fun _ ->
5228 state.glinks <- false;
5229 state.mode <- mode)
5231 state.text <- "";
5232 G.postRedisplay "view:linkent(F)"
5234 | 121 -> (* y *)
5235 state.glinks <- true;
5236 let mode = state.mode in
5237 state.mode <- Textentry (
5239 ":", "", None, linknentry, linkndone (fun under ->
5240 selstring (undertext under);
5241 ), false
5243 fun _ ->
5244 state.glinks <- false;
5245 state.mode <- mode
5247 state.text <- "";
5248 G.postRedisplay "view:linkent"
5250 | 97 -> (* a *)
5251 begin match state.autoscroll with
5252 | Some step ->
5253 conf.autoscrollstep <- step;
5254 state.autoscroll <- None
5255 | None ->
5256 if conf.autoscrollstep = 0
5257 then state.autoscroll <- Some 1
5258 else state.autoscroll <- Some conf.autoscrollstep
5261 | 112 when ctrl -> (* ctrl-p *)
5262 launchpath ()
5264 | 80 -> (* P *)
5265 setpresentationmode (not conf.presentation);
5266 showtext ' ' ("presentation mode " ^
5267 if conf.presentation then "on" else "off");
5269 | 102 -> (* f *)
5270 if List.mem Wsi.Fullscreen state.winstate
5271 then Wsi.reshape conf.cwinw conf.cwinh
5272 else Wsi.fullscreen ()
5274 | 112 | 78 -> (* p|N *)
5275 search state.searchpattern false
5277 | 110 | 0xffc0 -> (* n|F3 *)
5278 search state.searchpattern true
5280 | 116 -> (* t *)
5281 begin match state.layout with
5282 | [] -> ()
5283 | l :: _ ->
5284 gotoghyll (getpagey l.pageno)
5287 | 32 -> (* space *)
5288 nextpage ()
5290 | 0xff9f | 0xffff -> (* delete *)
5291 prevpage ()
5293 | 61 -> (* = *)
5294 showtext ' ' (describe_location ());
5296 | 119 -> (* w *)
5297 begin match state.layout with
5298 | [] -> ()
5299 | l :: _ ->
5300 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5301 G.postRedisplay "w"
5304 | 39 -> (* ' *)
5305 enterbookmarkmode ()
5307 | 104 | 0xffbe -> (* h|F1 *)
5308 enterhelpmode ()
5310 | 105 -> (* i *)
5311 enterinfomode ()
5313 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5314 entermsgsmode ()
5316 | 109 -> (* m *)
5317 let ondone s =
5318 match state.layout with
5319 | l :: _ ->
5320 if String.length s > 0
5321 then
5322 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5323 | _ -> ()
5325 enttext ("bookmark: ", "", None, textentry, ondone, true)
5327 | 126 -> (* ~ *)
5328 quickbookmark ();
5329 showtext ' ' "Quick bookmark added";
5331 | 122 -> (* z *)
5332 begin match state.layout with
5333 | l :: _ ->
5334 let rect = getpdimrect l.pagedimno in
5335 let w, h =
5336 if conf.crophack
5337 then
5338 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5339 truncate (1.2 *. (rect.(3) -. rect.(0))))
5340 else
5341 (truncate (rect.(1) -. rect.(0)),
5342 truncate (rect.(3) -. rect.(0)))
5344 let w = truncate ((float w)*.conf.zoom)
5345 and h = truncate ((float h)*.conf.zoom) in
5346 if w != 0 && h != 0
5347 then (
5348 state.anchor <- getanchor ();
5349 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5351 G.postRedisplay "z";
5353 | [] -> ()
5356 | 60 | 62 -> (* < > *)
5357 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5359 | 91 | 93 -> (* [ ] *)
5360 conf.colorscale <-
5361 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5363 G.postRedisplay "brightness";
5365 | 99 when state.mode = View -> (* c *)
5366 let (c, a, b), z =
5367 match state.prevcolumns with
5368 | None -> (1, 0, 0), 1.0
5369 | Some (columns, z) ->
5370 let cab =
5371 match columns with
5372 | Csplit (c, _) -> -c, 0, 0
5373 | Cmulti ((c, a, b), _) -> c, a, b
5374 | Csingle _ -> 1, 0, 0
5376 cab, z
5378 setcolumns View c a b;
5379 setzoom z;
5381 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5382 setzoom state.prevzoom
5384 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5385 begin match state.autoscroll with
5386 | None ->
5387 begin match state.mode with
5388 | Birdseye beye -> upbirdseye 1 beye
5389 | _ ->
5390 if ctrl
5391 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5392 else (
5393 if not (Wsi.withshift mask) && conf.presentation
5394 then prevpage ()
5395 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5398 | Some n ->
5399 setautoscrollspeed n false
5402 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5403 begin match state.autoscroll with
5404 | None ->
5405 begin match state.mode with
5406 | Birdseye beye -> downbirdseye 1 beye
5407 | _ ->
5408 if ctrl
5409 then gotoy_and_clear_text (clamp (state.winh/2))
5410 else (
5411 if not (Wsi.withshift mask) && conf.presentation
5412 then nextpage ()
5413 else gotoy_and_clear_text (clamp conf.scrollstep)
5416 | Some n ->
5417 setautoscrollspeed n true
5420 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5421 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5422 if canpan ()
5423 then
5424 let dx =
5425 if ctrl
5426 then state.winw / 2
5427 else conf.hscrollstep
5429 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5430 state.x <- panbound (state.x + dx);
5431 gotoy_and_clear_text state.y
5432 else (
5433 state.text <- "";
5434 G.postRedisplay "left/right"
5437 | 0xff55 | 0xff9a -> (* (kp) prior *)
5438 let y =
5439 if ctrl
5440 then
5441 match state.layout with
5442 | [] -> state.y
5443 | l :: _ -> state.y - l.pagey
5444 else
5445 clamp (pgscale (-state.winh))
5447 gotoghyll y
5449 | 0xff56 | 0xff9b -> (* (kp) next *)
5450 let y =
5451 if ctrl
5452 then
5453 match List.rev state.layout with
5454 | [] -> state.y
5455 | l :: _ -> getpagey l.pageno
5456 else
5457 clamp (pgscale state.winh)
5459 gotoghyll y
5461 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5462 gotoghyll 0
5463 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5464 gotoghyll (clamp state.maxy)
5466 | 0xff53 | 0xff98
5467 when Wsi.withalt mask -> (* alt-(kp) right *)
5468 gotoghyll (getnav 1)
5469 | 0xff51 | 0xff96
5470 when Wsi.withalt mask -> (* alt-(kp) left *)
5471 gotoghyll (getnav ~-1)
5473 | 114 -> (* r *)
5474 reload ()
5476 | 118 when conf.debug -> (* v *)
5477 state.rects <- [];
5478 List.iter (fun l ->
5479 match getopaque l.pageno with
5480 | None -> ()
5481 | Some opaque ->
5482 let x0, y0, x1, y1 = pagebbox opaque in
5483 let a,b = float x0, float y0 in
5484 let c,d = float x1, float y0 in
5485 let e,f = float x1, float y1 in
5486 let h,j = float x0, float y1 in
5487 let rect = (a,b,c,d,e,f,h,j) in
5488 debugrect rect;
5489 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5490 ) state.layout;
5491 G.postRedisplay "v";
5493 | _ ->
5494 vlog "huh? %s" (Wsi.keyname key)
5497 let linknavkeyboard key mask linknav =
5498 let getpage pageno =
5499 let rec loop = function
5500 | [] -> None
5501 | l :: _ when l.pageno = pageno -> Some l
5502 | _ :: rest -> loop rest
5503 in loop state.layout
5505 let doexact (pageno, n) =
5506 match getopaque pageno, getpage pageno with
5507 | Some opaque, Some l ->
5508 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5509 then
5510 let under = getlink opaque n in
5511 G.postRedisplay "link gotounder";
5512 gotounder under;
5513 state.mode <- View;
5514 else
5515 let opt, dir =
5516 match key with
5517 | 0xff50 -> (* home *)
5518 Some (findlink opaque LDfirst), -1
5520 | 0xff57 -> (* end *)
5521 Some (findlink opaque LDlast), 1
5523 | 0xff51 -> (* left *)
5524 Some (findlink opaque (LDleft n)), -1
5526 | 0xff53 -> (* right *)
5527 Some (findlink opaque (LDright n)), 1
5529 | 0xff52 -> (* up *)
5530 Some (findlink opaque (LDup n)), -1
5532 | 0xff54 -> (* down *)
5533 Some (findlink opaque (LDdown n)), 1
5535 | _ -> None, 0
5537 let pwl l dir =
5538 begin match findpwl l.pageno dir with
5539 | Pwlnotfound -> ()
5540 | Pwl pageno ->
5541 let notfound dir =
5542 state.mode <- LinkNav (Ltgendir dir);
5543 let y, h = getpageyh pageno in
5544 let y =
5545 if dir < 0
5546 then y + h - state.winh
5547 else y
5549 gotoy y
5551 begin match getopaque pageno, getpage pageno with
5552 | Some opaque, Some _ ->
5553 let link =
5554 let ld = if dir > 0 then LDfirst else LDlast in
5555 findlink opaque ld
5557 begin match link with
5558 | Lfound m ->
5559 showlinktype (getlink opaque m);
5560 state.mode <- LinkNav (Ltexact (pageno, m));
5561 G.postRedisplay "linknav jpage";
5562 | _ -> notfound dir
5563 end;
5564 | _ -> notfound dir
5565 end;
5566 end;
5568 begin match opt with
5569 | Some Lnotfound -> pwl l dir;
5570 | Some (Lfound m) ->
5571 if m = n
5572 then pwl l dir
5573 else (
5574 let _, y0, _, y1 = getlinkrect opaque m in
5575 if y0 < l.pagey
5576 then gotopage1 l.pageno y0
5577 else (
5578 let d = fstate.fontsize + 1 in
5579 if y1 - l.pagey > l.pagevh - d
5580 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5581 else G.postRedisplay "linknav";
5583 showlinktype (getlink opaque m);
5584 state.mode <- LinkNav (Ltexact (l.pageno, m));
5587 | None -> viewkeyboard key mask
5588 end;
5589 | _ -> viewkeyboard key mask
5591 if key = 0xff63
5592 then (
5593 state.mode <- View;
5594 G.postRedisplay "leave linknav"
5596 else
5597 match linknav with
5598 | Ltgendir _ -> viewkeyboard key mask
5599 | Ltexact exact -> doexact exact
5602 let keyboard key mask =
5603 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5604 then wcmd "interrupt"
5605 else state.uioh <- state.uioh#key key mask
5608 let birdseyekeyboard key mask
5609 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5610 let incr =
5611 match conf.columns with
5612 | Csingle _ -> 1
5613 | Cmulti ((c, _, _), _) -> c
5614 | Csplit _ -> failwith "bird's eye split mode"
5616 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5617 match key with
5618 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5619 let y, h = getpageyh pageno in
5620 let top = (state.winh - h) / 2 in
5621 gotoy (max 0 (y - top))
5622 | 0xff0d (* enter *)
5623 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5624 | 0xff1b -> leavebirdseye beye true (* escape *)
5625 | 0xff52 -> upbirdseye incr beye (* up *)
5626 | 0xff54 -> downbirdseye incr beye (* down *)
5627 | 0xff51 -> upbirdseye 1 beye (* left *)
5628 | 0xff53 -> downbirdseye 1 beye (* right *)
5630 | 0xff55 -> (* prior *)
5631 begin match state.layout with
5632 | l :: _ ->
5633 if l.pagey != 0
5634 then (
5635 state.mode <- Birdseye (
5636 oconf, leftx, l.pageno, hooverpageno, anchor
5638 gotopage1 l.pageno 0;
5640 else (
5641 let layout = layout (state.y-state.winh) (pgh state.layout) in
5642 match layout with
5643 | [] -> gotoy (clamp (-state.winh))
5644 | l :: _ ->
5645 state.mode <- Birdseye (
5646 oconf, leftx, l.pageno, hooverpageno, anchor
5648 gotopage1 l.pageno 0
5651 | [] -> gotoy (clamp (-state.winh))
5652 end;
5654 | 0xff56 -> (* next *)
5655 begin match List.rev state.layout with
5656 | l :: _ ->
5657 let layout = layout (state.y + (pgh state.layout)) state.winh in
5658 begin match layout with
5659 | [] ->
5660 let incr = l.pageh - l.pagevh in
5661 if incr = 0
5662 then (
5663 state.mode <-
5664 Birdseye (
5665 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5667 G.postRedisplay "birdseye pagedown";
5669 else gotoy (clamp (incr + conf.interpagespace*2));
5671 | l :: _ ->
5672 state.mode <-
5673 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5674 gotopage1 l.pageno 0;
5677 | [] -> gotoy (clamp state.winh)
5678 end;
5680 | 0xff50 -> (* home *)
5681 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5682 gotopage1 0 0
5684 | 0xff57 -> (* end *)
5685 let pageno = state.pagecount - 1 in
5686 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5687 if not (pagevisible state.layout pageno)
5688 then
5689 let h =
5690 match List.rev state.pdims with
5691 | [] -> state.winh
5692 | (_, _, h, _) :: _ -> h
5694 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5695 else G.postRedisplay "birdseye end";
5696 | _ -> viewkeyboard key mask
5699 let drawpage l =
5700 let color =
5701 match state.mode with
5702 | Textentry _ -> scalecolor 0.4
5703 | LinkNav _
5704 | View -> scalecolor 1.0
5705 | Birdseye (_, _, pageno, hooverpageno, _) ->
5706 if l.pageno = hooverpageno
5707 then scalecolor 0.9
5708 else (
5709 if l.pageno = pageno
5710 then scalecolor 1.0
5711 else scalecolor 0.8
5714 drawtiles l color;
5717 let postdrawpage l linkindexbase =
5718 match getopaque l.pageno with
5719 | Some opaque ->
5720 if tileready l l.pagex l.pagey
5721 then
5722 let x = l.pagedispx - l.pagex
5723 and y = l.pagedispy - l.pagey in
5724 let hlmask =
5725 match conf.columns with
5726 | Csingle _ | Cmulti _ ->
5727 (if conf.hlinks then 1 else 0)
5728 + (if state.glinks
5729 && not (isbirdseye state.mode) then 2 else 0)
5730 | _ -> 0
5732 let s =
5733 match state.mode with
5734 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5735 | _ -> ""
5737 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5738 else 0
5739 | _ -> 0
5742 let scrollindicator () =
5743 let sbw, ph, sh = state.uioh#scrollph in
5744 let sbh, pw, sw = state.uioh#scrollpw in
5746 GlDraw.color (0.64, 0.64, 0.64);
5747 GlDraw.rect
5748 (float (state.winw - sbw), 0.)
5749 (float state.winw, float state.winh)
5751 GlDraw.rect
5752 (0., float (state.winh - sbh))
5753 (float (state.winw - state.scrollw - 1), float state.winh)
5755 GlDraw.color (0.0, 0.0, 0.0);
5757 GlDraw.rect
5758 (float (state.winw - sbw), ph)
5759 (float state.winw, ph +. sh)
5761 GlDraw.rect
5762 (pw, float (state.winh - sbh))
5763 (pw +. sw, float state.winh)
5767 let showsel () =
5768 match state.mstate with
5769 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5772 | Msel ((x0, y0), (x1, y1)) ->
5773 let rec loop = function
5774 | l :: ls ->
5775 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5776 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5777 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5778 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5779 then
5780 match getopaque l.pageno with
5781 | Some opaque ->
5782 let x0, y0 = pagetranslatepoint l x0 y0 in
5783 let x1, y1 = pagetranslatepoint l x1 y1 in
5784 seltext opaque (x0, y0, x1, y1);
5785 | _ -> ()
5786 else loop ls
5787 | [] -> ()
5789 loop state.layout
5792 let showrects rects =
5793 Gl.enable `blend;
5794 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5795 GlDraw.polygon_mode `both `fill;
5796 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5797 List.iter
5798 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5799 List.iter (fun l ->
5800 if l.pageno = pageno
5801 then (
5802 let dx = float (l.pagedispx - l.pagex) in
5803 let dy = float (l.pagedispy - l.pagey) in
5804 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5805 GlDraw.begins `quads;
5807 GlDraw.vertex2 (x0+.dx, y0+.dy);
5808 GlDraw.vertex2 (x1+.dx, y1+.dy);
5809 GlDraw.vertex2 (x2+.dx, y2+.dy);
5810 GlDraw.vertex2 (x3+.dx, y3+.dy);
5812 GlDraw.ends ();
5814 ) state.layout
5815 ) rects
5817 Gl.disable `blend;
5820 let display () =
5821 GlClear.color (scalecolor2 conf.bgcolor);
5822 GlClear.clear [`color];
5823 List.iter drawpage state.layout;
5824 let rects =
5825 match state.mode with
5826 | LinkNav (Ltexact (pageno, linkno)) ->
5827 begin match getopaque pageno with
5828 | Some opaque ->
5829 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5830 (pageno, 5, (
5831 float x0, float y0,
5832 float x1, float y0,
5833 float x1, float y1,
5834 float x0, float y1)
5835 ) :: state.rects
5836 | None -> state.rects
5838 | _ -> state.rects
5840 showrects rects;
5841 let rec postloop linkindexbase = function
5842 | l :: rest ->
5843 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5844 postloop linkindexbase rest
5845 | [] -> ()
5847 showsel ();
5848 postloop 0 state.layout;
5849 state.uioh#display;
5850 begin match state.mstate with
5851 | Mzoomrect ((x0, y0), (x1, y1)) ->
5852 Gl.enable `blend;
5853 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5854 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5855 GlDraw.rect (float x0, float y0)
5856 (float x1, float y1);
5857 Gl.disable `blend;
5858 | _ -> ()
5859 end;
5860 enttext ();
5861 scrollindicator ();
5862 Wsi.swapb ();
5865 let zoomrect x y x1 y1 =
5866 let x0 = min x x1
5867 and x1 = max x x1
5868 and y0 = min y y1 in
5869 gotoy (state.y + y0);
5870 state.anchor <- getanchor ();
5871 let zoom = (float state.w) /. float (x1 - x0) in
5872 let margin =
5873 match conf.fitmodel, conf.columns with
5874 | FitPage, Csplit _ ->
5875 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5877 | _, _ ->
5878 if state.w < state.winw - state.scrollw
5879 then (state.winw - state.scrollw - state.w) / 2
5880 else 0
5882 state.x <- (state.x + margin) - x0;
5883 setzoom zoom;
5884 Wsi.setcursor Wsi.CURSOR_INHERIT;
5885 state.mstate <- Mnone;
5888 let scrollx x =
5889 let winw = state.winw - state.scrollw - 1 in
5890 let s = float x /. float winw in
5891 let destx = truncate (float (state.w + winw) *. s) in
5892 state.x <- winw - destx;
5893 gotoy_and_clear_text state.y;
5894 state.mstate <- Mscrollx;
5897 let scrolly y =
5898 let s = float y /. float state.winh in
5899 let desty = truncate (float (state.maxy - state.winh) *. s) in
5900 gotoy_and_clear_text desty;
5901 state.mstate <- Mscrolly;
5904 let viewmouse button down x y mask =
5905 match button with
5906 | n when (n == 4 || n == 5) && not down ->
5907 if Wsi.withctrl mask
5908 then (
5909 match state.mstate with
5910 | Mzoom (oldn, i) ->
5911 if oldn = n
5912 then (
5913 if i = 2
5914 then
5915 let incr =
5916 match n with
5917 | 5 ->
5918 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5919 | _ ->
5920 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5922 let zoom = conf.zoom -. incr in
5923 setzoom zoom;
5924 state.mstate <- Mzoom (n, 0);
5925 else
5926 state.mstate <- Mzoom (n, i+1);
5928 else state.mstate <- Mzoom (n, 0)
5930 | _ -> state.mstate <- Mzoom (n, 0)
5932 else (
5933 match state.autoscroll with
5934 | Some step -> setautoscrollspeed step (n=4)
5935 | None ->
5936 if conf.wheelbypage || conf.presentation
5937 then (
5938 if n = 4
5939 then prevpage ()
5940 else nextpage ()
5942 else
5943 let incr =
5944 if n = 4
5945 then -conf.scrollstep
5946 else conf.scrollstep
5948 let incr = incr * 2 in
5949 let y = clamp incr in
5950 gotoy_and_clear_text y
5953 | n when (n = 6 || n = 7) && not down && canpan () ->
5954 state.x <-
5955 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5956 gotoy_and_clear_text state.y
5958 | 1 when Wsi.withshift mask ->
5959 state.mstate <- Mnone;
5960 if not down
5961 then (
5962 match unproject x y with
5963 | Some (pageno, ux, uy) ->
5964 let cmd = Printf.sprintf
5965 "%s %s %d %d %d"
5966 conf.stcmd state.path pageno ux uy
5968 popen cmd []
5969 | None -> ()
5972 | 1 when Wsi.withctrl mask ->
5973 if down
5974 then (
5975 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5976 state.mstate <- Mpan (x, y)
5978 else
5979 state.mstate <- Mnone
5981 | 3 ->
5982 if down
5983 then (
5984 Wsi.setcursor Wsi.CURSOR_CYCLE;
5985 let p = (x, y) in
5986 state.mstate <- Mzoomrect (p, p)
5988 else (
5989 match state.mstate with
5990 | Mzoomrect ((x0, y0), _) ->
5991 if abs (x-x0) > 10 && abs (y - y0) > 10
5992 then zoomrect x0 y0 x y
5993 else (
5994 state.mstate <- Mnone;
5995 Wsi.setcursor Wsi.CURSOR_INHERIT;
5996 G.postRedisplay "kill accidental zoom rect";
5998 | _ ->
5999 Wsi.setcursor Wsi.CURSOR_INHERIT;
6000 state.mstate <- Mnone
6003 | 1 when x > state.winw - state.scrollw ->
6004 if down
6005 then
6006 let _, position, sh = state.uioh#scrollph in
6007 if y > truncate position && y < truncate (position +. sh)
6008 then state.mstate <- Mscrolly
6009 else scrolly y
6010 else
6011 state.mstate <- Mnone
6013 | 1 when y > state.winh - state.hscrollh ->
6014 if down
6015 then
6016 let _, position, sw = state.uioh#scrollpw in
6017 if x > truncate position && x < truncate (position +. sw)
6018 then state.mstate <- Mscrollx
6019 else scrollx x
6020 else
6021 state.mstate <- Mnone
6023 | 1 ->
6024 let dest = if down then getunder x y else Unone in
6025 begin match dest with
6026 | Ulinkgoto _
6027 | Ulinkuri _
6028 | Uremote _
6029 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6030 gotounder dest
6032 | Unone when down ->
6033 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6034 state.mstate <- Mpan (x, y);
6036 | Unone | Utext _ ->
6037 if down
6038 then (
6039 if conf.angle mod 360 = 0
6040 then (
6041 state.mstate <- Msel ((x, y), (x, y));
6042 G.postRedisplay "mouse select";
6045 else (
6046 match state.mstate with
6047 | Mnone -> ()
6049 | Mzoom _ | Mscrollx | Mscrolly ->
6050 state.mstate <- Mnone
6052 | Mzoomrect ((x0, y0), _) ->
6053 zoomrect x0 y0 x y
6055 | Mpan _ ->
6056 Wsi.setcursor Wsi.CURSOR_INHERIT;
6057 state.mstate <- Mnone
6059 | Msel ((x0, y0), (x1, y1)) ->
6060 let rec loop = function
6061 | [] -> ()
6062 | l :: rest ->
6063 let inside =
6064 let a0 = l.pagedispy in
6065 let a1 = a0 + l.pagevh in
6066 let b0 = l.pagedispx in
6067 let b1 = b0 + l.pagevw in
6068 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6069 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6071 if inside
6072 then
6073 match getopaque l.pageno with
6074 | Some opaque ->
6075 begin
6076 match Ne.pipe () with
6077 | Ne.Exn exn ->
6078 showtext '!'
6079 (Printf.sprintf
6080 "can not create sel pipe: %s"
6081 (exntos exn));
6082 | Ne.Res (r, w) ->
6083 let doclose what fd =
6084 Ne.clo fd (fun msg ->
6085 dolog "%s close failed: %s" what msg)
6088 popen conf.selcmd [r, 0; w, -1];
6089 copysel w opaque;
6090 doclose "pipe/r" r;
6091 G.postRedisplay "copysel";
6092 with exn ->
6093 dolog "can not execute %S: %s"
6094 conf.selcmd (exntos exn);
6095 doclose "pipe/r" r;
6096 doclose "pipe/w" w;
6098 | None -> ()
6099 else loop rest
6101 loop state.layout;
6102 Wsi.setcursor Wsi.CURSOR_INHERIT;
6103 state.mstate <- Mnone;
6107 | _ -> ()
6110 let birdseyemouse button down x y mask
6111 (conf, leftx, _, hooverpageno, anchor) =
6112 match button with
6113 | 1 when down ->
6114 let rec loop = function
6115 | [] -> ()
6116 | l :: rest ->
6117 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6118 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6119 then (
6120 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6122 else loop rest
6124 loop state.layout
6125 | 3 -> ()
6126 | _ -> viewmouse button down x y mask
6129 let mouse button down x y mask =
6130 state.uioh <- state.uioh#button button down x y mask;
6133 let motion ~x ~y =
6134 state.uioh <- state.uioh#motion x y
6137 let pmotion ~x ~y =
6138 state.uioh <- state.uioh#pmotion x y;
6141 let uioh = object
6142 method display = ()
6144 method key key mask =
6145 begin match state.mode with
6146 | Textentry textentry -> textentrykeyboard key mask textentry
6147 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6148 | View -> viewkeyboard key mask
6149 | LinkNav linknav -> linknavkeyboard key mask linknav
6150 end;
6151 state.uioh
6153 method button button bstate x y mask =
6154 begin match state.mode with
6155 | LinkNav _
6156 | View -> viewmouse button bstate x y mask
6157 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6158 | Textentry _ -> ()
6159 end;
6160 state.uioh
6162 method motion x y =
6163 begin match state.mode with
6164 | Textentry _ -> ()
6165 | View | Birdseye _ | LinkNav _ ->
6166 match state.mstate with
6167 | Mzoom _ | Mnone -> ()
6169 | Mpan (x0, y0) ->
6170 let dx = x - x0
6171 and dy = y0 - y in
6172 state.mstate <- Mpan (x, y);
6173 if canpan ()
6174 then state.x <- panbound (state.x + dx);
6175 let y = clamp dy in
6176 gotoy_and_clear_text y
6178 | Msel (a, _) ->
6179 state.mstate <- Msel (a, (x, y));
6180 G.postRedisplay "motion select";
6182 | Mscrolly ->
6183 let y = min state.winh (max 0 y) in
6184 scrolly y
6186 | Mscrollx ->
6187 let x = min state.winw (max 0 x) in
6188 scrollx x
6190 | Mzoomrect (p0, _) ->
6191 state.mstate <- Mzoomrect (p0, (x, y));
6192 G.postRedisplay "motion zoomrect";
6193 end;
6194 state.uioh
6196 method pmotion x y =
6197 begin match state.mode with
6198 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6199 let rec loop = function
6200 | [] ->
6201 if hooverpageno != -1
6202 then (
6203 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6204 G.postRedisplay "pmotion birdseye no hoover";
6206 | l :: rest ->
6207 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6208 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6209 then (
6210 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6211 G.postRedisplay "pmotion birdseye hoover";
6213 else loop rest
6215 loop state.layout
6217 | Textentry _ -> ()
6219 | LinkNav _
6220 | View ->
6221 match state.mstate with
6222 | Mnone -> updateunder x y
6223 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6225 end;
6226 state.uioh
6228 method infochanged _ = ()
6230 method scrollph =
6231 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6232 let p, h =
6233 if maxy = 0
6234 then 0.0, float state.winh
6235 else scrollph state.y maxy
6237 state.scrollw, p, h
6239 method scrollpw =
6240 let winw = state.winw - state.scrollw in
6241 let fwinw = float winw in
6242 let sw =
6243 let sw = fwinw /. float state.w in
6244 let sw = fwinw *. sw in
6245 max sw (float conf.scrollh)
6247 let position =
6248 let maxx = state.w + winw in
6249 let x = winw - state.x in
6250 let percent = float x /. float maxx in
6251 (fwinw -. sw) *. percent
6253 state.hscrollh, position, sw
6255 method modehash =
6256 let modename =
6257 match state.mode with
6258 | LinkNav _ -> "links"
6259 | Textentry _ -> "textentry"
6260 | Birdseye _ -> "birdseye"
6261 | View -> "view"
6263 findkeyhash conf modename
6265 method eformsgs = true
6266 end;;
6268 module Config =
6269 struct
6270 open Parser
6272 let fontpath = ref "";;
6274 module KeyMap =
6275 Map.Make (struct type t = (int * int) let compare = compare end);;
6277 let unent s =
6278 let l = String.length s in
6279 let b = Buffer.create l in
6280 unent b s 0 l;
6281 Buffer.contents b;
6284 let home =
6285 try Sys.getenv "HOME"
6286 with exn ->
6287 prerr_endline
6288 ("Can not determine home directory location: " ^ exntos exn);
6292 let modifier_of_string = function
6293 | "alt" -> Wsi.altmask
6294 | "shift" -> Wsi.shiftmask
6295 | "ctrl" | "control" -> Wsi.ctrlmask
6296 | "meta" -> Wsi.metamask
6297 | _ -> 0
6300 let key_of_string =
6301 let r = Str.regexp "-" in
6302 fun s ->
6303 let elems = Str.full_split r s in
6304 let f n k m =
6305 let g s =
6306 let m1 = modifier_of_string s in
6307 if m1 = 0
6308 then (Wsi.namekey s, m)
6309 else (k, m lor m1)
6310 in function
6311 | Str.Delim s when n land 1 = 0 -> g s
6312 | Str.Text s -> g s
6313 | Str.Delim _ -> (k, m)
6315 let rec loop n k m = function
6316 | [] -> (k, m)
6317 | x :: xs ->
6318 let k, m = f n k m x in
6319 loop (n+1) k m xs
6321 loop 0 0 0 elems
6324 let keys_of_string =
6325 let r = Str.regexp "[ \t]" in
6326 fun s ->
6327 let elems = Str.split r s in
6328 List.map key_of_string elems
6331 let copykeyhashes c =
6332 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6335 let config_of c attrs =
6336 let apply c k v =
6338 match k with
6339 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6340 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6341 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6342 | "preload" -> { c with preload = bool_of_string v }
6343 | "page-bias" -> { c with pagebias = int_of_string v }
6344 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6345 | "horizontal-scroll-step" ->
6346 { c with hscrollstep = max (int_of_string v) 1 }
6347 | "auto-scroll-step" ->
6348 { c with autoscrollstep = max 0 (int_of_string v) }
6349 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6350 | "crop-hack" -> { c with crophack = bool_of_string v }
6351 | "throttle" ->
6352 let mw =
6353 match String.lowercase v with
6354 | "true" -> Some infinity
6355 | "false" -> None
6356 | f -> Some (float_of_string f)
6358 { c with maxwait = mw}
6359 | "highlight-links" -> { c with hlinks = bool_of_string v }
6360 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6361 | "vertical-margin" ->
6362 { c with interpagespace = max 0 (int_of_string v) }
6363 | "zoom" ->
6364 let zoom = float_of_string v /. 100. in
6365 let zoom = max zoom 0.0 in
6366 { c with zoom = zoom }
6367 | "presentation" -> { c with presentation = bool_of_string v }
6368 | "rotation-angle" -> { c with angle = int_of_string v }
6369 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6370 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6371 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6372 | "proportional-display" ->
6373 let fm =
6374 if bool_of_string v
6375 then FitProportional
6376 else FitWidth
6378 { c with fitmodel = fm }
6379 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6380 | "pixmap-cache-size" ->
6381 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6382 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6383 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6384 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6385 | "persistent-location" -> { c with jumpback = bool_of_string v }
6386 | "background-color" -> { c with bgcolor = color_of_string v }
6387 | "scrollbar-in-presentation" ->
6388 { c with scrollbarinpm = bool_of_string v }
6389 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6390 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6391 | "mupdf-store-size" ->
6392 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6393 | "checkers" -> { c with checkers = bool_of_string v }
6394 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6395 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6396 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6397 | "uri-launcher" -> { c with urilauncher = unent v }
6398 | "path-launcher" -> { c with pathlauncher = unent v }
6399 | "color-space" -> { c with colorspace = colorspace_of_string v }
6400 | "invert-colors" -> { c with invert = bool_of_string v }
6401 | "brightness" -> { c with colorscale = float_of_string v }
6402 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6403 | "ghyllscroll" ->
6404 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6405 | "columns" ->
6406 let (n, _, _) as nab = multicolumns_of_string v in
6407 if n < 0
6408 then { c with columns = Csplit (-n, [||]) }
6409 else { c with columns = Cmulti (nab, [||]) }
6410 | "birds-eye-columns" ->
6411 { c with beyecolumns = Some (max (int_of_string v) 2) }
6412 | "selection-command" -> { c with selcmd = unent v }
6413 | "synctex-command" -> { c with stcmd = unent v }
6414 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6415 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6416 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6417 | "use-pbo" -> { c with usepbo = bool_of_string v }
6418 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6419 | _ -> c
6420 with exn ->
6421 prerr_endline ("Error processing attribute (`" ^
6422 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6425 let rec fold c = function
6426 | [] -> c
6427 | (k, v) :: rest ->
6428 let c = apply c k v in
6429 fold c rest
6431 fold { c with keyhashes = copykeyhashes c } attrs;
6434 let fromstring f pos n v d =
6435 try f v
6436 with exn ->
6437 dolog "Error processing attribute (%S=%S) at %d\n%s"
6438 n v pos (exntos exn)
6443 let bookmark_of attrs =
6444 let rec fold title page rely visy = function
6445 | ("title", v) :: rest -> fold v page rely visy rest
6446 | ("page", v) :: rest -> fold title v rely visy rest
6447 | ("rely", v) :: rest -> fold title page v visy rest
6448 | ("visy", v) :: rest -> fold title page rely v rest
6449 | _ :: rest -> fold title page rely visy rest
6450 | [] -> title, page, rely, visy
6452 fold "invalid" "0" "0" "0" attrs
6455 let doc_of attrs =
6456 let rec fold path page rely pan visy = function
6457 | ("path", v) :: rest -> fold v page rely pan visy rest
6458 | ("page", v) :: rest -> fold path v rely pan visy rest
6459 | ("rely", v) :: rest -> fold path page v pan visy rest
6460 | ("pan", v) :: rest -> fold path page rely v visy rest
6461 | ("visy", v) :: rest -> fold path page rely pan v rest
6462 | _ :: rest -> fold path page rely pan visy rest
6463 | [] -> path, page, rely, pan, visy
6465 fold "" "0" "0" "0" "0" attrs
6468 let map_of attrs =
6469 let rec fold rs ls = function
6470 | ("out", v) :: rest -> fold v ls rest
6471 | ("in", v) :: rest -> fold rs v rest
6472 | _ :: rest -> fold ls rs rest
6473 | [] -> ls, rs
6475 fold "" "" attrs
6478 let setconf dst src =
6479 dst.scrollbw <- src.scrollbw;
6480 dst.scrollh <- src.scrollh;
6481 dst.icase <- src.icase;
6482 dst.preload <- src.preload;
6483 dst.pagebias <- src.pagebias;
6484 dst.verbose <- src.verbose;
6485 dst.scrollstep <- src.scrollstep;
6486 dst.maxhfit <- src.maxhfit;
6487 dst.crophack <- src.crophack;
6488 dst.autoscrollstep <- src.autoscrollstep;
6489 dst.maxwait <- src.maxwait;
6490 dst.hlinks <- src.hlinks;
6491 dst.underinfo <- src.underinfo;
6492 dst.interpagespace <- src.interpagespace;
6493 dst.zoom <- src.zoom;
6494 dst.presentation <- src.presentation;
6495 dst.angle <- src.angle;
6496 dst.cwinw <- src.cwinw;
6497 dst.cwinh <- src.cwinh;
6498 dst.savebmarks <- src.savebmarks;
6499 dst.memlimit <- src.memlimit;
6500 dst.fitmodel <- src.fitmodel;
6501 dst.texcount <- src.texcount;
6502 dst.sliceheight <- src.sliceheight;
6503 dst.thumbw <- src.thumbw;
6504 dst.jumpback <- src.jumpback;
6505 dst.bgcolor <- src.bgcolor;
6506 dst.scrollbarinpm <- src.scrollbarinpm;
6507 dst.tilew <- src.tilew;
6508 dst.tileh <- src.tileh;
6509 dst.mustoresize <- src.mustoresize;
6510 dst.checkers <- src.checkers;
6511 dst.aalevel <- src.aalevel;
6512 dst.trimmargins <- src.trimmargins;
6513 dst.trimfuzz <- src.trimfuzz;
6514 dst.urilauncher <- src.urilauncher;
6515 dst.colorspace <- src.colorspace;
6516 dst.invert <- src.invert;
6517 dst.colorscale <- src.colorscale;
6518 dst.redirectstderr <- src.redirectstderr;
6519 dst.ghyllscroll <- src.ghyllscroll;
6520 dst.columns <- src.columns;
6521 dst.beyecolumns <- src.beyecolumns;
6522 dst.selcmd <- src.selcmd;
6523 dst.updatecurs <- src.updatecurs;
6524 dst.pathlauncher <- src.pathlauncher;
6525 dst.keyhashes <- copykeyhashes src;
6526 dst.hfsize <- src.hfsize;
6527 dst.hscrollstep <- src.hscrollstep;
6528 dst.pgscale <- src.pgscale;
6529 dst.usepbo <- src.usepbo;
6530 dst.wheelbypage <- src.wheelbypage;
6531 dst.stcmd <- src.stcmd;
6534 let get s =
6535 let h = Hashtbl.create 10 in
6536 let dc = { defconf with angle = defconf.angle } in
6537 let rec toplevel v t spos _ =
6538 match t with
6539 | Vdata | Vcdata | Vend -> v
6540 | Vopen ("llppconfig", _, closed) ->
6541 if closed
6542 then v
6543 else { v with f = llppconfig }
6544 | Vopen _ ->
6545 error "unexpected subelement at top level" s spos
6546 | Vclose _ -> error "unexpected close at top level" s spos
6548 and llppconfig v t spos _ =
6549 match t with
6550 | Vdata | Vcdata -> v
6551 | Vend -> error "unexpected end of input in llppconfig" s spos
6552 | Vopen ("defaults", attrs, closed) ->
6553 let c = config_of dc attrs in
6554 setconf dc c;
6555 if closed
6556 then v
6557 else { v with f = defaults }
6559 | Vopen ("ui-font", attrs, closed) ->
6560 let rec getsize size = function
6561 | [] -> size
6562 | ("size", v) :: rest ->
6563 let size =
6564 fromstring int_of_string spos "size" v fstate.fontsize in
6565 getsize size rest
6566 | l -> getsize size l
6568 fstate.fontsize <- getsize fstate.fontsize attrs;
6569 if closed
6570 then v
6571 else { v with f = uifont (Buffer.create 10) }
6573 | Vopen ("doc", attrs, closed) ->
6574 let pathent, spage, srely, span, svisy = doc_of attrs in
6575 let path = unent pathent
6576 and pageno = fromstring int_of_string spos "page" spage 0
6577 and rely = fromstring float_of_string spos "rely" srely 0.0
6578 and pan = fromstring int_of_string spos "pan" span 0
6579 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6580 let c = config_of dc attrs in
6581 let anchor = (pageno, rely, visy) in
6582 if closed
6583 then (Hashtbl.add h path (c, [], pan, anchor); v)
6584 else { v with f = doc path pan anchor c [] }
6586 | Vopen _ ->
6587 error "unexpected subelement in llppconfig" s spos
6589 | Vclose "llppconfig" -> { v with f = toplevel }
6590 | Vclose _ -> error "unexpected close in llppconfig" s spos
6592 and defaults v t spos _ =
6593 match t with
6594 | Vdata | Vcdata -> v
6595 | Vend -> error "unexpected end of input in defaults" s spos
6596 | Vopen ("keymap", attrs, closed) ->
6597 let modename =
6598 try List.assoc "mode" attrs
6599 with Not_found -> "global" in
6600 if closed
6601 then v
6602 else
6603 let ret keymap =
6604 let h = findkeyhash dc modename in
6605 KeyMap.iter (Hashtbl.replace h) keymap;
6606 defaults
6608 { v with f = pkeymap ret KeyMap.empty }
6610 | Vopen (_, _, _) ->
6611 error "unexpected subelement in defaults" s spos
6613 | Vclose "defaults" ->
6614 { v with f = llppconfig }
6616 | Vclose _ -> error "unexpected close in defaults" s spos
6618 and uifont b v t spos epos =
6619 match t with
6620 | Vdata | Vcdata ->
6621 Buffer.add_substring b s spos (epos - spos);
6623 | Vopen (_, _, _) ->
6624 error "unexpected subelement in ui-font" s spos
6625 | Vclose "ui-font" ->
6626 if String.length !fontpath = 0
6627 then fontpath := Buffer.contents b;
6628 { v with f = llppconfig }
6629 | Vclose _ -> error "unexpected close in ui-font" s spos
6630 | Vend -> error "unexpected end of input in ui-font" s spos
6632 and doc path pan anchor c bookmarks v t spos _ =
6633 match t with
6634 | Vdata | Vcdata -> v
6635 | Vend -> error "unexpected end of input in doc" s spos
6636 | Vopen ("bookmarks", _, closed) ->
6637 if closed
6638 then v
6639 else { v with f = pbookmarks path pan anchor c bookmarks }
6641 | Vopen ("keymap", attrs, closed) ->
6642 let modename =
6643 try List.assoc "mode" attrs
6644 with Not_found -> "global"
6646 if closed
6647 then v
6648 else
6649 let ret keymap =
6650 let h = findkeyhash c modename in
6651 KeyMap.iter (Hashtbl.replace h) keymap;
6652 doc path pan anchor c bookmarks
6654 { v with f = pkeymap ret KeyMap.empty }
6656 | Vopen (_, _, _) ->
6657 error "unexpected subelement in doc" s spos
6659 | Vclose "doc" ->
6660 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6661 { v with f = llppconfig }
6663 | Vclose _ -> error "unexpected close in doc" s spos
6665 and pkeymap ret keymap v t spos _ =
6666 match t with
6667 | Vdata | Vcdata -> v
6668 | Vend -> error "unexpected end of input in keymap" s spos
6669 | Vopen ("map", attrs, closed) ->
6670 let r, l = map_of attrs in
6671 let kss = fromstring keys_of_string spos "in" r [] in
6672 let lss = fromstring keys_of_string spos "out" l [] in
6673 let keymap =
6674 match kss with
6675 | [] -> keymap
6676 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6677 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6679 if closed
6680 then { v with f = pkeymap ret keymap }
6681 else
6682 let f () = v in
6683 { v with f = skip "map" f }
6685 | Vopen _ ->
6686 error "unexpected subelement in keymap" s spos
6688 | Vclose "keymap" ->
6689 { v with f = ret keymap }
6691 | Vclose _ -> error "unexpected close in keymap" s spos
6693 and pbookmarks path pan anchor c bookmarks v t spos _ =
6694 match t with
6695 | Vdata | Vcdata -> v
6696 | Vend -> error "unexpected end of input in bookmarks" s spos
6697 | Vopen ("item", attrs, closed) ->
6698 let titleent, spage, srely, svisy = bookmark_of attrs in
6699 let page = fromstring int_of_string spos "page" spage 0
6700 and rely = fromstring float_of_string spos "rely" srely 0.0
6701 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6702 let bookmarks =
6703 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6705 if closed
6706 then { v with f = pbookmarks path pan anchor c bookmarks }
6707 else
6708 let f () = v in
6709 { v with f = skip "item" f }
6711 | Vopen _ ->
6712 error "unexpected subelement in bookmarks" s spos
6714 | Vclose "bookmarks" ->
6715 { v with f = doc path pan anchor c bookmarks }
6717 | Vclose _ -> error "unexpected close in bookmarks" s spos
6719 and skip tag f v t spos _ =
6720 match t with
6721 | Vdata | Vcdata -> v
6722 | Vend ->
6723 error ("unexpected end of input in skipped " ^ tag) s spos
6724 | Vopen (tag', _, closed) ->
6725 if closed
6726 then v
6727 else
6728 let f' () = { v with f = skip tag f } in
6729 { v with f = skip tag' f' }
6730 | Vclose ctag ->
6731 if tag = ctag
6732 then f ()
6733 else error ("unexpected close in skipped " ^ tag) s spos
6736 parse { f = toplevel; accu = () } s;
6737 h, dc;
6740 let do_load f ic =
6742 let len = in_channel_length ic in
6743 let s = String.create len in
6744 really_input ic s 0 len;
6745 f s;
6746 with
6747 | Parse_error (msg, s, pos) ->
6748 let subs = subs s pos in
6749 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6750 failwith ("parse error: " ^ s)
6752 | exn ->
6753 failwith ("config load error: " ^ exntos exn)
6756 let defconfpath =
6757 let dir =
6759 let dir = Filename.concat home ".config" in
6760 if Sys.is_directory dir then dir else home
6761 with _ -> home
6763 Filename.concat dir "llpp.conf"
6766 let confpath = ref defconfpath;;
6768 let load1 f =
6769 if Sys.file_exists !confpath
6770 then
6771 match
6772 (try Some (open_in_bin !confpath)
6773 with exn ->
6774 prerr_endline
6775 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6776 exntos exn);
6777 None
6779 with
6780 | Some ic ->
6781 let success =
6783 f (do_load get ic)
6784 with exn ->
6785 prerr_endline
6786 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6787 exntos exn);
6788 false
6790 close_in ic;
6791 success
6793 | None -> false
6794 else
6795 f (Hashtbl.create 0, defconf)
6798 let load () =
6799 let f (h, dc) =
6800 let pc, pb, px, pa =
6802 let key =
6803 if String.length state.origin = 0
6804 then state.path
6805 else state.origin
6807 Hashtbl.find h (Filename.basename key)
6808 with Not_found -> dc, [], 0, emptyanchor
6810 setconf defconf dc;
6811 setconf conf pc;
6812 state.bookmarks <- pb;
6813 state.x <- px;
6814 state.scrollw <- conf.scrollbw;
6815 if conf.jumpback
6816 then state.anchor <- pa;
6817 cbput state.hists.nav pa;
6818 true
6820 load1 f
6823 let add_attrs bb always dc c =
6824 let ob s a b =
6825 if always || a != b
6826 then Printf.bprintf bb "\n %s='%b'" s a
6827 and oi s a b =
6828 if always || a != b
6829 then Printf.bprintf bb "\n %s='%d'" s a
6830 and oI s a b =
6831 if always || a != b
6832 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6833 and oz s a b =
6834 if always || a <> b
6835 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6836 and oF s a b =
6837 if always || a <> b
6838 then Printf.bprintf bb "\n %s='%f'" s a
6839 and oc s a b =
6840 if always || a <> b
6841 then
6842 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6843 and oC s a b =
6844 if always || a <> b
6845 then
6846 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6847 and oR s a b =
6848 if always || a <> b
6849 then
6850 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6851 and os s a b =
6852 if always || a <> b
6853 then
6854 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6855 and og s a b =
6856 if always || a <> b
6857 then
6858 match a with
6859 | None -> ()
6860 | Some (_N, _A, _B) ->
6861 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6862 and oW s a b =
6863 if always || a <> b
6864 then
6865 let v =
6866 match a with
6867 | None -> "false"
6868 | Some f ->
6869 if f = infinity
6870 then "true"
6871 else string_of_float f
6873 Printf.bprintf bb "\n %s='%s'" s v
6874 and oco s a b =
6875 if always || a <> b
6876 then
6877 match a with
6878 | Cmulti ((n, a, b), _) when n > 1 ->
6879 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6880 | Csplit (n, _) when n > 1 ->
6881 Printf.bprintf bb "\n %s='%d'" s ~-n
6882 | _ -> ()
6883 and obeco s a b =
6884 if always || a <> b
6885 then
6886 match a with
6887 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6888 | _ -> ()
6889 and oFm s a b =
6890 if always || a <> b
6891 then
6892 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6894 oi "width" c.cwinw dc.cwinw;
6895 oi "height" c.cwinh dc.cwinh;
6896 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6897 oi "scroll-handle-height" c.scrollh dc.scrollh;
6898 ob "case-insensitive-search" c.icase dc.icase;
6899 ob "preload" c.preload dc.preload;
6900 oi "page-bias" c.pagebias dc.pagebias;
6901 oi "scroll-step" c.scrollstep dc.scrollstep;
6902 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6903 ob "max-height-fit" c.maxhfit dc.maxhfit;
6904 ob "crop-hack" c.crophack dc.crophack;
6905 oW "throttle" c.maxwait dc.maxwait;
6906 ob "highlight-links" c.hlinks dc.hlinks;
6907 ob "under-cursor-info" c.underinfo dc.underinfo;
6908 oi "vertical-margin" c.interpagespace dc.interpagespace;
6909 oz "zoom" c.zoom dc.zoom;
6910 ob "presentation" c.presentation dc.presentation;
6911 oi "rotation-angle" c.angle dc.angle;
6912 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6913 oFm "fit-model" c.fitmodel dc.fitmodel;
6914 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6915 oi "tex-count" c.texcount dc.texcount;
6916 oi "slice-height" c.sliceheight dc.sliceheight;
6917 oi "thumbnail-width" c.thumbw dc.thumbw;
6918 ob "persistent-location" c.jumpback dc.jumpback;
6919 oc "background-color" c.bgcolor dc.bgcolor;
6920 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6921 oi "tile-width" c.tilew dc.tilew;
6922 oi "tile-height" c.tileh dc.tileh;
6923 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6924 ob "checkers" c.checkers dc.checkers;
6925 oi "aalevel" c.aalevel dc.aalevel;
6926 ob "trim-margins" c.trimmargins dc.trimmargins;
6927 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6928 os "uri-launcher" c.urilauncher dc.urilauncher;
6929 os "path-launcher" c.pathlauncher dc.pathlauncher;
6930 oC "color-space" c.colorspace dc.colorspace;
6931 ob "invert-colors" c.invert dc.invert;
6932 oF "brightness" c.colorscale dc.colorscale;
6933 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6934 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6935 oco "columns" c.columns dc.columns;
6936 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6937 os "selection-command" c.selcmd dc.selcmd;
6938 os "synctex-command" c.stcmd dc.stcmd;
6939 ob "update-cursor" c.updatecurs dc.updatecurs;
6940 oi "hint-font-size" c.hfsize dc.hfsize;
6941 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6942 oF "page-scroll-scale" c.pgscale dc.pgscale;
6943 ob "use-pbo" c.usepbo dc.usepbo;
6944 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6947 let keymapsbuf always dc c =
6948 let bb = Buffer.create 16 in
6949 let rec loop = function
6950 | [] -> ()
6951 | (modename, h) :: rest ->
6952 let dh = findkeyhash dc modename in
6953 if always || h <> dh
6954 then (
6955 if Hashtbl.length h > 0
6956 then (
6957 if Buffer.length bb > 0
6958 then Buffer.add_char bb '\n';
6959 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6960 Hashtbl.iter (fun i o ->
6961 let isdifferent = always ||
6963 let dO = Hashtbl.find dh i in
6964 dO <> o
6965 with Not_found -> true
6967 if isdifferent
6968 then
6969 let addkm (k, m) =
6970 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6971 if Wsi.withalt m then Buffer.add_string bb "alt-";
6972 if Wsi.withshift m then Buffer.add_string bb "shift-";
6973 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6974 Buffer.add_string bb (Wsi.keyname k);
6976 let addkms l =
6977 let rec loop = function
6978 | [] -> ()
6979 | km :: [] -> addkm km
6980 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6982 loop l
6984 Buffer.add_string bb "<map in='";
6985 addkm i;
6986 match o with
6987 | KMinsrt km ->
6988 Buffer.add_string bb "' out='";
6989 addkm km;
6990 Buffer.add_string bb "'/>\n"
6992 | KMinsrl kms ->
6993 Buffer.add_string bb "' out='";
6994 addkms kms;
6995 Buffer.add_string bb "'/>\n"
6997 | KMmulti (ins, kms) ->
6998 Buffer.add_char bb ' ';
6999 addkms ins;
7000 Buffer.add_string bb "' out='";
7001 addkms kms;
7002 Buffer.add_string bb "'/>\n"
7003 ) h;
7004 Buffer.add_string bb "</keymap>";
7007 loop rest
7009 loop c.keyhashes;
7013 let save () =
7014 let uifontsize = fstate.fontsize in
7015 let bb = Buffer.create 32768 in
7016 let w, h =
7017 List.fold_left
7018 (fun (w, h) ws ->
7019 match ws with
7020 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
7021 | Wsi.MaxVert -> (w, conf.cwinh)
7022 | Wsi.MaxHorz -> (conf.cwinw, h)
7024 (state.winw, state.winh) state.winstate
7026 conf.cwinw <- w;
7027 conf.cwinh <- h;
7028 let f (h, dc) =
7029 let dc = if conf.bedefault then conf else dc in
7030 Buffer.add_string bb "<llppconfig>\n";
7032 if String.length !fontpath > 0
7033 then
7034 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7035 uifontsize
7036 !fontpath
7037 else (
7038 if uifontsize <> 14
7039 then
7040 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7043 Buffer.add_string bb "<defaults ";
7044 add_attrs bb true dc dc;
7045 let kb = keymapsbuf true dc dc in
7046 if Buffer.length kb > 0
7047 then (
7048 Buffer.add_string bb ">\n";
7049 Buffer.add_buffer bb kb;
7050 Buffer.add_string bb "\n</defaults>\n";
7052 else Buffer.add_string bb "/>\n";
7054 let adddoc path pan anchor c bookmarks =
7055 if bookmarks == [] && c = dc && anchor = emptyanchor
7056 then ()
7057 else (
7058 Printf.bprintf bb "<doc path='%s'"
7059 (enent path 0 (String.length path));
7061 if anchor <> emptyanchor
7062 then (
7063 let n, rely, visy = anchor in
7064 Printf.bprintf bb " page='%d'" n;
7065 if rely > 1e-6
7066 then
7067 Printf.bprintf bb " rely='%f'" rely
7069 if abs_float visy > 1e-6
7070 then
7071 Printf.bprintf bb " visy='%f'" visy
7075 if pan != 0
7076 then Printf.bprintf bb " pan='%d'" pan;
7078 add_attrs bb false dc c;
7079 let kb = keymapsbuf false dc c in
7081 begin match bookmarks with
7082 | [] ->
7083 if Buffer.length kb > 0
7084 then (
7085 Buffer.add_string bb ">\n";
7086 Buffer.add_buffer bb kb;
7087 Buffer.add_string bb "\n</doc>\n";
7089 else Buffer.add_string bb "/>\n"
7090 | _ ->
7091 Buffer.add_string bb ">\n<bookmarks>\n";
7092 List.iter (fun (title, _level, (page, rely, visy)) ->
7093 Printf.bprintf bb
7094 "<item title='%s' page='%d'"
7095 (enent title 0 (String.length title))
7096 page
7098 if rely > 1e-6
7099 then
7100 Printf.bprintf bb " rely='%f'" rely
7102 if abs_float visy > 1e-6
7103 then
7104 Printf.bprintf bb " visy='%f'" visy
7106 Buffer.add_string bb "/>\n";
7107 ) bookmarks;
7108 Buffer.add_string bb "</bookmarks>";
7109 if Buffer.length kb > 0
7110 then (
7111 Buffer.add_string bb "\n";
7112 Buffer.add_buffer bb kb;
7114 Buffer.add_string bb "\n</doc>\n";
7115 end;
7119 let pan, conf =
7120 match state.mode with
7121 | Birdseye (c, pan, _, _, _) ->
7122 let beyecolumns =
7123 match conf.columns with
7124 | Cmulti ((c, _, _), _) -> Some c
7125 | Csingle _ -> None
7126 | Csplit _ -> None
7127 and columns =
7128 match c.columns with
7129 | Cmulti (c, _) -> Cmulti (c, [||])
7130 | Csingle _ -> Csingle [||]
7131 | Csplit _ -> failwith "quit from bird's eye while split"
7133 pan, { c with beyecolumns = beyecolumns; columns = columns }
7134 | _ -> state.x, conf
7136 let basename = Filename.basename
7137 (if String.length state.origin = 0 then state.path else state.origin)
7139 adddoc basename pan (getanchor ())
7140 (let conf =
7141 let autoscrollstep =
7142 match state.autoscroll with
7143 | Some step -> step
7144 | None -> conf.autoscrollstep
7146 match state.mode with
7147 | Birdseye (bc, _, _, _, _) ->
7148 { conf with
7149 zoom = bc.zoom;
7150 presentation = bc.presentation;
7151 interpagespace = bc.interpagespace;
7152 maxwait = bc.maxwait;
7153 autoscrollstep = autoscrollstep }
7154 | _ -> { conf with autoscrollstep = autoscrollstep }
7155 in conf)
7156 (if conf.savebmarks then state.bookmarks else []);
7158 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7159 if basename <> path
7160 then adddoc path x anchor c bookmarks
7161 ) h;
7162 Buffer.add_string bb "</llppconfig>\n";
7163 true;
7165 if load1 f && Buffer.length bb > 0
7166 then
7168 let tmp = !confpath ^ ".tmp" in
7169 let oc = open_out_bin tmp in
7170 Buffer.output_buffer oc bb;
7171 close_out oc;
7172 Unix.rename tmp !confpath;
7173 with exn ->
7174 prerr_endline
7175 ("error while saving configuration: " ^ exntos exn)
7177 end;;
7179 let adderrmsg src msg =
7180 Buffer.add_string state.errmsgs msg;
7181 state.newerrmsgs <- true;
7182 G.postRedisplay src
7185 let adderrfmt src fmt =
7186 Format.kprintf (fun s -> adderrmsg src s) fmt;
7189 let ract cmds =
7190 let cl = splitatspace cmds in
7191 let scan s fmt f =
7192 try Scanf.sscanf s fmt f
7193 with exn ->
7194 adderrfmt "remote exec"
7195 "error processing '%S': %s\n" cmds (exntos exn)
7197 match cl with
7198 | "reload" :: [] -> reload ()
7199 | "goto" :: args :: [] ->
7200 scan args "%u %f %f"
7201 (fun pageno x y ->
7202 let cmd, _ = state.geomcmds in
7203 if String.length cmd = 0
7204 then gotopagexy pageno x y
7205 else
7206 let f prevf () =
7207 gotopagexy pageno x y;
7208 prevf ()
7210 state.reprf <- f state.reprf
7212 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7213 | "rect" :: args :: [] ->
7214 scan args "%u %u %f %f %f %f"
7215 (fun pageno color x0 y0 x1 y1 ->
7216 onpagerect pageno (fun w h ->
7217 let _,w1,h1,_ = getpagedim pageno in
7218 let sw = float w1 /. w
7219 and sh = float h1 /. h in
7220 let x0s = x0 *. sw
7221 and x1s = x1 *. sw
7222 and y0s = y0 *. sh
7223 and y1s = y1 *. sh in
7224 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7225 debugrect rect;
7226 state.rects <- (pageno, color, rect) :: state.rects;
7227 G.postRedisplay "rect";
7230 | "activatewin" :: [] -> Wsi.activatewin ()
7231 | "quit" :: [] -> raise Quit
7232 | _ ->
7233 adderrfmt "remote command"
7234 "error processing remote command: %S\n" cmds;
7237 let remote =
7238 let scratch = String.create 80 in
7239 let buf = Buffer.create 80 in
7240 fun fd ->
7241 let rec tempfr () =
7242 try Some (Unix.read fd scratch 0 80)
7243 with
7244 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7245 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7246 | exn -> raise exn
7248 match tempfr () with
7249 | None -> Some fd
7250 | Some n ->
7251 if n = 0
7252 then (
7253 Unix.close fd;
7254 if Buffer.length buf > 0
7255 then (
7256 let s = Buffer.contents buf in
7257 Buffer.clear buf;
7258 ract s;
7260 None
7262 else
7263 let rec eat ppos =
7264 let nlpos =
7266 let pos = String.index_from scratch ppos '\n' in
7267 if pos >= n then -1 else pos
7268 with Not_found -> -1
7270 if nlpos >= 0
7271 then (
7272 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7273 let s = Buffer.contents buf in
7274 Buffer.clear buf;
7275 ract s;
7276 eat (nlpos+1);
7278 else (
7279 Buffer.add_substring buf scratch ppos (n-ppos);
7280 Some fd
7282 in eat 0
7285 let remoteopen path =
7286 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7287 with exn ->
7288 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7289 None
7292 let () =
7293 let trimcachepath = ref "" in
7294 let rcmdpath = ref "" in
7295 Arg.parse
7296 (Arg.align
7297 [("-p", Arg.String (fun s -> state.password <- s),
7298 "<password> Set password");
7300 ("-f", Arg.String (fun s -> Config.fontpath := s),
7301 "<path> Set path to the user interface font");
7303 ("-c", Arg.String (fun s -> Config.confpath := s),
7304 "<path> Set path to the configuration file");
7306 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7307 "<path> Set path to the trim cache file");
7309 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7310 "<named-destination> Set named destination");
7312 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7314 ("-remote", Arg.String (fun s -> rcmdpath := s),
7315 "<path> Set path to the remote commands source");
7317 ("-origin", Arg.String (fun s -> state.origin <- s),
7318 "<original path> Set original path");
7320 ("-v", Arg.Unit (fun () ->
7321 Printf.printf
7322 "%s\nconfiguration path: %s\n"
7323 (version ())
7324 Config.defconfpath
7326 exit 0), " Print version and exit");
7329 (fun s -> state.path <- s)
7330 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7332 if String.length state.path = 0
7333 then (prerr_endline "file name missing"; exit 1);
7335 if not (Config.load ())
7336 then prerr_endline "failed to load configuration";
7338 let globalkeyhash = findkeyhash conf "global" in
7339 let wsfd, winw, winh = Wsi.init (object
7340 method expose =
7341 if nogeomcmds state.geomcmds || platform == Posx
7342 then display ()
7343 else (
7344 GlClear.color (scalecolor2 conf.bgcolor);
7345 GlClear.clear [`color];
7347 method display = display ()
7348 method reshape w h = reshape w h
7349 method mouse b d x y m = mouse b d x y m
7350 method motion x y = state.mpos <- (x, y); motion x y
7351 method pmotion x y = state.mpos <- (x, y); pmotion x y
7352 method key k m =
7353 let mascm = m land (
7354 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7355 ) in
7356 match state.keystate with
7357 | KSnone ->
7358 let km = k, mascm in
7359 begin
7360 match
7361 let modehash = state.uioh#modehash in
7362 try Hashtbl.find modehash km
7363 with Not_found ->
7364 try Hashtbl.find globalkeyhash km
7365 with Not_found -> KMinsrt (k, m)
7366 with
7367 | KMinsrt (k, m) -> keyboard k m
7368 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7369 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7371 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7372 List.iter (fun (k, m) -> keyboard k m) insrt;
7373 state.keystate <- KSnone
7374 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7375 state.keystate <- KSinto (keys, insrt)
7376 | _ ->
7377 state.keystate <- KSnone
7379 method enter x y = state.mpos <- (x, y); pmotion x y
7380 method leave = state.mpos <- (-1, -1)
7381 method winstate wsl = state.winstate <- wsl
7382 method quit = raise Quit
7383 end) conf.cwinw conf.cwinh (platform = Posx) in
7385 state.wsfd <- wsfd;
7387 if not (
7388 List.exists GlMisc.check_extension
7389 [ "GL_ARB_texture_rectangle"
7390 ; "GL_EXT_texture_recangle"
7391 ; "GL_NV_texture_rectangle" ]
7393 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7395 let cr, sw =
7396 match Ne.pipe () with
7397 | Ne.Exn exn ->
7398 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7399 exit 1
7400 | Ne.Res rw -> rw
7401 and sr, cw =
7402 match Ne.pipe () with
7403 | Ne.Exn exn ->
7404 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7405 exit 1
7406 | Ne.Res rw -> rw
7409 cloexec cr;
7410 cloexec sw;
7411 cloexec sr;
7412 cloexec cw;
7414 setcheckers conf.checkers;
7415 redirectstderr ();
7417 init (cr, cw) (
7418 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7419 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7420 !Config.fontpath, !trimcachepath,
7421 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7423 state.sr <- sr;
7424 state.sw <- sw;
7425 state.text <- "Opening " ^ (mbtoutf8 state.path);
7426 reshape winw winh;
7427 opendoc state.path state.password;
7428 state.uioh <- uioh;
7430 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7431 let optrfd =
7432 ref (
7433 if String.length !rcmdpath > 0
7434 then remoteopen !rcmdpath
7435 else None
7439 let rec loop deadline =
7440 let r =
7441 match state.errfd with
7442 | None -> [state.sr; state.wsfd]
7443 | Some fd -> [state.sr; state.wsfd; fd]
7445 let r =
7446 match !optrfd with
7447 | None -> r
7448 | Some fd -> fd :: r
7450 if state.redisplay
7451 then (
7452 state.redisplay <- false;
7453 display ();
7455 let timeout =
7456 let now = now () in
7457 if deadline > now
7458 then (
7459 if deadline = infinity
7460 then ~-.1.0
7461 else max 0.0 (deadline -. now)
7463 else 0.0
7465 let r, _, _ =
7466 try Unix.select r [] [] timeout
7467 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7469 begin match r with
7470 | [] ->
7471 state.ghyll None;
7472 let newdeadline =
7473 if state.ghyll == noghyll
7474 then
7475 match state.autoscroll with
7476 | Some step when step != 0 ->
7477 let y = state.y + step in
7478 let y =
7479 if y < 0
7480 then state.maxy
7481 else if y >= state.maxy then 0 else y
7483 gotoy y;
7484 if state.mode = View
7485 then state.text <- "";
7486 deadline +. 0.01
7487 | _ -> infinity
7488 else deadline +. 0.01
7490 loop newdeadline
7492 | l ->
7493 let rec checkfds = function
7494 | [] -> ()
7495 | fd :: rest when fd = state.sr ->
7496 let cmd = readcmd state.sr in
7497 act cmd;
7498 checkfds rest
7500 | fd :: rest when fd = state.wsfd ->
7501 Wsi.readresp fd;
7502 checkfds rest
7504 | fd :: rest when Some fd = !optrfd ->
7505 begin match remote fd with
7506 | None -> optrfd := remoteopen !rcmdpath;
7507 | opt -> optrfd := opt
7508 end;
7509 checkfds rest
7511 | fd :: rest ->
7512 let s = String.create 80 in
7513 let n = tempfailureretry (Unix.read fd s 0) 80 in
7514 if conf.redirectstderr
7515 then (
7516 Buffer.add_substring state.errmsgs s 0 n;
7517 state.newerrmsgs <- true;
7518 state.redisplay <- true;
7520 else (
7521 prerr_string (String.sub s 0 n);
7522 flush stderr;
7524 checkfds rest
7526 checkfds l;
7527 let newdeadline =
7528 let deadline1 =
7529 if deadline = infinity
7530 then now () +. 0.01
7531 else deadline
7533 match state.autoscroll with
7534 | Some step when step != 0 -> deadline1
7535 | _ -> if state.ghyll == noghyll then infinity else deadline1
7537 loop newdeadline
7538 end;
7541 loop infinity;
7542 with Quit ->
7543 Config.save ();