Improve horizontal tracking
[llpp.git] / main.ml
blob581bf877a84e067a7e5f72931ecb014b9df9cccb
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 * proportional * 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 proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
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 getmaxw : unit -> float = "ml_getmaxw";;
96 external postprocess :
97 opaque -> int -> int -> int -> (int * string * int) -> int
98 = "ml_postprocess";;
99 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
100 external platform : unit -> platform = "ml_platform";;
101 external setaalevel : int -> unit = "ml_setaalevel";;
102 external realloctexts : int -> bool = "ml_realloctexts";;
103 external findlink : opaque -> linkdir -> link = "ml_findlink";;
104 external getlink : opaque -> int -> under = "ml_getlink";;
105 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
106 external getlinkcount : opaque -> int = "ml_getlinkcount";;
107 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
108 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
109 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
110 external freepbo : string -> unit = "ml_freepbo";;
111 external unmappbo : string -> unit = "ml_unmappbo";;
112 external pbousable : unit -> bool = "ml_pbo_usable";;
113 external unproject : opaque -> int -> int -> (int * int) option
114 = "ml_unproject";;
115 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
117 let platform_to_string = function
118 | Punknown -> "unknown"
119 | Plinux -> "Linux"
120 | Posx -> "OSX"
121 | Psun -> "Sun"
122 | Pfreebsd -> "FreeBSD"
123 | Pdragonflybsd -> "DragonflyBSD"
124 | Popenbsd -> "OpenBSD"
125 | Pnetbsd -> "NetBSD"
126 | Pcygwin -> "Cygwin"
129 let platform = platform ();;
131 let now = Unix.gettimeofday;;
133 let popen cmd fda =
134 if platform = Pcygwin
135 then (
136 let sh = "/bin/sh" in
137 let args = [|sh; "-c"; cmd|] in
138 let rec std si so se = function
139 | [] -> si, so, se
140 | (fd, 0) :: rest -> std fd so se rest
141 | (fd, -1) :: rest ->
142 Unix.set_close_on_exec fd;
143 std si so se rest
144 | (_, n) :: _ ->
145 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
147 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
148 ignore (Unix.create_process sh args si so se)
150 else popen cmd fda;
153 type mpos = int * int
154 and mstate =
155 | Msel of (mpos * mpos)
156 | Mpan of mpos
157 | Mscrolly | Mscrollx
158 | Mzoom of (int * int)
159 | Mzoomrect of (mpos * mpos)
160 | Mnone
163 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
164 and onkey = string -> int -> te
165 and ondone = string -> unit
166 and histcancel = unit -> unit
167 and onhist = ((histcmd -> string) * histcancel)
168 and histcmd = HCnext | HCprev | HCfirst | HClast
169 and cancelonempty = bool
170 and te =
171 | TEstop
172 | TEdone of string
173 | TEcont of string
174 | TEswitch of textentry
177 type 'a circbuf =
178 { store : 'a array
179 ; mutable rc : int
180 ; mutable wc : int
181 ; mutable len : int
185 let bound v minv maxv =
186 max minv (min maxv v);
189 let cbnew n v =
190 { store = Array.create n v
191 ; rc = 0
192 ; wc = 0
193 ; len = 0
197 let cbcap b = Array.length b.store;;
199 let cbput b v =
200 let cap = cbcap b in
201 b.store.(b.wc) <- v;
202 b.wc <- (b.wc + 1) mod cap;
203 b.rc <- b.wc;
204 b.len <- min (b.len + 1) cap;
207 let cbempty b = b.len = 0;;
209 let cbgetg b circular dir =
210 if cbempty b
211 then b.store.(0)
212 else
213 let rc = b.rc + dir in
214 let rc =
215 if circular
216 then (
217 if rc = -1
218 then b.len-1
219 else (
220 if rc >= b.len
221 then 0
222 else rc
225 else bound rc 0 (b.len-1)
227 b.rc <- rc;
228 b.store.(rc);
231 let cbget b = cbgetg b false;;
232 let cbgetc b = cbgetg b true;;
234 let drawstring size x y s =
235 Gl.enable `blend;
236 Gl.enable `texture_2d;
237 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
238 ignore (drawstr size x y s);
239 Gl.disable `blend;
240 Gl.disable `texture_2d;
243 let drawstring1 size x y s =
244 drawstr size x y s;
247 let drawstring2 size x y fmt =
248 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
251 type page =
252 { pageno : int
253 ; pagedimno : int
254 ; pagew : int
255 ; pageh : int
256 ; pagex : int
257 ; pagey : int
258 ; pagevw : int
259 ; pagevh : int
260 ; pagedispx : int
261 ; pagedispy : int
262 ; pagecol : int
266 let debugl l =
267 dolog "l %d dim=%d {" l.pageno l.pagedimno;
268 dolog " WxH %dx%d" l.pagew l.pageh;
269 dolog " vWxH %dx%d" l.pagevw l.pagevh;
270 dolog " pagex,y %d,%d" l.pagex l.pagey;
271 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
272 dolog " column %d" l.pagecol;
273 dolog "}";
276 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
277 dolog "rect {";
278 dolog " x0,y0=(% f, % f)" x0 y0;
279 dolog " x1,y1=(% f, % f)" x1 y1;
280 dolog " x2,y2=(% f, % f)" x2 y2;
281 dolog " x3,y3=(% f, % f)" x3 y3;
282 dolog "}";
285 type multicolumns = multicol * pagegeom
286 and singlecolumn = pagegeom
287 and splitcolumns = columncount * pagegeom
288 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
289 and multicol = columncount * covercount * covercount
290 and pdimno = int
291 and columncount = int
292 and covercount = int;;
294 type conf =
295 { mutable scrollbw : int
296 ; mutable scrollh : int
297 ; mutable icase : bool
298 ; mutable preload : bool
299 ; mutable pagebias : int
300 ; mutable verbose : bool
301 ; mutable debug : bool
302 ; mutable scrollstep : int
303 ; mutable hscrollstep : int
304 ; mutable maxhfit : bool
305 ; mutable crophack : bool
306 ; mutable autoscrollstep : int
307 ; mutable maxwait : float option
308 ; mutable hlinks : bool
309 ; mutable underinfo : bool
310 ; mutable interpagespace : interpagespace
311 ; mutable zoom : float
312 ; mutable presentation : bool
313 ; mutable angle : angle
314 ; mutable cwinw : int
315 ; mutable cwinh : int
316 ; mutable savebmarks : bool
317 ; mutable proportional : proportional
318 ; mutable trimmargins : trimmargins
319 ; mutable trimfuzz : irect
320 ; mutable memlimit : memsize
321 ; mutable texcount : texcount
322 ; mutable sliceheight : sliceheight
323 ; mutable thumbw : width
324 ; mutable jumpback : bool
325 ; mutable bgcolor : float * float * float
326 ; mutable bedefault : bool
327 ; mutable scrollbarinpm : bool
328 ; mutable tilew : int
329 ; mutable tileh : int
330 ; mutable mustoresize : memsize
331 ; mutable checkers : bool
332 ; mutable aalevel : int
333 ; mutable urilauncher : string
334 ; mutable pathlauncher : string
335 ; mutable colorspace : colorspace
336 ; mutable invert : bool
337 ; mutable colorscale : float
338 ; mutable redirectstderr : bool
339 ; mutable ghyllscroll : (int * int * int) option
340 ; mutable columns : columns
341 ; mutable beyecolumns : columncount option
342 ; mutable selcmd : string
343 ; mutable updatecurs : bool
344 ; mutable keyhashes : (string * keyhash) list
345 ; mutable hfsize : int
346 ; mutable pgscale : float
347 ; mutable usepbo : bool
348 ; mutable wheelbypage : bool
349 ; mutable stcmd : string
351 and columns =
352 | Csingle of singlecolumn
353 | Cmulti of multicolumns
354 | Csplit of splitcolumns
357 type anchor = pageno * top * dtop;;
359 type outline = string * int * anchor;;
361 type rect = float * float * float * float * float * float * float * float;;
363 type tile = opaque * pixmapsize * elapsed
364 and elapsed = float;;
365 type pagemapkey = pageno * gen;;
366 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
367 and row = int
368 and col = int;;
370 let emptyanchor = (0, 0.0, 0.0);;
372 type infochange = | Memused | Docinfo | Pdim;;
374 class type uioh = object
375 method display : unit
376 method key : int -> int -> uioh
377 method button : int -> bool -> int -> int -> int -> uioh
378 method motion : int -> int -> uioh
379 method pmotion : int -> int -> uioh
380 method infochanged : infochange -> unit
381 method scrollpw : (int * float * float)
382 method scrollph : (int * float * float)
383 method modehash : keyhash
384 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 end;;
427 type state =
428 { mutable sr : Unix.file_descr
429 ; mutable sw : Unix.file_descr
430 ; mutable wsfd : Unix.file_descr
431 ; mutable errfd : Unix.file_descr option
432 ; mutable stderr : Unix.file_descr
433 ; mutable errmsgs : Buffer.t
434 ; mutable newerrmsgs : bool
435 ; mutable w : int
436 ; mutable x : int
437 ; mutable y : int
438 ; mutable scrollw : int
439 ; mutable hscrollh : int
440 ; mutable anchor : anchor
441 ; mutable ranchors : (string * string * anchor) list
442 ; mutable maxy : int
443 ; mutable layout : page list
444 ; pagemap : (pagemapkey, opaque) Hashtbl.t
445 ; tilemap : (tilemapkey, tile) Hashtbl.t
446 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
447 ; mutable pdims : (pageno * width * height * leftx) list
448 ; mutable pagecount : int
449 ; mutable currently : currently
450 ; mutable mstate : mstate
451 ; mutable searchpattern : string
452 ; mutable rects : (pageno * recttype * rect) list
453 ; mutable rects1 : (pageno * recttype * rect) list
454 ; mutable text : string
455 ; mutable winstate : Wsi.winstate list
456 ; mutable mode : mode
457 ; mutable uioh : uioh
458 ; mutable outlines : outline array
459 ; mutable bookmarks : outline list
460 ; mutable path : string
461 ; mutable password : string
462 ; mutable nameddest : string
463 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
464 ; mutable memused : memsize
465 ; mutable gen : gen
466 ; mutable throttle : (page list * int * float) option
467 ; mutable autoscroll : int option
468 ; mutable ghyll : (int option -> unit)
469 ; mutable help : helpitem array
470 ; mutable docinfo : (int * string) list
471 ; mutable texid : GlTex.texture_id option
472 ; hists : hists
473 ; mutable prevzoom : float
474 ; mutable progress : float
475 ; mutable redisplay : bool
476 ; mutable mpos : mpos
477 ; mutable keystate : keystate
478 ; mutable glinks : bool
479 ; mutable prevcolumns : (columns * float) option
480 ; mutable wthack : bool
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
485 and hists =
486 { pat : string circbuf
487 ; pag : string circbuf
488 ; nav : anchor circbuf
489 ; sel : string circbuf
493 let defconf =
494 { scrollbw = 7
495 ; scrollh = 12
496 ; icase = true
497 ; preload = true
498 ; pagebias = 0
499 ; verbose = false
500 ; debug = false
501 ; scrollstep = 24
502 ; hscrollstep = 24
503 ; maxhfit = true
504 ; crophack = false
505 ; autoscrollstep = 2
506 ; maxwait = None
507 ; hlinks = false
508 ; underinfo = false
509 ; interpagespace = 2
510 ; zoom = 1.0
511 ; presentation = false
512 ; angle = 0
513 ; cwinw = 900
514 ; cwinh = 900
515 ; savebmarks = true
516 ; proportional = true
517 ; trimmargins = false
518 ; trimfuzz = (0,0,0,0)
519 ; memlimit = 32 lsl 20
520 ; texcount = 256
521 ; sliceheight = 24
522 ; thumbw = 76
523 ; jumpback = true
524 ; bgcolor = (0.5, 0.5, 0.5)
525 ; bedefault = false
526 ; scrollbarinpm = true
527 ; tilew = 2048
528 ; tileh = 2048
529 ; mustoresize = 256 lsl 20
530 ; checkers = true
531 ; aalevel = 8
532 ; urilauncher =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
536 | Posx -> "open \"%s\""
537 | Pcygwin -> "cygstart \"%s\""
538 | Punknown -> "echo %s")
539 ; pathlauncher = "lp \"%s\""
540 ; selcmd =
541 (match platform with
542 | Plinux | Pfreebsd | Pdragonflybsd
543 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
544 | Posx -> "pbcopy"
545 | Pcygwin -> "wsel"
546 | Punknown -> "cat")
547 ; colorspace = Rgb
548 ; invert = false
549 ; colorscale = 1.0
550 ; redirectstderr = false
551 ; ghyllscroll = None
552 ; columns = Csingle [||]
553 ; beyecolumns = None
554 ; updatecurs = false
555 ; hfsize = 12
556 ; pgscale = 1.0
557 ; usepbo = false
558 ; wheelbypage = false
559 ; stcmd = "echo SyncTex"
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let wtmode = ref false;;
577 let findkeyhash c name =
578 try List.assoc name c.keyhashes
579 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
582 let conf = { defconf with angle = defconf.angle };;
584 let pgscale h = truncate (float h *. conf.pgscale);;
586 type fontstate =
587 { mutable fontsize : int
588 ; mutable wwidth : float
589 ; mutable maxrows : int
593 let fstate =
594 { fontsize = 14
595 ; wwidth = nan
596 ; maxrows = -1
600 let geturl s =
601 let colonpos = try String.index s ':' with Not_found -> -1 in
602 let len = String.length s in
603 if colonpos >= 0 && colonpos + 3 < len
604 then (
605 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
606 then
607 let schemestartpos =
608 try String.rindex_from s colonpos ' '
609 with Not_found -> -1
611 let scheme =
612 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
614 match scheme with
615 | "http" | "ftp" | "mailto" ->
616 let epos =
617 try String.index_from s colonpos ' '
618 with Not_found -> len
620 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
621 | _ -> ""
622 else ""
624 else ""
627 let gotouri uri =
628 if String.length conf.urilauncher = 0
629 then print_endline uri
630 else (
631 let url = geturl uri in
632 if String.length url = 0
633 then print_endline uri
634 else
635 let re = Str.regexp "%s" in
636 let command = Str.global_replace re url conf.urilauncher in
637 try popen command []
638 with exn ->
639 Printf.eprintf
640 "failed to execute `%s': %s\n" command (exntos exn);
641 flush stderr;
645 let version () =
646 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
647 (platform_to_string platform) Sys.word_size Sys.ocaml_version
650 let makehelp () =
651 let strings = version () :: "" :: Help.keys in
652 Array.of_list (
653 List.map (fun s ->
654 let url = geturl s in
655 if String.length url > 0
656 then (s, 0, Action (fun u -> gotouri url; u))
657 else (s, 0, Noaction)
658 ) strings);
661 let noghyll _ = ();;
662 let firstgeomcmds = "", [];;
663 let noreprf () = ();;
665 let state =
666 { sr = Unix.stdin
667 ; sw = Unix.stdin
668 ; wsfd = Unix.stdin
669 ; errfd = None
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; scrollw = 0
677 ; hscrollh = 0
678 ; anchor = emptyanchor
679 ; ranchors = []
680 ; layout = []
681 ; maxy = max_int
682 ; tilelru = Queue.create ()
683 ; pagemap = Hashtbl.create 10
684 ; tilemap = Hashtbl.create 10
685 ; pdims = []
686 ; pagecount = 0
687 ; currently = Idle
688 ; mstate = Mnone
689 ; rects = []
690 ; rects1 = []
691 ; text = ""
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = ""
695 ; outlines = [||]
696 ; bookmarks = []
697 ; path = ""
698 ; password = ""
699 ; nameddest = ""
700 ; geomcmds = firstgeomcmds
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 ""
704 ; pag = cbnew 10 ""
705 ; sel = cbnew 10 ""
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = makehelp ()
713 ; docinfo = []
714 ; texid = None
715 ; prevzoom = 1.0
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; wthack = false
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
730 let setfontsize n =
731 fstate.fontsize <- n;
732 fstate.wwidth <- measurestr fstate.fontsize "w";
733 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
736 let vlog fmt =
737 if conf.verbose
738 then
739 Printf.kprintf prerr_endline fmt
740 else
741 Printf.kprintf ignore fmt
744 let launchpath () =
745 if String.length conf.pathlauncher = 0
746 then print_endline state.path
747 else (
748 let re = Str.regexp "%s" in
749 let command = Str.global_replace re state.path conf.pathlauncher in
750 try popen command []
751 with exn ->
752 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
753 flush stderr;
757 module Ne = struct
758 type 'a t = | Res of 'a | Exn of exn;;
760 let pipe () =
761 try Res (Unix.pipe ())
762 with exn -> Exn exn
765 let clo fd f =
766 try tempfailureretry Unix.close fd
767 with exn -> f (exntos exn)
770 let dup fd =
771 try Res (tempfailureretry Unix.dup fd)
772 with exn -> Exn exn
775 let dup2 fd1 fd2 =
776 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
777 with exn -> Exn exn
779 end;;
781 let redirectstderr () =
782 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
783 if conf.redirectstderr
784 then
785 match Ne.pipe () with
786 | Ne.Exn exn ->
787 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
789 | Ne.Res (r, w) ->
790 begin match Ne.dup Unix.stderr with
791 | Ne.Exn exn ->
792 dolog "failed to dup stderr: %s" (exntos exn);
793 Ne.clo r (clofail "pipe/r");
794 Ne.clo w (clofail "pipe/w");
796 | Ne.Res dupstderr ->
797 begin match Ne.dup2 w Unix.stderr with
798 | Ne.Exn exn ->
799 dolog "failed to dup2 to stderr: %s" (exntos exn);
800 Ne.clo dupstderr (clofail "stderr duplicate");
801 Ne.clo r (clofail "redir pipe/r");
802 Ne.clo w (clofail "redir pipe/w");
804 | Ne.Res () ->
805 state.stderr <- dupstderr;
806 state.errfd <- Some r;
807 end;
809 else (
810 state.newerrmsgs <- false;
811 begin match state.errfd with
812 | Some fd ->
813 begin match Ne.dup2 state.stderr Unix.stderr with
814 | Ne.Exn exn ->
815 dolog "failed to dup2 original stderr: %s" (exntos exn)
816 | Ne.Res () ->
817 Ne.clo fd (clofail "dup of stderr");
818 state.errfd <- None;
819 end;
820 | None -> ()
821 end;
822 prerr_string (Buffer.contents state.errmsgs);
823 flush stderr;
824 Buffer.clear state.errmsgs;
828 module G =
829 struct
830 let postRedisplay who =
831 if conf.verbose
832 then prerr_endline ("redisplay for " ^ who);
833 state.redisplay <- true;
835 end;;
837 let getopaque pageno =
838 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
839 with Not_found -> None
842 let putopaque pageno opaque =
843 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
846 let pagetranslatepoint l x y =
847 let dy = y - l.pagedispy in
848 let y = dy + l.pagey in
849 let dx = x - l.pagedispx in
850 let x = dx + l.pagex in
851 (x, y);
854 let onppundermouse g x y d =
855 let rec f = function
856 | l :: rest ->
857 begin match getopaque l.pageno with
858 | Some opaque ->
859 let x0 = l.pagedispx in
860 let x1 = x0 + l.pagevw in
861 let y0 = l.pagedispy in
862 let y1 = y0 + l.pagevh in
863 if y >= y0 && y <= y1 && x >= x0 && x <= x1
864 then
865 let px, py = pagetranslatepoint l x y in
866 match g opaque l px py with
867 | Some res -> res
868 | None -> f rest
869 else f rest
870 | _ ->
871 f rest
873 | [] -> d
875 f state.layout
878 let getunder x y =
879 let g opaque _ px py =
880 match whatsunder opaque px py with
881 | Unone -> None
882 | under -> Some under
884 onppundermouse g x y Unone
887 let unproject x y =
888 let g opaque l x y =
889 match unproject opaque x y with
890 | Some (x, y) -> Some (Some (l.pageno, x, y))
891 | None -> None
893 onppundermouse g x y None;
896 let showtext c s =
897 state.text <- Printf.sprintf "%c%s" c s;
898 G.postRedisplay "showtext";
901 let undertext = function
902 | Unone -> "none"
903 | Ulinkuri s -> s
904 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
905 | Utext s -> "font: " ^ s
906 | Uunexpected s -> "unexpected: " ^ s
907 | Ulaunch s -> "launch: " ^ s
908 | Unamed s -> "named: " ^ s
909 | Uremote (filename, pageno) ->
910 Printf.sprintf "%s: page %d" filename (pageno+1)
913 let updateunder x y =
914 match getunder x y with
915 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
916 | Ulinkuri uri ->
917 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
918 Wsi.setcursor Wsi.CURSOR_INFO
919 | Ulinkgoto (pageno, _) ->
920 if conf.underinfo
921 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
922 Wsi.setcursor Wsi.CURSOR_INFO
923 | Utext s ->
924 if conf.underinfo then showtext 'f' ("ont: " ^ s);
925 Wsi.setcursor Wsi.CURSOR_TEXT
926 | Uunexpected s ->
927 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
928 Wsi.setcursor Wsi.CURSOR_INHERIT
929 | Ulaunch s ->
930 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
931 Wsi.setcursor Wsi.CURSOR_INHERIT
932 | Unamed s ->
933 if conf.underinfo then showtext 'n' ("amed: " ^ s);
934 Wsi.setcursor Wsi.CURSOR_INHERIT
935 | Uremote (filename, pageno) ->
936 if conf.underinfo then showtext 'r'
937 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
938 Wsi.setcursor Wsi.CURSOR_INFO
941 let showlinktype under =
942 if conf.underinfo
943 then
944 match under with
945 | Unone -> ()
946 | under ->
947 let s = undertext under in
948 showtext ' ' s
951 let addchar s c =
952 let b = Buffer.create (String.length s + 1) in
953 Buffer.add_string b s;
954 Buffer.add_char b c;
955 Buffer.contents b;
958 let colorspace_of_string s =
959 match String.lowercase s with
960 | "rgb" -> Rgb
961 | "bgr" -> Bgr
962 | "gray" -> Gray
963 | _ -> failwith "invalid colorspace"
966 let int_of_colorspace = function
967 | Rgb -> 0
968 | Bgr -> 1
969 | Gray -> 2
972 let colorspace_of_int = function
973 | 0 -> Rgb
974 | 1 -> Bgr
975 | 2 -> Gray
976 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
979 let colorspace_to_string = function
980 | Rgb -> "rgb"
981 | Bgr -> "bgr"
982 | Gray -> "gray"
985 let intentry_with_suffix text key =
986 let c =
987 if key >= 32 && key < 127
988 then Char.chr key
989 else '\000'
991 match Char.lowercase c with
992 | '0' .. '9' ->
993 let text = addchar text c in
994 TEcont text
996 | 'k' | 'm' | 'g' ->
997 let text = addchar text c in
998 TEcont text
1000 | _ ->
1001 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1002 TEcont text
1005 let multicolumns_to_string (n, a, b) =
1006 if a = 0 && b = 0
1007 then Printf.sprintf "%d" n
1008 else Printf.sprintf "%d,%d,%d" n a b;
1011 let multicolumns_of_string s =
1013 (int_of_string s, 0, 0)
1014 with _ ->
1015 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1016 if a > 1 || b > 1
1017 then failwith "subtly broken"; (n, a, b)
1021 let readcmd fd =
1022 let s = "xxxx" in
1023 let n = tempfailureretry (Unix.read fd s 0) 4 in
1024 if n != 4 then failwith "incomplete read(len)";
1025 let len = 0
1026 lor (Char.code s.[0] lsl 24)
1027 lor (Char.code s.[1] lsl 16)
1028 lor (Char.code s.[2] lsl 8)
1029 lor (Char.code s.[3] lsl 0)
1031 let s = String.create len in
1032 let n = tempfailureretry (Unix.read fd s 0) len in
1033 if n != len then failwith "incomplete read(data)";
1037 let btod b = if b then 1 else 0;;
1039 let wcmd fmt =
1040 let b = Buffer.create 16 in
1041 Buffer.add_string b "llll";
1042 Printf.kbprintf
1043 (fun b ->
1044 let s = Buffer.contents b in
1045 let n = String.length s in
1046 let len = n - 4 in
1047 (* dolog "wcmd %S" (String.sub s 4 len); *)
1048 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1049 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1050 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1051 s.[3] <- Char.chr (len land 0xff);
1052 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1053 if n' != n then failwith "write failed";
1054 ) b fmt;
1057 let calcips h =
1058 let d = state.winh - h in
1059 max conf.interpagespace ((d + 1) / 2)
1062 let rowyh (c, coverA, coverB) b n =
1063 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1064 then
1065 let _, _, vy, (_, _, h, _) = b.(n) in
1066 (vy, h)
1067 else
1068 let n' = n - coverA in
1069 let d = n' mod c in
1070 let s = n - d in
1071 let e = min state.pagecount (s + c) in
1072 let rec find m miny maxh = if m = e then miny, maxh else
1073 let _, _, y, (_, _, h, _) = b.(m) in
1074 let miny = min miny y in
1075 let maxh = max maxh h in
1076 find (m+1) miny maxh
1077 in find s max_int 0
1080 let calcheight () =
1081 match conf.columns with
1082 | Cmulti ((_, _, _) as cl, b) ->
1083 if Array.length b > 0
1084 then
1085 let y, h = rowyh cl b (Array.length b - 1) in
1086 y + h + (if conf.presentation then calcips h else 0)
1087 else 0
1088 | Csingle b ->
1089 if Array.length b > 0
1090 then
1091 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1092 y + h + (if conf.presentation then calcips h else 0)
1093 else 0
1094 | Csplit (_, b) ->
1095 if Array.length b > 0
1096 then
1097 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1098 y + h
1099 else 0
1102 let getpageyh pageno =
1103 let pageno = bound pageno 0 (state.pagecount-1) in
1104 match conf.columns with
1105 | Csingle b ->
1106 if Array.length b = 0
1107 then 0, 0
1108 else
1109 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1110 let y =
1111 if conf.presentation
1112 then y - calcips h
1113 else y
1115 y, h
1116 | Cmulti (cl, b) ->
1117 if Array.length b = 0
1118 then 0, 0
1119 else
1120 let y, h = rowyh cl b pageno in
1121 let y =
1122 if conf.presentation
1123 then y - calcips h
1124 else y
1126 y, h
1127 | Csplit (c, b) ->
1128 if Array.length b = 0
1129 then 0, 0
1130 else
1131 let n = pageno*c in
1132 let (_, _, y, (_, _, h, _)) = b.(n) in
1133 y, h
1136 let getpagedim pageno =
1137 let rec f ppdim l =
1138 match l with
1139 | (n, _, _, _) as pdim :: rest ->
1140 if n >= pageno
1141 then (if n = pageno then pdim else ppdim)
1142 else f pdim rest
1144 | [] -> ppdim
1146 f (-1, -1, -1, -1) state.pdims
1149 let getpagey pageno = fst (getpageyh pageno);;
1151 let nogeomcmds cmds =
1152 match cmds with
1153 | s, [] -> String.length s = 0
1154 | _ -> false
1157 let page_of_y y =
1158 let ((c, coverA, coverB) as cl), b =
1159 match conf.columns with
1160 | Csingle b -> (1, 0, 0), b
1161 | Cmulti (c, b) -> c, b
1162 | Csplit (_, b) -> (1, 0, 0), b
1164 if Array.length b = 0
1165 then -1
1166 else
1167 let rec bsearch nmin nmax =
1168 if nmin > nmax
1169 then bound nmin 0 (state.pagecount-1)
1170 else
1171 let n = (nmax + nmin) / 2 in
1172 let vy, h = rowyh cl b n in
1173 let y0, y1 =
1174 if conf.presentation
1175 then
1176 let ips = calcips h in
1177 let y0 = vy - ips in
1178 let y1 = vy + h + ips in
1179 y0, y1
1180 else (
1181 if n = 0
1182 then 0, vy + h + conf.interpagespace
1183 else
1184 let y0 = vy - conf.interpagespace in
1185 y0, y0 + h + conf.interpagespace
1188 if y >= y0 && y < y1
1189 then (
1190 if c = 1
1191 then n
1192 else (
1193 if n > coverA
1194 then
1195 if n < state.pagecount - coverB
1196 then ((n-coverA)/c)*c + coverA
1197 else n
1198 else n
1201 else (
1202 if y > y0
1203 then bsearch (n+1) nmax
1204 else bsearch nmin (n-1)
1207 let r = bsearch 0 (state.pagecount-1) in
1211 let layoutN ((columns, coverA, coverB), b) y sh =
1212 let sh = sh - state.hscrollh in
1213 let rec fold accu n =
1214 if n = Array.length b
1215 then accu
1216 else
1217 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1218 if (vy - y) > sh &&
1219 (n = coverA - 1
1220 || n = state.pagecount - coverB
1221 || (n - coverA) mod columns = columns - 1)
1222 then accu
1223 else
1224 let accu =
1225 if vy + h > y
1226 then
1227 let pagey = max 0 (y - vy) in
1228 let pagedispy = if pagey > 0 then 0 else vy - y in
1229 let pagedispx, pagex =
1230 let pdx =
1231 if n = coverA - 1 || n = state.pagecount - coverB
1232 then state.x + (state.winw - state.scrollw - w) / 2
1233 else dx + xoff + state.x
1235 if pdx < 0
1236 then 0, -pdx
1237 else pdx, 0
1239 let pagevw =
1240 let vw = state.winw - state.scrollw - pagedispx in
1241 let pw = w - pagex in
1242 min vw pw
1244 let pagevh = min (h - pagey) (sh - pagedispy) in
1245 if pagevw > 0 && pagevh > 0
1246 then
1247 let e =
1248 { pageno = n
1249 ; pagedimno = pdimno
1250 ; pagew = w
1251 ; pageh = h
1252 ; pagex = pagex
1253 ; pagey = pagey
1254 ; pagevw = pagevw
1255 ; pagevh = pagevh
1256 ; pagedispx = pagedispx
1257 ; pagedispy = pagedispy
1258 ; pagecol = 0
1261 e :: accu
1262 else
1263 accu
1264 else
1265 accu
1267 fold accu (n+1)
1269 List.rev (fold [] (page_of_y y));
1272 let layoutS (columns, b) y sh =
1273 let sh = sh - state.hscrollh in
1274 let rec fold accu n =
1275 if n = Array.length b
1276 then accu
1277 else
1278 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1279 if (vy - y) > sh
1280 then accu
1281 else
1282 let accu =
1283 if vy + pageh > y
1284 then
1285 let x = xoff + state.x in
1286 let pagey = max 0 (y - vy) in
1287 let pagedispy = if pagey > 0 then 0 else vy - y in
1288 let pagedispx, pagex =
1289 if px = 0
1290 then (
1291 if x < 0
1292 then 0, -x
1293 else x, 0
1295 else (
1296 let px = px - x in
1297 if px < 0
1298 then -px, 0
1299 else 0, px
1302 let pagecolw = pagew/columns in
1303 let pagedispx =
1304 if pagecolw < state.winw
1305 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1306 else pagedispx
1308 let pagevw =
1309 let vw = state.winw - pagedispx - state.scrollw in
1310 let pw = pagew - pagex in
1311 min vw pw
1313 let pagevw = min pagevw pagecolw in
1314 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1315 if pagevw > 0 && pagevh > 0
1316 then
1317 let e =
1318 { pageno = n/columns
1319 ; pagedimno = pdimno
1320 ; pagew = pagew
1321 ; pageh = pageh
1322 ; pagex = pagex
1323 ; pagey = pagey
1324 ; pagevw = pagevw
1325 ; pagevh = pagevh
1326 ; pagedispx = pagedispx
1327 ; pagedispy = pagedispy
1328 ; pagecol = n mod columns
1331 e :: accu
1332 else
1333 accu
1334 else
1335 accu
1337 fold accu (n+1)
1339 List.rev (fold [] 0)
1342 let layout y sh =
1343 if nogeomcmds state.geomcmds
1344 then
1345 match conf.columns with
1346 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1347 | Cmulti c -> layoutN c y sh
1348 | Csplit s -> layoutS s y sh
1349 else []
1352 let clamp incr =
1353 let y = state.y + incr in
1354 let y = max 0 y in
1355 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1359 let itertiles l f =
1360 let tilex = l.pagex mod conf.tilew in
1361 let tiley = l.pagey mod conf.tileh in
1363 let col = l.pagex / conf.tilew in
1364 let row = l.pagey / conf.tileh in
1366 let rec rowloop row y0 dispy h =
1367 if h = 0
1368 then ()
1369 else (
1370 let dh = conf.tileh - y0 in
1371 let dh = min h dh in
1372 let rec colloop col x0 dispx w =
1373 if w = 0
1374 then ()
1375 else (
1376 let dw = conf.tilew - x0 in
1377 let dw = min w dw in
1379 f col row dispx dispy x0 y0 dw dh;
1380 colloop (col+1) 0 (dispx+dw) (w-dw)
1383 colloop col tilex l.pagedispx l.pagevw;
1384 rowloop (row+1) 0 (dispy+dh) (h-dh)
1387 if l.pagevw > 0 && l.pagevh > 0
1388 then rowloop row tiley l.pagedispy l.pagevh;
1391 let gettileopaque l col row =
1392 let key =
1393 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1395 try Some (Hashtbl.find state.tilemap key)
1396 with Not_found -> None
1399 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1400 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1401 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1404 let drawtiles l color =
1405 GlDraw.color color;
1406 let f col row x y tilex tiley w h =
1407 match gettileopaque l col row with
1408 | Some (opaque, _, t) ->
1409 let params = x, y, w, h, tilex, tiley in
1410 if conf.invert
1411 then (
1412 Gl.enable `blend;
1413 GlFunc.blend_func `zero `one_minus_src_color;
1415 drawtile params opaque;
1416 if conf.invert
1417 then Gl.disable `blend;
1418 if conf.debug
1419 then (
1420 let s = Printf.sprintf
1421 "%d[%d,%d] %f sec"
1422 l.pageno col row t
1424 let w = measurestr fstate.fontsize s in
1425 GlMisc.push_attrib [`current];
1426 GlDraw.color (0.0, 0.0, 0.0);
1427 GlDraw.rect
1428 (float (x-2), float (y-2))
1429 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1430 GlDraw.color (1.0, 1.0, 1.0);
1431 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1432 GlMisc.pop_attrib ();
1435 | _ ->
1436 let w =
1437 let lw = state.winw - state.scrollw - x in
1438 min lw w
1439 and h =
1440 let lh = state.winh - y in
1441 min lh h
1443 begin match state.texid with
1444 | Some id ->
1445 Gl.enable `texture_2d;
1446 GlTex.bind_texture `texture_2d id;
1447 let x0 = float x
1448 and y0 = float y
1449 and x1 = float (x+w)
1450 and y1 = float (y+h) in
1452 let tw = float w /. 16.0
1453 and th = float h /. 16.0 in
1454 let tx0 = float tilex /. 16.0
1455 and ty0 = float tiley /. 16.0 in
1456 let tx1 = tx0 +. tw
1457 and ty1 = ty0 +. th in
1458 GlDraw.begins `quads;
1459 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1460 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1461 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1462 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1463 GlDraw.ends ();
1465 Gl.disable `texture_2d;
1466 | None ->
1467 GlDraw.color (1.0, 1.0, 1.0);
1468 GlDraw.rect
1469 (float x, float y)
1470 (float (x+w), float (y+h));
1471 end;
1472 if w > 128 && h > fstate.fontsize + 10
1473 then (
1474 GlDraw.color (0.0, 0.0, 0.0);
1475 let c, r =
1476 if conf.verbose
1477 then (col*conf.tilew, row*conf.tileh)
1478 else col, row
1480 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1482 GlDraw.color color;
1484 itertiles l f
1487 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1489 let tilevisible1 l x y =
1490 let ax0 = l.pagex
1491 and ax1 = l.pagex + l.pagevw
1492 and ay0 = l.pagey
1493 and ay1 = l.pagey + l.pagevh in
1495 let bx0 = x
1496 and by0 = y in
1497 let bx1 = min (bx0 + conf.tilew) l.pagew
1498 and by1 = min (by0 + conf.tileh) l.pageh in
1500 let rx0 = max ax0 bx0
1501 and ry0 = max ay0 by0
1502 and rx1 = min ax1 bx1
1503 and ry1 = min ay1 by1 in
1505 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1506 nonemptyintersection
1509 let tilevisible layout n x y =
1510 let rec findpageinlayout m = function
1511 | l :: rest when l.pageno = n ->
1512 tilevisible1 l x y || (
1513 match conf.columns with
1514 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1515 | _ -> false
1517 | _ :: rest -> findpageinlayout 0 rest
1518 | [] -> false
1520 findpageinlayout 0 layout;
1523 let tileready l x y =
1524 tilevisible1 l x y &&
1525 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1528 let tilepage n p layout =
1529 let rec loop = function
1530 | l :: rest ->
1531 if l.pageno = n
1532 then
1533 let f col row _ _ _ _ _ _ =
1534 if state.currently = Idle
1535 then
1536 match gettileopaque l col row with
1537 | Some _ -> ()
1538 | None ->
1539 let x = col*conf.tilew
1540 and y = row*conf.tileh in
1541 let w =
1542 let w = l.pagew - x in
1543 min w conf.tilew
1545 let h =
1546 let h = l.pageh - y in
1547 min h conf.tileh
1549 let pbo =
1550 if conf.usepbo
1551 then getpbo w h conf.colorspace
1552 else "0"
1554 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1555 state.currently <-
1556 Tiling (
1557 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1558 conf.tilew, conf.tileh
1561 itertiles l f;
1562 else
1563 loop rest
1565 | [] -> ()
1567 if nogeomcmds state.geomcmds
1568 then loop layout;
1571 let preloadlayout y =
1572 let y = if y < state.winh then 0 else y - state.winh in
1573 let h = state.winh*3 in
1574 layout y h;
1577 let load pages =
1578 let rec loop pages =
1579 if state.currently != Idle
1580 then ()
1581 else
1582 match pages with
1583 | l :: rest ->
1584 begin match getopaque l.pageno with
1585 | None ->
1586 wcmd "page %d %d" l.pageno l.pagedimno;
1587 state.currently <- Loading (l, state.gen);
1588 | Some opaque ->
1589 tilepage l.pageno opaque pages;
1590 loop rest
1591 end;
1592 | _ -> ()
1594 if nogeomcmds state.geomcmds
1595 then loop pages
1598 let preload pages =
1599 load pages;
1600 if conf.preload && state.currently = Idle
1601 then load (preloadlayout state.y);
1604 let layoutready layout =
1605 let rec fold all ls =
1606 all && match ls with
1607 | l :: rest ->
1608 let seen = ref false in
1609 let allvisible = ref true in
1610 let foo col row _ _ _ _ _ _ =
1611 seen := true;
1612 allvisible := !allvisible &&
1613 begin match gettileopaque l col row with
1614 | Some _ -> true
1615 | None -> false
1618 itertiles l foo;
1619 fold (!seen && !allvisible) rest
1620 | [] -> true
1622 let alltilesvisible = fold true layout in
1623 alltilesvisible;
1626 let gotoy y =
1627 state.wthack <- false;
1628 let y = bound y 0 state.maxy in
1629 let y, layout, proceed =
1630 match conf.maxwait with
1631 | Some time when state.ghyll == noghyll ->
1632 begin match state.throttle with
1633 | None ->
1634 let layout = layout y state.winh in
1635 let ready = layoutready layout in
1636 if not ready
1637 then (
1638 load layout;
1639 state.throttle <- Some (layout, y, now ());
1641 else G.postRedisplay "gotoy showall (None)";
1642 y, layout, ready
1643 | Some (_, _, started) ->
1644 let dt = now () -. started in
1645 if dt > time
1646 then (
1647 state.throttle <- None;
1648 let layout = layout y state.winh in
1649 load layout;
1650 G.postRedisplay "maxwait";
1651 y, layout, true
1653 else -1, [], false
1656 | _ ->
1657 let layout = layout y state.winh in
1658 G.postRedisplay "gotoy ready";
1659 y, layout, true
1661 if proceed
1662 then (
1663 state.y <- y;
1664 state.layout <- layout;
1665 begin match state.mode with
1666 | LinkNav (Ltexact (pageno, linkno)) ->
1667 let rec loop = function
1668 | [] ->
1669 state.mode <- LinkNav (Ltgendir 0)
1670 | l :: _ when l.pageno = pageno ->
1671 begin match getopaque pageno with
1672 | None ->
1673 state.mode <- LinkNav (Ltgendir 0)
1674 | Some opaque ->
1675 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1676 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1677 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1678 then state.mode <- LinkNav (Ltgendir 0)
1680 | _ :: rest -> loop rest
1682 loop layout
1683 | _ -> ()
1684 end;
1685 begin match state.mode with
1686 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1687 if not (pagevisible layout pageno)
1688 then (
1689 match state.layout with
1690 | [] -> ()
1691 | l :: _ ->
1692 state.mode <- Birdseye (
1693 conf, leftx, l.pageno, hooverpageno, anchor
1696 | LinkNav (Ltgendir dir as lt) ->
1697 let linknav =
1698 let rec loop = function
1699 | [] -> lt
1700 | l :: rest ->
1701 match getopaque l.pageno with
1702 | None -> loop rest
1703 | Some opaque ->
1704 let link =
1705 let ld =
1706 if dir = 0
1707 then LDfirstvisible (l.pagex, l.pagey, dir)
1708 else (
1709 if dir > 0 then LDfirst else LDlast
1712 findlink opaque ld
1714 match link with
1715 | Lnotfound -> loop rest
1716 | Lfound n ->
1717 showlinktype (getlink opaque n);
1718 Ltexact (l.pageno, n)
1720 loop state.layout
1722 state.mode <- LinkNav linknav
1723 | _ -> ()
1724 end;
1725 preload layout;
1727 state.ghyll <- noghyll;
1728 if conf.updatecurs
1729 then (
1730 let mx, my = state.mpos in
1731 updateunder mx my;
1735 let conttiling pageno opaque =
1736 tilepage pageno opaque
1737 (if conf.preload then preloadlayout state.y else state.layout)
1740 let gotoy_and_clear_text y =
1741 if not conf.verbose then state.text <- "";
1742 gotoy y;
1745 let getanchor1 l =
1746 let top =
1747 let coloff = l.pagecol * l.pageh in
1748 float (l.pagey + coloff) /. float l.pageh
1750 let dtop =
1751 if l.pagedispy = 0
1752 then
1754 else
1755 if conf.presentation
1756 then float l.pagedispy /. float (calcips l.pageh)
1757 else float l.pagedispy /. float conf.interpagespace
1759 (l.pageno, top, dtop)
1762 let getanchor () =
1763 match state.layout with
1764 | l :: _ -> getanchor1 l
1765 | [] ->
1766 let n = page_of_y state.y in
1767 if n = -1
1768 then state.anchor
1769 else
1770 let y, h = getpageyh n in
1771 let dy = y - state.y in
1772 let dtop =
1773 if conf.presentation
1774 then
1775 let ips = calcips h in
1776 float (dy + ips) /. float ips
1777 else
1778 float dy /. float conf.interpagespace
1780 (n, 0.0, dtop)
1783 let getanchory (n, top, dtop) =
1784 let y, h = getpageyh n in
1785 if conf.presentation
1786 then
1787 let ips = calcips h in
1788 y + truncate (top*.float h -. dtop*.float ips) + ips;
1789 else
1790 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1793 let gotoanchor anchor =
1794 gotoy (getanchory anchor);
1797 let addnav () =
1798 cbput state.hists.nav (getanchor ());
1801 let getnav dir =
1802 let anchor = cbgetc state.hists.nav dir in
1803 getanchory anchor;
1806 let gotoghyll y =
1807 let scroll f n a b =
1808 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1809 let snake f a b =
1810 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1811 if f < a
1812 then s (float f /. float a)
1813 else (
1814 if f > b
1815 then 1.0 -. s ((float (f-b) /. float (n-b)))
1816 else 1.0
1819 snake f a b
1820 and summa f n a b =
1821 (* courtesy:
1822 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1823 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1824 let iv1 = iv f in
1825 let ins = float a *. iv1
1826 and outs = float (n-b) *. iv1 in
1827 let ones = b - a in
1828 ins +. outs +. float ones
1830 let rec set (_N, _A, _B) y sy =
1831 let sum = summa 1.0 _N _A _B in
1832 let dy = float (y - sy) in
1833 state.ghyll <- (
1834 let rec gf n y1 o =
1835 if n >= _N
1836 then state.ghyll <- noghyll
1837 else
1838 let go n =
1839 let s = scroll n _N _A _B in
1840 let y1 = y1 +. ((s *. dy) /. sum) in
1841 gotoy_and_clear_text (truncate y1);
1842 state.ghyll <- gf (n+1) y1;
1844 match o with
1845 | None -> go n
1846 | Some y' -> set (_N/2, 1, 1) y' state.y
1848 gf 0 (float state.y)
1851 match conf.ghyllscroll with
1852 | None ->
1853 gotoy_and_clear_text y
1854 | Some nab ->
1855 if state.ghyll == noghyll
1856 then set nab y state.y
1857 else state.ghyll (Some y)
1860 let gotopage n top =
1861 let y, h = getpageyh n in
1862 let y = y + (truncate (top *. float h)) in
1863 gotoghyll y
1866 let gotopage1 n top =
1867 let y = getpagey n in
1868 let y = y + top in
1869 gotoghyll y
1872 let invalidate s f =
1873 state.layout <- [];
1874 state.pdims <- [];
1875 state.rects <- [];
1876 state.rects1 <- [];
1877 match state.geomcmds with
1878 | ps, [] when String.length ps = 0 ->
1879 f ();
1880 state.geomcmds <- s, [];
1882 | ps, [] ->
1883 state.geomcmds <- ps, [s, f];
1885 | ps, (s', _) :: rest when s' = s ->
1886 state.geomcmds <- ps, ((s, f) :: rest);
1888 | ps, cmds ->
1889 state.geomcmds <- ps, ((s, f) :: cmds);
1892 let flushpages () =
1893 Hashtbl.iter (fun _ opaque ->
1894 wcmd "freepage %s" opaque;
1895 ) state.pagemap;
1896 Hashtbl.clear state.pagemap;
1899 let opendoc path password =
1900 state.path <- path;
1901 state.password <- password;
1902 state.gen <- state.gen + 1;
1903 state.docinfo <- [];
1905 flushpages ();
1906 setaalevel conf.aalevel;
1907 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1908 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1909 invalidate "reqlayout"
1910 (fun () ->
1911 wcmd "reqlayout %d %d %s\000"
1912 conf.angle (btod conf.proportional) state.nameddest;
1916 let reload () =
1917 state.anchor <- getanchor ();
1918 state.wthack <- !wtmode;
1919 opendoc state.path state.password;
1922 let scalecolor c =
1923 let c = c *. conf.colorscale in
1924 (c, c, c);
1927 let scalecolor2 (r, g, b) =
1928 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1931 let docolumns = function
1932 | Csingle _ ->
1933 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1934 let rec loop pageno pdimno pdim y ph pdims =
1935 if pageno = state.pagecount
1936 then ()
1937 else
1938 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1939 match pdims with
1940 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1941 pdimno+1, pdim, rest
1942 | _ ->
1943 pdimno, pdim, pdims
1945 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1946 let y = y +
1947 (if conf.presentation
1948 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1949 else (if pageno = 0 then 0 else conf.interpagespace)
1952 a.(pageno) <- (pdimno, x, y, pdim);
1953 loop (pageno+1) pdimno pdim (y + h) h pdims
1955 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1956 conf.columns <- Csingle a;
1958 | Cmulti ((columns, coverA, coverB), _) ->
1959 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1960 let rec loop pageno pdimno pdim x y rowh pdims =
1961 let rec fixrow m = if m = pageno then () else
1962 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1963 if h < rowh
1964 then (
1965 let y = y + (rowh - h) / 2 in
1966 a.(m) <- (pdimno, x, y, pdim);
1968 fixrow (m+1)
1970 if pageno = state.pagecount
1971 then fixrow (((pageno - 1) / columns) * columns)
1972 else
1973 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1974 match pdims with
1975 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1976 pdimno+1, pdim, rest
1977 | _ ->
1978 pdimno, pdim, pdims
1980 let x, y, rowh' =
1981 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1982 then (
1983 let x = (state.winw - state.scrollw - w) / 2 in
1984 let ips =
1985 if conf.presentation then calcips h else conf.interpagespace in
1986 x, y + ips + rowh, h
1988 else (
1989 if (pageno - coverA) mod columns = 0
1990 then (
1991 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
1992 let y =
1993 if conf.presentation
1994 then
1995 let ips = calcips h in
1996 y + (if pageno = 0 then 0 else calcips rowh + ips)
1997 else
1998 y + (if pageno = 0 then 0 else conf.interpagespace)
2000 x, y + rowh, h
2002 else x, y, max rowh h
2005 let y =
2006 if pageno > 1 && (pageno - coverA) mod columns = 0
2007 then (
2008 let y =
2009 if pageno = columns && conf.presentation
2010 then (
2011 let ips = calcips rowh in
2012 for i = 0 to pred columns
2014 let (pdimno, x, y, pdim) = a.(i) in
2015 a.(i) <- (pdimno, x, y+ips, pdim)
2016 done;
2017 y+ips;
2019 else y
2021 fixrow (pageno - columns);
2024 else y
2026 a.(pageno) <- (pdimno, x, y, pdim);
2027 let x = x + w + xoff*2 + conf.interpagespace in
2028 loop (pageno+1) pdimno pdim x y rowh' pdims
2030 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2031 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2033 | Csplit (c, _) ->
2034 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2035 let rec loop pageno pdimno pdim y pdims =
2036 if pageno = state.pagecount
2037 then ()
2038 else
2039 let pdimno, ((_, w, h, _) as pdim), pdims =
2040 match pdims with
2041 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2042 pdimno+1, pdim, rest
2043 | _ ->
2044 pdimno, pdim, pdims
2046 let cw = w / c in
2047 let rec loop1 n x y =
2048 if n = c then y else (
2049 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2050 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2053 let y = loop1 0 0 y in
2054 loop (pageno+1) pdimno pdim y pdims
2056 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2057 conf.columns <- Csplit (c, a);
2060 let represent () =
2061 docolumns conf.columns;
2062 state.maxy <- calcheight ();
2063 state.hscrollh <-
2064 if state.x = 0 && state.w <= state.winw - state.scrollw
2065 then 0
2066 else state.scrollw
2068 if state.reprf == noreprf
2069 then (
2070 match state.mode with
2071 | Birdseye (_, _, pageno, _, _) ->
2072 let y, h = getpageyh pageno in
2073 let top = (state.winh - h) / 2 in
2074 gotoy (max 0 (y - top))
2075 | _ -> gotoanchor state.anchor
2077 else (
2078 state.reprf ();
2079 state.reprf <- noreprf;
2083 let reshape w h =
2084 state.wthack <- false;
2085 GlDraw.viewport 0 0 w h;
2086 let firsttime = state.geomcmds == firstgeomcmds in
2087 if not firsttime && nogeomcmds state.geomcmds
2088 then state.anchor <- getanchor ();
2090 state.winw <- w;
2091 let w = truncate (float w *. conf.zoom) - state.scrollw in
2092 let w = max w 2 in
2093 state.winh <- h;
2094 setfontsize fstate.fontsize;
2095 GlMat.mode `modelview;
2096 GlMat.load_identity ();
2098 GlMat.mode `projection;
2099 GlMat.load_identity ();
2100 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2101 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2102 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2104 let relx =
2105 if conf.zoom <= 1.0
2106 then 0.0
2107 else float state.x /. float state.w
2109 invalidate "geometry"
2110 (fun () ->
2111 state.w <- w;
2112 if not firsttime
2113 then state.x <- truncate (relx *. float w);
2114 let w =
2115 match conf.columns with
2116 | Csingle _ -> w
2117 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2118 | Csplit (c, _) -> w * c
2120 wcmd "geometry %d %d" w h);
2123 let enttext () =
2124 let len = String.length state.text in
2125 let drawstring s =
2126 let hscrollh =
2127 match state.mode with
2128 | Textentry _
2129 | View ->
2130 let h, _, _ = state.uioh#scrollpw in
2132 | _ -> 0
2134 let rect x w =
2135 GlDraw.rect
2136 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2137 (x+.w, float (state.winh - hscrollh))
2140 let w = float (state.winw - state.scrollw - 1) in
2141 if state.progress >= 0.0 && state.progress < 1.0
2142 then (
2143 GlDraw.color (0.3, 0.3, 0.3);
2144 let w1 = w *. state.progress in
2145 rect 0.0 w1;
2146 GlDraw.color (0.0, 0.0, 0.0);
2147 rect w1 (w-.w1)
2149 else (
2150 GlDraw.color (0.0, 0.0, 0.0);
2151 rect 0.0 w;
2154 GlDraw.color (1.0, 1.0, 1.0);
2155 drawstring fstate.fontsize
2156 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2158 let s =
2159 match state.mode with
2160 | Textentry ((prefix, text, _, _, _, _), _) ->
2161 let s =
2162 if len > 0
2163 then
2164 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2165 else
2166 Printf.sprintf "%s%s_" prefix text
2170 | _ -> state.text
2172 let s =
2173 if state.newerrmsgs
2174 then (
2175 if not (istextentry state.mode)
2176 then
2177 let s1 = "(press 'e' to review error messasges)" in
2178 if String.length s > 0 then s ^ " " ^ s1 else s1
2179 else s
2181 else s
2183 if String.length s > 0
2184 then drawstring s
2187 let gctiles () =
2188 let len = Queue.length state.tilelru in
2189 let layout = lazy (
2190 match state.throttle with
2191 | None ->
2192 if conf.preload
2193 then preloadlayout state.y
2194 else state.layout
2195 | Some (layout, _, _) ->
2196 layout
2197 ) in
2198 let rec loop qpos =
2199 if state.memused <= conf.memlimit
2200 then ()
2201 else (
2202 if qpos < len
2203 then
2204 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2205 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2206 let (_, pw, ph, _) = getpagedim n in
2208 gen = state.gen
2209 && colorspace = conf.colorspace
2210 && angle = conf.angle
2211 && pagew = pw
2212 && pageh = ph
2213 && (
2214 let x = col*conf.tilew
2215 and y = row*conf.tileh in
2216 tilevisible (Lazy.force_val layout) n x y
2218 then Queue.push lruitem state.tilelru
2219 else (
2220 freepbo p;
2221 wcmd "freetile %s" p;
2222 state.memused <- state.memused - s;
2223 state.uioh#infochanged Memused;
2224 Hashtbl.remove state.tilemap k;
2226 loop (qpos+1)
2229 loop 0
2232 let flushtiles () =
2233 Queue.iter (fun (k, p, s) ->
2234 wcmd "freetile %s" p;
2235 state.memused <- state.memused - s;
2236 state.uioh#infochanged Memused;
2237 Hashtbl.remove state.tilemap k;
2238 ) state.tilelru;
2239 Queue.clear state.tilelru;
2240 load state.layout;
2243 let logcurrently = function
2244 | Idle -> dolog "Idle"
2245 | Loading (l, gen) ->
2246 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2247 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2248 dolog
2249 "Tiling %d[%d,%d] page=%s cs=%s angle"
2250 l.pageno col row pageopaque
2251 (colorspace_to_string colorspace)
2253 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2254 angle gen conf.angle state.gen
2255 tilew tileh
2256 conf.tilew conf.tileh
2258 | Outlining _ ->
2259 dolog "outlining"
2262 let splitatspace =
2263 let r = Str.regexp " " in
2264 fun s -> Str.bounded_split r s 2;
2267 let onpagerect pageno f =
2268 let b =
2269 match conf.columns with
2270 | Cmulti (_, b) -> b
2271 | Csingle b -> b
2272 | Csplit (_, b) -> b
2274 if pageno >= 0 && pageno < Array.length b
2275 then
2276 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2277 let r = getpdimrect pdimno in
2278 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2281 let gotopagexy pageno x y =
2282 onpagerect pageno (fun w h ->
2283 let top = y /. h in
2284 let _,w1,_,leftx = getpagedim pageno in
2285 let wh = state.winh - state.hscrollh in
2286 let sw = float w1 /. w in
2287 let x = sw *. x in
2288 let x = leftx + state.x + truncate x in
2289 let sx =
2290 if x < 0 || x >= state.winw - state.scrollw
2291 then state.x - x
2292 else state.x
2294 let py, h = getpageyh pageno in
2295 let y' = py + truncate (top *. float h) in
2296 let dy = y' - state.y in
2297 let sy =
2298 if x != state.x || not (dy > 0 && dy < wh)
2299 then (
2300 if conf.presentation
2301 then
2302 if abs (py - y') > wh
2303 then y'
2304 else py
2305 else y';
2307 else state.y
2309 if state.x != sx || state.y != sy
2310 then (
2311 let x, y =
2312 if !wtmode
2313 then (
2314 let ww = state.winw - state.scrollw in
2315 let qx = sx / ww
2316 and qy = sy / wh in
2317 let x = qx * ww
2318 and y = qy * wh in
2319 let x = if -x + ww > w1 then -(w1-ww) else x
2320 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2321 let y =
2322 if conf.presentation
2323 then
2324 if abs (py - y') > wh
2325 then y'
2326 else py
2327 else y';
2329 (x, y)
2331 else (sx, sy)
2333 state.x <- x;
2334 state.hscrollh <-
2335 if x = 0 && state.w <= state.winw - state.scrollw
2336 then 0
2337 else state.scrollw
2339 gotoy_and_clear_text y;
2341 else gotoy_and_clear_text state.y;
2342 state.wthack <- !wtmode && not (layoutready state.layout);
2346 let act cmds =
2347 (* dolog "%S" cmds; *)
2348 let cl = splitatspace cmds in
2349 let scan s fmt f =
2350 try Scanf.sscanf s fmt f
2351 with exn ->
2352 dolog "error processing '%S': %s" cmds (exntos exn);
2353 exit 1
2355 match cl with
2356 | "clear" :: [] ->
2357 state.uioh#infochanged Pdim;
2358 state.pdims <- [];
2360 | "clearrects" :: [] ->
2361 state.rects <- state.rects1;
2362 G.postRedisplay "clearrects";
2364 | "continue" :: args :: [] ->
2365 let n = scan args "%u" (fun n -> n) in
2366 state.pagecount <- n;
2367 begin match state.currently with
2368 | Outlining l ->
2369 state.currently <- Idle;
2370 state.outlines <- Array.of_list (List.rev l)
2371 | _ -> ()
2372 end;
2374 let cur, cmds = state.geomcmds in
2375 if String.length cur = 0
2376 then failwith "umpossible";
2378 begin match List.rev cmds with
2379 | [] ->
2380 state.geomcmds <- "", [];
2381 represent ();
2382 | (s, f) :: rest ->
2383 f ();
2384 state.geomcmds <- s, List.rev rest;
2385 end;
2386 if conf.maxwait = None
2387 then G.postRedisplay "continue";
2389 | "title" :: args :: [] ->
2390 Wsi.settitle args
2392 | "msg" :: args :: [] ->
2393 showtext ' ' args
2395 | "vmsg" :: args :: [] ->
2396 if conf.verbose
2397 then showtext ' ' args
2399 | "emsg" :: args :: [] ->
2400 Buffer.add_string state.errmsgs args;
2401 state.newerrmsgs <- true;
2402 G.postRedisplay "error message"
2404 | "progress" :: args :: [] ->
2405 let progress, text =
2406 scan args "%f %n"
2407 (fun f pos ->
2408 f, String.sub args pos (String.length args - pos))
2410 state.text <- text;
2411 state.progress <- progress;
2412 G.postRedisplay "progress"
2414 | "firstmatch" :: args :: [] ->
2415 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2416 scan args "%u %d %f %f %f %f %f %f %f %f"
2417 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2418 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2420 let y = (getpagey pageno) + truncate y0 in
2421 addnav ();
2422 gotoy y;
2423 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2425 | "match" :: args :: [] ->
2426 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2427 scan args "%u %d %f %f %f %f %f %f %f %f"
2428 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2429 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2431 state.rects1 <-
2432 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2434 | "page" :: args :: [] ->
2435 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2436 begin match state.currently with
2437 | Loading (l, gen) ->
2438 vlog "page %d took %f sec" l.pageno t;
2439 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2440 begin match state.throttle with
2441 | None ->
2442 let preloadedpages =
2443 if conf.preload
2444 then preloadlayout state.y
2445 else state.layout
2447 let evict () =
2448 let module IntSet =
2449 Set.Make (struct type t = int let compare = (-) end) in
2450 let set =
2451 List.fold_left (fun s l -> IntSet.add l.pageno s)
2452 IntSet.empty preloadedpages
2454 let evictedpages =
2455 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2456 if not (IntSet.mem pageno set)
2457 then (
2458 wcmd "freepage %s" opaque;
2459 key :: accu
2461 else accu
2462 ) state.pagemap []
2464 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2466 evict ();
2467 state.currently <- Idle;
2468 if gen = state.gen
2469 then (
2470 tilepage l.pageno pageopaque state.layout;
2471 load state.layout;
2472 load preloadedpages;
2473 if pagevisible state.layout l.pageno
2474 && layoutready state.layout
2475 then G.postRedisplay "page";
2478 | Some (layout, _, _) ->
2479 state.currently <- Idle;
2480 tilepage l.pageno pageopaque layout;
2481 load state.layout
2482 end;
2484 | _ ->
2485 dolog "Inconsistent loading state";
2486 logcurrently state.currently;
2487 exit 1
2490 | "tile" :: args :: [] ->
2491 let (x, y, opaque, size, t) =
2492 scan args "%u %u %s %u %f"
2493 (fun x y p size t -> (x, y, p, size, t))
2495 begin match state.currently with
2496 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2497 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2499 unmappbo opaque;
2500 if tilew != conf.tilew || tileh != conf.tileh
2501 then (
2502 wcmd "freetile %s" opaque;
2503 state.currently <- Idle;
2504 load state.layout;
2506 else (
2507 puttileopaque l col row gen cs angle opaque size t;
2508 state.memused <- state.memused + size;
2509 state.uioh#infochanged Memused;
2510 gctiles ();
2511 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2512 opaque, size) state.tilelru;
2514 let layout =
2515 match state.throttle with
2516 | None -> state.layout
2517 | Some (layout, _, _) -> layout
2520 state.currently <- Idle;
2521 if gen = state.gen
2522 && conf.colorspace = cs
2523 && conf.angle = angle
2524 && tilevisible layout l.pageno x y
2525 then conttiling l.pageno pageopaque;
2527 begin match state.throttle with
2528 | None ->
2529 if state.wthack
2530 then state.wthack <- not (layoutready state.layout);
2531 preload state.layout;
2532 if gen = state.gen
2533 && conf.colorspace = cs
2534 && conf.angle = angle
2535 && tilevisible state.layout l.pageno x y
2536 then G.postRedisplay "tile nothrottle";
2538 | Some (layout, y, _) ->
2539 let ready = layoutready layout in
2540 if ready
2541 then (
2542 state.wthack <- false;
2543 state.y <- y;
2544 state.layout <- layout;
2545 state.throttle <- None;
2546 G.postRedisplay "throttle";
2548 else load layout;
2549 end;
2552 | _ ->
2553 dolog "Inconsistent tiling state";
2554 logcurrently state.currently;
2555 exit 1
2558 | "pdim" :: args :: [] ->
2559 let pdim =
2560 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2562 state.uioh#infochanged Pdim;
2563 state.pdims <- pdim :: state.pdims
2565 | "o" :: args :: [] ->
2566 let (l, n, t, h, pos) =
2567 scan args "%u %u %d %u %n"
2568 (fun l n t h pos -> l, n, t, h, pos)
2570 let s = String.sub args pos (String.length args - pos) in
2571 let outline = (s, l, (n, float t /. float h, 0.0)) in
2572 begin match state.currently with
2573 | Outlining outlines ->
2574 state.currently <- Outlining (outline :: outlines)
2575 | Idle ->
2576 state.currently <- Outlining [outline]
2577 | currently ->
2578 dolog "invalid outlining state";
2579 logcurrently currently
2582 | "a" :: args :: [] ->
2583 let (n, l, t) =
2584 scan args "%u %d %d" (fun n l t -> n, l, t)
2586 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2588 | "info" :: args :: [] ->
2589 state.docinfo <- (1, args) :: state.docinfo
2591 | "infoend" :: [] ->
2592 state.uioh#infochanged Docinfo;
2593 state.docinfo <- List.rev state.docinfo
2595 | _ ->
2596 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2599 let onhist cb =
2600 let rc = cb.rc in
2601 let action = function
2602 | HCprev -> cbget cb ~-1
2603 | HCnext -> cbget cb 1
2604 | HCfirst -> cbget cb ~-(cb.rc)
2605 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2606 and cancel () = cb.rc <- rc
2607 in (action, cancel)
2610 let search pattern forward =
2611 if String.length pattern > 0
2612 then
2613 let pn, py =
2614 match state.layout with
2615 | [] -> 0, 0
2616 | l :: _ ->
2617 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2619 wcmd "search %d %d %d %d,%s\000"
2620 (btod conf.icase) pn py (btod forward) pattern;
2623 let intentry text key =
2624 let c =
2625 if key >= 32 && key < 127
2626 then Char.chr key
2627 else '\000'
2629 match c with
2630 | '0' .. '9' ->
2631 let text = addchar text c in
2632 TEcont text
2634 | _ ->
2635 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2636 TEcont text
2639 let linknentry text key =
2640 let c =
2641 if key >= 32 && key < 127
2642 then Char.chr key
2643 else '\000'
2645 match c with
2646 | 'a' .. 'z' ->
2647 let text = addchar text c in
2648 TEcont text
2650 | _ ->
2651 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2652 TEcont text
2655 let linkndone f s =
2656 if String.length s > 0
2657 then (
2658 let n =
2659 let l = String.length s in
2660 let rec loop pos n = if pos = l then n else
2661 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2662 loop (pos+1) (n*26 + m)
2663 in loop 0 0
2665 let rec loop n = function
2666 | [] -> ()
2667 | l :: rest ->
2668 match getopaque l.pageno with
2669 | None -> loop n rest
2670 | Some opaque ->
2671 let m = getlinkcount opaque in
2672 if n < m
2673 then (
2674 let under = getlink opaque n in
2675 f under
2677 else loop (n-m) rest
2679 loop n state.layout;
2683 let textentry text key =
2684 if key land 0xff00 = 0xff00
2685 then TEcont text
2686 else TEcont (text ^ toutf8 key)
2689 let reqlayout angle proportional =
2690 match state.throttle with
2691 | None ->
2692 if nogeomcmds state.geomcmds
2693 then state.anchor <- getanchor ();
2694 conf.angle <- angle mod 360;
2695 if conf.angle != 0
2696 then (
2697 match state.mode with
2698 | LinkNav _ -> state.mode <- View
2699 | _ -> ()
2701 conf.proportional <- proportional;
2702 invalidate "reqlayout"
2703 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2704 | _ -> ()
2707 let settrim trimmargins trimfuzz =
2708 if nogeomcmds state.geomcmds
2709 then state.anchor <- getanchor ();
2710 conf.trimmargins <- trimmargins;
2711 conf.trimfuzz <- trimfuzz;
2712 let x0, y0, x1, y1 = trimfuzz in
2713 invalidate "settrim"
2714 (fun () ->
2715 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2716 flushpages ();
2719 let setzoom zoom =
2720 match state.throttle with
2721 | None ->
2722 let zoom = max 0.01 zoom in
2723 if zoom <> conf.zoom
2724 then (
2725 state.prevzoom <- conf.zoom;
2726 conf.zoom <- zoom;
2727 reshape state.winw state.winh;
2728 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2731 | Some (layout, y, started) ->
2732 let time =
2733 match conf.maxwait with
2734 | None -> 0.0
2735 | Some t -> t
2737 let dt = now () -. started in
2738 if dt > time
2739 then (
2740 state.y <- y;
2741 load layout;
2745 let setcolumns mode columns coverA coverB =
2746 state.prevcolumns <- Some (conf.columns, conf.zoom);
2747 if columns < 0
2748 then (
2749 if isbirdseye mode
2750 then showtext '!' "split mode doesn't work in bird's eye"
2751 else (
2752 conf.columns <- Csplit (-columns, [||]);
2753 state.x <- 0;
2754 conf.zoom <- 1.0;
2757 else (
2758 if columns < 2
2759 then (
2760 conf.columns <- Csingle [||];
2761 state.x <- 0;
2762 setzoom 1.0;
2764 else (
2765 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2766 conf.zoom <- 1.0;
2769 reshape state.winw state.winh;
2772 let enterbirdseye () =
2773 let zoom = float conf.thumbw /. float state.winw in
2774 let birdseyepageno =
2775 let cy = state.winh / 2 in
2776 let fold = function
2777 | [] -> 0
2778 | l :: rest ->
2779 let rec fold best = function
2780 | [] -> best.pageno
2781 | l :: rest ->
2782 let d = cy - (l.pagedispy + l.pagevh/2)
2783 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2784 if abs d < abs dbest
2785 then fold l rest
2786 else best.pageno
2787 in fold l rest
2789 fold state.layout
2791 state.mode <- Birdseye (
2792 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2794 conf.zoom <- zoom;
2795 conf.presentation <- false;
2796 conf.interpagespace <- 10;
2797 conf.hlinks <- false;
2798 state.x <- 0;
2799 state.mstate <- Mnone;
2800 conf.maxwait <- None;
2801 conf.columns <- (
2802 match conf.beyecolumns with
2803 | Some c ->
2804 conf.zoom <- 1.0;
2805 Cmulti ((c, 0, 0), [||])
2806 | None -> Csingle [||]
2808 Wsi.setcursor Wsi.CURSOR_INHERIT;
2809 if conf.verbose
2810 then
2811 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2812 (100.0*.zoom)
2813 else
2814 state.text <- ""
2816 reshape state.winw state.winh;
2819 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2820 state.mode <- View;
2821 conf.zoom <- c.zoom;
2822 conf.presentation <- c.presentation;
2823 conf.interpagespace <- c.interpagespace;
2824 conf.maxwait <- c.maxwait;
2825 conf.hlinks <- c.hlinks;
2826 conf.beyecolumns <- (
2827 match conf.columns with
2828 | Cmulti ((c, _, _), _) -> Some c
2829 | Csingle _ -> None
2830 | Csplit _ -> failwith "leaving bird's eye split mode"
2832 conf.columns <- (
2833 match c.columns with
2834 | Cmulti (c, _) -> Cmulti (c, [||])
2835 | Csingle _ -> Csingle [||]
2836 | Csplit (c, _) -> Csplit (c, [||])
2838 state.x <- leftx;
2839 if conf.verbose
2840 then
2841 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2842 (100.0*.conf.zoom)
2844 reshape state.winw state.winh;
2845 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2848 let togglebirdseye () =
2849 match state.mode with
2850 | Birdseye vals -> leavebirdseye vals true
2851 | View -> enterbirdseye ()
2852 | _ -> ()
2855 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2856 let pageno = max 0 (pageno - incr) in
2857 let rec loop = function
2858 | [] -> gotopage1 pageno 0
2859 | l :: _ when l.pageno = pageno ->
2860 if l.pagedispy >= 0 && l.pagey = 0
2861 then G.postRedisplay "upbirdseye"
2862 else gotopage1 pageno 0
2863 | _ :: rest -> loop rest
2865 loop state.layout;
2866 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2869 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2870 let pageno = min (state.pagecount - 1) (pageno + incr) in
2871 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2872 let rec loop = function
2873 | [] ->
2874 let y, h = getpageyh pageno in
2875 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2876 gotoy (clamp dy)
2877 | l :: _ when l.pageno = pageno ->
2878 if l.pagevh != l.pageh
2879 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2880 else G.postRedisplay "downbirdseye"
2881 | _ :: rest -> loop rest
2883 loop state.layout
2886 let optentry mode _ key =
2887 let btos b = if b then "on" else "off" in
2888 if key >= 32 && key < 127
2889 then
2890 let c = Char.chr key in
2891 match c with
2892 | 's' ->
2893 let ondone s =
2894 try conf.scrollstep <- int_of_string s with exc ->
2895 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2897 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2899 | 'A' ->
2900 let ondone s =
2902 conf.autoscrollstep <- int_of_string s;
2903 if state.autoscroll <> None
2904 then state.autoscroll <- Some conf.autoscrollstep
2905 with exc ->
2906 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2908 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2910 | 'C' ->
2911 let ondone s =
2913 let n, a, b = multicolumns_of_string s in
2914 setcolumns mode n a b;
2915 with exc ->
2916 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2918 TEswitch ("columns: ", "", None, textentry, ondone, true)
2920 | 'Z' ->
2921 let ondone s =
2923 let zoom = float (int_of_string s) /. 100.0 in
2924 setzoom zoom
2925 with exc ->
2926 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2928 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2930 | 't' ->
2931 let ondone s =
2933 conf.thumbw <- bound (int_of_string s) 2 4096;
2934 state.text <-
2935 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2936 begin match mode with
2937 | Birdseye beye ->
2938 leavebirdseye beye false;
2939 enterbirdseye ();
2940 | _ -> ();
2942 with exc ->
2943 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2945 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2947 | 'R' ->
2948 let ondone s =
2949 match try
2950 Some (int_of_string s)
2951 with exc ->
2952 state.text <- Printf.sprintf "bad integer `%s': %s"
2953 s (exntos exc);
2954 None
2955 with
2956 | Some angle -> reqlayout angle conf.proportional
2957 | None -> ()
2959 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2961 | 'i' ->
2962 conf.icase <- not conf.icase;
2963 TEdone ("case insensitive search " ^ (btos conf.icase))
2965 | 'p' ->
2966 conf.preload <- not conf.preload;
2967 gotoy state.y;
2968 TEdone ("preload " ^ (btos conf.preload))
2970 | 'v' ->
2971 conf.verbose <- not conf.verbose;
2972 TEdone ("verbose " ^ (btos conf.verbose))
2974 | 'd' ->
2975 conf.debug <- not conf.debug;
2976 TEdone ("debug " ^ (btos conf.debug))
2978 | 'h' ->
2979 conf.maxhfit <- not conf.maxhfit;
2980 state.maxy <- calcheight ();
2981 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2983 | 'c' ->
2984 conf.crophack <- not conf.crophack;
2985 TEdone ("crophack " ^ btos conf.crophack)
2987 | 'a' ->
2988 let s =
2989 match conf.maxwait with
2990 | None ->
2991 conf.maxwait <- Some infinity;
2992 "always wait for page to complete"
2993 | Some _ ->
2994 conf.maxwait <- None;
2995 "show placeholder if page is not ready"
2997 TEdone s
2999 | 'f' ->
3000 conf.underinfo <- not conf.underinfo;
3001 TEdone ("underinfo " ^ btos conf.underinfo)
3003 | 'P' ->
3004 conf.savebmarks <- not conf.savebmarks;
3005 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3007 | 'S' ->
3008 let ondone s =
3010 let pageno, py =
3011 match state.layout with
3012 | [] -> 0, 0
3013 | l :: _ ->
3014 l.pageno, l.pagey
3016 conf.interpagespace <- int_of_string s;
3017 docolumns conf.columns;
3018 state.maxy <- calcheight ();
3019 let y = getpagey pageno in
3020 gotoy (y + py)
3021 with exc ->
3022 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3024 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3026 | 'l' ->
3027 reqlayout conf.angle (not conf.proportional);
3028 TEdone ("proportional display " ^ btos conf.proportional)
3030 | 'T' ->
3031 settrim (not conf.trimmargins) conf.trimfuzz;
3032 TEdone ("trim margins " ^ btos conf.trimmargins)
3034 | 'I' ->
3035 conf.invert <- not conf.invert;
3036 TEdone ("invert colors " ^ btos conf.invert)
3038 | 'x' ->
3039 let ondone s =
3040 cbput state.hists.sel s;
3041 conf.selcmd <- s;
3043 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3044 textentry, ondone, true)
3046 | _ ->
3047 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3048 TEstop
3049 else
3050 TEcont state.text
3053 class type lvsource = object
3054 method getitemcount : int
3055 method getitem : int -> (string * int)
3056 method hasaction : int -> bool
3057 method exit :
3058 uioh:uioh ->
3059 cancel:bool ->
3060 active:int ->
3061 first:int ->
3062 pan:int ->
3063 qsearch:string ->
3064 uioh option
3065 method getactive : int
3066 method getfirst : int
3067 method getqsearch : string
3068 method setqsearch : string -> unit
3069 method getpan : int
3070 end;;
3072 class virtual lvsourcebase = object
3073 val mutable m_active = 0
3074 val mutable m_first = 0
3075 val mutable m_qsearch = ""
3076 val mutable m_pan = 0
3077 method getactive = m_active
3078 method getfirst = m_first
3079 method getqsearch = m_qsearch
3080 method getpan = m_pan
3081 method setqsearch s = m_qsearch <- s
3082 end;;
3084 let withoutlastutf8 s =
3085 let len = String.length s in
3086 if len = 0
3087 then s
3088 else
3089 let rec find pos =
3090 if pos = 0
3091 then pos
3092 else
3093 let b = Char.code s.[pos] in
3094 if b land 0b11000000 = 0b11000000
3095 then pos
3096 else find (pos-1)
3098 let first =
3099 if Char.code s.[len-1] land 0x80 = 0
3100 then len-1
3101 else find (len-1)
3103 String.sub s 0 first;
3106 let textentrykeyboard
3107 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3108 let key =
3109 if key >= 0xffb0 && key <= 0xffb9
3110 then key - 0xffb0 + 48 else key
3112 let enttext te =
3113 state.mode <- Textentry (te, onleave);
3114 state.text <- "";
3115 enttext ();
3116 G.postRedisplay "textentrykeyboard enttext";
3118 let histaction cmd =
3119 match opthist with
3120 | None -> ()
3121 | Some (action, _) ->
3122 state.mode <- Textentry (
3123 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3125 G.postRedisplay "textentry histaction"
3127 match key with
3128 | 0xff08 -> (* backspace *)
3129 let s = withoutlastutf8 text in
3130 let len = String.length s in
3131 if cancelonempty && len = 0
3132 then (
3133 onleave Cancel;
3134 G.postRedisplay "textentrykeyboard after cancel";
3136 else (
3137 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3140 | 0xff0d | 0xff8d -> (* (kp) enter *)
3141 ondone text;
3142 onleave Confirm;
3143 G.postRedisplay "textentrykeyboard after confirm"
3145 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3146 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3147 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3148 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3150 | 0xff1b -> (* escape*)
3151 if String.length text = 0
3152 then (
3153 begin match opthist with
3154 | None -> ()
3155 | Some (_, onhistcancel) -> onhistcancel ()
3156 end;
3157 onleave Cancel;
3158 state.text <- "";
3159 G.postRedisplay "textentrykeyboard after cancel2"
3161 else (
3162 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3165 | 0xff9f | 0xffff -> () (* delete *)
3167 | _ when key != 0
3168 && key land 0xff00 != 0xff00 (* keyboard *)
3169 && key land 0xfe00 != 0xfe00 (* xkb *)
3170 && key land 0xfd00 != 0xfd00 (* 3270 *)
3172 begin match onkey text key with
3173 | TEdone text ->
3174 ondone text;
3175 onleave Confirm;
3176 G.postRedisplay "textentrykeyboard after confirm2";
3178 | TEcont text ->
3179 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3181 | TEstop ->
3182 onleave Cancel;
3183 G.postRedisplay "textentrykeyboard after cancel3"
3185 | TEswitch te ->
3186 state.mode <- Textentry (te, onleave);
3187 G.postRedisplay "textentrykeyboard switch";
3188 end;
3190 | _ ->
3191 vlog "unhandled key %s" (Wsi.keyname key)
3194 let firstof first active =
3195 if first > active || abs (first - active) > fstate.maxrows - 1
3196 then max 0 (active - (fstate.maxrows/2))
3197 else first
3200 let calcfirst first active =
3201 if active > first
3202 then
3203 let rows = active - first in
3204 if rows > fstate.maxrows then active - fstate.maxrows else first
3205 else active
3208 let scrollph y maxy =
3209 let sh = (float (maxy + state.winh) /. float state.winh) in
3210 let sh = float state.winh /. sh in
3211 let sh = max sh (float conf.scrollh) in
3213 let percent =
3214 if y = state.maxy
3215 then 1.0
3216 else float y /. float maxy
3218 let position = (float state.winh -. sh) *. percent in
3220 let position =
3221 if position +. sh > float state.winh
3222 then float state.winh -. sh
3223 else position
3225 position, sh;
3228 let coe s = (s :> uioh);;
3230 class listview ~(source:lvsource) ~trusted ~modehash =
3231 object (self)
3232 val m_pan = source#getpan
3233 val m_first = source#getfirst
3234 val m_active = source#getactive
3235 val m_qsearch = source#getqsearch
3236 val m_prev_uioh = state.uioh
3238 method private elemunder y =
3239 let n = y / (fstate.fontsize+1) in
3240 if m_first + n < source#getitemcount
3241 then (
3242 if source#hasaction (m_first + n)
3243 then Some (m_first + n)
3244 else None
3246 else None
3248 method display =
3249 Gl.enable `blend;
3250 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3251 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3252 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3253 GlDraw.color (1., 1., 1.);
3254 Gl.enable `texture_2d;
3255 let fs = fstate.fontsize in
3256 let nfs = fs + 1 in
3257 let ww = fstate.wwidth in
3258 let tabw = 30.0*.ww in
3259 let itemcount = source#getitemcount in
3260 let rec loop row =
3261 if (row - m_first) > fstate.maxrows
3262 then ()
3263 else (
3264 if row >= 0 && row < itemcount
3265 then (
3266 let (s, level) = source#getitem row in
3267 let y = (row - m_first) * nfs in
3268 let x = 5.0 +. float (level + m_pan) *. ww in
3269 if row = m_active
3270 then (
3271 Gl.disable `texture_2d;
3272 GlDraw.polygon_mode `both `line;
3273 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3274 GlDraw.rect (1., float (y + 1))
3275 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3276 GlDraw.polygon_mode `both `fill;
3277 GlDraw.color (1., 1., 1.);
3278 Gl.enable `texture_2d;
3281 let drawtabularstring s =
3282 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3283 if trusted
3284 then
3285 let tabpos = try String.index s '\t' with Not_found -> -1 in
3286 if tabpos > 0
3287 then
3288 let len = String.length s - tabpos - 1 in
3289 let s1 = String.sub s 0 tabpos
3290 and s2 = String.sub s (tabpos + 1) len in
3291 let nx = drawstr x s1 in
3292 let sw = nx -. x in
3293 let x = x +. (max tabw sw) in
3294 drawstr x s2
3295 else
3296 drawstr x s
3297 else
3298 drawstr x s
3300 let _ = drawtabularstring s in
3301 loop (row+1)
3305 loop m_first;
3306 Gl.disable `blend;
3307 Gl.disable `texture_2d;
3309 method updownlevel incr =
3310 let len = source#getitemcount in
3311 let curlevel =
3312 if m_active >= 0 && m_active < len
3313 then snd (source#getitem m_active)
3314 else -1
3316 let rec flow i =
3317 if i = len then i-1 else if i = -1 then 0 else
3318 let _, l = source#getitem i in
3319 if l != curlevel then i else flow (i+incr)
3321 let active = flow m_active in
3322 let first = calcfirst m_first active in
3323 G.postRedisplay "outline updownlevel";
3324 {< m_active = active; m_first = first >}
3326 method private key1 key mask =
3327 let set1 active first qsearch =
3328 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3330 let search active pattern incr =
3331 let dosearch re =
3332 let rec loop n =
3333 if n >= 0 && n < source#getitemcount
3334 then (
3335 let s, _ = source#getitem n in
3337 (try ignore (Str.search_forward re s 0); true
3338 with Not_found -> false)
3339 then Some n
3340 else loop (n + incr)
3342 else None
3344 loop active
3347 let re = Str.regexp_case_fold pattern in
3348 dosearch re
3349 with Failure s ->
3350 state.text <- s;
3351 None
3353 let itemcount = source#getitemcount in
3354 let find start incr =
3355 let rec find i =
3356 if i = -1 || i = itemcount
3357 then -1
3358 else (
3359 if source#hasaction i
3360 then i
3361 else find (i + incr)
3364 find start
3366 let set active first =
3367 let first = bound first 0 (itemcount - fstate.maxrows) in
3368 state.text <- "";
3369 coe {< m_active = active; m_first = first >}
3371 let navigate incr =
3372 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3373 let active, first =
3374 let incr1 = if incr > 0 then 1 else -1 in
3375 if isvisible m_first m_active
3376 then
3377 let next =
3378 let next = m_active + incr in
3379 let next =
3380 if next < 0 || next >= itemcount
3381 then -1
3382 else find next incr1
3384 if next = -1 || abs (m_active - next) > fstate.maxrows
3385 then -1
3386 else next
3388 if next = -1
3389 then
3390 let first = m_first + incr in
3391 let first = bound first 0 (itemcount - 1) in
3392 let next =
3393 let next = m_active + incr in
3394 let next = bound next 0 (itemcount - 1) in
3395 find next ~-incr1
3397 let active = if next = -1 then m_active else next in
3398 active, first
3399 else
3400 let first = min next m_first in
3401 let first =
3402 if abs (next - first) > fstate.maxrows
3403 then first + incr
3404 else first
3406 next, first
3407 else
3408 let first = m_first + incr in
3409 let first = bound first 0 (itemcount - 1) in
3410 let active =
3411 let next = m_active + incr in
3412 let next = bound next 0 (itemcount - 1) in
3413 let next = find next incr1 in
3414 let active =
3415 if next = -1 || abs (m_active - first) > fstate.maxrows
3416 then (
3417 let active = if m_active = -1 then next else m_active in
3418 active
3420 else next
3422 if isvisible first active
3423 then active
3424 else -1
3426 active, first
3428 G.postRedisplay "listview navigate";
3429 set active first;
3431 match key with
3432 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3433 let incr = if key = 0x72 then -1 else 1 in
3434 let active, first =
3435 match search (m_active + incr) m_qsearch incr with
3436 | None ->
3437 state.text <- m_qsearch ^ " [not found]";
3438 m_active, m_first
3439 | Some active ->
3440 state.text <- m_qsearch;
3441 active, firstof m_first active
3443 G.postRedisplay "listview ctrl-r/s";
3444 set1 active first m_qsearch;
3446 | 0xff08 -> (* backspace *)
3447 if String.length m_qsearch = 0
3448 then coe self
3449 else (
3450 let qsearch = withoutlastutf8 m_qsearch in
3451 let len = String.length qsearch in
3452 if len = 0
3453 then (
3454 state.text <- "";
3455 G.postRedisplay "listview empty qsearch";
3456 set1 m_active m_first "";
3458 else
3459 let active, first =
3460 match search m_active qsearch ~-1 with
3461 | None ->
3462 state.text <- qsearch ^ " [not found]";
3463 m_active, m_first
3464 | Some active ->
3465 state.text <- qsearch;
3466 active, firstof m_first active
3468 G.postRedisplay "listview backspace qsearch";
3469 set1 active first qsearch
3472 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3473 let pattern = m_qsearch ^ toutf8 key in
3474 let active, first =
3475 match search m_active pattern 1 with
3476 | None ->
3477 state.text <- pattern ^ " [not found]";
3478 m_active, m_first
3479 | Some active ->
3480 state.text <- pattern;
3481 active, firstof m_first active
3483 G.postRedisplay "listview qsearch add";
3484 set1 active first pattern;
3486 | 0xff1b -> (* escape *)
3487 state.text <- "";
3488 if String.length m_qsearch = 0
3489 then (
3490 G.postRedisplay "list view escape";
3491 begin
3492 match
3493 source#exit (coe self) true m_active m_first m_pan m_qsearch
3494 with
3495 | None -> m_prev_uioh
3496 | Some uioh -> uioh
3499 else (
3500 G.postRedisplay "list view kill qsearch";
3501 source#setqsearch "";
3502 coe {< m_qsearch = "" >}
3505 | 0xff0d | 0xff8d -> (* (kp) enter *)
3506 state.text <- "";
3507 let self = {< m_qsearch = "" >} in
3508 source#setqsearch "";
3509 let opt =
3510 G.postRedisplay "listview enter";
3511 if m_active >= 0 && m_active < source#getitemcount
3512 then (
3513 source#exit (coe self) false m_active m_first m_pan "";
3515 else (
3516 source#exit (coe self) true m_active m_first m_pan "";
3519 begin match opt with
3520 | None -> m_prev_uioh
3521 | Some uioh -> uioh
3524 | 0xff9f | 0xffff -> (* (kp) delete *)
3525 coe self
3527 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3528 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3529 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3530 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3532 | 0xff53 | 0xff98 -> (* (kp) right *)
3533 state.text <- "";
3534 G.postRedisplay "listview right";
3535 coe {< m_pan = m_pan - 1 >}
3537 | 0xff51 | 0xff96 -> (* (kp) left *)
3538 state.text <- "";
3539 G.postRedisplay "listview left";
3540 coe {< m_pan = m_pan + 1 >}
3542 | 0xff50 | 0xff95 -> (* (kp) home *)
3543 let active = find 0 1 in
3544 G.postRedisplay "listview home";
3545 set active 0;
3547 | 0xff57 | 0xff9c -> (* (kp) end *)
3548 let first = max 0 (itemcount - fstate.maxrows) in
3549 let active = find (itemcount - 1) ~-1 in
3550 G.postRedisplay "listview end";
3551 set active first;
3553 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3554 coe self
3556 | _ ->
3557 dolog "listview unknown key %#x" key; coe self
3559 method key key mask =
3560 match state.mode with
3561 | Textentry te -> textentrykeyboard key mask te; coe self
3562 | _ -> self#key1 key mask
3564 method button button down x y _ =
3565 let opt =
3566 match button with
3567 | 1 when x > state.winw - conf.scrollbw ->
3568 G.postRedisplay "listview scroll";
3569 if down
3570 then
3571 let _, position, sh = self#scrollph in
3572 if y > truncate position && y < truncate (position +. sh)
3573 then (
3574 state.mstate <- Mscrolly;
3575 Some (coe self)
3577 else
3578 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3579 let first = truncate (s *. float source#getitemcount) in
3580 let first = min source#getitemcount first in
3581 Some (coe {< m_first = first; m_active = first >})
3582 else (
3583 state.mstate <- Mnone;
3584 Some (coe self);
3586 | 1 when not down ->
3587 begin match self#elemunder y with
3588 | Some n ->
3589 G.postRedisplay "listview click";
3590 source#exit
3591 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3592 | _ ->
3593 Some (coe self)
3595 | n when (n == 4 || n == 5) && not down ->
3596 let len = source#getitemcount in
3597 let first =
3598 if n = 5 && m_first + fstate.maxrows >= len
3599 then
3600 m_first
3601 else
3602 let first = m_first + (if n == 4 then -1 else 1) in
3603 bound first 0 (len - 1)
3605 G.postRedisplay "listview wheel";
3606 Some (coe {< m_first = first >})
3607 | n when (n = 6 || n = 7) && not down ->
3608 let inc = m_first + (if n = 7 then -1 else 1) in
3609 G.postRedisplay "listview hwheel";
3610 Some (coe {< m_pan = m_pan + inc >})
3611 | _ ->
3612 Some (coe self)
3614 match opt with
3615 | None -> m_prev_uioh
3616 | Some uioh -> uioh
3618 method motion _ y =
3619 match state.mstate with
3620 | Mscrolly ->
3621 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3622 let first = truncate (s *. float source#getitemcount) in
3623 let first = min source#getitemcount first in
3624 G.postRedisplay "listview motion";
3625 coe {< m_first = first; m_active = first >}
3626 | _ -> coe self
3628 method pmotion x y =
3629 if x < state.winw - conf.scrollbw
3630 then
3631 let n =
3632 match self#elemunder y with
3633 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3634 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3636 let o =
3637 if n != m_active
3638 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3639 else self
3641 coe o
3642 else (
3643 Wsi.setcursor Wsi.CURSOR_INHERIT;
3644 coe self
3647 method infochanged _ = ()
3649 method scrollpw = (0, 0.0, 0.0)
3650 method scrollph =
3651 let nfs = fstate.fontsize + 1 in
3652 let y = m_first * nfs in
3653 let itemcount = source#getitemcount in
3654 let maxi = max 0 (itemcount - fstate.maxrows) in
3655 let maxy = maxi * nfs in
3656 let p, h = scrollph y maxy in
3657 conf.scrollbw, p, h
3659 method modehash = modehash
3660 end;;
3662 class outlinelistview ~source =
3663 object (self)
3664 inherit listview
3665 ~source:(source :> lvsource)
3666 ~trusted:false
3667 ~modehash:(findkeyhash conf "outline")
3668 as super
3670 method key key mask =
3671 let calcfirst first active =
3672 if active > first
3673 then
3674 let rows = active - first in
3675 let maxrows =
3676 if String.length state.text = 0
3677 then fstate.maxrows
3678 else fstate.maxrows - 2
3680 if rows > maxrows then active - maxrows else first
3681 else active
3683 let navigate incr =
3684 let active = m_active + incr in
3685 let active = bound active 0 (source#getitemcount - 1) in
3686 let first = calcfirst m_first active in
3687 G.postRedisplay "outline navigate";
3688 coe {< m_active = active; m_first = first >}
3690 let ctrl = Wsi.withctrl mask in
3691 match key with
3692 | 110 when ctrl -> (* ctrl-n *)
3693 source#narrow m_qsearch;
3694 G.postRedisplay "outline ctrl-n";
3695 coe {< m_first = 0; m_active = 0 >}
3697 | 117 when ctrl -> (* ctrl-u *)
3698 source#denarrow;
3699 G.postRedisplay "outline ctrl-u";
3700 state.text <- "";
3701 coe {< m_first = 0; m_active = 0 >}
3703 | 108 when ctrl -> (* ctrl-l *)
3704 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3705 G.postRedisplay "outline ctrl-l";
3706 coe {< m_first = first >}
3708 | 0xff9f | 0xffff -> (* (kp) delete *)
3709 source#remove m_active;
3710 G.postRedisplay "outline delete";
3711 let active = max 0 (m_active-1) in
3712 coe {< m_first = firstof m_first active;
3713 m_active = active >}
3715 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3716 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3717 | 0xff55 | 0xff9a -> (* (kp) prior *)
3718 navigate ~-(fstate.maxrows)
3719 | 0xff56 | 0xff9b -> (* (kp) next *)
3720 navigate fstate.maxrows
3722 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3723 let o =
3724 if ctrl
3725 then (
3726 G.postRedisplay "outline ctrl right";
3727 {< m_pan = m_pan + 1 >}
3729 else self#updownlevel 1
3731 coe o
3733 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3734 let o =
3735 if ctrl
3736 then (
3737 G.postRedisplay "outline ctrl left";
3738 {< m_pan = m_pan - 1 >}
3740 else self#updownlevel ~-1
3742 coe o
3744 | 0xff50 | 0xff95 -> (* (kp) home *)
3745 G.postRedisplay "outline home";
3746 coe {< m_first = 0; m_active = 0 >}
3748 | 0xff57 | 0xff9c -> (* (kp) end *)
3749 let active = source#getitemcount - 1 in
3750 let first = max 0 (active - fstate.maxrows) in
3751 G.postRedisplay "outline end";
3752 coe {< m_active = active; m_first = first >}
3754 | _ -> super#key key mask
3757 let outlinesource usebookmarks =
3758 let empty = [||] in
3759 (object
3760 inherit lvsourcebase
3761 val mutable m_items = empty
3762 val mutable m_orig_items = empty
3763 val mutable m_prev_items = empty
3764 val mutable m_narrow_pattern = ""
3765 val mutable m_hadremovals = false
3767 method getitemcount =
3768 Array.length m_items + (if m_hadremovals then 1 else 0)
3770 method getitem n =
3771 if n == Array.length m_items && m_hadremovals
3772 then
3773 ("[Confirm removal]", 0)
3774 else
3775 let s, n, _ = m_items.(n) in
3776 (s, n)
3778 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3779 ignore (uioh, first, qsearch);
3780 let confrimremoval = m_hadremovals && active = Array.length m_items in
3781 let items =
3782 if String.length m_narrow_pattern = 0
3783 then m_orig_items
3784 else m_items
3786 if not cancel
3787 then (
3788 if not confrimremoval
3789 then(
3790 let _, _, anchor = m_items.(active) in
3791 gotoghyll (getanchory anchor);
3792 m_items <- items;
3794 else (
3795 state.bookmarks <- Array.to_list m_items;
3796 m_orig_items <- m_items;
3799 else m_items <- items;
3800 m_pan <- pan;
3801 None
3803 method hasaction _ = true
3805 method greetmsg =
3806 if Array.length m_items != Array.length m_orig_items
3807 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3808 else ""
3810 method narrow pattern =
3811 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3812 match reopt with
3813 | None -> ()
3814 | Some re ->
3815 let rec loop accu n =
3816 if n = -1
3817 then (
3818 m_narrow_pattern <- pattern;
3819 m_items <- Array.of_list accu
3821 else
3822 let (s, _, _) as o = m_items.(n) in
3823 let accu =
3824 if (try ignore (Str.search_forward re s 0); true
3825 with Not_found -> false)
3826 then o :: accu
3827 else accu
3829 loop accu (n-1)
3831 loop [] (Array.length m_items - 1)
3833 method denarrow =
3834 m_orig_items <- (
3835 if usebookmarks
3836 then Array.of_list state.bookmarks
3837 else state.outlines
3839 m_items <- m_orig_items
3841 method remove m =
3842 if usebookmarks
3843 then
3844 if m >= 0 && m < Array.length m_items
3845 then (
3846 m_hadremovals <- true;
3847 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3848 let n = if n >= m then n+1 else n in
3849 m_items.(n)
3853 method reset anchor items =
3854 m_hadremovals <- false;
3855 if m_orig_items == empty || m_prev_items != items
3856 then (
3857 m_orig_items <- items;
3858 if String.length m_narrow_pattern = 0
3859 then m_items <- items;
3861 m_prev_items <- items;
3862 let rely = getanchory anchor in
3863 let active =
3864 let rec loop n best bestd =
3865 if n = Array.length m_items
3866 then best
3867 else
3868 let (_, _, anchor) = m_items.(n) in
3869 let orely = getanchory anchor in
3870 let d = abs (orely - rely) in
3871 if d < bestd
3872 then loop (n+1) n d
3873 else loop (n+1) best bestd
3875 loop 0 ~-1 max_int
3877 m_active <- active;
3878 m_first <- firstof m_first active
3879 end)
3882 let enterselector usebookmarks =
3883 let source = outlinesource usebookmarks in
3884 fun errmsg ->
3885 let outlines =
3886 if usebookmarks
3887 then Array.of_list state.bookmarks
3888 else state.outlines
3890 if Array.length outlines = 0
3891 then (
3892 showtext ' ' errmsg;
3894 else (
3895 state.text <- source#greetmsg;
3896 Wsi.setcursor Wsi.CURSOR_INHERIT;
3897 let anchor = getanchor () in
3898 source#reset anchor outlines;
3899 state.uioh <- coe (new outlinelistview ~source);
3900 G.postRedisplay "enter selector";
3904 let enteroutlinemode =
3905 let f = enterselector false in
3906 fun ()-> f "Document has no outline";
3909 let enterbookmarkmode =
3910 let f = enterselector true in
3911 fun () -> f "Document has no bookmarks (yet)";
3914 let color_of_string s =
3915 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3916 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3920 let color_to_string (r, g, b) =
3921 let r = truncate (r *. 256.0)
3922 and g = truncate (g *. 256.0)
3923 and b = truncate (b *. 256.0) in
3924 Printf.sprintf "%d/%d/%d" r g b
3927 let irect_of_string s =
3928 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3931 let irect_to_string (x0,y0,x1,y1) =
3932 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3935 let makecheckers () =
3936 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3937 following to say:
3938 converted by Issac Trotts. July 25, 2002 *)
3939 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3940 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3941 let id = GlTex.gen_texture () in
3942 GlTex.bind_texture `texture_2d id;
3943 GlPix.store (`unpack_alignment 1);
3944 GlTex.image2d image;
3945 List.iter (GlTex.parameter ~target:`texture_2d)
3946 [ `mag_filter `nearest; `min_filter `nearest ];
3950 let setcheckers enabled =
3951 match state.texid with
3952 | None ->
3953 if enabled then state.texid <- Some (makecheckers ())
3955 | Some texid ->
3956 if not enabled
3957 then (
3958 GlTex.delete_texture texid;
3959 state.texid <- None;
3963 let int_of_string_with_suffix s =
3964 let l = String.length s in
3965 let s1, shift =
3966 if l > 1
3967 then
3968 let suffix = Char.lowercase s.[l-1] in
3969 match suffix with
3970 | 'k' -> String.sub s 0 (l-1), 10
3971 | 'm' -> String.sub s 0 (l-1), 20
3972 | 'g' -> String.sub s 0 (l-1), 30
3973 | _ -> s, 0
3974 else s, 0
3976 let n = int_of_string s1 in
3977 let m = n lsl shift in
3978 if m < 0 || m < n
3979 then raise (Failure "value too large")
3980 else m
3983 let string_with_suffix_of_int n =
3984 if n = 0
3985 then "0"
3986 else
3987 let n, s =
3988 if n land ((1 lsl 30) - 1) = 0
3989 then n lsr 30, "G"
3990 else (
3991 if n land ((1 lsl 20) - 1) = 0
3992 then n lsr 20, "M"
3993 else (
3994 if n land ((1 lsl 10) - 1) = 0
3995 then n lsr 10, "K"
3996 else n, ""
4000 let rec loop s n =
4001 let h = n mod 1000 in
4002 let n = n / 1000 in
4003 if n = 0
4004 then string_of_int h ^ s
4005 else (
4006 let s = Printf.sprintf "_%03d%s" h s in
4007 loop s n
4010 loop "" n ^ s;
4013 let defghyllscroll = (40, 8, 32);;
4014 let ghyllscroll_of_string s =
4015 let (n, a, b) as nab =
4016 if s = "default"
4017 then defghyllscroll
4018 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4020 if n <= a || n <= b || a >= b
4021 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4022 nab;
4025 let ghyllscroll_to_string ((n, a, b) as nab) =
4026 if nab = defghyllscroll
4027 then "default"
4028 else Printf.sprintf "%d,%d,%d" n a b;
4031 let describe_location () =
4032 let fn = page_of_y state.y in
4033 let ln = page_of_y (state.y + state.winh - state.hscrollh) in
4034 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4035 let percent =
4036 if maxy <= 0
4037 then 100.
4038 else (100. *. (float state.y /. float maxy))
4040 if fn = ln
4041 then
4042 Printf.sprintf "page %d of %d [%.2f%%]"
4043 (fn+1) state.pagecount percent
4044 else
4045 Printf.sprintf
4046 "pages %d-%d of %d [%.2f%%]"
4047 (fn+1) (ln+1) state.pagecount percent
4050 let setpresentationmode v =
4051 let n = page_of_y state.y in
4052 state.anchor <- (n, 0.0, 1.0);
4053 conf.presentation <- v;
4054 if conf.presentation
4055 then (
4056 if not conf.scrollbarinpm
4057 then state.scrollw <- 0;
4059 else state.scrollw <- conf.scrollbw;
4060 represent ();
4063 let enterinfomode =
4064 let btos b = if b then "\xe2\x88\x9a" else "" in
4065 let showextended = ref false in
4066 let leave mode = function
4067 | Confirm -> state.mode <- mode
4068 | Cancel -> state.mode <- mode in
4069 let src =
4070 (object
4071 val mutable m_first_time = true
4072 val mutable m_l = []
4073 val mutable m_a = [||]
4074 val mutable m_prev_uioh = nouioh
4075 val mutable m_prev_mode = View
4077 inherit lvsourcebase
4079 method reset prev_mode prev_uioh =
4080 m_a <- Array.of_list (List.rev m_l);
4081 m_l <- [];
4082 m_prev_mode <- prev_mode;
4083 m_prev_uioh <- prev_uioh;
4084 if m_first_time
4085 then (
4086 let rec loop n =
4087 if n >= Array.length m_a
4088 then ()
4089 else
4090 match m_a.(n) with
4091 | _, _, _, Action _ -> m_active <- n
4092 | _ -> loop (n+1)
4094 loop 0;
4095 m_first_time <- false;
4098 method int name get set =
4099 m_l <-
4100 (name, `int get, 1, Action (
4101 fun u ->
4102 let ondone s =
4103 try set (int_of_string s)
4104 with exn ->
4105 state.text <- Printf.sprintf "bad integer `%s': %s"
4106 s (exntos exn)
4108 state.text <- "";
4109 let te = name ^ ": ", "", None, intentry, ondone, true in
4110 state.mode <- Textentry (te, leave m_prev_mode);
4112 )) :: m_l
4114 method int_with_suffix name get set =
4115 m_l <-
4116 (name, `intws get, 1, Action (
4117 fun u ->
4118 let ondone s =
4119 try set (int_of_string_with_suffix s)
4120 with exn ->
4121 state.text <- Printf.sprintf "bad integer `%s': %s"
4122 s (exntos exn)
4124 state.text <- "";
4125 let te =
4126 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4128 state.mode <- Textentry (te, leave m_prev_mode);
4130 )) :: m_l
4132 method bool ?(offset=1) ?(btos=btos) name get set =
4133 m_l <-
4134 (name, `bool (btos, get), offset, Action (
4135 fun u ->
4136 let v = get () in
4137 set (not v);
4139 )) :: m_l
4141 method color name get set =
4142 m_l <-
4143 (name, `color get, 1, Action (
4144 fun u ->
4145 let invalid = (nan, nan, nan) in
4146 let ondone s =
4147 let c =
4148 try color_of_string s
4149 with exn ->
4150 state.text <- Printf.sprintf "bad color `%s': %s"
4151 s (exntos exn);
4152 invalid
4154 if c <> invalid
4155 then set c;
4157 let te = name ^ ": ", "", None, textentry, ondone, true in
4158 state.text <- color_to_string (get ());
4159 state.mode <- Textentry (te, leave m_prev_mode);
4161 )) :: m_l
4163 method string name get set =
4164 m_l <-
4165 (name, `string get, 1, Action (
4166 fun u ->
4167 let ondone s = set s in
4168 let te = name ^ ": ", "", None, textentry, ondone, true in
4169 state.mode <- Textentry (te, leave m_prev_mode);
4171 )) :: m_l
4173 method colorspace name get set =
4174 m_l <-
4175 (name, `string get, 1, Action (
4176 fun _ ->
4177 let source =
4178 let vals = [| "rgb"; "bgr"; "gray" |] in
4179 (object
4180 inherit lvsourcebase
4182 initializer
4183 m_active <- int_of_colorspace conf.colorspace;
4184 m_first <- 0;
4186 method getitemcount = Array.length vals
4187 method getitem n = (vals.(n), 0)
4188 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4189 ignore (uioh, first, pan, qsearch);
4190 if not cancel then set active;
4191 None
4192 method hasaction _ = true
4193 end)
4195 state.text <- "";
4196 let modehash = findkeyhash conf "info" in
4197 coe (new listview ~source ~trusted:true ~modehash)
4198 )) :: m_l
4200 method caption s offset =
4201 m_l <- (s, `empty, offset, Noaction) :: m_l
4203 method caption2 s f offset =
4204 m_l <- (s, `string f, offset, Noaction) :: m_l
4206 method getitemcount = Array.length m_a
4208 method getitem n =
4209 let tostr = function
4210 | `int f -> string_of_int (f ())
4211 | `intws f -> string_with_suffix_of_int (f ())
4212 | `string f -> f ()
4213 | `color f -> color_to_string (f ())
4214 | `bool (btos, f) -> btos (f ())
4215 | `empty -> ""
4217 let name, t, offset, _ = m_a.(n) in
4218 ((let s = tostr t in
4219 if String.length s > 0
4220 then Printf.sprintf "%s\t%s" name s
4221 else name),
4222 offset)
4224 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4225 let uiohopt =
4226 if not cancel
4227 then (
4228 m_qsearch <- qsearch;
4229 let uioh =
4230 match m_a.(active) with
4231 | _, _, _, Action f -> f uioh
4232 | _ -> uioh
4234 Some uioh
4236 else None
4238 m_active <- active;
4239 m_first <- first;
4240 m_pan <- pan;
4241 uiohopt
4243 method hasaction n =
4244 match m_a.(n) with
4245 | _, _, _, Action _ -> true
4246 | _ -> false
4247 end)
4249 let rec fillsrc prevmode prevuioh =
4250 let sep () = src#caption "" 0 in
4251 let colorp name get set =
4252 src#string name
4253 (fun () -> color_to_string (get ()))
4254 (fun v ->
4256 let c = color_of_string v in
4257 set c
4258 with exn ->
4259 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4262 let oldmode = state.mode in
4263 let birdseye = isbirdseye state.mode in
4265 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4267 src#bool "presentation mode"
4268 (fun () -> conf.presentation)
4269 (fun v -> setpresentationmode v);
4271 src#bool "ignore case in searches"
4272 (fun () -> conf.icase)
4273 (fun v -> conf.icase <- v);
4275 src#bool "preload"
4276 (fun () -> conf.preload)
4277 (fun v -> conf.preload <- v);
4279 src#bool "highlight links"
4280 (fun () -> conf.hlinks)
4281 (fun v -> conf.hlinks <- v);
4283 src#bool "under info"
4284 (fun () -> conf.underinfo)
4285 (fun v -> conf.underinfo <- v);
4287 src#bool "persistent bookmarks"
4288 (fun () -> conf.savebmarks)
4289 (fun v -> conf.savebmarks <- v);
4291 src#bool "proportional display"
4292 (fun () -> conf.proportional)
4293 (fun v -> reqlayout conf.angle v);
4295 src#bool "trim margins"
4296 (fun () -> conf.trimmargins)
4297 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4299 src#bool "persistent location"
4300 (fun () -> conf.jumpback)
4301 (fun v -> conf.jumpback <- v);
4303 sep ();
4304 src#int "inter-page space"
4305 (fun () -> conf.interpagespace)
4306 (fun n ->
4307 conf.interpagespace <- n;
4308 docolumns conf.columns;
4309 let pageno, py =
4310 match state.layout with
4311 | [] -> 0, 0
4312 | l :: _ ->
4313 l.pageno, l.pagey
4315 state.maxy <- calcheight ();
4316 let y = getpagey pageno in
4317 gotoy (y + py)
4320 src#int "page bias"
4321 (fun () -> conf.pagebias)
4322 (fun v -> conf.pagebias <- v);
4324 src#int "scroll step"
4325 (fun () -> conf.scrollstep)
4326 (fun n -> conf.scrollstep <- n);
4328 src#int "horizontal scroll step"
4329 (fun () -> conf.hscrollstep)
4330 (fun v -> conf.hscrollstep <- v);
4332 src#int "auto scroll step"
4333 (fun () ->
4334 match state.autoscroll with
4335 | Some step -> step
4336 | _ -> conf.autoscrollstep)
4337 (fun n ->
4338 if state.autoscroll <> None
4339 then state.autoscroll <- Some n;
4340 conf.autoscrollstep <- n);
4342 src#int "zoom"
4343 (fun () -> truncate (conf.zoom *. 100.))
4344 (fun v -> setzoom ((float v) /. 100.));
4346 src#int "rotation"
4347 (fun () -> conf.angle)
4348 (fun v -> reqlayout v conf.proportional);
4350 src#int "scroll bar width"
4351 (fun () -> state.scrollw)
4352 (fun v ->
4353 state.scrollw <- v;
4354 conf.scrollbw <- v;
4355 reshape state.winw state.winh;
4358 src#int "scroll handle height"
4359 (fun () -> conf.scrollh)
4360 (fun v -> conf.scrollh <- v;);
4362 src#int "thumbnail width"
4363 (fun () -> conf.thumbw)
4364 (fun v ->
4365 conf.thumbw <- min 4096 v;
4366 match oldmode with
4367 | Birdseye beye ->
4368 leavebirdseye beye false;
4369 enterbirdseye ()
4370 | _ -> ()
4373 let mode = state.mode in
4374 src#string "columns"
4375 (fun () ->
4376 match conf.columns with
4377 | Csingle _ -> "1"
4378 | Cmulti (multi, _) -> multicolumns_to_string multi
4379 | Csplit (count, _) -> "-" ^ string_of_int count
4381 (fun v ->
4382 let n, a, b = multicolumns_of_string v in
4383 setcolumns mode n a b);
4385 sep ();
4386 src#caption "Presentation mode" 0;
4387 src#bool "scrollbar visible"
4388 (fun () -> conf.scrollbarinpm)
4389 (fun v ->
4390 if v != conf.scrollbarinpm
4391 then (
4392 conf.scrollbarinpm <- v;
4393 if conf.presentation
4394 then (
4395 state.scrollw <- if v then conf.scrollbw else 0;
4396 reshape state.winw state.winh;
4401 sep ();
4402 src#caption "Pixmap cache" 0;
4403 src#int_with_suffix "size (advisory)"
4404 (fun () -> conf.memlimit)
4405 (fun v -> conf.memlimit <- v);
4407 src#caption2 "used"
4408 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4409 (string_with_suffix_of_int state.memused)
4410 (Hashtbl.length state.tilemap)) 1;
4412 sep ();
4413 src#caption "Layout" 0;
4414 src#caption2 "Dimension"
4415 (fun () ->
4416 Printf.sprintf "%dx%d (virtual %dx%d)"
4417 state.winw state.winh
4418 state.w state.maxy)
4420 if conf.debug
4421 then
4422 src#caption2 "Position" (fun () ->
4423 Printf.sprintf "%dx%d" state.x state.y
4425 else
4426 src#caption2 "Visible" (fun () -> describe_location ()) 1
4429 sep ();
4430 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4431 "Save these parameters as global defaults at exit"
4432 (fun () -> conf.bedefault)
4433 (fun v -> conf.bedefault <- v)
4436 sep ();
4437 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4438 src#bool ~offset:0 ~btos "Extended parameters"
4439 (fun () -> !showextended)
4440 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4441 if !showextended
4442 then (
4443 src#bool "checkers"
4444 (fun () -> conf.checkers)
4445 (fun v -> conf.checkers <- v; setcheckers v);
4446 src#bool "update cursor"
4447 (fun () -> conf.updatecurs)
4448 (fun v -> conf.updatecurs <- v);
4449 src#bool "verbose"
4450 (fun () -> conf.verbose)
4451 (fun v -> conf.verbose <- v);
4452 src#bool "invert colors"
4453 (fun () -> conf.invert)
4454 (fun v -> conf.invert <- v);
4455 src#bool "max fit"
4456 (fun () -> conf.maxhfit)
4457 (fun v -> conf.maxhfit <- v);
4458 src#bool "redirect stderr"
4459 (fun () -> conf.redirectstderr)
4460 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4461 src#string "uri launcher"
4462 (fun () -> conf.urilauncher)
4463 (fun v -> conf.urilauncher <- v);
4464 src#string "path launcher"
4465 (fun () -> conf.pathlauncher)
4466 (fun v -> conf.pathlauncher <- v);
4467 src#string "tile size"
4468 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4469 (fun v ->
4471 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4472 conf.tilew <- max 64 w;
4473 conf.tileh <- max 64 h;
4474 flushtiles ();
4475 with exn ->
4476 state.text <- Printf.sprintf "bad tile size `%s': %s"
4477 v (exntos exn)
4479 src#int "texture count"
4480 (fun () -> conf.texcount)
4481 (fun v ->
4482 if realloctexts v
4483 then conf.texcount <- v
4484 else showtext '!' " Failed to set texture count please retry later"
4486 src#int "slice height"
4487 (fun () -> conf.sliceheight)
4488 (fun v ->
4489 conf.sliceheight <- v;
4490 wcmd "sliceh %d" conf.sliceheight;
4492 src#int "anti-aliasing level"
4493 (fun () -> conf.aalevel)
4494 (fun v ->
4495 conf.aalevel <- bound v 0 8;
4496 state.anchor <- getanchor ();
4497 opendoc state.path state.password;
4499 src#string "page scroll scaling factor"
4500 (fun () -> string_of_float conf.pgscale)
4501 (fun v ->
4503 let s = float_of_string v in
4504 conf.pgscale <- s
4505 with exn ->
4506 state.text <- Printf.sprintf
4507 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4510 src#int "ui font size"
4511 (fun () -> fstate.fontsize)
4512 (fun v -> setfontsize (bound v 5 100));
4513 src#int "hint font size"
4514 (fun () -> conf.hfsize)
4515 (fun v -> conf.hfsize <- bound v 5 100);
4516 colorp "background color"
4517 (fun () -> conf.bgcolor)
4518 (fun v -> conf.bgcolor <- v);
4519 src#bool "crop hack"
4520 (fun () -> conf.crophack)
4521 (fun v -> conf.crophack <- v);
4522 src#string "trim fuzz"
4523 (fun () -> irect_to_string conf.trimfuzz)
4524 (fun v ->
4526 conf.trimfuzz <- irect_of_string v;
4527 if conf.trimmargins
4528 then settrim true conf.trimfuzz;
4529 with exn ->
4530 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4532 src#string "throttle"
4533 (fun () ->
4534 match conf.maxwait with
4535 | None -> "show place holder if page is not ready"
4536 | Some time ->
4537 if time = infinity
4538 then "wait for page to fully render"
4539 else
4540 "wait " ^ string_of_float time
4541 ^ " seconds before showing placeholder"
4543 (fun v ->
4545 let f = float_of_string v in
4546 if f <= 0.0
4547 then conf.maxwait <- None
4548 else conf.maxwait <- Some f
4549 with exn ->
4550 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4552 src#string "ghyll scroll"
4553 (fun () ->
4554 match conf.ghyllscroll with
4555 | None -> ""
4556 | Some nab -> ghyllscroll_to_string nab
4558 (fun v ->
4560 let gs =
4561 if String.length v = 0
4562 then None
4563 else Some (ghyllscroll_of_string v)
4565 conf.ghyllscroll <- gs
4566 with exn ->
4567 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4569 src#string "selection command"
4570 (fun () -> conf.selcmd)
4571 (fun v -> conf.selcmd <- v);
4572 src#string "synctex command"
4573 (fun () -> conf.stcmd)
4574 (fun v -> conf.stcmd <- v);
4575 src#colorspace "color space"
4576 (fun () -> colorspace_to_string conf.colorspace)
4577 (fun v ->
4578 conf.colorspace <- colorspace_of_int v;
4579 wcmd "cs %d" v;
4580 load state.layout;
4582 if pbousable ()
4583 then
4584 src#bool "use PBO"
4585 (fun () -> conf.usepbo)
4586 (fun v -> conf.usepbo <- v);
4587 src#bool "mouse wheel scrolls pages"
4588 (fun () -> conf.wheelbypage)
4589 (fun v -> conf.wheelbypage <- v);
4592 sep ();
4593 src#caption "Document" 0;
4594 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4595 src#caption2 "Pages"
4596 (fun () -> string_of_int state.pagecount) 1;
4597 src#caption2 "Dimensions"
4598 (fun () -> string_of_int (List.length state.pdims)) 1;
4599 if conf.trimmargins
4600 then (
4601 sep ();
4602 src#caption "Trimmed margins" 0;
4603 src#caption2 "Dimensions"
4604 (fun () -> string_of_int (List.length state.pdims)) 1;
4607 sep ();
4608 src#caption "OpenGL" 0;
4609 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4610 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4611 src#reset prevmode prevuioh;
4613 fun () ->
4614 state.text <- "";
4615 let prevmode = state.mode
4616 and prevuioh = state.uioh in
4617 fillsrc prevmode prevuioh;
4618 let source = (src :> lvsource) in
4619 let modehash = findkeyhash conf "info" in
4620 state.uioh <- coe (object (self)
4621 inherit listview ~source ~trusted:true ~modehash as super
4622 val mutable m_prevmemused = 0
4623 method infochanged = function
4624 | Memused ->
4625 if m_prevmemused != state.memused
4626 then (
4627 m_prevmemused <- state.memused;
4628 G.postRedisplay "memusedchanged";
4630 | Pdim -> G.postRedisplay "pdimchanged"
4631 | Docinfo -> fillsrc prevmode prevuioh
4633 method key key mask =
4634 if not (Wsi.withctrl mask)
4635 then
4636 match key with
4637 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4638 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4639 | _ -> super#key key mask
4640 else super#key key mask
4641 end);
4642 G.postRedisplay "info";
4645 let enterhelpmode =
4646 let source =
4647 (object
4648 inherit lvsourcebase
4649 method getitemcount = Array.length state.help
4650 method getitem n =
4651 let s, l, _ = state.help.(n) in
4652 (s, l)
4654 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4655 let optuioh =
4656 if not cancel
4657 then (
4658 m_qsearch <- qsearch;
4659 match state.help.(active) with
4660 | _, _, Action f -> Some (f uioh)
4661 | _ -> Some (uioh)
4663 else None
4665 m_active <- active;
4666 m_first <- first;
4667 m_pan <- pan;
4668 optuioh
4670 method hasaction n =
4671 match state.help.(n) with
4672 | _, _, Action _ -> true
4673 | _ -> false
4675 initializer
4676 m_active <- -1
4677 end)
4678 in fun () ->
4679 let modehash = findkeyhash conf "help" in
4680 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4681 G.postRedisplay "help";
4684 let entermsgsmode =
4685 let msgsource =
4686 let re = Str.regexp "[\r\n]" in
4687 (object
4688 inherit lvsourcebase
4689 val mutable m_items = [||]
4691 method getitemcount = 1 + Array.length m_items
4693 method getitem n =
4694 if n = 0
4695 then "[Clear]", 0
4696 else m_items.(n-1), 0
4698 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4699 ignore uioh;
4700 if not cancel
4701 then (
4702 if active = 0
4703 then Buffer.clear state.errmsgs;
4704 m_qsearch <- qsearch;
4706 m_active <- active;
4707 m_first <- first;
4708 m_pan <- pan;
4709 None
4711 method hasaction n =
4712 n = 0
4714 method reset =
4715 state.newerrmsgs <- false;
4716 let l = Str.split re (Buffer.contents state.errmsgs) in
4717 m_items <- Array.of_list l
4719 initializer
4720 m_active <- 0
4721 end)
4722 in fun () ->
4723 state.text <- "";
4724 msgsource#reset;
4725 let source = (msgsource :> lvsource) in
4726 let modehash = findkeyhash conf "listview" in
4727 state.uioh <- coe (object
4728 inherit listview ~source ~trusted:false ~modehash as super
4729 method display =
4730 if state.newerrmsgs
4731 then msgsource#reset;
4732 super#display
4733 end);
4734 G.postRedisplay "msgs";
4737 let quickbookmark ?title () =
4738 match state.layout with
4739 | [] -> ()
4740 | l :: _ ->
4741 let title =
4742 match title with
4743 | None ->
4744 let sec = Unix.gettimeofday () in
4745 let tm = Unix.localtime sec in
4746 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4747 (l.pageno+1)
4748 tm.Unix.tm_mday
4749 tm.Unix.tm_mon
4750 (tm.Unix.tm_year + 1900)
4751 tm.Unix.tm_hour
4752 tm.Unix.tm_min
4753 | Some title -> title
4755 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4758 let doreshape w h =
4759 Wsi.reshape w h;
4762 let setautoscrollspeed step goingdown =
4763 let incr = max 1 ((abs step) / 2) in
4764 let incr = if goingdown then incr else -incr in
4765 let astep = step + incr in
4766 state.autoscroll <- Some astep;
4769 let gotounder = function
4770 | Ulinkgoto (pageno, top) ->
4771 if pageno >= 0
4772 then (
4773 addnav ();
4774 gotopage1 pageno top;
4777 | Ulinkuri s ->
4778 gotouri s
4780 | Uremote (filename, pageno) ->
4781 let path =
4782 if Sys.file_exists filename
4783 then filename
4784 else
4785 let dir = Filename.dirname state.path in
4786 let path = Filename.concat dir filename in
4787 if Sys.file_exists path
4788 then path
4789 else ""
4791 if String.length path > 0
4792 then (
4793 let anchor = getanchor () in
4794 let ranchor = state.path, state.password, anchor in
4795 state.anchor <- (pageno, 0.0, 0.0);
4796 state.ranchors <- ranchor :: state.ranchors;
4797 opendoc path "";
4799 else showtext '!' ("Could not find " ^ filename)
4801 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4804 let canpan () =
4805 match conf.columns with
4806 | Csplit _ -> true
4807 | _ -> state.x != 0 || conf.zoom > 1.0
4810 let existsinrow pageno (columns, coverA, coverB) p =
4811 let last = ((pageno - coverA) mod columns) + columns in
4812 let rec any = function
4813 | [] -> false
4814 | l :: rest ->
4815 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4816 then p l
4817 else (
4818 if not (p l)
4819 then (if l.pageno = last then false else any rest)
4820 else true
4823 any state.layout
4826 let nextpage () =
4827 match state.layout with
4828 | [] ->
4829 let pageno = page_of_y state.y in
4830 gotoghyll (getpagey (pageno+1))
4831 | l :: rest ->
4832 match conf.columns with
4833 | Csingle _ ->
4834 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4835 then
4836 let y = clamp (pgscale state.winh) in
4837 gotoghyll y
4838 else
4839 let pageno = min (l.pageno+1) (state.pagecount-1) in
4840 gotoghyll (getpagey pageno)
4841 | Cmulti ((c, _, _) as cl, _) ->
4842 if conf.presentation
4843 && (existsinrow l.pageno cl
4844 (fun l -> l.pageh > l.pagey + l.pagevh))
4845 then
4846 let y = clamp (pgscale state.winh) in
4847 gotoghyll y
4848 else
4849 let pageno = min (l.pageno+c) (state.pagecount-1) in
4850 gotoghyll (getpagey pageno)
4851 | Csplit (n, _) ->
4852 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4853 then
4854 let pagey, pageh = getpageyh l.pageno in
4855 let pagey = pagey + pageh * l.pagecol in
4856 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4857 gotoghyll (pagey + pageh + ips)
4860 let prevpage () =
4861 match state.layout with
4862 | [] ->
4863 let pageno = page_of_y state.y in
4864 gotoghyll (getpagey (pageno-1))
4865 | l :: _ ->
4866 match conf.columns with
4867 | Csingle _ ->
4868 if conf.presentation && l.pagey != 0
4869 then
4870 gotoghyll (clamp (pgscale ~-(state.winh)))
4871 else
4872 let pageno = max 0 (l.pageno-1) in
4873 gotoghyll (getpagey pageno)
4874 | Cmulti ((c, _, coverB) as cl, _) ->
4875 if conf.presentation &&
4876 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4877 then
4878 gotoghyll (clamp (pgscale ~-(state.winh)))
4879 else
4880 let decr =
4881 if l.pageno = state.pagecount - coverB
4882 then 1
4883 else c
4885 let pageno = max 0 (l.pageno-decr) in
4886 gotoghyll (getpagey pageno)
4887 | Csplit (n, _) ->
4888 let y =
4889 if l.pagecol = 0
4890 then
4891 if l.pageno = 0
4892 then l.pagey
4893 else
4894 let pageno = max 0 (l.pageno-1) in
4895 let pagey, pageh = getpageyh pageno in
4896 pagey + (n-1)*pageh
4897 else
4898 let pagey, pageh = getpageyh l.pageno in
4899 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4901 gotoghyll y
4904 let viewkeyboard key mask =
4905 let enttext te =
4906 let mode = state.mode in
4907 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4908 state.text <- "";
4909 enttext ();
4910 G.postRedisplay "view:enttext"
4912 let ctrl = Wsi.withctrl mask in
4913 let key =
4914 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4916 match key with
4917 | 81 -> (* Q *)
4918 exit 0
4920 | 0xff63 -> (* insert *)
4921 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4922 then (
4923 state.mode <- LinkNav (Ltgendir 0);
4924 gotoy state.y;
4926 else showtext '!' "Keyboard link navigation does not work under rotation"
4928 | 0xff1b | 113 -> (* escape / q *)
4929 begin match state.mstate with
4930 | Mzoomrect _ ->
4931 state.mstate <- Mnone;
4932 Wsi.setcursor Wsi.CURSOR_INHERIT;
4933 G.postRedisplay "kill zoom rect";
4934 | _ ->
4935 begin match state.mode with
4936 | LinkNav _ ->
4937 state.mode <- View;
4938 G.postRedisplay "esc leave linknav"
4939 | _ ->
4940 match state.ranchors with
4941 | [] -> raise Quit
4942 | (path, password, anchor) :: rest ->
4943 state.ranchors <- rest;
4944 state.anchor <- anchor;
4945 opendoc path password
4946 end;
4947 end;
4949 | 0xff08 -> (* backspace *)
4950 gotoghyll (getnav ~-1)
4952 | 111 -> (* o *)
4953 enteroutlinemode ()
4955 | 117 -> (* u *)
4956 state.rects <- [];
4957 state.text <- "";
4958 G.postRedisplay "dehighlight";
4960 | 47 | 63 -> (* / ? *)
4961 let ondone isforw s =
4962 cbput state.hists.pat s;
4963 state.searchpattern <- s;
4964 search s isforw
4966 let s = String.create 1 in
4967 s.[0] <- Char.chr key;
4968 enttext (s, "", Some (onhist state.hists.pat),
4969 textentry, ondone (key = 47), true)
4971 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4972 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4973 setzoom (conf.zoom +. incr)
4975 | 43 | 0xffab -> (* + *)
4976 let ondone s =
4977 let n =
4978 try int_of_string s with exc ->
4979 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4980 max_int
4982 if n != max_int
4983 then (
4984 conf.pagebias <- n;
4985 state.text <- "page bias is now " ^ string_of_int n;
4988 enttext ("page bias: ", "", None, intentry, ondone, true)
4990 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4991 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4992 setzoom (max 0.01 (conf.zoom -. decr))
4994 | 45 | 0xffad -> (* - *)
4995 let ondone msg = state.text <- msg in
4996 enttext (
4997 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4998 optentry state.mode, ondone, true
5001 | 48 when ctrl -> (* ctrl-0 *)
5002 setzoom 1.0
5004 | 49 when ctrl -> (* ctrl-1 *)
5005 let cols =
5006 match conf.columns with
5007 | Csingle _ | Cmulti _ -> 1
5008 | Csplit (n, _) -> n
5010 let zoom = zoomforh state.winw state.winh state.scrollw cols in
5011 if zoom < 1.0
5012 then setzoom zoom
5014 | 0xffc6 -> (* f9 *)
5015 togglebirdseye ()
5017 | 57 when ctrl -> (* ctrl-9 *)
5018 togglebirdseye ()
5020 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5021 when not ctrl -> (* 0..9 *)
5022 let ondone s =
5023 let n =
5024 try int_of_string s with exc ->
5025 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5028 if n >= 0
5029 then (
5030 addnav ();
5031 cbput state.hists.pag (string_of_int n);
5032 gotopage1 (n + conf.pagebias - 1) 0;
5035 let pageentry text key =
5036 match Char.unsafe_chr key with
5037 | 'g' -> TEdone text
5038 | _ -> intentry text key
5040 let text = "x" in text.[0] <- Char.chr key;
5041 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5043 | 98 -> (* b *)
5044 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5045 reshape state.winw state.winh;
5047 | 108 -> (* l *)
5048 conf.hlinks <- not conf.hlinks;
5049 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5050 G.postRedisplay "toggle highlightlinks";
5052 | 70 -> (* F *)
5053 state.glinks <- true;
5054 let mode = state.mode in
5055 state.mode <- Textentry (
5056 (":", "", None, linknentry, linkndone gotounder, false),
5057 (fun _ ->
5058 state.glinks <- false;
5059 state.mode <- mode)
5061 state.text <- "";
5062 G.postRedisplay "view:linkent(F)"
5064 | 121 -> (* y *)
5065 state.glinks <- true;
5066 let mode = state.mode in
5067 state.mode <- Textentry (
5068 (":", "", None, linknentry, linkndone (fun under ->
5069 match Ne.pipe () with
5070 | Ne.Exn exn ->
5071 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5072 | Ne.Res (r, w) ->
5073 let popened =
5074 try popen conf.selcmd [r, 0; w, -1]; true
5075 with exn ->
5076 showtext '!'
5077 (Printf.sprintf "failed to execute %s: %s"
5078 conf.selcmd (exntos exn));
5079 false
5081 let clo cap fd =
5082 Ne.clo fd (fun msg ->
5083 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5086 let s = undertext under in
5087 if popened
5088 then
5089 (try
5090 let l = String.length s in
5091 let n = tempfailureretry (Unix.write w s 0) l in
5092 if n != l
5093 then
5094 showtext '!'
5095 (Printf.sprintf
5096 "failed to write %d characters to sel pipe, wrote %d"
5099 with exn ->
5100 showtext '!'
5101 (Printf.sprintf "failed to write to sel pipe: %s"
5102 (exntos exn)
5105 else dolog "%s" s;
5106 clo "pipe/r" r;
5107 clo "pipe/w" w;
5108 ), false
5110 fun _ ->
5111 state.glinks <- false;
5112 state.mode <- mode
5114 state.text <- "";
5115 G.postRedisplay "view:linkent"
5117 | 97 -> (* a *)
5118 begin match state.autoscroll with
5119 | Some step ->
5120 conf.autoscrollstep <- step;
5121 state.autoscroll <- None
5122 | None ->
5123 if conf.autoscrollstep = 0
5124 then state.autoscroll <- Some 1
5125 else state.autoscroll <- Some conf.autoscrollstep
5128 | 112 when ctrl -> (* ctrl-p *)
5129 launchpath ()
5131 | 80 -> (* P *)
5132 setpresentationmode (not conf.presentation);
5133 showtext ' ' ("presentation mode " ^
5134 if conf.presentation then "on" else "off");
5136 | 102 -> (* f *)
5137 if List.mem Wsi.Fullscreen state.winstate
5138 then doreshape conf.cwinw conf.cwinh
5139 else Wsi.fullscreen ()
5141 | 112 | 78 -> (* p|N *)
5142 search state.searchpattern false
5144 | 110 | 0xffc0 -> (* n|F3 *)
5145 search state.searchpattern true
5147 | 116 -> (* t *)
5148 begin match state.layout with
5149 | [] -> ()
5150 | l :: _ ->
5151 gotoghyll (getpagey l.pageno)
5154 | 32 -> (* space *)
5155 nextpage ()
5157 | 0xff9f | 0xffff -> (* delete *)
5158 prevpage ()
5160 | 61 -> (* = *)
5161 showtext ' ' (describe_location ());
5163 | 119 -> (* w *)
5164 begin match state.layout with
5165 | [] -> ()
5166 | l :: _ ->
5167 doreshape (l.pagew + state.scrollw) l.pageh;
5168 G.postRedisplay "w"
5171 | 39 -> (* ' *)
5172 enterbookmarkmode ()
5174 | 104 | 0xffbe -> (* h|F1 *)
5175 enterhelpmode ()
5177 | 105 -> (* i *)
5178 enterinfomode ()
5180 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5181 entermsgsmode ()
5183 | 109 -> (* m *)
5184 let ondone s =
5185 match state.layout with
5186 | l :: _ ->
5187 if String.length s > 0
5188 then
5189 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5190 | _ -> ()
5192 enttext ("bookmark: ", "", None, textentry, ondone, true)
5194 | 126 -> (* ~ *)
5195 quickbookmark ();
5196 showtext ' ' "Quick bookmark added";
5198 | 122 -> (* z *)
5199 begin match state.layout with
5200 | l :: _ ->
5201 let rect = getpdimrect l.pagedimno in
5202 let w, h =
5203 if conf.crophack
5204 then
5205 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5206 truncate (1.2 *. (rect.(3) -. rect.(0))))
5207 else
5208 (truncate (rect.(1) -. rect.(0)),
5209 truncate (rect.(3) -. rect.(0)))
5211 let w = truncate ((float w)*.conf.zoom)
5212 and h = truncate ((float h)*.conf.zoom) in
5213 if w != 0 && h != 0
5214 then (
5215 state.anchor <- getanchor ();
5216 doreshape (w + state.scrollw) (h + conf.interpagespace)
5218 G.postRedisplay "z";
5220 | [] -> ()
5223 | 50 when ctrl -> (* ctrl-2 *)
5224 let maxw = getmaxw () in
5225 if maxw > 0.0
5226 then setzoom (maxw /. float state.winw)
5228 | 60 | 62 -> (* < > *)
5229 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5231 | 91 | 93 -> (* [ ] *)
5232 conf.colorscale <-
5233 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5235 G.postRedisplay "brightness";
5237 | 99 when state.mode = View -> (* c *)
5238 let (c, a, b), z =
5239 match state.prevcolumns with
5240 | None -> (1, 0, 0), 1.0
5241 | Some (columns, z) ->
5242 let cab =
5243 match columns with
5244 | Csplit (c, _) -> -c, 0, 0
5245 | Cmulti ((c, a, b), _) -> c, a, b
5246 | Csingle _ -> 1, 0, 0
5248 cab, z
5250 setcolumns View c a b;
5251 setzoom z;
5253 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5254 setzoom state.prevzoom
5256 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5257 begin match state.autoscroll with
5258 | None ->
5259 begin match state.mode with
5260 | Birdseye beye -> upbirdseye 1 beye
5261 | _ ->
5262 if ctrl
5263 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5264 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5266 | Some n ->
5267 setautoscrollspeed n false
5270 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5271 begin match state.autoscroll with
5272 | None ->
5273 begin match state.mode with
5274 | Birdseye beye -> downbirdseye 1 beye
5275 | _ ->
5276 if ctrl
5277 then gotoy_and_clear_text (clamp (state.winh/2))
5278 else gotoy_and_clear_text (clamp conf.scrollstep)
5280 | Some n ->
5281 setautoscrollspeed n true
5284 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5285 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5286 if canpan ()
5287 then
5288 let dx =
5289 if ctrl
5290 then state.winw / 2
5291 else conf.hscrollstep
5293 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5294 state.x <- state.x + dx;
5295 gotoy_and_clear_text state.y
5296 else (
5297 state.text <- "";
5298 G.postRedisplay "lef/right"
5301 | 0xff55 | 0xff9a -> (* (kp) prior *)
5302 let y =
5303 if ctrl
5304 then
5305 match state.layout with
5306 | [] -> state.y
5307 | l :: _ -> state.y - l.pagey
5308 else
5309 clamp (pgscale (-state.winh))
5311 gotoghyll y
5313 | 0xff56 | 0xff9b -> (* (kp) next *)
5314 let y =
5315 if ctrl
5316 then
5317 match List.rev state.layout with
5318 | [] -> state.y
5319 | l :: _ -> getpagey l.pageno
5320 else
5321 clamp (pgscale state.winh)
5323 gotoghyll y
5325 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5326 gotoghyll 0
5327 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5328 gotoghyll (clamp state.maxy)
5330 | 0xff53 | 0xff98
5331 when Wsi.withalt mask -> (* alt-(kp) right *)
5332 gotoghyll (getnav 1)
5333 | 0xff51 | 0xff96
5334 when Wsi.withalt mask -> (* alt-(kp) left *)
5335 gotoghyll (getnav ~-1)
5337 | 114 -> (* r *)
5338 reload ()
5340 | 118 when conf.debug -> (* v *)
5341 state.rects <- [];
5342 List.iter (fun l ->
5343 match getopaque l.pageno with
5344 | None -> ()
5345 | Some opaque ->
5346 let x0, y0, x1, y1 = pagebbox opaque in
5347 let a,b = float x0, float y0 in
5348 let c,d = float x1, float y0 in
5349 let e,f = float x1, float y1 in
5350 let h,j = float x0, float y1 in
5351 let rect = (a,b,c,d,e,f,h,j) in
5352 debugrect rect;
5353 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5354 ) state.layout;
5355 G.postRedisplay "v";
5357 | _ ->
5358 vlog "huh? %s" (Wsi.keyname key)
5361 let linknavkeyboard key mask linknav =
5362 let getpage pageno =
5363 let rec loop = function
5364 | [] -> None
5365 | l :: _ when l.pageno = pageno -> Some l
5366 | _ :: rest -> loop rest
5367 in loop state.layout
5369 let doexact (pageno, n) =
5370 match getopaque pageno, getpage pageno with
5371 | Some opaque, Some l ->
5372 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5373 then
5374 let under = getlink opaque n in
5375 G.postRedisplay "link gotounder";
5376 gotounder under;
5377 state.mode <- View;
5378 else
5379 let opt, dir =
5380 match key with
5381 | 0xff50 -> (* home *)
5382 Some (findlink opaque LDfirst), -1
5384 | 0xff57 -> (* end *)
5385 Some (findlink opaque LDlast), 1
5387 | 0xff51 -> (* left *)
5388 Some (findlink opaque (LDleft n)), -1
5390 | 0xff53 -> (* right *)
5391 Some (findlink opaque (LDright n)), 1
5393 | 0xff52 -> (* up *)
5394 Some (findlink opaque (LDup n)), -1
5396 | 0xff54 -> (* down *)
5397 Some (findlink opaque (LDdown n)), 1
5399 | _ -> None, 0
5401 let pwl l dir =
5402 begin match findpwl l.pageno dir with
5403 | Pwlnotfound -> ()
5404 | Pwl pageno ->
5405 let notfound dir =
5406 state.mode <- LinkNav (Ltgendir dir);
5407 let y, h = getpageyh pageno in
5408 let y =
5409 if dir < 0
5410 then y + h - state.winh
5411 else y
5413 gotoy y
5415 begin match getopaque pageno, getpage pageno with
5416 | Some opaque, Some _ ->
5417 let link =
5418 let ld = if dir > 0 then LDfirst else LDlast in
5419 findlink opaque ld
5421 begin match link with
5422 | Lfound m ->
5423 showlinktype (getlink opaque m);
5424 state.mode <- LinkNav (Ltexact (pageno, m));
5425 G.postRedisplay "linknav jpage";
5426 | _ -> notfound dir
5427 end;
5428 | _ -> notfound dir
5429 end;
5430 end;
5432 begin match opt with
5433 | Some Lnotfound -> pwl l dir;
5434 | Some (Lfound m) ->
5435 if m = n
5436 then pwl l dir
5437 else (
5438 let _, y0, _, y1 = getlinkrect opaque m in
5439 if y0 < l.pagey
5440 then gotopage1 l.pageno y0
5441 else (
5442 let d = fstate.fontsize + 1 in
5443 if y1 - l.pagey > l.pagevh - d
5444 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5445 else G.postRedisplay "linknav";
5447 showlinktype (getlink opaque m);
5448 state.mode <- LinkNav (Ltexact (l.pageno, m));
5451 | None -> viewkeyboard key mask
5452 end;
5453 | _ -> viewkeyboard key mask
5455 if key = 0xff63
5456 then (
5457 state.mode <- View;
5458 G.postRedisplay "leave linknav"
5460 else
5461 match linknav with
5462 | Ltgendir _ -> viewkeyboard key mask
5463 | Ltexact exact -> doexact exact
5466 let keyboard key mask =
5467 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5468 then wcmd "interrupt"
5469 else state.uioh <- state.uioh#key key mask
5472 let birdseyekeyboard key mask
5473 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5474 let incr =
5475 match conf.columns with
5476 | Csingle _ -> 1
5477 | Cmulti ((c, _, _), _) -> c
5478 | Csplit _ -> failwith "bird's eye split mode"
5480 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5481 match key with
5482 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5483 let y, h = getpageyh pageno in
5484 let top = (state.winh - h) / 2 in
5485 gotoy (max 0 (y - top))
5486 | 0xff0d (* enter *)
5487 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5488 | 0xff1b -> leavebirdseye beye true (* escape *)
5489 | 0xff52 -> upbirdseye incr beye (* up *)
5490 | 0xff54 -> downbirdseye incr beye (* down *)
5491 | 0xff51 -> upbirdseye 1 beye (* left *)
5492 | 0xff53 -> downbirdseye 1 beye (* right *)
5494 | 0xff55 -> (* prior *)
5495 begin match state.layout with
5496 | l :: _ ->
5497 if l.pagey != 0
5498 then (
5499 state.mode <- Birdseye (
5500 oconf, leftx, l.pageno, hooverpageno, anchor
5502 gotopage1 l.pageno 0;
5504 else (
5505 let layout = layout (state.y-state.winh) (pgh state.layout) in
5506 match layout with
5507 | [] -> gotoy (clamp (-state.winh))
5508 | l :: _ ->
5509 state.mode <- Birdseye (
5510 oconf, leftx, l.pageno, hooverpageno, anchor
5512 gotopage1 l.pageno 0
5515 | [] -> gotoy (clamp (-state.winh))
5516 end;
5518 | 0xff56 -> (* next *)
5519 begin match List.rev state.layout with
5520 | l :: _ ->
5521 let layout = layout (state.y + (pgh state.layout)) state.winh in
5522 begin match layout with
5523 | [] ->
5524 let incr = l.pageh - l.pagevh in
5525 if incr = 0
5526 then (
5527 state.mode <-
5528 Birdseye (
5529 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5531 G.postRedisplay "birdseye pagedown";
5533 else gotoy (clamp (incr + conf.interpagespace*2));
5535 | l :: _ ->
5536 state.mode <-
5537 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5538 gotopage1 l.pageno 0;
5541 | [] -> gotoy (clamp state.winh)
5542 end;
5544 | 0xff50 -> (* home *)
5545 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5546 gotopage1 0 0
5548 | 0xff57 -> (* end *)
5549 let pageno = state.pagecount - 1 in
5550 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5551 if not (pagevisible state.layout pageno)
5552 then
5553 let h =
5554 match List.rev state.pdims with
5555 | [] -> state.winh
5556 | (_, _, h, _) :: _ -> h
5558 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5559 else G.postRedisplay "birdseye end";
5560 | _ -> viewkeyboard key mask
5563 let drawpage l linkindexbase =
5564 let color =
5565 match state.mode with
5566 | Textentry _ -> scalecolor 0.4
5567 | LinkNav _
5568 | View -> scalecolor 1.0
5569 | Birdseye (_, _, pageno, hooverpageno, _) ->
5570 if l.pageno = hooverpageno
5571 then scalecolor 0.9
5572 else (
5573 if l.pageno = pageno
5574 then scalecolor 1.0
5575 else scalecolor 0.8
5578 drawtiles l color;
5579 begin match getopaque l.pageno with
5580 | Some opaque ->
5581 if tileready l l.pagex l.pagey
5582 then
5583 let x = l.pagedispx - l.pagex
5584 and y = l.pagedispy - l.pagey in
5585 let hlmask =
5586 match conf.columns with
5587 | Csingle _ | Cmulti _ ->
5588 (if conf.hlinks then 1 else 0)
5589 + (if state.glinks
5590 && not (isbirdseye state.mode) then 2 else 0)
5591 | _ -> 0
5593 let s =
5594 match state.mode with
5595 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5596 | _ -> ""
5598 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5599 else 0
5601 | _ -> 0
5602 end;
5605 let scrollindicator () =
5606 let sbw, ph, sh = state.uioh#scrollph in
5607 let sbh, pw, sw = state.uioh#scrollpw in
5609 GlDraw.color (0.64, 0.64, 0.64);
5610 GlDraw.rect
5611 (float (state.winw - sbw), 0.)
5612 (float state.winw, float state.winh)
5614 GlDraw.rect
5615 (0., float (state.winh - sbh))
5616 (float (state.winw - state.scrollw - 1), float state.winh)
5618 GlDraw.color (0.0, 0.0, 0.0);
5620 GlDraw.rect
5621 (float (state.winw - sbw), ph)
5622 (float state.winw, ph +. sh)
5624 GlDraw.rect
5625 (pw, float (state.winh - sbh))
5626 (pw +. sw, float state.winh)
5630 let showsel () =
5631 match state.mstate with
5632 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5635 | Msel ((x0, y0), (x1, y1)) ->
5636 let rec loop = function
5637 | l :: ls ->
5638 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5639 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5640 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5641 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5642 then
5643 match getopaque l.pageno with
5644 | Some opaque ->
5645 let x0, y0 = pagetranslatepoint l x0 y0 in
5646 let x1, y1 = pagetranslatepoint l x1 y1 in
5647 seltext opaque (x0, y0, x1, y1);
5648 | _ -> ()
5649 else loop ls
5650 | [] -> ()
5652 loop state.layout
5655 let showrects rects =
5656 Gl.enable `blend;
5657 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5658 GlDraw.polygon_mode `both `fill;
5659 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5660 List.iter
5661 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5662 List.iter (fun l ->
5663 if l.pageno = pageno
5664 then (
5665 let dx = float (l.pagedispx - l.pagex) in
5666 let dy = float (l.pagedispy - l.pagey) in
5667 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5668 GlDraw.begins `quads;
5670 GlDraw.vertex2 (x0+.dx, y0+.dy);
5671 GlDraw.vertex2 (x1+.dx, y1+.dy);
5672 GlDraw.vertex2 (x2+.dx, y2+.dy);
5673 GlDraw.vertex2 (x3+.dx, y3+.dy);
5675 GlDraw.ends ();
5677 ) state.layout
5678 ) rects
5680 Gl.disable `blend;
5683 let display () =
5684 GlClear.color (scalecolor2 conf.bgcolor);
5685 GlClear.clear [`color];
5686 let rec loop linkindexbase = function
5687 | l :: rest ->
5688 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5689 loop linkindexbase rest
5690 | [] -> ()
5692 loop 0 state.layout;
5693 let rects =
5694 match state.mode with
5695 | LinkNav (Ltexact (pageno, linkno)) ->
5696 begin match getopaque pageno with
5697 | Some opaque ->
5698 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5699 (pageno, 5, (
5700 float x0, float y0,
5701 float x1, float y0,
5702 float x1, float y1,
5703 float x0, float y1)
5704 ) :: state.rects
5705 | None -> state.rects
5707 | _ -> state.rects
5709 showrects rects;
5710 showsel ();
5711 state.uioh#display;
5712 begin match state.mstate with
5713 | Mzoomrect ((x0, y0), (x1, y1)) ->
5714 Gl.enable `blend;
5715 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5716 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5717 GlDraw.rect (float x0, float y0)
5718 (float x1, float y1);
5719 Gl.disable `blend;
5720 | _ -> ()
5721 end;
5722 enttext ();
5723 scrollindicator ();
5724 Wsi.swapb ();
5727 let zoomrect x y x1 y1 =
5728 let x0 = min x x1
5729 and x1 = max x x1
5730 and y0 = min y y1 in
5731 gotoy (state.y + y0);
5732 state.anchor <- getanchor ();
5733 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5734 let margin =
5735 if state.w < state.winw - state.scrollw
5736 then (state.winw - state.scrollw - state.w) / 2
5737 else 0
5739 state.x <- (state.x + margin) - x0;
5740 setzoom zoom;
5741 Wsi.setcursor Wsi.CURSOR_INHERIT;
5742 state.mstate <- Mnone;
5745 let scrollx x =
5746 let winw = state.winw - state.scrollw - 1 in
5747 let s = float x /. float winw in
5748 let destx = truncate (float (state.w + winw) *. s) in
5749 state.x <- winw - destx;
5750 gotoy_and_clear_text state.y;
5751 state.mstate <- Mscrollx;
5754 let scrolly y =
5755 let s = float y /. float state.winh in
5756 let desty = truncate (float (state.maxy - state.winh) *. s) in
5757 gotoy_and_clear_text desty;
5758 state.mstate <- Mscrolly;
5761 let viewmouse button down x y mask =
5762 match button with
5763 | n when (n == 4 || n == 5) && not down ->
5764 if Wsi.withctrl mask
5765 then (
5766 match state.mstate with
5767 | Mzoom (oldn, i) ->
5768 if oldn = n
5769 then (
5770 if i = 2
5771 then
5772 let incr =
5773 match n with
5774 | 5 ->
5775 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5776 | _ ->
5777 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5779 let zoom = conf.zoom -. incr in
5780 setzoom zoom;
5781 state.mstate <- Mzoom (n, 0);
5782 else
5783 state.mstate <- Mzoom (n, i+1);
5785 else state.mstate <- Mzoom (n, 0)
5787 | _ -> state.mstate <- Mzoom (n, 0)
5789 else (
5790 match state.autoscroll with
5791 | Some step -> setautoscrollspeed step (n=4)
5792 | None ->
5793 if conf.wheelbypage
5794 then (
5795 if n = 4
5796 then prevpage ()
5797 else nextpage ()
5799 else
5800 let incr =
5801 if n = 4
5802 then -conf.scrollstep
5803 else conf.scrollstep
5805 let incr = incr * 2 in
5806 let y = clamp incr in
5807 gotoy_and_clear_text y
5810 | n when (n = 6 || n = 7) && not down && canpan () ->
5811 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5812 gotoy_and_clear_text state.y
5814 | 1 when Wsi.withshift mask ->
5815 state.mstate <- Mnone;
5816 if not down then (
5817 match unproject x y with
5818 | Some (pageno, ux, uy) ->
5819 let cmd = Printf.sprintf
5820 "%s %s %d %d %d"
5821 conf.stcmd state.path pageno ux uy
5823 popen cmd []
5824 | None -> ()
5827 | 1 when Wsi.withctrl mask ->
5828 if down
5829 then (
5830 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5831 state.mstate <- Mpan (x, y)
5833 else
5834 state.mstate <- Mnone
5836 | 3 ->
5837 if down
5838 then (
5839 Wsi.setcursor Wsi.CURSOR_CYCLE;
5840 let p = (x, y) in
5841 state.mstate <- Mzoomrect (p, p)
5843 else (
5844 match state.mstate with
5845 | Mzoomrect ((x0, y0), _) ->
5846 if abs (x-x0) > 10 && abs (y - y0) > 10
5847 then zoomrect x0 y0 x y
5848 else (
5849 state.mstate <- Mnone;
5850 Wsi.setcursor Wsi.CURSOR_INHERIT;
5851 G.postRedisplay "kill accidental zoom rect";
5853 | _ ->
5854 Wsi.setcursor Wsi.CURSOR_INHERIT;
5855 state.mstate <- Mnone
5858 | 1 when x > state.winw - state.scrollw ->
5859 if down
5860 then
5861 let _, position, sh = state.uioh#scrollph in
5862 if y > truncate position && y < truncate (position +. sh)
5863 then state.mstate <- Mscrolly
5864 else scrolly y
5865 else
5866 state.mstate <- Mnone
5868 | 1 when y > state.winh - state.hscrollh ->
5869 if down
5870 then
5871 let _, position, sw = state.uioh#scrollpw in
5872 if x > truncate position && x < truncate (position +. sw)
5873 then state.mstate <- Mscrollx
5874 else scrollx x
5875 else
5876 state.mstate <- Mnone
5878 | 1 ->
5879 let dest = if down then getunder x y else Unone in
5880 begin match dest with
5881 | Ulinkgoto _
5882 | Ulinkuri _
5883 | Uremote _
5884 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5885 gotounder dest
5887 | Unone when down ->
5888 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5889 state.mstate <- Mpan (x, y);
5891 | Unone | Utext _ ->
5892 if down
5893 then (
5894 if conf.angle mod 360 = 0
5895 then (
5896 state.mstate <- Msel ((x, y), (x, y));
5897 G.postRedisplay "mouse select";
5900 else (
5901 match state.mstate with
5902 | Mnone -> ()
5904 | Mzoom _ | Mscrollx | Mscrolly ->
5905 state.mstate <- Mnone
5907 | Mzoomrect ((x0, y0), _) ->
5908 zoomrect x0 y0 x y
5910 | Mpan _ ->
5911 Wsi.setcursor Wsi.CURSOR_INHERIT;
5912 state.mstate <- Mnone
5914 | Msel ((x0, y0), (x1, y1)) ->
5915 let rec loop = function
5916 | [] -> ()
5917 | l :: rest ->
5918 let inside =
5919 let a0 = l.pagedispy in
5920 let a1 = a0 + l.pagevh in
5921 let b0 = l.pagedispx in
5922 let b1 = b0 + l.pagevw in
5923 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5924 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5926 if inside
5927 then
5928 match getopaque l.pageno with
5929 | Some opaque ->
5930 begin
5931 match Ne.pipe () with
5932 | Ne.Exn exn ->
5933 showtext '!'
5934 (Printf.sprintf
5935 "can not create sel pipe: %s"
5936 (exntos exn));
5937 | Ne.Res (r, w) ->
5938 let doclose what fd =
5939 Ne.clo fd (fun msg ->
5940 dolog "%s close failed: %s" what msg)
5943 popen conf.selcmd [r, 0; w, -1];
5944 copysel w opaque;
5945 doclose "pipe/r" r;
5946 G.postRedisplay "copysel";
5947 with exn ->
5948 dolog "can not execute %S: %s"
5949 conf.selcmd (exntos exn);
5950 doclose "pipe/r" r;
5951 doclose "pipe/w" w;
5953 | None -> ()
5954 else loop rest
5956 loop state.layout;
5957 Wsi.setcursor Wsi.CURSOR_INHERIT;
5958 state.mstate <- Mnone;
5962 | _ -> ()
5965 let birdseyemouse button down x y mask
5966 (conf, leftx, _, hooverpageno, anchor) =
5967 match button with
5968 | 1 when down ->
5969 let rec loop = function
5970 | [] -> ()
5971 | l :: rest ->
5972 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5973 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5974 then (
5975 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5977 else loop rest
5979 loop state.layout
5980 | 3 -> ()
5981 | _ -> viewmouse button down x y mask
5984 let mouse button down x y mask =
5985 state.uioh <- state.uioh#button button down x y mask;
5988 let motion ~x ~y =
5989 state.uioh <- state.uioh#motion x y
5992 let pmotion ~x ~y =
5993 state.uioh <- state.uioh#pmotion x y;
5996 let uioh = object
5997 method display = ()
5999 method key key mask =
6000 begin match state.mode with
6001 | Textentry textentry -> textentrykeyboard key mask textentry
6002 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6003 | View -> viewkeyboard key mask
6004 | LinkNav linknav -> linknavkeyboard key mask linknav
6005 end;
6006 state.uioh
6008 method button button bstate x y mask =
6009 begin match state.mode with
6010 | LinkNav _
6011 | View -> viewmouse button bstate x y mask
6012 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6013 | Textentry _ -> ()
6014 end;
6015 state.uioh
6017 method motion x y =
6018 begin match state.mode with
6019 | Textentry _ -> ()
6020 | View | Birdseye _ | LinkNav _ ->
6021 match state.mstate with
6022 | Mzoom _ | Mnone -> ()
6024 | Mpan (x0, y0) ->
6025 let dx = x - x0
6026 and dy = y0 - y in
6027 state.mstate <- Mpan (x, y);
6028 if canpan ()
6029 then state.x <- state.x + dx;
6030 let y = clamp dy in
6031 gotoy_and_clear_text y
6033 | Msel (a, _) ->
6034 state.mstate <- Msel (a, (x, y));
6035 G.postRedisplay "motion select";
6037 | Mscrolly ->
6038 let y = min state.winh (max 0 y) in
6039 scrolly y
6041 | Mscrollx ->
6042 let x = min state.winw (max 0 x) in
6043 scrollx x
6045 | Mzoomrect (p0, _) ->
6046 state.mstate <- Mzoomrect (p0, (x, y));
6047 G.postRedisplay "motion zoomrect";
6048 end;
6049 state.uioh
6051 method pmotion x y =
6052 begin match state.mode with
6053 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6054 let rec loop = function
6055 | [] ->
6056 if hooverpageno != -1
6057 then (
6058 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6059 G.postRedisplay "pmotion birdseye no hoover";
6061 | l :: rest ->
6062 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6063 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6064 then (
6065 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6066 G.postRedisplay "pmotion birdseye hoover";
6068 else loop rest
6070 loop state.layout
6072 | Textentry _ -> ()
6074 | LinkNav _
6075 | View ->
6076 match state.mstate with
6077 | Mnone -> updateunder x y
6078 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6080 end;
6081 state.uioh
6083 method infochanged _ = ()
6085 method scrollph =
6086 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6087 let p, h = scrollph state.y maxy in
6088 state.scrollw, p, h
6090 method scrollpw =
6091 let winw = state.winw - state.scrollw - 1 in
6092 let fwinw = float winw in
6093 let sw =
6094 let sw = fwinw /. float state.w in
6095 let sw = fwinw *. sw in
6096 max sw (float conf.scrollh)
6098 let position, sw =
6099 let f = state.w+winw in
6100 let r = float (winw-state.x) /. float f in
6101 let p = fwinw *. r in
6102 p-.sw/.2., sw
6104 let sw =
6105 if position +. sw > fwinw
6106 then fwinw -. position
6107 else sw
6109 state.hscrollh, position, sw
6111 method modehash =
6112 let modename =
6113 match state.mode with
6114 | LinkNav _ -> "links"
6115 | Textentry _ -> "textentry"
6116 | Birdseye _ -> "birdseye"
6117 | View -> "view"
6119 findkeyhash conf modename
6120 end;;
6122 module Config =
6123 struct
6124 open Parser
6126 let fontpath = ref "";;
6128 module KeyMap =
6129 Map.Make (struct type t = (int * int) let compare = compare end);;
6131 let unent s =
6132 let l = String.length s in
6133 let b = Buffer.create l in
6134 unent b s 0 l;
6135 Buffer.contents b;
6138 let home =
6139 try Sys.getenv "HOME"
6140 with exn ->
6141 prerr_endline
6142 ("Can not determine home directory location: " ^ exntos exn);
6146 let modifier_of_string = function
6147 | "alt" -> Wsi.altmask
6148 | "shift" -> Wsi.shiftmask
6149 | "ctrl" | "control" -> Wsi.ctrlmask
6150 | "meta" -> Wsi.metamask
6151 | _ -> 0
6154 let key_of_string =
6155 let r = Str.regexp "-" in
6156 fun s ->
6157 let elems = Str.full_split r s in
6158 let f n k m =
6159 let g s =
6160 let m1 = modifier_of_string s in
6161 if m1 = 0
6162 then (Wsi.namekey s, m)
6163 else (k, m lor m1)
6164 in function
6165 | Str.Delim s when n land 1 = 0 -> g s
6166 | Str.Text s -> g s
6167 | Str.Delim _ -> (k, m)
6169 let rec loop n k m = function
6170 | [] -> (k, m)
6171 | x :: xs ->
6172 let k, m = f n k m x in
6173 loop (n+1) k m xs
6175 loop 0 0 0 elems
6178 let keys_of_string =
6179 let r = Str.regexp "[ \t]" in
6180 fun s ->
6181 let elems = Str.split r s in
6182 List.map key_of_string elems
6185 let copykeyhashes c =
6186 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6189 let config_of c attrs =
6190 let apply c k v =
6192 match k with
6193 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6194 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6195 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6196 | "preload" -> { c with preload = bool_of_string v }
6197 | "page-bias" -> { c with pagebias = int_of_string v }
6198 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6199 | "horizontal-scroll-step" ->
6200 { c with hscrollstep = max (int_of_string v) 1 }
6201 | "auto-scroll-step" ->
6202 { c with autoscrollstep = max 0 (int_of_string v) }
6203 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6204 | "crop-hack" -> { c with crophack = bool_of_string v }
6205 | "throttle" ->
6206 let mw =
6207 match String.lowercase v with
6208 | "true" -> Some infinity
6209 | "false" -> None
6210 | f -> Some (float_of_string f)
6212 { c with maxwait = mw}
6213 | "highlight-links" -> { c with hlinks = bool_of_string v }
6214 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6215 | "vertical-margin" ->
6216 { c with interpagespace = max 0 (int_of_string v) }
6217 | "zoom" ->
6218 let zoom = float_of_string v /. 100. in
6219 let zoom = max zoom 0.0 in
6220 { c with zoom = zoom }
6221 | "presentation" -> { c with presentation = bool_of_string v }
6222 | "rotation-angle" -> { c with angle = int_of_string v }
6223 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6224 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6225 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6226 | "proportional-display" -> { c with proportional = bool_of_string v }
6227 | "pixmap-cache-size" ->
6228 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6229 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6230 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6231 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6232 | "persistent-location" -> { c with jumpback = bool_of_string v }
6233 | "background-color" -> { c with bgcolor = color_of_string v }
6234 | "scrollbar-in-presentation" ->
6235 { c with scrollbarinpm = bool_of_string v }
6236 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6237 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6238 | "mupdf-store-size" ->
6239 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6240 | "checkers" -> { c with checkers = bool_of_string v }
6241 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6242 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6243 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6244 | "uri-launcher" -> { c with urilauncher = unent v }
6245 | "path-launcher" -> { c with pathlauncher = unent v }
6246 | "color-space" -> { c with colorspace = colorspace_of_string v }
6247 | "invert-colors" -> { c with invert = bool_of_string v }
6248 | "brightness" -> { c with colorscale = float_of_string v }
6249 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6250 | "ghyllscroll" ->
6251 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6252 | "columns" ->
6253 let (n, _, _) as nab = multicolumns_of_string v in
6254 if n < 0
6255 then { c with columns = Csplit (-n, [||]) }
6256 else { c with columns = Cmulti (nab, [||]) }
6257 | "birds-eye-columns" ->
6258 { c with beyecolumns = Some (max (int_of_string v) 2) }
6259 | "selection-command" -> { c with selcmd = unent v }
6260 | "synctex-command" -> { c with stcmd = unent v }
6261 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6262 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6263 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6264 | "use-pbo" -> { c with usepbo = bool_of_string v }
6265 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6266 | _ -> c
6267 with exn ->
6268 prerr_endline ("Error processing attribute (`" ^
6269 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6272 let rec fold c = function
6273 | [] -> c
6274 | (k, v) :: rest ->
6275 let c = apply c k v in
6276 fold c rest
6278 fold { c with keyhashes = copykeyhashes c } attrs;
6281 let fromstring f pos n v d =
6282 try f v
6283 with exn ->
6284 dolog "Error processing attribute (%S=%S) at %d\n%s"
6285 n v pos (exntos exn)
6290 let bookmark_of attrs =
6291 let rec fold title page rely visy = function
6292 | ("title", v) :: rest -> fold v page rely visy rest
6293 | ("page", v) :: rest -> fold title v rely visy rest
6294 | ("rely", v) :: rest -> fold title page v visy rest
6295 | ("visy", v) :: rest -> fold title page rely v rest
6296 | _ :: rest -> fold title page rely visy rest
6297 | [] -> title, page, rely, visy
6299 fold "invalid" "0" "0" "0" attrs
6302 let doc_of attrs =
6303 let rec fold path page rely pan visy = function
6304 | ("path", v) :: rest -> fold v page rely pan visy rest
6305 | ("page", v) :: rest -> fold path v rely pan visy rest
6306 | ("rely", v) :: rest -> fold path page v pan visy rest
6307 | ("pan", v) :: rest -> fold path page rely v visy rest
6308 | ("visy", v) :: rest -> fold path page rely pan v rest
6309 | _ :: rest -> fold path page rely pan visy rest
6310 | [] -> path, page, rely, pan, visy
6312 fold "" "0" "0" "0" "0" attrs
6315 let map_of attrs =
6316 let rec fold rs ls = function
6317 | ("out", v) :: rest -> fold v ls rest
6318 | ("in", v) :: rest -> fold rs v rest
6319 | _ :: rest -> fold ls rs rest
6320 | [] -> ls, rs
6322 fold "" "" attrs
6325 let setconf dst src =
6326 dst.scrollbw <- src.scrollbw;
6327 dst.scrollh <- src.scrollh;
6328 dst.icase <- src.icase;
6329 dst.preload <- src.preload;
6330 dst.pagebias <- src.pagebias;
6331 dst.verbose <- src.verbose;
6332 dst.scrollstep <- src.scrollstep;
6333 dst.maxhfit <- src.maxhfit;
6334 dst.crophack <- src.crophack;
6335 dst.autoscrollstep <- src.autoscrollstep;
6336 dst.maxwait <- src.maxwait;
6337 dst.hlinks <- src.hlinks;
6338 dst.underinfo <- src.underinfo;
6339 dst.interpagespace <- src.interpagespace;
6340 dst.zoom <- src.zoom;
6341 dst.presentation <- src.presentation;
6342 dst.angle <- src.angle;
6343 dst.cwinw <- src.cwinw;
6344 dst.cwinh <- src.cwinh;
6345 dst.savebmarks <- src.savebmarks;
6346 dst.memlimit <- src.memlimit;
6347 dst.proportional <- src.proportional;
6348 dst.texcount <- src.texcount;
6349 dst.sliceheight <- src.sliceheight;
6350 dst.thumbw <- src.thumbw;
6351 dst.jumpback <- src.jumpback;
6352 dst.bgcolor <- src.bgcolor;
6353 dst.scrollbarinpm <- src.scrollbarinpm;
6354 dst.tilew <- src.tilew;
6355 dst.tileh <- src.tileh;
6356 dst.mustoresize <- src.mustoresize;
6357 dst.checkers <- src.checkers;
6358 dst.aalevel <- src.aalevel;
6359 dst.trimmargins <- src.trimmargins;
6360 dst.trimfuzz <- src.trimfuzz;
6361 dst.urilauncher <- src.urilauncher;
6362 dst.colorspace <- src.colorspace;
6363 dst.invert <- src.invert;
6364 dst.colorscale <- src.colorscale;
6365 dst.redirectstderr <- src.redirectstderr;
6366 dst.ghyllscroll <- src.ghyllscroll;
6367 dst.columns <- src.columns;
6368 dst.beyecolumns <- src.beyecolumns;
6369 dst.selcmd <- src.selcmd;
6370 dst.updatecurs <- src.updatecurs;
6371 dst.pathlauncher <- src.pathlauncher;
6372 dst.keyhashes <- copykeyhashes src;
6373 dst.hfsize <- src.hfsize;
6374 dst.hscrollstep <- src.hscrollstep;
6375 dst.pgscale <- src.pgscale;
6376 dst.usepbo <- src.usepbo;
6377 dst.wheelbypage <- src.wheelbypage;
6378 dst.stcmd <- src.stcmd;
6381 let get s =
6382 let h = Hashtbl.create 10 in
6383 let dc = { defconf with angle = defconf.angle } in
6384 let rec toplevel v t spos _ =
6385 match t with
6386 | Vdata | Vcdata | Vend -> v
6387 | Vopen ("llppconfig", _, closed) ->
6388 if closed
6389 then v
6390 else { v with f = llppconfig }
6391 | Vopen _ ->
6392 error "unexpected subelement at top level" s spos
6393 | Vclose _ -> error "unexpected close at top level" s spos
6395 and llppconfig v t spos _ =
6396 match t with
6397 | Vdata | Vcdata -> v
6398 | Vend -> error "unexpected end of input in llppconfig" s spos
6399 | Vopen ("defaults", attrs, closed) ->
6400 let c = config_of dc attrs in
6401 setconf dc c;
6402 if closed
6403 then v
6404 else { v with f = defaults }
6406 | Vopen ("ui-font", attrs, closed) ->
6407 let rec getsize size = function
6408 | [] -> size
6409 | ("size", v) :: rest ->
6410 let size =
6411 fromstring int_of_string spos "size" v fstate.fontsize in
6412 getsize size rest
6413 | l -> getsize size l
6415 fstate.fontsize <- getsize fstate.fontsize attrs;
6416 if closed
6417 then v
6418 else { v with f = uifont (Buffer.create 10) }
6420 | Vopen ("doc", attrs, closed) ->
6421 let pathent, spage, srely, span, svisy = doc_of attrs in
6422 let path = unent pathent
6423 and pageno = fromstring int_of_string spos "page" spage 0
6424 and rely = fromstring float_of_string spos "rely" srely 0.0
6425 and pan = fromstring int_of_string spos "pan" span 0
6426 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6427 let c = config_of dc attrs in
6428 let anchor = (pageno, rely, visy) in
6429 if closed
6430 then (Hashtbl.add h path (c, [], pan, anchor); v)
6431 else { v with f = doc path pan anchor c [] }
6433 | Vopen _ ->
6434 error "unexpected subelement in llppconfig" s spos
6436 | Vclose "llppconfig" -> { v with f = toplevel }
6437 | Vclose _ -> error "unexpected close in llppconfig" s spos
6439 and defaults v t spos _ =
6440 match t with
6441 | Vdata | Vcdata -> v
6442 | Vend -> error "unexpected end of input in defaults" s spos
6443 | Vopen ("keymap", attrs, closed) ->
6444 let modename =
6445 try List.assoc "mode" attrs
6446 with Not_found -> "global" in
6447 if closed
6448 then v
6449 else
6450 let ret keymap =
6451 let h = findkeyhash dc modename in
6452 KeyMap.iter (Hashtbl.replace h) keymap;
6453 defaults
6455 { v with f = pkeymap ret KeyMap.empty }
6457 | Vopen (_, _, _) ->
6458 error "unexpected subelement in defaults" s spos
6460 | Vclose "defaults" ->
6461 { v with f = llppconfig }
6463 | Vclose _ -> error "unexpected close in defaults" s spos
6465 and uifont b v t spos epos =
6466 match t with
6467 | Vdata | Vcdata ->
6468 Buffer.add_substring b s spos (epos - spos);
6470 | Vopen (_, _, _) ->
6471 error "unexpected subelement in ui-font" s spos
6472 | Vclose "ui-font" ->
6473 if String.length !fontpath = 0
6474 then fontpath := Buffer.contents b;
6475 { v with f = llppconfig }
6476 | Vclose _ -> error "unexpected close in ui-font" s spos
6477 | Vend -> error "unexpected end of input in ui-font" s spos
6479 and doc path pan anchor c bookmarks v t spos _ =
6480 match t with
6481 | Vdata | Vcdata -> v
6482 | Vend -> error "unexpected end of input in doc" s spos
6483 | Vopen ("bookmarks", _, closed) ->
6484 if closed
6485 then v
6486 else { v with f = pbookmarks path pan anchor c bookmarks }
6488 | Vopen ("keymap", attrs, closed) ->
6489 let modename =
6490 try List.assoc "mode" attrs
6491 with Not_found -> "global"
6493 if closed
6494 then v
6495 else
6496 let ret keymap =
6497 let h = findkeyhash c modename in
6498 KeyMap.iter (Hashtbl.replace h) keymap;
6499 doc path pan anchor c bookmarks
6501 { v with f = pkeymap ret KeyMap.empty }
6503 | Vopen (_, _, _) ->
6504 error "unexpected subelement in doc" s spos
6506 | Vclose "doc" ->
6507 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6508 { v with f = llppconfig }
6510 | Vclose _ -> error "unexpected close in doc" s spos
6512 and pkeymap ret keymap v t spos _ =
6513 match t with
6514 | Vdata | Vcdata -> v
6515 | Vend -> error "unexpected end of input in keymap" s spos
6516 | Vopen ("map", attrs, closed) ->
6517 let r, l = map_of attrs in
6518 let kss = fromstring keys_of_string spos "in" r [] in
6519 let lss = fromstring keys_of_string spos "out" l [] in
6520 let keymap =
6521 match kss with
6522 | [] -> keymap
6523 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6524 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6526 if closed
6527 then { v with f = pkeymap ret keymap }
6528 else
6529 let f () = v in
6530 { v with f = skip "map" f }
6532 | Vopen _ ->
6533 error "unexpected subelement in keymap" s spos
6535 | Vclose "keymap" ->
6536 { v with f = ret keymap }
6538 | Vclose _ -> error "unexpected close in keymap" s spos
6540 and pbookmarks path pan anchor c bookmarks v t spos _ =
6541 match t with
6542 | Vdata | Vcdata -> v
6543 | Vend -> error "unexpected end of input in bookmarks" s spos
6544 | Vopen ("item", attrs, closed) ->
6545 let titleent, spage, srely, svisy = bookmark_of attrs in
6546 let page = fromstring int_of_string spos "page" spage 0
6547 and rely = fromstring float_of_string spos "rely" srely 0.0
6548 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6549 let bookmarks =
6550 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6552 if closed
6553 then { v with f = pbookmarks path pan anchor c bookmarks }
6554 else
6555 let f () = v in
6556 { v with f = skip "item" f }
6558 | Vopen _ ->
6559 error "unexpected subelement in bookmarks" s spos
6561 | Vclose "bookmarks" ->
6562 { v with f = doc path pan anchor c bookmarks }
6564 | Vclose _ -> error "unexpected close in bookmarks" s spos
6566 and skip tag f v t spos _ =
6567 match t with
6568 | Vdata | Vcdata -> v
6569 | Vend ->
6570 error ("unexpected end of input in skipped " ^ tag) s spos
6571 | Vopen (tag', _, closed) ->
6572 if closed
6573 then v
6574 else
6575 let f' () = { v with f = skip tag f } in
6576 { v with f = skip tag' f' }
6577 | Vclose ctag ->
6578 if tag = ctag
6579 then f ()
6580 else error ("unexpected close in skipped " ^ tag) s spos
6583 parse { f = toplevel; accu = () } s;
6584 h, dc;
6587 let do_load f ic =
6589 let len = in_channel_length ic in
6590 let s = String.create len in
6591 really_input ic s 0 len;
6592 f s;
6593 with
6594 | Parse_error (msg, s, pos) ->
6595 let subs = subs s pos in
6596 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6597 failwith ("parse error: " ^ s)
6599 | exn ->
6600 failwith ("config load error: " ^ exntos exn)
6603 let defconfpath =
6604 let dir =
6606 let dir = Filename.concat home ".config" in
6607 if Sys.is_directory dir then dir else home
6608 with _ -> home
6610 Filename.concat dir "llpp.conf"
6613 let confpath = ref defconfpath;;
6615 let load1 f =
6616 if Sys.file_exists !confpath
6617 then
6618 match
6619 (try Some (open_in_bin !confpath)
6620 with exn ->
6621 prerr_endline
6622 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6623 exntos exn);
6624 None
6626 with
6627 | Some ic ->
6628 let success =
6630 f (do_load get ic)
6631 with exn ->
6632 prerr_endline
6633 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6634 exntos exn);
6635 false
6637 close_in ic;
6638 success
6640 | None -> false
6641 else
6642 f (Hashtbl.create 0, defconf)
6645 let load () =
6646 let f (h, dc) =
6647 let pc, pb, px, pa =
6649 Hashtbl.find h (Filename.basename state.path)
6650 with Not_found -> dc, [], 0, emptyanchor
6652 setconf defconf dc;
6653 setconf conf pc;
6654 state.bookmarks <- pb;
6655 state.x <- px;
6656 state.scrollw <- conf.scrollbw;
6657 if conf.jumpback
6658 then state.anchor <- pa;
6659 cbput state.hists.nav pa;
6660 true
6662 load1 f
6665 let add_attrs bb always dc c =
6666 let ob s a b =
6667 if always || a != b
6668 then Printf.bprintf bb "\n %s='%b'" s a
6669 and oi s a b =
6670 if always || a != b
6671 then Printf.bprintf bb "\n %s='%d'" s a
6672 and oI s a b =
6673 if always || a != b
6674 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6675 and oz s a b =
6676 if always || a <> b
6677 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6678 and oF s a b =
6679 if always || a <> b
6680 then Printf.bprintf bb "\n %s='%f'" s a
6681 and oc s a b =
6682 if always || a <> b
6683 then
6684 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6685 and oC s a b =
6686 if always || a <> b
6687 then
6688 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6689 and oR s a b =
6690 if always || a <> b
6691 then
6692 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6693 and os s a b =
6694 if always || a <> b
6695 then
6696 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6697 and og s a b =
6698 if always || a <> b
6699 then
6700 match a with
6701 | None -> ()
6702 | Some (_N, _A, _B) ->
6703 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6704 and oW s a b =
6705 if always || a <> b
6706 then
6707 let v =
6708 match a with
6709 | None -> "false"
6710 | Some f ->
6711 if f = infinity
6712 then "true"
6713 else string_of_float f
6715 Printf.bprintf bb "\n %s='%s'" s v
6716 and oco s a b =
6717 if always || a <> b
6718 then
6719 match a with
6720 | Cmulti ((n, a, b), _) when n > 1 ->
6721 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6722 | Csplit (n, _) when n > 1 ->
6723 Printf.bprintf bb "\n %s='%d'" s ~-n
6724 | _ -> ()
6725 and obeco s a b =
6726 if always || a <> b
6727 then
6728 match a with
6729 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6730 | _ -> ()
6732 oi "width" c.cwinw dc.cwinw;
6733 oi "height" c.cwinh dc.cwinh;
6734 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6735 oi "scroll-handle-height" c.scrollh dc.scrollh;
6736 ob "case-insensitive-search" c.icase dc.icase;
6737 ob "preload" c.preload dc.preload;
6738 oi "page-bias" c.pagebias dc.pagebias;
6739 oi "scroll-step" c.scrollstep dc.scrollstep;
6740 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6741 ob "max-height-fit" c.maxhfit dc.maxhfit;
6742 ob "crop-hack" c.crophack dc.crophack;
6743 oW "throttle" c.maxwait dc.maxwait;
6744 ob "highlight-links" c.hlinks dc.hlinks;
6745 ob "under-cursor-info" c.underinfo dc.underinfo;
6746 oi "vertical-margin" c.interpagespace dc.interpagespace;
6747 oz "zoom" c.zoom dc.zoom;
6748 ob "presentation" c.presentation dc.presentation;
6749 oi "rotation-angle" c.angle dc.angle;
6750 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6751 ob "proportional-display" c.proportional dc.proportional;
6752 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6753 oi "tex-count" c.texcount dc.texcount;
6754 oi "slice-height" c.sliceheight dc.sliceheight;
6755 oi "thumbnail-width" c.thumbw dc.thumbw;
6756 ob "persistent-location" c.jumpback dc.jumpback;
6757 oc "background-color" c.bgcolor dc.bgcolor;
6758 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6759 oi "tile-width" c.tilew dc.tilew;
6760 oi "tile-height" c.tileh dc.tileh;
6761 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6762 ob "checkers" c.checkers dc.checkers;
6763 oi "aalevel" c.aalevel dc.aalevel;
6764 ob "trim-margins" c.trimmargins dc.trimmargins;
6765 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6766 os "uri-launcher" c.urilauncher dc.urilauncher;
6767 os "path-launcher" c.pathlauncher dc.pathlauncher;
6768 oC "color-space" c.colorspace dc.colorspace;
6769 ob "invert-colors" c.invert dc.invert;
6770 oF "brightness" c.colorscale dc.colorscale;
6771 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6772 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6773 oco "columns" c.columns dc.columns;
6774 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6775 os "selection-command" c.selcmd dc.selcmd;
6776 os "synctex-command" c.stcmd dc.stcmd;
6777 ob "update-cursor" c.updatecurs dc.updatecurs;
6778 oi "hint-font-size" c.hfsize dc.hfsize;
6779 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6780 oF "page-scroll-scale" c.pgscale dc.pgscale;
6781 ob "use-pbo" c.usepbo dc.usepbo;
6782 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6785 let keymapsbuf always dc c =
6786 let bb = Buffer.create 16 in
6787 let rec loop = function
6788 | [] -> ()
6789 | (modename, h) :: rest ->
6790 let dh = findkeyhash dc modename in
6791 if always || h <> dh
6792 then (
6793 if Hashtbl.length h > 0
6794 then (
6795 if Buffer.length bb > 0
6796 then Buffer.add_char bb '\n';
6797 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6798 Hashtbl.iter (fun i o ->
6799 let isdifferent = always ||
6801 let dO = Hashtbl.find dh i in
6802 dO <> o
6803 with Not_found -> true
6805 if isdifferent
6806 then
6807 let addkm (k, m) =
6808 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6809 if Wsi.withalt m then Buffer.add_string bb "alt-";
6810 if Wsi.withshift m then Buffer.add_string bb "shift-";
6811 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6812 Buffer.add_string bb (Wsi.keyname k);
6814 let addkms l =
6815 let rec loop = function
6816 | [] -> ()
6817 | km :: [] -> addkm km
6818 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6820 loop l
6822 Buffer.add_string bb "<map in='";
6823 addkm i;
6824 match o with
6825 | KMinsrt km ->
6826 Buffer.add_string bb "' out='";
6827 addkm km;
6828 Buffer.add_string bb "'/>\n"
6830 | KMinsrl kms ->
6831 Buffer.add_string bb "' out='";
6832 addkms kms;
6833 Buffer.add_string bb "'/>\n"
6835 | KMmulti (ins, kms) ->
6836 Buffer.add_char bb ' ';
6837 addkms ins;
6838 Buffer.add_string bb "' out='";
6839 addkms kms;
6840 Buffer.add_string bb "'/>\n"
6841 ) h;
6842 Buffer.add_string bb "</keymap>";
6845 loop rest
6847 loop c.keyhashes;
6851 let save () =
6852 let uifontsize = fstate.fontsize in
6853 let bb = Buffer.create 32768 in
6854 let w, h =
6855 List.fold_left
6856 (fun (w, h) ws ->
6857 match ws with
6858 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6859 | Wsi.MaxVert -> (w, conf.cwinh)
6860 | Wsi.MaxHorz -> (conf.cwinw, h)
6862 (state.winw, state.winh) state.winstate
6864 conf.cwinw <- w;
6865 conf.cwinh <- h;
6866 let f (h, dc) =
6867 let dc = if conf.bedefault then conf else dc in
6868 Buffer.add_string bb "<llppconfig>\n";
6870 if String.length !fontpath > 0
6871 then
6872 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6873 uifontsize
6874 !fontpath
6875 else (
6876 if uifontsize <> 14
6877 then
6878 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6881 Buffer.add_string bb "<defaults ";
6882 add_attrs bb true dc dc;
6883 let kb = keymapsbuf true dc dc in
6884 if Buffer.length kb > 0
6885 then (
6886 Buffer.add_string bb ">\n";
6887 Buffer.add_buffer bb kb;
6888 Buffer.add_string bb "\n</defaults>\n";
6890 else Buffer.add_string bb "/>\n";
6892 let adddoc path pan anchor c bookmarks =
6893 if bookmarks == [] && c = dc && anchor = emptyanchor
6894 then ()
6895 else (
6896 Printf.bprintf bb "<doc path='%s'"
6897 (enent path 0 (String.length path));
6899 if anchor <> emptyanchor
6900 then (
6901 let n, rely, visy = anchor in
6902 Printf.bprintf bb " page='%d'" n;
6903 if rely > 1e-6
6904 then
6905 Printf.bprintf bb " rely='%f'" rely
6907 if abs_float visy > 1e-6
6908 then
6909 Printf.bprintf bb " visy='%f'" visy
6913 if pan != 0
6914 then Printf.bprintf bb " pan='%d'" pan;
6916 add_attrs bb false dc c;
6917 let kb = keymapsbuf false dc c in
6919 begin match bookmarks with
6920 | [] ->
6921 if Buffer.length kb > 0
6922 then (
6923 Buffer.add_string bb ">\n";
6924 Buffer.add_buffer bb kb;
6925 Buffer.add_string bb "\n</doc>\n";
6927 else Buffer.add_string bb "/>\n"
6928 | _ ->
6929 Buffer.add_string bb ">\n<bookmarks>\n";
6930 List.iter (fun (title, _level, (page, rely, visy)) ->
6931 Printf.bprintf bb
6932 "<item title='%s' page='%d'"
6933 (enent title 0 (String.length title))
6934 page
6936 if rely > 1e-6
6937 then
6938 Printf.bprintf bb " rely='%f'" rely
6940 if abs_float visy > 1e-6
6941 then
6942 Printf.bprintf bb " visy='%f'" visy
6944 Buffer.add_string bb "/>\n";
6945 ) bookmarks;
6946 Buffer.add_string bb "</bookmarks>";
6947 if Buffer.length kb > 0
6948 then (
6949 Buffer.add_string bb "\n";
6950 Buffer.add_buffer bb kb;
6952 Buffer.add_string bb "\n</doc>\n";
6953 end;
6957 let pan, conf =
6958 match state.mode with
6959 | Birdseye (c, pan, _, _, _) ->
6960 let beyecolumns =
6961 match conf.columns with
6962 | Cmulti ((c, _, _), _) -> Some c
6963 | Csingle _ -> None
6964 | Csplit _ -> None
6965 and columns =
6966 match c.columns with
6967 | Cmulti (c, _) -> Cmulti (c, [||])
6968 | Csingle _ -> Csingle [||]
6969 | Csplit _ -> failwith "quit from bird's eye while split"
6971 pan, { c with beyecolumns = beyecolumns; columns = columns }
6972 | _ -> state.x, conf
6974 let basename = Filename.basename state.path in
6975 adddoc basename pan (getanchor ())
6976 (let conf =
6977 let autoscrollstep =
6978 match state.autoscroll with
6979 | Some step -> step
6980 | None -> conf.autoscrollstep
6982 match state.mode with
6983 | Birdseye (bc, _, _, _, _) ->
6984 { conf with
6985 zoom = bc.zoom;
6986 presentation = bc.presentation;
6987 interpagespace = bc.interpagespace;
6988 maxwait = bc.maxwait;
6989 autoscrollstep = autoscrollstep }
6990 | _ -> { conf with autoscrollstep = autoscrollstep }
6991 in conf)
6992 (if conf.savebmarks then state.bookmarks else []);
6994 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6995 if basename <> path
6996 then adddoc path x anchor c bookmarks
6997 ) h;
6998 Buffer.add_string bb "</llppconfig>\n";
6999 true;
7001 if load1 f && Buffer.length bb > 0
7002 then
7004 let tmp = !confpath ^ ".tmp" in
7005 let oc = open_out_bin tmp in
7006 Buffer.output_buffer oc bb;
7007 close_out oc;
7008 Unix.rename tmp !confpath;
7009 with exn ->
7010 prerr_endline
7011 ("error while saving configuration: " ^ exntos exn)
7013 end;;
7015 let adderrmsg src msg =
7016 Buffer.add_string state.errmsgs msg;
7017 state.newerrmsgs <- true;
7018 G.postRedisplay src
7021 let adderrfmt src fmt =
7022 Format.kprintf (fun s -> adderrmsg src s) fmt;
7025 let ract cmds =
7026 let cl = splitatspace cmds in
7027 let scan s fmt f =
7028 try Scanf.sscanf s fmt f
7029 with exn ->
7030 adderrfmt "remote exec"
7031 "error processing '%S': %s\n" cmds (exntos exn)
7033 match cl with
7034 | "reload" :: [] -> reload ()
7035 | "goto" :: args :: [] ->
7036 scan args "%u %f %f"
7037 (fun pageno x y ->
7038 let cmd, _ = state.geomcmds in
7039 if String.length cmd = 0
7040 then gotopagexy pageno x y
7041 else
7042 let f prevf () =
7043 gotopagexy pageno x y;
7044 prevf ()
7046 state.reprf <- f state.reprf
7048 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7049 | "rect" :: args :: [] ->
7050 scan args "%u %u %f %f %f %f"
7051 (fun pageno color x0 y0 x1 y1 ->
7052 onpagerect pageno (fun w h ->
7053 let _,w1,h1,_ = getpagedim pageno in
7054 let sw = float w1 /. w
7055 and sh = float h1 /. h in
7056 let x0s = x0 *. sw
7057 and x1s = x1 *. sw
7058 and y0s = y0 *. sh
7059 and y1s = y1 *. sh in
7060 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7061 debugrect rect;
7062 state.rects <- (pageno, color, rect) :: state.rects;
7063 G.postRedisplay "rect";
7066 | "activatewin" :: [] -> Wsi.activatewin ()
7067 | "quit" :: [] -> raise Quit
7068 | _ ->
7069 adderrfmt "remote command"
7070 "error processing remote command: %S\n" cmds;
7073 let remote =
7074 let scratch = String.create 80 in
7075 let buf = Buffer.create 80 in
7076 fun fd ->
7077 let rec tempfr () =
7078 try Some (Unix.read fd scratch 0 80)
7079 with
7080 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7081 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7082 | exn -> raise exn
7084 match tempfr () with
7085 | None -> Some fd
7086 | Some n ->
7087 if n = 0
7088 then (
7089 Unix.close fd;
7090 if Buffer.length buf > 0
7091 then (
7092 let s = Buffer.contents buf in
7093 Buffer.clear buf;
7094 ract s;
7096 None
7098 else
7099 let rec eat ppos =
7100 let nlpos =
7102 let pos = String.index_from scratch ppos '\n' in
7103 if pos >= n then -1 else pos
7104 with Not_found -> -1
7106 if nlpos >= 0
7107 then (
7108 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7109 let s = Buffer.contents buf in
7110 Buffer.clear buf;
7111 ract s;
7112 eat (nlpos+1);
7114 else (
7115 Buffer.add_substring buf scratch ppos (n-ppos);
7116 Some fd
7118 in eat 0
7121 let remoteopen path =
7122 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7123 with exn ->
7124 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7125 None
7128 let () =
7129 let trimcachepath = ref "" in
7130 let rcmdpath = ref "" in
7131 Arg.parse
7132 (Arg.align
7133 [("-p", Arg.String (fun s -> state.password <- s),
7134 "<password> Set password");
7136 ("-f", Arg.String (fun s -> Config.fontpath := s),
7137 "<path> Set path to the user interface font");
7139 ("-c", Arg.String (fun s -> Config.confpath := s),
7140 "<path> Set path to the configuration file");
7142 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7143 "<path> Set path to the trim cache file");
7145 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7146 "<named-destination> Set named destination");
7148 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7150 ("-remote", Arg.String (fun s -> rcmdpath := s),
7151 "<path> Set path to the remote commands source");
7153 ("-v", Arg.Unit (fun () ->
7154 Printf.printf
7155 "%s\nconfiguration path: %s\n"
7156 (version ())
7157 Config.defconfpath
7159 exit 0), " Print version and exit");
7162 (fun s -> state.path <- s)
7163 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7165 if String.length state.path = 0
7166 then (prerr_endline "file name missing"; exit 1);
7168 if not (Config.load ())
7169 then prerr_endline "failed to load configuration";
7171 let globalkeyhash = findkeyhash conf "global" in
7172 let wsfd, winw, winh = Wsi.init (object
7173 method expose =
7174 state.wthack <- false;
7175 if nogeomcmds state.geomcmds || platform == Posx
7176 then display ()
7177 else (
7178 GlClear.color (scalecolor2 conf.bgcolor);
7179 GlClear.clear [`color];
7181 method display = display ()
7182 method reshape w h = reshape w h
7183 method mouse b d x y m = mouse b d x y m
7184 method motion x y = state.mpos <- (x, y); motion x y
7185 method pmotion x y = state.mpos <- (x, y); pmotion x y
7186 method key k m =
7187 let mascm = m land (
7188 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7189 ) in
7190 match state.keystate with
7191 | KSnone ->
7192 let km = k, mascm in
7193 begin
7194 match
7195 let modehash = state.uioh#modehash in
7196 try Hashtbl.find modehash km
7197 with Not_found ->
7198 try Hashtbl.find globalkeyhash km
7199 with Not_found -> KMinsrt (k, m)
7200 with
7201 | KMinsrt (k, m) -> keyboard k m
7202 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7203 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7205 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7206 List.iter (fun (k, m) -> keyboard k m) insrt;
7207 state.keystate <- KSnone
7208 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7209 state.keystate <- KSinto (keys, insrt)
7210 | _ ->
7211 state.keystate <- KSnone
7213 method enter x y = state.mpos <- (x, y); pmotion x y
7214 method leave = state.mpos <- (-1, -1)
7215 method winstate wsl = state.winstate <- wsl
7216 method quit = raise Quit
7217 end) conf.cwinw conf.cwinh (platform = Posx) in
7219 state.wsfd <- wsfd;
7221 if not (
7222 List.exists GlMisc.check_extension
7223 [ "GL_ARB_texture_rectangle"
7224 ; "GL_EXT_texture_recangle"
7225 ; "GL_NV_texture_rectangle" ]
7227 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7229 let cr, sw =
7230 match Ne.pipe () with
7231 | Ne.Exn exn ->
7232 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7233 exit 1
7234 | Ne.Res rw -> rw
7235 and sr, cw =
7236 match Ne.pipe () with
7237 | Ne.Exn exn ->
7238 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7239 exit 1
7240 | Ne.Res rw -> rw
7243 cloexec cr;
7244 cloexec sw;
7245 cloexec sr;
7246 cloexec cw;
7248 setcheckers conf.checkers;
7249 redirectstderr ();
7251 init (cr, cw) (
7252 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7253 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7254 !Config.fontpath, !trimcachepath,
7255 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7257 state.sr <- sr;
7258 state.sw <- sw;
7259 state.text <- "Opening " ^ (mbtoutf8 state.path);
7260 reshape winw winh;
7261 opendoc state.path state.password;
7262 state.uioh <- uioh;
7264 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7265 let optrfd =
7266 ref (
7267 if String.length !rcmdpath > 0
7268 then remoteopen !rcmdpath
7269 else None
7273 let rec loop deadline =
7274 let r =
7275 match state.errfd with
7276 | None -> [state.sr; state.wsfd]
7277 | Some fd -> [state.sr; state.wsfd; fd]
7279 let r =
7280 match !optrfd with
7281 | None -> r
7282 | Some fd -> fd :: r
7284 if state.redisplay && not state.wthack
7285 then (
7286 state.redisplay <- false;
7287 display ();
7289 let timeout =
7290 let now = now () in
7291 if deadline > now
7292 then (
7293 if deadline = infinity
7294 then ~-.1.0
7295 else max 0.0 (deadline -. now)
7297 else 0.0
7299 let r, _, _ =
7300 try Unix.select r [] [] timeout
7301 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7303 begin match r with
7304 | [] ->
7305 state.ghyll None;
7306 let newdeadline =
7307 if state.ghyll == noghyll
7308 then
7309 match state.autoscroll with
7310 | Some step when step != 0 ->
7311 let y = state.y + step in
7312 let y =
7313 if y < 0
7314 then state.maxy
7315 else if y >= state.maxy then 0 else y
7317 gotoy y;
7318 if state.mode = View
7319 then state.text <- "";
7320 deadline +. 0.01
7321 | _ -> infinity
7322 else deadline +. 0.01
7324 loop newdeadline
7326 | l ->
7327 let rec checkfds = function
7328 | [] -> ()
7329 | fd :: rest when fd = state.sr ->
7330 let cmd = readcmd state.sr in
7331 act cmd;
7332 checkfds rest
7334 | fd :: rest when fd = state.wsfd ->
7335 Wsi.readresp fd;
7336 checkfds rest
7338 | fd :: rest when Some fd = !optrfd ->
7339 begin match remote fd with
7340 | None -> optrfd := remoteopen !rcmdpath;
7341 | opt -> optrfd := opt
7342 end;
7343 checkfds rest
7345 | fd :: rest ->
7346 let s = String.create 80 in
7347 let n = tempfailureretry (Unix.read fd s 0) 80 in
7348 if conf.redirectstderr
7349 then (
7350 Buffer.add_substring state.errmsgs s 0 n;
7351 state.newerrmsgs <- true;
7352 state.redisplay <- true;
7354 else (
7355 prerr_string (String.sub s 0 n);
7356 flush stderr;
7358 checkfds rest
7360 checkfds l;
7361 let newdeadline =
7362 let deadline1 =
7363 if deadline = infinity
7364 then now () +. 0.01
7365 else deadline
7367 match state.autoscroll with
7368 | Some step when step != 0 -> deadline1
7369 | _ -> if state.ghyll == noghyll then infinity else deadline1
7371 loop newdeadline
7372 end;
7375 loop infinity;
7376 with Quit ->
7377 Config.save ();