Cosmetics
[llpp.git] / main.ml
blob36ac8f2540ed856dea43d18b3e2f9f2730dda3e4
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 link =
47 | Lnotfound
48 | Lfound of int
49 and linkdir =
50 | LDfirst
51 | LDlast
52 | LDfirstvisible of (int * int * int)
53 | LDleft of int
54 | LDright of int
55 | LDdown of int
56 | LDup of int
59 type pagewithlinks =
60 | Pwlnotfound
61 | Pwl of int
64 type keymap =
65 | KMinsrt of key
66 | KMinsrl of key list
67 | KMmulti of key list * key list
68 and key = int * int
69 and keyhash = (key, keymap) Hashtbl.t
70 and keystate =
71 | KSnone
72 | KSinto of (key list * key list)
75 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
76 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
78 type pipe = (Unix.file_descr * Unix.file_descr);;
80 external init : pipe -> params -> unit = "ml_init";;
81 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
82 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
83 external getpdimrect : int -> float array = "ml_getpdimrect";;
84 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
85 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
86 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
87 external measurestr : int -> string -> float = "ml_measure_string";;
88 external getmaxw : unit -> float = "ml_getmaxw";;
89 external postprocess :
90 opaque -> int -> int -> int -> (int * string * int) -> int
91 = "ml_postprocess";;
92 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
93 external platform : unit -> platform = "ml_platform";;
94 external setaalevel : int -> unit = "ml_setaalevel";;
95 external realloctexts : int -> bool = "ml_realloctexts";;
96 external findlink : opaque -> linkdir -> link = "ml_findlink";;
97 external getlink : opaque -> int -> under = "ml_getlink";;
98 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
99 external getlinkcount : opaque -> int = "ml_getlinkcount";;
100 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
101 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
102 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
103 external freepbo : string -> unit = "ml_freepbo";;
104 external unmappbo : string -> unit = "ml_unmappbo";;
105 external pbousable : unit -> bool = "ml_pbo_usable";;
106 external unproject : opaque -> int -> int -> (int * int) option
107 = "ml_unproject";;
109 let platform_to_string = function
110 | Punknown -> "unknown"
111 | Plinux -> "Linux"
112 | Posx -> "OSX"
113 | Psun -> "Sun"
114 | Pfreebsd -> "FreeBSD"
115 | Pdragonflybsd -> "DragonflyBSD"
116 | Popenbsd -> "OpenBSD"
117 | Pnetbsd -> "NetBSD"
118 | Pcygwin -> "Cygwin"
121 let platform = platform ();;
123 let now = Unix.gettimeofday;;
125 let popen cmd fda =
126 if platform = Pcygwin
127 then (
128 let sh = "/bin/sh" in
129 let args = [|sh; "-c"; cmd|] in
130 let rec std si so se = function
131 | [] -> si, so, se
132 | (fd, 0) :: rest -> std fd so se rest
133 | (fd, -1) :: rest ->
134 Unix.set_close_on_exec fd;
135 std si so se rest
136 | (_, n) :: _ ->
137 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
139 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
140 ignore (Unix.create_process sh args si so se)
142 else popen cmd fda;
145 type x = int
146 and y = int
147 and tilex = int
148 and tiley = int
149 and tileparams = (x * y * width * height * tilex * tiley)
152 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
154 type mpos = int * int
155 and mstate =
156 | Msel of (mpos * mpos)
157 | Mpan of mpos
158 | Mscrolly | Mscrollx
159 | Mzoom of (int * int)
160 | Mzoomrect of (mpos * mpos)
161 | Mnone
164 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
165 and onkey = string -> int -> te
166 and ondone = string -> unit
167 and histcancel = unit -> unit
168 and onhist = ((histcmd -> string) * histcancel)
169 and histcmd = HCnext | HCprev | HCfirst | HClast
170 and cancelonempty = bool
171 and te =
172 | TEstop
173 | TEdone of string
174 | TEcont of string
175 | TEswitch of textentry
178 type 'a circbuf =
179 { store : 'a array
180 ; mutable rc : int
181 ; mutable wc : int
182 ; mutable len : int
186 let bound v minv maxv =
187 max minv (min maxv v);
190 let cbnew n v =
191 { store = Array.create n v
192 ; rc = 0
193 ; wc = 0
194 ; len = 0
198 let cbcap b = Array.length b.store;;
200 let cbput b v =
201 let cap = cbcap b in
202 b.store.(b.wc) <- v;
203 b.wc <- (b.wc + 1) mod cap;
204 b.rc <- b.wc;
205 b.len <- min (b.len + 1) cap;
208 let cbempty b = b.len = 0;;
210 let cbgetg b circular dir =
211 if cbempty b
212 then b.store.(0)
213 else
214 let rc = b.rc + dir in
215 let rc =
216 if circular
217 then (
218 if rc = -1
219 then b.len-1
220 else (
221 if rc >= b.len
222 then 0
223 else rc
226 else bound rc 0 (b.len-1)
228 b.rc <- rc;
229 b.store.(rc);
232 let cbget b = cbgetg b false;;
233 let cbgetc b = cbgetg b true;;
235 let drawstring size x y s =
236 Gl.enable `blend;
237 Gl.enable `texture_2d;
238 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
239 ignore (drawstr size x y s);
240 Gl.disable `blend;
241 Gl.disable `texture_2d;
244 let drawstring1 size x y s =
245 drawstr size x y s;
248 let drawstring2 size x y fmt =
249 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
252 type page =
253 { pageno : int
254 ; pagedimno : int
255 ; pagew : int
256 ; pageh : int
257 ; pagex : int
258 ; pagey : int
259 ; pagevw : int
260 ; pagevh : int
261 ; pagedispx : int
262 ; pagedispy : int
263 ; pagecol : int
267 let debugl l =
268 dolog "l %d dim=%d {" l.pageno l.pagedimno;
269 dolog " WxH %dx%d" l.pagew l.pageh;
270 dolog " vWxH %dx%d" l.pagevw l.pagevh;
271 dolog " pagex,y %d,%d" l.pagex l.pagey;
272 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
273 dolog " column %d" l.pagecol;
274 dolog "}";
277 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
278 dolog "rect {";
279 dolog " x0,y0=(% f, % f)" x0 y0;
280 dolog " x1,y1=(% f, % f)" x1 y1;
281 dolog " x2,y2=(% f, % f)" x2 y2;
282 dolog " x3,y3=(% f, % f)" x3 y3;
283 dolog "}";
286 type multicolumns = multicol * pagegeom
287 and singlecolumn = pagegeom
288 and splitcolumns = columncount * pagegeom
289 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
290 and multicol = columncount * covercount * covercount
291 and pdimno = int
292 and columncount = int
293 and covercount = int;;
295 type conf =
296 { mutable scrollbw : int
297 ; mutable scrollh : int
298 ; mutable icase : bool
299 ; mutable preload : bool
300 ; mutable pagebias : int
301 ; mutable verbose : bool
302 ; mutable debug : bool
303 ; mutable scrollstep : int
304 ; mutable hscrollstep : int
305 ; mutable maxhfit : bool
306 ; mutable crophack : bool
307 ; mutable autoscrollstep : int
308 ; mutable maxwait : float option
309 ; mutable hlinks : bool
310 ; mutable underinfo : bool
311 ; mutable interpagespace : interpagespace
312 ; mutable zoom : float
313 ; mutable presentation : bool
314 ; mutable angle : angle
315 ; mutable cwinw : int
316 ; mutable cwinh : int
317 ; mutable savebmarks : bool
318 ; mutable proportional : proportional
319 ; mutable trimmargins : trimmargins
320 ; mutable trimfuzz : irect
321 ; mutable memlimit : memsize
322 ; mutable texcount : texcount
323 ; mutable sliceheight : sliceheight
324 ; mutable thumbw : width
325 ; mutable jumpback : bool
326 ; mutable bgcolor : float * float * float
327 ; mutable bedefault : bool
328 ; mutable scrollbarinpm : bool
329 ; mutable tilew : int
330 ; mutable tileh : int
331 ; mutable mustoresize : memsize
332 ; mutable checkers : bool
333 ; mutable aalevel : int
334 ; mutable urilauncher : string
335 ; mutable pathlauncher : string
336 ; mutable colorspace : colorspace
337 ; mutable invert : bool
338 ; mutable colorscale : float
339 ; mutable redirectstderr : bool
340 ; mutable ghyllscroll : (int * int * int) option
341 ; mutable columns : columns
342 ; mutable beyecolumns : columncount option
343 ; mutable selcmd : string
344 ; mutable updatecurs : bool
345 ; mutable keyhashes : (string * keyhash) list
346 ; mutable hfsize : int
347 ; mutable pgscale : float
348 ; mutable usepbo : bool
349 ; mutable wheelbypage : bool
350 ; mutable stcmd : string
352 and columns =
353 | Csingle of singlecolumn
354 | Cmulti of multicolumns
355 | Csplit of splitcolumns
358 type anchor = pageno * top * dtop;;
360 type outline = string * int * anchor;;
362 type rect = float * float * float * float * float * float * float * float;;
364 type tile = opaque * pixmapsize * elapsed
365 and elapsed = float;;
366 type pagemapkey = pageno * gen;;
367 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
368 and row = int
369 and col = int;;
371 let emptyanchor = (0, 0.0, 0.0);;
373 type infochange = | Memused | Docinfo | Pdim;;
375 class type uioh = object
376 method display : unit
377 method key : int -> int -> uioh
378 method button : int -> bool -> int -> int -> int -> uioh
379 method motion : int -> int -> uioh
380 method pmotion : int -> int -> uioh
381 method infochanged : infochange -> unit
382 method scrollpw : (int * float * float)
383 method scrollph : (int * float * float)
384 method modehash : keyhash
385 end;;
387 type mode =
388 | Birdseye of (conf * leftx * pageno * pageno * anchor)
389 | Textentry of (textentry * onleave)
390 | View
391 | LinkNav of linktarget
392 and onleave = leavetextentrystatus -> unit
393 and leavetextentrystatus = | Cancel | Confirm
394 and helpitem = string * int * action
395 and action =
396 | Noaction
397 | Action of (uioh -> uioh)
398 and linktarget =
399 | Ltexact of (pageno * int)
400 | Ltgendir of int
403 let isbirdseye = function Birdseye _ -> true | _ -> false;;
404 let istextentry = function Textentry _ -> true | _ -> false;;
406 type currently =
407 | Idle
408 | Loading of (page * gen)
409 | Tiling of (
410 page * opaque * colorspace * angle * gen * col * row * width * height
412 | Outlining of outline list
415 let emptykeyhash = Hashtbl.create 0;;
416 let nouioh : uioh = object (self)
417 method display = ()
418 method key _ _ = self
419 method button _ _ _ _ _ = self
420 method motion _ _ = self
421 method pmotion _ _ = self
422 method infochanged _ = ()
423 method scrollpw = (0, nan, nan)
424 method scrollph = (0, nan, nan)
425 method modehash = emptykeyhash
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable wthack : bool
482 ; mutable winw : int
483 ; mutable winh : int
484 ; mutable reprf : (unit -> unit)
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; cwinw = 900
515 ; cwinh = 900
516 ; savebmarks = true
517 ; proportional = true
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let geturl s =
602 let colonpos = try String.index s ':' with Not_found -> -1 in
603 let len = String.length s in
604 if colonpos >= 0 && colonpos + 3 < len
605 then (
606 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
607 then
608 let schemestartpos =
609 try String.rindex_from s colonpos ' '
610 with Not_found -> -1
612 let scheme =
613 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
615 match scheme with
616 | "http" | "ftp" | "mailto" ->
617 let epos =
618 try String.index_from s colonpos ' '
619 with Not_found -> len
621 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
622 | _ -> ""
623 else ""
625 else ""
628 let gotouri uri =
629 if String.length conf.urilauncher = 0
630 then print_endline uri
631 else (
632 let url = geturl uri in
633 if String.length url = 0
634 then print_endline uri
635 else
636 let re = Str.regexp "%s" in
637 let command = Str.global_replace re url conf.urilauncher in
638 try popen command []
639 with exn ->
640 Printf.eprintf
641 "failed to execute `%s': %s\n" command (exntos exn);
642 flush stderr;
646 let version () =
647 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
648 (platform_to_string platform) Sys.word_size Sys.ocaml_version
651 let makehelp () =
652 let strings = version () :: "" :: Help.keys in
653 Array.of_list (
654 List.map (fun s ->
655 let url = geturl s in
656 if String.length url > 0
657 then (s, 0, Action (fun u -> gotouri url; u))
658 else (s, 0, Noaction)
659 ) strings);
662 let noghyll _ = ();;
663 let firstgeomcmds = "", [];;
664 let noreprf () = ();;
666 let state =
667 { sr = Unix.stdin
668 ; sw = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; errfd = None
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; scrollw = 0
678 ; hscrollh = 0
679 ; anchor = emptyanchor
680 ; ranchors = []
681 ; layout = []
682 ; maxy = max_int
683 ; tilelru = Queue.create ()
684 ; pagemap = Hashtbl.create 10
685 ; tilemap = Hashtbl.create 10
686 ; pdims = []
687 ; pagecount = 0
688 ; currently = Idle
689 ; mstate = Mnone
690 ; rects = []
691 ; rects1 = []
692 ; text = ""
693 ; mode = View
694 ; winstate = []
695 ; searchpattern = ""
696 ; outlines = [||]
697 ; bookmarks = []
698 ; path = ""
699 ; password = ""
700 ; nameddest = ""
701 ; geomcmds = firstgeomcmds
702 ; hists =
703 { nav = cbnew 10 emptyanchor
704 ; pat = cbnew 10 ""
705 ; pag = cbnew 10 ""
706 ; sel = cbnew 10 ""
708 ; memused = 0
709 ; gen = 0
710 ; throttle = None
711 ; autoscroll = None
712 ; ghyll = noghyll
713 ; help = makehelp ()
714 ; docinfo = []
715 ; texid = None
716 ; prevzoom = 1.0
717 ; progress = -1.0
718 ; uioh = nouioh
719 ; redisplay = true
720 ; mpos = (-1, -1)
721 ; keystate = KSnone
722 ; glinks = false
723 ; prevcolumns = None
724 ; wthack = false
725 ; winw = -1
726 ; winh = -1
727 ; reprf = noreprf
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
754 flush stderr;
758 module Ne = struct
759 type 'a t = | Res of 'a | Exn of exn;;
761 let pipe () =
762 try Res (Unix.pipe ())
763 with exn -> Exn exn
766 let clo fd f =
767 try tempfailureretry Unix.close fd
768 with exn -> f (exntos exn)
771 let dup fd =
772 try Res (tempfailureretry Unix.dup fd)
773 with exn -> Exn exn
776 let dup2 fd1 fd2 =
777 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
778 with exn -> Exn exn
780 end;;
782 let redirectstderr () =
783 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
784 if conf.redirectstderr
785 then
786 match Ne.pipe () with
787 | Ne.Exn exn ->
788 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
790 | Ne.Res (r, w) ->
791 begin match Ne.dup Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup stderr: %s" (exntos exn);
794 Ne.clo r (clofail "pipe/r");
795 Ne.clo w (clofail "pipe/w");
797 | Ne.Res dupstderr ->
798 begin match Ne.dup2 w Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 to stderr: %s" (exntos exn);
801 Ne.clo dupstderr (clofail "stderr duplicate");
802 Ne.clo r (clofail "redir pipe/r");
803 Ne.clo w (clofail "redir pipe/w");
805 | Ne.Res () ->
806 state.stderr <- dupstderr;
807 state.errfd <- Some r;
808 end;
810 else (
811 state.newerrmsgs <- false;
812 begin match state.errfd with
813 | Some fd ->
814 begin match Ne.dup2 state.stderr Unix.stderr with
815 | Ne.Exn exn ->
816 dolog "failed to dup2 original stderr: %s" (exntos exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let undertext = function
903 | Unone -> "none"
904 | Ulinkuri s -> s
905 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
906 | Utext s -> "font: " ^ s
907 | Uunexpected s -> "unexpected: " ^ s
908 | Ulaunch s -> "launch: " ^ s
909 | Unamed s -> "named: " ^ s
910 | Uremote (filename, pageno) ->
911 Printf.sprintf "%s: page %d" filename (pageno+1)
914 let updateunder x y =
915 match getunder x y with
916 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
917 | Ulinkuri uri ->
918 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
919 Wsi.setcursor Wsi.CURSOR_INFO
920 | Ulinkgoto (pageno, _) ->
921 if conf.underinfo
922 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
923 Wsi.setcursor Wsi.CURSOR_INFO
924 | Utext s ->
925 if conf.underinfo then showtext 'f' ("ont: " ^ s);
926 Wsi.setcursor Wsi.CURSOR_TEXT
927 | Uunexpected s ->
928 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
929 Wsi.setcursor Wsi.CURSOR_INHERIT
930 | Ulaunch s ->
931 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
932 Wsi.setcursor Wsi.CURSOR_INHERIT
933 | Unamed s ->
934 if conf.underinfo then showtext 'n' ("amed: " ^ s);
935 Wsi.setcursor Wsi.CURSOR_INHERIT
936 | Uremote (filename, pageno) ->
937 if conf.underinfo then showtext 'r'
938 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
939 Wsi.setcursor Wsi.CURSOR_INFO
942 let showlinktype under =
943 if conf.underinfo
944 then
945 match under with
946 | Unone -> ()
947 | under ->
948 let s = undertext under in
949 showtext ' ' s
952 let addchar s c =
953 let b = Buffer.create (String.length s + 1) in
954 Buffer.add_string b s;
955 Buffer.add_char b c;
956 Buffer.contents b;
959 let colorspace_of_string s =
960 match String.lowercase s with
961 | "rgb" -> Rgb
962 | "bgr" -> Bgr
963 | "gray" -> Gray
964 | _ -> failwith "invalid colorspace"
967 let int_of_colorspace = function
968 | Rgb -> 0
969 | Bgr -> 1
970 | Gray -> 2
973 let colorspace_of_int = function
974 | 0 -> Rgb
975 | 1 -> Bgr
976 | 2 -> Gray
977 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
980 let colorspace_to_string = function
981 | Rgb -> "rgb"
982 | Bgr -> "bgr"
983 | Gray -> "gray"
986 let intentry_with_suffix text key =
987 let c =
988 if key >= 32 && key < 127
989 then Char.chr key
990 else '\000'
992 match Char.lowercase c with
993 | '0' .. '9' ->
994 let text = addchar text c in
995 TEcont text
997 | 'k' | 'm' | 'g' ->
998 let text = addchar text c in
999 TEcont text
1001 | _ ->
1002 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1003 TEcont text
1006 let multicolumns_to_string (n, a, b) =
1007 if a = 0 && b = 0
1008 then Printf.sprintf "%d" n
1009 else Printf.sprintf "%d,%d,%d" n a b;
1012 let multicolumns_of_string s =
1014 (int_of_string s, 0, 0)
1015 with _ ->
1016 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1017 if a > 1 || b > 1
1018 then failwith "subtly broken"; (n, a, b)
1022 let readcmd fd =
1023 let s = "xxxx" in
1024 let n = tempfailureretry (Unix.read fd s 0) 4 in
1025 if n != 4 then failwith "incomplete read(len)";
1026 let len = 0
1027 lor (Char.code s.[0] lsl 24)
1028 lor (Char.code s.[1] lsl 16)
1029 lor (Char.code s.[2] lsl 8)
1030 lor (Char.code s.[3] lsl 0)
1032 let s = String.create len in
1033 let n = tempfailureretry (Unix.read fd s 0) len in
1034 if n != len then failwith "incomplete read(data)";
1038 let btod b = if b then 1 else 0;;
1040 let wcmd fmt =
1041 let b = Buffer.create 16 in
1042 Buffer.add_string b "llll";
1043 Printf.kbprintf
1044 (fun b ->
1045 let s = Buffer.contents b in
1046 let n = String.length s in
1047 let len = n - 4 in
1048 (* dolog "wcmd %S" (String.sub s 4 len); *)
1049 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1050 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1051 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1052 s.[3] <- Char.chr (len land 0xff);
1053 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1054 if n' != n then failwith "write failed";
1055 ) b fmt;
1058 let calcips h =
1059 let d = state.winh - h in
1060 max conf.interpagespace ((d + 1) / 2)
1063 let rowyh (c, coverA, coverB) b n =
1064 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1065 then
1066 let _, _, vy, (_, _, h, _) = b.(n) in
1067 (vy, h)
1068 else
1069 let n' = n - coverA in
1070 let d = n' mod c in
1071 let s = n - d in
1072 let e = min state.pagecount (s + c) in
1073 let rec find m miny maxh = if m = e then miny, maxh else
1074 let _, _, y, (_, _, h, _) = b.(m) in
1075 let miny = min miny y in
1076 let maxh = max maxh h in
1077 find (m+1) miny maxh
1078 in find s max_int 0
1081 let calcheight () =
1082 match conf.columns with
1083 | Cmulti ((_, _, _) as cl, b) ->
1084 if Array.length b > 0
1085 then
1086 let y, h = rowyh cl b (Array.length b - 1) in
1087 y + h + (if conf.presentation then calcips h else 0)
1088 else 0
1089 | Csingle b ->
1090 if Array.length b > 0
1091 then
1092 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1093 y + h + (if conf.presentation then calcips h else 0)
1094 else 0
1095 | Csplit (_, b) ->
1096 if Array.length b > 0
1097 then
1098 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1099 y + h
1100 else 0
1103 let getpageyh pageno =
1104 let pageno = bound pageno 0 (state.pagecount-1) in
1105 match conf.columns with
1106 | Csingle b ->
1107 if Array.length b = 0
1108 then 0, 0
1109 else
1110 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1111 let y =
1112 if conf.presentation
1113 then y - calcips h
1114 else y
1116 y, h
1117 | Cmulti (cl, b) ->
1118 if Array.length b = 0
1119 then 0, 0
1120 else
1121 let y, h = rowyh cl b pageno in
1122 let y =
1123 if conf.presentation
1124 then y - calcips h
1125 else y
1127 y, h
1128 | Csplit (c, b) ->
1129 if Array.length b = 0
1130 then 0, 0
1131 else
1132 let n = pageno*c in
1133 let (_, _, y, (_, _, h, _)) = b.(n) in
1134 y, h
1137 let getpagedim pageno =
1138 let rec f ppdim l =
1139 match l with
1140 | (n, _, _, _) as pdim :: rest ->
1141 if n >= pageno
1142 then (if n = pageno then pdim else ppdim)
1143 else f pdim rest
1145 | [] -> ppdim
1147 f (-1, -1, -1, -1) state.pdims
1150 let getpagey pageno = fst (getpageyh pageno);;
1152 let nogeomcmds cmds =
1153 match cmds with
1154 | s, [] -> String.length s = 0
1155 | _ -> false
1158 let page_of_y y =
1159 let ((c, coverA, coverB) as cl), b =
1160 match conf.columns with
1161 | Csingle b -> (1, 0, 0), b
1162 | Cmulti (c, b) -> c, b
1163 | Csplit (_, b) -> (1, 0, 0), b
1165 if Array.length b = 0
1166 then -1
1167 else
1168 let rec bsearch nmin nmax =
1169 if nmin > nmax
1170 then bound nmin 0 (state.pagecount-1)
1171 else
1172 let n = (nmax + nmin) / 2 in
1173 let vy, h = rowyh cl b n in
1174 let y0, y1 =
1175 if conf.presentation
1176 then
1177 let ips = calcips h in
1178 let y0 = vy - ips in
1179 let y1 = vy + h + ips in
1180 y0, y1
1181 else (
1182 if n = 0
1183 then 0, vy + h + conf.interpagespace
1184 else
1185 let y0 = vy - conf.interpagespace in
1186 y0, y0 + h + conf.interpagespace
1189 if y >= y0 && y < y1
1190 then (
1191 if c = 1
1192 then n
1193 else (
1194 if n > coverA
1195 then
1196 if n < state.pagecount - coverB
1197 then ((n-coverA)/c)*c + coverA
1198 else n
1199 else n
1202 else (
1203 if y > y0
1204 then bsearch (n+1) nmax
1205 else bsearch nmin (n-1)
1208 let r = bsearch 0 (state.pagecount-1) in
1212 let layoutN ((columns, coverA, coverB), b) y sh =
1213 let sh = sh - state.hscrollh in
1214 let rec fold accu n =
1215 if n = Array.length b
1216 then accu
1217 else
1218 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1219 if (vy - y) > sh &&
1220 (n = coverA - 1
1221 || n = state.pagecount - coverB
1222 || (n - coverA) mod columns = columns - 1)
1223 then accu
1224 else
1225 let accu =
1226 if vy + h > y
1227 then
1228 let pagey = max 0 (y - vy) in
1229 let pagedispy = if pagey > 0 then 0 else vy - y in
1230 let pagedispx, pagex =
1231 let pdx =
1232 if n = coverA - 1 || n = state.pagecount - coverB
1233 then state.x + (state.winw - state.scrollw - w) / 2
1234 else dx + xoff + state.x
1236 if pdx < 0
1237 then 0, -pdx
1238 else pdx, 0
1240 let pagevw =
1241 let vw = state.winw - state.scrollw - pagedispx in
1242 let pw = w - pagex in
1243 min vw pw
1245 let pagevh = min (h - pagey) (sh - pagedispy) in
1246 if pagevw > 0 && pagevh > 0
1247 then
1248 let e =
1249 { pageno = n
1250 ; pagedimno = pdimno
1251 ; pagew = w
1252 ; pageh = h
1253 ; pagex = pagex
1254 ; pagey = pagey
1255 ; pagevw = pagevw
1256 ; pagevh = pagevh
1257 ; pagedispx = pagedispx
1258 ; pagedispy = pagedispy
1259 ; pagecol = 0
1262 e :: accu
1263 else
1264 accu
1265 else
1266 accu
1268 fold accu (n+1)
1270 List.rev (fold [] (page_of_y y));
1273 let layoutS (columns, b) y sh =
1274 let sh = sh - state.hscrollh in
1275 let rec fold accu n =
1276 if n = Array.length b
1277 then accu
1278 else
1279 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1280 if (vy - y) > sh
1281 then accu
1282 else
1283 let accu =
1284 if vy + pageh > y
1285 then
1286 let x = xoff + state.x in
1287 let pagey = max 0 (y - vy) in
1288 let pagedispy = if pagey > 0 then 0 else vy - y in
1289 let pagedispx, pagex =
1290 if px = 0
1291 then (
1292 if x < 0
1293 then 0, -x
1294 else x, 0
1296 else (
1297 let px = px - x in
1298 if px < 0
1299 then -px, 0
1300 else 0, px
1303 let pagecolw = pagew/columns in
1304 let pagedispx =
1305 if pagecolw < state.winw
1306 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1307 else pagedispx
1309 let pagevw =
1310 let vw = state.winw - pagedispx - state.scrollw in
1311 let pw = pagew - pagex in
1312 min vw pw
1314 let pagevw = min pagevw pagecolw in
1315 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1316 if pagevw > 0 && pagevh > 0
1317 then
1318 let e =
1319 { pageno = n/columns
1320 ; pagedimno = pdimno
1321 ; pagew = pagew
1322 ; pageh = pageh
1323 ; pagex = pagex
1324 ; pagey = pagey
1325 ; pagevw = pagevw
1326 ; pagevh = pagevh
1327 ; pagedispx = pagedispx
1328 ; pagedispy = pagedispy
1329 ; pagecol = n mod columns
1332 e :: accu
1333 else
1334 accu
1335 else
1336 accu
1338 fold accu (n+1)
1340 List.rev (fold [] 0)
1343 let layout y sh =
1344 if nogeomcmds state.geomcmds
1345 then
1346 match conf.columns with
1347 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1348 | Cmulti c -> layoutN c y sh
1349 | Csplit s -> layoutS s y sh
1350 else []
1353 let clamp incr =
1354 let y = state.y + incr in
1355 let y = max 0 y in
1356 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1360 let itertiles l f =
1361 let tilex = l.pagex mod conf.tilew in
1362 let tiley = l.pagey mod conf.tileh in
1364 let col = l.pagex / conf.tilew in
1365 let row = l.pagey / conf.tileh in
1367 let rec rowloop row y0 dispy h =
1368 if h = 0
1369 then ()
1370 else (
1371 let dh = conf.tileh - y0 in
1372 let dh = min h dh in
1373 let rec colloop col x0 dispx w =
1374 if w = 0
1375 then ()
1376 else (
1377 let dw = conf.tilew - x0 in
1378 let dw = min w dw in
1380 f col row dispx dispy x0 y0 dw dh;
1381 colloop (col+1) 0 (dispx+dw) (w-dw)
1384 colloop col tilex l.pagedispx l.pagevw;
1385 rowloop (row+1) 0 (dispy+dh) (h-dh)
1388 if l.pagevw > 0 && l.pagevh > 0
1389 then rowloop row tiley l.pagedispy l.pagevh;
1392 let gettileopaque l col row =
1393 let key =
1394 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1396 try Some (Hashtbl.find state.tilemap key)
1397 with Not_found -> None
1400 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1401 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1402 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1405 let drawtiles l color =
1406 GlDraw.color color;
1407 let f col row x y tilex tiley w h =
1408 match gettileopaque l col row with
1409 | Some (opaque, _, t) ->
1410 let params = x, y, w, h, tilex, tiley in
1411 if conf.invert
1412 then (
1413 Gl.enable `blend;
1414 GlFunc.blend_func `zero `one_minus_src_color;
1416 drawtile params opaque;
1417 if conf.invert
1418 then Gl.disable `blend;
1419 if conf.debug
1420 then (
1421 let s = Printf.sprintf
1422 "%d[%d,%d] %f sec"
1423 l.pageno col row t
1425 let w = measurestr fstate.fontsize s in
1426 GlMisc.push_attrib [`current];
1427 GlDraw.color (0.0, 0.0, 0.0);
1428 GlDraw.rect
1429 (float (x-2), float (y-2))
1430 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1431 GlDraw.color (1.0, 1.0, 1.0);
1432 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1433 GlMisc.pop_attrib ();
1436 | _ ->
1437 let w =
1438 let lw = state.winw - state.scrollw - x in
1439 min lw w
1440 and h =
1441 let lh = state.winh - y in
1442 min lh h
1444 begin match state.texid with
1445 | Some id ->
1446 Gl.enable `texture_2d;
1447 GlTex.bind_texture `texture_2d id;
1448 let x0 = float x
1449 and y0 = float y
1450 and x1 = float (x+w)
1451 and y1 = float (y+h) in
1453 let tw = float w /. 16.0
1454 and th = float h /. 16.0 in
1455 let tx0 = float tilex /. 16.0
1456 and ty0 = float tiley /. 16.0 in
1457 let tx1 = tx0 +. tw
1458 and ty1 = ty0 +. th in
1459 GlDraw.begins `quads;
1460 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1461 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1462 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1463 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1464 GlDraw.ends ();
1466 Gl.disable `texture_2d;
1467 | None ->
1468 GlDraw.color (1.0, 1.0, 1.0);
1469 GlDraw.rect
1470 (float x, float y)
1471 (float (x+w), float (y+h));
1472 end;
1473 if w > 128 && h > fstate.fontsize + 10
1474 then (
1475 GlDraw.color (0.0, 0.0, 0.0);
1476 let c, r =
1477 if conf.verbose
1478 then (col*conf.tilew, row*conf.tileh)
1479 else col, row
1481 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1483 GlDraw.color color;
1485 itertiles l f
1488 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1490 let tilevisible1 l x y =
1491 let ax0 = l.pagex
1492 and ax1 = l.pagex + l.pagevw
1493 and ay0 = l.pagey
1494 and ay1 = l.pagey + l.pagevh in
1496 let bx0 = x
1497 and by0 = y in
1498 let bx1 = min (bx0 + conf.tilew) l.pagew
1499 and by1 = min (by0 + conf.tileh) l.pageh in
1501 let rx0 = max ax0 bx0
1502 and ry0 = max ay0 by0
1503 and rx1 = min ax1 bx1
1504 and ry1 = min ay1 by1 in
1506 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1507 nonemptyintersection
1510 let tilevisible layout n x y =
1511 let rec findpageinlayout m = function
1512 | l :: rest when l.pageno = n ->
1513 tilevisible1 l x y || (
1514 match conf.columns with
1515 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1516 | _ -> false
1518 | _ :: rest -> findpageinlayout 0 rest
1519 | [] -> false
1521 findpageinlayout 0 layout;
1524 let tileready l x y =
1525 tilevisible1 l x y &&
1526 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1529 let tilepage n p layout =
1530 let rec loop = function
1531 | l :: rest ->
1532 if l.pageno = n
1533 then
1534 let f col row _ _ _ _ _ _ =
1535 if state.currently = Idle
1536 then
1537 match gettileopaque l col row with
1538 | Some _ -> ()
1539 | None ->
1540 let x = col*conf.tilew
1541 and y = row*conf.tileh in
1542 let w =
1543 let w = l.pagew - x in
1544 min w conf.tilew
1546 let h =
1547 let h = l.pageh - y in
1548 min h conf.tileh
1550 let pbo =
1551 if conf.usepbo
1552 then getpbo w h conf.colorspace
1553 else "0"
1555 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1556 state.currently <-
1557 Tiling (
1558 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1559 conf.tilew, conf.tileh
1562 itertiles l f;
1563 else
1564 loop rest
1566 | [] -> ()
1568 if nogeomcmds state.geomcmds
1569 then loop layout;
1572 let preloadlayout y =
1573 let y = if y < state.winh then 0 else y - state.winh in
1574 let h = state.winh*3 in
1575 layout y h;
1578 let load pages =
1579 let rec loop pages =
1580 if state.currently != Idle
1581 then ()
1582 else
1583 match pages with
1584 | l :: rest ->
1585 begin match getopaque l.pageno with
1586 | None ->
1587 wcmd "page %d %d" l.pageno l.pagedimno;
1588 state.currently <- Loading (l, state.gen);
1589 | Some opaque ->
1590 tilepage l.pageno opaque pages;
1591 loop rest
1592 end;
1593 | _ -> ()
1595 if nogeomcmds state.geomcmds
1596 then loop pages
1599 let preload pages =
1600 load pages;
1601 if conf.preload && state.currently = Idle
1602 then load (preloadlayout state.y);
1605 let layoutready layout =
1606 let rec fold all ls =
1607 all && match ls with
1608 | l :: rest ->
1609 let seen = ref false in
1610 let allvisible = ref true in
1611 let foo col row _ _ _ _ _ _ =
1612 seen := true;
1613 allvisible := !allvisible &&
1614 begin match gettileopaque l col row with
1615 | Some _ -> true
1616 | None -> false
1619 itertiles l foo;
1620 fold (!seen && !allvisible) rest
1621 | [] -> true
1623 let alltilesvisible = fold true layout in
1624 alltilesvisible;
1627 let gotoy y =
1628 state.wthack <- false;
1629 let y = bound y 0 state.maxy in
1630 let y, layout, proceed =
1631 match conf.maxwait with
1632 | Some time when state.ghyll == noghyll ->
1633 begin match state.throttle with
1634 | None ->
1635 let layout = layout y state.winh in
1636 let ready = layoutready layout in
1637 if not ready
1638 then (
1639 load layout;
1640 state.throttle <- Some (layout, y, now ());
1642 else G.postRedisplay "gotoy showall (None)";
1643 y, layout, ready
1644 | Some (_, _, started) ->
1645 let dt = now () -. started in
1646 if dt > time
1647 then (
1648 state.throttle <- None;
1649 let layout = layout y state.winh in
1650 load layout;
1651 G.postRedisplay "maxwait";
1652 y, layout, true
1654 else -1, [], false
1657 | _ ->
1658 let layout = layout y state.winh in
1659 G.postRedisplay "gotoy ready";
1660 y, layout, true
1662 if proceed
1663 then (
1664 state.y <- y;
1665 state.layout <- layout;
1666 begin match state.mode with
1667 | LinkNav (Ltexact (pageno, linkno)) ->
1668 let rec loop = function
1669 | [] ->
1670 state.mode <- LinkNav (Ltgendir 0)
1671 | l :: _ when l.pageno = pageno ->
1672 begin match getopaque pageno with
1673 | None ->
1674 state.mode <- LinkNav (Ltgendir 0)
1675 | Some opaque ->
1676 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1677 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1678 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1679 then state.mode <- LinkNav (Ltgendir 0)
1681 | _ :: rest -> loop rest
1683 loop layout
1684 | _ -> ()
1685 end;
1686 begin match state.mode with
1687 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1688 if not (pagevisible layout pageno)
1689 then (
1690 match state.layout with
1691 | [] -> ()
1692 | l :: _ ->
1693 state.mode <- Birdseye (
1694 conf, leftx, l.pageno, hooverpageno, anchor
1697 | LinkNav (Ltgendir dir as lt) ->
1698 let linknav =
1699 let rec loop = function
1700 | [] -> lt
1701 | l :: rest ->
1702 match getopaque l.pageno with
1703 | None -> loop rest
1704 | Some opaque ->
1705 let link =
1706 let ld =
1707 if dir = 0
1708 then LDfirstvisible (l.pagex, l.pagey, dir)
1709 else (
1710 if dir > 0 then LDfirst else LDlast
1713 findlink opaque ld
1715 match link with
1716 | Lnotfound -> loop rest
1717 | Lfound n ->
1718 showlinktype (getlink opaque n);
1719 Ltexact (l.pageno, n)
1721 loop state.layout
1723 state.mode <- LinkNav linknav
1724 | _ -> ()
1725 end;
1726 preload layout;
1728 state.ghyll <- noghyll;
1729 if conf.updatecurs
1730 then (
1731 let mx, my = state.mpos in
1732 updateunder mx my;
1736 let conttiling pageno opaque =
1737 tilepage pageno opaque
1738 (if conf.preload then preloadlayout state.y else state.layout)
1741 let gotoy_and_clear_text y =
1742 if not conf.verbose then state.text <- "";
1743 gotoy y;
1746 let getanchor1 l =
1747 let top =
1748 let coloff = l.pagecol * l.pageh in
1749 float (l.pagey + coloff) /. float l.pageh
1751 let dtop =
1752 if l.pagedispy = 0
1753 then
1755 else
1756 if conf.presentation
1757 then float l.pagedispy /. float (calcips l.pageh)
1758 else float l.pagedispy /. float conf.interpagespace
1760 (l.pageno, top, dtop)
1763 let getanchor () =
1764 match state.layout with
1765 | l :: _ -> getanchor1 l
1766 | [] ->
1767 let n = page_of_y state.y in
1768 if n = -1
1769 then state.anchor
1770 else
1771 let y, h = getpageyh n in
1772 let dy = y - state.y in
1773 let dtop =
1774 if conf.presentation
1775 then
1776 let ips = calcips h in
1777 float (dy + ips) /. float ips
1778 else
1779 float dy /. float conf.interpagespace
1781 (n, 0.0, dtop)
1784 let getanchory (n, top, dtop) =
1785 let y, h = getpageyh n in
1786 if conf.presentation
1787 then
1788 let ips = calcips h in
1789 y + truncate (top*.float h -. dtop*.float ips) + ips;
1790 else
1791 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1794 let gotoanchor anchor =
1795 gotoy (getanchory anchor);
1798 let addnav () =
1799 cbput state.hists.nav (getanchor ());
1802 let getnav dir =
1803 let anchor = cbgetc state.hists.nav dir in
1804 getanchory anchor;
1807 let gotoghyll y =
1808 let scroll f n a b =
1809 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1810 let snake f a b =
1811 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1812 if f < a
1813 then s (float f /. float a)
1814 else (
1815 if f > b
1816 then 1.0 -. s ((float (f-b) /. float (n-b)))
1817 else 1.0
1820 snake f a b
1821 and summa f n a b =
1822 (* courtesy:
1823 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1824 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1825 let iv1 = iv f in
1826 let ins = float a *. iv1
1827 and outs = float (n-b) *. iv1 in
1828 let ones = b - a in
1829 ins +. outs +. float ones
1831 let rec set (_N, _A, _B) y sy =
1832 let sum = summa 1.0 _N _A _B in
1833 let dy = float (y - sy) in
1834 state.ghyll <- (
1835 let rec gf n y1 o =
1836 if n >= _N
1837 then state.ghyll <- noghyll
1838 else
1839 let go n =
1840 let s = scroll n _N _A _B in
1841 let y1 = y1 +. ((s *. dy) /. sum) in
1842 gotoy_and_clear_text (truncate y1);
1843 state.ghyll <- gf (n+1) y1;
1845 match o with
1846 | None -> go n
1847 | Some y' -> set (_N/2, 1, 1) y' state.y
1849 gf 0 (float state.y)
1852 match conf.ghyllscroll with
1853 | None ->
1854 gotoy_and_clear_text y
1855 | Some nab ->
1856 if state.ghyll == noghyll
1857 then set nab y state.y
1858 else state.ghyll (Some y)
1861 let gotopage n top =
1862 let y, h = getpageyh n in
1863 let y = y + (truncate (top *. float h)) in
1864 gotoghyll y
1867 let gotopage1 n top =
1868 let y = getpagey n in
1869 let y = y + top in
1870 gotoghyll y
1873 let invalidate s f =
1874 state.layout <- [];
1875 state.pdims <- [];
1876 state.rects <- [];
1877 state.rects1 <- [];
1878 match state.geomcmds with
1879 | ps, [] when String.length ps = 0 ->
1880 f ();
1881 state.geomcmds <- s, [];
1883 | ps, [] ->
1884 state.geomcmds <- ps, [s, f];
1886 | ps, (s', _) :: rest when s' = s ->
1887 state.geomcmds <- ps, ((s, f) :: rest);
1889 | ps, cmds ->
1890 state.geomcmds <- ps, ((s, f) :: cmds);
1893 let flushpages () =
1894 Hashtbl.iter (fun _ opaque ->
1895 wcmd "freepage %s" opaque;
1896 ) state.pagemap;
1897 Hashtbl.clear state.pagemap;
1900 let opendoc path password =
1901 state.path <- path;
1902 state.password <- password;
1903 state.gen <- state.gen + 1;
1904 state.docinfo <- [];
1906 flushpages ();
1907 setaalevel conf.aalevel;
1908 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1909 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1910 invalidate "reqlayout"
1911 (fun () ->
1912 wcmd "reqlayout %d %d %s\000"
1913 conf.angle (btod conf.proportional) state.nameddest;
1917 let reload () =
1918 state.anchor <- getanchor ();
1919 state.wthack <- !wtmode;
1920 opendoc state.path state.password;
1923 let scalecolor c =
1924 let c = c *. conf.colorscale in
1925 (c, c, c);
1928 let scalecolor2 (r, g, b) =
1929 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1932 let docolumns = function
1933 | Csingle _ ->
1934 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1935 let rec loop pageno pdimno pdim y ph pdims =
1936 if pageno = state.pagecount
1937 then ()
1938 else
1939 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1940 match pdims with
1941 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1942 pdimno+1, pdim, rest
1943 | _ ->
1944 pdimno, pdim, pdims
1946 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1947 let y = y +
1948 (if conf.presentation
1949 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1950 else (if pageno = 0 then 0 else conf.interpagespace)
1953 a.(pageno) <- (pdimno, x, y, pdim);
1954 loop (pageno+1) pdimno pdim (y + h) h pdims
1956 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1957 conf.columns <- Csingle a;
1959 | Cmulti ((columns, coverA, coverB), _) ->
1960 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1961 let rec loop pageno pdimno pdim x y rowh pdims =
1962 let rec fixrow m = if m = pageno then () else
1963 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1964 if h < rowh
1965 then (
1966 let y = y + (rowh - h) / 2 in
1967 a.(m) <- (pdimno, x, y, pdim);
1969 fixrow (m+1)
1971 if pageno = state.pagecount
1972 then fixrow (((pageno - 1) / columns) * columns)
1973 else
1974 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1975 match pdims with
1976 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1977 pdimno+1, pdim, rest
1978 | _ ->
1979 pdimno, pdim, pdims
1981 let x, y, rowh' =
1982 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1983 then (
1984 let x = (state.winw - state.scrollw - w) / 2 in
1985 let ips =
1986 if conf.presentation then calcips h else conf.interpagespace in
1987 x, y + ips + rowh, h
1989 else (
1990 if (pageno - coverA) mod columns = 0
1991 then (
1992 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
1993 let y =
1994 if conf.presentation
1995 then
1996 let ips = calcips h in
1997 y + (if pageno = 0 then 0 else calcips rowh + ips)
1998 else
1999 y + (if pageno = 0 then 0 else conf.interpagespace)
2001 x, y + rowh, h
2003 else x, y, max rowh h
2006 let y =
2007 if pageno > 1 && (pageno - coverA) mod columns = 0
2008 then (
2009 let y =
2010 if pageno = columns && conf.presentation
2011 then (
2012 let ips = calcips rowh in
2013 for i = 0 to pred columns
2015 let (pdimno, x, y, pdim) = a.(i) in
2016 a.(i) <- (pdimno, x, y+ips, pdim)
2017 done;
2018 y+ips;
2020 else y
2022 fixrow (pageno - columns);
2025 else y
2027 a.(pageno) <- (pdimno, x, y, pdim);
2028 let x = x + w + xoff*2 + conf.interpagespace in
2029 loop (pageno+1) pdimno pdim x y rowh' pdims
2031 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2032 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2034 | Csplit (c, _) ->
2035 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2036 let rec loop pageno pdimno pdim y pdims =
2037 if pageno = state.pagecount
2038 then ()
2039 else
2040 let pdimno, ((_, w, h, _) as pdim), pdims =
2041 match pdims with
2042 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2043 pdimno+1, pdim, rest
2044 | _ ->
2045 pdimno, pdim, pdims
2047 let cw = w / c in
2048 let rec loop1 n x y =
2049 if n = c then y else (
2050 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2051 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2054 let y = loop1 0 0 y in
2055 loop (pageno+1) pdimno pdim y pdims
2057 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2058 conf.columns <- Csplit (c, a);
2061 let represent () =
2062 docolumns conf.columns;
2063 state.maxy <- calcheight ();
2064 state.hscrollh <-
2065 if state.w <= state.winw - state.scrollw
2066 then 0
2067 else state.scrollw
2069 if state.reprf == noreprf
2070 then
2071 let wthack = state.wthack in
2072 begin match state.mode with
2073 | Birdseye (_, _, pageno, _, _) ->
2074 let y, h = getpageyh pageno in
2075 let top = (state.winh - h) / 2 in
2076 gotoy (max 0 (y - top))
2077 | _ -> gotoanchor state.anchor
2078 end;
2079 state.wthack <- wthack;
2080 state.reprf <- noreprf;
2081 else state.reprf ()
2084 let reshape w h =
2085 state.wthack <- false;
2086 GlDraw.viewport 0 0 w h;
2087 let firsttime = state.geomcmds == firstgeomcmds in
2088 if not firsttime && nogeomcmds state.geomcmds
2089 then state.anchor <- getanchor ();
2091 state.winw <- w;
2092 let w = truncate (float w *. conf.zoom) - state.scrollw in
2093 let w = max w 2 in
2094 state.winh <- h;
2095 setfontsize fstate.fontsize;
2096 GlMat.mode `modelview;
2097 GlMat.load_identity ();
2099 GlMat.mode `projection;
2100 GlMat.load_identity ();
2101 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2102 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2103 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2105 let relx =
2106 if conf.zoom <= 1.0
2107 then 0.0
2108 else float state.x /. float state.w
2110 invalidate "geometry"
2111 (fun () ->
2112 state.w <- w;
2113 if not firsttime
2114 then state.x <- truncate (relx *. float w);
2115 let w =
2116 match conf.columns with
2117 | Csingle _ -> w
2118 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2119 | Csplit (c, _) -> w * c
2121 wcmd "geometry %d %d" w h);
2124 let enttext () =
2125 let len = String.length state.text in
2126 let drawstring s =
2127 let hscrollh =
2128 match state.mode with
2129 | Textentry _
2130 | View ->
2131 let h, _, _ = state.uioh#scrollpw in
2133 | _ -> 0
2135 let rect x w =
2136 GlDraw.rect
2137 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2138 (x+.w, float (state.winh - hscrollh))
2141 let w = float (state.winw - state.scrollw - 1) in
2142 if state.progress >= 0.0 && state.progress < 1.0
2143 then (
2144 GlDraw.color (0.3, 0.3, 0.3);
2145 let w1 = w *. state.progress in
2146 rect 0.0 w1;
2147 GlDraw.color (0.0, 0.0, 0.0);
2148 rect w1 (w-.w1)
2150 else (
2151 GlDraw.color (0.0, 0.0, 0.0);
2152 rect 0.0 w;
2155 GlDraw.color (1.0, 1.0, 1.0);
2156 drawstring fstate.fontsize
2157 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2159 let s =
2160 match state.mode with
2161 | Textentry ((prefix, text, _, _, _, _), _) ->
2162 let s =
2163 if len > 0
2164 then
2165 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2166 else
2167 Printf.sprintf "%s%s_" prefix text
2171 | _ -> state.text
2173 let s =
2174 if state.newerrmsgs
2175 then (
2176 if not (istextentry state.mode)
2177 then
2178 let s1 = "(press 'e' to review error messasges)" in
2179 if String.length s > 0 then s ^ " " ^ s1 else s1
2180 else s
2182 else s
2184 if String.length s > 0
2185 then drawstring s
2188 let gctiles () =
2189 let len = Queue.length state.tilelru in
2190 let layout = lazy (
2191 match state.throttle with
2192 | None ->
2193 if conf.preload
2194 then preloadlayout state.y
2195 else state.layout
2196 | Some (layout, _, _) ->
2197 layout
2198 ) in
2199 let rec loop qpos =
2200 if state.memused <= conf.memlimit
2201 then ()
2202 else (
2203 if qpos < len
2204 then
2205 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2206 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2207 let (_, pw, ph, _) = getpagedim n in
2209 gen = state.gen
2210 && colorspace = conf.colorspace
2211 && angle = conf.angle
2212 && pagew = pw
2213 && pageh = ph
2214 && (
2215 let x = col*conf.tilew
2216 and y = row*conf.tileh in
2217 tilevisible (Lazy.force_val layout) n x y
2219 then Queue.push lruitem state.tilelru
2220 else (
2221 freepbo p;
2222 wcmd "freetile %s" p;
2223 state.memused <- state.memused - s;
2224 state.uioh#infochanged Memused;
2225 Hashtbl.remove state.tilemap k;
2227 loop (qpos+1)
2230 loop 0
2233 let flushtiles () =
2234 Queue.iter (fun (k, p, s) ->
2235 wcmd "freetile %s" p;
2236 state.memused <- state.memused - s;
2237 state.uioh#infochanged Memused;
2238 Hashtbl.remove state.tilemap k;
2239 ) state.tilelru;
2240 Queue.clear state.tilelru;
2241 load state.layout;
2244 let logcurrently = function
2245 | Idle -> dolog "Idle"
2246 | Loading (l, gen) ->
2247 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2248 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2249 dolog
2250 "Tiling %d[%d,%d] page=%s cs=%s angle"
2251 l.pageno col row pageopaque
2252 (colorspace_to_string colorspace)
2254 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2255 angle gen conf.angle state.gen
2256 tilew tileh
2257 conf.tilew conf.tileh
2259 | Outlining _ ->
2260 dolog "outlining"
2263 let splitatspace =
2264 let r = Str.regexp " " in
2265 fun s -> Str.bounded_split r s 2;
2268 let onpagerect pageno f =
2269 let b =
2270 match conf.columns with
2271 | Cmulti (_, b) -> b
2272 | Csingle b -> b
2273 | Csplit (_, b) -> b
2275 if pageno >= 0 && pageno < Array.length b
2276 then
2277 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2278 let r = getpdimrect pdimno in
2279 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2282 let gotopagexy pageno x y =
2283 onpagerect pageno (fun w h ->
2284 let top = y /. h in
2285 let _,w1,_,leftx = getpagedim pageno in
2286 let sw = float w1 /. w in
2287 let x = sw *. x in
2288 let x = leftx + state.x + truncate x in
2289 let newpan =
2290 if x < 0 || x >= state.winw - state.scrollw
2291 then (state.x <- state.x - x; true)
2292 else false
2294 let y, h = getpageyh pageno in
2295 let y' = y + truncate (top *. float h) in
2296 let dy = y' - state.y in
2297 if newpan || not (dy > 0 && dy < state.winh - state.hscrollh)
2298 then (
2299 let y =
2300 if conf.presentation
2301 then
2302 if abs (y - y') > state.winh - state.hscrollh
2303 then y'
2304 else y
2305 else y';
2307 gotoy y;
2308 state.wthack <- !wtmode && not (layoutready state.layout);
2313 let act cmds =
2314 (* dolog "%S" cmds; *)
2315 let cl = splitatspace cmds in
2316 let scan s fmt f =
2317 try Scanf.sscanf s fmt f
2318 with exn ->
2319 dolog "error processing '%S': %s" cmds (exntos exn);
2320 exit 1
2322 match cl with
2323 | "clear" :: [] ->
2324 state.uioh#infochanged Pdim;
2325 state.pdims <- [];
2327 | "clearrects" :: [] ->
2328 state.rects <- state.rects1;
2329 G.postRedisplay "clearrects";
2331 | "continue" :: args :: [] ->
2332 let n = scan args "%u" (fun n -> n) in
2333 state.pagecount <- n;
2334 begin match state.currently with
2335 | Outlining l ->
2336 state.currently <- Idle;
2337 state.outlines <- Array.of_list (List.rev l)
2338 | _ -> ()
2339 end;
2341 let cur, cmds = state.geomcmds in
2342 if String.length cur = 0
2343 then failwith "umpossible";
2345 begin match List.rev cmds with
2346 | [] ->
2347 state.geomcmds <- "", [];
2348 represent ();
2349 | (s, f) :: rest ->
2350 f ();
2351 state.geomcmds <- s, List.rev rest;
2352 end;
2353 if conf.maxwait = None
2354 then G.postRedisplay "continue";
2356 | "title" :: args :: [] ->
2357 Wsi.settitle args
2359 | "msg" :: args :: [] ->
2360 showtext ' ' args
2362 | "vmsg" :: args :: [] ->
2363 if conf.verbose
2364 then showtext ' ' args
2366 | "emsg" :: args :: [] ->
2367 Buffer.add_string state.errmsgs args;
2368 state.newerrmsgs <- true;
2369 G.postRedisplay "error message"
2371 | "progress" :: args :: [] ->
2372 let progress, text =
2373 scan args "%f %n"
2374 (fun f pos ->
2375 f, String.sub args pos (String.length args - pos))
2377 state.text <- text;
2378 state.progress <- progress;
2379 G.postRedisplay "progress"
2381 | "firstmatch" :: args :: [] ->
2382 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2383 scan args "%u %d %f %f %f %f %f %f %f %f"
2384 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2385 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2387 let y = (getpagey pageno) + truncate y0 in
2388 addnav ();
2389 gotoy y;
2390 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2392 | "match" :: args :: [] ->
2393 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2394 scan args "%u %d %f %f %f %f %f %f %f %f"
2395 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2396 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2398 state.rects1 <-
2399 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2401 | "page" :: args :: [] ->
2402 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2403 begin match state.currently with
2404 | Loading (l, gen) ->
2405 vlog "page %d took %f sec" l.pageno t;
2406 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2407 begin match state.throttle with
2408 | None ->
2409 let preloadedpages =
2410 if conf.preload
2411 then preloadlayout state.y
2412 else state.layout
2414 let evict () =
2415 let module IntSet =
2416 Set.Make (struct type t = int let compare = (-) end) in
2417 let set =
2418 List.fold_left (fun s l -> IntSet.add l.pageno s)
2419 IntSet.empty preloadedpages
2421 let evictedpages =
2422 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2423 if not (IntSet.mem pageno set)
2424 then (
2425 wcmd "freepage %s" opaque;
2426 key :: accu
2428 else accu
2429 ) state.pagemap []
2431 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2433 evict ();
2434 state.currently <- Idle;
2435 if gen = state.gen
2436 then (
2437 tilepage l.pageno pageopaque state.layout;
2438 load state.layout;
2439 load preloadedpages;
2440 if pagevisible state.layout l.pageno
2441 && layoutready state.layout
2442 then G.postRedisplay "page";
2445 | Some (layout, _, _) ->
2446 state.currently <- Idle;
2447 tilepage l.pageno pageopaque layout;
2448 load state.layout
2449 end;
2451 | _ ->
2452 dolog "Inconsistent loading state";
2453 logcurrently state.currently;
2454 exit 1
2457 | "tile" :: args :: [] ->
2458 let (x, y, opaque, size, t) =
2459 scan args "%u %u %s %u %f"
2460 (fun x y p size t -> (x, y, p, size, t))
2462 begin match state.currently with
2463 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2464 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2466 unmappbo opaque;
2467 if tilew != conf.tilew || tileh != conf.tileh
2468 then (
2469 wcmd "freetile %s" opaque;
2470 state.currently <- Idle;
2471 load state.layout;
2473 else (
2474 puttileopaque l col row gen cs angle opaque size t;
2475 state.memused <- state.memused + size;
2476 state.uioh#infochanged Memused;
2477 gctiles ();
2478 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2479 opaque, size) state.tilelru;
2481 let layout =
2482 match state.throttle with
2483 | None -> state.layout
2484 | Some (layout, _, _) -> layout
2487 state.currently <- Idle;
2488 if gen = state.gen
2489 && conf.colorspace = cs
2490 && conf.angle = angle
2491 && tilevisible layout l.pageno x y
2492 then conttiling l.pageno pageopaque;
2494 begin match state.throttle with
2495 | None ->
2496 if state.wthack
2497 then state.wthack <- not (layoutready state.layout);
2498 preload state.layout;
2499 if gen = state.gen
2500 && conf.colorspace = cs
2501 && conf.angle = angle
2502 && tilevisible state.layout l.pageno x y
2503 then G.postRedisplay "tile nothrottle";
2505 | Some (layout, y, _) ->
2506 let ready = layoutready layout in
2507 if ready
2508 then (
2509 state.wthack <- false;
2510 state.y <- y;
2511 state.layout <- layout;
2512 state.throttle <- None;
2513 G.postRedisplay "throttle";
2515 else load layout;
2516 end;
2519 | _ ->
2520 dolog "Inconsistent tiling state";
2521 logcurrently state.currently;
2522 exit 1
2525 | "pdim" :: args :: [] ->
2526 let pdim =
2527 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2529 state.uioh#infochanged Pdim;
2530 state.pdims <- pdim :: state.pdims
2532 | "o" :: args :: [] ->
2533 let (l, n, t, h, pos) =
2534 scan args "%u %u %d %u %n"
2535 (fun l n t h pos -> l, n, t, h, pos)
2537 let s = String.sub args pos (String.length args - pos) in
2538 let outline = (s, l, (n, float t /. float h, 0.0)) in
2539 begin match state.currently with
2540 | Outlining outlines ->
2541 state.currently <- Outlining (outline :: outlines)
2542 | Idle ->
2543 state.currently <- Outlining [outline]
2544 | currently ->
2545 dolog "invalid outlining state";
2546 logcurrently currently
2549 | "a" :: args :: [] ->
2550 let (n, l, t) =
2551 scan args "%u %d %d" (fun n l t -> n, l, t)
2553 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2555 | "info" :: args :: [] ->
2556 state.docinfo <- (1, args) :: state.docinfo
2558 | "infoend" :: [] ->
2559 state.uioh#infochanged Docinfo;
2560 state.docinfo <- List.rev state.docinfo
2562 | _ ->
2563 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2566 let onhist cb =
2567 let rc = cb.rc in
2568 let action = function
2569 | HCprev -> cbget cb ~-1
2570 | HCnext -> cbget cb 1
2571 | HCfirst -> cbget cb ~-(cb.rc)
2572 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2573 and cancel () = cb.rc <- rc
2574 in (action, cancel)
2577 let search pattern forward =
2578 if String.length pattern > 0
2579 then
2580 let pn, py =
2581 match state.layout with
2582 | [] -> 0, 0
2583 | l :: _ ->
2584 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2586 wcmd "search %d %d %d %d,%s\000"
2587 (btod conf.icase) pn py (btod forward) pattern;
2590 let intentry text key =
2591 let c =
2592 if key >= 32 && key < 127
2593 then Char.chr key
2594 else '\000'
2596 match c with
2597 | '0' .. '9' ->
2598 let text = addchar text c in
2599 TEcont text
2601 | _ ->
2602 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2603 TEcont text
2606 let linknentry text key =
2607 let c =
2608 if key >= 32 && key < 127
2609 then Char.chr key
2610 else '\000'
2612 match c with
2613 | 'a' .. 'z' ->
2614 let text = addchar text c in
2615 TEcont text
2617 | _ ->
2618 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2619 TEcont text
2622 let linkndone f s =
2623 if String.length s > 0
2624 then (
2625 let n =
2626 let l = String.length s in
2627 let rec loop pos n = if pos = l then n else
2628 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2629 loop (pos+1) (n*26 + m)
2630 in loop 0 0
2632 let rec loop n = function
2633 | [] -> ()
2634 | l :: rest ->
2635 match getopaque l.pageno with
2636 | None -> loop n rest
2637 | Some opaque ->
2638 let m = getlinkcount opaque in
2639 if n < m
2640 then (
2641 let under = getlink opaque n in
2642 f under
2644 else loop (n-m) rest
2646 loop n state.layout;
2650 let textentry text key =
2651 if key land 0xff00 = 0xff00
2652 then TEcont text
2653 else TEcont (text ^ toutf8 key)
2656 let reqlayout angle proportional =
2657 match state.throttle with
2658 | None ->
2659 if nogeomcmds state.geomcmds
2660 then state.anchor <- getanchor ();
2661 conf.angle <- angle mod 360;
2662 if conf.angle != 0
2663 then (
2664 match state.mode with
2665 | LinkNav _ -> state.mode <- View
2666 | _ -> ()
2668 conf.proportional <- proportional;
2669 invalidate "reqlayout"
2670 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2671 | _ -> ()
2674 let settrim trimmargins trimfuzz =
2675 if nogeomcmds state.geomcmds
2676 then state.anchor <- getanchor ();
2677 conf.trimmargins <- trimmargins;
2678 conf.trimfuzz <- trimfuzz;
2679 let x0, y0, x1, y1 = trimfuzz in
2680 invalidate "settrim"
2681 (fun () ->
2682 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2683 flushpages ();
2686 let setzoom zoom =
2687 match state.throttle with
2688 | None ->
2689 let zoom = max 0.01 zoom in
2690 if zoom <> conf.zoom
2691 then (
2692 state.prevzoom <- conf.zoom;
2693 conf.zoom <- zoom;
2694 reshape state.winw state.winh;
2695 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2698 | Some (layout, y, started) ->
2699 let time =
2700 match conf.maxwait with
2701 | None -> 0.0
2702 | Some t -> t
2704 let dt = now () -. started in
2705 if dt > time
2706 then (
2707 state.y <- y;
2708 load layout;
2712 let setcolumns mode columns coverA coverB =
2713 state.prevcolumns <- Some (conf.columns, conf.zoom);
2714 if columns < 0
2715 then (
2716 if isbirdseye mode
2717 then showtext '!' "split mode doesn't work in bird's eye"
2718 else (
2719 conf.columns <- Csplit (-columns, [||]);
2720 state.x <- 0;
2721 conf.zoom <- 1.0;
2724 else (
2725 if columns < 2
2726 then (
2727 conf.columns <- Csingle [||];
2728 state.x <- 0;
2729 setzoom 1.0;
2731 else (
2732 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2733 conf.zoom <- 1.0;
2736 reshape state.winw state.winh;
2739 let enterbirdseye () =
2740 let zoom = float conf.thumbw /. float state.winw in
2741 let birdseyepageno =
2742 let cy = state.winh / 2 in
2743 let fold = function
2744 | [] -> 0
2745 | l :: rest ->
2746 let rec fold best = function
2747 | [] -> best.pageno
2748 | l :: rest ->
2749 let d = cy - (l.pagedispy + l.pagevh/2)
2750 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2751 if abs d < abs dbest
2752 then fold l rest
2753 else best.pageno
2754 in fold l rest
2756 fold state.layout
2758 state.mode <- Birdseye (
2759 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2761 conf.zoom <- zoom;
2762 conf.presentation <- false;
2763 conf.interpagespace <- 10;
2764 conf.hlinks <- false;
2765 state.x <- 0;
2766 state.mstate <- Mnone;
2767 conf.maxwait <- None;
2768 conf.columns <- (
2769 match conf.beyecolumns with
2770 | Some c ->
2771 conf.zoom <- 1.0;
2772 Cmulti ((c, 0, 0), [||])
2773 | None -> Csingle [||]
2775 Wsi.setcursor Wsi.CURSOR_INHERIT;
2776 if conf.verbose
2777 then
2778 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2779 (100.0*.zoom)
2780 else
2781 state.text <- ""
2783 reshape state.winw state.winh;
2786 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2787 state.mode <- View;
2788 conf.zoom <- c.zoom;
2789 conf.presentation <- c.presentation;
2790 conf.interpagespace <- c.interpagespace;
2791 conf.maxwait <- c.maxwait;
2792 conf.hlinks <- c.hlinks;
2793 conf.beyecolumns <- (
2794 match conf.columns with
2795 | Cmulti ((c, _, _), _) -> Some c
2796 | Csingle _ -> None
2797 | Csplit _ -> failwith "leaving bird's eye split mode"
2799 conf.columns <- (
2800 match c.columns with
2801 | Cmulti (c, _) -> Cmulti (c, [||])
2802 | Csingle _ -> Csingle [||]
2803 | Csplit (c, _) -> Csplit (c, [||])
2805 state.x <- leftx;
2806 if conf.verbose
2807 then
2808 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2809 (100.0*.conf.zoom)
2811 reshape state.winw state.winh;
2812 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2815 let togglebirdseye () =
2816 match state.mode with
2817 | Birdseye vals -> leavebirdseye vals true
2818 | View -> enterbirdseye ()
2819 | _ -> ()
2822 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2823 let pageno = max 0 (pageno - incr) in
2824 let rec loop = function
2825 | [] -> gotopage1 pageno 0
2826 | l :: _ when l.pageno = pageno ->
2827 if l.pagedispy >= 0 && l.pagey = 0
2828 then G.postRedisplay "upbirdseye"
2829 else gotopage1 pageno 0
2830 | _ :: rest -> loop rest
2832 loop state.layout;
2833 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2836 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2837 let pageno = min (state.pagecount - 1) (pageno + incr) in
2838 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2839 let rec loop = function
2840 | [] ->
2841 let y, h = getpageyh pageno in
2842 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2843 gotoy (clamp dy)
2844 | l :: _ when l.pageno = pageno ->
2845 if l.pagevh != l.pageh
2846 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2847 else G.postRedisplay "downbirdseye"
2848 | _ :: rest -> loop rest
2850 loop state.layout
2853 let optentry mode _ key =
2854 let btos b = if b then "on" else "off" in
2855 if key >= 32 && key < 127
2856 then
2857 let c = Char.chr key in
2858 match c with
2859 | 's' ->
2860 let ondone s =
2861 try conf.scrollstep <- int_of_string s with exc ->
2862 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2864 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2866 | 'A' ->
2867 let ondone s =
2869 conf.autoscrollstep <- int_of_string s;
2870 if state.autoscroll <> None
2871 then state.autoscroll <- Some conf.autoscrollstep
2872 with exc ->
2873 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2875 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2877 | 'C' ->
2878 let ondone s =
2880 let n, a, b = multicolumns_of_string s in
2881 setcolumns mode n a b;
2882 with exc ->
2883 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2885 TEswitch ("columns: ", "", None, textentry, ondone, true)
2887 | 'Z' ->
2888 let ondone s =
2890 let zoom = float (int_of_string s) /. 100.0 in
2891 setzoom zoom
2892 with exc ->
2893 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2895 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2897 | 't' ->
2898 let ondone s =
2900 conf.thumbw <- bound (int_of_string s) 2 4096;
2901 state.text <-
2902 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2903 begin match mode with
2904 | Birdseye beye ->
2905 leavebirdseye beye false;
2906 enterbirdseye ();
2907 | _ -> ();
2909 with exc ->
2910 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2912 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2914 | 'R' ->
2915 let ondone s =
2916 match try
2917 Some (int_of_string s)
2918 with exc ->
2919 state.text <- Printf.sprintf "bad integer `%s': %s"
2920 s (exntos exc);
2921 None
2922 with
2923 | Some angle -> reqlayout angle conf.proportional
2924 | None -> ()
2926 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2928 | 'i' ->
2929 conf.icase <- not conf.icase;
2930 TEdone ("case insensitive search " ^ (btos conf.icase))
2932 | 'p' ->
2933 conf.preload <- not conf.preload;
2934 gotoy state.y;
2935 TEdone ("preload " ^ (btos conf.preload))
2937 | 'v' ->
2938 conf.verbose <- not conf.verbose;
2939 TEdone ("verbose " ^ (btos conf.verbose))
2941 | 'd' ->
2942 conf.debug <- not conf.debug;
2943 TEdone ("debug " ^ (btos conf.debug))
2945 | 'h' ->
2946 conf.maxhfit <- not conf.maxhfit;
2947 state.maxy <- calcheight ();
2948 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2950 | 'c' ->
2951 conf.crophack <- not conf.crophack;
2952 TEdone ("crophack " ^ btos conf.crophack)
2954 | 'a' ->
2955 let s =
2956 match conf.maxwait with
2957 | None ->
2958 conf.maxwait <- Some infinity;
2959 "always wait for page to complete"
2960 | Some _ ->
2961 conf.maxwait <- None;
2962 "show placeholder if page is not ready"
2964 TEdone s
2966 | 'f' ->
2967 conf.underinfo <- not conf.underinfo;
2968 TEdone ("underinfo " ^ btos conf.underinfo)
2970 | 'P' ->
2971 conf.savebmarks <- not conf.savebmarks;
2972 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2974 | 'S' ->
2975 let ondone s =
2977 let pageno, py =
2978 match state.layout with
2979 | [] -> 0, 0
2980 | l :: _ ->
2981 l.pageno, l.pagey
2983 conf.interpagespace <- int_of_string s;
2984 docolumns conf.columns;
2985 state.maxy <- calcheight ();
2986 let y = getpagey pageno in
2987 gotoy (y + py)
2988 with exc ->
2989 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2991 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2993 | 'l' ->
2994 reqlayout conf.angle (not conf.proportional);
2995 TEdone ("proportional display " ^ btos conf.proportional)
2997 | 'T' ->
2998 settrim (not conf.trimmargins) conf.trimfuzz;
2999 TEdone ("trim margins " ^ btos conf.trimmargins)
3001 | 'I' ->
3002 conf.invert <- not conf.invert;
3003 TEdone ("invert colors " ^ btos conf.invert)
3005 | 'x' ->
3006 let ondone s =
3007 cbput state.hists.sel s;
3008 conf.selcmd <- s;
3010 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3011 textentry, ondone, true)
3013 | _ ->
3014 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3015 TEstop
3016 else
3017 TEcont state.text
3020 class type lvsource = object
3021 method getitemcount : int
3022 method getitem : int -> (string * int)
3023 method hasaction : int -> bool
3024 method exit :
3025 uioh:uioh ->
3026 cancel:bool ->
3027 active:int ->
3028 first:int ->
3029 pan:int ->
3030 qsearch:string ->
3031 uioh option
3032 method getactive : int
3033 method getfirst : int
3034 method getqsearch : string
3035 method setqsearch : string -> unit
3036 method getpan : int
3037 end;;
3039 class virtual lvsourcebase = object
3040 val mutable m_active = 0
3041 val mutable m_first = 0
3042 val mutable m_qsearch = ""
3043 val mutable m_pan = 0
3044 method getactive = m_active
3045 method getfirst = m_first
3046 method getqsearch = m_qsearch
3047 method getpan = m_pan
3048 method setqsearch s = m_qsearch <- s
3049 end;;
3051 let withoutlastutf8 s =
3052 let len = String.length s in
3053 if len = 0
3054 then s
3055 else
3056 let rec find pos =
3057 if pos = 0
3058 then pos
3059 else
3060 let b = Char.code s.[pos] in
3061 if b land 0b11000000 = 0b11000000
3062 then pos
3063 else find (pos-1)
3065 let first =
3066 if Char.code s.[len-1] land 0x80 = 0
3067 then len-1
3068 else find (len-1)
3070 String.sub s 0 first;
3073 let textentrykeyboard
3074 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3075 let key =
3076 if key >= 0xffb0 && key <= 0xffb9
3077 then key - 0xffb0 + 48 else key
3079 let enttext te =
3080 state.mode <- Textentry (te, onleave);
3081 state.text <- "";
3082 enttext ();
3083 G.postRedisplay "textentrykeyboard enttext";
3085 let histaction cmd =
3086 match opthist with
3087 | None -> ()
3088 | Some (action, _) ->
3089 state.mode <- Textentry (
3090 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3092 G.postRedisplay "textentry histaction"
3094 match key with
3095 | 0xff08 -> (* backspace *)
3096 let s = withoutlastutf8 text in
3097 let len = String.length s in
3098 if cancelonempty && len = 0
3099 then (
3100 onleave Cancel;
3101 G.postRedisplay "textentrykeyboard after cancel";
3103 else (
3104 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3107 | 0xff0d | 0xff8d -> (* (kp) enter *)
3108 ondone text;
3109 onleave Confirm;
3110 G.postRedisplay "textentrykeyboard after confirm"
3112 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3113 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3114 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3115 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3117 | 0xff1b -> (* escape*)
3118 if String.length text = 0
3119 then (
3120 begin match opthist with
3121 | None -> ()
3122 | Some (_, onhistcancel) -> onhistcancel ()
3123 end;
3124 onleave Cancel;
3125 state.text <- "";
3126 G.postRedisplay "textentrykeyboard after cancel2"
3128 else (
3129 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3132 | 0xff9f | 0xffff -> () (* delete *)
3134 | _ when key != 0
3135 && key land 0xff00 != 0xff00 (* keyboard *)
3136 && key land 0xfe00 != 0xfe00 (* xkb *)
3137 && key land 0xfd00 != 0xfd00 (* 3270 *)
3139 begin match onkey text key with
3140 | TEdone text ->
3141 ondone text;
3142 onleave Confirm;
3143 G.postRedisplay "textentrykeyboard after confirm2";
3145 | TEcont text ->
3146 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3148 | TEstop ->
3149 onleave Cancel;
3150 G.postRedisplay "textentrykeyboard after cancel3"
3152 | TEswitch te ->
3153 state.mode <- Textentry (te, onleave);
3154 G.postRedisplay "textentrykeyboard switch";
3155 end;
3157 | _ ->
3158 vlog "unhandled key %s" (Wsi.keyname key)
3161 let firstof first active =
3162 if first > active || abs (first - active) > fstate.maxrows - 1
3163 then max 0 (active - (fstate.maxrows/2))
3164 else first
3167 let calcfirst first active =
3168 if active > first
3169 then
3170 let rows = active - first in
3171 if rows > fstate.maxrows then active - fstate.maxrows else first
3172 else active
3175 let scrollph y maxy =
3176 let sh = (float (maxy + state.winh) /. float state.winh) in
3177 let sh = float state.winh /. sh in
3178 let sh = max sh (float conf.scrollh) in
3180 let percent =
3181 if y = state.maxy
3182 then 1.0
3183 else float y /. float maxy
3185 let position = (float state.winh -. sh) *. percent in
3187 let position =
3188 if position +. sh > float state.winh
3189 then float state.winh -. sh
3190 else position
3192 position, sh;
3195 let coe s = (s :> uioh);;
3197 class listview ~(source:lvsource) ~trusted ~modehash =
3198 object (self)
3199 val m_pan = source#getpan
3200 val m_first = source#getfirst
3201 val m_active = source#getactive
3202 val m_qsearch = source#getqsearch
3203 val m_prev_uioh = state.uioh
3205 method private elemunder y =
3206 let n = y / (fstate.fontsize+1) in
3207 if m_first + n < source#getitemcount
3208 then (
3209 if source#hasaction (m_first + n)
3210 then Some (m_first + n)
3211 else None
3213 else None
3215 method display =
3216 Gl.enable `blend;
3217 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3218 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3219 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3220 GlDraw.color (1., 1., 1.);
3221 Gl.enable `texture_2d;
3222 let fs = fstate.fontsize in
3223 let nfs = fs + 1 in
3224 let ww = fstate.wwidth in
3225 let tabw = 30.0*.ww in
3226 let itemcount = source#getitemcount in
3227 let rec loop row =
3228 if (row - m_first) > fstate.maxrows
3229 then ()
3230 else (
3231 if row >= 0 && row < itemcount
3232 then (
3233 let (s, level) = source#getitem row in
3234 let y = (row - m_first) * nfs in
3235 let x = 5.0 +. float (level + m_pan) *. ww in
3236 if row = m_active
3237 then (
3238 Gl.disable `texture_2d;
3239 GlDraw.polygon_mode `both `line;
3240 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3241 GlDraw.rect (1., float (y + 1))
3242 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3243 GlDraw.polygon_mode `both `fill;
3244 GlDraw.color (1., 1., 1.);
3245 Gl.enable `texture_2d;
3248 let drawtabularstring s =
3249 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3250 if trusted
3251 then
3252 let tabpos = try String.index s '\t' with Not_found -> -1 in
3253 if tabpos > 0
3254 then
3255 let len = String.length s - tabpos - 1 in
3256 let s1 = String.sub s 0 tabpos
3257 and s2 = String.sub s (tabpos + 1) len in
3258 let nx = drawstr x s1 in
3259 let sw = nx -. x in
3260 let x = x +. (max tabw sw) in
3261 drawstr x s2
3262 else
3263 drawstr x s
3264 else
3265 drawstr x s
3267 let _ = drawtabularstring s in
3268 loop (row+1)
3272 loop m_first;
3273 Gl.disable `blend;
3274 Gl.disable `texture_2d;
3276 method updownlevel incr =
3277 let len = source#getitemcount in
3278 let curlevel =
3279 if m_active >= 0 && m_active < len
3280 then snd (source#getitem m_active)
3281 else -1
3283 let rec flow i =
3284 if i = len then i-1 else if i = -1 then 0 else
3285 let _, l = source#getitem i in
3286 if l != curlevel then i else flow (i+incr)
3288 let active = flow m_active in
3289 let first = calcfirst m_first active in
3290 G.postRedisplay "outline updownlevel";
3291 {< m_active = active; m_first = first >}
3293 method private key1 key mask =
3294 let set1 active first qsearch =
3295 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3297 let search active pattern incr =
3298 let dosearch re =
3299 let rec loop n =
3300 if n >= 0 && n < source#getitemcount
3301 then (
3302 let s, _ = source#getitem n in
3304 (try ignore (Str.search_forward re s 0); true
3305 with Not_found -> false)
3306 then Some n
3307 else loop (n + incr)
3309 else None
3311 loop active
3314 let re = Str.regexp_case_fold pattern in
3315 dosearch re
3316 with Failure s ->
3317 state.text <- s;
3318 None
3320 let itemcount = source#getitemcount in
3321 let find start incr =
3322 let rec find i =
3323 if i = -1 || i = itemcount
3324 then -1
3325 else (
3326 if source#hasaction i
3327 then i
3328 else find (i + incr)
3331 find start
3333 let set active first =
3334 let first = bound first 0 (itemcount - fstate.maxrows) in
3335 state.text <- "";
3336 coe {< m_active = active; m_first = first >}
3338 let navigate incr =
3339 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3340 let active, first =
3341 let incr1 = if incr > 0 then 1 else -1 in
3342 if isvisible m_first m_active
3343 then
3344 let next =
3345 let next = m_active + incr in
3346 let next =
3347 if next < 0 || next >= itemcount
3348 then -1
3349 else find next incr1
3351 if next = -1 || abs (m_active - next) > fstate.maxrows
3352 then -1
3353 else next
3355 if next = -1
3356 then
3357 let first = m_first + incr in
3358 let first = bound first 0 (itemcount - 1) in
3359 let next =
3360 let next = m_active + incr in
3361 let next = bound next 0 (itemcount - 1) in
3362 find next ~-incr1
3364 let active = if next = -1 then m_active else next in
3365 active, first
3366 else
3367 let first = min next m_first in
3368 let first =
3369 if abs (next - first) > fstate.maxrows
3370 then first + incr
3371 else first
3373 next, first
3374 else
3375 let first = m_first + incr in
3376 let first = bound first 0 (itemcount - 1) in
3377 let active =
3378 let next = m_active + incr in
3379 let next = bound next 0 (itemcount - 1) in
3380 let next = find next incr1 in
3381 let active =
3382 if next = -1 || abs (m_active - first) > fstate.maxrows
3383 then (
3384 let active = if m_active = -1 then next else m_active in
3385 active
3387 else next
3389 if isvisible first active
3390 then active
3391 else -1
3393 active, first
3395 G.postRedisplay "listview navigate";
3396 set active first;
3398 match key with
3399 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3400 let incr = if key = 0x72 then -1 else 1 in
3401 let active, first =
3402 match search (m_active + incr) m_qsearch incr with
3403 | None ->
3404 state.text <- m_qsearch ^ " [not found]";
3405 m_active, m_first
3406 | Some active ->
3407 state.text <- m_qsearch;
3408 active, firstof m_first active
3410 G.postRedisplay "listview ctrl-r/s";
3411 set1 active first m_qsearch;
3413 | 0xff08 -> (* backspace *)
3414 if String.length m_qsearch = 0
3415 then coe self
3416 else (
3417 let qsearch = withoutlastutf8 m_qsearch in
3418 let len = String.length qsearch in
3419 if len = 0
3420 then (
3421 state.text <- "";
3422 G.postRedisplay "listview empty qsearch";
3423 set1 m_active m_first "";
3425 else
3426 let active, first =
3427 match search m_active qsearch ~-1 with
3428 | None ->
3429 state.text <- qsearch ^ " [not found]";
3430 m_active, m_first
3431 | Some active ->
3432 state.text <- qsearch;
3433 active, firstof m_first active
3435 G.postRedisplay "listview backspace qsearch";
3436 set1 active first qsearch
3439 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3440 let pattern = m_qsearch ^ toutf8 key in
3441 let active, first =
3442 match search m_active pattern 1 with
3443 | None ->
3444 state.text <- pattern ^ " [not found]";
3445 m_active, m_first
3446 | Some active ->
3447 state.text <- pattern;
3448 active, firstof m_first active
3450 G.postRedisplay "listview qsearch add";
3451 set1 active first pattern;
3453 | 0xff1b -> (* escape *)
3454 state.text <- "";
3455 if String.length m_qsearch = 0
3456 then (
3457 G.postRedisplay "list view escape";
3458 begin
3459 match
3460 source#exit (coe self) true m_active m_first m_pan m_qsearch
3461 with
3462 | None -> m_prev_uioh
3463 | Some uioh -> uioh
3466 else (
3467 G.postRedisplay "list view kill qsearch";
3468 source#setqsearch "";
3469 coe {< m_qsearch = "" >}
3472 | 0xff0d | 0xff8d -> (* (kp) enter *)
3473 state.text <- "";
3474 let self = {< m_qsearch = "" >} in
3475 source#setqsearch "";
3476 let opt =
3477 G.postRedisplay "listview enter";
3478 if m_active >= 0 && m_active < source#getitemcount
3479 then (
3480 source#exit (coe self) false m_active m_first m_pan "";
3482 else (
3483 source#exit (coe self) true m_active m_first m_pan "";
3486 begin match opt with
3487 | None -> m_prev_uioh
3488 | Some uioh -> uioh
3491 | 0xff9f | 0xffff -> (* (kp) delete *)
3492 coe self
3494 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3495 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3496 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3497 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3499 | 0xff53 | 0xff98 -> (* (kp) right *)
3500 state.text <- "";
3501 G.postRedisplay "listview right";
3502 coe {< m_pan = m_pan - 1 >}
3504 | 0xff51 | 0xff96 -> (* (kp) left *)
3505 state.text <- "";
3506 G.postRedisplay "listview left";
3507 coe {< m_pan = m_pan + 1 >}
3509 | 0xff50 | 0xff95 -> (* (kp) home *)
3510 let active = find 0 1 in
3511 G.postRedisplay "listview home";
3512 set active 0;
3514 | 0xff57 | 0xff9c -> (* (kp) end *)
3515 let first = max 0 (itemcount - fstate.maxrows) in
3516 let active = find (itemcount - 1) ~-1 in
3517 G.postRedisplay "listview end";
3518 set active first;
3520 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3521 coe self
3523 | _ ->
3524 dolog "listview unknown key %#x" key; coe self
3526 method key key mask =
3527 match state.mode with
3528 | Textentry te -> textentrykeyboard key mask te; coe self
3529 | _ -> self#key1 key mask
3531 method button button down x y _ =
3532 let opt =
3533 match button with
3534 | 1 when x > state.winw - conf.scrollbw ->
3535 G.postRedisplay "listview scroll";
3536 if down
3537 then
3538 let _, position, sh = self#scrollph in
3539 if y > truncate position && y < truncate (position +. sh)
3540 then (
3541 state.mstate <- Mscrolly;
3542 Some (coe self)
3544 else
3545 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3546 let first = truncate (s *. float source#getitemcount) in
3547 let first = min source#getitemcount first in
3548 Some (coe {< m_first = first; m_active = first >})
3549 else (
3550 state.mstate <- Mnone;
3551 Some (coe self);
3553 | 1 when not down ->
3554 begin match self#elemunder y with
3555 | Some n ->
3556 G.postRedisplay "listview click";
3557 source#exit
3558 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3559 | _ ->
3560 Some (coe self)
3562 | n when (n == 4 || n == 5) && not down ->
3563 let len = source#getitemcount in
3564 let first =
3565 if n = 5 && m_first + fstate.maxrows >= len
3566 then
3567 m_first
3568 else
3569 let first = m_first + (if n == 4 then -1 else 1) in
3570 bound first 0 (len - 1)
3572 G.postRedisplay "listview wheel";
3573 Some (coe {< m_first = first >})
3574 | n when (n = 6 || n = 7) && not down ->
3575 let inc = m_first + (if n = 7 then -1 else 1) in
3576 G.postRedisplay "listview hwheel";
3577 Some (coe {< m_pan = m_pan + inc >})
3578 | _ ->
3579 Some (coe self)
3581 match opt with
3582 | None -> m_prev_uioh
3583 | Some uioh -> uioh
3585 method motion _ y =
3586 match state.mstate with
3587 | Mscrolly ->
3588 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3589 let first = truncate (s *. float source#getitemcount) in
3590 let first = min source#getitemcount first in
3591 G.postRedisplay "listview motion";
3592 coe {< m_first = first; m_active = first >}
3593 | _ -> coe self
3595 method pmotion x y =
3596 if x < state.winw - conf.scrollbw
3597 then
3598 let n =
3599 match self#elemunder y with
3600 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3601 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3603 let o =
3604 if n != m_active
3605 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3606 else self
3608 coe o
3609 else (
3610 Wsi.setcursor Wsi.CURSOR_INHERIT;
3611 coe self
3614 method infochanged _ = ()
3616 method scrollpw = (0, 0.0, 0.0)
3617 method scrollph =
3618 let nfs = fstate.fontsize + 1 in
3619 let y = m_first * nfs in
3620 let itemcount = source#getitemcount in
3621 let maxi = max 0 (itemcount - fstate.maxrows) in
3622 let maxy = maxi * nfs in
3623 let p, h = scrollph y maxy in
3624 conf.scrollbw, p, h
3626 method modehash = modehash
3627 end;;
3629 class outlinelistview ~source =
3630 object (self)
3631 inherit listview
3632 ~source:(source :> lvsource)
3633 ~trusted:false
3634 ~modehash:(findkeyhash conf "outline")
3635 as super
3637 method key key mask =
3638 let calcfirst first active =
3639 if active > first
3640 then
3641 let rows = active - first in
3642 let maxrows =
3643 if String.length state.text = 0
3644 then fstate.maxrows
3645 else fstate.maxrows - 2
3647 if rows > maxrows then active - maxrows else first
3648 else active
3650 let navigate incr =
3651 let active = m_active + incr in
3652 let active = bound active 0 (source#getitemcount - 1) in
3653 let first = calcfirst m_first active in
3654 G.postRedisplay "outline navigate";
3655 coe {< m_active = active; m_first = first >}
3657 let ctrl = Wsi.withctrl mask in
3658 match key with
3659 | 110 when ctrl -> (* ctrl-n *)
3660 source#narrow m_qsearch;
3661 G.postRedisplay "outline ctrl-n";
3662 coe {< m_first = 0; m_active = 0 >}
3664 | 117 when ctrl -> (* ctrl-u *)
3665 source#denarrow;
3666 G.postRedisplay "outline ctrl-u";
3667 state.text <- "";
3668 coe {< m_first = 0; m_active = 0 >}
3670 | 108 when ctrl -> (* ctrl-l *)
3671 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3672 G.postRedisplay "outline ctrl-l";
3673 coe {< m_first = first >}
3675 | 0xff9f | 0xffff -> (* (kp) delete *)
3676 source#remove m_active;
3677 G.postRedisplay "outline delete";
3678 let active = max 0 (m_active-1) in
3679 coe {< m_first = firstof m_first active;
3680 m_active = active >}
3682 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3683 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3684 | 0xff55 | 0xff9a -> (* (kp) prior *)
3685 navigate ~-(fstate.maxrows)
3686 | 0xff56 | 0xff9b -> (* (kp) next *)
3687 navigate fstate.maxrows
3689 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3690 let o =
3691 if ctrl
3692 then (
3693 G.postRedisplay "outline ctrl right";
3694 {< m_pan = m_pan + 1 >}
3696 else self#updownlevel 1
3698 coe o
3700 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3701 let o =
3702 if ctrl
3703 then (
3704 G.postRedisplay "outline ctrl left";
3705 {< m_pan = m_pan - 1 >}
3707 else self#updownlevel ~-1
3709 coe o
3711 | 0xff50 | 0xff95 -> (* (kp) home *)
3712 G.postRedisplay "outline home";
3713 coe {< m_first = 0; m_active = 0 >}
3715 | 0xff57 | 0xff9c -> (* (kp) end *)
3716 let active = source#getitemcount - 1 in
3717 let first = max 0 (active - fstate.maxrows) in
3718 G.postRedisplay "outline end";
3719 coe {< m_active = active; m_first = first >}
3721 | _ -> super#key key mask
3724 let outlinesource usebookmarks =
3725 let empty = [||] in
3726 (object
3727 inherit lvsourcebase
3728 val mutable m_items = empty
3729 val mutable m_orig_items = empty
3730 val mutable m_prev_items = empty
3731 val mutable m_narrow_pattern = ""
3732 val mutable m_hadremovals = false
3734 method getitemcount =
3735 Array.length m_items + (if m_hadremovals then 1 else 0)
3737 method getitem n =
3738 if n == Array.length m_items && m_hadremovals
3739 then
3740 ("[Confirm removal]", 0)
3741 else
3742 let s, n, _ = m_items.(n) in
3743 (s, n)
3745 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3746 ignore (uioh, first, qsearch);
3747 let confrimremoval = m_hadremovals && active = Array.length m_items in
3748 let items =
3749 if String.length m_narrow_pattern = 0
3750 then m_orig_items
3751 else m_items
3753 if not cancel
3754 then (
3755 if not confrimremoval
3756 then(
3757 let _, _, anchor = m_items.(active) in
3758 gotoghyll (getanchory anchor);
3759 m_items <- items;
3761 else (
3762 state.bookmarks <- Array.to_list m_items;
3763 m_orig_items <- m_items;
3766 else m_items <- items;
3767 m_pan <- pan;
3768 None
3770 method hasaction _ = true
3772 method greetmsg =
3773 if Array.length m_items != Array.length m_orig_items
3774 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3775 else ""
3777 method narrow pattern =
3778 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3779 match reopt with
3780 | None -> ()
3781 | Some re ->
3782 let rec loop accu n =
3783 if n = -1
3784 then (
3785 m_narrow_pattern <- pattern;
3786 m_items <- Array.of_list accu
3788 else
3789 let (s, _, _) as o = m_items.(n) in
3790 let accu =
3791 if (try ignore (Str.search_forward re s 0); true
3792 with Not_found -> false)
3793 then o :: accu
3794 else accu
3796 loop accu (n-1)
3798 loop [] (Array.length m_items - 1)
3800 method denarrow =
3801 m_orig_items <- (
3802 if usebookmarks
3803 then Array.of_list state.bookmarks
3804 else state.outlines
3806 m_items <- m_orig_items
3808 method remove m =
3809 if usebookmarks
3810 then
3811 if m >= 0 && m < Array.length m_items
3812 then (
3813 m_hadremovals <- true;
3814 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3815 let n = if n >= m then n+1 else n in
3816 m_items.(n)
3820 method reset anchor items =
3821 m_hadremovals <- false;
3822 if m_orig_items == empty || m_prev_items != items
3823 then (
3824 m_orig_items <- items;
3825 if String.length m_narrow_pattern = 0
3826 then m_items <- items;
3828 m_prev_items <- items;
3829 let rely = getanchory anchor in
3830 let active =
3831 let rec loop n best bestd =
3832 if n = Array.length m_items
3833 then best
3834 else
3835 let (_, _, anchor) = m_items.(n) in
3836 let orely = getanchory anchor in
3837 let d = abs (orely - rely) in
3838 if d < bestd
3839 then loop (n+1) n d
3840 else loop (n+1) best bestd
3842 loop 0 ~-1 max_int
3844 m_active <- active;
3845 m_first <- firstof m_first active
3846 end)
3849 let enterselector usebookmarks =
3850 let source = outlinesource usebookmarks in
3851 fun errmsg ->
3852 let outlines =
3853 if usebookmarks
3854 then Array.of_list state.bookmarks
3855 else state.outlines
3857 if Array.length outlines = 0
3858 then (
3859 showtext ' ' errmsg;
3861 else (
3862 state.text <- source#greetmsg;
3863 Wsi.setcursor Wsi.CURSOR_INHERIT;
3864 let anchor = getanchor () in
3865 source#reset anchor outlines;
3866 state.uioh <- coe (new outlinelistview ~source);
3867 G.postRedisplay "enter selector";
3871 let enteroutlinemode =
3872 let f = enterselector false in
3873 fun ()-> f "Document has no outline";
3876 let enterbookmarkmode =
3877 let f = enterselector true in
3878 fun () -> f "Document has no bookmarks (yet)";
3881 let color_of_string s =
3882 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3883 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3887 let color_to_string (r, g, b) =
3888 let r = truncate (r *. 256.0)
3889 and g = truncate (g *. 256.0)
3890 and b = truncate (b *. 256.0) in
3891 Printf.sprintf "%d/%d/%d" r g b
3894 let irect_of_string s =
3895 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3898 let irect_to_string (x0,y0,x1,y1) =
3899 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3902 let makecheckers () =
3903 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3904 following to say:
3905 converted by Issac Trotts. July 25, 2002 *)
3906 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3907 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3908 let id = GlTex.gen_texture () in
3909 GlTex.bind_texture `texture_2d id;
3910 GlPix.store (`unpack_alignment 1);
3911 GlTex.image2d image;
3912 List.iter (GlTex.parameter ~target:`texture_2d)
3913 [ `mag_filter `nearest; `min_filter `nearest ];
3917 let setcheckers enabled =
3918 match state.texid with
3919 | None ->
3920 if enabled then state.texid <- Some (makecheckers ())
3922 | Some texid ->
3923 if not enabled
3924 then (
3925 GlTex.delete_texture texid;
3926 state.texid <- None;
3930 let int_of_string_with_suffix s =
3931 let l = String.length s in
3932 let s1, shift =
3933 if l > 1
3934 then
3935 let suffix = Char.lowercase s.[l-1] in
3936 match suffix with
3937 | 'k' -> String.sub s 0 (l-1), 10
3938 | 'm' -> String.sub s 0 (l-1), 20
3939 | 'g' -> String.sub s 0 (l-1), 30
3940 | _ -> s, 0
3941 else s, 0
3943 let n = int_of_string s1 in
3944 let m = n lsl shift in
3945 if m < 0 || m < n
3946 then raise (Failure "value too large")
3947 else m
3950 let string_with_suffix_of_int n =
3951 if n = 0
3952 then "0"
3953 else
3954 let n, s =
3955 if n land ((1 lsl 30) - 1) = 0
3956 then n lsr 30, "G"
3957 else (
3958 if n land ((1 lsl 20) - 1) = 0
3959 then n lsr 20, "M"
3960 else (
3961 if n land ((1 lsl 10) - 1) = 0
3962 then n lsr 10, "K"
3963 else n, ""
3967 let rec loop s n =
3968 let h = n mod 1000 in
3969 let n = n / 1000 in
3970 if n = 0
3971 then string_of_int h ^ s
3972 else (
3973 let s = Printf.sprintf "_%03d%s" h s in
3974 loop s n
3977 loop "" n ^ s;
3980 let defghyllscroll = (40, 8, 32);;
3981 let ghyllscroll_of_string s =
3982 let (n, a, b) as nab =
3983 if s = "default"
3984 then defghyllscroll
3985 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3987 if n <= a || n <= b || a >= b
3988 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3989 nab;
3992 let ghyllscroll_to_string ((n, a, b) as nab) =
3993 if nab = defghyllscroll
3994 then "default"
3995 else Printf.sprintf "%d,%d,%d" n a b;
3998 let describe_location () =
3999 let f (fn, _) l =
4000 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
4002 let fn, ln = List.fold_left f (-1, -1) state.layout in
4003 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4004 let percent =
4005 if maxy <= 0
4006 then 100.
4007 else (100. *. (float state.y /. float maxy))
4009 if fn = ln
4010 then
4011 Printf.sprintf "page %d of %d [%.2f%%]"
4012 (fn+1) state.pagecount percent
4013 else
4014 Printf.sprintf
4015 "pages %d-%d of %d [%.2f%%]"
4016 (fn+1) (ln+1) state.pagecount percent
4019 let setpresentationmode v =
4020 let (n, _, _) = getanchor () in
4021 let _, h = getpageyh n in
4022 let ips = if conf.presentation then calcips h else conf.interpagespace in
4023 state.anchor <- (n, 0.0, float ips);
4024 conf.presentation <- v;
4025 if conf.presentation
4026 then (
4027 if not conf.scrollbarinpm
4028 then state.scrollw <- 0;
4030 else state.scrollw <- conf.scrollbw;
4031 represent ();
4034 let enterinfomode =
4035 let btos b = if b then "\xe2\x88\x9a" else "" in
4036 let showextended = ref false in
4037 let leave mode = function
4038 | Confirm -> state.mode <- mode
4039 | Cancel -> state.mode <- mode in
4040 let src =
4041 (object
4042 val mutable m_first_time = true
4043 val mutable m_l = []
4044 val mutable m_a = [||]
4045 val mutable m_prev_uioh = nouioh
4046 val mutable m_prev_mode = View
4048 inherit lvsourcebase
4050 method reset prev_mode prev_uioh =
4051 m_a <- Array.of_list (List.rev m_l);
4052 m_l <- [];
4053 m_prev_mode <- prev_mode;
4054 m_prev_uioh <- prev_uioh;
4055 if m_first_time
4056 then (
4057 let rec loop n =
4058 if n >= Array.length m_a
4059 then ()
4060 else
4061 match m_a.(n) with
4062 | _, _, _, Action _ -> m_active <- n
4063 | _ -> loop (n+1)
4065 loop 0;
4066 m_first_time <- false;
4069 method int name get set =
4070 m_l <-
4071 (name, `int get, 1, Action (
4072 fun u ->
4073 let ondone s =
4074 try set (int_of_string s)
4075 with exn ->
4076 state.text <- Printf.sprintf "bad integer `%s': %s"
4077 s (exntos exn)
4079 state.text <- "";
4080 let te = name ^ ": ", "", None, intentry, ondone, true in
4081 state.mode <- Textentry (te, leave m_prev_mode);
4083 )) :: m_l
4085 method int_with_suffix name get set =
4086 m_l <-
4087 (name, `intws get, 1, Action (
4088 fun u ->
4089 let ondone s =
4090 try set (int_of_string_with_suffix s)
4091 with exn ->
4092 state.text <- Printf.sprintf "bad integer `%s': %s"
4093 s (exntos exn)
4095 state.text <- "";
4096 let te =
4097 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4099 state.mode <- Textentry (te, leave m_prev_mode);
4101 )) :: m_l
4103 method bool ?(offset=1) ?(btos=btos) name get set =
4104 m_l <-
4105 (name, `bool (btos, get), offset, Action (
4106 fun u ->
4107 let v = get () in
4108 set (not v);
4110 )) :: m_l
4112 method color name get set =
4113 m_l <-
4114 (name, `color get, 1, Action (
4115 fun u ->
4116 let invalid = (nan, nan, nan) in
4117 let ondone s =
4118 let c =
4119 try color_of_string s
4120 with exn ->
4121 state.text <- Printf.sprintf "bad color `%s': %s"
4122 s (exntos exn);
4123 invalid
4125 if c <> invalid
4126 then set c;
4128 let te = name ^ ": ", "", None, textentry, ondone, true in
4129 state.text <- color_to_string (get ());
4130 state.mode <- Textentry (te, leave m_prev_mode);
4132 )) :: m_l
4134 method string name get set =
4135 m_l <-
4136 (name, `string get, 1, Action (
4137 fun u ->
4138 let ondone s = set s in
4139 let te = name ^ ": ", "", None, textentry, ondone, true in
4140 state.mode <- Textentry (te, leave m_prev_mode);
4142 )) :: m_l
4144 method colorspace name get set =
4145 m_l <-
4146 (name, `string get, 1, Action (
4147 fun _ ->
4148 let source =
4149 let vals = [| "rgb"; "bgr"; "gray" |] in
4150 (object
4151 inherit lvsourcebase
4153 initializer
4154 m_active <- int_of_colorspace conf.colorspace;
4155 m_first <- 0;
4157 method getitemcount = Array.length vals
4158 method getitem n = (vals.(n), 0)
4159 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4160 ignore (uioh, first, pan, qsearch);
4161 if not cancel then set active;
4162 None
4163 method hasaction _ = true
4164 end)
4166 state.text <- "";
4167 let modehash = findkeyhash conf "info" in
4168 coe (new listview ~source ~trusted:true ~modehash)
4169 )) :: m_l
4171 method caption s offset =
4172 m_l <- (s, `empty, offset, Noaction) :: m_l
4174 method caption2 s f offset =
4175 m_l <- (s, `string f, offset, Noaction) :: m_l
4177 method getitemcount = Array.length m_a
4179 method getitem n =
4180 let tostr = function
4181 | `int f -> string_of_int (f ())
4182 | `intws f -> string_with_suffix_of_int (f ())
4183 | `string f -> f ()
4184 | `color f -> color_to_string (f ())
4185 | `bool (btos, f) -> btos (f ())
4186 | `empty -> ""
4188 let name, t, offset, _ = m_a.(n) in
4189 ((let s = tostr t in
4190 if String.length s > 0
4191 then Printf.sprintf "%s\t%s" name s
4192 else name),
4193 offset)
4195 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4196 let uiohopt =
4197 if not cancel
4198 then (
4199 m_qsearch <- qsearch;
4200 let uioh =
4201 match m_a.(active) with
4202 | _, _, _, Action f -> f uioh
4203 | _ -> uioh
4205 Some uioh
4207 else None
4209 m_active <- active;
4210 m_first <- first;
4211 m_pan <- pan;
4212 uiohopt
4214 method hasaction n =
4215 match m_a.(n) with
4216 | _, _, _, Action _ -> true
4217 | _ -> false
4218 end)
4220 let rec fillsrc prevmode prevuioh =
4221 let sep () = src#caption "" 0 in
4222 let colorp name get set =
4223 src#string name
4224 (fun () -> color_to_string (get ()))
4225 (fun v ->
4227 let c = color_of_string v in
4228 set c
4229 with exn ->
4230 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4233 let oldmode = state.mode in
4234 let birdseye = isbirdseye state.mode in
4236 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4238 src#bool "presentation mode"
4239 (fun () -> conf.presentation)
4240 (fun v -> setpresentationmode v);
4242 src#bool "ignore case in searches"
4243 (fun () -> conf.icase)
4244 (fun v -> conf.icase <- v);
4246 src#bool "preload"
4247 (fun () -> conf.preload)
4248 (fun v -> conf.preload <- v);
4250 src#bool "highlight links"
4251 (fun () -> conf.hlinks)
4252 (fun v -> conf.hlinks <- v);
4254 src#bool "under info"
4255 (fun () -> conf.underinfo)
4256 (fun v -> conf.underinfo <- v);
4258 src#bool "persistent bookmarks"
4259 (fun () -> conf.savebmarks)
4260 (fun v -> conf.savebmarks <- v);
4262 src#bool "proportional display"
4263 (fun () -> conf.proportional)
4264 (fun v -> reqlayout conf.angle v);
4266 src#bool "trim margins"
4267 (fun () -> conf.trimmargins)
4268 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4270 src#bool "persistent location"
4271 (fun () -> conf.jumpback)
4272 (fun v -> conf.jumpback <- v);
4274 sep ();
4275 src#int "inter-page space"
4276 (fun () -> conf.interpagespace)
4277 (fun n ->
4278 conf.interpagespace <- n;
4279 docolumns conf.columns;
4280 let pageno, py =
4281 match state.layout with
4282 | [] -> 0, 0
4283 | l :: _ ->
4284 l.pageno, l.pagey
4286 state.maxy <- calcheight ();
4287 let y = getpagey pageno in
4288 gotoy (y + py)
4291 src#int "page bias"
4292 (fun () -> conf.pagebias)
4293 (fun v -> conf.pagebias <- v);
4295 src#int "scroll step"
4296 (fun () -> conf.scrollstep)
4297 (fun n -> conf.scrollstep <- n);
4299 src#int "horizontal scroll step"
4300 (fun () -> conf.hscrollstep)
4301 (fun v -> conf.hscrollstep <- v);
4303 src#int "auto scroll step"
4304 (fun () ->
4305 match state.autoscroll with
4306 | Some step -> step
4307 | _ -> conf.autoscrollstep)
4308 (fun n ->
4309 if state.autoscroll <> None
4310 then state.autoscroll <- Some n;
4311 conf.autoscrollstep <- n);
4313 src#int "zoom"
4314 (fun () -> truncate (conf.zoom *. 100.))
4315 (fun v -> setzoom ((float v) /. 100.));
4317 src#int "rotation"
4318 (fun () -> conf.angle)
4319 (fun v -> reqlayout v conf.proportional);
4321 src#int "scroll bar width"
4322 (fun () -> state.scrollw)
4323 (fun v ->
4324 state.scrollw <- v;
4325 conf.scrollbw <- v;
4326 reshape state.winw state.winh;
4329 src#int "scroll handle height"
4330 (fun () -> conf.scrollh)
4331 (fun v -> conf.scrollh <- v;);
4333 src#int "thumbnail width"
4334 (fun () -> conf.thumbw)
4335 (fun v ->
4336 conf.thumbw <- min 4096 v;
4337 match oldmode with
4338 | Birdseye beye ->
4339 leavebirdseye beye false;
4340 enterbirdseye ()
4341 | _ -> ()
4344 let mode = state.mode in
4345 src#string "columns"
4346 (fun () ->
4347 match conf.columns with
4348 | Csingle _ -> "1"
4349 | Cmulti (multi, _) -> multicolumns_to_string multi
4350 | Csplit (count, _) -> "-" ^ string_of_int count
4352 (fun v ->
4353 let n, a, b = multicolumns_of_string v in
4354 setcolumns mode n a b);
4356 sep ();
4357 src#caption "Presentation mode" 0;
4358 src#bool "scrollbar visible"
4359 (fun () -> conf.scrollbarinpm)
4360 (fun v ->
4361 if v != conf.scrollbarinpm
4362 then (
4363 conf.scrollbarinpm <- v;
4364 if conf.presentation
4365 then (
4366 state.scrollw <- if v then conf.scrollbw else 0;
4367 reshape state.winw state.winh;
4372 sep ();
4373 src#caption "Pixmap cache" 0;
4374 src#int_with_suffix "size (advisory)"
4375 (fun () -> conf.memlimit)
4376 (fun v -> conf.memlimit <- v);
4378 src#caption2 "used"
4379 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4380 (string_with_suffix_of_int state.memused)
4381 (Hashtbl.length state.tilemap)) 1;
4383 sep ();
4384 src#caption "Layout" 0;
4385 src#caption2 "Dimension"
4386 (fun () ->
4387 Printf.sprintf "%dx%d (virtual %dx%d)"
4388 state.winw state.winh
4389 state.w state.maxy)
4391 if conf.debug
4392 then
4393 src#caption2 "Position" (fun () ->
4394 Printf.sprintf "%dx%d" state.x state.y
4396 else
4397 src#caption2 "Visible" (fun () -> describe_location ()) 1
4400 sep ();
4401 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4402 "Save these parameters as global defaults at exit"
4403 (fun () -> conf.bedefault)
4404 (fun v -> conf.bedefault <- v)
4407 sep ();
4408 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4409 src#bool ~offset:0 ~btos "Extended parameters"
4410 (fun () -> !showextended)
4411 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4412 if !showextended
4413 then (
4414 src#bool "checkers"
4415 (fun () -> conf.checkers)
4416 (fun v -> conf.checkers <- v; setcheckers v);
4417 src#bool "update cursor"
4418 (fun () -> conf.updatecurs)
4419 (fun v -> conf.updatecurs <- v);
4420 src#bool "verbose"
4421 (fun () -> conf.verbose)
4422 (fun v -> conf.verbose <- v);
4423 src#bool "invert colors"
4424 (fun () -> conf.invert)
4425 (fun v -> conf.invert <- v);
4426 src#bool "max fit"
4427 (fun () -> conf.maxhfit)
4428 (fun v -> conf.maxhfit <- v);
4429 src#bool "redirect stderr"
4430 (fun () -> conf.redirectstderr)
4431 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4432 src#string "uri launcher"
4433 (fun () -> conf.urilauncher)
4434 (fun v -> conf.urilauncher <- v);
4435 src#string "path launcher"
4436 (fun () -> conf.pathlauncher)
4437 (fun v -> conf.pathlauncher <- v);
4438 src#string "tile size"
4439 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4440 (fun v ->
4442 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4443 conf.tilew <- max 64 w;
4444 conf.tileh <- max 64 h;
4445 flushtiles ();
4446 with exn ->
4447 state.text <- Printf.sprintf "bad tile size `%s': %s"
4448 v (exntos exn)
4450 src#int "texture count"
4451 (fun () -> conf.texcount)
4452 (fun v ->
4453 if realloctexts v
4454 then conf.texcount <- v
4455 else showtext '!' " Failed to set texture count please retry later"
4457 src#int "slice height"
4458 (fun () -> conf.sliceheight)
4459 (fun v ->
4460 conf.sliceheight <- v;
4461 wcmd "sliceh %d" conf.sliceheight;
4463 src#int "anti-aliasing level"
4464 (fun () -> conf.aalevel)
4465 (fun v ->
4466 conf.aalevel <- bound v 0 8;
4467 state.anchor <- getanchor ();
4468 opendoc state.path state.password;
4470 src#string "page scroll scaling factor"
4471 (fun () -> string_of_float conf.pgscale)
4472 (fun v ->
4474 let s = float_of_string v in
4475 conf.pgscale <- s
4476 with exn ->
4477 state.text <- Printf.sprintf
4478 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4481 src#int "ui font size"
4482 (fun () -> fstate.fontsize)
4483 (fun v -> setfontsize (bound v 5 100));
4484 src#int "hint font size"
4485 (fun () -> conf.hfsize)
4486 (fun v -> conf.hfsize <- bound v 5 100);
4487 colorp "background color"
4488 (fun () -> conf.bgcolor)
4489 (fun v -> conf.bgcolor <- v);
4490 src#bool "crop hack"
4491 (fun () -> conf.crophack)
4492 (fun v -> conf.crophack <- v);
4493 src#string "trim fuzz"
4494 (fun () -> irect_to_string conf.trimfuzz)
4495 (fun v ->
4497 conf.trimfuzz <- irect_of_string v;
4498 if conf.trimmargins
4499 then settrim true conf.trimfuzz;
4500 with exn ->
4501 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4503 src#string "throttle"
4504 (fun () ->
4505 match conf.maxwait with
4506 | None -> "show place holder if page is not ready"
4507 | Some time ->
4508 if time = infinity
4509 then "wait for page to fully render"
4510 else
4511 "wait " ^ string_of_float time
4512 ^ " seconds before showing placeholder"
4514 (fun v ->
4516 let f = float_of_string v in
4517 if f <= 0.0
4518 then conf.maxwait <- None
4519 else conf.maxwait <- Some f
4520 with exn ->
4521 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4523 src#string "ghyll scroll"
4524 (fun () ->
4525 match conf.ghyllscroll with
4526 | None -> ""
4527 | Some nab -> ghyllscroll_to_string nab
4529 (fun v ->
4531 let gs =
4532 if String.length v = 0
4533 then None
4534 else Some (ghyllscroll_of_string v)
4536 conf.ghyllscroll <- gs
4537 with exn ->
4538 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4540 src#string "selection command"
4541 (fun () -> conf.selcmd)
4542 (fun v -> conf.selcmd <- v);
4543 src#string "synctex command"
4544 (fun () -> conf.stcmd)
4545 (fun v -> conf.stcmd <- v);
4546 src#colorspace "color space"
4547 (fun () -> colorspace_to_string conf.colorspace)
4548 (fun v ->
4549 conf.colorspace <- colorspace_of_int v;
4550 wcmd "cs %d" v;
4551 load state.layout;
4553 if pbousable ()
4554 then
4555 src#bool "use PBO"
4556 (fun () -> conf.usepbo)
4557 (fun v -> conf.usepbo <- v);
4558 src#bool "mouse wheel scrolls pages"
4559 (fun () -> conf.wheelbypage)
4560 (fun v -> conf.wheelbypage <- v);
4563 sep ();
4564 src#caption "Document" 0;
4565 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4566 src#caption2 "Pages"
4567 (fun () -> string_of_int state.pagecount) 1;
4568 src#caption2 "Dimensions"
4569 (fun () -> string_of_int (List.length state.pdims)) 1;
4570 if conf.trimmargins
4571 then (
4572 sep ();
4573 src#caption "Trimmed margins" 0;
4574 src#caption2 "Dimensions"
4575 (fun () -> string_of_int (List.length state.pdims)) 1;
4578 sep ();
4579 src#caption "OpenGL" 0;
4580 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4581 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4582 src#reset prevmode prevuioh;
4584 fun () ->
4585 state.text <- "";
4586 let prevmode = state.mode
4587 and prevuioh = state.uioh in
4588 fillsrc prevmode prevuioh;
4589 let source = (src :> lvsource) in
4590 let modehash = findkeyhash conf "info" in
4591 state.uioh <- coe (object (self)
4592 inherit listview ~source ~trusted:true ~modehash as super
4593 val mutable m_prevmemused = 0
4594 method infochanged = function
4595 | Memused ->
4596 if m_prevmemused != state.memused
4597 then (
4598 m_prevmemused <- state.memused;
4599 G.postRedisplay "memusedchanged";
4601 | Pdim -> G.postRedisplay "pdimchanged"
4602 | Docinfo -> fillsrc prevmode prevuioh
4604 method key key mask =
4605 if not (Wsi.withctrl mask)
4606 then
4607 match key with
4608 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4609 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4610 | _ -> super#key key mask
4611 else super#key key mask
4612 end);
4613 G.postRedisplay "info";
4616 let enterhelpmode =
4617 let source =
4618 (object
4619 inherit lvsourcebase
4620 method getitemcount = Array.length state.help
4621 method getitem n =
4622 let s, l, _ = state.help.(n) in
4623 (s, l)
4625 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4626 let optuioh =
4627 if not cancel
4628 then (
4629 m_qsearch <- qsearch;
4630 match state.help.(active) with
4631 | _, _, Action f -> Some (f uioh)
4632 | _ -> Some (uioh)
4634 else None
4636 m_active <- active;
4637 m_first <- first;
4638 m_pan <- pan;
4639 optuioh
4641 method hasaction n =
4642 match state.help.(n) with
4643 | _, _, Action _ -> true
4644 | _ -> false
4646 initializer
4647 m_active <- -1
4648 end)
4649 in fun () ->
4650 let modehash = findkeyhash conf "help" in
4651 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4652 G.postRedisplay "help";
4655 let entermsgsmode =
4656 let msgsource =
4657 let re = Str.regexp "[\r\n]" in
4658 (object
4659 inherit lvsourcebase
4660 val mutable m_items = [||]
4662 method getitemcount = 1 + Array.length m_items
4664 method getitem n =
4665 if n = 0
4666 then "[Clear]", 0
4667 else m_items.(n-1), 0
4669 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4670 ignore uioh;
4671 if not cancel
4672 then (
4673 if active = 0
4674 then Buffer.clear state.errmsgs;
4675 m_qsearch <- qsearch;
4677 m_active <- active;
4678 m_first <- first;
4679 m_pan <- pan;
4680 None
4682 method hasaction n =
4683 n = 0
4685 method reset =
4686 state.newerrmsgs <- false;
4687 let l = Str.split re (Buffer.contents state.errmsgs) in
4688 m_items <- Array.of_list l
4690 initializer
4691 m_active <- 0
4692 end)
4693 in fun () ->
4694 state.text <- "";
4695 msgsource#reset;
4696 let source = (msgsource :> lvsource) in
4697 let modehash = findkeyhash conf "listview" in
4698 state.uioh <- coe (object
4699 inherit listview ~source ~trusted:false ~modehash as super
4700 method display =
4701 if state.newerrmsgs
4702 then msgsource#reset;
4703 super#display
4704 end);
4705 G.postRedisplay "msgs";
4708 let quickbookmark ?title () =
4709 match state.layout with
4710 | [] -> ()
4711 | l :: _ ->
4712 let title =
4713 match title with
4714 | None ->
4715 let sec = Unix.gettimeofday () in
4716 let tm = Unix.localtime sec in
4717 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4718 (l.pageno+1)
4719 tm.Unix.tm_mday
4720 tm.Unix.tm_mon
4721 (tm.Unix.tm_year + 1900)
4722 tm.Unix.tm_hour
4723 tm.Unix.tm_min
4724 | Some title -> title
4726 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4729 let doreshape w h =
4730 Wsi.reshape w h;
4733 let setautoscrollspeed step goingdown =
4734 let incr = max 1 ((abs step) / 2) in
4735 let incr = if goingdown then incr else -incr in
4736 let astep = step + incr in
4737 state.autoscroll <- Some astep;
4740 let gotounder = function
4741 | Ulinkgoto (pageno, top) ->
4742 if pageno >= 0
4743 then (
4744 addnav ();
4745 gotopage1 pageno top;
4748 | Ulinkuri s ->
4749 gotouri s
4751 | Uremote (filename, pageno) ->
4752 let path =
4753 if Sys.file_exists filename
4754 then filename
4755 else
4756 let dir = Filename.dirname state.path in
4757 let path = Filename.concat dir filename in
4758 if Sys.file_exists path
4759 then path
4760 else ""
4762 if String.length path > 0
4763 then (
4764 let anchor = getanchor () in
4765 let ranchor = state.path, state.password, anchor in
4766 state.anchor <- (pageno, 0.0, 0.0);
4767 state.ranchors <- ranchor :: state.ranchors;
4768 opendoc path "";
4770 else showtext '!' ("Could not find " ^ filename)
4772 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4775 let canpan () =
4776 match conf.columns with
4777 | Csplit _ -> true
4778 | _ -> conf.zoom > 1.0
4781 let existsinrow pageno (columns, coverA, coverB) p =
4782 let last = ((pageno - coverA) mod columns) + columns in
4783 let rec any = function
4784 | [] -> false
4785 | l :: rest ->
4786 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4787 then p l
4788 else (
4789 if not (p l)
4790 then (if l.pageno = last then false else any rest)
4791 else true
4794 any state.layout
4797 let nextpage () =
4798 match state.layout with
4799 | [] -> ()
4800 | l :: rest ->
4801 match conf.columns with
4802 | Csingle _ ->
4803 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4804 then
4805 let y = clamp (pgscale state.winh) in
4806 gotoghyll y
4807 else
4808 let pageno = min (l.pageno+1) (state.pagecount-1) in
4809 gotoghyll (getpagey pageno)
4810 | Cmulti ((c, _, _) as cl, _) ->
4811 if conf.presentation
4812 && (existsinrow l.pageno cl
4813 (fun l -> l.pageh > l.pagey + l.pagevh))
4814 then
4815 let y = clamp (pgscale state.winh) in
4816 gotoghyll y
4817 else
4818 let pageno = min (l.pageno+c) (state.pagecount-1) in
4819 gotoghyll (getpagey pageno)
4820 | Csplit (n, _) ->
4821 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4822 then
4823 let pagey, pageh = getpageyh l.pageno in
4824 let pagey = pagey + pageh * l.pagecol in
4825 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4826 gotoghyll (pagey + pageh + ips)
4829 let prevpage () =
4830 match state.layout with
4831 | [] -> ()
4832 | l :: _ ->
4833 match conf.columns with
4834 | Csingle _ ->
4835 if conf.presentation && l.pagey != 0
4836 then
4837 gotoghyll (clamp (pgscale ~-(state.winh)))
4838 else
4839 let pageno = max 0 (l.pageno-1) in
4840 gotoghyll (getpagey pageno)
4841 | Cmulti ((c, _, coverB) as cl, _) ->
4842 if conf.presentation &&
4843 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4844 then
4845 gotoghyll (clamp (pgscale ~-(state.winh)))
4846 else
4847 let decr =
4848 if l.pageno = state.pagecount - coverB
4849 then 1
4850 else c
4852 let pageno = max 0 (l.pageno-decr) in
4853 gotoghyll (getpagey pageno)
4854 | Csplit (n, _) ->
4855 let y =
4856 if l.pagecol = 0
4857 then
4858 if l.pageno = 0
4859 then l.pagey
4860 else
4861 let pageno = max 0 (l.pageno-1) in
4862 let pagey, pageh = getpageyh pageno in
4863 pagey + (n-1)*pageh
4864 else
4865 let pagey, pageh = getpageyh l.pageno in
4866 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4868 gotoghyll y
4871 let viewkeyboard key mask =
4872 let enttext te =
4873 let mode = state.mode in
4874 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4875 state.text <- "";
4876 enttext ();
4877 G.postRedisplay "view:enttext"
4879 let ctrl = Wsi.withctrl mask in
4880 let key =
4881 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4883 match key with
4884 | 81 -> (* Q *)
4885 exit 0
4887 | 0xff63 -> (* insert *)
4888 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4889 then (
4890 state.mode <- LinkNav (Ltgendir 0);
4891 gotoy state.y;
4893 else showtext '!' "Keyboard link navigation does not work under rotation"
4895 | 0xff1b | 113 -> (* escape / q *)
4896 begin match state.mstate with
4897 | Mzoomrect _ ->
4898 state.mstate <- Mnone;
4899 Wsi.setcursor Wsi.CURSOR_INHERIT;
4900 G.postRedisplay "kill zoom rect";
4901 | _ ->
4902 begin match state.mode with
4903 | LinkNav _ ->
4904 state.mode <- View;
4905 G.postRedisplay "esc leave linknav"
4906 | _ ->
4907 match state.ranchors with
4908 | [] -> raise Quit
4909 | (path, password, anchor) :: rest ->
4910 state.ranchors <- rest;
4911 state.anchor <- anchor;
4912 opendoc path password
4913 end;
4914 end;
4916 | 0xff08 -> (* backspace *)
4917 gotoghyll (getnav ~-1)
4919 | 111 -> (* o *)
4920 enteroutlinemode ()
4922 | 117 -> (* u *)
4923 state.rects <- [];
4924 state.text <- "";
4925 G.postRedisplay "dehighlight";
4927 | 47 | 63 -> (* / ? *)
4928 let ondone isforw s =
4929 cbput state.hists.pat s;
4930 state.searchpattern <- s;
4931 search s isforw
4933 let s = String.create 1 in
4934 s.[0] <- Char.chr key;
4935 enttext (s, "", Some (onhist state.hists.pat),
4936 textentry, ondone (key = 47), true)
4938 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4939 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4940 setzoom (conf.zoom +. incr)
4942 | 43 | 0xffab -> (* + *)
4943 let ondone s =
4944 let n =
4945 try int_of_string s with exc ->
4946 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4947 max_int
4949 if n != max_int
4950 then (
4951 conf.pagebias <- n;
4952 state.text <- "page bias is now " ^ string_of_int n;
4955 enttext ("page bias: ", "", None, intentry, ondone, true)
4957 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4958 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4959 setzoom (max 0.01 (conf.zoom -. decr))
4961 | 45 | 0xffad -> (* - *)
4962 let ondone msg = state.text <- msg in
4963 enttext (
4964 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4965 optentry state.mode, ondone, true
4968 | 48 when ctrl -> (* ctrl-0 *)
4969 setzoom 1.0
4971 | 49 when ctrl -> (* ctrl-1 *)
4972 let cols =
4973 match conf.columns with
4974 | Csingle _ | Cmulti _ -> 1
4975 | Csplit (n, _) -> n
4977 let zoom = zoomforh state.winw state.winh state.scrollw cols in
4978 if zoom < 1.0
4979 then setzoom zoom
4981 | 0xffc6 -> (* f9 *)
4982 togglebirdseye ()
4984 | 57 when ctrl -> (* ctrl-9 *)
4985 togglebirdseye ()
4987 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4988 when not ctrl -> (* 0..9 *)
4989 let ondone s =
4990 let n =
4991 try int_of_string s with exc ->
4992 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4995 if n >= 0
4996 then (
4997 addnav ();
4998 cbput state.hists.pag (string_of_int n);
4999 gotopage1 (n + conf.pagebias - 1) 0;
5002 let pageentry text key =
5003 match Char.unsafe_chr key with
5004 | 'g' -> TEdone text
5005 | _ -> intentry text key
5007 let text = "x" in text.[0] <- Char.chr key;
5008 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5010 | 98 -> (* b *)
5011 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5012 reshape state.winw state.winh;
5014 | 108 -> (* l *)
5015 conf.hlinks <- not conf.hlinks;
5016 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5017 G.postRedisplay "toggle highlightlinks";
5019 | 70 -> (* F *)
5020 state.glinks <- true;
5021 let mode = state.mode in
5022 state.mode <- Textentry (
5023 (":", "", None, linknentry, linkndone gotounder, false),
5024 (fun _ ->
5025 state.glinks <- false;
5026 state.mode <- mode)
5028 state.text <- "";
5029 G.postRedisplay "view:linkent(F)"
5031 | 121 -> (* y *)
5032 state.glinks <- true;
5033 let mode = state.mode in
5034 state.mode <- Textentry (
5035 (":", "", None, linknentry, linkndone (fun under ->
5036 match Ne.pipe () with
5037 | Ne.Exn exn ->
5038 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5039 | Ne.Res (r, w) ->
5040 let popened =
5041 try popen conf.selcmd [r, 0; w, -1]; true
5042 with exn ->
5043 showtext '!'
5044 (Printf.sprintf "failed to execute %s: %s"
5045 conf.selcmd (exntos exn));
5046 false
5048 let clo cap fd =
5049 Ne.clo fd (fun msg ->
5050 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5053 let s = undertext under in
5054 if popened
5055 then
5056 (try
5057 let l = String.length s in
5058 let n = tempfailureretry (Unix.write w s 0) l in
5059 if n != l
5060 then
5061 showtext '!'
5062 (Printf.sprintf
5063 "failed to write %d characters to sel pipe, wrote %d"
5066 with exn ->
5067 showtext '!'
5068 (Printf.sprintf "failed to write to sel pipe: %s"
5069 (exntos exn)
5072 else dolog "%s" s;
5073 clo "pipe/r" r;
5074 clo "pipe/w" w;
5075 ), false
5077 fun _ ->
5078 state.glinks <- false;
5079 state.mode <- mode
5081 state.text <- "";
5082 G.postRedisplay "view:linkent"
5084 | 97 -> (* a *)
5085 begin match state.autoscroll with
5086 | Some step ->
5087 conf.autoscrollstep <- step;
5088 state.autoscroll <- None
5089 | None ->
5090 if conf.autoscrollstep = 0
5091 then state.autoscroll <- Some 1
5092 else state.autoscroll <- Some conf.autoscrollstep
5095 | 112 when ctrl -> (* ctrl-p *)
5096 launchpath ()
5098 | 80 -> (* P *)
5099 setpresentationmode (not conf.presentation);
5100 showtext ' ' ("presentation mode " ^
5101 if conf.presentation then "on" else "off");
5103 | 102 -> (* f *)
5104 if List.mem Wsi.Fullscreen state.winstate
5105 then doreshape conf.cwinw conf.cwinh
5106 else Wsi.fullscreen ()
5108 | 112 | 78 -> (* p|N *)
5109 search state.searchpattern false
5111 | 110 | 0xffc0 -> (* n|F3 *)
5112 search state.searchpattern true
5114 | 116 -> (* t *)
5115 begin match state.layout with
5116 | [] -> ()
5117 | l :: _ ->
5118 gotoghyll (getpagey l.pageno)
5121 | 32 -> (* space *)
5122 nextpage ()
5124 | 0xff9f | 0xffff -> (* delete *)
5125 prevpage ()
5127 | 61 -> (* = *)
5128 showtext ' ' (describe_location ());
5130 | 119 -> (* w *)
5131 begin match state.layout with
5132 | [] -> ()
5133 | l :: _ ->
5134 doreshape (l.pagew + state.scrollw) l.pageh;
5135 G.postRedisplay "w"
5138 | 39 -> (* ' *)
5139 enterbookmarkmode ()
5141 | 104 | 0xffbe -> (* h|F1 *)
5142 enterhelpmode ()
5144 | 105 -> (* i *)
5145 enterinfomode ()
5147 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5148 entermsgsmode ()
5150 | 109 -> (* m *)
5151 let ondone s =
5152 match state.layout with
5153 | l :: _ ->
5154 if String.length s > 0
5155 then
5156 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5157 | _ -> ()
5159 enttext ("bookmark: ", "", None, textentry, ondone, true)
5161 | 126 -> (* ~ *)
5162 quickbookmark ();
5163 showtext ' ' "Quick bookmark added";
5165 | 122 -> (* z *)
5166 begin match state.layout with
5167 | l :: _ ->
5168 let rect = getpdimrect l.pagedimno in
5169 let w, h =
5170 if conf.crophack
5171 then
5172 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5173 truncate (1.2 *. (rect.(3) -. rect.(0))))
5174 else
5175 (truncate (rect.(1) -. rect.(0)),
5176 truncate (rect.(3) -. rect.(0)))
5178 let w = truncate ((float w)*.conf.zoom)
5179 and h = truncate ((float h)*.conf.zoom) in
5180 if w != 0 && h != 0
5181 then (
5182 state.anchor <- getanchor ();
5183 doreshape (w + state.scrollw) (h + conf.interpagespace)
5185 G.postRedisplay "z";
5187 | [] -> ()
5190 | 50 when ctrl -> (* ctrl-2 *)
5191 let maxw = getmaxw () in
5192 if maxw > 0.0
5193 then setzoom (maxw /. float state.winw)
5195 | 60 | 62 -> (* < > *)
5196 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5198 | 91 | 93 -> (* [ ] *)
5199 conf.colorscale <-
5200 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5202 G.postRedisplay "brightness";
5204 | 99 when state.mode = View -> (* c *)
5205 let (c, a, b), z =
5206 match state.prevcolumns with
5207 | None -> (1, 0, 0), 1.0
5208 | Some (columns, z) ->
5209 let cab =
5210 match columns with
5211 | Csplit (c, _) -> -c, 0, 0
5212 | Cmulti ((c, a, b), _) -> c, a, b
5213 | Csingle _ -> 1, 0, 0
5215 cab, z
5217 setcolumns View c a b;
5218 setzoom z;
5220 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5221 setzoom state.prevzoom
5223 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5224 begin match state.autoscroll with
5225 | None ->
5226 begin match state.mode with
5227 | Birdseye beye -> upbirdseye 1 beye
5228 | _ ->
5229 if ctrl
5230 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5231 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5233 | Some n ->
5234 setautoscrollspeed n false
5237 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5238 begin match state.autoscroll with
5239 | None ->
5240 begin match state.mode with
5241 | Birdseye beye -> downbirdseye 1 beye
5242 | _ ->
5243 if ctrl
5244 then gotoy_and_clear_text (clamp (state.winh/2))
5245 else gotoy_and_clear_text (clamp conf.scrollstep)
5247 | Some n ->
5248 setautoscrollspeed n true
5251 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5252 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5253 if canpan ()
5254 then
5255 let dx =
5256 if ctrl
5257 then state.winw / 2
5258 else conf.hscrollstep
5260 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5261 state.x <- state.x + dx;
5262 gotoy_and_clear_text state.y
5263 else (
5264 state.text <- "";
5265 G.postRedisplay "lef/right"
5268 | 0xff55 | 0xff9a -> (* (kp) prior *)
5269 let y =
5270 if ctrl
5271 then
5272 match state.layout with
5273 | [] -> state.y
5274 | l :: _ -> state.y - l.pagey
5275 else
5276 clamp (pgscale (-state.winh))
5278 gotoghyll y
5280 | 0xff56 | 0xff9b -> (* (kp) next *)
5281 let y =
5282 if ctrl
5283 then
5284 match List.rev state.layout with
5285 | [] -> state.y
5286 | l :: _ -> getpagey l.pageno
5287 else
5288 clamp (pgscale state.winh)
5290 gotoghyll y
5292 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5293 gotoghyll 0
5294 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5295 gotoghyll (clamp state.maxy)
5297 | 0xff53 | 0xff98
5298 when Wsi.withalt mask -> (* alt-(kp) right *)
5299 gotoghyll (getnav 1)
5300 | 0xff51 | 0xff96
5301 when Wsi.withalt mask -> (* alt-(kp) left *)
5302 gotoghyll (getnav ~-1)
5304 | 114 -> (* r *)
5305 reload ()
5307 | 118 when conf.debug -> (* v *)
5308 state.rects <- [];
5309 List.iter (fun l ->
5310 match getopaque l.pageno with
5311 | None -> ()
5312 | Some opaque ->
5313 let x0, y0, x1, y1 = pagebbox opaque in
5314 let a,b = float x0, float y0 in
5315 let c,d = float x1, float y0 in
5316 let e,f = float x1, float y1 in
5317 let h,j = float x0, float y1 in
5318 let rect = (a,b,c,d,e,f,h,j) in
5319 debugrect rect;
5320 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5321 ) state.layout;
5322 G.postRedisplay "v";
5324 | _ ->
5325 vlog "huh? %s" (Wsi.keyname key)
5328 let linknavkeyboard key mask linknav =
5329 let getpage pageno =
5330 let rec loop = function
5331 | [] -> None
5332 | l :: _ when l.pageno = pageno -> Some l
5333 | _ :: rest -> loop rest
5334 in loop state.layout
5336 let doexact (pageno, n) =
5337 match getopaque pageno, getpage pageno with
5338 | Some opaque, Some l ->
5339 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5340 then
5341 let under = getlink opaque n in
5342 G.postRedisplay "link gotounder";
5343 gotounder under;
5344 state.mode <- View;
5345 else
5346 let opt, dir =
5347 match key with
5348 | 0xff50 -> (* home *)
5349 Some (findlink opaque LDfirst), -1
5351 | 0xff57 -> (* end *)
5352 Some (findlink opaque LDlast), 1
5354 | 0xff51 -> (* left *)
5355 Some (findlink opaque (LDleft n)), -1
5357 | 0xff53 -> (* right *)
5358 Some (findlink opaque (LDright n)), 1
5360 | 0xff52 -> (* up *)
5361 Some (findlink opaque (LDup n)), -1
5363 | 0xff54 -> (* down *)
5364 Some (findlink opaque (LDdown n)), 1
5366 | _ -> None, 0
5368 let pwl l dir =
5369 begin match findpwl l.pageno dir with
5370 | Pwlnotfound -> ()
5371 | Pwl pageno ->
5372 let notfound dir =
5373 state.mode <- LinkNav (Ltgendir dir);
5374 let y, h = getpageyh pageno in
5375 let y =
5376 if dir < 0
5377 then y + h - state.winh
5378 else y
5380 gotoy y
5382 begin match getopaque pageno, getpage pageno with
5383 | Some opaque, Some _ ->
5384 let link =
5385 let ld = if dir > 0 then LDfirst else LDlast in
5386 findlink opaque ld
5388 begin match link with
5389 | Lfound m ->
5390 showlinktype (getlink opaque m);
5391 state.mode <- LinkNav (Ltexact (pageno, m));
5392 G.postRedisplay "linknav jpage";
5393 | _ -> notfound dir
5394 end;
5395 | _ -> notfound dir
5396 end;
5397 end;
5399 begin match opt with
5400 | Some Lnotfound -> pwl l dir;
5401 | Some (Lfound m) ->
5402 if m = n
5403 then pwl l dir
5404 else (
5405 let _, y0, _, y1 = getlinkrect opaque m in
5406 if y0 < l.pagey
5407 then gotopage1 l.pageno y0
5408 else (
5409 let d = fstate.fontsize + 1 in
5410 if y1 - l.pagey > l.pagevh - d
5411 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5412 else G.postRedisplay "linknav";
5414 showlinktype (getlink opaque m);
5415 state.mode <- LinkNav (Ltexact (l.pageno, m));
5418 | None -> viewkeyboard key mask
5419 end;
5420 | _ -> viewkeyboard key mask
5422 if key = 0xff63
5423 then (
5424 state.mode <- View;
5425 G.postRedisplay "leave linknav"
5427 else
5428 match linknav with
5429 | Ltgendir _ -> viewkeyboard key mask
5430 | Ltexact exact -> doexact exact
5433 let keyboard key mask =
5434 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5435 then wcmd "interrupt"
5436 else state.uioh <- state.uioh#key key mask
5439 let birdseyekeyboard key mask
5440 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5441 let incr =
5442 match conf.columns with
5443 | Csingle _ -> 1
5444 | Cmulti ((c, _, _), _) -> c
5445 | Csplit _ -> failwith "bird's eye split mode"
5447 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5448 match key with
5449 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5450 let y, h = getpageyh pageno in
5451 let top = (state.winh - h) / 2 in
5452 gotoy (max 0 (y - top))
5453 | 0xff0d (* enter *)
5454 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5455 | 0xff1b -> leavebirdseye beye true (* escape *)
5456 | 0xff52 -> upbirdseye incr beye (* up *)
5457 | 0xff54 -> downbirdseye incr beye (* down *)
5458 | 0xff51 -> upbirdseye 1 beye (* left *)
5459 | 0xff53 -> downbirdseye 1 beye (* right *)
5461 | 0xff55 -> (* prior *)
5462 begin match state.layout with
5463 | l :: _ ->
5464 if l.pagey != 0
5465 then (
5466 state.mode <- Birdseye (
5467 oconf, leftx, l.pageno, hooverpageno, anchor
5469 gotopage1 l.pageno 0;
5471 else (
5472 let layout = layout (state.y-state.winh) (pgh state.layout) in
5473 match layout with
5474 | [] -> gotoy (clamp (-state.winh))
5475 | l :: _ ->
5476 state.mode <- Birdseye (
5477 oconf, leftx, l.pageno, hooverpageno, anchor
5479 gotopage1 l.pageno 0
5482 | [] -> gotoy (clamp (-state.winh))
5483 end;
5485 | 0xff56 -> (* next *)
5486 begin match List.rev state.layout with
5487 | l :: _ ->
5488 let layout = layout (state.y + (pgh state.layout)) state.winh in
5489 begin match layout with
5490 | [] ->
5491 let incr = l.pageh - l.pagevh in
5492 if incr = 0
5493 then (
5494 state.mode <-
5495 Birdseye (
5496 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5498 G.postRedisplay "birdseye pagedown";
5500 else gotoy (clamp (incr + conf.interpagespace*2));
5502 | l :: _ ->
5503 state.mode <-
5504 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5505 gotopage1 l.pageno 0;
5508 | [] -> gotoy (clamp state.winh)
5509 end;
5511 | 0xff50 -> (* home *)
5512 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5513 gotopage1 0 0
5515 | 0xff57 -> (* end *)
5516 let pageno = state.pagecount - 1 in
5517 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5518 if not (pagevisible state.layout pageno)
5519 then
5520 let h =
5521 match List.rev state.pdims with
5522 | [] -> state.winh
5523 | (_, _, h, _) :: _ -> h
5525 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5526 else G.postRedisplay "birdseye end";
5527 | _ -> viewkeyboard key mask
5530 let drawpage l linkindexbase =
5531 let color =
5532 match state.mode with
5533 | Textentry _ -> scalecolor 0.4
5534 | LinkNav _
5535 | View -> scalecolor 1.0
5536 | Birdseye (_, _, pageno, hooverpageno, _) ->
5537 if l.pageno = hooverpageno
5538 then scalecolor 0.9
5539 else (
5540 if l.pageno = pageno
5541 then scalecolor 1.0
5542 else scalecolor 0.8
5545 drawtiles l color;
5546 begin match getopaque l.pageno with
5547 | Some opaque ->
5548 if tileready l l.pagex l.pagey
5549 then
5550 let x = l.pagedispx - l.pagex
5551 and y = l.pagedispy - l.pagey in
5552 let hlmask =
5553 match conf.columns with
5554 | Csingle _ | Cmulti _ ->
5555 (if conf.hlinks then 1 else 0)
5556 + (if state.glinks
5557 && not (isbirdseye state.mode) then 2 else 0)
5558 | _ -> 0
5560 let s =
5561 match state.mode with
5562 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5563 | _ -> ""
5565 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5566 else 0
5568 | _ -> 0
5569 end;
5572 let scrollindicator () =
5573 let sbw, ph, sh = state.uioh#scrollph in
5574 let sbh, pw, sw = state.uioh#scrollpw in
5576 GlDraw.color (0.64, 0.64, 0.64);
5577 GlDraw.rect
5578 (float (state.winw - sbw), 0.)
5579 (float state.winw, float state.winh)
5581 GlDraw.rect
5582 (0., float (state.winh - sbh))
5583 (float (state.winw - state.scrollw - 1), float state.winh)
5585 GlDraw.color (0.0, 0.0, 0.0);
5587 GlDraw.rect
5588 (float (state.winw - sbw), ph)
5589 (float state.winw, ph +. sh)
5591 GlDraw.rect
5592 (pw, float (state.winh - sbh))
5593 (pw +. sw, float state.winh)
5597 let showsel () =
5598 match state.mstate with
5599 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5602 | Msel ((x0, y0), (x1, y1)) ->
5603 let rec loop = function
5604 | l :: ls ->
5605 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5606 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5607 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5608 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5609 then
5610 match getopaque l.pageno with
5611 | Some opaque ->
5612 let x0, y0 = pagetranslatepoint l x0 y0 in
5613 let x1, y1 = pagetranslatepoint l x1 y1 in
5614 seltext opaque (x0, y0, x1, y1);
5615 | _ -> ()
5616 else loop ls
5617 | [] -> ()
5619 loop state.layout
5622 let showrects rects =
5623 Gl.enable `blend;
5624 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5625 GlDraw.polygon_mode `both `fill;
5626 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5627 List.iter
5628 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5629 List.iter (fun l ->
5630 if l.pageno = pageno
5631 then (
5632 let dx = float (l.pagedispx - l.pagex) in
5633 let dy = float (l.pagedispy - l.pagey) in
5634 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5635 GlDraw.begins `quads;
5637 GlDraw.vertex2 (x0+.dx, y0+.dy);
5638 GlDraw.vertex2 (x1+.dx, y1+.dy);
5639 GlDraw.vertex2 (x2+.dx, y2+.dy);
5640 GlDraw.vertex2 (x3+.dx, y3+.dy);
5642 GlDraw.ends ();
5644 ) state.layout
5645 ) rects
5647 Gl.disable `blend;
5650 let display () =
5651 GlClear.color (scalecolor2 conf.bgcolor);
5652 GlClear.clear [`color];
5653 let rec loop linkindexbase = function
5654 | l :: rest ->
5655 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5656 loop linkindexbase rest
5657 | [] -> ()
5659 loop 0 state.layout;
5660 let rects =
5661 match state.mode with
5662 | LinkNav (Ltexact (pageno, linkno)) ->
5663 begin match getopaque pageno with
5664 | Some opaque ->
5665 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5666 (pageno, 5, (
5667 float x0, float y0,
5668 float x1, float y0,
5669 float x1, float y1,
5670 float x0, float y1)
5671 ) :: state.rects
5672 | None -> state.rects
5674 | _ -> state.rects
5676 showrects rects;
5677 showsel ();
5678 state.uioh#display;
5679 begin match state.mstate with
5680 | Mzoomrect ((x0, y0), (x1, y1)) ->
5681 Gl.enable `blend;
5682 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5683 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5684 GlDraw.rect (float x0, float y0)
5685 (float x1, float y1);
5686 Gl.disable `blend;
5687 | _ -> ()
5688 end;
5689 enttext ();
5690 scrollindicator ();
5691 Wsi.swapb ();
5694 let zoomrect x y x1 y1 =
5695 let x0 = min x x1
5696 and x1 = max x x1
5697 and y0 = min y y1 in
5698 gotoy (state.y + y0);
5699 state.anchor <- getanchor ();
5700 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5701 let margin =
5702 if state.w < state.winw - state.scrollw
5703 then (state.winw - state.scrollw - state.w) / 2
5704 else 0
5706 state.x <- (state.x + margin) - x0;
5707 setzoom zoom;
5708 Wsi.setcursor Wsi.CURSOR_INHERIT;
5709 state.mstate <- Mnone;
5712 let scrollx x =
5713 let winw = state.winw - state.scrollw - 1 in
5714 let s = float x /. float winw in
5715 let destx = truncate (float (state.w + winw) *. s) in
5716 state.x <- winw - destx;
5717 gotoy_and_clear_text state.y;
5718 state.mstate <- Mscrollx;
5721 let scrolly y =
5722 let s = float y /. float state.winh in
5723 let desty = truncate (float (state.maxy - state.winh) *. s) in
5724 gotoy_and_clear_text desty;
5725 state.mstate <- Mscrolly;
5728 let viewmouse button down x y mask =
5729 match button with
5730 | n when (n == 4 || n == 5) && not down ->
5731 if Wsi.withctrl mask
5732 then (
5733 match state.mstate with
5734 | Mzoom (oldn, i) ->
5735 if oldn = n
5736 then (
5737 if i = 2
5738 then
5739 let incr =
5740 match n with
5741 | 5 ->
5742 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5743 | _ ->
5744 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5746 let zoom = conf.zoom -. incr in
5747 setzoom zoom;
5748 state.mstate <- Mzoom (n, 0);
5749 else
5750 state.mstate <- Mzoom (n, i+1);
5752 else state.mstate <- Mzoom (n, 0)
5754 | _ -> state.mstate <- Mzoom (n, 0)
5756 else (
5757 match state.autoscroll with
5758 | Some step -> setautoscrollspeed step (n=4)
5759 | None ->
5760 if conf.wheelbypage
5761 then (
5762 if n = 4
5763 then prevpage ()
5764 else nextpage ()
5766 else
5767 let incr =
5768 if n = 4
5769 then -conf.scrollstep
5770 else conf.scrollstep
5772 let incr = incr * 2 in
5773 let y = clamp incr in
5774 gotoy_and_clear_text y
5777 | n when (n = 6 || n = 7) && not down && canpan () ->
5778 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5779 gotoy_and_clear_text state.y
5781 | 1 when Wsi.withshift mask ->
5782 state.mstate <- Mnone;
5783 if not down then (
5784 match unproject x y with
5785 | Some (pageno, ux, uy) ->
5786 let cmd = Printf.sprintf
5787 "%s %s %d %d %d"
5788 conf.stcmd state.path pageno ux uy
5790 popen cmd []
5791 | None -> ()
5794 | 1 when Wsi.withctrl mask ->
5795 if down
5796 then (
5797 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5798 state.mstate <- Mpan (x, y)
5800 else
5801 state.mstate <- Mnone
5803 | 3 ->
5804 if down
5805 then (
5806 Wsi.setcursor Wsi.CURSOR_CYCLE;
5807 let p = (x, y) in
5808 state.mstate <- Mzoomrect (p, p)
5810 else (
5811 match state.mstate with
5812 | Mzoomrect ((x0, y0), _) ->
5813 if abs (x-x0) > 10 && abs (y - y0) > 10
5814 then zoomrect x0 y0 x y
5815 else (
5816 state.mstate <- Mnone;
5817 Wsi.setcursor Wsi.CURSOR_INHERIT;
5818 G.postRedisplay "kill accidental zoom rect";
5820 | _ ->
5821 Wsi.setcursor Wsi.CURSOR_INHERIT;
5822 state.mstate <- Mnone
5825 | 1 when x > state.winw - state.scrollw ->
5826 if down
5827 then
5828 let _, position, sh = state.uioh#scrollph in
5829 if y > truncate position && y < truncate (position +. sh)
5830 then state.mstate <- Mscrolly
5831 else scrolly y
5832 else
5833 state.mstate <- Mnone
5835 | 1 when y > state.winh - state.hscrollh ->
5836 if down
5837 then
5838 let _, position, sw = state.uioh#scrollpw in
5839 if x > truncate position && x < truncate (position +. sw)
5840 then state.mstate <- Mscrollx
5841 else scrollx x
5842 else
5843 state.mstate <- Mnone
5845 | 1 ->
5846 let dest = if down then getunder x y else Unone in
5847 begin match dest with
5848 | Ulinkgoto _
5849 | Ulinkuri _
5850 | Uremote _
5851 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5852 gotounder dest
5854 | Unone when down ->
5855 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5856 state.mstate <- Mpan (x, y);
5858 | Unone | Utext _ ->
5859 if down
5860 then (
5861 if conf.angle mod 360 = 0
5862 then (
5863 state.mstate <- Msel ((x, y), (x, y));
5864 G.postRedisplay "mouse select";
5867 else (
5868 match state.mstate with
5869 | Mnone -> ()
5871 | Mzoom _ | Mscrollx | Mscrolly ->
5872 state.mstate <- Mnone
5874 | Mzoomrect ((x0, y0), _) ->
5875 zoomrect x0 y0 x y
5877 | Mpan _ ->
5878 Wsi.setcursor Wsi.CURSOR_INHERIT;
5879 state.mstate <- Mnone
5881 | Msel ((x0, y0), (x1, y1)) ->
5882 let rec loop = function
5883 | [] -> ()
5884 | l :: rest ->
5885 let inside =
5886 let a0 = l.pagedispy in
5887 let a1 = a0 + l.pagevh in
5888 let b0 = l.pagedispx in
5889 let b1 = b0 + l.pagevw in
5890 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5891 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5893 if inside
5894 then
5895 match getopaque l.pageno with
5896 | Some opaque ->
5897 begin
5898 match Ne.pipe () with
5899 | Ne.Exn exn ->
5900 showtext '!'
5901 (Printf.sprintf
5902 "can not create sel pipe: %s"
5903 (exntos exn));
5904 | Ne.Res (r, w) ->
5905 let doclose what fd =
5906 Ne.clo fd (fun msg ->
5907 dolog "%s close failed: %s" what msg)
5910 popen conf.selcmd [r, 0; w, -1];
5911 copysel w opaque;
5912 doclose "pipe/r" r;
5913 G.postRedisplay "copysel";
5914 with exn ->
5915 dolog "can not execute %S: %s"
5916 conf.selcmd (exntos exn);
5917 doclose "pipe/r" r;
5918 doclose "pipe/w" w;
5920 | None -> ()
5921 else loop rest
5923 loop state.layout;
5924 Wsi.setcursor Wsi.CURSOR_INHERIT;
5925 state.mstate <- Mnone;
5929 | _ -> ()
5932 let birdseyemouse button down x y mask
5933 (conf, leftx, _, hooverpageno, anchor) =
5934 match button with
5935 | 1 when down ->
5936 let rec loop = function
5937 | [] -> ()
5938 | l :: rest ->
5939 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5940 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5941 then (
5942 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5944 else loop rest
5946 loop state.layout
5947 | 3 -> ()
5948 | _ -> viewmouse button down x y mask
5951 let mouse button down x y mask =
5952 state.uioh <- state.uioh#button button down x y mask;
5955 let motion ~x ~y =
5956 state.uioh <- state.uioh#motion x y
5959 let pmotion ~x ~y =
5960 state.uioh <- state.uioh#pmotion x y;
5963 let uioh = object
5964 method display = ()
5966 method key key mask =
5967 begin match state.mode with
5968 | Textentry textentry -> textentrykeyboard key mask textentry
5969 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5970 | View -> viewkeyboard key mask
5971 | LinkNav linknav -> linknavkeyboard key mask linknav
5972 end;
5973 state.uioh
5975 method button button bstate x y mask =
5976 begin match state.mode with
5977 | LinkNav _
5978 | View -> viewmouse button bstate x y mask
5979 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5980 | Textentry _ -> ()
5981 end;
5982 state.uioh
5984 method motion x y =
5985 begin match state.mode with
5986 | Textentry _ -> ()
5987 | View | Birdseye _ | LinkNav _ ->
5988 match state.mstate with
5989 | Mzoom _ | Mnone -> ()
5991 | Mpan (x0, y0) ->
5992 let dx = x - x0
5993 and dy = y0 - y in
5994 state.mstate <- Mpan (x, y);
5995 if canpan ()
5996 then state.x <- state.x + dx;
5997 let y = clamp dy in
5998 gotoy_and_clear_text y
6000 | Msel (a, _) ->
6001 state.mstate <- Msel (a, (x, y));
6002 G.postRedisplay "motion select";
6004 | Mscrolly ->
6005 let y = min state.winh (max 0 y) in
6006 scrolly y
6008 | Mscrollx ->
6009 let x = min state.winw (max 0 x) in
6010 scrollx x
6012 | Mzoomrect (p0, _) ->
6013 state.mstate <- Mzoomrect (p0, (x, y));
6014 G.postRedisplay "motion zoomrect";
6015 end;
6016 state.uioh
6018 method pmotion x y =
6019 begin match state.mode with
6020 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6021 let rec loop = function
6022 | [] ->
6023 if hooverpageno != -1
6024 then (
6025 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6026 G.postRedisplay "pmotion birdseye no hoover";
6028 | l :: rest ->
6029 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6030 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6031 then (
6032 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6033 G.postRedisplay "pmotion birdseye hoover";
6035 else loop rest
6037 loop state.layout
6039 | Textentry _ -> ()
6041 | LinkNav _
6042 | View ->
6043 match state.mstate with
6044 | Mnone -> updateunder x y
6045 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6047 end;
6048 state.uioh
6050 method infochanged _ = ()
6052 method scrollph =
6053 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6054 let p, h = scrollph state.y maxy in
6055 state.scrollw, p, h
6057 method scrollpw =
6058 let winw = state.winw - state.scrollw - 1 in
6059 let fwinw = float winw in
6060 let sw =
6061 let sw = fwinw /. float state.w in
6062 let sw = fwinw *. sw in
6063 max sw (float conf.scrollh)
6065 let position, sw =
6066 let f = state.w+winw in
6067 let r = float (winw-state.x) /. float f in
6068 let p = fwinw *. r in
6069 p-.sw/.2., sw
6071 let sw =
6072 if position +. sw > fwinw
6073 then fwinw -. position
6074 else sw
6076 state.hscrollh, position, sw
6078 method modehash =
6079 let modename =
6080 match state.mode with
6081 | LinkNav _ -> "links"
6082 | Textentry _ -> "textentry"
6083 | Birdseye _ -> "birdseye"
6084 | View -> "view"
6086 findkeyhash conf modename
6087 end;;
6089 module Config =
6090 struct
6091 open Parser
6093 let fontpath = ref "";;
6095 module KeyMap =
6096 Map.Make (struct type t = (int * int) let compare = compare end);;
6098 let unent s =
6099 let l = String.length s in
6100 let b = Buffer.create l in
6101 unent b s 0 l;
6102 Buffer.contents b;
6105 let home =
6106 try Sys.getenv "HOME"
6107 with exn ->
6108 prerr_endline
6109 ("Can not determine home directory location: " ^ exntos exn);
6113 let modifier_of_string = function
6114 | "alt" -> Wsi.altmask
6115 | "shift" -> Wsi.shiftmask
6116 | "ctrl" | "control" -> Wsi.ctrlmask
6117 | "meta" -> Wsi.metamask
6118 | _ -> 0
6121 let key_of_string =
6122 let r = Str.regexp "-" in
6123 fun s ->
6124 let elems = Str.full_split r s in
6125 let f n k m =
6126 let g s =
6127 let m1 = modifier_of_string s in
6128 if m1 = 0
6129 then (Wsi.namekey s, m)
6130 else (k, m lor m1)
6131 in function
6132 | Str.Delim s when n land 1 = 0 -> g s
6133 | Str.Text s -> g s
6134 | Str.Delim _ -> (k, m)
6136 let rec loop n k m = function
6137 | [] -> (k, m)
6138 | x :: xs ->
6139 let k, m = f n k m x in
6140 loop (n+1) k m xs
6142 loop 0 0 0 elems
6145 let keys_of_string =
6146 let r = Str.regexp "[ \t]" in
6147 fun s ->
6148 let elems = Str.split r s in
6149 List.map key_of_string elems
6152 let copykeyhashes c =
6153 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6156 let config_of c attrs =
6157 let apply c k v =
6159 match k with
6160 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6161 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6162 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6163 | "preload" -> { c with preload = bool_of_string v }
6164 | "page-bias" -> { c with pagebias = int_of_string v }
6165 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6166 | "horizontal-scroll-step" ->
6167 { c with hscrollstep = max (int_of_string v) 1 }
6168 | "auto-scroll-step" ->
6169 { c with autoscrollstep = max 0 (int_of_string v) }
6170 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6171 | "crop-hack" -> { c with crophack = bool_of_string v }
6172 | "throttle" ->
6173 let mw =
6174 match String.lowercase v with
6175 | "true" -> Some infinity
6176 | "false" -> None
6177 | f -> Some (float_of_string f)
6179 { c with maxwait = mw}
6180 | "highlight-links" -> { c with hlinks = bool_of_string v }
6181 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6182 | "vertical-margin" ->
6183 { c with interpagespace = max 0 (int_of_string v) }
6184 | "zoom" ->
6185 let zoom = float_of_string v /. 100. in
6186 let zoom = max zoom 0.0 in
6187 { c with zoom = zoom }
6188 | "presentation" -> { c with presentation = bool_of_string v }
6189 | "rotation-angle" -> { c with angle = int_of_string v }
6190 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6191 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6192 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6193 | "proportional-display" -> { c with proportional = bool_of_string v }
6194 | "pixmap-cache-size" ->
6195 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6196 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6197 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6198 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6199 | "persistent-location" -> { c with jumpback = bool_of_string v }
6200 | "background-color" -> { c with bgcolor = color_of_string v }
6201 | "scrollbar-in-presentation" ->
6202 { c with scrollbarinpm = bool_of_string v }
6203 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6204 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6205 | "mupdf-store-size" ->
6206 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6207 | "checkers" -> { c with checkers = bool_of_string v }
6208 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6209 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6210 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6211 | "uri-launcher" -> { c with urilauncher = unent v }
6212 | "path-launcher" -> { c with pathlauncher = unent v }
6213 | "color-space" -> { c with colorspace = colorspace_of_string v }
6214 | "invert-colors" -> { c with invert = bool_of_string v }
6215 | "brightness" -> { c with colorscale = float_of_string v }
6216 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6217 | "ghyllscroll" ->
6218 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6219 | "columns" ->
6220 let (n, _, _) as nab = multicolumns_of_string v in
6221 if n < 0
6222 then { c with columns = Csplit (-n, [||]) }
6223 else { c with columns = Cmulti (nab, [||]) }
6224 | "birds-eye-columns" ->
6225 { c with beyecolumns = Some (max (int_of_string v) 2) }
6226 | "selection-command" -> { c with selcmd = unent v }
6227 | "synctex-command" -> { c with stcmd = unent v }
6228 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6229 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6230 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6231 | "use-pbo" -> { c with usepbo = bool_of_string v }
6232 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6233 | _ -> c
6234 with exn ->
6235 prerr_endline ("Error processing attribute (`" ^
6236 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6239 let rec fold c = function
6240 | [] -> c
6241 | (k, v) :: rest ->
6242 let c = apply c k v in
6243 fold c rest
6245 fold { c with keyhashes = copykeyhashes c } attrs;
6248 let fromstring f pos n v d =
6249 try f v
6250 with exn ->
6251 dolog "Error processing attribute (%S=%S) at %d\n%s"
6252 n v pos (exntos exn)
6257 let bookmark_of attrs =
6258 let rec fold title page rely visy = function
6259 | ("title", v) :: rest -> fold v page rely visy rest
6260 | ("page", v) :: rest -> fold title v rely visy rest
6261 | ("rely", v) :: rest -> fold title page v visy rest
6262 | ("visy", v) :: rest -> fold title page rely v rest
6263 | _ :: rest -> fold title page rely visy rest
6264 | [] -> title, page, rely, visy
6266 fold "invalid" "0" "0" "0" attrs
6269 let doc_of attrs =
6270 let rec fold path page rely pan visy = function
6271 | ("path", v) :: rest -> fold v page rely pan visy rest
6272 | ("page", v) :: rest -> fold path v rely pan visy rest
6273 | ("rely", v) :: rest -> fold path page v pan visy rest
6274 | ("pan", v) :: rest -> fold path page rely v visy rest
6275 | ("visy", v) :: rest -> fold path page rely pan v rest
6276 | _ :: rest -> fold path page rely pan visy rest
6277 | [] -> path, page, rely, pan, visy
6279 fold "" "0" "0" "0" "0" attrs
6282 let map_of attrs =
6283 let rec fold rs ls = function
6284 | ("out", v) :: rest -> fold v ls rest
6285 | ("in", v) :: rest -> fold rs v rest
6286 | _ :: rest -> fold ls rs rest
6287 | [] -> ls, rs
6289 fold "" "" attrs
6292 let setconf dst src =
6293 dst.scrollbw <- src.scrollbw;
6294 dst.scrollh <- src.scrollh;
6295 dst.icase <- src.icase;
6296 dst.preload <- src.preload;
6297 dst.pagebias <- src.pagebias;
6298 dst.verbose <- src.verbose;
6299 dst.scrollstep <- src.scrollstep;
6300 dst.maxhfit <- src.maxhfit;
6301 dst.crophack <- src.crophack;
6302 dst.autoscrollstep <- src.autoscrollstep;
6303 dst.maxwait <- src.maxwait;
6304 dst.hlinks <- src.hlinks;
6305 dst.underinfo <- src.underinfo;
6306 dst.interpagespace <- src.interpagespace;
6307 dst.zoom <- src.zoom;
6308 dst.presentation <- src.presentation;
6309 dst.angle <- src.angle;
6310 dst.cwinw <- src.cwinw;
6311 dst.cwinh <- src.cwinh;
6312 dst.savebmarks <- src.savebmarks;
6313 dst.memlimit <- src.memlimit;
6314 dst.proportional <- src.proportional;
6315 dst.texcount <- src.texcount;
6316 dst.sliceheight <- src.sliceheight;
6317 dst.thumbw <- src.thumbw;
6318 dst.jumpback <- src.jumpback;
6319 dst.bgcolor <- src.bgcolor;
6320 dst.scrollbarinpm <- src.scrollbarinpm;
6321 dst.tilew <- src.tilew;
6322 dst.tileh <- src.tileh;
6323 dst.mustoresize <- src.mustoresize;
6324 dst.checkers <- src.checkers;
6325 dst.aalevel <- src.aalevel;
6326 dst.trimmargins <- src.trimmargins;
6327 dst.trimfuzz <- src.trimfuzz;
6328 dst.urilauncher <- src.urilauncher;
6329 dst.colorspace <- src.colorspace;
6330 dst.invert <- src.invert;
6331 dst.colorscale <- src.colorscale;
6332 dst.redirectstderr <- src.redirectstderr;
6333 dst.ghyllscroll <- src.ghyllscroll;
6334 dst.columns <- src.columns;
6335 dst.beyecolumns <- src.beyecolumns;
6336 dst.selcmd <- src.selcmd;
6337 dst.updatecurs <- src.updatecurs;
6338 dst.pathlauncher <- src.pathlauncher;
6339 dst.keyhashes <- copykeyhashes src;
6340 dst.hfsize <- src.hfsize;
6341 dst.hscrollstep <- src.hscrollstep;
6342 dst.pgscale <- src.pgscale;
6343 dst.usepbo <- src.usepbo;
6344 dst.wheelbypage <- src.wheelbypage;
6345 dst.stcmd <- src.stcmd;
6348 let get s =
6349 let h = Hashtbl.create 10 in
6350 let dc = { defconf with angle = defconf.angle } in
6351 let rec toplevel v t spos _ =
6352 match t with
6353 | Vdata | Vcdata | Vend -> v
6354 | Vopen ("llppconfig", _, closed) ->
6355 if closed
6356 then v
6357 else { v with f = llppconfig }
6358 | Vopen _ ->
6359 error "unexpected subelement at top level" s spos
6360 | Vclose _ -> error "unexpected close at top level" s spos
6362 and llppconfig v t spos _ =
6363 match t with
6364 | Vdata | Vcdata -> v
6365 | Vend -> error "unexpected end of input in llppconfig" s spos
6366 | Vopen ("defaults", attrs, closed) ->
6367 let c = config_of dc attrs in
6368 setconf dc c;
6369 if closed
6370 then v
6371 else { v with f = defaults }
6373 | Vopen ("ui-font", attrs, closed) ->
6374 let rec getsize size = function
6375 | [] -> size
6376 | ("size", v) :: rest ->
6377 let size =
6378 fromstring int_of_string spos "size" v fstate.fontsize in
6379 getsize size rest
6380 | l -> getsize size l
6382 fstate.fontsize <- getsize fstate.fontsize attrs;
6383 if closed
6384 then v
6385 else { v with f = uifont (Buffer.create 10) }
6387 | Vopen ("doc", attrs, closed) ->
6388 let pathent, spage, srely, span, svisy = doc_of attrs in
6389 let path = unent pathent
6390 and pageno = fromstring int_of_string spos "page" spage 0
6391 and rely = fromstring float_of_string spos "rely" srely 0.0
6392 and pan = fromstring int_of_string spos "pan" span 0
6393 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6394 let c = config_of dc attrs in
6395 let anchor = (pageno, rely, visy) in
6396 if closed
6397 then (Hashtbl.add h path (c, [], pan, anchor); v)
6398 else { v with f = doc path pan anchor c [] }
6400 | Vopen _ ->
6401 error "unexpected subelement in llppconfig" s spos
6403 | Vclose "llppconfig" -> { v with f = toplevel }
6404 | Vclose _ -> error "unexpected close in llppconfig" s spos
6406 and defaults v t spos _ =
6407 match t with
6408 | Vdata | Vcdata -> v
6409 | Vend -> error "unexpected end of input in defaults" s spos
6410 | Vopen ("keymap", attrs, closed) ->
6411 let modename =
6412 try List.assoc "mode" attrs
6413 with Not_found -> "global" in
6414 if closed
6415 then v
6416 else
6417 let ret keymap =
6418 let h = findkeyhash dc modename in
6419 KeyMap.iter (Hashtbl.replace h) keymap;
6420 defaults
6422 { v with f = pkeymap ret KeyMap.empty }
6424 | Vopen (_, _, _) ->
6425 error "unexpected subelement in defaults" s spos
6427 | Vclose "defaults" ->
6428 { v with f = llppconfig }
6430 | Vclose _ -> error "unexpected close in defaults" s spos
6432 and uifont b v t spos epos =
6433 match t with
6434 | Vdata | Vcdata ->
6435 Buffer.add_substring b s spos (epos - spos);
6437 | Vopen (_, _, _) ->
6438 error "unexpected subelement in ui-font" s spos
6439 | Vclose "ui-font" ->
6440 if String.length !fontpath = 0
6441 then fontpath := Buffer.contents b;
6442 { v with f = llppconfig }
6443 | Vclose _ -> error "unexpected close in ui-font" s spos
6444 | Vend -> error "unexpected end of input in ui-font" s spos
6446 and doc path pan anchor c bookmarks v t spos _ =
6447 match t with
6448 | Vdata | Vcdata -> v
6449 | Vend -> error "unexpected end of input in doc" s spos
6450 | Vopen ("bookmarks", _, closed) ->
6451 if closed
6452 then v
6453 else { v with f = pbookmarks path pan anchor c bookmarks }
6455 | Vopen ("keymap", attrs, closed) ->
6456 let modename =
6457 try List.assoc "mode" attrs
6458 with Not_found -> "global"
6460 if closed
6461 then v
6462 else
6463 let ret keymap =
6464 let h = findkeyhash c modename in
6465 KeyMap.iter (Hashtbl.replace h) keymap;
6466 doc path pan anchor c bookmarks
6468 { v with f = pkeymap ret KeyMap.empty }
6470 | Vopen (_, _, _) ->
6471 error "unexpected subelement in doc" s spos
6473 | Vclose "doc" ->
6474 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6475 { v with f = llppconfig }
6477 | Vclose _ -> error "unexpected close in doc" s spos
6479 and pkeymap ret keymap v t spos _ =
6480 match t with
6481 | Vdata | Vcdata -> v
6482 | Vend -> error "unexpected end of input in keymap" s spos
6483 | Vopen ("map", attrs, closed) ->
6484 let r, l = map_of attrs in
6485 let kss = fromstring keys_of_string spos "in" r [] in
6486 let lss = fromstring keys_of_string spos "out" l [] in
6487 let keymap =
6488 match kss with
6489 | [] -> keymap
6490 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6491 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6493 if closed
6494 then { v with f = pkeymap ret keymap }
6495 else
6496 let f () = v in
6497 { v with f = skip "map" f }
6499 | Vopen _ ->
6500 error "unexpected subelement in keymap" s spos
6502 | Vclose "keymap" ->
6503 { v with f = ret keymap }
6505 | Vclose _ -> error "unexpected close in keymap" s spos
6507 and pbookmarks path pan anchor c bookmarks v t spos _ =
6508 match t with
6509 | Vdata | Vcdata -> v
6510 | Vend -> error "unexpected end of input in bookmarks" s spos
6511 | Vopen ("item", attrs, closed) ->
6512 let titleent, spage, srely, svisy = bookmark_of attrs in
6513 let page = fromstring int_of_string spos "page" spage 0
6514 and rely = fromstring float_of_string spos "rely" srely 0.0
6515 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6516 let bookmarks =
6517 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6519 if closed
6520 then { v with f = pbookmarks path pan anchor c bookmarks }
6521 else
6522 let f () = v in
6523 { v with f = skip "item" f }
6525 | Vopen _ ->
6526 error "unexpected subelement in bookmarks" s spos
6528 | Vclose "bookmarks" ->
6529 { v with f = doc path pan anchor c bookmarks }
6531 | Vclose _ -> error "unexpected close in bookmarks" s spos
6533 and skip tag f v t spos _ =
6534 match t with
6535 | Vdata | Vcdata -> v
6536 | Vend ->
6537 error ("unexpected end of input in skipped " ^ tag) s spos
6538 | Vopen (tag', _, closed) ->
6539 if closed
6540 then v
6541 else
6542 let f' () = { v with f = skip tag f } in
6543 { v with f = skip tag' f' }
6544 | Vclose ctag ->
6545 if tag = ctag
6546 then f ()
6547 else error ("unexpected close in skipped " ^ tag) s spos
6550 parse { f = toplevel; accu = () } s;
6551 h, dc;
6554 let do_load f ic =
6556 let len = in_channel_length ic in
6557 let s = String.create len in
6558 really_input ic s 0 len;
6559 f s;
6560 with
6561 | Parse_error (msg, s, pos) ->
6562 let subs = subs s pos in
6563 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6564 failwith ("parse error: " ^ s)
6566 | exn ->
6567 failwith ("config load error: " ^ exntos exn)
6570 let defconfpath =
6571 let dir =
6573 let dir = Filename.concat home ".config" in
6574 if Sys.is_directory dir then dir else home
6575 with _ -> home
6577 Filename.concat dir "llpp.conf"
6580 let confpath = ref defconfpath;;
6582 let load1 f =
6583 if Sys.file_exists !confpath
6584 then
6585 match
6586 (try Some (open_in_bin !confpath)
6587 with exn ->
6588 prerr_endline
6589 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6590 exntos exn);
6591 None
6593 with
6594 | Some ic ->
6595 let success =
6597 f (do_load get ic)
6598 with exn ->
6599 prerr_endline
6600 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6601 exntos exn);
6602 false
6604 close_in ic;
6605 success
6607 | None -> false
6608 else
6609 f (Hashtbl.create 0, defconf)
6612 let load () =
6613 let f (h, dc) =
6614 let pc, pb, px, pa =
6616 Hashtbl.find h (Filename.basename state.path)
6617 with Not_found -> dc, [], 0, emptyanchor
6619 setconf defconf dc;
6620 setconf conf pc;
6621 state.bookmarks <- pb;
6622 state.x <- px;
6623 state.scrollw <- conf.scrollbw;
6624 if conf.jumpback
6625 then state.anchor <- pa;
6626 cbput state.hists.nav pa;
6627 true
6629 load1 f
6632 let add_attrs bb always dc c =
6633 let ob s a b =
6634 if always || a != b
6635 then Printf.bprintf bb "\n %s='%b'" s a
6636 and oi s a b =
6637 if always || a != b
6638 then Printf.bprintf bb "\n %s='%d'" s a
6639 and oI s a b =
6640 if always || a != b
6641 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6642 and oz s a b =
6643 if always || a <> b
6644 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6645 and oF s a b =
6646 if always || a <> b
6647 then Printf.bprintf bb "\n %s='%f'" s a
6648 and oc s a b =
6649 if always || a <> b
6650 then
6651 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6652 and oC s a b =
6653 if always || a <> b
6654 then
6655 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6656 and oR s a b =
6657 if always || a <> b
6658 then
6659 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6660 and os s a b =
6661 if always || a <> b
6662 then
6663 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6664 and og s a b =
6665 if always || a <> b
6666 then
6667 match a with
6668 | None -> ()
6669 | Some (_N, _A, _B) ->
6670 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6671 and oW s a b =
6672 if always || a <> b
6673 then
6674 let v =
6675 match a with
6676 | None -> "false"
6677 | Some f ->
6678 if f = infinity
6679 then "true"
6680 else string_of_float f
6682 Printf.bprintf bb "\n %s='%s'" s v
6683 and oco s a b =
6684 if always || a <> b
6685 then
6686 match a with
6687 | Cmulti ((n, a, b), _) when n > 1 ->
6688 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6689 | Csplit (n, _) when n > 1 ->
6690 Printf.bprintf bb "\n %s='%d'" s ~-n
6691 | _ -> ()
6692 and obeco s a b =
6693 if always || a <> b
6694 then
6695 match a with
6696 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6697 | _ -> ()
6699 oi "width" c.cwinw dc.cwinw;
6700 oi "height" c.cwinh dc.cwinh;
6701 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6702 oi "scroll-handle-height" c.scrollh dc.scrollh;
6703 ob "case-insensitive-search" c.icase dc.icase;
6704 ob "preload" c.preload dc.preload;
6705 oi "page-bias" c.pagebias dc.pagebias;
6706 oi "scroll-step" c.scrollstep dc.scrollstep;
6707 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6708 ob "max-height-fit" c.maxhfit dc.maxhfit;
6709 ob "crop-hack" c.crophack dc.crophack;
6710 oW "throttle" c.maxwait dc.maxwait;
6711 ob "highlight-links" c.hlinks dc.hlinks;
6712 ob "under-cursor-info" c.underinfo dc.underinfo;
6713 oi "vertical-margin" c.interpagespace dc.interpagespace;
6714 oz "zoom" c.zoom dc.zoom;
6715 ob "presentation" c.presentation dc.presentation;
6716 oi "rotation-angle" c.angle dc.angle;
6717 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6718 ob "proportional-display" c.proportional dc.proportional;
6719 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6720 oi "tex-count" c.texcount dc.texcount;
6721 oi "slice-height" c.sliceheight dc.sliceheight;
6722 oi "thumbnail-width" c.thumbw dc.thumbw;
6723 ob "persistent-location" c.jumpback dc.jumpback;
6724 oc "background-color" c.bgcolor dc.bgcolor;
6725 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6726 oi "tile-width" c.tilew dc.tilew;
6727 oi "tile-height" c.tileh dc.tileh;
6728 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6729 ob "checkers" c.checkers dc.checkers;
6730 oi "aalevel" c.aalevel dc.aalevel;
6731 ob "trim-margins" c.trimmargins dc.trimmargins;
6732 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6733 os "uri-launcher" c.urilauncher dc.urilauncher;
6734 os "path-launcher" c.pathlauncher dc.pathlauncher;
6735 oC "color-space" c.colorspace dc.colorspace;
6736 ob "invert-colors" c.invert dc.invert;
6737 oF "brightness" c.colorscale dc.colorscale;
6738 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6739 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6740 oco "columns" c.columns dc.columns;
6741 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6742 os "selection-command" c.selcmd dc.selcmd;
6743 os "synctex-command" c.stcmd dc.stcmd;
6744 ob "update-cursor" c.updatecurs dc.updatecurs;
6745 oi "hint-font-size" c.hfsize dc.hfsize;
6746 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6747 oF "page-scroll-scale" c.pgscale dc.pgscale;
6748 ob "use-pbo" c.usepbo dc.usepbo;
6749 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6752 let keymapsbuf always dc c =
6753 let bb = Buffer.create 16 in
6754 let rec loop = function
6755 | [] -> ()
6756 | (modename, h) :: rest ->
6757 let dh = findkeyhash dc modename in
6758 if always || h <> dh
6759 then (
6760 if Hashtbl.length h > 0
6761 then (
6762 if Buffer.length bb > 0
6763 then Buffer.add_char bb '\n';
6764 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6765 Hashtbl.iter (fun i o ->
6766 let isdifferent = always ||
6768 let dO = Hashtbl.find dh i in
6769 dO <> o
6770 with Not_found -> true
6772 if isdifferent
6773 then
6774 let addkm (k, m) =
6775 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6776 if Wsi.withalt m then Buffer.add_string bb "alt-";
6777 if Wsi.withshift m then Buffer.add_string bb "shift-";
6778 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6779 Buffer.add_string bb (Wsi.keyname k);
6781 let addkms l =
6782 let rec loop = function
6783 | [] -> ()
6784 | km :: [] -> addkm km
6785 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6787 loop l
6789 Buffer.add_string bb "<map in='";
6790 addkm i;
6791 match o with
6792 | KMinsrt km ->
6793 Buffer.add_string bb "' out='";
6794 addkm km;
6795 Buffer.add_string bb "'/>\n"
6797 | KMinsrl kms ->
6798 Buffer.add_string bb "' out='";
6799 addkms kms;
6800 Buffer.add_string bb "'/>\n"
6802 | KMmulti (ins, kms) ->
6803 Buffer.add_char bb ' ';
6804 addkms ins;
6805 Buffer.add_string bb "' out='";
6806 addkms kms;
6807 Buffer.add_string bb "'/>\n"
6808 ) h;
6809 Buffer.add_string bb "</keymap>";
6812 loop rest
6814 loop c.keyhashes;
6818 let save () =
6819 let uifontsize = fstate.fontsize in
6820 let bb = Buffer.create 32768 in
6821 let w, h =
6822 List.fold_left
6823 (fun (w, h) ws ->
6824 match ws with
6825 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6826 | Wsi.MaxVert -> (w, conf.cwinh)
6827 | Wsi.MaxHorz -> (conf.cwinw, h)
6829 (state.winw, state.winh) state.winstate
6831 conf.cwinw <- w;
6832 conf.cwinh <- h;
6833 let f (h, dc) =
6834 let dc = if conf.bedefault then conf else dc in
6835 Buffer.add_string bb "<llppconfig>\n";
6837 if String.length !fontpath > 0
6838 then
6839 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6840 uifontsize
6841 !fontpath
6842 else (
6843 if uifontsize <> 14
6844 then
6845 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6848 Buffer.add_string bb "<defaults ";
6849 add_attrs bb true dc dc;
6850 let kb = keymapsbuf true dc dc in
6851 if Buffer.length kb > 0
6852 then (
6853 Buffer.add_string bb ">\n";
6854 Buffer.add_buffer bb kb;
6855 Buffer.add_string bb "\n</defaults>\n";
6857 else Buffer.add_string bb "/>\n";
6859 let adddoc path pan anchor c bookmarks =
6860 if bookmarks == [] && c = dc && anchor = emptyanchor
6861 then ()
6862 else (
6863 Printf.bprintf bb "<doc path='%s'"
6864 (enent path 0 (String.length path));
6866 if anchor <> emptyanchor
6867 then (
6868 let n, rely, visy = anchor in
6869 Printf.bprintf bb " page='%d'" n;
6870 if rely > 1e-6
6871 then
6872 Printf.bprintf bb " rely='%f'" rely
6874 if abs_float visy > 1e-6
6875 then
6876 Printf.bprintf bb " visy='%f'" visy
6880 if pan != 0
6881 then Printf.bprintf bb " pan='%d'" pan;
6883 add_attrs bb false dc c;
6884 let kb = keymapsbuf false dc c in
6886 begin match bookmarks with
6887 | [] ->
6888 if Buffer.length kb > 0
6889 then (
6890 Buffer.add_string bb ">\n";
6891 Buffer.add_buffer bb kb;
6892 Buffer.add_string bb "\n</doc>\n";
6894 else Buffer.add_string bb "/>\n"
6895 | _ ->
6896 Buffer.add_string bb ">\n<bookmarks>\n";
6897 List.iter (fun (title, _level, (page, rely, visy)) ->
6898 Printf.bprintf bb
6899 "<item title='%s' page='%d'"
6900 (enent title 0 (String.length title))
6901 page
6903 if rely > 1e-6
6904 then
6905 Printf.bprintf bb " rely='%f'" rely
6907 if abs_float visy > 1e-6
6908 then
6909 Printf.bprintf bb " visy='%f'" visy
6911 Buffer.add_string bb "/>\n";
6912 ) bookmarks;
6913 Buffer.add_string bb "</bookmarks>";
6914 if Buffer.length kb > 0
6915 then (
6916 Buffer.add_string bb "\n";
6917 Buffer.add_buffer bb kb;
6919 Buffer.add_string bb "\n</doc>\n";
6920 end;
6924 let pan, conf =
6925 match state.mode with
6926 | Birdseye (c, pan, _, _, _) ->
6927 let beyecolumns =
6928 match conf.columns with
6929 | Cmulti ((c, _, _), _) -> Some c
6930 | Csingle _ -> None
6931 | Csplit _ -> None
6932 and columns =
6933 match c.columns with
6934 | Cmulti (c, _) -> Cmulti (c, [||])
6935 | Csingle _ -> Csingle [||]
6936 | Csplit _ -> failwith "quit from bird's eye while split"
6938 pan, { c with beyecolumns = beyecolumns; columns = columns }
6939 | _ -> state.x, conf
6941 let basename = Filename.basename state.path in
6942 adddoc basename pan (getanchor ())
6943 (let conf =
6944 let autoscrollstep =
6945 match state.autoscroll with
6946 | Some step -> step
6947 | None -> conf.autoscrollstep
6949 match state.mode with
6950 | Birdseye (bc, _, _, _, _) ->
6951 { conf with
6952 zoom = bc.zoom;
6953 presentation = bc.presentation;
6954 interpagespace = bc.interpagespace;
6955 maxwait = bc.maxwait;
6956 autoscrollstep = autoscrollstep }
6957 | _ -> { conf with autoscrollstep = autoscrollstep }
6958 in conf)
6959 (if conf.savebmarks then state.bookmarks else []);
6961 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6962 if basename <> path
6963 then adddoc path x anchor c bookmarks
6964 ) h;
6965 Buffer.add_string bb "</llppconfig>\n";
6966 true;
6968 if load1 f && Buffer.length bb > 0
6969 then
6971 let tmp = !confpath ^ ".tmp" in
6972 let oc = open_out_bin tmp in
6973 Buffer.output_buffer oc bb;
6974 close_out oc;
6975 Unix.rename tmp !confpath;
6976 with exn ->
6977 prerr_endline
6978 ("error while saving configuration: " ^ exntos exn)
6980 end;;
6982 let adderrmsg src msg =
6983 Buffer.add_string state.errmsgs msg;
6984 state.newerrmsgs <- true;
6985 G.postRedisplay src
6988 let adderrfmt src fmt =
6989 Format.kprintf (fun s -> adderrmsg src s) fmt;
6992 let ract cmds =
6993 let cl = splitatspace cmds in
6994 let scan s fmt f =
6995 try Scanf.sscanf s fmt f
6996 with exn ->
6997 adderrfmt "remote exec"
6998 "error processing '%S': %s\n" cmds (exntos exn)
7000 match cl with
7001 | "reload" :: [] -> reload ()
7002 | "goto" :: args :: [] ->
7003 let cmd, _ = state.geomcmds in
7004 scan args "%u %f %f"
7005 (fun pageno x y ->
7006 if String.length cmd = 0
7007 then gotopagexy pageno x y
7008 else
7009 let prevf = state.reprf in
7010 let f () =
7011 gotopagexy pageno x y;
7012 prevf ()
7014 state.reprf <- f
7016 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7017 | "rect" :: args :: [] ->
7018 scan args "%u %u %f %f %f %f"
7019 (fun pageno color x0 y0 x1 y1 ->
7020 onpagerect pageno (fun w h ->
7021 let _,w1,h1,_ = getpagedim pageno in
7022 let sw = float w1 /. w
7023 and sh = float h1 /. h in
7024 let x0s = x0 *. sw
7025 and x1s = x1 *. sw
7026 and y0s = y0 *. sh
7027 and y1s = y1 *. sh in
7028 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7029 debugrect rect;
7030 state.rects <- (pageno, color, rect) :: state.rects;
7031 G.postRedisplay "rect";
7034 | "activatewin" :: [] -> Wsi.activatewin ()
7035 | "quit" :: [] -> raise Quit
7036 | _ ->
7037 adderrfmt "remote command"
7038 "error processing remote command: %S\n" cmds;
7041 let remote =
7042 let scratch = String.create 80 in
7043 let buf = Buffer.create 80 in
7044 fun fd ->
7045 let rec tempfr () =
7046 try Some (Unix.read fd scratch 0 80)
7047 with
7048 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7049 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7050 | exn -> raise exn
7052 match tempfr () with
7053 | None -> Some fd
7054 | Some n ->
7055 if n = 0
7056 then (
7057 Unix.close fd;
7058 if Buffer.length buf > 0
7059 then (
7060 let s = Buffer.contents buf in
7061 Buffer.clear buf;
7062 ract s;
7064 None
7066 else
7067 let rec eat ppos =
7068 let nlpos =
7070 let pos = String.index_from scratch ppos '\n' in
7071 if pos >= n then -1 else pos
7072 with Not_found -> -1
7074 if nlpos >= 0
7075 then (
7076 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7077 let s = Buffer.contents buf in
7078 Buffer.clear buf;
7079 ract s;
7080 eat (nlpos+1);
7082 else (
7083 Buffer.add_substring buf scratch ppos (n-ppos);
7084 Some fd
7086 in eat 0
7089 let remoteopen path =
7090 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7091 with exn ->
7092 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7093 None
7096 let () =
7097 let trimcachepath = ref "" in
7098 let rcmdpath = ref "" in
7099 Arg.parse
7100 (Arg.align
7101 [("-p", Arg.String (fun s -> state.password <- s) ,
7102 "<password> Set password");
7104 ("-f", Arg.String (fun s -> Config.fontpath := s),
7105 "<path> Set path to the user interface font");
7107 ("-c", Arg.String (fun s -> Config.confpath := s),
7108 "<path> Set path to the configuration file");
7110 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7111 "<path> Set path to the trim cache file");
7113 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7114 "<named-destination> Set named destination");
7116 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7118 ("-remote", Arg.String (fun s -> rcmdpath := s),
7119 "<path> Set path to the remote commands source");
7121 ("-v", Arg.Unit (fun () ->
7122 Printf.printf
7123 "%s\nconfiguration path: %s\n"
7124 (version ())
7125 Config.defconfpath
7127 exit 0), " Print version and exit");
7130 (fun s -> state.path <- s)
7131 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7133 if String.length state.path = 0
7134 then (prerr_endline "file name missing"; exit 1);
7136 if not (Config.load ())
7137 then prerr_endline "failed to load configuration";
7139 let globalkeyhash = findkeyhash conf "global" in
7140 let wsfd, winw, winh = Wsi.init (object
7141 method expose =
7142 state.wthack <- false;
7143 if nogeomcmds state.geomcmds || platform == Posx
7144 then display ()
7145 else (
7146 GlClear.color (scalecolor2 conf.bgcolor);
7147 GlClear.clear [`color];
7149 method display = display ()
7150 method reshape w h = reshape w h
7151 method mouse b d x y m = mouse b d x y m
7152 method motion x y = state.mpos <- (x, y); motion x y
7153 method pmotion x y = state.mpos <- (x, y); pmotion x y
7154 method key k m =
7155 let mascm = m land (
7156 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7157 ) in
7158 match state.keystate with
7159 | KSnone ->
7160 let km = k, mascm in
7161 begin
7162 match
7163 let modehash = state.uioh#modehash in
7164 try Hashtbl.find modehash km
7165 with Not_found ->
7166 try Hashtbl.find globalkeyhash km
7167 with Not_found -> KMinsrt (k, m)
7168 with
7169 | KMinsrt (k, m) -> keyboard k m
7170 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7171 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7173 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7174 List.iter (fun (k, m) -> keyboard k m) insrt;
7175 state.keystate <- KSnone
7176 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7177 state.keystate <- KSinto (keys, insrt)
7178 | _ ->
7179 state.keystate <- KSnone
7181 method enter x y = state.mpos <- (x, y); pmotion x y
7182 method leave = state.mpos <- (-1, -1)
7183 method winstate wsl = state.winstate <- wsl
7184 method quit = raise Quit
7185 end) conf.cwinw conf.cwinh (platform = Posx) in
7187 state.wsfd <- wsfd;
7189 if not (
7190 List.exists GlMisc.check_extension
7191 [ "GL_ARB_texture_rectangle"
7192 ; "GL_EXT_texture_recangle"
7193 ; "GL_NV_texture_rectangle" ]
7195 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7197 let cr, sw =
7198 match Ne.pipe () with
7199 | Ne.Exn exn ->
7200 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7201 exit 1
7202 | Ne.Res rw -> rw
7203 and sr, cw =
7204 match Ne.pipe () with
7205 | Ne.Exn exn ->
7206 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7207 exit 1
7208 | Ne.Res rw -> rw
7211 cloexec cr;
7212 cloexec sw;
7213 cloexec sr;
7214 cloexec cw;
7216 setcheckers conf.checkers;
7217 redirectstderr ();
7219 init (cr, cw) (
7220 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7221 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7222 !Config.fontpath, !trimcachepath,
7223 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7225 state.sr <- sr;
7226 state.sw <- sw;
7227 state.text <- "Opening " ^ (mbtoutf8 state.path);
7228 reshape winw winh;
7229 opendoc state.path state.password;
7230 state.uioh <- uioh;
7232 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7233 let optrfd =
7234 ref (
7235 if String.length !rcmdpath > 0
7236 then remoteopen !rcmdpath
7237 else None
7241 let rec loop deadline =
7242 let r =
7243 match state.errfd with
7244 | None -> [state.sr; state.wsfd]
7245 | Some fd -> [state.sr; state.wsfd; fd]
7247 let r =
7248 match !optrfd with
7249 | None -> r
7250 | Some fd -> fd :: r
7252 if state.redisplay && not state.wthack
7253 then (
7254 state.redisplay <- false;
7255 display ();
7257 let timeout =
7258 let now = now () in
7259 if deadline > now
7260 then (
7261 if deadline = infinity
7262 then ~-.1.0
7263 else max 0.0 (deadline -. now)
7265 else 0.0
7267 let r, _, _ =
7268 try Unix.select r [] [] timeout
7269 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7271 begin match r with
7272 | [] ->
7273 state.ghyll None;
7274 let newdeadline =
7275 if state.ghyll == noghyll
7276 then
7277 match state.autoscroll with
7278 | Some step when step != 0 ->
7279 let y = state.y + step in
7280 let y =
7281 if y < 0
7282 then state.maxy
7283 else if y >= state.maxy then 0 else y
7285 gotoy y;
7286 if state.mode = View
7287 then state.text <- "";
7288 deadline +. 0.01
7289 | _ -> infinity
7290 else deadline +. 0.01
7292 loop newdeadline
7294 | l ->
7295 let rec checkfds = function
7296 | [] -> ()
7297 | fd :: rest when fd = state.sr ->
7298 let cmd = readcmd state.sr in
7299 act cmd;
7300 checkfds rest
7302 | fd :: rest when fd = state.wsfd ->
7303 Wsi.readresp fd;
7304 checkfds rest
7306 | fd :: rest when Some fd = !optrfd ->
7307 begin match remote fd with
7308 | None -> optrfd := remoteopen !rcmdpath;
7309 | opt -> optrfd := opt
7310 end;
7311 checkfds rest
7313 | fd :: rest ->
7314 let s = String.create 80 in
7315 let n = tempfailureretry (Unix.read fd s 0) 80 in
7316 if conf.redirectstderr
7317 then (
7318 Buffer.add_substring state.errmsgs s 0 n;
7319 state.newerrmsgs <- true;
7320 state.redisplay <- true;
7322 else (
7323 prerr_string (String.sub s 0 n);
7324 flush stderr;
7326 checkfds rest
7328 checkfds l;
7329 let newdeadline =
7330 let deadline1 =
7331 if deadline = infinity
7332 then now () +. 0.01
7333 else deadline
7335 match state.autoscroll with
7336 | Some step when step != 0 -> deadline1
7337 | _ -> if state.ghyll == noghyll then infinity else deadline1
7339 loop newdeadline
7340 end;
7343 loop infinity;
7344 with Quit ->
7345 Config.save ();