Fix getanchor
[llpp.git] / main.ml
blobfc434d6b640484adb3bb01dbccc754610f031424
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
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 fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess :
87 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and cancelonempty = bool
160 and te =
161 | TEstop
162 | TEdone of string
163 | TEcont of string
164 | TEswitch of textentry
167 type 'a circbuf =
168 { store : 'a array
169 ; mutable rc : int
170 ; mutable wc : int
171 ; mutable len : int
175 let bound v minv maxv =
176 max minv (min maxv v);
179 let cbnew n v =
180 { store = Array.create n v
181 ; rc = 0
182 ; wc = 0
183 ; len = 0
187 let drawstring size x y s =
188 Gl.enable `blend;
189 Gl.enable `texture_2d;
190 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
191 ignore (drawstr size x y s);
192 Gl.disable `blend;
193 Gl.disable `texture_2d;
196 let drawstring1 size x y s =
197 drawstr size x y s;
200 let drawstring2 size x y fmt =
201 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
204 let cbcap b = Array.length b.store;;
206 let cbput b v =
207 let cap = cbcap b in
208 b.store.(b.wc) <- v;
209 b.wc <- (b.wc + 1) mod cap;
210 b.rc <- b.wc;
211 b.len <- min (b.len + 1) cap;
214 let cbempty b = b.len = 0;;
216 let cbgetg b circular dir =
217 if cbempty b
218 then b.store.(0)
219 else
220 let rc = b.rc + dir in
221 let rc =
222 if circular
223 then (
224 if rc = -1
225 then b.len-1
226 else (
227 if rc = b.len
228 then 0
229 else rc
232 else max 0 (min rc (b.len-1))
234 b.rc <- rc;
235 b.store.(rc);
238 let cbget b = cbgetg b false;;
239 let cbgetc b = cbgetg b true;;
241 type page =
242 { pageno : int
243 ; pagedimno : int
244 ; pagew : int
245 ; pageh : int
246 ; pagex : int
247 ; pagey : int
248 ; pagevw : int
249 ; pagevh : int
250 ; pagedispx : int
251 ; pagedispy : int
252 ; pagecol : int
256 let debugl l =
257 dolog "l %d dim=%d {" l.pageno l.pagedimno;
258 dolog " WxH %dx%d" l.pagew l.pageh;
259 dolog " vWxH %dx%d" l.pagevw l.pagevh;
260 dolog " pagex,y %d,%d" l.pagex l.pagey;
261 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
262 dolog " column %d" l.pagecol;
263 dolog "}";
266 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
267 dolog "rect {";
268 dolog " x0,y0=(% f, % f)" x0 y0;
269 dolog " x1,y1=(% f, % f)" x1 y1;
270 dolog " x2,y2=(% f, % f)" x2 y2;
271 dolog " x3,y3=(% f, % f)" x3 y3;
272 dolog "}";
275 type multicolumns = multicol * pagegeom
276 and splitcolumns = columncount * pagegeom
277 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
278 and multicol = columncount * covercount * covercount
279 and pdimno = int
280 and columncount = int
281 and covercount = int;;
283 type conf =
284 { mutable scrollbw : int
285 ; mutable scrollh : int
286 ; mutable icase : bool
287 ; mutable preload : bool
288 ; mutable pagebias : int
289 ; mutable verbose : bool
290 ; mutable debug : bool
291 ; mutable scrollstep : int
292 ; mutable hscrollstep : int
293 ; mutable maxhfit : bool
294 ; mutable crophack : bool
295 ; mutable autoscrollstep : int
296 ; mutable maxwait : float option
297 ; mutable hlinks : bool
298 ; mutable underinfo : bool
299 ; mutable interpagespace : interpagespace
300 ; mutable zoom : float
301 ; mutable presentation : bool
302 ; mutable angle : angle
303 ; mutable winw : int
304 ; mutable winh : int
305 ; mutable savebmarks : bool
306 ; mutable proportional : proportional
307 ; mutable trimmargins : trimmargins
308 ; mutable trimfuzz : irect
309 ; mutable memlimit : memsize
310 ; mutable texcount : texcount
311 ; mutable sliceheight : sliceheight
312 ; mutable thumbw : width
313 ; mutable jumpback : bool
314 ; mutable bgcolor : float * float * float
315 ; mutable bedefault : bool
316 ; mutable scrollbarinpm : bool
317 ; mutable tilew : int
318 ; mutable tileh : int
319 ; mutable mustoresize : memsize
320 ; mutable checkers : bool
321 ; mutable aalevel : int
322 ; mutable urilauncher : string
323 ; mutable pathlauncher : string
324 ; mutable colorspace : colorspace
325 ; mutable invert : bool
326 ; mutable colorscale : float
327 ; mutable redirectstderr : bool
328 ; mutable ghyllscroll : (int * int * int) option
329 ; mutable columns : columns
330 ; mutable beyecolumns : columncount option
331 ; mutable selcmd : string
332 ; mutable updatecurs : bool
333 ; mutable keyhashes : (string * keyhash) list
334 ; mutable hfsize : int
336 and columns =
337 | Csingle
338 | Cmulti of multicolumns
339 | Csplit of splitcolumns
342 type anchor = pageno * top;;
344 type outline = string * int * anchor;;
346 type rect = float * float * float * float * float * float * float * float;;
348 type tile = opaque * pixmapsize * elapsed
349 and elapsed = float;;
350 type pagemapkey = pageno * gen;;
351 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
352 and row = int
353 and col = int;;
355 let emptyanchor = (0, 0.0);;
357 type infochange = | Memused | Docinfo | Pdim;;
359 class type uioh = object
360 method display : unit
361 method key : int -> int -> uioh
362 method button : int -> bool -> int -> int -> int -> uioh
363 method motion : int -> int -> uioh
364 method pmotion : int -> int -> uioh
365 method infochanged : infochange -> unit
366 method scrollpw : (int * float * float)
367 method scrollph : (int * float * float)
368 method modehash : keyhash
369 end;;
371 type mode =
372 | Birdseye of (conf * leftx * pageno * pageno * anchor)
373 | Textentry of (textentry * onleave)
374 | View
375 | LinkNav of linktarget
376 and onleave = leavetextentrystatus -> unit
377 and leavetextentrystatus = | Cancel | Confirm
378 and helpitem = string * int * action
379 and action =
380 | Noaction
381 | Action of (uioh -> uioh)
382 and linktarget =
383 | Ltexact of (pageno * int)
384 | Ltgendir of int
387 let isbirdseye = function Birdseye _ -> true | _ -> false;;
388 let istextentry = function Textentry _ -> true | _ -> false;;
390 type currently =
391 | Idle
392 | Loading of (page * gen)
393 | Tiling of (
394 page * opaque * colorspace * angle * gen * col * row * width * height
396 | Outlining of outline list
399 let emptykeyhash = Hashtbl.create 0;;
400 let nouioh : uioh = object (self)
401 method display = ()
402 method key _ _ = self
403 method button _ _ _ _ _ = self
404 method motion _ _ = self
405 method pmotion _ _ = self
406 method infochanged _ = ()
407 method scrollpw = (0, nan, nan)
408 method scrollph = (0, nan, nan)
409 method modehash = emptykeyhash
410 end;;
412 type state =
413 { mutable sr : Unix.file_descr
414 ; mutable sw : Unix.file_descr
415 ; mutable wsfd : Unix.file_descr
416 ; mutable errfd : Unix.file_descr option
417 ; mutable stderr : Unix.file_descr
418 ; mutable errmsgs : Buffer.t
419 ; mutable newerrmsgs : bool
420 ; mutable w : int
421 ; mutable x : int
422 ; mutable y : int
423 ; mutable scrollw : int
424 ; mutable hscrollh : int
425 ; mutable anchor : anchor
426 ; mutable ranchors : (string * string * anchor) list
427 ; mutable maxy : int
428 ; mutable layout : page list
429 ; pagemap : (pagemapkey, opaque) Hashtbl.t
430 ; tilemap : (tilemapkey, tile) Hashtbl.t
431 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
432 ; mutable pdims : (pageno * width * height * leftx) list
433 ; mutable pagecount : int
434 ; mutable currently : currently
435 ; mutable mstate : mstate
436 ; mutable searchpattern : string
437 ; mutable rects : (pageno * recttype * rect) list
438 ; mutable rects1 : (pageno * recttype * rect) list
439 ; mutable text : string
440 ; mutable fullscreen : (width * height) option
441 ; mutable mode : mode
442 ; mutable uioh : uioh
443 ; mutable outlines : outline array
444 ; mutable bookmarks : outline list
445 ; mutable path : string
446 ; mutable password : string
447 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
448 ; mutable memused : memsize
449 ; mutable gen : gen
450 ; mutable throttle : (page list * int * float) option
451 ; mutable autoscroll : int option
452 ; mutable ghyll : (int option -> unit)
453 ; mutable help : helpitem array
454 ; mutable docinfo : (int * string) list
455 ; mutable texid : GlTex.texture_id option
456 ; hists : hists
457 ; mutable prevzoom : float
458 ; mutable progress : float
459 ; mutable redisplay : bool
460 ; mutable mpos : mpos
461 ; mutable keystate : keystate
462 ; mutable glinks : bool
463 ; mutable prevcolumns : (columns * float) option
465 and hists =
466 { pat : string circbuf
467 ; pag : string circbuf
468 ; nav : anchor circbuf
469 ; sel : string circbuf
473 let defconf =
474 { scrollbw = 7
475 ; scrollh = 12
476 ; icase = true
477 ; preload = true
478 ; pagebias = 0
479 ; verbose = false
480 ; debug = false
481 ; scrollstep = 24
482 ; hscrollstep = 24
483 ; maxhfit = true
484 ; crophack = false
485 ; autoscrollstep = 2
486 ; maxwait = None
487 ; hlinks = false
488 ; underinfo = false
489 ; interpagespace = 2
490 ; zoom = 1.0
491 ; presentation = false
492 ; angle = 0
493 ; winw = 900
494 ; winh = 900
495 ; savebmarks = true
496 ; proportional = true
497 ; trimmargins = false
498 ; trimfuzz = (0,0,0,0)
499 ; memlimit = 32 lsl 20
500 ; texcount = 256
501 ; sliceheight = 24
502 ; thumbw = 76
503 ; jumpback = true
504 ; bgcolor = (0.5, 0.5, 0.5)
505 ; bedefault = false
506 ; scrollbarinpm = true
507 ; tilew = 2048
508 ; tileh = 2048
509 ; mustoresize = 256 lsl 20
510 ; checkers = true
511 ; aalevel = 8
512 ; urilauncher =
513 (match platform with
514 | Plinux | Pfreebsd | Pdragonflybsd
515 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
516 | Posx -> "open \"%s\""
517 | Pcygwin -> "cygstart \"%s\""
518 | Punknown -> "echo %s")
519 ; pathlauncher = "lp \"%s\""
520 ; selcmd =
521 (match platform with
522 | Plinux | Pfreebsd | Pdragonflybsd
523 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
524 | Posx -> "pbcopy"
525 | Pcygwin -> "wsel"
526 | Punknown -> "cat")
527 ; colorspace = Rgb
528 ; invert = false
529 ; colorscale = 1.0
530 ; redirectstderr = false
531 ; ghyllscroll = None
532 ; columns = Csingle
533 ; beyecolumns = None
534 ; updatecurs = false
535 ; hfsize = 12
536 ; keyhashes =
537 let mk n = (n, Hashtbl.create 1) in
538 [ mk "global"
539 ; mk "info"
540 ; mk "help"
541 ; mk "outline"
542 ; mk "listview"
543 ; mk "birdseye"
544 ; mk "textentry"
545 ; mk "links"
546 ; mk "view"
551 let findkeyhash c name =
552 try List.assoc name c.keyhashes
553 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
556 let conf = { defconf with angle = defconf.angle };;
558 type fontstate =
559 { mutable fontsize : int
560 ; mutable wwidth : float
561 ; mutable maxrows : int
565 let fstate =
566 { fontsize = 14
567 ; wwidth = nan
568 ; maxrows = -1
572 let setfontsize n =
573 fstate.fontsize <- n;
574 fstate.wwidth <- measurestr fstate.fontsize "w";
575 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
578 let geturl s =
579 let colonpos = try String.index s ':' with Not_found -> -1 in
580 let len = String.length s in
581 if colonpos >= 0 && colonpos + 3 < len
582 then (
583 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
584 then
585 let schemestartpos =
586 try String.rindex_from s colonpos ' '
587 with Not_found -> -1
589 let scheme =
590 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
592 match scheme with
593 | "http" | "ftp" | "mailto" ->
594 let epos =
595 try String.index_from s colonpos ' '
596 with Not_found -> len
598 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
599 | _ -> ""
600 else ""
602 else ""
605 let gotouri uri =
606 if String.length conf.urilauncher = 0
607 then print_endline uri
608 else (
609 let url = geturl uri in
610 if String.length url = 0
611 then print_endline uri
612 else
613 let re = Str.regexp "%s" in
614 let command = Str.global_replace re url conf.urilauncher in
615 try popen command []
616 with exn ->
617 Printf.eprintf
618 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
619 flush stderr;
623 let version () =
624 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
625 (platform_to_string platform) Sys.word_size Sys.ocaml_version
628 let makehelp () =
629 let strings = version () :: "" :: Help.keys in
630 Array.of_list (
631 List.map (fun s ->
632 let url = geturl s in
633 if String.length url > 0
634 then (s, 0, Action (fun u -> gotouri url; u))
635 else (s, 0, Noaction)
636 ) strings);
639 let noghyll _ = ();;
640 let firstgeomcmds = "", [];;
642 let state =
643 { sr = Unix.stdin
644 ; sw = Unix.stdin
645 ; wsfd = Unix.stdin
646 ; errfd = None
647 ; stderr = Unix.stderr
648 ; errmsgs = Buffer.create 0
649 ; newerrmsgs = false
650 ; x = 0
651 ; y = 0
652 ; w = 0
653 ; scrollw = 0
654 ; hscrollh = 0
655 ; anchor = emptyanchor
656 ; ranchors = []
657 ; layout = []
658 ; maxy = max_int
659 ; tilelru = Queue.create ()
660 ; pagemap = Hashtbl.create 10
661 ; tilemap = Hashtbl.create 10
662 ; pdims = []
663 ; pagecount = 0
664 ; currently = Idle
665 ; mstate = Mnone
666 ; rects = []
667 ; rects1 = []
668 ; text = ""
669 ; mode = View
670 ; fullscreen = None
671 ; searchpattern = ""
672 ; outlines = [||]
673 ; bookmarks = []
674 ; path = ""
675 ; password = ""
676 ; geomcmds = firstgeomcmds
677 ; hists =
678 { nav = cbnew 10 (0, 0.0)
679 ; pat = cbnew 10 ""
680 ; pag = cbnew 10 ""
681 ; sel = cbnew 10 ""
683 ; memused = 0
684 ; gen = 0
685 ; throttle = None
686 ; autoscroll = None
687 ; ghyll = noghyll
688 ; help = makehelp ()
689 ; docinfo = []
690 ; texid = None
691 ; prevzoom = 1.0
692 ; progress = -1.0
693 ; uioh = nouioh
694 ; redisplay = true
695 ; mpos = (-1, -1)
696 ; keystate = KSnone
697 ; glinks = false
698 ; prevcolumns = None
702 let vlog fmt =
703 if conf.verbose
704 then
705 Printf.kprintf prerr_endline fmt
706 else
707 Printf.kprintf ignore fmt
710 let launchpath () =
711 if String.length conf.pathlauncher = 0
712 then print_endline state.path
713 else (
714 let re = Str.regexp "%s" in
715 let command = Str.global_replace re state.path conf.pathlauncher in
716 try popen command []
717 with exn ->
718 Printf.eprintf
719 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
720 flush stderr;
724 module Ne = struct
725 type 'a t = | Res of 'a | Exn of exn;;
727 let pipe () =
728 try Res (Unix.pipe ())
729 with exn -> Exn exn
732 let clo fd f =
733 try Unix.close fd
734 with exn -> f (Printexc.to_string exn)
737 let dup fd =
738 try Res (Unix.dup fd)
739 with exn -> Exn exn
742 let dup2 fd1 fd2 =
743 try Res (Unix.dup2 fd1 fd2)
744 with exn -> Exn exn
746 end;;
748 let redirectstderr () =
749 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
750 if conf.redirectstderr
751 then
752 match Ne.pipe () with
753 | Ne.Exn exn ->
754 dolog "failed to create stderr redirection pipes: %s"
755 (Printexc.to_string exn)
757 | Ne.Res (r, w) ->
758 begin match Ne.dup Unix.stderr with
759 | Ne.Exn exn ->
760 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
761 Ne.clo r (clofail "pipe/r");
762 Ne.clo w (clofail "pipe/w");
764 | Ne.Res dupstderr ->
765 begin match Ne.dup2 w Unix.stderr with
766 | Ne.Exn exn ->
767 dolog "failed to dup2 to stderr: %s"
768 (Printexc.to_string exn);
769 Ne.clo dupstderr (clofail "stderr duplicate");
770 Ne.clo r (clofail "redir pipe/r");
771 Ne.clo w (clofail "redir pipe/w");
773 | Ne.Res () ->
774 state.stderr <- dupstderr;
775 state.errfd <- Some r;
776 end;
778 else (
779 state.newerrmsgs <- false;
780 begin match state.errfd with
781 | Some fd ->
782 begin match Ne.dup2 state.stderr Unix.stderr with
783 | Ne.Exn exn ->
784 dolog "failed to dup2 original stderr: %s"
785 (Printexc.to_string exn)
786 | Ne.Res () ->
787 Ne.clo fd (clofail "dup of stderr");
788 Unix.dup2 state.stderr Unix.stderr;
789 state.errfd <- None;
790 end;
791 | None -> ()
792 end;
793 prerr_string (Buffer.contents state.errmsgs);
794 flush stderr;
795 Buffer.clear state.errmsgs;
799 module G =
800 struct
801 let postRedisplay who =
802 if conf.verbose
803 then prerr_endline ("redisplay for " ^ who);
804 state.redisplay <- true;
806 end;;
808 let getopaque pageno =
809 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
810 with Not_found -> None
813 let putopaque pageno opaque =
814 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
817 let pagetranslatepoint l x y =
818 let dy = y - l.pagedispy in
819 let y = dy + l.pagey in
820 let dx = x - l.pagedispx in
821 let x = dx + l.pagex in
822 (x, y);
825 let getunder x y =
826 let rec f = function
827 | l :: rest ->
828 begin match getopaque l.pageno with
829 | Some opaque ->
830 let x0 = l.pagedispx in
831 let x1 = x0 + l.pagevw in
832 let y0 = l.pagedispy in
833 let y1 = y0 + l.pagevh in
834 if y >= y0 && y <= y1 && x >= x0 && x <= x1
835 then
836 let px, py = pagetranslatepoint l x y in
837 match whatsunder opaque px py with
838 | Unone -> f rest
839 | under -> under
840 else f rest
841 | _ ->
842 f rest
844 | [] -> Unone
846 f state.layout
849 let showtext c s =
850 state.text <- Printf.sprintf "%c%s" c s;
851 G.postRedisplay "showtext";
854 let undertext = function
855 | Unone -> "none"
856 | Ulinkuri s -> s
857 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
858 | Utext s -> "font: " ^ s
859 | Uunexpected s -> "unexpected: " ^ s
860 | Ulaunch s -> "launch: " ^ s
861 | Unamed s -> "named: " ^ s
862 | Uremote (filename, pageno) ->
863 Printf.sprintf "%s: page %d" filename (pageno+1)
866 let updateunder x y =
867 match getunder x y with
868 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
869 | Ulinkuri uri ->
870 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
871 Wsi.setcursor Wsi.CURSOR_INFO
872 | Ulinkgoto (pageno, _) ->
873 if conf.underinfo
874 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
875 Wsi.setcursor Wsi.CURSOR_INFO
876 | Utext s ->
877 if conf.underinfo then showtext 'f' ("ont: " ^ s);
878 Wsi.setcursor Wsi.CURSOR_TEXT
879 | Uunexpected s ->
880 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
881 Wsi.setcursor Wsi.CURSOR_INHERIT
882 | Ulaunch s ->
883 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
884 Wsi.setcursor Wsi.CURSOR_INHERIT
885 | Unamed s ->
886 if conf.underinfo then showtext 'n' ("amed: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_INHERIT
888 | Uremote (filename, pageno) ->
889 if conf.underinfo then showtext 'r'
890 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
891 Wsi.setcursor Wsi.CURSOR_INFO
894 let showlinktype under =
895 if conf.underinfo
896 then
897 match under with
898 | Unone -> ()
899 | under ->
900 let s = undertext under in
901 showtext ' ' s
904 let addchar s c =
905 let b = Buffer.create (String.length s + 1) in
906 Buffer.add_string b s;
907 Buffer.add_char b c;
908 Buffer.contents b;
911 let colorspace_of_string s =
912 match String.lowercase s with
913 | "rgb" -> Rgb
914 | "bgr" -> Bgr
915 | "gray" -> Gray
916 | _ -> failwith "invalid colorspace"
919 let int_of_colorspace = function
920 | Rgb -> 0
921 | Bgr -> 1
922 | Gray -> 2
925 let colorspace_of_int = function
926 | 0 -> Rgb
927 | 1 -> Bgr
928 | 2 -> Gray
929 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
932 let colorspace_to_string = function
933 | Rgb -> "rgb"
934 | Bgr -> "bgr"
935 | Gray -> "gray"
938 let intentry_with_suffix text key =
939 let c =
940 if key >= 32 && key < 127
941 then Char.chr key
942 else '\000'
944 match Char.lowercase c with
945 | '0' .. '9' ->
946 let text = addchar text c in
947 TEcont text
949 | 'k' | 'm' | 'g' ->
950 let text = addchar text c in
951 TEcont text
953 | _ ->
954 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
955 TEcont text
958 let multicolumns_to_string (n, a, b) =
959 if a = 0 && b = 0
960 then Printf.sprintf "%d" n
961 else Printf.sprintf "%d,%d,%d" n a b;
964 let multicolumns_of_string s =
966 (int_of_string s, 0, 0)
967 with _ ->
968 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
971 let readcmd fd =
972 let s = "xxxx" in
973 let n = Unix.read fd s 0 4 in
974 if n != 4 then failwith "incomplete read(len)";
975 let len = 0
976 lor (Char.code s.[0] lsl 24)
977 lor (Char.code s.[1] lsl 16)
978 lor (Char.code s.[2] lsl 8)
979 lor (Char.code s.[3] lsl 0)
981 let s = String.create len in
982 let n = Unix.read fd s 0 len in
983 if n != len then failwith "incomplete read(data)";
987 let btod b = if b then 1 else 0;;
989 let wcmd fmt =
990 let b = Buffer.create 16 in
991 Buffer.add_string b "llll";
992 Printf.kbprintf
993 (fun b ->
994 let s = Buffer.contents b in
995 let n = String.length s in
996 let len = n - 4 in
997 (* dolog "wcmd %S" (String.sub s 4 len); *)
998 s.[0] <- Char.chr ((len lsr 24) land 0xff);
999 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1000 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1001 s.[3] <- Char.chr (len land 0xff);
1002 let n' = Unix.write state.sw s 0 n in
1003 if n' != n then failwith "write failed";
1004 ) b fmt;
1007 let calcips h =
1008 if conf.presentation
1009 then
1010 let d = conf.winh - h in
1011 max 0 ((d + 1) / 2)
1012 else
1013 conf.interpagespace
1016 let calcheight () =
1017 let rec f pn ph pi fh l =
1018 match l with
1019 | (n, _, h, _) :: rest ->
1020 let ips = calcips h in
1021 let fh =
1022 if conf.presentation
1023 then fh+ips
1024 else (
1025 if isbirdseye state.mode && pn = 0
1026 then fh + ips
1027 else fh
1030 let fh = fh + ((n - pn) * (ph + pi)) in
1031 f n h ips fh rest;
1033 | [] ->
1034 let inc =
1035 if conf.presentation || (isbirdseye state.mode && pn = 0)
1036 then 0
1037 else -pi
1039 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1040 max 0 fh
1042 let fh = f 0 0 0 0 state.pdims in
1046 let calcheight () =
1047 match conf.columns with
1048 | Csingle -> calcheight ()
1049 | Cmulti ((c, _, _), b) ->
1050 let rec loop y h n =
1051 if n < 0
1052 then loop y h (n+1)
1053 else (
1054 if n = Array.length b
1055 then y + h
1056 else
1057 let (_, _, y', (_, _, h', _)) = b.(n) in
1058 let y = min y y'
1059 and h = max h h' in
1060 loop y h (n+1)
1063 loop max_int 0 (((Array.length b - 1) / c) * c)
1064 | Csplit (_, b) ->
1065 if Array.length b > 0
1066 then
1067 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1068 y + h
1069 else 0
1072 let getpageyh pageno =
1073 let rec f pn ph pi y l =
1074 match l with
1075 | (n, _, h, _) :: rest ->
1076 let ips = calcips h in
1077 if n >= pageno
1078 then
1079 let h = if n = pageno then h else ph in
1080 if conf.presentation && n = pageno
1081 then
1082 y + (pageno - pn) * (ph + pi) + pi, h
1083 else
1084 y + (pageno - pn) * (ph + pi), h
1085 else
1086 let y = y + (if conf.presentation then pi else 0) in
1087 let y = y + (n - pn) * (ph + pi) in
1088 f n h ips y rest
1090 | [] ->
1091 y + (pageno - pn) * (ph + pi), ph
1093 f 0 0 0 0 state.pdims
1096 let getpageyh pageno =
1097 match conf.columns with
1098 | Csingle -> getpageyh pageno
1099 | Cmulti (_, b) ->
1100 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1101 y, h
1102 | Csplit (c, b) ->
1103 let n = pageno*c in
1104 let (_, _, y, (_, _, h, _)) = b.(n) in
1105 y, h
1108 let getpagedim pageno =
1109 let rec f ppdim l =
1110 match l with
1111 | (n, _, _, _) as pdim :: rest ->
1112 if n >= pageno
1113 then (if n = pageno then pdim else ppdim)
1114 else f pdim rest
1116 | [] -> ppdim
1118 f (-1, -1, -1, -1) state.pdims
1121 let getpagey pageno = fst (getpageyh pageno);;
1123 let nogeomcmds cmds =
1124 match cmds with
1125 | s, [] -> String.length s = 0
1126 | _ -> false
1129 let layout1 y sh =
1130 let sh = sh - state.hscrollh in
1131 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1132 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1133 match pdims with
1134 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1135 let ips = calcips h in
1136 let yinc =
1137 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1138 then ips
1139 else 0
1141 (w, h, ips, xoff), rest, pdimno + 1, yinc
1142 | _ ->
1143 prev, pdims, pdimno, 0
1145 let dy = dy + yinc in
1146 let py = py + yinc in
1147 if pageno = state.pagecount || dy >= sh
1148 then
1149 accu
1150 else
1151 let vy = y + dy in
1152 if py + h <= vy - yinc
1153 then
1154 let py = py + h + ips in
1155 let dy = max 0 (py - y) in
1156 f ~pageno:(pageno+1)
1157 ~pdimno
1158 ~prev:curr
1161 ~pdims:rest
1162 ~accu
1163 else
1164 let pagey = vy - py in
1165 let pagevh = h - pagey in
1166 let pagevh = min (sh - dy) pagevh in
1167 let off = if yinc > 0 then py - vy else 0 in
1168 let py = py + h + ips in
1169 let pagex, dx =
1170 let xoff = xoff +
1171 if state.w < conf.winw - state.scrollw
1172 then (conf.winw - state.scrollw - state.w) / 2
1173 else 0
1175 let dispx = xoff + state.x in
1176 if dispx < 0
1177 then (-dispx, 0)
1178 else (0, dispx)
1180 let pagevw =
1181 let lw = w - pagex in
1182 min lw (conf.winw - state.scrollw)
1184 let e =
1185 { pageno = pageno
1186 ; pagedimno = pdimno
1187 ; pagew = w
1188 ; pageh = h
1189 ; pagex = pagex
1190 ; pagey = pagey + off
1191 ; pagevw = pagevw
1192 ; pagevh = pagevh - off
1193 ; pagedispx = dx
1194 ; pagedispy = dy + off
1195 ; pagecol = 0
1198 let accu = e :: accu in
1199 f ~pageno:(pageno+1)
1200 ~pdimno
1201 ~prev:curr
1203 ~dy:(dy+pagevh+ips)
1204 ~pdims:rest
1205 ~accu
1207 let accu =
1209 ~pageno:0
1210 ~pdimno:~-1
1211 ~prev:(0,0,0,0)
1212 ~py:0
1213 ~dy:0
1214 ~pdims:state.pdims
1215 ~accu:[]
1217 List.rev accu
1220 let layoutN ((columns, coverA, coverB), b) y sh =
1221 let sh = sh - state.hscrollh in
1222 let rec fold accu n =
1223 if n = Array.length b
1224 then accu
1225 else
1226 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1227 if (vy - y) > sh &&
1228 (n = coverA - 1
1229 || n = state.pagecount - coverB
1230 || (n - coverA) mod columns = columns - 1)
1231 then accu
1232 else
1233 let accu =
1234 if vy + h > y
1235 then
1236 let pagey = max 0 (y - vy) in
1237 let pagedispy = if pagey > 0 then 0 else vy - y in
1238 let pagedispx, pagex =
1239 let pdx =
1240 if n = coverA - 1 || n = state.pagecount - coverB
1241 then state.x + (conf.winw - state.scrollw - w) / 2
1242 else dx + xoff + state.x
1244 if pdx < 0
1245 then 0, -pdx
1246 else pdx, 0
1248 let pagevw =
1249 let vw = conf.winw - state.scrollw - pagedispx in
1250 let pw = w - pagex in
1251 min vw pw
1253 let pagevh = min (h - pagey) (sh - pagedispy) in
1254 if pagevw > 0 && pagevh > 0
1255 then
1256 let e =
1257 { pageno = n
1258 ; pagedimno = pdimno
1259 ; pagew = w
1260 ; pageh = h
1261 ; pagex = pagex
1262 ; pagey = pagey
1263 ; pagevw = pagevw
1264 ; pagevh = pagevh
1265 ; pagedispx = pagedispx
1266 ; pagedispy = pagedispy
1267 ; pagecol = 0
1270 e :: accu
1271 else
1272 accu
1273 else
1274 accu
1276 fold accu (n+1)
1278 List.rev (fold [] 0)
1281 let layoutS (columns, b) y sh =
1282 let sh = sh - state.hscrollh in
1283 let rec fold accu n =
1284 if n = Array.length b
1285 then accu
1286 else
1287 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1288 if (vy - y) > sh
1289 then accu
1290 else
1291 let accu =
1292 if vy + pageh > y
1293 then
1294 let x = xoff + state.x in
1295 let pagey = max 0 (y - vy) in
1296 let pagedispy = if pagey > 0 then 0 else vy - y in
1297 let pagedispx, pagex =
1298 if px = 0
1299 then (
1300 if x < 0
1301 then 0, -x
1302 else x, 0
1304 else (
1305 let px = px - x in
1306 if px < 0
1307 then -px, 0
1308 else 0, px
1311 let pagecolw = pagew/columns in
1312 let pagedispx =
1313 if pagecolw < conf.winw
1314 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1315 else pagedispx
1317 let pagevw =
1318 let vw = conf.winw - pagedispx - state.scrollw in
1319 let pw = pagew - pagex in
1320 min vw pw
1322 let pagevw = min pagevw pagecolw in
1323 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1324 if pagevw > 0 && pagevh > 0
1325 then
1326 let e =
1327 { pageno = n/columns
1328 ; pagedimno = pdimno
1329 ; pagew = pagew
1330 ; pageh = pageh
1331 ; pagex = pagex
1332 ; pagey = pagey
1333 ; pagevw = pagevw
1334 ; pagevh = pagevh
1335 ; pagedispx = pagedispx
1336 ; pagedispy = pagedispy
1337 ; pagecol = n mod columns
1340 e :: accu
1341 else
1342 accu
1343 else
1344 accu
1346 fold accu (n+1)
1348 List.rev (fold [] 0)
1351 let layout y sh =
1352 if nogeomcmds state.geomcmds
1353 then
1354 match conf.columns with
1355 | Csingle -> layout1 y sh
1356 | Cmulti c -> layoutN c y sh
1357 | Csplit s -> layoutS s y sh
1358 else []
1361 let clamp incr =
1362 let y = state.y + incr in
1363 let y = max 0 y in
1364 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1368 let itertiles l f =
1369 let tilex = l.pagex mod conf.tilew in
1370 let tiley = l.pagey mod conf.tileh in
1372 let col = l.pagex / conf.tilew in
1373 let row = l.pagey / conf.tileh in
1375 let rec rowloop row y0 dispy h =
1376 if h = 0
1377 then ()
1378 else (
1379 let dh = conf.tileh - y0 in
1380 let dh = min h dh in
1381 let rec colloop col x0 dispx w =
1382 if w = 0
1383 then ()
1384 else (
1385 let dw = conf.tilew - x0 in
1386 let dw = min w dw in
1388 f col row dispx dispy x0 y0 dw dh;
1389 colloop (col+1) 0 (dispx+dw) (w-dw)
1392 colloop col tilex l.pagedispx l.pagevw;
1393 rowloop (row+1) 0 (dispy+dh) (h-dh)
1396 if l.pagevw > 0 && l.pagevh > 0
1397 then rowloop row tiley l.pagedispy l.pagevh;
1400 let gettileopaque l col row =
1401 let key =
1402 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1404 try Some (Hashtbl.find state.tilemap key)
1405 with Not_found -> None
1408 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1409 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1410 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1413 let drawtiles l color =
1414 GlDraw.color color;
1415 let f col row x y tilex tiley w h =
1416 match gettileopaque l col row with
1417 | Some (opaque, _, t) ->
1418 let params = x, y, w, h, tilex, tiley in
1419 if conf.invert
1420 then (
1421 Gl.enable `blend;
1422 GlFunc.blend_func `zero `one_minus_src_color;
1424 drawtile params opaque;
1425 if conf.invert
1426 then Gl.disable `blend;
1427 if conf.debug
1428 then (
1429 let s = Printf.sprintf
1430 "%d[%d,%d] %f sec"
1431 l.pageno col row t
1433 let w = measurestr fstate.fontsize s in
1434 GlMisc.push_attrib [`current];
1435 GlDraw.color (0.0, 0.0, 0.0);
1436 GlDraw.rect
1437 (float (x-2), float (y-2))
1438 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1439 GlDraw.color (1.0, 1.0, 1.0);
1440 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1441 GlMisc.pop_attrib ();
1444 | _ ->
1445 let w =
1446 let lw = conf.winw - state.scrollw - x in
1447 min lw w
1448 and h =
1449 let lh = conf.winh - y in
1450 min lh h
1452 begin match state.texid with
1453 | Some id ->
1454 Gl.enable `texture_2d;
1455 GlTex.bind_texture `texture_2d id;
1456 let x0 = float x
1457 and y0 = float y
1458 and x1 = float (x+w)
1459 and y1 = float (y+h) in
1461 let tw = float w /. 64.0
1462 and th = float h /. 64.0 in
1463 let tx0 = float tilex /. 64.0
1464 and ty0 = float tiley /. 64.0 in
1465 let tx1 = tx0 +. tw
1466 and ty1 = ty0 +. th in
1467 GlDraw.begins `quads;
1468 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1469 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1470 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1471 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1472 GlDraw.ends ();
1474 Gl.disable `texture_2d;
1475 | None ->
1476 GlDraw.color (1.0, 1.0, 1.0);
1477 GlDraw.rect
1478 (float x, float y)
1479 (float (x+w), float (y+h));
1480 end;
1481 if w > 128 && h > fstate.fontsize + 10
1482 then (
1483 GlDraw.color (0.0, 0.0, 0.0);
1484 let c, r =
1485 if conf.verbose
1486 then (col*conf.tilew, row*conf.tileh)
1487 else col, row
1489 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1491 GlDraw.color color;
1493 itertiles l f
1496 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1498 let tilevisible1 l x y =
1499 let ax0 = l.pagex
1500 and ax1 = l.pagex + l.pagevw
1501 and ay0 = l.pagey
1502 and ay1 = l.pagey + l.pagevh in
1504 let bx0 = x
1505 and by0 = y in
1506 let bx1 = min (bx0 + conf.tilew) l.pagew
1507 and by1 = min (by0 + conf.tileh) l.pageh in
1509 let rx0 = max ax0 bx0
1510 and ry0 = max ay0 by0
1511 and rx1 = min ax1 bx1
1512 and ry1 = min ay1 by1 in
1514 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1515 nonemptyintersection
1518 let tilevisible layout n x y =
1519 let rec findpageinlayout m = function
1520 | l :: rest when l.pageno = n ->
1521 tilevisible1 l x y || (
1522 match conf.columns with
1523 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1524 | _ -> false
1526 | _ :: rest -> findpageinlayout 0 rest
1527 | [] -> false
1529 findpageinlayout 0 layout;
1532 let tileready l x y =
1533 tilevisible1 l x y &&
1534 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1537 let tilepage n p layout =
1538 let rec loop = function
1539 | l :: rest ->
1540 if l.pageno = n
1541 then
1542 let f col row _ _ _ _ _ _ =
1543 if state.currently = Idle
1544 then
1545 match gettileopaque l col row with
1546 | Some _ -> ()
1547 | None ->
1548 let x = col*conf.tilew
1549 and y = row*conf.tileh in
1550 let w =
1551 let w = l.pagew - x in
1552 min w conf.tilew
1554 let h =
1555 let h = l.pageh - y in
1556 min h conf.tileh
1558 wcmd "tile %s %d %d %d %d" p x y w h;
1559 state.currently <-
1560 Tiling (
1561 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1562 conf.tilew, conf.tileh
1565 itertiles l f;
1566 else
1567 loop rest
1569 | [] -> ()
1571 if nogeomcmds state.geomcmds
1572 then loop layout;
1575 let preloadlayout visiblepages =
1576 let presentation = conf.presentation in
1577 let interpagespace = conf.interpagespace in
1578 let maxy = state.maxy in
1579 conf.presentation <- false;
1580 conf.interpagespace <- 0;
1581 state.maxy <- calcheight ();
1582 let y =
1583 match visiblepages with
1584 | [] -> if state.y >= maxy then maxy else 0
1585 | l :: _ -> getpagey l.pageno + l.pagey
1587 let y = if y < conf.winh then 0 else y - conf.winh in
1588 let h = conf.winh*3 in
1589 let pages = layout y h in
1590 conf.presentation <- presentation;
1591 conf.interpagespace <- interpagespace;
1592 state.maxy <- maxy;
1593 pages;
1596 let load pages =
1597 let rec loop pages =
1598 if state.currently != Idle
1599 then ()
1600 else
1601 match pages with
1602 | l :: rest ->
1603 begin match getopaque l.pageno with
1604 | None ->
1605 wcmd "page %d %d" l.pageno l.pagedimno;
1606 state.currently <- Loading (l, state.gen);
1607 | Some opaque ->
1608 tilepage l.pageno opaque pages;
1609 loop rest
1610 end;
1611 | _ -> ()
1613 if nogeomcmds state.geomcmds
1614 then loop pages
1617 let preload pages =
1618 load pages;
1619 if conf.preload && state.currently = Idle
1620 then load (preloadlayout pages);
1623 let layoutready layout =
1624 let rec fold all ls =
1625 all && match ls with
1626 | l :: rest ->
1627 let seen = ref false in
1628 let allvisible = ref true in
1629 let foo col row _ _ _ _ _ _ =
1630 seen := true;
1631 allvisible := !allvisible &&
1632 begin match gettileopaque l col row with
1633 | Some _ -> true
1634 | None -> false
1637 itertiles l foo;
1638 fold (!seen && !allvisible) rest
1639 | [] -> true
1641 let alltilesvisible = fold true layout in
1642 alltilesvisible;
1645 let gotoy y =
1646 let y = bound y 0 state.maxy in
1647 let y, layout, proceed =
1648 match conf.maxwait with
1649 | Some time when state.ghyll == noghyll ->
1650 begin match state.throttle with
1651 | None ->
1652 let layout = layout y conf.winh in
1653 let ready = layoutready layout in
1654 if not ready
1655 then (
1656 load layout;
1657 state.throttle <- Some (layout, y, now ());
1659 else G.postRedisplay "gotoy showall (None)";
1660 y, layout, ready
1661 | Some (_, _, started) ->
1662 let dt = now () -. started in
1663 if dt > time
1664 then (
1665 state.throttle <- None;
1666 let layout = layout y conf.winh in
1667 load layout;
1668 G.postRedisplay "maxwait";
1669 y, layout, true
1671 else -1, [], false
1674 | _ ->
1675 let layout = layout y conf.winh in
1676 if true || layoutready layout
1677 then G.postRedisplay "gotoy ready";
1678 y, layout, true
1680 if proceed
1681 then (
1682 state.y <- y;
1683 state.layout <- layout;
1684 begin match state.mode with
1685 | LinkNav (Ltexact (pageno, linkno)) ->
1686 let rec loop = function
1687 | [] ->
1688 state.mode <- LinkNav (Ltgendir 0)
1689 | l :: _ when l.pageno = pageno ->
1690 begin match getopaque pageno with
1691 | None ->
1692 state.mode <- LinkNav (Ltgendir 0)
1693 | Some opaque ->
1694 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1695 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1696 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1697 then state.mode <- LinkNav (Ltgendir 0)
1699 | _ :: rest -> loop rest
1701 loop layout
1702 | _ -> ()
1703 end;
1704 begin match state.mode with
1705 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1706 if not (pagevisible layout pageno)
1707 then (
1708 match state.layout with
1709 | [] -> ()
1710 | l :: _ ->
1711 state.mode <- Birdseye (
1712 conf, leftx, l.pageno, hooverpageno, anchor
1715 | LinkNav (Ltgendir dir as lt) ->
1716 let linknav =
1717 let rec loop = function
1718 | [] -> lt
1719 | l :: rest ->
1720 match getopaque l.pageno with
1721 | None -> loop rest
1722 | Some opaque ->
1723 let link =
1724 let ld =
1725 if dir = 0
1726 then LDfirstvisible (l.pagex, l.pagey, dir)
1727 else (
1728 if dir > 0 then LDfirst else LDlast
1731 findlink opaque ld
1733 match link with
1734 | Lnotfound -> loop rest
1735 | Lfound n ->
1736 showlinktype (getlink opaque n);
1737 Ltexact (l.pageno, n)
1739 loop state.layout
1741 state.mode <- LinkNav linknav
1742 | _ -> ()
1743 end;
1744 preload layout;
1746 state.ghyll <- noghyll;
1747 if conf.updatecurs
1748 then (
1749 let mx, my = state.mpos in
1750 updateunder mx my;
1754 let conttiling pageno opaque =
1755 tilepage pageno opaque
1756 (if conf.preload then preloadlayout state.layout else state.layout)
1759 let gotoy_and_clear_text y =
1760 if not conf.verbose then state.text <- "";
1761 gotoy y;
1764 let getanchor () =
1765 match state.layout with
1766 | [] -> emptyanchor
1767 | l :: _ ->
1768 let coloff = l.pagecol * l.pageh in
1769 (l.pageno,
1770 (float (l.pagey - l.pagedispy) +. float coloff) /. float l.pageh)
1773 let getanchory (n, top) =
1774 let y, h = getpageyh n in
1775 y + (truncate (top *. float h));
1778 let gotoanchor anchor =
1779 gotoy (getanchory anchor);
1782 let addnav () =
1783 cbput state.hists.nav (getanchor ());
1786 let getnav dir =
1787 let anchor = cbgetc state.hists.nav dir in
1788 getanchory anchor;
1791 let gotoghyll y =
1792 let scroll f n a b =
1793 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1794 let snake f a b =
1795 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1796 if f < a
1797 then s (float f /. float a)
1798 else (
1799 if f > b
1800 then 1.0 -. s ((float (f-b) /. float (n-b)))
1801 else 1.0
1804 snake f a b
1805 and summa f n a b =
1806 (* courtesy:
1807 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1808 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1809 let iv1 = iv f in
1810 let ins = float a *. iv1
1811 and outs = float (n-b) *. iv1 in
1812 let ones = b - a in
1813 ins +. outs +. float ones
1815 let rec set (_N, _A, _B) y sy =
1816 let sum = summa 1.0 _N _A _B in
1817 let dy = float (y - sy) in
1818 state.ghyll <- (
1819 let rec gf n y1 o =
1820 if n >= _N
1821 then state.ghyll <- noghyll
1822 else
1823 let go n =
1824 let s = scroll n _N _A _B in
1825 let y1 = y1 +. ((s *. dy) /. sum) in
1826 gotoy_and_clear_text (truncate y1);
1827 state.ghyll <- gf (n+1) y1;
1829 match o with
1830 | None -> go n
1831 | Some y' -> set (_N/2, 0, 0) y' state.y
1833 gf 0 (float state.y)
1836 match conf.ghyllscroll with
1837 | None ->
1838 gotoy_and_clear_text y
1839 | Some nab ->
1840 if state.ghyll == noghyll
1841 then set nab y state.y
1842 else state.ghyll (Some y)
1845 let gotopage n top =
1846 let y, h = getpageyh n in
1847 let y = y + (truncate (top *. float h)) in
1848 gotoghyll y
1851 let gotopage1 n top =
1852 let y = getpagey n in
1853 let y = y + top in
1854 gotoghyll y
1857 let invalidate s f =
1858 state.layout <- [];
1859 state.pdims <- [];
1860 state.rects <- [];
1861 state.rects1 <- [];
1862 match state.geomcmds with
1863 | ps, [] when String.length ps = 0 ->
1864 f ();
1865 state.geomcmds <- s, [];
1867 | ps, [] ->
1868 state.geomcmds <- ps, [s, f];
1870 | ps, (s', _) :: rest when s' = s ->
1871 state.geomcmds <- ps, ((s, f) :: rest);
1873 | ps, cmds ->
1874 state.geomcmds <- ps, ((s, f) :: cmds);
1877 let opendoc path password =
1878 state.path <- path;
1879 state.password <- password;
1880 state.gen <- state.gen + 1;
1881 state.docinfo <- [];
1883 setaalevel conf.aalevel;
1884 Wsi.settitle ("llpp " ^ Filename.basename path);
1885 wcmd "open %s\000%s\000" path password;
1886 invalidate "reqlayout"
1887 (fun () ->
1888 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1891 let scalecolor c =
1892 let c = c *. conf.colorscale in
1893 (c, c, c);
1896 let scalecolor2 (r, g, b) =
1897 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1900 let docolumns = function
1901 | Csingle -> ()
1903 | Cmulti ((columns, coverA, coverB), _) ->
1904 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1905 let rec loop pageno pdimno pdim x y rowh pdims =
1906 let rec fixrow m = if m = pageno then () else
1907 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1908 if h < rowh
1909 then (
1910 let y = y + (rowh - h) / 2 in
1911 a.(m) <- (pdimno, x, y, pdim);
1913 fixrow (m+1)
1915 if pageno = state.pagecount
1916 then fixrow (((pageno - 1) / columns) * columns)
1917 else
1918 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1919 match pdims with
1920 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1921 pdimno+1, pdim, rest
1922 | _ ->
1923 pdimno, pdim, pdims
1925 let x, y, rowh' =
1926 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1927 then (
1928 (conf.winw - state.scrollw - w) / 2,
1929 y + rowh + conf.interpagespace, h
1931 else (
1932 if (pageno - coverA) mod columns = 0
1933 then 0, y + rowh + conf.interpagespace, h
1934 else x, y, max rowh h
1937 if pageno > 1 && (pageno - coverA) mod columns = 0
1938 then fixrow (pageno - columns);
1939 a.(pageno) <- (pdimno, x, y, pdim);
1940 let x = x + w + xoff*2 + conf.interpagespace in
1941 loop (pageno+1) pdimno pdim x y rowh' pdims
1943 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1944 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1946 | Csplit (c, _) ->
1947 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1948 let rec loop pageno pdimno pdim y pdims =
1949 if pageno = state.pagecount
1950 then ()
1951 else
1952 let pdimno, ((_, w, h, _) as pdim), pdims =
1953 match pdims with
1954 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1955 pdimno+1, pdim, rest
1956 | _ ->
1957 pdimno, pdim, pdims
1959 let cw = w / c in
1960 let rec loop1 n x y =
1961 if n = c then y else (
1962 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1963 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1966 let y = loop1 0 0 y in
1967 loop (pageno+1) pdimno pdim y pdims
1969 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1970 conf.columns <- Csplit (c, a);
1973 let represent () =
1974 docolumns conf.columns;
1975 state.maxy <- calcheight ();
1976 state.hscrollh <-
1977 if state.w <= conf.winw - state.scrollw
1978 then 0
1979 else state.scrollw
1981 match state.mode with
1982 | Birdseye (_, _, pageno, _, _) ->
1983 let y, h = getpageyh pageno in
1984 let top = (conf.winh - h) / 2 in
1985 gotoy (max 0 (y - top))
1986 | _ -> gotoanchor state.anchor
1989 let reshape w h =
1990 GlDraw.viewport 0 0 w h;
1991 let firsttime = state.geomcmds == firstgeomcmds in
1992 if not firsttime && nogeomcmds state.geomcmds
1993 then state.anchor <- getanchor ();
1995 conf.winw <- w;
1996 let w = truncate (float w *. conf.zoom) - state.scrollw in
1997 let w = max w 2 in
1998 conf.winh <- h;
1999 setfontsize fstate.fontsize;
2000 GlMat.mode `modelview;
2001 GlMat.load_identity ();
2003 GlMat.mode `projection;
2004 GlMat.load_identity ();
2005 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2006 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2007 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2009 let relx =
2010 if conf.zoom <= 1.0
2011 then 0.0
2012 else float state.x /. float state.w
2014 invalidate "geometry"
2015 (fun () ->
2016 state.w <- w;
2017 if not firsttime
2018 then state.x <- truncate (relx *. float w);
2019 let w =
2020 match conf.columns with
2021 | Csingle -> w
2022 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2023 | Csplit (c, _) -> w * c
2025 wcmd "geometry %d %d" w h);
2028 let enttext () =
2029 let len = String.length state.text in
2030 let drawstring s =
2031 let hscrollh =
2032 match state.mode with
2033 | Textentry _
2034 | View ->
2035 let h, _, _ = state.uioh#scrollpw in
2037 | _ -> 0
2039 let rect x w =
2040 GlDraw.rect
2041 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2042 (x+.w, float (conf.winh - hscrollh))
2045 let w = float (conf.winw - state.scrollw - 1) in
2046 if state.progress >= 0.0 && state.progress < 1.0
2047 then (
2048 GlDraw.color (0.3, 0.3, 0.3);
2049 let w1 = w *. state.progress in
2050 rect 0.0 w1;
2051 GlDraw.color (0.0, 0.0, 0.0);
2052 rect w1 (w-.w1)
2054 else (
2055 GlDraw.color (0.0, 0.0, 0.0);
2056 rect 0.0 w;
2059 GlDraw.color (1.0, 1.0, 1.0);
2060 drawstring fstate.fontsize
2061 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2063 let s =
2064 match state.mode with
2065 | Textentry ((prefix, text, _, _, _, _), _) ->
2066 let s =
2067 if len > 0
2068 then
2069 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2070 else
2071 Printf.sprintf "%s%s_" prefix text
2075 | _ -> state.text
2077 let s =
2078 if state.newerrmsgs
2079 then (
2080 if not (istextentry state.mode)
2081 then
2082 let s1 = "(press 'e' to review error messasges)" in
2083 if String.length s > 0 then s ^ " " ^ s1 else s1
2084 else s
2086 else s
2088 if String.length s > 0
2089 then drawstring s
2092 let gctiles () =
2093 let len = Queue.length state.tilelru in
2094 let rec loop qpos =
2095 if state.memused <= conf.memlimit
2096 then ()
2097 else (
2098 if qpos < len
2099 then
2100 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2101 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2102 let (_, pw, ph, _) = getpagedim n in
2104 gen = state.gen
2105 && colorspace = conf.colorspace
2106 && angle = conf.angle
2107 && pagew = pw
2108 && pageh = ph
2109 && (
2110 let layout =
2111 match state.throttle with
2112 | None ->
2113 if conf.preload
2114 then preloadlayout state.layout
2115 else state.layout
2116 | Some (layout, _, _) ->
2117 layout
2119 let x = col*conf.tilew
2120 and y = row*conf.tileh in
2121 tilevisible layout n x y
2123 then Queue.push lruitem state.tilelru
2124 else (
2125 wcmd "freetile %s" p;
2126 state.memused <- state.memused - s;
2127 state.uioh#infochanged Memused;
2128 Hashtbl.remove state.tilemap k;
2130 loop (qpos+1)
2133 loop 0
2136 let flushtiles () =
2137 Queue.iter (fun (k, p, s) ->
2138 wcmd "freetile %s" p;
2139 state.memused <- state.memused - s;
2140 state.uioh#infochanged Memused;
2141 Hashtbl.remove state.tilemap k;
2142 ) state.tilelru;
2143 Queue.clear state.tilelru;
2144 load state.layout;
2147 let logcurrently = function
2148 | Idle -> dolog "Idle"
2149 | Loading (l, gen) ->
2150 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2151 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2152 dolog
2153 "Tiling %d[%d,%d] page=%s cs=%s angle"
2154 l.pageno col row pageopaque
2155 (colorspace_to_string colorspace)
2157 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2158 angle gen conf.angle state.gen
2159 tilew tileh
2160 conf.tilew conf.tileh
2162 | Outlining _ ->
2163 dolog "outlining"
2166 let act cmds =
2167 (* dolog "%S" cmds; *)
2168 let op, args =
2169 let spacepos =
2170 try String.index cmds ' '
2171 with Not_found -> -1
2173 if spacepos = -1
2174 then cmds, ""
2175 else
2176 let l = String.length cmds in
2177 let op = String.sub cmds 0 spacepos in
2178 op, begin
2179 if l - spacepos < 2 then ""
2180 else String.sub cmds (spacepos+1) (l-spacepos-1)
2183 match op with
2184 | "clear" ->
2185 state.uioh#infochanged Pdim;
2186 state.pdims <- [];
2188 | "clearrects" ->
2189 state.rects <- state.rects1;
2190 G.postRedisplay "clearrects";
2192 | "continue" ->
2193 let n =
2194 try Scanf.sscanf args "%u" (fun n -> n)
2195 with exn ->
2196 dolog "error processing 'continue' %S: %s"
2197 cmds (Printexc.to_string exn);
2198 exit 1;
2200 state.pagecount <- n;
2201 begin match state.currently with
2202 | Outlining l ->
2203 state.currently <- Idle;
2204 state.outlines <- Array.of_list (List.rev l)
2205 | _ -> ()
2206 end;
2208 let cur, cmds = state.geomcmds in
2209 if String.length cur = 0
2210 then failwith "umpossible";
2212 begin match List.rev cmds with
2213 | [] ->
2214 state.geomcmds <- "", [];
2215 represent ();
2216 | (s, f) :: rest ->
2217 f ();
2218 state.geomcmds <- s, List.rev rest;
2219 end;
2220 if conf.maxwait = None
2221 then G.postRedisplay "continue";
2223 | "title" ->
2224 Wsi.settitle args
2226 | "msg" ->
2227 showtext ' ' args
2229 | "vmsg" ->
2230 if conf.verbose
2231 then showtext ' ' args
2233 | "progress" ->
2234 let progress, text =
2236 Scanf.sscanf args "%f %n"
2237 (fun f pos ->
2238 f, String.sub args pos (String.length args - pos))
2239 with exn ->
2240 dolog "error processing 'progress' %S: %s"
2241 cmds (Printexc.to_string exn);
2242 exit 1;
2244 state.text <- text;
2245 state.progress <- progress;
2246 G.postRedisplay "progress"
2248 | "firstmatch" ->
2249 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2251 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2252 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2253 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2254 with exn ->
2255 dolog "error processing 'firstmatch' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 let y = (getpagey pageno) + truncate y0 in
2260 addnav ();
2261 gotoy y;
2262 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2264 | "match" ->
2265 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2267 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2268 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2269 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2270 with exn ->
2271 dolog "error processing 'match' %S: %s"
2272 cmds (Printexc.to_string exn);
2273 exit 1;
2275 state.rects1 <-
2276 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2278 | "page" ->
2279 let pageopaque, t =
2281 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2282 with exn ->
2283 dolog "error processing 'page' %S: %s"
2284 cmds (Printexc.to_string exn);
2285 exit 1;
2287 begin match state.currently with
2288 | Loading (l, gen) ->
2289 vlog "page %d took %f sec" l.pageno t;
2290 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2291 begin match state.throttle with
2292 | None ->
2293 let preloadedpages =
2294 if conf.preload
2295 then preloadlayout state.layout
2296 else state.layout
2298 let evict () =
2299 let module IntSet =
2300 Set.Make (struct type t = int let compare = (-) end) in
2301 let set =
2302 List.fold_left (fun s l -> IntSet.add l.pageno s)
2303 IntSet.empty preloadedpages
2305 let evictedpages =
2306 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2307 if not (IntSet.mem pageno set)
2308 then (
2309 wcmd "freepage %s" opaque;
2310 key :: accu
2312 else accu
2313 ) state.pagemap []
2315 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2317 evict ();
2318 state.currently <- Idle;
2319 if gen = state.gen
2320 then (
2321 tilepage l.pageno pageopaque state.layout;
2322 load state.layout;
2323 load preloadedpages;
2324 if pagevisible state.layout l.pageno
2325 && layoutready state.layout
2326 then G.postRedisplay "page";
2329 | Some (layout, _, _) ->
2330 state.currently <- Idle;
2331 tilepage l.pageno pageopaque layout;
2332 load state.layout
2333 end;
2335 | _ ->
2336 dolog "Inconsistent loading state";
2337 logcurrently state.currently;
2338 exit 1
2341 | "tile" ->
2342 let (x, y, opaque, size, t) =
2344 Scanf.sscanf args "%u %u %s %u %f"
2345 (fun x y p size t -> (x, y, p, size, t))
2346 with exn ->
2347 dolog "error processing 'tile' %S: %s"
2348 cmds (Printexc.to_string exn);
2349 exit 1;
2351 begin match state.currently with
2352 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2353 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2355 if tilew != conf.tilew || tileh != conf.tileh
2356 then (
2357 wcmd "freetile %s" opaque;
2358 state.currently <- Idle;
2359 load state.layout;
2361 else (
2362 puttileopaque l col row gen cs angle opaque size t;
2363 state.memused <- state.memused + size;
2364 state.uioh#infochanged Memused;
2365 gctiles ();
2366 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2367 opaque, size) state.tilelru;
2369 let layout =
2370 match state.throttle with
2371 | None -> state.layout
2372 | Some (layout, _, _) -> layout
2375 state.currently <- Idle;
2376 if gen = state.gen
2377 && conf.colorspace = cs
2378 && conf.angle = angle
2379 && tilevisible layout l.pageno x y
2380 then conttiling l.pageno pageopaque;
2382 begin match state.throttle with
2383 | None ->
2384 preload state.layout;
2385 if gen = state.gen
2386 && conf.colorspace = cs
2387 && conf.angle = angle
2388 && tilevisible state.layout l.pageno x y
2389 then G.postRedisplay "tile nothrottle";
2391 | Some (layout, y, _) ->
2392 let ready = layoutready layout in
2393 if ready
2394 then (
2395 state.y <- y;
2396 state.layout <- layout;
2397 state.throttle <- None;
2398 G.postRedisplay "throttle";
2400 else load layout;
2401 end;
2404 | _ ->
2405 dolog "Inconsistent tiling state";
2406 logcurrently state.currently;
2407 exit 1
2410 | "pdim" ->
2411 let pdim =
2413 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2414 with exn ->
2415 dolog "error processing 'pdim' %S: %s"
2416 cmds (Printexc.to_string exn);
2417 exit 1;
2419 state.uioh#infochanged Pdim;
2420 state.pdims <- pdim :: state.pdims
2422 | "o" ->
2423 let (l, n, t, h, pos) =
2425 Scanf.sscanf args "%u %u %d %u %n"
2426 (fun l n t h pos -> l, n, t, h, pos)
2427 with exn ->
2428 dolog "error processing 'o' %S: %s"
2429 cmds (Printexc.to_string exn);
2430 exit 1;
2432 let s = String.sub args pos (String.length args - pos) in
2433 let outline = (s, l, (n, float t /. float h)) in
2434 begin match state.currently with
2435 | Outlining outlines ->
2436 state.currently <- Outlining (outline :: outlines)
2437 | Idle ->
2438 state.currently <- Outlining [outline]
2439 | currently ->
2440 dolog "invalid outlining state";
2441 logcurrently currently
2444 | "info" ->
2445 state.docinfo <- (1, args) :: state.docinfo
2447 | "infoend" ->
2448 state.uioh#infochanged Docinfo;
2449 state.docinfo <- List.rev state.docinfo
2451 | _ ->
2452 dolog "unknown cmd `%S'" cmds
2455 let onhist cb =
2456 let rc = cb.rc in
2457 let action = function
2458 | HCprev -> cbget cb ~-1
2459 | HCnext -> cbget cb 1
2460 | HCfirst -> cbget cb ~-(cb.rc)
2461 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2462 and cancel () = cb.rc <- rc
2463 in (action, cancel)
2466 let search pattern forward =
2467 if String.length pattern > 0
2468 then
2469 let pn, py =
2470 match state.layout with
2471 | [] -> 0, 0
2472 | l :: _ ->
2473 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2475 wcmd "search %d %d %d %d,%s\000"
2476 (btod conf.icase) pn py (btod forward) pattern;
2479 let intentry text key =
2480 let c =
2481 if key >= 32 && key < 127
2482 then Char.chr key
2483 else '\000'
2485 match c with
2486 | '0' .. '9' ->
2487 let text = addchar text c in
2488 TEcont text
2490 | _ ->
2491 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2492 TEcont text
2495 let linknentry text key =
2496 let c =
2497 if key >= 32 && key < 127
2498 then Char.chr key
2499 else '\000'
2501 match c with
2502 | 'a' .. 'z' ->
2503 let text = addchar text c in
2504 TEcont text
2506 | _ ->
2507 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2508 TEcont text
2511 let linkndone f s =
2512 if String.length s > 0
2513 then (
2514 let n =
2515 let l = String.length s in
2516 let rec loop pos n = if pos = l then n else
2517 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2518 loop (pos+1) (n*26 + m)
2519 in loop 0 0
2521 let rec loop n = function
2522 | [] -> ()
2523 | l :: rest ->
2524 match getopaque l.pageno with
2525 | None -> loop n rest
2526 | Some opaque ->
2527 let m = getlinkcount opaque in
2528 if n < m
2529 then (
2530 let under = getlink opaque n in
2531 f under
2533 else loop (n-m) rest
2535 loop n state.layout;
2539 let textentry text key =
2540 if key land 0xff00 = 0xff00
2541 then TEcont text
2542 else TEcont (text ^ Wsi.toutf8 key)
2545 let reqlayout angle proportional =
2546 match state.throttle with
2547 | None ->
2548 if nogeomcmds state.geomcmds
2549 then state.anchor <- getanchor ();
2550 conf.angle <- angle mod 360;
2551 if conf.angle != 0
2552 then (
2553 match state.mode with
2554 | LinkNav _ -> state.mode <- View
2555 | _ -> ()
2557 conf.proportional <- proportional;
2558 invalidate "reqlayout"
2559 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2560 | _ -> ()
2563 let settrim trimmargins trimfuzz =
2564 if nogeomcmds state.geomcmds
2565 then state.anchor <- getanchor ();
2566 conf.trimmargins <- trimmargins;
2567 conf.trimfuzz <- trimfuzz;
2568 let x0, y0, x1, y1 = trimfuzz in
2569 invalidate "settrim"
2570 (fun () ->
2571 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2572 Hashtbl.iter (fun _ opaque ->
2573 wcmd "freepage %s" opaque;
2574 ) state.pagemap;
2575 Hashtbl.clear state.pagemap;
2578 let setzoom zoom =
2579 match state.throttle with
2580 | None ->
2581 let zoom = max 0.01 zoom in
2582 if zoom <> conf.zoom
2583 then (
2584 state.prevzoom <- conf.zoom;
2585 conf.zoom <- zoom;
2586 reshape conf.winw conf.winh;
2587 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2590 | Some (layout, y, started) ->
2591 let time =
2592 match conf.maxwait with
2593 | None -> 0.0
2594 | Some t -> t
2596 let dt = now () -. started in
2597 if dt > time
2598 then (
2599 state.y <- y;
2600 load layout;
2604 let setcolumns mode columns coverA coverB =
2605 state.prevcolumns <- Some (conf.columns, conf.zoom);
2606 if columns < 0
2607 then (
2608 if isbirdseye mode
2609 then showtext '!' "split mode doesn't work in bird's eye"
2610 else (
2611 conf.columns <- Csplit (-columns, [||]);
2612 state.x <- 0;
2613 conf.zoom <- 1.0;
2616 else (
2617 if columns < 2
2618 then (
2619 conf.columns <- Csingle;
2620 state.x <- 0;
2621 setzoom 1.0;
2623 else (
2624 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2625 conf.zoom <- 1.0;
2628 reshape conf.winw conf.winh;
2631 let enterbirdseye () =
2632 let zoom = float conf.thumbw /. float conf.winw in
2633 let birdseyepageno =
2634 let cy = conf.winh / 2 in
2635 let fold = function
2636 | [] -> 0
2637 | l :: rest ->
2638 let rec fold best = function
2639 | [] -> best.pageno
2640 | l :: rest ->
2641 let d = cy - (l.pagedispy + l.pagevh/2)
2642 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2643 if abs d < abs dbest
2644 then fold l rest
2645 else best.pageno
2646 in fold l rest
2648 fold state.layout
2650 state.mode <- Birdseye (
2651 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2653 conf.zoom <- zoom;
2654 conf.presentation <- false;
2655 conf.interpagespace <- 10;
2656 conf.hlinks <- false;
2657 state.x <- 0;
2658 state.mstate <- Mnone;
2659 conf.maxwait <- None;
2660 conf.columns <- (
2661 match conf.beyecolumns with
2662 | Some c ->
2663 conf.zoom <- 1.0;
2664 Cmulti ((c, 0, 0), [||])
2665 | None -> Csingle
2667 Wsi.setcursor Wsi.CURSOR_INHERIT;
2668 if conf.verbose
2669 then
2670 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2671 (100.0*.zoom)
2672 else
2673 state.text <- ""
2675 reshape conf.winw conf.winh;
2678 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2679 state.mode <- View;
2680 conf.zoom <- c.zoom;
2681 conf.presentation <- c.presentation;
2682 conf.interpagespace <- c.interpagespace;
2683 conf.maxwait <- c.maxwait;
2684 conf.hlinks <- c.hlinks;
2685 conf.beyecolumns <- (
2686 match conf.columns with
2687 | Cmulti ((c, _, _), _) -> Some c
2688 | Csingle -> None
2689 | Csplit _ -> failwith "leaving bird's eye split mode"
2691 conf.columns <- (
2692 match c.columns with
2693 | Cmulti (c, _) -> Cmulti (c, [||])
2694 | Csingle -> Csingle
2695 | Csplit (c, _) -> Csplit (c, [||])
2697 state.x <- leftx;
2698 if conf.verbose
2699 then
2700 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2701 (100.0*.conf.zoom)
2703 reshape conf.winw conf.winh;
2704 state.anchor <- if goback then anchor else (pageno, 0.0);
2707 let togglebirdseye () =
2708 match state.mode with
2709 | Birdseye vals -> leavebirdseye vals true
2710 | View -> enterbirdseye ()
2711 | _ -> ()
2714 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2715 let pageno = max 0 (pageno - incr) in
2716 let rec loop = function
2717 | [] -> gotopage1 pageno 0
2718 | l :: _ when l.pageno = pageno ->
2719 if l.pagedispy >= 0 && l.pagey = 0
2720 then G.postRedisplay "upbirdseye"
2721 else gotopage1 pageno 0
2722 | _ :: rest -> loop rest
2724 loop state.layout;
2725 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2728 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2729 let pageno = min (state.pagecount - 1) (pageno + incr) in
2730 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2731 let rec loop = function
2732 | [] ->
2733 let y, h = getpageyh pageno in
2734 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2735 gotoy (clamp dy)
2736 | l :: _ when l.pageno = pageno ->
2737 if l.pagevh != l.pageh
2738 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2739 else G.postRedisplay "downbirdseye"
2740 | _ :: rest -> loop rest
2742 loop state.layout
2745 let optentry mode _ key =
2746 let btos b = if b then "on" else "off" in
2747 if key >= 32 && key < 127
2748 then
2749 let c = Char.chr key in
2750 match c with
2751 | 's' ->
2752 let ondone s =
2753 try conf.scrollstep <- int_of_string s with exc ->
2754 state.text <- Printf.sprintf "bad integer `%s': %s"
2755 s (Printexc.to_string exc)
2757 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2759 | 'A' ->
2760 let ondone s =
2762 conf.autoscrollstep <- int_of_string s;
2763 if state.autoscroll <> None
2764 then state.autoscroll <- Some conf.autoscrollstep
2765 with exc ->
2766 state.text <- Printf.sprintf "bad integer `%s': %s"
2767 s (Printexc.to_string exc)
2769 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2771 | 'C' ->
2772 let ondone s =
2774 let n, a, b = multicolumns_of_string s in
2775 setcolumns mode n a b;
2776 with exc ->
2777 state.text <- Printf.sprintf "bad columns `%s': %s"
2778 s (Printexc.to_string exc)
2780 TEswitch ("columns: ", "", None, textentry, ondone, true)
2782 | 'Z' ->
2783 let ondone s =
2785 let zoom = float (int_of_string s) /. 100.0 in
2786 setzoom zoom
2787 with exc ->
2788 state.text <- Printf.sprintf "bad integer `%s': %s"
2789 s (Printexc.to_string exc)
2791 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2793 | 't' ->
2794 let ondone s =
2796 conf.thumbw <- bound (int_of_string s) 2 4096;
2797 state.text <-
2798 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2799 begin match mode with
2800 | Birdseye beye ->
2801 leavebirdseye beye false;
2802 enterbirdseye ();
2803 | _ -> ();
2805 with exc ->
2806 state.text <- Printf.sprintf "bad integer `%s': %s"
2807 s (Printexc.to_string exc)
2809 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2811 | 'R' ->
2812 let ondone s =
2813 match try
2814 Some (int_of_string s)
2815 with exc ->
2816 state.text <- Printf.sprintf "bad integer `%s': %s"
2817 s (Printexc.to_string exc);
2818 None
2819 with
2820 | Some angle -> reqlayout angle conf.proportional
2821 | None -> ()
2823 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2825 | 'i' ->
2826 conf.icase <- not conf.icase;
2827 TEdone ("case insensitive search " ^ (btos conf.icase))
2829 | 'p' ->
2830 conf.preload <- not conf.preload;
2831 gotoy state.y;
2832 TEdone ("preload " ^ (btos conf.preload))
2834 | 'v' ->
2835 conf.verbose <- not conf.verbose;
2836 TEdone ("verbose " ^ (btos conf.verbose))
2838 | 'd' ->
2839 conf.debug <- not conf.debug;
2840 TEdone ("debug " ^ (btos conf.debug))
2842 | 'h' ->
2843 conf.maxhfit <- not conf.maxhfit;
2844 state.maxy <- calcheight ();
2845 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2847 | 'c' ->
2848 conf.crophack <- not conf.crophack;
2849 TEdone ("crophack " ^ btos conf.crophack)
2851 | 'a' ->
2852 let s =
2853 match conf.maxwait with
2854 | None ->
2855 conf.maxwait <- Some infinity;
2856 "always wait for page to complete"
2857 | Some _ ->
2858 conf.maxwait <- None;
2859 "show placeholder if page is not ready"
2861 TEdone s
2863 | 'f' ->
2864 conf.underinfo <- not conf.underinfo;
2865 TEdone ("underinfo " ^ btos conf.underinfo)
2867 | 'P' ->
2868 conf.savebmarks <- not conf.savebmarks;
2869 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2871 | 'S' ->
2872 let ondone s =
2874 let pageno, py =
2875 match state.layout with
2876 | [] -> 0, 0
2877 | l :: _ ->
2878 l.pageno, l.pagey
2880 conf.interpagespace <- int_of_string s;
2881 docolumns conf.columns;
2882 state.maxy <- calcheight ();
2883 let y = getpagey pageno in
2884 gotoy (y + py)
2885 with exc ->
2886 state.text <- Printf.sprintf "bad integer `%s': %s"
2887 s (Printexc.to_string exc)
2889 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2891 | 'l' ->
2892 reqlayout conf.angle (not conf.proportional);
2893 TEdone ("proportional display " ^ btos conf.proportional)
2895 | 'T' ->
2896 settrim (not conf.trimmargins) conf.trimfuzz;
2897 TEdone ("trim margins " ^ btos conf.trimmargins)
2899 | 'I' ->
2900 conf.invert <- not conf.invert;
2901 TEdone ("invert colors " ^ btos conf.invert)
2903 | 'x' ->
2904 let ondone s =
2905 cbput state.hists.sel s;
2906 conf.selcmd <- s;
2908 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2909 textentry, ondone, true)
2911 | _ ->
2912 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2913 TEstop
2914 else
2915 TEcont state.text
2918 class type lvsource = object
2919 method getitemcount : int
2920 method getitem : int -> (string * int)
2921 method hasaction : int -> bool
2922 method exit :
2923 uioh:uioh ->
2924 cancel:bool ->
2925 active:int ->
2926 first:int ->
2927 pan:int ->
2928 qsearch:string ->
2929 uioh option
2930 method getactive : int
2931 method getfirst : int
2932 method getqsearch : string
2933 method setqsearch : string -> unit
2934 method getpan : int
2935 end;;
2937 class virtual lvsourcebase = object
2938 val mutable m_active = 0
2939 val mutable m_first = 0
2940 val mutable m_qsearch = ""
2941 val mutable m_pan = 0
2942 method getactive = m_active
2943 method getfirst = m_first
2944 method getqsearch = m_qsearch
2945 method getpan = m_pan
2946 method setqsearch s = m_qsearch <- s
2947 end;;
2949 let withoutlastutf8 s =
2950 let len = String.length s in
2951 if len = 0
2952 then s
2953 else
2954 let rec find pos =
2955 if pos = 0
2956 then pos
2957 else
2958 let b = Char.code s.[pos] in
2959 if b land 0b110000 = 0b11000000
2960 then find (pos-1)
2961 else pos-1
2963 let first =
2964 if Char.code s.[len-1] land 0x80 = 0
2965 then len-1
2966 else find (len-1)
2968 String.sub s 0 first;
2971 let textentrykeyboard
2972 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2973 let enttext te =
2974 state.mode <- Textentry (te, onleave);
2975 state.text <- "";
2976 enttext ();
2977 G.postRedisplay "textentrykeyboard enttext";
2979 let histaction cmd =
2980 match opthist with
2981 | None -> ()
2982 | Some (action, _) ->
2983 state.mode <- Textentry (
2984 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2986 G.postRedisplay "textentry histaction"
2988 match key with
2989 | 0xff08 -> (* backspace *)
2990 let s = withoutlastutf8 text in
2991 let len = String.length s in
2992 if cancelonempty && len = 0
2993 then (
2994 onleave Cancel;
2995 G.postRedisplay "textentrykeyboard after cancel";
2997 else (
2998 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3001 | 0xff0d ->
3002 ondone text;
3003 onleave Confirm;
3004 G.postRedisplay "textentrykeyboard after confirm"
3006 | 0xff52 -> histaction HCprev
3007 | 0xff54 -> histaction HCnext
3008 | 0xff50 -> histaction HCfirst
3009 | 0xff57 -> histaction HClast
3011 | 0xff1b -> (* escape*)
3012 if String.length text = 0
3013 then (
3014 begin match opthist with
3015 | None -> ()
3016 | Some (_, onhistcancel) -> onhistcancel ()
3017 end;
3018 onleave Cancel;
3019 state.text <- "";
3020 G.postRedisplay "textentrykeyboard after cancel2"
3022 else (
3023 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3026 | 0xff9f | 0xffff -> () (* delete *)
3028 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3029 begin match onkey text key with
3030 | TEdone text ->
3031 ondone text;
3032 onleave Confirm;
3033 G.postRedisplay "textentrykeyboard after confirm2";
3035 | TEcont text ->
3036 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3038 | TEstop ->
3039 onleave Cancel;
3040 G.postRedisplay "textentrykeyboard after cancel3"
3042 | TEswitch te ->
3043 state.mode <- Textentry (te, onleave);
3044 G.postRedisplay "textentrykeyboard switch";
3045 end;
3047 | _ ->
3048 vlog "unhandled key %s" (Wsi.keyname key)
3051 let firstof first active =
3052 if first > active || abs (first - active) > fstate.maxrows - 1
3053 then max 0 (active - (fstate.maxrows/2))
3054 else first
3057 let calcfirst first active =
3058 if active > first
3059 then
3060 let rows = active - first in
3061 if rows > fstate.maxrows then active - fstate.maxrows else first
3062 else active
3065 let scrollph y maxy =
3066 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3067 let sh = float conf.winh /. sh in
3068 let sh = max sh (float conf.scrollh) in
3070 let percent =
3071 if y = state.maxy
3072 then 1.0
3073 else float y /. float maxy
3075 let position = (float conf.winh -. sh) *. percent in
3077 let position =
3078 if position +. sh > float conf.winh
3079 then float conf.winh -. sh
3080 else position
3082 position, sh;
3085 let coe s = (s :> uioh);;
3087 class listview ~(source:lvsource) ~trusted ~modehash =
3088 object (self)
3089 val m_pan = source#getpan
3090 val m_first = source#getfirst
3091 val m_active = source#getactive
3092 val m_qsearch = source#getqsearch
3093 val m_prev_uioh = state.uioh
3095 method private elemunder y =
3096 let n = y / (fstate.fontsize+1) in
3097 if m_first + n < source#getitemcount
3098 then (
3099 if source#hasaction (m_first + n)
3100 then Some (m_first + n)
3101 else None
3103 else None
3105 method display =
3106 Gl.enable `blend;
3107 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3108 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3109 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3110 GlDraw.color (1., 1., 1.);
3111 Gl.enable `texture_2d;
3112 let fs = fstate.fontsize in
3113 let nfs = fs + 1 in
3114 let ww = fstate.wwidth in
3115 let tabw = 30.0*.ww in
3116 let itemcount = source#getitemcount in
3117 let rec loop row =
3118 if (row - m_first) * nfs > conf.winh
3119 then ()
3120 else (
3121 if row >= 0 && row < itemcount
3122 then (
3123 let (s, level) = source#getitem row in
3124 let y = (row - m_first) * nfs in
3125 let x = 5.0 +. float (level + m_pan) *. ww in
3126 if row = m_active
3127 then (
3128 Gl.disable `texture_2d;
3129 GlDraw.polygon_mode `both `line;
3130 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3131 GlDraw.rect (1., float (y + 1))
3132 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3133 GlDraw.polygon_mode `both `fill;
3134 GlDraw.color (1., 1., 1.);
3135 Gl.enable `texture_2d;
3138 let drawtabularstring s =
3139 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3140 if trusted
3141 then
3142 let tabpos = try String.index s '\t' with Not_found -> -1 in
3143 if tabpos > 0
3144 then
3145 let len = String.length s - tabpos - 1 in
3146 let s1 = String.sub s 0 tabpos
3147 and s2 = String.sub s (tabpos + 1) len in
3148 let nx = drawstr x s1 in
3149 let sw = nx -. x in
3150 let x = x +. (max tabw sw) in
3151 drawstr x s2
3152 else
3153 drawstr x s
3154 else
3155 drawstr x s
3157 let _ = drawtabularstring s in
3158 loop (row+1)
3162 loop m_first;
3163 Gl.disable `blend;
3164 Gl.disable `texture_2d;
3166 method updownlevel incr =
3167 let len = source#getitemcount in
3168 let curlevel =
3169 if m_active >= 0 && m_active < len
3170 then snd (source#getitem m_active)
3171 else -1
3173 let rec flow i =
3174 if i = len then i-1 else if i = -1 then 0 else
3175 let _, l = source#getitem i in
3176 if l != curlevel then i else flow (i+incr)
3178 let active = flow m_active in
3179 let first = calcfirst m_first active in
3180 G.postRedisplay "outline updownlevel";
3181 {< m_active = active; m_first = first >}
3183 method private key1 key mask =
3184 let set1 active first qsearch =
3185 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3187 let search active pattern incr =
3188 let dosearch re =
3189 let rec loop n =
3190 if n >= 0 && n < source#getitemcount
3191 then (
3192 let s, _ = source#getitem n in
3194 (try ignore (Str.search_forward re s 0); true
3195 with Not_found -> false)
3196 then Some n
3197 else loop (n + incr)
3199 else None
3201 loop active
3204 let re = Str.regexp_case_fold pattern in
3205 dosearch re
3206 with Failure s ->
3207 state.text <- s;
3208 None
3210 let itemcount = source#getitemcount in
3211 let find start incr =
3212 let rec find i =
3213 if i = -1 || i = itemcount
3214 then -1
3215 else (
3216 if source#hasaction i
3217 then i
3218 else find (i + incr)
3221 find start
3223 let set active first =
3224 let first = bound first 0 (itemcount - fstate.maxrows) in
3225 state.text <- "";
3226 coe {< m_active = active; m_first = first >}
3228 let navigate incr =
3229 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3230 let active, first =
3231 let incr1 = if incr > 0 then 1 else -1 in
3232 if isvisible m_first m_active
3233 then
3234 let next =
3235 let next = m_active + incr in
3236 let next =
3237 if next < 0 || next >= itemcount
3238 then -1
3239 else find next incr1
3241 if next = -1 || abs (m_active - next) > fstate.maxrows
3242 then -1
3243 else next
3245 if next = -1
3246 then
3247 let first = m_first + incr in
3248 let first = bound first 0 (itemcount - 1) in
3249 let next =
3250 let next = m_active + incr in
3251 let next = bound next 0 (itemcount - 1) in
3252 find next ~-incr1
3254 let active = if next = -1 then m_active else next in
3255 active, first
3256 else
3257 let first = min next m_first in
3258 let first =
3259 if abs (next - first) > fstate.maxrows
3260 then first + incr
3261 else first
3263 next, first
3264 else
3265 let first = m_first + incr in
3266 let first = bound first 0 (itemcount - 1) in
3267 let active =
3268 let next = m_active + incr in
3269 let next = bound next 0 (itemcount - 1) in
3270 let next = find next incr1 in
3271 let active =
3272 if next = -1 || abs (m_active - first) > fstate.maxrows
3273 then (
3274 let active = if m_active = -1 then next else m_active in
3275 active
3277 else next
3279 if isvisible first active
3280 then active
3281 else -1
3283 active, first
3285 G.postRedisplay "listview navigate";
3286 set active first;
3288 match key with
3289 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3290 let incr = if key = 0x72 then -1 else 1 in
3291 let active, first =
3292 match search (m_active + incr) m_qsearch incr with
3293 | None ->
3294 state.text <- m_qsearch ^ " [not found]";
3295 m_active, m_first
3296 | Some active ->
3297 state.text <- m_qsearch;
3298 active, firstof m_first active
3300 G.postRedisplay "listview ctrl-r/s";
3301 set1 active first m_qsearch;
3303 | 0xff08 -> (* backspace *)
3304 if String.length m_qsearch = 0
3305 then coe self
3306 else (
3307 let qsearch = withoutlastutf8 m_qsearch in
3308 let len = String.length qsearch in
3309 if len = 0
3310 then (
3311 state.text <- "";
3312 G.postRedisplay "listview empty qsearch";
3313 set1 m_active m_first "";
3315 else
3316 let active, first =
3317 match search m_active qsearch ~-1 with
3318 | None ->
3319 state.text <- qsearch ^ " [not found]";
3320 m_active, m_first
3321 | Some active ->
3322 state.text <- qsearch;
3323 active, firstof m_first active
3325 G.postRedisplay "listview backspace qsearch";
3326 set1 active first qsearch
3329 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3330 let pattern = m_qsearch ^ Wsi.toutf8 key in
3331 let active, first =
3332 match search m_active pattern 1 with
3333 | None ->
3334 state.text <- pattern ^ " [not found]";
3335 m_active, m_first
3336 | Some active ->
3337 state.text <- pattern;
3338 active, firstof m_first active
3340 G.postRedisplay "listview qsearch add";
3341 set1 active first pattern;
3343 | 0xff1b -> (* escape *)
3344 state.text <- "";
3345 if String.length m_qsearch = 0
3346 then (
3347 G.postRedisplay "list view escape";
3348 begin
3349 match
3350 source#exit (coe self) true m_active m_first m_pan m_qsearch
3351 with
3352 | None -> m_prev_uioh
3353 | Some uioh -> uioh
3356 else (
3357 G.postRedisplay "list view kill qsearch";
3358 source#setqsearch "";
3359 coe {< m_qsearch = "" >}
3362 | 0xff0d -> (* return *)
3363 state.text <- "";
3364 let self = {< m_qsearch = "" >} in
3365 source#setqsearch "";
3366 let opt =
3367 G.postRedisplay "listview enter";
3368 if m_active >= 0 && m_active < source#getitemcount
3369 then (
3370 source#exit (coe self) false m_active m_first m_pan "";
3372 else (
3373 source#exit (coe self) true m_active m_first m_pan "";
3376 begin match opt with
3377 | None -> m_prev_uioh
3378 | Some uioh -> uioh
3381 | 0xff9f | 0xffff -> (* delete *)
3382 coe self
3384 | 0xff52 -> navigate ~-1 (* up *)
3385 | 0xff54 -> navigate 1 (* down *)
3386 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3387 | 0xff56 -> navigate fstate.maxrows (* next *)
3389 | 0xff53 -> (* right *)
3390 state.text <- "";
3391 G.postRedisplay "listview right";
3392 coe {< m_pan = m_pan - 1 >}
3394 | 0xff51 -> (* left *)
3395 state.text <- "";
3396 G.postRedisplay "listview left";
3397 coe {< m_pan = m_pan + 1 >}
3399 | 0xff50 -> (* home *)
3400 let active = find 0 1 in
3401 G.postRedisplay "listview home";
3402 set active 0;
3404 | 0xff57 -> (* end *)
3405 let first = max 0 (itemcount - fstate.maxrows) in
3406 let active = find (itemcount - 1) ~-1 in
3407 G.postRedisplay "listview end";
3408 set active first;
3410 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3411 coe self
3413 | _ ->
3414 dolog "listview unknown key %#x" key; coe self
3416 method key key mask =
3417 match state.mode with
3418 | Textentry te -> textentrykeyboard key mask te; coe self
3419 | _ -> self#key1 key mask
3421 method button button down x y _ =
3422 let opt =
3423 match button with
3424 | 1 when x > conf.winw - conf.scrollbw ->
3425 G.postRedisplay "listview scroll";
3426 if down
3427 then
3428 let _, position, sh = self#scrollph in
3429 if y > truncate position && y < truncate (position +. sh)
3430 then (
3431 state.mstate <- Mscrolly;
3432 Some (coe self)
3434 else
3435 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3436 let first = truncate (s *. float source#getitemcount) in
3437 let first = min source#getitemcount first in
3438 Some (coe {< m_first = first; m_active = first >})
3439 else (
3440 state.mstate <- Mnone;
3441 Some (coe self);
3443 | 1 when not down ->
3444 begin match self#elemunder y with
3445 | Some n ->
3446 G.postRedisplay "listview click";
3447 source#exit
3448 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3449 | _ ->
3450 Some (coe self)
3452 | n when (n == 4 || n == 5) && not down ->
3453 let len = source#getitemcount in
3454 let first =
3455 if n = 5 && m_first + fstate.maxrows >= len
3456 then
3457 m_first
3458 else
3459 let first = m_first + (if n == 4 then -1 else 1) in
3460 bound first 0 (len - 1)
3462 G.postRedisplay "listview wheel";
3463 Some (coe {< m_first = first >})
3464 | n when (n = 6 || n = 7) && not down ->
3465 let inc = m_first + (if n = 7 then -1 else 1) in
3466 G.postRedisplay "listview hwheel";
3467 Some (coe {< m_pan = m_pan + inc >})
3468 | _ ->
3469 Some (coe self)
3471 match opt with
3472 | None -> m_prev_uioh
3473 | Some uioh -> uioh
3475 method motion _ y =
3476 match state.mstate with
3477 | Mscrolly ->
3478 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3479 let first = truncate (s *. float source#getitemcount) in
3480 let first = min source#getitemcount first in
3481 G.postRedisplay "listview motion";
3482 coe {< m_first = first; m_active = first >}
3483 | _ -> coe self
3485 method pmotion x y =
3486 if x < conf.winw - conf.scrollbw
3487 then
3488 let n =
3489 match self#elemunder y with
3490 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3491 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3493 let o =
3494 if n != m_active
3495 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3496 else self
3498 coe o
3499 else (
3500 Wsi.setcursor Wsi.CURSOR_INHERIT;
3501 coe self
3504 method infochanged _ = ()
3506 method scrollpw = (0, 0.0, 0.0)
3507 method scrollph =
3508 let nfs = fstate.fontsize + 1 in
3509 let y = m_first * nfs in
3510 let itemcount = source#getitemcount in
3511 let maxi = max 0 (itemcount - fstate.maxrows) in
3512 let maxy = maxi * nfs in
3513 let p, h = scrollph y maxy in
3514 conf.scrollbw, p, h
3516 method modehash = modehash
3517 end;;
3519 class outlinelistview ~source =
3520 object (self)
3521 inherit listview
3522 ~source:(source :> lvsource)
3523 ~trusted:false
3524 ~modehash:(findkeyhash conf "outline")
3525 as super
3527 method key key mask =
3528 let calcfirst first active =
3529 if active > first
3530 then
3531 let rows = active - first in
3532 let maxrows =
3533 if String.length state.text = 0
3534 then fstate.maxrows
3535 else fstate.maxrows - 2
3537 if rows > maxrows then active - maxrows else first
3538 else active
3540 let navigate incr =
3541 let active = m_active + incr in
3542 let active = bound active 0 (source#getitemcount - 1) in
3543 let first = calcfirst m_first active in
3544 G.postRedisplay "outline navigate";
3545 coe {< m_active = active; m_first = first >}
3547 let ctrl = Wsi.withctrl mask in
3548 match key with
3549 | 110 when ctrl -> (* ctrl-n *)
3550 source#narrow m_qsearch;
3551 G.postRedisplay "outline ctrl-n";
3552 coe {< m_first = 0; m_active = 0 >}
3554 | 117 when ctrl -> (* ctrl-u *)
3555 source#denarrow;
3556 G.postRedisplay "outline ctrl-u";
3557 state.text <- "";
3558 coe {< m_first = 0; m_active = 0 >}
3560 | 108 when ctrl -> (* ctrl-l *)
3561 let first = m_active - (fstate.maxrows / 2) in
3562 G.postRedisplay "outline ctrl-l";
3563 coe {< m_first = first >}
3565 | 0xff9f | 0xffff -> (* delete *)
3566 source#remove m_active;
3567 G.postRedisplay "outline delete";
3568 let active = max 0 (m_active-1) in
3569 coe {< m_first = firstof m_first active;
3570 m_active = active >}
3572 | 0xff52 -> navigate ~-1 (* up *)
3573 | 0xff54 -> navigate 1 (* down *)
3574 | 0xff55 -> (* prior *)
3575 navigate ~-(fstate.maxrows)
3576 | 0xff56 -> (* next *)
3577 navigate fstate.maxrows
3579 | 0xff53 -> (* [ctrl-]right *)
3580 let o =
3581 if ctrl
3582 then (
3583 G.postRedisplay "outline ctrl right";
3584 {< m_pan = m_pan + 1 >}
3586 else self#updownlevel 1
3588 coe o
3590 | 0xff51 -> (* [ctrl-]left *)
3591 let o =
3592 if ctrl
3593 then (
3594 G.postRedisplay "outline ctrl left";
3595 {< m_pan = m_pan - 1 >}
3597 else self#updownlevel ~-1
3599 coe o
3601 | 0xff50 -> (* home *)
3602 G.postRedisplay "outline home";
3603 coe {< m_first = 0; m_active = 0 >}
3605 | 0xff57 -> (* end *)
3606 let active = source#getitemcount - 1 in
3607 let first = max 0 (active - fstate.maxrows) in
3608 G.postRedisplay "outline end";
3609 coe {< m_active = active; m_first = first >}
3611 | _ -> super#key key mask
3614 let outlinesource usebookmarks =
3615 let empty = [||] in
3616 (object
3617 inherit lvsourcebase
3618 val mutable m_items = empty
3619 val mutable m_orig_items = empty
3620 val mutable m_prev_items = empty
3621 val mutable m_narrow_pattern = ""
3622 val mutable m_hadremovals = false
3624 method getitemcount =
3625 Array.length m_items + (if m_hadremovals then 1 else 0)
3627 method getitem n =
3628 if n == Array.length m_items && m_hadremovals
3629 then
3630 ("[Confirm removal]", 0)
3631 else
3632 let s, n, _ = m_items.(n) in
3633 (s, n)
3635 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3636 ignore (uioh, first, qsearch);
3637 let confrimremoval = m_hadremovals && active = Array.length m_items in
3638 let items =
3639 if String.length m_narrow_pattern = 0
3640 then m_orig_items
3641 else m_items
3643 if not cancel
3644 then (
3645 if not confrimremoval
3646 then(
3647 let _, _, anchor = m_items.(active) in
3648 gotoanchor anchor;
3649 m_items <- items;
3651 else (
3652 state.bookmarks <- Array.to_list m_items;
3653 m_orig_items <- m_items;
3656 else m_items <- items;
3657 m_pan <- pan;
3658 None
3660 method hasaction _ = true
3662 method greetmsg =
3663 if Array.length m_items != Array.length m_orig_items
3664 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3665 else ""
3667 method narrow pattern =
3668 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3669 match reopt with
3670 | None -> ()
3671 | Some re ->
3672 let rec loop accu n =
3673 if n = -1
3674 then (
3675 m_narrow_pattern <- pattern;
3676 m_items <- Array.of_list accu
3678 else
3679 let (s, _, _) as o = m_items.(n) in
3680 let accu =
3681 if (try ignore (Str.search_forward re s 0); true
3682 with Not_found -> false)
3683 then o :: accu
3684 else accu
3686 loop accu (n-1)
3688 loop [] (Array.length m_items - 1)
3690 method denarrow =
3691 m_orig_items <- (
3692 if usebookmarks
3693 then Array.of_list state.bookmarks
3694 else state.outlines
3696 m_items <- m_orig_items
3698 method remove m =
3699 if usebookmarks
3700 then
3701 if m >= 0 && m < Array.length m_items
3702 then (
3703 m_hadremovals <- true;
3704 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3705 let n = if n >= m then n+1 else n in
3706 m_items.(n)
3710 method reset anchor items =
3711 m_hadremovals <- false;
3712 if m_orig_items == empty || m_prev_items != items
3713 then (
3714 m_orig_items <- items;
3715 if String.length m_narrow_pattern = 0
3716 then m_items <- items;
3718 m_prev_items <- items;
3719 let rely = getanchory anchor in
3720 let active =
3721 let rec loop n best bestd =
3722 if n = Array.length m_items
3723 then best
3724 else
3725 let (_, _, anchor) = m_items.(n) in
3726 let orely = getanchory anchor in
3727 let d = abs (orely - rely) in
3728 if d < bestd
3729 then loop (n+1) n d
3730 else loop (n+1) best bestd
3732 loop 0 ~-1 max_int
3734 m_active <- active;
3735 m_first <- firstof m_first active
3736 end)
3739 let enterselector usebookmarks =
3740 let source = outlinesource usebookmarks in
3741 fun errmsg ->
3742 let outlines =
3743 if usebookmarks
3744 then Array.of_list state.bookmarks
3745 else state.outlines
3747 if Array.length outlines = 0
3748 then (
3749 showtext ' ' errmsg;
3751 else (
3752 state.text <- source#greetmsg;
3753 Wsi.setcursor Wsi.CURSOR_INHERIT;
3754 let anchor = getanchor () in
3755 source#reset anchor outlines;
3756 state.uioh <- coe (new outlinelistview ~source);
3757 G.postRedisplay "enter selector";
3761 let enteroutlinemode =
3762 let f = enterselector false in
3763 fun ()-> f "Document has no outline";
3766 let enterbookmarkmode =
3767 let f = enterselector true in
3768 fun () -> f "Document has no bookmarks (yet)";
3771 let color_of_string s =
3772 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3773 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3777 let color_to_string (r, g, b) =
3778 let r = truncate (r *. 256.0)
3779 and g = truncate (g *. 256.0)
3780 and b = truncate (b *. 256.0) in
3781 Printf.sprintf "%d/%d/%d" r g b
3784 let irect_of_string s =
3785 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3788 let irect_to_string (x0,y0,x1,y1) =
3789 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3792 let makecheckers () =
3793 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3794 following to say:
3795 converted by Issac Trotts. July 25, 2002 *)
3796 let image_height = 64
3797 and image_width = 64 in
3799 let make_image () =
3800 let image =
3801 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3803 for i = 0 to image_width - 1 do
3804 for j = 0 to image_height - 1 do
3805 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3806 (if (i land 8 ) lxor (j land 8) = 0
3807 then [|255;255;255|] else [|200;200;200|])
3808 done
3809 done;
3810 image
3812 let image = make_image () in
3813 let id = GlTex.gen_texture () in
3814 GlTex.bind_texture `texture_2d id;
3815 GlPix.store (`unpack_alignment 1);
3816 GlTex.image2d image;
3817 List.iter (GlTex.parameter ~target:`texture_2d)
3818 [ `wrap_s `repeat;
3819 `wrap_t `repeat;
3820 `mag_filter `nearest;
3821 `min_filter `nearest ];
3825 let setcheckers enabled =
3826 match state.texid with
3827 | None ->
3828 if enabled then state.texid <- Some (makecheckers ())
3830 | Some texid ->
3831 if not enabled
3832 then (
3833 GlTex.delete_texture texid;
3834 state.texid <- None;
3838 let int_of_string_with_suffix s =
3839 let l = String.length s in
3840 let s1, shift =
3841 if l > 1
3842 then
3843 let suffix = Char.lowercase s.[l-1] in
3844 match suffix with
3845 | 'k' -> String.sub s 0 (l-1), 10
3846 | 'm' -> String.sub s 0 (l-1), 20
3847 | 'g' -> String.sub s 0 (l-1), 30
3848 | _ -> s, 0
3849 else s, 0
3851 let n = int_of_string s1 in
3852 let m = n lsl shift in
3853 if m < 0 || m < n
3854 then raise (Failure "value too large")
3855 else m
3858 let string_with_suffix_of_int n =
3859 if n = 0
3860 then "0"
3861 else
3862 let n, s =
3863 if n land ((1 lsl 20) - 1) = 0
3864 then n lsr 20, "M"
3865 else (
3866 if n land ((1 lsl 10) - 1) = 0
3867 then n lsr 10, "K"
3868 else n, ""
3871 let rec loop s n =
3872 let h = n mod 1000 in
3873 let n = n / 1000 in
3874 if n = 0
3875 then string_of_int h ^ s
3876 else (
3877 let s = Printf.sprintf "_%03d%s" h s in
3878 loop s n
3881 loop "" n ^ s;
3884 let defghyllscroll = (40, 8, 32);;
3885 let ghyllscroll_of_string s =
3886 let (n, a, b) as nab =
3887 if s = "default"
3888 then defghyllscroll
3889 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3891 if n <= a || n <= b || a >= b
3892 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3893 nab;
3896 let ghyllscroll_to_string ((n, a, b) as nab) =
3897 if nab = defghyllscroll
3898 then "default"
3899 else Printf.sprintf "%d,%d,%d" n a b;
3902 let describe_location () =
3903 let f (fn, _) l =
3904 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3906 let fn, ln = List.fold_left f (-1, -1) state.layout in
3907 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3908 let percent =
3909 if maxy <= 0
3910 then 100.
3911 else (100. *. (float state.y /. float maxy))
3913 if fn = ln
3914 then
3915 Printf.sprintf "page %d of %d [%.2f%%]"
3916 (fn+1) state.pagecount percent
3917 else
3918 Printf.sprintf
3919 "pages %d-%d of %d [%.2f%%]"
3920 (fn+1) (ln+1) state.pagecount percent
3923 let enterinfomode =
3924 let btos b = if b then "\xe2\x88\x9a" else "" in
3925 let showextended = ref false in
3926 let leave mode = function
3927 | Confirm -> state.mode <- mode
3928 | Cancel -> state.mode <- mode in
3929 let src =
3930 (object
3931 val mutable m_first_time = true
3932 val mutable m_l = []
3933 val mutable m_a = [||]
3934 val mutable m_prev_uioh = nouioh
3935 val mutable m_prev_mode = View
3937 inherit lvsourcebase
3939 method reset prev_mode prev_uioh =
3940 m_a <- Array.of_list (List.rev m_l);
3941 m_l <- [];
3942 m_prev_mode <- prev_mode;
3943 m_prev_uioh <- prev_uioh;
3944 if m_first_time
3945 then (
3946 let rec loop n =
3947 if n >= Array.length m_a
3948 then ()
3949 else
3950 match m_a.(n) with
3951 | _, _, _, Action _ -> m_active <- n
3952 | _ -> loop (n+1)
3954 loop 0;
3955 m_first_time <- false;
3958 method int name get set =
3959 m_l <-
3960 (name, `int get, 1, Action (
3961 fun u ->
3962 let ondone s =
3963 try set (int_of_string s)
3964 with exn ->
3965 state.text <- Printf.sprintf "bad integer `%s': %s"
3966 s (Printexc.to_string exn)
3968 state.text <- "";
3969 let te = name ^ ": ", "", None, intentry, ondone, true in
3970 state.mode <- Textentry (te, leave m_prev_mode);
3972 )) :: m_l
3974 method int_with_suffix name get set =
3975 m_l <-
3976 (name, `intws get, 1, Action (
3977 fun u ->
3978 let ondone s =
3979 try set (int_of_string_with_suffix s)
3980 with exn ->
3981 state.text <- Printf.sprintf "bad integer `%s': %s"
3982 s (Printexc.to_string exn)
3984 state.text <- "";
3985 let te =
3986 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3988 state.mode <- Textentry (te, leave m_prev_mode);
3990 )) :: m_l
3992 method bool ?(offset=1) ?(btos=btos) name get set =
3993 m_l <-
3994 (name, `bool (btos, get), offset, Action (
3995 fun u ->
3996 let v = get () in
3997 set (not v);
3999 )) :: m_l
4001 method color name get set =
4002 m_l <-
4003 (name, `color get, 1, Action (
4004 fun u ->
4005 let invalid = (nan, nan, nan) in
4006 let ondone s =
4007 let c =
4008 try color_of_string s
4009 with exn ->
4010 state.text <- Printf.sprintf "bad color `%s': %s"
4011 s (Printexc.to_string exn);
4012 invalid
4014 if c <> invalid
4015 then set c;
4017 let te = name ^ ": ", "", None, textentry, ondone, true in
4018 state.text <- color_to_string (get ());
4019 state.mode <- Textentry (te, leave m_prev_mode);
4021 )) :: m_l
4023 method string name get set =
4024 m_l <-
4025 (name, `string get, 1, Action (
4026 fun u ->
4027 let ondone s = set s in
4028 let te = name ^ ": ", "", None, textentry, ondone, true in
4029 state.mode <- Textentry (te, leave m_prev_mode);
4031 )) :: m_l
4033 method colorspace name get set =
4034 m_l <-
4035 (name, `string get, 1, Action (
4036 fun _ ->
4037 let source =
4038 let vals = [| "rgb"; "bgr"; "gray" |] in
4039 (object
4040 inherit lvsourcebase
4042 initializer
4043 m_active <- int_of_colorspace conf.colorspace;
4044 m_first <- 0;
4046 method getitemcount = Array.length vals
4047 method getitem n = (vals.(n), 0)
4048 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4049 ignore (uioh, first, pan, qsearch);
4050 if not cancel then set active;
4051 None
4052 method hasaction _ = true
4053 end)
4055 state.text <- "";
4056 let modehash = findkeyhash conf "info" in
4057 coe (new listview ~source ~trusted:true ~modehash)
4058 )) :: m_l
4060 method caption s offset =
4061 m_l <- (s, `empty, offset, Noaction) :: m_l
4063 method caption2 s f offset =
4064 m_l <- (s, `string f, offset, Noaction) :: m_l
4066 method getitemcount = Array.length m_a
4068 method getitem n =
4069 let tostr = function
4070 | `int f -> string_of_int (f ())
4071 | `intws f -> string_with_suffix_of_int (f ())
4072 | `string f -> f ()
4073 | `color f -> color_to_string (f ())
4074 | `bool (btos, f) -> btos (f ())
4075 | `empty -> ""
4077 let name, t, offset, _ = m_a.(n) in
4078 ((let s = tostr t in
4079 if String.length s > 0
4080 then Printf.sprintf "%s\t%s" name s
4081 else name),
4082 offset)
4084 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4085 let uiohopt =
4086 if not cancel
4087 then (
4088 m_qsearch <- qsearch;
4089 let uioh =
4090 match m_a.(active) with
4091 | _, _, _, Action f -> f uioh
4092 | _ -> uioh
4094 Some uioh
4096 else None
4098 m_active <- active;
4099 m_first <- first;
4100 m_pan <- pan;
4101 uiohopt
4103 method hasaction n =
4104 match m_a.(n) with
4105 | _, _, _, Action _ -> true
4106 | _ -> false
4107 end)
4109 let rec fillsrc prevmode prevuioh =
4110 let sep () = src#caption "" 0 in
4111 let colorp name get set =
4112 src#string name
4113 (fun () -> color_to_string (get ()))
4114 (fun v ->
4116 let c = color_of_string v in
4117 set c
4118 with exn ->
4119 state.text <- Printf.sprintf "bad color `%s': %s"
4120 v (Printexc.to_string exn);
4123 let oldmode = state.mode in
4124 let birdseye = isbirdseye state.mode in
4126 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4128 src#bool "presentation mode"
4129 (fun () -> conf.presentation)
4130 (fun v ->
4131 conf.presentation <- v;
4132 state.anchor <- getanchor ();
4133 represent ());
4135 src#bool "ignore case in searches"
4136 (fun () -> conf.icase)
4137 (fun v -> conf.icase <- v);
4139 src#bool "preload"
4140 (fun () -> conf.preload)
4141 (fun v -> conf.preload <- v);
4143 src#bool "highlight links"
4144 (fun () -> conf.hlinks)
4145 (fun v -> conf.hlinks <- v);
4147 src#bool "under info"
4148 (fun () -> conf.underinfo)
4149 (fun v -> conf.underinfo <- v);
4151 src#bool "persistent bookmarks"
4152 (fun () -> conf.savebmarks)
4153 (fun v -> conf.savebmarks <- v);
4155 src#bool "proportional display"
4156 (fun () -> conf.proportional)
4157 (fun v -> reqlayout conf.angle v);
4159 src#bool "trim margins"
4160 (fun () -> conf.trimmargins)
4161 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4163 src#bool "persistent location"
4164 (fun () -> conf.jumpback)
4165 (fun v -> conf.jumpback <- v);
4167 sep ();
4168 src#int "inter-page space"
4169 (fun () -> conf.interpagespace)
4170 (fun n ->
4171 conf.interpagespace <- n;
4172 docolumns conf.columns;
4173 let pageno, py =
4174 match state.layout with
4175 | [] -> 0, 0
4176 | l :: _ ->
4177 l.pageno, l.pagey
4179 state.maxy <- calcheight ();
4180 let y = getpagey pageno in
4181 gotoy (y + py)
4184 src#int "page bias"
4185 (fun () -> conf.pagebias)
4186 (fun v -> conf.pagebias <- v);
4188 src#int "scroll step"
4189 (fun () -> conf.scrollstep)
4190 (fun n -> conf.scrollstep <- n);
4192 src#int "horizontal scroll step"
4193 (fun () -> conf.hscrollstep)
4194 (fun v -> conf.hscrollstep <- v);
4196 src#int "auto scroll step"
4197 (fun () ->
4198 match state.autoscroll with
4199 | Some step -> step
4200 | _ -> conf.autoscrollstep)
4201 (fun n ->
4202 if state.autoscroll <> None
4203 then state.autoscroll <- Some n;
4204 conf.autoscrollstep <- n);
4206 src#int "zoom"
4207 (fun () -> truncate (conf.zoom *. 100.))
4208 (fun v -> setzoom ((float v) /. 100.));
4210 src#int "rotation"
4211 (fun () -> conf.angle)
4212 (fun v -> reqlayout v conf.proportional);
4214 src#int "scroll bar width"
4215 (fun () -> state.scrollw)
4216 (fun v ->
4217 state.scrollw <- v;
4218 conf.scrollbw <- v;
4219 reshape conf.winw conf.winh;
4222 src#int "scroll handle height"
4223 (fun () -> conf.scrollh)
4224 (fun v -> conf.scrollh <- v;);
4226 src#int "thumbnail width"
4227 (fun () -> conf.thumbw)
4228 (fun v ->
4229 conf.thumbw <- min 4096 v;
4230 match oldmode with
4231 | Birdseye beye ->
4232 leavebirdseye beye false;
4233 enterbirdseye ()
4234 | _ -> ()
4237 let mode = state.mode in
4238 src#string "columns"
4239 (fun () ->
4240 match conf.columns with
4241 | Csingle -> "1"
4242 | Cmulti (multi, _) -> multicolumns_to_string multi
4243 | Csplit (count, _) -> "-" ^ string_of_int count
4245 (fun v ->
4246 let n, a, b = multicolumns_of_string v in
4247 setcolumns mode n a b);
4249 sep ();
4250 src#caption "Presentation mode" 0;
4251 src#bool "scrollbar visible"
4252 (fun () -> conf.scrollbarinpm)
4253 (fun v ->
4254 if v != conf.scrollbarinpm
4255 then (
4256 conf.scrollbarinpm <- v;
4257 if conf.presentation
4258 then (
4259 state.scrollw <- if v then conf.scrollbw else 0;
4260 reshape conf.winw conf.winh;
4265 sep ();
4266 src#caption "Pixmap cache" 0;
4267 src#int_with_suffix "size (advisory)"
4268 (fun () -> conf.memlimit)
4269 (fun v -> conf.memlimit <- v);
4271 src#caption2 "used"
4272 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4273 (string_with_suffix_of_int state.memused)
4274 (Hashtbl.length state.tilemap)) 1;
4276 sep ();
4277 src#caption "Layout" 0;
4278 src#caption2 "Dimension"
4279 (fun () ->
4280 Printf.sprintf "%dx%d (virtual %dx%d)"
4281 conf.winw conf.winh
4282 state.w state.maxy)
4284 if conf.debug
4285 then
4286 src#caption2 "Position" (fun () ->
4287 Printf.sprintf "%dx%d" state.x state.y
4289 else
4290 src#caption2 "Visible" (fun () -> describe_location ()) 1
4293 sep ();
4294 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4295 "Save these parameters as global defaults at exit"
4296 (fun () -> conf.bedefault)
4297 (fun v -> conf.bedefault <- v)
4300 sep ();
4301 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4302 src#bool ~offset:0 ~btos "Extended parameters"
4303 (fun () -> !showextended)
4304 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4305 if !showextended
4306 then (
4307 src#bool "checkers"
4308 (fun () -> conf.checkers)
4309 (fun v -> conf.checkers <- v; setcheckers v);
4310 src#bool "update cursor"
4311 (fun () -> conf.updatecurs)
4312 (fun v -> conf.updatecurs <- v);
4313 src#bool "verbose"
4314 (fun () -> conf.verbose)
4315 (fun v -> conf.verbose <- v);
4316 src#bool "invert colors"
4317 (fun () -> conf.invert)
4318 (fun v -> conf.invert <- v);
4319 src#bool "max fit"
4320 (fun () -> conf.maxhfit)
4321 (fun v -> conf.maxhfit <- v);
4322 src#bool "redirect stderr"
4323 (fun () -> conf.redirectstderr)
4324 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4325 src#string "uri launcher"
4326 (fun () -> conf.urilauncher)
4327 (fun v -> conf.urilauncher <- v);
4328 src#string "path launcher"
4329 (fun () -> conf.pathlauncher)
4330 (fun v -> conf.pathlauncher <- v);
4331 src#string "tile size"
4332 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4333 (fun v ->
4335 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4336 conf.tilew <- max 64 w;
4337 conf.tileh <- max 64 h;
4338 flushtiles ();
4339 with exn ->
4340 state.text <- Printf.sprintf "bad tile size `%s': %s"
4341 v (Printexc.to_string exn));
4342 src#int "texture count"
4343 (fun () -> conf.texcount)
4344 (fun v ->
4345 if realloctexts v
4346 then conf.texcount <- v
4347 else showtext '!' " Failed to set texture count please retry later"
4349 src#int "slice height"
4350 (fun () -> conf.sliceheight)
4351 (fun v ->
4352 conf.sliceheight <- v;
4353 wcmd "sliceh %d" conf.sliceheight;
4355 src#int "anti-aliasing level"
4356 (fun () -> conf.aalevel)
4357 (fun v ->
4358 conf.aalevel <- bound v 0 8;
4359 state.anchor <- getanchor ();
4360 opendoc state.path state.password;
4362 src#int "ui font size"
4363 (fun () -> fstate.fontsize)
4364 (fun v -> setfontsize (bound v 5 100));
4365 src#int "hint font size"
4366 (fun () -> conf.hfsize)
4367 (fun v -> conf.hfsize <- bound v 5 100);
4368 colorp "background color"
4369 (fun () -> conf.bgcolor)
4370 (fun v -> conf.bgcolor <- v);
4371 src#bool "crop hack"
4372 (fun () -> conf.crophack)
4373 (fun v -> conf.crophack <- v);
4374 src#string "trim fuzz"
4375 (fun () -> irect_to_string conf.trimfuzz)
4376 (fun v ->
4378 conf.trimfuzz <- irect_of_string v;
4379 if conf.trimmargins
4380 then settrim true conf.trimfuzz;
4381 with exn ->
4382 state.text <- Printf.sprintf "bad irect `%s': %s"
4383 v (Printexc.to_string exn)
4385 src#string "throttle"
4386 (fun () ->
4387 match conf.maxwait with
4388 | None -> "show place holder if page is not ready"
4389 | Some time ->
4390 if time = infinity
4391 then "wait for page to fully render"
4392 else
4393 "wait " ^ string_of_float time
4394 ^ " seconds before showing placeholder"
4396 (fun v ->
4398 let f = float_of_string v in
4399 if f <= 0.0
4400 then conf.maxwait <- None
4401 else conf.maxwait <- Some f
4402 with exn ->
4403 state.text <- Printf.sprintf "bad time `%s': %s"
4404 v (Printexc.to_string exn)
4406 src#string "ghyll scroll"
4407 (fun () ->
4408 match conf.ghyllscroll with
4409 | None -> ""
4410 | Some nab -> ghyllscroll_to_string nab
4412 (fun v ->
4414 let gs =
4415 if String.length v = 0
4416 then None
4417 else Some (ghyllscroll_of_string v)
4419 conf.ghyllscroll <- gs
4420 with exn ->
4421 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4422 v (Printexc.to_string exn)
4424 src#string "selection command"
4425 (fun () -> conf.selcmd)
4426 (fun v -> conf.selcmd <- v);
4427 src#colorspace "color space"
4428 (fun () -> colorspace_to_string conf.colorspace)
4429 (fun v ->
4430 conf.colorspace <- colorspace_of_int v;
4431 wcmd "cs %d" v;
4432 load state.layout;
4436 sep ();
4437 src#caption "Document" 0;
4438 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4439 src#caption2 "Pages"
4440 (fun () -> string_of_int state.pagecount) 1;
4441 src#caption2 "Dimensions"
4442 (fun () -> string_of_int (List.length state.pdims)) 1;
4443 if conf.trimmargins
4444 then (
4445 sep ();
4446 src#caption "Trimmed margins" 0;
4447 src#caption2 "Dimensions"
4448 (fun () -> string_of_int (List.length state.pdims)) 1;
4451 sep ();
4452 src#caption "OpenGL" 0;
4453 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4454 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4455 src#reset prevmode prevuioh;
4457 fun () ->
4458 state.text <- "";
4459 let prevmode = state.mode
4460 and prevuioh = state.uioh in
4461 fillsrc prevmode prevuioh;
4462 let source = (src :> lvsource) in
4463 let modehash = findkeyhash conf "info" in
4464 state.uioh <- coe (object (self)
4465 inherit listview ~source ~trusted:true ~modehash as super
4466 val mutable m_prevmemused = 0
4467 method infochanged = function
4468 | Memused ->
4469 if m_prevmemused != state.memused
4470 then (
4471 m_prevmemused <- state.memused;
4472 G.postRedisplay "memusedchanged";
4474 | Pdim -> G.postRedisplay "pdimchanged"
4475 | Docinfo -> fillsrc prevmode prevuioh
4477 method key key mask =
4478 if not (Wsi.withctrl mask)
4479 then
4480 match key with
4481 | 0xff51 -> coe (self#updownlevel ~-1)
4482 | 0xff53 -> coe (self#updownlevel 1)
4483 | _ -> super#key key mask
4484 else super#key key mask
4485 end);
4486 G.postRedisplay "info";
4489 let enterhelpmode =
4490 let source =
4491 (object
4492 inherit lvsourcebase
4493 method getitemcount = Array.length state.help
4494 method getitem n =
4495 let s, n, _ = state.help.(n) in
4496 (s, n)
4498 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4499 let optuioh =
4500 if not cancel
4501 then (
4502 m_qsearch <- qsearch;
4503 match state.help.(active) with
4504 | _, _, Action f -> Some (f uioh)
4505 | _ -> Some (uioh)
4507 else None
4509 m_active <- active;
4510 m_first <- first;
4511 m_pan <- pan;
4512 optuioh
4514 method hasaction n =
4515 match state.help.(n) with
4516 | _, _, Action _ -> true
4517 | _ -> false
4519 initializer
4520 m_active <- -1
4521 end)
4522 in fun () ->
4523 let modehash = findkeyhash conf "help" in
4524 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4525 G.postRedisplay "help";
4528 let entermsgsmode =
4529 let msgsource =
4530 let re = Str.regexp "[\r\n]" in
4531 (object
4532 inherit lvsourcebase
4533 val mutable m_items = [||]
4535 method getitemcount = 1 + Array.length m_items
4537 method getitem n =
4538 if n = 0
4539 then "[Clear]", 0
4540 else m_items.(n-1), 0
4542 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4543 ignore uioh;
4544 if not cancel
4545 then (
4546 if active = 0
4547 then Buffer.clear state.errmsgs;
4548 m_qsearch <- qsearch;
4550 m_active <- active;
4551 m_first <- first;
4552 m_pan <- pan;
4553 None
4555 method hasaction n =
4556 n = 0
4558 method reset =
4559 state.newerrmsgs <- false;
4560 let l = Str.split re (Buffer.contents state.errmsgs) in
4561 m_items <- Array.of_list l
4563 initializer
4564 m_active <- 0
4565 end)
4566 in fun () ->
4567 state.text <- "";
4568 msgsource#reset;
4569 let source = (msgsource :> lvsource) in
4570 let modehash = findkeyhash conf "listview" in
4571 state.uioh <- coe (object
4572 inherit listview ~source ~trusted:false ~modehash as super
4573 method display =
4574 if state.newerrmsgs
4575 then msgsource#reset;
4576 super#display
4577 end);
4578 G.postRedisplay "msgs";
4581 let quickbookmark ?title () =
4582 match state.layout with
4583 | [] -> ()
4584 | l :: _ ->
4585 let title =
4586 match title with
4587 | None ->
4588 let sec = Unix.gettimeofday () in
4589 let tm = Unix.localtime sec in
4590 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4591 (l.pageno+1)
4592 tm.Unix.tm_mday
4593 tm.Unix.tm_mon
4594 (tm.Unix.tm_year + 1900)
4595 tm.Unix.tm_hour
4596 tm.Unix.tm_min
4597 | Some title -> title
4599 state.bookmarks <-
4600 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4601 :: state.bookmarks
4604 let doreshape w h =
4605 state.fullscreen <- None;
4606 Wsi.reshape w h;
4609 let setautoscrollspeed step goingdown =
4610 let incr = max 1 ((abs step) / 2) in
4611 let incr = if goingdown then incr else -incr in
4612 let astep = step + incr in
4613 state.autoscroll <- Some astep;
4616 let gotounder = function
4617 | Ulinkgoto (pageno, top) ->
4618 if pageno >= 0
4619 then (
4620 addnav ();
4621 gotopage1 pageno top;
4624 | Ulinkuri s ->
4625 gotouri s
4627 | Uremote (filename, pageno) ->
4628 let path =
4629 if Sys.file_exists filename
4630 then filename
4631 else
4632 let dir = Filename.dirname state.path in
4633 let path = Filename.concat dir filename in
4634 if Sys.file_exists path
4635 then path
4636 else ""
4638 if String.length path > 0
4639 then (
4640 let anchor = getanchor () in
4641 let ranchor = state.path, state.password, anchor in
4642 state.anchor <- (pageno, 0.0);
4643 state.ranchors <- ranchor :: state.ranchors;
4644 opendoc path "";
4646 else showtext '!' ("Could not find " ^ filename)
4648 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4651 let canpan () =
4652 match conf.columns with
4653 | Csplit _ -> true
4654 | _ -> conf.zoom > 1.0
4657 let viewkeyboard key mask =
4658 let enttext te =
4659 let mode = state.mode in
4660 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4661 state.text <- "";
4662 enttext ();
4663 G.postRedisplay "view:enttext"
4665 let ctrl = Wsi.withctrl mask in
4666 match key with
4667 | 81 -> (* Q *)
4668 exit 0
4670 | 0xff63 -> (* insert *)
4671 if conf.angle mod 360 = 0
4672 then (
4673 state.mode <- LinkNav (Ltgendir 0);
4674 gotoy state.y;
4676 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4678 | 0xff1b | 113 -> (* escape / q *)
4679 begin match state.mstate with
4680 | Mzoomrect _ ->
4681 state.mstate <- Mnone;
4682 Wsi.setcursor Wsi.CURSOR_INHERIT;
4683 G.postRedisplay "kill zoom rect";
4684 | _ ->
4685 match state.ranchors with
4686 | [] -> raise Quit
4687 | (path, password, anchor) :: rest ->
4688 state.ranchors <- rest;
4689 state.anchor <- anchor;
4690 opendoc path password
4691 end;
4693 | 0xff08 -> (* backspace *)
4694 let y = getnav ~-1 in
4695 gotoy_and_clear_text y
4697 | 111 -> (* o *)
4698 enteroutlinemode ()
4700 | 117 -> (* u *)
4701 state.rects <- [];
4702 state.text <- "";
4703 G.postRedisplay "dehighlight";
4705 | 47 | 63 -> (* / ? *)
4706 let ondone isforw s =
4707 cbput state.hists.pat s;
4708 state.searchpattern <- s;
4709 search s isforw
4711 let s = String.create 1 in
4712 s.[0] <- Char.chr key;
4713 enttext (s, "", Some (onhist state.hists.pat),
4714 textentry, ondone (key = 47), true)
4716 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4717 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4718 setzoom (conf.zoom +. incr)
4720 | 43 | 0xffab -> (* + *)
4721 let ondone s =
4722 let n =
4723 try int_of_string s with exc ->
4724 state.text <- Printf.sprintf "bad integer `%s': %s"
4725 s (Printexc.to_string exc);
4726 max_int
4728 if n != max_int
4729 then (
4730 conf.pagebias <- n;
4731 state.text <- "page bias is now " ^ string_of_int n;
4734 enttext ("page bias: ", "", None, intentry, ondone, true)
4736 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4737 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4738 setzoom (max 0.01 (conf.zoom -. decr))
4740 | 45 | 0xffad -> (* - *)
4741 let ondone msg = state.text <- msg in
4742 enttext (
4743 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4744 optentry state.mode, ondone, true
4747 | 48 when ctrl -> (* ctrl-0 *)
4748 setzoom 1.0
4750 | 49 when ctrl -> (* ctrl-1 *)
4751 let cols =
4752 match conf.columns with
4753 | Csingle | Cmulti _ -> 1
4754 | Csplit (n, _) -> n
4756 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4757 if zoom < 1.0
4758 then setzoom zoom
4760 | 0xffc6 -> (* f9 *)
4761 togglebirdseye ()
4763 | 57 when ctrl -> (* ctrl-9 *)
4764 togglebirdseye ()
4766 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4767 when not ctrl -> (* 0..9 *)
4768 let ondone s =
4769 let n =
4770 try int_of_string s with exc ->
4771 state.text <- Printf.sprintf "bad integer `%s': %s"
4772 s (Printexc.to_string exc);
4775 if n >= 0
4776 then (
4777 addnav ();
4778 cbput state.hists.pag (string_of_int n);
4779 gotopage1 (n + conf.pagebias - 1) 0;
4782 let pageentry text key =
4783 match Char.unsafe_chr key with
4784 | 'g' -> TEdone text
4785 | _ -> intentry text key
4787 let text = "x" in text.[0] <- Char.chr key;
4788 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4790 | 98 -> (* b *)
4791 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4792 reshape conf.winw conf.winh;
4794 | 108 -> (* l *)
4795 conf.hlinks <- not conf.hlinks;
4796 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4797 G.postRedisplay "toggle highlightlinks";
4799 | 70 -> (* F *)
4800 state.glinks <- true;
4801 let mode = state.mode in
4802 state.mode <- Textentry (
4803 (":", "", None, linknentry, linkndone (fun under ->
4804 addnav ();
4805 gotounder under
4806 ), false
4807 ), fun _ ->
4808 state.glinks <- false;
4809 state.mode <- mode
4811 state.text <- "";
4812 G.postRedisplay "view:linkent(F)"
4814 | 121 -> (* y *)
4815 state.glinks <- true;
4816 let mode = state.mode in
4817 state.mode <- Textentry (
4818 (":", "", None, linknentry, linkndone (fun under ->
4819 match Ne.pipe () with
4820 | Ne.Exn exn ->
4821 showtext '!' (Printf.sprintf "pipe failed: %s"
4822 (Printexc.to_string exn));
4823 | Ne.Res (r, w) ->
4824 let popened =
4825 try popen conf.selcmd [r, 0; w, -1]; true
4826 with exn ->
4827 showtext '!'
4828 (Printf.sprintf "failed to execute %s: %s"
4829 conf.selcmd (Printexc.to_string exn));
4830 false
4832 let clo cap fd =
4833 Ne.clo fd (fun msg ->
4834 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4837 let s = undertext under in
4838 if popened
4839 then
4840 (try
4841 let l = String.length s in
4842 let n = Unix.write w s 0 l in
4843 if n != l
4844 then
4845 showtext '!'
4846 (Printf.sprintf
4847 "failed to write %d characters to sel pipe, wrote %d"
4850 with exn ->
4851 showtext '!'
4852 (Printf.sprintf "failed to write to sel pipe: %s"
4853 (Printexc.to_string exn)
4856 else dolog "%s" s;
4857 clo "pipe/r" r;
4858 clo "pipe/w" w;
4859 ), false
4861 fun _ ->
4862 state.glinks <- false;
4863 state.mode <- mode
4865 state.text <- "";
4866 G.postRedisplay "view:linkent"
4868 | 97 -> (* a *)
4869 begin match state.autoscroll with
4870 | Some step ->
4871 conf.autoscrollstep <- step;
4872 state.autoscroll <- None
4873 | None ->
4874 if conf.autoscrollstep = 0
4875 then state.autoscroll <- Some 1
4876 else state.autoscroll <- Some conf.autoscrollstep
4879 | 112 when ctrl -> (* ctrl-p *)
4880 launchpath ()
4882 | 80 -> (* P *)
4883 conf.presentation <- not conf.presentation;
4884 if conf.presentation
4885 then (
4886 if not conf.scrollbarinpm
4887 then state.scrollw <- 0;
4889 else
4890 state.scrollw <- conf.scrollbw;
4892 showtext ' ' ("presentation mode " ^
4893 if conf.presentation then "on" else "off");
4894 state.anchor <- getanchor ();
4895 represent ()
4897 | 102 -> (* f *)
4898 begin match state.fullscreen with
4899 | None ->
4900 state.fullscreen <- Some (conf.winw, conf.winh);
4901 Wsi.fullscreen ()
4902 | Some (w, h) ->
4903 state.fullscreen <- None;
4904 doreshape w h
4907 | 103 -> (* g *)
4908 gotoy_and_clear_text 0
4910 | 71 -> (* G *)
4911 gotopage1 (state.pagecount - 1) 0
4913 | 112 | 78 -> (* p|N *)
4914 search state.searchpattern false
4916 | 110 | 0xffc0 -> (* n|F3 *)
4917 search state.searchpattern true
4919 | 116 -> (* t *)
4920 begin match state.layout with
4921 | [] -> ()
4922 | l :: _ ->
4923 gotoy_and_clear_text (getpagey l.pageno)
4926 | 32 -> (* ' ' *)
4927 begin match state.layout with
4928 | [] -> ()
4929 | l :: _ ->
4930 match conf.columns with
4931 | Csingle | Cmulti _ ->
4932 let pageno = min (l.pageno+1) (state.pagecount-1) in
4933 gotoy_and_clear_text (getpagey pageno)
4934 | Csplit (n, _) ->
4935 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4936 then
4937 let pagey, pageh = getpageyh l.pageno in
4938 let pagey = pagey + pageh * l.pagecol in
4939 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4940 gotoy_and_clear_text (pagey + pageh + ips)
4943 | 0xff9f | 0xffff -> (* delete *)
4944 begin match state.layout with
4945 | [] -> ()
4946 | l :: _ ->
4947 match conf.columns with
4948 | Csingle | Cmulti _ ->
4949 let pageno = max 0 (l.pageno-1) in
4950 gotoy_and_clear_text (getpagey pageno)
4951 | Csplit (n, _) ->
4952 let y =
4953 if l.pagecol = 0
4954 then
4955 if l.pageno = 0
4956 then l.pagey
4957 else
4958 let pageno = max 0 (l.pageno-1) in
4959 let pagey, pageh = getpageyh pageno in
4960 pagey + (n-1)*pageh
4961 else
4962 let pagey, pageh = getpageyh l.pageno in
4963 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4965 gotoy_and_clear_text y
4968 | 61 -> (* = *)
4969 showtext ' ' (describe_location ());
4971 | 119 -> (* w *)
4972 begin match state.layout with
4973 | [] -> ()
4974 | l :: _ ->
4975 doreshape (l.pagew + state.scrollw) l.pageh;
4976 G.postRedisplay "w"
4979 | 39 -> (* ' *)
4980 enterbookmarkmode ()
4982 | 104 | 0xffbe -> (* h|F1 *)
4983 enterhelpmode ()
4985 | 105 -> (* i *)
4986 enterinfomode ()
4988 | 101 when conf.redirectstderr -> (* e *)
4989 entermsgsmode ()
4991 | 109 -> (* m *)
4992 let ondone s =
4993 match state.layout with
4994 | l :: _ ->
4995 state.bookmarks <-
4996 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4997 :: state.bookmarks
4998 | _ -> ()
5000 enttext ("bookmark: ", "", None, textentry, ondone, true)
5002 | 126 -> (* ~ *)
5003 quickbookmark ();
5004 showtext ' ' "Quick bookmark added";
5006 | 122 -> (* z *)
5007 begin match state.layout with
5008 | l :: _ ->
5009 let rect = getpdimrect l.pagedimno in
5010 let w, h =
5011 if conf.crophack
5012 then
5013 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5014 truncate (1.2 *. (rect.(3) -. rect.(0))))
5015 else
5016 (truncate (rect.(1) -. rect.(0)),
5017 truncate (rect.(3) -. rect.(0)))
5019 let w = truncate ((float w)*.conf.zoom)
5020 and h = truncate ((float h)*.conf.zoom) in
5021 if w != 0 && h != 0
5022 then (
5023 state.anchor <- getanchor ();
5024 doreshape (w + state.scrollw) (h + conf.interpagespace)
5026 G.postRedisplay "z";
5028 | [] -> ()
5031 | 50 when ctrl -> (* ctrl-2 *)
5032 let maxw = getmaxw () in
5033 if maxw > 0.0
5034 then setzoom (maxw /. float conf.winw)
5036 | 60 | 62 -> (* < > *)
5037 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5039 | 91 | 93 -> (* [ ] *)
5040 conf.colorscale <-
5041 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5043 G.postRedisplay "brightness";
5045 | 99 when state.mode = View -> (* c *)
5046 let (c, a, b), z =
5047 match state.prevcolumns with
5048 | None -> (1, 0, 0), 1.0
5049 | Some (columns, z) ->
5050 let cab =
5051 match columns with
5052 | Csplit (c, _) -> -c, 0, 0
5053 | Cmulti ((c, a, b), _) -> c, a, b
5054 | Csingle -> 1, 0, 0
5056 cab, z
5058 setcolumns View c a b;
5059 setzoom z;
5061 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5062 setzoom state.prevzoom
5064 | 107 | 0xff52 -> (* k up *)
5065 begin match state.autoscroll with
5066 | None ->
5067 begin match state.mode with
5068 | Birdseye beye -> upbirdseye 1 beye
5069 | _ ->
5070 if ctrl
5071 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5072 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5074 | Some n ->
5075 setautoscrollspeed n false
5078 | 106 | 0xff54 -> (* j down *)
5079 begin match state.autoscroll with
5080 | None ->
5081 begin match state.mode with
5082 | Birdseye beye -> downbirdseye 1 beye
5083 | _ ->
5084 if ctrl
5085 then gotoy_and_clear_text (clamp (conf.winh/2))
5086 else gotoy_and_clear_text (clamp conf.scrollstep)
5088 | Some n ->
5089 setautoscrollspeed n true
5092 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5093 if canpan ()
5094 then
5095 let dx =
5096 if ctrl
5097 then conf.winw / 2
5098 else 10
5100 let dx = if key = 0xff51 then dx else -dx in
5101 state.x <- state.x + dx;
5102 gotoy_and_clear_text state.y
5103 else (
5104 state.text <- "";
5105 G.postRedisplay "lef/right"
5108 | 0xff55 -> (* prior *)
5109 let y =
5110 if ctrl
5111 then
5112 match state.layout with
5113 | [] -> state.y
5114 | l :: _ -> state.y - l.pagey
5115 else
5116 clamp (-conf.winh)
5118 gotoghyll y
5120 | 0xff56 -> (* next *)
5121 let y =
5122 if ctrl
5123 then
5124 match List.rev state.layout with
5125 | [] -> state.y
5126 | l :: _ -> getpagey l.pageno
5127 else
5128 clamp conf.winh
5130 gotoghyll y
5132 | 0xff50 -> gotoghyll 0
5133 | 0xff57 -> gotoghyll (clamp state.maxy)
5134 | 0xff53 when Wsi.withalt mask ->
5135 gotoghyll (getnav ~-1)
5136 | 0xff51 when Wsi.withalt mask ->
5137 gotoghyll (getnav 1)
5139 | 114 -> (* r *)
5140 state.anchor <- getanchor ();
5141 opendoc state.path state.password
5143 | 118 when conf.debug -> (* v *)
5144 state.rects <- [];
5145 List.iter (fun l ->
5146 match getopaque l.pageno with
5147 | None -> ()
5148 | Some opaque ->
5149 let x0, y0, x1, y1 = pagebbox opaque in
5150 let a,b = float x0, float y0 in
5151 let c,d = float x1, float y0 in
5152 let e,f = float x1, float y1 in
5153 let h,j = float x0, float y1 in
5154 let rect = (a,b,c,d,e,f,h,j) in
5155 debugrect rect;
5156 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5157 ) state.layout;
5158 G.postRedisplay "v";
5160 | _ ->
5161 vlog "huh? %s" (Wsi.keyname key)
5164 let linknavkeyboard key mask linknav =
5165 let getpage pageno =
5166 let rec loop = function
5167 | [] -> None
5168 | l :: _ when l.pageno = pageno -> Some l
5169 | _ :: rest -> loop rest
5170 in loop state.layout
5172 let doexact (pageno, n) =
5173 match getopaque pageno, getpage pageno with
5174 | Some opaque, Some l ->
5175 if key = 0xff0d
5176 then
5177 let under = getlink opaque n in
5178 G.postRedisplay "link gotounder";
5179 gotounder under;
5180 state.mode <- View;
5181 else
5182 let opt, dir =
5183 match key with
5184 | 0xff50 -> (* home *)
5185 Some (findlink opaque LDfirst), -1
5187 | 0xff57 -> (* end *)
5188 Some (findlink opaque LDlast), 1
5190 | 0xff51 -> (* left *)
5191 Some (findlink opaque (LDleft n)), -1
5193 | 0xff53 -> (* right *)
5194 Some (findlink opaque (LDright n)), 1
5196 | 0xff52 -> (* up *)
5197 Some (findlink opaque (LDup n)), -1
5199 | 0xff54 -> (* down *)
5200 Some (findlink opaque (LDdown n)), 1
5202 | _ -> None, 0
5204 let pwl l dir =
5205 begin match findpwl l.pageno dir with
5206 | Pwlnotfound -> ()
5207 | Pwl pageno ->
5208 let notfound dir =
5209 state.mode <- LinkNav (Ltgendir dir);
5210 let y, h = getpageyh pageno in
5211 let y =
5212 if dir < 0
5213 then y + h - conf.winh
5214 else y
5216 gotoy y
5218 begin match getopaque pageno, getpage pageno with
5219 | Some opaque, Some _ ->
5220 let link =
5221 let ld = if dir > 0 then LDfirst else LDlast in
5222 findlink opaque ld
5224 begin match link with
5225 | Lfound m ->
5226 showlinktype (getlink opaque m);
5227 state.mode <- LinkNav (Ltexact (pageno, m));
5228 G.postRedisplay "linknav jpage";
5229 | _ -> notfound dir
5230 end;
5231 | _ -> notfound dir
5232 end;
5233 end;
5235 begin match opt with
5236 | Some Lnotfound -> pwl l dir;
5237 | Some (Lfound m) ->
5238 if m = n
5239 then pwl l dir
5240 else (
5241 let _, y0, _, y1 = getlinkrect opaque m in
5242 if y0 < l.pagey
5243 then gotopage1 l.pageno y0
5244 else (
5245 let d = fstate.fontsize + 1 in
5246 if y1 - l.pagey > l.pagevh - d
5247 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5248 else G.postRedisplay "linknav";
5250 showlinktype (getlink opaque m);
5251 state.mode <- LinkNav (Ltexact (l.pageno, m));
5254 | None -> viewkeyboard key mask
5255 end;
5256 | _ -> viewkeyboard key mask
5258 if key = 0xff63
5259 then (
5260 state.mode <- View;
5261 G.postRedisplay "leave linknav"
5263 else
5264 match linknav with
5265 | Ltgendir _ -> viewkeyboard key mask
5266 | Ltexact exact -> doexact exact
5269 let keyboard key mask =
5270 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5271 then wcmd "interrupt"
5272 else state.uioh <- state.uioh#key key mask
5275 let birdseyekeyboard key mask
5276 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5277 let incr =
5278 match conf.columns with
5279 | Csingle -> 1
5280 | Cmulti ((c, _, _), _) -> c
5281 | Csplit _ -> failwith "bird's eye split mode"
5283 match key with
5284 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5285 let y, h = getpageyh pageno in
5286 let top = (conf.winh - h) / 2 in
5287 gotoy (max 0 (y - top))
5288 | 0xff0d -> leavebirdseye beye false
5289 | 0xff1b -> leavebirdseye beye true (* escape *)
5290 | 0xff52 -> upbirdseye incr beye (* prior *)
5291 | 0xff54 -> downbirdseye incr beye (* next *)
5292 | 0xff51 -> upbirdseye 1 beye (* up *)
5293 | 0xff53 -> downbirdseye 1 beye (* down *)
5295 | 0xff55 ->
5296 begin match state.layout with
5297 | l :: _ ->
5298 if l.pagey != 0
5299 then (
5300 state.mode <- Birdseye (
5301 oconf, leftx, l.pageno, hooverpageno, anchor
5303 gotopage1 l.pageno 0;
5305 else (
5306 let layout = layout (state.y-conf.winh) conf.winh in
5307 match layout with
5308 | [] -> gotoy (clamp (-conf.winh))
5309 | l :: _ ->
5310 state.mode <- Birdseye (
5311 oconf, leftx, l.pageno, hooverpageno, anchor
5313 gotopage1 l.pageno 0
5316 | [] -> gotoy (clamp (-conf.winh))
5317 end;
5319 | 0xff56 ->
5320 begin match List.rev state.layout with
5321 | l :: _ ->
5322 let layout = layout (state.y + conf.winh) conf.winh in
5323 begin match layout with
5324 | [] ->
5325 let incr = l.pageh - l.pagevh in
5326 if incr = 0
5327 then (
5328 state.mode <-
5329 Birdseye (
5330 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5332 G.postRedisplay "birdseye pagedown";
5334 else gotoy (clamp (incr + conf.interpagespace*2));
5336 | l :: _ ->
5337 state.mode <-
5338 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5339 gotopage1 l.pageno 0;
5342 | [] -> gotoy (clamp conf.winh)
5343 end;
5345 | 0xff50 ->
5346 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5347 gotopage1 0 0
5349 | 0xff57 ->
5350 let pageno = state.pagecount - 1 in
5351 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5352 if not (pagevisible state.layout pageno)
5353 then
5354 let h =
5355 match List.rev state.pdims with
5356 | [] -> conf.winh
5357 | (_, _, h, _) :: _ -> h
5359 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5360 else G.postRedisplay "birdseye end";
5361 | _ -> viewkeyboard key mask
5364 let drawpage l linkindexbase =
5365 let color =
5366 match state.mode with
5367 | Textentry _ -> scalecolor 0.4
5368 | LinkNav _
5369 | View -> scalecolor 1.0
5370 | Birdseye (_, _, pageno, hooverpageno, _) ->
5371 if l.pageno = hooverpageno
5372 then scalecolor 0.9
5373 else (
5374 if l.pageno = pageno
5375 then scalecolor 1.0
5376 else scalecolor 0.8
5379 drawtiles l color;
5380 begin match getopaque l.pageno with
5381 | Some opaque ->
5382 if tileready l l.pagex l.pagey
5383 then
5384 let x = l.pagedispx - l.pagex
5385 and y = l.pagedispy - l.pagey in
5386 let hlmask =
5387 match conf.columns with
5388 | Csingle | Cmulti _ ->
5389 (if conf.hlinks then 1 else 0)
5390 + (if state.glinks
5391 && not (isbirdseye state.mode) then 2 else 0)
5392 | _ -> 0
5394 let s =
5395 match state.mode with
5396 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5397 | _ -> ""
5399 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5400 else 0
5402 | _ -> 0
5403 end;
5406 let scrollindicator () =
5407 let sbw, ph, sh = state.uioh#scrollph in
5408 let sbh, pw, sw = state.uioh#scrollpw in
5410 GlDraw.color (0.64, 0.64, 0.64);
5411 GlDraw.rect
5412 (float (conf.winw - sbw), 0.)
5413 (float conf.winw, float conf.winh)
5415 GlDraw.rect
5416 (0., float (conf.winh - sbh))
5417 (float (conf.winw - state.scrollw - 1), float conf.winh)
5419 GlDraw.color (0.0, 0.0, 0.0);
5421 GlDraw.rect
5422 (float (conf.winw - sbw), ph)
5423 (float conf.winw, ph +. sh)
5425 GlDraw.rect
5426 (pw, float (conf.winh - sbh))
5427 (pw +. sw, float conf.winh)
5431 let showsel () =
5432 match state.mstate with
5433 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5436 | Msel ((x0, y0), (x1, y1)) ->
5437 let rec loop = function
5438 | l :: ls ->
5439 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5440 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5441 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5442 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5443 then
5444 match getopaque l.pageno with
5445 | Some opaque ->
5446 let x0, y0 = pagetranslatepoint l x0 y0 in
5447 let x1, y1 = pagetranslatepoint l x1 y1 in
5448 seltext opaque (x0, y0, x1, y1);
5449 | _ -> ()
5450 else loop ls
5451 | [] -> ()
5453 loop state.layout
5456 let showrects rects =
5457 Gl.enable `blend;
5458 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5459 GlDraw.polygon_mode `both `fill;
5460 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5461 List.iter
5462 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5463 List.iter (fun l ->
5464 if l.pageno = pageno
5465 then (
5466 let dx = float (l.pagedispx - l.pagex) in
5467 let dy = float (l.pagedispy - l.pagey) in
5468 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5469 GlDraw.begins `quads;
5471 GlDraw.vertex2 (x0+.dx, y0+.dy);
5472 GlDraw.vertex2 (x1+.dx, y1+.dy);
5473 GlDraw.vertex2 (x2+.dx, y2+.dy);
5474 GlDraw.vertex2 (x3+.dx, y3+.dy);
5476 GlDraw.ends ();
5478 ) state.layout
5479 ) rects
5481 Gl.disable `blend;
5484 let display () =
5485 GlClear.color (scalecolor2 conf.bgcolor);
5486 GlClear.clear [`color];
5487 let rec loop linkindexbase = function
5488 | l :: rest ->
5489 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5490 loop linkindexbase rest
5491 | [] -> ()
5493 loop 0 state.layout;
5494 let rects =
5495 match state.mode with
5496 | LinkNav (Ltexact (pageno, linkno)) ->
5497 begin match getopaque pageno with
5498 | Some opaque ->
5499 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5500 (pageno, 5, (
5501 float x0, float y0,
5502 float x1, float y0,
5503 float x1, float y1,
5504 float x0, float y1)
5505 ) :: state.rects
5506 | None -> state.rects
5508 | _ -> state.rects
5510 showrects rects;
5511 showsel ();
5512 state.uioh#display;
5513 begin match state.mstate with
5514 | Mzoomrect ((x0, y0), (x1, y1)) ->
5515 Gl.enable `blend;
5516 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5517 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5518 GlDraw.rect (float x0, float y0)
5519 (float x1, float y1);
5520 Gl.disable `blend;
5521 | _ -> ()
5522 end;
5523 enttext ();
5524 scrollindicator ();
5525 Wsi.swapb ();
5528 let zoomrect x y x1 y1 =
5529 let x0 = min x x1
5530 and x1 = max x x1
5531 and y0 = min y y1 in
5532 gotoy (state.y + y0);
5533 state.anchor <- getanchor ();
5534 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5535 let margin =
5536 if state.w < conf.winw - state.scrollw
5537 then (conf.winw - state.scrollw - state.w) / 2
5538 else 0
5540 state.x <- (state.x + margin) - x0;
5541 setzoom zoom;
5542 Wsi.setcursor Wsi.CURSOR_INHERIT;
5543 state.mstate <- Mnone;
5546 let scrollx x =
5547 let winw = conf.winw - state.scrollw - 1 in
5548 let s = float x /. float winw in
5549 let destx = truncate (float (state.w + winw) *. s) in
5550 state.x <- winw - destx;
5551 gotoy_and_clear_text state.y;
5552 state.mstate <- Mscrollx;
5555 let scrolly y =
5556 let s = float y /. float conf.winh in
5557 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5558 gotoy_and_clear_text desty;
5559 state.mstate <- Mscrolly;
5562 let viewmouse button down x y mask =
5563 match button with
5564 | n when (n == 4 || n == 5) && not down ->
5565 if Wsi.withctrl mask
5566 then (
5567 match state.mstate with
5568 | Mzoom (oldn, i) ->
5569 if oldn = n
5570 then (
5571 if i = 2
5572 then
5573 let incr =
5574 match n with
5575 | 5 ->
5576 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5577 | _ ->
5578 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5580 let zoom = conf.zoom -. incr in
5581 setzoom zoom;
5582 state.mstate <- Mzoom (n, 0);
5583 else
5584 state.mstate <- Mzoom (n, i+1);
5586 else state.mstate <- Mzoom (n, 0)
5588 | _ -> state.mstate <- Mzoom (n, 0)
5590 else (
5591 match state.autoscroll with
5592 | Some step -> setautoscrollspeed step (n=4)
5593 | None ->
5594 let incr =
5595 if n = 4
5596 then -conf.scrollstep
5597 else conf.scrollstep
5599 let incr = incr * 2 in
5600 let y = clamp incr in
5601 gotoy_and_clear_text y
5604 | n when (n = 6 || n = 7) && not down && canpan () ->
5605 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5606 gotoy_and_clear_text state.y
5608 | 1 when Wsi.withctrl mask ->
5609 if down
5610 then (
5611 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5612 state.mstate <- Mpan (x, y)
5614 else
5615 state.mstate <- Mnone
5617 | 3 ->
5618 if down
5619 then (
5620 Wsi.setcursor Wsi.CURSOR_CYCLE;
5621 let p = (x, y) in
5622 state.mstate <- Mzoomrect (p, p)
5624 else (
5625 match state.mstate with
5626 | Mzoomrect ((x0, y0), _) ->
5627 if abs (x-x0) > 10 && abs (y - y0) > 10
5628 then zoomrect x0 y0 x y
5629 else (
5630 state.mstate <- Mnone;
5631 Wsi.setcursor Wsi.CURSOR_INHERIT;
5632 G.postRedisplay "kill accidental zoom rect";
5634 | _ ->
5635 Wsi.setcursor Wsi.CURSOR_INHERIT;
5636 state.mstate <- Mnone
5639 | 1 when x > conf.winw - state.scrollw ->
5640 if down
5641 then
5642 let _, position, sh = state.uioh#scrollph in
5643 if y > truncate position && y < truncate (position +. sh)
5644 then state.mstate <- Mscrolly
5645 else scrolly y
5646 else
5647 state.mstate <- Mnone
5649 | 1 when y > conf.winh - state.hscrollh ->
5650 if down
5651 then
5652 let _, position, sw = state.uioh#scrollpw in
5653 if x > truncate position && x < truncate (position +. sw)
5654 then state.mstate <- Mscrollx
5655 else scrollx x
5656 else
5657 state.mstate <- Mnone
5659 | 1 ->
5660 let dest = if down then getunder x y else Unone in
5661 begin match dest with
5662 | Ulinkgoto _
5663 | Ulinkuri _
5664 | Uremote _
5665 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5666 gotounder dest
5668 | Unone when down ->
5669 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5670 state.mstate <- Mpan (x, y);
5672 | Unone | Utext _ ->
5673 if down
5674 then (
5675 if conf.angle mod 360 = 0
5676 then (
5677 state.mstate <- Msel ((x, y), (x, y));
5678 G.postRedisplay "mouse select";
5681 else (
5682 match state.mstate with
5683 | Mnone -> ()
5685 | Mzoom _ | Mscrollx | Mscrolly ->
5686 state.mstate <- Mnone
5688 | Mzoomrect ((x0, y0), _) ->
5689 zoomrect x0 y0 x y
5691 | Mpan _ ->
5692 Wsi.setcursor Wsi.CURSOR_INHERIT;
5693 state.mstate <- Mnone
5695 | Msel ((_, y0), (_, y1)) ->
5696 let rec loop = function
5697 | [] -> ()
5698 | l :: rest ->
5699 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5700 || ((y1 >= l.pagedispy
5701 && y1 <= (l.pagedispy + l.pagevh)))
5702 then
5703 match getopaque l.pageno with
5704 | Some opaque ->
5705 begin
5706 match Ne.pipe () with
5707 | Ne.Exn exn ->
5708 showtext '!'
5709 (Printf.sprintf
5710 "can not create sel pipe: %s"
5711 (Printexc.to_string exn));
5712 | Ne.Res (r, w) ->
5713 let doclose what fd =
5714 Ne.clo fd (fun msg ->
5715 dolog "%s close failed: %s" what msg)
5718 popen conf.selcmd [r, 0; w, -1];
5719 copysel w opaque;
5720 doclose "pipe/r" r;
5721 G.postRedisplay "copysel";
5722 with exn ->
5723 dolog "can not execute %S: %s"
5724 conf.selcmd (Printexc.to_string exn);
5725 doclose "pipe/r" r;
5726 doclose "pipe/w" w;
5728 | None -> ()
5729 else loop rest
5731 loop state.layout;
5732 Wsi.setcursor Wsi.CURSOR_INHERIT;
5733 state.mstate <- Mnone;
5737 | _ -> ()
5740 let birdseyemouse button down x y mask
5741 (conf, leftx, _, hooverpageno, anchor) =
5742 match button with
5743 | 1 when down ->
5744 let rec loop = function
5745 | [] -> ()
5746 | l :: rest ->
5747 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5748 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5749 then (
5750 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5752 else loop rest
5754 loop state.layout
5755 | 3 -> ()
5756 | _ -> viewmouse button down x y mask
5759 let mouse button down x y mask =
5760 state.uioh <- state.uioh#button button down x y mask;
5763 let motion ~x ~y =
5764 state.uioh <- state.uioh#motion x y
5767 let pmotion ~x ~y =
5768 state.uioh <- state.uioh#pmotion x y;
5771 let uioh = object
5772 method display = ()
5774 method key key mask =
5775 begin match state.mode with
5776 | Textentry textentry -> textentrykeyboard key mask textentry
5777 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5778 | View -> viewkeyboard key mask
5779 | LinkNav linknav -> linknavkeyboard key mask linknav
5780 end;
5781 state.uioh
5783 method button button bstate x y mask =
5784 begin match state.mode with
5785 | LinkNav _
5786 | View -> viewmouse button bstate x y mask
5787 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5788 | Textentry _ -> ()
5789 end;
5790 state.uioh
5792 method motion x y =
5793 begin match state.mode with
5794 | Textentry _ -> ()
5795 | View | Birdseye _ | LinkNav _ ->
5796 match state.mstate with
5797 | Mzoom _ | Mnone -> ()
5799 | Mpan (x0, y0) ->
5800 let dx = x - x0
5801 and dy = y0 - y in
5802 state.mstate <- Mpan (x, y);
5803 if canpan ()
5804 then state.x <- state.x + dx;
5805 let y = clamp dy in
5806 gotoy_and_clear_text y
5808 | Msel (a, _) ->
5809 state.mstate <- Msel (a, (x, y));
5810 G.postRedisplay "motion select";
5812 | Mscrolly ->
5813 let y = min conf.winh (max 0 y) in
5814 scrolly y
5816 | Mscrollx ->
5817 let x = min conf.winw (max 0 x) in
5818 scrollx x
5820 | Mzoomrect (p0, _) ->
5821 state.mstate <- Mzoomrect (p0, (x, y));
5822 G.postRedisplay "motion zoomrect";
5823 end;
5824 state.uioh
5826 method pmotion x y =
5827 begin match state.mode with
5828 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5829 let rec loop = function
5830 | [] ->
5831 if hooverpageno != -1
5832 then (
5833 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5834 G.postRedisplay "pmotion birdseye no hoover";
5836 | l :: rest ->
5837 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5838 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5839 then (
5840 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5841 G.postRedisplay "pmotion birdseye hoover";
5843 else loop rest
5845 loop state.layout
5847 | Textentry _ -> ()
5849 | LinkNav _
5850 | View ->
5851 match state.mstate with
5852 | Mnone -> updateunder x y
5853 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5855 end;
5856 state.uioh
5858 method infochanged _ = ()
5860 method scrollph =
5861 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5862 let p, h = scrollph state.y maxy in
5863 state.scrollw, p, h
5865 method scrollpw =
5866 let winw = conf.winw - state.scrollw - 1 in
5867 let fwinw = float winw in
5868 let sw =
5869 let sw = fwinw /. float state.w in
5870 let sw = fwinw *. sw in
5871 max sw (float conf.scrollh)
5873 let position, sw =
5874 let f = state.w+winw in
5875 let r = float (winw-state.x) /. float f in
5876 let p = fwinw *. r in
5877 p-.sw/.2., sw
5879 let sw =
5880 if position +. sw > fwinw
5881 then fwinw -. position
5882 else sw
5884 state.hscrollh, position, sw
5886 method modehash =
5887 let modename =
5888 match state.mode with
5889 | LinkNav _ -> "links"
5890 | Textentry _ -> "textentry"
5891 | Birdseye _ -> "birdseye"
5892 | View -> "view"
5894 findkeyhash conf modename
5895 end;;
5897 module Config =
5898 struct
5899 open Parser
5901 let fontpath = ref "";;
5903 module KeyMap =
5904 Map.Make (struct type t = (int * int) let compare = compare end);;
5906 let unent s =
5907 let l = String.length s in
5908 let b = Buffer.create l in
5909 unent b s 0 l;
5910 Buffer.contents b;
5913 let home =
5914 try Sys.getenv "HOME"
5915 with exn ->
5916 prerr_endline
5917 ("Can not determine home directory location: " ^
5918 Printexc.to_string exn);
5922 let modifier_of_string = function
5923 | "alt" -> Wsi.altmask
5924 | "shift" -> Wsi.shiftmask
5925 | "ctrl" | "control" -> Wsi.ctrlmask
5926 | "meta" -> Wsi.metamask
5927 | _ -> 0
5930 let key_of_string =
5931 let r = Str.regexp "-" in
5932 fun s ->
5933 let elems = Str.full_split r s in
5934 let f n k m =
5935 let g s =
5936 let m1 = modifier_of_string s in
5937 if m1 = 0
5938 then (Wsi.namekey s, m)
5939 else (k, m lor m1)
5940 in function
5941 | Str.Delim s when n land 1 = 0 -> g s
5942 | Str.Text s -> g s
5943 | Str.Delim _ -> (k, m)
5945 let rec loop n k m = function
5946 | [] -> (k, m)
5947 | x :: xs ->
5948 let k, m = f n k m x in
5949 loop (n+1) k m xs
5951 loop 0 0 0 elems
5954 let keys_of_string =
5955 let r = Str.regexp "[ \t]" in
5956 fun s ->
5957 let elems = Str.split r s in
5958 List.map key_of_string elems
5961 let copykeyhashes c =
5962 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5965 let config_of c attrs =
5966 let apply c k v =
5968 match k with
5969 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5970 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5971 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5972 | "preload" -> { c with preload = bool_of_string v }
5973 | "page-bias" -> { c with pagebias = int_of_string v }
5974 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5975 | "horizontal-scroll-step" ->
5976 { c with hscrollstep = max (int_of_string v) 1 }
5977 | "auto-scroll-step" ->
5978 { c with autoscrollstep = max 0 (int_of_string v) }
5979 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5980 | "crop-hack" -> { c with crophack = bool_of_string v }
5981 | "throttle" ->
5982 let mw =
5983 match String.lowercase v with
5984 | "true" -> Some infinity
5985 | "false" -> None
5986 | f -> Some (float_of_string f)
5988 { c with maxwait = mw}
5989 | "highlight-links" -> { c with hlinks = bool_of_string v }
5990 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5991 | "vertical-margin" ->
5992 { c with interpagespace = max 0 (int_of_string v) }
5993 | "zoom" ->
5994 let zoom = float_of_string v /. 100. in
5995 let zoom = max zoom 0.0 in
5996 { c with zoom = zoom }
5997 | "presentation" -> { c with presentation = bool_of_string v }
5998 | "rotation-angle" -> { c with angle = int_of_string v }
5999 | "width" -> { c with winw = max 20 (int_of_string v) }
6000 | "height" -> { c with winh = max 20 (int_of_string v) }
6001 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6002 | "proportional-display" -> { c with proportional = bool_of_string v }
6003 | "pixmap-cache-size" ->
6004 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6005 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6006 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6007 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6008 | "persistent-location" -> { c with jumpback = bool_of_string v }
6009 | "background-color" -> { c with bgcolor = color_of_string v }
6010 | "scrollbar-in-presentation" ->
6011 { c with scrollbarinpm = bool_of_string v }
6012 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6013 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6014 | "mupdf-store-size" ->
6015 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6016 | "checkers" -> { c with checkers = bool_of_string v }
6017 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6018 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6019 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6020 | "uri-launcher" -> { c with urilauncher = unent v }
6021 | "path-launcher" -> { c with pathlauncher = unent v }
6022 | "color-space" -> { c with colorspace = colorspace_of_string v }
6023 | "invert-colors" -> { c with invert = bool_of_string v }
6024 | "brightness" -> { c with colorscale = float_of_string v }
6025 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6026 | "ghyllscroll" ->
6027 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6028 | "columns" ->
6029 let (n, _, _) as nab = multicolumns_of_string v in
6030 if n < 0
6031 then { c with columns = Csplit (-n, [||]) }
6032 else { c with columns = Cmulti (nab, [||]) }
6033 | "birds-eye-columns" ->
6034 { c with beyecolumns = Some (max (int_of_string v) 2) }
6035 | "selection-command" -> { c with selcmd = unent v }
6036 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6037 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6038 | _ -> c
6039 with exn ->
6040 prerr_endline ("Error processing attribute (`" ^
6041 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6044 let rec fold c = function
6045 | [] -> c
6046 | (k, v) :: rest ->
6047 let c = apply c k v in
6048 fold c rest
6050 fold { c with keyhashes = copykeyhashes c } attrs;
6053 let fromstring f pos n v d =
6054 try f v
6055 with exn ->
6056 dolog "Error processing attribute (%S=%S) at %d\n%s"
6057 n v pos (Printexc.to_string exn)
6062 let bookmark_of attrs =
6063 let rec fold title page rely = function
6064 | ("title", v) :: rest -> fold v page rely rest
6065 | ("page", v) :: rest -> fold title v rely rest
6066 | ("rely", v) :: rest -> fold title page v rest
6067 | _ :: rest -> fold title page rely rest
6068 | [] -> title, page, rely
6070 fold "invalid" "0" "0" attrs
6073 let doc_of attrs =
6074 let rec fold path page rely pan = function
6075 | ("path", v) :: rest -> fold v page rely pan rest
6076 | ("page", v) :: rest -> fold path v rely pan rest
6077 | ("rely", v) :: rest -> fold path page v pan rest
6078 | ("pan", v) :: rest -> fold path page rely v rest
6079 | _ :: rest -> fold path page rely pan rest
6080 | [] -> path, page, rely, pan
6082 fold "" "0" "0" "0" attrs
6085 let map_of attrs =
6086 let rec fold rs ls = function
6087 | ("out", v) :: rest -> fold v ls rest
6088 | ("in", v) :: rest -> fold rs v rest
6089 | _ :: rest -> fold ls rs rest
6090 | [] -> ls, rs
6092 fold "" "" attrs
6095 let setconf dst src =
6096 dst.scrollbw <- src.scrollbw;
6097 dst.scrollh <- src.scrollh;
6098 dst.icase <- src.icase;
6099 dst.preload <- src.preload;
6100 dst.pagebias <- src.pagebias;
6101 dst.verbose <- src.verbose;
6102 dst.scrollstep <- src.scrollstep;
6103 dst.maxhfit <- src.maxhfit;
6104 dst.crophack <- src.crophack;
6105 dst.autoscrollstep <- src.autoscrollstep;
6106 dst.maxwait <- src.maxwait;
6107 dst.hlinks <- src.hlinks;
6108 dst.underinfo <- src.underinfo;
6109 dst.interpagespace <- src.interpagespace;
6110 dst.zoom <- src.zoom;
6111 dst.presentation <- src.presentation;
6112 dst.angle <- src.angle;
6113 dst.winw <- src.winw;
6114 dst.winh <- src.winh;
6115 dst.savebmarks <- src.savebmarks;
6116 dst.memlimit <- src.memlimit;
6117 dst.proportional <- src.proportional;
6118 dst.texcount <- src.texcount;
6119 dst.sliceheight <- src.sliceheight;
6120 dst.thumbw <- src.thumbw;
6121 dst.jumpback <- src.jumpback;
6122 dst.bgcolor <- src.bgcolor;
6123 dst.scrollbarinpm <- src.scrollbarinpm;
6124 dst.tilew <- src.tilew;
6125 dst.tileh <- src.tileh;
6126 dst.mustoresize <- src.mustoresize;
6127 dst.checkers <- src.checkers;
6128 dst.aalevel <- src.aalevel;
6129 dst.trimmargins <- src.trimmargins;
6130 dst.trimfuzz <- src.trimfuzz;
6131 dst.urilauncher <- src.urilauncher;
6132 dst.colorspace <- src.colorspace;
6133 dst.invert <- src.invert;
6134 dst.colorscale <- src.colorscale;
6135 dst.redirectstderr <- src.redirectstderr;
6136 dst.ghyllscroll <- src.ghyllscroll;
6137 dst.columns <- src.columns;
6138 dst.beyecolumns <- src.beyecolumns;
6139 dst.selcmd <- src.selcmd;
6140 dst.updatecurs <- src.updatecurs;
6141 dst.pathlauncher <- src.pathlauncher;
6142 dst.keyhashes <- copykeyhashes src;
6143 dst.hfsize <- src.hfsize;
6144 dst.hscrollstep <- src.hscrollstep;
6147 let get s =
6148 let h = Hashtbl.create 10 in
6149 let dc = { defconf with angle = defconf.angle } in
6150 let rec toplevel v t spos _ =
6151 match t with
6152 | Vdata | Vcdata | Vend -> v
6153 | Vopen ("llppconfig", _, closed) ->
6154 if closed
6155 then v
6156 else { v with f = llppconfig }
6157 | Vopen _ ->
6158 error "unexpected subelement at top level" s spos
6159 | Vclose _ -> error "unexpected close at top level" s spos
6161 and llppconfig v t spos _ =
6162 match t with
6163 | Vdata | Vcdata -> v
6164 | Vend -> error "unexpected end of input in llppconfig" s spos
6165 | Vopen ("defaults", attrs, closed) ->
6166 let c = config_of dc attrs in
6167 setconf dc c;
6168 if closed
6169 then v
6170 else { v with f = defaults }
6172 | Vopen ("ui-font", attrs, closed) ->
6173 let rec getsize size = function
6174 | [] -> size
6175 | ("size", v) :: rest ->
6176 let size =
6177 fromstring int_of_string spos "size" v fstate.fontsize in
6178 getsize size rest
6179 | l -> getsize size l
6181 fstate.fontsize <- getsize fstate.fontsize attrs;
6182 if closed
6183 then v
6184 else { v with f = uifont (Buffer.create 10) }
6186 | Vopen ("doc", attrs, closed) ->
6187 let pathent, spage, srely, span = doc_of attrs in
6188 let path = unent pathent
6189 and pageno = fromstring int_of_string spos "page" spage 0
6190 and rely = fromstring float_of_string spos "rely" srely 0.0
6191 and pan = fromstring int_of_string spos "pan" span 0 in
6192 let c = config_of dc attrs in
6193 let anchor = (pageno, rely) in
6194 if closed
6195 then (Hashtbl.add h path (c, [], pan, anchor); v)
6196 else { v with f = doc path pan anchor c [] }
6198 | Vopen _ ->
6199 error "unexpected subelement in llppconfig" s spos
6201 | Vclose "llppconfig" -> { v with f = toplevel }
6202 | Vclose _ -> error "unexpected close in llppconfig" s spos
6204 and defaults v t spos _ =
6205 match t with
6206 | Vdata | Vcdata -> v
6207 | Vend -> error "unexpected end of input in defaults" s spos
6208 | Vopen ("keymap", attrs, closed) ->
6209 let modename =
6210 try List.assoc "mode" attrs
6211 with Not_found -> "global" in
6212 if closed
6213 then v
6214 else
6215 let ret keymap =
6216 let h = findkeyhash dc modename in
6217 KeyMap.iter (Hashtbl.replace h) keymap;
6218 defaults
6220 { v with f = pkeymap ret KeyMap.empty }
6222 | Vopen (_, _, _) ->
6223 error "unexpected subelement in defaults" s spos
6225 | Vclose "defaults" ->
6226 { v with f = llppconfig }
6228 | Vclose _ -> error "unexpected close in defaults" s spos
6230 and uifont b v t spos epos =
6231 match t with
6232 | Vdata | Vcdata ->
6233 Buffer.add_substring b s spos (epos - spos);
6235 | Vopen (_, _, _) ->
6236 error "unexpected subelement in ui-font" s spos
6237 | Vclose "ui-font" ->
6238 if String.length !fontpath = 0
6239 then fontpath := Buffer.contents b;
6240 { v with f = llppconfig }
6241 | Vclose _ -> error "unexpected close in ui-font" s spos
6242 | Vend -> error "unexpected end of input in ui-font" s spos
6244 and doc path pan anchor c bookmarks v t spos _ =
6245 match t with
6246 | Vdata | Vcdata -> v
6247 | Vend -> error "unexpected end of input in doc" s spos
6248 | Vopen ("bookmarks", _, closed) ->
6249 if closed
6250 then v
6251 else { v with f = pbookmarks path pan anchor c bookmarks }
6253 | Vopen ("keymap", attrs, closed) ->
6254 let modename =
6255 try List.assoc "mode" attrs
6256 with Not_found -> "global"
6258 if closed
6259 then v
6260 else
6261 let ret keymap =
6262 let h = findkeyhash c modename in
6263 KeyMap.iter (Hashtbl.replace h) keymap;
6264 doc path pan anchor c bookmarks
6266 { v with f = pkeymap ret KeyMap.empty }
6268 | Vopen (_, _, _) ->
6269 error "unexpected subelement in doc" s spos
6271 | Vclose "doc" ->
6272 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6273 { v with f = llppconfig }
6275 | Vclose _ -> error "unexpected close in doc" s spos
6277 and pkeymap ret keymap v t spos _ =
6278 match t with
6279 | Vdata | Vcdata -> v
6280 | Vend -> error "unexpected end of input in keymap" s spos
6281 | Vopen ("map", attrs, closed) ->
6282 let r, l = map_of attrs in
6283 let kss = fromstring keys_of_string spos "in" r [] in
6284 let lss = fromstring keys_of_string spos "out" l [] in
6285 let keymap =
6286 match kss with
6287 | [] -> keymap
6288 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6289 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6291 if closed
6292 then { v with f = pkeymap ret keymap }
6293 else
6294 let f () = v in
6295 { v with f = skip "map" f }
6297 | Vopen _ ->
6298 error "unexpected subelement in keymap" s spos
6300 | Vclose "keymap" ->
6301 { v with f = ret keymap }
6303 | Vclose _ -> error "unexpected close in keymap" s spos
6305 and pbookmarks path pan anchor c bookmarks v t spos _ =
6306 match t with
6307 | Vdata | Vcdata -> v
6308 | Vend -> error "unexpected end of input in bookmarks" s spos
6309 | Vopen ("item", attrs, closed) ->
6310 let titleent, spage, srely = bookmark_of attrs in
6311 let page = fromstring int_of_string spos "page" spage 0
6312 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6313 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6314 if closed
6315 then { v with f = pbookmarks path pan anchor c bookmarks }
6316 else
6317 let f () = v in
6318 { v with f = skip "item" f }
6320 | Vopen _ ->
6321 error "unexpected subelement in bookmarks" s spos
6323 | Vclose "bookmarks" ->
6324 { v with f = doc path pan anchor c bookmarks }
6326 | Vclose _ -> error "unexpected close in bookmarks" s spos
6328 and skip tag f v t spos _ =
6329 match t with
6330 | Vdata | Vcdata -> v
6331 | Vend ->
6332 error ("unexpected end of input in skipped " ^ tag) s spos
6333 | Vopen (tag', _, closed) ->
6334 if closed
6335 then v
6336 else
6337 let f' () = { v with f = skip tag f } in
6338 { v with f = skip tag' f' }
6339 | Vclose ctag ->
6340 if tag = ctag
6341 then f ()
6342 else error ("unexpected close in skipped " ^ tag) s spos
6345 parse { f = toplevel; accu = () } s;
6346 h, dc;
6349 let do_load f ic =
6351 let len = in_channel_length ic in
6352 let s = String.create len in
6353 really_input ic s 0 len;
6354 f s;
6355 with
6356 | Parse_error (msg, s, pos) ->
6357 let subs = subs s pos in
6358 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6359 failwith ("parse error: " ^ s)
6361 | exn ->
6362 failwith ("config load error: " ^ Printexc.to_string exn)
6365 let defconfpath =
6366 let dir =
6368 let dir = Filename.concat home ".config" in
6369 if Sys.is_directory dir then dir else home
6370 with _ -> home
6372 Filename.concat dir "llpp.conf"
6375 let confpath = ref defconfpath;;
6377 let load1 f =
6378 if Sys.file_exists !confpath
6379 then
6380 match
6381 (try Some (open_in_bin !confpath)
6382 with exn ->
6383 prerr_endline
6384 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6385 Printexc.to_string exn);
6386 None
6388 with
6389 | Some ic ->
6390 begin try
6391 f (do_load get ic)
6392 with exn ->
6393 prerr_endline
6394 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6395 Printexc.to_string exn);
6396 end;
6397 close_in ic;
6399 | None -> ()
6400 else
6401 f (Hashtbl.create 0, defconf)
6404 let load () =
6405 let f (h, dc) =
6406 let pc, pb, px, pa =
6408 Hashtbl.find h (Filename.basename state.path)
6409 with Not_found -> dc, [], 0, (0, 0.0)
6411 setconf defconf dc;
6412 setconf conf pc;
6413 state.bookmarks <- pb;
6414 state.x <- px;
6415 state.scrollw <- conf.scrollbw;
6416 if conf.jumpback
6417 then state.anchor <- pa;
6418 cbput state.hists.nav pa;
6420 load1 f
6423 let add_attrs bb always dc c =
6424 let ob s a b =
6425 if always || a != b
6426 then Printf.bprintf bb "\n %s='%b'" s a
6427 and oi s a b =
6428 if always || a != b
6429 then Printf.bprintf bb "\n %s='%d'" s a
6430 and oI s a b =
6431 if always || a != b
6432 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6433 and oz s a b =
6434 if always || a <> b
6435 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6436 and oF s a b =
6437 if always || a <> b
6438 then Printf.bprintf bb "\n %s='%f'" s a
6439 and oc s a b =
6440 if always || a <> b
6441 then
6442 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6443 and oC s a b =
6444 if always || a <> b
6445 then
6446 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6447 and oR s a b =
6448 if always || a <> b
6449 then
6450 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6451 and os s a b =
6452 if always || a <> b
6453 then
6454 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6455 and og s a b =
6456 if always || a <> b
6457 then
6458 match a with
6459 | None -> ()
6460 | Some (_N, _A, _B) ->
6461 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6462 and oW s a b =
6463 if always || a <> b
6464 then
6465 let v =
6466 match a with
6467 | None -> "false"
6468 | Some f ->
6469 if f = infinity
6470 then "true"
6471 else string_of_float f
6473 Printf.bprintf bb "\n %s='%s'" s v
6474 and oco s a b =
6475 if always || a <> b
6476 then
6477 match a with
6478 | Cmulti ((n, a, b), _) when n > 1 ->
6479 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6480 | Csplit (n, _) when n > 1 ->
6481 Printf.bprintf bb "\n %s='%d'" s ~-n
6482 | _ -> ()
6483 and obeco s a b =
6484 if always || a <> b
6485 then
6486 match a with
6487 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6488 | _ -> ()
6490 let w, h =
6491 if always
6492 then dc.winw, dc.winh
6493 else
6494 match state.fullscreen with
6495 | Some wh -> wh
6496 | None -> c.winw, c.winh
6498 let zoom, presentation, interpagespace, maxwait =
6499 if always
6500 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6501 else
6502 match state.mode with
6503 | Birdseye (bc, _, _, _, _) ->
6504 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6505 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6507 oi "width" w dc.winw;
6508 oi "height" h dc.winh;
6509 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6510 oi "scroll-handle-height" c.scrollh dc.scrollh;
6511 ob "case-insensitive-search" c.icase dc.icase;
6512 ob "preload" c.preload dc.preload;
6513 oi "page-bias" c.pagebias dc.pagebias;
6514 oi "scroll-step" c.scrollstep dc.scrollstep;
6515 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6516 ob "max-height-fit" c.maxhfit dc.maxhfit;
6517 ob "crop-hack" c.crophack dc.crophack;
6518 oW "throttle" maxwait dc.maxwait;
6519 ob "highlight-links" c.hlinks dc.hlinks;
6520 ob "under-cursor-info" c.underinfo dc.underinfo;
6521 oi "vertical-margin" interpagespace dc.interpagespace;
6522 oz "zoom" zoom dc.zoom;
6523 ob "presentation" presentation dc.presentation;
6524 oi "rotation-angle" c.angle dc.angle;
6525 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6526 ob "proportional-display" c.proportional dc.proportional;
6527 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6528 oi "tex-count" c.texcount dc.texcount;
6529 oi "slice-height" c.sliceheight dc.sliceheight;
6530 oi "thumbnail-width" c.thumbw dc.thumbw;
6531 ob "persistent-location" c.jumpback dc.jumpback;
6532 oc "background-color" c.bgcolor dc.bgcolor;
6533 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6534 oi "tile-width" c.tilew dc.tilew;
6535 oi "tile-height" c.tileh dc.tileh;
6536 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6537 ob "checkers" c.checkers dc.checkers;
6538 oi "aalevel" c.aalevel dc.aalevel;
6539 ob "trim-margins" c.trimmargins dc.trimmargins;
6540 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6541 os "uri-launcher" c.urilauncher dc.urilauncher;
6542 os "path-launcher" c.pathlauncher dc.pathlauncher;
6543 oC "color-space" c.colorspace dc.colorspace;
6544 ob "invert-colors" c.invert dc.invert;
6545 oF "brightness" c.colorscale dc.colorscale;
6546 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6547 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6548 oco "columns" c.columns dc.columns;
6549 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6550 os "selection-command" c.selcmd dc.selcmd;
6551 ob "update-cursor" c.updatecurs dc.updatecurs;
6552 oi "hint-font-size" c.hfsize dc.hfsize;
6553 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6556 let keymapsbuf always dc c =
6557 let bb = Buffer.create 16 in
6558 let rec loop = function
6559 | [] -> ()
6560 | (modename, h) :: rest ->
6561 let dh = findkeyhash dc modename in
6562 if always || h <> dh
6563 then (
6564 if Hashtbl.length h > 0
6565 then (
6566 if Buffer.length bb > 0
6567 then Buffer.add_char bb '\n';
6568 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6569 Hashtbl.iter (fun i o ->
6570 let isdifferent = always ||
6572 let dO = Hashtbl.find dh i in
6573 dO <> o
6574 with Not_found -> true
6576 if isdifferent
6577 then
6578 let addkm (k, m) =
6579 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6580 if Wsi.withalt m then Buffer.add_string bb "alt-";
6581 if Wsi.withshift m then Buffer.add_string bb "shift-";
6582 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6583 Buffer.add_string bb (Wsi.keyname k);
6585 let addkms l =
6586 let rec loop = function
6587 | [] -> ()
6588 | km :: [] -> addkm km
6589 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6591 loop l
6593 Buffer.add_string bb "<map in='";
6594 addkm i;
6595 match o with
6596 | KMinsrt km ->
6597 Buffer.add_string bb "' out='";
6598 addkm km;
6599 Buffer.add_string bb "'/>\n"
6601 | KMinsrl kms ->
6602 Buffer.add_string bb "' out='";
6603 addkms kms;
6604 Buffer.add_string bb "'/>\n"
6606 | KMmulti (ins, kms) ->
6607 Buffer.add_char bb ' ';
6608 addkms ins;
6609 Buffer.add_string bb "' out='";
6610 addkms kms;
6611 Buffer.add_string bb "'/>\n"
6612 ) h;
6613 Buffer.add_string bb "</keymap>";
6616 loop rest
6618 loop c.keyhashes;
6622 let save () =
6623 let uifontsize = fstate.fontsize in
6624 let bb = Buffer.create 32768 in
6625 let f (h, dc) =
6626 let dc = if conf.bedefault then conf else dc in
6627 Buffer.add_string bb "<llppconfig>\n";
6629 if String.length !fontpath > 0
6630 then
6631 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6632 uifontsize
6633 !fontpath
6634 else (
6635 if uifontsize <> 14
6636 then
6637 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6640 Buffer.add_string bb "<defaults ";
6641 add_attrs bb true dc dc;
6642 let kb = keymapsbuf true dc dc in
6643 if Buffer.length kb > 0
6644 then (
6645 Buffer.add_string bb ">\n";
6646 Buffer.add_buffer bb kb;
6647 Buffer.add_string bb "\n</defaults>\n";
6649 else Buffer.add_string bb "/>\n";
6651 let adddoc path pan anchor c bookmarks =
6652 if bookmarks == [] && c = dc && anchor = emptyanchor
6653 then ()
6654 else (
6655 Printf.bprintf bb "<doc path='%s'"
6656 (enent path 0 (String.length path));
6658 if anchor <> emptyanchor
6659 then (
6660 let n, y = anchor in
6661 Printf.bprintf bb " page='%d'" n;
6662 if y > 1e-6
6663 then
6664 Printf.bprintf bb " rely='%f'" y
6668 if pan != 0
6669 then Printf.bprintf bb " pan='%d'" pan;
6671 add_attrs bb false dc c;
6672 let kb = keymapsbuf false dc c in
6674 begin match bookmarks with
6675 | [] ->
6676 if Buffer.length kb > 0
6677 then (
6678 Buffer.add_string bb ">\n";
6679 Buffer.add_buffer bb kb;
6680 Buffer.add_string bb "\n</doc>\n";
6682 else Buffer.add_string bb "/>\n"
6683 | _ ->
6684 Buffer.add_string bb ">\n<bookmarks>\n";
6685 List.iter (fun (title, _level, (page, rely)) ->
6686 Printf.bprintf bb
6687 "<item title='%s' page='%d'"
6688 (enent title 0 (String.length title))
6689 page
6691 if rely > 1e-6
6692 then
6693 Printf.bprintf bb " rely='%f'" rely
6695 Buffer.add_string bb "/>\n";
6696 ) bookmarks;
6697 Buffer.add_string bb "</bookmarks>";
6698 if Buffer.length kb > 0
6699 then (
6700 Buffer.add_string bb "\n";
6701 Buffer.add_buffer bb kb;
6703 Buffer.add_string bb "\n</doc>\n";
6704 end;
6708 let pan, conf =
6709 match state.mode with
6710 | Birdseye (c, pan, _, _, _) ->
6711 let beyecolumns =
6712 match conf.columns with
6713 | Cmulti ((c, _, _), _) -> Some c
6714 | Csingle -> None
6715 | Csplit _ -> None
6716 and columns =
6717 match c.columns with
6718 | Cmulti (c, _) -> Cmulti (c, [||])
6719 | Csingle -> Csingle
6720 | Csplit _ -> failwith "quit from bird's eye while split"
6722 pan, { c with beyecolumns = beyecolumns; columns = columns }
6723 | _ -> state.x, conf
6725 let basename = Filename.basename state.path in
6726 adddoc basename pan (getanchor ())
6727 { conf with
6728 autoscrollstep =
6729 match state.autoscroll with
6730 | Some step -> step
6731 | None -> conf.autoscrollstep }
6732 (if conf.savebmarks then state.bookmarks else []);
6734 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6735 if basename <> path
6736 then adddoc path x y c bookmarks
6737 ) h;
6738 Buffer.add_string bb "</llppconfig>";
6740 load1 f;
6741 if Buffer.length bb > 0
6742 then
6744 let tmp = !confpath ^ ".tmp" in
6745 let oc = open_out_bin tmp in
6746 Buffer.output_buffer oc bb;
6747 close_out oc;
6748 Unix.rename tmp !confpath;
6749 with exn ->
6750 prerr_endline
6751 ("error while saving configuration: " ^ Printexc.to_string exn)
6753 end;;
6755 let () =
6756 Arg.parse
6757 (Arg.align
6758 [("-p", Arg.String (fun s -> state.password <- s) ,
6759 "<password> Set password");
6761 ("-f", Arg.String (fun s -> Config.fontpath := s),
6762 "<path> Set path to the user interface font");
6764 ("-c", Arg.String (fun s -> Config.confpath := s),
6765 "<path> Set path to the configuration file");
6767 ("-v", Arg.Unit (fun () ->
6768 Printf.printf
6769 "%s\nconfiguration path: %s\n"
6770 (version ())
6771 Config.defconfpath
6773 exit 0), " Print version and exit");
6776 (fun s -> state.path <- s)
6777 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6779 if String.length state.path = 0
6780 then (prerr_endline "file name missing"; exit 1);
6782 Config.load ();
6784 let globalkeyhash = findkeyhash conf "global" in
6785 let wsfd, winw, winh = Wsi.init (object
6786 method expose =
6787 if nogeomcmds state.geomcmds || platform == Posx
6788 then display ()
6789 else (
6790 GlClear.color (scalecolor2 conf.bgcolor);
6791 GlClear.clear [`color];
6793 method display = display ()
6794 method reshape w h = reshape w h
6795 method mouse b d x y m = mouse b d x y m
6796 method motion x y = state.mpos <- (x, y); motion x y
6797 method pmotion x y = state.mpos <- (x, y); pmotion x y
6798 method key k m =
6799 let mascm = m land (
6800 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6801 ) in
6802 match state.keystate with
6803 | KSnone ->
6804 let km = k, mascm in
6805 begin
6806 match
6807 let modehash = state.uioh#modehash in
6808 try Hashtbl.find modehash km
6809 with Not_found ->
6810 try Hashtbl.find globalkeyhash km
6811 with Not_found -> KMinsrt (k, m)
6812 with
6813 | KMinsrt (k, m) -> keyboard k m
6814 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6815 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6817 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6818 List.iter (fun (k, m) -> keyboard k m) insrt;
6819 state.keystate <- KSnone
6820 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6821 state.keystate <- KSinto (keys, insrt)
6822 | _ ->
6823 state.keystate <- KSnone
6825 method enter x y = state.mpos <- (x, y); pmotion x y
6826 method leave = state.mpos <- (-1, -1)
6827 method quit = raise Quit
6828 end) conf.winw conf.winh (platform = Posx) in
6830 state.wsfd <- wsfd;
6832 if not (
6833 List.exists GlMisc.check_extension
6834 [ "GL_ARB_texture_rectangle"
6835 ; "GL_EXT_texture_recangle"
6836 ; "GL_NV_texture_rectangle" ]
6838 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6840 let cr, sw =
6841 match Ne.pipe () with
6842 | Ne.Exn exn ->
6843 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6844 exit 1
6845 | Ne.Res rw -> rw
6846 and sr, cw =
6847 match Ne.pipe () with
6848 | Ne.Exn exn ->
6849 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6850 exit 1
6851 | Ne.Res rw -> rw
6854 cloexec cr;
6855 cloexec sw;
6856 cloexec sr;
6857 cloexec cw;
6859 setcheckers conf.checkers;
6860 redirectstderr ();
6862 init (cr, cw) (
6863 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6864 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6865 !Config.fontpath
6867 state.sr <- sr;
6868 state.sw <- sw;
6869 state.text <- "Opening " ^ state.path;
6870 reshape winw winh;
6871 opendoc state.path state.password;
6872 state.uioh <- uioh;
6874 let rec loop deadline =
6875 let r =
6876 match state.errfd with
6877 | None -> [state.sr; state.wsfd]
6878 | Some fd -> [state.sr; state.wsfd; fd]
6880 if state.redisplay
6881 then (
6882 state.redisplay <- false;
6883 display ();
6885 let timeout =
6886 let now = now () in
6887 if deadline > now
6888 then (
6889 if deadline = infinity
6890 then ~-.1.0
6891 else max 0.0 (deadline -. now)
6893 else 0.0
6895 let r, _, _ =
6896 try Unix.select r [] [] timeout
6897 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6899 begin match r with
6900 | [] ->
6901 state.ghyll None;
6902 let newdeadline =
6903 if state.ghyll == noghyll
6904 then
6905 match state.autoscroll with
6906 | Some step when step != 0 ->
6907 let y = state.y + step in
6908 let y =
6909 if y < 0
6910 then state.maxy
6911 else if y >= state.maxy then 0 else y
6913 gotoy y;
6914 if state.mode = View
6915 then state.text <- "";
6916 deadline +. 0.01
6917 | _ -> infinity
6918 else deadline +. 0.01
6920 loop newdeadline
6922 | l ->
6923 let rec checkfds = function
6924 | [] -> ()
6925 | fd :: rest when fd = state.sr ->
6926 let cmd = readcmd state.sr in
6927 act cmd;
6928 checkfds rest
6930 | fd :: rest when fd = state.wsfd ->
6931 Wsi.readresp fd;
6932 checkfds rest
6934 | fd :: rest ->
6935 let s = String.create 80 in
6936 let n = Unix.read fd s 0 80 in
6937 if conf.redirectstderr
6938 then (
6939 Buffer.add_substring state.errmsgs s 0 n;
6940 state.newerrmsgs <- true;
6941 state.redisplay <- true;
6943 else (
6944 prerr_string (String.sub s 0 n);
6945 flush stderr;
6947 checkfds rest
6949 checkfds l;
6950 let newdeadline =
6951 let deadline1 =
6952 if deadline = infinity
6953 then now () +. 0.01
6954 else deadline
6956 match state.autoscroll with
6957 | Some step when step != 0 -> deadline1
6958 | _ -> if state.ghyll == noghyll then infinity else deadline1
6960 loop newdeadline
6961 end;
6964 loop infinity;
6965 with Quit ->
6966 Config.save ();