Trading places
[llpp.git] / main.ml
blobd316492e99d77d9d134c927cab6b20733a992647
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * fitmodel * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and trimmargins = bool
29 and interpagespace = int
30 and texcount = int
31 and sliceheight = int
32 and gen = int
33 and top = float
34 and dtop = float
35 and fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
42 and fitmodel = | FitWidth | FitProportional | FitPage
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type conf =
294 { mutable scrollbw : int
295 ; mutable scrollh : int
296 ; mutable icase : bool
297 ; mutable preload : bool
298 ; mutable pagebias : int
299 ; mutable verbose : bool
300 ; mutable debug : bool
301 ; mutable scrollstep : int
302 ; mutable hscrollstep : int
303 ; mutable maxhfit : bool
304 ; mutable crophack : bool
305 ; mutable autoscrollstep : int
306 ; mutable maxwait : float option
307 ; mutable hlinks : bool
308 ; mutable underinfo : bool
309 ; mutable interpagespace : interpagespace
310 ; mutable zoom : float
311 ; mutable presentation : bool
312 ; mutable angle : angle
313 ; mutable cwinw : int
314 ; mutable cwinh : int
315 ; mutable savebmarks : bool
316 ; mutable fitmodel : fitmodel
317 ; mutable trimmargins : trimmargins
318 ; mutable trimfuzz : irect
319 ; mutable memlimit : memsize
320 ; mutable texcount : texcount
321 ; mutable sliceheight : sliceheight
322 ; mutable thumbw : width
323 ; mutable jumpback : bool
324 ; mutable bgcolor : (float * float * float)
325 ; mutable bedefault : bool
326 ; mutable scrollbarinpm : bool
327 ; mutable tilew : int
328 ; mutable tileh : int
329 ; mutable mustoresize : memsize
330 ; mutable checkers : bool
331 ; mutable aalevel : int
332 ; mutable urilauncher : string
333 ; mutable pathlauncher : string
334 ; mutable colorspace : colorspace
335 ; mutable invert : bool
336 ; mutable colorscale : float
337 ; mutable redirectstderr : bool
338 ; mutable ghyllscroll : (int * int * int) option
339 ; mutable columns : columns
340 ; mutable beyecolumns : columncount option
341 ; mutable selcmd : string
342 ; mutable updatecurs : bool
343 ; mutable keyhashes : (string * keyhash) list
344 ; mutable hfsize : int
345 ; mutable pgscale : float
346 ; mutable usepbo : bool
347 ; mutable wheelbypage : bool
348 ; mutable stcmd : string
350 and columns =
351 | Csingle of singlecolumn
352 | Cmulti of multicolumns
353 | Csplit of splitcolumns
356 type anchor = pageno * top * dtop;;
358 type outline = string * int * anchor;;
360 type rect = float * float * float * float * float * float * float * float;;
362 type tile = opaque * pixmapsize * elapsed
363 and elapsed = float;;
364 type pagemapkey = pageno * gen;;
365 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
366 and row = int
367 and col = int;;
369 let emptyanchor = (0, 0.0, 0.0);;
371 type infochange = | Memused | Docinfo | Pdim;;
373 class type uioh = object
374 method display : unit
375 method key : int -> int -> uioh
376 method button : int -> bool -> int -> int -> int -> uioh
377 method motion : int -> int -> uioh
378 method pmotion : int -> int -> uioh
379 method infochanged : infochange -> unit
380 method scrollpw : (int * float * float)
381 method scrollph : (int * float * float)
382 method modehash : keyhash
383 method eformsgs : bool
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 method eformsgs = false
426 end;;
428 type state =
429 { mutable sr : Unix.file_descr
430 ; mutable sw : Unix.file_descr
431 ; mutable wsfd : Unix.file_descr
432 ; mutable errfd : Unix.file_descr option
433 ; mutable stderr : Unix.file_descr
434 ; mutable errmsgs : Buffer.t
435 ; mutable newerrmsgs : bool
436 ; mutable w : int
437 ; mutable x : int
438 ; mutable y : int
439 ; mutable scrollw : int
440 ; mutable hscrollh : int
441 ; mutable anchor : anchor
442 ; mutable ranchors : (string * string * anchor * string) list
443 ; mutable maxy : int
444 ; mutable layout : page list
445 ; pagemap : (pagemapkey, opaque) Hashtbl.t
446 ; tilemap : (tilemapkey, tile) Hashtbl.t
447 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
448 ; mutable pdims : (pageno * width * height * leftx) list
449 ; mutable pagecount : int
450 ; mutable currently : currently
451 ; mutable mstate : mstate
452 ; mutable searchpattern : string
453 ; mutable rects : (pageno * recttype * rect) list
454 ; mutable rects1 : (pageno * recttype * rect) list
455 ; mutable text : string
456 ; mutable winstate : Wsi.winstate list
457 ; mutable mode : mode
458 ; mutable uioh : uioh
459 ; mutable outlines : outline array
460 ; mutable bookmarks : outline list
461 ; mutable path : string
462 ; mutable password : string
463 ; mutable nameddest : string
464 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
465 ; mutable memused : memsize
466 ; mutable gen : gen
467 ; mutable throttle : (page list * int * float) option
468 ; mutable autoscroll : int option
469 ; mutable ghyll : (int option -> unit)
470 ; mutable help : helpitem array
471 ; mutable docinfo : (int * string) list
472 ; mutable texid : GlTex.texture_id option
473 ; hists : hists
474 ; mutable prevzoom : float
475 ; mutable progress : float
476 ; mutable redisplay : bool
477 ; mutable mpos : mpos
478 ; mutable keystate : keystate
479 ; mutable glinks : bool
480 ; mutable prevcolumns : (columns * float) option
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
484 ; mutable origin : string
486 and hists =
487 { pat : string circbuf
488 ; pag : string circbuf
489 ; nav : anchor circbuf
490 ; sel : string circbuf
494 let defconf =
495 { scrollbw = 7
496 ; scrollh = 12
497 ; icase = true
498 ; preload = true
499 ; pagebias = 0
500 ; verbose = false
501 ; debug = false
502 ; scrollstep = 24
503 ; hscrollstep = 24
504 ; maxhfit = true
505 ; crophack = false
506 ; autoscrollstep = 2
507 ; maxwait = None
508 ; hlinks = false
509 ; underinfo = false
510 ; interpagespace = 2
511 ; zoom = 1.0
512 ; presentation = false
513 ; angle = 0
514 ; cwinw = 900
515 ; cwinh = 900
516 ; savebmarks = true
517 ; fitmodel = FitProportional
518 ; trimmargins = false
519 ; trimfuzz = (0,0,0,0)
520 ; memlimit = 32 lsl 20
521 ; texcount = 256
522 ; sliceheight = 24
523 ; thumbw = 76
524 ; jumpback = true
525 ; bgcolor = (0.5, 0.5, 0.5)
526 ; bedefault = false
527 ; scrollbarinpm = true
528 ; tilew = 2048
529 ; tileh = 2048
530 ; mustoresize = 256 lsl 20
531 ; checkers = true
532 ; aalevel = 8
533 ; urilauncher =
534 (match platform with
535 | Plinux | Pfreebsd | Pdragonflybsd
536 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
537 | Posx -> "open \"%s\""
538 | Pcygwin -> "cygstart \"%s\""
539 | Punknown -> "echo %s")
540 ; pathlauncher = "lp \"%s\""
541 ; selcmd =
542 (match platform with
543 | Plinux | Pfreebsd | Pdragonflybsd
544 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
545 | Posx -> "pbcopy"
546 | Pcygwin -> "wsel"
547 | Punknown -> "cat")
548 ; colorspace = Rgb
549 ; invert = false
550 ; colorscale = 1.0
551 ; redirectstderr = false
552 ; ghyllscroll = None
553 ; columns = Csingle [||]
554 ; beyecolumns = None
555 ; updatecurs = false
556 ; hfsize = 12
557 ; pgscale = 1.0
558 ; usepbo = false
559 ; wheelbypage = false
560 ; stcmd = "echo SyncTex"
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let wtmode = ref false;;
578 let findkeyhash c name =
579 try List.assoc name c.keyhashes
580 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
583 let conf = { defconf with angle = defconf.angle };;
585 let pgscale h = truncate (float h *. conf.pgscale);;
587 type fontstate =
588 { mutable fontsize : int
589 ; mutable wwidth : float
590 ; mutable maxrows : int
594 let fstate =
595 { fontsize = 14
596 ; wwidth = nan
597 ; maxrows = -1
601 let geturl s =
602 let colonpos = try String.index s ':' with Not_found -> -1 in
603 let len = String.length s in
604 if colonpos >= 0 && colonpos + 3 < len
605 then (
606 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
607 then
608 let schemestartpos =
609 try String.rindex_from s colonpos ' '
610 with Not_found -> -1
612 let scheme =
613 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
615 match scheme with
616 | "http" | "ftp" | "mailto" ->
617 let epos =
618 try String.index_from s colonpos ' '
619 with Not_found -> len
621 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
622 | _ -> ""
623 else ""
625 else ""
628 let gotouri uri =
629 if String.length conf.urilauncher = 0
630 then print_endline uri
631 else (
632 let url = geturl uri in
633 if String.length url = 0
634 then print_endline uri
635 else
636 let re = Str.regexp "%s" in
637 let command = Str.global_replace re url conf.urilauncher in
638 try popen command []
639 with exn ->
640 Printf.eprintf
641 "failed to execute `%s': %s\n" command (exntos exn);
642 flush stderr;
646 let version () =
647 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
648 (platform_to_string platform) Sys.word_size Sys.ocaml_version
651 let makehelp () =
652 let strings = version () :: "" :: Help.keys in
653 Array.of_list (
654 List.map (fun s ->
655 let url = geturl s in
656 if String.length url > 0
657 then (s, 0, Action (fun u -> gotouri url; u))
658 else (s, 0, Noaction)
659 ) strings);
662 let noghyll _ = ();;
663 let firstgeomcmds = "", [];;
664 let noreprf () = ();;
666 let state =
667 { sr = Unix.stdin
668 ; sw = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; errfd = None
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; scrollw = 0
678 ; hscrollh = 0
679 ; anchor = emptyanchor
680 ; ranchors = []
681 ; layout = []
682 ; maxy = max_int
683 ; tilelru = Queue.create ()
684 ; pagemap = Hashtbl.create 10
685 ; tilemap = Hashtbl.create 10
686 ; pdims = []
687 ; pagecount = 0
688 ; currently = Idle
689 ; mstate = Mnone
690 ; rects = []
691 ; rects1 = []
692 ; text = ""
693 ; mode = View
694 ; winstate = []
695 ; searchpattern = ""
696 ; outlines = [||]
697 ; bookmarks = []
698 ; path = ""
699 ; password = ""
700 ; nameddest = ""
701 ; geomcmds = firstgeomcmds
702 ; hists =
703 { nav = cbnew 10 emptyanchor
704 ; pat = cbnew 10 ""
705 ; pag = cbnew 10 ""
706 ; sel = cbnew 10 ""
708 ; memused = 0
709 ; gen = 0
710 ; throttle = None
711 ; autoscroll = None
712 ; ghyll = noghyll
713 ; help = makehelp ()
714 ; docinfo = []
715 ; texid = None
716 ; prevzoom = 1.0
717 ; progress = -1.0
718 ; uioh = nouioh
719 ; redisplay = true
720 ; mpos = (-1, -1)
721 ; keystate = KSnone
722 ; glinks = false
723 ; prevcolumns = None
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
727 ; origin = ""
731 let setfontsize n =
732 fstate.fontsize <- n;
733 fstate.wwidth <- measurestr fstate.fontsize "w";
734 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
737 let vlog fmt =
738 if conf.verbose
739 then
740 Printf.kprintf prerr_endline fmt
741 else
742 Printf.kprintf ignore fmt
745 let launchpath () =
746 if String.length conf.pathlauncher = 0
747 then print_endline state.path
748 else (
749 let re = Str.regexp "%s" in
750 let command = Str.global_replace re state.path conf.pathlauncher in
751 try popen command []
752 with exn ->
753 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
754 flush stderr;
758 module Ne = struct
759 type 'a t = | Res of 'a | Exn of exn;;
761 let pipe () =
762 try Res (Unix.pipe ())
763 with exn -> Exn exn
766 let clo fd f =
767 try tempfailureretry Unix.close fd
768 with exn -> f (exntos exn)
771 let dup fd =
772 try Res (tempfailureretry Unix.dup fd)
773 with exn -> Exn exn
776 let dup2 fd1 fd2 =
777 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
778 with exn -> Exn exn
780 end;;
782 let redirectstderr () =
783 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
784 if conf.redirectstderr
785 then
786 match Ne.pipe () with
787 | Ne.Exn exn ->
788 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
790 | Ne.Res (r, w) ->
791 begin match Ne.dup Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup stderr: %s" (exntos exn);
794 Ne.clo r (clofail "pipe/r");
795 Ne.clo w (clofail "pipe/w");
797 | Ne.Res dupstderr ->
798 begin match Ne.dup2 w Unix.stderr with
799 | Ne.Exn exn ->
800 dolog "failed to dup2 to stderr: %s" (exntos exn);
801 Ne.clo dupstderr (clofail "stderr duplicate");
802 Ne.clo r (clofail "redir pipe/r");
803 Ne.clo w (clofail "redir pipe/w");
805 | Ne.Res () ->
806 state.stderr <- dupstderr;
807 state.errfd <- Some r;
808 end;
810 else (
811 state.newerrmsgs <- false;
812 begin match state.errfd with
813 | Some fd ->
814 begin match Ne.dup2 state.stderr Unix.stderr with
815 | Ne.Exn exn ->
816 dolog "failed to dup2 original stderr: %s" (exntos exn)
817 | Ne.Res () ->
818 Ne.clo fd (clofail "dup of stderr");
819 state.errfd <- None;
820 end;
821 | None -> ()
822 end;
823 prerr_string (Buffer.contents state.errmsgs);
824 flush stderr;
825 Buffer.clear state.errmsgs;
829 module G =
830 struct
831 let postRedisplay who =
832 if conf.verbose
833 then prerr_endline ("redisplay for " ^ who);
834 state.redisplay <- true;
836 end;;
838 let getopaque pageno =
839 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
840 with Not_found -> None
843 let putopaque pageno opaque =
844 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
847 let pagetranslatepoint l x y =
848 let dy = y - l.pagedispy in
849 let y = dy + l.pagey in
850 let dx = x - l.pagedispx in
851 let x = dx + l.pagex in
852 (x, y);
855 let onppundermouse g x y d =
856 let rec f = function
857 | l :: rest ->
858 begin match getopaque l.pageno with
859 | Some opaque ->
860 let x0 = l.pagedispx in
861 let x1 = x0 + l.pagevw in
862 let y0 = l.pagedispy in
863 let y1 = y0 + l.pagevh in
864 if y >= y0 && y <= y1 && x >= x0 && x <= x1
865 then
866 let px, py = pagetranslatepoint l x y in
867 match g opaque l px py with
868 | Some res -> res
869 | None -> f rest
870 else f rest
871 | _ ->
872 f rest
874 | [] -> d
876 f state.layout
879 let getunder x y =
880 let g opaque _ px py =
881 match whatsunder opaque px py with
882 | Unone -> None
883 | under -> Some under
885 onppundermouse g x y Unone
888 let unproject x y =
889 let g opaque l x y =
890 match unproject opaque x y with
891 | Some (x, y) -> Some (Some (l.pageno, x, y))
892 | None -> None
894 onppundermouse g x y None;
897 let showtext c s =
898 state.text <- Printf.sprintf "%c%s" c s;
899 G.postRedisplay "showtext";
902 let selstring s =
903 match Ne.pipe () with
904 | Ne.Exn exn ->
905 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
906 | Ne.Res (r, w) ->
907 let popened =
908 try popen conf.selcmd [r, 0; w, -1]; true
909 with exn ->
910 showtext '!'
911 (Printf.sprintf "failed to execute %s: %s"
912 conf.selcmd (exntos exn));
913 false
915 let clo cap fd =
916 Ne.clo fd (fun msg ->
917 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
920 if popened
921 then
922 (try
923 let l = String.length s in
924 let n = tempfailureretry (Unix.write w s 0) l in
925 if n != l
926 then
927 showtext '!'
928 (Printf.sprintf
929 "failed to write %d characters to sel pipe, wrote %d"
932 with exn ->
933 showtext '!'
934 (Printf.sprintf "failed to write to sel pipe: %s"
935 (exntos exn)
938 else dolog "%s" s;
939 clo "pipe/r" r;
940 clo "pipe/w" w;
943 let undertext = function
944 | Unone -> "none"
945 | Ulinkuri s -> s
946 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
947 | Utext s -> "font: " ^ s
948 | Uunexpected s -> "unexpected: " ^ s
949 | Ulaunch s -> "launch: " ^ s
950 | Unamed s -> "named: " ^ s
951 | Uremote (filename, pageno) ->
952 Printf.sprintf "%s: page %d" filename (pageno+1)
955 let updateunder x y =
956 match getunder x y with
957 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
958 | Ulinkuri uri ->
959 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
960 Wsi.setcursor Wsi.CURSOR_INFO
961 | Ulinkgoto (pageno, _) ->
962 if conf.underinfo
963 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
964 Wsi.setcursor Wsi.CURSOR_INFO
965 | Utext s ->
966 if conf.underinfo then showtext 'f' ("ont: " ^ s);
967 Wsi.setcursor Wsi.CURSOR_TEXT
968 | Uunexpected s ->
969 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
970 Wsi.setcursor Wsi.CURSOR_INHERIT
971 | Ulaunch s ->
972 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
973 Wsi.setcursor Wsi.CURSOR_INHERIT
974 | Unamed s ->
975 if conf.underinfo then showtext 'n' ("amed: " ^ s);
976 Wsi.setcursor Wsi.CURSOR_INHERIT
977 | Uremote (filename, pageno) ->
978 if conf.underinfo then showtext 'r'
979 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
980 Wsi.setcursor Wsi.CURSOR_INFO
983 let showlinktype under =
984 if conf.underinfo
985 then
986 match under with
987 | Unone -> ()
988 | under ->
989 let s = undertext under in
990 showtext ' ' s
993 let addchar s c =
994 let b = Buffer.create (String.length s + 1) in
995 Buffer.add_string b s;
996 Buffer.add_char b c;
997 Buffer.contents b;
1000 let colorspace_of_string s =
1001 match String.lowercase s with
1002 | "rgb" -> Rgb
1003 | "bgr" -> Bgr
1004 | "gray" -> Gray
1005 | _ -> failwith "invalid colorspace"
1008 let int_of_colorspace = function
1009 | Rgb -> 0
1010 | Bgr -> 1
1011 | Gray -> 2
1014 let colorspace_of_int = function
1015 | 0 -> Rgb
1016 | 1 -> Bgr
1017 | 2 -> Gray
1018 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1021 let colorspace_to_string = function
1022 | Rgb -> "rgb"
1023 | Bgr -> "bgr"
1024 | Gray -> "gray"
1027 let fitmodel_of_string s =
1028 match String.lowercase s with
1029 | "width" -> FitWidth
1030 | "proportional" -> FitProportional
1031 | "page" -> FitPage
1032 | _ -> failwith "invalid fit model"
1035 let int_of_fitmodel = function
1036 | FitWidth -> 0
1037 | FitProportional -> 1
1038 | FitPage -> 2
1041 let fitmodel_of_int = function
1042 | 0 -> FitWidth
1043 | 1 -> FitProportional
1044 | 2 -> FitPage
1045 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1048 let fitmodel_to_string = function
1049 | FitWidth -> "width"
1050 | FitProportional -> "proportional"
1051 | FitPage -> "page"
1054 let intentry_with_suffix text key =
1055 let c =
1056 if key >= 32 && key < 127
1057 then Char.chr key
1058 else '\000'
1060 match Char.lowercase c with
1061 | '0' .. '9' ->
1062 let text = addchar text c in
1063 TEcont text
1065 | 'k' | 'm' | 'g' ->
1066 let text = addchar text c in
1067 TEcont text
1069 | _ ->
1070 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1071 TEcont text
1074 let multicolumns_to_string (n, a, b) =
1075 if a = 0 && b = 0
1076 then Printf.sprintf "%d" n
1077 else Printf.sprintf "%d,%d,%d" n a b;
1080 let multicolumns_of_string s =
1082 (int_of_string s, 0, 0)
1083 with _ ->
1084 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1085 if a > 1 || b > 1
1086 then failwith "subtly broken"; (n, a, b)
1090 let readcmd fd =
1091 let s = "xxxx" in
1092 let n = tempfailureretry (Unix.read fd s 0) 4 in
1093 if n != 4 then failwith "incomplete read(len)";
1094 let len = 0
1095 lor (Char.code s.[0] lsl 24)
1096 lor (Char.code s.[1] lsl 16)
1097 lor (Char.code s.[2] lsl 8)
1098 lor (Char.code s.[3] lsl 0)
1100 let s = String.create len in
1101 let n = tempfailureretry (Unix.read fd s 0) len in
1102 if n != len then failwith "incomplete read(data)";
1106 let btod b = if b then 1 else 0;;
1108 let wcmd fmt =
1109 let b = Buffer.create 16 in
1110 Buffer.add_string b "llll";
1111 Printf.kbprintf
1112 (fun b ->
1113 let s = Buffer.contents b in
1114 let n = String.length s in
1115 let len = n - 4 in
1116 (* dolog "wcmd %S" (String.sub s 4 len); *)
1117 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1118 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1119 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1120 s.[3] <- Char.chr (len land 0xff);
1121 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1122 if n' != n then failwith "write failed";
1123 ) b fmt;
1126 let calcips h =
1127 let d = state.winh - h in
1128 max conf.interpagespace ((d + 1) / 2)
1131 let rowyh (c, coverA, coverB) b n =
1132 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1133 then
1134 let _, _, vy, (_, _, h, _) = b.(n) in
1135 (vy, h)
1136 else
1137 let n' = n - coverA in
1138 let d = n' mod c in
1139 let s = n - d in
1140 let e = min state.pagecount (s + c) in
1141 let rec find m miny maxh = if m = e then miny, maxh else
1142 let _, _, y, (_, _, h, _) = b.(m) in
1143 let miny = min miny y in
1144 let maxh = max maxh h in
1145 find (m+1) miny maxh
1146 in find s max_int 0
1149 let calcheight () =
1150 match conf.columns with
1151 | Cmulti ((_, _, _) as cl, b) ->
1152 if Array.length b > 0
1153 then
1154 let y, h = rowyh cl b (Array.length b - 1) in
1155 y + h + (if conf.presentation then calcips h else 0)
1156 else 0
1157 | Csingle b ->
1158 if Array.length b > 0
1159 then
1160 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1161 y + h + (if conf.presentation then calcips h else 0)
1162 else 0
1163 | Csplit (_, b) ->
1164 if Array.length b > 0
1165 then
1166 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1167 y + h
1168 else 0
1171 let getpageyh pageno =
1172 let pageno = bound pageno 0 (state.pagecount-1) in
1173 match conf.columns with
1174 | Csingle b ->
1175 if Array.length b = 0
1176 then 0, 0
1177 else
1178 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1179 let y =
1180 if conf.presentation
1181 then y - calcips h
1182 else y
1184 y, h
1185 | Cmulti (cl, b) ->
1186 if Array.length b = 0
1187 then 0, 0
1188 else
1189 let y, h = rowyh cl b pageno in
1190 let y =
1191 if conf.presentation
1192 then y - calcips h
1193 else y
1195 y, h
1196 | Csplit (c, b) ->
1197 if Array.length b = 0
1198 then 0, 0
1199 else
1200 let n = pageno*c in
1201 let (_, _, y, (_, _, h, _)) = b.(n) in
1202 y, h
1205 let getpagedim pageno =
1206 let rec f ppdim l =
1207 match l with
1208 | (n, _, _, _) as pdim :: rest ->
1209 if n >= pageno
1210 then (if n = pageno then pdim else ppdim)
1211 else f pdim rest
1213 | [] -> ppdim
1215 f (-1, -1, -1, -1) state.pdims
1218 let getpagey pageno = fst (getpageyh pageno);;
1220 let nogeomcmds cmds =
1221 match cmds with
1222 | s, [] -> String.length s = 0
1223 | _ -> false
1226 let page_of_y y =
1227 let ((c, coverA, coverB) as cl), b =
1228 match conf.columns with
1229 | Csingle b -> (1, 0, 0), b
1230 | Cmulti (c, b) -> c, b
1231 | Csplit (_, b) -> (1, 0, 0), b
1233 if Array.length b = 0
1234 then -1
1235 else
1236 let rec bsearch nmin nmax =
1237 if nmin > nmax
1238 then bound nmin 0 (state.pagecount-1)
1239 else
1240 let n = (nmax + nmin) / 2 in
1241 let vy, h = rowyh cl b n in
1242 let y0, y1 =
1243 if conf.presentation
1244 then
1245 let ips = calcips h in
1246 let y0 = vy - ips in
1247 let y1 = vy + h + ips in
1248 y0, y1
1249 else (
1250 if n = 0
1251 then 0, vy + h + conf.interpagespace
1252 else
1253 let y0 = vy - conf.interpagespace in
1254 y0, y0 + h + conf.interpagespace
1257 if y >= y0 && y < y1
1258 then (
1259 if c = 1
1260 then n
1261 else (
1262 if n > coverA
1263 then
1264 if n < state.pagecount - coverB
1265 then ((n-coverA)/c)*c + coverA
1266 else n
1267 else n
1270 else (
1271 if y > y0
1272 then bsearch (n+1) nmax
1273 else bsearch nmin (n-1)
1276 let r = bsearch 0 (state.pagecount-1) in
1280 let layoutN ((columns, coverA, coverB), b) y sh =
1281 let sh = sh - state.hscrollh in
1282 let rec fold accu n =
1283 if n = Array.length b
1284 then accu
1285 else
1286 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1287 if (vy - y) > sh &&
1288 (n = coverA - 1
1289 || n = state.pagecount - coverB
1290 || (n - coverA) mod columns = columns - 1)
1291 then accu
1292 else
1293 let accu =
1294 if vy + h > y
1295 then
1296 let pagey = max 0 (y - vy) in
1297 let pagedispy = if pagey > 0 then 0 else vy - y in
1298 let pagedispx, pagex =
1299 let pdx =
1300 if n = coverA - 1 || n = state.pagecount - coverB
1301 then state.x + (state.winw - state.scrollw - w) / 2
1302 else dx + xoff + state.x
1304 if pdx < 0
1305 then 0, -pdx
1306 else pdx, 0
1308 let pagevw =
1309 let vw = state.winw - state.scrollw - pagedispx in
1310 let pw = w - pagex in
1311 min vw pw
1313 let pagevh = min (h - pagey) (sh - pagedispy) in
1314 if pagevw > 0 && pagevh > 0
1315 then
1316 let e =
1317 { pageno = n
1318 ; pagedimno = pdimno
1319 ; pagew = w
1320 ; pageh = h
1321 ; pagex = pagex
1322 ; pagey = pagey
1323 ; pagevw = pagevw
1324 ; pagevh = pagevh
1325 ; pagedispx = pagedispx
1326 ; pagedispy = pagedispy
1327 ; pagecol = 0
1330 e :: accu
1331 else
1332 accu
1333 else
1334 accu
1336 fold accu (n+1)
1338 List.rev (fold [] (page_of_y y));
1341 let layoutS (columns, b) y sh =
1342 let sh = sh - state.hscrollh in
1343 let rec fold accu n =
1344 if n = Array.length b
1345 then accu
1346 else
1347 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1348 if (vy - y) > sh
1349 then accu
1350 else
1351 let accu =
1352 if vy + pageh > y
1353 then
1354 let x = xoff + state.x in
1355 let pagey = max 0 (y - vy) in
1356 let pagedispy = if pagey > 0 then 0 else vy - y in
1357 let pagedispx, pagex =
1358 if px = 0
1359 then (
1360 if x < 0
1361 then 0, -x
1362 else x, 0
1364 else (
1365 let px = px - x in
1366 if px < 0
1367 then -px, 0
1368 else 0, px
1371 let pagecolw = pagew/columns in
1372 let pagedispx =
1373 if pagecolw < state.winw
1374 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1375 else pagedispx
1377 let pagevw =
1378 let vw = state.winw - pagedispx - state.scrollw in
1379 let pw = pagew - pagex in
1380 min vw pw
1382 let pagevw = min pagevw pagecolw in
1383 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1384 if pagevw > 0 && pagevh > 0
1385 then
1386 let e =
1387 { pageno = n/columns
1388 ; pagedimno = pdimno
1389 ; pagew = pagew
1390 ; pageh = pageh
1391 ; pagex = pagex
1392 ; pagey = pagey
1393 ; pagevw = pagevw
1394 ; pagevh = pagevh
1395 ; pagedispx = pagedispx
1396 ; pagedispy = pagedispy
1397 ; pagecol = n mod columns
1400 e :: accu
1401 else
1402 accu
1403 else
1404 accu
1406 fold accu (n+1)
1408 List.rev (fold [] 0)
1411 let layout y sh =
1412 if nogeomcmds state.geomcmds
1413 then
1414 match conf.columns with
1415 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1416 | Cmulti c -> layoutN c y sh
1417 | Csplit s -> layoutS s y sh
1418 else []
1421 let clamp incr =
1422 let y = state.y + incr in
1423 let y = max 0 y in
1424 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1428 let itertiles l f =
1429 let tilex = l.pagex mod conf.tilew in
1430 let tiley = l.pagey mod conf.tileh in
1432 let col = l.pagex / conf.tilew in
1433 let row = l.pagey / conf.tileh in
1435 let rec rowloop row y0 dispy h =
1436 if h = 0
1437 then ()
1438 else (
1439 let dh = conf.tileh - y0 in
1440 let dh = min h dh in
1441 let rec colloop col x0 dispx w =
1442 if w = 0
1443 then ()
1444 else (
1445 let dw = conf.tilew - x0 in
1446 let dw = min w dw in
1448 f col row dispx dispy x0 y0 dw dh;
1449 colloop (col+1) 0 (dispx+dw) (w-dw)
1452 colloop col tilex l.pagedispx l.pagevw;
1453 rowloop (row+1) 0 (dispy+dh) (h-dh)
1456 if l.pagevw > 0 && l.pagevh > 0
1457 then rowloop row tiley l.pagedispy l.pagevh;
1460 let gettileopaque l col row =
1461 let key =
1462 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1464 try Some (Hashtbl.find state.tilemap key)
1465 with Not_found -> None
1468 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1469 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1470 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1473 let drawtiles l color =
1474 GlDraw.color color;
1475 let f col row x y tilex tiley w h =
1476 match gettileopaque l col row with
1477 | Some (opaque, _, t) ->
1478 let params = x, y, w, h, tilex, tiley in
1479 if conf.invert
1480 then (
1481 Gl.enable `blend;
1482 GlFunc.blend_func `zero `one_minus_src_color;
1484 drawtile params opaque;
1485 if conf.invert
1486 then Gl.disable `blend;
1487 if conf.debug
1488 then (
1489 let s = Printf.sprintf
1490 "%d[%d,%d] %f sec"
1491 l.pageno col row t
1493 let w = measurestr fstate.fontsize s in
1494 GlMisc.push_attrib [`current];
1495 GlDraw.color (0.0, 0.0, 0.0);
1496 GlDraw.rect
1497 (float (x-2), float (y-2))
1498 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1499 GlDraw.color (1.0, 1.0, 1.0);
1500 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1501 GlMisc.pop_attrib ();
1504 | _ ->
1505 let w =
1506 let lw = state.winw - state.scrollw - x in
1507 min lw w
1508 and h =
1509 let lh = state.winh - y in
1510 min lh h
1512 begin match state.texid with
1513 | Some id ->
1514 Gl.enable `texture_2d;
1515 GlTex.bind_texture `texture_2d id;
1516 let x0 = float x
1517 and y0 = float y
1518 and x1 = float (x+w)
1519 and y1 = float (y+h) in
1521 let tw = float w /. 16.0
1522 and th = float h /. 16.0 in
1523 let tx0 = float tilex /. 16.0
1524 and ty0 = float tiley /. 16.0 in
1525 let tx1 = tx0 +. tw
1526 and ty1 = ty0 +. th in
1527 GlDraw.begins `quads;
1528 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1529 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1530 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1531 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1532 GlDraw.ends ();
1534 Gl.disable `texture_2d;
1535 | None ->
1536 GlDraw.color (1.0, 1.0, 1.0);
1537 GlDraw.rect
1538 (float x, float y)
1539 (float (x+w), float (y+h));
1540 end;
1541 if w > 128 && h > fstate.fontsize + 10
1542 then (
1543 GlDraw.color (0.0, 0.0, 0.0);
1544 let c, r =
1545 if conf.verbose
1546 then (col*conf.tilew, row*conf.tileh)
1547 else col, row
1549 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1551 GlDraw.color color;
1553 itertiles l f
1556 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1558 let tilevisible1 l x y =
1559 let ax0 = l.pagex
1560 and ax1 = l.pagex + l.pagevw
1561 and ay0 = l.pagey
1562 and ay1 = l.pagey + l.pagevh in
1564 let bx0 = x
1565 and by0 = y in
1566 let bx1 = min (bx0 + conf.tilew) l.pagew
1567 and by1 = min (by0 + conf.tileh) l.pageh in
1569 let rx0 = max ax0 bx0
1570 and ry0 = max ay0 by0
1571 and rx1 = min ax1 bx1
1572 and ry1 = min ay1 by1 in
1574 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1575 nonemptyintersection
1578 let tilevisible layout n x y =
1579 let rec findpageinlayout m = function
1580 | l :: rest when l.pageno = n ->
1581 tilevisible1 l x y || (
1582 match conf.columns with
1583 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1584 | _ -> false
1586 | _ :: rest -> findpageinlayout 0 rest
1587 | [] -> false
1589 findpageinlayout 0 layout;
1592 let tileready l x y =
1593 tilevisible1 l x y &&
1594 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1597 let tilepage n p layout =
1598 let rec loop = function
1599 | l :: rest ->
1600 if l.pageno = n
1601 then
1602 let f col row _ _ _ _ _ _ =
1603 if state.currently = Idle
1604 then
1605 match gettileopaque l col row with
1606 | Some _ -> ()
1607 | None ->
1608 let x = col*conf.tilew
1609 and y = row*conf.tileh in
1610 let w =
1611 let w = l.pagew - x in
1612 min w conf.tilew
1614 let h =
1615 let h = l.pageh - y in
1616 min h conf.tileh
1618 let pbo =
1619 if conf.usepbo
1620 then getpbo w h conf.colorspace
1621 else "0"
1623 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1624 state.currently <-
1625 Tiling (
1626 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1627 conf.tilew, conf.tileh
1630 itertiles l f;
1631 else
1632 loop rest
1634 | [] -> ()
1636 if nogeomcmds state.geomcmds
1637 then loop layout;
1640 let preloadlayout y =
1641 let y = if y < state.winh then 0 else y - state.winh in
1642 let h = state.winh*3 in
1643 layout y h;
1646 let load pages =
1647 let rec loop pages =
1648 if state.currently != Idle
1649 then ()
1650 else
1651 match pages with
1652 | l :: rest ->
1653 begin match getopaque l.pageno with
1654 | None ->
1655 wcmd "page %d %d" l.pageno l.pagedimno;
1656 state.currently <- Loading (l, state.gen);
1657 | Some opaque ->
1658 tilepage l.pageno opaque pages;
1659 loop rest
1660 end;
1661 | _ -> ()
1663 if nogeomcmds state.geomcmds
1664 then loop pages
1667 let preload pages =
1668 load pages;
1669 if conf.preload && state.currently = Idle
1670 then load (preloadlayout state.y);
1673 let layoutready layout =
1674 let rec fold all ls =
1675 all && match ls with
1676 | l :: rest ->
1677 let seen = ref false in
1678 let allvisible = ref true in
1679 let foo col row _ _ _ _ _ _ =
1680 seen := true;
1681 allvisible := !allvisible &&
1682 begin match gettileopaque l col row with
1683 | Some _ -> true
1684 | None -> false
1687 itertiles l foo;
1688 fold (!seen && !allvisible) rest
1689 | [] -> true
1691 let alltilesvisible = fold true layout in
1692 alltilesvisible;
1695 let gotoy y =
1696 let y = bound y 0 state.maxy in
1697 let y, layout, proceed =
1698 match conf.maxwait with
1699 | Some time when state.ghyll == noghyll ->
1700 begin match state.throttle with
1701 | None ->
1702 let layout = layout y state.winh in
1703 let ready = layoutready layout in
1704 if not ready
1705 then (
1706 load layout;
1707 state.throttle <- Some (layout, y, now ());
1709 else G.postRedisplay "gotoy showall (None)";
1710 y, layout, ready
1711 | Some (_, _, started) ->
1712 let dt = now () -. started in
1713 if dt > time
1714 then (
1715 state.throttle <- None;
1716 let layout = layout y state.winh in
1717 load layout;
1718 G.postRedisplay "maxwait";
1719 y, layout, true
1721 else -1, [], false
1724 | _ ->
1725 let layout = layout y state.winh in
1726 if not !wtmode || layoutready layout
1727 then G.postRedisplay "gotoy ready";
1728 y, layout, true
1730 if proceed
1731 then (
1732 state.y <- y;
1733 state.layout <- layout;
1734 begin match state.mode with
1735 | LinkNav (Ltexact (pageno, linkno)) ->
1736 let rec loop = function
1737 | [] ->
1738 state.mode <- LinkNav (Ltgendir 0)
1739 | l :: _ when l.pageno = pageno ->
1740 begin match getopaque pageno with
1741 | None ->
1742 state.mode <- LinkNav (Ltgendir 0)
1743 | Some opaque ->
1744 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1745 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1746 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1747 then state.mode <- LinkNav (Ltgendir 0)
1749 | _ :: rest -> loop rest
1751 loop layout
1752 | _ -> ()
1753 end;
1754 begin match state.mode with
1755 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1756 if not (pagevisible layout pageno)
1757 then (
1758 match state.layout with
1759 | [] -> ()
1760 | l :: _ ->
1761 state.mode <- Birdseye (
1762 conf, leftx, l.pageno, hooverpageno, anchor
1765 | LinkNav (Ltgendir dir as lt) ->
1766 let linknav =
1767 let rec loop = function
1768 | [] -> lt
1769 | l :: rest ->
1770 match getopaque l.pageno with
1771 | None -> loop rest
1772 | Some opaque ->
1773 let link =
1774 let ld =
1775 if dir = 0
1776 then LDfirstvisible (l.pagex, l.pagey, dir)
1777 else (
1778 if dir > 0 then LDfirst else LDlast
1781 findlink opaque ld
1783 match link with
1784 | Lnotfound -> loop rest
1785 | Lfound n ->
1786 showlinktype (getlink opaque n);
1787 Ltexact (l.pageno, n)
1789 loop state.layout
1791 state.mode <- LinkNav linknav
1792 | _ -> ()
1793 end;
1794 preload layout;
1796 state.ghyll <- noghyll;
1797 if conf.updatecurs
1798 then (
1799 let mx, my = state.mpos in
1800 updateunder mx my;
1804 let conttiling pageno opaque =
1805 tilepage pageno opaque
1806 (if conf.preload then preloadlayout state.y else state.layout)
1809 let gotoy_and_clear_text y =
1810 if not conf.verbose then state.text <- "";
1811 gotoy y;
1814 let getanchor1 l =
1815 let top =
1816 let coloff = l.pagecol * l.pageh in
1817 float (l.pagey + coloff) /. float l.pageh
1819 let dtop =
1820 if l.pagedispy = 0
1821 then
1823 else
1824 if conf.presentation
1825 then float l.pagedispy /. float (calcips l.pageh)
1826 else float l.pagedispy /. float conf.interpagespace
1828 (l.pageno, top, dtop)
1831 let getanchor () =
1832 match state.layout with
1833 | l :: _ -> getanchor1 l
1834 | [] ->
1835 let n = page_of_y state.y in
1836 if n = -1
1837 then state.anchor
1838 else
1839 let y, h = getpageyh n in
1840 let dy = y - state.y in
1841 let dtop =
1842 if conf.presentation
1843 then
1844 let ips = calcips h in
1845 float (dy + ips) /. float ips
1846 else
1847 float dy /. float conf.interpagespace
1849 (n, 0.0, dtop)
1852 let getanchory (n, top, dtop) =
1853 let y, h = getpageyh n in
1854 if conf.presentation
1855 then
1856 let ips = calcips h in
1857 y + truncate (top*.float h -. dtop*.float ips) + ips;
1858 else
1859 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1862 let gotoanchor anchor =
1863 gotoy (getanchory anchor);
1866 let addnav () =
1867 cbput state.hists.nav (getanchor ());
1870 let getnav dir =
1871 let anchor = cbgetc state.hists.nav dir in
1872 getanchory anchor;
1875 let gotoghyll y =
1876 let scroll f n a b =
1877 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1878 let snake f a b =
1879 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1880 if f < a
1881 then s (float f /. float a)
1882 else (
1883 if f > b
1884 then 1.0 -. s ((float (f-b) /. float (n-b)))
1885 else 1.0
1888 snake f a b
1889 and summa f n a b =
1890 (* courtesy:
1891 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1892 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1893 let iv1 = iv f in
1894 let ins = float a *. iv1
1895 and outs = float (n-b) *. iv1 in
1896 let ones = b - a in
1897 ins +. outs +. float ones
1899 let rec set (_N, _A, _B) y sy =
1900 let sum = summa 1.0 _N _A _B in
1901 let dy = float (y - sy) in
1902 state.ghyll <- (
1903 let rec gf n y1 o =
1904 if n >= _N
1905 then state.ghyll <- noghyll
1906 else
1907 let go n =
1908 let s = scroll n _N _A _B in
1909 let y1 = y1 +. ((s *. dy) /. sum) in
1910 gotoy_and_clear_text (truncate y1);
1911 state.ghyll <- gf (n+1) y1;
1913 match o with
1914 | None -> go n
1915 | Some y' -> set (_N/2, 1, 1) y' state.y
1917 gf 0 (float state.y)
1920 match conf.ghyllscroll with
1921 | None ->
1922 gotoy_and_clear_text y
1923 | Some nab ->
1924 if state.ghyll == noghyll
1925 then set nab y state.y
1926 else state.ghyll (Some y)
1929 let gotopage n top =
1930 let y, h = getpageyh n in
1931 let y = y + (truncate (top *. float h)) in
1932 gotoghyll y
1935 let gotopage1 n top =
1936 let y = getpagey n in
1937 let y = y + top in
1938 gotoghyll y
1941 let invalidate s f =
1942 state.layout <- [];
1943 state.pdims <- [];
1944 state.rects <- [];
1945 state.rects1 <- [];
1946 match state.geomcmds with
1947 | ps, [] when String.length ps = 0 ->
1948 f ();
1949 state.geomcmds <- s, [];
1951 | ps, [] ->
1952 state.geomcmds <- ps, [s, f];
1954 | ps, (s', _) :: rest when s' = s ->
1955 state.geomcmds <- ps, ((s, f) :: rest);
1957 | ps, cmds ->
1958 state.geomcmds <- ps, ((s, f) :: cmds);
1961 let flushpages () =
1962 Hashtbl.iter (fun _ opaque ->
1963 wcmd "freepage %s" opaque;
1964 ) state.pagemap;
1965 Hashtbl.clear state.pagemap;
1968 let flushtiles () =
1969 if not (Queue.is_empty state.tilelru)
1970 then (
1971 Queue.iter (fun (k, p, s) ->
1972 wcmd "freetile %s" p;
1973 state.memused <- state.memused - s;
1974 Hashtbl.remove state.tilemap k;
1975 ) state.tilelru;
1976 state.uioh#infochanged Memused;
1977 Queue.clear state.tilelru;
1979 load state.layout;
1982 let opendoc path password =
1983 state.path <- path;
1984 state.password <- password;
1985 state.gen <- state.gen + 1;
1986 state.docinfo <- [];
1988 flushpages ();
1989 setaalevel conf.aalevel;
1990 let titlepath =
1991 if String.length state.origin = 0
1992 then path
1993 else state.origin
1995 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1996 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
1997 invalidate "reqlayout"
1998 (fun () ->
1999 wcmd "reqlayout %d %d %s\000"
2000 conf.angle (int_of_fitmodel conf.fitmodel) state.nameddest;
2004 let reload () =
2005 state.anchor <- getanchor ();
2006 opendoc state.path state.password;
2009 let scalecolor c =
2010 let c = c *. conf.colorscale in
2011 (c, c, c);
2014 let scalecolor2 (r, g, b) =
2015 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2018 let docolumns = function
2019 | Csingle _ ->
2020 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2021 let rec loop pageno pdimno pdim y ph pdims =
2022 if pageno = state.pagecount
2023 then ()
2024 else
2025 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2026 match pdims with
2027 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2028 pdimno+1, pdim, rest
2029 | _ ->
2030 pdimno, pdim, pdims
2032 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
2033 let y = y +
2034 (if conf.presentation
2035 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2036 else (if pageno = 0 then 0 else conf.interpagespace)
2039 a.(pageno) <- (pdimno, x, y, pdim);
2040 loop (pageno+1) pdimno pdim (y + h) h pdims
2042 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2043 conf.columns <- Csingle a;
2045 | Cmulti ((columns, coverA, coverB), _) ->
2046 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2047 let rec loop pageno pdimno pdim x y rowh pdims =
2048 let rec fixrow m = if m = pageno then () else
2049 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2050 if h < rowh
2051 then (
2052 let y = y + (rowh - h) / 2 in
2053 a.(m) <- (pdimno, x, y, pdim);
2055 fixrow (m+1)
2057 if pageno = state.pagecount
2058 then fixrow (((pageno - 1) / columns) * columns)
2059 else
2060 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2061 match pdims with
2062 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2063 pdimno+1, pdim, rest
2064 | _ ->
2065 pdimno, pdim, pdims
2067 let x, y, rowh' =
2068 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2069 then (
2070 let x = (state.winw - state.scrollw - w) / 2 in
2071 let ips =
2072 if conf.presentation then calcips h else conf.interpagespace in
2073 x, y + ips + rowh, h
2075 else (
2076 if (pageno - coverA) mod columns = 0
2077 then (
2078 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2079 let y =
2080 if conf.presentation
2081 then
2082 let ips = calcips h in
2083 y + (if pageno = 0 then 0 else calcips rowh + ips)
2084 else
2085 y + (if pageno = 0 then 0 else conf.interpagespace)
2087 x, y + rowh, h
2089 else x, y, max rowh h
2092 let y =
2093 if pageno > 1 && (pageno - coverA) mod columns = 0
2094 then (
2095 let y =
2096 if pageno = columns && conf.presentation
2097 then (
2098 let ips = calcips rowh in
2099 for i = 0 to pred columns
2101 let (pdimno, x, y, pdim) = a.(i) in
2102 a.(i) <- (pdimno, x, y+ips, pdim)
2103 done;
2104 y+ips;
2106 else y
2108 fixrow (pageno - columns);
2111 else y
2113 a.(pageno) <- (pdimno, x, y, pdim);
2114 let x = x + w + xoff*2 + conf.interpagespace in
2115 loop (pageno+1) pdimno pdim x y rowh' pdims
2117 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2118 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2120 | Csplit (c, _) ->
2121 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2122 let rec loop pageno pdimno pdim y pdims =
2123 if pageno = state.pagecount
2124 then ()
2125 else
2126 let pdimno, ((_, w, h, _) as pdim), pdims =
2127 match pdims with
2128 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2129 pdimno+1, pdim, rest
2130 | _ ->
2131 pdimno, pdim, pdims
2133 let cw = w / c in
2134 let rec loop1 n x y =
2135 if n = c then y else (
2136 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2137 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2140 let y = loop1 0 0 y in
2141 loop (pageno+1) pdimno pdim y pdims
2143 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2144 conf.columns <- Csplit (c, a);
2147 let represent () =
2148 docolumns conf.columns;
2149 state.maxy <- calcheight ();
2150 state.hscrollh <-
2151 if state.x = 0 && state.w <= state.winw - state.scrollw
2152 then 0
2153 else state.scrollw
2155 if state.reprf == noreprf
2156 then (
2157 match state.mode with
2158 | Birdseye (_, _, pageno, _, _) ->
2159 let y, h = getpageyh pageno in
2160 let top = (state.winh - h) / 2 in
2161 gotoy (max 0 (y - top))
2162 | _ -> gotoanchor state.anchor
2164 else (
2165 state.reprf ();
2166 state.reprf <- noreprf;
2170 let reshape w h =
2171 GlDraw.viewport 0 0 w h;
2172 let firsttime = state.geomcmds == firstgeomcmds in
2173 if not firsttime && nogeomcmds state.geomcmds
2174 then state.anchor <- getanchor ();
2176 state.winw <- w;
2177 let w = truncate (float w *. conf.zoom) - state.scrollw in
2178 let w = max w 2 in
2179 state.winh <- h;
2180 setfontsize fstate.fontsize;
2181 GlMat.mode `modelview;
2182 GlMat.load_identity ();
2184 GlMat.mode `projection;
2185 GlMat.load_identity ();
2186 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2187 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2188 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2190 let relx =
2191 if conf.zoom <= 1.0
2192 then 0.0
2193 else float state.x /. float state.w
2195 invalidate "geometry"
2196 (fun () ->
2197 state.w <- w;
2198 if not firsttime
2199 then state.x <- truncate (relx *. float w);
2200 let w =
2201 match conf.columns with
2202 | Csingle _ -> w
2203 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2204 | Csplit (c, _) -> w * c
2206 wcmd "geometry %d %d %d"
2207 w ((truncate (float h*.conf.zoom)) - 2*conf.interpagespace)
2208 (int_of_fitmodel conf.fitmodel)
2212 let enttext () =
2213 let len = String.length state.text in
2214 let drawstring s =
2215 let hscrollh =
2216 match state.mode with
2217 | Textentry _
2218 | View ->
2219 let h, _, _ = state.uioh#scrollpw in
2221 | _ -> 0
2223 let rect x w =
2224 GlDraw.rect
2225 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2226 (x+.w, float (state.winh - hscrollh))
2229 let w = float (state.winw - state.scrollw - 1) in
2230 if state.progress >= 0.0 && state.progress < 1.0
2231 then (
2232 GlDraw.color (0.3, 0.3, 0.3);
2233 let w1 = w *. state.progress in
2234 rect 0.0 w1;
2235 GlDraw.color (0.0, 0.0, 0.0);
2236 rect w1 (w-.w1)
2238 else (
2239 GlDraw.color (0.0, 0.0, 0.0);
2240 rect 0.0 w;
2243 GlDraw.color (1.0, 1.0, 1.0);
2244 drawstring fstate.fontsize
2245 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2247 let s =
2248 match state.mode with
2249 | Textentry ((prefix, text, _, _, _, _), _) ->
2250 let s =
2251 if len > 0
2252 then
2253 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2254 else
2255 Printf.sprintf "%s%s_" prefix text
2259 | _ -> state.text
2261 let s =
2262 if state.newerrmsgs
2263 then (
2264 if not (istextentry state.mode) && state.uioh#eformsgs
2265 then
2266 let s1 = "(press 'e' to review error messasges)" in
2267 if String.length s > 0 then s ^ " " ^ s1 else s1
2268 else s
2270 else s
2272 if String.length s > 0
2273 then drawstring s
2276 let gctiles () =
2277 let len = Queue.length state.tilelru in
2278 let layout = lazy (
2279 match state.throttle with
2280 | None ->
2281 if conf.preload
2282 then preloadlayout state.y
2283 else state.layout
2284 | Some (layout, _, _) ->
2285 layout
2286 ) in
2287 let rec loop qpos =
2288 if state.memused <= conf.memlimit
2289 then ()
2290 else (
2291 if qpos < len
2292 then
2293 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2294 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2295 let (_, pw, ph, _) = getpagedim n in
2297 gen = state.gen
2298 && colorspace = conf.colorspace
2299 && angle = conf.angle
2300 && pagew = pw
2301 && pageh = ph
2302 && (
2303 let x = col*conf.tilew
2304 and y = row*conf.tileh in
2305 tilevisible (Lazy.force_val layout) n x y
2307 then Queue.push lruitem state.tilelru
2308 else (
2309 freepbo p;
2310 wcmd "freetile %s" p;
2311 state.memused <- state.memused - s;
2312 state.uioh#infochanged Memused;
2313 Hashtbl.remove state.tilemap k;
2315 loop (qpos+1)
2318 loop 0
2321 let logcurrently = function
2322 | Idle -> dolog "Idle"
2323 | Loading (l, gen) ->
2324 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2325 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2326 dolog
2327 "Tiling %d[%d,%d] page=%s cs=%s angle"
2328 l.pageno col row pageopaque
2329 (colorspace_to_string colorspace)
2331 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2332 angle gen conf.angle state.gen
2333 tilew tileh
2334 conf.tilew conf.tileh
2336 | Outlining _ ->
2337 dolog "outlining"
2340 let splitatspace =
2341 let r = Str.regexp " " in
2342 fun s -> Str.bounded_split r s 2;
2345 let onpagerect pageno f =
2346 let b =
2347 match conf.columns with
2348 | Cmulti (_, b) -> b
2349 | Csingle b -> b
2350 | Csplit (_, b) -> b
2352 if pageno >= 0 && pageno < Array.length b
2353 then
2354 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2355 let r = getpdimrect pdimno in
2356 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2359 let gotopagexy1 pageno x y =
2360 onpagerect pageno (fun w h ->
2361 let top = y /. h in
2362 let _,w1,_,leftx = getpagedim pageno in
2363 let wh = state.winh - state.hscrollh in
2364 let sw = float w1 /. w in
2365 let x = sw *. x in
2366 let x = leftx + state.x + truncate x in
2367 let sx =
2368 if x < 0 || x >= state.winw - state.scrollw
2369 then state.x - x
2370 else state.x
2372 let py, h = getpageyh pageno in
2373 let pdy = truncate (top *. float h) in
2374 let y' = py + pdy in
2375 let dy = y' - state.y in
2376 let sy =
2377 if x != state.x || not (dy > 0 && dy < wh)
2378 then (
2379 if conf.presentation
2380 then
2381 if abs (py - y') > wh
2382 then y'
2383 else py
2384 else y';
2386 else state.y
2388 if state.x != sx || state.y != sy
2389 then (
2390 let x, y =
2391 if !wtmode
2392 then (
2393 let ww = state.winw - state.scrollw in
2394 let qx = sx / ww
2395 and qy = pdy / wh in
2396 let x = qx * ww
2397 and y = py + qy * wh in
2398 let x = if -x + ww > w1 then -(w1-ww) else x
2399 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2400 let y =
2401 if conf.presentation
2402 then
2403 if abs (py - y') > wh
2404 then y'
2405 else py
2406 else y';
2408 (x, y)
2410 else (sx, sy)
2412 state.x <- x;
2413 state.hscrollh <-
2414 if x = 0 && state.w <= state.winw - state.scrollw
2415 then 0
2416 else state.scrollw
2418 gotoy_and_clear_text y;
2420 else gotoy_and_clear_text state.y;
2424 let gotopagexy pageno x y =
2425 match state.mode with
2426 | Birdseye _ -> gotopage pageno 0.0
2427 | _ -> gotopagexy1 pageno x y
2430 let act cmds =
2431 (* dolog "%S" cmds; *)
2432 let cl = splitatspace cmds in
2433 let scan s fmt f =
2434 try Scanf.sscanf s fmt f
2435 with exn ->
2436 dolog "error processing '%S': %s" cmds (exntos exn);
2437 exit 1
2439 match cl with
2440 | "clear" :: [] ->
2441 state.uioh#infochanged Pdim;
2442 state.pdims <- [];
2444 | "clearrects" :: [] ->
2445 state.rects <- state.rects1;
2446 G.postRedisplay "clearrects";
2448 | "continue" :: args :: [] ->
2449 let n = scan args "%u" (fun n -> n) in
2450 state.pagecount <- n;
2451 begin match state.currently with
2452 | Outlining l ->
2453 state.currently <- Idle;
2454 state.outlines <- Array.of_list (List.rev l)
2455 | _ -> ()
2456 end;
2458 let cur, cmds = state.geomcmds in
2459 if String.length cur = 0
2460 then failwith "umpossible";
2462 begin match List.rev cmds with
2463 | [] ->
2464 state.geomcmds <- "", [];
2465 represent ();
2466 | (s, f) :: rest ->
2467 f ();
2468 state.geomcmds <- s, List.rev rest;
2469 end;
2470 if conf.maxwait = None && not !wtmode
2471 then G.postRedisplay "continue";
2473 | "title" :: args :: [] ->
2474 Wsi.settitle args
2476 | "msg" :: args :: [] ->
2477 showtext ' ' args
2479 | "vmsg" :: args :: [] ->
2480 if conf.verbose
2481 then showtext ' ' args
2483 | "emsg" :: args :: [] ->
2484 Buffer.add_string state.errmsgs args;
2485 state.newerrmsgs <- true;
2486 G.postRedisplay "error message"
2488 | "progress" :: args :: [] ->
2489 let progress, text =
2490 scan args "%f %n"
2491 (fun f pos ->
2492 f, String.sub args pos (String.length args - pos))
2494 state.text <- text;
2495 state.progress <- progress;
2496 G.postRedisplay "progress"
2498 | "firstmatch" :: args :: [] ->
2499 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2500 scan args "%u %d %f %f %f %f %f %f %f %f"
2501 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2502 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2504 let y = (getpagey pageno) + truncate y0 in
2505 addnav ();
2506 gotoy y;
2507 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2509 | "match" :: args :: [] ->
2510 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2511 scan args "%u %d %f %f %f %f %f %f %f %f"
2512 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2513 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2515 state.rects1 <-
2516 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2518 | "page" :: args :: [] ->
2519 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2520 begin match state.currently with
2521 | Loading (l, gen) ->
2522 vlog "page %d took %f sec" l.pageno t;
2523 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2524 begin match state.throttle with
2525 | None ->
2526 let preloadedpages =
2527 if conf.preload
2528 then preloadlayout state.y
2529 else state.layout
2531 let evict () =
2532 let set =
2533 List.fold_left (fun s l -> IntSet.add l.pageno s)
2534 IntSet.empty preloadedpages
2536 let evictedpages =
2537 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2538 if not (IntSet.mem pageno set)
2539 then (
2540 wcmd "freepage %s" opaque;
2541 key :: accu
2543 else accu
2544 ) state.pagemap []
2546 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2548 evict ();
2549 state.currently <- Idle;
2550 if gen = state.gen
2551 then (
2552 tilepage l.pageno pageopaque state.layout;
2553 load state.layout;
2554 load preloadedpages;
2555 if pagevisible state.layout l.pageno
2556 && layoutready state.layout
2557 then G.postRedisplay "page";
2560 | Some (layout, _, _) ->
2561 state.currently <- Idle;
2562 tilepage l.pageno pageopaque layout;
2563 load state.layout
2564 end;
2566 | _ ->
2567 dolog "Inconsistent loading state";
2568 logcurrently state.currently;
2569 exit 1
2572 | "tile" :: args :: [] ->
2573 let (x, y, opaque, size, t) =
2574 scan args "%u %u %s %u %f"
2575 (fun x y p size t -> (x, y, p, size, t))
2577 begin match state.currently with
2578 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2579 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2581 unmappbo opaque;
2582 if tilew != conf.tilew || tileh != conf.tileh
2583 then (
2584 wcmd "freetile %s" opaque;
2585 state.currently <- Idle;
2586 load state.layout;
2588 else (
2589 puttileopaque l col row gen cs angle opaque size t;
2590 state.memused <- state.memused + size;
2591 state.uioh#infochanged Memused;
2592 gctiles ();
2593 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2594 opaque, size) state.tilelru;
2596 let layout =
2597 match state.throttle with
2598 | None -> state.layout
2599 | Some (layout, _, _) -> layout
2602 state.currently <- Idle;
2603 if gen = state.gen
2604 && conf.colorspace = cs
2605 && conf.angle = angle
2606 && tilevisible layout l.pageno x y
2607 then conttiling l.pageno pageopaque;
2609 begin match state.throttle with
2610 | None ->
2611 preload state.layout;
2612 if gen = state.gen
2613 && conf.colorspace = cs
2614 && conf.angle = angle
2615 && tilevisible state.layout l.pageno x y
2616 && (not !wtmode || layoutready state.layout)
2617 then G.postRedisplay "tile nothrottle";
2619 | Some (layout, y, _) ->
2620 let ready = layoutready layout in
2621 if ready
2622 then (
2623 state.y <- y;
2624 state.layout <- layout;
2625 state.throttle <- None;
2626 G.postRedisplay "throttle";
2628 else load layout;
2629 end;
2632 | _ ->
2633 dolog "Inconsistent tiling state";
2634 logcurrently state.currently;
2635 exit 1
2638 | "pdim" :: args :: [] ->
2639 let (n, w, h, _) as pdim =
2640 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2642 let pdim =
2643 match conf.fitmodel, conf.columns with
2644 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2645 | _ -> pdim
2647 state.uioh#infochanged Pdim;
2648 state.pdims <- pdim :: state.pdims
2650 | "o" :: args :: [] ->
2651 let (l, n, t, h, pos) =
2652 scan args "%u %u %d %u %n"
2653 (fun l n t h pos -> l, n, t, h, pos)
2655 let s = String.sub args pos (String.length args - pos) in
2656 let outline = (s, l, (n, float t /. float h, 0.0)) in
2657 begin match state.currently with
2658 | Outlining outlines ->
2659 state.currently <- Outlining (outline :: outlines)
2660 | Idle ->
2661 state.currently <- Outlining [outline]
2662 | currently ->
2663 dolog "invalid outlining state";
2664 logcurrently currently
2667 | "a" :: args :: [] ->
2668 let (n, l, t) =
2669 scan args "%u %d %d" (fun n l t -> n, l, t)
2671 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2673 | "info" :: args :: [] ->
2674 state.docinfo <- (1, args) :: state.docinfo
2676 | "infoend" :: [] ->
2677 state.uioh#infochanged Docinfo;
2678 state.docinfo <- List.rev state.docinfo
2680 | _ ->
2681 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2684 let onhist cb =
2685 let rc = cb.rc in
2686 let action = function
2687 | HCprev -> cbget cb ~-1
2688 | HCnext -> cbget cb 1
2689 | HCfirst -> cbget cb ~-(cb.rc)
2690 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2691 and cancel () = cb.rc <- rc
2692 in (action, cancel)
2695 let search pattern forward =
2696 if String.length pattern > 0
2697 then
2698 let pn, py =
2699 match state.layout with
2700 | [] -> 0, 0
2701 | l :: _ ->
2702 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2704 wcmd "search %d %d %d %d,%s\000"
2705 (btod conf.icase) pn py (btod forward) pattern;
2708 let intentry text key =
2709 let c =
2710 if key >= 32 && key < 127
2711 then Char.chr key
2712 else '\000'
2714 match c with
2715 | '0' .. '9' ->
2716 let text = addchar text c in
2717 TEcont text
2719 | _ ->
2720 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2721 TEcont text
2724 let linknentry text key =
2725 let c =
2726 if key >= 32 && key < 127
2727 then Char.chr key
2728 else '\000'
2730 match c with
2731 | 'a' .. 'z' ->
2732 let text = addchar text c in
2733 TEcont text
2735 | _ ->
2736 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2737 TEcont text
2740 let linkndone f s =
2741 if String.length s > 0
2742 then (
2743 let n =
2744 let l = String.length s in
2745 let rec loop pos n = if pos = l then n else
2746 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2747 loop (pos+1) (n*26 + m)
2748 in loop 0 0
2750 let rec loop n = function
2751 | [] -> ()
2752 | l :: rest ->
2753 match getopaque l.pageno with
2754 | None -> loop n rest
2755 | Some opaque ->
2756 let m = getlinkcount opaque in
2757 if n < m
2758 then (
2759 let under = getlink opaque n in
2760 f under
2762 else loop (n-m) rest
2764 loop n state.layout;
2768 let textentry text key =
2769 if key land 0xff00 = 0xff00
2770 then TEcont text
2771 else TEcont (text ^ toutf8 key)
2774 let reqlayout angle fitmodel =
2775 match state.throttle with
2776 | None ->
2777 if nogeomcmds state.geomcmds
2778 then state.anchor <- getanchor ();
2779 conf.angle <- angle mod 360;
2780 if conf.angle != 0
2781 then (
2782 match state.mode with
2783 | LinkNav _ -> state.mode <- View
2784 | _ -> ()
2786 conf.fitmodel <- fitmodel;
2787 invalidate "reqlayout"
2788 (fun () ->
2789 wcmd "reqlayout %d %d" conf.angle (int_of_fitmodel conf.fitmodel)
2791 | _ -> ()
2794 let settrim trimmargins trimfuzz =
2795 if nogeomcmds state.geomcmds
2796 then state.anchor <- getanchor ();
2797 conf.trimmargins <- trimmargins;
2798 conf.trimfuzz <- trimfuzz;
2799 let x0, y0, x1, y1 = trimfuzz in
2800 invalidate "settrim"
2801 (fun () ->
2802 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2803 flushpages ();
2806 let setzoom zoom =
2807 match state.throttle with
2808 | None ->
2809 let zoom = max 0.0001 zoom in
2810 if zoom <> conf.zoom
2811 then (
2812 state.prevzoom <- conf.zoom;
2813 conf.zoom <- zoom;
2814 reshape state.winw state.winh;
2815 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2818 | Some (layout, y, started) ->
2819 let time =
2820 match conf.maxwait with
2821 | None -> 0.0
2822 | Some t -> t
2824 let dt = now () -. started in
2825 if dt > time
2826 then (
2827 state.y <- y;
2828 load layout;
2832 let setcolumns mode columns coverA coverB =
2833 state.prevcolumns <- Some (conf.columns, conf.zoom);
2834 if columns < 0
2835 then (
2836 if isbirdseye mode
2837 then showtext '!' "split mode doesn't work in bird's eye"
2838 else (
2839 conf.columns <- Csplit (-columns, [||]);
2840 state.x <- 0;
2841 conf.zoom <- 1.0;
2844 else (
2845 if columns < 2
2846 then (
2847 conf.columns <- Csingle [||];
2848 state.x <- 0;
2849 setzoom 1.0;
2851 else (
2852 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2853 conf.zoom <- 1.0;
2856 reshape state.winw state.winh;
2859 let enterbirdseye () =
2860 let zoom = float conf.thumbw /. float state.winw in
2861 let birdseyepageno =
2862 let cy = state.winh / 2 in
2863 let fold = function
2864 | [] -> 0
2865 | l :: rest ->
2866 let rec fold best = function
2867 | [] -> best.pageno
2868 | l :: rest ->
2869 let d = cy - (l.pagedispy + l.pagevh/2)
2870 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2871 if abs d < abs dbest
2872 then fold l rest
2873 else best.pageno
2874 in fold l rest
2876 fold state.layout
2878 state.mode <- Birdseye (
2879 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2881 conf.zoom <- zoom;
2882 conf.presentation <- false;
2883 conf.interpagespace <- 10;
2884 conf.hlinks <- false;
2885 conf.fitmodel <- FitProportional;
2886 state.x <- 0;
2887 state.mstate <- Mnone;
2888 conf.maxwait <- None;
2889 conf.columns <- (
2890 match conf.beyecolumns with
2891 | Some c ->
2892 conf.zoom <- 1.0;
2893 Cmulti ((c, 0, 0), [||])
2894 | None -> Csingle [||]
2896 Wsi.setcursor Wsi.CURSOR_INHERIT;
2897 if conf.verbose
2898 then
2899 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2900 (100.0*.zoom)
2901 else
2902 state.text <- ""
2904 reshape state.winw state.winh;
2907 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2908 state.mode <- View;
2909 conf.zoom <- c.zoom;
2910 conf.presentation <- c.presentation;
2911 conf.interpagespace <- c.interpagespace;
2912 conf.maxwait <- c.maxwait;
2913 conf.hlinks <- c.hlinks;
2914 conf.fitmodel <- c.fitmodel;
2915 conf.beyecolumns <- (
2916 match conf.columns with
2917 | Cmulti ((c, _, _), _) -> Some c
2918 | Csingle _ -> None
2919 | Csplit _ -> failwith "leaving bird's eye split mode"
2921 conf.columns <- (
2922 match c.columns with
2923 | Cmulti (c, _) -> Cmulti (c, [||])
2924 | Csingle _ -> Csingle [||]
2925 | Csplit (c, _) -> Csplit (c, [||])
2927 state.x <- leftx;
2928 if conf.verbose
2929 then
2930 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2931 (100.0*.conf.zoom)
2933 reshape state.winw state.winh;
2934 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2937 let togglebirdseye () =
2938 match state.mode with
2939 | Birdseye vals -> leavebirdseye vals true
2940 | View -> enterbirdseye ()
2941 | _ -> ()
2944 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2945 let pageno = max 0 (pageno - incr) in
2946 let rec loop = function
2947 | [] -> gotopage1 pageno 0
2948 | l :: _ when l.pageno = pageno ->
2949 if l.pagedispy >= 0 && l.pagey = 0
2950 then G.postRedisplay "upbirdseye"
2951 else gotopage1 pageno 0
2952 | _ :: rest -> loop rest
2954 loop state.layout;
2955 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2958 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2959 let pageno = min (state.pagecount - 1) (pageno + incr) in
2960 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2961 let rec loop = function
2962 | [] ->
2963 let y, h = getpageyh pageno in
2964 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2965 gotoy (clamp dy)
2966 | l :: _ when l.pageno = pageno ->
2967 if l.pagevh != l.pageh
2968 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2969 else G.postRedisplay "downbirdseye"
2970 | _ :: rest -> loop rest
2972 loop state.layout
2975 let optentry mode _ key =
2976 let btos b = if b then "on" else "off" in
2977 if key >= 32 && key < 127
2978 then
2979 let c = Char.chr key in
2980 match c with
2981 | 's' ->
2982 let ondone s =
2983 try conf.scrollstep <- int_of_string s with exc ->
2984 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2986 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2988 | 'A' ->
2989 let ondone s =
2991 conf.autoscrollstep <- int_of_string s;
2992 if state.autoscroll <> None
2993 then state.autoscroll <- Some conf.autoscrollstep
2994 with exc ->
2995 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2997 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2999 | 'C' ->
3000 let ondone s =
3002 let n, a, b = multicolumns_of_string s in
3003 setcolumns mode n a b;
3004 with exc ->
3005 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3007 TEswitch ("columns: ", "", None, textentry, ondone, true)
3009 | 'Z' ->
3010 let ondone s =
3012 let zoom = float (int_of_string s) /. 100.0 in
3013 setzoom zoom
3014 with exc ->
3015 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3017 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3019 | 't' ->
3020 let ondone s =
3022 conf.thumbw <- bound (int_of_string s) 2 4096;
3023 state.text <-
3024 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3025 begin match mode with
3026 | Birdseye beye ->
3027 leavebirdseye beye false;
3028 enterbirdseye ();
3029 | _ -> ();
3031 with exc ->
3032 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3034 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3036 | 'R' ->
3037 let ondone s =
3038 match try
3039 Some (int_of_string s)
3040 with exc ->
3041 state.text <- Printf.sprintf "bad integer `%s': %s"
3042 s (exntos exc);
3043 None
3044 with
3045 | Some angle -> reqlayout angle conf.fitmodel
3046 | None -> ()
3048 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3050 | 'i' ->
3051 conf.icase <- not conf.icase;
3052 TEdone ("case insensitive search " ^ (btos conf.icase))
3054 | 'p' ->
3055 conf.preload <- not conf.preload;
3056 gotoy state.y;
3057 TEdone ("preload " ^ (btos conf.preload))
3059 | 'v' ->
3060 conf.verbose <- not conf.verbose;
3061 TEdone ("verbose " ^ (btos conf.verbose))
3063 | 'd' ->
3064 conf.debug <- not conf.debug;
3065 TEdone ("debug " ^ (btos conf.debug))
3067 | 'h' ->
3068 conf.maxhfit <- not conf.maxhfit;
3069 state.maxy <- calcheight ();
3070 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3072 | 'c' ->
3073 conf.crophack <- not conf.crophack;
3074 TEdone ("crophack " ^ btos conf.crophack)
3076 | 'a' ->
3077 let s =
3078 match conf.maxwait with
3079 | None ->
3080 conf.maxwait <- Some infinity;
3081 "always wait for page to complete"
3082 | Some _ ->
3083 conf.maxwait <- None;
3084 "show placeholder if page is not ready"
3086 TEdone s
3088 | 'f' ->
3089 conf.underinfo <- not conf.underinfo;
3090 TEdone ("underinfo " ^ btos conf.underinfo)
3092 | 'P' ->
3093 conf.savebmarks <- not conf.savebmarks;
3094 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3096 | 'S' ->
3097 let ondone s =
3099 let pageno, py =
3100 match state.layout with
3101 | [] -> 0, 0
3102 | l :: _ ->
3103 l.pageno, l.pagey
3105 conf.interpagespace <- int_of_string s;
3106 docolumns conf.columns;
3107 state.maxy <- calcheight ();
3108 let y = getpagey pageno in
3109 gotoy (y + py)
3110 with exc ->
3111 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3113 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3115 | 'l' ->
3116 let fm =
3117 match conf.fitmodel with
3118 | FitProportional -> FitWidth
3119 | _ -> FitProportional
3121 reqlayout conf.angle fm;
3122 TEdone ("proportional display " ^ btos (fm == FitProportional))
3124 | 'T' ->
3125 settrim (not conf.trimmargins) conf.trimfuzz;
3126 TEdone ("trim margins " ^ btos conf.trimmargins)
3128 | 'I' ->
3129 conf.invert <- not conf.invert;
3130 TEdone ("invert colors " ^ btos conf.invert)
3132 | 'x' ->
3133 let ondone s =
3134 cbput state.hists.sel s;
3135 conf.selcmd <- s;
3137 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3138 textentry, ondone, true)
3140 | _ ->
3141 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3142 TEstop
3143 else
3144 TEcont state.text
3147 class type lvsource = object
3148 method getitemcount : int
3149 method getitem : int -> (string * int)
3150 method hasaction : int -> bool
3151 method exit :
3152 uioh:uioh ->
3153 cancel:bool ->
3154 active:int ->
3155 first:int ->
3156 pan:int ->
3157 qsearch:string ->
3158 uioh option
3159 method getactive : int
3160 method getfirst : int
3161 method getqsearch : string
3162 method setqsearch : string -> unit
3163 method getpan : int
3164 end;;
3166 class virtual lvsourcebase = object
3167 val mutable m_active = 0
3168 val mutable m_first = 0
3169 val mutable m_qsearch = ""
3170 val mutable m_pan = 0
3171 method getactive = m_active
3172 method getfirst = m_first
3173 method getqsearch = m_qsearch
3174 method getpan = m_pan
3175 method setqsearch s = m_qsearch <- s
3176 end;;
3178 let withoutlastutf8 s =
3179 let len = String.length s in
3180 if len = 0
3181 then s
3182 else
3183 let rec find pos =
3184 if pos = 0
3185 then pos
3186 else
3187 let b = Char.code s.[pos] in
3188 if b land 0b11000000 = 0b11000000
3189 then pos
3190 else find (pos-1)
3192 let first =
3193 if Char.code s.[len-1] land 0x80 = 0
3194 then len-1
3195 else find (len-1)
3197 String.sub s 0 first;
3200 let textentrykeyboard
3201 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3202 let key =
3203 if key >= 0xffb0 && key <= 0xffb9
3204 then key - 0xffb0 + 48 else key
3206 let enttext te =
3207 state.mode <- Textentry (te, onleave);
3208 state.text <- "";
3209 enttext ();
3210 G.postRedisplay "textentrykeyboard enttext";
3212 let histaction cmd =
3213 match opthist with
3214 | None -> ()
3215 | Some (action, _) ->
3216 state.mode <- Textentry (
3217 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3219 G.postRedisplay "textentry histaction"
3221 match key with
3222 | 0xff08 -> (* backspace *)
3223 let s = withoutlastutf8 text in
3224 let len = String.length s in
3225 if cancelonempty && len = 0
3226 then (
3227 onleave Cancel;
3228 G.postRedisplay "textentrykeyboard after cancel";
3230 else (
3231 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3234 | 0xff0d | 0xff8d -> (* (kp) enter *)
3235 ondone text;
3236 onleave Confirm;
3237 G.postRedisplay "textentrykeyboard after confirm"
3239 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3240 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3241 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3242 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3244 | 0xff1b -> (* escape*)
3245 if String.length text = 0
3246 then (
3247 begin match opthist with
3248 | None -> ()
3249 | Some (_, onhistcancel) -> onhistcancel ()
3250 end;
3251 onleave Cancel;
3252 state.text <- "";
3253 G.postRedisplay "textentrykeyboard after cancel2"
3255 else (
3256 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3259 | 0xff9f | 0xffff -> () (* delete *)
3261 | _ when key != 0
3262 && key land 0xff00 != 0xff00 (* keyboard *)
3263 && key land 0xfe00 != 0xfe00 (* xkb *)
3264 && key land 0xfd00 != 0xfd00 (* 3270 *)
3266 begin match onkey text key with
3267 | TEdone text ->
3268 ondone text;
3269 onleave Confirm;
3270 G.postRedisplay "textentrykeyboard after confirm2";
3272 | TEcont text ->
3273 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3275 | TEstop ->
3276 onleave Cancel;
3277 G.postRedisplay "textentrykeyboard after cancel3"
3279 | TEswitch te ->
3280 state.mode <- Textentry (te, onleave);
3281 G.postRedisplay "textentrykeyboard switch";
3282 end;
3284 | _ ->
3285 vlog "unhandled key %s" (Wsi.keyname key)
3288 let firstof first active =
3289 if first > active || abs (first - active) > fstate.maxrows - 1
3290 then max 0 (active - (fstate.maxrows/2))
3291 else first
3294 let calcfirst first active =
3295 if active > first
3296 then
3297 let rows = active - first in
3298 if rows > fstate.maxrows then active - fstate.maxrows else first
3299 else active
3302 let scrollph y maxy =
3303 let sh = float (maxy + state.winh) /. float state.winh in
3304 let sh = float state.winh /. sh in
3305 let sh = max sh (float conf.scrollh) in
3307 let percent = float y /. float maxy in
3308 let position = (float state.winh -. sh) *. percent in
3310 let position =
3311 if position +. sh > float state.winh
3312 then float state.winh -. sh
3313 else position
3315 position, sh;
3318 let coe s = (s :> uioh);;
3320 class listview ~(source:lvsource) ~trusted ~modehash =
3321 object (self)
3322 val m_pan = source#getpan
3323 val m_first = source#getfirst
3324 val m_active = source#getactive
3325 val m_qsearch = source#getqsearch
3326 val m_prev_uioh = state.uioh
3328 method private elemunder y =
3329 let n = y / (fstate.fontsize+1) in
3330 if m_first + n < source#getitemcount
3331 then (
3332 if source#hasaction (m_first + n)
3333 then Some (m_first + n)
3334 else None
3336 else None
3338 method display =
3339 Gl.enable `blend;
3340 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3341 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3342 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3343 GlDraw.color (1., 1., 1.);
3344 Gl.enable `texture_2d;
3345 let fs = fstate.fontsize in
3346 let nfs = fs + 1 in
3347 let ww = fstate.wwidth in
3348 let tabw = 30.0*.ww in
3349 let itemcount = source#getitemcount in
3350 let rec loop row =
3351 if (row - m_first) > fstate.maxrows
3352 then ()
3353 else (
3354 if row >= 0 && row < itemcount
3355 then (
3356 let (s, level) = source#getitem row in
3357 let y = (row - m_first) * nfs in
3358 let x = 5.0 +. float (level + m_pan) *. ww in
3359 if row = m_active
3360 then (
3361 Gl.disable `texture_2d;
3362 GlDraw.polygon_mode `both `line;
3363 let alpha = if source#hasaction row then 0.9 else 0.3 in
3364 GlDraw.color (1., 1., 1.) ~alpha;
3365 GlDraw.rect (1., float (y + 1))
3366 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3367 GlDraw.polygon_mode `both `fill;
3368 GlDraw.color (1., 1., 1.);
3369 Gl.enable `texture_2d;
3372 let drawtabularstring s =
3373 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3374 if trusted
3375 then
3376 let tabpos = try String.index s '\t' with Not_found -> -1 in
3377 if tabpos > 0
3378 then
3379 let len = String.length s - tabpos - 1 in
3380 let s1 = String.sub s 0 tabpos
3381 and s2 = String.sub s (tabpos + 1) len in
3382 let nx = drawstr x s1 in
3383 let sw = nx -. x in
3384 let x = x +. (max tabw sw) in
3385 drawstr x s2
3386 else
3387 drawstr x s
3388 else
3389 drawstr x s
3391 let _ = drawtabularstring s in
3392 loop (row+1)
3396 loop m_first;
3397 Gl.disable `blend;
3398 Gl.disable `texture_2d;
3400 method updownlevel incr =
3401 let len = source#getitemcount in
3402 let curlevel =
3403 if m_active >= 0 && m_active < len
3404 then snd (source#getitem m_active)
3405 else -1
3407 let rec flow i =
3408 if i = len then i-1 else if i = -1 then 0 else
3409 let _, l = source#getitem i in
3410 if l != curlevel then i else flow (i+incr)
3412 let active = flow m_active in
3413 let first = calcfirst m_first active in
3414 G.postRedisplay "outline updownlevel";
3415 {< m_active = active; m_first = first >}
3417 method private key1 key mask =
3418 let set1 active first qsearch =
3419 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3421 let search active pattern incr =
3422 let active = if active = -1 then m_first else active in
3423 let dosearch re =
3424 let rec loop n =
3425 if n >= 0 && n < source#getitemcount
3426 then (
3427 let s, _ = source#getitem n in
3429 (try ignore (Str.search_forward re s 0); true
3430 with Not_found -> false)
3431 then Some n
3432 else loop (n + incr)
3434 else None
3436 loop active
3439 let re = Str.regexp_case_fold pattern in
3440 dosearch re
3441 with Failure s ->
3442 state.text <- s;
3443 None
3445 let itemcount = source#getitemcount in
3446 let find start incr =
3447 let rec find i =
3448 if i = -1 || i = itemcount
3449 then -1
3450 else (
3451 if source#hasaction i
3452 then i
3453 else find (i + incr)
3456 find start
3458 let set active first =
3459 let first = bound first 0 (itemcount - fstate.maxrows) in
3460 state.text <- "";
3461 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3463 let navigate incr =
3464 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3465 let active, first =
3466 let incr1 = if incr > 0 then 1 else -1 in
3467 if isvisible m_first m_active
3468 then
3469 let next =
3470 let next = m_active + incr in
3471 let next =
3472 if next < 0 || next >= itemcount
3473 then -1
3474 else find next incr1
3476 if abs (m_active - next) > fstate.maxrows
3477 then -1
3478 else next
3480 if next = -1
3481 then
3482 let first = m_first + incr in
3483 let first = bound first 0 (itemcount - 1) in
3484 let next =
3485 let next = m_active + incr in
3486 let next = bound next 0 (itemcount - 1) in
3487 find next ~-incr1
3489 let active =
3490 if next = -1
3491 then m_active
3492 else (
3493 if isvisible first next
3494 then next
3495 else m_active
3498 active, first
3499 else
3500 let first = min next m_first in
3501 let first =
3502 if abs (next - first) > fstate.maxrows
3503 then first + incr
3504 else first
3506 next, first
3507 else
3508 let first = m_first + incr in
3509 let first = bound first 0 (itemcount - 1) in
3510 let active =
3511 let next = m_active + incr in
3512 let next = bound next 0 (itemcount - 1) in
3513 let next = find next incr1 in
3514 let active =
3515 if next = -1 || abs (m_active - first) > fstate.maxrows
3516 then (
3517 let active = if m_active = -1 then next else m_active in
3518 active
3520 else next
3522 if isvisible first active
3523 then active
3524 else -1
3526 active, first
3528 G.postRedisplay "listview navigate";
3529 set active first;
3531 match key with
3532 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3533 let incr = if key = 0x72 then -1 else 1 in
3534 let active, first =
3535 match search (m_active + incr) m_qsearch incr with
3536 | None ->
3537 state.text <- m_qsearch ^ " [not found]";
3538 m_active, m_first
3539 | Some active ->
3540 state.text <- m_qsearch;
3541 active, firstof m_first active
3543 G.postRedisplay "listview ctrl-r/s";
3544 set1 active first m_qsearch;
3546 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3547 if m_active >= 0 && m_active < source#getitemcount
3548 then (
3549 let s, _ = source#getitem m_active in
3550 selstring s;
3552 coe self
3554 | 0xff08 -> (* backspace *)
3555 if String.length m_qsearch = 0
3556 then coe self
3557 else (
3558 let qsearch = withoutlastutf8 m_qsearch in
3559 let len = String.length qsearch in
3560 if len = 0
3561 then (
3562 state.text <- "";
3563 G.postRedisplay "listview empty qsearch";
3564 set1 m_active m_first "";
3566 else
3567 let active, first =
3568 match search m_active qsearch ~-1 with
3569 | None ->
3570 state.text <- qsearch ^ " [not found]";
3571 m_active, m_first
3572 | Some active ->
3573 state.text <- qsearch;
3574 active, firstof m_first active
3576 G.postRedisplay "listview backspace qsearch";
3577 set1 active first qsearch
3580 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3581 let pattern = m_qsearch ^ toutf8 key in
3582 let active, first =
3583 match search m_active pattern 1 with
3584 | None ->
3585 state.text <- pattern ^ " [not found]";
3586 m_active, m_first
3587 | Some active ->
3588 state.text <- pattern;
3589 active, firstof m_first active
3591 G.postRedisplay "listview qsearch add";
3592 set1 active first pattern;
3594 | 0xff1b -> (* escape *)
3595 state.text <- "";
3596 if String.length m_qsearch = 0
3597 then (
3598 G.postRedisplay "list view escape";
3599 begin
3600 match
3601 source#exit (coe self) true m_active m_first m_pan m_qsearch
3602 with
3603 | None -> m_prev_uioh
3604 | Some uioh -> uioh
3607 else (
3608 G.postRedisplay "list view kill qsearch";
3609 source#setqsearch "";
3610 coe {< m_qsearch = "" >}
3613 | 0xff0d | 0xff8d -> (* (kp) enter *)
3614 state.text <- "";
3615 let self = {< m_qsearch = "" >} in
3616 source#setqsearch "";
3617 let opt =
3618 G.postRedisplay "listview enter";
3619 if m_active >= 0 && m_active < source#getitemcount
3620 then (
3621 source#exit (coe self) false m_active m_first m_pan "";
3623 else (
3624 source#exit (coe self) true m_active m_first m_pan "";
3627 begin match opt with
3628 | None -> m_prev_uioh
3629 | Some uioh -> uioh
3632 | 0xff9f | 0xffff -> (* (kp) delete *)
3633 coe self
3635 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3636 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3637 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3638 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3640 | 0xff53 | 0xff98 -> (* (kp) right *)
3641 state.text <- "";
3642 G.postRedisplay "listview right";
3643 coe {< m_pan = m_pan - 1 >}
3645 | 0xff51 | 0xff96 -> (* (kp) left *)
3646 state.text <- "";
3647 G.postRedisplay "listview left";
3648 coe {< m_pan = m_pan + 1 >}
3650 | 0xff50 | 0xff95 -> (* (kp) home *)
3651 let active = find 0 1 in
3652 G.postRedisplay "listview home";
3653 set active 0;
3655 | 0xff57 | 0xff9c -> (* (kp) end *)
3656 let first = max 0 (itemcount - fstate.maxrows) in
3657 let active = find (itemcount - 1) ~-1 in
3658 G.postRedisplay "listview end";
3659 set active first;
3661 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3662 coe self
3664 | _ ->
3665 dolog "listview unknown key %#x" key; coe self
3667 method key key mask =
3668 match state.mode with
3669 | Textentry te -> textentrykeyboard key mask te; coe self
3670 | _ -> self#key1 key mask
3672 method button button down x y _ =
3673 let opt =
3674 match button with
3675 | 1 when x > state.winw - conf.scrollbw ->
3676 G.postRedisplay "listview scroll";
3677 if down
3678 then
3679 let _, position, sh = self#scrollph in
3680 if y > truncate position && y < truncate (position +. sh)
3681 then (
3682 state.mstate <- Mscrolly;
3683 Some (coe self)
3685 else
3686 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3687 let first = truncate (s *. float source#getitemcount) in
3688 let first = min source#getitemcount first in
3689 Some (coe {< m_first = first; m_active = first >})
3690 else (
3691 state.mstate <- Mnone;
3692 Some (coe self);
3694 | 1 when not down ->
3695 begin match self#elemunder y with
3696 | Some n ->
3697 G.postRedisplay "listview click";
3698 source#exit
3699 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3700 | _ ->
3701 Some (coe self)
3703 | n when (n == 4 || n == 5) && not down ->
3704 let len = source#getitemcount in
3705 let first =
3706 if n = 5 && m_first + fstate.maxrows >= len
3707 then
3708 m_first
3709 else
3710 let first = m_first + (if n == 4 then -1 else 1) in
3711 bound first 0 (len - 1)
3713 G.postRedisplay "listview wheel";
3714 Some (coe {< m_first = first >})
3715 | n when (n = 6 || n = 7) && not down ->
3716 let inc = m_first + (if n = 7 then -1 else 1) in
3717 G.postRedisplay "listview hwheel";
3718 Some (coe {< m_pan = m_pan + inc >})
3719 | _ ->
3720 Some (coe self)
3722 match opt with
3723 | None -> m_prev_uioh
3724 | Some uioh -> uioh
3726 method motion _ y =
3727 match state.mstate with
3728 | Mscrolly ->
3729 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3730 let first = truncate (s *. float source#getitemcount) in
3731 let first = min source#getitemcount first in
3732 G.postRedisplay "listview motion";
3733 coe {< m_first = first; m_active = first >}
3734 | _ -> coe self
3736 method pmotion x y =
3737 if x < state.winw - conf.scrollbw
3738 then
3739 let n =
3740 match self#elemunder y with
3741 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3742 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3744 let o =
3745 if n != m_active
3746 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3747 else self
3749 coe o
3750 else (
3751 Wsi.setcursor Wsi.CURSOR_INHERIT;
3752 coe self
3755 method infochanged _ = ()
3757 method scrollpw = (0, 0.0, 0.0)
3758 method scrollph =
3759 let nfs = fstate.fontsize + 1 in
3760 let y = m_first * nfs in
3761 let itemcount = source#getitemcount in
3762 let maxi = max 0 (itemcount - fstate.maxrows) in
3763 let maxy = maxi * nfs in
3764 let p, h = scrollph y maxy in
3765 conf.scrollbw, p, h
3767 method modehash = modehash
3768 method eformsgs = false
3769 end;;
3771 class outlinelistview ~source =
3772 object (self)
3773 inherit listview
3774 ~source:(source :> lvsource)
3775 ~trusted:false
3776 ~modehash:(findkeyhash conf "outline")
3777 as super
3779 method key key mask =
3780 let calcfirst first active =
3781 if active > first
3782 then
3783 let rows = active - first in
3784 let maxrows =
3785 if String.length state.text = 0
3786 then fstate.maxrows
3787 else fstate.maxrows - 2
3789 if rows > maxrows then active - maxrows else first
3790 else active
3792 let navigate incr =
3793 let active = m_active + incr in
3794 let active = bound active 0 (source#getitemcount - 1) in
3795 let first = calcfirst m_first active in
3796 G.postRedisplay "outline navigate";
3797 coe {< m_active = active; m_first = first >}
3799 let ctrl = Wsi.withctrl mask in
3800 match key with
3801 | 110 when ctrl -> (* ctrl-n *)
3802 source#narrow m_qsearch;
3803 G.postRedisplay "outline ctrl-n";
3804 coe {< m_first = 0; m_active = 0 >}
3806 | 117 when ctrl -> (* ctrl-u *)
3807 source#denarrow;
3808 G.postRedisplay "outline ctrl-u";
3809 state.text <- "";
3810 coe {< m_first = 0; m_active = 0 >}
3812 | 108 when ctrl -> (* ctrl-l *)
3813 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3814 G.postRedisplay "outline ctrl-l";
3815 coe {< m_first = first >}
3817 | 0xff9f | 0xffff -> (* (kp) delete *)
3818 source#remove m_active;
3819 G.postRedisplay "outline delete";
3820 let active = max 0 (m_active-1) in
3821 coe {< m_first = firstof m_first active;
3822 m_active = active >}
3824 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3825 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3826 | 0xff55 | 0xff9a -> (* (kp) prior *)
3827 navigate ~-(fstate.maxrows)
3828 | 0xff56 | 0xff9b -> (* (kp) next *)
3829 navigate fstate.maxrows
3831 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3832 let o =
3833 if ctrl
3834 then (
3835 G.postRedisplay "outline ctrl right";
3836 {< m_pan = m_pan + 1 >}
3838 else self#updownlevel 1
3840 coe o
3842 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3843 let o =
3844 if ctrl
3845 then (
3846 G.postRedisplay "outline ctrl left";
3847 {< m_pan = m_pan - 1 >}
3849 else self#updownlevel ~-1
3851 coe o
3853 | 0xff50 | 0xff95 -> (* (kp) home *)
3854 G.postRedisplay "outline home";
3855 coe {< m_first = 0; m_active = 0 >}
3857 | 0xff57 | 0xff9c -> (* (kp) end *)
3858 let active = source#getitemcount - 1 in
3859 let first = max 0 (active - fstate.maxrows) in
3860 G.postRedisplay "outline end";
3861 coe {< m_active = active; m_first = first >}
3863 | _ -> super#key key mask
3866 let outlinesource usebookmarks =
3867 let empty = [||] in
3868 (object
3869 inherit lvsourcebase
3870 val mutable m_items = empty
3871 val mutable m_orig_items = empty
3872 val mutable m_prev_items = empty
3873 val mutable m_narrow_pattern = ""
3874 val mutable m_hadremovals = false
3876 method getitemcount =
3877 Array.length m_items + (if m_hadremovals then 1 else 0)
3879 method getitem n =
3880 if n == Array.length m_items && m_hadremovals
3881 then
3882 ("[Confirm removal]", 0)
3883 else
3884 let s, n, _ = m_items.(n) in
3885 (s, n)
3887 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3888 ignore (uioh, first, qsearch);
3889 let confrimremoval = m_hadremovals && active = Array.length m_items in
3890 let items =
3891 if String.length m_narrow_pattern = 0
3892 then m_orig_items
3893 else m_items
3895 if not cancel
3896 then (
3897 if not confrimremoval
3898 then(
3899 let _, _, anchor = m_items.(active) in
3900 gotoghyll (getanchory anchor);
3901 m_items <- items;
3903 else (
3904 state.bookmarks <- Array.to_list m_items;
3905 m_orig_items <- m_items;
3908 else m_items <- items;
3909 m_pan <- pan;
3910 None
3912 method hasaction _ = true
3914 method greetmsg =
3915 if Array.length m_items != Array.length m_orig_items
3916 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3917 else ""
3919 method narrow pattern =
3920 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3921 match reopt with
3922 | None -> ()
3923 | Some re ->
3924 let rec loop accu n =
3925 if n = -1
3926 then (
3927 m_narrow_pattern <- pattern;
3928 m_items <- Array.of_list accu
3930 else
3931 let (s, _, _) as o = m_items.(n) in
3932 let accu =
3933 if (try ignore (Str.search_forward re s 0); true
3934 with Not_found -> false)
3935 then o :: accu
3936 else accu
3938 loop accu (n-1)
3940 loop [] (Array.length m_items - 1)
3942 method denarrow =
3943 m_orig_items <- (
3944 if usebookmarks
3945 then Array.of_list state.bookmarks
3946 else state.outlines
3948 m_items <- m_orig_items
3950 method remove m =
3951 if usebookmarks
3952 then
3953 if m >= 0 && m < Array.length m_items
3954 then (
3955 m_hadremovals <- true;
3956 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3957 let n = if n >= m then n+1 else n in
3958 m_items.(n)
3962 method reset anchor items =
3963 m_hadremovals <- false;
3964 if m_orig_items == empty || m_prev_items != items
3965 then (
3966 m_orig_items <- items;
3967 if String.length m_narrow_pattern = 0
3968 then m_items <- items;
3970 m_prev_items <- items;
3971 let rely = getanchory anchor in
3972 let active =
3973 let rec loop n best bestd =
3974 if n = Array.length m_items
3975 then best
3976 else
3977 let (_, _, anchor) = m_items.(n) in
3978 let orely = getanchory anchor in
3979 let d = abs (orely - rely) in
3980 if d < bestd
3981 then loop (n+1) n d
3982 else loop (n+1) best bestd
3984 loop 0 ~-1 max_int
3986 m_active <- active;
3987 m_first <- firstof m_first active
3988 end)
3991 let enterselector usebookmarks =
3992 let source = outlinesource usebookmarks in
3993 fun errmsg ->
3994 let outlines =
3995 if usebookmarks
3996 then Array.of_list state.bookmarks
3997 else state.outlines
3999 if Array.length outlines = 0
4000 then (
4001 showtext ' ' errmsg;
4003 else (
4004 state.text <- source#greetmsg;
4005 Wsi.setcursor Wsi.CURSOR_INHERIT;
4006 let anchor = getanchor () in
4007 source#reset anchor outlines;
4008 state.uioh <- coe (new outlinelistview ~source);
4009 G.postRedisplay "enter selector";
4013 let enteroutlinemode =
4014 let f = enterselector false in
4015 fun ()-> f "Document has no outline";
4018 let enterbookmarkmode =
4019 let f = enterselector true in
4020 fun () -> f "Document has no bookmarks (yet)";
4023 let color_of_string s =
4024 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4025 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4029 let color_to_string (r, g, b) =
4030 let r = truncate (r *. 256.0)
4031 and g = truncate (g *. 256.0)
4032 and b = truncate (b *. 256.0) in
4033 Printf.sprintf "%d/%d/%d" r g b
4036 let irect_of_string s =
4037 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4040 let irect_to_string (x0,y0,x1,y1) =
4041 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4044 let makecheckers () =
4045 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4046 following to say:
4047 converted by Issac Trotts. July 25, 2002 *)
4048 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4049 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4050 let id = GlTex.gen_texture () in
4051 GlTex.bind_texture `texture_2d id;
4052 GlPix.store (`unpack_alignment 1);
4053 GlTex.image2d image;
4054 List.iter (GlTex.parameter ~target:`texture_2d)
4055 [ `mag_filter `nearest; `min_filter `nearest ];
4059 let setcheckers enabled =
4060 match state.texid with
4061 | None ->
4062 if enabled then state.texid <- Some (makecheckers ())
4064 | Some texid ->
4065 if not enabled
4066 then (
4067 GlTex.delete_texture texid;
4068 state.texid <- None;
4072 let int_of_string_with_suffix s =
4073 let l = String.length s in
4074 let s1, shift =
4075 if l > 1
4076 then
4077 let suffix = Char.lowercase s.[l-1] in
4078 match suffix with
4079 | 'k' -> String.sub s 0 (l-1), 10
4080 | 'm' -> String.sub s 0 (l-1), 20
4081 | 'g' -> String.sub s 0 (l-1), 30
4082 | _ -> s, 0
4083 else s, 0
4085 let n = int_of_string s1 in
4086 let m = n lsl shift in
4087 if m < 0 || m < n
4088 then raise (Failure "value too large")
4089 else m
4092 let string_with_suffix_of_int n =
4093 if n = 0
4094 then "0"
4095 else
4096 let n, s =
4097 if n land ((1 lsl 30) - 1) = 0
4098 then n lsr 30, "G"
4099 else (
4100 if n land ((1 lsl 20) - 1) = 0
4101 then n lsr 20, "M"
4102 else (
4103 if n land ((1 lsl 10) - 1) = 0
4104 then n lsr 10, "K"
4105 else n, ""
4109 let rec loop s n =
4110 let h = n mod 1000 in
4111 let n = n / 1000 in
4112 if n = 0
4113 then string_of_int h ^ s
4114 else (
4115 let s = Printf.sprintf "_%03d%s" h s in
4116 loop s n
4119 loop "" n ^ s;
4122 let defghyllscroll = (40, 8, 32);;
4123 let ghyllscroll_of_string s =
4124 let (n, a, b) as nab =
4125 if s = "default"
4126 then defghyllscroll
4127 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4129 if n <= a || n <= b || a >= b
4130 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4131 nab;
4134 let ghyllscroll_to_string ((n, a, b) as nab) =
4135 if nab = defghyllscroll
4136 then "default"
4137 else Printf.sprintf "%d,%d,%d" n a b;
4140 let describe_location () =
4141 let fn = page_of_y state.y in
4142 let ln = page_of_y (state.y + state.winh - state.hscrollh - 1) in
4143 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4144 let percent =
4145 if maxy <= 0
4146 then 100.
4147 else (100. *. (float state.y /. float maxy))
4149 if fn = ln
4150 then
4151 Printf.sprintf "page %d of %d [%.2f%%]"
4152 (fn+1) state.pagecount percent
4153 else
4154 Printf.sprintf
4155 "pages %d-%d of %d [%.2f%%]"
4156 (fn+1) (ln+1) state.pagecount percent
4159 let setpresentationmode v =
4160 let n = page_of_y state.y in
4161 state.anchor <- (n, 0.0, 1.0);
4162 conf.presentation <- v;
4163 if conf.presentation
4164 then (
4165 if not conf.scrollbarinpm
4166 then state.scrollw <- 0;
4168 else state.scrollw <- conf.scrollbw;
4169 represent ();
4172 let enterinfomode =
4173 let btos b = if b then "\xe2\x88\x9a" else "" in
4174 let showextended = ref false in
4175 let leave mode = function
4176 | Confirm -> state.mode <- mode
4177 | Cancel -> state.mode <- mode in
4178 let src =
4179 (object
4180 val mutable m_first_time = true
4181 val mutable m_l = []
4182 val mutable m_a = [||]
4183 val mutable m_prev_uioh = nouioh
4184 val mutable m_prev_mode = View
4186 inherit lvsourcebase
4188 method reset prev_mode prev_uioh =
4189 m_a <- Array.of_list (List.rev m_l);
4190 m_l <- [];
4191 m_prev_mode <- prev_mode;
4192 m_prev_uioh <- prev_uioh;
4193 if m_first_time
4194 then (
4195 let rec loop n =
4196 if n >= Array.length m_a
4197 then ()
4198 else
4199 match m_a.(n) with
4200 | _, _, _, Action _ -> m_active <- n
4201 | _ -> loop (n+1)
4203 loop 0;
4204 m_first_time <- false;
4207 method int name get set =
4208 m_l <-
4209 (name, `int get, 1, Action (
4210 fun u ->
4211 let ondone s =
4212 try set (int_of_string s)
4213 with exn ->
4214 state.text <- Printf.sprintf "bad integer `%s': %s"
4215 s (exntos exn)
4217 state.text <- "";
4218 let te = name ^ ": ", "", None, intentry, ondone, true in
4219 state.mode <- Textentry (te, leave m_prev_mode);
4221 )) :: m_l
4223 method int_with_suffix name get set =
4224 m_l <-
4225 (name, `intws get, 1, Action (
4226 fun u ->
4227 let ondone s =
4228 try set (int_of_string_with_suffix s)
4229 with exn ->
4230 state.text <- Printf.sprintf "bad integer `%s': %s"
4231 s (exntos exn)
4233 state.text <- "";
4234 let te =
4235 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4237 state.mode <- Textentry (te, leave m_prev_mode);
4239 )) :: m_l
4241 method bool ?(offset=1) ?(btos=btos) name get set =
4242 m_l <-
4243 (name, `bool (btos, get), offset, Action (
4244 fun u ->
4245 let v = get () in
4246 set (not v);
4248 )) :: m_l
4250 method color name get set =
4251 m_l <-
4252 (name, `color get, 1, Action (
4253 fun u ->
4254 let invalid = (nan, nan, nan) in
4255 let ondone s =
4256 let c =
4257 try color_of_string s
4258 with exn ->
4259 state.text <- Printf.sprintf "bad color `%s': %s"
4260 s (exntos exn);
4261 invalid
4263 if c <> invalid
4264 then set c;
4266 let te = name ^ ": ", "", None, textentry, ondone, true in
4267 state.text <- color_to_string (get ());
4268 state.mode <- Textentry (te, leave m_prev_mode);
4270 )) :: m_l
4272 method string name get set =
4273 m_l <-
4274 (name, `string get, 1, Action (
4275 fun u ->
4276 let ondone s = set s in
4277 let te = name ^ ": ", "", None, textentry, ondone, true in
4278 state.mode <- Textentry (te, leave m_prev_mode);
4280 )) :: m_l
4282 method colorspace name get set =
4283 m_l <-
4284 (name, `string get, 1, Action (
4285 fun _ ->
4286 let source =
4287 let vals = [| "rgb"; "bgr"; "gray" |] in
4288 (object
4289 inherit lvsourcebase
4291 initializer
4292 m_active <- int_of_colorspace conf.colorspace;
4293 m_first <- 0;
4295 method getitemcount = Array.length vals
4296 method getitem n = (vals.(n), 0)
4297 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4298 ignore (uioh, first, pan, qsearch);
4299 if not cancel then set active;
4300 None
4301 method hasaction _ = true
4302 end)
4304 state.text <- "";
4305 let modehash = findkeyhash conf "info" in
4306 coe (new listview ~source ~trusted:true ~modehash)
4307 )) :: m_l
4309 method fitmodel name get set =
4310 m_l <-
4311 (name, `string get, 1, Action (
4312 fun _ ->
4313 let source =
4314 let vals = [| "fit width"; "proportional"; "fit page" |] in
4315 (object
4316 inherit lvsourcebase
4318 initializer
4319 m_active <- int_of_fitmodel conf.fitmodel;
4320 m_first <- 0;
4322 method getitemcount = Array.length vals
4323 method getitem n = (vals.(n), 0)
4324 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4325 ignore (uioh, first, pan, qsearch);
4326 if not cancel then set active;
4327 None
4328 method hasaction _ = true
4329 end)
4331 state.text <- "";
4332 let modehash = findkeyhash conf "info" in
4333 coe (new listview ~source ~trusted:true ~modehash)
4334 )) :: m_l
4336 method caption s offset =
4337 m_l <- (s, `empty, offset, Noaction) :: m_l
4339 method caption2 s f offset =
4340 m_l <- (s, `string f, offset, Noaction) :: m_l
4342 method getitemcount = Array.length m_a
4344 method getitem n =
4345 let tostr = function
4346 | `int f -> string_of_int (f ())
4347 | `intws f -> string_with_suffix_of_int (f ())
4348 | `string f -> f ()
4349 | `color f -> color_to_string (f ())
4350 | `bool (btos, f) -> btos (f ())
4351 | `empty -> ""
4353 let name, t, offset, _ = m_a.(n) in
4354 ((let s = tostr t in
4355 if String.length s > 0
4356 then Printf.sprintf "%s\t%s" name s
4357 else name),
4358 offset)
4360 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4361 let uiohopt =
4362 if not cancel
4363 then (
4364 m_qsearch <- qsearch;
4365 let uioh =
4366 match m_a.(active) with
4367 | _, _, _, Action f -> f uioh
4368 | _ -> uioh
4370 Some uioh
4372 else None
4374 m_active <- active;
4375 m_first <- first;
4376 m_pan <- pan;
4377 uiohopt
4379 method hasaction n =
4380 match m_a.(n) with
4381 | _, _, _, Action _ -> true
4382 | _ -> false
4383 end)
4385 let rec fillsrc prevmode prevuioh =
4386 let sep () = src#caption "" 0 in
4387 let colorp name get set =
4388 src#string name
4389 (fun () -> color_to_string (get ()))
4390 (fun v ->
4392 let c = color_of_string v in
4393 set c
4394 with exn ->
4395 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4398 let oldmode = state.mode in
4399 let birdseye = isbirdseye state.mode in
4401 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4403 src#bool "presentation mode"
4404 (fun () -> conf.presentation)
4405 (fun v -> setpresentationmode v);
4407 src#bool "ignore case in searches"
4408 (fun () -> conf.icase)
4409 (fun v -> conf.icase <- v);
4411 src#bool "preload"
4412 (fun () -> conf.preload)
4413 (fun v -> conf.preload <- v);
4415 src#bool "highlight links"
4416 (fun () -> conf.hlinks)
4417 (fun v -> conf.hlinks <- v);
4419 src#bool "under info"
4420 (fun () -> conf.underinfo)
4421 (fun v -> conf.underinfo <- v);
4423 src#bool "persistent bookmarks"
4424 (fun () -> conf.savebmarks)
4425 (fun v -> conf.savebmarks <- v);
4427 src#fitmodel "fit model"
4428 (fun () -> fitmodel_to_string conf.fitmodel)
4429 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4431 src#bool "trim margins"
4432 (fun () -> conf.trimmargins)
4433 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4435 src#bool "persistent location"
4436 (fun () -> conf.jumpback)
4437 (fun v -> conf.jumpback <- v);
4439 sep ();
4440 src#int "inter-page space"
4441 (fun () -> conf.interpagespace)
4442 (fun n ->
4443 conf.interpagespace <- n;
4444 docolumns conf.columns;
4445 let pageno, py =
4446 match state.layout with
4447 | [] -> 0, 0
4448 | l :: _ ->
4449 l.pageno, l.pagey
4451 state.maxy <- calcheight ();
4452 let y = getpagey pageno in
4453 gotoy (y + py)
4456 src#int "page bias"
4457 (fun () -> conf.pagebias)
4458 (fun v -> conf.pagebias <- v);
4460 src#int "scroll step"
4461 (fun () -> conf.scrollstep)
4462 (fun n -> conf.scrollstep <- n);
4464 src#int "horizontal scroll step"
4465 (fun () -> conf.hscrollstep)
4466 (fun v -> conf.hscrollstep <- v);
4468 src#int "auto scroll step"
4469 (fun () ->
4470 match state.autoscroll with
4471 | Some step -> step
4472 | _ -> conf.autoscrollstep)
4473 (fun n ->
4474 if state.autoscroll <> None
4475 then state.autoscroll <- Some n;
4476 conf.autoscrollstep <- n);
4478 src#int "zoom"
4479 (fun () -> truncate (conf.zoom *. 100.))
4480 (fun v -> setzoom ((float v) /. 100.));
4482 src#int "rotation"
4483 (fun () -> conf.angle)
4484 (fun v -> reqlayout v conf.fitmodel);
4486 src#int "scroll bar width"
4487 (fun () -> state.scrollw)
4488 (fun v ->
4489 state.scrollw <- v;
4490 conf.scrollbw <- v;
4491 reshape state.winw state.winh;
4494 src#int "scroll handle height"
4495 (fun () -> conf.scrollh)
4496 (fun v -> conf.scrollh <- v;);
4498 src#int "thumbnail width"
4499 (fun () -> conf.thumbw)
4500 (fun v ->
4501 conf.thumbw <- min 4096 v;
4502 match oldmode with
4503 | Birdseye beye ->
4504 leavebirdseye beye false;
4505 enterbirdseye ()
4506 | _ -> ()
4509 let mode = state.mode in
4510 src#string "columns"
4511 (fun () ->
4512 match conf.columns with
4513 | Csingle _ -> "1"
4514 | Cmulti (multi, _) -> multicolumns_to_string multi
4515 | Csplit (count, _) -> "-" ^ string_of_int count
4517 (fun v ->
4518 let n, a, b = multicolumns_of_string v in
4519 setcolumns mode n a b);
4521 sep ();
4522 src#caption "Presentation mode" 0;
4523 src#bool "scrollbar visible"
4524 (fun () -> conf.scrollbarinpm)
4525 (fun v ->
4526 if v != conf.scrollbarinpm
4527 then (
4528 conf.scrollbarinpm <- v;
4529 if conf.presentation
4530 then (
4531 state.scrollw <- if v then conf.scrollbw else 0;
4532 reshape state.winw state.winh;
4537 sep ();
4538 src#caption "Pixmap cache" 0;
4539 src#int_with_suffix "size (advisory)"
4540 (fun () -> conf.memlimit)
4541 (fun v -> conf.memlimit <- v);
4543 src#caption2 "used"
4544 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4545 (string_with_suffix_of_int state.memused)
4546 (Hashtbl.length state.tilemap)) 1;
4548 sep ();
4549 src#caption "Layout" 0;
4550 src#caption2 "Dimension"
4551 (fun () ->
4552 Printf.sprintf "%dx%d (virtual %dx%d)"
4553 state.winw state.winh
4554 state.w state.maxy)
4556 if conf.debug
4557 then
4558 src#caption2 "Position" (fun () ->
4559 Printf.sprintf "%dx%d" state.x state.y
4561 else
4562 src#caption2 "Position" (fun () -> describe_location ()) 1
4565 sep ();
4566 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4567 "Save these parameters as global defaults at exit"
4568 (fun () -> conf.bedefault)
4569 (fun v -> conf.bedefault <- v)
4572 sep ();
4573 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4574 src#bool ~offset:0 ~btos "Extended parameters"
4575 (fun () -> !showextended)
4576 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4577 if !showextended
4578 then (
4579 src#bool "checkers"
4580 (fun () -> conf.checkers)
4581 (fun v -> conf.checkers <- v; setcheckers v);
4582 src#bool "update cursor"
4583 (fun () -> conf.updatecurs)
4584 (fun v -> conf.updatecurs <- v);
4585 src#bool "verbose"
4586 (fun () -> conf.verbose)
4587 (fun v -> conf.verbose <- v);
4588 src#bool "invert colors"
4589 (fun () -> conf.invert)
4590 (fun v -> conf.invert <- v);
4591 src#bool "max fit"
4592 (fun () -> conf.maxhfit)
4593 (fun v -> conf.maxhfit <- v);
4594 src#bool "redirect stderr"
4595 (fun () -> conf.redirectstderr)
4596 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4597 src#string "uri launcher"
4598 (fun () -> conf.urilauncher)
4599 (fun v -> conf.urilauncher <- v);
4600 src#string "path launcher"
4601 (fun () -> conf.pathlauncher)
4602 (fun v -> conf.pathlauncher <- v);
4603 src#string "tile size"
4604 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4605 (fun v ->
4607 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4608 conf.tilew <- max 64 w;
4609 conf.tileh <- max 64 h;
4610 flushtiles ();
4611 with exn ->
4612 state.text <- Printf.sprintf "bad tile size `%s': %s"
4613 v (exntos exn)
4615 src#int "texture count"
4616 (fun () -> conf.texcount)
4617 (fun v ->
4618 if realloctexts v
4619 then conf.texcount <- v
4620 else showtext '!' " Failed to set texture count please retry later"
4622 src#int "slice height"
4623 (fun () -> conf.sliceheight)
4624 (fun v ->
4625 conf.sliceheight <- v;
4626 wcmd "sliceh %d" conf.sliceheight;
4628 src#int "anti-aliasing level"
4629 (fun () -> conf.aalevel)
4630 (fun v ->
4631 conf.aalevel <- bound v 0 8;
4632 state.anchor <- getanchor ();
4633 opendoc state.path state.password;
4635 src#string "page scroll scaling factor"
4636 (fun () -> string_of_float conf.pgscale)
4637 (fun v ->
4639 let s = float_of_string v in
4640 conf.pgscale <- s
4641 with exn ->
4642 state.text <- Printf.sprintf
4643 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4646 src#int "ui font size"
4647 (fun () -> fstate.fontsize)
4648 (fun v -> setfontsize (bound v 5 100));
4649 src#int "hint font size"
4650 (fun () -> conf.hfsize)
4651 (fun v -> conf.hfsize <- bound v 5 100);
4652 colorp "background color"
4653 (fun () -> conf.bgcolor)
4654 (fun v -> conf.bgcolor <- v);
4655 src#bool "crop hack"
4656 (fun () -> conf.crophack)
4657 (fun v -> conf.crophack <- v);
4658 src#string "trim fuzz"
4659 (fun () -> irect_to_string conf.trimfuzz)
4660 (fun v ->
4662 conf.trimfuzz <- irect_of_string v;
4663 if conf.trimmargins
4664 then settrim true conf.trimfuzz;
4665 with exn ->
4666 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4668 src#string "throttle"
4669 (fun () ->
4670 match conf.maxwait with
4671 | None -> "show place holder if page is not ready"
4672 | Some time ->
4673 if time = infinity
4674 then "wait for page to fully render"
4675 else
4676 "wait " ^ string_of_float time
4677 ^ " seconds before showing placeholder"
4679 (fun v ->
4681 let f = float_of_string v in
4682 if f <= 0.0
4683 then conf.maxwait <- None
4684 else conf.maxwait <- Some f
4685 with exn ->
4686 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4688 src#string "ghyll scroll"
4689 (fun () ->
4690 match conf.ghyllscroll with
4691 | None -> ""
4692 | Some nab -> ghyllscroll_to_string nab
4694 (fun v ->
4696 let gs =
4697 if String.length v = 0
4698 then None
4699 else Some (ghyllscroll_of_string v)
4701 conf.ghyllscroll <- gs
4702 with exn ->
4703 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4705 src#string "selection command"
4706 (fun () -> conf.selcmd)
4707 (fun v -> conf.selcmd <- v);
4708 src#string "synctex command"
4709 (fun () -> conf.stcmd)
4710 (fun v -> conf.stcmd <- v);
4711 src#colorspace "color space"
4712 (fun () -> colorspace_to_string conf.colorspace)
4713 (fun v ->
4714 conf.colorspace <- colorspace_of_int v;
4715 wcmd "cs %d" v;
4716 load state.layout;
4718 if pbousable ()
4719 then
4720 src#bool "use PBO"
4721 (fun () -> conf.usepbo)
4722 (fun v -> conf.usepbo <- v);
4723 src#bool "mouse wheel scrolls pages"
4724 (fun () -> conf.wheelbypage)
4725 (fun v -> conf.wheelbypage <- v);
4728 sep ();
4729 src#caption "Document" 0;
4730 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4731 src#caption2 "Pages"
4732 (fun () -> string_of_int state.pagecount) 1;
4733 src#caption2 "Dimensions"
4734 (fun () -> string_of_int (List.length state.pdims)) 1;
4735 if conf.trimmargins
4736 then (
4737 sep ();
4738 src#caption "Trimmed margins" 0;
4739 src#caption2 "Dimensions"
4740 (fun () -> string_of_int (List.length state.pdims)) 1;
4743 sep ();
4744 src#caption "OpenGL" 0;
4745 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4746 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4748 sep ();
4749 src#caption "Location" 0;
4750 if String.length state.origin > 0
4751 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4752 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4754 src#reset prevmode prevuioh;
4756 fun () ->
4757 state.text <- "";
4758 let prevmode = state.mode
4759 and prevuioh = state.uioh in
4760 fillsrc prevmode prevuioh;
4761 let source = (src :> lvsource) in
4762 let modehash = findkeyhash conf "info" in
4763 state.uioh <- coe (object (self)
4764 inherit listview ~source ~trusted:true ~modehash as super
4765 val mutable m_prevmemused = 0
4766 method infochanged = function
4767 | Memused ->
4768 if m_prevmemused != state.memused
4769 then (
4770 m_prevmemused <- state.memused;
4771 G.postRedisplay "memusedchanged";
4773 | Pdim -> G.postRedisplay "pdimchanged"
4774 | Docinfo -> fillsrc prevmode prevuioh
4776 method key key mask =
4777 if not (Wsi.withctrl mask)
4778 then
4779 match key with
4780 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4781 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4782 | _ -> super#key key mask
4783 else super#key key mask
4784 end);
4785 G.postRedisplay "info";
4788 let enterhelpmode =
4789 let source =
4790 (object
4791 inherit lvsourcebase
4792 method getitemcount = Array.length state.help
4793 method getitem n =
4794 let s, l, _ = state.help.(n) in
4795 (s, l)
4797 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4798 let optuioh =
4799 if not cancel
4800 then (
4801 m_qsearch <- qsearch;
4802 match state.help.(active) with
4803 | _, _, Action f -> Some (f uioh)
4804 | _ -> Some (uioh)
4806 else None
4808 m_active <- active;
4809 m_first <- first;
4810 m_pan <- pan;
4811 optuioh
4813 method hasaction n =
4814 match state.help.(n) with
4815 | _, _, Action _ -> true
4816 | _ -> false
4818 initializer
4819 m_active <- -1
4820 end)
4821 in fun () ->
4822 let modehash = findkeyhash conf "help" in
4823 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4824 G.postRedisplay "help";
4827 let entermsgsmode =
4828 let msgsource =
4829 let re = Str.regexp "[\r\n]" in
4830 (object
4831 inherit lvsourcebase
4832 val mutable m_items = [||]
4834 method getitemcount = 1 + Array.length m_items
4836 method getitem n =
4837 if n = 0
4838 then "[Clear]", 0
4839 else m_items.(n-1), 0
4841 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4842 ignore uioh;
4843 if not cancel
4844 then (
4845 if active = 0
4846 then Buffer.clear state.errmsgs;
4847 m_qsearch <- qsearch;
4849 m_active <- active;
4850 m_first <- first;
4851 m_pan <- pan;
4852 None
4854 method hasaction n =
4855 n = 0
4857 method reset =
4858 state.newerrmsgs <- false;
4859 let l = Str.split re (Buffer.contents state.errmsgs) in
4860 m_items <- Array.of_list l
4862 initializer
4863 m_active <- 0
4864 end)
4865 in fun () ->
4866 state.text <- "";
4867 msgsource#reset;
4868 let source = (msgsource :> lvsource) in
4869 let modehash = findkeyhash conf "listview" in
4870 state.uioh <- coe (object
4871 inherit listview ~source ~trusted:false ~modehash as super
4872 method display =
4873 if state.newerrmsgs
4874 then msgsource#reset;
4875 super#display
4876 end);
4877 G.postRedisplay "msgs";
4880 let quickbookmark ?title () =
4881 match state.layout with
4882 | [] -> ()
4883 | l :: _ ->
4884 let title =
4885 match title with
4886 | None ->
4887 let sec = Unix.gettimeofday () in
4888 let tm = Unix.localtime sec in
4889 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4890 (l.pageno+1)
4891 tm.Unix.tm_mday
4892 tm.Unix.tm_mon
4893 (tm.Unix.tm_year + 1900)
4894 tm.Unix.tm_hour
4895 tm.Unix.tm_min
4896 | Some title -> title
4898 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4901 let setautoscrollspeed step goingdown =
4902 let incr = max 1 ((abs step) / 2) in
4903 let incr = if goingdown then incr else -incr in
4904 let astep = step + incr in
4905 state.autoscroll <- Some astep;
4908 let gotounder = function
4909 | Ulinkgoto (pageno, top) ->
4910 if pageno >= 0
4911 then (
4912 addnav ();
4913 gotopage1 pageno top;
4916 | Ulinkuri s ->
4917 gotouri s
4919 | Uremote (filename, pageno) ->
4920 let path =
4921 if Sys.file_exists filename
4922 then filename
4923 else
4924 let dir = Filename.dirname state.path in
4925 let path = Filename.concat dir filename in
4926 if Sys.file_exists path
4927 then path
4928 else ""
4930 if String.length path > 0
4931 then (
4932 let anchor = getanchor () in
4933 let ranchor = state.path, state.password, anchor, state.origin in
4934 state.origin <- "";
4935 state.anchor <- (pageno, 0.0, 0.0);
4936 state.ranchors <- ranchor :: state.ranchors;
4937 opendoc path "";
4939 else showtext '!' ("Could not find " ^ filename)
4941 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4944 let canpan () =
4945 match conf.columns with
4946 | Csplit _ -> true
4947 | _ -> state.x != 0 || conf.zoom > 1.0
4950 let panbound x = bound x (-state.w) (state.winw - state.scrollw);;
4952 let existsinrow pageno (columns, coverA, coverB) p =
4953 let last = ((pageno - coverA) mod columns) + columns in
4954 let rec any = function
4955 | [] -> false
4956 | l :: rest ->
4957 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4958 then p l
4959 else (
4960 if not (p l)
4961 then (if l.pageno = last then false else any rest)
4962 else true
4965 any state.layout
4968 let nextpage () =
4969 match state.layout with
4970 | [] ->
4971 let pageno = page_of_y state.y in
4972 gotoghyll (getpagey (pageno+1))
4973 | l :: rest ->
4974 match conf.columns with
4975 | Csingle _ ->
4976 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4977 then
4978 let y = clamp (pgscale state.winh) in
4979 gotoghyll y
4980 else
4981 let pageno = min (l.pageno+1) (state.pagecount-1) in
4982 gotoghyll (getpagey pageno)
4983 | Cmulti ((c, _, _) as cl, _) ->
4984 if conf.presentation
4985 && (existsinrow l.pageno cl
4986 (fun l -> l.pageh > l.pagey + l.pagevh))
4987 then
4988 let y = clamp (pgscale state.winh) in
4989 gotoghyll y
4990 else
4991 let pageno = min (l.pageno+c) (state.pagecount-1) in
4992 gotoghyll (getpagey pageno)
4993 | Csplit (n, _) ->
4994 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4995 then
4996 let pagey, pageh = getpageyh l.pageno in
4997 let pagey = pagey + pageh * l.pagecol in
4998 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4999 gotoghyll (pagey + pageh + ips)
5002 let prevpage () =
5003 match state.layout with
5004 | [] ->
5005 let pageno = page_of_y state.y in
5006 gotoghyll (getpagey (pageno-1))
5007 | l :: _ ->
5008 match conf.columns with
5009 | Csingle _ ->
5010 if conf.presentation && l.pagey != 0
5011 then
5012 gotoghyll (clamp (pgscale ~-(state.winh)))
5013 else
5014 let pageno = max 0 (l.pageno-1) in
5015 gotoghyll (getpagey pageno)
5016 | Cmulti ((c, _, coverB) as cl, _) ->
5017 if conf.presentation &&
5018 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5019 then
5020 gotoghyll (clamp (pgscale ~-(state.winh)))
5021 else
5022 let decr =
5023 if l.pageno = state.pagecount - coverB
5024 then 1
5025 else c
5027 let pageno = max 0 (l.pageno-decr) in
5028 gotoghyll (getpagey pageno)
5029 | Csplit (n, _) ->
5030 let y =
5031 if l.pagecol = 0
5032 then
5033 if l.pageno = 0
5034 then l.pagey
5035 else
5036 let pageno = max 0 (l.pageno-1) in
5037 let pagey, pageh = getpageyh pageno in
5038 pagey + (n-1)*pageh
5039 else
5040 let pagey, pageh = getpageyh l.pageno in
5041 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5043 gotoghyll y
5046 let viewkeyboard key mask =
5047 let enttext te =
5048 let mode = state.mode in
5049 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5050 state.text <- "";
5051 enttext ();
5052 G.postRedisplay "view:enttext"
5054 let ctrl = Wsi.withctrl mask in
5055 let key =
5056 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5058 match key with
5059 | 81 -> (* Q *)
5060 exit 0
5062 | 0xff63 -> (* insert *)
5063 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5064 then (
5065 state.mode <- LinkNav (Ltgendir 0);
5066 gotoy state.y;
5068 else showtext '!' "Keyboard link navigation does not work under rotation"
5070 | 0xff1b | 113 -> (* escape / q *)
5071 begin match state.mstate with
5072 | Mzoomrect _ ->
5073 state.mstate <- Mnone;
5074 Wsi.setcursor Wsi.CURSOR_INHERIT;
5075 G.postRedisplay "kill zoom rect";
5076 | _ ->
5077 begin match state.mode with
5078 | LinkNav _ ->
5079 state.mode <- View;
5080 G.postRedisplay "esc leave linknav"
5081 | _ ->
5082 match state.ranchors with
5083 | [] -> raise Quit
5084 | (path, password, anchor, origin) :: rest ->
5085 state.ranchors <- rest;
5086 state.anchor <- anchor;
5087 state.origin <- origin;
5088 opendoc path password
5089 end;
5090 end;
5092 | 0xff08 -> (* backspace *)
5093 gotoghyll (getnav ~-1)
5095 | 111 -> (* o *)
5096 enteroutlinemode ()
5098 | 117 -> (* u *)
5099 state.rects <- [];
5100 state.text <- "";
5101 G.postRedisplay "dehighlight";
5103 | 47 | 63 -> (* / ? *)
5104 let ondone isforw s =
5105 cbput state.hists.pat s;
5106 state.searchpattern <- s;
5107 search s isforw
5109 let s = String.create 1 in
5110 s.[0] <- Char.chr key;
5111 enttext (s, "", Some (onhist state.hists.pat),
5112 textentry, ondone (key = 47), true)
5114 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5115 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5116 setzoom (conf.zoom +. incr)
5118 | 43 | 0xffab -> (* + *)
5119 let ondone s =
5120 let n =
5121 try int_of_string s with exc ->
5122 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5123 max_int
5125 if n != max_int
5126 then (
5127 conf.pagebias <- n;
5128 state.text <- "page bias is now " ^ string_of_int n;
5131 enttext ("page bias: ", "", None, intentry, ondone, true)
5133 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5134 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5135 setzoom (max 0.01 (conf.zoom -. decr))
5137 | 45 | 0xffad -> (* - *)
5138 let ondone msg = state.text <- msg in
5139 enttext (
5140 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5141 optentry state.mode, ondone, true
5144 | 48 when ctrl -> (* ctrl-0 *)
5145 if conf.zoom = 1.0
5146 then (
5147 state.x <- 0;
5148 state.hscrollh <-
5149 if state.w <= state.winw - state.scrollw
5150 then 0
5151 else state.scrollw
5153 gotoy state.y
5155 else setzoom 1.0
5157 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5158 let cols =
5159 match conf.columns with
5160 | Csingle _ | Cmulti _ -> 1
5161 | Csplit (n, _) -> n
5163 let h = state.winh -
5164 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5166 let zoom = zoomforh state.winw h state.scrollw cols in
5167 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5168 then setzoom zoom
5170 | 51 when ctrl -> (* ctrl-3 *)
5171 let fm =
5172 match conf.fitmodel with
5173 | FitWidth -> FitProportional
5174 | FitProportional -> FitPage
5175 | FitPage -> FitWidth
5177 state.text <- "fit model: " ^ fitmodel_to_string fm;
5178 reqlayout conf.angle fm
5180 | 0xffc6 -> (* f9 *)
5181 togglebirdseye ()
5183 | 57 when ctrl -> (* ctrl-9 *)
5184 togglebirdseye ()
5186 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5187 when not ctrl -> (* 0..9 *)
5188 let ondone s =
5189 let n =
5190 try int_of_string s with exc ->
5191 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5194 if n >= 0
5195 then (
5196 addnav ();
5197 cbput state.hists.pag (string_of_int n);
5198 gotopage1 (n + conf.pagebias - 1) 0;
5201 let pageentry text key =
5202 match Char.unsafe_chr key with
5203 | 'g' -> TEdone text
5204 | _ -> intentry text key
5206 let text = "x" in text.[0] <- Char.chr key;
5207 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5209 | 98 -> (* b *)
5210 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5211 reshape state.winw state.winh;
5213 | 108 -> (* l *)
5214 conf.hlinks <- not conf.hlinks;
5215 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5216 G.postRedisplay "toggle highlightlinks";
5218 | 70 -> (* F *)
5219 state.glinks <- true;
5220 let mode = state.mode in
5221 state.mode <- Textentry (
5222 (":", "", None, linknentry, linkndone gotounder, false),
5223 (fun _ ->
5224 state.glinks <- false;
5225 state.mode <- mode)
5227 state.text <- "";
5228 G.postRedisplay "view:linkent(F)"
5230 | 121 -> (* y *)
5231 state.glinks <- true;
5232 let mode = state.mode in
5233 state.mode <- Textentry (
5235 ":", "", None, linknentry, linkndone (fun under ->
5236 selstring (undertext under);
5237 ), false
5239 fun _ ->
5240 state.glinks <- false;
5241 state.mode <- mode
5243 state.text <- "";
5244 G.postRedisplay "view:linkent"
5246 | 97 -> (* a *)
5247 begin match state.autoscroll with
5248 | Some step ->
5249 conf.autoscrollstep <- step;
5250 state.autoscroll <- None
5251 | None ->
5252 if conf.autoscrollstep = 0
5253 then state.autoscroll <- Some 1
5254 else state.autoscroll <- Some conf.autoscrollstep
5257 | 112 when ctrl -> (* ctrl-p *)
5258 launchpath ()
5260 | 80 -> (* P *)
5261 setpresentationmode (not conf.presentation);
5262 showtext ' ' ("presentation mode " ^
5263 if conf.presentation then "on" else "off");
5265 | 102 -> (* f *)
5266 if List.mem Wsi.Fullscreen state.winstate
5267 then Wsi.reshape conf.cwinw conf.cwinh
5268 else Wsi.fullscreen ()
5270 | 112 | 78 -> (* p|N *)
5271 search state.searchpattern false
5273 | 110 | 0xffc0 -> (* n|F3 *)
5274 search state.searchpattern true
5276 | 116 -> (* t *)
5277 begin match state.layout with
5278 | [] -> ()
5279 | l :: _ ->
5280 gotoghyll (getpagey l.pageno)
5283 | 32 -> (* space *)
5284 nextpage ()
5286 | 0xff9f | 0xffff -> (* delete *)
5287 prevpage ()
5289 | 61 -> (* = *)
5290 showtext ' ' (describe_location ());
5292 | 119 -> (* w *)
5293 begin match state.layout with
5294 | [] -> ()
5295 | l :: _ ->
5296 Wsi.reshape (l.pagew + state.scrollw) l.pageh;
5297 G.postRedisplay "w"
5300 | 39 -> (* ' *)
5301 enterbookmarkmode ()
5303 | 104 | 0xffbe -> (* h|F1 *)
5304 enterhelpmode ()
5306 | 105 -> (* i *)
5307 enterinfomode ()
5309 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5310 entermsgsmode ()
5312 | 109 -> (* m *)
5313 let ondone s =
5314 match state.layout with
5315 | l :: _ ->
5316 if String.length s > 0
5317 then
5318 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5319 | _ -> ()
5321 enttext ("bookmark: ", "", None, textentry, ondone, true)
5323 | 126 -> (* ~ *)
5324 quickbookmark ();
5325 showtext ' ' "Quick bookmark added";
5327 | 122 -> (* z *)
5328 begin match state.layout with
5329 | l :: _ ->
5330 let rect = getpdimrect l.pagedimno in
5331 let w, h =
5332 if conf.crophack
5333 then
5334 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5335 truncate (1.2 *. (rect.(3) -. rect.(0))))
5336 else
5337 (truncate (rect.(1) -. rect.(0)),
5338 truncate (rect.(3) -. rect.(0)))
5340 let w = truncate ((float w)*.conf.zoom)
5341 and h = truncate ((float h)*.conf.zoom) in
5342 if w != 0 && h != 0
5343 then (
5344 state.anchor <- getanchor ();
5345 Wsi.reshape (w + state.scrollw) (h + conf.interpagespace)
5347 G.postRedisplay "z";
5349 | [] -> ()
5352 | 60 | 62 -> (* < > *)
5353 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5355 | 91 | 93 -> (* [ ] *)
5356 conf.colorscale <-
5357 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5359 G.postRedisplay "brightness";
5361 | 99 when state.mode = View -> (* c *)
5362 let (c, a, b), z =
5363 match state.prevcolumns with
5364 | None -> (1, 0, 0), 1.0
5365 | Some (columns, z) ->
5366 let cab =
5367 match columns with
5368 | Csplit (c, _) -> -c, 0, 0
5369 | Cmulti ((c, a, b), _) -> c, a, b
5370 | Csingle _ -> 1, 0, 0
5372 cab, z
5374 setcolumns View c a b;
5375 setzoom z;
5377 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5378 setzoom state.prevzoom
5380 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5381 begin match state.autoscroll with
5382 | None ->
5383 begin match state.mode with
5384 | Birdseye beye -> upbirdseye 1 beye
5385 | _ ->
5386 if ctrl
5387 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5388 else (
5389 if not (Wsi.withshift mask) && conf.presentation
5390 then prevpage ()
5391 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5394 | Some n ->
5395 setautoscrollspeed n false
5398 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5399 begin match state.autoscroll with
5400 | None ->
5401 begin match state.mode with
5402 | Birdseye beye -> downbirdseye 1 beye
5403 | _ ->
5404 if ctrl
5405 then gotoy_and_clear_text (clamp (state.winh/2))
5406 else (
5407 if not (Wsi.withshift mask) && conf.presentation
5408 then nextpage ()
5409 else gotoy_and_clear_text (clamp conf.scrollstep)
5412 | Some n ->
5413 setautoscrollspeed n true
5416 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5417 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5418 if canpan ()
5419 then
5420 let dx =
5421 if ctrl
5422 then state.winw / 2
5423 else conf.hscrollstep
5425 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5426 state.x <- panbound (state.x + dx);
5427 gotoy_and_clear_text state.y
5428 else (
5429 state.text <- "";
5430 G.postRedisplay "left/right"
5433 | 0xff55 | 0xff9a -> (* (kp) prior *)
5434 let y =
5435 if ctrl
5436 then
5437 match state.layout with
5438 | [] -> state.y
5439 | l :: _ -> state.y - l.pagey
5440 else
5441 clamp (pgscale (-state.winh))
5443 gotoghyll y
5445 | 0xff56 | 0xff9b -> (* (kp) next *)
5446 let y =
5447 if ctrl
5448 then
5449 match List.rev state.layout with
5450 | [] -> state.y
5451 | l :: _ -> getpagey l.pageno
5452 else
5453 clamp (pgscale state.winh)
5455 gotoghyll y
5457 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5458 gotoghyll 0
5459 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5460 gotoghyll (clamp state.maxy)
5462 | 0xff53 | 0xff98
5463 when Wsi.withalt mask -> (* alt-(kp) right *)
5464 gotoghyll (getnav 1)
5465 | 0xff51 | 0xff96
5466 when Wsi.withalt mask -> (* alt-(kp) left *)
5467 gotoghyll (getnav ~-1)
5469 | 114 -> (* r *)
5470 reload ()
5472 | 118 when conf.debug -> (* v *)
5473 state.rects <- [];
5474 List.iter (fun l ->
5475 match getopaque l.pageno with
5476 | None -> ()
5477 | Some opaque ->
5478 let x0, y0, x1, y1 = pagebbox opaque in
5479 let a,b = float x0, float y0 in
5480 let c,d = float x1, float y0 in
5481 let e,f = float x1, float y1 in
5482 let h,j = float x0, float y1 in
5483 let rect = (a,b,c,d,e,f,h,j) in
5484 debugrect rect;
5485 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5486 ) state.layout;
5487 G.postRedisplay "v";
5489 | _ ->
5490 vlog "huh? %s" (Wsi.keyname key)
5493 let linknavkeyboard key mask linknav =
5494 let getpage pageno =
5495 let rec loop = function
5496 | [] -> None
5497 | l :: _ when l.pageno = pageno -> Some l
5498 | _ :: rest -> loop rest
5499 in loop state.layout
5501 let doexact (pageno, n) =
5502 match getopaque pageno, getpage pageno with
5503 | Some opaque, Some l ->
5504 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5505 then
5506 let under = getlink opaque n in
5507 G.postRedisplay "link gotounder";
5508 gotounder under;
5509 state.mode <- View;
5510 else
5511 let opt, dir =
5512 match key with
5513 | 0xff50 -> (* home *)
5514 Some (findlink opaque LDfirst), -1
5516 | 0xff57 -> (* end *)
5517 Some (findlink opaque LDlast), 1
5519 | 0xff51 -> (* left *)
5520 Some (findlink opaque (LDleft n)), -1
5522 | 0xff53 -> (* right *)
5523 Some (findlink opaque (LDright n)), 1
5525 | 0xff52 -> (* up *)
5526 Some (findlink opaque (LDup n)), -1
5528 | 0xff54 -> (* down *)
5529 Some (findlink opaque (LDdown n)), 1
5531 | _ -> None, 0
5533 let pwl l dir =
5534 begin match findpwl l.pageno dir with
5535 | Pwlnotfound -> ()
5536 | Pwl pageno ->
5537 let notfound dir =
5538 state.mode <- LinkNav (Ltgendir dir);
5539 let y, h = getpageyh pageno in
5540 let y =
5541 if dir < 0
5542 then y + h - state.winh
5543 else y
5545 gotoy y
5547 begin match getopaque pageno, getpage pageno with
5548 | Some opaque, Some _ ->
5549 let link =
5550 let ld = if dir > 0 then LDfirst else LDlast in
5551 findlink opaque ld
5553 begin match link with
5554 | Lfound m ->
5555 showlinktype (getlink opaque m);
5556 state.mode <- LinkNav (Ltexact (pageno, m));
5557 G.postRedisplay "linknav jpage";
5558 | _ -> notfound dir
5559 end;
5560 | _ -> notfound dir
5561 end;
5562 end;
5564 begin match opt with
5565 | Some Lnotfound -> pwl l dir;
5566 | Some (Lfound m) ->
5567 if m = n
5568 then pwl l dir
5569 else (
5570 let _, y0, _, y1 = getlinkrect opaque m in
5571 if y0 < l.pagey
5572 then gotopage1 l.pageno y0
5573 else (
5574 let d = fstate.fontsize + 1 in
5575 if y1 - l.pagey > l.pagevh - d
5576 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5577 else G.postRedisplay "linknav";
5579 showlinktype (getlink opaque m);
5580 state.mode <- LinkNav (Ltexact (l.pageno, m));
5583 | None -> viewkeyboard key mask
5584 end;
5585 | _ -> viewkeyboard key mask
5587 if key = 0xff63
5588 then (
5589 state.mode <- View;
5590 G.postRedisplay "leave linknav"
5592 else
5593 match linknav with
5594 | Ltgendir _ -> viewkeyboard key mask
5595 | Ltexact exact -> doexact exact
5598 let keyboard key mask =
5599 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5600 then wcmd "interrupt"
5601 else state.uioh <- state.uioh#key key mask
5604 let birdseyekeyboard key mask
5605 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5606 let incr =
5607 match conf.columns with
5608 | Csingle _ -> 1
5609 | Cmulti ((c, _, _), _) -> c
5610 | Csplit _ -> failwith "bird's eye split mode"
5612 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5613 match key with
5614 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5615 let y, h = getpageyh pageno in
5616 let top = (state.winh - h) / 2 in
5617 gotoy (max 0 (y - top))
5618 | 0xff0d (* enter *)
5619 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5620 | 0xff1b -> leavebirdseye beye true (* escape *)
5621 | 0xff52 -> upbirdseye incr beye (* up *)
5622 | 0xff54 -> downbirdseye incr beye (* down *)
5623 | 0xff51 -> upbirdseye 1 beye (* left *)
5624 | 0xff53 -> downbirdseye 1 beye (* right *)
5626 | 0xff55 -> (* prior *)
5627 begin match state.layout with
5628 | l :: _ ->
5629 if l.pagey != 0
5630 then (
5631 state.mode <- Birdseye (
5632 oconf, leftx, l.pageno, hooverpageno, anchor
5634 gotopage1 l.pageno 0;
5636 else (
5637 let layout = layout (state.y-state.winh) (pgh state.layout) in
5638 match layout with
5639 | [] -> gotoy (clamp (-state.winh))
5640 | l :: _ ->
5641 state.mode <- Birdseye (
5642 oconf, leftx, l.pageno, hooverpageno, anchor
5644 gotopage1 l.pageno 0
5647 | [] -> gotoy (clamp (-state.winh))
5648 end;
5650 | 0xff56 -> (* next *)
5651 begin match List.rev state.layout with
5652 | l :: _ ->
5653 let layout = layout (state.y + (pgh state.layout)) state.winh in
5654 begin match layout with
5655 | [] ->
5656 let incr = l.pageh - l.pagevh in
5657 if incr = 0
5658 then (
5659 state.mode <-
5660 Birdseye (
5661 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5663 G.postRedisplay "birdseye pagedown";
5665 else gotoy (clamp (incr + conf.interpagespace*2));
5667 | l :: _ ->
5668 state.mode <-
5669 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5670 gotopage1 l.pageno 0;
5673 | [] -> gotoy (clamp state.winh)
5674 end;
5676 | 0xff50 -> (* home *)
5677 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5678 gotopage1 0 0
5680 | 0xff57 -> (* end *)
5681 let pageno = state.pagecount - 1 in
5682 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5683 if not (pagevisible state.layout pageno)
5684 then
5685 let h =
5686 match List.rev state.pdims with
5687 | [] -> state.winh
5688 | (_, _, h, _) :: _ -> h
5690 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5691 else G.postRedisplay "birdseye end";
5692 | _ -> viewkeyboard key mask
5695 let drawpage l =
5696 let color =
5697 match state.mode with
5698 | Textentry _ -> scalecolor 0.4
5699 | LinkNav _
5700 | View -> scalecolor 1.0
5701 | Birdseye (_, _, pageno, hooverpageno, _) ->
5702 if l.pageno = hooverpageno
5703 then scalecolor 0.9
5704 else (
5705 if l.pageno = pageno
5706 then scalecolor 1.0
5707 else scalecolor 0.8
5710 drawtiles l color;
5713 let postdrawpage l linkindexbase =
5714 match getopaque l.pageno with
5715 | Some opaque ->
5716 if tileready l l.pagex l.pagey
5717 then
5718 let x = l.pagedispx - l.pagex
5719 and y = l.pagedispy - l.pagey in
5720 let hlmask =
5721 match conf.columns with
5722 | Csingle _ | Cmulti _ ->
5723 (if conf.hlinks then 1 else 0)
5724 + (if state.glinks
5725 && not (isbirdseye state.mode) then 2 else 0)
5726 | _ -> 0
5728 let s =
5729 match state.mode with
5730 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5731 | _ -> ""
5733 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5734 else 0
5735 | _ -> 0
5738 let scrollindicator () =
5739 let sbw, ph, sh = state.uioh#scrollph in
5740 let sbh, pw, sw = state.uioh#scrollpw in
5742 GlDraw.color (0.64, 0.64, 0.64);
5743 GlDraw.rect
5744 (float (state.winw - sbw), 0.)
5745 (float state.winw, float state.winh)
5747 GlDraw.rect
5748 (0., float (state.winh - sbh))
5749 (float (state.winw - state.scrollw - 1), float state.winh)
5751 GlDraw.color (0.0, 0.0, 0.0);
5753 GlDraw.rect
5754 (float (state.winw - sbw), ph)
5755 (float state.winw, ph +. sh)
5757 GlDraw.rect
5758 (pw, float (state.winh - sbh))
5759 (pw +. sw, float state.winh)
5763 let showsel () =
5764 match state.mstate with
5765 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5768 | Msel ((x0, y0), (x1, y1)) ->
5769 let rec loop = function
5770 | l :: ls ->
5771 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5772 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5773 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5774 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5775 then
5776 match getopaque l.pageno with
5777 | Some opaque ->
5778 let x0, y0 = pagetranslatepoint l x0 y0 in
5779 let x1, y1 = pagetranslatepoint l x1 y1 in
5780 seltext opaque (x0, y0, x1, y1);
5781 | _ -> ()
5782 else loop ls
5783 | [] -> ()
5785 loop state.layout
5788 let showrects rects =
5789 Gl.enable `blend;
5790 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5791 GlDraw.polygon_mode `both `fill;
5792 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5793 List.iter
5794 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5795 List.iter (fun l ->
5796 if l.pageno = pageno
5797 then (
5798 let dx = float (l.pagedispx - l.pagex) in
5799 let dy = float (l.pagedispy - l.pagey) in
5800 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5801 GlDraw.begins `quads;
5803 GlDraw.vertex2 (x0+.dx, y0+.dy);
5804 GlDraw.vertex2 (x1+.dx, y1+.dy);
5805 GlDraw.vertex2 (x2+.dx, y2+.dy);
5806 GlDraw.vertex2 (x3+.dx, y3+.dy);
5808 GlDraw.ends ();
5810 ) state.layout
5811 ) rects
5813 Gl.disable `blend;
5816 let display () =
5817 GlClear.color (scalecolor2 conf.bgcolor);
5818 GlClear.clear [`color];
5819 List.iter drawpage state.layout;
5820 let rects =
5821 match state.mode with
5822 | LinkNav (Ltexact (pageno, linkno)) ->
5823 begin match getopaque pageno with
5824 | Some opaque ->
5825 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5826 (pageno, 5, (
5827 float x0, float y0,
5828 float x1, float y0,
5829 float x1, float y1,
5830 float x0, float y1)
5831 ) :: state.rects
5832 | None -> state.rects
5834 | _ -> state.rects
5836 showrects rects;
5837 let rec postloop linkindexbase = function
5838 | l :: rest ->
5839 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5840 postloop linkindexbase rest
5841 | [] -> ()
5843 showsel ();
5844 postloop 0 state.layout;
5845 state.uioh#display;
5846 begin match state.mstate with
5847 | Mzoomrect ((x0, y0), (x1, y1)) ->
5848 Gl.enable `blend;
5849 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5850 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5851 GlDraw.rect (float x0, float y0)
5852 (float x1, float y1);
5853 Gl.disable `blend;
5854 | _ -> ()
5855 end;
5856 enttext ();
5857 scrollindicator ();
5858 Wsi.swapb ();
5861 let zoomrect x y x1 y1 =
5862 let x0 = min x x1
5863 and x1 = max x x1
5864 and y0 = min y y1 in
5865 gotoy (state.y + y0);
5866 state.anchor <- getanchor ();
5867 let zoom = (float state.w) /. float (x1 - x0) in
5868 let margin =
5869 if state.w < state.winw - state.scrollw
5870 then (state.winw - state.scrollw - state.w) / 2
5871 else 0
5873 state.x <- (state.x + margin) - x0;
5874 setzoom zoom;
5875 Wsi.setcursor Wsi.CURSOR_INHERIT;
5876 state.mstate <- Mnone;
5879 let scrollx x =
5880 let winw = state.winw - state.scrollw - 1 in
5881 let s = float x /. float winw in
5882 let destx = truncate (float (state.w + winw) *. s) in
5883 state.x <- winw - destx;
5884 gotoy_and_clear_text state.y;
5885 state.mstate <- Mscrollx;
5888 let scrolly y =
5889 let s = float y /. float state.winh in
5890 let desty = truncate (float (state.maxy - state.winh) *. s) in
5891 gotoy_and_clear_text desty;
5892 state.mstate <- Mscrolly;
5895 let viewmouse button down x y mask =
5896 match button with
5897 | n when (n == 4 || n == 5) && not down ->
5898 if Wsi.withctrl mask
5899 then (
5900 match state.mstate with
5901 | Mzoom (oldn, i) ->
5902 if oldn = n
5903 then (
5904 if i = 2
5905 then
5906 let incr =
5907 match n with
5908 | 5 ->
5909 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5910 | _ ->
5911 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5913 let zoom = conf.zoom -. incr in
5914 setzoom zoom;
5915 state.mstate <- Mzoom (n, 0);
5916 else
5917 state.mstate <- Mzoom (n, i+1);
5919 else state.mstate <- Mzoom (n, 0)
5921 | _ -> state.mstate <- Mzoom (n, 0)
5923 else (
5924 match state.autoscroll with
5925 | Some step -> setautoscrollspeed step (n=4)
5926 | None ->
5927 if conf.wheelbypage || conf.presentation
5928 then (
5929 if n = 4
5930 then prevpage ()
5931 else nextpage ()
5933 else
5934 let incr =
5935 if n = 4
5936 then -conf.scrollstep
5937 else conf.scrollstep
5939 let incr = incr * 2 in
5940 let y = clamp incr in
5941 gotoy_and_clear_text y
5944 | n when (n = 6 || n = 7) && not down && canpan () ->
5945 state.x <-
5946 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5947 gotoy_and_clear_text state.y
5949 | 1 when Wsi.withshift mask ->
5950 state.mstate <- Mnone;
5951 if not down
5952 then (
5953 match unproject x y with
5954 | Some (pageno, ux, uy) ->
5955 let cmd = Printf.sprintf
5956 "%s %s %d %d %d"
5957 conf.stcmd state.path pageno ux uy
5959 popen cmd []
5960 | None -> ()
5963 | 1 when Wsi.withctrl mask ->
5964 if down
5965 then (
5966 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5967 state.mstate <- Mpan (x, y)
5969 else
5970 state.mstate <- Mnone
5972 | 3 ->
5973 if down
5974 then (
5975 Wsi.setcursor Wsi.CURSOR_CYCLE;
5976 let p = (x, y) in
5977 state.mstate <- Mzoomrect (p, p)
5979 else (
5980 match state.mstate with
5981 | Mzoomrect ((x0, y0), _) ->
5982 if abs (x-x0) > 10 && abs (y - y0) > 10
5983 then zoomrect x0 y0 x y
5984 else (
5985 state.mstate <- Mnone;
5986 Wsi.setcursor Wsi.CURSOR_INHERIT;
5987 G.postRedisplay "kill accidental zoom rect";
5989 | _ ->
5990 Wsi.setcursor Wsi.CURSOR_INHERIT;
5991 state.mstate <- Mnone
5994 | 1 when x > state.winw - state.scrollw ->
5995 if down
5996 then
5997 let _, position, sh = state.uioh#scrollph in
5998 if y > truncate position && y < truncate (position +. sh)
5999 then state.mstate <- Mscrolly
6000 else scrolly y
6001 else
6002 state.mstate <- Mnone
6004 | 1 when y > state.winh - state.hscrollh ->
6005 if down
6006 then
6007 let _, position, sw = state.uioh#scrollpw in
6008 if x > truncate position && x < truncate (position +. sw)
6009 then state.mstate <- Mscrollx
6010 else scrollx x
6011 else
6012 state.mstate <- Mnone
6014 | 1 ->
6015 let dest = if down then getunder x y else Unone in
6016 begin match dest with
6017 | Ulinkgoto _
6018 | Ulinkuri _
6019 | Uremote _
6020 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6021 gotounder dest
6023 | Unone when down ->
6024 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6025 state.mstate <- Mpan (x, y);
6027 | Unone | Utext _ ->
6028 if down
6029 then (
6030 if conf.angle mod 360 = 0
6031 then (
6032 state.mstate <- Msel ((x, y), (x, y));
6033 G.postRedisplay "mouse select";
6036 else (
6037 match state.mstate with
6038 | Mnone -> ()
6040 | Mzoom _ | Mscrollx | Mscrolly ->
6041 state.mstate <- Mnone
6043 | Mzoomrect ((x0, y0), _) ->
6044 zoomrect x0 y0 x y
6046 | Mpan _ ->
6047 Wsi.setcursor Wsi.CURSOR_INHERIT;
6048 state.mstate <- Mnone
6050 | Msel ((x0, y0), (x1, y1)) ->
6051 let rec loop = function
6052 | [] -> ()
6053 | l :: rest ->
6054 let inside =
6055 let a0 = l.pagedispy in
6056 let a1 = a0 + l.pagevh in
6057 let b0 = l.pagedispx in
6058 let b1 = b0 + l.pagevw in
6059 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6060 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6062 if inside
6063 then
6064 match getopaque l.pageno with
6065 | Some opaque ->
6066 begin
6067 match Ne.pipe () with
6068 | Ne.Exn exn ->
6069 showtext '!'
6070 (Printf.sprintf
6071 "can not create sel pipe: %s"
6072 (exntos exn));
6073 | Ne.Res (r, w) ->
6074 let doclose what fd =
6075 Ne.clo fd (fun msg ->
6076 dolog "%s close failed: %s" what msg)
6079 popen conf.selcmd [r, 0; w, -1];
6080 copysel w opaque;
6081 doclose "pipe/r" r;
6082 G.postRedisplay "copysel";
6083 with exn ->
6084 dolog "can not execute %S: %s"
6085 conf.selcmd (exntos exn);
6086 doclose "pipe/r" r;
6087 doclose "pipe/w" w;
6089 | None -> ()
6090 else loop rest
6092 loop state.layout;
6093 Wsi.setcursor Wsi.CURSOR_INHERIT;
6094 state.mstate <- Mnone;
6098 | _ -> ()
6101 let birdseyemouse button down x y mask
6102 (conf, leftx, _, hooverpageno, anchor) =
6103 match button with
6104 | 1 when down ->
6105 let rec loop = function
6106 | [] -> ()
6107 | l :: rest ->
6108 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6109 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6110 then (
6111 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6113 else loop rest
6115 loop state.layout
6116 | 3 -> ()
6117 | _ -> viewmouse button down x y mask
6120 let mouse button down x y mask =
6121 state.uioh <- state.uioh#button button down x y mask;
6124 let motion ~x ~y =
6125 state.uioh <- state.uioh#motion x y
6128 let pmotion ~x ~y =
6129 state.uioh <- state.uioh#pmotion x y;
6132 let uioh = object
6133 method display = ()
6135 method key key mask =
6136 begin match state.mode with
6137 | Textentry textentry -> textentrykeyboard key mask textentry
6138 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6139 | View -> viewkeyboard key mask
6140 | LinkNav linknav -> linknavkeyboard key mask linknav
6141 end;
6142 state.uioh
6144 method button button bstate x y mask =
6145 begin match state.mode with
6146 | LinkNav _
6147 | View -> viewmouse button bstate x y mask
6148 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6149 | Textentry _ -> ()
6150 end;
6151 state.uioh
6153 method motion x y =
6154 begin match state.mode with
6155 | Textentry _ -> ()
6156 | View | Birdseye _ | LinkNav _ ->
6157 match state.mstate with
6158 | Mzoom _ | Mnone -> ()
6160 | Mpan (x0, y0) ->
6161 let dx = x - x0
6162 and dy = y0 - y in
6163 state.mstate <- Mpan (x, y);
6164 if canpan ()
6165 then state.x <- panbound (state.x + dx);
6166 let y = clamp dy in
6167 gotoy_and_clear_text y
6169 | Msel (a, _) ->
6170 state.mstate <- Msel (a, (x, y));
6171 G.postRedisplay "motion select";
6173 | Mscrolly ->
6174 let y = min state.winh (max 0 y) in
6175 scrolly y
6177 | Mscrollx ->
6178 let x = min state.winw (max 0 x) in
6179 scrollx x
6181 | Mzoomrect (p0, _) ->
6182 state.mstate <- Mzoomrect (p0, (x, y));
6183 G.postRedisplay "motion zoomrect";
6184 end;
6185 state.uioh
6187 method pmotion x y =
6188 begin match state.mode with
6189 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6190 let rec loop = function
6191 | [] ->
6192 if hooverpageno != -1
6193 then (
6194 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6195 G.postRedisplay "pmotion birdseye no hoover";
6197 | l :: rest ->
6198 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6199 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6200 then (
6201 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6202 G.postRedisplay "pmotion birdseye hoover";
6204 else loop rest
6206 loop state.layout
6208 | Textentry _ -> ()
6210 | LinkNav _
6211 | View ->
6212 match state.mstate with
6213 | Mnone -> updateunder x y
6214 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6216 end;
6217 state.uioh
6219 method infochanged _ = ()
6221 method scrollph =
6222 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6223 let p, h =
6224 if maxy = 0
6225 then 0.0, float state.winh
6226 else scrollph state.y maxy
6228 state.scrollw, p, h
6230 method scrollpw =
6231 let winw = state.winw - state.scrollw in
6232 let fwinw = float winw in
6233 let sw =
6234 let sw = fwinw /. float state.w in
6235 let sw = fwinw *. sw in
6236 max sw (float conf.scrollh)
6238 let position =
6239 let maxx = state.w + winw in
6240 let x = winw - state.x in
6241 let percent = float x /. float maxx in
6242 (fwinw -. sw) *. percent
6244 state.hscrollh, position, sw
6246 method modehash =
6247 let modename =
6248 match state.mode with
6249 | LinkNav _ -> "links"
6250 | Textentry _ -> "textentry"
6251 | Birdseye _ -> "birdseye"
6252 | View -> "view"
6254 findkeyhash conf modename
6256 method eformsgs = true
6257 end;;
6259 module Config =
6260 struct
6261 open Parser
6263 let fontpath = ref "";;
6265 module KeyMap =
6266 Map.Make (struct type t = (int * int) let compare = compare end);;
6268 let unent s =
6269 let l = String.length s in
6270 let b = Buffer.create l in
6271 unent b s 0 l;
6272 Buffer.contents b;
6275 let home =
6276 try Sys.getenv "HOME"
6277 with exn ->
6278 prerr_endline
6279 ("Can not determine home directory location: " ^ exntos exn);
6283 let modifier_of_string = function
6284 | "alt" -> Wsi.altmask
6285 | "shift" -> Wsi.shiftmask
6286 | "ctrl" | "control" -> Wsi.ctrlmask
6287 | "meta" -> Wsi.metamask
6288 | _ -> 0
6291 let key_of_string =
6292 let r = Str.regexp "-" in
6293 fun s ->
6294 let elems = Str.full_split r s in
6295 let f n k m =
6296 let g s =
6297 let m1 = modifier_of_string s in
6298 if m1 = 0
6299 then (Wsi.namekey s, m)
6300 else (k, m lor m1)
6301 in function
6302 | Str.Delim s when n land 1 = 0 -> g s
6303 | Str.Text s -> g s
6304 | Str.Delim _ -> (k, m)
6306 let rec loop n k m = function
6307 | [] -> (k, m)
6308 | x :: xs ->
6309 let k, m = f n k m x in
6310 loop (n+1) k m xs
6312 loop 0 0 0 elems
6315 let keys_of_string =
6316 let r = Str.regexp "[ \t]" in
6317 fun s ->
6318 let elems = Str.split r s in
6319 List.map key_of_string elems
6322 let copykeyhashes c =
6323 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6326 let config_of c attrs =
6327 let apply c k v =
6329 match k with
6330 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6331 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6332 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6333 | "preload" -> { c with preload = bool_of_string v }
6334 | "page-bias" -> { c with pagebias = int_of_string v }
6335 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6336 | "horizontal-scroll-step" ->
6337 { c with hscrollstep = max (int_of_string v) 1 }
6338 | "auto-scroll-step" ->
6339 { c with autoscrollstep = max 0 (int_of_string v) }
6340 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6341 | "crop-hack" -> { c with crophack = bool_of_string v }
6342 | "throttle" ->
6343 let mw =
6344 match String.lowercase v with
6345 | "true" -> Some infinity
6346 | "false" -> None
6347 | f -> Some (float_of_string f)
6349 { c with maxwait = mw}
6350 | "highlight-links" -> { c with hlinks = bool_of_string v }
6351 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6352 | "vertical-margin" ->
6353 { c with interpagespace = max 0 (int_of_string v) }
6354 | "zoom" ->
6355 let zoom = float_of_string v /. 100. in
6356 let zoom = max zoom 0.0 in
6357 { c with zoom = zoom }
6358 | "presentation" -> { c with presentation = bool_of_string v }
6359 | "rotation-angle" -> { c with angle = int_of_string v }
6360 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6361 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6362 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6363 | "proportional-display" ->
6364 let fm =
6365 if bool_of_string v
6366 then FitProportional
6367 else FitWidth
6369 { c with fitmodel = fm }
6370 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6371 | "pixmap-cache-size" ->
6372 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6373 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6374 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6375 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6376 | "persistent-location" -> { c with jumpback = bool_of_string v }
6377 | "background-color" -> { c with bgcolor = color_of_string v }
6378 | "scrollbar-in-presentation" ->
6379 { c with scrollbarinpm = bool_of_string v }
6380 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6381 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6382 | "mupdf-store-size" ->
6383 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6384 | "checkers" -> { c with checkers = bool_of_string v }
6385 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6386 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6387 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6388 | "uri-launcher" -> { c with urilauncher = unent v }
6389 | "path-launcher" -> { c with pathlauncher = unent v }
6390 | "color-space" -> { c with colorspace = colorspace_of_string v }
6391 | "invert-colors" -> { c with invert = bool_of_string v }
6392 | "brightness" -> { c with colorscale = float_of_string v }
6393 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6394 | "ghyllscroll" ->
6395 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6396 | "columns" ->
6397 let (n, _, _) as nab = multicolumns_of_string v in
6398 if n < 0
6399 then { c with columns = Csplit (-n, [||]) }
6400 else { c with columns = Cmulti (nab, [||]) }
6401 | "birds-eye-columns" ->
6402 { c with beyecolumns = Some (max (int_of_string v) 2) }
6403 | "selection-command" -> { c with selcmd = unent v }
6404 | "synctex-command" -> { c with stcmd = unent v }
6405 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6406 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6407 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6408 | "use-pbo" -> { c with usepbo = bool_of_string v }
6409 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6410 | _ -> c
6411 with exn ->
6412 prerr_endline ("Error processing attribute (`" ^
6413 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6416 let rec fold c = function
6417 | [] -> c
6418 | (k, v) :: rest ->
6419 let c = apply c k v in
6420 fold c rest
6422 fold { c with keyhashes = copykeyhashes c } attrs;
6425 let fromstring f pos n v d =
6426 try f v
6427 with exn ->
6428 dolog "Error processing attribute (%S=%S) at %d\n%s"
6429 n v pos (exntos exn)
6434 let bookmark_of attrs =
6435 let rec fold title page rely visy = function
6436 | ("title", v) :: rest -> fold v page rely visy rest
6437 | ("page", v) :: rest -> fold title v rely visy rest
6438 | ("rely", v) :: rest -> fold title page v visy rest
6439 | ("visy", v) :: rest -> fold title page rely v rest
6440 | _ :: rest -> fold title page rely visy rest
6441 | [] -> title, page, rely, visy
6443 fold "invalid" "0" "0" "0" attrs
6446 let doc_of attrs =
6447 let rec fold path page rely pan visy = function
6448 | ("path", v) :: rest -> fold v page rely pan visy rest
6449 | ("page", v) :: rest -> fold path v rely pan visy rest
6450 | ("rely", v) :: rest -> fold path page v pan visy rest
6451 | ("pan", v) :: rest -> fold path page rely v visy rest
6452 | ("visy", v) :: rest -> fold path page rely pan v rest
6453 | _ :: rest -> fold path page rely pan visy rest
6454 | [] -> path, page, rely, pan, visy
6456 fold "" "0" "0" "0" "0" attrs
6459 let map_of attrs =
6460 let rec fold rs ls = function
6461 | ("out", v) :: rest -> fold v ls rest
6462 | ("in", v) :: rest -> fold rs v rest
6463 | _ :: rest -> fold ls rs rest
6464 | [] -> ls, rs
6466 fold "" "" attrs
6469 let setconf dst src =
6470 dst.scrollbw <- src.scrollbw;
6471 dst.scrollh <- src.scrollh;
6472 dst.icase <- src.icase;
6473 dst.preload <- src.preload;
6474 dst.pagebias <- src.pagebias;
6475 dst.verbose <- src.verbose;
6476 dst.scrollstep <- src.scrollstep;
6477 dst.maxhfit <- src.maxhfit;
6478 dst.crophack <- src.crophack;
6479 dst.autoscrollstep <- src.autoscrollstep;
6480 dst.maxwait <- src.maxwait;
6481 dst.hlinks <- src.hlinks;
6482 dst.underinfo <- src.underinfo;
6483 dst.interpagespace <- src.interpagespace;
6484 dst.zoom <- src.zoom;
6485 dst.presentation <- src.presentation;
6486 dst.angle <- src.angle;
6487 dst.cwinw <- src.cwinw;
6488 dst.cwinh <- src.cwinh;
6489 dst.savebmarks <- src.savebmarks;
6490 dst.memlimit <- src.memlimit;
6491 dst.fitmodel <- src.fitmodel;
6492 dst.texcount <- src.texcount;
6493 dst.sliceheight <- src.sliceheight;
6494 dst.thumbw <- src.thumbw;
6495 dst.jumpback <- src.jumpback;
6496 dst.bgcolor <- src.bgcolor;
6497 dst.scrollbarinpm <- src.scrollbarinpm;
6498 dst.tilew <- src.tilew;
6499 dst.tileh <- src.tileh;
6500 dst.mustoresize <- src.mustoresize;
6501 dst.checkers <- src.checkers;
6502 dst.aalevel <- src.aalevel;
6503 dst.trimmargins <- src.trimmargins;
6504 dst.trimfuzz <- src.trimfuzz;
6505 dst.urilauncher <- src.urilauncher;
6506 dst.colorspace <- src.colorspace;
6507 dst.invert <- src.invert;
6508 dst.colorscale <- src.colorscale;
6509 dst.redirectstderr <- src.redirectstderr;
6510 dst.ghyllscroll <- src.ghyllscroll;
6511 dst.columns <- src.columns;
6512 dst.beyecolumns <- src.beyecolumns;
6513 dst.selcmd <- src.selcmd;
6514 dst.updatecurs <- src.updatecurs;
6515 dst.pathlauncher <- src.pathlauncher;
6516 dst.keyhashes <- copykeyhashes src;
6517 dst.hfsize <- src.hfsize;
6518 dst.hscrollstep <- src.hscrollstep;
6519 dst.pgscale <- src.pgscale;
6520 dst.usepbo <- src.usepbo;
6521 dst.wheelbypage <- src.wheelbypage;
6522 dst.stcmd <- src.stcmd;
6525 let get s =
6526 let h = Hashtbl.create 10 in
6527 let dc = { defconf with angle = defconf.angle } in
6528 let rec toplevel v t spos _ =
6529 match t with
6530 | Vdata | Vcdata | Vend -> v
6531 | Vopen ("llppconfig", _, closed) ->
6532 if closed
6533 then v
6534 else { v with f = llppconfig }
6535 | Vopen _ ->
6536 error "unexpected subelement at top level" s spos
6537 | Vclose _ -> error "unexpected close at top level" s spos
6539 and llppconfig v t spos _ =
6540 match t with
6541 | Vdata | Vcdata -> v
6542 | Vend -> error "unexpected end of input in llppconfig" s spos
6543 | Vopen ("defaults", attrs, closed) ->
6544 let c = config_of dc attrs in
6545 setconf dc c;
6546 if closed
6547 then v
6548 else { v with f = defaults }
6550 | Vopen ("ui-font", attrs, closed) ->
6551 let rec getsize size = function
6552 | [] -> size
6553 | ("size", v) :: rest ->
6554 let size =
6555 fromstring int_of_string spos "size" v fstate.fontsize in
6556 getsize size rest
6557 | l -> getsize size l
6559 fstate.fontsize <- getsize fstate.fontsize attrs;
6560 if closed
6561 then v
6562 else { v with f = uifont (Buffer.create 10) }
6564 | Vopen ("doc", attrs, closed) ->
6565 let pathent, spage, srely, span, svisy = doc_of attrs in
6566 let path = unent pathent
6567 and pageno = fromstring int_of_string spos "page" spage 0
6568 and rely = fromstring float_of_string spos "rely" srely 0.0
6569 and pan = fromstring int_of_string spos "pan" span 0
6570 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6571 let c = config_of dc attrs in
6572 let anchor = (pageno, rely, visy) in
6573 if closed
6574 then (Hashtbl.add h path (c, [], pan, anchor); v)
6575 else { v with f = doc path pan anchor c [] }
6577 | Vopen _ ->
6578 error "unexpected subelement in llppconfig" s spos
6580 | Vclose "llppconfig" -> { v with f = toplevel }
6581 | Vclose _ -> error "unexpected close in llppconfig" s spos
6583 and defaults v t spos _ =
6584 match t with
6585 | Vdata | Vcdata -> v
6586 | Vend -> error "unexpected end of input in defaults" s spos
6587 | Vopen ("keymap", attrs, closed) ->
6588 let modename =
6589 try List.assoc "mode" attrs
6590 with Not_found -> "global" in
6591 if closed
6592 then v
6593 else
6594 let ret keymap =
6595 let h = findkeyhash dc modename in
6596 KeyMap.iter (Hashtbl.replace h) keymap;
6597 defaults
6599 { v with f = pkeymap ret KeyMap.empty }
6601 | Vopen (_, _, _) ->
6602 error "unexpected subelement in defaults" s spos
6604 | Vclose "defaults" ->
6605 { v with f = llppconfig }
6607 | Vclose _ -> error "unexpected close in defaults" s spos
6609 and uifont b v t spos epos =
6610 match t with
6611 | Vdata | Vcdata ->
6612 Buffer.add_substring b s spos (epos - spos);
6614 | Vopen (_, _, _) ->
6615 error "unexpected subelement in ui-font" s spos
6616 | Vclose "ui-font" ->
6617 if String.length !fontpath = 0
6618 then fontpath := Buffer.contents b;
6619 { v with f = llppconfig }
6620 | Vclose _ -> error "unexpected close in ui-font" s spos
6621 | Vend -> error "unexpected end of input in ui-font" s spos
6623 and doc path pan anchor c bookmarks v t spos _ =
6624 match t with
6625 | Vdata | Vcdata -> v
6626 | Vend -> error "unexpected end of input in doc" s spos
6627 | Vopen ("bookmarks", _, closed) ->
6628 if closed
6629 then v
6630 else { v with f = pbookmarks path pan anchor c bookmarks }
6632 | Vopen ("keymap", attrs, closed) ->
6633 let modename =
6634 try List.assoc "mode" attrs
6635 with Not_found -> "global"
6637 if closed
6638 then v
6639 else
6640 let ret keymap =
6641 let h = findkeyhash c modename in
6642 KeyMap.iter (Hashtbl.replace h) keymap;
6643 doc path pan anchor c bookmarks
6645 { v with f = pkeymap ret KeyMap.empty }
6647 | Vopen (_, _, _) ->
6648 error "unexpected subelement in doc" s spos
6650 | Vclose "doc" ->
6651 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6652 { v with f = llppconfig }
6654 | Vclose _ -> error "unexpected close in doc" s spos
6656 and pkeymap ret keymap v t spos _ =
6657 match t with
6658 | Vdata | Vcdata -> v
6659 | Vend -> error "unexpected end of input in keymap" s spos
6660 | Vopen ("map", attrs, closed) ->
6661 let r, l = map_of attrs in
6662 let kss = fromstring keys_of_string spos "in" r [] in
6663 let lss = fromstring keys_of_string spos "out" l [] in
6664 let keymap =
6665 match kss with
6666 | [] -> keymap
6667 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6668 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6670 if closed
6671 then { v with f = pkeymap ret keymap }
6672 else
6673 let f () = v in
6674 { v with f = skip "map" f }
6676 | Vopen _ ->
6677 error "unexpected subelement in keymap" s spos
6679 | Vclose "keymap" ->
6680 { v with f = ret keymap }
6682 | Vclose _ -> error "unexpected close in keymap" s spos
6684 and pbookmarks path pan anchor c bookmarks v t spos _ =
6685 match t with
6686 | Vdata | Vcdata -> v
6687 | Vend -> error "unexpected end of input in bookmarks" s spos
6688 | Vopen ("item", attrs, closed) ->
6689 let titleent, spage, srely, svisy = bookmark_of attrs in
6690 let page = fromstring int_of_string spos "page" spage 0
6691 and rely = fromstring float_of_string spos "rely" srely 0.0
6692 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6693 let bookmarks =
6694 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6696 if closed
6697 then { v with f = pbookmarks path pan anchor c bookmarks }
6698 else
6699 let f () = v in
6700 { v with f = skip "item" f }
6702 | Vopen _ ->
6703 error "unexpected subelement in bookmarks" s spos
6705 | Vclose "bookmarks" ->
6706 { v with f = doc path pan anchor c bookmarks }
6708 | Vclose _ -> error "unexpected close in bookmarks" s spos
6710 and skip tag f v t spos _ =
6711 match t with
6712 | Vdata | Vcdata -> v
6713 | Vend ->
6714 error ("unexpected end of input in skipped " ^ tag) s spos
6715 | Vopen (tag', _, closed) ->
6716 if closed
6717 then v
6718 else
6719 let f' () = { v with f = skip tag f } in
6720 { v with f = skip tag' f' }
6721 | Vclose ctag ->
6722 if tag = ctag
6723 then f ()
6724 else error ("unexpected close in skipped " ^ tag) s spos
6727 parse { f = toplevel; accu = () } s;
6728 h, dc;
6731 let do_load f ic =
6733 let len = in_channel_length ic in
6734 let s = String.create len in
6735 really_input ic s 0 len;
6736 f s;
6737 with
6738 | Parse_error (msg, s, pos) ->
6739 let subs = subs s pos in
6740 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6741 failwith ("parse error: " ^ s)
6743 | exn ->
6744 failwith ("config load error: " ^ exntos exn)
6747 let defconfpath =
6748 let dir =
6750 let dir = Filename.concat home ".config" in
6751 if Sys.is_directory dir then dir else home
6752 with _ -> home
6754 Filename.concat dir "llpp.conf"
6757 let confpath = ref defconfpath;;
6759 let load1 f =
6760 if Sys.file_exists !confpath
6761 then
6762 match
6763 (try Some (open_in_bin !confpath)
6764 with exn ->
6765 prerr_endline
6766 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6767 exntos exn);
6768 None
6770 with
6771 | Some ic ->
6772 let success =
6774 f (do_load get ic)
6775 with exn ->
6776 prerr_endline
6777 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6778 exntos exn);
6779 false
6781 close_in ic;
6782 success
6784 | None -> false
6785 else
6786 f (Hashtbl.create 0, defconf)
6789 let load () =
6790 let f (h, dc) =
6791 let pc, pb, px, pa =
6793 let key =
6794 if String.length state.origin = 0
6795 then state.path
6796 else state.origin
6798 Hashtbl.find h (Filename.basename key)
6799 with Not_found -> dc, [], 0, emptyanchor
6801 setconf defconf dc;
6802 setconf conf pc;
6803 state.bookmarks <- pb;
6804 state.x <- px;
6805 state.scrollw <- conf.scrollbw;
6806 if conf.jumpback
6807 then state.anchor <- pa;
6808 cbput state.hists.nav pa;
6809 true
6811 load1 f
6814 let add_attrs bb always dc c =
6815 let ob s a b =
6816 if always || a != b
6817 then Printf.bprintf bb "\n %s='%b'" s a
6818 and oi s a b =
6819 if always || a != b
6820 then Printf.bprintf bb "\n %s='%d'" s a
6821 and oI s a b =
6822 if always || a != b
6823 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6824 and oz s a b =
6825 if always || a <> b
6826 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6827 and oF s a b =
6828 if always || a <> b
6829 then Printf.bprintf bb "\n %s='%f'" s a
6830 and oc s a b =
6831 if always || a <> b
6832 then
6833 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6834 and oC s a b =
6835 if always || a <> b
6836 then
6837 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6838 and oR s a b =
6839 if always || a <> b
6840 then
6841 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6842 and os s a b =
6843 if always || a <> b
6844 then
6845 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6846 and og s a b =
6847 if always || a <> b
6848 then
6849 match a with
6850 | None -> ()
6851 | Some (_N, _A, _B) ->
6852 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6853 and oW s a b =
6854 if always || a <> b
6855 then
6856 let v =
6857 match a with
6858 | None -> "false"
6859 | Some f ->
6860 if f = infinity
6861 then "true"
6862 else string_of_float f
6864 Printf.bprintf bb "\n %s='%s'" s v
6865 and oco s a b =
6866 if always || a <> b
6867 then
6868 match a with
6869 | Cmulti ((n, a, b), _) when n > 1 ->
6870 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6871 | Csplit (n, _) when n > 1 ->
6872 Printf.bprintf bb "\n %s='%d'" s ~-n
6873 | _ -> ()
6874 and obeco s a b =
6875 if always || a <> b
6876 then
6877 match a with
6878 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6879 | _ -> ()
6880 and oFm s a b =
6881 if always || a <> b
6882 then
6883 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6885 oi "width" c.cwinw dc.cwinw;
6886 oi "height" c.cwinh dc.cwinh;
6887 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6888 oi "scroll-handle-height" c.scrollh dc.scrollh;
6889 ob "case-insensitive-search" c.icase dc.icase;
6890 ob "preload" c.preload dc.preload;
6891 oi "page-bias" c.pagebias dc.pagebias;
6892 oi "scroll-step" c.scrollstep dc.scrollstep;
6893 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6894 ob "max-height-fit" c.maxhfit dc.maxhfit;
6895 ob "crop-hack" c.crophack dc.crophack;
6896 oW "throttle" c.maxwait dc.maxwait;
6897 ob "highlight-links" c.hlinks dc.hlinks;
6898 ob "under-cursor-info" c.underinfo dc.underinfo;
6899 oi "vertical-margin" c.interpagespace dc.interpagespace;
6900 oz "zoom" c.zoom dc.zoom;
6901 ob "presentation" c.presentation dc.presentation;
6902 oi "rotation-angle" c.angle dc.angle;
6903 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6904 oFm "fit-model" c.fitmodel dc.fitmodel;
6905 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6906 oi "tex-count" c.texcount dc.texcount;
6907 oi "slice-height" c.sliceheight dc.sliceheight;
6908 oi "thumbnail-width" c.thumbw dc.thumbw;
6909 ob "persistent-location" c.jumpback dc.jumpback;
6910 oc "background-color" c.bgcolor dc.bgcolor;
6911 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6912 oi "tile-width" c.tilew dc.tilew;
6913 oi "tile-height" c.tileh dc.tileh;
6914 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6915 ob "checkers" c.checkers dc.checkers;
6916 oi "aalevel" c.aalevel dc.aalevel;
6917 ob "trim-margins" c.trimmargins dc.trimmargins;
6918 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6919 os "uri-launcher" c.urilauncher dc.urilauncher;
6920 os "path-launcher" c.pathlauncher dc.pathlauncher;
6921 oC "color-space" c.colorspace dc.colorspace;
6922 ob "invert-colors" c.invert dc.invert;
6923 oF "brightness" c.colorscale dc.colorscale;
6924 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6925 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6926 oco "columns" c.columns dc.columns;
6927 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6928 os "selection-command" c.selcmd dc.selcmd;
6929 os "synctex-command" c.stcmd dc.stcmd;
6930 ob "update-cursor" c.updatecurs dc.updatecurs;
6931 oi "hint-font-size" c.hfsize dc.hfsize;
6932 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6933 oF "page-scroll-scale" c.pgscale dc.pgscale;
6934 ob "use-pbo" c.usepbo dc.usepbo;
6935 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6938 let keymapsbuf always dc c =
6939 let bb = Buffer.create 16 in
6940 let rec loop = function
6941 | [] -> ()
6942 | (modename, h) :: rest ->
6943 let dh = findkeyhash dc modename in
6944 if always || h <> dh
6945 then (
6946 if Hashtbl.length h > 0
6947 then (
6948 if Buffer.length bb > 0
6949 then Buffer.add_char bb '\n';
6950 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6951 Hashtbl.iter (fun i o ->
6952 let isdifferent = always ||
6954 let dO = Hashtbl.find dh i in
6955 dO <> o
6956 with Not_found -> true
6958 if isdifferent
6959 then
6960 let addkm (k, m) =
6961 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6962 if Wsi.withalt m then Buffer.add_string bb "alt-";
6963 if Wsi.withshift m then Buffer.add_string bb "shift-";
6964 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6965 Buffer.add_string bb (Wsi.keyname k);
6967 let addkms l =
6968 let rec loop = function
6969 | [] -> ()
6970 | km :: [] -> addkm km
6971 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6973 loop l
6975 Buffer.add_string bb "<map in='";
6976 addkm i;
6977 match o with
6978 | KMinsrt km ->
6979 Buffer.add_string bb "' out='";
6980 addkm km;
6981 Buffer.add_string bb "'/>\n"
6983 | KMinsrl kms ->
6984 Buffer.add_string bb "' out='";
6985 addkms kms;
6986 Buffer.add_string bb "'/>\n"
6988 | KMmulti (ins, kms) ->
6989 Buffer.add_char bb ' ';
6990 addkms ins;
6991 Buffer.add_string bb "' out='";
6992 addkms kms;
6993 Buffer.add_string bb "'/>\n"
6994 ) h;
6995 Buffer.add_string bb "</keymap>";
6998 loop rest
7000 loop c.keyhashes;
7004 let save () =
7005 let uifontsize = fstate.fontsize in
7006 let bb = Buffer.create 32768 in
7007 let w, h =
7008 List.fold_left
7009 (fun (w, h) ws ->
7010 match ws with
7011 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
7012 | Wsi.MaxVert -> (w, conf.cwinh)
7013 | Wsi.MaxHorz -> (conf.cwinw, h)
7015 (state.winw, state.winh) state.winstate
7017 conf.cwinw <- w;
7018 conf.cwinh <- h;
7019 let f (h, dc) =
7020 let dc = if conf.bedefault then conf else dc in
7021 Buffer.add_string bb "<llppconfig>\n";
7023 if String.length !fontpath > 0
7024 then
7025 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7026 uifontsize
7027 !fontpath
7028 else (
7029 if uifontsize <> 14
7030 then
7031 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7034 Buffer.add_string bb "<defaults ";
7035 add_attrs bb true dc dc;
7036 let kb = keymapsbuf true dc dc in
7037 if Buffer.length kb > 0
7038 then (
7039 Buffer.add_string bb ">\n";
7040 Buffer.add_buffer bb kb;
7041 Buffer.add_string bb "\n</defaults>\n";
7043 else Buffer.add_string bb "/>\n";
7045 let adddoc path pan anchor c bookmarks =
7046 if bookmarks == [] && c = dc && anchor = emptyanchor
7047 then ()
7048 else (
7049 Printf.bprintf bb "<doc path='%s'"
7050 (enent path 0 (String.length path));
7052 if anchor <> emptyanchor
7053 then (
7054 let n, rely, visy = anchor in
7055 Printf.bprintf bb " page='%d'" n;
7056 if rely > 1e-6
7057 then
7058 Printf.bprintf bb " rely='%f'" rely
7060 if abs_float visy > 1e-6
7061 then
7062 Printf.bprintf bb " visy='%f'" visy
7066 if pan != 0
7067 then Printf.bprintf bb " pan='%d'" pan;
7069 add_attrs bb false dc c;
7070 let kb = keymapsbuf false dc c in
7072 begin match bookmarks with
7073 | [] ->
7074 if Buffer.length kb > 0
7075 then (
7076 Buffer.add_string bb ">\n";
7077 Buffer.add_buffer bb kb;
7078 Buffer.add_string bb "\n</doc>\n";
7080 else Buffer.add_string bb "/>\n"
7081 | _ ->
7082 Buffer.add_string bb ">\n<bookmarks>\n";
7083 List.iter (fun (title, _level, (page, rely, visy)) ->
7084 Printf.bprintf bb
7085 "<item title='%s' page='%d'"
7086 (enent title 0 (String.length title))
7087 page
7089 if rely > 1e-6
7090 then
7091 Printf.bprintf bb " rely='%f'" rely
7093 if abs_float visy > 1e-6
7094 then
7095 Printf.bprintf bb " visy='%f'" visy
7097 Buffer.add_string bb "/>\n";
7098 ) bookmarks;
7099 Buffer.add_string bb "</bookmarks>";
7100 if Buffer.length kb > 0
7101 then (
7102 Buffer.add_string bb "\n";
7103 Buffer.add_buffer bb kb;
7105 Buffer.add_string bb "\n</doc>\n";
7106 end;
7110 let pan, conf =
7111 match state.mode with
7112 | Birdseye (c, pan, _, _, _) ->
7113 let beyecolumns =
7114 match conf.columns with
7115 | Cmulti ((c, _, _), _) -> Some c
7116 | Csingle _ -> None
7117 | Csplit _ -> None
7118 and columns =
7119 match c.columns with
7120 | Cmulti (c, _) -> Cmulti (c, [||])
7121 | Csingle _ -> Csingle [||]
7122 | Csplit _ -> failwith "quit from bird's eye while split"
7124 pan, { c with beyecolumns = beyecolumns; columns = columns }
7125 | _ -> state.x, conf
7127 let basename = Filename.basename
7128 (if String.length state.origin = 0 then state.path else state.origin)
7130 adddoc basename pan (getanchor ())
7131 (let conf =
7132 let autoscrollstep =
7133 match state.autoscroll with
7134 | Some step -> step
7135 | None -> conf.autoscrollstep
7137 match state.mode with
7138 | Birdseye (bc, _, _, _, _) ->
7139 { conf with
7140 zoom = bc.zoom;
7141 presentation = bc.presentation;
7142 interpagespace = bc.interpagespace;
7143 maxwait = bc.maxwait;
7144 autoscrollstep = autoscrollstep }
7145 | _ -> { conf with autoscrollstep = autoscrollstep }
7146 in conf)
7147 (if conf.savebmarks then state.bookmarks else []);
7149 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7150 if basename <> path
7151 then adddoc path x anchor c bookmarks
7152 ) h;
7153 Buffer.add_string bb "</llppconfig>\n";
7154 true;
7156 if load1 f && Buffer.length bb > 0
7157 then
7159 let tmp = !confpath ^ ".tmp" in
7160 let oc = open_out_bin tmp in
7161 Buffer.output_buffer oc bb;
7162 close_out oc;
7163 Unix.rename tmp !confpath;
7164 with exn ->
7165 prerr_endline
7166 ("error while saving configuration: " ^ exntos exn)
7168 end;;
7170 let adderrmsg src msg =
7171 Buffer.add_string state.errmsgs msg;
7172 state.newerrmsgs <- true;
7173 G.postRedisplay src
7176 let adderrfmt src fmt =
7177 Format.kprintf (fun s -> adderrmsg src s) fmt;
7180 let ract cmds =
7181 let cl = splitatspace cmds in
7182 let scan s fmt f =
7183 try Scanf.sscanf s fmt f
7184 with exn ->
7185 adderrfmt "remote exec"
7186 "error processing '%S': %s\n" cmds (exntos exn)
7188 match cl with
7189 | "reload" :: [] -> reload ()
7190 | "goto" :: args :: [] ->
7191 scan args "%u %f %f"
7192 (fun pageno x y ->
7193 let cmd, _ = state.geomcmds in
7194 if String.length cmd = 0
7195 then gotopagexy pageno x y
7196 else
7197 let f prevf () =
7198 gotopagexy pageno x y;
7199 prevf ()
7201 state.reprf <- f state.reprf
7203 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7204 | "rect" :: args :: [] ->
7205 scan args "%u %u %f %f %f %f"
7206 (fun pageno color x0 y0 x1 y1 ->
7207 onpagerect pageno (fun w h ->
7208 let _,w1,h1,_ = getpagedim pageno in
7209 let sw = float w1 /. w
7210 and sh = float h1 /. h in
7211 let x0s = x0 *. sw
7212 and x1s = x1 *. sw
7213 and y0s = y0 *. sh
7214 and y1s = y1 *. sh in
7215 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7216 debugrect rect;
7217 state.rects <- (pageno, color, rect) :: state.rects;
7218 G.postRedisplay "rect";
7221 | "activatewin" :: [] -> Wsi.activatewin ()
7222 | "quit" :: [] -> raise Quit
7223 | _ ->
7224 adderrfmt "remote command"
7225 "error processing remote command: %S\n" cmds;
7228 let remote =
7229 let scratch = String.create 80 in
7230 let buf = Buffer.create 80 in
7231 fun fd ->
7232 let rec tempfr () =
7233 try Some (Unix.read fd scratch 0 80)
7234 with
7235 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7236 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7237 | exn -> raise exn
7239 match tempfr () with
7240 | None -> Some fd
7241 | Some n ->
7242 if n = 0
7243 then (
7244 Unix.close fd;
7245 if Buffer.length buf > 0
7246 then (
7247 let s = Buffer.contents buf in
7248 Buffer.clear buf;
7249 ract s;
7251 None
7253 else
7254 let rec eat ppos =
7255 let nlpos =
7257 let pos = String.index_from scratch ppos '\n' in
7258 if pos >= n then -1 else pos
7259 with Not_found -> -1
7261 if nlpos >= 0
7262 then (
7263 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7264 let s = Buffer.contents buf in
7265 Buffer.clear buf;
7266 ract s;
7267 eat (nlpos+1);
7269 else (
7270 Buffer.add_substring buf scratch ppos (n-ppos);
7271 Some fd
7273 in eat 0
7276 let remoteopen path =
7277 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7278 with exn ->
7279 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7280 None
7283 let () =
7284 let trimcachepath = ref "" in
7285 let rcmdpath = ref "" in
7286 Arg.parse
7287 (Arg.align
7288 [("-p", Arg.String (fun s -> state.password <- s),
7289 "<password> Set password");
7291 ("-f", Arg.String (fun s -> Config.fontpath := s),
7292 "<path> Set path to the user interface font");
7294 ("-c", Arg.String (fun s -> Config.confpath := s),
7295 "<path> Set path to the configuration file");
7297 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7298 "<path> Set path to the trim cache file");
7300 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7301 "<named-destination> Set named destination");
7303 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7305 ("-remote", Arg.String (fun s -> rcmdpath := s),
7306 "<path> Set path to the remote commands source");
7308 ("-origin", Arg.String (fun s -> state.origin <- s),
7309 "<original path> Set original path");
7311 ("-v", Arg.Unit (fun () ->
7312 Printf.printf
7313 "%s\nconfiguration path: %s\n"
7314 (version ())
7315 Config.defconfpath
7317 exit 0), " Print version and exit");
7320 (fun s -> state.path <- s)
7321 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7323 if String.length state.path = 0
7324 then (prerr_endline "file name missing"; exit 1);
7326 if not (Config.load ())
7327 then prerr_endline "failed to load configuration";
7329 let globalkeyhash = findkeyhash conf "global" in
7330 let wsfd, winw, winh = Wsi.init (object
7331 method expose =
7332 if nogeomcmds state.geomcmds || platform == Posx
7333 then display ()
7334 else (
7335 GlClear.color (scalecolor2 conf.bgcolor);
7336 GlClear.clear [`color];
7338 method display = display ()
7339 method reshape w h = reshape w h
7340 method mouse b d x y m = mouse b d x y m
7341 method motion x y = state.mpos <- (x, y); motion x y
7342 method pmotion x y = state.mpos <- (x, y); pmotion x y
7343 method key k m =
7344 let mascm = m land (
7345 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7346 ) in
7347 match state.keystate with
7348 | KSnone ->
7349 let km = k, mascm in
7350 begin
7351 match
7352 let modehash = state.uioh#modehash in
7353 try Hashtbl.find modehash km
7354 with Not_found ->
7355 try Hashtbl.find globalkeyhash km
7356 with Not_found -> KMinsrt (k, m)
7357 with
7358 | KMinsrt (k, m) -> keyboard k m
7359 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7360 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7362 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7363 List.iter (fun (k, m) -> keyboard k m) insrt;
7364 state.keystate <- KSnone
7365 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7366 state.keystate <- KSinto (keys, insrt)
7367 | _ ->
7368 state.keystate <- KSnone
7370 method enter x y = state.mpos <- (x, y); pmotion x y
7371 method leave = state.mpos <- (-1, -1)
7372 method winstate wsl = state.winstate <- wsl
7373 method quit = raise Quit
7374 end) conf.cwinw conf.cwinh (platform = Posx) in
7376 state.wsfd <- wsfd;
7378 if not (
7379 List.exists GlMisc.check_extension
7380 [ "GL_ARB_texture_rectangle"
7381 ; "GL_EXT_texture_recangle"
7382 ; "GL_NV_texture_rectangle" ]
7384 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7386 let cr, sw =
7387 match Ne.pipe () with
7388 | Ne.Exn exn ->
7389 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7390 exit 1
7391 | Ne.Res rw -> rw
7392 and sr, cw =
7393 match Ne.pipe () with
7394 | Ne.Exn exn ->
7395 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7396 exit 1
7397 | Ne.Res rw -> rw
7400 cloexec cr;
7401 cloexec sw;
7402 cloexec sr;
7403 cloexec cw;
7405 setcheckers conf.checkers;
7406 redirectstderr ();
7408 init (cr, cw) (
7409 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7410 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7411 !Config.fontpath, !trimcachepath,
7412 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7414 state.sr <- sr;
7415 state.sw <- sw;
7416 state.text <- "Opening " ^ (mbtoutf8 state.path);
7417 reshape winw winh;
7418 opendoc state.path state.password;
7419 state.uioh <- uioh;
7421 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7422 let optrfd =
7423 ref (
7424 if String.length !rcmdpath > 0
7425 then remoteopen !rcmdpath
7426 else None
7430 let rec loop deadline =
7431 let r =
7432 match state.errfd with
7433 | None -> [state.sr; state.wsfd]
7434 | Some fd -> [state.sr; state.wsfd; fd]
7436 let r =
7437 match !optrfd with
7438 | None -> r
7439 | Some fd -> fd :: r
7441 if state.redisplay
7442 then (
7443 state.redisplay <- false;
7444 display ();
7446 let timeout =
7447 let now = now () in
7448 if deadline > now
7449 then (
7450 if deadline = infinity
7451 then ~-.1.0
7452 else max 0.0 (deadline -. now)
7454 else 0.0
7456 let r, _, _ =
7457 try Unix.select r [] [] timeout
7458 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7460 begin match r with
7461 | [] ->
7462 state.ghyll None;
7463 let newdeadline =
7464 if state.ghyll == noghyll
7465 then
7466 match state.autoscroll with
7467 | Some step when step != 0 ->
7468 let y = state.y + step in
7469 let y =
7470 if y < 0
7471 then state.maxy
7472 else if y >= state.maxy then 0 else y
7474 gotoy y;
7475 if state.mode = View
7476 then state.text <- "";
7477 deadline +. 0.01
7478 | _ -> infinity
7479 else deadline +. 0.01
7481 loop newdeadline
7483 | l ->
7484 let rec checkfds = function
7485 | [] -> ()
7486 | fd :: rest when fd = state.sr ->
7487 let cmd = readcmd state.sr in
7488 act cmd;
7489 checkfds rest
7491 | fd :: rest when fd = state.wsfd ->
7492 Wsi.readresp fd;
7493 checkfds rest
7495 | fd :: rest when Some fd = !optrfd ->
7496 begin match remote fd with
7497 | None -> optrfd := remoteopen !rcmdpath;
7498 | opt -> optrfd := opt
7499 end;
7500 checkfds rest
7502 | fd :: rest ->
7503 let s = String.create 80 in
7504 let n = tempfailureretry (Unix.read fd s 0) 80 in
7505 if conf.redirectstderr
7506 then (
7507 Buffer.add_substring state.errmsgs s 0 n;
7508 state.newerrmsgs <- true;
7509 state.redisplay <- true;
7511 else (
7512 prerr_string (String.sub s 0 n);
7513 flush stderr;
7515 checkfds rest
7517 checkfds l;
7518 let newdeadline =
7519 let deadline1 =
7520 if deadline = infinity
7521 then now () +. 0.01
7522 else deadline
7524 match state.autoscroll with
7525 | Some step when step != 0 -> deadline1
7526 | _ -> if state.ghyll == noghyll then infinity else deadline1
7528 loop newdeadline
7529 end;
7532 loop infinity;
7533 with Quit ->
7534 Config.save ();