Shrug
[llpp.git] / main.ml
blob3f0b11a4d9d877b1eb96054880157bbc6df4ad5c
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * proportional * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable savebmarks : bool
316 ; mutable proportional : proportional
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 method eformsgs : bool
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 method eformsgs = false
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
485 and hists =
486 { pat : string circbuf
487 ; pag : string circbuf
488 ; nav : anchor circbuf
489 ; sel : string circbuf
493 let defconf =
494 { scrollbw = 7
495 ; scrollh = 12
496 ; icase = true
497 ; preload = true
498 ; pagebias = 0
499 ; verbose = false
500 ; debug = false
501 ; scrollstep = 24
502 ; hscrollstep = 24
503 ; maxhfit = true
504 ; crophack = false
505 ; autoscrollstep = 2
506 ; maxwait = None
507 ; hlinks = false
508 ; underinfo = false
509 ; interpagespace = 2
510 ; zoom = 1.0
511 ; presentation = false
512 ; angle = 0
513 ; cwinw = 900
514 ; cwinh = 900
515 ; savebmarks = true
516 ; proportional = true
517 ; trimmargins = false
518 ; trimfuzz = (0,0,0,0)
519 ; memlimit = 32 lsl 20
520 ; texcount = 256
521 ; sliceheight = 24
522 ; thumbw = 76
523 ; jumpback = true
524 ; bgcolor = (0.5, 0.5, 0.5)
525 ; bedefault = false
526 ; scrollbarinpm = true
527 ; tilew = 2048
528 ; tileh = 2048
529 ; mustoresize = 256 lsl 20
530 ; checkers = true
531 ; aalevel = 8
532 ; urilauncher =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
536 | Posx -> "open \"%s\""
537 | Pcygwin -> "cygstart \"%s\""
538 | Punknown -> "echo %s")
539 ; pathlauncher = "lp \"%s\""
540 ; selcmd =
541 (match platform with
542 | Plinux | Pfreebsd | Pdragonflybsd
543 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
544 | Posx -> "pbcopy"
545 | Pcygwin -> "wsel"
546 | Punknown -> "cat")
547 ; colorspace = Rgb
548 ; invert = false
549 ; colorscale = 1.0
550 ; redirectstderr = false
551 ; ghyllscroll = None
552 ; columns = Csingle [||]
553 ; beyecolumns = None
554 ; updatecurs = false
555 ; hfsize = 12
556 ; pgscale = 1.0
557 ; usepbo = false
558 ; wheelbypage = false
559 ; stcmd = "echo SyncTex"
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let wtmode = ref false;;
577 let findkeyhash c name =
578 try List.assoc name c.keyhashes
579 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
582 let conf = { defconf with angle = defconf.angle };;
584 let pgscale h = truncate (float h *. conf.pgscale);;
586 type fontstate =
587 { mutable fontsize : int
588 ; mutable wwidth : float
589 ; mutable maxrows : int
593 let fstate =
594 { fontsize = 14
595 ; wwidth = nan
596 ; maxrows = -1
600 let geturl s =
601 let colonpos = try String.index s ':' with Not_found -> -1 in
602 let len = String.length s in
603 if colonpos >= 0 && colonpos + 3 < len
604 then (
605 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
606 then
607 let schemestartpos =
608 try String.rindex_from s colonpos ' '
609 with Not_found -> -1
611 let scheme =
612 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
614 match scheme with
615 | "http" | "ftp" | "mailto" ->
616 let epos =
617 try String.index_from s colonpos ' '
618 with Not_found -> len
620 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
621 | _ -> ""
622 else ""
624 else ""
627 let gotouri uri =
628 if String.length conf.urilauncher = 0
629 then print_endline uri
630 else (
631 let url = geturl uri in
632 if String.length url = 0
633 then print_endline uri
634 else
635 let re = Str.regexp "%s" in
636 let command = Str.global_replace re url conf.urilauncher in
637 try popen command []
638 with exn ->
639 Printf.eprintf
640 "failed to execute `%s': %s\n" command (exntos exn);
641 flush stderr;
645 let version () =
646 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
647 (platform_to_string platform) Sys.word_size Sys.ocaml_version
650 let makehelp () =
651 let strings = version () :: "" :: Help.keys in
652 Array.of_list (
653 List.map (fun s ->
654 let url = geturl s in
655 if String.length url > 0
656 then (s, 0, Action (fun u -> gotouri url; u))
657 else (s, 0, Noaction)
658 ) strings);
661 let noghyll _ = ();;
662 let firstgeomcmds = "", [];;
663 let noreprf () = ();;
665 let state =
666 { sr = Unix.stdin
667 ; sw = Unix.stdin
668 ; wsfd = Unix.stdin
669 ; errfd = None
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; scrollw = 0
677 ; hscrollh = 0
678 ; anchor = emptyanchor
679 ; ranchors = []
680 ; layout = []
681 ; maxy = max_int
682 ; tilelru = Queue.create ()
683 ; pagemap = Hashtbl.create 10
684 ; tilemap = Hashtbl.create 10
685 ; pdims = []
686 ; pagecount = 0
687 ; currently = Idle
688 ; mstate = Mnone
689 ; rects = []
690 ; rects1 = []
691 ; text = ""
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = ""
695 ; outlines = [||]
696 ; bookmarks = []
697 ; path = ""
698 ; password = ""
699 ; nameddest = ""
700 ; geomcmds = firstgeomcmds
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 ""
704 ; pag = cbnew 10 ""
705 ; sel = cbnew 10 ""
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = makehelp ()
713 ; docinfo = []
714 ; texid = None
715 ; prevzoom = 1.0
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; winw = -1
724 ; winh = -1
725 ; reprf = noreprf
729 let setfontsize n =
730 fstate.fontsize <- n;
731 fstate.wwidth <- measurestr fstate.fontsize "w";
732 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
735 let vlog fmt =
736 if conf.verbose
737 then
738 Printf.kprintf prerr_endline fmt
739 else
740 Printf.kprintf ignore fmt
743 let launchpath () =
744 if String.length conf.pathlauncher = 0
745 then print_endline state.path
746 else (
747 let re = Str.regexp "%s" in
748 let command = Str.global_replace re state.path conf.pathlauncher in
749 try popen command []
750 with exn ->
751 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
752 flush stderr;
756 module Ne = struct
757 type 'a t = | Res of 'a | Exn of exn;;
759 let pipe () =
760 try Res (Unix.pipe ())
761 with exn -> Exn exn
764 let clo fd f =
765 try tempfailureretry Unix.close fd
766 with exn -> f (exntos exn)
769 let dup fd =
770 try Res (tempfailureretry Unix.dup fd)
771 with exn -> Exn exn
774 let dup2 fd1 fd2 =
775 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
776 with exn -> Exn exn
778 end;;
780 let redirectstderr () =
781 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
782 if conf.redirectstderr
783 then
784 match Ne.pipe () with
785 | Ne.Exn exn ->
786 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
788 | Ne.Res (r, w) ->
789 begin match Ne.dup Unix.stderr with
790 | Ne.Exn exn ->
791 dolog "failed to dup stderr: %s" (exntos exn);
792 Ne.clo r (clofail "pipe/r");
793 Ne.clo w (clofail "pipe/w");
795 | Ne.Res dupstderr ->
796 begin match Ne.dup2 w Unix.stderr with
797 | Ne.Exn exn ->
798 dolog "failed to dup2 to stderr: %s" (exntos exn);
799 Ne.clo dupstderr (clofail "stderr duplicate");
800 Ne.clo r (clofail "redir pipe/r");
801 Ne.clo w (clofail "redir pipe/w");
803 | Ne.Res () ->
804 state.stderr <- dupstderr;
805 state.errfd <- Some r;
806 end;
808 else (
809 state.newerrmsgs <- false;
810 begin match state.errfd with
811 | Some fd ->
812 begin match Ne.dup2 state.stderr Unix.stderr with
813 | Ne.Exn exn ->
814 dolog "failed to dup2 original stderr: %s" (exntos exn)
815 | Ne.Res () ->
816 Ne.clo fd (clofail "dup of stderr");
817 state.errfd <- None;
818 end;
819 | None -> ()
820 end;
821 prerr_string (Buffer.contents state.errmsgs);
822 flush stderr;
823 Buffer.clear state.errmsgs;
827 module G =
828 struct
829 let postRedisplay who =
830 if conf.verbose
831 then prerr_endline ("redisplay for " ^ who);
832 state.redisplay <- true;
834 end;;
836 let getopaque pageno =
837 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
838 with Not_found -> None
841 let putopaque pageno opaque =
842 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
845 let pagetranslatepoint l x y =
846 let dy = y - l.pagedispy in
847 let y = dy + l.pagey in
848 let dx = x - l.pagedispx in
849 let x = dx + l.pagex in
850 (x, y);
853 let onppundermouse g x y d =
854 let rec f = function
855 | l :: rest ->
856 begin match getopaque l.pageno with
857 | Some opaque ->
858 let x0 = l.pagedispx in
859 let x1 = x0 + l.pagevw in
860 let y0 = l.pagedispy in
861 let y1 = y0 + l.pagevh in
862 if y >= y0 && y <= y1 && x >= x0 && x <= x1
863 then
864 let px, py = pagetranslatepoint l x y in
865 match g opaque l px py with
866 | Some res -> res
867 | None -> f rest
868 else f rest
869 | _ ->
870 f rest
872 | [] -> d
874 f state.layout
877 let getunder x y =
878 let g opaque _ px py =
879 match whatsunder opaque px py with
880 | Unone -> None
881 | under -> Some under
883 onppundermouse g x y Unone
886 let unproject x y =
887 let g opaque l x y =
888 match unproject opaque x y with
889 | Some (x, y) -> Some (Some (l.pageno, x, y))
890 | None -> None
892 onppundermouse g x y None;
895 let showtext c s =
896 state.text <- Printf.sprintf "%c%s" c s;
897 G.postRedisplay "showtext";
900 let undertext = function
901 | Unone -> "none"
902 | Ulinkuri s -> s
903 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
904 | Utext s -> "font: " ^ s
905 | Uunexpected s -> "unexpected: " ^ s
906 | Ulaunch s -> "launch: " ^ s
907 | Unamed s -> "named: " ^ s
908 | Uremote (filename, pageno) ->
909 Printf.sprintf "%s: page %d" filename (pageno+1)
912 let updateunder x y =
913 match getunder x y with
914 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
915 | Ulinkuri uri ->
916 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
917 Wsi.setcursor Wsi.CURSOR_INFO
918 | Ulinkgoto (pageno, _) ->
919 if conf.underinfo
920 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
921 Wsi.setcursor Wsi.CURSOR_INFO
922 | Utext s ->
923 if conf.underinfo then showtext 'f' ("ont: " ^ s);
924 Wsi.setcursor Wsi.CURSOR_TEXT
925 | Uunexpected s ->
926 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
927 Wsi.setcursor Wsi.CURSOR_INHERIT
928 | Ulaunch s ->
929 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
930 Wsi.setcursor Wsi.CURSOR_INHERIT
931 | Unamed s ->
932 if conf.underinfo then showtext 'n' ("amed: " ^ s);
933 Wsi.setcursor Wsi.CURSOR_INHERIT
934 | Uremote (filename, pageno) ->
935 if conf.underinfo then showtext 'r'
936 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
937 Wsi.setcursor Wsi.CURSOR_INFO
940 let showlinktype under =
941 if conf.underinfo
942 then
943 match under with
944 | Unone -> ()
945 | under ->
946 let s = undertext under in
947 showtext ' ' s
950 let addchar s c =
951 let b = Buffer.create (String.length s + 1) in
952 Buffer.add_string b s;
953 Buffer.add_char b c;
954 Buffer.contents b;
957 let colorspace_of_string s =
958 match String.lowercase s with
959 | "rgb" -> Rgb
960 | "bgr" -> Bgr
961 | "gray" -> Gray
962 | _ -> failwith "invalid colorspace"
965 let int_of_colorspace = function
966 | Rgb -> 0
967 | Bgr -> 1
968 | Gray -> 2
971 let colorspace_of_int = function
972 | 0 -> Rgb
973 | 1 -> Bgr
974 | 2 -> Gray
975 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
978 let colorspace_to_string = function
979 | Rgb -> "rgb"
980 | Bgr -> "bgr"
981 | Gray -> "gray"
984 let intentry_with_suffix text key =
985 let c =
986 if key >= 32 && key < 127
987 then Char.chr key
988 else '\000'
990 match Char.lowercase c with
991 | '0' .. '9' ->
992 let text = addchar text c in
993 TEcont text
995 | 'k' | 'm' | 'g' ->
996 let text = addchar text c in
997 TEcont text
999 | _ ->
1000 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1001 TEcont text
1004 let multicolumns_to_string (n, a, b) =
1005 if a = 0 && b = 0
1006 then Printf.sprintf "%d" n
1007 else Printf.sprintf "%d,%d,%d" n a b;
1010 let multicolumns_of_string s =
1012 (int_of_string s, 0, 0)
1013 with _ ->
1014 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1015 if a > 1 || b > 1
1016 then failwith "subtly broken"; (n, a, b)
1020 let readcmd fd =
1021 let s = "xxxx" in
1022 let n = tempfailureretry (Unix.read fd s 0) 4 in
1023 if n != 4 then failwith "incomplete read(len)";
1024 let len = 0
1025 lor (Char.code s.[0] lsl 24)
1026 lor (Char.code s.[1] lsl 16)
1027 lor (Char.code s.[2] lsl 8)
1028 lor (Char.code s.[3] lsl 0)
1030 let s = String.create len in
1031 let n = tempfailureretry (Unix.read fd s 0) len in
1032 if n != len then failwith "incomplete read(data)";
1036 let btod b = if b then 1 else 0;;
1038 let wcmd fmt =
1039 let b = Buffer.create 16 in
1040 Buffer.add_string b "llll";
1041 Printf.kbprintf
1042 (fun b ->
1043 let s = Buffer.contents b in
1044 let n = String.length s in
1045 let len = n - 4 in
1046 (* dolog "wcmd %S" (String.sub s 4 len); *)
1047 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1048 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1049 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1050 s.[3] <- Char.chr (len land 0xff);
1051 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1052 if n' != n then failwith "write failed";
1053 ) b fmt;
1056 let calcips h =
1057 let d = state.winh - h in
1058 max conf.interpagespace ((d + 1) / 2)
1061 let rowyh (c, coverA, coverB) b n =
1062 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1063 then
1064 let _, _, vy, (_, _, h, _) = b.(n) in
1065 (vy, h)
1066 else
1067 let n' = n - coverA in
1068 let d = n' mod c in
1069 let s = n - d in
1070 let e = min state.pagecount (s + c) in
1071 let rec find m miny maxh = if m = e then miny, maxh else
1072 let _, _, y, (_, _, h, _) = b.(m) in
1073 let miny = min miny y in
1074 let maxh = max maxh h in
1075 find (m+1) miny maxh
1076 in find s max_int 0
1079 let calcheight () =
1080 match conf.columns with
1081 | Cmulti ((_, _, _) as cl, b) ->
1082 if Array.length b > 0
1083 then
1084 let y, h = rowyh cl b (Array.length b - 1) in
1085 y + h + (if conf.presentation then calcips h else 0)
1086 else 0
1087 | Csingle b ->
1088 if Array.length b > 0
1089 then
1090 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1091 y + h + (if conf.presentation then calcips h else 0)
1092 else 0
1093 | Csplit (_, b) ->
1094 if Array.length b > 0
1095 then
1096 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1097 y + h
1098 else 0
1101 let getpageyh pageno =
1102 let pageno = bound pageno 0 (state.pagecount-1) in
1103 match conf.columns with
1104 | Csingle b ->
1105 if Array.length b = 0
1106 then 0, 0
1107 else
1108 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1109 let y =
1110 if conf.presentation
1111 then y - calcips h
1112 else y
1114 y, h
1115 | Cmulti (cl, b) ->
1116 if Array.length b = 0
1117 then 0, 0
1118 else
1119 let y, h = rowyh cl b pageno in
1120 let y =
1121 if conf.presentation
1122 then y - calcips h
1123 else y
1125 y, h
1126 | Csplit (c, b) ->
1127 if Array.length b = 0
1128 then 0, 0
1129 else
1130 let n = pageno*c in
1131 let (_, _, y, (_, _, h, _)) = b.(n) in
1132 y, h
1135 let getpagedim pageno =
1136 let rec f ppdim l =
1137 match l with
1138 | (n, _, _, _) as pdim :: rest ->
1139 if n >= pageno
1140 then (if n = pageno then pdim else ppdim)
1141 else f pdim rest
1143 | [] -> ppdim
1145 f (-1, -1, -1, -1) state.pdims
1148 let getpagey pageno = fst (getpageyh pageno);;
1150 let nogeomcmds cmds =
1151 match cmds with
1152 | s, [] -> String.length s = 0
1153 | _ -> false
1156 let page_of_y y =
1157 let ((c, coverA, coverB) as cl), b =
1158 match conf.columns with
1159 | Csingle b -> (1, 0, 0), b
1160 | Cmulti (c, b) -> c, b
1161 | Csplit (_, b) -> (1, 0, 0), b
1163 if Array.length b = 0
1164 then -1
1165 else
1166 let rec bsearch nmin nmax =
1167 if nmin > nmax
1168 then bound nmin 0 (state.pagecount-1)
1169 else
1170 let n = (nmax + nmin) / 2 in
1171 let vy, h = rowyh cl b n in
1172 let y0, y1 =
1173 if conf.presentation
1174 then
1175 let ips = calcips h in
1176 let y0 = vy - ips in
1177 let y1 = vy + h + ips in
1178 y0, y1
1179 else (
1180 if n = 0
1181 then 0, vy + h + conf.interpagespace
1182 else
1183 let y0 = vy - conf.interpagespace in
1184 y0, y0 + h + conf.interpagespace
1187 if y >= y0 && y < y1
1188 then (
1189 if c = 1
1190 then n
1191 else (
1192 if n > coverA
1193 then
1194 if n < state.pagecount - coverB
1195 then ((n-coverA)/c)*c + coverA
1196 else n
1197 else n
1200 else (
1201 if y > y0
1202 then bsearch (n+1) nmax
1203 else bsearch nmin (n-1)
1206 let r = bsearch 0 (state.pagecount-1) in
1210 let layoutN ((columns, coverA, coverB), b) y sh =
1211 let sh = sh - state.hscrollh in
1212 let rec fold accu n =
1213 if n = Array.length b
1214 then accu
1215 else
1216 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1217 if (vy - y) > sh &&
1218 (n = coverA - 1
1219 || n = state.pagecount - coverB
1220 || (n - coverA) mod columns = columns - 1)
1221 then accu
1222 else
1223 let accu =
1224 if vy + h > y
1225 then
1226 let pagey = max 0 (y - vy) in
1227 let pagedispy = if pagey > 0 then 0 else vy - y in
1228 let pagedispx, pagex =
1229 let pdx =
1230 if n = coverA - 1 || n = state.pagecount - coverB
1231 then state.x + (state.winw - state.scrollw - w) / 2
1232 else dx + xoff + state.x
1234 if pdx < 0
1235 then 0, -pdx
1236 else pdx, 0
1238 let pagevw =
1239 let vw = state.winw - state.scrollw - pagedispx in
1240 let pw = w - pagex in
1241 min vw pw
1243 let pagevh = min (h - pagey) (sh - pagedispy) in
1244 if pagevw > 0 && pagevh > 0
1245 then
1246 let e =
1247 { pageno = n
1248 ; pagedimno = pdimno
1249 ; pagew = w
1250 ; pageh = h
1251 ; pagex = pagex
1252 ; pagey = pagey
1253 ; pagevw = pagevw
1254 ; pagevh = pagevh
1255 ; pagedispx = pagedispx
1256 ; pagedispy = pagedispy
1257 ; pagecol = 0
1260 e :: accu
1261 else
1262 accu
1263 else
1264 accu
1266 fold accu (n+1)
1268 List.rev (fold [] (page_of_y y));
1271 let layoutS (columns, b) y sh =
1272 let sh = sh - state.hscrollh in
1273 let rec fold accu n =
1274 if n = Array.length b
1275 then accu
1276 else
1277 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1278 if (vy - y) > sh
1279 then accu
1280 else
1281 let accu =
1282 if vy + pageh > y
1283 then
1284 let x = xoff + state.x in
1285 let pagey = max 0 (y - vy) in
1286 let pagedispy = if pagey > 0 then 0 else vy - y in
1287 let pagedispx, pagex =
1288 if px = 0
1289 then (
1290 if x < 0
1291 then 0, -x
1292 else x, 0
1294 else (
1295 let px = px - x in
1296 if px < 0
1297 then -px, 0
1298 else 0, px
1301 let pagecolw = pagew/columns in
1302 let pagedispx =
1303 if pagecolw < state.winw
1304 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1305 else pagedispx
1307 let pagevw =
1308 let vw = state.winw - pagedispx - state.scrollw in
1309 let pw = pagew - pagex in
1310 min vw pw
1312 let pagevw = min pagevw pagecolw in
1313 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1314 if pagevw > 0 && pagevh > 0
1315 then
1316 let e =
1317 { pageno = n/columns
1318 ; pagedimno = pdimno
1319 ; pagew = pagew
1320 ; pageh = pageh
1321 ; pagex = pagex
1322 ; pagey = pagey
1323 ; pagevw = pagevw
1324 ; pagevh = pagevh
1325 ; pagedispx = pagedispx
1326 ; pagedispy = pagedispy
1327 ; pagecol = n mod columns
1330 e :: accu
1331 else
1332 accu
1333 else
1334 accu
1336 fold accu (n+1)
1338 List.rev (fold [] 0)
1341 let layout y sh =
1342 if nogeomcmds state.geomcmds
1343 then
1344 match conf.columns with
1345 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1346 | Cmulti c -> layoutN c y sh
1347 | Csplit s -> layoutS s y sh
1348 else []
1351 let clamp incr =
1352 let y = state.y + incr in
1353 let y = max 0 y in
1354 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1358 let itertiles l f =
1359 let tilex = l.pagex mod conf.tilew in
1360 let tiley = l.pagey mod conf.tileh in
1362 let col = l.pagex / conf.tilew in
1363 let row = l.pagey / conf.tileh in
1365 let rec rowloop row y0 dispy h =
1366 if h = 0
1367 then ()
1368 else (
1369 let dh = conf.tileh - y0 in
1370 let dh = min h dh in
1371 let rec colloop col x0 dispx w =
1372 if w = 0
1373 then ()
1374 else (
1375 let dw = conf.tilew - x0 in
1376 let dw = min w dw in
1378 f col row dispx dispy x0 y0 dw dh;
1379 colloop (col+1) 0 (dispx+dw) (w-dw)
1382 colloop col tilex l.pagedispx l.pagevw;
1383 rowloop (row+1) 0 (dispy+dh) (h-dh)
1386 if l.pagevw > 0 && l.pagevh > 0
1387 then rowloop row tiley l.pagedispy l.pagevh;
1390 let gettileopaque l col row =
1391 let key =
1392 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1394 try Some (Hashtbl.find state.tilemap key)
1395 with Not_found -> None
1398 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1399 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1400 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1403 let drawtiles l color =
1404 GlDraw.color color;
1405 let f col row x y tilex tiley w h =
1406 match gettileopaque l col row with
1407 | Some (opaque, _, t) ->
1408 let params = x, y, w, h, tilex, tiley in
1409 if conf.invert
1410 then (
1411 Gl.enable `blend;
1412 GlFunc.blend_func `zero `one_minus_src_color;
1414 drawtile params opaque;
1415 if conf.invert
1416 then Gl.disable `blend;
1417 if conf.debug
1418 then (
1419 let s = Printf.sprintf
1420 "%d[%d,%d] %f sec"
1421 l.pageno col row t
1423 let w = measurestr fstate.fontsize s in
1424 GlMisc.push_attrib [`current];
1425 GlDraw.color (0.0, 0.0, 0.0);
1426 GlDraw.rect
1427 (float (x-2), float (y-2))
1428 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1429 GlDraw.color (1.0, 1.0, 1.0);
1430 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1431 GlMisc.pop_attrib ();
1434 | _ ->
1435 let w =
1436 let lw = state.winw - state.scrollw - x in
1437 min lw w
1438 and h =
1439 let lh = state.winh - y in
1440 min lh h
1442 begin match state.texid with
1443 | Some id ->
1444 Gl.enable `texture_2d;
1445 GlTex.bind_texture `texture_2d id;
1446 let x0 = float x
1447 and y0 = float y
1448 and x1 = float (x+w)
1449 and y1 = float (y+h) in
1451 let tw = float w /. 16.0
1452 and th = float h /. 16.0 in
1453 let tx0 = float tilex /. 16.0
1454 and ty0 = float tiley /. 16.0 in
1455 let tx1 = tx0 +. tw
1456 and ty1 = ty0 +. th in
1457 GlDraw.begins `quads;
1458 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1459 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1460 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1461 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1462 GlDraw.ends ();
1464 Gl.disable `texture_2d;
1465 | None ->
1466 GlDraw.color (1.0, 1.0, 1.0);
1467 GlDraw.rect
1468 (float x, float y)
1469 (float (x+w), float (y+h));
1470 end;
1471 if w > 128 && h > fstate.fontsize + 10
1472 then (
1473 GlDraw.color (0.0, 0.0, 0.0);
1474 let c, r =
1475 if conf.verbose
1476 then (col*conf.tilew, row*conf.tileh)
1477 else col, row
1479 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1481 GlDraw.color color;
1483 itertiles l f
1486 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1488 let tilevisible1 l x y =
1489 let ax0 = l.pagex
1490 and ax1 = l.pagex + l.pagevw
1491 and ay0 = l.pagey
1492 and ay1 = l.pagey + l.pagevh in
1494 let bx0 = x
1495 and by0 = y in
1496 let bx1 = min (bx0 + conf.tilew) l.pagew
1497 and by1 = min (by0 + conf.tileh) l.pageh in
1499 let rx0 = max ax0 bx0
1500 and ry0 = max ay0 by0
1501 and rx1 = min ax1 bx1
1502 and ry1 = min ay1 by1 in
1504 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1505 nonemptyintersection
1508 let tilevisible layout n x y =
1509 let rec findpageinlayout m = function
1510 | l :: rest when l.pageno = n ->
1511 tilevisible1 l x y || (
1512 match conf.columns with
1513 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1514 | _ -> false
1516 | _ :: rest -> findpageinlayout 0 rest
1517 | [] -> false
1519 findpageinlayout 0 layout;
1522 let tileready l x y =
1523 tilevisible1 l x y &&
1524 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1527 let tilepage n p layout =
1528 let rec loop = function
1529 | l :: rest ->
1530 if l.pageno = n
1531 then
1532 let f col row _ _ _ _ _ _ =
1533 if state.currently = Idle
1534 then
1535 match gettileopaque l col row with
1536 | Some _ -> ()
1537 | None ->
1538 let x = col*conf.tilew
1539 and y = row*conf.tileh in
1540 let w =
1541 let w = l.pagew - x in
1542 min w conf.tilew
1544 let h =
1545 let h = l.pageh - y in
1546 min h conf.tileh
1548 let pbo =
1549 if conf.usepbo
1550 then getpbo w h conf.colorspace
1551 else "0"
1553 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1554 state.currently <-
1555 Tiling (
1556 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1557 conf.tilew, conf.tileh
1560 itertiles l f;
1561 else
1562 loop rest
1564 | [] -> ()
1566 if nogeomcmds state.geomcmds
1567 then loop layout;
1570 let preloadlayout y =
1571 let y = if y < state.winh then 0 else y - state.winh in
1572 let h = state.winh*3 in
1573 layout y h;
1576 let load pages =
1577 let rec loop pages =
1578 if state.currently != Idle
1579 then ()
1580 else
1581 match pages with
1582 | l :: rest ->
1583 begin match getopaque l.pageno with
1584 | None ->
1585 wcmd "page %d %d" l.pageno l.pagedimno;
1586 state.currently <- Loading (l, state.gen);
1587 | Some opaque ->
1588 tilepage l.pageno opaque pages;
1589 loop rest
1590 end;
1591 | _ -> ()
1593 if nogeomcmds state.geomcmds
1594 then loop pages
1597 let preload pages =
1598 load pages;
1599 if conf.preload && state.currently = Idle
1600 then load (preloadlayout state.y);
1603 let layoutready layout =
1604 let rec fold all ls =
1605 all && match ls with
1606 | l :: rest ->
1607 let seen = ref false in
1608 let allvisible = ref true in
1609 let foo col row _ _ _ _ _ _ =
1610 seen := true;
1611 allvisible := !allvisible &&
1612 begin match gettileopaque l col row with
1613 | Some _ -> true
1614 | None -> false
1617 itertiles l foo;
1618 fold (!seen && !allvisible) rest
1619 | [] -> true
1621 let alltilesvisible = fold true layout in
1622 alltilesvisible;
1625 let gotoy y =
1626 let y = bound y 0 state.maxy in
1627 let y, layout, proceed =
1628 match conf.maxwait with
1629 | Some time when state.ghyll == noghyll ->
1630 begin match state.throttle with
1631 | None ->
1632 let layout = layout y state.winh in
1633 let ready = layoutready layout in
1634 if not ready
1635 then (
1636 load layout;
1637 state.throttle <- Some (layout, y, now ());
1639 else G.postRedisplay "gotoy showall (None)";
1640 y, layout, ready
1641 | Some (_, _, started) ->
1642 let dt = now () -. started in
1643 if dt > time
1644 then (
1645 state.throttle <- None;
1646 let layout = layout y state.winh in
1647 load layout;
1648 G.postRedisplay "maxwait";
1649 y, layout, true
1651 else -1, [], false
1654 | _ ->
1655 let layout = layout y state.winh in
1656 if not !wtmode || layoutready layout
1657 then G.postRedisplay "gotoy ready";
1658 y, layout, true
1660 if proceed
1661 then (
1662 state.y <- y;
1663 state.layout <- layout;
1664 begin match state.mode with
1665 | LinkNav (Ltexact (pageno, linkno)) ->
1666 let rec loop = function
1667 | [] ->
1668 state.mode <- LinkNav (Ltgendir 0)
1669 | l :: _ when l.pageno = pageno ->
1670 begin match getopaque pageno with
1671 | None ->
1672 state.mode <- LinkNav (Ltgendir 0)
1673 | Some opaque ->
1674 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1675 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1676 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1677 then state.mode <- LinkNav (Ltgendir 0)
1679 | _ :: rest -> loop rest
1681 loop layout
1682 | _ -> ()
1683 end;
1684 begin match state.mode with
1685 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1686 if not (pagevisible layout pageno)
1687 then (
1688 match state.layout with
1689 | [] -> ()
1690 | l :: _ ->
1691 state.mode <- Birdseye (
1692 conf, leftx, l.pageno, hooverpageno, anchor
1695 | LinkNav (Ltgendir dir as lt) ->
1696 let linknav =
1697 let rec loop = function
1698 | [] -> lt
1699 | l :: rest ->
1700 match getopaque l.pageno with
1701 | None -> loop rest
1702 | Some opaque ->
1703 let link =
1704 let ld =
1705 if dir = 0
1706 then LDfirstvisible (l.pagex, l.pagey, dir)
1707 else (
1708 if dir > 0 then LDfirst else LDlast
1711 findlink opaque ld
1713 match link with
1714 | Lnotfound -> loop rest
1715 | Lfound n ->
1716 showlinktype (getlink opaque n);
1717 Ltexact (l.pageno, n)
1719 loop state.layout
1721 state.mode <- LinkNav linknav
1722 | _ -> ()
1723 end;
1724 preload layout;
1726 state.ghyll <- noghyll;
1727 if conf.updatecurs
1728 then (
1729 let mx, my = state.mpos in
1730 updateunder mx my;
1734 let conttiling pageno opaque =
1735 tilepage pageno opaque
1736 (if conf.preload then preloadlayout state.y else state.layout)
1739 let gotoy_and_clear_text y =
1740 if not conf.verbose then state.text <- "";
1741 gotoy y;
1744 let getanchor1 l =
1745 let top =
1746 let coloff = l.pagecol * l.pageh in
1747 float (l.pagey + coloff) /. float l.pageh
1749 let dtop =
1750 if l.pagedispy = 0
1751 then
1753 else
1754 if conf.presentation
1755 then float l.pagedispy /. float (calcips l.pageh)
1756 else float l.pagedispy /. float conf.interpagespace
1758 (l.pageno, top, dtop)
1761 let getanchor () =
1762 match state.layout with
1763 | l :: _ -> getanchor1 l
1764 | [] ->
1765 let n = page_of_y state.y in
1766 if n = -1
1767 then state.anchor
1768 else
1769 let y, h = getpageyh n in
1770 let dy = y - state.y in
1771 let dtop =
1772 if conf.presentation
1773 then
1774 let ips = calcips h in
1775 float (dy + ips) /. float ips
1776 else
1777 float dy /. float conf.interpagespace
1779 (n, 0.0, dtop)
1782 let getanchory (n, top, dtop) =
1783 let y, h = getpageyh n in
1784 if conf.presentation
1785 then
1786 let ips = calcips h in
1787 y + truncate (top*.float h -. dtop*.float ips) + ips;
1788 else
1789 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1792 let gotoanchor anchor =
1793 gotoy (getanchory anchor);
1796 let addnav () =
1797 cbput state.hists.nav (getanchor ());
1800 let getnav dir =
1801 let anchor = cbgetc state.hists.nav dir in
1802 getanchory anchor;
1805 let gotoghyll y =
1806 let scroll f n a b =
1807 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1808 let snake f a b =
1809 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1810 if f < a
1811 then s (float f /. float a)
1812 else (
1813 if f > b
1814 then 1.0 -. s ((float (f-b) /. float (n-b)))
1815 else 1.0
1818 snake f a b
1819 and summa f n a b =
1820 (* courtesy:
1821 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1822 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1823 let iv1 = iv f in
1824 let ins = float a *. iv1
1825 and outs = float (n-b) *. iv1 in
1826 let ones = b - a in
1827 ins +. outs +. float ones
1829 let rec set (_N, _A, _B) y sy =
1830 let sum = summa 1.0 _N _A _B in
1831 let dy = float (y - sy) in
1832 state.ghyll <- (
1833 let rec gf n y1 o =
1834 if n >= _N
1835 then state.ghyll <- noghyll
1836 else
1837 let go n =
1838 let s = scroll n _N _A _B in
1839 let y1 = y1 +. ((s *. dy) /. sum) in
1840 gotoy_and_clear_text (truncate y1);
1841 state.ghyll <- gf (n+1) y1;
1843 match o with
1844 | None -> go n
1845 | Some y' -> set (_N/2, 1, 1) y' state.y
1847 gf 0 (float state.y)
1850 match conf.ghyllscroll with
1851 | None ->
1852 gotoy_and_clear_text y
1853 | Some nab ->
1854 if state.ghyll == noghyll
1855 then set nab y state.y
1856 else state.ghyll (Some y)
1859 let gotopage n top =
1860 let y, h = getpageyh n in
1861 let y = y + (truncate (top *. float h)) in
1862 gotoghyll y
1865 let gotopage1 n top =
1866 let y = getpagey n in
1867 let y = y + top in
1868 gotoghyll y
1871 let invalidate s f =
1872 state.layout <- [];
1873 state.pdims <- [];
1874 state.rects <- [];
1875 state.rects1 <- [];
1876 match state.geomcmds with
1877 | ps, [] when String.length ps = 0 ->
1878 f ();
1879 state.geomcmds <- s, [];
1881 | ps, [] ->
1882 state.geomcmds <- ps, [s, f];
1884 | ps, (s', _) :: rest when s' = s ->
1885 state.geomcmds <- ps, ((s, f) :: rest);
1887 | ps, cmds ->
1888 state.geomcmds <- ps, ((s, f) :: cmds);
1891 let flushpages () =
1892 Hashtbl.iter (fun _ opaque ->
1893 wcmd "freepage %s" opaque;
1894 ) state.pagemap;
1895 Hashtbl.clear state.pagemap;
1898 let flushtiles () =
1899 if not (Queue.is_empty state.tilelru)
1900 then (
1901 Queue.iter (fun (k, p, s) ->
1902 wcmd "freetile %s" p;
1903 state.memused <- state.memused - s;
1904 Hashtbl.remove state.tilemap k;
1905 ) state.tilelru;
1906 state.uioh#infochanged Memused;
1907 Queue.clear state.tilelru;
1909 load state.layout;
1912 let opendoc path password =
1913 state.path <- path;
1914 state.password <- password;
1915 state.gen <- state.gen + 1;
1916 state.docinfo <- [];
1918 flushpages ();
1919 setaalevel conf.aalevel;
1920 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1921 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1922 invalidate "reqlayout"
1923 (fun () ->
1924 wcmd "reqlayout %d %d %s\000"
1925 conf.angle (btod conf.proportional) state.nameddest;
1929 let reload () =
1930 state.anchor <- getanchor ();
1931 opendoc state.path state.password;
1934 let scalecolor c =
1935 let c = c *. conf.colorscale in
1936 (c, c, c);
1939 let scalecolor2 (r, g, b) =
1940 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1943 let docolumns = function
1944 | Csingle _ ->
1945 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1946 let rec loop pageno pdimno pdim y ph pdims =
1947 if pageno = state.pagecount
1948 then ()
1949 else
1950 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1951 match pdims with
1952 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1953 pdimno+1, pdim, rest
1954 | _ ->
1955 pdimno, pdim, pdims
1957 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1958 let y = y +
1959 (if conf.presentation
1960 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1961 else (if pageno = 0 then 0 else conf.interpagespace)
1964 a.(pageno) <- (pdimno, x, y, pdim);
1965 loop (pageno+1) pdimno pdim (y + h) h pdims
1967 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1968 conf.columns <- Csingle a;
1970 | Cmulti ((columns, coverA, coverB), _) ->
1971 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1972 let rec loop pageno pdimno pdim x y rowh pdims =
1973 let rec fixrow m = if m = pageno then () else
1974 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1975 if h < rowh
1976 then (
1977 let y = y + (rowh - h) / 2 in
1978 a.(m) <- (pdimno, x, y, pdim);
1980 fixrow (m+1)
1982 if pageno = state.pagecount
1983 then fixrow (((pageno - 1) / columns) * columns)
1984 else
1985 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1986 match pdims with
1987 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1988 pdimno+1, pdim, rest
1989 | _ ->
1990 pdimno, pdim, pdims
1992 let x, y, rowh' =
1993 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1994 then (
1995 let x = (state.winw - state.scrollw - w) / 2 in
1996 let ips =
1997 if conf.presentation then calcips h else conf.interpagespace in
1998 x, y + ips + rowh, h
2000 else (
2001 if (pageno - coverA) mod columns = 0
2002 then (
2003 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2004 let y =
2005 if conf.presentation
2006 then
2007 let ips = calcips h in
2008 y + (if pageno = 0 then 0 else calcips rowh + ips)
2009 else
2010 y + (if pageno = 0 then 0 else conf.interpagespace)
2012 x, y + rowh, h
2014 else x, y, max rowh h
2017 let y =
2018 if pageno > 1 && (pageno - coverA) mod columns = 0
2019 then (
2020 let y =
2021 if pageno = columns && conf.presentation
2022 then (
2023 let ips = calcips rowh in
2024 for i = 0 to pred columns
2026 let (pdimno, x, y, pdim) = a.(i) in
2027 a.(i) <- (pdimno, x, y+ips, pdim)
2028 done;
2029 y+ips;
2031 else y
2033 fixrow (pageno - columns);
2036 else y
2038 a.(pageno) <- (pdimno, x, y, pdim);
2039 let x = x + w + xoff*2 + conf.interpagespace in
2040 loop (pageno+1) pdimno pdim x y rowh' pdims
2042 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2043 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2045 | Csplit (c, _) ->
2046 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2047 let rec loop pageno pdimno pdim y pdims =
2048 if pageno = state.pagecount
2049 then ()
2050 else
2051 let pdimno, ((_, w, h, _) as pdim), pdims =
2052 match pdims with
2053 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2054 pdimno+1, pdim, rest
2055 | _ ->
2056 pdimno, pdim, pdims
2058 let cw = w / c in
2059 let rec loop1 n x y =
2060 if n = c then y else (
2061 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2062 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2065 let y = loop1 0 0 y in
2066 loop (pageno+1) pdimno pdim y pdims
2068 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2069 conf.columns <- Csplit (c, a);
2072 let represent () =
2073 docolumns conf.columns;
2074 state.maxy <- calcheight ();
2075 state.hscrollh <-
2076 if state.x = 0 && state.w <= state.winw - state.scrollw
2077 then 0
2078 else state.scrollw
2080 if state.reprf == noreprf
2081 then (
2082 match state.mode with
2083 | Birdseye (_, _, pageno, _, _) ->
2084 let y, h = getpageyh pageno in
2085 let top = (state.winh - h) / 2 in
2086 gotoy (max 0 (y - top))
2087 | _ -> gotoanchor state.anchor
2089 else (
2090 state.reprf ();
2091 state.reprf <- noreprf;
2095 let reshape w h =
2096 GlDraw.viewport 0 0 w h;
2097 let firsttime = state.geomcmds == firstgeomcmds in
2098 if not firsttime && nogeomcmds state.geomcmds
2099 then state.anchor <- getanchor ();
2101 state.winw <- w;
2102 let w = truncate (float w *. conf.zoom) - state.scrollw in
2103 let w = max w 2 in
2104 state.winh <- h;
2105 setfontsize fstate.fontsize;
2106 GlMat.mode `modelview;
2107 GlMat.load_identity ();
2109 GlMat.mode `projection;
2110 GlMat.load_identity ();
2111 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2112 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2113 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2115 let relx =
2116 if conf.zoom <= 1.0
2117 then 0.0
2118 else float state.x /. float state.w
2120 invalidate "geometry"
2121 (fun () ->
2122 state.w <- w;
2123 if not firsttime
2124 then state.x <- truncate (relx *. float w);
2125 let w =
2126 match conf.columns with
2127 | Csingle _ -> w
2128 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2129 | Csplit (c, _) -> w * c
2131 wcmd "geometry %d %d" w h);
2134 let enttext () =
2135 let len = String.length state.text in
2136 let drawstring s =
2137 let hscrollh =
2138 match state.mode with
2139 | Textentry _
2140 | View ->
2141 let h, _, _ = state.uioh#scrollpw in
2143 | _ -> 0
2145 let rect x w =
2146 GlDraw.rect
2147 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2148 (x+.w, float (state.winh - hscrollh))
2151 let w = float (state.winw - state.scrollw - 1) in
2152 if state.progress >= 0.0 && state.progress < 1.0
2153 then (
2154 GlDraw.color (0.3, 0.3, 0.3);
2155 let w1 = w *. state.progress in
2156 rect 0.0 w1;
2157 GlDraw.color (0.0, 0.0, 0.0);
2158 rect w1 (w-.w1)
2160 else (
2161 GlDraw.color (0.0, 0.0, 0.0);
2162 rect 0.0 w;
2165 GlDraw.color (1.0, 1.0, 1.0);
2166 drawstring fstate.fontsize
2167 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2169 let s =
2170 match state.mode with
2171 | Textentry ((prefix, text, _, _, _, _), _) ->
2172 let s =
2173 if len > 0
2174 then
2175 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2176 else
2177 Printf.sprintf "%s%s_" prefix text
2181 | _ -> state.text
2183 let s =
2184 if state.newerrmsgs
2185 then (
2186 if not (istextentry state.mode) && state.uioh#eformsgs
2187 then
2188 let s1 = "(press 'e' to review error messasges)" in
2189 if String.length s > 0 then s ^ " " ^ s1 else s1
2190 else s
2192 else s
2194 if String.length s > 0
2195 then drawstring s
2198 let gctiles () =
2199 let len = Queue.length state.tilelru in
2200 let layout = lazy (
2201 match state.throttle with
2202 | None ->
2203 if conf.preload
2204 then preloadlayout state.y
2205 else state.layout
2206 | Some (layout, _, _) ->
2207 layout
2208 ) in
2209 let rec loop qpos =
2210 if state.memused <= conf.memlimit
2211 then ()
2212 else (
2213 if qpos < len
2214 then
2215 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2216 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2217 let (_, pw, ph, _) = getpagedim n in
2219 gen = state.gen
2220 && colorspace = conf.colorspace
2221 && angle = conf.angle
2222 && pagew = pw
2223 && pageh = ph
2224 && (
2225 let x = col*conf.tilew
2226 and y = row*conf.tileh in
2227 tilevisible (Lazy.force_val layout) n x y
2229 then Queue.push lruitem state.tilelru
2230 else (
2231 freepbo p;
2232 wcmd "freetile %s" p;
2233 state.memused <- state.memused - s;
2234 state.uioh#infochanged Memused;
2235 Hashtbl.remove state.tilemap k;
2237 loop (qpos+1)
2240 loop 0
2243 let logcurrently = function
2244 | Idle -> dolog "Idle"
2245 | Loading (l, gen) ->
2246 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2247 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2248 dolog
2249 "Tiling %d[%d,%d] page=%s cs=%s angle"
2250 l.pageno col row pageopaque
2251 (colorspace_to_string colorspace)
2253 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2254 angle gen conf.angle state.gen
2255 tilew tileh
2256 conf.tilew conf.tileh
2258 | Outlining _ ->
2259 dolog "outlining"
2262 let splitatspace =
2263 let r = Str.regexp " " in
2264 fun s -> Str.bounded_split r s 2;
2267 let onpagerect pageno f =
2268 let b =
2269 match conf.columns with
2270 | Cmulti (_, b) -> b
2271 | Csingle b -> b
2272 | Csplit (_, b) -> b
2274 if pageno >= 0 && pageno < Array.length b
2275 then
2276 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2277 let r = getpdimrect pdimno in
2278 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2281 let gotopagexy1 pageno x y =
2282 onpagerect pageno (fun w h ->
2283 let top = y /. h in
2284 let _,w1,_,leftx = getpagedim pageno in
2285 let wh = state.winh - state.hscrollh in
2286 let sw = float w1 /. w in
2287 let x = sw *. x in
2288 let x = leftx + state.x + truncate x in
2289 let sx =
2290 if x < 0 || x >= state.winw - state.scrollw
2291 then state.x - x
2292 else state.x
2294 let py, h = getpageyh pageno in
2295 let pdy = truncate (top *. float h) in
2296 let y' = py + pdy in
2297 let dy = y' - state.y in
2298 let sy =
2299 if x != state.x || not (dy > 0 && dy < wh)
2300 then (
2301 if conf.presentation
2302 then
2303 if abs (py - y') > wh
2304 then y'
2305 else py
2306 else y';
2308 else state.y
2310 if state.x != sx || state.y != sy
2311 then (
2312 let x, y =
2313 if !wtmode
2314 then (
2315 let ww = state.winw - state.scrollw in
2316 let qx = sx / ww
2317 and qy = pdy / wh in
2318 let x = qx * ww
2319 and y = py + qy * wh in
2320 let x = if -x + ww > w1 then -(w1-ww) else x
2321 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2322 let y =
2323 if conf.presentation
2324 then
2325 if abs (py - y') > wh
2326 then y'
2327 else py
2328 else y';
2330 (x, y)
2332 else (sx, sy)
2334 state.x <- x;
2335 state.hscrollh <-
2336 if x = 0 && state.w <= state.winw - state.scrollw
2337 then 0
2338 else state.scrollw
2340 gotoy_and_clear_text y;
2342 else gotoy_and_clear_text state.y;
2346 let gotopagexy pageno x y =
2347 match state.mode with
2348 | Birdseye _ -> gotopage pageno 0.0
2349 | _ -> gotopagexy1 pageno x y
2352 let act cmds =
2353 (* dolog "%S" cmds; *)
2354 let cl = splitatspace cmds in
2355 let scan s fmt f =
2356 try Scanf.sscanf s fmt f
2357 with exn ->
2358 dolog "error processing '%S': %s" cmds (exntos exn);
2359 exit 1
2361 match cl with
2362 | "clear" :: [] ->
2363 state.uioh#infochanged Pdim;
2364 state.pdims <- [];
2366 | "clearrects" :: [] ->
2367 state.rects <- state.rects1;
2368 G.postRedisplay "clearrects";
2370 | "continue" :: args :: [] ->
2371 let n = scan args "%u" (fun n -> n) in
2372 state.pagecount <- n;
2373 begin match state.currently with
2374 | Outlining l ->
2375 state.currently <- Idle;
2376 state.outlines <- Array.of_list (List.rev l)
2377 | _ -> ()
2378 end;
2380 let cur, cmds = state.geomcmds in
2381 if String.length cur = 0
2382 then failwith "umpossible";
2384 begin match List.rev cmds with
2385 | [] ->
2386 state.geomcmds <- "", [];
2387 represent ();
2388 | (s, f) :: rest ->
2389 f ();
2390 state.geomcmds <- s, List.rev rest;
2391 end;
2392 if conf.maxwait = None && not !wtmode
2393 then G.postRedisplay "continue";
2395 | "title" :: args :: [] ->
2396 Wsi.settitle args
2398 | "msg" :: args :: [] ->
2399 showtext ' ' args
2401 | "vmsg" :: args :: [] ->
2402 if conf.verbose
2403 then showtext ' ' args
2405 | "emsg" :: args :: [] ->
2406 Buffer.add_string state.errmsgs args;
2407 state.newerrmsgs <- true;
2408 G.postRedisplay "error message"
2410 | "progress" :: args :: [] ->
2411 let progress, text =
2412 scan args "%f %n"
2413 (fun f pos ->
2414 f, String.sub args pos (String.length args - pos))
2416 state.text <- text;
2417 state.progress <- progress;
2418 G.postRedisplay "progress"
2420 | "firstmatch" :: args :: [] ->
2421 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2422 scan args "%u %d %f %f %f %f %f %f %f %f"
2423 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2424 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2426 let y = (getpagey pageno) + truncate y0 in
2427 addnav ();
2428 gotoy y;
2429 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2431 | "match" :: args :: [] ->
2432 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2433 scan args "%u %d %f %f %f %f %f %f %f %f"
2434 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2435 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2437 state.rects1 <-
2438 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2440 | "page" :: args :: [] ->
2441 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2442 begin match state.currently with
2443 | Loading (l, gen) ->
2444 vlog "page %d took %f sec" l.pageno t;
2445 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2446 begin match state.throttle with
2447 | None ->
2448 let preloadedpages =
2449 if conf.preload
2450 then preloadlayout state.y
2451 else state.layout
2453 let evict () =
2454 let module IntSet =
2455 Set.Make (struct type t = int let compare = (-) end) in
2456 let set =
2457 List.fold_left (fun s l -> IntSet.add l.pageno s)
2458 IntSet.empty preloadedpages
2460 let evictedpages =
2461 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2462 if not (IntSet.mem pageno set)
2463 then (
2464 wcmd "freepage %s" opaque;
2465 key :: accu
2467 else accu
2468 ) state.pagemap []
2470 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2472 evict ();
2473 state.currently <- Idle;
2474 if gen = state.gen
2475 then (
2476 tilepage l.pageno pageopaque state.layout;
2477 load state.layout;
2478 load preloadedpages;
2479 if pagevisible state.layout l.pageno
2480 && layoutready state.layout
2481 then G.postRedisplay "page";
2484 | Some (layout, _, _) ->
2485 state.currently <- Idle;
2486 tilepage l.pageno pageopaque layout;
2487 load state.layout
2488 end;
2490 | _ ->
2491 dolog "Inconsistent loading state";
2492 logcurrently state.currently;
2493 exit 1
2496 | "tile" :: args :: [] ->
2497 let (x, y, opaque, size, t) =
2498 scan args "%u %u %s %u %f"
2499 (fun x y p size t -> (x, y, p, size, t))
2501 begin match state.currently with
2502 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2503 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2505 unmappbo opaque;
2506 if tilew != conf.tilew || tileh != conf.tileh
2507 then (
2508 wcmd "freetile %s" opaque;
2509 state.currently <- Idle;
2510 load state.layout;
2512 else (
2513 puttileopaque l col row gen cs angle opaque size t;
2514 state.memused <- state.memused + size;
2515 state.uioh#infochanged Memused;
2516 gctiles ();
2517 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2518 opaque, size) state.tilelru;
2520 let layout =
2521 match state.throttle with
2522 | None -> state.layout
2523 | Some (layout, _, _) -> layout
2526 state.currently <- Idle;
2527 if gen = state.gen
2528 && conf.colorspace = cs
2529 && conf.angle = angle
2530 && tilevisible layout l.pageno x y
2531 then conttiling l.pageno pageopaque;
2533 begin match state.throttle with
2534 | None ->
2535 preload state.layout;
2536 if gen = state.gen
2537 && conf.colorspace = cs
2538 && conf.angle = angle
2539 && tilevisible state.layout l.pageno x y
2540 && (not !wtmode || layoutready state.layout)
2541 then G.postRedisplay "tile nothrottle";
2543 | Some (layout, y, _) ->
2544 let ready = layoutready layout in
2545 if ready
2546 then (
2547 state.y <- y;
2548 state.layout <- layout;
2549 state.throttle <- None;
2550 G.postRedisplay "throttle";
2552 else load layout;
2553 end;
2556 | _ ->
2557 dolog "Inconsistent tiling state";
2558 logcurrently state.currently;
2559 exit 1
2562 | "pdim" :: args :: [] ->
2563 let pdim =
2564 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2566 state.uioh#infochanged Pdim;
2567 state.pdims <- pdim :: state.pdims
2569 | "o" :: args :: [] ->
2570 let (l, n, t, h, pos) =
2571 scan args "%u %u %d %u %n"
2572 (fun l n t h pos -> l, n, t, h, pos)
2574 let s = String.sub args pos (String.length args - pos) in
2575 let outline = (s, l, (n, float t /. float h, 0.0)) in
2576 begin match state.currently with
2577 | Outlining outlines ->
2578 state.currently <- Outlining (outline :: outlines)
2579 | Idle ->
2580 state.currently <- Outlining [outline]
2581 | currently ->
2582 dolog "invalid outlining state";
2583 logcurrently currently
2586 | "a" :: args :: [] ->
2587 let (n, l, t) =
2588 scan args "%u %d %d" (fun n l t -> n, l, t)
2590 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2592 | "info" :: args :: [] ->
2593 state.docinfo <- (1, args) :: state.docinfo
2595 | "infoend" :: [] ->
2596 state.uioh#infochanged Docinfo;
2597 state.docinfo <- List.rev state.docinfo
2599 | _ ->
2600 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2603 let onhist cb =
2604 let rc = cb.rc in
2605 let action = function
2606 | HCprev -> cbget cb ~-1
2607 | HCnext -> cbget cb 1
2608 | HCfirst -> cbget cb ~-(cb.rc)
2609 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2610 and cancel () = cb.rc <- rc
2611 in (action, cancel)
2614 let search pattern forward =
2615 if String.length pattern > 0
2616 then
2617 let pn, py =
2618 match state.layout with
2619 | [] -> 0, 0
2620 | l :: _ ->
2621 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2623 wcmd "search %d %d %d %d,%s\000"
2624 (btod conf.icase) pn py (btod forward) pattern;
2627 let intentry text key =
2628 let c =
2629 if key >= 32 && key < 127
2630 then Char.chr key
2631 else '\000'
2633 match c with
2634 | '0' .. '9' ->
2635 let text = addchar text c in
2636 TEcont text
2638 | _ ->
2639 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2640 TEcont text
2643 let linknentry text key =
2644 let c =
2645 if key >= 32 && key < 127
2646 then Char.chr key
2647 else '\000'
2649 match c with
2650 | 'a' .. 'z' ->
2651 let text = addchar text c in
2652 TEcont text
2654 | _ ->
2655 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2656 TEcont text
2659 let linkndone f s =
2660 if String.length s > 0
2661 then (
2662 let n =
2663 let l = String.length s in
2664 let rec loop pos n = if pos = l then n else
2665 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2666 loop (pos+1) (n*26 + m)
2667 in loop 0 0
2669 let rec loop n = function
2670 | [] -> ()
2671 | l :: rest ->
2672 match getopaque l.pageno with
2673 | None -> loop n rest
2674 | Some opaque ->
2675 let m = getlinkcount opaque in
2676 if n < m
2677 then (
2678 let under = getlink opaque n in
2679 f under
2681 else loop (n-m) rest
2683 loop n state.layout;
2687 let textentry text key =
2688 if key land 0xff00 = 0xff00
2689 then TEcont text
2690 else TEcont (text ^ toutf8 key)
2693 let reqlayout angle proportional =
2694 match state.throttle with
2695 | None ->
2696 if nogeomcmds state.geomcmds
2697 then state.anchor <- getanchor ();
2698 conf.angle <- angle mod 360;
2699 if conf.angle != 0
2700 then (
2701 match state.mode with
2702 | LinkNav _ -> state.mode <- View
2703 | _ -> ()
2705 conf.proportional <- proportional;
2706 invalidate "reqlayout"
2707 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2708 | _ -> ()
2711 let settrim trimmargins trimfuzz =
2712 if nogeomcmds state.geomcmds
2713 then state.anchor <- getanchor ();
2714 conf.trimmargins <- trimmargins;
2715 conf.trimfuzz <- trimfuzz;
2716 let x0, y0, x1, y1 = trimfuzz in
2717 invalidate "settrim"
2718 (fun () ->
2719 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2720 flushpages ();
2723 let setzoom zoom =
2724 match state.throttle with
2725 | None ->
2726 let zoom = max 0.0001 zoom in
2727 if zoom <> conf.zoom
2728 then (
2729 state.prevzoom <- conf.zoom;
2730 conf.zoom <- zoom;
2731 reshape state.winw state.winh;
2732 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2735 | Some (layout, y, started) ->
2736 let time =
2737 match conf.maxwait with
2738 | None -> 0.0
2739 | Some t -> t
2741 let dt = now () -. started in
2742 if dt > time
2743 then (
2744 state.y <- y;
2745 load layout;
2749 let setcolumns mode columns coverA coverB =
2750 state.prevcolumns <- Some (conf.columns, conf.zoom);
2751 if columns < 0
2752 then (
2753 if isbirdseye mode
2754 then showtext '!' "split mode doesn't work in bird's eye"
2755 else (
2756 conf.columns <- Csplit (-columns, [||]);
2757 state.x <- 0;
2758 conf.zoom <- 1.0;
2761 else (
2762 if columns < 2
2763 then (
2764 conf.columns <- Csingle [||];
2765 state.x <- 0;
2766 setzoom 1.0;
2768 else (
2769 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2770 conf.zoom <- 1.0;
2773 reshape state.winw state.winh;
2776 let enterbirdseye () =
2777 let zoom = float conf.thumbw /. float state.winw in
2778 let birdseyepageno =
2779 let cy = state.winh / 2 in
2780 let fold = function
2781 | [] -> 0
2782 | l :: rest ->
2783 let rec fold best = function
2784 | [] -> best.pageno
2785 | l :: rest ->
2786 let d = cy - (l.pagedispy + l.pagevh/2)
2787 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2788 if abs d < abs dbest
2789 then fold l rest
2790 else best.pageno
2791 in fold l rest
2793 fold state.layout
2795 state.mode <- Birdseye (
2796 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2798 conf.zoom <- zoom;
2799 conf.presentation <- false;
2800 conf.interpagespace <- 10;
2801 conf.hlinks <- false;
2802 state.x <- 0;
2803 state.mstate <- Mnone;
2804 conf.maxwait <- None;
2805 conf.columns <- (
2806 match conf.beyecolumns with
2807 | Some c ->
2808 conf.zoom <- 1.0;
2809 Cmulti ((c, 0, 0), [||])
2810 | None -> Csingle [||]
2812 Wsi.setcursor Wsi.CURSOR_INHERIT;
2813 if conf.verbose
2814 then
2815 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2816 (100.0*.zoom)
2817 else
2818 state.text <- ""
2820 reshape state.winw state.winh;
2823 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2824 state.mode <- View;
2825 conf.zoom <- c.zoom;
2826 conf.presentation <- c.presentation;
2827 conf.interpagespace <- c.interpagespace;
2828 conf.maxwait <- c.maxwait;
2829 conf.hlinks <- c.hlinks;
2830 conf.beyecolumns <- (
2831 match conf.columns with
2832 | Cmulti ((c, _, _), _) -> Some c
2833 | Csingle _ -> None
2834 | Csplit _ -> failwith "leaving bird's eye split mode"
2836 conf.columns <- (
2837 match c.columns with
2838 | Cmulti (c, _) -> Cmulti (c, [||])
2839 | Csingle _ -> Csingle [||]
2840 | Csplit (c, _) -> Csplit (c, [||])
2842 state.x <- leftx;
2843 if conf.verbose
2844 then
2845 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2846 (100.0*.conf.zoom)
2848 reshape state.winw state.winh;
2849 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2852 let togglebirdseye () =
2853 match state.mode with
2854 | Birdseye vals -> leavebirdseye vals true
2855 | View -> enterbirdseye ()
2856 | _ -> ()
2859 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2860 let pageno = max 0 (pageno - incr) in
2861 let rec loop = function
2862 | [] -> gotopage1 pageno 0
2863 | l :: _ when l.pageno = pageno ->
2864 if l.pagedispy >= 0 && l.pagey = 0
2865 then G.postRedisplay "upbirdseye"
2866 else gotopage1 pageno 0
2867 | _ :: rest -> loop rest
2869 loop state.layout;
2870 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2873 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2874 let pageno = min (state.pagecount - 1) (pageno + incr) in
2875 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2876 let rec loop = function
2877 | [] ->
2878 let y, h = getpageyh pageno in
2879 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2880 gotoy (clamp dy)
2881 | l :: _ when l.pageno = pageno ->
2882 if l.pagevh != l.pageh
2883 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2884 else G.postRedisplay "downbirdseye"
2885 | _ :: rest -> loop rest
2887 loop state.layout
2890 let optentry mode _ key =
2891 let btos b = if b then "on" else "off" in
2892 if key >= 32 && key < 127
2893 then
2894 let c = Char.chr key in
2895 match c with
2896 | 's' ->
2897 let ondone s =
2898 try conf.scrollstep <- int_of_string s with exc ->
2899 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2901 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2903 | 'A' ->
2904 let ondone s =
2906 conf.autoscrollstep <- int_of_string s;
2907 if state.autoscroll <> None
2908 then state.autoscroll <- Some conf.autoscrollstep
2909 with exc ->
2910 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2912 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2914 | 'C' ->
2915 let ondone s =
2917 let n, a, b = multicolumns_of_string s in
2918 setcolumns mode n a b;
2919 with exc ->
2920 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2922 TEswitch ("columns: ", "", None, textentry, ondone, true)
2924 | 'Z' ->
2925 let ondone s =
2927 let zoom = float (int_of_string s) /. 100.0 in
2928 setzoom zoom
2929 with exc ->
2930 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2932 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2934 | 't' ->
2935 let ondone s =
2937 conf.thumbw <- bound (int_of_string s) 2 4096;
2938 state.text <-
2939 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2940 begin match mode with
2941 | Birdseye beye ->
2942 leavebirdseye beye false;
2943 enterbirdseye ();
2944 | _ -> ();
2946 with exc ->
2947 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2949 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2951 | 'R' ->
2952 let ondone s =
2953 match try
2954 Some (int_of_string s)
2955 with exc ->
2956 state.text <- Printf.sprintf "bad integer `%s': %s"
2957 s (exntos exc);
2958 None
2959 with
2960 | Some angle -> reqlayout angle conf.proportional
2961 | None -> ()
2963 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2965 | 'i' ->
2966 conf.icase <- not conf.icase;
2967 TEdone ("case insensitive search " ^ (btos conf.icase))
2969 | 'p' ->
2970 conf.preload <- not conf.preload;
2971 gotoy state.y;
2972 TEdone ("preload " ^ (btos conf.preload))
2974 | 'v' ->
2975 conf.verbose <- not conf.verbose;
2976 TEdone ("verbose " ^ (btos conf.verbose))
2978 | 'd' ->
2979 conf.debug <- not conf.debug;
2980 TEdone ("debug " ^ (btos conf.debug))
2982 | 'h' ->
2983 conf.maxhfit <- not conf.maxhfit;
2984 state.maxy <- calcheight ();
2985 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2987 | 'c' ->
2988 conf.crophack <- not conf.crophack;
2989 TEdone ("crophack " ^ btos conf.crophack)
2991 | 'a' ->
2992 let s =
2993 match conf.maxwait with
2994 | None ->
2995 conf.maxwait <- Some infinity;
2996 "always wait for page to complete"
2997 | Some _ ->
2998 conf.maxwait <- None;
2999 "show placeholder if page is not ready"
3001 TEdone s
3003 | 'f' ->
3004 conf.underinfo <- not conf.underinfo;
3005 TEdone ("underinfo " ^ btos conf.underinfo)
3007 | 'P' ->
3008 conf.savebmarks <- not conf.savebmarks;
3009 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3011 | 'S' ->
3012 let ondone s =
3014 let pageno, py =
3015 match state.layout with
3016 | [] -> 0, 0
3017 | l :: _ ->
3018 l.pageno, l.pagey
3020 conf.interpagespace <- int_of_string s;
3021 docolumns conf.columns;
3022 state.maxy <- calcheight ();
3023 let y = getpagey pageno in
3024 gotoy (y + py)
3025 with exc ->
3026 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3028 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3030 | 'l' ->
3031 reqlayout conf.angle (not conf.proportional);
3032 TEdone ("proportional display " ^ btos conf.proportional)
3034 | 'T' ->
3035 settrim (not conf.trimmargins) conf.trimfuzz;
3036 TEdone ("trim margins " ^ btos conf.trimmargins)
3038 | 'I' ->
3039 conf.invert <- not conf.invert;
3040 TEdone ("invert colors " ^ btos conf.invert)
3042 | 'x' ->
3043 let ondone s =
3044 cbput state.hists.sel s;
3045 conf.selcmd <- s;
3047 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3048 textentry, ondone, true)
3050 | _ ->
3051 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3052 TEstop
3053 else
3054 TEcont state.text
3057 class type lvsource = object
3058 method getitemcount : int
3059 method getitem : int -> (string * int)
3060 method hasaction : int -> bool
3061 method exit :
3062 uioh:uioh ->
3063 cancel:bool ->
3064 active:int ->
3065 first:int ->
3066 pan:int ->
3067 qsearch:string ->
3068 uioh option
3069 method getactive : int
3070 method getfirst : int
3071 method getqsearch : string
3072 method setqsearch : string -> unit
3073 method getpan : int
3074 end;;
3076 class virtual lvsourcebase = object
3077 val mutable m_active = 0
3078 val mutable m_first = 0
3079 val mutable m_qsearch = ""
3080 val mutable m_pan = 0
3081 method getactive = m_active
3082 method getfirst = m_first
3083 method getqsearch = m_qsearch
3084 method getpan = m_pan
3085 method setqsearch s = m_qsearch <- s
3086 end;;
3088 let withoutlastutf8 s =
3089 let len = String.length s in
3090 if len = 0
3091 then s
3092 else
3093 let rec find pos =
3094 if pos = 0
3095 then pos
3096 else
3097 let b = Char.code s.[pos] in
3098 if b land 0b11000000 = 0b11000000
3099 then pos
3100 else find (pos-1)
3102 let first =
3103 if Char.code s.[len-1] land 0x80 = 0
3104 then len-1
3105 else find (len-1)
3107 String.sub s 0 first;
3110 let textentrykeyboard
3111 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3112 let key =
3113 if key >= 0xffb0 && key <= 0xffb9
3114 then key - 0xffb0 + 48 else key
3116 let enttext te =
3117 state.mode <- Textentry (te, onleave);
3118 state.text <- "";
3119 enttext ();
3120 G.postRedisplay "textentrykeyboard enttext";
3122 let histaction cmd =
3123 match opthist with
3124 | None -> ()
3125 | Some (action, _) ->
3126 state.mode <- Textentry (
3127 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3129 G.postRedisplay "textentry histaction"
3131 match key with
3132 | 0xff08 -> (* backspace *)
3133 let s = withoutlastutf8 text in
3134 let len = String.length s in
3135 if cancelonempty && len = 0
3136 then (
3137 onleave Cancel;
3138 G.postRedisplay "textentrykeyboard after cancel";
3140 else (
3141 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3144 | 0xff0d | 0xff8d -> (* (kp) enter *)
3145 ondone text;
3146 onleave Confirm;
3147 G.postRedisplay "textentrykeyboard after confirm"
3149 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3150 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3151 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3152 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3154 | 0xff1b -> (* escape*)
3155 if String.length text = 0
3156 then (
3157 begin match opthist with
3158 | None -> ()
3159 | Some (_, onhistcancel) -> onhistcancel ()
3160 end;
3161 onleave Cancel;
3162 state.text <- "";
3163 G.postRedisplay "textentrykeyboard after cancel2"
3165 else (
3166 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3169 | 0xff9f | 0xffff -> () (* delete *)
3171 | _ when key != 0
3172 && key land 0xff00 != 0xff00 (* keyboard *)
3173 && key land 0xfe00 != 0xfe00 (* xkb *)
3174 && key land 0xfd00 != 0xfd00 (* 3270 *)
3176 begin match onkey text key with
3177 | TEdone text ->
3178 ondone text;
3179 onleave Confirm;
3180 G.postRedisplay "textentrykeyboard after confirm2";
3182 | TEcont text ->
3183 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3185 | TEstop ->
3186 onleave Cancel;
3187 G.postRedisplay "textentrykeyboard after cancel3"
3189 | TEswitch te ->
3190 state.mode <- Textentry (te, onleave);
3191 G.postRedisplay "textentrykeyboard switch";
3192 end;
3194 | _ ->
3195 vlog "unhandled key %s" (Wsi.keyname key)
3198 let firstof first active =
3199 if first > active || abs (first - active) > fstate.maxrows - 1
3200 then max 0 (active - (fstate.maxrows/2))
3201 else first
3204 let calcfirst first active =
3205 if active > first
3206 then
3207 let rows = active - first in
3208 if rows > fstate.maxrows then active - fstate.maxrows else first
3209 else active
3212 let scrollph y maxy =
3213 let sh = (float (maxy + state.winh) /. float state.winh) in
3214 let sh = float state.winh /. sh in
3215 let sh = max sh (float conf.scrollh) in
3217 let percent =
3218 if y = state.maxy
3219 then 1.0
3220 else float y /. float maxy
3222 let position = (float state.winh -. sh) *. percent in
3224 let position =
3225 if position +. sh > float state.winh
3226 then float state.winh -. sh
3227 else position
3229 position, sh;
3232 let coe s = (s :> uioh);;
3234 class listview ~(source:lvsource) ~trusted ~modehash =
3235 object (self)
3236 val m_pan = source#getpan
3237 val m_first = source#getfirst
3238 val m_active = source#getactive
3239 val m_qsearch = source#getqsearch
3240 val m_prev_uioh = state.uioh
3242 method private elemunder y =
3243 let n = y / (fstate.fontsize+1) in
3244 if m_first + n < source#getitemcount
3245 then (
3246 if source#hasaction (m_first + n)
3247 then Some (m_first + n)
3248 else None
3250 else None
3252 method display =
3253 Gl.enable `blend;
3254 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3255 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3256 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3257 GlDraw.color (1., 1., 1.);
3258 Gl.enable `texture_2d;
3259 let fs = fstate.fontsize in
3260 let nfs = fs + 1 in
3261 let ww = fstate.wwidth in
3262 let tabw = 30.0*.ww in
3263 let itemcount = source#getitemcount in
3264 let rec loop row =
3265 if (row - m_first) > fstate.maxrows
3266 then ()
3267 else (
3268 if row >= 0 && row < itemcount
3269 then (
3270 let (s, level) = source#getitem row in
3271 let y = (row - m_first) * nfs in
3272 let x = 5.0 +. float (level + m_pan) *. ww in
3273 if row = m_active
3274 then (
3275 Gl.disable `texture_2d;
3276 GlDraw.polygon_mode `both `line;
3277 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3278 GlDraw.rect (1., float (y + 1))
3279 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3280 GlDraw.polygon_mode `both `fill;
3281 GlDraw.color (1., 1., 1.);
3282 Gl.enable `texture_2d;
3285 let drawtabularstring s =
3286 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3287 if trusted
3288 then
3289 let tabpos = try String.index s '\t' with Not_found -> -1 in
3290 if tabpos > 0
3291 then
3292 let len = String.length s - tabpos - 1 in
3293 let s1 = String.sub s 0 tabpos
3294 and s2 = String.sub s (tabpos + 1) len in
3295 let nx = drawstr x s1 in
3296 let sw = nx -. x in
3297 let x = x +. (max tabw sw) in
3298 drawstr x s2
3299 else
3300 drawstr x s
3301 else
3302 drawstr x s
3304 let _ = drawtabularstring s in
3305 loop (row+1)
3309 loop m_first;
3310 Gl.disable `blend;
3311 Gl.disable `texture_2d;
3313 method updownlevel incr =
3314 let len = source#getitemcount in
3315 let curlevel =
3316 if m_active >= 0 && m_active < len
3317 then snd (source#getitem m_active)
3318 else -1
3320 let rec flow i =
3321 if i = len then i-1 else if i = -1 then 0 else
3322 let _, l = source#getitem i in
3323 if l != curlevel then i else flow (i+incr)
3325 let active = flow m_active in
3326 let first = calcfirst m_first active in
3327 G.postRedisplay "outline updownlevel";
3328 {< m_active = active; m_first = first >}
3330 method private key1 key mask =
3331 let set1 active first qsearch =
3332 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3334 let search active pattern incr =
3335 let dosearch re =
3336 let rec loop n =
3337 if n >= 0 && n < source#getitemcount
3338 then (
3339 let s, _ = source#getitem n in
3341 (try ignore (Str.search_forward re s 0); true
3342 with Not_found -> false)
3343 then Some n
3344 else loop (n + incr)
3346 else None
3348 loop active
3351 let re = Str.regexp_case_fold pattern in
3352 dosearch re
3353 with Failure s ->
3354 state.text <- s;
3355 None
3357 let itemcount = source#getitemcount in
3358 let find start incr =
3359 let rec find i =
3360 if i = -1 || i = itemcount
3361 then -1
3362 else (
3363 if source#hasaction i
3364 then i
3365 else find (i + incr)
3368 find start
3370 let set active first =
3371 let first = bound first 0 (itemcount - fstate.maxrows) in
3372 state.text <- "";
3373 coe {< m_active = active; m_first = first >}
3375 let navigate incr =
3376 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3377 let active, first =
3378 let incr1 = if incr > 0 then 1 else -1 in
3379 if isvisible m_first m_active
3380 then
3381 let next =
3382 let next = m_active + incr in
3383 let next =
3384 if next < 0 || next >= itemcount
3385 then -1
3386 else find next incr1
3388 if next = -1 || abs (m_active - next) > fstate.maxrows
3389 then -1
3390 else next
3392 if next = -1
3393 then
3394 let first = m_first + incr in
3395 let first = bound first 0 (itemcount - 1) in
3396 let next =
3397 let next = m_active + incr in
3398 let next = bound next 0 (itemcount - 1) in
3399 find next ~-incr1
3401 let active = if next = -1 then m_active else next in
3402 active, first
3403 else
3404 let first = min next m_first in
3405 let first =
3406 if abs (next - first) > fstate.maxrows
3407 then first + incr
3408 else first
3410 next, first
3411 else
3412 let first = m_first + incr in
3413 let first = bound first 0 (itemcount - 1) in
3414 let active =
3415 let next = m_active + incr in
3416 let next = bound next 0 (itemcount - 1) in
3417 let next = find next incr1 in
3418 let active =
3419 if next = -1 || abs (m_active - first) > fstate.maxrows
3420 then (
3421 let active = if m_active = -1 then next else m_active in
3422 active
3424 else next
3426 if isvisible first active
3427 then active
3428 else -1
3430 active, first
3432 G.postRedisplay "listview navigate";
3433 set active first;
3435 match key with
3436 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3437 let incr = if key = 0x72 then -1 else 1 in
3438 let active, first =
3439 match search (m_active + incr) m_qsearch incr with
3440 | None ->
3441 state.text <- m_qsearch ^ " [not found]";
3442 m_active, m_first
3443 | Some active ->
3444 state.text <- m_qsearch;
3445 active, firstof m_first active
3447 G.postRedisplay "listview ctrl-r/s";
3448 set1 active first m_qsearch;
3450 | 0xff08 -> (* backspace *)
3451 if String.length m_qsearch = 0
3452 then coe self
3453 else (
3454 let qsearch = withoutlastutf8 m_qsearch in
3455 let len = String.length qsearch in
3456 if len = 0
3457 then (
3458 state.text <- "";
3459 G.postRedisplay "listview empty qsearch";
3460 set1 m_active m_first "";
3462 else
3463 let active, first =
3464 match search m_active qsearch ~-1 with
3465 | None ->
3466 state.text <- qsearch ^ " [not found]";
3467 m_active, m_first
3468 | Some active ->
3469 state.text <- qsearch;
3470 active, firstof m_first active
3472 G.postRedisplay "listview backspace qsearch";
3473 set1 active first qsearch
3476 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3477 let pattern = m_qsearch ^ toutf8 key in
3478 let active, first =
3479 match search m_active pattern 1 with
3480 | None ->
3481 state.text <- pattern ^ " [not found]";
3482 m_active, m_first
3483 | Some active ->
3484 state.text <- pattern;
3485 active, firstof m_first active
3487 G.postRedisplay "listview qsearch add";
3488 set1 active first pattern;
3490 | 0xff1b -> (* escape *)
3491 state.text <- "";
3492 if String.length m_qsearch = 0
3493 then (
3494 G.postRedisplay "list view escape";
3495 begin
3496 match
3497 source#exit (coe self) true m_active m_first m_pan m_qsearch
3498 with
3499 | None -> m_prev_uioh
3500 | Some uioh -> uioh
3503 else (
3504 G.postRedisplay "list view kill qsearch";
3505 source#setqsearch "";
3506 coe {< m_qsearch = "" >}
3509 | 0xff0d | 0xff8d -> (* (kp) enter *)
3510 state.text <- "";
3511 let self = {< m_qsearch = "" >} in
3512 source#setqsearch "";
3513 let opt =
3514 G.postRedisplay "listview enter";
3515 if m_active >= 0 && m_active < source#getitemcount
3516 then (
3517 source#exit (coe self) false m_active m_first m_pan "";
3519 else (
3520 source#exit (coe self) true m_active m_first m_pan "";
3523 begin match opt with
3524 | None -> m_prev_uioh
3525 | Some uioh -> uioh
3528 | 0xff9f | 0xffff -> (* (kp) delete *)
3529 coe self
3531 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3532 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3533 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3534 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3536 | 0xff53 | 0xff98 -> (* (kp) right *)
3537 state.text <- "";
3538 G.postRedisplay "listview right";
3539 coe {< m_pan = m_pan - 1 >}
3541 | 0xff51 | 0xff96 -> (* (kp) left *)
3542 state.text <- "";
3543 G.postRedisplay "listview left";
3544 coe {< m_pan = m_pan + 1 >}
3546 | 0xff50 | 0xff95 -> (* (kp) home *)
3547 let active = find 0 1 in
3548 G.postRedisplay "listview home";
3549 set active 0;
3551 | 0xff57 | 0xff9c -> (* (kp) end *)
3552 let first = max 0 (itemcount - fstate.maxrows) in
3553 let active = find (itemcount - 1) ~-1 in
3554 G.postRedisplay "listview end";
3555 set active first;
3557 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3558 coe self
3560 | _ ->
3561 dolog "listview unknown key %#x" key; coe self
3563 method key key mask =
3564 match state.mode with
3565 | Textentry te -> textentrykeyboard key mask te; coe self
3566 | _ -> self#key1 key mask
3568 method button button down x y _ =
3569 let opt =
3570 match button with
3571 | 1 when x > state.winw - conf.scrollbw ->
3572 G.postRedisplay "listview scroll";
3573 if down
3574 then
3575 let _, position, sh = self#scrollph in
3576 if y > truncate position && y < truncate (position +. sh)
3577 then (
3578 state.mstate <- Mscrolly;
3579 Some (coe self)
3581 else
3582 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3583 let first = truncate (s *. float source#getitemcount) in
3584 let first = min source#getitemcount first in
3585 Some (coe {< m_first = first; m_active = first >})
3586 else (
3587 state.mstate <- Mnone;
3588 Some (coe self);
3590 | 1 when not down ->
3591 begin match self#elemunder y with
3592 | Some n ->
3593 G.postRedisplay "listview click";
3594 source#exit
3595 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3596 | _ ->
3597 Some (coe self)
3599 | n when (n == 4 || n == 5) && not down ->
3600 let len = source#getitemcount in
3601 let first =
3602 if n = 5 && m_first + fstate.maxrows >= len
3603 then
3604 m_first
3605 else
3606 let first = m_first + (if n == 4 then -1 else 1) in
3607 bound first 0 (len - 1)
3609 G.postRedisplay "listview wheel";
3610 Some (coe {< m_first = first >})
3611 | n when (n = 6 || n = 7) && not down ->
3612 let inc = m_first + (if n = 7 then -1 else 1) in
3613 G.postRedisplay "listview hwheel";
3614 Some (coe {< m_pan = m_pan + inc >})
3615 | _ ->
3616 Some (coe self)
3618 match opt with
3619 | None -> m_prev_uioh
3620 | Some uioh -> uioh
3622 method motion _ y =
3623 match state.mstate with
3624 | Mscrolly ->
3625 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3626 let first = truncate (s *. float source#getitemcount) in
3627 let first = min source#getitemcount first in
3628 G.postRedisplay "listview motion";
3629 coe {< m_first = first; m_active = first >}
3630 | _ -> coe self
3632 method pmotion x y =
3633 if x < state.winw - conf.scrollbw
3634 then
3635 let n =
3636 match self#elemunder y with
3637 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3638 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3640 let o =
3641 if n != m_active
3642 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3643 else self
3645 coe o
3646 else (
3647 Wsi.setcursor Wsi.CURSOR_INHERIT;
3648 coe self
3651 method infochanged _ = ()
3653 method scrollpw = (0, 0.0, 0.0)
3654 method scrollph =
3655 let nfs = fstate.fontsize + 1 in
3656 let y = m_first * nfs in
3657 let itemcount = source#getitemcount in
3658 let maxi = max 0 (itemcount - fstate.maxrows) in
3659 let maxy = maxi * nfs in
3660 let p, h = scrollph y maxy in
3661 conf.scrollbw, p, h
3663 method modehash = modehash
3664 method eformsgs = false
3665 end;;
3667 class outlinelistview ~source =
3668 object (self)
3669 inherit listview
3670 ~source:(source :> lvsource)
3671 ~trusted:false
3672 ~modehash:(findkeyhash conf "outline")
3673 as super
3675 method key key mask =
3676 let calcfirst first active =
3677 if active > first
3678 then
3679 let rows = active - first in
3680 let maxrows =
3681 if String.length state.text = 0
3682 then fstate.maxrows
3683 else fstate.maxrows - 2
3685 if rows > maxrows then active - maxrows else first
3686 else active
3688 let navigate incr =
3689 let active = m_active + incr in
3690 let active = bound active 0 (source#getitemcount - 1) in
3691 let first = calcfirst m_first active in
3692 G.postRedisplay "outline navigate";
3693 coe {< m_active = active; m_first = first >}
3695 let ctrl = Wsi.withctrl mask in
3696 match key with
3697 | 110 when ctrl -> (* ctrl-n *)
3698 source#narrow m_qsearch;
3699 G.postRedisplay "outline ctrl-n";
3700 coe {< m_first = 0; m_active = 0 >}
3702 | 117 when ctrl -> (* ctrl-u *)
3703 source#denarrow;
3704 G.postRedisplay "outline ctrl-u";
3705 state.text <- "";
3706 coe {< m_first = 0; m_active = 0 >}
3708 | 108 when ctrl -> (* ctrl-l *)
3709 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3710 G.postRedisplay "outline ctrl-l";
3711 coe {< m_first = first >}
3713 | 0xff9f | 0xffff -> (* (kp) delete *)
3714 source#remove m_active;
3715 G.postRedisplay "outline delete";
3716 let active = max 0 (m_active-1) in
3717 coe {< m_first = firstof m_first active;
3718 m_active = active >}
3720 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3721 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3722 | 0xff55 | 0xff9a -> (* (kp) prior *)
3723 navigate ~-(fstate.maxrows)
3724 | 0xff56 | 0xff9b -> (* (kp) next *)
3725 navigate fstate.maxrows
3727 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3728 let o =
3729 if ctrl
3730 then (
3731 G.postRedisplay "outline ctrl right";
3732 {< m_pan = m_pan + 1 >}
3734 else self#updownlevel 1
3736 coe o
3738 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3739 let o =
3740 if ctrl
3741 then (
3742 G.postRedisplay "outline ctrl left";
3743 {< m_pan = m_pan - 1 >}
3745 else self#updownlevel ~-1
3747 coe o
3749 | 0xff50 | 0xff95 -> (* (kp) home *)
3750 G.postRedisplay "outline home";
3751 coe {< m_first = 0; m_active = 0 >}
3753 | 0xff57 | 0xff9c -> (* (kp) end *)
3754 let active = source#getitemcount - 1 in
3755 let first = max 0 (active - fstate.maxrows) in
3756 G.postRedisplay "outline end";
3757 coe {< m_active = active; m_first = first >}
3759 | _ -> super#key key mask
3762 let outlinesource usebookmarks =
3763 let empty = [||] in
3764 (object
3765 inherit lvsourcebase
3766 val mutable m_items = empty
3767 val mutable m_orig_items = empty
3768 val mutable m_prev_items = empty
3769 val mutable m_narrow_pattern = ""
3770 val mutable m_hadremovals = false
3772 method getitemcount =
3773 Array.length m_items + (if m_hadremovals then 1 else 0)
3775 method getitem n =
3776 if n == Array.length m_items && m_hadremovals
3777 then
3778 ("[Confirm removal]", 0)
3779 else
3780 let s, n, _ = m_items.(n) in
3781 (s, n)
3783 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3784 ignore (uioh, first, qsearch);
3785 let confrimremoval = m_hadremovals && active = Array.length m_items in
3786 let items =
3787 if String.length m_narrow_pattern = 0
3788 then m_orig_items
3789 else m_items
3791 if not cancel
3792 then (
3793 if not confrimremoval
3794 then(
3795 let _, _, anchor = m_items.(active) in
3796 gotoghyll (getanchory anchor);
3797 m_items <- items;
3799 else (
3800 state.bookmarks <- Array.to_list m_items;
3801 m_orig_items <- m_items;
3804 else m_items <- items;
3805 m_pan <- pan;
3806 None
3808 method hasaction _ = true
3810 method greetmsg =
3811 if Array.length m_items != Array.length m_orig_items
3812 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3813 else ""
3815 method narrow pattern =
3816 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3817 match reopt with
3818 | None -> ()
3819 | Some re ->
3820 let rec loop accu n =
3821 if n = -1
3822 then (
3823 m_narrow_pattern <- pattern;
3824 m_items <- Array.of_list accu
3826 else
3827 let (s, _, _) as o = m_items.(n) in
3828 let accu =
3829 if (try ignore (Str.search_forward re s 0); true
3830 with Not_found -> false)
3831 then o :: accu
3832 else accu
3834 loop accu (n-1)
3836 loop [] (Array.length m_items - 1)
3838 method denarrow =
3839 m_orig_items <- (
3840 if usebookmarks
3841 then Array.of_list state.bookmarks
3842 else state.outlines
3844 m_items <- m_orig_items
3846 method remove m =
3847 if usebookmarks
3848 then
3849 if m >= 0 && m < Array.length m_items
3850 then (
3851 m_hadremovals <- true;
3852 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3853 let n = if n >= m then n+1 else n in
3854 m_items.(n)
3858 method reset anchor items =
3859 m_hadremovals <- false;
3860 if m_orig_items == empty || m_prev_items != items
3861 then (
3862 m_orig_items <- items;
3863 if String.length m_narrow_pattern = 0
3864 then m_items <- items;
3866 m_prev_items <- items;
3867 let rely = getanchory anchor in
3868 let active =
3869 let rec loop n best bestd =
3870 if n = Array.length m_items
3871 then best
3872 else
3873 let (_, _, anchor) = m_items.(n) in
3874 let orely = getanchory anchor in
3875 let d = abs (orely - rely) in
3876 if d < bestd
3877 then loop (n+1) n d
3878 else loop (n+1) best bestd
3880 loop 0 ~-1 max_int
3882 m_active <- active;
3883 m_first <- firstof m_first active
3884 end)
3887 let enterselector usebookmarks =
3888 let source = outlinesource usebookmarks in
3889 fun errmsg ->
3890 let outlines =
3891 if usebookmarks
3892 then Array.of_list state.bookmarks
3893 else state.outlines
3895 if Array.length outlines = 0
3896 then (
3897 showtext ' ' errmsg;
3899 else (
3900 state.text <- source#greetmsg;
3901 Wsi.setcursor Wsi.CURSOR_INHERIT;
3902 let anchor = getanchor () in
3903 source#reset anchor outlines;
3904 state.uioh <- coe (new outlinelistview ~source);
3905 G.postRedisplay "enter selector";
3909 let enteroutlinemode =
3910 let f = enterselector false in
3911 fun ()-> f "Document has no outline";
3914 let enterbookmarkmode =
3915 let f = enterselector true in
3916 fun () -> f "Document has no bookmarks (yet)";
3919 let color_of_string s =
3920 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3921 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3925 let color_to_string (r, g, b) =
3926 let r = truncate (r *. 256.0)
3927 and g = truncate (g *. 256.0)
3928 and b = truncate (b *. 256.0) in
3929 Printf.sprintf "%d/%d/%d" r g b
3932 let irect_of_string s =
3933 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3936 let irect_to_string (x0,y0,x1,y1) =
3937 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3940 let makecheckers () =
3941 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3942 following to say:
3943 converted by Issac Trotts. July 25, 2002 *)
3944 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3945 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3946 let id = GlTex.gen_texture () in
3947 GlTex.bind_texture `texture_2d id;
3948 GlPix.store (`unpack_alignment 1);
3949 GlTex.image2d image;
3950 List.iter (GlTex.parameter ~target:`texture_2d)
3951 [ `mag_filter `nearest; `min_filter `nearest ];
3955 let setcheckers enabled =
3956 match state.texid with
3957 | None ->
3958 if enabled then state.texid <- Some (makecheckers ())
3960 | Some texid ->
3961 if not enabled
3962 then (
3963 GlTex.delete_texture texid;
3964 state.texid <- None;
3968 let int_of_string_with_suffix s =
3969 let l = String.length s in
3970 let s1, shift =
3971 if l > 1
3972 then
3973 let suffix = Char.lowercase s.[l-1] in
3974 match suffix with
3975 | 'k' -> String.sub s 0 (l-1), 10
3976 | 'm' -> String.sub s 0 (l-1), 20
3977 | 'g' -> String.sub s 0 (l-1), 30
3978 | _ -> s, 0
3979 else s, 0
3981 let n = int_of_string s1 in
3982 let m = n lsl shift in
3983 if m < 0 || m < n
3984 then raise (Failure "value too large")
3985 else m
3988 let string_with_suffix_of_int n =
3989 if n = 0
3990 then "0"
3991 else
3992 let n, s =
3993 if n land ((1 lsl 30) - 1) = 0
3994 then n lsr 30, "G"
3995 else (
3996 if n land ((1 lsl 20) - 1) = 0
3997 then n lsr 20, "M"
3998 else (
3999 if n land ((1 lsl 10) - 1) = 0
4000 then n lsr 10, "K"
4001 else n, ""
4005 let rec loop s n =
4006 let h = n mod 1000 in
4007 let n = n / 1000 in
4008 if n = 0
4009 then string_of_int h ^ s
4010 else (
4011 let s = Printf.sprintf "_%03d%s" h s in
4012 loop s n
4015 loop "" n ^ s;
4018 let defghyllscroll = (40, 8, 32);;
4019 let ghyllscroll_of_string s =
4020 let (n, a, b) as nab =
4021 if s = "default"
4022 then defghyllscroll
4023 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4025 if n <= a || n <= b || a >= b
4026 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4027 nab;
4030 let ghyllscroll_to_string ((n, a, b) as nab) =
4031 if nab = defghyllscroll
4032 then "default"
4033 else Printf.sprintf "%d,%d,%d" n a b;
4036 let describe_location () =
4037 let fn = page_of_y state.y in
4038 let ln = page_of_y (state.y + state.winh - state.hscrollh) in
4039 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4040 let percent =
4041 if maxy <= 0
4042 then 100.
4043 else (100. *. (float state.y /. float maxy))
4045 if fn = ln
4046 then
4047 Printf.sprintf "page %d of %d [%.2f%%]"
4048 (fn+1) state.pagecount percent
4049 else
4050 Printf.sprintf
4051 "pages %d-%d of %d [%.2f%%]"
4052 (fn+1) (ln+1) state.pagecount percent
4055 let setpresentationmode v =
4056 let n = page_of_y state.y in
4057 state.anchor <- (n, 0.0, 1.0);
4058 conf.presentation <- v;
4059 if conf.presentation
4060 then (
4061 if not conf.scrollbarinpm
4062 then state.scrollw <- 0;
4064 else state.scrollw <- conf.scrollbw;
4065 represent ();
4068 let enterinfomode =
4069 let btos b = if b then "\xe2\x88\x9a" else "" in
4070 let showextended = ref false in
4071 let leave mode = function
4072 | Confirm -> state.mode <- mode
4073 | Cancel -> state.mode <- mode in
4074 let src =
4075 (object
4076 val mutable m_first_time = true
4077 val mutable m_l = []
4078 val mutable m_a = [||]
4079 val mutable m_prev_uioh = nouioh
4080 val mutable m_prev_mode = View
4082 inherit lvsourcebase
4084 method reset prev_mode prev_uioh =
4085 m_a <- Array.of_list (List.rev m_l);
4086 m_l <- [];
4087 m_prev_mode <- prev_mode;
4088 m_prev_uioh <- prev_uioh;
4089 if m_first_time
4090 then (
4091 let rec loop n =
4092 if n >= Array.length m_a
4093 then ()
4094 else
4095 match m_a.(n) with
4096 | _, _, _, Action _ -> m_active <- n
4097 | _ -> loop (n+1)
4099 loop 0;
4100 m_first_time <- false;
4103 method int name get set =
4104 m_l <-
4105 (name, `int get, 1, Action (
4106 fun u ->
4107 let ondone s =
4108 try set (int_of_string s)
4109 with exn ->
4110 state.text <- Printf.sprintf "bad integer `%s': %s"
4111 s (exntos exn)
4113 state.text <- "";
4114 let te = name ^ ": ", "", None, intentry, ondone, true in
4115 state.mode <- Textentry (te, leave m_prev_mode);
4117 )) :: m_l
4119 method int_with_suffix name get set =
4120 m_l <-
4121 (name, `intws get, 1, Action (
4122 fun u ->
4123 let ondone s =
4124 try set (int_of_string_with_suffix s)
4125 with exn ->
4126 state.text <- Printf.sprintf "bad integer `%s': %s"
4127 s (exntos exn)
4129 state.text <- "";
4130 let te =
4131 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4133 state.mode <- Textentry (te, leave m_prev_mode);
4135 )) :: m_l
4137 method bool ?(offset=1) ?(btos=btos) name get set =
4138 m_l <-
4139 (name, `bool (btos, get), offset, Action (
4140 fun u ->
4141 let v = get () in
4142 set (not v);
4144 )) :: m_l
4146 method color name get set =
4147 m_l <-
4148 (name, `color get, 1, Action (
4149 fun u ->
4150 let invalid = (nan, nan, nan) in
4151 let ondone s =
4152 let c =
4153 try color_of_string s
4154 with exn ->
4155 state.text <- Printf.sprintf "bad color `%s': %s"
4156 s (exntos exn);
4157 invalid
4159 if c <> invalid
4160 then set c;
4162 let te = name ^ ": ", "", None, textentry, ondone, true in
4163 state.text <- color_to_string (get ());
4164 state.mode <- Textentry (te, leave m_prev_mode);
4166 )) :: m_l
4168 method string name get set =
4169 m_l <-
4170 (name, `string get, 1, Action (
4171 fun u ->
4172 let ondone s = set s in
4173 let te = name ^ ": ", "", None, textentry, ondone, true in
4174 state.mode <- Textentry (te, leave m_prev_mode);
4176 )) :: m_l
4178 method colorspace name get set =
4179 m_l <-
4180 (name, `string get, 1, Action (
4181 fun _ ->
4182 let source =
4183 let vals = [| "rgb"; "bgr"; "gray" |] in
4184 (object
4185 inherit lvsourcebase
4187 initializer
4188 m_active <- int_of_colorspace conf.colorspace;
4189 m_first <- 0;
4191 method getitemcount = Array.length vals
4192 method getitem n = (vals.(n), 0)
4193 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4194 ignore (uioh, first, pan, qsearch);
4195 if not cancel then set active;
4196 None
4197 method hasaction _ = true
4198 end)
4200 state.text <- "";
4201 let modehash = findkeyhash conf "info" in
4202 coe (new listview ~source ~trusted:true ~modehash)
4203 )) :: m_l
4205 method caption s offset =
4206 m_l <- (s, `empty, offset, Noaction) :: m_l
4208 method caption2 s f offset =
4209 m_l <- (s, `string f, offset, Noaction) :: m_l
4211 method getitemcount = Array.length m_a
4213 method getitem n =
4214 let tostr = function
4215 | `int f -> string_of_int (f ())
4216 | `intws f -> string_with_suffix_of_int (f ())
4217 | `string f -> f ()
4218 | `color f -> color_to_string (f ())
4219 | `bool (btos, f) -> btos (f ())
4220 | `empty -> ""
4222 let name, t, offset, _ = m_a.(n) in
4223 ((let s = tostr t in
4224 if String.length s > 0
4225 then Printf.sprintf "%s\t%s" name s
4226 else name),
4227 offset)
4229 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4230 let uiohopt =
4231 if not cancel
4232 then (
4233 m_qsearch <- qsearch;
4234 let uioh =
4235 match m_a.(active) with
4236 | _, _, _, Action f -> f uioh
4237 | _ -> uioh
4239 Some uioh
4241 else None
4243 m_active <- active;
4244 m_first <- first;
4245 m_pan <- pan;
4246 uiohopt
4248 method hasaction n =
4249 match m_a.(n) with
4250 | _, _, _, Action _ -> true
4251 | _ -> false
4252 end)
4254 let rec fillsrc prevmode prevuioh =
4255 let sep () = src#caption "" 0 in
4256 let colorp name get set =
4257 src#string name
4258 (fun () -> color_to_string (get ()))
4259 (fun v ->
4261 let c = color_of_string v in
4262 set c
4263 with exn ->
4264 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4267 let oldmode = state.mode in
4268 let birdseye = isbirdseye state.mode in
4270 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4272 src#bool "presentation mode"
4273 (fun () -> conf.presentation)
4274 (fun v -> setpresentationmode v);
4276 src#bool "ignore case in searches"
4277 (fun () -> conf.icase)
4278 (fun v -> conf.icase <- v);
4280 src#bool "preload"
4281 (fun () -> conf.preload)
4282 (fun v -> conf.preload <- v);
4284 src#bool "highlight links"
4285 (fun () -> conf.hlinks)
4286 (fun v -> conf.hlinks <- v);
4288 src#bool "under info"
4289 (fun () -> conf.underinfo)
4290 (fun v -> conf.underinfo <- v);
4292 src#bool "persistent bookmarks"
4293 (fun () -> conf.savebmarks)
4294 (fun v -> conf.savebmarks <- v);
4296 src#bool "proportional display"
4297 (fun () -> conf.proportional)
4298 (fun v -> reqlayout conf.angle v);
4300 src#bool "trim margins"
4301 (fun () -> conf.trimmargins)
4302 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4304 src#bool "persistent location"
4305 (fun () -> conf.jumpback)
4306 (fun v -> conf.jumpback <- v);
4308 sep ();
4309 src#int "inter-page space"
4310 (fun () -> conf.interpagespace)
4311 (fun n ->
4312 conf.interpagespace <- n;
4313 docolumns conf.columns;
4314 let pageno, py =
4315 match state.layout with
4316 | [] -> 0, 0
4317 | l :: _ ->
4318 l.pageno, l.pagey
4320 state.maxy <- calcheight ();
4321 let y = getpagey pageno in
4322 gotoy (y + py)
4325 src#int "page bias"
4326 (fun () -> conf.pagebias)
4327 (fun v -> conf.pagebias <- v);
4329 src#int "scroll step"
4330 (fun () -> conf.scrollstep)
4331 (fun n -> conf.scrollstep <- n);
4333 src#int "horizontal scroll step"
4334 (fun () -> conf.hscrollstep)
4335 (fun v -> conf.hscrollstep <- v);
4337 src#int "auto scroll step"
4338 (fun () ->
4339 match state.autoscroll with
4340 | Some step -> step
4341 | _ -> conf.autoscrollstep)
4342 (fun n ->
4343 if state.autoscroll <> None
4344 then state.autoscroll <- Some n;
4345 conf.autoscrollstep <- n);
4347 src#int "zoom"
4348 (fun () -> truncate (conf.zoom *. 100.))
4349 (fun v -> setzoom ((float v) /. 100.));
4351 src#int "rotation"
4352 (fun () -> conf.angle)
4353 (fun v -> reqlayout v conf.proportional);
4355 src#int "scroll bar width"
4356 (fun () -> state.scrollw)
4357 (fun v ->
4358 state.scrollw <- v;
4359 conf.scrollbw <- v;
4360 reshape state.winw state.winh;
4363 src#int "scroll handle height"
4364 (fun () -> conf.scrollh)
4365 (fun v -> conf.scrollh <- v;);
4367 src#int "thumbnail width"
4368 (fun () -> conf.thumbw)
4369 (fun v ->
4370 conf.thumbw <- min 4096 v;
4371 match oldmode with
4372 | Birdseye beye ->
4373 leavebirdseye beye false;
4374 enterbirdseye ()
4375 | _ -> ()
4378 let mode = state.mode in
4379 src#string "columns"
4380 (fun () ->
4381 match conf.columns with
4382 | Csingle _ -> "1"
4383 | Cmulti (multi, _) -> multicolumns_to_string multi
4384 | Csplit (count, _) -> "-" ^ string_of_int count
4386 (fun v ->
4387 let n, a, b = multicolumns_of_string v in
4388 setcolumns mode n a b);
4390 sep ();
4391 src#caption "Presentation mode" 0;
4392 src#bool "scrollbar visible"
4393 (fun () -> conf.scrollbarinpm)
4394 (fun v ->
4395 if v != conf.scrollbarinpm
4396 then (
4397 conf.scrollbarinpm <- v;
4398 if conf.presentation
4399 then (
4400 state.scrollw <- if v then conf.scrollbw else 0;
4401 reshape state.winw state.winh;
4406 sep ();
4407 src#caption "Pixmap cache" 0;
4408 src#int_with_suffix "size (advisory)"
4409 (fun () -> conf.memlimit)
4410 (fun v -> conf.memlimit <- v);
4412 src#caption2 "used"
4413 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4414 (string_with_suffix_of_int state.memused)
4415 (Hashtbl.length state.tilemap)) 1;
4417 sep ();
4418 src#caption "Layout" 0;
4419 src#caption2 "Dimension"
4420 (fun () ->
4421 Printf.sprintf "%dx%d (virtual %dx%d)"
4422 state.winw state.winh
4423 state.w state.maxy)
4425 if conf.debug
4426 then
4427 src#caption2 "Position" (fun () ->
4428 Printf.sprintf "%dx%d" state.x state.y
4430 else
4431 src#caption2 "Position" (fun () -> describe_location ()) 1
4434 sep ();
4435 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4436 "Save these parameters as global defaults at exit"
4437 (fun () -> conf.bedefault)
4438 (fun v -> conf.bedefault <- v)
4441 sep ();
4442 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4443 src#bool ~offset:0 ~btos "Extended parameters"
4444 (fun () -> !showextended)
4445 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4446 if !showextended
4447 then (
4448 src#bool "checkers"
4449 (fun () -> conf.checkers)
4450 (fun v -> conf.checkers <- v; setcheckers v);
4451 src#bool "update cursor"
4452 (fun () -> conf.updatecurs)
4453 (fun v -> conf.updatecurs <- v);
4454 src#bool "verbose"
4455 (fun () -> conf.verbose)
4456 (fun v -> conf.verbose <- v);
4457 src#bool "invert colors"
4458 (fun () -> conf.invert)
4459 (fun v -> conf.invert <- v);
4460 src#bool "max fit"
4461 (fun () -> conf.maxhfit)
4462 (fun v -> conf.maxhfit <- v);
4463 src#bool "redirect stderr"
4464 (fun () -> conf.redirectstderr)
4465 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4466 src#string "uri launcher"
4467 (fun () -> conf.urilauncher)
4468 (fun v -> conf.urilauncher <- v);
4469 src#string "path launcher"
4470 (fun () -> conf.pathlauncher)
4471 (fun v -> conf.pathlauncher <- v);
4472 src#string "tile size"
4473 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4474 (fun v ->
4476 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4477 conf.tilew <- max 64 w;
4478 conf.tileh <- max 64 h;
4479 flushtiles ();
4480 with exn ->
4481 state.text <- Printf.sprintf "bad tile size `%s': %s"
4482 v (exntos exn)
4484 src#int "texture count"
4485 (fun () -> conf.texcount)
4486 (fun v ->
4487 if realloctexts v
4488 then conf.texcount <- v
4489 else showtext '!' " Failed to set texture count please retry later"
4491 src#int "slice height"
4492 (fun () -> conf.sliceheight)
4493 (fun v ->
4494 conf.sliceheight <- v;
4495 wcmd "sliceh %d" conf.sliceheight;
4497 src#int "anti-aliasing level"
4498 (fun () -> conf.aalevel)
4499 (fun v ->
4500 conf.aalevel <- bound v 0 8;
4501 state.anchor <- getanchor ();
4502 opendoc state.path state.password;
4504 src#string "page scroll scaling factor"
4505 (fun () -> string_of_float conf.pgscale)
4506 (fun v ->
4508 let s = float_of_string v in
4509 conf.pgscale <- s
4510 with exn ->
4511 state.text <- Printf.sprintf
4512 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4515 src#int "ui font size"
4516 (fun () -> fstate.fontsize)
4517 (fun v -> setfontsize (bound v 5 100));
4518 src#int "hint font size"
4519 (fun () -> conf.hfsize)
4520 (fun v -> conf.hfsize <- bound v 5 100);
4521 colorp "background color"
4522 (fun () -> conf.bgcolor)
4523 (fun v -> conf.bgcolor <- v);
4524 src#bool "crop hack"
4525 (fun () -> conf.crophack)
4526 (fun v -> conf.crophack <- v);
4527 src#string "trim fuzz"
4528 (fun () -> irect_to_string conf.trimfuzz)
4529 (fun v ->
4531 conf.trimfuzz <- irect_of_string v;
4532 if conf.trimmargins
4533 then settrim true conf.trimfuzz;
4534 with exn ->
4535 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4537 src#string "throttle"
4538 (fun () ->
4539 match conf.maxwait with
4540 | None -> "show place holder if page is not ready"
4541 | Some time ->
4542 if time = infinity
4543 then "wait for page to fully render"
4544 else
4545 "wait " ^ string_of_float time
4546 ^ " seconds before showing placeholder"
4548 (fun v ->
4550 let f = float_of_string v in
4551 if f <= 0.0
4552 then conf.maxwait <- None
4553 else conf.maxwait <- Some f
4554 with exn ->
4555 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4557 src#string "ghyll scroll"
4558 (fun () ->
4559 match conf.ghyllscroll with
4560 | None -> ""
4561 | Some nab -> ghyllscroll_to_string nab
4563 (fun v ->
4565 let gs =
4566 if String.length v = 0
4567 then None
4568 else Some (ghyllscroll_of_string v)
4570 conf.ghyllscroll <- gs
4571 with exn ->
4572 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4574 src#string "selection command"
4575 (fun () -> conf.selcmd)
4576 (fun v -> conf.selcmd <- v);
4577 src#string "synctex command"
4578 (fun () -> conf.stcmd)
4579 (fun v -> conf.stcmd <- v);
4580 src#colorspace "color space"
4581 (fun () -> colorspace_to_string conf.colorspace)
4582 (fun v ->
4583 conf.colorspace <- colorspace_of_int v;
4584 wcmd "cs %d" v;
4585 load state.layout;
4587 if pbousable ()
4588 then
4589 src#bool "use PBO"
4590 (fun () -> conf.usepbo)
4591 (fun v -> conf.usepbo <- v);
4592 src#bool "mouse wheel scrolls pages"
4593 (fun () -> conf.wheelbypage)
4594 (fun v -> conf.wheelbypage <- v);
4597 sep ();
4598 src#caption "Document" 0;
4599 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4600 src#caption2 "Pages"
4601 (fun () -> string_of_int state.pagecount) 1;
4602 src#caption2 "Dimensions"
4603 (fun () -> string_of_int (List.length state.pdims)) 1;
4604 if conf.trimmargins
4605 then (
4606 sep ();
4607 src#caption "Trimmed margins" 0;
4608 src#caption2 "Dimensions"
4609 (fun () -> string_of_int (List.length state.pdims)) 1;
4612 sep ();
4613 src#caption "OpenGL" 0;
4614 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4615 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4616 src#reset prevmode prevuioh;
4618 fun () ->
4619 state.text <- "";
4620 let prevmode = state.mode
4621 and prevuioh = state.uioh in
4622 fillsrc prevmode prevuioh;
4623 let source = (src :> lvsource) in
4624 let modehash = findkeyhash conf "info" in
4625 state.uioh <- coe (object (self)
4626 inherit listview ~source ~trusted:true ~modehash as super
4627 val mutable m_prevmemused = 0
4628 method infochanged = function
4629 | Memused ->
4630 if m_prevmemused != state.memused
4631 then (
4632 m_prevmemused <- state.memused;
4633 G.postRedisplay "memusedchanged";
4635 | Pdim -> G.postRedisplay "pdimchanged"
4636 | Docinfo -> fillsrc prevmode prevuioh
4638 method key key mask =
4639 if not (Wsi.withctrl mask)
4640 then
4641 match key with
4642 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4643 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4644 | _ -> super#key key mask
4645 else super#key key mask
4646 end);
4647 G.postRedisplay "info";
4650 let enterhelpmode =
4651 let source =
4652 (object
4653 inherit lvsourcebase
4654 method getitemcount = Array.length state.help
4655 method getitem n =
4656 let s, l, _ = state.help.(n) in
4657 (s, l)
4659 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4660 let optuioh =
4661 if not cancel
4662 then (
4663 m_qsearch <- qsearch;
4664 match state.help.(active) with
4665 | _, _, Action f -> Some (f uioh)
4666 | _ -> Some (uioh)
4668 else None
4670 m_active <- active;
4671 m_first <- first;
4672 m_pan <- pan;
4673 optuioh
4675 method hasaction n =
4676 match state.help.(n) with
4677 | _, _, Action _ -> true
4678 | _ -> false
4680 initializer
4681 m_active <- -1
4682 end)
4683 in fun () ->
4684 let modehash = findkeyhash conf "help" in
4685 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4686 G.postRedisplay "help";
4689 let entermsgsmode =
4690 let msgsource =
4691 let re = Str.regexp "[\r\n]" in
4692 (object
4693 inherit lvsourcebase
4694 val mutable m_items = [||]
4696 method getitemcount = 1 + Array.length m_items
4698 method getitem n =
4699 if n = 0
4700 then "[Clear]", 0
4701 else m_items.(n-1), 0
4703 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4704 ignore uioh;
4705 if not cancel
4706 then (
4707 if active = 0
4708 then Buffer.clear state.errmsgs;
4709 m_qsearch <- qsearch;
4711 m_active <- active;
4712 m_first <- first;
4713 m_pan <- pan;
4714 None
4716 method hasaction n =
4717 n = 0
4719 method reset =
4720 state.newerrmsgs <- false;
4721 let l = Str.split re (Buffer.contents state.errmsgs) in
4722 m_items <- Array.of_list l
4724 initializer
4725 m_active <- 0
4726 end)
4727 in fun () ->
4728 state.text <- "";
4729 msgsource#reset;
4730 let source = (msgsource :> lvsource) in
4731 let modehash = findkeyhash conf "listview" in
4732 state.uioh <- coe (object
4733 inherit listview ~source ~trusted:false ~modehash as super
4734 method display =
4735 if state.newerrmsgs
4736 then msgsource#reset;
4737 super#display
4738 end);
4739 G.postRedisplay "msgs";
4742 let quickbookmark ?title () =
4743 match state.layout with
4744 | [] -> ()
4745 | l :: _ ->
4746 let title =
4747 match title with
4748 | None ->
4749 let sec = Unix.gettimeofday () in
4750 let tm = Unix.localtime sec in
4751 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4752 (l.pageno+1)
4753 tm.Unix.tm_mday
4754 tm.Unix.tm_mon
4755 (tm.Unix.tm_year + 1900)
4756 tm.Unix.tm_hour
4757 tm.Unix.tm_min
4758 | Some title -> title
4760 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4763 let doreshape w h =
4764 Wsi.reshape w h;
4767 let setautoscrollspeed step goingdown =
4768 let incr = max 1 ((abs step) / 2) in
4769 let incr = if goingdown then incr else -incr in
4770 let astep = step + incr in
4771 state.autoscroll <- Some astep;
4774 let gotounder = function
4775 | Ulinkgoto (pageno, top) ->
4776 if pageno >= 0
4777 then (
4778 addnav ();
4779 gotopage1 pageno top;
4782 | Ulinkuri s ->
4783 gotouri s
4785 | Uremote (filename, pageno) ->
4786 let path =
4787 if Sys.file_exists filename
4788 then filename
4789 else
4790 let dir = Filename.dirname state.path in
4791 let path = Filename.concat dir filename in
4792 if Sys.file_exists path
4793 then path
4794 else ""
4796 if String.length path > 0
4797 then (
4798 let anchor = getanchor () in
4799 let ranchor = state.path, state.password, anchor in
4800 state.anchor <- (pageno, 0.0, 0.0);
4801 state.ranchors <- ranchor :: state.ranchors;
4802 opendoc path "";
4804 else showtext '!' ("Could not find " ^ filename)
4806 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4809 let canpan () =
4810 match conf.columns with
4811 | Csplit _ -> true
4812 | _ -> state.x != 0 || conf.zoom > 1.0
4815 let existsinrow pageno (columns, coverA, coverB) p =
4816 let last = ((pageno - coverA) mod columns) + columns in
4817 let rec any = function
4818 | [] -> false
4819 | l :: rest ->
4820 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4821 then p l
4822 else (
4823 if not (p l)
4824 then (if l.pageno = last then false else any rest)
4825 else true
4828 any state.layout
4831 let nextpage () =
4832 match state.layout with
4833 | [] ->
4834 let pageno = page_of_y state.y in
4835 gotoghyll (getpagey (pageno+1))
4836 | l :: rest ->
4837 match conf.columns with
4838 | Csingle _ ->
4839 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4840 then
4841 let y = clamp (pgscale state.winh) in
4842 gotoghyll y
4843 else
4844 let pageno = min (l.pageno+1) (state.pagecount-1) in
4845 gotoghyll (getpagey pageno)
4846 | Cmulti ((c, _, _) as cl, _) ->
4847 if conf.presentation
4848 && (existsinrow l.pageno cl
4849 (fun l -> l.pageh > l.pagey + l.pagevh))
4850 then
4851 let y = clamp (pgscale state.winh) in
4852 gotoghyll y
4853 else
4854 let pageno = min (l.pageno+c) (state.pagecount-1) in
4855 gotoghyll (getpagey pageno)
4856 | Csplit (n, _) ->
4857 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4858 then
4859 let pagey, pageh = getpageyh l.pageno in
4860 let pagey = pagey + pageh * l.pagecol in
4861 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4862 gotoghyll (pagey + pageh + ips)
4865 let prevpage () =
4866 match state.layout with
4867 | [] ->
4868 let pageno = page_of_y state.y in
4869 gotoghyll (getpagey (pageno-1))
4870 | l :: _ ->
4871 match conf.columns with
4872 | Csingle _ ->
4873 if conf.presentation && l.pagey != 0
4874 then
4875 gotoghyll (clamp (pgscale ~-(state.winh)))
4876 else
4877 let pageno = max 0 (l.pageno-1) in
4878 gotoghyll (getpagey pageno)
4879 | Cmulti ((c, _, coverB) as cl, _) ->
4880 if conf.presentation &&
4881 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4882 then
4883 gotoghyll (clamp (pgscale ~-(state.winh)))
4884 else
4885 let decr =
4886 if l.pageno = state.pagecount - coverB
4887 then 1
4888 else c
4890 let pageno = max 0 (l.pageno-decr) in
4891 gotoghyll (getpagey pageno)
4892 | Csplit (n, _) ->
4893 let y =
4894 if l.pagecol = 0
4895 then
4896 if l.pageno = 0
4897 then l.pagey
4898 else
4899 let pageno = max 0 (l.pageno-1) in
4900 let pagey, pageh = getpageyh pageno in
4901 pagey + (n-1)*pageh
4902 else
4903 let pagey, pageh = getpageyh l.pageno in
4904 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4906 gotoghyll y
4909 let viewkeyboard key mask =
4910 let enttext te =
4911 let mode = state.mode in
4912 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4913 state.text <- "";
4914 enttext ();
4915 G.postRedisplay "view:enttext"
4917 let ctrl = Wsi.withctrl mask in
4918 let key =
4919 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4921 match key with
4922 | 81 -> (* Q *)
4923 exit 0
4925 | 0xff63 -> (* insert *)
4926 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4927 then (
4928 state.mode <- LinkNav (Ltgendir 0);
4929 gotoy state.y;
4931 else showtext '!' "Keyboard link navigation does not work under rotation"
4933 | 0xff1b | 113 -> (* escape / q *)
4934 begin match state.mstate with
4935 | Mzoomrect _ ->
4936 state.mstate <- Mnone;
4937 Wsi.setcursor Wsi.CURSOR_INHERIT;
4938 G.postRedisplay "kill zoom rect";
4939 | _ ->
4940 begin match state.mode with
4941 | LinkNav _ ->
4942 state.mode <- View;
4943 G.postRedisplay "esc leave linknav"
4944 | _ ->
4945 match state.ranchors with
4946 | [] -> raise Quit
4947 | (path, password, anchor) :: rest ->
4948 state.ranchors <- rest;
4949 state.anchor <- anchor;
4950 opendoc path password
4951 end;
4952 end;
4954 | 0xff08 -> (* backspace *)
4955 gotoghyll (getnav ~-1)
4957 | 111 -> (* o *)
4958 enteroutlinemode ()
4960 | 117 -> (* u *)
4961 state.rects <- [];
4962 state.text <- "";
4963 G.postRedisplay "dehighlight";
4965 | 47 | 63 -> (* / ? *)
4966 let ondone isforw s =
4967 cbput state.hists.pat s;
4968 state.searchpattern <- s;
4969 search s isforw
4971 let s = String.create 1 in
4972 s.[0] <- Char.chr key;
4973 enttext (s, "", Some (onhist state.hists.pat),
4974 textentry, ondone (key = 47), true)
4976 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4977 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4978 setzoom (conf.zoom +. incr)
4980 | 43 | 0xffab -> (* + *)
4981 let ondone s =
4982 let n =
4983 try int_of_string s with exc ->
4984 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4985 max_int
4987 if n != max_int
4988 then (
4989 conf.pagebias <- n;
4990 state.text <- "page bias is now " ^ string_of_int n;
4993 enttext ("page bias: ", "", None, intentry, ondone, true)
4995 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4996 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4997 setzoom (max 0.01 (conf.zoom -. decr))
4999 | 45 | 0xffad -> (* - *)
5000 let ondone msg = state.text <- msg in
5001 enttext (
5002 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5003 optentry state.mode, ondone, true
5006 | 48 when ctrl -> (* ctrl-0 *)
5007 if conf.zoom = 1.0
5008 then (
5009 state.x <- 0;
5010 state.hscrollh <-
5011 if state.w <= state.winw - state.scrollw
5012 then 0
5013 else state.scrollw
5015 gotoy state.y
5017 else setzoom 1.0
5019 | 49 | 50 when ctrl -> (* ctrl-1/2 *)
5020 let cols =
5021 match conf.columns with
5022 | Csingle _ | Cmulti _ -> 1
5023 | Csplit (n, _) -> n
5025 let h = state.winh -
5026 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5028 let zoom = zoomforh state.winw h state.scrollw cols in
5029 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5030 then setzoom zoom
5032 | 0xffc6 -> (* f9 *)
5033 togglebirdseye ()
5035 | 57 when ctrl -> (* ctrl-9 *)
5036 togglebirdseye ()
5038 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5039 when not ctrl -> (* 0..9 *)
5040 let ondone s =
5041 let n =
5042 try int_of_string s with exc ->
5043 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5046 if n >= 0
5047 then (
5048 addnav ();
5049 cbput state.hists.pag (string_of_int n);
5050 gotopage1 (n + conf.pagebias - 1) 0;
5053 let pageentry text key =
5054 match Char.unsafe_chr key with
5055 | 'g' -> TEdone text
5056 | _ -> intentry text key
5058 let text = "x" in text.[0] <- Char.chr key;
5059 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5061 | 98 -> (* b *)
5062 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5063 reshape state.winw state.winh;
5065 | 108 -> (* l *)
5066 conf.hlinks <- not conf.hlinks;
5067 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5068 G.postRedisplay "toggle highlightlinks";
5070 | 70 -> (* F *)
5071 state.glinks <- true;
5072 let mode = state.mode in
5073 state.mode <- Textentry (
5074 (":", "", None, linknentry, linkndone gotounder, false),
5075 (fun _ ->
5076 state.glinks <- false;
5077 state.mode <- mode)
5079 state.text <- "";
5080 G.postRedisplay "view:linkent(F)"
5082 | 121 -> (* y *)
5083 state.glinks <- true;
5084 let mode = state.mode in
5085 state.mode <- Textentry (
5086 (":", "", None, linknentry, linkndone (fun under ->
5087 match Ne.pipe () with
5088 | Ne.Exn exn ->
5089 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5090 | Ne.Res (r, w) ->
5091 let popened =
5092 try popen conf.selcmd [r, 0; w, -1]; true
5093 with exn ->
5094 showtext '!'
5095 (Printf.sprintf "failed to execute %s: %s"
5096 conf.selcmd (exntos exn));
5097 false
5099 let clo cap fd =
5100 Ne.clo fd (fun msg ->
5101 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5104 let s = undertext under in
5105 if popened
5106 then
5107 (try
5108 let l = String.length s in
5109 let n = tempfailureretry (Unix.write w s 0) l in
5110 if n != l
5111 then
5112 showtext '!'
5113 (Printf.sprintf
5114 "failed to write %d characters to sel pipe, wrote %d"
5117 with exn ->
5118 showtext '!'
5119 (Printf.sprintf "failed to write to sel pipe: %s"
5120 (exntos exn)
5123 else dolog "%s" s;
5124 clo "pipe/r" r;
5125 clo "pipe/w" w;
5126 ), false
5128 fun _ ->
5129 state.glinks <- false;
5130 state.mode <- mode
5132 state.text <- "";
5133 G.postRedisplay "view:linkent"
5135 | 97 -> (* a *)
5136 begin match state.autoscroll with
5137 | Some step ->
5138 conf.autoscrollstep <- step;
5139 state.autoscroll <- None
5140 | None ->
5141 if conf.autoscrollstep = 0
5142 then state.autoscroll <- Some 1
5143 else state.autoscroll <- Some conf.autoscrollstep
5146 | 112 when ctrl -> (* ctrl-p *)
5147 launchpath ()
5149 | 80 -> (* P *)
5150 setpresentationmode (not conf.presentation);
5151 showtext ' ' ("presentation mode " ^
5152 if conf.presentation then "on" else "off");
5154 | 102 -> (* f *)
5155 if List.mem Wsi.Fullscreen state.winstate
5156 then doreshape conf.cwinw conf.cwinh
5157 else Wsi.fullscreen ()
5159 | 112 | 78 -> (* p|N *)
5160 search state.searchpattern false
5162 | 110 | 0xffc0 -> (* n|F3 *)
5163 search state.searchpattern true
5165 | 116 -> (* t *)
5166 begin match state.layout with
5167 | [] -> ()
5168 | l :: _ ->
5169 gotoghyll (getpagey l.pageno)
5172 | 32 -> (* space *)
5173 nextpage ()
5175 | 0xff9f | 0xffff -> (* delete *)
5176 prevpage ()
5178 | 61 -> (* = *)
5179 showtext ' ' (describe_location ());
5181 | 119 -> (* w *)
5182 begin match state.layout with
5183 | [] -> ()
5184 | l :: _ ->
5185 doreshape (l.pagew + state.scrollw) l.pageh;
5186 G.postRedisplay "w"
5189 | 39 -> (* ' *)
5190 enterbookmarkmode ()
5192 | 104 | 0xffbe -> (* h|F1 *)
5193 enterhelpmode ()
5195 | 105 -> (* i *)
5196 enterinfomode ()
5198 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5199 entermsgsmode ()
5201 | 109 -> (* m *)
5202 let ondone s =
5203 match state.layout with
5204 | l :: _ ->
5205 if String.length s > 0
5206 then
5207 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5208 | _ -> ()
5210 enttext ("bookmark: ", "", None, textentry, ondone, true)
5212 | 126 -> (* ~ *)
5213 quickbookmark ();
5214 showtext ' ' "Quick bookmark added";
5216 | 122 -> (* z *)
5217 begin match state.layout with
5218 | l :: _ ->
5219 let rect = getpdimrect l.pagedimno in
5220 let w, h =
5221 if conf.crophack
5222 then
5223 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5224 truncate (1.2 *. (rect.(3) -. rect.(0))))
5225 else
5226 (truncate (rect.(1) -. rect.(0)),
5227 truncate (rect.(3) -. rect.(0)))
5229 let w = truncate ((float w)*.conf.zoom)
5230 and h = truncate ((float h)*.conf.zoom) in
5231 if w != 0 && h != 0
5232 then (
5233 state.anchor <- getanchor ();
5234 doreshape (w + state.scrollw) (h + conf.interpagespace)
5236 G.postRedisplay "z";
5238 | [] -> ()
5241 | 60 | 62 -> (* < > *)
5242 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5244 | 91 | 93 -> (* [ ] *)
5245 conf.colorscale <-
5246 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5248 G.postRedisplay "brightness";
5250 | 99 when state.mode = View -> (* c *)
5251 let (c, a, b), z =
5252 match state.prevcolumns with
5253 | None -> (1, 0, 0), 1.0
5254 | Some (columns, z) ->
5255 let cab =
5256 match columns with
5257 | Csplit (c, _) -> -c, 0, 0
5258 | Cmulti ((c, a, b), _) -> c, a, b
5259 | Csingle _ -> 1, 0, 0
5261 cab, z
5263 setcolumns View c a b;
5264 setzoom z;
5266 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5267 setzoom state.prevzoom
5269 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5270 begin match state.autoscroll with
5271 | None ->
5272 begin match state.mode with
5273 | Birdseye beye -> upbirdseye 1 beye
5274 | _ ->
5275 if ctrl
5276 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5277 else (
5278 if not (Wsi.withshift mask) && conf.presentation
5279 then prevpage ()
5280 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5283 | Some n ->
5284 setautoscrollspeed n false
5287 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5288 begin match state.autoscroll with
5289 | None ->
5290 begin match state.mode with
5291 | Birdseye beye -> downbirdseye 1 beye
5292 | _ ->
5293 if ctrl
5294 then gotoy_and_clear_text (clamp (state.winh/2))
5295 else (
5296 if not (Wsi.withshift mask) && conf.presentation
5297 then nextpage ()
5298 else gotoy_and_clear_text (clamp conf.scrollstep)
5301 | Some n ->
5302 setautoscrollspeed n true
5305 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5306 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5307 if canpan ()
5308 then
5309 let dx =
5310 if ctrl
5311 then state.winw / 2
5312 else conf.hscrollstep
5314 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5315 state.x <- state.x + dx;
5316 gotoy_and_clear_text state.y
5317 else (
5318 state.text <- "";
5319 G.postRedisplay "lef/right"
5322 | 0xff55 | 0xff9a -> (* (kp) prior *)
5323 let y =
5324 if ctrl
5325 then
5326 match state.layout with
5327 | [] -> state.y
5328 | l :: _ -> state.y - l.pagey
5329 else
5330 clamp (pgscale (-state.winh))
5332 gotoghyll y
5334 | 0xff56 | 0xff9b -> (* (kp) next *)
5335 let y =
5336 if ctrl
5337 then
5338 match List.rev state.layout with
5339 | [] -> state.y
5340 | l :: _ -> getpagey l.pageno
5341 else
5342 clamp (pgscale state.winh)
5344 gotoghyll y
5346 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5347 gotoghyll 0
5348 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5349 gotoghyll (clamp state.maxy)
5351 | 0xff53 | 0xff98
5352 when Wsi.withalt mask -> (* alt-(kp) right *)
5353 gotoghyll (getnav 1)
5354 | 0xff51 | 0xff96
5355 when Wsi.withalt mask -> (* alt-(kp) left *)
5356 gotoghyll (getnav ~-1)
5358 | 114 -> (* r *)
5359 reload ()
5361 | 118 when conf.debug -> (* v *)
5362 state.rects <- [];
5363 List.iter (fun l ->
5364 match getopaque l.pageno with
5365 | None -> ()
5366 | Some opaque ->
5367 let x0, y0, x1, y1 = pagebbox opaque in
5368 let a,b = float x0, float y0 in
5369 let c,d = float x1, float y0 in
5370 let e,f = float x1, float y1 in
5371 let h,j = float x0, float y1 in
5372 let rect = (a,b,c,d,e,f,h,j) in
5373 debugrect rect;
5374 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5375 ) state.layout;
5376 G.postRedisplay "v";
5378 | _ ->
5379 vlog "huh? %s" (Wsi.keyname key)
5382 let linknavkeyboard key mask linknav =
5383 let getpage pageno =
5384 let rec loop = function
5385 | [] -> None
5386 | l :: _ when l.pageno = pageno -> Some l
5387 | _ :: rest -> loop rest
5388 in loop state.layout
5390 let doexact (pageno, n) =
5391 match getopaque pageno, getpage pageno with
5392 | Some opaque, Some l ->
5393 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5394 then
5395 let under = getlink opaque n in
5396 G.postRedisplay "link gotounder";
5397 gotounder under;
5398 state.mode <- View;
5399 else
5400 let opt, dir =
5401 match key with
5402 | 0xff50 -> (* home *)
5403 Some (findlink opaque LDfirst), -1
5405 | 0xff57 -> (* end *)
5406 Some (findlink opaque LDlast), 1
5408 | 0xff51 -> (* left *)
5409 Some (findlink opaque (LDleft n)), -1
5411 | 0xff53 -> (* right *)
5412 Some (findlink opaque (LDright n)), 1
5414 | 0xff52 -> (* up *)
5415 Some (findlink opaque (LDup n)), -1
5417 | 0xff54 -> (* down *)
5418 Some (findlink opaque (LDdown n)), 1
5420 | _ -> None, 0
5422 let pwl l dir =
5423 begin match findpwl l.pageno dir with
5424 | Pwlnotfound -> ()
5425 | Pwl pageno ->
5426 let notfound dir =
5427 state.mode <- LinkNav (Ltgendir dir);
5428 let y, h = getpageyh pageno in
5429 let y =
5430 if dir < 0
5431 then y + h - state.winh
5432 else y
5434 gotoy y
5436 begin match getopaque pageno, getpage pageno with
5437 | Some opaque, Some _ ->
5438 let link =
5439 let ld = if dir > 0 then LDfirst else LDlast in
5440 findlink opaque ld
5442 begin match link with
5443 | Lfound m ->
5444 showlinktype (getlink opaque m);
5445 state.mode <- LinkNav (Ltexact (pageno, m));
5446 G.postRedisplay "linknav jpage";
5447 | _ -> notfound dir
5448 end;
5449 | _ -> notfound dir
5450 end;
5451 end;
5453 begin match opt with
5454 | Some Lnotfound -> pwl l dir;
5455 | Some (Lfound m) ->
5456 if m = n
5457 then pwl l dir
5458 else (
5459 let _, y0, _, y1 = getlinkrect opaque m in
5460 if y0 < l.pagey
5461 then gotopage1 l.pageno y0
5462 else (
5463 let d = fstate.fontsize + 1 in
5464 if y1 - l.pagey > l.pagevh - d
5465 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5466 else G.postRedisplay "linknav";
5468 showlinktype (getlink opaque m);
5469 state.mode <- LinkNav (Ltexact (l.pageno, m));
5472 | None -> viewkeyboard key mask
5473 end;
5474 | _ -> viewkeyboard key mask
5476 if key = 0xff63
5477 then (
5478 state.mode <- View;
5479 G.postRedisplay "leave linknav"
5481 else
5482 match linknav with
5483 | Ltgendir _ -> viewkeyboard key mask
5484 | Ltexact exact -> doexact exact
5487 let keyboard key mask =
5488 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5489 then wcmd "interrupt"
5490 else state.uioh <- state.uioh#key key mask
5493 let birdseyekeyboard key mask
5494 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5495 let incr =
5496 match conf.columns with
5497 | Csingle _ -> 1
5498 | Cmulti ((c, _, _), _) -> c
5499 | Csplit _ -> failwith "bird's eye split mode"
5501 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5502 match key with
5503 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5504 let y, h = getpageyh pageno in
5505 let top = (state.winh - h) / 2 in
5506 gotoy (max 0 (y - top))
5507 | 0xff0d (* enter *)
5508 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5509 | 0xff1b -> leavebirdseye beye true (* escape *)
5510 | 0xff52 -> upbirdseye incr beye (* up *)
5511 | 0xff54 -> downbirdseye incr beye (* down *)
5512 | 0xff51 -> upbirdseye 1 beye (* left *)
5513 | 0xff53 -> downbirdseye 1 beye (* right *)
5515 | 0xff55 -> (* prior *)
5516 begin match state.layout with
5517 | l :: _ ->
5518 if l.pagey != 0
5519 then (
5520 state.mode <- Birdseye (
5521 oconf, leftx, l.pageno, hooverpageno, anchor
5523 gotopage1 l.pageno 0;
5525 else (
5526 let layout = layout (state.y-state.winh) (pgh state.layout) in
5527 match layout with
5528 | [] -> gotoy (clamp (-state.winh))
5529 | l :: _ ->
5530 state.mode <- Birdseye (
5531 oconf, leftx, l.pageno, hooverpageno, anchor
5533 gotopage1 l.pageno 0
5536 | [] -> gotoy (clamp (-state.winh))
5537 end;
5539 | 0xff56 -> (* next *)
5540 begin match List.rev state.layout with
5541 | l :: _ ->
5542 let layout = layout (state.y + (pgh state.layout)) state.winh in
5543 begin match layout with
5544 | [] ->
5545 let incr = l.pageh - l.pagevh in
5546 if incr = 0
5547 then (
5548 state.mode <-
5549 Birdseye (
5550 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5552 G.postRedisplay "birdseye pagedown";
5554 else gotoy (clamp (incr + conf.interpagespace*2));
5556 | l :: _ ->
5557 state.mode <-
5558 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5559 gotopage1 l.pageno 0;
5562 | [] -> gotoy (clamp state.winh)
5563 end;
5565 | 0xff50 -> (* home *)
5566 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5567 gotopage1 0 0
5569 | 0xff57 -> (* end *)
5570 let pageno = state.pagecount - 1 in
5571 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5572 if not (pagevisible state.layout pageno)
5573 then
5574 let h =
5575 match List.rev state.pdims with
5576 | [] -> state.winh
5577 | (_, _, h, _) :: _ -> h
5579 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5580 else G.postRedisplay "birdseye end";
5581 | _ -> viewkeyboard key mask
5584 let drawpage l linkindexbase =
5585 let color =
5586 match state.mode with
5587 | Textentry _ -> scalecolor 0.4
5588 | LinkNav _
5589 | View -> scalecolor 1.0
5590 | Birdseye (_, _, pageno, hooverpageno, _) ->
5591 if l.pageno = hooverpageno
5592 then scalecolor 0.9
5593 else (
5594 if l.pageno = pageno
5595 then scalecolor 1.0
5596 else scalecolor 0.8
5599 drawtiles l color;
5600 begin match getopaque l.pageno with
5601 | Some opaque ->
5602 if tileready l l.pagex l.pagey
5603 then
5604 let x = l.pagedispx - l.pagex
5605 and y = l.pagedispy - l.pagey in
5606 let hlmask =
5607 match conf.columns with
5608 | Csingle _ | Cmulti _ ->
5609 (if conf.hlinks then 1 else 0)
5610 + (if state.glinks
5611 && not (isbirdseye state.mode) then 2 else 0)
5612 | _ -> 0
5614 let s =
5615 match state.mode with
5616 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5617 | _ -> ""
5619 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5620 else 0
5622 | _ -> 0
5623 end;
5626 let scrollindicator () =
5627 let sbw, ph, sh = state.uioh#scrollph in
5628 let sbh, pw, sw = state.uioh#scrollpw in
5630 GlDraw.color (0.64, 0.64, 0.64);
5631 GlDraw.rect
5632 (float (state.winw - sbw), 0.)
5633 (float state.winw, float state.winh)
5635 GlDraw.rect
5636 (0., float (state.winh - sbh))
5637 (float (state.winw - state.scrollw - 1), float state.winh)
5639 GlDraw.color (0.0, 0.0, 0.0);
5641 GlDraw.rect
5642 (float (state.winw - sbw), ph)
5643 (float state.winw, ph +. sh)
5645 GlDraw.rect
5646 (pw, float (state.winh - sbh))
5647 (pw +. sw, float state.winh)
5651 let showsel () =
5652 match state.mstate with
5653 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5656 | Msel ((x0, y0), (x1, y1)) ->
5657 let rec loop = function
5658 | l :: ls ->
5659 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5660 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5661 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5662 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5663 then
5664 match getopaque l.pageno with
5665 | Some opaque ->
5666 let x0, y0 = pagetranslatepoint l x0 y0 in
5667 let x1, y1 = pagetranslatepoint l x1 y1 in
5668 seltext opaque (x0, y0, x1, y1);
5669 | _ -> ()
5670 else loop ls
5671 | [] -> ()
5673 loop state.layout
5676 let showrects rects =
5677 Gl.enable `blend;
5678 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5679 GlDraw.polygon_mode `both `fill;
5680 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5681 List.iter
5682 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5683 List.iter (fun l ->
5684 if l.pageno = pageno
5685 then (
5686 let dx = float (l.pagedispx - l.pagex) in
5687 let dy = float (l.pagedispy - l.pagey) in
5688 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5689 GlDraw.begins `quads;
5691 GlDraw.vertex2 (x0+.dx, y0+.dy);
5692 GlDraw.vertex2 (x1+.dx, y1+.dy);
5693 GlDraw.vertex2 (x2+.dx, y2+.dy);
5694 GlDraw.vertex2 (x3+.dx, y3+.dy);
5696 GlDraw.ends ();
5698 ) state.layout
5699 ) rects
5701 Gl.disable `blend;
5704 let display () =
5705 GlClear.color (scalecolor2 conf.bgcolor);
5706 GlClear.clear [`color];
5707 let rec loop linkindexbase = function
5708 | l :: rest ->
5709 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5710 loop linkindexbase rest
5711 | [] -> ()
5713 loop 0 state.layout;
5714 let rects =
5715 match state.mode with
5716 | LinkNav (Ltexact (pageno, linkno)) ->
5717 begin match getopaque pageno with
5718 | Some opaque ->
5719 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5720 (pageno, 5, (
5721 float x0, float y0,
5722 float x1, float y0,
5723 float x1, float y1,
5724 float x0, float y1)
5725 ) :: state.rects
5726 | None -> state.rects
5728 | _ -> state.rects
5730 showrects rects;
5731 showsel ();
5732 state.uioh#display;
5733 begin match state.mstate with
5734 | Mzoomrect ((x0, y0), (x1, y1)) ->
5735 Gl.enable `blend;
5736 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5737 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5738 GlDraw.rect (float x0, float y0)
5739 (float x1, float y1);
5740 Gl.disable `blend;
5741 | _ -> ()
5742 end;
5743 enttext ();
5744 scrollindicator ();
5745 Wsi.swapb ();
5748 let zoomrect x y x1 y1 =
5749 let x0 = min x x1
5750 and x1 = max x x1
5751 and y0 = min y y1 in
5752 gotoy (state.y + y0);
5753 state.anchor <- getanchor ();
5754 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5755 let margin =
5756 if state.w < state.winw - state.scrollw
5757 then (state.winw - state.scrollw - state.w) / 2
5758 else 0
5760 state.x <- (state.x + margin) - x0;
5761 setzoom zoom;
5762 Wsi.setcursor Wsi.CURSOR_INHERIT;
5763 state.mstate <- Mnone;
5766 let scrollx x =
5767 let winw = state.winw - state.scrollw - 1 in
5768 let s = float x /. float winw in
5769 let destx = truncate (float (state.w + winw) *. s) in
5770 state.x <- winw - destx;
5771 gotoy_and_clear_text state.y;
5772 state.mstate <- Mscrollx;
5775 let scrolly y =
5776 let s = float y /. float state.winh in
5777 let desty = truncate (float (state.maxy - state.winh) *. s) in
5778 gotoy_and_clear_text desty;
5779 state.mstate <- Mscrolly;
5782 let viewmouse button down x y mask =
5783 match button with
5784 | n when (n == 4 || n == 5) && not down ->
5785 if Wsi.withctrl mask
5786 then (
5787 match state.mstate with
5788 | Mzoom (oldn, i) ->
5789 if oldn = n
5790 then (
5791 if i = 2
5792 then
5793 let incr =
5794 match n with
5795 | 5 ->
5796 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5797 | _ ->
5798 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5800 let zoom = conf.zoom -. incr in
5801 setzoom zoom;
5802 state.mstate <- Mzoom (n, 0);
5803 else
5804 state.mstate <- Mzoom (n, i+1);
5806 else state.mstate <- Mzoom (n, 0)
5808 | _ -> state.mstate <- Mzoom (n, 0)
5810 else (
5811 match state.autoscroll with
5812 | Some step -> setautoscrollspeed step (n=4)
5813 | None ->
5814 if conf.wheelbypage || conf.presentation
5815 then (
5816 if n = 4
5817 then prevpage ()
5818 else nextpage ()
5820 else
5821 let incr =
5822 if n = 4
5823 then -conf.scrollstep
5824 else conf.scrollstep
5826 let incr = incr * 2 in
5827 let y = clamp incr in
5828 gotoy_and_clear_text y
5831 | n when (n = 6 || n = 7) && not down && canpan () ->
5832 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5833 gotoy_and_clear_text state.y
5835 | 1 when Wsi.withshift mask ->
5836 state.mstate <- Mnone;
5837 if not down
5838 then (
5839 match unproject x y with
5840 | Some (pageno, ux, uy) ->
5841 let cmd = Printf.sprintf
5842 "%s %s %d %d %d"
5843 conf.stcmd state.path pageno ux uy
5845 popen cmd []
5846 | None -> ()
5849 | 1 when Wsi.withctrl mask ->
5850 if down
5851 then (
5852 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5853 state.mstate <- Mpan (x, y)
5855 else
5856 state.mstate <- Mnone
5858 | 3 ->
5859 if down
5860 then (
5861 Wsi.setcursor Wsi.CURSOR_CYCLE;
5862 let p = (x, y) in
5863 state.mstate <- Mzoomrect (p, p)
5865 else (
5866 match state.mstate with
5867 | Mzoomrect ((x0, y0), _) ->
5868 if abs (x-x0) > 10 && abs (y - y0) > 10
5869 then zoomrect x0 y0 x y
5870 else (
5871 state.mstate <- Mnone;
5872 Wsi.setcursor Wsi.CURSOR_INHERIT;
5873 G.postRedisplay "kill accidental zoom rect";
5875 | _ ->
5876 Wsi.setcursor Wsi.CURSOR_INHERIT;
5877 state.mstate <- Mnone
5880 | 1 when x > state.winw - state.scrollw ->
5881 if down
5882 then
5883 let _, position, sh = state.uioh#scrollph in
5884 if y > truncate position && y < truncate (position +. sh)
5885 then state.mstate <- Mscrolly
5886 else scrolly y
5887 else
5888 state.mstate <- Mnone
5890 | 1 when y > state.winh - state.hscrollh ->
5891 if down
5892 then
5893 let _, position, sw = state.uioh#scrollpw in
5894 if x > truncate position && x < truncate (position +. sw)
5895 then state.mstate <- Mscrollx
5896 else scrollx x
5897 else
5898 state.mstate <- Mnone
5900 | 1 ->
5901 let dest = if down then getunder x y else Unone in
5902 begin match dest with
5903 | Ulinkgoto _
5904 | Ulinkuri _
5905 | Uremote _
5906 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5907 gotounder dest
5909 | Unone when down ->
5910 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5911 state.mstate <- Mpan (x, y);
5913 | Unone | Utext _ ->
5914 if down
5915 then (
5916 if conf.angle mod 360 = 0
5917 then (
5918 state.mstate <- Msel ((x, y), (x, y));
5919 G.postRedisplay "mouse select";
5922 else (
5923 match state.mstate with
5924 | Mnone -> ()
5926 | Mzoom _ | Mscrollx | Mscrolly ->
5927 state.mstate <- Mnone
5929 | Mzoomrect ((x0, y0), _) ->
5930 zoomrect x0 y0 x y
5932 | Mpan _ ->
5933 Wsi.setcursor Wsi.CURSOR_INHERIT;
5934 state.mstate <- Mnone
5936 | Msel ((x0, y0), (x1, y1)) ->
5937 let rec loop = function
5938 | [] -> ()
5939 | l :: rest ->
5940 let inside =
5941 let a0 = l.pagedispy in
5942 let a1 = a0 + l.pagevh in
5943 let b0 = l.pagedispx in
5944 let b1 = b0 + l.pagevw in
5945 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5946 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5948 if inside
5949 then
5950 match getopaque l.pageno with
5951 | Some opaque ->
5952 begin
5953 match Ne.pipe () with
5954 | Ne.Exn exn ->
5955 showtext '!'
5956 (Printf.sprintf
5957 "can not create sel pipe: %s"
5958 (exntos exn));
5959 | Ne.Res (r, w) ->
5960 let doclose what fd =
5961 Ne.clo fd (fun msg ->
5962 dolog "%s close failed: %s" what msg)
5965 popen conf.selcmd [r, 0; w, -1];
5966 copysel w opaque;
5967 doclose "pipe/r" r;
5968 G.postRedisplay "copysel";
5969 with exn ->
5970 dolog "can not execute %S: %s"
5971 conf.selcmd (exntos exn);
5972 doclose "pipe/r" r;
5973 doclose "pipe/w" w;
5975 | None -> ()
5976 else loop rest
5978 loop state.layout;
5979 Wsi.setcursor Wsi.CURSOR_INHERIT;
5980 state.mstate <- Mnone;
5984 | _ -> ()
5987 let birdseyemouse button down x y mask
5988 (conf, leftx, _, hooverpageno, anchor) =
5989 match button with
5990 | 1 when down ->
5991 let rec loop = function
5992 | [] -> ()
5993 | l :: rest ->
5994 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5995 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5996 then (
5997 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5999 else loop rest
6001 loop state.layout
6002 | 3 -> ()
6003 | _ -> viewmouse button down x y mask
6006 let mouse button down x y mask =
6007 state.uioh <- state.uioh#button button down x y mask;
6010 let motion ~x ~y =
6011 state.uioh <- state.uioh#motion x y
6014 let pmotion ~x ~y =
6015 state.uioh <- state.uioh#pmotion x y;
6018 let uioh = object
6019 method display = ()
6021 method key key mask =
6022 begin match state.mode with
6023 | Textentry textentry -> textentrykeyboard key mask textentry
6024 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6025 | View -> viewkeyboard key mask
6026 | LinkNav linknav -> linknavkeyboard key mask linknav
6027 end;
6028 state.uioh
6030 method button button bstate x y mask =
6031 begin match state.mode with
6032 | LinkNav _
6033 | View -> viewmouse button bstate x y mask
6034 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6035 | Textentry _ -> ()
6036 end;
6037 state.uioh
6039 method motion x y =
6040 begin match state.mode with
6041 | Textentry _ -> ()
6042 | View | Birdseye _ | LinkNav _ ->
6043 match state.mstate with
6044 | Mzoom _ | Mnone -> ()
6046 | Mpan (x0, y0) ->
6047 let dx = x - x0
6048 and dy = y0 - y in
6049 state.mstate <- Mpan (x, y);
6050 if canpan ()
6051 then state.x <- state.x + dx;
6052 let y = clamp dy in
6053 gotoy_and_clear_text y
6055 | Msel (a, _) ->
6056 state.mstate <- Msel (a, (x, y));
6057 G.postRedisplay "motion select";
6059 | Mscrolly ->
6060 let y = min state.winh (max 0 y) in
6061 scrolly y
6063 | Mscrollx ->
6064 let x = min state.winw (max 0 x) in
6065 scrollx x
6067 | Mzoomrect (p0, _) ->
6068 state.mstate <- Mzoomrect (p0, (x, y));
6069 G.postRedisplay "motion zoomrect";
6070 end;
6071 state.uioh
6073 method pmotion x y =
6074 begin match state.mode with
6075 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6076 let rec loop = function
6077 | [] ->
6078 if hooverpageno != -1
6079 then (
6080 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6081 G.postRedisplay "pmotion birdseye no hoover";
6083 | l :: rest ->
6084 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6085 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6086 then (
6087 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6088 G.postRedisplay "pmotion birdseye hoover";
6090 else loop rest
6092 loop state.layout
6094 | Textentry _ -> ()
6096 | LinkNav _
6097 | View ->
6098 match state.mstate with
6099 | Mnone -> updateunder x y
6100 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6102 end;
6103 state.uioh
6105 method infochanged _ = ()
6107 method scrollph =
6108 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6109 let p, h = scrollph state.y maxy in
6110 state.scrollw, p, h
6112 method scrollpw =
6113 let winw = state.winw - state.scrollw - 1 in
6114 let fwinw = float winw in
6115 let sw =
6116 let sw = fwinw /. float state.w in
6117 let sw = fwinw *. sw in
6118 max sw (float conf.scrollh)
6120 let position, sw =
6121 let f = state.w+winw in
6122 let r = float (winw-state.x) /. float f in
6123 let p = fwinw *. r in
6124 p-.sw/.2., sw
6126 let sw =
6127 if position +. sw > fwinw
6128 then fwinw -. position
6129 else sw
6131 state.hscrollh, position, sw
6133 method modehash =
6134 let modename =
6135 match state.mode with
6136 | LinkNav _ -> "links"
6137 | Textentry _ -> "textentry"
6138 | Birdseye _ -> "birdseye"
6139 | View -> "view"
6141 findkeyhash conf modename
6143 method eformsgs = true
6144 end;;
6146 module Config =
6147 struct
6148 open Parser
6150 let fontpath = ref "";;
6152 module KeyMap =
6153 Map.Make (struct type t = (int * int) let compare = compare end);;
6155 let unent s =
6156 let l = String.length s in
6157 let b = Buffer.create l in
6158 unent b s 0 l;
6159 Buffer.contents b;
6162 let home =
6163 try Sys.getenv "HOME"
6164 with exn ->
6165 prerr_endline
6166 ("Can not determine home directory location: " ^ exntos exn);
6170 let modifier_of_string = function
6171 | "alt" -> Wsi.altmask
6172 | "shift" -> Wsi.shiftmask
6173 | "ctrl" | "control" -> Wsi.ctrlmask
6174 | "meta" -> Wsi.metamask
6175 | _ -> 0
6178 let key_of_string =
6179 let r = Str.regexp "-" in
6180 fun s ->
6181 let elems = Str.full_split r s in
6182 let f n k m =
6183 let g s =
6184 let m1 = modifier_of_string s in
6185 if m1 = 0
6186 then (Wsi.namekey s, m)
6187 else (k, m lor m1)
6188 in function
6189 | Str.Delim s when n land 1 = 0 -> g s
6190 | Str.Text s -> g s
6191 | Str.Delim _ -> (k, m)
6193 let rec loop n k m = function
6194 | [] -> (k, m)
6195 | x :: xs ->
6196 let k, m = f n k m x in
6197 loop (n+1) k m xs
6199 loop 0 0 0 elems
6202 let keys_of_string =
6203 let r = Str.regexp "[ \t]" in
6204 fun s ->
6205 let elems = Str.split r s in
6206 List.map key_of_string elems
6209 let copykeyhashes c =
6210 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6213 let config_of c attrs =
6214 let apply c k v =
6216 match k with
6217 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6218 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6219 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6220 | "preload" -> { c with preload = bool_of_string v }
6221 | "page-bias" -> { c with pagebias = int_of_string v }
6222 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6223 | "horizontal-scroll-step" ->
6224 { c with hscrollstep = max (int_of_string v) 1 }
6225 | "auto-scroll-step" ->
6226 { c with autoscrollstep = max 0 (int_of_string v) }
6227 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6228 | "crop-hack" -> { c with crophack = bool_of_string v }
6229 | "throttle" ->
6230 let mw =
6231 match String.lowercase v with
6232 | "true" -> Some infinity
6233 | "false" -> None
6234 | f -> Some (float_of_string f)
6236 { c with maxwait = mw}
6237 | "highlight-links" -> { c with hlinks = bool_of_string v }
6238 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6239 | "vertical-margin" ->
6240 { c with interpagespace = max 0 (int_of_string v) }
6241 | "zoom" ->
6242 let zoom = float_of_string v /. 100. in
6243 let zoom = max zoom 0.0 in
6244 { c with zoom = zoom }
6245 | "presentation" -> { c with presentation = bool_of_string v }
6246 | "rotation-angle" -> { c with angle = int_of_string v }
6247 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6248 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6249 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6250 | "proportional-display" -> { c with proportional = bool_of_string v }
6251 | "pixmap-cache-size" ->
6252 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6253 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6254 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6255 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6256 | "persistent-location" -> { c with jumpback = bool_of_string v }
6257 | "background-color" -> { c with bgcolor = color_of_string v }
6258 | "scrollbar-in-presentation" ->
6259 { c with scrollbarinpm = bool_of_string v }
6260 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6261 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6262 | "mupdf-store-size" ->
6263 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6264 | "checkers" -> { c with checkers = bool_of_string v }
6265 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6266 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6267 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6268 | "uri-launcher" -> { c with urilauncher = unent v }
6269 | "path-launcher" -> { c with pathlauncher = unent v }
6270 | "color-space" -> { c with colorspace = colorspace_of_string v }
6271 | "invert-colors" -> { c with invert = bool_of_string v }
6272 | "brightness" -> { c with colorscale = float_of_string v }
6273 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6274 | "ghyllscroll" ->
6275 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6276 | "columns" ->
6277 let (n, _, _) as nab = multicolumns_of_string v in
6278 if n < 0
6279 then { c with columns = Csplit (-n, [||]) }
6280 else { c with columns = Cmulti (nab, [||]) }
6281 | "birds-eye-columns" ->
6282 { c with beyecolumns = Some (max (int_of_string v) 2) }
6283 | "selection-command" -> { c with selcmd = unent v }
6284 | "synctex-command" -> { c with stcmd = unent v }
6285 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6286 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6287 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6288 | "use-pbo" -> { c with usepbo = bool_of_string v }
6289 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6290 | _ -> c
6291 with exn ->
6292 prerr_endline ("Error processing attribute (`" ^
6293 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6296 let rec fold c = function
6297 | [] -> c
6298 | (k, v) :: rest ->
6299 let c = apply c k v in
6300 fold c rest
6302 fold { c with keyhashes = copykeyhashes c } attrs;
6305 let fromstring f pos n v d =
6306 try f v
6307 with exn ->
6308 dolog "Error processing attribute (%S=%S) at %d\n%s"
6309 n v pos (exntos exn)
6314 let bookmark_of attrs =
6315 let rec fold title page rely visy = function
6316 | ("title", v) :: rest -> fold v page rely visy rest
6317 | ("page", v) :: rest -> fold title v rely visy rest
6318 | ("rely", v) :: rest -> fold title page v visy rest
6319 | ("visy", v) :: rest -> fold title page rely v rest
6320 | _ :: rest -> fold title page rely visy rest
6321 | [] -> title, page, rely, visy
6323 fold "invalid" "0" "0" "0" attrs
6326 let doc_of attrs =
6327 let rec fold path page rely pan visy = function
6328 | ("path", v) :: rest -> fold v page rely pan visy rest
6329 | ("page", v) :: rest -> fold path v rely pan visy rest
6330 | ("rely", v) :: rest -> fold path page v pan visy rest
6331 | ("pan", v) :: rest -> fold path page rely v visy rest
6332 | ("visy", v) :: rest -> fold path page rely pan v rest
6333 | _ :: rest -> fold path page rely pan visy rest
6334 | [] -> path, page, rely, pan, visy
6336 fold "" "0" "0" "0" "0" attrs
6339 let map_of attrs =
6340 let rec fold rs ls = function
6341 | ("out", v) :: rest -> fold v ls rest
6342 | ("in", v) :: rest -> fold rs v rest
6343 | _ :: rest -> fold ls rs rest
6344 | [] -> ls, rs
6346 fold "" "" attrs
6349 let setconf dst src =
6350 dst.scrollbw <- src.scrollbw;
6351 dst.scrollh <- src.scrollh;
6352 dst.icase <- src.icase;
6353 dst.preload <- src.preload;
6354 dst.pagebias <- src.pagebias;
6355 dst.verbose <- src.verbose;
6356 dst.scrollstep <- src.scrollstep;
6357 dst.maxhfit <- src.maxhfit;
6358 dst.crophack <- src.crophack;
6359 dst.autoscrollstep <- src.autoscrollstep;
6360 dst.maxwait <- src.maxwait;
6361 dst.hlinks <- src.hlinks;
6362 dst.underinfo <- src.underinfo;
6363 dst.interpagespace <- src.interpagespace;
6364 dst.zoom <- src.zoom;
6365 dst.presentation <- src.presentation;
6366 dst.angle <- src.angle;
6367 dst.cwinw <- src.cwinw;
6368 dst.cwinh <- src.cwinh;
6369 dst.savebmarks <- src.savebmarks;
6370 dst.memlimit <- src.memlimit;
6371 dst.proportional <- src.proportional;
6372 dst.texcount <- src.texcount;
6373 dst.sliceheight <- src.sliceheight;
6374 dst.thumbw <- src.thumbw;
6375 dst.jumpback <- src.jumpback;
6376 dst.bgcolor <- src.bgcolor;
6377 dst.scrollbarinpm <- src.scrollbarinpm;
6378 dst.tilew <- src.tilew;
6379 dst.tileh <- src.tileh;
6380 dst.mustoresize <- src.mustoresize;
6381 dst.checkers <- src.checkers;
6382 dst.aalevel <- src.aalevel;
6383 dst.trimmargins <- src.trimmargins;
6384 dst.trimfuzz <- src.trimfuzz;
6385 dst.urilauncher <- src.urilauncher;
6386 dst.colorspace <- src.colorspace;
6387 dst.invert <- src.invert;
6388 dst.colorscale <- src.colorscale;
6389 dst.redirectstderr <- src.redirectstderr;
6390 dst.ghyllscroll <- src.ghyllscroll;
6391 dst.columns <- src.columns;
6392 dst.beyecolumns <- src.beyecolumns;
6393 dst.selcmd <- src.selcmd;
6394 dst.updatecurs <- src.updatecurs;
6395 dst.pathlauncher <- src.pathlauncher;
6396 dst.keyhashes <- copykeyhashes src;
6397 dst.hfsize <- src.hfsize;
6398 dst.hscrollstep <- src.hscrollstep;
6399 dst.pgscale <- src.pgscale;
6400 dst.usepbo <- src.usepbo;
6401 dst.wheelbypage <- src.wheelbypage;
6402 dst.stcmd <- src.stcmd;
6405 let get s =
6406 let h = Hashtbl.create 10 in
6407 let dc = { defconf with angle = defconf.angle } in
6408 let rec toplevel v t spos _ =
6409 match t with
6410 | Vdata | Vcdata | Vend -> v
6411 | Vopen ("llppconfig", _, closed) ->
6412 if closed
6413 then v
6414 else { v with f = llppconfig }
6415 | Vopen _ ->
6416 error "unexpected subelement at top level" s spos
6417 | Vclose _ -> error "unexpected close at top level" s spos
6419 and llppconfig v t spos _ =
6420 match t with
6421 | Vdata | Vcdata -> v
6422 | Vend -> error "unexpected end of input in llppconfig" s spos
6423 | Vopen ("defaults", attrs, closed) ->
6424 let c = config_of dc attrs in
6425 setconf dc c;
6426 if closed
6427 then v
6428 else { v with f = defaults }
6430 | Vopen ("ui-font", attrs, closed) ->
6431 let rec getsize size = function
6432 | [] -> size
6433 | ("size", v) :: rest ->
6434 let size =
6435 fromstring int_of_string spos "size" v fstate.fontsize in
6436 getsize size rest
6437 | l -> getsize size l
6439 fstate.fontsize <- getsize fstate.fontsize attrs;
6440 if closed
6441 then v
6442 else { v with f = uifont (Buffer.create 10) }
6444 | Vopen ("doc", attrs, closed) ->
6445 let pathent, spage, srely, span, svisy = doc_of attrs in
6446 let path = unent pathent
6447 and pageno = fromstring int_of_string spos "page" spage 0
6448 and rely = fromstring float_of_string spos "rely" srely 0.0
6449 and pan = fromstring int_of_string spos "pan" span 0
6450 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6451 let c = config_of dc attrs in
6452 let anchor = (pageno, rely, visy) in
6453 if closed
6454 then (Hashtbl.add h path (c, [], pan, anchor); v)
6455 else { v with f = doc path pan anchor c [] }
6457 | Vopen _ ->
6458 error "unexpected subelement in llppconfig" s spos
6460 | Vclose "llppconfig" -> { v with f = toplevel }
6461 | Vclose _ -> error "unexpected close in llppconfig" s spos
6463 and defaults v t spos _ =
6464 match t with
6465 | Vdata | Vcdata -> v
6466 | Vend -> error "unexpected end of input in defaults" s spos
6467 | Vopen ("keymap", attrs, closed) ->
6468 let modename =
6469 try List.assoc "mode" attrs
6470 with Not_found -> "global" in
6471 if closed
6472 then v
6473 else
6474 let ret keymap =
6475 let h = findkeyhash dc modename in
6476 KeyMap.iter (Hashtbl.replace h) keymap;
6477 defaults
6479 { v with f = pkeymap ret KeyMap.empty }
6481 | Vopen (_, _, _) ->
6482 error "unexpected subelement in defaults" s spos
6484 | Vclose "defaults" ->
6485 { v with f = llppconfig }
6487 | Vclose _ -> error "unexpected close in defaults" s spos
6489 and uifont b v t spos epos =
6490 match t with
6491 | Vdata | Vcdata ->
6492 Buffer.add_substring b s spos (epos - spos);
6494 | Vopen (_, _, _) ->
6495 error "unexpected subelement in ui-font" s spos
6496 | Vclose "ui-font" ->
6497 if String.length !fontpath = 0
6498 then fontpath := Buffer.contents b;
6499 { v with f = llppconfig }
6500 | Vclose _ -> error "unexpected close in ui-font" s spos
6501 | Vend -> error "unexpected end of input in ui-font" s spos
6503 and doc path pan anchor c bookmarks v t spos _ =
6504 match t with
6505 | Vdata | Vcdata -> v
6506 | Vend -> error "unexpected end of input in doc" s spos
6507 | Vopen ("bookmarks", _, closed) ->
6508 if closed
6509 then v
6510 else { v with f = pbookmarks path pan anchor c bookmarks }
6512 | Vopen ("keymap", attrs, closed) ->
6513 let modename =
6514 try List.assoc "mode" attrs
6515 with Not_found -> "global"
6517 if closed
6518 then v
6519 else
6520 let ret keymap =
6521 let h = findkeyhash c modename in
6522 KeyMap.iter (Hashtbl.replace h) keymap;
6523 doc path pan anchor c bookmarks
6525 { v with f = pkeymap ret KeyMap.empty }
6527 | Vopen (_, _, _) ->
6528 error "unexpected subelement in doc" s spos
6530 | Vclose "doc" ->
6531 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6532 { v with f = llppconfig }
6534 | Vclose _ -> error "unexpected close in doc" s spos
6536 and pkeymap ret keymap v t spos _ =
6537 match t with
6538 | Vdata | Vcdata -> v
6539 | Vend -> error "unexpected end of input in keymap" s spos
6540 | Vopen ("map", attrs, closed) ->
6541 let r, l = map_of attrs in
6542 let kss = fromstring keys_of_string spos "in" r [] in
6543 let lss = fromstring keys_of_string spos "out" l [] in
6544 let keymap =
6545 match kss with
6546 | [] -> keymap
6547 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6548 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6550 if closed
6551 then { v with f = pkeymap ret keymap }
6552 else
6553 let f () = v in
6554 { v with f = skip "map" f }
6556 | Vopen _ ->
6557 error "unexpected subelement in keymap" s spos
6559 | Vclose "keymap" ->
6560 { v with f = ret keymap }
6562 | Vclose _ -> error "unexpected close in keymap" s spos
6564 and pbookmarks path pan anchor c bookmarks v t spos _ =
6565 match t with
6566 | Vdata | Vcdata -> v
6567 | Vend -> error "unexpected end of input in bookmarks" s spos
6568 | Vopen ("item", attrs, closed) ->
6569 let titleent, spage, srely, svisy = bookmark_of attrs in
6570 let page = fromstring int_of_string spos "page" spage 0
6571 and rely = fromstring float_of_string spos "rely" srely 0.0
6572 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6573 let bookmarks =
6574 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6576 if closed
6577 then { v with f = pbookmarks path pan anchor c bookmarks }
6578 else
6579 let f () = v in
6580 { v with f = skip "item" f }
6582 | Vopen _ ->
6583 error "unexpected subelement in bookmarks" s spos
6585 | Vclose "bookmarks" ->
6586 { v with f = doc path pan anchor c bookmarks }
6588 | Vclose _ -> error "unexpected close in bookmarks" s spos
6590 and skip tag f v t spos _ =
6591 match t with
6592 | Vdata | Vcdata -> v
6593 | Vend ->
6594 error ("unexpected end of input in skipped " ^ tag) s spos
6595 | Vopen (tag', _, closed) ->
6596 if closed
6597 then v
6598 else
6599 let f' () = { v with f = skip tag f } in
6600 { v with f = skip tag' f' }
6601 | Vclose ctag ->
6602 if tag = ctag
6603 then f ()
6604 else error ("unexpected close in skipped " ^ tag) s spos
6607 parse { f = toplevel; accu = () } s;
6608 h, dc;
6611 let do_load f ic =
6613 let len = in_channel_length ic in
6614 let s = String.create len in
6615 really_input ic s 0 len;
6616 f s;
6617 with
6618 | Parse_error (msg, s, pos) ->
6619 let subs = subs s pos in
6620 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6621 failwith ("parse error: " ^ s)
6623 | exn ->
6624 failwith ("config load error: " ^ exntos exn)
6627 let defconfpath =
6628 let dir =
6630 let dir = Filename.concat home ".config" in
6631 if Sys.is_directory dir then dir else home
6632 with _ -> home
6634 Filename.concat dir "llpp.conf"
6637 let confpath = ref defconfpath;;
6639 let load1 f =
6640 if Sys.file_exists !confpath
6641 then
6642 match
6643 (try Some (open_in_bin !confpath)
6644 with exn ->
6645 prerr_endline
6646 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6647 exntos exn);
6648 None
6650 with
6651 | Some ic ->
6652 let success =
6654 f (do_load get ic)
6655 with exn ->
6656 prerr_endline
6657 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6658 exntos exn);
6659 false
6661 close_in ic;
6662 success
6664 | None -> false
6665 else
6666 f (Hashtbl.create 0, defconf)
6669 let load () =
6670 let f (h, dc) =
6671 let pc, pb, px, pa =
6673 Hashtbl.find h (Filename.basename state.path)
6674 with Not_found -> dc, [], 0, emptyanchor
6676 setconf defconf dc;
6677 setconf conf pc;
6678 state.bookmarks <- pb;
6679 state.x <- px;
6680 state.scrollw <- conf.scrollbw;
6681 if conf.jumpback
6682 then state.anchor <- pa;
6683 cbput state.hists.nav pa;
6684 true
6686 load1 f
6689 let add_attrs bb always dc c =
6690 let ob s a b =
6691 if always || a != b
6692 then Printf.bprintf bb "\n %s='%b'" s a
6693 and oi s a b =
6694 if always || a != b
6695 then Printf.bprintf bb "\n %s='%d'" s a
6696 and oI s a b =
6697 if always || a != b
6698 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6699 and oz s a b =
6700 if always || a <> b
6701 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6702 and oF s a b =
6703 if always || a <> b
6704 then Printf.bprintf bb "\n %s='%f'" s a
6705 and oc s a b =
6706 if always || a <> b
6707 then
6708 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6709 and oC s a b =
6710 if always || a <> b
6711 then
6712 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6713 and oR s a b =
6714 if always || a <> b
6715 then
6716 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6717 and os s a b =
6718 if always || a <> b
6719 then
6720 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6721 and og s a b =
6722 if always || a <> b
6723 then
6724 match a with
6725 | None -> ()
6726 | Some (_N, _A, _B) ->
6727 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6728 and oW s a b =
6729 if always || a <> b
6730 then
6731 let v =
6732 match a with
6733 | None -> "false"
6734 | Some f ->
6735 if f = infinity
6736 then "true"
6737 else string_of_float f
6739 Printf.bprintf bb "\n %s='%s'" s v
6740 and oco s a b =
6741 if always || a <> b
6742 then
6743 match a with
6744 | Cmulti ((n, a, b), _) when n > 1 ->
6745 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6746 | Csplit (n, _) when n > 1 ->
6747 Printf.bprintf bb "\n %s='%d'" s ~-n
6748 | _ -> ()
6749 and obeco s a b =
6750 if always || a <> b
6751 then
6752 match a with
6753 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6754 | _ -> ()
6756 oi "width" c.cwinw dc.cwinw;
6757 oi "height" c.cwinh dc.cwinh;
6758 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6759 oi "scroll-handle-height" c.scrollh dc.scrollh;
6760 ob "case-insensitive-search" c.icase dc.icase;
6761 ob "preload" c.preload dc.preload;
6762 oi "page-bias" c.pagebias dc.pagebias;
6763 oi "scroll-step" c.scrollstep dc.scrollstep;
6764 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6765 ob "max-height-fit" c.maxhfit dc.maxhfit;
6766 ob "crop-hack" c.crophack dc.crophack;
6767 oW "throttle" c.maxwait dc.maxwait;
6768 ob "highlight-links" c.hlinks dc.hlinks;
6769 ob "under-cursor-info" c.underinfo dc.underinfo;
6770 oi "vertical-margin" c.interpagespace dc.interpagespace;
6771 oz "zoom" c.zoom dc.zoom;
6772 ob "presentation" c.presentation dc.presentation;
6773 oi "rotation-angle" c.angle dc.angle;
6774 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6775 ob "proportional-display" c.proportional dc.proportional;
6776 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6777 oi "tex-count" c.texcount dc.texcount;
6778 oi "slice-height" c.sliceheight dc.sliceheight;
6779 oi "thumbnail-width" c.thumbw dc.thumbw;
6780 ob "persistent-location" c.jumpback dc.jumpback;
6781 oc "background-color" c.bgcolor dc.bgcolor;
6782 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6783 oi "tile-width" c.tilew dc.tilew;
6784 oi "tile-height" c.tileh dc.tileh;
6785 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6786 ob "checkers" c.checkers dc.checkers;
6787 oi "aalevel" c.aalevel dc.aalevel;
6788 ob "trim-margins" c.trimmargins dc.trimmargins;
6789 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6790 os "uri-launcher" c.urilauncher dc.urilauncher;
6791 os "path-launcher" c.pathlauncher dc.pathlauncher;
6792 oC "color-space" c.colorspace dc.colorspace;
6793 ob "invert-colors" c.invert dc.invert;
6794 oF "brightness" c.colorscale dc.colorscale;
6795 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6796 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6797 oco "columns" c.columns dc.columns;
6798 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6799 os "selection-command" c.selcmd dc.selcmd;
6800 os "synctex-command" c.stcmd dc.stcmd;
6801 ob "update-cursor" c.updatecurs dc.updatecurs;
6802 oi "hint-font-size" c.hfsize dc.hfsize;
6803 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6804 oF "page-scroll-scale" c.pgscale dc.pgscale;
6805 ob "use-pbo" c.usepbo dc.usepbo;
6806 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6809 let keymapsbuf always dc c =
6810 let bb = Buffer.create 16 in
6811 let rec loop = function
6812 | [] -> ()
6813 | (modename, h) :: rest ->
6814 let dh = findkeyhash dc modename in
6815 if always || h <> dh
6816 then (
6817 if Hashtbl.length h > 0
6818 then (
6819 if Buffer.length bb > 0
6820 then Buffer.add_char bb '\n';
6821 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6822 Hashtbl.iter (fun i o ->
6823 let isdifferent = always ||
6825 let dO = Hashtbl.find dh i in
6826 dO <> o
6827 with Not_found -> true
6829 if isdifferent
6830 then
6831 let addkm (k, m) =
6832 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6833 if Wsi.withalt m then Buffer.add_string bb "alt-";
6834 if Wsi.withshift m then Buffer.add_string bb "shift-";
6835 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6836 Buffer.add_string bb (Wsi.keyname k);
6838 let addkms l =
6839 let rec loop = function
6840 | [] -> ()
6841 | km :: [] -> addkm km
6842 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6844 loop l
6846 Buffer.add_string bb "<map in='";
6847 addkm i;
6848 match o with
6849 | KMinsrt km ->
6850 Buffer.add_string bb "' out='";
6851 addkm km;
6852 Buffer.add_string bb "'/>\n"
6854 | KMinsrl kms ->
6855 Buffer.add_string bb "' out='";
6856 addkms kms;
6857 Buffer.add_string bb "'/>\n"
6859 | KMmulti (ins, kms) ->
6860 Buffer.add_char bb ' ';
6861 addkms ins;
6862 Buffer.add_string bb "' out='";
6863 addkms kms;
6864 Buffer.add_string bb "'/>\n"
6865 ) h;
6866 Buffer.add_string bb "</keymap>";
6869 loop rest
6871 loop c.keyhashes;
6875 let save () =
6876 let uifontsize = fstate.fontsize in
6877 let bb = Buffer.create 32768 in
6878 let w, h =
6879 List.fold_left
6880 (fun (w, h) ws ->
6881 match ws with
6882 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6883 | Wsi.MaxVert -> (w, conf.cwinh)
6884 | Wsi.MaxHorz -> (conf.cwinw, h)
6886 (state.winw, state.winh) state.winstate
6888 conf.cwinw <- w;
6889 conf.cwinh <- h;
6890 let f (h, dc) =
6891 let dc = if conf.bedefault then conf else dc in
6892 Buffer.add_string bb "<llppconfig>\n";
6894 if String.length !fontpath > 0
6895 then
6896 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6897 uifontsize
6898 !fontpath
6899 else (
6900 if uifontsize <> 14
6901 then
6902 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6905 Buffer.add_string bb "<defaults ";
6906 add_attrs bb true dc dc;
6907 let kb = keymapsbuf true dc dc in
6908 if Buffer.length kb > 0
6909 then (
6910 Buffer.add_string bb ">\n";
6911 Buffer.add_buffer bb kb;
6912 Buffer.add_string bb "\n</defaults>\n";
6914 else Buffer.add_string bb "/>\n";
6916 let adddoc path pan anchor c bookmarks =
6917 if bookmarks == [] && c = dc && anchor = emptyanchor
6918 then ()
6919 else (
6920 Printf.bprintf bb "<doc path='%s'"
6921 (enent path 0 (String.length path));
6923 if anchor <> emptyanchor
6924 then (
6925 let n, rely, visy = anchor in
6926 Printf.bprintf bb " page='%d'" n;
6927 if rely > 1e-6
6928 then
6929 Printf.bprintf bb " rely='%f'" rely
6931 if abs_float visy > 1e-6
6932 then
6933 Printf.bprintf bb " visy='%f'" visy
6937 if pan != 0
6938 then Printf.bprintf bb " pan='%d'" pan;
6940 add_attrs bb false dc c;
6941 let kb = keymapsbuf false dc c in
6943 begin match bookmarks with
6944 | [] ->
6945 if Buffer.length kb > 0
6946 then (
6947 Buffer.add_string bb ">\n";
6948 Buffer.add_buffer bb kb;
6949 Buffer.add_string bb "\n</doc>\n";
6951 else Buffer.add_string bb "/>\n"
6952 | _ ->
6953 Buffer.add_string bb ">\n<bookmarks>\n";
6954 List.iter (fun (title, _level, (page, rely, visy)) ->
6955 Printf.bprintf bb
6956 "<item title='%s' page='%d'"
6957 (enent title 0 (String.length title))
6958 page
6960 if rely > 1e-6
6961 then
6962 Printf.bprintf bb " rely='%f'" rely
6964 if abs_float visy > 1e-6
6965 then
6966 Printf.bprintf bb " visy='%f'" visy
6968 Buffer.add_string bb "/>\n";
6969 ) bookmarks;
6970 Buffer.add_string bb "</bookmarks>";
6971 if Buffer.length kb > 0
6972 then (
6973 Buffer.add_string bb "\n";
6974 Buffer.add_buffer bb kb;
6976 Buffer.add_string bb "\n</doc>\n";
6977 end;
6981 let pan, conf =
6982 match state.mode with
6983 | Birdseye (c, pan, _, _, _) ->
6984 let beyecolumns =
6985 match conf.columns with
6986 | Cmulti ((c, _, _), _) -> Some c
6987 | Csingle _ -> None
6988 | Csplit _ -> None
6989 and columns =
6990 match c.columns with
6991 | Cmulti (c, _) -> Cmulti (c, [||])
6992 | Csingle _ -> Csingle [||]
6993 | Csplit _ -> failwith "quit from bird's eye while split"
6995 pan, { c with beyecolumns = beyecolumns; columns = columns }
6996 | _ -> state.x, conf
6998 let basename = Filename.basename state.path in
6999 adddoc basename pan (getanchor ())
7000 (let conf =
7001 let autoscrollstep =
7002 match state.autoscroll with
7003 | Some step -> step
7004 | None -> conf.autoscrollstep
7006 match state.mode with
7007 | Birdseye (bc, _, _, _, _) ->
7008 { conf with
7009 zoom = bc.zoom;
7010 presentation = bc.presentation;
7011 interpagespace = bc.interpagespace;
7012 maxwait = bc.maxwait;
7013 autoscrollstep = autoscrollstep }
7014 | _ -> { conf with autoscrollstep = autoscrollstep }
7015 in conf)
7016 (if conf.savebmarks then state.bookmarks else []);
7018 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7019 if basename <> path
7020 then adddoc path x anchor c bookmarks
7021 ) h;
7022 Buffer.add_string bb "</llppconfig>\n";
7023 true;
7025 if load1 f && Buffer.length bb > 0
7026 then
7028 let tmp = !confpath ^ ".tmp" in
7029 let oc = open_out_bin tmp in
7030 Buffer.output_buffer oc bb;
7031 close_out oc;
7032 Unix.rename tmp !confpath;
7033 with exn ->
7034 prerr_endline
7035 ("error while saving configuration: " ^ exntos exn)
7037 end;;
7039 let adderrmsg src msg =
7040 Buffer.add_string state.errmsgs msg;
7041 state.newerrmsgs <- true;
7042 G.postRedisplay src
7045 let adderrfmt src fmt =
7046 Format.kprintf (fun s -> adderrmsg src s) fmt;
7049 let ract cmds =
7050 let cl = splitatspace cmds in
7051 let scan s fmt f =
7052 try Scanf.sscanf s fmt f
7053 with exn ->
7054 adderrfmt "remote exec"
7055 "error processing '%S': %s\n" cmds (exntos exn)
7057 match cl with
7058 | "reload" :: [] -> reload ()
7059 | "goto" :: args :: [] ->
7060 scan args "%u %f %f"
7061 (fun pageno x y ->
7062 let cmd, _ = state.geomcmds in
7063 if String.length cmd = 0
7064 then gotopagexy pageno x y
7065 else
7066 let f prevf () =
7067 gotopagexy pageno x y;
7068 prevf ()
7070 state.reprf <- f state.reprf
7072 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7073 | "rect" :: args :: [] ->
7074 scan args "%u %u %f %f %f %f"
7075 (fun pageno color x0 y0 x1 y1 ->
7076 onpagerect pageno (fun w h ->
7077 let _,w1,h1,_ = getpagedim pageno in
7078 let sw = float w1 /. w
7079 and sh = float h1 /. h in
7080 let x0s = x0 *. sw
7081 and x1s = x1 *. sw
7082 and y0s = y0 *. sh
7083 and y1s = y1 *. sh in
7084 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7085 debugrect rect;
7086 state.rects <- (pageno, color, rect) :: state.rects;
7087 G.postRedisplay "rect";
7090 | "activatewin" :: [] -> Wsi.activatewin ()
7091 | "quit" :: [] -> raise Quit
7092 | _ ->
7093 adderrfmt "remote command"
7094 "error processing remote command: %S\n" cmds;
7097 let remote =
7098 let scratch = String.create 80 in
7099 let buf = Buffer.create 80 in
7100 fun fd ->
7101 let rec tempfr () =
7102 try Some (Unix.read fd scratch 0 80)
7103 with
7104 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7105 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7106 | exn -> raise exn
7108 match tempfr () with
7109 | None -> Some fd
7110 | Some n ->
7111 if n = 0
7112 then (
7113 Unix.close fd;
7114 if Buffer.length buf > 0
7115 then (
7116 let s = Buffer.contents buf in
7117 Buffer.clear buf;
7118 ract s;
7120 None
7122 else
7123 let rec eat ppos =
7124 let nlpos =
7126 let pos = String.index_from scratch ppos '\n' in
7127 if pos >= n then -1 else pos
7128 with Not_found -> -1
7130 if nlpos >= 0
7131 then (
7132 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7133 let s = Buffer.contents buf in
7134 Buffer.clear buf;
7135 ract s;
7136 eat (nlpos+1);
7138 else (
7139 Buffer.add_substring buf scratch ppos (n-ppos);
7140 Some fd
7142 in eat 0
7145 let remoteopen path =
7146 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7147 with exn ->
7148 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7149 None
7152 let () =
7153 let trimcachepath = ref "" in
7154 let rcmdpath = ref "" in
7155 Arg.parse
7156 (Arg.align
7157 [("-p", Arg.String (fun s -> state.password <- s),
7158 "<password> Set password");
7160 ("-f", Arg.String (fun s -> Config.fontpath := s),
7161 "<path> Set path to the user interface font");
7163 ("-c", Arg.String (fun s -> Config.confpath := s),
7164 "<path> Set path to the configuration file");
7166 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7167 "<path> Set path to the trim cache file");
7169 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7170 "<named-destination> Set named destination");
7172 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7174 ("-remote", Arg.String (fun s -> rcmdpath := s),
7175 "<path> Set path to the remote commands source");
7177 ("-v", Arg.Unit (fun () ->
7178 Printf.printf
7179 "%s\nconfiguration path: %s\n"
7180 (version ())
7181 Config.defconfpath
7183 exit 0), " Print version and exit");
7186 (fun s -> state.path <- s)
7187 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7189 if String.length state.path = 0
7190 then (prerr_endline "file name missing"; exit 1);
7192 if not (Config.load ())
7193 then prerr_endline "failed to load configuration";
7195 let globalkeyhash = findkeyhash conf "global" in
7196 let wsfd, winw, winh = Wsi.init (object
7197 method expose =
7198 if nogeomcmds state.geomcmds || platform == Posx
7199 then display ()
7200 else (
7201 GlClear.color (scalecolor2 conf.bgcolor);
7202 GlClear.clear [`color];
7204 method display = display ()
7205 method reshape w h = reshape w h
7206 method mouse b d x y m = mouse b d x y m
7207 method motion x y = state.mpos <- (x, y); motion x y
7208 method pmotion x y = state.mpos <- (x, y); pmotion x y
7209 method key k m =
7210 let mascm = m land (
7211 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7212 ) in
7213 match state.keystate with
7214 | KSnone ->
7215 let km = k, mascm in
7216 begin
7217 match
7218 let modehash = state.uioh#modehash in
7219 try Hashtbl.find modehash km
7220 with Not_found ->
7221 try Hashtbl.find globalkeyhash km
7222 with Not_found -> KMinsrt (k, m)
7223 with
7224 | KMinsrt (k, m) -> keyboard k m
7225 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7226 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7228 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7229 List.iter (fun (k, m) -> keyboard k m) insrt;
7230 state.keystate <- KSnone
7231 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7232 state.keystate <- KSinto (keys, insrt)
7233 | _ ->
7234 state.keystate <- KSnone
7236 method enter x y = state.mpos <- (x, y); pmotion x y
7237 method leave = state.mpos <- (-1, -1)
7238 method winstate wsl = state.winstate <- wsl
7239 method quit = raise Quit
7240 end) conf.cwinw conf.cwinh (platform = Posx) in
7242 state.wsfd <- wsfd;
7244 if not (
7245 List.exists GlMisc.check_extension
7246 [ "GL_ARB_texture_rectangle"
7247 ; "GL_EXT_texture_recangle"
7248 ; "GL_NV_texture_rectangle" ]
7250 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7252 let cr, sw =
7253 match Ne.pipe () with
7254 | Ne.Exn exn ->
7255 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7256 exit 1
7257 | Ne.Res rw -> rw
7258 and sr, cw =
7259 match Ne.pipe () with
7260 | Ne.Exn exn ->
7261 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7262 exit 1
7263 | Ne.Res rw -> rw
7266 cloexec cr;
7267 cloexec sw;
7268 cloexec sr;
7269 cloexec cw;
7271 setcheckers conf.checkers;
7272 redirectstderr ();
7274 init (cr, cw) (
7275 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7276 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7277 !Config.fontpath, !trimcachepath,
7278 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7280 state.sr <- sr;
7281 state.sw <- sw;
7282 state.text <- "Opening " ^ (mbtoutf8 state.path);
7283 reshape winw winh;
7284 opendoc state.path state.password;
7285 state.uioh <- uioh;
7287 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7288 let optrfd =
7289 ref (
7290 if String.length !rcmdpath > 0
7291 then remoteopen !rcmdpath
7292 else None
7296 let rec loop deadline =
7297 let r =
7298 match state.errfd with
7299 | None -> [state.sr; state.wsfd]
7300 | Some fd -> [state.sr; state.wsfd; fd]
7302 let r =
7303 match !optrfd with
7304 | None -> r
7305 | Some fd -> fd :: r
7307 if state.redisplay
7308 then (
7309 state.redisplay <- false;
7310 display ();
7312 let timeout =
7313 let now = now () in
7314 if deadline > now
7315 then (
7316 if deadline = infinity
7317 then ~-.1.0
7318 else max 0.0 (deadline -. now)
7320 else 0.0
7322 let r, _, _ =
7323 try Unix.select r [] [] timeout
7324 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7326 begin match r with
7327 | [] ->
7328 state.ghyll None;
7329 let newdeadline =
7330 if state.ghyll == noghyll
7331 then
7332 match state.autoscroll with
7333 | Some step when step != 0 ->
7334 let y = state.y + step in
7335 let y =
7336 if y < 0
7337 then state.maxy
7338 else if y >= state.maxy then 0 else y
7340 gotoy y;
7341 if state.mode = View
7342 then state.text <- "";
7343 deadline +. 0.01
7344 | _ -> infinity
7345 else deadline +. 0.01
7347 loop newdeadline
7349 | l ->
7350 let rec checkfds = function
7351 | [] -> ()
7352 | fd :: rest when fd = state.sr ->
7353 let cmd = readcmd state.sr in
7354 act cmd;
7355 checkfds rest
7357 | fd :: rest when fd = state.wsfd ->
7358 Wsi.readresp fd;
7359 checkfds rest
7361 | fd :: rest when Some fd = !optrfd ->
7362 begin match remote fd with
7363 | None -> optrfd := remoteopen !rcmdpath;
7364 | opt -> optrfd := opt
7365 end;
7366 checkfds rest
7368 | fd :: rest ->
7369 let s = String.create 80 in
7370 let n = tempfailureretry (Unix.read fd s 0) 80 in
7371 if conf.redirectstderr
7372 then (
7373 Buffer.add_substring state.errmsgs s 0 n;
7374 state.newerrmsgs <- true;
7375 state.redisplay <- true;
7377 else (
7378 prerr_string (String.sub s 0 n);
7379 flush stderr;
7381 checkfds rest
7383 checkfds l;
7384 let newdeadline =
7385 let deadline1 =
7386 if deadline = infinity
7387 then now () +. 0.01
7388 else deadline
7390 match state.autoscroll with
7391 | Some step when step != 0 -> deadline1
7392 | _ -> if state.ghyll == noghyll then infinity else deadline1
7394 loop newdeadline
7395 end;
7398 loop infinity;
7399 with Quit ->
7400 Config.save ();