-F full-split
[llpp.git] / main.ml
blob631ccb9147de84143a192c4e4602acf903951d63
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
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 te =
160 | TEstop
161 | TEdone of string
162 | TEcont of string
163 | TEswitch of textentry
166 type 'a circbuf =
167 { store : 'a array
168 ; mutable rc : int
169 ; mutable wc : int
170 ; mutable len : int
174 let bound v minv maxv =
175 max minv (min maxv v);
178 let cbnew n v =
179 { store = Array.create n v
180 ; rc = 0
181 ; wc = 0
182 ; len = 0
186 let drawstring size x y s =
187 Gl.enable `blend;
188 Gl.enable `texture_2d;
189 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
190 ignore (drawstr size x y s);
191 Gl.disable `blend;
192 Gl.disable `texture_2d;
195 let drawstring1 size x y s =
196 drawstr size x y s;
199 let drawstring2 size x y fmt =
200 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
203 let cbcap b = Array.length b.store;;
205 let cbput b v =
206 let cap = cbcap b in
207 b.store.(b.wc) <- v;
208 b.wc <- (b.wc + 1) mod cap;
209 b.rc <- b.wc;
210 b.len <- min (b.len + 1) cap;
213 let cbempty b = b.len = 0;;
215 let cbgetg b circular dir =
216 if cbempty b
217 then b.store.(0)
218 else
219 let rc = b.rc + dir in
220 let rc =
221 if circular
222 then (
223 if rc = -1
224 then b.len-1
225 else (
226 if rc = b.len
227 then 0
228 else rc
231 else max 0 (min rc (b.len-1))
233 b.rc <- rc;
234 b.store.(rc);
237 let cbget b = cbgetg b false;;
238 let cbgetc b = cbgetg b true;;
240 type page =
241 { pageno : int
242 ; pagedimno : int
243 ; pagew : int
244 ; pageh : int
245 ; pagex : int
246 ; pagey : int
247 ; pagevw : int
248 ; pagevh : int
249 ; pagedispx : int
250 ; pagedispy : int
251 ; pagecol : int
255 let debugl l =
256 dolog "l %d dim=%d {" l.pageno l.pagedimno;
257 dolog " WxH %dx%d" l.pagew l.pageh;
258 dolog " vWxH %dx%d" l.pagevw l.pagevh;
259 dolog " pagex,y %d,%d" l.pagex l.pagey;
260 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
261 dolog " column %d" l.pagecol;
262 dolog "}";
265 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
266 dolog "rect {";
267 dolog " x0,y0=(% f, % f)" x0 y0;
268 dolog " x1,y1=(% f, % f)" x1 y1;
269 dolog " x2,y2=(% f, % f)" x2 y2;
270 dolog " x3,y3=(% f, % f)" x3 y3;
271 dolog "}";
274 type multicolumns = multicol * pagegeom
275 and splitcolumns = columncount * pagegeom
276 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
277 and multicol = columncount * covercount * covercount
278 and pdimno = int
279 and columncount = int
280 and covercount = int;;
282 type conf =
283 { mutable scrollbw : int
284 ; mutable scrollh : int
285 ; mutable icase : bool
286 ; mutable preload : bool
287 ; mutable pagebias : int
288 ; mutable verbose : bool
289 ; mutable debug : bool
290 ; mutable scrollstep : int
291 ; mutable maxhfit : bool
292 ; mutable crophack : bool
293 ; mutable autoscrollstep : int
294 ; mutable maxwait : float option
295 ; mutable hlinks : bool
296 ; mutable underinfo : bool
297 ; mutable interpagespace : interpagespace
298 ; mutable zoom : float
299 ; mutable presentation : bool
300 ; mutable angle : angle
301 ; mutable winw : int
302 ; mutable winh : int
303 ; mutable savebmarks : bool
304 ; mutable proportional : proportional
305 ; mutable trimmargins : trimmargins
306 ; mutable trimfuzz : irect
307 ; mutable memlimit : memsize
308 ; mutable texcount : texcount
309 ; mutable sliceheight : sliceheight
310 ; mutable thumbw : width
311 ; mutable jumpback : bool
312 ; mutable bgcolor : float * float * float
313 ; mutable bedefault : bool
314 ; mutable scrollbarinpm : bool
315 ; mutable tilew : int
316 ; mutable tileh : int
317 ; mutable mustoresize : memsize
318 ; mutable checkers : bool
319 ; mutable aalevel : int
320 ; mutable urilauncher : string
321 ; mutable pathlauncher : string
322 ; mutable colorspace : colorspace
323 ; mutable invert : bool
324 ; mutable colorscale : float
325 ; mutable redirectstderr : bool
326 ; mutable ghyllscroll : (int * int * int) option
327 ; mutable columns : columns
328 ; mutable beyecolumns : columncount option
329 ; mutable selcmd : string
330 ; mutable updatecurs : bool
331 ; mutable keyhashes : (string * keyhash) list
332 ; mutable hfsize : int
333 ; mutable fullsplit : bool
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
463 and hists =
464 { pat : string circbuf
465 ; pag : string circbuf
466 ; nav : anchor circbuf
467 ; sel : string circbuf
471 let defconf =
472 { scrollbw = 7
473 ; scrollh = 12
474 ; icase = true
475 ; preload = true
476 ; pagebias = 0
477 ; verbose = false
478 ; debug = false
479 ; scrollstep = 24
480 ; maxhfit = true
481 ; crophack = false
482 ; autoscrollstep = 2
483 ; maxwait = None
484 ; hlinks = false
485 ; underinfo = false
486 ; interpagespace = 2
487 ; zoom = 1.0
488 ; presentation = false
489 ; angle = 0
490 ; winw = 900
491 ; winh = 900
492 ; savebmarks = true
493 ; proportional = true
494 ; trimmargins = false
495 ; trimfuzz = (0,0,0,0)
496 ; memlimit = 32 lsl 20
497 ; texcount = 256
498 ; sliceheight = 24
499 ; thumbw = 76
500 ; jumpback = true
501 ; bgcolor = (0.5, 0.5, 0.5)
502 ; bedefault = false
503 ; scrollbarinpm = true
504 ; tilew = 2048
505 ; tileh = 2048
506 ; mustoresize = 256 lsl 20
507 ; checkers = true
508 ; aalevel = 8
509 ; urilauncher =
510 (match platform with
511 | Plinux | Pfreebsd | Pdragonflybsd
512 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
513 | Posx -> "open \"%s\""
514 | Pcygwin -> "cygstart \"%s\""
515 | Punknown -> "echo %s")
516 ; pathlauncher = "lp \"%s\""
517 ; selcmd =
518 (match platform with
519 | Plinux | Pfreebsd | Pdragonflybsd
520 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
521 | Posx -> "pbcopy"
522 | Pcygwin -> "wsel"
523 | Punknown -> "cat")
524 ; colorspace = Rgb
525 ; invert = false
526 ; colorscale = 1.0
527 ; redirectstderr = false
528 ; ghyllscroll = None
529 ; columns = Csingle
530 ; beyecolumns = None
531 ; updatecurs = false
532 ; hfsize = 12
533 ; fullsplit = false
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
699 let vlog fmt =
700 if conf.verbose
701 then
702 Printf.kprintf prerr_endline fmt
703 else
704 Printf.kprintf ignore fmt
707 let launchpath () =
708 if String.length conf.pathlauncher = 0
709 then print_endline state.path
710 else (
711 let re = Str.regexp "%s" in
712 let command = Str.global_replace re state.path conf.pathlauncher in
713 try popen command []
714 with exn ->
715 Printf.eprintf
716 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
717 flush stderr;
721 module Ne = struct
722 type 'a t = | Res of 'a | Exn of exn;;
724 let pipe () =
725 try Res (Unix.pipe ())
726 with exn -> Exn exn
729 let clo fd f =
730 try Unix.close fd
731 with exn -> f (Printexc.to_string exn)
734 let dup fd =
735 try Res (Unix.dup fd)
736 with exn -> Exn exn
739 let dup2 fd1 fd2 =
740 try Res (Unix.dup2 fd1 fd2)
741 with exn -> Exn exn
743 end;;
745 let redirectstderr () =
746 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
747 if conf.redirectstderr
748 then
749 match Ne.pipe () with
750 | Ne.Exn exn ->
751 dolog "failed to create stderr redirection pipes: %s"
752 (Printexc.to_string exn)
754 | Ne.Res (r, w) ->
755 begin match Ne.dup Unix.stderr with
756 | Ne.Exn exn ->
757 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
758 Ne.clo r (clofail "pipe/r");
759 Ne.clo w (clofail "pipe/w");
761 | Ne.Res dupstderr ->
762 begin match Ne.dup2 w Unix.stderr with
763 | Ne.Exn exn ->
764 dolog "failed to dup2 to stderr: %s"
765 (Printexc.to_string exn);
766 Ne.clo dupstderr (clofail "stderr duplicate");
767 Ne.clo r (clofail "redir pipe/r");
768 Ne.clo w (clofail "redir pipe/w");
770 | Ne.Res () ->
771 state.stderr <- dupstderr;
772 state.errfd <- Some r;
773 end;
775 else (
776 state.newerrmsgs <- false;
777 begin match state.errfd with
778 | Some fd ->
779 begin match Ne.dup2 state.stderr Unix.stderr with
780 | Ne.Exn exn ->
781 dolog "failed to dup2 original stderr: %s"
782 (Printexc.to_string exn)
783 | Ne.Res () ->
784 Ne.clo fd (clofail "dup of stderr");
785 Unix.dup2 state.stderr Unix.stderr;
786 state.errfd <- None;
787 end;
788 | None -> ()
789 end;
790 prerr_string (Buffer.contents state.errmsgs);
791 flush stderr;
792 Buffer.clear state.errmsgs;
796 module G =
797 struct
798 let postRedisplay who =
799 if conf.verbose
800 then prerr_endline ("redisplay for " ^ who);
801 state.redisplay <- true;
803 end;;
805 let getopaque pageno =
806 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
807 with Not_found -> None
810 let putopaque pageno opaque =
811 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
814 let pagetranslatepoint l x y =
815 let dy = y - l.pagedispy in
816 let y = dy + l.pagey in
817 let dx = x - l.pagedispx in
818 let x = dx + l.pagex in
819 (x, y);
822 let getunder x y =
823 let rec f = function
824 | l :: rest ->
825 begin match getopaque l.pageno with
826 | Some opaque ->
827 let x0 = l.pagedispx in
828 let x1 = x0 + l.pagevw in
829 let y0 = l.pagedispy in
830 let y1 = y0 + l.pagevh in
831 if y >= y0 && y <= y1 && x >= x0 && x <= x1
832 then
833 let px, py = pagetranslatepoint l x y in
834 match whatsunder opaque px py with
835 | Unone -> f rest
836 | under -> under
837 else f rest
838 | _ ->
839 f rest
841 | [] -> Unone
843 f state.layout
846 let showtext c s =
847 state.text <- Printf.sprintf "%c%s" c s;
848 G.postRedisplay "showtext";
851 let undertext = function
852 | Unone -> "none"
853 | Ulinkuri s -> s
854 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
855 | Utext s -> "font: " ^ s
856 | Uunexpected s -> "unexpected: " ^ s
857 | Ulaunch s -> "launch: " ^ s
858 | Unamed s -> "named: " ^ s
859 | Uremote (filename, pageno) ->
860 Printf.sprintf "%s: page %d" filename (pageno+1)
863 let updateunder x y =
864 match getunder x y with
865 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
866 | Ulinkuri uri ->
867 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
868 Wsi.setcursor Wsi.CURSOR_INFO
869 | Ulinkgoto (pageno, _) ->
870 if conf.underinfo
871 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
872 Wsi.setcursor Wsi.CURSOR_INFO
873 | Utext s ->
874 if conf.underinfo then showtext 'f' ("ont: " ^ s);
875 Wsi.setcursor Wsi.CURSOR_TEXT
876 | Uunexpected s ->
877 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
878 Wsi.setcursor Wsi.CURSOR_INHERIT
879 | Ulaunch s ->
880 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
881 Wsi.setcursor Wsi.CURSOR_INHERIT
882 | Unamed s ->
883 if conf.underinfo then showtext 'n' ("amed: " ^ s);
884 Wsi.setcursor Wsi.CURSOR_INHERIT
885 | Uremote (filename, pageno) ->
886 if conf.underinfo then showtext 'r'
887 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
888 Wsi.setcursor Wsi.CURSOR_INFO
891 let showlinktype under =
892 if conf.underinfo
893 then
894 match under with
895 | Unone -> ()
896 | under ->
897 let s = undertext under in
898 showtext ' ' s
901 let addchar s c =
902 let b = Buffer.create (String.length s + 1) in
903 Buffer.add_string b s;
904 Buffer.add_char b c;
905 Buffer.contents b;
908 let colorspace_of_string s =
909 match String.lowercase s with
910 | "rgb" -> Rgb
911 | "bgr" -> Bgr
912 | "gray" -> Gray
913 | _ -> failwith "invalid colorspace"
916 let int_of_colorspace = function
917 | Rgb -> 0
918 | Bgr -> 1
919 | Gray -> 2
922 let colorspace_of_int = function
923 | 0 -> Rgb
924 | 1 -> Bgr
925 | 2 -> Gray
926 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
929 let colorspace_to_string = function
930 | Rgb -> "rgb"
931 | Bgr -> "bgr"
932 | Gray -> "gray"
935 let intentry_with_suffix text key =
936 let c =
937 if key >= 32 && key < 127
938 then Char.chr key
939 else '\000'
941 match Char.lowercase c with
942 | '0' .. '9' ->
943 let text = addchar text c in
944 TEcont text
946 | 'k' | 'm' | 'g' ->
947 let text = addchar text c in
948 TEcont text
950 | _ ->
951 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
952 TEcont text
955 let multicolumns_to_string (n, a, b) =
956 if a = 0 && b = 0
957 then Printf.sprintf "%d" n
958 else Printf.sprintf "%d,%d,%d" n a b;
961 let multicolumns_of_string s =
963 (int_of_string s, 0, 0)
964 with _ ->
965 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
968 let readcmd fd =
969 let s = "xxxx" in
970 let n = Unix.read fd s 0 4 in
971 if n != 4 then failwith "incomplete read(len)";
972 let len = 0
973 lor (Char.code s.[0] lsl 24)
974 lor (Char.code s.[1] lsl 16)
975 lor (Char.code s.[2] lsl 8)
976 lor (Char.code s.[3] lsl 0)
978 let s = String.create len in
979 let n = Unix.read fd s 0 len in
980 if n != len then failwith "incomplete read(data)";
984 let btod b = if b then 1 else 0;;
986 let wcmd fmt =
987 let b = Buffer.create 16 in
988 Buffer.add_string b "llll";
989 Printf.kbprintf
990 (fun b ->
991 let s = Buffer.contents b in
992 let n = String.length s in
993 let len = n - 4 in
994 (* dolog "wcmd %S" (String.sub s 4 len); *)
995 s.[0] <- Char.chr ((len lsr 24) land 0xff);
996 s.[1] <- Char.chr ((len lsr 16) land 0xff);
997 s.[2] <- Char.chr ((len lsr 8) land 0xff);
998 s.[3] <- Char.chr (len land 0xff);
999 let n' = Unix.write state.sw s 0 n in
1000 if n' != n then failwith "write failed";
1001 ) b fmt;
1004 let calcips h =
1005 if conf.presentation
1006 then
1007 let d = conf.winh - h in
1008 max 0 ((d + 1) / 2)
1009 else
1010 conf.interpagespace
1013 let calcheight () =
1014 let rec f pn ph pi fh l =
1015 match l with
1016 | (n, _, h, _) :: rest ->
1017 let ips = calcips h in
1018 let fh =
1019 if conf.presentation
1020 then fh+ips
1021 else (
1022 if isbirdseye state.mode && pn = 0
1023 then fh + ips
1024 else fh
1027 let fh = fh + ((n - pn) * (ph + pi)) in
1028 f n h ips fh rest;
1030 | [] ->
1031 let inc =
1032 if conf.presentation || (isbirdseye state.mode && pn = 0)
1033 then 0
1034 else -pi
1036 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1037 max 0 fh
1039 let fh = f 0 0 0 0 state.pdims in
1043 let calcheight () =
1044 match conf.columns with
1045 | Csingle -> calcheight ()
1046 | Cmulti ((c, _, _), b) ->
1047 let rec loop y h n =
1048 if n < 0
1049 then loop y h (n+1)
1050 else (
1051 if n = Array.length b
1052 then y + h
1053 else
1054 let (_, _, y', (_, _, h', _)) = b.(n) in
1055 let y = min y y'
1056 and h = max h h' in
1057 loop y h (n+1)
1060 loop max_int 0 (((Array.length b - 1) / c) * c)
1061 | Csplit (_, b) ->
1062 if Array.length b > 0
1063 then
1064 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1065 y + h
1066 else 0
1069 let getpageyh pageno =
1070 let rec f pn ph pi y l =
1071 match l with
1072 | (n, _, h, _) :: rest ->
1073 let ips = calcips h in
1074 if n >= pageno
1075 then
1076 let h = if n = pageno then h else ph in
1077 if conf.presentation && n = pageno
1078 then
1079 y + (pageno - pn) * (ph + pi) + pi, h
1080 else
1081 y + (pageno - pn) * (ph + pi), h
1082 else
1083 let y = y + (if conf.presentation then pi else 0) in
1084 let y = y + (n - pn) * (ph + pi) in
1085 f n h ips y rest
1087 | [] ->
1088 y + (pageno - pn) * (ph + pi), ph
1090 f 0 0 0 0 state.pdims
1093 let getpageyh pageno =
1094 match conf.columns with
1095 | Csingle -> getpageyh pageno
1096 | Cmulti (_, b) ->
1097 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1098 y, h
1099 | Csplit (c, b) ->
1100 let n = pageno*c in
1101 let (_, _, y, (_, _, h, _)) = b.(n) in
1102 y, h
1105 let getpagedim pageno =
1106 let rec f ppdim l =
1107 match l with
1108 | (n, _, _, _) as pdim :: rest ->
1109 if n >= pageno
1110 then (if n = pageno then pdim else ppdim)
1111 else f pdim rest
1113 | [] -> ppdim
1115 f (-1, -1, -1, -1) state.pdims
1118 let getpagey pageno = fst (getpageyh pageno);;
1120 let nogeomcmds cmds =
1121 match cmds with
1122 | s, [] -> String.length s = 0
1123 | _ -> false
1126 let layout1 y sh =
1127 let sh = sh - state.hscrollh in
1128 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1129 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1130 match pdims with
1131 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1132 let ips = calcips h in
1133 let yinc =
1134 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1135 then ips
1136 else 0
1138 (w, h, ips, xoff), rest, pdimno + 1, yinc
1139 | _ ->
1140 prev, pdims, pdimno, 0
1142 let dy = dy + yinc in
1143 let py = py + yinc in
1144 if pageno = state.pagecount || dy >= sh
1145 then
1146 accu
1147 else
1148 let vy = y + dy in
1149 if py + h <= vy - yinc
1150 then
1151 let py = py + h + ips in
1152 let dy = max 0 (py - y) in
1153 f ~pageno:(pageno+1)
1154 ~pdimno
1155 ~prev:curr
1158 ~pdims:rest
1159 ~accu
1160 else
1161 let pagey = vy - py in
1162 let pagevh = h - pagey in
1163 let pagevh = min (sh - dy) pagevh in
1164 let off = if yinc > 0 then py - vy else 0 in
1165 let py = py + h + ips in
1166 let pagex, dx =
1167 let xoff = xoff +
1168 if state.w < conf.winw - state.scrollw
1169 then (conf.winw - state.scrollw - state.w) / 2
1170 else 0
1172 let dispx = xoff + state.x in
1173 if dispx < 0
1174 then (-dispx, 0)
1175 else (0, dispx)
1177 let pagevw =
1178 let lw = w - pagex in
1179 min lw (conf.winw - state.scrollw)
1181 let e =
1182 { pageno = pageno
1183 ; pagedimno = pdimno
1184 ; pagew = w
1185 ; pageh = h
1186 ; pagex = pagex
1187 ; pagey = pagey + off
1188 ; pagevw = pagevw
1189 ; pagevh = pagevh - off
1190 ; pagedispx = dx
1191 ; pagedispy = dy + off
1192 ; pagecol = 0
1195 let accu = e :: accu in
1196 f ~pageno:(pageno+1)
1197 ~pdimno
1198 ~prev:curr
1200 ~dy:(dy+pagevh+ips)
1201 ~pdims:rest
1202 ~accu
1204 let accu =
1206 ~pageno:0
1207 ~pdimno:~-1
1208 ~prev:(0,0,0,0)
1209 ~py:0
1210 ~dy:0
1211 ~pdims:state.pdims
1212 ~accu:[]
1214 List.rev accu
1217 let layoutN ((columns, coverA, coverB), b) y sh =
1218 let sh = sh - state.hscrollh in
1219 let rec fold accu n =
1220 if n = Array.length b
1221 then accu
1222 else
1223 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1224 if (vy - y) > sh &&
1225 (n = coverA - 1
1226 || n = state.pagecount - coverB
1227 || (n - coverA) mod columns = columns - 1)
1228 then accu
1229 else
1230 let accu =
1231 if vy + h > y
1232 then
1233 let pagey = max 0 (y - vy) in
1234 let pagedispy = if pagey > 0 then 0 else vy - y in
1235 let pagedispx, pagex =
1236 let pdx =
1237 if n = coverA - 1 || n = state.pagecount - coverB
1238 then state.x + (conf.winw - state.scrollw - w) / 2
1239 else dx + xoff + state.x
1241 if pdx < 0
1242 then 0, -pdx
1243 else pdx, 0
1245 let pagevw =
1246 let vw = conf.winw - state.scrollw - pagedispx in
1247 let pw = w - pagex in
1248 min vw pw
1250 let pagevh = min (h - pagey) (sh - pagedispy) in
1251 if pagevw > 0 && pagevh > 0
1252 then
1253 let e =
1254 { pageno = n
1255 ; pagedimno = pdimno
1256 ; pagew = w
1257 ; pageh = h
1258 ; pagex = pagex
1259 ; pagey = pagey
1260 ; pagevw = pagevw
1261 ; pagevh = pagevh
1262 ; pagedispx = pagedispx
1263 ; pagedispy = pagedispy
1264 ; pagecol = 0
1267 e :: accu
1268 else
1269 accu
1270 else
1271 accu
1273 fold accu (n+1)
1275 List.rev (fold [] 0)
1278 let layoutS (columns, b) y sh =
1279 let sh = sh - state.hscrollh in
1280 let rec fold accu n =
1281 if n = Array.length b
1282 then accu
1283 else
1284 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1285 if (vy - y) > sh
1286 then accu
1287 else
1288 let accu =
1289 if vy + pageh > y
1290 then
1291 let x = xoff + state.x in
1292 let pagey = max 0 (y - vy) in
1293 let pagedispy = if pagey > 0 then 0 else vy - y in
1294 let pagedispx, pagex =
1295 if px = 0
1296 then (
1297 if x < 0
1298 then 0, -x
1299 else x, 0
1301 else (
1302 let px = px - x in
1303 if px < 0
1304 then -px, 0
1305 else 0, px
1308 let pagecolw = pagew/columns in
1309 let pagedispx =
1310 if pagecolw < conf.winw
1311 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1312 else pagedispx
1314 let pagevw =
1315 let vw = conf.winw - pagedispx - state.scrollw in
1316 let pw = pagew - pagex in
1317 min vw pw
1319 let pagevw =
1320 if conf.fullsplit
1321 then pagevw
1322 else min pagevw pagecolw
1324 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1325 if pagevw > 0 && pagevh > 0
1326 then
1327 let e =
1328 { pageno = n/columns
1329 ; pagedimno = pdimno
1330 ; pagew = pagew
1331 ; pageh = pageh
1332 ; pagex = pagex
1333 ; pagey = pagey
1334 ; pagevw = pagevw
1335 ; pagevh = pagevh
1336 ; pagedispx = pagedispx
1337 ; pagedispy = pagedispy
1338 ; pagecol = n mod columns
1341 e :: accu
1342 else
1343 accu
1344 else
1345 accu
1347 fold accu (n+1)
1349 List.rev (fold [] 0)
1352 let layout y sh =
1353 if nogeomcmds state.geomcmds
1354 then
1355 match conf.columns with
1356 | Csingle -> layout1 y sh
1357 | Cmulti c -> layoutN c y sh
1358 | Csplit s -> layoutS s y sh
1359 else []
1362 let clamp incr =
1363 let y = state.y + incr in
1364 let y = max 0 y in
1365 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1369 let itertiles l f =
1370 let tilex = l.pagex mod conf.tilew in
1371 let tiley = l.pagey mod conf.tileh in
1373 let col = l.pagex / conf.tilew in
1374 let row = l.pagey / conf.tileh in
1376 let rec rowloop row y0 dispy h =
1377 if h = 0
1378 then ()
1379 else (
1380 let dh = conf.tileh - y0 in
1381 let dh = min h dh in
1382 let rec colloop col x0 dispx w =
1383 if w = 0
1384 then ()
1385 else (
1386 let dw = conf.tilew - x0 in
1387 let dw = min w dw in
1389 f col row dispx dispy x0 y0 dw dh;
1390 colloop (col+1) 0 (dispx+dw) (w-dw)
1393 colloop col tilex l.pagedispx l.pagevw;
1394 rowloop (row+1) 0 (dispy+dh) (h-dh)
1397 if l.pagevw > 0 && l.pagevh > 0
1398 then rowloop row tiley l.pagedispy l.pagevh;
1401 let gettileopaque l col row =
1402 let key =
1403 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1405 try Some (Hashtbl.find state.tilemap key)
1406 with Not_found -> None
1409 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1410 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1411 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1414 let drawtiles l color =
1415 GlDraw.color color;
1416 let f col row x y tilex tiley w h =
1417 match gettileopaque l col row with
1418 | Some (opaque, _, t) ->
1419 let params = x, y, w, h, tilex, tiley in
1420 if conf.invert
1421 then (
1422 Gl.enable `blend;
1423 GlFunc.blend_func `zero `one_minus_src_color;
1425 drawtile params opaque;
1426 if conf.invert
1427 then Gl.disable `blend;
1428 if conf.debug
1429 then (
1430 let s = Printf.sprintf
1431 "%d[%d,%d] %f sec"
1432 l.pageno col row t
1434 let w = measurestr fstate.fontsize s in
1435 GlMisc.push_attrib [`current];
1436 GlDraw.color (0.0, 0.0, 0.0);
1437 GlDraw.rect
1438 (float (x-2), float (y-2))
1439 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1440 GlDraw.color (1.0, 1.0, 1.0);
1441 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1442 GlMisc.pop_attrib ();
1445 | _ ->
1446 let w =
1447 let lw = conf.winw - state.scrollw - x in
1448 min lw w
1449 and h =
1450 let lh = conf.winh - y in
1451 min lh h
1453 Gl.enable `texture_2d;
1454 begin match state.texid with
1455 | Some id ->
1456 GlTex.bind_texture `texture_2d id;
1457 let x0 = float x
1458 and y0 = float y
1459 and x1 = float (x+w)
1460 and y1 = float (y+h) in
1462 let tw = float w /. 64.0
1463 and th = float h /. 64.0 in
1464 let tx0 = float tilex /. 64.0
1465 and ty0 = float tiley /. 64.0 in
1466 let tx1 = tx0 +. tw
1467 and ty1 = ty0 +. th in
1468 GlDraw.begins `quads;
1469 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1470 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1471 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1472 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1473 GlDraw.ends ();
1475 Gl.disable `texture_2d;
1476 | None ->
1477 GlDraw.color (1.0, 1.0, 1.0);
1478 GlDraw.rect
1479 (float x, float y)
1480 (float (x+w), float (y+h));
1481 end;
1482 if w > 128 && h > fstate.fontsize + 10
1483 then (
1484 GlDraw.color (0.0, 0.0, 0.0);
1485 let c, r =
1486 if conf.verbose
1487 then (col*conf.tilew, row*conf.tileh)
1488 else col, row
1490 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1492 GlDraw.color color;
1494 itertiles l f
1497 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1499 let tilevisible1 l x y =
1500 let ax0 = l.pagex
1501 and ax1 = l.pagex + l.pagevw
1502 and ay0 = l.pagey
1503 and ay1 = l.pagey + l.pagevh in
1505 let bx0 = x
1506 and by0 = y in
1507 let bx1 = min (bx0 + conf.tilew) l.pagew
1508 and by1 = min (by0 + conf.tileh) l.pageh in
1510 let rx0 = max ax0 bx0
1511 and ry0 = max ay0 by0
1512 and rx1 = min ax1 bx1
1513 and ry1 = min ay1 by1 in
1515 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1516 nonemptyintersection
1519 let tilevisible layout n x y =
1520 let rec findpageinlayout m = function
1521 | l :: rest when l.pageno = n ->
1522 tilevisible1 l x y || (
1523 match conf.columns with
1524 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1525 | _ -> false
1527 | _ :: rest -> findpageinlayout 0 rest
1528 | [] -> false
1530 findpageinlayout 0 layout;
1533 let tileready l x y =
1534 tilevisible1 l x y &&
1535 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1538 let tilepage n p layout =
1539 let rec loop = function
1540 | l :: rest ->
1541 if l.pageno = n
1542 then
1543 let f col row _ _ _ _ _ _ =
1544 if state.currently = Idle
1545 then
1546 match gettileopaque l col row with
1547 | Some _ -> ()
1548 | None ->
1549 let x = col*conf.tilew
1550 and y = row*conf.tileh in
1551 let w =
1552 let w = l.pagew - x in
1553 min w conf.tilew
1555 let h =
1556 let h = l.pageh - y in
1557 min h conf.tileh
1559 wcmd "tile %s %d %d %d %d" p x y w h;
1560 state.currently <-
1561 Tiling (
1562 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1563 conf.tilew, conf.tileh
1566 itertiles l f;
1567 else
1568 loop rest
1570 | [] -> ()
1572 if nogeomcmds state.geomcmds
1573 then loop layout;
1576 let preloadlayout visiblepages =
1577 let presentation = conf.presentation in
1578 let interpagespace = conf.interpagespace in
1579 let maxy = state.maxy in
1580 conf.presentation <- false;
1581 conf.interpagespace <- 0;
1582 state.maxy <- calcheight ();
1583 let y =
1584 match visiblepages with
1585 | [] -> if state.y >= maxy then maxy else 0
1586 | l :: _ -> getpagey l.pageno + l.pagey
1588 let y = if y < conf.winh then 0 else y - conf.winh in
1589 let h = state.y - y + conf.winh*3 in
1590 let pages = layout y h in
1591 conf.presentation <- presentation;
1592 conf.interpagespace <- interpagespace;
1593 state.maxy <- maxy;
1594 pages;
1597 let load pages =
1598 let rec loop pages =
1599 if state.currently != Idle
1600 then ()
1601 else
1602 match pages with
1603 | l :: rest ->
1604 begin match getopaque l.pageno with
1605 | None ->
1606 wcmd "page %d %d" l.pageno l.pagedimno;
1607 state.currently <- Loading (l, state.gen);
1608 | Some opaque ->
1609 tilepage l.pageno opaque pages;
1610 loop rest
1611 end;
1612 | _ -> ()
1614 if nogeomcmds state.geomcmds
1615 then loop pages
1618 let preload pages =
1619 load pages;
1620 if conf.preload && state.currently = Idle
1621 then load (preloadlayout pages);
1624 let layoutready layout =
1625 let rec fold all ls =
1626 all && match ls with
1627 | l :: rest ->
1628 let seen = ref false in
1629 let allvisible = ref true in
1630 let foo col row _ _ _ _ _ _ =
1631 seen := true;
1632 allvisible := !allvisible &&
1633 begin match gettileopaque l col row with
1634 | Some _ -> true
1635 | None -> false
1638 itertiles l foo;
1639 fold (!seen && !allvisible) rest
1640 | [] -> true
1642 let alltilesvisible = fold true layout in
1643 alltilesvisible;
1646 let gotoy y =
1647 let y = bound y 0 state.maxy in
1648 let y, layout, proceed =
1649 match conf.maxwait with
1650 | Some time when state.ghyll == noghyll ->
1651 begin match state.throttle with
1652 | None ->
1653 let layout = layout y conf.winh in
1654 let ready = layoutready layout in
1655 if not ready
1656 then (
1657 load layout;
1658 state.throttle <- Some (layout, y, now ());
1660 else G.postRedisplay "gotoy showall (None)";
1661 y, layout, ready
1662 | Some (_, _, started) ->
1663 let dt = now () -. started in
1664 if dt > time
1665 then (
1666 state.throttle <- None;
1667 let layout = layout y conf.winh in
1668 load layout;
1669 G.postRedisplay "maxwait";
1670 y, layout, true
1672 else -1, [], false
1675 | _ ->
1676 let layout = layout y conf.winh in
1677 if true || layoutready layout
1678 then G.postRedisplay "gotoy ready";
1679 y, layout, true
1681 if proceed
1682 then (
1683 state.y <- y;
1684 state.layout <- layout;
1685 begin match state.mode with
1686 | LinkNav (Ltexact (pageno, linkno)) ->
1687 let rec loop = function
1688 | [] ->
1689 state.mode <- LinkNav (Ltgendir 0)
1690 | l :: _ when l.pageno = pageno ->
1691 begin match getopaque pageno with
1692 | None ->
1693 state.mode <- LinkNav (Ltgendir 0)
1694 | Some opaque ->
1695 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1696 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1697 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1698 then state.mode <- LinkNav (Ltgendir 0)
1700 | _ :: rest -> loop rest
1702 loop layout
1703 | _ -> ()
1704 end;
1705 begin match state.mode with
1706 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1707 if not (pagevisible layout pageno)
1708 then (
1709 match state.layout with
1710 | [] -> ()
1711 | l :: _ ->
1712 state.mode <- Birdseye (
1713 conf, leftx, l.pageno, hooverpageno, anchor
1716 | LinkNav (Ltgendir dir as lt) ->
1717 let linknav =
1718 let rec loop = function
1719 | [] -> lt
1720 | l :: rest ->
1721 match getopaque l.pageno with
1722 | None -> loop rest
1723 | Some opaque ->
1724 let link =
1725 let ld =
1726 if dir = 0
1727 then LDfirstvisible (l.pagex, l.pagey, dir)
1728 else (
1729 if dir > 0 then LDfirst else LDlast
1732 findlink opaque ld
1734 match link with
1735 | Lnotfound -> loop rest
1736 | Lfound n ->
1737 showlinktype (getlink opaque n);
1738 Ltexact (l.pageno, n)
1740 loop state.layout
1742 state.mode <- LinkNav linknav
1743 | _ -> ()
1744 end;
1745 preload layout;
1747 state.ghyll <- noghyll;
1748 if conf.updatecurs
1749 then (
1750 let mx, my = state.mpos in
1751 updateunder mx my;
1755 let conttiling pageno opaque =
1756 tilepage pageno opaque
1757 (if conf.preload then preloadlayout state.layout else state.layout)
1760 let gotoy_and_clear_text y =
1761 if not conf.verbose then state.text <- "";
1762 gotoy y;
1765 let getanchor () =
1766 match state.layout with
1767 | [] -> emptyanchor
1768 | l :: _ ->
1769 let coloff = l.pagecol * l.pageh in
1770 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1773 let getanchory (n, top) =
1774 let y, h = getpageyh n in
1775 y + (truncate (top *. float h));
1778 let gotoanchor anchor =
1779 gotoy (getanchory anchor);
1782 let addnav () =
1783 cbput state.hists.nav (getanchor ());
1786 let getnav dir =
1787 let anchor = cbgetc state.hists.nav dir in
1788 getanchory anchor;
1791 let gotoghyll y =
1792 let rec scroll f n a b =
1793 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1794 let snake f a b =
1795 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1796 if f < a
1797 then s (float f /. float a)
1798 else (
1799 if f > b
1800 then 1.0 -. s ((float (f-b) /. float (n-b)))
1801 else 1.0
1804 snake f a b
1805 and summa f n a b =
1806 (* courtesy:
1807 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1808 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1809 let iv1 = iv f in
1810 let ins = float a *. iv1
1811 and outs = float (n-b) *. iv1 in
1812 let ones = b - a in
1813 ins +. outs +. float ones
1815 let rec set (_N, _A, _B) y sy =
1816 let sum = summa 1.0 _N _A _B in
1817 let dy = float (y - sy) in
1818 state.ghyll <- (
1819 let rec gf n y1 o =
1820 if n >= _N
1821 then state.ghyll <- noghyll
1822 else
1823 let go n =
1824 let s = scroll n _N _A _B in
1825 let y1 = y1 +. ((s *. dy) /. sum) in
1826 gotoy_and_clear_text (truncate y1);
1827 state.ghyll <- gf (n+1) y1;
1829 match o with
1830 | None -> go n
1831 | Some y' -> set (_N/2, 0, 0) y' state.y
1833 gf 0 (float state.y)
1836 match conf.ghyllscroll with
1837 | None ->
1838 gotoy_and_clear_text y
1839 | Some nab ->
1840 if state.ghyll == noghyll
1841 then set nab y state.y
1842 else state.ghyll (Some y)
1845 let gotopage n top =
1846 let y, h = getpageyh n in
1847 let y = y + (truncate (top *. float h)) in
1848 gotoghyll y
1851 let gotopage1 n top =
1852 let y = getpagey n in
1853 let y = y + top in
1854 gotoghyll y
1857 let invalidate s f =
1858 state.layout <- [];
1859 state.pdims <- [];
1860 state.rects <- [];
1861 state.rects1 <- [];
1862 match state.geomcmds with
1863 | ps, [] when String.length ps = 0 ->
1864 f ();
1865 state.geomcmds <- s, [];
1867 | ps, [] ->
1868 state.geomcmds <- ps, [s, f];
1870 | ps, (s', _) :: rest when s' = s ->
1871 state.geomcmds <- ps, ((s, f) :: rest);
1873 | ps, cmds ->
1874 state.geomcmds <- ps, ((s, f) :: cmds);
1877 let opendoc path password =
1878 state.path <- path;
1879 state.password <- password;
1880 state.gen <- state.gen + 1;
1881 state.docinfo <- [];
1883 setaalevel conf.aalevel;
1884 Wsi.settitle ("llpp " ^ Filename.basename path);
1885 wcmd "open %s\000%s\000" path password;
1886 invalidate "reqlayout"
1887 (fun () ->
1888 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1891 let scalecolor c =
1892 let c = c *. conf.colorscale in
1893 (c, c, c);
1896 let scalecolor2 (r, g, b) =
1897 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1900 let docolumns = function
1901 | Csingle -> ()
1903 | Cmulti ((columns, coverA, coverB), _) ->
1904 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1905 let rec loop pageno pdimno pdim x y rowh pdims =
1906 let rec fixrow m = if m = pageno then () else
1907 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1908 if h < rowh
1909 then (
1910 let y = y + (rowh - h) / 2 in
1911 a.(m) <- (pdimno, x, y, pdim);
1913 fixrow (m+1)
1915 if pageno = state.pagecount
1916 then fixrow (((pageno - 1) / columns) * columns)
1917 else
1918 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1919 match pdims with
1920 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1921 pdimno+1, pdim, rest
1922 | _ ->
1923 pdimno, pdim, pdims
1925 let x, y, rowh' =
1926 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1927 then (
1928 (conf.winw - state.scrollw - w) / 2,
1929 y + rowh + conf.interpagespace, h
1931 else (
1932 if (pageno - coverA) mod columns = 0
1933 then 0, y + rowh + conf.interpagespace, h
1934 else x, y, max rowh h
1937 if pageno > 1 && (pageno - coverA) mod columns = 0
1938 then fixrow (pageno - columns);
1939 a.(pageno) <- (pdimno, x, y, pdim);
1940 let x = x + w + xoff*2 + conf.interpagespace in
1941 loop (pageno+1) pdimno pdim x y rowh' pdims
1943 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1944 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1946 | Csplit (c, _) ->
1947 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1948 let rec loop pageno pdimno pdim y pdims =
1949 if pageno = state.pagecount
1950 then ()
1951 else
1952 let pdimno, ((_, w, h, _) as pdim), pdims =
1953 match pdims with
1954 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1955 pdimno+1, pdim, rest
1956 | _ ->
1957 pdimno, pdim, pdims
1959 let cw = w / c in
1960 let rec loop1 n x y =
1961 if n = c then y else (
1962 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1963 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1966 let y = loop1 0 0 y in
1967 loop (pageno+1) pdimno pdim y pdims
1969 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1970 conf.columns <- Csplit (c, a);
1973 let represent () =
1974 docolumns conf.columns;
1975 state.maxy <- calcheight ();
1976 state.hscrollh <-
1977 if state.w <= conf.winw - state.scrollw
1978 then 0
1979 else state.scrollw
1981 match state.mode with
1982 | Birdseye (_, _, pageno, _, _) ->
1983 let y, h = getpageyh pageno in
1984 let top = (conf.winh - h) / 2 in
1985 gotoy (max 0 (y - top))
1986 | _ -> gotoanchor state.anchor
1989 let reshape w h =
1990 GlDraw.viewport 0 0 w h;
1991 let firsttime = state.geomcmds == firstgeomcmds in
1992 if not firsttime && nogeomcmds state.geomcmds
1993 then state.anchor <- getanchor ();
1995 conf.winw <- w;
1996 let w = truncate (float w *. conf.zoom) - state.scrollw in
1997 let w = max w 2 in
1998 conf.winh <- h;
1999 setfontsize fstate.fontsize;
2000 GlMat.mode `modelview;
2001 GlMat.load_identity ();
2003 GlMat.mode `projection;
2004 GlMat.load_identity ();
2005 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2006 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2007 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2009 let relx =
2010 if conf.zoom <= 1.0
2011 then 0.0
2012 else float state.x /. float state.w
2014 invalidate "geometry"
2015 (fun () ->
2016 state.w <- w;
2017 if not firsttime
2018 then state.x <- truncate (relx *. float w);
2019 let w =
2020 match conf.columns with
2021 | Csingle -> w
2022 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2023 | Csplit (c, _) -> w * c
2025 wcmd "geometry %d %d" w h);
2028 let enttext () =
2029 let len = String.length state.text in
2030 let drawstring s =
2031 let hscrollh =
2032 match state.mode with
2033 | Textentry _
2034 | View ->
2035 let h, _, _ = state.uioh#scrollpw in
2037 | _ -> 0
2039 let rect x w =
2040 GlDraw.rect
2041 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2042 (x+.w, float (conf.winh - hscrollh))
2045 let w = float (conf.winw - state.scrollw - 1) in
2046 if state.progress >= 0.0 && state.progress < 1.0
2047 then (
2048 GlDraw.color (0.3, 0.3, 0.3);
2049 let w1 = w *. state.progress in
2050 rect 0.0 w1;
2051 GlDraw.color (0.0, 0.0, 0.0);
2052 rect w1 (w-.w1)
2054 else (
2055 GlDraw.color (0.0, 0.0, 0.0);
2056 rect 0.0 w;
2059 GlDraw.color (1.0, 1.0, 1.0);
2060 drawstring fstate.fontsize
2061 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2063 let s =
2064 match state.mode with
2065 | Textentry ((prefix, text, _, _, _), _) ->
2066 let s =
2067 if len > 0
2068 then
2069 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2070 else
2071 Printf.sprintf "%s%s_" prefix text
2075 | _ -> state.text
2077 let s =
2078 if state.newerrmsgs
2079 then (
2080 if not (istextentry state.mode)
2081 then
2082 let s1 = "(press 'e' to review error messasges)" in
2083 if String.length s > 0 then s ^ " " ^ s1 else s1
2084 else s
2086 else s
2088 if String.length s > 0
2089 then drawstring s
2092 let gctiles () =
2093 let len = Queue.length state.tilelru in
2094 let rec loop qpos =
2095 if state.memused <= conf.memlimit
2096 then ()
2097 else (
2098 if qpos < len
2099 then
2100 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2101 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2102 let (_, pw, ph, _) = getpagedim n in
2104 gen = state.gen
2105 && colorspace = conf.colorspace
2106 && angle = conf.angle
2107 && pagew = pw
2108 && pageh = ph
2109 && (
2110 let layout =
2111 match state.throttle with
2112 | None ->
2113 if conf.preload
2114 then preloadlayout state.layout
2115 else state.layout
2116 | Some (layout, _, _) ->
2117 layout
2119 let x = col*conf.tilew
2120 and y = row*conf.tileh in
2121 tilevisible layout n x y
2123 then Queue.push lruitem state.tilelru
2124 else (
2125 wcmd "freetile %s" p;
2126 state.memused <- state.memused - s;
2127 state.uioh#infochanged Memused;
2128 Hashtbl.remove state.tilemap k;
2130 loop (qpos+1)
2133 loop 0
2136 let flushtiles () =
2137 Queue.iter (fun (k, p, s) ->
2138 wcmd "freetile %s" p;
2139 state.memused <- state.memused - s;
2140 state.uioh#infochanged Memused;
2141 Hashtbl.remove state.tilemap k;
2142 ) state.tilelru;
2143 Queue.clear state.tilelru;
2144 load state.layout;
2147 let logcurrently = function
2148 | Idle -> dolog "Idle"
2149 | Loading (l, gen) ->
2150 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2151 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2152 dolog
2153 "Tiling %d[%d,%d] page=%s cs=%s angle"
2154 l.pageno col row pageopaque
2155 (colorspace_to_string colorspace)
2157 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2158 angle gen conf.angle state.gen
2159 tilew tileh
2160 conf.tilew conf.tileh
2162 | Outlining _ ->
2163 dolog "outlining"
2166 let act cmds =
2167 (* dolog "%S" cmds; *)
2168 let op, args =
2169 let spacepos =
2170 try String.index cmds ' '
2171 with Not_found -> -1
2173 if spacepos = -1
2174 then cmds, ""
2175 else
2176 let l = String.length cmds in
2177 let op = String.sub cmds 0 spacepos in
2178 op, begin
2179 if l - spacepos < 2 then ""
2180 else String.sub cmds (spacepos+1) (l-spacepos-1)
2183 match op with
2184 | "clear" ->
2185 state.uioh#infochanged Pdim;
2186 state.pdims <- [];
2188 | "clearrects" ->
2189 state.rects <- state.rects1;
2190 G.postRedisplay "clearrects";
2192 | "continue" ->
2193 let n =
2194 try Scanf.sscanf args "%u" (fun n -> n)
2195 with exn ->
2196 dolog "error processing 'continue' %S: %s"
2197 cmds (Printexc.to_string exn);
2198 exit 1;
2200 state.pagecount <- n;
2201 begin match state.currently with
2202 | Outlining l ->
2203 state.currently <- Idle;
2204 state.outlines <- Array.of_list (List.rev l)
2205 | _ -> ()
2206 end;
2208 let cur, cmds = state.geomcmds in
2209 if String.length cur = 0
2210 then failwith "umpossible";
2212 begin match List.rev cmds with
2213 | [] ->
2214 state.geomcmds <- "", [];
2215 represent ();
2216 | (s, f) :: rest ->
2217 f ();
2218 state.geomcmds <- s, List.rev rest;
2219 end;
2220 if conf.maxwait = None
2221 then G.postRedisplay "continue";
2223 | "title" ->
2224 Wsi.settitle args
2226 | "msg" ->
2227 showtext ' ' args
2229 | "vmsg" ->
2230 if conf.verbose
2231 then showtext ' ' args
2233 | "progress" ->
2234 let progress, text =
2236 Scanf.sscanf args "%f %n"
2237 (fun f pos ->
2238 f, String.sub args pos (String.length args - pos))
2239 with exn ->
2240 dolog "error processing 'progress' %S: %s"
2241 cmds (Printexc.to_string exn);
2242 exit 1;
2244 state.text <- text;
2245 state.progress <- progress;
2246 G.postRedisplay "progress"
2248 | "firstmatch" ->
2249 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2251 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2252 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2253 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2254 with exn ->
2255 dolog "error processing 'firstmatch' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 let y = (getpagey pageno) + truncate y0 in
2260 addnav ();
2261 gotoy y;
2262 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2264 | "match" ->
2265 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2267 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2268 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2269 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2270 with exn ->
2271 dolog "error processing 'match' %S: %s"
2272 cmds (Printexc.to_string exn);
2273 exit 1;
2275 state.rects1 <-
2276 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2278 | "page" ->
2279 let pageopaque, t =
2281 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2282 with exn ->
2283 dolog "error processing 'page' %S: %s"
2284 cmds (Printexc.to_string exn);
2285 exit 1;
2287 begin match state.currently with
2288 | Loading (l, gen) ->
2289 vlog "page %d took %f sec" l.pageno t;
2290 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2291 begin match state.throttle with
2292 | None ->
2293 let preloadedpages =
2294 if conf.preload
2295 then preloadlayout state.layout
2296 else state.layout
2298 let evict () =
2299 let module IntSet =
2300 Set.Make (struct type t = int let compare = (-) end) in
2301 let set =
2302 List.fold_left (fun s l -> IntSet.add l.pageno s)
2303 IntSet.empty preloadedpages
2305 let evictedpages =
2306 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2307 if not (IntSet.mem pageno set)
2308 then (
2309 wcmd "freepage %s" opaque;
2310 key :: accu
2312 else accu
2313 ) state.pagemap []
2315 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2317 evict ();
2318 state.currently <- Idle;
2319 if gen = state.gen
2320 then (
2321 tilepage l.pageno pageopaque state.layout;
2322 load state.layout;
2323 load preloadedpages;
2324 if pagevisible state.layout l.pageno
2325 && layoutready state.layout
2326 then G.postRedisplay "page";
2329 | Some (layout, _, _) ->
2330 state.currently <- Idle;
2331 tilepage l.pageno pageopaque layout;
2332 load state.layout
2333 end;
2335 | _ ->
2336 dolog "Inconsistent loading state";
2337 logcurrently state.currently;
2338 exit 1
2341 | "tile" ->
2342 let (x, y, opaque, size, t) =
2344 Scanf.sscanf args "%u %u %s %u %f"
2345 (fun x y p size t -> (x, y, p, size, t))
2346 with exn ->
2347 dolog "error processing 'tile' %S: %s"
2348 cmds (Printexc.to_string exn);
2349 exit 1;
2351 begin match state.currently with
2352 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2353 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2355 if tilew != conf.tilew || tileh != conf.tileh
2356 then (
2357 wcmd "freetile %s" opaque;
2358 state.currently <- Idle;
2359 load state.layout;
2361 else (
2362 puttileopaque l col row gen cs angle opaque size t;
2363 state.memused <- state.memused + size;
2364 state.uioh#infochanged Memused;
2365 gctiles ();
2366 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2367 opaque, size) state.tilelru;
2369 let layout =
2370 match state.throttle with
2371 | None -> state.layout
2372 | Some (layout, _, _) -> layout
2375 state.currently <- Idle;
2376 if gen = state.gen
2377 && conf.colorspace = cs
2378 && conf.angle = angle
2379 && tilevisible layout l.pageno x y
2380 then conttiling l.pageno pageopaque;
2382 begin match state.throttle with
2383 | None ->
2384 preload state.layout;
2385 if gen = state.gen
2386 && conf.colorspace = cs
2387 && conf.angle = angle
2388 && tilevisible state.layout l.pageno x y
2389 then G.postRedisplay "tile nothrottle";
2391 | Some (layout, y, _) ->
2392 let ready = layoutready layout in
2393 if ready
2394 then (
2395 state.y <- y;
2396 state.layout <- layout;
2397 state.throttle <- None;
2398 G.postRedisplay "throttle";
2400 else load layout;
2401 end;
2404 | _ ->
2405 dolog "Inconsistent tiling state";
2406 logcurrently state.currently;
2407 exit 1
2410 | "pdim" ->
2411 let pdim =
2413 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2414 with exn ->
2415 dolog "error processing 'pdim' %S: %s"
2416 cmds (Printexc.to_string exn);
2417 exit 1;
2419 state.uioh#infochanged Pdim;
2420 state.pdims <- pdim :: state.pdims
2422 | "o" ->
2423 let (l, n, t, h, pos) =
2425 Scanf.sscanf args "%u %u %d %u %n"
2426 (fun l n t h pos -> l, n, t, h, pos)
2427 with exn ->
2428 dolog "error processing 'o' %S: %s"
2429 cmds (Printexc.to_string exn);
2430 exit 1;
2432 let s = String.sub args pos (String.length args - pos) in
2433 let outline = (s, l, (n, float t /. float h)) in
2434 begin match state.currently with
2435 | Outlining outlines ->
2436 state.currently <- Outlining (outline :: outlines)
2437 | Idle ->
2438 state.currently <- Outlining [outline]
2439 | currently ->
2440 dolog "invalid outlining state";
2441 logcurrently currently
2444 | "info" ->
2445 state.docinfo <- (1, args) :: state.docinfo
2447 | "infoend" ->
2448 state.uioh#infochanged Docinfo;
2449 state.docinfo <- List.rev state.docinfo
2451 | _ ->
2452 dolog "unknown cmd `%S'" cmds
2455 let onhist cb =
2456 let rc = cb.rc in
2457 let action = function
2458 | HCprev -> cbget cb ~-1
2459 | HCnext -> cbget cb 1
2460 | HCfirst -> cbget cb ~-(cb.rc)
2461 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2462 and cancel () = cb.rc <- rc
2463 in (action, cancel)
2466 let search pattern forward =
2467 if String.length pattern > 0
2468 then
2469 let pn, py =
2470 match state.layout with
2471 | [] -> 0, 0
2472 | l :: _ ->
2473 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2475 wcmd "search %d %d %d %d,%s\000"
2476 (btod conf.icase) pn py (btod forward) pattern;
2479 let intentry text key =
2480 let c =
2481 if key >= 32 && key < 127
2482 then Char.chr key
2483 else '\000'
2485 match c with
2486 | '0' .. '9' ->
2487 let text = addchar text c in
2488 TEcont text
2490 | _ ->
2491 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2492 TEcont text
2495 let linknentry text key =
2496 let c =
2497 if key >= 32 && key < 127
2498 then Char.chr key
2499 else '\000'
2501 match c with
2502 | 'a' .. 'z' ->
2503 let text = addchar text c in
2504 TEcont text
2506 | _ ->
2507 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2508 TEcont text
2511 let linkndone f s =
2512 if String.length s > 0
2513 then (
2514 let n =
2515 let l = String.length s in
2516 let rec loop pos n = if pos = l then n else
2517 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2518 loop (pos+1) (n*26 + m)
2519 in loop 0 0
2521 let rec loop n = function
2522 | [] -> ()
2523 | l :: rest ->
2524 match getopaque l.pageno with
2525 | None -> loop n rest
2526 | Some opaque ->
2527 let m = getlinkcount opaque in
2528 if n < m
2529 then (
2530 let under = getlink opaque n in
2531 f under
2533 else loop (n-m) rest
2535 loop n state.layout;
2539 let textentry text key =
2540 if key land 0xff00 = 0xff00
2541 then TEcont text
2542 else TEcont (text ^ Wsi.toutf8 key)
2545 let reqlayout angle proportional =
2546 match state.throttle with
2547 | None ->
2548 if nogeomcmds state.geomcmds
2549 then state.anchor <- getanchor ();
2550 conf.angle <- angle mod 360;
2551 if conf.angle != 0
2552 then (
2553 match state.mode with
2554 | LinkNav _ -> state.mode <- View
2555 | _ -> ()
2557 conf.proportional <- proportional;
2558 invalidate "reqlayout"
2559 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2560 | _ -> ()
2563 let settrim trimmargins trimfuzz =
2564 if nogeomcmds state.geomcmds
2565 then state.anchor <- getanchor ();
2566 conf.trimmargins <- trimmargins;
2567 conf.trimfuzz <- trimfuzz;
2568 let x0, y0, x1, y1 = trimfuzz in
2569 invalidate "settrim"
2570 (fun () ->
2571 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2572 Hashtbl.iter (fun _ opaque ->
2573 wcmd "freepage %s" opaque;
2574 ) state.pagemap;
2575 Hashtbl.clear state.pagemap;
2578 let setzoom zoom =
2579 match state.throttle with
2580 | None ->
2581 let zoom = max 0.01 zoom in
2582 if zoom <> conf.zoom
2583 then (
2584 state.prevzoom <- conf.zoom;
2585 conf.zoom <- zoom;
2586 reshape conf.winw conf.winh;
2587 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2590 | Some (layout, y, started) ->
2591 let time =
2592 match conf.maxwait with
2593 | None -> 0.0
2594 | Some t -> t
2596 let dt = now () -. started in
2597 if dt > time
2598 then (
2599 state.y <- y;
2600 load layout;
2604 let setcolumns mode columns coverA coverB =
2605 if columns < 0
2606 then (
2607 if isbirdseye mode
2608 then showtext '!' "split mode doesn't work in bird's eye"
2609 else (
2610 conf.columns <- Csplit (-columns, [||]);
2611 state.x <- 0;
2612 conf.zoom <- 1.0;
2615 else (
2616 if columns < 2
2617 then (
2618 conf.columns <- Csingle;
2619 state.x <- 0;
2620 setzoom 1.0;
2622 else (
2623 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2624 conf.zoom <- 1.0;
2627 reshape conf.winw conf.winh;
2630 let enterbirdseye () =
2631 let zoom = float conf.thumbw /. float conf.winw in
2632 let birdseyepageno =
2633 let cy = conf.winh / 2 in
2634 let fold = function
2635 | [] -> 0
2636 | l :: rest ->
2637 let rec fold best = function
2638 | [] -> best.pageno
2639 | l :: rest ->
2640 let d = cy - (l.pagedispy + l.pagevh/2)
2641 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2642 if abs d < abs dbest
2643 then fold l rest
2644 else best.pageno
2645 in fold l rest
2647 fold state.layout
2649 state.mode <- Birdseye (
2650 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2652 conf.zoom <- zoom;
2653 conf.presentation <- false;
2654 conf.interpagespace <- 10;
2655 conf.hlinks <- false;
2656 state.x <- 0;
2657 state.mstate <- Mnone;
2658 conf.maxwait <- None;
2659 conf.columns <- (
2660 match conf.beyecolumns with
2661 | Some c ->
2662 conf.zoom <- 1.0;
2663 Cmulti ((c, 0, 0), [||])
2664 | None -> Csingle
2666 Wsi.setcursor Wsi.CURSOR_INHERIT;
2667 if conf.verbose
2668 then
2669 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2670 (100.0*.zoom)
2671 else
2672 state.text <- ""
2674 reshape conf.winw conf.winh;
2677 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2678 state.mode <- View;
2679 conf.zoom <- c.zoom;
2680 conf.presentation <- c.presentation;
2681 conf.interpagespace <- c.interpagespace;
2682 conf.maxwait <- c.maxwait;
2683 conf.hlinks <- c.hlinks;
2684 conf.beyecolumns <- (
2685 match conf.columns with
2686 | Cmulti ((c, _, _), _) -> Some c
2687 | Csingle -> None
2688 | Csplit _ -> failwith "leaving bird's eye split mode"
2690 conf.columns <- (
2691 match c.columns with
2692 | Cmulti (c, _) -> Cmulti (c, [||])
2693 | Csingle -> Csingle
2694 | Csplit (c, _) -> Csplit (c, [||])
2696 state.x <- leftx;
2697 if conf.verbose
2698 then
2699 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2700 (100.0*.conf.zoom)
2702 reshape conf.winw conf.winh;
2703 state.anchor <- if goback then anchor else (pageno, 0.0);
2706 let togglebirdseye () =
2707 match state.mode with
2708 | Birdseye vals -> leavebirdseye vals true
2709 | View -> enterbirdseye ()
2710 | _ -> ()
2713 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2714 let pageno = max 0 (pageno - incr) in
2715 let rec loop = function
2716 | [] -> gotopage1 pageno 0
2717 | l :: _ when l.pageno = pageno ->
2718 if l.pagedispy >= 0 && l.pagey = 0
2719 then G.postRedisplay "upbirdseye"
2720 else gotopage1 pageno 0
2721 | _ :: rest -> loop rest
2723 loop state.layout;
2724 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2727 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2728 let pageno = min (state.pagecount - 1) (pageno + incr) in
2729 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2730 let rec loop = function
2731 | [] ->
2732 let y, h = getpageyh pageno in
2733 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2734 gotoy (clamp dy)
2735 | l :: _ when l.pageno = pageno ->
2736 if l.pagevh != l.pageh
2737 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2738 else G.postRedisplay "downbirdseye"
2739 | _ :: rest -> loop rest
2741 loop state.layout
2744 let optentry mode _ key =
2745 let btos b = if b then "on" else "off" in
2746 if key >= 32 && key < 127
2747 then
2748 let c = Char.chr key in
2749 match c with
2750 | 's' ->
2751 let ondone s =
2752 try conf.scrollstep <- int_of_string s with exc ->
2753 state.text <- Printf.sprintf "bad integer `%s': %s"
2754 s (Printexc.to_string exc)
2756 TEswitch ("scroll step: ", "", None, intentry, ondone)
2758 | 'A' ->
2759 let ondone s =
2761 conf.autoscrollstep <- int_of_string s;
2762 if state.autoscroll <> None
2763 then state.autoscroll <- Some conf.autoscrollstep
2764 with exc ->
2765 state.text <- Printf.sprintf "bad integer `%s': %s"
2766 s (Printexc.to_string exc)
2768 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2770 | 'C' ->
2771 let mode = state.mode in
2772 let ondone s =
2774 let n, a, b = multicolumns_of_string s in
2775 setcolumns mode n a b;
2776 with exc ->
2777 state.text <- Printf.sprintf "bad columns `%s': %s"
2778 s (Printexc.to_string exc)
2780 TEswitch ("columns: ", "", None, textentry, ondone)
2782 | 'Z' ->
2783 let ondone s =
2785 let zoom = float (int_of_string s) /. 100.0 in
2786 setzoom zoom
2787 with exc ->
2788 state.text <- Printf.sprintf "bad integer `%s': %s"
2789 s (Printexc.to_string exc)
2791 TEswitch ("zoom: ", "", None, intentry, ondone)
2793 | 't' ->
2794 let ondone s =
2796 conf.thumbw <- bound (int_of_string s) 2 4096;
2797 state.text <-
2798 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2799 begin match mode with
2800 | Birdseye beye ->
2801 leavebirdseye beye false;
2802 enterbirdseye ();
2803 | _ -> ();
2805 with exc ->
2806 state.text <- Printf.sprintf "bad integer `%s': %s"
2807 s (Printexc.to_string exc)
2809 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2811 | 'R' ->
2812 let ondone s =
2813 match try
2814 Some (int_of_string s)
2815 with exc ->
2816 state.text <- Printf.sprintf "bad integer `%s': %s"
2817 s (Printexc.to_string exc);
2818 None
2819 with
2820 | Some angle -> reqlayout angle conf.proportional
2821 | None -> ()
2823 TEswitch ("rotation: ", "", None, intentry, ondone)
2825 | 'i' ->
2826 conf.icase <- not conf.icase;
2827 TEdone ("case insensitive search " ^ (btos conf.icase))
2829 | 'p' ->
2830 conf.preload <- not conf.preload;
2831 gotoy state.y;
2832 TEdone ("preload " ^ (btos conf.preload))
2834 | 'v' ->
2835 conf.verbose <- not conf.verbose;
2836 TEdone ("verbose " ^ (btos conf.verbose))
2838 | 'd' ->
2839 conf.debug <- not conf.debug;
2840 TEdone ("debug " ^ (btos conf.debug))
2842 | 'h' ->
2843 conf.maxhfit <- not conf.maxhfit;
2844 state.maxy <- calcheight ();
2845 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2847 | 'c' ->
2848 conf.crophack <- not conf.crophack;
2849 TEdone ("crophack " ^ btos conf.crophack)
2851 | 'a' ->
2852 let s =
2853 match conf.maxwait with
2854 | None ->
2855 conf.maxwait <- Some infinity;
2856 "always wait for page to complete"
2857 | Some _ ->
2858 conf.maxwait <- None;
2859 "show placeholder if page is not ready"
2861 TEdone s
2863 | 'f' ->
2864 conf.underinfo <- not conf.underinfo;
2865 TEdone ("underinfo " ^ btos conf.underinfo)
2867 | 'P' ->
2868 conf.savebmarks <- not conf.savebmarks;
2869 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2871 | 'S' ->
2872 let ondone s =
2874 let pageno, py =
2875 match state.layout with
2876 | [] -> 0, 0
2877 | l :: _ ->
2878 l.pageno, l.pagey
2880 conf.interpagespace <- int_of_string s;
2881 docolumns conf.columns;
2882 state.maxy <- calcheight ();
2883 let y = getpagey pageno in
2884 gotoy (y + py)
2885 with exc ->
2886 state.text <- Printf.sprintf "bad integer `%s': %s"
2887 s (Printexc.to_string exc)
2889 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2891 | 'l' ->
2892 reqlayout conf.angle (not conf.proportional);
2893 TEdone ("proportional display " ^ btos conf.proportional)
2895 | 'T' ->
2896 settrim (not conf.trimmargins) conf.trimfuzz;
2897 TEdone ("trim margins " ^ btos conf.trimmargins)
2899 | 'I' ->
2900 conf.invert <- not conf.invert;
2901 TEdone ("invert colors " ^ btos conf.invert)
2903 | 'x' ->
2904 let ondone s =
2905 cbput state.hists.sel s;
2906 conf.selcmd <- s;
2908 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2909 textentry, ondone)
2911 | 'F' ->
2912 conf.fullsplit <- not conf.fullsplit;
2913 gotoy state.y;
2914 TEdone ("full split " ^ btos conf.fullsplit)
2916 | _ ->
2917 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2918 TEstop
2919 else
2920 TEcont state.text
2923 class type lvsource = object
2924 method getitemcount : int
2925 method getitem : int -> (string * int)
2926 method hasaction : int -> bool
2927 method exit :
2928 uioh:uioh ->
2929 cancel:bool ->
2930 active:int ->
2931 first:int ->
2932 pan:int ->
2933 qsearch:string ->
2934 uioh option
2935 method getactive : int
2936 method getfirst : int
2937 method getqsearch : string
2938 method setqsearch : string -> unit
2939 method getpan : int
2940 end;;
2942 class virtual lvsourcebase = object
2943 val mutable m_active = 0
2944 val mutable m_first = 0
2945 val mutable m_qsearch = ""
2946 val mutable m_pan = 0
2947 method getactive = m_active
2948 method getfirst = m_first
2949 method getqsearch = m_qsearch
2950 method getpan = m_pan
2951 method setqsearch s = m_qsearch <- s
2952 end;;
2954 let withoutlastutf8 s =
2955 let len = String.length s in
2956 if len = 0
2957 then s
2958 else
2959 let rec find pos =
2960 if pos = 0
2961 then pos
2962 else
2963 let b = Char.code s.[pos] in
2964 if b land 0b110000 = 0b11000000
2965 then find (pos-1)
2966 else pos-1
2968 let first =
2969 if Char.code s.[len-1] land 0x80 = 0
2970 then len-1
2971 else find (len-1)
2973 String.sub s 0 first;
2976 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2977 let enttext te =
2978 state.mode <- Textentry (te, onleave);
2979 state.text <- "";
2980 enttext ();
2981 G.postRedisplay "textentrykeyboard enttext";
2983 let histaction cmd =
2984 match opthist with
2985 | None -> ()
2986 | Some (action, _) ->
2987 state.mode <- Textentry (
2988 (c, action cmd, opthist, onkey, ondone), onleave
2990 G.postRedisplay "textentry histaction"
2992 match key with
2993 | 0xff08 -> (* backspace *)
2994 let s = withoutlastutf8 text in
2995 let len = String.length s in
2996 if len = 0
2997 then (
2998 onleave Cancel;
2999 G.postRedisplay "textentrykeyboard after cancel";
3001 else (
3002 enttext (c, s, opthist, onkey, ondone)
3005 | 0xff0d ->
3006 ondone text;
3007 onleave Confirm;
3008 G.postRedisplay "textentrykeyboard after confirm"
3010 | 0xff52 -> histaction HCprev
3011 | 0xff54 -> histaction HCnext
3012 | 0xff50 -> histaction HCfirst
3013 | 0xff57 -> histaction HClast
3015 | 0xff1b -> (* escape*)
3016 if String.length text = 0
3017 then (
3018 begin match opthist with
3019 | None -> ()
3020 | Some (_, onhistcancel) -> onhistcancel ()
3021 end;
3022 onleave Cancel;
3023 state.text <- "";
3024 G.postRedisplay "textentrykeyboard after cancel2"
3026 else (
3027 enttext (c, "", opthist, onkey, ondone)
3030 | 0xff9f | 0xffff -> () (* delete *)
3032 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3033 begin match onkey text key with
3034 | TEdone text ->
3035 ondone text;
3036 onleave Confirm;
3037 G.postRedisplay "textentrykeyboard after confirm2";
3039 | TEcont text ->
3040 enttext (c, text, opthist, onkey, ondone);
3042 | TEstop ->
3043 onleave Cancel;
3044 G.postRedisplay "textentrykeyboard after cancel3"
3046 | TEswitch te ->
3047 state.mode <- Textentry (te, onleave);
3048 G.postRedisplay "textentrykeyboard switch";
3049 end;
3051 | _ ->
3052 vlog "unhandled key %s" (Wsi.keyname key)
3055 let firstof first active =
3056 if first > active || abs (first - active) > fstate.maxrows - 1
3057 then max 0 (active - (fstate.maxrows/2))
3058 else first
3061 let calcfirst first active =
3062 if active > first
3063 then
3064 let rows = active - first in
3065 if rows > fstate.maxrows then active - fstate.maxrows else first
3066 else active
3069 let scrollph y maxy =
3070 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3071 let sh = float conf.winh /. sh in
3072 let sh = max sh (float conf.scrollh) in
3074 let percent =
3075 if y = state.maxy
3076 then 1.0
3077 else float y /. float maxy
3079 let position = (float conf.winh -. sh) *. percent in
3081 let position =
3082 if position +. sh > float conf.winh
3083 then float conf.winh -. sh
3084 else position
3086 position, sh;
3089 let coe s = (s :> uioh);;
3091 class listview ~(source:lvsource) ~trusted ~modehash =
3092 object (self)
3093 val m_pan = source#getpan
3094 val m_first = source#getfirst
3095 val m_active = source#getactive
3096 val m_qsearch = source#getqsearch
3097 val m_prev_uioh = state.uioh
3099 method private elemunder y =
3100 let n = y / (fstate.fontsize+1) in
3101 if m_first + n < source#getitemcount
3102 then (
3103 if source#hasaction (m_first + n)
3104 then Some (m_first + n)
3105 else None
3107 else None
3109 method display =
3110 Gl.enable `blend;
3111 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3112 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3113 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3114 GlDraw.color (1., 1., 1.);
3115 Gl.enable `texture_2d;
3116 let fs = fstate.fontsize in
3117 let nfs = fs + 1 in
3118 let ww = fstate.wwidth in
3119 let tabw = 30.0*.ww in
3120 let itemcount = source#getitemcount in
3121 let rec loop row =
3122 if (row - m_first) * nfs > conf.winh
3123 then ()
3124 else (
3125 if row >= 0 && row < itemcount
3126 then (
3127 let (s, level) = source#getitem row in
3128 let y = (row - m_first) * nfs in
3129 let x = 5.0 +. float (level + m_pan) *. ww in
3130 if row = m_active
3131 then (
3132 Gl.disable `texture_2d;
3133 GlDraw.polygon_mode `both `line;
3134 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3135 GlDraw.rect (1., float (y + 1))
3136 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3137 GlDraw.polygon_mode `both `fill;
3138 GlDraw.color (1., 1., 1.);
3139 Gl.enable `texture_2d;
3142 let drawtabularstring s =
3143 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3144 if trusted
3145 then
3146 let tabpos = try String.index s '\t' with Not_found -> -1 in
3147 if tabpos > 0
3148 then
3149 let len = String.length s - tabpos - 1 in
3150 let s1 = String.sub s 0 tabpos
3151 and s2 = String.sub s (tabpos + 1) len in
3152 let nx = drawstr x s1 in
3153 let sw = nx -. x in
3154 let x = x +. (max tabw sw) in
3155 drawstr x s2
3156 else
3157 drawstr x s
3158 else
3159 drawstr x s
3161 let _ = drawtabularstring s in
3162 loop (row+1)
3166 loop m_first;
3167 Gl.disable `blend;
3168 Gl.disable `texture_2d;
3170 method updownlevel incr =
3171 let len = source#getitemcount in
3172 let curlevel =
3173 if m_active >= 0 && m_active < len
3174 then snd (source#getitem m_active)
3175 else -1
3177 let rec flow i =
3178 if i = len then i-1 else if i = -1 then 0 else
3179 let _, l = source#getitem i in
3180 if l != curlevel then i else flow (i+incr)
3182 let active = flow m_active in
3183 let first = calcfirst m_first active in
3184 G.postRedisplay "outline updownlevel";
3185 {< m_active = active; m_first = first >}
3187 method private key1 key mask =
3188 let set1 active first qsearch =
3189 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3191 let search active pattern incr =
3192 let dosearch re =
3193 let rec loop n =
3194 if n >= 0 && n < source#getitemcount
3195 then (
3196 let s, _ = source#getitem n in
3198 (try ignore (Str.search_forward re s 0); true
3199 with Not_found -> false)
3200 then Some n
3201 else loop (n + incr)
3203 else None
3205 loop active
3208 let re = Str.regexp_case_fold pattern in
3209 dosearch re
3210 with Failure s ->
3211 state.text <- s;
3212 None
3214 let itemcount = source#getitemcount in
3215 let find start incr =
3216 let rec find i =
3217 if i = -1 || i = itemcount
3218 then -1
3219 else (
3220 if source#hasaction i
3221 then i
3222 else find (i + incr)
3225 find start
3227 let set active first =
3228 let first = bound first 0 (itemcount - fstate.maxrows) in
3229 state.text <- "";
3230 coe {< m_active = active; m_first = first >}
3232 let navigate incr =
3233 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3234 let active, first =
3235 let incr1 = if incr > 0 then 1 else -1 in
3236 if isvisible m_first m_active
3237 then
3238 let next =
3239 let next = m_active + incr in
3240 let next =
3241 if next < 0 || next >= itemcount
3242 then -1
3243 else find next incr1
3245 if next = -1 || abs (m_active - next) > fstate.maxrows
3246 then -1
3247 else next
3249 if next = -1
3250 then
3251 let first = m_first + incr in
3252 let first = bound first 0 (itemcount - 1) in
3253 let next =
3254 let next = m_active + incr in
3255 let next = bound next 0 (itemcount - 1) in
3256 find next ~-incr1
3258 let active = if next = -1 then m_active else next in
3259 active, first
3260 else
3261 let first = min next m_first in
3262 let first =
3263 if abs (next - first) > fstate.maxrows
3264 then first + incr
3265 else first
3267 next, first
3268 else
3269 let first = m_first + incr in
3270 let first = bound first 0 (itemcount - 1) in
3271 let active =
3272 let next = m_active + incr in
3273 let next = bound next 0 (itemcount - 1) in
3274 let next = find next incr1 in
3275 let active =
3276 if next = -1 || abs (m_active - first) > fstate.maxrows
3277 then (
3278 let active = if m_active = -1 then next else m_active in
3279 active
3281 else next
3283 if isvisible first active
3284 then active
3285 else -1
3287 active, first
3289 G.postRedisplay "listview navigate";
3290 set active first;
3292 match key with
3293 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3294 let incr = if key = 0x72 then -1 else 1 in
3295 let active, first =
3296 match search (m_active + incr) m_qsearch incr with
3297 | None ->
3298 state.text <- m_qsearch ^ " [not found]";
3299 m_active, m_first
3300 | Some active ->
3301 state.text <- m_qsearch;
3302 active, firstof m_first active
3304 G.postRedisplay "listview ctrl-r/s";
3305 set1 active first m_qsearch;
3307 | 0xff08 -> (* backspace *)
3308 if String.length m_qsearch = 0
3309 then coe self
3310 else (
3311 let qsearch = withoutlastutf8 m_qsearch in
3312 let len = String.length qsearch in
3313 if len = 0
3314 then (
3315 state.text <- "";
3316 G.postRedisplay "listview empty qsearch";
3317 set1 m_active m_first "";
3319 else
3320 let active, first =
3321 match search m_active qsearch ~-1 with
3322 | None ->
3323 state.text <- qsearch ^ " [not found]";
3324 m_active, m_first
3325 | Some active ->
3326 state.text <- qsearch;
3327 active, firstof m_first active
3329 G.postRedisplay "listview backspace qsearch";
3330 set1 active first qsearch
3333 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3334 let pattern = m_qsearch ^ Wsi.toutf8 key in
3335 let active, first =
3336 match search m_active pattern 1 with
3337 | None ->
3338 state.text <- pattern ^ " [not found]";
3339 m_active, m_first
3340 | Some active ->
3341 state.text <- pattern;
3342 active, firstof m_first active
3344 G.postRedisplay "listview qsearch add";
3345 set1 active first pattern;
3347 | 0xff1b -> (* escape *)
3348 state.text <- "";
3349 if String.length m_qsearch = 0
3350 then (
3351 G.postRedisplay "list view escape";
3352 begin
3353 match
3354 source#exit (coe self) true m_active m_first m_pan m_qsearch
3355 with
3356 | None -> m_prev_uioh
3357 | Some uioh -> uioh
3360 else (
3361 G.postRedisplay "list view kill qsearch";
3362 source#setqsearch "";
3363 coe {< m_qsearch = "" >}
3366 | 0xff0d -> (* return *)
3367 state.text <- "";
3368 let self = {< m_qsearch = "" >} in
3369 source#setqsearch "";
3370 let opt =
3371 G.postRedisplay "listview enter";
3372 if m_active >= 0 && m_active < source#getitemcount
3373 then (
3374 source#exit (coe self) false m_active m_first m_pan "";
3376 else (
3377 source#exit (coe self) true m_active m_first m_pan "";
3380 begin match opt with
3381 | None -> m_prev_uioh
3382 | Some uioh -> uioh
3385 | 0xff9f | 0xffff -> (* delete *)
3386 coe self
3388 | 0xff52 -> navigate ~-1 (* up *)
3389 | 0xff54 -> navigate 1 (* down *)
3390 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3391 | 0xff56 -> navigate fstate.maxrows (* next *)
3393 | 0xff53 -> (* right *)
3394 state.text <- "";
3395 G.postRedisplay "listview right";
3396 coe {< m_pan = m_pan - 1 >}
3398 | 0xff51 -> (* left *)
3399 state.text <- "";
3400 G.postRedisplay "listview left";
3401 coe {< m_pan = m_pan + 1 >}
3403 | 0xff50 -> (* home *)
3404 let active = find 0 1 in
3405 G.postRedisplay "listview home";
3406 set active 0;
3408 | 0xff57 -> (* end *)
3409 let first = max 0 (itemcount - fstate.maxrows) in
3410 let active = find (itemcount - 1) ~-1 in
3411 G.postRedisplay "listview end";
3412 set active first;
3414 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3415 coe self
3417 | _ ->
3418 dolog "listview unknown key %#x" key; coe self
3420 method key key mask =
3421 match state.mode with
3422 | Textentry te -> textentrykeyboard key mask te; coe self
3423 | _ -> self#key1 key mask
3425 method button button down x y _ =
3426 let opt =
3427 match button with
3428 | 1 when x > conf.winw - conf.scrollbw ->
3429 G.postRedisplay "listview scroll";
3430 if down
3431 then
3432 let _, position, sh = self#scrollph in
3433 if y > truncate position && y < truncate (position +. sh)
3434 then (
3435 state.mstate <- Mscrolly;
3436 Some (coe self)
3438 else
3439 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3440 let first = truncate (s *. float source#getitemcount) in
3441 let first = min source#getitemcount first in
3442 Some (coe {< m_first = first; m_active = first >})
3443 else (
3444 state.mstate <- Mnone;
3445 Some (coe self);
3447 | 1 when not down ->
3448 begin match self#elemunder y with
3449 | Some n ->
3450 G.postRedisplay "listview click";
3451 source#exit
3452 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3453 | _ ->
3454 Some (coe self)
3456 | n when (n == 4 || n == 5) && not down ->
3457 let len = source#getitemcount in
3458 let first =
3459 if n = 5 && m_first + fstate.maxrows >= len
3460 then
3461 m_first
3462 else
3463 let first = m_first + (if n == 4 then -1 else 1) in
3464 bound first 0 (len - 1)
3466 G.postRedisplay "listview wheel";
3467 Some (coe {< m_first = first >})
3468 | _ ->
3469 Some (coe self)
3471 match opt with
3472 | None -> m_prev_uioh
3473 | Some uioh -> uioh
3475 method motion _ y =
3476 match state.mstate with
3477 | Mscrolly ->
3478 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3479 let first = truncate (s *. float source#getitemcount) in
3480 let first = min source#getitemcount first in
3481 G.postRedisplay "listview motion";
3482 coe {< m_first = first; m_active = first >}
3483 | _ -> coe self
3485 method pmotion x y =
3486 if x < conf.winw - conf.scrollbw
3487 then
3488 let n =
3489 match self#elemunder y with
3490 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3491 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3493 let o =
3494 if n != m_active
3495 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3496 else self
3498 coe o
3499 else (
3500 Wsi.setcursor Wsi.CURSOR_INHERIT;
3501 coe self
3504 method infochanged _ = ()
3506 method scrollpw = (0, 0.0, 0.0)
3507 method scrollph =
3508 let nfs = fstate.fontsize + 1 in
3509 let y = m_first * nfs in
3510 let itemcount = source#getitemcount in
3511 let maxi = max 0 (itemcount - fstate.maxrows) in
3512 let maxy = maxi * nfs in
3513 let p, h = scrollph y maxy in
3514 conf.scrollbw, p, h
3516 method modehash = modehash
3517 end;;
3519 class outlinelistview ~source =
3520 object (self)
3521 inherit listview
3522 ~source:(source :> lvsource)
3523 ~trusted:false
3524 ~modehash:(findkeyhash conf "outline")
3525 as super
3527 method key key mask =
3528 let calcfirst first active =
3529 if active > first
3530 then
3531 let rows = active - first in
3532 if rows > fstate.maxrows then active - fstate.maxrows else first
3533 else active
3535 let navigate incr =
3536 let active = m_active + incr in
3537 let active = bound active 0 (source#getitemcount - 1) in
3538 let first = calcfirst m_first active in
3539 G.postRedisplay "outline navigate";
3540 coe {< m_active = active; m_first = first >}
3542 let ctrl = Wsi.withctrl mask in
3543 match key with
3544 | 110 when ctrl -> (* ctrl-n *)
3545 source#narrow m_qsearch;
3546 G.postRedisplay "outline ctrl-n";
3547 coe {< m_first = 0; m_active = 0 >}
3549 | 117 when ctrl -> (* ctrl-u *)
3550 source#denarrow;
3551 G.postRedisplay "outline ctrl-u";
3552 state.text <- "";
3553 coe {< m_first = 0; m_active = 0 >}
3555 | 108 when ctrl -> (* ctrl-l *)
3556 let first = m_active - (fstate.maxrows / 2) in
3557 G.postRedisplay "outline ctrl-l";
3558 coe {< m_first = first >}
3560 | 0xff9f | 0xffff -> (* delete *)
3561 source#remove m_active;
3562 G.postRedisplay "outline delete";
3563 let active = max 0 (m_active-1) in
3564 coe {< m_first = firstof m_first active;
3565 m_active = active >}
3567 | 0xff52 -> navigate ~-1 (* up *)
3568 | 0xff54 -> navigate 1 (* down *)
3569 | 0xff55 -> (* prior *)
3570 navigate ~-(fstate.maxrows)
3571 | 0xff56 -> (* next *)
3572 navigate fstate.maxrows
3574 | 0xff53 -> (* [ctrl-]right *)
3575 let o =
3576 if ctrl
3577 then (
3578 G.postRedisplay "outline ctrl right";
3579 {< m_pan = m_pan + 1 >}
3581 else self#updownlevel 1
3583 coe o
3585 | 0xff51 -> (* [ctrl-]left *)
3586 let o =
3587 if ctrl
3588 then (
3589 G.postRedisplay "outline ctrl left";
3590 {< m_pan = m_pan - 1 >}
3592 else self#updownlevel ~-1
3594 coe o
3596 | 0xff50 -> (* home *)
3597 G.postRedisplay "outline home";
3598 coe {< m_first = 0; m_active = 0 >}
3600 | 0xff57 -> (* end *)
3601 let active = source#getitemcount - 1 in
3602 let first = max 0 (active - fstate.maxrows) in
3603 G.postRedisplay "outline end";
3604 coe {< m_active = active; m_first = first >}
3606 | _ -> super#key key mask
3609 let outlinesource usebookmarks =
3610 let empty = [||] in
3611 (object
3612 inherit lvsourcebase
3613 val mutable m_items = empty
3614 val mutable m_orig_items = empty
3615 val mutable m_prev_items = empty
3616 val mutable m_narrow_pattern = ""
3617 val mutable m_hadremovals = false
3619 method getitemcount =
3620 Array.length m_items + (if m_hadremovals then 1 else 0)
3622 method getitem n =
3623 if n == Array.length m_items && m_hadremovals
3624 then
3625 ("[Confirm removal]", 0)
3626 else
3627 let s, n, _ = m_items.(n) in
3628 (s, n)
3630 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3631 ignore (uioh, first, qsearch);
3632 let confrimremoval = m_hadremovals && active = Array.length m_items in
3633 let items =
3634 if String.length m_narrow_pattern = 0
3635 then m_orig_items
3636 else m_items
3638 if not cancel
3639 then (
3640 if not confrimremoval
3641 then(
3642 let _, _, anchor = m_items.(active) in
3643 gotoanchor anchor;
3644 m_items <- items;
3646 else (
3647 state.bookmarks <- Array.to_list m_items;
3648 m_orig_items <- m_items;
3651 else m_items <- items;
3652 m_pan <- pan;
3653 None
3655 method hasaction _ = true
3657 method greetmsg =
3658 if Array.length m_items != Array.length m_orig_items
3659 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3660 else ""
3662 method narrow pattern =
3663 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3664 match reopt with
3665 | None -> ()
3666 | Some re ->
3667 let rec loop accu n =
3668 if n = -1
3669 then (
3670 m_narrow_pattern <- pattern;
3671 m_items <- Array.of_list accu
3673 else
3674 let (s, _, _) as o = m_items.(n) in
3675 let accu =
3676 if (try ignore (Str.search_forward re s 0); true
3677 with Not_found -> false)
3678 then o :: accu
3679 else accu
3681 loop accu (n-1)
3683 loop [] (Array.length m_items - 1)
3685 method denarrow =
3686 m_orig_items <- (
3687 if usebookmarks
3688 then Array.of_list state.bookmarks
3689 else state.outlines
3691 m_items <- m_orig_items
3693 method remove m =
3694 if usebookmarks
3695 then
3696 if m >= 0 && m < Array.length m_items
3697 then (
3698 m_hadremovals <- true;
3699 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3700 let n = if n >= m then n+1 else n in
3701 m_items.(n)
3705 method reset anchor items =
3706 m_hadremovals <- false;
3707 if m_orig_items == empty || m_prev_items != items
3708 then (
3709 m_orig_items <- items;
3710 if String.length m_narrow_pattern = 0
3711 then m_items <- items;
3713 m_prev_items <- items;
3714 let rely = getanchory anchor in
3715 let active =
3716 let rec loop n best bestd =
3717 if n = Array.length m_items
3718 then best
3719 else
3720 let (_, _, anchor) = m_items.(n) in
3721 let orely = getanchory anchor in
3722 let d = abs (orely - rely) in
3723 if d < bestd
3724 then loop (n+1) n d
3725 else loop (n+1) best bestd
3727 loop 0 ~-1 max_int
3729 m_active <- active;
3730 m_first <- firstof m_first active
3731 end)
3734 let enterselector usebookmarks =
3735 let source = outlinesource usebookmarks in
3736 fun errmsg ->
3737 let outlines =
3738 if usebookmarks
3739 then Array.of_list state.bookmarks
3740 else state.outlines
3742 if Array.length outlines = 0
3743 then (
3744 showtext ' ' errmsg;
3746 else (
3747 state.text <- source#greetmsg;
3748 Wsi.setcursor Wsi.CURSOR_INHERIT;
3749 let anchor = getanchor () in
3750 source#reset anchor outlines;
3751 state.uioh <- coe (new outlinelistview ~source);
3752 G.postRedisplay "enter selector";
3756 let enteroutlinemode =
3757 let f = enterselector false in
3758 fun ()-> f "Document has no outline";
3761 let enterbookmarkmode =
3762 let f = enterselector true in
3763 fun () -> f "Document has no bookmarks (yet)";
3766 let color_of_string s =
3767 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3768 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3772 let color_to_string (r, g, b) =
3773 let r = truncate (r *. 256.0)
3774 and g = truncate (g *. 256.0)
3775 and b = truncate (b *. 256.0) in
3776 Printf.sprintf "%d/%d/%d" r g b
3779 let irect_of_string s =
3780 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3783 let irect_to_string (x0,y0,x1,y1) =
3784 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3787 let makecheckers () =
3788 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3789 following to say:
3790 converted by Issac Trotts. July 25, 2002 *)
3791 let image_height = 64
3792 and image_width = 64 in
3794 let make_image () =
3795 let image =
3796 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3798 for i = 0 to image_width - 1 do
3799 for j = 0 to image_height - 1 do
3800 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3801 (if (i land 8 ) lxor (j land 8) = 0
3802 then [|255;255;255|] else [|200;200;200|])
3803 done
3804 done;
3805 image
3807 let image = make_image () in
3808 let id = GlTex.gen_texture () in
3809 GlTex.bind_texture `texture_2d id;
3810 GlPix.store (`unpack_alignment 1);
3811 GlTex.image2d image;
3812 List.iter (GlTex.parameter ~target:`texture_2d)
3813 [ `wrap_s `repeat;
3814 `wrap_t `repeat;
3815 `mag_filter `nearest;
3816 `min_filter `nearest ];
3820 let setcheckers enabled =
3821 match state.texid with
3822 | None ->
3823 if enabled then state.texid <- Some (makecheckers ())
3825 | Some texid ->
3826 if not enabled
3827 then (
3828 GlTex.delete_texture texid;
3829 state.texid <- None;
3833 let int_of_string_with_suffix s =
3834 let l = String.length s in
3835 let s1, shift =
3836 if l > 1
3837 then
3838 let suffix = Char.lowercase s.[l-1] in
3839 match suffix with
3840 | 'k' -> String.sub s 0 (l-1), 10
3841 | 'm' -> String.sub s 0 (l-1), 20
3842 | 'g' -> String.sub s 0 (l-1), 30
3843 | _ -> s, 0
3844 else s, 0
3846 let n = int_of_string s1 in
3847 let m = n lsl shift in
3848 if m < 0 || m < n
3849 then raise (Failure "value too large")
3850 else m
3853 let string_with_suffix_of_int n =
3854 if n = 0
3855 then "0"
3856 else
3857 let n, s =
3858 if n = 0
3859 then 0, ""
3860 else (
3861 if n land ((1 lsl 20) - 1) = 0
3862 then n lsr 20, "M"
3863 else (
3864 if n land ((1 lsl 10) - 1) = 0
3865 then n lsr 10, "K"
3866 else n, ""
3870 let rec loop s n =
3871 let h = n mod 1000 in
3872 let n = n / 1000 in
3873 if n = 0
3874 then string_of_int h ^ s
3875 else (
3876 let s = Printf.sprintf "_%03d%s" h s in
3877 loop s n
3880 loop "" n ^ s;
3883 let defghyllscroll = (40, 8, 32);;
3884 let ghyllscroll_of_string s =
3885 let (n, a, b) as nab =
3886 if s = "default"
3887 then defghyllscroll
3888 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3890 if n <= a || n <= b || a >= b
3891 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3892 nab;
3895 let ghyllscroll_to_string ((n, a, b) as nab) =
3896 if nab = defghyllscroll
3897 then "default"
3898 else Printf.sprintf "%d,%d,%d" n a b;
3901 let describe_location () =
3902 let f (fn, _) l =
3903 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3905 let fn, ln = List.fold_left f (-1, -1) state.layout in
3906 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3907 let percent =
3908 if maxy <= 0
3909 then 100.
3910 else (100. *. (float state.y /. float maxy))
3912 if fn = ln
3913 then
3914 Printf.sprintf "page %d of %d [%.2f%%]"
3915 (fn+1) state.pagecount percent
3916 else
3917 Printf.sprintf
3918 "pages %d-%d of %d [%.2f%%]"
3919 (fn+1) (ln+1) state.pagecount percent
3922 let enterinfomode =
3923 let btos b = if b then "\xe2\x88\x9a" else "" in
3924 let showextended = ref false in
3925 let leave mode = function
3926 | Confirm -> state.mode <- mode
3927 | Cancel -> state.mode <- mode in
3928 let src =
3929 (object
3930 val mutable m_first_time = true
3931 val mutable m_l = []
3932 val mutable m_a = [||]
3933 val mutable m_prev_uioh = nouioh
3934 val mutable m_prev_mode = View
3936 inherit lvsourcebase
3938 method reset prev_mode prev_uioh =
3939 m_a <- Array.of_list (List.rev m_l);
3940 m_l <- [];
3941 m_prev_mode <- prev_mode;
3942 m_prev_uioh <- prev_uioh;
3943 if m_first_time
3944 then (
3945 let rec loop n =
3946 if n >= Array.length m_a
3947 then ()
3948 else
3949 match m_a.(n) with
3950 | _, _, _, Action _ -> m_active <- n
3951 | _ -> loop (n+1)
3953 loop 0;
3954 m_first_time <- false;
3957 method int name get set =
3958 m_l <-
3959 (name, `int get, 1, Action (
3960 fun u ->
3961 let ondone s =
3962 try set (int_of_string s)
3963 with exn ->
3964 state.text <- Printf.sprintf "bad integer `%s': %s"
3965 s (Printexc.to_string exn)
3967 state.text <- "";
3968 let te = name ^ ": ", "", None, intentry, ondone in
3969 state.mode <- Textentry (te, leave m_prev_mode);
3971 )) :: m_l
3973 method int_with_suffix name get set =
3974 m_l <-
3975 (name, `intws get, 1, Action (
3976 fun u ->
3977 let ondone s =
3978 try set (int_of_string_with_suffix s)
3979 with exn ->
3980 state.text <- Printf.sprintf "bad integer `%s': %s"
3981 s (Printexc.to_string exn)
3983 state.text <- "";
3984 let te =
3985 name ^ ": ", "", None, intentry_with_suffix, ondone
3987 state.mode <- Textentry (te, leave m_prev_mode);
3989 )) :: m_l
3991 method bool ?(offset=1) ?(btos=btos) name get set =
3992 m_l <-
3993 (name, `bool (btos, get), offset, Action (
3994 fun u ->
3995 let v = get () in
3996 set (not v);
3998 )) :: m_l
4000 method color name get set =
4001 m_l <-
4002 (name, `color get, 1, Action (
4003 fun u ->
4004 let invalid = (nan, nan, nan) in
4005 let ondone s =
4006 let c =
4007 try color_of_string s
4008 with exn ->
4009 state.text <- Printf.sprintf "bad color `%s': %s"
4010 s (Printexc.to_string exn);
4011 invalid
4013 if c <> invalid
4014 then set c;
4016 let te = name ^ ": ", "", None, textentry, ondone in
4017 state.text <- color_to_string (get ());
4018 state.mode <- Textentry (te, leave m_prev_mode);
4020 )) :: m_l
4022 method string name get set =
4023 m_l <-
4024 (name, `string get, 1, Action (
4025 fun u ->
4026 let ondone s = set s in
4027 let te = name ^ ": ", "", None, textentry, ondone in
4028 state.mode <- Textentry (te, leave m_prev_mode);
4030 )) :: m_l
4032 method colorspace name get set =
4033 m_l <-
4034 (name, `string get, 1, Action (
4035 fun _ ->
4036 let source =
4037 let vals = [| "rgb"; "bgr"; "gray" |] in
4038 (object
4039 inherit lvsourcebase
4041 initializer
4042 m_active <- int_of_colorspace conf.colorspace;
4043 m_first <- 0;
4045 method getitemcount = Array.length vals
4046 method getitem n = (vals.(n), 0)
4047 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4048 ignore (uioh, first, pan, qsearch);
4049 if not cancel then set active;
4050 None
4051 method hasaction _ = true
4052 end)
4054 state.text <- "";
4055 let modehash = findkeyhash conf "info" in
4056 coe (new listview ~source ~trusted:true ~modehash)
4057 )) :: m_l
4059 method caption s offset =
4060 m_l <- (s, `empty, offset, Noaction) :: m_l
4062 method caption2 s f offset =
4063 m_l <- (s, `string f, offset, Noaction) :: m_l
4065 method getitemcount = Array.length m_a
4067 method getitem n =
4068 let tostr = function
4069 | `int f -> string_of_int (f ())
4070 | `intws f -> string_with_suffix_of_int (f ())
4071 | `string f -> f ()
4072 | `color f -> color_to_string (f ())
4073 | `bool (btos, f) -> btos (f ())
4074 | `empty -> ""
4076 let name, t, offset, _ = m_a.(n) in
4077 ((let s = tostr t in
4078 if String.length s > 0
4079 then Printf.sprintf "%s\t%s" name s
4080 else name),
4081 offset)
4083 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4084 let uiohopt =
4085 if not cancel
4086 then (
4087 m_qsearch <- qsearch;
4088 let uioh =
4089 match m_a.(active) with
4090 | _, _, _, Action f -> f uioh
4091 | _ -> uioh
4093 Some uioh
4095 else None
4097 m_active <- active;
4098 m_first <- first;
4099 m_pan <- pan;
4100 uiohopt
4102 method hasaction n =
4103 match m_a.(n) with
4104 | _, _, _, Action _ -> true
4105 | _ -> false
4106 end)
4108 let rec fillsrc prevmode prevuioh =
4109 let sep () = src#caption "" 0 in
4110 let colorp name get set =
4111 src#string name
4112 (fun () -> color_to_string (get ()))
4113 (fun v ->
4115 let c = color_of_string v in
4116 set c
4117 with exn ->
4118 state.text <- Printf.sprintf "bad color `%s': %s"
4119 v (Printexc.to_string exn);
4122 let oldmode = state.mode in
4123 let birdseye = isbirdseye state.mode in
4125 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4127 src#bool "presentation mode"
4128 (fun () -> conf.presentation)
4129 (fun v ->
4130 conf.presentation <- v;
4131 state.anchor <- getanchor ();
4132 represent ());
4134 src#bool "ignore case in searches"
4135 (fun () -> conf.icase)
4136 (fun v -> conf.icase <- v);
4138 src#bool "preload"
4139 (fun () -> conf.preload)
4140 (fun v -> conf.preload <- v);
4142 src#bool "highlight links"
4143 (fun () -> conf.hlinks)
4144 (fun v -> conf.hlinks <- v);
4146 src#bool "under info"
4147 (fun () -> conf.underinfo)
4148 (fun v -> conf.underinfo <- v);
4150 src#bool "persistent bookmarks"
4151 (fun () -> conf.savebmarks)
4152 (fun v -> conf.savebmarks <- v);
4154 src#bool "proportional display"
4155 (fun () -> conf.proportional)
4156 (fun v -> reqlayout conf.angle v);
4158 src#bool "trim margins"
4159 (fun () -> conf.trimmargins)
4160 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4162 src#bool "persistent location"
4163 (fun () -> conf.jumpback)
4164 (fun v -> conf.jumpback <- v);
4166 sep ();
4167 src#int "inter-page space"
4168 (fun () -> conf.interpagespace)
4169 (fun n ->
4170 conf.interpagespace <- n;
4171 docolumns conf.columns;
4172 let pageno, py =
4173 match state.layout with
4174 | [] -> 0, 0
4175 | l :: _ ->
4176 l.pageno, l.pagey
4178 state.maxy <- calcheight ();
4179 let y = getpagey pageno in
4180 gotoy (y + py)
4183 src#int "page bias"
4184 (fun () -> conf.pagebias)
4185 (fun v -> conf.pagebias <- v);
4187 src#int "scroll step"
4188 (fun () -> conf.scrollstep)
4189 (fun n -> conf.scrollstep <- n);
4191 src#int "auto scroll step"
4192 (fun () ->
4193 match state.autoscroll with
4194 | Some step -> step
4195 | _ -> conf.autoscrollstep)
4196 (fun n ->
4197 if state.autoscroll <> None
4198 then state.autoscroll <- Some n;
4199 conf.autoscrollstep <- n);
4201 src#int "zoom"
4202 (fun () -> truncate (conf.zoom *. 100.))
4203 (fun v -> setzoom ((float v) /. 100.));
4205 src#int "rotation"
4206 (fun () -> conf.angle)
4207 (fun v -> reqlayout v conf.proportional);
4209 src#int "scroll bar width"
4210 (fun () -> state.scrollw)
4211 (fun v ->
4212 state.scrollw <- v;
4213 conf.scrollbw <- v;
4214 reshape conf.winw conf.winh;
4217 src#int "scroll handle height"
4218 (fun () -> conf.scrollh)
4219 (fun v -> conf.scrollh <- v;);
4221 src#int "thumbnail width"
4222 (fun () -> conf.thumbw)
4223 (fun v ->
4224 conf.thumbw <- min 4096 v;
4225 match oldmode with
4226 | Birdseye beye ->
4227 leavebirdseye beye false;
4228 enterbirdseye ()
4229 | _ -> ()
4232 let mode = state.mode in
4233 src#string "columns"
4234 (fun () ->
4235 match conf.columns with
4236 | Csingle -> "1"
4237 | Cmulti (multi, _) -> multicolumns_to_string multi
4238 | Csplit (count, _) -> "-" ^ string_of_int count
4240 (fun v ->
4241 let n, a, b = multicolumns_of_string v in
4242 setcolumns mode n a b);
4244 sep ();
4245 src#caption "Presentation mode" 0;
4246 src#bool "scrollbar visible"
4247 (fun () -> conf.scrollbarinpm)
4248 (fun v ->
4249 if v != conf.scrollbarinpm
4250 then (
4251 conf.scrollbarinpm <- v;
4252 if conf.presentation
4253 then (
4254 state.scrollw <- if v then conf.scrollbw else 0;
4255 reshape conf.winw conf.winh;
4260 sep ();
4261 src#caption "Pixmap cache" 0;
4262 src#int_with_suffix "size (advisory)"
4263 (fun () -> conf.memlimit)
4264 (fun v -> conf.memlimit <- v);
4266 src#caption2 "used"
4267 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4268 (string_with_suffix_of_int state.memused)
4269 (Hashtbl.length state.tilemap)) 1;
4271 sep ();
4272 src#caption "Layout" 0;
4273 src#caption2 "Dimension"
4274 (fun () ->
4275 Printf.sprintf "%dx%d (virtual %dx%d)"
4276 conf.winw conf.winh
4277 state.w state.maxy)
4279 if conf.debug
4280 then
4281 src#caption2 "Position" (fun () ->
4282 Printf.sprintf "%dx%d" state.x state.y
4284 else
4285 src#caption2 "Visible" (fun () -> describe_location ()) 1
4288 sep ();
4289 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4290 "Save these parameters as global defaults at exit"
4291 (fun () -> conf.bedefault)
4292 (fun v -> conf.bedefault <- v)
4295 sep ();
4296 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4297 src#bool ~offset:0 ~btos "Extended parameters"
4298 (fun () -> !showextended)
4299 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4300 if !showextended
4301 then (
4302 src#bool "checkers"
4303 (fun () -> conf.checkers)
4304 (fun v -> conf.checkers <- v; setcheckers v);
4305 src#bool "update cursor"
4306 (fun () -> conf.updatecurs)
4307 (fun v -> conf.updatecurs <- v);
4308 src#bool "verbose"
4309 (fun () -> conf.verbose)
4310 (fun v -> conf.verbose <- v);
4311 src#bool "full split"
4312 (fun () -> conf.fullsplit)
4313 (fun v -> conf.fullsplit <- v; gotoy state.y);
4314 src#bool "invert colors"
4315 (fun () -> conf.invert)
4316 (fun v -> conf.invert <- v);
4317 src#bool "max fit"
4318 (fun () -> conf.maxhfit)
4319 (fun v -> conf.maxhfit <- v);
4320 src#bool "redirect stderr"
4321 (fun () -> conf.redirectstderr)
4322 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4323 src#string "uri launcher"
4324 (fun () -> conf.urilauncher)
4325 (fun v -> conf.urilauncher <- v);
4326 src#string "path launcher"
4327 (fun () -> conf.pathlauncher)
4328 (fun v -> conf.pathlauncher <- v);
4329 src#string "tile size"
4330 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4331 (fun v ->
4333 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4334 conf.tilew <- max 64 w;
4335 conf.tileh <- max 64 h;
4336 flushtiles ();
4337 with exn ->
4338 state.text <- Printf.sprintf "bad tile size `%s': %s"
4339 v (Printexc.to_string exn));
4340 src#int "texture count"
4341 (fun () -> conf.texcount)
4342 (fun v ->
4343 if realloctexts v
4344 then conf.texcount <- v
4345 else showtext '!' " Failed to set texture count please retry later"
4347 src#int "slice height"
4348 (fun () -> conf.sliceheight)
4349 (fun v ->
4350 conf.sliceheight <- v;
4351 wcmd "sliceh %d" conf.sliceheight;
4353 src#int "anti-aliasing level"
4354 (fun () -> conf.aalevel)
4355 (fun v ->
4356 conf.aalevel <- bound v 0 8;
4357 state.anchor <- getanchor ();
4358 opendoc state.path state.password;
4360 src#int "ui font size"
4361 (fun () -> fstate.fontsize)
4362 (fun v -> setfontsize (bound v 5 100));
4363 src#int "hint font size"
4364 (fun () -> conf.hfsize)
4365 (fun v -> conf.hfsize <- bound v 5 100);
4366 colorp "background color"
4367 (fun () -> conf.bgcolor)
4368 (fun v -> conf.bgcolor <- v);
4369 src#bool "crop hack"
4370 (fun () -> conf.crophack)
4371 (fun v -> conf.crophack <- v);
4372 src#string "trim fuzz"
4373 (fun () -> irect_to_string conf.trimfuzz)
4374 (fun v ->
4376 conf.trimfuzz <- irect_of_string v;
4377 if conf.trimmargins
4378 then settrim true conf.trimfuzz;
4379 with exn ->
4380 state.text <- Printf.sprintf "bad irect `%s': %s"
4381 v (Printexc.to_string exn)
4383 src#string "throttle"
4384 (fun () ->
4385 match conf.maxwait with
4386 | None -> "show place holder if page is not ready"
4387 | Some time ->
4388 if time = infinity
4389 then "wait for page to fully render"
4390 else
4391 "wait " ^ string_of_float time
4392 ^ " seconds before showing placeholder"
4394 (fun v ->
4396 let f = float_of_string v in
4397 if f <= 0.0
4398 then conf.maxwait <- None
4399 else conf.maxwait <- Some f
4400 with exn ->
4401 state.text <- Printf.sprintf "bad time `%s': %s"
4402 v (Printexc.to_string exn)
4404 src#string "ghyll scroll"
4405 (fun () ->
4406 match conf.ghyllscroll with
4407 | None -> ""
4408 | Some nab -> ghyllscroll_to_string nab
4410 (fun v ->
4412 let gs =
4413 if String.length v = 0
4414 then None
4415 else Some (ghyllscroll_of_string v)
4417 conf.ghyllscroll <- gs
4418 with exn ->
4419 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4420 v (Printexc.to_string exn)
4422 src#string "selection command"
4423 (fun () -> conf.selcmd)
4424 (fun v -> conf.selcmd <- v);
4425 src#colorspace "color space"
4426 (fun () -> colorspace_to_string conf.colorspace)
4427 (fun v ->
4428 conf.colorspace <- colorspace_of_int v;
4429 wcmd "cs %d" v;
4430 load state.layout;
4434 sep ();
4435 src#caption "Document" 0;
4436 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4437 src#caption2 "Pages"
4438 (fun () -> string_of_int state.pagecount) 1;
4439 src#caption2 "Dimensions"
4440 (fun () -> string_of_int (List.length state.pdims)) 1;
4441 if conf.trimmargins
4442 then (
4443 sep ();
4444 src#caption "Trimmed margins" 0;
4445 src#caption2 "Dimensions"
4446 (fun () -> string_of_int (List.length state.pdims)) 1;
4449 src#reset prevmode prevuioh;
4451 fun () ->
4452 state.text <- "";
4453 let prevmode = state.mode
4454 and prevuioh = state.uioh in
4455 fillsrc prevmode prevuioh;
4456 let source = (src :> lvsource) in
4457 let modehash = findkeyhash conf "info" in
4458 state.uioh <- coe (object (self)
4459 inherit listview ~source ~trusted:true ~modehash as super
4460 val mutable m_prevmemused = 0
4461 method infochanged = function
4462 | Memused ->
4463 if m_prevmemused != state.memused
4464 then (
4465 m_prevmemused <- state.memused;
4466 G.postRedisplay "memusedchanged";
4468 | Pdim -> G.postRedisplay "pdimchanged"
4469 | Docinfo -> fillsrc prevmode prevuioh
4471 method key key mask =
4472 if not (Wsi.withctrl mask)
4473 then
4474 match key with
4475 | 0xff51 -> coe (self#updownlevel ~-1)
4476 | 0xff53 -> coe (self#updownlevel 1)
4477 | _ -> super#key key mask
4478 else super#key key mask
4479 end);
4480 G.postRedisplay "info";
4483 let enterhelpmode =
4484 let source =
4485 (object
4486 inherit lvsourcebase
4487 method getitemcount = Array.length state.help
4488 method getitem n =
4489 let s, n, _ = state.help.(n) in
4490 (s, n)
4492 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4493 let optuioh =
4494 if not cancel
4495 then (
4496 m_qsearch <- qsearch;
4497 match state.help.(active) with
4498 | _, _, Action f -> Some (f uioh)
4499 | _ -> Some (uioh)
4501 else None
4503 m_active <- active;
4504 m_first <- first;
4505 m_pan <- pan;
4506 optuioh
4508 method hasaction n =
4509 match state.help.(n) with
4510 | _, _, Action _ -> true
4511 | _ -> false
4513 initializer
4514 m_active <- -1
4515 end)
4516 in fun () ->
4517 let modehash = findkeyhash conf "help" in
4518 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4519 G.postRedisplay "help";
4522 let entermsgsmode =
4523 let msgsource =
4524 let re = Str.regexp "[\r\n]" in
4525 (object
4526 inherit lvsourcebase
4527 val mutable m_items = [||]
4529 method getitemcount = 1 + Array.length m_items
4531 method getitem n =
4532 if n = 0
4533 then "[Clear]", 0
4534 else m_items.(n-1), 0
4536 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4537 ignore uioh;
4538 if not cancel
4539 then (
4540 if active = 0
4541 then Buffer.clear state.errmsgs;
4542 m_qsearch <- qsearch;
4544 m_active <- active;
4545 m_first <- first;
4546 m_pan <- pan;
4547 None
4549 method hasaction n =
4550 n = 0
4552 method reset =
4553 state.newerrmsgs <- false;
4554 let l = Str.split re (Buffer.contents state.errmsgs) in
4555 m_items <- Array.of_list l
4557 initializer
4558 m_active <- 0
4559 end)
4560 in fun () ->
4561 state.text <- "";
4562 msgsource#reset;
4563 let source = (msgsource :> lvsource) in
4564 let modehash = findkeyhash conf "listview" in
4565 state.uioh <- coe (object
4566 inherit listview ~source ~trusted:false ~modehash as super
4567 method display =
4568 if state.newerrmsgs
4569 then msgsource#reset;
4570 super#display
4571 end);
4572 G.postRedisplay "msgs";
4575 let quickbookmark ?title () =
4576 match state.layout with
4577 | [] -> ()
4578 | l :: _ ->
4579 let title =
4580 match title with
4581 | None ->
4582 let sec = Unix.gettimeofday () in
4583 let tm = Unix.localtime sec in
4584 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4585 (l.pageno+1)
4586 tm.Unix.tm_mday
4587 tm.Unix.tm_mon
4588 (tm.Unix.tm_year + 1900)
4589 tm.Unix.tm_hour
4590 tm.Unix.tm_min
4591 | Some title -> title
4593 state.bookmarks <-
4594 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4595 :: state.bookmarks
4598 let doreshape w h =
4599 state.fullscreen <- None;
4600 Wsi.reshape w h;
4603 let setautoscrollspeed step goingdown =
4604 let incr = max 1 ((abs step) / 2) in
4605 let incr = if goingdown then incr else -incr in
4606 let astep = step + incr in
4607 state.autoscroll <- Some astep;
4610 let gotounder = function
4611 | Ulinkgoto (pageno, top) ->
4612 if pageno >= 0
4613 then (
4614 addnav ();
4615 gotopage1 pageno top;
4618 | Ulinkuri s ->
4619 gotouri s
4621 | Uremote (filename, pageno) ->
4622 let path =
4623 if Sys.file_exists filename
4624 then filename
4625 else
4626 let dir = Filename.dirname state.path in
4627 let path = Filename.concat dir filename in
4628 if Sys.file_exists path
4629 then path
4630 else ""
4632 if String.length path > 0
4633 then (
4634 let anchor = getanchor () in
4635 let ranchor = state.path, state.password, anchor in
4636 state.anchor <- (pageno, 0.0);
4637 state.ranchors <- ranchor :: state.ranchors;
4638 opendoc path "";
4640 else showtext '!' ("Could not find " ^ filename)
4642 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4645 let canpan () =
4646 match conf.columns with
4647 | Csplit _ -> true
4648 | _ -> conf.zoom > 1.0
4651 let viewkeyboard key mask =
4652 let enttext te =
4653 let mode = state.mode in
4654 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4655 state.text <- "";
4656 enttext ();
4657 G.postRedisplay "view:enttext"
4659 let ctrl = Wsi.withctrl mask in
4660 match key with
4661 | 81 -> (* Q *)
4662 exit 0
4664 | 0xff63 -> (* insert *)
4665 if conf.angle mod 360 = 0
4666 then (
4667 state.mode <- LinkNav (Ltgendir 0);
4668 gotoy state.y;
4670 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4672 | 0xff1b | 113 -> (* escape / q *)
4673 begin match state.mstate with
4674 | Mzoomrect _ ->
4675 state.mstate <- Mnone;
4676 Wsi.setcursor Wsi.CURSOR_INHERIT;
4677 G.postRedisplay "kill zoom rect";
4678 | _ ->
4679 match state.ranchors with
4680 | [] -> raise Quit
4681 | (path, password, anchor) :: rest ->
4682 state.ranchors <- rest;
4683 state.anchor <- anchor;
4684 opendoc path password
4685 end;
4687 | 0xff08 -> (* backspace *)
4688 let y = getnav ~-1 in
4689 gotoy_and_clear_text y
4691 | 111 -> (* o *)
4692 enteroutlinemode ()
4694 | 117 -> (* u *)
4695 state.rects <- [];
4696 state.text <- "";
4697 G.postRedisplay "dehighlight";
4699 | 47 | 63 -> (* / ? *)
4700 let ondone isforw s =
4701 cbput state.hists.pat s;
4702 state.searchpattern <- s;
4703 search s isforw
4705 let s = String.create 1 in
4706 s.[0] <- Char.chr key;
4707 enttext (s, "", Some (onhist state.hists.pat),
4708 textentry, ondone (key = 47))
4710 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4711 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4712 setzoom (conf.zoom +. incr)
4714 | 43 | 0xffab -> (* + *)
4715 let ondone s =
4716 let n =
4717 try int_of_string s with exc ->
4718 state.text <- Printf.sprintf "bad integer `%s': %s"
4719 s (Printexc.to_string exc);
4720 max_int
4722 if n != max_int
4723 then (
4724 conf.pagebias <- n;
4725 state.text <- "page bias is now " ^ string_of_int n;
4728 enttext ("page bias: ", "", None, intentry, ondone)
4730 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4731 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4732 setzoom (max 0.01 (conf.zoom -. decr))
4734 | 45 | 0xffad -> (* - *)
4735 let ondone msg = state.text <- msg in
4736 enttext (
4737 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4738 optentry state.mode, ondone
4741 | 48 when ctrl -> (* ctrl-0 *)
4742 setzoom 1.0
4744 | 49 when ctrl -> (* 1 *)
4745 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4746 if zoom < 1.0
4747 then setzoom zoom
4749 | 0xffc6 -> (* f9 *)
4750 togglebirdseye ()
4752 | 57 when ctrl -> (* ctrl-9 *)
4753 togglebirdseye ()
4755 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4756 when not ctrl -> (* 0..9 *)
4757 let ondone s =
4758 let n =
4759 try int_of_string s with exc ->
4760 state.text <- Printf.sprintf "bad integer `%s': %s"
4761 s (Printexc.to_string exc);
4764 if n >= 0
4765 then (
4766 addnav ();
4767 cbput state.hists.pag (string_of_int n);
4768 gotopage1 (n + conf.pagebias - 1) 0;
4771 let pageentry text key =
4772 match Char.unsafe_chr key with
4773 | 'g' -> TEdone text
4774 | _ -> intentry text key
4776 let text = "x" in text.[0] <- Char.chr key;
4777 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4779 | 98 -> (* b *)
4780 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4781 reshape conf.winw conf.winh;
4783 | 108 -> (* l *)
4784 conf.hlinks <- not conf.hlinks;
4785 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4786 G.postRedisplay "toggle highlightlinks";
4788 | 70 -> (* F *)
4789 state.glinks <- true;
4790 let mode = state.mode in
4791 state.mode <- Textentry (
4792 (":", "", None, linknentry, linkndone (fun under ->
4793 addnav ();
4794 gotounder under
4796 ), fun _ ->
4797 state.glinks <- false;
4798 state.mode <- mode
4800 state.text <- "";
4801 G.postRedisplay "view:linkent(F)"
4803 | 121 -> (* y *)
4804 state.glinks <- true;
4805 let mode = state.mode in
4806 state.mode <- Textentry (
4807 (":", "", None, linknentry, linkndone (fun under ->
4808 match Ne.pipe () with
4809 | Ne.Exn exn ->
4810 showtext '!' (Printf.sprintf "pipe failed: %s"
4811 (Printexc.to_string exn));
4812 | Ne.Res (r, w) ->
4813 let popened =
4814 try popen conf.selcmd [r, 0; w, -1]; true
4815 with exn ->
4816 showtext '!'
4817 (Printf.sprintf "failed to execute %s: %s"
4818 conf.selcmd (Printexc.to_string exn));
4819 false
4821 let clo cap fd =
4822 Ne.clo fd (fun msg ->
4823 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4826 let s = undertext under in
4827 if popened
4828 then
4829 (try
4830 let l = String.length s in
4831 let n = Unix.write w s 0 l in
4832 if n != l
4833 then
4834 showtext '!'
4835 (Printf.sprintf
4836 "failed to write %d characters to sel pipe, wrote %d"
4839 with exn ->
4840 showtext '!'
4841 (Printf.sprintf "failed to write to sel pipe: %s"
4842 (Printexc.to_string exn)
4845 else dolog "%s" s;
4846 clo "pipe/r" r;
4847 clo "pipe/w" w;
4850 fun _ ->
4851 state.glinks <- false;
4852 state.mode <- mode
4854 state.text <- "";
4855 G.postRedisplay "view:linkent"
4857 | 97 -> (* a *)
4858 begin match state.autoscroll with
4859 | Some step ->
4860 conf.autoscrollstep <- step;
4861 state.autoscroll <- None
4862 | None ->
4863 if conf.autoscrollstep = 0
4864 then state.autoscroll <- Some 1
4865 else state.autoscroll <- Some conf.autoscrollstep
4868 | 112 when ctrl -> (* ctrl-p *)
4869 launchpath ()
4871 | 80 -> (* P *)
4872 conf.presentation <- not conf.presentation;
4873 if conf.presentation
4874 then (
4875 if not conf.scrollbarinpm
4876 then state.scrollw <- 0;
4878 else
4879 state.scrollw <- conf.scrollbw;
4881 showtext ' ' ("presentation mode " ^
4882 if conf.presentation then "on" else "off");
4883 state.anchor <- getanchor ();
4884 represent ()
4886 | 102 -> (* f *)
4887 begin match state.fullscreen with
4888 | None ->
4889 state.fullscreen <- Some (conf.winw, conf.winh);
4890 Wsi.fullscreen ()
4891 | Some (w, h) ->
4892 state.fullscreen <- None;
4893 doreshape w h
4896 | 103 -> (* g *)
4897 gotoy_and_clear_text 0
4899 | 71 -> (* G *)
4900 gotopage1 (state.pagecount - 1) 0
4902 | 112 | 78 -> (* p|N *)
4903 search state.searchpattern false
4905 | 110 | 0xffc0 -> (* n|F3 *)
4906 search state.searchpattern true
4908 | 116 -> (* t *)
4909 begin match state.layout with
4910 | [] -> ()
4911 | l :: _ ->
4912 gotoy_and_clear_text (getpagey l.pageno)
4915 | 32 -> (* ' ' *)
4916 begin match List.rev state.layout with
4917 | [] -> ()
4918 | l :: _ ->
4919 let pageno = min (l.pageno+1) (state.pagecount-1) in
4920 gotoy_and_clear_text (getpagey pageno)
4923 | 0xff9f | 0xffff -> (* delete *)
4924 begin match state.layout with
4925 | [] -> ()
4926 | l :: _ ->
4927 let pageno = max 0 (l.pageno-1) in
4928 gotoy_and_clear_text (getpagey pageno)
4931 | 61 -> (* = *)
4932 showtext ' ' (describe_location ());
4934 | 119 -> (* w *)
4935 begin match state.layout with
4936 | [] -> ()
4937 | l :: _ ->
4938 doreshape (l.pagew + state.scrollw) l.pageh;
4939 G.postRedisplay "w"
4942 | 39 -> (* ' *)
4943 enterbookmarkmode ()
4945 | 104 | 0xffbe -> (* h|F1 *)
4946 enterhelpmode ()
4948 | 105 -> (* i *)
4949 enterinfomode ()
4951 | 101 when conf.redirectstderr -> (* e *)
4952 entermsgsmode ()
4954 | 109 -> (* m *)
4955 let ondone s =
4956 match state.layout with
4957 | l :: _ ->
4958 state.bookmarks <-
4959 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4960 :: state.bookmarks
4961 | _ -> ()
4963 enttext ("bookmark: ", "", None, textentry, ondone)
4965 | 126 -> (* ~ *)
4966 quickbookmark ();
4967 showtext ' ' "Quick bookmark added";
4969 | 122 -> (* z *)
4970 begin match state.layout with
4971 | l :: _ ->
4972 let rect = getpdimrect l.pagedimno in
4973 let w, h =
4974 if conf.crophack
4975 then
4976 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4977 truncate (1.2 *. (rect.(3) -. rect.(0))))
4978 else
4979 (truncate (rect.(1) -. rect.(0)),
4980 truncate (rect.(3) -. rect.(0)))
4982 let w = truncate ((float w)*.conf.zoom)
4983 and h = truncate ((float h)*.conf.zoom) in
4984 if w != 0 && h != 0
4985 then (
4986 state.anchor <- getanchor ();
4987 doreshape (w + state.scrollw) (h + conf.interpagespace)
4989 G.postRedisplay "z";
4991 | [] -> ()
4994 | 50 when ctrl -> (* ctrl-2 *)
4995 let maxw = getmaxw () in
4996 if maxw > 0.0
4997 then setzoom (maxw /. float conf.winw)
4999 | 60 | 62 -> (* < > *)
5000 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5002 | 91 | 93 -> (* [ ] *)
5003 conf.colorscale <-
5004 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5006 G.postRedisplay "brightness";
5008 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5009 setzoom state.prevzoom
5011 | 107 | 0xff52 -> (* k up *)
5012 begin match state.autoscroll with
5013 | None ->
5014 begin match state.mode with
5015 | Birdseye beye -> upbirdseye 1 beye
5016 | _ ->
5017 if ctrl
5018 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5019 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5021 | Some n ->
5022 setautoscrollspeed n false
5025 | 106 | 0xff54 -> (* j down *)
5026 begin match state.autoscroll with
5027 | None ->
5028 begin match state.mode with
5029 | Birdseye beye -> downbirdseye 1 beye
5030 | _ ->
5031 if ctrl
5032 then gotoy_and_clear_text (clamp (conf.winh/2))
5033 else gotoy_and_clear_text (clamp conf.scrollstep)
5035 | Some n ->
5036 setautoscrollspeed n true
5039 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5040 if canpan ()
5041 then
5042 let dx =
5043 if ctrl
5044 then conf.winw / 2
5045 else 10
5047 let dx = if key = 0xff51 then dx else -dx in
5048 state.x <- state.x + dx;
5049 gotoy_and_clear_text state.y
5050 else (
5051 state.text <- "";
5052 G.postRedisplay "lef/right"
5055 | 0xff55 -> (* prior *)
5056 let y =
5057 if ctrl
5058 then
5059 match state.layout with
5060 | [] -> state.y
5061 | l :: _ -> state.y - l.pagey
5062 else
5063 clamp (-conf.winh)
5065 gotoghyll y
5067 | 0xff56 -> (* next *)
5068 let y =
5069 if ctrl
5070 then
5071 match List.rev state.layout with
5072 | [] -> state.y
5073 | l :: _ -> getpagey l.pageno
5074 else
5075 clamp conf.winh
5077 gotoghyll y
5079 | 0xff50 -> gotoghyll 0
5080 | 0xff57 -> gotoghyll (clamp state.maxy)
5081 | 0xff53 when Wsi.withalt mask ->
5082 gotoghyll (getnav ~-1)
5083 | 0xff51 when Wsi.withalt mask ->
5084 gotoghyll (getnav 1)
5086 | 114 -> (* r *)
5087 state.anchor <- getanchor ();
5088 opendoc state.path state.password
5090 | 118 when conf.debug -> (* v *)
5091 state.rects <- [];
5092 List.iter (fun l ->
5093 match getopaque l.pageno with
5094 | None -> ()
5095 | Some opaque ->
5096 let x0, y0, x1, y1 = pagebbox opaque in
5097 let a,b = float x0, float y0 in
5098 let c,d = float x1, float y0 in
5099 let e,f = float x1, float y1 in
5100 let h,j = float x0, float y1 in
5101 let rect = (a,b,c,d,e,f,h,j) in
5102 debugrect rect;
5103 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5104 ) state.layout;
5105 G.postRedisplay "v";
5107 | _ ->
5108 vlog "huh? %s" (Wsi.keyname key)
5111 let linknavkeyboard key mask linknav =
5112 let getpage pageno =
5113 let rec loop = function
5114 | [] -> None
5115 | l :: _ when l.pageno = pageno -> Some l
5116 | _ :: rest -> loop rest
5117 in loop state.layout
5119 let doexact (pageno, n) =
5120 match getopaque pageno, getpage pageno with
5121 | Some opaque, Some l ->
5122 if key = 0xff0d
5123 then
5124 let under = getlink opaque n in
5125 G.postRedisplay "link gotounder";
5126 gotounder under;
5127 state.mode <- View;
5128 else
5129 let opt, dir =
5130 match key with
5131 | 0xff50 -> (* home *)
5132 Some (findlink opaque LDfirst), -1
5134 | 0xff57 -> (* end *)
5135 Some (findlink opaque LDlast), 1
5137 | 0xff51 -> (* left *)
5138 Some (findlink opaque (LDleft n)), -1
5140 | 0xff53 -> (* right *)
5141 Some (findlink opaque (LDright n)), 1
5143 | 0xff52 -> (* up *)
5144 Some (findlink opaque (LDup n)), -1
5146 | 0xff54 -> (* down *)
5147 Some (findlink opaque (LDdown n)), 1
5149 | _ -> None, 0
5151 let pwl l dir =
5152 begin match findpwl l.pageno dir with
5153 | Pwlnotfound -> ()
5154 | Pwl pageno ->
5155 let notfound dir =
5156 state.mode <- LinkNav (Ltgendir dir);
5157 let y, h = getpageyh pageno in
5158 let y =
5159 if dir < 0
5160 then y + h - conf.winh
5161 else y
5163 gotoy y
5165 begin match getopaque pageno, getpage pageno with
5166 | Some opaque, Some _ ->
5167 let link =
5168 let ld = if dir > 0 then LDfirst else LDlast in
5169 findlink opaque ld
5171 begin match link with
5172 | Lfound m ->
5173 showlinktype (getlink opaque m);
5174 state.mode <- LinkNav (Ltexact (pageno, m));
5175 G.postRedisplay "linknav jpage";
5176 | _ -> notfound dir
5177 end;
5178 | _ -> notfound dir
5179 end;
5180 end;
5182 begin match opt with
5183 | Some Lnotfound -> pwl l dir;
5184 | Some (Lfound m) ->
5185 if m = n
5186 then pwl l dir
5187 else (
5188 let _, y0, _, y1 = getlinkrect opaque m in
5189 if y0 < l.pagey
5190 then gotopage1 l.pageno y0
5191 else (
5192 let d = fstate.fontsize + 1 in
5193 if y1 - l.pagey > l.pagevh - d
5194 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5195 else G.postRedisplay "linknav";
5197 showlinktype (getlink opaque m);
5198 state.mode <- LinkNav (Ltexact (l.pageno, m));
5201 | None -> viewkeyboard key mask
5202 end;
5203 | _ -> viewkeyboard key mask
5205 if key = 0xff63
5206 then (
5207 state.mode <- View;
5208 G.postRedisplay "leave linknav"
5210 else
5211 match linknav with
5212 | Ltgendir _ -> viewkeyboard key mask
5213 | Ltexact exact -> doexact exact
5216 let keyboard key mask =
5217 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5218 then wcmd "interrupt"
5219 else state.uioh <- state.uioh#key key mask
5222 let birdseyekeyboard key mask
5223 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5224 let incr =
5225 match conf.columns with
5226 | Csingle -> 1
5227 | Cmulti ((c, _, _), _) -> c
5228 | Csplit _ -> failwith "bird's eye split mode"
5230 match key with
5231 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5232 let y, h = getpageyh pageno in
5233 let top = (conf.winh - h) / 2 in
5234 gotoy (max 0 (y - top))
5235 | 0xff0d -> leavebirdseye beye false
5236 | 0xff1b -> leavebirdseye beye true (* escape *)
5237 | 0xff52 -> upbirdseye incr beye (* prior *)
5238 | 0xff54 -> downbirdseye incr beye (* next *)
5239 | 0xff51 -> upbirdseye 1 beye (* up *)
5240 | 0xff53 -> downbirdseye 1 beye (* down *)
5242 | 0xff55 ->
5243 begin match state.layout with
5244 | l :: _ ->
5245 if l.pagey != 0
5246 then (
5247 state.mode <- Birdseye (
5248 oconf, leftx, l.pageno, hooverpageno, anchor
5250 gotopage1 l.pageno 0;
5252 else (
5253 let layout = layout (state.y-conf.winh) conf.winh in
5254 match layout with
5255 | [] -> gotoy (clamp (-conf.winh))
5256 | l :: _ ->
5257 state.mode <- Birdseye (
5258 oconf, leftx, l.pageno, hooverpageno, anchor
5260 gotopage1 l.pageno 0
5263 | [] -> gotoy (clamp (-conf.winh))
5264 end;
5266 | 0xff56 ->
5267 begin match List.rev state.layout with
5268 | l :: _ ->
5269 let layout = layout (state.y + conf.winh) conf.winh in
5270 begin match layout with
5271 | [] ->
5272 let incr = l.pageh - l.pagevh in
5273 if incr = 0
5274 then (
5275 state.mode <-
5276 Birdseye (
5277 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5279 G.postRedisplay "birdseye pagedown";
5281 else gotoy (clamp (incr + conf.interpagespace*2));
5283 | l :: _ ->
5284 state.mode <-
5285 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5286 gotopage1 l.pageno 0;
5289 | [] -> gotoy (clamp conf.winh)
5290 end;
5292 | 0xff50 ->
5293 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5294 gotopage1 0 0
5296 | 0xff57 ->
5297 let pageno = state.pagecount - 1 in
5298 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5299 if not (pagevisible state.layout pageno)
5300 then
5301 let h =
5302 match List.rev state.pdims with
5303 | [] -> conf.winh
5304 | (_, _, h, _) :: _ -> h
5306 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5307 else G.postRedisplay "birdseye end";
5308 | _ -> viewkeyboard key mask
5311 let drawpage l linkindexbase =
5312 let color =
5313 match state.mode with
5314 | Textentry _ -> scalecolor 0.4
5315 | LinkNav _
5316 | View -> scalecolor 1.0
5317 | Birdseye (_, _, pageno, hooverpageno, _) ->
5318 if l.pageno = hooverpageno
5319 then scalecolor 0.9
5320 else (
5321 if l.pageno = pageno
5322 then scalecolor 1.0
5323 else scalecolor 0.8
5326 drawtiles l color;
5327 begin match getopaque l.pageno with
5328 | Some opaque ->
5329 if tileready l l.pagex l.pagey
5330 then
5331 let x = l.pagedispx - l.pagex
5332 and y = l.pagedispy - l.pagey in
5333 let hlmask = (if conf.hlinks then 1 else 0)
5334 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5336 let s =
5337 match state.mode with
5338 | Textentry ((_, s, _, _, _), _) when state.glinks -> s
5339 | _ -> ""
5341 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5342 else 0
5344 | _ -> 0
5345 end;
5348 let scrollindicator () =
5349 let sbw, ph, sh = state.uioh#scrollph in
5350 let sbh, pw, sw = state.uioh#scrollpw in
5352 GlDraw.color (0.64, 0.64, 0.64);
5353 GlDraw.rect
5354 (float (conf.winw - sbw), 0.)
5355 (float conf.winw, float conf.winh)
5357 GlDraw.rect
5358 (0., float (conf.winh - sbh))
5359 (float (conf.winw - state.scrollw - 1), float conf.winh)
5361 GlDraw.color (0.0, 0.0, 0.0);
5363 GlDraw.rect
5364 (float (conf.winw - sbw), ph)
5365 (float conf.winw, ph +. sh)
5367 GlDraw.rect
5368 (pw, float (conf.winh - sbh))
5369 (pw +. sw, float conf.winh)
5373 let showsel () =
5374 match state.mstate with
5375 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5378 | Msel ((x0, y0), (x1, y1)) ->
5379 let rec loop = function
5380 | l :: ls ->
5381 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5382 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5383 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5384 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5385 then
5386 match getopaque l.pageno with
5387 | Some opaque ->
5388 let x0, y0 = pagetranslatepoint l x0 y0 in
5389 let x1, y1 = pagetranslatepoint l x1 y1 in
5390 seltext opaque (x0, y0, x1, y1);
5391 | _ -> ()
5392 else loop ls
5393 | [] -> ()
5395 loop state.layout
5398 let showrects rects =
5399 Gl.enable `blend;
5400 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5401 GlDraw.polygon_mode `both `fill;
5402 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5403 List.iter
5404 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5405 List.iter (fun l ->
5406 if l.pageno = pageno
5407 then (
5408 let dx = float (l.pagedispx - l.pagex) in
5409 let dy = float (l.pagedispy - l.pagey) in
5410 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5411 GlDraw.begins `quads;
5413 GlDraw.vertex2 (x0+.dx, y0+.dy);
5414 GlDraw.vertex2 (x1+.dx, y1+.dy);
5415 GlDraw.vertex2 (x2+.dx, y2+.dy);
5416 GlDraw.vertex2 (x3+.dx, y3+.dy);
5418 GlDraw.ends ();
5420 ) state.layout
5421 ) rects
5423 Gl.disable `blend;
5426 let display () =
5427 GlClear.color (scalecolor2 conf.bgcolor);
5428 GlClear.clear [`color];
5429 let rec loop linkindexbase = function
5430 | l :: rest ->
5431 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5432 loop linkindexbase rest
5433 | [] -> ()
5435 loop 0 state.layout;
5436 let rects =
5437 match state.mode with
5438 | LinkNav (Ltexact (pageno, linkno)) ->
5439 begin match getopaque pageno with
5440 | Some opaque ->
5441 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5442 (pageno, 5, (
5443 float x0, float y0,
5444 float x1, float y0,
5445 float x1, float y1,
5446 float x0, float y1)
5447 ) :: state.rects
5448 | None -> state.rects
5450 | _ -> state.rects
5452 showrects rects;
5453 showsel ();
5454 state.uioh#display;
5455 begin match state.mstate with
5456 | Mzoomrect ((x0, y0), (x1, y1)) ->
5457 Gl.enable `blend;
5458 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5459 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5460 GlDraw.rect (float x0, float y0)
5461 (float x1, float y1);
5462 Gl.disable `blend;
5463 | _ -> ()
5464 end;
5465 enttext ();
5466 scrollindicator ();
5467 Wsi.swapb ();
5470 let zoomrect x y x1 y1 =
5471 let x0 = min x x1
5472 and x1 = max x x1
5473 and y0 = min y y1 in
5474 gotoy (state.y + y0);
5475 state.anchor <- getanchor ();
5476 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5477 let margin =
5478 if state.w < conf.winw - state.scrollw
5479 then (conf.winw - state.scrollw - state.w) / 2
5480 else 0
5482 state.x <- (state.x + margin) - x0;
5483 setzoom zoom;
5484 Wsi.setcursor Wsi.CURSOR_INHERIT;
5485 state.mstate <- Mnone;
5488 let scrollx x =
5489 let winw = conf.winw - state.scrollw - 1 in
5490 let s = float x /. float winw in
5491 let destx = truncate (float (state.w + winw) *. s) in
5492 state.x <- winw - destx;
5493 gotoy_and_clear_text state.y;
5494 state.mstate <- Mscrollx;
5497 let scrolly y =
5498 let s = float y /. float conf.winh in
5499 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5500 gotoy_and_clear_text desty;
5501 state.mstate <- Mscrolly;
5504 let viewmouse button down x y mask =
5505 match button with
5506 | n when (n == 4 || n == 5) && not down ->
5507 if Wsi.withctrl mask
5508 then (
5509 match state.mstate with
5510 | Mzoom (oldn, i) ->
5511 if oldn = n
5512 then (
5513 if i = 2
5514 then
5515 let incr =
5516 match n with
5517 | 5 ->
5518 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5519 | _ ->
5520 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5522 let zoom = conf.zoom -. incr in
5523 setzoom zoom;
5524 state.mstate <- Mzoom (n, 0);
5525 else
5526 state.mstate <- Mzoom (n, i+1);
5528 else state.mstate <- Mzoom (n, 0)
5530 | _ -> state.mstate <- Mzoom (n, 0)
5532 else (
5533 match state.autoscroll with
5534 | Some step -> setautoscrollspeed step (n=4)
5535 | None ->
5536 let incr =
5537 if n = 4
5538 then -conf.scrollstep
5539 else conf.scrollstep
5541 let incr = incr * 2 in
5542 let y = clamp incr in
5543 gotoy_and_clear_text y
5546 | 1 when Wsi.withctrl mask ->
5547 if down
5548 then (
5549 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5550 state.mstate <- Mpan (x, y)
5552 else
5553 state.mstate <- Mnone
5555 | 3 ->
5556 if down
5557 then (
5558 Wsi.setcursor Wsi.CURSOR_CYCLE;
5559 let p = (x, y) in
5560 state.mstate <- Mzoomrect (p, p)
5562 else (
5563 match state.mstate with
5564 | Mzoomrect ((x0, y0), _) ->
5565 if abs (x-x0) > 10 && abs (y - y0) > 10
5566 then zoomrect x0 y0 x y
5567 else (
5568 state.mstate <- Mnone;
5569 Wsi.setcursor Wsi.CURSOR_INHERIT;
5570 G.postRedisplay "kill accidental zoom rect";
5572 | _ ->
5573 Wsi.setcursor Wsi.CURSOR_INHERIT;
5574 state.mstate <- Mnone
5577 | 1 when x > conf.winw - state.scrollw ->
5578 if down
5579 then
5580 let _, position, sh = state.uioh#scrollph in
5581 if y > truncate position && y < truncate (position +. sh)
5582 then state.mstate <- Mscrolly
5583 else scrolly y
5584 else
5585 state.mstate <- Mnone
5587 | 1 when y > conf.winh - state.hscrollh ->
5588 if down
5589 then
5590 let _, position, sw = state.uioh#scrollpw in
5591 if x > truncate position && x < truncate (position +. sw)
5592 then state.mstate <- Mscrollx
5593 else scrollx x
5594 else
5595 state.mstate <- Mnone
5597 | 1 ->
5598 let dest = if down then getunder x y else Unone in
5599 begin match dest with
5600 | Ulinkgoto _
5601 | Ulinkuri _
5602 | Uremote _
5603 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5604 gotounder dest
5606 | Unone when down ->
5607 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5608 state.mstate <- Mpan (x, y);
5610 | Unone | Utext _ ->
5611 if down
5612 then (
5613 if conf.angle mod 360 = 0
5614 then (
5615 state.mstate <- Msel ((x, y), (x, y));
5616 G.postRedisplay "mouse select";
5619 else (
5620 match state.mstate with
5621 | Mnone -> ()
5623 | Mzoom _ | Mscrollx | Mscrolly ->
5624 state.mstate <- Mnone
5626 | Mzoomrect ((x0, y0), _) ->
5627 zoomrect x0 y0 x y
5629 | Mpan _ ->
5630 Wsi.setcursor Wsi.CURSOR_INHERIT;
5631 state.mstate <- Mnone
5633 | Msel ((_, y0), (_, y1)) ->
5634 let rec loop = function
5635 | [] -> ()
5636 | l :: rest ->
5637 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5638 || ((y1 >= l.pagedispy
5639 && y1 <= (l.pagedispy + l.pagevh)))
5640 then
5641 match getopaque l.pageno with
5642 | Some opaque ->
5643 begin
5644 match Ne.pipe () with
5645 | Ne.Exn exn ->
5646 showtext '!'
5647 (Printf.sprintf
5648 "can not create sel pipe: %s"
5649 (Printexc.to_string exn));
5650 | Ne.Res (r, w) ->
5651 let doclose what fd =
5652 Ne.clo fd (fun msg ->
5653 dolog "%s close failed: %s" what msg)
5656 popen conf.selcmd [r, 0; w, -1];
5657 copysel w opaque;
5658 doclose "pipe/r" r;
5659 G.postRedisplay "copysel";
5660 with exn ->
5661 dolog "can not exectute %S: %s"
5662 conf.selcmd (Printexc.to_string exn);
5663 doclose "pipe/r" r;
5664 doclose "pipe/w" w;
5666 | None -> ()
5667 else loop rest
5669 loop state.layout;
5670 Wsi.setcursor Wsi.CURSOR_INHERIT;
5671 state.mstate <- Mnone;
5675 | _ -> ()
5678 let birdseyemouse button down x y mask
5679 (conf, leftx, _, hooverpageno, anchor) =
5680 match button with
5681 | 1 when down ->
5682 let rec loop = function
5683 | [] -> ()
5684 | l :: rest ->
5685 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5686 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5687 then (
5688 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5690 else loop rest
5692 loop state.layout
5693 | 3 -> ()
5694 | _ -> viewmouse button down x y mask
5697 let mouse button down x y mask =
5698 state.uioh <- state.uioh#button button down x y mask;
5701 let motion ~x ~y =
5702 state.uioh <- state.uioh#motion x y
5705 let pmotion ~x ~y =
5706 state.uioh <- state.uioh#pmotion x y;
5709 let uioh = object
5710 method display = ()
5712 method key key mask =
5713 begin match state.mode with
5714 | Textentry textentry -> textentrykeyboard key mask textentry
5715 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5716 | View -> viewkeyboard key mask
5717 | LinkNav linknav -> linknavkeyboard key mask linknav
5718 end;
5719 state.uioh
5721 method button button bstate x y mask =
5722 begin match state.mode with
5723 | LinkNav _
5724 | View -> viewmouse button bstate x y mask
5725 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5726 | Textentry _ -> ()
5727 end;
5728 state.uioh
5730 method motion x y =
5731 begin match state.mode with
5732 | Textentry _ -> ()
5733 | View | Birdseye _ | LinkNav _ ->
5734 match state.mstate with
5735 | Mzoom _ | Mnone -> ()
5737 | Mpan (x0, y0) ->
5738 let dx = x - x0
5739 and dy = y0 - y in
5740 state.mstate <- Mpan (x, y);
5741 if canpan ()
5742 then state.x <- state.x + dx;
5743 let y = clamp dy in
5744 gotoy_and_clear_text y
5746 | Msel (a, _) ->
5747 state.mstate <- Msel (a, (x, y));
5748 G.postRedisplay "motion select";
5750 | Mscrolly ->
5751 let y = min conf.winh (max 0 y) in
5752 scrolly y
5754 | Mscrollx ->
5755 let x = min conf.winw (max 0 x) in
5756 scrollx x
5758 | Mzoomrect (p0, _) ->
5759 state.mstate <- Mzoomrect (p0, (x, y));
5760 G.postRedisplay "motion zoomrect";
5761 end;
5762 state.uioh
5764 method pmotion x y =
5765 begin match state.mode with
5766 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5767 let rec loop = function
5768 | [] ->
5769 if hooverpageno != -1
5770 then (
5771 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5772 G.postRedisplay "pmotion birdseye no hoover";
5774 | l :: rest ->
5775 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5776 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5777 then (
5778 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5779 G.postRedisplay "pmotion birdseye hoover";
5781 else loop rest
5783 loop state.layout
5785 | Textentry _ -> ()
5787 | LinkNav _
5788 | View ->
5789 match state.mstate with
5790 | Mnone -> updateunder x y
5791 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5793 end;
5794 state.uioh
5796 method infochanged _ = ()
5798 method scrollph =
5799 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5800 let p, h = scrollph state.y maxy in
5801 state.scrollw, p, h
5803 method scrollpw =
5804 let winw = conf.winw - state.scrollw - 1 in
5805 let fwinw = float winw in
5806 let sw =
5807 let sw = fwinw /. float state.w in
5808 let sw = fwinw *. sw in
5809 max sw (float conf.scrollh)
5811 let position, sw =
5812 let f = state.w+winw in
5813 let r = float (winw-state.x) /. float f in
5814 let p = fwinw *. r in
5815 p-.sw/.2., sw
5817 let sw =
5818 if position +. sw > fwinw
5819 then fwinw -. position
5820 else sw
5822 state.hscrollh, position, sw
5824 method modehash =
5825 let modename =
5826 match state.mode with
5827 | LinkNav _ -> "links"
5828 | Textentry _ -> "textentry"
5829 | Birdseye _ -> "birdseye"
5830 | View -> "view"
5832 findkeyhash conf modename
5833 end;;
5835 module Config =
5836 struct
5837 open Parser
5839 let fontpath = ref "";;
5841 module KeyMap =
5842 Map.Make (struct type t = (int * int) let compare = compare end);;
5844 let unent s =
5845 let l = String.length s in
5846 let b = Buffer.create l in
5847 unent b s 0 l;
5848 Buffer.contents b;
5851 let home =
5852 try Sys.getenv "HOME"
5853 with exn ->
5854 prerr_endline
5855 ("Can not determine home directory location: " ^
5856 Printexc.to_string exn);
5860 let modifier_of_string = function
5861 | "alt" -> Wsi.altmask
5862 | "shift" -> Wsi.shiftmask
5863 | "ctrl" | "control" -> Wsi.ctrlmask
5864 | "meta" -> Wsi.metamask
5865 | _ -> 0
5868 let key_of_string =
5869 let r = Str.regexp "-" in
5870 fun s ->
5871 let elems = Str.full_split r s in
5872 let f n k m =
5873 let g s =
5874 let m1 = modifier_of_string s in
5875 if m1 = 0
5876 then (Wsi.namekey s, m)
5877 else (k, m lor m1)
5878 in function
5879 | Str.Delim s when n land 1 = 0 -> g s
5880 | Str.Text s -> g s
5881 | Str.Delim _ -> (k, m)
5883 let rec loop n k m = function
5884 | [] -> (k, m)
5885 | x :: xs ->
5886 let k, m = f n k m x in
5887 loop (n+1) k m xs
5889 loop 0 0 0 elems
5892 let keys_of_string =
5893 let r = Str.regexp "[ \t]" in
5894 fun s ->
5895 let elems = Str.split r s in
5896 List.map key_of_string elems
5899 let copykeyhashes c =
5900 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5903 let config_of c attrs =
5904 let apply c k v =
5906 match k with
5907 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5908 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5909 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5910 | "preload" -> { c with preload = bool_of_string v }
5911 | "page-bias" -> { c with pagebias = int_of_string v }
5912 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5913 | "auto-scroll-step" ->
5914 { c with autoscrollstep = max 0 (int_of_string v) }
5915 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5916 | "crop-hack" -> { c with crophack = bool_of_string v }
5917 | "throttle" ->
5918 let mw =
5919 match String.lowercase v with
5920 | "true" -> Some infinity
5921 | "false" -> None
5922 | f -> Some (float_of_string f)
5924 { c with maxwait = mw}
5925 | "highlight-links" -> { c with hlinks = bool_of_string v }
5926 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5927 | "vertical-margin" ->
5928 { c with interpagespace = max 0 (int_of_string v) }
5929 | "zoom" ->
5930 let zoom = float_of_string v /. 100. in
5931 let zoom = max zoom 0.0 in
5932 { c with zoom = zoom }
5933 | "presentation" -> { c with presentation = bool_of_string v }
5934 | "rotation-angle" -> { c with angle = int_of_string v }
5935 | "width" -> { c with winw = max 20 (int_of_string v) }
5936 | "height" -> { c with winh = max 20 (int_of_string v) }
5937 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5938 | "proportional-display" -> { c with proportional = bool_of_string v }
5939 | "pixmap-cache-size" ->
5940 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5941 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5942 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5943 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5944 | "persistent-location" -> { c with jumpback = bool_of_string v }
5945 | "background-color" -> { c with bgcolor = color_of_string v }
5946 | "scrollbar-in-presentation" ->
5947 { c with scrollbarinpm = bool_of_string v }
5948 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5949 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5950 | "mupdf-store-size" ->
5951 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5952 | "checkers" -> { c with checkers = bool_of_string v }
5953 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5954 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5955 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5956 | "uri-launcher" -> { c with urilauncher = unent v }
5957 | "path-launcher" -> { c with pathlauncher = unent v }
5958 | "color-space" -> { c with colorspace = colorspace_of_string v }
5959 | "invert-colors" -> { c with invert = bool_of_string v }
5960 | "brightness" -> { c with colorscale = float_of_string v }
5961 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5962 | "ghyllscroll" ->
5963 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5964 | "columns" ->
5965 let (n, _, _) as nab = multicolumns_of_string v in
5966 if n < 0
5967 then { c with columns = Csplit (-n, [||]) }
5968 else { c with columns = Cmulti (nab, [||]) }
5969 | "birds-eye-columns" ->
5970 { c with beyecolumns = Some (max (int_of_string v) 2) }
5971 | "selection-command" -> { c with selcmd = unent v }
5972 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5973 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5974 | "full-split" -> { c with fullsplit = bool_of_string v }
5975 | _ -> c
5976 with exn ->
5977 prerr_endline ("Error processing attribute (`" ^
5978 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5981 let rec fold c = function
5982 | [] -> c
5983 | (k, v) :: rest ->
5984 let c = apply c k v in
5985 fold c rest
5987 fold { c with keyhashes = copykeyhashes c } attrs;
5990 let fromstring f pos n v d =
5991 try f v
5992 with exn ->
5993 dolog "Error processing attribute (%S=%S) at %d\n%s"
5994 n v pos (Printexc.to_string exn)
5999 let bookmark_of attrs =
6000 let rec fold title page rely = function
6001 | ("title", v) :: rest -> fold v page rely rest
6002 | ("page", v) :: rest -> fold title v rely rest
6003 | ("rely", v) :: rest -> fold title page v rest
6004 | _ :: rest -> fold title page rely rest
6005 | [] -> title, page, rely
6007 fold "invalid" "0" "0" attrs
6010 let doc_of attrs =
6011 let rec fold path page rely pan = function
6012 | ("path", v) :: rest -> fold v page rely pan rest
6013 | ("page", v) :: rest -> fold path v rely pan rest
6014 | ("rely", v) :: rest -> fold path page v pan rest
6015 | ("pan", v) :: rest -> fold path page rely v rest
6016 | _ :: rest -> fold path page rely pan rest
6017 | [] -> path, page, rely, pan
6019 fold "" "0" "0" "0" attrs
6022 let map_of attrs =
6023 let rec fold rs ls = function
6024 | ("out", v) :: rest -> fold v ls rest
6025 | ("in", v) :: rest -> fold rs v rest
6026 | _ :: rest -> fold ls rs rest
6027 | [] -> ls, rs
6029 fold "" "" attrs
6032 let setconf dst src =
6033 dst.scrollbw <- src.scrollbw;
6034 dst.scrollh <- src.scrollh;
6035 dst.icase <- src.icase;
6036 dst.preload <- src.preload;
6037 dst.pagebias <- src.pagebias;
6038 dst.verbose <- src.verbose;
6039 dst.scrollstep <- src.scrollstep;
6040 dst.maxhfit <- src.maxhfit;
6041 dst.crophack <- src.crophack;
6042 dst.autoscrollstep <- src.autoscrollstep;
6043 dst.maxwait <- src.maxwait;
6044 dst.hlinks <- src.hlinks;
6045 dst.underinfo <- src.underinfo;
6046 dst.interpagespace <- src.interpagespace;
6047 dst.zoom <- src.zoom;
6048 dst.presentation <- src.presentation;
6049 dst.angle <- src.angle;
6050 dst.winw <- src.winw;
6051 dst.winh <- src.winh;
6052 dst.savebmarks <- src.savebmarks;
6053 dst.memlimit <- src.memlimit;
6054 dst.proportional <- src.proportional;
6055 dst.texcount <- src.texcount;
6056 dst.sliceheight <- src.sliceheight;
6057 dst.thumbw <- src.thumbw;
6058 dst.jumpback <- src.jumpback;
6059 dst.bgcolor <- src.bgcolor;
6060 dst.scrollbarinpm <- src.scrollbarinpm;
6061 dst.tilew <- src.tilew;
6062 dst.tileh <- src.tileh;
6063 dst.mustoresize <- src.mustoresize;
6064 dst.checkers <- src.checkers;
6065 dst.aalevel <- src.aalevel;
6066 dst.trimmargins <- src.trimmargins;
6067 dst.trimfuzz <- src.trimfuzz;
6068 dst.urilauncher <- src.urilauncher;
6069 dst.colorspace <- src.colorspace;
6070 dst.invert <- src.invert;
6071 dst.colorscale <- src.colorscale;
6072 dst.redirectstderr <- src.redirectstderr;
6073 dst.ghyllscroll <- src.ghyllscroll;
6074 dst.columns <- src.columns;
6075 dst.beyecolumns <- src.beyecolumns;
6076 dst.selcmd <- src.selcmd;
6077 dst.updatecurs <- src.updatecurs;
6078 dst.pathlauncher <- src.pathlauncher;
6079 dst.keyhashes <- copykeyhashes src;
6080 dst.hfsize <- src.hfsize;
6081 dst.fullsplit <- src.fullsplit;
6084 let get s =
6085 let h = Hashtbl.create 10 in
6086 let dc = { defconf with angle = defconf.angle } in
6087 let rec toplevel v t spos _ =
6088 match t with
6089 | Vdata | Vcdata | Vend -> v
6090 | Vopen ("llppconfig", _, closed) ->
6091 if closed
6092 then v
6093 else { v with f = llppconfig }
6094 | Vopen _ ->
6095 error "unexpected subelement at top level" s spos
6096 | Vclose _ -> error "unexpected close at top level" s spos
6098 and llppconfig v t spos _ =
6099 match t with
6100 | Vdata | Vcdata -> v
6101 | Vend -> error "unexpected end of input in llppconfig" s spos
6102 | Vopen ("defaults", attrs, closed) ->
6103 let c = config_of dc attrs in
6104 setconf dc c;
6105 if closed
6106 then v
6107 else { v with f = defaults }
6109 | Vopen ("ui-font", attrs, closed) ->
6110 let rec getsize size = function
6111 | [] -> size
6112 | ("size", v) :: rest ->
6113 let size =
6114 fromstring int_of_string spos "size" v fstate.fontsize in
6115 getsize size rest
6116 | l -> getsize size l
6118 fstate.fontsize <- getsize fstate.fontsize attrs;
6119 if closed
6120 then v
6121 else { v with f = uifont (Buffer.create 10) }
6123 | Vopen ("doc", attrs, closed) ->
6124 let pathent, spage, srely, span = doc_of attrs in
6125 let path = unent pathent
6126 and pageno = fromstring int_of_string spos "page" spage 0
6127 and rely = fromstring float_of_string spos "rely" srely 0.0
6128 and pan = fromstring int_of_string spos "pan" span 0 in
6129 let c = config_of dc attrs in
6130 let anchor = (pageno, rely) in
6131 if closed
6132 then (Hashtbl.add h path (c, [], pan, anchor); v)
6133 else { v with f = doc path pan anchor c [] }
6135 | Vopen _ ->
6136 error "unexpected subelement in llppconfig" s spos
6138 | Vclose "llppconfig" -> { v with f = toplevel }
6139 | Vclose _ -> error "unexpected close in llppconfig" s spos
6141 and defaults v t spos _ =
6142 match t with
6143 | Vdata | Vcdata -> v
6144 | Vend -> error "unexpected end of input in defaults" s spos
6145 | Vopen ("keymap", attrs, closed) ->
6146 let modename =
6147 try List.assoc "mode" attrs
6148 with Not_found -> "global" in
6149 if closed
6150 then v
6151 else
6152 let ret keymap =
6153 let h = findkeyhash dc modename in
6154 KeyMap.iter (Hashtbl.replace h) keymap;
6155 defaults
6157 { v with f = pkeymap ret KeyMap.empty }
6159 | Vopen (_, _, _) ->
6160 error "unexpected subelement in defaults" s spos
6162 | Vclose "defaults" ->
6163 { v with f = llppconfig }
6165 | Vclose _ -> error "unexpected close in defaults" s spos
6167 and uifont b v t spos epos =
6168 match t with
6169 | Vdata | Vcdata ->
6170 Buffer.add_substring b s spos (epos - spos);
6172 | Vopen (_, _, _) ->
6173 error "unexpected subelement in ui-font" s spos
6174 | Vclose "ui-font" ->
6175 if String.length !fontpath = 0
6176 then fontpath := Buffer.contents b;
6177 { v with f = llppconfig }
6178 | Vclose _ -> error "unexpected close in ui-font" s spos
6179 | Vend -> error "unexpected end of input in ui-font" s spos
6181 and doc path pan anchor c bookmarks v t spos _ =
6182 match t with
6183 | Vdata | Vcdata -> v
6184 | Vend -> error "unexpected end of input in doc" s spos
6185 | Vopen ("bookmarks", _, closed) ->
6186 if closed
6187 then v
6188 else { v with f = pbookmarks path pan anchor c bookmarks }
6190 | Vopen ("keymap", attrs, closed) ->
6191 let modename =
6192 try List.assoc "mode" attrs
6193 with Not_found -> "global"
6195 if closed
6196 then v
6197 else
6198 let ret keymap =
6199 let h = findkeyhash c modename in
6200 KeyMap.iter (Hashtbl.replace h) keymap;
6201 doc path pan anchor c bookmarks
6203 { v with f = pkeymap ret KeyMap.empty }
6205 | Vopen (_, _, _) ->
6206 error "unexpected subelement in doc" s spos
6208 | Vclose "doc" ->
6209 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6210 { v with f = llppconfig }
6212 | Vclose _ -> error "unexpected close in doc" s spos
6214 and pkeymap ret keymap v t spos _ =
6215 match t with
6216 | Vdata | Vcdata -> v
6217 | Vend -> error "unexpected end of input in keymap" s spos
6218 | Vopen ("map", attrs, closed) ->
6219 let r, l = map_of attrs in
6220 let kss = fromstring keys_of_string spos "in" r [] in
6221 let lss = fromstring keys_of_string spos "out" l [] in
6222 let keymap =
6223 match kss with
6224 | [] -> keymap
6225 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6226 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6228 if closed
6229 then { v with f = pkeymap ret keymap }
6230 else
6231 let f () = v in
6232 { v with f = skip "map" f }
6234 | Vopen _ ->
6235 error "unexpected subelement in keymap" s spos
6237 | Vclose "keymap" ->
6238 { v with f = ret keymap }
6240 | Vclose _ -> error "unexpected close in keymap" s spos
6242 and pbookmarks path pan anchor c bookmarks v t spos _ =
6243 match t with
6244 | Vdata | Vcdata -> v
6245 | Vend -> error "unexpected end of input in bookmarks" s spos
6246 | Vopen ("item", attrs, closed) ->
6247 let titleent, spage, srely = bookmark_of attrs in
6248 let page = fromstring int_of_string spos "page" spage 0
6249 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6250 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6251 if closed
6252 then { v with f = pbookmarks path pan anchor c bookmarks }
6253 else
6254 let f () = v in
6255 { v with f = skip "item" f }
6257 | Vopen _ ->
6258 error "unexpected subelement in bookmarks" s spos
6260 | Vclose "bookmarks" ->
6261 { v with f = doc path pan anchor c bookmarks }
6263 | Vclose _ -> error "unexpected close in bookmarks" s spos
6265 and skip tag f v t spos _ =
6266 match t with
6267 | Vdata | Vcdata -> v
6268 | Vend ->
6269 error ("unexpected end of input in skipped " ^ tag) s spos
6270 | Vopen (tag', _, closed) ->
6271 if closed
6272 then v
6273 else
6274 let f' () = { v with f = skip tag f } in
6275 { v with f = skip tag' f' }
6276 | Vclose ctag ->
6277 if tag = ctag
6278 then f ()
6279 else error ("unexpected close in skipped " ^ tag) s spos
6282 parse { f = toplevel; accu = () } s;
6283 h, dc;
6286 let do_load f ic =
6288 let len = in_channel_length ic in
6289 let s = String.create len in
6290 really_input ic s 0 len;
6291 f s;
6292 with
6293 | Parse_error (msg, s, pos) ->
6294 let subs = subs s pos in
6295 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6296 failwith ("parse error: " ^ s)
6298 | exn ->
6299 failwith ("config load error: " ^ Printexc.to_string exn)
6302 let defconfpath =
6303 let dir =
6305 let dir = Filename.concat home ".config" in
6306 if Sys.is_directory dir then dir else home
6307 with _ -> home
6309 Filename.concat dir "llpp.conf"
6312 let confpath = ref defconfpath;;
6314 let load1 f =
6315 if Sys.file_exists !confpath
6316 then
6317 match
6318 (try Some (open_in_bin !confpath)
6319 with exn ->
6320 prerr_endline
6321 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6322 Printexc.to_string exn);
6323 None
6325 with
6326 | Some ic ->
6327 begin try
6328 f (do_load get ic)
6329 with exn ->
6330 prerr_endline
6331 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6332 Printexc.to_string exn);
6333 end;
6334 close_in ic;
6336 | None -> ()
6337 else
6338 f (Hashtbl.create 0, defconf)
6341 let load () =
6342 let f (h, dc) =
6343 let pc, pb, px, pa =
6345 Hashtbl.find h (Filename.basename state.path)
6346 with Not_found -> dc, [], 0, (0, 0.0)
6348 setconf defconf dc;
6349 setconf conf pc;
6350 state.bookmarks <- pb;
6351 state.x <- px;
6352 state.scrollw <- conf.scrollbw;
6353 if conf.jumpback
6354 then state.anchor <- pa;
6355 cbput state.hists.nav pa;
6357 load1 f
6360 let add_attrs bb always dc c =
6361 let ob s a b =
6362 if always || a != b
6363 then Printf.bprintf bb "\n %s='%b'" s a
6364 and oi s a b =
6365 if always || a != b
6366 then Printf.bprintf bb "\n %s='%d'" s a
6367 and oI s a b =
6368 if always || a != b
6369 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6370 and oz s a b =
6371 if always || a <> b
6372 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6373 and oF s a b =
6374 if always || a <> b
6375 then Printf.bprintf bb "\n %s='%f'" s a
6376 and oc s a b =
6377 if always || a <> b
6378 then
6379 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6380 and oC s a b =
6381 if always || a <> b
6382 then
6383 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6384 and oR s a b =
6385 if always || a <> b
6386 then
6387 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6388 and os s a b =
6389 if always || a <> b
6390 then
6391 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6392 and og s a b =
6393 if always || a <> b
6394 then
6395 match a with
6396 | None -> ()
6397 | Some (_N, _A, _B) ->
6398 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6399 and oW s a b =
6400 if always || a <> b
6401 then
6402 let v =
6403 match a with
6404 | None -> "false"
6405 | Some f ->
6406 if f = infinity
6407 then "true"
6408 else string_of_float f
6410 Printf.bprintf bb "\n %s='%s'" s v
6411 and oco s a b =
6412 if always || a <> b
6413 then
6414 match a with
6415 | Cmulti ((n, a, b), _) when n > 1 ->
6416 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6417 | Csplit (n, _) when n > 1 ->
6418 Printf.bprintf bb "\n %s='%d'" s ~-n
6419 | _ -> ()
6420 and obeco s a b =
6421 if always || a <> b
6422 then
6423 match a with
6424 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6425 | _ -> ()
6427 let w, h =
6428 if always
6429 then dc.winw, dc.winh
6430 else
6431 match state.fullscreen with
6432 | Some wh -> wh
6433 | None -> c.winw, c.winh
6435 let zoom, presentation, interpagespace, maxwait =
6436 if always
6437 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6438 else
6439 match state.mode with
6440 | Birdseye (bc, _, _, _, _) ->
6441 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6442 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6444 oi "width" w dc.winw;
6445 oi "height" h dc.winh;
6446 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6447 oi "scroll-handle-height" c.scrollh dc.scrollh;
6448 ob "case-insensitive-search" c.icase dc.icase;
6449 ob "preload" c.preload dc.preload;
6450 oi "page-bias" c.pagebias dc.pagebias;
6451 oi "scroll-step" c.scrollstep dc.scrollstep;
6452 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6453 ob "max-height-fit" c.maxhfit dc.maxhfit;
6454 ob "crop-hack" c.crophack dc.crophack;
6455 oW "throttle" maxwait dc.maxwait;
6456 ob "highlight-links" c.hlinks dc.hlinks;
6457 ob "under-cursor-info" c.underinfo dc.underinfo;
6458 oi "vertical-margin" interpagespace dc.interpagespace;
6459 oz "zoom" zoom dc.zoom;
6460 ob "presentation" presentation dc.presentation;
6461 oi "rotation-angle" c.angle dc.angle;
6462 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6463 ob "proportional-display" c.proportional dc.proportional;
6464 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6465 oi "tex-count" c.texcount dc.texcount;
6466 oi "slice-height" c.sliceheight dc.sliceheight;
6467 oi "thumbnail-width" c.thumbw dc.thumbw;
6468 ob "persistent-location" c.jumpback dc.jumpback;
6469 oc "background-color" c.bgcolor dc.bgcolor;
6470 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6471 oi "tile-width" c.tilew dc.tilew;
6472 oi "tile-height" c.tileh dc.tileh;
6473 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6474 ob "checkers" c.checkers dc.checkers;
6475 oi "aalevel" c.aalevel dc.aalevel;
6476 ob "trim-margins" c.trimmargins dc.trimmargins;
6477 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6478 os "uri-launcher" c.urilauncher dc.urilauncher;
6479 os "path-launcher" c.pathlauncher dc.pathlauncher;
6480 oC "color-space" c.colorspace dc.colorspace;
6481 ob "invert-colors" c.invert dc.invert;
6482 oF "brightness" c.colorscale dc.colorscale;
6483 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6484 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6485 oco "columns" c.columns dc.columns;
6486 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6487 os "selection-command" c.selcmd dc.selcmd;
6488 ob "update-cursor" c.updatecurs dc.updatecurs;
6489 oi "hint-font-size" c.hfsize dc.hfsize;
6490 ob "full-split" c.fullsplit dc.fullsplit;
6493 let keymapsbuf always dc c =
6494 let bb = Buffer.create 16 in
6495 let rec loop = function
6496 | [] -> ()
6497 | (modename, h) :: rest ->
6498 let dh = findkeyhash dc modename in
6499 if always || h <> dh
6500 then (
6501 if Hashtbl.length h > 0
6502 then (
6503 if Buffer.length bb > 0
6504 then Buffer.add_char bb '\n';
6505 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6506 Hashtbl.iter (fun i o ->
6507 let isdifferent = always ||
6509 let dO = Hashtbl.find dh i in
6510 dO <> o
6511 with Not_found -> true
6513 if isdifferent
6514 then
6515 let addkm (k, m) =
6516 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6517 if Wsi.withalt m then Buffer.add_string bb "alt-";
6518 if Wsi.withshift m then Buffer.add_string bb "shift-";
6519 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6520 Buffer.add_string bb (Wsi.keyname k);
6522 let addkms l =
6523 let rec loop = function
6524 | [] -> ()
6525 | km :: [] -> addkm km
6526 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6528 loop l
6530 Buffer.add_string bb "<map in='";
6531 addkm i;
6532 match o with
6533 | KMinsrt km ->
6534 Buffer.add_string bb "' out='";
6535 addkm km;
6536 Buffer.add_string bb "'/>\n"
6538 | KMinsrl kms ->
6539 Buffer.add_string bb "' out='";
6540 addkms kms;
6541 Buffer.add_string bb "'/>\n"
6543 | KMmulti (ins, kms) ->
6544 Buffer.add_char bb ' ';
6545 addkms ins;
6546 Buffer.add_string bb "' out='";
6547 addkms kms;
6548 Buffer.add_string bb "'/>\n"
6549 ) h;
6550 Buffer.add_string bb "</keymap>";
6553 loop rest
6555 loop c.keyhashes;
6559 let save () =
6560 let uifontsize = fstate.fontsize in
6561 let bb = Buffer.create 32768 in
6562 let f (h, dc) =
6563 let dc = if conf.bedefault then conf else dc in
6564 Buffer.add_string bb "<llppconfig>\n";
6566 if String.length !fontpath > 0
6567 then
6568 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6569 uifontsize
6570 !fontpath
6571 else (
6572 if uifontsize <> 14
6573 then
6574 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6577 Buffer.add_string bb "<defaults ";
6578 add_attrs bb true dc dc;
6579 let kb = keymapsbuf true dc dc in
6580 if Buffer.length kb > 0
6581 then (
6582 Buffer.add_string bb ">\n";
6583 Buffer.add_buffer bb kb;
6584 Buffer.add_string bb "\n</defaults>\n";
6586 else Buffer.add_string bb "/>\n";
6588 let adddoc path pan anchor c bookmarks =
6589 if bookmarks == [] && c = dc && anchor = emptyanchor
6590 then ()
6591 else (
6592 Printf.bprintf bb "<doc path='%s'"
6593 (enent path 0 (String.length path));
6595 if anchor <> emptyanchor
6596 then (
6597 let n, y = anchor in
6598 Printf.bprintf bb " page='%d'" n;
6599 if y > 1e-6
6600 then
6601 Printf.bprintf bb " rely='%f'" y
6605 if pan != 0
6606 then Printf.bprintf bb " pan='%d'" pan;
6608 add_attrs bb false dc c;
6609 let kb = keymapsbuf false dc c in
6611 begin match bookmarks with
6612 | [] ->
6613 if Buffer.length kb > 0
6614 then (
6615 Buffer.add_string bb ">\n";
6616 Buffer.add_buffer bb kb;
6617 Buffer.add_string bb "\n</doc>\n";
6619 else Buffer.add_string bb "/>\n"
6620 | _ ->
6621 Buffer.add_string bb ">\n<bookmarks>\n";
6622 List.iter (fun (title, _level, (page, rely)) ->
6623 Printf.bprintf bb
6624 "<item title='%s' page='%d'"
6625 (enent title 0 (String.length title))
6626 page
6628 if rely > 1e-6
6629 then
6630 Printf.bprintf bb " rely='%f'" rely
6632 Buffer.add_string bb "/>\n";
6633 ) bookmarks;
6634 Buffer.add_string bb "</bookmarks>";
6635 if Buffer.length kb > 0
6636 then (
6637 Buffer.add_string bb "\n";
6638 Buffer.add_buffer bb kb;
6640 Buffer.add_string bb "\n</doc>\n";
6641 end;
6645 let pan, conf =
6646 match state.mode with
6647 | Birdseye (c, pan, _, _, _) ->
6648 let beyecolumns =
6649 match conf.columns with
6650 | Cmulti ((c, _, _), _) -> Some c
6651 | Csingle -> None
6652 | Csplit _ -> None
6653 and columns =
6654 match c.columns with
6655 | Cmulti (c, _) -> Cmulti (c, [||])
6656 | Csingle -> Csingle
6657 | Csplit _ -> failwith "quit from bird's eye while split"
6659 pan, { c with beyecolumns = beyecolumns; columns = columns }
6660 | _ -> state.x, conf
6662 let basename = Filename.basename state.path in
6663 adddoc basename pan (getanchor ())
6664 { conf with
6665 autoscrollstep =
6666 match state.autoscroll with
6667 | Some step -> step
6668 | None -> conf.autoscrollstep }
6669 (if conf.savebmarks then state.bookmarks else []);
6671 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6672 if basename <> path
6673 then adddoc path x y c bookmarks
6674 ) h;
6675 Buffer.add_string bb "</llppconfig>";
6677 load1 f;
6678 if Buffer.length bb > 0
6679 then
6681 let tmp = !confpath ^ ".tmp" in
6682 let oc = open_out_bin tmp in
6683 Buffer.output_buffer oc bb;
6684 close_out oc;
6685 Unix.rename tmp !confpath;
6686 with exn ->
6687 prerr_endline
6688 ("error while saving configuration: " ^ Printexc.to_string exn)
6690 end;;
6692 let () =
6693 Arg.parse
6694 (Arg.align
6695 [("-p", Arg.String (fun s -> state.password <- s) ,
6696 "<password> Set password");
6698 ("-f", Arg.String (fun s -> Config.fontpath := s),
6699 "<path> Set path to the user interface font");
6701 ("-c", Arg.String (fun s -> Config.confpath := s),
6702 "<path> Set path to the configuration file");
6704 ("-v", Arg.Unit (fun () ->
6705 Printf.printf
6706 "%s\nconfiguration path: %s\n"
6707 (version ())
6708 Config.defconfpath
6710 exit 0), " Print version and exit");
6713 (fun s -> state.path <- s)
6714 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6716 if String.length state.path = 0
6717 then (prerr_endline "file name missing"; exit 1);
6719 Config.load ();
6721 let globalkeyhash = findkeyhash conf "global" in
6722 let wsfd, winw, winh = Wsi.init (object
6723 method expose =
6724 if nogeomcmds state.geomcmds || platform == Posx
6725 then display ()
6726 else (
6727 GlFunc.draw_buffer `front;
6728 GlClear.color (scalecolor2 conf.bgcolor);
6729 GlClear.clear [`color];
6730 GlFunc.draw_buffer `back;
6732 method display = display ()
6733 method reshape w h = reshape w h
6734 method mouse b d x y m = mouse b d x y m
6735 method motion x y = state.mpos <- (x, y); motion x y
6736 method pmotion x y = state.mpos <- (x, y); pmotion x y
6737 method key k m =
6738 let mascm = m land (
6739 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6740 ) in
6741 match state.keystate with
6742 | KSnone ->
6743 let km = k, mascm in
6744 begin
6745 match
6746 let modehash = state.uioh#modehash in
6747 try Hashtbl.find modehash km
6748 with Not_found ->
6749 try Hashtbl.find globalkeyhash km
6750 with Not_found -> KMinsrt (k, m)
6751 with
6752 | KMinsrt (k, m) -> keyboard k m
6753 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6754 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6756 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6757 List.iter (fun (k, m) -> keyboard k m) insrt;
6758 state.keystate <- KSnone
6759 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6760 state.keystate <- KSinto (keys, insrt)
6761 | _ ->
6762 state.keystate <- KSnone
6764 method enter x y = state.mpos <- (x, y); pmotion x y
6765 method leave = state.mpos <- (-1, -1)
6766 method quit = raise Quit
6767 end) conf.winw conf.winh (platform = Posx) in
6769 state.wsfd <- wsfd;
6771 if not (
6772 List.exists GlMisc.check_extension
6773 [ "GL_ARB_texture_rectangle"
6774 ; "GL_EXT_texture_recangle"
6775 ; "GL_NV_texture_rectangle" ]
6777 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6779 let cr, sw =
6780 match Ne.pipe () with
6781 | Ne.Exn exn ->
6782 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6783 exit 1
6784 | Ne.Res rw -> rw
6785 and sr, cw =
6786 match Ne.pipe () with
6787 | Ne.Exn exn ->
6788 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6789 exit 1
6790 | Ne.Res rw -> rw
6793 cloexec cr;
6794 cloexec sw;
6795 cloexec sr;
6796 cloexec cw;
6798 setcheckers conf.checkers;
6799 redirectstderr ();
6801 init (cr, cw) (
6802 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6803 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6804 !Config.fontpath
6806 state.sr <- sr;
6807 state.sw <- sw;
6808 state.text <- "Opening " ^ state.path;
6809 reshape winw winh;
6810 opendoc state.path state.password;
6811 state.uioh <- uioh;
6813 let rec loop deadline =
6814 let r =
6815 match state.errfd with
6816 | None -> [state.sr; state.wsfd]
6817 | Some fd -> [state.sr; state.wsfd; fd]
6819 if state.redisplay
6820 then (
6821 state.redisplay <- false;
6822 display ();
6824 let timeout =
6825 let now = now () in
6826 if deadline > now
6827 then (
6828 if deadline = infinity
6829 then ~-.1.0
6830 else max 0.0 (deadline -. now)
6832 else 0.0
6834 let r, _, _ =
6835 try Unix.select r [] [] timeout
6836 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6838 begin match r with
6839 | [] ->
6840 state.ghyll None;
6841 let newdeadline =
6842 if state.ghyll == noghyll
6843 then
6844 match state.autoscroll with
6845 | Some step when step != 0 ->
6846 let y = state.y + step in
6847 let y =
6848 if y < 0
6849 then state.maxy
6850 else if y >= state.maxy then 0 else y
6852 gotoy y;
6853 if state.mode = View
6854 then state.text <- "";
6855 deadline +. 0.01
6856 | _ -> infinity
6857 else deadline +. 0.01
6859 loop newdeadline
6861 | l ->
6862 let rec checkfds = function
6863 | [] -> ()
6864 | fd :: rest when fd = state.sr ->
6865 let cmd = readcmd state.sr in
6866 act cmd;
6867 checkfds rest
6869 | fd :: rest when fd = state.wsfd ->
6870 Wsi.readresp fd;
6871 checkfds rest
6873 | fd :: rest ->
6874 let s = String.create 80 in
6875 let n = Unix.read fd s 0 80 in
6876 if conf.redirectstderr
6877 then (
6878 Buffer.add_substring state.errmsgs s 0 n;
6879 state.newerrmsgs <- true;
6880 state.redisplay <- true;
6882 else (
6883 prerr_string (String.sub s 0 n);
6884 flush stderr;
6886 checkfds rest
6888 checkfds l;
6889 let newdeadline =
6890 let deadline1 =
6891 if deadline = infinity
6892 then now () +. 0.01
6893 else deadline
6895 match state.autoscroll with
6896 | Some step when step != 0 -> deadline1
6897 | _ -> if state.ghyll == noghyll then infinity else deadline1
6899 loop newdeadline
6900 end;
6903 loop infinity;
6904 with Quit ->
6905 Config.save ();
6906 exit 0;