Grrr...
[llpp.git] / main.ml
blobbabffcc6de3c24f228884fd97ea6eb0bd145beac
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * fitmodel * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
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 trimmargins = bool
29 and interpagespace = int
30 and texcount = int
31 and sliceheight = int
32 and gen = int
33 and top = float
34 and dtop = float
35 and fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
42 and fitmodel = | FitWidth | FitProportional | FitPage
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable savebmarks : bool
316 ; mutable fitmodel : fitmodel
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 method eformsgs : bool
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 method eformsgs = false
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor * string) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
484 ; mutable origin : string
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; cwinw = 900
515 ; cwinh = 900
516 ; savebmarks = true
517 ; fitmodel = FitProportional
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let geturl s =
602 let colonpos = try String.index s ':' with Not_found -> -1 in
603 let len = String.length s in
604 if colonpos >= 0 && colonpos + 3 < len
605 then (
606 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
607 then
608 let schemestartpos =
609 try String.rindex_from s colonpos ' '
610 with Not_found -> -1
612 let scheme =
613 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
615 match scheme with
616 | "http" | "ftp" | "mailto" ->
617 let epos =
618 try String.index_from s colonpos ' '
619 with Not_found -> len
621 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
622 | _ -> ""
623 else ""
625 else ""
628 let gotouri uri =
629 if String.length conf.urilauncher = 0
630 then print_endline uri
631 else (
632 let url = geturl uri in
633 if String.length url = 0
634 then print_endline uri
635 else
636 let re = Str.regexp "%s" in
637 let command = Str.global_replace re url conf.urilauncher in
638 try popen command []
639 with exn ->
640 Printf.eprintf
641 "failed to execute `%s': %s\n" command (exntos exn);
642 flush stderr;
646 let version () =
647 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
648 (platform_to_string platform) Sys.word_size Sys.ocaml_version
651 let makehelp () =
652 let strings = version () :: "" :: Help.keys in
653 Array.of_list (
654 List.map (fun s ->
655 let url = geturl s in
656 if String.length url > 0
657 then (s, 0, Action (fun u -> gotouri url; u))
658 else (s, 0, Noaction)
659 ) strings);
662 let noghyll _ = ();;
663 let firstgeomcmds = "", [];;
664 let noreprf () = ();;
666 let state =
667 { sr = Unix.stdin
668 ; sw = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; errfd = None
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; scrollw = 0
678 ; hscrollh = 0
679 ; anchor = emptyanchor
680 ; ranchors = []
681 ; layout = []
682 ; maxy = max_int
683 ; tilelru = Queue.create ()
684 ; pagemap = Hashtbl.create 10
685 ; tilemap = Hashtbl.create 10
686 ; pdims = []
687 ; pagecount = 0
688 ; currently = Idle
689 ; mstate = Mnone
690 ; rects = []
691 ; rects1 = []
692 ; text = ""
693 ; mode = View
694 ; winstate = []
695 ; searchpattern = ""
696 ; outlines = [||]
697 ; bookmarks = []
698 ; path = ""
699 ; password = ""
700 ; nameddest = ""
701 ; geomcmds = firstgeomcmds
702 ; hists =
703 { nav = cbnew 10 emptyanchor
704 ; pat = cbnew 10 ""
705 ; pag = cbnew 10 ""
706 ; sel = cbnew 10 ""
708 ; memused = 0
709 ; gen = 0
710 ; throttle = None
711 ; autoscroll = None
712 ; ghyll = noghyll
713 ; help = makehelp ()
714 ; docinfo = []
715 ; texid = None
716 ; prevzoom = 1.0
717 ; progress = -1.0
718 ; uioh = nouioh
719 ; redisplay = true
720 ; mpos = (-1, -1)
721 ; keystate = KSnone
722 ; glinks = false
723 ; prevcolumns = None
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
727 ; origin = ""
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
754 flush stderr;
758 module Ne = struct
759 type 'a t = | Res of 'a | Exn of exn;;
761 let pipe () =
762 try Res (Unix.pipe ())
763 with exn -> Exn exn
766 let clo fd f =
767 try tempfailureretry Unix.close fd
768 with exn -> f (exntos exn)
771 let dup fd =
772 try Res (tempfailureretry Unix.dup fd)
773 with exn -> Exn exn
776 let dup2 fd1 fd2 =
777 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
778 with exn -> Exn exn
780 end;;
782 let redirectstderr () =
783 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
784 if conf.redirectstderr
785 then
786 match Ne.pipe () with
787 | Ne.Exn exn ->
788 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
790 | Ne.Res (r, w) ->
791 begin match Ne.dup Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup stderr: %s" (exntos exn);
794 Ne.clo r (clofail "pipe/r");
795 Ne.clo w (clofail "pipe/w");
797 | Ne.Res dupstderr ->
798 begin match Ne.dup2 w Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 to stderr: %s" (exntos exn);
801 Ne.clo dupstderr (clofail "stderr duplicate");
802 Ne.clo r (clofail "redir pipe/r");
803 Ne.clo w (clofail "redir pipe/w");
805 | Ne.Res () ->
806 state.stderr <- dupstderr;
807 state.errfd <- Some r;
808 end;
810 else (
811 state.newerrmsgs <- false;
812 begin match state.errfd with
813 | Some fd ->
814 begin match Ne.dup2 state.stderr Unix.stderr with
815 | Ne.Exn exn ->
816 dolog "failed to dup2 original stderr: %s" (exntos exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let selstring s =
903 match Ne.pipe () with
904 | Ne.Exn exn ->
905 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
906 | Ne.Res (r, w) ->
907 let popened =
908 try popen conf.selcmd [r, 0; w, -1]; true
909 with exn ->
910 showtext '!'
911 (Printf.sprintf "failed to execute %s: %s"
912 conf.selcmd (exntos exn));
913 false
915 let clo cap fd =
916 Ne.clo fd (fun msg ->
917 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
920 if popened
921 then
922 (try
923 let l = String.length s in
924 let n = tempfailureretry (Unix.write w s 0) l in
925 if n != l
926 then
927 showtext '!'
928 (Printf.sprintf
929 "failed to write %d characters to sel pipe, wrote %d"
932 with exn ->
933 showtext '!'
934 (Printf.sprintf "failed to write to sel pipe: %s"
935 (exntos exn)
938 else dolog "%s" s;
939 clo "pipe/r" r;
940 clo "pipe/w" w;
943 let undertext = function
944 | Unone -> "none"
945 | Ulinkuri s -> s
946 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
947 | Utext s -> "font: " ^ s
948 | Uunexpected s -> "unexpected: " ^ s
949 | Ulaunch s -> "launch: " ^ s
950 | Unamed s -> "named: " ^ s
951 | Uremote (filename, pageno) ->
952 Printf.sprintf "%s: page %d" filename (pageno+1)
955 let updateunder x y =
956 match getunder x y with
957 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
958 | Ulinkuri uri ->
959 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
960 Wsi.setcursor Wsi.CURSOR_INFO
961 | Ulinkgoto (pageno, _) ->
962 if conf.underinfo
963 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
964 Wsi.setcursor Wsi.CURSOR_INFO
965 | Utext s ->
966 if conf.underinfo then showtext 'f' ("ont: " ^ s);
967 Wsi.setcursor Wsi.CURSOR_TEXT
968 | Uunexpected s ->
969 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
970 Wsi.setcursor Wsi.CURSOR_INHERIT
971 | Ulaunch s ->
972 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
973 Wsi.setcursor Wsi.CURSOR_INHERIT
974 | Unamed s ->
975 if conf.underinfo then showtext 'n' ("amed: " ^ s);
976 Wsi.setcursor Wsi.CURSOR_INHERIT
977 | Uremote (filename, pageno) ->
978 if conf.underinfo then showtext 'r'
979 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
980 Wsi.setcursor Wsi.CURSOR_INFO
983 let showlinktype under =
984 if conf.underinfo
985 then
986 match under with
987 | Unone -> ()
988 | under ->
989 let s = undertext under in
990 showtext ' ' s
993 let addchar s c =
994 let b = Buffer.create (String.length s + 1) in
995 Buffer.add_string b s;
996 Buffer.add_char b c;
997 Buffer.contents b;
1000 let colorspace_of_string s =
1001 match String.lowercase s with
1002 | "rgb" -> Rgb
1003 | "bgr" -> Bgr
1004 | "gray" -> Gray
1005 | _ -> failwith "invalid colorspace"
1008 let int_of_colorspace = function
1009 | Rgb -> 0
1010 | Bgr -> 1
1011 | Gray -> 2
1014 let colorspace_of_int = function
1015 | 0 -> Rgb
1016 | 1 -> Bgr
1017 | 2 -> Gray
1018 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1021 let colorspace_to_string = function
1022 | Rgb -> "rgb"
1023 | Bgr -> "bgr"
1024 | Gray -> "gray"
1027 let fitmodel_of_string s =
1028 match String.lowercase s with
1029 | "width" -> FitWidth
1030 | "proportional" -> FitProportional
1031 | "page" -> FitPage
1032 | _ -> failwith "invalid fit model"
1035 let int_of_fitmodel = function
1036 | FitWidth -> 0
1037 | FitProportional -> 1
1038 | FitPage -> 2
1041 let fitmodel_of_int = function
1042 | 0 -> FitWidth
1043 | 1 -> FitProportional
1044 | 2 -> FitPage
1045 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1048 let fitmodel_to_string = function
1049 | FitWidth -> "width"
1050 | FitProportional -> "proportional"
1051 | FitPage -> "page"
1054 let intentry_with_suffix text key =
1055 let c =
1056 if key >= 32 && key < 127
1057 then Char.chr key
1058 else '\000'
1060 match Char.lowercase c with
1061 | '0' .. '9' ->
1062 let text = addchar text c in
1063 TEcont text
1065 | 'k' | 'm' | 'g' ->
1066 let text = addchar text c in
1067 TEcont text
1069 | _ ->
1070 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1071 TEcont text
1074 let multicolumns_to_string (n, a, b) =
1075 if a = 0 && b = 0
1076 then Printf.sprintf "%d" n
1077 else Printf.sprintf "%d,%d,%d" n a b;
1080 let multicolumns_of_string s =
1082 (int_of_string s, 0, 0)
1083 with _ ->
1084 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1085 if a > 1 || b > 1
1086 then failwith "subtly broken"; (n, a, b)
1090 let readcmd fd =
1091 let s = "xxxx" in
1092 let n = tempfailureretry (Unix.read fd s 0) 4 in
1093 if n != 4 then failwith "incomplete read(len)";
1094 let len = 0
1095 lor (Char.code s.[0] lsl 24)
1096 lor (Char.code s.[1] lsl 16)
1097 lor (Char.code s.[2] lsl 8)
1098 lor (Char.code s.[3] lsl 0)
1100 let s = String.create len in
1101 let n = tempfailureretry (Unix.read fd s 0) len in
1102 if n != len then failwith "incomplete read(data)";
1106 let btod b = if b then 1 else 0;;
1108 let wcmd fmt =
1109 let b = Buffer.create 16 in
1110 Buffer.add_string b "llll";
1111 Printf.kbprintf
1112 (fun b ->
1113 let s = Buffer.contents b in
1114 let n = String.length s in
1115 let len = n - 4 in
1116 (* dolog "wcmd %S" (String.sub s 4 len); *)
1117 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1118 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1119 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1120 s.[3] <- Char.chr (len land 0xff);
1121 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1122 if n' != n then failwith "write failed";
1123 ) b fmt;
1126 let calcips h =
1127 let d = state.winh - h in
1128 max conf.interpagespace ((d + 1) / 2)
1131 let rowyh (c, coverA, coverB) b n =
1132 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1133 then
1134 let _, _, vy, (_, _, h, _) = b.(n) in
1135 (vy, h)
1136 else
1137 let n' = n - coverA in
1138 let d = n' mod c in
1139 let s = n - d in
1140 let e = min state.pagecount (s + c) in
1141 let rec find m miny maxh = if m = e then miny, maxh else
1142 let _, _, y, (_, _, h, _) = b.(m) in
1143 let miny = min miny y in
1144 let maxh = max maxh h in
1145 find (m+1) miny maxh
1146 in find s max_int 0
1149 let calcheight () =
1150 match conf.columns with
1151 | Cmulti ((_, _, _) as cl, b) ->
1152 if Array.length b > 0
1153 then
1154 let y, h = rowyh cl b (Array.length b - 1) in
1155 y + h + (if conf.presentation then calcips h else 0)
1156 else 0
1157 | Csingle b ->
1158 if Array.length b > 0
1159 then
1160 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1161 y + h + (if conf.presentation then calcips h else 0)
1162 else 0
1163 | Csplit (_, b) ->
1164 if Array.length b > 0
1165 then
1166 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1167 y + h
1168 else 0
1171 let getpageyh pageno =
1172 let pageno = bound pageno 0 (state.pagecount-1) in
1173 match conf.columns with
1174 | Csingle b ->
1175 if Array.length b = 0
1176 then 0, 0
1177 else
1178 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1179 let y =
1180 if conf.presentation
1181 then y - calcips h
1182 else y
1184 y, h
1185 | Cmulti (cl, b) ->
1186 if Array.length b = 0
1187 then 0, 0
1188 else
1189 let y, h = rowyh cl b pageno in
1190 let y =
1191 if conf.presentation
1192 then y - calcips h
1193 else y
1195 y, h
1196 | Csplit (c, b) ->
1197 if Array.length b = 0
1198 then 0, 0
1199 else
1200 let n = pageno*c in
1201 let (_, _, y, (_, _, h, _)) = b.(n) in
1202 y, h
1205 let getpagedim pageno =
1206 let rec f ppdim l =
1207 match l with
1208 | (n, _, _, _) as pdim :: rest ->
1209 if n >= pageno
1210 then (if n = pageno then pdim else ppdim)
1211 else f pdim rest
1213 | [] -> ppdim
1215 f (-1, -1, -1, -1) state.pdims
1218 let getpagey pageno = fst (getpageyh pageno);;
1220 let nogeomcmds cmds =
1221 match cmds with
1222 | s, [] -> String.length s = 0
1223 | _ -> false
1226 let page_of_y y =
1227 let ((c, coverA, coverB) as cl), b =
1228 match conf.columns with
1229 | Csingle b -> (1, 0, 0), b
1230 | Cmulti (c, b) -> c, b
1231 | Csplit (_, b) -> (1, 0, 0), b
1233 if Array.length b = 0
1234 then -1
1235 else
1236 let rec bsearch nmin nmax =
1237 if nmin > nmax
1238 then bound nmin 0 (state.pagecount-1)
1239 else
1240 let n = (nmax + nmin) / 2 in
1241 let vy, h = rowyh cl b n in
1242 let y0, y1 =
1243 if conf.presentation
1244 then
1245 let ips = calcips h in
1246 let y0 = vy - ips in
1247 let y1 = vy + h + ips in
1248 y0, y1
1249 else (
1250 if n = 0
1251 then 0, vy + h + conf.interpagespace
1252 else
1253 let y0 = vy - conf.interpagespace in
1254 y0, y0 + h + conf.interpagespace
1257 if y >= y0 && y < y1
1258 then (
1259 if c = 1
1260 then n
1261 else (
1262 if n > coverA
1263 then
1264 if n < state.pagecount - coverB
1265 then ((n-coverA)/c)*c + coverA
1266 else n
1267 else n
1270 else (
1271 if y > y0
1272 then bsearch (n+1) nmax
1273 else bsearch nmin (n-1)
1276 let r = bsearch 0 (state.pagecount-1) in
1280 let layoutN ((columns, coverA, coverB), b) y sh =
1281 let sh = sh - state.hscrollh in
1282 let rec fold accu n =
1283 if n = Array.length b
1284 then accu
1285 else
1286 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1287 if (vy - y) > sh &&
1288 (n = coverA - 1
1289 || n = state.pagecount - coverB
1290 || (n - coverA) mod columns = columns - 1)
1291 then accu
1292 else
1293 let accu =
1294 if vy + h > y
1295 then
1296 let pagey = max 0 (y - vy) in
1297 let pagedispy = if pagey > 0 then 0 else vy - y in
1298 let pagedispx, pagex =
1299 let pdx =
1300 if n = coverA - 1 || n = state.pagecount - coverB
1301 then state.x + (state.winw - state.scrollw - w) / 2
1302 else dx + xoff + state.x
1304 if pdx < 0
1305 then 0, -pdx
1306 else pdx, 0
1308 let pagevw =
1309 let vw = state.winw - state.scrollw - pagedispx in
1310 let pw = w - pagex in
1311 min vw pw
1313 let pagevh = min (h - pagey) (sh - pagedispy) in
1314 if pagevw > 0 && pagevh > 0
1315 then
1316 let e =
1317 { pageno = n
1318 ; pagedimno = pdimno
1319 ; pagew = w
1320 ; pageh = h
1321 ; pagex = pagex
1322 ; pagey = pagey
1323 ; pagevw = pagevw
1324 ; pagevh = pagevh
1325 ; pagedispx = pagedispx
1326 ; pagedispy = pagedispy
1327 ; pagecol = 0
1330 e :: accu
1331 else
1332 accu
1333 else
1334 accu
1336 fold accu (n+1)
1338 List.rev (fold [] (page_of_y y));
1341 let layoutS (columns, b) y sh =
1342 let sh = sh - state.hscrollh in
1343 let rec fold accu n =
1344 if n = Array.length b
1345 then accu
1346 else
1347 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1348 if (vy - y) > sh
1349 then accu
1350 else
1351 let accu =
1352 if vy + pageh > y
1353 then
1354 let x = xoff + state.x in
1355 let pagey = max 0 (y - vy) in
1356 let pagedispy = if pagey > 0 then 0 else vy - y in
1357 let pagedispx, pagex =
1358 if px = 0
1359 then (
1360 if x < 0
1361 then 0, -x
1362 else x, 0
1364 else (
1365 let px = px - x in
1366 if px < 0
1367 then -px, 0
1368 else 0, px
1371 let pagecolw = pagew/columns in
1372 let pagedispx =
1373 if pagecolw < state.winw
1374 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1375 else pagedispx
1377 let pagevw =
1378 let vw = state.winw - pagedispx - state.scrollw in
1379 let pw = pagew - pagex in
1380 min vw pw
1382 let pagevw = min pagevw pagecolw in
1383 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1384 if pagevw > 0 && pagevh > 0
1385 then
1386 let e =
1387 { pageno = n/columns
1388 ; pagedimno = pdimno
1389 ; pagew = pagew
1390 ; pageh = pageh
1391 ; pagex = pagex
1392 ; pagey = pagey
1393 ; pagevw = pagevw
1394 ; pagevh = pagevh
1395 ; pagedispx = pagedispx
1396 ; pagedispy = pagedispy
1397 ; pagecol = n mod columns
1400 e :: accu
1401 else
1402 accu
1403 else
1404 accu
1406 fold accu (n+1)
1408 List.rev (fold [] 0)
1411 let layout y sh =
1412 if nogeomcmds state.geomcmds
1413 then
1414 match conf.columns with
1415 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1416 | Cmulti c -> layoutN c y sh
1417 | Csplit s -> layoutS s y sh
1418 else []
1421 let clamp incr =
1422 let y = state.y + incr in
1423 let y = max 0 y in
1424 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1428 let itertiles l f =
1429 let tilex = l.pagex mod conf.tilew in
1430 let tiley = l.pagey mod conf.tileh in
1432 let col = l.pagex / conf.tilew in
1433 let row = l.pagey / conf.tileh in
1435 let rec rowloop row y0 dispy h =
1436 if h = 0
1437 then ()
1438 else (
1439 let dh = conf.tileh - y0 in
1440 let dh = min h dh in
1441 let rec colloop col x0 dispx w =
1442 if w = 0
1443 then ()
1444 else (
1445 let dw = conf.tilew - x0 in
1446 let dw = min w dw in
1448 f col row dispx dispy x0 y0 dw dh;
1449 colloop (col+1) 0 (dispx+dw) (w-dw)
1452 colloop col tilex l.pagedispx l.pagevw;
1453 rowloop (row+1) 0 (dispy+dh) (h-dh)
1456 if l.pagevw > 0 && l.pagevh > 0
1457 then rowloop row tiley l.pagedispy l.pagevh;
1460 let gettileopaque l col row =
1461 let key =
1462 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1464 try Some (Hashtbl.find state.tilemap key)
1465 with Not_found -> None
1468 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1469 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1470 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1473 let drawtiles l color =
1474 GlDraw.color color;
1475 let f col row x y tilex tiley w h =
1476 match gettileopaque l col row with
1477 | Some (opaque, _, t) ->
1478 let params = x, y, w, h, tilex, tiley in
1479 if conf.invert
1480 then (
1481 Gl.enable `blend;
1482 GlFunc.blend_func `zero `one_minus_src_color;
1484 drawtile params opaque;
1485 if conf.invert
1486 then Gl.disable `blend;
1487 if conf.debug
1488 then (
1489 let s = Printf.sprintf
1490 "%d[%d,%d] %f sec"
1491 l.pageno col row t
1493 let w = measurestr fstate.fontsize s in
1494 GlMisc.push_attrib [`current];
1495 GlDraw.color (0.0, 0.0, 0.0);
1496 GlDraw.rect
1497 (float (x-2), float (y-2))
1498 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1499 GlDraw.color (1.0, 1.0, 1.0);
1500 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1501 GlMisc.pop_attrib ();
1504 | _ ->
1505 let w =
1506 let lw = state.winw - state.scrollw - x in
1507 min lw w
1508 and h =
1509 let lh = state.winh - y in
1510 min lh h
1512 begin match state.texid with
1513 | Some id ->
1514 Gl.enable `texture_2d;
1515 GlTex.bind_texture `texture_2d id;
1516 let x0 = float x
1517 and y0 = float y
1518 and x1 = float (x+w)
1519 and y1 = float (y+h) in
1521 let tw = float w /. 16.0
1522 and th = float h /. 16.0 in
1523 let tx0 = float tilex /. 16.0
1524 and ty0 = float tiley /. 16.0 in
1525 let tx1 = tx0 +. tw
1526 and ty1 = ty0 +. th in
1527 GlDraw.begins `quads;
1528 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1529 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1530 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1531 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1532 GlDraw.ends ();
1534 Gl.disable `texture_2d;
1535 | None ->
1536 GlDraw.color (1.0, 1.0, 1.0);
1537 GlDraw.rect
1538 (float x, float y)
1539 (float (x+w), float (y+h));
1540 end;
1541 if w > 128 && h > fstate.fontsize + 10
1542 then (
1543 GlDraw.color (0.0, 0.0, 0.0);
1544 let c, r =
1545 if conf.verbose
1546 then (col*conf.tilew, row*conf.tileh)
1547 else col, row
1549 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1551 GlDraw.color color;
1553 itertiles l f
1556 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1558 let tilevisible1 l x y =
1559 let ax0 = l.pagex
1560 and ax1 = l.pagex + l.pagevw
1561 and ay0 = l.pagey
1562 and ay1 = l.pagey + l.pagevh in
1564 let bx0 = x
1565 and by0 = y in
1566 let bx1 = min (bx0 + conf.tilew) l.pagew
1567 and by1 = min (by0 + conf.tileh) l.pageh in
1569 let rx0 = max ax0 bx0
1570 and ry0 = max ay0 by0
1571 and rx1 = min ax1 bx1
1572 and ry1 = min ay1 by1 in
1574 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1575 nonemptyintersection
1578 let tilevisible layout n x y =
1579 let rec findpageinlayout m = function
1580 | l :: rest when l.pageno = n ->
1581 tilevisible1 l x y || (
1582 match conf.columns with
1583 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1584 | _ -> false
1586 | _ :: rest -> findpageinlayout 0 rest
1587 | [] -> false
1589 findpageinlayout 0 layout;
1592 let tileready l x y =
1593 tilevisible1 l x y &&
1594 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1597 let tilepage n p layout =
1598 let rec loop = function
1599 | l :: rest ->
1600 if l.pageno = n
1601 then
1602 let f col row _ _ _ _ _ _ =
1603 if state.currently = Idle
1604 then
1605 match gettileopaque l col row with
1606 | Some _ -> ()
1607 | None ->
1608 let x = col*conf.tilew
1609 and y = row*conf.tileh in
1610 let w =
1611 let w = l.pagew - x in
1612 min w conf.tilew
1614 let h =
1615 let h = l.pageh - y in
1616 min h conf.tileh
1618 let pbo =
1619 if conf.usepbo
1620 then getpbo w h conf.colorspace
1621 else "0"
1623 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1624 state.currently <-
1625 Tiling (
1626 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1627 conf.tilew, conf.tileh
1630 itertiles l f;
1631 else
1632 loop rest
1634 | [] -> ()
1636 if nogeomcmds state.geomcmds
1637 then loop layout;
1640 let preloadlayout y =
1641 let y = if y < state.winh then 0 else y - state.winh in
1642 let h = state.winh*3 in
1643 layout y h;
1646 let load pages =
1647 let rec loop pages =
1648 if state.currently != Idle
1649 then ()
1650 else
1651 match pages with
1652 | l :: rest ->
1653 begin match getopaque l.pageno with
1654 | None ->
1655 wcmd "page %d %d" l.pageno l.pagedimno;
1656 state.currently <- Loading (l, state.gen);
1657 | Some opaque ->
1658 tilepage l.pageno opaque pages;
1659 loop rest
1660 end;
1661 | _ -> ()
1663 if nogeomcmds state.geomcmds
1664 then loop pages
1667 let preload pages =
1668 load pages;
1669 if conf.preload && state.currently = Idle
1670 then load (preloadlayout state.y);
1673 let layoutready layout =
1674 let rec fold all ls =
1675 all && match ls with
1676 | l :: rest ->
1677 let seen = ref false in
1678 let allvisible = ref true in
1679 let foo col row _ _ _ _ _ _ =
1680 seen := true;
1681 allvisible := !allvisible &&
1682 begin match gettileopaque l col row with
1683 | Some _ -> true
1684 | None -> false
1687 itertiles l foo;
1688 fold (!seen && !allvisible) rest
1689 | [] -> true
1691 let alltilesvisible = fold true layout in
1692 alltilesvisible;
1695 let gotoy y =
1696 let y = bound y 0 state.maxy in
1697 let y, layout, proceed =
1698 match conf.maxwait with
1699 | Some time when state.ghyll == noghyll ->
1700 begin match state.throttle with
1701 | None ->
1702 let layout = layout y state.winh in
1703 let ready = layoutready layout in
1704 if not ready
1705 then (
1706 load layout;
1707 state.throttle <- Some (layout, y, now ());
1709 else G.postRedisplay "gotoy showall (None)";
1710 y, layout, ready
1711 | Some (_, _, started) ->
1712 let dt = now () -. started in
1713 if dt > time
1714 then (
1715 state.throttle <- None;
1716 let layout = layout y state.winh in
1717 load layout;
1718 G.postRedisplay "maxwait";
1719 y, layout, true
1721 else -1, [], false
1724 | _ ->
1725 let layout = layout y state.winh in
1726 if not !wtmode || layoutready layout
1727 then G.postRedisplay "gotoy ready";
1728 y, layout, true
1730 if proceed
1731 then (
1732 state.y <- y;
1733 state.layout <- layout;
1734 begin match state.mode with
1735 | LinkNav (Ltexact (pageno, linkno)) ->
1736 let rec loop = function
1737 | [] ->
1738 state.mode <- LinkNav (Ltgendir 0)
1739 | l :: _ when l.pageno = pageno ->
1740 begin match getopaque pageno with
1741 | None ->
1742 state.mode <- LinkNav (Ltgendir 0)
1743 | Some opaque ->
1744 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1745 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1746 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1747 then state.mode <- LinkNav (Ltgendir 0)
1749 | _ :: rest -> loop rest
1751 loop layout
1752 | _ -> ()
1753 end;
1754 begin match state.mode with
1755 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1756 if not (pagevisible layout pageno)
1757 then (
1758 match state.layout with
1759 | [] -> ()
1760 | l :: _ ->
1761 state.mode <- Birdseye (
1762 conf, leftx, l.pageno, hooverpageno, anchor
1765 | LinkNav (Ltgendir dir as lt) ->
1766 let linknav =
1767 let rec loop = function
1768 | [] -> lt
1769 | l :: rest ->
1770 match getopaque l.pageno with
1771 | None -> loop rest
1772 | Some opaque ->
1773 let link =
1774 let ld =
1775 if dir = 0
1776 then LDfirstvisible (l.pagex, l.pagey, dir)
1777 else (
1778 if dir > 0 then LDfirst else LDlast
1781 findlink opaque ld
1783 match link with
1784 | Lnotfound -> loop rest
1785 | Lfound n ->
1786 showlinktype (getlink opaque n);
1787 Ltexact (l.pageno, n)
1789 loop state.layout
1791 state.mode <- LinkNav linknav
1792 | _ -> ()
1793 end;
1794 preload layout;
1796 state.ghyll <- noghyll;
1797 if conf.updatecurs
1798 then (
1799 let mx, my = state.mpos in
1800 updateunder mx my;
1804 let conttiling pageno opaque =
1805 tilepage pageno opaque
1806 (if conf.preload then preloadlayout state.y else state.layout)
1809 let gotoy_and_clear_text y =
1810 if not conf.verbose then state.text <- "";
1811 gotoy y;
1814 let getanchor1 l =
1815 let top =
1816 let coloff = l.pagecol * l.pageh in
1817 float (l.pagey + coloff) /. float l.pageh
1819 let dtop =
1820 if l.pagedispy = 0
1821 then
1823 else
1824 if conf.presentation
1825 then float l.pagedispy /. float (calcips l.pageh)
1826 else float l.pagedispy /. float conf.interpagespace
1828 (l.pageno, top, dtop)
1831 let getanchor () =
1832 match state.layout with
1833 | l :: _ -> getanchor1 l
1834 | [] ->
1835 let n = page_of_y state.y in
1836 if n = -1
1837 then state.anchor
1838 else
1839 let y, h = getpageyh n in
1840 let dy = y - state.y in
1841 let dtop =
1842 if conf.presentation
1843 then
1844 let ips = calcips h in
1845 float (dy + ips) /. float ips
1846 else
1847 float dy /. float conf.interpagespace
1849 (n, 0.0, dtop)
1852 let getanchory (n, top, dtop) =
1853 let y, h = getpageyh n in
1854 if conf.presentation
1855 then
1856 let ips = calcips h in
1857 y + truncate (top*.float h -. dtop*.float ips) + ips;
1858 else
1859 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1862 let gotoanchor anchor =
1863 gotoy (getanchory anchor);
1866 let addnav () =
1867 cbput state.hists.nav (getanchor ());
1870 let getnav dir =
1871 let anchor = cbgetc state.hists.nav dir in
1872 getanchory anchor;
1875 let gotoghyll y =
1876 let scroll f n a b =
1877 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1878 let snake f a b =
1879 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1880 if f < a
1881 then s (float f /. float a)
1882 else (
1883 if f > b
1884 then 1.0 -. s ((float (f-b) /. float (n-b)))
1885 else 1.0
1888 snake f a b
1889 and summa f n a b =
1890 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1891 let iv x = x**3.-.0.5*.x**4. in
1892 let iv1 = iv f in
1893 let ins = float a *. iv1
1894 and outs = float (n-b) *. iv1 in
1895 let ones = b - a in
1896 ins +. outs +. float ones
1898 let rec set (_N, _A, _B) y sy =
1899 let sum = summa 1.0 _N _A _B in
1900 let dy = float (y - sy) in
1901 state.ghyll <- (
1902 let rec gf n y1 o =
1903 if n >= _N
1904 then state.ghyll <- noghyll
1905 else
1906 let go n =
1907 let s = scroll n _N _A _B in
1908 let y1 = y1 +. ((s *. dy) /. sum) in
1909 gotoy_and_clear_text (truncate y1);
1910 state.ghyll <- gf (n+1) y1;
1912 match o with
1913 | None -> go n
1914 | Some y' -> set (_N/2, 1, 1) y' state.y
1916 gf 0 (float state.y)
1919 match conf.ghyllscroll with
1920 | None ->
1921 gotoy_and_clear_text y
1922 | Some nab ->
1923 if state.ghyll == noghyll
1924 then set nab y state.y
1925 else state.ghyll (Some y)
1928 let gotopage n top =
1929 let y, h = getpageyh n in
1930 let y = y + (truncate (top *. float h)) in
1931 gotoghyll y
1934 let gotopage1 n top =
1935 let y = getpagey n in
1936 let y = y + top in
1937 gotoghyll y
1940 let invalidate s f =
1941 state.layout <- [];
1942 state.pdims <- [];
1943 state.rects <- [];
1944 state.rects1 <- [];
1945 match state.geomcmds with
1946 | ps, [] when String.length ps = 0 ->
1947 f ();
1948 state.geomcmds <- s, [];
1950 | ps, [] ->
1951 state.geomcmds <- ps, [s, f];
1953 | ps, (s', _) :: rest when s' = s ->
1954 state.geomcmds <- ps, ((s, f) :: rest);
1956 | ps, cmds ->
1957 state.geomcmds <- ps, ((s, f) :: cmds);
1960 let flushpages () =
1961 Hashtbl.iter (fun _ opaque ->
1962 wcmd "freepage %s" opaque;
1963 ) state.pagemap;
1964 Hashtbl.clear state.pagemap;
1967 let flushtiles () =
1968 if not (Queue.is_empty state.tilelru)
1969 then (
1970 Queue.iter (fun (k, p, s) ->
1971 wcmd "freetile %s" p;
1972 state.memused <- state.memused - s;
1973 Hashtbl.remove state.tilemap k;
1974 ) state.tilelru;
1975 state.uioh#infochanged Memused;
1976 Queue.clear state.tilelru;
1978 load state.layout;
1981 let opendoc path password =
1982 state.path <- path;
1983 state.password <- password;
1984 state.gen <- state.gen + 1;
1985 state.docinfo <- [];
1987 flushpages ();
1988 setaalevel conf.aalevel;
1989 let titlepath =
1990 if String.length state.origin = 0
1991 then path
1992 else state.origin
1994 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1995 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1996 invalidate "reqlayout"
1997 (fun () ->
1998 wcmd "reqlayout %d %d %s\000"
1999 conf.angle (int_of_fitmodel conf.fitmodel) state.nameddest;
2003 let reload () =
2004 state.anchor <- getanchor ();
2005 opendoc state.path state.password;
2008 let scalecolor c =
2009 let c = c *. conf.colorscale in
2010 (c, c, c);
2013 let scalecolor2 (r, g, b) =
2014 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2017 let docolumns = function
2018 | Csingle _ ->
2019 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2020 let rec loop pageno pdimno pdim y ph pdims =
2021 if pageno = state.pagecount
2022 then ()
2023 else
2024 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2025 match pdims with
2026 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2027 pdimno+1, pdim, rest
2028 | _ ->
2029 pdimno, pdim, pdims
2031 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2032 let y = y +
2033 (if conf.presentation
2034 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2035 else (if pageno = 0 then 0 else conf.interpagespace)
2038 a.(pageno) <- (pdimno, x, y, pdim);
2039 loop (pageno+1) pdimno pdim (y + h) h pdims
2041 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2042 conf.columns <- Csingle a;
2044 | Cmulti ((columns, coverA, coverB), _) ->
2045 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2046 let rec loop pageno pdimno pdim x y rowh pdims =
2047 let rec fixrow m = if m = pageno then () else
2048 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2049 if h < rowh
2050 then (
2051 let y = y + (rowh - h) / 2 in
2052 a.(m) <- (pdimno, x, y, pdim);
2054 fixrow (m+1)
2056 if pageno = state.pagecount
2057 then fixrow (((pageno - 1) / columns) * columns)
2058 else
2059 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2060 match pdims with
2061 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2062 pdimno+1, pdim, rest
2063 | _ ->
2064 pdimno, pdim, pdims
2066 let x, y, rowh' =
2067 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2068 then (
2069 let x = (state.winw - state.scrollw - w) / 2 in
2070 let ips =
2071 if conf.presentation then calcips h else conf.interpagespace in
2072 x, y + ips + rowh, h
2074 else (
2075 if (pageno - coverA) mod columns = 0
2076 then (
2077 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2078 let y =
2079 if conf.presentation
2080 then
2081 let ips = calcips h in
2082 y + (if pageno = 0 then 0 else calcips rowh + ips)
2083 else
2084 y + (if pageno = 0 then 0 else conf.interpagespace)
2086 x, y + rowh, h
2088 else x, y, max rowh h
2091 let y =
2092 if pageno > 1 && (pageno - coverA) mod columns = 0
2093 then (
2094 let y =
2095 if pageno = columns && conf.presentation
2096 then (
2097 let ips = calcips rowh in
2098 for i = 0 to pred columns
2100 let (pdimno, x, y, pdim) = a.(i) in
2101 a.(i) <- (pdimno, x, y+ips, pdim)
2102 done;
2103 y+ips;
2105 else y
2107 fixrow (pageno - columns);
2110 else y
2112 a.(pageno) <- (pdimno, x, y, pdim);
2113 let x = x + w + xoff*2 + conf.interpagespace in
2114 loop (pageno+1) pdimno pdim x y rowh' pdims
2116 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2117 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2119 | Csplit (c, _) ->
2120 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2121 let rec loop pageno pdimno pdim y pdims =
2122 if pageno = state.pagecount
2123 then ()
2124 else
2125 let pdimno, ((_, w, h, _) as pdim), pdims =
2126 match pdims with
2127 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2128 pdimno+1, pdim, rest
2129 | _ ->
2130 pdimno, pdim, pdims
2132 let cw = w / c in
2133 let rec loop1 n x y =
2134 if n = c then y else (
2135 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2136 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2139 let y = loop1 0 0 y in
2140 loop (pageno+1) pdimno pdim y pdims
2142 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2143 conf.columns <- Csplit (c, a);
2146 let represent () =
2147 docolumns conf.columns;
2148 state.maxy <- calcheight ();
2149 state.hscrollh <-
2150 if state.x = 0 && state.w <= state.winw - state.scrollw
2151 then 0
2152 else state.scrollw
2154 if state.reprf == noreprf
2155 then (
2156 match state.mode with
2157 | Birdseye (_, _, pageno, _, _) ->
2158 let y, h = getpageyh pageno in
2159 let top = (state.winh - h) / 2 in
2160 gotoy (max 0 (y - top))
2161 | _ -> gotoanchor state.anchor
2163 else (
2164 state.reprf ();
2165 state.reprf <- noreprf;
2169 let reshape w h =
2170 GlDraw.viewport 0 0 w h;
2171 let firsttime = state.geomcmds == firstgeomcmds in
2172 if not firsttime && nogeomcmds state.geomcmds
2173 then state.anchor <- getanchor ();
2175 state.winw <- w;
2176 let w = truncate (float w *. conf.zoom) - state.scrollw in
2177 let w = max w 2 in
2178 state.winh <- h;
2179 setfontsize fstate.fontsize;
2180 GlMat.mode `modelview;
2181 GlMat.load_identity ();
2183 GlMat.mode `projection;
2184 GlMat.load_identity ();
2185 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2186 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2187 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2189 let relx =
2190 if conf.zoom <= 1.0
2191 then 0.0
2192 else float state.x /. float state.w
2194 invalidate "geometry"
2195 (fun () ->
2196 state.w <- w;
2197 if not firsttime
2198 then state.x <- truncate (relx *. float w);
2199 let w =
2200 match conf.columns with
2201 | Csingle _ -> w
2202 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2203 | Csplit (c, _) -> w * c
2205 wcmd "geometry %d %d %d"
2206 w ((truncate (float h*.conf.zoom)) - 2*conf.interpagespace)
2207 (int_of_fitmodel conf.fitmodel)
2211 let enttext () =
2212 let len = String.length state.text in
2213 let drawstring s =
2214 let hscrollh =
2215 match state.mode with
2216 | Textentry _
2217 | View ->
2218 let h, _, _ = state.uioh#scrollpw in
2220 | _ -> 0
2222 let rect x w =
2223 GlDraw.rect
2224 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2225 (x+.w, float (state.winh - hscrollh))
2228 let w = float (state.winw - state.scrollw - 1) in
2229 if state.progress >= 0.0 && state.progress < 1.0
2230 then (
2231 GlDraw.color (0.3, 0.3, 0.3);
2232 let w1 = w *. state.progress in
2233 rect 0.0 w1;
2234 GlDraw.color (0.0, 0.0, 0.0);
2235 rect w1 (w-.w1)
2237 else (
2238 GlDraw.color (0.0, 0.0, 0.0);
2239 rect 0.0 w;
2242 GlDraw.color (1.0, 1.0, 1.0);
2243 drawstring fstate.fontsize
2244 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2246 let s =
2247 match state.mode with
2248 | Textentry ((prefix, text, _, _, _, _), _) ->
2249 let s =
2250 if len > 0
2251 then
2252 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2253 else
2254 Printf.sprintf "%s%s_" prefix text
2258 | _ -> state.text
2260 let s =
2261 if state.newerrmsgs
2262 then (
2263 if not (istextentry state.mode) && state.uioh#eformsgs
2264 then
2265 let s1 = "(press 'e' to review error messasges)" in
2266 if String.length s > 0 then s ^ " " ^ s1 else s1
2267 else s
2269 else s
2271 if String.length s > 0
2272 then drawstring s
2275 let gctiles () =
2276 let len = Queue.length state.tilelru in
2277 let layout = lazy (
2278 match state.throttle with
2279 | None ->
2280 if conf.preload
2281 then preloadlayout state.y
2282 else state.layout
2283 | Some (layout, _, _) ->
2284 layout
2285 ) in
2286 let rec loop qpos =
2287 if state.memused <= conf.memlimit
2288 then ()
2289 else (
2290 if qpos < len
2291 then
2292 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2293 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2294 let (_, pw, ph, _) = getpagedim n in
2296 gen = state.gen
2297 && colorspace = conf.colorspace
2298 && angle = conf.angle
2299 && pagew = pw
2300 && pageh = ph
2301 && (
2302 let x = col*conf.tilew
2303 and y = row*conf.tileh in
2304 tilevisible (Lazy.force_val layout) n x y
2306 then Queue.push lruitem state.tilelru
2307 else (
2308 freepbo p;
2309 wcmd "freetile %s" p;
2310 state.memused <- state.memused - s;
2311 state.uioh#infochanged Memused;
2312 Hashtbl.remove state.tilemap k;
2314 loop (qpos+1)
2317 loop 0
2320 let logcurrently = function
2321 | Idle -> dolog "Idle"
2322 | Loading (l, gen) ->
2323 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2324 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2325 dolog
2326 "Tiling %d[%d,%d] page=%s cs=%s angle"
2327 l.pageno col row pageopaque
2328 (colorspace_to_string colorspace)
2330 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2331 angle gen conf.angle state.gen
2332 tilew tileh
2333 conf.tilew conf.tileh
2335 | Outlining _ ->
2336 dolog "outlining"
2339 let splitatspace =
2340 let r = Str.regexp " " in
2341 fun s -> Str.bounded_split r s 2;
2344 let onpagerect pageno f =
2345 let b =
2346 match conf.columns with
2347 | Cmulti (_, b) -> b
2348 | Csingle b -> b
2349 | Csplit (_, b) -> b
2351 if pageno >= 0 && pageno < Array.length b
2352 then
2353 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2354 let r = getpdimrect pdimno in
2355 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2358 let gotopagexy1 pageno x y =
2359 onpagerect pageno (fun w h ->
2360 let top = y /. h in
2361 let _,w1,_,leftx = getpagedim pageno in
2362 let wh = state.winh - state.hscrollh in
2363 let sw = float w1 /. w in
2364 let x = sw *. x in
2365 let x = leftx + state.x + truncate x in
2366 let sx =
2367 if x < 0 || x >= state.winw - state.scrollw
2368 then state.x - x
2369 else state.x
2371 let py, h = getpageyh pageno in
2372 let pdy = truncate (top *. float h) in
2373 let y' = py + pdy in
2374 let dy = y' - state.y in
2375 let sy =
2376 if x != state.x || not (dy > 0 && dy < wh)
2377 then (
2378 if conf.presentation
2379 then
2380 if abs (py - y') > wh
2381 then y'
2382 else py
2383 else y';
2385 else state.y
2387 if state.x != sx || state.y != sy
2388 then (
2389 let x, y =
2390 if !wtmode
2391 then (
2392 let ww = state.winw - state.scrollw in
2393 let qx = sx / ww
2394 and qy = pdy / wh in
2395 let x = qx * ww
2396 and y = py + qy * wh in
2397 let x = if -x + ww > w1 then -(w1-ww) else x
2398 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2399 let y =
2400 if conf.presentation
2401 then
2402 if abs (py - y') > wh
2403 then y'
2404 else py
2405 else y';
2407 (x, y)
2409 else (sx, sy)
2411 state.x <- x;
2412 state.hscrollh <-
2413 if x = 0 && state.w <= state.winw - state.scrollw
2414 then 0
2415 else state.scrollw
2417 gotoy_and_clear_text y;
2419 else gotoy_and_clear_text state.y;
2423 let gotopagexy pageno x y =
2424 match state.mode with
2425 | Birdseye _ -> gotopage pageno 0.0
2426 | _ -> gotopagexy1 pageno x y
2429 let act cmds =
2430 (* dolog "%S" cmds; *)
2431 let cl = splitatspace cmds in
2432 let scan s fmt f =
2433 try Scanf.sscanf s fmt f
2434 with exn ->
2435 dolog "error processing '%S': %s" cmds (exntos exn);
2436 exit 1
2438 match cl with
2439 | "clear" :: [] ->
2440 state.uioh#infochanged Pdim;
2441 state.pdims <- [];
2443 | "clearrects" :: [] ->
2444 state.rects <- state.rects1;
2445 G.postRedisplay "clearrects";
2447 | "continue" :: args :: [] ->
2448 let n = scan args "%u" (fun n -> n) in
2449 state.pagecount <- n;
2450 begin match state.currently with
2451 | Outlining l ->
2452 state.currently <- Idle;
2453 state.outlines <- Array.of_list (List.rev l)
2454 | _ -> ()
2455 end;
2457 let cur, cmds = state.geomcmds in
2458 if String.length cur = 0
2459 then failwith "umpossible";
2461 begin match List.rev cmds with
2462 | [] ->
2463 state.geomcmds <- "", [];
2464 represent ();
2465 | (s, f) :: rest ->
2466 f ();
2467 state.geomcmds <- s, List.rev rest;
2468 end;
2469 if conf.maxwait = None && not !wtmode
2470 then G.postRedisplay "continue";
2472 | "title" :: args :: [] ->
2473 Wsi.settitle args
2475 | "msg" :: args :: [] ->
2476 showtext ' ' args
2478 | "vmsg" :: args :: [] ->
2479 if conf.verbose
2480 then showtext ' ' args
2482 | "emsg" :: args :: [] ->
2483 Buffer.add_string state.errmsgs args;
2484 state.newerrmsgs <- true;
2485 G.postRedisplay "error message"
2487 | "progress" :: args :: [] ->
2488 let progress, text =
2489 scan args "%f %n"
2490 (fun f pos ->
2491 f, String.sub args pos (String.length args - pos))
2493 state.text <- text;
2494 state.progress <- progress;
2495 G.postRedisplay "progress"
2497 | "firstmatch" :: args :: [] ->
2498 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2499 scan args "%u %d %f %f %f %f %f %f %f %f"
2500 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2501 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2503 let y = (getpagey pageno) + truncate y0 in
2504 addnav ();
2505 gotoy y;
2506 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2508 | "match" :: args :: [] ->
2509 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2510 scan args "%u %d %f %f %f %f %f %f %f %f"
2511 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2512 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2514 state.rects1 <-
2515 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2517 | "page" :: args :: [] ->
2518 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2519 begin match state.currently with
2520 | Loading (l, gen) ->
2521 vlog "page %d took %f sec" l.pageno t;
2522 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2523 begin match state.throttle with
2524 | None ->
2525 let preloadedpages =
2526 if conf.preload
2527 then preloadlayout state.y
2528 else state.layout
2530 let evict () =
2531 let set =
2532 List.fold_left (fun s l -> IntSet.add l.pageno s)
2533 IntSet.empty preloadedpages
2535 let evictedpages =
2536 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2537 if not (IntSet.mem pageno set)
2538 then (
2539 wcmd "freepage %s" opaque;
2540 key :: accu
2542 else accu
2543 ) state.pagemap []
2545 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2547 evict ();
2548 state.currently <- Idle;
2549 if gen = state.gen
2550 then (
2551 tilepage l.pageno pageopaque state.layout;
2552 load state.layout;
2553 load preloadedpages;
2554 if pagevisible state.layout l.pageno
2555 && layoutready state.layout
2556 then G.postRedisplay "page";
2559 | Some (layout, _, _) ->
2560 state.currently <- Idle;
2561 tilepage l.pageno pageopaque layout;
2562 load state.layout
2563 end;
2565 | _ ->
2566 dolog "Inconsistent loading state";
2567 logcurrently state.currently;
2568 exit 1
2571 | "tile" :: args :: [] ->
2572 let (x, y, opaque, size, t) =
2573 scan args "%u %u %s %u %f"
2574 (fun x y p size t -> (x, y, p, size, t))
2576 begin match state.currently with
2577 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2578 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2580 unmappbo opaque;
2581 if tilew != conf.tilew || tileh != conf.tileh
2582 then (
2583 wcmd "freetile %s" opaque;
2584 state.currently <- Idle;
2585 load state.layout;
2587 else (
2588 puttileopaque l col row gen cs angle opaque size t;
2589 state.memused <- state.memused + size;
2590 state.uioh#infochanged Memused;
2591 gctiles ();
2592 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2593 opaque, size) state.tilelru;
2595 let layout =
2596 match state.throttle with
2597 | None -> state.layout
2598 | Some (layout, _, _) -> layout
2601 state.currently <- Idle;
2602 if gen = state.gen
2603 && conf.colorspace = cs
2604 && conf.angle = angle
2605 && tilevisible layout l.pageno x y
2606 then conttiling l.pageno pageopaque;
2608 begin match state.throttle with
2609 | None ->
2610 preload state.layout;
2611 if gen = state.gen
2612 && conf.colorspace = cs
2613 && conf.angle = angle
2614 && tilevisible state.layout l.pageno x y
2615 && (not !wtmode || layoutready state.layout)
2616 then G.postRedisplay "tile nothrottle";
2618 | Some (layout, y, _) ->
2619 let ready = layoutready layout in
2620 if ready
2621 then (
2622 state.y <- y;
2623 state.layout <- layout;
2624 state.throttle <- None;
2625 G.postRedisplay "throttle";
2627 else load layout;
2628 end;
2631 | _ ->
2632 dolog "Inconsistent tiling state";
2633 logcurrently state.currently;
2634 exit 1
2637 | "pdim" :: args :: [] ->
2638 let (n, w, h, _) as pdim =
2639 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2641 let pdim =
2642 match conf.fitmodel, conf.columns with
2643 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2644 | _ -> pdim
2646 state.uioh#infochanged Pdim;
2647 state.pdims <- pdim :: state.pdims
2649 | "o" :: args :: [] ->
2650 let (l, n, t, h, pos) =
2651 scan args "%u %u %d %u %n"
2652 (fun l n t h pos -> l, n, t, h, pos)
2654 let s = String.sub args pos (String.length args - pos) in
2655 let outline = (s, l, (n, float t /. float h, 0.0)) in
2656 begin match state.currently with
2657 | Outlining outlines ->
2658 state.currently <- Outlining (outline :: outlines)
2659 | Idle ->
2660 state.currently <- Outlining [outline]
2661 | currently ->
2662 dolog "invalid outlining state";
2663 logcurrently currently
2666 | "a" :: args :: [] ->
2667 let (n, l, t) =
2668 scan args "%u %d %d" (fun n l t -> n, l, t)
2670 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2672 | "info" :: args :: [] ->
2673 state.docinfo <- (1, args) :: state.docinfo
2675 | "infoend" :: [] ->
2676 state.uioh#infochanged Docinfo;
2677 state.docinfo <- List.rev state.docinfo
2679 | _ ->
2680 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2683 let onhist cb =
2684 let rc = cb.rc in
2685 let action = function
2686 | HCprev -> cbget cb ~-1
2687 | HCnext -> cbget cb 1
2688 | HCfirst -> cbget cb ~-(cb.rc)
2689 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2690 and cancel () = cb.rc <- rc
2691 in (action, cancel)
2694 let search pattern forward =
2695 match conf.columns with
2696 | Csplit _ ->
2697 showtext '!' "searching does not work properly in split columns mode"
2698 | _ ->
2699 if String.length pattern > 0
2700 then
2701 let pn, py =
2702 match state.layout with
2703 | [] -> 0, 0
2704 | l :: _ ->
2705 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2707 wcmd "search %d %d %d %d,%s\000"
2708 (btod conf.icase) pn py (btod forward) pattern;
2711 let intentry text key =
2712 let c =
2713 if key >= 32 && key < 127
2714 then Char.chr key
2715 else '\000'
2717 match c with
2718 | '0' .. '9' ->
2719 let text = addchar text c in
2720 TEcont text
2722 | _ ->
2723 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2724 TEcont text
2727 let linknentry text key =
2728 let c =
2729 if key >= 32 && key < 127
2730 then Char.chr key
2731 else '\000'
2733 match c with
2734 | 'a' .. 'z' ->
2735 let text = addchar text c in
2736 TEcont text
2738 | _ ->
2739 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2740 TEcont text
2743 let linkndone f s =
2744 if String.length s > 0
2745 then (
2746 let n =
2747 let l = String.length s in
2748 let rec loop pos n = if pos = l then n else
2749 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2750 loop (pos+1) (n*26 + m)
2751 in loop 0 0
2753 let rec loop n = function
2754 | [] -> ()
2755 | l :: rest ->
2756 match getopaque l.pageno with
2757 | None -> loop n rest
2758 | Some opaque ->
2759 let m = getlinkcount opaque in
2760 if n < m
2761 then (
2762 let under = getlink opaque n in
2763 f under
2765 else loop (n-m) rest
2767 loop n state.layout;
2771 let textentry text key =
2772 if key land 0xff00 = 0xff00
2773 then TEcont text
2774 else TEcont (text ^ toutf8 key)
2777 let reqlayout angle fitmodel =
2778 match state.throttle with
2779 | None ->
2780 if nogeomcmds state.geomcmds
2781 then state.anchor <- getanchor ();
2782 conf.angle <- angle mod 360;
2783 if conf.angle != 0
2784 then (
2785 match state.mode with
2786 | LinkNav _ -> state.mode <- View
2787 | _ -> ()
2789 conf.fitmodel <- fitmodel;
2790 invalidate "reqlayout"
2791 (fun () ->
2792 wcmd "reqlayout %d %d" conf.angle (int_of_fitmodel conf.fitmodel)
2794 | _ -> ()
2797 let settrim trimmargins trimfuzz =
2798 if nogeomcmds state.geomcmds
2799 then state.anchor <- getanchor ();
2800 conf.trimmargins <- trimmargins;
2801 conf.trimfuzz <- trimfuzz;
2802 let x0, y0, x1, y1 = trimfuzz in
2803 invalidate "settrim"
2804 (fun () ->
2805 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2806 flushpages ();
2809 let setzoom zoom =
2810 match state.throttle with
2811 | None ->
2812 let zoom = max 0.0001 zoom in
2813 if zoom <> conf.zoom
2814 then (
2815 state.prevzoom <- conf.zoom;
2816 conf.zoom <- zoom;
2817 reshape state.winw state.winh;
2818 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2821 | Some (layout, y, started) ->
2822 let time =
2823 match conf.maxwait with
2824 | None -> 0.0
2825 | Some t -> t
2827 let dt = now () -. started in
2828 if dt > time
2829 then (
2830 state.y <- y;
2831 load layout;
2835 let setcolumns mode columns coverA coverB =
2836 state.prevcolumns <- Some (conf.columns, conf.zoom);
2837 if columns < 0
2838 then (
2839 if isbirdseye mode
2840 then showtext '!' "split mode doesn't work in bird's eye"
2841 else (
2842 conf.columns <- Csplit (-columns, [||]);
2843 state.x <- 0;
2844 conf.zoom <- 1.0;
2847 else (
2848 if columns < 2
2849 then (
2850 conf.columns <- Csingle [||];
2851 state.x <- 0;
2852 setzoom 1.0;
2854 else (
2855 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2856 conf.zoom <- 1.0;
2859 reshape state.winw state.winh;
2862 let enterbirdseye () =
2863 let zoom = float conf.thumbw /. float state.winw in
2864 let birdseyepageno =
2865 let cy = state.winh / 2 in
2866 let fold = function
2867 | [] -> 0
2868 | l :: rest ->
2869 let rec fold best = function
2870 | [] -> best.pageno
2871 | l :: rest ->
2872 let d = cy - (l.pagedispy + l.pagevh/2)
2873 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2874 if abs d < abs dbest
2875 then fold l rest
2876 else best.pageno
2877 in fold l rest
2879 fold state.layout
2881 state.mode <- Birdseye (
2882 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2884 conf.zoom <- zoom;
2885 conf.presentation <- false;
2886 conf.interpagespace <- 10;
2887 conf.hlinks <- false;
2888 conf.fitmodel <- FitProportional;
2889 state.x <- 0;
2890 state.mstate <- Mnone;
2891 conf.maxwait <- None;
2892 conf.columns <- (
2893 match conf.beyecolumns with
2894 | Some c ->
2895 conf.zoom <- 1.0;
2896 Cmulti ((c, 0, 0), [||])
2897 | None -> Csingle [||]
2899 Wsi.setcursor Wsi.CURSOR_INHERIT;
2900 if conf.verbose
2901 then
2902 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2903 (100.0*.zoom)
2904 else
2905 state.text <- ""
2907 reshape state.winw state.winh;
2910 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2911 state.mode <- View;
2912 conf.zoom <- c.zoom;
2913 conf.presentation <- c.presentation;
2914 conf.interpagespace <- c.interpagespace;
2915 conf.maxwait <- c.maxwait;
2916 conf.hlinks <- c.hlinks;
2917 conf.fitmodel <- c.fitmodel;
2918 conf.beyecolumns <- (
2919 match conf.columns with
2920 | Cmulti ((c, _, _), _) -> Some c
2921 | Csingle _ -> None
2922 | Csplit _ -> failwith "leaving bird's eye split mode"
2924 conf.columns <- (
2925 match c.columns with
2926 | Cmulti (c, _) -> Cmulti (c, [||])
2927 | Csingle _ -> Csingle [||]
2928 | Csplit (c, _) -> Csplit (c, [||])
2930 state.x <- leftx;
2931 if conf.verbose
2932 then
2933 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2934 (100.0*.conf.zoom)
2936 reshape state.winw state.winh;
2937 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2940 let togglebirdseye () =
2941 match state.mode with
2942 | Birdseye vals -> leavebirdseye vals true
2943 | View -> enterbirdseye ()
2944 | _ -> ()
2947 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2948 let pageno = max 0 (pageno - incr) in
2949 let rec loop = function
2950 | [] -> gotopage1 pageno 0
2951 | l :: _ when l.pageno = pageno ->
2952 if l.pagedispy >= 0 && l.pagey = 0
2953 then G.postRedisplay "upbirdseye"
2954 else gotopage1 pageno 0
2955 | _ :: rest -> loop rest
2957 loop state.layout;
2958 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2961 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2962 let pageno = min (state.pagecount - 1) (pageno + incr) in
2963 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2964 let rec loop = function
2965 | [] ->
2966 let y, h = getpageyh pageno in
2967 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2968 gotoy (clamp dy)
2969 | l :: _ when l.pageno = pageno ->
2970 if l.pagevh != l.pageh
2971 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2972 else G.postRedisplay "downbirdseye"
2973 | _ :: rest -> loop rest
2975 loop state.layout
2978 let optentry mode _ key =
2979 let btos b = if b then "on" else "off" in
2980 if key >= 32 && key < 127
2981 then
2982 let c = Char.chr key in
2983 match c with
2984 | 's' ->
2985 let ondone s =
2986 try conf.scrollstep <- int_of_string s with exc ->
2987 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2989 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2991 | 'A' ->
2992 let ondone s =
2994 conf.autoscrollstep <- int_of_string s;
2995 if state.autoscroll <> None
2996 then state.autoscroll <- Some conf.autoscrollstep
2997 with exc ->
2998 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3000 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3002 | 'C' ->
3003 let ondone s =
3005 let n, a, b = multicolumns_of_string s in
3006 setcolumns mode n a b;
3007 with exc ->
3008 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3010 TEswitch ("columns: ", "", None, textentry, ondone, true)
3012 | 'Z' ->
3013 let ondone s =
3015 let zoom = float (int_of_string s) /. 100.0 in
3016 setzoom zoom
3017 with exc ->
3018 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3020 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3022 | 't' ->
3023 let ondone s =
3025 conf.thumbw <- bound (int_of_string s) 2 4096;
3026 state.text <-
3027 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3028 begin match mode with
3029 | Birdseye beye ->
3030 leavebirdseye beye false;
3031 enterbirdseye ();
3032 | _ -> ();
3034 with exc ->
3035 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3037 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3039 | 'R' ->
3040 let ondone s =
3041 match try
3042 Some (int_of_string s)
3043 with exc ->
3044 state.text <- Printf.sprintf "bad integer `%s': %s"
3045 s (exntos exc);
3046 None
3047 with
3048 | Some angle -> reqlayout angle conf.fitmodel
3049 | None -> ()
3051 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3053 | 'i' ->
3054 conf.icase <- not conf.icase;
3055 TEdone ("case insensitive search " ^ (btos conf.icase))
3057 | 'p' ->
3058 conf.preload <- not conf.preload;
3059 gotoy state.y;
3060 TEdone ("preload " ^ (btos conf.preload))
3062 | 'v' ->
3063 conf.verbose <- not conf.verbose;
3064 TEdone ("verbose " ^ (btos conf.verbose))
3066 | 'd' ->
3067 conf.debug <- not conf.debug;
3068 TEdone ("debug " ^ (btos conf.debug))
3070 | 'h' ->
3071 conf.maxhfit <- not conf.maxhfit;
3072 state.maxy <- calcheight ();
3073 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3075 | 'c' ->
3076 conf.crophack <- not conf.crophack;
3077 TEdone ("crophack " ^ btos conf.crophack)
3079 | 'a' ->
3080 let s =
3081 match conf.maxwait with
3082 | None ->
3083 conf.maxwait <- Some infinity;
3084 "always wait for page to complete"
3085 | Some _ ->
3086 conf.maxwait <- None;
3087 "show placeholder if page is not ready"
3089 TEdone s
3091 | 'f' ->
3092 conf.underinfo <- not conf.underinfo;
3093 TEdone ("underinfo " ^ btos conf.underinfo)
3095 | 'P' ->
3096 conf.savebmarks <- not conf.savebmarks;
3097 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3099 | 'S' ->
3100 let ondone s =
3102 let pageno, py =
3103 match state.layout with
3104 | [] -> 0, 0
3105 | l :: _ ->
3106 l.pageno, l.pagey
3108 conf.interpagespace <- int_of_string s;
3109 docolumns conf.columns;
3110 state.maxy <- calcheight ();
3111 let y = getpagey pageno in
3112 gotoy (y + py)
3113 with exc ->
3114 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3116 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3118 | 'l' ->
3119 let fm =
3120 match conf.fitmodel with
3121 | FitProportional -> FitWidth
3122 | _ -> FitProportional
3124 reqlayout conf.angle fm;
3125 TEdone ("proportional display " ^ btos (fm == FitProportional))
3127 | 'T' ->
3128 settrim (not conf.trimmargins) conf.trimfuzz;
3129 TEdone ("trim margins " ^ btos conf.trimmargins)
3131 | 'I' ->
3132 conf.invert <- not conf.invert;
3133 TEdone ("invert colors " ^ btos conf.invert)
3135 | 'x' ->
3136 let ondone s =
3137 cbput state.hists.sel s;
3138 conf.selcmd <- s;
3140 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3141 textentry, ondone, true)
3143 | _ ->
3144 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3145 TEstop
3146 else
3147 TEcont state.text
3150 class type lvsource = object
3151 method getitemcount : int
3152 method getitem : int -> (string * int)
3153 method hasaction : int -> bool
3154 method exit :
3155 uioh:uioh ->
3156 cancel:bool ->
3157 active:int ->
3158 first:int ->
3159 pan:int ->
3160 qsearch:string ->
3161 uioh option
3162 method getactive : int
3163 method getfirst : int
3164 method getqsearch : string
3165 method setqsearch : string -> unit
3166 method getpan : int
3167 end;;
3169 class virtual lvsourcebase = object
3170 val mutable m_active = 0
3171 val mutable m_first = 0
3172 val mutable m_qsearch = ""
3173 val mutable m_pan = 0
3174 method getactive = m_active
3175 method getfirst = m_first
3176 method getqsearch = m_qsearch
3177 method getpan = m_pan
3178 method setqsearch s = m_qsearch <- s
3179 end;;
3181 let withoutlastutf8 s =
3182 let len = String.length s in
3183 if len = 0
3184 then s
3185 else
3186 let rec find pos =
3187 if pos = 0
3188 then pos
3189 else
3190 let b = Char.code s.[pos] in
3191 if b land 0b11000000 = 0b11000000
3192 then pos
3193 else find (pos-1)
3195 let first =
3196 if Char.code s.[len-1] land 0x80 = 0
3197 then len-1
3198 else find (len-1)
3200 String.sub s 0 first;
3203 let textentrykeyboard
3204 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3205 let key =
3206 if key >= 0xffb0 && key <= 0xffb9
3207 then key - 0xffb0 + 48 else key
3209 let enttext te =
3210 state.mode <- Textentry (te, onleave);
3211 state.text <- "";
3212 enttext ();
3213 G.postRedisplay "textentrykeyboard enttext";
3215 let histaction cmd =
3216 match opthist with
3217 | None -> ()
3218 | Some (action, _) ->
3219 state.mode <- Textentry (
3220 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3222 G.postRedisplay "textentry histaction"
3224 match key with
3225 | 0xff08 -> (* backspace *)
3226 let s = withoutlastutf8 text in
3227 let len = String.length s in
3228 if cancelonempty && len = 0
3229 then (
3230 onleave Cancel;
3231 G.postRedisplay "textentrykeyboard after cancel";
3233 else (
3234 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3237 | 0xff0d | 0xff8d -> (* (kp) enter *)
3238 ondone text;
3239 onleave Confirm;
3240 G.postRedisplay "textentrykeyboard after confirm"
3242 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3243 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3244 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3245 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3247 | 0xff1b -> (* escape*)
3248 if String.length text = 0
3249 then (
3250 begin match opthist with
3251 | None -> ()
3252 | Some (_, onhistcancel) -> onhistcancel ()
3253 end;
3254 onleave Cancel;
3255 state.text <- "";
3256 G.postRedisplay "textentrykeyboard after cancel2"
3258 else (
3259 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3262 | 0xff9f | 0xffff -> () (* delete *)
3264 | _ when key != 0
3265 && key land 0xff00 != 0xff00 (* keyboard *)
3266 && key land 0xfe00 != 0xfe00 (* xkb *)
3267 && key land 0xfd00 != 0xfd00 (* 3270 *)
3269 begin match onkey text key with
3270 | TEdone text ->
3271 ondone text;
3272 onleave Confirm;
3273 G.postRedisplay "textentrykeyboard after confirm2";
3275 | TEcont text ->
3276 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3278 | TEstop ->
3279 onleave Cancel;
3280 G.postRedisplay "textentrykeyboard after cancel3"
3282 | TEswitch te ->
3283 state.mode <- Textentry (te, onleave);
3284 G.postRedisplay "textentrykeyboard switch";
3285 end;
3287 | _ ->
3288 vlog "unhandled key %s" (Wsi.keyname key)
3291 let firstof first active =
3292 if first > active || abs (first - active) > fstate.maxrows - 1
3293 then max 0 (active - (fstate.maxrows/2))
3294 else first
3297 let calcfirst first active =
3298 if active > first
3299 then
3300 let rows = active - first in
3301 if rows > fstate.maxrows then active - fstate.maxrows else first
3302 else active
3305 let scrollph y maxy =
3306 let sh = float (maxy + state.winh) /. float state.winh in
3307 let sh = float state.winh /. sh in
3308 let sh = max sh (float conf.scrollh) in
3310 let percent = float y /. float maxy in
3311 let position = (float state.winh -. sh) *. percent in
3313 let position =
3314 if position +. sh > float state.winh
3315 then float state.winh -. sh
3316 else position
3318 position, sh;
3321 let coe s = (s :> uioh);;
3323 class listview ~(source:lvsource) ~trusted ~modehash =
3324 object (self)
3325 val m_pan = source#getpan
3326 val m_first = source#getfirst
3327 val m_active = source#getactive
3328 val m_qsearch = source#getqsearch
3329 val m_prev_uioh = state.uioh
3331 method private elemunder y =
3332 let n = y / (fstate.fontsize+1) in
3333 if m_first + n < source#getitemcount
3334 then (
3335 if source#hasaction (m_first + n)
3336 then Some (m_first + n)
3337 else None
3339 else None
3341 method display =
3342 Gl.enable `blend;
3343 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3344 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3345 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3346 GlDraw.color (1., 1., 1.);
3347 Gl.enable `texture_2d;
3348 let fs = fstate.fontsize in
3349 let nfs = fs + 1 in
3350 let ww = fstate.wwidth in
3351 let tabw = 30.0*.ww in
3352 let itemcount = source#getitemcount in
3353 let rec loop row =
3354 if (row - m_first) > fstate.maxrows
3355 then ()
3356 else (
3357 if row >= 0 && row < itemcount
3358 then (
3359 let (s, level) = source#getitem row in
3360 let y = (row - m_first) * nfs in
3361 let x = 5.0 +. float (level + m_pan) *. ww in
3362 if row = m_active
3363 then (
3364 Gl.disable `texture_2d;
3365 GlDraw.polygon_mode `both `line;
3366 let alpha = if source#hasaction row then 0.9 else 0.3 in
3367 GlDraw.color (1., 1., 1.) ~alpha;
3368 GlDraw.rect (1., float (y + 1))
3369 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3370 GlDraw.polygon_mode `both `fill;
3371 GlDraw.color (1., 1., 1.);
3372 Gl.enable `texture_2d;
3375 let drawtabularstring s =
3376 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3377 if trusted
3378 then
3379 let tabpos = try String.index s '\t' with Not_found -> -1 in
3380 if tabpos > 0
3381 then
3382 let len = String.length s - tabpos - 1 in
3383 let s1 = String.sub s 0 tabpos
3384 and s2 = String.sub s (tabpos + 1) len in
3385 let nx = drawstr x s1 in
3386 let sw = nx -. x in
3387 let x = x +. (max tabw sw) in
3388 drawstr x s2
3389 else
3390 drawstr x s
3391 else
3392 drawstr x s
3394 let _ = drawtabularstring s in
3395 loop (row+1)
3399 loop m_first;
3400 Gl.disable `blend;
3401 Gl.disable `texture_2d;
3403 method updownlevel incr =
3404 let len = source#getitemcount in
3405 let curlevel =
3406 if m_active >= 0 && m_active < len
3407 then snd (source#getitem m_active)
3408 else -1
3410 let rec flow i =
3411 if i = len then i-1 else if i = -1 then 0 else
3412 let _, l = source#getitem i in
3413 if l != curlevel then i else flow (i+incr)
3415 let active = flow m_active in
3416 let first = calcfirst m_first active in
3417 G.postRedisplay "outline updownlevel";
3418 {< m_active = active; m_first = first >}
3420 method private key1 key mask =
3421 let set1 active first qsearch =
3422 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3424 let search active pattern incr =
3425 let active = if active = -1 then m_first else active in
3426 let dosearch re =
3427 let rec loop n =
3428 if n >= 0 && n < source#getitemcount
3429 then (
3430 let s, _ = source#getitem n in
3432 (try ignore (Str.search_forward re s 0); true
3433 with Not_found -> false)
3434 then Some n
3435 else loop (n + incr)
3437 else None
3439 loop active
3442 let re = Str.regexp_case_fold pattern in
3443 dosearch re
3444 with Failure s ->
3445 state.text <- s;
3446 None
3448 let itemcount = source#getitemcount in
3449 let find start incr =
3450 let rec find i =
3451 if i = -1 || i = itemcount
3452 then -1
3453 else (
3454 if source#hasaction i
3455 then i
3456 else find (i + incr)
3459 find start
3461 let set active first =
3462 let first = bound first 0 (itemcount - fstate.maxrows) in
3463 state.text <- "";
3464 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3466 let navigate incr =
3467 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3468 let active, first =
3469 let incr1 = if incr > 0 then 1 else -1 in
3470 if isvisible m_first m_active
3471 then
3472 let next =
3473 let next = m_active + incr in
3474 let next =
3475 if next < 0 || next >= itemcount
3476 then -1
3477 else find next incr1
3479 if abs (m_active - next) > fstate.maxrows
3480 then -1
3481 else next
3483 if next = -1
3484 then
3485 let first = m_first + incr in
3486 let first = bound first 0 (itemcount - 1) in
3487 let next =
3488 let next = m_active + incr in
3489 let next = bound next 0 (itemcount - 1) in
3490 find next ~-incr1
3492 let active =
3493 if next = -1
3494 then m_active
3495 else (
3496 if isvisible first next
3497 then next
3498 else m_active
3501 active, first
3502 else
3503 let first = min next m_first in
3504 let first =
3505 if abs (next - first) > fstate.maxrows
3506 then first + incr
3507 else first
3509 next, first
3510 else
3511 let first = m_first + incr in
3512 let first = bound first 0 (itemcount - 1) in
3513 let active =
3514 let next = m_active + incr in
3515 let next = bound next 0 (itemcount - 1) in
3516 let next = find next incr1 in
3517 let active =
3518 if next = -1 || abs (m_active - first) > fstate.maxrows
3519 then (
3520 let active = if m_active = -1 then next else m_active in
3521 active
3523 else next
3525 if isvisible first active
3526 then active
3527 else -1
3529 active, first
3531 G.postRedisplay "listview navigate";
3532 set active first;
3534 match key with
3535 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3536 let incr = if key = 0x72 then -1 else 1 in
3537 let active, first =
3538 match search (m_active + incr) m_qsearch incr with
3539 | None ->
3540 state.text <- m_qsearch ^ " [not found]";
3541 m_active, m_first
3542 | Some active ->
3543 state.text <- m_qsearch;
3544 active, firstof m_first active
3546 G.postRedisplay "listview ctrl-r/s";
3547 set1 active first m_qsearch;
3549 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3550 if m_active >= 0 && m_active < source#getitemcount
3551 then (
3552 let s, _ = source#getitem m_active in
3553 selstring s;
3555 coe self
3557 | 0xff08 -> (* backspace *)
3558 if String.length m_qsearch = 0
3559 then coe self
3560 else (
3561 let qsearch = withoutlastutf8 m_qsearch in
3562 let len = String.length qsearch in
3563 if len = 0
3564 then (
3565 state.text <- "";
3566 G.postRedisplay "listview empty qsearch";
3567 set1 m_active m_first "";
3569 else
3570 let active, first =
3571 match search m_active qsearch ~-1 with
3572 | None ->
3573 state.text <- qsearch ^ " [not found]";
3574 m_active, m_first
3575 | Some active ->
3576 state.text <- qsearch;
3577 active, firstof m_first active
3579 G.postRedisplay "listview backspace qsearch";
3580 set1 active first qsearch
3583 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3584 let pattern = m_qsearch ^ toutf8 key in
3585 let active, first =
3586 match search m_active pattern 1 with
3587 | None ->
3588 state.text <- pattern ^ " [not found]";
3589 m_active, m_first
3590 | Some active ->
3591 state.text <- pattern;
3592 active, firstof m_first active
3594 G.postRedisplay "listview qsearch add";
3595 set1 active first pattern;
3597 | 0xff1b -> (* escape *)
3598 state.text <- "";
3599 if String.length m_qsearch = 0
3600 then (
3601 G.postRedisplay "list view escape";
3602 begin
3603 match
3604 source#exit (coe self) true m_active m_first m_pan m_qsearch
3605 with
3606 | None -> m_prev_uioh
3607 | Some uioh -> uioh
3610 else (
3611 G.postRedisplay "list view kill qsearch";
3612 source#setqsearch "";
3613 coe {< m_qsearch = "" >}
3616 | 0xff0d | 0xff8d -> (* (kp) enter *)
3617 state.text <- "";
3618 let self = {< m_qsearch = "" >} in
3619 source#setqsearch "";
3620 let opt =
3621 G.postRedisplay "listview enter";
3622 if m_active >= 0 && m_active < source#getitemcount
3623 then (
3624 source#exit (coe self) false m_active m_first m_pan "";
3626 else (
3627 source#exit (coe self) true m_active m_first m_pan "";
3630 begin match opt with
3631 | None -> m_prev_uioh
3632 | Some uioh -> uioh
3635 | 0xff9f | 0xffff -> (* (kp) delete *)
3636 coe self
3638 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3639 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3640 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3641 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3643 | 0xff53 | 0xff98 -> (* (kp) right *)
3644 state.text <- "";
3645 G.postRedisplay "listview right";
3646 coe {< m_pan = m_pan - 1 >}
3648 | 0xff51 | 0xff96 -> (* (kp) left *)
3649 state.text <- "";
3650 G.postRedisplay "listview left";
3651 coe {< m_pan = m_pan + 1 >}
3653 | 0xff50 | 0xff95 -> (* (kp) home *)
3654 let active = find 0 1 in
3655 G.postRedisplay "listview home";
3656 set active 0;
3658 | 0xff57 | 0xff9c -> (* (kp) end *)
3659 let first = max 0 (itemcount - fstate.maxrows) in
3660 let active = find (itemcount - 1) ~-1 in
3661 G.postRedisplay "listview end";
3662 set active first;
3664 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3665 coe self
3667 | _ ->
3668 dolog "listview unknown key %#x" key; coe self
3670 method key key mask =
3671 match state.mode with
3672 | Textentry te -> textentrykeyboard key mask te; coe self
3673 | _ -> self#key1 key mask
3675 method button button down x y _ =
3676 let opt =
3677 match button with
3678 | 1 when x > state.winw - conf.scrollbw ->
3679 G.postRedisplay "listview scroll";
3680 if down
3681 then
3682 let _, position, sh = self#scrollph in
3683 if y > truncate position && y < truncate (position +. sh)
3684 then (
3685 state.mstate <- Mscrolly;
3686 Some (coe self)
3688 else
3689 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3690 let first = truncate (s *. float source#getitemcount) in
3691 let first = min source#getitemcount first in
3692 Some (coe {< m_first = first; m_active = first >})
3693 else (
3694 state.mstate <- Mnone;
3695 Some (coe self);
3697 | 1 when not down ->
3698 begin match self#elemunder y with
3699 | Some n ->
3700 G.postRedisplay "listview click";
3701 source#exit
3702 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3703 | _ ->
3704 Some (coe self)
3706 | n when (n == 4 || n == 5) && not down ->
3707 let len = source#getitemcount in
3708 let first =
3709 if n = 5 && m_first + fstate.maxrows >= len
3710 then
3711 m_first
3712 else
3713 let first = m_first + (if n == 4 then -1 else 1) in
3714 bound first 0 (len - 1)
3716 G.postRedisplay "listview wheel";
3717 Some (coe {< m_first = first >})
3718 | n when (n = 6 || n = 7) && not down ->
3719 let inc = m_first + (if n = 7 then -1 else 1) in
3720 G.postRedisplay "listview hwheel";
3721 Some (coe {< m_pan = m_pan + inc >})
3722 | _ ->
3723 Some (coe self)
3725 match opt with
3726 | None -> m_prev_uioh
3727 | Some uioh -> uioh
3729 method motion _ y =
3730 match state.mstate with
3731 | Mscrolly ->
3732 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3733 let first = truncate (s *. float source#getitemcount) in
3734 let first = min source#getitemcount first in
3735 G.postRedisplay "listview motion";
3736 coe {< m_first = first; m_active = first >}
3737 | _ -> coe self
3739 method pmotion x y =
3740 if x < state.winw - conf.scrollbw
3741 then
3742 let n =
3743 match self#elemunder y with
3744 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3745 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3747 let o =
3748 if n != m_active
3749 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3750 else self
3752 coe o
3753 else (
3754 Wsi.setcursor Wsi.CURSOR_INHERIT;
3755 coe self
3758 method infochanged _ = ()
3760 method scrollpw = (0, 0.0, 0.0)
3761 method scrollph =
3762 let nfs = fstate.fontsize + 1 in
3763 let y = m_first * nfs in
3764 let itemcount = source#getitemcount in
3765 let maxi = max 0 (itemcount - fstate.maxrows) in
3766 let maxy = maxi * nfs in
3767 let p, h = scrollph y maxy in
3768 conf.scrollbw, p, h
3770 method modehash = modehash
3771 method eformsgs = false
3772 end;;
3774 class outlinelistview ~source =
3775 object (self)
3776 inherit listview
3777 ~source:(source :> lvsource)
3778 ~trusted:false
3779 ~modehash:(findkeyhash conf "outline")
3780 as super
3782 method key key mask =
3783 let calcfirst first active =
3784 if active > first
3785 then
3786 let rows = active - first in
3787 let maxrows =
3788 if String.length state.text = 0
3789 then fstate.maxrows
3790 else fstate.maxrows - 2
3792 if rows > maxrows then active - maxrows else first
3793 else active
3795 let navigate incr =
3796 let active = m_active + incr in
3797 let active = bound active 0 (source#getitemcount - 1) in
3798 let first = calcfirst m_first active in
3799 G.postRedisplay "outline navigate";
3800 coe {< m_active = active; m_first = first >}
3802 let ctrl = Wsi.withctrl mask in
3803 match key with
3804 | 110 when ctrl -> (* ctrl-n *)
3805 source#narrow m_qsearch;
3806 G.postRedisplay "outline ctrl-n";
3807 coe {< m_first = 0; m_active = 0 >}
3809 | 117 when ctrl -> (* ctrl-u *)
3810 source#denarrow;
3811 G.postRedisplay "outline ctrl-u";
3812 state.text <- "";
3813 coe {< m_first = 0; m_active = 0 >}
3815 | 108 when ctrl -> (* ctrl-l *)
3816 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3817 G.postRedisplay "outline ctrl-l";
3818 coe {< m_first = first >}
3820 | 0xff9f | 0xffff -> (* (kp) delete *)
3821 source#remove m_active;
3822 G.postRedisplay "outline delete";
3823 let active = max 0 (m_active-1) in
3824 coe {< m_first = firstof m_first active;
3825 m_active = active >}
3827 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3828 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3829 | 0xff55 | 0xff9a -> (* (kp) prior *)
3830 navigate ~-(fstate.maxrows)
3831 | 0xff56 | 0xff9b -> (* (kp) next *)
3832 navigate fstate.maxrows
3834 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3835 let o =
3836 if ctrl
3837 then (
3838 G.postRedisplay "outline ctrl right";
3839 {< m_pan = m_pan + 1 >}
3841 else self#updownlevel 1
3843 coe o
3845 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3846 let o =
3847 if ctrl
3848 then (
3849 G.postRedisplay "outline ctrl left";
3850 {< m_pan = m_pan - 1 >}
3852 else self#updownlevel ~-1
3854 coe o
3856 | 0xff50 | 0xff95 -> (* (kp) home *)
3857 G.postRedisplay "outline home";
3858 coe {< m_first = 0; m_active = 0 >}
3860 | 0xff57 | 0xff9c -> (* (kp) end *)
3861 let active = source#getitemcount - 1 in
3862 let first = max 0 (active - fstate.maxrows) in
3863 G.postRedisplay "outline end";
3864 coe {< m_active = active; m_first = first >}
3866 | _ -> super#key key mask
3869 let outlinesource usebookmarks =
3870 let empty = [||] in
3871 (object
3872 inherit lvsourcebase
3873 val mutable m_items = empty
3874 val mutable m_orig_items = empty
3875 val mutable m_prev_items = empty
3876 val mutable m_narrow_pattern = ""
3877 val mutable m_hadremovals = false
3879 method getitemcount =
3880 Array.length m_items + (if m_hadremovals then 1 else 0)
3882 method getitem n =
3883 if n == Array.length m_items && m_hadremovals
3884 then
3885 ("[Confirm removal]", 0)
3886 else
3887 let s, n, _ = m_items.(n) in
3888 (s, n)
3890 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3891 ignore (uioh, first, qsearch);
3892 let confrimremoval = m_hadremovals && active = Array.length m_items in
3893 let items =
3894 if String.length m_narrow_pattern = 0
3895 then m_orig_items
3896 else m_items
3898 if not cancel
3899 then (
3900 if not confrimremoval
3901 then(
3902 let _, _, anchor = m_items.(active) in
3903 gotoghyll (getanchory anchor);
3904 m_items <- items;
3906 else (
3907 state.bookmarks <- Array.to_list m_items;
3908 m_orig_items <- m_items;
3911 else m_items <- items;
3912 m_pan <- pan;
3913 None
3915 method hasaction _ = true
3917 method greetmsg =
3918 if Array.length m_items != Array.length m_orig_items
3919 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3920 else ""
3922 method narrow pattern =
3923 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3924 match reopt with
3925 | None -> ()
3926 | Some re ->
3927 let rec loop accu n =
3928 if n = -1
3929 then (
3930 m_narrow_pattern <- pattern;
3931 m_items <- Array.of_list accu
3933 else
3934 let (s, _, _) as o = m_items.(n) in
3935 let accu =
3936 if (try ignore (Str.search_forward re s 0); true
3937 with Not_found -> false)
3938 then o :: accu
3939 else accu
3941 loop accu (n-1)
3943 loop [] (Array.length m_items - 1)
3945 method denarrow =
3946 m_orig_items <- (
3947 if usebookmarks
3948 then Array.of_list state.bookmarks
3949 else state.outlines
3951 m_items <- m_orig_items
3953 method remove m =
3954 if usebookmarks
3955 then
3956 if m >= 0 && m < Array.length m_items
3957 then (
3958 m_hadremovals <- true;
3959 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3960 let n = if n >= m then n+1 else n in
3961 m_items.(n)
3965 method reset anchor items =
3966 m_hadremovals <- false;
3967 if m_orig_items == empty || m_prev_items != items
3968 then (
3969 m_orig_items <- items;
3970 if String.length m_narrow_pattern = 0
3971 then m_items <- items;
3973 m_prev_items <- items;
3974 let rely = getanchory anchor in
3975 let active =
3976 let rec loop n best bestd =
3977 if n = Array.length m_items
3978 then best
3979 else
3980 let (_, _, anchor) = m_items.(n) in
3981 let orely = getanchory anchor in
3982 let d = abs (orely - rely) in
3983 if d < bestd
3984 then loop (n+1) n d
3985 else loop (n+1) best bestd
3987 loop 0 ~-1 max_int
3989 m_active <- active;
3990 m_first <- firstof m_first active
3991 end)
3994 let enterselector usebookmarks =
3995 let source = outlinesource usebookmarks in
3996 fun errmsg ->
3997 let outlines =
3998 if usebookmarks
3999 then Array.of_list state.bookmarks
4000 else state.outlines
4002 if Array.length outlines = 0
4003 then (
4004 showtext ' ' errmsg;
4006 else (
4007 state.text <- source#greetmsg;
4008 Wsi.setcursor Wsi.CURSOR_INHERIT;
4009 let anchor = getanchor () in
4010 source#reset anchor outlines;
4011 state.uioh <- coe (new outlinelistview ~source);
4012 G.postRedisplay "enter selector";
4016 let enteroutlinemode =
4017 let f = enterselector false in
4018 fun ()-> f "Document has no outline";
4021 let enterbookmarkmode =
4022 let f = enterselector true in
4023 fun () -> f "Document has no bookmarks (yet)";
4026 let color_of_string s =
4027 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4028 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4032 let color_to_string (r, g, b) =
4033 let r = truncate (r *. 256.0)
4034 and g = truncate (g *. 256.0)
4035 and b = truncate (b *. 256.0) in
4036 Printf.sprintf "%d/%d/%d" r g b
4039 let irect_of_string s =
4040 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4043 let irect_to_string (x0,y0,x1,y1) =
4044 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4047 let makecheckers () =
4048 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4049 following to say:
4050 converted by Issac Trotts. July 25, 2002 *)
4051 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4052 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4053 let id = GlTex.gen_texture () in
4054 GlTex.bind_texture `texture_2d id;
4055 GlPix.store (`unpack_alignment 1);
4056 GlTex.image2d image;
4057 List.iter (GlTex.parameter ~target:`texture_2d)
4058 [ `mag_filter `nearest; `min_filter `nearest ];
4062 let setcheckers enabled =
4063 match state.texid with
4064 | None ->
4065 if enabled then state.texid <- Some (makecheckers ())
4067 | Some texid ->
4068 if not enabled
4069 then (
4070 GlTex.delete_texture texid;
4071 state.texid <- None;
4075 let int_of_string_with_suffix s =
4076 let l = String.length s in
4077 let s1, shift =
4078 if l > 1
4079 then
4080 let suffix = Char.lowercase s.[l-1] in
4081 match suffix with
4082 | 'k' -> String.sub s 0 (l-1), 10
4083 | 'm' -> String.sub s 0 (l-1), 20
4084 | 'g' -> String.sub s 0 (l-1), 30
4085 | _ -> s, 0
4086 else s, 0
4088 let n = int_of_string s1 in
4089 let m = n lsl shift in
4090 if m < 0 || m < n
4091 then raise (Failure "value too large")
4092 else m
4095 let string_with_suffix_of_int n =
4096 if n = 0
4097 then "0"
4098 else
4099 let n, s =
4100 if n land ((1 lsl 30) - 1) = 0
4101 then n lsr 30, "G"
4102 else (
4103 if n land ((1 lsl 20) - 1) = 0
4104 then n lsr 20, "M"
4105 else (
4106 if n land ((1 lsl 10) - 1) = 0
4107 then n lsr 10, "K"
4108 else n, ""
4112 let rec loop s n =
4113 let h = n mod 1000 in
4114 let n = n / 1000 in
4115 if n = 0
4116 then string_of_int h ^ s
4117 else (
4118 let s = Printf.sprintf "_%03d%s" h s in
4119 loop s n
4122 loop "" n ^ s;
4125 let defghyllscroll = (40, 8, 32);;
4126 let ghyllscroll_of_string s =
4127 let (n, a, b) as nab =
4128 if s = "default"
4129 then defghyllscroll
4130 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4132 if n <= a || n <= b || a >= b
4133 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4134 nab;
4137 let ghyllscroll_to_string ((n, a, b) as nab) =
4138 if nab = defghyllscroll
4139 then "default"
4140 else Printf.sprintf "%d,%d,%d" n a b;
4143 let describe_location () =
4144 let fn = page_of_y state.y in
4145 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4146 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4147 let percent =
4148 if maxy <= 0
4149 then 100.
4150 else (100. *. (float state.y /. float maxy))
4152 if fn = ln
4153 then
4154 Printf.sprintf "page %d of %d [%.2f%%]"
4155 (fn+1) state.pagecount percent
4156 else
4157 Printf.sprintf
4158 "pages %d-%d of %d [%.2f%%]"
4159 (fn+1) (ln+1) state.pagecount percent
4162 let setpresentationmode v =
4163 let n = page_of_y state.y in
4164 state.anchor <- (n, 0.0, 1.0);
4165 conf.presentation <- v;
4166 if conf.presentation
4167 then (
4168 if not conf.scrollbarinpm
4169 then state.scrollw <- 0;
4171 else state.scrollw <- conf.scrollbw;
4172 represent ();
4175 let enterinfomode =
4176 let btos b = if b then "\xe2\x88\x9a" else "" in
4177 let showextended = ref false in
4178 let leave mode = function
4179 | Confirm -> state.mode <- mode
4180 | Cancel -> state.mode <- mode in
4181 let src =
4182 (object
4183 val mutable m_first_time = true
4184 val mutable m_l = []
4185 val mutable m_a = [||]
4186 val mutable m_prev_uioh = nouioh
4187 val mutable m_prev_mode = View
4189 inherit lvsourcebase
4191 method reset prev_mode prev_uioh =
4192 m_a <- Array.of_list (List.rev m_l);
4193 m_l <- [];
4194 m_prev_mode <- prev_mode;
4195 m_prev_uioh <- prev_uioh;
4196 if m_first_time
4197 then (
4198 let rec loop n =
4199 if n >= Array.length m_a
4200 then ()
4201 else
4202 match m_a.(n) with
4203 | _, _, _, Action _ -> m_active <- n
4204 | _ -> loop (n+1)
4206 loop 0;
4207 m_first_time <- false;
4210 method int name get set =
4211 m_l <-
4212 (name, `int get, 1, Action (
4213 fun u ->
4214 let ondone s =
4215 try set (int_of_string s)
4216 with exn ->
4217 state.text <- Printf.sprintf "bad integer `%s': %s"
4218 s (exntos exn)
4220 state.text <- "";
4221 let te = name ^ ": ", "", None, intentry, ondone, true in
4222 state.mode <- Textentry (te, leave m_prev_mode);
4224 )) :: m_l
4226 method int_with_suffix name get set =
4227 m_l <-
4228 (name, `intws get, 1, Action (
4229 fun u ->
4230 let ondone s =
4231 try set (int_of_string_with_suffix s)
4232 with exn ->
4233 state.text <- Printf.sprintf "bad integer `%s': %s"
4234 s (exntos exn)
4236 state.text <- "";
4237 let te =
4238 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4240 state.mode <- Textentry (te, leave m_prev_mode);
4242 )) :: m_l
4244 method bool ?(offset=1) ?(btos=btos) name get set =
4245 m_l <-
4246 (name, `bool (btos, get), offset, Action (
4247 fun u ->
4248 let v = get () in
4249 set (not v);
4251 )) :: m_l
4253 method color name get set =
4254 m_l <-
4255 (name, `color get, 1, Action (
4256 fun u ->
4257 let invalid = (nan, nan, nan) in
4258 let ondone s =
4259 let c =
4260 try color_of_string s
4261 with exn ->
4262 state.text <- Printf.sprintf "bad color `%s': %s"
4263 s (exntos exn);
4264 invalid
4266 if c <> invalid
4267 then set c;
4269 let te = name ^ ": ", "", None, textentry, ondone, true in
4270 state.text <- color_to_string (get ());
4271 state.mode <- Textentry (te, leave m_prev_mode);
4273 )) :: m_l
4275 method string name get set =
4276 m_l <-
4277 (name, `string get, 1, Action (
4278 fun u ->
4279 let ondone s = set s in
4280 let te = name ^ ": ", "", None, textentry, ondone, true in
4281 state.mode <- Textentry (te, leave m_prev_mode);
4283 )) :: m_l
4285 method colorspace name get set =
4286 m_l <-
4287 (name, `string get, 1, Action (
4288 fun _ ->
4289 let source =
4290 let vals = [| "rgb"; "bgr"; "gray" |] in
4291 (object
4292 inherit lvsourcebase
4294 initializer
4295 m_active <- int_of_colorspace conf.colorspace;
4296 m_first <- 0;
4298 method getitemcount = Array.length vals
4299 method getitem n = (vals.(n), 0)
4300 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4301 ignore (uioh, first, pan, qsearch);
4302 if not cancel then set active;
4303 None
4304 method hasaction _ = true
4305 end)
4307 state.text <- "";
4308 let modehash = findkeyhash conf "info" in
4309 coe (new listview ~source ~trusted:true ~modehash)
4310 )) :: m_l
4312 method fitmodel name get set =
4313 m_l <-
4314 (name, `string get, 1, Action (
4315 fun _ ->
4316 let source =
4317 let vals = [| "fit width"; "proportional"; "fit page" |] in
4318 (object
4319 inherit lvsourcebase
4321 initializer
4322 m_active <- int_of_fitmodel conf.fitmodel;
4323 m_first <- 0;
4325 method getitemcount = Array.length vals
4326 method getitem n = (vals.(n), 0)
4327 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4328 ignore (uioh, first, pan, qsearch);
4329 if not cancel then set active;
4330 None
4331 method hasaction _ = true
4332 end)
4334 state.text <- "";
4335 let modehash = findkeyhash conf "info" in
4336 coe (new listview ~source ~trusted:true ~modehash)
4337 )) :: m_l
4339 method caption s offset =
4340 m_l <- (s, `empty, offset, Noaction) :: m_l
4342 method caption2 s f offset =
4343 m_l <- (s, `string f, offset, Noaction) :: m_l
4345 method getitemcount = Array.length m_a
4347 method getitem n =
4348 let tostr = function
4349 | `int f -> string_of_int (f ())
4350 | `intws f -> string_with_suffix_of_int (f ())
4351 | `string f -> f ()
4352 | `color f -> color_to_string (f ())
4353 | `bool (btos, f) -> btos (f ())
4354 | `empty -> ""
4356 let name, t, offset, _ = m_a.(n) in
4357 ((let s = tostr t in
4358 if String.length s > 0
4359 then Printf.sprintf "%s\t%s" name s
4360 else name),
4361 offset)
4363 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4364 let uiohopt =
4365 if not cancel
4366 then (
4367 m_qsearch <- qsearch;
4368 let uioh =
4369 match m_a.(active) with
4370 | _, _, _, Action f -> f uioh
4371 | _ -> uioh
4373 Some uioh
4375 else None
4377 m_active <- active;
4378 m_first <- first;
4379 m_pan <- pan;
4380 uiohopt
4382 method hasaction n =
4383 match m_a.(n) with
4384 | _, _, _, Action _ -> true
4385 | _ -> false
4386 end)
4388 let rec fillsrc prevmode prevuioh =
4389 let sep () = src#caption "" 0 in
4390 let colorp name get set =
4391 src#string name
4392 (fun () -> color_to_string (get ()))
4393 (fun v ->
4395 let c = color_of_string v in
4396 set c
4397 with exn ->
4398 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4401 let oldmode = state.mode in
4402 let birdseye = isbirdseye state.mode in
4404 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4406 src#bool "presentation mode"
4407 (fun () -> conf.presentation)
4408 (fun v -> setpresentationmode v);
4410 src#bool "ignore case in searches"
4411 (fun () -> conf.icase)
4412 (fun v -> conf.icase <- v);
4414 src#bool "preload"
4415 (fun () -> conf.preload)
4416 (fun v -> conf.preload <- v);
4418 src#bool "highlight links"
4419 (fun () -> conf.hlinks)
4420 (fun v -> conf.hlinks <- v);
4422 src#bool "under info"
4423 (fun () -> conf.underinfo)
4424 (fun v -> conf.underinfo <- v);
4426 src#bool "persistent bookmarks"
4427 (fun () -> conf.savebmarks)
4428 (fun v -> conf.savebmarks <- v);
4430 src#fitmodel "fit model"
4431 (fun () -> fitmodel_to_string conf.fitmodel)
4432 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4434 src#bool "trim margins"
4435 (fun () -> conf.trimmargins)
4436 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4438 src#bool "persistent location"
4439 (fun () -> conf.jumpback)
4440 (fun v -> conf.jumpback <- v);
4442 sep ();
4443 src#int "inter-page space"
4444 (fun () -> conf.interpagespace)
4445 (fun n ->
4446 conf.interpagespace <- n;
4447 docolumns conf.columns;
4448 let pageno, py =
4449 match state.layout with
4450 | [] -> 0, 0
4451 | l :: _ ->
4452 l.pageno, l.pagey
4454 state.maxy <- calcheight ();
4455 let y = getpagey pageno in
4456 gotoy (y + py)
4459 src#int "page bias"
4460 (fun () -> conf.pagebias)
4461 (fun v -> conf.pagebias <- v);
4463 src#int "scroll step"
4464 (fun () -> conf.scrollstep)
4465 (fun n -> conf.scrollstep <- n);
4467 src#int "horizontal scroll step"
4468 (fun () -> conf.hscrollstep)
4469 (fun v -> conf.hscrollstep <- v);
4471 src#int "auto scroll step"
4472 (fun () ->
4473 match state.autoscroll with
4474 | Some step -> step
4475 | _ -> conf.autoscrollstep)
4476 (fun n ->
4477 if state.autoscroll <> None
4478 then state.autoscroll <- Some n;
4479 conf.autoscrollstep <- n);
4481 src#int "zoom"
4482 (fun () -> truncate (conf.zoom *. 100.))
4483 (fun v -> setzoom ((float v) /. 100.));
4485 src#int "rotation"
4486 (fun () -> conf.angle)
4487 (fun v -> reqlayout v conf.fitmodel);
4489 src#int "scroll bar width"
4490 (fun () -> state.scrollw)
4491 (fun v ->
4492 state.scrollw <- v;
4493 conf.scrollbw <- v;
4494 reshape state.winw state.winh;
4497 src#int "scroll handle height"
4498 (fun () -> conf.scrollh)
4499 (fun v -> conf.scrollh <- v;);
4501 src#int "thumbnail width"
4502 (fun () -> conf.thumbw)
4503 (fun v ->
4504 conf.thumbw <- min 4096 v;
4505 match oldmode with
4506 | Birdseye beye ->
4507 leavebirdseye beye false;
4508 enterbirdseye ()
4509 | _ -> ()
4512 let mode = state.mode in
4513 src#string "columns"
4514 (fun () ->
4515 match conf.columns with
4516 | Csingle _ -> "1"
4517 | Cmulti (multi, _) -> multicolumns_to_string multi
4518 | Csplit (count, _) -> "-" ^ string_of_int count
4520 (fun v ->
4521 let n, a, b = multicolumns_of_string v in
4522 setcolumns mode n a b);
4524 sep ();
4525 src#caption "Presentation mode" 0;
4526 src#bool "scrollbar visible"
4527 (fun () -> conf.scrollbarinpm)
4528 (fun v ->
4529 if v != conf.scrollbarinpm
4530 then (
4531 conf.scrollbarinpm <- v;
4532 if conf.presentation
4533 then (
4534 state.scrollw <- if v then conf.scrollbw else 0;
4535 reshape state.winw state.winh;
4540 sep ();
4541 src#caption "Pixmap cache" 0;
4542 src#int_with_suffix "size (advisory)"
4543 (fun () -> conf.memlimit)
4544 (fun v -> conf.memlimit <- v);
4546 src#caption2 "used"
4547 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4548 (string_with_suffix_of_int state.memused)
4549 (Hashtbl.length state.tilemap)) 1;
4551 sep ();
4552 src#caption "Layout" 0;
4553 src#caption2 "Dimension"
4554 (fun () ->
4555 Printf.sprintf "%dx%d (virtual %dx%d)"
4556 state.winw state.winh
4557 state.w state.maxy)
4559 if conf.debug
4560 then
4561 src#caption2 "Position" (fun () ->
4562 Printf.sprintf "%dx%d" state.x state.y
4564 else
4565 src#caption2 "Position" (fun () -> describe_location ()) 1
4568 sep ();
4569 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4570 "Save these parameters as global defaults at exit"
4571 (fun () -> conf.bedefault)
4572 (fun v -> conf.bedefault <- v)
4575 sep ();
4576 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4577 src#bool ~offset:0 ~btos "Extended parameters"
4578 (fun () -> !showextended)
4579 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4580 if !showextended
4581 then (
4582 src#bool "checkers"
4583 (fun () -> conf.checkers)
4584 (fun v -> conf.checkers <- v; setcheckers v);
4585 src#bool "update cursor"
4586 (fun () -> conf.updatecurs)
4587 (fun v -> conf.updatecurs <- v);
4588 src#bool "verbose"
4589 (fun () -> conf.verbose)
4590 (fun v -> conf.verbose <- v);
4591 src#bool "invert colors"
4592 (fun () -> conf.invert)
4593 (fun v -> conf.invert <- v);
4594 src#bool "max fit"
4595 (fun () -> conf.maxhfit)
4596 (fun v -> conf.maxhfit <- v);
4597 src#bool "redirect stderr"
4598 (fun () -> conf.redirectstderr)
4599 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4600 src#string "uri launcher"
4601 (fun () -> conf.urilauncher)
4602 (fun v -> conf.urilauncher <- v);
4603 src#string "path launcher"
4604 (fun () -> conf.pathlauncher)
4605 (fun v -> conf.pathlauncher <- v);
4606 src#string "tile size"
4607 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4608 (fun v ->
4610 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4611 conf.tilew <- max 64 w;
4612 conf.tileh <- max 64 h;
4613 flushtiles ();
4614 with exn ->
4615 state.text <- Printf.sprintf "bad tile size `%s': %s"
4616 v (exntos exn)
4618 src#int "texture count"
4619 (fun () -> conf.texcount)
4620 (fun v ->
4621 if realloctexts v
4622 then conf.texcount <- v
4623 else showtext '!' " Failed to set texture count please retry later"
4625 src#int "slice height"
4626 (fun () -> conf.sliceheight)
4627 (fun v ->
4628 conf.sliceheight <- v;
4629 wcmd "sliceh %d" conf.sliceheight;
4631 src#int "anti-aliasing level"
4632 (fun () -> conf.aalevel)
4633 (fun v ->
4634 conf.aalevel <- bound v 0 8;
4635 state.anchor <- getanchor ();
4636 opendoc state.path state.password;
4638 src#string "page scroll scaling factor"
4639 (fun () -> string_of_float conf.pgscale)
4640 (fun v ->
4642 let s = float_of_string v in
4643 conf.pgscale <- s
4644 with exn ->
4645 state.text <- Printf.sprintf
4646 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4649 src#int "ui font size"
4650 (fun () -> fstate.fontsize)
4651 (fun v -> setfontsize (bound v 5 100));
4652 src#int "hint font size"
4653 (fun () -> conf.hfsize)
4654 (fun v -> conf.hfsize <- bound v 5 100);
4655 colorp "background color"
4656 (fun () -> conf.bgcolor)
4657 (fun v -> conf.bgcolor <- v);
4658 src#bool "crop hack"
4659 (fun () -> conf.crophack)
4660 (fun v -> conf.crophack <- v);
4661 src#string "trim fuzz"
4662 (fun () -> irect_to_string conf.trimfuzz)
4663 (fun v ->
4665 conf.trimfuzz <- irect_of_string v;
4666 if conf.trimmargins
4667 then settrim true conf.trimfuzz;
4668 with exn ->
4669 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4671 src#string "throttle"
4672 (fun () ->
4673 match conf.maxwait with
4674 | None -> "show place holder if page is not ready"
4675 | Some time ->
4676 if time = infinity
4677 then "wait for page to fully render"
4678 else
4679 "wait " ^ string_of_float time
4680 ^ " seconds before showing placeholder"
4682 (fun v ->
4684 let f = float_of_string v in
4685 if f <= 0.0
4686 then conf.maxwait <- None
4687 else conf.maxwait <- Some f
4688 with exn ->
4689 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4691 src#string "ghyll scroll"
4692 (fun () ->
4693 match conf.ghyllscroll with
4694 | None -> ""
4695 | Some nab -> ghyllscroll_to_string nab
4697 (fun v ->
4699 let gs =
4700 if String.length v = 0
4701 then None
4702 else Some (ghyllscroll_of_string v)
4704 conf.ghyllscroll <- gs
4705 with exn ->
4706 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4708 src#string "selection command"
4709 (fun () -> conf.selcmd)
4710 (fun v -> conf.selcmd <- v);
4711 src#string "synctex command"
4712 (fun () -> conf.stcmd)
4713 (fun v -> conf.stcmd <- v);
4714 src#colorspace "color space"
4715 (fun () -> colorspace_to_string conf.colorspace)
4716 (fun v ->
4717 conf.colorspace <- colorspace_of_int v;
4718 wcmd "cs %d" v;
4719 load state.layout;
4721 if pbousable ()
4722 then
4723 src#bool "use PBO"
4724 (fun () -> conf.usepbo)
4725 (fun v -> conf.usepbo <- v);
4726 src#bool "mouse wheel scrolls pages"
4727 (fun () -> conf.wheelbypage)
4728 (fun v -> conf.wheelbypage <- v);
4731 sep ();
4732 src#caption "Document" 0;
4733 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4734 src#caption2 "Pages"
4735 (fun () -> string_of_int state.pagecount) 1;
4736 src#caption2 "Dimensions"
4737 (fun () -> string_of_int (List.length state.pdims)) 1;
4738 if conf.trimmargins
4739 then (
4740 sep ();
4741 src#caption "Trimmed margins" 0;
4742 src#caption2 "Dimensions"
4743 (fun () -> string_of_int (List.length state.pdims)) 1;
4746 sep ();
4747 src#caption "OpenGL" 0;
4748 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4749 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4751 sep ();
4752 src#caption "Location" 0;
4753 if String.length state.origin > 0
4754 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4755 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4757 src#reset prevmode prevuioh;
4759 fun () ->
4760 state.text <- "";
4761 let prevmode = state.mode
4762 and prevuioh = state.uioh in
4763 fillsrc prevmode prevuioh;
4764 let source = (src :> lvsource) in
4765 let modehash = findkeyhash conf "info" in
4766 state.uioh <- coe (object (self)
4767 inherit listview ~source ~trusted:true ~modehash as super
4768 val mutable m_prevmemused = 0
4769 method infochanged = function
4770 | Memused ->
4771 if m_prevmemused != state.memused
4772 then (
4773 m_prevmemused <- state.memused;
4774 G.postRedisplay "memusedchanged";
4776 | Pdim -> G.postRedisplay "pdimchanged"
4777 | Docinfo -> fillsrc prevmode prevuioh
4779 method key key mask =
4780 if not (Wsi.withctrl mask)
4781 then
4782 match key with
4783 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4784 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4785 | _ -> super#key key mask
4786 else super#key key mask
4787 end);
4788 G.postRedisplay "info";
4791 let enterhelpmode =
4792 let source =
4793 (object
4794 inherit lvsourcebase
4795 method getitemcount = Array.length state.help
4796 method getitem n =
4797 let s, l, _ = state.help.(n) in
4798 (s, l)
4800 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4801 let optuioh =
4802 if not cancel
4803 then (
4804 m_qsearch <- qsearch;
4805 match state.help.(active) with
4806 | _, _, Action f -> Some (f uioh)
4807 | _ -> Some (uioh)
4809 else None
4811 m_active <- active;
4812 m_first <- first;
4813 m_pan <- pan;
4814 optuioh
4816 method hasaction n =
4817 match state.help.(n) with
4818 | _, _, Action _ -> true
4819 | _ -> false
4821 initializer
4822 m_active <- -1
4823 end)
4824 in fun () ->
4825 let modehash = findkeyhash conf "help" in
4826 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4827 G.postRedisplay "help";
4830 let entermsgsmode =
4831 let msgsource =
4832 let re = Str.regexp "[\r\n]" in
4833 (object
4834 inherit lvsourcebase
4835 val mutable m_items = [||]
4837 method getitemcount = 1 + Array.length m_items
4839 method getitem n =
4840 if n = 0
4841 then "[Clear]", 0
4842 else m_items.(n-1), 0
4844 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4845 ignore uioh;
4846 if not cancel
4847 then (
4848 if active = 0
4849 then Buffer.clear state.errmsgs;
4850 m_qsearch <- qsearch;
4852 m_active <- active;
4853 m_first <- first;
4854 m_pan <- pan;
4855 None
4857 method hasaction n =
4858 n = 0
4860 method reset =
4861 state.newerrmsgs <- false;
4862 let l = Str.split re (Buffer.contents state.errmsgs) in
4863 m_items <- Array.of_list l
4865 initializer
4866 m_active <- 0
4867 end)
4868 in fun () ->
4869 state.text <- "";
4870 msgsource#reset;
4871 let source = (msgsource :> lvsource) in
4872 let modehash = findkeyhash conf "listview" in
4873 state.uioh <- coe (object
4874 inherit listview ~source ~trusted:false ~modehash as super
4875 method display =
4876 if state.newerrmsgs
4877 then msgsource#reset;
4878 super#display
4879 end);
4880 G.postRedisplay "msgs";
4883 let quickbookmark ?title () =
4884 match state.layout with
4885 | [] -> ()
4886 | l :: _ ->
4887 let title =
4888 match title with
4889 | None ->
4890 let sec = Unix.gettimeofday () in
4891 let tm = Unix.localtime sec in
4892 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4893 (l.pageno+1)
4894 tm.Unix.tm_mday
4895 tm.Unix.tm_mon
4896 (tm.Unix.tm_year + 1900)
4897 tm.Unix.tm_hour
4898 tm.Unix.tm_min
4899 | Some title -> title
4901 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4904 let setautoscrollspeed step goingdown =
4905 let incr = max 1 ((abs step) / 2) in
4906 let incr = if goingdown then incr else -incr in
4907 let astep = step + incr in
4908 state.autoscroll <- Some astep;
4911 let gotounder = function
4912 | Ulinkgoto (pageno, top) ->
4913 if pageno >= 0
4914 then (
4915 addnav ();
4916 gotopage1 pageno top;
4919 | Ulinkuri s ->
4920 gotouri s
4922 | Uremote (filename, pageno) ->
4923 let path =
4924 if Sys.file_exists filename
4925 then filename
4926 else
4927 let dir = Filename.dirname state.path in
4928 let path = Filename.concat dir filename in
4929 if Sys.file_exists path
4930 then path
4931 else ""
4933 if String.length path > 0
4934 then (
4935 let anchor = getanchor () in
4936 let ranchor = state.path, state.password, anchor, state.origin in
4937 state.origin <- "";
4938 state.anchor <- (pageno, 0.0, 0.0);
4939 state.ranchors <- ranchor :: state.ranchors;
4940 opendoc path "";
4942 else showtext '!' ("Could not find " ^ filename)
4944 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4947 let canpan () =
4948 match conf.columns with
4949 | Csplit _ -> true
4950 | _ -> state.x != 0 || conf.zoom > 1.0
4953 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4955 let existsinrow pageno (columns, coverA, coverB) p =
4956 let last = ((pageno - coverA) mod columns) + columns in
4957 let rec any = function
4958 | [] -> false
4959 | l :: rest ->
4960 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4961 then p l
4962 else (
4963 if not (p l)
4964 then (if l.pageno = last then false else any rest)
4965 else true
4968 any state.layout
4971 let nextpage () =
4972 match state.layout with
4973 | [] ->
4974 let pageno = page_of_y state.y in
4975 gotoghyll (getpagey (pageno+1))
4976 | l :: rest ->
4977 match conf.columns with
4978 | Csingle _ ->
4979 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4980 then
4981 let y = clamp (pgscale state.winh) in
4982 gotoghyll y
4983 else
4984 let pageno = min (l.pageno+1) (state.pagecount-1) in
4985 gotoghyll (getpagey pageno)
4986 | Cmulti ((c, _, _) as cl, _) ->
4987 if conf.presentation
4988 && (existsinrow l.pageno cl
4989 (fun l -> l.pageh > l.pagey + l.pagevh))
4990 then
4991 let y = clamp (pgscale state.winh) in
4992 gotoghyll y
4993 else
4994 let pageno = min (l.pageno+c) (state.pagecount-1) in
4995 gotoghyll (getpagey pageno)
4996 | Csplit (n, _) ->
4997 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4998 then
4999 let pagey, pageh = getpageyh l.pageno in
5000 let pagey = pagey + pageh * l.pagecol in
5001 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5002 gotoghyll (pagey + pageh + ips)
5005 let prevpage () =
5006 match state.layout with
5007 | [] ->
5008 let pageno = page_of_y state.y in
5009 gotoghyll (getpagey (pageno-1))
5010 | l :: _ ->
5011 match conf.columns with
5012 | Csingle _ ->
5013 if conf.presentation && l.pagey != 0
5014 then
5015 gotoghyll (clamp (pgscale ~-(state.winh)))
5016 else
5017 let pageno = max 0 (l.pageno-1) in
5018 gotoghyll (getpagey pageno)
5019 | Cmulti ((c, _, coverB) as cl, _) ->
5020 if conf.presentation &&
5021 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5022 then
5023 gotoghyll (clamp (pgscale ~-(state.winh)))
5024 else
5025 let decr =
5026 if l.pageno = state.pagecount - coverB
5027 then 1
5028 else c
5030 let pageno = max 0 (l.pageno-decr) in
5031 gotoghyll (getpagey pageno)
5032 | Csplit (n, _) ->
5033 let y =
5034 if l.pagecol = 0
5035 then
5036 if l.pageno = 0
5037 then l.pagey
5038 else
5039 let pageno = max 0 (l.pageno-1) in
5040 let pagey, pageh = getpageyh pageno in
5041 pagey + (n-1)*pageh
5042 else
5043 let pagey, pageh = getpageyh l.pageno in
5044 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5046 gotoghyll y
5049 let viewkeyboard key mask =
5050 let enttext te =
5051 let mode = state.mode in
5052 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5053 state.text <- "";
5054 enttext ();
5055 G.postRedisplay "view:enttext"
5057 let ctrl = Wsi.withctrl mask in
5058 let key =
5059 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5061 match key with
5062 | 81 -> (* Q *)
5063 exit 0
5065 | 0xff63 -> (* insert *)
5066 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5067 then (
5068 state.mode <- LinkNav (Ltgendir 0);
5069 gotoy state.y;
5071 else showtext '!' "Keyboard link navigation does not work under rotation"
5073 | 0xff1b | 113 -> (* escape / q *)
5074 begin match state.mstate with
5075 | Mzoomrect _ ->
5076 state.mstate <- Mnone;
5077 Wsi.setcursor Wsi.CURSOR_INHERIT;
5078 G.postRedisplay "kill zoom rect";
5079 | _ ->
5080 begin match state.mode with
5081 | LinkNav _ ->
5082 state.mode <- View;
5083 G.postRedisplay "esc leave linknav"
5084 | _ ->
5085 match state.ranchors with
5086 | [] -> raise Quit
5087 | (path, password, anchor, origin) :: rest ->
5088 state.ranchors <- rest;
5089 state.anchor <- anchor;
5090 state.origin <- origin;
5091 opendoc path password
5092 end;
5093 end;
5095 | 0xff08 -> (* backspace *)
5096 gotoghyll (getnav ~-1)
5098 | 111 -> (* o *)
5099 enteroutlinemode ()
5101 | 117 -> (* u *)
5102 state.rects <- [];
5103 state.text <- "";
5104 G.postRedisplay "dehighlight";
5106 | 47 | 63 -> (* / ? *)
5107 let ondone isforw s =
5108 cbput state.hists.pat s;
5109 state.searchpattern <- s;
5110 search s isforw
5112 let s = String.create 1 in
5113 s.[0] <- Char.chr key;
5114 enttext (s, "", Some (onhist state.hists.pat),
5115 textentry, ondone (key = 47), true)
5117 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5118 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5119 setzoom (conf.zoom +. incr)
5121 | 43 | 0xffab -> (* + *)
5122 let ondone s =
5123 let n =
5124 try int_of_string s with exc ->
5125 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5126 max_int
5128 if n != max_int
5129 then (
5130 conf.pagebias <- n;
5131 state.text <- "page bias is now " ^ string_of_int n;
5134 enttext ("page bias: ", "", None, intentry, ondone, true)
5136 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5137 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5138 setzoom (max 0.01 (conf.zoom -. decr))
5140 | 45 | 0xffad -> (* - *)
5141 let ondone msg = state.text <- msg in
5142 enttext (
5143 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5144 optentry state.mode, ondone, true
5147 | 48 when ctrl -> (* ctrl-0 *)
5148 if conf.zoom = 1.0
5149 then (
5150 state.x <- 0;
5151 state.hscrollh <-
5152 if state.w <= state.winw - state.scrollw
5153 then 0
5154 else state.scrollw
5156 gotoy state.y
5158 else setzoom 1.0
5160 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5161 let cols =
5162 match conf.columns with
5163 | Csingle _ | Cmulti _ -> 1
5164 | Csplit (n, _) -> n
5166 let h = state.winh -
5167 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5169 let zoom = zoomforh state.winw h state.scrollw cols in
5170 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5171 then setzoom zoom
5173 | 51 when ctrl -> (* ctrl-3 *)
5174 let fm =
5175 match conf.fitmodel with
5176 | FitWidth -> FitProportional
5177 | FitProportional -> FitPage
5178 | FitPage -> FitWidth
5180 state.text <- "fit model: " ^ fitmodel_to_string fm;
5181 reqlayout conf.angle fm
5183 | 0xffc6 -> (* f9 *)
5184 togglebirdseye ()
5186 | 57 when ctrl -> (* ctrl-9 *)
5187 togglebirdseye ()
5189 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5190 when not ctrl -> (* 0..9 *)
5191 let ondone s =
5192 let n =
5193 try int_of_string s with exc ->
5194 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5197 if n >= 0
5198 then (
5199 addnav ();
5200 cbput state.hists.pag (string_of_int n);
5201 gotopage1 (n + conf.pagebias - 1) 0;
5204 let pageentry text key =
5205 match Char.unsafe_chr key with
5206 | 'g' -> TEdone text
5207 | _ -> intentry text key
5209 let text = "x" in text.[0] <- Char.chr key;
5210 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5212 | 98 -> (* b *)
5213 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5214 reshape state.winw state.winh;
5216 | 108 -> (* l *)
5217 conf.hlinks <- not conf.hlinks;
5218 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5219 G.postRedisplay "toggle highlightlinks";
5221 | 70 -> (* F *)
5222 state.glinks <- true;
5223 let mode = state.mode in
5224 state.mode <- Textentry (
5225 (":", "", None, linknentry, linkndone gotounder, false),
5226 (fun _ ->
5227 state.glinks <- false;
5228 state.mode <- mode)
5230 state.text <- "";
5231 G.postRedisplay "view:linkent(F)"
5233 | 121 -> (* y *)
5234 state.glinks <- true;
5235 let mode = state.mode in
5236 state.mode <- Textentry (
5238 ":", "", None, linknentry, linkndone (fun under ->
5239 selstring (undertext under);
5240 ), false
5242 fun _ ->
5243 state.glinks <- false;
5244 state.mode <- mode
5246 state.text <- "";
5247 G.postRedisplay "view:linkent"
5249 | 97 -> (* a *)
5250 begin match state.autoscroll with
5251 | Some step ->
5252 conf.autoscrollstep <- step;
5253 state.autoscroll <- None
5254 | None ->
5255 if conf.autoscrollstep = 0
5256 then state.autoscroll <- Some 1
5257 else state.autoscroll <- Some conf.autoscrollstep
5260 | 112 when ctrl -> (* ctrl-p *)
5261 launchpath ()
5263 | 80 -> (* P *)
5264 setpresentationmode (not conf.presentation);
5265 showtext ' ' ("presentation mode " ^
5266 if conf.presentation then "on" else "off");
5268 | 102 -> (* f *)
5269 if List.mem Wsi.Fullscreen state.winstate
5270 then Wsi.reshape conf.cwinw conf.cwinh
5271 else Wsi.fullscreen ()
5273 | 112 | 78 -> (* p|N *)
5274 search state.searchpattern false
5276 | 110 | 0xffc0 -> (* n|F3 *)
5277 search state.searchpattern true
5279 | 116 -> (* t *)
5280 begin match state.layout with
5281 | [] -> ()
5282 | l :: _ ->
5283 gotoghyll (getpagey l.pageno)
5286 | 32 -> (* space *)
5287 nextpage ()
5289 | 0xff9f | 0xffff -> (* delete *)
5290 prevpage ()
5292 | 61 -> (* = *)
5293 showtext ' ' (describe_location ());
5295 | 119 -> (* w *)
5296 begin match state.layout with
5297 | [] -> ()
5298 | l :: _ ->
5299 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5300 G.postRedisplay "w"
5303 | 39 -> (* ' *)
5304 enterbookmarkmode ()
5306 | 104 | 0xffbe -> (* h|F1 *)
5307 enterhelpmode ()
5309 | 105 -> (* i *)
5310 enterinfomode ()
5312 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5313 entermsgsmode ()
5315 | 109 -> (* m *)
5316 let ondone s =
5317 match state.layout with
5318 | l :: _ ->
5319 if String.length s > 0
5320 then
5321 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5322 | _ -> ()
5324 enttext ("bookmark: ", "", None, textentry, ondone, true)
5326 | 126 -> (* ~ *)
5327 quickbookmark ();
5328 showtext ' ' "Quick bookmark added";
5330 | 122 -> (* z *)
5331 begin match state.layout with
5332 | l :: _ ->
5333 let rect = getpdimrect l.pagedimno in
5334 let w, h =
5335 if conf.crophack
5336 then
5337 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5338 truncate (1.2 *. (rect.(3) -. rect.(0))))
5339 else
5340 (truncate (rect.(1) -. rect.(0)),
5341 truncate (rect.(3) -. rect.(0)))
5343 let w = truncate ((float w)*.conf.zoom)
5344 and h = truncate ((float h)*.conf.zoom) in
5345 if w != 0 && h != 0
5346 then (
5347 state.anchor <- getanchor ();
5348 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5350 G.postRedisplay "z";
5352 | [] -> ()
5355 | 60 | 62 -> (* < > *)
5356 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5358 | 91 | 93 -> (* [ ] *)
5359 conf.colorscale <-
5360 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5362 G.postRedisplay "brightness";
5364 | 99 when state.mode = View -> (* c *)
5365 let (c, a, b), z =
5366 match state.prevcolumns with
5367 | None -> (1, 0, 0), 1.0
5368 | Some (columns, z) ->
5369 let cab =
5370 match columns with
5371 | Csplit (c, _) -> -c, 0, 0
5372 | Cmulti ((c, a, b), _) -> c, a, b
5373 | Csingle _ -> 1, 0, 0
5375 cab, z
5377 setcolumns View c a b;
5378 setzoom z;
5380 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5381 setzoom state.prevzoom
5383 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5384 begin match state.autoscroll with
5385 | None ->
5386 begin match state.mode with
5387 | Birdseye beye -> upbirdseye 1 beye
5388 | _ ->
5389 if ctrl
5390 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5391 else (
5392 if not (Wsi.withshift mask) && conf.presentation
5393 then prevpage ()
5394 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5397 | Some n ->
5398 setautoscrollspeed n false
5401 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5402 begin match state.autoscroll with
5403 | None ->
5404 begin match state.mode with
5405 | Birdseye beye -> downbirdseye 1 beye
5406 | _ ->
5407 if ctrl
5408 then gotoy_and_clear_text (clamp (state.winh/2))
5409 else (
5410 if not (Wsi.withshift mask) && conf.presentation
5411 then nextpage ()
5412 else gotoy_and_clear_text (clamp conf.scrollstep)
5415 | Some n ->
5416 setautoscrollspeed n true
5419 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5420 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5421 if canpan ()
5422 then
5423 let dx =
5424 if ctrl
5425 then state.winw / 2
5426 else conf.hscrollstep
5428 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5429 state.x <- panbound (state.x + dx);
5430 gotoy_and_clear_text state.y
5431 else (
5432 state.text <- "";
5433 G.postRedisplay "left/right"
5436 | 0xff55 | 0xff9a -> (* (kp) prior *)
5437 let y =
5438 if ctrl
5439 then
5440 match state.layout with
5441 | [] -> state.y
5442 | l :: _ -> state.y - l.pagey
5443 else
5444 clamp (pgscale (-state.winh))
5446 gotoghyll y
5448 | 0xff56 | 0xff9b -> (* (kp) next *)
5449 let y =
5450 if ctrl
5451 then
5452 match List.rev state.layout with
5453 | [] -> state.y
5454 | l :: _ -> getpagey l.pageno
5455 else
5456 clamp (pgscale state.winh)
5458 gotoghyll y
5460 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5461 gotoghyll 0
5462 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5463 gotoghyll (clamp state.maxy)
5465 | 0xff53 | 0xff98
5466 when Wsi.withalt mask -> (* alt-(kp) right *)
5467 gotoghyll (getnav 1)
5468 | 0xff51 | 0xff96
5469 when Wsi.withalt mask -> (* alt-(kp) left *)
5470 gotoghyll (getnav ~-1)
5472 | 114 -> (* r *)
5473 reload ()
5475 | 118 when conf.debug -> (* v *)
5476 state.rects <- [];
5477 List.iter (fun l ->
5478 match getopaque l.pageno with
5479 | None -> ()
5480 | Some opaque ->
5481 let x0, y0, x1, y1 = pagebbox opaque in
5482 let a,b = float x0, float y0 in
5483 let c,d = float x1, float y0 in
5484 let e,f = float x1, float y1 in
5485 let h,j = float x0, float y1 in
5486 let rect = (a,b,c,d,e,f,h,j) in
5487 debugrect rect;
5488 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5489 ) state.layout;
5490 G.postRedisplay "v";
5492 | _ ->
5493 vlog "huh? %s" (Wsi.keyname key)
5496 let linknavkeyboard key mask linknav =
5497 let getpage pageno =
5498 let rec loop = function
5499 | [] -> None
5500 | l :: _ when l.pageno = pageno -> Some l
5501 | _ :: rest -> loop rest
5502 in loop state.layout
5504 let doexact (pageno, n) =
5505 match getopaque pageno, getpage pageno with
5506 | Some opaque, Some l ->
5507 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5508 then
5509 let under = getlink opaque n in
5510 G.postRedisplay "link gotounder";
5511 gotounder under;
5512 state.mode <- View;
5513 else
5514 let opt, dir =
5515 match key with
5516 | 0xff50 -> (* home *)
5517 Some (findlink opaque LDfirst), -1
5519 | 0xff57 -> (* end *)
5520 Some (findlink opaque LDlast), 1
5522 | 0xff51 -> (* left *)
5523 Some (findlink opaque (LDleft n)), -1
5525 | 0xff53 -> (* right *)
5526 Some (findlink opaque (LDright n)), 1
5528 | 0xff52 -> (* up *)
5529 Some (findlink opaque (LDup n)), -1
5531 | 0xff54 -> (* down *)
5532 Some (findlink opaque (LDdown n)), 1
5534 | _ -> None, 0
5536 let pwl l dir =
5537 begin match findpwl l.pageno dir with
5538 | Pwlnotfound -> ()
5539 | Pwl pageno ->
5540 let notfound dir =
5541 state.mode <- LinkNav (Ltgendir dir);
5542 let y, h = getpageyh pageno in
5543 let y =
5544 if dir < 0
5545 then y + h - state.winh
5546 else y
5548 gotoy y
5550 begin match getopaque pageno, getpage pageno with
5551 | Some opaque, Some _ ->
5552 let link =
5553 let ld = if dir > 0 then LDfirst else LDlast in
5554 findlink opaque ld
5556 begin match link with
5557 | Lfound m ->
5558 showlinktype (getlink opaque m);
5559 state.mode <- LinkNav (Ltexact (pageno, m));
5560 G.postRedisplay "linknav jpage";
5561 | _ -> notfound dir
5562 end;
5563 | _ -> notfound dir
5564 end;
5565 end;
5567 begin match opt with
5568 | Some Lnotfound -> pwl l dir;
5569 | Some (Lfound m) ->
5570 if m = n
5571 then pwl l dir
5572 else (
5573 let _, y0, _, y1 = getlinkrect opaque m in
5574 if y0 < l.pagey
5575 then gotopage1 l.pageno y0
5576 else (
5577 let d = fstate.fontsize + 1 in
5578 if y1 - l.pagey > l.pagevh - d
5579 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5580 else G.postRedisplay "linknav";
5582 showlinktype (getlink opaque m);
5583 state.mode <- LinkNav (Ltexact (l.pageno, m));
5586 | None -> viewkeyboard key mask
5587 end;
5588 | _ -> viewkeyboard key mask
5590 if key = 0xff63
5591 then (
5592 state.mode <- View;
5593 G.postRedisplay "leave linknav"
5595 else
5596 match linknav with
5597 | Ltgendir _ -> viewkeyboard key mask
5598 | Ltexact exact -> doexact exact
5601 let keyboard key mask =
5602 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5603 then wcmd "interrupt"
5604 else state.uioh <- state.uioh#key key mask
5607 let birdseyekeyboard key mask
5608 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5609 let incr =
5610 match conf.columns with
5611 | Csingle _ -> 1
5612 | Cmulti ((c, _, _), _) -> c
5613 | Csplit _ -> failwith "bird's eye split mode"
5615 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5616 match key with
5617 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5618 let y, h = getpageyh pageno in
5619 let top = (state.winh - h) / 2 in
5620 gotoy (max 0 (y - top))
5621 | 0xff0d (* enter *)
5622 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5623 | 0xff1b -> leavebirdseye beye true (* escape *)
5624 | 0xff52 -> upbirdseye incr beye (* up *)
5625 | 0xff54 -> downbirdseye incr beye (* down *)
5626 | 0xff51 -> upbirdseye 1 beye (* left *)
5627 | 0xff53 -> downbirdseye 1 beye (* right *)
5629 | 0xff55 -> (* prior *)
5630 begin match state.layout with
5631 | l :: _ ->
5632 if l.pagey != 0
5633 then (
5634 state.mode <- Birdseye (
5635 oconf, leftx, l.pageno, hooverpageno, anchor
5637 gotopage1 l.pageno 0;
5639 else (
5640 let layout = layout (state.y-state.winh) (pgh state.layout) in
5641 match layout with
5642 | [] -> gotoy (clamp (-state.winh))
5643 | l :: _ ->
5644 state.mode <- Birdseye (
5645 oconf, leftx, l.pageno, hooverpageno, anchor
5647 gotopage1 l.pageno 0
5650 | [] -> gotoy (clamp (-state.winh))
5651 end;
5653 | 0xff56 -> (* next *)
5654 begin match List.rev state.layout with
5655 | l :: _ ->
5656 let layout = layout (state.y + (pgh state.layout)) state.winh in
5657 begin match layout with
5658 | [] ->
5659 let incr = l.pageh - l.pagevh in
5660 if incr = 0
5661 then (
5662 state.mode <-
5663 Birdseye (
5664 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5666 G.postRedisplay "birdseye pagedown";
5668 else gotoy (clamp (incr + conf.interpagespace*2));
5670 | l :: _ ->
5671 state.mode <-
5672 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5673 gotopage1 l.pageno 0;
5676 | [] -> gotoy (clamp state.winh)
5677 end;
5679 | 0xff50 -> (* home *)
5680 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5681 gotopage1 0 0
5683 | 0xff57 -> (* end *)
5684 let pageno = state.pagecount - 1 in
5685 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5686 if not (pagevisible state.layout pageno)
5687 then
5688 let h =
5689 match List.rev state.pdims with
5690 | [] -> state.winh
5691 | (_, _, h, _) :: _ -> h
5693 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5694 else G.postRedisplay "birdseye end";
5695 | _ -> viewkeyboard key mask
5698 let drawpage l =
5699 let color =
5700 match state.mode with
5701 | Textentry _ -> scalecolor 0.4
5702 | LinkNav _
5703 | View -> scalecolor 1.0
5704 | Birdseye (_, _, pageno, hooverpageno, _) ->
5705 if l.pageno = hooverpageno
5706 then scalecolor 0.9
5707 else (
5708 if l.pageno = pageno
5709 then scalecolor 1.0
5710 else scalecolor 0.8
5713 drawtiles l color;
5716 let postdrawpage l linkindexbase =
5717 match getopaque l.pageno with
5718 | Some opaque ->
5719 if tileready l l.pagex l.pagey
5720 then
5721 let x = l.pagedispx - l.pagex
5722 and y = l.pagedispy - l.pagey in
5723 let hlmask =
5724 match conf.columns with
5725 | Csingle _ | Cmulti _ ->
5726 (if conf.hlinks then 1 else 0)
5727 + (if state.glinks
5728 && not (isbirdseye state.mode) then 2 else 0)
5729 | _ -> 0
5731 let s =
5732 match state.mode with
5733 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5734 | _ -> ""
5736 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5737 else 0
5738 | _ -> 0
5741 let scrollindicator () =
5742 let sbw, ph, sh = state.uioh#scrollph in
5743 let sbh, pw, sw = state.uioh#scrollpw in
5745 GlDraw.color (0.64, 0.64, 0.64);
5746 GlDraw.rect
5747 (float (state.winw - sbw), 0.)
5748 (float state.winw, float state.winh)
5750 GlDraw.rect
5751 (0., float (state.winh - sbh))
5752 (float (state.winw - state.scrollw - 1), float state.winh)
5754 GlDraw.color (0.0, 0.0, 0.0);
5756 GlDraw.rect
5757 (float (state.winw - sbw), ph)
5758 (float state.winw, ph +. sh)
5760 GlDraw.rect
5761 (pw, float (state.winh - sbh))
5762 (pw +. sw, float state.winh)
5766 let showsel () =
5767 match state.mstate with
5768 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5771 | Msel ((x0, y0), (x1, y1)) ->
5772 let rec loop = function
5773 | l :: ls ->
5774 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5775 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5776 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5777 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5778 then
5779 match getopaque l.pageno with
5780 | Some opaque ->
5781 let x0, y0 = pagetranslatepoint l x0 y0 in
5782 let x1, y1 = pagetranslatepoint l x1 y1 in
5783 seltext opaque (x0, y0, x1, y1);
5784 | _ -> ()
5785 else loop ls
5786 | [] -> ()
5788 loop state.layout
5791 let showrects rects =
5792 Gl.enable `blend;
5793 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5794 GlDraw.polygon_mode `both `fill;
5795 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5796 List.iter
5797 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5798 List.iter (fun l ->
5799 if l.pageno = pageno
5800 then (
5801 let dx = float (l.pagedispx - l.pagex) in
5802 let dy = float (l.pagedispy - l.pagey) in
5803 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5804 GlDraw.begins `quads;
5806 GlDraw.vertex2 (x0+.dx, y0+.dy);
5807 GlDraw.vertex2 (x1+.dx, y1+.dy);
5808 GlDraw.vertex2 (x2+.dx, y2+.dy);
5809 GlDraw.vertex2 (x3+.dx, y3+.dy);
5811 GlDraw.ends ();
5813 ) state.layout
5814 ) rects
5816 Gl.disable `blend;
5819 let display () =
5820 GlClear.color (scalecolor2 conf.bgcolor);
5821 GlClear.clear [`color];
5822 List.iter drawpage state.layout;
5823 let rects =
5824 match state.mode with
5825 | LinkNav (Ltexact (pageno, linkno)) ->
5826 begin match getopaque pageno with
5827 | Some opaque ->
5828 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5829 (pageno, 5, (
5830 float x0, float y0,
5831 float x1, float y0,
5832 float x1, float y1,
5833 float x0, float y1)
5834 ) :: state.rects
5835 | None -> state.rects
5837 | _ -> state.rects
5839 showrects rects;
5840 let rec postloop linkindexbase = function
5841 | l :: rest ->
5842 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5843 postloop linkindexbase rest
5844 | [] -> ()
5846 showsel ();
5847 postloop 0 state.layout;
5848 state.uioh#display;
5849 begin match state.mstate with
5850 | Mzoomrect ((x0, y0), (x1, y1)) ->
5851 Gl.enable `blend;
5852 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5853 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5854 GlDraw.rect (float x0, float y0)
5855 (float x1, float y1);
5856 Gl.disable `blend;
5857 | _ -> ()
5858 end;
5859 enttext ();
5860 scrollindicator ();
5861 Wsi.swapb ();
5864 let zoomrect x y x1 y1 =
5865 let x0 = min x x1
5866 and x1 = max x x1
5867 and y0 = min y y1 in
5868 gotoy (state.y + y0);
5869 state.anchor <- getanchor ();
5870 let zoom = (float state.w) /. float (x1 - x0) in
5871 let margin =
5872 match conf.fitmodel, conf.columns with
5873 | FitPage, Csplit _ ->
5874 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5876 | _, _ ->
5877 if state.w < state.winw - state.scrollw
5878 then (state.winw - state.scrollw - state.w) / 2
5879 else 0
5881 state.x <- (state.x + margin) - x0;
5882 setzoom zoom;
5883 Wsi.setcursor Wsi.CURSOR_INHERIT;
5884 state.mstate <- Mnone;
5887 let scrollx x =
5888 let winw = state.winw - state.scrollw - 1 in
5889 let s = float x /. float winw in
5890 let destx = truncate (float (state.w + winw) *. s) in
5891 state.x <- winw - destx;
5892 gotoy_and_clear_text state.y;
5893 state.mstate <- Mscrollx;
5896 let scrolly y =
5897 let s = float y /. float state.winh in
5898 let desty = truncate (float (state.maxy - state.winh) *. s) in
5899 gotoy_and_clear_text desty;
5900 state.mstate <- Mscrolly;
5903 let viewmouse button down x y mask =
5904 match button with
5905 | n when (n == 4 || n == 5) && not down ->
5906 if Wsi.withctrl mask
5907 then (
5908 match state.mstate with
5909 | Mzoom (oldn, i) ->
5910 if oldn = n
5911 then (
5912 if i = 2
5913 then
5914 let incr =
5915 match n with
5916 | 5 ->
5917 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5918 | _ ->
5919 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5921 let zoom = conf.zoom -. incr in
5922 setzoom zoom;
5923 state.mstate <- Mzoom (n, 0);
5924 else
5925 state.mstate <- Mzoom (n, i+1);
5927 else state.mstate <- Mzoom (n, 0)
5929 | _ -> state.mstate <- Mzoom (n, 0)
5931 else (
5932 match state.autoscroll with
5933 | Some step -> setautoscrollspeed step (n=4)
5934 | None ->
5935 if conf.wheelbypage || conf.presentation
5936 then (
5937 if n = 4
5938 then prevpage ()
5939 else nextpage ()
5941 else
5942 let incr =
5943 if n = 4
5944 then -conf.scrollstep
5945 else conf.scrollstep
5947 let incr = incr * 2 in
5948 let y = clamp incr in
5949 gotoy_and_clear_text y
5952 | n when (n = 6 || n = 7) && not down && canpan () ->
5953 state.x <-
5954 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5955 gotoy_and_clear_text state.y
5957 | 1 when Wsi.withshift mask ->
5958 state.mstate <- Mnone;
5959 if not down
5960 then (
5961 match unproject x y with
5962 | Some (pageno, ux, uy) ->
5963 let cmd = Printf.sprintf
5964 "%s %s %d %d %d"
5965 conf.stcmd state.path pageno ux uy
5967 popen cmd []
5968 | None -> ()
5971 | 1 when Wsi.withctrl mask ->
5972 if down
5973 then (
5974 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5975 state.mstate <- Mpan (x, y)
5977 else
5978 state.mstate <- Mnone
5980 | 3 ->
5981 if down
5982 then (
5983 Wsi.setcursor Wsi.CURSOR_CYCLE;
5984 let p = (x, y) in
5985 state.mstate <- Mzoomrect (p, p)
5987 else (
5988 match state.mstate with
5989 | Mzoomrect ((x0, y0), _) ->
5990 if abs (x-x0) > 10 && abs (y - y0) > 10
5991 then zoomrect x0 y0 x y
5992 else (
5993 state.mstate <- Mnone;
5994 Wsi.setcursor Wsi.CURSOR_INHERIT;
5995 G.postRedisplay "kill accidental zoom rect";
5997 | _ ->
5998 Wsi.setcursor Wsi.CURSOR_INHERIT;
5999 state.mstate <- Mnone
6002 | 1 when x > state.winw - state.scrollw ->
6003 if down
6004 then
6005 let _, position, sh = state.uioh#scrollph in
6006 if y > truncate position && y < truncate (position +. sh)
6007 then state.mstate <- Mscrolly
6008 else scrolly y
6009 else
6010 state.mstate <- Mnone
6012 | 1 when y > state.winh - state.hscrollh ->
6013 if down
6014 then
6015 let _, position, sw = state.uioh#scrollpw in
6016 if x > truncate position && x < truncate (position +. sw)
6017 then state.mstate <- Mscrollx
6018 else scrollx x
6019 else
6020 state.mstate <- Mnone
6022 | 1 ->
6023 let dest = if down then getunder x y else Unone in
6024 begin match dest with
6025 | Ulinkgoto _
6026 | Ulinkuri _
6027 | Uremote _
6028 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6029 gotounder dest
6031 | Unone when down ->
6032 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6033 state.mstate <- Mpan (x, y);
6035 | Unone | Utext _ ->
6036 if down
6037 then (
6038 if conf.angle mod 360 = 0
6039 then (
6040 state.mstate <- Msel ((x, y), (x, y));
6041 G.postRedisplay "mouse select";
6044 else (
6045 match state.mstate with
6046 | Mnone -> ()
6048 | Mzoom _ | Mscrollx | Mscrolly ->
6049 state.mstate <- Mnone
6051 | Mzoomrect ((x0, y0), _) ->
6052 zoomrect x0 y0 x y
6054 | Mpan _ ->
6055 Wsi.setcursor Wsi.CURSOR_INHERIT;
6056 state.mstate <- Mnone
6058 | Msel ((x0, y0), (x1, y1)) ->
6059 let rec loop = function
6060 | [] -> ()
6061 | l :: rest ->
6062 let inside =
6063 let a0 = l.pagedispy in
6064 let a1 = a0 + l.pagevh in
6065 let b0 = l.pagedispx in
6066 let b1 = b0 + l.pagevw in
6067 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6068 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6070 if inside
6071 then
6072 match getopaque l.pageno with
6073 | Some opaque ->
6074 begin
6075 match Ne.pipe () with
6076 | Ne.Exn exn ->
6077 showtext '!'
6078 (Printf.sprintf
6079 "can not create sel pipe: %s"
6080 (exntos exn));
6081 | Ne.Res (r, w) ->
6082 let doclose what fd =
6083 Ne.clo fd (fun msg ->
6084 dolog "%s close failed: %s" what msg)
6087 popen conf.selcmd [r, 0; w, -1];
6088 copysel w opaque;
6089 doclose "pipe/r" r;
6090 G.postRedisplay "copysel";
6091 with exn ->
6092 dolog "can not execute %S: %s"
6093 conf.selcmd (exntos exn);
6094 doclose "pipe/r" r;
6095 doclose "pipe/w" w;
6097 | None -> ()
6098 else loop rest
6100 loop state.layout;
6101 Wsi.setcursor Wsi.CURSOR_INHERIT;
6102 state.mstate <- Mnone;
6106 | _ -> ()
6109 let birdseyemouse button down x y mask
6110 (conf, leftx, _, hooverpageno, anchor) =
6111 match button with
6112 | 1 when down ->
6113 let rec loop = function
6114 | [] -> ()
6115 | l :: rest ->
6116 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6117 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6118 then (
6119 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6121 else loop rest
6123 loop state.layout
6124 | 3 -> ()
6125 | _ -> viewmouse button down x y mask
6128 let mouse button down x y mask =
6129 state.uioh <- state.uioh#button button down x y mask;
6132 let motion ~x ~y =
6133 state.uioh <- state.uioh#motion x y
6136 let pmotion ~x ~y =
6137 state.uioh <- state.uioh#pmotion x y;
6140 let uioh = object
6141 method display = ()
6143 method key key mask =
6144 begin match state.mode with
6145 | Textentry textentry -> textentrykeyboard key mask textentry
6146 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6147 | View -> viewkeyboard key mask
6148 | LinkNav linknav -> linknavkeyboard key mask linknav
6149 end;
6150 state.uioh
6152 method button button bstate x y mask =
6153 begin match state.mode with
6154 | LinkNav _
6155 | View -> viewmouse button bstate x y mask
6156 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6157 | Textentry _ -> ()
6158 end;
6159 state.uioh
6161 method motion x y =
6162 begin match state.mode with
6163 | Textentry _ -> ()
6164 | View | Birdseye _ | LinkNav _ ->
6165 match state.mstate with
6166 | Mzoom _ | Mnone -> ()
6168 | Mpan (x0, y0) ->
6169 let dx = x - x0
6170 and dy = y0 - y in
6171 state.mstate <- Mpan (x, y);
6172 if canpan ()
6173 then state.x <- panbound (state.x + dx);
6174 let y = clamp dy in
6175 gotoy_and_clear_text y
6177 | Msel (a, _) ->
6178 state.mstate <- Msel (a, (x, y));
6179 G.postRedisplay "motion select";
6181 | Mscrolly ->
6182 let y = min state.winh (max 0 y) in
6183 scrolly y
6185 | Mscrollx ->
6186 let x = min state.winw (max 0 x) in
6187 scrollx x
6189 | Mzoomrect (p0, _) ->
6190 state.mstate <- Mzoomrect (p0, (x, y));
6191 G.postRedisplay "motion zoomrect";
6192 end;
6193 state.uioh
6195 method pmotion x y =
6196 begin match state.mode with
6197 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6198 let rec loop = function
6199 | [] ->
6200 if hooverpageno != -1
6201 then (
6202 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6203 G.postRedisplay "pmotion birdseye no hoover";
6205 | l :: rest ->
6206 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6207 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6208 then (
6209 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6210 G.postRedisplay "pmotion birdseye hoover";
6212 else loop rest
6214 loop state.layout
6216 | Textentry _ -> ()
6218 | LinkNav _
6219 | View ->
6220 match state.mstate with
6221 | Mnone -> updateunder x y
6222 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6224 end;
6225 state.uioh
6227 method infochanged _ = ()
6229 method scrollph =
6230 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6231 let p, h =
6232 if maxy = 0
6233 then 0.0, float state.winh
6234 else scrollph state.y maxy
6236 state.scrollw, p, h
6238 method scrollpw =
6239 let winw = state.winw - state.scrollw in
6240 let fwinw = float winw in
6241 let sw =
6242 let sw = fwinw /. float state.w in
6243 let sw = fwinw *. sw in
6244 max sw (float conf.scrollh)
6246 let position =
6247 let maxx = state.w + winw in
6248 let x = winw - state.x in
6249 let percent = float x /. float maxx in
6250 (fwinw -. sw) *. percent
6252 state.hscrollh, position, sw
6254 method modehash =
6255 let modename =
6256 match state.mode with
6257 | LinkNav _ -> "links"
6258 | Textentry _ -> "textentry"
6259 | Birdseye _ -> "birdseye"
6260 | View -> "view"
6262 findkeyhash conf modename
6264 method eformsgs = true
6265 end;;
6267 module Config =
6268 struct
6269 open Parser
6271 let fontpath = ref "";;
6273 module KeyMap =
6274 Map.Make (struct type t = (int * int) let compare = compare end);;
6276 let unent s =
6277 let l = String.length s in
6278 let b = Buffer.create l in
6279 unent b s 0 l;
6280 Buffer.contents b;
6283 let home =
6284 try Sys.getenv "HOME"
6285 with exn ->
6286 prerr_endline
6287 ("Can not determine home directory location: " ^ exntos exn);
6291 let modifier_of_string = function
6292 | "alt" -> Wsi.altmask
6293 | "shift" -> Wsi.shiftmask
6294 | "ctrl" | "control" -> Wsi.ctrlmask
6295 | "meta" -> Wsi.metamask
6296 | _ -> 0
6299 let key_of_string =
6300 let r = Str.regexp "-" in
6301 fun s ->
6302 let elems = Str.full_split r s in
6303 let f n k m =
6304 let g s =
6305 let m1 = modifier_of_string s in
6306 if m1 = 0
6307 then (Wsi.namekey s, m)
6308 else (k, m lor m1)
6309 in function
6310 | Str.Delim s when n land 1 = 0 -> g s
6311 | Str.Text s -> g s
6312 | Str.Delim _ -> (k, m)
6314 let rec loop n k m = function
6315 | [] -> (k, m)
6316 | x :: xs ->
6317 let k, m = f n k m x in
6318 loop (n+1) k m xs
6320 loop 0 0 0 elems
6323 let keys_of_string =
6324 let r = Str.regexp "[ \t]" in
6325 fun s ->
6326 let elems = Str.split r s in
6327 List.map key_of_string elems
6330 let copykeyhashes c =
6331 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6334 let config_of c attrs =
6335 let apply c k v =
6337 match k with
6338 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6339 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6340 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6341 | "preload" -> { c with preload = bool_of_string v }
6342 | "page-bias" -> { c with pagebias = int_of_string v }
6343 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6344 | "horizontal-scroll-step" ->
6345 { c with hscrollstep = max (int_of_string v) 1 }
6346 | "auto-scroll-step" ->
6347 { c with autoscrollstep = max 0 (int_of_string v) }
6348 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6349 | "crop-hack" -> { c with crophack = bool_of_string v }
6350 | "throttle" ->
6351 let mw =
6352 match String.lowercase v with
6353 | "true" -> Some infinity
6354 | "false" -> None
6355 | f -> Some (float_of_string f)
6357 { c with maxwait = mw}
6358 | "highlight-links" -> { c with hlinks = bool_of_string v }
6359 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6360 | "vertical-margin" ->
6361 { c with interpagespace = max 0 (int_of_string v) }
6362 | "zoom" ->
6363 let zoom = float_of_string v /. 100. in
6364 let zoom = max zoom 0.0 in
6365 { c with zoom = zoom }
6366 | "presentation" -> { c with presentation = bool_of_string v }
6367 | "rotation-angle" -> { c with angle = int_of_string v }
6368 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6369 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6370 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6371 | "proportional-display" ->
6372 let fm =
6373 if bool_of_string v
6374 then FitProportional
6375 else FitWidth
6377 { c with fitmodel = fm }
6378 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6379 | "pixmap-cache-size" ->
6380 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6381 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6382 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6383 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6384 | "persistent-location" -> { c with jumpback = bool_of_string v }
6385 | "background-color" -> { c with bgcolor = color_of_string v }
6386 | "scrollbar-in-presentation" ->
6387 { c with scrollbarinpm = bool_of_string v }
6388 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6389 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6390 | "mupdf-store-size" ->
6391 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6392 | "checkers" -> { c with checkers = bool_of_string v }
6393 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6394 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6395 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6396 | "uri-launcher" -> { c with urilauncher = unent v }
6397 | "path-launcher" -> { c with pathlauncher = unent v }
6398 | "color-space" -> { c with colorspace = colorspace_of_string v }
6399 | "invert-colors" -> { c with invert = bool_of_string v }
6400 | "brightness" -> { c with colorscale = float_of_string v }
6401 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6402 | "ghyllscroll" ->
6403 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6404 | "columns" ->
6405 let (n, _, _) as nab = multicolumns_of_string v in
6406 if n < 0
6407 then { c with columns = Csplit (-n, [||]) }
6408 else { c with columns = Cmulti (nab, [||]) }
6409 | "birds-eye-columns" ->
6410 { c with beyecolumns = Some (max (int_of_string v) 2) }
6411 | "selection-command" -> { c with selcmd = unent v }
6412 | "synctex-command" -> { c with stcmd = unent v }
6413 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6414 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6415 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6416 | "use-pbo" -> { c with usepbo = bool_of_string v }
6417 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6418 | _ -> c
6419 with exn ->
6420 prerr_endline ("Error processing attribute (`" ^
6421 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6424 let rec fold c = function
6425 | [] -> c
6426 | (k, v) :: rest ->
6427 let c = apply c k v in
6428 fold c rest
6430 fold { c with keyhashes = copykeyhashes c } attrs;
6433 let fromstring f pos n v d =
6434 try f v
6435 with exn ->
6436 dolog "Error processing attribute (%S=%S) at %d\n%s"
6437 n v pos (exntos exn)
6442 let bookmark_of attrs =
6443 let rec fold title page rely visy = function
6444 | ("title", v) :: rest -> fold v page rely visy rest
6445 | ("page", v) :: rest -> fold title v rely visy rest
6446 | ("rely", v) :: rest -> fold title page v visy rest
6447 | ("visy", v) :: rest -> fold title page rely v rest
6448 | _ :: rest -> fold title page rely visy rest
6449 | [] -> title, page, rely, visy
6451 fold "invalid" "0" "0" "0" attrs
6454 let doc_of attrs =
6455 let rec fold path page rely pan visy = function
6456 | ("path", v) :: rest -> fold v page rely pan visy rest
6457 | ("page", v) :: rest -> fold path v rely pan visy rest
6458 | ("rely", v) :: rest -> fold path page v pan visy rest
6459 | ("pan", v) :: rest -> fold path page rely v visy rest
6460 | ("visy", v) :: rest -> fold path page rely pan v rest
6461 | _ :: rest -> fold path page rely pan visy rest
6462 | [] -> path, page, rely, pan, visy
6464 fold "" "0" "0" "0" "0" attrs
6467 let map_of attrs =
6468 let rec fold rs ls = function
6469 | ("out", v) :: rest -> fold v ls rest
6470 | ("in", v) :: rest -> fold rs v rest
6471 | _ :: rest -> fold ls rs rest
6472 | [] -> ls, rs
6474 fold "" "" attrs
6477 let setconf dst src =
6478 dst.scrollbw <- src.scrollbw;
6479 dst.scrollh <- src.scrollh;
6480 dst.icase <- src.icase;
6481 dst.preload <- src.preload;
6482 dst.pagebias <- src.pagebias;
6483 dst.verbose <- src.verbose;
6484 dst.scrollstep <- src.scrollstep;
6485 dst.maxhfit <- src.maxhfit;
6486 dst.crophack <- src.crophack;
6487 dst.autoscrollstep <- src.autoscrollstep;
6488 dst.maxwait <- src.maxwait;
6489 dst.hlinks <- src.hlinks;
6490 dst.underinfo <- src.underinfo;
6491 dst.interpagespace <- src.interpagespace;
6492 dst.zoom <- src.zoom;
6493 dst.presentation <- src.presentation;
6494 dst.angle <- src.angle;
6495 dst.cwinw <- src.cwinw;
6496 dst.cwinh <- src.cwinh;
6497 dst.savebmarks <- src.savebmarks;
6498 dst.memlimit <- src.memlimit;
6499 dst.fitmodel <- src.fitmodel;
6500 dst.texcount <- src.texcount;
6501 dst.sliceheight <- src.sliceheight;
6502 dst.thumbw <- src.thumbw;
6503 dst.jumpback <- src.jumpback;
6504 dst.bgcolor <- src.bgcolor;
6505 dst.scrollbarinpm <- src.scrollbarinpm;
6506 dst.tilew <- src.tilew;
6507 dst.tileh <- src.tileh;
6508 dst.mustoresize <- src.mustoresize;
6509 dst.checkers <- src.checkers;
6510 dst.aalevel <- src.aalevel;
6511 dst.trimmargins <- src.trimmargins;
6512 dst.trimfuzz <- src.trimfuzz;
6513 dst.urilauncher <- src.urilauncher;
6514 dst.colorspace <- src.colorspace;
6515 dst.invert <- src.invert;
6516 dst.colorscale <- src.colorscale;
6517 dst.redirectstderr <- src.redirectstderr;
6518 dst.ghyllscroll <- src.ghyllscroll;
6519 dst.columns <- src.columns;
6520 dst.beyecolumns <- src.beyecolumns;
6521 dst.selcmd <- src.selcmd;
6522 dst.updatecurs <- src.updatecurs;
6523 dst.pathlauncher <- src.pathlauncher;
6524 dst.keyhashes <- copykeyhashes src;
6525 dst.hfsize <- src.hfsize;
6526 dst.hscrollstep <- src.hscrollstep;
6527 dst.pgscale <- src.pgscale;
6528 dst.usepbo <- src.usepbo;
6529 dst.wheelbypage <- src.wheelbypage;
6530 dst.stcmd <- src.stcmd;
6533 let get s =
6534 let h = Hashtbl.create 10 in
6535 let dc = { defconf with angle = defconf.angle } in
6536 let rec toplevel v t spos _ =
6537 match t with
6538 | Vdata | Vcdata | Vend -> v
6539 | Vopen ("llppconfig", _, closed) ->
6540 if closed
6541 then v
6542 else { v with f = llppconfig }
6543 | Vopen _ ->
6544 error "unexpected subelement at top level" s spos
6545 | Vclose _ -> error "unexpected close at top level" s spos
6547 and llppconfig v t spos _ =
6548 match t with
6549 | Vdata | Vcdata -> v
6550 | Vend -> error "unexpected end of input in llppconfig" s spos
6551 | Vopen ("defaults", attrs, closed) ->
6552 let c = config_of dc attrs in
6553 setconf dc c;
6554 if closed
6555 then v
6556 else { v with f = defaults }
6558 | Vopen ("ui-font", attrs, closed) ->
6559 let rec getsize size = function
6560 | [] -> size
6561 | ("size", v) :: rest ->
6562 let size =
6563 fromstring int_of_string spos "size" v fstate.fontsize in
6564 getsize size rest
6565 | l -> getsize size l
6567 fstate.fontsize <- getsize fstate.fontsize attrs;
6568 if closed
6569 then v
6570 else { v with f = uifont (Buffer.create 10) }
6572 | Vopen ("doc", attrs, closed) ->
6573 let pathent, spage, srely, span, svisy = doc_of attrs in
6574 let path = unent pathent
6575 and pageno = fromstring int_of_string spos "page" spage 0
6576 and rely = fromstring float_of_string spos "rely" srely 0.0
6577 and pan = fromstring int_of_string spos "pan" span 0
6578 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6579 let c = config_of dc attrs in
6580 let anchor = (pageno, rely, visy) in
6581 if closed
6582 then (Hashtbl.add h path (c, [], pan, anchor); v)
6583 else { v with f = doc path pan anchor c [] }
6585 | Vopen _ ->
6586 error "unexpected subelement in llppconfig" s spos
6588 | Vclose "llppconfig" -> { v with f = toplevel }
6589 | Vclose _ -> error "unexpected close in llppconfig" s spos
6591 and defaults v t spos _ =
6592 match t with
6593 | Vdata | Vcdata -> v
6594 | Vend -> error "unexpected end of input in defaults" s spos
6595 | Vopen ("keymap", attrs, closed) ->
6596 let modename =
6597 try List.assoc "mode" attrs
6598 with Not_found -> "global" in
6599 if closed
6600 then v
6601 else
6602 let ret keymap =
6603 let h = findkeyhash dc modename in
6604 KeyMap.iter (Hashtbl.replace h) keymap;
6605 defaults
6607 { v with f = pkeymap ret KeyMap.empty }
6609 | Vopen (_, _, _) ->
6610 error "unexpected subelement in defaults" s spos
6612 | Vclose "defaults" ->
6613 { v with f = llppconfig }
6615 | Vclose _ -> error "unexpected close in defaults" s spos
6617 and uifont b v t spos epos =
6618 match t with
6619 | Vdata | Vcdata ->
6620 Buffer.add_substring b s spos (epos - spos);
6622 | Vopen (_, _, _) ->
6623 error "unexpected subelement in ui-font" s spos
6624 | Vclose "ui-font" ->
6625 if String.length !fontpath = 0
6626 then fontpath := Buffer.contents b;
6627 { v with f = llppconfig }
6628 | Vclose _ -> error "unexpected close in ui-font" s spos
6629 | Vend -> error "unexpected end of input in ui-font" s spos
6631 and doc path pan anchor c bookmarks v t spos _ =
6632 match t with
6633 | Vdata | Vcdata -> v
6634 | Vend -> error "unexpected end of input in doc" s spos
6635 | Vopen ("bookmarks", _, closed) ->
6636 if closed
6637 then v
6638 else { v with f = pbookmarks path pan anchor c bookmarks }
6640 | Vopen ("keymap", attrs, closed) ->
6641 let modename =
6642 try List.assoc "mode" attrs
6643 with Not_found -> "global"
6645 if closed
6646 then v
6647 else
6648 let ret keymap =
6649 let h = findkeyhash c modename in
6650 KeyMap.iter (Hashtbl.replace h) keymap;
6651 doc path pan anchor c bookmarks
6653 { v with f = pkeymap ret KeyMap.empty }
6655 | Vopen (_, _, _) ->
6656 error "unexpected subelement in doc" s spos
6658 | Vclose "doc" ->
6659 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6660 { v with f = llppconfig }
6662 | Vclose _ -> error "unexpected close in doc" s spos
6664 and pkeymap ret keymap v t spos _ =
6665 match t with
6666 | Vdata | Vcdata -> v
6667 | Vend -> error "unexpected end of input in keymap" s spos
6668 | Vopen ("map", attrs, closed) ->
6669 let r, l = map_of attrs in
6670 let kss = fromstring keys_of_string spos "in" r [] in
6671 let lss = fromstring keys_of_string spos "out" l [] in
6672 let keymap =
6673 match kss with
6674 | [] -> keymap
6675 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6676 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6678 if closed
6679 then { v with f = pkeymap ret keymap }
6680 else
6681 let f () = v in
6682 { v with f = skip "map" f }
6684 | Vopen _ ->
6685 error "unexpected subelement in keymap" s spos
6687 | Vclose "keymap" ->
6688 { v with f = ret keymap }
6690 | Vclose _ -> error "unexpected close in keymap" s spos
6692 and pbookmarks path pan anchor c bookmarks v t spos _ =
6693 match t with
6694 | Vdata | Vcdata -> v
6695 | Vend -> error "unexpected end of input in bookmarks" s spos
6696 | Vopen ("item", attrs, closed) ->
6697 let titleent, spage, srely, svisy = bookmark_of attrs in
6698 let page = fromstring int_of_string spos "page" spage 0
6699 and rely = fromstring float_of_string spos "rely" srely 0.0
6700 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6701 let bookmarks =
6702 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6704 if closed
6705 then { v with f = pbookmarks path pan anchor c bookmarks }
6706 else
6707 let f () = v in
6708 { v with f = skip "item" f }
6710 | Vopen _ ->
6711 error "unexpected subelement in bookmarks" s spos
6713 | Vclose "bookmarks" ->
6714 { v with f = doc path pan anchor c bookmarks }
6716 | Vclose _ -> error "unexpected close in bookmarks" s spos
6718 and skip tag f v t spos _ =
6719 match t with
6720 | Vdata | Vcdata -> v
6721 | Vend ->
6722 error ("unexpected end of input in skipped " ^ tag) s spos
6723 | Vopen (tag', _, closed) ->
6724 if closed
6725 then v
6726 else
6727 let f' () = { v with f = skip tag f } in
6728 { v with f = skip tag' f' }
6729 | Vclose ctag ->
6730 if tag = ctag
6731 then f ()
6732 else error ("unexpected close in skipped " ^ tag) s spos
6735 parse { f = toplevel; accu = () } s;
6736 h, dc;
6739 let do_load f ic =
6741 let len = in_channel_length ic in
6742 let s = String.create len in
6743 really_input ic s 0 len;
6744 f s;
6745 with
6746 | Parse_error (msg, s, pos) ->
6747 let subs = subs s pos in
6748 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6749 failwith ("parse error: " ^ s)
6751 | exn ->
6752 failwith ("config load error: " ^ exntos exn)
6755 let defconfpath =
6756 let dir =
6758 let dir = Filename.concat home ".config" in
6759 if Sys.is_directory dir then dir else home
6760 with _ -> home
6762 Filename.concat dir "llpp.conf"
6765 let confpath = ref defconfpath;;
6767 let load1 f =
6768 if Sys.file_exists !confpath
6769 then
6770 match
6771 (try Some (open_in_bin !confpath)
6772 with exn ->
6773 prerr_endline
6774 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6775 exntos exn);
6776 None
6778 with
6779 | Some ic ->
6780 let success =
6782 f (do_load get ic)
6783 with exn ->
6784 prerr_endline
6785 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6786 exntos exn);
6787 false
6789 close_in ic;
6790 success
6792 | None -> false
6793 else
6794 f (Hashtbl.create 0, defconf)
6797 let load () =
6798 let f (h, dc) =
6799 let pc, pb, px, pa =
6801 let key =
6802 if String.length state.origin = 0
6803 then state.path
6804 else state.origin
6806 Hashtbl.find h (Filename.basename key)
6807 with Not_found -> dc, [], 0, emptyanchor
6809 setconf defconf dc;
6810 setconf conf pc;
6811 state.bookmarks <- pb;
6812 state.x <- px;
6813 state.scrollw <- conf.scrollbw;
6814 if conf.jumpback
6815 then state.anchor <- pa;
6816 cbput state.hists.nav pa;
6817 true
6819 load1 f
6822 let add_attrs bb always dc c =
6823 let ob s a b =
6824 if always || a != b
6825 then Printf.bprintf bb "\n %s='%b'" s a
6826 and oi s a b =
6827 if always || a != b
6828 then Printf.bprintf bb "\n %s='%d'" s a
6829 and oI s a b =
6830 if always || a != b
6831 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6832 and oz s a b =
6833 if always || a <> b
6834 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6835 and oF s a b =
6836 if always || a <> b
6837 then Printf.bprintf bb "\n %s='%f'" s a
6838 and oc s a b =
6839 if always || a <> b
6840 then
6841 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6842 and oC s a b =
6843 if always || a <> b
6844 then
6845 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6846 and oR s a b =
6847 if always || a <> b
6848 then
6849 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6850 and os s a b =
6851 if always || a <> b
6852 then
6853 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6854 and og s a b =
6855 if always || a <> b
6856 then
6857 match a with
6858 | None -> ()
6859 | Some (_N, _A, _B) ->
6860 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6861 and oW s a b =
6862 if always || a <> b
6863 then
6864 let v =
6865 match a with
6866 | None -> "false"
6867 | Some f ->
6868 if f = infinity
6869 then "true"
6870 else string_of_float f
6872 Printf.bprintf bb "\n %s='%s'" s v
6873 and oco s a b =
6874 if always || a <> b
6875 then
6876 match a with
6877 | Cmulti ((n, a, b), _) when n > 1 ->
6878 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6879 | Csplit (n, _) when n > 1 ->
6880 Printf.bprintf bb "\n %s='%d'" s ~-n
6881 | _ -> ()
6882 and obeco s a b =
6883 if always || a <> b
6884 then
6885 match a with
6886 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6887 | _ -> ()
6888 and oFm s a b =
6889 if always || a <> b
6890 then
6891 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6893 oi "width" c.cwinw dc.cwinw;
6894 oi "height" c.cwinh dc.cwinh;
6895 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6896 oi "scroll-handle-height" c.scrollh dc.scrollh;
6897 ob "case-insensitive-search" c.icase dc.icase;
6898 ob "preload" c.preload dc.preload;
6899 oi "page-bias" c.pagebias dc.pagebias;
6900 oi "scroll-step" c.scrollstep dc.scrollstep;
6901 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6902 ob "max-height-fit" c.maxhfit dc.maxhfit;
6903 ob "crop-hack" c.crophack dc.crophack;
6904 oW "throttle" c.maxwait dc.maxwait;
6905 ob "highlight-links" c.hlinks dc.hlinks;
6906 ob "under-cursor-info" c.underinfo dc.underinfo;
6907 oi "vertical-margin" c.interpagespace dc.interpagespace;
6908 oz "zoom" c.zoom dc.zoom;
6909 ob "presentation" c.presentation dc.presentation;
6910 oi "rotation-angle" c.angle dc.angle;
6911 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6912 oFm "fit-model" c.fitmodel dc.fitmodel;
6913 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6914 oi "tex-count" c.texcount dc.texcount;
6915 oi "slice-height" c.sliceheight dc.sliceheight;
6916 oi "thumbnail-width" c.thumbw dc.thumbw;
6917 ob "persistent-location" c.jumpback dc.jumpback;
6918 oc "background-color" c.bgcolor dc.bgcolor;
6919 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6920 oi "tile-width" c.tilew dc.tilew;
6921 oi "tile-height" c.tileh dc.tileh;
6922 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6923 ob "checkers" c.checkers dc.checkers;
6924 oi "aalevel" c.aalevel dc.aalevel;
6925 ob "trim-margins" c.trimmargins dc.trimmargins;
6926 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6927 os "uri-launcher" c.urilauncher dc.urilauncher;
6928 os "path-launcher" c.pathlauncher dc.pathlauncher;
6929 oC "color-space" c.colorspace dc.colorspace;
6930 ob "invert-colors" c.invert dc.invert;
6931 oF "brightness" c.colorscale dc.colorscale;
6932 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6933 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6934 oco "columns" c.columns dc.columns;
6935 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6936 os "selection-command" c.selcmd dc.selcmd;
6937 os "synctex-command" c.stcmd dc.stcmd;
6938 ob "update-cursor" c.updatecurs dc.updatecurs;
6939 oi "hint-font-size" c.hfsize dc.hfsize;
6940 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6941 oF "page-scroll-scale" c.pgscale dc.pgscale;
6942 ob "use-pbo" c.usepbo dc.usepbo;
6943 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6946 let keymapsbuf always dc c =
6947 let bb = Buffer.create 16 in
6948 let rec loop = function
6949 | [] -> ()
6950 | (modename, h) :: rest ->
6951 let dh = findkeyhash dc modename in
6952 if always || h <> dh
6953 then (
6954 if Hashtbl.length h > 0
6955 then (
6956 if Buffer.length bb > 0
6957 then Buffer.add_char bb '\n';
6958 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6959 Hashtbl.iter (fun i o ->
6960 let isdifferent = always ||
6962 let dO = Hashtbl.find dh i in
6963 dO <> o
6964 with Not_found -> true
6966 if isdifferent
6967 then
6968 let addkm (k, m) =
6969 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6970 if Wsi.withalt m then Buffer.add_string bb "alt-";
6971 if Wsi.withshift m then Buffer.add_string bb "shift-";
6972 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6973 Buffer.add_string bb (Wsi.keyname k);
6975 let addkms l =
6976 let rec loop = function
6977 | [] -> ()
6978 | km :: [] -> addkm km
6979 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6981 loop l
6983 Buffer.add_string bb "<map in='";
6984 addkm i;
6985 match o with
6986 | KMinsrt km ->
6987 Buffer.add_string bb "' out='";
6988 addkm km;
6989 Buffer.add_string bb "'/>\n"
6991 | KMinsrl kms ->
6992 Buffer.add_string bb "' out='";
6993 addkms kms;
6994 Buffer.add_string bb "'/>\n"
6996 | KMmulti (ins, kms) ->
6997 Buffer.add_char bb ' ';
6998 addkms ins;
6999 Buffer.add_string bb "' out='";
7000 addkms kms;
7001 Buffer.add_string bb "'/>\n"
7002 ) h;
7003 Buffer.add_string bb "</keymap>";
7006 loop rest
7008 loop c.keyhashes;
7012 let save () =
7013 let uifontsize = fstate.fontsize in
7014 let bb = Buffer.create 32768 in
7015 let relx = float state.x /. float state.winw in
7016 let w, h, x =
7017 let cx w = truncate (relx *. float w) in
7018 List.fold_left
7019 (fun (w, h, x) ws ->
7020 match ws with
7021 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7022 | Wsi.MaxVert -> (w, conf.cwinh, x)
7023 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7025 (state.winw, state.winh, state.x) state.winstate
7027 conf.cwinw <- w;
7028 conf.cwinh <- h;
7029 let f (h, dc) =
7030 let dc = if conf.bedefault then conf else dc in
7031 Buffer.add_string bb "<llppconfig>\n";
7033 if String.length !fontpath > 0
7034 then
7035 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7036 uifontsize
7037 !fontpath
7038 else (
7039 if uifontsize <> 14
7040 then
7041 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7044 Buffer.add_string bb "<defaults ";
7045 add_attrs bb true dc dc;
7046 let kb = keymapsbuf true dc dc in
7047 if Buffer.length kb > 0
7048 then (
7049 Buffer.add_string bb ">\n";
7050 Buffer.add_buffer bb kb;
7051 Buffer.add_string bb "\n</defaults>\n";
7053 else Buffer.add_string bb "/>\n";
7055 let adddoc path pan anchor c bookmarks =
7056 if bookmarks == [] && c = dc && anchor = emptyanchor
7057 then ()
7058 else (
7059 Printf.bprintf bb "<doc path='%s'"
7060 (enent path 0 (String.length path));
7062 if anchor <> emptyanchor
7063 then (
7064 let n, rely, visy = anchor in
7065 Printf.bprintf bb " page='%d'" n;
7066 if rely > 1e-6
7067 then
7068 Printf.bprintf bb " rely='%f'" rely
7070 if abs_float visy > 1e-6
7071 then
7072 Printf.bprintf bb " visy='%f'" visy
7076 if pan != 0
7077 then Printf.bprintf bb " pan='%d'" pan;
7079 add_attrs bb false dc c;
7080 let kb = keymapsbuf false dc c in
7082 begin match bookmarks with
7083 | [] ->
7084 if Buffer.length kb > 0
7085 then (
7086 Buffer.add_string bb ">\n";
7087 Buffer.add_buffer bb kb;
7088 Buffer.add_string bb "\n</doc>\n";
7090 else Buffer.add_string bb "/>\n"
7091 | _ ->
7092 Buffer.add_string bb ">\n<bookmarks>\n";
7093 List.iter (fun (title, _level, (page, rely, visy)) ->
7094 Printf.bprintf bb
7095 "<item title='%s' page='%d'"
7096 (enent title 0 (String.length title))
7097 page
7099 if rely > 1e-6
7100 then
7101 Printf.bprintf bb " rely='%f'" rely
7103 if abs_float visy > 1e-6
7104 then
7105 Printf.bprintf bb " visy='%f'" visy
7107 Buffer.add_string bb "/>\n";
7108 ) bookmarks;
7109 Buffer.add_string bb "</bookmarks>";
7110 if Buffer.length kb > 0
7111 then (
7112 Buffer.add_string bb "\n";
7113 Buffer.add_buffer bb kb;
7115 Buffer.add_string bb "\n</doc>\n";
7116 end;
7120 let pan, conf =
7121 match state.mode with
7122 | Birdseye (c, pan, _, _, _) ->
7123 let beyecolumns =
7124 match conf.columns with
7125 | Cmulti ((c, _, _), _) -> Some c
7126 | Csingle _ -> None
7127 | Csplit _ -> None
7128 and columns =
7129 match c.columns with
7130 | Cmulti (c, _) -> Cmulti (c, [||])
7131 | Csingle _ -> Csingle [||]
7132 | Csplit _ -> failwith "quit from bird's eye while split"
7134 pan, { c with beyecolumns = beyecolumns; columns = columns }
7135 | _ -> x, conf
7137 let basename = Filename.basename
7138 (if String.length state.origin = 0 then state.path else state.origin)
7140 adddoc basename pan (getanchor ())
7141 (let conf =
7142 let autoscrollstep =
7143 match state.autoscroll with
7144 | Some step -> step
7145 | None -> conf.autoscrollstep
7147 match state.mode with
7148 | Birdseye (bc, _, _, _, _) ->
7149 { conf with
7150 zoom = bc.zoom;
7151 presentation = bc.presentation;
7152 interpagespace = bc.interpagespace;
7153 maxwait = bc.maxwait;
7154 autoscrollstep = autoscrollstep }
7155 | _ -> { conf with autoscrollstep = autoscrollstep }
7156 in conf)
7157 (if conf.savebmarks then state.bookmarks else []);
7159 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7160 if basename <> path
7161 then adddoc path x anchor c bookmarks
7162 ) h;
7163 Buffer.add_string bb "</llppconfig>\n";
7164 true;
7166 if load1 f && Buffer.length bb > 0
7167 then
7169 let tmp = !confpath ^ ".tmp" in
7170 let oc = open_out_bin tmp in
7171 Buffer.output_buffer oc bb;
7172 close_out oc;
7173 Unix.rename tmp !confpath;
7174 with exn ->
7175 prerr_endline
7176 ("error while saving configuration: " ^ exntos exn)
7178 end;;
7180 let adderrmsg src msg =
7181 Buffer.add_string state.errmsgs msg;
7182 state.newerrmsgs <- true;
7183 G.postRedisplay src
7186 let adderrfmt src fmt =
7187 Format.kprintf (fun s -> adderrmsg src s) fmt;
7190 let ract cmds =
7191 let cl = splitatspace cmds in
7192 let scan s fmt f =
7193 try Scanf.sscanf s fmt f
7194 with exn ->
7195 adderrfmt "remote exec"
7196 "error processing '%S': %s\n" cmds (exntos exn)
7198 match cl with
7199 | "reload" :: [] -> reload ()
7200 | "goto" :: args :: [] ->
7201 scan args "%u %f %f"
7202 (fun pageno x y ->
7203 let cmd, _ = state.geomcmds in
7204 if String.length cmd = 0
7205 then gotopagexy pageno x y
7206 else
7207 let f prevf () =
7208 gotopagexy pageno x y;
7209 prevf ()
7211 state.reprf <- f state.reprf
7213 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7214 | "rect" :: args :: [] ->
7215 scan args "%u %u %f %f %f %f"
7216 (fun pageno color x0 y0 x1 y1 ->
7217 onpagerect pageno (fun w h ->
7218 let _,w1,h1,_ = getpagedim pageno in
7219 let sw = float w1 /. w
7220 and sh = float h1 /. h in
7221 let x0s = x0 *. sw
7222 and x1s = x1 *. sw
7223 and y0s = y0 *. sh
7224 and y1s = y1 *. sh in
7225 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7226 debugrect rect;
7227 state.rects <- (pageno, color, rect) :: state.rects;
7228 G.postRedisplay "rect";
7231 | "activatewin" :: [] -> Wsi.activatewin ()
7232 | "quit" :: [] -> raise Quit
7233 | _ ->
7234 adderrfmt "remote command"
7235 "error processing remote command: %S\n" cmds;
7238 let remote =
7239 let scratch = String.create 80 in
7240 let buf = Buffer.create 80 in
7241 fun fd ->
7242 let rec tempfr () =
7243 try Some (Unix.read fd scratch 0 80)
7244 with
7245 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7246 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7247 | exn -> raise exn
7249 match tempfr () with
7250 | None -> Some fd
7251 | Some n ->
7252 if n = 0
7253 then (
7254 Unix.close fd;
7255 if Buffer.length buf > 0
7256 then (
7257 let s = Buffer.contents buf in
7258 Buffer.clear buf;
7259 ract s;
7261 None
7263 else
7264 let rec eat ppos =
7265 let nlpos =
7267 let pos = String.index_from scratch ppos '\n' in
7268 if pos >= n then -1 else pos
7269 with Not_found -> -1
7271 if nlpos >= 0
7272 then (
7273 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7274 let s = Buffer.contents buf in
7275 Buffer.clear buf;
7276 ract s;
7277 eat (nlpos+1);
7279 else (
7280 Buffer.add_substring buf scratch ppos (n-ppos);
7281 Some fd
7283 in eat 0
7286 let remoteopen path =
7287 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7288 with exn ->
7289 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7290 None
7293 let () =
7294 let trimcachepath = ref "" in
7295 let rcmdpath = ref "" in
7296 Arg.parse
7297 (Arg.align
7298 [("-p", Arg.String (fun s -> state.password <- s),
7299 "<password> Set password");
7301 ("-f", Arg.String (fun s -> Config.fontpath := s),
7302 "<path> Set path to the user interface font");
7304 ("-c", Arg.String (fun s -> Config.confpath := s),
7305 "<path> Set path to the configuration file");
7307 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7308 "<path> Set path to the trim cache file");
7310 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7311 "<named-destination> Set named destination");
7313 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7315 ("-remote", Arg.String (fun s -> rcmdpath := s),
7316 "<path> Set path to the remote commands source");
7318 ("-origin", Arg.String (fun s -> state.origin <- s),
7319 "<original-path> Set original path");
7321 ("-v", Arg.Unit (fun () ->
7322 Printf.printf
7323 "%s\nconfiguration path: %s\n"
7324 (version ())
7325 Config.defconfpath
7327 exit 0), " Print version and exit");
7330 (fun s -> state.path <- s)
7331 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7333 if String.length state.path = 0
7334 then (prerr_endline "file name missing"; exit 1);
7336 if not (Config.load ())
7337 then prerr_endline "failed to load configuration";
7339 let globalkeyhash = findkeyhash conf "global" in
7340 let wsfd, winw, winh = Wsi.init (object
7341 method expose =
7342 if nogeomcmds state.geomcmds || platform == Posx
7343 then display ()
7344 else (
7345 GlClear.color (scalecolor2 conf.bgcolor);
7346 GlClear.clear [`color];
7348 method display = display ()
7349 method reshape w h = reshape w h
7350 method mouse b d x y m = mouse b d x y m
7351 method motion x y = state.mpos <- (x, y); motion x y
7352 method pmotion x y = state.mpos <- (x, y); pmotion x y
7353 method key k m =
7354 let mascm = m land (
7355 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7356 ) in
7357 match state.keystate with
7358 | KSnone ->
7359 let km = k, mascm in
7360 begin
7361 match
7362 let modehash = state.uioh#modehash in
7363 try Hashtbl.find modehash km
7364 with Not_found ->
7365 try Hashtbl.find globalkeyhash km
7366 with Not_found -> KMinsrt (k, m)
7367 with
7368 | KMinsrt (k, m) -> keyboard k m
7369 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7370 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7372 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7373 List.iter (fun (k, m) -> keyboard k m) insrt;
7374 state.keystate <- KSnone
7375 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7376 state.keystate <- KSinto (keys, insrt)
7377 | _ ->
7378 state.keystate <- KSnone
7380 method enter x y = state.mpos <- (x, y); pmotion x y
7381 method leave = state.mpos <- (-1, -1)
7382 method winstate wsl = state.winstate <- wsl
7383 method quit = raise Quit
7384 end) conf.cwinw conf.cwinh (platform = Posx) in
7386 state.wsfd <- wsfd;
7388 if not (
7389 List.exists GlMisc.check_extension
7390 [ "GL_ARB_texture_rectangle"
7391 ; "GL_EXT_texture_recangle"
7392 ; "GL_NV_texture_rectangle" ]
7394 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7396 let cr, sw =
7397 match Ne.pipe () with
7398 | Ne.Exn exn ->
7399 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7400 exit 1
7401 | Ne.Res rw -> rw
7402 and sr, cw =
7403 match Ne.pipe () with
7404 | Ne.Exn exn ->
7405 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7406 exit 1
7407 | Ne.Res rw -> rw
7410 cloexec cr;
7411 cloexec sw;
7412 cloexec sr;
7413 cloexec cw;
7415 setcheckers conf.checkers;
7416 redirectstderr ();
7418 init (cr, cw) (
7419 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7420 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7421 !Config.fontpath, !trimcachepath,
7422 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7424 state.sr <- sr;
7425 state.sw <- sw;
7426 state.text <- "Opening " ^ (mbtoutf8 state.path);
7427 reshape winw winh;
7428 opendoc state.path state.password;
7429 state.uioh <- uioh;
7431 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7432 let optrfd =
7433 ref (
7434 if String.length !rcmdpath > 0
7435 then remoteopen !rcmdpath
7436 else None
7440 let rec loop deadline =
7441 let r =
7442 match state.errfd with
7443 | None -> [state.sr; state.wsfd]
7444 | Some fd -> [state.sr; state.wsfd; fd]
7446 let r =
7447 match !optrfd with
7448 | None -> r
7449 | Some fd -> fd :: r
7451 if state.redisplay
7452 then (
7453 state.redisplay <- false;
7454 display ();
7456 let timeout =
7457 let now = now () in
7458 if deadline > now
7459 then (
7460 if deadline = infinity
7461 then ~-.1.0
7462 else max 0.0 (deadline -. now)
7464 else 0.0
7466 let r, _, _ =
7467 try Unix.select r [] [] timeout
7468 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7470 begin match r with
7471 | [] ->
7472 state.ghyll None;
7473 let newdeadline =
7474 if state.ghyll == noghyll
7475 then
7476 match state.autoscroll with
7477 | Some step when step != 0 ->
7478 let y = state.y + step in
7479 let y =
7480 if y < 0
7481 then state.maxy
7482 else if y >= state.maxy then 0 else y
7484 gotoy y;
7485 if state.mode = View
7486 then state.text <- "";
7487 deadline +. 0.01
7488 | _ -> infinity
7489 else deadline +. 0.01
7491 loop newdeadline
7493 | l ->
7494 let rec checkfds = function
7495 | [] -> ()
7496 | fd :: rest when fd = state.sr ->
7497 let cmd = readcmd state.sr in
7498 act cmd;
7499 checkfds rest
7501 | fd :: rest when fd = state.wsfd ->
7502 Wsi.readresp fd;
7503 checkfds rest
7505 | fd :: rest when Some fd = !optrfd ->
7506 begin match remote fd with
7507 | None -> optrfd := remoteopen !rcmdpath;
7508 | opt -> optrfd := opt
7509 end;
7510 checkfds rest
7512 | fd :: rest ->
7513 let s = String.create 80 in
7514 let n = tempfailureretry (Unix.read fd s 0) 80 in
7515 if conf.redirectstderr
7516 then (
7517 Buffer.add_substring state.errmsgs s 0 n;
7518 state.newerrmsgs <- true;
7519 state.redisplay <- true;
7521 else (
7522 prerr_string (String.sub s 0 n);
7523 flush stderr;
7525 checkfds rest
7527 checkfds l;
7528 let newdeadline =
7529 let deadline1 =
7530 if deadline = infinity
7531 then now () +. 0.01
7532 else deadline
7534 match state.autoscroll with
7535 | Some step when step != 0 -> deadline1
7536 | _ -> if state.ghyll == noghyll then infinity else deadline1
7538 loop newdeadline
7539 end;
7542 loop infinity;
7543 with Quit ->
7544 Config.save ();