Start fixing zooming in fit page model (WIP)
[llpp.git] / main.ml
blobf2e31e063be7e4cc93ed98b92f73a8f0840c7ce4
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:
1891 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1892 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1893 let iv1 = iv f in
1894 let ins = float a *. iv1
1895 and outs = float (n-b) *. iv1 in
1896 let ones = b - a in
1897 ins +. outs +. float ones
1899 let rec set (_N, _A, _B) y sy =
1900 let sum = summa 1.0 _N _A _B in
1901 let dy = float (y - sy) in
1902 state.ghyll <- (
1903 let rec gf n y1 o =
1904 if n >= _N
1905 then state.ghyll <- noghyll
1906 else
1907 let go n =
1908 let s = scroll n _N _A _B in
1909 let y1 = y1 +. ((s *. dy) /. sum) in
1910 gotoy_and_clear_text (truncate y1);
1911 state.ghyll <- gf (n+1) y1;
1913 match o with
1914 | None -> go n
1915 | Some y' -> set (_N/2, 1, 1) y' state.y
1917 gf 0 (float state.y)
1920 match conf.ghyllscroll with
1921 | None ->
1922 gotoy_and_clear_text y
1923 | Some nab ->
1924 if state.ghyll == noghyll
1925 then set nab y state.y
1926 else state.ghyll (Some y)
1929 let gotopage n top =
1930 let y, h = getpageyh n in
1931 let y = y + (truncate (top *. float h)) in
1932 gotoghyll y
1935 let gotopage1 n top =
1936 let y = getpagey n in
1937 let y = y + top in
1938 gotoghyll y
1941 let invalidate s f =
1942 state.layout <- [];
1943 state.pdims <- [];
1944 state.rects <- [];
1945 state.rects1 <- [];
1946 match state.geomcmds with
1947 | ps, [] when String.length ps = 0 ->
1948 f ();
1949 state.geomcmds <- s, [];
1951 | ps, [] ->
1952 state.geomcmds <- ps, [s, f];
1954 | ps, (s', _) :: rest when s' = s ->
1955 state.geomcmds <- ps, ((s, f) :: rest);
1957 | ps, cmds ->
1958 state.geomcmds <- ps, ((s, f) :: cmds);
1961 let flushpages () =
1962 Hashtbl.iter (fun _ opaque ->
1963 wcmd "freepage %s" opaque;
1964 ) state.pagemap;
1965 Hashtbl.clear state.pagemap;
1968 let flushtiles () =
1969 if not (Queue.is_empty state.tilelru)
1970 then (
1971 Queue.iter (fun (k, p, s) ->
1972 wcmd "freetile %s" p;
1973 state.memused <- state.memused - s;
1974 Hashtbl.remove state.tilemap k;
1975 ) state.tilelru;
1976 state.uioh#infochanged Memused;
1977 Queue.clear state.tilelru;
1979 load state.layout;
1982 let opendoc path password =
1983 state.path <- path;
1984 state.password <- password;
1985 state.gen <- state.gen + 1;
1986 state.docinfo <- [];
1988 flushpages ();
1989 setaalevel conf.aalevel;
1990 let titlepath =
1991 if String.length state.origin = 0
1992 then path
1993 else state.origin
1995 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1996 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1997 invalidate "reqlayout"
1998 (fun () ->
1999 wcmd "reqlayout %d %d %f %s\000"
2000 conf.angle (int_of_fitmodel conf.fitmodel) conf.zoom state.nameddest;
2004 let reload () =
2005 state.anchor <- getanchor ();
2006 opendoc state.path state.password;
2009 let scalecolor c =
2010 let c = c *. conf.colorscale in
2011 (c, c, c);
2014 let scalecolor2 (r, g, b) =
2015 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2018 let docolumns = function
2019 | Csingle _ ->
2020 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2021 let rec loop pageno pdimno pdim y ph pdims =
2022 if pageno = state.pagecount
2023 then ()
2024 else
2025 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2026 match pdims with
2027 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2028 pdimno+1, pdim, rest
2029 | _ ->
2030 pdimno, pdim, pdims
2032 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2033 let y = y +
2034 (if conf.presentation
2035 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2036 else (if pageno = 0 then 0 else conf.interpagespace)
2039 a.(pageno) <- (pdimno, x, y, pdim);
2040 loop (pageno+1) pdimno pdim (y + h) h pdims
2042 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2043 conf.columns <- Csingle a;
2045 | Cmulti ((columns, coverA, coverB), _) ->
2046 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2047 let rec loop pageno pdimno pdim x y rowh pdims =
2048 let rec fixrow m = if m = pageno then () else
2049 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2050 if h < rowh
2051 then (
2052 let y = y + (rowh - h) / 2 in
2053 a.(m) <- (pdimno, x, y, pdim);
2055 fixrow (m+1)
2057 if pageno = state.pagecount
2058 then fixrow (((pageno - 1) / columns) * columns)
2059 else
2060 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2061 match pdims with
2062 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2063 pdimno+1, pdim, rest
2064 | _ ->
2065 pdimno, pdim, pdims
2067 let x, y, rowh' =
2068 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2069 then (
2070 let x = (state.winw - state.scrollw - w) / 2 in
2071 let ips =
2072 if conf.presentation then calcips h else conf.interpagespace in
2073 x, y + ips + rowh, h
2075 else (
2076 if (pageno - coverA) mod columns = 0
2077 then (
2078 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2079 let y =
2080 if conf.presentation
2081 then
2082 let ips = calcips h in
2083 y + (if pageno = 0 then 0 else calcips rowh + ips)
2084 else
2085 y + (if pageno = 0 then 0 else conf.interpagespace)
2087 x, y + rowh, h
2089 else x, y, max rowh h
2092 let y =
2093 if pageno > 1 && (pageno - coverA) mod columns = 0
2094 then (
2095 let y =
2096 if pageno = columns && conf.presentation
2097 then (
2098 let ips = calcips rowh in
2099 for i = 0 to pred columns
2101 let (pdimno, x, y, pdim) = a.(i) in
2102 a.(i) <- (pdimno, x, y+ips, pdim)
2103 done;
2104 y+ips;
2106 else y
2108 fixrow (pageno - columns);
2111 else y
2113 a.(pageno) <- (pdimno, x, y, pdim);
2114 let x = x + w + xoff*2 + conf.interpagespace in
2115 loop (pageno+1) pdimno pdim x y rowh' pdims
2117 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2118 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2120 | Csplit (c, _) ->
2121 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2122 let rec loop pageno pdimno pdim y pdims =
2123 if pageno = state.pagecount
2124 then ()
2125 else
2126 let pdimno, ((_, w, h, _) as pdim), pdims =
2127 match pdims with
2128 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2129 pdimno+1, pdim, rest
2130 | _ ->
2131 pdimno, pdim, pdims
2133 let cw = w / c in
2134 let rec loop1 n x y =
2135 if n = c then y else (
2136 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2137 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2140 let y = loop1 0 0 y in
2141 loop (pageno+1) pdimno pdim y pdims
2143 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2144 conf.columns <- Csplit (c, a);
2147 let represent () =
2148 docolumns conf.columns;
2149 state.maxy <- calcheight ();
2150 state.hscrollh <-
2151 if state.x = 0 && state.w <= state.winw - state.scrollw
2152 then 0
2153 else state.scrollw
2155 if state.reprf == noreprf
2156 then (
2157 match state.mode with
2158 | Birdseye (_, _, pageno, _, _) ->
2159 let y, h = getpageyh pageno in
2160 let top = (state.winh - h) / 2 in
2161 gotoy (max 0 (y - top))
2162 | _ -> gotoanchor state.anchor
2164 else (
2165 state.reprf ();
2166 state.reprf <- noreprf;
2170 let reshape w h =
2171 GlDraw.viewport 0 0 w h;
2172 let firsttime = state.geomcmds == firstgeomcmds in
2173 if not firsttime && nogeomcmds state.geomcmds
2174 then state.anchor <- getanchor ();
2176 state.winw <- w;
2177 let w = truncate (float w *. conf.zoom) - state.scrollw in
2178 let w = max w 2 in
2179 state.winh <- h;
2180 setfontsize fstate.fontsize;
2181 GlMat.mode `modelview;
2182 GlMat.load_identity ();
2184 GlMat.mode `projection;
2185 GlMat.load_identity ();
2186 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2187 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2188 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2190 let relx =
2191 if conf.zoom <= 1.0
2192 then 0.0
2193 else float state.x /. float state.w
2195 invalidate "geometry"
2196 (fun () ->
2197 state.w <- w;
2198 if not firsttime
2199 then state.x <- truncate (relx *. float w);
2200 let w =
2201 match conf.columns with
2202 | Csingle _ -> w
2203 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2204 | Csplit (c, _) -> w * c
2206 wcmd "geometry %d %d %f %d"
2207 w (h - 2*conf.interpagespace) conf.zoom
2208 (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 pdim =
2639 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2641 state.uioh#infochanged Pdim;
2642 state.pdims <- pdim :: state.pdims
2644 | "o" :: args :: [] ->
2645 let (l, n, t, h, pos) =
2646 scan args "%u %u %d %u %n"
2647 (fun l n t h pos -> l, n, t, h, pos)
2649 let s = String.sub args pos (String.length args - pos) in
2650 let outline = (s, l, (n, float t /. float h, 0.0)) in
2651 begin match state.currently with
2652 | Outlining outlines ->
2653 state.currently <- Outlining (outline :: outlines)
2654 | Idle ->
2655 state.currently <- Outlining [outline]
2656 | currently ->
2657 dolog "invalid outlining state";
2658 logcurrently currently
2661 | "a" :: args :: [] ->
2662 let (n, l, t) =
2663 scan args "%u %d %d" (fun n l t -> n, l, t)
2665 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2667 | "info" :: args :: [] ->
2668 state.docinfo <- (1, args) :: state.docinfo
2670 | "infoend" :: [] ->
2671 state.uioh#infochanged Docinfo;
2672 state.docinfo <- List.rev state.docinfo
2674 | _ ->
2675 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2678 let onhist cb =
2679 let rc = cb.rc in
2680 let action = function
2681 | HCprev -> cbget cb ~-1
2682 | HCnext -> cbget cb 1
2683 | HCfirst -> cbget cb ~-(cb.rc)
2684 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2685 and cancel () = cb.rc <- rc
2686 in (action, cancel)
2689 let search pattern forward =
2690 if String.length pattern > 0
2691 then
2692 let pn, py =
2693 match state.layout with
2694 | [] -> 0, 0
2695 | l :: _ ->
2696 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2698 wcmd "search %d %d %d %d,%s\000"
2699 (btod conf.icase) pn py (btod forward) pattern;
2702 let intentry text key =
2703 let c =
2704 if key >= 32 && key < 127
2705 then Char.chr key
2706 else '\000'
2708 match c with
2709 | '0' .. '9' ->
2710 let text = addchar text c in
2711 TEcont text
2713 | _ ->
2714 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2715 TEcont text
2718 let linknentry text key =
2719 let c =
2720 if key >= 32 && key < 127
2721 then Char.chr key
2722 else '\000'
2724 match c with
2725 | 'a' .. 'z' ->
2726 let text = addchar text c in
2727 TEcont text
2729 | _ ->
2730 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2731 TEcont text
2734 let linkndone f s =
2735 if String.length s > 0
2736 then (
2737 let n =
2738 let l = String.length s in
2739 let rec loop pos n = if pos = l then n else
2740 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2741 loop (pos+1) (n*26 + m)
2742 in loop 0 0
2744 let rec loop n = function
2745 | [] -> ()
2746 | l :: rest ->
2747 match getopaque l.pageno with
2748 | None -> loop n rest
2749 | Some opaque ->
2750 let m = getlinkcount opaque in
2751 if n < m
2752 then (
2753 let under = getlink opaque n in
2754 f under
2756 else loop (n-m) rest
2758 loop n state.layout;
2762 let textentry text key =
2763 if key land 0xff00 = 0xff00
2764 then TEcont text
2765 else TEcont (text ^ toutf8 key)
2768 let reqlayout angle fitmodel =
2769 match state.throttle with
2770 | None ->
2771 if nogeomcmds state.geomcmds
2772 then state.anchor <- getanchor ();
2773 conf.angle <- angle mod 360;
2774 if conf.angle != 0
2775 then (
2776 match state.mode with
2777 | LinkNav _ -> state.mode <- View
2778 | _ -> ()
2780 conf.fitmodel <- fitmodel;
2781 invalidate "reqlayout"
2782 (fun () ->
2783 wcmd "reqlayout %d %d %f"
2784 conf.angle (int_of_fitmodel fitmodel) conf.zoom);
2785 | _ -> ()
2788 let settrim trimmargins trimfuzz =
2789 if nogeomcmds state.geomcmds
2790 then state.anchor <- getanchor ();
2791 conf.trimmargins <- trimmargins;
2792 conf.trimfuzz <- trimfuzz;
2793 let x0, y0, x1, y1 = trimfuzz in
2794 invalidate "settrim"
2795 (fun () ->
2796 wcmd "settrim %d %d %d %d %d %f"
2797 (btod conf.trimmargins) x0 y0 x1 y1 conf.zoom);
2798 flushpages ();
2801 let setzoom zoom =
2802 match state.throttle with
2803 | None ->
2804 let zoom = max 0.0001 zoom in
2805 if zoom <> conf.zoom
2806 then (
2807 state.prevzoom <- conf.zoom;
2808 conf.zoom <- zoom;
2809 reshape state.winw state.winh;
2810 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2813 | Some (layout, y, started) ->
2814 let time =
2815 match conf.maxwait with
2816 | None -> 0.0
2817 | Some t -> t
2819 let dt = now () -. started in
2820 if dt > time
2821 then (
2822 state.y <- y;
2823 load layout;
2827 let setcolumns mode columns coverA coverB =
2828 state.prevcolumns <- Some (conf.columns, conf.zoom);
2829 if columns < 0
2830 then (
2831 if isbirdseye mode
2832 then showtext '!' "split mode doesn't work in bird's eye"
2833 else (
2834 conf.columns <- Csplit (-columns, [||]);
2835 state.x <- 0;
2836 conf.zoom <- 1.0;
2839 else (
2840 if columns < 2
2841 then (
2842 conf.columns <- Csingle [||];
2843 state.x <- 0;
2844 setzoom 1.0;
2846 else (
2847 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2848 conf.zoom <- 1.0;
2851 reshape state.winw state.winh;
2854 let enterbirdseye () =
2855 let zoom = float conf.thumbw /. float state.winw in
2856 let birdseyepageno =
2857 let cy = state.winh / 2 in
2858 let fold = function
2859 | [] -> 0
2860 | l :: rest ->
2861 let rec fold best = function
2862 | [] -> best.pageno
2863 | l :: rest ->
2864 let d = cy - (l.pagedispy + l.pagevh/2)
2865 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2866 if abs d < abs dbest
2867 then fold l rest
2868 else best.pageno
2869 in fold l rest
2871 fold state.layout
2873 state.mode <- Birdseye (
2874 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2876 conf.zoom <- zoom;
2877 conf.presentation <- false;
2878 conf.interpagespace <- 10;
2879 conf.hlinks <- false;
2880 conf.fitmodel <- FitProportional;
2881 state.x <- 0;
2882 state.mstate <- Mnone;
2883 conf.maxwait <- None;
2884 conf.columns <- (
2885 match conf.beyecolumns with
2886 | Some c ->
2887 conf.zoom <- 1.0;
2888 Cmulti ((c, 0, 0), [||])
2889 | None -> Csingle [||]
2891 Wsi.setcursor Wsi.CURSOR_INHERIT;
2892 if conf.verbose
2893 then
2894 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2895 (100.0*.zoom)
2896 else
2897 state.text <- ""
2899 reshape state.winw state.winh;
2902 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2903 state.mode <- View;
2904 conf.zoom <- c.zoom;
2905 conf.presentation <- c.presentation;
2906 conf.interpagespace <- c.interpagespace;
2907 conf.maxwait <- c.maxwait;
2908 conf.hlinks <- c.hlinks;
2909 conf.fitmodel <- c.fitmodel;
2910 conf.beyecolumns <- (
2911 match conf.columns with
2912 | Cmulti ((c, _, _), _) -> Some c
2913 | Csingle _ -> None
2914 | Csplit _ -> failwith "leaving bird's eye split mode"
2916 conf.columns <- (
2917 match c.columns with
2918 | Cmulti (c, _) -> Cmulti (c, [||])
2919 | Csingle _ -> Csingle [||]
2920 | Csplit (c, _) -> Csplit (c, [||])
2922 state.x <- leftx;
2923 if conf.verbose
2924 then
2925 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2926 (100.0*.conf.zoom)
2928 reshape state.winw state.winh;
2929 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2932 let togglebirdseye () =
2933 match state.mode with
2934 | Birdseye vals -> leavebirdseye vals true
2935 | View -> enterbirdseye ()
2936 | _ -> ()
2939 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2940 let pageno = max 0 (pageno - incr) in
2941 let rec loop = function
2942 | [] -> gotopage1 pageno 0
2943 | l :: _ when l.pageno = pageno ->
2944 if l.pagedispy >= 0 && l.pagey = 0
2945 then G.postRedisplay "upbirdseye"
2946 else gotopage1 pageno 0
2947 | _ :: rest -> loop rest
2949 loop state.layout;
2950 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2953 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2954 let pageno = min (state.pagecount - 1) (pageno + incr) in
2955 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2956 let rec loop = function
2957 | [] ->
2958 let y, h = getpageyh pageno in
2959 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2960 gotoy (clamp dy)
2961 | l :: _ when l.pageno = pageno ->
2962 if l.pagevh != l.pageh
2963 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2964 else G.postRedisplay "downbirdseye"
2965 | _ :: rest -> loop rest
2967 loop state.layout
2970 let optentry mode _ key =
2971 let btos b = if b then "on" else "off" in
2972 if key >= 32 && key < 127
2973 then
2974 let c = Char.chr key in
2975 match c with
2976 | 's' ->
2977 let ondone s =
2978 try conf.scrollstep <- int_of_string s with exc ->
2979 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2981 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2983 | 'A' ->
2984 let ondone s =
2986 conf.autoscrollstep <- int_of_string s;
2987 if state.autoscroll <> None
2988 then state.autoscroll <- Some conf.autoscrollstep
2989 with exc ->
2990 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2992 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2994 | 'C' ->
2995 let ondone s =
2997 let n, a, b = multicolumns_of_string s in
2998 setcolumns mode n a b;
2999 with exc ->
3000 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3002 TEswitch ("columns: ", "", None, textentry, ondone, true)
3004 | 'Z' ->
3005 let ondone s =
3007 let zoom = float (int_of_string s) /. 100.0 in
3008 setzoom zoom
3009 with exc ->
3010 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3012 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3014 | 't' ->
3015 let ondone s =
3017 conf.thumbw <- bound (int_of_string s) 2 4096;
3018 state.text <-
3019 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3020 begin match mode with
3021 | Birdseye beye ->
3022 leavebirdseye beye false;
3023 enterbirdseye ();
3024 | _ -> ();
3026 with exc ->
3027 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3029 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3031 | 'R' ->
3032 let ondone s =
3033 match try
3034 Some (int_of_string s)
3035 with exc ->
3036 state.text <- Printf.sprintf "bad integer `%s': %s"
3037 s (exntos exc);
3038 None
3039 with
3040 | Some angle -> reqlayout angle conf.fitmodel
3041 | None -> ()
3043 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3045 | 'i' ->
3046 conf.icase <- not conf.icase;
3047 TEdone ("case insensitive search " ^ (btos conf.icase))
3049 | 'p' ->
3050 conf.preload <- not conf.preload;
3051 gotoy state.y;
3052 TEdone ("preload " ^ (btos conf.preload))
3054 | 'v' ->
3055 conf.verbose <- not conf.verbose;
3056 TEdone ("verbose " ^ (btos conf.verbose))
3058 | 'd' ->
3059 conf.debug <- not conf.debug;
3060 TEdone ("debug " ^ (btos conf.debug))
3062 | 'h' ->
3063 conf.maxhfit <- not conf.maxhfit;
3064 state.maxy <- calcheight ();
3065 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3067 | 'c' ->
3068 conf.crophack <- not conf.crophack;
3069 TEdone ("crophack " ^ btos conf.crophack)
3071 | 'a' ->
3072 let s =
3073 match conf.maxwait with
3074 | None ->
3075 conf.maxwait <- Some infinity;
3076 "always wait for page to complete"
3077 | Some _ ->
3078 conf.maxwait <- None;
3079 "show placeholder if page is not ready"
3081 TEdone s
3083 | 'f' ->
3084 conf.underinfo <- not conf.underinfo;
3085 TEdone ("underinfo " ^ btos conf.underinfo)
3087 | 'P' ->
3088 conf.savebmarks <- not conf.savebmarks;
3089 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3091 | 'S' ->
3092 let ondone s =
3094 let pageno, py =
3095 match state.layout with
3096 | [] -> 0, 0
3097 | l :: _ ->
3098 l.pageno, l.pagey
3100 conf.interpagespace <- int_of_string s;
3101 docolumns conf.columns;
3102 state.maxy <- calcheight ();
3103 let y = getpagey pageno in
3104 gotoy (y + py)
3105 with exc ->
3106 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3108 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3110 | 'l' ->
3111 let fm =
3112 match conf.fitmodel with
3113 | FitProportional -> FitWidth
3114 | _ -> FitProportional
3116 reqlayout conf.angle fm;
3117 TEdone ("proportional display " ^ btos (fm == FitProportional))
3119 | 'T' ->
3120 settrim (not conf.trimmargins) conf.trimfuzz;
3121 TEdone ("trim margins " ^ btos conf.trimmargins)
3123 | 'I' ->
3124 conf.invert <- not conf.invert;
3125 TEdone ("invert colors " ^ btos conf.invert)
3127 | 'x' ->
3128 let ondone s =
3129 cbput state.hists.sel s;
3130 conf.selcmd <- s;
3132 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3133 textentry, ondone, true)
3135 | _ ->
3136 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3137 TEstop
3138 else
3139 TEcont state.text
3142 class type lvsource = object
3143 method getitemcount : int
3144 method getitem : int -> (string * int)
3145 method hasaction : int -> bool
3146 method exit :
3147 uioh:uioh ->
3148 cancel:bool ->
3149 active:int ->
3150 first:int ->
3151 pan:int ->
3152 qsearch:string ->
3153 uioh option
3154 method getactive : int
3155 method getfirst : int
3156 method getqsearch : string
3157 method setqsearch : string -> unit
3158 method getpan : int
3159 end;;
3161 class virtual lvsourcebase = object
3162 val mutable m_active = 0
3163 val mutable m_first = 0
3164 val mutable m_qsearch = ""
3165 val mutable m_pan = 0
3166 method getactive = m_active
3167 method getfirst = m_first
3168 method getqsearch = m_qsearch
3169 method getpan = m_pan
3170 method setqsearch s = m_qsearch <- s
3171 end;;
3173 let withoutlastutf8 s =
3174 let len = String.length s in
3175 if len = 0
3176 then s
3177 else
3178 let rec find pos =
3179 if pos = 0
3180 then pos
3181 else
3182 let b = Char.code s.[pos] in
3183 if b land 0b11000000 = 0b11000000
3184 then pos
3185 else find (pos-1)
3187 let first =
3188 if Char.code s.[len-1] land 0x80 = 0
3189 then len-1
3190 else find (len-1)
3192 String.sub s 0 first;
3195 let textentrykeyboard
3196 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3197 let key =
3198 if key >= 0xffb0 && key <= 0xffb9
3199 then key - 0xffb0 + 48 else key
3201 let enttext te =
3202 state.mode <- Textentry (te, onleave);
3203 state.text <- "";
3204 enttext ();
3205 G.postRedisplay "textentrykeyboard enttext";
3207 let histaction cmd =
3208 match opthist with
3209 | None -> ()
3210 | Some (action, _) ->
3211 state.mode <- Textentry (
3212 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3214 G.postRedisplay "textentry histaction"
3216 match key with
3217 | 0xff08 -> (* backspace *)
3218 let s = withoutlastutf8 text in
3219 let len = String.length s in
3220 if cancelonempty && len = 0
3221 then (
3222 onleave Cancel;
3223 G.postRedisplay "textentrykeyboard after cancel";
3225 else (
3226 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3229 | 0xff0d | 0xff8d -> (* (kp) enter *)
3230 ondone text;
3231 onleave Confirm;
3232 G.postRedisplay "textentrykeyboard after confirm"
3234 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3235 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3236 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3237 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3239 | 0xff1b -> (* escape*)
3240 if String.length text = 0
3241 then (
3242 begin match opthist with
3243 | None -> ()
3244 | Some (_, onhistcancel) -> onhistcancel ()
3245 end;
3246 onleave Cancel;
3247 state.text <- "";
3248 G.postRedisplay "textentrykeyboard after cancel2"
3250 else (
3251 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3254 | 0xff9f | 0xffff -> () (* delete *)
3256 | _ when key != 0
3257 && key land 0xff00 != 0xff00 (* keyboard *)
3258 && key land 0xfe00 != 0xfe00 (* xkb *)
3259 && key land 0xfd00 != 0xfd00 (* 3270 *)
3261 begin match onkey text key with
3262 | TEdone text ->
3263 ondone text;
3264 onleave Confirm;
3265 G.postRedisplay "textentrykeyboard after confirm2";
3267 | TEcont text ->
3268 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3270 | TEstop ->
3271 onleave Cancel;
3272 G.postRedisplay "textentrykeyboard after cancel3"
3274 | TEswitch te ->
3275 state.mode <- Textentry (te, onleave);
3276 G.postRedisplay "textentrykeyboard switch";
3277 end;
3279 | _ ->
3280 vlog "unhandled key %s" (Wsi.keyname key)
3283 let firstof first active =
3284 if first > active || abs (first - active) > fstate.maxrows - 1
3285 then max 0 (active - (fstate.maxrows/2))
3286 else first
3289 let calcfirst first active =
3290 if active > first
3291 then
3292 let rows = active - first in
3293 if rows > fstate.maxrows then active - fstate.maxrows else first
3294 else active
3297 let scrollph y maxy =
3298 let sh = float (maxy + state.winh) /. float state.winh in
3299 let sh = float state.winh /. sh in
3300 let sh = max sh (float conf.scrollh) in
3302 let percent = float y /. float maxy in
3303 let position = (float state.winh -. sh) *. percent in
3305 let position =
3306 if position +. sh > float state.winh
3307 then float state.winh -. sh
3308 else position
3310 position, sh;
3313 let coe s = (s :> uioh);;
3315 class listview ~(source:lvsource) ~trusted ~modehash =
3316 object (self)
3317 val m_pan = source#getpan
3318 val m_first = source#getfirst
3319 val m_active = source#getactive
3320 val m_qsearch = source#getqsearch
3321 val m_prev_uioh = state.uioh
3323 method private elemunder y =
3324 let n = y / (fstate.fontsize+1) in
3325 if m_first + n < source#getitemcount
3326 then (
3327 if source#hasaction (m_first + n)
3328 then Some (m_first + n)
3329 else None
3331 else None
3333 method display =
3334 Gl.enable `blend;
3335 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3336 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3337 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3338 GlDraw.color (1., 1., 1.);
3339 Gl.enable `texture_2d;
3340 let fs = fstate.fontsize in
3341 let nfs = fs + 1 in
3342 let ww = fstate.wwidth in
3343 let tabw = 30.0*.ww in
3344 let itemcount = source#getitemcount in
3345 let rec loop row =
3346 if (row - m_first) > fstate.maxrows
3347 then ()
3348 else (
3349 if row >= 0 && row < itemcount
3350 then (
3351 let (s, level) = source#getitem row in
3352 let y = (row - m_first) * nfs in
3353 let x = 5.0 +. float (level + m_pan) *. ww in
3354 if row = m_active
3355 then (
3356 Gl.disable `texture_2d;
3357 GlDraw.polygon_mode `both `line;
3358 let alpha = if source#hasaction row then 0.9 else 0.3 in
3359 GlDraw.color (1., 1., 1.) ~alpha;
3360 GlDraw.rect (1., float (y + 1))
3361 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3362 GlDraw.polygon_mode `both `fill;
3363 GlDraw.color (1., 1., 1.);
3364 Gl.enable `texture_2d;
3367 let drawtabularstring s =
3368 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3369 if trusted
3370 then
3371 let tabpos = try String.index s '\t' with Not_found -> -1 in
3372 if tabpos > 0
3373 then
3374 let len = String.length s - tabpos - 1 in
3375 let s1 = String.sub s 0 tabpos
3376 and s2 = String.sub s (tabpos + 1) len in
3377 let nx = drawstr x s1 in
3378 let sw = nx -. x in
3379 let x = x +. (max tabw sw) in
3380 drawstr x s2
3381 else
3382 drawstr x s
3383 else
3384 drawstr x s
3386 let _ = drawtabularstring s in
3387 loop (row+1)
3391 loop m_first;
3392 Gl.disable `blend;
3393 Gl.disable `texture_2d;
3395 method updownlevel incr =
3396 let len = source#getitemcount in
3397 let curlevel =
3398 if m_active >= 0 && m_active < len
3399 then snd (source#getitem m_active)
3400 else -1
3402 let rec flow i =
3403 if i = len then i-1 else if i = -1 then 0 else
3404 let _, l = source#getitem i in
3405 if l != curlevel then i else flow (i+incr)
3407 let active = flow m_active in
3408 let first = calcfirst m_first active in
3409 G.postRedisplay "outline updownlevel";
3410 {< m_active = active; m_first = first >}
3412 method private key1 key mask =
3413 let set1 active first qsearch =
3414 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3416 let search active pattern incr =
3417 let active = if active = -1 then m_first else active in
3418 let dosearch re =
3419 let rec loop n =
3420 if n >= 0 && n < source#getitemcount
3421 then (
3422 let s, _ = source#getitem n in
3424 (try ignore (Str.search_forward re s 0); true
3425 with Not_found -> false)
3426 then Some n
3427 else loop (n + incr)
3429 else None
3431 loop active
3434 let re = Str.regexp_case_fold pattern in
3435 dosearch re
3436 with Failure s ->
3437 state.text <- s;
3438 None
3440 let itemcount = source#getitemcount in
3441 let find start incr =
3442 let rec find i =
3443 if i = -1 || i = itemcount
3444 then -1
3445 else (
3446 if source#hasaction i
3447 then i
3448 else find (i + incr)
3451 find start
3453 let set active first =
3454 let first = bound first 0 (itemcount - fstate.maxrows) in
3455 state.text <- "";
3456 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3458 let navigate incr =
3459 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3460 let active, first =
3461 let incr1 = if incr > 0 then 1 else -1 in
3462 if isvisible m_first m_active
3463 then
3464 let next =
3465 let next = m_active + incr in
3466 let next =
3467 if next < 0 || next >= itemcount
3468 then -1
3469 else find next incr1
3471 if next = -1 || abs (m_active - next) > fstate.maxrows
3472 then -1
3473 else next
3475 if next = -1
3476 then
3477 let first = m_first + incr in
3478 let first = bound first 0 (itemcount - 1) in
3479 let next =
3480 let next = m_active + incr in
3481 let next = bound next 0 (itemcount - 1) in
3482 find next ~-incr1
3484 let active = if next = -1 then m_active else next in
3485 active, first
3486 else
3487 let first = min next m_first in
3488 let first =
3489 if abs (next - first) > fstate.maxrows
3490 then first + incr
3491 else first
3493 next, first
3494 else
3495 let first = m_first + incr in
3496 let first = bound first 0 (itemcount - 1) in
3497 let active =
3498 let next = m_active + incr in
3499 let next = bound next 0 (itemcount - 1) in
3500 let next = find next incr1 in
3501 let active =
3502 if next = -1 || abs (m_active - first) > fstate.maxrows
3503 then (
3504 let active = if m_active = -1 then next else m_active in
3505 active
3507 else next
3509 if isvisible first active
3510 then active
3511 else -1
3513 active, first
3515 G.postRedisplay "listview navigate";
3516 set active first;
3518 match key with
3519 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3520 let incr = if key = 0x72 then -1 else 1 in
3521 let active, first =
3522 match search (m_active + incr) m_qsearch incr with
3523 | None ->
3524 state.text <- m_qsearch ^ " [not found]";
3525 m_active, m_first
3526 | Some active ->
3527 state.text <- m_qsearch;
3528 active, firstof m_first active
3530 G.postRedisplay "listview ctrl-r/s";
3531 set1 active first m_qsearch;
3533 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3534 if m_active >= 0 && m_active < source#getitemcount
3535 then (
3536 let s, _ = source#getitem m_active in
3537 selstring s;
3539 coe self
3541 | 0xff08 -> (* backspace *)
3542 if String.length m_qsearch = 0
3543 then coe self
3544 else (
3545 let qsearch = withoutlastutf8 m_qsearch in
3546 let len = String.length qsearch in
3547 if len = 0
3548 then (
3549 state.text <- "";
3550 G.postRedisplay "listview empty qsearch";
3551 set1 m_active m_first "";
3553 else
3554 let active, first =
3555 match search m_active qsearch ~-1 with
3556 | None ->
3557 state.text <- qsearch ^ " [not found]";
3558 m_active, m_first
3559 | Some active ->
3560 state.text <- qsearch;
3561 active, firstof m_first active
3563 G.postRedisplay "listview backspace qsearch";
3564 set1 active first qsearch
3567 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3568 let pattern = m_qsearch ^ toutf8 key in
3569 let active, first =
3570 match search m_active pattern 1 with
3571 | None ->
3572 state.text <- pattern ^ " [not found]";
3573 m_active, m_first
3574 | Some active ->
3575 state.text <- pattern;
3576 active, firstof m_first active
3578 G.postRedisplay "listview qsearch add";
3579 set1 active first pattern;
3581 | 0xff1b -> (* escape *)
3582 state.text <- "";
3583 if String.length m_qsearch = 0
3584 then (
3585 G.postRedisplay "list view escape";
3586 begin
3587 match
3588 source#exit (coe self) true m_active m_first m_pan m_qsearch
3589 with
3590 | None -> m_prev_uioh
3591 | Some uioh -> uioh
3594 else (
3595 G.postRedisplay "list view kill qsearch";
3596 source#setqsearch "";
3597 coe {< m_qsearch = "" >}
3600 | 0xff0d | 0xff8d -> (* (kp) enter *)
3601 state.text <- "";
3602 let self = {< m_qsearch = "" >} in
3603 source#setqsearch "";
3604 let opt =
3605 G.postRedisplay "listview enter";
3606 if m_active >= 0 && m_active < source#getitemcount
3607 then (
3608 source#exit (coe self) false m_active m_first m_pan "";
3610 else (
3611 source#exit (coe self) true m_active m_first m_pan "";
3614 begin match opt with
3615 | None -> m_prev_uioh
3616 | Some uioh -> uioh
3619 | 0xff9f | 0xffff -> (* (kp) delete *)
3620 coe self
3622 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3623 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3624 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3625 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3627 | 0xff53 | 0xff98 -> (* (kp) right *)
3628 state.text <- "";
3629 G.postRedisplay "listview right";
3630 coe {< m_pan = m_pan - 1 >}
3632 | 0xff51 | 0xff96 -> (* (kp) left *)
3633 state.text <- "";
3634 G.postRedisplay "listview left";
3635 coe {< m_pan = m_pan + 1 >}
3637 | 0xff50 | 0xff95 -> (* (kp) home *)
3638 let active = find 0 1 in
3639 G.postRedisplay "listview home";
3640 set active 0;
3642 | 0xff57 | 0xff9c -> (* (kp) end *)
3643 let first = max 0 (itemcount - fstate.maxrows) in
3644 let active = find (itemcount - 1) ~-1 in
3645 G.postRedisplay "listview end";
3646 set active first;
3648 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3649 coe self
3651 | _ ->
3652 dolog "listview unknown key %#x" key; coe self
3654 method key key mask =
3655 match state.mode with
3656 | Textentry te -> textentrykeyboard key mask te; coe self
3657 | _ -> self#key1 key mask
3659 method button button down x y _ =
3660 let opt =
3661 match button with
3662 | 1 when x > state.winw - conf.scrollbw ->
3663 G.postRedisplay "listview scroll";
3664 if down
3665 then
3666 let _, position, sh = self#scrollph in
3667 if y > truncate position && y < truncate (position +. sh)
3668 then (
3669 state.mstate <- Mscrolly;
3670 Some (coe self)
3672 else
3673 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3674 let first = truncate (s *. float source#getitemcount) in
3675 let first = min source#getitemcount first in
3676 Some (coe {< m_first = first; m_active = first >})
3677 else (
3678 state.mstate <- Mnone;
3679 Some (coe self);
3681 | 1 when not down ->
3682 begin match self#elemunder y with
3683 | Some n ->
3684 G.postRedisplay "listview click";
3685 source#exit
3686 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3687 | _ ->
3688 Some (coe self)
3690 | n when (n == 4 || n == 5) && not down ->
3691 let len = source#getitemcount in
3692 let first =
3693 if n = 5 && m_first + fstate.maxrows >= len
3694 then
3695 m_first
3696 else
3697 let first = m_first + (if n == 4 then -1 else 1) in
3698 bound first 0 (len - 1)
3700 G.postRedisplay "listview wheel";
3701 Some (coe {< m_first = first >})
3702 | n when (n = 6 || n = 7) && not down ->
3703 let inc = m_first + (if n = 7 then -1 else 1) in
3704 G.postRedisplay "listview hwheel";
3705 Some (coe {< m_pan = m_pan + inc >})
3706 | _ ->
3707 Some (coe self)
3709 match opt with
3710 | None -> m_prev_uioh
3711 | Some uioh -> uioh
3713 method motion _ y =
3714 match state.mstate with
3715 | Mscrolly ->
3716 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3717 let first = truncate (s *. float source#getitemcount) in
3718 let first = min source#getitemcount first in
3719 G.postRedisplay "listview motion";
3720 coe {< m_first = first; m_active = first >}
3721 | _ -> coe self
3723 method pmotion x y =
3724 if x < state.winw - conf.scrollbw
3725 then
3726 let n =
3727 match self#elemunder y with
3728 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3729 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3731 let o =
3732 if n != m_active
3733 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3734 else self
3736 coe o
3737 else (
3738 Wsi.setcursor Wsi.CURSOR_INHERIT;
3739 coe self
3742 method infochanged _ = ()
3744 method scrollpw = (0, 0.0, 0.0)
3745 method scrollph =
3746 let nfs = fstate.fontsize + 1 in
3747 let y = m_first * nfs in
3748 let itemcount = source#getitemcount in
3749 let maxi = max 0 (itemcount - fstate.maxrows) in
3750 let maxy = maxi * nfs in
3751 let p, h = scrollph y maxy in
3752 conf.scrollbw, p, h
3754 method modehash = modehash
3755 method eformsgs = false
3756 end;;
3758 class outlinelistview ~source =
3759 object (self)
3760 inherit listview
3761 ~source:(source :> lvsource)
3762 ~trusted:false
3763 ~modehash:(findkeyhash conf "outline")
3764 as super
3766 method key key mask =
3767 let calcfirst first active =
3768 if active > first
3769 then
3770 let rows = active - first in
3771 let maxrows =
3772 if String.length state.text = 0
3773 then fstate.maxrows
3774 else fstate.maxrows - 2
3776 if rows > maxrows then active - maxrows else first
3777 else active
3779 let navigate incr =
3780 let active = m_active + incr in
3781 let active = bound active 0 (source#getitemcount - 1) in
3782 let first = calcfirst m_first active in
3783 G.postRedisplay "outline navigate";
3784 coe {< m_active = active; m_first = first >}
3786 let ctrl = Wsi.withctrl mask in
3787 match key with
3788 | 110 when ctrl -> (* ctrl-n *)
3789 source#narrow m_qsearch;
3790 G.postRedisplay "outline ctrl-n";
3791 coe {< m_first = 0; m_active = 0 >}
3793 | 117 when ctrl -> (* ctrl-u *)
3794 source#denarrow;
3795 G.postRedisplay "outline ctrl-u";
3796 state.text <- "";
3797 coe {< m_first = 0; m_active = 0 >}
3799 | 108 when ctrl -> (* ctrl-l *)
3800 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3801 G.postRedisplay "outline ctrl-l";
3802 coe {< m_first = first >}
3804 | 0xff9f | 0xffff -> (* (kp) delete *)
3805 source#remove m_active;
3806 G.postRedisplay "outline delete";
3807 let active = max 0 (m_active-1) in
3808 coe {< m_first = firstof m_first active;
3809 m_active = active >}
3811 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3812 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3813 | 0xff55 | 0xff9a -> (* (kp) prior *)
3814 navigate ~-(fstate.maxrows)
3815 | 0xff56 | 0xff9b -> (* (kp) next *)
3816 navigate fstate.maxrows
3818 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3819 let o =
3820 if ctrl
3821 then (
3822 G.postRedisplay "outline ctrl right";
3823 {< m_pan = m_pan + 1 >}
3825 else self#updownlevel 1
3827 coe o
3829 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3830 let o =
3831 if ctrl
3832 then (
3833 G.postRedisplay "outline ctrl left";
3834 {< m_pan = m_pan - 1 >}
3836 else self#updownlevel ~-1
3838 coe o
3840 | 0xff50 | 0xff95 -> (* (kp) home *)
3841 G.postRedisplay "outline home";
3842 coe {< m_first = 0; m_active = 0 >}
3844 | 0xff57 | 0xff9c -> (* (kp) end *)
3845 let active = source#getitemcount - 1 in
3846 let first = max 0 (active - fstate.maxrows) in
3847 G.postRedisplay "outline end";
3848 coe {< m_active = active; m_first = first >}
3850 | _ -> super#key key mask
3853 let outlinesource usebookmarks =
3854 let empty = [||] in
3855 (object
3856 inherit lvsourcebase
3857 val mutable m_items = empty
3858 val mutable m_orig_items = empty
3859 val mutable m_prev_items = empty
3860 val mutable m_narrow_pattern = ""
3861 val mutable m_hadremovals = false
3863 method getitemcount =
3864 Array.length m_items + (if m_hadremovals then 1 else 0)
3866 method getitem n =
3867 if n == Array.length m_items && m_hadremovals
3868 then
3869 ("[Confirm removal]", 0)
3870 else
3871 let s, n, _ = m_items.(n) in
3872 (s, n)
3874 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3875 ignore (uioh, first, qsearch);
3876 let confrimremoval = m_hadremovals && active = Array.length m_items in
3877 let items =
3878 if String.length m_narrow_pattern = 0
3879 then m_orig_items
3880 else m_items
3882 if not cancel
3883 then (
3884 if not confrimremoval
3885 then(
3886 let _, _, anchor = m_items.(active) in
3887 gotoghyll (getanchory anchor);
3888 m_items <- items;
3890 else (
3891 state.bookmarks <- Array.to_list m_items;
3892 m_orig_items <- m_items;
3895 else m_items <- items;
3896 m_pan <- pan;
3897 None
3899 method hasaction _ = true
3901 method greetmsg =
3902 if Array.length m_items != Array.length m_orig_items
3903 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3904 else ""
3906 method narrow pattern =
3907 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3908 match reopt with
3909 | None -> ()
3910 | Some re ->
3911 let rec loop accu n =
3912 if n = -1
3913 then (
3914 m_narrow_pattern <- pattern;
3915 m_items <- Array.of_list accu
3917 else
3918 let (s, _, _) as o = m_items.(n) in
3919 let accu =
3920 if (try ignore (Str.search_forward re s 0); true
3921 with Not_found -> false)
3922 then o :: accu
3923 else accu
3925 loop accu (n-1)
3927 loop [] (Array.length m_items - 1)
3929 method denarrow =
3930 m_orig_items <- (
3931 if usebookmarks
3932 then Array.of_list state.bookmarks
3933 else state.outlines
3935 m_items <- m_orig_items
3937 method remove m =
3938 if usebookmarks
3939 then
3940 if m >= 0 && m < Array.length m_items
3941 then (
3942 m_hadremovals <- true;
3943 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3944 let n = if n >= m then n+1 else n in
3945 m_items.(n)
3949 method reset anchor items =
3950 m_hadremovals <- false;
3951 if m_orig_items == empty || m_prev_items != items
3952 then (
3953 m_orig_items <- items;
3954 if String.length m_narrow_pattern = 0
3955 then m_items <- items;
3957 m_prev_items <- items;
3958 let rely = getanchory anchor in
3959 let active =
3960 let rec loop n best bestd =
3961 if n = Array.length m_items
3962 then best
3963 else
3964 let (_, _, anchor) = m_items.(n) in
3965 let orely = getanchory anchor in
3966 let d = abs (orely - rely) in
3967 if d < bestd
3968 then loop (n+1) n d
3969 else loop (n+1) best bestd
3971 loop 0 ~-1 max_int
3973 m_active <- active;
3974 m_first <- firstof m_first active
3975 end)
3978 let enterselector usebookmarks =
3979 let source = outlinesource usebookmarks in
3980 fun errmsg ->
3981 let outlines =
3982 if usebookmarks
3983 then Array.of_list state.bookmarks
3984 else state.outlines
3986 if Array.length outlines = 0
3987 then (
3988 showtext ' ' errmsg;
3990 else (
3991 state.text <- source#greetmsg;
3992 Wsi.setcursor Wsi.CURSOR_INHERIT;
3993 let anchor = getanchor () in
3994 source#reset anchor outlines;
3995 state.uioh <- coe (new outlinelistview ~source);
3996 G.postRedisplay "enter selector";
4000 let enteroutlinemode =
4001 let f = enterselector false in
4002 fun ()-> f "Document has no outline";
4005 let enterbookmarkmode =
4006 let f = enterselector true in
4007 fun () -> f "Document has no bookmarks (yet)";
4010 let color_of_string s =
4011 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4012 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4016 let color_to_string (r, g, b) =
4017 let r = truncate (r *. 256.0)
4018 and g = truncate (g *. 256.0)
4019 and b = truncate (b *. 256.0) in
4020 Printf.sprintf "%d/%d/%d" r g b
4023 let irect_of_string s =
4024 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4027 let irect_to_string (x0,y0,x1,y1) =
4028 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4031 let makecheckers () =
4032 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4033 following to say:
4034 converted by Issac Trotts. July 25, 2002 *)
4035 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4036 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4037 let id = GlTex.gen_texture () in
4038 GlTex.bind_texture `texture_2d id;
4039 GlPix.store (`unpack_alignment 1);
4040 GlTex.image2d image;
4041 List.iter (GlTex.parameter ~target:`texture_2d)
4042 [ `mag_filter `nearest; `min_filter `nearest ];
4046 let setcheckers enabled =
4047 match state.texid with
4048 | None ->
4049 if enabled then state.texid <- Some (makecheckers ())
4051 | Some texid ->
4052 if not enabled
4053 then (
4054 GlTex.delete_texture texid;
4055 state.texid <- None;
4059 let int_of_string_with_suffix s =
4060 let l = String.length s in
4061 let s1, shift =
4062 if l > 1
4063 then
4064 let suffix = Char.lowercase s.[l-1] in
4065 match suffix with
4066 | 'k' -> String.sub s 0 (l-1), 10
4067 | 'm' -> String.sub s 0 (l-1), 20
4068 | 'g' -> String.sub s 0 (l-1), 30
4069 | _ -> s, 0
4070 else s, 0
4072 let n = int_of_string s1 in
4073 let m = n lsl shift in
4074 if m < 0 || m < n
4075 then raise (Failure "value too large")
4076 else m
4079 let string_with_suffix_of_int n =
4080 if n = 0
4081 then "0"
4082 else
4083 let n, s =
4084 if n land ((1 lsl 30) - 1) = 0
4085 then n lsr 30, "G"
4086 else (
4087 if n land ((1 lsl 20) - 1) = 0
4088 then n lsr 20, "M"
4089 else (
4090 if n land ((1 lsl 10) - 1) = 0
4091 then n lsr 10, "K"
4092 else n, ""
4096 let rec loop s n =
4097 let h = n mod 1000 in
4098 let n = n / 1000 in
4099 if n = 0
4100 then string_of_int h ^ s
4101 else (
4102 let s = Printf.sprintf "_%03d%s" h s in
4103 loop s n
4106 loop "" n ^ s;
4109 let defghyllscroll = (40, 8, 32);;
4110 let ghyllscroll_of_string s =
4111 let (n, a, b) as nab =
4112 if s = "default"
4113 then defghyllscroll
4114 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4116 if n <= a || n <= b || a >= b
4117 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4118 nab;
4121 let ghyllscroll_to_string ((n, a, b) as nab) =
4122 if nab = defghyllscroll
4123 then "default"
4124 else Printf.sprintf "%d,%d,%d" n a b;
4127 let describe_location () =
4128 let fn = page_of_y state.y in
4129 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4130 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4131 let percent =
4132 if maxy <= 0
4133 then 100.
4134 else (100. *. (float state.y /. float maxy))
4136 if fn = ln
4137 then
4138 Printf.sprintf "page %d of %d [%.2f%%]"
4139 (fn+1) state.pagecount percent
4140 else
4141 Printf.sprintf
4142 "pages %d-%d of %d [%.2f%%]"
4143 (fn+1) (ln+1) state.pagecount percent
4146 let setpresentationmode v =
4147 let n = page_of_y state.y in
4148 state.anchor <- (n, 0.0, 1.0);
4149 conf.presentation <- v;
4150 if conf.presentation
4151 then (
4152 if not conf.scrollbarinpm
4153 then state.scrollw <- 0;
4155 else state.scrollw <- conf.scrollbw;
4156 represent ();
4159 let enterinfomode =
4160 let btos b = if b then "\xe2\x88\x9a" else "" in
4161 let showextended = ref false in
4162 let leave mode = function
4163 | Confirm -> state.mode <- mode
4164 | Cancel -> state.mode <- mode in
4165 let src =
4166 (object
4167 val mutable m_first_time = true
4168 val mutable m_l = []
4169 val mutable m_a = [||]
4170 val mutable m_prev_uioh = nouioh
4171 val mutable m_prev_mode = View
4173 inherit lvsourcebase
4175 method reset prev_mode prev_uioh =
4176 m_a <- Array.of_list (List.rev m_l);
4177 m_l <- [];
4178 m_prev_mode <- prev_mode;
4179 m_prev_uioh <- prev_uioh;
4180 if m_first_time
4181 then (
4182 let rec loop n =
4183 if n >= Array.length m_a
4184 then ()
4185 else
4186 match m_a.(n) with
4187 | _, _, _, Action _ -> m_active <- n
4188 | _ -> loop (n+1)
4190 loop 0;
4191 m_first_time <- false;
4194 method int name get set =
4195 m_l <-
4196 (name, `int get, 1, Action (
4197 fun u ->
4198 let ondone s =
4199 try set (int_of_string s)
4200 with exn ->
4201 state.text <- Printf.sprintf "bad integer `%s': %s"
4202 s (exntos exn)
4204 state.text <- "";
4205 let te = name ^ ": ", "", None, intentry, ondone, true in
4206 state.mode <- Textentry (te, leave m_prev_mode);
4208 )) :: m_l
4210 method int_with_suffix name get set =
4211 m_l <-
4212 (name, `intws get, 1, Action (
4213 fun u ->
4214 let ondone s =
4215 try set (int_of_string_with_suffix s)
4216 with exn ->
4217 state.text <- Printf.sprintf "bad integer `%s': %s"
4218 s (exntos exn)
4220 state.text <- "";
4221 let te =
4222 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4224 state.mode <- Textentry (te, leave m_prev_mode);
4226 )) :: m_l
4228 method bool ?(offset=1) ?(btos=btos) name get set =
4229 m_l <-
4230 (name, `bool (btos, get), offset, Action (
4231 fun u ->
4232 let v = get () in
4233 set (not v);
4235 )) :: m_l
4237 method color name get set =
4238 m_l <-
4239 (name, `color get, 1, Action (
4240 fun u ->
4241 let invalid = (nan, nan, nan) in
4242 let ondone s =
4243 let c =
4244 try color_of_string s
4245 with exn ->
4246 state.text <- Printf.sprintf "bad color `%s': %s"
4247 s (exntos exn);
4248 invalid
4250 if c <> invalid
4251 then set c;
4253 let te = name ^ ": ", "", None, textentry, ondone, true in
4254 state.text <- color_to_string (get ());
4255 state.mode <- Textentry (te, leave m_prev_mode);
4257 )) :: m_l
4259 method string name get set =
4260 m_l <-
4261 (name, `string get, 1, Action (
4262 fun u ->
4263 let ondone s = set s in
4264 let te = name ^ ": ", "", None, textentry, ondone, true in
4265 state.mode <- Textentry (te, leave m_prev_mode);
4267 )) :: m_l
4269 method colorspace name get set =
4270 m_l <-
4271 (name, `string get, 1, Action (
4272 fun _ ->
4273 let source =
4274 let vals = [| "rgb"; "bgr"; "gray" |] in
4275 (object
4276 inherit lvsourcebase
4278 initializer
4279 m_active <- int_of_colorspace conf.colorspace;
4280 m_first <- 0;
4282 method getitemcount = Array.length vals
4283 method getitem n = (vals.(n), 0)
4284 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4285 ignore (uioh, first, pan, qsearch);
4286 if not cancel then set active;
4287 None
4288 method hasaction _ = true
4289 end)
4291 state.text <- "";
4292 let modehash = findkeyhash conf "info" in
4293 coe (new listview ~source ~trusted:true ~modehash)
4294 )) :: m_l
4296 method fitmodel name get set =
4297 m_l <-
4298 (name, `string get, 1, Action (
4299 fun _ ->
4300 let source =
4301 let vals = [| "fit width"; "proportional"; "fit page" |] in
4302 (object
4303 inherit lvsourcebase
4305 initializer
4306 m_active <- int_of_fitmodel conf.fitmodel;
4307 m_first <- 0;
4309 method getitemcount = Array.length vals
4310 method getitem n = (vals.(n), 0)
4311 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4312 ignore (uioh, first, pan, qsearch);
4313 if not cancel then set active;
4314 None
4315 method hasaction _ = true
4316 end)
4318 state.text <- "";
4319 let modehash = findkeyhash conf "info" in
4320 coe (new listview ~source ~trusted:true ~modehash)
4321 )) :: m_l
4323 method caption s offset =
4324 m_l <- (s, `empty, offset, Noaction) :: m_l
4326 method caption2 s f offset =
4327 m_l <- (s, `string f, offset, Noaction) :: m_l
4329 method getitemcount = Array.length m_a
4331 method getitem n =
4332 let tostr = function
4333 | `int f -> string_of_int (f ())
4334 | `intws f -> string_with_suffix_of_int (f ())
4335 | `string f -> f ()
4336 | `color f -> color_to_string (f ())
4337 | `bool (btos, f) -> btos (f ())
4338 | `empty -> ""
4340 let name, t, offset, _ = m_a.(n) in
4341 ((let s = tostr t in
4342 if String.length s > 0
4343 then Printf.sprintf "%s\t%s" name s
4344 else name),
4345 offset)
4347 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4348 let uiohopt =
4349 if not cancel
4350 then (
4351 m_qsearch <- qsearch;
4352 let uioh =
4353 match m_a.(active) with
4354 | _, _, _, Action f -> f uioh
4355 | _ -> uioh
4357 Some uioh
4359 else None
4361 m_active <- active;
4362 m_first <- first;
4363 m_pan <- pan;
4364 uiohopt
4366 method hasaction n =
4367 match m_a.(n) with
4368 | _, _, _, Action _ -> true
4369 | _ -> false
4370 end)
4372 let rec fillsrc prevmode prevuioh =
4373 let sep () = src#caption "" 0 in
4374 let colorp name get set =
4375 src#string name
4376 (fun () -> color_to_string (get ()))
4377 (fun v ->
4379 let c = color_of_string v in
4380 set c
4381 with exn ->
4382 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4385 let oldmode = state.mode in
4386 let birdseye = isbirdseye state.mode in
4388 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4390 src#bool "presentation mode"
4391 (fun () -> conf.presentation)
4392 (fun v -> setpresentationmode v);
4394 src#bool "ignore case in searches"
4395 (fun () -> conf.icase)
4396 (fun v -> conf.icase <- v);
4398 src#bool "preload"
4399 (fun () -> conf.preload)
4400 (fun v -> conf.preload <- v);
4402 src#bool "highlight links"
4403 (fun () -> conf.hlinks)
4404 (fun v -> conf.hlinks <- v);
4406 src#bool "under info"
4407 (fun () -> conf.underinfo)
4408 (fun v -> conf.underinfo <- v);
4410 src#bool "persistent bookmarks"
4411 (fun () -> conf.savebmarks)
4412 (fun v -> conf.savebmarks <- v);
4414 src#fitmodel "fit model"
4415 (fun () -> fitmodel_to_string conf.fitmodel)
4416 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4418 src#bool "trim margins"
4419 (fun () -> conf.trimmargins)
4420 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4422 src#bool "persistent location"
4423 (fun () -> conf.jumpback)
4424 (fun v -> conf.jumpback <- v);
4426 sep ();
4427 src#int "inter-page space"
4428 (fun () -> conf.interpagespace)
4429 (fun n ->
4430 conf.interpagespace <- n;
4431 docolumns conf.columns;
4432 let pageno, py =
4433 match state.layout with
4434 | [] -> 0, 0
4435 | l :: _ ->
4436 l.pageno, l.pagey
4438 state.maxy <- calcheight ();
4439 let y = getpagey pageno in
4440 gotoy (y + py)
4443 src#int "page bias"
4444 (fun () -> conf.pagebias)
4445 (fun v -> conf.pagebias <- v);
4447 src#int "scroll step"
4448 (fun () -> conf.scrollstep)
4449 (fun n -> conf.scrollstep <- n);
4451 src#int "horizontal scroll step"
4452 (fun () -> conf.hscrollstep)
4453 (fun v -> conf.hscrollstep <- v);
4455 src#int "auto scroll step"
4456 (fun () ->
4457 match state.autoscroll with
4458 | Some step -> step
4459 | _ -> conf.autoscrollstep)
4460 (fun n ->
4461 if state.autoscroll <> None
4462 then state.autoscroll <- Some n;
4463 conf.autoscrollstep <- n);
4465 src#int "zoom"
4466 (fun () -> truncate (conf.zoom *. 100.))
4467 (fun v -> setzoom ((float v) /. 100.));
4469 src#int "rotation"
4470 (fun () -> conf.angle)
4471 (fun v -> reqlayout v conf.fitmodel);
4473 src#int "scroll bar width"
4474 (fun () -> state.scrollw)
4475 (fun v ->
4476 state.scrollw <- v;
4477 conf.scrollbw <- v;
4478 reshape state.winw state.winh;
4481 src#int "scroll handle height"
4482 (fun () -> conf.scrollh)
4483 (fun v -> conf.scrollh <- v;);
4485 src#int "thumbnail width"
4486 (fun () -> conf.thumbw)
4487 (fun v ->
4488 conf.thumbw <- min 4096 v;
4489 match oldmode with
4490 | Birdseye beye ->
4491 leavebirdseye beye false;
4492 enterbirdseye ()
4493 | _ -> ()
4496 let mode = state.mode in
4497 src#string "columns"
4498 (fun () ->
4499 match conf.columns with
4500 | Csingle _ -> "1"
4501 | Cmulti (multi, _) -> multicolumns_to_string multi
4502 | Csplit (count, _) -> "-" ^ string_of_int count
4504 (fun v ->
4505 let n, a, b = multicolumns_of_string v in
4506 setcolumns mode n a b);
4508 sep ();
4509 src#caption "Presentation mode" 0;
4510 src#bool "scrollbar visible"
4511 (fun () -> conf.scrollbarinpm)
4512 (fun v ->
4513 if v != conf.scrollbarinpm
4514 then (
4515 conf.scrollbarinpm <- v;
4516 if conf.presentation
4517 then (
4518 state.scrollw <- if v then conf.scrollbw else 0;
4519 reshape state.winw state.winh;
4524 sep ();
4525 src#caption "Pixmap cache" 0;
4526 src#int_with_suffix "size (advisory)"
4527 (fun () -> conf.memlimit)
4528 (fun v -> conf.memlimit <- v);
4530 src#caption2 "used"
4531 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4532 (string_with_suffix_of_int state.memused)
4533 (Hashtbl.length state.tilemap)) 1;
4535 sep ();
4536 src#caption "Layout" 0;
4537 src#caption2 "Dimension"
4538 (fun () ->
4539 Printf.sprintf "%dx%d (virtual %dx%d)"
4540 state.winw state.winh
4541 state.w state.maxy)
4543 if conf.debug
4544 then
4545 src#caption2 "Position" (fun () ->
4546 Printf.sprintf "%dx%d" state.x state.y
4548 else
4549 src#caption2 "Position" (fun () -> describe_location ()) 1
4552 sep ();
4553 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4554 "Save these parameters as global defaults at exit"
4555 (fun () -> conf.bedefault)
4556 (fun v -> conf.bedefault <- v)
4559 sep ();
4560 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4561 src#bool ~offset:0 ~btos "Extended parameters"
4562 (fun () -> !showextended)
4563 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4564 if !showextended
4565 then (
4566 src#bool "checkers"
4567 (fun () -> conf.checkers)
4568 (fun v -> conf.checkers <- v; setcheckers v);
4569 src#bool "update cursor"
4570 (fun () -> conf.updatecurs)
4571 (fun v -> conf.updatecurs <- v);
4572 src#bool "verbose"
4573 (fun () -> conf.verbose)
4574 (fun v -> conf.verbose <- v);
4575 src#bool "invert colors"
4576 (fun () -> conf.invert)
4577 (fun v -> conf.invert <- v);
4578 src#bool "max fit"
4579 (fun () -> conf.maxhfit)
4580 (fun v -> conf.maxhfit <- v);
4581 src#bool "redirect stderr"
4582 (fun () -> conf.redirectstderr)
4583 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4584 src#string "uri launcher"
4585 (fun () -> conf.urilauncher)
4586 (fun v -> conf.urilauncher <- v);
4587 src#string "path launcher"
4588 (fun () -> conf.pathlauncher)
4589 (fun v -> conf.pathlauncher <- v);
4590 src#string "tile size"
4591 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4592 (fun v ->
4594 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4595 conf.tilew <- max 64 w;
4596 conf.tileh <- max 64 h;
4597 flushtiles ();
4598 with exn ->
4599 state.text <- Printf.sprintf "bad tile size `%s': %s"
4600 v (exntos exn)
4602 src#int "texture count"
4603 (fun () -> conf.texcount)
4604 (fun v ->
4605 if realloctexts v
4606 then conf.texcount <- v
4607 else showtext '!' " Failed to set texture count please retry later"
4609 src#int "slice height"
4610 (fun () -> conf.sliceheight)
4611 (fun v ->
4612 conf.sliceheight <- v;
4613 wcmd "sliceh %d" conf.sliceheight;
4615 src#int "anti-aliasing level"
4616 (fun () -> conf.aalevel)
4617 (fun v ->
4618 conf.aalevel <- bound v 0 8;
4619 state.anchor <- getanchor ();
4620 opendoc state.path state.password;
4622 src#string "page scroll scaling factor"
4623 (fun () -> string_of_float conf.pgscale)
4624 (fun v ->
4626 let s = float_of_string v in
4627 conf.pgscale <- s
4628 with exn ->
4629 state.text <- Printf.sprintf
4630 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4633 src#int "ui font size"
4634 (fun () -> fstate.fontsize)
4635 (fun v -> setfontsize (bound v 5 100));
4636 src#int "hint font size"
4637 (fun () -> conf.hfsize)
4638 (fun v -> conf.hfsize <- bound v 5 100);
4639 colorp "background color"
4640 (fun () -> conf.bgcolor)
4641 (fun v -> conf.bgcolor <- v);
4642 src#bool "crop hack"
4643 (fun () -> conf.crophack)
4644 (fun v -> conf.crophack <- v);
4645 src#string "trim fuzz"
4646 (fun () -> irect_to_string conf.trimfuzz)
4647 (fun v ->
4649 conf.trimfuzz <- irect_of_string v;
4650 if conf.trimmargins
4651 then settrim true conf.trimfuzz;
4652 with exn ->
4653 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4655 src#string "throttle"
4656 (fun () ->
4657 match conf.maxwait with
4658 | None -> "show place holder if page is not ready"
4659 | Some time ->
4660 if time = infinity
4661 then "wait for page to fully render"
4662 else
4663 "wait " ^ string_of_float time
4664 ^ " seconds before showing placeholder"
4666 (fun v ->
4668 let f = float_of_string v in
4669 if f <= 0.0
4670 then conf.maxwait <- None
4671 else conf.maxwait <- Some f
4672 with exn ->
4673 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4675 src#string "ghyll scroll"
4676 (fun () ->
4677 match conf.ghyllscroll with
4678 | None -> ""
4679 | Some nab -> ghyllscroll_to_string nab
4681 (fun v ->
4683 let gs =
4684 if String.length v = 0
4685 then None
4686 else Some (ghyllscroll_of_string v)
4688 conf.ghyllscroll <- gs
4689 with exn ->
4690 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4692 src#string "selection command"
4693 (fun () -> conf.selcmd)
4694 (fun v -> conf.selcmd <- v);
4695 src#string "synctex command"
4696 (fun () -> conf.stcmd)
4697 (fun v -> conf.stcmd <- v);
4698 src#colorspace "color space"
4699 (fun () -> colorspace_to_string conf.colorspace)
4700 (fun v ->
4701 conf.colorspace <- colorspace_of_int v;
4702 wcmd "cs %d" v;
4703 load state.layout;
4705 if pbousable ()
4706 then
4707 src#bool "use PBO"
4708 (fun () -> conf.usepbo)
4709 (fun v -> conf.usepbo <- v);
4710 src#bool "mouse wheel scrolls pages"
4711 (fun () -> conf.wheelbypage)
4712 (fun v -> conf.wheelbypage <- v);
4715 sep ();
4716 src#caption "Document" 0;
4717 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4718 src#caption2 "Pages"
4719 (fun () -> string_of_int state.pagecount) 1;
4720 src#caption2 "Dimensions"
4721 (fun () -> string_of_int (List.length state.pdims)) 1;
4722 if conf.trimmargins
4723 then (
4724 sep ();
4725 src#caption "Trimmed margins" 0;
4726 src#caption2 "Dimensions"
4727 (fun () -> string_of_int (List.length state.pdims)) 1;
4730 sep ();
4731 src#caption "OpenGL" 0;
4732 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4733 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4735 sep ();
4736 src#caption "Location" 0;
4737 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4738 if String.length state.origin > 0
4739 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4741 src#reset prevmode prevuioh;
4743 fun () ->
4744 state.text <- "";
4745 let prevmode = state.mode
4746 and prevuioh = state.uioh in
4747 fillsrc prevmode prevuioh;
4748 let source = (src :> lvsource) in
4749 let modehash = findkeyhash conf "info" in
4750 state.uioh <- coe (object (self)
4751 inherit listview ~source ~trusted:true ~modehash as super
4752 val mutable m_prevmemused = 0
4753 method infochanged = function
4754 | Memused ->
4755 if m_prevmemused != state.memused
4756 then (
4757 m_prevmemused <- state.memused;
4758 G.postRedisplay "memusedchanged";
4760 | Pdim -> G.postRedisplay "pdimchanged"
4761 | Docinfo -> fillsrc prevmode prevuioh
4763 method key key mask =
4764 if not (Wsi.withctrl mask)
4765 then
4766 match key with
4767 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4768 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4769 | _ -> super#key key mask
4770 else super#key key mask
4771 end);
4772 G.postRedisplay "info";
4775 let enterhelpmode =
4776 let source =
4777 (object
4778 inherit lvsourcebase
4779 method getitemcount = Array.length state.help
4780 method getitem n =
4781 let s, l, _ = state.help.(n) in
4782 (s, l)
4784 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4785 let optuioh =
4786 if not cancel
4787 then (
4788 m_qsearch <- qsearch;
4789 match state.help.(active) with
4790 | _, _, Action f -> Some (f uioh)
4791 | _ -> Some (uioh)
4793 else None
4795 m_active <- active;
4796 m_first <- first;
4797 m_pan <- pan;
4798 optuioh
4800 method hasaction n =
4801 match state.help.(n) with
4802 | _, _, Action _ -> true
4803 | _ -> false
4805 initializer
4806 m_active <- -1
4807 end)
4808 in fun () ->
4809 let modehash = findkeyhash conf "help" in
4810 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4811 G.postRedisplay "help";
4814 let entermsgsmode =
4815 let msgsource =
4816 let re = Str.regexp "[\r\n]" in
4817 (object
4818 inherit lvsourcebase
4819 val mutable m_items = [||]
4821 method getitemcount = 1 + Array.length m_items
4823 method getitem n =
4824 if n = 0
4825 then "[Clear]", 0
4826 else m_items.(n-1), 0
4828 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4829 ignore uioh;
4830 if not cancel
4831 then (
4832 if active = 0
4833 then Buffer.clear state.errmsgs;
4834 m_qsearch <- qsearch;
4836 m_active <- active;
4837 m_first <- first;
4838 m_pan <- pan;
4839 None
4841 method hasaction n =
4842 n = 0
4844 method reset =
4845 state.newerrmsgs <- false;
4846 let l = Str.split re (Buffer.contents state.errmsgs) in
4847 m_items <- Array.of_list l
4849 initializer
4850 m_active <- 0
4851 end)
4852 in fun () ->
4853 state.text <- "";
4854 msgsource#reset;
4855 let source = (msgsource :> lvsource) in
4856 let modehash = findkeyhash conf "listview" in
4857 state.uioh <- coe (object
4858 inherit listview ~source ~trusted:false ~modehash as super
4859 method display =
4860 if state.newerrmsgs
4861 then msgsource#reset;
4862 super#display
4863 end);
4864 G.postRedisplay "msgs";
4867 let quickbookmark ?title () =
4868 match state.layout with
4869 | [] -> ()
4870 | l :: _ ->
4871 let title =
4872 match title with
4873 | None ->
4874 let sec = Unix.gettimeofday () in
4875 let tm = Unix.localtime sec in
4876 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4877 (l.pageno+1)
4878 tm.Unix.tm_mday
4879 tm.Unix.tm_mon
4880 (tm.Unix.tm_year + 1900)
4881 tm.Unix.tm_hour
4882 tm.Unix.tm_min
4883 | Some title -> title
4885 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4888 let setautoscrollspeed step goingdown =
4889 let incr = max 1 ((abs step) / 2) in
4890 let incr = if goingdown then incr else -incr in
4891 let astep = step + incr in
4892 state.autoscroll <- Some astep;
4895 let gotounder = function
4896 | Ulinkgoto (pageno, top) ->
4897 if pageno >= 0
4898 then (
4899 addnav ();
4900 gotopage1 pageno top;
4903 | Ulinkuri s ->
4904 gotouri s
4906 | Uremote (filename, pageno) ->
4907 let path =
4908 if Sys.file_exists filename
4909 then filename
4910 else
4911 let dir = Filename.dirname state.path in
4912 let path = Filename.concat dir filename in
4913 if Sys.file_exists path
4914 then path
4915 else ""
4917 if String.length path > 0
4918 then (
4919 let anchor = getanchor () in
4920 let ranchor = state.path, state.password, anchor, state.origin in
4921 state.origin <- "";
4922 state.anchor <- (pageno, 0.0, 0.0);
4923 state.ranchors <- ranchor :: state.ranchors;
4924 opendoc path "";
4926 else showtext '!' ("Could not find " ^ filename)
4928 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4931 let canpan () =
4932 match conf.columns with
4933 | Csplit _ -> true
4934 | _ -> state.x != 0 || conf.zoom > 1.0
4937 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4939 let existsinrow pageno (columns, coverA, coverB) p =
4940 let last = ((pageno - coverA) mod columns) + columns in
4941 let rec any = function
4942 | [] -> false
4943 | l :: rest ->
4944 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4945 then p l
4946 else (
4947 if not (p l)
4948 then (if l.pageno = last then false else any rest)
4949 else true
4952 any state.layout
4955 let nextpage () =
4956 match state.layout with
4957 | [] ->
4958 let pageno = page_of_y state.y in
4959 gotoghyll (getpagey (pageno+1))
4960 | l :: rest ->
4961 match conf.columns with
4962 | Csingle _ ->
4963 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4964 then
4965 let y = clamp (pgscale state.winh) in
4966 gotoghyll y
4967 else
4968 let pageno = min (l.pageno+1) (state.pagecount-1) in
4969 gotoghyll (getpagey pageno)
4970 | Cmulti ((c, _, _) as cl, _) ->
4971 if conf.presentation
4972 && (existsinrow l.pageno cl
4973 (fun l -> l.pageh > l.pagey + l.pagevh))
4974 then
4975 let y = clamp (pgscale state.winh) in
4976 gotoghyll y
4977 else
4978 let pageno = min (l.pageno+c) (state.pagecount-1) in
4979 gotoghyll (getpagey pageno)
4980 | Csplit (n, _) ->
4981 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4982 then
4983 let pagey, pageh = getpageyh l.pageno in
4984 let pagey = pagey + pageh * l.pagecol in
4985 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4986 gotoghyll (pagey + pageh + ips)
4989 let prevpage () =
4990 match state.layout with
4991 | [] ->
4992 let pageno = page_of_y state.y in
4993 gotoghyll (getpagey (pageno-1))
4994 | l :: _ ->
4995 match conf.columns with
4996 | Csingle _ ->
4997 if conf.presentation && l.pagey != 0
4998 then
4999 gotoghyll (clamp (pgscale ~-(state.winh)))
5000 else
5001 let pageno = max 0 (l.pageno-1) in
5002 gotoghyll (getpagey pageno)
5003 | Cmulti ((c, _, coverB) as cl, _) ->
5004 if conf.presentation &&
5005 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5006 then
5007 gotoghyll (clamp (pgscale ~-(state.winh)))
5008 else
5009 let decr =
5010 if l.pageno = state.pagecount - coverB
5011 then 1
5012 else c
5014 let pageno = max 0 (l.pageno-decr) in
5015 gotoghyll (getpagey pageno)
5016 | Csplit (n, _) ->
5017 let y =
5018 if l.pagecol = 0
5019 then
5020 if l.pageno = 0
5021 then l.pagey
5022 else
5023 let pageno = max 0 (l.pageno-1) in
5024 let pagey, pageh = getpageyh pageno in
5025 pagey + (n-1)*pageh
5026 else
5027 let pagey, pageh = getpageyh l.pageno in
5028 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5030 gotoghyll y
5033 let viewkeyboard key mask =
5034 let enttext te =
5035 let mode = state.mode in
5036 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5037 state.text <- "";
5038 enttext ();
5039 G.postRedisplay "view:enttext"
5041 let ctrl = Wsi.withctrl mask in
5042 let key =
5043 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5045 match key with
5046 | 81 -> (* Q *)
5047 exit 0
5049 | 0xff63 -> (* insert *)
5050 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5051 then (
5052 state.mode <- LinkNav (Ltgendir 0);
5053 gotoy state.y;
5055 else showtext '!' "Keyboard link navigation does not work under rotation"
5057 | 0xff1b | 113 -> (* escape / q *)
5058 begin match state.mstate with
5059 | Mzoomrect _ ->
5060 state.mstate <- Mnone;
5061 Wsi.setcursor Wsi.CURSOR_INHERIT;
5062 G.postRedisplay "kill zoom rect";
5063 | _ ->
5064 begin match state.mode with
5065 | LinkNav _ ->
5066 state.mode <- View;
5067 G.postRedisplay "esc leave linknav"
5068 | _ ->
5069 match state.ranchors with
5070 | [] -> raise Quit
5071 | (path, password, anchor, origin) :: rest ->
5072 state.ranchors <- rest;
5073 state.anchor <- anchor;
5074 state.origin <- origin;
5075 opendoc path password
5076 end;
5077 end;
5079 | 0xff08 -> (* backspace *)
5080 gotoghyll (getnav ~-1)
5082 | 111 -> (* o *)
5083 enteroutlinemode ()
5085 | 117 -> (* u *)
5086 state.rects <- [];
5087 state.text <- "";
5088 G.postRedisplay "dehighlight";
5090 | 47 | 63 -> (* / ? *)
5091 let ondone isforw s =
5092 cbput state.hists.pat s;
5093 state.searchpattern <- s;
5094 search s isforw
5096 let s = String.create 1 in
5097 s.[0] <- Char.chr key;
5098 enttext (s, "", Some (onhist state.hists.pat),
5099 textentry, ondone (key = 47), true)
5101 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5102 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5103 setzoom (conf.zoom +. incr)
5105 | 43 | 0xffab -> (* + *)
5106 let ondone s =
5107 let n =
5108 try int_of_string s with exc ->
5109 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5110 max_int
5112 if n != max_int
5113 then (
5114 conf.pagebias <- n;
5115 state.text <- "page bias is now " ^ string_of_int n;
5118 enttext ("page bias: ", "", None, intentry, ondone, true)
5120 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5121 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5122 setzoom (max 0.01 (conf.zoom -. decr))
5124 | 45 | 0xffad -> (* - *)
5125 let ondone msg = state.text <- msg in
5126 enttext (
5127 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5128 optentry state.mode, ondone, true
5131 | 48 when ctrl -> (* ctrl-0 *)
5132 if conf.zoom = 1.0
5133 then (
5134 state.x <- 0;
5135 state.hscrollh <-
5136 if state.w <= state.winw - state.scrollw
5137 then 0
5138 else state.scrollw
5140 gotoy state.y
5142 else setzoom 1.0
5144 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5145 let cols =
5146 match conf.columns with
5147 | Csingle _ | Cmulti _ -> 1
5148 | Csplit (n, _) -> n
5150 let h = state.winh -
5151 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5153 let zoom = zoomforh state.winw h state.scrollw cols in
5154 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5155 then setzoom zoom
5157 | 51 when ctrl -> (* ctrl-3 *)
5158 let fm =
5159 match conf.fitmodel with
5160 | FitWidth -> FitProportional
5161 | FitProportional -> FitPage
5162 | FitPage -> FitWidth
5164 state.text <- "fit model: " ^ fitmodel_to_string fm;
5165 reqlayout conf.angle fm
5167 | 0xffc6 -> (* f9 *)
5168 togglebirdseye ()
5170 | 57 when ctrl -> (* ctrl-9 *)
5171 togglebirdseye ()
5173 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5174 when not ctrl -> (* 0..9 *)
5175 let ondone s =
5176 let n =
5177 try int_of_string s with exc ->
5178 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5181 if n >= 0
5182 then (
5183 addnav ();
5184 cbput state.hists.pag (string_of_int n);
5185 gotopage1 (n + conf.pagebias - 1) 0;
5188 let pageentry text key =
5189 match Char.unsafe_chr key with
5190 | 'g' -> TEdone text
5191 | _ -> intentry text key
5193 let text = "x" in text.[0] <- Char.chr key;
5194 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5196 | 98 -> (* b *)
5197 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5198 reshape state.winw state.winh;
5200 | 108 -> (* l *)
5201 conf.hlinks <- not conf.hlinks;
5202 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5203 G.postRedisplay "toggle highlightlinks";
5205 | 70 -> (* F *)
5206 state.glinks <- true;
5207 let mode = state.mode in
5208 state.mode <- Textentry (
5209 (":", "", None, linknentry, linkndone gotounder, false),
5210 (fun _ ->
5211 state.glinks <- false;
5212 state.mode <- mode)
5214 state.text <- "";
5215 G.postRedisplay "view:linkent(F)"
5217 | 121 -> (* y *)
5218 state.glinks <- true;
5219 let mode = state.mode in
5220 state.mode <- Textentry (
5222 ":", "", None, linknentry, linkndone (fun under ->
5223 selstring (undertext under);
5224 ), false
5226 fun _ ->
5227 state.glinks <- false;
5228 state.mode <- mode
5230 state.text <- "";
5231 G.postRedisplay "view:linkent"
5233 | 97 -> (* a *)
5234 begin match state.autoscroll with
5235 | Some step ->
5236 conf.autoscrollstep <- step;
5237 state.autoscroll <- None
5238 | None ->
5239 if conf.autoscrollstep = 0
5240 then state.autoscroll <- Some 1
5241 else state.autoscroll <- Some conf.autoscrollstep
5244 | 112 when ctrl -> (* ctrl-p *)
5245 launchpath ()
5247 | 80 -> (* P *)
5248 setpresentationmode (not conf.presentation);
5249 showtext ' ' ("presentation mode " ^
5250 if conf.presentation then "on" else "off");
5252 | 102 -> (* f *)
5253 if List.mem Wsi.Fullscreen state.winstate
5254 then Wsi.reshape conf.cwinw conf.cwinh
5255 else Wsi.fullscreen ()
5257 | 112 | 78 -> (* p|N *)
5258 search state.searchpattern false
5260 | 110 | 0xffc0 -> (* n|F3 *)
5261 search state.searchpattern true
5263 | 116 -> (* t *)
5264 begin match state.layout with
5265 | [] -> ()
5266 | l :: _ ->
5267 gotoghyll (getpagey l.pageno)
5270 | 32 -> (* space *)
5271 nextpage ()
5273 | 0xff9f | 0xffff -> (* delete *)
5274 prevpage ()
5276 | 61 -> (* = *)
5277 showtext ' ' (describe_location ());
5279 | 119 -> (* w *)
5280 begin match state.layout with
5281 | [] -> ()
5282 | l :: _ ->
5283 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5284 G.postRedisplay "w"
5287 | 39 -> (* ' *)
5288 enterbookmarkmode ()
5290 | 104 | 0xffbe -> (* h|F1 *)
5291 enterhelpmode ()
5293 | 105 -> (* i *)
5294 enterinfomode ()
5296 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5297 entermsgsmode ()
5299 | 109 -> (* m *)
5300 let ondone s =
5301 match state.layout with
5302 | l :: _ ->
5303 if String.length s > 0
5304 then
5305 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5306 | _ -> ()
5308 enttext ("bookmark: ", "", None, textentry, ondone, true)
5310 | 126 -> (* ~ *)
5311 quickbookmark ();
5312 showtext ' ' "Quick bookmark added";
5314 | 122 -> (* z *)
5315 begin match state.layout with
5316 | l :: _ ->
5317 let rect = getpdimrect l.pagedimno in
5318 let w, h =
5319 if conf.crophack
5320 then
5321 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5322 truncate (1.2 *. (rect.(3) -. rect.(0))))
5323 else
5324 (truncate (rect.(1) -. rect.(0)),
5325 truncate (rect.(3) -. rect.(0)))
5327 let w = truncate ((float w)*.conf.zoom)
5328 and h = truncate ((float h)*.conf.zoom) in
5329 if w != 0 && h != 0
5330 then (
5331 state.anchor <- getanchor ();
5332 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5334 G.postRedisplay "z";
5336 | [] -> ()
5339 | 60 | 62 -> (* < > *)
5340 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5342 | 91 | 93 -> (* [ ] *)
5343 conf.colorscale <-
5344 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5346 G.postRedisplay "brightness";
5348 | 99 when state.mode = View -> (* c *)
5349 let (c, a, b), z =
5350 match state.prevcolumns with
5351 | None -> (1, 0, 0), 1.0
5352 | Some (columns, z) ->
5353 let cab =
5354 match columns with
5355 | Csplit (c, _) -> -c, 0, 0
5356 | Cmulti ((c, a, b), _) -> c, a, b
5357 | Csingle _ -> 1, 0, 0
5359 cab, z
5361 setcolumns View c a b;
5362 setzoom z;
5364 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5365 setzoom state.prevzoom
5367 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5368 begin match state.autoscroll with
5369 | None ->
5370 begin match state.mode with
5371 | Birdseye beye -> upbirdseye 1 beye
5372 | _ ->
5373 if ctrl
5374 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5375 else (
5376 if not (Wsi.withshift mask) && conf.presentation
5377 then prevpage ()
5378 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5381 | Some n ->
5382 setautoscrollspeed n false
5385 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5386 begin match state.autoscroll with
5387 | None ->
5388 begin match state.mode with
5389 | Birdseye beye -> downbirdseye 1 beye
5390 | _ ->
5391 if ctrl
5392 then gotoy_and_clear_text (clamp (state.winh/2))
5393 else (
5394 if not (Wsi.withshift mask) && conf.presentation
5395 then nextpage ()
5396 else gotoy_and_clear_text (clamp conf.scrollstep)
5399 | Some n ->
5400 setautoscrollspeed n true
5403 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5404 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5405 if canpan ()
5406 then
5407 let dx =
5408 if ctrl
5409 then state.winw / 2
5410 else conf.hscrollstep
5412 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5413 state.x <- panbound (state.x + dx);
5414 gotoy_and_clear_text state.y
5415 else (
5416 state.text <- "";
5417 G.postRedisplay "left/right"
5420 | 0xff55 | 0xff9a -> (* (kp) prior *)
5421 let y =
5422 if ctrl
5423 then
5424 match state.layout with
5425 | [] -> state.y
5426 | l :: _ -> state.y - l.pagey
5427 else
5428 clamp (pgscale (-state.winh))
5430 gotoghyll y
5432 | 0xff56 | 0xff9b -> (* (kp) next *)
5433 let y =
5434 if ctrl
5435 then
5436 match List.rev state.layout with
5437 | [] -> state.y
5438 | l :: _ -> getpagey l.pageno
5439 else
5440 clamp (pgscale state.winh)
5442 gotoghyll y
5444 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5445 gotoghyll 0
5446 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5447 gotoghyll (clamp state.maxy)
5449 | 0xff53 | 0xff98
5450 when Wsi.withalt mask -> (* alt-(kp) right *)
5451 gotoghyll (getnav 1)
5452 | 0xff51 | 0xff96
5453 when Wsi.withalt mask -> (* alt-(kp) left *)
5454 gotoghyll (getnav ~-1)
5456 | 114 -> (* r *)
5457 reload ()
5459 | 118 when conf.debug -> (* v *)
5460 state.rects <- [];
5461 List.iter (fun l ->
5462 match getopaque l.pageno with
5463 | None -> ()
5464 | Some opaque ->
5465 let x0, y0, x1, y1 = pagebbox opaque in
5466 let a,b = float x0, float y0 in
5467 let c,d = float x1, float y0 in
5468 let e,f = float x1, float y1 in
5469 let h,j = float x0, float y1 in
5470 let rect = (a,b,c,d,e,f,h,j) in
5471 debugrect rect;
5472 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5473 ) state.layout;
5474 G.postRedisplay "v";
5476 | _ ->
5477 vlog "huh? %s" (Wsi.keyname key)
5480 let linknavkeyboard key mask linknav =
5481 let getpage pageno =
5482 let rec loop = function
5483 | [] -> None
5484 | l :: _ when l.pageno = pageno -> Some l
5485 | _ :: rest -> loop rest
5486 in loop state.layout
5488 let doexact (pageno, n) =
5489 match getopaque pageno, getpage pageno with
5490 | Some opaque, Some l ->
5491 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5492 then
5493 let under = getlink opaque n in
5494 G.postRedisplay "link gotounder";
5495 gotounder under;
5496 state.mode <- View;
5497 else
5498 let opt, dir =
5499 match key with
5500 | 0xff50 -> (* home *)
5501 Some (findlink opaque LDfirst), -1
5503 | 0xff57 -> (* end *)
5504 Some (findlink opaque LDlast), 1
5506 | 0xff51 -> (* left *)
5507 Some (findlink opaque (LDleft n)), -1
5509 | 0xff53 -> (* right *)
5510 Some (findlink opaque (LDright n)), 1
5512 | 0xff52 -> (* up *)
5513 Some (findlink opaque (LDup n)), -1
5515 | 0xff54 -> (* down *)
5516 Some (findlink opaque (LDdown n)), 1
5518 | _ -> None, 0
5520 let pwl l dir =
5521 begin match findpwl l.pageno dir with
5522 | Pwlnotfound -> ()
5523 | Pwl pageno ->
5524 let notfound dir =
5525 state.mode <- LinkNav (Ltgendir dir);
5526 let y, h = getpageyh pageno in
5527 let y =
5528 if dir < 0
5529 then y + h - state.winh
5530 else y
5532 gotoy y
5534 begin match getopaque pageno, getpage pageno with
5535 | Some opaque, Some _ ->
5536 let link =
5537 let ld = if dir > 0 then LDfirst else LDlast in
5538 findlink opaque ld
5540 begin match link with
5541 | Lfound m ->
5542 showlinktype (getlink opaque m);
5543 state.mode <- LinkNav (Ltexact (pageno, m));
5544 G.postRedisplay "linknav jpage";
5545 | _ -> notfound dir
5546 end;
5547 | _ -> notfound dir
5548 end;
5549 end;
5551 begin match opt with
5552 | Some Lnotfound -> pwl l dir;
5553 | Some (Lfound m) ->
5554 if m = n
5555 then pwl l dir
5556 else (
5557 let _, y0, _, y1 = getlinkrect opaque m in
5558 if y0 < l.pagey
5559 then gotopage1 l.pageno y0
5560 else (
5561 let d = fstate.fontsize + 1 in
5562 if y1 - l.pagey > l.pagevh - d
5563 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5564 else G.postRedisplay "linknav";
5566 showlinktype (getlink opaque m);
5567 state.mode <- LinkNav (Ltexact (l.pageno, m));
5570 | None -> viewkeyboard key mask
5571 end;
5572 | _ -> viewkeyboard key mask
5574 if key = 0xff63
5575 then (
5576 state.mode <- View;
5577 G.postRedisplay "leave linknav"
5579 else
5580 match linknav with
5581 | Ltgendir _ -> viewkeyboard key mask
5582 | Ltexact exact -> doexact exact
5585 let keyboard key mask =
5586 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5587 then wcmd "interrupt"
5588 else state.uioh <- state.uioh#key key mask
5591 let birdseyekeyboard key mask
5592 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5593 let incr =
5594 match conf.columns with
5595 | Csingle _ -> 1
5596 | Cmulti ((c, _, _), _) -> c
5597 | Csplit _ -> failwith "bird's eye split mode"
5599 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5600 match key with
5601 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5602 let y, h = getpageyh pageno in
5603 let top = (state.winh - h) / 2 in
5604 gotoy (max 0 (y - top))
5605 | 0xff0d (* enter *)
5606 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5607 | 0xff1b -> leavebirdseye beye true (* escape *)
5608 | 0xff52 -> upbirdseye incr beye (* up *)
5609 | 0xff54 -> downbirdseye incr beye (* down *)
5610 | 0xff51 -> upbirdseye 1 beye (* left *)
5611 | 0xff53 -> downbirdseye 1 beye (* right *)
5613 | 0xff55 -> (* prior *)
5614 begin match state.layout with
5615 | l :: _ ->
5616 if l.pagey != 0
5617 then (
5618 state.mode <- Birdseye (
5619 oconf, leftx, l.pageno, hooverpageno, anchor
5621 gotopage1 l.pageno 0;
5623 else (
5624 let layout = layout (state.y-state.winh) (pgh state.layout) in
5625 match layout with
5626 | [] -> gotoy (clamp (-state.winh))
5627 | l :: _ ->
5628 state.mode <- Birdseye (
5629 oconf, leftx, l.pageno, hooverpageno, anchor
5631 gotopage1 l.pageno 0
5634 | [] -> gotoy (clamp (-state.winh))
5635 end;
5637 | 0xff56 -> (* next *)
5638 begin match List.rev state.layout with
5639 | l :: _ ->
5640 let layout = layout (state.y + (pgh state.layout)) state.winh in
5641 begin match layout with
5642 | [] ->
5643 let incr = l.pageh - l.pagevh in
5644 if incr = 0
5645 then (
5646 state.mode <-
5647 Birdseye (
5648 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5650 G.postRedisplay "birdseye pagedown";
5652 else gotoy (clamp (incr + conf.interpagespace*2));
5654 | l :: _ ->
5655 state.mode <-
5656 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5657 gotopage1 l.pageno 0;
5660 | [] -> gotoy (clamp state.winh)
5661 end;
5663 | 0xff50 -> (* home *)
5664 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5665 gotopage1 0 0
5667 | 0xff57 -> (* end *)
5668 let pageno = state.pagecount - 1 in
5669 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5670 if not (pagevisible state.layout pageno)
5671 then
5672 let h =
5673 match List.rev state.pdims with
5674 | [] -> state.winh
5675 | (_, _, h, _) :: _ -> h
5677 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5678 else G.postRedisplay "birdseye end";
5679 | _ -> viewkeyboard key mask
5682 let drawpage l =
5683 let color =
5684 match state.mode with
5685 | Textentry _ -> scalecolor 0.4
5686 | LinkNav _
5687 | View -> scalecolor 1.0
5688 | Birdseye (_, _, pageno, hooverpageno, _) ->
5689 if l.pageno = hooverpageno
5690 then scalecolor 0.9
5691 else (
5692 if l.pageno = pageno
5693 then scalecolor 1.0
5694 else scalecolor 0.8
5697 drawtiles l color;
5700 let postdrawpage l linkindexbase =
5701 match getopaque l.pageno with
5702 | Some opaque ->
5703 if tileready l l.pagex l.pagey
5704 then
5705 let x = l.pagedispx - l.pagex
5706 and y = l.pagedispy - l.pagey in
5707 let hlmask =
5708 match conf.columns with
5709 | Csingle _ | Cmulti _ ->
5710 (if conf.hlinks then 1 else 0)
5711 + (if state.glinks
5712 && not (isbirdseye state.mode) then 2 else 0)
5713 | _ -> 0
5715 let s =
5716 match state.mode with
5717 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5718 | _ -> ""
5720 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5721 else 0
5722 | _ -> 0
5725 let scrollindicator () =
5726 let sbw, ph, sh = state.uioh#scrollph in
5727 let sbh, pw, sw = state.uioh#scrollpw in
5729 GlDraw.color (0.64, 0.64, 0.64);
5730 GlDraw.rect
5731 (float (state.winw - sbw), 0.)
5732 (float state.winw, float state.winh)
5734 GlDraw.rect
5735 (0., float (state.winh - sbh))
5736 (float (state.winw - state.scrollw - 1), float state.winh)
5738 GlDraw.color (0.0, 0.0, 0.0);
5740 GlDraw.rect
5741 (float (state.winw - sbw), ph)
5742 (float state.winw, ph +. sh)
5744 GlDraw.rect
5745 (pw, float (state.winh - sbh))
5746 (pw +. sw, float state.winh)
5750 let showsel () =
5751 match state.mstate with
5752 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5755 | Msel ((x0, y0), (x1, y1)) ->
5756 let rec loop = function
5757 | l :: ls ->
5758 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5759 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5760 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5761 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5762 then
5763 match getopaque l.pageno with
5764 | Some opaque ->
5765 let x0, y0 = pagetranslatepoint l x0 y0 in
5766 let x1, y1 = pagetranslatepoint l x1 y1 in
5767 seltext opaque (x0, y0, x1, y1);
5768 | _ -> ()
5769 else loop ls
5770 | [] -> ()
5772 loop state.layout
5775 let showrects rects =
5776 Gl.enable `blend;
5777 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5778 GlDraw.polygon_mode `both `fill;
5779 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5780 List.iter
5781 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5782 List.iter (fun l ->
5783 if l.pageno = pageno
5784 then (
5785 let dx = float (l.pagedispx - l.pagex) in
5786 let dy = float (l.pagedispy - l.pagey) in
5787 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5788 GlDraw.begins `quads;
5790 GlDraw.vertex2 (x0+.dx, y0+.dy);
5791 GlDraw.vertex2 (x1+.dx, y1+.dy);
5792 GlDraw.vertex2 (x2+.dx, y2+.dy);
5793 GlDraw.vertex2 (x3+.dx, y3+.dy);
5795 GlDraw.ends ();
5797 ) state.layout
5798 ) rects
5800 Gl.disable `blend;
5803 let display () =
5804 GlClear.color (scalecolor2 conf.bgcolor);
5805 GlClear.clear [`color];
5806 List.iter drawpage state.layout;
5807 let rects =
5808 match state.mode with
5809 | LinkNav (Ltexact (pageno, linkno)) ->
5810 begin match getopaque pageno with
5811 | Some opaque ->
5812 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5813 (pageno, 5, (
5814 float x0, float y0,
5815 float x1, float y0,
5816 float x1, float y1,
5817 float x0, float y1)
5818 ) :: state.rects
5819 | None -> state.rects
5821 | _ -> state.rects
5823 showrects rects;
5824 let rec postloop linkindexbase = function
5825 | l :: rest ->
5826 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5827 postloop linkindexbase rest
5828 | [] -> ()
5830 showsel ();
5831 postloop 0 state.layout;
5832 state.uioh#display;
5833 begin match state.mstate with
5834 | Mzoomrect ((x0, y0), (x1, y1)) ->
5835 Gl.enable `blend;
5836 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5837 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5838 GlDraw.rect (float x0, float y0)
5839 (float x1, float y1);
5840 Gl.disable `blend;
5841 | _ -> ()
5842 end;
5843 enttext ();
5844 scrollindicator ();
5845 Wsi.swapb ();
5848 let zoomrect x y x1 y1 =
5849 let x0 = min x x1
5850 and x1 = max x x1
5851 and y0 = min y y1 in
5852 gotoy (state.y + y0);
5853 state.anchor <- getanchor ();
5854 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5855 let margin =
5856 if state.w < state.winw - state.scrollw
5857 then (state.winw - state.scrollw - state.w) / 2
5858 else 0
5860 state.x <- (state.x + margin) - x0;
5861 setzoom zoom;
5862 Wsi.setcursor Wsi.CURSOR_INHERIT;
5863 state.mstate <- Mnone;
5866 let scrollx x =
5867 let winw = state.winw - state.scrollw - 1 in
5868 let s = float x /. float winw in
5869 let destx = truncate (float (state.w + winw) *. s) in
5870 state.x <- winw - destx;
5871 gotoy_and_clear_text state.y;
5872 state.mstate <- Mscrollx;
5875 let scrolly y =
5876 let s = float y /. float state.winh in
5877 let desty = truncate (float (state.maxy - state.winh) *. s) in
5878 gotoy_and_clear_text desty;
5879 state.mstate <- Mscrolly;
5882 let viewmouse button down x y mask =
5883 match button with
5884 | n when (n == 4 || n == 5) && not down ->
5885 if Wsi.withctrl mask
5886 then (
5887 match state.mstate with
5888 | Mzoom (oldn, i) ->
5889 if oldn = n
5890 then (
5891 if i = 2
5892 then
5893 let incr =
5894 match n with
5895 | 5 ->
5896 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5897 | _ ->
5898 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5900 let zoom = conf.zoom -. incr in
5901 setzoom zoom;
5902 state.mstate <- Mzoom (n, 0);
5903 else
5904 state.mstate <- Mzoom (n, i+1);
5906 else state.mstate <- Mzoom (n, 0)
5908 | _ -> state.mstate <- Mzoom (n, 0)
5910 else (
5911 match state.autoscroll with
5912 | Some step -> setautoscrollspeed step (n=4)
5913 | None ->
5914 if conf.wheelbypage || conf.presentation
5915 then (
5916 if n = 4
5917 then prevpage ()
5918 else nextpage ()
5920 else
5921 let incr =
5922 if n = 4
5923 then -conf.scrollstep
5924 else conf.scrollstep
5926 let incr = incr * 2 in
5927 let y = clamp incr in
5928 gotoy_and_clear_text y
5931 | n when (n = 6 || n = 7) && not down && canpan () ->
5932 state.x <-
5933 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5934 gotoy_and_clear_text state.y
5936 | 1 when Wsi.withshift mask ->
5937 state.mstate <- Mnone;
5938 if not down
5939 then (
5940 match unproject x y with
5941 | Some (pageno, ux, uy) ->
5942 let cmd = Printf.sprintf
5943 "%s %s %d %d %d"
5944 conf.stcmd state.path pageno ux uy
5946 popen cmd []
5947 | None -> ()
5950 | 1 when Wsi.withctrl mask ->
5951 if down
5952 then (
5953 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5954 state.mstate <- Mpan (x, y)
5956 else
5957 state.mstate <- Mnone
5959 | 3 ->
5960 if down
5961 then (
5962 Wsi.setcursor Wsi.CURSOR_CYCLE;
5963 let p = (x, y) in
5964 state.mstate <- Mzoomrect (p, p)
5966 else (
5967 match state.mstate with
5968 | Mzoomrect ((x0, y0), _) ->
5969 if abs (x-x0) > 10 && abs (y - y0) > 10
5970 then zoomrect x0 y0 x y
5971 else (
5972 state.mstate <- Mnone;
5973 Wsi.setcursor Wsi.CURSOR_INHERIT;
5974 G.postRedisplay "kill accidental zoom rect";
5976 | _ ->
5977 Wsi.setcursor Wsi.CURSOR_INHERIT;
5978 state.mstate <- Mnone
5981 | 1 when x > state.winw - state.scrollw ->
5982 if down
5983 then
5984 let _, position, sh = state.uioh#scrollph in
5985 if y > truncate position && y < truncate (position +. sh)
5986 then state.mstate <- Mscrolly
5987 else scrolly y
5988 else
5989 state.mstate <- Mnone
5991 | 1 when y > state.winh - state.hscrollh ->
5992 if down
5993 then
5994 let _, position, sw = state.uioh#scrollpw in
5995 if x > truncate position && x < truncate (position +. sw)
5996 then state.mstate <- Mscrollx
5997 else scrollx x
5998 else
5999 state.mstate <- Mnone
6001 | 1 ->
6002 let dest = if down then getunder x y else Unone in
6003 begin match dest with
6004 | Ulinkgoto _
6005 | Ulinkuri _
6006 | Uremote _
6007 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6008 gotounder dest
6010 | Unone when down ->
6011 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6012 state.mstate <- Mpan (x, y);
6014 | Unone | Utext _ ->
6015 if down
6016 then (
6017 if conf.angle mod 360 = 0
6018 then (
6019 state.mstate <- Msel ((x, y), (x, y));
6020 G.postRedisplay "mouse select";
6023 else (
6024 match state.mstate with
6025 | Mnone -> ()
6027 | Mzoom _ | Mscrollx | Mscrolly ->
6028 state.mstate <- Mnone
6030 | Mzoomrect ((x0, y0), _) ->
6031 zoomrect x0 y0 x y
6033 | Mpan _ ->
6034 Wsi.setcursor Wsi.CURSOR_INHERIT;
6035 state.mstate <- Mnone
6037 | Msel ((x0, y0), (x1, y1)) ->
6038 let rec loop = function
6039 | [] -> ()
6040 | l :: rest ->
6041 let inside =
6042 let a0 = l.pagedispy in
6043 let a1 = a0 + l.pagevh in
6044 let b0 = l.pagedispx in
6045 let b1 = b0 + l.pagevw in
6046 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6047 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6049 if inside
6050 then
6051 match getopaque l.pageno with
6052 | Some opaque ->
6053 begin
6054 match Ne.pipe () with
6055 | Ne.Exn exn ->
6056 showtext '!'
6057 (Printf.sprintf
6058 "can not create sel pipe: %s"
6059 (exntos exn));
6060 | Ne.Res (r, w) ->
6061 let doclose what fd =
6062 Ne.clo fd (fun msg ->
6063 dolog "%s close failed: %s" what msg)
6066 popen conf.selcmd [r, 0; w, -1];
6067 copysel w opaque;
6068 doclose "pipe/r" r;
6069 G.postRedisplay "copysel";
6070 with exn ->
6071 dolog "can not execute %S: %s"
6072 conf.selcmd (exntos exn);
6073 doclose "pipe/r" r;
6074 doclose "pipe/w" w;
6076 | None -> ()
6077 else loop rest
6079 loop state.layout;
6080 Wsi.setcursor Wsi.CURSOR_INHERIT;
6081 state.mstate <- Mnone;
6085 | _ -> ()
6088 let birdseyemouse button down x y mask
6089 (conf, leftx, _, hooverpageno, anchor) =
6090 match button with
6091 | 1 when down ->
6092 let rec loop = function
6093 | [] -> ()
6094 | l :: rest ->
6095 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6096 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6097 then (
6098 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6100 else loop rest
6102 loop state.layout
6103 | 3 -> ()
6104 | _ -> viewmouse button down x y mask
6107 let mouse button down x y mask =
6108 state.uioh <- state.uioh#button button down x y mask;
6111 let motion ~x ~y =
6112 state.uioh <- state.uioh#motion x y
6115 let pmotion ~x ~y =
6116 state.uioh <- state.uioh#pmotion x y;
6119 let uioh = object
6120 method display = ()
6122 method key key mask =
6123 begin match state.mode with
6124 | Textentry textentry -> textentrykeyboard key mask textentry
6125 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6126 | View -> viewkeyboard key mask
6127 | LinkNav linknav -> linknavkeyboard key mask linknav
6128 end;
6129 state.uioh
6131 method button button bstate x y mask =
6132 begin match state.mode with
6133 | LinkNav _
6134 | View -> viewmouse button bstate x y mask
6135 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6136 | Textentry _ -> ()
6137 end;
6138 state.uioh
6140 method motion x y =
6141 begin match state.mode with
6142 | Textentry _ -> ()
6143 | View | Birdseye _ | LinkNav _ ->
6144 match state.mstate with
6145 | Mzoom _ | Mnone -> ()
6147 | Mpan (x0, y0) ->
6148 let dx = x - x0
6149 and dy = y0 - y in
6150 state.mstate <- Mpan (x, y);
6151 if canpan ()
6152 then state.x <- panbound (state.x + dx);
6153 let y = clamp dy in
6154 gotoy_and_clear_text y
6156 | Msel (a, _) ->
6157 state.mstate <- Msel (a, (x, y));
6158 G.postRedisplay "motion select";
6160 | Mscrolly ->
6161 let y = min state.winh (max 0 y) in
6162 scrolly y
6164 | Mscrollx ->
6165 let x = min state.winw (max 0 x) in
6166 scrollx x
6168 | Mzoomrect (p0, _) ->
6169 state.mstate <- Mzoomrect (p0, (x, y));
6170 G.postRedisplay "motion zoomrect";
6171 end;
6172 state.uioh
6174 method pmotion x y =
6175 begin match state.mode with
6176 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6177 let rec loop = function
6178 | [] ->
6179 if hooverpageno != -1
6180 then (
6181 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6182 G.postRedisplay "pmotion birdseye no hoover";
6184 | l :: rest ->
6185 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6186 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6187 then (
6188 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6189 G.postRedisplay "pmotion birdseye hoover";
6191 else loop rest
6193 loop state.layout
6195 | Textentry _ -> ()
6197 | LinkNav _
6198 | View ->
6199 match state.mstate with
6200 | Mnone -> updateunder x y
6201 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6203 end;
6204 state.uioh
6206 method infochanged _ = ()
6208 method scrollph =
6209 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6210 let p, h =
6211 if maxy = 0
6212 then 0.0, float state.winh
6213 else scrollph state.y maxy
6215 state.scrollw, p, h
6217 method scrollpw =
6218 let winw = state.winw - state.scrollw in
6219 let fwinw = float winw in
6220 let sw =
6221 let sw = fwinw /. float state.w in
6222 let sw = fwinw *. sw in
6223 max sw (float conf.scrollh)
6225 let position =
6226 let maxx = state.w + winw in
6227 let x = winw - state.x in
6228 let percent = float x /. float maxx in
6229 (fwinw -. sw) *. percent
6231 state.hscrollh, position, sw
6233 method modehash =
6234 let modename =
6235 match state.mode with
6236 | LinkNav _ -> "links"
6237 | Textentry _ -> "textentry"
6238 | Birdseye _ -> "birdseye"
6239 | View -> "view"
6241 findkeyhash conf modename
6243 method eformsgs = true
6244 end;;
6246 module Config =
6247 struct
6248 open Parser
6250 let fontpath = ref "";;
6252 module KeyMap =
6253 Map.Make (struct type t = (int * int) let compare = compare end);;
6255 let unent s =
6256 let l = String.length s in
6257 let b = Buffer.create l in
6258 unent b s 0 l;
6259 Buffer.contents b;
6262 let home =
6263 try Sys.getenv "HOME"
6264 with exn ->
6265 prerr_endline
6266 ("Can not determine home directory location: " ^ exntos exn);
6270 let modifier_of_string = function
6271 | "alt" -> Wsi.altmask
6272 | "shift" -> Wsi.shiftmask
6273 | "ctrl" | "control" -> Wsi.ctrlmask
6274 | "meta" -> Wsi.metamask
6275 | _ -> 0
6278 let key_of_string =
6279 let r = Str.regexp "-" in
6280 fun s ->
6281 let elems = Str.full_split r s in
6282 let f n k m =
6283 let g s =
6284 let m1 = modifier_of_string s in
6285 if m1 = 0
6286 then (Wsi.namekey s, m)
6287 else (k, m lor m1)
6288 in function
6289 | Str.Delim s when n land 1 = 0 -> g s
6290 | Str.Text s -> g s
6291 | Str.Delim _ -> (k, m)
6293 let rec loop n k m = function
6294 | [] -> (k, m)
6295 | x :: xs ->
6296 let k, m = f n k m x in
6297 loop (n+1) k m xs
6299 loop 0 0 0 elems
6302 let keys_of_string =
6303 let r = Str.regexp "[ \t]" in
6304 fun s ->
6305 let elems = Str.split r s in
6306 List.map key_of_string elems
6309 let copykeyhashes c =
6310 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6313 let config_of c attrs =
6314 let apply c k v =
6316 match k with
6317 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6318 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6319 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6320 | "preload" -> { c with preload = bool_of_string v }
6321 | "page-bias" -> { c with pagebias = int_of_string v }
6322 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6323 | "horizontal-scroll-step" ->
6324 { c with hscrollstep = max (int_of_string v) 1 }
6325 | "auto-scroll-step" ->
6326 { c with autoscrollstep = max 0 (int_of_string v) }
6327 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6328 | "crop-hack" -> { c with crophack = bool_of_string v }
6329 | "throttle" ->
6330 let mw =
6331 match String.lowercase v with
6332 | "true" -> Some infinity
6333 | "false" -> None
6334 | f -> Some (float_of_string f)
6336 { c with maxwait = mw}
6337 | "highlight-links" -> { c with hlinks = bool_of_string v }
6338 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6339 | "vertical-margin" ->
6340 { c with interpagespace = max 0 (int_of_string v) }
6341 | "zoom" ->
6342 let zoom = float_of_string v /. 100. in
6343 let zoom = max zoom 0.0 in
6344 { c with zoom = zoom }
6345 | "presentation" -> { c with presentation = bool_of_string v }
6346 | "rotation-angle" -> { c with angle = int_of_string v }
6347 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6348 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6349 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6350 | "proportional-display" ->
6351 let fm =
6352 if bool_of_string v
6353 then FitProportional
6354 else FitWidth
6356 { c with fitmodel = fm }
6357 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6358 | "pixmap-cache-size" ->
6359 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6360 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6361 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6362 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6363 | "persistent-location" -> { c with jumpback = bool_of_string v }
6364 | "background-color" -> { c with bgcolor = color_of_string v }
6365 | "scrollbar-in-presentation" ->
6366 { c with scrollbarinpm = bool_of_string v }
6367 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6368 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6369 | "mupdf-store-size" ->
6370 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6371 | "checkers" -> { c with checkers = bool_of_string v }
6372 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6373 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6374 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6375 | "uri-launcher" -> { c with urilauncher = unent v }
6376 | "path-launcher" -> { c with pathlauncher = unent v }
6377 | "color-space" -> { c with colorspace = colorspace_of_string v }
6378 | "invert-colors" -> { c with invert = bool_of_string v }
6379 | "brightness" -> { c with colorscale = float_of_string v }
6380 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6381 | "ghyllscroll" ->
6382 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6383 | "columns" ->
6384 let (n, _, _) as nab = multicolumns_of_string v in
6385 if n < 0
6386 then { c with columns = Csplit (-n, [||]) }
6387 else { c with columns = Cmulti (nab, [||]) }
6388 | "birds-eye-columns" ->
6389 { c with beyecolumns = Some (max (int_of_string v) 2) }
6390 | "selection-command" -> { c with selcmd = unent v }
6391 | "synctex-command" -> { c with stcmd = unent v }
6392 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6393 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6394 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6395 | "use-pbo" -> { c with usepbo = bool_of_string v }
6396 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6397 | _ -> c
6398 with exn ->
6399 prerr_endline ("Error processing attribute (`" ^
6400 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6403 let rec fold c = function
6404 | [] -> c
6405 | (k, v) :: rest ->
6406 let c = apply c k v in
6407 fold c rest
6409 fold { c with keyhashes = copykeyhashes c } attrs;
6412 let fromstring f pos n v d =
6413 try f v
6414 with exn ->
6415 dolog "Error processing attribute (%S=%S) at %d\n%s"
6416 n v pos (exntos exn)
6421 let bookmark_of attrs =
6422 let rec fold title page rely visy = function
6423 | ("title", v) :: rest -> fold v page rely visy rest
6424 | ("page", v) :: rest -> fold title v rely visy rest
6425 | ("rely", v) :: rest -> fold title page v visy rest
6426 | ("visy", v) :: rest -> fold title page rely v rest
6427 | _ :: rest -> fold title page rely visy rest
6428 | [] -> title, page, rely, visy
6430 fold "invalid" "0" "0" "0" attrs
6433 let doc_of attrs =
6434 let rec fold path page rely pan visy = function
6435 | ("path", v) :: rest -> fold v page rely pan visy rest
6436 | ("page", v) :: rest -> fold path v rely pan visy rest
6437 | ("rely", v) :: rest -> fold path page v pan visy rest
6438 | ("pan", v) :: rest -> fold path page rely v visy rest
6439 | ("visy", v) :: rest -> fold path page rely pan v rest
6440 | _ :: rest -> fold path page rely pan visy rest
6441 | [] -> path, page, rely, pan, visy
6443 fold "" "0" "0" "0" "0" attrs
6446 let map_of attrs =
6447 let rec fold rs ls = function
6448 | ("out", v) :: rest -> fold v ls rest
6449 | ("in", v) :: rest -> fold rs v rest
6450 | _ :: rest -> fold ls rs rest
6451 | [] -> ls, rs
6453 fold "" "" attrs
6456 let setconf dst src =
6457 dst.scrollbw <- src.scrollbw;
6458 dst.scrollh <- src.scrollh;
6459 dst.icase <- src.icase;
6460 dst.preload <- src.preload;
6461 dst.pagebias <- src.pagebias;
6462 dst.verbose <- src.verbose;
6463 dst.scrollstep <- src.scrollstep;
6464 dst.maxhfit <- src.maxhfit;
6465 dst.crophack <- src.crophack;
6466 dst.autoscrollstep <- src.autoscrollstep;
6467 dst.maxwait <- src.maxwait;
6468 dst.hlinks <- src.hlinks;
6469 dst.underinfo <- src.underinfo;
6470 dst.interpagespace <- src.interpagespace;
6471 dst.zoom <- src.zoom;
6472 dst.presentation <- src.presentation;
6473 dst.angle <- src.angle;
6474 dst.cwinw <- src.cwinw;
6475 dst.cwinh <- src.cwinh;
6476 dst.savebmarks <- src.savebmarks;
6477 dst.memlimit <- src.memlimit;
6478 dst.fitmodel <- src.fitmodel;
6479 dst.texcount <- src.texcount;
6480 dst.sliceheight <- src.sliceheight;
6481 dst.thumbw <- src.thumbw;
6482 dst.jumpback <- src.jumpback;
6483 dst.bgcolor <- src.bgcolor;
6484 dst.scrollbarinpm <- src.scrollbarinpm;
6485 dst.tilew <- src.tilew;
6486 dst.tileh <- src.tileh;
6487 dst.mustoresize <- src.mustoresize;
6488 dst.checkers <- src.checkers;
6489 dst.aalevel <- src.aalevel;
6490 dst.trimmargins <- src.trimmargins;
6491 dst.trimfuzz <- src.trimfuzz;
6492 dst.urilauncher <- src.urilauncher;
6493 dst.colorspace <- src.colorspace;
6494 dst.invert <- src.invert;
6495 dst.colorscale <- src.colorscale;
6496 dst.redirectstderr <- src.redirectstderr;
6497 dst.ghyllscroll <- src.ghyllscroll;
6498 dst.columns <- src.columns;
6499 dst.beyecolumns <- src.beyecolumns;
6500 dst.selcmd <- src.selcmd;
6501 dst.updatecurs <- src.updatecurs;
6502 dst.pathlauncher <- src.pathlauncher;
6503 dst.keyhashes <- copykeyhashes src;
6504 dst.hfsize <- src.hfsize;
6505 dst.hscrollstep <- src.hscrollstep;
6506 dst.pgscale <- src.pgscale;
6507 dst.usepbo <- src.usepbo;
6508 dst.wheelbypage <- src.wheelbypage;
6509 dst.stcmd <- src.stcmd;
6512 let get s =
6513 let h = Hashtbl.create 10 in
6514 let dc = { defconf with angle = defconf.angle } in
6515 let rec toplevel v t spos _ =
6516 match t with
6517 | Vdata | Vcdata | Vend -> v
6518 | Vopen ("llppconfig", _, closed) ->
6519 if closed
6520 then v
6521 else { v with f = llppconfig }
6522 | Vopen _ ->
6523 error "unexpected subelement at top level" s spos
6524 | Vclose _ -> error "unexpected close at top level" s spos
6526 and llppconfig v t spos _ =
6527 match t with
6528 | Vdata | Vcdata -> v
6529 | Vend -> error "unexpected end of input in llppconfig" s spos
6530 | Vopen ("defaults", attrs, closed) ->
6531 let c = config_of dc attrs in
6532 setconf dc c;
6533 if closed
6534 then v
6535 else { v with f = defaults }
6537 | Vopen ("ui-font", attrs, closed) ->
6538 let rec getsize size = function
6539 | [] -> size
6540 | ("size", v) :: rest ->
6541 let size =
6542 fromstring int_of_string spos "size" v fstate.fontsize in
6543 getsize size rest
6544 | l -> getsize size l
6546 fstate.fontsize <- getsize fstate.fontsize attrs;
6547 if closed
6548 then v
6549 else { v with f = uifont (Buffer.create 10) }
6551 | Vopen ("doc", attrs, closed) ->
6552 let pathent, spage, srely, span, svisy = doc_of attrs in
6553 let path = unent pathent
6554 and pageno = fromstring int_of_string spos "page" spage 0
6555 and rely = fromstring float_of_string spos "rely" srely 0.0
6556 and pan = fromstring int_of_string spos "pan" span 0
6557 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6558 let c = config_of dc attrs in
6559 let anchor = (pageno, rely, visy) in
6560 if closed
6561 then (Hashtbl.add h path (c, [], pan, anchor); v)
6562 else { v with f = doc path pan anchor c [] }
6564 | Vopen _ ->
6565 error "unexpected subelement in llppconfig" s spos
6567 | Vclose "llppconfig" -> { v with f = toplevel }
6568 | Vclose _ -> error "unexpected close in llppconfig" s spos
6570 and defaults v t spos _ =
6571 match t with
6572 | Vdata | Vcdata -> v
6573 | Vend -> error "unexpected end of input in defaults" s spos
6574 | Vopen ("keymap", attrs, closed) ->
6575 let modename =
6576 try List.assoc "mode" attrs
6577 with Not_found -> "global" in
6578 if closed
6579 then v
6580 else
6581 let ret keymap =
6582 let h = findkeyhash dc modename in
6583 KeyMap.iter (Hashtbl.replace h) keymap;
6584 defaults
6586 { v with f = pkeymap ret KeyMap.empty }
6588 | Vopen (_, _, _) ->
6589 error "unexpected subelement in defaults" s spos
6591 | Vclose "defaults" ->
6592 { v with f = llppconfig }
6594 | Vclose _ -> error "unexpected close in defaults" s spos
6596 and uifont b v t spos epos =
6597 match t with
6598 | Vdata | Vcdata ->
6599 Buffer.add_substring b s spos (epos - spos);
6601 | Vopen (_, _, _) ->
6602 error "unexpected subelement in ui-font" s spos
6603 | Vclose "ui-font" ->
6604 if String.length !fontpath = 0
6605 then fontpath := Buffer.contents b;
6606 { v with f = llppconfig }
6607 | Vclose _ -> error "unexpected close in ui-font" s spos
6608 | Vend -> error "unexpected end of input in ui-font" s spos
6610 and doc path pan anchor c bookmarks v t spos _ =
6611 match t with
6612 | Vdata | Vcdata -> v
6613 | Vend -> error "unexpected end of input in doc" s spos
6614 | Vopen ("bookmarks", _, closed) ->
6615 if closed
6616 then v
6617 else { v with f = pbookmarks path pan anchor c bookmarks }
6619 | Vopen ("keymap", attrs, closed) ->
6620 let modename =
6621 try List.assoc "mode" attrs
6622 with Not_found -> "global"
6624 if closed
6625 then v
6626 else
6627 let ret keymap =
6628 let h = findkeyhash c modename in
6629 KeyMap.iter (Hashtbl.replace h) keymap;
6630 doc path pan anchor c bookmarks
6632 { v with f = pkeymap ret KeyMap.empty }
6634 | Vopen (_, _, _) ->
6635 error "unexpected subelement in doc" s spos
6637 | Vclose "doc" ->
6638 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6639 { v with f = llppconfig }
6641 | Vclose _ -> error "unexpected close in doc" s spos
6643 and pkeymap ret keymap v t spos _ =
6644 match t with
6645 | Vdata | Vcdata -> v
6646 | Vend -> error "unexpected end of input in keymap" s spos
6647 | Vopen ("map", attrs, closed) ->
6648 let r, l = map_of attrs in
6649 let kss = fromstring keys_of_string spos "in" r [] in
6650 let lss = fromstring keys_of_string spos "out" l [] in
6651 let keymap =
6652 match kss with
6653 | [] -> keymap
6654 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6655 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6657 if closed
6658 then { v with f = pkeymap ret keymap }
6659 else
6660 let f () = v in
6661 { v with f = skip "map" f }
6663 | Vopen _ ->
6664 error "unexpected subelement in keymap" s spos
6666 | Vclose "keymap" ->
6667 { v with f = ret keymap }
6669 | Vclose _ -> error "unexpected close in keymap" s spos
6671 and pbookmarks path pan anchor c bookmarks v t spos _ =
6672 match t with
6673 | Vdata | Vcdata -> v
6674 | Vend -> error "unexpected end of input in bookmarks" s spos
6675 | Vopen ("item", attrs, closed) ->
6676 let titleent, spage, srely, svisy = bookmark_of attrs in
6677 let page = fromstring int_of_string spos "page" spage 0
6678 and rely = fromstring float_of_string spos "rely" srely 0.0
6679 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6680 let bookmarks =
6681 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6683 if closed
6684 then { v with f = pbookmarks path pan anchor c bookmarks }
6685 else
6686 let f () = v in
6687 { v with f = skip "item" f }
6689 | Vopen _ ->
6690 error "unexpected subelement in bookmarks" s spos
6692 | Vclose "bookmarks" ->
6693 { v with f = doc path pan anchor c bookmarks }
6695 | Vclose _ -> error "unexpected close in bookmarks" s spos
6697 and skip tag f v t spos _ =
6698 match t with
6699 | Vdata | Vcdata -> v
6700 | Vend ->
6701 error ("unexpected end of input in skipped " ^ tag) s spos
6702 | Vopen (tag', _, closed) ->
6703 if closed
6704 then v
6705 else
6706 let f' () = { v with f = skip tag f } in
6707 { v with f = skip tag' f' }
6708 | Vclose ctag ->
6709 if tag = ctag
6710 then f ()
6711 else error ("unexpected close in skipped " ^ tag) s spos
6714 parse { f = toplevel; accu = () } s;
6715 h, dc;
6718 let do_load f ic =
6720 let len = in_channel_length ic in
6721 let s = String.create len in
6722 really_input ic s 0 len;
6723 f s;
6724 with
6725 | Parse_error (msg, s, pos) ->
6726 let subs = subs s pos in
6727 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6728 failwith ("parse error: " ^ s)
6730 | exn ->
6731 failwith ("config load error: " ^ exntos exn)
6734 let defconfpath =
6735 let dir =
6737 let dir = Filename.concat home ".config" in
6738 if Sys.is_directory dir then dir else home
6739 with _ -> home
6741 Filename.concat dir "llpp.conf"
6744 let confpath = ref defconfpath;;
6746 let load1 f =
6747 if Sys.file_exists !confpath
6748 then
6749 match
6750 (try Some (open_in_bin !confpath)
6751 with exn ->
6752 prerr_endline
6753 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6754 exntos exn);
6755 None
6757 with
6758 | Some ic ->
6759 let success =
6761 f (do_load get ic)
6762 with exn ->
6763 prerr_endline
6764 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6765 exntos exn);
6766 false
6768 close_in ic;
6769 success
6771 | None -> false
6772 else
6773 f (Hashtbl.create 0, defconf)
6776 let load () =
6777 let f (h, dc) =
6778 let pc, pb, px, pa =
6780 let key =
6781 if String.length state.origin = 0
6782 then state.path
6783 else state.origin
6785 Hashtbl.find h (Filename.basename key)
6786 with Not_found -> dc, [], 0, emptyanchor
6788 setconf defconf dc;
6789 setconf conf pc;
6790 state.bookmarks <- pb;
6791 state.x <- px;
6792 state.scrollw <- conf.scrollbw;
6793 if conf.jumpback
6794 then state.anchor <- pa;
6795 cbput state.hists.nav pa;
6796 true
6798 load1 f
6801 let add_attrs bb always dc c =
6802 let ob s a b =
6803 if always || a != b
6804 then Printf.bprintf bb "\n %s='%b'" s a
6805 and oi s a b =
6806 if always || a != b
6807 then Printf.bprintf bb "\n %s='%d'" s a
6808 and oI s a b =
6809 if always || a != b
6810 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6811 and oz s a b =
6812 if always || a <> b
6813 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6814 and oF s a b =
6815 if always || a <> b
6816 then Printf.bprintf bb "\n %s='%f'" s a
6817 and oc s a b =
6818 if always || a <> b
6819 then
6820 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6821 and oC s a b =
6822 if always || a <> b
6823 then
6824 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6825 and oR s a b =
6826 if always || a <> b
6827 then
6828 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6829 and os s a b =
6830 if always || a <> b
6831 then
6832 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6833 and og s a b =
6834 if always || a <> b
6835 then
6836 match a with
6837 | None -> ()
6838 | Some (_N, _A, _B) ->
6839 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6840 and oW s a b =
6841 if always || a <> b
6842 then
6843 let v =
6844 match a with
6845 | None -> "false"
6846 | Some f ->
6847 if f = infinity
6848 then "true"
6849 else string_of_float f
6851 Printf.bprintf bb "\n %s='%s'" s v
6852 and oco s a b =
6853 if always || a <> b
6854 then
6855 match a with
6856 | Cmulti ((n, a, b), _) when n > 1 ->
6857 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6858 | Csplit (n, _) when n > 1 ->
6859 Printf.bprintf bb "\n %s='%d'" s ~-n
6860 | _ -> ()
6861 and obeco s a b =
6862 if always || a <> b
6863 then
6864 match a with
6865 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6866 | _ -> ()
6867 and oFm s a b =
6868 if always || a <> b
6869 then
6870 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6872 oi "width" c.cwinw dc.cwinw;
6873 oi "height" c.cwinh dc.cwinh;
6874 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6875 oi "scroll-handle-height" c.scrollh dc.scrollh;
6876 ob "case-insensitive-search" c.icase dc.icase;
6877 ob "preload" c.preload dc.preload;
6878 oi "page-bias" c.pagebias dc.pagebias;
6879 oi "scroll-step" c.scrollstep dc.scrollstep;
6880 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6881 ob "max-height-fit" c.maxhfit dc.maxhfit;
6882 ob "crop-hack" c.crophack dc.crophack;
6883 oW "throttle" c.maxwait dc.maxwait;
6884 ob "highlight-links" c.hlinks dc.hlinks;
6885 ob "under-cursor-info" c.underinfo dc.underinfo;
6886 oi "vertical-margin" c.interpagespace dc.interpagespace;
6887 oz "zoom" c.zoom dc.zoom;
6888 ob "presentation" c.presentation dc.presentation;
6889 oi "rotation-angle" c.angle dc.angle;
6890 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6891 oFm "fit-model" c.fitmodel dc.fitmodel;
6892 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6893 oi "tex-count" c.texcount dc.texcount;
6894 oi "slice-height" c.sliceheight dc.sliceheight;
6895 oi "thumbnail-width" c.thumbw dc.thumbw;
6896 ob "persistent-location" c.jumpback dc.jumpback;
6897 oc "background-color" c.bgcolor dc.bgcolor;
6898 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6899 oi "tile-width" c.tilew dc.tilew;
6900 oi "tile-height" c.tileh dc.tileh;
6901 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6902 ob "checkers" c.checkers dc.checkers;
6903 oi "aalevel" c.aalevel dc.aalevel;
6904 ob "trim-margins" c.trimmargins dc.trimmargins;
6905 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6906 os "uri-launcher" c.urilauncher dc.urilauncher;
6907 os "path-launcher" c.pathlauncher dc.pathlauncher;
6908 oC "color-space" c.colorspace dc.colorspace;
6909 ob "invert-colors" c.invert dc.invert;
6910 oF "brightness" c.colorscale dc.colorscale;
6911 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6912 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6913 oco "columns" c.columns dc.columns;
6914 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6915 os "selection-command" c.selcmd dc.selcmd;
6916 os "synctex-command" c.stcmd dc.stcmd;
6917 ob "update-cursor" c.updatecurs dc.updatecurs;
6918 oi "hint-font-size" c.hfsize dc.hfsize;
6919 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6920 oF "page-scroll-scale" c.pgscale dc.pgscale;
6921 ob "use-pbo" c.usepbo dc.usepbo;
6922 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6925 let keymapsbuf always dc c =
6926 let bb = Buffer.create 16 in
6927 let rec loop = function
6928 | [] -> ()
6929 | (modename, h) :: rest ->
6930 let dh = findkeyhash dc modename in
6931 if always || h <> dh
6932 then (
6933 if Hashtbl.length h > 0
6934 then (
6935 if Buffer.length bb > 0
6936 then Buffer.add_char bb '\n';
6937 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6938 Hashtbl.iter (fun i o ->
6939 let isdifferent = always ||
6941 let dO = Hashtbl.find dh i in
6942 dO <> o
6943 with Not_found -> true
6945 if isdifferent
6946 then
6947 let addkm (k, m) =
6948 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6949 if Wsi.withalt m then Buffer.add_string bb "alt-";
6950 if Wsi.withshift m then Buffer.add_string bb "shift-";
6951 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6952 Buffer.add_string bb (Wsi.keyname k);
6954 let addkms l =
6955 let rec loop = function
6956 | [] -> ()
6957 | km :: [] -> addkm km
6958 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6960 loop l
6962 Buffer.add_string bb "<map in='";
6963 addkm i;
6964 match o with
6965 | KMinsrt km ->
6966 Buffer.add_string bb "' out='";
6967 addkm km;
6968 Buffer.add_string bb "'/>\n"
6970 | KMinsrl kms ->
6971 Buffer.add_string bb "' out='";
6972 addkms kms;
6973 Buffer.add_string bb "'/>\n"
6975 | KMmulti (ins, kms) ->
6976 Buffer.add_char bb ' ';
6977 addkms ins;
6978 Buffer.add_string bb "' out='";
6979 addkms kms;
6980 Buffer.add_string bb "'/>\n"
6981 ) h;
6982 Buffer.add_string bb "</keymap>";
6985 loop rest
6987 loop c.keyhashes;
6991 let save () =
6992 let uifontsize = fstate.fontsize in
6993 let bb = Buffer.create 32768 in
6994 let w, h =
6995 List.fold_left
6996 (fun (w, h) ws ->
6997 match ws with
6998 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6999 | Wsi.MaxVert -> (w, conf.cwinh)
7000 | Wsi.MaxHorz -> (conf.cwinw, h)
7002 (state.winw, state.winh) state.winstate
7004 conf.cwinw <- w;
7005 conf.cwinh <- h;
7006 let f (h, dc) =
7007 let dc = if conf.bedefault then conf else dc in
7008 Buffer.add_string bb "<llppconfig>\n";
7010 if String.length !fontpath > 0
7011 then
7012 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7013 uifontsize
7014 !fontpath
7015 else (
7016 if uifontsize <> 14
7017 then
7018 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7021 Buffer.add_string bb "<defaults ";
7022 add_attrs bb true dc dc;
7023 let kb = keymapsbuf true dc dc in
7024 if Buffer.length kb > 0
7025 then (
7026 Buffer.add_string bb ">\n";
7027 Buffer.add_buffer bb kb;
7028 Buffer.add_string bb "\n</defaults>\n";
7030 else Buffer.add_string bb "/>\n";
7032 let adddoc path pan anchor c bookmarks =
7033 if bookmarks == [] && c = dc && anchor = emptyanchor
7034 then ()
7035 else (
7036 Printf.bprintf bb "<doc path='%s'"
7037 (enent path 0 (String.length path));
7039 if anchor <> emptyanchor
7040 then (
7041 let n, rely, visy = anchor in
7042 Printf.bprintf bb " page='%d'" n;
7043 if rely > 1e-6
7044 then
7045 Printf.bprintf bb " rely='%f'" rely
7047 if abs_float visy > 1e-6
7048 then
7049 Printf.bprintf bb " visy='%f'" visy
7053 if pan != 0
7054 then Printf.bprintf bb " pan='%d'" pan;
7056 add_attrs bb false dc c;
7057 let kb = keymapsbuf false dc c in
7059 begin match bookmarks with
7060 | [] ->
7061 if Buffer.length kb > 0
7062 then (
7063 Buffer.add_string bb ">\n";
7064 Buffer.add_buffer bb kb;
7065 Buffer.add_string bb "\n</doc>\n";
7067 else Buffer.add_string bb "/>\n"
7068 | _ ->
7069 Buffer.add_string bb ">\n<bookmarks>\n";
7070 List.iter (fun (title, _level, (page, rely, visy)) ->
7071 Printf.bprintf bb
7072 "<item title='%s' page='%d'"
7073 (enent title 0 (String.length title))
7074 page
7076 if rely > 1e-6
7077 then
7078 Printf.bprintf bb " rely='%f'" rely
7080 if abs_float visy > 1e-6
7081 then
7082 Printf.bprintf bb " visy='%f'" visy
7084 Buffer.add_string bb "/>\n";
7085 ) bookmarks;
7086 Buffer.add_string bb "</bookmarks>";
7087 if Buffer.length kb > 0
7088 then (
7089 Buffer.add_string bb "\n";
7090 Buffer.add_buffer bb kb;
7092 Buffer.add_string bb "\n</doc>\n";
7093 end;
7097 let pan, conf =
7098 match state.mode with
7099 | Birdseye (c, pan, _, _, _) ->
7100 let beyecolumns =
7101 match conf.columns with
7102 | Cmulti ((c, _, _), _) -> Some c
7103 | Csingle _ -> None
7104 | Csplit _ -> None
7105 and columns =
7106 match c.columns with
7107 | Cmulti (c, _) -> Cmulti (c, [||])
7108 | Csingle _ -> Csingle [||]
7109 | Csplit _ -> failwith "quit from bird's eye while split"
7111 pan, { c with beyecolumns = beyecolumns; columns = columns }
7112 | _ -> state.x, conf
7114 let basename = Filename.basename
7115 (if String.length state.origin = 0 then state.path else state.origin)
7117 adddoc basename pan (getanchor ())
7118 (let conf =
7119 let autoscrollstep =
7120 match state.autoscroll with
7121 | Some step -> step
7122 | None -> conf.autoscrollstep
7124 match state.mode with
7125 | Birdseye (bc, _, _, _, _) ->
7126 { conf with
7127 zoom = bc.zoom;
7128 presentation = bc.presentation;
7129 interpagespace = bc.interpagespace;
7130 maxwait = bc.maxwait;
7131 autoscrollstep = autoscrollstep }
7132 | _ -> { conf with autoscrollstep = autoscrollstep }
7133 in conf)
7134 (if conf.savebmarks then state.bookmarks else []);
7136 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7137 if basename <> path
7138 then adddoc path x anchor c bookmarks
7139 ) h;
7140 Buffer.add_string bb "</llppconfig>\n";
7141 true;
7143 if load1 f && Buffer.length bb > 0
7144 then
7146 let tmp = !confpath ^ ".tmp" in
7147 let oc = open_out_bin tmp in
7148 Buffer.output_buffer oc bb;
7149 close_out oc;
7150 Unix.rename tmp !confpath;
7151 with exn ->
7152 prerr_endline
7153 ("error while saving configuration: " ^ exntos exn)
7155 end;;
7157 let adderrmsg src msg =
7158 Buffer.add_string state.errmsgs msg;
7159 state.newerrmsgs <- true;
7160 G.postRedisplay src
7163 let adderrfmt src fmt =
7164 Format.kprintf (fun s -> adderrmsg src s) fmt;
7167 let ract cmds =
7168 let cl = splitatspace cmds in
7169 let scan s fmt f =
7170 try Scanf.sscanf s fmt f
7171 with exn ->
7172 adderrfmt "remote exec"
7173 "error processing '%S': %s\n" cmds (exntos exn)
7175 match cl with
7176 | "reload" :: [] -> reload ()
7177 | "goto" :: args :: [] ->
7178 scan args "%u %f %f"
7179 (fun pageno x y ->
7180 let cmd, _ = state.geomcmds in
7181 if String.length cmd = 0
7182 then gotopagexy pageno x y
7183 else
7184 let f prevf () =
7185 gotopagexy pageno x y;
7186 prevf ()
7188 state.reprf <- f state.reprf
7190 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7191 | "rect" :: args :: [] ->
7192 scan args "%u %u %f %f %f %f"
7193 (fun pageno color x0 y0 x1 y1 ->
7194 onpagerect pageno (fun w h ->
7195 let _,w1,h1,_ = getpagedim pageno in
7196 let sw = float w1 /. w
7197 and sh = float h1 /. h in
7198 let x0s = x0 *. sw
7199 and x1s = x1 *. sw
7200 and y0s = y0 *. sh
7201 and y1s = y1 *. sh in
7202 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7203 debugrect rect;
7204 state.rects <- (pageno, color, rect) :: state.rects;
7205 G.postRedisplay "rect";
7208 | "activatewin" :: [] -> Wsi.activatewin ()
7209 | "quit" :: [] -> raise Quit
7210 | _ ->
7211 adderrfmt "remote command"
7212 "error processing remote command: %S\n" cmds;
7215 let remote =
7216 let scratch = String.create 80 in
7217 let buf = Buffer.create 80 in
7218 fun fd ->
7219 let rec tempfr () =
7220 try Some (Unix.read fd scratch 0 80)
7221 with
7222 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7223 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7224 | exn -> raise exn
7226 match tempfr () with
7227 | None -> Some fd
7228 | Some n ->
7229 if n = 0
7230 then (
7231 Unix.close fd;
7232 if Buffer.length buf > 0
7233 then (
7234 let s = Buffer.contents buf in
7235 Buffer.clear buf;
7236 ract s;
7238 None
7240 else
7241 let rec eat ppos =
7242 let nlpos =
7244 let pos = String.index_from scratch ppos '\n' in
7245 if pos >= n then -1 else pos
7246 with Not_found -> -1
7248 if nlpos >= 0
7249 then (
7250 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7251 let s = Buffer.contents buf in
7252 Buffer.clear buf;
7253 ract s;
7254 eat (nlpos+1);
7256 else (
7257 Buffer.add_substring buf scratch ppos (n-ppos);
7258 Some fd
7260 in eat 0
7263 let remoteopen path =
7264 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7265 with exn ->
7266 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7267 None
7270 let () =
7271 let trimcachepath = ref "" in
7272 let rcmdpath = ref "" in
7273 Arg.parse
7274 (Arg.align
7275 [("-p", Arg.String (fun s -> state.password <- s),
7276 "<password> Set password");
7278 ("-f", Arg.String (fun s -> Config.fontpath := s),
7279 "<path> Set path to the user interface font");
7281 ("-c", Arg.String (fun s -> Config.confpath := s),
7282 "<path> Set path to the configuration file");
7284 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7285 "<path> Set path to the trim cache file");
7287 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7288 "<named-destination> Set named destination");
7290 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7292 ("-remote", Arg.String (fun s -> rcmdpath := s),
7293 "<path> Set path to the remote commands source");
7295 ("-origin", Arg.String (fun s -> state.origin <- s),
7296 "<original path> Set original path");
7298 ("-v", Arg.Unit (fun () ->
7299 Printf.printf
7300 "%s\nconfiguration path: %s\n"
7301 (version ())
7302 Config.defconfpath
7304 exit 0), " Print version and exit");
7307 (fun s -> state.path <- s)
7308 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7310 if String.length state.path = 0
7311 then (prerr_endline "file name missing"; exit 1);
7313 if not (Config.load ())
7314 then prerr_endline "failed to load configuration";
7316 let globalkeyhash = findkeyhash conf "global" in
7317 let wsfd, winw, winh = Wsi.init (object
7318 method expose =
7319 if nogeomcmds state.geomcmds || platform == Posx
7320 then display ()
7321 else (
7322 GlClear.color (scalecolor2 conf.bgcolor);
7323 GlClear.clear [`color];
7325 method display = display ()
7326 method reshape w h = reshape w h
7327 method mouse b d x y m = mouse b d x y m
7328 method motion x y = state.mpos <- (x, y); motion x y
7329 method pmotion x y = state.mpos <- (x, y); pmotion x y
7330 method key k m =
7331 let mascm = m land (
7332 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7333 ) in
7334 match state.keystate with
7335 | KSnone ->
7336 let km = k, mascm in
7337 begin
7338 match
7339 let modehash = state.uioh#modehash in
7340 try Hashtbl.find modehash km
7341 with Not_found ->
7342 try Hashtbl.find globalkeyhash km
7343 with Not_found -> KMinsrt (k, m)
7344 with
7345 | KMinsrt (k, m) -> keyboard k m
7346 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7347 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7349 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7350 List.iter (fun (k, m) -> keyboard k m) insrt;
7351 state.keystate <- KSnone
7352 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7353 state.keystate <- KSinto (keys, insrt)
7354 | _ ->
7355 state.keystate <- KSnone
7357 method enter x y = state.mpos <- (x, y); pmotion x y
7358 method leave = state.mpos <- (-1, -1)
7359 method winstate wsl = state.winstate <- wsl
7360 method quit = raise Quit
7361 end) conf.cwinw conf.cwinh (platform = Posx) in
7363 state.wsfd <- wsfd;
7365 if not (
7366 List.exists GlMisc.check_extension
7367 [ "GL_ARB_texture_rectangle"
7368 ; "GL_EXT_texture_recangle"
7369 ; "GL_NV_texture_rectangle" ]
7371 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7373 let cr, sw =
7374 match Ne.pipe () with
7375 | Ne.Exn exn ->
7376 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7377 exit 1
7378 | Ne.Res rw -> rw
7379 and sr, cw =
7380 match Ne.pipe () with
7381 | Ne.Exn exn ->
7382 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7383 exit 1
7384 | Ne.Res rw -> rw
7387 cloexec cr;
7388 cloexec sw;
7389 cloexec sr;
7390 cloexec cw;
7392 setcheckers conf.checkers;
7393 redirectstderr ();
7395 init (cr, cw) (
7396 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7397 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7398 !Config.fontpath, !trimcachepath,
7399 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7401 state.sr <- sr;
7402 state.sw <- sw;
7403 state.text <- "Opening " ^ (mbtoutf8 state.path);
7404 reshape winw winh;
7405 opendoc state.path state.password;
7406 state.uioh <- uioh;
7408 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7409 let optrfd =
7410 ref (
7411 if String.length !rcmdpath > 0
7412 then remoteopen !rcmdpath
7413 else None
7417 let rec loop deadline =
7418 let r =
7419 match state.errfd with
7420 | None -> [state.sr; state.wsfd]
7421 | Some fd -> [state.sr; state.wsfd; fd]
7423 let r =
7424 match !optrfd with
7425 | None -> r
7426 | Some fd -> fd :: r
7428 if state.redisplay
7429 then (
7430 state.redisplay <- false;
7431 display ();
7433 let timeout =
7434 let now = now () in
7435 if deadline > now
7436 then (
7437 if deadline = infinity
7438 then ~-.1.0
7439 else max 0.0 (deadline -. now)
7441 else 0.0
7443 let r, _, _ =
7444 try Unix.select r [] [] timeout
7445 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7447 begin match r with
7448 | [] ->
7449 state.ghyll None;
7450 let newdeadline =
7451 if state.ghyll == noghyll
7452 then
7453 match state.autoscroll with
7454 | Some step when step != 0 ->
7455 let y = state.y + step in
7456 let y =
7457 if y < 0
7458 then state.maxy
7459 else if y >= state.maxy then 0 else y
7461 gotoy y;
7462 if state.mode = View
7463 then state.text <- "";
7464 deadline +. 0.01
7465 | _ -> infinity
7466 else deadline +. 0.01
7468 loop newdeadline
7470 | l ->
7471 let rec checkfds = function
7472 | [] -> ()
7473 | fd :: rest when fd = state.sr ->
7474 let cmd = readcmd state.sr in
7475 act cmd;
7476 checkfds rest
7478 | fd :: rest when fd = state.wsfd ->
7479 Wsi.readresp fd;
7480 checkfds rest
7482 | fd :: rest when Some fd = !optrfd ->
7483 begin match remote fd with
7484 | None -> optrfd := remoteopen !rcmdpath;
7485 | opt -> optrfd := opt
7486 end;
7487 checkfds rest
7489 | fd :: rest ->
7490 let s = String.create 80 in
7491 let n = tempfailureretry (Unix.read fd s 0) 80 in
7492 if conf.redirectstderr
7493 then (
7494 Buffer.add_substring state.errmsgs s 0 n;
7495 state.newerrmsgs <- true;
7496 state.redisplay <- true;
7498 else (
7499 prerr_string (String.sub s 0 n);
7500 flush stderr;
7502 checkfds rest
7504 checkfds l;
7505 let newdeadline =
7506 let deadline1 =
7507 if deadline = infinity
7508 then now () +. 0.01
7509 else deadline
7511 match state.autoscroll with
7512 | Some step when step != 0 -> deadline1
7513 | _ -> if state.ghyll == noghyll then infinity else deadline1
7515 loop newdeadline
7516 end;
7519 loop infinity;
7520 with Quit ->
7521 Config.save ();