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