Cosmetics (C-x h C-M-\)
[llpp.git] / main.ml
blobda26386b6ff133a2c91806f7e3fa9bdb5ca01d27
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
1829 (l.pageno, top, dtop)
1832 let getanchor () =
1833 match state.layout with
1834 | l :: _ -> getanchor1 l
1835 | [] ->
1836 let n = page_of_y state.y in
1837 if n = -1
1838 then state.anchor
1839 else
1840 let y, h = getpageyh n in
1841 let dy = y - state.y in
1842 let dtop =
1843 if conf.presentation
1844 then
1845 let ips = calcips h in
1846 float (dy + ips) /. float ips
1847 else
1848 float dy /. float conf.interpagespace
1850 (n, 0.0, dtop)
1853 let getanchory (n, top, dtop) =
1854 let y, h = getpageyh n in
1855 if conf.presentation
1856 then
1857 let ips = calcips h in
1858 y + truncate (top*.float h -. dtop*.float ips) + ips;
1859 else
1860 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1863 let gotoanchor anchor =
1864 gotoy (getanchory anchor);
1867 let addnav () =
1868 cbput state.hists.nav (getanchor ());
1871 let getnav dir =
1872 let anchor = cbgetc state.hists.nav dir in
1873 getanchory anchor;
1876 let gotoghyll y =
1877 let scroll f n a b =
1878 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1879 let snake f a b =
1880 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1881 if f < a
1882 then s (float f /. float a)
1883 else (
1884 if f > b
1885 then 1.0 -. s ((float (f-b) /. float (n-b)))
1886 else 1.0
1889 snake f a b
1890 and summa f n a b =
1891 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1892 let iv x = x**3.-.0.5*.x**4. in
1893 let iv1 = iv f in
1894 let ins = float a *. iv1
1895 and outs = float (n-b) *. iv1 in
1896 let ones = b - a in
1897 ins +. outs +. float ones
1899 let rec set (_N, _A, _B) y sy =
1900 let sum = summa 1.0 _N _A _B in
1901 let dy = float (y - sy) in
1902 state.ghyll <- (
1903 let rec gf n y1 o =
1904 if n >= _N
1905 then state.ghyll <- noghyll
1906 else
1907 let go n =
1908 let s = scroll n _N _A _B in
1909 let y1 = y1 +. ((s *. dy) /. sum) in
1910 gotoy_and_clear_text (truncate y1);
1911 state.ghyll <- gf (n+1) y1;
1913 match o with
1914 | None -> go n
1915 | Some y' -> set (_N/2, 1, 1) y' state.y
1917 gf 0 (float state.y)
1920 match conf.ghyllscroll with
1921 | None ->
1922 gotoy_and_clear_text y
1923 | Some nab ->
1924 if state.ghyll == noghyll
1925 then set nab y state.y
1926 else state.ghyll (Some y)
1929 let gotopage n top =
1930 let y, h = getpageyh n in
1931 let y = y + (truncate (top *. float h)) in
1932 gotoghyll y
1935 let gotopage1 n top =
1936 let y = getpagey n in
1937 let y = y + top in
1938 gotoghyll y
1941 let invalidate s f =
1942 state.layout <- [];
1943 state.pdims <- [];
1944 state.rects <- [];
1945 state.rects1 <- [];
1946 match state.geomcmds with
1947 | ps, [] when String.length ps = 0 ->
1948 f ();
1949 state.geomcmds <- s, [];
1951 | ps, [] ->
1952 state.geomcmds <- ps, [s, f];
1954 | ps, (s', _) :: rest when s' = s ->
1955 state.geomcmds <- ps, ((s, f) :: rest);
1957 | ps, cmds ->
1958 state.geomcmds <- ps, ((s, f) :: cmds);
1961 let flushpages () =
1962 Hashtbl.iter (fun _ opaque ->
1963 wcmd "freepage %s" opaque;
1964 ) state.pagemap;
1965 Hashtbl.clear state.pagemap;
1968 let flushtiles () =
1969 if not (Queue.is_empty state.tilelru)
1970 then (
1971 Queue.iter (fun (k, p, s) ->
1972 wcmd "freetile %s" p;
1973 state.memused <- state.memused - s;
1974 Hashtbl.remove state.tilemap k;
1975 ) state.tilelru;
1976 state.uioh#infochanged Memused;
1977 Queue.clear state.tilelru;
1979 load state.layout;
1982 let stateh h =
1983 let h = truncate (float h*.conf.zoom) in
1984 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1985 h - d
1988 let opendoc path password =
1989 state.path <- path;
1990 state.password <- password;
1991 state.gen <- state.gen + 1;
1992 state.docinfo <- [];
1994 flushpages ();
1995 setaalevel conf.aalevel;
1996 let titlepath =
1997 if String.length state.origin = 0
1998 then path
1999 else state.origin
2001 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2002 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2003 invalidate "reqlayout"
2004 (fun () ->
2005 wcmd "reqlayout %d %d %d %s\000"
2006 conf.angle (int_of_fitmodel conf.fitmodel)
2007 (stateh state.winh) state.nameddest
2011 let reload () =
2012 state.anchor <- getanchor ();
2013 opendoc state.path state.password;
2016 let scalecolor c =
2017 let c = c *. conf.colorscale in
2018 (c, c, c);
2021 let scalecolor2 (r, g, b) =
2022 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2025 let docolumns = function
2026 | Csingle _ ->
2027 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2028 let rec loop pageno pdimno pdim y ph pdims =
2029 if pageno = state.pagecount
2030 then ()
2031 else
2032 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2033 match pdims with
2034 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2035 pdimno+1, pdim, rest
2036 | _ ->
2037 pdimno, pdim, pdims
2039 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2040 let y = y +
2041 (if conf.presentation
2042 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2043 else (if pageno = 0 then 0 else conf.interpagespace)
2046 a.(pageno) <- (pdimno, x, y, pdim);
2047 loop (pageno+1) pdimno pdim (y + h) h pdims
2049 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2050 conf.columns <- Csingle a;
2052 | Cmulti ((columns, coverA, coverB), _) ->
2053 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2054 let rec loop pageno pdimno pdim x y rowh pdims =
2055 let rec fixrow m = if m = pageno then () else
2056 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2057 if h < rowh
2058 then (
2059 let y = y + (rowh - h) / 2 in
2060 a.(m) <- (pdimno, x, y, pdim);
2062 fixrow (m+1)
2064 if pageno = state.pagecount
2065 then fixrow (((pageno - 1) / columns) * columns)
2066 else
2067 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2068 match pdims with
2069 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2070 pdimno+1, pdim, rest
2071 | _ ->
2072 pdimno, pdim, pdims
2074 let x, y, rowh' =
2075 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2076 then (
2077 let x = (state.winw - state.scrollw - w) / 2 in
2078 let ips =
2079 if conf.presentation then calcips h else conf.interpagespace in
2080 x, y + ips + rowh, h
2082 else (
2083 if (pageno - coverA) mod columns = 0
2084 then (
2085 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2086 let y =
2087 if conf.presentation
2088 then
2089 let ips = calcips h in
2090 y + (if pageno = 0 then 0 else calcips rowh + ips)
2091 else
2092 y + (if pageno = 0 then 0 else conf.interpagespace)
2094 x, y + rowh, h
2096 else x, y, max rowh h
2099 let y =
2100 if pageno > 1 && (pageno - coverA) mod columns = 0
2101 then (
2102 let y =
2103 if pageno = columns && conf.presentation
2104 then (
2105 let ips = calcips rowh in
2106 for i = 0 to pred columns
2108 let (pdimno, x, y, pdim) = a.(i) in
2109 a.(i) <- (pdimno, x, y+ips, pdim)
2110 done;
2111 y+ips;
2113 else y
2115 fixrow (pageno - columns);
2118 else y
2120 a.(pageno) <- (pdimno, x, y, pdim);
2121 let x = x + w + xoff*2 + conf.interpagespace in
2122 loop (pageno+1) pdimno pdim x y rowh' pdims
2124 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2125 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2127 | Csplit (c, _) ->
2128 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2129 let rec loop pageno pdimno pdim y pdims =
2130 if pageno = state.pagecount
2131 then ()
2132 else
2133 let pdimno, ((_, w, h, _) as pdim), pdims =
2134 match pdims with
2135 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2136 pdimno+1, pdim, rest
2137 | _ ->
2138 pdimno, pdim, pdims
2140 let cw = w / c in
2141 let rec loop1 n x y =
2142 if n = c then y else (
2143 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2144 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2147 let y = loop1 0 0 y in
2148 loop (pageno+1) pdimno pdim y pdims
2150 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2151 conf.columns <- Csplit (c, a);
2154 let represent () =
2155 docolumns conf.columns;
2156 state.maxy <- calcheight ();
2157 state.hscrollh <-
2158 if state.x = 0 && state.w <= state.winw - state.scrollw
2159 then 0
2160 else state.scrollw
2162 if state.reprf == noreprf
2163 then (
2164 match state.mode with
2165 | Birdseye (_, _, pageno, _, _) ->
2166 let y, h = getpageyh pageno in
2167 let top = (state.winh - h) / 2 in
2168 gotoy (max 0 (y - top))
2169 | _ -> gotoanchor state.anchor
2171 else (
2172 state.reprf ();
2173 state.reprf <- noreprf;
2177 let reshape w h =
2178 GlDraw.viewport 0 0 w h;
2179 let firsttime = state.geomcmds == firstgeomcmds in
2180 if not firsttime && nogeomcmds state.geomcmds
2181 then state.anchor <- getanchor ();
2183 state.winw <- w;
2184 let w = truncate (float w *. conf.zoom) - state.scrollw in
2185 let w = max w 2 in
2186 state.winh <- h;
2187 setfontsize fstate.fontsize;
2188 GlMat.mode `modelview;
2189 GlMat.load_identity ();
2191 GlMat.mode `projection;
2192 GlMat.load_identity ();
2193 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2194 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2195 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2197 let relx =
2198 if conf.zoom <= 1.0
2199 then 0.0
2200 else float state.x /. float state.w
2202 invalidate "geometry"
2203 (fun () ->
2204 state.w <- w;
2205 if not firsttime
2206 then state.x <- truncate (relx *. float w);
2207 let w =
2208 match conf.columns with
2209 | Csingle _ -> w
2210 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2211 | Csplit (c, _) -> w * c
2213 wcmd "geometry %d %d %d"
2214 w (stateh h) (int_of_fitmodel conf.fitmodel)
2218 let enttext () =
2219 let len = String.length state.text in
2220 let drawstring s =
2221 let hscrollh =
2222 match state.mode with
2223 | Textentry _
2224 | View ->
2225 let h, _, _ = state.uioh#scrollpw in
2227 | _ -> 0
2229 let rect x w =
2230 GlDraw.rect
2231 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2232 (x+.w, float (state.winh - hscrollh))
2235 let w = float (state.winw - state.scrollw - 1) in
2236 if state.progress >= 0.0 && state.progress < 1.0
2237 then (
2238 GlDraw.color (0.3, 0.3, 0.3);
2239 let w1 = w *. state.progress in
2240 rect 0.0 w1;
2241 GlDraw.color (0.0, 0.0, 0.0);
2242 rect w1 (w-.w1)
2244 else (
2245 GlDraw.color (0.0, 0.0, 0.0);
2246 rect 0.0 w;
2249 GlDraw.color (1.0, 1.0, 1.0);
2250 drawstring fstate.fontsize
2251 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2253 let s =
2254 match state.mode with
2255 | Textentry ((prefix, text, _, _, _, _), _) ->
2256 let s =
2257 if len > 0
2258 then
2259 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2260 else
2261 Printf.sprintf "%s%s_" prefix text
2265 | _ -> state.text
2267 let s =
2268 if state.newerrmsgs
2269 then (
2270 if not (istextentry state.mode) && state.uioh#eformsgs
2271 then
2272 let s1 = "(press 'e' to review error messasges)" in
2273 if String.length s > 0 then s ^ " " ^ s1 else s1
2274 else s
2276 else s
2278 if String.length s > 0
2279 then drawstring s
2282 let gctiles () =
2283 let len = Queue.length state.tilelru in
2284 let layout = lazy (
2285 match state.throttle with
2286 | None ->
2287 if conf.preload
2288 then preloadlayout state.y
2289 else state.layout
2290 | Some (layout, _, _) ->
2291 layout
2292 ) in
2293 let rec loop qpos =
2294 if state.memused <= conf.memlimit
2295 then ()
2296 else (
2297 if qpos < len
2298 then
2299 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2300 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2301 let (_, pw, ph, _) = getpagedim n in
2303 gen = state.gen
2304 && colorspace = conf.colorspace
2305 && angle = conf.angle
2306 && pagew = pw
2307 && pageh = ph
2308 && (
2309 let x = col*conf.tilew
2310 and y = row*conf.tileh in
2311 tilevisible (Lazy.force_val layout) n x y
2313 then Queue.push lruitem state.tilelru
2314 else (
2315 freepbo p;
2316 wcmd "freetile %s" p;
2317 state.memused <- state.memused - s;
2318 state.uioh#infochanged Memused;
2319 Hashtbl.remove state.tilemap k;
2321 loop (qpos+1)
2324 loop 0
2327 let logcurrently = function
2328 | Idle -> dolog "Idle"
2329 | Loading (l, gen) ->
2330 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2331 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2332 dolog
2333 "Tiling %d[%d,%d] page=%s cs=%s angle"
2334 l.pageno col row pageopaque
2335 (colorspace_to_string colorspace)
2337 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2338 angle gen conf.angle state.gen
2339 tilew tileh
2340 conf.tilew conf.tileh
2342 | Outlining _ ->
2343 dolog "outlining"
2346 let splitatspace =
2347 let r = Str.regexp " " in
2348 fun s -> Str.bounded_split r s 2;
2351 let onpagerect pageno f =
2352 let b =
2353 match conf.columns with
2354 | Cmulti (_, b) -> b
2355 | Csingle b -> b
2356 | Csplit (_, b) -> b
2358 if pageno >= 0 && pageno < Array.length b
2359 then
2360 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2361 let r = getpdimrect pdimno in
2362 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2365 let gotopagexy1 pageno x y =
2366 onpagerect pageno (fun w h ->
2367 let top = y /. h in
2368 let _,w1,_,leftx = getpagedim pageno in
2369 let wh = state.winh - state.hscrollh in
2370 let sw = float w1 /. w in
2371 let x = sw *. x in
2372 let x = leftx + state.x + truncate x in
2373 let sx =
2374 if x < 0 || x >= state.winw - state.scrollw
2375 then state.x - x
2376 else state.x
2378 let py, h = getpageyh pageno in
2379 let pdy = truncate (top *. float h) in
2380 let y' = py + pdy in
2381 let dy = y' - state.y in
2382 let sy =
2383 if x != state.x || not (dy > 0 && dy < wh)
2384 then (
2385 if conf.presentation
2386 then
2387 if abs (py - y') > wh
2388 then y'
2389 else py
2390 else y';
2392 else state.y
2394 if state.x != sx || state.y != sy
2395 then (
2396 let x, y =
2397 if !wtmode
2398 then (
2399 let ww = state.winw - state.scrollw in
2400 let qx = sx / ww
2401 and qy = pdy / wh in
2402 let x = qx * ww
2403 and y = py + qy * wh in
2404 let x = if -x + ww > w1 then -(w1-ww) else x
2405 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2406 let y =
2407 if conf.presentation
2408 then
2409 if abs (py - y') > wh
2410 then y'
2411 else py
2412 else y';
2414 (x, y)
2416 else (sx, sy)
2418 state.x <- x;
2419 state.hscrollh <-
2420 if x = 0 && state.w <= state.winw - state.scrollw
2421 then 0
2422 else state.scrollw
2424 gotoy_and_clear_text y;
2426 else gotoy_and_clear_text state.y;
2430 let gotopagexy pageno x y =
2431 match state.mode with
2432 | Birdseye _ -> gotopage pageno 0.0
2433 | _ -> gotopagexy1 pageno x y
2436 let act cmds =
2437 (* dolog "%S" cmds; *)
2438 let cl = splitatspace cmds in
2439 let scan s fmt f =
2440 try Scanf.sscanf s fmt f
2441 with exn ->
2442 dolog "error processing '%S': %s" cmds (exntos exn);
2443 exit 1
2445 match cl with
2446 | "clear" :: [] ->
2447 state.uioh#infochanged Pdim;
2448 state.pdims <- [];
2450 | "clearrects" :: [] ->
2451 state.rects <- state.rects1;
2452 G.postRedisplay "clearrects";
2454 | "continue" :: args :: [] ->
2455 let n = scan args "%u" (fun n -> n) in
2456 state.pagecount <- n;
2457 begin match state.currently with
2458 | Outlining l ->
2459 state.currently <- Idle;
2460 state.outlines <- Array.of_list (List.rev l)
2461 | _ -> ()
2462 end;
2464 let cur, cmds = state.geomcmds in
2465 if String.length cur = 0
2466 then failwith "umpossible";
2468 begin match List.rev cmds with
2469 | [] ->
2470 state.geomcmds <- "", [];
2471 represent ();
2472 | (s, f) :: rest ->
2473 f ();
2474 state.geomcmds <- s, List.rev rest;
2475 end;
2476 if conf.maxwait = None && not !wtmode
2477 then G.postRedisplay "continue";
2479 | "title" :: args :: [] ->
2480 Wsi.settitle args
2482 | "msg" :: args :: [] ->
2483 showtext ' ' args
2485 | "vmsg" :: args :: [] ->
2486 if conf.verbose
2487 then showtext ' ' args
2489 | "emsg" :: args :: [] ->
2490 Buffer.add_string state.errmsgs args;
2491 state.newerrmsgs <- true;
2492 G.postRedisplay "error message"
2494 | "progress" :: args :: [] ->
2495 let progress, text =
2496 scan args "%f %n"
2497 (fun f pos ->
2498 f, String.sub args pos (String.length args - pos))
2500 state.text <- text;
2501 state.progress <- progress;
2502 G.postRedisplay "progress"
2504 | "firstmatch" :: args :: [] ->
2505 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2506 scan args "%u %d %f %f %f %f %f %f %f %f"
2507 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2508 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2510 let y = (getpagey pageno) + truncate y0 in
2511 addnav ();
2512 gotoy y;
2513 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2515 | "match" :: args :: [] ->
2516 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2517 scan args "%u %d %f %f %f %f %f %f %f %f"
2518 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2519 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2521 state.rects1 <-
2522 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2524 | "page" :: args :: [] ->
2525 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2526 begin match state.currently with
2527 | Loading (l, gen) ->
2528 vlog "page %d took %f sec" l.pageno t;
2529 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2530 begin match state.throttle with
2531 | None ->
2532 let preloadedpages =
2533 if conf.preload
2534 then preloadlayout state.y
2535 else state.layout
2537 let evict () =
2538 let set =
2539 List.fold_left (fun s l -> IntSet.add l.pageno s)
2540 IntSet.empty preloadedpages
2542 let evictedpages =
2543 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2544 if not (IntSet.mem pageno set)
2545 then (
2546 wcmd "freepage %s" opaque;
2547 key :: accu
2549 else accu
2550 ) state.pagemap []
2552 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2554 evict ();
2555 state.currently <- Idle;
2556 if gen = state.gen
2557 then (
2558 tilepage l.pageno pageopaque state.layout;
2559 load state.layout;
2560 load preloadedpages;
2561 if pagevisible state.layout l.pageno
2562 && layoutready state.layout
2563 then G.postRedisplay "page";
2566 | Some (layout, _, _) ->
2567 state.currently <- Idle;
2568 tilepage l.pageno pageopaque layout;
2569 load state.layout
2570 end;
2572 | _ ->
2573 dolog "Inconsistent loading state";
2574 logcurrently state.currently;
2575 exit 1
2578 | "tile" :: args :: [] ->
2579 let (x, y, opaque, size, t) =
2580 scan args "%u %u %s %u %f"
2581 (fun x y p size t -> (x, y, p, size, t))
2583 begin match state.currently with
2584 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2585 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2587 unmappbo opaque;
2588 if tilew != conf.tilew || tileh != conf.tileh
2589 then (
2590 wcmd "freetile %s" opaque;
2591 state.currently <- Idle;
2592 load state.layout;
2594 else (
2595 puttileopaque l col row gen cs angle opaque size t;
2596 state.memused <- state.memused + size;
2597 state.uioh#infochanged Memused;
2598 gctiles ();
2599 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2600 opaque, size) state.tilelru;
2602 let layout =
2603 match state.throttle with
2604 | None -> state.layout
2605 | Some (layout, _, _) -> layout
2608 state.currently <- Idle;
2609 if gen = state.gen
2610 && conf.colorspace = cs
2611 && conf.angle = angle
2612 && tilevisible layout l.pageno x y
2613 then conttiling l.pageno pageopaque;
2615 begin match state.throttle with
2616 | None ->
2617 preload state.layout;
2618 if gen = state.gen
2619 && conf.colorspace = cs
2620 && conf.angle = angle
2621 && tilevisible state.layout l.pageno x y
2622 && (not !wtmode || layoutready state.layout)
2623 then G.postRedisplay "tile nothrottle";
2625 | Some (layout, y, _) ->
2626 let ready = layoutready layout in
2627 if ready
2628 then (
2629 state.y <- y;
2630 state.layout <- layout;
2631 state.throttle <- None;
2632 G.postRedisplay "throttle";
2634 else load layout;
2635 end;
2638 | _ ->
2639 dolog "Inconsistent tiling state";
2640 logcurrently state.currently;
2641 exit 1
2644 | "pdim" :: args :: [] ->
2645 let (n, w, h, _) as pdim =
2646 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2648 let pdim =
2649 match conf.fitmodel, conf.columns with
2650 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2651 | _ -> pdim
2653 state.uioh#infochanged Pdim;
2654 state.pdims <- pdim :: state.pdims
2656 | "o" :: args :: [] ->
2657 let (l, n, t, h, pos) =
2658 scan args "%u %u %d %u %n"
2659 (fun l n t h pos -> l, n, t, h, pos)
2661 let s = String.sub args pos (String.length args - pos) in
2662 let outline = (s, l, (n, float t /. float h, 0.0)) in
2663 begin match state.currently with
2664 | Outlining outlines ->
2665 state.currently <- Outlining (outline :: outlines)
2666 | Idle ->
2667 state.currently <- Outlining [outline]
2668 | currently ->
2669 dolog "invalid outlining state";
2670 logcurrently currently
2673 | "a" :: args :: [] ->
2674 let (n, l, t) =
2675 scan args "%u %d %d" (fun n l t -> n, l, t)
2677 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2679 | "info" :: args :: [] ->
2680 state.docinfo <- (1, args) :: state.docinfo
2682 | "infoend" :: [] ->
2683 state.uioh#infochanged Docinfo;
2684 state.docinfo <- List.rev state.docinfo
2686 | _ ->
2687 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2690 let onhist cb =
2691 let rc = cb.rc in
2692 let action = function
2693 | HCprev -> cbget cb ~-1
2694 | HCnext -> cbget cb 1
2695 | HCfirst -> cbget cb ~-(cb.rc)
2696 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2697 and cancel () = cb.rc <- rc
2698 in (action, cancel)
2701 let search pattern forward =
2702 match conf.columns with
2703 | Csplit _ ->
2704 showtext '!' "searching does not work properly in split columns mode"
2705 | _ ->
2706 if String.length pattern > 0
2707 then
2708 let pn, py =
2709 match state.layout with
2710 | [] -> 0, 0
2711 | l :: _ ->
2712 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2714 wcmd "search %d %d %d %d,%s\000"
2715 (btod conf.icase) pn py (btod forward) pattern;
2718 let intentry text key =
2719 let c =
2720 if key >= 32 && key < 127
2721 then Char.chr key
2722 else '\000'
2724 match c with
2725 | '0' .. '9' ->
2726 let text = addchar text c in
2727 TEcont text
2729 | _ ->
2730 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2731 TEcont text
2734 let linknentry text key =
2735 let c =
2736 if key >= 32 && key < 127
2737 then Char.chr key
2738 else '\000'
2740 match c with
2741 | 'a' .. 'z' ->
2742 let text = addchar text c in
2743 TEcont text
2745 | _ ->
2746 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2747 TEcont text
2750 let linkndone f s =
2751 if String.length s > 0
2752 then (
2753 let n =
2754 let l = String.length s in
2755 let rec loop pos n = if pos = l then n else
2756 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2757 loop (pos+1) (n*26 + m)
2758 in loop 0 0
2760 let rec loop n = function
2761 | [] -> ()
2762 | l :: rest ->
2763 match getopaque l.pageno with
2764 | None -> loop n rest
2765 | Some opaque ->
2766 let m = getlinkcount opaque in
2767 if n < m
2768 then (
2769 let under = getlink opaque n in
2770 f under
2772 else loop (n-m) rest
2774 loop n state.layout;
2778 let textentry text key =
2779 if key land 0xff00 = 0xff00
2780 then TEcont text
2781 else TEcont (text ^ toutf8 key)
2784 let reqlayout angle fitmodel =
2785 match state.throttle with
2786 | None ->
2787 if nogeomcmds state.geomcmds
2788 then state.anchor <- getanchor ();
2789 conf.angle <- angle mod 360;
2790 if conf.angle != 0
2791 then (
2792 match state.mode with
2793 | LinkNav _ -> state.mode <- View
2794 | _ -> ()
2796 conf.fitmodel <- fitmodel;
2797 invalidate "reqlayout"
2798 (fun () ->
2799 wcmd "reqlayout %d %d %d"
2800 conf.angle (int_of_fitmodel conf.fitmodel) (stateh state.winh)
2802 | _ -> ()
2805 let settrim trimmargins trimfuzz =
2806 if nogeomcmds state.geomcmds
2807 then state.anchor <- getanchor ();
2808 conf.trimmargins <- trimmargins;
2809 conf.trimfuzz <- trimfuzz;
2810 let x0, y0, x1, y1 = trimfuzz in
2811 invalidate "settrim"
2812 (fun () ->
2813 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2814 flushpages ();
2817 let setzoom zoom =
2818 match state.throttle with
2819 | None ->
2820 let zoom = max 0.0001 zoom in
2821 if zoom <> conf.zoom
2822 then (
2823 state.prevzoom <- conf.zoom;
2824 conf.zoom <- zoom;
2825 reshape state.winw state.winh;
2826 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2829 | Some (layout, y, started) ->
2830 let time =
2831 match conf.maxwait with
2832 | None -> 0.0
2833 | Some t -> t
2835 let dt = now () -. started in
2836 if dt > time
2837 then (
2838 state.y <- y;
2839 load layout;
2843 let setcolumns mode columns coverA coverB =
2844 state.prevcolumns <- Some (conf.columns, conf.zoom);
2845 if columns < 0
2846 then (
2847 if isbirdseye mode
2848 then showtext '!' "split mode doesn't work in bird's eye"
2849 else (
2850 conf.columns <- Csplit (-columns, [||]);
2851 state.x <- 0;
2852 conf.zoom <- 1.0;
2855 else (
2856 if columns < 2
2857 then (
2858 conf.columns <- Csingle [||];
2859 state.x <- 0;
2860 setzoom 1.0;
2862 else (
2863 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2864 conf.zoom <- 1.0;
2867 reshape state.winw state.winh;
2870 let enterbirdseye () =
2871 let zoom = float conf.thumbw /. float state.winw in
2872 let birdseyepageno =
2873 let cy = state.winh / 2 in
2874 let fold = function
2875 | [] -> 0
2876 | l :: rest ->
2877 let rec fold best = function
2878 | [] -> best.pageno
2879 | l :: rest ->
2880 let d = cy - (l.pagedispy + l.pagevh/2)
2881 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2882 if abs d < abs dbest
2883 then fold l rest
2884 else best.pageno
2885 in fold l rest
2887 fold state.layout
2889 state.mode <- Birdseye (
2890 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2892 conf.zoom <- zoom;
2893 conf.presentation <- false;
2894 conf.interpagespace <- 10;
2895 conf.hlinks <- false;
2896 conf.fitmodel <- FitProportional;
2897 state.x <- 0;
2898 state.mstate <- Mnone;
2899 conf.maxwait <- None;
2900 conf.columns <- (
2901 match conf.beyecolumns with
2902 | Some c ->
2903 conf.zoom <- 1.0;
2904 Cmulti ((c, 0, 0), [||])
2905 | None -> Csingle [||]
2907 Wsi.setcursor Wsi.CURSOR_INHERIT;
2908 if conf.verbose
2909 then
2910 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2911 (100.0*.zoom)
2912 else
2913 state.text <- ""
2915 reshape state.winw state.winh;
2918 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2919 state.mode <- View;
2920 conf.zoom <- c.zoom;
2921 conf.presentation <- c.presentation;
2922 conf.interpagespace <- c.interpagespace;
2923 conf.maxwait <- c.maxwait;
2924 conf.hlinks <- c.hlinks;
2925 conf.fitmodel <- c.fitmodel;
2926 conf.beyecolumns <- (
2927 match conf.columns with
2928 | Cmulti ((c, _, _), _) -> Some c
2929 | Csingle _ -> None
2930 | Csplit _ -> failwith "leaving bird's eye split mode"
2932 conf.columns <- (
2933 match c.columns with
2934 | Cmulti (c, _) -> Cmulti (c, [||])
2935 | Csingle _ -> Csingle [||]
2936 | Csplit (c, _) -> Csplit (c, [||])
2938 state.x <- leftx;
2939 if conf.verbose
2940 then
2941 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2942 (100.0*.conf.zoom)
2944 reshape state.winw state.winh;
2945 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2948 let togglebirdseye () =
2949 match state.mode with
2950 | Birdseye vals -> leavebirdseye vals true
2951 | View -> enterbirdseye ()
2952 | _ -> ()
2955 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2956 let pageno = max 0 (pageno - incr) in
2957 let rec loop = function
2958 | [] -> gotopage1 pageno 0
2959 | l :: _ when l.pageno = pageno ->
2960 if l.pagedispy >= 0 && l.pagey = 0
2961 then G.postRedisplay "upbirdseye"
2962 else gotopage1 pageno 0
2963 | _ :: rest -> loop rest
2965 loop state.layout;
2966 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2969 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2970 let pageno = min (state.pagecount - 1) (pageno + incr) in
2971 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2972 let rec loop = function
2973 | [] ->
2974 let y, h = getpageyh pageno in
2975 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2976 gotoy (clamp dy)
2977 | l :: _ when l.pageno = pageno ->
2978 if l.pagevh != l.pageh
2979 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2980 else G.postRedisplay "downbirdseye"
2981 | _ :: rest -> loop rest
2983 loop state.layout
2986 let optentry mode _ key =
2987 let btos b = if b then "on" else "off" in
2988 if key >= 32 && key < 127
2989 then
2990 let c = Char.chr key in
2991 match c with
2992 | 's' ->
2993 let ondone s =
2994 try conf.scrollstep <- int_of_string s with exc ->
2995 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2997 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2999 | 'A' ->
3000 let ondone s =
3002 conf.autoscrollstep <- int_of_string s;
3003 if state.autoscroll <> None
3004 then state.autoscroll <- Some conf.autoscrollstep
3005 with exc ->
3006 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3008 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3010 | 'C' ->
3011 let ondone s =
3013 let n, a, b = multicolumns_of_string s in
3014 setcolumns mode n a b;
3015 with exc ->
3016 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3018 TEswitch ("columns: ", "", None, textentry, ondone, true)
3020 | 'Z' ->
3021 let ondone s =
3023 let zoom = float (int_of_string s) /. 100.0 in
3024 setzoom zoom
3025 with exc ->
3026 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3028 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3030 | 't' ->
3031 let ondone s =
3033 conf.thumbw <- bound (int_of_string s) 2 4096;
3034 state.text <-
3035 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3036 begin match mode with
3037 | Birdseye beye ->
3038 leavebirdseye beye false;
3039 enterbirdseye ();
3040 | _ -> ();
3042 with exc ->
3043 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3045 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3047 | 'R' ->
3048 let ondone s =
3049 match try
3050 Some (int_of_string s)
3051 with exc ->
3052 state.text <- Printf.sprintf "bad integer `%s': %s"
3053 s (exntos exc);
3054 None
3055 with
3056 | Some angle -> reqlayout angle conf.fitmodel
3057 | None -> ()
3059 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3061 | 'i' ->
3062 conf.icase <- not conf.icase;
3063 TEdone ("case insensitive search " ^ (btos conf.icase))
3065 | 'p' ->
3066 conf.preload <- not conf.preload;
3067 gotoy state.y;
3068 TEdone ("preload " ^ (btos conf.preload))
3070 | 'v' ->
3071 conf.verbose <- not conf.verbose;
3072 TEdone ("verbose " ^ (btos conf.verbose))
3074 | 'd' ->
3075 conf.debug <- not conf.debug;
3076 TEdone ("debug " ^ (btos conf.debug))
3078 | 'h' ->
3079 conf.maxhfit <- not conf.maxhfit;
3080 state.maxy <- calcheight ();
3081 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3083 | 'c' ->
3084 conf.crophack <- not conf.crophack;
3085 TEdone ("crophack " ^ btos conf.crophack)
3087 | 'a' ->
3088 let s =
3089 match conf.maxwait with
3090 | None ->
3091 conf.maxwait <- Some infinity;
3092 "always wait for page to complete"
3093 | Some _ ->
3094 conf.maxwait <- None;
3095 "show placeholder if page is not ready"
3097 TEdone s
3099 | 'f' ->
3100 conf.underinfo <- not conf.underinfo;
3101 TEdone ("underinfo " ^ btos conf.underinfo)
3103 | 'P' ->
3104 conf.savebmarks <- not conf.savebmarks;
3105 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3107 | 'S' ->
3108 let ondone s =
3110 let pageno, py =
3111 match state.layout with
3112 | [] -> 0, 0
3113 | l :: _ ->
3114 l.pageno, l.pagey
3116 conf.interpagespace <- int_of_string s;
3117 docolumns conf.columns;
3118 state.maxy <- calcheight ();
3119 let y = getpagey pageno in
3120 gotoy (y + py)
3121 with exc ->
3122 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3124 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3126 | 'l' ->
3127 let fm =
3128 match conf.fitmodel with
3129 | FitProportional -> FitWidth
3130 | _ -> FitProportional
3132 reqlayout conf.angle fm;
3133 TEdone ("proportional display " ^ btos (fm == FitProportional))
3135 | 'T' ->
3136 settrim (not conf.trimmargins) conf.trimfuzz;
3137 TEdone ("trim margins " ^ btos conf.trimmargins)
3139 | 'I' ->
3140 conf.invert <- not conf.invert;
3141 TEdone ("invert colors " ^ btos conf.invert)
3143 | 'x' ->
3144 let ondone s =
3145 cbput state.hists.sel s;
3146 conf.selcmd <- s;
3148 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3149 textentry, ondone, true)
3151 | _ ->
3152 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3153 TEstop
3154 else
3155 TEcont state.text
3158 class type lvsource = object
3159 method getitemcount : int
3160 method getitem : int -> (string * int)
3161 method hasaction : int -> bool
3162 method exit :
3163 uioh:uioh ->
3164 cancel:bool ->
3165 active:int ->
3166 first:int ->
3167 pan:int ->
3168 qsearch:string ->
3169 uioh option
3170 method getactive : int
3171 method getfirst : int
3172 method getqsearch : string
3173 method setqsearch : string -> unit
3174 method getpan : int
3175 end;;
3177 class virtual lvsourcebase = object
3178 val mutable m_active = 0
3179 val mutable m_first = 0
3180 val mutable m_qsearch = ""
3181 val mutable m_pan = 0
3182 method getactive = m_active
3183 method getfirst = m_first
3184 method getqsearch = m_qsearch
3185 method getpan = m_pan
3186 method setqsearch s = m_qsearch <- s
3187 end;;
3189 let withoutlastutf8 s =
3190 let len = String.length s in
3191 if len = 0
3192 then s
3193 else
3194 let rec find pos =
3195 if pos = 0
3196 then pos
3197 else
3198 let b = Char.code s.[pos] in
3199 if b land 0b11000000 = 0b11000000
3200 then pos
3201 else find (pos-1)
3203 let first =
3204 if Char.code s.[len-1] land 0x80 = 0
3205 then len-1
3206 else find (len-1)
3208 String.sub s 0 first;
3211 let textentrykeyboard
3212 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3213 let key =
3214 if key >= 0xffb0 && key <= 0xffb9
3215 then key - 0xffb0 + 48 else key
3217 let enttext te =
3218 state.mode <- Textentry (te, onleave);
3219 state.text <- "";
3220 enttext ();
3221 G.postRedisplay "textentrykeyboard enttext";
3223 let histaction cmd =
3224 match opthist with
3225 | None -> ()
3226 | Some (action, _) ->
3227 state.mode <- Textentry (
3228 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3230 G.postRedisplay "textentry histaction"
3232 match key with
3233 | 0xff08 -> (* backspace *)
3234 let s = withoutlastutf8 text in
3235 let len = String.length s in
3236 if cancelonempty && len = 0
3237 then (
3238 onleave Cancel;
3239 G.postRedisplay "textentrykeyboard after cancel";
3241 else (
3242 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3245 | 0xff0d | 0xff8d -> (* (kp) enter *)
3246 ondone text;
3247 onleave Confirm;
3248 G.postRedisplay "textentrykeyboard after confirm"
3250 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3251 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3252 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3253 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3255 | 0xff1b -> (* escape*)
3256 if String.length text = 0
3257 then (
3258 begin match opthist with
3259 | None -> ()
3260 | Some (_, onhistcancel) -> onhistcancel ()
3261 end;
3262 onleave Cancel;
3263 state.text <- "";
3264 G.postRedisplay "textentrykeyboard after cancel2"
3266 else (
3267 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3270 | 0xff9f | 0xffff -> () (* delete *)
3272 | _ when key != 0
3273 && key land 0xff00 != 0xff00 (* keyboard *)
3274 && key land 0xfe00 != 0xfe00 (* xkb *)
3275 && key land 0xfd00 != 0xfd00 (* 3270 *)
3277 begin match onkey text key with
3278 | TEdone text ->
3279 ondone text;
3280 onleave Confirm;
3281 G.postRedisplay "textentrykeyboard after confirm2";
3283 | TEcont text ->
3284 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3286 | TEstop ->
3287 onleave Cancel;
3288 G.postRedisplay "textentrykeyboard after cancel3"
3290 | TEswitch te ->
3291 state.mode <- Textentry (te, onleave);
3292 G.postRedisplay "textentrykeyboard switch";
3293 end;
3295 | _ ->
3296 vlog "unhandled key %s" (Wsi.keyname key)
3299 let firstof first active =
3300 if first > active || abs (first - active) > fstate.maxrows - 1
3301 then max 0 (active - (fstate.maxrows/2))
3302 else first
3305 let calcfirst first active =
3306 if active > first
3307 then
3308 let rows = active - first in
3309 if rows > fstate.maxrows then active - fstate.maxrows else first
3310 else active
3313 let scrollph y maxy =
3314 let sh = float (maxy + state.winh) /. float state.winh in
3315 let sh = float state.winh /. sh in
3316 let sh = max sh (float conf.scrollh) in
3318 let percent = float y /. float maxy in
3319 let position = (float state.winh -. sh) *. percent in
3321 let position =
3322 if position +. sh > float state.winh
3323 then float state.winh -. sh
3324 else position
3326 position, sh;
3329 let coe s = (s :> uioh);;
3331 class listview ~(source:lvsource) ~trusted ~modehash =
3332 object (self)
3333 val m_pan = source#getpan
3334 val m_first = source#getfirst
3335 val m_active = source#getactive
3336 val m_qsearch = source#getqsearch
3337 val m_prev_uioh = state.uioh
3339 method private elemunder y =
3340 let n = y / (fstate.fontsize+1) in
3341 if m_first + n < source#getitemcount
3342 then (
3343 if source#hasaction (m_first + n)
3344 then Some (m_first + n)
3345 else None
3347 else None
3349 method display =
3350 Gl.enable `blend;
3351 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3352 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3353 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3354 GlDraw.color (1., 1., 1.);
3355 Gl.enable `texture_2d;
3356 let fs = fstate.fontsize in
3357 let nfs = fs + 1 in
3358 let ww = fstate.wwidth in
3359 let tabw = 30.0*.ww in
3360 let itemcount = source#getitemcount in
3361 let rec loop row =
3362 if (row - m_first) > fstate.maxrows
3363 then ()
3364 else (
3365 if row >= 0 && row < itemcount
3366 then (
3367 let (s, level) = source#getitem row in
3368 let y = (row - m_first) * nfs in
3369 let x = 5.0 +. float (level + m_pan) *. ww in
3370 if row = m_active
3371 then (
3372 Gl.disable `texture_2d;
3373 GlDraw.polygon_mode `both `line;
3374 let alpha = if source#hasaction row then 0.9 else 0.3 in
3375 GlDraw.color (1., 1., 1.) ~alpha;
3376 GlDraw.rect (1., float (y + 1))
3377 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3378 GlDraw.polygon_mode `both `fill;
3379 GlDraw.color (1., 1., 1.);
3380 Gl.enable `texture_2d;
3383 let drawtabularstring s =
3384 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3385 if trusted
3386 then
3387 let tabpos = try String.index s '\t' with Not_found -> -1 in
3388 if tabpos > 0
3389 then
3390 let len = String.length s - tabpos - 1 in
3391 let s1 = String.sub s 0 tabpos
3392 and s2 = String.sub s (tabpos + 1) len in
3393 let nx = drawstr x s1 in
3394 let sw = nx -. x in
3395 let x = x +. (max tabw sw) in
3396 drawstr x s2
3397 else
3398 drawstr x s
3399 else
3400 drawstr x s
3402 let _ = drawtabularstring s in
3403 loop (row+1)
3407 loop m_first;
3408 Gl.disable `blend;
3409 Gl.disable `texture_2d;
3411 method updownlevel incr =
3412 let len = source#getitemcount in
3413 let curlevel =
3414 if m_active >= 0 && m_active < len
3415 then snd (source#getitem m_active)
3416 else -1
3418 let rec flow i =
3419 if i = len then i-1 else if i = -1 then 0 else
3420 let _, l = source#getitem i in
3421 if l != curlevel then i else flow (i+incr)
3423 let active = flow m_active in
3424 let first = calcfirst m_first active in
3425 G.postRedisplay "outline updownlevel";
3426 {< m_active = active; m_first = first >}
3428 method private key1 key mask =
3429 let set1 active first qsearch =
3430 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3432 let search active pattern incr =
3433 let active = if active = -1 then m_first else active in
3434 let dosearch re =
3435 let rec loop n =
3436 if n >= 0 && n < source#getitemcount
3437 then (
3438 let s, _ = source#getitem n in
3440 (try ignore (Str.search_forward re s 0); true
3441 with Not_found -> false)
3442 then Some n
3443 else loop (n + incr)
3445 else None
3447 loop active
3450 let re = Str.regexp_case_fold pattern in
3451 dosearch re
3452 with Failure s ->
3453 state.text <- s;
3454 None
3456 let itemcount = source#getitemcount in
3457 let find start incr =
3458 let rec find i =
3459 if i = -1 || i = itemcount
3460 then -1
3461 else (
3462 if source#hasaction i
3463 then i
3464 else find (i + incr)
3467 find start
3469 let set active first =
3470 let first = bound first 0 (itemcount - fstate.maxrows) in
3471 state.text <- "";
3472 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3474 let navigate incr =
3475 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3476 let active, first =
3477 let incr1 = if incr > 0 then 1 else -1 in
3478 if isvisible m_first m_active
3479 then
3480 let next =
3481 let next = m_active + incr in
3482 let next =
3483 if next < 0 || next >= itemcount
3484 then -1
3485 else find next incr1
3487 if abs (m_active - next) > fstate.maxrows
3488 then -1
3489 else next
3491 if next = -1
3492 then
3493 let first = m_first + incr in
3494 let first = bound first 0 (itemcount - 1) in
3495 let next =
3496 let next = m_active + incr in
3497 let next = bound next 0 (itemcount - 1) in
3498 find next ~-incr1
3500 let active =
3501 if next = -1
3502 then m_active
3503 else (
3504 if isvisible first next
3505 then next
3506 else m_active
3509 active, first
3510 else
3511 let first = min next m_first in
3512 let first =
3513 if abs (next - first) > fstate.maxrows
3514 then first + incr
3515 else first
3517 next, first
3518 else
3519 let first = m_first + incr in
3520 let first = bound first 0 (itemcount - 1) in
3521 let active =
3522 let next = m_active + incr in
3523 let next = bound next 0 (itemcount - 1) in
3524 let next = find next incr1 in
3525 let active =
3526 if next = -1 || abs (m_active - first) > fstate.maxrows
3527 then (
3528 let active = if m_active = -1 then next else m_active in
3529 active
3531 else next
3533 if isvisible first active
3534 then active
3535 else -1
3537 active, first
3539 G.postRedisplay "listview navigate";
3540 set active first;
3542 match key with
3543 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3544 let incr = if key = 0x72 then -1 else 1 in
3545 let active, first =
3546 match search (m_active + incr) m_qsearch incr with
3547 | None ->
3548 state.text <- m_qsearch ^ " [not found]";
3549 m_active, m_first
3550 | Some active ->
3551 state.text <- m_qsearch;
3552 active, firstof m_first active
3554 G.postRedisplay "listview ctrl-r/s";
3555 set1 active first m_qsearch;
3557 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3558 if m_active >= 0 && m_active < source#getitemcount
3559 then (
3560 let s, _ = source#getitem m_active in
3561 selstring s;
3563 coe self
3565 | 0xff08 -> (* backspace *)
3566 if String.length m_qsearch = 0
3567 then coe self
3568 else (
3569 let qsearch = withoutlastutf8 m_qsearch in
3570 let len = String.length qsearch in
3571 if len = 0
3572 then (
3573 state.text <- "";
3574 G.postRedisplay "listview empty qsearch";
3575 set1 m_active m_first "";
3577 else
3578 let active, first =
3579 match search m_active qsearch ~-1 with
3580 | None ->
3581 state.text <- qsearch ^ " [not found]";
3582 m_active, m_first
3583 | Some active ->
3584 state.text <- qsearch;
3585 active, firstof m_first active
3587 G.postRedisplay "listview backspace qsearch";
3588 set1 active first qsearch
3591 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3592 let pattern = m_qsearch ^ toutf8 key in
3593 let active, first =
3594 match search m_active pattern 1 with
3595 | None ->
3596 state.text <- pattern ^ " [not found]";
3597 m_active, m_first
3598 | Some active ->
3599 state.text <- pattern;
3600 active, firstof m_first active
3602 G.postRedisplay "listview qsearch add";
3603 set1 active first pattern;
3605 | 0xff1b -> (* escape *)
3606 state.text <- "";
3607 if String.length m_qsearch = 0
3608 then (
3609 G.postRedisplay "list view escape";
3610 begin
3611 match
3612 source#exit (coe self) true m_active m_first m_pan m_qsearch
3613 with
3614 | None -> m_prev_uioh
3615 | Some uioh -> uioh
3618 else (
3619 G.postRedisplay "list view kill qsearch";
3620 source#setqsearch "";
3621 coe {< m_qsearch = "" >}
3624 | 0xff0d | 0xff8d -> (* (kp) enter *)
3625 state.text <- "";
3626 let self = {< m_qsearch = "" >} in
3627 source#setqsearch "";
3628 let opt =
3629 G.postRedisplay "listview enter";
3630 if m_active >= 0 && m_active < source#getitemcount
3631 then (
3632 source#exit (coe self) false m_active m_first m_pan "";
3634 else (
3635 source#exit (coe self) true m_active m_first m_pan "";
3638 begin match opt with
3639 | None -> m_prev_uioh
3640 | Some uioh -> uioh
3643 | 0xff9f | 0xffff -> (* (kp) delete *)
3644 coe self
3646 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3647 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3648 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3649 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3651 | 0xff53 | 0xff98 -> (* (kp) right *)
3652 state.text <- "";
3653 G.postRedisplay "listview right";
3654 coe {< m_pan = m_pan - 1 >}
3656 | 0xff51 | 0xff96 -> (* (kp) left *)
3657 state.text <- "";
3658 G.postRedisplay "listview left";
3659 coe {< m_pan = m_pan + 1 >}
3661 | 0xff50 | 0xff95 -> (* (kp) home *)
3662 let active = find 0 1 in
3663 G.postRedisplay "listview home";
3664 set active 0;
3666 | 0xff57 | 0xff9c -> (* (kp) end *)
3667 let first = max 0 (itemcount - fstate.maxrows) in
3668 let active = find (itemcount - 1) ~-1 in
3669 G.postRedisplay "listview end";
3670 set active first;
3672 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3673 coe self
3675 | _ ->
3676 dolog "listview unknown key %#x" key; coe self
3678 method key key mask =
3679 match state.mode with
3680 | Textentry te -> textentrykeyboard key mask te; coe self
3681 | _ -> self#key1 key mask
3683 method button button down x y _ =
3684 let opt =
3685 match button with
3686 | 1 when x > state.winw - conf.scrollbw ->
3687 G.postRedisplay "listview scroll";
3688 if down
3689 then
3690 let _, position, sh = self#scrollph in
3691 if y > truncate position && y < truncate (position +. sh)
3692 then (
3693 state.mstate <- Mscrolly;
3694 Some (coe self)
3696 else
3697 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3698 let first = truncate (s *. float source#getitemcount) in
3699 let first = min source#getitemcount first in
3700 Some (coe {< m_first = first; m_active = first >})
3701 else (
3702 state.mstate <- Mnone;
3703 Some (coe self);
3705 | 1 when not down ->
3706 begin match self#elemunder y with
3707 | Some n ->
3708 G.postRedisplay "listview click";
3709 source#exit
3710 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3711 | _ ->
3712 Some (coe self)
3714 | n when (n == 4 || n == 5) && not down ->
3715 let len = source#getitemcount in
3716 let first =
3717 if n = 5 && m_first + fstate.maxrows >= len
3718 then
3719 m_first
3720 else
3721 let first = m_first + (if n == 4 then -1 else 1) in
3722 bound first 0 (len - 1)
3724 G.postRedisplay "listview wheel";
3725 Some (coe {< m_first = first >})
3726 | n when (n = 6 || n = 7) && not down ->
3727 let inc = m_first + (if n = 7 then -1 else 1) in
3728 G.postRedisplay "listview hwheel";
3729 Some (coe {< m_pan = m_pan + inc >})
3730 | _ ->
3731 Some (coe self)
3733 match opt with
3734 | None -> m_prev_uioh
3735 | Some uioh -> uioh
3737 method motion _ y =
3738 match state.mstate with
3739 | Mscrolly ->
3740 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3741 let first = truncate (s *. float source#getitemcount) in
3742 let first = min source#getitemcount first in
3743 G.postRedisplay "listview motion";
3744 coe {< m_first = first; m_active = first >}
3745 | _ -> coe self
3747 method pmotion x y =
3748 if x < state.winw - conf.scrollbw
3749 then
3750 let n =
3751 match self#elemunder y with
3752 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3753 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3755 let o =
3756 if n != m_active
3757 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3758 else self
3760 coe o
3761 else (
3762 Wsi.setcursor Wsi.CURSOR_INHERIT;
3763 coe self
3766 method infochanged _ = ()
3768 method scrollpw = (0, 0.0, 0.0)
3769 method scrollph =
3770 let nfs = fstate.fontsize + 1 in
3771 let y = m_first * nfs in
3772 let itemcount = source#getitemcount in
3773 let maxi = max 0 (itemcount - fstate.maxrows) in
3774 let maxy = maxi * nfs in
3775 let p, h = scrollph y maxy in
3776 conf.scrollbw, p, h
3778 method modehash = modehash
3779 method eformsgs = false
3780 end;;
3782 class outlinelistview ~source =
3783 object (self)
3784 inherit listview
3785 ~source:(source :> lvsource)
3786 ~trusted:false
3787 ~modehash:(findkeyhash conf "outline")
3788 as super
3790 method key key mask =
3791 let calcfirst first active =
3792 if active > first
3793 then
3794 let rows = active - first in
3795 let maxrows =
3796 if String.length state.text = 0
3797 then fstate.maxrows
3798 else fstate.maxrows - 2
3800 if rows > maxrows then active - maxrows else first
3801 else active
3803 let navigate incr =
3804 let active = m_active + incr in
3805 let active = bound active 0 (source#getitemcount - 1) in
3806 let first = calcfirst m_first active in
3807 G.postRedisplay "outline navigate";
3808 coe {< m_active = active; m_first = first >}
3810 let ctrl = Wsi.withctrl mask in
3811 match key with
3812 | 110 when ctrl -> (* ctrl-n *)
3813 source#narrow m_qsearch;
3814 G.postRedisplay "outline ctrl-n";
3815 coe {< m_first = 0; m_active = 0 >}
3817 | 117 when ctrl -> (* ctrl-u *)
3818 source#denarrow;
3819 G.postRedisplay "outline ctrl-u";
3820 state.text <- "";
3821 coe {< m_first = 0; m_active = 0 >}
3823 | 108 when ctrl -> (* ctrl-l *)
3824 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3825 G.postRedisplay "outline ctrl-l";
3826 coe {< m_first = first >}
3828 | 0xff9f | 0xffff -> (* (kp) delete *)
3829 source#remove m_active;
3830 G.postRedisplay "outline delete";
3831 let active = max 0 (m_active-1) in
3832 coe {< m_first = firstof m_first active;
3833 m_active = active >}
3835 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3836 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3837 | 0xff55 | 0xff9a -> (* (kp) prior *)
3838 navigate ~-(fstate.maxrows)
3839 | 0xff56 | 0xff9b -> (* (kp) next *)
3840 navigate fstate.maxrows
3842 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3843 let o =
3844 if ctrl
3845 then (
3846 G.postRedisplay "outline ctrl right";
3847 {< m_pan = m_pan + 1 >}
3849 else self#updownlevel 1
3851 coe o
3853 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3854 let o =
3855 if ctrl
3856 then (
3857 G.postRedisplay "outline ctrl left";
3858 {< m_pan = m_pan - 1 >}
3860 else self#updownlevel ~-1
3862 coe o
3864 | 0xff50 | 0xff95 -> (* (kp) home *)
3865 G.postRedisplay "outline home";
3866 coe {< m_first = 0; m_active = 0 >}
3868 | 0xff57 | 0xff9c -> (* (kp) end *)
3869 let active = source#getitemcount - 1 in
3870 let first = max 0 (active - fstate.maxrows) in
3871 G.postRedisplay "outline end";
3872 coe {< m_active = active; m_first = first >}
3874 | _ -> super#key key mask
3877 let outlinesource usebookmarks =
3878 let empty = [||] in
3879 (object
3880 inherit lvsourcebase
3881 val mutable m_items = empty
3882 val mutable m_orig_items = empty
3883 val mutable m_prev_items = empty
3884 val mutable m_narrow_pattern = ""
3885 val mutable m_hadremovals = false
3887 method getitemcount =
3888 Array.length m_items + (if m_hadremovals then 1 else 0)
3890 method getitem n =
3891 if n == Array.length m_items && m_hadremovals
3892 then
3893 ("[Confirm removal]", 0)
3894 else
3895 let s, n, _ = m_items.(n) in
3896 (s, n)
3898 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3899 ignore (uioh, first, qsearch);
3900 let confrimremoval = m_hadremovals && active = Array.length m_items in
3901 let items =
3902 if String.length m_narrow_pattern = 0
3903 then m_orig_items
3904 else m_items
3906 if not cancel
3907 then (
3908 if not confrimremoval
3909 then(
3910 let _, _, anchor = m_items.(active) in
3911 gotoghyll (getanchory anchor);
3912 m_items <- items;
3914 else (
3915 state.bookmarks <- Array.to_list m_items;
3916 m_orig_items <- m_items;
3919 else m_items <- items;
3920 m_pan <- pan;
3921 None
3923 method hasaction _ = true
3925 method greetmsg =
3926 if Array.length m_items != Array.length m_orig_items
3927 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3928 else ""
3930 method narrow pattern =
3931 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3932 match reopt with
3933 | None -> ()
3934 | Some re ->
3935 let rec loop accu n =
3936 if n = -1
3937 then (
3938 m_narrow_pattern <- pattern;
3939 m_items <- Array.of_list accu
3941 else
3942 let (s, _, _) as o = m_items.(n) in
3943 let accu =
3944 if (try ignore (Str.search_forward re s 0); true
3945 with Not_found -> false)
3946 then o :: accu
3947 else accu
3949 loop accu (n-1)
3951 loop [] (Array.length m_items - 1)
3953 method denarrow =
3954 m_orig_items <- (
3955 if usebookmarks
3956 then Array.of_list state.bookmarks
3957 else state.outlines
3959 m_items <- m_orig_items
3961 method remove m =
3962 if usebookmarks
3963 then
3964 if m >= 0 && m < Array.length m_items
3965 then (
3966 m_hadremovals <- true;
3967 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3968 let n = if n >= m then n+1 else n in
3969 m_items.(n)
3973 method reset anchor items =
3974 m_hadremovals <- false;
3975 if m_orig_items == empty || m_prev_items != items
3976 then (
3977 m_orig_items <- items;
3978 if String.length m_narrow_pattern = 0
3979 then m_items <- items;
3981 m_prev_items <- items;
3982 let rely = getanchory anchor in
3983 let active =
3984 let rec loop n best bestd =
3985 if n = Array.length m_items
3986 then best
3987 else
3988 let (_, _, anchor) = m_items.(n) in
3989 let orely = getanchory anchor in
3990 let d = abs (orely - rely) in
3991 if d < bestd
3992 then loop (n+1) n d
3993 else loop (n+1) best bestd
3995 loop 0 ~-1 max_int
3997 m_active <- active;
3998 m_first <- firstof m_first active
3999 end)
4002 let enterselector usebookmarks =
4003 let source = outlinesource usebookmarks in
4004 fun errmsg ->
4005 let outlines =
4006 if usebookmarks
4007 then Array.of_list state.bookmarks
4008 else state.outlines
4010 if Array.length outlines = 0
4011 then (
4012 showtext ' ' errmsg;
4014 else (
4015 state.text <- source#greetmsg;
4016 Wsi.setcursor Wsi.CURSOR_INHERIT;
4017 let anchor = getanchor () in
4018 source#reset anchor outlines;
4019 state.uioh <- coe (new outlinelistview ~source);
4020 G.postRedisplay "enter selector";
4024 let enteroutlinemode =
4025 let f = enterselector false in
4026 fun ()-> f "Document has no outline";
4029 let enterbookmarkmode =
4030 let f = enterselector true in
4031 fun () -> f "Document has no bookmarks (yet)";
4034 let color_of_string s =
4035 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4036 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4040 let color_to_string (r, g, b) =
4041 let r = truncate (r *. 256.0)
4042 and g = truncate (g *. 256.0)
4043 and b = truncate (b *. 256.0) in
4044 Printf.sprintf "%d/%d/%d" r g b
4047 let irect_of_string s =
4048 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4051 let irect_to_string (x0,y0,x1,y1) =
4052 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4055 let makecheckers () =
4056 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4057 following to say:
4058 converted by Issac Trotts. July 25, 2002 *)
4059 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4060 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4061 let id = GlTex.gen_texture () in
4062 GlTex.bind_texture `texture_2d id;
4063 GlPix.store (`unpack_alignment 1);
4064 GlTex.image2d image;
4065 List.iter (GlTex.parameter ~target:`texture_2d)
4066 [ `mag_filter `nearest; `min_filter `nearest ];
4070 let setcheckers enabled =
4071 match state.texid with
4072 | None ->
4073 if enabled then state.texid <- Some (makecheckers ())
4075 | Some texid ->
4076 if not enabled
4077 then (
4078 GlTex.delete_texture texid;
4079 state.texid <- None;
4083 let int_of_string_with_suffix s =
4084 let l = String.length s in
4085 let s1, shift =
4086 if l > 1
4087 then
4088 let suffix = Char.lowercase s.[l-1] in
4089 match suffix with
4090 | 'k' -> String.sub s 0 (l-1), 10
4091 | 'm' -> String.sub s 0 (l-1), 20
4092 | 'g' -> String.sub s 0 (l-1), 30
4093 | _ -> s, 0
4094 else s, 0
4096 let n = int_of_string s1 in
4097 let m = n lsl shift in
4098 if m < 0 || m < n
4099 then raise (Failure "value too large")
4100 else m
4103 let string_with_suffix_of_int n =
4104 if n = 0
4105 then "0"
4106 else
4107 let n, s =
4108 if n land ((1 lsl 30) - 1) = 0
4109 then n lsr 30, "G"
4110 else (
4111 if n land ((1 lsl 20) - 1) = 0
4112 then n lsr 20, "M"
4113 else (
4114 if n land ((1 lsl 10) - 1) = 0
4115 then n lsr 10, "K"
4116 else n, ""
4120 let rec loop s n =
4121 let h = n mod 1000 in
4122 let n = n / 1000 in
4123 if n = 0
4124 then string_of_int h ^ s
4125 else (
4126 let s = Printf.sprintf "_%03d%s" h s in
4127 loop s n
4130 loop "" n ^ s;
4133 let defghyllscroll = (40, 8, 32);;
4134 let ghyllscroll_of_string s =
4135 let (n, a, b) as nab =
4136 if s = "default"
4137 then defghyllscroll
4138 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4140 if n <= a || n <= b || a >= b
4141 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4142 nab;
4145 let ghyllscroll_to_string ((n, a, b) as nab) =
4146 if nab = defghyllscroll
4147 then "default"
4148 else Printf.sprintf "%d,%d,%d" n a b;
4151 let describe_location () =
4152 let fn = page_of_y state.y in
4153 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4154 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4155 let percent =
4156 if maxy <= 0
4157 then 100.
4158 else (100. *. (float state.y /. float maxy))
4160 if fn = ln
4161 then
4162 Printf.sprintf "page %d of %d [%.2f%%]"
4163 (fn+1) state.pagecount percent
4164 else
4165 Printf.sprintf
4166 "pages %d-%d of %d [%.2f%%]"
4167 (fn+1) (ln+1) state.pagecount percent
4170 let setpresentationmode v =
4171 let n = page_of_y state.y in
4172 state.anchor <- (n, 0.0, 1.0);
4173 conf.presentation <- v;
4174 if conf.presentation
4175 then (
4176 if not conf.scrollbarinpm
4177 then state.scrollw <- 0;
4179 else state.scrollw <- conf.scrollbw;
4180 if conf.fitmodel = FitPage
4181 then reqlayout conf.angle conf.fitmodel;
4182 represent ();
4185 let enterinfomode =
4186 let btos b = if b then "\xe2\x88\x9a" else "" in
4187 let showextended = ref false in
4188 let leave mode = function
4189 | Confirm -> state.mode <- mode
4190 | Cancel -> state.mode <- mode in
4191 let src =
4192 (object
4193 val mutable m_first_time = true
4194 val mutable m_l = []
4195 val mutable m_a = [||]
4196 val mutable m_prev_uioh = nouioh
4197 val mutable m_prev_mode = View
4199 inherit lvsourcebase
4201 method reset prev_mode prev_uioh =
4202 m_a <- Array.of_list (List.rev m_l);
4203 m_l <- [];
4204 m_prev_mode <- prev_mode;
4205 m_prev_uioh <- prev_uioh;
4206 if m_first_time
4207 then (
4208 let rec loop n =
4209 if n >= Array.length m_a
4210 then ()
4211 else
4212 match m_a.(n) with
4213 | _, _, _, Action _ -> m_active <- n
4214 | _ -> loop (n+1)
4216 loop 0;
4217 m_first_time <- false;
4220 method int name get set =
4221 m_l <-
4222 (name, `int get, 1, Action (
4223 fun u ->
4224 let ondone s =
4225 try set (int_of_string s)
4226 with exn ->
4227 state.text <- Printf.sprintf "bad integer `%s': %s"
4228 s (exntos exn)
4230 state.text <- "";
4231 let te = name ^ ": ", "", None, intentry, ondone, true in
4232 state.mode <- Textentry (te, leave m_prev_mode);
4234 )) :: m_l
4236 method int_with_suffix name get set =
4237 m_l <-
4238 (name, `intws get, 1, Action (
4239 fun u ->
4240 let ondone s =
4241 try set (int_of_string_with_suffix s)
4242 with exn ->
4243 state.text <- Printf.sprintf "bad integer `%s': %s"
4244 s (exntos exn)
4246 state.text <- "";
4247 let te =
4248 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4250 state.mode <- Textentry (te, leave m_prev_mode);
4252 )) :: m_l
4254 method bool ?(offset=1) ?(btos=btos) name get set =
4255 m_l <-
4256 (name, `bool (btos, get), offset, Action (
4257 fun u ->
4258 let v = get () in
4259 set (not v);
4261 )) :: m_l
4263 method color name get set =
4264 m_l <-
4265 (name, `color get, 1, Action (
4266 fun u ->
4267 let invalid = (nan, nan, nan) in
4268 let ondone s =
4269 let c =
4270 try color_of_string s
4271 with exn ->
4272 state.text <- Printf.sprintf "bad color `%s': %s"
4273 s (exntos exn);
4274 invalid
4276 if c <> invalid
4277 then set c;
4279 let te = name ^ ": ", "", None, textentry, ondone, true in
4280 state.text <- color_to_string (get ());
4281 state.mode <- Textentry (te, leave m_prev_mode);
4283 )) :: m_l
4285 method string name get set =
4286 m_l <-
4287 (name, `string get, 1, Action (
4288 fun u ->
4289 let ondone s = set s in
4290 let te = name ^ ": ", "", None, textentry, ondone, true in
4291 state.mode <- Textentry (te, leave m_prev_mode);
4293 )) :: m_l
4295 method colorspace name get set =
4296 m_l <-
4297 (name, `string get, 1, Action (
4298 fun _ ->
4299 let source =
4300 let vals = [| "rgb"; "bgr"; "gray" |] in
4301 (object
4302 inherit lvsourcebase
4304 initializer
4305 m_active <- int_of_colorspace conf.colorspace;
4306 m_first <- 0;
4308 method getitemcount = Array.length vals
4309 method getitem n = (vals.(n), 0)
4310 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4311 ignore (uioh, first, pan, qsearch);
4312 if not cancel then set active;
4313 None
4314 method hasaction _ = true
4315 end)
4317 state.text <- "";
4318 let modehash = findkeyhash conf "info" in
4319 coe (new listview ~source ~trusted:true ~modehash)
4320 )) :: m_l
4322 method fitmodel name get set =
4323 m_l <-
4324 (name, `string get, 1, Action (
4325 fun _ ->
4326 let source =
4327 let vals = [| "fit width"; "proportional"; "fit page" |] in
4328 (object
4329 inherit lvsourcebase
4331 initializer
4332 m_active <- int_of_fitmodel conf.fitmodel;
4333 m_first <- 0;
4335 method getitemcount = Array.length vals
4336 method getitem n = (vals.(n), 0)
4337 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4338 ignore (uioh, first, pan, qsearch);
4339 if not cancel then set active;
4340 None
4341 method hasaction _ = true
4342 end)
4344 state.text <- "";
4345 let modehash = findkeyhash conf "info" in
4346 coe (new listview ~source ~trusted:true ~modehash)
4347 )) :: m_l
4349 method caption s offset =
4350 m_l <- (s, `empty, offset, Noaction) :: m_l
4352 method caption2 s f offset =
4353 m_l <- (s, `string f, offset, Noaction) :: m_l
4355 method getitemcount = Array.length m_a
4357 method getitem n =
4358 let tostr = function
4359 | `int f -> string_of_int (f ())
4360 | `intws f -> string_with_suffix_of_int (f ())
4361 | `string f -> f ()
4362 | `color f -> color_to_string (f ())
4363 | `bool (btos, f) -> btos (f ())
4364 | `empty -> ""
4366 let name, t, offset, _ = m_a.(n) in
4367 ((let s = tostr t in
4368 if String.length s > 0
4369 then Printf.sprintf "%s\t%s" name s
4370 else name),
4371 offset)
4373 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4374 let uiohopt =
4375 if not cancel
4376 then (
4377 m_qsearch <- qsearch;
4378 let uioh =
4379 match m_a.(active) with
4380 | _, _, _, Action f -> f uioh
4381 | _ -> uioh
4383 Some uioh
4385 else None
4387 m_active <- active;
4388 m_first <- first;
4389 m_pan <- pan;
4390 uiohopt
4392 method hasaction n =
4393 match m_a.(n) with
4394 | _, _, _, Action _ -> true
4395 | _ -> false
4396 end)
4398 let rec fillsrc prevmode prevuioh =
4399 let sep () = src#caption "" 0 in
4400 let colorp name get set =
4401 src#string name
4402 (fun () -> color_to_string (get ()))
4403 (fun v ->
4405 let c = color_of_string v in
4406 set c
4407 with exn ->
4408 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4411 let oldmode = state.mode in
4412 let birdseye = isbirdseye state.mode in
4414 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4416 src#bool "presentation mode"
4417 (fun () -> conf.presentation)
4418 (fun v -> setpresentationmode v);
4420 src#bool "ignore case in searches"
4421 (fun () -> conf.icase)
4422 (fun v -> conf.icase <- v);
4424 src#bool "preload"
4425 (fun () -> conf.preload)
4426 (fun v -> conf.preload <- v);
4428 src#bool "highlight links"
4429 (fun () -> conf.hlinks)
4430 (fun v -> conf.hlinks <- v);
4432 src#bool "under info"
4433 (fun () -> conf.underinfo)
4434 (fun v -> conf.underinfo <- v);
4436 src#bool "persistent bookmarks"
4437 (fun () -> conf.savebmarks)
4438 (fun v -> conf.savebmarks <- v);
4440 src#fitmodel "fit model"
4441 (fun () -> fitmodel_to_string conf.fitmodel)
4442 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4444 src#bool "trim margins"
4445 (fun () -> conf.trimmargins)
4446 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4448 src#bool "persistent location"
4449 (fun () -> conf.jumpback)
4450 (fun v -> conf.jumpback <- v);
4452 sep ();
4453 src#int "inter-page space"
4454 (fun () -> conf.interpagespace)
4455 (fun n ->
4456 conf.interpagespace <- n;
4457 docolumns conf.columns;
4458 let pageno, py =
4459 match state.layout with
4460 | [] -> 0, 0
4461 | l :: _ ->
4462 l.pageno, l.pagey
4464 state.maxy <- calcheight ();
4465 let y = getpagey pageno in
4466 gotoy (y + py)
4469 src#int "page bias"
4470 (fun () -> conf.pagebias)
4471 (fun v -> conf.pagebias <- v);
4473 src#int "scroll step"
4474 (fun () -> conf.scrollstep)
4475 (fun n -> conf.scrollstep <- n);
4477 src#int "horizontal scroll step"
4478 (fun () -> conf.hscrollstep)
4479 (fun v -> conf.hscrollstep <- v);
4481 src#int "auto scroll step"
4482 (fun () ->
4483 match state.autoscroll with
4484 | Some step -> step
4485 | _ -> conf.autoscrollstep)
4486 (fun n ->
4487 if state.autoscroll <> None
4488 then state.autoscroll <- Some n;
4489 conf.autoscrollstep <- n);
4491 src#int "zoom"
4492 (fun () -> truncate (conf.zoom *. 100.))
4493 (fun v -> setzoom ((float v) /. 100.));
4495 src#int "rotation"
4496 (fun () -> conf.angle)
4497 (fun v -> reqlayout v conf.fitmodel);
4499 src#int "scroll bar width"
4500 (fun () -> state.scrollw)
4501 (fun v ->
4502 state.scrollw <- v;
4503 conf.scrollbw <- v;
4504 reshape state.winw state.winh;
4507 src#int "scroll handle height"
4508 (fun () -> conf.scrollh)
4509 (fun v -> conf.scrollh <- v;);
4511 src#int "thumbnail width"
4512 (fun () -> conf.thumbw)
4513 (fun v ->
4514 conf.thumbw <- min 4096 v;
4515 match oldmode with
4516 | Birdseye beye ->
4517 leavebirdseye beye false;
4518 enterbirdseye ()
4519 | _ -> ()
4522 let mode = state.mode in
4523 src#string "columns"
4524 (fun () ->
4525 match conf.columns with
4526 | Csingle _ -> "1"
4527 | Cmulti (multi, _) -> multicolumns_to_string multi
4528 | Csplit (count, _) -> "-" ^ string_of_int count
4530 (fun v ->
4531 let n, a, b = multicolumns_of_string v in
4532 setcolumns mode n a b);
4534 sep ();
4535 src#caption "Presentation mode" 0;
4536 src#bool "scrollbar visible"
4537 (fun () -> conf.scrollbarinpm)
4538 (fun v ->
4539 if v != conf.scrollbarinpm
4540 then (
4541 conf.scrollbarinpm <- v;
4542 if conf.presentation
4543 then (
4544 state.scrollw <- if v then conf.scrollbw else 0;
4545 reshape state.winw state.winh;
4550 sep ();
4551 src#caption "Pixmap cache" 0;
4552 src#int_with_suffix "size (advisory)"
4553 (fun () -> conf.memlimit)
4554 (fun v -> conf.memlimit <- v);
4556 src#caption2 "used"
4557 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4558 (string_with_suffix_of_int state.memused)
4559 (Hashtbl.length state.tilemap)) 1;
4561 sep ();
4562 src#caption "Layout" 0;
4563 src#caption2 "Dimension"
4564 (fun () ->
4565 Printf.sprintf "%dx%d (virtual %dx%d)"
4566 state.winw state.winh
4567 state.w state.maxy)
4569 if conf.debug
4570 then
4571 src#caption2 "Position" (fun () ->
4572 Printf.sprintf "%dx%d" state.x state.y
4574 else
4575 src#caption2 "Position" (fun () -> describe_location ()) 1
4578 sep ();
4579 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4580 "Save these parameters as global defaults at exit"
4581 (fun () -> conf.bedefault)
4582 (fun v -> conf.bedefault <- v)
4585 sep ();
4586 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4587 src#bool ~offset:0 ~btos "Extended parameters"
4588 (fun () -> !showextended)
4589 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4590 if !showextended
4591 then (
4592 src#bool "checkers"
4593 (fun () -> conf.checkers)
4594 (fun v -> conf.checkers <- v; setcheckers v);
4595 src#bool "update cursor"
4596 (fun () -> conf.updatecurs)
4597 (fun v -> conf.updatecurs <- v);
4598 src#bool "verbose"
4599 (fun () -> conf.verbose)
4600 (fun v -> conf.verbose <- v);
4601 src#bool "invert colors"
4602 (fun () -> conf.invert)
4603 (fun v -> conf.invert <- v);
4604 src#bool "max fit"
4605 (fun () -> conf.maxhfit)
4606 (fun v -> conf.maxhfit <- v);
4607 src#bool "redirect stderr"
4608 (fun () -> conf.redirectstderr)
4609 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4610 src#string "uri launcher"
4611 (fun () -> conf.urilauncher)
4612 (fun v -> conf.urilauncher <- v);
4613 src#string "path launcher"
4614 (fun () -> conf.pathlauncher)
4615 (fun v -> conf.pathlauncher <- v);
4616 src#string "tile size"
4617 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4618 (fun v ->
4620 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4621 conf.tilew <- max 64 w;
4622 conf.tileh <- max 64 h;
4623 flushtiles ();
4624 with exn ->
4625 state.text <- Printf.sprintf "bad tile size `%s': %s"
4626 v (exntos exn)
4628 src#int "texture count"
4629 (fun () -> conf.texcount)
4630 (fun v ->
4631 if realloctexts v
4632 then conf.texcount <- v
4633 else showtext '!' " Failed to set texture count please retry later"
4635 src#int "slice height"
4636 (fun () -> conf.sliceheight)
4637 (fun v ->
4638 conf.sliceheight <- v;
4639 wcmd "sliceh %d" conf.sliceheight;
4641 src#int "anti-aliasing level"
4642 (fun () -> conf.aalevel)
4643 (fun v ->
4644 conf.aalevel <- bound v 0 8;
4645 state.anchor <- getanchor ();
4646 opendoc state.path state.password;
4648 src#string "page scroll scaling factor"
4649 (fun () -> string_of_float conf.pgscale)
4650 (fun v ->
4652 let s = float_of_string v in
4653 conf.pgscale <- s
4654 with exn ->
4655 state.text <- Printf.sprintf
4656 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4659 src#int "ui font size"
4660 (fun () -> fstate.fontsize)
4661 (fun v -> setfontsize (bound v 5 100));
4662 src#int "hint font size"
4663 (fun () -> conf.hfsize)
4664 (fun v -> conf.hfsize <- bound v 5 100);
4665 colorp "background color"
4666 (fun () -> conf.bgcolor)
4667 (fun v -> conf.bgcolor <- v);
4668 src#bool "crop hack"
4669 (fun () -> conf.crophack)
4670 (fun v -> conf.crophack <- v);
4671 src#string "trim fuzz"
4672 (fun () -> irect_to_string conf.trimfuzz)
4673 (fun v ->
4675 conf.trimfuzz <- irect_of_string v;
4676 if conf.trimmargins
4677 then settrim true conf.trimfuzz;
4678 with exn ->
4679 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4681 src#string "throttle"
4682 (fun () ->
4683 match conf.maxwait with
4684 | None -> "show place holder if page is not ready"
4685 | Some time ->
4686 if time = infinity
4687 then "wait for page to fully render"
4688 else
4689 "wait " ^ string_of_float time
4690 ^ " seconds before showing placeholder"
4692 (fun v ->
4694 let f = float_of_string v in
4695 if f <= 0.0
4696 then conf.maxwait <- None
4697 else conf.maxwait <- Some f
4698 with exn ->
4699 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4701 src#string "ghyll scroll"
4702 (fun () ->
4703 match conf.ghyllscroll with
4704 | None -> ""
4705 | Some nab -> ghyllscroll_to_string nab
4707 (fun v ->
4709 let gs =
4710 if String.length v = 0
4711 then None
4712 else Some (ghyllscroll_of_string v)
4714 conf.ghyllscroll <- gs
4715 with exn ->
4716 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4718 src#string "selection command"
4719 (fun () -> conf.selcmd)
4720 (fun v -> conf.selcmd <- v);
4721 src#string "synctex command"
4722 (fun () -> conf.stcmd)
4723 (fun v -> conf.stcmd <- v);
4724 src#colorspace "color space"
4725 (fun () -> colorspace_to_string conf.colorspace)
4726 (fun v ->
4727 conf.colorspace <- colorspace_of_int v;
4728 wcmd "cs %d" v;
4729 load state.layout;
4731 if pbousable ()
4732 then
4733 src#bool "use PBO"
4734 (fun () -> conf.usepbo)
4735 (fun v -> conf.usepbo <- v);
4736 src#bool "mouse wheel scrolls pages"
4737 (fun () -> conf.wheelbypage)
4738 (fun v -> conf.wheelbypage <- v);
4741 sep ();
4742 src#caption "Document" 0;
4743 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4744 src#caption2 "Pages"
4745 (fun () -> string_of_int state.pagecount) 1;
4746 src#caption2 "Dimensions"
4747 (fun () -> string_of_int (List.length state.pdims)) 1;
4748 if conf.trimmargins
4749 then (
4750 sep ();
4751 src#caption "Trimmed margins" 0;
4752 src#caption2 "Dimensions"
4753 (fun () -> string_of_int (List.length state.pdims)) 1;
4756 sep ();
4757 src#caption "OpenGL" 0;
4758 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4759 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4761 sep ();
4762 src#caption "Location" 0;
4763 if String.length state.origin > 0
4764 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4765 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4767 src#reset prevmode prevuioh;
4769 fun () ->
4770 state.text <- "";
4771 let prevmode = state.mode
4772 and prevuioh = state.uioh in
4773 fillsrc prevmode prevuioh;
4774 let source = (src :> lvsource) in
4775 let modehash = findkeyhash conf "info" in
4776 state.uioh <- coe (object (self)
4777 inherit listview ~source ~trusted:true ~modehash as super
4778 val mutable m_prevmemused = 0
4779 method infochanged = function
4780 | Memused ->
4781 if m_prevmemused != state.memused
4782 then (
4783 m_prevmemused <- state.memused;
4784 G.postRedisplay "memusedchanged";
4786 | Pdim -> G.postRedisplay "pdimchanged"
4787 | Docinfo -> fillsrc prevmode prevuioh
4789 method key key mask =
4790 if not (Wsi.withctrl mask)
4791 then
4792 match key with
4793 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4794 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4795 | _ -> super#key key mask
4796 else super#key key mask
4797 end);
4798 G.postRedisplay "info";
4801 let enterhelpmode =
4802 let source =
4803 (object
4804 inherit lvsourcebase
4805 method getitemcount = Array.length state.help
4806 method getitem n =
4807 let s, l, _ = state.help.(n) in
4808 (s, l)
4810 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4811 let optuioh =
4812 if not cancel
4813 then (
4814 m_qsearch <- qsearch;
4815 match state.help.(active) with
4816 | _, _, Action f -> Some (f uioh)
4817 | _ -> Some (uioh)
4819 else None
4821 m_active <- active;
4822 m_first <- first;
4823 m_pan <- pan;
4824 optuioh
4826 method hasaction n =
4827 match state.help.(n) with
4828 | _, _, Action _ -> true
4829 | _ -> false
4831 initializer
4832 m_active <- -1
4833 end)
4834 in fun () ->
4835 let modehash = findkeyhash conf "help" in
4836 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4837 G.postRedisplay "help";
4840 let entermsgsmode =
4841 let msgsource =
4842 let re = Str.regexp "[\r\n]" in
4843 (object
4844 inherit lvsourcebase
4845 val mutable m_items = [||]
4847 method getitemcount = 1 + Array.length m_items
4849 method getitem n =
4850 if n = 0
4851 then "[Clear]", 0
4852 else m_items.(n-1), 0
4854 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4855 ignore uioh;
4856 if not cancel
4857 then (
4858 if active = 0
4859 then Buffer.clear state.errmsgs;
4860 m_qsearch <- qsearch;
4862 m_active <- active;
4863 m_first <- first;
4864 m_pan <- pan;
4865 None
4867 method hasaction n =
4868 n = 0
4870 method reset =
4871 state.newerrmsgs <- false;
4872 let l = Str.split re (Buffer.contents state.errmsgs) in
4873 m_items <- Array.of_list l
4875 initializer
4876 m_active <- 0
4877 end)
4878 in fun () ->
4879 state.text <- "";
4880 msgsource#reset;
4881 let source = (msgsource :> lvsource) in
4882 let modehash = findkeyhash conf "listview" in
4883 state.uioh <- coe (object
4884 inherit listview ~source ~trusted:false ~modehash as super
4885 method display =
4886 if state.newerrmsgs
4887 then msgsource#reset;
4888 super#display
4889 end);
4890 G.postRedisplay "msgs";
4893 let quickbookmark ?title () =
4894 match state.layout with
4895 | [] -> ()
4896 | l :: _ ->
4897 let title =
4898 match title with
4899 | None ->
4900 let sec = Unix.gettimeofday () in
4901 let tm = Unix.localtime sec in
4902 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4903 (l.pageno+1)
4904 tm.Unix.tm_mday
4905 tm.Unix.tm_mon
4906 (tm.Unix.tm_year + 1900)
4907 tm.Unix.tm_hour
4908 tm.Unix.tm_min
4909 | Some title -> title
4911 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4914 let setautoscrollspeed step goingdown =
4915 let incr = max 1 ((abs step) / 2) in
4916 let incr = if goingdown then incr else -incr in
4917 let astep = step + incr in
4918 state.autoscroll <- Some astep;
4921 let gotounder = function
4922 | Ulinkgoto (pageno, top) ->
4923 if pageno >= 0
4924 then (
4925 addnav ();
4926 gotopage1 pageno top;
4929 | Ulinkuri s ->
4930 gotouri s
4932 | Uremote (filename, pageno) ->
4933 let path =
4934 if Sys.file_exists filename
4935 then filename
4936 else
4937 let dir = Filename.dirname state.path in
4938 let path = Filename.concat dir filename in
4939 if Sys.file_exists path
4940 then path
4941 else ""
4943 if String.length path > 0
4944 then (
4945 let anchor = getanchor () in
4946 let ranchor = state.path, state.password, anchor, state.origin in
4947 state.origin <- "";
4948 state.anchor <- (pageno, 0.0, 0.0);
4949 state.ranchors <- ranchor :: state.ranchors;
4950 opendoc path "";
4952 else showtext '!' ("Could not find " ^ filename)
4954 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4957 let canpan () =
4958 match conf.columns with
4959 | Csplit _ -> true
4960 | _ -> state.x != 0 || conf.zoom > 1.0
4963 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4965 let existsinrow pageno (columns, coverA, coverB) p =
4966 let last = ((pageno - coverA) mod columns) + columns in
4967 let rec any = function
4968 | [] -> false
4969 | l :: rest ->
4970 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4971 then p l
4972 else (
4973 if not (p l)
4974 then (if l.pageno = last then false else any rest)
4975 else true
4978 any state.layout
4981 let nextpage () =
4982 match state.layout with
4983 | [] ->
4984 let pageno = page_of_y state.y in
4985 gotoghyll (getpagey (pageno+1))
4986 | l :: rest ->
4987 match conf.columns with
4988 | Csingle _ ->
4989 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4990 then
4991 let y = clamp (pgscale state.winh) in
4992 gotoghyll y
4993 else
4994 let pageno = min (l.pageno+1) (state.pagecount-1) in
4995 gotoghyll (getpagey pageno)
4996 | Cmulti ((c, _, _) as cl, _) ->
4997 if conf.presentation
4998 && (existsinrow l.pageno cl
4999 (fun l -> l.pageh > l.pagey + l.pagevh))
5000 then
5001 let y = clamp (pgscale state.winh) in
5002 gotoghyll y
5003 else
5004 let pageno = min (l.pageno+c) (state.pagecount-1) in
5005 gotoghyll (getpagey pageno)
5006 | Csplit (n, _) ->
5007 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5008 then
5009 let pagey, pageh = getpageyh l.pageno in
5010 let pagey = pagey + pageh * l.pagecol in
5011 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5012 gotoghyll (pagey + pageh + ips)
5015 let prevpage () =
5016 match state.layout with
5017 | [] ->
5018 let pageno = page_of_y state.y in
5019 gotoghyll (getpagey (pageno-1))
5020 | l :: _ ->
5021 match conf.columns with
5022 | Csingle _ ->
5023 if conf.presentation && l.pagey != 0
5024 then
5025 gotoghyll (clamp (pgscale ~-(state.winh)))
5026 else
5027 let pageno = max 0 (l.pageno-1) in
5028 gotoghyll (getpagey pageno)
5029 | Cmulti ((c, _, coverB) as cl, _) ->
5030 if conf.presentation &&
5031 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5032 then
5033 gotoghyll (clamp (pgscale ~-(state.winh)))
5034 else
5035 let decr =
5036 if l.pageno = state.pagecount - coverB
5037 then 1
5038 else c
5040 let pageno = max 0 (l.pageno-decr) in
5041 gotoghyll (getpagey pageno)
5042 | Csplit (n, _) ->
5043 let y =
5044 if l.pagecol = 0
5045 then
5046 if l.pageno = 0
5047 then l.pagey
5048 else
5049 let pageno = max 0 (l.pageno-1) in
5050 let pagey, pageh = getpageyh pageno in
5051 pagey + (n-1)*pageh
5052 else
5053 let pagey, pageh = getpageyh l.pageno in
5054 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5056 gotoghyll y
5059 let viewkeyboard key mask =
5060 let enttext te =
5061 let mode = state.mode in
5062 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5063 state.text <- "";
5064 enttext ();
5065 G.postRedisplay "view:enttext"
5067 let ctrl = Wsi.withctrl mask in
5068 let key =
5069 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5071 match key with
5072 | 81 -> (* Q *)
5073 exit 0
5075 | 0xff63 -> (* insert *)
5076 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5077 then (
5078 state.mode <- LinkNav (Ltgendir 0);
5079 gotoy state.y;
5081 else showtext '!' "Keyboard link navigation does not work under rotation"
5083 | 0xff1b | 113 -> (* escape / q *)
5084 begin match state.mstate with
5085 | Mzoomrect _ ->
5086 state.mstate <- Mnone;
5087 Wsi.setcursor Wsi.CURSOR_INHERIT;
5088 G.postRedisplay "kill zoom rect";
5089 | _ ->
5090 begin match state.mode with
5091 | LinkNav _ ->
5092 state.mode <- View;
5093 G.postRedisplay "esc leave linknav"
5094 | _ ->
5095 match state.ranchors with
5096 | [] -> raise Quit
5097 | (path, password, anchor, origin) :: rest ->
5098 state.ranchors <- rest;
5099 state.anchor <- anchor;
5100 state.origin <- origin;
5101 opendoc path password
5102 end;
5103 end;
5105 | 0xff08 -> (* backspace *)
5106 gotoghyll (getnav ~-1)
5108 | 111 -> (* o *)
5109 enteroutlinemode ()
5111 | 117 -> (* u *)
5112 state.rects <- [];
5113 state.text <- "";
5114 G.postRedisplay "dehighlight";
5116 | 47 | 63 -> (* / ? *)
5117 let ondone isforw s =
5118 cbput state.hists.pat s;
5119 state.searchpattern <- s;
5120 search s isforw
5122 let s = String.create 1 in
5123 s.[0] <- Char.chr key;
5124 enttext (s, "", Some (onhist state.hists.pat),
5125 textentry, ondone (key = 47), true)
5127 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5128 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5129 setzoom (conf.zoom +. incr)
5131 | 43 | 0xffab -> (* + *)
5132 let ondone s =
5133 let n =
5134 try int_of_string s with exc ->
5135 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5136 max_int
5138 if n != max_int
5139 then (
5140 conf.pagebias <- n;
5141 state.text <- "page bias is now " ^ string_of_int n;
5144 enttext ("page bias: ", "", None, intentry, ondone, true)
5146 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5147 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5148 setzoom (max 0.01 (conf.zoom -. decr))
5150 | 45 | 0xffad -> (* - *)
5151 let ondone msg = state.text <- msg in
5152 enttext (
5153 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5154 optentry state.mode, ondone, true
5157 | 48 when ctrl -> (* ctrl-0 *)
5158 if conf.zoom = 1.0
5159 then (
5160 state.x <- 0;
5161 state.hscrollh <-
5162 if state.w <= state.winw - state.scrollw
5163 then 0
5164 else state.scrollw
5166 gotoy state.y
5168 else setzoom 1.0
5170 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5171 let cols =
5172 match conf.columns with
5173 | Csingle _ | Cmulti _ -> 1
5174 | Csplit (n, _) -> n
5176 let h = state.winh -
5177 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5179 let zoom = zoomforh state.winw h state.scrollw cols in
5180 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5181 then setzoom zoom
5183 | 51 when ctrl -> (* ctrl-3 *)
5184 let fm =
5185 match conf.fitmodel with
5186 | FitWidth -> FitProportional
5187 | FitProportional -> FitPage
5188 | FitPage -> FitWidth
5190 state.text <- "fit model: " ^ fitmodel_to_string fm;
5191 reqlayout conf.angle fm
5193 | 0xffc6 -> (* f9 *)
5194 togglebirdseye ()
5196 | 57 when ctrl -> (* ctrl-9 *)
5197 togglebirdseye ()
5199 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5200 when not ctrl -> (* 0..9 *)
5201 let ondone s =
5202 let n =
5203 try int_of_string s with exc ->
5204 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5207 if n >= 0
5208 then (
5209 addnav ();
5210 cbput state.hists.pag (string_of_int n);
5211 gotopage1 (n + conf.pagebias - 1) 0;
5214 let pageentry text key =
5215 match Char.unsafe_chr key with
5216 | 'g' -> TEdone text
5217 | _ -> intentry text key
5219 let text = "x" in text.[0] <- Char.chr key;
5220 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5222 | 98 -> (* b *)
5223 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5224 reshape state.winw state.winh;
5226 | 108 -> (* l *)
5227 conf.hlinks <- not conf.hlinks;
5228 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5229 G.postRedisplay "toggle highlightlinks";
5231 | 70 -> (* F *)
5232 state.glinks <- true;
5233 let mode = state.mode in
5234 state.mode <- Textentry (
5235 (":", "", None, linknentry, linkndone gotounder, false),
5236 (fun _ ->
5237 state.glinks <- false;
5238 state.mode <- mode)
5240 state.text <- "";
5241 G.postRedisplay "view:linkent(F)"
5243 | 121 -> (* y *)
5244 state.glinks <- true;
5245 let mode = state.mode in
5246 state.mode <- Textentry (
5248 ":", "", None, linknentry, linkndone (fun under ->
5249 selstring (undertext under);
5250 ), false
5252 fun _ ->
5253 state.glinks <- false;
5254 state.mode <- mode
5256 state.text <- "";
5257 G.postRedisplay "view:linkent"
5259 | 97 -> (* a *)
5260 begin match state.autoscroll with
5261 | Some step ->
5262 conf.autoscrollstep <- step;
5263 state.autoscroll <- None
5264 | None ->
5265 if conf.autoscrollstep = 0
5266 then state.autoscroll <- Some 1
5267 else state.autoscroll <- Some conf.autoscrollstep
5270 | 112 when ctrl -> (* ctrl-p *)
5271 launchpath ()
5273 | 80 -> (* P *)
5274 setpresentationmode (not conf.presentation);
5275 showtext ' ' ("presentation mode " ^
5276 if conf.presentation then "on" else "off");
5278 | 102 -> (* f *)
5279 if List.mem Wsi.Fullscreen state.winstate
5280 then Wsi.reshape conf.cwinw conf.cwinh
5281 else Wsi.fullscreen ()
5283 | 112 | 78 -> (* p|N *)
5284 search state.searchpattern false
5286 | 110 | 0xffc0 -> (* n|F3 *)
5287 search state.searchpattern true
5289 | 116 -> (* t *)
5290 begin match state.layout with
5291 | [] -> ()
5292 | l :: _ ->
5293 gotoghyll (getpagey l.pageno)
5296 | 32 -> (* space *)
5297 nextpage ()
5299 | 0xff9f | 0xffff -> (* delete *)
5300 prevpage ()
5302 | 61 -> (* = *)
5303 showtext ' ' (describe_location ());
5305 | 119 -> (* w *)
5306 begin match state.layout with
5307 | [] -> ()
5308 | l :: _ ->
5309 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5310 G.postRedisplay "w"
5313 | 39 -> (* ' *)
5314 enterbookmarkmode ()
5316 | 104 | 0xffbe -> (* h|F1 *)
5317 enterhelpmode ()
5319 | 105 -> (* i *)
5320 enterinfomode ()
5322 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5323 entermsgsmode ()
5325 | 109 -> (* m *)
5326 let ondone s =
5327 match state.layout with
5328 | l :: _ ->
5329 if String.length s > 0
5330 then
5331 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5332 | _ -> ()
5334 enttext ("bookmark: ", "", None, textentry, ondone, true)
5336 | 126 -> (* ~ *)
5337 quickbookmark ();
5338 showtext ' ' "Quick bookmark added";
5340 | 122 -> (* z *)
5341 begin match state.layout with
5342 | l :: _ ->
5343 let rect = getpdimrect l.pagedimno in
5344 let w, h =
5345 if conf.crophack
5346 then
5347 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5348 truncate (1.2 *. (rect.(3) -. rect.(0))))
5349 else
5350 (truncate (rect.(1) -. rect.(0)),
5351 truncate (rect.(3) -. rect.(0)))
5353 let w = truncate ((float w)*.conf.zoom)
5354 and h = truncate ((float h)*.conf.zoom) in
5355 if w != 0 && h != 0
5356 then (
5357 state.anchor <- getanchor ();
5358 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5360 G.postRedisplay "z";
5362 | [] -> ()
5365 | 60 | 62 -> (* < > *)
5366 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5368 | 91 | 93 -> (* [ ] *)
5369 conf.colorscale <-
5370 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5372 G.postRedisplay "brightness";
5374 | 99 when state.mode = View -> (* [alt]-c *)
5375 if Wsi.withalt mask
5376 then (
5377 if conf.zoom > 1.0
5378 then
5379 let m = (state.winw - state.w - state.scrollw) / 2 in
5380 state.x <- m;
5381 gotoy_and_clear_text state.y
5383 else
5384 let (c, a, b), z =
5385 match state.prevcolumns with
5386 | None -> (1, 0, 0), 1.0
5387 | Some (columns, z) ->
5388 let cab =
5389 match columns with
5390 | Csplit (c, _) -> -c, 0, 0
5391 | Cmulti ((c, a, b), _) -> c, a, b
5392 | Csingle _ -> 1, 0, 0
5394 cab, z
5396 setcolumns View c a b;
5397 setzoom z
5399 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5400 setzoom state.prevzoom
5402 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5403 begin match state.autoscroll with
5404 | None ->
5405 begin match state.mode with
5406 | Birdseye beye -> upbirdseye 1 beye
5407 | _ ->
5408 if ctrl
5409 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5410 else (
5411 if not (Wsi.withshift mask) && conf.presentation
5412 then prevpage ()
5413 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5416 | Some n ->
5417 setautoscrollspeed n false
5420 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5421 begin match state.autoscroll with
5422 | None ->
5423 begin match state.mode with
5424 | Birdseye beye -> downbirdseye 1 beye
5425 | _ ->
5426 if ctrl
5427 then gotoy_and_clear_text (clamp (state.winh/2))
5428 else (
5429 if not (Wsi.withshift mask) && conf.presentation
5430 then nextpage ()
5431 else gotoy_and_clear_text (clamp conf.scrollstep)
5434 | Some n ->
5435 setautoscrollspeed n true
5438 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5439 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5440 if canpan ()
5441 then
5442 let dx =
5443 if ctrl
5444 then state.winw / 2
5445 else conf.hscrollstep
5447 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5448 state.x <- panbound (state.x + dx);
5449 gotoy_and_clear_text state.y
5450 else (
5451 state.text <- "";
5452 G.postRedisplay "left/right"
5455 | 0xff55 | 0xff9a -> (* (kp) prior *)
5456 let y =
5457 if ctrl
5458 then
5459 match state.layout with
5460 | [] -> state.y
5461 | l :: _ -> state.y - l.pagey
5462 else
5463 clamp (pgscale (-state.winh))
5465 gotoghyll y
5467 | 0xff56 | 0xff9b -> (* (kp) next *)
5468 let y =
5469 if ctrl
5470 then
5471 match List.rev state.layout with
5472 | [] -> state.y
5473 | l :: _ -> getpagey l.pageno
5474 else
5475 clamp (pgscale state.winh)
5477 gotoghyll y
5479 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5480 gotoghyll 0
5481 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5482 gotoghyll (clamp state.maxy)
5484 | 0xff53 | 0xff98
5485 when Wsi.withalt mask -> (* alt-(kp) right *)
5486 gotoghyll (getnav 1)
5487 | 0xff51 | 0xff96
5488 when Wsi.withalt mask -> (* alt-(kp) left *)
5489 gotoghyll (getnav ~-1)
5491 | 114 -> (* r *)
5492 reload ()
5494 | 118 when conf.debug -> (* v *)
5495 state.rects <- [];
5496 List.iter (fun l ->
5497 match getopaque l.pageno with
5498 | None -> ()
5499 | Some opaque ->
5500 let x0, y0, x1, y1 = pagebbox opaque in
5501 let a,b = float x0, float y0 in
5502 let c,d = float x1, float y0 in
5503 let e,f = float x1, float y1 in
5504 let h,j = float x0, float y1 in
5505 let rect = (a,b,c,d,e,f,h,j) in
5506 debugrect rect;
5507 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5508 ) state.layout;
5509 G.postRedisplay "v";
5511 | _ ->
5512 vlog "huh? %s" (Wsi.keyname key)
5515 let linknavkeyboard key mask linknav =
5516 let getpage pageno =
5517 let rec loop = function
5518 | [] -> None
5519 | l :: _ when l.pageno = pageno -> Some l
5520 | _ :: rest -> loop rest
5521 in loop state.layout
5523 let doexact (pageno, n) =
5524 match getopaque pageno, getpage pageno with
5525 | Some opaque, Some l ->
5526 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5527 then
5528 let under = getlink opaque n in
5529 G.postRedisplay "link gotounder";
5530 gotounder under;
5531 state.mode <- View;
5532 else
5533 let opt, dir =
5534 match key with
5535 | 0xff50 -> (* home *)
5536 Some (findlink opaque LDfirst), -1
5538 | 0xff57 -> (* end *)
5539 Some (findlink opaque LDlast), 1
5541 | 0xff51 -> (* left *)
5542 Some (findlink opaque (LDleft n)), -1
5544 | 0xff53 -> (* right *)
5545 Some (findlink opaque (LDright n)), 1
5547 | 0xff52 -> (* up *)
5548 Some (findlink opaque (LDup n)), -1
5550 | 0xff54 -> (* down *)
5551 Some (findlink opaque (LDdown n)), 1
5553 | _ -> None, 0
5555 let pwl l dir =
5556 begin match findpwl l.pageno dir with
5557 | Pwlnotfound -> ()
5558 | Pwl pageno ->
5559 let notfound dir =
5560 state.mode <- LinkNav (Ltgendir dir);
5561 let y, h = getpageyh pageno in
5562 let y =
5563 if dir < 0
5564 then y + h - state.winh
5565 else y
5567 gotoy y
5569 begin match getopaque pageno, getpage pageno with
5570 | Some opaque, Some _ ->
5571 let link =
5572 let ld = if dir > 0 then LDfirst else LDlast in
5573 findlink opaque ld
5575 begin match link with
5576 | Lfound m ->
5577 showlinktype (getlink opaque m);
5578 state.mode <- LinkNav (Ltexact (pageno, m));
5579 G.postRedisplay "linknav jpage";
5580 | _ -> notfound dir
5581 end;
5582 | _ -> notfound dir
5583 end;
5584 end;
5586 begin match opt with
5587 | Some Lnotfound -> pwl l dir;
5588 | Some (Lfound m) ->
5589 if m = n
5590 then pwl l dir
5591 else (
5592 let _, y0, _, y1 = getlinkrect opaque m in
5593 if y0 < l.pagey
5594 then gotopage1 l.pageno y0
5595 else (
5596 let d = fstate.fontsize + 1 in
5597 if y1 - l.pagey > l.pagevh - d
5598 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5599 else G.postRedisplay "linknav";
5601 showlinktype (getlink opaque m);
5602 state.mode <- LinkNav (Ltexact (l.pageno, m));
5605 | None -> viewkeyboard key mask
5606 end;
5607 | _ -> viewkeyboard key mask
5609 if key = 0xff63
5610 then (
5611 state.mode <- View;
5612 G.postRedisplay "leave linknav"
5614 else
5615 match linknav with
5616 | Ltgendir _ -> viewkeyboard key mask
5617 | Ltexact exact -> doexact exact
5620 let keyboard key mask =
5621 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5622 then wcmd "interrupt"
5623 else state.uioh <- state.uioh#key key mask
5626 let birdseyekeyboard key mask
5627 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5628 let incr =
5629 match conf.columns with
5630 | Csingle _ -> 1
5631 | Cmulti ((c, _, _), _) -> c
5632 | Csplit _ -> failwith "bird's eye split mode"
5634 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5635 match key with
5636 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5637 let y, h = getpageyh pageno in
5638 let top = (state.winh - h) / 2 in
5639 gotoy (max 0 (y - top))
5640 | 0xff0d (* enter *)
5641 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5642 | 0xff1b -> leavebirdseye beye true (* escape *)
5643 | 0xff52 -> upbirdseye incr beye (* up *)
5644 | 0xff54 -> downbirdseye incr beye (* down *)
5645 | 0xff51 -> upbirdseye 1 beye (* left *)
5646 | 0xff53 -> downbirdseye 1 beye (* right *)
5648 | 0xff55 -> (* prior *)
5649 begin match state.layout with
5650 | l :: _ ->
5651 if l.pagey != 0
5652 then (
5653 state.mode <- Birdseye (
5654 oconf, leftx, l.pageno, hooverpageno, anchor
5656 gotopage1 l.pageno 0;
5658 else (
5659 let layout = layout (state.y-state.winh) (pgh state.layout) in
5660 match layout with
5661 | [] -> gotoy (clamp (-state.winh))
5662 | l :: _ ->
5663 state.mode <- Birdseye (
5664 oconf, leftx, l.pageno, hooverpageno, anchor
5666 gotopage1 l.pageno 0
5669 | [] -> gotoy (clamp (-state.winh))
5670 end;
5672 | 0xff56 -> (* next *)
5673 begin match List.rev state.layout with
5674 | l :: _ ->
5675 let layout = layout (state.y + (pgh state.layout)) state.winh in
5676 begin match layout with
5677 | [] ->
5678 let incr = l.pageh - l.pagevh in
5679 if incr = 0
5680 then (
5681 state.mode <-
5682 Birdseye (
5683 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5685 G.postRedisplay "birdseye pagedown";
5687 else gotoy (clamp (incr + conf.interpagespace*2));
5689 | l :: _ ->
5690 state.mode <-
5691 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5692 gotopage1 l.pageno 0;
5695 | [] -> gotoy (clamp state.winh)
5696 end;
5698 | 0xff50 -> (* home *)
5699 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5700 gotopage1 0 0
5702 | 0xff57 -> (* end *)
5703 let pageno = state.pagecount - 1 in
5704 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5705 if not (pagevisible state.layout pageno)
5706 then
5707 let h =
5708 match List.rev state.pdims with
5709 | [] -> state.winh
5710 | (_, _, h, _) :: _ -> h
5712 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5713 else G.postRedisplay "birdseye end";
5714 | _ -> viewkeyboard key mask
5717 let drawpage l =
5718 let color =
5719 match state.mode with
5720 | Textentry _ -> scalecolor 0.4
5721 | LinkNav _
5722 | View -> scalecolor 1.0
5723 | Birdseye (_, _, pageno, hooverpageno, _) ->
5724 if l.pageno = hooverpageno
5725 then scalecolor 0.9
5726 else (
5727 if l.pageno = pageno
5728 then scalecolor 1.0
5729 else scalecolor 0.8
5732 drawtiles l color;
5735 let postdrawpage l linkindexbase =
5736 match getopaque l.pageno with
5737 | Some opaque ->
5738 if tileready l l.pagex l.pagey
5739 then
5740 let x = l.pagedispx - l.pagex
5741 and y = l.pagedispy - l.pagey in
5742 let hlmask =
5743 match conf.columns with
5744 | Csingle _ | Cmulti _ ->
5745 (if conf.hlinks then 1 else 0)
5746 + (if state.glinks
5747 && not (isbirdseye state.mode) then 2 else 0)
5748 | _ -> 0
5750 let s =
5751 match state.mode with
5752 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5753 | _ -> ""
5755 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5756 else 0
5757 | _ -> 0
5760 let scrollindicator () =
5761 let sbw, ph, sh = state.uioh#scrollph in
5762 let sbh, pw, sw = state.uioh#scrollpw in
5764 GlDraw.color (0.64, 0.64, 0.64);
5765 GlDraw.rect
5766 (float (state.winw - sbw), 0.)
5767 (float state.winw, float state.winh)
5769 GlDraw.rect
5770 (0., float (state.winh - sbh))
5771 (float (state.winw - state.scrollw - 1), float state.winh)
5773 GlDraw.color (0.0, 0.0, 0.0);
5775 GlDraw.rect
5776 (float (state.winw - sbw), ph)
5777 (float state.winw, ph +. sh)
5779 GlDraw.rect
5780 (pw, float (state.winh - sbh))
5781 (pw +. sw, float state.winh)
5785 let showsel () =
5786 match state.mstate with
5787 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5790 | Msel ((x0, y0), (x1, y1)) ->
5791 let rec loop = function
5792 | l :: ls ->
5793 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5794 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5795 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5796 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5797 then
5798 match getopaque l.pageno with
5799 | Some opaque ->
5800 let x0, y0 = pagetranslatepoint l x0 y0 in
5801 let x1, y1 = pagetranslatepoint l x1 y1 in
5802 seltext opaque (x0, y0, x1, y1);
5803 | _ -> ()
5804 else loop ls
5805 | [] -> ()
5807 loop state.layout
5810 let showrects rects =
5811 Gl.enable `blend;
5812 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5813 GlDraw.polygon_mode `both `fill;
5814 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5815 List.iter
5816 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5817 List.iter (fun l ->
5818 if l.pageno = pageno
5819 then (
5820 let dx = float (l.pagedispx - l.pagex) in
5821 let dy = float (l.pagedispy - l.pagey) in
5822 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5823 GlDraw.begins `quads;
5825 GlDraw.vertex2 (x0+.dx, y0+.dy);
5826 GlDraw.vertex2 (x1+.dx, y1+.dy);
5827 GlDraw.vertex2 (x2+.dx, y2+.dy);
5828 GlDraw.vertex2 (x3+.dx, y3+.dy);
5830 GlDraw.ends ();
5832 ) state.layout
5833 ) rects
5835 Gl.disable `blend;
5838 let display () =
5839 GlClear.color (scalecolor2 conf.bgcolor);
5840 GlClear.clear [`color];
5841 List.iter drawpage state.layout;
5842 let rects =
5843 match state.mode with
5844 | LinkNav (Ltexact (pageno, linkno)) ->
5845 begin match getopaque pageno with
5846 | Some opaque ->
5847 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5848 (pageno, 5, (
5849 float x0, float y0,
5850 float x1, float y0,
5851 float x1, float y1,
5852 float x0, float y1)
5853 ) :: state.rects
5854 | None -> state.rects
5856 | _ -> state.rects
5858 showrects rects;
5859 let rec postloop linkindexbase = function
5860 | l :: rest ->
5861 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5862 postloop linkindexbase rest
5863 | [] -> ()
5865 showsel ();
5866 postloop 0 state.layout;
5867 state.uioh#display;
5868 begin match state.mstate with
5869 | Mzoomrect ((x0, y0), (x1, y1)) ->
5870 Gl.enable `blend;
5871 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5872 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5873 GlDraw.rect (float x0, float y0)
5874 (float x1, float y1);
5875 Gl.disable `blend;
5876 | _ -> ()
5877 end;
5878 enttext ();
5879 scrollindicator ();
5880 Wsi.swapb ();
5883 let zoomrect x y x1 y1 =
5884 let x0 = min x x1
5885 and x1 = max x x1
5886 and y0 = min y y1 in
5887 gotoy (state.y + y0);
5888 state.anchor <- getanchor ();
5889 let zoom = (float state.w) /. float (x1 - x0) in
5890 let margin =
5891 match conf.fitmodel, conf.columns with
5892 | FitPage, Csplit _ ->
5893 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5895 | _, _ ->
5896 if state.w < state.winw - state.scrollw
5897 then (state.winw - state.scrollw - state.w) / 2
5898 else 0
5900 state.x <- (state.x + margin) - x0;
5901 setzoom zoom;
5902 Wsi.setcursor Wsi.CURSOR_INHERIT;
5903 state.mstate <- Mnone;
5906 let scrollx x =
5907 let winw = state.winw - state.scrollw - 1 in
5908 let s = float x /. float winw in
5909 let destx = truncate (float (state.w + winw) *. s) in
5910 state.x <- winw - destx;
5911 gotoy_and_clear_text state.y;
5912 state.mstate <- Mscrollx;
5915 let scrolly y =
5916 let s = float y /. float state.winh in
5917 let desty = truncate (float (state.maxy - state.winh) *. s) in
5918 gotoy_and_clear_text desty;
5919 state.mstate <- Mscrolly;
5922 let viewmouse button down x y mask =
5923 match button with
5924 | n when (n == 4 || n == 5) && not down ->
5925 if Wsi.withctrl mask
5926 then (
5927 match state.mstate with
5928 | Mzoom (oldn, i) ->
5929 if oldn = n
5930 then (
5931 if i = 2
5932 then
5933 let incr =
5934 match n with
5935 | 5 ->
5936 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5937 | _ ->
5938 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5940 let zoom = conf.zoom -. incr in
5941 setzoom zoom;
5942 state.mstate <- Mzoom (n, 0);
5943 else
5944 state.mstate <- Mzoom (n, i+1);
5946 else state.mstate <- Mzoom (n, 0)
5948 | _ -> state.mstate <- Mzoom (n, 0)
5950 else (
5951 match state.autoscroll with
5952 | Some step -> setautoscrollspeed step (n=4)
5953 | None ->
5954 if conf.wheelbypage || conf.presentation
5955 then (
5956 if n = 4
5957 then prevpage ()
5958 else nextpage ()
5960 else
5961 let incr =
5962 if n = 4
5963 then -conf.scrollstep
5964 else conf.scrollstep
5966 let incr = incr * 2 in
5967 let y = clamp incr in
5968 gotoy_and_clear_text y
5971 | n when (n = 6 || n = 7) && not down && canpan () ->
5972 state.x <-
5973 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5974 gotoy_and_clear_text state.y
5976 | 1 when Wsi.withshift mask ->
5977 state.mstate <- Mnone;
5978 if not down
5979 then (
5980 match unproject x y with
5981 | Some (pageno, ux, uy) ->
5982 let cmd = Printf.sprintf
5983 "%s %s %d %d %d"
5984 conf.stcmd state.path pageno ux uy
5986 popen cmd []
5987 | None -> ()
5990 | 1 when Wsi.withctrl mask ->
5991 if down
5992 then (
5993 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5994 state.mstate <- Mpan (x, y)
5996 else
5997 state.mstate <- Mnone
5999 | 3 ->
6000 if down
6001 then (
6002 Wsi.setcursor Wsi.CURSOR_CYCLE;
6003 let p = (x, y) in
6004 state.mstate <- Mzoomrect (p, p)
6006 else (
6007 match state.mstate with
6008 | Mzoomrect ((x0, y0), _) ->
6009 if abs (x-x0) > 10 && abs (y - y0) > 10
6010 then zoomrect x0 y0 x y
6011 else (
6012 state.mstate <- Mnone;
6013 Wsi.setcursor Wsi.CURSOR_INHERIT;
6014 G.postRedisplay "kill accidental zoom rect";
6016 | _ ->
6017 Wsi.setcursor Wsi.CURSOR_INHERIT;
6018 state.mstate <- Mnone
6021 | 1 when x > state.winw - state.scrollw ->
6022 if down
6023 then
6024 let _, position, sh = state.uioh#scrollph in
6025 if y > truncate position && y < truncate (position +. sh)
6026 then state.mstate <- Mscrolly
6027 else scrolly y
6028 else
6029 state.mstate <- Mnone
6031 | 1 when y > state.winh - state.hscrollh ->
6032 if down
6033 then
6034 let _, position, sw = state.uioh#scrollpw in
6035 if x > truncate position && x < truncate (position +. sw)
6036 then state.mstate <- Mscrollx
6037 else scrollx x
6038 else
6039 state.mstate <- Mnone
6041 | 1 ->
6042 let dest = if down then getunder x y else Unone in
6043 begin match dest with
6044 | Ulinkgoto _
6045 | Ulinkuri _
6046 | Uremote _
6047 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6048 gotounder dest
6050 | Unone when down ->
6051 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6052 state.mstate <- Mpan (x, y);
6054 | Unone | Utext _ ->
6055 if down
6056 then (
6057 if conf.angle mod 360 = 0
6058 then (
6059 state.mstate <- Msel ((x, y), (x, y));
6060 G.postRedisplay "mouse select";
6063 else (
6064 match state.mstate with
6065 | Mnone -> ()
6067 | Mzoom _ | Mscrollx | Mscrolly ->
6068 state.mstate <- Mnone
6070 | Mzoomrect ((x0, y0), _) ->
6071 zoomrect x0 y0 x y
6073 | Mpan _ ->
6074 Wsi.setcursor Wsi.CURSOR_INHERIT;
6075 state.mstate <- Mnone
6077 | Msel ((x0, y0), (x1, y1)) ->
6078 let rec loop = function
6079 | [] -> ()
6080 | l :: rest ->
6081 let inside =
6082 let a0 = l.pagedispy in
6083 let a1 = a0 + l.pagevh in
6084 let b0 = l.pagedispx in
6085 let b1 = b0 + l.pagevw in
6086 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6087 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6089 if inside
6090 then
6091 match getopaque l.pageno with
6092 | Some opaque ->
6093 begin
6094 match Ne.pipe () with
6095 | Ne.Exn exn ->
6096 showtext '!'
6097 (Printf.sprintf
6098 "can not create sel pipe: %s"
6099 (exntos exn));
6100 | Ne.Res (r, w) ->
6101 let doclose what fd =
6102 Ne.clo fd (fun msg ->
6103 dolog "%s close failed: %s" what msg)
6106 popen conf.selcmd [r, 0; w, -1];
6107 copysel w opaque;
6108 doclose "pipe/r" r;
6109 G.postRedisplay "copysel";
6110 with exn ->
6111 dolog "can not execute %S: %s"
6112 conf.selcmd (exntos exn);
6113 doclose "pipe/r" r;
6114 doclose "pipe/w" w;
6116 | None -> ()
6117 else loop rest
6119 loop state.layout;
6120 Wsi.setcursor Wsi.CURSOR_INHERIT;
6121 state.mstate <- Mnone;
6125 | _ -> ()
6128 let birdseyemouse button down x y mask
6129 (conf, leftx, _, hooverpageno, anchor) =
6130 match button with
6131 | 1 when down ->
6132 let rec loop = function
6133 | [] -> ()
6134 | l :: rest ->
6135 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6136 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6137 then (
6138 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6140 else loop rest
6142 loop state.layout
6143 | 3 -> ()
6144 | _ -> viewmouse button down x y mask
6147 let mouse button down x y mask =
6148 state.uioh <- state.uioh#button button down x y mask;
6151 let motion ~x ~y =
6152 state.uioh <- state.uioh#motion x y
6155 let pmotion ~x ~y =
6156 state.uioh <- state.uioh#pmotion x y;
6159 let uioh = object
6160 method display = ()
6162 method key key mask =
6163 begin match state.mode with
6164 | Textentry textentry -> textentrykeyboard key mask textentry
6165 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6166 | View -> viewkeyboard key mask
6167 | LinkNav linknav -> linknavkeyboard key mask linknav
6168 end;
6169 state.uioh
6171 method button button bstate x y mask =
6172 begin match state.mode with
6173 | LinkNav _
6174 | View -> viewmouse button bstate x y mask
6175 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6176 | Textentry _ -> ()
6177 end;
6178 state.uioh
6180 method motion x y =
6181 begin match state.mode with
6182 | Textentry _ -> ()
6183 | View | Birdseye _ | LinkNav _ ->
6184 match state.mstate with
6185 | Mzoom _ | Mnone -> ()
6187 | Mpan (x0, y0) ->
6188 let dx = x - x0
6189 and dy = y0 - y in
6190 state.mstate <- Mpan (x, y);
6191 if canpan ()
6192 then state.x <- panbound (state.x + dx);
6193 let y = clamp dy in
6194 gotoy_and_clear_text y
6196 | Msel (a, _) ->
6197 state.mstate <- Msel (a, (x, y));
6198 G.postRedisplay "motion select";
6200 | Mscrolly ->
6201 let y = min state.winh (max 0 y) in
6202 scrolly y
6204 | Mscrollx ->
6205 let x = min state.winw (max 0 x) in
6206 scrollx x
6208 | Mzoomrect (p0, _) ->
6209 state.mstate <- Mzoomrect (p0, (x, y));
6210 G.postRedisplay "motion zoomrect";
6211 end;
6212 state.uioh
6214 method pmotion x y =
6215 begin match state.mode with
6216 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6217 let rec loop = function
6218 | [] ->
6219 if hooverpageno != -1
6220 then (
6221 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6222 G.postRedisplay "pmotion birdseye no hoover";
6224 | l :: rest ->
6225 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6226 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6227 then (
6228 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6229 G.postRedisplay "pmotion birdseye hoover";
6231 else loop rest
6233 loop state.layout
6235 | Textentry _ -> ()
6237 | LinkNav _
6238 | View ->
6239 match state.mstate with
6240 | Mnone -> updateunder x y
6241 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6243 end;
6244 state.uioh
6246 method infochanged _ = ()
6248 method scrollph =
6249 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6250 let p, h =
6251 if maxy = 0
6252 then 0.0, float state.winh
6253 else scrollph state.y maxy
6255 state.scrollw, p, h
6257 method scrollpw =
6258 let winw = state.winw - state.scrollw in
6259 let fwinw = float winw in
6260 let sw =
6261 let sw = fwinw /. float state.w in
6262 let sw = fwinw *. sw in
6263 max sw (float conf.scrollh)
6265 let position =
6266 let maxx = state.w + winw in
6267 let x = winw - state.x in
6268 let percent = float x /. float maxx in
6269 (fwinw -. sw) *. percent
6271 state.hscrollh, position, sw
6273 method modehash =
6274 let modename =
6275 match state.mode with
6276 | LinkNav _ -> "links"
6277 | Textentry _ -> "textentry"
6278 | Birdseye _ -> "birdseye"
6279 | View -> "view"
6281 findkeyhash conf modename
6283 method eformsgs = true
6284 end;;
6286 module Config =
6287 struct
6288 open Parser
6290 let fontpath = ref "";;
6292 module KeyMap =
6293 Map.Make (struct type t = (int * int) let compare = compare end);;
6295 let unent s =
6296 let l = String.length s in
6297 let b = Buffer.create l in
6298 unent b s 0 l;
6299 Buffer.contents b;
6302 let home =
6303 try Sys.getenv "HOME"
6304 with exn ->
6305 prerr_endline
6306 ("Can not determine home directory location: " ^ exntos exn);
6310 let modifier_of_string = function
6311 | "alt" -> Wsi.altmask
6312 | "shift" -> Wsi.shiftmask
6313 | "ctrl" | "control" -> Wsi.ctrlmask
6314 | "meta" -> Wsi.metamask
6315 | _ -> 0
6318 let key_of_string =
6319 let r = Str.regexp "-" in
6320 fun s ->
6321 let elems = Str.full_split r s in
6322 let f n k m =
6323 let g s =
6324 let m1 = modifier_of_string s in
6325 if m1 = 0
6326 then (Wsi.namekey s, m)
6327 else (k, m lor m1)
6328 in function
6329 | Str.Delim s when n land 1 = 0 -> g s
6330 | Str.Text s -> g s
6331 | Str.Delim _ -> (k, m)
6333 let rec loop n k m = function
6334 | [] -> (k, m)
6335 | x :: xs ->
6336 let k, m = f n k m x in
6337 loop (n+1) k m xs
6339 loop 0 0 0 elems
6342 let keys_of_string =
6343 let r = Str.regexp "[ \t]" in
6344 fun s ->
6345 let elems = Str.split r s in
6346 List.map key_of_string elems
6349 let copykeyhashes c =
6350 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6353 let config_of c attrs =
6354 let apply c k v =
6356 match k with
6357 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6358 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6359 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6360 | "preload" -> { c with preload = bool_of_string v }
6361 | "page-bias" -> { c with pagebias = int_of_string v }
6362 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6363 | "horizontal-scroll-step" ->
6364 { c with hscrollstep = max (int_of_string v) 1 }
6365 | "auto-scroll-step" ->
6366 { c with autoscrollstep = max 0 (int_of_string v) }
6367 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6368 | "crop-hack" -> { c with crophack = bool_of_string v }
6369 | "throttle" ->
6370 let mw =
6371 match String.lowercase v with
6372 | "true" -> Some infinity
6373 | "false" -> None
6374 | f -> Some (float_of_string f)
6376 { c with maxwait = mw}
6377 | "highlight-links" -> { c with hlinks = bool_of_string v }
6378 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6379 | "vertical-margin" ->
6380 { c with interpagespace = max 0 (int_of_string v) }
6381 | "zoom" ->
6382 let zoom = float_of_string v /. 100. in
6383 let zoom = max zoom 0.0 in
6384 { c with zoom = zoom }
6385 | "presentation" -> { c with presentation = bool_of_string v }
6386 | "rotation-angle" -> { c with angle = int_of_string v }
6387 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6388 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6389 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6390 | "proportional-display" ->
6391 let fm =
6392 if bool_of_string v
6393 then FitProportional
6394 else FitWidth
6396 { c with fitmodel = fm }
6397 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6398 | "pixmap-cache-size" ->
6399 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6400 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6401 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6402 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6403 | "persistent-location" -> { c with jumpback = bool_of_string v }
6404 | "background-color" -> { c with bgcolor = color_of_string v }
6405 | "scrollbar-in-presentation" ->
6406 { c with scrollbarinpm = bool_of_string v }
6407 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6408 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6409 | "mupdf-store-size" ->
6410 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6411 | "checkers" -> { c with checkers = bool_of_string v }
6412 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6413 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6414 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6415 | "uri-launcher" -> { c with urilauncher = unent v }
6416 | "path-launcher" -> { c with pathlauncher = unent v }
6417 | "color-space" -> { c with colorspace = colorspace_of_string v }
6418 | "invert-colors" -> { c with invert = bool_of_string v }
6419 | "brightness" -> { c with colorscale = float_of_string v }
6420 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6421 | "ghyllscroll" ->
6422 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6423 | "columns" ->
6424 let (n, _, _) as nab = multicolumns_of_string v in
6425 if n < 0
6426 then { c with columns = Csplit (-n, [||]) }
6427 else { c with columns = Cmulti (nab, [||]) }
6428 | "birds-eye-columns" ->
6429 { c with beyecolumns = Some (max (int_of_string v) 2) }
6430 | "selection-command" -> { c with selcmd = unent v }
6431 | "synctex-command" -> { c with stcmd = unent v }
6432 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6433 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6434 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6435 | "use-pbo" -> { c with usepbo = bool_of_string v }
6436 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6437 | _ -> c
6438 with exn ->
6439 prerr_endline ("Error processing attribute (`" ^
6440 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6443 let rec fold c = function
6444 | [] -> c
6445 | (k, v) :: rest ->
6446 let c = apply c k v in
6447 fold c rest
6449 fold { c with keyhashes = copykeyhashes c } attrs;
6452 let fromstring f pos n v d =
6453 try f v
6454 with exn ->
6455 dolog "Error processing attribute (%S=%S) at %d\n%s"
6456 n v pos (exntos exn)
6461 let bookmark_of attrs =
6462 let rec fold title page rely visy = function
6463 | ("title", v) :: rest -> fold v page rely visy rest
6464 | ("page", v) :: rest -> fold title v rely visy rest
6465 | ("rely", v) :: rest -> fold title page v visy rest
6466 | ("visy", v) :: rest -> fold title page rely v rest
6467 | _ :: rest -> fold title page rely visy rest
6468 | [] -> title, page, rely, visy
6470 fold "invalid" "0" "0" "0" attrs
6473 let doc_of attrs =
6474 let rec fold path page rely pan visy = function
6475 | ("path", v) :: rest -> fold v page rely pan visy rest
6476 | ("page", v) :: rest -> fold path v rely pan visy rest
6477 | ("rely", v) :: rest -> fold path page v pan visy rest
6478 | ("pan", v) :: rest -> fold path page rely v visy rest
6479 | ("visy", v) :: rest -> fold path page rely pan v rest
6480 | _ :: rest -> fold path page rely pan visy rest
6481 | [] -> path, page, rely, pan, visy
6483 fold "" "0" "0" "0" "0" attrs
6486 let map_of attrs =
6487 let rec fold rs ls = function
6488 | ("out", v) :: rest -> fold v ls rest
6489 | ("in", v) :: rest -> fold rs v rest
6490 | _ :: rest -> fold ls rs rest
6491 | [] -> ls, rs
6493 fold "" "" attrs
6496 let setconf dst src =
6497 dst.scrollbw <- src.scrollbw;
6498 dst.scrollh <- src.scrollh;
6499 dst.icase <- src.icase;
6500 dst.preload <- src.preload;
6501 dst.pagebias <- src.pagebias;
6502 dst.verbose <- src.verbose;
6503 dst.scrollstep <- src.scrollstep;
6504 dst.maxhfit <- src.maxhfit;
6505 dst.crophack <- src.crophack;
6506 dst.autoscrollstep <- src.autoscrollstep;
6507 dst.maxwait <- src.maxwait;
6508 dst.hlinks <- src.hlinks;
6509 dst.underinfo <- src.underinfo;
6510 dst.interpagespace <- src.interpagespace;
6511 dst.zoom <- src.zoom;
6512 dst.presentation <- src.presentation;
6513 dst.angle <- src.angle;
6514 dst.cwinw <- src.cwinw;
6515 dst.cwinh <- src.cwinh;
6516 dst.savebmarks <- src.savebmarks;
6517 dst.memlimit <- src.memlimit;
6518 dst.fitmodel <- src.fitmodel;
6519 dst.texcount <- src.texcount;
6520 dst.sliceheight <- src.sliceheight;
6521 dst.thumbw <- src.thumbw;
6522 dst.jumpback <- src.jumpback;
6523 dst.bgcolor <- src.bgcolor;
6524 dst.scrollbarinpm <- src.scrollbarinpm;
6525 dst.tilew <- src.tilew;
6526 dst.tileh <- src.tileh;
6527 dst.mustoresize <- src.mustoresize;
6528 dst.checkers <- src.checkers;
6529 dst.aalevel <- src.aalevel;
6530 dst.trimmargins <- src.trimmargins;
6531 dst.trimfuzz <- src.trimfuzz;
6532 dst.urilauncher <- src.urilauncher;
6533 dst.colorspace <- src.colorspace;
6534 dst.invert <- src.invert;
6535 dst.colorscale <- src.colorscale;
6536 dst.redirectstderr <- src.redirectstderr;
6537 dst.ghyllscroll <- src.ghyllscroll;
6538 dst.columns <- src.columns;
6539 dst.beyecolumns <- src.beyecolumns;
6540 dst.selcmd <- src.selcmd;
6541 dst.updatecurs <- src.updatecurs;
6542 dst.pathlauncher <- src.pathlauncher;
6543 dst.keyhashes <- copykeyhashes src;
6544 dst.hfsize <- src.hfsize;
6545 dst.hscrollstep <- src.hscrollstep;
6546 dst.pgscale <- src.pgscale;
6547 dst.usepbo <- src.usepbo;
6548 dst.wheelbypage <- src.wheelbypage;
6549 dst.stcmd <- src.stcmd;
6552 let get s =
6553 let h = Hashtbl.create 10 in
6554 let dc = { defconf with angle = defconf.angle } in
6555 let rec toplevel v t spos _ =
6556 match t with
6557 | Vdata | Vcdata | Vend -> v
6558 | Vopen ("llppconfig", _, closed) ->
6559 if closed
6560 then v
6561 else { v with f = llppconfig }
6562 | Vopen _ ->
6563 error "unexpected subelement at top level" s spos
6564 | Vclose _ -> error "unexpected close at top level" s spos
6566 and llppconfig v t spos _ =
6567 match t with
6568 | Vdata | Vcdata -> v
6569 | Vend -> error "unexpected end of input in llppconfig" s spos
6570 | Vopen ("defaults", attrs, closed) ->
6571 let c = config_of dc attrs in
6572 setconf dc c;
6573 if closed
6574 then v
6575 else { v with f = defaults }
6577 | Vopen ("ui-font", attrs, closed) ->
6578 let rec getsize size = function
6579 | [] -> size
6580 | ("size", v) :: rest ->
6581 let size =
6582 fromstring int_of_string spos "size" v fstate.fontsize in
6583 getsize size rest
6584 | l -> getsize size l
6586 fstate.fontsize <- getsize fstate.fontsize attrs;
6587 if closed
6588 then v
6589 else { v with f = uifont (Buffer.create 10) }
6591 | Vopen ("doc", attrs, closed) ->
6592 let pathent, spage, srely, span, svisy = doc_of attrs in
6593 let path = unent pathent
6594 and pageno = fromstring int_of_string spos "page" spage 0
6595 and rely = fromstring float_of_string spos "rely" srely 0.0
6596 and pan = fromstring int_of_string spos "pan" span 0
6597 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6598 let c = config_of dc attrs in
6599 let anchor = (pageno, rely, visy) in
6600 if closed
6601 then (Hashtbl.add h path (c, [], pan, anchor); v)
6602 else { v with f = doc path pan anchor c [] }
6604 | Vopen _ ->
6605 error "unexpected subelement in llppconfig" s spos
6607 | Vclose "llppconfig" -> { v with f = toplevel }
6608 | Vclose _ -> error "unexpected close in llppconfig" s spos
6610 and defaults v t spos _ =
6611 match t with
6612 | Vdata | Vcdata -> v
6613 | Vend -> error "unexpected end of input in defaults" s spos
6614 | Vopen ("keymap", attrs, closed) ->
6615 let modename =
6616 try List.assoc "mode" attrs
6617 with Not_found -> "global" in
6618 if closed
6619 then v
6620 else
6621 let ret keymap =
6622 let h = findkeyhash dc modename in
6623 KeyMap.iter (Hashtbl.replace h) keymap;
6624 defaults
6626 { v with f = pkeymap ret KeyMap.empty }
6628 | Vopen (_, _, _) ->
6629 error "unexpected subelement in defaults" s spos
6631 | Vclose "defaults" ->
6632 { v with f = llppconfig }
6634 | Vclose _ -> error "unexpected close in defaults" s spos
6636 and uifont b v t spos epos =
6637 match t with
6638 | Vdata | Vcdata ->
6639 Buffer.add_substring b s spos (epos - spos);
6641 | Vopen (_, _, _) ->
6642 error "unexpected subelement in ui-font" s spos
6643 | Vclose "ui-font" ->
6644 if String.length !fontpath = 0
6645 then fontpath := Buffer.contents b;
6646 { v with f = llppconfig }
6647 | Vclose _ -> error "unexpected close in ui-font" s spos
6648 | Vend -> error "unexpected end of input in ui-font" s spos
6650 and doc path pan anchor c bookmarks v t spos _ =
6651 match t with
6652 | Vdata | Vcdata -> v
6653 | Vend -> error "unexpected end of input in doc" s spos
6654 | Vopen ("bookmarks", _, closed) ->
6655 if closed
6656 then v
6657 else { v with f = pbookmarks path pan anchor c bookmarks }
6659 | Vopen ("keymap", attrs, closed) ->
6660 let modename =
6661 try List.assoc "mode" attrs
6662 with Not_found -> "global"
6664 if closed
6665 then v
6666 else
6667 let ret keymap =
6668 let h = findkeyhash c modename in
6669 KeyMap.iter (Hashtbl.replace h) keymap;
6670 doc path pan anchor c bookmarks
6672 { v with f = pkeymap ret KeyMap.empty }
6674 | Vopen (_, _, _) ->
6675 error "unexpected subelement in doc" s spos
6677 | Vclose "doc" ->
6678 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6679 { v with f = llppconfig }
6681 | Vclose _ -> error "unexpected close in doc" s spos
6683 and pkeymap ret keymap v t spos _ =
6684 match t with
6685 | Vdata | Vcdata -> v
6686 | Vend -> error "unexpected end of input in keymap" s spos
6687 | Vopen ("map", attrs, closed) ->
6688 let r, l = map_of attrs in
6689 let kss = fromstring keys_of_string spos "in" r [] in
6690 let lss = fromstring keys_of_string spos "out" l [] in
6691 let keymap =
6692 match kss with
6693 | [] -> keymap
6694 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6695 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6697 if closed
6698 then { v with f = pkeymap ret keymap }
6699 else
6700 let f () = v in
6701 { v with f = skip "map" f }
6703 | Vopen _ ->
6704 error "unexpected subelement in keymap" s spos
6706 | Vclose "keymap" ->
6707 { v with f = ret keymap }
6709 | Vclose _ -> error "unexpected close in keymap" s spos
6711 and pbookmarks path pan anchor c bookmarks v t spos _ =
6712 match t with
6713 | Vdata | Vcdata -> v
6714 | Vend -> error "unexpected end of input in bookmarks" s spos
6715 | Vopen ("item", attrs, closed) ->
6716 let titleent, spage, srely, svisy = bookmark_of attrs in
6717 let page = fromstring int_of_string spos "page" spage 0
6718 and rely = fromstring float_of_string spos "rely" srely 0.0
6719 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6720 let bookmarks =
6721 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6723 if closed
6724 then { v with f = pbookmarks path pan anchor c bookmarks }
6725 else
6726 let f () = v in
6727 { v with f = skip "item" f }
6729 | Vopen _ ->
6730 error "unexpected subelement in bookmarks" s spos
6732 | Vclose "bookmarks" ->
6733 { v with f = doc path pan anchor c bookmarks }
6735 | Vclose _ -> error "unexpected close in bookmarks" s spos
6737 and skip tag f v t spos _ =
6738 match t with
6739 | Vdata | Vcdata -> v
6740 | Vend ->
6741 error ("unexpected end of input in skipped " ^ tag) s spos
6742 | Vopen (tag', _, closed) ->
6743 if closed
6744 then v
6745 else
6746 let f' () = { v with f = skip tag f } in
6747 { v with f = skip tag' f' }
6748 | Vclose ctag ->
6749 if tag = ctag
6750 then f ()
6751 else error ("unexpected close in skipped " ^ tag) s spos
6754 parse { f = toplevel; accu = () } s;
6755 h, dc;
6758 let do_load f ic =
6760 let len = in_channel_length ic in
6761 let s = String.create len in
6762 really_input ic s 0 len;
6763 f s;
6764 with
6765 | Parse_error (msg, s, pos) ->
6766 let subs = subs s pos in
6767 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6768 failwith ("parse error: " ^ s)
6770 | exn ->
6771 failwith ("config load error: " ^ exntos exn)
6774 let defconfpath =
6775 let dir =
6777 let dir = Filename.concat home ".config" in
6778 if Sys.is_directory dir then dir else home
6779 with _ -> home
6781 Filename.concat dir "llpp.conf"
6784 let confpath = ref defconfpath;;
6786 let load1 f =
6787 if Sys.file_exists !confpath
6788 then
6789 match
6790 (try Some (open_in_bin !confpath)
6791 with exn ->
6792 prerr_endline
6793 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6794 exntos exn);
6795 None
6797 with
6798 | Some ic ->
6799 let success =
6801 f (do_load get ic)
6802 with exn ->
6803 prerr_endline
6804 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6805 exntos exn);
6806 false
6808 close_in ic;
6809 success
6811 | None -> false
6812 else
6813 f (Hashtbl.create 0, defconf)
6816 let load () =
6817 let f (h, dc) =
6818 let pc, pb, px, pa =
6820 let key =
6821 if String.length state.origin = 0
6822 then state.path
6823 else state.origin
6825 Hashtbl.find h (Filename.basename key)
6826 with Not_found -> dc, [], 0, emptyanchor
6828 setconf defconf dc;
6829 setconf conf pc;
6830 state.bookmarks <- pb;
6831 state.x <- px;
6832 state.scrollw <- conf.scrollbw;
6833 if conf.jumpback
6834 then state.anchor <- pa;
6835 cbput state.hists.nav pa;
6836 true
6838 load1 f
6841 let add_attrs bb always dc c =
6842 let ob s a b =
6843 if always || a != b
6844 then Printf.bprintf bb "\n %s='%b'" s a
6845 and oi s a b =
6846 if always || a != b
6847 then Printf.bprintf bb "\n %s='%d'" s a
6848 and oI s a b =
6849 if always || a != b
6850 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6851 and oz s a b =
6852 if always || a <> b
6853 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6854 and oF s a b =
6855 if always || a <> b
6856 then Printf.bprintf bb "\n %s='%f'" s a
6857 and oc s a b =
6858 if always || a <> b
6859 then
6860 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6861 and oC s a b =
6862 if always || a <> b
6863 then
6864 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6865 and oR s a b =
6866 if always || a <> b
6867 then
6868 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6869 and os s a b =
6870 if always || a <> b
6871 then
6872 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6873 and og s a b =
6874 if always || a <> b
6875 then
6876 match a with
6877 | None -> ()
6878 | Some (_N, _A, _B) ->
6879 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6880 and oW s a b =
6881 if always || a <> b
6882 then
6883 let v =
6884 match a with
6885 | None -> "false"
6886 | Some f ->
6887 if f = infinity
6888 then "true"
6889 else string_of_float f
6891 Printf.bprintf bb "\n %s='%s'" s v
6892 and oco s a b =
6893 if always || a <> b
6894 then
6895 match a with
6896 | Cmulti ((n, a, b), _) when n > 1 ->
6897 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6898 | Csplit (n, _) when n > 1 ->
6899 Printf.bprintf bb "\n %s='%d'" s ~-n
6900 | _ -> ()
6901 and obeco s a b =
6902 if always || a <> b
6903 then
6904 match a with
6905 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6906 | _ -> ()
6907 and oFm s a b =
6908 if always || a <> b
6909 then
6910 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6912 oi "width" c.cwinw dc.cwinw;
6913 oi "height" c.cwinh dc.cwinh;
6914 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6915 oi "scroll-handle-height" c.scrollh dc.scrollh;
6916 ob "case-insensitive-search" c.icase dc.icase;
6917 ob "preload" c.preload dc.preload;
6918 oi "page-bias" c.pagebias dc.pagebias;
6919 oi "scroll-step" c.scrollstep dc.scrollstep;
6920 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6921 ob "max-height-fit" c.maxhfit dc.maxhfit;
6922 ob "crop-hack" c.crophack dc.crophack;
6923 oW "throttle" c.maxwait dc.maxwait;
6924 ob "highlight-links" c.hlinks dc.hlinks;
6925 ob "under-cursor-info" c.underinfo dc.underinfo;
6926 oi "vertical-margin" c.interpagespace dc.interpagespace;
6927 oz "zoom" c.zoom dc.zoom;
6928 ob "presentation" c.presentation dc.presentation;
6929 oi "rotation-angle" c.angle dc.angle;
6930 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6931 oFm "fit-model" c.fitmodel dc.fitmodel;
6932 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6933 oi "tex-count" c.texcount dc.texcount;
6934 oi "slice-height" c.sliceheight dc.sliceheight;
6935 oi "thumbnail-width" c.thumbw dc.thumbw;
6936 ob "persistent-location" c.jumpback dc.jumpback;
6937 oc "background-color" c.bgcolor dc.bgcolor;
6938 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6939 oi "tile-width" c.tilew dc.tilew;
6940 oi "tile-height" c.tileh dc.tileh;
6941 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6942 ob "checkers" c.checkers dc.checkers;
6943 oi "aalevel" c.aalevel dc.aalevel;
6944 ob "trim-margins" c.trimmargins dc.trimmargins;
6945 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6946 os "uri-launcher" c.urilauncher dc.urilauncher;
6947 os "path-launcher" c.pathlauncher dc.pathlauncher;
6948 oC "color-space" c.colorspace dc.colorspace;
6949 ob "invert-colors" c.invert dc.invert;
6950 oF "brightness" c.colorscale dc.colorscale;
6951 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6952 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6953 oco "columns" c.columns dc.columns;
6954 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6955 os "selection-command" c.selcmd dc.selcmd;
6956 os "synctex-command" c.stcmd dc.stcmd;
6957 ob "update-cursor" c.updatecurs dc.updatecurs;
6958 oi "hint-font-size" c.hfsize dc.hfsize;
6959 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6960 oF "page-scroll-scale" c.pgscale dc.pgscale;
6961 ob "use-pbo" c.usepbo dc.usepbo;
6962 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6965 let keymapsbuf always dc c =
6966 let bb = Buffer.create 16 in
6967 let rec loop = function
6968 | [] -> ()
6969 | (modename, h) :: rest ->
6970 let dh = findkeyhash dc modename in
6971 if always || h <> dh
6972 then (
6973 if Hashtbl.length h > 0
6974 then (
6975 if Buffer.length bb > 0
6976 then Buffer.add_char bb '\n';
6977 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6978 Hashtbl.iter (fun i o ->
6979 let isdifferent = always ||
6981 let dO = Hashtbl.find dh i in
6982 dO <> o
6983 with Not_found -> true
6985 if isdifferent
6986 then
6987 let addkm (k, m) =
6988 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6989 if Wsi.withalt m then Buffer.add_string bb "alt-";
6990 if Wsi.withshift m then Buffer.add_string bb "shift-";
6991 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6992 Buffer.add_string bb (Wsi.keyname k);
6994 let addkms l =
6995 let rec loop = function
6996 | [] -> ()
6997 | km :: [] -> addkm km
6998 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7000 loop l
7002 Buffer.add_string bb "<map in='";
7003 addkm i;
7004 match o with
7005 | KMinsrt km ->
7006 Buffer.add_string bb "' out='";
7007 addkm km;
7008 Buffer.add_string bb "'/>\n"
7010 | KMinsrl kms ->
7011 Buffer.add_string bb "' out='";
7012 addkms kms;
7013 Buffer.add_string bb "'/>\n"
7015 | KMmulti (ins, kms) ->
7016 Buffer.add_char bb ' ';
7017 addkms ins;
7018 Buffer.add_string bb "' out='";
7019 addkms kms;
7020 Buffer.add_string bb "'/>\n"
7021 ) h;
7022 Buffer.add_string bb "</keymap>";
7025 loop rest
7027 loop c.keyhashes;
7031 let save () =
7032 let uifontsize = fstate.fontsize in
7033 let bb = Buffer.create 32768 in
7034 let relx = float state.x /. float state.winw in
7035 let w, h, x =
7036 let cx w = truncate (relx *. float w) in
7037 List.fold_left
7038 (fun (w, h, x) ws ->
7039 match ws with
7040 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7041 | Wsi.MaxVert -> (w, conf.cwinh, x)
7042 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7044 (state.winw, state.winh, state.x) state.winstate
7046 conf.cwinw <- w;
7047 conf.cwinh <- h;
7048 let f (h, dc) =
7049 let dc = if conf.bedefault then conf else dc in
7050 Buffer.add_string bb "<llppconfig>\n";
7052 if String.length !fontpath > 0
7053 then
7054 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7055 uifontsize
7056 !fontpath
7057 else (
7058 if uifontsize <> 14
7059 then
7060 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7063 Buffer.add_string bb "<defaults ";
7064 add_attrs bb true dc dc;
7065 let kb = keymapsbuf true dc dc in
7066 if Buffer.length kb > 0
7067 then (
7068 Buffer.add_string bb ">\n";
7069 Buffer.add_buffer bb kb;
7070 Buffer.add_string bb "\n</defaults>\n";
7072 else Buffer.add_string bb "/>\n";
7074 let adddoc path pan anchor c bookmarks =
7075 if bookmarks == [] && c = dc && anchor = emptyanchor
7076 then ()
7077 else (
7078 Printf.bprintf bb "<doc path='%s'"
7079 (enent path 0 (String.length path));
7081 if anchor <> emptyanchor
7082 then (
7083 let n, rely, visy = anchor in
7084 Printf.bprintf bb " page='%d'" n;
7085 if rely > 1e-6
7086 then
7087 Printf.bprintf bb " rely='%f'" rely
7089 if abs_float visy > 1e-6
7090 then
7091 Printf.bprintf bb " visy='%f'" visy
7095 if pan != 0
7096 then Printf.bprintf bb " pan='%d'" pan;
7098 add_attrs bb false dc c;
7099 let kb = keymapsbuf false dc c in
7101 begin match bookmarks with
7102 | [] ->
7103 if Buffer.length kb > 0
7104 then (
7105 Buffer.add_string bb ">\n";
7106 Buffer.add_buffer bb kb;
7107 Buffer.add_string bb "\n</doc>\n";
7109 else Buffer.add_string bb "/>\n"
7110 | _ ->
7111 Buffer.add_string bb ">\n<bookmarks>\n";
7112 List.iter (fun (title, _level, (page, rely, visy)) ->
7113 Printf.bprintf bb
7114 "<item title='%s' page='%d'"
7115 (enent title 0 (String.length title))
7116 page
7118 if rely > 1e-6
7119 then
7120 Printf.bprintf bb " rely='%f'" rely
7122 if abs_float visy > 1e-6
7123 then
7124 Printf.bprintf bb " visy='%f'" visy
7126 Buffer.add_string bb "/>\n";
7127 ) bookmarks;
7128 Buffer.add_string bb "</bookmarks>";
7129 if Buffer.length kb > 0
7130 then (
7131 Buffer.add_string bb "\n";
7132 Buffer.add_buffer bb kb;
7134 Buffer.add_string bb "\n</doc>\n";
7135 end;
7139 let pan, conf =
7140 match state.mode with
7141 | Birdseye (c, pan, _, _, _) ->
7142 let beyecolumns =
7143 match conf.columns with
7144 | Cmulti ((c, _, _), _) -> Some c
7145 | Csingle _ -> None
7146 | Csplit _ -> None
7147 and columns =
7148 match c.columns with
7149 | Cmulti (c, _) -> Cmulti (c, [||])
7150 | Csingle _ -> Csingle [||]
7151 | Csplit _ -> failwith "quit from bird's eye while split"
7153 pan, { c with beyecolumns = beyecolumns; columns = columns }
7154 | _ -> x, conf
7156 let basename = Filename.basename
7157 (if String.length state.origin = 0 then state.path else state.origin)
7159 adddoc basename pan (getanchor ())
7160 (let conf =
7161 let autoscrollstep =
7162 match state.autoscroll with
7163 | Some step -> step
7164 | None -> conf.autoscrollstep
7166 match state.mode with
7167 | Birdseye (bc, _, _, _, _) ->
7168 { conf with
7169 zoom = bc.zoom;
7170 presentation = bc.presentation;
7171 interpagespace = bc.interpagespace;
7172 maxwait = bc.maxwait;
7173 autoscrollstep = autoscrollstep }
7174 | _ -> { conf with autoscrollstep = autoscrollstep }
7175 in conf)
7176 (if conf.savebmarks then state.bookmarks else []);
7178 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7179 if basename <> path
7180 then adddoc path x anchor c bookmarks
7181 ) h;
7182 Buffer.add_string bb "</llppconfig>\n";
7183 true;
7185 if load1 f && Buffer.length bb > 0
7186 then
7188 let tmp = !confpath ^ ".tmp" in
7189 let oc = open_out_bin tmp in
7190 Buffer.output_buffer oc bb;
7191 close_out oc;
7192 Unix.rename tmp !confpath;
7193 with exn ->
7194 prerr_endline
7195 ("error while saving configuration: " ^ exntos exn)
7197 end;;
7199 let adderrmsg src msg =
7200 Buffer.add_string state.errmsgs msg;
7201 state.newerrmsgs <- true;
7202 G.postRedisplay src
7205 let adderrfmt src fmt =
7206 Format.kprintf (fun s -> adderrmsg src s) fmt;
7209 let ract cmds =
7210 let cl = splitatspace cmds in
7211 let scan s fmt f =
7212 try Scanf.sscanf s fmt f
7213 with exn ->
7214 adderrfmt "remote exec"
7215 "error processing '%S': %s\n" cmds (exntos exn)
7217 match cl with
7218 | "reload" :: [] -> reload ()
7219 | "goto" :: args :: [] ->
7220 scan args "%u %f %f"
7221 (fun pageno x y ->
7222 let cmd, _ = state.geomcmds in
7223 if String.length cmd = 0
7224 then gotopagexy pageno x y
7225 else
7226 let f prevf () =
7227 gotopagexy pageno x y;
7228 prevf ()
7230 state.reprf <- f state.reprf
7232 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7233 | "rect" :: args :: [] ->
7234 scan args "%u %u %f %f %f %f"
7235 (fun pageno color x0 y0 x1 y1 ->
7236 onpagerect pageno (fun w h ->
7237 let _,w1,h1,_ = getpagedim pageno in
7238 let sw = float w1 /. w
7239 and sh = float h1 /. h in
7240 let x0s = x0 *. sw
7241 and x1s = x1 *. sw
7242 and y0s = y0 *. sh
7243 and y1s = y1 *. sh in
7244 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7245 debugrect rect;
7246 state.rects <- (pageno, color, rect) :: state.rects;
7247 G.postRedisplay "rect";
7250 | "activatewin" :: [] -> Wsi.activatewin ()
7251 | "quit" :: [] -> raise Quit
7252 | _ ->
7253 adderrfmt "remote command"
7254 "error processing remote command: %S\n" cmds;
7257 let remote =
7258 let scratch = String.create 80 in
7259 let buf = Buffer.create 80 in
7260 fun fd ->
7261 let rec tempfr () =
7262 try Some (Unix.read fd scratch 0 80)
7263 with
7264 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7265 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7266 | exn -> raise exn
7268 match tempfr () with
7269 | None -> Some fd
7270 | Some n ->
7271 if n = 0
7272 then (
7273 Unix.close fd;
7274 if Buffer.length buf > 0
7275 then (
7276 let s = Buffer.contents buf in
7277 Buffer.clear buf;
7278 ract s;
7280 None
7282 else
7283 let rec eat ppos =
7284 let nlpos =
7286 let pos = String.index_from scratch ppos '\n' in
7287 if pos >= n then -1 else pos
7288 with Not_found -> -1
7290 if nlpos >= 0
7291 then (
7292 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7293 let s = Buffer.contents buf in
7294 Buffer.clear buf;
7295 ract s;
7296 eat (nlpos+1);
7298 else (
7299 Buffer.add_substring buf scratch ppos (n-ppos);
7300 Some fd
7302 in eat 0
7305 let remoteopen path =
7306 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7307 with exn ->
7308 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7309 None
7312 let () =
7313 let trimcachepath = ref "" in
7314 let rcmdpath = ref "" in
7315 Arg.parse
7316 (Arg.align
7317 [("-p", Arg.String (fun s -> state.password <- s),
7318 "<password> Set password");
7320 ("-f", Arg.String (fun s -> Config.fontpath := s),
7321 "<path> Set path to the user interface font");
7323 ("-c", Arg.String (fun s -> Config.confpath := s),
7324 "<path> Set path to the configuration file");
7326 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7327 "<path> Set path to the trim cache file");
7329 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7330 "<named-destination> Set named destination");
7332 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7334 ("-remote", Arg.String (fun s -> rcmdpath := s),
7335 "<path> Set path to the remote commands source");
7337 ("-origin", Arg.String (fun s -> state.origin <- s),
7338 "<original-path> Set original path");
7340 ("-v", Arg.Unit (fun () ->
7341 Printf.printf
7342 "%s\nconfiguration path: %s\n"
7343 (version ())
7344 Config.defconfpath
7346 exit 0), " Print version and exit");
7349 (fun s -> state.path <- s)
7350 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7352 if String.length state.path = 0
7353 then (prerr_endline "file name missing"; exit 1);
7355 if not (Config.load ())
7356 then prerr_endline "failed to load configuration";
7358 let globalkeyhash = findkeyhash conf "global" in
7359 let wsfd, winw, winh = Wsi.init (object
7360 method expose =
7361 if nogeomcmds state.geomcmds || platform == Posx
7362 then display ()
7363 else (
7364 GlClear.color (scalecolor2 conf.bgcolor);
7365 GlClear.clear [`color];
7367 method display = display ()
7368 method reshape w h = reshape w h
7369 method mouse b d x y m = mouse b d x y m
7370 method motion x y = state.mpos <- (x, y); motion x y
7371 method pmotion x y = state.mpos <- (x, y); pmotion x y
7372 method key k m =
7373 let mascm = m land (
7374 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7375 ) in
7376 match state.keystate with
7377 | KSnone ->
7378 let km = k, mascm in
7379 begin
7380 match
7381 let modehash = state.uioh#modehash in
7382 try Hashtbl.find modehash km
7383 with Not_found ->
7384 try Hashtbl.find globalkeyhash km
7385 with Not_found -> KMinsrt (k, m)
7386 with
7387 | KMinsrt (k, m) -> keyboard k m
7388 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7389 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7391 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7392 List.iter (fun (k, m) -> keyboard k m) insrt;
7393 state.keystate <- KSnone
7394 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7395 state.keystate <- KSinto (keys, insrt)
7396 | _ ->
7397 state.keystate <- KSnone
7399 method enter x y = state.mpos <- (x, y); pmotion x y
7400 method leave = state.mpos <- (-1, -1)
7401 method winstate wsl = state.winstate <- wsl
7402 method quit = raise Quit
7403 end) conf.cwinw conf.cwinh (platform = Posx) in
7405 state.wsfd <- wsfd;
7407 if not (
7408 List.exists GlMisc.check_extension
7409 [ "GL_ARB_texture_rectangle"
7410 ; "GL_EXT_texture_recangle"
7411 ; "GL_NV_texture_rectangle" ]
7413 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7415 let cr, sw =
7416 match Ne.pipe () with
7417 | Ne.Exn exn ->
7418 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7419 exit 1
7420 | Ne.Res rw -> rw
7421 and sr, cw =
7422 match Ne.pipe () with
7423 | Ne.Exn exn ->
7424 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7425 exit 1
7426 | Ne.Res rw -> rw
7429 cloexec cr;
7430 cloexec sw;
7431 cloexec sr;
7432 cloexec cw;
7434 setcheckers conf.checkers;
7435 redirectstderr ();
7437 init (cr, cw) (
7438 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7439 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7440 !Config.fontpath, !trimcachepath,
7441 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7443 state.sr <- sr;
7444 state.sw <- sw;
7445 state.text <- "Opening " ^ (mbtoutf8 state.path);
7446 reshape winw winh;
7447 opendoc state.path state.password;
7448 state.uioh <- uioh;
7450 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7451 let optrfd =
7452 ref (
7453 if String.length !rcmdpath > 0
7454 then remoteopen !rcmdpath
7455 else None
7459 let rec loop deadline =
7460 let r =
7461 match state.errfd with
7462 | None -> [state.sr; state.wsfd]
7463 | Some fd -> [state.sr; state.wsfd; fd]
7465 let r =
7466 match !optrfd with
7467 | None -> r
7468 | Some fd -> fd :: r
7470 if state.redisplay
7471 then (
7472 state.redisplay <- false;
7473 display ();
7475 let timeout =
7476 let now = now () in
7477 if deadline > now
7478 then (
7479 if deadline = infinity
7480 then ~-.1.0
7481 else max 0.0 (deadline -. now)
7483 else 0.0
7485 let r, _, _ =
7486 try Unix.select r [] [] timeout
7487 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7489 begin match r with
7490 | [] ->
7491 state.ghyll None;
7492 let newdeadline =
7493 if state.ghyll == noghyll
7494 then
7495 match state.autoscroll with
7496 | Some step when step != 0 ->
7497 let y = state.y + step in
7498 let y =
7499 if y < 0
7500 then state.maxy
7501 else if y >= state.maxy then 0 else y
7503 gotoy y;
7504 if state.mode = View
7505 then state.text <- "";
7506 deadline +. 0.01
7507 | _ -> infinity
7508 else deadline +. 0.01
7510 loop newdeadline
7512 | l ->
7513 let rec checkfds = function
7514 | [] -> ()
7515 | fd :: rest when fd = state.sr ->
7516 let cmd = readcmd state.sr in
7517 act cmd;
7518 checkfds rest
7520 | fd :: rest when fd = state.wsfd ->
7521 Wsi.readresp fd;
7522 checkfds rest
7524 | fd :: rest when Some fd = !optrfd ->
7525 begin match remote fd with
7526 | None -> optrfd := remoteopen !rcmdpath;
7527 | opt -> optrfd := opt
7528 end;
7529 checkfds rest
7531 | fd :: rest ->
7532 let s = String.create 80 in
7533 let n = tempfailureretry (Unix.read fd s 0) 80 in
7534 if conf.redirectstderr
7535 then (
7536 Buffer.add_substring state.errmsgs s 0 n;
7537 state.newerrmsgs <- true;
7538 state.redisplay <- true;
7540 else (
7541 prerr_string (String.sub s 0 n);
7542 flush stderr;
7544 checkfds rest
7546 checkfds l;
7547 let newdeadline =
7548 let deadline1 =
7549 if deadline = infinity
7550 then now () +. 0.01
7551 else deadline
7553 match state.autoscroll with
7554 | Some step when step != 0 -> deadline1
7555 | _ -> if state.ghyll == noghyll then infinity else deadline1
7557 loop newdeadline
7558 end;
7561 loop infinity;
7562 with Quit ->
7563 Config.save ();