llppac: For readability, use single-quoting in error messages
[llpp.git] / main.ml
blob4c25f491de8bb59a9f404df83fabed278a850317
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * fitmodel * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and trimmargins = bool
29 and interpagespace = int
30 and texcount = int
31 and sliceheight = int
32 and gen = int
33 and top = float
34 and dtop = float
35 and fontpath = string
36 and trimcachepath = string
37 and memsize = int
38 and aalevel = int
39 and irect = (int * int * int * int)
40 and trimparams = (trimmargins * irect)
41 and colorspace = | Rgb | Bgr | Gray
42 and fitmodel = | FitWidth | FitProportional | FitPage
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external postprocess :
96 opaque -> int -> int -> int -> (int * string * int) -> int
97 = "ml_postprocess";;
98 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
99 external platform : unit -> platform = "ml_platform";;
100 external setaalevel : int -> unit = "ml_setaalevel";;
101 external realloctexts : int -> bool = "ml_realloctexts";;
102 external findlink : opaque -> linkdir -> link = "ml_findlink";;
103 external getlink : opaque -> int -> under = "ml_getlink";;
104 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
105 external getlinkcount : opaque -> int = "ml_getlinkcount";;
106 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
107 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
108 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
109 external freepbo : string -> unit = "ml_freepbo";;
110 external unmappbo : string -> unit = "ml_unmappbo";;
111 external pbousable : unit -> bool = "ml_pbo_usable";;
112 external unproject : opaque -> int -> int -> (int * int) option
113 = "ml_unproject";;
114 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
116 let platform_to_string = function
117 | Punknown -> "unknown"
118 | Plinux -> "Linux"
119 | Posx -> "OSX"
120 | Psun -> "Sun"
121 | Pfreebsd -> "FreeBSD"
122 | Pdragonflybsd -> "DragonflyBSD"
123 | Popenbsd -> "OpenBSD"
124 | Pnetbsd -> "NetBSD"
125 | Pcygwin -> "Cygwin"
128 let platform = platform ();;
130 let now = Unix.gettimeofday;;
132 let popen cmd fda =
133 if platform = Pcygwin
134 then (
135 let sh = "/bin/sh" in
136 let args = [|sh; "-c"; cmd|] in
137 let rec std si so se = function
138 | [] -> si, so, se
139 | (fd, 0) :: rest -> std fd so se rest
140 | (fd, -1) :: rest ->
141 Unix.set_close_on_exec fd;
142 std si so se rest
143 | (_, n) :: _ ->
144 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
146 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
147 ignore (Unix.create_process sh args si so se)
149 else popen cmd fda;
152 type mpos = int * int
153 and mstate =
154 | Msel of (mpos * mpos)
155 | Mpan of mpos
156 | Mscrolly | Mscrollx
157 | Mzoom of (int * int)
158 | Mzoomrect of (mpos * mpos)
159 | Mnone
162 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
163 and onkey = string -> int -> te
164 and ondone = string -> unit
165 and histcancel = unit -> unit
166 and onhist = ((histcmd -> string) * histcancel)
167 and histcmd = HCnext | HCprev | HCfirst | HClast
168 and cancelonempty = bool
169 and te =
170 | TEstop
171 | TEdone of string
172 | TEcont of string
173 | TEswitch of textentry
176 type 'a circbuf =
177 { store : 'a array
178 ; mutable rc : int
179 ; mutable wc : int
180 ; mutable len : int
184 let bound v minv maxv =
185 max minv (min maxv v);
188 let cbnew n v =
189 { store = Array.create n v
190 ; rc = 0
191 ; wc = 0
192 ; len = 0
196 let cbcap b = Array.length b.store;;
198 let cbput b v =
199 let cap = cbcap b in
200 b.store.(b.wc) <- v;
201 b.wc <- (b.wc + 1) mod cap;
202 b.rc <- b.wc;
203 b.len <- min (b.len + 1) cap;
206 let cbempty b = b.len = 0;;
208 let cbgetg b circular dir =
209 if cbempty b
210 then b.store.(0)
211 else
212 let rc = b.rc + dir in
213 let rc =
214 if circular
215 then (
216 if rc = -1
217 then b.len-1
218 else (
219 if rc >= b.len
220 then 0
221 else rc
224 else bound rc 0 (b.len-1)
226 b.rc <- rc;
227 b.store.(rc);
230 let cbget b = cbgetg b false;;
231 let cbgetc b = cbgetg b true;;
233 let drawstring size x y s =
234 Gl.enable `blend;
235 Gl.enable `texture_2d;
236 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
237 ignore (drawstr size x y s);
238 Gl.disable `blend;
239 Gl.disable `texture_2d;
242 let drawstring1 size x y s =
243 drawstr size x y s;
246 let drawstring2 size x y fmt =
247 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
250 type page =
251 { pageno : int
252 ; pagedimno : int
253 ; pagew : int
254 ; pageh : int
255 ; pagex : int
256 ; pagey : int
257 ; pagevw : int
258 ; pagevh : int
259 ; pagedispx : int
260 ; pagedispy : int
261 ; pagecol : int
265 let debugl l =
266 dolog "l %d dim=%d {" l.pageno l.pagedimno;
267 dolog " WxH %dx%d" l.pagew l.pageh;
268 dolog " vWxH %dx%d" l.pagevw l.pagevh;
269 dolog " pagex,y %d,%d" l.pagex l.pagey;
270 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
271 dolog " column %d" l.pagecol;
272 dolog "}";
275 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
276 dolog "rect {";
277 dolog " x0,y0=(% f, % f)" x0 y0;
278 dolog " x1,y1=(% f, % f)" x1 y1;
279 dolog " x2,y2=(% f, % f)" x2 y2;
280 dolog " x3,y3=(% f, % f)" x3 y3;
281 dolog "}";
284 type multicolumns = multicol * pagegeom
285 and singlecolumn = pagegeom
286 and splitcolumns = columncount * pagegeom
287 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
288 and multicol = columncount * covercount * covercount
289 and pdimno = int
290 and columncount = int
291 and covercount = int;;
293 type scrollb = int;;
294 let scrollbvv = 1;;
295 let scrollbhv = 2;;
297 type conf =
298 { mutable scrollbw : int
299 ; mutable scrollh : int
300 ; mutable scrollb : scrollb
301 ; mutable icase : bool
302 ; mutable preload : bool
303 ; mutable pagebias : int
304 ; mutable verbose : bool
305 ; mutable debug : bool
306 ; mutable scrollstep : int
307 ; mutable hscrollstep : int
308 ; mutable maxhfit : bool
309 ; mutable crophack : bool
310 ; mutable autoscrollstep : int
311 ; mutable maxwait : float option
312 ; mutable hlinks : bool
313 ; mutable underinfo : bool
314 ; mutable interpagespace : interpagespace
315 ; mutable zoom : float
316 ; mutable presentation : bool
317 ; mutable angle : angle
318 ; mutable cwinw : int
319 ; mutable cwinh : int
320 ; mutable savebmarks : bool
321 ; mutable fitmodel : fitmodel
322 ; mutable trimmargins : trimmargins
323 ; mutable trimfuzz : irect
324 ; mutable memlimit : memsize
325 ; mutable texcount : texcount
326 ; mutable sliceheight : sliceheight
327 ; mutable thumbw : width
328 ; mutable jumpback : bool
329 ; mutable bgcolor : (float * float * float)
330 ; mutable bedefault : bool
331 ; mutable tilew : int
332 ; mutable tileh : int
333 ; mutable mustoresize : memsize
334 ; mutable checkers : bool
335 ; mutable aalevel : int
336 ; mutable urilauncher : string
337 ; mutable pathlauncher : string
338 ; mutable colorspace : colorspace
339 ; mutable invert : bool
340 ; mutable colorscale : float
341 ; mutable redirectstderr : bool
342 ; mutable ghyllscroll : (int * int * int) option
343 ; mutable columns : columns
344 ; mutable beyecolumns : columncount option
345 ; mutable selcmd : string
346 ; mutable updatecurs : bool
347 ; mutable keyhashes : (string * keyhash) list
348 ; mutable hfsize : int
349 ; mutable pgscale : float
350 ; mutable usepbo : bool
351 ; mutable wheelbypage : bool
352 ; mutable stcmd : string
353 ; mutable riani : bool
355 and columns =
356 | Csingle of singlecolumn
357 | Cmulti of multicolumns
358 | Csplit of splitcolumns
361 type anchor = pageno * top * dtop;;
363 type outline = string * int * anchor;;
365 type rect = float * float * float * float * float * float * float * float;;
367 type tile = opaque * pixmapsize * elapsed
368 and elapsed = float;;
369 type pagemapkey = pageno * gen;;
370 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
371 and row = int
372 and col = int;;
374 let emptyanchor = (0, 0.0, 0.0);;
376 type infochange = | Memused | Docinfo | Pdim;;
378 class type uioh = object
379 method display : unit
380 method key : int -> int -> uioh
381 method button : int -> bool -> int -> int -> int -> uioh
382 method motion : int -> int -> uioh
383 method pmotion : int -> int -> uioh
384 method infochanged : infochange -> unit
385 method scrollpw : (int * float * float)
386 method scrollph : (int * float * float)
387 method modehash : keyhash
388 method eformsgs : bool
389 end;;
391 type mode =
392 | Birdseye of (conf * leftx * pageno * pageno * anchor)
393 | Textentry of (textentry * onleave)
394 | View
395 | LinkNav of linktarget
396 and onleave = leavetextentrystatus -> unit
397 and leavetextentrystatus = | Cancel | Confirm
398 and helpitem = string * int * action
399 and action =
400 | Noaction
401 | Action of (uioh -> uioh)
402 and linktarget =
403 | Ltexact of (pageno * int)
404 | Ltgendir of int
407 let isbirdseye = function Birdseye _ -> true | _ -> false;;
408 let istextentry = function Textentry _ -> true | _ -> false;;
410 type currently =
411 | Idle
412 | Loading of (page * gen)
413 | Tiling of (
414 page * opaque * colorspace * angle * gen * col * row * width * height
416 | Outlining of outline list
419 let emptykeyhash = Hashtbl.create 0;;
420 let nouioh : uioh = object (self)
421 method display = ()
422 method key _ _ = self
423 method button _ _ _ _ _ = self
424 method motion _ _ = self
425 method pmotion _ _ = self
426 method infochanged _ = ()
427 method scrollpw = (0, nan, nan)
428 method scrollph = (0, nan, nan)
429 method modehash = emptykeyhash
430 method eformsgs = false
431 end;;
433 type state =
434 { mutable sr : Unix.file_descr
435 ; mutable sw : Unix.file_descr
436 ; mutable wsfd : Unix.file_descr
437 ; mutable errfd : Unix.file_descr option
438 ; mutable stderr : Unix.file_descr
439 ; mutable errmsgs : Buffer.t
440 ; mutable newerrmsgs : bool
441 ; mutable w : int
442 ; mutable x : int
443 ; mutable y : int
444 ; mutable anchor : anchor
445 ; mutable ranchors : (string * string * anchor * string) list
446 ; mutable maxy : int
447 ; mutable layout : page list
448 ; pagemap : (pagemapkey, opaque) Hashtbl.t
449 ; tilemap : (tilemapkey, tile) Hashtbl.t
450 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
451 ; mutable pdims : (pageno * width * height * leftx) list
452 ; mutable pagecount : int
453 ; mutable currently : currently
454 ; mutable mstate : mstate
455 ; mutable searchpattern : string
456 ; mutable rects : (pageno * recttype * rect) list
457 ; mutable rects1 : (pageno * recttype * rect) list
458 ; mutable text : string
459 ; mutable winstate : Wsi.winstate list
460 ; mutable mode : mode
461 ; mutable uioh : uioh
462 ; mutable outlines : outline array
463 ; mutable bookmarks : outline list
464 ; mutable path : string
465 ; mutable password : string
466 ; mutable nameddest : string
467 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
468 ; mutable memused : memsize
469 ; mutable gen : gen
470 ; mutable throttle : (page list * int * float) option
471 ; mutable autoscroll : int option
472 ; mutable ghyll : (int option -> unit)
473 ; mutable help : helpitem array
474 ; mutable docinfo : (int * string) list
475 ; mutable texid : GlTex.texture_id option
476 ; hists : hists
477 ; mutable prevzoom : float
478 ; mutable progress : float
479 ; mutable redisplay : bool
480 ; mutable mpos : mpos
481 ; mutable keystate : keystate
482 ; mutable glinks : bool
483 ; mutable prevcolumns : (columns * float) option
484 ; mutable winw : int
485 ; mutable winh : int
486 ; mutable reprf : (unit -> unit)
487 ; mutable origin : string
489 and hists =
490 { pat : string circbuf
491 ; pag : string circbuf
492 ; nav : anchor circbuf
493 ; sel : string circbuf
497 let defconf =
498 { scrollbw = 7
499 ; scrollh = 12
500 ; scrollb = scrollbhv lor scrollbvv
501 ; icase = true
502 ; preload = true
503 ; pagebias = 0
504 ; verbose = false
505 ; debug = false
506 ; scrollstep = 24
507 ; hscrollstep = 24
508 ; maxhfit = true
509 ; crophack = false
510 ; autoscrollstep = 2
511 ; maxwait = None
512 ; hlinks = false
513 ; underinfo = false
514 ; interpagespace = 2
515 ; zoom = 1.0
516 ; presentation = false
517 ; angle = 0
518 ; cwinw = 900
519 ; cwinh = 900
520 ; savebmarks = true
521 ; fitmodel = FitProportional
522 ; trimmargins = false
523 ; trimfuzz = (0,0,0,0)
524 ; memlimit = 32 lsl 20
525 ; texcount = 256
526 ; sliceheight = 24
527 ; thumbw = 76
528 ; jumpback = true
529 ; bgcolor = (0.5, 0.5, 0.5)
530 ; bedefault = false
531 ; tilew = 2048
532 ; tileh = 2048
533 ; mustoresize = 256 lsl 20
534 ; checkers = true
535 ; aalevel = 8
536 ; urilauncher =
537 (match platform with
538 | Plinux | Pfreebsd | Pdragonflybsd
539 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
540 | Posx -> "open \"%s\""
541 | Pcygwin -> "cygstart \"%s\""
542 | Punknown -> "echo %s")
543 ; pathlauncher = "lp \"%s\""
544 ; selcmd =
545 (match platform with
546 | Plinux | Pfreebsd | Pdragonflybsd
547 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
548 | Posx -> "pbcopy"
549 | Pcygwin -> "wsel"
550 | Punknown -> "cat")
551 ; colorspace = Rgb
552 ; invert = false
553 ; colorscale = 1.0
554 ; redirectstderr = false
555 ; ghyllscroll = None
556 ; columns = Csingle [||]
557 ; beyecolumns = None
558 ; updatecurs = false
559 ; hfsize = 12
560 ; pgscale = 1.0
561 ; usepbo = false
562 ; wheelbypage = false
563 ; stcmd = "echo SyncTex"
564 ; riani = false
565 ; keyhashes =
566 let mk n = (n, Hashtbl.create 1) in
567 [ mk "global"
568 ; mk "info"
569 ; mk "help"
570 ; mk "outline"
571 ; mk "listview"
572 ; mk "birdseye"
573 ; mk "textentry"
574 ; mk "links"
575 ; mk "view"
580 let wtmode = ref false;;
582 let findkeyhash c name =
583 try List.assoc name c.keyhashes
584 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
587 let conf = { defconf with angle = defconf.angle };;
589 let pgscale h = truncate (float h *. conf.pgscale);;
591 type fontstate =
592 { mutable fontsize : int
593 ; mutable wwidth : float
594 ; mutable maxrows : int
598 let fstate =
599 { fontsize = 14
600 ; wwidth = nan
601 ; maxrows = -1
605 let geturl s =
606 let colonpos = try String.index s ':' with Not_found -> -1 in
607 let len = String.length s in
608 if colonpos >= 0 && colonpos + 3 < len
609 then (
610 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
611 then
612 let schemestartpos =
613 try String.rindex_from s colonpos ' '
614 with Not_found -> -1
616 let scheme =
617 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
619 match scheme with
620 | "http" | "ftp" | "mailto" ->
621 let epos =
622 try String.index_from s colonpos ' '
623 with Not_found -> len
625 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
626 | _ -> ""
627 else ""
629 else ""
632 let gotouri uri =
633 if String.length conf.urilauncher = 0
634 then print_endline uri
635 else (
636 let url = geturl uri in
637 if String.length url = 0
638 then Printf.eprintf "obtained empty url from uri %S" uri
639 else
640 let re = Str.regexp "%s" in
641 let command = Str.global_replace re url conf.urilauncher in
642 try popen command []
643 with exn ->
644 Printf.eprintf
645 "failed to execute `%s': %s\n" command (exntos exn);
646 flush stderr;
650 let version () =
651 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
652 (platform_to_string platform) Sys.word_size Sys.ocaml_version
655 let makehelp () =
656 let strings = version () :: "" :: Help.keys in
657 Array.of_list (
658 List.map (fun s ->
659 let url = geturl s in
660 if String.length url > 0
661 then (s, 0, Action (fun u -> gotouri url; u))
662 else (s, 0, Noaction)
663 ) strings);
666 let noghyll _ = ();;
667 let firstgeomcmds = "", [];;
668 let noreprf () = ();;
670 let state =
671 { sr = Unix.stdin
672 ; sw = Unix.stdin
673 ; wsfd = Unix.stdin
674 ; errfd = None
675 ; stderr = Unix.stderr
676 ; errmsgs = Buffer.create 0
677 ; newerrmsgs = false
678 ; x = 0
679 ; y = 0
680 ; w = 0
681 ; anchor = emptyanchor
682 ; ranchors = []
683 ; layout = []
684 ; maxy = max_int
685 ; tilelru = Queue.create ()
686 ; pagemap = Hashtbl.create 10
687 ; tilemap = Hashtbl.create 10
688 ; pdims = []
689 ; pagecount = 0
690 ; currently = Idle
691 ; mstate = Mnone
692 ; rects = []
693 ; rects1 = []
694 ; text = ""
695 ; mode = View
696 ; winstate = []
697 ; searchpattern = ""
698 ; outlines = [||]
699 ; bookmarks = []
700 ; path = ""
701 ; password = ""
702 ; nameddest = ""
703 ; geomcmds = firstgeomcmds
704 ; hists =
705 { nav = cbnew 10 emptyanchor
706 ; pat = cbnew 10 ""
707 ; pag = cbnew 10 ""
708 ; sel = cbnew 10 ""
710 ; memused = 0
711 ; gen = 0
712 ; throttle = None
713 ; autoscroll = None
714 ; ghyll = noghyll
715 ; help = makehelp ()
716 ; docinfo = []
717 ; texid = None
718 ; prevzoom = 1.0
719 ; progress = -1.0
720 ; uioh = nouioh
721 ; redisplay = true
722 ; mpos = (-1, -1)
723 ; keystate = KSnone
724 ; glinks = false
725 ; prevcolumns = None
726 ; winw = -1
727 ; winh = -1
728 ; reprf = noreprf
729 ; origin = ""
733 let hscrollh () =
734 if (conf.scrollb land scrollbhv = 0)
735 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
736 then 0
737 else conf.scrollbw
740 let vscrollw () =
741 if (conf.scrollb land scrollbvv = 0)
742 then 0
743 else conf.scrollbw
746 let wadjsb w = w - vscrollw ();;
748 let setfontsize n =
749 fstate.fontsize <- n;
750 fstate.wwidth <- measurestr fstate.fontsize "w";
751 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
754 let vlog fmt =
755 if conf.verbose
756 then
757 Printf.kprintf prerr_endline fmt
758 else
759 Printf.kprintf ignore fmt
762 let launchpath () =
763 if String.length conf.pathlauncher = 0
764 then print_endline state.path
765 else (
766 let re = Str.regexp "%s" in
767 let command = Str.global_replace re state.path conf.pathlauncher in
768 try popen command []
769 with exn ->
770 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
771 flush stderr;
775 module Ne = struct
776 type 'a t = | Res of 'a | Exn of exn;;
778 let pipe () =
779 try Res (Unix.pipe ())
780 with exn -> Exn exn
783 let clo fd f =
784 try tempfailureretry Unix.close fd
785 with exn -> f (exntos exn)
788 let dup fd =
789 try Res (tempfailureretry Unix.dup fd)
790 with exn -> Exn exn
793 let dup2 fd1 fd2 =
794 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
795 with exn -> Exn exn
797 end;;
799 let redirectstderr () =
800 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
801 if conf.redirectstderr
802 then
803 match Ne.pipe () with
804 | Ne.Exn exn ->
805 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
807 | Ne.Res (r, w) ->
808 begin match Ne.dup Unix.stderr with
809 | Ne.Exn exn ->
810 dolog "failed to dup stderr: %s" (exntos exn);
811 Ne.clo r (clofail "pipe/r");
812 Ne.clo w (clofail "pipe/w");
814 | Ne.Res dupstderr ->
815 begin match Ne.dup2 w Unix.stderr with
816 | Ne.Exn exn ->
817 dolog "failed to dup2 to stderr: %s" (exntos exn);
818 Ne.clo dupstderr (clofail "stderr duplicate");
819 Ne.clo r (clofail "redir pipe/r");
820 Ne.clo w (clofail "redir pipe/w");
822 | Ne.Res () ->
823 state.stderr <- dupstderr;
824 state.errfd <- Some r;
825 end;
827 else (
828 state.newerrmsgs <- false;
829 begin match state.errfd with
830 | Some fd ->
831 begin match Ne.dup2 state.stderr Unix.stderr with
832 | Ne.Exn exn ->
833 dolog "failed to dup2 original stderr: %s" (exntos exn)
834 | Ne.Res () ->
835 Ne.clo fd (clofail "dup of stderr");
836 state.errfd <- None;
837 end;
838 | None -> ()
839 end;
840 prerr_string (Buffer.contents state.errmsgs);
841 flush stderr;
842 Buffer.clear state.errmsgs;
846 module G =
847 struct
848 let postRedisplay who =
849 if conf.verbose
850 then prerr_endline ("redisplay for " ^ who);
851 state.redisplay <- true;
853 end;;
855 let getopaque pageno =
856 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
857 with Not_found -> None
860 let putopaque pageno opaque =
861 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
864 let pagetranslatepoint l x y =
865 let dy = y - l.pagedispy in
866 let y = dy + l.pagey in
867 let dx = x - l.pagedispx in
868 let x = dx + l.pagex in
869 (x, y);
872 let onppundermouse g x y d =
873 let rec f = function
874 | l :: rest ->
875 begin match getopaque l.pageno with
876 | Some opaque ->
877 let x0 = l.pagedispx in
878 let x1 = x0 + l.pagevw in
879 let y0 = l.pagedispy in
880 let y1 = y0 + l.pagevh in
881 if y >= y0 && y <= y1 && x >= x0 && x <= x1
882 then
883 let px, py = pagetranslatepoint l x y in
884 match g opaque l px py with
885 | Some res -> res
886 | None -> f rest
887 else f rest
888 | _ ->
889 f rest
891 | [] -> d
893 f state.layout
896 let getunder x y =
897 let g opaque _ px py =
898 match whatsunder opaque px py with
899 | Unone -> None
900 | under -> Some under
902 onppundermouse g x y Unone
905 let unproject x y =
906 let g opaque l x y =
907 match unproject opaque x y with
908 | Some (x, y) -> Some (Some (l.pageno, x, y))
909 | None -> None
911 onppundermouse g x y None;
914 let showtext c s =
915 state.text <- Printf.sprintf "%c%s" c s;
916 G.postRedisplay "showtext";
919 let selstring s =
920 match Ne.pipe () with
921 | Ne.Exn exn ->
922 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
923 | Ne.Res (r, w) ->
924 let popened =
925 try popen conf.selcmd [r, 0; w, -1]; true
926 with exn ->
927 showtext '!'
928 (Printf.sprintf "failed to execute %s: %s"
929 conf.selcmd (exntos exn));
930 false
932 let clo cap fd =
933 Ne.clo fd (fun msg ->
934 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
937 if popened
938 then
939 (try
940 let l = String.length s in
941 let n = tempfailureretry (Unix.write w s 0) l in
942 if n != l
943 then
944 showtext '!'
945 (Printf.sprintf
946 "failed to write %d characters to sel pipe, wrote %d"
949 with exn ->
950 showtext '!'
951 (Printf.sprintf "failed to write to sel pipe: %s"
952 (exntos exn)
955 else dolog "%s" s;
956 clo "pipe/r" r;
957 clo "pipe/w" w;
960 let undertext = function
961 | Unone -> "none"
962 | Ulinkuri s -> s
963 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
964 | Utext s -> "font: " ^ s
965 | Uunexpected s -> "unexpected: " ^ s
966 | Ulaunch s -> "launch: " ^ s
967 | Unamed s -> "named: " ^ s
968 | Uremote (filename, pageno) ->
969 Printf.sprintf "%s: page %d" filename (pageno+1)
972 let updateunder x y =
973 match getunder x y with
974 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
975 | Ulinkuri uri ->
976 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
977 Wsi.setcursor Wsi.CURSOR_INFO
978 | Ulinkgoto (pageno, _) ->
979 if conf.underinfo
980 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
981 Wsi.setcursor Wsi.CURSOR_INFO
982 | Utext s ->
983 if conf.underinfo then showtext 'f' ("ont: " ^ s);
984 Wsi.setcursor Wsi.CURSOR_TEXT
985 | Uunexpected s ->
986 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
987 Wsi.setcursor Wsi.CURSOR_INHERIT
988 | Ulaunch s ->
989 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
990 Wsi.setcursor Wsi.CURSOR_INHERIT
991 | Unamed s ->
992 if conf.underinfo then showtext 'n' ("amed: " ^ s);
993 Wsi.setcursor Wsi.CURSOR_INHERIT
994 | Uremote (filename, pageno) ->
995 if conf.underinfo then showtext 'r'
996 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
997 Wsi.setcursor Wsi.CURSOR_INFO
1000 let showlinktype under =
1001 if conf.underinfo
1002 then
1003 match under with
1004 | Unone -> ()
1005 | under ->
1006 let s = undertext under in
1007 showtext ' ' s
1010 let addchar s c =
1011 let b = Buffer.create (String.length s + 1) in
1012 Buffer.add_string b s;
1013 Buffer.add_char b c;
1014 Buffer.contents b;
1017 let colorspace_of_string s =
1018 match String.lowercase s with
1019 | "rgb" -> Rgb
1020 | "bgr" -> Bgr
1021 | "gray" -> Gray
1022 | _ -> failwith "invalid colorspace"
1025 let int_of_colorspace = function
1026 | Rgb -> 0
1027 | Bgr -> 1
1028 | Gray -> 2
1031 let colorspace_of_int = function
1032 | 0 -> Rgb
1033 | 1 -> Bgr
1034 | 2 -> Gray
1035 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1038 let colorspace_to_string = function
1039 | Rgb -> "rgb"
1040 | Bgr -> "bgr"
1041 | Gray -> "gray"
1044 let fitmodel_of_string s =
1045 match String.lowercase s with
1046 | "width" -> FitWidth
1047 | "proportional" -> FitProportional
1048 | "page" -> FitPage
1049 | _ -> failwith "invalid fit model"
1052 let int_of_fitmodel = function
1053 | FitWidth -> 0
1054 | FitProportional -> 1
1055 | FitPage -> 2
1058 let fitmodel_of_int = function
1059 | 0 -> FitWidth
1060 | 1 -> FitProportional
1061 | 2 -> FitPage
1062 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1065 let fitmodel_to_string = function
1066 | FitWidth -> "width"
1067 | FitProportional -> "proportional"
1068 | FitPage -> "page"
1071 let intentry_with_suffix text key =
1072 let c =
1073 if key >= 32 && key < 127
1074 then Char.chr key
1075 else '\000'
1077 match Char.lowercase c with
1078 | '0' .. '9' ->
1079 let text = addchar text c in
1080 TEcont text
1082 | 'k' | 'm' | 'g' ->
1083 let text = addchar text c in
1084 TEcont text
1086 | _ ->
1087 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1088 TEcont text
1091 let multicolumns_to_string (n, a, b) =
1092 if a = 0 && b = 0
1093 then Printf.sprintf "%d" n
1094 else Printf.sprintf "%d,%d,%d" n a b;
1097 let multicolumns_of_string s =
1099 (int_of_string s, 0, 0)
1100 with _ ->
1101 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1102 if a > 1 || b > 1
1103 then failwith "subtly broken"; (n, a, b)
1107 let readcmd fd =
1108 let s = "xxxx" in
1109 let n = tempfailureretry (Unix.read fd s 0) 4 in
1110 if n != 4 then failwith "incomplete read(len)";
1111 let len = 0
1112 lor (Char.code s.[0] lsl 24)
1113 lor (Char.code s.[1] lsl 16)
1114 lor (Char.code s.[2] lsl 8)
1115 lor (Char.code s.[3] lsl 0)
1117 let s = String.create len in
1118 let n = tempfailureretry (Unix.read fd s 0) len in
1119 if n != len then failwith "incomplete read(data)";
1123 let btod b = if b then 1 else 0;;
1125 let wcmd fmt =
1126 let b = Buffer.create 16 in
1127 Buffer.add_string b "llll";
1128 Printf.kbprintf
1129 (fun b ->
1130 let s = Buffer.contents b in
1131 let n = String.length s in
1132 let len = n - 4 in
1133 (* dolog "wcmd %S" (String.sub s 4 len); *)
1134 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1135 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1136 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1137 s.[3] <- Char.chr (len land 0xff);
1138 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1139 if n' != n then failwith "write failed";
1140 ) b fmt;
1143 let calcips h =
1144 let d = state.winh - h in
1145 max conf.interpagespace ((d + 1) / 2)
1148 let rowyh (c, coverA, coverB) b n =
1149 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1150 then
1151 let _, _, vy, (_, _, h, _) = b.(n) in
1152 (vy, h)
1153 else
1154 let n' = n - coverA in
1155 let d = n' mod c in
1156 let s = n - d in
1157 let e = min state.pagecount (s + c) in
1158 let rec find m miny maxh = if m = e then miny, maxh else
1159 let _, _, y, (_, _, h, _) = b.(m) in
1160 let miny = min miny y in
1161 let maxh = max maxh h in
1162 find (m+1) miny maxh
1163 in find s max_int 0
1166 let calcheight () =
1167 match conf.columns with
1168 | Cmulti ((_, _, _) as cl, b) ->
1169 if Array.length b > 0
1170 then
1171 let y, h = rowyh cl b (Array.length b - 1) in
1172 y + h + (if conf.presentation then calcips h else 0)
1173 else 0
1174 | Csingle b ->
1175 if Array.length b > 0
1176 then
1177 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1178 y + h + (if conf.presentation then calcips h else 0)
1179 else 0
1180 | Csplit (_, b) ->
1181 if Array.length b > 0
1182 then
1183 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1184 y + h
1185 else 0
1188 let getpageyh pageno =
1189 let pageno = bound pageno 0 (state.pagecount-1) in
1190 match conf.columns with
1191 | Csingle b ->
1192 if Array.length b = 0
1193 then 0, 0
1194 else
1195 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1196 let y =
1197 if conf.presentation
1198 then y - calcips h
1199 else y
1201 y, h
1202 | Cmulti (cl, b) ->
1203 if Array.length b = 0
1204 then 0, 0
1205 else
1206 let y, h = rowyh cl b pageno in
1207 let y =
1208 if conf.presentation
1209 then y - calcips h
1210 else y
1212 y, h
1213 | Csplit (c, b) ->
1214 if Array.length b = 0
1215 then 0, 0
1216 else
1217 let n = pageno*c in
1218 let (_, _, y, (_, _, h, _)) = b.(n) in
1219 y, h
1222 let getpagedim pageno =
1223 let rec f ppdim l =
1224 match l with
1225 | (n, _, _, _) as pdim :: rest ->
1226 if n >= pageno
1227 then (if n = pageno then pdim else ppdim)
1228 else f pdim rest
1230 | [] -> ppdim
1232 f (-1, -1, -1, -1) state.pdims
1235 let getpagey pageno = fst (getpageyh pageno);;
1237 let nogeomcmds cmds =
1238 match cmds with
1239 | s, [] -> String.length s = 0
1240 | _ -> false
1243 let page_of_y y =
1244 let ((c, coverA, coverB) as cl), b =
1245 match conf.columns with
1246 | Csingle b -> (1, 0, 0), b
1247 | Cmulti (c, b) -> c, b
1248 | Csplit (_, b) -> (1, 0, 0), b
1250 if Array.length b = 0
1251 then -1
1252 else
1253 let rec bsearch nmin nmax =
1254 if nmin > nmax
1255 then bound nmin 0 (state.pagecount-1)
1256 else
1257 let n = (nmax + nmin) / 2 in
1258 let vy, h = rowyh cl b n in
1259 let y0, y1 =
1260 if conf.presentation
1261 then
1262 let ips = calcips h in
1263 let y0 = vy - ips in
1264 let y1 = vy + h + ips in
1265 y0, y1
1266 else (
1267 if n = 0
1268 then 0, vy + h + conf.interpagespace
1269 else
1270 let y0 = vy - conf.interpagespace in
1271 y0, y0 + h + conf.interpagespace
1274 if y >= y0 && y < y1
1275 then (
1276 if c = 1
1277 then n
1278 else (
1279 if n > coverA
1280 then
1281 if n < state.pagecount - coverB
1282 then ((n-coverA)/c)*c + coverA
1283 else n
1284 else n
1287 else (
1288 if y > y0
1289 then bsearch (n+1) nmax
1290 else bsearch nmin (n-1)
1293 let r = bsearch 0 (state.pagecount-1) in
1297 let layoutN ((columns, coverA, coverB), b) y sh =
1298 let sh = sh - (hscrollh ()) in
1299 let rec fold accu n =
1300 if n = Array.length b
1301 then accu
1302 else
1303 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1304 if (vy - y) > sh &&
1305 (n = coverA - 1
1306 || n = state.pagecount - coverB
1307 || (n - coverA) mod columns = columns - 1)
1308 then accu
1309 else
1310 let accu =
1311 if vy + h > y
1312 then
1313 let pagey = max 0 (y - vy) in
1314 let pagedispy = if pagey > 0 then 0 else vy - y in
1315 let pagedispx, pagex =
1316 let pdx =
1317 if n = coverA - 1 || n = state.pagecount - coverB
1318 then state.x + (wadjsb state.winw - w) / 2
1319 else dx + xoff + state.x
1321 if pdx < 0
1322 then 0, -pdx
1323 else pdx, 0
1325 let pagevw =
1326 let vw = wadjsb state.winw - pagedispx in
1327 let pw = w - pagex in
1328 min vw pw
1330 let pagevh = min (h - pagey) (sh - pagedispy) in
1331 if pagevw > 0 && pagevh > 0
1332 then
1333 let e =
1334 { pageno = n
1335 ; pagedimno = pdimno
1336 ; pagew = w
1337 ; pageh = h
1338 ; pagex = pagex
1339 ; pagey = pagey
1340 ; pagevw = pagevw
1341 ; pagevh = pagevh
1342 ; pagedispx = pagedispx
1343 ; pagedispy = pagedispy
1344 ; pagecol = 0
1347 e :: accu
1348 else
1349 accu
1350 else
1351 accu
1353 fold accu (n+1)
1355 List.rev (fold [] (page_of_y y));
1358 let layoutS (columns, b) y sh =
1359 let sh = sh - hscrollh () in
1360 let rec fold accu n =
1361 if n = Array.length b
1362 then accu
1363 else
1364 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1365 if (vy - y) > sh
1366 then accu
1367 else
1368 let accu =
1369 if vy + pageh > y
1370 then
1371 let x = xoff + state.x in
1372 let pagey = max 0 (y - vy) in
1373 let pagedispy = if pagey > 0 then 0 else vy - y in
1374 let pagedispx, pagex =
1375 if px = 0
1376 then (
1377 if x < 0
1378 then 0, -x
1379 else x, 0
1381 else (
1382 let px = px - x in
1383 if px < 0
1384 then -px, 0
1385 else 0, px
1388 let pagecolw = pagew/columns in
1389 let pagedispx =
1390 if pagecolw < state.winw
1391 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1392 else pagedispx
1394 let pagevw =
1395 let vw = wadjsb state.winw - pagedispx in
1396 let pw = pagew - pagex in
1397 min vw pw
1399 let pagevw = min pagevw pagecolw in
1400 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1401 if pagevw > 0 && pagevh > 0
1402 then
1403 let e =
1404 { pageno = n/columns
1405 ; pagedimno = pdimno
1406 ; pagew = pagew
1407 ; pageh = pageh
1408 ; pagex = pagex
1409 ; pagey = pagey
1410 ; pagevw = pagevw
1411 ; pagevh = pagevh
1412 ; pagedispx = pagedispx
1413 ; pagedispy = pagedispy
1414 ; pagecol = n mod columns
1417 e :: accu
1418 else
1419 accu
1420 else
1421 accu
1423 fold accu (n+1)
1425 List.rev (fold [] 0)
1428 let layout y sh =
1429 if nogeomcmds state.geomcmds
1430 then
1431 match conf.columns with
1432 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1433 | Cmulti c -> layoutN c y sh
1434 | Csplit s -> layoutS s y sh
1435 else []
1438 let clamp incr =
1439 let y = state.y + incr in
1440 let y = max 0 y in
1441 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1445 let itertiles l f =
1446 let tilex = l.pagex mod conf.tilew in
1447 let tiley = l.pagey mod conf.tileh in
1449 let col = l.pagex / conf.tilew in
1450 let row = l.pagey / conf.tileh in
1452 let rec rowloop row y0 dispy h =
1453 if h = 0
1454 then ()
1455 else (
1456 let dh = conf.tileh - y0 in
1457 let dh = min h dh in
1458 let rec colloop col x0 dispx w =
1459 if w = 0
1460 then ()
1461 else (
1462 let dw = conf.tilew - x0 in
1463 let dw = min w dw in
1465 f col row dispx dispy x0 y0 dw dh;
1466 colloop (col+1) 0 (dispx+dw) (w-dw)
1469 colloop col tilex l.pagedispx l.pagevw;
1470 rowloop (row+1) 0 (dispy+dh) (h-dh)
1473 if l.pagevw > 0 && l.pagevh > 0
1474 then rowloop row tiley l.pagedispy l.pagevh;
1477 let gettileopaque l col row =
1478 let key =
1479 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1481 try Some (Hashtbl.find state.tilemap key)
1482 with Not_found -> None
1485 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1486 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1487 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1490 let drawtiles l color =
1491 GlDraw.color color;
1492 let f col row x y tilex tiley w h =
1493 match gettileopaque l col row with
1494 | Some (opaque, _, t) ->
1495 let params = x, y, w, h, tilex, tiley in
1496 if conf.invert
1497 then (
1498 Gl.enable `blend;
1499 GlFunc.blend_func `zero `one_minus_src_color;
1501 drawtile params opaque;
1502 if conf.invert
1503 then Gl.disable `blend;
1504 if conf.debug
1505 then (
1506 let s = Printf.sprintf
1507 "%d[%d,%d] %f sec"
1508 l.pageno col row t
1510 let w = measurestr fstate.fontsize s in
1511 GlMisc.push_attrib [`current];
1512 GlDraw.color (0.0, 0.0, 0.0);
1513 GlDraw.rect
1514 (float (x-2), float (y-2))
1515 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1516 GlDraw.color (1.0, 1.0, 1.0);
1517 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1518 GlMisc.pop_attrib ();
1521 | _ ->
1522 let w =
1523 let lw = wadjsb state.winw - x in
1524 min lw w
1525 and h =
1526 let lh = state.winh - y in
1527 min lh h
1529 begin match state.texid with
1530 | Some id ->
1531 Gl.enable `texture_2d;
1532 GlTex.bind_texture `texture_2d id;
1533 let x0 = float x
1534 and y0 = float y
1535 and x1 = float (x+w)
1536 and y1 = float (y+h) in
1538 let tw = float w /. 16.0
1539 and th = float h /. 16.0 in
1540 let tx0 = float tilex /. 16.0
1541 and ty0 = float tiley /. 16.0 in
1542 let tx1 = tx0 +. tw
1543 and ty1 = ty0 +. th in
1544 GlDraw.begins `quads;
1545 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1546 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1547 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1548 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1549 GlDraw.ends ();
1551 Gl.disable `texture_2d;
1552 | None ->
1553 GlDraw.color (1.0, 1.0, 1.0);
1554 GlDraw.rect
1555 (float x, float y)
1556 (float (x+w), float (y+h));
1557 end;
1558 if w > 128 && h > fstate.fontsize + 10
1559 then (
1560 GlDraw.color (0.0, 0.0, 0.0);
1561 let c, r =
1562 if conf.verbose
1563 then (col*conf.tilew, row*conf.tileh)
1564 else col, row
1566 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1568 GlDraw.color color;
1570 itertiles l f
1573 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1575 let tilevisible1 l x y =
1576 let ax0 = l.pagex
1577 and ax1 = l.pagex + l.pagevw
1578 and ay0 = l.pagey
1579 and ay1 = l.pagey + l.pagevh in
1581 let bx0 = x
1582 and by0 = y in
1583 let bx1 = min (bx0 + conf.tilew) l.pagew
1584 and by1 = min (by0 + conf.tileh) l.pageh in
1586 let rx0 = max ax0 bx0
1587 and ry0 = max ay0 by0
1588 and rx1 = min ax1 bx1
1589 and ry1 = min ay1 by1 in
1591 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1592 nonemptyintersection
1595 let tilevisible layout n x y =
1596 let rec findpageinlayout m = function
1597 | l :: rest when l.pageno = n ->
1598 tilevisible1 l x y || (
1599 match conf.columns with
1600 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1601 | _ -> false
1603 | _ :: rest -> findpageinlayout 0 rest
1604 | [] -> false
1606 findpageinlayout 0 layout;
1609 let tileready l x y =
1610 tilevisible1 l x y &&
1611 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1614 let tilepage n p layout =
1615 let rec loop = function
1616 | l :: rest ->
1617 if l.pageno = n
1618 then
1619 let f col row _ _ _ _ _ _ =
1620 if state.currently = Idle
1621 then
1622 match gettileopaque l col row with
1623 | Some _ -> ()
1624 | None ->
1625 let x = col*conf.tilew
1626 and y = row*conf.tileh in
1627 let w =
1628 let w = l.pagew - x in
1629 min w conf.tilew
1631 let h =
1632 let h = l.pageh - y in
1633 min h conf.tileh
1635 let pbo =
1636 if conf.usepbo
1637 then getpbo w h conf.colorspace
1638 else "0"
1640 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1641 state.currently <-
1642 Tiling (
1643 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1644 conf.tilew, conf.tileh
1647 itertiles l f;
1648 else
1649 loop rest
1651 | [] -> ()
1653 if nogeomcmds state.geomcmds
1654 then loop layout;
1657 let preloadlayout y =
1658 let y = if y < state.winh then 0 else y - state.winh in
1659 let h = state.winh*3 in
1660 layout y h;
1663 let load pages =
1664 let rec loop pages =
1665 if state.currently != Idle
1666 then ()
1667 else
1668 match pages with
1669 | l :: rest ->
1670 begin match getopaque l.pageno with
1671 | None ->
1672 wcmd "page %d %d" l.pageno l.pagedimno;
1673 state.currently <- Loading (l, state.gen);
1674 | Some opaque ->
1675 tilepage l.pageno opaque pages;
1676 loop rest
1677 end;
1678 | _ -> ()
1680 if nogeomcmds state.geomcmds
1681 then loop pages
1684 let preload pages =
1685 load pages;
1686 if conf.preload && state.currently = Idle
1687 then load (preloadlayout state.y);
1690 let layoutready layout =
1691 let rec fold all ls =
1692 all && match ls with
1693 | l :: rest ->
1694 let seen = ref false in
1695 let allvisible = ref true in
1696 let foo col row _ _ _ _ _ _ =
1697 seen := true;
1698 allvisible := !allvisible &&
1699 begin match gettileopaque l col row with
1700 | Some _ -> true
1701 | None -> false
1704 itertiles l foo;
1705 fold (!seen && !allvisible) rest
1706 | [] -> true
1708 let alltilesvisible = fold true layout in
1709 alltilesvisible;
1712 let gotoy y =
1713 let y = bound y 0 state.maxy in
1714 let y, layout, proceed =
1715 match conf.maxwait with
1716 | Some time when state.ghyll == noghyll ->
1717 begin match state.throttle with
1718 | None ->
1719 let layout = layout y state.winh in
1720 let ready = layoutready layout in
1721 if not ready
1722 then (
1723 load layout;
1724 state.throttle <- Some (layout, y, now ());
1726 else G.postRedisplay "gotoy showall (None)";
1727 y, layout, ready
1728 | Some (_, _, started) ->
1729 let dt = now () -. started in
1730 if dt > time
1731 then (
1732 state.throttle <- None;
1733 let layout = layout y state.winh in
1734 load layout;
1735 G.postRedisplay "maxwait";
1736 y, layout, true
1738 else -1, [], false
1741 | _ ->
1742 let layout = layout y state.winh in
1743 if not !wtmode || layoutready layout
1744 then G.postRedisplay "gotoy ready";
1745 y, layout, true
1747 if proceed
1748 then (
1749 state.y <- y;
1750 state.layout <- layout;
1751 begin match state.mode with
1752 | LinkNav (Ltexact (pageno, linkno)) ->
1753 let rec loop = function
1754 | [] ->
1755 state.mode <- LinkNav (Ltgendir 0)
1756 | l :: _ when l.pageno = pageno ->
1757 begin match getopaque pageno with
1758 | None ->
1759 state.mode <- LinkNav (Ltgendir 0)
1760 | Some opaque ->
1761 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1762 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1763 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1764 then state.mode <- LinkNav (Ltgendir 0)
1766 | _ :: rest -> loop rest
1768 loop layout
1769 | _ -> ()
1770 end;
1771 begin match state.mode with
1772 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1773 if not (pagevisible layout pageno)
1774 then (
1775 match state.layout with
1776 | [] -> ()
1777 | l :: _ ->
1778 state.mode <- Birdseye (
1779 conf, leftx, l.pageno, hooverpageno, anchor
1782 | LinkNav (Ltgendir dir as lt) ->
1783 let linknav =
1784 let rec loop = function
1785 | [] -> lt
1786 | l :: rest ->
1787 match getopaque l.pageno with
1788 | None -> loop rest
1789 | Some opaque ->
1790 let link =
1791 let ld =
1792 if dir = 0
1793 then LDfirstvisible (l.pagex, l.pagey, dir)
1794 else (
1795 if dir > 0 then LDfirst else LDlast
1798 findlink opaque ld
1800 match link with
1801 | Lnotfound -> loop rest
1802 | Lfound n ->
1803 showlinktype (getlink opaque n);
1804 Ltexact (l.pageno, n)
1806 loop state.layout
1808 state.mode <- LinkNav linknav
1809 | _ -> ()
1810 end;
1811 preload layout;
1813 state.ghyll <- noghyll;
1814 if conf.updatecurs
1815 then (
1816 let mx, my = state.mpos in
1817 updateunder mx my;
1821 let conttiling pageno opaque =
1822 tilepage pageno opaque
1823 (if conf.preload then preloadlayout state.y else state.layout)
1826 let gotoy_and_clear_text y =
1827 if not conf.verbose then state.text <- "";
1828 gotoy y;
1831 let getanchor1 l =
1832 let top =
1833 let coloff = l.pagecol * l.pageh in
1834 float (l.pagey + coloff) /. float l.pageh
1836 let dtop =
1837 if l.pagedispy = 0
1838 then
1840 else (
1841 if conf.presentation
1842 then float l.pagedispy /. float (calcips l.pageh)
1843 else float l.pagedispy /. float conf.interpagespace
1846 (l.pageno, top, dtop)
1849 let getanchor () =
1850 match state.layout with
1851 | l :: _ -> getanchor1 l
1852 | [] ->
1853 let n = page_of_y state.y in
1854 if n = -1
1855 then state.anchor
1856 else
1857 let y, h = getpageyh n in
1858 let dy = y - state.y in
1859 let dtop =
1860 if conf.presentation
1861 then
1862 let ips = calcips h in
1863 float (dy + ips) /. float ips
1864 else
1865 float dy /. float conf.interpagespace
1867 (n, 0.0, dtop)
1870 let getanchory (n, top, dtop) =
1871 let y, h = getpageyh n in
1872 if conf.presentation
1873 then
1874 let ips = calcips h in
1875 y + truncate (top*.float h -. dtop*.float ips) + ips;
1876 else
1877 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1880 let gotoanchor anchor =
1881 gotoy (getanchory anchor);
1884 let addnav () =
1885 cbput state.hists.nav (getanchor ());
1888 let getnav dir =
1889 let anchor = cbgetc state.hists.nav dir in
1890 getanchory anchor;
1893 let gotoghyll y =
1894 let scroll f n a b =
1895 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1896 let snake f a b =
1897 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1898 if f < a
1899 then s (float f /. float a)
1900 else (
1901 if f > b
1902 then 1.0 -. s ((float (f-b) /. float (n-b)))
1903 else 1.0
1906 snake f a b
1907 and summa f n a b =
1908 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1909 let iv x = x**3.-.0.5*.x**4. in
1910 let iv1 = iv f in
1911 let ins = float a *. iv1
1912 and outs = float (n-b) *. iv1 in
1913 let ones = b - a in
1914 ins +. outs +. float ones
1916 let rec set (_N, _A, _B) y sy =
1917 let sum = summa 1.0 _N _A _B in
1918 let dy = float (y - sy) in
1919 state.ghyll <- (
1920 let rec gf n y1 o =
1921 if n >= _N
1922 then state.ghyll <- noghyll
1923 else
1924 let go n =
1925 let s = scroll n _N _A _B in
1926 let y1 = y1 +. ((s *. dy) /. sum) in
1927 gotoy_and_clear_text (truncate y1);
1928 state.ghyll <- gf (n+1) y1;
1930 match o with
1931 | None -> go n
1932 | Some y' -> set (_N/2, 1, 1) y' state.y
1934 gf 0 (float state.y)
1937 match conf.ghyllscroll with
1938 | None ->
1939 gotoy_and_clear_text y
1940 | Some nab ->
1941 if state.ghyll == noghyll
1942 then set nab y state.y
1943 else state.ghyll (Some y)
1946 let gotopage n top =
1947 let y, h = getpageyh n in
1948 let y = y + (truncate (top *. float h)) in
1949 gotoghyll y
1952 let gotopage1 n top =
1953 let y = getpagey n in
1954 let y = y + top in
1955 gotoghyll y
1958 let invalidate s f =
1959 state.layout <- [];
1960 state.pdims <- [];
1961 state.rects <- [];
1962 state.rects1 <- [];
1963 match state.geomcmds with
1964 | ps, [] when String.length ps = 0 ->
1965 f ();
1966 state.geomcmds <- s, [];
1968 | ps, [] ->
1969 state.geomcmds <- ps, [s, f];
1971 | ps, (s', _) :: rest when s' = s ->
1972 state.geomcmds <- ps, ((s, f) :: rest);
1974 | ps, cmds ->
1975 state.geomcmds <- ps, ((s, f) :: cmds);
1978 let flushpages () =
1979 Hashtbl.iter (fun _ opaque ->
1980 wcmd "freepage %s" opaque;
1981 ) state.pagemap;
1982 Hashtbl.clear state.pagemap;
1985 let flushtiles () =
1986 if not (Queue.is_empty state.tilelru)
1987 then (
1988 Queue.iter (fun (k, p, s) ->
1989 wcmd "freetile %s" p;
1990 state.memused <- state.memused - s;
1991 Hashtbl.remove state.tilemap k;
1992 ) state.tilelru;
1993 state.uioh#infochanged Memused;
1994 Queue.clear state.tilelru;
1996 load state.layout;
1999 let stateh h =
2000 let h = truncate (float h*.conf.zoom) in
2001 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2002 h - d
2005 let opendoc path password =
2006 state.path <- path;
2007 state.password <- password;
2008 state.gen <- state.gen + 1;
2009 state.docinfo <- [];
2011 flushpages ();
2012 setaalevel conf.aalevel;
2013 let titlepath =
2014 if String.length state.origin = 0
2015 then path
2016 else state.origin
2018 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2019 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2020 invalidate "reqlayout"
2021 (fun () ->
2022 wcmd "reqlayout %d %d %d %s\000"
2023 conf.angle (int_of_fitmodel conf.fitmodel)
2024 (stateh state.winh) state.nameddest
2028 let reload () =
2029 state.anchor <- getanchor ();
2030 opendoc state.path state.password;
2033 let scalecolor c =
2034 let c = c *. conf.colorscale in
2035 (c, c, c);
2038 let scalecolor2 (r, g, b) =
2039 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2042 let docolumns = function
2043 | Csingle _ ->
2044 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2045 let rec loop pageno pdimno pdim y ph pdims =
2046 if pageno = state.pagecount
2047 then ()
2048 else
2049 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2050 match pdims with
2051 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2052 pdimno+1, pdim, rest
2053 | _ ->
2054 pdimno, pdim, pdims
2056 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2057 let y = y +
2058 (if conf.presentation
2059 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2060 else (if pageno = 0 then 0 else conf.interpagespace)
2063 a.(pageno) <- (pdimno, x, y, pdim);
2064 loop (pageno+1) pdimno pdim (y + h) h pdims
2066 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2067 conf.columns <- Csingle a;
2069 | Cmulti ((columns, coverA, coverB), _) ->
2070 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2071 let rec loop pageno pdimno pdim x y rowh pdims =
2072 let rec fixrow m = if m = pageno then () else
2073 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2074 if h < rowh
2075 then (
2076 let y = y + (rowh - h) / 2 in
2077 a.(m) <- (pdimno, x, y, pdim);
2079 fixrow (m+1)
2081 if pageno = state.pagecount
2082 then fixrow (((pageno - 1) / columns) * columns)
2083 else
2084 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2085 match pdims with
2086 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2087 pdimno+1, pdim, rest
2088 | _ ->
2089 pdimno, pdim, pdims
2091 let x, y, rowh' =
2092 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2093 then (
2094 let x = (wadjsb state.winw - w) / 2 in
2095 let ips =
2096 if conf.presentation then calcips h else conf.interpagespace in
2097 x, y + ips + rowh, h
2099 else (
2100 if (pageno - coverA) mod columns = 0
2101 then (
2102 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2103 let y =
2104 if conf.presentation
2105 then
2106 let ips = calcips h in
2107 y + (if pageno = 0 then 0 else calcips rowh + ips)
2108 else
2109 y + (if pageno = 0 then 0 else conf.interpagespace)
2111 x, y + rowh, h
2113 else x, y, max rowh h
2116 let y =
2117 if pageno > 1 && (pageno - coverA) mod columns = 0
2118 then (
2119 let y =
2120 if pageno = columns && conf.presentation
2121 then (
2122 let ips = calcips rowh in
2123 for i = 0 to pred columns
2125 let (pdimno, x, y, pdim) = a.(i) in
2126 a.(i) <- (pdimno, x, y+ips, pdim)
2127 done;
2128 y+ips;
2130 else y
2132 fixrow (pageno - columns);
2135 else y
2137 a.(pageno) <- (pdimno, x, y, pdim);
2138 let x = x + w + xoff*2 + conf.interpagespace in
2139 loop (pageno+1) pdimno pdim x y rowh' pdims
2141 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2142 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2144 | Csplit (c, _) ->
2145 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2146 let rec loop pageno pdimno pdim y pdims =
2147 if pageno = state.pagecount
2148 then ()
2149 else
2150 let pdimno, ((_, w, h, _) as pdim), pdims =
2151 match pdims with
2152 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2153 pdimno+1, pdim, rest
2154 | _ ->
2155 pdimno, pdim, pdims
2157 let cw = w / c in
2158 let rec loop1 n x y =
2159 if n = c then y else (
2160 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2161 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2164 let y = loop1 0 0 y in
2165 loop (pageno+1) pdimno pdim y pdims
2167 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2168 conf.columns <- Csplit (c, a);
2171 let represent () =
2172 docolumns conf.columns;
2173 state.maxy <- calcheight ();
2174 if state.reprf == noreprf
2175 then (
2176 match state.mode with
2177 | Birdseye (_, _, pageno, _, _) ->
2178 let y, h = getpageyh pageno in
2179 let top = (state.winh - h) / 2 in
2180 gotoy (max 0 (y - top))
2181 | _ -> gotoanchor state.anchor
2183 else (
2184 state.reprf ();
2185 state.reprf <- noreprf;
2189 let reshape w h =
2190 GlDraw.viewport 0 0 w h;
2191 let firsttime = state.geomcmds == firstgeomcmds in
2192 if not firsttime && nogeomcmds state.geomcmds
2193 then state.anchor <- getanchor ();
2195 state.winw <- w;
2196 let w = wadjsb (truncate (float w *. conf.zoom)) in
2197 let w = max w 2 in
2198 state.winh <- h;
2199 setfontsize fstate.fontsize;
2200 GlMat.mode `modelview;
2201 GlMat.load_identity ();
2203 GlMat.mode `projection;
2204 GlMat.load_identity ();
2205 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2206 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2207 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2209 let relx =
2210 if conf.zoom <= 1.0
2211 then 0.0
2212 else float state.x /. float state.w
2214 invalidate "geometry"
2215 (fun () ->
2216 state.w <- w;
2217 if not firsttime
2218 then state.x <- truncate (relx *. float w);
2219 let w =
2220 match conf.columns with
2221 | Csingle _ -> w
2222 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2223 | Csplit (c, _) -> w * c
2225 wcmd "geometry %d %d %d"
2226 w (stateh h) (int_of_fitmodel conf.fitmodel)
2230 let enttext () =
2231 let len = String.length state.text in
2232 let drawstring s =
2233 let hscrollh =
2234 match state.mode with
2235 | Textentry _ | View | LinkNav _ ->
2236 let h, _, _ = state.uioh#scrollpw in
2238 | _ -> 0
2240 let rect x w =
2241 GlDraw.rect
2242 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2243 (x+.w, float (state.winh - hscrollh))
2246 let w = float (wadjsb state.winw - 1) in
2247 if state.progress >= 0.0 && state.progress < 1.0
2248 then (
2249 GlDraw.color (0.3, 0.3, 0.3);
2250 let w1 = w *. state.progress in
2251 rect 0.0 w1;
2252 GlDraw.color (0.0, 0.0, 0.0);
2253 rect w1 (w-.w1)
2255 else (
2256 GlDraw.color (0.0, 0.0, 0.0);
2257 rect 0.0 w;
2260 GlDraw.color (1.0, 1.0, 1.0);
2261 drawstring fstate.fontsize
2262 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2264 let s =
2265 match state.mode with
2266 | Textentry ((prefix, text, _, _, _, _), _) ->
2267 let s =
2268 if len > 0
2269 then
2270 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2271 else
2272 Printf.sprintf "%s%s_" prefix text
2276 | _ -> state.text
2278 let s =
2279 if state.newerrmsgs
2280 then (
2281 if not (istextentry state.mode) && state.uioh#eformsgs
2282 then
2283 let s1 = "(press 'e' to review error messasges)" in
2284 if String.length s > 0 then s ^ " " ^ s1 else s1
2285 else s
2287 else s
2289 if String.length s > 0
2290 then drawstring s
2293 let gctiles () =
2294 let len = Queue.length state.tilelru in
2295 let layout = lazy (
2296 match state.throttle with
2297 | None ->
2298 if conf.preload
2299 then preloadlayout state.y
2300 else state.layout
2301 | Some (layout, _, _) ->
2302 layout
2303 ) in
2304 let rec loop qpos =
2305 if state.memused <= conf.memlimit
2306 then ()
2307 else (
2308 if qpos < len
2309 then
2310 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2311 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2312 let (_, pw, ph, _) = getpagedim n in
2314 gen = state.gen
2315 && colorspace = conf.colorspace
2316 && angle = conf.angle
2317 && pagew = pw
2318 && pageh = ph
2319 && (
2320 let x = col*conf.tilew
2321 and y = row*conf.tileh in
2322 tilevisible (Lazy.force_val layout) n x y
2324 then Queue.push lruitem state.tilelru
2325 else (
2326 freepbo p;
2327 wcmd "freetile %s" p;
2328 state.memused <- state.memused - s;
2329 state.uioh#infochanged Memused;
2330 Hashtbl.remove state.tilemap k;
2332 loop (qpos+1)
2335 loop 0
2338 let logcurrently = function
2339 | Idle -> dolog "Idle"
2340 | Loading (l, gen) ->
2341 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2342 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2343 dolog
2344 "Tiling %d[%d,%d] page=%s cs=%s angle"
2345 l.pageno col row pageopaque
2346 (colorspace_to_string colorspace)
2348 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2349 angle gen conf.angle state.gen
2350 tilew tileh
2351 conf.tilew conf.tileh
2353 | Outlining _ ->
2354 dolog "outlining"
2357 let splitatspace =
2358 let r = Str.regexp " " in
2359 fun s -> Str.bounded_split r s 2;
2362 let onpagerect pageno f =
2363 let b =
2364 match conf.columns with
2365 | Cmulti (_, b) -> b
2366 | Csingle b -> b
2367 | Csplit (_, b) -> b
2369 if pageno >= 0 && pageno < Array.length b
2370 then
2371 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2372 let r = getpdimrect pdimno in
2373 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2376 let gotopagexy1 pageno x y =
2377 onpagerect pageno (fun w h ->
2378 let top = y /. h in
2379 let _,w1,_,leftx = getpagedim pageno in
2380 let wh = state.winh - hscrollh () in
2381 let sw = float w1 /. w in
2382 let x = sw *. x in
2383 let x = leftx + state.x + truncate x in
2384 let sx =
2385 if x < 0 || x >= wadjsb state.winw
2386 then state.x - x
2387 else state.x
2389 let py, h = getpageyh pageno in
2390 let pdy = truncate (top *. float h) in
2391 let y' = py + pdy in
2392 let dy = y' - state.y in
2393 let sy =
2394 if x != state.x || not (dy > 0 && dy < wh)
2395 then (
2396 if conf.presentation
2397 then
2398 if abs (py - y') > wh
2399 then y'
2400 else py
2401 else y';
2403 else state.y
2405 if state.x != sx || state.y != sy
2406 then (
2407 let x, y =
2408 if !wtmode
2409 then (
2410 let ww = wadjsb state.winw in
2411 let qx = sx / ww
2412 and qy = pdy / wh in
2413 let x = qx * ww
2414 and y = py + qy * wh in
2415 let x = if -x + ww > w1 then -(w1-ww) else x
2416 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2417 let y =
2418 if conf.presentation
2419 then
2420 if abs (py - y') > wh
2421 then y'
2422 else py
2423 else y';
2425 (x, y)
2427 else (sx, sy)
2429 state.x <- x;
2430 gotoy_and_clear_text y;
2432 else gotoy_and_clear_text state.y;
2436 let gotopagexy pageno x y =
2437 match state.mode with
2438 | Birdseye _ -> gotopage pageno 0.0
2439 | _ -> gotopagexy1 pageno x y
2442 let act cmds =
2443 (* dolog "%S" cmds; *)
2444 let cl = splitatspace cmds in
2445 let scan s fmt f =
2446 try Scanf.sscanf s fmt f
2447 with exn ->
2448 dolog "error processing '%S': %s" cmds (exntos exn);
2449 exit 1
2451 match cl with
2452 | "clear" :: [] ->
2453 state.uioh#infochanged Pdim;
2454 state.pdims <- [];
2456 | "clearrects" :: [] ->
2457 state.rects <- state.rects1;
2458 G.postRedisplay "clearrects";
2460 | "continue" :: args :: [] ->
2461 let n = scan args "%u" (fun n -> n) in
2462 state.pagecount <- n;
2463 begin match state.currently with
2464 | Outlining l ->
2465 state.currently <- Idle;
2466 state.outlines <- Array.of_list (List.rev l)
2467 | _ -> ()
2468 end;
2470 let cur, cmds = state.geomcmds in
2471 if String.length cur = 0
2472 then failwith "umpossible";
2474 begin match List.rev cmds with
2475 | [] ->
2476 state.geomcmds <- "", [];
2477 represent ();
2478 | (s, f) :: rest ->
2479 f ();
2480 state.geomcmds <- s, List.rev rest;
2481 end;
2482 if conf.maxwait = None && not !wtmode
2483 then G.postRedisplay "continue";
2485 | "title" :: args :: [] ->
2486 Wsi.settitle args
2488 | "msg" :: args :: [] ->
2489 showtext ' ' args
2491 | "vmsg" :: args :: [] ->
2492 if conf.verbose
2493 then showtext ' ' args
2495 | "emsg" :: args :: [] ->
2496 Buffer.add_string state.errmsgs args;
2497 state.newerrmsgs <- true;
2498 G.postRedisplay "error message"
2500 | "progress" :: args :: [] ->
2501 let progress, text =
2502 scan args "%f %n"
2503 (fun f pos ->
2504 f, String.sub args pos (String.length args - pos))
2506 state.text <- text;
2507 state.progress <- progress;
2508 G.postRedisplay "progress"
2510 | "firstmatch" :: args :: [] ->
2511 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2512 scan args "%u %d %f %f %f %f %f %f %f %f"
2513 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2514 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2516 let y = (getpagey pageno) + truncate y0 in
2517 addnav ();
2518 gotoy y;
2519 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2521 | "match" :: args :: [] ->
2522 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2523 scan args "%u %d %f %f %f %f %f %f %f %f"
2524 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2525 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2527 state.rects1 <-
2528 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2530 | "page" :: args :: [] ->
2531 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2532 begin match state.currently with
2533 | Loading (l, gen) ->
2534 vlog "page %d took %f sec" l.pageno t;
2535 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2536 begin match state.throttle with
2537 | None ->
2538 let preloadedpages =
2539 if conf.preload
2540 then preloadlayout state.y
2541 else state.layout
2543 let evict () =
2544 let set =
2545 List.fold_left (fun s l -> IntSet.add l.pageno s)
2546 IntSet.empty preloadedpages
2548 let evictedpages =
2549 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2550 if not (IntSet.mem pageno set)
2551 then (
2552 wcmd "freepage %s" opaque;
2553 key :: accu
2555 else accu
2556 ) state.pagemap []
2558 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2560 evict ();
2561 state.currently <- Idle;
2562 if gen = state.gen
2563 then (
2564 tilepage l.pageno pageopaque state.layout;
2565 load state.layout;
2566 load preloadedpages;
2567 if pagevisible state.layout l.pageno
2568 && layoutready state.layout
2569 then G.postRedisplay "page";
2572 | Some (layout, _, _) ->
2573 state.currently <- Idle;
2574 tilepage l.pageno pageopaque layout;
2575 load state.layout
2576 end;
2578 | _ ->
2579 dolog "Inconsistent loading state";
2580 logcurrently state.currently;
2581 exit 1
2584 | "tile" :: args :: [] ->
2585 let (x, y, opaque, size, t) =
2586 scan args "%u %u %s %u %f"
2587 (fun x y p size t -> (x, y, p, size, t))
2589 begin match state.currently with
2590 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2591 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2593 unmappbo opaque;
2594 if tilew != conf.tilew || tileh != conf.tileh
2595 then (
2596 wcmd "freetile %s" opaque;
2597 state.currently <- Idle;
2598 load state.layout;
2600 else (
2601 puttileopaque l col row gen cs angle opaque size t;
2602 state.memused <- state.memused + size;
2603 state.uioh#infochanged Memused;
2604 gctiles ();
2605 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2606 opaque, size) state.tilelru;
2608 let layout =
2609 match state.throttle with
2610 | None -> state.layout
2611 | Some (layout, _, _) -> layout
2614 state.currently <- Idle;
2615 if gen = state.gen
2616 && conf.colorspace = cs
2617 && conf.angle = angle
2618 && tilevisible layout l.pageno x y
2619 then conttiling l.pageno pageopaque;
2621 begin match state.throttle with
2622 | None ->
2623 preload state.layout;
2624 if gen = state.gen
2625 && conf.colorspace = cs
2626 && conf.angle = angle
2627 && tilevisible state.layout l.pageno x y
2628 && (not !wtmode || layoutready state.layout)
2629 then G.postRedisplay "tile nothrottle";
2631 | Some (layout, y, _) ->
2632 let ready = layoutready layout in
2633 if ready
2634 then (
2635 state.y <- y;
2636 state.layout <- layout;
2637 state.throttle <- None;
2638 G.postRedisplay "throttle";
2640 else load layout;
2641 end;
2644 | _ ->
2645 dolog "Inconsistent tiling state";
2646 logcurrently state.currently;
2647 exit 1
2650 | "pdim" :: args :: [] ->
2651 let (n, w, h, _) as pdim =
2652 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2654 let pdim =
2655 match conf.fitmodel, conf.columns with
2656 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2657 | _ -> pdim
2659 state.uioh#infochanged Pdim;
2660 state.pdims <- pdim :: state.pdims
2662 | "o" :: args :: [] ->
2663 let (l, n, t, h, pos) =
2664 scan args "%u %u %d %u %n"
2665 (fun l n t h pos -> l, n, t, h, pos)
2667 let s = String.sub args pos (String.length args - pos) in
2668 let outline = (s, l, (n, float t /. float h, 0.0)) in
2669 begin match state.currently with
2670 | Outlining outlines ->
2671 state.currently <- Outlining (outline :: outlines)
2672 | Idle ->
2673 state.currently <- Outlining [outline]
2674 | currently ->
2675 dolog "invalid outlining state";
2676 logcurrently currently
2679 | "a" :: args :: [] ->
2680 let (n, l, t) =
2681 scan args "%u %d %d" (fun n l t -> n, l, t)
2683 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2685 | "info" :: args :: [] ->
2686 state.docinfo <- (1, args) :: state.docinfo
2688 | "infoend" :: [] ->
2689 state.uioh#infochanged Docinfo;
2690 state.docinfo <- List.rev state.docinfo
2692 | _ ->
2693 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2696 let onhist cb =
2697 let rc = cb.rc in
2698 let action = function
2699 | HCprev -> cbget cb ~-1
2700 | HCnext -> cbget cb 1
2701 | HCfirst -> cbget cb ~-(cb.rc)
2702 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2703 and cancel () = cb.rc <- rc
2704 in (action, cancel)
2707 let search pattern forward =
2708 match conf.columns with
2709 | Csplit _ ->
2710 showtext '!' "searching does not work properly in split columns mode"
2711 | _ ->
2712 if String.length pattern > 0
2713 then
2714 let pn, py =
2715 match state.layout with
2716 | [] -> 0, 0
2717 | l :: _ ->
2718 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2720 wcmd "search %d %d %d %d,%s\000"
2721 (btod conf.icase) pn py (btod forward) pattern;
2724 let intentry text key =
2725 let c =
2726 if key >= 32 && key < 127
2727 then Char.chr key
2728 else '\000'
2730 match c with
2731 | '0' .. '9' ->
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 linknentry text key =
2741 let c =
2742 if key >= 32 && key < 127
2743 then Char.chr key
2744 else '\000'
2746 match c with
2747 | 'a' .. 'z' ->
2748 let text = addchar text c in
2749 TEcont text
2751 | _ ->
2752 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2753 TEcont text
2756 let linkndone f s =
2757 if String.length s > 0
2758 then (
2759 let n =
2760 let l = String.length s in
2761 let rec loop pos n = if pos = l then n else
2762 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2763 loop (pos+1) (n*26 + m)
2764 in loop 0 0
2766 let rec loop n = function
2767 | [] -> ()
2768 | l :: rest ->
2769 match getopaque l.pageno with
2770 | None -> loop n rest
2771 | Some opaque ->
2772 let m = getlinkcount opaque in
2773 if n < m
2774 then (
2775 let under = getlink opaque n in
2776 f under
2778 else loop (n-m) rest
2780 loop n state.layout;
2784 let textentry text key =
2785 if key land 0xff00 = 0xff00
2786 then TEcont text
2787 else TEcont (text ^ toutf8 key)
2790 let reqlayout angle fitmodel =
2791 match state.throttle with
2792 | None ->
2793 if nogeomcmds state.geomcmds
2794 then state.anchor <- getanchor ();
2795 conf.angle <- angle mod 360;
2796 if conf.angle != 0
2797 then (
2798 match state.mode with
2799 | LinkNav _ -> state.mode <- View
2800 | _ -> ()
2802 conf.fitmodel <- fitmodel;
2803 invalidate "reqlayout"
2804 (fun () ->
2805 wcmd "reqlayout %d %d %d"
2806 conf.angle (int_of_fitmodel conf.fitmodel) (stateh state.winh)
2808 | _ -> ()
2811 let settrim trimmargins trimfuzz =
2812 if nogeomcmds state.geomcmds
2813 then state.anchor <- getanchor ();
2814 conf.trimmargins <- trimmargins;
2815 conf.trimfuzz <- trimfuzz;
2816 let x0, y0, x1, y1 = trimfuzz in
2817 invalidate "settrim"
2818 (fun () ->
2819 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2820 flushpages ();
2823 let setzoom zoom =
2824 match state.throttle with
2825 | None ->
2826 let zoom = max 0.0001 zoom in
2827 if zoom <> conf.zoom
2828 then (
2829 state.prevzoom <- conf.zoom;
2830 conf.zoom <- zoom;
2831 reshape state.winw state.winh;
2832 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2835 | Some (layout, y, started) ->
2836 let time =
2837 match conf.maxwait with
2838 | None -> 0.0
2839 | Some t -> t
2841 let dt = now () -. started in
2842 if dt > time
2843 then (
2844 state.y <- y;
2845 load layout;
2849 let setcolumns mode columns coverA coverB =
2850 state.prevcolumns <- Some (conf.columns, conf.zoom);
2851 if columns < 0
2852 then (
2853 if isbirdseye mode
2854 then showtext '!' "split mode doesn't work in bird's eye"
2855 else (
2856 conf.columns <- Csplit (-columns, [||]);
2857 state.x <- 0;
2858 conf.zoom <- 1.0;
2861 else (
2862 if columns < 2
2863 then (
2864 conf.columns <- Csingle [||];
2865 state.x <- 0;
2866 setzoom 1.0;
2868 else (
2869 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2870 conf.zoom <- 1.0;
2873 reshape state.winw state.winh;
2876 let enterbirdseye () =
2877 let zoom = float conf.thumbw /. float state.winw in
2878 let birdseyepageno =
2879 let cy = state.winh / 2 in
2880 let fold = function
2881 | [] -> 0
2882 | l :: rest ->
2883 let rec fold best = function
2884 | [] -> best.pageno
2885 | l :: rest ->
2886 let d = cy - (l.pagedispy + l.pagevh/2)
2887 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2888 if abs d < abs dbest
2889 then fold l rest
2890 else best.pageno
2891 in fold l rest
2893 fold state.layout
2895 state.mode <- Birdseye (
2896 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2898 conf.zoom <- zoom;
2899 conf.presentation <- false;
2900 conf.interpagespace <- 10;
2901 conf.hlinks <- false;
2902 conf.fitmodel <- FitProportional;
2903 state.x <- 0;
2904 state.mstate <- Mnone;
2905 conf.maxwait <- None;
2906 conf.columns <- (
2907 match conf.beyecolumns with
2908 | Some c ->
2909 conf.zoom <- 1.0;
2910 Cmulti ((c, 0, 0), [||])
2911 | None -> Csingle [||]
2913 Wsi.setcursor Wsi.CURSOR_INHERIT;
2914 if conf.verbose
2915 then
2916 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2917 (100.0*.zoom)
2918 else
2919 state.text <- ""
2921 reshape state.winw state.winh;
2924 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2925 state.mode <- View;
2926 conf.zoom <- c.zoom;
2927 conf.presentation <- c.presentation;
2928 conf.interpagespace <- c.interpagespace;
2929 conf.maxwait <- c.maxwait;
2930 conf.hlinks <- c.hlinks;
2931 conf.fitmodel <- c.fitmodel;
2932 conf.beyecolumns <- (
2933 match conf.columns with
2934 | Cmulti ((c, _, _), _) -> Some c
2935 | Csingle _ -> None
2936 | Csplit _ -> failwith "leaving bird's eye split mode"
2938 conf.columns <- (
2939 match c.columns with
2940 | Cmulti (c, _) -> Cmulti (c, [||])
2941 | Csingle _ -> Csingle [||]
2942 | Csplit (c, _) -> Csplit (c, [||])
2944 state.x <- leftx;
2945 if conf.verbose
2946 then
2947 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2948 (100.0*.conf.zoom)
2950 reshape state.winw state.winh;
2951 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2954 let togglebirdseye () =
2955 match state.mode with
2956 | Birdseye vals -> leavebirdseye vals true
2957 | View -> enterbirdseye ()
2958 | _ -> ()
2961 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2962 let pageno = max 0 (pageno - incr) in
2963 let rec loop = function
2964 | [] -> gotopage1 pageno 0
2965 | l :: _ when l.pageno = pageno ->
2966 if l.pagedispy >= 0 && l.pagey = 0
2967 then G.postRedisplay "upbirdseye"
2968 else gotopage1 pageno 0
2969 | _ :: rest -> loop rest
2971 loop state.layout;
2972 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2975 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2976 let pageno = min (state.pagecount - 1) (pageno + incr) in
2977 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2978 let rec loop = function
2979 | [] ->
2980 let y, h = getpageyh pageno in
2981 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2982 gotoy (clamp dy)
2983 | l :: _ when l.pageno = pageno ->
2984 if l.pagevh != l.pageh
2985 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2986 else G.postRedisplay "downbirdseye"
2987 | _ :: rest -> loop rest
2989 loop state.layout
2992 let optentry mode _ key =
2993 let btos b = if b then "on" else "off" in
2994 if key >= 32 && key < 127
2995 then
2996 let c = Char.chr key in
2997 match c with
2998 | 's' ->
2999 let ondone s =
3000 try conf.scrollstep <- int_of_string s with exc ->
3001 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3003 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3005 | 'A' ->
3006 let ondone s =
3008 conf.autoscrollstep <- int_of_string s;
3009 if state.autoscroll <> None
3010 then state.autoscroll <- Some conf.autoscrollstep
3011 with exc ->
3012 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3014 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3016 | 'C' ->
3017 let ondone s =
3019 let n, a, b = multicolumns_of_string s in
3020 setcolumns mode n a b;
3021 with exc ->
3022 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3024 TEswitch ("columns: ", "", None, textentry, ondone, true)
3026 | 'Z' ->
3027 let ondone s =
3029 let zoom = float (int_of_string s) /. 100.0 in
3030 setzoom zoom
3031 with exc ->
3032 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3034 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3036 | 't' ->
3037 let ondone s =
3039 conf.thumbw <- bound (int_of_string s) 2 4096;
3040 state.text <-
3041 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3042 begin match mode with
3043 | Birdseye beye ->
3044 leavebirdseye beye false;
3045 enterbirdseye ();
3046 | _ -> ();
3048 with exc ->
3049 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3051 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3053 | 'R' ->
3054 let ondone s =
3055 match try
3056 Some (int_of_string s)
3057 with exc ->
3058 state.text <- Printf.sprintf "bad integer `%s': %s"
3059 s (exntos exc);
3060 None
3061 with
3062 | Some angle -> reqlayout angle conf.fitmodel
3063 | None -> ()
3065 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3067 | 'i' ->
3068 conf.icase <- not conf.icase;
3069 TEdone ("case insensitive search " ^ (btos conf.icase))
3071 | 'p' ->
3072 conf.preload <- not conf.preload;
3073 gotoy state.y;
3074 TEdone ("preload " ^ (btos conf.preload))
3076 | 'v' ->
3077 conf.verbose <- not conf.verbose;
3078 TEdone ("verbose " ^ (btos conf.verbose))
3080 | 'd' ->
3081 conf.debug <- not conf.debug;
3082 TEdone ("debug " ^ (btos conf.debug))
3084 | 'h' ->
3085 conf.maxhfit <- not conf.maxhfit;
3086 state.maxy <- calcheight ();
3087 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3089 | 'c' ->
3090 conf.crophack <- not conf.crophack;
3091 TEdone ("crophack " ^ btos conf.crophack)
3093 | 'a' ->
3094 let s =
3095 match conf.maxwait with
3096 | None ->
3097 conf.maxwait <- Some infinity;
3098 "always wait for page to complete"
3099 | Some _ ->
3100 conf.maxwait <- None;
3101 "show placeholder if page is not ready"
3103 TEdone s
3105 | 'f' ->
3106 conf.underinfo <- not conf.underinfo;
3107 TEdone ("underinfo " ^ btos conf.underinfo)
3109 | 'P' ->
3110 conf.savebmarks <- not conf.savebmarks;
3111 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3113 | 'S' ->
3114 let ondone s =
3116 let pageno, py =
3117 match state.layout with
3118 | [] -> 0, 0
3119 | l :: _ ->
3120 l.pageno, l.pagey
3122 conf.interpagespace <- int_of_string s;
3123 docolumns conf.columns;
3124 state.maxy <- calcheight ();
3125 let y = getpagey pageno in
3126 gotoy (y + py)
3127 with exc ->
3128 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3130 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3132 | 'l' ->
3133 let fm =
3134 match conf.fitmodel with
3135 | FitProportional -> FitWidth
3136 | _ -> FitProportional
3138 reqlayout conf.angle fm;
3139 TEdone ("proportional display " ^ btos (fm == FitProportional))
3141 | 'T' ->
3142 settrim (not conf.trimmargins) conf.trimfuzz;
3143 TEdone ("trim margins " ^ btos conf.trimmargins)
3145 | 'I' ->
3146 conf.invert <- not conf.invert;
3147 TEdone ("invert colors " ^ btos conf.invert)
3149 | 'x' ->
3150 let ondone s =
3151 cbput state.hists.sel s;
3152 conf.selcmd <- s;
3154 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3155 textentry, ondone, true)
3157 | _ ->
3158 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3159 TEstop
3160 else
3161 TEcont state.text
3164 class type lvsource = object
3165 method getitemcount : int
3166 method getitem : int -> (string * int)
3167 method hasaction : int -> bool
3168 method exit :
3169 uioh:uioh ->
3170 cancel:bool ->
3171 active:int ->
3172 first:int ->
3173 pan:int ->
3174 qsearch:string ->
3175 uioh option
3176 method getactive : int
3177 method getfirst : int
3178 method getqsearch : string
3179 method setqsearch : string -> unit
3180 method getpan : int
3181 end;;
3183 class virtual lvsourcebase = object
3184 val mutable m_active = 0
3185 val mutable m_first = 0
3186 val mutable m_qsearch = ""
3187 val mutable m_pan = 0
3188 method getactive = m_active
3189 method getfirst = m_first
3190 method getqsearch = m_qsearch
3191 method getpan = m_pan
3192 method setqsearch s = m_qsearch <- s
3193 end;;
3195 let withoutlastutf8 s =
3196 let len = String.length s in
3197 if len = 0
3198 then s
3199 else
3200 let rec find pos =
3201 if pos = 0
3202 then pos
3203 else
3204 let b = Char.code s.[pos] in
3205 if b land 0b11000000 = 0b11000000
3206 then pos
3207 else find (pos-1)
3209 let first =
3210 if Char.code s.[len-1] land 0x80 = 0
3211 then len-1
3212 else find (len-1)
3214 String.sub s 0 first;
3217 let textentrykeyboard
3218 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3219 let key =
3220 if key >= 0xffb0 && key <= 0xffb9
3221 then key - 0xffb0 + 48 else key
3223 let enttext te =
3224 state.mode <- Textentry (te, onleave);
3225 state.text <- "";
3226 enttext ();
3227 G.postRedisplay "textentrykeyboard enttext";
3229 let histaction cmd =
3230 match opthist with
3231 | None -> ()
3232 | Some (action, _) ->
3233 state.mode <- Textentry (
3234 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3236 G.postRedisplay "textentry histaction"
3238 match key with
3239 | 0xff08 -> (* backspace *)
3240 let s = withoutlastutf8 text in
3241 let len = String.length s in
3242 if cancelonempty && len = 0
3243 then (
3244 onleave Cancel;
3245 G.postRedisplay "textentrykeyboard after cancel";
3247 else (
3248 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3251 | 0xff0d | 0xff8d -> (* (kp) enter *)
3252 ondone text;
3253 onleave Confirm;
3254 G.postRedisplay "textentrykeyboard after confirm"
3256 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3257 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3258 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3259 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3261 | 0xff1b -> (* escape*)
3262 if String.length text = 0
3263 then (
3264 begin match opthist with
3265 | None -> ()
3266 | Some (_, onhistcancel) -> onhistcancel ()
3267 end;
3268 onleave Cancel;
3269 state.text <- "";
3270 G.postRedisplay "textentrykeyboard after cancel2"
3272 else (
3273 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3276 | 0xff9f | 0xffff -> () (* delete *)
3278 | _ when key != 0
3279 && key land 0xff00 != 0xff00 (* keyboard *)
3280 && key land 0xfe00 != 0xfe00 (* xkb *)
3281 && key land 0xfd00 != 0xfd00 (* 3270 *)
3283 begin match onkey text key with
3284 | TEdone text ->
3285 ondone text;
3286 onleave Confirm;
3287 G.postRedisplay "textentrykeyboard after confirm2";
3289 | TEcont text ->
3290 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3292 | TEstop ->
3293 onleave Cancel;
3294 G.postRedisplay "textentrykeyboard after cancel3"
3296 | TEswitch te ->
3297 state.mode <- Textentry (te, onleave);
3298 G.postRedisplay "textentrykeyboard switch";
3299 end;
3301 | _ ->
3302 vlog "unhandled key %s" (Wsi.keyname key)
3305 let firstof first active =
3306 if first > active || abs (first - active) > fstate.maxrows - 1
3307 then max 0 (active - (fstate.maxrows/2))
3308 else first
3311 let calcfirst first active =
3312 if active > first
3313 then
3314 let rows = active - first in
3315 if rows > fstate.maxrows then active - fstate.maxrows else first
3316 else active
3319 let scrollph y maxy =
3320 let sh = float (maxy + state.winh) /. float state.winh in
3321 let sh = float state.winh /. sh in
3322 let sh = max sh (float conf.scrollh) in
3324 let percent = float y /. float maxy in
3325 let position = (float state.winh -. sh) *. percent in
3327 let position =
3328 if position +. sh > float state.winh
3329 then float state.winh -. sh
3330 else position
3332 position, sh;
3335 let coe s = (s :> uioh);;
3337 class listview ~(source:lvsource) ~trusted ~modehash =
3338 object (self)
3339 val m_pan = source#getpan
3340 val m_first = source#getfirst
3341 val m_active = source#getactive
3342 val m_qsearch = source#getqsearch
3343 val m_prev_uioh = state.uioh
3345 method private elemunder y =
3346 let n = y / (fstate.fontsize+1) in
3347 if m_first + n < source#getitemcount
3348 then (
3349 if source#hasaction (m_first + n)
3350 then Some (m_first + n)
3351 else None
3353 else None
3355 method display =
3356 Gl.enable `blend;
3357 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3358 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3359 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3360 GlDraw.color (1., 1., 1.);
3361 Gl.enable `texture_2d;
3362 let fs = fstate.fontsize in
3363 let nfs = fs + 1 in
3364 let ww = fstate.wwidth in
3365 let tabw = 30.0*.ww in
3366 let itemcount = source#getitemcount in
3367 let rec loop row =
3368 if (row - m_first) > fstate.maxrows
3369 then ()
3370 else (
3371 if row >= 0 && row < itemcount
3372 then (
3373 let (s, level) = source#getitem row in
3374 let y = (row - m_first) * nfs in
3375 let x = 5.0 +. float (level + m_pan) *. ww in
3376 if row = m_active
3377 then (
3378 Gl.disable `texture_2d;
3379 GlDraw.polygon_mode `both `line;
3380 let alpha = if source#hasaction row then 0.9 else 0.3 in
3381 GlDraw.color (1., 1., 1.) ~alpha;
3382 GlDraw.rect (1., float (y + 1))
3383 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3384 GlDraw.polygon_mode `both `fill;
3385 GlDraw.color (1., 1., 1.);
3386 Gl.enable `texture_2d;
3389 let drawtabularstring s =
3390 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3391 if trusted
3392 then
3393 let tabpos = try String.index s '\t' with Not_found -> -1 in
3394 if tabpos > 0
3395 then
3396 let len = String.length s - tabpos - 1 in
3397 let s1 = String.sub s 0 tabpos
3398 and s2 = String.sub s (tabpos + 1) len in
3399 let nx = drawstr x s1 in
3400 let sw = nx -. x in
3401 let x = x +. (max tabw sw) in
3402 drawstr x s2
3403 else
3404 drawstr x s
3405 else
3406 drawstr x s
3408 let _ = drawtabularstring s in
3409 loop (row+1)
3413 loop m_first;
3414 Gl.disable `blend;
3415 Gl.disable `texture_2d;
3417 method updownlevel incr =
3418 let len = source#getitemcount in
3419 let curlevel =
3420 if m_active >= 0 && m_active < len
3421 then snd (source#getitem m_active)
3422 else -1
3424 let rec flow i =
3425 if i = len then i-1 else if i = -1 then 0 else
3426 let _, l = source#getitem i in
3427 if l != curlevel then i else flow (i+incr)
3429 let active = flow m_active in
3430 let first = calcfirst m_first active in
3431 G.postRedisplay "outline updownlevel";
3432 {< m_active = active; m_first = first >}
3434 method private key1 key mask =
3435 let set1 active first qsearch =
3436 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3438 let search active pattern incr =
3439 let active = if active = -1 then m_first else active in
3440 let dosearch re =
3441 let rec loop n =
3442 if n >= 0 && n < source#getitemcount
3443 then (
3444 let s, _ = source#getitem n in
3446 (try ignore (Str.search_forward re s 0); true
3447 with Not_found -> false)
3448 then Some n
3449 else loop (n + incr)
3451 else None
3453 loop active
3456 let re = Str.regexp_case_fold pattern in
3457 dosearch re
3458 with Failure s ->
3459 state.text <- s;
3460 None
3462 let itemcount = source#getitemcount in
3463 let find start incr =
3464 let rec find i =
3465 if i = -1 || i = itemcount
3466 then -1
3467 else (
3468 if source#hasaction i
3469 then i
3470 else find (i + incr)
3473 find start
3475 let set active first =
3476 let first = bound first 0 (itemcount - fstate.maxrows) in
3477 state.text <- "";
3478 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3480 let navigate incr =
3481 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3482 let active, first =
3483 let incr1 = if incr > 0 then 1 else -1 in
3484 if isvisible m_first m_active
3485 then
3486 let next =
3487 let next = m_active + incr in
3488 let next =
3489 if next < 0 || next >= itemcount
3490 then -1
3491 else find next incr1
3493 if abs (m_active - next) > fstate.maxrows
3494 then -1
3495 else next
3497 if next = -1
3498 then
3499 let first = m_first + incr in
3500 let first = bound first 0 (itemcount - fstate.maxrows) in
3501 let next =
3502 let next = m_active + incr in
3503 let next = bound next 0 (itemcount - 1) in
3504 find next ~-incr1
3506 let active =
3507 if next = -1
3508 then m_active
3509 else (
3510 if isvisible first next
3511 then next
3512 else m_active
3515 active, first
3516 else
3517 let first = min next m_first in
3518 let first =
3519 if abs (next - first) > fstate.maxrows
3520 then first + incr
3521 else first
3523 next, first
3524 else
3525 let first = m_first + incr in
3526 let first = bound first 0 (itemcount - 1) in
3527 let active =
3528 let next = m_active + incr in
3529 let next = bound next 0 (itemcount - 1) in
3530 let next = find next incr1 in
3531 let active =
3532 if next = -1 || abs (m_active - first) > fstate.maxrows
3533 then (
3534 let active = if m_active = -1 then next else m_active in
3535 active
3537 else next
3539 if isvisible first active
3540 then active
3541 else -1
3543 active, first
3545 G.postRedisplay "listview navigate";
3546 set active first;
3548 match key with
3549 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3550 let incr = if key = 0x72 then -1 else 1 in
3551 let active, first =
3552 match search (m_active + incr) m_qsearch incr with
3553 | None ->
3554 state.text <- m_qsearch ^ " [not found]";
3555 m_active, m_first
3556 | Some active ->
3557 state.text <- m_qsearch;
3558 active, firstof m_first active
3560 G.postRedisplay "listview ctrl-r/s";
3561 set1 active first m_qsearch;
3563 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3564 if m_active >= 0 && m_active < source#getitemcount
3565 then (
3566 let s, _ = source#getitem m_active in
3567 selstring s;
3569 coe self
3571 | 0xff08 -> (* backspace *)
3572 if String.length m_qsearch = 0
3573 then coe self
3574 else (
3575 let qsearch = withoutlastutf8 m_qsearch in
3576 let len = String.length qsearch in
3577 if len = 0
3578 then (
3579 state.text <- "";
3580 G.postRedisplay "listview empty qsearch";
3581 set1 m_active m_first "";
3583 else
3584 let active, first =
3585 match search m_active qsearch ~-1 with
3586 | None ->
3587 state.text <- qsearch ^ " [not found]";
3588 m_active, m_first
3589 | Some active ->
3590 state.text <- qsearch;
3591 active, firstof m_first active
3593 G.postRedisplay "listview backspace qsearch";
3594 set1 active first qsearch
3597 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3598 let pattern = m_qsearch ^ toutf8 key in
3599 let active, first =
3600 match search m_active pattern 1 with
3601 | None ->
3602 state.text <- pattern ^ " [not found]";
3603 m_active, m_first
3604 | Some active ->
3605 state.text <- pattern;
3606 active, firstof m_first active
3608 G.postRedisplay "listview qsearch add";
3609 set1 active first pattern;
3611 | 0xff1b -> (* escape *)
3612 state.text <- "";
3613 if String.length m_qsearch = 0
3614 then (
3615 G.postRedisplay "list view escape";
3616 begin
3617 match
3618 source#exit (coe self) true m_active m_first m_pan m_qsearch
3619 with
3620 | None -> m_prev_uioh
3621 | Some uioh -> uioh
3624 else (
3625 G.postRedisplay "list view kill qsearch";
3626 source#setqsearch "";
3627 coe {< m_qsearch = "" >}
3630 | 0xff0d | 0xff8d -> (* (kp) enter *)
3631 state.text <- "";
3632 let self = {< m_qsearch = "" >} in
3633 source#setqsearch "";
3634 let opt =
3635 G.postRedisplay "listview enter";
3636 if m_active >= 0 && m_active < source#getitemcount
3637 then (
3638 source#exit (coe self) false m_active m_first m_pan "";
3640 else (
3641 source#exit (coe self) true m_active m_first m_pan "";
3644 begin match opt with
3645 | None -> m_prev_uioh
3646 | Some uioh -> uioh
3649 | 0xff9f | 0xffff -> (* (kp) delete *)
3650 coe self
3652 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3653 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3654 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3655 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3657 | 0xff53 | 0xff98 -> (* (kp) right *)
3658 state.text <- "";
3659 G.postRedisplay "listview right";
3660 coe {< m_pan = m_pan - 1 >}
3662 | 0xff51 | 0xff96 -> (* (kp) left *)
3663 state.text <- "";
3664 G.postRedisplay "listview left";
3665 coe {< m_pan = m_pan + 1 >}
3667 | 0xff50 | 0xff95 -> (* (kp) home *)
3668 let active = find 0 1 in
3669 G.postRedisplay "listview home";
3670 set active 0;
3672 | 0xff57 | 0xff9c -> (* (kp) end *)
3673 let first = max 0 (itemcount - fstate.maxrows) in
3674 let active = find (itemcount - 1) ~-1 in
3675 G.postRedisplay "listview end";
3676 set active first;
3678 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3679 coe self
3681 | _ ->
3682 dolog "listview unknown key %#x" key; coe self
3684 method key key mask =
3685 match state.mode with
3686 | Textentry te -> textentrykeyboard key mask te; coe self
3687 | _ -> self#key1 key mask
3689 method button button down x y _ =
3690 let opt =
3691 match button with
3692 | 1 when x > state.winw - conf.scrollbw ->
3693 G.postRedisplay "listview scroll";
3694 if down
3695 then
3696 let _, position, sh = self#scrollph in
3697 if y > truncate position && y < truncate (position +. sh)
3698 then (
3699 state.mstate <- Mscrolly;
3700 Some (coe self)
3702 else
3703 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3704 let first = truncate (s *. float source#getitemcount) in
3705 let first = min source#getitemcount first in
3706 Some (coe {< m_first = first; m_active = first >})
3707 else (
3708 state.mstate <- Mnone;
3709 Some (coe self);
3711 | 1 when not down ->
3712 begin match self#elemunder y with
3713 | Some n ->
3714 G.postRedisplay "listview click";
3715 source#exit
3716 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3717 | _ ->
3718 Some (coe self)
3720 | n when (n == 4 || n == 5) && not down ->
3721 let len = source#getitemcount in
3722 let first =
3723 if n = 5 && m_first + fstate.maxrows >= len
3724 then
3725 m_first
3726 else
3727 let first = m_first + (if n == 4 then -1 else 1) in
3728 bound first 0 (len - 1)
3730 G.postRedisplay "listview wheel";
3731 Some (coe {< m_first = first >})
3732 | n when (n = 6 || n = 7) && not down ->
3733 let inc = m_first + (if n = 7 then -1 else 1) in
3734 G.postRedisplay "listview hwheel";
3735 Some (coe {< m_pan = m_pan + inc >})
3736 | _ ->
3737 Some (coe self)
3739 match opt with
3740 | None -> m_prev_uioh
3741 | Some uioh -> uioh
3743 method motion _ y =
3744 match state.mstate with
3745 | Mscrolly ->
3746 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3747 let first = truncate (s *. float source#getitemcount) in
3748 let first = min source#getitemcount first in
3749 G.postRedisplay "listview motion";
3750 coe {< m_first = first; m_active = first >}
3751 | _ -> coe self
3753 method pmotion x y =
3754 if x < state.winw - conf.scrollbw
3755 then
3756 let n =
3757 match self#elemunder y with
3758 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3759 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3761 let o =
3762 if n != m_active
3763 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3764 else self
3766 coe o
3767 else (
3768 Wsi.setcursor Wsi.CURSOR_INHERIT;
3769 coe self
3772 method infochanged _ = ()
3774 method scrollpw = (0, 0.0, 0.0)
3775 method scrollph =
3776 let nfs = fstate.fontsize + 1 in
3777 let y = m_first * nfs in
3778 let itemcount = source#getitemcount in
3779 let maxi = max 0 (itemcount - fstate.maxrows) in
3780 let maxy = maxi * nfs in
3781 let p, h = scrollph y maxy in
3782 conf.scrollbw, p, h
3784 method modehash = modehash
3785 method eformsgs = false
3786 end;;
3788 class outlinelistview ~source =
3789 object (self)
3790 inherit listview
3791 ~source:(source :> lvsource)
3792 ~trusted:false
3793 ~modehash:(findkeyhash conf "outline")
3794 as super
3796 method key key mask =
3797 let calcfirst first active =
3798 if active > first
3799 then
3800 let rows = active - first in
3801 let maxrows =
3802 if String.length state.text = 0
3803 then fstate.maxrows
3804 else fstate.maxrows - 2
3806 if rows > maxrows then active - maxrows else first
3807 else active
3809 let navigate incr =
3810 let active = m_active + incr in
3811 let active = bound active 0 (source#getitemcount - 1) in
3812 let first = calcfirst m_first active in
3813 G.postRedisplay "outline navigate";
3814 coe {< m_active = active; m_first = first >}
3816 let ctrl = Wsi.withctrl mask in
3817 match key with
3818 | 110 when ctrl -> (* ctrl-n *)
3819 source#narrow m_qsearch;
3820 G.postRedisplay "outline ctrl-n";
3821 coe {< m_first = 0; m_active = 0 >}
3823 | 117 when ctrl -> (* ctrl-u *)
3824 source#denarrow;
3825 G.postRedisplay "outline ctrl-u";
3826 state.text <- "";
3827 coe {< m_first = 0; m_active = 0 >}
3829 | 108 when ctrl -> (* ctrl-l *)
3830 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3831 G.postRedisplay "outline ctrl-l";
3832 coe {< m_first = first >}
3834 | 0xff9f | 0xffff -> (* (kp) delete *)
3835 source#remove m_active;
3836 G.postRedisplay "outline delete";
3837 let active = max 0 (m_active-1) in
3838 coe {< m_first = firstof m_first active;
3839 m_active = active >}
3841 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3842 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3843 | 0xff55 | 0xff9a -> (* (kp) prior *)
3844 navigate ~-(fstate.maxrows)
3845 | 0xff56 | 0xff9b -> (* (kp) next *)
3846 navigate fstate.maxrows
3848 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3849 let o =
3850 if ctrl
3851 then (
3852 G.postRedisplay "outline ctrl right";
3853 {< m_pan = m_pan + 1 >}
3855 else self#updownlevel 1
3857 coe o
3859 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3860 let o =
3861 if ctrl
3862 then (
3863 G.postRedisplay "outline ctrl left";
3864 {< m_pan = m_pan - 1 >}
3866 else self#updownlevel ~-1
3868 coe o
3870 | 0xff50 | 0xff95 -> (* (kp) home *)
3871 G.postRedisplay "outline home";
3872 coe {< m_first = 0; m_active = 0 >}
3874 | 0xff57 | 0xff9c -> (* (kp) end *)
3875 let active = source#getitemcount - 1 in
3876 let first = max 0 (active - fstate.maxrows) in
3877 G.postRedisplay "outline end";
3878 coe {< m_active = active; m_first = first >}
3880 | _ -> super#key key mask
3883 let outlinesource usebookmarks =
3884 let empty = [||] in
3885 (object
3886 inherit lvsourcebase
3887 val mutable m_items = empty
3888 val mutable m_orig_items = empty
3889 val mutable m_prev_items = empty
3890 val mutable m_narrow_pattern = ""
3891 val mutable m_hadremovals = false
3893 method getitemcount =
3894 Array.length m_items + (if m_hadremovals then 1 else 0)
3896 method getitem n =
3897 if n == Array.length m_items && m_hadremovals
3898 then
3899 ("[Confirm removal]", 0)
3900 else
3901 let s, n, _ = m_items.(n) in
3902 (s, n)
3904 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3905 ignore (uioh, first, qsearch);
3906 let confrimremoval = m_hadremovals && active = Array.length m_items in
3907 let items =
3908 if String.length m_narrow_pattern = 0
3909 then m_orig_items
3910 else m_items
3912 if not cancel
3913 then (
3914 if not confrimremoval
3915 then(
3916 let _, _, anchor = m_items.(active) in
3917 gotoghyll (getanchory anchor);
3918 m_items <- items;
3920 else (
3921 state.bookmarks <- Array.to_list m_items;
3922 m_orig_items <- m_items;
3925 else m_items <- items;
3926 m_pan <- pan;
3927 None
3929 method hasaction _ = true
3931 method greetmsg =
3932 if Array.length m_items != Array.length m_orig_items
3933 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3934 else ""
3936 method narrow pattern =
3937 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3938 match reopt with
3939 | None -> ()
3940 | Some re ->
3941 let rec loop accu n =
3942 if n = -1
3943 then (
3944 m_narrow_pattern <- pattern;
3945 m_items <- Array.of_list accu
3947 else
3948 let (s, _, _) as o = m_items.(n) in
3949 let accu =
3950 if (try ignore (Str.search_forward re s 0); true
3951 with Not_found -> false)
3952 then o :: accu
3953 else accu
3955 loop accu (n-1)
3957 loop [] (Array.length m_items - 1)
3959 method denarrow =
3960 m_orig_items <- (
3961 if usebookmarks
3962 then Array.of_list state.bookmarks
3963 else state.outlines
3965 m_items <- m_orig_items
3967 method remove m =
3968 if usebookmarks
3969 then
3970 if m >= 0 && m < Array.length m_items
3971 then (
3972 m_hadremovals <- true;
3973 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3974 let n = if n >= m then n+1 else n in
3975 m_items.(n)
3979 method reset anchor items =
3980 m_hadremovals <- false;
3981 if m_orig_items == empty || m_prev_items != items
3982 then (
3983 m_orig_items <- items;
3984 if String.length m_narrow_pattern = 0
3985 then m_items <- items;
3987 m_prev_items <- items;
3988 let rely = getanchory anchor in
3989 let active =
3990 let rec loop n best bestd =
3991 if n = Array.length m_items
3992 then best
3993 else
3994 let (_, _, anchor) = m_items.(n) in
3995 let orely = getanchory anchor in
3996 let d = abs (orely - rely) in
3997 if d < bestd
3998 then loop (n+1) n d
3999 else loop (n+1) best bestd
4001 loop 0 ~-1 max_int
4003 m_active <- active;
4004 m_first <- firstof m_first active
4005 end)
4008 let enterselector usebookmarks =
4009 let source = outlinesource usebookmarks in
4010 fun errmsg ->
4011 let outlines =
4012 if usebookmarks
4013 then Array.of_list state.bookmarks
4014 else state.outlines
4016 if Array.length outlines = 0
4017 then (
4018 showtext ' ' errmsg;
4020 else (
4021 state.text <- source#greetmsg;
4022 Wsi.setcursor Wsi.CURSOR_INHERIT;
4023 let anchor = getanchor () in
4024 source#reset anchor outlines;
4025 state.uioh <- coe (new outlinelistview ~source);
4026 G.postRedisplay "enter selector";
4030 let enteroutlinemode =
4031 let f = enterselector false in
4032 fun ()-> f "Document has no outline";
4035 let enterbookmarkmode =
4036 let f = enterselector true in
4037 fun () -> f "Document has no bookmarks (yet)";
4040 let color_of_string s =
4041 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4042 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4046 let color_to_string (r, g, b) =
4047 let r = truncate (r *. 256.0)
4048 and g = truncate (g *. 256.0)
4049 and b = truncate (b *. 256.0) in
4050 Printf.sprintf "%d/%d/%d" r g b
4053 let irect_of_string s =
4054 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4057 let irect_to_string (x0,y0,x1,y1) =
4058 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4061 let makecheckers () =
4062 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4063 following to say:
4064 converted by Issac Trotts. July 25, 2002 *)
4065 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4066 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4067 let id = GlTex.gen_texture () in
4068 GlTex.bind_texture `texture_2d id;
4069 GlPix.store (`unpack_alignment 1);
4070 GlTex.image2d image;
4071 List.iter (GlTex.parameter ~target:`texture_2d)
4072 [ `mag_filter `nearest; `min_filter `nearest ];
4076 let setcheckers enabled =
4077 match state.texid with
4078 | None ->
4079 if enabled then state.texid <- Some (makecheckers ())
4081 | Some texid ->
4082 if not enabled
4083 then (
4084 GlTex.delete_texture texid;
4085 state.texid <- None;
4089 let int_of_string_with_suffix s =
4090 let l = String.length s in
4091 let s1, shift =
4092 if l > 1
4093 then
4094 let suffix = Char.lowercase s.[l-1] in
4095 match suffix with
4096 | 'k' -> String.sub s 0 (l-1), 10
4097 | 'm' -> String.sub s 0 (l-1), 20
4098 | 'g' -> String.sub s 0 (l-1), 30
4099 | _ -> s, 0
4100 else s, 0
4102 let n = int_of_string s1 in
4103 let m = n lsl shift in
4104 if m < 0 || m < n
4105 then raise (Failure "value too large")
4106 else m
4109 let string_with_suffix_of_int n =
4110 if n = 0
4111 then "0"
4112 else
4113 let n, s =
4114 if n land ((1 lsl 30) - 1) = 0
4115 then n lsr 30, "G"
4116 else (
4117 if n land ((1 lsl 20) - 1) = 0
4118 then n lsr 20, "M"
4119 else (
4120 if n land ((1 lsl 10) - 1) = 0
4121 then n lsr 10, "K"
4122 else n, ""
4126 let rec loop s n =
4127 let h = n mod 1000 in
4128 let n = n / 1000 in
4129 if n = 0
4130 then string_of_int h ^ s
4131 else (
4132 let s = Printf.sprintf "_%03d%s" h s in
4133 loop s n
4136 loop "" n ^ s;
4139 let defghyllscroll = (40, 8, 32);;
4140 let ghyllscroll_of_string s =
4141 let (n, a, b) as nab =
4142 if s = "default"
4143 then defghyllscroll
4144 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4146 if n <= a || n <= b || a >= b
4147 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4148 nab;
4151 let ghyllscroll_to_string ((n, a, b) as nab) =
4152 if nab = defghyllscroll
4153 then "default"
4154 else Printf.sprintf "%d,%d,%d" n a b;
4157 let describe_location () =
4158 let fn = page_of_y state.y in
4159 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4160 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4161 let percent =
4162 if maxy <= 0
4163 then 100.
4164 else (100. *. (float state.y /. float maxy))
4166 if fn = ln
4167 then
4168 Printf.sprintf "page %d of %d [%.2f%%]"
4169 (fn+1) state.pagecount percent
4170 else
4171 Printf.sprintf
4172 "pages %d-%d of %d [%.2f%%]"
4173 (fn+1) (ln+1) state.pagecount percent
4176 let setpresentationmode v =
4177 let n = page_of_y state.y in
4178 state.anchor <- (n, 0.0, 1.0);
4179 conf.presentation <- v;
4180 if conf.fitmodel = FitPage
4181 then reqlayout conf.angle conf.fitmodel;
4182 represent ();
4185 let enterinfomode =
4186 let btos b = if b then "\xe2\x88\x9a" else "" in
4187 let showextended = ref false in
4188 let leave mode = function
4189 | Confirm -> state.mode <- mode
4190 | Cancel -> state.mode <- mode in
4191 let src =
4192 (object
4193 val mutable m_first_time = true
4194 val mutable m_l = []
4195 val mutable m_a = [||]
4196 val mutable m_prev_uioh = nouioh
4197 val mutable m_prev_mode = View
4199 inherit lvsourcebase
4201 method reset prev_mode prev_uioh =
4202 m_a <- Array.of_list (List.rev m_l);
4203 m_l <- [];
4204 m_prev_mode <- prev_mode;
4205 m_prev_uioh <- prev_uioh;
4206 if m_first_time
4207 then (
4208 let rec loop n =
4209 if n >= Array.length m_a
4210 then ()
4211 else
4212 match m_a.(n) with
4213 | _, _, _, Action _ -> m_active <- n
4214 | _ -> loop (n+1)
4216 loop 0;
4217 m_first_time <- false;
4220 method int name get set =
4221 m_l <-
4222 (name, `int get, 1, Action (
4223 fun u ->
4224 let ondone s =
4225 try set (int_of_string s)
4226 with exn ->
4227 state.text <- Printf.sprintf "bad integer `%s': %s"
4228 s (exntos exn)
4230 state.text <- "";
4231 let te = name ^ ": ", "", None, intentry, ondone, true in
4232 state.mode <- Textentry (te, leave m_prev_mode);
4234 )) :: m_l
4236 method int_with_suffix name get set =
4237 m_l <-
4238 (name, `intws get, 1, Action (
4239 fun u ->
4240 let ondone s =
4241 try set (int_of_string_with_suffix s)
4242 with exn ->
4243 state.text <- Printf.sprintf "bad integer `%s': %s"
4244 s (exntos exn)
4246 state.text <- "";
4247 let te =
4248 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4250 state.mode <- Textentry (te, leave m_prev_mode);
4252 )) :: m_l
4254 method bool ?(offset=1) ?(btos=btos) name get set =
4255 m_l <-
4256 (name, `bool (btos, get), offset, Action (
4257 fun u ->
4258 let v = get () in
4259 set (not v);
4261 )) :: m_l
4263 method color name get set =
4264 m_l <-
4265 (name, `color get, 1, Action (
4266 fun u ->
4267 let invalid = (nan, nan, nan) in
4268 let ondone s =
4269 let c =
4270 try color_of_string s
4271 with exn ->
4272 state.text <- Printf.sprintf "bad color `%s': %s"
4273 s (exntos exn);
4274 invalid
4276 if c <> invalid
4277 then set c;
4279 let te = name ^ ": ", "", None, textentry, ondone, true in
4280 state.text <- color_to_string (get ());
4281 state.mode <- Textentry (te, leave m_prev_mode);
4283 )) :: m_l
4285 method string name get set =
4286 m_l <-
4287 (name, `string get, 1, Action (
4288 fun u ->
4289 let ondone s = set s in
4290 let te = name ^ ": ", "", None, textentry, ondone, true in
4291 state.mode <- Textentry (te, leave m_prev_mode);
4293 )) :: m_l
4295 method colorspace name get set =
4296 m_l <-
4297 (name, `string get, 1, Action (
4298 fun _ ->
4299 let source =
4300 let vals = [| "rgb"; "bgr"; "gray" |] in
4301 (object
4302 inherit lvsourcebase
4304 initializer
4305 m_active <- int_of_colorspace conf.colorspace;
4306 m_first <- 0;
4308 method getitemcount = Array.length vals
4309 method getitem n = (vals.(n), 0)
4310 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4311 ignore (uioh, first, pan, qsearch);
4312 if not cancel then set active;
4313 None
4314 method hasaction _ = true
4315 end)
4317 state.text <- "";
4318 let modehash = findkeyhash conf "info" in
4319 coe (new listview ~source ~trusted:true ~modehash)
4320 )) :: m_l
4322 method fitmodel name get set =
4323 m_l <-
4324 (name, `string get, 1, Action (
4325 fun _ ->
4326 let source =
4327 let vals = [| "fit width"; "proportional"; "fit page" |] in
4328 (object
4329 inherit lvsourcebase
4331 initializer
4332 m_active <- int_of_fitmodel conf.fitmodel;
4333 m_first <- 0;
4335 method getitemcount = Array.length vals
4336 method getitem n = (vals.(n), 0)
4337 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4338 ignore (uioh, first, pan, qsearch);
4339 if not cancel then set active;
4340 None
4341 method hasaction _ = true
4342 end)
4344 state.text <- "";
4345 let modehash = findkeyhash conf "info" in
4346 coe (new listview ~source ~trusted:true ~modehash)
4347 )) :: m_l
4349 method caption s offset =
4350 m_l <- (s, `empty, offset, Noaction) :: m_l
4352 method caption2 s f offset =
4353 m_l <- (s, `string f, offset, Noaction) :: m_l
4355 method getitemcount = Array.length m_a
4357 method getitem n =
4358 let tostr = function
4359 | `int f -> string_of_int (f ())
4360 | `intws f -> string_with_suffix_of_int (f ())
4361 | `string f -> f ()
4362 | `color f -> color_to_string (f ())
4363 | `bool (btos, f) -> btos (f ())
4364 | `empty -> ""
4366 let name, t, offset, _ = m_a.(n) in
4367 ((let s = tostr t in
4368 if String.length s > 0
4369 then Printf.sprintf "%s\t%s" name s
4370 else name),
4371 offset)
4373 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4374 let uiohopt =
4375 if not cancel
4376 then (
4377 m_qsearch <- qsearch;
4378 let uioh =
4379 match m_a.(active) with
4380 | _, _, _, Action f -> f uioh
4381 | _ -> uioh
4383 Some uioh
4385 else None
4387 m_active <- active;
4388 m_first <- first;
4389 m_pan <- pan;
4390 uiohopt
4392 method hasaction n =
4393 match m_a.(n) with
4394 | _, _, _, Action _ -> true
4395 | _ -> false
4396 end)
4398 let rec fillsrc prevmode prevuioh =
4399 let sep () = src#caption "" 0 in
4400 let colorp name get set =
4401 src#string name
4402 (fun () -> color_to_string (get ()))
4403 (fun v ->
4405 let c = color_of_string v in
4406 set c
4407 with exn ->
4408 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4411 let oldmode = state.mode in
4412 let birdseye = isbirdseye state.mode in
4414 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4416 src#bool "presentation mode"
4417 (fun () -> conf.presentation)
4418 (fun v -> setpresentationmode v);
4420 src#bool "ignore case in searches"
4421 (fun () -> conf.icase)
4422 (fun v -> conf.icase <- v);
4424 src#bool "preload"
4425 (fun () -> conf.preload)
4426 (fun v -> conf.preload <- v);
4428 src#bool "highlight links"
4429 (fun () -> conf.hlinks)
4430 (fun v -> conf.hlinks <- v);
4432 src#bool "under info"
4433 (fun () -> conf.underinfo)
4434 (fun v -> conf.underinfo <- v);
4436 src#bool "persistent bookmarks"
4437 (fun () -> conf.savebmarks)
4438 (fun v -> conf.savebmarks <- v);
4440 src#fitmodel "fit model"
4441 (fun () -> fitmodel_to_string conf.fitmodel)
4442 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4444 src#bool "trim margins"
4445 (fun () -> conf.trimmargins)
4446 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4448 src#bool "persistent location"
4449 (fun () -> conf.jumpback)
4450 (fun v -> conf.jumpback <- v);
4452 sep ();
4453 src#int "inter-page space"
4454 (fun () -> conf.interpagespace)
4455 (fun n ->
4456 conf.interpagespace <- n;
4457 docolumns conf.columns;
4458 let pageno, py =
4459 match state.layout with
4460 | [] -> 0, 0
4461 | l :: _ ->
4462 l.pageno, l.pagey
4464 state.maxy <- calcheight ();
4465 let y = getpagey pageno in
4466 gotoy (y + py)
4469 src#int "page bias"
4470 (fun () -> conf.pagebias)
4471 (fun v -> conf.pagebias <- v);
4473 src#int "scroll step"
4474 (fun () -> conf.scrollstep)
4475 (fun n -> conf.scrollstep <- n);
4477 src#int "horizontal scroll step"
4478 (fun () -> conf.hscrollstep)
4479 (fun v -> conf.hscrollstep <- v);
4481 src#int "auto scroll step"
4482 (fun () ->
4483 match state.autoscroll with
4484 | Some step -> step
4485 | _ -> conf.autoscrollstep)
4486 (fun n ->
4487 if state.autoscroll <> None
4488 then state.autoscroll <- Some n;
4489 conf.autoscrollstep <- n);
4491 src#int "zoom"
4492 (fun () -> truncate (conf.zoom *. 100.))
4493 (fun v -> setzoom ((float v) /. 100.));
4495 src#int "rotation"
4496 (fun () -> conf.angle)
4497 (fun v -> reqlayout v conf.fitmodel);
4499 src#int "scroll bar width"
4500 (fun () -> conf.scrollbw)
4501 (fun v ->
4502 conf.scrollbw <- v;
4503 reshape state.winw state.winh;
4506 src#int "scroll handle height"
4507 (fun () -> conf.scrollh)
4508 (fun v -> conf.scrollh <- v;);
4510 src#int "thumbnail width"
4511 (fun () -> conf.thumbw)
4512 (fun v ->
4513 conf.thumbw <- min 4096 v;
4514 match oldmode with
4515 | Birdseye beye ->
4516 leavebirdseye beye false;
4517 enterbirdseye ()
4518 | _ -> ()
4521 let mode = state.mode in
4522 src#string "columns"
4523 (fun () ->
4524 match conf.columns with
4525 | Csingle _ -> "1"
4526 | Cmulti (multi, _) -> multicolumns_to_string multi
4527 | Csplit (count, _) -> "-" ^ string_of_int count
4529 (fun v ->
4530 let n, a, b = multicolumns_of_string v in
4531 setcolumns mode n a b);
4533 sep ();
4534 src#caption "Pixmap cache" 0;
4535 src#int_with_suffix "size (advisory)"
4536 (fun () -> conf.memlimit)
4537 (fun v -> conf.memlimit <- v);
4539 src#caption2 "used"
4540 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4541 (string_with_suffix_of_int state.memused)
4542 (Hashtbl.length state.tilemap)) 1;
4544 sep ();
4545 src#caption "Layout" 0;
4546 src#caption2 "Dimension"
4547 (fun () ->
4548 Printf.sprintf "%dx%d (virtual %dx%d)"
4549 state.winw state.winh
4550 state.w state.maxy)
4552 if conf.debug
4553 then
4554 src#caption2 "Position" (fun () ->
4555 Printf.sprintf "%dx%d" state.x state.y
4557 else
4558 src#caption2 "Position" (fun () -> describe_location ()) 1
4561 sep ();
4562 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4563 "Save these parameters as global defaults at exit"
4564 (fun () -> conf.bedefault)
4565 (fun v -> conf.bedefault <- v)
4568 sep ();
4569 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4570 src#bool ~offset:0 ~btos "Extended parameters"
4571 (fun () -> !showextended)
4572 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4573 if !showextended
4574 then (
4575 src#bool "checkers"
4576 (fun () -> conf.checkers)
4577 (fun v -> conf.checkers <- v; setcheckers v);
4578 src#bool "update cursor"
4579 (fun () -> conf.updatecurs)
4580 (fun v -> conf.updatecurs <- v);
4581 src#bool "verbose"
4582 (fun () -> conf.verbose)
4583 (fun v -> conf.verbose <- v);
4584 src#bool "invert colors"
4585 (fun () -> conf.invert)
4586 (fun v -> conf.invert <- v);
4587 src#bool "max fit"
4588 (fun () -> conf.maxhfit)
4589 (fun v -> conf.maxhfit <- v);
4590 src#bool "redirect stderr"
4591 (fun () -> conf.redirectstderr)
4592 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4593 src#string "uri launcher"
4594 (fun () -> conf.urilauncher)
4595 (fun v -> conf.urilauncher <- v);
4596 src#string "path launcher"
4597 (fun () -> conf.pathlauncher)
4598 (fun v -> conf.pathlauncher <- v);
4599 src#string "tile size"
4600 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4601 (fun v ->
4603 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4604 conf.tilew <- max 64 w;
4605 conf.tileh <- max 64 h;
4606 flushtiles ();
4607 with exn ->
4608 state.text <- Printf.sprintf "bad tile size `%s': %s"
4609 v (exntos exn)
4611 src#int "texture count"
4612 (fun () -> conf.texcount)
4613 (fun v ->
4614 if realloctexts v
4615 then conf.texcount <- v
4616 else showtext '!' " Failed to set texture count please retry later"
4618 src#int "slice height"
4619 (fun () -> conf.sliceheight)
4620 (fun v ->
4621 conf.sliceheight <- v;
4622 wcmd "sliceh %d" conf.sliceheight;
4624 src#int "anti-aliasing level"
4625 (fun () -> conf.aalevel)
4626 (fun v ->
4627 conf.aalevel <- bound v 0 8;
4628 state.anchor <- getanchor ();
4629 opendoc state.path state.password;
4631 src#string "page scroll scaling factor"
4632 (fun () -> string_of_float conf.pgscale)
4633 (fun v ->
4635 let s = float_of_string v in
4636 conf.pgscale <- s
4637 with exn ->
4638 state.text <- Printf.sprintf
4639 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4642 src#int "ui font size"
4643 (fun () -> fstate.fontsize)
4644 (fun v -> setfontsize (bound v 5 100));
4645 src#int "hint font size"
4646 (fun () -> conf.hfsize)
4647 (fun v -> conf.hfsize <- bound v 5 100);
4648 colorp "background color"
4649 (fun () -> conf.bgcolor)
4650 (fun v -> conf.bgcolor <- v);
4651 src#bool "crop hack"
4652 (fun () -> conf.crophack)
4653 (fun v -> conf.crophack <- v);
4654 src#string "trim fuzz"
4655 (fun () -> irect_to_string conf.trimfuzz)
4656 (fun v ->
4658 conf.trimfuzz <- irect_of_string v;
4659 if conf.trimmargins
4660 then settrim true conf.trimfuzz;
4661 with exn ->
4662 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4664 src#string "throttle"
4665 (fun () ->
4666 match conf.maxwait with
4667 | None -> "show place holder if page is not ready"
4668 | Some time ->
4669 if time = infinity
4670 then "wait for page to fully render"
4671 else
4672 "wait " ^ string_of_float time
4673 ^ " seconds before showing placeholder"
4675 (fun v ->
4677 let f = float_of_string v in
4678 if f <= 0.0
4679 then conf.maxwait <- None
4680 else conf.maxwait <- Some f
4681 with exn ->
4682 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4684 src#string "ghyll scroll"
4685 (fun () ->
4686 match conf.ghyllscroll with
4687 | None -> ""
4688 | Some nab -> ghyllscroll_to_string nab
4690 (fun v ->
4692 let gs =
4693 if String.length v = 0
4694 then None
4695 else Some (ghyllscroll_of_string v)
4697 conf.ghyllscroll <- gs
4698 with exn ->
4699 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4701 src#string "selection command"
4702 (fun () -> conf.selcmd)
4703 (fun v -> conf.selcmd <- v);
4704 src#string "synctex command"
4705 (fun () -> conf.stcmd)
4706 (fun v -> conf.stcmd <- v);
4707 src#colorspace "color space"
4708 (fun () -> colorspace_to_string conf.colorspace)
4709 (fun v ->
4710 conf.colorspace <- colorspace_of_int v;
4711 wcmd "cs %d" v;
4712 load state.layout;
4714 if pbousable ()
4715 then
4716 src#bool "use PBO"
4717 (fun () -> conf.usepbo)
4718 (fun v -> conf.usepbo <- v);
4719 src#bool "mouse wheel scrolls pages"
4720 (fun () -> conf.wheelbypage)
4721 (fun v -> conf.wheelbypage <- v);
4722 src#bool "open remote links in a new instance"
4723 (fun () -> conf.riani)
4724 (fun v -> conf.riani <- v);
4727 sep ();
4728 src#caption "Document" 0;
4729 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4730 src#caption2 "Pages"
4731 (fun () -> string_of_int state.pagecount) 1;
4732 src#caption2 "Dimensions"
4733 (fun () -> string_of_int (List.length state.pdims)) 1;
4734 if conf.trimmargins
4735 then (
4736 sep ();
4737 src#caption "Trimmed margins" 0;
4738 src#caption2 "Dimensions"
4739 (fun () -> string_of_int (List.length state.pdims)) 1;
4742 sep ();
4743 src#caption "OpenGL" 0;
4744 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4745 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4747 sep ();
4748 src#caption "Location" 0;
4749 if String.length state.origin > 0
4750 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4751 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4753 src#reset prevmode prevuioh;
4755 fun () ->
4756 state.text <- "";
4757 let prevmode = state.mode
4758 and prevuioh = state.uioh in
4759 fillsrc prevmode prevuioh;
4760 let source = (src :> lvsource) in
4761 let modehash = findkeyhash conf "info" in
4762 state.uioh <- coe (object (self)
4763 inherit listview ~source ~trusted:true ~modehash as super
4764 val mutable m_prevmemused = 0
4765 method infochanged = function
4766 | Memused ->
4767 if m_prevmemused != state.memused
4768 then (
4769 m_prevmemused <- state.memused;
4770 G.postRedisplay "memusedchanged";
4772 | Pdim -> G.postRedisplay "pdimchanged"
4773 | Docinfo -> fillsrc prevmode prevuioh
4775 method key key mask =
4776 if not (Wsi.withctrl mask)
4777 then
4778 match key with
4779 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4780 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4781 | _ -> super#key key mask
4782 else super#key key mask
4783 end);
4784 G.postRedisplay "info";
4787 let enterhelpmode =
4788 let source =
4789 (object
4790 inherit lvsourcebase
4791 method getitemcount = Array.length state.help
4792 method getitem n =
4793 let s, l, _ = state.help.(n) in
4794 (s, l)
4796 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4797 let optuioh =
4798 if not cancel
4799 then (
4800 m_qsearch <- qsearch;
4801 match state.help.(active) with
4802 | _, _, Action f -> Some (f uioh)
4803 | _ -> Some (uioh)
4805 else None
4807 m_active <- active;
4808 m_first <- first;
4809 m_pan <- pan;
4810 optuioh
4812 method hasaction n =
4813 match state.help.(n) with
4814 | _, _, Action _ -> true
4815 | _ -> false
4817 initializer
4818 m_active <- -1
4819 end)
4820 in fun () ->
4821 let modehash = findkeyhash conf "help" in
4822 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4823 G.postRedisplay "help";
4826 let entermsgsmode =
4827 let msgsource =
4828 let re = Str.regexp "[\r\n]" in
4829 (object
4830 inherit lvsourcebase
4831 val mutable m_items = [||]
4833 method getitemcount = 1 + Array.length m_items
4835 method getitem n =
4836 if n = 0
4837 then "[Clear]", 0
4838 else m_items.(n-1), 0
4840 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4841 ignore uioh;
4842 if not cancel
4843 then (
4844 if active = 0
4845 then Buffer.clear state.errmsgs;
4846 m_qsearch <- qsearch;
4848 m_active <- active;
4849 m_first <- first;
4850 m_pan <- pan;
4851 None
4853 method hasaction n =
4854 n = 0
4856 method reset =
4857 state.newerrmsgs <- false;
4858 let l = Str.split re (Buffer.contents state.errmsgs) in
4859 m_items <- Array.of_list l
4861 initializer
4862 m_active <- 0
4863 end)
4864 in fun () ->
4865 state.text <- "";
4866 msgsource#reset;
4867 let source = (msgsource :> lvsource) in
4868 let modehash = findkeyhash conf "listview" in
4869 state.uioh <- coe (object
4870 inherit listview ~source ~trusted:false ~modehash as super
4871 method display =
4872 if state.newerrmsgs
4873 then msgsource#reset;
4874 super#display
4875 end);
4876 G.postRedisplay "msgs";
4879 let quickbookmark ?title () =
4880 match state.layout with
4881 | [] -> ()
4882 | l :: _ ->
4883 let title =
4884 match title with
4885 | None ->
4886 let sec = Unix.gettimeofday () in
4887 let tm = Unix.localtime sec in
4888 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4889 (l.pageno+1)
4890 tm.Unix.tm_mday
4891 tm.Unix.tm_mon
4892 (tm.Unix.tm_year + 1900)
4893 tm.Unix.tm_hour
4894 tm.Unix.tm_min
4895 | Some title -> title
4897 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4900 let setautoscrollspeed step goingdown =
4901 let incr = max 1 ((abs step) / 2) in
4902 let incr = if goingdown then incr else -incr in
4903 let astep = step + incr in
4904 state.autoscroll <- Some astep;
4907 let gotounder = function
4908 | Ulinkgoto (pageno, top) ->
4909 if pageno >= 0
4910 then (
4911 addnav ();
4912 gotopage1 pageno top;
4915 | Ulinkuri s ->
4916 gotouri s
4918 | Uremote (filename, pageno) ->
4919 let path =
4920 if String.length filename > 0
4921 then
4922 if Filename.is_relative filename
4923 then
4924 let dir = Filename.dirname state.path in
4925 let dir =
4926 if Filename.is_implicit dir
4927 then Filename.concat (Sys.getcwd ()) dir
4928 else dir
4930 Filename.concat dir filename
4931 else filename
4932 else ""
4934 let path =
4935 if Sys.file_exists path
4936 then path
4937 else ""
4939 if String.length path > 0
4940 then (
4941 if conf.riani
4942 then
4943 let command = Printf.sprintf "%s '%s'" Sys.argv.(0) path in
4944 try popen command []
4945 with exn ->
4946 Printf.eprintf
4947 "failed to execute `%s': %s\n" command (exntos exn);
4948 flush stderr;
4949 else
4950 let anchor = getanchor () in
4951 let ranchor = state.path, state.password, anchor, state.origin in
4952 state.origin <- "";
4953 state.anchor <- (pageno, 0.0, 0.0);
4954 state.ranchors <- ranchor :: state.ranchors;
4955 opendoc path "";
4957 else showtext '!' ("Could not find " ^ filename)
4959 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4962 let canpan () =
4963 match conf.columns with
4964 | Csplit _ -> true
4965 | _ -> state.x != 0 || conf.zoom > 1.0
4968 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4970 let existsinrow pageno (columns, coverA, coverB) p =
4971 let last = ((pageno - coverA) mod columns) + columns in
4972 let rec any = function
4973 | [] -> false
4974 | l :: rest ->
4975 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4976 then p l
4977 else (
4978 if not (p l)
4979 then (if l.pageno = last then false else any rest)
4980 else true
4983 any state.layout
4986 let nextpage () =
4987 match state.layout with
4988 | [] ->
4989 let pageno = page_of_y state.y in
4990 gotoghyll (getpagey (pageno+1))
4991 | l :: rest ->
4992 match conf.columns with
4993 | Csingle _ ->
4994 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4995 then
4996 let y = clamp (pgscale state.winh) in
4997 gotoghyll y
4998 else
4999 let pageno = min (l.pageno+1) (state.pagecount-1) in
5000 gotoghyll (getpagey pageno)
5001 | Cmulti ((c, _, _) as cl, _) ->
5002 if conf.presentation
5003 && (existsinrow l.pageno cl
5004 (fun l -> l.pageh > l.pagey + l.pagevh))
5005 then
5006 let y = clamp (pgscale state.winh) in
5007 gotoghyll y
5008 else
5009 let pageno = min (l.pageno+c) (state.pagecount-1) in
5010 gotoghyll (getpagey pageno)
5011 | Csplit (n, _) ->
5012 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5013 then
5014 let pagey, pageh = getpageyh l.pageno in
5015 let pagey = pagey + pageh * l.pagecol in
5016 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5017 gotoghyll (pagey + pageh + ips)
5020 let prevpage () =
5021 match state.layout with
5022 | [] ->
5023 let pageno = page_of_y state.y in
5024 gotoghyll (getpagey (pageno-1))
5025 | l :: _ ->
5026 match conf.columns with
5027 | Csingle _ ->
5028 if conf.presentation && l.pagey != 0
5029 then
5030 gotoghyll (clamp (pgscale ~-(state.winh)))
5031 else
5032 let pageno = max 0 (l.pageno-1) in
5033 gotoghyll (getpagey pageno)
5034 | Cmulti ((c, _, coverB) as cl, _) ->
5035 if conf.presentation &&
5036 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5037 then
5038 gotoghyll (clamp (pgscale ~-(state.winh)))
5039 else
5040 let decr =
5041 if l.pageno = state.pagecount - coverB
5042 then 1
5043 else c
5045 let pageno = max 0 (l.pageno-decr) in
5046 gotoghyll (getpagey pageno)
5047 | Csplit (n, _) ->
5048 let y =
5049 if l.pagecol = 0
5050 then
5051 if l.pageno = 0
5052 then l.pagey
5053 else
5054 let pageno = max 0 (l.pageno-1) in
5055 let pagey, pageh = getpageyh pageno in
5056 pagey + (n-1)*pageh
5057 else
5058 let pagey, pageh = getpageyh l.pageno in
5059 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5061 gotoghyll y
5064 let viewkeyboard key mask =
5065 let enttext te =
5066 let mode = state.mode in
5067 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5068 state.text <- "";
5069 enttext ();
5070 G.postRedisplay "view:enttext"
5072 let ctrl = Wsi.withctrl mask in
5073 let key =
5074 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5076 match key with
5077 | 81 -> (* Q *)
5078 exit 0
5080 | 0xff63 -> (* insert *)
5081 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5082 then (
5083 state.mode <- LinkNav (Ltgendir 0);
5084 gotoy state.y;
5086 else showtext '!' "Keyboard link navigation does not work under rotation"
5088 | 0xff1b | 113 -> (* escape / q *)
5089 begin match state.mstate with
5090 | Mzoomrect _ ->
5091 state.mstate <- Mnone;
5092 Wsi.setcursor Wsi.CURSOR_INHERIT;
5093 G.postRedisplay "kill zoom rect";
5094 | _ ->
5095 begin match state.mode with
5096 | LinkNav _ ->
5097 state.mode <- View;
5098 G.postRedisplay "esc leave linknav"
5099 | _ ->
5100 match state.ranchors with
5101 | [] -> raise Quit
5102 | (path, password, anchor, origin) :: rest ->
5103 state.ranchors <- rest;
5104 state.anchor <- anchor;
5105 state.origin <- origin;
5106 opendoc path password
5107 end;
5108 end;
5110 | 0xff08 -> (* backspace *)
5111 gotoghyll (getnav ~-1)
5113 | 111 -> (* o *)
5114 enteroutlinemode ()
5116 | 117 -> (* u *)
5117 state.rects <- [];
5118 state.text <- "";
5119 G.postRedisplay "dehighlight";
5121 | 47 | 63 -> (* / ? *)
5122 let ondone isforw s =
5123 cbput state.hists.pat s;
5124 state.searchpattern <- s;
5125 search s isforw
5127 let s = String.create 1 in
5128 s.[0] <- Char.chr key;
5129 enttext (s, "", Some (onhist state.hists.pat),
5130 textentry, ondone (key = 47), true)
5132 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5133 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5134 setzoom (conf.zoom +. incr)
5136 | 43 | 0xffab -> (* + *)
5137 let ondone s =
5138 let n =
5139 try int_of_string s with exc ->
5140 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5141 max_int
5143 if n != max_int
5144 then (
5145 conf.pagebias <- n;
5146 state.text <- "page bias is now " ^ string_of_int n;
5149 enttext ("page bias: ", "", None, intentry, ondone, true)
5151 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5152 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5153 setzoom (max 0.01 (conf.zoom -. decr))
5155 | 45 | 0xffad -> (* - *)
5156 let ondone msg = state.text <- msg in
5157 enttext (
5158 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5159 optentry state.mode, ondone, true
5162 | 48 when ctrl -> (* ctrl-0 *)
5163 if conf.zoom = 1.0
5164 then (
5165 state.x <- 0;
5166 gotoy state.y
5168 else setzoom 1.0
5170 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5171 let cols =
5172 match conf.columns with
5173 | Csingle _ | Cmulti _ -> 1
5174 | Csplit (n, _) -> n
5176 let h = state.winh -
5177 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5179 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5180 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5181 then setzoom zoom
5183 | 51 when ctrl -> (* ctrl-3 *)
5184 let fm =
5185 match conf.fitmodel with
5186 | FitWidth -> FitProportional
5187 | FitProportional -> FitPage
5188 | FitPage -> FitWidth
5190 state.text <- "fit model: " ^ fitmodel_to_string fm;
5191 reqlayout conf.angle fm
5193 | 0xffc6 -> (* f9 *)
5194 togglebirdseye ()
5196 | 57 when ctrl -> (* ctrl-9 *)
5197 togglebirdseye ()
5199 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5200 when not ctrl -> (* 0..9 *)
5201 let ondone s =
5202 let n =
5203 try int_of_string s with exc ->
5204 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5207 if n >= 0
5208 then (
5209 addnav ();
5210 cbput state.hists.pag (string_of_int n);
5211 gotopage1 (n + conf.pagebias - 1) 0;
5214 let pageentry text key =
5215 match Char.unsafe_chr key with
5216 | 'g' -> TEdone text
5217 | _ -> intentry text key
5219 let text = "x" in text.[0] <- Char.chr key;
5220 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5222 | 98 -> (* b *)
5223 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5224 reshape state.winw state.winh;
5226 | 108 -> (* l *)
5227 conf.hlinks <- not conf.hlinks;
5228 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5229 G.postRedisplay "toggle highlightlinks";
5231 | 70 -> (* F *)
5232 state.glinks <- true;
5233 let mode = state.mode in
5234 state.mode <- Textentry (
5235 (":", "", None, linknentry, linkndone gotounder, false),
5236 (fun _ ->
5237 state.glinks <- false;
5238 state.mode <- mode)
5240 state.text <- "";
5241 G.postRedisplay "view:linkent(F)"
5243 | 121 -> (* y *)
5244 state.glinks <- true;
5245 let mode = state.mode in
5246 state.mode <- Textentry (
5248 ":", "", None, linknentry, linkndone (fun under ->
5249 selstring (undertext under);
5250 ), false
5252 fun _ ->
5253 state.glinks <- false;
5254 state.mode <- mode
5256 state.text <- "";
5257 G.postRedisplay "view:linkent"
5259 | 97 -> (* a *)
5260 begin match state.autoscroll with
5261 | Some step ->
5262 conf.autoscrollstep <- step;
5263 state.autoscroll <- None
5264 | None ->
5265 if conf.autoscrollstep = 0
5266 then state.autoscroll <- Some 1
5267 else state.autoscroll <- Some conf.autoscrollstep
5270 | 112 when ctrl -> (* ctrl-p *)
5271 launchpath ()
5273 | 80 -> (* P *)
5274 setpresentationmode (not conf.presentation);
5275 showtext ' ' ("presentation mode " ^
5276 if conf.presentation then "on" else "off");
5278 | 102 -> (* f *)
5279 if List.mem Wsi.Fullscreen state.winstate
5280 then Wsi.reshape conf.cwinw conf.cwinh
5281 else Wsi.fullscreen ()
5283 | 112 | 78 -> (* p|N *)
5284 search state.searchpattern false
5286 | 110 | 0xffc0 -> (* n|F3 *)
5287 search state.searchpattern true
5289 | 116 -> (* t *)
5290 begin match state.layout with
5291 | [] -> ()
5292 | l :: _ ->
5293 gotoghyll (getpagey l.pageno)
5296 | 32 -> (* space *)
5297 nextpage ()
5299 | 0xff9f | 0xffff -> (* delete *)
5300 prevpage ()
5302 | 61 -> (* = *)
5303 showtext ' ' (describe_location ());
5305 | 119 -> (* w *)
5306 begin match state.layout with
5307 | [] -> ()
5308 | l :: _ ->
5309 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5310 G.postRedisplay "w"
5313 | 39 -> (* ' *)
5314 enterbookmarkmode ()
5316 | 104 | 0xffbe -> (* h|F1 *)
5317 enterhelpmode ()
5319 | 105 -> (* i *)
5320 enterinfomode ()
5322 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5323 entermsgsmode ()
5325 | 109 -> (* m *)
5326 let ondone s =
5327 match state.layout with
5328 | l :: _ ->
5329 if String.length s > 0
5330 then
5331 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5332 | _ -> ()
5334 enttext ("bookmark: ", "", None, textentry, ondone, true)
5336 | 126 -> (* ~ *)
5337 quickbookmark ();
5338 showtext ' ' "Quick bookmark added";
5340 | 122 -> (* z *)
5341 begin match state.layout with
5342 | l :: _ ->
5343 let rect = getpdimrect l.pagedimno in
5344 let w, h =
5345 if conf.crophack
5346 then
5347 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5348 truncate (1.2 *. (rect.(3) -. rect.(0))))
5349 else
5350 (truncate (rect.(1) -. rect.(0)),
5351 truncate (rect.(3) -. rect.(0)))
5353 let w = truncate ((float w)*.conf.zoom)
5354 and h = truncate ((float h)*.conf.zoom) in
5355 if w != 0 && h != 0
5356 then (
5357 state.anchor <- getanchor ();
5358 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5360 G.postRedisplay "z";
5362 | [] -> ()
5365 | 60 | 62 -> (* < > *)
5366 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5368 | 91 | 93 -> (* [ ] *)
5369 conf.colorscale <-
5370 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5372 G.postRedisplay "brightness";
5374 | 99 when state.mode = View -> (* [alt]-c *)
5375 if Wsi.withalt mask
5376 then (
5377 if conf.zoom > 1.0
5378 then
5379 let m = (wadjsb state.winw - state.w) / 2 in
5380 state.x <- m;
5381 gotoy_and_clear_text state.y
5383 else
5384 let (c, a, b), z =
5385 match state.prevcolumns with
5386 | None -> (1, 0, 0), 1.0
5387 | Some (columns, z) ->
5388 let cab =
5389 match columns with
5390 | Csplit (c, _) -> -c, 0, 0
5391 | Cmulti ((c, a, b), _) -> c, a, b
5392 | Csingle _ -> 1, 0, 0
5394 cab, z
5396 setcolumns View c a b;
5397 setzoom z
5399 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5400 setzoom state.prevzoom
5402 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5403 begin match state.autoscroll with
5404 | None ->
5405 begin match state.mode with
5406 | Birdseye beye -> upbirdseye 1 beye
5407 | _ ->
5408 if ctrl
5409 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5410 else (
5411 if not (Wsi.withshift mask) && conf.presentation
5412 then prevpage ()
5413 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5416 | Some n ->
5417 setautoscrollspeed n false
5420 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5421 begin match state.autoscroll with
5422 | None ->
5423 begin match state.mode with
5424 | Birdseye beye -> downbirdseye 1 beye
5425 | _ ->
5426 if ctrl
5427 then gotoy_and_clear_text (clamp (state.winh/2))
5428 else (
5429 if not (Wsi.withshift mask) && conf.presentation
5430 then nextpage ()
5431 else gotoy_and_clear_text (clamp conf.scrollstep)
5434 | Some n ->
5435 setautoscrollspeed n true
5438 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5439 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5440 if canpan ()
5441 then
5442 let dx =
5443 if ctrl
5444 then state.winw / 2
5445 else conf.hscrollstep
5447 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5448 state.x <- panbound (state.x + dx);
5449 gotoy_and_clear_text state.y
5450 else (
5451 state.text <- "";
5452 G.postRedisplay "left/right"
5455 | 0xff55 | 0xff9a -> (* (kp) prior *)
5456 let y =
5457 if ctrl
5458 then
5459 match state.layout with
5460 | [] -> state.y
5461 | l :: _ -> state.y - l.pagey
5462 else
5463 clamp (pgscale (-state.winh))
5465 gotoghyll y
5467 | 0xff56 | 0xff9b -> (* (kp) next *)
5468 let y =
5469 if ctrl
5470 then
5471 match List.rev state.layout with
5472 | [] -> state.y
5473 | l :: _ -> getpagey l.pageno
5474 else
5475 clamp (pgscale state.winh)
5477 gotoghyll y
5479 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5480 gotoghyll 0
5481 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5482 gotoghyll (clamp state.maxy)
5484 | 0xff53 | 0xff98
5485 when Wsi.withalt mask -> (* alt-(kp) right *)
5486 gotoghyll (getnav 1)
5487 | 0xff51 | 0xff96
5488 when Wsi.withalt mask -> (* alt-(kp) left *)
5489 gotoghyll (getnav ~-1)
5491 | 114 -> (* r *)
5492 reload ()
5494 | 118 when conf.debug -> (* v *)
5495 state.rects <- [];
5496 List.iter (fun l ->
5497 match getopaque l.pageno with
5498 | None -> ()
5499 | Some opaque ->
5500 let x0, y0, x1, y1 = pagebbox opaque in
5501 let a,b = float x0, float y0 in
5502 let c,d = float x1, float y0 in
5503 let e,f = float x1, float y1 in
5504 let h,j = float x0, float y1 in
5505 let rect = (a,b,c,d,e,f,h,j) in
5506 debugrect rect;
5507 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5508 ) state.layout;
5509 G.postRedisplay "v";
5511 | _ ->
5512 vlog "huh? %s" (Wsi.keyname key)
5515 let linknavkeyboard key mask linknav =
5516 let getpage pageno =
5517 let rec loop = function
5518 | [] -> None
5519 | l :: _ when l.pageno = pageno -> Some l
5520 | _ :: rest -> loop rest
5521 in loop state.layout
5523 let doexact (pageno, n) =
5524 match getopaque pageno, getpage pageno with
5525 | Some opaque, Some l ->
5526 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5527 then
5528 let under = getlink opaque n in
5529 G.postRedisplay "link gotounder";
5530 gotounder under;
5531 state.mode <- View;
5532 else
5533 let opt, dir =
5534 match key with
5535 | 0xff50 -> (* home *)
5536 Some (findlink opaque LDfirst), -1
5538 | 0xff57 -> (* end *)
5539 Some (findlink opaque LDlast), 1
5541 | 0xff51 -> (* left *)
5542 Some (findlink opaque (LDleft n)), -1
5544 | 0xff53 -> (* right *)
5545 Some (findlink opaque (LDright n)), 1
5547 | 0xff52 -> (* up *)
5548 Some (findlink opaque (LDup n)), -1
5550 | 0xff54 -> (* down *)
5551 Some (findlink opaque (LDdown n)), 1
5553 | _ -> None, 0
5555 let pwl l dir =
5556 begin match findpwl l.pageno dir with
5557 | Pwlnotfound -> ()
5558 | Pwl pageno ->
5559 let notfound dir =
5560 state.mode <- LinkNav (Ltgendir dir);
5561 let y, h = getpageyh pageno in
5562 let y =
5563 if dir < 0
5564 then y + h - state.winh
5565 else y
5567 gotoy y
5569 begin match getopaque pageno, getpage pageno with
5570 | Some opaque, Some _ ->
5571 let link =
5572 let ld = if dir > 0 then LDfirst else LDlast in
5573 findlink opaque ld
5575 begin match link with
5576 | Lfound m ->
5577 showlinktype (getlink opaque m);
5578 state.mode <- LinkNav (Ltexact (pageno, m));
5579 G.postRedisplay "linknav jpage";
5580 | _ -> notfound dir
5581 end;
5582 | _ -> notfound dir
5583 end;
5584 end;
5586 begin match opt with
5587 | Some Lnotfound -> pwl l dir;
5588 | Some (Lfound m) ->
5589 if m = n
5590 then pwl l dir
5591 else (
5592 let _, y0, _, y1 = getlinkrect opaque m in
5593 if y0 < l.pagey
5594 then gotopage1 l.pageno y0
5595 else (
5596 let d = fstate.fontsize + 1 in
5597 if y1 - l.pagey > l.pagevh - d
5598 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5599 else G.postRedisplay "linknav";
5601 showlinktype (getlink opaque m);
5602 state.mode <- LinkNav (Ltexact (l.pageno, m));
5605 | None -> viewkeyboard key mask
5606 end;
5607 | _ -> viewkeyboard key mask
5609 if key = 0xff63
5610 then (
5611 state.mode <- View;
5612 G.postRedisplay "leave linknav"
5614 else
5615 match linknav with
5616 | Ltgendir _ -> viewkeyboard key mask
5617 | Ltexact exact -> doexact exact
5620 let keyboard key mask =
5621 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5622 then wcmd "interrupt"
5623 else state.uioh <- state.uioh#key key mask
5626 let birdseyekeyboard key mask
5627 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5628 let incr =
5629 match conf.columns with
5630 | Csingle _ -> 1
5631 | Cmulti ((c, _, _), _) -> c
5632 | Csplit _ -> failwith "bird's eye split mode"
5634 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5635 match key with
5636 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5637 let y, h = getpageyh pageno in
5638 let top = (state.winh - h) / 2 in
5639 gotoy (max 0 (y - top))
5640 | 0xff0d (* enter *)
5641 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5642 | 0xff1b -> leavebirdseye beye true (* escape *)
5643 | 0xff52 -> upbirdseye incr beye (* up *)
5644 | 0xff54 -> downbirdseye incr beye (* down *)
5645 | 0xff51 -> upbirdseye 1 beye (* left *)
5646 | 0xff53 -> downbirdseye 1 beye (* right *)
5648 | 0xff55 -> (* prior *)
5649 begin match state.layout with
5650 | l :: _ ->
5651 if l.pagey != 0
5652 then (
5653 state.mode <- Birdseye (
5654 oconf, leftx, l.pageno, hooverpageno, anchor
5656 gotopage1 l.pageno 0;
5658 else (
5659 let layout = layout (state.y-state.winh) (pgh state.layout) in
5660 match layout with
5661 | [] -> gotoy (clamp (-state.winh))
5662 | l :: _ ->
5663 state.mode <- Birdseye (
5664 oconf, leftx, l.pageno, hooverpageno, anchor
5666 gotopage1 l.pageno 0
5669 | [] -> gotoy (clamp (-state.winh))
5670 end;
5672 | 0xff56 -> (* next *)
5673 begin match List.rev state.layout with
5674 | l :: _ ->
5675 let layout = layout (state.y + (pgh state.layout)) state.winh in
5676 begin match layout with
5677 | [] ->
5678 let incr = l.pageh - l.pagevh in
5679 if incr = 0
5680 then (
5681 state.mode <-
5682 Birdseye (
5683 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5685 G.postRedisplay "birdseye pagedown";
5687 else gotoy (clamp (incr + conf.interpagespace*2));
5689 | l :: _ ->
5690 state.mode <-
5691 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5692 gotopage1 l.pageno 0;
5695 | [] -> gotoy (clamp state.winh)
5696 end;
5698 | 0xff50 -> (* home *)
5699 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5700 gotopage1 0 0
5702 | 0xff57 -> (* end *)
5703 let pageno = state.pagecount - 1 in
5704 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5705 if not (pagevisible state.layout pageno)
5706 then
5707 let h =
5708 match List.rev state.pdims with
5709 | [] -> state.winh
5710 | (_, _, h, _) :: _ -> h
5712 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5713 else G.postRedisplay "birdseye end";
5714 | _ -> viewkeyboard key mask
5717 let drawpage l =
5718 let color =
5719 match state.mode with
5720 | Textentry _ -> scalecolor 0.4
5721 | LinkNav _
5722 | View -> scalecolor 1.0
5723 | Birdseye (_, _, pageno, hooverpageno, _) ->
5724 if l.pageno = hooverpageno
5725 then scalecolor 0.9
5726 else (
5727 if l.pageno = pageno
5728 then scalecolor 1.0
5729 else scalecolor 0.8
5732 drawtiles l color;
5735 let postdrawpage l linkindexbase =
5736 match getopaque l.pageno with
5737 | Some opaque ->
5738 if tileready l l.pagex l.pagey
5739 then
5740 let x = l.pagedispx - l.pagex
5741 and y = l.pagedispy - l.pagey in
5742 let hlmask =
5743 match conf.columns with
5744 | Csingle _ | Cmulti _ ->
5745 (if conf.hlinks then 1 else 0)
5746 + (if state.glinks
5747 && not (isbirdseye state.mode) then 2 else 0)
5748 | _ -> 0
5750 let s =
5751 match state.mode with
5752 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5753 | _ -> ""
5755 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5756 else 0
5757 | _ -> 0
5760 let scrollindicator () =
5761 let sbw, ph, sh = state.uioh#scrollph in
5762 let sbh, pw, sw = state.uioh#scrollpw in
5764 GlDraw.color (0.64, 0.64, 0.64);
5765 GlDraw.rect
5766 (float (state.winw - sbw), 0.)
5767 (float state.winw, float state.winh)
5769 GlDraw.rect
5770 (0., float (state.winh - sbh))
5771 (float (wadjsb state.winw - 1), float state.winh)
5773 GlDraw.color (0.0, 0.0, 0.0);
5775 GlDraw.rect
5776 (float (state.winw - sbw), ph)
5777 (float state.winw, ph +. sh)
5779 GlDraw.rect
5780 (pw, float (state.winh - sbh))
5781 (pw +. sw, float state.winh)
5785 let showsel () =
5786 match state.mstate with
5787 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5790 | Msel ((x0, y0), (x1, y1)) ->
5791 let rec loop = function
5792 | l :: ls ->
5793 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5794 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5795 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5796 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5797 then
5798 match getopaque l.pageno with
5799 | Some opaque ->
5800 let x0, y0 = pagetranslatepoint l x0 y0 in
5801 let x1, y1 = pagetranslatepoint l x1 y1 in
5802 seltext opaque (x0, y0, x1, y1);
5803 | _ -> ()
5804 else loop ls
5805 | [] -> ()
5807 loop state.layout
5810 let showrects rects =
5811 Gl.enable `blend;
5812 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5813 GlDraw.polygon_mode `both `fill;
5814 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5815 List.iter
5816 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5817 List.iter (fun l ->
5818 if l.pageno = pageno
5819 then (
5820 let dx = float (l.pagedispx - l.pagex) in
5821 let dy = float (l.pagedispy - l.pagey) in
5822 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5823 GlDraw.begins `quads;
5825 GlDraw.vertex2 (x0+.dx, y0+.dy);
5826 GlDraw.vertex2 (x1+.dx, y1+.dy);
5827 GlDraw.vertex2 (x2+.dx, y2+.dy);
5828 GlDraw.vertex2 (x3+.dx, y3+.dy);
5830 GlDraw.ends ();
5832 ) state.layout
5833 ) rects
5835 Gl.disable `blend;
5838 let display () =
5839 GlClear.color (scalecolor2 conf.bgcolor);
5840 GlClear.clear [`color];
5841 List.iter drawpage state.layout;
5842 let rects =
5843 match state.mode with
5844 | LinkNav (Ltexact (pageno, linkno)) ->
5845 begin match getopaque pageno with
5846 | Some opaque ->
5847 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5848 (pageno, 5, (
5849 float x0, float y0,
5850 float x1, float y0,
5851 float x1, float y1,
5852 float x0, float y1)
5853 ) :: state.rects
5854 | None -> state.rects
5856 | _ -> state.rects
5858 showrects rects;
5859 let rec postloop linkindexbase = function
5860 | l :: rest ->
5861 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5862 postloop linkindexbase rest
5863 | [] -> ()
5865 showsel ();
5866 postloop 0 state.layout;
5867 state.uioh#display;
5868 begin match state.mstate with
5869 | Mzoomrect ((x0, y0), (x1, y1)) ->
5870 Gl.enable `blend;
5871 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5872 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5873 GlDraw.rect (float x0, float y0)
5874 (float x1, float y1);
5875 Gl.disable `blend;
5876 | _ -> ()
5877 end;
5878 enttext ();
5879 scrollindicator ();
5880 Wsi.swapb ();
5883 let zoomrect x y x1 y1 =
5884 let x0 = min x x1
5885 and x1 = max x x1
5886 and y0 = min y y1 in
5887 gotoy (state.y + y0);
5888 state.anchor <- getanchor ();
5889 let zoom = (float state.w) /. float (x1 - x0) in
5890 let margin =
5891 match conf.fitmodel, conf.columns with
5892 | FitPage, Csplit _ ->
5893 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5895 | _, _ ->
5896 let adjw = wadjsb state.winw in
5897 if state.w < adjw
5898 then (adjw - state.w) / 2
5899 else 0
5901 state.x <- (state.x + margin) - x0;
5902 setzoom zoom;
5903 Wsi.setcursor Wsi.CURSOR_INHERIT;
5904 state.mstate <- Mnone;
5907 let scrollx x =
5908 let winw = wadjsb state.winw - 1 in
5909 let s = float x /. float winw in
5910 let destx = truncate (float (state.w + winw) *. s) in
5911 state.x <- winw - destx;
5912 gotoy_and_clear_text state.y;
5913 state.mstate <- Mscrollx;
5916 let scrolly y =
5917 let s = float y /. float state.winh in
5918 let desty = truncate (float (state.maxy - state.winh) *. s) in
5919 gotoy_and_clear_text desty;
5920 state.mstate <- Mscrolly;
5923 let viewmouse button down x y mask =
5924 match button with
5925 | n when (n == 4 || n == 5) && not down ->
5926 if Wsi.withctrl mask
5927 then (
5928 match state.mstate with
5929 | Mzoom (oldn, i) ->
5930 if oldn = n
5931 then (
5932 if i = 2
5933 then
5934 let incr =
5935 match n with
5936 | 5 ->
5937 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5938 | _ ->
5939 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5941 let zoom = conf.zoom -. incr in
5942 setzoom zoom;
5943 state.mstate <- Mzoom (n, 0);
5944 else
5945 state.mstate <- Mzoom (n, i+1);
5947 else state.mstate <- Mzoom (n, 0)
5949 | _ -> state.mstate <- Mzoom (n, 0)
5951 else (
5952 match state.autoscroll with
5953 | Some step -> setautoscrollspeed step (n=4)
5954 | None ->
5955 if conf.wheelbypage || conf.presentation
5956 then (
5957 if n = 4
5958 then prevpage ()
5959 else nextpage ()
5961 else
5962 let incr =
5963 if n = 4
5964 then -conf.scrollstep
5965 else conf.scrollstep
5967 let incr = incr * 2 in
5968 let y = clamp incr in
5969 gotoy_and_clear_text y
5972 | n when (n = 6 || n = 7) && not down && canpan () ->
5973 state.x <-
5974 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5975 gotoy_and_clear_text state.y
5977 | 1 when Wsi.withshift mask ->
5978 state.mstate <- Mnone;
5979 if not down
5980 then (
5981 match unproject x y with
5982 | Some (pageno, ux, uy) ->
5983 let cmd = Printf.sprintf
5984 "%s %s %d %d %d"
5985 conf.stcmd state.path pageno ux uy
5987 popen cmd []
5988 | None -> ()
5991 | 1 when Wsi.withctrl mask ->
5992 if down
5993 then (
5994 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5995 state.mstate <- Mpan (x, y)
5997 else
5998 state.mstate <- Mnone
6000 | 3 ->
6001 if down
6002 then (
6003 Wsi.setcursor Wsi.CURSOR_CYCLE;
6004 let p = (x, y) in
6005 state.mstate <- Mzoomrect (p, p)
6007 else (
6008 match state.mstate with
6009 | Mzoomrect ((x0, y0), _) ->
6010 if abs (x-x0) > 10 && abs (y - y0) > 10
6011 then zoomrect x0 y0 x y
6012 else (
6013 state.mstate <- Mnone;
6014 Wsi.setcursor Wsi.CURSOR_INHERIT;
6015 G.postRedisplay "kill accidental zoom rect";
6017 | _ ->
6018 Wsi.setcursor Wsi.CURSOR_INHERIT;
6019 state.mstate <- Mnone
6022 | 1 when x > state.winw - vscrollw () ->
6023 if down
6024 then
6025 let _, position, sh = state.uioh#scrollph in
6026 if y > truncate position && y < truncate (position +. sh)
6027 then state.mstate <- Mscrolly
6028 else scrolly y
6029 else
6030 state.mstate <- Mnone
6032 | 1 when y > state.winh - hscrollh () ->
6033 if down
6034 then
6035 let _, position, sw = state.uioh#scrollpw in
6036 if x > truncate position && x < truncate (position +. sw)
6037 then state.mstate <- Mscrollx
6038 else scrollx x
6039 else
6040 state.mstate <- Mnone
6042 | 1 ->
6043 let dest = if down then getunder x y else Unone in
6044 begin match dest with
6045 | Ulinkgoto _
6046 | Ulinkuri _
6047 | Uremote _
6048 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6049 gotounder dest
6051 | Unone when down ->
6052 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6053 state.mstate <- Mpan (x, y);
6055 | Unone | Utext _ ->
6056 if down
6057 then (
6058 if conf.angle mod 360 = 0
6059 then (
6060 state.mstate <- Msel ((x, y), (x, y));
6061 G.postRedisplay "mouse select";
6064 else (
6065 match state.mstate with
6066 | Mnone -> ()
6068 | Mzoom _ | Mscrollx | Mscrolly ->
6069 state.mstate <- Mnone
6071 | Mzoomrect ((x0, y0), _) ->
6072 zoomrect x0 y0 x y
6074 | Mpan _ ->
6075 Wsi.setcursor Wsi.CURSOR_INHERIT;
6076 state.mstate <- Mnone
6078 | Msel ((x0, y0), (x1, y1)) ->
6079 let rec loop = function
6080 | [] -> ()
6081 | l :: rest ->
6082 let inside =
6083 let a0 = l.pagedispy in
6084 let a1 = a0 + l.pagevh in
6085 let b0 = l.pagedispx in
6086 let b1 = b0 + l.pagevw in
6087 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6088 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6090 if inside
6091 then
6092 match getopaque l.pageno with
6093 | Some opaque ->
6094 begin
6095 match Ne.pipe () with
6096 | Ne.Exn exn ->
6097 showtext '!'
6098 (Printf.sprintf
6099 "can not create sel pipe: %s"
6100 (exntos exn));
6101 | Ne.Res (r, w) ->
6102 let doclose what fd =
6103 Ne.clo fd (fun msg ->
6104 dolog "%s close failed: %s" what msg)
6107 popen conf.selcmd [r, 0; w, -1];
6108 copysel w opaque;
6109 doclose "pipe/r" r;
6110 G.postRedisplay "copysel";
6111 with exn ->
6112 dolog "can not execute %S: %s"
6113 conf.selcmd (exntos exn);
6114 doclose "pipe/r" r;
6115 doclose "pipe/w" w;
6117 | None -> ()
6118 else loop rest
6120 loop state.layout;
6121 Wsi.setcursor Wsi.CURSOR_INHERIT;
6122 state.mstate <- Mnone;
6126 | _ -> ()
6129 let birdseyemouse button down x y mask
6130 (conf, leftx, _, hooverpageno, anchor) =
6131 match button with
6132 | 1 when down ->
6133 let rec loop = function
6134 | [] -> ()
6135 | l :: rest ->
6136 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6137 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6138 then (
6139 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6141 else loop rest
6143 loop state.layout
6144 | 3 -> ()
6145 | _ -> viewmouse button down x y mask
6148 let mouse button down x y mask =
6149 state.uioh <- state.uioh#button button down x y mask;
6152 let motion ~x ~y =
6153 state.uioh <- state.uioh#motion x y
6156 let pmotion ~x ~y =
6157 state.uioh <- state.uioh#pmotion x y;
6160 let uioh = object
6161 method display = ()
6163 method key key mask =
6164 begin match state.mode with
6165 | Textentry textentry -> textentrykeyboard key mask textentry
6166 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6167 | View -> viewkeyboard key mask
6168 | LinkNav linknav -> linknavkeyboard key mask linknav
6169 end;
6170 state.uioh
6172 method button button bstate x y mask =
6173 begin match state.mode with
6174 | LinkNav _
6175 | View -> viewmouse button bstate x y mask
6176 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6177 | Textentry _ -> ()
6178 end;
6179 state.uioh
6181 method motion x y =
6182 begin match state.mode with
6183 | Textentry _ -> ()
6184 | View | Birdseye _ | LinkNav _ ->
6185 match state.mstate with
6186 | Mzoom _ | Mnone -> ()
6188 | Mpan (x0, y0) ->
6189 let dx = x - x0
6190 and dy = y0 - y in
6191 state.mstate <- Mpan (x, y);
6192 if canpan ()
6193 then state.x <- panbound (state.x + dx);
6194 let y = clamp dy in
6195 gotoy_and_clear_text y
6197 | Msel (a, _) ->
6198 state.mstate <- Msel (a, (x, y));
6199 G.postRedisplay "motion select";
6201 | Mscrolly ->
6202 let y = min state.winh (max 0 y) in
6203 scrolly y
6205 | Mscrollx ->
6206 let x = min state.winw (max 0 x) in
6207 scrollx x
6209 | Mzoomrect (p0, _) ->
6210 state.mstate <- Mzoomrect (p0, (x, y));
6211 G.postRedisplay "motion zoomrect";
6212 end;
6213 state.uioh
6215 method pmotion x y =
6216 begin match state.mode with
6217 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6218 let rec loop = function
6219 | [] ->
6220 if hooverpageno != -1
6221 then (
6222 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6223 G.postRedisplay "pmotion birdseye no hoover";
6225 | l :: rest ->
6226 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6227 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6228 then (
6229 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6230 G.postRedisplay "pmotion birdseye hoover";
6232 else loop rest
6234 loop state.layout
6236 | Textentry _ -> ()
6238 | LinkNav _
6239 | View ->
6240 match state.mstate with
6241 | Mnone -> updateunder x y
6242 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6244 end;
6245 state.uioh
6247 method infochanged _ = ()
6249 method scrollph =
6250 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6251 let p, h =
6252 if maxy = 0
6253 then 0.0, float state.winh
6254 else scrollph state.y maxy
6256 vscrollw (), p, h
6258 method scrollpw =
6259 let winw = wadjsb state.winw in
6260 let fwinw = float winw in
6261 let sw =
6262 let sw = fwinw /. float state.w in
6263 let sw = fwinw *. sw in
6264 max sw (float conf.scrollh)
6266 let position =
6267 let maxx = state.w + winw in
6268 let x = winw - state.x in
6269 let percent = float x /. float maxx in
6270 (fwinw -. sw) *. percent
6272 hscrollh (), position, sw
6274 method modehash =
6275 let modename =
6276 match state.mode with
6277 | LinkNav _ -> "links"
6278 | Textentry _ -> "textentry"
6279 | Birdseye _ -> "birdseye"
6280 | View -> "view"
6282 findkeyhash conf modename
6284 method eformsgs = true
6285 end;;
6287 module Config =
6288 struct
6289 open Parser
6291 let fontpath = ref "";;
6293 module KeyMap =
6294 Map.Make (struct type t = (int * int) let compare = compare end);;
6296 let unent s =
6297 let l = String.length s in
6298 let b = Buffer.create l in
6299 unent b s 0 l;
6300 Buffer.contents b;
6303 let home =
6304 try Sys.getenv "HOME"
6305 with exn ->
6306 prerr_endline
6307 ("Can not determine home directory location: " ^ exntos exn);
6311 let modifier_of_string = function
6312 | "alt" -> Wsi.altmask
6313 | "shift" -> Wsi.shiftmask
6314 | "ctrl" | "control" -> Wsi.ctrlmask
6315 | "meta" -> Wsi.metamask
6316 | _ -> 0
6319 let key_of_string =
6320 let r = Str.regexp "-" in
6321 fun s ->
6322 let elems = Str.full_split r s in
6323 let f n k m =
6324 let g s =
6325 let m1 = modifier_of_string s in
6326 if m1 = 0
6327 then (Wsi.namekey s, m)
6328 else (k, m lor m1)
6329 in function
6330 | Str.Delim s when n land 1 = 0 -> g s
6331 | Str.Text s -> g s
6332 | Str.Delim _ -> (k, m)
6334 let rec loop n k m = function
6335 | [] -> (k, m)
6336 | x :: xs ->
6337 let k, m = f n k m x in
6338 loop (n+1) k m xs
6340 loop 0 0 0 elems
6343 let keys_of_string =
6344 let r = Str.regexp "[ \t]" in
6345 fun s ->
6346 let elems = Str.split r s in
6347 List.map key_of_string elems
6350 let copykeyhashes c =
6351 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6354 let config_of c attrs =
6355 let apply c k v =
6357 match k with
6358 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6359 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6360 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6361 | "preload" -> { c with preload = bool_of_string v }
6362 | "page-bias" -> { c with pagebias = int_of_string v }
6363 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6364 | "horizontal-scroll-step" ->
6365 { c with hscrollstep = max (int_of_string v) 1 }
6366 | "auto-scroll-step" ->
6367 { c with autoscrollstep = max 0 (int_of_string v) }
6368 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6369 | "crop-hack" -> { c with crophack = bool_of_string v }
6370 | "throttle" ->
6371 let mw =
6372 match String.lowercase v with
6373 | "true" -> Some infinity
6374 | "false" -> None
6375 | f -> Some (float_of_string f)
6377 { c with maxwait = mw}
6378 | "highlight-links" -> { c with hlinks = bool_of_string v }
6379 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6380 | "vertical-margin" ->
6381 { c with interpagespace = max 0 (int_of_string v) }
6382 | "zoom" ->
6383 let zoom = float_of_string v /. 100. in
6384 let zoom = max zoom 0.0 in
6385 { c with zoom = zoom }
6386 | "presentation" -> { c with presentation = bool_of_string v }
6387 | "rotation-angle" -> { c with angle = int_of_string v }
6388 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6389 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6390 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6391 | "proportional-display" ->
6392 let fm =
6393 if bool_of_string v
6394 then FitProportional
6395 else FitWidth
6397 { c with fitmodel = fm }
6398 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6399 | "pixmap-cache-size" ->
6400 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6401 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6402 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6403 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6404 | "persistent-location" -> { c with jumpback = bool_of_string v }
6405 | "background-color" -> { c with bgcolor = color_of_string v }
6406 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6407 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6408 | "mupdf-store-size" ->
6409 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6410 | "checkers" -> { c with checkers = bool_of_string v }
6411 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6412 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6413 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6414 | "uri-launcher" -> { c with urilauncher = unent v }
6415 | "path-launcher" -> { c with pathlauncher = unent v }
6416 | "color-space" -> { c with colorspace = colorspace_of_string v }
6417 | "invert-colors" -> { c with invert = bool_of_string v }
6418 | "brightness" -> { c with colorscale = float_of_string v }
6419 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6420 | "ghyllscroll" ->
6421 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6422 | "columns" ->
6423 let (n, _, _) as nab = multicolumns_of_string v in
6424 if n < 0
6425 then { c with columns = Csplit (-n, [||]) }
6426 else { c with columns = Cmulti (nab, [||]) }
6427 | "birds-eye-columns" ->
6428 { c with beyecolumns = Some (max (int_of_string v) 2) }
6429 | "selection-command" -> { c with selcmd = unent v }
6430 | "synctex-command" -> { c with stcmd = unent v }
6431 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6432 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6433 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6434 | "use-pbo" -> { c with usepbo = bool_of_string v }
6435 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6436 | "horizontal-scrollbar-visible" ->
6437 let b =
6438 if bool_of_string v
6439 then c.scrollb lor scrollbhv
6440 else c.scrollb land (lnot scrollbhv)
6442 { c with scrollb = b }
6443 | "vertical-scrollbar-visible" ->
6444 let b =
6445 if bool_of_string v
6446 then c.scrollb lor scrollbvv
6447 else c.scrollb land (lnot scrollbvv)
6449 { c with scrollb = b }
6450 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6451 | _ -> c
6452 with exn ->
6453 prerr_endline ("Error processing attribute (`" ^
6454 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6457 let rec fold c = function
6458 | [] -> c
6459 | (k, v) :: rest ->
6460 let c = apply c k v in
6461 fold c rest
6463 fold { c with keyhashes = copykeyhashes c } attrs;
6466 let fromstring f pos n v d =
6467 try f v
6468 with exn ->
6469 dolog "Error processing attribute (%S=%S) at %d\n%s"
6470 n v pos (exntos exn)
6475 let bookmark_of attrs =
6476 let rec fold title page rely visy = function
6477 | ("title", v) :: rest -> fold v page rely visy rest
6478 | ("page", v) :: rest -> fold title v rely visy rest
6479 | ("rely", v) :: rest -> fold title page v visy rest
6480 | ("visy", v) :: rest -> fold title page rely v rest
6481 | _ :: rest -> fold title page rely visy rest
6482 | [] -> title, page, rely, visy
6484 fold "invalid" "0" "0" "0" attrs
6487 let doc_of attrs =
6488 let rec fold path page rely pan visy = function
6489 | ("path", v) :: rest -> fold v page rely pan visy rest
6490 | ("page", v) :: rest -> fold path v rely pan visy rest
6491 | ("rely", v) :: rest -> fold path page v pan visy rest
6492 | ("pan", v) :: rest -> fold path page rely v visy rest
6493 | ("visy", v) :: rest -> fold path page rely pan v rest
6494 | _ :: rest -> fold path page rely pan visy rest
6495 | [] -> path, page, rely, pan, visy
6497 fold "" "0" "0" "0" "0" attrs
6500 let map_of attrs =
6501 let rec fold rs ls = function
6502 | ("out", v) :: rest -> fold v ls rest
6503 | ("in", v) :: rest -> fold rs v rest
6504 | _ :: rest -> fold ls rs rest
6505 | [] -> ls, rs
6507 fold "" "" attrs
6510 let setconf dst src =
6511 dst.scrollbw <- src.scrollbw;
6512 dst.scrollh <- src.scrollh;
6513 dst.icase <- src.icase;
6514 dst.preload <- src.preload;
6515 dst.pagebias <- src.pagebias;
6516 dst.verbose <- src.verbose;
6517 dst.scrollstep <- src.scrollstep;
6518 dst.maxhfit <- src.maxhfit;
6519 dst.crophack <- src.crophack;
6520 dst.autoscrollstep <- src.autoscrollstep;
6521 dst.maxwait <- src.maxwait;
6522 dst.hlinks <- src.hlinks;
6523 dst.underinfo <- src.underinfo;
6524 dst.interpagespace <- src.interpagespace;
6525 dst.zoom <- src.zoom;
6526 dst.presentation <- src.presentation;
6527 dst.angle <- src.angle;
6528 dst.cwinw <- src.cwinw;
6529 dst.cwinh <- src.cwinh;
6530 dst.savebmarks <- src.savebmarks;
6531 dst.memlimit <- src.memlimit;
6532 dst.fitmodel <- src.fitmodel;
6533 dst.texcount <- src.texcount;
6534 dst.sliceheight <- src.sliceheight;
6535 dst.thumbw <- src.thumbw;
6536 dst.jumpback <- src.jumpback;
6537 dst.bgcolor <- src.bgcolor;
6538 dst.tilew <- src.tilew;
6539 dst.tileh <- src.tileh;
6540 dst.mustoresize <- src.mustoresize;
6541 dst.checkers <- src.checkers;
6542 dst.aalevel <- src.aalevel;
6543 dst.trimmargins <- src.trimmargins;
6544 dst.trimfuzz <- src.trimfuzz;
6545 dst.urilauncher <- src.urilauncher;
6546 dst.colorspace <- src.colorspace;
6547 dst.invert <- src.invert;
6548 dst.colorscale <- src.colorscale;
6549 dst.redirectstderr <- src.redirectstderr;
6550 dst.ghyllscroll <- src.ghyllscroll;
6551 dst.columns <- src.columns;
6552 dst.beyecolumns <- src.beyecolumns;
6553 dst.selcmd <- src.selcmd;
6554 dst.updatecurs <- src.updatecurs;
6555 dst.pathlauncher <- src.pathlauncher;
6556 dst.keyhashes <- copykeyhashes src;
6557 dst.hfsize <- src.hfsize;
6558 dst.hscrollstep <- src.hscrollstep;
6559 dst.pgscale <- src.pgscale;
6560 dst.usepbo <- src.usepbo;
6561 dst.wheelbypage <- src.wheelbypage;
6562 dst.stcmd <- src.stcmd;
6563 dst.scrollb <- src.scrollb;
6564 dst.riani <- src.riani;
6567 let get s =
6568 let h = Hashtbl.create 10 in
6569 let dc = { defconf with angle = defconf.angle } in
6570 let rec toplevel v t spos _ =
6571 match t with
6572 | Vdata | Vcdata | Vend -> v
6573 | Vopen ("llppconfig", _, closed) ->
6574 if closed
6575 then v
6576 else { v with f = llppconfig }
6577 | Vopen _ ->
6578 error "unexpected subelement at top level" s spos
6579 | Vclose _ -> error "unexpected close at top level" s spos
6581 and llppconfig v t spos _ =
6582 match t with
6583 | Vdata | Vcdata -> v
6584 | Vend -> error "unexpected end of input in llppconfig" s spos
6585 | Vopen ("defaults", attrs, closed) ->
6586 let c = config_of dc attrs in
6587 setconf dc c;
6588 if closed
6589 then v
6590 else { v with f = defaults }
6592 | Vopen ("ui-font", attrs, closed) ->
6593 let rec getsize size = function
6594 | [] -> size
6595 | ("size", v) :: rest ->
6596 let size =
6597 fromstring int_of_string spos "size" v fstate.fontsize in
6598 getsize size rest
6599 | l -> getsize size l
6601 fstate.fontsize <- getsize fstate.fontsize attrs;
6602 if closed
6603 then v
6604 else { v with f = uifont (Buffer.create 10) }
6606 | Vopen ("doc", attrs, closed) ->
6607 let pathent, spage, srely, span, svisy = doc_of attrs in
6608 let path = unent pathent
6609 and pageno = fromstring int_of_string spos "page" spage 0
6610 and rely = fromstring float_of_string spos "rely" srely 0.0
6611 and pan = fromstring int_of_string spos "pan" span 0
6612 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6613 let c = config_of dc attrs in
6614 let anchor = (pageno, rely, visy) in
6615 if closed
6616 then (Hashtbl.add h path (c, [], pan, anchor); v)
6617 else { v with f = doc path pan anchor c [] }
6619 | Vopen _ ->
6620 error "unexpected subelement in llppconfig" s spos
6622 | Vclose "llppconfig" -> { v with f = toplevel }
6623 | Vclose _ -> error "unexpected close in llppconfig" s spos
6625 and defaults v t spos _ =
6626 match t with
6627 | Vdata | Vcdata -> v
6628 | Vend -> error "unexpected end of input in defaults" s spos
6629 | Vopen ("keymap", attrs, closed) ->
6630 let modename =
6631 try List.assoc "mode" attrs
6632 with Not_found -> "global" in
6633 if closed
6634 then v
6635 else
6636 let ret keymap =
6637 let h = findkeyhash dc modename in
6638 KeyMap.iter (Hashtbl.replace h) keymap;
6639 defaults
6641 { v with f = pkeymap ret KeyMap.empty }
6643 | Vopen (_, _, _) ->
6644 error "unexpected subelement in defaults" s spos
6646 | Vclose "defaults" ->
6647 { v with f = llppconfig }
6649 | Vclose _ -> error "unexpected close in defaults" s spos
6651 and uifont b v t spos epos =
6652 match t with
6653 | Vdata | Vcdata ->
6654 Buffer.add_substring b s spos (epos - spos);
6656 | Vopen (_, _, _) ->
6657 error "unexpected subelement in ui-font" s spos
6658 | Vclose "ui-font" ->
6659 if String.length !fontpath = 0
6660 then fontpath := Buffer.contents b;
6661 { v with f = llppconfig }
6662 | Vclose _ -> error "unexpected close in ui-font" s spos
6663 | Vend -> error "unexpected end of input in ui-font" s spos
6665 and doc path pan anchor c bookmarks v t spos _ =
6666 match t with
6667 | Vdata | Vcdata -> v
6668 | Vend -> error "unexpected end of input in doc" s spos
6669 | Vopen ("bookmarks", _, closed) ->
6670 if closed
6671 then v
6672 else { v with f = pbookmarks path pan anchor c bookmarks }
6674 | Vopen ("keymap", attrs, closed) ->
6675 let modename =
6676 try List.assoc "mode" attrs
6677 with Not_found -> "global"
6679 if closed
6680 then v
6681 else
6682 let ret keymap =
6683 let h = findkeyhash c modename in
6684 KeyMap.iter (Hashtbl.replace h) keymap;
6685 doc path pan anchor c bookmarks
6687 { v with f = pkeymap ret KeyMap.empty }
6689 | Vopen (_, _, _) ->
6690 error "unexpected subelement in doc" s spos
6692 | Vclose "doc" ->
6693 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6694 { v with f = llppconfig }
6696 | Vclose _ -> error "unexpected close in doc" s spos
6698 and pkeymap ret keymap v t spos _ =
6699 match t with
6700 | Vdata | Vcdata -> v
6701 | Vend -> error "unexpected end of input in keymap" s spos
6702 | Vopen ("map", attrs, closed) ->
6703 let r, l = map_of attrs in
6704 let kss = fromstring keys_of_string spos "in" r [] in
6705 let lss = fromstring keys_of_string spos "out" l [] in
6706 let keymap =
6707 match kss with
6708 | [] -> keymap
6709 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6710 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6712 if closed
6713 then { v with f = pkeymap ret keymap }
6714 else
6715 let f () = v in
6716 { v with f = skip "map" f }
6718 | Vopen _ ->
6719 error "unexpected subelement in keymap" s spos
6721 | Vclose "keymap" ->
6722 { v with f = ret keymap }
6724 | Vclose _ -> error "unexpected close in keymap" s spos
6726 and pbookmarks path pan anchor c bookmarks v t spos _ =
6727 match t with
6728 | Vdata | Vcdata -> v
6729 | Vend -> error "unexpected end of input in bookmarks" s spos
6730 | Vopen ("item", attrs, closed) ->
6731 let titleent, spage, srely, svisy = bookmark_of attrs in
6732 let page = fromstring int_of_string spos "page" spage 0
6733 and rely = fromstring float_of_string spos "rely" srely 0.0
6734 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6735 let bookmarks =
6736 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6738 if closed
6739 then { v with f = pbookmarks path pan anchor c bookmarks }
6740 else
6741 let f () = v in
6742 { v with f = skip "item" f }
6744 | Vopen _ ->
6745 error "unexpected subelement in bookmarks" s spos
6747 | Vclose "bookmarks" ->
6748 { v with f = doc path pan anchor c bookmarks }
6750 | Vclose _ -> error "unexpected close in bookmarks" s spos
6752 and skip tag f v t spos _ =
6753 match t with
6754 | Vdata | Vcdata -> v
6755 | Vend ->
6756 error ("unexpected end of input in skipped " ^ tag) s spos
6757 | Vopen (tag', _, closed) ->
6758 if closed
6759 then v
6760 else
6761 let f' () = { v with f = skip tag f } in
6762 { v with f = skip tag' f' }
6763 | Vclose ctag ->
6764 if tag = ctag
6765 then f ()
6766 else error ("unexpected close in skipped " ^ tag) s spos
6769 parse { f = toplevel; accu = () } s;
6770 h, dc;
6773 let do_load f ic =
6775 let len = in_channel_length ic in
6776 let s = String.create len in
6777 really_input ic s 0 len;
6778 f s;
6779 with
6780 | Parse_error (msg, s, pos) ->
6781 let subs = subs s pos in
6782 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6783 failwith ("parse error: " ^ s)
6785 | exn ->
6786 failwith ("config load error: " ^ exntos exn)
6789 let defconfpath =
6790 let dir =
6792 let dir = Filename.concat home ".config" in
6793 if Sys.is_directory dir then dir else home
6794 with _ -> home
6796 Filename.concat dir "llpp.conf"
6799 let confpath = ref defconfpath;;
6801 let load1 f =
6802 if Sys.file_exists !confpath
6803 then
6804 match
6805 (try Some (open_in_bin !confpath)
6806 with exn ->
6807 prerr_endline
6808 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6809 exntos exn);
6810 None
6812 with
6813 | Some ic ->
6814 let success =
6816 f (do_load get ic)
6817 with exn ->
6818 prerr_endline
6819 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6820 exntos exn);
6821 false
6823 close_in ic;
6824 success
6826 | None -> false
6827 else
6828 f (Hashtbl.create 0, defconf)
6831 let load () =
6832 let f (h, dc) =
6833 let pc, pb, px, pa =
6835 let key =
6836 if String.length state.origin = 0
6837 then state.path
6838 else state.origin
6840 Hashtbl.find h (Filename.basename key)
6841 with Not_found -> dc, [], 0, emptyanchor
6843 setconf defconf dc;
6844 setconf conf pc;
6845 state.bookmarks <- pb;
6846 state.x <- px;
6847 if conf.jumpback
6848 then state.anchor <- pa;
6849 cbput state.hists.nav pa;
6850 true
6852 load1 f
6855 let add_attrs bb always dc c =
6856 let ob s a b =
6857 if always || a != b
6858 then Printf.bprintf bb "\n %s='%b'" s a
6859 and oi s a b =
6860 if always || a != b
6861 then Printf.bprintf bb "\n %s='%d'" s a
6862 and oI s a b =
6863 if always || a != b
6864 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6865 and oz s a b =
6866 if always || a <> b
6867 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6868 and oF s a b =
6869 if always || a <> b
6870 then Printf.bprintf bb "\n %s='%f'" s a
6871 and oc s a b =
6872 if always || a <> b
6873 then
6874 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6875 and oC s a b =
6876 if always || a <> b
6877 then
6878 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6879 and oR s a b =
6880 if always || a <> b
6881 then
6882 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6883 and os s a b =
6884 if always || a <> b
6885 then
6886 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6887 and og s a b =
6888 if always || a <> b
6889 then
6890 match a with
6891 | None -> ()
6892 | Some (_N, _A, _B) ->
6893 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6894 and oW s a b =
6895 if always || a <> b
6896 then
6897 let v =
6898 match a with
6899 | None -> "false"
6900 | Some f ->
6901 if f = infinity
6902 then "true"
6903 else string_of_float f
6905 Printf.bprintf bb "\n %s='%s'" s v
6906 and oco s a b =
6907 if always || a <> b
6908 then
6909 match a with
6910 | Cmulti ((n, a, b), _) when n > 1 ->
6911 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6912 | Csplit (n, _) when n > 1 ->
6913 Printf.bprintf bb "\n %s='%d'" s ~-n
6914 | _ -> ()
6915 and obeco s a b =
6916 if always || a <> b
6917 then
6918 match a with
6919 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6920 | _ -> ()
6921 and oFm s a b =
6922 if always || a <> b
6923 then
6924 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6925 and oSv s a b m =
6926 if always || a <> b
6927 then
6928 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
6930 oi "width" c.cwinw dc.cwinw;
6931 oi "height" c.cwinh dc.cwinh;
6932 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6933 oi "scroll-handle-height" c.scrollh dc.scrollh;
6934 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
6935 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
6936 ob "case-insensitive-search" c.icase dc.icase;
6937 ob "preload" c.preload dc.preload;
6938 oi "page-bias" c.pagebias dc.pagebias;
6939 oi "scroll-step" c.scrollstep dc.scrollstep;
6940 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6941 ob "max-height-fit" c.maxhfit dc.maxhfit;
6942 ob "crop-hack" c.crophack dc.crophack;
6943 oW "throttle" c.maxwait dc.maxwait;
6944 ob "highlight-links" c.hlinks dc.hlinks;
6945 ob "under-cursor-info" c.underinfo dc.underinfo;
6946 oi "vertical-margin" c.interpagespace dc.interpagespace;
6947 oz "zoom" c.zoom dc.zoom;
6948 ob "presentation" c.presentation dc.presentation;
6949 oi "rotation-angle" c.angle dc.angle;
6950 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6951 oFm "fit-model" c.fitmodel dc.fitmodel;
6952 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6953 oi "tex-count" c.texcount dc.texcount;
6954 oi "slice-height" c.sliceheight dc.sliceheight;
6955 oi "thumbnail-width" c.thumbw dc.thumbw;
6956 ob "persistent-location" c.jumpback dc.jumpback;
6957 oc "background-color" c.bgcolor dc.bgcolor;
6958 oi "tile-width" c.tilew dc.tilew;
6959 oi "tile-height" c.tileh dc.tileh;
6960 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6961 ob "checkers" c.checkers dc.checkers;
6962 oi "aalevel" c.aalevel dc.aalevel;
6963 ob "trim-margins" c.trimmargins dc.trimmargins;
6964 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6965 os "uri-launcher" c.urilauncher dc.urilauncher;
6966 os "path-launcher" c.pathlauncher dc.pathlauncher;
6967 oC "color-space" c.colorspace dc.colorspace;
6968 ob "invert-colors" c.invert dc.invert;
6969 oF "brightness" c.colorscale dc.colorscale;
6970 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6971 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6972 oco "columns" c.columns dc.columns;
6973 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6974 os "selection-command" c.selcmd dc.selcmd;
6975 os "synctex-command" c.stcmd dc.stcmd;
6976 ob "update-cursor" c.updatecurs dc.updatecurs;
6977 oi "hint-font-size" c.hfsize dc.hfsize;
6978 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6979 oF "page-scroll-scale" c.pgscale dc.pgscale;
6980 ob "use-pbo" c.usepbo dc.usepbo;
6981 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6982 ob "remote-in-a-new-instance" c.riani dc.riani;
6985 let keymapsbuf always dc c =
6986 let bb = Buffer.create 16 in
6987 let rec loop = function
6988 | [] -> ()
6989 | (modename, h) :: rest ->
6990 let dh = findkeyhash dc modename in
6991 if always || h <> dh
6992 then (
6993 if Hashtbl.length h > 0
6994 then (
6995 if Buffer.length bb > 0
6996 then Buffer.add_char bb '\n';
6997 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6998 Hashtbl.iter (fun i o ->
6999 let isdifferent = always ||
7001 let dO = Hashtbl.find dh i in
7002 dO <> o
7003 with Not_found -> true
7005 if isdifferent
7006 then
7007 let addkm (k, m) =
7008 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7009 if Wsi.withalt m then Buffer.add_string bb "alt-";
7010 if Wsi.withshift m then Buffer.add_string bb "shift-";
7011 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7012 Buffer.add_string bb (Wsi.keyname k);
7014 let addkms l =
7015 let rec loop = function
7016 | [] -> ()
7017 | km :: [] -> addkm km
7018 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7020 loop l
7022 Buffer.add_string bb "<map in='";
7023 addkm i;
7024 match o with
7025 | KMinsrt km ->
7026 Buffer.add_string bb "' out='";
7027 addkm km;
7028 Buffer.add_string bb "'/>\n"
7030 | KMinsrl kms ->
7031 Buffer.add_string bb "' out='";
7032 addkms kms;
7033 Buffer.add_string bb "'/>\n"
7035 | KMmulti (ins, kms) ->
7036 Buffer.add_char bb ' ';
7037 addkms ins;
7038 Buffer.add_string bb "' out='";
7039 addkms kms;
7040 Buffer.add_string bb "'/>\n"
7041 ) h;
7042 Buffer.add_string bb "</keymap>";
7045 loop rest
7047 loop c.keyhashes;
7051 let save () =
7052 let uifontsize = fstate.fontsize in
7053 let bb = Buffer.create 32768 in
7054 let relx = float state.x /. float state.winw in
7055 let w, h, x =
7056 let cx w = truncate (relx *. float w) in
7057 List.fold_left
7058 (fun (w, h, x) ws ->
7059 match ws with
7060 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7061 | Wsi.MaxVert -> (w, conf.cwinh, x)
7062 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7064 (state.winw, state.winh, state.x) state.winstate
7066 conf.cwinw <- w;
7067 conf.cwinh <- h;
7068 let f (h, dc) =
7069 let dc = if conf.bedefault then conf else dc in
7070 Buffer.add_string bb "<llppconfig>\n";
7072 if String.length !fontpath > 0
7073 then
7074 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7075 uifontsize
7076 !fontpath
7077 else (
7078 if uifontsize <> 14
7079 then
7080 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7083 Buffer.add_string bb "<defaults ";
7084 add_attrs bb true dc dc;
7085 let kb = keymapsbuf true dc dc in
7086 if Buffer.length kb > 0
7087 then (
7088 Buffer.add_string bb ">\n";
7089 Buffer.add_buffer bb kb;
7090 Buffer.add_string bb "\n</defaults>\n";
7092 else Buffer.add_string bb "/>\n";
7094 let adddoc path pan anchor c bookmarks =
7095 if bookmarks == [] && c = dc && anchor = emptyanchor
7096 then ()
7097 else (
7098 Printf.bprintf bb "<doc path='%s'"
7099 (enent path 0 (String.length path));
7101 if anchor <> emptyanchor
7102 then (
7103 let n, rely, visy = anchor in
7104 Printf.bprintf bb " page='%d'" n;
7105 if rely > 1e-6
7106 then
7107 Printf.bprintf bb " rely='%f'" rely
7109 if abs_float visy > 1e-6
7110 then
7111 Printf.bprintf bb " visy='%f'" visy
7115 if pan != 0
7116 then Printf.bprintf bb " pan='%d'" pan;
7118 add_attrs bb false dc c;
7119 let kb = keymapsbuf false dc c in
7121 begin match bookmarks with
7122 | [] ->
7123 if Buffer.length kb > 0
7124 then (
7125 Buffer.add_string bb ">\n";
7126 Buffer.add_buffer bb kb;
7127 Buffer.add_string bb "\n</doc>\n";
7129 else Buffer.add_string bb "/>\n"
7130 | _ ->
7131 Buffer.add_string bb ">\n<bookmarks>\n";
7132 List.iter (fun (title, _level, (page, rely, visy)) ->
7133 Printf.bprintf bb
7134 "<item title='%s' page='%d'"
7135 (enent title 0 (String.length title))
7136 page
7138 if rely > 1e-6
7139 then
7140 Printf.bprintf bb " rely='%f'" rely
7142 if abs_float visy > 1e-6
7143 then
7144 Printf.bprintf bb " visy='%f'" visy
7146 Buffer.add_string bb "/>\n";
7147 ) bookmarks;
7148 Buffer.add_string bb "</bookmarks>";
7149 if Buffer.length kb > 0
7150 then (
7151 Buffer.add_string bb "\n";
7152 Buffer.add_buffer bb kb;
7154 Buffer.add_string bb "\n</doc>\n";
7155 end;
7159 let pan, conf =
7160 match state.mode with
7161 | Birdseye (c, pan, _, _, _) ->
7162 let beyecolumns =
7163 match conf.columns with
7164 | Cmulti ((c, _, _), _) -> Some c
7165 | Csingle _ -> None
7166 | Csplit _ -> None
7167 and columns =
7168 match c.columns with
7169 | Cmulti (c, _) -> Cmulti (c, [||])
7170 | Csingle _ -> Csingle [||]
7171 | Csplit _ -> failwith "quit from bird's eye while split"
7173 pan, { c with beyecolumns = beyecolumns; columns = columns }
7174 | _ -> x, conf
7176 let basename = Filename.basename
7177 (if String.length state.origin = 0 then state.path else state.origin)
7179 adddoc basename pan (getanchor ())
7180 (let conf =
7181 let autoscrollstep =
7182 match state.autoscroll with
7183 | Some step -> step
7184 | None -> conf.autoscrollstep
7186 match state.mode with
7187 | Birdseye (bc, _, _, _, _) ->
7188 { conf with
7189 zoom = bc.zoom;
7190 presentation = bc.presentation;
7191 interpagespace = bc.interpagespace;
7192 maxwait = bc.maxwait;
7193 autoscrollstep = autoscrollstep }
7194 | _ -> { conf with autoscrollstep = autoscrollstep }
7195 in conf)
7196 (if conf.savebmarks then state.bookmarks else []);
7198 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7199 if basename <> path
7200 then adddoc path x anchor c bookmarks
7201 ) h;
7202 Buffer.add_string bb "</llppconfig>\n";
7203 true;
7205 if load1 f && Buffer.length bb > 0
7206 then
7208 let tmp = !confpath ^ ".tmp" in
7209 let oc = open_out_bin tmp in
7210 Buffer.output_buffer oc bb;
7211 close_out oc;
7212 Unix.rename tmp !confpath;
7213 with exn ->
7214 prerr_endline
7215 ("error while saving configuration: " ^ exntos exn)
7217 end;;
7219 let adderrmsg src msg =
7220 Buffer.add_string state.errmsgs msg;
7221 state.newerrmsgs <- true;
7222 G.postRedisplay src
7225 let adderrfmt src fmt =
7226 Format.kprintf (fun s -> adderrmsg src s) fmt;
7229 let ract cmds =
7230 let cl = splitatspace cmds in
7231 let scan s fmt f =
7232 try Scanf.sscanf s fmt f
7233 with exn ->
7234 adderrfmt "remote exec"
7235 "error processing '%S': %s\n" cmds (exntos exn)
7237 match cl with
7238 | "reload" :: [] -> reload ()
7239 | "goto" :: args :: [] ->
7240 scan args "%u %f %f"
7241 (fun pageno x y ->
7242 let cmd, _ = state.geomcmds in
7243 if String.length cmd = 0
7244 then gotopagexy pageno x y
7245 else
7246 let f prevf () =
7247 gotopagexy pageno x y;
7248 prevf ()
7250 state.reprf <- f state.reprf
7252 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7253 | "rect" :: args :: [] ->
7254 scan args "%u %u %f %f %f %f"
7255 (fun pageno color x0 y0 x1 y1 ->
7256 onpagerect pageno (fun w h ->
7257 let _,w1,h1,_ = getpagedim pageno in
7258 let sw = float w1 /. w
7259 and sh = float h1 /. h in
7260 let x0s = x0 *. sw
7261 and x1s = x1 *. sw
7262 and y0s = y0 *. sh
7263 and y1s = y1 *. sh in
7264 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7265 debugrect rect;
7266 state.rects <- (pageno, color, rect) :: state.rects;
7267 G.postRedisplay "rect";
7270 | "activatewin" :: [] -> Wsi.activatewin ()
7271 | "quit" :: [] -> raise Quit
7272 | _ ->
7273 adderrfmt "remote command"
7274 "error processing remote command: %S\n" cmds;
7277 let remote =
7278 let scratch = String.create 80 in
7279 let buf = Buffer.create 80 in
7280 fun fd ->
7281 let rec tempfr () =
7282 try Some (Unix.read fd scratch 0 80)
7283 with
7284 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7285 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7286 | exn -> raise exn
7288 match tempfr () with
7289 | None -> Some fd
7290 | Some n ->
7291 if n = 0
7292 then (
7293 Unix.close fd;
7294 if Buffer.length buf > 0
7295 then (
7296 let s = Buffer.contents buf in
7297 Buffer.clear buf;
7298 ract s;
7300 None
7302 else
7303 let rec eat ppos =
7304 let nlpos =
7306 let pos = String.index_from scratch ppos '\n' in
7307 if pos >= n then -1 else pos
7308 with Not_found -> -1
7310 if nlpos >= 0
7311 then (
7312 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7313 let s = Buffer.contents buf in
7314 Buffer.clear buf;
7315 ract s;
7316 eat (nlpos+1);
7318 else (
7319 Buffer.add_substring buf scratch ppos (n-ppos);
7320 Some fd
7322 in eat 0
7325 let remoteopen path =
7326 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7327 with exn ->
7328 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7329 None
7332 let () =
7333 let trimcachepath = ref "" in
7334 let rcmdpath = ref "" in
7335 Arg.parse
7336 (Arg.align
7337 [("-p", Arg.String (fun s -> state.password <- s),
7338 "<password> Set password");
7340 ("-f", Arg.String (fun s -> Config.fontpath := s),
7341 "<path> Set path to the user interface font");
7343 ("-c", Arg.String (fun s -> Config.confpath := s),
7344 "<path> Set path to the configuration file");
7346 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7347 "<path> Set path to the trim cache file");
7349 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7350 "<named-destination> Set named destination");
7352 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7354 ("-remote", Arg.String (fun s -> rcmdpath := s),
7355 "<path> Set path to the remote commands source");
7357 ("-origin", Arg.String (fun s -> state.origin <- s),
7358 "<original-path> Set original path");
7360 ("-v", Arg.Unit (fun () ->
7361 Printf.printf
7362 "%s\nconfiguration path: %s\n"
7363 (version ())
7364 Config.defconfpath
7366 exit 0), " Print version and exit");
7369 (fun s -> state.path <- s)
7370 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7372 if String.length state.path = 0
7373 then (prerr_endline "file name missing"; exit 1);
7375 if not (Config.load ())
7376 then prerr_endline "failed to load configuration";
7378 let wsfd, winw, winh = Wsi.init (object
7379 val mutable m_hack = false
7380 method expose = if not m_hack then G.postRedisplay "expose"
7381 method visible = G.postRedisplay "visible"
7382 method display = m_hack <- false; display ()
7383 method reshape w h =
7384 m_hack <- w < state.winw && h < state.winh;
7385 reshape w h
7386 method mouse b d x y m = mouse b d x y m
7387 method motion x y = state.mpos <- (x, y); motion x y
7388 method pmotion x y = state.mpos <- (x, y); pmotion x y
7389 method key k m =
7390 let mascm = m land (
7391 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7392 ) in
7393 match state.keystate with
7394 | KSnone ->
7395 let km = k, mascm in
7396 begin
7397 match
7398 let modehash = state.uioh#modehash in
7399 try Hashtbl.find modehash km
7400 with Not_found ->
7401 try Hashtbl.find (findkeyhash conf "global") km
7402 with Not_found -> KMinsrt (k, m)
7403 with
7404 | KMinsrt (k, m) -> keyboard k m
7405 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7406 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7408 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7409 List.iter (fun (k, m) -> keyboard k m) insrt;
7410 state.keystate <- KSnone
7411 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7412 state.keystate <- KSinto (keys, insrt)
7413 | _ ->
7414 state.keystate <- KSnone
7416 method enter x y = state.mpos <- (x, y); pmotion x y
7417 method leave = state.mpos <- (-1, -1)
7418 method winstate wsl = state.winstate <- wsl
7419 method quit = raise Quit
7420 end) conf.cwinw conf.cwinh (platform = Posx) in
7422 state.wsfd <- wsfd;
7424 if not (
7425 List.exists GlMisc.check_extension
7426 [ "GL_ARB_texture_rectangle"
7427 ; "GL_EXT_texture_recangle"
7428 ; "GL_NV_texture_rectangle" ]
7430 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7432 if (
7433 let r = GlMisc.get_string `renderer in
7434 let p = "Mesa DRI Intel(" in
7435 let l = String.length p in
7436 String.length r > l && String.sub r 0 l = p
7438 then defconf.sliceheight <- 1024;
7440 let cr, sw =
7441 match Ne.pipe () with
7442 | Ne.Exn exn ->
7443 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7444 exit 1
7445 | Ne.Res rw -> rw
7446 and sr, cw =
7447 match Ne.pipe () with
7448 | Ne.Exn exn ->
7449 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7450 exit 1
7451 | Ne.Res rw -> rw
7454 cloexec cr;
7455 cloexec sw;
7456 cloexec sr;
7457 cloexec cw;
7459 setcheckers conf.checkers;
7460 redirectstderr ();
7462 init (cr, cw) (
7463 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7464 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7465 !Config.fontpath, !trimcachepath,
7466 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7468 state.sr <- sr;
7469 state.sw <- sw;
7470 state.text <- "Opening " ^ (mbtoutf8 state.path);
7471 reshape winw winh;
7472 opendoc state.path state.password;
7473 state.uioh <- uioh;
7474 display ();
7475 Wsi.mapwin ();
7476 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7477 let optrfd =
7478 ref (
7479 if String.length !rcmdpath > 0
7480 then remoteopen !rcmdpath
7481 else None
7485 let rec loop deadline =
7486 let r =
7487 match state.errfd with
7488 | None -> [state.sr; state.wsfd]
7489 | Some fd -> [state.sr; state.wsfd; fd]
7491 let r =
7492 match !optrfd with
7493 | None -> r
7494 | Some fd -> fd :: r
7496 if state.redisplay
7497 then (
7498 state.redisplay <- false;
7499 display ();
7501 let timeout =
7502 let now = now () in
7503 if deadline > now
7504 then (
7505 if deadline = infinity
7506 then ~-.1.0
7507 else max 0.0 (deadline -. now)
7509 else 0.0
7511 let r, _, _ =
7512 try Unix.select r [] [] timeout
7513 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7515 begin match r with
7516 | [] ->
7517 state.ghyll None;
7518 let newdeadline =
7519 if state.ghyll == noghyll
7520 then
7521 match state.autoscroll with
7522 | Some step when step != 0 ->
7523 let y = state.y + step in
7524 let y =
7525 if y < 0
7526 then state.maxy
7527 else if y >= state.maxy then 0 else y
7529 gotoy y;
7530 if state.mode = View
7531 then state.text <- "";
7532 deadline +. 0.01
7533 | _ -> infinity
7534 else deadline +. 0.01
7536 loop newdeadline
7538 | l ->
7539 let rec checkfds = function
7540 | [] -> ()
7541 | fd :: rest when fd = state.sr ->
7542 let cmd = readcmd state.sr in
7543 act cmd;
7544 checkfds rest
7546 | fd :: rest when fd = state.wsfd ->
7547 Wsi.readresp fd;
7548 checkfds rest
7550 | fd :: rest when Some fd = !optrfd ->
7551 begin match remote fd with
7552 | None -> optrfd := remoteopen !rcmdpath;
7553 | opt -> optrfd := opt
7554 end;
7555 checkfds rest
7557 | fd :: rest ->
7558 let s = String.create 80 in
7559 let n = tempfailureretry (Unix.read fd s 0) 80 in
7560 if conf.redirectstderr
7561 then (
7562 Buffer.add_substring state.errmsgs s 0 n;
7563 state.newerrmsgs <- true;
7564 state.redisplay <- true;
7566 else (
7567 prerr_string (String.sub s 0 n);
7568 flush stderr;
7570 checkfds rest
7572 checkfds l;
7573 let newdeadline =
7574 let deadline1 =
7575 if deadline = infinity
7576 then now () +. 0.01
7577 else deadline
7579 match state.autoscroll with
7580 | Some step when step != 0 -> deadline1
7581 | _ -> if state.ghyll == noghyll then infinity else deadline1
7583 loop newdeadline
7584 end;
7587 loop infinity;
7588 with Quit ->
7589 Config.save ();