Another fullsplit tweak
[llpp.git] / main.ml
blob0ec9f8a72c43c889a27fed94fc8a6ee45ed881c1
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of int
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess :
87 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
98 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
100 let platform_to_string = function
101 | Punknown -> "unknown"
102 | Plinux -> "Linux"
103 | Posx -> "OSX"
104 | Psun -> "Sun"
105 | Pfreebsd -> "FreeBSD"
106 | Pdragonflybsd -> "DragonflyBSD"
107 | Popenbsd -> "OpenBSD"
108 | Pnetbsd -> "NetBSD"
109 | Pcygwin -> "Cygwin"
112 let platform = platform ();;
114 let popen cmd fda =
115 if platform = Pcygwin
116 then (
117 let sh = "/bin/sh" in
118 let args = [|sh; "-c"; cmd|] in
119 let rec std si so se = function
120 | [] -> si, so, se
121 | (fd, 0) :: rest -> std fd so se rest
122 | (fd, -1) :: rest ->
123 Unix.set_close_on_exec fd;
124 std si so se rest
125 | (_, n) :: _ ->
126 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
128 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
129 ignore (Unix.create_process sh args si so se)
131 else popen cmd fda;
134 type x = int
135 and y = int
136 and tilex = int
137 and tiley = int
138 and tileparams = (x * y * width * height * tilex * tiley)
141 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
143 type mpos = int * int
144 and mstate =
145 | Msel of (mpos * mpos)
146 | Mpan of mpos
147 | Mscrolly | Mscrollx
148 | Mzoom of (int * int)
149 | Mzoomrect of (mpos * mpos)
150 | Mnone
153 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
154 and onkey = string -> int -> te
155 and ondone = string -> unit
156 and histcancel = unit -> unit
157 and onhist = ((histcmd -> string) * histcancel)
158 and histcmd = HCnext | HCprev | HCfirst | HClast
159 and cancelonempty = bool
160 and te =
161 | TEstop
162 | TEdone of string
163 | TEcont of string
164 | TEswitch of textentry
167 type 'a circbuf =
168 { store : 'a array
169 ; mutable rc : int
170 ; mutable wc : int
171 ; mutable len : int
175 let bound v minv maxv =
176 max minv (min maxv v);
179 let cbnew n v =
180 { store = Array.create n v
181 ; rc = 0
182 ; wc = 0
183 ; len = 0
187 let drawstring size x y s =
188 Gl.enable `blend;
189 Gl.enable `texture_2d;
190 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
191 ignore (drawstr size x y s);
192 Gl.disable `blend;
193 Gl.disable `texture_2d;
196 let drawstring1 size x y s =
197 drawstr size x y s;
200 let drawstring2 size x y fmt =
201 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
204 let cbcap b = Array.length b.store;;
206 let cbput b v =
207 let cap = cbcap b in
208 b.store.(b.wc) <- v;
209 b.wc <- (b.wc + 1) mod cap;
210 b.rc <- b.wc;
211 b.len <- min (b.len + 1) cap;
214 let cbempty b = b.len = 0;;
216 let cbgetg b circular dir =
217 if cbempty b
218 then b.store.(0)
219 else
220 let rc = b.rc + dir in
221 let rc =
222 if circular
223 then (
224 if rc = -1
225 then b.len-1
226 else (
227 if rc = b.len
228 then 0
229 else rc
232 else max 0 (min rc (b.len-1))
234 b.rc <- rc;
235 b.store.(rc);
238 let cbget b = cbgetg b false;;
239 let cbgetc b = cbgetg b true;;
241 type page =
242 { pageno : int
243 ; pagedimno : int
244 ; pagew : int
245 ; pageh : int
246 ; pagex : int
247 ; pagey : int
248 ; pagevw : int
249 ; pagevh : int
250 ; pagedispx : int
251 ; pagedispy : int
252 ; pagecol : int
256 let debugl l =
257 dolog "l %d dim=%d {" l.pageno l.pagedimno;
258 dolog " WxH %dx%d" l.pagew l.pageh;
259 dolog " vWxH %dx%d" l.pagevw l.pagevh;
260 dolog " pagex,y %d,%d" l.pagex l.pagey;
261 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
262 dolog " column %d" l.pagecol;
263 dolog "}";
266 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
267 dolog "rect {";
268 dolog " x0,y0=(% f, % f)" x0 y0;
269 dolog " x1,y1=(% f, % f)" x1 y1;
270 dolog " x2,y2=(% f, % f)" x2 y2;
271 dolog " x3,y3=(% f, % f)" x3 y3;
272 dolog "}";
275 type multicolumns = multicol * pagegeom
276 and splitcolumns = columncount * pagegeom
277 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
278 and multicol = columncount * covercount * covercount
279 and pdimno = int
280 and columncount = int
281 and covercount = int;;
283 type conf =
284 { mutable scrollbw : int
285 ; mutable scrollh : int
286 ; mutable icase : bool
287 ; mutable preload : bool
288 ; mutable pagebias : int
289 ; mutable verbose : bool
290 ; mutable debug : bool
291 ; mutable scrollstep : int
292 ; mutable maxhfit : bool
293 ; mutable crophack : bool
294 ; mutable autoscrollstep : int
295 ; mutable maxwait : float option
296 ; mutable hlinks : bool
297 ; mutable underinfo : bool
298 ; mutable interpagespace : interpagespace
299 ; mutable zoom : float
300 ; mutable presentation : bool
301 ; mutable angle : angle
302 ; mutable winw : int
303 ; mutable winh : int
304 ; mutable savebmarks : bool
305 ; mutable proportional : proportional
306 ; mutable trimmargins : trimmargins
307 ; mutable trimfuzz : irect
308 ; mutable memlimit : memsize
309 ; mutable texcount : texcount
310 ; mutable sliceheight : sliceheight
311 ; mutable thumbw : width
312 ; mutable jumpback : bool
313 ; mutable bgcolor : float * float * float
314 ; mutable bedefault : bool
315 ; mutable scrollbarinpm : bool
316 ; mutable tilew : int
317 ; mutable tileh : int
318 ; mutable mustoresize : memsize
319 ; mutable checkers : bool
320 ; mutable aalevel : int
321 ; mutable urilauncher : string
322 ; mutable pathlauncher : string
323 ; mutable colorspace : colorspace
324 ; mutable invert : bool
325 ; mutable colorscale : float
326 ; mutable redirectstderr : bool
327 ; mutable ghyllscroll : (int * int * int) option
328 ; mutable columns : columns
329 ; mutable beyecolumns : columncount option
330 ; mutable selcmd : string
331 ; mutable updatecurs : bool
332 ; mutable keyhashes : (string * keyhash) list
333 ; mutable hfsize : int
334 ; mutable fullsplit : bool
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
464 and hists =
465 { pat : string circbuf
466 ; pag : string circbuf
467 ; nav : anchor circbuf
468 ; sel : string circbuf
472 let defconf =
473 { scrollbw = 7
474 ; scrollh = 12
475 ; icase = true
476 ; preload = true
477 ; pagebias = 0
478 ; verbose = false
479 ; debug = false
480 ; scrollstep = 24
481 ; maxhfit = true
482 ; crophack = false
483 ; autoscrollstep = 2
484 ; maxwait = None
485 ; hlinks = false
486 ; underinfo = false
487 ; interpagespace = 2
488 ; zoom = 1.0
489 ; presentation = false
490 ; angle = 0
491 ; winw = 900
492 ; winh = 900
493 ; savebmarks = true
494 ; proportional = true
495 ; trimmargins = false
496 ; trimfuzz = (0,0,0,0)
497 ; memlimit = 32 lsl 20
498 ; texcount = 256
499 ; sliceheight = 24
500 ; thumbw = 76
501 ; jumpback = true
502 ; bgcolor = (0.5, 0.5, 0.5)
503 ; bedefault = false
504 ; scrollbarinpm = true
505 ; tilew = 2048
506 ; tileh = 2048
507 ; mustoresize = 256 lsl 20
508 ; checkers = true
509 ; aalevel = 8
510 ; urilauncher =
511 (match platform with
512 | Plinux | Pfreebsd | Pdragonflybsd
513 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
514 | Posx -> "open \"%s\""
515 | Pcygwin -> "cygstart \"%s\""
516 | Punknown -> "echo %s")
517 ; pathlauncher = "lp \"%s\""
518 ; selcmd =
519 (match platform with
520 | Plinux | Pfreebsd | Pdragonflybsd
521 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
522 | Posx -> "pbcopy"
523 | Pcygwin -> "wsel"
524 | Punknown -> "cat")
525 ; colorspace = Rgb
526 ; invert = false
527 ; colorscale = 1.0
528 ; redirectstderr = false
529 ; ghyllscroll = None
530 ; columns = Csingle
531 ; beyecolumns = None
532 ; updatecurs = false
533 ; hfsize = 12
534 ; fullsplit = false
535 ; keyhashes =
536 let mk n = (n, Hashtbl.create 1) in
537 [ mk "global"
538 ; mk "info"
539 ; mk "help"
540 ; mk "outline"
541 ; mk "listview"
542 ; mk "birdseye"
543 ; mk "textentry"
544 ; mk "links"
545 ; mk "view"
550 let findkeyhash c name =
551 try List.assoc name c.keyhashes
552 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
555 let conf = { defconf with angle = defconf.angle };;
557 type fontstate =
558 { mutable fontsize : int
559 ; mutable wwidth : float
560 ; mutable maxrows : int
564 let fstate =
565 { fontsize = 14
566 ; wwidth = nan
567 ; maxrows = -1
571 let setfontsize n =
572 fstate.fontsize <- n;
573 fstate.wwidth <- measurestr fstate.fontsize "w";
574 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
577 let geturl s =
578 let colonpos = try String.index s ':' with Not_found -> -1 in
579 let len = String.length s in
580 if colonpos >= 0 && colonpos + 3 < len
581 then (
582 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
583 then
584 let schemestartpos =
585 try String.rindex_from s colonpos ' '
586 with Not_found -> -1
588 let scheme =
589 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
591 match scheme with
592 | "http" | "ftp" | "mailto" ->
593 let epos =
594 try String.index_from s colonpos ' '
595 with Not_found -> len
597 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
598 | _ -> ""
599 else ""
601 else ""
604 let gotouri uri =
605 if String.length conf.urilauncher = 0
606 then print_endline uri
607 else (
608 let url = geturl uri in
609 if String.length url = 0
610 then print_endline uri
611 else
612 let re = Str.regexp "%s" in
613 let command = Str.global_replace re url conf.urilauncher in
614 try popen command []
615 with exn ->
616 Printf.eprintf
617 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
618 flush stderr;
622 let version () =
623 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
624 (platform_to_string platform) Sys.word_size Sys.ocaml_version
627 let makehelp () =
628 let strings = version () :: "" :: Help.keys in
629 Array.of_list (
630 List.map (fun s ->
631 let url = geturl s in
632 if String.length url > 0
633 then (s, 0, Action (fun u -> gotouri url; u))
634 else (s, 0, Noaction)
635 ) strings);
638 let noghyll _ = ();;
639 let firstgeomcmds = "", [];;
641 let state =
642 { sr = Unix.stdin
643 ; sw = Unix.stdin
644 ; wsfd = Unix.stdin
645 ; errfd = None
646 ; stderr = Unix.stderr
647 ; errmsgs = Buffer.create 0
648 ; newerrmsgs = false
649 ; x = 0
650 ; y = 0
651 ; w = 0
652 ; scrollw = 0
653 ; hscrollh = 0
654 ; anchor = emptyanchor
655 ; ranchors = []
656 ; layout = []
657 ; maxy = max_int
658 ; tilelru = Queue.create ()
659 ; pagemap = Hashtbl.create 10
660 ; tilemap = Hashtbl.create 10
661 ; pdims = []
662 ; pagecount = 0
663 ; currently = Idle
664 ; mstate = Mnone
665 ; rects = []
666 ; rects1 = []
667 ; text = ""
668 ; mode = View
669 ; fullscreen = None
670 ; searchpattern = ""
671 ; outlines = [||]
672 ; bookmarks = []
673 ; path = ""
674 ; password = ""
675 ; geomcmds = firstgeomcmds
676 ; hists =
677 { nav = cbnew 10 (0, 0.0)
678 ; pat = cbnew 10 ""
679 ; pag = cbnew 10 ""
680 ; sel = cbnew 10 ""
682 ; memused = 0
683 ; gen = 0
684 ; throttle = None
685 ; autoscroll = None
686 ; ghyll = noghyll
687 ; help = makehelp ()
688 ; docinfo = []
689 ; texid = None
690 ; prevzoom = 1.0
691 ; progress = -1.0
692 ; uioh = nouioh
693 ; redisplay = true
694 ; mpos = (-1, -1)
695 ; keystate = KSnone
696 ; glinks = false
700 let vlog fmt =
701 if conf.verbose
702 then
703 Printf.kprintf prerr_endline fmt
704 else
705 Printf.kprintf ignore fmt
708 let launchpath () =
709 if String.length conf.pathlauncher = 0
710 then print_endline state.path
711 else (
712 let re = Str.regexp "%s" in
713 let command = Str.global_replace re state.path conf.pathlauncher in
714 try popen command []
715 with exn ->
716 Printf.eprintf
717 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
718 flush stderr;
722 module Ne = struct
723 type 'a t = | Res of 'a | Exn of exn;;
725 let pipe () =
726 try Res (Unix.pipe ())
727 with exn -> Exn exn
730 let clo fd f =
731 try Unix.close fd
732 with exn -> f (Printexc.to_string exn)
735 let dup fd =
736 try Res (Unix.dup fd)
737 with exn -> Exn exn
740 let dup2 fd1 fd2 =
741 try Res (Unix.dup2 fd1 fd2)
742 with exn -> Exn exn
744 end;;
746 let redirectstderr () =
747 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
748 if conf.redirectstderr
749 then
750 match Ne.pipe () with
751 | Ne.Exn exn ->
752 dolog "failed to create stderr redirection pipes: %s"
753 (Printexc.to_string exn)
755 | Ne.Res (r, w) ->
756 begin match Ne.dup Unix.stderr with
757 | Ne.Exn exn ->
758 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
759 Ne.clo r (clofail "pipe/r");
760 Ne.clo w (clofail "pipe/w");
762 | Ne.Res dupstderr ->
763 begin match Ne.dup2 w Unix.stderr with
764 | Ne.Exn exn ->
765 dolog "failed to dup2 to stderr: %s"
766 (Printexc.to_string exn);
767 Ne.clo dupstderr (clofail "stderr duplicate");
768 Ne.clo r (clofail "redir pipe/r");
769 Ne.clo w (clofail "redir pipe/w");
771 | Ne.Res () ->
772 state.stderr <- dupstderr;
773 state.errfd <- Some r;
774 end;
776 else (
777 state.newerrmsgs <- false;
778 begin match state.errfd with
779 | Some fd ->
780 begin match Ne.dup2 state.stderr Unix.stderr with
781 | Ne.Exn exn ->
782 dolog "failed to dup2 original stderr: %s"
783 (Printexc.to_string exn)
784 | Ne.Res () ->
785 Ne.clo fd (clofail "dup of stderr");
786 Unix.dup2 state.stderr Unix.stderr;
787 state.errfd <- None;
788 end;
789 | None -> ()
790 end;
791 prerr_string (Buffer.contents state.errmsgs);
792 flush stderr;
793 Buffer.clear state.errmsgs;
797 module G =
798 struct
799 let postRedisplay who =
800 if conf.verbose
801 then prerr_endline ("redisplay for " ^ who);
802 state.redisplay <- true;
804 end;;
806 let getopaque pageno =
807 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
808 with Not_found -> None
811 let putopaque pageno opaque =
812 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
815 let pagetranslatepoint l x y =
816 let dy = y - l.pagedispy in
817 let y = dy + l.pagey in
818 let dx = x - l.pagedispx in
819 let x = dx + l.pagex in
820 (x, y);
823 let getunder x y =
824 let rec f = function
825 | l :: rest ->
826 begin match getopaque l.pageno with
827 | Some opaque ->
828 let x0 = l.pagedispx in
829 let x1 = x0 + l.pagevw in
830 let y0 = l.pagedispy in
831 let y1 = y0 + l.pagevh in
832 if y >= y0 && y <= y1 && x >= x0 && x <= x1
833 then
834 let px, py = pagetranslatepoint l x y in
835 match whatsunder opaque px py with
836 | Unone -> f rest
837 | under -> under
838 else f rest
839 | _ ->
840 f rest
842 | [] -> Unone
844 f state.layout
847 let showtext c s =
848 state.text <- Printf.sprintf "%c%s" c s;
849 G.postRedisplay "showtext";
852 let undertext = function
853 | Unone -> "none"
854 | Ulinkuri s -> s
855 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
856 | Utext s -> "font: " ^ s
857 | Uunexpected s -> "unexpected: " ^ s
858 | Ulaunch s -> "launch: " ^ s
859 | Unamed s -> "named: " ^ s
860 | Uremote (filename, pageno) ->
861 Printf.sprintf "%s: page %d" filename (pageno+1)
864 let updateunder x y =
865 match getunder x y with
866 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
867 | Ulinkuri uri ->
868 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
869 Wsi.setcursor Wsi.CURSOR_INFO
870 | Ulinkgoto (pageno, _) ->
871 if conf.underinfo
872 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
873 Wsi.setcursor Wsi.CURSOR_INFO
874 | Utext s ->
875 if conf.underinfo then showtext 'f' ("ont: " ^ s);
876 Wsi.setcursor Wsi.CURSOR_TEXT
877 | Uunexpected s ->
878 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
879 Wsi.setcursor Wsi.CURSOR_INHERIT
880 | Ulaunch s ->
881 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
882 Wsi.setcursor Wsi.CURSOR_INHERIT
883 | Unamed s ->
884 if conf.underinfo then showtext 'n' ("amed: " ^ s);
885 Wsi.setcursor Wsi.CURSOR_INHERIT
886 | Uremote (filename, pageno) ->
887 if conf.underinfo then showtext 'r'
888 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
889 Wsi.setcursor Wsi.CURSOR_INFO
892 let showlinktype under =
893 if conf.underinfo
894 then
895 match under with
896 | Unone -> ()
897 | under ->
898 let s = undertext under in
899 showtext ' ' s
902 let addchar s c =
903 let b = Buffer.create (String.length s + 1) in
904 Buffer.add_string b s;
905 Buffer.add_char b c;
906 Buffer.contents b;
909 let colorspace_of_string s =
910 match String.lowercase s with
911 | "rgb" -> Rgb
912 | "bgr" -> Bgr
913 | "gray" -> Gray
914 | _ -> failwith "invalid colorspace"
917 let int_of_colorspace = function
918 | Rgb -> 0
919 | Bgr -> 1
920 | Gray -> 2
923 let colorspace_of_int = function
924 | 0 -> Rgb
925 | 1 -> Bgr
926 | 2 -> Gray
927 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
930 let colorspace_to_string = function
931 | Rgb -> "rgb"
932 | Bgr -> "bgr"
933 | Gray -> "gray"
936 let intentry_with_suffix text key =
937 let c =
938 if key >= 32 && key < 127
939 then Char.chr key
940 else '\000'
942 match Char.lowercase c with
943 | '0' .. '9' ->
944 let text = addchar text c in
945 TEcont text
947 | 'k' | 'm' | 'g' ->
948 let text = addchar text c in
949 TEcont text
951 | _ ->
952 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
953 TEcont text
956 let multicolumns_to_string (n, a, b) =
957 if a = 0 && b = 0
958 then Printf.sprintf "%d" n
959 else Printf.sprintf "%d,%d,%d" n a b;
962 let multicolumns_of_string s =
964 (int_of_string s, 0, 0)
965 with _ ->
966 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
969 let readcmd fd =
970 let s = "xxxx" in
971 let n = Unix.read fd s 0 4 in
972 if n != 4 then failwith "incomplete read(len)";
973 let len = 0
974 lor (Char.code s.[0] lsl 24)
975 lor (Char.code s.[1] lsl 16)
976 lor (Char.code s.[2] lsl 8)
977 lor (Char.code s.[3] lsl 0)
979 let s = String.create len in
980 let n = Unix.read fd s 0 len in
981 if n != len then failwith "incomplete read(data)";
985 let btod b = if b then 1 else 0;;
987 let wcmd fmt =
988 let b = Buffer.create 16 in
989 Buffer.add_string b "llll";
990 Printf.kbprintf
991 (fun b ->
992 let s = Buffer.contents b in
993 let n = String.length s in
994 let len = n - 4 in
995 (* dolog "wcmd %S" (String.sub s 4 len); *)
996 s.[0] <- Char.chr ((len lsr 24) land 0xff);
997 s.[1] <- Char.chr ((len lsr 16) land 0xff);
998 s.[2] <- Char.chr ((len lsr 8) land 0xff);
999 s.[3] <- Char.chr (len land 0xff);
1000 let n' = Unix.write state.sw s 0 n in
1001 if n' != n then failwith "write failed";
1002 ) b fmt;
1005 let calcips h =
1006 if conf.presentation
1007 then
1008 let d = conf.winh - h in
1009 max 0 ((d + 1) / 2)
1010 else
1011 conf.interpagespace
1014 let calcheight () =
1015 let rec f pn ph pi fh l =
1016 match l with
1017 | (n, _, h, _) :: rest ->
1018 let ips = calcips h in
1019 let fh =
1020 if conf.presentation
1021 then fh+ips
1022 else (
1023 if isbirdseye state.mode && pn = 0
1024 then fh + ips
1025 else fh
1028 let fh = fh + ((n - pn) * (ph + pi)) in
1029 f n h ips fh rest;
1031 | [] ->
1032 let inc =
1033 if conf.presentation || (isbirdseye state.mode && pn = 0)
1034 then 0
1035 else -pi
1037 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
1038 max 0 fh
1040 let fh = f 0 0 0 0 state.pdims in
1044 let calcheight () =
1045 match conf.columns with
1046 | Csingle -> calcheight ()
1047 | Cmulti ((c, _, _), b) ->
1048 let rec loop y h n =
1049 if n < 0
1050 then loop y h (n+1)
1051 else (
1052 if n = Array.length b
1053 then y + h
1054 else
1055 let (_, _, y', (_, _, h', _)) = b.(n) in
1056 let y = min y y'
1057 and h = max h h' in
1058 loop y h (n+1)
1061 loop max_int 0 (((Array.length b - 1) / c) * c)
1062 | Csplit (_, b) ->
1063 if Array.length b > 0
1064 then
1065 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1066 y + h
1067 else 0
1070 let getpageyh pageno =
1071 let rec f pn ph pi y l =
1072 match l with
1073 | (n, _, h, _) :: rest ->
1074 let ips = calcips h in
1075 if n >= pageno
1076 then
1077 let h = if n = pageno then h else ph in
1078 if conf.presentation && n = pageno
1079 then
1080 y + (pageno - pn) * (ph + pi) + pi, h
1081 else
1082 y + (pageno - pn) * (ph + pi), h
1083 else
1084 let y = y + (if conf.presentation then pi else 0) in
1085 let y = y + (n - pn) * (ph + pi) in
1086 f n h ips y rest
1088 | [] ->
1089 y + (pageno - pn) * (ph + pi), ph
1091 f 0 0 0 0 state.pdims
1094 let getpageyh pageno =
1095 match conf.columns with
1096 | Csingle -> getpageyh pageno
1097 | Cmulti (_, b) ->
1098 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1099 y, h
1100 | Csplit (c, b) ->
1101 let n = pageno*c in
1102 let (_, _, y, (_, _, h, _)) = b.(n) in
1103 y, h
1106 let getpagedim pageno =
1107 let rec f ppdim l =
1108 match l with
1109 | (n, _, _, _) as pdim :: rest ->
1110 if n >= pageno
1111 then (if n = pageno then pdim else ppdim)
1112 else f pdim rest
1114 | [] -> ppdim
1116 f (-1, -1, -1, -1) state.pdims
1119 let getpagey pageno = fst (getpageyh pageno);;
1121 let nogeomcmds cmds =
1122 match cmds with
1123 | s, [] -> String.length s = 0
1124 | _ -> false
1127 let layout1 y sh =
1128 let sh = sh - state.hscrollh in
1129 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1130 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1131 match pdims with
1132 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1133 let ips = calcips h in
1134 let yinc =
1135 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1136 then ips
1137 else 0
1139 (w, h, ips, xoff), rest, pdimno + 1, yinc
1140 | _ ->
1141 prev, pdims, pdimno, 0
1143 let dy = dy + yinc in
1144 let py = py + yinc in
1145 if pageno = state.pagecount || dy >= sh
1146 then
1147 accu
1148 else
1149 let vy = y + dy in
1150 if py + h <= vy - yinc
1151 then
1152 let py = py + h + ips in
1153 let dy = max 0 (py - y) in
1154 f ~pageno:(pageno+1)
1155 ~pdimno
1156 ~prev:curr
1159 ~pdims:rest
1160 ~accu
1161 else
1162 let pagey = vy - py in
1163 let pagevh = h - pagey in
1164 let pagevh = min (sh - dy) pagevh in
1165 let off = if yinc > 0 then py - vy else 0 in
1166 let py = py + h + ips in
1167 let pagex, dx =
1168 let xoff = xoff +
1169 if state.w < conf.winw - state.scrollw
1170 then (conf.winw - state.scrollw - state.w) / 2
1171 else 0
1173 let dispx = xoff + state.x in
1174 if dispx < 0
1175 then (-dispx, 0)
1176 else (0, dispx)
1178 let pagevw =
1179 let lw = w - pagex in
1180 min lw (conf.winw - state.scrollw)
1182 let e =
1183 { pageno = pageno
1184 ; pagedimno = pdimno
1185 ; pagew = w
1186 ; pageh = h
1187 ; pagex = pagex
1188 ; pagey = pagey + off
1189 ; pagevw = pagevw
1190 ; pagevh = pagevh - off
1191 ; pagedispx = dx
1192 ; pagedispy = dy + off
1193 ; pagecol = 0
1196 let accu = e :: accu in
1197 f ~pageno:(pageno+1)
1198 ~pdimno
1199 ~prev:curr
1201 ~dy:(dy+pagevh+ips)
1202 ~pdims:rest
1203 ~accu
1205 let accu =
1207 ~pageno:0
1208 ~pdimno:~-1
1209 ~prev:(0,0,0,0)
1210 ~py:0
1211 ~dy:0
1212 ~pdims:state.pdims
1213 ~accu:[]
1215 List.rev accu
1218 let layoutN ((columns, coverA, coverB), b) y sh =
1219 let sh = sh - state.hscrollh in
1220 let rec fold accu n =
1221 if n = Array.length b
1222 then accu
1223 else
1224 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1225 if (vy - y) > sh &&
1226 (n = coverA - 1
1227 || n = state.pagecount - coverB
1228 || (n - coverA) mod columns = columns - 1)
1229 then accu
1230 else
1231 let accu =
1232 if vy + h > y
1233 then
1234 let pagey = max 0 (y - vy) in
1235 let pagedispy = if pagey > 0 then 0 else vy - y in
1236 let pagedispx, pagex =
1237 let pdx =
1238 if n = coverA - 1 || n = state.pagecount - coverB
1239 then state.x + (conf.winw - state.scrollw - w) / 2
1240 else dx + xoff + state.x
1242 if pdx < 0
1243 then 0, -pdx
1244 else pdx, 0
1246 let pagevw =
1247 let vw = conf.winw - state.scrollw - pagedispx in
1248 let pw = w - pagex in
1249 min vw pw
1251 let pagevh = min (h - pagey) (sh - pagedispy) in
1252 if pagevw > 0 && pagevh > 0
1253 then
1254 let e =
1255 { pageno = n
1256 ; pagedimno = pdimno
1257 ; pagew = w
1258 ; pageh = h
1259 ; pagex = pagex
1260 ; pagey = pagey
1261 ; pagevw = pagevw
1262 ; pagevh = pagevh
1263 ; pagedispx = pagedispx
1264 ; pagedispy = pagedispy
1265 ; pagecol = 0
1268 e :: accu
1269 else
1270 accu
1271 else
1272 accu
1274 fold accu (n+1)
1276 List.rev (fold [] 0)
1279 let layoutS (columns, b) y sh =
1280 let sh = sh - state.hscrollh in
1281 let rec fold accu n =
1282 if n = Array.length b
1283 then accu
1284 else
1285 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1286 if (vy - y) > sh
1287 then accu
1288 else
1289 let accu =
1290 if vy + pageh > y
1291 then
1292 let x = xoff + state.x in
1293 let pagey = max 0 (y - vy) in
1294 let pagedispy = if pagey > 0 then 0 else vy - y in
1295 let pagedispx, pagex =
1296 if px = 0
1297 then (
1298 if x < 0
1299 then 0, -x
1300 else x, 0
1302 else (
1303 let px = px - x in
1304 if px < 0
1305 then -px, 0
1306 else 0, px
1309 let pagecolw = pagew/columns in
1310 let pagedispx =
1311 if not conf.fullsplit && pagecolw < conf.winw
1312 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1313 else pagedispx
1315 let pagevw =
1316 let vw = conf.winw - pagedispx - state.scrollw in
1317 let pw = pagew - pagex in
1318 min vw pw
1320 let pagevw =
1321 if conf.fullsplit
1322 then pagevw
1323 else min pagevw pagecolw
1325 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1326 if pagevw > 0 && pagevh > 0
1327 then
1328 let e =
1329 { pageno = n/columns
1330 ; pagedimno = pdimno
1331 ; pagew = pagew
1332 ; pageh = pageh
1333 ; pagex = pagex
1334 ; pagey = pagey
1335 ; pagevw = pagevw
1336 ; pagevh = pagevh
1337 ; pagedispx = pagedispx
1338 ; pagedispy = pagedispy
1339 ; pagecol = n mod columns
1342 e :: accu
1343 else
1344 accu
1345 else
1346 accu
1348 fold accu (n+1)
1350 List.rev (fold [] 0)
1353 let layout y sh =
1354 if nogeomcmds state.geomcmds
1355 then
1356 match conf.columns with
1357 | Csingle -> layout1 y sh
1358 | Cmulti c -> layoutN c y sh
1359 | Csplit s -> layoutS s y sh
1360 else []
1363 let clamp incr =
1364 let y = state.y + incr in
1365 let y = max 0 y in
1366 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1370 let itertiles l f =
1371 let tilex = l.pagex mod conf.tilew in
1372 let tiley = l.pagey mod conf.tileh in
1374 let col = l.pagex / conf.tilew in
1375 let row = l.pagey / conf.tileh in
1377 let rec rowloop row y0 dispy h =
1378 if h = 0
1379 then ()
1380 else (
1381 let dh = conf.tileh - y0 in
1382 let dh = min h dh in
1383 let rec colloop col x0 dispx w =
1384 if w = 0
1385 then ()
1386 else (
1387 let dw = conf.tilew - x0 in
1388 let dw = min w dw in
1390 f col row dispx dispy x0 y0 dw dh;
1391 colloop (col+1) 0 (dispx+dw) (w-dw)
1394 colloop col tilex l.pagedispx l.pagevw;
1395 rowloop (row+1) 0 (dispy+dh) (h-dh)
1398 if l.pagevw > 0 && l.pagevh > 0
1399 then rowloop row tiley l.pagedispy l.pagevh;
1402 let gettileopaque l col row =
1403 let key =
1404 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1406 try Some (Hashtbl.find state.tilemap key)
1407 with Not_found -> None
1410 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1411 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1412 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1415 let drawtiles l color =
1416 GlDraw.color color;
1417 let f col row x y tilex tiley w h =
1418 match gettileopaque l col row with
1419 | Some (opaque, _, t) ->
1420 let params = x, y, w, h, tilex, tiley in
1421 if conf.invert
1422 then (
1423 Gl.enable `blend;
1424 GlFunc.blend_func `zero `one_minus_src_color;
1426 drawtile params opaque;
1427 if conf.invert
1428 then Gl.disable `blend;
1429 if conf.debug
1430 then (
1431 let s = Printf.sprintf
1432 "%d[%d,%d] %f sec"
1433 l.pageno col row t
1435 let w = measurestr fstate.fontsize s in
1436 GlMisc.push_attrib [`current];
1437 GlDraw.color (0.0, 0.0, 0.0);
1438 GlDraw.rect
1439 (float (x-2), float (y-2))
1440 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1441 GlDraw.color (1.0, 1.0, 1.0);
1442 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1443 GlMisc.pop_attrib ();
1446 | _ ->
1447 let w =
1448 let lw = conf.winw - state.scrollw - x in
1449 min lw w
1450 and h =
1451 let lh = conf.winh - y in
1452 min lh h
1454 Gl.enable `texture_2d;
1455 begin match state.texid with
1456 | Some id ->
1457 GlTex.bind_texture `texture_2d id;
1458 let x0 = float x
1459 and y0 = float y
1460 and x1 = float (x+w)
1461 and y1 = float (y+h) in
1463 let tw = float w /. 64.0
1464 and th = float h /. 64.0 in
1465 let tx0 = float tilex /. 64.0
1466 and ty0 = float tiley /. 64.0 in
1467 let tx1 = tx0 +. tw
1468 and ty1 = ty0 +. th in
1469 GlDraw.begins `quads;
1470 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1471 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1472 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1473 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1474 GlDraw.ends ();
1476 Gl.disable `texture_2d;
1477 | None ->
1478 GlDraw.color (1.0, 1.0, 1.0);
1479 GlDraw.rect
1480 (float x, float y)
1481 (float (x+w), float (y+h));
1482 end;
1483 if w > 128 && h > fstate.fontsize + 10
1484 then (
1485 GlDraw.color (0.0, 0.0, 0.0);
1486 let c, r =
1487 if conf.verbose
1488 then (col*conf.tilew, row*conf.tileh)
1489 else col, row
1491 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1493 GlDraw.color color;
1495 itertiles l f
1498 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1500 let tilevisible1 l x y =
1501 let ax0 = l.pagex
1502 and ax1 = l.pagex + l.pagevw
1503 and ay0 = l.pagey
1504 and ay1 = l.pagey + l.pagevh in
1506 let bx0 = x
1507 and by0 = y in
1508 let bx1 = min (bx0 + conf.tilew) l.pagew
1509 and by1 = min (by0 + conf.tileh) l.pageh in
1511 let rx0 = max ax0 bx0
1512 and ry0 = max ay0 by0
1513 and rx1 = min ax1 bx1
1514 and ry1 = min ay1 by1 in
1516 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1517 nonemptyintersection
1520 let tilevisible layout n x y =
1521 let rec findpageinlayout m = function
1522 | l :: rest when l.pageno = n ->
1523 tilevisible1 l x y || (
1524 match conf.columns with
1525 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1526 | _ -> false
1528 | _ :: rest -> findpageinlayout 0 rest
1529 | [] -> false
1531 findpageinlayout 0 layout;
1534 let tileready l x y =
1535 tilevisible1 l x y &&
1536 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1539 let tilepage n p layout =
1540 let rec loop = function
1541 | l :: rest ->
1542 if l.pageno = n
1543 then
1544 let f col row _ _ _ _ _ _ =
1545 if state.currently = Idle
1546 then
1547 match gettileopaque l col row with
1548 | Some _ -> ()
1549 | None ->
1550 let x = col*conf.tilew
1551 and y = row*conf.tileh in
1552 let w =
1553 let w = l.pagew - x in
1554 min w conf.tilew
1556 let h =
1557 let h = l.pageh - y in
1558 min h conf.tileh
1560 wcmd "tile %s %d %d %d %d" p x y w h;
1561 state.currently <-
1562 Tiling (
1563 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1564 conf.tilew, conf.tileh
1567 itertiles l f;
1568 else
1569 loop rest
1571 | [] -> ()
1573 if nogeomcmds state.geomcmds
1574 then loop layout;
1577 let preloadlayout visiblepages =
1578 let presentation = conf.presentation in
1579 let interpagespace = conf.interpagespace in
1580 let maxy = state.maxy in
1581 conf.presentation <- false;
1582 conf.interpagespace <- 0;
1583 state.maxy <- calcheight ();
1584 let y =
1585 match visiblepages with
1586 | [] -> if state.y >= maxy then maxy else 0
1587 | l :: _ -> getpagey l.pageno + l.pagey
1589 let y = if y < conf.winh then 0 else y - conf.winh in
1590 let h = state.y - y + conf.winh*3 in
1591 let pages = layout y h in
1592 conf.presentation <- presentation;
1593 conf.interpagespace <- interpagespace;
1594 state.maxy <- maxy;
1595 pages;
1598 let load pages =
1599 let rec loop pages =
1600 if state.currently != Idle
1601 then ()
1602 else
1603 match pages with
1604 | l :: rest ->
1605 begin match getopaque l.pageno with
1606 | None ->
1607 wcmd "page %d %d" l.pageno l.pagedimno;
1608 state.currently <- Loading (l, state.gen);
1609 | Some opaque ->
1610 tilepage l.pageno opaque pages;
1611 loop rest
1612 end;
1613 | _ -> ()
1615 if nogeomcmds state.geomcmds
1616 then loop pages
1619 let preload pages =
1620 load pages;
1621 if conf.preload && state.currently = Idle
1622 then load (preloadlayout pages);
1625 let layoutready layout =
1626 let rec fold all ls =
1627 all && match ls with
1628 | l :: rest ->
1629 let seen = ref false in
1630 let allvisible = ref true in
1631 let foo col row _ _ _ _ _ _ =
1632 seen := true;
1633 allvisible := !allvisible &&
1634 begin match gettileopaque l col row with
1635 | Some _ -> true
1636 | None -> false
1639 itertiles l foo;
1640 fold (!seen && !allvisible) rest
1641 | [] -> true
1643 let alltilesvisible = fold true layout in
1644 alltilesvisible;
1647 let gotoy y =
1648 let y = bound y 0 state.maxy in
1649 let y, layout, proceed =
1650 match conf.maxwait with
1651 | Some time when state.ghyll == noghyll ->
1652 begin match state.throttle with
1653 | None ->
1654 let layout = layout y conf.winh in
1655 let ready = layoutready layout in
1656 if not ready
1657 then (
1658 load layout;
1659 state.throttle <- Some (layout, y, now ());
1661 else G.postRedisplay "gotoy showall (None)";
1662 y, layout, ready
1663 | Some (_, _, started) ->
1664 let dt = now () -. started in
1665 if dt > time
1666 then (
1667 state.throttle <- None;
1668 let layout = layout y conf.winh in
1669 load layout;
1670 G.postRedisplay "maxwait";
1671 y, layout, true
1673 else -1, [], false
1676 | _ ->
1677 let layout = layout y conf.winh in
1678 if true || layoutready layout
1679 then G.postRedisplay "gotoy ready";
1680 y, layout, true
1682 if proceed
1683 then (
1684 state.y <- y;
1685 state.layout <- layout;
1686 begin match state.mode with
1687 | LinkNav (Ltexact (pageno, linkno)) ->
1688 let rec loop = function
1689 | [] ->
1690 state.mode <- LinkNav (Ltgendir 0)
1691 | l :: _ when l.pageno = pageno ->
1692 begin match getopaque pageno with
1693 | None ->
1694 state.mode <- LinkNav (Ltgendir 0)
1695 | Some opaque ->
1696 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1697 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1698 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1699 then state.mode <- LinkNav (Ltgendir 0)
1701 | _ :: rest -> loop rest
1703 loop layout
1704 | _ -> ()
1705 end;
1706 begin match state.mode with
1707 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1708 if not (pagevisible layout pageno)
1709 then (
1710 match state.layout with
1711 | [] -> ()
1712 | l :: _ ->
1713 state.mode <- Birdseye (
1714 conf, leftx, l.pageno, hooverpageno, anchor
1717 | LinkNav (Ltgendir dir as lt) ->
1718 let linknav =
1719 let rec loop = function
1720 | [] -> lt
1721 | l :: rest ->
1722 match getopaque l.pageno with
1723 | None -> loop rest
1724 | Some opaque ->
1725 let link =
1726 let ld =
1727 if dir = 0
1728 then LDfirstvisible (l.pagex, l.pagey, dir)
1729 else (
1730 if dir > 0 then LDfirst else LDlast
1733 findlink opaque ld
1735 match link with
1736 | Lnotfound -> loop rest
1737 | Lfound n ->
1738 showlinktype (getlink opaque n);
1739 Ltexact (l.pageno, n)
1741 loop state.layout
1743 state.mode <- LinkNav linknav
1744 | _ -> ()
1745 end;
1746 preload layout;
1748 state.ghyll <- noghyll;
1749 if conf.updatecurs
1750 then (
1751 let mx, my = state.mpos in
1752 updateunder mx my;
1756 let conttiling pageno opaque =
1757 tilepage pageno opaque
1758 (if conf.preload then preloadlayout state.layout else state.layout)
1761 let gotoy_and_clear_text y =
1762 if not conf.verbose then state.text <- "";
1763 gotoy y;
1766 let getanchor () =
1767 match state.layout with
1768 | [] -> emptyanchor
1769 | l :: _ ->
1770 let coloff = l.pagecol * l.pageh in
1771 (l.pageno, (float l.pagey +. float coloff) /. float l.pageh)
1774 let getanchory (n, top) =
1775 let y, h = getpageyh n in
1776 y + (truncate (top *. float h));
1779 let gotoanchor anchor =
1780 gotoy (getanchory anchor);
1783 let addnav () =
1784 cbput state.hists.nav (getanchor ());
1787 let getnav dir =
1788 let anchor = cbgetc state.hists.nav dir in
1789 getanchory anchor;
1792 let gotoghyll y =
1793 let rec scroll f n a b =
1794 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1795 let snake f a b =
1796 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1797 if f < a
1798 then s (float f /. float a)
1799 else (
1800 if f > b
1801 then 1.0 -. s ((float (f-b) /. float (n-b)))
1802 else 1.0
1805 snake f a b
1806 and summa f n a b =
1807 (* courtesy:
1808 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1809 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1810 let iv1 = iv f in
1811 let ins = float a *. iv1
1812 and outs = float (n-b) *. iv1 in
1813 let ones = b - a in
1814 ins +. outs +. float ones
1816 let rec set (_N, _A, _B) y sy =
1817 let sum = summa 1.0 _N _A _B in
1818 let dy = float (y - sy) in
1819 state.ghyll <- (
1820 let rec gf n y1 o =
1821 if n >= _N
1822 then state.ghyll <- noghyll
1823 else
1824 let go n =
1825 let s = scroll n _N _A _B in
1826 let y1 = y1 +. ((s *. dy) /. sum) in
1827 gotoy_and_clear_text (truncate y1);
1828 state.ghyll <- gf (n+1) y1;
1830 match o with
1831 | None -> go n
1832 | Some y' -> set (_N/2, 0, 0) y' state.y
1834 gf 0 (float state.y)
1837 match conf.ghyllscroll with
1838 | None ->
1839 gotoy_and_clear_text y
1840 | Some nab ->
1841 if state.ghyll == noghyll
1842 then set nab y state.y
1843 else state.ghyll (Some y)
1846 let gotopage n top =
1847 let y, h = getpageyh n in
1848 let y = y + (truncate (top *. float h)) in
1849 gotoghyll y
1852 let gotopage1 n top =
1853 let y = getpagey n in
1854 let y = y + top in
1855 gotoghyll y
1858 let invalidate s f =
1859 state.layout <- [];
1860 state.pdims <- [];
1861 state.rects <- [];
1862 state.rects1 <- [];
1863 match state.geomcmds with
1864 | ps, [] when String.length ps = 0 ->
1865 f ();
1866 state.geomcmds <- s, [];
1868 | ps, [] ->
1869 state.geomcmds <- ps, [s, f];
1871 | ps, (s', _) :: rest when s' = s ->
1872 state.geomcmds <- ps, ((s, f) :: rest);
1874 | ps, cmds ->
1875 state.geomcmds <- ps, ((s, f) :: cmds);
1878 let opendoc path password =
1879 state.path <- path;
1880 state.password <- password;
1881 state.gen <- state.gen + 1;
1882 state.docinfo <- [];
1884 setaalevel conf.aalevel;
1885 Wsi.settitle ("llpp " ^ Filename.basename path);
1886 wcmd "open %s\000%s\000" path password;
1887 invalidate "reqlayout"
1888 (fun () ->
1889 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1892 let scalecolor c =
1893 let c = c *. conf.colorscale in
1894 (c, c, c);
1897 let scalecolor2 (r, g, b) =
1898 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1901 let docolumns = function
1902 | Csingle -> ()
1904 | Cmulti ((columns, coverA, coverB), _) ->
1905 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1906 let rec loop pageno pdimno pdim x y rowh pdims =
1907 let rec fixrow m = if m = pageno then () else
1908 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1909 if h < rowh
1910 then (
1911 let y = y + (rowh - h) / 2 in
1912 a.(m) <- (pdimno, x, y, pdim);
1914 fixrow (m+1)
1916 if pageno = state.pagecount
1917 then fixrow (((pageno - 1) / columns) * columns)
1918 else
1919 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1920 match pdims with
1921 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1922 pdimno+1, pdim, rest
1923 | _ ->
1924 pdimno, pdim, pdims
1926 let x, y, rowh' =
1927 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1928 then (
1929 (conf.winw - state.scrollw - w) / 2,
1930 y + rowh + conf.interpagespace, h
1932 else (
1933 if (pageno - coverA) mod columns = 0
1934 then 0, y + rowh + conf.interpagespace, h
1935 else x, y, max rowh h
1938 if pageno > 1 && (pageno - coverA) mod columns = 0
1939 then fixrow (pageno - columns);
1940 a.(pageno) <- (pdimno, x, y, pdim);
1941 let x = x + w + xoff*2 + conf.interpagespace in
1942 loop (pageno+1) pdimno pdim x y rowh' pdims
1944 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1945 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1947 | Csplit (c, _) ->
1948 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1949 let rec loop pageno pdimno pdim y pdims =
1950 if pageno = state.pagecount
1951 then ()
1952 else
1953 let pdimno, ((_, w, h, _) as pdim), pdims =
1954 match pdims with
1955 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1956 pdimno+1, pdim, rest
1957 | _ ->
1958 pdimno, pdim, pdims
1960 let cw = w / c in
1961 let rec loop1 n x y =
1962 if n = c then y else (
1963 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1964 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1967 let y = loop1 0 0 y in
1968 loop (pageno+1) pdimno pdim y pdims
1970 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1971 conf.columns <- Csplit (c, a);
1974 let represent () =
1975 docolumns conf.columns;
1976 state.maxy <- calcheight ();
1977 state.hscrollh <-
1978 if state.w <= conf.winw - state.scrollw
1979 then 0
1980 else state.scrollw
1982 match state.mode with
1983 | Birdseye (_, _, pageno, _, _) ->
1984 let y, h = getpageyh pageno in
1985 let top = (conf.winh - h) / 2 in
1986 gotoy (max 0 (y - top))
1987 | _ -> gotoanchor state.anchor
1990 let reshape w h =
1991 GlDraw.viewport 0 0 w h;
1992 let firsttime = state.geomcmds == firstgeomcmds in
1993 if not firsttime && nogeomcmds state.geomcmds
1994 then state.anchor <- getanchor ();
1996 conf.winw <- w;
1997 let w = truncate (float w *. conf.zoom) - state.scrollw in
1998 let w = max w 2 in
1999 conf.winh <- h;
2000 setfontsize fstate.fontsize;
2001 GlMat.mode `modelview;
2002 GlMat.load_identity ();
2004 GlMat.mode `projection;
2005 GlMat.load_identity ();
2006 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2007 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2008 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2010 let relx =
2011 if conf.zoom <= 1.0
2012 then 0.0
2013 else float state.x /. float state.w
2015 invalidate "geometry"
2016 (fun () ->
2017 state.w <- w;
2018 if not firsttime
2019 then state.x <- truncate (relx *. float w);
2020 let w =
2021 match conf.columns with
2022 | Csingle -> w
2023 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2024 | Csplit (c, _) -> w * c
2026 wcmd "geometry %d %d" w h);
2029 let enttext () =
2030 let len = String.length state.text in
2031 let drawstring s =
2032 let hscrollh =
2033 match state.mode with
2034 | Textentry _
2035 | View ->
2036 let h, _, _ = state.uioh#scrollpw in
2038 | _ -> 0
2040 let rect x w =
2041 GlDraw.rect
2042 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2043 (x+.w, float (conf.winh - hscrollh))
2046 let w = float (conf.winw - state.scrollw - 1) in
2047 if state.progress >= 0.0 && state.progress < 1.0
2048 then (
2049 GlDraw.color (0.3, 0.3, 0.3);
2050 let w1 = w *. state.progress in
2051 rect 0.0 w1;
2052 GlDraw.color (0.0, 0.0, 0.0);
2053 rect w1 (w-.w1)
2055 else (
2056 GlDraw.color (0.0, 0.0, 0.0);
2057 rect 0.0 w;
2060 GlDraw.color (1.0, 1.0, 1.0);
2061 drawstring fstate.fontsize
2062 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2064 let s =
2065 match state.mode with
2066 | Textentry ((prefix, text, _, _, _, _), _) ->
2067 let s =
2068 if len > 0
2069 then
2070 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2071 else
2072 Printf.sprintf "%s%s_" prefix text
2076 | _ -> state.text
2078 let s =
2079 if state.newerrmsgs
2080 then (
2081 if not (istextentry state.mode)
2082 then
2083 let s1 = "(press 'e' to review error messasges)" in
2084 if String.length s > 0 then s ^ " " ^ s1 else s1
2085 else s
2087 else s
2089 if String.length s > 0
2090 then drawstring s
2093 let gctiles () =
2094 let len = Queue.length state.tilelru in
2095 let rec loop qpos =
2096 if state.memused <= conf.memlimit
2097 then ()
2098 else (
2099 if qpos < len
2100 then
2101 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2102 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2103 let (_, pw, ph, _) = getpagedim n in
2105 gen = state.gen
2106 && colorspace = conf.colorspace
2107 && angle = conf.angle
2108 && pagew = pw
2109 && pageh = ph
2110 && (
2111 let layout =
2112 match state.throttle with
2113 | None ->
2114 if conf.preload
2115 then preloadlayout state.layout
2116 else state.layout
2117 | Some (layout, _, _) ->
2118 layout
2120 let x = col*conf.tilew
2121 and y = row*conf.tileh in
2122 tilevisible layout n x y
2124 then Queue.push lruitem state.tilelru
2125 else (
2126 wcmd "freetile %s" p;
2127 state.memused <- state.memused - s;
2128 state.uioh#infochanged Memused;
2129 Hashtbl.remove state.tilemap k;
2131 loop (qpos+1)
2134 loop 0
2137 let flushtiles () =
2138 Queue.iter (fun (k, p, s) ->
2139 wcmd "freetile %s" p;
2140 state.memused <- state.memused - s;
2141 state.uioh#infochanged Memused;
2142 Hashtbl.remove state.tilemap k;
2143 ) state.tilelru;
2144 Queue.clear state.tilelru;
2145 load state.layout;
2148 let logcurrently = function
2149 | Idle -> dolog "Idle"
2150 | Loading (l, gen) ->
2151 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2152 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2153 dolog
2154 "Tiling %d[%d,%d] page=%s cs=%s angle"
2155 l.pageno col row pageopaque
2156 (colorspace_to_string colorspace)
2158 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2159 angle gen conf.angle state.gen
2160 tilew tileh
2161 conf.tilew conf.tileh
2163 | Outlining _ ->
2164 dolog "outlining"
2167 let act cmds =
2168 (* dolog "%S" cmds; *)
2169 let op, args =
2170 let spacepos =
2171 try String.index cmds ' '
2172 with Not_found -> -1
2174 if spacepos = -1
2175 then cmds, ""
2176 else
2177 let l = String.length cmds in
2178 let op = String.sub cmds 0 spacepos in
2179 op, begin
2180 if l - spacepos < 2 then ""
2181 else String.sub cmds (spacepos+1) (l-spacepos-1)
2184 match op with
2185 | "clear" ->
2186 state.uioh#infochanged Pdim;
2187 state.pdims <- [];
2189 | "clearrects" ->
2190 state.rects <- state.rects1;
2191 G.postRedisplay "clearrects";
2193 | "continue" ->
2194 let n =
2195 try Scanf.sscanf args "%u" (fun n -> n)
2196 with exn ->
2197 dolog "error processing 'continue' %S: %s"
2198 cmds (Printexc.to_string exn);
2199 exit 1;
2201 state.pagecount <- n;
2202 begin match state.currently with
2203 | Outlining l ->
2204 state.currently <- Idle;
2205 state.outlines <- Array.of_list (List.rev l)
2206 | _ -> ()
2207 end;
2209 let cur, cmds = state.geomcmds in
2210 if String.length cur = 0
2211 then failwith "umpossible";
2213 begin match List.rev cmds with
2214 | [] ->
2215 state.geomcmds <- "", [];
2216 represent ();
2217 | (s, f) :: rest ->
2218 f ();
2219 state.geomcmds <- s, List.rev rest;
2220 end;
2221 if conf.maxwait = None
2222 then G.postRedisplay "continue";
2224 | "title" ->
2225 Wsi.settitle args
2227 | "msg" ->
2228 showtext ' ' args
2230 | "vmsg" ->
2231 if conf.verbose
2232 then showtext ' ' args
2234 | "progress" ->
2235 let progress, text =
2237 Scanf.sscanf args "%f %n"
2238 (fun f pos ->
2239 f, String.sub args pos (String.length args - pos))
2240 with exn ->
2241 dolog "error processing 'progress' %S: %s"
2242 cmds (Printexc.to_string exn);
2243 exit 1;
2245 state.text <- text;
2246 state.progress <- progress;
2247 G.postRedisplay "progress"
2249 | "firstmatch" ->
2250 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2252 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2253 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2254 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2255 with exn ->
2256 dolog "error processing 'firstmatch' %S: %s"
2257 cmds (Printexc.to_string exn);
2258 exit 1;
2260 let y = (getpagey pageno) + truncate y0 in
2261 addnav ();
2262 gotoy y;
2263 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2265 | "match" ->
2266 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2268 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2269 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2270 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2271 with exn ->
2272 dolog "error processing 'match' %S: %s"
2273 cmds (Printexc.to_string exn);
2274 exit 1;
2276 state.rects1 <-
2277 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2279 | "page" ->
2280 let pageopaque, t =
2282 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2283 with exn ->
2284 dolog "error processing 'page' %S: %s"
2285 cmds (Printexc.to_string exn);
2286 exit 1;
2288 begin match state.currently with
2289 | Loading (l, gen) ->
2290 vlog "page %d took %f sec" l.pageno t;
2291 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2292 begin match state.throttle with
2293 | None ->
2294 let preloadedpages =
2295 if conf.preload
2296 then preloadlayout state.layout
2297 else state.layout
2299 let evict () =
2300 let module IntSet =
2301 Set.Make (struct type t = int let compare = (-) end) in
2302 let set =
2303 List.fold_left (fun s l -> IntSet.add l.pageno s)
2304 IntSet.empty preloadedpages
2306 let evictedpages =
2307 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2308 if not (IntSet.mem pageno set)
2309 then (
2310 wcmd "freepage %s" opaque;
2311 key :: accu
2313 else accu
2314 ) state.pagemap []
2316 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2318 evict ();
2319 state.currently <- Idle;
2320 if gen = state.gen
2321 then (
2322 tilepage l.pageno pageopaque state.layout;
2323 load state.layout;
2324 load preloadedpages;
2325 if pagevisible state.layout l.pageno
2326 && layoutready state.layout
2327 then G.postRedisplay "page";
2330 | Some (layout, _, _) ->
2331 state.currently <- Idle;
2332 tilepage l.pageno pageopaque layout;
2333 load state.layout
2334 end;
2336 | _ ->
2337 dolog "Inconsistent loading state";
2338 logcurrently state.currently;
2339 exit 1
2342 | "tile" ->
2343 let (x, y, opaque, size, t) =
2345 Scanf.sscanf args "%u %u %s %u %f"
2346 (fun x y p size t -> (x, y, p, size, t))
2347 with exn ->
2348 dolog "error processing 'tile' %S: %s"
2349 cmds (Printexc.to_string exn);
2350 exit 1;
2352 begin match state.currently with
2353 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2354 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2356 if tilew != conf.tilew || tileh != conf.tileh
2357 then (
2358 wcmd "freetile %s" opaque;
2359 state.currently <- Idle;
2360 load state.layout;
2362 else (
2363 puttileopaque l col row gen cs angle opaque size t;
2364 state.memused <- state.memused + size;
2365 state.uioh#infochanged Memused;
2366 gctiles ();
2367 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2368 opaque, size) state.tilelru;
2370 let layout =
2371 match state.throttle with
2372 | None -> state.layout
2373 | Some (layout, _, _) -> layout
2376 state.currently <- Idle;
2377 if gen = state.gen
2378 && conf.colorspace = cs
2379 && conf.angle = angle
2380 && tilevisible layout l.pageno x y
2381 then conttiling l.pageno pageopaque;
2383 begin match state.throttle with
2384 | None ->
2385 preload state.layout;
2386 if gen = state.gen
2387 && conf.colorspace = cs
2388 && conf.angle = angle
2389 && tilevisible state.layout l.pageno x y
2390 then G.postRedisplay "tile nothrottle";
2392 | Some (layout, y, _) ->
2393 let ready = layoutready layout in
2394 if ready
2395 then (
2396 state.y <- y;
2397 state.layout <- layout;
2398 state.throttle <- None;
2399 G.postRedisplay "throttle";
2401 else load layout;
2402 end;
2405 | _ ->
2406 dolog "Inconsistent tiling state";
2407 logcurrently state.currently;
2408 exit 1
2411 | "pdim" ->
2412 let pdim =
2414 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2415 with exn ->
2416 dolog "error processing 'pdim' %S: %s"
2417 cmds (Printexc.to_string exn);
2418 exit 1;
2420 state.uioh#infochanged Pdim;
2421 state.pdims <- pdim :: state.pdims
2423 | "o" ->
2424 let (l, n, t, h, pos) =
2426 Scanf.sscanf args "%u %u %d %u %n"
2427 (fun l n t h pos -> l, n, t, h, pos)
2428 with exn ->
2429 dolog "error processing 'o' %S: %s"
2430 cmds (Printexc.to_string exn);
2431 exit 1;
2433 let s = String.sub args pos (String.length args - pos) in
2434 let outline = (s, l, (n, float t /. float h)) in
2435 begin match state.currently with
2436 | Outlining outlines ->
2437 state.currently <- Outlining (outline :: outlines)
2438 | Idle ->
2439 state.currently <- Outlining [outline]
2440 | currently ->
2441 dolog "invalid outlining state";
2442 logcurrently currently
2445 | "info" ->
2446 state.docinfo <- (1, args) :: state.docinfo
2448 | "infoend" ->
2449 state.uioh#infochanged Docinfo;
2450 state.docinfo <- List.rev state.docinfo
2452 | _ ->
2453 dolog "unknown cmd `%S'" cmds
2456 let onhist cb =
2457 let rc = cb.rc in
2458 let action = function
2459 | HCprev -> cbget cb ~-1
2460 | HCnext -> cbget cb 1
2461 | HCfirst -> cbget cb ~-(cb.rc)
2462 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2463 and cancel () = cb.rc <- rc
2464 in (action, cancel)
2467 let search pattern forward =
2468 if String.length pattern > 0
2469 then
2470 let pn, py =
2471 match state.layout with
2472 | [] -> 0, 0
2473 | l :: _ ->
2474 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2476 wcmd "search %d %d %d %d,%s\000"
2477 (btod conf.icase) pn py (btod forward) pattern;
2480 let intentry text key =
2481 let c =
2482 if key >= 32 && key < 127
2483 then Char.chr key
2484 else '\000'
2486 match c with
2487 | '0' .. '9' ->
2488 let text = addchar text c in
2489 TEcont text
2491 | _ ->
2492 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2493 TEcont text
2496 let linknentry text key =
2497 let c =
2498 if key >= 32 && key < 127
2499 then Char.chr key
2500 else '\000'
2502 match c with
2503 | 'a' .. 'z' ->
2504 let text = addchar text c in
2505 TEcont text
2507 | _ ->
2508 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2509 TEcont text
2512 let linkndone f s =
2513 if String.length s > 0
2514 then (
2515 let n =
2516 let l = String.length s in
2517 let rec loop pos n = if pos = l then n else
2518 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2519 loop (pos+1) (n*26 + m)
2520 in loop 0 0
2522 let rec loop n = function
2523 | [] -> ()
2524 | l :: rest ->
2525 match getopaque l.pageno with
2526 | None -> loop n rest
2527 | Some opaque ->
2528 let m = getlinkcount opaque in
2529 if n < m
2530 then (
2531 let under = getlink opaque n in
2532 f under
2534 else loop (n-m) rest
2536 loop n state.layout;
2540 let textentry text key =
2541 if key land 0xff00 = 0xff00
2542 then TEcont text
2543 else TEcont (text ^ Wsi.toutf8 key)
2546 let reqlayout angle proportional =
2547 match state.throttle with
2548 | None ->
2549 if nogeomcmds state.geomcmds
2550 then state.anchor <- getanchor ();
2551 conf.angle <- angle mod 360;
2552 if conf.angle != 0
2553 then (
2554 match state.mode with
2555 | LinkNav _ -> state.mode <- View
2556 | _ -> ()
2558 conf.proportional <- proportional;
2559 invalidate "reqlayout"
2560 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2561 | _ -> ()
2564 let settrim trimmargins trimfuzz =
2565 if nogeomcmds state.geomcmds
2566 then state.anchor <- getanchor ();
2567 conf.trimmargins <- trimmargins;
2568 conf.trimfuzz <- trimfuzz;
2569 let x0, y0, x1, y1 = trimfuzz in
2570 invalidate "settrim"
2571 (fun () ->
2572 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2573 Hashtbl.iter (fun _ opaque ->
2574 wcmd "freepage %s" opaque;
2575 ) state.pagemap;
2576 Hashtbl.clear state.pagemap;
2579 let setzoom zoom =
2580 match state.throttle with
2581 | None ->
2582 let zoom = max 0.01 zoom in
2583 if zoom <> conf.zoom
2584 then (
2585 state.prevzoom <- conf.zoom;
2586 conf.zoom <- zoom;
2587 reshape conf.winw conf.winh;
2588 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2591 | Some (layout, y, started) ->
2592 let time =
2593 match conf.maxwait with
2594 | None -> 0.0
2595 | Some t -> t
2597 let dt = now () -. started in
2598 if dt > time
2599 then (
2600 state.y <- y;
2601 load layout;
2605 let setcolumns mode columns coverA coverB =
2606 if columns < 0
2607 then (
2608 if isbirdseye mode
2609 then showtext '!' "split mode doesn't work in bird's eye"
2610 else (
2611 conf.columns <- Csplit (-columns, [||]);
2612 state.x <- 0;
2613 conf.zoom <- 1.0;
2616 else (
2617 if columns < 2
2618 then (
2619 conf.columns <- Csingle;
2620 state.x <- 0;
2621 setzoom 1.0;
2623 else (
2624 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2625 conf.zoom <- 1.0;
2628 reshape conf.winw conf.winh;
2631 let enterbirdseye () =
2632 let zoom = float conf.thumbw /. float conf.winw in
2633 let birdseyepageno =
2634 let cy = conf.winh / 2 in
2635 let fold = function
2636 | [] -> 0
2637 | l :: rest ->
2638 let rec fold best = function
2639 | [] -> best.pageno
2640 | l :: rest ->
2641 let d = cy - (l.pagedispy + l.pagevh/2)
2642 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2643 if abs d < abs dbest
2644 then fold l rest
2645 else best.pageno
2646 in fold l rest
2648 fold state.layout
2650 state.mode <- Birdseye (
2651 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2653 conf.zoom <- zoom;
2654 conf.presentation <- false;
2655 conf.interpagespace <- 10;
2656 conf.hlinks <- false;
2657 state.x <- 0;
2658 state.mstate <- Mnone;
2659 conf.maxwait <- None;
2660 conf.columns <- (
2661 match conf.beyecolumns with
2662 | Some c ->
2663 conf.zoom <- 1.0;
2664 Cmulti ((c, 0, 0), [||])
2665 | None -> Csingle
2667 Wsi.setcursor Wsi.CURSOR_INHERIT;
2668 if conf.verbose
2669 then
2670 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2671 (100.0*.zoom)
2672 else
2673 state.text <- ""
2675 reshape conf.winw conf.winh;
2678 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2679 state.mode <- View;
2680 conf.zoom <- c.zoom;
2681 conf.presentation <- c.presentation;
2682 conf.interpagespace <- c.interpagespace;
2683 conf.maxwait <- c.maxwait;
2684 conf.hlinks <- c.hlinks;
2685 conf.beyecolumns <- (
2686 match conf.columns with
2687 | Cmulti ((c, _, _), _) -> Some c
2688 | Csingle -> None
2689 | Csplit _ -> failwith "leaving bird's eye split mode"
2691 conf.columns <- (
2692 match c.columns with
2693 | Cmulti (c, _) -> Cmulti (c, [||])
2694 | Csingle -> Csingle
2695 | Csplit (c, _) -> Csplit (c, [||])
2697 state.x <- leftx;
2698 if conf.verbose
2699 then
2700 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2701 (100.0*.conf.zoom)
2703 reshape conf.winw conf.winh;
2704 state.anchor <- if goback then anchor else (pageno, 0.0);
2707 let togglebirdseye () =
2708 match state.mode with
2709 | Birdseye vals -> leavebirdseye vals true
2710 | View -> enterbirdseye ()
2711 | _ -> ()
2714 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2715 let pageno = max 0 (pageno - incr) in
2716 let rec loop = function
2717 | [] -> gotopage1 pageno 0
2718 | l :: _ when l.pageno = pageno ->
2719 if l.pagedispy >= 0 && l.pagey = 0
2720 then G.postRedisplay "upbirdseye"
2721 else gotopage1 pageno 0
2722 | _ :: rest -> loop rest
2724 loop state.layout;
2725 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2728 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2729 let pageno = min (state.pagecount - 1) (pageno + incr) in
2730 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2731 let rec loop = function
2732 | [] ->
2733 let y, h = getpageyh pageno in
2734 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2735 gotoy (clamp dy)
2736 | l :: _ when l.pageno = pageno ->
2737 if l.pagevh != l.pageh
2738 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2739 else G.postRedisplay "downbirdseye"
2740 | _ :: rest -> loop rest
2742 loop state.layout
2745 let optentry mode _ key =
2746 let btos b = if b then "on" else "off" in
2747 if key >= 32 && key < 127
2748 then
2749 let c = Char.chr key in
2750 match c with
2751 | 's' ->
2752 let ondone s =
2753 try conf.scrollstep <- int_of_string s with exc ->
2754 state.text <- Printf.sprintf "bad integer `%s': %s"
2755 s (Printexc.to_string exc)
2757 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2759 | 'A' ->
2760 let ondone s =
2762 conf.autoscrollstep <- int_of_string s;
2763 if state.autoscroll <> None
2764 then state.autoscroll <- Some conf.autoscrollstep
2765 with exc ->
2766 state.text <- Printf.sprintf "bad integer `%s': %s"
2767 s (Printexc.to_string exc)
2769 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2771 | 'C' ->
2772 let mode = state.mode in
2773 let ondone s =
2775 let n, a, b = multicolumns_of_string s in
2776 setcolumns mode n a b;
2777 with exc ->
2778 state.text <- Printf.sprintf "bad columns `%s': %s"
2779 s (Printexc.to_string exc)
2781 TEswitch ("columns: ", "", None, textentry, ondone, true)
2783 | 'Z' ->
2784 let ondone s =
2786 let zoom = float (int_of_string s) /. 100.0 in
2787 setzoom zoom
2788 with exc ->
2789 state.text <- Printf.sprintf "bad integer `%s': %s"
2790 s (Printexc.to_string exc)
2792 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2794 | 't' ->
2795 let ondone s =
2797 conf.thumbw <- bound (int_of_string s) 2 4096;
2798 state.text <-
2799 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2800 begin match mode with
2801 | Birdseye beye ->
2802 leavebirdseye beye false;
2803 enterbirdseye ();
2804 | _ -> ();
2806 with exc ->
2807 state.text <- Printf.sprintf "bad integer `%s': %s"
2808 s (Printexc.to_string exc)
2810 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2812 | 'R' ->
2813 let ondone s =
2814 match try
2815 Some (int_of_string s)
2816 with exc ->
2817 state.text <- Printf.sprintf "bad integer `%s': %s"
2818 s (Printexc.to_string exc);
2819 None
2820 with
2821 | Some angle -> reqlayout angle conf.proportional
2822 | None -> ()
2824 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2826 | 'i' ->
2827 conf.icase <- not conf.icase;
2828 TEdone ("case insensitive search " ^ (btos conf.icase))
2830 | 'p' ->
2831 conf.preload <- not conf.preload;
2832 gotoy state.y;
2833 TEdone ("preload " ^ (btos conf.preload))
2835 | 'v' ->
2836 conf.verbose <- not conf.verbose;
2837 TEdone ("verbose " ^ (btos conf.verbose))
2839 | 'd' ->
2840 conf.debug <- not conf.debug;
2841 TEdone ("debug " ^ (btos conf.debug))
2843 | 'h' ->
2844 conf.maxhfit <- not conf.maxhfit;
2845 state.maxy <- calcheight ();
2846 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2848 | 'c' ->
2849 conf.crophack <- not conf.crophack;
2850 TEdone ("crophack " ^ btos conf.crophack)
2852 | 'a' ->
2853 let s =
2854 match conf.maxwait with
2855 | None ->
2856 conf.maxwait <- Some infinity;
2857 "always wait for page to complete"
2858 | Some _ ->
2859 conf.maxwait <- None;
2860 "show placeholder if page is not ready"
2862 TEdone s
2864 | 'f' ->
2865 conf.underinfo <- not conf.underinfo;
2866 TEdone ("underinfo " ^ btos conf.underinfo)
2868 | 'P' ->
2869 conf.savebmarks <- not conf.savebmarks;
2870 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2872 | 'S' ->
2873 let ondone s =
2875 let pageno, py =
2876 match state.layout with
2877 | [] -> 0, 0
2878 | l :: _ ->
2879 l.pageno, l.pagey
2881 conf.interpagespace <- int_of_string s;
2882 docolumns conf.columns;
2883 state.maxy <- calcheight ();
2884 let y = getpagey pageno in
2885 gotoy (y + py)
2886 with exc ->
2887 state.text <- Printf.sprintf "bad integer `%s': %s"
2888 s (Printexc.to_string exc)
2890 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2892 | 'l' ->
2893 reqlayout conf.angle (not conf.proportional);
2894 TEdone ("proportional display " ^ btos conf.proportional)
2896 | 'T' ->
2897 settrim (not conf.trimmargins) conf.trimfuzz;
2898 TEdone ("trim margins " ^ btos conf.trimmargins)
2900 | 'I' ->
2901 conf.invert <- not conf.invert;
2902 TEdone ("invert colors " ^ btos conf.invert)
2904 | 'x' ->
2905 let ondone s =
2906 cbput state.hists.sel s;
2907 conf.selcmd <- s;
2909 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2910 textentry, ondone, true)
2912 | 'F' ->
2913 conf.fullsplit <- not conf.fullsplit;
2914 gotoy state.y;
2915 TEdone ("full split " ^ btos conf.fullsplit)
2917 | _ ->
2918 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2919 TEstop
2920 else
2921 TEcont state.text
2924 class type lvsource = object
2925 method getitemcount : int
2926 method getitem : int -> (string * int)
2927 method hasaction : int -> bool
2928 method exit :
2929 uioh:uioh ->
2930 cancel:bool ->
2931 active:int ->
2932 first:int ->
2933 pan:int ->
2934 qsearch:string ->
2935 uioh option
2936 method getactive : int
2937 method getfirst : int
2938 method getqsearch : string
2939 method setqsearch : string -> unit
2940 method getpan : int
2941 end;;
2943 class virtual lvsourcebase = object
2944 val mutable m_active = 0
2945 val mutable m_first = 0
2946 val mutable m_qsearch = ""
2947 val mutable m_pan = 0
2948 method getactive = m_active
2949 method getfirst = m_first
2950 method getqsearch = m_qsearch
2951 method getpan = m_pan
2952 method setqsearch s = m_qsearch <- s
2953 end;;
2955 let withoutlastutf8 s =
2956 let len = String.length s in
2957 if len = 0
2958 then s
2959 else
2960 let rec find pos =
2961 if pos = 0
2962 then pos
2963 else
2964 let b = Char.code s.[pos] in
2965 if b land 0b110000 = 0b11000000
2966 then find (pos-1)
2967 else pos-1
2969 let first =
2970 if Char.code s.[len-1] land 0x80 = 0
2971 then len-1
2972 else find (len-1)
2974 String.sub s 0 first;
2977 let textentrykeyboard
2978 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2979 let enttext te =
2980 state.mode <- Textentry (te, onleave);
2981 state.text <- "";
2982 enttext ();
2983 G.postRedisplay "textentrykeyboard enttext";
2985 let histaction cmd =
2986 match opthist with
2987 | None -> ()
2988 | Some (action, _) ->
2989 state.mode <- Textentry (
2990 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2992 G.postRedisplay "textentry histaction"
2994 match key with
2995 | 0xff08 -> (* backspace *)
2996 let s = withoutlastutf8 text in
2997 let len = String.length s in
2998 if cancelonempty && len = 0
2999 then (
3000 onleave Cancel;
3001 G.postRedisplay "textentrykeyboard after cancel";
3003 else (
3004 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3007 | 0xff0d ->
3008 ondone text;
3009 onleave Confirm;
3010 G.postRedisplay "textentrykeyboard after confirm"
3012 | 0xff52 -> histaction HCprev
3013 | 0xff54 -> histaction HCnext
3014 | 0xff50 -> histaction HCfirst
3015 | 0xff57 -> histaction HClast
3017 | 0xff1b -> (* escape*)
3018 if String.length text = 0
3019 then (
3020 begin match opthist with
3021 | None -> ()
3022 | Some (_, onhistcancel) -> onhistcancel ()
3023 end;
3024 onleave Cancel;
3025 state.text <- "";
3026 G.postRedisplay "textentrykeyboard after cancel2"
3028 else (
3029 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3032 | 0xff9f | 0xffff -> () (* delete *)
3034 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3035 begin match onkey text key with
3036 | TEdone text ->
3037 ondone text;
3038 onleave Confirm;
3039 G.postRedisplay "textentrykeyboard after confirm2";
3041 | TEcont text ->
3042 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3044 | TEstop ->
3045 onleave Cancel;
3046 G.postRedisplay "textentrykeyboard after cancel3"
3048 | TEswitch te ->
3049 state.mode <- Textentry (te, onleave);
3050 G.postRedisplay "textentrykeyboard switch";
3051 end;
3053 | _ ->
3054 vlog "unhandled key %s" (Wsi.keyname key)
3057 let firstof first active =
3058 if first > active || abs (first - active) > fstate.maxrows - 1
3059 then max 0 (active - (fstate.maxrows/2))
3060 else first
3063 let calcfirst first active =
3064 if active > first
3065 then
3066 let rows = active - first in
3067 if rows > fstate.maxrows then active - fstate.maxrows else first
3068 else active
3071 let scrollph y maxy =
3072 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3073 let sh = float conf.winh /. sh in
3074 let sh = max sh (float conf.scrollh) in
3076 let percent =
3077 if y = state.maxy
3078 then 1.0
3079 else float y /. float maxy
3081 let position = (float conf.winh -. sh) *. percent in
3083 let position =
3084 if position +. sh > float conf.winh
3085 then float conf.winh -. sh
3086 else position
3088 position, sh;
3091 let coe s = (s :> uioh);;
3093 class listview ~(source:lvsource) ~trusted ~modehash =
3094 object (self)
3095 val m_pan = source#getpan
3096 val m_first = source#getfirst
3097 val m_active = source#getactive
3098 val m_qsearch = source#getqsearch
3099 val m_prev_uioh = state.uioh
3101 method private elemunder y =
3102 let n = y / (fstate.fontsize+1) in
3103 if m_first + n < source#getitemcount
3104 then (
3105 if source#hasaction (m_first + n)
3106 then Some (m_first + n)
3107 else None
3109 else None
3111 method display =
3112 Gl.enable `blend;
3113 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3114 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3115 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3116 GlDraw.color (1., 1., 1.);
3117 Gl.enable `texture_2d;
3118 let fs = fstate.fontsize in
3119 let nfs = fs + 1 in
3120 let ww = fstate.wwidth in
3121 let tabw = 30.0*.ww in
3122 let itemcount = source#getitemcount in
3123 let rec loop row =
3124 if (row - m_first) * nfs > conf.winh
3125 then ()
3126 else (
3127 if row >= 0 && row < itemcount
3128 then (
3129 let (s, level) = source#getitem row in
3130 let y = (row - m_first) * nfs in
3131 let x = 5.0 +. float (level + m_pan) *. ww in
3132 if row = m_active
3133 then (
3134 Gl.disable `texture_2d;
3135 GlDraw.polygon_mode `both `line;
3136 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3137 GlDraw.rect (1., float (y + 1))
3138 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3139 GlDraw.polygon_mode `both `fill;
3140 GlDraw.color (1., 1., 1.);
3141 Gl.enable `texture_2d;
3144 let drawtabularstring s =
3145 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3146 if trusted
3147 then
3148 let tabpos = try String.index s '\t' with Not_found -> -1 in
3149 if tabpos > 0
3150 then
3151 let len = String.length s - tabpos - 1 in
3152 let s1 = String.sub s 0 tabpos
3153 and s2 = String.sub s (tabpos + 1) len in
3154 let nx = drawstr x s1 in
3155 let sw = nx -. x in
3156 let x = x +. (max tabw sw) in
3157 drawstr x s2
3158 else
3159 drawstr x s
3160 else
3161 drawstr x s
3163 let _ = drawtabularstring s in
3164 loop (row+1)
3168 loop m_first;
3169 Gl.disable `blend;
3170 Gl.disable `texture_2d;
3172 method updownlevel incr =
3173 let len = source#getitemcount in
3174 let curlevel =
3175 if m_active >= 0 && m_active < len
3176 then snd (source#getitem m_active)
3177 else -1
3179 let rec flow i =
3180 if i = len then i-1 else if i = -1 then 0 else
3181 let _, l = source#getitem i in
3182 if l != curlevel then i else flow (i+incr)
3184 let active = flow m_active in
3185 let first = calcfirst m_first active in
3186 G.postRedisplay "outline updownlevel";
3187 {< m_active = active; m_first = first >}
3189 method private key1 key mask =
3190 let set1 active first qsearch =
3191 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3193 let search active pattern incr =
3194 let dosearch re =
3195 let rec loop n =
3196 if n >= 0 && n < source#getitemcount
3197 then (
3198 let s, _ = source#getitem n in
3200 (try ignore (Str.search_forward re s 0); true
3201 with Not_found -> false)
3202 then Some n
3203 else loop (n + incr)
3205 else None
3207 loop active
3210 let re = Str.regexp_case_fold pattern in
3211 dosearch re
3212 with Failure s ->
3213 state.text <- s;
3214 None
3216 let itemcount = source#getitemcount in
3217 let find start incr =
3218 let rec find i =
3219 if i = -1 || i = itemcount
3220 then -1
3221 else (
3222 if source#hasaction i
3223 then i
3224 else find (i + incr)
3227 find start
3229 let set active first =
3230 let first = bound first 0 (itemcount - fstate.maxrows) in
3231 state.text <- "";
3232 coe {< m_active = active; m_first = first >}
3234 let navigate incr =
3235 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3236 let active, first =
3237 let incr1 = if incr > 0 then 1 else -1 in
3238 if isvisible m_first m_active
3239 then
3240 let next =
3241 let next = m_active + incr in
3242 let next =
3243 if next < 0 || next >= itemcount
3244 then -1
3245 else find next incr1
3247 if next = -1 || abs (m_active - next) > fstate.maxrows
3248 then -1
3249 else next
3251 if next = -1
3252 then
3253 let first = m_first + incr in
3254 let first = bound first 0 (itemcount - 1) in
3255 let next =
3256 let next = m_active + incr in
3257 let next = bound next 0 (itemcount - 1) in
3258 find next ~-incr1
3260 let active = if next = -1 then m_active else next in
3261 active, first
3262 else
3263 let first = min next m_first in
3264 let first =
3265 if abs (next - first) > fstate.maxrows
3266 then first + incr
3267 else first
3269 next, first
3270 else
3271 let first = m_first + incr in
3272 let first = bound first 0 (itemcount - 1) in
3273 let active =
3274 let next = m_active + incr in
3275 let next = bound next 0 (itemcount - 1) in
3276 let next = find next incr1 in
3277 let active =
3278 if next = -1 || abs (m_active - first) > fstate.maxrows
3279 then (
3280 let active = if m_active = -1 then next else m_active in
3281 active
3283 else next
3285 if isvisible first active
3286 then active
3287 else -1
3289 active, first
3291 G.postRedisplay "listview navigate";
3292 set active first;
3294 match key with
3295 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3296 let incr = if key = 0x72 then -1 else 1 in
3297 let active, first =
3298 match search (m_active + incr) m_qsearch incr with
3299 | None ->
3300 state.text <- m_qsearch ^ " [not found]";
3301 m_active, m_first
3302 | Some active ->
3303 state.text <- m_qsearch;
3304 active, firstof m_first active
3306 G.postRedisplay "listview ctrl-r/s";
3307 set1 active first m_qsearch;
3309 | 0xff08 -> (* backspace *)
3310 if String.length m_qsearch = 0
3311 then coe self
3312 else (
3313 let qsearch = withoutlastutf8 m_qsearch in
3314 let len = String.length qsearch in
3315 if len = 0
3316 then (
3317 state.text <- "";
3318 G.postRedisplay "listview empty qsearch";
3319 set1 m_active m_first "";
3321 else
3322 let active, first =
3323 match search m_active qsearch ~-1 with
3324 | None ->
3325 state.text <- qsearch ^ " [not found]";
3326 m_active, m_first
3327 | Some active ->
3328 state.text <- qsearch;
3329 active, firstof m_first active
3331 G.postRedisplay "listview backspace qsearch";
3332 set1 active first qsearch
3335 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3336 let pattern = m_qsearch ^ Wsi.toutf8 key in
3337 let active, first =
3338 match search m_active pattern 1 with
3339 | None ->
3340 state.text <- pattern ^ " [not found]";
3341 m_active, m_first
3342 | Some active ->
3343 state.text <- pattern;
3344 active, firstof m_first active
3346 G.postRedisplay "listview qsearch add";
3347 set1 active first pattern;
3349 | 0xff1b -> (* escape *)
3350 state.text <- "";
3351 if String.length m_qsearch = 0
3352 then (
3353 G.postRedisplay "list view escape";
3354 begin
3355 match
3356 source#exit (coe self) true m_active m_first m_pan m_qsearch
3357 with
3358 | None -> m_prev_uioh
3359 | Some uioh -> uioh
3362 else (
3363 G.postRedisplay "list view kill qsearch";
3364 source#setqsearch "";
3365 coe {< m_qsearch = "" >}
3368 | 0xff0d -> (* return *)
3369 state.text <- "";
3370 let self = {< m_qsearch = "" >} in
3371 source#setqsearch "";
3372 let opt =
3373 G.postRedisplay "listview enter";
3374 if m_active >= 0 && m_active < source#getitemcount
3375 then (
3376 source#exit (coe self) false m_active m_first m_pan "";
3378 else (
3379 source#exit (coe self) true m_active m_first m_pan "";
3382 begin match opt with
3383 | None -> m_prev_uioh
3384 | Some uioh -> uioh
3387 | 0xff9f | 0xffff -> (* delete *)
3388 coe self
3390 | 0xff52 -> navigate ~-1 (* up *)
3391 | 0xff54 -> navigate 1 (* down *)
3392 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3393 | 0xff56 -> navigate fstate.maxrows (* next *)
3395 | 0xff53 -> (* right *)
3396 state.text <- "";
3397 G.postRedisplay "listview right";
3398 coe {< m_pan = m_pan - 1 >}
3400 | 0xff51 -> (* left *)
3401 state.text <- "";
3402 G.postRedisplay "listview left";
3403 coe {< m_pan = m_pan + 1 >}
3405 | 0xff50 -> (* home *)
3406 let active = find 0 1 in
3407 G.postRedisplay "listview home";
3408 set active 0;
3410 | 0xff57 -> (* end *)
3411 let first = max 0 (itemcount - fstate.maxrows) in
3412 let active = find (itemcount - 1) ~-1 in
3413 G.postRedisplay "listview end";
3414 set active first;
3416 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3417 coe self
3419 | _ ->
3420 dolog "listview unknown key %#x" key; coe self
3422 method key key mask =
3423 match state.mode with
3424 | Textentry te -> textentrykeyboard key mask te; coe self
3425 | _ -> self#key1 key mask
3427 method button button down x y _ =
3428 let opt =
3429 match button with
3430 | 1 when x > conf.winw - conf.scrollbw ->
3431 G.postRedisplay "listview scroll";
3432 if down
3433 then
3434 let _, position, sh = self#scrollph in
3435 if y > truncate position && y < truncate (position +. sh)
3436 then (
3437 state.mstate <- Mscrolly;
3438 Some (coe self)
3440 else
3441 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3442 let first = truncate (s *. float source#getitemcount) in
3443 let first = min source#getitemcount first in
3444 Some (coe {< m_first = first; m_active = first >})
3445 else (
3446 state.mstate <- Mnone;
3447 Some (coe self);
3449 | 1 when not down ->
3450 begin match self#elemunder y with
3451 | Some n ->
3452 G.postRedisplay "listview click";
3453 source#exit
3454 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3455 | _ ->
3456 Some (coe self)
3458 | n when (n == 4 || n == 5) && not down ->
3459 let len = source#getitemcount in
3460 let first =
3461 if n = 5 && m_first + fstate.maxrows >= len
3462 then
3463 m_first
3464 else
3465 let first = m_first + (if n == 4 then -1 else 1) in
3466 bound first 0 (len - 1)
3468 G.postRedisplay "listview wheel";
3469 Some (coe {< m_first = first >})
3470 | _ ->
3471 Some (coe self)
3473 match opt with
3474 | None -> m_prev_uioh
3475 | Some uioh -> uioh
3477 method motion _ y =
3478 match state.mstate with
3479 | Mscrolly ->
3480 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3481 let first = truncate (s *. float source#getitemcount) in
3482 let first = min source#getitemcount first in
3483 G.postRedisplay "listview motion";
3484 coe {< m_first = first; m_active = first >}
3485 | _ -> coe self
3487 method pmotion x y =
3488 if x < conf.winw - conf.scrollbw
3489 then
3490 let n =
3491 match self#elemunder y with
3492 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3493 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3495 let o =
3496 if n != m_active
3497 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3498 else self
3500 coe o
3501 else (
3502 Wsi.setcursor Wsi.CURSOR_INHERIT;
3503 coe self
3506 method infochanged _ = ()
3508 method scrollpw = (0, 0.0, 0.0)
3509 method scrollph =
3510 let nfs = fstate.fontsize + 1 in
3511 let y = m_first * nfs in
3512 let itemcount = source#getitemcount in
3513 let maxi = max 0 (itemcount - fstate.maxrows) in
3514 let maxy = maxi * nfs in
3515 let p, h = scrollph y maxy in
3516 conf.scrollbw, p, h
3518 method modehash = modehash
3519 end;;
3521 class outlinelistview ~source =
3522 object (self)
3523 inherit listview
3524 ~source:(source :> lvsource)
3525 ~trusted:false
3526 ~modehash:(findkeyhash conf "outline")
3527 as super
3529 method key key mask =
3530 let calcfirst first active =
3531 if active > first
3532 then
3533 let rows = active - first in
3534 if rows > fstate.maxrows then active - fstate.maxrows else first
3535 else active
3537 let navigate incr =
3538 let active = m_active + incr in
3539 let active = bound active 0 (source#getitemcount - 1) in
3540 let first = calcfirst m_first active in
3541 G.postRedisplay "outline navigate";
3542 coe {< m_active = active; m_first = first >}
3544 let ctrl = Wsi.withctrl mask in
3545 match key with
3546 | 110 when ctrl -> (* ctrl-n *)
3547 source#narrow m_qsearch;
3548 G.postRedisplay "outline ctrl-n";
3549 coe {< m_first = 0; m_active = 0 >}
3551 | 117 when ctrl -> (* ctrl-u *)
3552 source#denarrow;
3553 G.postRedisplay "outline ctrl-u";
3554 state.text <- "";
3555 coe {< m_first = 0; m_active = 0 >}
3557 | 108 when ctrl -> (* ctrl-l *)
3558 let first = m_active - (fstate.maxrows / 2) in
3559 G.postRedisplay "outline ctrl-l";
3560 coe {< m_first = first >}
3562 | 0xff9f | 0xffff -> (* delete *)
3563 source#remove m_active;
3564 G.postRedisplay "outline delete";
3565 let active = max 0 (m_active-1) in
3566 coe {< m_first = firstof m_first active;
3567 m_active = active >}
3569 | 0xff52 -> navigate ~-1 (* up *)
3570 | 0xff54 -> navigate 1 (* down *)
3571 | 0xff55 -> (* prior *)
3572 navigate ~-(fstate.maxrows)
3573 | 0xff56 -> (* next *)
3574 navigate fstate.maxrows
3576 | 0xff53 -> (* [ctrl-]right *)
3577 let o =
3578 if ctrl
3579 then (
3580 G.postRedisplay "outline ctrl right";
3581 {< m_pan = m_pan + 1 >}
3583 else self#updownlevel 1
3585 coe o
3587 | 0xff51 -> (* [ctrl-]left *)
3588 let o =
3589 if ctrl
3590 then (
3591 G.postRedisplay "outline ctrl left";
3592 {< m_pan = m_pan - 1 >}
3594 else self#updownlevel ~-1
3596 coe o
3598 | 0xff50 -> (* home *)
3599 G.postRedisplay "outline home";
3600 coe {< m_first = 0; m_active = 0 >}
3602 | 0xff57 -> (* end *)
3603 let active = source#getitemcount - 1 in
3604 let first = max 0 (active - fstate.maxrows) in
3605 G.postRedisplay "outline end";
3606 coe {< m_active = active; m_first = first >}
3608 | _ -> super#key key mask
3611 let outlinesource usebookmarks =
3612 let empty = [||] in
3613 (object
3614 inherit lvsourcebase
3615 val mutable m_items = empty
3616 val mutable m_orig_items = empty
3617 val mutable m_prev_items = empty
3618 val mutable m_narrow_pattern = ""
3619 val mutable m_hadremovals = false
3621 method getitemcount =
3622 Array.length m_items + (if m_hadremovals then 1 else 0)
3624 method getitem n =
3625 if n == Array.length m_items && m_hadremovals
3626 then
3627 ("[Confirm removal]", 0)
3628 else
3629 let s, n, _ = m_items.(n) in
3630 (s, n)
3632 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3633 ignore (uioh, first, qsearch);
3634 let confrimremoval = m_hadremovals && active = Array.length m_items in
3635 let items =
3636 if String.length m_narrow_pattern = 0
3637 then m_orig_items
3638 else m_items
3640 if not cancel
3641 then (
3642 if not confrimremoval
3643 then(
3644 let _, _, anchor = m_items.(active) in
3645 gotoanchor anchor;
3646 m_items <- items;
3648 else (
3649 state.bookmarks <- Array.to_list m_items;
3650 m_orig_items <- m_items;
3653 else m_items <- items;
3654 m_pan <- pan;
3655 None
3657 method hasaction _ = true
3659 method greetmsg =
3660 if Array.length m_items != Array.length m_orig_items
3661 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3662 else ""
3664 method narrow pattern =
3665 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3666 match reopt with
3667 | None -> ()
3668 | Some re ->
3669 let rec loop accu n =
3670 if n = -1
3671 then (
3672 m_narrow_pattern <- pattern;
3673 m_items <- Array.of_list accu
3675 else
3676 let (s, _, _) as o = m_items.(n) in
3677 let accu =
3678 if (try ignore (Str.search_forward re s 0); true
3679 with Not_found -> false)
3680 then o :: accu
3681 else accu
3683 loop accu (n-1)
3685 loop [] (Array.length m_items - 1)
3687 method denarrow =
3688 m_orig_items <- (
3689 if usebookmarks
3690 then Array.of_list state.bookmarks
3691 else state.outlines
3693 m_items <- m_orig_items
3695 method remove m =
3696 if usebookmarks
3697 then
3698 if m >= 0 && m < Array.length m_items
3699 then (
3700 m_hadremovals <- true;
3701 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3702 let n = if n >= m then n+1 else n in
3703 m_items.(n)
3707 method reset anchor items =
3708 m_hadremovals <- false;
3709 if m_orig_items == empty || m_prev_items != items
3710 then (
3711 m_orig_items <- items;
3712 if String.length m_narrow_pattern = 0
3713 then m_items <- items;
3715 m_prev_items <- items;
3716 let rely = getanchory anchor in
3717 let active =
3718 let rec loop n best bestd =
3719 if n = Array.length m_items
3720 then best
3721 else
3722 let (_, _, anchor) = m_items.(n) in
3723 let orely = getanchory anchor in
3724 let d = abs (orely - rely) in
3725 if d < bestd
3726 then loop (n+1) n d
3727 else loop (n+1) best bestd
3729 loop 0 ~-1 max_int
3731 m_active <- active;
3732 m_first <- firstof m_first active
3733 end)
3736 let enterselector usebookmarks =
3737 let source = outlinesource usebookmarks in
3738 fun errmsg ->
3739 let outlines =
3740 if usebookmarks
3741 then Array.of_list state.bookmarks
3742 else state.outlines
3744 if Array.length outlines = 0
3745 then (
3746 showtext ' ' errmsg;
3748 else (
3749 state.text <- source#greetmsg;
3750 Wsi.setcursor Wsi.CURSOR_INHERIT;
3751 let anchor = getanchor () in
3752 source#reset anchor outlines;
3753 state.uioh <- coe (new outlinelistview ~source);
3754 G.postRedisplay "enter selector";
3758 let enteroutlinemode =
3759 let f = enterselector false in
3760 fun ()-> f "Document has no outline";
3763 let enterbookmarkmode =
3764 let f = enterselector true in
3765 fun () -> f "Document has no bookmarks (yet)";
3768 let color_of_string s =
3769 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3770 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3774 let color_to_string (r, g, b) =
3775 let r = truncate (r *. 256.0)
3776 and g = truncate (g *. 256.0)
3777 and b = truncate (b *. 256.0) in
3778 Printf.sprintf "%d/%d/%d" r g b
3781 let irect_of_string s =
3782 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3785 let irect_to_string (x0,y0,x1,y1) =
3786 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3789 let makecheckers () =
3790 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3791 following to say:
3792 converted by Issac Trotts. July 25, 2002 *)
3793 let image_height = 64
3794 and image_width = 64 in
3796 let make_image () =
3797 let image =
3798 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3800 for i = 0 to image_width - 1 do
3801 for j = 0 to image_height - 1 do
3802 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3803 (if (i land 8 ) lxor (j land 8) = 0
3804 then [|255;255;255|] else [|200;200;200|])
3805 done
3806 done;
3807 image
3809 let image = make_image () in
3810 let id = GlTex.gen_texture () in
3811 GlTex.bind_texture `texture_2d id;
3812 GlPix.store (`unpack_alignment 1);
3813 GlTex.image2d image;
3814 List.iter (GlTex.parameter ~target:`texture_2d)
3815 [ `wrap_s `repeat;
3816 `wrap_t `repeat;
3817 `mag_filter `nearest;
3818 `min_filter `nearest ];
3822 let setcheckers enabled =
3823 match state.texid with
3824 | None ->
3825 if enabled then state.texid <- Some (makecheckers ())
3827 | Some texid ->
3828 if not enabled
3829 then (
3830 GlTex.delete_texture texid;
3831 state.texid <- None;
3835 let int_of_string_with_suffix s =
3836 let l = String.length s in
3837 let s1, shift =
3838 if l > 1
3839 then
3840 let suffix = Char.lowercase s.[l-1] in
3841 match suffix with
3842 | 'k' -> String.sub s 0 (l-1), 10
3843 | 'm' -> String.sub s 0 (l-1), 20
3844 | 'g' -> String.sub s 0 (l-1), 30
3845 | _ -> s, 0
3846 else s, 0
3848 let n = int_of_string s1 in
3849 let m = n lsl shift in
3850 if m < 0 || m < n
3851 then raise (Failure "value too large")
3852 else m
3855 let string_with_suffix_of_int n =
3856 if n = 0
3857 then "0"
3858 else
3859 let n, s =
3860 if n = 0
3861 then 0, ""
3862 else (
3863 if n land ((1 lsl 20) - 1) = 0
3864 then n lsr 20, "M"
3865 else (
3866 if n land ((1 lsl 10) - 1) = 0
3867 then n lsr 10, "K"
3868 else n, ""
3872 let rec loop s n =
3873 let h = n mod 1000 in
3874 let n = n / 1000 in
3875 if n = 0
3876 then string_of_int h ^ s
3877 else (
3878 let s = Printf.sprintf "_%03d%s" h s in
3879 loop s n
3882 loop "" n ^ s;
3885 let defghyllscroll = (40, 8, 32);;
3886 let ghyllscroll_of_string s =
3887 let (n, a, b) as nab =
3888 if s = "default"
3889 then defghyllscroll
3890 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3892 if n <= a || n <= b || a >= b
3893 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3894 nab;
3897 let ghyllscroll_to_string ((n, a, b) as nab) =
3898 if nab = defghyllscroll
3899 then "default"
3900 else Printf.sprintf "%d,%d,%d" n a b;
3903 let describe_location () =
3904 let f (fn, _) l =
3905 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3907 let fn, ln = List.fold_left f (-1, -1) state.layout in
3908 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3909 let percent =
3910 if maxy <= 0
3911 then 100.
3912 else (100. *. (float state.y /. float maxy))
3914 if fn = ln
3915 then
3916 Printf.sprintf "page %d of %d [%.2f%%]"
3917 (fn+1) state.pagecount percent
3918 else
3919 Printf.sprintf
3920 "pages %d-%d of %d [%.2f%%]"
3921 (fn+1) (ln+1) state.pagecount percent
3924 let enterinfomode =
3925 let btos b = if b then "\xe2\x88\x9a" else "" in
3926 let showextended = ref false in
3927 let leave mode = function
3928 | Confirm -> state.mode <- mode
3929 | Cancel -> state.mode <- mode in
3930 let src =
3931 (object
3932 val mutable m_first_time = true
3933 val mutable m_l = []
3934 val mutable m_a = [||]
3935 val mutable m_prev_uioh = nouioh
3936 val mutable m_prev_mode = View
3938 inherit lvsourcebase
3940 method reset prev_mode prev_uioh =
3941 m_a <- Array.of_list (List.rev m_l);
3942 m_l <- [];
3943 m_prev_mode <- prev_mode;
3944 m_prev_uioh <- prev_uioh;
3945 if m_first_time
3946 then (
3947 let rec loop n =
3948 if n >= Array.length m_a
3949 then ()
3950 else
3951 match m_a.(n) with
3952 | _, _, _, Action _ -> m_active <- n
3953 | _ -> loop (n+1)
3955 loop 0;
3956 m_first_time <- false;
3959 method int name get set =
3960 m_l <-
3961 (name, `int get, 1, Action (
3962 fun u ->
3963 let ondone s =
3964 try set (int_of_string s)
3965 with exn ->
3966 state.text <- Printf.sprintf "bad integer `%s': %s"
3967 s (Printexc.to_string exn)
3969 state.text <- "";
3970 let te = name ^ ": ", "", None, intentry, ondone, true in
3971 state.mode <- Textentry (te, leave m_prev_mode);
3973 )) :: m_l
3975 method int_with_suffix name get set =
3976 m_l <-
3977 (name, `intws get, 1, Action (
3978 fun u ->
3979 let ondone s =
3980 try set (int_of_string_with_suffix s)
3981 with exn ->
3982 state.text <- Printf.sprintf "bad integer `%s': %s"
3983 s (Printexc.to_string exn)
3985 state.text <- "";
3986 let te =
3987 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3989 state.mode <- Textentry (te, leave m_prev_mode);
3991 )) :: m_l
3993 method bool ?(offset=1) ?(btos=btos) name get set =
3994 m_l <-
3995 (name, `bool (btos, get), offset, Action (
3996 fun u ->
3997 let v = get () in
3998 set (not v);
4000 )) :: m_l
4002 method color name get set =
4003 m_l <-
4004 (name, `color get, 1, Action (
4005 fun u ->
4006 let invalid = (nan, nan, nan) in
4007 let ondone s =
4008 let c =
4009 try color_of_string s
4010 with exn ->
4011 state.text <- Printf.sprintf "bad color `%s': %s"
4012 s (Printexc.to_string exn);
4013 invalid
4015 if c <> invalid
4016 then set c;
4018 let te = name ^ ": ", "", None, textentry, ondone, true in
4019 state.text <- color_to_string (get ());
4020 state.mode <- Textentry (te, leave m_prev_mode);
4022 )) :: m_l
4024 method string name get set =
4025 m_l <-
4026 (name, `string get, 1, Action (
4027 fun u ->
4028 let ondone s = set s in
4029 let te = name ^ ": ", "", None, textentry, ondone, true in
4030 state.mode <- Textentry (te, leave m_prev_mode);
4032 )) :: m_l
4034 method colorspace name get set =
4035 m_l <-
4036 (name, `string get, 1, Action (
4037 fun _ ->
4038 let source =
4039 let vals = [| "rgb"; "bgr"; "gray" |] in
4040 (object
4041 inherit lvsourcebase
4043 initializer
4044 m_active <- int_of_colorspace conf.colorspace;
4045 m_first <- 0;
4047 method getitemcount = Array.length vals
4048 method getitem n = (vals.(n), 0)
4049 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4050 ignore (uioh, first, pan, qsearch);
4051 if not cancel then set active;
4052 None
4053 method hasaction _ = true
4054 end)
4056 state.text <- "";
4057 let modehash = findkeyhash conf "info" in
4058 coe (new listview ~source ~trusted:true ~modehash)
4059 )) :: m_l
4061 method caption s offset =
4062 m_l <- (s, `empty, offset, Noaction) :: m_l
4064 method caption2 s f offset =
4065 m_l <- (s, `string f, offset, Noaction) :: m_l
4067 method getitemcount = Array.length m_a
4069 method getitem n =
4070 let tostr = function
4071 | `int f -> string_of_int (f ())
4072 | `intws f -> string_with_suffix_of_int (f ())
4073 | `string f -> f ()
4074 | `color f -> color_to_string (f ())
4075 | `bool (btos, f) -> btos (f ())
4076 | `empty -> ""
4078 let name, t, offset, _ = m_a.(n) in
4079 ((let s = tostr t in
4080 if String.length s > 0
4081 then Printf.sprintf "%s\t%s" name s
4082 else name),
4083 offset)
4085 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4086 let uiohopt =
4087 if not cancel
4088 then (
4089 m_qsearch <- qsearch;
4090 let uioh =
4091 match m_a.(active) with
4092 | _, _, _, Action f -> f uioh
4093 | _ -> uioh
4095 Some uioh
4097 else None
4099 m_active <- active;
4100 m_first <- first;
4101 m_pan <- pan;
4102 uiohopt
4104 method hasaction n =
4105 match m_a.(n) with
4106 | _, _, _, Action _ -> true
4107 | _ -> false
4108 end)
4110 let rec fillsrc prevmode prevuioh =
4111 let sep () = src#caption "" 0 in
4112 let colorp name get set =
4113 src#string name
4114 (fun () -> color_to_string (get ()))
4115 (fun v ->
4117 let c = color_of_string v in
4118 set c
4119 with exn ->
4120 state.text <- Printf.sprintf "bad color `%s': %s"
4121 v (Printexc.to_string exn);
4124 let oldmode = state.mode in
4125 let birdseye = isbirdseye state.mode in
4127 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4129 src#bool "presentation mode"
4130 (fun () -> conf.presentation)
4131 (fun v ->
4132 conf.presentation <- v;
4133 state.anchor <- getanchor ();
4134 represent ());
4136 src#bool "ignore case in searches"
4137 (fun () -> conf.icase)
4138 (fun v -> conf.icase <- v);
4140 src#bool "preload"
4141 (fun () -> conf.preload)
4142 (fun v -> conf.preload <- v);
4144 src#bool "highlight links"
4145 (fun () -> conf.hlinks)
4146 (fun v -> conf.hlinks <- v);
4148 src#bool "under info"
4149 (fun () -> conf.underinfo)
4150 (fun v -> conf.underinfo <- v);
4152 src#bool "persistent bookmarks"
4153 (fun () -> conf.savebmarks)
4154 (fun v -> conf.savebmarks <- v);
4156 src#bool "proportional display"
4157 (fun () -> conf.proportional)
4158 (fun v -> reqlayout conf.angle v);
4160 src#bool "trim margins"
4161 (fun () -> conf.trimmargins)
4162 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4164 src#bool "persistent location"
4165 (fun () -> conf.jumpback)
4166 (fun v -> conf.jumpback <- v);
4168 sep ();
4169 src#int "inter-page space"
4170 (fun () -> conf.interpagespace)
4171 (fun n ->
4172 conf.interpagespace <- n;
4173 docolumns conf.columns;
4174 let pageno, py =
4175 match state.layout with
4176 | [] -> 0, 0
4177 | l :: _ ->
4178 l.pageno, l.pagey
4180 state.maxy <- calcheight ();
4181 let y = getpagey pageno in
4182 gotoy (y + py)
4185 src#int "page bias"
4186 (fun () -> conf.pagebias)
4187 (fun v -> conf.pagebias <- v);
4189 src#int "scroll step"
4190 (fun () -> conf.scrollstep)
4191 (fun n -> conf.scrollstep <- n);
4193 src#int "auto scroll step"
4194 (fun () ->
4195 match state.autoscroll with
4196 | Some step -> step
4197 | _ -> conf.autoscrollstep)
4198 (fun n ->
4199 if state.autoscroll <> None
4200 then state.autoscroll <- Some n;
4201 conf.autoscrollstep <- n);
4203 src#int "zoom"
4204 (fun () -> truncate (conf.zoom *. 100.))
4205 (fun v -> setzoom ((float v) /. 100.));
4207 src#int "rotation"
4208 (fun () -> conf.angle)
4209 (fun v -> reqlayout v conf.proportional);
4211 src#int "scroll bar width"
4212 (fun () -> state.scrollw)
4213 (fun v ->
4214 state.scrollw <- v;
4215 conf.scrollbw <- v;
4216 reshape conf.winw conf.winh;
4219 src#int "scroll handle height"
4220 (fun () -> conf.scrollh)
4221 (fun v -> conf.scrollh <- v;);
4223 src#int "thumbnail width"
4224 (fun () -> conf.thumbw)
4225 (fun v ->
4226 conf.thumbw <- min 4096 v;
4227 match oldmode with
4228 | Birdseye beye ->
4229 leavebirdseye beye false;
4230 enterbirdseye ()
4231 | _ -> ()
4234 let mode = state.mode in
4235 src#string "columns"
4236 (fun () ->
4237 match conf.columns with
4238 | Csingle -> "1"
4239 | Cmulti (multi, _) -> multicolumns_to_string multi
4240 | Csplit (count, _) -> "-" ^ string_of_int count
4242 (fun v ->
4243 let n, a, b = multicolumns_of_string v in
4244 setcolumns mode n a b);
4246 sep ();
4247 src#caption "Presentation mode" 0;
4248 src#bool "scrollbar visible"
4249 (fun () -> conf.scrollbarinpm)
4250 (fun v ->
4251 if v != conf.scrollbarinpm
4252 then (
4253 conf.scrollbarinpm <- v;
4254 if conf.presentation
4255 then (
4256 state.scrollw <- if v then conf.scrollbw else 0;
4257 reshape conf.winw conf.winh;
4262 sep ();
4263 src#caption "Pixmap cache" 0;
4264 src#int_with_suffix "size (advisory)"
4265 (fun () -> conf.memlimit)
4266 (fun v -> conf.memlimit <- v);
4268 src#caption2 "used"
4269 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4270 (string_with_suffix_of_int state.memused)
4271 (Hashtbl.length state.tilemap)) 1;
4273 sep ();
4274 src#caption "Layout" 0;
4275 src#caption2 "Dimension"
4276 (fun () ->
4277 Printf.sprintf "%dx%d (virtual %dx%d)"
4278 conf.winw conf.winh
4279 state.w state.maxy)
4281 if conf.debug
4282 then
4283 src#caption2 "Position" (fun () ->
4284 Printf.sprintf "%dx%d" state.x state.y
4286 else
4287 src#caption2 "Visible" (fun () -> describe_location ()) 1
4290 sep ();
4291 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4292 "Save these parameters as global defaults at exit"
4293 (fun () -> conf.bedefault)
4294 (fun v -> conf.bedefault <- v)
4297 sep ();
4298 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4299 src#bool ~offset:0 ~btos "Extended parameters"
4300 (fun () -> !showextended)
4301 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4302 if !showextended
4303 then (
4304 src#bool "checkers"
4305 (fun () -> conf.checkers)
4306 (fun v -> conf.checkers <- v; setcheckers v);
4307 src#bool "update cursor"
4308 (fun () -> conf.updatecurs)
4309 (fun v -> conf.updatecurs <- v);
4310 src#bool "verbose"
4311 (fun () -> conf.verbose)
4312 (fun v -> conf.verbose <- v);
4313 src#bool "full split"
4314 (fun () -> conf.fullsplit)
4315 (fun v -> conf.fullsplit <- v; gotoy state.y);
4316 src#bool "invert colors"
4317 (fun () -> conf.invert)
4318 (fun v -> conf.invert <- v);
4319 src#bool "max fit"
4320 (fun () -> conf.maxhfit)
4321 (fun v -> conf.maxhfit <- v);
4322 src#bool "redirect stderr"
4323 (fun () -> conf.redirectstderr)
4324 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4325 src#string "uri launcher"
4326 (fun () -> conf.urilauncher)
4327 (fun v -> conf.urilauncher <- v);
4328 src#string "path launcher"
4329 (fun () -> conf.pathlauncher)
4330 (fun v -> conf.pathlauncher <- v);
4331 src#string "tile size"
4332 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4333 (fun v ->
4335 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4336 conf.tilew <- max 64 w;
4337 conf.tileh <- max 64 h;
4338 flushtiles ();
4339 with exn ->
4340 state.text <- Printf.sprintf "bad tile size `%s': %s"
4341 v (Printexc.to_string exn));
4342 src#int "texture count"
4343 (fun () -> conf.texcount)
4344 (fun v ->
4345 if realloctexts v
4346 then conf.texcount <- v
4347 else showtext '!' " Failed to set texture count please retry later"
4349 src#int "slice height"
4350 (fun () -> conf.sliceheight)
4351 (fun v ->
4352 conf.sliceheight <- v;
4353 wcmd "sliceh %d" conf.sliceheight;
4355 src#int "anti-aliasing level"
4356 (fun () -> conf.aalevel)
4357 (fun v ->
4358 conf.aalevel <- bound v 0 8;
4359 state.anchor <- getanchor ();
4360 opendoc state.path state.password;
4362 src#int "ui font size"
4363 (fun () -> fstate.fontsize)
4364 (fun v -> setfontsize (bound v 5 100));
4365 src#int "hint font size"
4366 (fun () -> conf.hfsize)
4367 (fun v -> conf.hfsize <- bound v 5 100);
4368 colorp "background color"
4369 (fun () -> conf.bgcolor)
4370 (fun v -> conf.bgcolor <- v);
4371 src#bool "crop hack"
4372 (fun () -> conf.crophack)
4373 (fun v -> conf.crophack <- v);
4374 src#string "trim fuzz"
4375 (fun () -> irect_to_string conf.trimfuzz)
4376 (fun v ->
4378 conf.trimfuzz <- irect_of_string v;
4379 if conf.trimmargins
4380 then settrim true conf.trimfuzz;
4381 with exn ->
4382 state.text <- Printf.sprintf "bad irect `%s': %s"
4383 v (Printexc.to_string exn)
4385 src#string "throttle"
4386 (fun () ->
4387 match conf.maxwait with
4388 | None -> "show place holder if page is not ready"
4389 | Some time ->
4390 if time = infinity
4391 then "wait for page to fully render"
4392 else
4393 "wait " ^ string_of_float time
4394 ^ " seconds before showing placeholder"
4396 (fun v ->
4398 let f = float_of_string v in
4399 if f <= 0.0
4400 then conf.maxwait <- None
4401 else conf.maxwait <- Some f
4402 with exn ->
4403 state.text <- Printf.sprintf "bad time `%s': %s"
4404 v (Printexc.to_string exn)
4406 src#string "ghyll scroll"
4407 (fun () ->
4408 match conf.ghyllscroll with
4409 | None -> ""
4410 | Some nab -> ghyllscroll_to_string nab
4412 (fun v ->
4414 let gs =
4415 if String.length v = 0
4416 then None
4417 else Some (ghyllscroll_of_string v)
4419 conf.ghyllscroll <- gs
4420 with exn ->
4421 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4422 v (Printexc.to_string exn)
4424 src#string "selection command"
4425 (fun () -> conf.selcmd)
4426 (fun v -> conf.selcmd <- v);
4427 src#colorspace "color space"
4428 (fun () -> colorspace_to_string conf.colorspace)
4429 (fun v ->
4430 conf.colorspace <- colorspace_of_int v;
4431 wcmd "cs %d" v;
4432 load state.layout;
4436 sep ();
4437 src#caption "Document" 0;
4438 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4439 src#caption2 "Pages"
4440 (fun () -> string_of_int state.pagecount) 1;
4441 src#caption2 "Dimensions"
4442 (fun () -> string_of_int (List.length state.pdims)) 1;
4443 if conf.trimmargins
4444 then (
4445 sep ();
4446 src#caption "Trimmed margins" 0;
4447 src#caption2 "Dimensions"
4448 (fun () -> string_of_int (List.length state.pdims)) 1;
4451 src#reset prevmode prevuioh;
4453 fun () ->
4454 state.text <- "";
4455 let prevmode = state.mode
4456 and prevuioh = state.uioh in
4457 fillsrc prevmode prevuioh;
4458 let source = (src :> lvsource) in
4459 let modehash = findkeyhash conf "info" in
4460 state.uioh <- coe (object (self)
4461 inherit listview ~source ~trusted:true ~modehash as super
4462 val mutable m_prevmemused = 0
4463 method infochanged = function
4464 | Memused ->
4465 if m_prevmemused != state.memused
4466 then (
4467 m_prevmemused <- state.memused;
4468 G.postRedisplay "memusedchanged";
4470 | Pdim -> G.postRedisplay "pdimchanged"
4471 | Docinfo -> fillsrc prevmode prevuioh
4473 method key key mask =
4474 if not (Wsi.withctrl mask)
4475 then
4476 match key with
4477 | 0xff51 -> coe (self#updownlevel ~-1)
4478 | 0xff53 -> coe (self#updownlevel 1)
4479 | _ -> super#key key mask
4480 else super#key key mask
4481 end);
4482 G.postRedisplay "info";
4485 let enterhelpmode =
4486 let source =
4487 (object
4488 inherit lvsourcebase
4489 method getitemcount = Array.length state.help
4490 method getitem n =
4491 let s, n, _ = state.help.(n) in
4492 (s, n)
4494 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4495 let optuioh =
4496 if not cancel
4497 then (
4498 m_qsearch <- qsearch;
4499 match state.help.(active) with
4500 | _, _, Action f -> Some (f uioh)
4501 | _ -> Some (uioh)
4503 else None
4505 m_active <- active;
4506 m_first <- first;
4507 m_pan <- pan;
4508 optuioh
4510 method hasaction n =
4511 match state.help.(n) with
4512 | _, _, Action _ -> true
4513 | _ -> false
4515 initializer
4516 m_active <- -1
4517 end)
4518 in fun () ->
4519 let modehash = findkeyhash conf "help" in
4520 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4521 G.postRedisplay "help";
4524 let entermsgsmode =
4525 let msgsource =
4526 let re = Str.regexp "[\r\n]" in
4527 (object
4528 inherit lvsourcebase
4529 val mutable m_items = [||]
4531 method getitemcount = 1 + Array.length m_items
4533 method getitem n =
4534 if n = 0
4535 then "[Clear]", 0
4536 else m_items.(n-1), 0
4538 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4539 ignore uioh;
4540 if not cancel
4541 then (
4542 if active = 0
4543 then Buffer.clear state.errmsgs;
4544 m_qsearch <- qsearch;
4546 m_active <- active;
4547 m_first <- first;
4548 m_pan <- pan;
4549 None
4551 method hasaction n =
4552 n = 0
4554 method reset =
4555 state.newerrmsgs <- false;
4556 let l = Str.split re (Buffer.contents state.errmsgs) in
4557 m_items <- Array.of_list l
4559 initializer
4560 m_active <- 0
4561 end)
4562 in fun () ->
4563 state.text <- "";
4564 msgsource#reset;
4565 let source = (msgsource :> lvsource) in
4566 let modehash = findkeyhash conf "listview" in
4567 state.uioh <- coe (object
4568 inherit listview ~source ~trusted:false ~modehash as super
4569 method display =
4570 if state.newerrmsgs
4571 then msgsource#reset;
4572 super#display
4573 end);
4574 G.postRedisplay "msgs";
4577 let quickbookmark ?title () =
4578 match state.layout with
4579 | [] -> ()
4580 | l :: _ ->
4581 let title =
4582 match title with
4583 | None ->
4584 let sec = Unix.gettimeofday () in
4585 let tm = Unix.localtime sec in
4586 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4587 (l.pageno+1)
4588 tm.Unix.tm_mday
4589 tm.Unix.tm_mon
4590 (tm.Unix.tm_year + 1900)
4591 tm.Unix.tm_hour
4592 tm.Unix.tm_min
4593 | Some title -> title
4595 state.bookmarks <-
4596 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4597 :: state.bookmarks
4600 let doreshape w h =
4601 state.fullscreen <- None;
4602 Wsi.reshape w h;
4605 let setautoscrollspeed step goingdown =
4606 let incr = max 1 ((abs step) / 2) in
4607 let incr = if goingdown then incr else -incr in
4608 let astep = step + incr in
4609 state.autoscroll <- Some astep;
4612 let gotounder = function
4613 | Ulinkgoto (pageno, top) ->
4614 if pageno >= 0
4615 then (
4616 addnav ();
4617 gotopage1 pageno top;
4620 | Ulinkuri s ->
4621 gotouri s
4623 | Uremote (filename, pageno) ->
4624 let path =
4625 if Sys.file_exists filename
4626 then filename
4627 else
4628 let dir = Filename.dirname state.path in
4629 let path = Filename.concat dir filename in
4630 if Sys.file_exists path
4631 then path
4632 else ""
4634 if String.length path > 0
4635 then (
4636 let anchor = getanchor () in
4637 let ranchor = state.path, state.password, anchor in
4638 state.anchor <- (pageno, 0.0);
4639 state.ranchors <- ranchor :: state.ranchors;
4640 opendoc path "";
4642 else showtext '!' ("Could not find " ^ filename)
4644 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4647 let canpan () =
4648 match conf.columns with
4649 | Csplit _ -> true
4650 | _ -> conf.zoom > 1.0
4653 let viewkeyboard key mask =
4654 let enttext te =
4655 let mode = state.mode in
4656 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4657 state.text <- "";
4658 enttext ();
4659 G.postRedisplay "view:enttext"
4661 let ctrl = Wsi.withctrl mask in
4662 match key with
4663 | 81 -> (* Q *)
4664 exit 0
4666 | 0xff63 -> (* insert *)
4667 if conf.angle mod 360 = 0
4668 then (
4669 state.mode <- LinkNav (Ltgendir 0);
4670 gotoy state.y;
4672 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4674 | 0xff1b | 113 -> (* escape / q *)
4675 begin match state.mstate with
4676 | Mzoomrect _ ->
4677 state.mstate <- Mnone;
4678 Wsi.setcursor Wsi.CURSOR_INHERIT;
4679 G.postRedisplay "kill zoom rect";
4680 | _ ->
4681 match state.ranchors with
4682 | [] -> raise Quit
4683 | (path, password, anchor) :: rest ->
4684 state.ranchors <- rest;
4685 state.anchor <- anchor;
4686 opendoc path password
4687 end;
4689 | 0xff08 -> (* backspace *)
4690 let y = getnav ~-1 in
4691 gotoy_and_clear_text y
4693 | 111 -> (* o *)
4694 enteroutlinemode ()
4696 | 117 -> (* u *)
4697 state.rects <- [];
4698 state.text <- "";
4699 G.postRedisplay "dehighlight";
4701 | 47 | 63 -> (* / ? *)
4702 let ondone isforw s =
4703 cbput state.hists.pat s;
4704 state.searchpattern <- s;
4705 search s isforw
4707 let s = String.create 1 in
4708 s.[0] <- Char.chr key;
4709 enttext (s, "", Some (onhist state.hists.pat),
4710 textentry, ondone (key = 47), true)
4712 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4713 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4714 setzoom (conf.zoom +. incr)
4716 | 43 | 0xffab -> (* + *)
4717 let ondone s =
4718 let n =
4719 try int_of_string s with exc ->
4720 state.text <- Printf.sprintf "bad integer `%s': %s"
4721 s (Printexc.to_string exc);
4722 max_int
4724 if n != max_int
4725 then (
4726 conf.pagebias <- n;
4727 state.text <- "page bias is now " ^ string_of_int n;
4730 enttext ("page bias: ", "", None, intentry, ondone, true)
4732 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4733 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4734 setzoom (max 0.01 (conf.zoom -. decr))
4736 | 45 | 0xffad -> (* - *)
4737 let ondone msg = state.text <- msg in
4738 enttext (
4739 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4740 optentry state.mode, ondone, true
4743 | 48 when ctrl -> (* ctrl-0 *)
4744 setzoom 1.0
4746 | 49 when ctrl -> (* 1 *)
4747 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4748 if zoom < 1.0
4749 then setzoom zoom
4751 | 0xffc6 -> (* f9 *)
4752 togglebirdseye ()
4754 | 57 when ctrl -> (* ctrl-9 *)
4755 togglebirdseye ()
4757 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4758 when not ctrl -> (* 0..9 *)
4759 let ondone s =
4760 let n =
4761 try int_of_string s with exc ->
4762 state.text <- Printf.sprintf "bad integer `%s': %s"
4763 s (Printexc.to_string exc);
4766 if n >= 0
4767 then (
4768 addnav ();
4769 cbput state.hists.pag (string_of_int n);
4770 gotopage1 (n + conf.pagebias - 1) 0;
4773 let pageentry text key =
4774 match Char.unsafe_chr key with
4775 | 'g' -> TEdone text
4776 | _ -> intentry text key
4778 let text = "x" in text.[0] <- Char.chr key;
4779 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4781 | 98 -> (* b *)
4782 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4783 reshape conf.winw conf.winh;
4785 | 108 -> (* l *)
4786 conf.hlinks <- not conf.hlinks;
4787 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4788 G.postRedisplay "toggle highlightlinks";
4790 | 70 -> (* F *)
4791 state.glinks <- true;
4792 let mode = state.mode in
4793 state.mode <- Textentry (
4794 (":", "", None, linknentry, linkndone (fun under ->
4795 addnav ();
4796 gotounder under
4797 ), false
4798 ), fun _ ->
4799 state.glinks <- false;
4800 state.mode <- mode
4802 state.text <- "";
4803 G.postRedisplay "view:linkent(F)"
4805 | 121 -> (* y *)
4806 state.glinks <- true;
4807 let mode = state.mode in
4808 state.mode <- Textentry (
4809 (":", "", None, linknentry, linkndone (fun under ->
4810 match Ne.pipe () with
4811 | Ne.Exn exn ->
4812 showtext '!' (Printf.sprintf "pipe failed: %s"
4813 (Printexc.to_string exn));
4814 | Ne.Res (r, w) ->
4815 let popened =
4816 try popen conf.selcmd [r, 0; w, -1]; true
4817 with exn ->
4818 showtext '!'
4819 (Printf.sprintf "failed to execute %s: %s"
4820 conf.selcmd (Printexc.to_string exn));
4821 false
4823 let clo cap fd =
4824 Ne.clo fd (fun msg ->
4825 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4828 let s = undertext under in
4829 if popened
4830 then
4831 (try
4832 let l = String.length s in
4833 let n = Unix.write w s 0 l in
4834 if n != l
4835 then
4836 showtext '!'
4837 (Printf.sprintf
4838 "failed to write %d characters to sel pipe, wrote %d"
4841 with exn ->
4842 showtext '!'
4843 (Printf.sprintf "failed to write to sel pipe: %s"
4844 (Printexc.to_string exn)
4847 else dolog "%s" s;
4848 clo "pipe/r" r;
4849 clo "pipe/w" w;
4850 ), false
4852 fun _ ->
4853 state.glinks <- false;
4854 state.mode <- mode
4856 state.text <- "";
4857 G.postRedisplay "view:linkent"
4859 | 97 -> (* a *)
4860 begin match state.autoscroll with
4861 | Some step ->
4862 conf.autoscrollstep <- step;
4863 state.autoscroll <- None
4864 | None ->
4865 if conf.autoscrollstep = 0
4866 then state.autoscroll <- Some 1
4867 else state.autoscroll <- Some conf.autoscrollstep
4870 | 112 when ctrl -> (* ctrl-p *)
4871 launchpath ()
4873 | 80 -> (* P *)
4874 conf.presentation <- not conf.presentation;
4875 if conf.presentation
4876 then (
4877 if not conf.scrollbarinpm
4878 then state.scrollw <- 0;
4880 else
4881 state.scrollw <- conf.scrollbw;
4883 showtext ' ' ("presentation mode " ^
4884 if conf.presentation then "on" else "off");
4885 state.anchor <- getanchor ();
4886 represent ()
4888 | 102 -> (* f *)
4889 begin match state.fullscreen with
4890 | None ->
4891 state.fullscreen <- Some (conf.winw, conf.winh);
4892 Wsi.fullscreen ()
4893 | Some (w, h) ->
4894 state.fullscreen <- None;
4895 doreshape w h
4898 | 103 -> (* g *)
4899 gotoy_and_clear_text 0
4901 | 71 -> (* G *)
4902 gotopage1 (state.pagecount - 1) 0
4904 | 112 | 78 -> (* p|N *)
4905 search state.searchpattern false
4907 | 110 | 0xffc0 -> (* n|F3 *)
4908 search state.searchpattern true
4910 | 116 -> (* t *)
4911 begin match state.layout with
4912 | [] -> ()
4913 | l :: _ ->
4914 gotoy_and_clear_text (getpagey l.pageno)
4917 | 32 -> (* ' ' *)
4918 begin match List.rev state.layout with
4919 | [] -> ()
4920 | l :: _ ->
4921 let pageno = min (l.pageno+1) (state.pagecount-1) in
4922 gotoy_and_clear_text (getpagey pageno)
4925 | 0xff9f | 0xffff -> (* delete *)
4926 begin match state.layout with
4927 | [] -> ()
4928 | l :: _ ->
4929 let pageno = max 0 (l.pageno-1) in
4930 gotoy_and_clear_text (getpagey pageno)
4933 | 61 -> (* = *)
4934 showtext ' ' (describe_location ());
4936 | 119 -> (* w *)
4937 begin match state.layout with
4938 | [] -> ()
4939 | l :: _ ->
4940 doreshape (l.pagew + state.scrollw) l.pageh;
4941 G.postRedisplay "w"
4944 | 39 -> (* ' *)
4945 enterbookmarkmode ()
4947 | 104 | 0xffbe -> (* h|F1 *)
4948 enterhelpmode ()
4950 | 105 -> (* i *)
4951 enterinfomode ()
4953 | 101 when conf.redirectstderr -> (* e *)
4954 entermsgsmode ()
4956 | 109 -> (* m *)
4957 let ondone s =
4958 match state.layout with
4959 | l :: _ ->
4960 state.bookmarks <-
4961 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4962 :: state.bookmarks
4963 | _ -> ()
4965 enttext ("bookmark: ", "", None, textentry, ondone, true)
4967 | 126 -> (* ~ *)
4968 quickbookmark ();
4969 showtext ' ' "Quick bookmark added";
4971 | 122 -> (* z *)
4972 begin match state.layout with
4973 | l :: _ ->
4974 let rect = getpdimrect l.pagedimno in
4975 let w, h =
4976 if conf.crophack
4977 then
4978 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4979 truncate (1.2 *. (rect.(3) -. rect.(0))))
4980 else
4981 (truncate (rect.(1) -. rect.(0)),
4982 truncate (rect.(3) -. rect.(0)))
4984 let w = truncate ((float w)*.conf.zoom)
4985 and h = truncate ((float h)*.conf.zoom) in
4986 if w != 0 && h != 0
4987 then (
4988 state.anchor <- getanchor ();
4989 doreshape (w + state.scrollw) (h + conf.interpagespace)
4991 G.postRedisplay "z";
4993 | [] -> ()
4996 | 50 when ctrl -> (* ctrl-2 *)
4997 let maxw = getmaxw () in
4998 if maxw > 0.0
4999 then setzoom (maxw /. float conf.winw)
5001 | 60 | 62 -> (* < > *)
5002 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5004 | 91 | 93 -> (* [ ] *)
5005 conf.colorscale <-
5006 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5008 G.postRedisplay "brightness";
5010 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5011 setzoom state.prevzoom
5013 | 107 | 0xff52 -> (* k up *)
5014 begin match state.autoscroll with
5015 | None ->
5016 begin match state.mode with
5017 | Birdseye beye -> upbirdseye 1 beye
5018 | _ ->
5019 if ctrl
5020 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5021 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5023 | Some n ->
5024 setautoscrollspeed n false
5027 | 106 | 0xff54 -> (* j down *)
5028 begin match state.autoscroll with
5029 | None ->
5030 begin match state.mode with
5031 | Birdseye beye -> downbirdseye 1 beye
5032 | _ ->
5033 if ctrl
5034 then gotoy_and_clear_text (clamp (conf.winh/2))
5035 else gotoy_and_clear_text (clamp conf.scrollstep)
5037 | Some n ->
5038 setautoscrollspeed n true
5041 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5042 if canpan ()
5043 then
5044 let dx =
5045 if ctrl
5046 then conf.winw / 2
5047 else 10
5049 let dx = if key = 0xff51 then dx else -dx in
5050 state.x <- state.x + dx;
5051 gotoy_and_clear_text state.y
5052 else (
5053 state.text <- "";
5054 G.postRedisplay "lef/right"
5057 | 0xff55 -> (* prior *)
5058 let y =
5059 if ctrl
5060 then
5061 match state.layout with
5062 | [] -> state.y
5063 | l :: _ -> state.y - l.pagey
5064 else
5065 clamp (-conf.winh)
5067 gotoghyll y
5069 | 0xff56 -> (* next *)
5070 let y =
5071 if ctrl
5072 then
5073 match List.rev state.layout with
5074 | [] -> state.y
5075 | l :: _ -> getpagey l.pageno
5076 else
5077 clamp conf.winh
5079 gotoghyll y
5081 | 0xff50 -> gotoghyll 0
5082 | 0xff57 -> gotoghyll (clamp state.maxy)
5083 | 0xff53 when Wsi.withalt mask ->
5084 gotoghyll (getnav ~-1)
5085 | 0xff51 when Wsi.withalt mask ->
5086 gotoghyll (getnav 1)
5088 | 114 -> (* r *)
5089 state.anchor <- getanchor ();
5090 opendoc state.path state.password
5092 | 118 when conf.debug -> (* v *)
5093 state.rects <- [];
5094 List.iter (fun l ->
5095 match getopaque l.pageno with
5096 | None -> ()
5097 | Some opaque ->
5098 let x0, y0, x1, y1 = pagebbox opaque in
5099 let a,b = float x0, float y0 in
5100 let c,d = float x1, float y0 in
5101 let e,f = float x1, float y1 in
5102 let h,j = float x0, float y1 in
5103 let rect = (a,b,c,d,e,f,h,j) in
5104 debugrect rect;
5105 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5106 ) state.layout;
5107 G.postRedisplay "v";
5109 | _ ->
5110 vlog "huh? %s" (Wsi.keyname key)
5113 let linknavkeyboard key mask linknav =
5114 let getpage pageno =
5115 let rec loop = function
5116 | [] -> None
5117 | l :: _ when l.pageno = pageno -> Some l
5118 | _ :: rest -> loop rest
5119 in loop state.layout
5121 let doexact (pageno, n) =
5122 match getopaque pageno, getpage pageno with
5123 | Some opaque, Some l ->
5124 if key = 0xff0d
5125 then
5126 let under = getlink opaque n in
5127 G.postRedisplay "link gotounder";
5128 gotounder under;
5129 state.mode <- View;
5130 else
5131 let opt, dir =
5132 match key with
5133 | 0xff50 -> (* home *)
5134 Some (findlink opaque LDfirst), -1
5136 | 0xff57 -> (* end *)
5137 Some (findlink opaque LDlast), 1
5139 | 0xff51 -> (* left *)
5140 Some (findlink opaque (LDleft n)), -1
5142 | 0xff53 -> (* right *)
5143 Some (findlink opaque (LDright n)), 1
5145 | 0xff52 -> (* up *)
5146 Some (findlink opaque (LDup n)), -1
5148 | 0xff54 -> (* down *)
5149 Some (findlink opaque (LDdown n)), 1
5151 | _ -> None, 0
5153 let pwl l dir =
5154 begin match findpwl l.pageno dir with
5155 | Pwlnotfound -> ()
5156 | Pwl pageno ->
5157 let notfound dir =
5158 state.mode <- LinkNav (Ltgendir dir);
5159 let y, h = getpageyh pageno in
5160 let y =
5161 if dir < 0
5162 then y + h - conf.winh
5163 else y
5165 gotoy y
5167 begin match getopaque pageno, getpage pageno with
5168 | Some opaque, Some _ ->
5169 let link =
5170 let ld = if dir > 0 then LDfirst else LDlast in
5171 findlink opaque ld
5173 begin match link with
5174 | Lfound m ->
5175 showlinktype (getlink opaque m);
5176 state.mode <- LinkNav (Ltexact (pageno, m));
5177 G.postRedisplay "linknav jpage";
5178 | _ -> notfound dir
5179 end;
5180 | _ -> notfound dir
5181 end;
5182 end;
5184 begin match opt with
5185 | Some Lnotfound -> pwl l dir;
5186 | Some (Lfound m) ->
5187 if m = n
5188 then pwl l dir
5189 else (
5190 let _, y0, _, y1 = getlinkrect opaque m in
5191 if y0 < l.pagey
5192 then gotopage1 l.pageno y0
5193 else (
5194 let d = fstate.fontsize + 1 in
5195 if y1 - l.pagey > l.pagevh - d
5196 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5197 else G.postRedisplay "linknav";
5199 showlinktype (getlink opaque m);
5200 state.mode <- LinkNav (Ltexact (l.pageno, m));
5203 | None -> viewkeyboard key mask
5204 end;
5205 | _ -> viewkeyboard key mask
5207 if key = 0xff63
5208 then (
5209 state.mode <- View;
5210 G.postRedisplay "leave linknav"
5212 else
5213 match linknav with
5214 | Ltgendir _ -> viewkeyboard key mask
5215 | Ltexact exact -> doexact exact
5218 let keyboard key mask =
5219 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5220 then wcmd "interrupt"
5221 else state.uioh <- state.uioh#key key mask
5224 let birdseyekeyboard key mask
5225 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5226 let incr =
5227 match conf.columns with
5228 | Csingle -> 1
5229 | Cmulti ((c, _, _), _) -> c
5230 | Csplit _ -> failwith "bird's eye split mode"
5232 match key with
5233 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5234 let y, h = getpageyh pageno in
5235 let top = (conf.winh - h) / 2 in
5236 gotoy (max 0 (y - top))
5237 | 0xff0d -> leavebirdseye beye false
5238 | 0xff1b -> leavebirdseye beye true (* escape *)
5239 | 0xff52 -> upbirdseye incr beye (* prior *)
5240 | 0xff54 -> downbirdseye incr beye (* next *)
5241 | 0xff51 -> upbirdseye 1 beye (* up *)
5242 | 0xff53 -> downbirdseye 1 beye (* down *)
5244 | 0xff55 ->
5245 begin match state.layout with
5246 | l :: _ ->
5247 if l.pagey != 0
5248 then (
5249 state.mode <- Birdseye (
5250 oconf, leftx, l.pageno, hooverpageno, anchor
5252 gotopage1 l.pageno 0;
5254 else (
5255 let layout = layout (state.y-conf.winh) conf.winh in
5256 match layout with
5257 | [] -> gotoy (clamp (-conf.winh))
5258 | l :: _ ->
5259 state.mode <- Birdseye (
5260 oconf, leftx, l.pageno, hooverpageno, anchor
5262 gotopage1 l.pageno 0
5265 | [] -> gotoy (clamp (-conf.winh))
5266 end;
5268 | 0xff56 ->
5269 begin match List.rev state.layout with
5270 | l :: _ ->
5271 let layout = layout (state.y + conf.winh) conf.winh in
5272 begin match layout with
5273 | [] ->
5274 let incr = l.pageh - l.pagevh in
5275 if incr = 0
5276 then (
5277 state.mode <-
5278 Birdseye (
5279 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5281 G.postRedisplay "birdseye pagedown";
5283 else gotoy (clamp (incr + conf.interpagespace*2));
5285 | l :: _ ->
5286 state.mode <-
5287 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5288 gotopage1 l.pageno 0;
5291 | [] -> gotoy (clamp conf.winh)
5292 end;
5294 | 0xff50 ->
5295 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5296 gotopage1 0 0
5298 | 0xff57 ->
5299 let pageno = state.pagecount - 1 in
5300 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5301 if not (pagevisible state.layout pageno)
5302 then
5303 let h =
5304 match List.rev state.pdims with
5305 | [] -> conf.winh
5306 | (_, _, h, _) :: _ -> h
5308 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5309 else G.postRedisplay "birdseye end";
5310 | _ -> viewkeyboard key mask
5313 let drawpage l linkindexbase =
5314 let color =
5315 match state.mode with
5316 | Textentry _ -> scalecolor 0.4
5317 | LinkNav _
5318 | View -> scalecolor 1.0
5319 | Birdseye (_, _, pageno, hooverpageno, _) ->
5320 if l.pageno = hooverpageno
5321 then scalecolor 0.9
5322 else (
5323 if l.pageno = pageno
5324 then scalecolor 1.0
5325 else scalecolor 0.8
5328 drawtiles l color;
5329 begin match getopaque l.pageno with
5330 | Some opaque ->
5331 if tileready l l.pagex l.pagey
5332 then
5333 let x = l.pagedispx - l.pagex
5334 and y = l.pagedispy - l.pagey in
5335 let hlmask = (if conf.hlinks then 1 else 0)
5336 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5338 let s =
5339 match state.mode with
5340 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5341 | _ -> ""
5343 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5344 else 0
5346 | _ -> 0
5347 end;
5350 let scrollindicator () =
5351 let sbw, ph, sh = state.uioh#scrollph in
5352 let sbh, pw, sw = state.uioh#scrollpw in
5354 GlDraw.color (0.64, 0.64, 0.64);
5355 GlDraw.rect
5356 (float (conf.winw - sbw), 0.)
5357 (float conf.winw, float conf.winh)
5359 GlDraw.rect
5360 (0., float (conf.winh - sbh))
5361 (float (conf.winw - state.scrollw - 1), float conf.winh)
5363 GlDraw.color (0.0, 0.0, 0.0);
5365 GlDraw.rect
5366 (float (conf.winw - sbw), ph)
5367 (float conf.winw, ph +. sh)
5369 GlDraw.rect
5370 (pw, float (conf.winh - sbh))
5371 (pw +. sw, float conf.winh)
5375 let showsel () =
5376 match state.mstate with
5377 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5380 | Msel ((x0, y0), (x1, y1)) ->
5381 let rec loop = function
5382 | l :: ls ->
5383 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5384 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5385 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5386 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5387 then
5388 match getopaque l.pageno with
5389 | Some opaque ->
5390 let x0, y0 = pagetranslatepoint l x0 y0 in
5391 let x1, y1 = pagetranslatepoint l x1 y1 in
5392 seltext opaque (x0, y0, x1, y1);
5393 | _ -> ()
5394 else loop ls
5395 | [] -> ()
5397 loop state.layout
5400 let showrects rects =
5401 Gl.enable `blend;
5402 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5403 GlDraw.polygon_mode `both `fill;
5404 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5405 List.iter
5406 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5407 List.iter (fun l ->
5408 if l.pageno = pageno
5409 then (
5410 let dx = float (l.pagedispx - l.pagex) in
5411 let dy = float (l.pagedispy - l.pagey) in
5412 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5413 GlDraw.begins `quads;
5415 GlDraw.vertex2 (x0+.dx, y0+.dy);
5416 GlDraw.vertex2 (x1+.dx, y1+.dy);
5417 GlDraw.vertex2 (x2+.dx, y2+.dy);
5418 GlDraw.vertex2 (x3+.dx, y3+.dy);
5420 GlDraw.ends ();
5422 ) state.layout
5423 ) rects
5425 Gl.disable `blend;
5428 let display () =
5429 GlClear.color (scalecolor2 conf.bgcolor);
5430 GlClear.clear [`color];
5431 let rec loop linkindexbase = function
5432 | l :: rest ->
5433 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5434 loop linkindexbase rest
5435 | [] -> ()
5437 loop 0 state.layout;
5438 let rects =
5439 match state.mode with
5440 | LinkNav (Ltexact (pageno, linkno)) ->
5441 begin match getopaque pageno with
5442 | Some opaque ->
5443 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5444 (pageno, 5, (
5445 float x0, float y0,
5446 float x1, float y0,
5447 float x1, float y1,
5448 float x0, float y1)
5449 ) :: state.rects
5450 | None -> state.rects
5452 | _ -> state.rects
5454 showrects rects;
5455 showsel ();
5456 state.uioh#display;
5457 begin match state.mstate with
5458 | Mzoomrect ((x0, y0), (x1, y1)) ->
5459 Gl.enable `blend;
5460 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5461 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5462 GlDraw.rect (float x0, float y0)
5463 (float x1, float y1);
5464 Gl.disable `blend;
5465 | _ -> ()
5466 end;
5467 enttext ();
5468 scrollindicator ();
5469 Wsi.swapb ();
5472 let zoomrect x y x1 y1 =
5473 let x0 = min x x1
5474 and x1 = max x x1
5475 and y0 = min y y1 in
5476 gotoy (state.y + y0);
5477 state.anchor <- getanchor ();
5478 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5479 let margin =
5480 if state.w < conf.winw - state.scrollw
5481 then (conf.winw - state.scrollw - state.w) / 2
5482 else 0
5484 state.x <- (state.x + margin) - x0;
5485 setzoom zoom;
5486 Wsi.setcursor Wsi.CURSOR_INHERIT;
5487 state.mstate <- Mnone;
5490 let scrollx x =
5491 let winw = conf.winw - state.scrollw - 1 in
5492 let s = float x /. float winw in
5493 let destx = truncate (float (state.w + winw) *. s) in
5494 state.x <- winw - destx;
5495 gotoy_and_clear_text state.y;
5496 state.mstate <- Mscrollx;
5499 let scrolly y =
5500 let s = float y /. float conf.winh in
5501 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5502 gotoy_and_clear_text desty;
5503 state.mstate <- Mscrolly;
5506 let viewmouse button down x y mask =
5507 match button with
5508 | n when (n == 4 || n == 5) && not down ->
5509 if Wsi.withctrl mask
5510 then (
5511 match state.mstate with
5512 | Mzoom (oldn, i) ->
5513 if oldn = n
5514 then (
5515 if i = 2
5516 then
5517 let incr =
5518 match n with
5519 | 5 ->
5520 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5521 | _ ->
5522 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5524 let zoom = conf.zoom -. incr in
5525 setzoom zoom;
5526 state.mstate <- Mzoom (n, 0);
5527 else
5528 state.mstate <- Mzoom (n, i+1);
5530 else state.mstate <- Mzoom (n, 0)
5532 | _ -> state.mstate <- Mzoom (n, 0)
5534 else (
5535 match state.autoscroll with
5536 | Some step -> setautoscrollspeed step (n=4)
5537 | None ->
5538 let incr =
5539 if n = 4
5540 then -conf.scrollstep
5541 else conf.scrollstep
5543 let incr = incr * 2 in
5544 let y = clamp incr in
5545 gotoy_and_clear_text y
5548 | 1 when Wsi.withctrl mask ->
5549 if down
5550 then (
5551 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5552 state.mstate <- Mpan (x, y)
5554 else
5555 state.mstate <- Mnone
5557 | 3 ->
5558 if down
5559 then (
5560 Wsi.setcursor Wsi.CURSOR_CYCLE;
5561 let p = (x, y) in
5562 state.mstate <- Mzoomrect (p, p)
5564 else (
5565 match state.mstate with
5566 | Mzoomrect ((x0, y0), _) ->
5567 if abs (x-x0) > 10 && abs (y - y0) > 10
5568 then zoomrect x0 y0 x y
5569 else (
5570 state.mstate <- Mnone;
5571 Wsi.setcursor Wsi.CURSOR_INHERIT;
5572 G.postRedisplay "kill accidental zoom rect";
5574 | _ ->
5575 Wsi.setcursor Wsi.CURSOR_INHERIT;
5576 state.mstate <- Mnone
5579 | 1 when x > conf.winw - state.scrollw ->
5580 if down
5581 then
5582 let _, position, sh = state.uioh#scrollph in
5583 if y > truncate position && y < truncate (position +. sh)
5584 then state.mstate <- Mscrolly
5585 else scrolly y
5586 else
5587 state.mstate <- Mnone
5589 | 1 when y > conf.winh - state.hscrollh ->
5590 if down
5591 then
5592 let _, position, sw = state.uioh#scrollpw in
5593 if x > truncate position && x < truncate (position +. sw)
5594 then state.mstate <- Mscrollx
5595 else scrollx x
5596 else
5597 state.mstate <- Mnone
5599 | 1 ->
5600 let dest = if down then getunder x y else Unone in
5601 begin match dest with
5602 | Ulinkgoto _
5603 | Ulinkuri _
5604 | Uremote _
5605 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5606 gotounder dest
5608 | Unone when down ->
5609 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5610 state.mstate <- Mpan (x, y);
5612 | Unone | Utext _ ->
5613 if down
5614 then (
5615 if conf.angle mod 360 = 0
5616 then (
5617 state.mstate <- Msel ((x, y), (x, y));
5618 G.postRedisplay "mouse select";
5621 else (
5622 match state.mstate with
5623 | Mnone -> ()
5625 | Mzoom _ | Mscrollx | Mscrolly ->
5626 state.mstate <- Mnone
5628 | Mzoomrect ((x0, y0), _) ->
5629 zoomrect x0 y0 x y
5631 | Mpan _ ->
5632 Wsi.setcursor Wsi.CURSOR_INHERIT;
5633 state.mstate <- Mnone
5635 | Msel ((_, y0), (_, y1)) ->
5636 let rec loop = function
5637 | [] -> ()
5638 | l :: rest ->
5639 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5640 || ((y1 >= l.pagedispy
5641 && y1 <= (l.pagedispy + l.pagevh)))
5642 then
5643 match getopaque l.pageno with
5644 | Some opaque ->
5645 begin
5646 match Ne.pipe () with
5647 | Ne.Exn exn ->
5648 showtext '!'
5649 (Printf.sprintf
5650 "can not create sel pipe: %s"
5651 (Printexc.to_string exn));
5652 | Ne.Res (r, w) ->
5653 let doclose what fd =
5654 Ne.clo fd (fun msg ->
5655 dolog "%s close failed: %s" what msg)
5658 popen conf.selcmd [r, 0; w, -1];
5659 copysel w opaque;
5660 doclose "pipe/r" r;
5661 G.postRedisplay "copysel";
5662 with exn ->
5663 dolog "can not exectute %S: %s"
5664 conf.selcmd (Printexc.to_string exn);
5665 doclose "pipe/r" r;
5666 doclose "pipe/w" w;
5668 | None -> ()
5669 else loop rest
5671 loop state.layout;
5672 Wsi.setcursor Wsi.CURSOR_INHERIT;
5673 state.mstate <- Mnone;
5677 | _ -> ()
5680 let birdseyemouse button down x y mask
5681 (conf, leftx, _, hooverpageno, anchor) =
5682 match button with
5683 | 1 when down ->
5684 let rec loop = function
5685 | [] -> ()
5686 | l :: rest ->
5687 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5688 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5689 then (
5690 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5692 else loop rest
5694 loop state.layout
5695 | 3 -> ()
5696 | _ -> viewmouse button down x y mask
5699 let mouse button down x y mask =
5700 state.uioh <- state.uioh#button button down x y mask;
5703 let motion ~x ~y =
5704 state.uioh <- state.uioh#motion x y
5707 let pmotion ~x ~y =
5708 state.uioh <- state.uioh#pmotion x y;
5711 let uioh = object
5712 method display = ()
5714 method key key mask =
5715 begin match state.mode with
5716 | Textentry textentry -> textentrykeyboard key mask textentry
5717 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5718 | View -> viewkeyboard key mask
5719 | LinkNav linknav -> linknavkeyboard key mask linknav
5720 end;
5721 state.uioh
5723 method button button bstate x y mask =
5724 begin match state.mode with
5725 | LinkNav _
5726 | View -> viewmouse button bstate x y mask
5727 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5728 | Textentry _ -> ()
5729 end;
5730 state.uioh
5732 method motion x y =
5733 begin match state.mode with
5734 | Textentry _ -> ()
5735 | View | Birdseye _ | LinkNav _ ->
5736 match state.mstate with
5737 | Mzoom _ | Mnone -> ()
5739 | Mpan (x0, y0) ->
5740 let dx = x - x0
5741 and dy = y0 - y in
5742 state.mstate <- Mpan (x, y);
5743 if canpan ()
5744 then state.x <- state.x + dx;
5745 let y = clamp dy in
5746 gotoy_and_clear_text y
5748 | Msel (a, _) ->
5749 state.mstate <- Msel (a, (x, y));
5750 G.postRedisplay "motion select";
5752 | Mscrolly ->
5753 let y = min conf.winh (max 0 y) in
5754 scrolly y
5756 | Mscrollx ->
5757 let x = min conf.winw (max 0 x) in
5758 scrollx x
5760 | Mzoomrect (p0, _) ->
5761 state.mstate <- Mzoomrect (p0, (x, y));
5762 G.postRedisplay "motion zoomrect";
5763 end;
5764 state.uioh
5766 method pmotion x y =
5767 begin match state.mode with
5768 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5769 let rec loop = function
5770 | [] ->
5771 if hooverpageno != -1
5772 then (
5773 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5774 G.postRedisplay "pmotion birdseye no hoover";
5776 | l :: rest ->
5777 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5778 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5779 then (
5780 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5781 G.postRedisplay "pmotion birdseye hoover";
5783 else loop rest
5785 loop state.layout
5787 | Textentry _ -> ()
5789 | LinkNav _
5790 | View ->
5791 match state.mstate with
5792 | Mnone -> updateunder x y
5793 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5795 end;
5796 state.uioh
5798 method infochanged _ = ()
5800 method scrollph =
5801 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5802 let p, h = scrollph state.y maxy in
5803 state.scrollw, p, h
5805 method scrollpw =
5806 let winw = conf.winw - state.scrollw - 1 in
5807 let fwinw = float winw in
5808 let sw =
5809 let sw = fwinw /. float state.w in
5810 let sw = fwinw *. sw in
5811 max sw (float conf.scrollh)
5813 let position, sw =
5814 let f = state.w+winw in
5815 let r = float (winw-state.x) /. float f in
5816 let p = fwinw *. r in
5817 p-.sw/.2., sw
5819 let sw =
5820 if position +. sw > fwinw
5821 then fwinw -. position
5822 else sw
5824 state.hscrollh, position, sw
5826 method modehash =
5827 let modename =
5828 match state.mode with
5829 | LinkNav _ -> "links"
5830 | Textentry _ -> "textentry"
5831 | Birdseye _ -> "birdseye"
5832 | View -> "view"
5834 findkeyhash conf modename
5835 end;;
5837 module Config =
5838 struct
5839 open Parser
5841 let fontpath = ref "";;
5843 module KeyMap =
5844 Map.Make (struct type t = (int * int) let compare = compare end);;
5846 let unent s =
5847 let l = String.length s in
5848 let b = Buffer.create l in
5849 unent b s 0 l;
5850 Buffer.contents b;
5853 let home =
5854 try Sys.getenv "HOME"
5855 with exn ->
5856 prerr_endline
5857 ("Can not determine home directory location: " ^
5858 Printexc.to_string exn);
5862 let modifier_of_string = function
5863 | "alt" -> Wsi.altmask
5864 | "shift" -> Wsi.shiftmask
5865 | "ctrl" | "control" -> Wsi.ctrlmask
5866 | "meta" -> Wsi.metamask
5867 | _ -> 0
5870 let key_of_string =
5871 let r = Str.regexp "-" in
5872 fun s ->
5873 let elems = Str.full_split r s in
5874 let f n k m =
5875 let g s =
5876 let m1 = modifier_of_string s in
5877 if m1 = 0
5878 then (Wsi.namekey s, m)
5879 else (k, m lor m1)
5880 in function
5881 | Str.Delim s when n land 1 = 0 -> g s
5882 | Str.Text s -> g s
5883 | Str.Delim _ -> (k, m)
5885 let rec loop n k m = function
5886 | [] -> (k, m)
5887 | x :: xs ->
5888 let k, m = f n k m x in
5889 loop (n+1) k m xs
5891 loop 0 0 0 elems
5894 let keys_of_string =
5895 let r = Str.regexp "[ \t]" in
5896 fun s ->
5897 let elems = Str.split r s in
5898 List.map key_of_string elems
5901 let copykeyhashes c =
5902 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5905 let config_of c attrs =
5906 let apply c k v =
5908 match k with
5909 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5910 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5911 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5912 | "preload" -> { c with preload = bool_of_string v }
5913 | "page-bias" -> { c with pagebias = int_of_string v }
5914 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5915 | "auto-scroll-step" ->
5916 { c with autoscrollstep = max 0 (int_of_string v) }
5917 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5918 | "crop-hack" -> { c with crophack = bool_of_string v }
5919 | "throttle" ->
5920 let mw =
5921 match String.lowercase v with
5922 | "true" -> Some infinity
5923 | "false" -> None
5924 | f -> Some (float_of_string f)
5926 { c with maxwait = mw}
5927 | "highlight-links" -> { c with hlinks = bool_of_string v }
5928 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5929 | "vertical-margin" ->
5930 { c with interpagespace = max 0 (int_of_string v) }
5931 | "zoom" ->
5932 let zoom = float_of_string v /. 100. in
5933 let zoom = max zoom 0.0 in
5934 { c with zoom = zoom }
5935 | "presentation" -> { c with presentation = bool_of_string v }
5936 | "rotation-angle" -> { c with angle = int_of_string v }
5937 | "width" -> { c with winw = max 20 (int_of_string v) }
5938 | "height" -> { c with winh = max 20 (int_of_string v) }
5939 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5940 | "proportional-display" -> { c with proportional = bool_of_string v }
5941 | "pixmap-cache-size" ->
5942 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5943 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5944 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5945 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5946 | "persistent-location" -> { c with jumpback = bool_of_string v }
5947 | "background-color" -> { c with bgcolor = color_of_string v }
5948 | "scrollbar-in-presentation" ->
5949 { c with scrollbarinpm = bool_of_string v }
5950 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5951 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5952 | "mupdf-store-size" ->
5953 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5954 | "checkers" -> { c with checkers = bool_of_string v }
5955 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5956 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5957 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5958 | "uri-launcher" -> { c with urilauncher = unent v }
5959 | "path-launcher" -> { c with pathlauncher = unent v }
5960 | "color-space" -> { c with colorspace = colorspace_of_string v }
5961 | "invert-colors" -> { c with invert = bool_of_string v }
5962 | "brightness" -> { c with colorscale = float_of_string v }
5963 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5964 | "ghyllscroll" ->
5965 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5966 | "columns" ->
5967 let (n, _, _) as nab = multicolumns_of_string v in
5968 if n < 0
5969 then { c with columns = Csplit (-n, [||]) }
5970 else { c with columns = Cmulti (nab, [||]) }
5971 | "birds-eye-columns" ->
5972 { c with beyecolumns = Some (max (int_of_string v) 2) }
5973 | "selection-command" -> { c with selcmd = unent v }
5974 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5975 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
5976 | "full-split" -> { c with fullsplit = bool_of_string v }
5977 | _ -> c
5978 with exn ->
5979 prerr_endline ("Error processing attribute (`" ^
5980 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5983 let rec fold c = function
5984 | [] -> c
5985 | (k, v) :: rest ->
5986 let c = apply c k v in
5987 fold c rest
5989 fold { c with keyhashes = copykeyhashes c } attrs;
5992 let fromstring f pos n v d =
5993 try f v
5994 with exn ->
5995 dolog "Error processing attribute (%S=%S) at %d\n%s"
5996 n v pos (Printexc.to_string exn)
6001 let bookmark_of attrs =
6002 let rec fold title page rely = function
6003 | ("title", v) :: rest -> fold v page rely rest
6004 | ("page", v) :: rest -> fold title v rely rest
6005 | ("rely", v) :: rest -> fold title page v rest
6006 | _ :: rest -> fold title page rely rest
6007 | [] -> title, page, rely
6009 fold "invalid" "0" "0" attrs
6012 let doc_of attrs =
6013 let rec fold path page rely pan = function
6014 | ("path", v) :: rest -> fold v page rely pan rest
6015 | ("page", v) :: rest -> fold path v rely pan rest
6016 | ("rely", v) :: rest -> fold path page v pan rest
6017 | ("pan", v) :: rest -> fold path page rely v rest
6018 | _ :: rest -> fold path page rely pan rest
6019 | [] -> path, page, rely, pan
6021 fold "" "0" "0" "0" attrs
6024 let map_of attrs =
6025 let rec fold rs ls = function
6026 | ("out", v) :: rest -> fold v ls rest
6027 | ("in", v) :: rest -> fold rs v rest
6028 | _ :: rest -> fold ls rs rest
6029 | [] -> ls, rs
6031 fold "" "" attrs
6034 let setconf dst src =
6035 dst.scrollbw <- src.scrollbw;
6036 dst.scrollh <- src.scrollh;
6037 dst.icase <- src.icase;
6038 dst.preload <- src.preload;
6039 dst.pagebias <- src.pagebias;
6040 dst.verbose <- src.verbose;
6041 dst.scrollstep <- src.scrollstep;
6042 dst.maxhfit <- src.maxhfit;
6043 dst.crophack <- src.crophack;
6044 dst.autoscrollstep <- src.autoscrollstep;
6045 dst.maxwait <- src.maxwait;
6046 dst.hlinks <- src.hlinks;
6047 dst.underinfo <- src.underinfo;
6048 dst.interpagespace <- src.interpagespace;
6049 dst.zoom <- src.zoom;
6050 dst.presentation <- src.presentation;
6051 dst.angle <- src.angle;
6052 dst.winw <- src.winw;
6053 dst.winh <- src.winh;
6054 dst.savebmarks <- src.savebmarks;
6055 dst.memlimit <- src.memlimit;
6056 dst.proportional <- src.proportional;
6057 dst.texcount <- src.texcount;
6058 dst.sliceheight <- src.sliceheight;
6059 dst.thumbw <- src.thumbw;
6060 dst.jumpback <- src.jumpback;
6061 dst.bgcolor <- src.bgcolor;
6062 dst.scrollbarinpm <- src.scrollbarinpm;
6063 dst.tilew <- src.tilew;
6064 dst.tileh <- src.tileh;
6065 dst.mustoresize <- src.mustoresize;
6066 dst.checkers <- src.checkers;
6067 dst.aalevel <- src.aalevel;
6068 dst.trimmargins <- src.trimmargins;
6069 dst.trimfuzz <- src.trimfuzz;
6070 dst.urilauncher <- src.urilauncher;
6071 dst.colorspace <- src.colorspace;
6072 dst.invert <- src.invert;
6073 dst.colorscale <- src.colorscale;
6074 dst.redirectstderr <- src.redirectstderr;
6075 dst.ghyllscroll <- src.ghyllscroll;
6076 dst.columns <- src.columns;
6077 dst.beyecolumns <- src.beyecolumns;
6078 dst.selcmd <- src.selcmd;
6079 dst.updatecurs <- src.updatecurs;
6080 dst.pathlauncher <- src.pathlauncher;
6081 dst.keyhashes <- copykeyhashes src;
6082 dst.hfsize <- src.hfsize;
6083 dst.fullsplit <- src.fullsplit;
6086 let get s =
6087 let h = Hashtbl.create 10 in
6088 let dc = { defconf with angle = defconf.angle } in
6089 let rec toplevel v t spos _ =
6090 match t with
6091 | Vdata | Vcdata | Vend -> v
6092 | Vopen ("llppconfig", _, closed) ->
6093 if closed
6094 then v
6095 else { v with f = llppconfig }
6096 | Vopen _ ->
6097 error "unexpected subelement at top level" s spos
6098 | Vclose _ -> error "unexpected close at top level" s spos
6100 and llppconfig v t spos _ =
6101 match t with
6102 | Vdata | Vcdata -> v
6103 | Vend -> error "unexpected end of input in llppconfig" s spos
6104 | Vopen ("defaults", attrs, closed) ->
6105 let c = config_of dc attrs in
6106 setconf dc c;
6107 if closed
6108 then v
6109 else { v with f = defaults }
6111 | Vopen ("ui-font", attrs, closed) ->
6112 let rec getsize size = function
6113 | [] -> size
6114 | ("size", v) :: rest ->
6115 let size =
6116 fromstring int_of_string spos "size" v fstate.fontsize in
6117 getsize size rest
6118 | l -> getsize size l
6120 fstate.fontsize <- getsize fstate.fontsize attrs;
6121 if closed
6122 then v
6123 else { v with f = uifont (Buffer.create 10) }
6125 | Vopen ("doc", attrs, closed) ->
6126 let pathent, spage, srely, span = doc_of attrs in
6127 let path = unent pathent
6128 and pageno = fromstring int_of_string spos "page" spage 0
6129 and rely = fromstring float_of_string spos "rely" srely 0.0
6130 and pan = fromstring int_of_string spos "pan" span 0 in
6131 let c = config_of dc attrs in
6132 let anchor = (pageno, rely) in
6133 if closed
6134 then (Hashtbl.add h path (c, [], pan, anchor); v)
6135 else { v with f = doc path pan anchor c [] }
6137 | Vopen _ ->
6138 error "unexpected subelement in llppconfig" s spos
6140 | Vclose "llppconfig" -> { v with f = toplevel }
6141 | Vclose _ -> error "unexpected close in llppconfig" s spos
6143 and defaults v t spos _ =
6144 match t with
6145 | Vdata | Vcdata -> v
6146 | Vend -> error "unexpected end of input in defaults" s spos
6147 | Vopen ("keymap", attrs, closed) ->
6148 let modename =
6149 try List.assoc "mode" attrs
6150 with Not_found -> "global" in
6151 if closed
6152 then v
6153 else
6154 let ret keymap =
6155 let h = findkeyhash dc modename in
6156 KeyMap.iter (Hashtbl.replace h) keymap;
6157 defaults
6159 { v with f = pkeymap ret KeyMap.empty }
6161 | Vopen (_, _, _) ->
6162 error "unexpected subelement in defaults" s spos
6164 | Vclose "defaults" ->
6165 { v with f = llppconfig }
6167 | Vclose _ -> error "unexpected close in defaults" s spos
6169 and uifont b v t spos epos =
6170 match t with
6171 | Vdata | Vcdata ->
6172 Buffer.add_substring b s spos (epos - spos);
6174 | Vopen (_, _, _) ->
6175 error "unexpected subelement in ui-font" s spos
6176 | Vclose "ui-font" ->
6177 if String.length !fontpath = 0
6178 then fontpath := Buffer.contents b;
6179 { v with f = llppconfig }
6180 | Vclose _ -> error "unexpected close in ui-font" s spos
6181 | Vend -> error "unexpected end of input in ui-font" s spos
6183 and doc path pan anchor c bookmarks v t spos _ =
6184 match t with
6185 | Vdata | Vcdata -> v
6186 | Vend -> error "unexpected end of input in doc" s spos
6187 | Vopen ("bookmarks", _, closed) ->
6188 if closed
6189 then v
6190 else { v with f = pbookmarks path pan anchor c bookmarks }
6192 | Vopen ("keymap", attrs, closed) ->
6193 let modename =
6194 try List.assoc "mode" attrs
6195 with Not_found -> "global"
6197 if closed
6198 then v
6199 else
6200 let ret keymap =
6201 let h = findkeyhash c modename in
6202 KeyMap.iter (Hashtbl.replace h) keymap;
6203 doc path pan anchor c bookmarks
6205 { v with f = pkeymap ret KeyMap.empty }
6207 | Vopen (_, _, _) ->
6208 error "unexpected subelement in doc" s spos
6210 | Vclose "doc" ->
6211 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6212 { v with f = llppconfig }
6214 | Vclose _ -> error "unexpected close in doc" s spos
6216 and pkeymap ret keymap v t spos _ =
6217 match t with
6218 | Vdata | Vcdata -> v
6219 | Vend -> error "unexpected end of input in keymap" s spos
6220 | Vopen ("map", attrs, closed) ->
6221 let r, l = map_of attrs in
6222 let kss = fromstring keys_of_string spos "in" r [] in
6223 let lss = fromstring keys_of_string spos "out" l [] in
6224 let keymap =
6225 match kss with
6226 | [] -> keymap
6227 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6228 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6230 if closed
6231 then { v with f = pkeymap ret keymap }
6232 else
6233 let f () = v in
6234 { v with f = skip "map" f }
6236 | Vopen _ ->
6237 error "unexpected subelement in keymap" s spos
6239 | Vclose "keymap" ->
6240 { v with f = ret keymap }
6242 | Vclose _ -> error "unexpected close in keymap" s spos
6244 and pbookmarks path pan anchor c bookmarks v t spos _ =
6245 match t with
6246 | Vdata | Vcdata -> v
6247 | Vend -> error "unexpected end of input in bookmarks" s spos
6248 | Vopen ("item", attrs, closed) ->
6249 let titleent, spage, srely = bookmark_of attrs in
6250 let page = fromstring int_of_string spos "page" spage 0
6251 and rely = fromstring float_of_string spos "rely" srely 0.0 in
6252 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
6253 if closed
6254 then { v with f = pbookmarks path pan anchor c bookmarks }
6255 else
6256 let f () = v in
6257 { v with f = skip "item" f }
6259 | Vopen _ ->
6260 error "unexpected subelement in bookmarks" s spos
6262 | Vclose "bookmarks" ->
6263 { v with f = doc path pan anchor c bookmarks }
6265 | Vclose _ -> error "unexpected close in bookmarks" s spos
6267 and skip tag f v t spos _ =
6268 match t with
6269 | Vdata | Vcdata -> v
6270 | Vend ->
6271 error ("unexpected end of input in skipped " ^ tag) s spos
6272 | Vopen (tag', _, closed) ->
6273 if closed
6274 then v
6275 else
6276 let f' () = { v with f = skip tag f } in
6277 { v with f = skip tag' f' }
6278 | Vclose ctag ->
6279 if tag = ctag
6280 then f ()
6281 else error ("unexpected close in skipped " ^ tag) s spos
6284 parse { f = toplevel; accu = () } s;
6285 h, dc;
6288 let do_load f ic =
6290 let len = in_channel_length ic in
6291 let s = String.create len in
6292 really_input ic s 0 len;
6293 f s;
6294 with
6295 | Parse_error (msg, s, pos) ->
6296 let subs = subs s pos in
6297 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6298 failwith ("parse error: " ^ s)
6300 | exn ->
6301 failwith ("config load error: " ^ Printexc.to_string exn)
6304 let defconfpath =
6305 let dir =
6307 let dir = Filename.concat home ".config" in
6308 if Sys.is_directory dir then dir else home
6309 with _ -> home
6311 Filename.concat dir "llpp.conf"
6314 let confpath = ref defconfpath;;
6316 let load1 f =
6317 if Sys.file_exists !confpath
6318 then
6319 match
6320 (try Some (open_in_bin !confpath)
6321 with exn ->
6322 prerr_endline
6323 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6324 Printexc.to_string exn);
6325 None
6327 with
6328 | Some ic ->
6329 begin try
6330 f (do_load get ic)
6331 with exn ->
6332 prerr_endline
6333 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6334 Printexc.to_string exn);
6335 end;
6336 close_in ic;
6338 | None -> ()
6339 else
6340 f (Hashtbl.create 0, defconf)
6343 let load () =
6344 let f (h, dc) =
6345 let pc, pb, px, pa =
6347 Hashtbl.find h (Filename.basename state.path)
6348 with Not_found -> dc, [], 0, (0, 0.0)
6350 setconf defconf dc;
6351 setconf conf pc;
6352 state.bookmarks <- pb;
6353 state.x <- px;
6354 state.scrollw <- conf.scrollbw;
6355 if conf.jumpback
6356 then state.anchor <- pa;
6357 cbput state.hists.nav pa;
6359 load1 f
6362 let add_attrs bb always dc c =
6363 let ob s a b =
6364 if always || a != b
6365 then Printf.bprintf bb "\n %s='%b'" s a
6366 and oi s a b =
6367 if always || a != b
6368 then Printf.bprintf bb "\n %s='%d'" s a
6369 and oI s a b =
6370 if always || a != b
6371 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6372 and oz s a b =
6373 if always || a <> b
6374 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6375 and oF s a b =
6376 if always || a <> b
6377 then Printf.bprintf bb "\n %s='%f'" s a
6378 and oc s a b =
6379 if always || a <> b
6380 then
6381 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6382 and oC s a b =
6383 if always || a <> b
6384 then
6385 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6386 and oR s a b =
6387 if always || a <> b
6388 then
6389 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6390 and os s a b =
6391 if always || a <> b
6392 then
6393 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6394 and og s a b =
6395 if always || a <> b
6396 then
6397 match a with
6398 | None -> ()
6399 | Some (_N, _A, _B) ->
6400 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6401 and oW s a b =
6402 if always || a <> b
6403 then
6404 let v =
6405 match a with
6406 | None -> "false"
6407 | Some f ->
6408 if f = infinity
6409 then "true"
6410 else string_of_float f
6412 Printf.bprintf bb "\n %s='%s'" s v
6413 and oco s a b =
6414 if always || a <> b
6415 then
6416 match a with
6417 | Cmulti ((n, a, b), _) when n > 1 ->
6418 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6419 | Csplit (n, _) when n > 1 ->
6420 Printf.bprintf bb "\n %s='%d'" s ~-n
6421 | _ -> ()
6422 and obeco s a b =
6423 if always || a <> b
6424 then
6425 match a with
6426 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6427 | _ -> ()
6429 let w, h =
6430 if always
6431 then dc.winw, dc.winh
6432 else
6433 match state.fullscreen with
6434 | Some wh -> wh
6435 | None -> c.winw, c.winh
6437 let zoom, presentation, interpagespace, maxwait =
6438 if always
6439 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6440 else
6441 match state.mode with
6442 | Birdseye (bc, _, _, _, _) ->
6443 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6444 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6446 oi "width" w dc.winw;
6447 oi "height" h dc.winh;
6448 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6449 oi "scroll-handle-height" c.scrollh dc.scrollh;
6450 ob "case-insensitive-search" c.icase dc.icase;
6451 ob "preload" c.preload dc.preload;
6452 oi "page-bias" c.pagebias dc.pagebias;
6453 oi "scroll-step" c.scrollstep dc.scrollstep;
6454 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6455 ob "max-height-fit" c.maxhfit dc.maxhfit;
6456 ob "crop-hack" c.crophack dc.crophack;
6457 oW "throttle" maxwait dc.maxwait;
6458 ob "highlight-links" c.hlinks dc.hlinks;
6459 ob "under-cursor-info" c.underinfo dc.underinfo;
6460 oi "vertical-margin" interpagespace dc.interpagespace;
6461 oz "zoom" zoom dc.zoom;
6462 ob "presentation" presentation dc.presentation;
6463 oi "rotation-angle" c.angle dc.angle;
6464 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6465 ob "proportional-display" c.proportional dc.proportional;
6466 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6467 oi "tex-count" c.texcount dc.texcount;
6468 oi "slice-height" c.sliceheight dc.sliceheight;
6469 oi "thumbnail-width" c.thumbw dc.thumbw;
6470 ob "persistent-location" c.jumpback dc.jumpback;
6471 oc "background-color" c.bgcolor dc.bgcolor;
6472 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6473 oi "tile-width" c.tilew dc.tilew;
6474 oi "tile-height" c.tileh dc.tileh;
6475 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6476 ob "checkers" c.checkers dc.checkers;
6477 oi "aalevel" c.aalevel dc.aalevel;
6478 ob "trim-margins" c.trimmargins dc.trimmargins;
6479 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6480 os "uri-launcher" c.urilauncher dc.urilauncher;
6481 os "path-launcher" c.pathlauncher dc.pathlauncher;
6482 oC "color-space" c.colorspace dc.colorspace;
6483 ob "invert-colors" c.invert dc.invert;
6484 oF "brightness" c.colorscale dc.colorscale;
6485 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6486 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6487 oco "columns" c.columns dc.columns;
6488 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6489 os "selection-command" c.selcmd dc.selcmd;
6490 ob "update-cursor" c.updatecurs dc.updatecurs;
6491 oi "hint-font-size" c.hfsize dc.hfsize;
6492 ob "full-split" c.fullsplit dc.fullsplit;
6495 let keymapsbuf always dc c =
6496 let bb = Buffer.create 16 in
6497 let rec loop = function
6498 | [] -> ()
6499 | (modename, h) :: rest ->
6500 let dh = findkeyhash dc modename in
6501 if always || h <> dh
6502 then (
6503 if Hashtbl.length h > 0
6504 then (
6505 if Buffer.length bb > 0
6506 then Buffer.add_char bb '\n';
6507 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6508 Hashtbl.iter (fun i o ->
6509 let isdifferent = always ||
6511 let dO = Hashtbl.find dh i in
6512 dO <> o
6513 with Not_found -> true
6515 if isdifferent
6516 then
6517 let addkm (k, m) =
6518 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6519 if Wsi.withalt m then Buffer.add_string bb "alt-";
6520 if Wsi.withshift m then Buffer.add_string bb "shift-";
6521 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6522 Buffer.add_string bb (Wsi.keyname k);
6524 let addkms l =
6525 let rec loop = function
6526 | [] -> ()
6527 | km :: [] -> addkm km
6528 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6530 loop l
6532 Buffer.add_string bb "<map in='";
6533 addkm i;
6534 match o with
6535 | KMinsrt km ->
6536 Buffer.add_string bb "' out='";
6537 addkm km;
6538 Buffer.add_string bb "'/>\n"
6540 | KMinsrl kms ->
6541 Buffer.add_string bb "' out='";
6542 addkms kms;
6543 Buffer.add_string bb "'/>\n"
6545 | KMmulti (ins, kms) ->
6546 Buffer.add_char bb ' ';
6547 addkms ins;
6548 Buffer.add_string bb "' out='";
6549 addkms kms;
6550 Buffer.add_string bb "'/>\n"
6551 ) h;
6552 Buffer.add_string bb "</keymap>";
6555 loop rest
6557 loop c.keyhashes;
6561 let save () =
6562 let uifontsize = fstate.fontsize in
6563 let bb = Buffer.create 32768 in
6564 let f (h, dc) =
6565 let dc = if conf.bedefault then conf else dc in
6566 Buffer.add_string bb "<llppconfig>\n";
6568 if String.length !fontpath > 0
6569 then
6570 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6571 uifontsize
6572 !fontpath
6573 else (
6574 if uifontsize <> 14
6575 then
6576 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6579 Buffer.add_string bb "<defaults ";
6580 add_attrs bb true dc dc;
6581 let kb = keymapsbuf true dc dc in
6582 if Buffer.length kb > 0
6583 then (
6584 Buffer.add_string bb ">\n";
6585 Buffer.add_buffer bb kb;
6586 Buffer.add_string bb "\n</defaults>\n";
6588 else Buffer.add_string bb "/>\n";
6590 let adddoc path pan anchor c bookmarks =
6591 if bookmarks == [] && c = dc && anchor = emptyanchor
6592 then ()
6593 else (
6594 Printf.bprintf bb "<doc path='%s'"
6595 (enent path 0 (String.length path));
6597 if anchor <> emptyanchor
6598 then (
6599 let n, y = anchor in
6600 Printf.bprintf bb " page='%d'" n;
6601 if y > 1e-6
6602 then
6603 Printf.bprintf bb " rely='%f'" y
6607 if pan != 0
6608 then Printf.bprintf bb " pan='%d'" pan;
6610 add_attrs bb false dc c;
6611 let kb = keymapsbuf false dc c in
6613 begin match bookmarks with
6614 | [] ->
6615 if Buffer.length kb > 0
6616 then (
6617 Buffer.add_string bb ">\n";
6618 Buffer.add_buffer bb kb;
6619 Buffer.add_string bb "\n</doc>\n";
6621 else Buffer.add_string bb "/>\n"
6622 | _ ->
6623 Buffer.add_string bb ">\n<bookmarks>\n";
6624 List.iter (fun (title, _level, (page, rely)) ->
6625 Printf.bprintf bb
6626 "<item title='%s' page='%d'"
6627 (enent title 0 (String.length title))
6628 page
6630 if rely > 1e-6
6631 then
6632 Printf.bprintf bb " rely='%f'" rely
6634 Buffer.add_string bb "/>\n";
6635 ) bookmarks;
6636 Buffer.add_string bb "</bookmarks>";
6637 if Buffer.length kb > 0
6638 then (
6639 Buffer.add_string bb "\n";
6640 Buffer.add_buffer bb kb;
6642 Buffer.add_string bb "\n</doc>\n";
6643 end;
6647 let pan, conf =
6648 match state.mode with
6649 | Birdseye (c, pan, _, _, _) ->
6650 let beyecolumns =
6651 match conf.columns with
6652 | Cmulti ((c, _, _), _) -> Some c
6653 | Csingle -> None
6654 | Csplit _ -> None
6655 and columns =
6656 match c.columns with
6657 | Cmulti (c, _) -> Cmulti (c, [||])
6658 | Csingle -> Csingle
6659 | Csplit _ -> failwith "quit from bird's eye while split"
6661 pan, { c with beyecolumns = beyecolumns; columns = columns }
6662 | _ -> state.x, conf
6664 let basename = Filename.basename state.path in
6665 adddoc basename pan (getanchor ())
6666 { conf with
6667 autoscrollstep =
6668 match state.autoscroll with
6669 | Some step -> step
6670 | None -> conf.autoscrollstep }
6671 (if conf.savebmarks then state.bookmarks else []);
6673 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6674 if basename <> path
6675 then adddoc path x y c bookmarks
6676 ) h;
6677 Buffer.add_string bb "</llppconfig>";
6679 load1 f;
6680 if Buffer.length bb > 0
6681 then
6683 let tmp = !confpath ^ ".tmp" in
6684 let oc = open_out_bin tmp in
6685 Buffer.output_buffer oc bb;
6686 close_out oc;
6687 Unix.rename tmp !confpath;
6688 with exn ->
6689 prerr_endline
6690 ("error while saving configuration: " ^ Printexc.to_string exn)
6692 end;;
6694 let () =
6695 Arg.parse
6696 (Arg.align
6697 [("-p", Arg.String (fun s -> state.password <- s) ,
6698 "<password> Set password");
6700 ("-f", Arg.String (fun s -> Config.fontpath := s),
6701 "<path> Set path to the user interface font");
6703 ("-c", Arg.String (fun s -> Config.confpath := s),
6704 "<path> Set path to the configuration file");
6706 ("-v", Arg.Unit (fun () ->
6707 Printf.printf
6708 "%s\nconfiguration path: %s\n"
6709 (version ())
6710 Config.defconfpath
6712 exit 0), " Print version and exit");
6715 (fun s -> state.path <- s)
6716 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6718 if String.length state.path = 0
6719 then (prerr_endline "file name missing"; exit 1);
6721 Config.load ();
6723 let globalkeyhash = findkeyhash conf "global" in
6724 let wsfd, winw, winh = Wsi.init (object
6725 method expose =
6726 if nogeomcmds state.geomcmds || platform == Posx
6727 then display ()
6728 else (
6729 GlFunc.draw_buffer `front;
6730 GlClear.color (scalecolor2 conf.bgcolor);
6731 GlClear.clear [`color];
6732 GlFunc.draw_buffer `back;
6734 method display = display ()
6735 method reshape w h = reshape w h
6736 method mouse b d x y m = mouse b d x y m
6737 method motion x y = state.mpos <- (x, y); motion x y
6738 method pmotion x y = state.mpos <- (x, y); pmotion x y
6739 method key k m =
6740 let mascm = m land (
6741 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6742 ) in
6743 match state.keystate with
6744 | KSnone ->
6745 let km = k, mascm in
6746 begin
6747 match
6748 let modehash = state.uioh#modehash in
6749 try Hashtbl.find modehash km
6750 with Not_found ->
6751 try Hashtbl.find globalkeyhash km
6752 with Not_found -> KMinsrt (k, m)
6753 with
6754 | KMinsrt (k, m) -> keyboard k m
6755 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6756 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6758 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6759 List.iter (fun (k, m) -> keyboard k m) insrt;
6760 state.keystate <- KSnone
6761 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6762 state.keystate <- KSinto (keys, insrt)
6763 | _ ->
6764 state.keystate <- KSnone
6766 method enter x y = state.mpos <- (x, y); pmotion x y
6767 method leave = state.mpos <- (-1, -1)
6768 method quit = raise Quit
6769 end) conf.winw conf.winh (platform = Posx) in
6771 state.wsfd <- wsfd;
6773 if not (
6774 List.exists GlMisc.check_extension
6775 [ "GL_ARB_texture_rectangle"
6776 ; "GL_EXT_texture_recangle"
6777 ; "GL_NV_texture_rectangle" ]
6779 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6781 let cr, sw =
6782 match Ne.pipe () with
6783 | Ne.Exn exn ->
6784 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6785 exit 1
6786 | Ne.Res rw -> rw
6787 and sr, cw =
6788 match Ne.pipe () with
6789 | Ne.Exn exn ->
6790 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6791 exit 1
6792 | Ne.Res rw -> rw
6795 cloexec cr;
6796 cloexec sw;
6797 cloexec sr;
6798 cloexec cw;
6800 setcheckers conf.checkers;
6801 redirectstderr ();
6803 init (cr, cw) (
6804 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6805 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6806 !Config.fontpath
6808 state.sr <- sr;
6809 state.sw <- sw;
6810 state.text <- "Opening " ^ state.path;
6811 reshape winw winh;
6812 opendoc state.path state.password;
6813 state.uioh <- uioh;
6815 let rec loop deadline =
6816 let r =
6817 match state.errfd with
6818 | None -> [state.sr; state.wsfd]
6819 | Some fd -> [state.sr; state.wsfd; fd]
6821 if state.redisplay
6822 then (
6823 state.redisplay <- false;
6824 display ();
6826 let timeout =
6827 let now = now () in
6828 if deadline > now
6829 then (
6830 if deadline = infinity
6831 then ~-.1.0
6832 else max 0.0 (deadline -. now)
6834 else 0.0
6836 let r, _, _ =
6837 try Unix.select r [] [] timeout
6838 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6840 begin match r with
6841 | [] ->
6842 state.ghyll None;
6843 let newdeadline =
6844 if state.ghyll == noghyll
6845 then
6846 match state.autoscroll with
6847 | Some step when step != 0 ->
6848 let y = state.y + step in
6849 let y =
6850 if y < 0
6851 then state.maxy
6852 else if y >= state.maxy then 0 else y
6854 gotoy y;
6855 if state.mode = View
6856 then state.text <- "";
6857 deadline +. 0.01
6858 | _ -> infinity
6859 else deadline +. 0.01
6861 loop newdeadline
6863 | l ->
6864 let rec checkfds = function
6865 | [] -> ()
6866 | fd :: rest when fd = state.sr ->
6867 let cmd = readcmd state.sr in
6868 act cmd;
6869 checkfds rest
6871 | fd :: rest when fd = state.wsfd ->
6872 Wsi.readresp fd;
6873 checkfds rest
6875 | fd :: rest ->
6876 let s = String.create 80 in
6877 let n = Unix.read fd s 0 80 in
6878 if conf.redirectstderr
6879 then (
6880 Buffer.add_substring state.errmsgs s 0 n;
6881 state.newerrmsgs <- true;
6882 state.redisplay <- true;
6884 else (
6885 prerr_string (String.sub s 0 n);
6886 flush stderr;
6888 checkfds rest
6890 checkfds l;
6891 let newdeadline =
6892 let deadline1 =
6893 if deadline = infinity
6894 then now () +. 0.01
6895 else deadline
6897 match state.autoscroll with
6898 | Some step when step != 0 -> deadline1
6899 | _ -> if state.ghyll == noghyll then infinity else deadline1
6901 loop newdeadline
6902 end;
6905 loop infinity;
6906 with Quit ->
6907 Config.save ();
6908 exit 0;