Whoops
[llpp.git] / main.ml
blob0592440fd3f293b5b7943ccd50d01ed1b9efcc1c
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 postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable savebmarks : bool
316 ; mutable proportional : proportional
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 end;;
385 type mode =
386 | Birdseye of (conf * leftx * pageno * pageno * anchor)
387 | Textentry of (textentry * onleave)
388 | View
389 | LinkNav of linktarget
390 and onleave = leavetextentrystatus -> unit
391 and leavetextentrystatus = | Cancel | Confirm
392 and helpitem = string * int * action
393 and action =
394 | Noaction
395 | Action of (uioh -> uioh)
396 and linktarget =
397 | Ltexact of (pageno * int)
398 | Ltgendir of int
401 let isbirdseye = function Birdseye _ -> true | _ -> false;;
402 let istextentry = function Textentry _ -> true | _ -> false;;
404 type currently =
405 | Idle
406 | Loading of (page * gen)
407 | Tiling of (
408 page * opaque * colorspace * angle * gen * col * row * width * height
410 | Outlining of outline list
413 let emptykeyhash = Hashtbl.create 0;;
414 let nouioh : uioh = object (self)
415 method display = ()
416 method key _ _ = self
417 method button _ _ _ _ _ = self
418 method motion _ _ = self
419 method pmotion _ _ = self
420 method infochanged _ = ()
421 method scrollpw = (0, nan, nan)
422 method scrollph = (0, nan, nan)
423 method modehash = emptykeyhash
424 end;;
426 type state =
427 { mutable sr : Unix.file_descr
428 ; mutable sw : Unix.file_descr
429 ; mutable wsfd : Unix.file_descr
430 ; mutable errfd : Unix.file_descr option
431 ; mutable stderr : Unix.file_descr
432 ; mutable errmsgs : Buffer.t
433 ; mutable newerrmsgs : bool
434 ; mutable w : int
435 ; mutable x : int
436 ; mutable y : int
437 ; mutable scrollw : int
438 ; mutable hscrollh : int
439 ; mutable anchor : anchor
440 ; mutable ranchors : (string * string * anchor) list
441 ; mutable maxy : int
442 ; mutable layout : page list
443 ; pagemap : (pagemapkey, opaque) Hashtbl.t
444 ; tilemap : (tilemapkey, tile) Hashtbl.t
445 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
446 ; mutable pdims : (pageno * width * height * leftx) list
447 ; mutable pagecount : int
448 ; mutable currently : currently
449 ; mutable mstate : mstate
450 ; mutable searchpattern : string
451 ; mutable rects : (pageno * recttype * rect) list
452 ; mutable rects1 : (pageno * recttype * rect) list
453 ; mutable text : string
454 ; mutable winstate : Wsi.winstate list
455 ; mutable mode : mode
456 ; mutable uioh : uioh
457 ; mutable outlines : outline array
458 ; mutable bookmarks : outline list
459 ; mutable path : string
460 ; mutable password : string
461 ; mutable nameddest : string
462 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
463 ; mutable memused : memsize
464 ; mutable gen : gen
465 ; mutable throttle : (page list * int * float) option
466 ; mutable autoscroll : int option
467 ; mutable ghyll : (int option -> unit)
468 ; mutable help : helpitem array
469 ; mutable docinfo : (int * string) list
470 ; mutable texid : GlTex.texture_id option
471 ; hists : hists
472 ; mutable prevzoom : float
473 ; mutable progress : float
474 ; mutable redisplay : bool
475 ; mutable mpos : mpos
476 ; mutable keystate : keystate
477 ; mutable glinks : bool
478 ; mutable prevcolumns : (columns * float) option
479 ; mutable winw : int
480 ; mutable winh : int
481 ; mutable reprf : (unit -> unit)
483 and hists =
484 { pat : string circbuf
485 ; pag : string circbuf
486 ; nav : anchor circbuf
487 ; sel : string circbuf
491 let defconf =
492 { scrollbw = 7
493 ; scrollh = 12
494 ; icase = true
495 ; preload = true
496 ; pagebias = 0
497 ; verbose = false
498 ; debug = false
499 ; scrollstep = 24
500 ; hscrollstep = 24
501 ; maxhfit = true
502 ; crophack = false
503 ; autoscrollstep = 2
504 ; maxwait = None
505 ; hlinks = false
506 ; underinfo = false
507 ; interpagespace = 2
508 ; zoom = 1.0
509 ; presentation = false
510 ; angle = 0
511 ; cwinw = 900
512 ; cwinh = 900
513 ; savebmarks = true
514 ; proportional = true
515 ; trimmargins = false
516 ; trimfuzz = (0,0,0,0)
517 ; memlimit = 32 lsl 20
518 ; texcount = 256
519 ; sliceheight = 24
520 ; thumbw = 76
521 ; jumpback = true
522 ; bgcolor = (0.5, 0.5, 0.5)
523 ; bedefault = false
524 ; scrollbarinpm = true
525 ; tilew = 2048
526 ; tileh = 2048
527 ; mustoresize = 256 lsl 20
528 ; checkers = true
529 ; aalevel = 8
530 ; urilauncher =
531 (match platform with
532 | Plinux | Pfreebsd | Pdragonflybsd
533 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
534 | Posx -> "open \"%s\""
535 | Pcygwin -> "cygstart \"%s\""
536 | Punknown -> "echo %s")
537 ; pathlauncher = "lp \"%s\""
538 ; selcmd =
539 (match platform with
540 | Plinux | Pfreebsd | Pdragonflybsd
541 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
542 | Posx -> "pbcopy"
543 | Pcygwin -> "wsel"
544 | Punknown -> "cat")
545 ; colorspace = Rgb
546 ; invert = false
547 ; colorscale = 1.0
548 ; redirectstderr = false
549 ; ghyllscroll = None
550 ; columns = Csingle [||]
551 ; beyecolumns = None
552 ; updatecurs = false
553 ; hfsize = 12
554 ; pgscale = 1.0
555 ; usepbo = false
556 ; wheelbypage = false
557 ; stcmd = "echo SyncTex"
558 ; keyhashes =
559 let mk n = (n, Hashtbl.create 1) in
560 [ mk "global"
561 ; mk "info"
562 ; mk "help"
563 ; mk "outline"
564 ; mk "listview"
565 ; mk "birdseye"
566 ; mk "textentry"
567 ; mk "links"
568 ; mk "view"
573 let wtmode = ref false;;
575 let findkeyhash c name =
576 try List.assoc name c.keyhashes
577 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
580 let conf = { defconf with angle = defconf.angle };;
582 let pgscale h = truncate (float h *. conf.pgscale);;
584 type fontstate =
585 { mutable fontsize : int
586 ; mutable wwidth : float
587 ; mutable maxrows : int
591 let fstate =
592 { fontsize = 14
593 ; wwidth = nan
594 ; maxrows = -1
598 let geturl s =
599 let colonpos = try String.index s ':' with Not_found -> -1 in
600 let len = String.length s in
601 if colonpos >= 0 && colonpos + 3 < len
602 then (
603 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
604 then
605 let schemestartpos =
606 try String.rindex_from s colonpos ' '
607 with Not_found -> -1
609 let scheme =
610 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
612 match scheme with
613 | "http" | "ftp" | "mailto" ->
614 let epos =
615 try String.index_from s colonpos ' '
616 with Not_found -> len
618 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
619 | _ -> ""
620 else ""
622 else ""
625 let gotouri uri =
626 if String.length conf.urilauncher = 0
627 then print_endline uri
628 else (
629 let url = geturl uri in
630 if String.length url = 0
631 then print_endline uri
632 else
633 let re = Str.regexp "%s" in
634 let command = Str.global_replace re url conf.urilauncher in
635 try popen command []
636 with exn ->
637 Printf.eprintf
638 "failed to execute `%s': %s\n" command (exntos exn);
639 flush stderr;
643 let version () =
644 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
645 (platform_to_string platform) Sys.word_size Sys.ocaml_version
648 let makehelp () =
649 let strings = version () :: "" :: Help.keys in
650 Array.of_list (
651 List.map (fun s ->
652 let url = geturl s in
653 if String.length url > 0
654 then (s, 0, Action (fun u -> gotouri url; u))
655 else (s, 0, Noaction)
656 ) strings);
659 let noghyll _ = ();;
660 let firstgeomcmds = "", [];;
661 let noreprf () = ();;
663 let state =
664 { sr = Unix.stdin
665 ; sw = Unix.stdin
666 ; wsfd = Unix.stdin
667 ; errfd = None
668 ; stderr = Unix.stderr
669 ; errmsgs = Buffer.create 0
670 ; newerrmsgs = false
671 ; x = 0
672 ; y = 0
673 ; w = 0
674 ; scrollw = 0
675 ; hscrollh = 0
676 ; anchor = emptyanchor
677 ; ranchors = []
678 ; layout = []
679 ; maxy = max_int
680 ; tilelru = Queue.create ()
681 ; pagemap = Hashtbl.create 10
682 ; tilemap = Hashtbl.create 10
683 ; pdims = []
684 ; pagecount = 0
685 ; currently = Idle
686 ; mstate = Mnone
687 ; rects = []
688 ; rects1 = []
689 ; text = ""
690 ; mode = View
691 ; winstate = []
692 ; searchpattern = ""
693 ; outlines = [||]
694 ; bookmarks = []
695 ; path = ""
696 ; password = ""
697 ; nameddest = ""
698 ; geomcmds = firstgeomcmds
699 ; hists =
700 { nav = cbnew 10 emptyanchor
701 ; pat = cbnew 10 ""
702 ; pag = cbnew 10 ""
703 ; sel = cbnew 10 ""
705 ; memused = 0
706 ; gen = 0
707 ; throttle = None
708 ; autoscroll = None
709 ; ghyll = noghyll
710 ; help = makehelp ()
711 ; docinfo = []
712 ; texid = None
713 ; prevzoom = 1.0
714 ; progress = -1.0
715 ; uioh = nouioh
716 ; redisplay = true
717 ; mpos = (-1, -1)
718 ; keystate = KSnone
719 ; glinks = false
720 ; prevcolumns = None
721 ; winw = -1
722 ; winh = -1
723 ; reprf = noreprf
727 let setfontsize n =
728 fstate.fontsize <- n;
729 fstate.wwidth <- measurestr fstate.fontsize "w";
730 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
733 let vlog fmt =
734 if conf.verbose
735 then
736 Printf.kprintf prerr_endline fmt
737 else
738 Printf.kprintf ignore fmt
741 let launchpath () =
742 if String.length conf.pathlauncher = 0
743 then print_endline state.path
744 else (
745 let re = Str.regexp "%s" in
746 let command = Str.global_replace re state.path conf.pathlauncher in
747 try popen command []
748 with exn ->
749 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
750 flush stderr;
754 module Ne = struct
755 type 'a t = | Res of 'a | Exn of exn;;
757 let pipe () =
758 try Res (Unix.pipe ())
759 with exn -> Exn exn
762 let clo fd f =
763 try tempfailureretry Unix.close fd
764 with exn -> f (exntos exn)
767 let dup fd =
768 try Res (tempfailureretry Unix.dup fd)
769 with exn -> Exn exn
772 let dup2 fd1 fd2 =
773 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
774 with exn -> Exn exn
776 end;;
778 let redirectstderr () =
779 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
780 if conf.redirectstderr
781 then
782 match Ne.pipe () with
783 | Ne.Exn exn ->
784 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
786 | Ne.Res (r, w) ->
787 begin match Ne.dup Unix.stderr with
788 | Ne.Exn exn ->
789 dolog "failed to dup stderr: %s" (exntos exn);
790 Ne.clo r (clofail "pipe/r");
791 Ne.clo w (clofail "pipe/w");
793 | Ne.Res dupstderr ->
794 begin match Ne.dup2 w Unix.stderr with
795 | Ne.Exn exn ->
796 dolog "failed to dup2 to stderr: %s" (exntos exn);
797 Ne.clo dupstderr (clofail "stderr duplicate");
798 Ne.clo r (clofail "redir pipe/r");
799 Ne.clo w (clofail "redir pipe/w");
801 | Ne.Res () ->
802 state.stderr <- dupstderr;
803 state.errfd <- Some r;
804 end;
806 else (
807 state.newerrmsgs <- false;
808 begin match state.errfd with
809 | Some fd ->
810 begin match Ne.dup2 state.stderr Unix.stderr with
811 | Ne.Exn exn ->
812 dolog "failed to dup2 original stderr: %s" (exntos exn)
813 | Ne.Res () ->
814 Ne.clo fd (clofail "dup of stderr");
815 state.errfd <- None;
816 end;
817 | None -> ()
818 end;
819 prerr_string (Buffer.contents state.errmsgs);
820 flush stderr;
821 Buffer.clear state.errmsgs;
825 module G =
826 struct
827 let postRedisplay who =
828 if conf.verbose
829 then prerr_endline ("redisplay for " ^ who);
830 state.redisplay <- true;
832 end;;
834 let getopaque pageno =
835 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
836 with Not_found -> None
839 let putopaque pageno opaque =
840 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
843 let pagetranslatepoint l x y =
844 let dy = y - l.pagedispy in
845 let y = dy + l.pagey in
846 let dx = x - l.pagedispx in
847 let x = dx + l.pagex in
848 (x, y);
851 let onppundermouse g x y d =
852 let rec f = function
853 | l :: rest ->
854 begin match getopaque l.pageno with
855 | Some opaque ->
856 let x0 = l.pagedispx in
857 let x1 = x0 + l.pagevw in
858 let y0 = l.pagedispy in
859 let y1 = y0 + l.pagevh in
860 if y >= y0 && y <= y1 && x >= x0 && x <= x1
861 then
862 let px, py = pagetranslatepoint l x y in
863 match g opaque l px py with
864 | Some res -> res
865 | None -> f rest
866 else f rest
867 | _ ->
868 f rest
870 | [] -> d
872 f state.layout
875 let getunder x y =
876 let g opaque _ px py =
877 match whatsunder opaque px py with
878 | Unone -> None
879 | under -> Some under
881 onppundermouse g x y Unone
884 let unproject x y =
885 let g opaque l x y =
886 match unproject opaque x y with
887 | Some (x, y) -> Some (Some (l.pageno, x, y))
888 | None -> None
890 onppundermouse g x y None;
893 let showtext c s =
894 state.text <- Printf.sprintf "%c%s" c s;
895 G.postRedisplay "showtext";
898 let undertext = function
899 | Unone -> "none"
900 | Ulinkuri s -> s
901 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
902 | Utext s -> "font: " ^ s
903 | Uunexpected s -> "unexpected: " ^ s
904 | Ulaunch s -> "launch: " ^ s
905 | Unamed s -> "named: " ^ s
906 | Uremote (filename, pageno) ->
907 Printf.sprintf "%s: page %d" filename (pageno+1)
910 let updateunder x y =
911 match getunder x y with
912 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
913 | Ulinkuri uri ->
914 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
915 Wsi.setcursor Wsi.CURSOR_INFO
916 | Ulinkgoto (pageno, _) ->
917 if conf.underinfo
918 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
919 Wsi.setcursor Wsi.CURSOR_INFO
920 | Utext s ->
921 if conf.underinfo then showtext 'f' ("ont: " ^ s);
922 Wsi.setcursor Wsi.CURSOR_TEXT
923 | Uunexpected s ->
924 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
925 Wsi.setcursor Wsi.CURSOR_INHERIT
926 | Ulaunch s ->
927 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
928 Wsi.setcursor Wsi.CURSOR_INHERIT
929 | Unamed s ->
930 if conf.underinfo then showtext 'n' ("amed: " ^ s);
931 Wsi.setcursor Wsi.CURSOR_INHERIT
932 | Uremote (filename, pageno) ->
933 if conf.underinfo then showtext 'r'
934 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
935 Wsi.setcursor Wsi.CURSOR_INFO
938 let showlinktype under =
939 if conf.underinfo
940 then
941 match under with
942 | Unone -> ()
943 | under ->
944 let s = undertext under in
945 showtext ' ' s
948 let addchar s c =
949 let b = Buffer.create (String.length s + 1) in
950 Buffer.add_string b s;
951 Buffer.add_char b c;
952 Buffer.contents b;
955 let colorspace_of_string s =
956 match String.lowercase s with
957 | "rgb" -> Rgb
958 | "bgr" -> Bgr
959 | "gray" -> Gray
960 | _ -> failwith "invalid colorspace"
963 let int_of_colorspace = function
964 | Rgb -> 0
965 | Bgr -> 1
966 | Gray -> 2
969 let colorspace_of_int = function
970 | 0 -> Rgb
971 | 1 -> Bgr
972 | 2 -> Gray
973 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
976 let colorspace_to_string = function
977 | Rgb -> "rgb"
978 | Bgr -> "bgr"
979 | Gray -> "gray"
982 let intentry_with_suffix text key =
983 let c =
984 if key >= 32 && key < 127
985 then Char.chr key
986 else '\000'
988 match Char.lowercase c with
989 | '0' .. '9' ->
990 let text = addchar text c in
991 TEcont text
993 | 'k' | 'm' | 'g' ->
994 let text = addchar text c in
995 TEcont text
997 | _ ->
998 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
999 TEcont text
1002 let multicolumns_to_string (n, a, b) =
1003 if a = 0 && b = 0
1004 then Printf.sprintf "%d" n
1005 else Printf.sprintf "%d,%d,%d" n a b;
1008 let multicolumns_of_string s =
1010 (int_of_string s, 0, 0)
1011 with _ ->
1012 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1013 if a > 1 || b > 1
1014 then failwith "subtly broken"; (n, a, b)
1018 let readcmd fd =
1019 let s = "xxxx" in
1020 let n = tempfailureretry (Unix.read fd s 0) 4 in
1021 if n != 4 then failwith "incomplete read(len)";
1022 let len = 0
1023 lor (Char.code s.[0] lsl 24)
1024 lor (Char.code s.[1] lsl 16)
1025 lor (Char.code s.[2] lsl 8)
1026 lor (Char.code s.[3] lsl 0)
1028 let s = String.create len in
1029 let n = tempfailureretry (Unix.read fd s 0) len in
1030 if n != len then failwith "incomplete read(data)";
1034 let btod b = if b then 1 else 0;;
1036 let wcmd fmt =
1037 let b = Buffer.create 16 in
1038 Buffer.add_string b "llll";
1039 Printf.kbprintf
1040 (fun b ->
1041 let s = Buffer.contents b in
1042 let n = String.length s in
1043 let len = n - 4 in
1044 (* dolog "wcmd %S" (String.sub s 4 len); *)
1045 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1046 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1047 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1048 s.[3] <- Char.chr (len land 0xff);
1049 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1050 if n' != n then failwith "write failed";
1051 ) b fmt;
1054 let calcips h =
1055 let d = state.winh - h in
1056 max conf.interpagespace ((d + 1) / 2)
1059 let rowyh (c, coverA, coverB) b n =
1060 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1061 then
1062 let _, _, vy, (_, _, h, _) = b.(n) in
1063 (vy, h)
1064 else
1065 let n' = n - coverA in
1066 let d = n' mod c in
1067 let s = n - d in
1068 let e = min state.pagecount (s + c) in
1069 let rec find m miny maxh = if m = e then miny, maxh else
1070 let _, _, y, (_, _, h, _) = b.(m) in
1071 let miny = min miny y in
1072 let maxh = max maxh h in
1073 find (m+1) miny maxh
1074 in find s max_int 0
1077 let calcheight () =
1078 match conf.columns with
1079 | Cmulti ((_, _, _) as cl, b) ->
1080 if Array.length b > 0
1081 then
1082 let y, h = rowyh cl b (Array.length b - 1) in
1083 y + h + (if conf.presentation then calcips h else 0)
1084 else 0
1085 | Csingle b ->
1086 if Array.length b > 0
1087 then
1088 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1089 y + h + (if conf.presentation then calcips h else 0)
1090 else 0
1091 | Csplit (_, b) ->
1092 if Array.length b > 0
1093 then
1094 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1095 y + h
1096 else 0
1099 let getpageyh pageno =
1100 let pageno = bound pageno 0 (state.pagecount-1) in
1101 match conf.columns with
1102 | Csingle b ->
1103 if Array.length b = 0
1104 then 0, 0
1105 else
1106 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1107 let y =
1108 if conf.presentation
1109 then y - calcips h
1110 else y
1112 y, h
1113 | Cmulti (cl, b) ->
1114 if Array.length b = 0
1115 then 0, 0
1116 else
1117 let y, h = rowyh cl b pageno in
1118 let y =
1119 if conf.presentation
1120 then y - calcips h
1121 else y
1123 y, h
1124 | Csplit (c, b) ->
1125 if Array.length b = 0
1126 then 0, 0
1127 else
1128 let n = pageno*c in
1129 let (_, _, y, (_, _, h, _)) = b.(n) in
1130 y, h
1133 let getpagedim pageno =
1134 let rec f ppdim l =
1135 match l with
1136 | (n, _, _, _) as pdim :: rest ->
1137 if n >= pageno
1138 then (if n = pageno then pdim else ppdim)
1139 else f pdim rest
1141 | [] -> ppdim
1143 f (-1, -1, -1, -1) state.pdims
1146 let getpagey pageno = fst (getpageyh pageno);;
1148 let nogeomcmds cmds =
1149 match cmds with
1150 | s, [] -> String.length s = 0
1151 | _ -> false
1154 let page_of_y y =
1155 let ((c, coverA, coverB) as cl), b =
1156 match conf.columns with
1157 | Csingle b -> (1, 0, 0), b
1158 | Cmulti (c, b) -> c, b
1159 | Csplit (_, b) -> (1, 0, 0), b
1161 if Array.length b = 0
1162 then -1
1163 else
1164 let rec bsearch nmin nmax =
1165 if nmin > nmax
1166 then bound nmin 0 (state.pagecount-1)
1167 else
1168 let n = (nmax + nmin) / 2 in
1169 let vy, h = rowyh cl b n in
1170 let y0, y1 =
1171 if conf.presentation
1172 then
1173 let ips = calcips h in
1174 let y0 = vy - ips in
1175 let y1 = vy + h + ips in
1176 y0, y1
1177 else (
1178 if n = 0
1179 then 0, vy + h + conf.interpagespace
1180 else
1181 let y0 = vy - conf.interpagespace in
1182 y0, y0 + h + conf.interpagespace
1185 if y >= y0 && y < y1
1186 then (
1187 if c = 1
1188 then n
1189 else (
1190 if n > coverA
1191 then
1192 if n < state.pagecount - coverB
1193 then ((n-coverA)/c)*c + coverA
1194 else n
1195 else n
1198 else (
1199 if y > y0
1200 then bsearch (n+1) nmax
1201 else bsearch nmin (n-1)
1204 let r = bsearch 0 (state.pagecount-1) in
1208 let layoutN ((columns, coverA, coverB), b) y sh =
1209 let sh = sh - state.hscrollh in
1210 let rec fold accu n =
1211 if n = Array.length b
1212 then accu
1213 else
1214 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1215 if (vy - y) > sh &&
1216 (n = coverA - 1
1217 || n = state.pagecount - coverB
1218 || (n - coverA) mod columns = columns - 1)
1219 then accu
1220 else
1221 let accu =
1222 if vy + h > y
1223 then
1224 let pagey = max 0 (y - vy) in
1225 let pagedispy = if pagey > 0 then 0 else vy - y in
1226 let pagedispx, pagex =
1227 let pdx =
1228 if n = coverA - 1 || n = state.pagecount - coverB
1229 then state.x + (state.winw - state.scrollw - w) / 2
1230 else dx + xoff + state.x
1232 if pdx < 0
1233 then 0, -pdx
1234 else pdx, 0
1236 let pagevw =
1237 let vw = state.winw - state.scrollw - pagedispx in
1238 let pw = w - pagex in
1239 min vw pw
1241 let pagevh = min (h - pagey) (sh - pagedispy) in
1242 if pagevw > 0 && pagevh > 0
1243 then
1244 let e =
1245 { pageno = n
1246 ; pagedimno = pdimno
1247 ; pagew = w
1248 ; pageh = h
1249 ; pagex = pagex
1250 ; pagey = pagey
1251 ; pagevw = pagevw
1252 ; pagevh = pagevh
1253 ; pagedispx = pagedispx
1254 ; pagedispy = pagedispy
1255 ; pagecol = 0
1258 e :: accu
1259 else
1260 accu
1261 else
1262 accu
1264 fold accu (n+1)
1266 List.rev (fold [] (page_of_y y));
1269 let layoutS (columns, b) y sh =
1270 let sh = sh - state.hscrollh in
1271 let rec fold accu n =
1272 if n = Array.length b
1273 then accu
1274 else
1275 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1276 if (vy - y) > sh
1277 then accu
1278 else
1279 let accu =
1280 if vy + pageh > y
1281 then
1282 let x = xoff + state.x in
1283 let pagey = max 0 (y - vy) in
1284 let pagedispy = if pagey > 0 then 0 else vy - y in
1285 let pagedispx, pagex =
1286 if px = 0
1287 then (
1288 if x < 0
1289 then 0, -x
1290 else x, 0
1292 else (
1293 let px = px - x in
1294 if px < 0
1295 then -px, 0
1296 else 0, px
1299 let pagecolw = pagew/columns in
1300 let pagedispx =
1301 if pagecolw < state.winw
1302 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1303 else pagedispx
1305 let pagevw =
1306 let vw = state.winw - pagedispx - state.scrollw in
1307 let pw = pagew - pagex in
1308 min vw pw
1310 let pagevw = min pagevw pagecolw in
1311 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1312 if pagevw > 0 && pagevh > 0
1313 then
1314 let e =
1315 { pageno = n/columns
1316 ; pagedimno = pdimno
1317 ; pagew = pagew
1318 ; pageh = pageh
1319 ; pagex = pagex
1320 ; pagey = pagey
1321 ; pagevw = pagevw
1322 ; pagevh = pagevh
1323 ; pagedispx = pagedispx
1324 ; pagedispy = pagedispy
1325 ; pagecol = n mod columns
1328 e :: accu
1329 else
1330 accu
1331 else
1332 accu
1334 fold accu (n+1)
1336 List.rev (fold [] 0)
1339 let layout y sh =
1340 if nogeomcmds state.geomcmds
1341 then
1342 match conf.columns with
1343 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1344 | Cmulti c -> layoutN c y sh
1345 | Csplit s -> layoutS s y sh
1346 else []
1349 let clamp incr =
1350 let y = state.y + incr in
1351 let y = max 0 y in
1352 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1356 let itertiles l f =
1357 let tilex = l.pagex mod conf.tilew in
1358 let tiley = l.pagey mod conf.tileh in
1360 let col = l.pagex / conf.tilew in
1361 let row = l.pagey / conf.tileh in
1363 let rec rowloop row y0 dispy h =
1364 if h = 0
1365 then ()
1366 else (
1367 let dh = conf.tileh - y0 in
1368 let dh = min h dh in
1369 let rec colloop col x0 dispx w =
1370 if w = 0
1371 then ()
1372 else (
1373 let dw = conf.tilew - x0 in
1374 let dw = min w dw in
1376 f col row dispx dispy x0 y0 dw dh;
1377 colloop (col+1) 0 (dispx+dw) (w-dw)
1380 colloop col tilex l.pagedispx l.pagevw;
1381 rowloop (row+1) 0 (dispy+dh) (h-dh)
1384 if l.pagevw > 0 && l.pagevh > 0
1385 then rowloop row tiley l.pagedispy l.pagevh;
1388 let gettileopaque l col row =
1389 let key =
1390 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1392 try Some (Hashtbl.find state.tilemap key)
1393 with Not_found -> None
1396 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1397 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1398 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1401 let drawtiles l color =
1402 GlDraw.color color;
1403 let f col row x y tilex tiley w h =
1404 match gettileopaque l col row with
1405 | Some (opaque, _, t) ->
1406 let params = x, y, w, h, tilex, tiley in
1407 if conf.invert
1408 then (
1409 Gl.enable `blend;
1410 GlFunc.blend_func `zero `one_minus_src_color;
1412 drawtile params opaque;
1413 if conf.invert
1414 then Gl.disable `blend;
1415 if conf.debug
1416 then (
1417 let s = Printf.sprintf
1418 "%d[%d,%d] %f sec"
1419 l.pageno col row t
1421 let w = measurestr fstate.fontsize s in
1422 GlMisc.push_attrib [`current];
1423 GlDraw.color (0.0, 0.0, 0.0);
1424 GlDraw.rect
1425 (float (x-2), float (y-2))
1426 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1427 GlDraw.color (1.0, 1.0, 1.0);
1428 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1429 GlMisc.pop_attrib ();
1432 | _ ->
1433 let w =
1434 let lw = state.winw - state.scrollw - x in
1435 min lw w
1436 and h =
1437 let lh = state.winh - y in
1438 min lh h
1440 begin match state.texid with
1441 | Some id ->
1442 Gl.enable `texture_2d;
1443 GlTex.bind_texture `texture_2d id;
1444 let x0 = float x
1445 and y0 = float y
1446 and x1 = float (x+w)
1447 and y1 = float (y+h) in
1449 let tw = float w /. 16.0
1450 and th = float h /. 16.0 in
1451 let tx0 = float tilex /. 16.0
1452 and ty0 = float tiley /. 16.0 in
1453 let tx1 = tx0 +. tw
1454 and ty1 = ty0 +. th in
1455 GlDraw.begins `quads;
1456 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1457 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1458 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1459 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1460 GlDraw.ends ();
1462 Gl.disable `texture_2d;
1463 | None ->
1464 GlDraw.color (1.0, 1.0, 1.0);
1465 GlDraw.rect
1466 (float x, float y)
1467 (float (x+w), float (y+h));
1468 end;
1469 if w > 128 && h > fstate.fontsize + 10
1470 then (
1471 GlDraw.color (0.0, 0.0, 0.0);
1472 let c, r =
1473 if conf.verbose
1474 then (col*conf.tilew, row*conf.tileh)
1475 else col, row
1477 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1479 GlDraw.color color;
1481 itertiles l f
1484 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1486 let tilevisible1 l x y =
1487 let ax0 = l.pagex
1488 and ax1 = l.pagex + l.pagevw
1489 and ay0 = l.pagey
1490 and ay1 = l.pagey + l.pagevh in
1492 let bx0 = x
1493 and by0 = y in
1494 let bx1 = min (bx0 + conf.tilew) l.pagew
1495 and by1 = min (by0 + conf.tileh) l.pageh in
1497 let rx0 = max ax0 bx0
1498 and ry0 = max ay0 by0
1499 and rx1 = min ax1 bx1
1500 and ry1 = min ay1 by1 in
1502 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1503 nonemptyintersection
1506 let tilevisible layout n x y =
1507 let rec findpageinlayout m = function
1508 | l :: rest when l.pageno = n ->
1509 tilevisible1 l x y || (
1510 match conf.columns with
1511 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1512 | _ -> false
1514 | _ :: rest -> findpageinlayout 0 rest
1515 | [] -> false
1517 findpageinlayout 0 layout;
1520 let tileready l x y =
1521 tilevisible1 l x y &&
1522 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1525 let tilepage n p layout =
1526 let rec loop = function
1527 | l :: rest ->
1528 if l.pageno = n
1529 then
1530 let f col row _ _ _ _ _ _ =
1531 if state.currently = Idle
1532 then
1533 match gettileopaque l col row with
1534 | Some _ -> ()
1535 | None ->
1536 let x = col*conf.tilew
1537 and y = row*conf.tileh in
1538 let w =
1539 let w = l.pagew - x in
1540 min w conf.tilew
1542 let h =
1543 let h = l.pageh - y in
1544 min h conf.tileh
1546 let pbo =
1547 if conf.usepbo
1548 then getpbo w h conf.colorspace
1549 else "0"
1551 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1552 state.currently <-
1553 Tiling (
1554 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1555 conf.tilew, conf.tileh
1558 itertiles l f;
1559 else
1560 loop rest
1562 | [] -> ()
1564 if nogeomcmds state.geomcmds
1565 then loop layout;
1568 let preloadlayout y =
1569 let y = if y < state.winh then 0 else y - state.winh in
1570 let h = state.winh*3 in
1571 layout y h;
1574 let load pages =
1575 let rec loop pages =
1576 if state.currently != Idle
1577 then ()
1578 else
1579 match pages with
1580 | l :: rest ->
1581 begin match getopaque l.pageno with
1582 | None ->
1583 wcmd "page %d %d" l.pageno l.pagedimno;
1584 state.currently <- Loading (l, state.gen);
1585 | Some opaque ->
1586 tilepage l.pageno opaque pages;
1587 loop rest
1588 end;
1589 | _ -> ()
1591 if nogeomcmds state.geomcmds
1592 then loop pages
1595 let preload pages =
1596 load pages;
1597 if conf.preload && state.currently = Idle
1598 then load (preloadlayout state.y);
1601 let layoutready layout =
1602 let rec fold all ls =
1603 all && match ls with
1604 | l :: rest ->
1605 let seen = ref false in
1606 let allvisible = ref true in
1607 let foo col row _ _ _ _ _ _ =
1608 seen := true;
1609 allvisible := !allvisible &&
1610 begin match gettileopaque l col row with
1611 | Some _ -> true
1612 | None -> false
1615 itertiles l foo;
1616 fold (!seen && !allvisible) rest
1617 | [] -> true
1619 let alltilesvisible = fold true layout in
1620 alltilesvisible;
1623 let gotoy y =
1624 let y = bound y 0 state.maxy in
1625 let y, layout, proceed =
1626 match conf.maxwait with
1627 | Some time when state.ghyll == noghyll ->
1628 begin match state.throttle with
1629 | None ->
1630 let layout = layout y state.winh in
1631 let ready = layoutready layout in
1632 if not ready
1633 then (
1634 load layout;
1635 state.throttle <- Some (layout, y, now ());
1637 else G.postRedisplay "gotoy showall (None)";
1638 y, layout, ready
1639 | Some (_, _, started) ->
1640 let dt = now () -. started in
1641 if dt > time
1642 then (
1643 state.throttle <- None;
1644 let layout = layout y state.winh in
1645 load layout;
1646 G.postRedisplay "maxwait";
1647 y, layout, true
1649 else -1, [], false
1652 | _ ->
1653 let layout = layout y state.winh in
1654 if not !wtmode || layoutready layout
1655 then G.postRedisplay "gotoy ready";
1656 y, layout, true
1658 if proceed
1659 then (
1660 state.y <- y;
1661 state.layout <- layout;
1662 begin match state.mode with
1663 | LinkNav (Ltexact (pageno, linkno)) ->
1664 let rec loop = function
1665 | [] ->
1666 state.mode <- LinkNav (Ltgendir 0)
1667 | l :: _ when l.pageno = pageno ->
1668 begin match getopaque pageno with
1669 | None ->
1670 state.mode <- LinkNav (Ltgendir 0)
1671 | Some opaque ->
1672 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1673 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1674 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1675 then state.mode <- LinkNav (Ltgendir 0)
1677 | _ :: rest -> loop rest
1679 loop layout
1680 | _ -> ()
1681 end;
1682 begin match state.mode with
1683 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1684 if not (pagevisible layout pageno)
1685 then (
1686 match state.layout with
1687 | [] -> ()
1688 | l :: _ ->
1689 state.mode <- Birdseye (
1690 conf, leftx, l.pageno, hooverpageno, anchor
1693 | LinkNav (Ltgendir dir as lt) ->
1694 let linknav =
1695 let rec loop = function
1696 | [] -> lt
1697 | l :: rest ->
1698 match getopaque l.pageno with
1699 | None -> loop rest
1700 | Some opaque ->
1701 let link =
1702 let ld =
1703 if dir = 0
1704 then LDfirstvisible (l.pagex, l.pagey, dir)
1705 else (
1706 if dir > 0 then LDfirst else LDlast
1709 findlink opaque ld
1711 match link with
1712 | Lnotfound -> loop rest
1713 | Lfound n ->
1714 showlinktype (getlink opaque n);
1715 Ltexact (l.pageno, n)
1717 loop state.layout
1719 state.mode <- LinkNav linknav
1720 | _ -> ()
1721 end;
1722 preload layout;
1724 state.ghyll <- noghyll;
1725 if conf.updatecurs
1726 then (
1727 let mx, my = state.mpos in
1728 updateunder mx my;
1732 let conttiling pageno opaque =
1733 tilepage pageno opaque
1734 (if conf.preload then preloadlayout state.y else state.layout)
1737 let gotoy_and_clear_text y =
1738 if not conf.verbose then state.text <- "";
1739 gotoy y;
1742 let getanchor1 l =
1743 let top =
1744 let coloff = l.pagecol * l.pageh in
1745 float (l.pagey + coloff) /. float l.pageh
1747 let dtop =
1748 if l.pagedispy = 0
1749 then
1751 else
1752 if conf.presentation
1753 then float l.pagedispy /. float (calcips l.pageh)
1754 else float l.pagedispy /. float conf.interpagespace
1756 (l.pageno, top, dtop)
1759 let getanchor () =
1760 match state.layout with
1761 | l :: _ -> getanchor1 l
1762 | [] ->
1763 let n = page_of_y state.y in
1764 if n = -1
1765 then state.anchor
1766 else
1767 let y, h = getpageyh n in
1768 let dy = y - state.y in
1769 let dtop =
1770 if conf.presentation
1771 then
1772 let ips = calcips h in
1773 float (dy + ips) /. float ips
1774 else
1775 float dy /. float conf.interpagespace
1777 (n, 0.0, dtop)
1780 let getanchory (n, top, dtop) =
1781 let y, h = getpageyh n in
1782 if conf.presentation
1783 then
1784 let ips = calcips h in
1785 y + truncate (top*.float h -. dtop*.float ips) + ips;
1786 else
1787 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1790 let gotoanchor anchor =
1791 gotoy (getanchory anchor);
1794 let addnav () =
1795 cbput state.hists.nav (getanchor ());
1798 let getnav dir =
1799 let anchor = cbgetc state.hists.nav dir in
1800 getanchory anchor;
1803 let gotoghyll y =
1804 let scroll f n a b =
1805 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1806 let snake f a b =
1807 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1808 if f < a
1809 then s (float f /. float a)
1810 else (
1811 if f > b
1812 then 1.0 -. s ((float (f-b) /. float (n-b)))
1813 else 1.0
1816 snake f a b
1817 and summa f n a b =
1818 (* courtesy:
1819 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1820 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1821 let iv1 = iv f in
1822 let ins = float a *. iv1
1823 and outs = float (n-b) *. iv1 in
1824 let ones = b - a in
1825 ins +. outs +. float ones
1827 let rec set (_N, _A, _B) y sy =
1828 let sum = summa 1.0 _N _A _B in
1829 let dy = float (y - sy) in
1830 state.ghyll <- (
1831 let rec gf n y1 o =
1832 if n >= _N
1833 then state.ghyll <- noghyll
1834 else
1835 let go n =
1836 let s = scroll n _N _A _B in
1837 let y1 = y1 +. ((s *. dy) /. sum) in
1838 gotoy_and_clear_text (truncate y1);
1839 state.ghyll <- gf (n+1) y1;
1841 match o with
1842 | None -> go n
1843 | Some y' -> set (_N/2, 1, 1) y' state.y
1845 gf 0 (float state.y)
1848 match conf.ghyllscroll with
1849 | None ->
1850 gotoy_and_clear_text y
1851 | Some nab ->
1852 if state.ghyll == noghyll
1853 then set nab y state.y
1854 else state.ghyll (Some y)
1857 let gotopage n top =
1858 let y, h = getpageyh n in
1859 let y = y + (truncate (top *. float h)) in
1860 gotoghyll y
1863 let gotopage1 n top =
1864 let y = getpagey n in
1865 let y = y + top in
1866 gotoghyll y
1869 let invalidate s f =
1870 state.layout <- [];
1871 state.pdims <- [];
1872 state.rects <- [];
1873 state.rects1 <- [];
1874 match state.geomcmds with
1875 | ps, [] when String.length ps = 0 ->
1876 f ();
1877 state.geomcmds <- s, [];
1879 | ps, [] ->
1880 state.geomcmds <- ps, [s, f];
1882 | ps, (s', _) :: rest when s' = s ->
1883 state.geomcmds <- ps, ((s, f) :: rest);
1885 | ps, cmds ->
1886 state.geomcmds <- ps, ((s, f) :: cmds);
1889 let flushpages () =
1890 Hashtbl.iter (fun _ opaque ->
1891 wcmd "freepage %s" opaque;
1892 ) state.pagemap;
1893 Hashtbl.clear state.pagemap;
1896 let flushtiles () =
1897 if not (Queue.is_empty state.tilelru)
1898 then (
1899 Queue.iter (fun (k, p, s) ->
1900 wcmd "freetile %s" p;
1901 state.memused <- state.memused - s;
1902 Hashtbl.remove state.tilemap k;
1903 ) state.tilelru;
1904 state.uioh#infochanged Memused;
1905 Queue.clear state.tilelru;
1907 load state.layout;
1910 let opendoc path password =
1911 state.path <- path;
1912 state.password <- password;
1913 state.gen <- state.gen + 1;
1914 state.docinfo <- [];
1916 flushpages ();
1917 setaalevel conf.aalevel;
1918 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1919 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1920 invalidate "reqlayout"
1921 (fun () ->
1922 wcmd "reqlayout %d %d %s\000"
1923 conf.angle (btod conf.proportional) state.nameddest;
1927 let reload () =
1928 state.anchor <- getanchor ();
1929 opendoc state.path state.password;
1932 let scalecolor c =
1933 let c = c *. conf.colorscale in
1934 (c, c, c);
1937 let scalecolor2 (r, g, b) =
1938 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1941 let docolumns = function
1942 | Csingle _ ->
1943 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1944 let rec loop pageno pdimno pdim y ph pdims =
1945 if pageno = state.pagecount
1946 then ()
1947 else
1948 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1949 match pdims with
1950 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1951 pdimno+1, pdim, rest
1952 | _ ->
1953 pdimno, pdim, pdims
1955 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1956 let y = y +
1957 (if conf.presentation
1958 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1959 else (if pageno = 0 then 0 else conf.interpagespace)
1962 a.(pageno) <- (pdimno, x, y, pdim);
1963 loop (pageno+1) pdimno pdim (y + h) h pdims
1965 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1966 conf.columns <- Csingle a;
1968 | Cmulti ((columns, coverA, coverB), _) ->
1969 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1970 let rec loop pageno pdimno pdim x y rowh pdims =
1971 let rec fixrow m = if m = pageno then () else
1972 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1973 if h < rowh
1974 then (
1975 let y = y + (rowh - h) / 2 in
1976 a.(m) <- (pdimno, x, y, pdim);
1978 fixrow (m+1)
1980 if pageno = state.pagecount
1981 then fixrow (((pageno - 1) / columns) * columns)
1982 else
1983 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1984 match pdims with
1985 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1986 pdimno+1, pdim, rest
1987 | _ ->
1988 pdimno, pdim, pdims
1990 let x, y, rowh' =
1991 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1992 then (
1993 let x = (state.winw - state.scrollw - w) / 2 in
1994 let ips =
1995 if conf.presentation then calcips h else conf.interpagespace in
1996 x, y + ips + rowh, h
1998 else (
1999 if (pageno - coverA) mod columns = 0
2000 then (
2001 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2002 let y =
2003 if conf.presentation
2004 then
2005 let ips = calcips h in
2006 y + (if pageno = 0 then 0 else calcips rowh + ips)
2007 else
2008 y + (if pageno = 0 then 0 else conf.interpagespace)
2010 x, y + rowh, h
2012 else x, y, max rowh h
2015 let y =
2016 if pageno > 1 && (pageno - coverA) mod columns = 0
2017 then (
2018 let y =
2019 if pageno = columns && conf.presentation
2020 then (
2021 let ips = calcips rowh in
2022 for i = 0 to pred columns
2024 let (pdimno, x, y, pdim) = a.(i) in
2025 a.(i) <- (pdimno, x, y+ips, pdim)
2026 done;
2027 y+ips;
2029 else y
2031 fixrow (pageno - columns);
2034 else y
2036 a.(pageno) <- (pdimno, x, y, pdim);
2037 let x = x + w + xoff*2 + conf.interpagespace in
2038 loop (pageno+1) pdimno pdim x y rowh' pdims
2040 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2041 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2043 | Csplit (c, _) ->
2044 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2045 let rec loop pageno pdimno pdim y pdims =
2046 if pageno = state.pagecount
2047 then ()
2048 else
2049 let pdimno, ((_, w, h, _) as pdim), pdims =
2050 match pdims with
2051 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2052 pdimno+1, pdim, rest
2053 | _ ->
2054 pdimno, pdim, pdims
2056 let cw = w / c in
2057 let rec loop1 n x y =
2058 if n = c then y else (
2059 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2060 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2063 let y = loop1 0 0 y in
2064 loop (pageno+1) pdimno pdim y pdims
2066 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2067 conf.columns <- Csplit (c, a);
2070 let represent () =
2071 docolumns conf.columns;
2072 state.maxy <- calcheight ();
2073 state.hscrollh <-
2074 if state.x = 0 && state.w <= state.winw - state.scrollw
2075 then 0
2076 else state.scrollw
2078 if state.reprf == noreprf
2079 then (
2080 match state.mode with
2081 | Birdseye (_, _, pageno, _, _) ->
2082 let y, h = getpageyh pageno in
2083 let top = (state.winh - h) / 2 in
2084 gotoy (max 0 (y - top))
2085 | _ -> gotoanchor state.anchor
2087 else (
2088 state.reprf ();
2089 state.reprf <- noreprf;
2093 let reshape w h =
2094 GlDraw.viewport 0 0 w h;
2095 let firsttime = state.geomcmds == firstgeomcmds in
2096 if not firsttime && nogeomcmds state.geomcmds
2097 then state.anchor <- getanchor ();
2099 state.winw <- w;
2100 let w = truncate (float w *. conf.zoom) - state.scrollw in
2101 let w = max w 2 in
2102 state.winh <- h;
2103 setfontsize fstate.fontsize;
2104 GlMat.mode `modelview;
2105 GlMat.load_identity ();
2107 GlMat.mode `projection;
2108 GlMat.load_identity ();
2109 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2110 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2111 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2113 let relx =
2114 if conf.zoom <= 1.0
2115 then 0.0
2116 else float state.x /. float state.w
2118 invalidate "geometry"
2119 (fun () ->
2120 state.w <- w;
2121 if not firsttime
2122 then state.x <- truncate (relx *. float w);
2123 let w =
2124 match conf.columns with
2125 | Csingle _ -> w
2126 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2127 | Csplit (c, _) -> w * c
2129 wcmd "geometry %d %d" w h);
2132 let enttext () =
2133 let len = String.length state.text in
2134 let drawstring s =
2135 let hscrollh =
2136 match state.mode with
2137 | Textentry _
2138 | View ->
2139 let h, _, _ = state.uioh#scrollpw in
2141 | _ -> 0
2143 let rect x w =
2144 GlDraw.rect
2145 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2146 (x+.w, float (state.winh - hscrollh))
2149 let w = float (state.winw - state.scrollw - 1) in
2150 if state.progress >= 0.0 && state.progress < 1.0
2151 then (
2152 GlDraw.color (0.3, 0.3, 0.3);
2153 let w1 = w *. state.progress in
2154 rect 0.0 w1;
2155 GlDraw.color (0.0, 0.0, 0.0);
2156 rect w1 (w-.w1)
2158 else (
2159 GlDraw.color (0.0, 0.0, 0.0);
2160 rect 0.0 w;
2163 GlDraw.color (1.0, 1.0, 1.0);
2164 drawstring fstate.fontsize
2165 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2167 let s =
2168 match state.mode with
2169 | Textentry ((prefix, text, _, _, _, _), _) ->
2170 let s =
2171 if len > 0
2172 then
2173 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2174 else
2175 Printf.sprintf "%s%s_" prefix text
2179 | _ -> state.text
2181 let s =
2182 if state.newerrmsgs
2183 then (
2184 if not (istextentry state.mode)
2185 then
2186 let s1 = "(press 'e' to review error messasges)" in
2187 if String.length s > 0 then s ^ " " ^ s1 else s1
2188 else s
2190 else s
2192 if String.length s > 0
2193 then drawstring s
2196 let gctiles () =
2197 let len = Queue.length state.tilelru in
2198 let layout = lazy (
2199 match state.throttle with
2200 | None ->
2201 if conf.preload
2202 then preloadlayout state.y
2203 else state.layout
2204 | Some (layout, _, _) ->
2205 layout
2206 ) in
2207 let rec loop qpos =
2208 if state.memused <= conf.memlimit
2209 then ()
2210 else (
2211 if qpos < len
2212 then
2213 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2214 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2215 let (_, pw, ph, _) = getpagedim n in
2217 gen = state.gen
2218 && colorspace = conf.colorspace
2219 && angle = conf.angle
2220 && pagew = pw
2221 && pageh = ph
2222 && (
2223 let x = col*conf.tilew
2224 and y = row*conf.tileh in
2225 tilevisible (Lazy.force_val layout) n x y
2227 then Queue.push lruitem state.tilelru
2228 else (
2229 freepbo p;
2230 wcmd "freetile %s" p;
2231 state.memused <- state.memused - s;
2232 state.uioh#infochanged Memused;
2233 Hashtbl.remove state.tilemap k;
2235 loop (qpos+1)
2238 loop 0
2241 let logcurrently = function
2242 | Idle -> dolog "Idle"
2243 | Loading (l, gen) ->
2244 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2245 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2246 dolog
2247 "Tiling %d[%d,%d] page=%s cs=%s angle"
2248 l.pageno col row pageopaque
2249 (colorspace_to_string colorspace)
2251 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2252 angle gen conf.angle state.gen
2253 tilew tileh
2254 conf.tilew conf.tileh
2256 | Outlining _ ->
2257 dolog "outlining"
2260 let splitatspace =
2261 let r = Str.regexp " " in
2262 fun s -> Str.bounded_split r s 2;
2265 let onpagerect pageno f =
2266 let b =
2267 match conf.columns with
2268 | Cmulti (_, b) -> b
2269 | Csingle b -> b
2270 | Csplit (_, b) -> b
2272 if pageno >= 0 && pageno < Array.length b
2273 then
2274 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2275 let r = getpdimrect pdimno in
2276 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2279 let gotopagexy1 pageno x y =
2280 onpagerect pageno (fun w h ->
2281 let top = y /. h in
2282 let _,w1,_,leftx = getpagedim pageno in
2283 let wh = state.winh - state.hscrollh in
2284 let sw = float w1 /. w in
2285 let x = sw *. x in
2286 let x = leftx + state.x + truncate x in
2287 let sx =
2288 if x < 0 || x >= state.winw - state.scrollw
2289 then state.x - x
2290 else state.x
2292 let py, h = getpageyh pageno in
2293 let pdy = truncate (top *. float h) in
2294 let y' = py + pdy in
2295 let dy = y' - state.y in
2296 let sy =
2297 if x != state.x || not (dy > 0 && dy < wh)
2298 then (
2299 if conf.presentation
2300 then
2301 if abs (py - y') > wh
2302 then y'
2303 else py
2304 else y';
2306 else state.y
2308 if state.x != sx || state.y != sy
2309 then (
2310 let x, y =
2311 if !wtmode
2312 then (
2313 let ww = state.winw - state.scrollw in
2314 let qx = sx / ww
2315 and qy = pdy / wh in
2316 let x = qx * ww
2317 and y = py + qy * wh in
2318 let x = if -x + ww > w1 then -(w1-ww) else x
2319 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2320 let y =
2321 if conf.presentation
2322 then
2323 if abs (py - y') > wh
2324 then y'
2325 else py
2326 else y';
2328 (x, y)
2330 else (sx, sy)
2332 state.x <- x;
2333 state.hscrollh <-
2334 if x = 0 && state.w <= state.winw - state.scrollw
2335 then 0
2336 else state.scrollw
2338 gotoy_and_clear_text y;
2340 else gotoy_and_clear_text state.y;
2344 let gotopagexy pageno x y =
2345 match state.mode with
2346 | Birdseye _ -> gotopage pageno 0.0
2347 | _ -> gotopagexy1 pageno x y
2350 let act cmds =
2351 (* dolog "%S" cmds; *)
2352 let cl = splitatspace cmds in
2353 let scan s fmt f =
2354 try Scanf.sscanf s fmt f
2355 with exn ->
2356 dolog "error processing '%S': %s" cmds (exntos exn);
2357 exit 1
2359 match cl with
2360 | "clear" :: [] ->
2361 state.uioh#infochanged Pdim;
2362 state.pdims <- [];
2364 | "clearrects" :: [] ->
2365 state.rects <- state.rects1;
2366 G.postRedisplay "clearrects";
2368 | "continue" :: args :: [] ->
2369 let n = scan args "%u" (fun n -> n) in
2370 state.pagecount <- n;
2371 begin match state.currently with
2372 | Outlining l ->
2373 state.currently <- Idle;
2374 state.outlines <- Array.of_list (List.rev l)
2375 | _ -> ()
2376 end;
2378 let cur, cmds = state.geomcmds in
2379 if String.length cur = 0
2380 then failwith "umpossible";
2382 begin match List.rev cmds with
2383 | [] ->
2384 state.geomcmds <- "", [];
2385 represent ();
2386 | (s, f) :: rest ->
2387 f ();
2388 state.geomcmds <- s, List.rev rest;
2389 end;
2390 if conf.maxwait = None && not !wtmode
2391 then G.postRedisplay "continue";
2393 | "title" :: args :: [] ->
2394 Wsi.settitle args
2396 | "msg" :: args :: [] ->
2397 showtext ' ' args
2399 | "vmsg" :: args :: [] ->
2400 if conf.verbose
2401 then showtext ' ' args
2403 | "emsg" :: args :: [] ->
2404 Buffer.add_string state.errmsgs args;
2405 state.newerrmsgs <- true;
2406 G.postRedisplay "error message"
2408 | "progress" :: args :: [] ->
2409 let progress, text =
2410 scan args "%f %n"
2411 (fun f pos ->
2412 f, String.sub args pos (String.length args - pos))
2414 state.text <- text;
2415 state.progress <- progress;
2416 G.postRedisplay "progress"
2418 | "firstmatch" :: args :: [] ->
2419 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2420 scan args "%u %d %f %f %f %f %f %f %f %f"
2421 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2422 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2424 let y = (getpagey pageno) + truncate y0 in
2425 addnav ();
2426 gotoy y;
2427 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2429 | "match" :: args :: [] ->
2430 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2431 scan args "%u %d %f %f %f %f %f %f %f %f"
2432 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2433 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2435 state.rects1 <-
2436 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2438 | "page" :: args :: [] ->
2439 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2440 begin match state.currently with
2441 | Loading (l, gen) ->
2442 vlog "page %d took %f sec" l.pageno t;
2443 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2444 begin match state.throttle with
2445 | None ->
2446 let preloadedpages =
2447 if conf.preload
2448 then preloadlayout state.y
2449 else state.layout
2451 let evict () =
2452 let module IntSet =
2453 Set.Make (struct type t = int let compare = (-) end) in
2454 let set =
2455 List.fold_left (fun s l -> IntSet.add l.pageno s)
2456 IntSet.empty preloadedpages
2458 let evictedpages =
2459 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2460 if not (IntSet.mem pageno set)
2461 then (
2462 wcmd "freepage %s" opaque;
2463 key :: accu
2465 else accu
2466 ) state.pagemap []
2468 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2470 evict ();
2471 state.currently <- Idle;
2472 if gen = state.gen
2473 then (
2474 tilepage l.pageno pageopaque state.layout;
2475 load state.layout;
2476 load preloadedpages;
2477 if pagevisible state.layout l.pageno
2478 && layoutready state.layout
2479 then G.postRedisplay "page";
2482 | Some (layout, _, _) ->
2483 state.currently <- Idle;
2484 tilepage l.pageno pageopaque layout;
2485 load state.layout
2486 end;
2488 | _ ->
2489 dolog "Inconsistent loading state";
2490 logcurrently state.currently;
2491 exit 1
2494 | "tile" :: args :: [] ->
2495 let (x, y, opaque, size, t) =
2496 scan args "%u %u %s %u %f"
2497 (fun x y p size t -> (x, y, p, size, t))
2499 begin match state.currently with
2500 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2501 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2503 unmappbo opaque;
2504 if tilew != conf.tilew || tileh != conf.tileh
2505 then (
2506 wcmd "freetile %s" opaque;
2507 state.currently <- Idle;
2508 load state.layout;
2510 else (
2511 puttileopaque l col row gen cs angle opaque size t;
2512 state.memused <- state.memused + size;
2513 state.uioh#infochanged Memused;
2514 gctiles ();
2515 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2516 opaque, size) state.tilelru;
2518 let layout =
2519 match state.throttle with
2520 | None -> state.layout
2521 | Some (layout, _, _) -> layout
2524 state.currently <- Idle;
2525 if gen = state.gen
2526 && conf.colorspace = cs
2527 && conf.angle = angle
2528 && tilevisible layout l.pageno x y
2529 then conttiling l.pageno pageopaque;
2531 begin match state.throttle with
2532 | None ->
2533 preload state.layout;
2534 if gen = state.gen
2535 && conf.colorspace = cs
2536 && conf.angle = angle
2537 && tilevisible state.layout l.pageno x y
2538 && (not !wtmode || layoutready state.layout)
2539 then G.postRedisplay "tile nothrottle";
2541 | Some (layout, y, _) ->
2542 let ready = layoutready layout in
2543 if ready
2544 then (
2545 state.y <- y;
2546 state.layout <- layout;
2547 state.throttle <- None;
2548 G.postRedisplay "throttle";
2550 else load layout;
2551 end;
2554 | _ ->
2555 dolog "Inconsistent tiling state";
2556 logcurrently state.currently;
2557 exit 1
2560 | "pdim" :: args :: [] ->
2561 let pdim =
2562 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2564 state.uioh#infochanged Pdim;
2565 state.pdims <- pdim :: state.pdims
2567 | "o" :: args :: [] ->
2568 let (l, n, t, h, pos) =
2569 scan args "%u %u %d %u %n"
2570 (fun l n t h pos -> l, n, t, h, pos)
2572 let s = String.sub args pos (String.length args - pos) in
2573 let outline = (s, l, (n, float t /. float h, 0.0)) in
2574 begin match state.currently with
2575 | Outlining outlines ->
2576 state.currently <- Outlining (outline :: outlines)
2577 | Idle ->
2578 state.currently <- Outlining [outline]
2579 | currently ->
2580 dolog "invalid outlining state";
2581 logcurrently currently
2584 | "a" :: args :: [] ->
2585 let (n, l, t) =
2586 scan args "%u %d %d" (fun n l t -> n, l, t)
2588 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2590 | "info" :: args :: [] ->
2591 state.docinfo <- (1, args) :: state.docinfo
2593 | "infoend" :: [] ->
2594 state.uioh#infochanged Docinfo;
2595 state.docinfo <- List.rev state.docinfo
2597 | _ ->
2598 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2601 let onhist cb =
2602 let rc = cb.rc in
2603 let action = function
2604 | HCprev -> cbget cb ~-1
2605 | HCnext -> cbget cb 1
2606 | HCfirst -> cbget cb ~-(cb.rc)
2607 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2608 and cancel () = cb.rc <- rc
2609 in (action, cancel)
2612 let search pattern forward =
2613 if String.length pattern > 0
2614 then
2615 let pn, py =
2616 match state.layout with
2617 | [] -> 0, 0
2618 | l :: _ ->
2619 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2621 wcmd "search %d %d %d %d,%s\000"
2622 (btod conf.icase) pn py (btod forward) pattern;
2625 let intentry text key =
2626 let c =
2627 if key >= 32 && key < 127
2628 then Char.chr key
2629 else '\000'
2631 match c with
2632 | '0' .. '9' ->
2633 let text = addchar text c in
2634 TEcont text
2636 | _ ->
2637 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2638 TEcont text
2641 let linknentry text key =
2642 let c =
2643 if key >= 32 && key < 127
2644 then Char.chr key
2645 else '\000'
2647 match c with
2648 | 'a' .. 'z' ->
2649 let text = addchar text c in
2650 TEcont text
2652 | _ ->
2653 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2654 TEcont text
2657 let linkndone f s =
2658 if String.length s > 0
2659 then (
2660 let n =
2661 let l = String.length s in
2662 let rec loop pos n = if pos = l then n else
2663 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2664 loop (pos+1) (n*26 + m)
2665 in loop 0 0
2667 let rec loop n = function
2668 | [] -> ()
2669 | l :: rest ->
2670 match getopaque l.pageno with
2671 | None -> loop n rest
2672 | Some opaque ->
2673 let m = getlinkcount opaque in
2674 if n < m
2675 then (
2676 let under = getlink opaque n in
2677 f under
2679 else loop (n-m) rest
2681 loop n state.layout;
2685 let textentry text key =
2686 if key land 0xff00 = 0xff00
2687 then TEcont text
2688 else TEcont (text ^ toutf8 key)
2691 let reqlayout angle proportional =
2692 match state.throttle with
2693 | None ->
2694 if nogeomcmds state.geomcmds
2695 then state.anchor <- getanchor ();
2696 conf.angle <- angle mod 360;
2697 if conf.angle != 0
2698 then (
2699 match state.mode with
2700 | LinkNav _ -> state.mode <- View
2701 | _ -> ()
2703 conf.proportional <- proportional;
2704 invalidate "reqlayout"
2705 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2706 | _ -> ()
2709 let settrim trimmargins trimfuzz =
2710 if nogeomcmds state.geomcmds
2711 then state.anchor <- getanchor ();
2712 conf.trimmargins <- trimmargins;
2713 conf.trimfuzz <- trimfuzz;
2714 let x0, y0, x1, y1 = trimfuzz in
2715 invalidate "settrim"
2716 (fun () ->
2717 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2718 flushpages ();
2721 let setzoom zoom =
2722 match state.throttle with
2723 | None ->
2724 let zoom = max 0.01 zoom in
2725 if zoom <> conf.zoom
2726 then (
2727 state.prevzoom <- conf.zoom;
2728 conf.zoom <- zoom;
2729 reshape state.winw state.winh;
2730 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2733 | Some (layout, y, started) ->
2734 let time =
2735 match conf.maxwait with
2736 | None -> 0.0
2737 | Some t -> t
2739 let dt = now () -. started in
2740 if dt > time
2741 then (
2742 state.y <- y;
2743 load layout;
2747 let setcolumns mode columns coverA coverB =
2748 state.prevcolumns <- Some (conf.columns, conf.zoom);
2749 if columns < 0
2750 then (
2751 if isbirdseye mode
2752 then showtext '!' "split mode doesn't work in bird's eye"
2753 else (
2754 conf.columns <- Csplit (-columns, [||]);
2755 state.x <- 0;
2756 conf.zoom <- 1.0;
2759 else (
2760 if columns < 2
2761 then (
2762 conf.columns <- Csingle [||];
2763 state.x <- 0;
2764 setzoom 1.0;
2766 else (
2767 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2768 conf.zoom <- 1.0;
2771 reshape state.winw state.winh;
2774 let enterbirdseye () =
2775 let zoom = float conf.thumbw /. float state.winw in
2776 let birdseyepageno =
2777 let cy = state.winh / 2 in
2778 let fold = function
2779 | [] -> 0
2780 | l :: rest ->
2781 let rec fold best = function
2782 | [] -> best.pageno
2783 | l :: rest ->
2784 let d = cy - (l.pagedispy + l.pagevh/2)
2785 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2786 if abs d < abs dbest
2787 then fold l rest
2788 else best.pageno
2789 in fold l rest
2791 fold state.layout
2793 state.mode <- Birdseye (
2794 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2796 conf.zoom <- zoom;
2797 conf.presentation <- false;
2798 conf.interpagespace <- 10;
2799 conf.hlinks <- false;
2800 state.x <- 0;
2801 state.mstate <- Mnone;
2802 conf.maxwait <- None;
2803 conf.columns <- (
2804 match conf.beyecolumns with
2805 | Some c ->
2806 conf.zoom <- 1.0;
2807 Cmulti ((c, 0, 0), [||])
2808 | None -> Csingle [||]
2810 Wsi.setcursor Wsi.CURSOR_INHERIT;
2811 if conf.verbose
2812 then
2813 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2814 (100.0*.zoom)
2815 else
2816 state.text <- ""
2818 reshape state.winw state.winh;
2821 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2822 state.mode <- View;
2823 conf.zoom <- c.zoom;
2824 conf.presentation <- c.presentation;
2825 conf.interpagespace <- c.interpagespace;
2826 conf.maxwait <- c.maxwait;
2827 conf.hlinks <- c.hlinks;
2828 conf.beyecolumns <- (
2829 match conf.columns with
2830 | Cmulti ((c, _, _), _) -> Some c
2831 | Csingle _ -> None
2832 | Csplit _ -> failwith "leaving bird's eye split mode"
2834 conf.columns <- (
2835 match c.columns with
2836 | Cmulti (c, _) -> Cmulti (c, [||])
2837 | Csingle _ -> Csingle [||]
2838 | Csplit (c, _) -> Csplit (c, [||])
2840 state.x <- leftx;
2841 if conf.verbose
2842 then
2843 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2844 (100.0*.conf.zoom)
2846 reshape state.winw state.winh;
2847 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2850 let togglebirdseye () =
2851 match state.mode with
2852 | Birdseye vals -> leavebirdseye vals true
2853 | View -> enterbirdseye ()
2854 | _ -> ()
2857 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2858 let pageno = max 0 (pageno - incr) in
2859 let rec loop = function
2860 | [] -> gotopage1 pageno 0
2861 | l :: _ when l.pageno = pageno ->
2862 if l.pagedispy >= 0 && l.pagey = 0
2863 then G.postRedisplay "upbirdseye"
2864 else gotopage1 pageno 0
2865 | _ :: rest -> loop rest
2867 loop state.layout;
2868 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2871 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2872 let pageno = min (state.pagecount - 1) (pageno + incr) in
2873 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2874 let rec loop = function
2875 | [] ->
2876 let y, h = getpageyh pageno in
2877 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2878 gotoy (clamp dy)
2879 | l :: _ when l.pageno = pageno ->
2880 if l.pagevh != l.pageh
2881 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2882 else G.postRedisplay "downbirdseye"
2883 | _ :: rest -> loop rest
2885 loop state.layout
2888 let optentry mode _ key =
2889 let btos b = if b then "on" else "off" in
2890 if key >= 32 && key < 127
2891 then
2892 let c = Char.chr key in
2893 match c with
2894 | 's' ->
2895 let ondone s =
2896 try conf.scrollstep <- int_of_string s with exc ->
2897 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2899 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2901 | 'A' ->
2902 let ondone s =
2904 conf.autoscrollstep <- int_of_string s;
2905 if state.autoscroll <> None
2906 then state.autoscroll <- Some conf.autoscrollstep
2907 with exc ->
2908 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2910 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2912 | 'C' ->
2913 let ondone s =
2915 let n, a, b = multicolumns_of_string s in
2916 setcolumns mode n a b;
2917 with exc ->
2918 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2920 TEswitch ("columns: ", "", None, textentry, ondone, true)
2922 | 'Z' ->
2923 let ondone s =
2925 let zoom = float (int_of_string s) /. 100.0 in
2926 setzoom zoom
2927 with exc ->
2928 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2930 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2932 | 't' ->
2933 let ondone s =
2935 conf.thumbw <- bound (int_of_string s) 2 4096;
2936 state.text <-
2937 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2938 begin match mode with
2939 | Birdseye beye ->
2940 leavebirdseye beye false;
2941 enterbirdseye ();
2942 | _ -> ();
2944 with exc ->
2945 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2947 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2949 | 'R' ->
2950 let ondone s =
2951 match try
2952 Some (int_of_string s)
2953 with exc ->
2954 state.text <- Printf.sprintf "bad integer `%s': %s"
2955 s (exntos exc);
2956 None
2957 with
2958 | Some angle -> reqlayout angle conf.proportional
2959 | None -> ()
2961 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2963 | 'i' ->
2964 conf.icase <- not conf.icase;
2965 TEdone ("case insensitive search " ^ (btos conf.icase))
2967 | 'p' ->
2968 conf.preload <- not conf.preload;
2969 gotoy state.y;
2970 TEdone ("preload " ^ (btos conf.preload))
2972 | 'v' ->
2973 conf.verbose <- not conf.verbose;
2974 TEdone ("verbose " ^ (btos conf.verbose))
2976 | 'd' ->
2977 conf.debug <- not conf.debug;
2978 TEdone ("debug " ^ (btos conf.debug))
2980 | 'h' ->
2981 conf.maxhfit <- not conf.maxhfit;
2982 state.maxy <- calcheight ();
2983 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2985 | 'c' ->
2986 conf.crophack <- not conf.crophack;
2987 TEdone ("crophack " ^ btos conf.crophack)
2989 | 'a' ->
2990 let s =
2991 match conf.maxwait with
2992 | None ->
2993 conf.maxwait <- Some infinity;
2994 "always wait for page to complete"
2995 | Some _ ->
2996 conf.maxwait <- None;
2997 "show placeholder if page is not ready"
2999 TEdone s
3001 | 'f' ->
3002 conf.underinfo <- not conf.underinfo;
3003 TEdone ("underinfo " ^ btos conf.underinfo)
3005 | 'P' ->
3006 conf.savebmarks <- not conf.savebmarks;
3007 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3009 | 'S' ->
3010 let ondone s =
3012 let pageno, py =
3013 match state.layout with
3014 | [] -> 0, 0
3015 | l :: _ ->
3016 l.pageno, l.pagey
3018 conf.interpagespace <- int_of_string s;
3019 docolumns conf.columns;
3020 state.maxy <- calcheight ();
3021 let y = getpagey pageno in
3022 gotoy (y + py)
3023 with exc ->
3024 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3026 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3028 | 'l' ->
3029 reqlayout conf.angle (not conf.proportional);
3030 TEdone ("proportional display " ^ btos conf.proportional)
3032 | 'T' ->
3033 settrim (not conf.trimmargins) conf.trimfuzz;
3034 TEdone ("trim margins " ^ btos conf.trimmargins)
3036 | 'I' ->
3037 conf.invert <- not conf.invert;
3038 TEdone ("invert colors " ^ btos conf.invert)
3040 | 'x' ->
3041 let ondone s =
3042 cbput state.hists.sel s;
3043 conf.selcmd <- s;
3045 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3046 textentry, ondone, true)
3048 | _ ->
3049 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3050 TEstop
3051 else
3052 TEcont state.text
3055 class type lvsource = object
3056 method getitemcount : int
3057 method getitem : int -> (string * int)
3058 method hasaction : int -> bool
3059 method exit :
3060 uioh:uioh ->
3061 cancel:bool ->
3062 active:int ->
3063 first:int ->
3064 pan:int ->
3065 qsearch:string ->
3066 uioh option
3067 method getactive : int
3068 method getfirst : int
3069 method getqsearch : string
3070 method setqsearch : string -> unit
3071 method getpan : int
3072 end;;
3074 class virtual lvsourcebase = object
3075 val mutable m_active = 0
3076 val mutable m_first = 0
3077 val mutable m_qsearch = ""
3078 val mutable m_pan = 0
3079 method getactive = m_active
3080 method getfirst = m_first
3081 method getqsearch = m_qsearch
3082 method getpan = m_pan
3083 method setqsearch s = m_qsearch <- s
3084 end;;
3086 let withoutlastutf8 s =
3087 let len = String.length s in
3088 if len = 0
3089 then s
3090 else
3091 let rec find pos =
3092 if pos = 0
3093 then pos
3094 else
3095 let b = Char.code s.[pos] in
3096 if b land 0b11000000 = 0b11000000
3097 then pos
3098 else find (pos-1)
3100 let first =
3101 if Char.code s.[len-1] land 0x80 = 0
3102 then len-1
3103 else find (len-1)
3105 String.sub s 0 first;
3108 let textentrykeyboard
3109 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3110 let key =
3111 if key >= 0xffb0 && key <= 0xffb9
3112 then key - 0xffb0 + 48 else key
3114 let enttext te =
3115 state.mode <- Textentry (te, onleave);
3116 state.text <- "";
3117 enttext ();
3118 G.postRedisplay "textentrykeyboard enttext";
3120 let histaction cmd =
3121 match opthist with
3122 | None -> ()
3123 | Some (action, _) ->
3124 state.mode <- Textentry (
3125 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3127 G.postRedisplay "textentry histaction"
3129 match key with
3130 | 0xff08 -> (* backspace *)
3131 let s = withoutlastutf8 text in
3132 let len = String.length s in
3133 if cancelonempty && len = 0
3134 then (
3135 onleave Cancel;
3136 G.postRedisplay "textentrykeyboard after cancel";
3138 else (
3139 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3142 | 0xff0d | 0xff8d -> (* (kp) enter *)
3143 ondone text;
3144 onleave Confirm;
3145 G.postRedisplay "textentrykeyboard after confirm"
3147 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3148 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3149 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3150 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3152 | 0xff1b -> (* escape*)
3153 if String.length text = 0
3154 then (
3155 begin match opthist with
3156 | None -> ()
3157 | Some (_, onhistcancel) -> onhistcancel ()
3158 end;
3159 onleave Cancel;
3160 state.text <- "";
3161 G.postRedisplay "textentrykeyboard after cancel2"
3163 else (
3164 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3167 | 0xff9f | 0xffff -> () (* delete *)
3169 | _ when key != 0
3170 && key land 0xff00 != 0xff00 (* keyboard *)
3171 && key land 0xfe00 != 0xfe00 (* xkb *)
3172 && key land 0xfd00 != 0xfd00 (* 3270 *)
3174 begin match onkey text key with
3175 | TEdone text ->
3176 ondone text;
3177 onleave Confirm;
3178 G.postRedisplay "textentrykeyboard after confirm2";
3180 | TEcont text ->
3181 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3183 | TEstop ->
3184 onleave Cancel;
3185 G.postRedisplay "textentrykeyboard after cancel3"
3187 | TEswitch te ->
3188 state.mode <- Textentry (te, onleave);
3189 G.postRedisplay "textentrykeyboard switch";
3190 end;
3192 | _ ->
3193 vlog "unhandled key %s" (Wsi.keyname key)
3196 let firstof first active =
3197 if first > active || abs (first - active) > fstate.maxrows - 1
3198 then max 0 (active - (fstate.maxrows/2))
3199 else first
3202 let calcfirst first active =
3203 if active > first
3204 then
3205 let rows = active - first in
3206 if rows > fstate.maxrows then active - fstate.maxrows else first
3207 else active
3210 let scrollph y maxy =
3211 let sh = (float (maxy + state.winh) /. float state.winh) in
3212 let sh = float state.winh /. sh in
3213 let sh = max sh (float conf.scrollh) in
3215 let percent =
3216 if y = state.maxy
3217 then 1.0
3218 else float y /. float maxy
3220 let position = (float state.winh -. sh) *. percent in
3222 let position =
3223 if position +. sh > float state.winh
3224 then float state.winh -. sh
3225 else position
3227 position, sh;
3230 let coe s = (s :> uioh);;
3232 class listview ~(source:lvsource) ~trusted ~modehash =
3233 object (self)
3234 val m_pan = source#getpan
3235 val m_first = source#getfirst
3236 val m_active = source#getactive
3237 val m_qsearch = source#getqsearch
3238 val m_prev_uioh = state.uioh
3240 method private elemunder y =
3241 let n = y / (fstate.fontsize+1) in
3242 if m_first + n < source#getitemcount
3243 then (
3244 if source#hasaction (m_first + n)
3245 then Some (m_first + n)
3246 else None
3248 else None
3250 method display =
3251 Gl.enable `blend;
3252 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3253 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3254 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3255 GlDraw.color (1., 1., 1.);
3256 Gl.enable `texture_2d;
3257 let fs = fstate.fontsize in
3258 let nfs = fs + 1 in
3259 let ww = fstate.wwidth in
3260 let tabw = 30.0*.ww in
3261 let itemcount = source#getitemcount in
3262 let rec loop row =
3263 if (row - m_first) > fstate.maxrows
3264 then ()
3265 else (
3266 if row >= 0 && row < itemcount
3267 then (
3268 let (s, level) = source#getitem row in
3269 let y = (row - m_first) * nfs in
3270 let x = 5.0 +. float (level + m_pan) *. ww in
3271 if row = m_active
3272 then (
3273 Gl.disable `texture_2d;
3274 GlDraw.polygon_mode `both `line;
3275 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3276 GlDraw.rect (1., float (y + 1))
3277 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3278 GlDraw.polygon_mode `both `fill;
3279 GlDraw.color (1., 1., 1.);
3280 Gl.enable `texture_2d;
3283 let drawtabularstring s =
3284 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3285 if trusted
3286 then
3287 let tabpos = try String.index s '\t' with Not_found -> -1 in
3288 if tabpos > 0
3289 then
3290 let len = String.length s - tabpos - 1 in
3291 let s1 = String.sub s 0 tabpos
3292 and s2 = String.sub s (tabpos + 1) len in
3293 let nx = drawstr x s1 in
3294 let sw = nx -. x in
3295 let x = x +. (max tabw sw) in
3296 drawstr x s2
3297 else
3298 drawstr x s
3299 else
3300 drawstr x s
3302 let _ = drawtabularstring s in
3303 loop (row+1)
3307 loop m_first;
3308 Gl.disable `blend;
3309 Gl.disable `texture_2d;
3311 method updownlevel incr =
3312 let len = source#getitemcount in
3313 let curlevel =
3314 if m_active >= 0 && m_active < len
3315 then snd (source#getitem m_active)
3316 else -1
3318 let rec flow i =
3319 if i = len then i-1 else if i = -1 then 0 else
3320 let _, l = source#getitem i in
3321 if l != curlevel then i else flow (i+incr)
3323 let active = flow m_active in
3324 let first = calcfirst m_first active in
3325 G.postRedisplay "outline updownlevel";
3326 {< m_active = active; m_first = first >}
3328 method private key1 key mask =
3329 let set1 active first qsearch =
3330 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3332 let search active pattern incr =
3333 let dosearch re =
3334 let rec loop n =
3335 if n >= 0 && n < source#getitemcount
3336 then (
3337 let s, _ = source#getitem n in
3339 (try ignore (Str.search_forward re s 0); true
3340 with Not_found -> false)
3341 then Some n
3342 else loop (n + incr)
3344 else None
3346 loop active
3349 let re = Str.regexp_case_fold pattern in
3350 dosearch re
3351 with Failure s ->
3352 state.text <- s;
3353 None
3355 let itemcount = source#getitemcount in
3356 let find start incr =
3357 let rec find i =
3358 if i = -1 || i = itemcount
3359 then -1
3360 else (
3361 if source#hasaction i
3362 then i
3363 else find (i + incr)
3366 find start
3368 let set active first =
3369 let first = bound first 0 (itemcount - fstate.maxrows) in
3370 state.text <- "";
3371 coe {< m_active = active; m_first = first >}
3373 let navigate incr =
3374 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3375 let active, first =
3376 let incr1 = if incr > 0 then 1 else -1 in
3377 if isvisible m_first m_active
3378 then
3379 let next =
3380 let next = m_active + incr in
3381 let next =
3382 if next < 0 || next >= itemcount
3383 then -1
3384 else find next incr1
3386 if next = -1 || abs (m_active - next) > fstate.maxrows
3387 then -1
3388 else next
3390 if next = -1
3391 then
3392 let first = m_first + incr in
3393 let first = bound first 0 (itemcount - 1) in
3394 let next =
3395 let next = m_active + incr in
3396 let next = bound next 0 (itemcount - 1) in
3397 find next ~-incr1
3399 let active = if next = -1 then m_active else next in
3400 active, first
3401 else
3402 let first = min next m_first in
3403 let first =
3404 if abs (next - first) > fstate.maxrows
3405 then first + incr
3406 else first
3408 next, first
3409 else
3410 let first = m_first + incr in
3411 let first = bound first 0 (itemcount - 1) in
3412 let active =
3413 let next = m_active + incr in
3414 let next = bound next 0 (itemcount - 1) in
3415 let next = find next incr1 in
3416 let active =
3417 if next = -1 || abs (m_active - first) > fstate.maxrows
3418 then (
3419 let active = if m_active = -1 then next else m_active in
3420 active
3422 else next
3424 if isvisible first active
3425 then active
3426 else -1
3428 active, first
3430 G.postRedisplay "listview navigate";
3431 set active first;
3433 match key with
3434 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3435 let incr = if key = 0x72 then -1 else 1 in
3436 let active, first =
3437 match search (m_active + incr) m_qsearch incr with
3438 | None ->
3439 state.text <- m_qsearch ^ " [not found]";
3440 m_active, m_first
3441 | Some active ->
3442 state.text <- m_qsearch;
3443 active, firstof m_first active
3445 G.postRedisplay "listview ctrl-r/s";
3446 set1 active first m_qsearch;
3448 | 0xff08 -> (* backspace *)
3449 if String.length m_qsearch = 0
3450 then coe self
3451 else (
3452 let qsearch = withoutlastutf8 m_qsearch in
3453 let len = String.length qsearch in
3454 if len = 0
3455 then (
3456 state.text <- "";
3457 G.postRedisplay "listview empty qsearch";
3458 set1 m_active m_first "";
3460 else
3461 let active, first =
3462 match search m_active qsearch ~-1 with
3463 | None ->
3464 state.text <- qsearch ^ " [not found]";
3465 m_active, m_first
3466 | Some active ->
3467 state.text <- qsearch;
3468 active, firstof m_first active
3470 G.postRedisplay "listview backspace qsearch";
3471 set1 active first qsearch
3474 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3475 let pattern = m_qsearch ^ toutf8 key in
3476 let active, first =
3477 match search m_active pattern 1 with
3478 | None ->
3479 state.text <- pattern ^ " [not found]";
3480 m_active, m_first
3481 | Some active ->
3482 state.text <- pattern;
3483 active, firstof m_first active
3485 G.postRedisplay "listview qsearch add";
3486 set1 active first pattern;
3488 | 0xff1b -> (* escape *)
3489 state.text <- "";
3490 if String.length m_qsearch = 0
3491 then (
3492 G.postRedisplay "list view escape";
3493 begin
3494 match
3495 source#exit (coe self) true m_active m_first m_pan m_qsearch
3496 with
3497 | None -> m_prev_uioh
3498 | Some uioh -> uioh
3501 else (
3502 G.postRedisplay "list view kill qsearch";
3503 source#setqsearch "";
3504 coe {< m_qsearch = "" >}
3507 | 0xff0d | 0xff8d -> (* (kp) enter *)
3508 state.text <- "";
3509 let self = {< m_qsearch = "" >} in
3510 source#setqsearch "";
3511 let opt =
3512 G.postRedisplay "listview enter";
3513 if m_active >= 0 && m_active < source#getitemcount
3514 then (
3515 source#exit (coe self) false m_active m_first m_pan "";
3517 else (
3518 source#exit (coe self) true m_active m_first m_pan "";
3521 begin match opt with
3522 | None -> m_prev_uioh
3523 | Some uioh -> uioh
3526 | 0xff9f | 0xffff -> (* (kp) delete *)
3527 coe self
3529 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3530 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3531 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3532 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3534 | 0xff53 | 0xff98 -> (* (kp) right *)
3535 state.text <- "";
3536 G.postRedisplay "listview right";
3537 coe {< m_pan = m_pan - 1 >}
3539 | 0xff51 | 0xff96 -> (* (kp) left *)
3540 state.text <- "";
3541 G.postRedisplay "listview left";
3542 coe {< m_pan = m_pan + 1 >}
3544 | 0xff50 | 0xff95 -> (* (kp) home *)
3545 let active = find 0 1 in
3546 G.postRedisplay "listview home";
3547 set active 0;
3549 | 0xff57 | 0xff9c -> (* (kp) end *)
3550 let first = max 0 (itemcount - fstate.maxrows) in
3551 let active = find (itemcount - 1) ~-1 in
3552 G.postRedisplay "listview end";
3553 set active first;
3555 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3556 coe self
3558 | _ ->
3559 dolog "listview unknown key %#x" key; coe self
3561 method key key mask =
3562 match state.mode with
3563 | Textentry te -> textentrykeyboard key mask te; coe self
3564 | _ -> self#key1 key mask
3566 method button button down x y _ =
3567 let opt =
3568 match button with
3569 | 1 when x > state.winw - conf.scrollbw ->
3570 G.postRedisplay "listview scroll";
3571 if down
3572 then
3573 let _, position, sh = self#scrollph in
3574 if y > truncate position && y < truncate (position +. sh)
3575 then (
3576 state.mstate <- Mscrolly;
3577 Some (coe self)
3579 else
3580 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3581 let first = truncate (s *. float source#getitemcount) in
3582 let first = min source#getitemcount first in
3583 Some (coe {< m_first = first; m_active = first >})
3584 else (
3585 state.mstate <- Mnone;
3586 Some (coe self);
3588 | 1 when not down ->
3589 begin match self#elemunder y with
3590 | Some n ->
3591 G.postRedisplay "listview click";
3592 source#exit
3593 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3594 | _ ->
3595 Some (coe self)
3597 | n when (n == 4 || n == 5) && not down ->
3598 let len = source#getitemcount in
3599 let first =
3600 if n = 5 && m_first + fstate.maxrows >= len
3601 then
3602 m_first
3603 else
3604 let first = m_first + (if n == 4 then -1 else 1) in
3605 bound first 0 (len - 1)
3607 G.postRedisplay "listview wheel";
3608 Some (coe {< m_first = first >})
3609 | n when (n = 6 || n = 7) && not down ->
3610 let inc = m_first + (if n = 7 then -1 else 1) in
3611 G.postRedisplay "listview hwheel";
3612 Some (coe {< m_pan = m_pan + inc >})
3613 | _ ->
3614 Some (coe self)
3616 match opt with
3617 | None -> m_prev_uioh
3618 | Some uioh -> uioh
3620 method motion _ y =
3621 match state.mstate with
3622 | Mscrolly ->
3623 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3624 let first = truncate (s *. float source#getitemcount) in
3625 let first = min source#getitemcount first in
3626 G.postRedisplay "listview motion";
3627 coe {< m_first = first; m_active = first >}
3628 | _ -> coe self
3630 method pmotion x y =
3631 if x < state.winw - conf.scrollbw
3632 then
3633 let n =
3634 match self#elemunder y with
3635 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3636 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3638 let o =
3639 if n != m_active
3640 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3641 else self
3643 coe o
3644 else (
3645 Wsi.setcursor Wsi.CURSOR_INHERIT;
3646 coe self
3649 method infochanged _ = ()
3651 method scrollpw = (0, 0.0, 0.0)
3652 method scrollph =
3653 let nfs = fstate.fontsize + 1 in
3654 let y = m_first * nfs in
3655 let itemcount = source#getitemcount in
3656 let maxi = max 0 (itemcount - fstate.maxrows) in
3657 let maxy = maxi * nfs in
3658 let p, h = scrollph y maxy in
3659 conf.scrollbw, p, h
3661 method modehash = modehash
3662 end;;
3664 class outlinelistview ~source =
3665 object (self)
3666 inherit listview
3667 ~source:(source :> lvsource)
3668 ~trusted:false
3669 ~modehash:(findkeyhash conf "outline")
3670 as super
3672 method key key mask =
3673 let calcfirst first active =
3674 if active > first
3675 then
3676 let rows = active - first in
3677 let maxrows =
3678 if String.length state.text = 0
3679 then fstate.maxrows
3680 else fstate.maxrows - 2
3682 if rows > maxrows then active - maxrows else first
3683 else active
3685 let navigate incr =
3686 let active = m_active + incr in
3687 let active = bound active 0 (source#getitemcount - 1) in
3688 let first = calcfirst m_first active in
3689 G.postRedisplay "outline navigate";
3690 coe {< m_active = active; m_first = first >}
3692 let ctrl = Wsi.withctrl mask in
3693 match key with
3694 | 110 when ctrl -> (* ctrl-n *)
3695 source#narrow m_qsearch;
3696 G.postRedisplay "outline ctrl-n";
3697 coe {< m_first = 0; m_active = 0 >}
3699 | 117 when ctrl -> (* ctrl-u *)
3700 source#denarrow;
3701 G.postRedisplay "outline ctrl-u";
3702 state.text <- "";
3703 coe {< m_first = 0; m_active = 0 >}
3705 | 108 when ctrl -> (* ctrl-l *)
3706 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3707 G.postRedisplay "outline ctrl-l";
3708 coe {< m_first = first >}
3710 | 0xff9f | 0xffff -> (* (kp) delete *)
3711 source#remove m_active;
3712 G.postRedisplay "outline delete";
3713 let active = max 0 (m_active-1) in
3714 coe {< m_first = firstof m_first active;
3715 m_active = active >}
3717 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3718 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3719 | 0xff55 | 0xff9a -> (* (kp) prior *)
3720 navigate ~-(fstate.maxrows)
3721 | 0xff56 | 0xff9b -> (* (kp) next *)
3722 navigate fstate.maxrows
3724 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3725 let o =
3726 if ctrl
3727 then (
3728 G.postRedisplay "outline ctrl right";
3729 {< m_pan = m_pan + 1 >}
3731 else self#updownlevel 1
3733 coe o
3735 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3736 let o =
3737 if ctrl
3738 then (
3739 G.postRedisplay "outline ctrl left";
3740 {< m_pan = m_pan - 1 >}
3742 else self#updownlevel ~-1
3744 coe o
3746 | 0xff50 | 0xff95 -> (* (kp) home *)
3747 G.postRedisplay "outline home";
3748 coe {< m_first = 0; m_active = 0 >}
3750 | 0xff57 | 0xff9c -> (* (kp) end *)
3751 let active = source#getitemcount - 1 in
3752 let first = max 0 (active - fstate.maxrows) in
3753 G.postRedisplay "outline end";
3754 coe {< m_active = active; m_first = first >}
3756 | _ -> super#key key mask
3759 let outlinesource usebookmarks =
3760 let empty = [||] in
3761 (object
3762 inherit lvsourcebase
3763 val mutable m_items = empty
3764 val mutable m_orig_items = empty
3765 val mutable m_prev_items = empty
3766 val mutable m_narrow_pattern = ""
3767 val mutable m_hadremovals = false
3769 method getitemcount =
3770 Array.length m_items + (if m_hadremovals then 1 else 0)
3772 method getitem n =
3773 if n == Array.length m_items && m_hadremovals
3774 then
3775 ("[Confirm removal]", 0)
3776 else
3777 let s, n, _ = m_items.(n) in
3778 (s, n)
3780 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3781 ignore (uioh, first, qsearch);
3782 let confrimremoval = m_hadremovals && active = Array.length m_items in
3783 let items =
3784 if String.length m_narrow_pattern = 0
3785 then m_orig_items
3786 else m_items
3788 if not cancel
3789 then (
3790 if not confrimremoval
3791 then(
3792 let _, _, anchor = m_items.(active) in
3793 gotoghyll (getanchory anchor);
3794 m_items <- items;
3796 else (
3797 state.bookmarks <- Array.to_list m_items;
3798 m_orig_items <- m_items;
3801 else m_items <- items;
3802 m_pan <- pan;
3803 None
3805 method hasaction _ = true
3807 method greetmsg =
3808 if Array.length m_items != Array.length m_orig_items
3809 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3810 else ""
3812 method narrow pattern =
3813 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3814 match reopt with
3815 | None -> ()
3816 | Some re ->
3817 let rec loop accu n =
3818 if n = -1
3819 then (
3820 m_narrow_pattern <- pattern;
3821 m_items <- Array.of_list accu
3823 else
3824 let (s, _, _) as o = m_items.(n) in
3825 let accu =
3826 if (try ignore (Str.search_forward re s 0); true
3827 with Not_found -> false)
3828 then o :: accu
3829 else accu
3831 loop accu (n-1)
3833 loop [] (Array.length m_items - 1)
3835 method denarrow =
3836 m_orig_items <- (
3837 if usebookmarks
3838 then Array.of_list state.bookmarks
3839 else state.outlines
3841 m_items <- m_orig_items
3843 method remove m =
3844 if usebookmarks
3845 then
3846 if m >= 0 && m < Array.length m_items
3847 then (
3848 m_hadremovals <- true;
3849 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3850 let n = if n >= m then n+1 else n in
3851 m_items.(n)
3855 method reset anchor items =
3856 m_hadremovals <- false;
3857 if m_orig_items == empty || m_prev_items != items
3858 then (
3859 m_orig_items <- items;
3860 if String.length m_narrow_pattern = 0
3861 then m_items <- items;
3863 m_prev_items <- items;
3864 let rely = getanchory anchor in
3865 let active =
3866 let rec loop n best bestd =
3867 if n = Array.length m_items
3868 then best
3869 else
3870 let (_, _, anchor) = m_items.(n) in
3871 let orely = getanchory anchor in
3872 let d = abs (orely - rely) in
3873 if d < bestd
3874 then loop (n+1) n d
3875 else loop (n+1) best bestd
3877 loop 0 ~-1 max_int
3879 m_active <- active;
3880 m_first <- firstof m_first active
3881 end)
3884 let enterselector usebookmarks =
3885 let source = outlinesource usebookmarks in
3886 fun errmsg ->
3887 let outlines =
3888 if usebookmarks
3889 then Array.of_list state.bookmarks
3890 else state.outlines
3892 if Array.length outlines = 0
3893 then (
3894 showtext ' ' errmsg;
3896 else (
3897 state.text <- source#greetmsg;
3898 Wsi.setcursor Wsi.CURSOR_INHERIT;
3899 let anchor = getanchor () in
3900 source#reset anchor outlines;
3901 state.uioh <- coe (new outlinelistview ~source);
3902 G.postRedisplay "enter selector";
3906 let enteroutlinemode =
3907 let f = enterselector false in
3908 fun ()-> f "Document has no outline";
3911 let enterbookmarkmode =
3912 let f = enterselector true in
3913 fun () -> f "Document has no bookmarks (yet)";
3916 let color_of_string s =
3917 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3918 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3922 let color_to_string (r, g, b) =
3923 let r = truncate (r *. 256.0)
3924 and g = truncate (g *. 256.0)
3925 and b = truncate (b *. 256.0) in
3926 Printf.sprintf "%d/%d/%d" r g b
3929 let irect_of_string s =
3930 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3933 let irect_to_string (x0,y0,x1,y1) =
3934 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3937 let makecheckers () =
3938 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3939 following to say:
3940 converted by Issac Trotts. July 25, 2002 *)
3941 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3942 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3943 let id = GlTex.gen_texture () in
3944 GlTex.bind_texture `texture_2d id;
3945 GlPix.store (`unpack_alignment 1);
3946 GlTex.image2d image;
3947 List.iter (GlTex.parameter ~target:`texture_2d)
3948 [ `mag_filter `nearest; `min_filter `nearest ];
3952 let setcheckers enabled =
3953 match state.texid with
3954 | None ->
3955 if enabled then state.texid <- Some (makecheckers ())
3957 | Some texid ->
3958 if not enabled
3959 then (
3960 GlTex.delete_texture texid;
3961 state.texid <- None;
3965 let int_of_string_with_suffix s =
3966 let l = String.length s in
3967 let s1, shift =
3968 if l > 1
3969 then
3970 let suffix = Char.lowercase s.[l-1] in
3971 match suffix with
3972 | 'k' -> String.sub s 0 (l-1), 10
3973 | 'm' -> String.sub s 0 (l-1), 20
3974 | 'g' -> String.sub s 0 (l-1), 30
3975 | _ -> s, 0
3976 else s, 0
3978 let n = int_of_string s1 in
3979 let m = n lsl shift in
3980 if m < 0 || m < n
3981 then raise (Failure "value too large")
3982 else m
3985 let string_with_suffix_of_int n =
3986 if n = 0
3987 then "0"
3988 else
3989 let n, s =
3990 if n land ((1 lsl 30) - 1) = 0
3991 then n lsr 30, "G"
3992 else (
3993 if n land ((1 lsl 20) - 1) = 0
3994 then n lsr 20, "M"
3995 else (
3996 if n land ((1 lsl 10) - 1) = 0
3997 then n lsr 10, "K"
3998 else n, ""
4002 let rec loop s n =
4003 let h = n mod 1000 in
4004 let n = n / 1000 in
4005 if n = 0
4006 then string_of_int h ^ s
4007 else (
4008 let s = Printf.sprintf "_%03d%s" h s in
4009 loop s n
4012 loop "" n ^ s;
4015 let defghyllscroll = (40, 8, 32);;
4016 let ghyllscroll_of_string s =
4017 let (n, a, b) as nab =
4018 if s = "default"
4019 then defghyllscroll
4020 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4022 if n <= a || n <= b || a >= b
4023 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4024 nab;
4027 let ghyllscroll_to_string ((n, a, b) as nab) =
4028 if nab = defghyllscroll
4029 then "default"
4030 else Printf.sprintf "%d,%d,%d" n a b;
4033 let describe_location () =
4034 let fn = page_of_y state.y in
4035 let ln = page_of_y (state.y + state.winh - state.hscrollh) in
4036 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4037 let percent =
4038 if maxy <= 0
4039 then 100.
4040 else (100. *. (float state.y /. float maxy))
4042 if fn = ln
4043 then
4044 Printf.sprintf "page %d of %d [%.2f%%]"
4045 (fn+1) state.pagecount percent
4046 else
4047 Printf.sprintf
4048 "pages %d-%d of %d [%.2f%%]"
4049 (fn+1) (ln+1) state.pagecount percent
4052 let setpresentationmode v =
4053 let n = page_of_y state.y in
4054 state.anchor <- (n, 0.0, 1.0);
4055 conf.presentation <- v;
4056 if conf.presentation
4057 then (
4058 if not conf.scrollbarinpm
4059 then state.scrollw <- 0;
4061 else state.scrollw <- conf.scrollbw;
4062 represent ();
4065 let enterinfomode =
4066 let btos b = if b then "\xe2\x88\x9a" else "" in
4067 let showextended = ref false in
4068 let leave mode = function
4069 | Confirm -> state.mode <- mode
4070 | Cancel -> state.mode <- mode in
4071 let src =
4072 (object
4073 val mutable m_first_time = true
4074 val mutable m_l = []
4075 val mutable m_a = [||]
4076 val mutable m_prev_uioh = nouioh
4077 val mutable m_prev_mode = View
4079 inherit lvsourcebase
4081 method reset prev_mode prev_uioh =
4082 m_a <- Array.of_list (List.rev m_l);
4083 m_l <- [];
4084 m_prev_mode <- prev_mode;
4085 m_prev_uioh <- prev_uioh;
4086 if m_first_time
4087 then (
4088 let rec loop n =
4089 if n >= Array.length m_a
4090 then ()
4091 else
4092 match m_a.(n) with
4093 | _, _, _, Action _ -> m_active <- n
4094 | _ -> loop (n+1)
4096 loop 0;
4097 m_first_time <- false;
4100 method int name get set =
4101 m_l <-
4102 (name, `int get, 1, Action (
4103 fun u ->
4104 let ondone s =
4105 try set (int_of_string s)
4106 with exn ->
4107 state.text <- Printf.sprintf "bad integer `%s': %s"
4108 s (exntos exn)
4110 state.text <- "";
4111 let te = name ^ ": ", "", None, intentry, ondone, true in
4112 state.mode <- Textentry (te, leave m_prev_mode);
4114 )) :: m_l
4116 method int_with_suffix name get set =
4117 m_l <-
4118 (name, `intws get, 1, Action (
4119 fun u ->
4120 let ondone s =
4121 try set (int_of_string_with_suffix s)
4122 with exn ->
4123 state.text <- Printf.sprintf "bad integer `%s': %s"
4124 s (exntos exn)
4126 state.text <- "";
4127 let te =
4128 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4130 state.mode <- Textentry (te, leave m_prev_mode);
4132 )) :: m_l
4134 method bool ?(offset=1) ?(btos=btos) name get set =
4135 m_l <-
4136 (name, `bool (btos, get), offset, Action (
4137 fun u ->
4138 let v = get () in
4139 set (not v);
4141 )) :: m_l
4143 method color name get set =
4144 m_l <-
4145 (name, `color get, 1, Action (
4146 fun u ->
4147 let invalid = (nan, nan, nan) in
4148 let ondone s =
4149 let c =
4150 try color_of_string s
4151 with exn ->
4152 state.text <- Printf.sprintf "bad color `%s': %s"
4153 s (exntos exn);
4154 invalid
4156 if c <> invalid
4157 then set c;
4159 let te = name ^ ": ", "", None, textentry, ondone, true in
4160 state.text <- color_to_string (get ());
4161 state.mode <- Textentry (te, leave m_prev_mode);
4163 )) :: m_l
4165 method string name get set =
4166 m_l <-
4167 (name, `string get, 1, Action (
4168 fun u ->
4169 let ondone s = set s in
4170 let te = name ^ ": ", "", None, textentry, ondone, true in
4171 state.mode <- Textentry (te, leave m_prev_mode);
4173 )) :: m_l
4175 method colorspace name get set =
4176 m_l <-
4177 (name, `string get, 1, Action (
4178 fun _ ->
4179 let source =
4180 let vals = [| "rgb"; "bgr"; "gray" |] in
4181 (object
4182 inherit lvsourcebase
4184 initializer
4185 m_active <- int_of_colorspace conf.colorspace;
4186 m_first <- 0;
4188 method getitemcount = Array.length vals
4189 method getitem n = (vals.(n), 0)
4190 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4191 ignore (uioh, first, pan, qsearch);
4192 if not cancel then set active;
4193 None
4194 method hasaction _ = true
4195 end)
4197 state.text <- "";
4198 let modehash = findkeyhash conf "info" in
4199 coe (new listview ~source ~trusted:true ~modehash)
4200 )) :: m_l
4202 method caption s offset =
4203 m_l <- (s, `empty, offset, Noaction) :: m_l
4205 method caption2 s f offset =
4206 m_l <- (s, `string f, offset, Noaction) :: m_l
4208 method getitemcount = Array.length m_a
4210 method getitem n =
4211 let tostr = function
4212 | `int f -> string_of_int (f ())
4213 | `intws f -> string_with_suffix_of_int (f ())
4214 | `string f -> f ()
4215 | `color f -> color_to_string (f ())
4216 | `bool (btos, f) -> btos (f ())
4217 | `empty -> ""
4219 let name, t, offset, _ = m_a.(n) in
4220 ((let s = tostr t in
4221 if String.length s > 0
4222 then Printf.sprintf "%s\t%s" name s
4223 else name),
4224 offset)
4226 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4227 let uiohopt =
4228 if not cancel
4229 then (
4230 m_qsearch <- qsearch;
4231 let uioh =
4232 match m_a.(active) with
4233 | _, _, _, Action f -> f uioh
4234 | _ -> uioh
4236 Some uioh
4238 else None
4240 m_active <- active;
4241 m_first <- first;
4242 m_pan <- pan;
4243 uiohopt
4245 method hasaction n =
4246 match m_a.(n) with
4247 | _, _, _, Action _ -> true
4248 | _ -> false
4249 end)
4251 let rec fillsrc prevmode prevuioh =
4252 let sep () = src#caption "" 0 in
4253 let colorp name get set =
4254 src#string name
4255 (fun () -> color_to_string (get ()))
4256 (fun v ->
4258 let c = color_of_string v in
4259 set c
4260 with exn ->
4261 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4264 let oldmode = state.mode in
4265 let birdseye = isbirdseye state.mode in
4267 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4269 src#bool "presentation mode"
4270 (fun () -> conf.presentation)
4271 (fun v -> setpresentationmode v);
4273 src#bool "ignore case in searches"
4274 (fun () -> conf.icase)
4275 (fun v -> conf.icase <- v);
4277 src#bool "preload"
4278 (fun () -> conf.preload)
4279 (fun v -> conf.preload <- v);
4281 src#bool "highlight links"
4282 (fun () -> conf.hlinks)
4283 (fun v -> conf.hlinks <- v);
4285 src#bool "under info"
4286 (fun () -> conf.underinfo)
4287 (fun v -> conf.underinfo <- v);
4289 src#bool "persistent bookmarks"
4290 (fun () -> conf.savebmarks)
4291 (fun v -> conf.savebmarks <- v);
4293 src#bool "proportional display"
4294 (fun () -> conf.proportional)
4295 (fun v -> reqlayout conf.angle v);
4297 src#bool "trim margins"
4298 (fun () -> conf.trimmargins)
4299 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4301 src#bool "persistent location"
4302 (fun () -> conf.jumpback)
4303 (fun v -> conf.jumpback <- v);
4305 sep ();
4306 src#int "inter-page space"
4307 (fun () -> conf.interpagespace)
4308 (fun n ->
4309 conf.interpagespace <- n;
4310 docolumns conf.columns;
4311 let pageno, py =
4312 match state.layout with
4313 | [] -> 0, 0
4314 | l :: _ ->
4315 l.pageno, l.pagey
4317 state.maxy <- calcheight ();
4318 let y = getpagey pageno in
4319 gotoy (y + py)
4322 src#int "page bias"
4323 (fun () -> conf.pagebias)
4324 (fun v -> conf.pagebias <- v);
4326 src#int "scroll step"
4327 (fun () -> conf.scrollstep)
4328 (fun n -> conf.scrollstep <- n);
4330 src#int "horizontal scroll step"
4331 (fun () -> conf.hscrollstep)
4332 (fun v -> conf.hscrollstep <- v);
4334 src#int "auto scroll step"
4335 (fun () ->
4336 match state.autoscroll with
4337 | Some step -> step
4338 | _ -> conf.autoscrollstep)
4339 (fun n ->
4340 if state.autoscroll <> None
4341 then state.autoscroll <- Some n;
4342 conf.autoscrollstep <- n);
4344 src#int "zoom"
4345 (fun () -> truncate (conf.zoom *. 100.))
4346 (fun v -> setzoom ((float v) /. 100.));
4348 src#int "rotation"
4349 (fun () -> conf.angle)
4350 (fun v -> reqlayout v conf.proportional);
4352 src#int "scroll bar width"
4353 (fun () -> state.scrollw)
4354 (fun v ->
4355 state.scrollw <- v;
4356 conf.scrollbw <- v;
4357 reshape state.winw state.winh;
4360 src#int "scroll handle height"
4361 (fun () -> conf.scrollh)
4362 (fun v -> conf.scrollh <- v;);
4364 src#int "thumbnail width"
4365 (fun () -> conf.thumbw)
4366 (fun v ->
4367 conf.thumbw <- min 4096 v;
4368 match oldmode with
4369 | Birdseye beye ->
4370 leavebirdseye beye false;
4371 enterbirdseye ()
4372 | _ -> ()
4375 let mode = state.mode in
4376 src#string "columns"
4377 (fun () ->
4378 match conf.columns with
4379 | Csingle _ -> "1"
4380 | Cmulti (multi, _) -> multicolumns_to_string multi
4381 | Csplit (count, _) -> "-" ^ string_of_int count
4383 (fun v ->
4384 let n, a, b = multicolumns_of_string v in
4385 setcolumns mode n a b);
4387 sep ();
4388 src#caption "Presentation mode" 0;
4389 src#bool "scrollbar visible"
4390 (fun () -> conf.scrollbarinpm)
4391 (fun v ->
4392 if v != conf.scrollbarinpm
4393 then (
4394 conf.scrollbarinpm <- v;
4395 if conf.presentation
4396 then (
4397 state.scrollw <- if v then conf.scrollbw else 0;
4398 reshape state.winw state.winh;
4403 sep ();
4404 src#caption "Pixmap cache" 0;
4405 src#int_with_suffix "size (advisory)"
4406 (fun () -> conf.memlimit)
4407 (fun v -> conf.memlimit <- v);
4409 src#caption2 "used"
4410 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4411 (string_with_suffix_of_int state.memused)
4412 (Hashtbl.length state.tilemap)) 1;
4414 sep ();
4415 src#caption "Layout" 0;
4416 src#caption2 "Dimension"
4417 (fun () ->
4418 Printf.sprintf "%dx%d (virtual %dx%d)"
4419 state.winw state.winh
4420 state.w state.maxy)
4422 if conf.debug
4423 then
4424 src#caption2 "Position" (fun () ->
4425 Printf.sprintf "%dx%d" state.x state.y
4427 else
4428 src#caption2 "Position" (fun () -> describe_location ()) 1
4431 sep ();
4432 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4433 "Save these parameters as global defaults at exit"
4434 (fun () -> conf.bedefault)
4435 (fun v -> conf.bedefault <- v)
4438 sep ();
4439 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4440 src#bool ~offset:0 ~btos "Extended parameters"
4441 (fun () -> !showextended)
4442 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4443 if !showextended
4444 then (
4445 src#bool "checkers"
4446 (fun () -> conf.checkers)
4447 (fun v -> conf.checkers <- v; setcheckers v);
4448 src#bool "update cursor"
4449 (fun () -> conf.updatecurs)
4450 (fun v -> conf.updatecurs <- v);
4451 src#bool "verbose"
4452 (fun () -> conf.verbose)
4453 (fun v -> conf.verbose <- v);
4454 src#bool "invert colors"
4455 (fun () -> conf.invert)
4456 (fun v -> conf.invert <- v);
4457 src#bool "max fit"
4458 (fun () -> conf.maxhfit)
4459 (fun v -> conf.maxhfit <- v);
4460 src#bool "redirect stderr"
4461 (fun () -> conf.redirectstderr)
4462 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4463 src#string "uri launcher"
4464 (fun () -> conf.urilauncher)
4465 (fun v -> conf.urilauncher <- v);
4466 src#string "path launcher"
4467 (fun () -> conf.pathlauncher)
4468 (fun v -> conf.pathlauncher <- v);
4469 src#string "tile size"
4470 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4471 (fun v ->
4473 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4474 conf.tilew <- max 64 w;
4475 conf.tileh <- max 64 h;
4476 flushtiles ();
4477 with exn ->
4478 state.text <- Printf.sprintf "bad tile size `%s': %s"
4479 v (exntos exn)
4481 src#int "texture count"
4482 (fun () -> conf.texcount)
4483 (fun v ->
4484 if realloctexts v
4485 then conf.texcount <- v
4486 else showtext '!' " Failed to set texture count please retry later"
4488 src#int "slice height"
4489 (fun () -> conf.sliceheight)
4490 (fun v ->
4491 conf.sliceheight <- v;
4492 wcmd "sliceh %d" conf.sliceheight;
4494 src#int "anti-aliasing level"
4495 (fun () -> conf.aalevel)
4496 (fun v ->
4497 conf.aalevel <- bound v 0 8;
4498 state.anchor <- getanchor ();
4499 opendoc state.path state.password;
4501 src#string "page scroll scaling factor"
4502 (fun () -> string_of_float conf.pgscale)
4503 (fun v ->
4505 let s = float_of_string v in
4506 conf.pgscale <- s
4507 with exn ->
4508 state.text <- Printf.sprintf
4509 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4512 src#int "ui font size"
4513 (fun () -> fstate.fontsize)
4514 (fun v -> setfontsize (bound v 5 100));
4515 src#int "hint font size"
4516 (fun () -> conf.hfsize)
4517 (fun v -> conf.hfsize <- bound v 5 100);
4518 colorp "background color"
4519 (fun () -> conf.bgcolor)
4520 (fun v -> conf.bgcolor <- v);
4521 src#bool "crop hack"
4522 (fun () -> conf.crophack)
4523 (fun v -> conf.crophack <- v);
4524 src#string "trim fuzz"
4525 (fun () -> irect_to_string conf.trimfuzz)
4526 (fun v ->
4528 conf.trimfuzz <- irect_of_string v;
4529 if conf.trimmargins
4530 then settrim true conf.trimfuzz;
4531 with exn ->
4532 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4534 src#string "throttle"
4535 (fun () ->
4536 match conf.maxwait with
4537 | None -> "show place holder if page is not ready"
4538 | Some time ->
4539 if time = infinity
4540 then "wait for page to fully render"
4541 else
4542 "wait " ^ string_of_float time
4543 ^ " seconds before showing placeholder"
4545 (fun v ->
4547 let f = float_of_string v in
4548 if f <= 0.0
4549 then conf.maxwait <- None
4550 else conf.maxwait <- Some f
4551 with exn ->
4552 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4554 src#string "ghyll scroll"
4555 (fun () ->
4556 match conf.ghyllscroll with
4557 | None -> ""
4558 | Some nab -> ghyllscroll_to_string nab
4560 (fun v ->
4562 let gs =
4563 if String.length v = 0
4564 then None
4565 else Some (ghyllscroll_of_string v)
4567 conf.ghyllscroll <- gs
4568 with exn ->
4569 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4571 src#string "selection command"
4572 (fun () -> conf.selcmd)
4573 (fun v -> conf.selcmd <- v);
4574 src#string "synctex command"
4575 (fun () -> conf.stcmd)
4576 (fun v -> conf.stcmd <- v);
4577 src#colorspace "color space"
4578 (fun () -> colorspace_to_string conf.colorspace)
4579 (fun v ->
4580 conf.colorspace <- colorspace_of_int v;
4581 wcmd "cs %d" v;
4582 load state.layout;
4584 if pbousable ()
4585 then
4586 src#bool "use PBO"
4587 (fun () -> conf.usepbo)
4588 (fun v -> conf.usepbo <- v);
4589 src#bool "mouse wheel scrolls pages"
4590 (fun () -> conf.wheelbypage)
4591 (fun v -> conf.wheelbypage <- v);
4594 sep ();
4595 src#caption "Document" 0;
4596 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4597 src#caption2 "Pages"
4598 (fun () -> string_of_int state.pagecount) 1;
4599 src#caption2 "Dimensions"
4600 (fun () -> string_of_int (List.length state.pdims)) 1;
4601 if conf.trimmargins
4602 then (
4603 sep ();
4604 src#caption "Trimmed margins" 0;
4605 src#caption2 "Dimensions"
4606 (fun () -> string_of_int (List.length state.pdims)) 1;
4609 sep ();
4610 src#caption "OpenGL" 0;
4611 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4612 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4613 src#reset prevmode prevuioh;
4615 fun () ->
4616 state.text <- "";
4617 let prevmode = state.mode
4618 and prevuioh = state.uioh in
4619 fillsrc prevmode prevuioh;
4620 let source = (src :> lvsource) in
4621 let modehash = findkeyhash conf "info" in
4622 state.uioh <- coe (object (self)
4623 inherit listview ~source ~trusted:true ~modehash as super
4624 val mutable m_prevmemused = 0
4625 method infochanged = function
4626 | Memused ->
4627 if m_prevmemused != state.memused
4628 then (
4629 m_prevmemused <- state.memused;
4630 G.postRedisplay "memusedchanged";
4632 | Pdim -> G.postRedisplay "pdimchanged"
4633 | Docinfo -> fillsrc prevmode prevuioh
4635 method key key mask =
4636 if not (Wsi.withctrl mask)
4637 then
4638 match key with
4639 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4640 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4641 | _ -> super#key key mask
4642 else super#key key mask
4643 end);
4644 G.postRedisplay "info";
4647 let enterhelpmode =
4648 let source =
4649 (object
4650 inherit lvsourcebase
4651 method getitemcount = Array.length state.help
4652 method getitem n =
4653 let s, l, _ = state.help.(n) in
4654 (s, l)
4656 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4657 let optuioh =
4658 if not cancel
4659 then (
4660 m_qsearch <- qsearch;
4661 match state.help.(active) with
4662 | _, _, Action f -> Some (f uioh)
4663 | _ -> Some (uioh)
4665 else None
4667 m_active <- active;
4668 m_first <- first;
4669 m_pan <- pan;
4670 optuioh
4672 method hasaction n =
4673 match state.help.(n) with
4674 | _, _, Action _ -> true
4675 | _ -> false
4677 initializer
4678 m_active <- -1
4679 end)
4680 in fun () ->
4681 let modehash = findkeyhash conf "help" in
4682 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4683 G.postRedisplay "help";
4686 let entermsgsmode =
4687 let msgsource =
4688 let re = Str.regexp "[\r\n]" in
4689 (object
4690 inherit lvsourcebase
4691 val mutable m_items = [||]
4693 method getitemcount = 1 + Array.length m_items
4695 method getitem n =
4696 if n = 0
4697 then "[Clear]", 0
4698 else m_items.(n-1), 0
4700 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4701 ignore uioh;
4702 if not cancel
4703 then (
4704 if active = 0
4705 then Buffer.clear state.errmsgs;
4706 m_qsearch <- qsearch;
4708 m_active <- active;
4709 m_first <- first;
4710 m_pan <- pan;
4711 None
4713 method hasaction n =
4714 n = 0
4716 method reset =
4717 state.newerrmsgs <- false;
4718 let l = Str.split re (Buffer.contents state.errmsgs) in
4719 m_items <- Array.of_list l
4721 initializer
4722 m_active <- 0
4723 end)
4724 in fun () ->
4725 state.text <- "";
4726 msgsource#reset;
4727 let source = (msgsource :> lvsource) in
4728 let modehash = findkeyhash conf "listview" in
4729 state.uioh <- coe (object
4730 inherit listview ~source ~trusted:false ~modehash as super
4731 method display =
4732 if state.newerrmsgs
4733 then msgsource#reset;
4734 super#display
4735 end);
4736 G.postRedisplay "msgs";
4739 let quickbookmark ?title () =
4740 match state.layout with
4741 | [] -> ()
4742 | l :: _ ->
4743 let title =
4744 match title with
4745 | None ->
4746 let sec = Unix.gettimeofday () in
4747 let tm = Unix.localtime sec in
4748 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4749 (l.pageno+1)
4750 tm.Unix.tm_mday
4751 tm.Unix.tm_mon
4752 (tm.Unix.tm_year + 1900)
4753 tm.Unix.tm_hour
4754 tm.Unix.tm_min
4755 | Some title -> title
4757 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4760 let doreshape w h =
4761 Wsi.reshape w h;
4764 let setautoscrollspeed step goingdown =
4765 let incr = max 1 ((abs step) / 2) in
4766 let incr = if goingdown then incr else -incr in
4767 let astep = step + incr in
4768 state.autoscroll <- Some astep;
4771 let gotounder = function
4772 | Ulinkgoto (pageno, top) ->
4773 if pageno >= 0
4774 then (
4775 addnav ();
4776 gotopage1 pageno top;
4779 | Ulinkuri s ->
4780 gotouri s
4782 | Uremote (filename, pageno) ->
4783 let path =
4784 if Sys.file_exists filename
4785 then filename
4786 else
4787 let dir = Filename.dirname state.path in
4788 let path = Filename.concat dir filename in
4789 if Sys.file_exists path
4790 then path
4791 else ""
4793 if String.length path > 0
4794 then (
4795 let anchor = getanchor () in
4796 let ranchor = state.path, state.password, anchor in
4797 state.anchor <- (pageno, 0.0, 0.0);
4798 state.ranchors <- ranchor :: state.ranchors;
4799 opendoc path "";
4801 else showtext '!' ("Could not find " ^ filename)
4803 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4806 let canpan () =
4807 match conf.columns with
4808 | Csplit _ -> true
4809 | _ -> state.x != 0 || conf.zoom > 1.0
4812 let existsinrow pageno (columns, coverA, coverB) p =
4813 let last = ((pageno - coverA) mod columns) + columns in
4814 let rec any = function
4815 | [] -> false
4816 | l :: rest ->
4817 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4818 then p l
4819 else (
4820 if not (p l)
4821 then (if l.pageno = last then false else any rest)
4822 else true
4825 any state.layout
4828 let nextpage () =
4829 match state.layout with
4830 | [] ->
4831 let pageno = page_of_y state.y in
4832 gotoghyll (getpagey (pageno+1))
4833 | l :: rest ->
4834 match conf.columns with
4835 | Csingle _ ->
4836 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4837 then
4838 let y = clamp (pgscale state.winh) in
4839 gotoghyll y
4840 else
4841 let pageno = min (l.pageno+1) (state.pagecount-1) in
4842 gotoghyll (getpagey pageno)
4843 | Cmulti ((c, _, _) as cl, _) ->
4844 if conf.presentation
4845 && (existsinrow l.pageno cl
4846 (fun l -> l.pageh > l.pagey + l.pagevh))
4847 then
4848 let y = clamp (pgscale state.winh) in
4849 gotoghyll y
4850 else
4851 let pageno = min (l.pageno+c) (state.pagecount-1) in
4852 gotoghyll (getpagey pageno)
4853 | Csplit (n, _) ->
4854 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4855 then
4856 let pagey, pageh = getpageyh l.pageno in
4857 let pagey = pagey + pageh * l.pagecol in
4858 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4859 gotoghyll (pagey + pageh + ips)
4862 let prevpage () =
4863 match state.layout with
4864 | [] ->
4865 let pageno = page_of_y state.y in
4866 gotoghyll (getpagey (pageno-1))
4867 | l :: _ ->
4868 match conf.columns with
4869 | Csingle _ ->
4870 if conf.presentation && l.pagey != 0
4871 then
4872 gotoghyll (clamp (pgscale ~-(state.winh)))
4873 else
4874 let pageno = max 0 (l.pageno-1) in
4875 gotoghyll (getpagey pageno)
4876 | Cmulti ((c, _, coverB) as cl, _) ->
4877 if conf.presentation &&
4878 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4879 then
4880 gotoghyll (clamp (pgscale ~-(state.winh)))
4881 else
4882 let decr =
4883 if l.pageno = state.pagecount - coverB
4884 then 1
4885 else c
4887 let pageno = max 0 (l.pageno-decr) in
4888 gotoghyll (getpagey pageno)
4889 | Csplit (n, _) ->
4890 let y =
4891 if l.pagecol = 0
4892 then
4893 if l.pageno = 0
4894 then l.pagey
4895 else
4896 let pageno = max 0 (l.pageno-1) in
4897 let pagey, pageh = getpageyh pageno in
4898 pagey + (n-1)*pageh
4899 else
4900 let pagey, pageh = getpageyh l.pageno in
4901 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4903 gotoghyll y
4906 let viewkeyboard key mask =
4907 let enttext te =
4908 let mode = state.mode in
4909 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4910 state.text <- "";
4911 enttext ();
4912 G.postRedisplay "view:enttext"
4914 let ctrl = Wsi.withctrl mask in
4915 let key =
4916 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4918 match key with
4919 | 81 -> (* Q *)
4920 exit 0
4922 | 0xff63 -> (* insert *)
4923 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4924 then (
4925 state.mode <- LinkNav (Ltgendir 0);
4926 gotoy state.y;
4928 else showtext '!' "Keyboard link navigation does not work under rotation"
4930 | 0xff1b | 113 -> (* escape / q *)
4931 begin match state.mstate with
4932 | Mzoomrect _ ->
4933 state.mstate <- Mnone;
4934 Wsi.setcursor Wsi.CURSOR_INHERIT;
4935 G.postRedisplay "kill zoom rect";
4936 | _ ->
4937 begin match state.mode with
4938 | LinkNav _ ->
4939 state.mode <- View;
4940 G.postRedisplay "esc leave linknav"
4941 | _ ->
4942 match state.ranchors with
4943 | [] -> raise Quit
4944 | (path, password, anchor) :: rest ->
4945 state.ranchors <- rest;
4946 state.anchor <- anchor;
4947 opendoc path password
4948 end;
4949 end;
4951 | 0xff08 -> (* backspace *)
4952 gotoghyll (getnav ~-1)
4954 | 111 -> (* o *)
4955 enteroutlinemode ()
4957 | 117 -> (* u *)
4958 state.rects <- [];
4959 state.text <- "";
4960 G.postRedisplay "dehighlight";
4962 | 47 | 63 -> (* / ? *)
4963 let ondone isforw s =
4964 cbput state.hists.pat s;
4965 state.searchpattern <- s;
4966 search s isforw
4968 let s = String.create 1 in
4969 s.[0] <- Char.chr key;
4970 enttext (s, "", Some (onhist state.hists.pat),
4971 textentry, ondone (key = 47), true)
4973 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4974 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4975 setzoom (conf.zoom +. incr)
4977 | 43 | 0xffab -> (* + *)
4978 let ondone s =
4979 let n =
4980 try int_of_string s with exc ->
4981 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4982 max_int
4984 if n != max_int
4985 then (
4986 conf.pagebias <- n;
4987 state.text <- "page bias is now " ^ string_of_int n;
4990 enttext ("page bias: ", "", None, intentry, ondone, true)
4992 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4993 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4994 setzoom (max 0.01 (conf.zoom -. decr))
4996 | 45 | 0xffad -> (* - *)
4997 let ondone msg = state.text <- msg in
4998 enttext (
4999 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5000 optentry state.mode, ondone, true
5003 | 48 when ctrl -> (* ctrl-0 *)
5004 if conf.zoom = 1.0
5005 then (
5006 state.x <- 0;
5007 state.hscrollh <-
5008 if state.w <= state.winw - state.scrollw
5009 then 0
5010 else state.scrollw
5012 gotoy state.y
5014 else setzoom 1.0
5016 | 49 when ctrl -> (* ctrl-1 *)
5017 let cols =
5018 match conf.columns with
5019 | Csingle _ | Cmulti _ -> 1
5020 | Csplit (n, _) -> n
5022 let h = state.winh -
5023 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5025 let zoom = zoomforh state.winw h state.scrollw cols in
5026 if zoom < 1.0
5027 then setzoom zoom
5029 | 0xffc6 -> (* f9 *)
5030 togglebirdseye ()
5032 | 57 when ctrl -> (* ctrl-9 *)
5033 togglebirdseye ()
5035 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5036 when not ctrl -> (* 0..9 *)
5037 let ondone s =
5038 let n =
5039 try int_of_string s with exc ->
5040 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5043 if n >= 0
5044 then (
5045 addnav ();
5046 cbput state.hists.pag (string_of_int n);
5047 gotopage1 (n + conf.pagebias - 1) 0;
5050 let pageentry text key =
5051 match Char.unsafe_chr key with
5052 | 'g' -> TEdone text
5053 | _ -> intentry text key
5055 let text = "x" in text.[0] <- Char.chr key;
5056 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5058 | 98 -> (* b *)
5059 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5060 reshape state.winw state.winh;
5062 | 108 -> (* l *)
5063 conf.hlinks <- not conf.hlinks;
5064 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5065 G.postRedisplay "toggle highlightlinks";
5067 | 70 -> (* F *)
5068 state.glinks <- true;
5069 let mode = state.mode in
5070 state.mode <- Textentry (
5071 (":", "", None, linknentry, linkndone gotounder, false),
5072 (fun _ ->
5073 state.glinks <- false;
5074 state.mode <- mode)
5076 state.text <- "";
5077 G.postRedisplay "view:linkent(F)"
5079 | 121 -> (* y *)
5080 state.glinks <- true;
5081 let mode = state.mode in
5082 state.mode <- Textentry (
5083 (":", "", None, linknentry, linkndone (fun under ->
5084 match Ne.pipe () with
5085 | Ne.Exn exn ->
5086 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5087 | Ne.Res (r, w) ->
5088 let popened =
5089 try popen conf.selcmd [r, 0; w, -1]; true
5090 with exn ->
5091 showtext '!'
5092 (Printf.sprintf "failed to execute %s: %s"
5093 conf.selcmd (exntos exn));
5094 false
5096 let clo cap fd =
5097 Ne.clo fd (fun msg ->
5098 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5101 let s = undertext under in
5102 if popened
5103 then
5104 (try
5105 let l = String.length s in
5106 let n = tempfailureretry (Unix.write w s 0) l in
5107 if n != l
5108 then
5109 showtext '!'
5110 (Printf.sprintf
5111 "failed to write %d characters to sel pipe, wrote %d"
5114 with exn ->
5115 showtext '!'
5116 (Printf.sprintf "failed to write to sel pipe: %s"
5117 (exntos exn)
5120 else dolog "%s" s;
5121 clo "pipe/r" r;
5122 clo "pipe/w" w;
5123 ), false
5125 fun _ ->
5126 state.glinks <- false;
5127 state.mode <- mode
5129 state.text <- "";
5130 G.postRedisplay "view:linkent"
5132 | 97 -> (* a *)
5133 begin match state.autoscroll with
5134 | Some step ->
5135 conf.autoscrollstep <- step;
5136 state.autoscroll <- None
5137 | None ->
5138 if conf.autoscrollstep = 0
5139 then state.autoscroll <- Some 1
5140 else state.autoscroll <- Some conf.autoscrollstep
5143 | 112 when ctrl -> (* ctrl-p *)
5144 launchpath ()
5146 | 80 -> (* P *)
5147 setpresentationmode (not conf.presentation);
5148 showtext ' ' ("presentation mode " ^
5149 if conf.presentation then "on" else "off");
5151 | 102 -> (* f *)
5152 if List.mem Wsi.Fullscreen state.winstate
5153 then doreshape conf.cwinw conf.cwinh
5154 else Wsi.fullscreen ()
5156 | 112 | 78 -> (* p|N *)
5157 search state.searchpattern false
5159 | 110 | 0xffc0 -> (* n|F3 *)
5160 search state.searchpattern true
5162 | 116 -> (* t *)
5163 begin match state.layout with
5164 | [] -> ()
5165 | l :: _ ->
5166 gotoghyll (getpagey l.pageno)
5169 | 32 -> (* space *)
5170 nextpage ()
5172 | 0xff9f | 0xffff -> (* delete *)
5173 prevpage ()
5175 | 61 -> (* = *)
5176 showtext ' ' (describe_location ());
5178 | 119 -> (* w *)
5179 begin match state.layout with
5180 | [] -> ()
5181 | l :: _ ->
5182 doreshape (l.pagew + state.scrollw) l.pageh;
5183 G.postRedisplay "w"
5186 | 39 -> (* ' *)
5187 enterbookmarkmode ()
5189 | 104 | 0xffbe -> (* h|F1 *)
5190 enterhelpmode ()
5192 | 105 -> (* i *)
5193 enterinfomode ()
5195 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5196 entermsgsmode ()
5198 | 109 -> (* m *)
5199 let ondone s =
5200 match state.layout with
5201 | l :: _ ->
5202 if String.length s > 0
5203 then
5204 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5205 | _ -> ()
5207 enttext ("bookmark: ", "", None, textentry, ondone, true)
5209 | 126 -> (* ~ *)
5210 quickbookmark ();
5211 showtext ' ' "Quick bookmark added";
5213 | 122 -> (* z *)
5214 begin match state.layout with
5215 | l :: _ ->
5216 let rect = getpdimrect l.pagedimno in
5217 let w, h =
5218 if conf.crophack
5219 then
5220 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5221 truncate (1.2 *. (rect.(3) -. rect.(0))))
5222 else
5223 (truncate (rect.(1) -. rect.(0)),
5224 truncate (rect.(3) -. rect.(0)))
5226 let w = truncate ((float w)*.conf.zoom)
5227 and h = truncate ((float h)*.conf.zoom) in
5228 if w != 0 && h != 0
5229 then (
5230 state.anchor <- getanchor ();
5231 doreshape (w + state.scrollw) (h + conf.interpagespace)
5233 G.postRedisplay "z";
5235 | [] -> ()
5238 | 60 | 62 -> (* < > *)
5239 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5241 | 91 | 93 -> (* [ ] *)
5242 conf.colorscale <-
5243 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5245 G.postRedisplay "brightness";
5247 | 99 when state.mode = View -> (* c *)
5248 let (c, a, b), z =
5249 match state.prevcolumns with
5250 | None -> (1, 0, 0), 1.0
5251 | Some (columns, z) ->
5252 let cab =
5253 match columns with
5254 | Csplit (c, _) -> -c, 0, 0
5255 | Cmulti ((c, a, b), _) -> c, a, b
5256 | Csingle _ -> 1, 0, 0
5258 cab, z
5260 setcolumns View c a b;
5261 setzoom z;
5263 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5264 setzoom state.prevzoom
5266 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5267 begin match state.autoscroll with
5268 | None ->
5269 begin match state.mode with
5270 | Birdseye beye -> upbirdseye 1 beye
5271 | _ ->
5272 if ctrl
5273 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5274 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5276 | Some n ->
5277 setautoscrollspeed n false
5280 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5281 begin match state.autoscroll with
5282 | None ->
5283 begin match state.mode with
5284 | Birdseye beye -> downbirdseye 1 beye
5285 | _ ->
5286 if ctrl
5287 then gotoy_and_clear_text (clamp (state.winh/2))
5288 else gotoy_and_clear_text (clamp conf.scrollstep)
5290 | Some n ->
5291 setautoscrollspeed n true
5294 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5295 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5296 if canpan ()
5297 then
5298 let dx =
5299 if ctrl
5300 then state.winw / 2
5301 else conf.hscrollstep
5303 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5304 state.x <- state.x + dx;
5305 gotoy_and_clear_text state.y
5306 else (
5307 state.text <- "";
5308 G.postRedisplay "lef/right"
5311 | 0xff55 | 0xff9a -> (* (kp) prior *)
5312 let y =
5313 if ctrl
5314 then
5315 match state.layout with
5316 | [] -> state.y
5317 | l :: _ -> state.y - l.pagey
5318 else
5319 clamp (pgscale (-state.winh))
5321 gotoghyll y
5323 | 0xff56 | 0xff9b -> (* (kp) next *)
5324 let y =
5325 if ctrl
5326 then
5327 match List.rev state.layout with
5328 | [] -> state.y
5329 | l :: _ -> getpagey l.pageno
5330 else
5331 clamp (pgscale state.winh)
5333 gotoghyll y
5335 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5336 gotoghyll 0
5337 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5338 gotoghyll (clamp state.maxy)
5340 | 0xff53 | 0xff98
5341 when Wsi.withalt mask -> (* alt-(kp) right *)
5342 gotoghyll (getnav 1)
5343 | 0xff51 | 0xff96
5344 when Wsi.withalt mask -> (* alt-(kp) left *)
5345 gotoghyll (getnav ~-1)
5347 | 114 -> (* r *)
5348 reload ()
5350 | 118 when conf.debug -> (* v *)
5351 state.rects <- [];
5352 List.iter (fun l ->
5353 match getopaque l.pageno with
5354 | None -> ()
5355 | Some opaque ->
5356 let x0, y0, x1, y1 = pagebbox opaque in
5357 let a,b = float x0, float y0 in
5358 let c,d = float x1, float y0 in
5359 let e,f = float x1, float y1 in
5360 let h,j = float x0, float y1 in
5361 let rect = (a,b,c,d,e,f,h,j) in
5362 debugrect rect;
5363 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5364 ) state.layout;
5365 G.postRedisplay "v";
5367 | _ ->
5368 vlog "huh? %s" (Wsi.keyname key)
5371 let linknavkeyboard key mask linknav =
5372 let getpage pageno =
5373 let rec loop = function
5374 | [] -> None
5375 | l :: _ when l.pageno = pageno -> Some l
5376 | _ :: rest -> loop rest
5377 in loop state.layout
5379 let doexact (pageno, n) =
5380 match getopaque pageno, getpage pageno with
5381 | Some opaque, Some l ->
5382 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5383 then
5384 let under = getlink opaque n in
5385 G.postRedisplay "link gotounder";
5386 gotounder under;
5387 state.mode <- View;
5388 else
5389 let opt, dir =
5390 match key with
5391 | 0xff50 -> (* home *)
5392 Some (findlink opaque LDfirst), -1
5394 | 0xff57 -> (* end *)
5395 Some (findlink opaque LDlast), 1
5397 | 0xff51 -> (* left *)
5398 Some (findlink opaque (LDleft n)), -1
5400 | 0xff53 -> (* right *)
5401 Some (findlink opaque (LDright n)), 1
5403 | 0xff52 -> (* up *)
5404 Some (findlink opaque (LDup n)), -1
5406 | 0xff54 -> (* down *)
5407 Some (findlink opaque (LDdown n)), 1
5409 | _ -> None, 0
5411 let pwl l dir =
5412 begin match findpwl l.pageno dir with
5413 | Pwlnotfound -> ()
5414 | Pwl pageno ->
5415 let notfound dir =
5416 state.mode <- LinkNav (Ltgendir dir);
5417 let y, h = getpageyh pageno in
5418 let y =
5419 if dir < 0
5420 then y + h - state.winh
5421 else y
5423 gotoy y
5425 begin match getopaque pageno, getpage pageno with
5426 | Some opaque, Some _ ->
5427 let link =
5428 let ld = if dir > 0 then LDfirst else LDlast in
5429 findlink opaque ld
5431 begin match link with
5432 | Lfound m ->
5433 showlinktype (getlink opaque m);
5434 state.mode <- LinkNav (Ltexact (pageno, m));
5435 G.postRedisplay "linknav jpage";
5436 | _ -> notfound dir
5437 end;
5438 | _ -> notfound dir
5439 end;
5440 end;
5442 begin match opt with
5443 | Some Lnotfound -> pwl l dir;
5444 | Some (Lfound m) ->
5445 if m = n
5446 then pwl l dir
5447 else (
5448 let _, y0, _, y1 = getlinkrect opaque m in
5449 if y0 < l.pagey
5450 then gotopage1 l.pageno y0
5451 else (
5452 let d = fstate.fontsize + 1 in
5453 if y1 - l.pagey > l.pagevh - d
5454 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5455 else G.postRedisplay "linknav";
5457 showlinktype (getlink opaque m);
5458 state.mode <- LinkNav (Ltexact (l.pageno, m));
5461 | None -> viewkeyboard key mask
5462 end;
5463 | _ -> viewkeyboard key mask
5465 if key = 0xff63
5466 then (
5467 state.mode <- View;
5468 G.postRedisplay "leave linknav"
5470 else
5471 match linknav with
5472 | Ltgendir _ -> viewkeyboard key mask
5473 | Ltexact exact -> doexact exact
5476 let keyboard key mask =
5477 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5478 then wcmd "interrupt"
5479 else state.uioh <- state.uioh#key key mask
5482 let birdseyekeyboard key mask
5483 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5484 let incr =
5485 match conf.columns with
5486 | Csingle _ -> 1
5487 | Cmulti ((c, _, _), _) -> c
5488 | Csplit _ -> failwith "bird's eye split mode"
5490 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5491 match key with
5492 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5493 let y, h = getpageyh pageno in
5494 let top = (state.winh - h) / 2 in
5495 gotoy (max 0 (y - top))
5496 | 0xff0d (* enter *)
5497 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5498 | 0xff1b -> leavebirdseye beye true (* escape *)
5499 | 0xff52 -> upbirdseye incr beye (* up *)
5500 | 0xff54 -> downbirdseye incr beye (* down *)
5501 | 0xff51 -> upbirdseye 1 beye (* left *)
5502 | 0xff53 -> downbirdseye 1 beye (* right *)
5504 | 0xff55 -> (* prior *)
5505 begin match state.layout with
5506 | l :: _ ->
5507 if l.pagey != 0
5508 then (
5509 state.mode <- Birdseye (
5510 oconf, leftx, l.pageno, hooverpageno, anchor
5512 gotopage1 l.pageno 0;
5514 else (
5515 let layout = layout (state.y-state.winh) (pgh state.layout) in
5516 match layout with
5517 | [] -> gotoy (clamp (-state.winh))
5518 | l :: _ ->
5519 state.mode <- Birdseye (
5520 oconf, leftx, l.pageno, hooverpageno, anchor
5522 gotopage1 l.pageno 0
5525 | [] -> gotoy (clamp (-state.winh))
5526 end;
5528 | 0xff56 -> (* next *)
5529 begin match List.rev state.layout with
5530 | l :: _ ->
5531 let layout = layout (state.y + (pgh state.layout)) state.winh in
5532 begin match layout with
5533 | [] ->
5534 let incr = l.pageh - l.pagevh in
5535 if incr = 0
5536 then (
5537 state.mode <-
5538 Birdseye (
5539 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5541 G.postRedisplay "birdseye pagedown";
5543 else gotoy (clamp (incr + conf.interpagespace*2));
5545 | l :: _ ->
5546 state.mode <-
5547 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5548 gotopage1 l.pageno 0;
5551 | [] -> gotoy (clamp state.winh)
5552 end;
5554 | 0xff50 -> (* home *)
5555 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5556 gotopage1 0 0
5558 | 0xff57 -> (* end *)
5559 let pageno = state.pagecount - 1 in
5560 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5561 if not (pagevisible state.layout pageno)
5562 then
5563 let h =
5564 match List.rev state.pdims with
5565 | [] -> state.winh
5566 | (_, _, h, _) :: _ -> h
5568 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5569 else G.postRedisplay "birdseye end";
5570 | _ -> viewkeyboard key mask
5573 let drawpage l linkindexbase =
5574 let color =
5575 match state.mode with
5576 | Textentry _ -> scalecolor 0.4
5577 | LinkNav _
5578 | View -> scalecolor 1.0
5579 | Birdseye (_, _, pageno, hooverpageno, _) ->
5580 if l.pageno = hooverpageno
5581 then scalecolor 0.9
5582 else (
5583 if l.pageno = pageno
5584 then scalecolor 1.0
5585 else scalecolor 0.8
5588 drawtiles l color;
5589 begin match getopaque l.pageno with
5590 | Some opaque ->
5591 if tileready l l.pagex l.pagey
5592 then
5593 let x = l.pagedispx - l.pagex
5594 and y = l.pagedispy - l.pagey in
5595 let hlmask =
5596 match conf.columns with
5597 | Csingle _ | Cmulti _ ->
5598 (if conf.hlinks then 1 else 0)
5599 + (if state.glinks
5600 && not (isbirdseye state.mode) then 2 else 0)
5601 | _ -> 0
5603 let s =
5604 match state.mode with
5605 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5606 | _ -> ""
5608 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5609 else 0
5611 | _ -> 0
5612 end;
5615 let scrollindicator () =
5616 let sbw, ph, sh = state.uioh#scrollph in
5617 let sbh, pw, sw = state.uioh#scrollpw in
5619 GlDraw.color (0.64, 0.64, 0.64);
5620 GlDraw.rect
5621 (float (state.winw - sbw), 0.)
5622 (float state.winw, float state.winh)
5624 GlDraw.rect
5625 (0., float (state.winh - sbh))
5626 (float (state.winw - state.scrollw - 1), float state.winh)
5628 GlDraw.color (0.0, 0.0, 0.0);
5630 GlDraw.rect
5631 (float (state.winw - sbw), ph)
5632 (float state.winw, ph +. sh)
5634 GlDraw.rect
5635 (pw, float (state.winh - sbh))
5636 (pw +. sw, float state.winh)
5640 let showsel () =
5641 match state.mstate with
5642 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5645 | Msel ((x0, y0), (x1, y1)) ->
5646 let rec loop = function
5647 | l :: ls ->
5648 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5649 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5650 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5651 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5652 then
5653 match getopaque l.pageno with
5654 | Some opaque ->
5655 let x0, y0 = pagetranslatepoint l x0 y0 in
5656 let x1, y1 = pagetranslatepoint l x1 y1 in
5657 seltext opaque (x0, y0, x1, y1);
5658 | _ -> ()
5659 else loop ls
5660 | [] -> ()
5662 loop state.layout
5665 let showrects rects =
5666 Gl.enable `blend;
5667 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5668 GlDraw.polygon_mode `both `fill;
5669 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5670 List.iter
5671 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5672 List.iter (fun l ->
5673 if l.pageno = pageno
5674 then (
5675 let dx = float (l.pagedispx - l.pagex) in
5676 let dy = float (l.pagedispy - l.pagey) in
5677 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5678 GlDraw.begins `quads;
5680 GlDraw.vertex2 (x0+.dx, y0+.dy);
5681 GlDraw.vertex2 (x1+.dx, y1+.dy);
5682 GlDraw.vertex2 (x2+.dx, y2+.dy);
5683 GlDraw.vertex2 (x3+.dx, y3+.dy);
5685 GlDraw.ends ();
5687 ) state.layout
5688 ) rects
5690 Gl.disable `blend;
5693 let display () =
5694 GlClear.color (scalecolor2 conf.bgcolor);
5695 GlClear.clear [`color];
5696 let rec loop linkindexbase = function
5697 | l :: rest ->
5698 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5699 loop linkindexbase rest
5700 | [] -> ()
5702 loop 0 state.layout;
5703 let rects =
5704 match state.mode with
5705 | LinkNav (Ltexact (pageno, linkno)) ->
5706 begin match getopaque pageno with
5707 | Some opaque ->
5708 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5709 (pageno, 5, (
5710 float x0, float y0,
5711 float x1, float y0,
5712 float x1, float y1,
5713 float x0, float y1)
5714 ) :: state.rects
5715 | None -> state.rects
5717 | _ -> state.rects
5719 showrects rects;
5720 showsel ();
5721 state.uioh#display;
5722 begin match state.mstate with
5723 | Mzoomrect ((x0, y0), (x1, y1)) ->
5724 Gl.enable `blend;
5725 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5726 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5727 GlDraw.rect (float x0, float y0)
5728 (float x1, float y1);
5729 Gl.disable `blend;
5730 | _ -> ()
5731 end;
5732 enttext ();
5733 scrollindicator ();
5734 Wsi.swapb ();
5737 let zoomrect x y x1 y1 =
5738 let x0 = min x x1
5739 and x1 = max x x1
5740 and y0 = min y y1 in
5741 gotoy (state.y + y0);
5742 state.anchor <- getanchor ();
5743 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5744 let margin =
5745 if state.w < state.winw - state.scrollw
5746 then (state.winw - state.scrollw - state.w) / 2
5747 else 0
5749 state.x <- (state.x + margin) - x0;
5750 setzoom zoom;
5751 Wsi.setcursor Wsi.CURSOR_INHERIT;
5752 state.mstate <- Mnone;
5755 let scrollx x =
5756 let winw = state.winw - state.scrollw - 1 in
5757 let s = float x /. float winw in
5758 let destx = truncate (float (state.w + winw) *. s) in
5759 state.x <- winw - destx;
5760 gotoy_and_clear_text state.y;
5761 state.mstate <- Mscrollx;
5764 let scrolly y =
5765 let s = float y /. float state.winh in
5766 let desty = truncate (float (state.maxy - state.winh) *. s) in
5767 gotoy_and_clear_text desty;
5768 state.mstate <- Mscrolly;
5771 let viewmouse button down x y mask =
5772 match button with
5773 | n when (n == 4 || n == 5) && not down ->
5774 if Wsi.withctrl mask
5775 then (
5776 match state.mstate with
5777 | Mzoom (oldn, i) ->
5778 if oldn = n
5779 then (
5780 if i = 2
5781 then
5782 let incr =
5783 match n with
5784 | 5 ->
5785 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5786 | _ ->
5787 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5789 let zoom = conf.zoom -. incr in
5790 setzoom zoom;
5791 state.mstate <- Mzoom (n, 0);
5792 else
5793 state.mstate <- Mzoom (n, i+1);
5795 else state.mstate <- Mzoom (n, 0)
5797 | _ -> state.mstate <- Mzoom (n, 0)
5799 else (
5800 match state.autoscroll with
5801 | Some step -> setautoscrollspeed step (n=4)
5802 | None ->
5803 if conf.wheelbypage
5804 then (
5805 if n = 4
5806 then prevpage ()
5807 else nextpage ()
5809 else
5810 let incr =
5811 if n = 4
5812 then -conf.scrollstep
5813 else conf.scrollstep
5815 let incr = incr * 2 in
5816 let y = clamp incr in
5817 gotoy_and_clear_text y
5820 | n when (n = 6 || n = 7) && not down && canpan () ->
5821 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5822 gotoy_and_clear_text state.y
5824 | 1 when Wsi.withshift mask ->
5825 state.mstate <- Mnone;
5826 if not down
5827 then (
5828 match unproject x y with
5829 | Some (pageno, ux, uy) ->
5830 let cmd = Printf.sprintf
5831 "%s %s %d %d %d"
5832 conf.stcmd state.path pageno ux uy
5834 popen cmd []
5835 | None -> ()
5838 | 1 when Wsi.withctrl mask ->
5839 if down
5840 then (
5841 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5842 state.mstate <- Mpan (x, y)
5844 else
5845 state.mstate <- Mnone
5847 | 3 ->
5848 if down
5849 then (
5850 Wsi.setcursor Wsi.CURSOR_CYCLE;
5851 let p = (x, y) in
5852 state.mstate <- Mzoomrect (p, p)
5854 else (
5855 match state.mstate with
5856 | Mzoomrect ((x0, y0), _) ->
5857 if abs (x-x0) > 10 && abs (y - y0) > 10
5858 then zoomrect x0 y0 x y
5859 else (
5860 state.mstate <- Mnone;
5861 Wsi.setcursor Wsi.CURSOR_INHERIT;
5862 G.postRedisplay "kill accidental zoom rect";
5864 | _ ->
5865 Wsi.setcursor Wsi.CURSOR_INHERIT;
5866 state.mstate <- Mnone
5869 | 1 when x > state.winw - state.scrollw ->
5870 if down
5871 then
5872 let _, position, sh = state.uioh#scrollph in
5873 if y > truncate position && y < truncate (position +. sh)
5874 then state.mstate <- Mscrolly
5875 else scrolly y
5876 else
5877 state.mstate <- Mnone
5879 | 1 when y > state.winh - state.hscrollh ->
5880 if down
5881 then
5882 let _, position, sw = state.uioh#scrollpw in
5883 if x > truncate position && x < truncate (position +. sw)
5884 then state.mstate <- Mscrollx
5885 else scrollx x
5886 else
5887 state.mstate <- Mnone
5889 | 1 ->
5890 let dest = if down then getunder x y else Unone in
5891 begin match dest with
5892 | Ulinkgoto _
5893 | Ulinkuri _
5894 | Uremote _
5895 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5896 gotounder dest
5898 | Unone when down ->
5899 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5900 state.mstate <- Mpan (x, y);
5902 | Unone | Utext _ ->
5903 if down
5904 then (
5905 if conf.angle mod 360 = 0
5906 then (
5907 state.mstate <- Msel ((x, y), (x, y));
5908 G.postRedisplay "mouse select";
5911 else (
5912 match state.mstate with
5913 | Mnone -> ()
5915 | Mzoom _ | Mscrollx | Mscrolly ->
5916 state.mstate <- Mnone
5918 | Mzoomrect ((x0, y0), _) ->
5919 zoomrect x0 y0 x y
5921 | Mpan _ ->
5922 Wsi.setcursor Wsi.CURSOR_INHERIT;
5923 state.mstate <- Mnone
5925 | Msel ((x0, y0), (x1, y1)) ->
5926 let rec loop = function
5927 | [] -> ()
5928 | l :: rest ->
5929 let inside =
5930 let a0 = l.pagedispy in
5931 let a1 = a0 + l.pagevh in
5932 let b0 = l.pagedispx in
5933 let b1 = b0 + l.pagevw in
5934 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5935 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5937 if inside
5938 then
5939 match getopaque l.pageno with
5940 | Some opaque ->
5941 begin
5942 match Ne.pipe () with
5943 | Ne.Exn exn ->
5944 showtext '!'
5945 (Printf.sprintf
5946 "can not create sel pipe: %s"
5947 (exntos exn));
5948 | Ne.Res (r, w) ->
5949 let doclose what fd =
5950 Ne.clo fd (fun msg ->
5951 dolog "%s close failed: %s" what msg)
5954 popen conf.selcmd [r, 0; w, -1];
5955 copysel w opaque;
5956 doclose "pipe/r" r;
5957 G.postRedisplay "copysel";
5958 with exn ->
5959 dolog "can not execute %S: %s"
5960 conf.selcmd (exntos exn);
5961 doclose "pipe/r" r;
5962 doclose "pipe/w" w;
5964 | None -> ()
5965 else loop rest
5967 loop state.layout;
5968 Wsi.setcursor Wsi.CURSOR_INHERIT;
5969 state.mstate <- Mnone;
5973 | _ -> ()
5976 let birdseyemouse button down x y mask
5977 (conf, leftx, _, hooverpageno, anchor) =
5978 match button with
5979 | 1 when down ->
5980 let rec loop = function
5981 | [] -> ()
5982 | l :: rest ->
5983 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5984 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5985 then (
5986 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5988 else loop rest
5990 loop state.layout
5991 | 3 -> ()
5992 | _ -> viewmouse button down x y mask
5995 let mouse button down x y mask =
5996 state.uioh <- state.uioh#button button down x y mask;
5999 let motion ~x ~y =
6000 state.uioh <- state.uioh#motion x y
6003 let pmotion ~x ~y =
6004 state.uioh <- state.uioh#pmotion x y;
6007 let uioh = object
6008 method display = ()
6010 method key key mask =
6011 begin match state.mode with
6012 | Textentry textentry -> textentrykeyboard key mask textentry
6013 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6014 | View -> viewkeyboard key mask
6015 | LinkNav linknav -> linknavkeyboard key mask linknav
6016 end;
6017 state.uioh
6019 method button button bstate x y mask =
6020 begin match state.mode with
6021 | LinkNav _
6022 | View -> viewmouse button bstate x y mask
6023 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6024 | Textentry _ -> ()
6025 end;
6026 state.uioh
6028 method motion x y =
6029 begin match state.mode with
6030 | Textentry _ -> ()
6031 | View | Birdseye _ | LinkNav _ ->
6032 match state.mstate with
6033 | Mzoom _ | Mnone -> ()
6035 | Mpan (x0, y0) ->
6036 let dx = x - x0
6037 and dy = y0 - y in
6038 state.mstate <- Mpan (x, y);
6039 if canpan ()
6040 then state.x <- state.x + dx;
6041 let y = clamp dy in
6042 gotoy_and_clear_text y
6044 | Msel (a, _) ->
6045 state.mstate <- Msel (a, (x, y));
6046 G.postRedisplay "motion select";
6048 | Mscrolly ->
6049 let y = min state.winh (max 0 y) in
6050 scrolly y
6052 | Mscrollx ->
6053 let x = min state.winw (max 0 x) in
6054 scrollx x
6056 | Mzoomrect (p0, _) ->
6057 state.mstate <- Mzoomrect (p0, (x, y));
6058 G.postRedisplay "motion zoomrect";
6059 end;
6060 state.uioh
6062 method pmotion x y =
6063 begin match state.mode with
6064 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6065 let rec loop = function
6066 | [] ->
6067 if hooverpageno != -1
6068 then (
6069 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6070 G.postRedisplay "pmotion birdseye no hoover";
6072 | l :: rest ->
6073 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6074 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6075 then (
6076 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6077 G.postRedisplay "pmotion birdseye hoover";
6079 else loop rest
6081 loop state.layout
6083 | Textentry _ -> ()
6085 | LinkNav _
6086 | View ->
6087 match state.mstate with
6088 | Mnone -> updateunder x y
6089 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6091 end;
6092 state.uioh
6094 method infochanged _ = ()
6096 method scrollph =
6097 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6098 let p, h = scrollph state.y maxy in
6099 state.scrollw, p, h
6101 method scrollpw =
6102 let winw = state.winw - state.scrollw - 1 in
6103 let fwinw = float winw in
6104 let sw =
6105 let sw = fwinw /. float state.w in
6106 let sw = fwinw *. sw in
6107 max sw (float conf.scrollh)
6109 let position, sw =
6110 let f = state.w+winw in
6111 let r = float (winw-state.x) /. float f in
6112 let p = fwinw *. r in
6113 p-.sw/.2., sw
6115 let sw =
6116 if position +. sw > fwinw
6117 then fwinw -. position
6118 else sw
6120 state.hscrollh, position, sw
6122 method modehash =
6123 let modename =
6124 match state.mode with
6125 | LinkNav _ -> "links"
6126 | Textentry _ -> "textentry"
6127 | Birdseye _ -> "birdseye"
6128 | View -> "view"
6130 findkeyhash conf modename
6131 end;;
6133 module Config =
6134 struct
6135 open Parser
6137 let fontpath = ref "";;
6139 module KeyMap =
6140 Map.Make (struct type t = (int * int) let compare = compare end);;
6142 let unent s =
6143 let l = String.length s in
6144 let b = Buffer.create l in
6145 unent b s 0 l;
6146 Buffer.contents b;
6149 let home =
6150 try Sys.getenv "HOME"
6151 with exn ->
6152 prerr_endline
6153 ("Can not determine home directory location: " ^ exntos exn);
6157 let modifier_of_string = function
6158 | "alt" -> Wsi.altmask
6159 | "shift" -> Wsi.shiftmask
6160 | "ctrl" | "control" -> Wsi.ctrlmask
6161 | "meta" -> Wsi.metamask
6162 | _ -> 0
6165 let key_of_string =
6166 let r = Str.regexp "-" in
6167 fun s ->
6168 let elems = Str.full_split r s in
6169 let f n k m =
6170 let g s =
6171 let m1 = modifier_of_string s in
6172 if m1 = 0
6173 then (Wsi.namekey s, m)
6174 else (k, m lor m1)
6175 in function
6176 | Str.Delim s when n land 1 = 0 -> g s
6177 | Str.Text s -> g s
6178 | Str.Delim _ -> (k, m)
6180 let rec loop n k m = function
6181 | [] -> (k, m)
6182 | x :: xs ->
6183 let k, m = f n k m x in
6184 loop (n+1) k m xs
6186 loop 0 0 0 elems
6189 let keys_of_string =
6190 let r = Str.regexp "[ \t]" in
6191 fun s ->
6192 let elems = Str.split r s in
6193 List.map key_of_string elems
6196 let copykeyhashes c =
6197 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6200 let config_of c attrs =
6201 let apply c k v =
6203 match k with
6204 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6205 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6206 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6207 | "preload" -> { c with preload = bool_of_string v }
6208 | "page-bias" -> { c with pagebias = int_of_string v }
6209 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6210 | "horizontal-scroll-step" ->
6211 { c with hscrollstep = max (int_of_string v) 1 }
6212 | "auto-scroll-step" ->
6213 { c with autoscrollstep = max 0 (int_of_string v) }
6214 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6215 | "crop-hack" -> { c with crophack = bool_of_string v }
6216 | "throttle" ->
6217 let mw =
6218 match String.lowercase v with
6219 | "true" -> Some infinity
6220 | "false" -> None
6221 | f -> Some (float_of_string f)
6223 { c with maxwait = mw}
6224 | "highlight-links" -> { c with hlinks = bool_of_string v }
6225 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6226 | "vertical-margin" ->
6227 { c with interpagespace = max 0 (int_of_string v) }
6228 | "zoom" ->
6229 let zoom = float_of_string v /. 100. in
6230 let zoom = max zoom 0.0 in
6231 { c with zoom = zoom }
6232 | "presentation" -> { c with presentation = bool_of_string v }
6233 | "rotation-angle" -> { c with angle = int_of_string v }
6234 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6235 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6236 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6237 | "proportional-display" -> { c with proportional = bool_of_string v }
6238 | "pixmap-cache-size" ->
6239 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6240 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6241 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6242 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6243 | "persistent-location" -> { c with jumpback = bool_of_string v }
6244 | "background-color" -> { c with bgcolor = color_of_string v }
6245 | "scrollbar-in-presentation" ->
6246 { c with scrollbarinpm = bool_of_string v }
6247 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6248 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6249 | "mupdf-store-size" ->
6250 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6251 | "checkers" -> { c with checkers = bool_of_string v }
6252 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6253 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6254 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6255 | "uri-launcher" -> { c with urilauncher = unent v }
6256 | "path-launcher" -> { c with pathlauncher = unent v }
6257 | "color-space" -> { c with colorspace = colorspace_of_string v }
6258 | "invert-colors" -> { c with invert = bool_of_string v }
6259 | "brightness" -> { c with colorscale = float_of_string v }
6260 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6261 | "ghyllscroll" ->
6262 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6263 | "columns" ->
6264 let (n, _, _) as nab = multicolumns_of_string v in
6265 if n < 0
6266 then { c with columns = Csplit (-n, [||]) }
6267 else { c with columns = Cmulti (nab, [||]) }
6268 | "birds-eye-columns" ->
6269 { c with beyecolumns = Some (max (int_of_string v) 2) }
6270 | "selection-command" -> { c with selcmd = unent v }
6271 | "synctex-command" -> { c with stcmd = unent v }
6272 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6273 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6274 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6275 | "use-pbo" -> { c with usepbo = bool_of_string v }
6276 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6277 | _ -> c
6278 with exn ->
6279 prerr_endline ("Error processing attribute (`" ^
6280 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6283 let rec fold c = function
6284 | [] -> c
6285 | (k, v) :: rest ->
6286 let c = apply c k v in
6287 fold c rest
6289 fold { c with keyhashes = copykeyhashes c } attrs;
6292 let fromstring f pos n v d =
6293 try f v
6294 with exn ->
6295 dolog "Error processing attribute (%S=%S) at %d\n%s"
6296 n v pos (exntos exn)
6301 let bookmark_of attrs =
6302 let rec fold title page rely visy = function
6303 | ("title", v) :: rest -> fold v page rely visy rest
6304 | ("page", v) :: rest -> fold title v rely visy rest
6305 | ("rely", v) :: rest -> fold title page v visy rest
6306 | ("visy", v) :: rest -> fold title page rely v rest
6307 | _ :: rest -> fold title page rely visy rest
6308 | [] -> title, page, rely, visy
6310 fold "invalid" "0" "0" "0" attrs
6313 let doc_of attrs =
6314 let rec fold path page rely pan visy = function
6315 | ("path", v) :: rest -> fold v page rely pan visy rest
6316 | ("page", v) :: rest -> fold path v rely pan visy rest
6317 | ("rely", v) :: rest -> fold path page v pan visy rest
6318 | ("pan", v) :: rest -> fold path page rely v visy rest
6319 | ("visy", v) :: rest -> fold path page rely pan v rest
6320 | _ :: rest -> fold path page rely pan visy rest
6321 | [] -> path, page, rely, pan, visy
6323 fold "" "0" "0" "0" "0" attrs
6326 let map_of attrs =
6327 let rec fold rs ls = function
6328 | ("out", v) :: rest -> fold v ls rest
6329 | ("in", v) :: rest -> fold rs v rest
6330 | _ :: rest -> fold ls rs rest
6331 | [] -> ls, rs
6333 fold "" "" attrs
6336 let setconf dst src =
6337 dst.scrollbw <- src.scrollbw;
6338 dst.scrollh <- src.scrollh;
6339 dst.icase <- src.icase;
6340 dst.preload <- src.preload;
6341 dst.pagebias <- src.pagebias;
6342 dst.verbose <- src.verbose;
6343 dst.scrollstep <- src.scrollstep;
6344 dst.maxhfit <- src.maxhfit;
6345 dst.crophack <- src.crophack;
6346 dst.autoscrollstep <- src.autoscrollstep;
6347 dst.maxwait <- src.maxwait;
6348 dst.hlinks <- src.hlinks;
6349 dst.underinfo <- src.underinfo;
6350 dst.interpagespace <- src.interpagespace;
6351 dst.zoom <- src.zoom;
6352 dst.presentation <- src.presentation;
6353 dst.angle <- src.angle;
6354 dst.cwinw <- src.cwinw;
6355 dst.cwinh <- src.cwinh;
6356 dst.savebmarks <- src.savebmarks;
6357 dst.memlimit <- src.memlimit;
6358 dst.proportional <- src.proportional;
6359 dst.texcount <- src.texcount;
6360 dst.sliceheight <- src.sliceheight;
6361 dst.thumbw <- src.thumbw;
6362 dst.jumpback <- src.jumpback;
6363 dst.bgcolor <- src.bgcolor;
6364 dst.scrollbarinpm <- src.scrollbarinpm;
6365 dst.tilew <- src.tilew;
6366 dst.tileh <- src.tileh;
6367 dst.mustoresize <- src.mustoresize;
6368 dst.checkers <- src.checkers;
6369 dst.aalevel <- src.aalevel;
6370 dst.trimmargins <- src.trimmargins;
6371 dst.trimfuzz <- src.trimfuzz;
6372 dst.urilauncher <- src.urilauncher;
6373 dst.colorspace <- src.colorspace;
6374 dst.invert <- src.invert;
6375 dst.colorscale <- src.colorscale;
6376 dst.redirectstderr <- src.redirectstderr;
6377 dst.ghyllscroll <- src.ghyllscroll;
6378 dst.columns <- src.columns;
6379 dst.beyecolumns <- src.beyecolumns;
6380 dst.selcmd <- src.selcmd;
6381 dst.updatecurs <- src.updatecurs;
6382 dst.pathlauncher <- src.pathlauncher;
6383 dst.keyhashes <- copykeyhashes src;
6384 dst.hfsize <- src.hfsize;
6385 dst.hscrollstep <- src.hscrollstep;
6386 dst.pgscale <- src.pgscale;
6387 dst.usepbo <- src.usepbo;
6388 dst.wheelbypage <- src.wheelbypage;
6389 dst.stcmd <- src.stcmd;
6392 let get s =
6393 let h = Hashtbl.create 10 in
6394 let dc = { defconf with angle = defconf.angle } in
6395 let rec toplevel v t spos _ =
6396 match t with
6397 | Vdata | Vcdata | Vend -> v
6398 | Vopen ("llppconfig", _, closed) ->
6399 if closed
6400 then v
6401 else { v with f = llppconfig }
6402 | Vopen _ ->
6403 error "unexpected subelement at top level" s spos
6404 | Vclose _ -> error "unexpected close at top level" s spos
6406 and llppconfig v t spos _ =
6407 match t with
6408 | Vdata | Vcdata -> v
6409 | Vend -> error "unexpected end of input in llppconfig" s spos
6410 | Vopen ("defaults", attrs, closed) ->
6411 let c = config_of dc attrs in
6412 setconf dc c;
6413 if closed
6414 then v
6415 else { v with f = defaults }
6417 | Vopen ("ui-font", attrs, closed) ->
6418 let rec getsize size = function
6419 | [] -> size
6420 | ("size", v) :: rest ->
6421 let size =
6422 fromstring int_of_string spos "size" v fstate.fontsize in
6423 getsize size rest
6424 | l -> getsize size l
6426 fstate.fontsize <- getsize fstate.fontsize attrs;
6427 if closed
6428 then v
6429 else { v with f = uifont (Buffer.create 10) }
6431 | Vopen ("doc", attrs, closed) ->
6432 let pathent, spage, srely, span, svisy = doc_of attrs in
6433 let path = unent pathent
6434 and pageno = fromstring int_of_string spos "page" spage 0
6435 and rely = fromstring float_of_string spos "rely" srely 0.0
6436 and pan = fromstring int_of_string spos "pan" span 0
6437 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6438 let c = config_of dc attrs in
6439 let anchor = (pageno, rely, visy) in
6440 if closed
6441 then (Hashtbl.add h path (c, [], pan, anchor); v)
6442 else { v with f = doc path pan anchor c [] }
6444 | Vopen _ ->
6445 error "unexpected subelement in llppconfig" s spos
6447 | Vclose "llppconfig" -> { v with f = toplevel }
6448 | Vclose _ -> error "unexpected close in llppconfig" s spos
6450 and defaults v t spos _ =
6451 match t with
6452 | Vdata | Vcdata -> v
6453 | Vend -> error "unexpected end of input in defaults" s spos
6454 | Vopen ("keymap", attrs, closed) ->
6455 let modename =
6456 try List.assoc "mode" attrs
6457 with Not_found -> "global" in
6458 if closed
6459 then v
6460 else
6461 let ret keymap =
6462 let h = findkeyhash dc modename in
6463 KeyMap.iter (Hashtbl.replace h) keymap;
6464 defaults
6466 { v with f = pkeymap ret KeyMap.empty }
6468 | Vopen (_, _, _) ->
6469 error "unexpected subelement in defaults" s spos
6471 | Vclose "defaults" ->
6472 { v with f = llppconfig }
6474 | Vclose _ -> error "unexpected close in defaults" s spos
6476 and uifont b v t spos epos =
6477 match t with
6478 | Vdata | Vcdata ->
6479 Buffer.add_substring b s spos (epos - spos);
6481 | Vopen (_, _, _) ->
6482 error "unexpected subelement in ui-font" s spos
6483 | Vclose "ui-font" ->
6484 if String.length !fontpath = 0
6485 then fontpath := Buffer.contents b;
6486 { v with f = llppconfig }
6487 | Vclose _ -> error "unexpected close in ui-font" s spos
6488 | Vend -> error "unexpected end of input in ui-font" s spos
6490 and doc path pan anchor c bookmarks v t spos _ =
6491 match t with
6492 | Vdata | Vcdata -> v
6493 | Vend -> error "unexpected end of input in doc" s spos
6494 | Vopen ("bookmarks", _, closed) ->
6495 if closed
6496 then v
6497 else { v with f = pbookmarks path pan anchor c bookmarks }
6499 | Vopen ("keymap", attrs, closed) ->
6500 let modename =
6501 try List.assoc "mode" attrs
6502 with Not_found -> "global"
6504 if closed
6505 then v
6506 else
6507 let ret keymap =
6508 let h = findkeyhash c modename in
6509 KeyMap.iter (Hashtbl.replace h) keymap;
6510 doc path pan anchor c bookmarks
6512 { v with f = pkeymap ret KeyMap.empty }
6514 | Vopen (_, _, _) ->
6515 error "unexpected subelement in doc" s spos
6517 | Vclose "doc" ->
6518 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6519 { v with f = llppconfig }
6521 | Vclose _ -> error "unexpected close in doc" s spos
6523 and pkeymap ret keymap v t spos _ =
6524 match t with
6525 | Vdata | Vcdata -> v
6526 | Vend -> error "unexpected end of input in keymap" s spos
6527 | Vopen ("map", attrs, closed) ->
6528 let r, l = map_of attrs in
6529 let kss = fromstring keys_of_string spos "in" r [] in
6530 let lss = fromstring keys_of_string spos "out" l [] in
6531 let keymap =
6532 match kss with
6533 | [] -> keymap
6534 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6535 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6537 if closed
6538 then { v with f = pkeymap ret keymap }
6539 else
6540 let f () = v in
6541 { v with f = skip "map" f }
6543 | Vopen _ ->
6544 error "unexpected subelement in keymap" s spos
6546 | Vclose "keymap" ->
6547 { v with f = ret keymap }
6549 | Vclose _ -> error "unexpected close in keymap" s spos
6551 and pbookmarks path pan anchor c bookmarks v t spos _ =
6552 match t with
6553 | Vdata | Vcdata -> v
6554 | Vend -> error "unexpected end of input in bookmarks" s spos
6555 | Vopen ("item", attrs, closed) ->
6556 let titleent, spage, srely, svisy = bookmark_of attrs in
6557 let page = fromstring int_of_string spos "page" spage 0
6558 and rely = fromstring float_of_string spos "rely" srely 0.0
6559 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6560 let bookmarks =
6561 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6563 if closed
6564 then { v with f = pbookmarks path pan anchor c bookmarks }
6565 else
6566 let f () = v in
6567 { v with f = skip "item" f }
6569 | Vopen _ ->
6570 error "unexpected subelement in bookmarks" s spos
6572 | Vclose "bookmarks" ->
6573 { v with f = doc path pan anchor c bookmarks }
6575 | Vclose _ -> error "unexpected close in bookmarks" s spos
6577 and skip tag f v t spos _ =
6578 match t with
6579 | Vdata | Vcdata -> v
6580 | Vend ->
6581 error ("unexpected end of input in skipped " ^ tag) s spos
6582 | Vopen (tag', _, closed) ->
6583 if closed
6584 then v
6585 else
6586 let f' () = { v with f = skip tag f } in
6587 { v with f = skip tag' f' }
6588 | Vclose ctag ->
6589 if tag = ctag
6590 then f ()
6591 else error ("unexpected close in skipped " ^ tag) s spos
6594 parse { f = toplevel; accu = () } s;
6595 h, dc;
6598 let do_load f ic =
6600 let len = in_channel_length ic in
6601 let s = String.create len in
6602 really_input ic s 0 len;
6603 f s;
6604 with
6605 | Parse_error (msg, s, pos) ->
6606 let subs = subs s pos in
6607 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6608 failwith ("parse error: " ^ s)
6610 | exn ->
6611 failwith ("config load error: " ^ exntos exn)
6614 let defconfpath =
6615 let dir =
6617 let dir = Filename.concat home ".config" in
6618 if Sys.is_directory dir then dir else home
6619 with _ -> home
6621 Filename.concat dir "llpp.conf"
6624 let confpath = ref defconfpath;;
6626 let load1 f =
6627 if Sys.file_exists !confpath
6628 then
6629 match
6630 (try Some (open_in_bin !confpath)
6631 with exn ->
6632 prerr_endline
6633 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6634 exntos exn);
6635 None
6637 with
6638 | Some ic ->
6639 let success =
6641 f (do_load get ic)
6642 with exn ->
6643 prerr_endline
6644 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6645 exntos exn);
6646 false
6648 close_in ic;
6649 success
6651 | None -> false
6652 else
6653 f (Hashtbl.create 0, defconf)
6656 let load () =
6657 let f (h, dc) =
6658 let pc, pb, px, pa =
6660 Hashtbl.find h (Filename.basename state.path)
6661 with Not_found -> dc, [], 0, emptyanchor
6663 setconf defconf dc;
6664 setconf conf pc;
6665 state.bookmarks <- pb;
6666 state.x <- px;
6667 state.scrollw <- conf.scrollbw;
6668 if conf.jumpback
6669 then state.anchor <- pa;
6670 cbput state.hists.nav pa;
6671 true
6673 load1 f
6676 let add_attrs bb always dc c =
6677 let ob s a b =
6678 if always || a != b
6679 then Printf.bprintf bb "\n %s='%b'" s a
6680 and oi s a b =
6681 if always || a != b
6682 then Printf.bprintf bb "\n %s='%d'" s a
6683 and oI s a b =
6684 if always || a != b
6685 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6686 and oz s a b =
6687 if always || a <> b
6688 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6689 and oF s a b =
6690 if always || a <> b
6691 then Printf.bprintf bb "\n %s='%f'" s a
6692 and oc s a b =
6693 if always || a <> b
6694 then
6695 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6696 and oC s a b =
6697 if always || a <> b
6698 then
6699 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6700 and oR s a b =
6701 if always || a <> b
6702 then
6703 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6704 and os s a b =
6705 if always || a <> b
6706 then
6707 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6708 and og s a b =
6709 if always || a <> b
6710 then
6711 match a with
6712 | None -> ()
6713 | Some (_N, _A, _B) ->
6714 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6715 and oW s a b =
6716 if always || a <> b
6717 then
6718 let v =
6719 match a with
6720 | None -> "false"
6721 | Some f ->
6722 if f = infinity
6723 then "true"
6724 else string_of_float f
6726 Printf.bprintf bb "\n %s='%s'" s v
6727 and oco s a b =
6728 if always || a <> b
6729 then
6730 match a with
6731 | Cmulti ((n, a, b), _) when n > 1 ->
6732 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6733 | Csplit (n, _) when n > 1 ->
6734 Printf.bprintf bb "\n %s='%d'" s ~-n
6735 | _ -> ()
6736 and obeco s a b =
6737 if always || a <> b
6738 then
6739 match a with
6740 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6741 | _ -> ()
6743 oi "width" c.cwinw dc.cwinw;
6744 oi "height" c.cwinh dc.cwinh;
6745 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6746 oi "scroll-handle-height" c.scrollh dc.scrollh;
6747 ob "case-insensitive-search" c.icase dc.icase;
6748 ob "preload" c.preload dc.preload;
6749 oi "page-bias" c.pagebias dc.pagebias;
6750 oi "scroll-step" c.scrollstep dc.scrollstep;
6751 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6752 ob "max-height-fit" c.maxhfit dc.maxhfit;
6753 ob "crop-hack" c.crophack dc.crophack;
6754 oW "throttle" c.maxwait dc.maxwait;
6755 ob "highlight-links" c.hlinks dc.hlinks;
6756 ob "under-cursor-info" c.underinfo dc.underinfo;
6757 oi "vertical-margin" c.interpagespace dc.interpagespace;
6758 oz "zoom" c.zoom dc.zoom;
6759 ob "presentation" c.presentation dc.presentation;
6760 oi "rotation-angle" c.angle dc.angle;
6761 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6762 ob "proportional-display" c.proportional dc.proportional;
6763 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6764 oi "tex-count" c.texcount dc.texcount;
6765 oi "slice-height" c.sliceheight dc.sliceheight;
6766 oi "thumbnail-width" c.thumbw dc.thumbw;
6767 ob "persistent-location" c.jumpback dc.jumpback;
6768 oc "background-color" c.bgcolor dc.bgcolor;
6769 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6770 oi "tile-width" c.tilew dc.tilew;
6771 oi "tile-height" c.tileh dc.tileh;
6772 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6773 ob "checkers" c.checkers dc.checkers;
6774 oi "aalevel" c.aalevel dc.aalevel;
6775 ob "trim-margins" c.trimmargins dc.trimmargins;
6776 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6777 os "uri-launcher" c.urilauncher dc.urilauncher;
6778 os "path-launcher" c.pathlauncher dc.pathlauncher;
6779 oC "color-space" c.colorspace dc.colorspace;
6780 ob "invert-colors" c.invert dc.invert;
6781 oF "brightness" c.colorscale dc.colorscale;
6782 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6783 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6784 oco "columns" c.columns dc.columns;
6785 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6786 os "selection-command" c.selcmd dc.selcmd;
6787 os "synctex-command" c.stcmd dc.stcmd;
6788 ob "update-cursor" c.updatecurs dc.updatecurs;
6789 oi "hint-font-size" c.hfsize dc.hfsize;
6790 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6791 oF "page-scroll-scale" c.pgscale dc.pgscale;
6792 ob "use-pbo" c.usepbo dc.usepbo;
6793 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6796 let keymapsbuf always dc c =
6797 let bb = Buffer.create 16 in
6798 let rec loop = function
6799 | [] -> ()
6800 | (modename, h) :: rest ->
6801 let dh = findkeyhash dc modename in
6802 if always || h <> dh
6803 then (
6804 if Hashtbl.length h > 0
6805 then (
6806 if Buffer.length bb > 0
6807 then Buffer.add_char bb '\n';
6808 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6809 Hashtbl.iter (fun i o ->
6810 let isdifferent = always ||
6812 let dO = Hashtbl.find dh i in
6813 dO <> o
6814 with Not_found -> true
6816 if isdifferent
6817 then
6818 let addkm (k, m) =
6819 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6820 if Wsi.withalt m then Buffer.add_string bb "alt-";
6821 if Wsi.withshift m then Buffer.add_string bb "shift-";
6822 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6823 Buffer.add_string bb (Wsi.keyname k);
6825 let addkms l =
6826 let rec loop = function
6827 | [] -> ()
6828 | km :: [] -> addkm km
6829 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6831 loop l
6833 Buffer.add_string bb "<map in='";
6834 addkm i;
6835 match o with
6836 | KMinsrt km ->
6837 Buffer.add_string bb "' out='";
6838 addkm km;
6839 Buffer.add_string bb "'/>\n"
6841 | KMinsrl kms ->
6842 Buffer.add_string bb "' out='";
6843 addkms kms;
6844 Buffer.add_string bb "'/>\n"
6846 | KMmulti (ins, kms) ->
6847 Buffer.add_char bb ' ';
6848 addkms ins;
6849 Buffer.add_string bb "' out='";
6850 addkms kms;
6851 Buffer.add_string bb "'/>\n"
6852 ) h;
6853 Buffer.add_string bb "</keymap>";
6856 loop rest
6858 loop c.keyhashes;
6862 let save () =
6863 let uifontsize = fstate.fontsize in
6864 let bb = Buffer.create 32768 in
6865 let w, h =
6866 List.fold_left
6867 (fun (w, h) ws ->
6868 match ws with
6869 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6870 | Wsi.MaxVert -> (w, conf.cwinh)
6871 | Wsi.MaxHorz -> (conf.cwinw, h)
6873 (state.winw, state.winh) state.winstate
6875 conf.cwinw <- w;
6876 conf.cwinh <- h;
6877 let f (h, dc) =
6878 let dc = if conf.bedefault then conf else dc in
6879 Buffer.add_string bb "<llppconfig>\n";
6881 if String.length !fontpath > 0
6882 then
6883 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6884 uifontsize
6885 !fontpath
6886 else (
6887 if uifontsize <> 14
6888 then
6889 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6892 Buffer.add_string bb "<defaults ";
6893 add_attrs bb true dc dc;
6894 let kb = keymapsbuf true dc dc in
6895 if Buffer.length kb > 0
6896 then (
6897 Buffer.add_string bb ">\n";
6898 Buffer.add_buffer bb kb;
6899 Buffer.add_string bb "\n</defaults>\n";
6901 else Buffer.add_string bb "/>\n";
6903 let adddoc path pan anchor c bookmarks =
6904 if bookmarks == [] && c = dc && anchor = emptyanchor
6905 then ()
6906 else (
6907 Printf.bprintf bb "<doc path='%s'"
6908 (enent path 0 (String.length path));
6910 if anchor <> emptyanchor
6911 then (
6912 let n, rely, visy = anchor in
6913 Printf.bprintf bb " page='%d'" n;
6914 if rely > 1e-6
6915 then
6916 Printf.bprintf bb " rely='%f'" rely
6918 if abs_float visy > 1e-6
6919 then
6920 Printf.bprintf bb " visy='%f'" visy
6924 if pan != 0
6925 then Printf.bprintf bb " pan='%d'" pan;
6927 add_attrs bb false dc c;
6928 let kb = keymapsbuf false dc c in
6930 begin match bookmarks with
6931 | [] ->
6932 if Buffer.length kb > 0
6933 then (
6934 Buffer.add_string bb ">\n";
6935 Buffer.add_buffer bb kb;
6936 Buffer.add_string bb "\n</doc>\n";
6938 else Buffer.add_string bb "/>\n"
6939 | _ ->
6940 Buffer.add_string bb ">\n<bookmarks>\n";
6941 List.iter (fun (title, _level, (page, rely, visy)) ->
6942 Printf.bprintf bb
6943 "<item title='%s' page='%d'"
6944 (enent title 0 (String.length title))
6945 page
6947 if rely > 1e-6
6948 then
6949 Printf.bprintf bb " rely='%f'" rely
6951 if abs_float visy > 1e-6
6952 then
6953 Printf.bprintf bb " visy='%f'" visy
6955 Buffer.add_string bb "/>\n";
6956 ) bookmarks;
6957 Buffer.add_string bb "</bookmarks>";
6958 if Buffer.length kb > 0
6959 then (
6960 Buffer.add_string bb "\n";
6961 Buffer.add_buffer bb kb;
6963 Buffer.add_string bb "\n</doc>\n";
6964 end;
6968 let pan, conf =
6969 match state.mode with
6970 | Birdseye (c, pan, _, _, _) ->
6971 let beyecolumns =
6972 match conf.columns with
6973 | Cmulti ((c, _, _), _) -> Some c
6974 | Csingle _ -> None
6975 | Csplit _ -> None
6976 and columns =
6977 match c.columns with
6978 | Cmulti (c, _) -> Cmulti (c, [||])
6979 | Csingle _ -> Csingle [||]
6980 | Csplit _ -> failwith "quit from bird's eye while split"
6982 pan, { c with beyecolumns = beyecolumns; columns = columns }
6983 | _ -> state.x, conf
6985 let basename = Filename.basename state.path in
6986 adddoc basename pan (getanchor ())
6987 (let conf =
6988 let autoscrollstep =
6989 match state.autoscroll with
6990 | Some step -> step
6991 | None -> conf.autoscrollstep
6993 match state.mode with
6994 | Birdseye (bc, _, _, _, _) ->
6995 { conf with
6996 zoom = bc.zoom;
6997 presentation = bc.presentation;
6998 interpagespace = bc.interpagespace;
6999 maxwait = bc.maxwait;
7000 autoscrollstep = autoscrollstep }
7001 | _ -> { conf with autoscrollstep = autoscrollstep }
7002 in conf)
7003 (if conf.savebmarks then state.bookmarks else []);
7005 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7006 if basename <> path
7007 then adddoc path x anchor c bookmarks
7008 ) h;
7009 Buffer.add_string bb "</llppconfig>\n";
7010 true;
7012 if load1 f && Buffer.length bb > 0
7013 then
7015 let tmp = !confpath ^ ".tmp" in
7016 let oc = open_out_bin tmp in
7017 Buffer.output_buffer oc bb;
7018 close_out oc;
7019 Unix.rename tmp !confpath;
7020 with exn ->
7021 prerr_endline
7022 ("error while saving configuration: " ^ exntos exn)
7024 end;;
7026 let adderrmsg src msg =
7027 Buffer.add_string state.errmsgs msg;
7028 state.newerrmsgs <- true;
7029 G.postRedisplay src
7032 let adderrfmt src fmt =
7033 Format.kprintf (fun s -> adderrmsg src s) fmt;
7036 let ract cmds =
7037 let cl = splitatspace cmds in
7038 let scan s fmt f =
7039 try Scanf.sscanf s fmt f
7040 with exn ->
7041 adderrfmt "remote exec"
7042 "error processing '%S': %s\n" cmds (exntos exn)
7044 match cl with
7045 | "reload" :: [] -> reload ()
7046 | "goto" :: args :: [] ->
7047 scan args "%u %f %f"
7048 (fun pageno x y ->
7049 let cmd, _ = state.geomcmds in
7050 if String.length cmd = 0
7051 then gotopagexy pageno x y
7052 else
7053 let f prevf () =
7054 gotopagexy pageno x y;
7055 prevf ()
7057 state.reprf <- f state.reprf
7059 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7060 | "rect" :: args :: [] ->
7061 scan args "%u %u %f %f %f %f"
7062 (fun pageno color x0 y0 x1 y1 ->
7063 onpagerect pageno (fun w h ->
7064 let _,w1,h1,_ = getpagedim pageno in
7065 let sw = float w1 /. w
7066 and sh = float h1 /. h in
7067 let x0s = x0 *. sw
7068 and x1s = x1 *. sw
7069 and y0s = y0 *. sh
7070 and y1s = y1 *. sh in
7071 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7072 debugrect rect;
7073 state.rects <- (pageno, color, rect) :: state.rects;
7074 G.postRedisplay "rect";
7077 | "activatewin" :: [] -> Wsi.activatewin ()
7078 | "quit" :: [] -> raise Quit
7079 | _ ->
7080 adderrfmt "remote command"
7081 "error processing remote command: %S\n" cmds;
7084 let remote =
7085 let scratch = String.create 80 in
7086 let buf = Buffer.create 80 in
7087 fun fd ->
7088 let rec tempfr () =
7089 try Some (Unix.read fd scratch 0 80)
7090 with
7091 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7092 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7093 | exn -> raise exn
7095 match tempfr () with
7096 | None -> Some fd
7097 | Some n ->
7098 if n = 0
7099 then (
7100 Unix.close fd;
7101 if Buffer.length buf > 0
7102 then (
7103 let s = Buffer.contents buf in
7104 Buffer.clear buf;
7105 ract s;
7107 None
7109 else
7110 let rec eat ppos =
7111 let nlpos =
7113 let pos = String.index_from scratch ppos '\n' in
7114 if pos >= n then -1 else pos
7115 with Not_found -> -1
7117 if nlpos >= 0
7118 then (
7119 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7120 let s = Buffer.contents buf in
7121 Buffer.clear buf;
7122 ract s;
7123 eat (nlpos+1);
7125 else (
7126 Buffer.add_substring buf scratch ppos (n-ppos);
7127 Some fd
7129 in eat 0
7132 let remoteopen path =
7133 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7134 with exn ->
7135 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7136 None
7139 let () =
7140 let trimcachepath = ref "" in
7141 let rcmdpath = ref "" in
7142 Arg.parse
7143 (Arg.align
7144 [("-p", Arg.String (fun s -> state.password <- s),
7145 "<password> Set password");
7147 ("-f", Arg.String (fun s -> Config.fontpath := s),
7148 "<path> Set path to the user interface font");
7150 ("-c", Arg.String (fun s -> Config.confpath := s),
7151 "<path> Set path to the configuration file");
7153 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7154 "<path> Set path to the trim cache file");
7156 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7157 "<named-destination> Set named destination");
7159 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7161 ("-remote", Arg.String (fun s -> rcmdpath := s),
7162 "<path> Set path to the remote commands source");
7164 ("-v", Arg.Unit (fun () ->
7165 Printf.printf
7166 "%s\nconfiguration path: %s\n"
7167 (version ())
7168 Config.defconfpath
7170 exit 0), " Print version and exit");
7173 (fun s -> state.path <- s)
7174 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7176 if String.length state.path = 0
7177 then (prerr_endline "file name missing"; exit 1);
7179 if not (Config.load ())
7180 then prerr_endline "failed to load configuration";
7182 let globalkeyhash = findkeyhash conf "global" in
7183 let wsfd, winw, winh = Wsi.init (object
7184 method expose =
7185 if nogeomcmds state.geomcmds || platform == Posx
7186 then display ()
7187 else (
7188 GlClear.color (scalecolor2 conf.bgcolor);
7189 GlClear.clear [`color];
7191 method display = display ()
7192 method reshape w h = reshape w h
7193 method mouse b d x y m = mouse b d x y m
7194 method motion x y = state.mpos <- (x, y); motion x y
7195 method pmotion x y = state.mpos <- (x, y); pmotion x y
7196 method key k m =
7197 let mascm = m land (
7198 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7199 ) in
7200 match state.keystate with
7201 | KSnone ->
7202 let km = k, mascm in
7203 begin
7204 match
7205 let modehash = state.uioh#modehash in
7206 try Hashtbl.find modehash km
7207 with Not_found ->
7208 try Hashtbl.find globalkeyhash km
7209 with Not_found -> KMinsrt (k, m)
7210 with
7211 | KMinsrt (k, m) -> keyboard k m
7212 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7213 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7215 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7216 List.iter (fun (k, m) -> keyboard k m) insrt;
7217 state.keystate <- KSnone
7218 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7219 state.keystate <- KSinto (keys, insrt)
7220 | _ ->
7221 state.keystate <- KSnone
7223 method enter x y = state.mpos <- (x, y); pmotion x y
7224 method leave = state.mpos <- (-1, -1)
7225 method winstate wsl = state.winstate <- wsl
7226 method quit = raise Quit
7227 end) conf.cwinw conf.cwinh (platform = Posx) in
7229 state.wsfd <- wsfd;
7231 if not (
7232 List.exists GlMisc.check_extension
7233 [ "GL_ARB_texture_rectangle"
7234 ; "GL_EXT_texture_recangle"
7235 ; "GL_NV_texture_rectangle" ]
7237 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7239 let cr, sw =
7240 match Ne.pipe () with
7241 | Ne.Exn exn ->
7242 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7243 exit 1
7244 | Ne.Res rw -> rw
7245 and sr, cw =
7246 match Ne.pipe () with
7247 | Ne.Exn exn ->
7248 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7249 exit 1
7250 | Ne.Res rw -> rw
7253 cloexec cr;
7254 cloexec sw;
7255 cloexec sr;
7256 cloexec cw;
7258 setcheckers conf.checkers;
7259 redirectstderr ();
7261 init (cr, cw) (
7262 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7263 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7264 !Config.fontpath, !trimcachepath,
7265 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7267 state.sr <- sr;
7268 state.sw <- sw;
7269 state.text <- "Opening " ^ (mbtoutf8 state.path);
7270 reshape winw winh;
7271 opendoc state.path state.password;
7272 state.uioh <- uioh;
7274 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7275 let optrfd =
7276 ref (
7277 if String.length !rcmdpath > 0
7278 then remoteopen !rcmdpath
7279 else None
7283 let rec loop deadline =
7284 let r =
7285 match state.errfd with
7286 | None -> [state.sr; state.wsfd]
7287 | Some fd -> [state.sr; state.wsfd; fd]
7289 let r =
7290 match !optrfd with
7291 | None -> r
7292 | Some fd -> fd :: r
7294 if state.redisplay
7295 then (
7296 state.redisplay <- false;
7297 display ();
7299 let timeout =
7300 let now = now () in
7301 if deadline > now
7302 then (
7303 if deadline = infinity
7304 then ~-.1.0
7305 else max 0.0 (deadline -. now)
7307 else 0.0
7309 let r, _, _ =
7310 try Unix.select r [] [] timeout
7311 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7313 begin match r with
7314 | [] ->
7315 state.ghyll None;
7316 let newdeadline =
7317 if state.ghyll == noghyll
7318 then
7319 match state.autoscroll with
7320 | Some step when step != 0 ->
7321 let y = state.y + step in
7322 let y =
7323 if y < 0
7324 then state.maxy
7325 else if y >= state.maxy then 0 else y
7327 gotoy y;
7328 if state.mode = View
7329 then state.text <- "";
7330 deadline +. 0.01
7331 | _ -> infinity
7332 else deadline +. 0.01
7334 loop newdeadline
7336 | l ->
7337 let rec checkfds = function
7338 | [] -> ()
7339 | fd :: rest when fd = state.sr ->
7340 let cmd = readcmd state.sr in
7341 act cmd;
7342 checkfds rest
7344 | fd :: rest when fd = state.wsfd ->
7345 Wsi.readresp fd;
7346 checkfds rest
7348 | fd :: rest when Some fd = !optrfd ->
7349 begin match remote fd with
7350 | None -> optrfd := remoteopen !rcmdpath;
7351 | opt -> optrfd := opt
7352 end;
7353 checkfds rest
7355 | fd :: rest ->
7356 let s = String.create 80 in
7357 let n = tempfailureretry (Unix.read fd s 0) 80 in
7358 if conf.redirectstderr
7359 then (
7360 Buffer.add_substring state.errmsgs s 0 n;
7361 state.newerrmsgs <- true;
7362 state.redisplay <- true;
7364 else (
7365 prerr_string (String.sub s 0 n);
7366 flush stderr;
7368 checkfds rest
7370 checkfds l;
7371 let newdeadline =
7372 let deadline1 =
7373 if deadline = infinity
7374 then now () +. 0.01
7375 else deadline
7377 match state.autoscroll with
7378 | Some step when step != 0 -> deadline1
7379 | _ -> if state.ghyll == noghyll then infinity else deadline1
7381 loop newdeadline
7382 end;
7385 loop infinity;
7386 with Quit ->
7387 Config.save ();