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