Robustness
[llpp.git] / main.ml
blob381aa1af8e5ac0d222becc9e77d9d66511352f33
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 -> 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 maxhfit : bool
293 ; mutable crophack : bool
294 ; mutable autoscrollstep : int
295 ; mutable maxwait : float option
296 ; mutable hlinks : bool
297 ; mutable underinfo : bool
298 ; mutable interpagespace : interpagespace
299 ; mutable zoom : float
300 ; mutable presentation : bool
301 ; mutable angle : angle
302 ; mutable winw : int
303 ; mutable winh : int
304 ; mutable savebmarks : bool
305 ; mutable proportional : proportional
306 ; mutable trimmargins : trimmargins
307 ; mutable trimfuzz : irect
308 ; mutable memlimit : memsize
309 ; mutable texcount : texcount
310 ; mutable sliceheight : sliceheight
311 ; mutable thumbw : width
312 ; mutable jumpback : bool
313 ; mutable bgcolor : float * float * float
314 ; mutable bedefault : bool
315 ; mutable scrollbarinpm : bool
316 ; mutable tilew : int
317 ; mutable tileh : int
318 ; mutable mustoresize : memsize
319 ; mutable checkers : bool
320 ; mutable aalevel : int
321 ; mutable urilauncher : string
322 ; mutable pathlauncher : string
323 ; mutable colorspace : colorspace
324 ; mutable invert : bool
325 ; mutable colorscale : float
326 ; mutable redirectstderr : bool
327 ; mutable ghyllscroll : (int * int * int) option
328 ; mutable columns : columns
329 ; mutable beyecolumns : columncount option
330 ; mutable selcmd : string
331 ; mutable updatecurs : bool
332 ; mutable keyhashes : (string * keyhash) list
333 ; mutable hfsize : int
335 and columns =
336 | Csingle
337 | Cmulti of multicolumns
338 | Csplit of splitcolumns
341 type anchor = pageno * top;;
343 type outline = string * int * anchor;;
345 type rect = float * float * float * float * float * float * float * float;;
347 type tile = opaque * pixmapsize * elapsed
348 and elapsed = float;;
349 type pagemapkey = pageno * gen;;
350 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
351 and row = int
352 and col = int;;
354 let emptyanchor = (0, 0.0);;
356 type infochange = | Memused | Docinfo | Pdim;;
358 class type uioh = object
359 method display : unit
360 method key : int -> int -> uioh
361 method button : int -> bool -> int -> int -> int -> uioh
362 method motion : int -> int -> uioh
363 method pmotion : int -> int -> uioh
364 method infochanged : infochange -> unit
365 method scrollpw : (int * float * float)
366 method scrollph : (int * float * float)
367 method modehash : keyhash
368 end;;
370 type mode =
371 | Birdseye of (conf * leftx * pageno * pageno * anchor)
372 | Textentry of (textentry * onleave)
373 | View
374 | LinkNav of linktarget
375 and onleave = leavetextentrystatus -> unit
376 and leavetextentrystatus = | Cancel | Confirm
377 and helpitem = string * int * action
378 and action =
379 | Noaction
380 | Action of (uioh -> uioh)
381 and linktarget =
382 | Ltexact of (pageno * int)
383 | Ltgendir of int
386 let isbirdseye = function Birdseye _ -> true | _ -> false;;
387 let istextentry = function Textentry _ -> true | _ -> false;;
389 type currently =
390 | Idle
391 | Loading of (page * gen)
392 | Tiling of (
393 page * opaque * colorspace * angle * gen * col * row * width * height
395 | Outlining of outline list
398 let emptykeyhash = Hashtbl.create 0;;
399 let nouioh : uioh = object (self)
400 method display = ()
401 method key _ _ = self
402 method button _ _ _ _ _ = self
403 method motion _ _ = self
404 method pmotion _ _ = self
405 method infochanged _ = ()
406 method scrollpw = (0, nan, nan)
407 method scrollph = (0, nan, nan)
408 method modehash = emptykeyhash
409 end;;
411 type state =
412 { mutable sr : Unix.file_descr
413 ; mutable sw : Unix.file_descr
414 ; mutable wsfd : Unix.file_descr
415 ; mutable errfd : Unix.file_descr option
416 ; mutable stderr : Unix.file_descr
417 ; mutable errmsgs : Buffer.t
418 ; mutable newerrmsgs : bool
419 ; mutable w : int
420 ; mutable x : int
421 ; mutable y : int
422 ; mutable scrollw : int
423 ; mutable hscrollh : int
424 ; mutable anchor : anchor
425 ; mutable ranchors : (string * string * anchor) list
426 ; mutable maxy : int
427 ; mutable layout : page list
428 ; pagemap : (pagemapkey, opaque) Hashtbl.t
429 ; tilemap : (tilemapkey, tile) Hashtbl.t
430 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
431 ; mutable pdims : (pageno * width * height * leftx) list
432 ; mutable pagecount : int
433 ; mutable currently : currently
434 ; mutable mstate : mstate
435 ; mutable searchpattern : string
436 ; mutable rects : (pageno * recttype * rect) list
437 ; mutable rects1 : (pageno * recttype * rect) list
438 ; mutable text : string
439 ; mutable fullscreen : (width * height) option
440 ; mutable mode : mode
441 ; mutable uioh : uioh
442 ; mutable outlines : outline array
443 ; mutable bookmarks : outline list
444 ; mutable path : string
445 ; mutable password : string
446 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
447 ; mutable memused : memsize
448 ; mutable gen : gen
449 ; mutable throttle : (page list * int * float) option
450 ; mutable autoscroll : int option
451 ; mutable ghyll : (int option -> unit)
452 ; mutable help : helpitem array
453 ; mutable docinfo : (int * string) list
454 ; mutable texid : GlTex.texture_id option
455 ; hists : hists
456 ; mutable prevzoom : float
457 ; mutable progress : float
458 ; mutable redisplay : bool
459 ; mutable mpos : mpos
460 ; mutable keystate : keystate
461 ; mutable glinks : bool
462 ; mutable prevcolumns : (columns * float) option
464 and hists =
465 { pat : string circbuf
466 ; pag : string circbuf
467 ; nav : anchor circbuf
468 ; sel : string circbuf
472 let defconf =
473 { scrollbw = 7
474 ; scrollh = 12
475 ; icase = true
476 ; preload = true
477 ; pagebias = 0
478 ; verbose = false
479 ; debug = false
480 ; scrollstep = 24
481 ; maxhfit = true
482 ; crophack = false
483 ; autoscrollstep = 2
484 ; maxwait = None
485 ; hlinks = false
486 ; underinfo = false
487 ; interpagespace = 2
488 ; zoom = 1.0
489 ; presentation = false
490 ; angle = 0
491 ; winw = 900
492 ; winh = 900
493 ; savebmarks = true
494 ; proportional = true
495 ; trimmargins = false
496 ; trimfuzz = (0,0,0,0)
497 ; memlimit = 32 lsl 20
498 ; texcount = 256
499 ; sliceheight = 24
500 ; thumbw = 76
501 ; jumpback = true
502 ; bgcolor = (0.5, 0.5, 0.5)
503 ; bedefault = false
504 ; scrollbarinpm = true
505 ; tilew = 2048
506 ; tileh = 2048
507 ; mustoresize = 256 lsl 20
508 ; checkers = true
509 ; aalevel = 8
510 ; urilauncher =
511 (match platform with
512 | Plinux | Pfreebsd | Pdragonflybsd
513 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
514 | Posx -> "open \"%s\""
515 | Pcygwin -> "cygstart \"%s\""
516 | Punknown -> "echo %s")
517 ; pathlauncher = "lp \"%s\""
518 ; selcmd =
519 (match platform with
520 | Plinux | Pfreebsd | Pdragonflybsd
521 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
522 | Posx -> "pbcopy"
523 | Pcygwin -> "wsel"
524 | Punknown -> "cat")
525 ; colorspace = Rgb
526 ; invert = false
527 ; colorscale = 1.0
528 ; redirectstderr = false
529 ; ghyllscroll = None
530 ; columns = Csingle
531 ; beyecolumns = None
532 ; updatecurs = false
533 ; hfsize = 12
534 ; keyhashes =
535 let mk n = (n, Hashtbl.create 1) in
536 [ mk "global"
537 ; mk "info"
538 ; mk "help"
539 ; mk "outline"
540 ; mk "listview"
541 ; mk "birdseye"
542 ; mk "textentry"
543 ; mk "links"
544 ; mk "view"
549 let findkeyhash c name =
550 try List.assoc name c.keyhashes
551 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
554 let conf = { defconf with angle = defconf.angle };;
556 type fontstate =
557 { mutable fontsize : int
558 ; mutable wwidth : float
559 ; mutable maxrows : int
563 let fstate =
564 { fontsize = 14
565 ; wwidth = nan
566 ; maxrows = -1
570 let setfontsize n =
571 fstate.fontsize <- n;
572 fstate.wwidth <- measurestr fstate.fontsize "w";
573 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
576 let geturl s =
577 let colonpos = try String.index s ':' with Not_found -> -1 in
578 let len = String.length s in
579 if colonpos >= 0 && colonpos + 3 < len
580 then (
581 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
582 then
583 let schemestartpos =
584 try String.rindex_from s colonpos ' '
585 with Not_found -> -1
587 let scheme =
588 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
590 match scheme with
591 | "http" | "ftp" | "mailto" ->
592 let epos =
593 try String.index_from s colonpos ' '
594 with Not_found -> len
596 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
597 | _ -> ""
598 else ""
600 else ""
603 let gotouri uri =
604 if String.length conf.urilauncher = 0
605 then print_endline uri
606 else (
607 let url = geturl uri in
608 if String.length url = 0
609 then print_endline uri
610 else
611 let re = Str.regexp "%s" in
612 let command = Str.global_replace re url conf.urilauncher in
613 try popen command []
614 with exn ->
615 Printf.eprintf
616 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
617 flush stderr;
621 let version () =
622 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
623 (platform_to_string platform) Sys.word_size Sys.ocaml_version
626 let makehelp () =
627 let strings = version () :: "" :: Help.keys in
628 Array.of_list (
629 List.map (fun s ->
630 let url = geturl s in
631 if String.length url > 0
632 then (s, 0, Action (fun u -> gotouri url; u))
633 else (s, 0, Noaction)
634 ) strings);
637 let noghyll _ = ();;
638 let firstgeomcmds = "", [];;
640 let state =
641 { sr = Unix.stdin
642 ; sw = Unix.stdin
643 ; wsfd = Unix.stdin
644 ; errfd = None
645 ; stderr = Unix.stderr
646 ; errmsgs = Buffer.create 0
647 ; newerrmsgs = false
648 ; x = 0
649 ; y = 0
650 ; w = 0
651 ; scrollw = 0
652 ; hscrollh = 0
653 ; anchor = emptyanchor
654 ; ranchors = []
655 ; layout = []
656 ; maxy = max_int
657 ; tilelru = Queue.create ()
658 ; pagemap = Hashtbl.create 10
659 ; tilemap = Hashtbl.create 10
660 ; pdims = []
661 ; pagecount = 0
662 ; currently = Idle
663 ; mstate = Mnone
664 ; rects = []
665 ; rects1 = []
666 ; text = ""
667 ; mode = View
668 ; fullscreen = None
669 ; searchpattern = ""
670 ; outlines = [||]
671 ; bookmarks = []
672 ; path = ""
673 ; password = ""
674 ; geomcmds = firstgeomcmds
675 ; hists =
676 { nav = cbnew 10 (0, 0.0)
677 ; pat = cbnew 10 ""
678 ; pag = cbnew 10 ""
679 ; sel = cbnew 10 ""
681 ; memused = 0
682 ; gen = 0
683 ; throttle = None
684 ; autoscroll = None
685 ; ghyll = noghyll
686 ; help = makehelp ()
687 ; docinfo = []
688 ; texid = None
689 ; prevzoom = 1.0
690 ; progress = -1.0
691 ; uioh = nouioh
692 ; redisplay = true
693 ; mpos = (-1, -1)
694 ; keystate = KSnone
695 ; glinks = false
696 ; prevcolumns = None
700 let vlog fmt =
701 if conf.verbose
702 then
703 Printf.kprintf prerr_endline fmt
704 else
705 Printf.kprintf ignore fmt
708 let launchpath () =
709 if String.length conf.pathlauncher = 0
710 then print_endline state.path
711 else (
712 let re = Str.regexp "%s" in
713 let command = Str.global_replace re state.path conf.pathlauncher in
714 try popen command []
715 with exn ->
716 Printf.eprintf
717 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
718 flush stderr;
722 module Ne = struct
723 type 'a t = | Res of 'a | Exn of exn;;
725 let pipe () =
726 try Res (Unix.pipe ())
727 with exn -> Exn exn
730 let clo fd f =
731 try Unix.close fd
732 with exn -> f (Printexc.to_string exn)
735 let dup fd =
736 try Res (Unix.dup fd)
737 with exn -> Exn exn
740 let dup2 fd1 fd2 =
741 try Res (Unix.dup2 fd1 fd2)
742 with exn -> Exn exn
744 end;;
746 let redirectstderr () =
747 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
748 if conf.redirectstderr
749 then
750 match Ne.pipe () with
751 | Ne.Exn exn ->
752 dolog "failed to create stderr redirection pipes: %s"
753 (Printexc.to_string exn)
755 | Ne.Res (r, w) ->
756 begin match Ne.dup Unix.stderr with
757 | Ne.Exn exn ->
758 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
759 Ne.clo r (clofail "pipe/r");
760 Ne.clo w (clofail "pipe/w");
762 | Ne.Res dupstderr ->
763 begin match Ne.dup2 w Unix.stderr with
764 | Ne.Exn exn ->
765 dolog "failed to dup2 to stderr: %s"
766 (Printexc.to_string exn);
767 Ne.clo dupstderr (clofail "stderr duplicate");
768 Ne.clo r (clofail "redir pipe/r");
769 Ne.clo w (clofail "redir pipe/w");
771 | Ne.Res () ->
772 state.stderr <- dupstderr;
773 state.errfd <- Some r;
774 end;
776 else (
777 state.newerrmsgs <- false;
778 begin match state.errfd with
779 | Some fd ->
780 begin match Ne.dup2 state.stderr Unix.stderr with
781 | Ne.Exn exn ->
782 dolog "failed to dup2 original stderr: %s"
783 (Printexc.to_string exn)
784 | Ne.Res () ->
785 Ne.clo fd (clofail "dup of stderr");
786 Unix.dup2 state.stderr Unix.stderr;
787 state.errfd <- None;
788 end;
789 | None -> ()
790 end;
791 prerr_string (Buffer.contents state.errmsgs);
792 flush stderr;
793 Buffer.clear state.errmsgs;
797 module G =
798 struct
799 let postRedisplay who =
800 if conf.verbose
801 then prerr_endline ("redisplay for " ^ who);
802 state.redisplay <- true;
804 end;;
806 let getopaque pageno =
807 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
808 with Not_found -> None
811 let putopaque pageno opaque =
812 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
815 let pagetranslatepoint l x y =
816 let dy = y - l.pagedispy in
817 let y = dy + l.pagey in
818 let dx = x - l.pagedispx in
819 let x = dx + l.pagex in
820 (x, y);
823 let getunder x y =
824 let rec f = function
825 | l :: rest ->
826 begin match getopaque l.pageno with
827 | Some opaque ->
828 let x0 = l.pagedispx in
829 let x1 = x0 + l.pagevw in
830 let y0 = l.pagedispy in
831 let y1 = y0 + l.pagevh in
832 if y >= y0 && y <= y1 && x >= x0 && x <= x1
833 then
834 let px, py = pagetranslatepoint l x y in
835 match whatsunder opaque px py with
836 | Unone -> f rest
837 | under -> under
838 else f rest
839 | _ ->
840 f rest
842 | [] -> Unone
844 f state.layout
847 let showtext c s =
848 state.text <- Printf.sprintf "%c%s" c s;
849 G.postRedisplay "showtext";
852 let undertext = function
853 | Unone -> "none"
854 | Ulinkuri s -> s
855 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
856 | Utext s -> "font: " ^ s
857 | Uunexpected s -> "unexpected: " ^ s
858 | Ulaunch s -> "launch: " ^ s
859 | Unamed s -> "named: " ^ s
860 | Uremote (filename, pageno) ->
861 Printf.sprintf "%s: page %d" filename (pageno+1)
864 let updateunder x y =
865 match getunder x y with
866 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
867 | Ulinkuri uri ->
868 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
869 Wsi.setcursor Wsi.CURSOR_INFO
870 | Ulinkgoto (pageno, _) ->
871 if conf.underinfo
872 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
873 Wsi.setcursor Wsi.CURSOR_INFO
874 | Utext s ->
875 if conf.underinfo then showtext 'f' ("ont: " ^ s);
876 Wsi.setcursor Wsi.CURSOR_TEXT
877 | Uunexpected s ->
878 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
879 Wsi.setcursor Wsi.CURSOR_INHERIT
880 | Ulaunch s ->
881 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
882 Wsi.setcursor Wsi.CURSOR_INHERIT
883 | Unamed s ->
884 if conf.underinfo then showtext 'n' ("amed: " ^ s);
885 Wsi.setcursor Wsi.CURSOR_INHERIT
886 | Uremote (filename, pageno) ->
887 if conf.underinfo then showtext 'r'
888 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
889 Wsi.setcursor Wsi.CURSOR_INFO
892 let showlinktype under =
893 if conf.underinfo
894 then
895 match under with
896 | Unone -> ()
897 | under ->
898 let s = undertext under in
899 showtext ' ' s
902 let addchar s c =
903 let b = Buffer.create (String.length s + 1) in
904 Buffer.add_string b s;
905 Buffer.add_char b c;
906 Buffer.contents b;
909 let colorspace_of_string s =
910 match String.lowercase s with
911 | "rgb" -> Rgb
912 | "bgr" -> Bgr
913 | "gray" -> Gray
914 | _ -> failwith "invalid colorspace"
917 let int_of_colorspace = function
918 | Rgb -> 0
919 | Bgr -> 1
920 | Gray -> 2
923 let colorspace_of_int = function
924 | 0 -> Rgb
925 | 1 -> Bgr
926 | 2 -> Gray
927 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
930 let colorspace_to_string = function
931 | Rgb -> "rgb"
932 | Bgr -> "bgr"
933 | Gray -> "gray"
936 let intentry_with_suffix text key =
937 let c =
938 if key >= 32 && key < 127
939 then Char.chr key
940 else '\000'
942 match Char.lowercase c with
943 | '0' .. '9' ->
944 let text = addchar text c in
945 TEcont text
947 | 'k' | 'm' | 'g' ->
948 let text = addchar text c in
949 TEcont text
951 | _ ->
952 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
953 TEcont text
956 let multicolumns_to_string (n, a, b) =
957 if a = 0 && b = 0
958 then Printf.sprintf "%d" n
959 else Printf.sprintf "%d,%d,%d" n a b;
962 let multicolumns_of_string s =
964 (int_of_string s, 0, 0)
965 with _ ->
966 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
969 let readcmd fd =
970 let s = "xxxx" in
971 let n = Unix.read fd s 0 4 in
972 if n != 4 then failwith "incomplete read(len)";
973 let len = 0
974 lor (Char.code s.[0] lsl 24)
975 lor (Char.code s.[1] lsl 16)
976 lor (Char.code s.[2] lsl 8)
977 lor (Char.code s.[3] lsl 0)
979 let s = String.create len in
980 let n = Unix.read fd s 0 len in
981 if n != len then failwith "incomplete read(data)";
985 let btod b = if b then 1 else 0;;
987 let wcmd fmt =
988 let b = Buffer.create 16 in
989 Buffer.add_string b "llll";
990 Printf.kbprintf
991 (fun b ->
992 let s = Buffer.contents b in
993 let n = String.length s in
994 let len = n - 4 in
995 (* dolog "wcmd %S" (String.sub s 4 len); *)
996 s.[0] <- Char.chr ((len lsr 24) land 0xff);
997 s.[1] <- Char.chr ((len lsr 16) land 0xff);
998 s.[2] <- Char.chr ((len lsr 8) land 0xff);
999 s.[3] <- Char.chr (len land 0xff);
1000 let n' = Unix.write state.sw s 0 n in
1001 if n' != n then failwith "write failed";
1002 ) b fmt;
1005 let calcips h =
1006 if conf.presentation
1007 then
1008 let d = conf.winh - h in
1009 max 0 ((d + 1) / 2)
1010 else
1011 conf.interpagespace
1014 let calcheight () =
1015 let rec f pn ph pi fh l =
1016 match l with
1017 | (n, _, h, _) :: rest ->
1018 let ips = calcips h in
1019 let fh =
1020 if conf.presentation
1021 then fh+ips
1022 else (
1023 if isbirdseye state.mode && pn = 0
1024 then fh + ips
1025 else fh
1028 let fh = fh + ((n - pn) * (ph + pi)) in
1029 f n h ips fh rest;
1031 | [] ->
1032 let inc =
1033 if conf.presentation || (isbirdseye state.mode && pn = 0)
1034 then 0
1035 else -pi
1037 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1038 max 0 fh
1040 let fh = f 0 0 0 0 state.pdims in
1044 let calcheight () =
1045 match conf.columns with
1046 | Csingle -> calcheight ()
1047 | Cmulti ((c, _, _), b) ->
1048 let rec loop y h n =
1049 if n < 0
1050 then loop y h (n+1)
1051 else (
1052 if n = Array.length b
1053 then y + h
1054 else
1055 let (_, _, y', (_, _, h', _)) = b.(n) in
1056 let y = min y y'
1057 and h = max h h' in
1058 loop y h (n+1)
1061 loop max_int 0 (((Array.length b - 1) / c) * c)
1062 | Csplit (_, b) ->
1063 if Array.length b > 0
1064 then
1065 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1066 y + h
1067 else 0
1070 let getpageyh pageno =
1071 let rec f pn ph pi y l =
1072 match l with
1073 | (n, _, h, _) :: rest ->
1074 let ips = calcips h in
1075 if n >= pageno
1076 then
1077 let h = if n = pageno then h else ph in
1078 if conf.presentation && n = pageno
1079 then
1080 y + (pageno - pn) * (ph + pi) + pi, h
1081 else
1082 y + (pageno - pn) * (ph + pi), h
1083 else
1084 let y = y + (if conf.presentation then pi else 0) in
1085 let y = y + (n - pn) * (ph + pi) in
1086 f n h ips y rest
1088 | [] ->
1089 y + (pageno - pn) * (ph + pi), ph
1091 f 0 0 0 0 state.pdims
1094 let getpageyh pageno =
1095 match conf.columns with
1096 | Csingle -> getpageyh pageno
1097 | Cmulti (_, b) ->
1098 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1099 y, h
1100 | Csplit (c, b) ->
1101 let n = pageno*c in
1102 let (_, _, y, (_, _, h, _)) = b.(n) in
1103 y, h
1106 let getpagedim pageno =
1107 let rec f ppdim l =
1108 match l with
1109 | (n, _, _, _) as pdim :: rest ->
1110 if n >= pageno
1111 then (if n = pageno then pdim else ppdim)
1112 else f pdim rest
1114 | [] -> ppdim
1116 f (-1, -1, -1, -1) state.pdims
1119 let getpagey pageno = fst (getpageyh pageno);;
1121 let nogeomcmds cmds =
1122 match cmds with
1123 | s, [] -> String.length s = 0
1124 | _ -> false
1127 let layout1 y sh =
1128 let sh = sh - state.hscrollh in
1129 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1130 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1131 match pdims with
1132 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1133 let ips = calcips h in
1134 let yinc =
1135 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1136 then ips
1137 else 0
1139 (w, h, ips, xoff), rest, pdimno + 1, yinc
1140 | _ ->
1141 prev, pdims, pdimno, 0
1143 let dy = dy + yinc in
1144 let py = py + yinc in
1145 if pageno = state.pagecount || dy >= sh
1146 then
1147 accu
1148 else
1149 let vy = y + dy in
1150 if py + h <= vy - yinc
1151 then
1152 let py = py + h + ips in
1153 let dy = max 0 (py - y) in
1154 f ~pageno:(pageno+1)
1155 ~pdimno
1156 ~prev:curr
1159 ~pdims:rest
1160 ~accu
1161 else
1162 let pagey = vy - py in
1163 let pagevh = h - pagey in
1164 let pagevh = min (sh - dy) pagevh in
1165 let off = if yinc > 0 then py - vy else 0 in
1166 let py = py + h + ips in
1167 let pagex, dx =
1168 let xoff = xoff +
1169 if state.w < conf.winw - state.scrollw
1170 then (conf.winw - state.scrollw - state.w) / 2
1171 else 0
1173 let dispx = xoff + state.x in
1174 if dispx < 0
1175 then (-dispx, 0)
1176 else (0, dispx)
1178 let pagevw =
1179 let lw = w - pagex in
1180 min lw (conf.winw - state.scrollw)
1182 let e =
1183 { pageno = pageno
1184 ; pagedimno = pdimno
1185 ; pagew = w
1186 ; pageh = h
1187 ; pagex = pagex
1188 ; pagey = pagey + off
1189 ; pagevw = pagevw
1190 ; pagevh = pagevh - off
1191 ; pagedispx = dx
1192 ; pagedispy = dy + off
1193 ; pagecol = 0
1196 let accu = e :: accu in
1197 f ~pageno:(pageno+1)
1198 ~pdimno
1199 ~prev:curr
1201 ~dy:(dy+pagevh+ips)
1202 ~pdims:rest
1203 ~accu
1205 let accu =
1207 ~pageno:0
1208 ~pdimno:~-1
1209 ~prev:(0,0,0,0)
1210 ~py:0
1211 ~dy:0
1212 ~pdims:state.pdims
1213 ~accu:[]
1215 List.rev accu
1218 let layoutN ((columns, coverA, coverB), b) y sh =
1219 let sh = sh - state.hscrollh in
1220 let rec fold accu n =
1221 if n = Array.length b
1222 then accu
1223 else
1224 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1225 if (vy - y) > sh &&
1226 (n = coverA - 1
1227 || n = state.pagecount - coverB
1228 || (n - coverA) mod columns = columns - 1)
1229 then accu
1230 else
1231 let accu =
1232 if vy + h > y
1233 then
1234 let pagey = max 0 (y - vy) in
1235 let pagedispy = if pagey > 0 then 0 else vy - y in
1236 let pagedispx, pagex =
1237 let pdx =
1238 if n = coverA - 1 || n = state.pagecount - coverB
1239 then state.x + (conf.winw - state.scrollw - w) / 2
1240 else dx + xoff + state.x
1242 if pdx < 0
1243 then 0, -pdx
1244 else pdx, 0
1246 let pagevw =
1247 let vw = conf.winw - state.scrollw - pagedispx in
1248 let pw = w - pagex in
1249 min vw pw
1251 let pagevh = min (h - pagey) (sh - pagedispy) in
1252 if pagevw > 0 && pagevh > 0
1253 then
1254 let e =
1255 { pageno = n
1256 ; pagedimno = pdimno
1257 ; pagew = w
1258 ; pageh = h
1259 ; pagex = pagex
1260 ; pagey = pagey
1261 ; pagevw = pagevw
1262 ; pagevh = pagevh
1263 ; pagedispx = pagedispx
1264 ; pagedispy = pagedispy
1265 ; pagecol = 0
1268 e :: accu
1269 else
1270 accu
1271 else
1272 accu
1274 fold accu (n+1)
1276 List.rev (fold [] 0)
1279 let layoutS (columns, b) y sh =
1280 let sh = sh - state.hscrollh in
1281 let rec fold accu n =
1282 if n = Array.length b
1283 then accu
1284 else
1285 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1286 if (vy - y) > sh
1287 then accu
1288 else
1289 let accu =
1290 if vy + pageh > y
1291 then
1292 let x = xoff + state.x in
1293 let pagey = max 0 (y - vy) in
1294 let pagedispy = if pagey > 0 then 0 else vy - y in
1295 let pagedispx, pagex =
1296 if px = 0
1297 then (
1298 if x < 0
1299 then 0, -x
1300 else x, 0
1302 else (
1303 let px = px - x in
1304 if px < 0
1305 then -px, 0
1306 else 0, px
1309 let pagecolw = pagew/columns in
1310 let pagedispx =
1311 if pagecolw < conf.winw
1312 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1313 else pagedispx
1315 let pagevw =
1316 let vw = conf.winw - pagedispx - state.scrollw in
1317 let pw = pagew - pagex in
1318 min vw pw
1320 let pagevw = min pagevw pagecolw in
1321 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1322 if pagevw > 0 && pagevh > 0
1323 then
1324 let e =
1325 { pageno = n/columns
1326 ; pagedimno = pdimno
1327 ; pagew = pagew
1328 ; pageh = pageh
1329 ; pagex = pagex
1330 ; pagey = pagey
1331 ; pagevw = pagevw
1332 ; pagevh = pagevh
1333 ; pagedispx = pagedispx
1334 ; pagedispy = pagedispy
1335 ; pagecol = n mod columns
1338 e :: accu
1339 else
1340 accu
1341 else
1342 accu
1344 fold accu (n+1)
1346 List.rev (fold [] 0)
1349 let layout y sh =
1350 if nogeomcmds state.geomcmds
1351 then
1352 match conf.columns with
1353 | Csingle -> layout1 y sh
1354 | Cmulti c -> layoutN c y sh
1355 | Csplit s -> layoutS s y sh
1356 else []
1359 let clamp incr =
1360 let y = state.y + incr in
1361 let y = max 0 y in
1362 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1366 let itertiles l f =
1367 let tilex = l.pagex mod conf.tilew in
1368 let tiley = l.pagey mod conf.tileh in
1370 let col = l.pagex / conf.tilew in
1371 let row = l.pagey / conf.tileh in
1373 let rec rowloop row y0 dispy h =
1374 if h = 0
1375 then ()
1376 else (
1377 let dh = conf.tileh - y0 in
1378 let dh = min h dh in
1379 let rec colloop col x0 dispx w =
1380 if w = 0
1381 then ()
1382 else (
1383 let dw = conf.tilew - x0 in
1384 let dw = min w dw in
1386 f col row dispx dispy x0 y0 dw dh;
1387 colloop (col+1) 0 (dispx+dw) (w-dw)
1390 colloop col tilex l.pagedispx l.pagevw;
1391 rowloop (row+1) 0 (dispy+dh) (h-dh)
1394 if l.pagevw > 0 && l.pagevh > 0
1395 then rowloop row tiley l.pagedispy l.pagevh;
1398 let gettileopaque l col row =
1399 let key =
1400 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1402 try Some (Hashtbl.find state.tilemap key)
1403 with Not_found -> None
1406 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1407 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1408 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1411 let drawtiles l color =
1412 GlDraw.color color;
1413 let f col row x y tilex tiley w h =
1414 match gettileopaque l col row with
1415 | Some (opaque, _, t) ->
1416 let params = x, y, w, h, tilex, tiley in
1417 if conf.invert
1418 then (
1419 Gl.enable `blend;
1420 GlFunc.blend_func `zero `one_minus_src_color;
1422 drawtile params opaque;
1423 if conf.invert
1424 then Gl.disable `blend;
1425 if conf.debug
1426 then (
1427 let s = Printf.sprintf
1428 "%d[%d,%d] %f sec"
1429 l.pageno col row t
1431 let w = measurestr fstate.fontsize s in
1432 GlMisc.push_attrib [`current];
1433 GlDraw.color (0.0, 0.0, 0.0);
1434 GlDraw.rect
1435 (float (x-2), float (y-2))
1436 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1437 GlDraw.color (1.0, 1.0, 1.0);
1438 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1439 GlMisc.pop_attrib ();
1442 | _ ->
1443 let w =
1444 let lw = conf.winw - state.scrollw - x in
1445 min lw w
1446 and h =
1447 let lh = conf.winh - y in
1448 min lh h
1450 Gl.enable `texture_2d;
1451 begin match state.texid with
1452 | Some id ->
1453 GlTex.bind_texture `texture_2d id;
1454 let x0 = float x
1455 and y0 = float y
1456 and x1 = float (x+w)
1457 and y1 = float (y+h) in
1459 let tw = float w /. 64.0
1460 and th = float h /. 64.0 in
1461 let tx0 = float tilex /. 64.0
1462 and ty0 = float tiley /. 64.0 in
1463 let tx1 = tx0 +. tw
1464 and ty1 = ty0 +. th in
1465 GlDraw.begins `quads;
1466 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1467 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1468 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1469 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1470 GlDraw.ends ();
1472 Gl.disable `texture_2d;
1473 | None ->
1474 GlDraw.color (1.0, 1.0, 1.0);
1475 GlDraw.rect
1476 (float x, float y)
1477 (float (x+w), float (y+h));
1478 end;
1479 if w > 128 && h > fstate.fontsize + 10
1480 then (
1481 GlDraw.color (0.0, 0.0, 0.0);
1482 let c, r =
1483 if conf.verbose
1484 then (col*conf.tilew, row*conf.tileh)
1485 else col, row
1487 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1489 GlDraw.color color;
1491 itertiles l f
1494 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1496 let tilevisible1 l x y =
1497 let ax0 = l.pagex
1498 and ax1 = l.pagex + l.pagevw
1499 and ay0 = l.pagey
1500 and ay1 = l.pagey + l.pagevh in
1502 let bx0 = x
1503 and by0 = y in
1504 let bx1 = min (bx0 + conf.tilew) l.pagew
1505 and by1 = min (by0 + conf.tileh) l.pageh in
1507 let rx0 = max ax0 bx0
1508 and ry0 = max ay0 by0
1509 and rx1 = min ax1 bx1
1510 and ry1 = min ay1 by1 in
1512 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1513 nonemptyintersection
1516 let tilevisible layout n x y =
1517 let rec findpageinlayout m = function
1518 | l :: rest when l.pageno = n ->
1519 tilevisible1 l x y || (
1520 match conf.columns with
1521 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1522 | _ -> false
1524 | _ :: rest -> findpageinlayout 0 rest
1525 | [] -> false
1527 findpageinlayout 0 layout;
1530 let tileready l x y =
1531 tilevisible1 l x y &&
1532 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1535 let tilepage n p layout =
1536 let rec loop = function
1537 | l :: rest ->
1538 if l.pageno = n
1539 then
1540 let f col row _ _ _ _ _ _ =
1541 if state.currently = Idle
1542 then
1543 match gettileopaque l col row with
1544 | Some _ -> ()
1545 | None ->
1546 let x = col*conf.tilew
1547 and y = row*conf.tileh in
1548 let w =
1549 let w = l.pagew - x in
1550 min w conf.tilew
1552 let h =
1553 let h = l.pageh - y in
1554 min h conf.tileh
1556 wcmd "tile %s %d %d %d %d" p x y w h;
1557 state.currently <-
1558 Tiling (
1559 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1560 conf.tilew, conf.tileh
1563 itertiles l f;
1564 else
1565 loop rest
1567 | [] -> ()
1569 if nogeomcmds state.geomcmds
1570 then loop layout;
1573 let preloadlayout visiblepages =
1574 let presentation = conf.presentation in
1575 let interpagespace = conf.interpagespace in
1576 let maxy = state.maxy in
1577 conf.presentation <- false;
1578 conf.interpagespace <- 0;
1579 state.maxy <- calcheight ();
1580 let y =
1581 match visiblepages with
1582 | [] -> if state.y >= maxy then maxy else 0
1583 | l :: _ -> getpagey l.pageno + l.pagey
1585 let y = if y < conf.winh then 0 else y - conf.winh in
1586 let h = state.y - y + conf.winh*3 in
1587 let pages = layout y h in
1588 conf.presentation <- presentation;
1589 conf.interpagespace <- interpagespace;
1590 state.maxy <- maxy;
1591 pages;
1594 let load pages =
1595 let rec loop pages =
1596 if state.currently != Idle
1597 then ()
1598 else
1599 match pages with
1600 | l :: rest ->
1601 begin match getopaque l.pageno with
1602 | None ->
1603 wcmd "page %d %d" l.pageno l.pagedimno;
1604 state.currently <- Loading (l, state.gen);
1605 | Some opaque ->
1606 tilepage l.pageno opaque pages;
1607 loop rest
1608 end;
1609 | _ -> ()
1611 if nogeomcmds state.geomcmds
1612 then loop pages
1615 let preload pages =
1616 load pages;
1617 if conf.preload && state.currently = Idle
1618 then load (preloadlayout pages);
1621 let layoutready layout =
1622 let rec fold all ls =
1623 all && match ls with
1624 | l :: rest ->
1625 let seen = ref false in
1626 let allvisible = ref true in
1627 let foo col row _ _ _ _ _ _ =
1628 seen := true;
1629 allvisible := !allvisible &&
1630 begin match gettileopaque l col row with
1631 | Some _ -> true
1632 | None -> false
1635 itertiles l foo;
1636 fold (!seen && !allvisible) rest
1637 | [] -> true
1639 let alltilesvisible = fold true layout in
1640 alltilesvisible;
1643 let gotoy y =
1644 let y = bound y 0 state.maxy in
1645 let y, layout, proceed =
1646 match conf.maxwait with
1647 | Some time when state.ghyll == noghyll ->
1648 begin match state.throttle with
1649 | None ->
1650 let layout = layout y conf.winh in
1651 let ready = layoutready layout in
1652 if not ready
1653 then (
1654 load layout;
1655 state.throttle <- Some (layout, y, now ());
1657 else G.postRedisplay "gotoy showall (None)";
1658 y, layout, ready
1659 | Some (_, _, started) ->
1660 let dt = now () -. started in
1661 if dt > time
1662 then (
1663 state.throttle <- None;
1664 let layout = layout y conf.winh in
1665 load layout;
1666 G.postRedisplay "maxwait";
1667 y, layout, true
1669 else -1, [], false
1672 | _ ->
1673 let layout = layout y conf.winh in
1674 if true || layoutready layout
1675 then G.postRedisplay "gotoy ready";
1676 y, layout, true
1678 if proceed
1679 then (
1680 state.y <- y;
1681 state.layout <- layout;
1682 begin match state.mode with
1683 | LinkNav (Ltexact (pageno, linkno)) ->
1684 let rec loop = function
1685 | [] ->
1686 state.mode <- LinkNav (Ltgendir 0)
1687 | l :: _ when l.pageno = pageno ->
1688 begin match getopaque pageno with
1689 | None ->
1690 state.mode <- LinkNav (Ltgendir 0)
1691 | Some opaque ->
1692 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1693 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1694 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1695 then state.mode <- LinkNav (Ltgendir 0)
1697 | _ :: rest -> loop rest
1699 loop layout
1700 | _ -> ()
1701 end;
1702 begin match state.mode with
1703 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1704 if not (pagevisible layout pageno)
1705 then (
1706 match state.layout with
1707 | [] -> ()
1708 | l :: _ ->
1709 state.mode <- Birdseye (
1710 conf, leftx, l.pageno, hooverpageno, anchor
1713 | LinkNav (Ltgendir dir as lt) ->
1714 let linknav =
1715 let rec loop = function
1716 | [] -> lt
1717 | l :: rest ->
1718 match getopaque l.pageno with
1719 | None -> loop rest
1720 | Some opaque ->
1721 let link =
1722 let ld =
1723 if dir = 0
1724 then LDfirstvisible (l.pagex, l.pagey, dir)
1725 else (
1726 if dir > 0 then LDfirst else LDlast
1729 findlink opaque ld
1731 match link with
1732 | Lnotfound -> loop rest
1733 | Lfound n ->
1734 showlinktype (getlink opaque n);
1735 Ltexact (l.pageno, n)
1737 loop state.layout
1739 state.mode <- LinkNav linknav
1740 | _ -> ()
1741 end;
1742 preload layout;
1744 state.ghyll <- noghyll;
1745 if conf.updatecurs
1746 then (
1747 let mx, my = state.mpos in
1748 updateunder mx my;
1752 let conttiling pageno opaque =
1753 tilepage pageno opaque
1754 (if conf.preload then preloadlayout state.layout else state.layout)
1757 let gotoy_and_clear_text y =
1758 if not conf.verbose then state.text <- "";
1759 gotoy y;
1762 let getanchor () =
1763 match state.layout with
1764 | [] -> emptyanchor
1765 | l :: _ ->
1766 let coloff = l.pagecol * l.pageh in
1767 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1770 let getanchory (n, top) =
1771 let y, h = getpageyh n in
1772 y + (truncate (top *. float h));
1775 let gotoanchor anchor =
1776 gotoy (getanchory anchor);
1779 let addnav () =
1780 cbput state.hists.nav (getanchor ());
1783 let getnav dir =
1784 let anchor = cbgetc state.hists.nav dir in
1785 getanchory anchor;
1788 let gotoghyll y =
1789 let rec scroll f n a b =
1790 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1791 let snake f a b =
1792 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1793 if f < a
1794 then s (float f /. float a)
1795 else (
1796 if f > b
1797 then 1.0 -. s ((float (f-b) /. float (n-b)))
1798 else 1.0
1801 snake f a b
1802 and summa f n a b =
1803 (* courtesy:
1804 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1805 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1806 let iv1 = iv f in
1807 let ins = float a *. iv1
1808 and outs = float (n-b) *. iv1 in
1809 let ones = b - a in
1810 ins +. outs +. float ones
1812 let rec set (_N, _A, _B) y sy =
1813 let sum = summa 1.0 _N _A _B in
1814 let dy = float (y - sy) in
1815 state.ghyll <- (
1816 let rec gf n y1 o =
1817 if n >= _N
1818 then state.ghyll <- noghyll
1819 else
1820 let go n =
1821 let s = scroll n _N _A _B in
1822 let y1 = y1 +. ((s *. dy) /. sum) in
1823 gotoy_and_clear_text (truncate y1);
1824 state.ghyll <- gf (n+1) y1;
1826 match o with
1827 | None -> go n
1828 | Some y' -> set (_N/2, 0, 0) y' state.y
1830 gf 0 (float state.y)
1833 match conf.ghyllscroll with
1834 | None ->
1835 gotoy_and_clear_text y
1836 | Some nab ->
1837 if state.ghyll == noghyll
1838 then set nab y state.y
1839 else state.ghyll (Some y)
1842 let gotopage n top =
1843 let y, h = getpageyh n in
1844 let y = y + (truncate (top *. float h)) in
1845 gotoghyll y
1848 let gotopage1 n top =
1849 let y = getpagey n in
1850 let y = y + top in
1851 gotoghyll y
1854 let invalidate s f =
1855 state.layout <- [];
1856 state.pdims <- [];
1857 state.rects <- [];
1858 state.rects1 <- [];
1859 match state.geomcmds with
1860 | ps, [] when String.length ps = 0 ->
1861 f ();
1862 state.geomcmds <- s, [];
1864 | ps, [] ->
1865 state.geomcmds <- ps, [s, f];
1867 | ps, (s', _) :: rest when s' = s ->
1868 state.geomcmds <- ps, ((s, f) :: rest);
1870 | ps, cmds ->
1871 state.geomcmds <- ps, ((s, f) :: cmds);
1874 let opendoc path password =
1875 state.path <- path;
1876 state.password <- password;
1877 state.gen <- state.gen + 1;
1878 state.docinfo <- [];
1880 setaalevel conf.aalevel;
1881 Wsi.settitle ("llpp " ^ Filename.basename path);
1882 wcmd "open %s\000%s\000" path password;
1883 invalidate "reqlayout"
1884 (fun () ->
1885 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1888 let scalecolor c =
1889 let c = c *. conf.colorscale in
1890 (c, c, c);
1893 let scalecolor2 (r, g, b) =
1894 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1897 let docolumns = function
1898 | Csingle -> ()
1900 | Cmulti ((columns, coverA, coverB), _) ->
1901 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1902 let rec loop pageno pdimno pdim x y rowh pdims =
1903 let rec fixrow m = if m = pageno then () else
1904 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1905 if h < rowh
1906 then (
1907 let y = y + (rowh - h) / 2 in
1908 a.(m) <- (pdimno, x, y, pdim);
1910 fixrow (m+1)
1912 if pageno = state.pagecount
1913 then fixrow (((pageno - 1) / columns) * columns)
1914 else
1915 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1916 match pdims with
1917 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1918 pdimno+1, pdim, rest
1919 | _ ->
1920 pdimno, pdim, pdims
1922 let x, y, rowh' =
1923 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1924 then (
1925 (conf.winw - state.scrollw - w) / 2,
1926 y + rowh + conf.interpagespace, h
1928 else (
1929 if (pageno - coverA) mod columns = 0
1930 then 0, y + rowh + conf.interpagespace, h
1931 else x, y, max rowh h
1934 if pageno > 1 && (pageno - coverA) mod columns = 0
1935 then fixrow (pageno - columns);
1936 a.(pageno) <- (pdimno, x, y, pdim);
1937 let x = x + w + xoff*2 + conf.interpagespace in
1938 loop (pageno+1) pdimno pdim x y rowh' pdims
1940 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1941 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1943 | Csplit (c, _) ->
1944 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1945 let rec loop pageno pdimno pdim y pdims =
1946 if pageno = state.pagecount
1947 then ()
1948 else
1949 let pdimno, ((_, w, h, _) as pdim), pdims =
1950 match pdims with
1951 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1952 pdimno+1, pdim, rest
1953 | _ ->
1954 pdimno, pdim, pdims
1956 let cw = w / c in
1957 let rec loop1 n x y =
1958 if n = c then y else (
1959 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1960 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1963 let y = loop1 0 0 y in
1964 loop (pageno+1) pdimno pdim y pdims
1966 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1967 conf.columns <- Csplit (c, a);
1970 let represent () =
1971 docolumns conf.columns;
1972 state.maxy <- calcheight ();
1973 state.hscrollh <-
1974 if state.w <= conf.winw - state.scrollw
1975 then 0
1976 else state.scrollw
1978 match state.mode with
1979 | Birdseye (_, _, pageno, _, _) ->
1980 let y, h = getpageyh pageno in
1981 let top = (conf.winh - h) / 2 in
1982 gotoy (max 0 (y - top))
1983 | _ -> gotoanchor state.anchor
1986 let reshape w h =
1987 GlDraw.viewport 0 0 w h;
1988 let firsttime = state.geomcmds == firstgeomcmds in
1989 if not firsttime && nogeomcmds state.geomcmds
1990 then state.anchor <- getanchor ();
1992 conf.winw <- w;
1993 let w = truncate (float w *. conf.zoom) - state.scrollw in
1994 let w = max w 2 in
1995 conf.winh <- h;
1996 setfontsize fstate.fontsize;
1997 GlMat.mode `modelview;
1998 GlMat.load_identity ();
2000 GlMat.mode `projection;
2001 GlMat.load_identity ();
2002 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2003 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2004 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2006 let relx =
2007 if conf.zoom <= 1.0
2008 then 0.0
2009 else float state.x /. float state.w
2011 invalidate "geometry"
2012 (fun () ->
2013 state.w <- w;
2014 if not firsttime
2015 then state.x <- truncate (relx *. float w);
2016 let w =
2017 match conf.columns with
2018 | Csingle -> w
2019 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2020 | Csplit (c, _) -> w * c
2022 wcmd "geometry %d %d" w h);
2025 let enttext () =
2026 let len = String.length state.text in
2027 let drawstring s =
2028 let hscrollh =
2029 match state.mode with
2030 | Textentry _
2031 | View ->
2032 let h, _, _ = state.uioh#scrollpw in
2034 | _ -> 0
2036 let rect x w =
2037 GlDraw.rect
2038 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2039 (x+.w, float (conf.winh - hscrollh))
2042 let w = float (conf.winw - state.scrollw - 1) in
2043 if state.progress >= 0.0 && state.progress < 1.0
2044 then (
2045 GlDraw.color (0.3, 0.3, 0.3);
2046 let w1 = w *. state.progress in
2047 rect 0.0 w1;
2048 GlDraw.color (0.0, 0.0, 0.0);
2049 rect w1 (w-.w1)
2051 else (
2052 GlDraw.color (0.0, 0.0, 0.0);
2053 rect 0.0 w;
2056 GlDraw.color (1.0, 1.0, 1.0);
2057 drawstring fstate.fontsize
2058 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2060 let s =
2061 match state.mode with
2062 | Textentry ((prefix, text, _, _, _, _), _) ->
2063 let s =
2064 if len > 0
2065 then
2066 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2067 else
2068 Printf.sprintf "%s%s_" prefix text
2072 | _ -> state.text
2074 let s =
2075 if state.newerrmsgs
2076 then (
2077 if not (istextentry state.mode)
2078 then
2079 let s1 = "(press 'e' to review error messasges)" in
2080 if String.length s > 0 then s ^ " " ^ s1 else s1
2081 else s
2083 else s
2085 if String.length s > 0
2086 then drawstring s
2089 let gctiles () =
2090 let len = Queue.length state.tilelru in
2091 let rec loop qpos =
2092 if state.memused <= conf.memlimit
2093 then ()
2094 else (
2095 if qpos < len
2096 then
2097 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2098 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2099 let (_, pw, ph, _) = getpagedim n in
2101 gen = state.gen
2102 && colorspace = conf.colorspace
2103 && angle = conf.angle
2104 && pagew = pw
2105 && pageh = ph
2106 && (
2107 let layout =
2108 match state.throttle with
2109 | None ->
2110 if conf.preload
2111 then preloadlayout state.layout
2112 else state.layout
2113 | Some (layout, _, _) ->
2114 layout
2116 let x = col*conf.tilew
2117 and y = row*conf.tileh in
2118 tilevisible layout n x y
2120 then Queue.push lruitem state.tilelru
2121 else (
2122 wcmd "freetile %s" p;
2123 state.memused <- state.memused - s;
2124 state.uioh#infochanged Memused;
2125 Hashtbl.remove state.tilemap k;
2127 loop (qpos+1)
2130 loop 0
2133 let flushtiles () =
2134 Queue.iter (fun (k, p, s) ->
2135 wcmd "freetile %s" p;
2136 state.memused <- state.memused - s;
2137 state.uioh#infochanged Memused;
2138 Hashtbl.remove state.tilemap k;
2139 ) state.tilelru;
2140 Queue.clear state.tilelru;
2141 load state.layout;
2144 let logcurrently = function
2145 | Idle -> dolog "Idle"
2146 | Loading (l, gen) ->
2147 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2148 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2149 dolog
2150 "Tiling %d[%d,%d] page=%s cs=%s angle"
2151 l.pageno col row pageopaque
2152 (colorspace_to_string colorspace)
2154 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2155 angle gen conf.angle state.gen
2156 tilew tileh
2157 conf.tilew conf.tileh
2159 | Outlining _ ->
2160 dolog "outlining"
2163 let act cmds =
2164 (* dolog "%S" cmds; *)
2165 let op, args =
2166 let spacepos =
2167 try String.index cmds ' '
2168 with Not_found -> -1
2170 if spacepos = -1
2171 then cmds, ""
2172 else
2173 let l = String.length cmds in
2174 let op = String.sub cmds 0 spacepos in
2175 op, begin
2176 if l - spacepos < 2 then ""
2177 else String.sub cmds (spacepos+1) (l-spacepos-1)
2180 match op with
2181 | "clear" ->
2182 state.uioh#infochanged Pdim;
2183 state.pdims <- [];
2185 | "clearrects" ->
2186 state.rects <- state.rects1;
2187 G.postRedisplay "clearrects";
2189 | "continue" ->
2190 let n =
2191 try Scanf.sscanf args "%u" (fun n -> n)
2192 with exn ->
2193 dolog "error processing 'continue' %S: %s"
2194 cmds (Printexc.to_string exn);
2195 exit 1;
2197 state.pagecount <- n;
2198 begin match state.currently with
2199 | Outlining l ->
2200 state.currently <- Idle;
2201 state.outlines <- Array.of_list (List.rev l)
2202 | _ -> ()
2203 end;
2205 let cur, cmds = state.geomcmds in
2206 if String.length cur = 0
2207 then failwith "umpossible";
2209 begin match List.rev cmds with
2210 | [] ->
2211 state.geomcmds <- "", [];
2212 represent ();
2213 | (s, f) :: rest ->
2214 f ();
2215 state.geomcmds <- s, List.rev rest;
2216 end;
2217 if conf.maxwait = None
2218 then G.postRedisplay "continue";
2220 | "title" ->
2221 Wsi.settitle args
2223 | "msg" ->
2224 showtext ' ' args
2226 | "vmsg" ->
2227 if conf.verbose
2228 then showtext ' ' args
2230 | "progress" ->
2231 let progress, text =
2233 Scanf.sscanf args "%f %n"
2234 (fun f pos ->
2235 f, String.sub args pos (String.length args - pos))
2236 with exn ->
2237 dolog "error processing 'progress' %S: %s"
2238 cmds (Printexc.to_string exn);
2239 exit 1;
2241 state.text <- text;
2242 state.progress <- progress;
2243 G.postRedisplay "progress"
2245 | "firstmatch" ->
2246 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2248 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2249 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2250 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2251 with exn ->
2252 dolog "error processing 'firstmatch' %S: %s"
2253 cmds (Printexc.to_string exn);
2254 exit 1;
2256 let y = (getpagey pageno) + truncate y0 in
2257 addnav ();
2258 gotoy y;
2259 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2261 | "match" ->
2262 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2264 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2265 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2266 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2267 with exn ->
2268 dolog "error processing 'match' %S: %s"
2269 cmds (Printexc.to_string exn);
2270 exit 1;
2272 state.rects1 <-
2273 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2275 | "page" ->
2276 let pageopaque, t =
2278 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2279 with exn ->
2280 dolog "error processing 'page' %S: %s"
2281 cmds (Printexc.to_string exn);
2282 exit 1;
2284 begin match state.currently with
2285 | Loading (l, gen) ->
2286 vlog "page %d took %f sec" l.pageno t;
2287 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2288 begin match state.throttle with
2289 | None ->
2290 let preloadedpages =
2291 if conf.preload
2292 then preloadlayout state.layout
2293 else state.layout
2295 let evict () =
2296 let module IntSet =
2297 Set.Make (struct type t = int let compare = (-) end) in
2298 let set =
2299 List.fold_left (fun s l -> IntSet.add l.pageno s)
2300 IntSet.empty preloadedpages
2302 let evictedpages =
2303 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2304 if not (IntSet.mem pageno set)
2305 then (
2306 wcmd "freepage %s" opaque;
2307 key :: accu
2309 else accu
2310 ) state.pagemap []
2312 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2314 evict ();
2315 state.currently <- Idle;
2316 if gen = state.gen
2317 then (
2318 tilepage l.pageno pageopaque state.layout;
2319 load state.layout;
2320 load preloadedpages;
2321 if pagevisible state.layout l.pageno
2322 && layoutready state.layout
2323 then G.postRedisplay "page";
2326 | Some (layout, _, _) ->
2327 state.currently <- Idle;
2328 tilepage l.pageno pageopaque layout;
2329 load state.layout
2330 end;
2332 | _ ->
2333 dolog "Inconsistent loading state";
2334 logcurrently state.currently;
2335 exit 1
2338 | "tile" ->
2339 let (x, y, opaque, size, t) =
2341 Scanf.sscanf args "%u %u %s %u %f"
2342 (fun x y p size t -> (x, y, p, size, t))
2343 with exn ->
2344 dolog "error processing 'tile' %S: %s"
2345 cmds (Printexc.to_string exn);
2346 exit 1;
2348 begin match state.currently with
2349 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2350 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2352 if tilew != conf.tilew || tileh != conf.tileh
2353 then (
2354 wcmd "freetile %s" opaque;
2355 state.currently <- Idle;
2356 load state.layout;
2358 else (
2359 puttileopaque l col row gen cs angle opaque size t;
2360 state.memused <- state.memused + size;
2361 state.uioh#infochanged Memused;
2362 gctiles ();
2363 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2364 opaque, size) state.tilelru;
2366 let layout =
2367 match state.throttle with
2368 | None -> state.layout
2369 | Some (layout, _, _) -> layout
2372 state.currently <- Idle;
2373 if gen = state.gen
2374 && conf.colorspace = cs
2375 && conf.angle = angle
2376 && tilevisible layout l.pageno x y
2377 then conttiling l.pageno pageopaque;
2379 begin match state.throttle with
2380 | None ->
2381 preload state.layout;
2382 if gen = state.gen
2383 && conf.colorspace = cs
2384 && conf.angle = angle
2385 && tilevisible state.layout l.pageno x y
2386 then G.postRedisplay "tile nothrottle";
2388 | Some (layout, y, _) ->
2389 let ready = layoutready layout in
2390 if ready
2391 then (
2392 state.y <- y;
2393 state.layout <- layout;
2394 state.throttle <- None;
2395 G.postRedisplay "throttle";
2397 else load layout;
2398 end;
2401 | _ ->
2402 dolog "Inconsistent tiling state";
2403 logcurrently state.currently;
2404 exit 1
2407 | "pdim" ->
2408 let pdim =
2410 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2411 with exn ->
2412 dolog "error processing 'pdim' %S: %s"
2413 cmds (Printexc.to_string exn);
2414 exit 1;
2416 state.uioh#infochanged Pdim;
2417 state.pdims <- pdim :: state.pdims
2419 | "o" ->
2420 let (l, n, t, h, pos) =
2422 Scanf.sscanf args "%u %u %d %u %n"
2423 (fun l n t h pos -> l, n, t, h, pos)
2424 with exn ->
2425 dolog "error processing 'o' %S: %s"
2426 cmds (Printexc.to_string exn);
2427 exit 1;
2429 let s = String.sub args pos (String.length args - pos) in
2430 let outline = (s, l, (n, float t /. float h)) in
2431 begin match state.currently with
2432 | Outlining outlines ->
2433 state.currently <- Outlining (outline :: outlines)
2434 | Idle ->
2435 state.currently <- Outlining [outline]
2436 | currently ->
2437 dolog "invalid outlining state";
2438 logcurrently currently
2441 | "info" ->
2442 state.docinfo <- (1, args) :: state.docinfo
2444 | "infoend" ->
2445 state.uioh#infochanged Docinfo;
2446 state.docinfo <- List.rev state.docinfo
2448 | _ ->
2449 dolog "unknown cmd `%S'" cmds
2452 let onhist cb =
2453 let rc = cb.rc in
2454 let action = function
2455 | HCprev -> cbget cb ~-1
2456 | HCnext -> cbget cb 1
2457 | HCfirst -> cbget cb ~-(cb.rc)
2458 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2459 and cancel () = cb.rc <- rc
2460 in (action, cancel)
2463 let search pattern forward =
2464 if String.length pattern > 0
2465 then
2466 let pn, py =
2467 match state.layout with
2468 | [] -> 0, 0
2469 | l :: _ ->
2470 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2472 wcmd "search %d %d %d %d,%s\000"
2473 (btod conf.icase) pn py (btod forward) pattern;
2476 let intentry text key =
2477 let c =
2478 if key >= 32 && key < 127
2479 then Char.chr key
2480 else '\000'
2482 match c with
2483 | '0' .. '9' ->
2484 let text = addchar text c in
2485 TEcont text
2487 | _ ->
2488 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2489 TEcont text
2492 let linknentry text key =
2493 let c =
2494 if key >= 32 && key < 127
2495 then Char.chr key
2496 else '\000'
2498 match c with
2499 | 'a' .. 'z' ->
2500 let text = addchar text c in
2501 TEcont text
2503 | _ ->
2504 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2505 TEcont text
2508 let linkndone f s =
2509 if String.length s > 0
2510 then (
2511 let n =
2512 let l = String.length s in
2513 let rec loop pos n = if pos = l then n else
2514 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2515 loop (pos+1) (n*26 + m)
2516 in loop 0 0
2518 let rec loop n = function
2519 | [] -> ()
2520 | l :: rest ->
2521 match getopaque l.pageno with
2522 | None -> loop n rest
2523 | Some opaque ->
2524 let m = getlinkcount opaque in
2525 if n < m
2526 then (
2527 let under = getlink opaque n in
2528 f under
2530 else loop (n-m) rest
2532 loop n state.layout;
2536 let textentry text key =
2537 if key land 0xff00 = 0xff00
2538 then TEcont text
2539 else TEcont (text ^ Wsi.toutf8 key)
2542 let reqlayout angle proportional =
2543 match state.throttle with
2544 | None ->
2545 if nogeomcmds state.geomcmds
2546 then state.anchor <- getanchor ();
2547 conf.angle <- angle mod 360;
2548 if conf.angle != 0
2549 then (
2550 match state.mode with
2551 | LinkNav _ -> state.mode <- View
2552 | _ -> ()
2554 conf.proportional <- proportional;
2555 invalidate "reqlayout"
2556 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2557 | _ -> ()
2560 let settrim trimmargins trimfuzz =
2561 if nogeomcmds state.geomcmds
2562 then state.anchor <- getanchor ();
2563 conf.trimmargins <- trimmargins;
2564 conf.trimfuzz <- trimfuzz;
2565 let x0, y0, x1, y1 = trimfuzz in
2566 invalidate "settrim"
2567 (fun () ->
2568 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2569 Hashtbl.iter (fun _ opaque ->
2570 wcmd "freepage %s" opaque;
2571 ) state.pagemap;
2572 Hashtbl.clear state.pagemap;
2575 let setzoom zoom =
2576 match state.throttle with
2577 | None ->
2578 let zoom = max 0.01 zoom in
2579 if zoom <> conf.zoom
2580 then (
2581 state.prevzoom <- conf.zoom;
2582 conf.zoom <- zoom;
2583 reshape conf.winw conf.winh;
2584 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2587 | Some (layout, y, started) ->
2588 let time =
2589 match conf.maxwait with
2590 | None -> 0.0
2591 | Some t -> t
2593 let dt = now () -. started in
2594 if dt > time
2595 then (
2596 state.y <- y;
2597 load layout;
2601 let setcolumns mode columns coverA coverB =
2602 state.prevcolumns <- Some (conf.columns, conf.zoom);
2603 if columns < 0
2604 then (
2605 if isbirdseye mode
2606 then showtext '!' "split mode doesn't work in bird's eye"
2607 else (
2608 conf.columns <- Csplit (-columns, [||]);
2609 state.x <- 0;
2610 conf.zoom <- 1.0;
2613 else (
2614 if columns < 2
2615 then (
2616 conf.columns <- Csingle;
2617 state.x <- 0;
2618 setzoom 1.0;
2620 else (
2621 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2622 conf.zoom <- 1.0;
2625 reshape conf.winw conf.winh;
2628 let enterbirdseye () =
2629 let zoom = float conf.thumbw /. float conf.winw in
2630 let birdseyepageno =
2631 let cy = conf.winh / 2 in
2632 let fold = function
2633 | [] -> 0
2634 | l :: rest ->
2635 let rec fold best = function
2636 | [] -> best.pageno
2637 | l :: rest ->
2638 let d = cy - (l.pagedispy + l.pagevh/2)
2639 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2640 if abs d < abs dbest
2641 then fold l rest
2642 else best.pageno
2643 in fold l rest
2645 fold state.layout
2647 state.mode <- Birdseye (
2648 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2650 conf.zoom <- zoom;
2651 conf.presentation <- false;
2652 conf.interpagespace <- 10;
2653 conf.hlinks <- false;
2654 state.x <- 0;
2655 state.mstate <- Mnone;
2656 conf.maxwait <- None;
2657 conf.columns <- (
2658 match conf.beyecolumns with
2659 | Some c ->
2660 conf.zoom <- 1.0;
2661 Cmulti ((c, 0, 0), [||])
2662 | None -> Csingle
2664 Wsi.setcursor Wsi.CURSOR_INHERIT;
2665 if conf.verbose
2666 then
2667 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2668 (100.0*.zoom)
2669 else
2670 state.text <- ""
2672 reshape conf.winw conf.winh;
2675 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2676 state.mode <- View;
2677 conf.zoom <- c.zoom;
2678 conf.presentation <- c.presentation;
2679 conf.interpagespace <- c.interpagespace;
2680 conf.maxwait <- c.maxwait;
2681 conf.hlinks <- c.hlinks;
2682 conf.beyecolumns <- (
2683 match conf.columns with
2684 | Cmulti ((c, _, _), _) -> Some c
2685 | Csingle -> None
2686 | Csplit _ -> failwith "leaving bird's eye split mode"
2688 conf.columns <- (
2689 match c.columns with
2690 | Cmulti (c, _) -> Cmulti (c, [||])
2691 | Csingle -> Csingle
2692 | Csplit (c, _) -> Csplit (c, [||])
2694 state.x <- leftx;
2695 if conf.verbose
2696 then
2697 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2698 (100.0*.conf.zoom)
2700 reshape conf.winw conf.winh;
2701 state.anchor <- if goback then anchor else (pageno, 0.0);
2704 let togglebirdseye () =
2705 match state.mode with
2706 | Birdseye vals -> leavebirdseye vals true
2707 | View -> enterbirdseye ()
2708 | _ -> ()
2711 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2712 let pageno = max 0 (pageno - incr) in
2713 let rec loop = function
2714 | [] -> gotopage1 pageno 0
2715 | l :: _ when l.pageno = pageno ->
2716 if l.pagedispy >= 0 && l.pagey = 0
2717 then G.postRedisplay "upbirdseye"
2718 else gotopage1 pageno 0
2719 | _ :: rest -> loop rest
2721 loop state.layout;
2722 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2725 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2726 let pageno = min (state.pagecount - 1) (pageno + incr) in
2727 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2728 let rec loop = function
2729 | [] ->
2730 let y, h = getpageyh pageno in
2731 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2732 gotoy (clamp dy)
2733 | l :: _ when l.pageno = pageno ->
2734 if l.pagevh != l.pageh
2735 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2736 else G.postRedisplay "downbirdseye"
2737 | _ :: rest -> loop rest
2739 loop state.layout
2742 let optentry mode _ key =
2743 let btos b = if b then "on" else "off" in
2744 if key >= 32 && key < 127
2745 then
2746 let c = Char.chr key in
2747 match c with
2748 | 's' ->
2749 let ondone s =
2750 try conf.scrollstep <- int_of_string s with exc ->
2751 state.text <- Printf.sprintf "bad integer `%s': %s"
2752 s (Printexc.to_string exc)
2754 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2756 | 'A' ->
2757 let ondone s =
2759 conf.autoscrollstep <- int_of_string s;
2760 if state.autoscroll <> None
2761 then state.autoscroll <- Some conf.autoscrollstep
2762 with exc ->
2763 state.text <- Printf.sprintf "bad integer `%s': %s"
2764 s (Printexc.to_string exc)
2766 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2768 | 'C' ->
2769 let ondone s =
2771 let n, a, b = multicolumns_of_string s in
2772 setcolumns mode n a b;
2773 with exc ->
2774 state.text <- Printf.sprintf "bad columns `%s': %s"
2775 s (Printexc.to_string exc)
2777 TEswitch ("columns: ", "", None, textentry, ondone, true)
2779 | 'Z' ->
2780 let ondone s =
2782 let zoom = float (int_of_string s) /. 100.0 in
2783 setzoom zoom
2784 with exc ->
2785 state.text <- Printf.sprintf "bad integer `%s': %s"
2786 s (Printexc.to_string exc)
2788 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2790 | 't' ->
2791 let ondone s =
2793 conf.thumbw <- bound (int_of_string s) 2 4096;
2794 state.text <-
2795 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2796 begin match mode with
2797 | Birdseye beye ->
2798 leavebirdseye beye false;
2799 enterbirdseye ();
2800 | _ -> ();
2802 with exc ->
2803 state.text <- Printf.sprintf "bad integer `%s': %s"
2804 s (Printexc.to_string exc)
2806 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2808 | 'R' ->
2809 let ondone s =
2810 match try
2811 Some (int_of_string s)
2812 with exc ->
2813 state.text <- Printf.sprintf "bad integer `%s': %s"
2814 s (Printexc.to_string exc);
2815 None
2816 with
2817 | Some angle -> reqlayout angle conf.proportional
2818 | None -> ()
2820 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2822 | 'i' ->
2823 conf.icase <- not conf.icase;
2824 TEdone ("case insensitive search " ^ (btos conf.icase))
2826 | 'p' ->
2827 conf.preload <- not conf.preload;
2828 gotoy state.y;
2829 TEdone ("preload " ^ (btos conf.preload))
2831 | 'v' ->
2832 conf.verbose <- not conf.verbose;
2833 TEdone ("verbose " ^ (btos conf.verbose))
2835 | 'd' ->
2836 conf.debug <- not conf.debug;
2837 TEdone ("debug " ^ (btos conf.debug))
2839 | 'h' ->
2840 conf.maxhfit <- not conf.maxhfit;
2841 state.maxy <- calcheight ();
2842 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2844 | 'c' ->
2845 conf.crophack <- not conf.crophack;
2846 TEdone ("crophack " ^ btos conf.crophack)
2848 | 'a' ->
2849 let s =
2850 match conf.maxwait with
2851 | None ->
2852 conf.maxwait <- Some infinity;
2853 "always wait for page to complete"
2854 | Some _ ->
2855 conf.maxwait <- None;
2856 "show placeholder if page is not ready"
2858 TEdone s
2860 | 'f' ->
2861 conf.underinfo <- not conf.underinfo;
2862 TEdone ("underinfo " ^ btos conf.underinfo)
2864 | 'P' ->
2865 conf.savebmarks <- not conf.savebmarks;
2866 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2868 | 'S' ->
2869 let ondone s =
2871 let pageno, py =
2872 match state.layout with
2873 | [] -> 0, 0
2874 | l :: _ ->
2875 l.pageno, l.pagey
2877 conf.interpagespace <- int_of_string s;
2878 docolumns conf.columns;
2879 state.maxy <- calcheight ();
2880 let y = getpagey pageno in
2881 gotoy (y + py)
2882 with exc ->
2883 state.text <- Printf.sprintf "bad integer `%s': %s"
2884 s (Printexc.to_string exc)
2886 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2888 | 'l' ->
2889 reqlayout conf.angle (not conf.proportional);
2890 TEdone ("proportional display " ^ btos conf.proportional)
2892 | 'T' ->
2893 settrim (not conf.trimmargins) conf.trimfuzz;
2894 TEdone ("trim margins " ^ btos conf.trimmargins)
2896 | 'I' ->
2897 conf.invert <- not conf.invert;
2898 TEdone ("invert colors " ^ btos conf.invert)
2900 | 'x' ->
2901 let ondone s =
2902 cbput state.hists.sel s;
2903 conf.selcmd <- s;
2905 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2906 textentry, ondone, true)
2908 | _ ->
2909 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2910 TEstop
2911 else
2912 TEcont state.text
2915 class type lvsource = object
2916 method getitemcount : int
2917 method getitem : int -> (string * int)
2918 method hasaction : int -> bool
2919 method exit :
2920 uioh:uioh ->
2921 cancel:bool ->
2922 active:int ->
2923 first:int ->
2924 pan:int ->
2925 qsearch:string ->
2926 uioh option
2927 method getactive : int
2928 method getfirst : int
2929 method getqsearch : string
2930 method setqsearch : string -> unit
2931 method getpan : int
2932 end;;
2934 class virtual lvsourcebase = object
2935 val mutable m_active = 0
2936 val mutable m_first = 0
2937 val mutable m_qsearch = ""
2938 val mutable m_pan = 0
2939 method getactive = m_active
2940 method getfirst = m_first
2941 method getqsearch = m_qsearch
2942 method getpan = m_pan
2943 method setqsearch s = m_qsearch <- s
2944 end;;
2946 let withoutlastutf8 s =
2947 let len = String.length s in
2948 if len = 0
2949 then s
2950 else
2951 let rec find pos =
2952 if pos = 0
2953 then pos
2954 else
2955 let b = Char.code s.[pos] in
2956 if b land 0b110000 = 0b11000000
2957 then find (pos-1)
2958 else pos-1
2960 let first =
2961 if Char.code s.[len-1] land 0x80 = 0
2962 then len-1
2963 else find (len-1)
2965 String.sub s 0 first;
2968 let textentrykeyboard
2969 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2970 let enttext te =
2971 state.mode <- Textentry (te, onleave);
2972 state.text <- "";
2973 enttext ();
2974 G.postRedisplay "textentrykeyboard enttext";
2976 let histaction cmd =
2977 match opthist with
2978 | None -> ()
2979 | Some (action, _) ->
2980 state.mode <- Textentry (
2981 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2983 G.postRedisplay "textentry histaction"
2985 match key with
2986 | 0xff08 -> (* backspace *)
2987 let s = withoutlastutf8 text in
2988 let len = String.length s in
2989 if cancelonempty && len = 0
2990 then (
2991 onleave Cancel;
2992 G.postRedisplay "textentrykeyboard after cancel";
2994 else (
2995 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2998 | 0xff0d ->
2999 ondone text;
3000 onleave Confirm;
3001 G.postRedisplay "textentrykeyboard after confirm"
3003 | 0xff52 -> histaction HCprev
3004 | 0xff54 -> histaction HCnext
3005 | 0xff50 -> histaction HCfirst
3006 | 0xff57 -> histaction HClast
3008 | 0xff1b -> (* escape*)
3009 if String.length text = 0
3010 then (
3011 begin match opthist with
3012 | None -> ()
3013 | Some (_, onhistcancel) -> onhistcancel ()
3014 end;
3015 onleave Cancel;
3016 state.text <- "";
3017 G.postRedisplay "textentrykeyboard after cancel2"
3019 else (
3020 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3023 | 0xff9f | 0xffff -> () (* delete *)
3025 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3026 begin match onkey text key with
3027 | TEdone text ->
3028 ondone text;
3029 onleave Confirm;
3030 G.postRedisplay "textentrykeyboard after confirm2";
3032 | TEcont text ->
3033 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3035 | TEstop ->
3036 onleave Cancel;
3037 G.postRedisplay "textentrykeyboard after cancel3"
3039 | TEswitch te ->
3040 state.mode <- Textentry (te, onleave);
3041 G.postRedisplay "textentrykeyboard switch";
3042 end;
3044 | _ ->
3045 vlog "unhandled key %s" (Wsi.keyname key)
3048 let firstof first active =
3049 if first > active || abs (first - active) > fstate.maxrows - 1
3050 then max 0 (active - (fstate.maxrows/2))
3051 else first
3054 let calcfirst first active =
3055 if active > first
3056 then
3057 let rows = active - first in
3058 if rows > fstate.maxrows then active - fstate.maxrows else first
3059 else active
3062 let scrollph y maxy =
3063 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3064 let sh = float conf.winh /. sh in
3065 let sh = max sh (float conf.scrollh) in
3067 let percent =
3068 if y = state.maxy
3069 then 1.0
3070 else float y /. float maxy
3072 let position = (float conf.winh -. sh) *. percent in
3074 let position =
3075 if position +. sh > float conf.winh
3076 then float conf.winh -. sh
3077 else position
3079 position, sh;
3082 let coe s = (s :> uioh);;
3084 class listview ~(source:lvsource) ~trusted ~modehash =
3085 object (self)
3086 val m_pan = source#getpan
3087 val m_first = source#getfirst
3088 val m_active = source#getactive
3089 val m_qsearch = source#getqsearch
3090 val m_prev_uioh = state.uioh
3092 method private elemunder y =
3093 let n = y / (fstate.fontsize+1) in
3094 if m_first + n < source#getitemcount
3095 then (
3096 if source#hasaction (m_first + n)
3097 then Some (m_first + n)
3098 else None
3100 else None
3102 method display =
3103 Gl.enable `blend;
3104 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3105 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3106 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3107 GlDraw.color (1., 1., 1.);
3108 Gl.enable `texture_2d;
3109 let fs = fstate.fontsize in
3110 let nfs = fs + 1 in
3111 let ww = fstate.wwidth in
3112 let tabw = 30.0*.ww in
3113 let itemcount = source#getitemcount in
3114 let rec loop row =
3115 if (row - m_first) * nfs > conf.winh
3116 then ()
3117 else (
3118 if row >= 0 && row < itemcount
3119 then (
3120 let (s, level) = source#getitem row in
3121 let y = (row - m_first) * nfs in
3122 let x = 5.0 +. float (level + m_pan) *. ww in
3123 if row = m_active
3124 then (
3125 Gl.disable `texture_2d;
3126 GlDraw.polygon_mode `both `line;
3127 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3128 GlDraw.rect (1., float (y + 1))
3129 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3130 GlDraw.polygon_mode `both `fill;
3131 GlDraw.color (1., 1., 1.);
3132 Gl.enable `texture_2d;
3135 let drawtabularstring s =
3136 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3137 if trusted
3138 then
3139 let tabpos = try String.index s '\t' with Not_found -> -1 in
3140 if tabpos > 0
3141 then
3142 let len = String.length s - tabpos - 1 in
3143 let s1 = String.sub s 0 tabpos
3144 and s2 = String.sub s (tabpos + 1) len in
3145 let nx = drawstr x s1 in
3146 let sw = nx -. x in
3147 let x = x +. (max tabw sw) in
3148 drawstr x s2
3149 else
3150 drawstr x s
3151 else
3152 drawstr x s
3154 let _ = drawtabularstring s in
3155 loop (row+1)
3159 loop m_first;
3160 Gl.disable `blend;
3161 Gl.disable `texture_2d;
3163 method updownlevel incr =
3164 let len = source#getitemcount in
3165 let curlevel =
3166 if m_active >= 0 && m_active < len
3167 then snd (source#getitem m_active)
3168 else -1
3170 let rec flow i =
3171 if i = len then i-1 else if i = -1 then 0 else
3172 let _, l = source#getitem i in
3173 if l != curlevel then i else flow (i+incr)
3175 let active = flow m_active in
3176 let first = calcfirst m_first active in
3177 G.postRedisplay "outline updownlevel";
3178 {< m_active = active; m_first = first >}
3180 method private key1 key mask =
3181 let set1 active first qsearch =
3182 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3184 let search active pattern incr =
3185 let dosearch re =
3186 let rec loop n =
3187 if n >= 0 && n < source#getitemcount
3188 then (
3189 let s, _ = source#getitem n in
3191 (try ignore (Str.search_forward re s 0); true
3192 with Not_found -> false)
3193 then Some n
3194 else loop (n + incr)
3196 else None
3198 loop active
3201 let re = Str.regexp_case_fold pattern in
3202 dosearch re
3203 with Failure s ->
3204 state.text <- s;
3205 None
3207 let itemcount = source#getitemcount in
3208 let find start incr =
3209 let rec find i =
3210 if i = -1 || i = itemcount
3211 then -1
3212 else (
3213 if source#hasaction i
3214 then i
3215 else find (i + incr)
3218 find start
3220 let set active first =
3221 let first = bound first 0 (itemcount - fstate.maxrows) in
3222 state.text <- "";
3223 coe {< m_active = active; m_first = first >}
3225 let navigate incr =
3226 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3227 let active, first =
3228 let incr1 = if incr > 0 then 1 else -1 in
3229 if isvisible m_first m_active
3230 then
3231 let next =
3232 let next = m_active + incr in
3233 let next =
3234 if next < 0 || next >= itemcount
3235 then -1
3236 else find next incr1
3238 if next = -1 || abs (m_active - next) > fstate.maxrows
3239 then -1
3240 else next
3242 if next = -1
3243 then
3244 let first = m_first + incr in
3245 let first = bound first 0 (itemcount - 1) in
3246 let next =
3247 let next = m_active + incr in
3248 let next = bound next 0 (itemcount - 1) in
3249 find next ~-incr1
3251 let active = if next = -1 then m_active else next in
3252 active, first
3253 else
3254 let first = min next m_first in
3255 let first =
3256 if abs (next - first) > fstate.maxrows
3257 then first + incr
3258 else first
3260 next, first
3261 else
3262 let first = m_first + incr in
3263 let first = bound first 0 (itemcount - 1) in
3264 let active =
3265 let next = m_active + incr in
3266 let next = bound next 0 (itemcount - 1) in
3267 let next = find next incr1 in
3268 let active =
3269 if next = -1 || abs (m_active - first) > fstate.maxrows
3270 then (
3271 let active = if m_active = -1 then next else m_active in
3272 active
3274 else next
3276 if isvisible first active
3277 then active
3278 else -1
3280 active, first
3282 G.postRedisplay "listview navigate";
3283 set active first;
3285 match key with
3286 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3287 let incr = if key = 0x72 then -1 else 1 in
3288 let active, first =
3289 match search (m_active + incr) m_qsearch incr with
3290 | None ->
3291 state.text <- m_qsearch ^ " [not found]";
3292 m_active, m_first
3293 | Some active ->
3294 state.text <- m_qsearch;
3295 active, firstof m_first active
3297 G.postRedisplay "listview ctrl-r/s";
3298 set1 active first m_qsearch;
3300 | 0xff08 -> (* backspace *)
3301 if String.length m_qsearch = 0
3302 then coe self
3303 else (
3304 let qsearch = withoutlastutf8 m_qsearch in
3305 let len = String.length qsearch in
3306 if len = 0
3307 then (
3308 state.text <- "";
3309 G.postRedisplay "listview empty qsearch";
3310 set1 m_active m_first "";
3312 else
3313 let active, first =
3314 match search m_active qsearch ~-1 with
3315 | None ->
3316 state.text <- qsearch ^ " [not found]";
3317 m_active, m_first
3318 | Some active ->
3319 state.text <- qsearch;
3320 active, firstof m_first active
3322 G.postRedisplay "listview backspace qsearch";
3323 set1 active first qsearch
3326 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3327 let pattern = m_qsearch ^ Wsi.toutf8 key in
3328 let active, first =
3329 match search m_active pattern 1 with
3330 | None ->
3331 state.text <- pattern ^ " [not found]";
3332 m_active, m_first
3333 | Some active ->
3334 state.text <- pattern;
3335 active, firstof m_first active
3337 G.postRedisplay "listview qsearch add";
3338 set1 active first pattern;
3340 | 0xff1b -> (* escape *)
3341 state.text <- "";
3342 if String.length m_qsearch = 0
3343 then (
3344 G.postRedisplay "list view escape";
3345 begin
3346 match
3347 source#exit (coe self) true m_active m_first m_pan m_qsearch
3348 with
3349 | None -> m_prev_uioh
3350 | Some uioh -> uioh
3353 else (
3354 G.postRedisplay "list view kill qsearch";
3355 source#setqsearch "";
3356 coe {< m_qsearch = "" >}
3359 | 0xff0d -> (* return *)
3360 state.text <- "";
3361 let self = {< m_qsearch = "" >} in
3362 source#setqsearch "";
3363 let opt =
3364 G.postRedisplay "listview enter";
3365 if m_active >= 0 && m_active < source#getitemcount
3366 then (
3367 source#exit (coe self) false m_active m_first m_pan "";
3369 else (
3370 source#exit (coe self) true m_active m_first m_pan "";
3373 begin match opt with
3374 | None -> m_prev_uioh
3375 | Some uioh -> uioh
3378 | 0xff9f | 0xffff -> (* delete *)
3379 coe self
3381 | 0xff52 -> navigate ~-1 (* up *)
3382 | 0xff54 -> navigate 1 (* down *)
3383 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3384 | 0xff56 -> navigate fstate.maxrows (* next *)
3386 | 0xff53 -> (* right *)
3387 state.text <- "";
3388 G.postRedisplay "listview right";
3389 coe {< m_pan = m_pan - 1 >}
3391 | 0xff51 -> (* left *)
3392 state.text <- "";
3393 G.postRedisplay "listview left";
3394 coe {< m_pan = m_pan + 1 >}
3396 | 0xff50 -> (* home *)
3397 let active = find 0 1 in
3398 G.postRedisplay "listview home";
3399 set active 0;
3401 | 0xff57 -> (* end *)
3402 let first = max 0 (itemcount - fstate.maxrows) in
3403 let active = find (itemcount - 1) ~-1 in
3404 G.postRedisplay "listview end";
3405 set active first;
3407 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3408 coe self
3410 | _ ->
3411 dolog "listview unknown key %#x" key; coe self
3413 method key key mask =
3414 match state.mode with
3415 | Textentry te -> textentrykeyboard key mask te; coe self
3416 | _ -> self#key1 key mask
3418 method button button down x y _ =
3419 let opt =
3420 match button with
3421 | 1 when x > conf.winw - conf.scrollbw ->
3422 G.postRedisplay "listview scroll";
3423 if down
3424 then
3425 let _, position, sh = self#scrollph in
3426 if y > truncate position && y < truncate (position +. sh)
3427 then (
3428 state.mstate <- Mscrolly;
3429 Some (coe self)
3431 else
3432 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3433 let first = truncate (s *. float source#getitemcount) in
3434 let first = min source#getitemcount first in
3435 Some (coe {< m_first = first; m_active = first >})
3436 else (
3437 state.mstate <- Mnone;
3438 Some (coe self);
3440 | 1 when not down ->
3441 begin match self#elemunder y with
3442 | Some n ->
3443 G.postRedisplay "listview click";
3444 source#exit
3445 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3446 | _ ->
3447 Some (coe self)
3449 | n when (n == 4 || n == 5) && not down ->
3450 let len = source#getitemcount in
3451 let first =
3452 if n = 5 && m_first + fstate.maxrows >= len
3453 then
3454 m_first
3455 else
3456 let first = m_first + (if n == 4 then -1 else 1) in
3457 bound first 0 (len - 1)
3459 G.postRedisplay "listview wheel";
3460 Some (coe {< m_first = first >})
3461 | _ ->
3462 Some (coe self)
3464 match opt with
3465 | None -> m_prev_uioh
3466 | Some uioh -> uioh
3468 method motion _ y =
3469 match state.mstate with
3470 | Mscrolly ->
3471 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3472 let first = truncate (s *. float source#getitemcount) in
3473 let first = min source#getitemcount first in
3474 G.postRedisplay "listview motion";
3475 coe {< m_first = first; m_active = first >}
3476 | _ -> coe self
3478 method pmotion x y =
3479 if x < conf.winw - conf.scrollbw
3480 then
3481 let n =
3482 match self#elemunder y with
3483 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3484 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3486 let o =
3487 if n != m_active
3488 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3489 else self
3491 coe o
3492 else (
3493 Wsi.setcursor Wsi.CURSOR_INHERIT;
3494 coe self
3497 method infochanged _ = ()
3499 method scrollpw = (0, 0.0, 0.0)
3500 method scrollph =
3501 let nfs = fstate.fontsize + 1 in
3502 let y = m_first * nfs in
3503 let itemcount = source#getitemcount in
3504 let maxi = max 0 (itemcount - fstate.maxrows) in
3505 let maxy = maxi * nfs in
3506 let p, h = scrollph y maxy in
3507 conf.scrollbw, p, h
3509 method modehash = modehash
3510 end;;
3512 class outlinelistview ~source =
3513 object (self)
3514 inherit listview
3515 ~source:(source :> lvsource)
3516 ~trusted:false
3517 ~modehash:(findkeyhash conf "outline")
3518 as super
3520 method key key mask =
3521 let calcfirst first active =
3522 if active > first
3523 then
3524 let rows = active - first in
3525 if rows > fstate.maxrows then active - fstate.maxrows else first
3526 else active
3528 let navigate incr =
3529 let active = m_active + incr in
3530 let active = bound active 0 (source#getitemcount - 1) in
3531 let first = calcfirst m_first active in
3532 G.postRedisplay "outline navigate";
3533 coe {< m_active = active; m_first = first >}
3535 let ctrl = Wsi.withctrl mask in
3536 match key with
3537 | 110 when ctrl -> (* ctrl-n *)
3538 source#narrow m_qsearch;
3539 G.postRedisplay "outline ctrl-n";
3540 coe {< m_first = 0; m_active = 0 >}
3542 | 117 when ctrl -> (* ctrl-u *)
3543 source#denarrow;
3544 G.postRedisplay "outline ctrl-u";
3545 state.text <- "";
3546 coe {< m_first = 0; m_active = 0 >}
3548 | 108 when ctrl -> (* ctrl-l *)
3549 let first = m_active - (fstate.maxrows / 2) in
3550 G.postRedisplay "outline ctrl-l";
3551 coe {< m_first = first >}
3553 | 0xff9f | 0xffff -> (* delete *)
3554 source#remove m_active;
3555 G.postRedisplay "outline delete";
3556 let active = max 0 (m_active-1) in
3557 coe {< m_first = firstof m_first active;
3558 m_active = active >}
3560 | 0xff52 -> navigate ~-1 (* up *)
3561 | 0xff54 -> navigate 1 (* down *)
3562 | 0xff55 -> (* prior *)
3563 navigate ~-(fstate.maxrows)
3564 | 0xff56 -> (* next *)
3565 navigate fstate.maxrows
3567 | 0xff53 -> (* [ctrl-]right *)
3568 let o =
3569 if ctrl
3570 then (
3571 G.postRedisplay "outline ctrl right";
3572 {< m_pan = m_pan + 1 >}
3574 else self#updownlevel 1
3576 coe o
3578 | 0xff51 -> (* [ctrl-]left *)
3579 let o =
3580 if ctrl
3581 then (
3582 G.postRedisplay "outline ctrl left";
3583 {< m_pan = m_pan - 1 >}
3585 else self#updownlevel ~-1
3587 coe o
3589 | 0xff50 -> (* home *)
3590 G.postRedisplay "outline home";
3591 coe {< m_first = 0; m_active = 0 >}
3593 | 0xff57 -> (* end *)
3594 let active = source#getitemcount - 1 in
3595 let first = max 0 (active - fstate.maxrows) in
3596 G.postRedisplay "outline end";
3597 coe {< m_active = active; m_first = first >}
3599 | _ -> super#key key mask
3602 let outlinesource usebookmarks =
3603 let empty = [||] in
3604 (object
3605 inherit lvsourcebase
3606 val mutable m_items = empty
3607 val mutable m_orig_items = empty
3608 val mutable m_prev_items = empty
3609 val mutable m_narrow_pattern = ""
3610 val mutable m_hadremovals = false
3612 method getitemcount =
3613 Array.length m_items + (if m_hadremovals then 1 else 0)
3615 method getitem n =
3616 if n == Array.length m_items && m_hadremovals
3617 then
3618 ("[Confirm removal]", 0)
3619 else
3620 let s, n, _ = m_items.(n) in
3621 (s, n)
3623 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3624 ignore (uioh, first, qsearch);
3625 let confrimremoval = m_hadremovals && active = Array.length m_items in
3626 let items =
3627 if String.length m_narrow_pattern = 0
3628 then m_orig_items
3629 else m_items
3631 if not cancel
3632 then (
3633 if not confrimremoval
3634 then(
3635 let _, _, anchor = m_items.(active) in
3636 gotoanchor anchor;
3637 m_items <- items;
3639 else (
3640 state.bookmarks <- Array.to_list m_items;
3641 m_orig_items <- m_items;
3644 else m_items <- items;
3645 m_pan <- pan;
3646 None
3648 method hasaction _ = true
3650 method greetmsg =
3651 if Array.length m_items != Array.length m_orig_items
3652 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3653 else ""
3655 method narrow pattern =
3656 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3657 match reopt with
3658 | None -> ()
3659 | Some re ->
3660 let rec loop accu n =
3661 if n = -1
3662 then (
3663 m_narrow_pattern <- pattern;
3664 m_items <- Array.of_list accu
3666 else
3667 let (s, _, _) as o = m_items.(n) in
3668 let accu =
3669 if (try ignore (Str.search_forward re s 0); true
3670 with Not_found -> false)
3671 then o :: accu
3672 else accu
3674 loop accu (n-1)
3676 loop [] (Array.length m_items - 1)
3678 method denarrow =
3679 m_orig_items <- (
3680 if usebookmarks
3681 then Array.of_list state.bookmarks
3682 else state.outlines
3684 m_items <- m_orig_items
3686 method remove m =
3687 if usebookmarks
3688 then
3689 if m >= 0 && m < Array.length m_items
3690 then (
3691 m_hadremovals <- true;
3692 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3693 let n = if n >= m then n+1 else n in
3694 m_items.(n)
3698 method reset anchor items =
3699 m_hadremovals <- false;
3700 if m_orig_items == empty || m_prev_items != items
3701 then (
3702 m_orig_items <- items;
3703 if String.length m_narrow_pattern = 0
3704 then m_items <- items;
3706 m_prev_items <- items;
3707 let rely = getanchory anchor in
3708 let active =
3709 let rec loop n best bestd =
3710 if n = Array.length m_items
3711 then best
3712 else
3713 let (_, _, anchor) = m_items.(n) in
3714 let orely = getanchory anchor in
3715 let d = abs (orely - rely) in
3716 if d < bestd
3717 then loop (n+1) n d
3718 else loop (n+1) best bestd
3720 loop 0 ~-1 max_int
3722 m_active <- active;
3723 m_first <- firstof m_first active
3724 end)
3727 let enterselector usebookmarks =
3728 let source = outlinesource usebookmarks in
3729 fun errmsg ->
3730 let outlines =
3731 if usebookmarks
3732 then Array.of_list state.bookmarks
3733 else state.outlines
3735 if Array.length outlines = 0
3736 then (
3737 showtext ' ' errmsg;
3739 else (
3740 state.text <- source#greetmsg;
3741 Wsi.setcursor Wsi.CURSOR_INHERIT;
3742 let anchor = getanchor () in
3743 source#reset anchor outlines;
3744 state.uioh <- coe (new outlinelistview ~source);
3745 G.postRedisplay "enter selector";
3749 let enteroutlinemode =
3750 let f = enterselector false in
3751 fun ()-> f "Document has no outline";
3754 let enterbookmarkmode =
3755 let f = enterselector true in
3756 fun () -> f "Document has no bookmarks (yet)";
3759 let color_of_string s =
3760 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3761 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3765 let color_to_string (r, g, b) =
3766 let r = truncate (r *. 256.0)
3767 and g = truncate (g *. 256.0)
3768 and b = truncate (b *. 256.0) in
3769 Printf.sprintf "%d/%d/%d" r g b
3772 let irect_of_string s =
3773 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3776 let irect_to_string (x0,y0,x1,y1) =
3777 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3780 let makecheckers () =
3781 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3782 following to say:
3783 converted by Issac Trotts. July 25, 2002 *)
3784 let image_height = 64
3785 and image_width = 64 in
3787 let make_image () =
3788 let image =
3789 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3791 for i = 0 to image_width - 1 do
3792 for j = 0 to image_height - 1 do
3793 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3794 (if (i land 8 ) lxor (j land 8) = 0
3795 then [|255;255;255|] else [|200;200;200|])
3796 done
3797 done;
3798 image
3800 let image = make_image () in
3801 let id = GlTex.gen_texture () in
3802 GlTex.bind_texture `texture_2d id;
3803 GlPix.store (`unpack_alignment 1);
3804 GlTex.image2d image;
3805 List.iter (GlTex.parameter ~target:`texture_2d)
3806 [ `wrap_s `repeat;
3807 `wrap_t `repeat;
3808 `mag_filter `nearest;
3809 `min_filter `nearest ];
3813 let setcheckers enabled =
3814 match state.texid with
3815 | None ->
3816 if enabled then state.texid <- Some (makecheckers ())
3818 | Some texid ->
3819 if not enabled
3820 then (
3821 GlTex.delete_texture texid;
3822 state.texid <- None;
3826 let int_of_string_with_suffix s =
3827 let l = String.length s in
3828 let s1, shift =
3829 if l > 1
3830 then
3831 let suffix = Char.lowercase s.[l-1] in
3832 match suffix with
3833 | 'k' -> String.sub s 0 (l-1), 10
3834 | 'm' -> String.sub s 0 (l-1), 20
3835 | 'g' -> String.sub s 0 (l-1), 30
3836 | _ -> s, 0
3837 else s, 0
3839 let n = int_of_string s1 in
3840 let m = n lsl shift in
3841 if m < 0 || m < n
3842 then raise (Failure "value too large")
3843 else m
3846 let string_with_suffix_of_int n =
3847 if n = 0
3848 then "0"
3849 else
3850 let n, s =
3851 if n = 0
3852 then 0, ""
3853 else (
3854 if n land ((1 lsl 20) - 1) = 0
3855 then n lsr 20, "M"
3856 else (
3857 if n land ((1 lsl 10) - 1) = 0
3858 then n lsr 10, "K"
3859 else n, ""
3863 let rec loop s n =
3864 let h = n mod 1000 in
3865 let n = n / 1000 in
3866 if n = 0
3867 then string_of_int h ^ s
3868 else (
3869 let s = Printf.sprintf "_%03d%s" h s in
3870 loop s n
3873 loop "" n ^ s;
3876 let defghyllscroll = (40, 8, 32);;
3877 let ghyllscroll_of_string s =
3878 let (n, a, b) as nab =
3879 if s = "default"
3880 then defghyllscroll
3881 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3883 if n <= a || n <= b || a >= b
3884 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3885 nab;
3888 let ghyllscroll_to_string ((n, a, b) as nab) =
3889 if nab = defghyllscroll
3890 then "default"
3891 else Printf.sprintf "%d,%d,%d" n a b;
3894 let describe_location () =
3895 let f (fn, _) l =
3896 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3898 let fn, ln = List.fold_left f (-1, -1) state.layout in
3899 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3900 let percent =
3901 if maxy <= 0
3902 then 100.
3903 else (100. *. (float state.y /. float maxy))
3905 if fn = ln
3906 then
3907 Printf.sprintf "page %d of %d [%.2f%%]"
3908 (fn+1) state.pagecount percent
3909 else
3910 Printf.sprintf
3911 "pages %d-%d of %d [%.2f%%]"
3912 (fn+1) (ln+1) state.pagecount percent
3915 let enterinfomode =
3916 let btos b = if b then "\xe2\x88\x9a" else "" in
3917 let showextended = ref false in
3918 let leave mode = function
3919 | Confirm -> state.mode <- mode
3920 | Cancel -> state.mode <- mode in
3921 let src =
3922 (object
3923 val mutable m_first_time = true
3924 val mutable m_l = []
3925 val mutable m_a = [||]
3926 val mutable m_prev_uioh = nouioh
3927 val mutable m_prev_mode = View
3929 inherit lvsourcebase
3931 method reset prev_mode prev_uioh =
3932 m_a <- Array.of_list (List.rev m_l);
3933 m_l <- [];
3934 m_prev_mode <- prev_mode;
3935 m_prev_uioh <- prev_uioh;
3936 if m_first_time
3937 then (
3938 let rec loop n =
3939 if n >= Array.length m_a
3940 then ()
3941 else
3942 match m_a.(n) with
3943 | _, _, _, Action _ -> m_active <- n
3944 | _ -> loop (n+1)
3946 loop 0;
3947 m_first_time <- false;
3950 method int name get set =
3951 m_l <-
3952 (name, `int get, 1, Action (
3953 fun u ->
3954 let ondone s =
3955 try set (int_of_string s)
3956 with exn ->
3957 state.text <- Printf.sprintf "bad integer `%s': %s"
3958 s (Printexc.to_string exn)
3960 state.text <- "";
3961 let te = name ^ ": ", "", None, intentry, ondone, true in
3962 state.mode <- Textentry (te, leave m_prev_mode);
3964 )) :: m_l
3966 method int_with_suffix name get set =
3967 m_l <-
3968 (name, `intws get, 1, Action (
3969 fun u ->
3970 let ondone s =
3971 try set (int_of_string_with_suffix s)
3972 with exn ->
3973 state.text <- Printf.sprintf "bad integer `%s': %s"
3974 s (Printexc.to_string exn)
3976 state.text <- "";
3977 let te =
3978 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3980 state.mode <- Textentry (te, leave m_prev_mode);
3982 )) :: m_l
3984 method bool ?(offset=1) ?(btos=btos) name get set =
3985 m_l <-
3986 (name, `bool (btos, get), offset, Action (
3987 fun u ->
3988 let v = get () in
3989 set (not v);
3991 )) :: m_l
3993 method color name get set =
3994 m_l <-
3995 (name, `color get, 1, Action (
3996 fun u ->
3997 let invalid = (nan, nan, nan) in
3998 let ondone s =
3999 let c =
4000 try color_of_string s
4001 with exn ->
4002 state.text <- Printf.sprintf "bad color `%s': %s"
4003 s (Printexc.to_string exn);
4004 invalid
4006 if c <> invalid
4007 then set c;
4009 let te = name ^ ": ", "", None, textentry, ondone, true in
4010 state.text <- color_to_string (get ());
4011 state.mode <- Textentry (te, leave m_prev_mode);
4013 )) :: m_l
4015 method string name get set =
4016 m_l <-
4017 (name, `string get, 1, Action (
4018 fun u ->
4019 let ondone s = set s in
4020 let te = name ^ ": ", "", None, textentry, ondone, true in
4021 state.mode <- Textentry (te, leave m_prev_mode);
4023 )) :: m_l
4025 method colorspace name get set =
4026 m_l <-
4027 (name, `string get, 1, Action (
4028 fun _ ->
4029 let source =
4030 let vals = [| "rgb"; "bgr"; "gray" |] in
4031 (object
4032 inherit lvsourcebase
4034 initializer
4035 m_active <- int_of_colorspace conf.colorspace;
4036 m_first <- 0;
4038 method getitemcount = Array.length vals
4039 method getitem n = (vals.(n), 0)
4040 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4041 ignore (uioh, first, pan, qsearch);
4042 if not cancel then set active;
4043 None
4044 method hasaction _ = true
4045 end)
4047 state.text <- "";
4048 let modehash = findkeyhash conf "info" in
4049 coe (new listview ~source ~trusted:true ~modehash)
4050 )) :: m_l
4052 method caption s offset =
4053 m_l <- (s, `empty, offset, Noaction) :: m_l
4055 method caption2 s f offset =
4056 m_l <- (s, `string f, offset, Noaction) :: m_l
4058 method getitemcount = Array.length m_a
4060 method getitem n =
4061 let tostr = function
4062 | `int f -> string_of_int (f ())
4063 | `intws f -> string_with_suffix_of_int (f ())
4064 | `string f -> f ()
4065 | `color f -> color_to_string (f ())
4066 | `bool (btos, f) -> btos (f ())
4067 | `empty -> ""
4069 let name, t, offset, _ = m_a.(n) in
4070 ((let s = tostr t in
4071 if String.length s > 0
4072 then Printf.sprintf "%s\t%s" name s
4073 else name),
4074 offset)
4076 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4077 let uiohopt =
4078 if not cancel
4079 then (
4080 m_qsearch <- qsearch;
4081 let uioh =
4082 match m_a.(active) with
4083 | _, _, _, Action f -> f uioh
4084 | _ -> uioh
4086 Some uioh
4088 else None
4090 m_active <- active;
4091 m_first <- first;
4092 m_pan <- pan;
4093 uiohopt
4095 method hasaction n =
4096 match m_a.(n) with
4097 | _, _, _, Action _ -> true
4098 | _ -> false
4099 end)
4101 let rec fillsrc prevmode prevuioh =
4102 let sep () = src#caption "" 0 in
4103 let colorp name get set =
4104 src#string name
4105 (fun () -> color_to_string (get ()))
4106 (fun v ->
4108 let c = color_of_string v in
4109 set c
4110 with exn ->
4111 state.text <- Printf.sprintf "bad color `%s': %s"
4112 v (Printexc.to_string exn);
4115 let oldmode = state.mode in
4116 let birdseye = isbirdseye state.mode in
4118 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4120 src#bool "presentation mode"
4121 (fun () -> conf.presentation)
4122 (fun v ->
4123 conf.presentation <- v;
4124 state.anchor <- getanchor ();
4125 represent ());
4127 src#bool "ignore case in searches"
4128 (fun () -> conf.icase)
4129 (fun v -> conf.icase <- v);
4131 src#bool "preload"
4132 (fun () -> conf.preload)
4133 (fun v -> conf.preload <- v);
4135 src#bool "highlight links"
4136 (fun () -> conf.hlinks)
4137 (fun v -> conf.hlinks <- v);
4139 src#bool "under info"
4140 (fun () -> conf.underinfo)
4141 (fun v -> conf.underinfo <- v);
4143 src#bool "persistent bookmarks"
4144 (fun () -> conf.savebmarks)
4145 (fun v -> conf.savebmarks <- v);
4147 src#bool "proportional display"
4148 (fun () -> conf.proportional)
4149 (fun v -> reqlayout conf.angle v);
4151 src#bool "trim margins"
4152 (fun () -> conf.trimmargins)
4153 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4155 src#bool "persistent location"
4156 (fun () -> conf.jumpback)
4157 (fun v -> conf.jumpback <- v);
4159 sep ();
4160 src#int "inter-page space"
4161 (fun () -> conf.interpagespace)
4162 (fun n ->
4163 conf.interpagespace <- n;
4164 docolumns conf.columns;
4165 let pageno, py =
4166 match state.layout with
4167 | [] -> 0, 0
4168 | l :: _ ->
4169 l.pageno, l.pagey
4171 state.maxy <- calcheight ();
4172 let y = getpagey pageno in
4173 gotoy (y + py)
4176 src#int "page bias"
4177 (fun () -> conf.pagebias)
4178 (fun v -> conf.pagebias <- v);
4180 src#int "scroll step"
4181 (fun () -> conf.scrollstep)
4182 (fun n -> conf.scrollstep <- n);
4184 src#int "auto scroll step"
4185 (fun () ->
4186 match state.autoscroll with
4187 | Some step -> step
4188 | _ -> conf.autoscrollstep)
4189 (fun n ->
4190 if state.autoscroll <> None
4191 then state.autoscroll <- Some n;
4192 conf.autoscrollstep <- n);
4194 src#int "zoom"
4195 (fun () -> truncate (conf.zoom *. 100.))
4196 (fun v -> setzoom ((float v) /. 100.));
4198 src#int "rotation"
4199 (fun () -> conf.angle)
4200 (fun v -> reqlayout v conf.proportional);
4202 src#int "scroll bar width"
4203 (fun () -> state.scrollw)
4204 (fun v ->
4205 state.scrollw <- v;
4206 conf.scrollbw <- v;
4207 reshape conf.winw conf.winh;
4210 src#int "scroll handle height"
4211 (fun () -> conf.scrollh)
4212 (fun v -> conf.scrollh <- v;);
4214 src#int "thumbnail width"
4215 (fun () -> conf.thumbw)
4216 (fun v ->
4217 conf.thumbw <- min 4096 v;
4218 match oldmode with
4219 | Birdseye beye ->
4220 leavebirdseye beye false;
4221 enterbirdseye ()
4222 | _ -> ()
4225 let mode = state.mode in
4226 src#string "columns"
4227 (fun () ->
4228 match conf.columns with
4229 | Csingle -> "1"
4230 | Cmulti (multi, _) -> multicolumns_to_string multi
4231 | Csplit (count, _) -> "-" ^ string_of_int count
4233 (fun v ->
4234 let n, a, b = multicolumns_of_string v in
4235 setcolumns mode n a b);
4237 sep ();
4238 src#caption "Presentation mode" 0;
4239 src#bool "scrollbar visible"
4240 (fun () -> conf.scrollbarinpm)
4241 (fun v ->
4242 if v != conf.scrollbarinpm
4243 then (
4244 conf.scrollbarinpm <- v;
4245 if conf.presentation
4246 then (
4247 state.scrollw <- if v then conf.scrollbw else 0;
4248 reshape conf.winw conf.winh;
4253 sep ();
4254 src#caption "Pixmap cache" 0;
4255 src#int_with_suffix "size (advisory)"
4256 (fun () -> conf.memlimit)
4257 (fun v -> conf.memlimit <- v);
4259 src#caption2 "used"
4260 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4261 (string_with_suffix_of_int state.memused)
4262 (Hashtbl.length state.tilemap)) 1;
4264 sep ();
4265 src#caption "Layout" 0;
4266 src#caption2 "Dimension"
4267 (fun () ->
4268 Printf.sprintf "%dx%d (virtual %dx%d)"
4269 conf.winw conf.winh
4270 state.w state.maxy)
4272 if conf.debug
4273 then
4274 src#caption2 "Position" (fun () ->
4275 Printf.sprintf "%dx%d" state.x state.y
4277 else
4278 src#caption2 "Visible" (fun () -> describe_location ()) 1
4281 sep ();
4282 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4283 "Save these parameters as global defaults at exit"
4284 (fun () -> conf.bedefault)
4285 (fun v -> conf.bedefault <- v)
4288 sep ();
4289 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4290 src#bool ~offset:0 ~btos "Extended parameters"
4291 (fun () -> !showextended)
4292 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4293 if !showextended
4294 then (
4295 src#bool "checkers"
4296 (fun () -> conf.checkers)
4297 (fun v -> conf.checkers <- v; setcheckers v);
4298 src#bool "update cursor"
4299 (fun () -> conf.updatecurs)
4300 (fun v -> conf.updatecurs <- v);
4301 src#bool "verbose"
4302 (fun () -> conf.verbose)
4303 (fun v -> conf.verbose <- v);
4304 src#bool "invert colors"
4305 (fun () -> conf.invert)
4306 (fun v -> conf.invert <- v);
4307 src#bool "max fit"
4308 (fun () -> conf.maxhfit)
4309 (fun v -> conf.maxhfit <- v);
4310 src#bool "redirect stderr"
4311 (fun () -> conf.redirectstderr)
4312 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4313 src#string "uri launcher"
4314 (fun () -> conf.urilauncher)
4315 (fun v -> conf.urilauncher <- v);
4316 src#string "path launcher"
4317 (fun () -> conf.pathlauncher)
4318 (fun v -> conf.pathlauncher <- v);
4319 src#string "tile size"
4320 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4321 (fun v ->
4323 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4324 conf.tilew <- max 64 w;
4325 conf.tileh <- max 64 h;
4326 flushtiles ();
4327 with exn ->
4328 state.text <- Printf.sprintf "bad tile size `%s': %s"
4329 v (Printexc.to_string exn));
4330 src#int "texture count"
4331 (fun () -> conf.texcount)
4332 (fun v ->
4333 if realloctexts v
4334 then conf.texcount <- v
4335 else showtext '!' " Failed to set texture count please retry later"
4337 src#int "slice height"
4338 (fun () -> conf.sliceheight)
4339 (fun v ->
4340 conf.sliceheight <- v;
4341 wcmd "sliceh %d" conf.sliceheight;
4343 src#int "anti-aliasing level"
4344 (fun () -> conf.aalevel)
4345 (fun v ->
4346 conf.aalevel <- bound v 0 8;
4347 state.anchor <- getanchor ();
4348 opendoc state.path state.password;
4350 src#int "ui font size"
4351 (fun () -> fstate.fontsize)
4352 (fun v -> setfontsize (bound v 5 100));
4353 src#int "hint font size"
4354 (fun () -> conf.hfsize)
4355 (fun v -> conf.hfsize <- bound v 5 100);
4356 colorp "background color"
4357 (fun () -> conf.bgcolor)
4358 (fun v -> conf.bgcolor <- v);
4359 src#bool "crop hack"
4360 (fun () -> conf.crophack)
4361 (fun v -> conf.crophack <- v);
4362 src#string "trim fuzz"
4363 (fun () -> irect_to_string conf.trimfuzz)
4364 (fun v ->
4366 conf.trimfuzz <- irect_of_string v;
4367 if conf.trimmargins
4368 then settrim true conf.trimfuzz;
4369 with exn ->
4370 state.text <- Printf.sprintf "bad irect `%s': %s"
4371 v (Printexc.to_string exn)
4373 src#string "throttle"
4374 (fun () ->
4375 match conf.maxwait with
4376 | None -> "show place holder if page is not ready"
4377 | Some time ->
4378 if time = infinity
4379 then "wait for page to fully render"
4380 else
4381 "wait " ^ string_of_float time
4382 ^ " seconds before showing placeholder"
4384 (fun v ->
4386 let f = float_of_string v in
4387 if f <= 0.0
4388 then conf.maxwait <- None
4389 else conf.maxwait <- Some f
4390 with exn ->
4391 state.text <- Printf.sprintf "bad time `%s': %s"
4392 v (Printexc.to_string exn)
4394 src#string "ghyll scroll"
4395 (fun () ->
4396 match conf.ghyllscroll with
4397 | None -> ""
4398 | Some nab -> ghyllscroll_to_string nab
4400 (fun v ->
4402 let gs =
4403 if String.length v = 0
4404 then None
4405 else Some (ghyllscroll_of_string v)
4407 conf.ghyllscroll <- gs
4408 with exn ->
4409 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4410 v (Printexc.to_string exn)
4412 src#string "selection command"
4413 (fun () -> conf.selcmd)
4414 (fun v -> conf.selcmd <- v);
4415 src#colorspace "color space"
4416 (fun () -> colorspace_to_string conf.colorspace)
4417 (fun v ->
4418 conf.colorspace <- colorspace_of_int v;
4419 wcmd "cs %d" v;
4420 load state.layout;
4424 sep ();
4425 src#caption "Document" 0;
4426 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4427 src#caption2 "Pages"
4428 (fun () -> string_of_int state.pagecount) 1;
4429 src#caption2 "Dimensions"
4430 (fun () -> string_of_int (List.length state.pdims)) 1;
4431 if conf.trimmargins
4432 then (
4433 sep ();
4434 src#caption "Trimmed margins" 0;
4435 src#caption2 "Dimensions"
4436 (fun () -> string_of_int (List.length state.pdims)) 1;
4439 sep ();
4440 src#caption "OpenGL" 0;
4441 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4442 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4443 src#reset prevmode prevuioh;
4445 fun () ->
4446 state.text <- "";
4447 let prevmode = state.mode
4448 and prevuioh = state.uioh in
4449 fillsrc prevmode prevuioh;
4450 let source = (src :> lvsource) in
4451 let modehash = findkeyhash conf "info" in
4452 state.uioh <- coe (object (self)
4453 inherit listview ~source ~trusted:true ~modehash as super
4454 val mutable m_prevmemused = 0
4455 method infochanged = function
4456 | Memused ->
4457 if m_prevmemused != state.memused
4458 then (
4459 m_prevmemused <- state.memused;
4460 G.postRedisplay "memusedchanged";
4462 | Pdim -> G.postRedisplay "pdimchanged"
4463 | Docinfo -> fillsrc prevmode prevuioh
4465 method key key mask =
4466 if not (Wsi.withctrl mask)
4467 then
4468 match key with
4469 | 0xff51 -> coe (self#updownlevel ~-1)
4470 | 0xff53 -> coe (self#updownlevel 1)
4471 | _ -> super#key key mask
4472 else super#key key mask
4473 end);
4474 G.postRedisplay "info";
4477 let enterhelpmode =
4478 let source =
4479 (object
4480 inherit lvsourcebase
4481 method getitemcount = Array.length state.help
4482 method getitem n =
4483 let s, n, _ = state.help.(n) in
4484 (s, n)
4486 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4487 let optuioh =
4488 if not cancel
4489 then (
4490 m_qsearch <- qsearch;
4491 match state.help.(active) with
4492 | _, _, Action f -> Some (f uioh)
4493 | _ -> Some (uioh)
4495 else None
4497 m_active <- active;
4498 m_first <- first;
4499 m_pan <- pan;
4500 optuioh
4502 method hasaction n =
4503 match state.help.(n) with
4504 | _, _, Action _ -> true
4505 | _ -> false
4507 initializer
4508 m_active <- -1
4509 end)
4510 in fun () ->
4511 let modehash = findkeyhash conf "help" in
4512 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4513 G.postRedisplay "help";
4516 let entermsgsmode =
4517 let msgsource =
4518 let re = Str.regexp "[\r\n]" in
4519 (object
4520 inherit lvsourcebase
4521 val mutable m_items = [||]
4523 method getitemcount = 1 + Array.length m_items
4525 method getitem n =
4526 if n = 0
4527 then "[Clear]", 0
4528 else m_items.(n-1), 0
4530 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4531 ignore uioh;
4532 if not cancel
4533 then (
4534 if active = 0
4535 then Buffer.clear state.errmsgs;
4536 m_qsearch <- qsearch;
4538 m_active <- active;
4539 m_first <- first;
4540 m_pan <- pan;
4541 None
4543 method hasaction n =
4544 n = 0
4546 method reset =
4547 state.newerrmsgs <- false;
4548 let l = Str.split re (Buffer.contents state.errmsgs) in
4549 m_items <- Array.of_list l
4551 initializer
4552 m_active <- 0
4553 end)
4554 in fun () ->
4555 state.text <- "";
4556 msgsource#reset;
4557 let source = (msgsource :> lvsource) in
4558 let modehash = findkeyhash conf "listview" in
4559 state.uioh <- coe (object
4560 inherit listview ~source ~trusted:false ~modehash as super
4561 method display =
4562 if state.newerrmsgs
4563 then msgsource#reset;
4564 super#display
4565 end);
4566 G.postRedisplay "msgs";
4569 let quickbookmark ?title () =
4570 match state.layout with
4571 | [] -> ()
4572 | l :: _ ->
4573 let title =
4574 match title with
4575 | None ->
4576 let sec = Unix.gettimeofday () in
4577 let tm = Unix.localtime sec in
4578 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4579 (l.pageno+1)
4580 tm.Unix.tm_mday
4581 tm.Unix.tm_mon
4582 (tm.Unix.tm_year + 1900)
4583 tm.Unix.tm_hour
4584 tm.Unix.tm_min
4585 | Some title -> title
4587 state.bookmarks <-
4588 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4589 :: state.bookmarks
4592 let doreshape w h =
4593 state.fullscreen <- None;
4594 Wsi.reshape w h;
4597 let setautoscrollspeed step goingdown =
4598 let incr = max 1 ((abs step) / 2) in
4599 let incr = if goingdown then incr else -incr in
4600 let astep = step + incr in
4601 state.autoscroll <- Some astep;
4604 let gotounder = function
4605 | Ulinkgoto (pageno, top) ->
4606 if pageno >= 0
4607 then (
4608 addnav ();
4609 gotopage1 pageno top;
4612 | Ulinkuri s ->
4613 gotouri s
4615 | Uremote (filename, pageno) ->
4616 let path =
4617 if Sys.file_exists filename
4618 then filename
4619 else
4620 let dir = Filename.dirname state.path in
4621 let path = Filename.concat dir filename in
4622 if Sys.file_exists path
4623 then path
4624 else ""
4626 if String.length path > 0
4627 then (
4628 let anchor = getanchor () in
4629 let ranchor = state.path, state.password, anchor in
4630 state.anchor <- (pageno, 0.0);
4631 state.ranchors <- ranchor :: state.ranchors;
4632 opendoc path "";
4634 else showtext '!' ("Could not find " ^ filename)
4636 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4639 let canpan () =
4640 match conf.columns with
4641 | Csplit _ -> true
4642 | _ -> conf.zoom > 1.0
4645 let viewkeyboard key mask =
4646 let enttext te =
4647 let mode = state.mode in
4648 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4649 state.text <- "";
4650 enttext ();
4651 G.postRedisplay "view:enttext"
4653 let ctrl = Wsi.withctrl mask in
4654 match key with
4655 | 81 -> (* Q *)
4656 exit 0
4658 | 0xff63 -> (* insert *)
4659 if conf.angle mod 360 = 0
4660 then (
4661 state.mode <- LinkNav (Ltgendir 0);
4662 gotoy state.y;
4664 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4666 | 0xff1b | 113 -> (* escape / q *)
4667 begin match state.mstate with
4668 | Mzoomrect _ ->
4669 state.mstate <- Mnone;
4670 Wsi.setcursor Wsi.CURSOR_INHERIT;
4671 G.postRedisplay "kill zoom rect";
4672 | _ ->
4673 match state.ranchors with
4674 | [] -> raise Quit
4675 | (path, password, anchor) :: rest ->
4676 state.ranchors <- rest;
4677 state.anchor <- anchor;
4678 opendoc path password
4679 end;
4681 | 0xff08 -> (* backspace *)
4682 let y = getnav ~-1 in
4683 gotoy_and_clear_text y
4685 | 111 -> (* o *)
4686 enteroutlinemode ()
4688 | 117 -> (* u *)
4689 state.rects <- [];
4690 state.text <- "";
4691 G.postRedisplay "dehighlight";
4693 | 47 | 63 -> (* / ? *)
4694 let ondone isforw s =
4695 cbput state.hists.pat s;
4696 state.searchpattern <- s;
4697 search s isforw
4699 let s = String.create 1 in
4700 s.[0] <- Char.chr key;
4701 enttext (s, "", Some (onhist state.hists.pat),
4702 textentry, ondone (key = 47), true)
4704 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4705 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4706 setzoom (conf.zoom +. incr)
4708 | 43 | 0xffab -> (* + *)
4709 let ondone s =
4710 let n =
4711 try int_of_string s with exc ->
4712 state.text <- Printf.sprintf "bad integer `%s': %s"
4713 s (Printexc.to_string exc);
4714 max_int
4716 if n != max_int
4717 then (
4718 conf.pagebias <- n;
4719 state.text <- "page bias is now " ^ string_of_int n;
4722 enttext ("page bias: ", "", None, intentry, ondone, true)
4724 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4725 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4726 setzoom (max 0.01 (conf.zoom -. decr))
4728 | 45 | 0xffad -> (* - *)
4729 let ondone msg = state.text <- msg in
4730 enttext (
4731 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4732 optentry state.mode, ondone, true
4735 | 48 when ctrl -> (* ctrl-0 *)
4736 setzoom 1.0
4738 | 49 when ctrl -> (* 1 *)
4739 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4740 if zoom < 1.0
4741 then setzoom zoom
4743 | 0xffc6 -> (* f9 *)
4744 togglebirdseye ()
4746 | 57 when ctrl -> (* ctrl-9 *)
4747 togglebirdseye ()
4749 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4750 when not ctrl -> (* 0..9 *)
4751 let ondone s =
4752 let n =
4753 try int_of_string s with exc ->
4754 state.text <- Printf.sprintf "bad integer `%s': %s"
4755 s (Printexc.to_string exc);
4758 if n >= 0
4759 then (
4760 addnav ();
4761 cbput state.hists.pag (string_of_int n);
4762 gotopage1 (n + conf.pagebias - 1) 0;
4765 let pageentry text key =
4766 match Char.unsafe_chr key with
4767 | 'g' -> TEdone text
4768 | _ -> intentry text key
4770 let text = "x" in text.[0] <- Char.chr key;
4771 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4773 | 98 -> (* b *)
4774 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4775 reshape conf.winw conf.winh;
4777 | 108 -> (* l *)
4778 conf.hlinks <- not conf.hlinks;
4779 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4780 G.postRedisplay "toggle highlightlinks";
4782 | 70 -> (* F *)
4783 state.glinks <- true;
4784 let mode = state.mode in
4785 state.mode <- Textentry (
4786 (":", "", None, linknentry, linkndone (fun under ->
4787 addnav ();
4788 gotounder under
4789 ), false
4790 ), fun _ ->
4791 state.glinks <- false;
4792 state.mode <- mode
4794 state.text <- "";
4795 G.postRedisplay "view:linkent(F)"
4797 | 121 -> (* y *)
4798 state.glinks <- true;
4799 let mode = state.mode in
4800 state.mode <- Textentry (
4801 (":", "", None, linknentry, linkndone (fun under ->
4802 match Ne.pipe () with
4803 | Ne.Exn exn ->
4804 showtext '!' (Printf.sprintf "pipe failed: %s"
4805 (Printexc.to_string exn));
4806 | Ne.Res (r, w) ->
4807 let popened =
4808 try popen conf.selcmd [r, 0; w, -1]; true
4809 with exn ->
4810 showtext '!'
4811 (Printf.sprintf "failed to execute %s: %s"
4812 conf.selcmd (Printexc.to_string exn));
4813 false
4815 let clo cap fd =
4816 Ne.clo fd (fun msg ->
4817 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4820 let s = undertext under in
4821 if popened
4822 then
4823 (try
4824 let l = String.length s in
4825 let n = Unix.write w s 0 l in
4826 if n != l
4827 then
4828 showtext '!'
4829 (Printf.sprintf
4830 "failed to write %d characters to sel pipe, wrote %d"
4833 with exn ->
4834 showtext '!'
4835 (Printf.sprintf "failed to write to sel pipe: %s"
4836 (Printexc.to_string exn)
4839 else dolog "%s" s;
4840 clo "pipe/r" r;
4841 clo "pipe/w" w;
4842 ), false
4844 fun _ ->
4845 state.glinks <- false;
4846 state.mode <- mode
4848 state.text <- "";
4849 G.postRedisplay "view:linkent"
4851 | 97 -> (* a *)
4852 begin match state.autoscroll with
4853 | Some step ->
4854 conf.autoscrollstep <- step;
4855 state.autoscroll <- None
4856 | None ->
4857 if conf.autoscrollstep = 0
4858 then state.autoscroll <- Some 1
4859 else state.autoscroll <- Some conf.autoscrollstep
4862 | 112 when ctrl -> (* ctrl-p *)
4863 launchpath ()
4865 | 80 -> (* P *)
4866 conf.presentation <- not conf.presentation;
4867 if conf.presentation
4868 then (
4869 if not conf.scrollbarinpm
4870 then state.scrollw <- 0;
4872 else
4873 state.scrollw <- conf.scrollbw;
4875 showtext ' ' ("presentation mode " ^
4876 if conf.presentation then "on" else "off");
4877 state.anchor <- getanchor ();
4878 represent ()
4880 | 102 -> (* f *)
4881 begin match state.fullscreen with
4882 | None ->
4883 state.fullscreen <- Some (conf.winw, conf.winh);
4884 Wsi.fullscreen ()
4885 | Some (w, h) ->
4886 state.fullscreen <- None;
4887 doreshape w h
4890 | 103 -> (* g *)
4891 gotoy_and_clear_text 0
4893 | 71 -> (* G *)
4894 gotopage1 (state.pagecount - 1) 0
4896 | 112 | 78 -> (* p|N *)
4897 search state.searchpattern false
4899 | 110 | 0xffc0 -> (* n|F3 *)
4900 search state.searchpattern true
4902 | 116 -> (* t *)
4903 begin match state.layout with
4904 | [] -> ()
4905 | l :: _ ->
4906 gotoy_and_clear_text (getpagey l.pageno)
4909 | 32 -> (* ' ' *)
4910 begin match List.rev state.layout with
4911 | [] -> ()
4912 | l :: _ ->
4913 let pageno = min (l.pageno+1) (state.pagecount-1) in
4914 gotoy_and_clear_text (getpagey pageno)
4917 | 0xff9f | 0xffff -> (* delete *)
4918 begin match state.layout with
4919 | [] -> ()
4920 | l :: _ ->
4921 let pageno = max 0 (l.pageno-1) in
4922 gotoy_and_clear_text (getpagey pageno)
4925 | 61 -> (* = *)
4926 showtext ' ' (describe_location ());
4928 | 119 -> (* w *)
4929 begin match state.layout with
4930 | [] -> ()
4931 | l :: _ ->
4932 doreshape (l.pagew + state.scrollw) l.pageh;
4933 G.postRedisplay "w"
4936 | 39 -> (* ' *)
4937 enterbookmarkmode ()
4939 | 104 | 0xffbe -> (* h|F1 *)
4940 enterhelpmode ()
4942 | 105 -> (* i *)
4943 enterinfomode ()
4945 | 101 when conf.redirectstderr -> (* e *)
4946 entermsgsmode ()
4948 | 109 -> (* m *)
4949 let ondone s =
4950 match state.layout with
4951 | l :: _ ->
4952 state.bookmarks <-
4953 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4954 :: state.bookmarks
4955 | _ -> ()
4957 enttext ("bookmark: ", "", None, textentry, ondone, true)
4959 | 126 -> (* ~ *)
4960 quickbookmark ();
4961 showtext ' ' "Quick bookmark added";
4963 | 122 -> (* z *)
4964 begin match state.layout with
4965 | l :: _ ->
4966 let rect = getpdimrect l.pagedimno in
4967 let w, h =
4968 if conf.crophack
4969 then
4970 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4971 truncate (1.2 *. (rect.(3) -. rect.(0))))
4972 else
4973 (truncate (rect.(1) -. rect.(0)),
4974 truncate (rect.(3) -. rect.(0)))
4976 let w = truncate ((float w)*.conf.zoom)
4977 and h = truncate ((float h)*.conf.zoom) in
4978 if w != 0 && h != 0
4979 then (
4980 state.anchor <- getanchor ();
4981 doreshape (w + state.scrollw) (h + conf.interpagespace)
4983 G.postRedisplay "z";
4985 | [] -> ()
4988 | 50 when ctrl -> (* ctrl-2 *)
4989 let maxw = getmaxw () in
4990 if maxw > 0.0
4991 then setzoom (maxw /. float conf.winw)
4993 | 60 | 62 -> (* < > *)
4994 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4996 | 91 | 93 -> (* [ ] *)
4997 conf.colorscale <-
4998 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5000 G.postRedisplay "brightness";
5002 | 99 when state.mode = View -> (* c *)
5003 let (c, a, b), z =
5004 match state.prevcolumns with
5005 | None -> (1, 0, 0), 1.0
5006 | Some (columns, z) ->
5007 let cab =
5008 match columns with
5009 | Csplit (c, _) -> -c, 0, 0
5010 | Cmulti ((c, a, b), _) -> c, a, b
5011 | Csingle -> 1, 0, 0
5013 cab, z
5015 setcolumns View c a b;
5016 setzoom z;
5018 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5019 setzoom state.prevzoom
5021 | 107 | 0xff52 -> (* k up *)
5022 begin match state.autoscroll with
5023 | None ->
5024 begin match state.mode with
5025 | Birdseye beye -> upbirdseye 1 beye
5026 | _ ->
5027 if ctrl
5028 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5029 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5031 | Some n ->
5032 setautoscrollspeed n false
5035 | 106 | 0xff54 -> (* j down *)
5036 begin match state.autoscroll with
5037 | None ->
5038 begin match state.mode with
5039 | Birdseye beye -> downbirdseye 1 beye
5040 | _ ->
5041 if ctrl
5042 then gotoy_and_clear_text (clamp (conf.winh/2))
5043 else gotoy_and_clear_text (clamp conf.scrollstep)
5045 | Some n ->
5046 setautoscrollspeed n true
5049 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5050 if canpan ()
5051 then
5052 let dx =
5053 if ctrl
5054 then conf.winw / 2
5055 else 10
5057 let dx = if key = 0xff51 then dx else -dx in
5058 state.x <- state.x + dx;
5059 gotoy_and_clear_text state.y
5060 else (
5061 state.text <- "";
5062 G.postRedisplay "lef/right"
5065 | 0xff55 -> (* prior *)
5066 let y =
5067 if ctrl
5068 then
5069 match state.layout with
5070 | [] -> state.y
5071 | l :: _ -> state.y - l.pagey
5072 else
5073 clamp (-conf.winh)
5075 gotoghyll y
5077 | 0xff56 -> (* next *)
5078 let y =
5079 if ctrl
5080 then
5081 match List.rev state.layout with
5082 | [] -> state.y
5083 | l :: _ -> getpagey l.pageno
5084 else
5085 clamp conf.winh
5087 gotoghyll y
5089 | 0xff50 -> gotoghyll 0
5090 | 0xff57 -> gotoghyll (clamp state.maxy)
5091 | 0xff53 when Wsi.withalt mask ->
5092 gotoghyll (getnav ~-1)
5093 | 0xff51 when Wsi.withalt mask ->
5094 gotoghyll (getnav 1)
5096 | 114 -> (* r *)
5097 state.anchor <- getanchor ();
5098 opendoc state.path state.password
5100 | 118 when conf.debug -> (* v *)
5101 state.rects <- [];
5102 List.iter (fun l ->
5103 match getopaque l.pageno with
5104 | None -> ()
5105 | Some opaque ->
5106 let x0, y0, x1, y1 = pagebbox opaque in
5107 let a,b = float x0, float y0 in
5108 let c,d = float x1, float y0 in
5109 let e,f = float x1, float y1 in
5110 let h,j = float x0, float y1 in
5111 let rect = (a,b,c,d,e,f,h,j) in
5112 debugrect rect;
5113 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5114 ) state.layout;
5115 G.postRedisplay "v";
5117 | _ ->
5118 vlog "huh? %s" (Wsi.keyname key)
5121 let linknavkeyboard key mask linknav =
5122 let getpage pageno =
5123 let rec loop = function
5124 | [] -> None
5125 | l :: _ when l.pageno = pageno -> Some l
5126 | _ :: rest -> loop rest
5127 in loop state.layout
5129 let doexact (pageno, n) =
5130 match getopaque pageno, getpage pageno with
5131 | Some opaque, Some l ->
5132 if key = 0xff0d
5133 then
5134 let under = getlink opaque n in
5135 G.postRedisplay "link gotounder";
5136 gotounder under;
5137 state.mode <- View;
5138 else
5139 let opt, dir =
5140 match key with
5141 | 0xff50 -> (* home *)
5142 Some (findlink opaque LDfirst), -1
5144 | 0xff57 -> (* end *)
5145 Some (findlink opaque LDlast), 1
5147 | 0xff51 -> (* left *)
5148 Some (findlink opaque (LDleft n)), -1
5150 | 0xff53 -> (* right *)
5151 Some (findlink opaque (LDright n)), 1
5153 | 0xff52 -> (* up *)
5154 Some (findlink opaque (LDup n)), -1
5156 | 0xff54 -> (* down *)
5157 Some (findlink opaque (LDdown n)), 1
5159 | _ -> None, 0
5161 let pwl l dir =
5162 begin match findpwl l.pageno dir with
5163 | Pwlnotfound -> ()
5164 | Pwl pageno ->
5165 let notfound dir =
5166 state.mode <- LinkNav (Ltgendir dir);
5167 let y, h = getpageyh pageno in
5168 let y =
5169 if dir < 0
5170 then y + h - conf.winh
5171 else y
5173 gotoy y
5175 begin match getopaque pageno, getpage pageno with
5176 | Some opaque, Some _ ->
5177 let link =
5178 let ld = if dir > 0 then LDfirst else LDlast in
5179 findlink opaque ld
5181 begin match link with
5182 | Lfound m ->
5183 showlinktype (getlink opaque m);
5184 state.mode <- LinkNav (Ltexact (pageno, m));
5185 G.postRedisplay "linknav jpage";
5186 | _ -> notfound dir
5187 end;
5188 | _ -> notfound dir
5189 end;
5190 end;
5192 begin match opt with
5193 | Some Lnotfound -> pwl l dir;
5194 | Some (Lfound m) ->
5195 if m = n
5196 then pwl l dir
5197 else (
5198 let _, y0, _, y1 = getlinkrect opaque m in
5199 if y0 < l.pagey
5200 then gotopage1 l.pageno y0
5201 else (
5202 let d = fstate.fontsize + 1 in
5203 if y1 - l.pagey > l.pagevh - d
5204 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5205 else G.postRedisplay "linknav";
5207 showlinktype (getlink opaque m);
5208 state.mode <- LinkNav (Ltexact (l.pageno, m));
5211 | None -> viewkeyboard key mask
5212 end;
5213 | _ -> viewkeyboard key mask
5215 if key = 0xff63
5216 then (
5217 state.mode <- View;
5218 G.postRedisplay "leave linknav"
5220 else
5221 match linknav with
5222 | Ltgendir _ -> viewkeyboard key mask
5223 | Ltexact exact -> doexact exact
5226 let keyboard key mask =
5227 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5228 then wcmd "interrupt"
5229 else state.uioh <- state.uioh#key key mask
5232 let birdseyekeyboard key mask
5233 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5234 let incr =
5235 match conf.columns with
5236 | Csingle -> 1
5237 | Cmulti ((c, _, _), _) -> c
5238 | Csplit _ -> failwith "bird's eye split mode"
5240 match key with
5241 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5242 let y, h = getpageyh pageno in
5243 let top = (conf.winh - h) / 2 in
5244 gotoy (max 0 (y - top))
5245 | 0xff0d -> leavebirdseye beye false
5246 | 0xff1b -> leavebirdseye beye true (* escape *)
5247 | 0xff52 -> upbirdseye incr beye (* prior *)
5248 | 0xff54 -> downbirdseye incr beye (* next *)
5249 | 0xff51 -> upbirdseye 1 beye (* up *)
5250 | 0xff53 -> downbirdseye 1 beye (* down *)
5252 | 0xff55 ->
5253 begin match state.layout with
5254 | l :: _ ->
5255 if l.pagey != 0
5256 then (
5257 state.mode <- Birdseye (
5258 oconf, leftx, l.pageno, hooverpageno, anchor
5260 gotopage1 l.pageno 0;
5262 else (
5263 let layout = layout (state.y-conf.winh) conf.winh in
5264 match layout with
5265 | [] -> gotoy (clamp (-conf.winh))
5266 | l :: _ ->
5267 state.mode <- Birdseye (
5268 oconf, leftx, l.pageno, hooverpageno, anchor
5270 gotopage1 l.pageno 0
5273 | [] -> gotoy (clamp (-conf.winh))
5274 end;
5276 | 0xff56 ->
5277 begin match List.rev state.layout with
5278 | l :: _ ->
5279 let layout = layout (state.y + conf.winh) conf.winh in
5280 begin match layout with
5281 | [] ->
5282 let incr = l.pageh - l.pagevh in
5283 if incr = 0
5284 then (
5285 state.mode <-
5286 Birdseye (
5287 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5289 G.postRedisplay "birdseye pagedown";
5291 else gotoy (clamp (incr + conf.interpagespace*2));
5293 | l :: _ ->
5294 state.mode <-
5295 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5296 gotopage1 l.pageno 0;
5299 | [] -> gotoy (clamp conf.winh)
5300 end;
5302 | 0xff50 ->
5303 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5304 gotopage1 0 0
5306 | 0xff57 ->
5307 let pageno = state.pagecount - 1 in
5308 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5309 if not (pagevisible state.layout pageno)
5310 then
5311 let h =
5312 match List.rev state.pdims with
5313 | [] -> conf.winh
5314 | (_, _, h, _) :: _ -> h
5316 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5317 else G.postRedisplay "birdseye end";
5318 | _ -> viewkeyboard key mask
5321 let drawpage l linkindexbase =
5322 let color =
5323 match state.mode with
5324 | Textentry _ -> scalecolor 0.4
5325 | LinkNav _
5326 | View -> scalecolor 1.0
5327 | Birdseye (_, _, pageno, hooverpageno, _) ->
5328 if l.pageno = hooverpageno
5329 then scalecolor 0.9
5330 else (
5331 if l.pageno = pageno
5332 then scalecolor 1.0
5333 else scalecolor 0.8
5336 drawtiles l color;
5337 begin match getopaque l.pageno with
5338 | Some opaque ->
5339 if tileready l l.pagex l.pagey
5340 then
5341 let x = l.pagedispx - l.pagex
5342 and y = l.pagedispy - l.pagey in
5343 let hlmask =
5344 match conf.columns with
5345 | Csingle | Cmulti _ ->
5346 (if conf.hlinks then 1 else 0)
5347 + (if state.glinks
5348 && not (isbirdseye state.mode) then 2 else 0)
5349 | _ -> 0
5351 let s =
5352 match state.mode with
5353 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5354 | _ -> ""
5356 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5357 else 0
5359 | _ -> 0
5360 end;
5363 let scrollindicator () =
5364 let sbw, ph, sh = state.uioh#scrollph in
5365 let sbh, pw, sw = state.uioh#scrollpw in
5367 GlDraw.color (0.64, 0.64, 0.64);
5368 GlDraw.rect
5369 (float (conf.winw - sbw), 0.)
5370 (float conf.winw, float conf.winh)
5372 GlDraw.rect
5373 (0., float (conf.winh - sbh))
5374 (float (conf.winw - state.scrollw - 1), float conf.winh)
5376 GlDraw.color (0.0, 0.0, 0.0);
5378 GlDraw.rect
5379 (float (conf.winw - sbw), ph)
5380 (float conf.winw, ph +. sh)
5382 GlDraw.rect
5383 (pw, float (conf.winh - sbh))
5384 (pw +. sw, float conf.winh)
5388 let showsel () =
5389 match state.mstate with
5390 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5393 | Msel ((x0, y0), (x1, y1)) ->
5394 let rec loop = function
5395 | l :: ls ->
5396 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5397 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5398 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5399 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5400 then
5401 match getopaque l.pageno with
5402 | Some opaque ->
5403 let x0, y0 = pagetranslatepoint l x0 y0 in
5404 let x1, y1 = pagetranslatepoint l x1 y1 in
5405 seltext opaque (x0, y0, x1, y1);
5406 | _ -> ()
5407 else loop ls
5408 | [] -> ()
5410 loop state.layout
5413 let showrects rects =
5414 Gl.enable `blend;
5415 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5416 GlDraw.polygon_mode `both `fill;
5417 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5418 List.iter
5419 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5420 List.iter (fun l ->
5421 if l.pageno = pageno
5422 then (
5423 let dx = float (l.pagedispx - l.pagex) in
5424 let dy = float (l.pagedispy - l.pagey) in
5425 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5426 GlDraw.begins `quads;
5428 GlDraw.vertex2 (x0+.dx, y0+.dy);
5429 GlDraw.vertex2 (x1+.dx, y1+.dy);
5430 GlDraw.vertex2 (x2+.dx, y2+.dy);
5431 GlDraw.vertex2 (x3+.dx, y3+.dy);
5433 GlDraw.ends ();
5435 ) state.layout
5436 ) rects
5438 Gl.disable `blend;
5441 let display () =
5442 GlClear.color (scalecolor2 conf.bgcolor);
5443 GlClear.clear [`color];
5444 let rec loop linkindexbase = function
5445 | l :: rest ->
5446 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5447 loop linkindexbase rest
5448 | [] -> ()
5450 loop 0 state.layout;
5451 let rects =
5452 match state.mode with
5453 | LinkNav (Ltexact (pageno, linkno)) ->
5454 begin match getopaque pageno with
5455 | Some opaque ->
5456 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5457 (pageno, 5, (
5458 float x0, float y0,
5459 float x1, float y0,
5460 float x1, float y1,
5461 float x0, float y1)
5462 ) :: state.rects
5463 | None -> state.rects
5465 | _ -> state.rects
5467 showrects rects;
5468 showsel ();
5469 state.uioh#display;
5470 begin match state.mstate with
5471 | Mzoomrect ((x0, y0), (x1, y1)) ->
5472 Gl.enable `blend;
5473 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5474 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5475 GlDraw.rect (float x0, float y0)
5476 (float x1, float y1);
5477 Gl.disable `blend;
5478 | _ -> ()
5479 end;
5480 enttext ();
5481 scrollindicator ();
5482 Wsi.swapb ();
5485 let zoomrect x y x1 y1 =
5486 let x0 = min x x1
5487 and x1 = max x x1
5488 and y0 = min y y1 in
5489 gotoy (state.y + y0);
5490 state.anchor <- getanchor ();
5491 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5492 let margin =
5493 if state.w < conf.winw - state.scrollw
5494 then (conf.winw - state.scrollw - state.w) / 2
5495 else 0
5497 state.x <- (state.x + margin) - x0;
5498 setzoom zoom;
5499 Wsi.setcursor Wsi.CURSOR_INHERIT;
5500 state.mstate <- Mnone;
5503 let scrollx x =
5504 let winw = conf.winw - state.scrollw - 1 in
5505 let s = float x /. float winw in
5506 let destx = truncate (float (state.w + winw) *. s) in
5507 state.x <- winw - destx;
5508 gotoy_and_clear_text state.y;
5509 state.mstate <- Mscrollx;
5512 let scrolly y =
5513 let s = float y /. float conf.winh in
5514 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5515 gotoy_and_clear_text desty;
5516 state.mstate <- Mscrolly;
5519 let viewmouse button down x y mask =
5520 match button with
5521 | n when (n == 4 || n == 5) && not down ->
5522 if Wsi.withctrl mask
5523 then (
5524 match state.mstate with
5525 | Mzoom (oldn, i) ->
5526 if oldn = n
5527 then (
5528 if i = 2
5529 then
5530 let incr =
5531 match n with
5532 | 5 ->
5533 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5534 | _ ->
5535 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5537 let zoom = conf.zoom -. incr in
5538 setzoom zoom;
5539 state.mstate <- Mzoom (n, 0);
5540 else
5541 state.mstate <- Mzoom (n, i+1);
5543 else state.mstate <- Mzoom (n, 0)
5545 | _ -> state.mstate <- Mzoom (n, 0)
5547 else (
5548 match state.autoscroll with
5549 | Some step -> setautoscrollspeed step (n=4)
5550 | None ->
5551 let incr =
5552 if n = 4
5553 then -conf.scrollstep
5554 else conf.scrollstep
5556 let incr = incr * 2 in
5557 let y = clamp incr in
5558 gotoy_and_clear_text y
5561 | 1 when Wsi.withctrl mask ->
5562 if down
5563 then (
5564 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5565 state.mstate <- Mpan (x, y)
5567 else
5568 state.mstate <- Mnone
5570 | 3 ->
5571 if down
5572 then (
5573 Wsi.setcursor Wsi.CURSOR_CYCLE;
5574 let p = (x, y) in
5575 state.mstate <- Mzoomrect (p, p)
5577 else (
5578 match state.mstate with
5579 | Mzoomrect ((x0, y0), _) ->
5580 if abs (x-x0) > 10 && abs (y - y0) > 10
5581 then zoomrect x0 y0 x y
5582 else (
5583 state.mstate <- Mnone;
5584 Wsi.setcursor Wsi.CURSOR_INHERIT;
5585 G.postRedisplay "kill accidental zoom rect";
5587 | _ ->
5588 Wsi.setcursor Wsi.CURSOR_INHERIT;
5589 state.mstate <- Mnone
5592 | 1 when x > conf.winw - state.scrollw ->
5593 if down
5594 then
5595 let _, position, sh = state.uioh#scrollph in
5596 if y > truncate position && y < truncate (position +. sh)
5597 then state.mstate <- Mscrolly
5598 else scrolly y
5599 else
5600 state.mstate <- Mnone
5602 | 1 when y > conf.winh - state.hscrollh ->
5603 if down
5604 then
5605 let _, position, sw = state.uioh#scrollpw in
5606 if x > truncate position && x < truncate (position +. sw)
5607 then state.mstate <- Mscrollx
5608 else scrollx x
5609 else
5610 state.mstate <- Mnone
5612 | 1 ->
5613 let dest = if down then getunder x y else Unone in
5614 begin match dest with
5615 | Ulinkgoto _
5616 | Ulinkuri _
5617 | Uremote _
5618 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5619 gotounder dest
5621 | Unone when down ->
5622 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5623 state.mstate <- Mpan (x, y);
5625 | Unone | Utext _ ->
5626 if down
5627 then (
5628 if conf.angle mod 360 = 0
5629 then (
5630 state.mstate <- Msel ((x, y), (x, y));
5631 G.postRedisplay "mouse select";
5634 else (
5635 match state.mstate with
5636 | Mnone -> ()
5638 | Mzoom _ | Mscrollx | Mscrolly ->
5639 state.mstate <- Mnone
5641 | Mzoomrect ((x0, y0), _) ->
5642 zoomrect x0 y0 x y
5644 | Mpan _ ->
5645 Wsi.setcursor Wsi.CURSOR_INHERIT;
5646 state.mstate <- Mnone
5648 | Msel ((_, y0), (_, y1)) ->
5649 let rec loop = function
5650 | [] -> ()
5651 | l :: rest ->
5652 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5653 || ((y1 >= l.pagedispy
5654 && y1 <= (l.pagedispy + l.pagevh)))
5655 then
5656 match getopaque l.pageno with
5657 | Some opaque ->
5658 begin
5659 match Ne.pipe () with
5660 | Ne.Exn exn ->
5661 showtext '!'
5662 (Printf.sprintf
5663 "can not create sel pipe: %s"
5664 (Printexc.to_string exn));
5665 | Ne.Res (r, w) ->
5666 let doclose what fd =
5667 Ne.clo fd (fun msg ->
5668 dolog "%s close failed: %s" what msg)
5671 popen conf.selcmd [r, 0; w, -1];
5672 copysel w opaque;
5673 doclose "pipe/r" r;
5674 G.postRedisplay "copysel";
5675 with exn ->
5676 dolog "can not execute %S: %s"
5677 conf.selcmd (Printexc.to_string exn);
5678 doclose "pipe/r" r;
5679 doclose "pipe/w" w;
5681 | None -> ()
5682 else loop rest
5684 loop state.layout;
5685 Wsi.setcursor Wsi.CURSOR_INHERIT;
5686 state.mstate <- Mnone;
5690 | _ -> ()
5693 let birdseyemouse button down x y mask
5694 (conf, leftx, _, hooverpageno, anchor) =
5695 match button with
5696 | 1 when down ->
5697 let rec loop = function
5698 | [] -> ()
5699 | l :: rest ->
5700 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5701 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5702 then (
5703 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5705 else loop rest
5707 loop state.layout
5708 | 3 -> ()
5709 | _ -> viewmouse button down x y mask
5712 let mouse button down x y mask =
5713 state.uioh <- state.uioh#button button down x y mask;
5716 let motion ~x ~y =
5717 state.uioh <- state.uioh#motion x y
5720 let pmotion ~x ~y =
5721 state.uioh <- state.uioh#pmotion x y;
5724 let uioh = object
5725 method display = ()
5727 method key key mask =
5728 begin match state.mode with
5729 | Textentry textentry -> textentrykeyboard key mask textentry
5730 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5731 | View -> viewkeyboard key mask
5732 | LinkNav linknav -> linknavkeyboard key mask linknav
5733 end;
5734 state.uioh
5736 method button button bstate x y mask =
5737 begin match state.mode with
5738 | LinkNav _
5739 | View -> viewmouse button bstate x y mask
5740 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5741 | Textentry _ -> ()
5742 end;
5743 state.uioh
5745 method motion x y =
5746 begin match state.mode with
5747 | Textentry _ -> ()
5748 | View | Birdseye _ | LinkNav _ ->
5749 match state.mstate with
5750 | Mzoom _ | Mnone -> ()
5752 | Mpan (x0, y0) ->
5753 let dx = x - x0
5754 and dy = y0 - y in
5755 state.mstate <- Mpan (x, y);
5756 if canpan ()
5757 then state.x <- state.x + dx;
5758 let y = clamp dy in
5759 gotoy_and_clear_text y
5761 | Msel (a, _) ->
5762 state.mstate <- Msel (a, (x, y));
5763 G.postRedisplay "motion select";
5765 | Mscrolly ->
5766 let y = min conf.winh (max 0 y) in
5767 scrolly y
5769 | Mscrollx ->
5770 let x = min conf.winw (max 0 x) in
5771 scrollx x
5773 | Mzoomrect (p0, _) ->
5774 state.mstate <- Mzoomrect (p0, (x, y));
5775 G.postRedisplay "motion zoomrect";
5776 end;
5777 state.uioh
5779 method pmotion x y =
5780 begin match state.mode with
5781 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5782 let rec loop = function
5783 | [] ->
5784 if hooverpageno != -1
5785 then (
5786 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5787 G.postRedisplay "pmotion birdseye no hoover";
5789 | l :: rest ->
5790 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5791 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5792 then (
5793 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5794 G.postRedisplay "pmotion birdseye hoover";
5796 else loop rest
5798 loop state.layout
5800 | Textentry _ -> ()
5802 | LinkNav _
5803 | View ->
5804 match state.mstate with
5805 | Mnone -> updateunder x y
5806 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5808 end;
5809 state.uioh
5811 method infochanged _ = ()
5813 method scrollph =
5814 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5815 let p, h = scrollph state.y maxy in
5816 state.scrollw, p, h
5818 method scrollpw =
5819 let winw = conf.winw - state.scrollw - 1 in
5820 let fwinw = float winw in
5821 let sw =
5822 let sw = fwinw /. float state.w in
5823 let sw = fwinw *. sw in
5824 max sw (float conf.scrollh)
5826 let position, sw =
5827 let f = state.w+winw in
5828 let r = float (winw-state.x) /. float f in
5829 let p = fwinw *. r in
5830 p-.sw/.2., sw
5832 let sw =
5833 if position +. sw > fwinw
5834 then fwinw -. position
5835 else sw
5837 state.hscrollh, position, sw
5839 method modehash =
5840 let modename =
5841 match state.mode with
5842 | LinkNav _ -> "links"
5843 | Textentry _ -> "textentry"
5844 | Birdseye _ -> "birdseye"
5845 | View -> "view"
5847 findkeyhash conf modename
5848 end;;
5850 module Config =
5851 struct
5852 open Parser
5854 let fontpath = ref "";;
5856 module KeyMap =
5857 Map.Make (struct type t = (int * int) let compare = compare end);;
5859 let unent s =
5860 let l = String.length s in
5861 let b = Buffer.create l in
5862 unent b s 0 l;
5863 Buffer.contents b;
5866 let home =
5867 try Sys.getenv "HOME"
5868 with exn ->
5869 prerr_endline
5870 ("Can not determine home directory location: " ^
5871 Printexc.to_string exn);
5875 let modifier_of_string = function
5876 | "alt" -> Wsi.altmask
5877 | "shift" -> Wsi.shiftmask
5878 | "ctrl" | "control" -> Wsi.ctrlmask
5879 | "meta" -> Wsi.metamask
5880 | _ -> 0
5883 let key_of_string =
5884 let r = Str.regexp "-" in
5885 fun s ->
5886 let elems = Str.full_split r s in
5887 let f n k m =
5888 let g s =
5889 let m1 = modifier_of_string s in
5890 if m1 = 0
5891 then (Wsi.namekey s, m)
5892 else (k, m lor m1)
5893 in function
5894 | Str.Delim s when n land 1 = 0 -> g s
5895 | Str.Text s -> g s
5896 | Str.Delim _ -> (k, m)
5898 let rec loop n k m = function
5899 | [] -> (k, m)
5900 | x :: xs ->
5901 let k, m = f n k m x in
5902 loop (n+1) k m xs
5904 loop 0 0 0 elems
5907 let keys_of_string =
5908 let r = Str.regexp "[ \t]" in
5909 fun s ->
5910 let elems = Str.split r s in
5911 List.map key_of_string elems
5914 let copykeyhashes c =
5915 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5918 let config_of c attrs =
5919 let apply c k v =
5921 match k with
5922 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5923 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5924 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5925 | "preload" -> { c with preload = bool_of_string v }
5926 | "page-bias" -> { c with pagebias = int_of_string v }
5927 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5928 | "auto-scroll-step" ->
5929 { c with autoscrollstep = max 0 (int_of_string v) }
5930 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5931 | "crop-hack" -> { c with crophack = bool_of_string v }
5932 | "throttle" ->
5933 let mw =
5934 match String.lowercase v with
5935 | "true" -> Some infinity
5936 | "false" -> None
5937 | f -> Some (float_of_string f)
5939 { c with maxwait = mw}
5940 | "highlight-links" -> { c with hlinks = bool_of_string v }
5941 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5942 | "vertical-margin" ->
5943 { c with interpagespace = max 0 (int_of_string v) }
5944 | "zoom" ->
5945 let zoom = float_of_string v /. 100. in
5946 let zoom = max zoom 0.0 in
5947 { c with zoom = zoom }
5948 | "presentation" -> { c with presentation = bool_of_string v }
5949 | "rotation-angle" -> { c with angle = int_of_string v }
5950 | "width" -> { c with winw = max 20 (int_of_string v) }
5951 | "height" -> { c with winh = max 20 (int_of_string v) }
5952 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5953 | "proportional-display" -> { c with proportional = bool_of_string v }
5954 | "pixmap-cache-size" ->
5955 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5956 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5957 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5958 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5959 | "persistent-location" -> { c with jumpback = bool_of_string v }
5960 | "background-color" -> { c with bgcolor = color_of_string v }
5961 | "scrollbar-in-presentation" ->
5962 { c with scrollbarinpm = bool_of_string v }
5963 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5964 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5965 | "mupdf-store-size" ->
5966 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5967 | "checkers" -> { c with checkers = bool_of_string v }
5968 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5969 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5970 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5971 | "uri-launcher" -> { c with urilauncher = unent v }
5972 | "path-launcher" -> { c with pathlauncher = unent v }
5973 | "color-space" -> { c with colorspace = colorspace_of_string v }
5974 | "invert-colors" -> { c with invert = bool_of_string v }
5975 | "brightness" -> { c with colorscale = float_of_string v }
5976 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5977 | "ghyllscroll" ->
5978 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5979 | "columns" ->
5980 let (n, _, _) as nab = multicolumns_of_string v in
5981 if n < 0
5982 then { c with columns = Csplit (-n, [||]) }
5983 else { c with columns = Cmulti (nab, [||]) }
5984 | "birds-eye-columns" ->
5985 { c with beyecolumns = Some (max (int_of_string v) 2) }
5986 | "selection-command" -> { c with selcmd = unent v }
5987 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5988 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5989 | _ -> c
5990 with exn ->
5991 prerr_endline ("Error processing attribute (`" ^
5992 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5995 let rec fold c = function
5996 | [] -> c
5997 | (k, v) :: rest ->
5998 let c = apply c k v in
5999 fold c rest
6001 fold { c with keyhashes = copykeyhashes c } attrs;
6004 let fromstring f pos n v d =
6005 try f v
6006 with exn ->
6007 dolog "Error processing attribute (%S=%S) at %d\n%s"
6008 n v pos (Printexc.to_string exn)
6013 let bookmark_of attrs =
6014 let rec fold title page rely = function
6015 | ("title", v) :: rest -> fold v page rely rest
6016 | ("page", v) :: rest -> fold title v rely rest
6017 | ("rely", v) :: rest -> fold title page v rest
6018 | _ :: rest -> fold title page rely rest
6019 | [] -> title, page, rely
6021 fold "invalid" "0" "0" attrs
6024 let doc_of attrs =
6025 let rec fold path page rely pan = function
6026 | ("path", v) :: rest -> fold v page rely pan rest
6027 | ("page", v) :: rest -> fold path v rely pan rest
6028 | ("rely", v) :: rest -> fold path page v pan rest
6029 | ("pan", v) :: rest -> fold path page rely v rest
6030 | _ :: rest -> fold path page rely pan rest
6031 | [] -> path, page, rely, pan
6033 fold "" "0" "0" "0" attrs
6036 let map_of attrs =
6037 let rec fold rs ls = function
6038 | ("out", v) :: rest -> fold v ls rest
6039 | ("in", v) :: rest -> fold rs v rest
6040 | _ :: rest -> fold ls rs rest
6041 | [] -> ls, rs
6043 fold "" "" attrs
6046 let setconf dst src =
6047 dst.scrollbw <- src.scrollbw;
6048 dst.scrollh <- src.scrollh;
6049 dst.icase <- src.icase;
6050 dst.preload <- src.preload;
6051 dst.pagebias <- src.pagebias;
6052 dst.verbose <- src.verbose;
6053 dst.scrollstep <- src.scrollstep;
6054 dst.maxhfit <- src.maxhfit;
6055 dst.crophack <- src.crophack;
6056 dst.autoscrollstep <- src.autoscrollstep;
6057 dst.maxwait <- src.maxwait;
6058 dst.hlinks <- src.hlinks;
6059 dst.underinfo <- src.underinfo;
6060 dst.interpagespace <- src.interpagespace;
6061 dst.zoom <- src.zoom;
6062 dst.presentation <- src.presentation;
6063 dst.angle <- src.angle;
6064 dst.winw <- src.winw;
6065 dst.winh <- src.winh;
6066 dst.savebmarks <- src.savebmarks;
6067 dst.memlimit <- src.memlimit;
6068 dst.proportional <- src.proportional;
6069 dst.texcount <- src.texcount;
6070 dst.sliceheight <- src.sliceheight;
6071 dst.thumbw <- src.thumbw;
6072 dst.jumpback <- src.jumpback;
6073 dst.bgcolor <- src.bgcolor;
6074 dst.scrollbarinpm <- src.scrollbarinpm;
6075 dst.tilew <- src.tilew;
6076 dst.tileh <- src.tileh;
6077 dst.mustoresize <- src.mustoresize;
6078 dst.checkers <- src.checkers;
6079 dst.aalevel <- src.aalevel;
6080 dst.trimmargins <- src.trimmargins;
6081 dst.trimfuzz <- src.trimfuzz;
6082 dst.urilauncher <- src.urilauncher;
6083 dst.colorspace <- src.colorspace;
6084 dst.invert <- src.invert;
6085 dst.colorscale <- src.colorscale;
6086 dst.redirectstderr <- src.redirectstderr;
6087 dst.ghyllscroll <- src.ghyllscroll;
6088 dst.columns <- src.columns;
6089 dst.beyecolumns <- src.beyecolumns;
6090 dst.selcmd <- src.selcmd;
6091 dst.updatecurs <- src.updatecurs;
6092 dst.pathlauncher <- src.pathlauncher;
6093 dst.keyhashes <- copykeyhashes src;
6094 dst.hfsize <- src.hfsize;
6097 let get s =
6098 let h = Hashtbl.create 10 in
6099 let dc = { defconf with angle = defconf.angle } in
6100 let rec toplevel v t spos _ =
6101 match t with
6102 | Vdata | Vcdata | Vend -> v
6103 | Vopen ("llppconfig", _, closed) ->
6104 if closed
6105 then v
6106 else { v with f = llppconfig }
6107 | Vopen _ ->
6108 error "unexpected subelement at top level" s spos
6109 | Vclose _ -> error "unexpected close at top level" s spos
6111 and llppconfig v t spos _ =
6112 match t with
6113 | Vdata | Vcdata -> v
6114 | Vend -> error "unexpected end of input in llppconfig" s spos
6115 | Vopen ("defaults", attrs, closed) ->
6116 let c = config_of dc attrs in
6117 setconf dc c;
6118 if closed
6119 then v
6120 else { v with f = defaults }
6122 | Vopen ("ui-font", attrs, closed) ->
6123 let rec getsize size = function
6124 | [] -> size
6125 | ("size", v) :: rest ->
6126 let size =
6127 fromstring int_of_string spos "size" v fstate.fontsize in
6128 getsize size rest
6129 | l -> getsize size l
6131 fstate.fontsize <- getsize fstate.fontsize attrs;
6132 if closed
6133 then v
6134 else { v with f = uifont (Buffer.create 10) }
6136 | Vopen ("doc", attrs, closed) ->
6137 let pathent, spage, srely, span = doc_of attrs in
6138 let path = unent pathent
6139 and pageno = fromstring int_of_string spos "page" spage 0
6140 and rely = fromstring float_of_string spos "rely" srely 0.0
6141 and pan = fromstring int_of_string spos "pan" span 0 in
6142 let c = config_of dc attrs in
6143 let anchor = (pageno, rely) in
6144 if closed
6145 then (Hashtbl.add h path (c, [], pan, anchor); v)
6146 else { v with f = doc path pan anchor c [] }
6148 | Vopen _ ->
6149 error "unexpected subelement in llppconfig" s spos
6151 | Vclose "llppconfig" -> { v with f = toplevel }
6152 | Vclose _ -> error "unexpected close in llppconfig" s spos
6154 and defaults v t spos _ =
6155 match t with
6156 | Vdata | Vcdata -> v
6157 | Vend -> error "unexpected end of input in defaults" s spos
6158 | Vopen ("keymap", attrs, closed) ->
6159 let modename =
6160 try List.assoc "mode" attrs
6161 with Not_found -> "global" in
6162 if closed
6163 then v
6164 else
6165 let ret keymap =
6166 let h = findkeyhash dc modename in
6167 KeyMap.iter (Hashtbl.replace h) keymap;
6168 defaults
6170 { v with f = pkeymap ret KeyMap.empty }
6172 | Vopen (_, _, _) ->
6173 error "unexpected subelement in defaults" s spos
6175 | Vclose "defaults" ->
6176 { v with f = llppconfig }
6178 | Vclose _ -> error "unexpected close in defaults" s spos
6180 and uifont b v t spos epos =
6181 match t with
6182 | Vdata | Vcdata ->
6183 Buffer.add_substring b s spos (epos - spos);
6185 | Vopen (_, _, _) ->
6186 error "unexpected subelement in ui-font" s spos
6187 | Vclose "ui-font" ->
6188 if String.length !fontpath = 0
6189 then fontpath := Buffer.contents b;
6190 { v with f = llppconfig }
6191 | Vclose _ -> error "unexpected close in ui-font" s spos
6192 | Vend -> error "unexpected end of input in ui-font" s spos
6194 and doc path pan anchor c bookmarks v t spos _ =
6195 match t with
6196 | Vdata | Vcdata -> v
6197 | Vend -> error "unexpected end of input in doc" s spos
6198 | Vopen ("bookmarks", _, closed) ->
6199 if closed
6200 then v
6201 else { v with f = pbookmarks path pan anchor c bookmarks }
6203 | Vopen ("keymap", attrs, closed) ->
6204 let modename =
6205 try List.assoc "mode" attrs
6206 with Not_found -> "global"
6208 if closed
6209 then v
6210 else
6211 let ret keymap =
6212 let h = findkeyhash c modename in
6213 KeyMap.iter (Hashtbl.replace h) keymap;
6214 doc path pan anchor c bookmarks
6216 { v with f = pkeymap ret KeyMap.empty }
6218 | Vopen (_, _, _) ->
6219 error "unexpected subelement in doc" s spos
6221 | Vclose "doc" ->
6222 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6223 { v with f = llppconfig }
6225 | Vclose _ -> error "unexpected close in doc" s spos
6227 and pkeymap ret keymap v t spos _ =
6228 match t with
6229 | Vdata | Vcdata -> v
6230 | Vend -> error "unexpected end of input in keymap" s spos
6231 | Vopen ("map", attrs, closed) ->
6232 let r, l = map_of attrs in
6233 let kss = fromstring keys_of_string spos "in" r [] in
6234 let lss = fromstring keys_of_string spos "out" l [] in
6235 let keymap =
6236 match kss with
6237 | [] -> keymap
6238 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6239 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6241 if closed
6242 then { v with f = pkeymap ret keymap }
6243 else
6244 let f () = v in
6245 { v with f = skip "map" f }
6247 | Vopen _ ->
6248 error "unexpected subelement in keymap" s spos
6250 | Vclose "keymap" ->
6251 { v with f = ret keymap }
6253 | Vclose _ -> error "unexpected close in keymap" s spos
6255 and pbookmarks path pan anchor c bookmarks v t spos _ =
6256 match t with
6257 | Vdata | Vcdata -> v
6258 | Vend -> error "unexpected end of input in bookmarks" s spos
6259 | Vopen ("item", attrs, closed) ->
6260 let titleent, spage, srely = bookmark_of attrs in
6261 let page = fromstring int_of_string spos "page" spage 0
6262 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6263 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6264 if closed
6265 then { v with f = pbookmarks path pan anchor c bookmarks }
6266 else
6267 let f () = v in
6268 { v with f = skip "item" f }
6270 | Vopen _ ->
6271 error "unexpected subelement in bookmarks" s spos
6273 | Vclose "bookmarks" ->
6274 { v with f = doc path pan anchor c bookmarks }
6276 | Vclose _ -> error "unexpected close in bookmarks" s spos
6278 and skip tag f v t spos _ =
6279 match t with
6280 | Vdata | Vcdata -> v
6281 | Vend ->
6282 error ("unexpected end of input in skipped " ^ tag) s spos
6283 | Vopen (tag', _, closed) ->
6284 if closed
6285 then v
6286 else
6287 let f' () = { v with f = skip tag f } in
6288 { v with f = skip tag' f' }
6289 | Vclose ctag ->
6290 if tag = ctag
6291 then f ()
6292 else error ("unexpected close in skipped " ^ tag) s spos
6295 parse { f = toplevel; accu = () } s;
6296 h, dc;
6299 let do_load f ic =
6301 let len = in_channel_length ic in
6302 let s = String.create len in
6303 really_input ic s 0 len;
6304 f s;
6305 with
6306 | Parse_error (msg, s, pos) ->
6307 let subs = subs s pos in
6308 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6309 failwith ("parse error: " ^ s)
6311 | exn ->
6312 failwith ("config load error: " ^ Printexc.to_string exn)
6315 let defconfpath =
6316 let dir =
6318 let dir = Filename.concat home ".config" in
6319 if Sys.is_directory dir then dir else home
6320 with _ -> home
6322 Filename.concat dir "llpp.conf"
6325 let confpath = ref defconfpath;;
6327 let load1 f =
6328 if Sys.file_exists !confpath
6329 then
6330 match
6331 (try Some (open_in_bin !confpath)
6332 with exn ->
6333 prerr_endline
6334 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6335 Printexc.to_string exn);
6336 None
6338 with
6339 | Some ic ->
6340 begin try
6341 f (do_load get ic)
6342 with exn ->
6343 prerr_endline
6344 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6345 Printexc.to_string exn);
6346 end;
6347 close_in ic;
6349 | None -> ()
6350 else
6351 f (Hashtbl.create 0, defconf)
6354 let load () =
6355 let f (h, dc) =
6356 let pc, pb, px, pa =
6358 Hashtbl.find h (Filename.basename state.path)
6359 with Not_found -> dc, [], 0, (0, 0.0)
6361 setconf defconf dc;
6362 setconf conf pc;
6363 state.bookmarks <- pb;
6364 state.x <- px;
6365 state.scrollw <- conf.scrollbw;
6366 if conf.jumpback
6367 then state.anchor <- pa;
6368 cbput state.hists.nav pa;
6370 load1 f
6373 let add_attrs bb always dc c =
6374 let ob s a b =
6375 if always || a != b
6376 then Printf.bprintf bb "\n %s='%b'" s a
6377 and oi s a b =
6378 if always || a != b
6379 then Printf.bprintf bb "\n %s='%d'" s a
6380 and oI s a b =
6381 if always || a != b
6382 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6383 and oz s a b =
6384 if always || a <> b
6385 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6386 and oF s a b =
6387 if always || a <> b
6388 then Printf.bprintf bb "\n %s='%f'" s a
6389 and oc s a b =
6390 if always || a <> b
6391 then
6392 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6393 and oC s a b =
6394 if always || a <> b
6395 then
6396 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6397 and oR s a b =
6398 if always || a <> b
6399 then
6400 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6401 and os s a b =
6402 if always || a <> b
6403 then
6404 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6405 and og s a b =
6406 if always || a <> b
6407 then
6408 match a with
6409 | None -> ()
6410 | Some (_N, _A, _B) ->
6411 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6412 and oW s a b =
6413 if always || a <> b
6414 then
6415 let v =
6416 match a with
6417 | None -> "false"
6418 | Some f ->
6419 if f = infinity
6420 then "true"
6421 else string_of_float f
6423 Printf.bprintf bb "\n %s='%s'" s v
6424 and oco s a b =
6425 if always || a <> b
6426 then
6427 match a with
6428 | Cmulti ((n, a, b), _) when n > 1 ->
6429 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6430 | Csplit (n, _) when n > 1 ->
6431 Printf.bprintf bb "\n %s='%d'" s ~-n
6432 | _ -> ()
6433 and obeco s a b =
6434 if always || a <> b
6435 then
6436 match a with
6437 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6438 | _ -> ()
6440 let w, h =
6441 if always
6442 then dc.winw, dc.winh
6443 else
6444 match state.fullscreen with
6445 | Some wh -> wh
6446 | None -> c.winw, c.winh
6448 let zoom, presentation, interpagespace, maxwait =
6449 if always
6450 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6451 else
6452 match state.mode with
6453 | Birdseye (bc, _, _, _, _) ->
6454 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6455 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6457 oi "width" w dc.winw;
6458 oi "height" h dc.winh;
6459 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6460 oi "scroll-handle-height" c.scrollh dc.scrollh;
6461 ob "case-insensitive-search" c.icase dc.icase;
6462 ob "preload" c.preload dc.preload;
6463 oi "page-bias" c.pagebias dc.pagebias;
6464 oi "scroll-step" c.scrollstep dc.scrollstep;
6465 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6466 ob "max-height-fit" c.maxhfit dc.maxhfit;
6467 ob "crop-hack" c.crophack dc.crophack;
6468 oW "throttle" maxwait dc.maxwait;
6469 ob "highlight-links" c.hlinks dc.hlinks;
6470 ob "under-cursor-info" c.underinfo dc.underinfo;
6471 oi "vertical-margin" interpagespace dc.interpagespace;
6472 oz "zoom" zoom dc.zoom;
6473 ob "presentation" presentation dc.presentation;
6474 oi "rotation-angle" c.angle dc.angle;
6475 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6476 ob "proportional-display" c.proportional dc.proportional;
6477 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6478 oi "tex-count" c.texcount dc.texcount;
6479 oi "slice-height" c.sliceheight dc.sliceheight;
6480 oi "thumbnail-width" c.thumbw dc.thumbw;
6481 ob "persistent-location" c.jumpback dc.jumpback;
6482 oc "background-color" c.bgcolor dc.bgcolor;
6483 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6484 oi "tile-width" c.tilew dc.tilew;
6485 oi "tile-height" c.tileh dc.tileh;
6486 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6487 ob "checkers" c.checkers dc.checkers;
6488 oi "aalevel" c.aalevel dc.aalevel;
6489 ob "trim-margins" c.trimmargins dc.trimmargins;
6490 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6491 os "uri-launcher" c.urilauncher dc.urilauncher;
6492 os "path-launcher" c.pathlauncher dc.pathlauncher;
6493 oC "color-space" c.colorspace dc.colorspace;
6494 ob "invert-colors" c.invert dc.invert;
6495 oF "brightness" c.colorscale dc.colorscale;
6496 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6497 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6498 oco "columns" c.columns dc.columns;
6499 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6500 os "selection-command" c.selcmd dc.selcmd;
6501 ob "update-cursor" c.updatecurs dc.updatecurs;
6502 oi "hint-font-size" c.hfsize dc.hfsize;
6505 let keymapsbuf always dc c =
6506 let bb = Buffer.create 16 in
6507 let rec loop = function
6508 | [] -> ()
6509 | (modename, h) :: rest ->
6510 let dh = findkeyhash dc modename in
6511 if always || h <> dh
6512 then (
6513 if Hashtbl.length h > 0
6514 then (
6515 if Buffer.length bb > 0
6516 then Buffer.add_char bb '\n';
6517 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6518 Hashtbl.iter (fun i o ->
6519 let isdifferent = always ||
6521 let dO = Hashtbl.find dh i in
6522 dO <> o
6523 with Not_found -> true
6525 if isdifferent
6526 then
6527 let addkm (k, m) =
6528 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6529 if Wsi.withalt m then Buffer.add_string bb "alt-";
6530 if Wsi.withshift m then Buffer.add_string bb "shift-";
6531 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6532 Buffer.add_string bb (Wsi.keyname k);
6534 let addkms l =
6535 let rec loop = function
6536 | [] -> ()
6537 | km :: [] -> addkm km
6538 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6540 loop l
6542 Buffer.add_string bb "<map in='";
6543 addkm i;
6544 match o with
6545 | KMinsrt km ->
6546 Buffer.add_string bb "' out='";
6547 addkm km;
6548 Buffer.add_string bb "'/>\n"
6550 | KMinsrl kms ->
6551 Buffer.add_string bb "' out='";
6552 addkms kms;
6553 Buffer.add_string bb "'/>\n"
6555 | KMmulti (ins, kms) ->
6556 Buffer.add_char bb ' ';
6557 addkms ins;
6558 Buffer.add_string bb "' out='";
6559 addkms kms;
6560 Buffer.add_string bb "'/>\n"
6561 ) h;
6562 Buffer.add_string bb "</keymap>";
6565 loop rest
6567 loop c.keyhashes;
6571 let save () =
6572 let uifontsize = fstate.fontsize in
6573 let bb = Buffer.create 32768 in
6574 let f (h, dc) =
6575 let dc = if conf.bedefault then conf else dc in
6576 Buffer.add_string bb "<llppconfig>\n";
6578 if String.length !fontpath > 0
6579 then
6580 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6581 uifontsize
6582 !fontpath
6583 else (
6584 if uifontsize <> 14
6585 then
6586 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6589 Buffer.add_string bb "<defaults ";
6590 add_attrs bb true dc dc;
6591 let kb = keymapsbuf true dc dc in
6592 if Buffer.length kb > 0
6593 then (
6594 Buffer.add_string bb ">\n";
6595 Buffer.add_buffer bb kb;
6596 Buffer.add_string bb "\n</defaults>\n";
6598 else Buffer.add_string bb "/>\n";
6600 let adddoc path pan anchor c bookmarks =
6601 if bookmarks == [] && c = dc && anchor = emptyanchor
6602 then ()
6603 else (
6604 Printf.bprintf bb "<doc path='%s'"
6605 (enent path 0 (String.length path));
6607 if anchor <> emptyanchor
6608 then (
6609 let n, y = anchor in
6610 Printf.bprintf bb " page='%d'" n;
6611 if y > 1e-6
6612 then
6613 Printf.bprintf bb " rely='%f'" y
6617 if pan != 0
6618 then Printf.bprintf bb " pan='%d'" pan;
6620 add_attrs bb false dc c;
6621 let kb = keymapsbuf false dc c in
6623 begin match bookmarks with
6624 | [] ->
6625 if Buffer.length kb > 0
6626 then (
6627 Buffer.add_string bb ">\n";
6628 Buffer.add_buffer bb kb;
6629 Buffer.add_string bb "\n</doc>\n";
6631 else Buffer.add_string bb "/>\n"
6632 | _ ->
6633 Buffer.add_string bb ">\n<bookmarks>\n";
6634 List.iter (fun (title, _level, (page, rely)) ->
6635 Printf.bprintf bb
6636 "<item title='%s' page='%d'"
6637 (enent title 0 (String.length title))
6638 page
6640 if rely > 1e-6
6641 then
6642 Printf.bprintf bb " rely='%f'" rely
6644 Buffer.add_string bb "/>\n";
6645 ) bookmarks;
6646 Buffer.add_string bb "</bookmarks>";
6647 if Buffer.length kb > 0
6648 then (
6649 Buffer.add_string bb "\n";
6650 Buffer.add_buffer bb kb;
6652 Buffer.add_string bb "\n</doc>\n";
6653 end;
6657 let pan, conf =
6658 match state.mode with
6659 | Birdseye (c, pan, _, _, _) ->
6660 let beyecolumns =
6661 match conf.columns with
6662 | Cmulti ((c, _, _), _) -> Some c
6663 | Csingle -> None
6664 | Csplit _ -> None
6665 and columns =
6666 match c.columns with
6667 | Cmulti (c, _) -> Cmulti (c, [||])
6668 | Csingle -> Csingle
6669 | Csplit _ -> failwith "quit from bird's eye while split"
6671 pan, { c with beyecolumns = beyecolumns; columns = columns }
6672 | _ -> state.x, conf
6674 let basename = Filename.basename state.path in
6675 adddoc basename pan (getanchor ())
6676 { conf with
6677 autoscrollstep =
6678 match state.autoscroll with
6679 | Some step -> step
6680 | None -> conf.autoscrollstep }
6681 (if conf.savebmarks then state.bookmarks else []);
6683 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6684 if basename <> path
6685 then adddoc path x y c bookmarks
6686 ) h;
6687 Buffer.add_string bb "</llppconfig>";
6689 load1 f;
6690 if Buffer.length bb > 0
6691 then
6693 let tmp = !confpath ^ ".tmp" in
6694 let oc = open_out_bin tmp in
6695 Buffer.output_buffer oc bb;
6696 close_out oc;
6697 Unix.rename tmp !confpath;
6698 with exn ->
6699 prerr_endline
6700 ("error while saving configuration: " ^ Printexc.to_string exn)
6702 end;;
6704 let () =
6705 Arg.parse
6706 (Arg.align
6707 [("-p", Arg.String (fun s -> state.password <- s) ,
6708 "<password> Set password");
6710 ("-f", Arg.String (fun s -> Config.fontpath := s),
6711 "<path> Set path to the user interface font");
6713 ("-c", Arg.String (fun s -> Config.confpath := s),
6714 "<path> Set path to the configuration file");
6716 ("-v", Arg.Unit (fun () ->
6717 Printf.printf
6718 "%s\nconfiguration path: %s\n"
6719 (version ())
6720 Config.defconfpath
6722 exit 0), " Print version and exit");
6725 (fun s -> state.path <- s)
6726 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6728 if String.length state.path = 0
6729 then (prerr_endline "file name missing"; exit 1);
6731 Config.load ();
6733 let globalkeyhash = findkeyhash conf "global" in
6734 let wsfd, winw, winh = Wsi.init (object
6735 method expose =
6736 if nogeomcmds state.geomcmds || platform == Posx
6737 then display ()
6738 else (
6739 GlClear.color (scalecolor2 conf.bgcolor);
6740 GlClear.clear [`color];
6742 method display = display ()
6743 method reshape w h = reshape w h
6744 method mouse b d x y m = mouse b d x y m
6745 method motion x y = state.mpos <- (x, y); motion x y
6746 method pmotion x y = state.mpos <- (x, y); pmotion x y
6747 method key k m =
6748 let mascm = m land (
6749 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6750 ) in
6751 match state.keystate with
6752 | KSnone ->
6753 let km = k, mascm in
6754 begin
6755 match
6756 let modehash = state.uioh#modehash in
6757 try Hashtbl.find modehash km
6758 with Not_found ->
6759 try Hashtbl.find globalkeyhash km
6760 with Not_found -> KMinsrt (k, m)
6761 with
6762 | KMinsrt (k, m) -> keyboard k m
6763 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6764 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6766 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6767 List.iter (fun (k, m) -> keyboard k m) insrt;
6768 state.keystate <- KSnone
6769 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6770 state.keystate <- KSinto (keys, insrt)
6771 | _ ->
6772 state.keystate <- KSnone
6774 method enter x y = state.mpos <- (x, y); pmotion x y
6775 method leave = state.mpos <- (-1, -1)
6776 method quit = raise Quit
6777 end) conf.winw conf.winh (platform = Posx) in
6779 state.wsfd <- wsfd;
6781 if not (
6782 List.exists GlMisc.check_extension
6783 [ "GL_ARB_texture_rectangle"
6784 ; "GL_EXT_texture_recangle"
6785 ; "GL_NV_texture_rectangle" ]
6787 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6789 let cr, sw =
6790 match Ne.pipe () with
6791 | Ne.Exn exn ->
6792 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6793 exit 1
6794 | Ne.Res rw -> rw
6795 and sr, cw =
6796 match Ne.pipe () with
6797 | Ne.Exn exn ->
6798 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6799 exit 1
6800 | Ne.Res rw -> rw
6803 cloexec cr;
6804 cloexec sw;
6805 cloexec sr;
6806 cloexec cw;
6808 setcheckers conf.checkers;
6809 redirectstderr ();
6811 init (cr, cw) (
6812 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6813 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6814 !Config.fontpath
6816 state.sr <- sr;
6817 state.sw <- sw;
6818 state.text <- "Opening " ^ state.path;
6819 reshape winw winh;
6820 opendoc state.path state.password;
6821 state.uioh <- uioh;
6823 let rec loop deadline =
6824 let r =
6825 match state.errfd with
6826 | None -> [state.sr; state.wsfd]
6827 | Some fd -> [state.sr; state.wsfd; fd]
6829 if state.redisplay
6830 then (
6831 state.redisplay <- false;
6832 display ();
6834 let timeout =
6835 let now = now () in
6836 if deadline > now
6837 then (
6838 if deadline = infinity
6839 then ~-.1.0
6840 else max 0.0 (deadline -. now)
6842 else 0.0
6844 let r, _, _ =
6845 try Unix.select r [] [] timeout
6846 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6848 begin match r with
6849 | [] ->
6850 state.ghyll None;
6851 let newdeadline =
6852 if state.ghyll == noghyll
6853 then
6854 match state.autoscroll with
6855 | Some step when step != 0 ->
6856 let y = state.y + step in
6857 let y =
6858 if y < 0
6859 then state.maxy
6860 else if y >= state.maxy then 0 else y
6862 gotoy y;
6863 if state.mode = View
6864 then state.text <- "";
6865 deadline +. 0.01
6866 | _ -> infinity
6867 else deadline +. 0.01
6869 loop newdeadline
6871 | l ->
6872 let rec checkfds = function
6873 | [] -> ()
6874 | fd :: rest when fd = state.sr ->
6875 let cmd = readcmd state.sr in
6876 act cmd;
6877 checkfds rest
6879 | fd :: rest when fd = state.wsfd ->
6880 Wsi.readresp fd;
6881 checkfds rest
6883 | fd :: rest ->
6884 let s = String.create 80 in
6885 let n = Unix.read fd s 0 80 in
6886 if conf.redirectstderr
6887 then (
6888 Buffer.add_substring state.errmsgs s 0 n;
6889 state.newerrmsgs <- true;
6890 state.redisplay <- true;
6892 else (
6893 prerr_string (String.sub s 0 n);
6894 flush stderr;
6896 checkfds rest
6898 checkfds l;
6899 let newdeadline =
6900 let deadline1 =
6901 if deadline = infinity
6902 then now () +. 0.01
6903 else deadline
6905 match state.autoscroll with
6906 | Some step when step != 0 -> deadline1
6907 | _ -> if state.ghyll == noghyll then infinity else deadline1
6909 loop newdeadline
6910 end;
6913 loop infinity;
6914 with Quit ->
6915 Config.save ();
6916 exit 0;