Only center when zoomed
[llpp.git] / main.ml
blobc6f4c231c12b86834b68bdbbf4e11d8cb0f19926
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * fitmodel * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and trimmargins = bool
29 and interpagespace = int
30 and texcount = int
31 and sliceheight = int
32 and gen = int
33 and top = float
34 and dtop = float
35 and fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
42 and fitmodel = | FitWidth | FitProportional | FitPage
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable savebmarks : bool
316 ; mutable fitmodel : fitmodel
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 method eformsgs : bool
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 method eformsgs = false
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor * string) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
484 ; mutable origin : string
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; cwinw = 900
515 ; cwinh = 900
516 ; savebmarks = true
517 ; fitmodel = FitProportional
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let geturl s =
602 let colonpos = try String.index s ':' with Not_found -> -1 in
603 let len = String.length s in
604 if colonpos >= 0 && colonpos + 3 < len
605 then (
606 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
607 then
608 let schemestartpos =
609 try String.rindex_from s colonpos ' '
610 with Not_found -> -1
612 let scheme =
613 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
615 match scheme with
616 | "http" | "ftp" | "mailto" ->
617 let epos =
618 try String.index_from s colonpos ' '
619 with Not_found -> len
621 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
622 | _ -> ""
623 else ""
625 else ""
628 let gotouri uri =
629 if String.length conf.urilauncher = 0
630 then print_endline uri
631 else (
632 let url = geturl uri in
633 if String.length url = 0
634 then print_endline uri
635 else
636 let re = Str.regexp "%s" in
637 let command = Str.global_replace re url conf.urilauncher in
638 try popen command []
639 with exn ->
640 Printf.eprintf
641 "failed to execute `%s': %s\n" command (exntos exn);
642 flush stderr;
646 let version () =
647 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
648 (platform_to_string platform) Sys.word_size Sys.ocaml_version
651 let makehelp () =
652 let strings = version () :: "" :: Help.keys in
653 Array.of_list (
654 List.map (fun s ->
655 let url = geturl s in
656 if String.length url > 0
657 then (s, 0, Action (fun u -> gotouri url; u))
658 else (s, 0, Noaction)
659 ) strings);
662 let noghyll _ = ();;
663 let firstgeomcmds = "", [];;
664 let noreprf () = ();;
666 let state =
667 { sr = Unix.stdin
668 ; sw = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; errfd = None
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; scrollw = 0
678 ; hscrollh = 0
679 ; anchor = emptyanchor
680 ; ranchors = []
681 ; layout = []
682 ; maxy = max_int
683 ; tilelru = Queue.create ()
684 ; pagemap = Hashtbl.create 10
685 ; tilemap = Hashtbl.create 10
686 ; pdims = []
687 ; pagecount = 0
688 ; currently = Idle
689 ; mstate = Mnone
690 ; rects = []
691 ; rects1 = []
692 ; text = ""
693 ; mode = View
694 ; winstate = []
695 ; searchpattern = ""
696 ; outlines = [||]
697 ; bookmarks = []
698 ; path = ""
699 ; password = ""
700 ; nameddest = ""
701 ; geomcmds = firstgeomcmds
702 ; hists =
703 { nav = cbnew 10 emptyanchor
704 ; pat = cbnew 10 ""
705 ; pag = cbnew 10 ""
706 ; sel = cbnew 10 ""
708 ; memused = 0
709 ; gen = 0
710 ; throttle = None
711 ; autoscroll = None
712 ; ghyll = noghyll
713 ; help = makehelp ()
714 ; docinfo = []
715 ; texid = None
716 ; prevzoom = 1.0
717 ; progress = -1.0
718 ; uioh = nouioh
719 ; redisplay = true
720 ; mpos = (-1, -1)
721 ; keystate = KSnone
722 ; glinks = false
723 ; prevcolumns = None
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
727 ; origin = ""
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
754 flush stderr;
758 module Ne = struct
759 type 'a t = | Res of 'a | Exn of exn;;
761 let pipe () =
762 try Res (Unix.pipe ())
763 with exn -> Exn exn
766 let clo fd f =
767 try tempfailureretry Unix.close fd
768 with exn -> f (exntos exn)
771 let dup fd =
772 try Res (tempfailureretry Unix.dup fd)
773 with exn -> Exn exn
776 let dup2 fd1 fd2 =
777 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
778 with exn -> Exn exn
780 end;;
782 let redirectstderr () =
783 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
784 if conf.redirectstderr
785 then
786 match Ne.pipe () with
787 | Ne.Exn exn ->
788 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
790 | Ne.Res (r, w) ->
791 begin match Ne.dup Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup stderr: %s" (exntos exn);
794 Ne.clo r (clofail "pipe/r");
795 Ne.clo w (clofail "pipe/w");
797 | Ne.Res dupstderr ->
798 begin match Ne.dup2 w Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 to stderr: %s" (exntos exn);
801 Ne.clo dupstderr (clofail "stderr duplicate");
802 Ne.clo r (clofail "redir pipe/r");
803 Ne.clo w (clofail "redir pipe/w");
805 | Ne.Res () ->
806 state.stderr <- dupstderr;
807 state.errfd <- Some r;
808 end;
810 else (
811 state.newerrmsgs <- false;
812 begin match state.errfd with
813 | Some fd ->
814 begin match Ne.dup2 state.stderr Unix.stderr with
815 | Ne.Exn exn ->
816 dolog "failed to dup2 original stderr: %s" (exntos exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let selstring s =
903 match Ne.pipe () with
904 | Ne.Exn exn ->
905 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
906 | Ne.Res (r, w) ->
907 let popened =
908 try popen conf.selcmd [r, 0; w, -1]; true
909 with exn ->
910 showtext '!'
911 (Printf.sprintf "failed to execute %s: %s"
912 conf.selcmd (exntos exn));
913 false
915 let clo cap fd =
916 Ne.clo fd (fun msg ->
917 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
920 if popened
921 then
922 (try
923 let l = String.length s in
924 let n = tempfailureretry (Unix.write w s 0) l in
925 if n != l
926 then
927 showtext '!'
928 (Printf.sprintf
929 "failed to write %d characters to sel pipe, wrote %d"
932 with exn ->
933 showtext '!'
934 (Printf.sprintf "failed to write to sel pipe: %s"
935 (exntos exn)
938 else dolog "%s" s;
939 clo "pipe/r" r;
940 clo "pipe/w" w;
943 let undertext = function
944 | Unone -> "none"
945 | Ulinkuri s -> s
946 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
947 | Utext s -> "font: " ^ s
948 | Uunexpected s -> "unexpected: " ^ s
949 | Ulaunch s -> "launch: " ^ s
950 | Unamed s -> "named: " ^ s
951 | Uremote (filename, pageno) ->
952 Printf.sprintf "%s: page %d" filename (pageno+1)
955 let updateunder x y =
956 match getunder x y with
957 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
958 | Ulinkuri uri ->
959 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
960 Wsi.setcursor Wsi.CURSOR_INFO
961 | Ulinkgoto (pageno, _) ->
962 if conf.underinfo
963 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
964 Wsi.setcursor Wsi.CURSOR_INFO
965 | Utext s ->
966 if conf.underinfo then showtext 'f' ("ont: " ^ s);
967 Wsi.setcursor Wsi.CURSOR_TEXT
968 | Uunexpected s ->
969 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
970 Wsi.setcursor Wsi.CURSOR_INHERIT
971 | Ulaunch s ->
972 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
973 Wsi.setcursor Wsi.CURSOR_INHERIT
974 | Unamed s ->
975 if conf.underinfo then showtext 'n' ("amed: " ^ s);
976 Wsi.setcursor Wsi.CURSOR_INHERIT
977 | Uremote (filename, pageno) ->
978 if conf.underinfo then showtext 'r'
979 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
980 Wsi.setcursor Wsi.CURSOR_INFO
983 let showlinktype under =
984 if conf.underinfo
985 then
986 match under with
987 | Unone -> ()
988 | under ->
989 let s = undertext under in
990 showtext ' ' s
993 let addchar s c =
994 let b = Buffer.create (String.length s + 1) in
995 Buffer.add_string b s;
996 Buffer.add_char b c;
997 Buffer.contents b;
1000 let colorspace_of_string s =
1001 match String.lowercase s with
1002 | "rgb" -> Rgb
1003 | "bgr" -> Bgr
1004 | "gray" -> Gray
1005 | _ -> failwith "invalid colorspace"
1008 let int_of_colorspace = function
1009 | Rgb -> 0
1010 | Bgr -> 1
1011 | Gray -> 2
1014 let colorspace_of_int = function
1015 | 0 -> Rgb
1016 | 1 -> Bgr
1017 | 2 -> Gray
1018 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1021 let colorspace_to_string = function
1022 | Rgb -> "rgb"
1023 | Bgr -> "bgr"
1024 | Gray -> "gray"
1027 let fitmodel_of_string s =
1028 match String.lowercase s with
1029 | "width" -> FitWidth
1030 | "proportional" -> FitProportional
1031 | "page" -> FitPage
1032 | _ -> failwith "invalid fit model"
1035 let int_of_fitmodel = function
1036 | FitWidth -> 0
1037 | FitProportional -> 1
1038 | FitPage -> 2
1041 let fitmodel_of_int = function
1042 | 0 -> FitWidth
1043 | 1 -> FitProportional
1044 | 2 -> FitPage
1045 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1048 let fitmodel_to_string = function
1049 | FitWidth -> "width"
1050 | FitProportional -> "proportional"
1051 | FitPage -> "page"
1054 let intentry_with_suffix text key =
1055 let c =
1056 if key >= 32 && key < 127
1057 then Char.chr key
1058 else '\000'
1060 match Char.lowercase c with
1061 | '0' .. '9' ->
1062 let text = addchar text c in
1063 TEcont text
1065 | 'k' | 'm' | 'g' ->
1066 let text = addchar text c in
1067 TEcont text
1069 | _ ->
1070 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1071 TEcont text
1074 let multicolumns_to_string (n, a, b) =
1075 if a = 0 && b = 0
1076 then Printf.sprintf "%d" n
1077 else Printf.sprintf "%d,%d,%d" n a b;
1080 let multicolumns_of_string s =
1082 (int_of_string s, 0, 0)
1083 with _ ->
1084 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1085 if a > 1 || b > 1
1086 then failwith "subtly broken"; (n, a, b)
1090 let readcmd fd =
1091 let s = "xxxx" in
1092 let n = tempfailureretry (Unix.read fd s 0) 4 in
1093 if n != 4 then failwith "incomplete read(len)";
1094 let len = 0
1095 lor (Char.code s.[0] lsl 24)
1096 lor (Char.code s.[1] lsl 16)
1097 lor (Char.code s.[2] lsl 8)
1098 lor (Char.code s.[3] lsl 0)
1100 let s = String.create len in
1101 let n = tempfailureretry (Unix.read fd s 0) len in
1102 if n != len then failwith "incomplete read(data)";
1106 let btod b = if b then 1 else 0;;
1108 let wcmd fmt =
1109 let b = Buffer.create 16 in
1110 Buffer.add_string b "llll";
1111 Printf.kbprintf
1112 (fun b ->
1113 let s = Buffer.contents b in
1114 let n = String.length s in
1115 let len = n - 4 in
1116 (* dolog "wcmd %S" (String.sub s 4 len); *)
1117 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1118 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1119 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1120 s.[3] <- Char.chr (len land 0xff);
1121 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1122 if n' != n then failwith "write failed";
1123 ) b fmt;
1126 let calcips h =
1127 let d = state.winh - h in
1128 max conf.interpagespace ((d + 1) / 2)
1131 let rowyh (c, coverA, coverB) b n =
1132 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1133 then
1134 let _, _, vy, (_, _, h, _) = b.(n) in
1135 (vy, h)
1136 else
1137 let n' = n - coverA in
1138 let d = n' mod c in
1139 let s = n - d in
1140 let e = min state.pagecount (s + c) in
1141 let rec find m miny maxh = if m = e then miny, maxh else
1142 let _, _, y, (_, _, h, _) = b.(m) in
1143 let miny = min miny y in
1144 let maxh = max maxh h in
1145 find (m+1) miny maxh
1146 in find s max_int 0
1149 let calcheight () =
1150 match conf.columns with
1151 | Cmulti ((_, _, _) as cl, b) ->
1152 if Array.length b > 0
1153 then
1154 let y, h = rowyh cl b (Array.length b - 1) in
1155 y + h + (if conf.presentation then calcips h else 0)
1156 else 0
1157 | Csingle b ->
1158 if Array.length b > 0
1159 then
1160 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1161 y + h + (if conf.presentation then calcips h else 0)
1162 else 0
1163 | Csplit (_, b) ->
1164 if Array.length b > 0
1165 then
1166 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1167 y + h
1168 else 0
1171 let getpageyh pageno =
1172 let pageno = bound pageno 0 (state.pagecount-1) in
1173 match conf.columns with
1174 | Csingle b ->
1175 if Array.length b = 0
1176 then 0, 0
1177 else
1178 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1179 let y =
1180 if conf.presentation
1181 then y - calcips h
1182 else y
1184 y, h
1185 | Cmulti (cl, b) ->
1186 if Array.length b = 0
1187 then 0, 0
1188 else
1189 let y, h = rowyh cl b pageno in
1190 let y =
1191 if conf.presentation
1192 then y - calcips h
1193 else y
1195 y, h
1196 | Csplit (c, b) ->
1197 if Array.length b = 0
1198 then 0, 0
1199 else
1200 let n = pageno*c in
1201 let (_, _, y, (_, _, h, _)) = b.(n) in
1202 y, h
1205 let getpagedim pageno =
1206 let rec f ppdim l =
1207 match l with
1208 | (n, _, _, _) as pdim :: rest ->
1209 if n >= pageno
1210 then (if n = pageno then pdim else ppdim)
1211 else f pdim rest
1213 | [] -> ppdim
1215 f (-1, -1, -1, -1) state.pdims
1218 let getpagey pageno = fst (getpageyh pageno);;
1220 let nogeomcmds cmds =
1221 match cmds with
1222 | s, [] -> String.length s = 0
1223 | _ -> false
1226 let page_of_y y =
1227 let ((c, coverA, coverB) as cl), b =
1228 match conf.columns with
1229 | Csingle b -> (1, 0, 0), b
1230 | Cmulti (c, b) -> c, b
1231 | Csplit (_, b) -> (1, 0, 0), b
1233 if Array.length b = 0
1234 then -1
1235 else
1236 let rec bsearch nmin nmax =
1237 if nmin > nmax
1238 then bound nmin 0 (state.pagecount-1)
1239 else
1240 let n = (nmax + nmin) / 2 in
1241 let vy, h = rowyh cl b n in
1242 let y0, y1 =
1243 if conf.presentation
1244 then
1245 let ips = calcips h in
1246 let y0 = vy - ips in
1247 let y1 = vy + h + ips in
1248 y0, y1
1249 else (
1250 if n = 0
1251 then 0, vy + h + conf.interpagespace
1252 else
1253 let y0 = vy - conf.interpagespace in
1254 y0, y0 + h + conf.interpagespace
1257 if y >= y0 && y < y1
1258 then (
1259 if c = 1
1260 then n
1261 else (
1262 if n > coverA
1263 then
1264 if n < state.pagecount - coverB
1265 then ((n-coverA)/c)*c + coverA
1266 else n
1267 else n
1270 else (
1271 if y > y0
1272 then bsearch (n+1) nmax
1273 else bsearch nmin (n-1)
1276 let r = bsearch 0 (state.pagecount-1) in
1280 let layoutN ((columns, coverA, coverB), b) y sh =
1281 let sh = sh - state.hscrollh in
1282 let rec fold accu n =
1283 if n = Array.length b
1284 then accu
1285 else
1286 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1287 if (vy - y) > sh &&
1288 (n = coverA - 1
1289 || n = state.pagecount - coverB
1290 || (n - coverA) mod columns = columns - 1)
1291 then accu
1292 else
1293 let accu =
1294 if vy + h > y
1295 then
1296 let pagey = max 0 (y - vy) in
1297 let pagedispy = if pagey > 0 then 0 else vy - y in
1298 let pagedispx, pagex =
1299 let pdx =
1300 if n = coverA - 1 || n = state.pagecount - coverB
1301 then state.x + (state.winw - state.scrollw - w) / 2
1302 else dx + xoff + state.x
1304 if pdx < 0
1305 then 0, -pdx
1306 else pdx, 0
1308 let pagevw =
1309 let vw = state.winw - state.scrollw - pagedispx in
1310 let pw = w - pagex in
1311 min vw pw
1313 let pagevh = min (h - pagey) (sh - pagedispy) in
1314 if pagevw > 0 && pagevh > 0
1315 then
1316 let e =
1317 { pageno = n
1318 ; pagedimno = pdimno
1319 ; pagew = w
1320 ; pageh = h
1321 ; pagex = pagex
1322 ; pagey = pagey
1323 ; pagevw = pagevw
1324 ; pagevh = pagevh
1325 ; pagedispx = pagedispx
1326 ; pagedispy = pagedispy
1327 ; pagecol = 0
1330 e :: accu
1331 else
1332 accu
1333 else
1334 accu
1336 fold accu (n+1)
1338 List.rev (fold [] (page_of_y y));
1341 let layoutS (columns, b) y sh =
1342 let sh = sh - state.hscrollh in
1343 let rec fold accu n =
1344 if n = Array.length b
1345 then accu
1346 else
1347 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1348 if (vy - y) > sh
1349 then accu
1350 else
1351 let accu =
1352 if vy + pageh > y
1353 then
1354 let x = xoff + state.x in
1355 let pagey = max 0 (y - vy) in
1356 let pagedispy = if pagey > 0 then 0 else vy - y in
1357 let pagedispx, pagex =
1358 if px = 0
1359 then (
1360 if x < 0
1361 then 0, -x
1362 else x, 0
1364 else (
1365 let px = px - x in
1366 if px < 0
1367 then -px, 0
1368 else 0, px
1371 let pagecolw = pagew/columns in
1372 let pagedispx =
1373 if pagecolw < state.winw
1374 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1375 else pagedispx
1377 let pagevw =
1378 let vw = state.winw - pagedispx - state.scrollw in
1379 let pw = pagew - pagex in
1380 min vw pw
1382 let pagevw = min pagevw pagecolw in
1383 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1384 if pagevw > 0 && pagevh > 0
1385 then
1386 let e =
1387 { pageno = n/columns
1388 ; pagedimno = pdimno
1389 ; pagew = pagew
1390 ; pageh = pageh
1391 ; pagex = pagex
1392 ; pagey = pagey
1393 ; pagevw = pagevw
1394 ; pagevh = pagevh
1395 ; pagedispx = pagedispx
1396 ; pagedispy = pagedispy
1397 ; pagecol = n mod columns
1400 e :: accu
1401 else
1402 accu
1403 else
1404 accu
1406 fold accu (n+1)
1408 List.rev (fold [] 0)
1411 let layout y sh =
1412 if nogeomcmds state.geomcmds
1413 then
1414 match conf.columns with
1415 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1416 | Cmulti c -> layoutN c y sh
1417 | Csplit s -> layoutS s y sh
1418 else []
1421 let clamp incr =
1422 let y = state.y + incr in
1423 let y = max 0 y in
1424 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1428 let itertiles l f =
1429 let tilex = l.pagex mod conf.tilew in
1430 let tiley = l.pagey mod conf.tileh in
1432 let col = l.pagex / conf.tilew in
1433 let row = l.pagey / conf.tileh in
1435 let rec rowloop row y0 dispy h =
1436 if h = 0
1437 then ()
1438 else (
1439 let dh = conf.tileh - y0 in
1440 let dh = min h dh in
1441 let rec colloop col x0 dispx w =
1442 if w = 0
1443 then ()
1444 else (
1445 let dw = conf.tilew - x0 in
1446 let dw = min w dw in
1448 f col row dispx dispy x0 y0 dw dh;
1449 colloop (col+1) 0 (dispx+dw) (w-dw)
1452 colloop col tilex l.pagedispx l.pagevw;
1453 rowloop (row+1) 0 (dispy+dh) (h-dh)
1456 if l.pagevw > 0 && l.pagevh > 0
1457 then rowloop row tiley l.pagedispy l.pagevh;
1460 let gettileopaque l col row =
1461 let key =
1462 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1464 try Some (Hashtbl.find state.tilemap key)
1465 with Not_found -> None
1468 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1469 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1470 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1473 let drawtiles l color =
1474 GlDraw.color color;
1475 let f col row x y tilex tiley w h =
1476 match gettileopaque l col row with
1477 | Some (opaque, _, t) ->
1478 let params = x, y, w, h, tilex, tiley in
1479 if conf.invert
1480 then (
1481 Gl.enable `blend;
1482 GlFunc.blend_func `zero `one_minus_src_color;
1484 drawtile params opaque;
1485 if conf.invert
1486 then Gl.disable `blend;
1487 if conf.debug
1488 then (
1489 let s = Printf.sprintf
1490 "%d[%d,%d] %f sec"
1491 l.pageno col row t
1493 let w = measurestr fstate.fontsize s in
1494 GlMisc.push_attrib [`current];
1495 GlDraw.color (0.0, 0.0, 0.0);
1496 GlDraw.rect
1497 (float (x-2), float (y-2))
1498 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1499 GlDraw.color (1.0, 1.0, 1.0);
1500 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1501 GlMisc.pop_attrib ();
1504 | _ ->
1505 let w =
1506 let lw = state.winw - state.scrollw - x in
1507 min lw w
1508 and h =
1509 let lh = state.winh - y in
1510 min lh h
1512 begin match state.texid with
1513 | Some id ->
1514 Gl.enable `texture_2d;
1515 GlTex.bind_texture `texture_2d id;
1516 let x0 = float x
1517 and y0 = float y
1518 and x1 = float (x+w)
1519 and y1 = float (y+h) in
1521 let tw = float w /. 16.0
1522 and th = float h /. 16.0 in
1523 let tx0 = float tilex /. 16.0
1524 and ty0 = float tiley /. 16.0 in
1525 let tx1 = tx0 +. tw
1526 and ty1 = ty0 +. th in
1527 GlDraw.begins `quads;
1528 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1529 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1530 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1531 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1532 GlDraw.ends ();
1534 Gl.disable `texture_2d;
1535 | None ->
1536 GlDraw.color (1.0, 1.0, 1.0);
1537 GlDraw.rect
1538 (float x, float y)
1539 (float (x+w), float (y+h));
1540 end;
1541 if w > 128 && h > fstate.fontsize + 10
1542 then (
1543 GlDraw.color (0.0, 0.0, 0.0);
1544 let c, r =
1545 if conf.verbose
1546 then (col*conf.tilew, row*conf.tileh)
1547 else col, row
1549 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1551 GlDraw.color color;
1553 itertiles l f
1556 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1558 let tilevisible1 l x y =
1559 let ax0 = l.pagex
1560 and ax1 = l.pagex + l.pagevw
1561 and ay0 = l.pagey
1562 and ay1 = l.pagey + l.pagevh in
1564 let bx0 = x
1565 and by0 = y in
1566 let bx1 = min (bx0 + conf.tilew) l.pagew
1567 and by1 = min (by0 + conf.tileh) l.pageh in
1569 let rx0 = max ax0 bx0
1570 and ry0 = max ay0 by0
1571 and rx1 = min ax1 bx1
1572 and ry1 = min ay1 by1 in
1574 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1575 nonemptyintersection
1578 let tilevisible layout n x y =
1579 let rec findpageinlayout m = function
1580 | l :: rest when l.pageno = n ->
1581 tilevisible1 l x y || (
1582 match conf.columns with
1583 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1584 | _ -> false
1586 | _ :: rest -> findpageinlayout 0 rest
1587 | [] -> false
1589 findpageinlayout 0 layout;
1592 let tileready l x y =
1593 tilevisible1 l x y &&
1594 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1597 let tilepage n p layout =
1598 let rec loop = function
1599 | l :: rest ->
1600 if l.pageno = n
1601 then
1602 let f col row _ _ _ _ _ _ =
1603 if state.currently = Idle
1604 then
1605 match gettileopaque l col row with
1606 | Some _ -> ()
1607 | None ->
1608 let x = col*conf.tilew
1609 and y = row*conf.tileh in
1610 let w =
1611 let w = l.pagew - x in
1612 min w conf.tilew
1614 let h =
1615 let h = l.pageh - y in
1616 min h conf.tileh
1618 let pbo =
1619 if conf.usepbo
1620 then getpbo w h conf.colorspace
1621 else "0"
1623 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1624 state.currently <-
1625 Tiling (
1626 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1627 conf.tilew, conf.tileh
1630 itertiles l f;
1631 else
1632 loop rest
1634 | [] -> ()
1636 if nogeomcmds state.geomcmds
1637 then loop layout;
1640 let preloadlayout y =
1641 let y = if y < state.winh then 0 else y - state.winh in
1642 let h = state.winh*3 in
1643 layout y h;
1646 let load pages =
1647 let rec loop pages =
1648 if state.currently != Idle
1649 then ()
1650 else
1651 match pages with
1652 | l :: rest ->
1653 begin match getopaque l.pageno with
1654 | None ->
1655 wcmd "page %d %d" l.pageno l.pagedimno;
1656 state.currently <- Loading (l, state.gen);
1657 | Some opaque ->
1658 tilepage l.pageno opaque pages;
1659 loop rest
1660 end;
1661 | _ -> ()
1663 if nogeomcmds state.geomcmds
1664 then loop pages
1667 let preload pages =
1668 load pages;
1669 if conf.preload && state.currently = Idle
1670 then load (preloadlayout state.y);
1673 let layoutready layout =
1674 let rec fold all ls =
1675 all && match ls with
1676 | l :: rest ->
1677 let seen = ref false in
1678 let allvisible = ref true in
1679 let foo col row _ _ _ _ _ _ =
1680 seen := true;
1681 allvisible := !allvisible &&
1682 begin match gettileopaque l col row with
1683 | Some _ -> true
1684 | None -> false
1687 itertiles l foo;
1688 fold (!seen && !allvisible) rest
1689 | [] -> true
1691 let alltilesvisible = fold true layout in
1692 alltilesvisible;
1695 let gotoy y =
1696 let y = bound y 0 state.maxy in
1697 let y, layout, proceed =
1698 match conf.maxwait with
1699 | Some time when state.ghyll == noghyll ->
1700 begin match state.throttle with
1701 | None ->
1702 let layout = layout y state.winh in
1703 let ready = layoutready layout in
1704 if not ready
1705 then (
1706 load layout;
1707 state.throttle <- Some (layout, y, now ());
1709 else G.postRedisplay "gotoy showall (None)";
1710 y, layout, ready
1711 | Some (_, _, started) ->
1712 let dt = now () -. started in
1713 if dt > time
1714 then (
1715 state.throttle <- None;
1716 let layout = layout y state.winh in
1717 load layout;
1718 G.postRedisplay "maxwait";
1719 y, layout, true
1721 else -1, [], false
1724 | _ ->
1725 let layout = layout y state.winh in
1726 if not !wtmode || layoutready layout
1727 then G.postRedisplay "gotoy ready";
1728 y, layout, true
1730 if proceed
1731 then (
1732 state.y <- y;
1733 state.layout <- layout;
1734 begin match state.mode with
1735 | LinkNav (Ltexact (pageno, linkno)) ->
1736 let rec loop = function
1737 | [] ->
1738 state.mode <- LinkNav (Ltgendir 0)
1739 | l :: _ when l.pageno = pageno ->
1740 begin match getopaque pageno with
1741 | None ->
1742 state.mode <- LinkNav (Ltgendir 0)
1743 | Some opaque ->
1744 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1745 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1746 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1747 then state.mode <- LinkNav (Ltgendir 0)
1749 | _ :: rest -> loop rest
1751 loop layout
1752 | _ -> ()
1753 end;
1754 begin match state.mode with
1755 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1756 if not (pagevisible layout pageno)
1757 then (
1758 match state.layout with
1759 | [] -> ()
1760 | l :: _ ->
1761 state.mode <- Birdseye (
1762 conf, leftx, l.pageno, hooverpageno, anchor
1765 | LinkNav (Ltgendir dir as lt) ->
1766 let linknav =
1767 let rec loop = function
1768 | [] -> lt
1769 | l :: rest ->
1770 match getopaque l.pageno with
1771 | None -> loop rest
1772 | Some opaque ->
1773 let link =
1774 let ld =
1775 if dir = 0
1776 then LDfirstvisible (l.pagex, l.pagey, dir)
1777 else (
1778 if dir > 0 then LDfirst else LDlast
1781 findlink opaque ld
1783 match link with
1784 | Lnotfound -> loop rest
1785 | Lfound n ->
1786 showlinktype (getlink opaque n);
1787 Ltexact (l.pageno, n)
1789 loop state.layout
1791 state.mode <- LinkNav linknav
1792 | _ -> ()
1793 end;
1794 preload layout;
1796 state.ghyll <- noghyll;
1797 if conf.updatecurs
1798 then (
1799 let mx, my = state.mpos in
1800 updateunder mx my;
1804 let conttiling pageno opaque =
1805 tilepage pageno opaque
1806 (if conf.preload then preloadlayout state.y else state.layout)
1809 let gotoy_and_clear_text y =
1810 if not conf.verbose then state.text <- "";
1811 gotoy y;
1814 let getanchor1 l =
1815 let top =
1816 let coloff = l.pagecol * l.pageh in
1817 float (l.pagey + coloff) /. float l.pageh
1819 let dtop =
1820 if l.pagedispy = 0
1821 then
1823 else
1824 if conf.presentation
1825 then float l.pagedispy /. float (calcips l.pageh)
1826 else float l.pagedispy /. float conf.interpagespace
1828 (l.pageno, top, dtop)
1831 let getanchor () =
1832 match state.layout with
1833 | l :: _ -> getanchor1 l
1834 | [] ->
1835 let n = page_of_y state.y in
1836 if n = -1
1837 then state.anchor
1838 else
1839 let y, h = getpageyh n in
1840 let dy = y - state.y in
1841 let dtop =
1842 if conf.presentation
1843 then
1844 let ips = calcips h in
1845 float (dy + ips) /. float ips
1846 else
1847 float dy /. float conf.interpagespace
1849 (n, 0.0, dtop)
1852 let getanchory (n, top, dtop) =
1853 let y, h = getpageyh n in
1854 if conf.presentation
1855 then
1856 let ips = calcips h in
1857 y + truncate (top*.float h -. dtop*.float ips) + ips;
1858 else
1859 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1862 let gotoanchor anchor =
1863 gotoy (getanchory anchor);
1866 let addnav () =
1867 cbput state.hists.nav (getanchor ());
1870 let getnav dir =
1871 let anchor = cbgetc state.hists.nav dir in
1872 getanchory anchor;
1875 let gotoghyll y =
1876 let scroll f n a b =
1877 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1878 let snake f a b =
1879 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1880 if f < a
1881 then s (float f /. float a)
1882 else (
1883 if f > b
1884 then 1.0 -. s ((float (f-b) /. float (n-b)))
1885 else 1.0
1888 snake f a b
1889 and summa f n a b =
1890 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1891 let iv x = x**3.-.0.5*.x**4. in
1892 let iv1 = iv f in
1893 let ins = float a *. iv1
1894 and outs = float (n-b) *. iv1 in
1895 let ones = b - a in
1896 ins +. outs +. float ones
1898 let rec set (_N, _A, _B) y sy =
1899 let sum = summa 1.0 _N _A _B in
1900 let dy = float (y - sy) in
1901 state.ghyll <- (
1902 let rec gf n y1 o =
1903 if n >= _N
1904 then state.ghyll <- noghyll
1905 else
1906 let go n =
1907 let s = scroll n _N _A _B in
1908 let y1 = y1 +. ((s *. dy) /. sum) in
1909 gotoy_and_clear_text (truncate y1);
1910 state.ghyll <- gf (n+1) y1;
1912 match o with
1913 | None -> go n
1914 | Some y' -> set (_N/2, 1, 1) y' state.y
1916 gf 0 (float state.y)
1919 match conf.ghyllscroll with
1920 | None ->
1921 gotoy_and_clear_text y
1922 | Some nab ->
1923 if state.ghyll == noghyll
1924 then set nab y state.y
1925 else state.ghyll (Some y)
1928 let gotopage n top =
1929 let y, h = getpageyh n in
1930 let y = y + (truncate (top *. float h)) in
1931 gotoghyll y
1934 let gotopage1 n top =
1935 let y = getpagey n in
1936 let y = y + top in
1937 gotoghyll y
1940 let invalidate s f =
1941 state.layout <- [];
1942 state.pdims <- [];
1943 state.rects <- [];
1944 state.rects1 <- [];
1945 match state.geomcmds with
1946 | ps, [] when String.length ps = 0 ->
1947 f ();
1948 state.geomcmds <- s, [];
1950 | ps, [] ->
1951 state.geomcmds <- ps, [s, f];
1953 | ps, (s', _) :: rest when s' = s ->
1954 state.geomcmds <- ps, ((s, f) :: rest);
1956 | ps, cmds ->
1957 state.geomcmds <- ps, ((s, f) :: cmds);
1960 let flushpages () =
1961 Hashtbl.iter (fun _ opaque ->
1962 wcmd "freepage %s" opaque;
1963 ) state.pagemap;
1964 Hashtbl.clear state.pagemap;
1967 let flushtiles () =
1968 if not (Queue.is_empty state.tilelru)
1969 then (
1970 Queue.iter (fun (k, p, s) ->
1971 wcmd "freetile %s" p;
1972 state.memused <- state.memused - s;
1973 Hashtbl.remove state.tilemap k;
1974 ) state.tilelru;
1975 state.uioh#infochanged Memused;
1976 Queue.clear state.tilelru;
1978 load state.layout;
1981 let stateh h =
1982 let h = truncate (float h*.conf.zoom) in
1983 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1984 h - d
1987 let opendoc path password =
1988 state.path <- path;
1989 state.password <- password;
1990 state.gen <- state.gen + 1;
1991 state.docinfo <- [];
1993 flushpages ();
1994 setaalevel conf.aalevel;
1995 let titlepath =
1996 if String.length state.origin = 0
1997 then path
1998 else state.origin
2000 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2001 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2002 invalidate "reqlayout"
2003 (fun () ->
2004 wcmd "reqlayout %d %d %d %s\000"
2005 conf.angle (int_of_fitmodel conf.fitmodel)
2006 (stateh state.winh) state.nameddest
2010 let reload () =
2011 state.anchor <- getanchor ();
2012 opendoc state.path state.password;
2015 let scalecolor c =
2016 let c = c *. conf.colorscale in
2017 (c, c, c);
2020 let scalecolor2 (r, g, b) =
2021 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2024 let docolumns = function
2025 | Csingle _ ->
2026 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2027 let rec loop pageno pdimno pdim y ph pdims =
2028 if pageno = state.pagecount
2029 then ()
2030 else
2031 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2032 match pdims with
2033 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2034 pdimno+1, pdim, rest
2035 | _ ->
2036 pdimno, pdim, pdims
2038 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2039 let y = y +
2040 (if conf.presentation
2041 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2042 else (if pageno = 0 then 0 else conf.interpagespace)
2045 a.(pageno) <- (pdimno, x, y, pdim);
2046 loop (pageno+1) pdimno pdim (y + h) h pdims
2048 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2049 conf.columns <- Csingle a;
2051 | Cmulti ((columns, coverA, coverB), _) ->
2052 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2053 let rec loop pageno pdimno pdim x y rowh pdims =
2054 let rec fixrow m = if m = pageno then () else
2055 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2056 if h < rowh
2057 then (
2058 let y = y + (rowh - h) / 2 in
2059 a.(m) <- (pdimno, x, y, pdim);
2061 fixrow (m+1)
2063 if pageno = state.pagecount
2064 then fixrow (((pageno - 1) / columns) * columns)
2065 else
2066 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2067 match pdims with
2068 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2069 pdimno+1, pdim, rest
2070 | _ ->
2071 pdimno, pdim, pdims
2073 let x, y, rowh' =
2074 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2075 then (
2076 let x = (state.winw - state.scrollw - w) / 2 in
2077 let ips =
2078 if conf.presentation then calcips h else conf.interpagespace in
2079 x, y + ips + rowh, h
2081 else (
2082 if (pageno - coverA) mod columns = 0
2083 then (
2084 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2085 let y =
2086 if conf.presentation
2087 then
2088 let ips = calcips h in
2089 y + (if pageno = 0 then 0 else calcips rowh + ips)
2090 else
2091 y + (if pageno = 0 then 0 else conf.interpagespace)
2093 x, y + rowh, h
2095 else x, y, max rowh h
2098 let y =
2099 if pageno > 1 && (pageno - coverA) mod columns = 0
2100 then (
2101 let y =
2102 if pageno = columns && conf.presentation
2103 then (
2104 let ips = calcips rowh in
2105 for i = 0 to pred columns
2107 let (pdimno, x, y, pdim) = a.(i) in
2108 a.(i) <- (pdimno, x, y+ips, pdim)
2109 done;
2110 y+ips;
2112 else y
2114 fixrow (pageno - columns);
2117 else y
2119 a.(pageno) <- (pdimno, x, y, pdim);
2120 let x = x + w + xoff*2 + conf.interpagespace in
2121 loop (pageno+1) pdimno pdim x y rowh' pdims
2123 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2124 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2126 | Csplit (c, _) ->
2127 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2128 let rec loop pageno pdimno pdim y pdims =
2129 if pageno = state.pagecount
2130 then ()
2131 else
2132 let pdimno, ((_, w, h, _) as pdim), pdims =
2133 match pdims with
2134 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2135 pdimno+1, pdim, rest
2136 | _ ->
2137 pdimno, pdim, pdims
2139 let cw = w / c in
2140 let rec loop1 n x y =
2141 if n = c then y else (
2142 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2143 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2146 let y = loop1 0 0 y in
2147 loop (pageno+1) pdimno pdim y pdims
2149 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2150 conf.columns <- Csplit (c, a);
2153 let represent () =
2154 docolumns conf.columns;
2155 state.maxy <- calcheight ();
2156 state.hscrollh <-
2157 if state.x = 0 && state.w <= state.winw - state.scrollw
2158 then 0
2159 else state.scrollw
2161 if state.reprf == noreprf
2162 then (
2163 match state.mode with
2164 | Birdseye (_, _, pageno, _, _) ->
2165 let y, h = getpageyh pageno in
2166 let top = (state.winh - h) / 2 in
2167 gotoy (max 0 (y - top))
2168 | _ -> gotoanchor state.anchor
2170 else (
2171 state.reprf ();
2172 state.reprf <- noreprf;
2176 let reshape w h =
2177 GlDraw.viewport 0 0 w h;
2178 let firsttime = state.geomcmds == firstgeomcmds in
2179 if not firsttime && nogeomcmds state.geomcmds
2180 then state.anchor <- getanchor ();
2182 state.winw <- w;
2183 let w = truncate (float w *. conf.zoom) - state.scrollw in
2184 let w = max w 2 in
2185 state.winh <- h;
2186 setfontsize fstate.fontsize;
2187 GlMat.mode `modelview;
2188 GlMat.load_identity ();
2190 GlMat.mode `projection;
2191 GlMat.load_identity ();
2192 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2193 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2194 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2196 let relx =
2197 if conf.zoom <= 1.0
2198 then 0.0
2199 else float state.x /. float state.w
2201 invalidate "geometry"
2202 (fun () ->
2203 state.w <- w;
2204 if not firsttime
2205 then state.x <- truncate (relx *. float w);
2206 let w =
2207 match conf.columns with
2208 | Csingle _ -> w
2209 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2210 | Csplit (c, _) -> w * c
2212 wcmd "geometry %d %d %d"
2213 w (stateh h) (int_of_fitmodel conf.fitmodel)
2217 let enttext () =
2218 let len = String.length state.text in
2219 let drawstring s =
2220 let hscrollh =
2221 match state.mode with
2222 | Textentry _
2223 | View ->
2224 let h, _, _ = state.uioh#scrollpw in
2226 | _ -> 0
2228 let rect x w =
2229 GlDraw.rect
2230 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2231 (x+.w, float (state.winh - hscrollh))
2234 let w = float (state.winw - state.scrollw - 1) in
2235 if state.progress >= 0.0 && state.progress < 1.0
2236 then (
2237 GlDraw.color (0.3, 0.3, 0.3);
2238 let w1 = w *. state.progress in
2239 rect 0.0 w1;
2240 GlDraw.color (0.0, 0.0, 0.0);
2241 rect w1 (w-.w1)
2243 else (
2244 GlDraw.color (0.0, 0.0, 0.0);
2245 rect 0.0 w;
2248 GlDraw.color (1.0, 1.0, 1.0);
2249 drawstring fstate.fontsize
2250 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2252 let s =
2253 match state.mode with
2254 | Textentry ((prefix, text, _, _, _, _), _) ->
2255 let s =
2256 if len > 0
2257 then
2258 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2259 else
2260 Printf.sprintf "%s%s_" prefix text
2264 | _ -> state.text
2266 let s =
2267 if state.newerrmsgs
2268 then (
2269 if not (istextentry state.mode) && state.uioh#eformsgs
2270 then
2271 let s1 = "(press 'e' to review error messasges)" in
2272 if String.length s > 0 then s ^ " " ^ s1 else s1
2273 else s
2275 else s
2277 if String.length s > 0
2278 then drawstring s
2281 let gctiles () =
2282 let len = Queue.length state.tilelru in
2283 let layout = lazy (
2284 match state.throttle with
2285 | None ->
2286 if conf.preload
2287 then preloadlayout state.y
2288 else state.layout
2289 | Some (layout, _, _) ->
2290 layout
2291 ) in
2292 let rec loop qpos =
2293 if state.memused <= conf.memlimit
2294 then ()
2295 else (
2296 if qpos < len
2297 then
2298 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2299 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2300 let (_, pw, ph, _) = getpagedim n in
2302 gen = state.gen
2303 && colorspace = conf.colorspace
2304 && angle = conf.angle
2305 && pagew = pw
2306 && pageh = ph
2307 && (
2308 let x = col*conf.tilew
2309 and y = row*conf.tileh in
2310 tilevisible (Lazy.force_val layout) n x y
2312 then Queue.push lruitem state.tilelru
2313 else (
2314 freepbo p;
2315 wcmd "freetile %s" p;
2316 state.memused <- state.memused - s;
2317 state.uioh#infochanged Memused;
2318 Hashtbl.remove state.tilemap k;
2320 loop (qpos+1)
2323 loop 0
2326 let logcurrently = function
2327 | Idle -> dolog "Idle"
2328 | Loading (l, gen) ->
2329 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2330 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2331 dolog
2332 "Tiling %d[%d,%d] page=%s cs=%s angle"
2333 l.pageno col row pageopaque
2334 (colorspace_to_string colorspace)
2336 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2337 angle gen conf.angle state.gen
2338 tilew tileh
2339 conf.tilew conf.tileh
2341 | Outlining _ ->
2342 dolog "outlining"
2345 let splitatspace =
2346 let r = Str.regexp " " in
2347 fun s -> Str.bounded_split r s 2;
2350 let onpagerect pageno f =
2351 let b =
2352 match conf.columns with
2353 | Cmulti (_, b) -> b
2354 | Csingle b -> b
2355 | Csplit (_, b) -> b
2357 if pageno >= 0 && pageno < Array.length b
2358 then
2359 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2360 let r = getpdimrect pdimno in
2361 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2364 let gotopagexy1 pageno x y =
2365 onpagerect pageno (fun w h ->
2366 let top = y /. h in
2367 let _,w1,_,leftx = getpagedim pageno in
2368 let wh = state.winh - state.hscrollh in
2369 let sw = float w1 /. w in
2370 let x = sw *. x in
2371 let x = leftx + state.x + truncate x in
2372 let sx =
2373 if x < 0 || x >= state.winw - state.scrollw
2374 then state.x - x
2375 else state.x
2377 let py, h = getpageyh pageno in
2378 let pdy = truncate (top *. float h) in
2379 let y' = py + pdy in
2380 let dy = y' - state.y in
2381 let sy =
2382 if x != state.x || not (dy > 0 && dy < wh)
2383 then (
2384 if conf.presentation
2385 then
2386 if abs (py - y') > wh
2387 then y'
2388 else py
2389 else y';
2391 else state.y
2393 if state.x != sx || state.y != sy
2394 then (
2395 let x, y =
2396 if !wtmode
2397 then (
2398 let ww = state.winw - state.scrollw in
2399 let qx = sx / ww
2400 and qy = pdy / wh in
2401 let x = qx * ww
2402 and y = py + qy * wh in
2403 let x = if -x + ww > w1 then -(w1-ww) else x
2404 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2405 let y =
2406 if conf.presentation
2407 then
2408 if abs (py - y') > wh
2409 then y'
2410 else py
2411 else y';
2413 (x, y)
2415 else (sx, sy)
2417 state.x <- x;
2418 state.hscrollh <-
2419 if x = 0 && state.w <= state.winw - state.scrollw
2420 then 0
2421 else state.scrollw
2423 gotoy_and_clear_text y;
2425 else gotoy_and_clear_text state.y;
2429 let gotopagexy pageno x y =
2430 match state.mode with
2431 | Birdseye _ -> gotopage pageno 0.0
2432 | _ -> gotopagexy1 pageno x y
2435 let act cmds =
2436 (* dolog "%S" cmds; *)
2437 let cl = splitatspace cmds in
2438 let scan s fmt f =
2439 try Scanf.sscanf s fmt f
2440 with exn ->
2441 dolog "error processing '%S': %s" cmds (exntos exn);
2442 exit 1
2444 match cl with
2445 | "clear" :: [] ->
2446 state.uioh#infochanged Pdim;
2447 state.pdims <- [];
2449 | "clearrects" :: [] ->
2450 state.rects <- state.rects1;
2451 G.postRedisplay "clearrects";
2453 | "continue" :: args :: [] ->
2454 let n = scan args "%u" (fun n -> n) in
2455 state.pagecount <- n;
2456 begin match state.currently with
2457 | Outlining l ->
2458 state.currently <- Idle;
2459 state.outlines <- Array.of_list (List.rev l)
2460 | _ -> ()
2461 end;
2463 let cur, cmds = state.geomcmds in
2464 if String.length cur = 0
2465 then failwith "umpossible";
2467 begin match List.rev cmds with
2468 | [] ->
2469 state.geomcmds <- "", [];
2470 represent ();
2471 | (s, f) :: rest ->
2472 f ();
2473 state.geomcmds <- s, List.rev rest;
2474 end;
2475 if conf.maxwait = None && not !wtmode
2476 then G.postRedisplay "continue";
2478 | "title" :: args :: [] ->
2479 Wsi.settitle args
2481 | "msg" :: args :: [] ->
2482 showtext ' ' args
2484 | "vmsg" :: args :: [] ->
2485 if conf.verbose
2486 then showtext ' ' args
2488 | "emsg" :: args :: [] ->
2489 Buffer.add_string state.errmsgs args;
2490 state.newerrmsgs <- true;
2491 G.postRedisplay "error message"
2493 | "progress" :: args :: [] ->
2494 let progress, text =
2495 scan args "%f %n"
2496 (fun f pos ->
2497 f, String.sub args pos (String.length args - pos))
2499 state.text <- text;
2500 state.progress <- progress;
2501 G.postRedisplay "progress"
2503 | "firstmatch" :: args :: [] ->
2504 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2505 scan args "%u %d %f %f %f %f %f %f %f %f"
2506 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2507 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2509 let y = (getpagey pageno) + truncate y0 in
2510 addnav ();
2511 gotoy y;
2512 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2514 | "match" :: args :: [] ->
2515 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2516 scan args "%u %d %f %f %f %f %f %f %f %f"
2517 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2518 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2520 state.rects1 <-
2521 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2523 | "page" :: args :: [] ->
2524 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2525 begin match state.currently with
2526 | Loading (l, gen) ->
2527 vlog "page %d took %f sec" l.pageno t;
2528 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2529 begin match state.throttle with
2530 | None ->
2531 let preloadedpages =
2532 if conf.preload
2533 then preloadlayout state.y
2534 else state.layout
2536 let evict () =
2537 let set =
2538 List.fold_left (fun s l -> IntSet.add l.pageno s)
2539 IntSet.empty preloadedpages
2541 let evictedpages =
2542 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2543 if not (IntSet.mem pageno set)
2544 then (
2545 wcmd "freepage %s" opaque;
2546 key :: accu
2548 else accu
2549 ) state.pagemap []
2551 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2553 evict ();
2554 state.currently <- Idle;
2555 if gen = state.gen
2556 then (
2557 tilepage l.pageno pageopaque state.layout;
2558 load state.layout;
2559 load preloadedpages;
2560 if pagevisible state.layout l.pageno
2561 && layoutready state.layout
2562 then G.postRedisplay "page";
2565 | Some (layout, _, _) ->
2566 state.currently <- Idle;
2567 tilepage l.pageno pageopaque layout;
2568 load state.layout
2569 end;
2571 | _ ->
2572 dolog "Inconsistent loading state";
2573 logcurrently state.currently;
2574 exit 1
2577 | "tile" :: args :: [] ->
2578 let (x, y, opaque, size, t) =
2579 scan args "%u %u %s %u %f"
2580 (fun x y p size t -> (x, y, p, size, t))
2582 begin match state.currently with
2583 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2584 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2586 unmappbo opaque;
2587 if tilew != conf.tilew || tileh != conf.tileh
2588 then (
2589 wcmd "freetile %s" opaque;
2590 state.currently <- Idle;
2591 load state.layout;
2593 else (
2594 puttileopaque l col row gen cs angle opaque size t;
2595 state.memused <- state.memused + size;
2596 state.uioh#infochanged Memused;
2597 gctiles ();
2598 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2599 opaque, size) state.tilelru;
2601 let layout =
2602 match state.throttle with
2603 | None -> state.layout
2604 | Some (layout, _, _) -> layout
2607 state.currently <- Idle;
2608 if gen = state.gen
2609 && conf.colorspace = cs
2610 && conf.angle = angle
2611 && tilevisible layout l.pageno x y
2612 then conttiling l.pageno pageopaque;
2614 begin match state.throttle with
2615 | None ->
2616 preload state.layout;
2617 if gen = state.gen
2618 && conf.colorspace = cs
2619 && conf.angle = angle
2620 && tilevisible state.layout l.pageno x y
2621 && (not !wtmode || layoutready state.layout)
2622 then G.postRedisplay "tile nothrottle";
2624 | Some (layout, y, _) ->
2625 let ready = layoutready layout in
2626 if ready
2627 then (
2628 state.y <- y;
2629 state.layout <- layout;
2630 state.throttle <- None;
2631 G.postRedisplay "throttle";
2633 else load layout;
2634 end;
2637 | _ ->
2638 dolog "Inconsistent tiling state";
2639 logcurrently state.currently;
2640 exit 1
2643 | "pdim" :: args :: [] ->
2644 let (n, w, h, _) as pdim =
2645 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2647 let pdim =
2648 match conf.fitmodel, conf.columns with
2649 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2650 | _ -> pdim
2652 state.uioh#infochanged Pdim;
2653 state.pdims <- pdim :: state.pdims
2655 | "o" :: args :: [] ->
2656 let (l, n, t, h, pos) =
2657 scan args "%u %u %d %u %n"
2658 (fun l n t h pos -> l, n, t, h, pos)
2660 let s = String.sub args pos (String.length args - pos) in
2661 let outline = (s, l, (n, float t /. float h, 0.0)) in
2662 begin match state.currently with
2663 | Outlining outlines ->
2664 state.currently <- Outlining (outline :: outlines)
2665 | Idle ->
2666 state.currently <- Outlining [outline]
2667 | currently ->
2668 dolog "invalid outlining state";
2669 logcurrently currently
2672 | "a" :: args :: [] ->
2673 let (n, l, t) =
2674 scan args "%u %d %d" (fun n l t -> n, l, t)
2676 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2678 | "info" :: args :: [] ->
2679 state.docinfo <- (1, args) :: state.docinfo
2681 | "infoend" :: [] ->
2682 state.uioh#infochanged Docinfo;
2683 state.docinfo <- List.rev state.docinfo
2685 | _ ->
2686 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2689 let onhist cb =
2690 let rc = cb.rc in
2691 let action = function
2692 | HCprev -> cbget cb ~-1
2693 | HCnext -> cbget cb 1
2694 | HCfirst -> cbget cb ~-(cb.rc)
2695 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2696 and cancel () = cb.rc <- rc
2697 in (action, cancel)
2700 let search pattern forward =
2701 match conf.columns with
2702 | Csplit _ ->
2703 showtext '!' "searching does not work properly in split columns mode"
2704 | _ ->
2705 if String.length pattern > 0
2706 then
2707 let pn, py =
2708 match state.layout with
2709 | [] -> 0, 0
2710 | l :: _ ->
2711 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2713 wcmd "search %d %d %d %d,%s\000"
2714 (btod conf.icase) pn py (btod forward) pattern;
2717 let intentry text key =
2718 let c =
2719 if key >= 32 && key < 127
2720 then Char.chr key
2721 else '\000'
2723 match c with
2724 | '0' .. '9' ->
2725 let text = addchar text c in
2726 TEcont text
2728 | _ ->
2729 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2730 TEcont text
2733 let linknentry text key =
2734 let c =
2735 if key >= 32 && key < 127
2736 then Char.chr key
2737 else '\000'
2739 match c with
2740 | 'a' .. 'z' ->
2741 let text = addchar text c in
2742 TEcont text
2744 | _ ->
2745 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2746 TEcont text
2749 let linkndone f s =
2750 if String.length s > 0
2751 then (
2752 let n =
2753 let l = String.length s in
2754 let rec loop pos n = if pos = l then n else
2755 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2756 loop (pos+1) (n*26 + m)
2757 in loop 0 0
2759 let rec loop n = function
2760 | [] -> ()
2761 | l :: rest ->
2762 match getopaque l.pageno with
2763 | None -> loop n rest
2764 | Some opaque ->
2765 let m = getlinkcount opaque in
2766 if n < m
2767 then (
2768 let under = getlink opaque n in
2769 f under
2771 else loop (n-m) rest
2773 loop n state.layout;
2777 let textentry text key =
2778 if key land 0xff00 = 0xff00
2779 then TEcont text
2780 else TEcont (text ^ toutf8 key)
2783 let reqlayout angle fitmodel =
2784 match state.throttle with
2785 | None ->
2786 if nogeomcmds state.geomcmds
2787 then state.anchor <- getanchor ();
2788 conf.angle <- angle mod 360;
2789 if conf.angle != 0
2790 then (
2791 match state.mode with
2792 | LinkNav _ -> state.mode <- View
2793 | _ -> ()
2795 conf.fitmodel <- fitmodel;
2796 invalidate "reqlayout"
2797 (fun () ->
2798 wcmd "reqlayout %d %d %d"
2799 conf.angle (int_of_fitmodel conf.fitmodel) (stateh state.winh)
2801 | _ -> ()
2804 let settrim trimmargins trimfuzz =
2805 if nogeomcmds state.geomcmds
2806 then state.anchor <- getanchor ();
2807 conf.trimmargins <- trimmargins;
2808 conf.trimfuzz <- trimfuzz;
2809 let x0, y0, x1, y1 = trimfuzz in
2810 invalidate "settrim"
2811 (fun () ->
2812 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2813 flushpages ();
2816 let setzoom zoom =
2817 match state.throttle with
2818 | None ->
2819 let zoom = max 0.0001 zoom in
2820 if zoom <> conf.zoom
2821 then (
2822 state.prevzoom <- conf.zoom;
2823 conf.zoom <- zoom;
2824 reshape state.winw state.winh;
2825 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2828 | Some (layout, y, started) ->
2829 let time =
2830 match conf.maxwait with
2831 | None -> 0.0
2832 | Some t -> t
2834 let dt = now () -. started in
2835 if dt > time
2836 then (
2837 state.y <- y;
2838 load layout;
2842 let setcolumns mode columns coverA coverB =
2843 state.prevcolumns <- Some (conf.columns, conf.zoom);
2844 if columns < 0
2845 then (
2846 if isbirdseye mode
2847 then showtext '!' "split mode doesn't work in bird's eye"
2848 else (
2849 conf.columns <- Csplit (-columns, [||]);
2850 state.x <- 0;
2851 conf.zoom <- 1.0;
2854 else (
2855 if columns < 2
2856 then (
2857 conf.columns <- Csingle [||];
2858 state.x <- 0;
2859 setzoom 1.0;
2861 else (
2862 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2863 conf.zoom <- 1.0;
2866 reshape state.winw state.winh;
2869 let enterbirdseye () =
2870 let zoom = float conf.thumbw /. float state.winw in
2871 let birdseyepageno =
2872 let cy = state.winh / 2 in
2873 let fold = function
2874 | [] -> 0
2875 | l :: rest ->
2876 let rec fold best = function
2877 | [] -> best.pageno
2878 | l :: rest ->
2879 let d = cy - (l.pagedispy + l.pagevh/2)
2880 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2881 if abs d < abs dbest
2882 then fold l rest
2883 else best.pageno
2884 in fold l rest
2886 fold state.layout
2888 state.mode <- Birdseye (
2889 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2891 conf.zoom <- zoom;
2892 conf.presentation <- false;
2893 conf.interpagespace <- 10;
2894 conf.hlinks <- false;
2895 conf.fitmodel <- FitProportional;
2896 state.x <- 0;
2897 state.mstate <- Mnone;
2898 conf.maxwait <- None;
2899 conf.columns <- (
2900 match conf.beyecolumns with
2901 | Some c ->
2902 conf.zoom <- 1.0;
2903 Cmulti ((c, 0, 0), [||])
2904 | None -> Csingle [||]
2906 Wsi.setcursor Wsi.CURSOR_INHERIT;
2907 if conf.verbose
2908 then
2909 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2910 (100.0*.zoom)
2911 else
2912 state.text <- ""
2914 reshape state.winw state.winh;
2917 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2918 state.mode <- View;
2919 conf.zoom <- c.zoom;
2920 conf.presentation <- c.presentation;
2921 conf.interpagespace <- c.interpagespace;
2922 conf.maxwait <- c.maxwait;
2923 conf.hlinks <- c.hlinks;
2924 conf.fitmodel <- c.fitmodel;
2925 conf.beyecolumns <- (
2926 match conf.columns with
2927 | Cmulti ((c, _, _), _) -> Some c
2928 | Csingle _ -> None
2929 | Csplit _ -> failwith "leaving bird's eye split mode"
2931 conf.columns <- (
2932 match c.columns with
2933 | Cmulti (c, _) -> Cmulti (c, [||])
2934 | Csingle _ -> Csingle [||]
2935 | Csplit (c, _) -> Csplit (c, [||])
2937 state.x <- leftx;
2938 if conf.verbose
2939 then
2940 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2941 (100.0*.conf.zoom)
2943 reshape state.winw state.winh;
2944 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2947 let togglebirdseye () =
2948 match state.mode with
2949 | Birdseye vals -> leavebirdseye vals true
2950 | View -> enterbirdseye ()
2951 | _ -> ()
2954 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2955 let pageno = max 0 (pageno - incr) in
2956 let rec loop = function
2957 | [] -> gotopage1 pageno 0
2958 | l :: _ when l.pageno = pageno ->
2959 if l.pagedispy >= 0 && l.pagey = 0
2960 then G.postRedisplay "upbirdseye"
2961 else gotopage1 pageno 0
2962 | _ :: rest -> loop rest
2964 loop state.layout;
2965 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2968 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2969 let pageno = min (state.pagecount - 1) (pageno + incr) in
2970 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2971 let rec loop = function
2972 | [] ->
2973 let y, h = getpageyh pageno in
2974 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2975 gotoy (clamp dy)
2976 | l :: _ when l.pageno = pageno ->
2977 if l.pagevh != l.pageh
2978 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2979 else G.postRedisplay "downbirdseye"
2980 | _ :: rest -> loop rest
2982 loop state.layout
2985 let optentry mode _ key =
2986 let btos b = if b then "on" else "off" in
2987 if key >= 32 && key < 127
2988 then
2989 let c = Char.chr key in
2990 match c with
2991 | 's' ->
2992 let ondone s =
2993 try conf.scrollstep <- int_of_string s with exc ->
2994 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2996 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2998 | 'A' ->
2999 let ondone s =
3001 conf.autoscrollstep <- int_of_string s;
3002 if state.autoscroll <> None
3003 then state.autoscroll <- Some conf.autoscrollstep
3004 with exc ->
3005 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3007 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3009 | 'C' ->
3010 let ondone s =
3012 let n, a, b = multicolumns_of_string s in
3013 setcolumns mode n a b;
3014 with exc ->
3015 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3017 TEswitch ("columns: ", "", None, textentry, ondone, true)
3019 | 'Z' ->
3020 let ondone s =
3022 let zoom = float (int_of_string s) /. 100.0 in
3023 setzoom zoom
3024 with exc ->
3025 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3027 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3029 | 't' ->
3030 let ondone s =
3032 conf.thumbw <- bound (int_of_string s) 2 4096;
3033 state.text <-
3034 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3035 begin match mode with
3036 | Birdseye beye ->
3037 leavebirdseye beye false;
3038 enterbirdseye ();
3039 | _ -> ();
3041 with exc ->
3042 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3044 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3046 | 'R' ->
3047 let ondone s =
3048 match try
3049 Some (int_of_string s)
3050 with exc ->
3051 state.text <- Printf.sprintf "bad integer `%s': %s"
3052 s (exntos exc);
3053 None
3054 with
3055 | Some angle -> reqlayout angle conf.fitmodel
3056 | None -> ()
3058 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3060 | 'i' ->
3061 conf.icase <- not conf.icase;
3062 TEdone ("case insensitive search " ^ (btos conf.icase))
3064 | 'p' ->
3065 conf.preload <- not conf.preload;
3066 gotoy state.y;
3067 TEdone ("preload " ^ (btos conf.preload))
3069 | 'v' ->
3070 conf.verbose <- not conf.verbose;
3071 TEdone ("verbose " ^ (btos conf.verbose))
3073 | 'd' ->
3074 conf.debug <- not conf.debug;
3075 TEdone ("debug " ^ (btos conf.debug))
3077 | 'h' ->
3078 conf.maxhfit <- not conf.maxhfit;
3079 state.maxy <- calcheight ();
3080 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3082 | 'c' ->
3083 conf.crophack <- not conf.crophack;
3084 TEdone ("crophack " ^ btos conf.crophack)
3086 | 'a' ->
3087 let s =
3088 match conf.maxwait with
3089 | None ->
3090 conf.maxwait <- Some infinity;
3091 "always wait for page to complete"
3092 | Some _ ->
3093 conf.maxwait <- None;
3094 "show placeholder if page is not ready"
3096 TEdone s
3098 | 'f' ->
3099 conf.underinfo <- not conf.underinfo;
3100 TEdone ("underinfo " ^ btos conf.underinfo)
3102 | 'P' ->
3103 conf.savebmarks <- not conf.savebmarks;
3104 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3106 | 'S' ->
3107 let ondone s =
3109 let pageno, py =
3110 match state.layout with
3111 | [] -> 0, 0
3112 | l :: _ ->
3113 l.pageno, l.pagey
3115 conf.interpagespace <- int_of_string s;
3116 docolumns conf.columns;
3117 state.maxy <- calcheight ();
3118 let y = getpagey pageno in
3119 gotoy (y + py)
3120 with exc ->
3121 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3123 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3125 | 'l' ->
3126 let fm =
3127 match conf.fitmodel with
3128 | FitProportional -> FitWidth
3129 | _ -> FitProportional
3131 reqlayout conf.angle fm;
3132 TEdone ("proportional display " ^ btos (fm == FitProportional))
3134 | 'T' ->
3135 settrim (not conf.trimmargins) conf.trimfuzz;
3136 TEdone ("trim margins " ^ btos conf.trimmargins)
3138 | 'I' ->
3139 conf.invert <- not conf.invert;
3140 TEdone ("invert colors " ^ btos conf.invert)
3142 | 'x' ->
3143 let ondone s =
3144 cbput state.hists.sel s;
3145 conf.selcmd <- s;
3147 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3148 textentry, ondone, true)
3150 | _ ->
3151 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3152 TEstop
3153 else
3154 TEcont state.text
3157 class type lvsource = object
3158 method getitemcount : int
3159 method getitem : int -> (string * int)
3160 method hasaction : int -> bool
3161 method exit :
3162 uioh:uioh ->
3163 cancel:bool ->
3164 active:int ->
3165 first:int ->
3166 pan:int ->
3167 qsearch:string ->
3168 uioh option
3169 method getactive : int
3170 method getfirst : int
3171 method getqsearch : string
3172 method setqsearch : string -> unit
3173 method getpan : int
3174 end;;
3176 class virtual lvsourcebase = object
3177 val mutable m_active = 0
3178 val mutable m_first = 0
3179 val mutable m_qsearch = ""
3180 val mutable m_pan = 0
3181 method getactive = m_active
3182 method getfirst = m_first
3183 method getqsearch = m_qsearch
3184 method getpan = m_pan
3185 method setqsearch s = m_qsearch <- s
3186 end;;
3188 let withoutlastutf8 s =
3189 let len = String.length s in
3190 if len = 0
3191 then s
3192 else
3193 let rec find pos =
3194 if pos = 0
3195 then pos
3196 else
3197 let b = Char.code s.[pos] in
3198 if b land 0b11000000 = 0b11000000
3199 then pos
3200 else find (pos-1)
3202 let first =
3203 if Char.code s.[len-1] land 0x80 = 0
3204 then len-1
3205 else find (len-1)
3207 String.sub s 0 first;
3210 let textentrykeyboard
3211 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3212 let key =
3213 if key >= 0xffb0 && key <= 0xffb9
3214 then key - 0xffb0 + 48 else key
3216 let enttext te =
3217 state.mode <- Textentry (te, onleave);
3218 state.text <- "";
3219 enttext ();
3220 G.postRedisplay "textentrykeyboard enttext";
3222 let histaction cmd =
3223 match opthist with
3224 | None -> ()
3225 | Some (action, _) ->
3226 state.mode <- Textentry (
3227 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3229 G.postRedisplay "textentry histaction"
3231 match key with
3232 | 0xff08 -> (* backspace *)
3233 let s = withoutlastutf8 text in
3234 let len = String.length s in
3235 if cancelonempty && len = 0
3236 then (
3237 onleave Cancel;
3238 G.postRedisplay "textentrykeyboard after cancel";
3240 else (
3241 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3244 | 0xff0d | 0xff8d -> (* (kp) enter *)
3245 ondone text;
3246 onleave Confirm;
3247 G.postRedisplay "textentrykeyboard after confirm"
3249 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3250 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3251 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3252 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3254 | 0xff1b -> (* escape*)
3255 if String.length text = 0
3256 then (
3257 begin match opthist with
3258 | None -> ()
3259 | Some (_, onhistcancel) -> onhistcancel ()
3260 end;
3261 onleave Cancel;
3262 state.text <- "";
3263 G.postRedisplay "textentrykeyboard after cancel2"
3265 else (
3266 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3269 | 0xff9f | 0xffff -> () (* delete *)
3271 | _ when key != 0
3272 && key land 0xff00 != 0xff00 (* keyboard *)
3273 && key land 0xfe00 != 0xfe00 (* xkb *)
3274 && key land 0xfd00 != 0xfd00 (* 3270 *)
3276 begin match onkey text key with
3277 | TEdone text ->
3278 ondone text;
3279 onleave Confirm;
3280 G.postRedisplay "textentrykeyboard after confirm2";
3282 | TEcont text ->
3283 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3285 | TEstop ->
3286 onleave Cancel;
3287 G.postRedisplay "textentrykeyboard after cancel3"
3289 | TEswitch te ->
3290 state.mode <- Textentry (te, onleave);
3291 G.postRedisplay "textentrykeyboard switch";
3292 end;
3294 | _ ->
3295 vlog "unhandled key %s" (Wsi.keyname key)
3298 let firstof first active =
3299 if first > active || abs (first - active) > fstate.maxrows - 1
3300 then max 0 (active - (fstate.maxrows/2))
3301 else first
3304 let calcfirst first active =
3305 if active > first
3306 then
3307 let rows = active - first in
3308 if rows > fstate.maxrows then active - fstate.maxrows else first
3309 else active
3312 let scrollph y maxy =
3313 let sh = float (maxy + state.winh) /. float state.winh in
3314 let sh = float state.winh /. sh in
3315 let sh = max sh (float conf.scrollh) in
3317 let percent = float y /. float maxy in
3318 let position = (float state.winh -. sh) *. percent in
3320 let position =
3321 if position +. sh > float state.winh
3322 then float state.winh -. sh
3323 else position
3325 position, sh;
3328 let coe s = (s :> uioh);;
3330 class listview ~(source:lvsource) ~trusted ~modehash =
3331 object (self)
3332 val m_pan = source#getpan
3333 val m_first = source#getfirst
3334 val m_active = source#getactive
3335 val m_qsearch = source#getqsearch
3336 val m_prev_uioh = state.uioh
3338 method private elemunder y =
3339 let n = y / (fstate.fontsize+1) in
3340 if m_first + n < source#getitemcount
3341 then (
3342 if source#hasaction (m_first + n)
3343 then Some (m_first + n)
3344 else None
3346 else None
3348 method display =
3349 Gl.enable `blend;
3350 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3351 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3352 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3353 GlDraw.color (1., 1., 1.);
3354 Gl.enable `texture_2d;
3355 let fs = fstate.fontsize in
3356 let nfs = fs + 1 in
3357 let ww = fstate.wwidth in
3358 let tabw = 30.0*.ww in
3359 let itemcount = source#getitemcount in
3360 let rec loop row =
3361 if (row - m_first) > fstate.maxrows
3362 then ()
3363 else (
3364 if row >= 0 && row < itemcount
3365 then (
3366 let (s, level) = source#getitem row in
3367 let y = (row - m_first) * nfs in
3368 let x = 5.0 +. float (level + m_pan) *. ww in
3369 if row = m_active
3370 then (
3371 Gl.disable `texture_2d;
3372 GlDraw.polygon_mode `both `line;
3373 let alpha = if source#hasaction row then 0.9 else 0.3 in
3374 GlDraw.color (1., 1., 1.) ~alpha;
3375 GlDraw.rect (1., float (y + 1))
3376 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3377 GlDraw.polygon_mode `both `fill;
3378 GlDraw.color (1., 1., 1.);
3379 Gl.enable `texture_2d;
3382 let drawtabularstring s =
3383 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3384 if trusted
3385 then
3386 let tabpos = try String.index s '\t' with Not_found -> -1 in
3387 if tabpos > 0
3388 then
3389 let len = String.length s - tabpos - 1 in
3390 let s1 = String.sub s 0 tabpos
3391 and s2 = String.sub s (tabpos + 1) len in
3392 let nx = drawstr x s1 in
3393 let sw = nx -. x in
3394 let x = x +. (max tabw sw) in
3395 drawstr x s2
3396 else
3397 drawstr x s
3398 else
3399 drawstr x s
3401 let _ = drawtabularstring s in
3402 loop (row+1)
3406 loop m_first;
3407 Gl.disable `blend;
3408 Gl.disable `texture_2d;
3410 method updownlevel incr =
3411 let len = source#getitemcount in
3412 let curlevel =
3413 if m_active >= 0 && m_active < len
3414 then snd (source#getitem m_active)
3415 else -1
3417 let rec flow i =
3418 if i = len then i-1 else if i = -1 then 0 else
3419 let _, l = source#getitem i in
3420 if l != curlevel then i else flow (i+incr)
3422 let active = flow m_active in
3423 let first = calcfirst m_first active in
3424 G.postRedisplay "outline updownlevel";
3425 {< m_active = active; m_first = first >}
3427 method private key1 key mask =
3428 let set1 active first qsearch =
3429 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3431 let search active pattern incr =
3432 let active = if active = -1 then m_first else active in
3433 let dosearch re =
3434 let rec loop n =
3435 if n >= 0 && n < source#getitemcount
3436 then (
3437 let s, _ = source#getitem n in
3439 (try ignore (Str.search_forward re s 0); true
3440 with Not_found -> false)
3441 then Some n
3442 else loop (n + incr)
3444 else None
3446 loop active
3449 let re = Str.regexp_case_fold pattern in
3450 dosearch re
3451 with Failure s ->
3452 state.text <- s;
3453 None
3455 let itemcount = source#getitemcount in
3456 let find start incr =
3457 let rec find i =
3458 if i = -1 || i = itemcount
3459 then -1
3460 else (
3461 if source#hasaction i
3462 then i
3463 else find (i + incr)
3466 find start
3468 let set active first =
3469 let first = bound first 0 (itemcount - fstate.maxrows) in
3470 state.text <- "";
3471 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3473 let navigate incr =
3474 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3475 let active, first =
3476 let incr1 = if incr > 0 then 1 else -1 in
3477 if isvisible m_first m_active
3478 then
3479 let next =
3480 let next = m_active + incr in
3481 let next =
3482 if next < 0 || next >= itemcount
3483 then -1
3484 else find next incr1
3486 if abs (m_active - next) > fstate.maxrows
3487 then -1
3488 else next
3490 if next = -1
3491 then
3492 let first = m_first + incr in
3493 let first = bound first 0 (itemcount - 1) in
3494 let next =
3495 let next = m_active + incr in
3496 let next = bound next 0 (itemcount - 1) in
3497 find next ~-incr1
3499 let active =
3500 if next = -1
3501 then m_active
3502 else (
3503 if isvisible first next
3504 then next
3505 else m_active
3508 active, first
3509 else
3510 let first = min next m_first in
3511 let first =
3512 if abs (next - first) > fstate.maxrows
3513 then first + incr
3514 else first
3516 next, first
3517 else
3518 let first = m_first + incr in
3519 let first = bound first 0 (itemcount - 1) in
3520 let active =
3521 let next = m_active + incr in
3522 let next = bound next 0 (itemcount - 1) in
3523 let next = find next incr1 in
3524 let active =
3525 if next = -1 || abs (m_active - first) > fstate.maxrows
3526 then (
3527 let active = if m_active = -1 then next else m_active in
3528 active
3530 else next
3532 if isvisible first active
3533 then active
3534 else -1
3536 active, first
3538 G.postRedisplay "listview navigate";
3539 set active first;
3541 match key with
3542 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3543 let incr = if key = 0x72 then -1 else 1 in
3544 let active, first =
3545 match search (m_active + incr) m_qsearch incr with
3546 | None ->
3547 state.text <- m_qsearch ^ " [not found]";
3548 m_active, m_first
3549 | Some active ->
3550 state.text <- m_qsearch;
3551 active, firstof m_first active
3553 G.postRedisplay "listview ctrl-r/s";
3554 set1 active first m_qsearch;
3556 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3557 if m_active >= 0 && m_active < source#getitemcount
3558 then (
3559 let s, _ = source#getitem m_active in
3560 selstring s;
3562 coe self
3564 | 0xff08 -> (* backspace *)
3565 if String.length m_qsearch = 0
3566 then coe self
3567 else (
3568 let qsearch = withoutlastutf8 m_qsearch in
3569 let len = String.length qsearch in
3570 if len = 0
3571 then (
3572 state.text <- "";
3573 G.postRedisplay "listview empty qsearch";
3574 set1 m_active m_first "";
3576 else
3577 let active, first =
3578 match search m_active qsearch ~-1 with
3579 | None ->
3580 state.text <- qsearch ^ " [not found]";
3581 m_active, m_first
3582 | Some active ->
3583 state.text <- qsearch;
3584 active, firstof m_first active
3586 G.postRedisplay "listview backspace qsearch";
3587 set1 active first qsearch
3590 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3591 let pattern = m_qsearch ^ toutf8 key in
3592 let active, first =
3593 match search m_active pattern 1 with
3594 | None ->
3595 state.text <- pattern ^ " [not found]";
3596 m_active, m_first
3597 | Some active ->
3598 state.text <- pattern;
3599 active, firstof m_first active
3601 G.postRedisplay "listview qsearch add";
3602 set1 active first pattern;
3604 | 0xff1b -> (* escape *)
3605 state.text <- "";
3606 if String.length m_qsearch = 0
3607 then (
3608 G.postRedisplay "list view escape";
3609 begin
3610 match
3611 source#exit (coe self) true m_active m_first m_pan m_qsearch
3612 with
3613 | None -> m_prev_uioh
3614 | Some uioh -> uioh
3617 else (
3618 G.postRedisplay "list view kill qsearch";
3619 source#setqsearch "";
3620 coe {< m_qsearch = "" >}
3623 | 0xff0d | 0xff8d -> (* (kp) enter *)
3624 state.text <- "";
3625 let self = {< m_qsearch = "" >} in
3626 source#setqsearch "";
3627 let opt =
3628 G.postRedisplay "listview enter";
3629 if m_active >= 0 && m_active < source#getitemcount
3630 then (
3631 source#exit (coe self) false m_active m_first m_pan "";
3633 else (
3634 source#exit (coe self) true m_active m_first m_pan "";
3637 begin match opt with
3638 | None -> m_prev_uioh
3639 | Some uioh -> uioh
3642 | 0xff9f | 0xffff -> (* (kp) delete *)
3643 coe self
3645 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3646 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3647 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3648 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3650 | 0xff53 | 0xff98 -> (* (kp) right *)
3651 state.text <- "";
3652 G.postRedisplay "listview right";
3653 coe {< m_pan = m_pan - 1 >}
3655 | 0xff51 | 0xff96 -> (* (kp) left *)
3656 state.text <- "";
3657 G.postRedisplay "listview left";
3658 coe {< m_pan = m_pan + 1 >}
3660 | 0xff50 | 0xff95 -> (* (kp) home *)
3661 let active = find 0 1 in
3662 G.postRedisplay "listview home";
3663 set active 0;
3665 | 0xff57 | 0xff9c -> (* (kp) end *)
3666 let first = max 0 (itemcount - fstate.maxrows) in
3667 let active = find (itemcount - 1) ~-1 in
3668 G.postRedisplay "listview end";
3669 set active first;
3671 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3672 coe self
3674 | _ ->
3675 dolog "listview unknown key %#x" key; coe self
3677 method key key mask =
3678 match state.mode with
3679 | Textentry te -> textentrykeyboard key mask te; coe self
3680 | _ -> self#key1 key mask
3682 method button button down x y _ =
3683 let opt =
3684 match button with
3685 | 1 when x > state.winw - conf.scrollbw ->
3686 G.postRedisplay "listview scroll";
3687 if down
3688 then
3689 let _, position, sh = self#scrollph in
3690 if y > truncate position && y < truncate (position +. sh)
3691 then (
3692 state.mstate <- Mscrolly;
3693 Some (coe self)
3695 else
3696 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3697 let first = truncate (s *. float source#getitemcount) in
3698 let first = min source#getitemcount first in
3699 Some (coe {< m_first = first; m_active = first >})
3700 else (
3701 state.mstate <- Mnone;
3702 Some (coe self);
3704 | 1 when not down ->
3705 begin match self#elemunder y with
3706 | Some n ->
3707 G.postRedisplay "listview click";
3708 source#exit
3709 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3710 | _ ->
3711 Some (coe self)
3713 | n when (n == 4 || n == 5) && not down ->
3714 let len = source#getitemcount in
3715 let first =
3716 if n = 5 && m_first + fstate.maxrows >= len
3717 then
3718 m_first
3719 else
3720 let first = m_first + (if n == 4 then -1 else 1) in
3721 bound first 0 (len - 1)
3723 G.postRedisplay "listview wheel";
3724 Some (coe {< m_first = first >})
3725 | n when (n = 6 || n = 7) && not down ->
3726 let inc = m_first + (if n = 7 then -1 else 1) in
3727 G.postRedisplay "listview hwheel";
3728 Some (coe {< m_pan = m_pan + inc >})
3729 | _ ->
3730 Some (coe self)
3732 match opt with
3733 | None -> m_prev_uioh
3734 | Some uioh -> uioh
3736 method motion _ y =
3737 match state.mstate with
3738 | Mscrolly ->
3739 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3740 let first = truncate (s *. float source#getitemcount) in
3741 let first = min source#getitemcount first in
3742 G.postRedisplay "listview motion";
3743 coe {< m_first = first; m_active = first >}
3744 | _ -> coe self
3746 method pmotion x y =
3747 if x < state.winw - conf.scrollbw
3748 then
3749 let n =
3750 match self#elemunder y with
3751 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3752 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3754 let o =
3755 if n != m_active
3756 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3757 else self
3759 coe o
3760 else (
3761 Wsi.setcursor Wsi.CURSOR_INHERIT;
3762 coe self
3765 method infochanged _ = ()
3767 method scrollpw = (0, 0.0, 0.0)
3768 method scrollph =
3769 let nfs = fstate.fontsize + 1 in
3770 let y = m_first * nfs in
3771 let itemcount = source#getitemcount in
3772 let maxi = max 0 (itemcount - fstate.maxrows) in
3773 let maxy = maxi * nfs in
3774 let p, h = scrollph y maxy in
3775 conf.scrollbw, p, h
3777 method modehash = modehash
3778 method eformsgs = false
3779 end;;
3781 class outlinelistview ~source =
3782 object (self)
3783 inherit listview
3784 ~source:(source :> lvsource)
3785 ~trusted:false
3786 ~modehash:(findkeyhash conf "outline")
3787 as super
3789 method key key mask =
3790 let calcfirst first active =
3791 if active > first
3792 then
3793 let rows = active - first in
3794 let maxrows =
3795 if String.length state.text = 0
3796 then fstate.maxrows
3797 else fstate.maxrows - 2
3799 if rows > maxrows then active - maxrows else first
3800 else active
3802 let navigate incr =
3803 let active = m_active + incr in
3804 let active = bound active 0 (source#getitemcount - 1) in
3805 let first = calcfirst m_first active in
3806 G.postRedisplay "outline navigate";
3807 coe {< m_active = active; m_first = first >}
3809 let ctrl = Wsi.withctrl mask in
3810 match key with
3811 | 110 when ctrl -> (* ctrl-n *)
3812 source#narrow m_qsearch;
3813 G.postRedisplay "outline ctrl-n";
3814 coe {< m_first = 0; m_active = 0 >}
3816 | 117 when ctrl -> (* ctrl-u *)
3817 source#denarrow;
3818 G.postRedisplay "outline ctrl-u";
3819 state.text <- "";
3820 coe {< m_first = 0; m_active = 0 >}
3822 | 108 when ctrl -> (* ctrl-l *)
3823 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3824 G.postRedisplay "outline ctrl-l";
3825 coe {< m_first = first >}
3827 | 0xff9f | 0xffff -> (* (kp) delete *)
3828 source#remove m_active;
3829 G.postRedisplay "outline delete";
3830 let active = max 0 (m_active-1) in
3831 coe {< m_first = firstof m_first active;
3832 m_active = active >}
3834 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3835 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3836 | 0xff55 | 0xff9a -> (* (kp) prior *)
3837 navigate ~-(fstate.maxrows)
3838 | 0xff56 | 0xff9b -> (* (kp) next *)
3839 navigate fstate.maxrows
3841 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3842 let o =
3843 if ctrl
3844 then (
3845 G.postRedisplay "outline ctrl right";
3846 {< m_pan = m_pan + 1 >}
3848 else self#updownlevel 1
3850 coe o
3852 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3853 let o =
3854 if ctrl
3855 then (
3856 G.postRedisplay "outline ctrl left";
3857 {< m_pan = m_pan - 1 >}
3859 else self#updownlevel ~-1
3861 coe o
3863 | 0xff50 | 0xff95 -> (* (kp) home *)
3864 G.postRedisplay "outline home";
3865 coe {< m_first = 0; m_active = 0 >}
3867 | 0xff57 | 0xff9c -> (* (kp) end *)
3868 let active = source#getitemcount - 1 in
3869 let first = max 0 (active - fstate.maxrows) in
3870 G.postRedisplay "outline end";
3871 coe {< m_active = active; m_first = first >}
3873 | _ -> super#key key mask
3876 let outlinesource usebookmarks =
3877 let empty = [||] in
3878 (object
3879 inherit lvsourcebase
3880 val mutable m_items = empty
3881 val mutable m_orig_items = empty
3882 val mutable m_prev_items = empty
3883 val mutable m_narrow_pattern = ""
3884 val mutable m_hadremovals = false
3886 method getitemcount =
3887 Array.length m_items + (if m_hadremovals then 1 else 0)
3889 method getitem n =
3890 if n == Array.length m_items && m_hadremovals
3891 then
3892 ("[Confirm removal]", 0)
3893 else
3894 let s, n, _ = m_items.(n) in
3895 (s, n)
3897 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3898 ignore (uioh, first, qsearch);
3899 let confrimremoval = m_hadremovals && active = Array.length m_items in
3900 let items =
3901 if String.length m_narrow_pattern = 0
3902 then m_orig_items
3903 else m_items
3905 if not cancel
3906 then (
3907 if not confrimremoval
3908 then(
3909 let _, _, anchor = m_items.(active) in
3910 gotoghyll (getanchory anchor);
3911 m_items <- items;
3913 else (
3914 state.bookmarks <- Array.to_list m_items;
3915 m_orig_items <- m_items;
3918 else m_items <- items;
3919 m_pan <- pan;
3920 None
3922 method hasaction _ = true
3924 method greetmsg =
3925 if Array.length m_items != Array.length m_orig_items
3926 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3927 else ""
3929 method narrow pattern =
3930 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3931 match reopt with
3932 | None -> ()
3933 | Some re ->
3934 let rec loop accu n =
3935 if n = -1
3936 then (
3937 m_narrow_pattern <- pattern;
3938 m_items <- Array.of_list accu
3940 else
3941 let (s, _, _) as o = m_items.(n) in
3942 let accu =
3943 if (try ignore (Str.search_forward re s 0); true
3944 with Not_found -> false)
3945 then o :: accu
3946 else accu
3948 loop accu (n-1)
3950 loop [] (Array.length m_items - 1)
3952 method denarrow =
3953 m_orig_items <- (
3954 if usebookmarks
3955 then Array.of_list state.bookmarks
3956 else state.outlines
3958 m_items <- m_orig_items
3960 method remove m =
3961 if usebookmarks
3962 then
3963 if m >= 0 && m < Array.length m_items
3964 then (
3965 m_hadremovals <- true;
3966 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3967 let n = if n >= m then n+1 else n in
3968 m_items.(n)
3972 method reset anchor items =
3973 m_hadremovals <- false;
3974 if m_orig_items == empty || m_prev_items != items
3975 then (
3976 m_orig_items <- items;
3977 if String.length m_narrow_pattern = 0
3978 then m_items <- items;
3980 m_prev_items <- items;
3981 let rely = getanchory anchor in
3982 let active =
3983 let rec loop n best bestd =
3984 if n = Array.length m_items
3985 then best
3986 else
3987 let (_, _, anchor) = m_items.(n) in
3988 let orely = getanchory anchor in
3989 let d = abs (orely - rely) in
3990 if d < bestd
3991 then loop (n+1) n d
3992 else loop (n+1) best bestd
3994 loop 0 ~-1 max_int
3996 m_active <- active;
3997 m_first <- firstof m_first active
3998 end)
4001 let enterselector usebookmarks =
4002 let source = outlinesource usebookmarks in
4003 fun errmsg ->
4004 let outlines =
4005 if usebookmarks
4006 then Array.of_list state.bookmarks
4007 else state.outlines
4009 if Array.length outlines = 0
4010 then (
4011 showtext ' ' errmsg;
4013 else (
4014 state.text <- source#greetmsg;
4015 Wsi.setcursor Wsi.CURSOR_INHERIT;
4016 let anchor = getanchor () in
4017 source#reset anchor outlines;
4018 state.uioh <- coe (new outlinelistview ~source);
4019 G.postRedisplay "enter selector";
4023 let enteroutlinemode =
4024 let f = enterselector false in
4025 fun ()-> f "Document has no outline";
4028 let enterbookmarkmode =
4029 let f = enterselector true in
4030 fun () -> f "Document has no bookmarks (yet)";
4033 let color_of_string s =
4034 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4035 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4039 let color_to_string (r, g, b) =
4040 let r = truncate (r *. 256.0)
4041 and g = truncate (g *. 256.0)
4042 and b = truncate (b *. 256.0) in
4043 Printf.sprintf "%d/%d/%d" r g b
4046 let irect_of_string s =
4047 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4050 let irect_to_string (x0,y0,x1,y1) =
4051 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4054 let makecheckers () =
4055 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4056 following to say:
4057 converted by Issac Trotts. July 25, 2002 *)
4058 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4059 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4060 let id = GlTex.gen_texture () in
4061 GlTex.bind_texture `texture_2d id;
4062 GlPix.store (`unpack_alignment 1);
4063 GlTex.image2d image;
4064 List.iter (GlTex.parameter ~target:`texture_2d)
4065 [ `mag_filter `nearest; `min_filter `nearest ];
4069 let setcheckers enabled =
4070 match state.texid with
4071 | None ->
4072 if enabled then state.texid <- Some (makecheckers ())
4074 | Some texid ->
4075 if not enabled
4076 then (
4077 GlTex.delete_texture texid;
4078 state.texid <- None;
4082 let int_of_string_with_suffix s =
4083 let l = String.length s in
4084 let s1, shift =
4085 if l > 1
4086 then
4087 let suffix = Char.lowercase s.[l-1] in
4088 match suffix with
4089 | 'k' -> String.sub s 0 (l-1), 10
4090 | 'm' -> String.sub s 0 (l-1), 20
4091 | 'g' -> String.sub s 0 (l-1), 30
4092 | _ -> s, 0
4093 else s, 0
4095 let n = int_of_string s1 in
4096 let m = n lsl shift in
4097 if m < 0 || m < n
4098 then raise (Failure "value too large")
4099 else m
4102 let string_with_suffix_of_int n =
4103 if n = 0
4104 then "0"
4105 else
4106 let n, s =
4107 if n land ((1 lsl 30) - 1) = 0
4108 then n lsr 30, "G"
4109 else (
4110 if n land ((1 lsl 20) - 1) = 0
4111 then n lsr 20, "M"
4112 else (
4113 if n land ((1 lsl 10) - 1) = 0
4114 then n lsr 10, "K"
4115 else n, ""
4119 let rec loop s n =
4120 let h = n mod 1000 in
4121 let n = n / 1000 in
4122 if n = 0
4123 then string_of_int h ^ s
4124 else (
4125 let s = Printf.sprintf "_%03d%s" h s in
4126 loop s n
4129 loop "" n ^ s;
4132 let defghyllscroll = (40, 8, 32);;
4133 let ghyllscroll_of_string s =
4134 let (n, a, b) as nab =
4135 if s = "default"
4136 then defghyllscroll
4137 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4139 if n <= a || n <= b || a >= b
4140 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4141 nab;
4144 let ghyllscroll_to_string ((n, a, b) as nab) =
4145 if nab = defghyllscroll
4146 then "default"
4147 else Printf.sprintf "%d,%d,%d" n a b;
4150 let describe_location () =
4151 let fn = page_of_y state.y in
4152 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4153 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4154 let percent =
4155 if maxy <= 0
4156 then 100.
4157 else (100. *. (float state.y /. float maxy))
4159 if fn = ln
4160 then
4161 Printf.sprintf "page %d of %d [%.2f%%]"
4162 (fn+1) state.pagecount percent
4163 else
4164 Printf.sprintf
4165 "pages %d-%d of %d [%.2f%%]"
4166 (fn+1) (ln+1) state.pagecount percent
4169 let setpresentationmode v =
4170 let n = page_of_y state.y in
4171 state.anchor <- (n, 0.0, 1.0);
4172 conf.presentation <- v;
4173 if conf.presentation
4174 then (
4175 if not conf.scrollbarinpm
4176 then state.scrollw <- 0;
4178 else state.scrollw <- conf.scrollbw;
4179 if conf.fitmodel = FitPage
4180 then reqlayout conf.angle conf.fitmodel;
4181 represent ();
4184 let enterinfomode =
4185 let btos b = if b then "\xe2\x88\x9a" else "" in
4186 let showextended = ref false in
4187 let leave mode = function
4188 | Confirm -> state.mode <- mode
4189 | Cancel -> state.mode <- mode in
4190 let src =
4191 (object
4192 val mutable m_first_time = true
4193 val mutable m_l = []
4194 val mutable m_a = [||]
4195 val mutable m_prev_uioh = nouioh
4196 val mutable m_prev_mode = View
4198 inherit lvsourcebase
4200 method reset prev_mode prev_uioh =
4201 m_a <- Array.of_list (List.rev m_l);
4202 m_l <- [];
4203 m_prev_mode <- prev_mode;
4204 m_prev_uioh <- prev_uioh;
4205 if m_first_time
4206 then (
4207 let rec loop n =
4208 if n >= Array.length m_a
4209 then ()
4210 else
4211 match m_a.(n) with
4212 | _, _, _, Action _ -> m_active <- n
4213 | _ -> loop (n+1)
4215 loop 0;
4216 m_first_time <- false;
4219 method int name get set =
4220 m_l <-
4221 (name, `int get, 1, Action (
4222 fun u ->
4223 let ondone s =
4224 try set (int_of_string s)
4225 with exn ->
4226 state.text <- Printf.sprintf "bad integer `%s': %s"
4227 s (exntos exn)
4229 state.text <- "";
4230 let te = name ^ ": ", "", None, intentry, ondone, true in
4231 state.mode <- Textentry (te, leave m_prev_mode);
4233 )) :: m_l
4235 method int_with_suffix name get set =
4236 m_l <-
4237 (name, `intws get, 1, Action (
4238 fun u ->
4239 let ondone s =
4240 try set (int_of_string_with_suffix s)
4241 with exn ->
4242 state.text <- Printf.sprintf "bad integer `%s': %s"
4243 s (exntos exn)
4245 state.text <- "";
4246 let te =
4247 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4249 state.mode <- Textentry (te, leave m_prev_mode);
4251 )) :: m_l
4253 method bool ?(offset=1) ?(btos=btos) name get set =
4254 m_l <-
4255 (name, `bool (btos, get), offset, Action (
4256 fun u ->
4257 let v = get () in
4258 set (not v);
4260 )) :: m_l
4262 method color name get set =
4263 m_l <-
4264 (name, `color get, 1, Action (
4265 fun u ->
4266 let invalid = (nan, nan, nan) in
4267 let ondone s =
4268 let c =
4269 try color_of_string s
4270 with exn ->
4271 state.text <- Printf.sprintf "bad color `%s': %s"
4272 s (exntos exn);
4273 invalid
4275 if c <> invalid
4276 then set c;
4278 let te = name ^ ": ", "", None, textentry, ondone, true in
4279 state.text <- color_to_string (get ());
4280 state.mode <- Textentry (te, leave m_prev_mode);
4282 )) :: m_l
4284 method string name get set =
4285 m_l <-
4286 (name, `string get, 1, Action (
4287 fun u ->
4288 let ondone s = set s in
4289 let te = name ^ ": ", "", None, textentry, ondone, true in
4290 state.mode <- Textentry (te, leave m_prev_mode);
4292 )) :: m_l
4294 method colorspace name get set =
4295 m_l <-
4296 (name, `string get, 1, Action (
4297 fun _ ->
4298 let source =
4299 let vals = [| "rgb"; "bgr"; "gray" |] in
4300 (object
4301 inherit lvsourcebase
4303 initializer
4304 m_active <- int_of_colorspace conf.colorspace;
4305 m_first <- 0;
4307 method getitemcount = Array.length vals
4308 method getitem n = (vals.(n), 0)
4309 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4310 ignore (uioh, first, pan, qsearch);
4311 if not cancel then set active;
4312 None
4313 method hasaction _ = true
4314 end)
4316 state.text <- "";
4317 let modehash = findkeyhash conf "info" in
4318 coe (new listview ~source ~trusted:true ~modehash)
4319 )) :: m_l
4321 method fitmodel name get set =
4322 m_l <-
4323 (name, `string get, 1, Action (
4324 fun _ ->
4325 let source =
4326 let vals = [| "fit width"; "proportional"; "fit page" |] in
4327 (object
4328 inherit lvsourcebase
4330 initializer
4331 m_active <- int_of_fitmodel conf.fitmodel;
4332 m_first <- 0;
4334 method getitemcount = Array.length vals
4335 method getitem n = (vals.(n), 0)
4336 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4337 ignore (uioh, first, pan, qsearch);
4338 if not cancel then set active;
4339 None
4340 method hasaction _ = true
4341 end)
4343 state.text <- "";
4344 let modehash = findkeyhash conf "info" in
4345 coe (new listview ~source ~trusted:true ~modehash)
4346 )) :: m_l
4348 method caption s offset =
4349 m_l <- (s, `empty, offset, Noaction) :: m_l
4351 method caption2 s f offset =
4352 m_l <- (s, `string f, offset, Noaction) :: m_l
4354 method getitemcount = Array.length m_a
4356 method getitem n =
4357 let tostr = function
4358 | `int f -> string_of_int (f ())
4359 | `intws f -> string_with_suffix_of_int (f ())
4360 | `string f -> f ()
4361 | `color f -> color_to_string (f ())
4362 | `bool (btos, f) -> btos (f ())
4363 | `empty -> ""
4365 let name, t, offset, _ = m_a.(n) in
4366 ((let s = tostr t in
4367 if String.length s > 0
4368 then Printf.sprintf "%s\t%s" name s
4369 else name),
4370 offset)
4372 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4373 let uiohopt =
4374 if not cancel
4375 then (
4376 m_qsearch <- qsearch;
4377 let uioh =
4378 match m_a.(active) with
4379 | _, _, _, Action f -> f uioh
4380 | _ -> uioh
4382 Some uioh
4384 else None
4386 m_active <- active;
4387 m_first <- first;
4388 m_pan <- pan;
4389 uiohopt
4391 method hasaction n =
4392 match m_a.(n) with
4393 | _, _, _, Action _ -> true
4394 | _ -> false
4395 end)
4397 let rec fillsrc prevmode prevuioh =
4398 let sep () = src#caption "" 0 in
4399 let colorp name get set =
4400 src#string name
4401 (fun () -> color_to_string (get ()))
4402 (fun v ->
4404 let c = color_of_string v in
4405 set c
4406 with exn ->
4407 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4410 let oldmode = state.mode in
4411 let birdseye = isbirdseye state.mode in
4413 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4415 src#bool "presentation mode"
4416 (fun () -> conf.presentation)
4417 (fun v -> setpresentationmode v);
4419 src#bool "ignore case in searches"
4420 (fun () -> conf.icase)
4421 (fun v -> conf.icase <- v);
4423 src#bool "preload"
4424 (fun () -> conf.preload)
4425 (fun v -> conf.preload <- v);
4427 src#bool "highlight links"
4428 (fun () -> conf.hlinks)
4429 (fun v -> conf.hlinks <- v);
4431 src#bool "under info"
4432 (fun () -> conf.underinfo)
4433 (fun v -> conf.underinfo <- v);
4435 src#bool "persistent bookmarks"
4436 (fun () -> conf.savebmarks)
4437 (fun v -> conf.savebmarks <- v);
4439 src#fitmodel "fit model"
4440 (fun () -> fitmodel_to_string conf.fitmodel)
4441 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4443 src#bool "trim margins"
4444 (fun () -> conf.trimmargins)
4445 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4447 src#bool "persistent location"
4448 (fun () -> conf.jumpback)
4449 (fun v -> conf.jumpback <- v);
4451 sep ();
4452 src#int "inter-page space"
4453 (fun () -> conf.interpagespace)
4454 (fun n ->
4455 conf.interpagespace <- n;
4456 docolumns conf.columns;
4457 let pageno, py =
4458 match state.layout with
4459 | [] -> 0, 0
4460 | l :: _ ->
4461 l.pageno, l.pagey
4463 state.maxy <- calcheight ();
4464 let y = getpagey pageno in
4465 gotoy (y + py)
4468 src#int "page bias"
4469 (fun () -> conf.pagebias)
4470 (fun v -> conf.pagebias <- v);
4472 src#int "scroll step"
4473 (fun () -> conf.scrollstep)
4474 (fun n -> conf.scrollstep <- n);
4476 src#int "horizontal scroll step"
4477 (fun () -> conf.hscrollstep)
4478 (fun v -> conf.hscrollstep <- v);
4480 src#int "auto scroll step"
4481 (fun () ->
4482 match state.autoscroll with
4483 | Some step -> step
4484 | _ -> conf.autoscrollstep)
4485 (fun n ->
4486 if state.autoscroll <> None
4487 then state.autoscroll <- Some n;
4488 conf.autoscrollstep <- n);
4490 src#int "zoom"
4491 (fun () -> truncate (conf.zoom *. 100.))
4492 (fun v -> setzoom ((float v) /. 100.));
4494 src#int "rotation"
4495 (fun () -> conf.angle)
4496 (fun v -> reqlayout v conf.fitmodel);
4498 src#int "scroll bar width"
4499 (fun () -> state.scrollw)
4500 (fun v ->
4501 state.scrollw <- v;
4502 conf.scrollbw <- v;
4503 reshape state.winw state.winh;
4506 src#int "scroll handle height"
4507 (fun () -> conf.scrollh)
4508 (fun v -> conf.scrollh <- v;);
4510 src#int "thumbnail width"
4511 (fun () -> conf.thumbw)
4512 (fun v ->
4513 conf.thumbw <- min 4096 v;
4514 match oldmode with
4515 | Birdseye beye ->
4516 leavebirdseye beye false;
4517 enterbirdseye ()
4518 | _ -> ()
4521 let mode = state.mode in
4522 src#string "columns"
4523 (fun () ->
4524 match conf.columns with
4525 | Csingle _ -> "1"
4526 | Cmulti (multi, _) -> multicolumns_to_string multi
4527 | Csplit (count, _) -> "-" ^ string_of_int count
4529 (fun v ->
4530 let n, a, b = multicolumns_of_string v in
4531 setcolumns mode n a b);
4533 sep ();
4534 src#caption "Presentation mode" 0;
4535 src#bool "scrollbar visible"
4536 (fun () -> conf.scrollbarinpm)
4537 (fun v ->
4538 if v != conf.scrollbarinpm
4539 then (
4540 conf.scrollbarinpm <- v;
4541 if conf.presentation
4542 then (
4543 state.scrollw <- if v then conf.scrollbw else 0;
4544 reshape state.winw state.winh;
4549 sep ();
4550 src#caption "Pixmap cache" 0;
4551 src#int_with_suffix "size (advisory)"
4552 (fun () -> conf.memlimit)
4553 (fun v -> conf.memlimit <- v);
4555 src#caption2 "used"
4556 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4557 (string_with_suffix_of_int state.memused)
4558 (Hashtbl.length state.tilemap)) 1;
4560 sep ();
4561 src#caption "Layout" 0;
4562 src#caption2 "Dimension"
4563 (fun () ->
4564 Printf.sprintf "%dx%d (virtual %dx%d)"
4565 state.winw state.winh
4566 state.w state.maxy)
4568 if conf.debug
4569 then
4570 src#caption2 "Position" (fun () ->
4571 Printf.sprintf "%dx%d" state.x state.y
4573 else
4574 src#caption2 "Position" (fun () -> describe_location ()) 1
4577 sep ();
4578 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4579 "Save these parameters as global defaults at exit"
4580 (fun () -> conf.bedefault)
4581 (fun v -> conf.bedefault <- v)
4584 sep ();
4585 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4586 src#bool ~offset:0 ~btos "Extended parameters"
4587 (fun () -> !showextended)
4588 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4589 if !showextended
4590 then (
4591 src#bool "checkers"
4592 (fun () -> conf.checkers)
4593 (fun v -> conf.checkers <- v; setcheckers v);
4594 src#bool "update cursor"
4595 (fun () -> conf.updatecurs)
4596 (fun v -> conf.updatecurs <- v);
4597 src#bool "verbose"
4598 (fun () -> conf.verbose)
4599 (fun v -> conf.verbose <- v);
4600 src#bool "invert colors"
4601 (fun () -> conf.invert)
4602 (fun v -> conf.invert <- v);
4603 src#bool "max fit"
4604 (fun () -> conf.maxhfit)
4605 (fun v -> conf.maxhfit <- v);
4606 src#bool "redirect stderr"
4607 (fun () -> conf.redirectstderr)
4608 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4609 src#string "uri launcher"
4610 (fun () -> conf.urilauncher)
4611 (fun v -> conf.urilauncher <- v);
4612 src#string "path launcher"
4613 (fun () -> conf.pathlauncher)
4614 (fun v -> conf.pathlauncher <- v);
4615 src#string "tile size"
4616 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4617 (fun v ->
4619 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4620 conf.tilew <- max 64 w;
4621 conf.tileh <- max 64 h;
4622 flushtiles ();
4623 with exn ->
4624 state.text <- Printf.sprintf "bad tile size `%s': %s"
4625 v (exntos exn)
4627 src#int "texture count"
4628 (fun () -> conf.texcount)
4629 (fun v ->
4630 if realloctexts v
4631 then conf.texcount <- v
4632 else showtext '!' " Failed to set texture count please retry later"
4634 src#int "slice height"
4635 (fun () -> conf.sliceheight)
4636 (fun v ->
4637 conf.sliceheight <- v;
4638 wcmd "sliceh %d" conf.sliceheight;
4640 src#int "anti-aliasing level"
4641 (fun () -> conf.aalevel)
4642 (fun v ->
4643 conf.aalevel <- bound v 0 8;
4644 state.anchor <- getanchor ();
4645 opendoc state.path state.password;
4647 src#string "page scroll scaling factor"
4648 (fun () -> string_of_float conf.pgscale)
4649 (fun v ->
4651 let s = float_of_string v in
4652 conf.pgscale <- s
4653 with exn ->
4654 state.text <- Printf.sprintf
4655 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4658 src#int "ui font size"
4659 (fun () -> fstate.fontsize)
4660 (fun v -> setfontsize (bound v 5 100));
4661 src#int "hint font size"
4662 (fun () -> conf.hfsize)
4663 (fun v -> conf.hfsize <- bound v 5 100);
4664 colorp "background color"
4665 (fun () -> conf.bgcolor)
4666 (fun v -> conf.bgcolor <- v);
4667 src#bool "crop hack"
4668 (fun () -> conf.crophack)
4669 (fun v -> conf.crophack <- v);
4670 src#string "trim fuzz"
4671 (fun () -> irect_to_string conf.trimfuzz)
4672 (fun v ->
4674 conf.trimfuzz <- irect_of_string v;
4675 if conf.trimmargins
4676 then settrim true conf.trimfuzz;
4677 with exn ->
4678 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4680 src#string "throttle"
4681 (fun () ->
4682 match conf.maxwait with
4683 | None -> "show place holder if page is not ready"
4684 | Some time ->
4685 if time = infinity
4686 then "wait for page to fully render"
4687 else
4688 "wait " ^ string_of_float time
4689 ^ " seconds before showing placeholder"
4691 (fun v ->
4693 let f = float_of_string v in
4694 if f <= 0.0
4695 then conf.maxwait <- None
4696 else conf.maxwait <- Some f
4697 with exn ->
4698 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4700 src#string "ghyll scroll"
4701 (fun () ->
4702 match conf.ghyllscroll with
4703 | None -> ""
4704 | Some nab -> ghyllscroll_to_string nab
4706 (fun v ->
4708 let gs =
4709 if String.length v = 0
4710 then None
4711 else Some (ghyllscroll_of_string v)
4713 conf.ghyllscroll <- gs
4714 with exn ->
4715 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4717 src#string "selection command"
4718 (fun () -> conf.selcmd)
4719 (fun v -> conf.selcmd <- v);
4720 src#string "synctex command"
4721 (fun () -> conf.stcmd)
4722 (fun v -> conf.stcmd <- v);
4723 src#colorspace "color space"
4724 (fun () -> colorspace_to_string conf.colorspace)
4725 (fun v ->
4726 conf.colorspace <- colorspace_of_int v;
4727 wcmd "cs %d" v;
4728 load state.layout;
4730 if pbousable ()
4731 then
4732 src#bool "use PBO"
4733 (fun () -> conf.usepbo)
4734 (fun v -> conf.usepbo <- v);
4735 src#bool "mouse wheel scrolls pages"
4736 (fun () -> conf.wheelbypage)
4737 (fun v -> conf.wheelbypage <- v);
4740 sep ();
4741 src#caption "Document" 0;
4742 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4743 src#caption2 "Pages"
4744 (fun () -> string_of_int state.pagecount) 1;
4745 src#caption2 "Dimensions"
4746 (fun () -> string_of_int (List.length state.pdims)) 1;
4747 if conf.trimmargins
4748 then (
4749 sep ();
4750 src#caption "Trimmed margins" 0;
4751 src#caption2 "Dimensions"
4752 (fun () -> string_of_int (List.length state.pdims)) 1;
4755 sep ();
4756 src#caption "OpenGL" 0;
4757 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4758 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4760 sep ();
4761 src#caption "Location" 0;
4762 if String.length state.origin > 0
4763 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4764 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4766 src#reset prevmode prevuioh;
4768 fun () ->
4769 state.text <- "";
4770 let prevmode = state.mode
4771 and prevuioh = state.uioh in
4772 fillsrc prevmode prevuioh;
4773 let source = (src :> lvsource) in
4774 let modehash = findkeyhash conf "info" in
4775 state.uioh <- coe (object (self)
4776 inherit listview ~source ~trusted:true ~modehash as super
4777 val mutable m_prevmemused = 0
4778 method infochanged = function
4779 | Memused ->
4780 if m_prevmemused != state.memused
4781 then (
4782 m_prevmemused <- state.memused;
4783 G.postRedisplay "memusedchanged";
4785 | Pdim -> G.postRedisplay "pdimchanged"
4786 | Docinfo -> fillsrc prevmode prevuioh
4788 method key key mask =
4789 if not (Wsi.withctrl mask)
4790 then
4791 match key with
4792 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4793 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4794 | _ -> super#key key mask
4795 else super#key key mask
4796 end);
4797 G.postRedisplay "info";
4800 let enterhelpmode =
4801 let source =
4802 (object
4803 inherit lvsourcebase
4804 method getitemcount = Array.length state.help
4805 method getitem n =
4806 let s, l, _ = state.help.(n) in
4807 (s, l)
4809 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4810 let optuioh =
4811 if not cancel
4812 then (
4813 m_qsearch <- qsearch;
4814 match state.help.(active) with
4815 | _, _, Action f -> Some (f uioh)
4816 | _ -> Some (uioh)
4818 else None
4820 m_active <- active;
4821 m_first <- first;
4822 m_pan <- pan;
4823 optuioh
4825 method hasaction n =
4826 match state.help.(n) with
4827 | _, _, Action _ -> true
4828 | _ -> false
4830 initializer
4831 m_active <- -1
4832 end)
4833 in fun () ->
4834 let modehash = findkeyhash conf "help" in
4835 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4836 G.postRedisplay "help";
4839 let entermsgsmode =
4840 let msgsource =
4841 let re = Str.regexp "[\r\n]" in
4842 (object
4843 inherit lvsourcebase
4844 val mutable m_items = [||]
4846 method getitemcount = 1 + Array.length m_items
4848 method getitem n =
4849 if n = 0
4850 then "[Clear]", 0
4851 else m_items.(n-1), 0
4853 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4854 ignore uioh;
4855 if not cancel
4856 then (
4857 if active = 0
4858 then Buffer.clear state.errmsgs;
4859 m_qsearch <- qsearch;
4861 m_active <- active;
4862 m_first <- first;
4863 m_pan <- pan;
4864 None
4866 method hasaction n =
4867 n = 0
4869 method reset =
4870 state.newerrmsgs <- false;
4871 let l = Str.split re (Buffer.contents state.errmsgs) in
4872 m_items <- Array.of_list l
4874 initializer
4875 m_active <- 0
4876 end)
4877 in fun () ->
4878 state.text <- "";
4879 msgsource#reset;
4880 let source = (msgsource :> lvsource) in
4881 let modehash = findkeyhash conf "listview" in
4882 state.uioh <- coe (object
4883 inherit listview ~source ~trusted:false ~modehash as super
4884 method display =
4885 if state.newerrmsgs
4886 then msgsource#reset;
4887 super#display
4888 end);
4889 G.postRedisplay "msgs";
4892 let quickbookmark ?title () =
4893 match state.layout with
4894 | [] -> ()
4895 | l :: _ ->
4896 let title =
4897 match title with
4898 | None ->
4899 let sec = Unix.gettimeofday () in
4900 let tm = Unix.localtime sec in
4901 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4902 (l.pageno+1)
4903 tm.Unix.tm_mday
4904 tm.Unix.tm_mon
4905 (tm.Unix.tm_year + 1900)
4906 tm.Unix.tm_hour
4907 tm.Unix.tm_min
4908 | Some title -> title
4910 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4913 let setautoscrollspeed step goingdown =
4914 let incr = max 1 ((abs step) / 2) in
4915 let incr = if goingdown then incr else -incr in
4916 let astep = step + incr in
4917 state.autoscroll <- Some astep;
4920 let gotounder = function
4921 | Ulinkgoto (pageno, top) ->
4922 if pageno >= 0
4923 then (
4924 addnav ();
4925 gotopage1 pageno top;
4928 | Ulinkuri s ->
4929 gotouri s
4931 | Uremote (filename, pageno) ->
4932 let path =
4933 if Sys.file_exists filename
4934 then filename
4935 else
4936 let dir = Filename.dirname state.path in
4937 let path = Filename.concat dir filename in
4938 if Sys.file_exists path
4939 then path
4940 else ""
4942 if String.length path > 0
4943 then (
4944 let anchor = getanchor () in
4945 let ranchor = state.path, state.password, anchor, state.origin in
4946 state.origin <- "";
4947 state.anchor <- (pageno, 0.0, 0.0);
4948 state.ranchors <- ranchor :: state.ranchors;
4949 opendoc path "";
4951 else showtext '!' ("Could not find " ^ filename)
4953 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4956 let canpan () =
4957 match conf.columns with
4958 | Csplit _ -> true
4959 | _ -> state.x != 0 || conf.zoom > 1.0
4962 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4964 let existsinrow pageno (columns, coverA, coverB) p =
4965 let last = ((pageno - coverA) mod columns) + columns in
4966 let rec any = function
4967 | [] -> false
4968 | l :: rest ->
4969 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4970 then p l
4971 else (
4972 if not (p l)
4973 then (if l.pageno = last then false else any rest)
4974 else true
4977 any state.layout
4980 let nextpage () =
4981 match state.layout with
4982 | [] ->
4983 let pageno = page_of_y state.y in
4984 gotoghyll (getpagey (pageno+1))
4985 | l :: rest ->
4986 match conf.columns with
4987 | Csingle _ ->
4988 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4989 then
4990 let y = clamp (pgscale state.winh) in
4991 gotoghyll y
4992 else
4993 let pageno = min (l.pageno+1) (state.pagecount-1) in
4994 gotoghyll (getpagey pageno)
4995 | Cmulti ((c, _, _) as cl, _) ->
4996 if conf.presentation
4997 && (existsinrow l.pageno cl
4998 (fun l -> l.pageh > l.pagey + l.pagevh))
4999 then
5000 let y = clamp (pgscale state.winh) in
5001 gotoghyll y
5002 else
5003 let pageno = min (l.pageno+c) (state.pagecount-1) in
5004 gotoghyll (getpagey pageno)
5005 | Csplit (n, _) ->
5006 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5007 then
5008 let pagey, pageh = getpageyh l.pageno in
5009 let pagey = pagey + pageh * l.pagecol in
5010 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5011 gotoghyll (pagey + pageh + ips)
5014 let prevpage () =
5015 match state.layout with
5016 | [] ->
5017 let pageno = page_of_y state.y in
5018 gotoghyll (getpagey (pageno-1))
5019 | l :: _ ->
5020 match conf.columns with
5021 | Csingle _ ->
5022 if conf.presentation && l.pagey != 0
5023 then
5024 gotoghyll (clamp (pgscale ~-(state.winh)))
5025 else
5026 let pageno = max 0 (l.pageno-1) in
5027 gotoghyll (getpagey pageno)
5028 | Cmulti ((c, _, coverB) as cl, _) ->
5029 if conf.presentation &&
5030 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5031 then
5032 gotoghyll (clamp (pgscale ~-(state.winh)))
5033 else
5034 let decr =
5035 if l.pageno = state.pagecount - coverB
5036 then 1
5037 else c
5039 let pageno = max 0 (l.pageno-decr) in
5040 gotoghyll (getpagey pageno)
5041 | Csplit (n, _) ->
5042 let y =
5043 if l.pagecol = 0
5044 then
5045 if l.pageno = 0
5046 then l.pagey
5047 else
5048 let pageno = max 0 (l.pageno-1) in
5049 let pagey, pageh = getpageyh pageno in
5050 pagey + (n-1)*pageh
5051 else
5052 let pagey, pageh = getpageyh l.pageno in
5053 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5055 gotoghyll y
5058 let viewkeyboard key mask =
5059 let enttext te =
5060 let mode = state.mode in
5061 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5062 state.text <- "";
5063 enttext ();
5064 G.postRedisplay "view:enttext"
5066 let ctrl = Wsi.withctrl mask in
5067 let key =
5068 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5070 match key with
5071 | 81 -> (* Q *)
5072 exit 0
5074 | 0xff63 -> (* insert *)
5075 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5076 then (
5077 state.mode <- LinkNav (Ltgendir 0);
5078 gotoy state.y;
5080 else showtext '!' "Keyboard link navigation does not work under rotation"
5082 | 0xff1b | 113 -> (* escape / q *)
5083 begin match state.mstate with
5084 | Mzoomrect _ ->
5085 state.mstate <- Mnone;
5086 Wsi.setcursor Wsi.CURSOR_INHERIT;
5087 G.postRedisplay "kill zoom rect";
5088 | _ ->
5089 begin match state.mode with
5090 | LinkNav _ ->
5091 state.mode <- View;
5092 G.postRedisplay "esc leave linknav"
5093 | _ ->
5094 match state.ranchors with
5095 | [] -> raise Quit
5096 | (path, password, anchor, origin) :: rest ->
5097 state.ranchors <- rest;
5098 state.anchor <- anchor;
5099 state.origin <- origin;
5100 opendoc path password
5101 end;
5102 end;
5104 | 0xff08 -> (* backspace *)
5105 gotoghyll (getnav ~-1)
5107 | 111 -> (* o *)
5108 enteroutlinemode ()
5110 | 117 -> (* u *)
5111 state.rects <- [];
5112 state.text <- "";
5113 G.postRedisplay "dehighlight";
5115 | 47 | 63 -> (* / ? *)
5116 let ondone isforw s =
5117 cbput state.hists.pat s;
5118 state.searchpattern <- s;
5119 search s isforw
5121 let s = String.create 1 in
5122 s.[0] <- Char.chr key;
5123 enttext (s, "", Some (onhist state.hists.pat),
5124 textentry, ondone (key = 47), true)
5126 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5127 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5128 setzoom (conf.zoom +. incr)
5130 | 43 | 0xffab -> (* + *)
5131 let ondone s =
5132 let n =
5133 try int_of_string s with exc ->
5134 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5135 max_int
5137 if n != max_int
5138 then (
5139 conf.pagebias <- n;
5140 state.text <- "page bias is now " ^ string_of_int n;
5143 enttext ("page bias: ", "", None, intentry, ondone, true)
5145 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5146 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5147 setzoom (max 0.01 (conf.zoom -. decr))
5149 | 45 | 0xffad -> (* - *)
5150 let ondone msg = state.text <- msg in
5151 enttext (
5152 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5153 optentry state.mode, ondone, true
5156 | 48 when ctrl -> (* ctrl-0 *)
5157 if conf.zoom = 1.0
5158 then (
5159 state.x <- 0;
5160 state.hscrollh <-
5161 if state.w <= state.winw - state.scrollw
5162 then 0
5163 else state.scrollw
5165 gotoy state.y
5167 else setzoom 1.0
5169 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5170 let cols =
5171 match conf.columns with
5172 | Csingle _ | Cmulti _ -> 1
5173 | Csplit (n, _) -> n
5175 let h = state.winh -
5176 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5178 let zoom = zoomforh state.winw h state.scrollw cols in
5179 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5180 then setzoom zoom
5182 | 51 when ctrl -> (* ctrl-3 *)
5183 let fm =
5184 match conf.fitmodel with
5185 | FitWidth -> FitProportional
5186 | FitProportional -> FitPage
5187 | FitPage -> FitWidth
5189 state.text <- "fit model: " ^ fitmodel_to_string fm;
5190 reqlayout conf.angle fm
5192 | 0xffc6 -> (* f9 *)
5193 togglebirdseye ()
5195 | 57 when ctrl -> (* ctrl-9 *)
5196 togglebirdseye ()
5198 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5199 when not ctrl -> (* 0..9 *)
5200 let ondone s =
5201 let n =
5202 try int_of_string s with exc ->
5203 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5206 if n >= 0
5207 then (
5208 addnav ();
5209 cbput state.hists.pag (string_of_int n);
5210 gotopage1 (n + conf.pagebias - 1) 0;
5213 let pageentry text key =
5214 match Char.unsafe_chr key with
5215 | 'g' -> TEdone text
5216 | _ -> intentry text key
5218 let text = "x" in text.[0] <- Char.chr key;
5219 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5221 | 98 -> (* b *)
5222 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5223 reshape state.winw state.winh;
5225 | 108 -> (* l *)
5226 conf.hlinks <- not conf.hlinks;
5227 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5228 G.postRedisplay "toggle highlightlinks";
5230 | 70 -> (* F *)
5231 state.glinks <- true;
5232 let mode = state.mode in
5233 state.mode <- Textentry (
5234 (":", "", None, linknentry, linkndone gotounder, false),
5235 (fun _ ->
5236 state.glinks <- false;
5237 state.mode <- mode)
5239 state.text <- "";
5240 G.postRedisplay "view:linkent(F)"
5242 | 121 -> (* y *)
5243 state.glinks <- true;
5244 let mode = state.mode in
5245 state.mode <- Textentry (
5247 ":", "", None, linknentry, linkndone (fun under ->
5248 selstring (undertext under);
5249 ), false
5251 fun _ ->
5252 state.glinks <- false;
5253 state.mode <- mode
5255 state.text <- "";
5256 G.postRedisplay "view:linkent"
5258 | 97 -> (* a *)
5259 begin match state.autoscroll with
5260 | Some step ->
5261 conf.autoscrollstep <- step;
5262 state.autoscroll <- None
5263 | None ->
5264 if conf.autoscrollstep = 0
5265 then state.autoscroll <- Some 1
5266 else state.autoscroll <- Some conf.autoscrollstep
5269 | 112 when ctrl -> (* ctrl-p *)
5270 launchpath ()
5272 | 80 -> (* P *)
5273 setpresentationmode (not conf.presentation);
5274 showtext ' ' ("presentation mode " ^
5275 if conf.presentation then "on" else "off");
5277 | 102 -> (* f *)
5278 if List.mem Wsi.Fullscreen state.winstate
5279 then Wsi.reshape conf.cwinw conf.cwinh
5280 else Wsi.fullscreen ()
5282 | 112 | 78 -> (* p|N *)
5283 search state.searchpattern false
5285 | 110 | 0xffc0 -> (* n|F3 *)
5286 search state.searchpattern true
5288 | 116 -> (* t *)
5289 begin match state.layout with
5290 | [] -> ()
5291 | l :: _ ->
5292 gotoghyll (getpagey l.pageno)
5295 | 32 -> (* space *)
5296 nextpage ()
5298 | 0xff9f | 0xffff -> (* delete *)
5299 prevpage ()
5301 | 61 -> (* = *)
5302 showtext ' ' (describe_location ());
5304 | 119 -> (* w *)
5305 begin match state.layout with
5306 | [] -> ()
5307 | l :: _ ->
5308 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5309 G.postRedisplay "w"
5312 | 39 -> (* ' *)
5313 enterbookmarkmode ()
5315 | 104 | 0xffbe -> (* h|F1 *)
5316 enterhelpmode ()
5318 | 105 -> (* i *)
5319 enterinfomode ()
5321 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5322 entermsgsmode ()
5324 | 109 -> (* m *)
5325 let ondone s =
5326 match state.layout with
5327 | l :: _ ->
5328 if String.length s > 0
5329 then
5330 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5331 | _ -> ()
5333 enttext ("bookmark: ", "", None, textentry, ondone, true)
5335 | 126 -> (* ~ *)
5336 quickbookmark ();
5337 showtext ' ' "Quick bookmark added";
5339 | 122 -> (* z *)
5340 begin match state.layout with
5341 | l :: _ ->
5342 let rect = getpdimrect l.pagedimno in
5343 let w, h =
5344 if conf.crophack
5345 then
5346 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5347 truncate (1.2 *. (rect.(3) -. rect.(0))))
5348 else
5349 (truncate (rect.(1) -. rect.(0)),
5350 truncate (rect.(3) -. rect.(0)))
5352 let w = truncate ((float w)*.conf.zoom)
5353 and h = truncate ((float h)*.conf.zoom) in
5354 if w != 0 && h != 0
5355 then (
5356 state.anchor <- getanchor ();
5357 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5359 G.postRedisplay "z";
5361 | [] -> ()
5364 | 60 | 62 -> (* < > *)
5365 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5367 | 91 | 93 -> (* [ ] *)
5368 conf.colorscale <-
5369 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5371 G.postRedisplay "brightness";
5373 | 99 when state.mode = View -> (* [alt]-c *)
5374 if Wsi.withalt mask
5375 then (
5376 if conf.zoom > 1.0
5377 then
5378 let m = (state.winw - state.w - state.scrollw) / 2 in
5379 state.x <- m;
5380 gotoy_and_clear_text state.y
5382 else
5383 let (c, a, b), z =
5384 match state.prevcolumns with
5385 | None -> (1, 0, 0), 1.0
5386 | Some (columns, z) ->
5387 let cab =
5388 match columns with
5389 | Csplit (c, _) -> -c, 0, 0
5390 | Cmulti ((c, a, b), _) -> c, a, b
5391 | Csingle _ -> 1, 0, 0
5393 cab, z
5395 setcolumns View c a b;
5396 setzoom z
5398 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5399 setzoom state.prevzoom
5401 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5402 begin match state.autoscroll with
5403 | None ->
5404 begin match state.mode with
5405 | Birdseye beye -> upbirdseye 1 beye
5406 | _ ->
5407 if ctrl
5408 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5409 else (
5410 if not (Wsi.withshift mask) && conf.presentation
5411 then prevpage ()
5412 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5415 | Some n ->
5416 setautoscrollspeed n false
5419 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5420 begin match state.autoscroll with
5421 | None ->
5422 begin match state.mode with
5423 | Birdseye beye -> downbirdseye 1 beye
5424 | _ ->
5425 if ctrl
5426 then gotoy_and_clear_text (clamp (state.winh/2))
5427 else (
5428 if not (Wsi.withshift mask) && conf.presentation
5429 then nextpage ()
5430 else gotoy_and_clear_text (clamp conf.scrollstep)
5433 | Some n ->
5434 setautoscrollspeed n true
5437 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5438 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5439 if canpan ()
5440 then
5441 let dx =
5442 if ctrl
5443 then state.winw / 2
5444 else conf.hscrollstep
5446 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5447 state.x <- panbound (state.x + dx);
5448 gotoy_and_clear_text state.y
5449 else (
5450 state.text <- "";
5451 G.postRedisplay "left/right"
5454 | 0xff55 | 0xff9a -> (* (kp) prior *)
5455 let y =
5456 if ctrl
5457 then
5458 match state.layout with
5459 | [] -> state.y
5460 | l :: _ -> state.y - l.pagey
5461 else
5462 clamp (pgscale (-state.winh))
5464 gotoghyll y
5466 | 0xff56 | 0xff9b -> (* (kp) next *)
5467 let y =
5468 if ctrl
5469 then
5470 match List.rev state.layout with
5471 | [] -> state.y
5472 | l :: _ -> getpagey l.pageno
5473 else
5474 clamp (pgscale state.winh)
5476 gotoghyll y
5478 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5479 gotoghyll 0
5480 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5481 gotoghyll (clamp state.maxy)
5483 | 0xff53 | 0xff98
5484 when Wsi.withalt mask -> (* alt-(kp) right *)
5485 gotoghyll (getnav 1)
5486 | 0xff51 | 0xff96
5487 when Wsi.withalt mask -> (* alt-(kp) left *)
5488 gotoghyll (getnav ~-1)
5490 | 114 -> (* r *)
5491 reload ()
5493 | 118 when conf.debug -> (* v *)
5494 state.rects <- [];
5495 List.iter (fun l ->
5496 match getopaque l.pageno with
5497 | None -> ()
5498 | Some opaque ->
5499 let x0, y0, x1, y1 = pagebbox opaque in
5500 let a,b = float x0, float y0 in
5501 let c,d = float x1, float y0 in
5502 let e,f = float x1, float y1 in
5503 let h,j = float x0, float y1 in
5504 let rect = (a,b,c,d,e,f,h,j) in
5505 debugrect rect;
5506 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5507 ) state.layout;
5508 G.postRedisplay "v";
5510 | _ ->
5511 vlog "huh? %s" (Wsi.keyname key)
5514 let linknavkeyboard key mask linknav =
5515 let getpage pageno =
5516 let rec loop = function
5517 | [] -> None
5518 | l :: _ when l.pageno = pageno -> Some l
5519 | _ :: rest -> loop rest
5520 in loop state.layout
5522 let doexact (pageno, n) =
5523 match getopaque pageno, getpage pageno with
5524 | Some opaque, Some l ->
5525 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5526 then
5527 let under = getlink opaque n in
5528 G.postRedisplay "link gotounder";
5529 gotounder under;
5530 state.mode <- View;
5531 else
5532 let opt, dir =
5533 match key with
5534 | 0xff50 -> (* home *)
5535 Some (findlink opaque LDfirst), -1
5537 | 0xff57 -> (* end *)
5538 Some (findlink opaque LDlast), 1
5540 | 0xff51 -> (* left *)
5541 Some (findlink opaque (LDleft n)), -1
5543 | 0xff53 -> (* right *)
5544 Some (findlink opaque (LDright n)), 1
5546 | 0xff52 -> (* up *)
5547 Some (findlink opaque (LDup n)), -1
5549 | 0xff54 -> (* down *)
5550 Some (findlink opaque (LDdown n)), 1
5552 | _ -> None, 0
5554 let pwl l dir =
5555 begin match findpwl l.pageno dir with
5556 | Pwlnotfound -> ()
5557 | Pwl pageno ->
5558 let notfound dir =
5559 state.mode <- LinkNav (Ltgendir dir);
5560 let y, h = getpageyh pageno in
5561 let y =
5562 if dir < 0
5563 then y + h - state.winh
5564 else y
5566 gotoy y
5568 begin match getopaque pageno, getpage pageno with
5569 | Some opaque, Some _ ->
5570 let link =
5571 let ld = if dir > 0 then LDfirst else LDlast in
5572 findlink opaque ld
5574 begin match link with
5575 | Lfound m ->
5576 showlinktype (getlink opaque m);
5577 state.mode <- LinkNav (Ltexact (pageno, m));
5578 G.postRedisplay "linknav jpage";
5579 | _ -> notfound dir
5580 end;
5581 | _ -> notfound dir
5582 end;
5583 end;
5585 begin match opt with
5586 | Some Lnotfound -> pwl l dir;
5587 | Some (Lfound m) ->
5588 if m = n
5589 then pwl l dir
5590 else (
5591 let _, y0, _, y1 = getlinkrect opaque m in
5592 if y0 < l.pagey
5593 then gotopage1 l.pageno y0
5594 else (
5595 let d = fstate.fontsize + 1 in
5596 if y1 - l.pagey > l.pagevh - d
5597 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5598 else G.postRedisplay "linknav";
5600 showlinktype (getlink opaque m);
5601 state.mode <- LinkNav (Ltexact (l.pageno, m));
5604 | None -> viewkeyboard key mask
5605 end;
5606 | _ -> viewkeyboard key mask
5608 if key = 0xff63
5609 then (
5610 state.mode <- View;
5611 G.postRedisplay "leave linknav"
5613 else
5614 match linknav with
5615 | Ltgendir _ -> viewkeyboard key mask
5616 | Ltexact exact -> doexact exact
5619 let keyboard key mask =
5620 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5621 then wcmd "interrupt"
5622 else state.uioh <- state.uioh#key key mask
5625 let birdseyekeyboard key mask
5626 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5627 let incr =
5628 match conf.columns with
5629 | Csingle _ -> 1
5630 | Cmulti ((c, _, _), _) -> c
5631 | Csplit _ -> failwith "bird's eye split mode"
5633 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5634 match key with
5635 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5636 let y, h = getpageyh pageno in
5637 let top = (state.winh - h) / 2 in
5638 gotoy (max 0 (y - top))
5639 | 0xff0d (* enter *)
5640 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5641 | 0xff1b -> leavebirdseye beye true (* escape *)
5642 | 0xff52 -> upbirdseye incr beye (* up *)
5643 | 0xff54 -> downbirdseye incr beye (* down *)
5644 | 0xff51 -> upbirdseye 1 beye (* left *)
5645 | 0xff53 -> downbirdseye 1 beye (* right *)
5647 | 0xff55 -> (* prior *)
5648 begin match state.layout with
5649 | l :: _ ->
5650 if l.pagey != 0
5651 then (
5652 state.mode <- Birdseye (
5653 oconf, leftx, l.pageno, hooverpageno, anchor
5655 gotopage1 l.pageno 0;
5657 else (
5658 let layout = layout (state.y-state.winh) (pgh state.layout) in
5659 match layout with
5660 | [] -> gotoy (clamp (-state.winh))
5661 | l :: _ ->
5662 state.mode <- Birdseye (
5663 oconf, leftx, l.pageno, hooverpageno, anchor
5665 gotopage1 l.pageno 0
5668 | [] -> gotoy (clamp (-state.winh))
5669 end;
5671 | 0xff56 -> (* next *)
5672 begin match List.rev state.layout with
5673 | l :: _ ->
5674 let layout = layout (state.y + (pgh state.layout)) state.winh in
5675 begin match layout with
5676 | [] ->
5677 let incr = l.pageh - l.pagevh in
5678 if incr = 0
5679 then (
5680 state.mode <-
5681 Birdseye (
5682 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5684 G.postRedisplay "birdseye pagedown";
5686 else gotoy (clamp (incr + conf.interpagespace*2));
5688 | l :: _ ->
5689 state.mode <-
5690 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5691 gotopage1 l.pageno 0;
5694 | [] -> gotoy (clamp state.winh)
5695 end;
5697 | 0xff50 -> (* home *)
5698 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5699 gotopage1 0 0
5701 | 0xff57 -> (* end *)
5702 let pageno = state.pagecount - 1 in
5703 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5704 if not (pagevisible state.layout pageno)
5705 then
5706 let h =
5707 match List.rev state.pdims with
5708 | [] -> state.winh
5709 | (_, _, h, _) :: _ -> h
5711 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5712 else G.postRedisplay "birdseye end";
5713 | _ -> viewkeyboard key mask
5716 let drawpage l =
5717 let color =
5718 match state.mode with
5719 | Textentry _ -> scalecolor 0.4
5720 | LinkNav _
5721 | View -> scalecolor 1.0
5722 | Birdseye (_, _, pageno, hooverpageno, _) ->
5723 if l.pageno = hooverpageno
5724 then scalecolor 0.9
5725 else (
5726 if l.pageno = pageno
5727 then scalecolor 1.0
5728 else scalecolor 0.8
5731 drawtiles l color;
5734 let postdrawpage l linkindexbase =
5735 match getopaque l.pageno with
5736 | Some opaque ->
5737 if tileready l l.pagex l.pagey
5738 then
5739 let x = l.pagedispx - l.pagex
5740 and y = l.pagedispy - l.pagey in
5741 let hlmask =
5742 match conf.columns with
5743 | Csingle _ | Cmulti _ ->
5744 (if conf.hlinks then 1 else 0)
5745 + (if state.glinks
5746 && not (isbirdseye state.mode) then 2 else 0)
5747 | _ -> 0
5749 let s =
5750 match state.mode with
5751 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5752 | _ -> ""
5754 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5755 else 0
5756 | _ -> 0
5759 let scrollindicator () =
5760 let sbw, ph, sh = state.uioh#scrollph in
5761 let sbh, pw, sw = state.uioh#scrollpw in
5763 GlDraw.color (0.64, 0.64, 0.64);
5764 GlDraw.rect
5765 (float (state.winw - sbw), 0.)
5766 (float state.winw, float state.winh)
5768 GlDraw.rect
5769 (0., float (state.winh - sbh))
5770 (float (state.winw - state.scrollw - 1), float state.winh)
5772 GlDraw.color (0.0, 0.0, 0.0);
5774 GlDraw.rect
5775 (float (state.winw - sbw), ph)
5776 (float state.winw, ph +. sh)
5778 GlDraw.rect
5779 (pw, float (state.winh - sbh))
5780 (pw +. sw, float state.winh)
5784 let showsel () =
5785 match state.mstate with
5786 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5789 | Msel ((x0, y0), (x1, y1)) ->
5790 let rec loop = function
5791 | l :: ls ->
5792 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5793 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5794 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5795 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5796 then
5797 match getopaque l.pageno with
5798 | Some opaque ->
5799 let x0, y0 = pagetranslatepoint l x0 y0 in
5800 let x1, y1 = pagetranslatepoint l x1 y1 in
5801 seltext opaque (x0, y0, x1, y1);
5802 | _ -> ()
5803 else loop ls
5804 | [] -> ()
5806 loop state.layout
5809 let showrects rects =
5810 Gl.enable `blend;
5811 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5812 GlDraw.polygon_mode `both `fill;
5813 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5814 List.iter
5815 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5816 List.iter (fun l ->
5817 if l.pageno = pageno
5818 then (
5819 let dx = float (l.pagedispx - l.pagex) in
5820 let dy = float (l.pagedispy - l.pagey) in
5821 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5822 GlDraw.begins `quads;
5824 GlDraw.vertex2 (x0+.dx, y0+.dy);
5825 GlDraw.vertex2 (x1+.dx, y1+.dy);
5826 GlDraw.vertex2 (x2+.dx, y2+.dy);
5827 GlDraw.vertex2 (x3+.dx, y3+.dy);
5829 GlDraw.ends ();
5831 ) state.layout
5832 ) rects
5834 Gl.disable `blend;
5837 let display () =
5838 GlClear.color (scalecolor2 conf.bgcolor);
5839 GlClear.clear [`color];
5840 List.iter drawpage state.layout;
5841 let rects =
5842 match state.mode with
5843 | LinkNav (Ltexact (pageno, linkno)) ->
5844 begin match getopaque pageno with
5845 | Some opaque ->
5846 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5847 (pageno, 5, (
5848 float x0, float y0,
5849 float x1, float y0,
5850 float x1, float y1,
5851 float x0, float y1)
5852 ) :: state.rects
5853 | None -> state.rects
5855 | _ -> state.rects
5857 showrects rects;
5858 let rec postloop linkindexbase = function
5859 | l :: rest ->
5860 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5861 postloop linkindexbase rest
5862 | [] -> ()
5864 showsel ();
5865 postloop 0 state.layout;
5866 state.uioh#display;
5867 begin match state.mstate with
5868 | Mzoomrect ((x0, y0), (x1, y1)) ->
5869 Gl.enable `blend;
5870 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5871 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5872 GlDraw.rect (float x0, float y0)
5873 (float x1, float y1);
5874 Gl.disable `blend;
5875 | _ -> ()
5876 end;
5877 enttext ();
5878 scrollindicator ();
5879 Wsi.swapb ();
5882 let zoomrect x y x1 y1 =
5883 let x0 = min x x1
5884 and x1 = max x x1
5885 and y0 = min y y1 in
5886 gotoy (state.y + y0);
5887 state.anchor <- getanchor ();
5888 let zoom = (float state.w) /. float (x1 - x0) in
5889 let margin =
5890 match conf.fitmodel, conf.columns with
5891 | FitPage, Csplit _ ->
5892 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5894 | _, _ ->
5895 if state.w < state.winw - state.scrollw
5896 then (state.winw - state.scrollw - state.w) / 2
5897 else 0
5899 state.x <- (state.x + margin) - x0;
5900 setzoom zoom;
5901 Wsi.setcursor Wsi.CURSOR_INHERIT;
5902 state.mstate <- Mnone;
5905 let scrollx x =
5906 let winw = state.winw - state.scrollw - 1 in
5907 let s = float x /. float winw in
5908 let destx = truncate (float (state.w + winw) *. s) in
5909 state.x <- winw - destx;
5910 gotoy_and_clear_text state.y;
5911 state.mstate <- Mscrollx;
5914 let scrolly y =
5915 let s = float y /. float state.winh in
5916 let desty = truncate (float (state.maxy - state.winh) *. s) in
5917 gotoy_and_clear_text desty;
5918 state.mstate <- Mscrolly;
5921 let viewmouse button down x y mask =
5922 match button with
5923 | n when (n == 4 || n == 5) && not down ->
5924 if Wsi.withctrl mask
5925 then (
5926 match state.mstate with
5927 | Mzoom (oldn, i) ->
5928 if oldn = n
5929 then (
5930 if i = 2
5931 then
5932 let incr =
5933 match n with
5934 | 5 ->
5935 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5936 | _ ->
5937 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5939 let zoom = conf.zoom -. incr in
5940 setzoom zoom;
5941 state.mstate <- Mzoom (n, 0);
5942 else
5943 state.mstate <- Mzoom (n, i+1);
5945 else state.mstate <- Mzoom (n, 0)
5947 | _ -> state.mstate <- Mzoom (n, 0)
5949 else (
5950 match state.autoscroll with
5951 | Some step -> setautoscrollspeed step (n=4)
5952 | None ->
5953 if conf.wheelbypage || conf.presentation
5954 then (
5955 if n = 4
5956 then prevpage ()
5957 else nextpage ()
5959 else
5960 let incr =
5961 if n = 4
5962 then -conf.scrollstep
5963 else conf.scrollstep
5965 let incr = incr * 2 in
5966 let y = clamp incr in
5967 gotoy_and_clear_text y
5970 | n when (n = 6 || n = 7) && not down && canpan () ->
5971 state.x <-
5972 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5973 gotoy_and_clear_text state.y
5975 | 1 when Wsi.withshift mask ->
5976 state.mstate <- Mnone;
5977 if not down
5978 then (
5979 match unproject x y with
5980 | Some (pageno, ux, uy) ->
5981 let cmd = Printf.sprintf
5982 "%s %s %d %d %d"
5983 conf.stcmd state.path pageno ux uy
5985 popen cmd []
5986 | None -> ()
5989 | 1 when Wsi.withctrl mask ->
5990 if down
5991 then (
5992 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5993 state.mstate <- Mpan (x, y)
5995 else
5996 state.mstate <- Mnone
5998 | 3 ->
5999 if down
6000 then (
6001 Wsi.setcursor Wsi.CURSOR_CYCLE;
6002 let p = (x, y) in
6003 state.mstate <- Mzoomrect (p, p)
6005 else (
6006 match state.mstate with
6007 | Mzoomrect ((x0, y0), _) ->
6008 if abs (x-x0) > 10 && abs (y - y0) > 10
6009 then zoomrect x0 y0 x y
6010 else (
6011 state.mstate <- Mnone;
6012 Wsi.setcursor Wsi.CURSOR_INHERIT;
6013 G.postRedisplay "kill accidental zoom rect";
6015 | _ ->
6016 Wsi.setcursor Wsi.CURSOR_INHERIT;
6017 state.mstate <- Mnone
6020 | 1 when x > state.winw - state.scrollw ->
6021 if down
6022 then
6023 let _, position, sh = state.uioh#scrollph in
6024 if y > truncate position && y < truncate (position +. sh)
6025 then state.mstate <- Mscrolly
6026 else scrolly y
6027 else
6028 state.mstate <- Mnone
6030 | 1 when y > state.winh - state.hscrollh ->
6031 if down
6032 then
6033 let _, position, sw = state.uioh#scrollpw in
6034 if x > truncate position && x < truncate (position +. sw)
6035 then state.mstate <- Mscrollx
6036 else scrollx x
6037 else
6038 state.mstate <- Mnone
6040 | 1 ->
6041 let dest = if down then getunder x y else Unone in
6042 begin match dest with
6043 | Ulinkgoto _
6044 | Ulinkuri _
6045 | Uremote _
6046 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6047 gotounder dest
6049 | Unone when down ->
6050 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6051 state.mstate <- Mpan (x, y);
6053 | Unone | Utext _ ->
6054 if down
6055 then (
6056 if conf.angle mod 360 = 0
6057 then (
6058 state.mstate <- Msel ((x, y), (x, y));
6059 G.postRedisplay "mouse select";
6062 else (
6063 match state.mstate with
6064 | Mnone -> ()
6066 | Mzoom _ | Mscrollx | Mscrolly ->
6067 state.mstate <- Mnone
6069 | Mzoomrect ((x0, y0), _) ->
6070 zoomrect x0 y0 x y
6072 | Mpan _ ->
6073 Wsi.setcursor Wsi.CURSOR_INHERIT;
6074 state.mstate <- Mnone
6076 | Msel ((x0, y0), (x1, y1)) ->
6077 let rec loop = function
6078 | [] -> ()
6079 | l :: rest ->
6080 let inside =
6081 let a0 = l.pagedispy in
6082 let a1 = a0 + l.pagevh in
6083 let b0 = l.pagedispx in
6084 let b1 = b0 + l.pagevw in
6085 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6086 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6088 if inside
6089 then
6090 match getopaque l.pageno with
6091 | Some opaque ->
6092 begin
6093 match Ne.pipe () with
6094 | Ne.Exn exn ->
6095 showtext '!'
6096 (Printf.sprintf
6097 "can not create sel pipe: %s"
6098 (exntos exn));
6099 | Ne.Res (r, w) ->
6100 let doclose what fd =
6101 Ne.clo fd (fun msg ->
6102 dolog "%s close failed: %s" what msg)
6105 popen conf.selcmd [r, 0; w, -1];
6106 copysel w opaque;
6107 doclose "pipe/r" r;
6108 G.postRedisplay "copysel";
6109 with exn ->
6110 dolog "can not execute %S: %s"
6111 conf.selcmd (exntos exn);
6112 doclose "pipe/r" r;
6113 doclose "pipe/w" w;
6115 | None -> ()
6116 else loop rest
6118 loop state.layout;
6119 Wsi.setcursor Wsi.CURSOR_INHERIT;
6120 state.mstate <- Mnone;
6124 | _ -> ()
6127 let birdseyemouse button down x y mask
6128 (conf, leftx, _, hooverpageno, anchor) =
6129 match button with
6130 | 1 when down ->
6131 let rec loop = function
6132 | [] -> ()
6133 | l :: rest ->
6134 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6135 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6136 then (
6137 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6139 else loop rest
6141 loop state.layout
6142 | 3 -> ()
6143 | _ -> viewmouse button down x y mask
6146 let mouse button down x y mask =
6147 state.uioh <- state.uioh#button button down x y mask;
6150 let motion ~x ~y =
6151 state.uioh <- state.uioh#motion x y
6154 let pmotion ~x ~y =
6155 state.uioh <- state.uioh#pmotion x y;
6158 let uioh = object
6159 method display = ()
6161 method key key mask =
6162 begin match state.mode with
6163 | Textentry textentry -> textentrykeyboard key mask textentry
6164 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6165 | View -> viewkeyboard key mask
6166 | LinkNav linknav -> linknavkeyboard key mask linknav
6167 end;
6168 state.uioh
6170 method button button bstate x y mask =
6171 begin match state.mode with
6172 | LinkNav _
6173 | View -> viewmouse button bstate x y mask
6174 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6175 | Textentry _ -> ()
6176 end;
6177 state.uioh
6179 method motion x y =
6180 begin match state.mode with
6181 | Textentry _ -> ()
6182 | View | Birdseye _ | LinkNav _ ->
6183 match state.mstate with
6184 | Mzoom _ | Mnone -> ()
6186 | Mpan (x0, y0) ->
6187 let dx = x - x0
6188 and dy = y0 - y in
6189 state.mstate <- Mpan (x, y);
6190 if canpan ()
6191 then state.x <- panbound (state.x + dx);
6192 let y = clamp dy in
6193 gotoy_and_clear_text y
6195 | Msel (a, _) ->
6196 state.mstate <- Msel (a, (x, y));
6197 G.postRedisplay "motion select";
6199 | Mscrolly ->
6200 let y = min state.winh (max 0 y) in
6201 scrolly y
6203 | Mscrollx ->
6204 let x = min state.winw (max 0 x) in
6205 scrollx x
6207 | Mzoomrect (p0, _) ->
6208 state.mstate <- Mzoomrect (p0, (x, y));
6209 G.postRedisplay "motion zoomrect";
6210 end;
6211 state.uioh
6213 method pmotion x y =
6214 begin match state.mode with
6215 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6216 let rec loop = function
6217 | [] ->
6218 if hooverpageno != -1
6219 then (
6220 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6221 G.postRedisplay "pmotion birdseye no hoover";
6223 | l :: rest ->
6224 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6225 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6226 then (
6227 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6228 G.postRedisplay "pmotion birdseye hoover";
6230 else loop rest
6232 loop state.layout
6234 | Textentry _ -> ()
6236 | LinkNav _
6237 | View ->
6238 match state.mstate with
6239 | Mnone -> updateunder x y
6240 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6242 end;
6243 state.uioh
6245 method infochanged _ = ()
6247 method scrollph =
6248 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6249 let p, h =
6250 if maxy = 0
6251 then 0.0, float state.winh
6252 else scrollph state.y maxy
6254 state.scrollw, p, h
6256 method scrollpw =
6257 let winw = state.winw - state.scrollw in
6258 let fwinw = float winw in
6259 let sw =
6260 let sw = fwinw /. float state.w in
6261 let sw = fwinw *. sw in
6262 max sw (float conf.scrollh)
6264 let position =
6265 let maxx = state.w + winw in
6266 let x = winw - state.x in
6267 let percent = float x /. float maxx in
6268 (fwinw -. sw) *. percent
6270 state.hscrollh, position, sw
6272 method modehash =
6273 let modename =
6274 match state.mode with
6275 | LinkNav _ -> "links"
6276 | Textentry _ -> "textentry"
6277 | Birdseye _ -> "birdseye"
6278 | View -> "view"
6280 findkeyhash conf modename
6282 method eformsgs = true
6283 end;;
6285 module Config =
6286 struct
6287 open Parser
6289 let fontpath = ref "";;
6291 module KeyMap =
6292 Map.Make (struct type t = (int * int) let compare = compare end);;
6294 let unent s =
6295 let l = String.length s in
6296 let b = Buffer.create l in
6297 unent b s 0 l;
6298 Buffer.contents b;
6301 let home =
6302 try Sys.getenv "HOME"
6303 with exn ->
6304 prerr_endline
6305 ("Can not determine home directory location: " ^ exntos exn);
6309 let modifier_of_string = function
6310 | "alt" -> Wsi.altmask
6311 | "shift" -> Wsi.shiftmask
6312 | "ctrl" | "control" -> Wsi.ctrlmask
6313 | "meta" -> Wsi.metamask
6314 | _ -> 0
6317 let key_of_string =
6318 let r = Str.regexp "-" in
6319 fun s ->
6320 let elems = Str.full_split r s in
6321 let f n k m =
6322 let g s =
6323 let m1 = modifier_of_string s in
6324 if m1 = 0
6325 then (Wsi.namekey s, m)
6326 else (k, m lor m1)
6327 in function
6328 | Str.Delim s when n land 1 = 0 -> g s
6329 | Str.Text s -> g s
6330 | Str.Delim _ -> (k, m)
6332 let rec loop n k m = function
6333 | [] -> (k, m)
6334 | x :: xs ->
6335 let k, m = f n k m x in
6336 loop (n+1) k m xs
6338 loop 0 0 0 elems
6341 let keys_of_string =
6342 let r = Str.regexp "[ \t]" in
6343 fun s ->
6344 let elems = Str.split r s in
6345 List.map key_of_string elems
6348 let copykeyhashes c =
6349 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6352 let config_of c attrs =
6353 let apply c k v =
6355 match k with
6356 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6357 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6358 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6359 | "preload" -> { c with preload = bool_of_string v }
6360 | "page-bias" -> { c with pagebias = int_of_string v }
6361 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6362 | "horizontal-scroll-step" ->
6363 { c with hscrollstep = max (int_of_string v) 1 }
6364 | "auto-scroll-step" ->
6365 { c with autoscrollstep = max 0 (int_of_string v) }
6366 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6367 | "crop-hack" -> { c with crophack = bool_of_string v }
6368 | "throttle" ->
6369 let mw =
6370 match String.lowercase v with
6371 | "true" -> Some infinity
6372 | "false" -> None
6373 | f -> Some (float_of_string f)
6375 { c with maxwait = mw}
6376 | "highlight-links" -> { c with hlinks = bool_of_string v }
6377 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6378 | "vertical-margin" ->
6379 { c with interpagespace = max 0 (int_of_string v) }
6380 | "zoom" ->
6381 let zoom = float_of_string v /. 100. in
6382 let zoom = max zoom 0.0 in
6383 { c with zoom = zoom }
6384 | "presentation" -> { c with presentation = bool_of_string v }
6385 | "rotation-angle" -> { c with angle = int_of_string v }
6386 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6387 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6388 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6389 | "proportional-display" ->
6390 let fm =
6391 if bool_of_string v
6392 then FitProportional
6393 else FitWidth
6395 { c with fitmodel = fm }
6396 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6397 | "pixmap-cache-size" ->
6398 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6399 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6400 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6401 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6402 | "persistent-location" -> { c with jumpback = bool_of_string v }
6403 | "background-color" -> { c with bgcolor = color_of_string v }
6404 | "scrollbar-in-presentation" ->
6405 { c with scrollbarinpm = bool_of_string v }
6406 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6407 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6408 | "mupdf-store-size" ->
6409 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6410 | "checkers" -> { c with checkers = bool_of_string v }
6411 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6412 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6413 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6414 | "uri-launcher" -> { c with urilauncher = unent v }
6415 | "path-launcher" -> { c with pathlauncher = unent v }
6416 | "color-space" -> { c with colorspace = colorspace_of_string v }
6417 | "invert-colors" -> { c with invert = bool_of_string v }
6418 | "brightness" -> { c with colorscale = float_of_string v }
6419 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6420 | "ghyllscroll" ->
6421 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6422 | "columns" ->
6423 let (n, _, _) as nab = multicolumns_of_string v in
6424 if n < 0
6425 then { c with columns = Csplit (-n, [||]) }
6426 else { c with columns = Cmulti (nab, [||]) }
6427 | "birds-eye-columns" ->
6428 { c with beyecolumns = Some (max (int_of_string v) 2) }
6429 | "selection-command" -> { c with selcmd = unent v }
6430 | "synctex-command" -> { c with stcmd = unent v }
6431 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6432 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6433 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6434 | "use-pbo" -> { c with usepbo = bool_of_string v }
6435 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6436 | _ -> c
6437 with exn ->
6438 prerr_endline ("Error processing attribute (`" ^
6439 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6442 let rec fold c = function
6443 | [] -> c
6444 | (k, v) :: rest ->
6445 let c = apply c k v in
6446 fold c rest
6448 fold { c with keyhashes = copykeyhashes c } attrs;
6451 let fromstring f pos n v d =
6452 try f v
6453 with exn ->
6454 dolog "Error processing attribute (%S=%S) at %d\n%s"
6455 n v pos (exntos exn)
6460 let bookmark_of attrs =
6461 let rec fold title page rely visy = function
6462 | ("title", v) :: rest -> fold v page rely visy rest
6463 | ("page", v) :: rest -> fold title v rely visy rest
6464 | ("rely", v) :: rest -> fold title page v visy rest
6465 | ("visy", v) :: rest -> fold title page rely v rest
6466 | _ :: rest -> fold title page rely visy rest
6467 | [] -> title, page, rely, visy
6469 fold "invalid" "0" "0" "0" attrs
6472 let doc_of attrs =
6473 let rec fold path page rely pan visy = function
6474 | ("path", v) :: rest -> fold v page rely pan visy rest
6475 | ("page", v) :: rest -> fold path v rely pan visy rest
6476 | ("rely", v) :: rest -> fold path page v pan visy rest
6477 | ("pan", v) :: rest -> fold path page rely v visy rest
6478 | ("visy", v) :: rest -> fold path page rely pan v rest
6479 | _ :: rest -> fold path page rely pan visy rest
6480 | [] -> path, page, rely, pan, visy
6482 fold "" "0" "0" "0" "0" attrs
6485 let map_of attrs =
6486 let rec fold rs ls = function
6487 | ("out", v) :: rest -> fold v ls rest
6488 | ("in", v) :: rest -> fold rs v rest
6489 | _ :: rest -> fold ls rs rest
6490 | [] -> ls, rs
6492 fold "" "" attrs
6495 let setconf dst src =
6496 dst.scrollbw <- src.scrollbw;
6497 dst.scrollh <- src.scrollh;
6498 dst.icase <- src.icase;
6499 dst.preload <- src.preload;
6500 dst.pagebias <- src.pagebias;
6501 dst.verbose <- src.verbose;
6502 dst.scrollstep <- src.scrollstep;
6503 dst.maxhfit <- src.maxhfit;
6504 dst.crophack <- src.crophack;
6505 dst.autoscrollstep <- src.autoscrollstep;
6506 dst.maxwait <- src.maxwait;
6507 dst.hlinks <- src.hlinks;
6508 dst.underinfo <- src.underinfo;
6509 dst.interpagespace <- src.interpagespace;
6510 dst.zoom <- src.zoom;
6511 dst.presentation <- src.presentation;
6512 dst.angle <- src.angle;
6513 dst.cwinw <- src.cwinw;
6514 dst.cwinh <- src.cwinh;
6515 dst.savebmarks <- src.savebmarks;
6516 dst.memlimit <- src.memlimit;
6517 dst.fitmodel <- src.fitmodel;
6518 dst.texcount <- src.texcount;
6519 dst.sliceheight <- src.sliceheight;
6520 dst.thumbw <- src.thumbw;
6521 dst.jumpback <- src.jumpback;
6522 dst.bgcolor <- src.bgcolor;
6523 dst.scrollbarinpm <- src.scrollbarinpm;
6524 dst.tilew <- src.tilew;
6525 dst.tileh <- src.tileh;
6526 dst.mustoresize <- src.mustoresize;
6527 dst.checkers <- src.checkers;
6528 dst.aalevel <- src.aalevel;
6529 dst.trimmargins <- src.trimmargins;
6530 dst.trimfuzz <- src.trimfuzz;
6531 dst.urilauncher <- src.urilauncher;
6532 dst.colorspace <- src.colorspace;
6533 dst.invert <- src.invert;
6534 dst.colorscale <- src.colorscale;
6535 dst.redirectstderr <- src.redirectstderr;
6536 dst.ghyllscroll <- src.ghyllscroll;
6537 dst.columns <- src.columns;
6538 dst.beyecolumns <- src.beyecolumns;
6539 dst.selcmd <- src.selcmd;
6540 dst.updatecurs <- src.updatecurs;
6541 dst.pathlauncher <- src.pathlauncher;
6542 dst.keyhashes <- copykeyhashes src;
6543 dst.hfsize <- src.hfsize;
6544 dst.hscrollstep <- src.hscrollstep;
6545 dst.pgscale <- src.pgscale;
6546 dst.usepbo <- src.usepbo;
6547 dst.wheelbypage <- src.wheelbypage;
6548 dst.stcmd <- src.stcmd;
6551 let get s =
6552 let h = Hashtbl.create 10 in
6553 let dc = { defconf with angle = defconf.angle } in
6554 let rec toplevel v t spos _ =
6555 match t with
6556 | Vdata | Vcdata | Vend -> v
6557 | Vopen ("llppconfig", _, closed) ->
6558 if closed
6559 then v
6560 else { v with f = llppconfig }
6561 | Vopen _ ->
6562 error "unexpected subelement at top level" s spos
6563 | Vclose _ -> error "unexpected close at top level" s spos
6565 and llppconfig v t spos _ =
6566 match t with
6567 | Vdata | Vcdata -> v
6568 | Vend -> error "unexpected end of input in llppconfig" s spos
6569 | Vopen ("defaults", attrs, closed) ->
6570 let c = config_of dc attrs in
6571 setconf dc c;
6572 if closed
6573 then v
6574 else { v with f = defaults }
6576 | Vopen ("ui-font", attrs, closed) ->
6577 let rec getsize size = function
6578 | [] -> size
6579 | ("size", v) :: rest ->
6580 let size =
6581 fromstring int_of_string spos "size" v fstate.fontsize in
6582 getsize size rest
6583 | l -> getsize size l
6585 fstate.fontsize <- getsize fstate.fontsize attrs;
6586 if closed
6587 then v
6588 else { v with f = uifont (Buffer.create 10) }
6590 | Vopen ("doc", attrs, closed) ->
6591 let pathent, spage, srely, span, svisy = doc_of attrs in
6592 let path = unent pathent
6593 and pageno = fromstring int_of_string spos "page" spage 0
6594 and rely = fromstring float_of_string spos "rely" srely 0.0
6595 and pan = fromstring int_of_string spos "pan" span 0
6596 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6597 let c = config_of dc attrs in
6598 let anchor = (pageno, rely, visy) in
6599 if closed
6600 then (Hashtbl.add h path (c, [], pan, anchor); v)
6601 else { v with f = doc path pan anchor c [] }
6603 | Vopen _ ->
6604 error "unexpected subelement in llppconfig" s spos
6606 | Vclose "llppconfig" -> { v with f = toplevel }
6607 | Vclose _ -> error "unexpected close in llppconfig" s spos
6609 and defaults v t spos _ =
6610 match t with
6611 | Vdata | Vcdata -> v
6612 | Vend -> error "unexpected end of input in defaults" s spos
6613 | Vopen ("keymap", attrs, closed) ->
6614 let modename =
6615 try List.assoc "mode" attrs
6616 with Not_found -> "global" in
6617 if closed
6618 then v
6619 else
6620 let ret keymap =
6621 let h = findkeyhash dc modename in
6622 KeyMap.iter (Hashtbl.replace h) keymap;
6623 defaults
6625 { v with f = pkeymap ret KeyMap.empty }
6627 | Vopen (_, _, _) ->
6628 error "unexpected subelement in defaults" s spos
6630 | Vclose "defaults" ->
6631 { v with f = llppconfig }
6633 | Vclose _ -> error "unexpected close in defaults" s spos
6635 and uifont b v t spos epos =
6636 match t with
6637 | Vdata | Vcdata ->
6638 Buffer.add_substring b s spos (epos - spos);
6640 | Vopen (_, _, _) ->
6641 error "unexpected subelement in ui-font" s spos
6642 | Vclose "ui-font" ->
6643 if String.length !fontpath = 0
6644 then fontpath := Buffer.contents b;
6645 { v with f = llppconfig }
6646 | Vclose _ -> error "unexpected close in ui-font" s spos
6647 | Vend -> error "unexpected end of input in ui-font" s spos
6649 and doc path pan anchor c bookmarks v t spos _ =
6650 match t with
6651 | Vdata | Vcdata -> v
6652 | Vend -> error "unexpected end of input in doc" s spos
6653 | Vopen ("bookmarks", _, closed) ->
6654 if closed
6655 then v
6656 else { v with f = pbookmarks path pan anchor c bookmarks }
6658 | Vopen ("keymap", attrs, closed) ->
6659 let modename =
6660 try List.assoc "mode" attrs
6661 with Not_found -> "global"
6663 if closed
6664 then v
6665 else
6666 let ret keymap =
6667 let h = findkeyhash c modename in
6668 KeyMap.iter (Hashtbl.replace h) keymap;
6669 doc path pan anchor c bookmarks
6671 { v with f = pkeymap ret KeyMap.empty }
6673 | Vopen (_, _, _) ->
6674 error "unexpected subelement in doc" s spos
6676 | Vclose "doc" ->
6677 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6678 { v with f = llppconfig }
6680 | Vclose _ -> error "unexpected close in doc" s spos
6682 and pkeymap ret keymap v t spos _ =
6683 match t with
6684 | Vdata | Vcdata -> v
6685 | Vend -> error "unexpected end of input in keymap" s spos
6686 | Vopen ("map", attrs, closed) ->
6687 let r, l = map_of attrs in
6688 let kss = fromstring keys_of_string spos "in" r [] in
6689 let lss = fromstring keys_of_string spos "out" l [] in
6690 let keymap =
6691 match kss with
6692 | [] -> keymap
6693 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6694 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6696 if closed
6697 then { v with f = pkeymap ret keymap }
6698 else
6699 let f () = v in
6700 { v with f = skip "map" f }
6702 | Vopen _ ->
6703 error "unexpected subelement in keymap" s spos
6705 | Vclose "keymap" ->
6706 { v with f = ret keymap }
6708 | Vclose _ -> error "unexpected close in keymap" s spos
6710 and pbookmarks path pan anchor c bookmarks v t spos _ =
6711 match t with
6712 | Vdata | Vcdata -> v
6713 | Vend -> error "unexpected end of input in bookmarks" s spos
6714 | Vopen ("item", attrs, closed) ->
6715 let titleent, spage, srely, svisy = bookmark_of attrs in
6716 let page = fromstring int_of_string spos "page" spage 0
6717 and rely = fromstring float_of_string spos "rely" srely 0.0
6718 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6719 let bookmarks =
6720 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6722 if closed
6723 then { v with f = pbookmarks path pan anchor c bookmarks }
6724 else
6725 let f () = v in
6726 { v with f = skip "item" f }
6728 | Vopen _ ->
6729 error "unexpected subelement in bookmarks" s spos
6731 | Vclose "bookmarks" ->
6732 { v with f = doc path pan anchor c bookmarks }
6734 | Vclose _ -> error "unexpected close in bookmarks" s spos
6736 and skip tag f v t spos _ =
6737 match t with
6738 | Vdata | Vcdata -> v
6739 | Vend ->
6740 error ("unexpected end of input in skipped " ^ tag) s spos
6741 | Vopen (tag', _, closed) ->
6742 if closed
6743 then v
6744 else
6745 let f' () = { v with f = skip tag f } in
6746 { v with f = skip tag' f' }
6747 | Vclose ctag ->
6748 if tag = ctag
6749 then f ()
6750 else error ("unexpected close in skipped " ^ tag) s spos
6753 parse { f = toplevel; accu = () } s;
6754 h, dc;
6757 let do_load f ic =
6759 let len = in_channel_length ic in
6760 let s = String.create len in
6761 really_input ic s 0 len;
6762 f s;
6763 with
6764 | Parse_error (msg, s, pos) ->
6765 let subs = subs s pos in
6766 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6767 failwith ("parse error: " ^ s)
6769 | exn ->
6770 failwith ("config load error: " ^ exntos exn)
6773 let defconfpath =
6774 let dir =
6776 let dir = Filename.concat home ".config" in
6777 if Sys.is_directory dir then dir else home
6778 with _ -> home
6780 Filename.concat dir "llpp.conf"
6783 let confpath = ref defconfpath;;
6785 let load1 f =
6786 if Sys.file_exists !confpath
6787 then
6788 match
6789 (try Some (open_in_bin !confpath)
6790 with exn ->
6791 prerr_endline
6792 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6793 exntos exn);
6794 None
6796 with
6797 | Some ic ->
6798 let success =
6800 f (do_load get ic)
6801 with exn ->
6802 prerr_endline
6803 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6804 exntos exn);
6805 false
6807 close_in ic;
6808 success
6810 | None -> false
6811 else
6812 f (Hashtbl.create 0, defconf)
6815 let load () =
6816 let f (h, dc) =
6817 let pc, pb, px, pa =
6819 let key =
6820 if String.length state.origin = 0
6821 then state.path
6822 else state.origin
6824 Hashtbl.find h (Filename.basename key)
6825 with Not_found -> dc, [], 0, emptyanchor
6827 setconf defconf dc;
6828 setconf conf pc;
6829 state.bookmarks <- pb;
6830 state.x <- px;
6831 state.scrollw <- conf.scrollbw;
6832 if conf.jumpback
6833 then state.anchor <- pa;
6834 cbput state.hists.nav pa;
6835 true
6837 load1 f
6840 let add_attrs bb always dc c =
6841 let ob s a b =
6842 if always || a != b
6843 then Printf.bprintf bb "\n %s='%b'" s a
6844 and oi s a b =
6845 if always || a != b
6846 then Printf.bprintf bb "\n %s='%d'" s a
6847 and oI s a b =
6848 if always || a != b
6849 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6850 and oz s a b =
6851 if always || a <> b
6852 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6853 and oF s a b =
6854 if always || a <> b
6855 then Printf.bprintf bb "\n %s='%f'" s a
6856 and oc s a b =
6857 if always || a <> b
6858 then
6859 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6860 and oC s a b =
6861 if always || a <> b
6862 then
6863 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6864 and oR s a b =
6865 if always || a <> b
6866 then
6867 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6868 and os s a b =
6869 if always || a <> b
6870 then
6871 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6872 and og s a b =
6873 if always || a <> b
6874 then
6875 match a with
6876 | None -> ()
6877 | Some (_N, _A, _B) ->
6878 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6879 and oW s a b =
6880 if always || a <> b
6881 then
6882 let v =
6883 match a with
6884 | None -> "false"
6885 | Some f ->
6886 if f = infinity
6887 then "true"
6888 else string_of_float f
6890 Printf.bprintf bb "\n %s='%s'" s v
6891 and oco s a b =
6892 if always || a <> b
6893 then
6894 match a with
6895 | Cmulti ((n, a, b), _) when n > 1 ->
6896 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6897 | Csplit (n, _) when n > 1 ->
6898 Printf.bprintf bb "\n %s='%d'" s ~-n
6899 | _ -> ()
6900 and obeco s a b =
6901 if always || a <> b
6902 then
6903 match a with
6904 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6905 | _ -> ()
6906 and oFm s a b =
6907 if always || a <> b
6908 then
6909 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6911 oi "width" c.cwinw dc.cwinw;
6912 oi "height" c.cwinh dc.cwinh;
6913 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6914 oi "scroll-handle-height" c.scrollh dc.scrollh;
6915 ob "case-insensitive-search" c.icase dc.icase;
6916 ob "preload" c.preload dc.preload;
6917 oi "page-bias" c.pagebias dc.pagebias;
6918 oi "scroll-step" c.scrollstep dc.scrollstep;
6919 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6920 ob "max-height-fit" c.maxhfit dc.maxhfit;
6921 ob "crop-hack" c.crophack dc.crophack;
6922 oW "throttle" c.maxwait dc.maxwait;
6923 ob "highlight-links" c.hlinks dc.hlinks;
6924 ob "under-cursor-info" c.underinfo dc.underinfo;
6925 oi "vertical-margin" c.interpagespace dc.interpagespace;
6926 oz "zoom" c.zoom dc.zoom;
6927 ob "presentation" c.presentation dc.presentation;
6928 oi "rotation-angle" c.angle dc.angle;
6929 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6930 oFm "fit-model" c.fitmodel dc.fitmodel;
6931 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6932 oi "tex-count" c.texcount dc.texcount;
6933 oi "slice-height" c.sliceheight dc.sliceheight;
6934 oi "thumbnail-width" c.thumbw dc.thumbw;
6935 ob "persistent-location" c.jumpback dc.jumpback;
6936 oc "background-color" c.bgcolor dc.bgcolor;
6937 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6938 oi "tile-width" c.tilew dc.tilew;
6939 oi "tile-height" c.tileh dc.tileh;
6940 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6941 ob "checkers" c.checkers dc.checkers;
6942 oi "aalevel" c.aalevel dc.aalevel;
6943 ob "trim-margins" c.trimmargins dc.trimmargins;
6944 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6945 os "uri-launcher" c.urilauncher dc.urilauncher;
6946 os "path-launcher" c.pathlauncher dc.pathlauncher;
6947 oC "color-space" c.colorspace dc.colorspace;
6948 ob "invert-colors" c.invert dc.invert;
6949 oF "brightness" c.colorscale dc.colorscale;
6950 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6951 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6952 oco "columns" c.columns dc.columns;
6953 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6954 os "selection-command" c.selcmd dc.selcmd;
6955 os "synctex-command" c.stcmd dc.stcmd;
6956 ob "update-cursor" c.updatecurs dc.updatecurs;
6957 oi "hint-font-size" c.hfsize dc.hfsize;
6958 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6959 oF "page-scroll-scale" c.pgscale dc.pgscale;
6960 ob "use-pbo" c.usepbo dc.usepbo;
6961 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6964 let keymapsbuf always dc c =
6965 let bb = Buffer.create 16 in
6966 let rec loop = function
6967 | [] -> ()
6968 | (modename, h) :: rest ->
6969 let dh = findkeyhash dc modename in
6970 if always || h <> dh
6971 then (
6972 if Hashtbl.length h > 0
6973 then (
6974 if Buffer.length bb > 0
6975 then Buffer.add_char bb '\n';
6976 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6977 Hashtbl.iter (fun i o ->
6978 let isdifferent = always ||
6980 let dO = Hashtbl.find dh i in
6981 dO <> o
6982 with Not_found -> true
6984 if isdifferent
6985 then
6986 let addkm (k, m) =
6987 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6988 if Wsi.withalt m then Buffer.add_string bb "alt-";
6989 if Wsi.withshift m then Buffer.add_string bb "shift-";
6990 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6991 Buffer.add_string bb (Wsi.keyname k);
6993 let addkms l =
6994 let rec loop = function
6995 | [] -> ()
6996 | km :: [] -> addkm km
6997 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6999 loop l
7001 Buffer.add_string bb "<map in='";
7002 addkm i;
7003 match o with
7004 | KMinsrt km ->
7005 Buffer.add_string bb "' out='";
7006 addkm km;
7007 Buffer.add_string bb "'/>\n"
7009 | KMinsrl kms ->
7010 Buffer.add_string bb "' out='";
7011 addkms kms;
7012 Buffer.add_string bb "'/>\n"
7014 | KMmulti (ins, kms) ->
7015 Buffer.add_char bb ' ';
7016 addkms ins;
7017 Buffer.add_string bb "' out='";
7018 addkms kms;
7019 Buffer.add_string bb "'/>\n"
7020 ) h;
7021 Buffer.add_string bb "</keymap>";
7024 loop rest
7026 loop c.keyhashes;
7030 let save () =
7031 let uifontsize = fstate.fontsize in
7032 let bb = Buffer.create 32768 in
7033 let relx = float state.x /. float state.winw in
7034 let w, h, x =
7035 let cx w = truncate (relx *. float w) in
7036 List.fold_left
7037 (fun (w, h, x) ws ->
7038 match ws with
7039 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7040 | Wsi.MaxVert -> (w, conf.cwinh, x)
7041 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7043 (state.winw, state.winh, state.x) state.winstate
7045 conf.cwinw <- w;
7046 conf.cwinh <- h;
7047 let f (h, dc) =
7048 let dc = if conf.bedefault then conf else dc in
7049 Buffer.add_string bb "<llppconfig>\n";
7051 if String.length !fontpath > 0
7052 then
7053 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7054 uifontsize
7055 !fontpath
7056 else (
7057 if uifontsize <> 14
7058 then
7059 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7062 Buffer.add_string bb "<defaults ";
7063 add_attrs bb true dc dc;
7064 let kb = keymapsbuf true dc dc in
7065 if Buffer.length kb > 0
7066 then (
7067 Buffer.add_string bb ">\n";
7068 Buffer.add_buffer bb kb;
7069 Buffer.add_string bb "\n</defaults>\n";
7071 else Buffer.add_string bb "/>\n";
7073 let adddoc path pan anchor c bookmarks =
7074 if bookmarks == [] && c = dc && anchor = emptyanchor
7075 then ()
7076 else (
7077 Printf.bprintf bb "<doc path='%s'"
7078 (enent path 0 (String.length path));
7080 if anchor <> emptyanchor
7081 then (
7082 let n, rely, visy = anchor in
7083 Printf.bprintf bb " page='%d'" n;
7084 if rely > 1e-6
7085 then
7086 Printf.bprintf bb " rely='%f'" rely
7088 if abs_float visy > 1e-6
7089 then
7090 Printf.bprintf bb " visy='%f'" visy
7094 if pan != 0
7095 then Printf.bprintf bb " pan='%d'" pan;
7097 add_attrs bb false dc c;
7098 let kb = keymapsbuf false dc c in
7100 begin match bookmarks with
7101 | [] ->
7102 if Buffer.length kb > 0
7103 then (
7104 Buffer.add_string bb ">\n";
7105 Buffer.add_buffer bb kb;
7106 Buffer.add_string bb "\n</doc>\n";
7108 else Buffer.add_string bb "/>\n"
7109 | _ ->
7110 Buffer.add_string bb ">\n<bookmarks>\n";
7111 List.iter (fun (title, _level, (page, rely, visy)) ->
7112 Printf.bprintf bb
7113 "<item title='%s' page='%d'"
7114 (enent title 0 (String.length title))
7115 page
7117 if rely > 1e-6
7118 then
7119 Printf.bprintf bb " rely='%f'" rely
7121 if abs_float visy > 1e-6
7122 then
7123 Printf.bprintf bb " visy='%f'" visy
7125 Buffer.add_string bb "/>\n";
7126 ) bookmarks;
7127 Buffer.add_string bb "</bookmarks>";
7128 if Buffer.length kb > 0
7129 then (
7130 Buffer.add_string bb "\n";
7131 Buffer.add_buffer bb kb;
7133 Buffer.add_string bb "\n</doc>\n";
7134 end;
7138 let pan, conf =
7139 match state.mode with
7140 | Birdseye (c, pan, _, _, _) ->
7141 let beyecolumns =
7142 match conf.columns with
7143 | Cmulti ((c, _, _), _) -> Some c
7144 | Csingle _ -> None
7145 | Csplit _ -> None
7146 and columns =
7147 match c.columns with
7148 | Cmulti (c, _) -> Cmulti (c, [||])
7149 | Csingle _ -> Csingle [||]
7150 | Csplit _ -> failwith "quit from bird's eye while split"
7152 pan, { c with beyecolumns = beyecolumns; columns = columns }
7153 | _ -> x, conf
7155 let basename = Filename.basename
7156 (if String.length state.origin = 0 then state.path else state.origin)
7158 adddoc basename pan (getanchor ())
7159 (let conf =
7160 let autoscrollstep =
7161 match state.autoscroll with
7162 | Some step -> step
7163 | None -> conf.autoscrollstep
7165 match state.mode with
7166 | Birdseye (bc, _, _, _, _) ->
7167 { conf with
7168 zoom = bc.zoom;
7169 presentation = bc.presentation;
7170 interpagespace = bc.interpagespace;
7171 maxwait = bc.maxwait;
7172 autoscrollstep = autoscrollstep }
7173 | _ -> { conf with autoscrollstep = autoscrollstep }
7174 in conf)
7175 (if conf.savebmarks then state.bookmarks else []);
7177 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7178 if basename <> path
7179 then adddoc path x anchor c bookmarks
7180 ) h;
7181 Buffer.add_string bb "</llppconfig>\n";
7182 true;
7184 if load1 f && Buffer.length bb > 0
7185 then
7187 let tmp = !confpath ^ ".tmp" in
7188 let oc = open_out_bin tmp in
7189 Buffer.output_buffer oc bb;
7190 close_out oc;
7191 Unix.rename tmp !confpath;
7192 with exn ->
7193 prerr_endline
7194 ("error while saving configuration: " ^ exntos exn)
7196 end;;
7198 let adderrmsg src msg =
7199 Buffer.add_string state.errmsgs msg;
7200 state.newerrmsgs <- true;
7201 G.postRedisplay src
7204 let adderrfmt src fmt =
7205 Format.kprintf (fun s -> adderrmsg src s) fmt;
7208 let ract cmds =
7209 let cl = splitatspace cmds in
7210 let scan s fmt f =
7211 try Scanf.sscanf s fmt f
7212 with exn ->
7213 adderrfmt "remote exec"
7214 "error processing '%S': %s\n" cmds (exntos exn)
7216 match cl with
7217 | "reload" :: [] -> reload ()
7218 | "goto" :: args :: [] ->
7219 scan args "%u %f %f"
7220 (fun pageno x y ->
7221 let cmd, _ = state.geomcmds in
7222 if String.length cmd = 0
7223 then gotopagexy pageno x y
7224 else
7225 let f prevf () =
7226 gotopagexy pageno x y;
7227 prevf ()
7229 state.reprf <- f state.reprf
7231 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7232 | "rect" :: args :: [] ->
7233 scan args "%u %u %f %f %f %f"
7234 (fun pageno color x0 y0 x1 y1 ->
7235 onpagerect pageno (fun w h ->
7236 let _,w1,h1,_ = getpagedim pageno in
7237 let sw = float w1 /. w
7238 and sh = float h1 /. h in
7239 let x0s = x0 *. sw
7240 and x1s = x1 *. sw
7241 and y0s = y0 *. sh
7242 and y1s = y1 *. sh in
7243 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7244 debugrect rect;
7245 state.rects <- (pageno, color, rect) :: state.rects;
7246 G.postRedisplay "rect";
7249 | "activatewin" :: [] -> Wsi.activatewin ()
7250 | "quit" :: [] -> raise Quit
7251 | _ ->
7252 adderrfmt "remote command"
7253 "error processing remote command: %S\n" cmds;
7256 let remote =
7257 let scratch = String.create 80 in
7258 let buf = Buffer.create 80 in
7259 fun fd ->
7260 let rec tempfr () =
7261 try Some (Unix.read fd scratch 0 80)
7262 with
7263 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7264 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7265 | exn -> raise exn
7267 match tempfr () with
7268 | None -> Some fd
7269 | Some n ->
7270 if n = 0
7271 then (
7272 Unix.close fd;
7273 if Buffer.length buf > 0
7274 then (
7275 let s = Buffer.contents buf in
7276 Buffer.clear buf;
7277 ract s;
7279 None
7281 else
7282 let rec eat ppos =
7283 let nlpos =
7285 let pos = String.index_from scratch ppos '\n' in
7286 if pos >= n then -1 else pos
7287 with Not_found -> -1
7289 if nlpos >= 0
7290 then (
7291 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7292 let s = Buffer.contents buf in
7293 Buffer.clear buf;
7294 ract s;
7295 eat (nlpos+1);
7297 else (
7298 Buffer.add_substring buf scratch ppos (n-ppos);
7299 Some fd
7301 in eat 0
7304 let remoteopen path =
7305 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7306 with exn ->
7307 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7308 None
7311 let () =
7312 let trimcachepath = ref "" in
7313 let rcmdpath = ref "" in
7314 Arg.parse
7315 (Arg.align
7316 [("-p", Arg.String (fun s -> state.password <- s),
7317 "<password> Set password");
7319 ("-f", Arg.String (fun s -> Config.fontpath := s),
7320 "<path> Set path to the user interface font");
7322 ("-c", Arg.String (fun s -> Config.confpath := s),
7323 "<path> Set path to the configuration file");
7325 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7326 "<path> Set path to the trim cache file");
7328 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7329 "<named-destination> Set named destination");
7331 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7333 ("-remote", Arg.String (fun s -> rcmdpath := s),
7334 "<path> Set path to the remote commands source");
7336 ("-origin", Arg.String (fun s -> state.origin <- s),
7337 "<original-path> Set original path");
7339 ("-v", Arg.Unit (fun () ->
7340 Printf.printf
7341 "%s\nconfiguration path: %s\n"
7342 (version ())
7343 Config.defconfpath
7345 exit 0), " Print version and exit");
7348 (fun s -> state.path <- s)
7349 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7351 if String.length state.path = 0
7352 then (prerr_endline "file name missing"; exit 1);
7354 if not (Config.load ())
7355 then prerr_endline "failed to load configuration";
7357 let globalkeyhash = findkeyhash conf "global" in
7358 let wsfd, winw, winh = Wsi.init (object
7359 method expose =
7360 if nogeomcmds state.geomcmds || platform == Posx
7361 then display ()
7362 else (
7363 GlClear.color (scalecolor2 conf.bgcolor);
7364 GlClear.clear [`color];
7366 method display = display ()
7367 method reshape w h = reshape w h
7368 method mouse b d x y m = mouse b d x y m
7369 method motion x y = state.mpos <- (x, y); motion x y
7370 method pmotion x y = state.mpos <- (x, y); pmotion x y
7371 method key k m =
7372 let mascm = m land (
7373 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7374 ) in
7375 match state.keystate with
7376 | KSnone ->
7377 let km = k, mascm in
7378 begin
7379 match
7380 let modehash = state.uioh#modehash in
7381 try Hashtbl.find modehash km
7382 with Not_found ->
7383 try Hashtbl.find globalkeyhash km
7384 with Not_found -> KMinsrt (k, m)
7385 with
7386 | KMinsrt (k, m) -> keyboard k m
7387 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7388 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7390 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7391 List.iter (fun (k, m) -> keyboard k m) insrt;
7392 state.keystate <- KSnone
7393 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7394 state.keystate <- KSinto (keys, insrt)
7395 | _ ->
7396 state.keystate <- KSnone
7398 method enter x y = state.mpos <- (x, y); pmotion x y
7399 method leave = state.mpos <- (-1, -1)
7400 method winstate wsl = state.winstate <- wsl
7401 method quit = raise Quit
7402 end) conf.cwinw conf.cwinh (platform = Posx) in
7404 state.wsfd <- wsfd;
7406 if not (
7407 List.exists GlMisc.check_extension
7408 [ "GL_ARB_texture_rectangle"
7409 ; "GL_EXT_texture_recangle"
7410 ; "GL_NV_texture_rectangle" ]
7412 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7414 let cr, sw =
7415 match Ne.pipe () with
7416 | Ne.Exn exn ->
7417 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7418 exit 1
7419 | Ne.Res rw -> rw
7420 and sr, cw =
7421 match Ne.pipe () with
7422 | Ne.Exn exn ->
7423 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7424 exit 1
7425 | Ne.Res rw -> rw
7428 cloexec cr;
7429 cloexec sw;
7430 cloexec sr;
7431 cloexec cw;
7433 setcheckers conf.checkers;
7434 redirectstderr ();
7436 init (cr, cw) (
7437 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7438 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7439 !Config.fontpath, !trimcachepath,
7440 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7442 state.sr <- sr;
7443 state.sw <- sw;
7444 state.text <- "Opening " ^ (mbtoutf8 state.path);
7445 reshape winw winh;
7446 opendoc state.path state.password;
7447 state.uioh <- uioh;
7449 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7450 let optrfd =
7451 ref (
7452 if String.length !rcmdpath > 0
7453 then remoteopen !rcmdpath
7454 else None
7458 let rec loop deadline =
7459 let r =
7460 match state.errfd with
7461 | None -> [state.sr; state.wsfd]
7462 | Some fd -> [state.sr; state.wsfd; fd]
7464 let r =
7465 match !optrfd with
7466 | None -> r
7467 | Some fd -> fd :: r
7469 if state.redisplay
7470 then (
7471 state.redisplay <- false;
7472 display ();
7474 let timeout =
7475 let now = now () in
7476 if deadline > now
7477 then (
7478 if deadline = infinity
7479 then ~-.1.0
7480 else max 0.0 (deadline -. now)
7482 else 0.0
7484 let r, _, _ =
7485 try Unix.select r [] [] timeout
7486 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7488 begin match r with
7489 | [] ->
7490 state.ghyll None;
7491 let newdeadline =
7492 if state.ghyll == noghyll
7493 then
7494 match state.autoscroll with
7495 | Some step when step != 0 ->
7496 let y = state.y + step in
7497 let y =
7498 if y < 0
7499 then state.maxy
7500 else if y >= state.maxy then 0 else y
7502 gotoy y;
7503 if state.mode = View
7504 then state.text <- "";
7505 deadline +. 0.01
7506 | _ -> infinity
7507 else deadline +. 0.01
7509 loop newdeadline
7511 | l ->
7512 let rec checkfds = function
7513 | [] -> ()
7514 | fd :: rest when fd = state.sr ->
7515 let cmd = readcmd state.sr in
7516 act cmd;
7517 checkfds rest
7519 | fd :: rest when fd = state.wsfd ->
7520 Wsi.readresp fd;
7521 checkfds rest
7523 | fd :: rest when Some fd = !optrfd ->
7524 begin match remote fd with
7525 | None -> optrfd := remoteopen !rcmdpath;
7526 | opt -> optrfd := opt
7527 end;
7528 checkfds rest
7530 | fd :: rest ->
7531 let s = String.create 80 in
7532 let n = tempfailureretry (Unix.read fd s 0) 80 in
7533 if conf.redirectstderr
7534 then (
7535 Buffer.add_substring state.errmsgs s 0 n;
7536 state.newerrmsgs <- true;
7537 state.redisplay <- true;
7539 else (
7540 prerr_string (String.sub s 0 n);
7541 flush stderr;
7543 checkfds rest
7545 checkfds l;
7546 let newdeadline =
7547 let deadline1 =
7548 if deadline = infinity
7549 then now () +. 0.01
7550 else deadline
7552 match state.autoscroll with
7553 | Some step when step != 0 -> deadline1
7554 | _ -> if state.ghyll == noghyll then infinity else deadline1
7556 loop newdeadline
7557 end;
7560 loop infinity;
7561 with Quit ->
7562 Config.save ();