Don't paper over the issue
[llpp.git] / main.ml
blob9668bf731eecd076dec14c415b55d973adcf5e71
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 scrollb = int;;
294 let scrollbvv = 1;;
295 let scrollbhv = 2;;
297 type conf =
298 { mutable scrollbw : int
299 ; mutable scrollh : int
300 ; mutable scrollb : scrollb
301 ; mutable icase : bool
302 ; mutable preload : bool
303 ; mutable pagebias : int
304 ; mutable verbose : bool
305 ; mutable debug : bool
306 ; mutable scrollstep : int
307 ; mutable hscrollstep : int
308 ; mutable maxhfit : bool
309 ; mutable crophack : bool
310 ; mutable autoscrollstep : int
311 ; mutable maxwait : float option
312 ; mutable hlinks : bool
313 ; mutable underinfo : bool
314 ; mutable interpagespace : interpagespace
315 ; mutable zoom : float
316 ; mutable presentation : bool
317 ; mutable angle : angle
318 ; mutable cwinw : int
319 ; mutable cwinh : int
320 ; mutable savebmarks : bool
321 ; mutable fitmodel : fitmodel
322 ; mutable trimmargins : trimmargins
323 ; mutable trimfuzz : irect
324 ; mutable memlimit : memsize
325 ; mutable texcount : texcount
326 ; mutable sliceheight : sliceheight
327 ; mutable thumbw : width
328 ; mutable jumpback : bool
329 ; mutable bgcolor : (float * float * float)
330 ; mutable bedefault : bool
331 ; mutable tilew : int
332 ; mutable tileh : int
333 ; mutable mustoresize : memsize
334 ; mutable checkers : bool
335 ; mutable aalevel : int
336 ; mutable urilauncher : string
337 ; mutable pathlauncher : string
338 ; mutable colorspace : colorspace
339 ; mutable invert : bool
340 ; mutable colorscale : float
341 ; mutable redirectstderr : bool
342 ; mutable ghyllscroll : (int * int * int) option
343 ; mutable columns : columns
344 ; mutable beyecolumns : columncount option
345 ; mutable selcmd : string
346 ; mutable updatecurs : bool
347 ; mutable keyhashes : (string * keyhash) list
348 ; mutable hfsize : int
349 ; mutable pgscale : float
350 ; mutable usepbo : bool
351 ; mutable wheelbypage : bool
352 ; mutable stcmd : string
354 and columns =
355 | Csingle of singlecolumn
356 | Cmulti of multicolumns
357 | Csplit of splitcolumns
360 type anchor = pageno * top * dtop;;
362 type outline = string * int * anchor;;
364 type rect = float * float * float * float * float * float * float * float;;
366 type tile = opaque * pixmapsize * elapsed
367 and elapsed = float;;
368 type pagemapkey = pageno * gen;;
369 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
370 and row = int
371 and col = int;;
373 let emptyanchor = (0, 0.0, 0.0);;
375 type infochange = | Memused | Docinfo | Pdim;;
377 class type uioh = object
378 method display : unit
379 method key : int -> int -> uioh
380 method button : int -> bool -> int -> int -> int -> uioh
381 method motion : int -> int -> uioh
382 method pmotion : int -> int -> uioh
383 method infochanged : infochange -> unit
384 method scrollpw : (int * float * float)
385 method scrollph : (int * float * float)
386 method modehash : keyhash
387 method eformsgs : bool
388 end;;
390 type mode =
391 | Birdseye of (conf * leftx * pageno * pageno * anchor)
392 | Textentry of (textentry * onleave)
393 | View
394 | LinkNav of linktarget
395 and onleave = leavetextentrystatus -> unit
396 and leavetextentrystatus = | Cancel | Confirm
397 and helpitem = string * int * action
398 and action =
399 | Noaction
400 | Action of (uioh -> uioh)
401 and linktarget =
402 | Ltexact of (pageno * int)
403 | Ltgendir of int
406 let isbirdseye = function Birdseye _ -> true | _ -> false;;
407 let istextentry = function Textentry _ -> true | _ -> false;;
409 type currently =
410 | Idle
411 | Loading of (page * gen)
412 | Tiling of (
413 page * opaque * colorspace * angle * gen * col * row * width * height
415 | Outlining of outline list
418 let emptykeyhash = Hashtbl.create 0;;
419 let nouioh : uioh = object (self)
420 method display = ()
421 method key _ _ = self
422 method button _ _ _ _ _ = self
423 method motion _ _ = self
424 method pmotion _ _ = self
425 method infochanged _ = ()
426 method scrollpw = (0, nan, nan)
427 method scrollph = (0, nan, nan)
428 method modehash = emptykeyhash
429 method eformsgs = false
430 end;;
432 type state =
433 { mutable sr : Unix.file_descr
434 ; mutable sw : Unix.file_descr
435 ; mutable wsfd : Unix.file_descr
436 ; mutable errfd : Unix.file_descr option
437 ; mutable stderr : Unix.file_descr
438 ; mutable errmsgs : Buffer.t
439 ; mutable newerrmsgs : bool
440 ; mutable w : int
441 ; mutable x : int
442 ; mutable y : int
443 ; mutable anchor : anchor
444 ; mutable ranchors : (string * string * anchor * string) list
445 ; mutable maxy : int
446 ; mutable layout : page list
447 ; pagemap : (pagemapkey, opaque) Hashtbl.t
448 ; tilemap : (tilemapkey, tile) Hashtbl.t
449 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
450 ; mutable pdims : (pageno * width * height * leftx) list
451 ; mutable pagecount : int
452 ; mutable currently : currently
453 ; mutable mstate : mstate
454 ; mutable searchpattern : string
455 ; mutable rects : (pageno * recttype * rect) list
456 ; mutable rects1 : (pageno * recttype * rect) list
457 ; mutable text : string
458 ; mutable winstate : Wsi.winstate list
459 ; mutable mode : mode
460 ; mutable uioh : uioh
461 ; mutable outlines : outline array
462 ; mutable bookmarks : outline list
463 ; mutable path : string
464 ; mutable password : string
465 ; mutable nameddest : string
466 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
467 ; mutable memused : memsize
468 ; mutable gen : gen
469 ; mutable throttle : (page list * int * float) option
470 ; mutable autoscroll : int option
471 ; mutable ghyll : (int option -> unit)
472 ; mutable help : helpitem array
473 ; mutable docinfo : (int * string) list
474 ; mutable texid : GlTex.texture_id option
475 ; hists : hists
476 ; mutable prevzoom : float
477 ; mutable progress : float
478 ; mutable redisplay : bool
479 ; mutable mpos : mpos
480 ; mutable keystate : keystate
481 ; mutable glinks : bool
482 ; mutable prevcolumns : (columns * float) option
483 ; mutable winw : int
484 ; mutable winh : int
485 ; mutable reprf : (unit -> unit)
486 ; mutable origin : string
488 and hists =
489 { pat : string circbuf
490 ; pag : string circbuf
491 ; nav : anchor circbuf
492 ; sel : string circbuf
496 let defconf =
497 { scrollbw = 7
498 ; scrollh = 12
499 ; scrollb = scrollbhv lor scrollbvv
500 ; icase = true
501 ; preload = true
502 ; pagebias = 0
503 ; verbose = false
504 ; debug = false
505 ; scrollstep = 24
506 ; hscrollstep = 24
507 ; maxhfit = true
508 ; crophack = false
509 ; autoscrollstep = 2
510 ; maxwait = None
511 ; hlinks = false
512 ; underinfo = false
513 ; interpagespace = 2
514 ; zoom = 1.0
515 ; presentation = false
516 ; angle = 0
517 ; cwinw = 900
518 ; cwinh = 900
519 ; savebmarks = true
520 ; fitmodel = FitProportional
521 ; trimmargins = false
522 ; trimfuzz = (0,0,0,0)
523 ; memlimit = 32 lsl 20
524 ; texcount = 256
525 ; sliceheight = 24
526 ; thumbw = 76
527 ; jumpback = true
528 ; bgcolor = (0.5, 0.5, 0.5)
529 ; bedefault = false
530 ; tilew = 2048
531 ; tileh = 2048
532 ; mustoresize = 256 lsl 20
533 ; checkers = true
534 ; aalevel = 8
535 ; urilauncher =
536 (match platform with
537 | Plinux | Pfreebsd | Pdragonflybsd
538 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
539 | Posx -> "open \"%s\""
540 | Pcygwin -> "cygstart \"%s\""
541 | Punknown -> "echo %s")
542 ; pathlauncher = "lp \"%s\""
543 ; selcmd =
544 (match platform with
545 | Plinux | Pfreebsd | Pdragonflybsd
546 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
547 | Posx -> "pbcopy"
548 | Pcygwin -> "wsel"
549 | Punknown -> "cat")
550 ; colorspace = Rgb
551 ; invert = false
552 ; colorscale = 1.0
553 ; redirectstderr = false
554 ; ghyllscroll = None
555 ; columns = Csingle [||]
556 ; beyecolumns = None
557 ; updatecurs = false
558 ; hfsize = 12
559 ; pgscale = 1.0
560 ; usepbo = false
561 ; wheelbypage = false
562 ; stcmd = "echo SyncTex"
563 ; keyhashes =
564 let mk n = (n, Hashtbl.create 1) in
565 [ mk "global"
566 ; mk "info"
567 ; mk "help"
568 ; mk "outline"
569 ; mk "listview"
570 ; mk "birdseye"
571 ; mk "textentry"
572 ; mk "links"
573 ; mk "view"
578 let wtmode = ref false;;
580 let findkeyhash c name =
581 try List.assoc name c.keyhashes
582 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
585 let conf = { defconf with angle = defconf.angle };;
587 let pgscale h = truncate (float h *. conf.pgscale);;
589 type fontstate =
590 { mutable fontsize : int
591 ; mutable wwidth : float
592 ; mutable maxrows : int
596 let fstate =
597 { fontsize = 14
598 ; wwidth = nan
599 ; maxrows = -1
603 let geturl s =
604 let colonpos = try String.index s ':' with Not_found -> -1 in
605 let len = String.length s in
606 if colonpos >= 0 && colonpos + 3 < len
607 then (
608 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
609 then
610 let schemestartpos =
611 try String.rindex_from s colonpos ' '
612 with Not_found -> -1
614 let scheme =
615 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
617 match scheme with
618 | "http" | "ftp" | "mailto" ->
619 let epos =
620 try String.index_from s colonpos ' '
621 with Not_found -> len
623 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
624 | _ -> ""
625 else ""
627 else ""
630 let gotouri uri =
631 if String.length conf.urilauncher = 0
632 then print_endline uri
633 else (
634 let url = geturl uri in
635 if String.length url = 0
636 then print_endline uri
637 else
638 let re = Str.regexp "%s" in
639 let command = Str.global_replace re url conf.urilauncher in
640 try popen command []
641 with exn ->
642 Printf.eprintf
643 "failed to execute `%s': %s\n" command (exntos exn);
644 flush stderr;
648 let version () =
649 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
650 (platform_to_string platform) Sys.word_size Sys.ocaml_version
653 let makehelp () =
654 let strings = version () :: "" :: Help.keys in
655 Array.of_list (
656 List.map (fun s ->
657 let url = geturl s in
658 if String.length url > 0
659 then (s, 0, Action (fun u -> gotouri url; u))
660 else (s, 0, Noaction)
661 ) strings);
664 let noghyll _ = ();;
665 let firstgeomcmds = "", [];;
666 let noreprf () = ();;
668 let state =
669 { sr = Unix.stdin
670 ; sw = Unix.stdin
671 ; wsfd = Unix.stdin
672 ; errfd = None
673 ; stderr = Unix.stderr
674 ; errmsgs = Buffer.create 0
675 ; newerrmsgs = false
676 ; x = 0
677 ; y = 0
678 ; w = 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 hscrollh () =
732 if (conf.scrollb land scrollbhv = 0)
733 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
734 then 0
735 else conf.scrollbw
738 let vscrollw () =
739 if (conf.scrollb land scrollbvv = 0)
740 then 0
741 else conf.scrollbw
744 let wadjsb w = w - vscrollw ();;
746 let setfontsize n =
747 fstate.fontsize <- n;
748 fstate.wwidth <- measurestr fstate.fontsize "w";
749 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
752 let vlog fmt =
753 if conf.verbose
754 then
755 Printf.kprintf prerr_endline fmt
756 else
757 Printf.kprintf ignore fmt
760 let launchpath () =
761 if String.length conf.pathlauncher = 0
762 then print_endline state.path
763 else (
764 let re = Str.regexp "%s" in
765 let command = Str.global_replace re state.path conf.pathlauncher in
766 try popen command []
767 with exn ->
768 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
769 flush stderr;
773 module Ne = struct
774 type 'a t = | Res of 'a | Exn of exn;;
776 let pipe () =
777 try Res (Unix.pipe ())
778 with exn -> Exn exn
781 let clo fd f =
782 try tempfailureretry Unix.close fd
783 with exn -> f (exntos exn)
786 let dup fd =
787 try Res (tempfailureretry Unix.dup fd)
788 with exn -> Exn exn
791 let dup2 fd1 fd2 =
792 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
793 with exn -> Exn exn
795 end;;
797 let redirectstderr () =
798 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
799 if conf.redirectstderr
800 then
801 match Ne.pipe () with
802 | Ne.Exn exn ->
803 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
805 | Ne.Res (r, w) ->
806 begin match Ne.dup Unix.stderr with
807 | Ne.Exn exn ->
808 dolog "failed to dup stderr: %s" (exntos exn);
809 Ne.clo r (clofail "pipe/r");
810 Ne.clo w (clofail "pipe/w");
812 | Ne.Res dupstderr ->
813 begin match Ne.dup2 w Unix.stderr with
814 | Ne.Exn exn ->
815 dolog "failed to dup2 to stderr: %s" (exntos exn);
816 Ne.clo dupstderr (clofail "stderr duplicate");
817 Ne.clo r (clofail "redir pipe/r");
818 Ne.clo w (clofail "redir pipe/w");
820 | Ne.Res () ->
821 state.stderr <- dupstderr;
822 state.errfd <- Some r;
823 end;
825 else (
826 state.newerrmsgs <- false;
827 begin match state.errfd with
828 | Some fd ->
829 begin match Ne.dup2 state.stderr Unix.stderr with
830 | Ne.Exn exn ->
831 dolog "failed to dup2 original stderr: %s" (exntos exn)
832 | Ne.Res () ->
833 Ne.clo fd (clofail "dup of stderr");
834 state.errfd <- None;
835 end;
836 | None -> ()
837 end;
838 prerr_string (Buffer.contents state.errmsgs);
839 flush stderr;
840 Buffer.clear state.errmsgs;
844 module G =
845 struct
846 let postRedisplay who =
847 if conf.verbose
848 then prerr_endline ("redisplay for " ^ who);
849 state.redisplay <- true;
851 end;;
853 let getopaque pageno =
854 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
855 with Not_found -> None
858 let putopaque pageno opaque =
859 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
862 let pagetranslatepoint l x y =
863 let dy = y - l.pagedispy in
864 let y = dy + l.pagey in
865 let dx = x - l.pagedispx in
866 let x = dx + l.pagex in
867 (x, y);
870 let onppundermouse g x y d =
871 let rec f = function
872 | l :: rest ->
873 begin match getopaque l.pageno with
874 | Some opaque ->
875 let x0 = l.pagedispx in
876 let x1 = x0 + l.pagevw in
877 let y0 = l.pagedispy in
878 let y1 = y0 + l.pagevh in
879 if y >= y0 && y <= y1 && x >= x0 && x <= x1
880 then
881 let px, py = pagetranslatepoint l x y in
882 match g opaque l px py with
883 | Some res -> res
884 | None -> f rest
885 else f rest
886 | _ ->
887 f rest
889 | [] -> d
891 f state.layout
894 let getunder x y =
895 let g opaque _ px py =
896 match whatsunder opaque px py with
897 | Unone -> None
898 | under -> Some under
900 onppundermouse g x y Unone
903 let unproject x y =
904 let g opaque l x y =
905 match unproject opaque x y with
906 | Some (x, y) -> Some (Some (l.pageno, x, y))
907 | None -> None
909 onppundermouse g x y None;
912 let showtext c s =
913 state.text <- Printf.sprintf "%c%s" c s;
914 G.postRedisplay "showtext";
917 let selstring s =
918 match Ne.pipe () with
919 | Ne.Exn exn ->
920 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
921 | Ne.Res (r, w) ->
922 let popened =
923 try popen conf.selcmd [r, 0; w, -1]; true
924 with exn ->
925 showtext '!'
926 (Printf.sprintf "failed to execute %s: %s"
927 conf.selcmd (exntos exn));
928 false
930 let clo cap fd =
931 Ne.clo fd (fun msg ->
932 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
935 if popened
936 then
937 (try
938 let l = String.length s in
939 let n = tempfailureretry (Unix.write w s 0) l in
940 if n != l
941 then
942 showtext '!'
943 (Printf.sprintf
944 "failed to write %d characters to sel pipe, wrote %d"
947 with exn ->
948 showtext '!'
949 (Printf.sprintf "failed to write to sel pipe: %s"
950 (exntos exn)
953 else dolog "%s" s;
954 clo "pipe/r" r;
955 clo "pipe/w" w;
958 let undertext = function
959 | Unone -> "none"
960 | Ulinkuri s -> s
961 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
962 | Utext s -> "font: " ^ s
963 | Uunexpected s -> "unexpected: " ^ s
964 | Ulaunch s -> "launch: " ^ s
965 | Unamed s -> "named: " ^ s
966 | Uremote (filename, pageno) ->
967 Printf.sprintf "%s: page %d" filename (pageno+1)
970 let updateunder x y =
971 match getunder x y with
972 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
973 | Ulinkuri uri ->
974 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
975 Wsi.setcursor Wsi.CURSOR_INFO
976 | Ulinkgoto (pageno, _) ->
977 if conf.underinfo
978 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
979 Wsi.setcursor Wsi.CURSOR_INFO
980 | Utext s ->
981 if conf.underinfo then showtext 'f' ("ont: " ^ s);
982 Wsi.setcursor Wsi.CURSOR_TEXT
983 | Uunexpected s ->
984 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
985 Wsi.setcursor Wsi.CURSOR_INHERIT
986 | Ulaunch s ->
987 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
988 Wsi.setcursor Wsi.CURSOR_INHERIT
989 | Unamed s ->
990 if conf.underinfo then showtext 'n' ("amed: " ^ s);
991 Wsi.setcursor Wsi.CURSOR_INHERIT
992 | Uremote (filename, pageno) ->
993 if conf.underinfo then showtext 'r'
994 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
995 Wsi.setcursor Wsi.CURSOR_INFO
998 let showlinktype under =
999 if conf.underinfo
1000 then
1001 match under with
1002 | Unone -> ()
1003 | under ->
1004 let s = undertext under in
1005 showtext ' ' s
1008 let addchar s c =
1009 let b = Buffer.create (String.length s + 1) in
1010 Buffer.add_string b s;
1011 Buffer.add_char b c;
1012 Buffer.contents b;
1015 let colorspace_of_string s =
1016 match String.lowercase s with
1017 | "rgb" -> Rgb
1018 | "bgr" -> Bgr
1019 | "gray" -> Gray
1020 | _ -> failwith "invalid colorspace"
1023 let int_of_colorspace = function
1024 | Rgb -> 0
1025 | Bgr -> 1
1026 | Gray -> 2
1029 let colorspace_of_int = function
1030 | 0 -> Rgb
1031 | 1 -> Bgr
1032 | 2 -> Gray
1033 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1036 let colorspace_to_string = function
1037 | Rgb -> "rgb"
1038 | Bgr -> "bgr"
1039 | Gray -> "gray"
1042 let fitmodel_of_string s =
1043 match String.lowercase s with
1044 | "width" -> FitWidth
1045 | "proportional" -> FitProportional
1046 | "page" -> FitPage
1047 | _ -> failwith "invalid fit model"
1050 let int_of_fitmodel = function
1051 | FitWidth -> 0
1052 | FitProportional -> 1
1053 | FitPage -> 2
1056 let fitmodel_of_int = function
1057 | 0 -> FitWidth
1058 | 1 -> FitProportional
1059 | 2 -> FitPage
1060 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1063 let fitmodel_to_string = function
1064 | FitWidth -> "width"
1065 | FitProportional -> "proportional"
1066 | FitPage -> "page"
1069 let intentry_with_suffix text key =
1070 let c =
1071 if key >= 32 && key < 127
1072 then Char.chr key
1073 else '\000'
1075 match Char.lowercase c with
1076 | '0' .. '9' ->
1077 let text = addchar text c in
1078 TEcont text
1080 | 'k' | 'm' | 'g' ->
1081 let text = addchar text c in
1082 TEcont text
1084 | _ ->
1085 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1086 TEcont text
1089 let multicolumns_to_string (n, a, b) =
1090 if a = 0 && b = 0
1091 then Printf.sprintf "%d" n
1092 else Printf.sprintf "%d,%d,%d" n a b;
1095 let multicolumns_of_string s =
1097 (int_of_string s, 0, 0)
1098 with _ ->
1099 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1100 if a > 1 || b > 1
1101 then failwith "subtly broken"; (n, a, b)
1105 let readcmd fd =
1106 let s = "xxxx" in
1107 let n = tempfailureretry (Unix.read fd s 0) 4 in
1108 if n != 4 then failwith "incomplete read(len)";
1109 let len = 0
1110 lor (Char.code s.[0] lsl 24)
1111 lor (Char.code s.[1] lsl 16)
1112 lor (Char.code s.[2] lsl 8)
1113 lor (Char.code s.[3] lsl 0)
1115 let s = String.create len in
1116 let n = tempfailureretry (Unix.read fd s 0) len in
1117 if n != len then failwith "incomplete read(data)";
1121 let btod b = if b then 1 else 0;;
1123 let wcmd fmt =
1124 let b = Buffer.create 16 in
1125 Buffer.add_string b "llll";
1126 Printf.kbprintf
1127 (fun b ->
1128 let s = Buffer.contents b in
1129 let n = String.length s in
1130 let len = n - 4 in
1131 (* dolog "wcmd %S" (String.sub s 4 len); *)
1132 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1133 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1134 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1135 s.[3] <- Char.chr (len land 0xff);
1136 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1137 if n' != n then failwith "write failed";
1138 ) b fmt;
1141 let calcips h =
1142 let d = state.winh - h in
1143 max conf.interpagespace ((d + 1) / 2)
1146 let rowyh (c, coverA, coverB) b n =
1147 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1148 then
1149 let _, _, vy, (_, _, h, _) = b.(n) in
1150 (vy, h)
1151 else
1152 let n' = n - coverA in
1153 let d = n' mod c in
1154 let s = n - d in
1155 let e = min state.pagecount (s + c) in
1156 let rec find m miny maxh = if m = e then miny, maxh else
1157 let _, _, y, (_, _, h, _) = b.(m) in
1158 let miny = min miny y in
1159 let maxh = max maxh h in
1160 find (m+1) miny maxh
1161 in find s max_int 0
1164 let calcheight () =
1165 match conf.columns with
1166 | Cmulti ((_, _, _) as cl, b) ->
1167 if Array.length b > 0
1168 then
1169 let y, h = rowyh cl b (Array.length b - 1) in
1170 y + h + (if conf.presentation then calcips h else 0)
1171 else 0
1172 | Csingle b ->
1173 if Array.length b > 0
1174 then
1175 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1176 y + h + (if conf.presentation then calcips h else 0)
1177 else 0
1178 | Csplit (_, b) ->
1179 if Array.length b > 0
1180 then
1181 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1182 y + h
1183 else 0
1186 let getpageyh pageno =
1187 let pageno = bound pageno 0 (state.pagecount-1) in
1188 match conf.columns with
1189 | Csingle b ->
1190 if Array.length b = 0
1191 then 0, 0
1192 else
1193 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1194 let y =
1195 if conf.presentation
1196 then y - calcips h
1197 else y
1199 y, h
1200 | Cmulti (cl, b) ->
1201 if Array.length b = 0
1202 then 0, 0
1203 else
1204 let y, h = rowyh cl b pageno in
1205 let y =
1206 if conf.presentation
1207 then y - calcips h
1208 else y
1210 y, h
1211 | Csplit (c, b) ->
1212 if Array.length b = 0
1213 then 0, 0
1214 else
1215 let n = pageno*c in
1216 let (_, _, y, (_, _, h, _)) = b.(n) in
1217 y, h
1220 let getpagedim pageno =
1221 let rec f ppdim l =
1222 match l with
1223 | (n, _, _, _) as pdim :: rest ->
1224 if n >= pageno
1225 then (if n = pageno then pdim else ppdim)
1226 else f pdim rest
1228 | [] -> ppdim
1230 f (-1, -1, -1, -1) state.pdims
1233 let getpagey pageno = fst (getpageyh pageno);;
1235 let nogeomcmds cmds =
1236 match cmds with
1237 | s, [] -> String.length s = 0
1238 | _ -> false
1241 let page_of_y y =
1242 let ((c, coverA, coverB) as cl), b =
1243 match conf.columns with
1244 | Csingle b -> (1, 0, 0), b
1245 | Cmulti (c, b) -> c, b
1246 | Csplit (_, b) -> (1, 0, 0), b
1248 if Array.length b = 0
1249 then -1
1250 else
1251 let rec bsearch nmin nmax =
1252 if nmin > nmax
1253 then bound nmin 0 (state.pagecount-1)
1254 else
1255 let n = (nmax + nmin) / 2 in
1256 let vy, h = rowyh cl b n in
1257 let y0, y1 =
1258 if conf.presentation
1259 then
1260 let ips = calcips h in
1261 let y0 = vy - ips in
1262 let y1 = vy + h + ips in
1263 y0, y1
1264 else (
1265 if n = 0
1266 then 0, vy + h + conf.interpagespace
1267 else
1268 let y0 = vy - conf.interpagespace in
1269 y0, y0 + h + conf.interpagespace
1272 if y >= y0 && y < y1
1273 then (
1274 if c = 1
1275 then n
1276 else (
1277 if n > coverA
1278 then
1279 if n < state.pagecount - coverB
1280 then ((n-coverA)/c)*c + coverA
1281 else n
1282 else n
1285 else (
1286 if y > y0
1287 then bsearch (n+1) nmax
1288 else bsearch nmin (n-1)
1291 let r = bsearch 0 (state.pagecount-1) in
1295 let layoutN ((columns, coverA, coverB), b) y sh =
1296 let sh = sh - (hscrollh ()) in
1297 let rec fold accu n =
1298 if n = Array.length b
1299 then accu
1300 else
1301 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1302 if (vy - y) > sh &&
1303 (n = coverA - 1
1304 || n = state.pagecount - coverB
1305 || (n - coverA) mod columns = columns - 1)
1306 then accu
1307 else
1308 let accu =
1309 if vy + h > y
1310 then
1311 let pagey = max 0 (y - vy) in
1312 let pagedispy = if pagey > 0 then 0 else vy - y in
1313 let pagedispx, pagex =
1314 let pdx =
1315 if n = coverA - 1 || n = state.pagecount - coverB
1316 then state.x + (wadjsb state.winw - w) / 2
1317 else dx + xoff + state.x
1319 if pdx < 0
1320 then 0, -pdx
1321 else pdx, 0
1323 let pagevw =
1324 let vw = wadjsb state.winw - pagedispx in
1325 let pw = w - pagex in
1326 min vw pw
1328 let pagevh = min (h - pagey) (sh - pagedispy) in
1329 if pagevw > 0 && pagevh > 0
1330 then
1331 let e =
1332 { pageno = n
1333 ; pagedimno = pdimno
1334 ; pagew = w
1335 ; pageh = h
1336 ; pagex = pagex
1337 ; pagey = pagey
1338 ; pagevw = pagevw
1339 ; pagevh = pagevh
1340 ; pagedispx = pagedispx
1341 ; pagedispy = pagedispy
1342 ; pagecol = 0
1345 e :: accu
1346 else
1347 accu
1348 else
1349 accu
1351 fold accu (n+1)
1353 List.rev (fold [] (page_of_y y));
1356 let layoutS (columns, b) y sh =
1357 let sh = sh - hscrollh () in
1358 let rec fold accu n =
1359 if n = Array.length b
1360 then accu
1361 else
1362 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1363 if (vy - y) > sh
1364 then accu
1365 else
1366 let accu =
1367 if vy + pageh > y
1368 then
1369 let x = xoff + state.x in
1370 let pagey = max 0 (y - vy) in
1371 let pagedispy = if pagey > 0 then 0 else vy - y in
1372 let pagedispx, pagex =
1373 if px = 0
1374 then (
1375 if x < 0
1376 then 0, -x
1377 else x, 0
1379 else (
1380 let px = px - x in
1381 if px < 0
1382 then -px, 0
1383 else 0, px
1386 let pagecolw = pagew/columns in
1387 let pagedispx =
1388 if pagecolw < state.winw
1389 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1390 else pagedispx
1392 let pagevw =
1393 let vw = wadjsb state.winw - pagedispx in
1394 let pw = pagew - pagex in
1395 min vw pw
1397 let pagevw = min pagevw pagecolw in
1398 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1399 if pagevw > 0 && pagevh > 0
1400 then
1401 let e =
1402 { pageno = n/columns
1403 ; pagedimno = pdimno
1404 ; pagew = pagew
1405 ; pageh = pageh
1406 ; pagex = pagex
1407 ; pagey = pagey
1408 ; pagevw = pagevw
1409 ; pagevh = pagevh
1410 ; pagedispx = pagedispx
1411 ; pagedispy = pagedispy
1412 ; pagecol = n mod columns
1415 e :: accu
1416 else
1417 accu
1418 else
1419 accu
1421 fold accu (n+1)
1423 List.rev (fold [] 0)
1426 let layout y sh =
1427 if nogeomcmds state.geomcmds
1428 then
1429 match conf.columns with
1430 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1431 | Cmulti c -> layoutN c y sh
1432 | Csplit s -> layoutS s y sh
1433 else []
1436 let clamp incr =
1437 let y = state.y + incr in
1438 let y = max 0 y in
1439 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1443 let itertiles l f =
1444 let tilex = l.pagex mod conf.tilew in
1445 let tiley = l.pagey mod conf.tileh in
1447 let col = l.pagex / conf.tilew in
1448 let row = l.pagey / conf.tileh in
1450 let rec rowloop row y0 dispy h =
1451 if h = 0
1452 then ()
1453 else (
1454 let dh = conf.tileh - y0 in
1455 let dh = min h dh in
1456 let rec colloop col x0 dispx w =
1457 if w = 0
1458 then ()
1459 else (
1460 let dw = conf.tilew - x0 in
1461 let dw = min w dw in
1463 f col row dispx dispy x0 y0 dw dh;
1464 colloop (col+1) 0 (dispx+dw) (w-dw)
1467 colloop col tilex l.pagedispx l.pagevw;
1468 rowloop (row+1) 0 (dispy+dh) (h-dh)
1471 if l.pagevw > 0 && l.pagevh > 0
1472 then rowloop row tiley l.pagedispy l.pagevh;
1475 let gettileopaque l col row =
1476 let key =
1477 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1479 try Some (Hashtbl.find state.tilemap key)
1480 with Not_found -> None
1483 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1484 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1485 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1488 let drawtiles l color =
1489 GlDraw.color color;
1490 let f col row x y tilex tiley w h =
1491 match gettileopaque l col row with
1492 | Some (opaque, _, t) ->
1493 let params = x, y, w, h, tilex, tiley in
1494 if conf.invert
1495 then (
1496 Gl.enable `blend;
1497 GlFunc.blend_func `zero `one_minus_src_color;
1499 drawtile params opaque;
1500 if conf.invert
1501 then Gl.disable `blend;
1502 if conf.debug
1503 then (
1504 let s = Printf.sprintf
1505 "%d[%d,%d] %f sec"
1506 l.pageno col row t
1508 let w = measurestr fstate.fontsize s in
1509 GlMisc.push_attrib [`current];
1510 GlDraw.color (0.0, 0.0, 0.0);
1511 GlDraw.rect
1512 (float (x-2), float (y-2))
1513 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1514 GlDraw.color (1.0, 1.0, 1.0);
1515 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1516 GlMisc.pop_attrib ();
1519 | _ ->
1520 let w =
1521 let lw = wadjsb state.winw - x in
1522 min lw w
1523 and h =
1524 let lh = state.winh - y in
1525 min lh h
1527 begin match state.texid with
1528 | Some id ->
1529 Gl.enable `texture_2d;
1530 GlTex.bind_texture `texture_2d id;
1531 let x0 = float x
1532 and y0 = float y
1533 and x1 = float (x+w)
1534 and y1 = float (y+h) in
1536 let tw = float w /. 16.0
1537 and th = float h /. 16.0 in
1538 let tx0 = float tilex /. 16.0
1539 and ty0 = float tiley /. 16.0 in
1540 let tx1 = tx0 +. tw
1541 and ty1 = ty0 +. th in
1542 GlDraw.begins `quads;
1543 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1544 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1545 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1546 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1547 GlDraw.ends ();
1549 Gl.disable `texture_2d;
1550 | None ->
1551 GlDraw.color (1.0, 1.0, 1.0);
1552 GlDraw.rect
1553 (float x, float y)
1554 (float (x+w), float (y+h));
1555 end;
1556 if w > 128 && h > fstate.fontsize + 10
1557 then (
1558 GlDraw.color (0.0, 0.0, 0.0);
1559 let c, r =
1560 if conf.verbose
1561 then (col*conf.tilew, row*conf.tileh)
1562 else col, row
1564 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1566 GlDraw.color color;
1568 itertiles l f
1571 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1573 let tilevisible1 l x y =
1574 let ax0 = l.pagex
1575 and ax1 = l.pagex + l.pagevw
1576 and ay0 = l.pagey
1577 and ay1 = l.pagey + l.pagevh in
1579 let bx0 = x
1580 and by0 = y in
1581 let bx1 = min (bx0 + conf.tilew) l.pagew
1582 and by1 = min (by0 + conf.tileh) l.pageh in
1584 let rx0 = max ax0 bx0
1585 and ry0 = max ay0 by0
1586 and rx1 = min ax1 bx1
1587 and ry1 = min ay1 by1 in
1589 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1590 nonemptyintersection
1593 let tilevisible layout n x y =
1594 let rec findpageinlayout m = function
1595 | l :: rest when l.pageno = n ->
1596 tilevisible1 l x y || (
1597 match conf.columns with
1598 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1599 | _ -> false
1601 | _ :: rest -> findpageinlayout 0 rest
1602 | [] -> false
1604 findpageinlayout 0 layout;
1607 let tileready l x y =
1608 tilevisible1 l x y &&
1609 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1612 let tilepage n p layout =
1613 let rec loop = function
1614 | l :: rest ->
1615 if l.pageno = n
1616 then
1617 let f col row _ _ _ _ _ _ =
1618 if state.currently = Idle
1619 then
1620 match gettileopaque l col row with
1621 | Some _ -> ()
1622 | None ->
1623 let x = col*conf.tilew
1624 and y = row*conf.tileh in
1625 let w =
1626 let w = l.pagew - x in
1627 min w conf.tilew
1629 let h =
1630 let h = l.pageh - y in
1631 min h conf.tileh
1633 let pbo =
1634 if conf.usepbo
1635 then getpbo w h conf.colorspace
1636 else "0"
1638 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1639 state.currently <-
1640 Tiling (
1641 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1642 conf.tilew, conf.tileh
1645 itertiles l f;
1646 else
1647 loop rest
1649 | [] -> ()
1651 if nogeomcmds state.geomcmds
1652 then loop layout;
1655 let preloadlayout y =
1656 let y = if y < state.winh then 0 else y - state.winh in
1657 let h = state.winh*3 in
1658 layout y h;
1661 let load pages =
1662 let rec loop pages =
1663 if state.currently != Idle
1664 then ()
1665 else
1666 match pages with
1667 | l :: rest ->
1668 begin match getopaque l.pageno with
1669 | None ->
1670 wcmd "page %d %d" l.pageno l.pagedimno;
1671 state.currently <- Loading (l, state.gen);
1672 | Some opaque ->
1673 tilepage l.pageno opaque pages;
1674 loop rest
1675 end;
1676 | _ -> ()
1678 if nogeomcmds state.geomcmds
1679 then loop pages
1682 let preload pages =
1683 load pages;
1684 if conf.preload && state.currently = Idle
1685 then load (preloadlayout state.y);
1688 let layoutready layout =
1689 let rec fold all ls =
1690 all && match ls with
1691 | l :: rest ->
1692 let seen = ref false in
1693 let allvisible = ref true in
1694 let foo col row _ _ _ _ _ _ =
1695 seen := true;
1696 allvisible := !allvisible &&
1697 begin match gettileopaque l col row with
1698 | Some _ -> true
1699 | None -> false
1702 itertiles l foo;
1703 fold (!seen && !allvisible) rest
1704 | [] -> true
1706 let alltilesvisible = fold true layout in
1707 alltilesvisible;
1710 let gotoy y =
1711 let y = bound y 0 state.maxy in
1712 let y, layout, proceed =
1713 match conf.maxwait with
1714 | Some time when state.ghyll == noghyll ->
1715 begin match state.throttle with
1716 | None ->
1717 let layout = layout y state.winh in
1718 let ready = layoutready layout in
1719 if not ready
1720 then (
1721 load layout;
1722 state.throttle <- Some (layout, y, now ());
1724 else G.postRedisplay "gotoy showall (None)";
1725 y, layout, ready
1726 | Some (_, _, started) ->
1727 let dt = now () -. started in
1728 if dt > time
1729 then (
1730 state.throttle <- None;
1731 let layout = layout y state.winh in
1732 load layout;
1733 G.postRedisplay "maxwait";
1734 y, layout, true
1736 else -1, [], false
1739 | _ ->
1740 let layout = layout y state.winh in
1741 if not !wtmode || layoutready layout
1742 then G.postRedisplay "gotoy ready";
1743 y, layout, true
1745 if proceed
1746 then (
1747 state.y <- y;
1748 state.layout <- layout;
1749 begin match state.mode with
1750 | LinkNav (Ltexact (pageno, linkno)) ->
1751 let rec loop = function
1752 | [] ->
1753 state.mode <- LinkNav (Ltgendir 0)
1754 | l :: _ when l.pageno = pageno ->
1755 begin match getopaque pageno with
1756 | None ->
1757 state.mode <- LinkNav (Ltgendir 0)
1758 | Some opaque ->
1759 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1760 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1761 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1762 then state.mode <- LinkNav (Ltgendir 0)
1764 | _ :: rest -> loop rest
1766 loop layout
1767 | _ -> ()
1768 end;
1769 begin match state.mode with
1770 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1771 if not (pagevisible layout pageno)
1772 then (
1773 match state.layout with
1774 | [] -> ()
1775 | l :: _ ->
1776 state.mode <- Birdseye (
1777 conf, leftx, l.pageno, hooverpageno, anchor
1780 | LinkNav (Ltgendir dir as lt) ->
1781 let linknav =
1782 let rec loop = function
1783 | [] -> lt
1784 | l :: rest ->
1785 match getopaque l.pageno with
1786 | None -> loop rest
1787 | Some opaque ->
1788 let link =
1789 let ld =
1790 if dir = 0
1791 then LDfirstvisible (l.pagex, l.pagey, dir)
1792 else (
1793 if dir > 0 then LDfirst else LDlast
1796 findlink opaque ld
1798 match link with
1799 | Lnotfound -> loop rest
1800 | Lfound n ->
1801 showlinktype (getlink opaque n);
1802 Ltexact (l.pageno, n)
1804 loop state.layout
1806 state.mode <- LinkNav linknav
1807 | _ -> ()
1808 end;
1809 preload layout;
1811 state.ghyll <- noghyll;
1812 if conf.updatecurs
1813 then (
1814 let mx, my = state.mpos in
1815 updateunder mx my;
1819 let conttiling pageno opaque =
1820 tilepage pageno opaque
1821 (if conf.preload then preloadlayout state.y else state.layout)
1824 let gotoy_and_clear_text y =
1825 if not conf.verbose then state.text <- "";
1826 gotoy y;
1829 let getanchor1 l =
1830 let top =
1831 let coloff = l.pagecol * l.pageh in
1832 float (l.pagey + coloff) /. float l.pageh
1834 let dtop =
1835 if l.pagedispy = 0
1836 then
1838 else (
1839 if conf.presentation
1840 then float l.pagedispy /. float (calcips l.pageh)
1841 else float l.pagedispy /. float conf.interpagespace
1844 (l.pageno, top, dtop)
1847 let getanchor () =
1848 match state.layout with
1849 | l :: _ -> getanchor1 l
1850 | [] ->
1851 let n = page_of_y state.y in
1852 if n = -1
1853 then state.anchor
1854 else
1855 let y, h = getpageyh n in
1856 let dy = y - state.y in
1857 let dtop =
1858 if conf.presentation
1859 then
1860 let ips = calcips h in
1861 float (dy + ips) /. float ips
1862 else
1863 float dy /. float conf.interpagespace
1865 (n, 0.0, dtop)
1868 let getanchory (n, top, dtop) =
1869 let y, h = getpageyh n in
1870 if conf.presentation
1871 then
1872 let ips = calcips h in
1873 y + truncate (top*.float h -. dtop*.float ips) + ips;
1874 else
1875 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1878 let gotoanchor anchor =
1879 gotoy (getanchory anchor);
1882 let addnav () =
1883 cbput state.hists.nav (getanchor ());
1886 let getnav dir =
1887 let anchor = cbgetc state.hists.nav dir in
1888 getanchory anchor;
1891 let gotoghyll y =
1892 let scroll f n a b =
1893 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1894 let snake f a b =
1895 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1896 if f < a
1897 then s (float f /. float a)
1898 else (
1899 if f > b
1900 then 1.0 -. s ((float (f-b) /. float (n-b)))
1901 else 1.0
1904 snake f a b
1905 and summa f n a b =
1906 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1907 let iv x = x**3.-.0.5*.x**4. in
1908 let iv1 = iv f in
1909 let ins = float a *. iv1
1910 and outs = float (n-b) *. iv1 in
1911 let ones = b - a in
1912 ins +. outs +. float ones
1914 let rec set (_N, _A, _B) y sy =
1915 let sum = summa 1.0 _N _A _B in
1916 let dy = float (y - sy) in
1917 state.ghyll <- (
1918 let rec gf n y1 o =
1919 if n >= _N
1920 then state.ghyll <- noghyll
1921 else
1922 let go n =
1923 let s = scroll n _N _A _B in
1924 let y1 = y1 +. ((s *. dy) /. sum) in
1925 gotoy_and_clear_text (truncate y1);
1926 state.ghyll <- gf (n+1) y1;
1928 match o with
1929 | None -> go n
1930 | Some y' -> set (_N/2, 1, 1) y' state.y
1932 gf 0 (float state.y)
1935 match conf.ghyllscroll with
1936 | None ->
1937 gotoy_and_clear_text y
1938 | Some nab ->
1939 if state.ghyll == noghyll
1940 then set nab y state.y
1941 else state.ghyll (Some y)
1944 let gotopage n top =
1945 let y, h = getpageyh n in
1946 let y = y + (truncate (top *. float h)) in
1947 gotoghyll y
1950 let gotopage1 n top =
1951 let y = getpagey n in
1952 let y = y + top in
1953 gotoghyll y
1956 let invalidate s f =
1957 state.layout <- [];
1958 state.pdims <- [];
1959 state.rects <- [];
1960 state.rects1 <- [];
1961 match state.geomcmds with
1962 | ps, [] when String.length ps = 0 ->
1963 f ();
1964 state.geomcmds <- s, [];
1966 | ps, [] ->
1967 state.geomcmds <- ps, [s, f];
1969 | ps, (s', _) :: rest when s' = s ->
1970 state.geomcmds <- ps, ((s, f) :: rest);
1972 | ps, cmds ->
1973 state.geomcmds <- ps, ((s, f) :: cmds);
1976 let flushpages () =
1977 Hashtbl.iter (fun _ opaque ->
1978 wcmd "freepage %s" opaque;
1979 ) state.pagemap;
1980 Hashtbl.clear state.pagemap;
1983 let flushtiles () =
1984 if not (Queue.is_empty state.tilelru)
1985 then (
1986 Queue.iter (fun (k, p, s) ->
1987 wcmd "freetile %s" p;
1988 state.memused <- state.memused - s;
1989 Hashtbl.remove state.tilemap k;
1990 ) state.tilelru;
1991 state.uioh#infochanged Memused;
1992 Queue.clear state.tilelru;
1994 load state.layout;
1997 let stateh h =
1998 let h = truncate (float h*.conf.zoom) in
1999 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2000 h - d
2003 let opendoc path password =
2004 state.path <- path;
2005 state.password <- password;
2006 state.gen <- state.gen + 1;
2007 state.docinfo <- [];
2009 flushpages ();
2010 setaalevel conf.aalevel;
2011 let titlepath =
2012 if String.length state.origin = 0
2013 then path
2014 else state.origin
2016 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2017 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2018 invalidate "reqlayout"
2019 (fun () ->
2020 wcmd "reqlayout %d %d %d %s\000"
2021 conf.angle (int_of_fitmodel conf.fitmodel)
2022 (stateh state.winh) state.nameddest
2026 let reload () =
2027 state.anchor <- getanchor ();
2028 opendoc state.path state.password;
2031 let scalecolor c =
2032 let c = c *. conf.colorscale in
2033 (c, c, c);
2036 let scalecolor2 (r, g, b) =
2037 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2040 let docolumns = function
2041 | Csingle _ ->
2042 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2043 let rec loop pageno pdimno pdim y ph pdims =
2044 if pageno = state.pagecount
2045 then ()
2046 else
2047 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2048 match pdims with
2049 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2050 pdimno+1, pdim, rest
2051 | _ ->
2052 pdimno, pdim, pdims
2054 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2055 let y = y +
2056 (if conf.presentation
2057 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2058 else (if pageno = 0 then 0 else conf.interpagespace)
2061 a.(pageno) <- (pdimno, x, y, pdim);
2062 loop (pageno+1) pdimno pdim (y + h) h pdims
2064 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2065 conf.columns <- Csingle a;
2067 | Cmulti ((columns, coverA, coverB), _) ->
2068 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2069 let rec loop pageno pdimno pdim x y rowh pdims =
2070 let rec fixrow m = if m = pageno then () else
2071 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2072 if h < rowh
2073 then (
2074 let y = y + (rowh - h) / 2 in
2075 a.(m) <- (pdimno, x, y, pdim);
2077 fixrow (m+1)
2079 if pageno = state.pagecount
2080 then fixrow (((pageno - 1) / columns) * columns)
2081 else
2082 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2083 match pdims with
2084 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2085 pdimno+1, pdim, rest
2086 | _ ->
2087 pdimno, pdim, pdims
2089 let x, y, rowh' =
2090 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2091 then (
2092 let x = (wadjsb state.winw - w) / 2 in
2093 let ips =
2094 if conf.presentation then calcips h else conf.interpagespace in
2095 x, y + ips + rowh, h
2097 else (
2098 if (pageno - coverA) mod columns = 0
2099 then (
2100 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2101 let y =
2102 if conf.presentation
2103 then
2104 let ips = calcips h in
2105 y + (if pageno = 0 then 0 else calcips rowh + ips)
2106 else
2107 y + (if pageno = 0 then 0 else conf.interpagespace)
2109 x, y + rowh, h
2111 else x, y, max rowh h
2114 let y =
2115 if pageno > 1 && (pageno - coverA) mod columns = 0
2116 then (
2117 let y =
2118 if pageno = columns && conf.presentation
2119 then (
2120 let ips = calcips rowh in
2121 for i = 0 to pred columns
2123 let (pdimno, x, y, pdim) = a.(i) in
2124 a.(i) <- (pdimno, x, y+ips, pdim)
2125 done;
2126 y+ips;
2128 else y
2130 fixrow (pageno - columns);
2133 else y
2135 a.(pageno) <- (pdimno, x, y, pdim);
2136 let x = x + w + xoff*2 + conf.interpagespace in
2137 loop (pageno+1) pdimno pdim x y rowh' pdims
2139 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2140 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2142 | Csplit (c, _) ->
2143 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2144 let rec loop pageno pdimno pdim y pdims =
2145 if pageno = state.pagecount
2146 then ()
2147 else
2148 let pdimno, ((_, w, h, _) as pdim), pdims =
2149 match pdims with
2150 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2151 pdimno+1, pdim, rest
2152 | _ ->
2153 pdimno, pdim, pdims
2155 let cw = w / c in
2156 let rec loop1 n x y =
2157 if n = c then y else (
2158 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2159 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2162 let y = loop1 0 0 y in
2163 loop (pageno+1) pdimno pdim y pdims
2165 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2166 conf.columns <- Csplit (c, a);
2169 let represent () =
2170 docolumns conf.columns;
2171 state.maxy <- calcheight ();
2172 if state.reprf == noreprf
2173 then (
2174 match state.mode with
2175 | Birdseye (_, _, pageno, _, _) ->
2176 let y, h = getpageyh pageno in
2177 let top = (state.winh - h) / 2 in
2178 gotoy (max 0 (y - top))
2179 | _ -> gotoanchor state.anchor
2181 else (
2182 state.reprf ();
2183 state.reprf <- noreprf;
2187 let reshape w h =
2188 GlDraw.viewport 0 0 w h;
2189 let firsttime = state.geomcmds == firstgeomcmds in
2190 if not firsttime && nogeomcmds state.geomcmds
2191 then state.anchor <- getanchor ();
2193 state.winw <- w;
2194 let w = wadjsb (truncate (float w *. conf.zoom)) in
2195 let w = max w 2 in
2196 state.winh <- h;
2197 setfontsize fstate.fontsize;
2198 GlMat.mode `modelview;
2199 GlMat.load_identity ();
2201 GlMat.mode `projection;
2202 GlMat.load_identity ();
2203 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2204 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2205 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2207 let relx =
2208 if conf.zoom <= 1.0
2209 then 0.0
2210 else float state.x /. float state.w
2212 invalidate "geometry"
2213 (fun () ->
2214 state.w <- w;
2215 if not firsttime
2216 then state.x <- truncate (relx *. float w);
2217 let w =
2218 match conf.columns with
2219 | Csingle _ -> w
2220 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2221 | Csplit (c, _) -> w * c
2223 wcmd "geometry %d %d %d"
2224 w (stateh h) (int_of_fitmodel conf.fitmodel)
2228 let enttext () =
2229 let len = String.length state.text in
2230 let drawstring s =
2231 let hscrollh =
2232 match state.mode with
2233 | Textentry _ | View | LinkNav _ ->
2234 let h, _, _ = state.uioh#scrollpw in
2236 | _ -> 0
2238 let rect x w =
2239 GlDraw.rect
2240 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2241 (x+.w, float (state.winh - hscrollh))
2244 let w = float (wadjsb state.winw - 1) in
2245 if state.progress >= 0.0 && state.progress < 1.0
2246 then (
2247 GlDraw.color (0.3, 0.3, 0.3);
2248 let w1 = w *. state.progress in
2249 rect 0.0 w1;
2250 GlDraw.color (0.0, 0.0, 0.0);
2251 rect w1 (w-.w1)
2253 else (
2254 GlDraw.color (0.0, 0.0, 0.0);
2255 rect 0.0 w;
2258 GlDraw.color (1.0, 1.0, 1.0);
2259 drawstring fstate.fontsize
2260 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2262 let s =
2263 match state.mode with
2264 | Textentry ((prefix, text, _, _, _, _), _) ->
2265 let s =
2266 if len > 0
2267 then
2268 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2269 else
2270 Printf.sprintf "%s%s_" prefix text
2274 | _ -> state.text
2276 let s =
2277 if state.newerrmsgs
2278 then (
2279 if not (istextentry state.mode) && state.uioh#eformsgs
2280 then
2281 let s1 = "(press 'e' to review error messasges)" in
2282 if String.length s > 0 then s ^ " " ^ s1 else s1
2283 else s
2285 else s
2287 if String.length s > 0
2288 then drawstring s
2291 let gctiles () =
2292 let len = Queue.length state.tilelru in
2293 let layout = lazy (
2294 match state.throttle with
2295 | None ->
2296 if conf.preload
2297 then preloadlayout state.y
2298 else state.layout
2299 | Some (layout, _, _) ->
2300 layout
2301 ) in
2302 let rec loop qpos =
2303 if state.memused <= conf.memlimit
2304 then ()
2305 else (
2306 if qpos < len
2307 then
2308 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2309 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2310 let (_, pw, ph, _) = getpagedim n in
2312 gen = state.gen
2313 && colorspace = conf.colorspace
2314 && angle = conf.angle
2315 && pagew = pw
2316 && pageh = ph
2317 && (
2318 let x = col*conf.tilew
2319 and y = row*conf.tileh in
2320 tilevisible (Lazy.force_val layout) n x y
2322 then Queue.push lruitem state.tilelru
2323 else (
2324 freepbo p;
2325 wcmd "freetile %s" p;
2326 state.memused <- state.memused - s;
2327 state.uioh#infochanged Memused;
2328 Hashtbl.remove state.tilemap k;
2330 loop (qpos+1)
2333 loop 0
2336 let logcurrently = function
2337 | Idle -> dolog "Idle"
2338 | Loading (l, gen) ->
2339 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2340 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2341 dolog
2342 "Tiling %d[%d,%d] page=%s cs=%s angle"
2343 l.pageno col row pageopaque
2344 (colorspace_to_string colorspace)
2346 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2347 angle gen conf.angle state.gen
2348 tilew tileh
2349 conf.tilew conf.tileh
2351 | Outlining _ ->
2352 dolog "outlining"
2355 let splitatspace =
2356 let r = Str.regexp " " in
2357 fun s -> Str.bounded_split r s 2;
2360 let onpagerect pageno f =
2361 let b =
2362 match conf.columns with
2363 | Cmulti (_, b) -> b
2364 | Csingle b -> b
2365 | Csplit (_, b) -> b
2367 if pageno >= 0 && pageno < Array.length b
2368 then
2369 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2370 let r = getpdimrect pdimno in
2371 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2374 let gotopagexy1 pageno x y =
2375 onpagerect pageno (fun w h ->
2376 let top = y /. h in
2377 let _,w1,_,leftx = getpagedim pageno in
2378 let wh = state.winh - hscrollh () in
2379 let sw = float w1 /. w in
2380 let x = sw *. x in
2381 let x = leftx + state.x + truncate x in
2382 let sx =
2383 if x < 0 || x >= wadjsb state.winw
2384 then state.x - x
2385 else state.x
2387 let py, h = getpageyh pageno in
2388 let pdy = truncate (top *. float h) in
2389 let y' = py + pdy in
2390 let dy = y' - state.y in
2391 let sy =
2392 if x != state.x || not (dy > 0 && dy < wh)
2393 then (
2394 if conf.presentation
2395 then
2396 if abs (py - y') > wh
2397 then y'
2398 else py
2399 else y';
2401 else state.y
2403 if state.x != sx || state.y != sy
2404 then (
2405 let x, y =
2406 if !wtmode
2407 then (
2408 let ww = wadjsb state.winw in
2409 let qx = sx / ww
2410 and qy = pdy / wh in
2411 let x = qx * ww
2412 and y = py + qy * wh in
2413 let x = if -x + ww > w1 then -(w1-ww) else x
2414 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2415 let y =
2416 if conf.presentation
2417 then
2418 if abs (py - y') > wh
2419 then y'
2420 else py
2421 else y';
2423 (x, y)
2425 else (sx, sy)
2427 state.x <- x;
2428 gotoy_and_clear_text y;
2430 else gotoy_and_clear_text state.y;
2434 let gotopagexy pageno x y =
2435 match state.mode with
2436 | Birdseye _ -> gotopage pageno 0.0
2437 | _ -> gotopagexy1 pageno x y
2440 let act cmds =
2441 (* dolog "%S" cmds; *)
2442 let cl = splitatspace cmds in
2443 let scan s fmt f =
2444 try Scanf.sscanf s fmt f
2445 with exn ->
2446 dolog "error processing '%S': %s" cmds (exntos exn);
2447 exit 1
2449 match cl with
2450 | "clear" :: [] ->
2451 state.uioh#infochanged Pdim;
2452 state.pdims <- [];
2454 | "clearrects" :: [] ->
2455 state.rects <- state.rects1;
2456 G.postRedisplay "clearrects";
2458 | "continue" :: args :: [] ->
2459 let n = scan args "%u" (fun n -> n) in
2460 state.pagecount <- n;
2461 begin match state.currently with
2462 | Outlining l ->
2463 state.currently <- Idle;
2464 state.outlines <- Array.of_list (List.rev l)
2465 | _ -> ()
2466 end;
2468 let cur, cmds = state.geomcmds in
2469 if String.length cur = 0
2470 then failwith "umpossible";
2472 begin match List.rev cmds with
2473 | [] ->
2474 state.geomcmds <- "", [];
2475 represent ();
2476 | (s, f) :: rest ->
2477 f ();
2478 state.geomcmds <- s, List.rev rest;
2479 end;
2480 if conf.maxwait = None && not !wtmode
2481 then G.postRedisplay "continue";
2483 | "title" :: args :: [] ->
2484 Wsi.settitle args
2486 | "msg" :: args :: [] ->
2487 showtext ' ' args
2489 | "vmsg" :: args :: [] ->
2490 if conf.verbose
2491 then showtext ' ' args
2493 | "emsg" :: args :: [] ->
2494 Buffer.add_string state.errmsgs args;
2495 state.newerrmsgs <- true;
2496 G.postRedisplay "error message"
2498 | "progress" :: args :: [] ->
2499 let progress, text =
2500 scan args "%f %n"
2501 (fun f pos ->
2502 f, String.sub args pos (String.length args - pos))
2504 state.text <- text;
2505 state.progress <- progress;
2506 G.postRedisplay "progress"
2508 | "firstmatch" :: args :: [] ->
2509 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2510 scan args "%u %d %f %f %f %f %f %f %f %f"
2511 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2512 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2514 let y = (getpagey pageno) + truncate y0 in
2515 addnav ();
2516 gotoy y;
2517 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2519 | "match" :: args :: [] ->
2520 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2521 scan args "%u %d %f %f %f %f %f %f %f %f"
2522 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2523 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2525 state.rects1 <-
2526 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2528 | "page" :: args :: [] ->
2529 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2530 begin match state.currently with
2531 | Loading (l, gen) ->
2532 vlog "page %d took %f sec" l.pageno t;
2533 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2534 begin match state.throttle with
2535 | None ->
2536 let preloadedpages =
2537 if conf.preload
2538 then preloadlayout state.y
2539 else state.layout
2541 let evict () =
2542 let set =
2543 List.fold_left (fun s l -> IntSet.add l.pageno s)
2544 IntSet.empty preloadedpages
2546 let evictedpages =
2547 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2548 if not (IntSet.mem pageno set)
2549 then (
2550 wcmd "freepage %s" opaque;
2551 key :: accu
2553 else accu
2554 ) state.pagemap []
2556 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2558 evict ();
2559 state.currently <- Idle;
2560 if gen = state.gen
2561 then (
2562 tilepage l.pageno pageopaque state.layout;
2563 load state.layout;
2564 load preloadedpages;
2565 if pagevisible state.layout l.pageno
2566 && layoutready state.layout
2567 then G.postRedisplay "page";
2570 | Some (layout, _, _) ->
2571 state.currently <- Idle;
2572 tilepage l.pageno pageopaque layout;
2573 load state.layout
2574 end;
2576 | _ ->
2577 dolog "Inconsistent loading state";
2578 logcurrently state.currently;
2579 exit 1
2582 | "tile" :: args :: [] ->
2583 let (x, y, opaque, size, t) =
2584 scan args "%u %u %s %u %f"
2585 (fun x y p size t -> (x, y, p, size, t))
2587 begin match state.currently with
2588 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2589 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2591 unmappbo opaque;
2592 if tilew != conf.tilew || tileh != conf.tileh
2593 then (
2594 wcmd "freetile %s" opaque;
2595 state.currently <- Idle;
2596 load state.layout;
2598 else (
2599 puttileopaque l col row gen cs angle opaque size t;
2600 state.memused <- state.memused + size;
2601 state.uioh#infochanged Memused;
2602 gctiles ();
2603 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2604 opaque, size) state.tilelru;
2606 let layout =
2607 match state.throttle with
2608 | None -> state.layout
2609 | Some (layout, _, _) -> layout
2612 state.currently <- Idle;
2613 if gen = state.gen
2614 && conf.colorspace = cs
2615 && conf.angle = angle
2616 && tilevisible layout l.pageno x y
2617 then conttiling l.pageno pageopaque;
2619 begin match state.throttle with
2620 | None ->
2621 preload state.layout;
2622 if gen = state.gen
2623 && conf.colorspace = cs
2624 && conf.angle = angle
2625 && tilevisible state.layout l.pageno x y
2626 && (not !wtmode || layoutready state.layout)
2627 then G.postRedisplay "tile nothrottle";
2629 | Some (layout, y, _) ->
2630 let ready = layoutready layout in
2631 if ready
2632 then (
2633 state.y <- y;
2634 state.layout <- layout;
2635 state.throttle <- None;
2636 G.postRedisplay "throttle";
2638 else load layout;
2639 end;
2642 | _ ->
2643 dolog "Inconsistent tiling state";
2644 logcurrently state.currently;
2645 exit 1
2648 | "pdim" :: args :: [] ->
2649 let (n, w, h, _) as pdim =
2650 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2652 let pdim =
2653 match conf.fitmodel, conf.columns with
2654 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2655 | _ -> pdim
2657 state.uioh#infochanged Pdim;
2658 state.pdims <- pdim :: state.pdims
2660 | "o" :: args :: [] ->
2661 let (l, n, t, h, pos) =
2662 scan args "%u %u %d %u %n"
2663 (fun l n t h pos -> l, n, t, h, pos)
2665 let s = String.sub args pos (String.length args - pos) in
2666 let outline = (s, l, (n, float t /. float h, 0.0)) in
2667 begin match state.currently with
2668 | Outlining outlines ->
2669 state.currently <- Outlining (outline :: outlines)
2670 | Idle ->
2671 state.currently <- Outlining [outline]
2672 | currently ->
2673 dolog "invalid outlining state";
2674 logcurrently currently
2677 | "a" :: args :: [] ->
2678 let (n, l, t) =
2679 scan args "%u %d %d" (fun n l t -> n, l, t)
2681 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2683 | "info" :: args :: [] ->
2684 state.docinfo <- (1, args) :: state.docinfo
2686 | "infoend" :: [] ->
2687 state.uioh#infochanged Docinfo;
2688 state.docinfo <- List.rev state.docinfo
2690 | _ ->
2691 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2694 let onhist cb =
2695 let rc = cb.rc in
2696 let action = function
2697 | HCprev -> cbget cb ~-1
2698 | HCnext -> cbget cb 1
2699 | HCfirst -> cbget cb ~-(cb.rc)
2700 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2701 and cancel () = cb.rc <- rc
2702 in (action, cancel)
2705 let search pattern forward =
2706 match conf.columns with
2707 | Csplit _ ->
2708 showtext '!' "searching does not work properly in split columns mode"
2709 | _ ->
2710 if String.length pattern > 0
2711 then
2712 let pn, py =
2713 match state.layout with
2714 | [] -> 0, 0
2715 | l :: _ ->
2716 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2718 wcmd "search %d %d %d %d,%s\000"
2719 (btod conf.icase) pn py (btod forward) pattern;
2722 let intentry text key =
2723 let c =
2724 if key >= 32 && key < 127
2725 then Char.chr key
2726 else '\000'
2728 match c with
2729 | '0' .. '9' ->
2730 let text = addchar text c in
2731 TEcont text
2733 | _ ->
2734 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2735 TEcont text
2738 let linknentry text key =
2739 let c =
2740 if key >= 32 && key < 127
2741 then Char.chr key
2742 else '\000'
2744 match c with
2745 | 'a' .. 'z' ->
2746 let text = addchar text c in
2747 TEcont text
2749 | _ ->
2750 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2751 TEcont text
2754 let linkndone f s =
2755 if String.length s > 0
2756 then (
2757 let n =
2758 let l = String.length s in
2759 let rec loop pos n = if pos = l then n else
2760 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2761 loop (pos+1) (n*26 + m)
2762 in loop 0 0
2764 let rec loop n = function
2765 | [] -> ()
2766 | l :: rest ->
2767 match getopaque l.pageno with
2768 | None -> loop n rest
2769 | Some opaque ->
2770 let m = getlinkcount opaque in
2771 if n < m
2772 then (
2773 let under = getlink opaque n in
2774 f under
2776 else loop (n-m) rest
2778 loop n state.layout;
2782 let textentry text key =
2783 if key land 0xff00 = 0xff00
2784 then TEcont text
2785 else TEcont (text ^ toutf8 key)
2788 let reqlayout angle fitmodel =
2789 match state.throttle with
2790 | None ->
2791 if nogeomcmds state.geomcmds
2792 then state.anchor <- getanchor ();
2793 conf.angle <- angle mod 360;
2794 if conf.angle != 0
2795 then (
2796 match state.mode with
2797 | LinkNav _ -> state.mode <- View
2798 | _ -> ()
2800 conf.fitmodel <- fitmodel;
2801 invalidate "reqlayout"
2802 (fun () ->
2803 wcmd "reqlayout %d %d %d"
2804 conf.angle (int_of_fitmodel conf.fitmodel) (stateh state.winh)
2806 | _ -> ()
2809 let settrim trimmargins trimfuzz =
2810 if nogeomcmds state.geomcmds
2811 then state.anchor <- getanchor ();
2812 conf.trimmargins <- trimmargins;
2813 conf.trimfuzz <- trimfuzz;
2814 let x0, y0, x1, y1 = trimfuzz in
2815 invalidate "settrim"
2816 (fun () ->
2817 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2818 flushpages ();
2821 let setzoom zoom =
2822 match state.throttle with
2823 | None ->
2824 let zoom = max 0.0001 zoom in
2825 if zoom <> conf.zoom
2826 then (
2827 state.prevzoom <- conf.zoom;
2828 conf.zoom <- zoom;
2829 reshape state.winw state.winh;
2830 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2833 | Some (layout, y, started) ->
2834 let time =
2835 match conf.maxwait with
2836 | None -> 0.0
2837 | Some t -> t
2839 let dt = now () -. started in
2840 if dt > time
2841 then (
2842 state.y <- y;
2843 load layout;
2847 let setcolumns mode columns coverA coverB =
2848 state.prevcolumns <- Some (conf.columns, conf.zoom);
2849 if columns < 0
2850 then (
2851 if isbirdseye mode
2852 then showtext '!' "split mode doesn't work in bird's eye"
2853 else (
2854 conf.columns <- Csplit (-columns, [||]);
2855 state.x <- 0;
2856 conf.zoom <- 1.0;
2859 else (
2860 if columns < 2
2861 then (
2862 conf.columns <- Csingle [||];
2863 state.x <- 0;
2864 setzoom 1.0;
2866 else (
2867 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2868 conf.zoom <- 1.0;
2871 reshape state.winw state.winh;
2874 let enterbirdseye () =
2875 let zoom = float conf.thumbw /. float state.winw in
2876 let birdseyepageno =
2877 let cy = state.winh / 2 in
2878 let fold = function
2879 | [] -> 0
2880 | l :: rest ->
2881 let rec fold best = function
2882 | [] -> best.pageno
2883 | l :: rest ->
2884 let d = cy - (l.pagedispy + l.pagevh/2)
2885 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2886 if abs d < abs dbest
2887 then fold l rest
2888 else best.pageno
2889 in fold l rest
2891 fold state.layout
2893 state.mode <- Birdseye (
2894 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2896 conf.zoom <- zoom;
2897 conf.presentation <- false;
2898 conf.interpagespace <- 10;
2899 conf.hlinks <- false;
2900 conf.fitmodel <- FitProportional;
2901 state.x <- 0;
2902 state.mstate <- Mnone;
2903 conf.maxwait <- None;
2904 conf.columns <- (
2905 match conf.beyecolumns with
2906 | Some c ->
2907 conf.zoom <- 1.0;
2908 Cmulti ((c, 0, 0), [||])
2909 | None -> Csingle [||]
2911 Wsi.setcursor Wsi.CURSOR_INHERIT;
2912 if conf.verbose
2913 then
2914 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2915 (100.0*.zoom)
2916 else
2917 state.text <- ""
2919 reshape state.winw state.winh;
2922 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2923 state.mode <- View;
2924 conf.zoom <- c.zoom;
2925 conf.presentation <- c.presentation;
2926 conf.interpagespace <- c.interpagespace;
2927 conf.maxwait <- c.maxwait;
2928 conf.hlinks <- c.hlinks;
2929 conf.fitmodel <- c.fitmodel;
2930 conf.beyecolumns <- (
2931 match conf.columns with
2932 | Cmulti ((c, _, _), _) -> Some c
2933 | Csingle _ -> None
2934 | Csplit _ -> failwith "leaving bird's eye split mode"
2936 conf.columns <- (
2937 match c.columns with
2938 | Cmulti (c, _) -> Cmulti (c, [||])
2939 | Csingle _ -> Csingle [||]
2940 | Csplit (c, _) -> Csplit (c, [||])
2942 state.x <- leftx;
2943 if conf.verbose
2944 then
2945 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2946 (100.0*.conf.zoom)
2948 reshape state.winw state.winh;
2949 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2952 let togglebirdseye () =
2953 match state.mode with
2954 | Birdseye vals -> leavebirdseye vals true
2955 | View -> enterbirdseye ()
2956 | _ -> ()
2959 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2960 let pageno = max 0 (pageno - incr) in
2961 let rec loop = function
2962 | [] -> gotopage1 pageno 0
2963 | l :: _ when l.pageno = pageno ->
2964 if l.pagedispy >= 0 && l.pagey = 0
2965 then G.postRedisplay "upbirdseye"
2966 else gotopage1 pageno 0
2967 | _ :: rest -> loop rest
2969 loop state.layout;
2970 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2973 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2974 let pageno = min (state.pagecount - 1) (pageno + incr) in
2975 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2976 let rec loop = function
2977 | [] ->
2978 let y, h = getpageyh pageno in
2979 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2980 gotoy (clamp dy)
2981 | l :: _ when l.pageno = pageno ->
2982 if l.pagevh != l.pageh
2983 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2984 else G.postRedisplay "downbirdseye"
2985 | _ :: rest -> loop rest
2987 loop state.layout
2990 let optentry mode _ key =
2991 let btos b = if b then "on" else "off" in
2992 if key >= 32 && key < 127
2993 then
2994 let c = Char.chr key in
2995 match c with
2996 | 's' ->
2997 let ondone s =
2998 try conf.scrollstep <- int_of_string s with exc ->
2999 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3001 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3003 | 'A' ->
3004 let ondone s =
3006 conf.autoscrollstep <- int_of_string s;
3007 if state.autoscroll <> None
3008 then state.autoscroll <- Some conf.autoscrollstep
3009 with exc ->
3010 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3012 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3014 | 'C' ->
3015 let ondone s =
3017 let n, a, b = multicolumns_of_string s in
3018 setcolumns mode n a b;
3019 with exc ->
3020 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3022 TEswitch ("columns: ", "", None, textentry, ondone, true)
3024 | 'Z' ->
3025 let ondone s =
3027 let zoom = float (int_of_string s) /. 100.0 in
3028 setzoom zoom
3029 with exc ->
3030 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3032 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3034 | 't' ->
3035 let ondone s =
3037 conf.thumbw <- bound (int_of_string s) 2 4096;
3038 state.text <-
3039 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3040 begin match mode with
3041 | Birdseye beye ->
3042 leavebirdseye beye false;
3043 enterbirdseye ();
3044 | _ -> ();
3046 with exc ->
3047 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3049 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3051 | 'R' ->
3052 let ondone s =
3053 match try
3054 Some (int_of_string s)
3055 with exc ->
3056 state.text <- Printf.sprintf "bad integer `%s': %s"
3057 s (exntos exc);
3058 None
3059 with
3060 | Some angle -> reqlayout angle conf.fitmodel
3061 | None -> ()
3063 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3065 | 'i' ->
3066 conf.icase <- not conf.icase;
3067 TEdone ("case insensitive search " ^ (btos conf.icase))
3069 | 'p' ->
3070 conf.preload <- not conf.preload;
3071 gotoy state.y;
3072 TEdone ("preload " ^ (btos conf.preload))
3074 | 'v' ->
3075 conf.verbose <- not conf.verbose;
3076 TEdone ("verbose " ^ (btos conf.verbose))
3078 | 'd' ->
3079 conf.debug <- not conf.debug;
3080 TEdone ("debug " ^ (btos conf.debug))
3082 | 'h' ->
3083 conf.maxhfit <- not conf.maxhfit;
3084 state.maxy <- calcheight ();
3085 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3087 | 'c' ->
3088 conf.crophack <- not conf.crophack;
3089 TEdone ("crophack " ^ btos conf.crophack)
3091 | 'a' ->
3092 let s =
3093 match conf.maxwait with
3094 | None ->
3095 conf.maxwait <- Some infinity;
3096 "always wait for page to complete"
3097 | Some _ ->
3098 conf.maxwait <- None;
3099 "show placeholder if page is not ready"
3101 TEdone s
3103 | 'f' ->
3104 conf.underinfo <- not conf.underinfo;
3105 TEdone ("underinfo " ^ btos conf.underinfo)
3107 | 'P' ->
3108 conf.savebmarks <- not conf.savebmarks;
3109 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3111 | 'S' ->
3112 let ondone s =
3114 let pageno, py =
3115 match state.layout with
3116 | [] -> 0, 0
3117 | l :: _ ->
3118 l.pageno, l.pagey
3120 conf.interpagespace <- int_of_string s;
3121 docolumns conf.columns;
3122 state.maxy <- calcheight ();
3123 let y = getpagey pageno in
3124 gotoy (y + py)
3125 with exc ->
3126 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3128 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3130 | 'l' ->
3131 let fm =
3132 match conf.fitmodel with
3133 | FitProportional -> FitWidth
3134 | _ -> FitProportional
3136 reqlayout conf.angle fm;
3137 TEdone ("proportional display " ^ btos (fm == FitProportional))
3139 | 'T' ->
3140 settrim (not conf.trimmargins) conf.trimfuzz;
3141 TEdone ("trim margins " ^ btos conf.trimmargins)
3143 | 'I' ->
3144 conf.invert <- not conf.invert;
3145 TEdone ("invert colors " ^ btos conf.invert)
3147 | 'x' ->
3148 let ondone s =
3149 cbput state.hists.sel s;
3150 conf.selcmd <- s;
3152 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3153 textentry, ondone, true)
3155 | _ ->
3156 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3157 TEstop
3158 else
3159 TEcont state.text
3162 class type lvsource = object
3163 method getitemcount : int
3164 method getitem : int -> (string * int)
3165 method hasaction : int -> bool
3166 method exit :
3167 uioh:uioh ->
3168 cancel:bool ->
3169 active:int ->
3170 first:int ->
3171 pan:int ->
3172 qsearch:string ->
3173 uioh option
3174 method getactive : int
3175 method getfirst : int
3176 method getqsearch : string
3177 method setqsearch : string -> unit
3178 method getpan : int
3179 end;;
3181 class virtual lvsourcebase = object
3182 val mutable m_active = 0
3183 val mutable m_first = 0
3184 val mutable m_qsearch = ""
3185 val mutable m_pan = 0
3186 method getactive = m_active
3187 method getfirst = m_first
3188 method getqsearch = m_qsearch
3189 method getpan = m_pan
3190 method setqsearch s = m_qsearch <- s
3191 end;;
3193 let withoutlastutf8 s =
3194 let len = String.length s in
3195 if len = 0
3196 then s
3197 else
3198 let rec find pos =
3199 if pos = 0
3200 then pos
3201 else
3202 let b = Char.code s.[pos] in
3203 if b land 0b11000000 = 0b11000000
3204 then pos
3205 else find (pos-1)
3207 let first =
3208 if Char.code s.[len-1] land 0x80 = 0
3209 then len-1
3210 else find (len-1)
3212 String.sub s 0 first;
3215 let textentrykeyboard
3216 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3217 let key =
3218 if key >= 0xffb0 && key <= 0xffb9
3219 then key - 0xffb0 + 48 else key
3221 let enttext te =
3222 state.mode <- Textentry (te, onleave);
3223 state.text <- "";
3224 enttext ();
3225 G.postRedisplay "textentrykeyboard enttext";
3227 let histaction cmd =
3228 match opthist with
3229 | None -> ()
3230 | Some (action, _) ->
3231 state.mode <- Textentry (
3232 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3234 G.postRedisplay "textentry histaction"
3236 match key with
3237 | 0xff08 -> (* backspace *)
3238 let s = withoutlastutf8 text in
3239 let len = String.length s in
3240 if cancelonempty && len = 0
3241 then (
3242 onleave Cancel;
3243 G.postRedisplay "textentrykeyboard after cancel";
3245 else (
3246 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3249 | 0xff0d | 0xff8d -> (* (kp) enter *)
3250 ondone text;
3251 onleave Confirm;
3252 G.postRedisplay "textentrykeyboard after confirm"
3254 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3255 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3256 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3257 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3259 | 0xff1b -> (* escape*)
3260 if String.length text = 0
3261 then (
3262 begin match opthist with
3263 | None -> ()
3264 | Some (_, onhistcancel) -> onhistcancel ()
3265 end;
3266 onleave Cancel;
3267 state.text <- "";
3268 G.postRedisplay "textentrykeyboard after cancel2"
3270 else (
3271 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3274 | 0xff9f | 0xffff -> () (* delete *)
3276 | _ when key != 0
3277 && key land 0xff00 != 0xff00 (* keyboard *)
3278 && key land 0xfe00 != 0xfe00 (* xkb *)
3279 && key land 0xfd00 != 0xfd00 (* 3270 *)
3281 begin match onkey text key with
3282 | TEdone text ->
3283 ondone text;
3284 onleave Confirm;
3285 G.postRedisplay "textentrykeyboard after confirm2";
3287 | TEcont text ->
3288 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3290 | TEstop ->
3291 onleave Cancel;
3292 G.postRedisplay "textentrykeyboard after cancel3"
3294 | TEswitch te ->
3295 state.mode <- Textentry (te, onleave);
3296 G.postRedisplay "textentrykeyboard switch";
3297 end;
3299 | _ ->
3300 vlog "unhandled key %s" (Wsi.keyname key)
3303 let firstof first active =
3304 if first > active || abs (first - active) > fstate.maxrows - 1
3305 then max 0 (active - (fstate.maxrows/2))
3306 else first
3309 let calcfirst first active =
3310 if active > first
3311 then
3312 let rows = active - first in
3313 if rows > fstate.maxrows then active - fstate.maxrows else first
3314 else active
3317 let scrollph y maxy =
3318 let sh = float (maxy + state.winh) /. float state.winh in
3319 let sh = float state.winh /. sh in
3320 let sh = max sh (float conf.scrollh) in
3322 let percent = float y /. float maxy in
3323 let position = (float state.winh -. sh) *. percent in
3325 let position =
3326 if position +. sh > float state.winh
3327 then float state.winh -. sh
3328 else position
3330 position, sh;
3333 let coe s = (s :> uioh);;
3335 class listview ~(source:lvsource) ~trusted ~modehash =
3336 object (self)
3337 val m_pan = source#getpan
3338 val m_first = source#getfirst
3339 val m_active = source#getactive
3340 val m_qsearch = source#getqsearch
3341 val m_prev_uioh = state.uioh
3343 method private elemunder y =
3344 let n = y / (fstate.fontsize+1) in
3345 if m_first + n < source#getitemcount
3346 then (
3347 if source#hasaction (m_first + n)
3348 then Some (m_first + n)
3349 else None
3351 else None
3353 method display =
3354 Gl.enable `blend;
3355 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3356 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3357 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3358 GlDraw.color (1., 1., 1.);
3359 Gl.enable `texture_2d;
3360 let fs = fstate.fontsize in
3361 let nfs = fs + 1 in
3362 let ww = fstate.wwidth in
3363 let tabw = 30.0*.ww in
3364 let itemcount = source#getitemcount in
3365 let rec loop row =
3366 if (row - m_first) > fstate.maxrows
3367 then ()
3368 else (
3369 if row >= 0 && row < itemcount
3370 then (
3371 let (s, level) = source#getitem row in
3372 let y = (row - m_first) * nfs in
3373 let x = 5.0 +. float (level + m_pan) *. ww in
3374 if row = m_active
3375 then (
3376 Gl.disable `texture_2d;
3377 GlDraw.polygon_mode `both `line;
3378 let alpha = if source#hasaction row then 0.9 else 0.3 in
3379 GlDraw.color (1., 1., 1.) ~alpha;
3380 GlDraw.rect (1., float (y + 1))
3381 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3382 GlDraw.polygon_mode `both `fill;
3383 GlDraw.color (1., 1., 1.);
3384 Gl.enable `texture_2d;
3387 let drawtabularstring s =
3388 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3389 if trusted
3390 then
3391 let tabpos = try String.index s '\t' with Not_found -> -1 in
3392 if tabpos > 0
3393 then
3394 let len = String.length s - tabpos - 1 in
3395 let s1 = String.sub s 0 tabpos
3396 and s2 = String.sub s (tabpos + 1) len in
3397 let nx = drawstr x s1 in
3398 let sw = nx -. x in
3399 let x = x +. (max tabw sw) in
3400 drawstr x s2
3401 else
3402 drawstr x s
3403 else
3404 drawstr x s
3406 let _ = drawtabularstring s in
3407 loop (row+1)
3411 loop m_first;
3412 Gl.disable `blend;
3413 Gl.disable `texture_2d;
3415 method updownlevel incr =
3416 let len = source#getitemcount in
3417 let curlevel =
3418 if m_active >= 0 && m_active < len
3419 then snd (source#getitem m_active)
3420 else -1
3422 let rec flow i =
3423 if i = len then i-1 else if i = -1 then 0 else
3424 let _, l = source#getitem i in
3425 if l != curlevel then i else flow (i+incr)
3427 let active = flow m_active in
3428 let first = calcfirst m_first active in
3429 G.postRedisplay "outline updownlevel";
3430 {< m_active = active; m_first = first >}
3432 method private key1 key mask =
3433 let set1 active first qsearch =
3434 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3436 let search active pattern incr =
3437 let active = if active = -1 then m_first else active in
3438 let dosearch re =
3439 let rec loop n =
3440 if n >= 0 && n < source#getitemcount
3441 then (
3442 let s, _ = source#getitem n in
3444 (try ignore (Str.search_forward re s 0); true
3445 with Not_found -> false)
3446 then Some n
3447 else loop (n + incr)
3449 else None
3451 loop active
3454 let re = Str.regexp_case_fold pattern in
3455 dosearch re
3456 with Failure s ->
3457 state.text <- s;
3458 None
3460 let itemcount = source#getitemcount in
3461 let find start incr =
3462 let rec find i =
3463 if i = -1 || i = itemcount
3464 then -1
3465 else (
3466 if source#hasaction i
3467 then i
3468 else find (i + incr)
3471 find start
3473 let set active first =
3474 let first = bound first 0 (itemcount - fstate.maxrows) in
3475 state.text <- "";
3476 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3478 let navigate incr =
3479 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3480 let active, first =
3481 let incr1 = if incr > 0 then 1 else -1 in
3482 if isvisible m_first m_active
3483 then
3484 let next =
3485 let next = m_active + incr in
3486 let next =
3487 if next < 0 || next >= itemcount
3488 then -1
3489 else find next incr1
3491 if abs (m_active - next) > fstate.maxrows
3492 then -1
3493 else next
3495 if next = -1
3496 then
3497 let first = m_first + incr in
3498 let first = bound first 0 (itemcount - fstate.maxrows) in
3499 let next =
3500 let next = m_active + incr in
3501 let next = bound next 0 (itemcount - 1) in
3502 find next ~-incr1
3504 let active =
3505 if next = -1
3506 then m_active
3507 else (
3508 if isvisible first next
3509 then next
3510 else m_active
3513 active, first
3514 else
3515 let first = min next m_first in
3516 let first =
3517 if abs (next - first) > fstate.maxrows
3518 then first + incr
3519 else first
3521 next, first
3522 else
3523 let first = m_first + incr in
3524 let first = bound first 0 (itemcount - 1) in
3525 let active =
3526 let next = m_active + incr in
3527 let next = bound next 0 (itemcount - 1) in
3528 let next = find next incr1 in
3529 let active =
3530 if next = -1 || abs (m_active - first) > fstate.maxrows
3531 then (
3532 let active = if m_active = -1 then next else m_active in
3533 active
3535 else next
3537 if isvisible first active
3538 then active
3539 else -1
3541 active, first
3543 G.postRedisplay "listview navigate";
3544 set active first;
3546 match key with
3547 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3548 let incr = if key = 0x72 then -1 else 1 in
3549 let active, first =
3550 match search (m_active + incr) m_qsearch incr with
3551 | None ->
3552 state.text <- m_qsearch ^ " [not found]";
3553 m_active, m_first
3554 | Some active ->
3555 state.text <- m_qsearch;
3556 active, firstof m_first active
3558 G.postRedisplay "listview ctrl-r/s";
3559 set1 active first m_qsearch;
3561 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3562 if m_active >= 0 && m_active < source#getitemcount
3563 then (
3564 let s, _ = source#getitem m_active in
3565 selstring s;
3567 coe self
3569 | 0xff08 -> (* backspace *)
3570 if String.length m_qsearch = 0
3571 then coe self
3572 else (
3573 let qsearch = withoutlastutf8 m_qsearch in
3574 let len = String.length qsearch in
3575 if len = 0
3576 then (
3577 state.text <- "";
3578 G.postRedisplay "listview empty qsearch";
3579 set1 m_active m_first "";
3581 else
3582 let active, first =
3583 match search m_active qsearch ~-1 with
3584 | None ->
3585 state.text <- qsearch ^ " [not found]";
3586 m_active, m_first
3587 | Some active ->
3588 state.text <- qsearch;
3589 active, firstof m_first active
3591 G.postRedisplay "listview backspace qsearch";
3592 set1 active first qsearch
3595 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3596 let pattern = m_qsearch ^ toutf8 key in
3597 let active, first =
3598 match search m_active pattern 1 with
3599 | None ->
3600 state.text <- pattern ^ " [not found]";
3601 m_active, m_first
3602 | Some active ->
3603 state.text <- pattern;
3604 active, firstof m_first active
3606 G.postRedisplay "listview qsearch add";
3607 set1 active first pattern;
3609 | 0xff1b -> (* escape *)
3610 state.text <- "";
3611 if String.length m_qsearch = 0
3612 then (
3613 G.postRedisplay "list view escape";
3614 begin
3615 match
3616 source#exit (coe self) true m_active m_first m_pan m_qsearch
3617 with
3618 | None -> m_prev_uioh
3619 | Some uioh -> uioh
3622 else (
3623 G.postRedisplay "list view kill qsearch";
3624 source#setqsearch "";
3625 coe {< m_qsearch = "" >}
3628 | 0xff0d | 0xff8d -> (* (kp) enter *)
3629 state.text <- "";
3630 let self = {< m_qsearch = "" >} in
3631 source#setqsearch "";
3632 let opt =
3633 G.postRedisplay "listview enter";
3634 if m_active >= 0 && m_active < source#getitemcount
3635 then (
3636 source#exit (coe self) false m_active m_first m_pan "";
3638 else (
3639 source#exit (coe self) true m_active m_first m_pan "";
3642 begin match opt with
3643 | None -> m_prev_uioh
3644 | Some uioh -> uioh
3647 | 0xff9f | 0xffff -> (* (kp) delete *)
3648 coe self
3650 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3651 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3652 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3653 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3655 | 0xff53 | 0xff98 -> (* (kp) right *)
3656 state.text <- "";
3657 G.postRedisplay "listview right";
3658 coe {< m_pan = m_pan - 1 >}
3660 | 0xff51 | 0xff96 -> (* (kp) left *)
3661 state.text <- "";
3662 G.postRedisplay "listview left";
3663 coe {< m_pan = m_pan + 1 >}
3665 | 0xff50 | 0xff95 -> (* (kp) home *)
3666 let active = find 0 1 in
3667 G.postRedisplay "listview home";
3668 set active 0;
3670 | 0xff57 | 0xff9c -> (* (kp) end *)
3671 let first = max 0 (itemcount - fstate.maxrows) in
3672 let active = find (itemcount - 1) ~-1 in
3673 G.postRedisplay "listview end";
3674 set active first;
3676 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3677 coe self
3679 | _ ->
3680 dolog "listview unknown key %#x" key; coe self
3682 method key key mask =
3683 match state.mode with
3684 | Textentry te -> textentrykeyboard key mask te; coe self
3685 | _ -> self#key1 key mask
3687 method button button down x y _ =
3688 let opt =
3689 match button with
3690 | 1 when x > state.winw - conf.scrollbw ->
3691 G.postRedisplay "listview scroll";
3692 if down
3693 then
3694 let _, position, sh = self#scrollph in
3695 if y > truncate position && y < truncate (position +. sh)
3696 then (
3697 state.mstate <- Mscrolly;
3698 Some (coe self)
3700 else
3701 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3702 let first = truncate (s *. float source#getitemcount) in
3703 let first = min source#getitemcount first in
3704 Some (coe {< m_first = first; m_active = first >})
3705 else (
3706 state.mstate <- Mnone;
3707 Some (coe self);
3709 | 1 when not down ->
3710 begin match self#elemunder y with
3711 | Some n ->
3712 G.postRedisplay "listview click";
3713 source#exit
3714 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3715 | _ ->
3716 Some (coe self)
3718 | n when (n == 4 || n == 5) && not down ->
3719 let len = source#getitemcount in
3720 let first =
3721 if n = 5 && m_first + fstate.maxrows >= len
3722 then
3723 m_first
3724 else
3725 let first = m_first + (if n == 4 then -1 else 1) in
3726 bound first 0 (len - 1)
3728 G.postRedisplay "listview wheel";
3729 Some (coe {< m_first = first >})
3730 | n when (n = 6 || n = 7) && not down ->
3731 let inc = m_first + (if n = 7 then -1 else 1) in
3732 G.postRedisplay "listview hwheel";
3733 Some (coe {< m_pan = m_pan + inc >})
3734 | _ ->
3735 Some (coe self)
3737 match opt with
3738 | None -> m_prev_uioh
3739 | Some uioh -> uioh
3741 method motion _ y =
3742 match state.mstate with
3743 | Mscrolly ->
3744 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3745 let first = truncate (s *. float source#getitemcount) in
3746 let first = min source#getitemcount first in
3747 G.postRedisplay "listview motion";
3748 coe {< m_first = first; m_active = first >}
3749 | _ -> coe self
3751 method pmotion x y =
3752 if x < state.winw - conf.scrollbw
3753 then
3754 let n =
3755 match self#elemunder y with
3756 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3757 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3759 let o =
3760 if n != m_active
3761 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3762 else self
3764 coe o
3765 else (
3766 Wsi.setcursor Wsi.CURSOR_INHERIT;
3767 coe self
3770 method infochanged _ = ()
3772 method scrollpw = (0, 0.0, 0.0)
3773 method scrollph =
3774 let nfs = fstate.fontsize + 1 in
3775 let y = m_first * nfs in
3776 let itemcount = source#getitemcount in
3777 let maxi = max 0 (itemcount - fstate.maxrows) in
3778 let maxy = maxi * nfs in
3779 let p, h = scrollph y maxy in
3780 conf.scrollbw, p, h
3782 method modehash = modehash
3783 method eformsgs = false
3784 end;;
3786 class outlinelistview ~source =
3787 object (self)
3788 inherit listview
3789 ~source:(source :> lvsource)
3790 ~trusted:false
3791 ~modehash:(findkeyhash conf "outline")
3792 as super
3794 method key key mask =
3795 let calcfirst first active =
3796 if active > first
3797 then
3798 let rows = active - first in
3799 let maxrows =
3800 if String.length state.text = 0
3801 then fstate.maxrows
3802 else fstate.maxrows - 2
3804 if rows > maxrows then active - maxrows else first
3805 else active
3807 let navigate incr =
3808 let active = m_active + incr in
3809 let active = bound active 0 (source#getitemcount - 1) in
3810 let first = calcfirst m_first active in
3811 G.postRedisplay "outline navigate";
3812 coe {< m_active = active; m_first = first >}
3814 let ctrl = Wsi.withctrl mask in
3815 match key with
3816 | 110 when ctrl -> (* ctrl-n *)
3817 source#narrow m_qsearch;
3818 G.postRedisplay "outline ctrl-n";
3819 coe {< m_first = 0; m_active = 0 >}
3821 | 117 when ctrl -> (* ctrl-u *)
3822 source#denarrow;
3823 G.postRedisplay "outline ctrl-u";
3824 state.text <- "";
3825 coe {< m_first = 0; m_active = 0 >}
3827 | 108 when ctrl -> (* ctrl-l *)
3828 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3829 G.postRedisplay "outline ctrl-l";
3830 coe {< m_first = first >}
3832 | 0xff9f | 0xffff -> (* (kp) delete *)
3833 source#remove m_active;
3834 G.postRedisplay "outline delete";
3835 let active = max 0 (m_active-1) in
3836 coe {< m_first = firstof m_first active;
3837 m_active = active >}
3839 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3840 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3841 | 0xff55 | 0xff9a -> (* (kp) prior *)
3842 navigate ~-(fstate.maxrows)
3843 | 0xff56 | 0xff9b -> (* (kp) next *)
3844 navigate fstate.maxrows
3846 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3847 let o =
3848 if ctrl
3849 then (
3850 G.postRedisplay "outline ctrl right";
3851 {< m_pan = m_pan + 1 >}
3853 else self#updownlevel 1
3855 coe o
3857 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3858 let o =
3859 if ctrl
3860 then (
3861 G.postRedisplay "outline ctrl left";
3862 {< m_pan = m_pan - 1 >}
3864 else self#updownlevel ~-1
3866 coe o
3868 | 0xff50 | 0xff95 -> (* (kp) home *)
3869 G.postRedisplay "outline home";
3870 coe {< m_first = 0; m_active = 0 >}
3872 | 0xff57 | 0xff9c -> (* (kp) end *)
3873 let active = source#getitemcount - 1 in
3874 let first = max 0 (active - fstate.maxrows) in
3875 G.postRedisplay "outline end";
3876 coe {< m_active = active; m_first = first >}
3878 | _ -> super#key key mask
3881 let outlinesource usebookmarks =
3882 let empty = [||] in
3883 (object
3884 inherit lvsourcebase
3885 val mutable m_items = empty
3886 val mutable m_orig_items = empty
3887 val mutable m_prev_items = empty
3888 val mutable m_narrow_pattern = ""
3889 val mutable m_hadremovals = false
3891 method getitemcount =
3892 Array.length m_items + (if m_hadremovals then 1 else 0)
3894 method getitem n =
3895 if n == Array.length m_items && m_hadremovals
3896 then
3897 ("[Confirm removal]", 0)
3898 else
3899 let s, n, _ = m_items.(n) in
3900 (s, n)
3902 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3903 ignore (uioh, first, qsearch);
3904 let confrimremoval = m_hadremovals && active = Array.length m_items in
3905 let items =
3906 if String.length m_narrow_pattern = 0
3907 then m_orig_items
3908 else m_items
3910 if not cancel
3911 then (
3912 if not confrimremoval
3913 then(
3914 let _, _, anchor = m_items.(active) in
3915 gotoghyll (getanchory anchor);
3916 m_items <- items;
3918 else (
3919 state.bookmarks <- Array.to_list m_items;
3920 m_orig_items <- m_items;
3923 else m_items <- items;
3924 m_pan <- pan;
3925 None
3927 method hasaction _ = true
3929 method greetmsg =
3930 if Array.length m_items != Array.length m_orig_items
3931 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3932 else ""
3934 method narrow pattern =
3935 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3936 match reopt with
3937 | None -> ()
3938 | Some re ->
3939 let rec loop accu n =
3940 if n = -1
3941 then (
3942 m_narrow_pattern <- pattern;
3943 m_items <- Array.of_list accu
3945 else
3946 let (s, _, _) as o = m_items.(n) in
3947 let accu =
3948 if (try ignore (Str.search_forward re s 0); true
3949 with Not_found -> false)
3950 then o :: accu
3951 else accu
3953 loop accu (n-1)
3955 loop [] (Array.length m_items - 1)
3957 method denarrow =
3958 m_orig_items <- (
3959 if usebookmarks
3960 then Array.of_list state.bookmarks
3961 else state.outlines
3963 m_items <- m_orig_items
3965 method remove m =
3966 if usebookmarks
3967 then
3968 if m >= 0 && m < Array.length m_items
3969 then (
3970 m_hadremovals <- true;
3971 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3972 let n = if n >= m then n+1 else n in
3973 m_items.(n)
3977 method reset anchor items =
3978 m_hadremovals <- false;
3979 if m_orig_items == empty || m_prev_items != items
3980 then (
3981 m_orig_items <- items;
3982 if String.length m_narrow_pattern = 0
3983 then m_items <- items;
3985 m_prev_items <- items;
3986 let rely = getanchory anchor in
3987 let active =
3988 let rec loop n best bestd =
3989 if n = Array.length m_items
3990 then best
3991 else
3992 let (_, _, anchor) = m_items.(n) in
3993 let orely = getanchory anchor in
3994 let d = abs (orely - rely) in
3995 if d < bestd
3996 then loop (n+1) n d
3997 else loop (n+1) best bestd
3999 loop 0 ~-1 max_int
4001 m_active <- active;
4002 m_first <- firstof m_first active
4003 end)
4006 let enterselector usebookmarks =
4007 let source = outlinesource usebookmarks in
4008 fun errmsg ->
4009 let outlines =
4010 if usebookmarks
4011 then Array.of_list state.bookmarks
4012 else state.outlines
4014 if Array.length outlines = 0
4015 then (
4016 showtext ' ' errmsg;
4018 else (
4019 state.text <- source#greetmsg;
4020 Wsi.setcursor Wsi.CURSOR_INHERIT;
4021 let anchor = getanchor () in
4022 source#reset anchor outlines;
4023 state.uioh <- coe (new outlinelistview ~source);
4024 G.postRedisplay "enter selector";
4028 let enteroutlinemode =
4029 let f = enterselector false in
4030 fun ()-> f "Document has no outline";
4033 let enterbookmarkmode =
4034 let f = enterselector true in
4035 fun () -> f "Document has no bookmarks (yet)";
4038 let color_of_string s =
4039 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4040 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4044 let color_to_string (r, g, b) =
4045 let r = truncate (r *. 256.0)
4046 and g = truncate (g *. 256.0)
4047 and b = truncate (b *. 256.0) in
4048 Printf.sprintf "%d/%d/%d" r g b
4051 let irect_of_string s =
4052 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4055 let irect_to_string (x0,y0,x1,y1) =
4056 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4059 let makecheckers () =
4060 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4061 following to say:
4062 converted by Issac Trotts. July 25, 2002 *)
4063 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4064 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4065 let id = GlTex.gen_texture () in
4066 GlTex.bind_texture `texture_2d id;
4067 GlPix.store (`unpack_alignment 1);
4068 GlTex.image2d image;
4069 List.iter (GlTex.parameter ~target:`texture_2d)
4070 [ `mag_filter `nearest; `min_filter `nearest ];
4074 let setcheckers enabled =
4075 match state.texid with
4076 | None ->
4077 if enabled then state.texid <- Some (makecheckers ())
4079 | Some texid ->
4080 if not enabled
4081 then (
4082 GlTex.delete_texture texid;
4083 state.texid <- None;
4087 let int_of_string_with_suffix s =
4088 let l = String.length s in
4089 let s1, shift =
4090 if l > 1
4091 then
4092 let suffix = Char.lowercase s.[l-1] in
4093 match suffix with
4094 | 'k' -> String.sub s 0 (l-1), 10
4095 | 'm' -> String.sub s 0 (l-1), 20
4096 | 'g' -> String.sub s 0 (l-1), 30
4097 | _ -> s, 0
4098 else s, 0
4100 let n = int_of_string s1 in
4101 let m = n lsl shift in
4102 if m < 0 || m < n
4103 then raise (Failure "value too large")
4104 else m
4107 let string_with_suffix_of_int n =
4108 if n = 0
4109 then "0"
4110 else
4111 let n, s =
4112 if n land ((1 lsl 30) - 1) = 0
4113 then n lsr 30, "G"
4114 else (
4115 if n land ((1 lsl 20) - 1) = 0
4116 then n lsr 20, "M"
4117 else (
4118 if n land ((1 lsl 10) - 1) = 0
4119 then n lsr 10, "K"
4120 else n, ""
4124 let rec loop s n =
4125 let h = n mod 1000 in
4126 let n = n / 1000 in
4127 if n = 0
4128 then string_of_int h ^ s
4129 else (
4130 let s = Printf.sprintf "_%03d%s" h s in
4131 loop s n
4134 loop "" n ^ s;
4137 let defghyllscroll = (40, 8, 32);;
4138 let ghyllscroll_of_string s =
4139 let (n, a, b) as nab =
4140 if s = "default"
4141 then defghyllscroll
4142 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4144 if n <= a || n <= b || a >= b
4145 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4146 nab;
4149 let ghyllscroll_to_string ((n, a, b) as nab) =
4150 if nab = defghyllscroll
4151 then "default"
4152 else Printf.sprintf "%d,%d,%d" n a b;
4155 let describe_location () =
4156 let fn = page_of_y state.y in
4157 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4158 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4159 let percent =
4160 if maxy <= 0
4161 then 100.
4162 else (100. *. (float state.y /. float maxy))
4164 if fn = ln
4165 then
4166 Printf.sprintf "page %d of %d [%.2f%%]"
4167 (fn+1) state.pagecount percent
4168 else
4169 Printf.sprintf
4170 "pages %d-%d of %d [%.2f%%]"
4171 (fn+1) (ln+1) state.pagecount percent
4174 let setpresentationmode v =
4175 let n = page_of_y state.y in
4176 state.anchor <- (n, 0.0, 1.0);
4177 conf.presentation <- v;
4178 if conf.fitmodel = FitPage
4179 then reqlayout conf.angle conf.fitmodel;
4180 represent ();
4183 let enterinfomode =
4184 let btos b = if b then "\xe2\x88\x9a" else "" in
4185 let showextended = ref false in
4186 let leave mode = function
4187 | Confirm -> state.mode <- mode
4188 | Cancel -> state.mode <- mode in
4189 let src =
4190 (object
4191 val mutable m_first_time = true
4192 val mutable m_l = []
4193 val mutable m_a = [||]
4194 val mutable m_prev_uioh = nouioh
4195 val mutable m_prev_mode = View
4197 inherit lvsourcebase
4199 method reset prev_mode prev_uioh =
4200 m_a <- Array.of_list (List.rev m_l);
4201 m_l <- [];
4202 m_prev_mode <- prev_mode;
4203 m_prev_uioh <- prev_uioh;
4204 if m_first_time
4205 then (
4206 let rec loop n =
4207 if n >= Array.length m_a
4208 then ()
4209 else
4210 match m_a.(n) with
4211 | _, _, _, Action _ -> m_active <- n
4212 | _ -> loop (n+1)
4214 loop 0;
4215 m_first_time <- false;
4218 method int name get set =
4219 m_l <-
4220 (name, `int get, 1, Action (
4221 fun u ->
4222 let ondone s =
4223 try set (int_of_string s)
4224 with exn ->
4225 state.text <- Printf.sprintf "bad integer `%s': %s"
4226 s (exntos exn)
4228 state.text <- "";
4229 let te = name ^ ": ", "", None, intentry, ondone, true in
4230 state.mode <- Textentry (te, leave m_prev_mode);
4232 )) :: m_l
4234 method int_with_suffix name get set =
4235 m_l <-
4236 (name, `intws get, 1, Action (
4237 fun u ->
4238 let ondone s =
4239 try set (int_of_string_with_suffix s)
4240 with exn ->
4241 state.text <- Printf.sprintf "bad integer `%s': %s"
4242 s (exntos exn)
4244 state.text <- "";
4245 let te =
4246 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4248 state.mode <- Textentry (te, leave m_prev_mode);
4250 )) :: m_l
4252 method bool ?(offset=1) ?(btos=btos) name get set =
4253 m_l <-
4254 (name, `bool (btos, get), offset, Action (
4255 fun u ->
4256 let v = get () in
4257 set (not v);
4259 )) :: m_l
4261 method color name get set =
4262 m_l <-
4263 (name, `color get, 1, Action (
4264 fun u ->
4265 let invalid = (nan, nan, nan) in
4266 let ondone s =
4267 let c =
4268 try color_of_string s
4269 with exn ->
4270 state.text <- Printf.sprintf "bad color `%s': %s"
4271 s (exntos exn);
4272 invalid
4274 if c <> invalid
4275 then set c;
4277 let te = name ^ ": ", "", None, textentry, ondone, true in
4278 state.text <- color_to_string (get ());
4279 state.mode <- Textentry (te, leave m_prev_mode);
4281 )) :: m_l
4283 method string name get set =
4284 m_l <-
4285 (name, `string get, 1, Action (
4286 fun u ->
4287 let ondone s = set s in
4288 let te = name ^ ": ", "", None, textentry, ondone, true in
4289 state.mode <- Textentry (te, leave m_prev_mode);
4291 )) :: m_l
4293 method colorspace name get set =
4294 m_l <-
4295 (name, `string get, 1, Action (
4296 fun _ ->
4297 let source =
4298 let vals = [| "rgb"; "bgr"; "gray" |] in
4299 (object
4300 inherit lvsourcebase
4302 initializer
4303 m_active <- int_of_colorspace conf.colorspace;
4304 m_first <- 0;
4306 method getitemcount = Array.length vals
4307 method getitem n = (vals.(n), 0)
4308 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4309 ignore (uioh, first, pan, qsearch);
4310 if not cancel then set active;
4311 None
4312 method hasaction _ = true
4313 end)
4315 state.text <- "";
4316 let modehash = findkeyhash conf "info" in
4317 coe (new listview ~source ~trusted:true ~modehash)
4318 )) :: m_l
4320 method fitmodel name get set =
4321 m_l <-
4322 (name, `string get, 1, Action (
4323 fun _ ->
4324 let source =
4325 let vals = [| "fit width"; "proportional"; "fit page" |] in
4326 (object
4327 inherit lvsourcebase
4329 initializer
4330 m_active <- int_of_fitmodel conf.fitmodel;
4331 m_first <- 0;
4333 method getitemcount = Array.length vals
4334 method getitem n = (vals.(n), 0)
4335 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4336 ignore (uioh, first, pan, qsearch);
4337 if not cancel then set active;
4338 None
4339 method hasaction _ = true
4340 end)
4342 state.text <- "";
4343 let modehash = findkeyhash conf "info" in
4344 coe (new listview ~source ~trusted:true ~modehash)
4345 )) :: m_l
4347 method caption s offset =
4348 m_l <- (s, `empty, offset, Noaction) :: m_l
4350 method caption2 s f offset =
4351 m_l <- (s, `string f, offset, Noaction) :: m_l
4353 method getitemcount = Array.length m_a
4355 method getitem n =
4356 let tostr = function
4357 | `int f -> string_of_int (f ())
4358 | `intws f -> string_with_suffix_of_int (f ())
4359 | `string f -> f ()
4360 | `color f -> color_to_string (f ())
4361 | `bool (btos, f) -> btos (f ())
4362 | `empty -> ""
4364 let name, t, offset, _ = m_a.(n) in
4365 ((let s = tostr t in
4366 if String.length s > 0
4367 then Printf.sprintf "%s\t%s" name s
4368 else name),
4369 offset)
4371 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4372 let uiohopt =
4373 if not cancel
4374 then (
4375 m_qsearch <- qsearch;
4376 let uioh =
4377 match m_a.(active) with
4378 | _, _, _, Action f -> f uioh
4379 | _ -> uioh
4381 Some uioh
4383 else None
4385 m_active <- active;
4386 m_first <- first;
4387 m_pan <- pan;
4388 uiohopt
4390 method hasaction n =
4391 match m_a.(n) with
4392 | _, _, _, Action _ -> true
4393 | _ -> false
4394 end)
4396 let rec fillsrc prevmode prevuioh =
4397 let sep () = src#caption "" 0 in
4398 let colorp name get set =
4399 src#string name
4400 (fun () -> color_to_string (get ()))
4401 (fun v ->
4403 let c = color_of_string v in
4404 set c
4405 with exn ->
4406 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4409 let oldmode = state.mode in
4410 let birdseye = isbirdseye state.mode in
4412 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4414 src#bool "presentation mode"
4415 (fun () -> conf.presentation)
4416 (fun v -> setpresentationmode v);
4418 src#bool "ignore case in searches"
4419 (fun () -> conf.icase)
4420 (fun v -> conf.icase <- v);
4422 src#bool "preload"
4423 (fun () -> conf.preload)
4424 (fun v -> conf.preload <- v);
4426 src#bool "highlight links"
4427 (fun () -> conf.hlinks)
4428 (fun v -> conf.hlinks <- v);
4430 src#bool "under info"
4431 (fun () -> conf.underinfo)
4432 (fun v -> conf.underinfo <- v);
4434 src#bool "persistent bookmarks"
4435 (fun () -> conf.savebmarks)
4436 (fun v -> conf.savebmarks <- v);
4438 src#fitmodel "fit model"
4439 (fun () -> fitmodel_to_string conf.fitmodel)
4440 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4442 src#bool "trim margins"
4443 (fun () -> conf.trimmargins)
4444 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4446 src#bool "persistent location"
4447 (fun () -> conf.jumpback)
4448 (fun v -> conf.jumpback <- v);
4450 sep ();
4451 src#int "inter-page space"
4452 (fun () -> conf.interpagespace)
4453 (fun n ->
4454 conf.interpagespace <- n;
4455 docolumns conf.columns;
4456 let pageno, py =
4457 match state.layout with
4458 | [] -> 0, 0
4459 | l :: _ ->
4460 l.pageno, l.pagey
4462 state.maxy <- calcheight ();
4463 let y = getpagey pageno in
4464 gotoy (y + py)
4467 src#int "page bias"
4468 (fun () -> conf.pagebias)
4469 (fun v -> conf.pagebias <- v);
4471 src#int "scroll step"
4472 (fun () -> conf.scrollstep)
4473 (fun n -> conf.scrollstep <- n);
4475 src#int "horizontal scroll step"
4476 (fun () -> conf.hscrollstep)
4477 (fun v -> conf.hscrollstep <- v);
4479 src#int "auto scroll step"
4480 (fun () ->
4481 match state.autoscroll with
4482 | Some step -> step
4483 | _ -> conf.autoscrollstep)
4484 (fun n ->
4485 if state.autoscroll <> None
4486 then state.autoscroll <- Some n;
4487 conf.autoscrollstep <- n);
4489 src#int "zoom"
4490 (fun () -> truncate (conf.zoom *. 100.))
4491 (fun v -> setzoom ((float v) /. 100.));
4493 src#int "rotation"
4494 (fun () -> conf.angle)
4495 (fun v -> reqlayout v conf.fitmodel);
4497 src#int "scroll bar width"
4498 (fun () -> conf.scrollbw)
4499 (fun v ->
4500 conf.scrollbw <- v;
4501 reshape state.winw state.winh;
4504 src#int "scroll handle height"
4505 (fun () -> conf.scrollh)
4506 (fun v -> conf.scrollh <- v;);
4508 src#int "thumbnail width"
4509 (fun () -> conf.thumbw)
4510 (fun v ->
4511 conf.thumbw <- min 4096 v;
4512 match oldmode with
4513 | Birdseye beye ->
4514 leavebirdseye beye false;
4515 enterbirdseye ()
4516 | _ -> ()
4519 let mode = state.mode in
4520 src#string "columns"
4521 (fun () ->
4522 match conf.columns with
4523 | Csingle _ -> "1"
4524 | Cmulti (multi, _) -> multicolumns_to_string multi
4525 | Csplit (count, _) -> "-" ^ string_of_int count
4527 (fun v ->
4528 let n, a, b = multicolumns_of_string v in
4529 setcolumns mode n a b);
4531 sep ();
4532 src#caption "Pixmap cache" 0;
4533 src#int_with_suffix "size (advisory)"
4534 (fun () -> conf.memlimit)
4535 (fun v -> conf.memlimit <- v);
4537 src#caption2 "used"
4538 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4539 (string_with_suffix_of_int state.memused)
4540 (Hashtbl.length state.tilemap)) 1;
4542 sep ();
4543 src#caption "Layout" 0;
4544 src#caption2 "Dimension"
4545 (fun () ->
4546 Printf.sprintf "%dx%d (virtual %dx%d)"
4547 state.winw state.winh
4548 state.w state.maxy)
4550 if conf.debug
4551 then
4552 src#caption2 "Position" (fun () ->
4553 Printf.sprintf "%dx%d" state.x state.y
4555 else
4556 src#caption2 "Position" (fun () -> describe_location ()) 1
4559 sep ();
4560 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4561 "Save these parameters as global defaults at exit"
4562 (fun () -> conf.bedefault)
4563 (fun v -> conf.bedefault <- v)
4566 sep ();
4567 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4568 src#bool ~offset:0 ~btos "Extended parameters"
4569 (fun () -> !showextended)
4570 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4571 if !showextended
4572 then (
4573 src#bool "checkers"
4574 (fun () -> conf.checkers)
4575 (fun v -> conf.checkers <- v; setcheckers v);
4576 src#bool "update cursor"
4577 (fun () -> conf.updatecurs)
4578 (fun v -> conf.updatecurs <- v);
4579 src#bool "verbose"
4580 (fun () -> conf.verbose)
4581 (fun v -> conf.verbose <- v);
4582 src#bool "invert colors"
4583 (fun () -> conf.invert)
4584 (fun v -> conf.invert <- v);
4585 src#bool "max fit"
4586 (fun () -> conf.maxhfit)
4587 (fun v -> conf.maxhfit <- v);
4588 src#bool "redirect stderr"
4589 (fun () -> conf.redirectstderr)
4590 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4591 src#string "uri launcher"
4592 (fun () -> conf.urilauncher)
4593 (fun v -> conf.urilauncher <- v);
4594 src#string "path launcher"
4595 (fun () -> conf.pathlauncher)
4596 (fun v -> conf.pathlauncher <- v);
4597 src#string "tile size"
4598 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4599 (fun v ->
4601 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4602 conf.tilew <- max 64 w;
4603 conf.tileh <- max 64 h;
4604 flushtiles ();
4605 with exn ->
4606 state.text <- Printf.sprintf "bad tile size `%s': %s"
4607 v (exntos exn)
4609 src#int "texture count"
4610 (fun () -> conf.texcount)
4611 (fun v ->
4612 if realloctexts v
4613 then conf.texcount <- v
4614 else showtext '!' " Failed to set texture count please retry later"
4616 src#int "slice height"
4617 (fun () -> conf.sliceheight)
4618 (fun v ->
4619 conf.sliceheight <- v;
4620 wcmd "sliceh %d" conf.sliceheight;
4622 src#int "anti-aliasing level"
4623 (fun () -> conf.aalevel)
4624 (fun v ->
4625 conf.aalevel <- bound v 0 8;
4626 state.anchor <- getanchor ();
4627 opendoc state.path state.password;
4629 src#string "page scroll scaling factor"
4630 (fun () -> string_of_float conf.pgscale)
4631 (fun v ->
4633 let s = float_of_string v in
4634 conf.pgscale <- s
4635 with exn ->
4636 state.text <- Printf.sprintf
4637 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4640 src#int "ui font size"
4641 (fun () -> fstate.fontsize)
4642 (fun v -> setfontsize (bound v 5 100));
4643 src#int "hint font size"
4644 (fun () -> conf.hfsize)
4645 (fun v -> conf.hfsize <- bound v 5 100);
4646 colorp "background color"
4647 (fun () -> conf.bgcolor)
4648 (fun v -> conf.bgcolor <- v);
4649 src#bool "crop hack"
4650 (fun () -> conf.crophack)
4651 (fun v -> conf.crophack <- v);
4652 src#string "trim fuzz"
4653 (fun () -> irect_to_string conf.trimfuzz)
4654 (fun v ->
4656 conf.trimfuzz <- irect_of_string v;
4657 if conf.trimmargins
4658 then settrim true conf.trimfuzz;
4659 with exn ->
4660 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4662 src#string "throttle"
4663 (fun () ->
4664 match conf.maxwait with
4665 | None -> "show place holder if page is not ready"
4666 | Some time ->
4667 if time = infinity
4668 then "wait for page to fully render"
4669 else
4670 "wait " ^ string_of_float time
4671 ^ " seconds before showing placeholder"
4673 (fun v ->
4675 let f = float_of_string v in
4676 if f <= 0.0
4677 then conf.maxwait <- None
4678 else conf.maxwait <- Some f
4679 with exn ->
4680 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4682 src#string "ghyll scroll"
4683 (fun () ->
4684 match conf.ghyllscroll with
4685 | None -> ""
4686 | Some nab -> ghyllscroll_to_string nab
4688 (fun v ->
4690 let gs =
4691 if String.length v = 0
4692 then None
4693 else Some (ghyllscroll_of_string v)
4695 conf.ghyllscroll <- gs
4696 with exn ->
4697 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4699 src#string "selection command"
4700 (fun () -> conf.selcmd)
4701 (fun v -> conf.selcmd <- v);
4702 src#string "synctex command"
4703 (fun () -> conf.stcmd)
4704 (fun v -> conf.stcmd <- v);
4705 src#colorspace "color space"
4706 (fun () -> colorspace_to_string conf.colorspace)
4707 (fun v ->
4708 conf.colorspace <- colorspace_of_int v;
4709 wcmd "cs %d" v;
4710 load state.layout;
4712 if pbousable ()
4713 then
4714 src#bool "use PBO"
4715 (fun () -> conf.usepbo)
4716 (fun v -> conf.usepbo <- v);
4717 src#bool "mouse wheel scrolls pages"
4718 (fun () -> conf.wheelbypage)
4719 (fun v -> conf.wheelbypage <- v);
4722 sep ();
4723 src#caption "Document" 0;
4724 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4725 src#caption2 "Pages"
4726 (fun () -> string_of_int state.pagecount) 1;
4727 src#caption2 "Dimensions"
4728 (fun () -> string_of_int (List.length state.pdims)) 1;
4729 if conf.trimmargins
4730 then (
4731 sep ();
4732 src#caption "Trimmed margins" 0;
4733 src#caption2 "Dimensions"
4734 (fun () -> string_of_int (List.length state.pdims)) 1;
4737 sep ();
4738 src#caption "OpenGL" 0;
4739 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4740 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4742 sep ();
4743 src#caption "Location" 0;
4744 if String.length state.origin > 0
4745 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4746 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4748 src#reset prevmode prevuioh;
4750 fun () ->
4751 state.text <- "";
4752 let prevmode = state.mode
4753 and prevuioh = state.uioh in
4754 fillsrc prevmode prevuioh;
4755 let source = (src :> lvsource) in
4756 let modehash = findkeyhash conf "info" in
4757 state.uioh <- coe (object (self)
4758 inherit listview ~source ~trusted:true ~modehash as super
4759 val mutable m_prevmemused = 0
4760 method infochanged = function
4761 | Memused ->
4762 if m_prevmemused != state.memused
4763 then (
4764 m_prevmemused <- state.memused;
4765 G.postRedisplay "memusedchanged";
4767 | Pdim -> G.postRedisplay "pdimchanged"
4768 | Docinfo -> fillsrc prevmode prevuioh
4770 method key key mask =
4771 if not (Wsi.withctrl mask)
4772 then
4773 match key with
4774 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4775 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4776 | _ -> super#key key mask
4777 else super#key key mask
4778 end);
4779 G.postRedisplay "info";
4782 let enterhelpmode =
4783 let source =
4784 (object
4785 inherit lvsourcebase
4786 method getitemcount = Array.length state.help
4787 method getitem n =
4788 let s, l, _ = state.help.(n) in
4789 (s, l)
4791 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4792 let optuioh =
4793 if not cancel
4794 then (
4795 m_qsearch <- qsearch;
4796 match state.help.(active) with
4797 | _, _, Action f -> Some (f uioh)
4798 | _ -> Some (uioh)
4800 else None
4802 m_active <- active;
4803 m_first <- first;
4804 m_pan <- pan;
4805 optuioh
4807 method hasaction n =
4808 match state.help.(n) with
4809 | _, _, Action _ -> true
4810 | _ -> false
4812 initializer
4813 m_active <- -1
4814 end)
4815 in fun () ->
4816 let modehash = findkeyhash conf "help" in
4817 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4818 G.postRedisplay "help";
4821 let entermsgsmode =
4822 let msgsource =
4823 let re = Str.regexp "[\r\n]" in
4824 (object
4825 inherit lvsourcebase
4826 val mutable m_items = [||]
4828 method getitemcount = 1 + Array.length m_items
4830 method getitem n =
4831 if n = 0
4832 then "[Clear]", 0
4833 else m_items.(n-1), 0
4835 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4836 ignore uioh;
4837 if not cancel
4838 then (
4839 if active = 0
4840 then Buffer.clear state.errmsgs;
4841 m_qsearch <- qsearch;
4843 m_active <- active;
4844 m_first <- first;
4845 m_pan <- pan;
4846 None
4848 method hasaction n =
4849 n = 0
4851 method reset =
4852 state.newerrmsgs <- false;
4853 let l = Str.split re (Buffer.contents state.errmsgs) in
4854 m_items <- Array.of_list l
4856 initializer
4857 m_active <- 0
4858 end)
4859 in fun () ->
4860 state.text <- "";
4861 msgsource#reset;
4862 let source = (msgsource :> lvsource) in
4863 let modehash = findkeyhash conf "listview" in
4864 state.uioh <- coe (object
4865 inherit listview ~source ~trusted:false ~modehash as super
4866 method display =
4867 if state.newerrmsgs
4868 then msgsource#reset;
4869 super#display
4870 end);
4871 G.postRedisplay "msgs";
4874 let quickbookmark ?title () =
4875 match state.layout with
4876 | [] -> ()
4877 | l :: _ ->
4878 let title =
4879 match title with
4880 | None ->
4881 let sec = Unix.gettimeofday () in
4882 let tm = Unix.localtime sec in
4883 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4884 (l.pageno+1)
4885 tm.Unix.tm_mday
4886 tm.Unix.tm_mon
4887 (tm.Unix.tm_year + 1900)
4888 tm.Unix.tm_hour
4889 tm.Unix.tm_min
4890 | Some title -> title
4892 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4895 let setautoscrollspeed step goingdown =
4896 let incr = max 1 ((abs step) / 2) in
4897 let incr = if goingdown then incr else -incr in
4898 let astep = step + incr in
4899 state.autoscroll <- Some astep;
4902 let gotounder = function
4903 | Ulinkgoto (pageno, top) ->
4904 if pageno >= 0
4905 then (
4906 addnav ();
4907 gotopage1 pageno top;
4910 | Ulinkuri s ->
4911 gotouri s
4913 | Uremote (filename, pageno) ->
4914 let path =
4915 if Sys.file_exists filename
4916 then filename
4917 else
4918 let dir = Filename.dirname state.path in
4919 let path = Filename.concat dir filename in
4920 if Sys.file_exists path
4921 then path
4922 else ""
4924 if String.length path > 0
4925 then (
4926 let anchor = getanchor () in
4927 let ranchor = state.path, state.password, anchor, state.origin in
4928 state.origin <- "";
4929 state.anchor <- (pageno, 0.0, 0.0);
4930 state.ranchors <- ranchor :: state.ranchors;
4931 opendoc path "";
4933 else showtext '!' ("Could not find " ^ filename)
4935 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4938 let canpan () =
4939 match conf.columns with
4940 | Csplit _ -> true
4941 | _ -> state.x != 0 || conf.zoom > 1.0
4944 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4946 let existsinrow pageno (columns, coverA, coverB) p =
4947 let last = ((pageno - coverA) mod columns) + columns in
4948 let rec any = function
4949 | [] -> false
4950 | l :: rest ->
4951 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4952 then p l
4953 else (
4954 if not (p l)
4955 then (if l.pageno = last then false else any rest)
4956 else true
4959 any state.layout
4962 let nextpage () =
4963 match state.layout with
4964 | [] ->
4965 let pageno = page_of_y state.y in
4966 gotoghyll (getpagey (pageno+1))
4967 | l :: rest ->
4968 match conf.columns with
4969 | Csingle _ ->
4970 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4971 then
4972 let y = clamp (pgscale state.winh) in
4973 gotoghyll y
4974 else
4975 let pageno = min (l.pageno+1) (state.pagecount-1) in
4976 gotoghyll (getpagey pageno)
4977 | Cmulti ((c, _, _) as cl, _) ->
4978 if conf.presentation
4979 && (existsinrow l.pageno cl
4980 (fun l -> l.pageh > l.pagey + l.pagevh))
4981 then
4982 let y = clamp (pgscale state.winh) in
4983 gotoghyll y
4984 else
4985 let pageno = min (l.pageno+c) (state.pagecount-1) in
4986 gotoghyll (getpagey pageno)
4987 | Csplit (n, _) ->
4988 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4989 then
4990 let pagey, pageh = getpageyh l.pageno in
4991 let pagey = pagey + pageh * l.pagecol in
4992 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4993 gotoghyll (pagey + pageh + ips)
4996 let prevpage () =
4997 match state.layout with
4998 | [] ->
4999 let pageno = page_of_y state.y in
5000 gotoghyll (getpagey (pageno-1))
5001 | l :: _ ->
5002 match conf.columns with
5003 | Csingle _ ->
5004 if conf.presentation && l.pagey != 0
5005 then
5006 gotoghyll (clamp (pgscale ~-(state.winh)))
5007 else
5008 let pageno = max 0 (l.pageno-1) in
5009 gotoghyll (getpagey pageno)
5010 | Cmulti ((c, _, coverB) as cl, _) ->
5011 if conf.presentation &&
5012 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5013 then
5014 gotoghyll (clamp (pgscale ~-(state.winh)))
5015 else
5016 let decr =
5017 if l.pageno = state.pagecount - coverB
5018 then 1
5019 else c
5021 let pageno = max 0 (l.pageno-decr) in
5022 gotoghyll (getpagey pageno)
5023 | Csplit (n, _) ->
5024 let y =
5025 if l.pagecol = 0
5026 then
5027 if l.pageno = 0
5028 then l.pagey
5029 else
5030 let pageno = max 0 (l.pageno-1) in
5031 let pagey, pageh = getpageyh pageno in
5032 pagey + (n-1)*pageh
5033 else
5034 let pagey, pageh = getpageyh l.pageno in
5035 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5037 gotoghyll y
5040 let viewkeyboard key mask =
5041 let enttext te =
5042 let mode = state.mode in
5043 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5044 state.text <- "";
5045 enttext ();
5046 G.postRedisplay "view:enttext"
5048 let ctrl = Wsi.withctrl mask in
5049 let key =
5050 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5052 match key with
5053 | 81 -> (* Q *)
5054 exit 0
5056 | 0xff63 -> (* insert *)
5057 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5058 then (
5059 state.mode <- LinkNav (Ltgendir 0);
5060 gotoy state.y;
5062 else showtext '!' "Keyboard link navigation does not work under rotation"
5064 | 0xff1b | 113 -> (* escape / q *)
5065 begin match state.mstate with
5066 | Mzoomrect _ ->
5067 state.mstate <- Mnone;
5068 Wsi.setcursor Wsi.CURSOR_INHERIT;
5069 G.postRedisplay "kill zoom rect";
5070 | _ ->
5071 begin match state.mode with
5072 | LinkNav _ ->
5073 state.mode <- View;
5074 G.postRedisplay "esc leave linknav"
5075 | _ ->
5076 match state.ranchors with
5077 | [] -> raise Quit
5078 | (path, password, anchor, origin) :: rest ->
5079 state.ranchors <- rest;
5080 state.anchor <- anchor;
5081 state.origin <- origin;
5082 opendoc path password
5083 end;
5084 end;
5086 | 0xff08 -> (* backspace *)
5087 gotoghyll (getnav ~-1)
5089 | 111 -> (* o *)
5090 enteroutlinemode ()
5092 | 117 -> (* u *)
5093 state.rects <- [];
5094 state.text <- "";
5095 G.postRedisplay "dehighlight";
5097 | 47 | 63 -> (* / ? *)
5098 let ondone isforw s =
5099 cbput state.hists.pat s;
5100 state.searchpattern <- s;
5101 search s isforw
5103 let s = String.create 1 in
5104 s.[0] <- Char.chr key;
5105 enttext (s, "", Some (onhist state.hists.pat),
5106 textentry, ondone (key = 47), true)
5108 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5109 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5110 setzoom (conf.zoom +. incr)
5112 | 43 | 0xffab -> (* + *)
5113 let ondone s =
5114 let n =
5115 try int_of_string s with exc ->
5116 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5117 max_int
5119 if n != max_int
5120 then (
5121 conf.pagebias <- n;
5122 state.text <- "page bias is now " ^ string_of_int n;
5125 enttext ("page bias: ", "", None, intentry, ondone, true)
5127 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5128 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5129 setzoom (max 0.01 (conf.zoom -. decr))
5131 | 45 | 0xffad -> (* - *)
5132 let ondone msg = state.text <- msg in
5133 enttext (
5134 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5135 optentry state.mode, ondone, true
5138 | 48 when ctrl -> (* ctrl-0 *)
5139 if conf.zoom = 1.0
5140 then (
5141 state.x <- 0;
5142 gotoy state.y
5144 else setzoom 1.0
5146 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5147 let cols =
5148 match conf.columns with
5149 | Csingle _ | Cmulti _ -> 1
5150 | Csplit (n, _) -> n
5152 let h = state.winh -
5153 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5155 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5156 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5157 then setzoom zoom
5159 | 51 when ctrl -> (* ctrl-3 *)
5160 let fm =
5161 match conf.fitmodel with
5162 | FitWidth -> FitProportional
5163 | FitProportional -> FitPage
5164 | FitPage -> FitWidth
5166 state.text <- "fit model: " ^ fitmodel_to_string fm;
5167 reqlayout conf.angle fm
5169 | 0xffc6 -> (* f9 *)
5170 togglebirdseye ()
5172 | 57 when ctrl -> (* ctrl-9 *)
5173 togglebirdseye ()
5175 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5176 when not ctrl -> (* 0..9 *)
5177 let ondone s =
5178 let n =
5179 try int_of_string s with exc ->
5180 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5183 if n >= 0
5184 then (
5185 addnav ();
5186 cbput state.hists.pag (string_of_int n);
5187 gotopage1 (n + conf.pagebias - 1) 0;
5190 let pageentry text key =
5191 match Char.unsafe_chr key with
5192 | 'g' -> TEdone text
5193 | _ -> intentry text key
5195 let text = "x" in text.[0] <- Char.chr key;
5196 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5198 | 98 -> (* b *)
5199 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5200 reshape state.winw state.winh;
5202 | 108 -> (* l *)
5203 conf.hlinks <- not conf.hlinks;
5204 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5205 G.postRedisplay "toggle highlightlinks";
5207 | 70 -> (* F *)
5208 state.glinks <- true;
5209 let mode = state.mode in
5210 state.mode <- Textentry (
5211 (":", "", None, linknentry, linkndone gotounder, false),
5212 (fun _ ->
5213 state.glinks <- false;
5214 state.mode <- mode)
5216 state.text <- "";
5217 G.postRedisplay "view:linkent(F)"
5219 | 121 -> (* y *)
5220 state.glinks <- true;
5221 let mode = state.mode in
5222 state.mode <- Textentry (
5224 ":", "", None, linknentry, linkndone (fun under ->
5225 selstring (undertext under);
5226 ), false
5228 fun _ ->
5229 state.glinks <- false;
5230 state.mode <- mode
5232 state.text <- "";
5233 G.postRedisplay "view:linkent"
5235 | 97 -> (* a *)
5236 begin match state.autoscroll with
5237 | Some step ->
5238 conf.autoscrollstep <- step;
5239 state.autoscroll <- None
5240 | None ->
5241 if conf.autoscrollstep = 0
5242 then state.autoscroll <- Some 1
5243 else state.autoscroll <- Some conf.autoscrollstep
5246 | 112 when ctrl -> (* ctrl-p *)
5247 launchpath ()
5249 | 80 -> (* P *)
5250 setpresentationmode (not conf.presentation);
5251 showtext ' ' ("presentation mode " ^
5252 if conf.presentation then "on" else "off");
5254 | 102 -> (* f *)
5255 if List.mem Wsi.Fullscreen state.winstate
5256 then Wsi.reshape conf.cwinw conf.cwinh
5257 else Wsi.fullscreen ()
5259 | 112 | 78 -> (* p|N *)
5260 search state.searchpattern false
5262 | 110 | 0xffc0 -> (* n|F3 *)
5263 search state.searchpattern true
5265 | 116 -> (* t *)
5266 begin match state.layout with
5267 | [] -> ()
5268 | l :: _ ->
5269 gotoghyll (getpagey l.pageno)
5272 | 32 -> (* space *)
5273 nextpage ()
5275 | 0xff9f | 0xffff -> (* delete *)
5276 prevpage ()
5278 | 61 -> (* = *)
5279 showtext ' ' (describe_location ());
5281 | 119 -> (* w *)
5282 begin match state.layout with
5283 | [] -> ()
5284 | l :: _ ->
5285 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5286 G.postRedisplay "w"
5289 | 39 -> (* ' *)
5290 enterbookmarkmode ()
5292 | 104 | 0xffbe -> (* h|F1 *)
5293 enterhelpmode ()
5295 | 105 -> (* i *)
5296 enterinfomode ()
5298 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5299 entermsgsmode ()
5301 | 109 -> (* m *)
5302 let ondone s =
5303 match state.layout with
5304 | l :: _ ->
5305 if String.length s > 0
5306 then
5307 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5308 | _ -> ()
5310 enttext ("bookmark: ", "", None, textentry, ondone, true)
5312 | 126 -> (* ~ *)
5313 quickbookmark ();
5314 showtext ' ' "Quick bookmark added";
5316 | 122 -> (* z *)
5317 begin match state.layout with
5318 | l :: _ ->
5319 let rect = getpdimrect l.pagedimno in
5320 let w, h =
5321 if conf.crophack
5322 then
5323 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5324 truncate (1.2 *. (rect.(3) -. rect.(0))))
5325 else
5326 (truncate (rect.(1) -. rect.(0)),
5327 truncate (rect.(3) -. rect.(0)))
5329 let w = truncate ((float w)*.conf.zoom)
5330 and h = truncate ((float h)*.conf.zoom) in
5331 if w != 0 && h != 0
5332 then (
5333 state.anchor <- getanchor ();
5334 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5336 G.postRedisplay "z";
5338 | [] -> ()
5341 | 60 | 62 -> (* < > *)
5342 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5344 | 91 | 93 -> (* [ ] *)
5345 conf.colorscale <-
5346 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5348 G.postRedisplay "brightness";
5350 | 99 when state.mode = View -> (* [alt]-c *)
5351 if Wsi.withalt mask
5352 then (
5353 if conf.zoom > 1.0
5354 then
5355 let m = (wadjsb state.winw - state.w) / 2 in
5356 state.x <- m;
5357 gotoy_and_clear_text state.y
5359 else
5360 let (c, a, b), z =
5361 match state.prevcolumns with
5362 | None -> (1, 0, 0), 1.0
5363 | Some (columns, z) ->
5364 let cab =
5365 match columns with
5366 | Csplit (c, _) -> -c, 0, 0
5367 | Cmulti ((c, a, b), _) -> c, a, b
5368 | Csingle _ -> 1, 0, 0
5370 cab, z
5372 setcolumns View c a b;
5373 setzoom z
5375 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5376 setzoom state.prevzoom
5378 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5379 begin match state.autoscroll with
5380 | None ->
5381 begin match state.mode with
5382 | Birdseye beye -> upbirdseye 1 beye
5383 | _ ->
5384 if ctrl
5385 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5386 else (
5387 if not (Wsi.withshift mask) && conf.presentation
5388 then prevpage ()
5389 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5392 | Some n ->
5393 setautoscrollspeed n false
5396 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5397 begin match state.autoscroll with
5398 | None ->
5399 begin match state.mode with
5400 | Birdseye beye -> downbirdseye 1 beye
5401 | _ ->
5402 if ctrl
5403 then gotoy_and_clear_text (clamp (state.winh/2))
5404 else (
5405 if not (Wsi.withshift mask) && conf.presentation
5406 then nextpage ()
5407 else gotoy_and_clear_text (clamp conf.scrollstep)
5410 | Some n ->
5411 setautoscrollspeed n true
5414 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5415 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5416 if canpan ()
5417 then
5418 let dx =
5419 if ctrl
5420 then state.winw / 2
5421 else conf.hscrollstep
5423 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5424 state.x <- panbound (state.x + dx);
5425 gotoy_and_clear_text state.y
5426 else (
5427 state.text <- "";
5428 G.postRedisplay "left/right"
5431 | 0xff55 | 0xff9a -> (* (kp) prior *)
5432 let y =
5433 if ctrl
5434 then
5435 match state.layout with
5436 | [] -> state.y
5437 | l :: _ -> state.y - l.pagey
5438 else
5439 clamp (pgscale (-state.winh))
5441 gotoghyll y
5443 | 0xff56 | 0xff9b -> (* (kp) next *)
5444 let y =
5445 if ctrl
5446 then
5447 match List.rev state.layout with
5448 | [] -> state.y
5449 | l :: _ -> getpagey l.pageno
5450 else
5451 clamp (pgscale state.winh)
5453 gotoghyll y
5455 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5456 gotoghyll 0
5457 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5458 gotoghyll (clamp state.maxy)
5460 | 0xff53 | 0xff98
5461 when Wsi.withalt mask -> (* alt-(kp) right *)
5462 gotoghyll (getnav 1)
5463 | 0xff51 | 0xff96
5464 when Wsi.withalt mask -> (* alt-(kp) left *)
5465 gotoghyll (getnav ~-1)
5467 | 114 -> (* r *)
5468 reload ()
5470 | 118 when conf.debug -> (* v *)
5471 state.rects <- [];
5472 List.iter (fun l ->
5473 match getopaque l.pageno with
5474 | None -> ()
5475 | Some opaque ->
5476 let x0, y0, x1, y1 = pagebbox opaque in
5477 let a,b = float x0, float y0 in
5478 let c,d = float x1, float y0 in
5479 let e,f = float x1, float y1 in
5480 let h,j = float x0, float y1 in
5481 let rect = (a,b,c,d,e,f,h,j) in
5482 debugrect rect;
5483 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5484 ) state.layout;
5485 G.postRedisplay "v";
5487 | _ ->
5488 vlog "huh? %s" (Wsi.keyname key)
5491 let linknavkeyboard key mask linknav =
5492 let getpage pageno =
5493 let rec loop = function
5494 | [] -> None
5495 | l :: _ when l.pageno = pageno -> Some l
5496 | _ :: rest -> loop rest
5497 in loop state.layout
5499 let doexact (pageno, n) =
5500 match getopaque pageno, getpage pageno with
5501 | Some opaque, Some l ->
5502 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5503 then
5504 let under = getlink opaque n in
5505 G.postRedisplay "link gotounder";
5506 gotounder under;
5507 state.mode <- View;
5508 else
5509 let opt, dir =
5510 match key with
5511 | 0xff50 -> (* home *)
5512 Some (findlink opaque LDfirst), -1
5514 | 0xff57 -> (* end *)
5515 Some (findlink opaque LDlast), 1
5517 | 0xff51 -> (* left *)
5518 Some (findlink opaque (LDleft n)), -1
5520 | 0xff53 -> (* right *)
5521 Some (findlink opaque (LDright n)), 1
5523 | 0xff52 -> (* up *)
5524 Some (findlink opaque (LDup n)), -1
5526 | 0xff54 -> (* down *)
5527 Some (findlink opaque (LDdown n)), 1
5529 | _ -> None, 0
5531 let pwl l dir =
5532 begin match findpwl l.pageno dir with
5533 | Pwlnotfound -> ()
5534 | Pwl pageno ->
5535 let notfound dir =
5536 state.mode <- LinkNav (Ltgendir dir);
5537 let y, h = getpageyh pageno in
5538 let y =
5539 if dir < 0
5540 then y + h - state.winh
5541 else y
5543 gotoy y
5545 begin match getopaque pageno, getpage pageno with
5546 | Some opaque, Some _ ->
5547 let link =
5548 let ld = if dir > 0 then LDfirst else LDlast in
5549 findlink opaque ld
5551 begin match link with
5552 | Lfound m ->
5553 showlinktype (getlink opaque m);
5554 state.mode <- LinkNav (Ltexact (pageno, m));
5555 G.postRedisplay "linknav jpage";
5556 | _ -> notfound dir
5557 end;
5558 | _ -> notfound dir
5559 end;
5560 end;
5562 begin match opt with
5563 | Some Lnotfound -> pwl l dir;
5564 | Some (Lfound m) ->
5565 if m = n
5566 then pwl l dir
5567 else (
5568 let _, y0, _, y1 = getlinkrect opaque m in
5569 if y0 < l.pagey
5570 then gotopage1 l.pageno y0
5571 else (
5572 let d = fstate.fontsize + 1 in
5573 if y1 - l.pagey > l.pagevh - d
5574 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5575 else G.postRedisplay "linknav";
5577 showlinktype (getlink opaque m);
5578 state.mode <- LinkNav (Ltexact (l.pageno, m));
5581 | None -> viewkeyboard key mask
5582 end;
5583 | _ -> viewkeyboard key mask
5585 if key = 0xff63
5586 then (
5587 state.mode <- View;
5588 G.postRedisplay "leave linknav"
5590 else
5591 match linknav with
5592 | Ltgendir _ -> viewkeyboard key mask
5593 | Ltexact exact -> doexact exact
5596 let keyboard key mask =
5597 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5598 then wcmd "interrupt"
5599 else state.uioh <- state.uioh#key key mask
5602 let birdseyekeyboard key mask
5603 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5604 let incr =
5605 match conf.columns with
5606 | Csingle _ -> 1
5607 | Cmulti ((c, _, _), _) -> c
5608 | Csplit _ -> failwith "bird's eye split mode"
5610 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5611 match key with
5612 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5613 let y, h = getpageyh pageno in
5614 let top = (state.winh - h) / 2 in
5615 gotoy (max 0 (y - top))
5616 | 0xff0d (* enter *)
5617 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5618 | 0xff1b -> leavebirdseye beye true (* escape *)
5619 | 0xff52 -> upbirdseye incr beye (* up *)
5620 | 0xff54 -> downbirdseye incr beye (* down *)
5621 | 0xff51 -> upbirdseye 1 beye (* left *)
5622 | 0xff53 -> downbirdseye 1 beye (* right *)
5624 | 0xff55 -> (* prior *)
5625 begin match state.layout with
5626 | l :: _ ->
5627 if l.pagey != 0
5628 then (
5629 state.mode <- Birdseye (
5630 oconf, leftx, l.pageno, hooverpageno, anchor
5632 gotopage1 l.pageno 0;
5634 else (
5635 let layout = layout (state.y-state.winh) (pgh state.layout) in
5636 match layout with
5637 | [] -> gotoy (clamp (-state.winh))
5638 | l :: _ ->
5639 state.mode <- Birdseye (
5640 oconf, leftx, l.pageno, hooverpageno, anchor
5642 gotopage1 l.pageno 0
5645 | [] -> gotoy (clamp (-state.winh))
5646 end;
5648 | 0xff56 -> (* next *)
5649 begin match List.rev state.layout with
5650 | l :: _ ->
5651 let layout = layout (state.y + (pgh state.layout)) state.winh in
5652 begin match layout with
5653 | [] ->
5654 let incr = l.pageh - l.pagevh in
5655 if incr = 0
5656 then (
5657 state.mode <-
5658 Birdseye (
5659 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5661 G.postRedisplay "birdseye pagedown";
5663 else gotoy (clamp (incr + conf.interpagespace*2));
5665 | l :: _ ->
5666 state.mode <-
5667 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5668 gotopage1 l.pageno 0;
5671 | [] -> gotoy (clamp state.winh)
5672 end;
5674 | 0xff50 -> (* home *)
5675 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5676 gotopage1 0 0
5678 | 0xff57 -> (* end *)
5679 let pageno = state.pagecount - 1 in
5680 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5681 if not (pagevisible state.layout pageno)
5682 then
5683 let h =
5684 match List.rev state.pdims with
5685 | [] -> state.winh
5686 | (_, _, h, _) :: _ -> h
5688 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5689 else G.postRedisplay "birdseye end";
5690 | _ -> viewkeyboard key mask
5693 let drawpage l =
5694 let color =
5695 match state.mode with
5696 | Textentry _ -> scalecolor 0.4
5697 | LinkNav _
5698 | View -> scalecolor 1.0
5699 | Birdseye (_, _, pageno, hooverpageno, _) ->
5700 if l.pageno = hooverpageno
5701 then scalecolor 0.9
5702 else (
5703 if l.pageno = pageno
5704 then scalecolor 1.0
5705 else scalecolor 0.8
5708 drawtiles l color;
5711 let postdrawpage l linkindexbase =
5712 match getopaque l.pageno with
5713 | Some opaque ->
5714 if tileready l l.pagex l.pagey
5715 then
5716 let x = l.pagedispx - l.pagex
5717 and y = l.pagedispy - l.pagey in
5718 let hlmask =
5719 match conf.columns with
5720 | Csingle _ | Cmulti _ ->
5721 (if conf.hlinks then 1 else 0)
5722 + (if state.glinks
5723 && not (isbirdseye state.mode) then 2 else 0)
5724 | _ -> 0
5726 let s =
5727 match state.mode with
5728 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5729 | _ -> ""
5731 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5732 else 0
5733 | _ -> 0
5736 let scrollindicator () =
5737 let sbw, ph, sh = state.uioh#scrollph in
5738 let sbh, pw, sw = state.uioh#scrollpw in
5740 GlDraw.color (0.64, 0.64, 0.64);
5741 GlDraw.rect
5742 (float (state.winw - sbw), 0.)
5743 (float state.winw, float state.winh)
5745 GlDraw.rect
5746 (0., float (state.winh - sbh))
5747 (float (wadjsb state.winw - 1), float state.winh)
5749 GlDraw.color (0.0, 0.0, 0.0);
5751 GlDraw.rect
5752 (float (state.winw - sbw), ph)
5753 (float state.winw, ph +. sh)
5755 GlDraw.rect
5756 (pw, float (state.winh - sbh))
5757 (pw +. sw, float state.winh)
5761 let showsel () =
5762 match state.mstate with
5763 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5766 | Msel ((x0, y0), (x1, y1)) ->
5767 let rec loop = function
5768 | l :: ls ->
5769 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5770 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5771 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5772 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5773 then
5774 match getopaque l.pageno with
5775 | Some opaque ->
5776 let x0, y0 = pagetranslatepoint l x0 y0 in
5777 let x1, y1 = pagetranslatepoint l x1 y1 in
5778 seltext opaque (x0, y0, x1, y1);
5779 | _ -> ()
5780 else loop ls
5781 | [] -> ()
5783 loop state.layout
5786 let showrects rects =
5787 Gl.enable `blend;
5788 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5789 GlDraw.polygon_mode `both `fill;
5790 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5791 List.iter
5792 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5793 List.iter (fun l ->
5794 if l.pageno = pageno
5795 then (
5796 let dx = float (l.pagedispx - l.pagex) in
5797 let dy = float (l.pagedispy - l.pagey) in
5798 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5799 GlDraw.begins `quads;
5801 GlDraw.vertex2 (x0+.dx, y0+.dy);
5802 GlDraw.vertex2 (x1+.dx, y1+.dy);
5803 GlDraw.vertex2 (x2+.dx, y2+.dy);
5804 GlDraw.vertex2 (x3+.dx, y3+.dy);
5806 GlDraw.ends ();
5808 ) state.layout
5809 ) rects
5811 Gl.disable `blend;
5814 let display () =
5815 GlClear.color (scalecolor2 conf.bgcolor);
5816 GlClear.clear [`color];
5817 List.iter drawpage state.layout;
5818 let rects =
5819 match state.mode with
5820 | LinkNav (Ltexact (pageno, linkno)) ->
5821 begin match getopaque pageno with
5822 | Some opaque ->
5823 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5824 (pageno, 5, (
5825 float x0, float y0,
5826 float x1, float y0,
5827 float x1, float y1,
5828 float x0, float y1)
5829 ) :: state.rects
5830 | None -> state.rects
5832 | _ -> state.rects
5834 showrects rects;
5835 let rec postloop linkindexbase = function
5836 | l :: rest ->
5837 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5838 postloop linkindexbase rest
5839 | [] -> ()
5841 showsel ();
5842 postloop 0 state.layout;
5843 state.uioh#display;
5844 begin match state.mstate with
5845 | Mzoomrect ((x0, y0), (x1, y1)) ->
5846 Gl.enable `blend;
5847 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5848 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5849 GlDraw.rect (float x0, float y0)
5850 (float x1, float y1);
5851 Gl.disable `blend;
5852 | _ -> ()
5853 end;
5854 enttext ();
5855 scrollindicator ();
5856 Wsi.swapb ();
5859 let zoomrect x y x1 y1 =
5860 let x0 = min x x1
5861 and x1 = max x x1
5862 and y0 = min y y1 in
5863 gotoy (state.y + y0);
5864 state.anchor <- getanchor ();
5865 let zoom = (float state.w) /. float (x1 - x0) in
5866 let margin =
5867 match conf.fitmodel, conf.columns with
5868 | FitPage, Csplit _ ->
5869 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5871 | _, _ ->
5872 let adjw = wadjsb state.winw in
5873 if state.w < adjw
5874 then (adjw - state.w) / 2
5875 else 0
5877 state.x <- (state.x + margin) - x0;
5878 setzoom zoom;
5879 Wsi.setcursor Wsi.CURSOR_INHERIT;
5880 state.mstate <- Mnone;
5883 let scrollx x =
5884 let winw = wadjsb state.winw - 1 in
5885 let s = float x /. float winw in
5886 let destx = truncate (float (state.w + winw) *. s) in
5887 state.x <- winw - destx;
5888 gotoy_and_clear_text state.y;
5889 state.mstate <- Mscrollx;
5892 let scrolly y =
5893 let s = float y /. float state.winh in
5894 let desty = truncate (float (state.maxy - state.winh) *. s) in
5895 gotoy_and_clear_text desty;
5896 state.mstate <- Mscrolly;
5899 let viewmouse button down x y mask =
5900 match button with
5901 | n when (n == 4 || n == 5) && not down ->
5902 if Wsi.withctrl mask
5903 then (
5904 match state.mstate with
5905 | Mzoom (oldn, i) ->
5906 if oldn = n
5907 then (
5908 if i = 2
5909 then
5910 let incr =
5911 match n with
5912 | 5 ->
5913 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5914 | _ ->
5915 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5917 let zoom = conf.zoom -. incr in
5918 setzoom zoom;
5919 state.mstate <- Mzoom (n, 0);
5920 else
5921 state.mstate <- Mzoom (n, i+1);
5923 else state.mstate <- Mzoom (n, 0)
5925 | _ -> state.mstate <- Mzoom (n, 0)
5927 else (
5928 match state.autoscroll with
5929 | Some step -> setautoscrollspeed step (n=4)
5930 | None ->
5931 if conf.wheelbypage || conf.presentation
5932 then (
5933 if n = 4
5934 then prevpage ()
5935 else nextpage ()
5937 else
5938 let incr =
5939 if n = 4
5940 then -conf.scrollstep
5941 else conf.scrollstep
5943 let incr = incr * 2 in
5944 let y = clamp incr in
5945 gotoy_and_clear_text y
5948 | n when (n = 6 || n = 7) && not down && canpan () ->
5949 state.x <-
5950 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5951 gotoy_and_clear_text state.y
5953 | 1 when Wsi.withshift mask ->
5954 state.mstate <- Mnone;
5955 if not down
5956 then (
5957 match unproject x y with
5958 | Some (pageno, ux, uy) ->
5959 let cmd = Printf.sprintf
5960 "%s %s %d %d %d"
5961 conf.stcmd state.path pageno ux uy
5963 popen cmd []
5964 | None -> ()
5967 | 1 when Wsi.withctrl mask ->
5968 if down
5969 then (
5970 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5971 state.mstate <- Mpan (x, y)
5973 else
5974 state.mstate <- Mnone
5976 | 3 ->
5977 if down
5978 then (
5979 Wsi.setcursor Wsi.CURSOR_CYCLE;
5980 let p = (x, y) in
5981 state.mstate <- Mzoomrect (p, p)
5983 else (
5984 match state.mstate with
5985 | Mzoomrect ((x0, y0), _) ->
5986 if abs (x-x0) > 10 && abs (y - y0) > 10
5987 then zoomrect x0 y0 x y
5988 else (
5989 state.mstate <- Mnone;
5990 Wsi.setcursor Wsi.CURSOR_INHERIT;
5991 G.postRedisplay "kill accidental zoom rect";
5993 | _ ->
5994 Wsi.setcursor Wsi.CURSOR_INHERIT;
5995 state.mstate <- Mnone
5998 | 1 when x > state.winw - vscrollw () ->
5999 if down
6000 then
6001 let _, position, sh = state.uioh#scrollph in
6002 if y > truncate position && y < truncate (position +. sh)
6003 then state.mstate <- Mscrolly
6004 else scrolly y
6005 else
6006 state.mstate <- Mnone
6008 | 1 when y > state.winh - hscrollh () ->
6009 if down
6010 then
6011 let _, position, sw = state.uioh#scrollpw in
6012 if x > truncate position && x < truncate (position +. sw)
6013 then state.mstate <- Mscrollx
6014 else scrollx x
6015 else
6016 state.mstate <- Mnone
6018 | 1 ->
6019 let dest = if down then getunder x y else Unone in
6020 begin match dest with
6021 | Ulinkgoto _
6022 | Ulinkuri _
6023 | Uremote _
6024 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6025 gotounder dest
6027 | Unone when down ->
6028 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6029 state.mstate <- Mpan (x, y);
6031 | Unone | Utext _ ->
6032 if down
6033 then (
6034 if conf.angle mod 360 = 0
6035 then (
6036 state.mstate <- Msel ((x, y), (x, y));
6037 G.postRedisplay "mouse select";
6040 else (
6041 match state.mstate with
6042 | Mnone -> ()
6044 | Mzoom _ | Mscrollx | Mscrolly ->
6045 state.mstate <- Mnone
6047 | Mzoomrect ((x0, y0), _) ->
6048 zoomrect x0 y0 x y
6050 | Mpan _ ->
6051 Wsi.setcursor Wsi.CURSOR_INHERIT;
6052 state.mstate <- Mnone
6054 | Msel ((x0, y0), (x1, y1)) ->
6055 let rec loop = function
6056 | [] -> ()
6057 | l :: rest ->
6058 let inside =
6059 let a0 = l.pagedispy in
6060 let a1 = a0 + l.pagevh in
6061 let b0 = l.pagedispx in
6062 let b1 = b0 + l.pagevw in
6063 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6064 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6066 if inside
6067 then
6068 match getopaque l.pageno with
6069 | Some opaque ->
6070 begin
6071 match Ne.pipe () with
6072 | Ne.Exn exn ->
6073 showtext '!'
6074 (Printf.sprintf
6075 "can not create sel pipe: %s"
6076 (exntos exn));
6077 | Ne.Res (r, w) ->
6078 let doclose what fd =
6079 Ne.clo fd (fun msg ->
6080 dolog "%s close failed: %s" what msg)
6083 popen conf.selcmd [r, 0; w, -1];
6084 copysel w opaque;
6085 doclose "pipe/r" r;
6086 G.postRedisplay "copysel";
6087 with exn ->
6088 dolog "can not execute %S: %s"
6089 conf.selcmd (exntos exn);
6090 doclose "pipe/r" r;
6091 doclose "pipe/w" w;
6093 | None -> ()
6094 else loop rest
6096 loop state.layout;
6097 Wsi.setcursor Wsi.CURSOR_INHERIT;
6098 state.mstate <- Mnone;
6102 | _ -> ()
6105 let birdseyemouse button down x y mask
6106 (conf, leftx, _, hooverpageno, anchor) =
6107 match button with
6108 | 1 when down ->
6109 let rec loop = function
6110 | [] -> ()
6111 | l :: rest ->
6112 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6113 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6114 then (
6115 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6117 else loop rest
6119 loop state.layout
6120 | 3 -> ()
6121 | _ -> viewmouse button down x y mask
6124 let mouse button down x y mask =
6125 state.uioh <- state.uioh#button button down x y mask;
6128 let motion ~x ~y =
6129 state.uioh <- state.uioh#motion x y
6132 let pmotion ~x ~y =
6133 state.uioh <- state.uioh#pmotion x y;
6136 let uioh = object
6137 method display = ()
6139 method key key mask =
6140 begin match state.mode with
6141 | Textentry textentry -> textentrykeyboard key mask textentry
6142 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6143 | View -> viewkeyboard key mask
6144 | LinkNav linknav -> linknavkeyboard key mask linknav
6145 end;
6146 state.uioh
6148 method button button bstate x y mask =
6149 begin match state.mode with
6150 | LinkNav _
6151 | View -> viewmouse button bstate x y mask
6152 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6153 | Textentry _ -> ()
6154 end;
6155 state.uioh
6157 method motion x y =
6158 begin match state.mode with
6159 | Textentry _ -> ()
6160 | View | Birdseye _ | LinkNav _ ->
6161 match state.mstate with
6162 | Mzoom _ | Mnone -> ()
6164 | Mpan (x0, y0) ->
6165 let dx = x - x0
6166 and dy = y0 - y in
6167 state.mstate <- Mpan (x, y);
6168 if canpan ()
6169 then state.x <- panbound (state.x + dx);
6170 let y = clamp dy in
6171 gotoy_and_clear_text y
6173 | Msel (a, _) ->
6174 state.mstate <- Msel (a, (x, y));
6175 G.postRedisplay "motion select";
6177 | Mscrolly ->
6178 let y = min state.winh (max 0 y) in
6179 scrolly y
6181 | Mscrollx ->
6182 let x = min state.winw (max 0 x) in
6183 scrollx x
6185 | Mzoomrect (p0, _) ->
6186 state.mstate <- Mzoomrect (p0, (x, y));
6187 G.postRedisplay "motion zoomrect";
6188 end;
6189 state.uioh
6191 method pmotion x y =
6192 begin match state.mode with
6193 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6194 let rec loop = function
6195 | [] ->
6196 if hooverpageno != -1
6197 then (
6198 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6199 G.postRedisplay "pmotion birdseye no hoover";
6201 | l :: rest ->
6202 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6203 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6204 then (
6205 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6206 G.postRedisplay "pmotion birdseye hoover";
6208 else loop rest
6210 loop state.layout
6212 | Textentry _ -> ()
6214 | LinkNav _
6215 | View ->
6216 match state.mstate with
6217 | Mnone -> updateunder x y
6218 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6220 end;
6221 state.uioh
6223 method infochanged _ = ()
6225 method scrollph =
6226 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6227 let p, h =
6228 if maxy = 0
6229 then 0.0, float state.winh
6230 else scrollph state.y maxy
6232 vscrollw (), p, h
6234 method scrollpw =
6235 let winw = wadjsb state.winw in
6236 let fwinw = float winw in
6237 let sw =
6238 let sw = fwinw /. float state.w in
6239 let sw = fwinw *. sw in
6240 max sw (float conf.scrollh)
6242 let position =
6243 let maxx = state.w + winw in
6244 let x = winw - state.x in
6245 let percent = float x /. float maxx in
6246 (fwinw -. sw) *. percent
6248 hscrollh (), position, sw
6250 method modehash =
6251 let modename =
6252 match state.mode with
6253 | LinkNav _ -> "links"
6254 | Textentry _ -> "textentry"
6255 | Birdseye _ -> "birdseye"
6256 | View -> "view"
6258 findkeyhash conf modename
6260 method eformsgs = true
6261 end;;
6263 module Config =
6264 struct
6265 open Parser
6267 let fontpath = ref "";;
6269 module KeyMap =
6270 Map.Make (struct type t = (int * int) let compare = compare end);;
6272 let unent s =
6273 let l = String.length s in
6274 let b = Buffer.create l in
6275 unent b s 0 l;
6276 Buffer.contents b;
6279 let home =
6280 try Sys.getenv "HOME"
6281 with exn ->
6282 prerr_endline
6283 ("Can not determine home directory location: " ^ exntos exn);
6287 let modifier_of_string = function
6288 | "alt" -> Wsi.altmask
6289 | "shift" -> Wsi.shiftmask
6290 | "ctrl" | "control" -> Wsi.ctrlmask
6291 | "meta" -> Wsi.metamask
6292 | _ -> 0
6295 let key_of_string =
6296 let r = Str.regexp "-" in
6297 fun s ->
6298 let elems = Str.full_split r s in
6299 let f n k m =
6300 let g s =
6301 let m1 = modifier_of_string s in
6302 if m1 = 0
6303 then (Wsi.namekey s, m)
6304 else (k, m lor m1)
6305 in function
6306 | Str.Delim s when n land 1 = 0 -> g s
6307 | Str.Text s -> g s
6308 | Str.Delim _ -> (k, m)
6310 let rec loop n k m = function
6311 | [] -> (k, m)
6312 | x :: xs ->
6313 let k, m = f n k m x in
6314 loop (n+1) k m xs
6316 loop 0 0 0 elems
6319 let keys_of_string =
6320 let r = Str.regexp "[ \t]" in
6321 fun s ->
6322 let elems = Str.split r s in
6323 List.map key_of_string elems
6326 let copykeyhashes c =
6327 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6330 let config_of c attrs =
6331 let apply c k v =
6333 match k with
6334 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6335 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6336 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6337 | "preload" -> { c with preload = bool_of_string v }
6338 | "page-bias" -> { c with pagebias = int_of_string v }
6339 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6340 | "horizontal-scroll-step" ->
6341 { c with hscrollstep = max (int_of_string v) 1 }
6342 | "auto-scroll-step" ->
6343 { c with autoscrollstep = max 0 (int_of_string v) }
6344 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6345 | "crop-hack" -> { c with crophack = bool_of_string v }
6346 | "throttle" ->
6347 let mw =
6348 match String.lowercase v with
6349 | "true" -> Some infinity
6350 | "false" -> None
6351 | f -> Some (float_of_string f)
6353 { c with maxwait = mw}
6354 | "highlight-links" -> { c with hlinks = bool_of_string v }
6355 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6356 | "vertical-margin" ->
6357 { c with interpagespace = max 0 (int_of_string v) }
6358 | "zoom" ->
6359 let zoom = float_of_string v /. 100. in
6360 let zoom = max zoom 0.0 in
6361 { c with zoom = zoom }
6362 | "presentation" -> { c with presentation = bool_of_string v }
6363 | "rotation-angle" -> { c with angle = int_of_string v }
6364 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6365 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6366 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6367 | "proportional-display" ->
6368 let fm =
6369 if bool_of_string v
6370 then FitProportional
6371 else FitWidth
6373 { c with fitmodel = fm }
6374 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6375 | "pixmap-cache-size" ->
6376 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6377 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6378 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6379 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6380 | "persistent-location" -> { c with jumpback = bool_of_string v }
6381 | "background-color" -> { c with bgcolor = color_of_string v }
6382 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6383 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6384 | "mupdf-store-size" ->
6385 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6386 | "checkers" -> { c with checkers = bool_of_string v }
6387 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6388 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6389 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6390 | "uri-launcher" -> { c with urilauncher = unent v }
6391 | "path-launcher" -> { c with pathlauncher = unent v }
6392 | "color-space" -> { c with colorspace = colorspace_of_string v }
6393 | "invert-colors" -> { c with invert = bool_of_string v }
6394 | "brightness" -> { c with colorscale = float_of_string v }
6395 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6396 | "ghyllscroll" ->
6397 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6398 | "columns" ->
6399 let (n, _, _) as nab = multicolumns_of_string v in
6400 if n < 0
6401 then { c with columns = Csplit (-n, [||]) }
6402 else { c with columns = Cmulti (nab, [||]) }
6403 | "birds-eye-columns" ->
6404 { c with beyecolumns = Some (max (int_of_string v) 2) }
6405 | "selection-command" -> { c with selcmd = unent v }
6406 | "synctex-command" -> { c with stcmd = unent v }
6407 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6408 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6409 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6410 | "use-pbo" -> { c with usepbo = bool_of_string v }
6411 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6412 | "horizontal-scrollbar-visible" ->
6413 let b =
6414 if bool_of_string v
6415 then c.scrollb lor scrollbhv
6416 else c.scrollb land (lnot scrollbhv)
6418 { c with scrollb = b }
6419 | "vertical-scrollbar-visible" ->
6420 let b =
6421 if bool_of_string v
6422 then c.scrollb lor scrollbvv
6423 else c.scrollb land (lnot scrollbvv)
6425 { c with scrollb = b }
6426 | _ -> c
6427 with exn ->
6428 prerr_endline ("Error processing attribute (`" ^
6429 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6432 let rec fold c = function
6433 | [] -> c
6434 | (k, v) :: rest ->
6435 let c = apply c k v in
6436 fold c rest
6438 fold { c with keyhashes = copykeyhashes c } attrs;
6441 let fromstring f pos n v d =
6442 try f v
6443 with exn ->
6444 dolog "Error processing attribute (%S=%S) at %d\n%s"
6445 n v pos (exntos exn)
6450 let bookmark_of attrs =
6451 let rec fold title page rely visy = function
6452 | ("title", v) :: rest -> fold v page rely visy rest
6453 | ("page", v) :: rest -> fold title v rely visy rest
6454 | ("rely", v) :: rest -> fold title page v visy rest
6455 | ("visy", v) :: rest -> fold title page rely v rest
6456 | _ :: rest -> fold title page rely visy rest
6457 | [] -> title, page, rely, visy
6459 fold "invalid" "0" "0" "0" attrs
6462 let doc_of attrs =
6463 let rec fold path page rely pan visy = function
6464 | ("path", v) :: rest -> fold v page rely pan visy rest
6465 | ("page", v) :: rest -> fold path v rely pan visy rest
6466 | ("rely", v) :: rest -> fold path page v pan visy rest
6467 | ("pan", v) :: rest -> fold path page rely v visy rest
6468 | ("visy", v) :: rest -> fold path page rely pan v rest
6469 | _ :: rest -> fold path page rely pan visy rest
6470 | [] -> path, page, rely, pan, visy
6472 fold "" "0" "0" "0" "0" attrs
6475 let map_of attrs =
6476 let rec fold rs ls = function
6477 | ("out", v) :: rest -> fold v ls rest
6478 | ("in", v) :: rest -> fold rs v rest
6479 | _ :: rest -> fold ls rs rest
6480 | [] -> ls, rs
6482 fold "" "" attrs
6485 let setconf dst src =
6486 dst.scrollbw <- src.scrollbw;
6487 dst.scrollh <- src.scrollh;
6488 dst.icase <- src.icase;
6489 dst.preload <- src.preload;
6490 dst.pagebias <- src.pagebias;
6491 dst.verbose <- src.verbose;
6492 dst.scrollstep <- src.scrollstep;
6493 dst.maxhfit <- src.maxhfit;
6494 dst.crophack <- src.crophack;
6495 dst.autoscrollstep <- src.autoscrollstep;
6496 dst.maxwait <- src.maxwait;
6497 dst.hlinks <- src.hlinks;
6498 dst.underinfo <- src.underinfo;
6499 dst.interpagespace <- src.interpagespace;
6500 dst.zoom <- src.zoom;
6501 dst.presentation <- src.presentation;
6502 dst.angle <- src.angle;
6503 dst.cwinw <- src.cwinw;
6504 dst.cwinh <- src.cwinh;
6505 dst.savebmarks <- src.savebmarks;
6506 dst.memlimit <- src.memlimit;
6507 dst.fitmodel <- src.fitmodel;
6508 dst.texcount <- src.texcount;
6509 dst.sliceheight <- src.sliceheight;
6510 dst.thumbw <- src.thumbw;
6511 dst.jumpback <- src.jumpback;
6512 dst.bgcolor <- src.bgcolor;
6513 dst.tilew <- src.tilew;
6514 dst.tileh <- src.tileh;
6515 dst.mustoresize <- src.mustoresize;
6516 dst.checkers <- src.checkers;
6517 dst.aalevel <- src.aalevel;
6518 dst.trimmargins <- src.trimmargins;
6519 dst.trimfuzz <- src.trimfuzz;
6520 dst.urilauncher <- src.urilauncher;
6521 dst.colorspace <- src.colorspace;
6522 dst.invert <- src.invert;
6523 dst.colorscale <- src.colorscale;
6524 dst.redirectstderr <- src.redirectstderr;
6525 dst.ghyllscroll <- src.ghyllscroll;
6526 dst.columns <- src.columns;
6527 dst.beyecolumns <- src.beyecolumns;
6528 dst.selcmd <- src.selcmd;
6529 dst.updatecurs <- src.updatecurs;
6530 dst.pathlauncher <- src.pathlauncher;
6531 dst.keyhashes <- copykeyhashes src;
6532 dst.hfsize <- src.hfsize;
6533 dst.hscrollstep <- src.hscrollstep;
6534 dst.pgscale <- src.pgscale;
6535 dst.usepbo <- src.usepbo;
6536 dst.wheelbypage <- src.wheelbypage;
6537 dst.stcmd <- src.stcmd;
6538 dst.scrollb <- src.scrollb;
6541 let get s =
6542 let h = Hashtbl.create 10 in
6543 let dc = { defconf with angle = defconf.angle } in
6544 let rec toplevel v t spos _ =
6545 match t with
6546 | Vdata | Vcdata | Vend -> v
6547 | Vopen ("llppconfig", _, closed) ->
6548 if closed
6549 then v
6550 else { v with f = llppconfig }
6551 | Vopen _ ->
6552 error "unexpected subelement at top level" s spos
6553 | Vclose _ -> error "unexpected close at top level" s spos
6555 and llppconfig v t spos _ =
6556 match t with
6557 | Vdata | Vcdata -> v
6558 | Vend -> error "unexpected end of input in llppconfig" s spos
6559 | Vopen ("defaults", attrs, closed) ->
6560 let c = config_of dc attrs in
6561 setconf dc c;
6562 if closed
6563 then v
6564 else { v with f = defaults }
6566 | Vopen ("ui-font", attrs, closed) ->
6567 let rec getsize size = function
6568 | [] -> size
6569 | ("size", v) :: rest ->
6570 let size =
6571 fromstring int_of_string spos "size" v fstate.fontsize in
6572 getsize size rest
6573 | l -> getsize size l
6575 fstate.fontsize <- getsize fstate.fontsize attrs;
6576 if closed
6577 then v
6578 else { v with f = uifont (Buffer.create 10) }
6580 | Vopen ("doc", attrs, closed) ->
6581 let pathent, spage, srely, span, svisy = doc_of attrs in
6582 let path = unent pathent
6583 and pageno = fromstring int_of_string spos "page" spage 0
6584 and rely = fromstring float_of_string spos "rely" srely 0.0
6585 and pan = fromstring int_of_string spos "pan" span 0
6586 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6587 let c = config_of dc attrs in
6588 let anchor = (pageno, rely, visy) in
6589 if closed
6590 then (Hashtbl.add h path (c, [], pan, anchor); v)
6591 else { v with f = doc path pan anchor c [] }
6593 | Vopen _ ->
6594 error "unexpected subelement in llppconfig" s spos
6596 | Vclose "llppconfig" -> { v with f = toplevel }
6597 | Vclose _ -> error "unexpected close in llppconfig" s spos
6599 and defaults v t spos _ =
6600 match t with
6601 | Vdata | Vcdata -> v
6602 | Vend -> error "unexpected end of input in defaults" s spos
6603 | Vopen ("keymap", attrs, closed) ->
6604 let modename =
6605 try List.assoc "mode" attrs
6606 with Not_found -> "global" in
6607 if closed
6608 then v
6609 else
6610 let ret keymap =
6611 let h = findkeyhash dc modename in
6612 KeyMap.iter (Hashtbl.replace h) keymap;
6613 defaults
6615 { v with f = pkeymap ret KeyMap.empty }
6617 | Vopen (_, _, _) ->
6618 error "unexpected subelement in defaults" s spos
6620 | Vclose "defaults" ->
6621 { v with f = llppconfig }
6623 | Vclose _ -> error "unexpected close in defaults" s spos
6625 and uifont b v t spos epos =
6626 match t with
6627 | Vdata | Vcdata ->
6628 Buffer.add_substring b s spos (epos - spos);
6630 | Vopen (_, _, _) ->
6631 error "unexpected subelement in ui-font" s spos
6632 | Vclose "ui-font" ->
6633 if String.length !fontpath = 0
6634 then fontpath := Buffer.contents b;
6635 { v with f = llppconfig }
6636 | Vclose _ -> error "unexpected close in ui-font" s spos
6637 | Vend -> error "unexpected end of input in ui-font" s spos
6639 and doc path pan anchor c bookmarks v t spos _ =
6640 match t with
6641 | Vdata | Vcdata -> v
6642 | Vend -> error "unexpected end of input in doc" s spos
6643 | Vopen ("bookmarks", _, closed) ->
6644 if closed
6645 then v
6646 else { v with f = pbookmarks path pan anchor c bookmarks }
6648 | Vopen ("keymap", attrs, closed) ->
6649 let modename =
6650 try List.assoc "mode" attrs
6651 with Not_found -> "global"
6653 if closed
6654 then v
6655 else
6656 let ret keymap =
6657 let h = findkeyhash c modename in
6658 KeyMap.iter (Hashtbl.replace h) keymap;
6659 doc path pan anchor c bookmarks
6661 { v with f = pkeymap ret KeyMap.empty }
6663 | Vopen (_, _, _) ->
6664 error "unexpected subelement in doc" s spos
6666 | Vclose "doc" ->
6667 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6668 { v with f = llppconfig }
6670 | Vclose _ -> error "unexpected close in doc" s spos
6672 and pkeymap ret keymap v t spos _ =
6673 match t with
6674 | Vdata | Vcdata -> v
6675 | Vend -> error "unexpected end of input in keymap" s spos
6676 | Vopen ("map", attrs, closed) ->
6677 let r, l = map_of attrs in
6678 let kss = fromstring keys_of_string spos "in" r [] in
6679 let lss = fromstring keys_of_string spos "out" l [] in
6680 let keymap =
6681 match kss with
6682 | [] -> keymap
6683 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6684 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6686 if closed
6687 then { v with f = pkeymap ret keymap }
6688 else
6689 let f () = v in
6690 { v with f = skip "map" f }
6692 | Vopen _ ->
6693 error "unexpected subelement in keymap" s spos
6695 | Vclose "keymap" ->
6696 { v with f = ret keymap }
6698 | Vclose _ -> error "unexpected close in keymap" s spos
6700 and pbookmarks path pan anchor c bookmarks v t spos _ =
6701 match t with
6702 | Vdata | Vcdata -> v
6703 | Vend -> error "unexpected end of input in bookmarks" s spos
6704 | Vopen ("item", attrs, closed) ->
6705 let titleent, spage, srely, svisy = bookmark_of attrs in
6706 let page = fromstring int_of_string spos "page" spage 0
6707 and rely = fromstring float_of_string spos "rely" srely 0.0
6708 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6709 let bookmarks =
6710 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6712 if closed
6713 then { v with f = pbookmarks path pan anchor c bookmarks }
6714 else
6715 let f () = v in
6716 { v with f = skip "item" f }
6718 | Vopen _ ->
6719 error "unexpected subelement in bookmarks" s spos
6721 | Vclose "bookmarks" ->
6722 { v with f = doc path pan anchor c bookmarks }
6724 | Vclose _ -> error "unexpected close in bookmarks" s spos
6726 and skip tag f v t spos _ =
6727 match t with
6728 | Vdata | Vcdata -> v
6729 | Vend ->
6730 error ("unexpected end of input in skipped " ^ tag) s spos
6731 | Vopen (tag', _, closed) ->
6732 if closed
6733 then v
6734 else
6735 let f' () = { v with f = skip tag f } in
6736 { v with f = skip tag' f' }
6737 | Vclose ctag ->
6738 if tag = ctag
6739 then f ()
6740 else error ("unexpected close in skipped " ^ tag) s spos
6743 parse { f = toplevel; accu = () } s;
6744 h, dc;
6747 let do_load f ic =
6749 let len = in_channel_length ic in
6750 let s = String.create len in
6751 really_input ic s 0 len;
6752 f s;
6753 with
6754 | Parse_error (msg, s, pos) ->
6755 let subs = subs s pos in
6756 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6757 failwith ("parse error: " ^ s)
6759 | exn ->
6760 failwith ("config load error: " ^ exntos exn)
6763 let defconfpath =
6764 let dir =
6766 let dir = Filename.concat home ".config" in
6767 if Sys.is_directory dir then dir else home
6768 with _ -> home
6770 Filename.concat dir "llpp.conf"
6773 let confpath = ref defconfpath;;
6775 let load1 f =
6776 if Sys.file_exists !confpath
6777 then
6778 match
6779 (try Some (open_in_bin !confpath)
6780 with exn ->
6781 prerr_endline
6782 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6783 exntos exn);
6784 None
6786 with
6787 | Some ic ->
6788 let success =
6790 f (do_load get ic)
6791 with exn ->
6792 prerr_endline
6793 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6794 exntos exn);
6795 false
6797 close_in ic;
6798 success
6800 | None -> false
6801 else
6802 f (Hashtbl.create 0, defconf)
6805 let load () =
6806 let f (h, dc) =
6807 let pc, pb, px, pa =
6809 let key =
6810 if String.length state.origin = 0
6811 then state.path
6812 else state.origin
6814 Hashtbl.find h (Filename.basename key)
6815 with Not_found -> dc, [], 0, emptyanchor
6817 setconf defconf dc;
6818 setconf conf pc;
6819 state.bookmarks <- pb;
6820 state.x <- px;
6821 if conf.jumpback
6822 then state.anchor <- pa;
6823 cbput state.hists.nav pa;
6824 true
6826 load1 f
6829 let add_attrs bb always dc c =
6830 let ob s a b =
6831 if always || a != b
6832 then Printf.bprintf bb "\n %s='%b'" s a
6833 and oi s a b =
6834 if always || a != b
6835 then Printf.bprintf bb "\n %s='%d'" s a
6836 and oI s a b =
6837 if always || a != b
6838 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6839 and oz s a b =
6840 if always || a <> b
6841 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6842 and oF s a b =
6843 if always || a <> b
6844 then Printf.bprintf bb "\n %s='%f'" s a
6845 and oc s a b =
6846 if always || a <> b
6847 then
6848 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6849 and oC s a b =
6850 if always || a <> b
6851 then
6852 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6853 and oR s a b =
6854 if always || a <> b
6855 then
6856 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6857 and os s a b =
6858 if always || a <> b
6859 then
6860 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6861 and og s a b =
6862 if always || a <> b
6863 then
6864 match a with
6865 | None -> ()
6866 | Some (_N, _A, _B) ->
6867 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6868 and oW s a b =
6869 if always || a <> b
6870 then
6871 let v =
6872 match a with
6873 | None -> "false"
6874 | Some f ->
6875 if f = infinity
6876 then "true"
6877 else string_of_float f
6879 Printf.bprintf bb "\n %s='%s'" s v
6880 and oco s a b =
6881 if always || a <> b
6882 then
6883 match a with
6884 | Cmulti ((n, a, b), _) when n > 1 ->
6885 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6886 | Csplit (n, _) when n > 1 ->
6887 Printf.bprintf bb "\n %s='%d'" s ~-n
6888 | _ -> ()
6889 and obeco s a b =
6890 if always || a <> b
6891 then
6892 match a with
6893 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6894 | _ -> ()
6895 and oFm s a b =
6896 if always || a <> b
6897 then
6898 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6899 and oSv s a b m =
6900 if always || a <> b
6901 then
6902 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
6904 oi "width" c.cwinw dc.cwinw;
6905 oi "height" c.cwinh dc.cwinh;
6906 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6907 oi "scroll-handle-height" c.scrollh dc.scrollh;
6908 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
6909 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
6910 ob "case-insensitive-search" c.icase dc.icase;
6911 ob "preload" c.preload dc.preload;
6912 oi "page-bias" c.pagebias dc.pagebias;
6913 oi "scroll-step" c.scrollstep dc.scrollstep;
6914 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6915 ob "max-height-fit" c.maxhfit dc.maxhfit;
6916 ob "crop-hack" c.crophack dc.crophack;
6917 oW "throttle" c.maxwait dc.maxwait;
6918 ob "highlight-links" c.hlinks dc.hlinks;
6919 ob "under-cursor-info" c.underinfo dc.underinfo;
6920 oi "vertical-margin" c.interpagespace dc.interpagespace;
6921 oz "zoom" c.zoom dc.zoom;
6922 ob "presentation" c.presentation dc.presentation;
6923 oi "rotation-angle" c.angle dc.angle;
6924 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6925 oFm "fit-model" c.fitmodel dc.fitmodel;
6926 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6927 oi "tex-count" c.texcount dc.texcount;
6928 oi "slice-height" c.sliceheight dc.sliceheight;
6929 oi "thumbnail-width" c.thumbw dc.thumbw;
6930 ob "persistent-location" c.jumpback dc.jumpback;
6931 oc "background-color" c.bgcolor dc.bgcolor;
6932 oi "tile-width" c.tilew dc.tilew;
6933 oi "tile-height" c.tileh dc.tileh;
6934 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6935 ob "checkers" c.checkers dc.checkers;
6936 oi "aalevel" c.aalevel dc.aalevel;
6937 ob "trim-margins" c.trimmargins dc.trimmargins;
6938 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6939 os "uri-launcher" c.urilauncher dc.urilauncher;
6940 os "path-launcher" c.pathlauncher dc.pathlauncher;
6941 oC "color-space" c.colorspace dc.colorspace;
6942 ob "invert-colors" c.invert dc.invert;
6943 oF "brightness" c.colorscale dc.colorscale;
6944 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6945 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6946 oco "columns" c.columns dc.columns;
6947 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6948 os "selection-command" c.selcmd dc.selcmd;
6949 os "synctex-command" c.stcmd dc.stcmd;
6950 ob "update-cursor" c.updatecurs dc.updatecurs;
6951 oi "hint-font-size" c.hfsize dc.hfsize;
6952 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6953 oF "page-scroll-scale" c.pgscale dc.pgscale;
6954 ob "use-pbo" c.usepbo dc.usepbo;
6955 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6958 let keymapsbuf always dc c =
6959 let bb = Buffer.create 16 in
6960 let rec loop = function
6961 | [] -> ()
6962 | (modename, h) :: rest ->
6963 let dh = findkeyhash dc modename in
6964 if always || h <> dh
6965 then (
6966 if Hashtbl.length h > 0
6967 then (
6968 if Buffer.length bb > 0
6969 then Buffer.add_char bb '\n';
6970 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6971 Hashtbl.iter (fun i o ->
6972 let isdifferent = always ||
6974 let dO = Hashtbl.find dh i in
6975 dO <> o
6976 with Not_found -> true
6978 if isdifferent
6979 then
6980 let addkm (k, m) =
6981 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6982 if Wsi.withalt m then Buffer.add_string bb "alt-";
6983 if Wsi.withshift m then Buffer.add_string bb "shift-";
6984 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6985 Buffer.add_string bb (Wsi.keyname k);
6987 let addkms l =
6988 let rec loop = function
6989 | [] -> ()
6990 | km :: [] -> addkm km
6991 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6993 loop l
6995 Buffer.add_string bb "<map in='";
6996 addkm i;
6997 match o with
6998 | KMinsrt km ->
6999 Buffer.add_string bb "' out='";
7000 addkm km;
7001 Buffer.add_string bb "'/>\n"
7003 | KMinsrl kms ->
7004 Buffer.add_string bb "' out='";
7005 addkms kms;
7006 Buffer.add_string bb "'/>\n"
7008 | KMmulti (ins, kms) ->
7009 Buffer.add_char bb ' ';
7010 addkms ins;
7011 Buffer.add_string bb "' out='";
7012 addkms kms;
7013 Buffer.add_string bb "'/>\n"
7014 ) h;
7015 Buffer.add_string bb "</keymap>";
7018 loop rest
7020 loop c.keyhashes;
7024 let save () =
7025 let uifontsize = fstate.fontsize in
7026 let bb = Buffer.create 32768 in
7027 let relx = float state.x /. float state.winw in
7028 let w, h, x =
7029 let cx w = truncate (relx *. float w) in
7030 List.fold_left
7031 (fun (w, h, x) ws ->
7032 match ws with
7033 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7034 | Wsi.MaxVert -> (w, conf.cwinh, x)
7035 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7037 (state.winw, state.winh, state.x) state.winstate
7039 conf.cwinw <- w;
7040 conf.cwinh <- h;
7041 let f (h, dc) =
7042 let dc = if conf.bedefault then conf else dc in
7043 Buffer.add_string bb "<llppconfig>\n";
7045 if String.length !fontpath > 0
7046 then
7047 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7048 uifontsize
7049 !fontpath
7050 else (
7051 if uifontsize <> 14
7052 then
7053 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7056 Buffer.add_string bb "<defaults ";
7057 add_attrs bb true dc dc;
7058 let kb = keymapsbuf true dc dc in
7059 if Buffer.length kb > 0
7060 then (
7061 Buffer.add_string bb ">\n";
7062 Buffer.add_buffer bb kb;
7063 Buffer.add_string bb "\n</defaults>\n";
7065 else Buffer.add_string bb "/>\n";
7067 let adddoc path pan anchor c bookmarks =
7068 if bookmarks == [] && c = dc && anchor = emptyanchor
7069 then ()
7070 else (
7071 Printf.bprintf bb "<doc path='%s'"
7072 (enent path 0 (String.length path));
7074 if anchor <> emptyanchor
7075 then (
7076 let n, rely, visy = anchor in
7077 Printf.bprintf bb " page='%d'" n;
7078 if rely > 1e-6
7079 then
7080 Printf.bprintf bb " rely='%f'" rely
7082 if abs_float visy > 1e-6
7083 then
7084 Printf.bprintf bb " visy='%f'" visy
7088 if pan != 0
7089 then Printf.bprintf bb " pan='%d'" pan;
7091 add_attrs bb false dc c;
7092 let kb = keymapsbuf false dc c in
7094 begin match bookmarks with
7095 | [] ->
7096 if Buffer.length kb > 0
7097 then (
7098 Buffer.add_string bb ">\n";
7099 Buffer.add_buffer bb kb;
7100 Buffer.add_string bb "\n</doc>\n";
7102 else Buffer.add_string bb "/>\n"
7103 | _ ->
7104 Buffer.add_string bb ">\n<bookmarks>\n";
7105 List.iter (fun (title, _level, (page, rely, visy)) ->
7106 Printf.bprintf bb
7107 "<item title='%s' page='%d'"
7108 (enent title 0 (String.length title))
7109 page
7111 if rely > 1e-6
7112 then
7113 Printf.bprintf bb " rely='%f'" rely
7115 if abs_float visy > 1e-6
7116 then
7117 Printf.bprintf bb " visy='%f'" visy
7119 Buffer.add_string bb "/>\n";
7120 ) bookmarks;
7121 Buffer.add_string bb "</bookmarks>";
7122 if Buffer.length kb > 0
7123 then (
7124 Buffer.add_string bb "\n";
7125 Buffer.add_buffer bb kb;
7127 Buffer.add_string bb "\n</doc>\n";
7128 end;
7132 let pan, conf =
7133 match state.mode with
7134 | Birdseye (c, pan, _, _, _) ->
7135 let beyecolumns =
7136 match conf.columns with
7137 | Cmulti ((c, _, _), _) -> Some c
7138 | Csingle _ -> None
7139 | Csplit _ -> None
7140 and columns =
7141 match c.columns with
7142 | Cmulti (c, _) -> Cmulti (c, [||])
7143 | Csingle _ -> Csingle [||]
7144 | Csplit _ -> failwith "quit from bird's eye while split"
7146 pan, { c with beyecolumns = beyecolumns; columns = columns }
7147 | _ -> x, conf
7149 let basename = Filename.basename
7150 (if String.length state.origin = 0 then state.path else state.origin)
7152 adddoc basename pan (getanchor ())
7153 (let conf =
7154 let autoscrollstep =
7155 match state.autoscroll with
7156 | Some step -> step
7157 | None -> conf.autoscrollstep
7159 match state.mode with
7160 | Birdseye (bc, _, _, _, _) ->
7161 { conf with
7162 zoom = bc.zoom;
7163 presentation = bc.presentation;
7164 interpagespace = bc.interpagespace;
7165 maxwait = bc.maxwait;
7166 autoscrollstep = autoscrollstep }
7167 | _ -> { conf with autoscrollstep = autoscrollstep }
7168 in conf)
7169 (if conf.savebmarks then state.bookmarks else []);
7171 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7172 if basename <> path
7173 then adddoc path x anchor c bookmarks
7174 ) h;
7175 Buffer.add_string bb "</llppconfig>\n";
7176 true;
7178 if load1 f && Buffer.length bb > 0
7179 then
7181 let tmp = !confpath ^ ".tmp" in
7182 let oc = open_out_bin tmp in
7183 Buffer.output_buffer oc bb;
7184 close_out oc;
7185 Unix.rename tmp !confpath;
7186 with exn ->
7187 prerr_endline
7188 ("error while saving configuration: " ^ exntos exn)
7190 end;;
7192 let adderrmsg src msg =
7193 Buffer.add_string state.errmsgs msg;
7194 state.newerrmsgs <- true;
7195 G.postRedisplay src
7198 let adderrfmt src fmt =
7199 Format.kprintf (fun s -> adderrmsg src s) fmt;
7202 let ract cmds =
7203 let cl = splitatspace cmds in
7204 let scan s fmt f =
7205 try Scanf.sscanf s fmt f
7206 with exn ->
7207 adderrfmt "remote exec"
7208 "error processing '%S': %s\n" cmds (exntos exn)
7210 match cl with
7211 | "reload" :: [] -> reload ()
7212 | "goto" :: args :: [] ->
7213 scan args "%u %f %f"
7214 (fun pageno x y ->
7215 let cmd, _ = state.geomcmds in
7216 if String.length cmd = 0
7217 then gotopagexy pageno x y
7218 else
7219 let f prevf () =
7220 gotopagexy pageno x y;
7221 prevf ()
7223 state.reprf <- f state.reprf
7225 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7226 | "rect" :: args :: [] ->
7227 scan args "%u %u %f %f %f %f"
7228 (fun pageno color x0 y0 x1 y1 ->
7229 onpagerect pageno (fun w h ->
7230 let _,w1,h1,_ = getpagedim pageno in
7231 let sw = float w1 /. w
7232 and sh = float h1 /. h in
7233 let x0s = x0 *. sw
7234 and x1s = x1 *. sw
7235 and y0s = y0 *. sh
7236 and y1s = y1 *. sh in
7237 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7238 debugrect rect;
7239 state.rects <- (pageno, color, rect) :: state.rects;
7240 G.postRedisplay "rect";
7243 | "activatewin" :: [] -> Wsi.activatewin ()
7244 | "quit" :: [] -> raise Quit
7245 | _ ->
7246 adderrfmt "remote command"
7247 "error processing remote command: %S\n" cmds;
7250 let remote =
7251 let scratch = String.create 80 in
7252 let buf = Buffer.create 80 in
7253 fun fd ->
7254 let rec tempfr () =
7255 try Some (Unix.read fd scratch 0 80)
7256 with
7257 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7258 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7259 | exn -> raise exn
7261 match tempfr () with
7262 | None -> Some fd
7263 | Some n ->
7264 if n = 0
7265 then (
7266 Unix.close fd;
7267 if Buffer.length buf > 0
7268 then (
7269 let s = Buffer.contents buf in
7270 Buffer.clear buf;
7271 ract s;
7273 None
7275 else
7276 let rec eat ppos =
7277 let nlpos =
7279 let pos = String.index_from scratch ppos '\n' in
7280 if pos >= n then -1 else pos
7281 with Not_found -> -1
7283 if nlpos >= 0
7284 then (
7285 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7286 let s = Buffer.contents buf in
7287 Buffer.clear buf;
7288 ract s;
7289 eat (nlpos+1);
7291 else (
7292 Buffer.add_substring buf scratch ppos (n-ppos);
7293 Some fd
7295 in eat 0
7298 let remoteopen path =
7299 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7300 with exn ->
7301 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7302 None
7305 let () =
7306 let trimcachepath = ref "" in
7307 let rcmdpath = ref "" in
7308 Arg.parse
7309 (Arg.align
7310 [("-p", Arg.String (fun s -> state.password <- s),
7311 "<password> Set password");
7313 ("-f", Arg.String (fun s -> Config.fontpath := s),
7314 "<path> Set path to the user interface font");
7316 ("-c", Arg.String (fun s -> Config.confpath := s),
7317 "<path> Set path to the configuration file");
7319 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7320 "<path> Set path to the trim cache file");
7322 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7323 "<named-destination> Set named destination");
7325 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7327 ("-remote", Arg.String (fun s -> rcmdpath := s),
7328 "<path> Set path to the remote commands source");
7330 ("-origin", Arg.String (fun s -> state.origin <- s),
7331 "<original-path> Set original path");
7333 ("-v", Arg.Unit (fun () ->
7334 Printf.printf
7335 "%s\nconfiguration path: %s\n"
7336 (version ())
7337 Config.defconfpath
7339 exit 0), " Print version and exit");
7342 (fun s -> state.path <- s)
7343 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7345 if String.length state.path = 0
7346 then (prerr_endline "file name missing"; exit 1);
7348 if not (Config.load ())
7349 then prerr_endline "failed to load configuration";
7351 let wsfd, winw, winh = Wsi.init (object (self)
7352 val mutable m_hack = false
7353 method expose =
7354 if not m_hack
7355 then G.postRedisplay "expose"
7356 method visible = self#expose
7357 method display = m_hack <- false; display ()
7358 method reshape w h = m_hack <- true; reshape w h
7359 method mouse b d x y m = mouse b d x y m
7360 method motion x y = state.mpos <- (x, y); motion x y
7361 method pmotion x y = state.mpos <- (x, y); pmotion x y
7362 method key k m =
7363 let mascm = m land (
7364 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7365 ) in
7366 match state.keystate with
7367 | KSnone ->
7368 let km = k, mascm in
7369 begin
7370 match
7371 let modehash = state.uioh#modehash in
7372 try Hashtbl.find modehash km
7373 with Not_found ->
7374 try Hashtbl.find (findkeyhash conf "global") km
7375 with Not_found -> KMinsrt (k, m)
7376 with
7377 | KMinsrt (k, m) -> keyboard k m
7378 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7379 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7381 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7382 List.iter (fun (k, m) -> keyboard k m) insrt;
7383 state.keystate <- KSnone
7384 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7385 state.keystate <- KSinto (keys, insrt)
7386 | _ ->
7387 state.keystate <- KSnone
7389 method enter x y = state.mpos <- (x, y); pmotion x y
7390 method leave = state.mpos <- (-1, -1)
7391 method winstate wsl = state.winstate <- wsl
7392 method quit = raise Quit
7393 end) conf.cwinw conf.cwinh (platform = Posx) in
7395 state.wsfd <- wsfd;
7397 if not (
7398 List.exists GlMisc.check_extension
7399 [ "GL_ARB_texture_rectangle"
7400 ; "GL_EXT_texture_recangle"
7401 ; "GL_NV_texture_rectangle" ]
7403 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7405 if (
7406 let r = GlMisc.get_string `renderer in
7407 let p = "Mesa DRI Intel(" in
7408 let l = String.length p in
7409 String.length r > l && String.sub r 0 l = p
7411 then defconf.sliceheight <- 1024;
7413 let cr, sw =
7414 match Ne.pipe () with
7415 | Ne.Exn exn ->
7416 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7417 exit 1
7418 | Ne.Res rw -> rw
7419 and sr, cw =
7420 match Ne.pipe () with
7421 | Ne.Exn exn ->
7422 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7423 exit 1
7424 | Ne.Res rw -> rw
7427 cloexec cr;
7428 cloexec sw;
7429 cloexec sr;
7430 cloexec cw;
7432 setcheckers conf.checkers;
7433 redirectstderr ();
7435 init (cr, cw) (
7436 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7437 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7438 !Config.fontpath, !trimcachepath,
7439 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7441 state.sr <- sr;
7442 state.sw <- sw;
7443 state.text <- "Opening " ^ (mbtoutf8 state.path);
7444 reshape winw winh;
7445 opendoc state.path state.password;
7446 state.uioh <- uioh;
7447 display ();
7448 Wsi.mapwin ();
7449 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7450 let optrfd =
7451 ref (
7452 if String.length !rcmdpath > 0
7453 then remoteopen !rcmdpath
7454 else None
7458 let rec loop deadline =
7459 let r =
7460 match state.errfd with
7461 | None -> [state.sr; state.wsfd]
7462 | Some fd -> [state.sr; state.wsfd; fd]
7464 let r =
7465 match !optrfd with
7466 | None -> r
7467 | Some fd -> fd :: r
7469 if state.redisplay
7470 then (
7471 state.redisplay <- false;
7472 display ();
7474 let timeout =
7475 let now = now () in
7476 if deadline > now
7477 then (
7478 if deadline = infinity
7479 then ~-.1.0
7480 else max 0.0 (deadline -. now)
7482 else 0.0
7484 let r, _, _ =
7485 try Unix.select r [] [] timeout
7486 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7488 begin match r with
7489 | [] ->
7490 state.ghyll None;
7491 let newdeadline =
7492 if state.ghyll == noghyll
7493 then
7494 match state.autoscroll with
7495 | Some step when step != 0 ->
7496 let y = state.y + step in
7497 let y =
7498 if y < 0
7499 then state.maxy
7500 else if y >= state.maxy then 0 else y
7502 gotoy y;
7503 if state.mode = View
7504 then state.text <- "";
7505 deadline +. 0.01
7506 | _ -> infinity
7507 else deadline +. 0.01
7509 loop newdeadline
7511 | l ->
7512 let rec checkfds = function
7513 | [] -> ()
7514 | fd :: rest when fd = state.sr ->
7515 let cmd = readcmd state.sr in
7516 act cmd;
7517 checkfds rest
7519 | fd :: rest when fd = state.wsfd ->
7520 Wsi.readresp fd;
7521 checkfds rest
7523 | fd :: rest when Some fd = !optrfd ->
7524 begin match remote fd with
7525 | None -> optrfd := remoteopen !rcmdpath;
7526 | opt -> optrfd := opt
7527 end;
7528 checkfds rest
7530 | fd :: rest ->
7531 let s = String.create 80 in
7532 let n = tempfailureretry (Unix.read fd s 0) 80 in
7533 if conf.redirectstderr
7534 then (
7535 Buffer.add_substring state.errmsgs s 0 n;
7536 state.newerrmsgs <- true;
7537 state.redisplay <- true;
7539 else (
7540 prerr_string (String.sub s 0 n);
7541 flush stderr;
7543 checkfds rest
7545 checkfds l;
7546 let newdeadline =
7547 let deadline1 =
7548 if deadline = infinity
7549 then now () +. 0.01
7550 else deadline
7552 match state.autoscroll with
7553 | Some step when step != 0 -> deadline1
7554 | _ -> if state.ghyll == noghyll then infinity else deadline1
7556 loop newdeadline
7557 end;
7560 loop infinity;
7561 with Quit ->
7562 Config.save ();