9caec02b9e6bc7a8d27aba9cb935a69867651e95
[llpp.git] / main.ml
blob9caec02b9e6bc7a8d27aba9cb935a69867651e95
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 selfexec = ref "";;
134 let popen cmd fda =
135 if platform = Pcygwin
136 then (
137 let sh = "/bin/sh" in
138 let args = [|sh; "-c"; cmd|] in
139 let rec std si so se = function
140 | [] -> si, so, se
141 | (fd, 0) :: rest -> std fd so se rest
142 | (fd, -1) :: rest ->
143 Unix.set_close_on_exec fd;
144 std si so se rest
145 | (_, n) :: _ ->
146 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
148 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
149 ignore (Unix.create_process sh args si so se)
151 else popen cmd fda;
154 type mpos = int * int
155 and mstate =
156 | Msel of (mpos * mpos)
157 | Mpan of mpos
158 | Mscrolly | Mscrollx
159 | Mzoom of (int * int)
160 | Mzoomrect of (mpos * mpos)
161 | Mnone
164 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
165 and onkey = string -> int -> te
166 and ondone = string -> unit
167 and histcancel = unit -> unit
168 and onhist = ((histcmd -> string) * histcancel)
169 and histcmd = HCnext | HCprev | HCfirst | HClast
170 and cancelonempty = bool
171 and te =
172 | TEstop
173 | TEdone of string
174 | TEcont of string
175 | TEswitch of textentry
178 type 'a circbuf =
179 { store : 'a array
180 ; mutable rc : int
181 ; mutable wc : int
182 ; mutable len : int
186 let bound v minv maxv =
187 max minv (min maxv v);
190 let cbnew n v =
191 { store = Array.create n v
192 ; rc = 0
193 ; wc = 0
194 ; len = 0
198 let cbcap b = Array.length b.store;;
200 let cbput b v =
201 let cap = cbcap b in
202 b.store.(b.wc) <- v;
203 b.wc <- (b.wc + 1) mod cap;
204 b.rc <- b.wc;
205 b.len <- min (b.len + 1) cap;
208 let cbempty b = b.len = 0;;
210 let cbgetg b circular dir =
211 if cbempty b
212 then b.store.(0)
213 else
214 let rc = b.rc + dir in
215 let rc =
216 if circular
217 then (
218 if rc = -1
219 then b.len-1
220 else (
221 if rc >= b.len
222 then 0
223 else rc
226 else bound rc 0 (b.len-1)
228 b.rc <- rc;
229 b.store.(rc);
232 let cbget b = cbgetg b false;;
233 let cbgetc b = cbgetg b true;;
235 let drawstring size x y s =
236 Gl.enable `blend;
237 Gl.enable `texture_2d;
238 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
239 ignore (drawstr size x y s);
240 Gl.disable `blend;
241 Gl.disable `texture_2d;
244 let drawstring1 size x y s =
245 drawstr size x y s;
248 let drawstring2 size x y fmt =
249 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
252 type page =
253 { pageno : int
254 ; pagedimno : int
255 ; pagew : int
256 ; pageh : int
257 ; pagex : int
258 ; pagey : int
259 ; pagevw : int
260 ; pagevh : int
261 ; pagedispx : int
262 ; pagedispy : int
263 ; pagecol : int
267 let debugl l =
268 dolog "l %d dim=%d {" l.pageno l.pagedimno;
269 dolog " WxH %dx%d" l.pagew l.pageh;
270 dolog " vWxH %dx%d" l.pagevw l.pagevh;
271 dolog " pagex,y %d,%d" l.pagex l.pagey;
272 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
273 dolog " column %d" l.pagecol;
274 dolog "}";
277 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
278 dolog "rect {";
279 dolog " x0,y0=(% f, % f)" x0 y0;
280 dolog " x1,y1=(% f, % f)" x1 y1;
281 dolog " x2,y2=(% f, % f)" x2 y2;
282 dolog " x3,y3=(% f, % f)" x3 y3;
283 dolog "}";
286 type multicolumns = multicol * pagegeom
287 and singlecolumn = pagegeom
288 and splitcolumns = columncount * pagegeom
289 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
290 and multicol = columncount * covercount * covercount
291 and pdimno = int
292 and columncount = int
293 and covercount = int;;
295 type scrollb = int;;
296 let scrollbvv = 1;;
297 let scrollbhv = 2;;
299 type conf =
300 { mutable scrollbw : int
301 ; mutable scrollh : int
302 ; mutable scrollb : scrollb
303 ; mutable icase : bool
304 ; mutable preload : bool
305 ; mutable pagebias : int
306 ; mutable verbose : bool
307 ; mutable debug : bool
308 ; mutable scrollstep : int
309 ; mutable hscrollstep : int
310 ; mutable maxhfit : bool
311 ; mutable crophack : bool
312 ; mutable autoscrollstep : int
313 ; mutable maxwait : float option
314 ; mutable hlinks : bool
315 ; mutable underinfo : bool
316 ; mutable interpagespace : interpagespace
317 ; mutable zoom : float
318 ; mutable presentation : bool
319 ; mutable angle : angle
320 ; mutable cwinw : int
321 ; mutable cwinh : int
322 ; mutable savebmarks : bool
323 ; mutable fitmodel : fitmodel
324 ; mutable trimmargins : trimmargins
325 ; mutable trimfuzz : irect
326 ; mutable memlimit : memsize
327 ; mutable texcount : texcount
328 ; mutable sliceheight : sliceheight
329 ; mutable thumbw : width
330 ; mutable jumpback : bool
331 ; mutable bgcolor : (float * float * float)
332 ; mutable bedefault : bool
333 ; mutable tilew : int
334 ; mutable tileh : int
335 ; mutable mustoresize : memsize
336 ; mutable checkers : bool
337 ; mutable aalevel : int
338 ; mutable urilauncher : string
339 ; mutable pathlauncher : string
340 ; mutable colorspace : colorspace
341 ; mutable invert : bool
342 ; mutable colorscale : float
343 ; mutable redirectstderr : bool
344 ; mutable ghyllscroll : (int * int * int) option
345 ; mutable columns : columns
346 ; mutable beyecolumns : columncount option
347 ; mutable selcmd : string
348 ; mutable updatecurs : bool
349 ; mutable keyhashes : (string * keyhash) list
350 ; mutable hfsize : int
351 ; mutable pgscale : float
352 ; mutable usepbo : bool
353 ; mutable wheelbypage : bool
354 ; mutable stcmd : string
355 ; mutable riani : bool
357 and columns =
358 | Csingle of singlecolumn
359 | Cmulti of multicolumns
360 | Csplit of splitcolumns
363 type anchor = pageno * top * dtop;;
365 type outline = string * int * anchor;;
367 type rect = float * float * float * float * float * float * float * float;;
369 type tile = opaque * pixmapsize * elapsed
370 and elapsed = float;;
371 type pagemapkey = pageno * gen;;
372 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
373 and row = int
374 and col = int;;
376 let emptyanchor = (0, 0.0, 0.0);;
378 type infochange = | Memused | Docinfo | Pdim;;
380 class type uioh = object
381 method display : unit
382 method key : int -> int -> uioh
383 method button : int -> bool -> int -> int -> int -> uioh
384 method motion : int -> int -> uioh
385 method pmotion : int -> int -> uioh
386 method infochanged : infochange -> unit
387 method scrollpw : (int * float * float)
388 method scrollph : (int * float * float)
389 method modehash : keyhash
390 method eformsgs : bool
391 end;;
393 type mode =
394 | Birdseye of (conf * leftx * pageno * pageno * anchor)
395 | Textentry of (textentry * onleave)
396 | View
397 | LinkNav of linktarget
398 and onleave = leavetextentrystatus -> unit
399 and leavetextentrystatus = | Cancel | Confirm
400 and helpitem = string * int * action
401 and action =
402 | Noaction
403 | Action of (uioh -> uioh)
404 and linktarget =
405 | Ltexact of (pageno * int)
406 | Ltgendir of int
409 let isbirdseye = function Birdseye _ -> true | _ -> false;;
410 let istextentry = function Textentry _ -> true | _ -> false;;
412 type currently =
413 | Idle
414 | Loading of (page * gen)
415 | Tiling of (
416 page * opaque * colorspace * angle * gen * col * row * width * height
418 | Outlining of outline list
421 let emptykeyhash = Hashtbl.create 0;;
422 let nouioh : uioh = object (self)
423 method display = ()
424 method key _ _ = self
425 method button _ _ _ _ _ = self
426 method motion _ _ = self
427 method pmotion _ _ = self
428 method infochanged _ = ()
429 method scrollpw = (0, nan, nan)
430 method scrollph = (0, nan, nan)
431 method modehash = emptykeyhash
432 method eformsgs = false
433 end;;
435 type state =
436 { mutable sr : Unix.file_descr
437 ; mutable sw : Unix.file_descr
438 ; mutable wsfd : Unix.file_descr
439 ; mutable errfd : Unix.file_descr option
440 ; mutable stderr : Unix.file_descr
441 ; mutable errmsgs : Buffer.t
442 ; mutable newerrmsgs : bool
443 ; mutable w : int
444 ; mutable x : int
445 ; mutable y : int
446 ; mutable anchor : anchor
447 ; mutable ranchors : (string * string * anchor * string) list
448 ; mutable maxy : int
449 ; mutable layout : page list
450 ; pagemap : (pagemapkey, opaque) Hashtbl.t
451 ; tilemap : (tilemapkey, tile) Hashtbl.t
452 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
453 ; mutable pdims : (pageno * width * height * leftx) list
454 ; mutable pagecount : int
455 ; mutable currently : currently
456 ; mutable mstate : mstate
457 ; mutable searchpattern : string
458 ; mutable rects : (pageno * recttype * rect) list
459 ; mutable rects1 : (pageno * recttype * rect) list
460 ; mutable text : string
461 ; mutable winstate : Wsi.winstate list
462 ; mutable mode : mode
463 ; mutable uioh : uioh
464 ; mutable outlines : outline array
465 ; mutable bookmarks : outline list
466 ; mutable path : string
467 ; mutable password : string
468 ; mutable nameddest : string
469 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
470 ; mutable memused : memsize
471 ; mutable gen : gen
472 ; mutable throttle : (page list * int * float) option
473 ; mutable autoscroll : int option
474 ; mutable ghyll : (int option -> unit)
475 ; mutable help : helpitem array
476 ; mutable docinfo : (int * string) list
477 ; mutable texid : GlTex.texture_id option
478 ; hists : hists
479 ; mutable prevzoom : float
480 ; mutable progress : float
481 ; mutable redisplay : bool
482 ; mutable mpos : mpos
483 ; mutable keystate : keystate
484 ; mutable glinks : bool
485 ; mutable prevcolumns : (columns * float) option
486 ; mutable winw : int
487 ; mutable winh : int
488 ; mutable reprf : (unit -> unit)
489 ; mutable origin : string
491 and hists =
492 { pat : string circbuf
493 ; pag : string circbuf
494 ; nav : anchor circbuf
495 ; sel : string circbuf
499 let defconf =
500 { scrollbw = 7
501 ; scrollh = 12
502 ; scrollb = scrollbhv lor scrollbvv
503 ; icase = true
504 ; preload = true
505 ; pagebias = 0
506 ; verbose = false
507 ; debug = false
508 ; scrollstep = 24
509 ; hscrollstep = 24
510 ; maxhfit = true
511 ; crophack = false
512 ; autoscrollstep = 2
513 ; maxwait = None
514 ; hlinks = false
515 ; underinfo = false
516 ; interpagespace = 2
517 ; zoom = 1.0
518 ; presentation = false
519 ; angle = 0
520 ; cwinw = 900
521 ; cwinh = 900
522 ; savebmarks = true
523 ; fitmodel = FitProportional
524 ; trimmargins = false
525 ; trimfuzz = (0,0,0,0)
526 ; memlimit = 32 lsl 20
527 ; texcount = 256
528 ; sliceheight = 24
529 ; thumbw = 76
530 ; jumpback = true
531 ; bgcolor = (0.5, 0.5, 0.5)
532 ; bedefault = false
533 ; tilew = 2048
534 ; tileh = 2048
535 ; mustoresize = 256 lsl 20
536 ; checkers = true
537 ; aalevel = 8
538 ; urilauncher =
539 (match platform with
540 | Plinux | Pfreebsd | Pdragonflybsd
541 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
542 | Posx -> "open \"%s\""
543 | Pcygwin -> "cygstart \"%s\""
544 | Punknown -> "echo %s")
545 ; pathlauncher = "lp \"%s\""
546 ; selcmd =
547 (match platform with
548 | Plinux | Pfreebsd | Pdragonflybsd
549 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
550 | Posx -> "pbcopy"
551 | Pcygwin -> "wsel"
552 | Punknown -> "cat")
553 ; colorspace = Rgb
554 ; invert = false
555 ; colorscale = 1.0
556 ; redirectstderr = false
557 ; ghyllscroll = None
558 ; columns = Csingle [||]
559 ; beyecolumns = None
560 ; updatecurs = false
561 ; hfsize = 12
562 ; pgscale = 1.0
563 ; usepbo = false
564 ; wheelbypage = false
565 ; stcmd = "echo SyncTex"
566 ; riani = false
567 ; keyhashes =
568 let mk n = (n, Hashtbl.create 1) in
569 [ mk "global"
570 ; mk "info"
571 ; mk "help"
572 ; mk "outline"
573 ; mk "listview"
574 ; mk "birdseye"
575 ; mk "textentry"
576 ; mk "links"
577 ; mk "view"
582 let wtmode = ref false;;
584 let findkeyhash c name =
585 try List.assoc name c.keyhashes
586 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
589 let conf = { defconf with angle = defconf.angle };;
591 let pgscale h = truncate (float h *. conf.pgscale);;
593 type fontstate =
594 { mutable fontsize : int
595 ; mutable wwidth : float
596 ; mutable maxrows : int
600 let fstate =
601 { fontsize = 14
602 ; wwidth = nan
603 ; maxrows = -1
607 let geturl s =
608 let colonpos = try String.index s ':' with Not_found -> -1 in
609 let len = String.length s in
610 if colonpos >= 0 && colonpos + 3 < len
611 then (
612 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
613 then
614 let schemestartpos =
615 try String.rindex_from s colonpos ' '
616 with Not_found -> -1
618 let scheme =
619 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
621 match scheme with
622 | "http" | "ftp" | "mailto" ->
623 let epos =
624 try String.index_from s colonpos ' '
625 with Not_found -> len
627 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
628 | _ -> ""
629 else ""
631 else ""
634 let gotouri uri =
635 if String.length conf.urilauncher = 0
636 then print_endline uri
637 else (
638 let url = geturl uri in
639 if String.length url = 0
640 then Printf.eprintf "obtained empty url from uri %S" uri
641 else
642 let re = Str.regexp "%s" in
643 let command = Str.global_replace re url conf.urilauncher in
644 try popen command []
645 with exn ->
646 Printf.eprintf
647 "failed to execute `%s': %s\n" command (exntos exn);
648 flush stderr;
652 let version () =
653 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
654 (platform_to_string platform) Sys.word_size Sys.ocaml_version
657 let makehelp () =
658 let strings = version () :: "" :: Help.keys in
659 Array.of_list (
660 List.map (fun s ->
661 let url = geturl s in
662 if String.length url > 0
663 then (s, 0, Action (fun u -> gotouri url; u))
664 else (s, 0, Noaction)
665 ) strings);
668 let noghyll _ = ();;
669 let firstgeomcmds = "", [];;
670 let noreprf () = ();;
672 let state =
673 { sr = Unix.stdin
674 ; sw = Unix.stdin
675 ; wsfd = Unix.stdin
676 ; errfd = None
677 ; stderr = Unix.stderr
678 ; errmsgs = Buffer.create 0
679 ; newerrmsgs = false
680 ; x = 0
681 ; y = 0
682 ; w = 0
683 ; anchor = emptyanchor
684 ; ranchors = []
685 ; layout = []
686 ; maxy = max_int
687 ; tilelru = Queue.create ()
688 ; pagemap = Hashtbl.create 10
689 ; tilemap = Hashtbl.create 10
690 ; pdims = []
691 ; pagecount = 0
692 ; currently = Idle
693 ; mstate = Mnone
694 ; rects = []
695 ; rects1 = []
696 ; text = ""
697 ; mode = View
698 ; winstate = []
699 ; searchpattern = ""
700 ; outlines = [||]
701 ; bookmarks = []
702 ; path = ""
703 ; password = ""
704 ; nameddest = ""
705 ; geomcmds = firstgeomcmds
706 ; hists =
707 { nav = cbnew 10 emptyanchor
708 ; pat = cbnew 10 ""
709 ; pag = cbnew 10 ""
710 ; sel = cbnew 10 ""
712 ; memused = 0
713 ; gen = 0
714 ; throttle = None
715 ; autoscroll = None
716 ; ghyll = noghyll
717 ; help = makehelp ()
718 ; docinfo = []
719 ; texid = None
720 ; prevzoom = 1.0
721 ; progress = -1.0
722 ; uioh = nouioh
723 ; redisplay = true
724 ; mpos = (-1, -1)
725 ; keystate = KSnone
726 ; glinks = false
727 ; prevcolumns = None
728 ; winw = -1
729 ; winh = -1
730 ; reprf = noreprf
731 ; origin = ""
735 let hscrollh () =
736 if (conf.scrollb land scrollbhv = 0)
737 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
738 then 0
739 else conf.scrollbw
742 let vscrollw () =
743 if (conf.scrollb land scrollbvv = 0)
744 then 0
745 else conf.scrollbw
748 let wadjsb w = w - vscrollw ();;
750 let setfontsize n =
751 fstate.fontsize <- n;
752 fstate.wwidth <- measurestr fstate.fontsize "w";
753 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
756 let vlog fmt =
757 if conf.verbose
758 then
759 Printf.kprintf prerr_endline fmt
760 else
761 Printf.kprintf ignore fmt
764 let launchpath () =
765 if String.length conf.pathlauncher = 0
766 then print_endline state.path
767 else (
768 let re = Str.regexp "%s" in
769 let command = Str.global_replace re state.path conf.pathlauncher in
770 try popen command []
771 with exn ->
772 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
773 flush stderr;
777 module Ne = struct
778 type 'a t = | Res of 'a | Exn of exn;;
780 let pipe () =
781 try Res (Unix.pipe ())
782 with exn -> Exn exn
785 let clo fd f =
786 try tempfailureretry Unix.close fd
787 with exn -> f (exntos exn)
790 let dup fd =
791 try Res (tempfailureretry Unix.dup fd)
792 with exn -> Exn exn
795 let dup2 fd1 fd2 =
796 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
797 with exn -> Exn exn
799 end;;
801 let redirectstderr () =
802 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
803 if conf.redirectstderr
804 then
805 match Ne.pipe () with
806 | Ne.Exn exn ->
807 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
809 | Ne.Res (r, w) ->
810 begin match Ne.dup Unix.stderr with
811 | Ne.Exn exn ->
812 dolog "failed to dup stderr: %s" (exntos exn);
813 Ne.clo r (clofail "pipe/r");
814 Ne.clo w (clofail "pipe/w");
816 | Ne.Res dupstderr ->
817 begin match Ne.dup2 w Unix.stderr with
818 | Ne.Exn exn ->
819 dolog "failed to dup2 to stderr: %s" (exntos exn);
820 Ne.clo dupstderr (clofail "stderr duplicate");
821 Ne.clo r (clofail "redir pipe/r");
822 Ne.clo w (clofail "redir pipe/w");
824 | Ne.Res () ->
825 state.stderr <- dupstderr;
826 state.errfd <- Some r;
827 end;
829 else (
830 state.newerrmsgs <- false;
831 begin match state.errfd with
832 | Some fd ->
833 begin match Ne.dup2 state.stderr Unix.stderr with
834 | Ne.Exn exn ->
835 dolog "failed to dup2 original stderr: %s" (exntos exn)
836 | Ne.Res () ->
837 Ne.clo fd (clofail "dup of stderr");
838 state.errfd <- None;
839 end;
840 | None -> ()
841 end;
842 prerr_string (Buffer.contents state.errmsgs);
843 flush stderr;
844 Buffer.clear state.errmsgs;
848 module G =
849 struct
850 let postRedisplay who =
851 if conf.verbose
852 then prerr_endline ("redisplay for " ^ who);
853 state.redisplay <- true;
855 end;;
857 let getopaque pageno =
858 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
859 with Not_found -> None
862 let putopaque pageno opaque =
863 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
866 let pagetranslatepoint l x y =
867 let dy = y - l.pagedispy in
868 let y = dy + l.pagey in
869 let dx = x - l.pagedispx in
870 let x = dx + l.pagex in
871 (x, y);
874 let onppundermouse g x y d =
875 let rec f = function
876 | l :: rest ->
877 begin match getopaque l.pageno with
878 | Some opaque ->
879 let x0 = l.pagedispx in
880 let x1 = x0 + l.pagevw in
881 let y0 = l.pagedispy in
882 let y1 = y0 + l.pagevh in
883 if y >= y0 && y <= y1 && x >= x0 && x <= x1
884 then
885 let px, py = pagetranslatepoint l x y in
886 match g opaque l px py with
887 | Some res -> res
888 | None -> f rest
889 else f rest
890 | _ ->
891 f rest
893 | [] -> d
895 f state.layout
898 let getunder x y =
899 let g opaque _ px py =
900 match whatsunder opaque px py with
901 | Unone -> None
902 | under -> Some under
904 onppundermouse g x y Unone
907 let unproject x y =
908 let g opaque l x y =
909 match unproject opaque x y with
910 | Some (x, y) -> Some (Some (l.pageno, x, y))
911 | None -> None
913 onppundermouse g x y None;
916 let showtext c s =
917 state.text <- Printf.sprintf "%c%s" c s;
918 G.postRedisplay "showtext";
921 let selstring s =
922 match Ne.pipe () with
923 | Ne.Exn exn ->
924 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
925 | Ne.Res (r, w) ->
926 let popened =
927 try popen conf.selcmd [r, 0; w, -1]; true
928 with exn ->
929 showtext '!'
930 (Printf.sprintf "failed to execute %s: %s"
931 conf.selcmd (exntos exn));
932 false
934 let clo cap fd =
935 Ne.clo fd (fun msg ->
936 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
939 if popened
940 then
941 (try
942 let l = String.length s in
943 let n = tempfailureretry (Unix.write w s 0) l in
944 if n != l
945 then
946 showtext '!'
947 (Printf.sprintf
948 "failed to write %d characters to sel pipe, wrote %d"
951 with exn ->
952 showtext '!'
953 (Printf.sprintf "failed to write to sel pipe: %s"
954 (exntos exn)
957 else dolog "%s" s;
958 clo "pipe/r" r;
959 clo "pipe/w" w;
962 let undertext = function
963 | Unone -> "none"
964 | Ulinkuri s -> s
965 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
966 | Utext s -> "font: " ^ s
967 | Uunexpected s -> "unexpected: " ^ s
968 | Ulaunch s -> "launch: " ^ s
969 | Unamed s -> "named: " ^ s
970 | Uremote (filename, pageno) ->
971 Printf.sprintf "%s: page %d" filename (pageno+1)
974 let updateunder x y =
975 match getunder x y with
976 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
977 | Ulinkuri uri ->
978 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
979 Wsi.setcursor Wsi.CURSOR_INFO
980 | Ulinkgoto (pageno, _) ->
981 if conf.underinfo
982 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
983 Wsi.setcursor Wsi.CURSOR_INFO
984 | Utext s ->
985 if conf.underinfo then showtext 'f' ("ont: " ^ s);
986 Wsi.setcursor Wsi.CURSOR_TEXT
987 | Uunexpected s ->
988 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
989 Wsi.setcursor Wsi.CURSOR_INHERIT
990 | Ulaunch s ->
991 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
992 Wsi.setcursor Wsi.CURSOR_INHERIT
993 | Unamed s ->
994 if conf.underinfo then showtext 'n' ("amed: " ^ s);
995 Wsi.setcursor Wsi.CURSOR_INHERIT
996 | Uremote (filename, pageno) ->
997 if conf.underinfo then showtext 'r'
998 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
999 Wsi.setcursor Wsi.CURSOR_INFO
1002 let showlinktype under =
1003 if conf.underinfo
1004 then
1005 match under with
1006 | Unone -> ()
1007 | under ->
1008 let s = undertext under in
1009 showtext ' ' s
1012 let addchar s c =
1013 let b = Buffer.create (String.length s + 1) in
1014 Buffer.add_string b s;
1015 Buffer.add_char b c;
1016 Buffer.contents b;
1019 let colorspace_of_string s =
1020 match String.lowercase s with
1021 | "rgb" -> Rgb
1022 | "bgr" -> Bgr
1023 | "gray" -> Gray
1024 | _ -> failwith "invalid colorspace"
1027 let int_of_colorspace = function
1028 | Rgb -> 0
1029 | Bgr -> 1
1030 | Gray -> 2
1033 let colorspace_of_int = function
1034 | 0 -> Rgb
1035 | 1 -> Bgr
1036 | 2 -> Gray
1037 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
1040 let colorspace_to_string = function
1041 | Rgb -> "rgb"
1042 | Bgr -> "bgr"
1043 | Gray -> "gray"
1046 let fitmodel_of_string s =
1047 match String.lowercase s with
1048 | "width" -> FitWidth
1049 | "proportional" -> FitProportional
1050 | "page" -> FitPage
1051 | _ -> failwith "invalid fit model"
1054 let int_of_fitmodel = function
1055 | FitWidth -> 0
1056 | FitProportional -> 1
1057 | FitPage -> 2
1060 let fitmodel_of_int = function
1061 | 0 -> FitWidth
1062 | 1 -> FitProportional
1063 | 2 -> FitPage
1064 | n -> failwith ("invalid fit model index " ^ string_of_int n)
1067 let fitmodel_to_string = function
1068 | FitWidth -> "width"
1069 | FitProportional -> "proportional"
1070 | FitPage -> "page"
1073 let intentry_with_suffix text key =
1074 let c =
1075 if key >= 32 && key < 127
1076 then Char.chr key
1077 else '\000'
1079 match Char.lowercase c with
1080 | '0' .. '9' ->
1081 let text = addchar text c in
1082 TEcont text
1084 | 'k' | 'm' | 'g' ->
1085 let text = addchar text c in
1086 TEcont text
1088 | _ ->
1089 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1090 TEcont text
1093 let multicolumns_to_string (n, a, b) =
1094 if a = 0 && b = 0
1095 then Printf.sprintf "%d" n
1096 else Printf.sprintf "%d,%d,%d" n a b;
1099 let multicolumns_of_string s =
1101 (int_of_string s, 0, 0)
1102 with _ ->
1103 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1104 if a > 1 || b > 1
1105 then failwith "subtly broken"; (n, a, b)
1109 let readcmd fd =
1110 let s = "xxxx" in
1111 let n = tempfailureretry (Unix.read fd s 0) 4 in
1112 if n != 4 then failwith "incomplete read(len)";
1113 let len = 0
1114 lor (Char.code s.[0] lsl 24)
1115 lor (Char.code s.[1] lsl 16)
1116 lor (Char.code s.[2] lsl 8)
1117 lor (Char.code s.[3] lsl 0)
1119 let s = String.create len in
1120 let n = tempfailureretry (Unix.read fd s 0) len in
1121 if n != len then failwith "incomplete read(data)";
1125 let btod b = if b then 1 else 0;;
1127 let wcmd fmt =
1128 let b = Buffer.create 16 in
1129 Buffer.add_string b "llll";
1130 Printf.kbprintf
1131 (fun b ->
1132 let s = Buffer.contents b in
1133 let n = String.length s in
1134 let len = n - 4 in
1135 (* dolog "wcmd %S" (String.sub s 4 len); *)
1136 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1137 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1138 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1139 s.[3] <- Char.chr (len land 0xff);
1140 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1141 if n' != n then failwith "write failed";
1142 ) b fmt;
1145 let calcips h =
1146 let d = state.winh - h in
1147 max conf.interpagespace ((d + 1) / 2)
1150 let rowyh (c, coverA, coverB) b n =
1151 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1152 then
1153 let _, _, vy, (_, _, h, _) = b.(n) in
1154 (vy, h)
1155 else
1156 let n' = n - coverA in
1157 let d = n' mod c in
1158 let s = n - d in
1159 let e = min state.pagecount (s + c) in
1160 let rec find m miny maxh = if m = e then miny, maxh else
1161 let _, _, y, (_, _, h, _) = b.(m) in
1162 let miny = min miny y in
1163 let maxh = max maxh h in
1164 find (m+1) miny maxh
1165 in find s max_int 0
1168 let calcheight () =
1169 match conf.columns with
1170 | Cmulti ((_, _, _) as cl, b) ->
1171 if Array.length b > 0
1172 then
1173 let y, h = rowyh cl b (Array.length b - 1) in
1174 y + h + (if conf.presentation then calcips h else 0)
1175 else 0
1176 | Csingle b ->
1177 if Array.length b > 0
1178 then
1179 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1180 y + h + (if conf.presentation then calcips h else 0)
1181 else 0
1182 | Csplit (_, b) ->
1183 if Array.length b > 0
1184 then
1185 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1186 y + h
1187 else 0
1190 let getpageyh pageno =
1191 let pageno = bound pageno 0 (state.pagecount-1) in
1192 match conf.columns with
1193 | Csingle b ->
1194 if Array.length b = 0
1195 then 0, 0
1196 else
1197 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1198 let y =
1199 if conf.presentation
1200 then y - calcips h
1201 else y
1203 y, h
1204 | Cmulti (cl, b) ->
1205 if Array.length b = 0
1206 then 0, 0
1207 else
1208 let y, h = rowyh cl b pageno in
1209 let y =
1210 if conf.presentation
1211 then y - calcips h
1212 else y
1214 y, h
1215 | Csplit (c, b) ->
1216 if Array.length b = 0
1217 then 0, 0
1218 else
1219 let n = pageno*c in
1220 let (_, _, y, (_, _, h, _)) = b.(n) in
1221 y, h
1224 let getpagedim pageno =
1225 let rec f ppdim l =
1226 match l with
1227 | (n, _, _, _) as pdim :: rest ->
1228 if n >= pageno
1229 then (if n = pageno then pdim else ppdim)
1230 else f pdim rest
1232 | [] -> ppdim
1234 f (-1, -1, -1, -1) state.pdims
1237 let getpagey pageno = fst (getpageyh pageno);;
1239 let nogeomcmds cmds =
1240 match cmds with
1241 | s, [] -> String.length s = 0
1242 | _ -> false
1245 let page_of_y y =
1246 let ((c, coverA, coverB) as cl), b =
1247 match conf.columns with
1248 | Csingle b -> (1, 0, 0), b
1249 | Cmulti (c, b) -> c, b
1250 | Csplit (_, b) -> (1, 0, 0), b
1252 if Array.length b = 0
1253 then -1
1254 else
1255 let rec bsearch nmin nmax =
1256 if nmin > nmax
1257 then bound nmin 0 (state.pagecount-1)
1258 else
1259 let n = (nmax + nmin) / 2 in
1260 let vy, h = rowyh cl b n in
1261 let y0, y1 =
1262 if conf.presentation
1263 then
1264 let ips = calcips h in
1265 let y0 = vy - ips in
1266 let y1 = vy + h + ips in
1267 y0, y1
1268 else (
1269 if n = 0
1270 then 0, vy + h + conf.interpagespace
1271 else
1272 let y0 = vy - conf.interpagespace in
1273 y0, y0 + h + conf.interpagespace
1276 if y >= y0 && y < y1
1277 then (
1278 if c = 1
1279 then n
1280 else (
1281 if n > coverA
1282 then
1283 if n < state.pagecount - coverB
1284 then ((n-coverA)/c)*c + coverA
1285 else n
1286 else n
1289 else (
1290 if y > y0
1291 then bsearch (n+1) nmax
1292 else bsearch nmin (n-1)
1295 let r = bsearch 0 (state.pagecount-1) in
1299 let layoutN ((columns, coverA, coverB), b) y sh =
1300 let sh = sh - (hscrollh ()) in
1301 let rec fold accu n =
1302 if n = Array.length b
1303 then accu
1304 else
1305 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1306 if (vy - y) > sh &&
1307 (n = coverA - 1
1308 || n = state.pagecount - coverB
1309 || (n - coverA) mod columns = columns - 1)
1310 then accu
1311 else
1312 let accu =
1313 if vy + h > y
1314 then
1315 let pagey = max 0 (y - vy) in
1316 let pagedispy = if pagey > 0 then 0 else vy - y in
1317 let pagedispx, pagex =
1318 let pdx =
1319 if n = coverA - 1 || n = state.pagecount - coverB
1320 then state.x + (wadjsb state.winw - w) / 2
1321 else dx + xoff + state.x
1323 if pdx < 0
1324 then 0, -pdx
1325 else pdx, 0
1327 let pagevw =
1328 let vw = wadjsb state.winw - pagedispx in
1329 let pw = w - pagex in
1330 min vw pw
1332 let pagevh = min (h - pagey) (sh - pagedispy) in
1333 if pagevw > 0 && pagevh > 0
1334 then
1335 let e =
1336 { pageno = n
1337 ; pagedimno = pdimno
1338 ; pagew = w
1339 ; pageh = h
1340 ; pagex = pagex
1341 ; pagey = pagey
1342 ; pagevw = pagevw
1343 ; pagevh = pagevh
1344 ; pagedispx = pagedispx
1345 ; pagedispy = pagedispy
1346 ; pagecol = 0
1349 e :: accu
1350 else
1351 accu
1352 else
1353 accu
1355 fold accu (n+1)
1357 List.rev (fold [] (page_of_y y));
1360 let layoutS (columns, b) y sh =
1361 let sh = sh - hscrollh () in
1362 let rec fold accu n =
1363 if n = Array.length b
1364 then accu
1365 else
1366 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1367 if (vy - y) > sh
1368 then accu
1369 else
1370 let accu =
1371 if vy + pageh > y
1372 then
1373 let x = xoff + state.x in
1374 let pagey = max 0 (y - vy) in
1375 let pagedispy = if pagey > 0 then 0 else vy - y in
1376 let pagedispx, pagex =
1377 if px = 0
1378 then (
1379 if x < 0
1380 then 0, -x
1381 else x, 0
1383 else (
1384 let px = px - x in
1385 if px < 0
1386 then -px, 0
1387 else 0, px
1390 let pagecolw = pagew/columns in
1391 let pagedispx =
1392 if pagecolw < state.winw
1393 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1394 else pagedispx
1396 let pagevw =
1397 let vw = wadjsb state.winw - pagedispx in
1398 let pw = pagew - pagex in
1399 min vw pw
1401 let pagevw = min pagevw pagecolw in
1402 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1403 if pagevw > 0 && pagevh > 0
1404 then
1405 let e =
1406 { pageno = n/columns
1407 ; pagedimno = pdimno
1408 ; pagew = pagew
1409 ; pageh = pageh
1410 ; pagex = pagex
1411 ; pagey = pagey
1412 ; pagevw = pagevw
1413 ; pagevh = pagevh
1414 ; pagedispx = pagedispx
1415 ; pagedispy = pagedispy
1416 ; pagecol = n mod columns
1419 e :: accu
1420 else
1421 accu
1422 else
1423 accu
1425 fold accu (n+1)
1427 List.rev (fold [] 0)
1430 let layout y sh =
1431 if nogeomcmds state.geomcmds
1432 then
1433 match conf.columns with
1434 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1435 | Cmulti c -> layoutN c y sh
1436 | Csplit s -> layoutS s y sh
1437 else []
1440 let clamp incr =
1441 let y = state.y + incr in
1442 let y = max 0 y in
1443 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1447 let itertiles l f =
1448 let tilex = l.pagex mod conf.tilew in
1449 let tiley = l.pagey mod conf.tileh in
1451 let col = l.pagex / conf.tilew in
1452 let row = l.pagey / conf.tileh in
1454 let rec rowloop row y0 dispy h =
1455 if h = 0
1456 then ()
1457 else (
1458 let dh = conf.tileh - y0 in
1459 let dh = min h dh in
1460 let rec colloop col x0 dispx w =
1461 if w = 0
1462 then ()
1463 else (
1464 let dw = conf.tilew - x0 in
1465 let dw = min w dw in
1467 f col row dispx dispy x0 y0 dw dh;
1468 colloop (col+1) 0 (dispx+dw) (w-dw)
1471 colloop col tilex l.pagedispx l.pagevw;
1472 rowloop (row+1) 0 (dispy+dh) (h-dh)
1475 if l.pagevw > 0 && l.pagevh > 0
1476 then rowloop row tiley l.pagedispy l.pagevh;
1479 let gettileopaque l col row =
1480 let key =
1481 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1483 try Some (Hashtbl.find state.tilemap key)
1484 with Not_found -> None
1487 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1488 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1489 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1492 let drawtiles l color =
1493 GlDraw.color color;
1494 let f col row x y tilex tiley w h =
1495 match gettileopaque l col row with
1496 | Some (opaque, _, t) ->
1497 let params = x, y, w, h, tilex, tiley in
1498 if conf.invert
1499 then (
1500 Gl.enable `blend;
1501 GlFunc.blend_func `zero `one_minus_src_color;
1503 drawtile params opaque;
1504 if conf.invert
1505 then Gl.disable `blend;
1506 if conf.debug
1507 then (
1508 let s = Printf.sprintf
1509 "%d[%d,%d] %f sec"
1510 l.pageno col row t
1512 let w = measurestr fstate.fontsize s in
1513 GlMisc.push_attrib [`current];
1514 GlDraw.color (0.0, 0.0, 0.0);
1515 GlDraw.rect
1516 (float (x-2), float (y-2))
1517 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1518 GlDraw.color (1.0, 1.0, 1.0);
1519 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1520 GlMisc.pop_attrib ();
1523 | _ ->
1524 let w =
1525 let lw = wadjsb state.winw - x in
1526 min lw w
1527 and h =
1528 let lh = state.winh - y in
1529 min lh h
1531 begin match state.texid with
1532 | Some id ->
1533 Gl.enable `texture_2d;
1534 GlTex.bind_texture `texture_2d id;
1535 let x0 = float x
1536 and y0 = float y
1537 and x1 = float (x+w)
1538 and y1 = float (y+h) in
1540 let tw = float w /. 16.0
1541 and th = float h /. 16.0 in
1542 let tx0 = float tilex /. 16.0
1543 and ty0 = float tiley /. 16.0 in
1544 let tx1 = tx0 +. tw
1545 and ty1 = ty0 +. th in
1546 GlDraw.begins `quads;
1547 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1548 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1549 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1550 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1551 GlDraw.ends ();
1553 Gl.disable `texture_2d;
1554 | None ->
1555 GlDraw.color (1.0, 1.0, 1.0);
1556 GlDraw.rect
1557 (float x, float y)
1558 (float (x+w), float (y+h));
1559 end;
1560 if w > 128 && h > fstate.fontsize + 10
1561 then (
1562 GlDraw.color (0.0, 0.0, 0.0);
1563 let c, r =
1564 if conf.verbose
1565 then (col*conf.tilew, row*conf.tileh)
1566 else col, row
1568 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1570 GlDraw.color color;
1572 itertiles l f
1575 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1577 let tilevisible1 l x y =
1578 let ax0 = l.pagex
1579 and ax1 = l.pagex + l.pagevw
1580 and ay0 = l.pagey
1581 and ay1 = l.pagey + l.pagevh in
1583 let bx0 = x
1584 and by0 = y in
1585 let bx1 = min (bx0 + conf.tilew) l.pagew
1586 and by1 = min (by0 + conf.tileh) l.pageh in
1588 let rx0 = max ax0 bx0
1589 and ry0 = max ay0 by0
1590 and rx1 = min ax1 bx1
1591 and ry1 = min ay1 by1 in
1593 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1594 nonemptyintersection
1597 let tilevisible layout n x y =
1598 let rec findpageinlayout m = function
1599 | l :: rest when l.pageno = n ->
1600 tilevisible1 l x y || (
1601 match conf.columns with
1602 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1603 | _ -> false
1605 | _ :: rest -> findpageinlayout 0 rest
1606 | [] -> false
1608 findpageinlayout 0 layout;
1611 let tileready l x y =
1612 tilevisible1 l x y &&
1613 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1616 let tilepage n p layout =
1617 let rec loop = function
1618 | l :: rest ->
1619 if l.pageno = n
1620 then
1621 let f col row _ _ _ _ _ _ =
1622 if state.currently = Idle
1623 then
1624 match gettileopaque l col row with
1625 | Some _ -> ()
1626 | None ->
1627 let x = col*conf.tilew
1628 and y = row*conf.tileh in
1629 let w =
1630 let w = l.pagew - x in
1631 min w conf.tilew
1633 let h =
1634 let h = l.pageh - y in
1635 min h conf.tileh
1637 let pbo =
1638 if conf.usepbo
1639 then getpbo w h conf.colorspace
1640 else "0"
1642 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1643 state.currently <-
1644 Tiling (
1645 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1646 conf.tilew, conf.tileh
1649 itertiles l f;
1650 else
1651 loop rest
1653 | [] -> ()
1655 if nogeomcmds state.geomcmds
1656 then loop layout;
1659 let preloadlayout y =
1660 let y = if y < state.winh then 0 else y - state.winh in
1661 let h = state.winh*3 in
1662 layout y h;
1665 let load pages =
1666 let rec loop pages =
1667 if state.currently != Idle
1668 then ()
1669 else
1670 match pages with
1671 | l :: rest ->
1672 begin match getopaque l.pageno with
1673 | None ->
1674 wcmd "page %d %d" l.pageno l.pagedimno;
1675 state.currently <- Loading (l, state.gen);
1676 | Some opaque ->
1677 tilepage l.pageno opaque pages;
1678 loop rest
1679 end;
1680 | _ -> ()
1682 if nogeomcmds state.geomcmds
1683 then loop pages
1686 let preload pages =
1687 load pages;
1688 if conf.preload && state.currently = Idle
1689 then load (preloadlayout state.y);
1692 let layoutready layout =
1693 let rec fold all ls =
1694 all && match ls with
1695 | l :: rest ->
1696 let seen = ref false in
1697 let allvisible = ref true in
1698 let foo col row _ _ _ _ _ _ =
1699 seen := true;
1700 allvisible := !allvisible &&
1701 begin match gettileopaque l col row with
1702 | Some _ -> true
1703 | None -> false
1706 itertiles l foo;
1707 fold (!seen && !allvisible) rest
1708 | [] -> true
1710 let alltilesvisible = fold true layout in
1711 alltilesvisible;
1714 let gotoy y =
1715 let y = bound y 0 state.maxy in
1716 let y, layout, proceed =
1717 match conf.maxwait with
1718 | Some time when state.ghyll == noghyll ->
1719 begin match state.throttle with
1720 | None ->
1721 let layout = layout y state.winh in
1722 let ready = layoutready layout in
1723 if not ready
1724 then (
1725 load layout;
1726 state.throttle <- Some (layout, y, now ());
1728 else G.postRedisplay "gotoy showall (None)";
1729 y, layout, ready
1730 | Some (_, _, started) ->
1731 let dt = now () -. started in
1732 if dt > time
1733 then (
1734 state.throttle <- None;
1735 let layout = layout y state.winh in
1736 load layout;
1737 G.postRedisplay "maxwait";
1738 y, layout, true
1740 else -1, [], false
1743 | _ ->
1744 let layout = layout y state.winh in
1745 if not !wtmode || layoutready layout
1746 then G.postRedisplay "gotoy ready";
1747 y, layout, true
1749 if proceed
1750 then (
1751 state.y <- y;
1752 state.layout <- layout;
1753 begin match state.mode with
1754 | LinkNav (Ltexact (pageno, linkno)) ->
1755 let rec loop = function
1756 | [] ->
1757 state.mode <- LinkNav (Ltgendir 0)
1758 | l :: _ when l.pageno = pageno ->
1759 begin match getopaque pageno with
1760 | None ->
1761 state.mode <- LinkNav (Ltgendir 0)
1762 | Some opaque ->
1763 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1764 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1765 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1766 then state.mode <- LinkNav (Ltgendir 0)
1768 | _ :: rest -> loop rest
1770 loop layout
1771 | _ -> ()
1772 end;
1773 begin match state.mode with
1774 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1775 if not (pagevisible layout pageno)
1776 then (
1777 match state.layout with
1778 | [] -> ()
1779 | l :: _ ->
1780 state.mode <- Birdseye (
1781 conf, leftx, l.pageno, hooverpageno, anchor
1784 | LinkNav (Ltgendir dir as lt) ->
1785 let linknav =
1786 let rec loop = function
1787 | [] -> lt
1788 | l :: rest ->
1789 match getopaque l.pageno with
1790 | None -> loop rest
1791 | Some opaque ->
1792 let link =
1793 let ld =
1794 if dir = 0
1795 then LDfirstvisible (l.pagex, l.pagey, dir)
1796 else (
1797 if dir > 0 then LDfirst else LDlast
1800 findlink opaque ld
1802 match link with
1803 | Lnotfound -> loop rest
1804 | Lfound n ->
1805 showlinktype (getlink opaque n);
1806 Ltexact (l.pageno, n)
1808 loop state.layout
1810 state.mode <- LinkNav linknav
1811 | _ -> ()
1812 end;
1813 preload layout;
1815 state.ghyll <- noghyll;
1816 if conf.updatecurs
1817 then (
1818 let mx, my = state.mpos in
1819 updateunder mx my;
1823 let conttiling pageno opaque =
1824 tilepage pageno opaque
1825 (if conf.preload then preloadlayout state.y else state.layout)
1828 let gotoy_and_clear_text y =
1829 if not conf.verbose then state.text <- "";
1830 gotoy y;
1833 let getanchor1 l =
1834 let top =
1835 let coloff = l.pagecol * l.pageh in
1836 float (l.pagey + coloff) /. float l.pageh
1838 let dtop =
1839 if l.pagedispy = 0
1840 then
1842 else (
1843 if conf.presentation
1844 then float l.pagedispy /. float (calcips l.pageh)
1845 else float l.pagedispy /. float conf.interpagespace
1848 (l.pageno, top, dtop)
1851 let getanchor () =
1852 match state.layout with
1853 | l :: _ -> getanchor1 l
1854 | [] ->
1855 let n = page_of_y state.y in
1856 if n = -1
1857 then state.anchor
1858 else
1859 let y, h = getpageyh n in
1860 let dy = y - state.y in
1861 let dtop =
1862 if conf.presentation
1863 then
1864 let ips = calcips h in
1865 float (dy + ips) /. float ips
1866 else
1867 float dy /. float conf.interpagespace
1869 (n, 0.0, dtop)
1872 let getanchory (n, top, dtop) =
1873 let y, h = getpageyh n in
1874 if conf.presentation
1875 then
1876 let ips = calcips h in
1877 y + truncate (top*.float h -. dtop*.float ips) + ips;
1878 else
1879 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1882 let gotoanchor anchor =
1883 gotoy (getanchory anchor);
1886 let addnav () =
1887 cbput state.hists.nav (getanchor ());
1890 let getnav dir =
1891 let anchor = cbgetc state.hists.nav dir in
1892 getanchory anchor;
1895 let gotoghyll y =
1896 let scroll f n a b =
1897 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1898 let snake f a b =
1899 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1900 if f < a
1901 then s (float f /. float a)
1902 else (
1903 if f > b
1904 then 1.0 -. s ((float (f-b) /. float (n-b)))
1905 else 1.0
1908 snake f a b
1909 and summa f n a b =
1910 (* courtesy: (calc-eval "integ(3x^2-2x^3,x)") *)
1911 let iv x = x**3.-.0.5*.x**4. in
1912 let iv1 = iv f in
1913 let ins = float a *. iv1
1914 and outs = float (n-b) *. iv1 in
1915 let ones = b - a in
1916 ins +. outs +. float ones
1918 let rec set (_N, _A, _B) y sy =
1919 let sum = summa 1.0 _N _A _B in
1920 let dy = float (y - sy) in
1921 state.ghyll <- (
1922 let rec gf n y1 o =
1923 if n >= _N
1924 then state.ghyll <- noghyll
1925 else
1926 let go n =
1927 let s = scroll n _N _A _B in
1928 let y1 = y1 +. ((s *. dy) /. sum) in
1929 gotoy_and_clear_text (truncate y1);
1930 state.ghyll <- gf (n+1) y1;
1932 match o with
1933 | None -> go n
1934 | Some y' -> set (_N/2, 1, 1) y' state.y
1936 gf 0 (float state.y)
1939 match conf.ghyllscroll with
1940 | None ->
1941 gotoy_and_clear_text y
1942 | Some nab ->
1943 if state.ghyll == noghyll
1944 then set nab y state.y
1945 else state.ghyll (Some y)
1948 let gotopage n top =
1949 let y, h = getpageyh n in
1950 let y = y + (truncate (top *. float h)) in
1951 gotoghyll y
1954 let gotopage1 n top =
1955 let y = getpagey n in
1956 let y = y + top in
1957 gotoghyll y
1960 let invalidate s f =
1961 state.layout <- [];
1962 state.pdims <- [];
1963 state.rects <- [];
1964 state.rects1 <- [];
1965 match state.geomcmds with
1966 | ps, [] when String.length ps = 0 ->
1967 f ();
1968 state.geomcmds <- s, [];
1970 | ps, [] ->
1971 state.geomcmds <- ps, [s, f];
1973 | ps, (s', _) :: rest when s' = s ->
1974 state.geomcmds <- ps, ((s, f) :: rest);
1976 | ps, cmds ->
1977 state.geomcmds <- ps, ((s, f) :: cmds);
1980 let flushpages () =
1981 Hashtbl.iter (fun _ opaque ->
1982 wcmd "freepage %s" opaque;
1983 ) state.pagemap;
1984 Hashtbl.clear state.pagemap;
1987 let flushtiles () =
1988 if not (Queue.is_empty state.tilelru)
1989 then (
1990 Queue.iter (fun (k, p, s) ->
1991 wcmd "freetile %s" p;
1992 state.memused <- state.memused - s;
1993 Hashtbl.remove state.tilemap k;
1994 ) state.tilelru;
1995 state.uioh#infochanged Memused;
1996 Queue.clear state.tilelru;
1998 load state.layout;
2001 let stateh h =
2002 let h = truncate (float h*.conf.zoom) in
2003 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2004 h - d
2007 let opendoc path password =
2008 state.path <- path;
2009 state.password <- password;
2010 state.gen <- state.gen + 1;
2011 state.docinfo <- [];
2013 flushpages ();
2014 setaalevel conf.aalevel;
2015 let titlepath =
2016 if String.length state.origin = 0
2017 then path
2018 else state.origin
2020 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2021 wcmd "open %d %s\000%s\000" (btod !wtmode) path password;
2022 invalidate "reqlayout"
2023 (fun () ->
2024 wcmd "reqlayout %d %d %d %s\000"
2025 conf.angle (int_of_fitmodel conf.fitmodel)
2026 (stateh state.winh) state.nameddest
2030 let reload () =
2031 state.anchor <- getanchor ();
2032 opendoc state.path state.password;
2035 let scalecolor c =
2036 let c = c *. conf.colorscale in
2037 (c, c, c);
2040 let scalecolor2 (r, g, b) =
2041 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2044 let docolumns = function
2045 | Csingle _ ->
2046 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2047 let rec loop pageno pdimno pdim y ph pdims =
2048 if pageno = state.pagecount
2049 then ()
2050 else
2051 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2052 match pdims with
2053 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2054 pdimno+1, pdim, rest
2055 | _ ->
2056 pdimno, pdim, pdims
2058 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2059 let y = y +
2060 (if conf.presentation
2061 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2062 else (if pageno = 0 then 0 else conf.interpagespace)
2065 a.(pageno) <- (pdimno, x, y, pdim);
2066 loop (pageno+1) pdimno pdim (y + h) h pdims
2068 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2069 conf.columns <- Csingle a;
2071 | Cmulti ((columns, coverA, coverB), _) ->
2072 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2073 let rec loop pageno pdimno pdim x y rowh pdims =
2074 let rec fixrow m = if m = pageno then () else
2075 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2076 if h < rowh
2077 then (
2078 let y = y + (rowh - h) / 2 in
2079 a.(m) <- (pdimno, x, y, pdim);
2081 fixrow (m+1)
2083 if pageno = state.pagecount
2084 then fixrow (((pageno - 1) / columns) * columns)
2085 else
2086 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2087 match pdims with
2088 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2089 pdimno+1, pdim, rest
2090 | _ ->
2091 pdimno, pdim, pdims
2093 let x, y, rowh' =
2094 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2095 then (
2096 let x = (wadjsb state.winw - w) / 2 in
2097 let ips =
2098 if conf.presentation then calcips h else conf.interpagespace in
2099 x, y + ips + rowh, h
2101 else (
2102 if (pageno - coverA) mod columns = 0
2103 then (
2104 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2105 let y =
2106 if conf.presentation
2107 then
2108 let ips = calcips h in
2109 y + (if pageno = 0 then 0 else calcips rowh + ips)
2110 else
2111 y + (if pageno = 0 then 0 else conf.interpagespace)
2113 x, y + rowh, h
2115 else x, y, max rowh h
2118 let y =
2119 if pageno > 1 && (pageno - coverA) mod columns = 0
2120 then (
2121 let y =
2122 if pageno = columns && conf.presentation
2123 then (
2124 let ips = calcips rowh in
2125 for i = 0 to pred columns
2127 let (pdimno, x, y, pdim) = a.(i) in
2128 a.(i) <- (pdimno, x, y+ips, pdim)
2129 done;
2130 y+ips;
2132 else y
2134 fixrow (pageno - columns);
2137 else y
2139 a.(pageno) <- (pdimno, x, y, pdim);
2140 let x = x + w + xoff*2 + conf.interpagespace in
2141 loop (pageno+1) pdimno pdim x y rowh' pdims
2143 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2144 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2146 | Csplit (c, _) ->
2147 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2148 let rec loop pageno pdimno pdim y pdims =
2149 if pageno = state.pagecount
2150 then ()
2151 else
2152 let pdimno, ((_, w, h, _) as pdim), pdims =
2153 match pdims with
2154 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2155 pdimno+1, pdim, rest
2156 | _ ->
2157 pdimno, pdim, pdims
2159 let cw = w / c in
2160 let rec loop1 n x y =
2161 if n = c then y else (
2162 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2163 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2166 let y = loop1 0 0 y in
2167 loop (pageno+1) pdimno pdim y pdims
2169 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2170 conf.columns <- Csplit (c, a);
2173 let represent () =
2174 docolumns conf.columns;
2175 state.maxy <- calcheight ();
2176 if state.reprf == noreprf
2177 then (
2178 match state.mode with
2179 | Birdseye (_, _, pageno, _, _) ->
2180 let y, h = getpageyh pageno in
2181 let top = (state.winh - h) / 2 in
2182 gotoy (max 0 (y - top))
2183 | _ -> gotoanchor state.anchor
2185 else (
2186 state.reprf ();
2187 state.reprf <- noreprf;
2191 let reshape w h =
2192 GlDraw.viewport 0 0 w h;
2193 let firsttime = state.geomcmds == firstgeomcmds in
2194 if not firsttime && nogeomcmds state.geomcmds
2195 then state.anchor <- getanchor ();
2197 state.winw <- w;
2198 let w = wadjsb (truncate (float w *. conf.zoom)) in
2199 let w = max w 2 in
2200 state.winh <- h;
2201 setfontsize fstate.fontsize;
2202 GlMat.mode `modelview;
2203 GlMat.load_identity ();
2205 GlMat.mode `projection;
2206 GlMat.load_identity ();
2207 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2208 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2209 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2211 let relx =
2212 if conf.zoom <= 1.0
2213 then 0.0
2214 else float state.x /. float state.w
2216 invalidate "geometry"
2217 (fun () ->
2218 state.w <- w;
2219 if not firsttime
2220 then state.x <- truncate (relx *. float w);
2221 let w =
2222 match conf.columns with
2223 | Csingle _ -> w
2224 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2225 | Csplit (c, _) -> w * c
2227 wcmd "geometry %d %d %d"
2228 w (stateh h) (int_of_fitmodel conf.fitmodel)
2232 let enttext () =
2233 let len = String.length state.text in
2234 let drawstring s =
2235 let hscrollh =
2236 match state.mode with
2237 | Textentry _ | View | LinkNav _ ->
2238 let h, _, _ = state.uioh#scrollpw in
2240 | _ -> 0
2242 let rect x w =
2243 GlDraw.rect
2244 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2245 (x+.w, float (state.winh - hscrollh))
2248 let w = float (wadjsb state.winw - 1) in
2249 if state.progress >= 0.0 && state.progress < 1.0
2250 then (
2251 GlDraw.color (0.3, 0.3, 0.3);
2252 let w1 = w *. state.progress in
2253 rect 0.0 w1;
2254 GlDraw.color (0.0, 0.0, 0.0);
2255 rect w1 (w-.w1)
2257 else (
2258 GlDraw.color (0.0, 0.0, 0.0);
2259 rect 0.0 w;
2262 GlDraw.color (1.0, 1.0, 1.0);
2263 drawstring fstate.fontsize
2264 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2266 let s =
2267 match state.mode with
2268 | Textentry ((prefix, text, _, _, _, _), _) ->
2269 let s =
2270 if len > 0
2271 then
2272 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2273 else
2274 Printf.sprintf "%s%s_" prefix text
2278 | _ -> state.text
2280 let s =
2281 if state.newerrmsgs
2282 then (
2283 if not (istextentry state.mode) && state.uioh#eformsgs
2284 then
2285 let s1 = "(press 'e' to review error messasges)" in
2286 if String.length s > 0 then s ^ " " ^ s1 else s1
2287 else s
2289 else s
2291 if String.length s > 0
2292 then drawstring s
2295 let gctiles () =
2296 let len = Queue.length state.tilelru in
2297 let layout = lazy (
2298 match state.throttle with
2299 | None ->
2300 if conf.preload
2301 then preloadlayout state.y
2302 else state.layout
2303 | Some (layout, _, _) ->
2304 layout
2305 ) in
2306 let rec loop qpos =
2307 if state.memused <= conf.memlimit
2308 then ()
2309 else (
2310 if qpos < len
2311 then
2312 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2313 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2314 let (_, pw, ph, _) = getpagedim n in
2316 gen = state.gen
2317 && colorspace = conf.colorspace
2318 && angle = conf.angle
2319 && pagew = pw
2320 && pageh = ph
2321 && (
2322 let x = col*conf.tilew
2323 and y = row*conf.tileh in
2324 tilevisible (Lazy.force_val layout) n x y
2326 then Queue.push lruitem state.tilelru
2327 else (
2328 freepbo p;
2329 wcmd "freetile %s" p;
2330 state.memused <- state.memused - s;
2331 state.uioh#infochanged Memused;
2332 Hashtbl.remove state.tilemap k;
2334 loop (qpos+1)
2337 loop 0
2340 let logcurrently = function
2341 | Idle -> dolog "Idle"
2342 | Loading (l, gen) ->
2343 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2344 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2345 dolog
2346 "Tiling %d[%d,%d] page=%s cs=%s angle"
2347 l.pageno col row pageopaque
2348 (colorspace_to_string colorspace)
2350 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2351 angle gen conf.angle state.gen
2352 tilew tileh
2353 conf.tilew conf.tileh
2355 | Outlining _ ->
2356 dolog "outlining"
2359 let splitatspace =
2360 let r = Str.regexp " " in
2361 fun s -> Str.bounded_split r s 2;
2364 let onpagerect pageno f =
2365 let b =
2366 match conf.columns with
2367 | Cmulti (_, b) -> b
2368 | Csingle b -> b
2369 | Csplit (_, b) -> b
2371 if pageno >= 0 && pageno < Array.length b
2372 then
2373 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2374 let r = getpdimrect pdimno in
2375 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2378 let gotopagexy1 pageno x y =
2379 onpagerect pageno (fun w h ->
2380 let top = y /. h in
2381 let _,w1,_,leftx = getpagedim pageno in
2382 let wh = state.winh - hscrollh () in
2383 let sw = float w1 /. w in
2384 let x = sw *. x in
2385 let x = leftx + state.x + truncate x in
2386 let sx =
2387 if x < 0 || x >= wadjsb state.winw
2388 then state.x - x
2389 else state.x
2391 let py, h = getpageyh pageno in
2392 let pdy = truncate (top *. float h) in
2393 let y' = py + pdy in
2394 let dy = y' - state.y in
2395 let sy =
2396 if x != state.x || not (dy > 0 && dy < wh)
2397 then (
2398 if conf.presentation
2399 then
2400 if abs (py - y') > wh
2401 then y'
2402 else py
2403 else y';
2405 else state.y
2407 if state.x != sx || state.y != sy
2408 then (
2409 let x, y =
2410 if !wtmode
2411 then (
2412 let ww = wadjsb state.winw in
2413 let qx = sx / ww
2414 and qy = pdy / wh in
2415 let x = qx * ww
2416 and y = py + qy * wh in
2417 let x = if -x + ww > w1 then -(w1-ww) else x
2418 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2419 let y =
2420 if conf.presentation
2421 then
2422 if abs (py - y') > wh
2423 then y'
2424 else py
2425 else y';
2427 (x, y)
2429 else (sx, sy)
2431 state.x <- x;
2432 gotoy_and_clear_text y;
2434 else gotoy_and_clear_text state.y;
2438 let gotopagexy pageno x y =
2439 match state.mode with
2440 | Birdseye _ -> gotopage pageno 0.0
2441 | _ -> gotopagexy1 pageno x y
2444 let act cmds =
2445 (* dolog "%S" cmds; *)
2446 let cl = splitatspace cmds in
2447 let scan s fmt f =
2448 try Scanf.sscanf s fmt f
2449 with exn ->
2450 dolog "error processing '%S': %s" cmds (exntos exn);
2451 exit 1
2453 match cl with
2454 | "clear" :: [] ->
2455 state.uioh#infochanged Pdim;
2456 state.pdims <- [];
2458 | "clearrects" :: [] ->
2459 state.rects <- state.rects1;
2460 G.postRedisplay "clearrects";
2462 | "continue" :: args :: [] ->
2463 let n = scan args "%u" (fun n -> n) in
2464 state.pagecount <- n;
2465 begin match state.currently with
2466 | Outlining l ->
2467 state.currently <- Idle;
2468 state.outlines <- Array.of_list (List.rev l)
2469 | _ -> ()
2470 end;
2472 let cur, cmds = state.geomcmds in
2473 if String.length cur = 0
2474 then failwith "umpossible";
2476 begin match List.rev cmds with
2477 | [] ->
2478 state.geomcmds <- "", [];
2479 represent ();
2480 | (s, f) :: rest ->
2481 f ();
2482 state.geomcmds <- s, List.rev rest;
2483 end;
2484 if conf.maxwait = None && not !wtmode
2485 then G.postRedisplay "continue";
2487 | "title" :: args :: [] ->
2488 Wsi.settitle args
2490 | "msg" :: args :: [] ->
2491 showtext ' ' args
2493 | "vmsg" :: args :: [] ->
2494 if conf.verbose
2495 then showtext ' ' args
2497 | "emsg" :: args :: [] ->
2498 Buffer.add_string state.errmsgs args;
2499 state.newerrmsgs <- true;
2500 G.postRedisplay "error message"
2502 | "progress" :: args :: [] ->
2503 let progress, text =
2504 scan args "%f %n"
2505 (fun f pos ->
2506 f, String.sub args pos (String.length args - pos))
2508 state.text <- text;
2509 state.progress <- progress;
2510 G.postRedisplay "progress"
2512 | "firstmatch" :: args :: [] ->
2513 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2514 scan args "%u %d %f %f %f %f %f %f %f %f"
2515 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2516 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2518 let y = (getpagey pageno) + truncate y0 in
2519 addnav ();
2520 gotoy y;
2521 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2523 | "match" :: args :: [] ->
2524 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2525 scan args "%u %d %f %f %f %f %f %f %f %f"
2526 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2527 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2529 state.rects1 <-
2530 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2532 | "page" :: args :: [] ->
2533 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2534 begin match state.currently with
2535 | Loading (l, gen) ->
2536 vlog "page %d took %f sec" l.pageno t;
2537 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2538 begin match state.throttle with
2539 | None ->
2540 let preloadedpages =
2541 if conf.preload
2542 then preloadlayout state.y
2543 else state.layout
2545 let evict () =
2546 let set =
2547 List.fold_left (fun s l -> IntSet.add l.pageno s)
2548 IntSet.empty preloadedpages
2550 let evictedpages =
2551 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2552 if not (IntSet.mem pageno set)
2553 then (
2554 wcmd "freepage %s" opaque;
2555 key :: accu
2557 else accu
2558 ) state.pagemap []
2560 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2562 evict ();
2563 state.currently <- Idle;
2564 if gen = state.gen
2565 then (
2566 tilepage l.pageno pageopaque state.layout;
2567 load state.layout;
2568 load preloadedpages;
2569 if pagevisible state.layout l.pageno
2570 && layoutready state.layout
2571 then G.postRedisplay "page";
2574 | Some (layout, _, _) ->
2575 state.currently <- Idle;
2576 tilepage l.pageno pageopaque layout;
2577 load state.layout
2578 end;
2580 | _ ->
2581 dolog "Inconsistent loading state";
2582 logcurrently state.currently;
2583 exit 1
2586 | "tile" :: args :: [] ->
2587 let (x, y, opaque, size, t) =
2588 scan args "%u %u %s %u %f"
2589 (fun x y p size t -> (x, y, p, size, t))
2591 begin match state.currently with
2592 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2593 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2595 unmappbo opaque;
2596 if tilew != conf.tilew || tileh != conf.tileh
2597 then (
2598 wcmd "freetile %s" opaque;
2599 state.currently <- Idle;
2600 load state.layout;
2602 else (
2603 puttileopaque l col row gen cs angle opaque size t;
2604 state.memused <- state.memused + size;
2605 state.uioh#infochanged Memused;
2606 gctiles ();
2607 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2608 opaque, size) state.tilelru;
2610 let layout =
2611 match state.throttle with
2612 | None -> state.layout
2613 | Some (layout, _, _) -> layout
2616 state.currently <- Idle;
2617 if gen = state.gen
2618 && conf.colorspace = cs
2619 && conf.angle = angle
2620 && tilevisible layout l.pageno x y
2621 then conttiling l.pageno pageopaque;
2623 begin match state.throttle with
2624 | None ->
2625 preload state.layout;
2626 if gen = state.gen
2627 && conf.colorspace = cs
2628 && conf.angle = angle
2629 && tilevisible state.layout l.pageno x y
2630 && (not !wtmode || layoutready state.layout)
2631 then G.postRedisplay "tile nothrottle";
2633 | Some (layout, y, _) ->
2634 let ready = layoutready layout in
2635 if ready
2636 then (
2637 state.y <- y;
2638 state.layout <- layout;
2639 state.throttle <- None;
2640 G.postRedisplay "throttle";
2642 else load layout;
2643 end;
2646 | _ ->
2647 dolog "Inconsistent tiling state";
2648 logcurrently state.currently;
2649 exit 1
2652 | "pdim" :: args :: [] ->
2653 let (n, w, h, _) as pdim =
2654 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2656 let pdim =
2657 match conf.fitmodel, conf.columns with
2658 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2659 | _ -> pdim
2661 state.uioh#infochanged Pdim;
2662 state.pdims <- pdim :: state.pdims
2664 | "o" :: args :: [] ->
2665 let (l, n, t, h, pos) =
2666 scan args "%u %u %d %u %n"
2667 (fun l n t h pos -> l, n, t, h, pos)
2669 let s = String.sub args pos (String.length args - pos) in
2670 let outline = (s, l, (n, float t /. float h, 0.0)) in
2671 begin match state.currently with
2672 | Outlining outlines ->
2673 state.currently <- Outlining (outline :: outlines)
2674 | Idle ->
2675 state.currently <- Outlining [outline]
2676 | currently ->
2677 dolog "invalid outlining state";
2678 logcurrently currently
2681 | "a" :: args :: [] ->
2682 let (n, l, t) =
2683 scan args "%u %d %d" (fun n l t -> n, l, t)
2685 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2687 | "info" :: args :: [] ->
2688 state.docinfo <- (1, args) :: state.docinfo
2690 | "infoend" :: [] ->
2691 state.uioh#infochanged Docinfo;
2692 state.docinfo <- List.rev state.docinfo
2694 | _ ->
2695 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2698 let onhist cb =
2699 let rc = cb.rc in
2700 let action = function
2701 | HCprev -> cbget cb ~-1
2702 | HCnext -> cbget cb 1
2703 | HCfirst -> cbget cb ~-(cb.rc)
2704 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2705 and cancel () = cb.rc <- rc
2706 in (action, cancel)
2709 let search pattern forward =
2710 match conf.columns with
2711 | Csplit _ ->
2712 showtext '!' "searching does not work properly in split columns mode"
2713 | _ ->
2714 if String.length pattern > 0
2715 then
2716 let pn, py =
2717 match state.layout with
2718 | [] -> 0, 0
2719 | l :: _ ->
2720 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2722 wcmd "search %d %d %d %d,%s\000"
2723 (btod conf.icase) pn py (btod forward) pattern;
2726 let intentry text key =
2727 let c =
2728 if key >= 32 && key < 127
2729 then Char.chr key
2730 else '\000'
2732 match c with
2733 | '0' .. '9' ->
2734 let text = addchar text c in
2735 TEcont text
2737 | _ ->
2738 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2739 TEcont text
2742 let linknentry text key =
2743 let c =
2744 if key >= 32 && key < 127
2745 then Char.chr key
2746 else '\000'
2748 match c with
2749 | 'a' .. 'z' ->
2750 let text = addchar text c in
2751 TEcont text
2753 | _ ->
2754 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2755 TEcont text
2758 let linkndone f s =
2759 if String.length s > 0
2760 then (
2761 let n =
2762 let l = String.length s in
2763 let rec loop pos n = if pos = l then n else
2764 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2765 loop (pos+1) (n*26 + m)
2766 in loop 0 0
2768 let rec loop n = function
2769 | [] -> ()
2770 | l :: rest ->
2771 match getopaque l.pageno with
2772 | None -> loop n rest
2773 | Some opaque ->
2774 let m = getlinkcount opaque in
2775 if n < m
2776 then (
2777 let under = getlink opaque n in
2778 f under
2780 else loop (n-m) rest
2782 loop n state.layout;
2786 let textentry text key =
2787 if key land 0xff00 = 0xff00
2788 then TEcont text
2789 else TEcont (text ^ toutf8 key)
2792 let reqlayout angle fitmodel =
2793 match state.throttle with
2794 | None ->
2795 if nogeomcmds state.geomcmds
2796 then state.anchor <- getanchor ();
2797 conf.angle <- angle mod 360;
2798 if conf.angle != 0
2799 then (
2800 match state.mode with
2801 | LinkNav _ -> state.mode <- View
2802 | _ -> ()
2804 conf.fitmodel <- fitmodel;
2805 invalidate "reqlayout"
2806 (fun () ->
2807 wcmd "reqlayout %d %d %d"
2808 conf.angle (int_of_fitmodel conf.fitmodel) (stateh state.winh)
2810 | _ -> ()
2813 let settrim trimmargins trimfuzz =
2814 if nogeomcmds state.geomcmds
2815 then state.anchor <- getanchor ();
2816 conf.trimmargins <- trimmargins;
2817 conf.trimfuzz <- trimfuzz;
2818 let x0, y0, x1, y1 = trimfuzz in
2819 invalidate "settrim"
2820 (fun () ->
2821 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2822 flushpages ();
2825 let setzoom zoom =
2826 match state.throttle with
2827 | None ->
2828 let zoom = max 0.0001 zoom in
2829 if zoom <> conf.zoom
2830 then (
2831 state.prevzoom <- conf.zoom;
2832 conf.zoom <- zoom;
2833 reshape state.winw state.winh;
2834 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2837 | Some (layout, y, started) ->
2838 let time =
2839 match conf.maxwait with
2840 | None -> 0.0
2841 | Some t -> t
2843 let dt = now () -. started in
2844 if dt > time
2845 then (
2846 state.y <- y;
2847 load layout;
2851 let setcolumns mode columns coverA coverB =
2852 state.prevcolumns <- Some (conf.columns, conf.zoom);
2853 if columns < 0
2854 then (
2855 if isbirdseye mode
2856 then showtext '!' "split mode doesn't work in bird's eye"
2857 else (
2858 conf.columns <- Csplit (-columns, [||]);
2859 state.x <- 0;
2860 conf.zoom <- 1.0;
2863 else (
2864 if columns < 2
2865 then (
2866 conf.columns <- Csingle [||];
2867 state.x <- 0;
2868 setzoom 1.0;
2870 else (
2871 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2872 conf.zoom <- 1.0;
2875 reshape state.winw state.winh;
2878 let enterbirdseye () =
2879 let zoom = float conf.thumbw /. float state.winw in
2880 let birdseyepageno =
2881 let cy = state.winh / 2 in
2882 let fold = function
2883 | [] -> 0
2884 | l :: rest ->
2885 let rec fold best = function
2886 | [] -> best.pageno
2887 | l :: rest ->
2888 let d = cy - (l.pagedispy + l.pagevh/2)
2889 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2890 if abs d < abs dbest
2891 then fold l rest
2892 else best.pageno
2893 in fold l rest
2895 fold state.layout
2897 state.mode <- Birdseye (
2898 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2900 conf.zoom <- zoom;
2901 conf.presentation <- false;
2902 conf.interpagespace <- 10;
2903 conf.hlinks <- false;
2904 conf.fitmodel <- FitProportional;
2905 state.x <- 0;
2906 state.mstate <- Mnone;
2907 conf.maxwait <- None;
2908 conf.columns <- (
2909 match conf.beyecolumns with
2910 | Some c ->
2911 conf.zoom <- 1.0;
2912 Cmulti ((c, 0, 0), [||])
2913 | None -> Csingle [||]
2915 Wsi.setcursor Wsi.CURSOR_INHERIT;
2916 if conf.verbose
2917 then
2918 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2919 (100.0*.zoom)
2920 else
2921 state.text <- ""
2923 reshape state.winw state.winh;
2926 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2927 state.mode <- View;
2928 conf.zoom <- c.zoom;
2929 conf.presentation <- c.presentation;
2930 conf.interpagespace <- c.interpagespace;
2931 conf.maxwait <- c.maxwait;
2932 conf.hlinks <- c.hlinks;
2933 conf.fitmodel <- c.fitmodel;
2934 conf.beyecolumns <- (
2935 match conf.columns with
2936 | Cmulti ((c, _, _), _) -> Some c
2937 | Csingle _ -> None
2938 | Csplit _ -> failwith "leaving bird's eye split mode"
2940 conf.columns <- (
2941 match c.columns with
2942 | Cmulti (c, _) -> Cmulti (c, [||])
2943 | Csingle _ -> Csingle [||]
2944 | Csplit (c, _) -> Csplit (c, [||])
2946 state.x <- leftx;
2947 if conf.verbose
2948 then
2949 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2950 (100.0*.conf.zoom)
2952 reshape state.winw state.winh;
2953 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2956 let togglebirdseye () =
2957 match state.mode with
2958 | Birdseye vals -> leavebirdseye vals true
2959 | View -> enterbirdseye ()
2960 | _ -> ()
2963 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2964 let pageno = max 0 (pageno - incr) in
2965 let rec loop = function
2966 | [] -> gotopage1 pageno 0
2967 | l :: _ when l.pageno = pageno ->
2968 if l.pagedispy >= 0 && l.pagey = 0
2969 then G.postRedisplay "upbirdseye"
2970 else gotopage1 pageno 0
2971 | _ :: rest -> loop rest
2973 loop state.layout;
2974 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2977 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2978 let pageno = min (state.pagecount - 1) (pageno + incr) in
2979 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2980 let rec loop = function
2981 | [] ->
2982 let y, h = getpageyh pageno in
2983 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2984 gotoy (clamp dy)
2985 | l :: _ when l.pageno = pageno ->
2986 if l.pagevh != l.pageh
2987 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2988 else G.postRedisplay "downbirdseye"
2989 | _ :: rest -> loop rest
2991 loop state.layout
2994 let optentry mode _ key =
2995 let btos b = if b then "on" else "off" in
2996 if key >= 32 && key < 127
2997 then
2998 let c = Char.chr key in
2999 match c with
3000 | 's' ->
3001 let ondone s =
3002 try conf.scrollstep <- int_of_string s with exc ->
3003 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3005 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3007 | 'A' ->
3008 let ondone s =
3010 conf.autoscrollstep <- int_of_string s;
3011 if state.autoscroll <> None
3012 then state.autoscroll <- Some conf.autoscrollstep
3013 with exc ->
3014 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3016 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3018 | 'C' ->
3019 let ondone s =
3021 let n, a, b = multicolumns_of_string s in
3022 setcolumns mode n a b;
3023 with exc ->
3024 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3026 TEswitch ("columns: ", "", None, textentry, ondone, true)
3028 | 'Z' ->
3029 let ondone s =
3031 let zoom = float (int_of_string s) /. 100.0 in
3032 setzoom zoom
3033 with exc ->
3034 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3036 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3038 | 't' ->
3039 let ondone s =
3041 conf.thumbw <- bound (int_of_string s) 2 4096;
3042 state.text <-
3043 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3044 begin match mode with
3045 | Birdseye beye ->
3046 leavebirdseye beye false;
3047 enterbirdseye ();
3048 | _ -> ();
3050 with exc ->
3051 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3053 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3055 | 'R' ->
3056 let ondone s =
3057 match try
3058 Some (int_of_string s)
3059 with exc ->
3060 state.text <- Printf.sprintf "bad integer `%s': %s"
3061 s (exntos exc);
3062 None
3063 with
3064 | Some angle -> reqlayout angle conf.fitmodel
3065 | None -> ()
3067 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3069 | 'i' ->
3070 conf.icase <- not conf.icase;
3071 TEdone ("case insensitive search " ^ (btos conf.icase))
3073 | 'p' ->
3074 conf.preload <- not conf.preload;
3075 gotoy state.y;
3076 TEdone ("preload " ^ (btos conf.preload))
3078 | 'v' ->
3079 conf.verbose <- not conf.verbose;
3080 TEdone ("verbose " ^ (btos conf.verbose))
3082 | 'd' ->
3083 conf.debug <- not conf.debug;
3084 TEdone ("debug " ^ (btos conf.debug))
3086 | 'h' ->
3087 conf.maxhfit <- not conf.maxhfit;
3088 state.maxy <- calcheight ();
3089 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3091 | 'c' ->
3092 conf.crophack <- not conf.crophack;
3093 TEdone ("crophack " ^ btos conf.crophack)
3095 | 'a' ->
3096 let s =
3097 match conf.maxwait with
3098 | None ->
3099 conf.maxwait <- Some infinity;
3100 "always wait for page to complete"
3101 | Some _ ->
3102 conf.maxwait <- None;
3103 "show placeholder if page is not ready"
3105 TEdone s
3107 | 'f' ->
3108 conf.underinfo <- not conf.underinfo;
3109 TEdone ("underinfo " ^ btos conf.underinfo)
3111 | 'P' ->
3112 conf.savebmarks <- not conf.savebmarks;
3113 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3115 | 'S' ->
3116 let ondone s =
3118 let pageno, py =
3119 match state.layout with
3120 | [] -> 0, 0
3121 | l :: _ ->
3122 l.pageno, l.pagey
3124 conf.interpagespace <- int_of_string s;
3125 docolumns conf.columns;
3126 state.maxy <- calcheight ();
3127 let y = getpagey pageno in
3128 gotoy (y + py)
3129 with exc ->
3130 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3132 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3134 | 'l' ->
3135 let fm =
3136 match conf.fitmodel with
3137 | FitProportional -> FitWidth
3138 | _ -> FitProportional
3140 reqlayout conf.angle fm;
3141 TEdone ("proportional display " ^ btos (fm == FitProportional))
3143 | 'T' ->
3144 settrim (not conf.trimmargins) conf.trimfuzz;
3145 TEdone ("trim margins " ^ btos conf.trimmargins)
3147 | 'I' ->
3148 conf.invert <- not conf.invert;
3149 TEdone ("invert colors " ^ btos conf.invert)
3151 | 'x' ->
3152 let ondone s =
3153 cbput state.hists.sel s;
3154 conf.selcmd <- s;
3156 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3157 textentry, ondone, true)
3159 | _ ->
3160 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3161 TEstop
3162 else
3163 TEcont state.text
3166 class type lvsource = object
3167 method getitemcount : int
3168 method getitem : int -> (string * int)
3169 method hasaction : int -> bool
3170 method exit :
3171 uioh:uioh ->
3172 cancel:bool ->
3173 active:int ->
3174 first:int ->
3175 pan:int ->
3176 qsearch:string ->
3177 uioh option
3178 method getactive : int
3179 method getfirst : int
3180 method getqsearch : string
3181 method setqsearch : string -> unit
3182 method getpan : int
3183 end;;
3185 class virtual lvsourcebase = object
3186 val mutable m_active = 0
3187 val mutable m_first = 0
3188 val mutable m_qsearch = ""
3189 val mutable m_pan = 0
3190 method getactive = m_active
3191 method getfirst = m_first
3192 method getqsearch = m_qsearch
3193 method getpan = m_pan
3194 method setqsearch s = m_qsearch <- s
3195 end;;
3197 let withoutlastutf8 s =
3198 let len = String.length s in
3199 if len = 0
3200 then s
3201 else
3202 let rec find pos =
3203 if pos = 0
3204 then pos
3205 else
3206 let b = Char.code s.[pos] in
3207 if b land 0b11000000 = 0b11000000
3208 then pos
3209 else find (pos-1)
3211 let first =
3212 if Char.code s.[len-1] land 0x80 = 0
3213 then len-1
3214 else find (len-1)
3216 String.sub s 0 first;
3219 let textentrykeyboard
3220 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3221 let key =
3222 if key >= 0xffb0 && key <= 0xffb9
3223 then key - 0xffb0 + 48 else key
3225 let enttext te =
3226 state.mode <- Textentry (te, onleave);
3227 state.text <- "";
3228 enttext ();
3229 G.postRedisplay "textentrykeyboard enttext";
3231 let histaction cmd =
3232 match opthist with
3233 | None -> ()
3234 | Some (action, _) ->
3235 state.mode <- Textentry (
3236 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3238 G.postRedisplay "textentry histaction"
3240 match key with
3241 | 0xff08 -> (* backspace *)
3242 let s = withoutlastutf8 text in
3243 let len = String.length s in
3244 if cancelonempty && len = 0
3245 then (
3246 onleave Cancel;
3247 G.postRedisplay "textentrykeyboard after cancel";
3249 else (
3250 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3253 | 0xff0d | 0xff8d -> (* (kp) enter *)
3254 ondone text;
3255 onleave Confirm;
3256 G.postRedisplay "textentrykeyboard after confirm"
3258 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3259 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3260 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3261 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3263 | 0xff1b -> (* escape*)
3264 if String.length text = 0
3265 then (
3266 begin match opthist with
3267 | None -> ()
3268 | Some (_, onhistcancel) -> onhistcancel ()
3269 end;
3270 onleave Cancel;
3271 state.text <- "";
3272 G.postRedisplay "textentrykeyboard after cancel2"
3274 else (
3275 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3278 | 0xff9f | 0xffff -> () (* delete *)
3280 | _ when key != 0
3281 && key land 0xff00 != 0xff00 (* keyboard *)
3282 && key land 0xfe00 != 0xfe00 (* xkb *)
3283 && key land 0xfd00 != 0xfd00 (* 3270 *)
3285 begin match onkey text key with
3286 | TEdone text ->
3287 ondone text;
3288 onleave Confirm;
3289 G.postRedisplay "textentrykeyboard after confirm2";
3291 | TEcont text ->
3292 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3294 | TEstop ->
3295 onleave Cancel;
3296 G.postRedisplay "textentrykeyboard after cancel3"
3298 | TEswitch te ->
3299 state.mode <- Textentry (te, onleave);
3300 G.postRedisplay "textentrykeyboard switch";
3301 end;
3303 | _ ->
3304 vlog "unhandled key %s" (Wsi.keyname key)
3307 let firstof first active =
3308 if first > active || abs (first - active) > fstate.maxrows - 1
3309 then max 0 (active - (fstate.maxrows/2))
3310 else first
3313 let calcfirst first active =
3314 if active > first
3315 then
3316 let rows = active - first in
3317 if rows > fstate.maxrows then active - fstate.maxrows else first
3318 else active
3321 let scrollph y maxy =
3322 let sh = float (maxy + state.winh) /. float state.winh in
3323 let sh = float state.winh /. sh in
3324 let sh = max sh (float conf.scrollh) in
3326 let percent = float y /. float maxy in
3327 let position = (float state.winh -. sh) *. percent in
3329 let position =
3330 if position +. sh > float state.winh
3331 then float state.winh -. sh
3332 else position
3334 position, sh;
3337 let coe s = (s :> uioh);;
3339 class listview ~(source:lvsource) ~trusted ~modehash =
3340 object (self)
3341 val m_pan = source#getpan
3342 val m_first = source#getfirst
3343 val m_active = source#getactive
3344 val m_qsearch = source#getqsearch
3345 val m_prev_uioh = state.uioh
3347 method private elemunder y =
3348 let n = y / (fstate.fontsize+1) in
3349 if m_first + n < source#getitemcount
3350 then (
3351 if source#hasaction (m_first + n)
3352 then Some (m_first + n)
3353 else None
3355 else None
3357 method display =
3358 Gl.enable `blend;
3359 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3360 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3361 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3362 GlDraw.color (1., 1., 1.);
3363 Gl.enable `texture_2d;
3364 let fs = fstate.fontsize in
3365 let nfs = fs + 1 in
3366 let ww = fstate.wwidth in
3367 let tabw = 30.0*.ww in
3368 let itemcount = source#getitemcount in
3369 let rec loop row =
3370 if (row - m_first) > fstate.maxrows
3371 then ()
3372 else (
3373 if row >= 0 && row < itemcount
3374 then (
3375 let (s, level) = source#getitem row in
3376 let y = (row - m_first) * nfs in
3377 let x = 5.0 +. float (level + m_pan) *. ww in
3378 if row = m_active
3379 then (
3380 Gl.disable `texture_2d;
3381 GlDraw.polygon_mode `both `line;
3382 let alpha = if source#hasaction row then 0.9 else 0.3 in
3383 GlDraw.color (1., 1., 1.) ~alpha;
3384 GlDraw.rect (1., float (y + 1))
3385 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3386 GlDraw.polygon_mode `both `fill;
3387 GlDraw.color (1., 1., 1.);
3388 Gl.enable `texture_2d;
3391 let drawtabularstring s =
3392 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3393 if trusted
3394 then
3395 let tabpos = try String.index s '\t' with Not_found -> -1 in
3396 if tabpos > 0
3397 then
3398 let len = String.length s - tabpos - 1 in
3399 let s1 = String.sub s 0 tabpos
3400 and s2 = String.sub s (tabpos + 1) len in
3401 let nx = drawstr x s1 in
3402 let sw = nx -. x in
3403 let x = x +. (max tabw sw) in
3404 drawstr x s2
3405 else
3406 drawstr x s
3407 else
3408 drawstr x s
3410 let _ = drawtabularstring s in
3411 loop (row+1)
3415 loop m_first;
3416 Gl.disable `blend;
3417 Gl.disable `texture_2d;
3419 method updownlevel incr =
3420 let len = source#getitemcount in
3421 let curlevel =
3422 if m_active >= 0 && m_active < len
3423 then snd (source#getitem m_active)
3424 else -1
3426 let rec flow i =
3427 if i = len then i-1 else if i = -1 then 0 else
3428 let _, l = source#getitem i in
3429 if l != curlevel then i else flow (i+incr)
3431 let active = flow m_active in
3432 let first = calcfirst m_first active in
3433 G.postRedisplay "outline updownlevel";
3434 {< m_active = active; m_first = first >}
3436 method private key1 key mask =
3437 let set1 active first qsearch =
3438 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3440 let search active pattern incr =
3441 let active = if active = -1 then m_first else active in
3442 let dosearch re =
3443 let rec loop n =
3444 if n >= 0 && n < source#getitemcount
3445 then (
3446 let s, _ = source#getitem n in
3448 (try ignore (Str.search_forward re s 0); true
3449 with Not_found -> false)
3450 then Some n
3451 else loop (n + incr)
3453 else None
3455 loop active
3458 let re = Str.regexp_case_fold pattern in
3459 dosearch re
3460 with Failure s ->
3461 state.text <- s;
3462 None
3464 let itemcount = source#getitemcount in
3465 let find start incr =
3466 let rec find i =
3467 if i = -1 || i = itemcount
3468 then -1
3469 else (
3470 if source#hasaction i
3471 then i
3472 else find (i + incr)
3475 find start
3477 let set active first =
3478 let first = bound first 0 (itemcount - fstate.maxrows) in
3479 state.text <- "";
3480 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3482 let navigate incr =
3483 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3484 let active, first =
3485 let incr1 = if incr > 0 then 1 else -1 in
3486 if isvisible m_first m_active
3487 then
3488 let next =
3489 let next = m_active + incr in
3490 let next =
3491 if next < 0 || next >= itemcount
3492 then -1
3493 else find next incr1
3495 if abs (m_active - next) > fstate.maxrows
3496 then -1
3497 else next
3499 if next = -1
3500 then
3501 let first = m_first + incr in
3502 let first = bound first 0 (itemcount - fstate.maxrows) in
3503 let next =
3504 let next = m_active + incr in
3505 let next = bound next 0 (itemcount - 1) in
3506 find next ~-incr1
3508 let active =
3509 if next = -1
3510 then m_active
3511 else (
3512 if isvisible first next
3513 then next
3514 else m_active
3517 active, first
3518 else
3519 let first = min next m_first in
3520 let first =
3521 if abs (next - first) > fstate.maxrows
3522 then first + incr
3523 else first
3525 next, first
3526 else
3527 let first = m_first + incr in
3528 let first = bound first 0 (itemcount - 1) in
3529 let active =
3530 let next = m_active + incr in
3531 let next = bound next 0 (itemcount - 1) in
3532 let next = find next incr1 in
3533 let active =
3534 if next = -1 || abs (m_active - first) > fstate.maxrows
3535 then (
3536 let active = if m_active = -1 then next else m_active in
3537 active
3539 else next
3541 if isvisible first active
3542 then active
3543 else -1
3545 active, first
3547 G.postRedisplay "listview navigate";
3548 set active first;
3550 match key with
3551 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3552 let incr = if key = 0x72 then -1 else 1 in
3553 let active, first =
3554 match search (m_active + incr) m_qsearch incr with
3555 | None ->
3556 state.text <- m_qsearch ^ " [not found]";
3557 m_active, m_first
3558 | Some active ->
3559 state.text <- m_qsearch;
3560 active, firstof m_first active
3562 G.postRedisplay "listview ctrl-r/s";
3563 set1 active first m_qsearch;
3565 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3566 if m_active >= 0 && m_active < source#getitemcount
3567 then (
3568 let s, _ = source#getitem m_active in
3569 selstring s;
3571 coe self
3573 | 0xff08 -> (* backspace *)
3574 if String.length m_qsearch = 0
3575 then coe self
3576 else (
3577 let qsearch = withoutlastutf8 m_qsearch in
3578 let len = String.length qsearch in
3579 if len = 0
3580 then (
3581 state.text <- "";
3582 G.postRedisplay "listview empty qsearch";
3583 set1 m_active m_first "";
3585 else
3586 let active, first =
3587 match search m_active qsearch ~-1 with
3588 | None ->
3589 state.text <- qsearch ^ " [not found]";
3590 m_active, m_first
3591 | Some active ->
3592 state.text <- qsearch;
3593 active, firstof m_first active
3595 G.postRedisplay "listview backspace qsearch";
3596 set1 active first qsearch
3599 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3600 let pattern = m_qsearch ^ toutf8 key in
3601 let active, first =
3602 match search m_active pattern 1 with
3603 | None ->
3604 state.text <- pattern ^ " [not found]";
3605 m_active, m_first
3606 | Some active ->
3607 state.text <- pattern;
3608 active, firstof m_first active
3610 G.postRedisplay "listview qsearch add";
3611 set1 active first pattern;
3613 | 0xff1b -> (* escape *)
3614 state.text <- "";
3615 if String.length m_qsearch = 0
3616 then (
3617 G.postRedisplay "list view escape";
3618 begin
3619 match
3620 source#exit (coe self) true m_active m_first m_pan m_qsearch
3621 with
3622 | None -> m_prev_uioh
3623 | Some uioh -> uioh
3626 else (
3627 G.postRedisplay "list view kill qsearch";
3628 source#setqsearch "";
3629 coe {< m_qsearch = "" >}
3632 | 0xff0d | 0xff8d -> (* (kp) enter *)
3633 state.text <- "";
3634 let self = {< m_qsearch = "" >} in
3635 source#setqsearch "";
3636 let opt =
3637 G.postRedisplay "listview enter";
3638 if m_active >= 0 && m_active < source#getitemcount
3639 then (
3640 source#exit (coe self) false m_active m_first m_pan "";
3642 else (
3643 source#exit (coe self) true m_active m_first m_pan "";
3646 begin match opt with
3647 | None -> m_prev_uioh
3648 | Some uioh -> uioh
3651 | 0xff9f | 0xffff -> (* (kp) delete *)
3652 coe self
3654 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3655 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3656 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3657 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3659 | 0xff53 | 0xff98 -> (* (kp) right *)
3660 state.text <- "";
3661 G.postRedisplay "listview right";
3662 coe {< m_pan = m_pan - 1 >}
3664 | 0xff51 | 0xff96 -> (* (kp) left *)
3665 state.text <- "";
3666 G.postRedisplay "listview left";
3667 coe {< m_pan = m_pan + 1 >}
3669 | 0xff50 | 0xff95 -> (* (kp) home *)
3670 let active = find 0 1 in
3671 G.postRedisplay "listview home";
3672 set active 0;
3674 | 0xff57 | 0xff9c -> (* (kp) end *)
3675 let first = max 0 (itemcount - fstate.maxrows) in
3676 let active = find (itemcount - 1) ~-1 in
3677 G.postRedisplay "listview end";
3678 set active first;
3680 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3681 coe self
3683 | _ ->
3684 dolog "listview unknown key %#x" key; coe self
3686 method key key mask =
3687 match state.mode with
3688 | Textentry te -> textentrykeyboard key mask te; coe self
3689 | _ -> self#key1 key mask
3691 method button button down x y _ =
3692 let opt =
3693 match button with
3694 | 1 when x > state.winw - conf.scrollbw ->
3695 G.postRedisplay "listview scroll";
3696 if down
3697 then
3698 let _, position, sh = self#scrollph in
3699 if y > truncate position && y < truncate (position +. sh)
3700 then (
3701 state.mstate <- Mscrolly;
3702 Some (coe self)
3704 else
3705 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3706 let first = truncate (s *. float source#getitemcount) in
3707 let first = min source#getitemcount first in
3708 Some (coe {< m_first = first; m_active = first >})
3709 else (
3710 state.mstate <- Mnone;
3711 Some (coe self);
3713 | 1 when not down ->
3714 begin match self#elemunder y with
3715 | Some n ->
3716 G.postRedisplay "listview click";
3717 source#exit
3718 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3719 | _ ->
3720 Some (coe self)
3722 | n when (n == 4 || n == 5) && not down ->
3723 let len = source#getitemcount in
3724 let first =
3725 if n = 5 && m_first + fstate.maxrows >= len
3726 then
3727 m_first
3728 else
3729 let first = m_first + (if n == 4 then -1 else 1) in
3730 bound first 0 (len - 1)
3732 G.postRedisplay "listview wheel";
3733 Some (coe {< m_first = first >})
3734 | n when (n = 6 || n = 7) && not down ->
3735 let inc = m_first + (if n = 7 then -1 else 1) in
3736 G.postRedisplay "listview hwheel";
3737 Some (coe {< m_pan = m_pan + inc >})
3738 | _ ->
3739 Some (coe self)
3741 match opt with
3742 | None -> m_prev_uioh
3743 | Some uioh -> uioh
3745 method motion _ y =
3746 match state.mstate with
3747 | Mscrolly ->
3748 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3749 let first = truncate (s *. float source#getitemcount) in
3750 let first = min source#getitemcount first in
3751 G.postRedisplay "listview motion";
3752 coe {< m_first = first; m_active = first >}
3753 | _ -> coe self
3755 method pmotion x y =
3756 if x < state.winw - conf.scrollbw
3757 then
3758 let n =
3759 match self#elemunder y with
3760 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3761 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3763 let o =
3764 if n != m_active
3765 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3766 else self
3768 coe o
3769 else (
3770 Wsi.setcursor Wsi.CURSOR_INHERIT;
3771 coe self
3774 method infochanged _ = ()
3776 method scrollpw = (0, 0.0, 0.0)
3777 method scrollph =
3778 let nfs = fstate.fontsize + 1 in
3779 let y = m_first * nfs in
3780 let itemcount = source#getitemcount in
3781 let maxi = max 0 (itemcount - fstate.maxrows) in
3782 let maxy = maxi * nfs in
3783 let p, h = scrollph y maxy in
3784 conf.scrollbw, p, h
3786 method modehash = modehash
3787 method eformsgs = false
3788 end;;
3790 class outlinelistview ~source =
3791 object (self)
3792 inherit listview
3793 ~source:(source :> lvsource)
3794 ~trusted:false
3795 ~modehash:(findkeyhash conf "outline")
3796 as super
3798 method key key mask =
3799 let calcfirst first active =
3800 if active > first
3801 then
3802 let rows = active - first in
3803 let maxrows =
3804 if String.length state.text = 0
3805 then fstate.maxrows
3806 else fstate.maxrows - 2
3808 if rows > maxrows then active - maxrows else first
3809 else active
3811 let navigate incr =
3812 let active = m_active + incr in
3813 let active = bound active 0 (source#getitemcount - 1) in
3814 let first = calcfirst m_first active in
3815 G.postRedisplay "outline navigate";
3816 coe {< m_active = active; m_first = first >}
3818 let ctrl = Wsi.withctrl mask in
3819 match key with
3820 | 110 when ctrl -> (* ctrl-n *)
3821 source#narrow m_qsearch;
3822 G.postRedisplay "outline ctrl-n";
3823 coe {< m_first = 0; m_active = 0 >}
3825 | 117 when ctrl -> (* ctrl-u *)
3826 source#denarrow;
3827 G.postRedisplay "outline ctrl-u";
3828 state.text <- "";
3829 coe {< m_first = 0; m_active = 0 >}
3831 | 108 when ctrl -> (* ctrl-l *)
3832 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3833 G.postRedisplay "outline ctrl-l";
3834 coe {< m_first = first >}
3836 | 0xff9f | 0xffff -> (* (kp) delete *)
3837 source#remove m_active;
3838 G.postRedisplay "outline delete";
3839 let active = max 0 (m_active-1) in
3840 coe {< m_first = firstof m_first active;
3841 m_active = active >}
3843 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3844 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3845 | 0xff55 | 0xff9a -> (* (kp) prior *)
3846 navigate ~-(fstate.maxrows)
3847 | 0xff56 | 0xff9b -> (* (kp) next *)
3848 navigate fstate.maxrows
3850 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3851 let o =
3852 if ctrl
3853 then (
3854 G.postRedisplay "outline ctrl right";
3855 {< m_pan = m_pan + 1 >}
3857 else self#updownlevel 1
3859 coe o
3861 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3862 let o =
3863 if ctrl
3864 then (
3865 G.postRedisplay "outline ctrl left";
3866 {< m_pan = m_pan - 1 >}
3868 else self#updownlevel ~-1
3870 coe o
3872 | 0xff50 | 0xff95 -> (* (kp) home *)
3873 G.postRedisplay "outline home";
3874 coe {< m_first = 0; m_active = 0 >}
3876 | 0xff57 | 0xff9c -> (* (kp) end *)
3877 let active = source#getitemcount - 1 in
3878 let first = max 0 (active - fstate.maxrows) in
3879 G.postRedisplay "outline end";
3880 coe {< m_active = active; m_first = first >}
3882 | _ -> super#key key mask
3885 let outlinesource usebookmarks =
3886 let empty = [||] in
3887 (object
3888 inherit lvsourcebase
3889 val mutable m_items = empty
3890 val mutable m_orig_items = empty
3891 val mutable m_prev_items = empty
3892 val mutable m_narrow_pattern = ""
3893 val mutable m_hadremovals = false
3895 method getitemcount =
3896 Array.length m_items + (if m_hadremovals then 1 else 0)
3898 method getitem n =
3899 if n == Array.length m_items && m_hadremovals
3900 then
3901 ("[Confirm removal]", 0)
3902 else
3903 let s, n, _ = m_items.(n) in
3904 (s, n)
3906 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3907 ignore (uioh, first, qsearch);
3908 let confrimremoval = m_hadremovals && active = Array.length m_items in
3909 let items =
3910 if String.length m_narrow_pattern = 0
3911 then m_orig_items
3912 else m_items
3914 if not cancel
3915 then (
3916 if not confrimremoval
3917 then(
3918 let _, _, anchor = m_items.(active) in
3919 gotoghyll (getanchory anchor);
3920 m_items <- items;
3922 else (
3923 state.bookmarks <- Array.to_list m_items;
3924 m_orig_items <- m_items;
3927 else m_items <- items;
3928 m_pan <- pan;
3929 None
3931 method hasaction _ = true
3933 method greetmsg =
3934 if Array.length m_items != Array.length m_orig_items
3935 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3936 else ""
3938 method narrow pattern =
3939 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3940 match reopt with
3941 | None -> ()
3942 | Some re ->
3943 let rec loop accu n =
3944 if n = -1
3945 then (
3946 m_narrow_pattern <- pattern;
3947 m_items <- Array.of_list accu
3949 else
3950 let (s, _, _) as o = m_items.(n) in
3951 let accu =
3952 if (try ignore (Str.search_forward re s 0); true
3953 with Not_found -> false)
3954 then o :: accu
3955 else accu
3957 loop accu (n-1)
3959 loop [] (Array.length m_items - 1)
3961 method denarrow =
3962 m_orig_items <- (
3963 if usebookmarks
3964 then Array.of_list state.bookmarks
3965 else state.outlines
3967 m_items <- m_orig_items
3969 method remove m =
3970 if usebookmarks
3971 then
3972 if m >= 0 && m < Array.length m_items
3973 then (
3974 m_hadremovals <- true;
3975 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3976 let n = if n >= m then n+1 else n in
3977 m_items.(n)
3981 method reset anchor items =
3982 m_hadremovals <- false;
3983 if m_orig_items == empty || m_prev_items != items
3984 then (
3985 m_orig_items <- items;
3986 if String.length m_narrow_pattern = 0
3987 then m_items <- items;
3989 m_prev_items <- items;
3990 let rely = getanchory anchor in
3991 let active =
3992 let rec loop n best bestd =
3993 if n = Array.length m_items
3994 then best
3995 else
3996 let (_, _, anchor) = m_items.(n) in
3997 let orely = getanchory anchor in
3998 let d = abs (orely - rely) in
3999 if d < bestd
4000 then loop (n+1) n d
4001 else loop (n+1) best bestd
4003 loop 0 ~-1 max_int
4005 m_active <- active;
4006 m_first <- firstof m_first active
4007 end)
4010 let enterselector usebookmarks =
4011 let source = outlinesource usebookmarks in
4012 fun errmsg ->
4013 let outlines =
4014 if usebookmarks
4015 then Array.of_list state.bookmarks
4016 else state.outlines
4018 if Array.length outlines = 0
4019 then (
4020 showtext ' ' errmsg;
4022 else (
4023 state.text <- source#greetmsg;
4024 Wsi.setcursor Wsi.CURSOR_INHERIT;
4025 let anchor = getanchor () in
4026 source#reset anchor outlines;
4027 state.uioh <- coe (new outlinelistview ~source);
4028 G.postRedisplay "enter selector";
4032 let enteroutlinemode =
4033 let f = enterselector false in
4034 fun ()-> f "Document has no outline";
4037 let enterbookmarkmode =
4038 let f = enterselector true in
4039 fun () -> f "Document has no bookmarks (yet)";
4042 let color_of_string s =
4043 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4044 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4048 let color_to_string (r, g, b) =
4049 let r = truncate (r *. 256.0)
4050 and g = truncate (g *. 256.0)
4051 and b = truncate (b *. 256.0) in
4052 Printf.sprintf "%d/%d/%d" r g b
4055 let irect_of_string s =
4056 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4059 let irect_to_string (x0,y0,x1,y1) =
4060 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4063 let makecheckers () =
4064 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4065 following to say:
4066 converted by Issac Trotts. July 25, 2002 *)
4067 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4068 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4069 let id = GlTex.gen_texture () in
4070 GlTex.bind_texture `texture_2d id;
4071 GlPix.store (`unpack_alignment 1);
4072 GlTex.image2d image;
4073 List.iter (GlTex.parameter ~target:`texture_2d)
4074 [ `mag_filter `nearest; `min_filter `nearest ];
4078 let setcheckers enabled =
4079 match state.texid with
4080 | None ->
4081 if enabled then state.texid <- Some (makecheckers ())
4083 | Some texid ->
4084 if not enabled
4085 then (
4086 GlTex.delete_texture texid;
4087 state.texid <- None;
4091 let int_of_string_with_suffix s =
4092 let l = String.length s in
4093 let s1, shift =
4094 if l > 1
4095 then
4096 let suffix = Char.lowercase s.[l-1] in
4097 match suffix with
4098 | 'k' -> String.sub s 0 (l-1), 10
4099 | 'm' -> String.sub s 0 (l-1), 20
4100 | 'g' -> String.sub s 0 (l-1), 30
4101 | _ -> s, 0
4102 else s, 0
4104 let n = int_of_string s1 in
4105 let m = n lsl shift in
4106 if m < 0 || m < n
4107 then raise (Failure "value too large")
4108 else m
4111 let string_with_suffix_of_int n =
4112 if n = 0
4113 then "0"
4114 else
4115 let n, s =
4116 if n land ((1 lsl 30) - 1) = 0
4117 then n lsr 30, "G"
4118 else (
4119 if n land ((1 lsl 20) - 1) = 0
4120 then n lsr 20, "M"
4121 else (
4122 if n land ((1 lsl 10) - 1) = 0
4123 then n lsr 10, "K"
4124 else n, ""
4128 let rec loop s n =
4129 let h = n mod 1000 in
4130 let n = n / 1000 in
4131 if n = 0
4132 then string_of_int h ^ s
4133 else (
4134 let s = Printf.sprintf "_%03d%s" h s in
4135 loop s n
4138 loop "" n ^ s;
4141 let defghyllscroll = (40, 8, 32);;
4142 let ghyllscroll_of_string s =
4143 let (n, a, b) as nab =
4144 if s = "default"
4145 then defghyllscroll
4146 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4148 if n <= a || n <= b || a >= b
4149 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4150 nab;
4153 let ghyllscroll_to_string ((n, a, b) as nab) =
4154 if nab = defghyllscroll
4155 then "default"
4156 else Printf.sprintf "%d,%d,%d" n a b;
4159 let describe_location () =
4160 let fn = page_of_y state.y in
4161 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4162 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4163 let percent =
4164 if maxy <= 0
4165 then 100.
4166 else (100. *. (float state.y /. float maxy))
4168 if fn = ln
4169 then
4170 Printf.sprintf "page %d of %d [%.2f%%]"
4171 (fn+1) state.pagecount percent
4172 else
4173 Printf.sprintf
4174 "pages %d-%d of %d [%.2f%%]"
4175 (fn+1) (ln+1) state.pagecount percent
4178 let setpresentationmode v =
4179 let n = page_of_y state.y in
4180 state.anchor <- (n, 0.0, 1.0);
4181 conf.presentation <- v;
4182 if conf.fitmodel = FitPage
4183 then reqlayout conf.angle conf.fitmodel;
4184 represent ();
4187 let enterinfomode =
4188 let btos b = if b then "\xe2\x88\x9a" else "" in
4189 let showextended = ref false in
4190 let leave mode = function
4191 | Confirm -> state.mode <- mode
4192 | Cancel -> state.mode <- mode in
4193 let src =
4194 (object
4195 val mutable m_first_time = true
4196 val mutable m_l = []
4197 val mutable m_a = [||]
4198 val mutable m_prev_uioh = nouioh
4199 val mutable m_prev_mode = View
4201 inherit lvsourcebase
4203 method reset prev_mode prev_uioh =
4204 m_a <- Array.of_list (List.rev m_l);
4205 m_l <- [];
4206 m_prev_mode <- prev_mode;
4207 m_prev_uioh <- prev_uioh;
4208 if m_first_time
4209 then (
4210 let rec loop n =
4211 if n >= Array.length m_a
4212 then ()
4213 else
4214 match m_a.(n) with
4215 | _, _, _, Action _ -> m_active <- n
4216 | _ -> loop (n+1)
4218 loop 0;
4219 m_first_time <- false;
4222 method int name get set =
4223 m_l <-
4224 (name, `int get, 1, Action (
4225 fun u ->
4226 let ondone s =
4227 try set (int_of_string s)
4228 with exn ->
4229 state.text <- Printf.sprintf "bad integer `%s': %s"
4230 s (exntos exn)
4232 state.text <- "";
4233 let te = name ^ ": ", "", None, intentry, ondone, true in
4234 state.mode <- Textentry (te, leave m_prev_mode);
4236 )) :: m_l
4238 method int_with_suffix name get set =
4239 m_l <-
4240 (name, `intws get, 1, Action (
4241 fun u ->
4242 let ondone s =
4243 try set (int_of_string_with_suffix s)
4244 with exn ->
4245 state.text <- Printf.sprintf "bad integer `%s': %s"
4246 s (exntos exn)
4248 state.text <- "";
4249 let te =
4250 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4252 state.mode <- Textentry (te, leave m_prev_mode);
4254 )) :: m_l
4256 method bool ?(offset=1) ?(btos=btos) name get set =
4257 m_l <-
4258 (name, `bool (btos, get), offset, Action (
4259 fun u ->
4260 let v = get () in
4261 set (not v);
4263 )) :: m_l
4265 method color name get set =
4266 m_l <-
4267 (name, `color get, 1, Action (
4268 fun u ->
4269 let invalid = (nan, nan, nan) in
4270 let ondone s =
4271 let c =
4272 try color_of_string s
4273 with exn ->
4274 state.text <- Printf.sprintf "bad color `%s': %s"
4275 s (exntos exn);
4276 invalid
4278 if c <> invalid
4279 then set c;
4281 let te = name ^ ": ", "", None, textentry, ondone, true in
4282 state.text <- color_to_string (get ());
4283 state.mode <- Textentry (te, leave m_prev_mode);
4285 )) :: m_l
4287 method string name get set =
4288 m_l <-
4289 (name, `string get, 1, Action (
4290 fun u ->
4291 let ondone s = set s in
4292 let te = name ^ ": ", "", None, textentry, ondone, true in
4293 state.mode <- Textentry (te, leave m_prev_mode);
4295 )) :: m_l
4297 method colorspace name get set =
4298 m_l <-
4299 (name, `string get, 1, Action (
4300 fun _ ->
4301 let source =
4302 let vals = [| "rgb"; "bgr"; "gray" |] in
4303 (object
4304 inherit lvsourcebase
4306 initializer
4307 m_active <- int_of_colorspace conf.colorspace;
4308 m_first <- 0;
4310 method getitemcount = Array.length vals
4311 method getitem n = (vals.(n), 0)
4312 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4313 ignore (uioh, first, pan, qsearch);
4314 if not cancel then set active;
4315 None
4316 method hasaction _ = true
4317 end)
4319 state.text <- "";
4320 let modehash = findkeyhash conf "info" in
4321 coe (new listview ~source ~trusted:true ~modehash)
4322 )) :: m_l
4324 method fitmodel name get set =
4325 m_l <-
4326 (name, `string get, 1, Action (
4327 fun _ ->
4328 let source =
4329 let vals = [| "fit width"; "proportional"; "fit page" |] in
4330 (object
4331 inherit lvsourcebase
4333 initializer
4334 m_active <- int_of_fitmodel conf.fitmodel;
4335 m_first <- 0;
4337 method getitemcount = Array.length vals
4338 method getitem n = (vals.(n), 0)
4339 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4340 ignore (uioh, first, pan, qsearch);
4341 if not cancel then set active;
4342 None
4343 method hasaction _ = true
4344 end)
4346 state.text <- "";
4347 let modehash = findkeyhash conf "info" in
4348 coe (new listview ~source ~trusted:true ~modehash)
4349 )) :: m_l
4351 method caption s offset =
4352 m_l <- (s, `empty, offset, Noaction) :: m_l
4354 method caption2 s f offset =
4355 m_l <- (s, `string f, offset, Noaction) :: m_l
4357 method getitemcount = Array.length m_a
4359 method getitem n =
4360 let tostr = function
4361 | `int f -> string_of_int (f ())
4362 | `intws f -> string_with_suffix_of_int (f ())
4363 | `string f -> f ()
4364 | `color f -> color_to_string (f ())
4365 | `bool (btos, f) -> btos (f ())
4366 | `empty -> ""
4368 let name, t, offset, _ = m_a.(n) in
4369 ((let s = tostr t in
4370 if String.length s > 0
4371 then Printf.sprintf "%s\t%s" name s
4372 else name),
4373 offset)
4375 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4376 let uiohopt =
4377 if not cancel
4378 then (
4379 m_qsearch <- qsearch;
4380 let uioh =
4381 match m_a.(active) with
4382 | _, _, _, Action f -> f uioh
4383 | _ -> uioh
4385 Some uioh
4387 else None
4389 m_active <- active;
4390 m_first <- first;
4391 m_pan <- pan;
4392 uiohopt
4394 method hasaction n =
4395 match m_a.(n) with
4396 | _, _, _, Action _ -> true
4397 | _ -> false
4398 end)
4400 let rec fillsrc prevmode prevuioh =
4401 let sep () = src#caption "" 0 in
4402 let colorp name get set =
4403 src#string name
4404 (fun () -> color_to_string (get ()))
4405 (fun v ->
4407 let c = color_of_string v in
4408 set c
4409 with exn ->
4410 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4413 let oldmode = state.mode in
4414 let birdseye = isbirdseye state.mode in
4416 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4418 src#bool "presentation mode"
4419 (fun () -> conf.presentation)
4420 (fun v -> setpresentationmode v);
4422 src#bool "ignore case in searches"
4423 (fun () -> conf.icase)
4424 (fun v -> conf.icase <- v);
4426 src#bool "preload"
4427 (fun () -> conf.preload)
4428 (fun v -> conf.preload <- v);
4430 src#bool "highlight links"
4431 (fun () -> conf.hlinks)
4432 (fun v -> conf.hlinks <- v);
4434 src#bool "under info"
4435 (fun () -> conf.underinfo)
4436 (fun v -> conf.underinfo <- v);
4438 src#bool "persistent bookmarks"
4439 (fun () -> conf.savebmarks)
4440 (fun v -> conf.savebmarks <- v);
4442 src#fitmodel "fit model"
4443 (fun () -> fitmodel_to_string conf.fitmodel)
4444 (fun v -> reqlayout conf.angle (fitmodel_of_int v));
4446 src#bool "trim margins"
4447 (fun () -> conf.trimmargins)
4448 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4450 src#bool "persistent location"
4451 (fun () -> conf.jumpback)
4452 (fun v -> conf.jumpback <- v);
4454 sep ();
4455 src#int "inter-page space"
4456 (fun () -> conf.interpagespace)
4457 (fun n ->
4458 conf.interpagespace <- n;
4459 docolumns conf.columns;
4460 let pageno, py =
4461 match state.layout with
4462 | [] -> 0, 0
4463 | l :: _ ->
4464 l.pageno, l.pagey
4466 state.maxy <- calcheight ();
4467 let y = getpagey pageno in
4468 gotoy (y + py)
4471 src#int "page bias"
4472 (fun () -> conf.pagebias)
4473 (fun v -> conf.pagebias <- v);
4475 src#int "scroll step"
4476 (fun () -> conf.scrollstep)
4477 (fun n -> conf.scrollstep <- n);
4479 src#int "horizontal scroll step"
4480 (fun () -> conf.hscrollstep)
4481 (fun v -> conf.hscrollstep <- v);
4483 src#int "auto scroll step"
4484 (fun () ->
4485 match state.autoscroll with
4486 | Some step -> step
4487 | _ -> conf.autoscrollstep)
4488 (fun n ->
4489 if state.autoscroll <> None
4490 then state.autoscroll <- Some n;
4491 conf.autoscrollstep <- n);
4493 src#int "zoom"
4494 (fun () -> truncate (conf.zoom *. 100.))
4495 (fun v -> setzoom ((float v) /. 100.));
4497 src#int "rotation"
4498 (fun () -> conf.angle)
4499 (fun v -> reqlayout v conf.fitmodel);
4501 src#int "scroll bar width"
4502 (fun () -> conf.scrollbw)
4503 (fun v ->
4504 conf.scrollbw <- v;
4505 reshape state.winw state.winh;
4508 src#int "scroll handle height"
4509 (fun () -> conf.scrollh)
4510 (fun v -> conf.scrollh <- v;);
4512 src#int "thumbnail width"
4513 (fun () -> conf.thumbw)
4514 (fun v ->
4515 conf.thumbw <- min 4096 v;
4516 match oldmode with
4517 | Birdseye beye ->
4518 leavebirdseye beye false;
4519 enterbirdseye ()
4520 | _ -> ()
4523 let mode = state.mode in
4524 src#string "columns"
4525 (fun () ->
4526 match conf.columns with
4527 | Csingle _ -> "1"
4528 | Cmulti (multi, _) -> multicolumns_to_string multi
4529 | Csplit (count, _) -> "-" ^ string_of_int count
4531 (fun v ->
4532 let n, a, b = multicolumns_of_string v in
4533 setcolumns mode n a b);
4535 sep ();
4536 src#caption "Pixmap cache" 0;
4537 src#int_with_suffix "size (advisory)"
4538 (fun () -> conf.memlimit)
4539 (fun v -> conf.memlimit <- v);
4541 src#caption2 "used"
4542 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4543 (string_with_suffix_of_int state.memused)
4544 (Hashtbl.length state.tilemap)) 1;
4546 sep ();
4547 src#caption "Layout" 0;
4548 src#caption2 "Dimension"
4549 (fun () ->
4550 Printf.sprintf "%dx%d (virtual %dx%d)"
4551 state.winw state.winh
4552 state.w state.maxy)
4554 if conf.debug
4555 then
4556 src#caption2 "Position" (fun () ->
4557 Printf.sprintf "%dx%d" state.x state.y
4559 else
4560 src#caption2 "Position" (fun () -> describe_location ()) 1
4563 sep ();
4564 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4565 "Save these parameters as global defaults at exit"
4566 (fun () -> conf.bedefault)
4567 (fun v -> conf.bedefault <- v)
4570 sep ();
4571 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4572 src#bool ~offset:0 ~btos "Extended parameters"
4573 (fun () -> !showextended)
4574 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4575 if !showextended
4576 then (
4577 src#bool "checkers"
4578 (fun () -> conf.checkers)
4579 (fun v -> conf.checkers <- v; setcheckers v);
4580 src#bool "update cursor"
4581 (fun () -> conf.updatecurs)
4582 (fun v -> conf.updatecurs <- v);
4583 src#bool "verbose"
4584 (fun () -> conf.verbose)
4585 (fun v -> conf.verbose <- v);
4586 src#bool "invert colors"
4587 (fun () -> conf.invert)
4588 (fun v -> conf.invert <- v);
4589 src#bool "max fit"
4590 (fun () -> conf.maxhfit)
4591 (fun v -> conf.maxhfit <- v);
4592 src#bool "redirect stderr"
4593 (fun () -> conf.redirectstderr)
4594 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4595 src#string "uri launcher"
4596 (fun () -> conf.urilauncher)
4597 (fun v -> conf.urilauncher <- v);
4598 src#string "path launcher"
4599 (fun () -> conf.pathlauncher)
4600 (fun v -> conf.pathlauncher <- v);
4601 src#string "tile size"
4602 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4603 (fun v ->
4605 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4606 conf.tilew <- max 64 w;
4607 conf.tileh <- max 64 h;
4608 flushtiles ();
4609 with exn ->
4610 state.text <- Printf.sprintf "bad tile size `%s': %s"
4611 v (exntos exn)
4613 src#int "texture count"
4614 (fun () -> conf.texcount)
4615 (fun v ->
4616 if realloctexts v
4617 then conf.texcount <- v
4618 else showtext '!' " Failed to set texture count please retry later"
4620 src#int "slice height"
4621 (fun () -> conf.sliceheight)
4622 (fun v ->
4623 conf.sliceheight <- v;
4624 wcmd "sliceh %d" conf.sliceheight;
4626 src#int "anti-aliasing level"
4627 (fun () -> conf.aalevel)
4628 (fun v ->
4629 conf.aalevel <- bound v 0 8;
4630 state.anchor <- getanchor ();
4631 opendoc state.path state.password;
4633 src#string "page scroll scaling factor"
4634 (fun () -> string_of_float conf.pgscale)
4635 (fun v ->
4637 let s = float_of_string v in
4638 conf.pgscale <- s
4639 with exn ->
4640 state.text <- Printf.sprintf
4641 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4644 src#int "ui font size"
4645 (fun () -> fstate.fontsize)
4646 (fun v -> setfontsize (bound v 5 100));
4647 src#int "hint font size"
4648 (fun () -> conf.hfsize)
4649 (fun v -> conf.hfsize <- bound v 5 100);
4650 colorp "background color"
4651 (fun () -> conf.bgcolor)
4652 (fun v -> conf.bgcolor <- v);
4653 src#bool "crop hack"
4654 (fun () -> conf.crophack)
4655 (fun v -> conf.crophack <- v);
4656 src#string "trim fuzz"
4657 (fun () -> irect_to_string conf.trimfuzz)
4658 (fun v ->
4660 conf.trimfuzz <- irect_of_string v;
4661 if conf.trimmargins
4662 then settrim true conf.trimfuzz;
4663 with exn ->
4664 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4666 src#string "throttle"
4667 (fun () ->
4668 match conf.maxwait with
4669 | None -> "show place holder if page is not ready"
4670 | Some time ->
4671 if time = infinity
4672 then "wait for page to fully render"
4673 else
4674 "wait " ^ string_of_float time
4675 ^ " seconds before showing placeholder"
4677 (fun v ->
4679 let f = float_of_string v in
4680 if f <= 0.0
4681 then conf.maxwait <- None
4682 else conf.maxwait <- Some f
4683 with exn ->
4684 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4686 src#string "ghyll scroll"
4687 (fun () ->
4688 match conf.ghyllscroll with
4689 | None -> ""
4690 | Some nab -> ghyllscroll_to_string nab
4692 (fun v ->
4694 let gs =
4695 if String.length v = 0
4696 then None
4697 else Some (ghyllscroll_of_string v)
4699 conf.ghyllscroll <- gs
4700 with exn ->
4701 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4703 src#string "selection command"
4704 (fun () -> conf.selcmd)
4705 (fun v -> conf.selcmd <- v);
4706 src#string "synctex command"
4707 (fun () -> conf.stcmd)
4708 (fun v -> conf.stcmd <- v);
4709 src#colorspace "color space"
4710 (fun () -> colorspace_to_string conf.colorspace)
4711 (fun v ->
4712 conf.colorspace <- colorspace_of_int v;
4713 wcmd "cs %d" v;
4714 load state.layout;
4716 if pbousable ()
4717 then
4718 src#bool "use PBO"
4719 (fun () -> conf.usepbo)
4720 (fun v -> conf.usepbo <- v);
4721 src#bool "mouse wheel scrolls pages"
4722 (fun () -> conf.wheelbypage)
4723 (fun v -> conf.wheelbypage <- v);
4724 src#bool "open remote links in a new instance"
4725 (fun () -> conf.riani)
4726 (fun v -> conf.riani <- v);
4729 sep ();
4730 src#caption "Document" 0;
4731 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4732 src#caption2 "Pages"
4733 (fun () -> string_of_int state.pagecount) 1;
4734 src#caption2 "Dimensions"
4735 (fun () -> string_of_int (List.length state.pdims)) 1;
4736 if conf.trimmargins
4737 then (
4738 sep ();
4739 src#caption "Trimmed margins" 0;
4740 src#caption2 "Dimensions"
4741 (fun () -> string_of_int (List.length state.pdims)) 1;
4744 sep ();
4745 src#caption "OpenGL" 0;
4746 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4747 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4749 sep ();
4750 src#caption "Location" 0;
4751 if String.length state.origin > 0
4752 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4753 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4755 src#reset prevmode prevuioh;
4757 fun () ->
4758 state.text <- "";
4759 let prevmode = state.mode
4760 and prevuioh = state.uioh in
4761 fillsrc prevmode prevuioh;
4762 let source = (src :> lvsource) in
4763 let modehash = findkeyhash conf "info" in
4764 state.uioh <- coe (object (self)
4765 inherit listview ~source ~trusted:true ~modehash as super
4766 val mutable m_prevmemused = 0
4767 method infochanged = function
4768 | Memused ->
4769 if m_prevmemused != state.memused
4770 then (
4771 m_prevmemused <- state.memused;
4772 G.postRedisplay "memusedchanged";
4774 | Pdim -> G.postRedisplay "pdimchanged"
4775 | Docinfo -> fillsrc prevmode prevuioh
4777 method key key mask =
4778 if not (Wsi.withctrl mask)
4779 then
4780 match key with
4781 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4782 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4783 | _ -> super#key key mask
4784 else super#key key mask
4785 end);
4786 G.postRedisplay "info";
4789 let enterhelpmode =
4790 let source =
4791 (object
4792 inherit lvsourcebase
4793 method getitemcount = Array.length state.help
4794 method getitem n =
4795 let s, l, _ = state.help.(n) in
4796 (s, l)
4798 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4799 let optuioh =
4800 if not cancel
4801 then (
4802 m_qsearch <- qsearch;
4803 match state.help.(active) with
4804 | _, _, Action f -> Some (f uioh)
4805 | _ -> Some (uioh)
4807 else None
4809 m_active <- active;
4810 m_first <- first;
4811 m_pan <- pan;
4812 optuioh
4814 method hasaction n =
4815 match state.help.(n) with
4816 | _, _, Action _ -> true
4817 | _ -> false
4819 initializer
4820 m_active <- -1
4821 end)
4822 in fun () ->
4823 let modehash = findkeyhash conf "help" in
4824 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4825 G.postRedisplay "help";
4828 let entermsgsmode =
4829 let msgsource =
4830 let re = Str.regexp "[\r\n]" in
4831 (object
4832 inherit lvsourcebase
4833 val mutable m_items = [||]
4835 method getitemcount = 1 + Array.length m_items
4837 method getitem n =
4838 if n = 0
4839 then "[Clear]", 0
4840 else m_items.(n-1), 0
4842 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4843 ignore uioh;
4844 if not cancel
4845 then (
4846 if active = 0
4847 then Buffer.clear state.errmsgs;
4848 m_qsearch <- qsearch;
4850 m_active <- active;
4851 m_first <- first;
4852 m_pan <- pan;
4853 None
4855 method hasaction n =
4856 n = 0
4858 method reset =
4859 state.newerrmsgs <- false;
4860 let l = Str.split re (Buffer.contents state.errmsgs) in
4861 m_items <- Array.of_list l
4863 initializer
4864 m_active <- 0
4865 end)
4866 in fun () ->
4867 state.text <- "";
4868 msgsource#reset;
4869 let source = (msgsource :> lvsource) in
4870 let modehash = findkeyhash conf "listview" in
4871 state.uioh <- coe (object
4872 inherit listview ~source ~trusted:false ~modehash as super
4873 method display =
4874 if state.newerrmsgs
4875 then msgsource#reset;
4876 super#display
4877 end);
4878 G.postRedisplay "msgs";
4881 let quickbookmark ?title () =
4882 match state.layout with
4883 | [] -> ()
4884 | l :: _ ->
4885 let title =
4886 match title with
4887 | None ->
4888 let sec = Unix.gettimeofday () in
4889 let tm = Unix.localtime sec in
4890 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4891 (l.pageno+1)
4892 tm.Unix.tm_mday
4893 tm.Unix.tm_mon
4894 (tm.Unix.tm_year + 1900)
4895 tm.Unix.tm_hour
4896 tm.Unix.tm_min
4897 | Some title -> title
4899 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4902 let setautoscrollspeed step goingdown =
4903 let incr = max 1 ((abs step) / 2) in
4904 let incr = if goingdown then incr else -incr in
4905 let astep = step + incr in
4906 state.autoscroll <- Some astep;
4909 let gotounder = function
4910 | Ulinkgoto (pageno, top) ->
4911 if pageno >= 0
4912 then (
4913 addnav ();
4914 gotopage1 pageno top;
4917 | Ulinkuri s ->
4918 gotouri s
4920 | Uremote (filename, pageno) ->
4921 let path =
4922 if String.length filename > 0
4923 then
4924 if Filename.is_relative filename
4925 then
4926 let dir = Filename.dirname state.path in
4927 let dir =
4928 if Filename.is_implicit dir
4929 then Filename.concat (Sys.getcwd ()) dir
4930 else dir
4932 Filename.concat dir filename
4933 else filename
4934 else ""
4936 let path =
4937 if Sys.file_exists path
4938 then path
4939 else ""
4941 if String.length path > 0
4942 then (
4943 if conf.riani
4944 then
4945 let command = !selfexec ^ " " ^ path in
4946 try popen command []
4947 with exn ->
4948 Printf.eprintf
4949 "failed to execute `%s': %s\n" command (exntos exn);
4950 flush stderr;
4951 else
4952 let anchor = getanchor () in
4953 let ranchor = state.path, state.password, anchor, state.origin in
4954 state.origin <- "";
4955 state.anchor <- (pageno, 0.0, 0.0);
4956 state.ranchors <- ranchor :: state.ranchors;
4957 opendoc path "";
4959 else showtext '!' ("Could not find " ^ filename)
4961 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4964 let canpan () =
4965 match conf.columns with
4966 | Csplit _ -> true
4967 | _ -> state.x != 0 || conf.zoom > 1.0
4970 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4972 let existsinrow pageno (columns, coverA, coverB) p =
4973 let last = ((pageno - coverA) mod columns) + columns in
4974 let rec any = function
4975 | [] -> false
4976 | l :: rest ->
4977 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4978 then p l
4979 else (
4980 if not (p l)
4981 then (if l.pageno = last then false else any rest)
4982 else true
4985 any state.layout
4988 let nextpage () =
4989 match state.layout with
4990 | [] ->
4991 let pageno = page_of_y state.y in
4992 gotoghyll (getpagey (pageno+1))
4993 | l :: rest ->
4994 match conf.columns with
4995 | Csingle _ ->
4996 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4997 then
4998 let y = clamp (pgscale state.winh) in
4999 gotoghyll y
5000 else
5001 let pageno = min (l.pageno+1) (state.pagecount-1) in
5002 gotoghyll (getpagey pageno)
5003 | Cmulti ((c, _, _) as cl, _) ->
5004 if conf.presentation
5005 && (existsinrow l.pageno cl
5006 (fun l -> l.pageh > l.pagey + l.pagevh))
5007 then
5008 let y = clamp (pgscale state.winh) in
5009 gotoghyll y
5010 else
5011 let pageno = min (l.pageno+c) (state.pagecount-1) in
5012 gotoghyll (getpagey pageno)
5013 | Csplit (n, _) ->
5014 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5015 then
5016 let pagey, pageh = getpageyh l.pageno in
5017 let pagey = pagey + pageh * l.pagecol in
5018 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5019 gotoghyll (pagey + pageh + ips)
5022 let prevpage () =
5023 match state.layout with
5024 | [] ->
5025 let pageno = page_of_y state.y in
5026 gotoghyll (getpagey (pageno-1))
5027 | l :: _ ->
5028 match conf.columns with
5029 | Csingle _ ->
5030 if conf.presentation && l.pagey != 0
5031 then
5032 gotoghyll (clamp (pgscale ~-(state.winh)))
5033 else
5034 let pageno = max 0 (l.pageno-1) in
5035 gotoghyll (getpagey pageno)
5036 | Cmulti ((c, _, coverB) as cl, _) ->
5037 if conf.presentation &&
5038 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5039 then
5040 gotoghyll (clamp (pgscale ~-(state.winh)))
5041 else
5042 let decr =
5043 if l.pageno = state.pagecount - coverB
5044 then 1
5045 else c
5047 let pageno = max 0 (l.pageno-decr) in
5048 gotoghyll (getpagey pageno)
5049 | Csplit (n, _) ->
5050 let y =
5051 if l.pagecol = 0
5052 then
5053 if l.pageno = 0
5054 then l.pagey
5055 else
5056 let pageno = max 0 (l.pageno-1) in
5057 let pagey, pageh = getpageyh pageno in
5058 pagey + (n-1)*pageh
5059 else
5060 let pagey, pageh = getpageyh l.pageno in
5061 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5063 gotoghyll y
5066 let viewkeyboard key mask =
5067 let enttext te =
5068 let mode = state.mode in
5069 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5070 state.text <- "";
5071 enttext ();
5072 G.postRedisplay "view:enttext"
5074 let ctrl = Wsi.withctrl mask in
5075 let key =
5076 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5078 match key with
5079 | 81 -> (* Q *)
5080 exit 0
5082 | 0xff63 -> (* insert *)
5083 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5084 then (
5085 state.mode <- LinkNav (Ltgendir 0);
5086 gotoy state.y;
5088 else showtext '!' "Keyboard link navigation does not work under rotation"
5090 | 0xff1b | 113 -> (* escape / q *)
5091 begin match state.mstate with
5092 | Mzoomrect _ ->
5093 state.mstate <- Mnone;
5094 Wsi.setcursor Wsi.CURSOR_INHERIT;
5095 G.postRedisplay "kill zoom rect";
5096 | _ ->
5097 begin match state.mode with
5098 | LinkNav _ ->
5099 state.mode <- View;
5100 G.postRedisplay "esc leave linknav"
5101 | _ ->
5102 match state.ranchors with
5103 | [] -> raise Quit
5104 | (path, password, anchor, origin) :: rest ->
5105 state.ranchors <- rest;
5106 state.anchor <- anchor;
5107 state.origin <- origin;
5108 opendoc path password
5109 end;
5110 end;
5112 | 0xff08 -> (* backspace *)
5113 gotoghyll (getnav ~-1)
5115 | 111 -> (* o *)
5116 enteroutlinemode ()
5118 | 117 -> (* u *)
5119 state.rects <- [];
5120 state.text <- "";
5121 G.postRedisplay "dehighlight";
5123 | 47 | 63 -> (* / ? *)
5124 let ondone isforw s =
5125 cbput state.hists.pat s;
5126 state.searchpattern <- s;
5127 search s isforw
5129 let s = String.create 1 in
5130 s.[0] <- Char.chr key;
5131 enttext (s, "", Some (onhist state.hists.pat),
5132 textentry, ondone (key = 47), true)
5134 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5135 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5136 setzoom (conf.zoom +. incr)
5138 | 43 | 0xffab -> (* + *)
5139 let ondone s =
5140 let n =
5141 try int_of_string s with exc ->
5142 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5143 max_int
5145 if n != max_int
5146 then (
5147 conf.pagebias <- n;
5148 state.text <- "page bias is now " ^ string_of_int n;
5151 enttext ("page bias: ", "", None, intentry, ondone, true)
5153 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5154 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5155 setzoom (max 0.01 (conf.zoom -. decr))
5157 | 45 | 0xffad -> (* - *)
5158 let ondone msg = state.text <- msg in
5159 enttext (
5160 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
5161 optentry state.mode, ondone, true
5164 | 48 when ctrl -> (* ctrl-0 *)
5165 if conf.zoom = 1.0
5166 then (
5167 state.x <- 0;
5168 gotoy state.y
5170 else setzoom 1.0
5172 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5173 let cols =
5174 match conf.columns with
5175 | Csingle _ | Cmulti _ -> 1
5176 | Csplit (n, _) -> n
5178 let h = state.winh -
5179 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5181 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5182 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5183 then setzoom zoom
5185 | 51 when ctrl -> (* ctrl-3 *)
5186 let fm =
5187 match conf.fitmodel with
5188 | FitWidth -> FitProportional
5189 | FitProportional -> FitPage
5190 | FitPage -> FitWidth
5192 state.text <- "fit model: " ^ fitmodel_to_string fm;
5193 reqlayout conf.angle fm
5195 | 0xffc6 -> (* f9 *)
5196 togglebirdseye ()
5198 | 57 when ctrl -> (* ctrl-9 *)
5199 togglebirdseye ()
5201 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5202 when not ctrl -> (* 0..9 *)
5203 let ondone s =
5204 let n =
5205 try int_of_string s with exc ->
5206 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5209 if n >= 0
5210 then (
5211 addnav ();
5212 cbput state.hists.pag (string_of_int n);
5213 gotopage1 (n + conf.pagebias - 1) 0;
5216 let pageentry text key =
5217 match Char.unsafe_chr key with
5218 | 'g' -> TEdone text
5219 | _ -> intentry text key
5221 let text = "x" in text.[0] <- Char.chr key;
5222 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5224 | 98 -> (* b *)
5225 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5226 reshape state.winw state.winh;
5228 | 108 -> (* l *)
5229 conf.hlinks <- not conf.hlinks;
5230 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5231 G.postRedisplay "toggle highlightlinks";
5233 | 70 -> (* F *)
5234 state.glinks <- true;
5235 let mode = state.mode in
5236 state.mode <- Textentry (
5237 (":", "", None, linknentry, linkndone gotounder, false),
5238 (fun _ ->
5239 state.glinks <- false;
5240 state.mode <- mode)
5242 state.text <- "";
5243 G.postRedisplay "view:linkent(F)"
5245 | 121 -> (* y *)
5246 state.glinks <- true;
5247 let mode = state.mode in
5248 state.mode <- Textentry (
5250 ":", "", None, linknentry, linkndone (fun under ->
5251 selstring (undertext under);
5252 ), false
5254 fun _ ->
5255 state.glinks <- false;
5256 state.mode <- mode
5258 state.text <- "";
5259 G.postRedisplay "view:linkent"
5261 | 97 -> (* a *)
5262 begin match state.autoscroll with
5263 | Some step ->
5264 conf.autoscrollstep <- step;
5265 state.autoscroll <- None
5266 | None ->
5267 if conf.autoscrollstep = 0
5268 then state.autoscroll <- Some 1
5269 else state.autoscroll <- Some conf.autoscrollstep
5272 | 112 when ctrl -> (* ctrl-p *)
5273 launchpath ()
5275 | 80 -> (* P *)
5276 setpresentationmode (not conf.presentation);
5277 showtext ' ' ("presentation mode " ^
5278 if conf.presentation then "on" else "off");
5280 | 102 -> (* f *)
5281 if List.mem Wsi.Fullscreen state.winstate
5282 then Wsi.reshape conf.cwinw conf.cwinh
5283 else Wsi.fullscreen ()
5285 | 112 | 78 -> (* p|N *)
5286 search state.searchpattern false
5288 | 110 | 0xffc0 -> (* n|F3 *)
5289 search state.searchpattern true
5291 | 116 -> (* t *)
5292 begin match state.layout with
5293 | [] -> ()
5294 | l :: _ ->
5295 gotoghyll (getpagey l.pageno)
5298 | 32 -> (* space *)
5299 nextpage ()
5301 | 0xff9f | 0xffff -> (* delete *)
5302 prevpage ()
5304 | 61 -> (* = *)
5305 showtext ' ' (describe_location ());
5307 | 119 -> (* w *)
5308 begin match state.layout with
5309 | [] -> ()
5310 | l :: _ ->
5311 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5312 G.postRedisplay "w"
5315 | 39 -> (* ' *)
5316 enterbookmarkmode ()
5318 | 104 | 0xffbe -> (* h|F1 *)
5319 enterhelpmode ()
5321 | 105 -> (* i *)
5322 enterinfomode ()
5324 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5325 entermsgsmode ()
5327 | 109 -> (* m *)
5328 let ondone s =
5329 match state.layout with
5330 | l :: _ ->
5331 if String.length s > 0
5332 then
5333 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5334 | _ -> ()
5336 enttext ("bookmark: ", "", None, textentry, ondone, true)
5338 | 126 -> (* ~ *)
5339 quickbookmark ();
5340 showtext ' ' "Quick bookmark added";
5342 | 122 -> (* z *)
5343 begin match state.layout with
5344 | l :: _ ->
5345 let rect = getpdimrect l.pagedimno in
5346 let w, h =
5347 if conf.crophack
5348 then
5349 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5350 truncate (1.2 *. (rect.(3) -. rect.(0))))
5351 else
5352 (truncate (rect.(1) -. rect.(0)),
5353 truncate (rect.(3) -. rect.(0)))
5355 let w = truncate ((float w)*.conf.zoom)
5356 and h = truncate ((float h)*.conf.zoom) in
5357 if w != 0 && h != 0
5358 then (
5359 state.anchor <- getanchor ();
5360 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5362 G.postRedisplay "z";
5364 | [] -> ()
5367 | 60 | 62 -> (* < > *)
5368 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5370 | 91 | 93 -> (* [ ] *)
5371 conf.colorscale <-
5372 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5374 G.postRedisplay "brightness";
5376 | 99 when state.mode = View -> (* [alt]-c *)
5377 if Wsi.withalt mask
5378 then (
5379 if conf.zoom > 1.0
5380 then
5381 let m = (wadjsb state.winw - state.w) / 2 in
5382 state.x <- m;
5383 gotoy_and_clear_text state.y
5385 else
5386 let (c, a, b), z =
5387 match state.prevcolumns with
5388 | None -> (1, 0, 0), 1.0
5389 | Some (columns, z) ->
5390 let cab =
5391 match columns with
5392 | Csplit (c, _) -> -c, 0, 0
5393 | Cmulti ((c, a, b), _) -> c, a, b
5394 | Csingle _ -> 1, 0, 0
5396 cab, z
5398 setcolumns View c a b;
5399 setzoom z
5401 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5402 setzoom state.prevzoom
5404 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5405 begin match state.autoscroll with
5406 | None ->
5407 begin match state.mode with
5408 | Birdseye beye -> upbirdseye 1 beye
5409 | _ ->
5410 if ctrl
5411 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5412 else (
5413 if not (Wsi.withshift mask) && conf.presentation
5414 then prevpage ()
5415 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5418 | Some n ->
5419 setautoscrollspeed n false
5422 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5423 begin match state.autoscroll with
5424 | None ->
5425 begin match state.mode with
5426 | Birdseye beye -> downbirdseye 1 beye
5427 | _ ->
5428 if ctrl
5429 then gotoy_and_clear_text (clamp (state.winh/2))
5430 else (
5431 if not (Wsi.withshift mask) && conf.presentation
5432 then nextpage ()
5433 else gotoy_and_clear_text (clamp conf.scrollstep)
5436 | Some n ->
5437 setautoscrollspeed n true
5440 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5441 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5442 if canpan ()
5443 then
5444 let dx =
5445 if ctrl
5446 then state.winw / 2
5447 else conf.hscrollstep
5449 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5450 state.x <- panbound (state.x + dx);
5451 gotoy_and_clear_text state.y
5452 else (
5453 state.text <- "";
5454 G.postRedisplay "left/right"
5457 | 0xff55 | 0xff9a -> (* (kp) prior *)
5458 let y =
5459 if ctrl
5460 then
5461 match state.layout with
5462 | [] -> state.y
5463 | l :: _ -> state.y - l.pagey
5464 else
5465 clamp (pgscale (-state.winh))
5467 gotoghyll y
5469 | 0xff56 | 0xff9b -> (* (kp) next *)
5470 let y =
5471 if ctrl
5472 then
5473 match List.rev state.layout with
5474 | [] -> state.y
5475 | l :: _ -> getpagey l.pageno
5476 else
5477 clamp (pgscale state.winh)
5479 gotoghyll y
5481 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5482 gotoghyll 0
5483 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5484 gotoghyll (clamp state.maxy)
5486 | 0xff53 | 0xff98
5487 when Wsi.withalt mask -> (* alt-(kp) right *)
5488 gotoghyll (getnav 1)
5489 | 0xff51 | 0xff96
5490 when Wsi.withalt mask -> (* alt-(kp) left *)
5491 gotoghyll (getnav ~-1)
5493 | 114 -> (* r *)
5494 reload ()
5496 | 118 when conf.debug -> (* v *)
5497 state.rects <- [];
5498 List.iter (fun l ->
5499 match getopaque l.pageno with
5500 | None -> ()
5501 | Some opaque ->
5502 let x0, y0, x1, y1 = pagebbox opaque in
5503 let a,b = float x0, float y0 in
5504 let c,d = float x1, float y0 in
5505 let e,f = float x1, float y1 in
5506 let h,j = float x0, float y1 in
5507 let rect = (a,b,c,d,e,f,h,j) in
5508 debugrect rect;
5509 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5510 ) state.layout;
5511 G.postRedisplay "v";
5513 | _ ->
5514 vlog "huh? %s" (Wsi.keyname key)
5517 let linknavkeyboard key mask linknav =
5518 let getpage pageno =
5519 let rec loop = function
5520 | [] -> None
5521 | l :: _ when l.pageno = pageno -> Some l
5522 | _ :: rest -> loop rest
5523 in loop state.layout
5525 let doexact (pageno, n) =
5526 match getopaque pageno, getpage pageno with
5527 | Some opaque, Some l ->
5528 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5529 then
5530 let under = getlink opaque n in
5531 G.postRedisplay "link gotounder";
5532 gotounder under;
5533 state.mode <- View;
5534 else
5535 let opt, dir =
5536 match key with
5537 | 0xff50 -> (* home *)
5538 Some (findlink opaque LDfirst), -1
5540 | 0xff57 -> (* end *)
5541 Some (findlink opaque LDlast), 1
5543 | 0xff51 -> (* left *)
5544 Some (findlink opaque (LDleft n)), -1
5546 | 0xff53 -> (* right *)
5547 Some (findlink opaque (LDright n)), 1
5549 | 0xff52 -> (* up *)
5550 Some (findlink opaque (LDup n)), -1
5552 | 0xff54 -> (* down *)
5553 Some (findlink opaque (LDdown n)), 1
5555 | _ -> None, 0
5557 let pwl l dir =
5558 begin match findpwl l.pageno dir with
5559 | Pwlnotfound -> ()
5560 | Pwl pageno ->
5561 let notfound dir =
5562 state.mode <- LinkNav (Ltgendir dir);
5563 let y, h = getpageyh pageno in
5564 let y =
5565 if dir < 0
5566 then y + h - state.winh
5567 else y
5569 gotoy y
5571 begin match getopaque pageno, getpage pageno with
5572 | Some opaque, Some _ ->
5573 let link =
5574 let ld = if dir > 0 then LDfirst else LDlast in
5575 findlink opaque ld
5577 begin match link with
5578 | Lfound m ->
5579 showlinktype (getlink opaque m);
5580 state.mode <- LinkNav (Ltexact (pageno, m));
5581 G.postRedisplay "linknav jpage";
5582 | _ -> notfound dir
5583 end;
5584 | _ -> notfound dir
5585 end;
5586 end;
5588 begin match opt with
5589 | Some Lnotfound -> pwl l dir;
5590 | Some (Lfound m) ->
5591 if m = n
5592 then pwl l dir
5593 else (
5594 let _, y0, _, y1 = getlinkrect opaque m in
5595 if y0 < l.pagey
5596 then gotopage1 l.pageno y0
5597 else (
5598 let d = fstate.fontsize + 1 in
5599 if y1 - l.pagey > l.pagevh - d
5600 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5601 else G.postRedisplay "linknav";
5603 showlinktype (getlink opaque m);
5604 state.mode <- LinkNav (Ltexact (l.pageno, m));
5607 | None -> viewkeyboard key mask
5608 end;
5609 | _ -> viewkeyboard key mask
5611 if key = 0xff63
5612 then (
5613 state.mode <- View;
5614 G.postRedisplay "leave linknav"
5616 else
5617 match linknav with
5618 | Ltgendir _ -> viewkeyboard key mask
5619 | Ltexact exact -> doexact exact
5622 let keyboard key mask =
5623 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5624 then wcmd "interrupt"
5625 else state.uioh <- state.uioh#key key mask
5628 let birdseyekeyboard key mask
5629 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5630 let incr =
5631 match conf.columns with
5632 | Csingle _ -> 1
5633 | Cmulti ((c, _, _), _) -> c
5634 | Csplit _ -> failwith "bird's eye split mode"
5636 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5637 match key with
5638 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5639 let y, h = getpageyh pageno in
5640 let top = (state.winh - h) / 2 in
5641 gotoy (max 0 (y - top))
5642 | 0xff0d (* enter *)
5643 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5644 | 0xff1b -> leavebirdseye beye true (* escape *)
5645 | 0xff52 -> upbirdseye incr beye (* up *)
5646 | 0xff54 -> downbirdseye incr beye (* down *)
5647 | 0xff51 -> upbirdseye 1 beye (* left *)
5648 | 0xff53 -> downbirdseye 1 beye (* right *)
5650 | 0xff55 -> (* prior *)
5651 begin match state.layout with
5652 | l :: _ ->
5653 if l.pagey != 0
5654 then (
5655 state.mode <- Birdseye (
5656 oconf, leftx, l.pageno, hooverpageno, anchor
5658 gotopage1 l.pageno 0;
5660 else (
5661 let layout = layout (state.y-state.winh) (pgh state.layout) in
5662 match layout with
5663 | [] -> gotoy (clamp (-state.winh))
5664 | l :: _ ->
5665 state.mode <- Birdseye (
5666 oconf, leftx, l.pageno, hooverpageno, anchor
5668 gotopage1 l.pageno 0
5671 | [] -> gotoy (clamp (-state.winh))
5672 end;
5674 | 0xff56 -> (* next *)
5675 begin match List.rev state.layout with
5676 | l :: _ ->
5677 let layout = layout (state.y + (pgh state.layout)) state.winh in
5678 begin match layout with
5679 | [] ->
5680 let incr = l.pageh - l.pagevh in
5681 if incr = 0
5682 then (
5683 state.mode <-
5684 Birdseye (
5685 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5687 G.postRedisplay "birdseye pagedown";
5689 else gotoy (clamp (incr + conf.interpagespace*2));
5691 | l :: _ ->
5692 state.mode <-
5693 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5694 gotopage1 l.pageno 0;
5697 | [] -> gotoy (clamp state.winh)
5698 end;
5700 | 0xff50 -> (* home *)
5701 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5702 gotopage1 0 0
5704 | 0xff57 -> (* end *)
5705 let pageno = state.pagecount - 1 in
5706 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5707 if not (pagevisible state.layout pageno)
5708 then
5709 let h =
5710 match List.rev state.pdims with
5711 | [] -> state.winh
5712 | (_, _, h, _) :: _ -> h
5714 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5715 else G.postRedisplay "birdseye end";
5716 | _ -> viewkeyboard key mask
5719 let drawpage l =
5720 let color =
5721 match state.mode with
5722 | Textentry _ -> scalecolor 0.4
5723 | LinkNav _
5724 | View -> scalecolor 1.0
5725 | Birdseye (_, _, pageno, hooverpageno, _) ->
5726 if l.pageno = hooverpageno
5727 then scalecolor 0.9
5728 else (
5729 if l.pageno = pageno
5730 then scalecolor 1.0
5731 else scalecolor 0.8
5734 drawtiles l color;
5737 let postdrawpage l linkindexbase =
5738 match getopaque l.pageno with
5739 | Some opaque ->
5740 if tileready l l.pagex l.pagey
5741 then
5742 let x = l.pagedispx - l.pagex
5743 and y = l.pagedispy - l.pagey in
5744 let hlmask =
5745 match conf.columns with
5746 | Csingle _ | Cmulti _ ->
5747 (if conf.hlinks then 1 else 0)
5748 + (if state.glinks
5749 && not (isbirdseye state.mode) then 2 else 0)
5750 | _ -> 0
5752 let s =
5753 match state.mode with
5754 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5755 | _ -> ""
5757 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5758 else 0
5759 | _ -> 0
5762 let scrollindicator () =
5763 let sbw, ph, sh = state.uioh#scrollph in
5764 let sbh, pw, sw = state.uioh#scrollpw in
5766 GlDraw.color (0.64, 0.64, 0.64);
5767 GlDraw.rect
5768 (float (state.winw - sbw), 0.)
5769 (float state.winw, float state.winh)
5771 GlDraw.rect
5772 (0., float (state.winh - sbh))
5773 (float (wadjsb state.winw - 1), float state.winh)
5775 GlDraw.color (0.0, 0.0, 0.0);
5777 GlDraw.rect
5778 (float (state.winw - sbw), ph)
5779 (float state.winw, ph +. sh)
5781 GlDraw.rect
5782 (pw, float (state.winh - sbh))
5783 (pw +. sw, float state.winh)
5787 let showsel () =
5788 match state.mstate with
5789 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5792 | Msel ((x0, y0), (x1, y1)) ->
5793 let rec loop = function
5794 | l :: ls ->
5795 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5796 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5797 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5798 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5799 then
5800 match getopaque l.pageno with
5801 | Some opaque ->
5802 let x0, y0 = pagetranslatepoint l x0 y0 in
5803 let x1, y1 = pagetranslatepoint l x1 y1 in
5804 seltext opaque (x0, y0, x1, y1);
5805 | _ -> ()
5806 else loop ls
5807 | [] -> ()
5809 loop state.layout
5812 let showrects rects =
5813 Gl.enable `blend;
5814 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5815 GlDraw.polygon_mode `both `fill;
5816 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5817 List.iter
5818 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5819 List.iter (fun l ->
5820 if l.pageno = pageno
5821 then (
5822 let dx = float (l.pagedispx - l.pagex) in
5823 let dy = float (l.pagedispy - l.pagey) in
5824 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5825 GlDraw.begins `quads;
5827 GlDraw.vertex2 (x0+.dx, y0+.dy);
5828 GlDraw.vertex2 (x1+.dx, y1+.dy);
5829 GlDraw.vertex2 (x2+.dx, y2+.dy);
5830 GlDraw.vertex2 (x3+.dx, y3+.dy);
5832 GlDraw.ends ();
5834 ) state.layout
5835 ) rects
5837 Gl.disable `blend;
5840 let display () =
5841 GlClear.color (scalecolor2 conf.bgcolor);
5842 GlClear.clear [`color];
5843 List.iter drawpage state.layout;
5844 let rects =
5845 match state.mode with
5846 | LinkNav (Ltexact (pageno, linkno)) ->
5847 begin match getopaque pageno with
5848 | Some opaque ->
5849 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5850 (pageno, 5, (
5851 float x0, float y0,
5852 float x1, float y0,
5853 float x1, float y1,
5854 float x0, float y1)
5855 ) :: state.rects
5856 | None -> state.rects
5858 | _ -> state.rects
5860 showrects rects;
5861 let rec postloop linkindexbase = function
5862 | l :: rest ->
5863 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5864 postloop linkindexbase rest
5865 | [] -> ()
5867 showsel ();
5868 postloop 0 state.layout;
5869 state.uioh#display;
5870 begin match state.mstate with
5871 | Mzoomrect ((x0, y0), (x1, y1)) ->
5872 Gl.enable `blend;
5873 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5874 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5875 GlDraw.rect (float x0, float y0)
5876 (float x1, float y1);
5877 Gl.disable `blend;
5878 | _ -> ()
5879 end;
5880 enttext ();
5881 scrollindicator ();
5882 Wsi.swapb ();
5885 let zoomrect x y x1 y1 =
5886 let x0 = min x x1
5887 and x1 = max x x1
5888 and y0 = min y y1 in
5889 gotoy (state.y + y0);
5890 state.anchor <- getanchor ();
5891 let zoom = (float state.w) /. float (x1 - x0) in
5892 let margin =
5893 match conf.fitmodel, conf.columns with
5894 | FitPage, Csplit _ ->
5895 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5897 | _, _ ->
5898 let adjw = wadjsb state.winw in
5899 if state.w < adjw
5900 then (adjw - state.w) / 2
5901 else 0
5903 state.x <- (state.x + margin) - x0;
5904 setzoom zoom;
5905 Wsi.setcursor Wsi.CURSOR_INHERIT;
5906 state.mstate <- Mnone;
5909 let scrollx x =
5910 let winw = wadjsb state.winw - 1 in
5911 let s = float x /. float winw in
5912 let destx = truncate (float (state.w + winw) *. s) in
5913 state.x <- winw - destx;
5914 gotoy_and_clear_text state.y;
5915 state.mstate <- Mscrollx;
5918 let scrolly y =
5919 let s = float y /. float state.winh in
5920 let desty = truncate (float (state.maxy - state.winh) *. s) in
5921 gotoy_and_clear_text desty;
5922 state.mstate <- Mscrolly;
5925 let viewmouse button down x y mask =
5926 match button with
5927 | n when (n == 4 || n == 5) && not down ->
5928 if Wsi.withctrl mask
5929 then (
5930 match state.mstate with
5931 | Mzoom (oldn, i) ->
5932 if oldn = n
5933 then (
5934 if i = 2
5935 then
5936 let incr =
5937 match n with
5938 | 5 ->
5939 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5940 | _ ->
5941 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5943 let zoom = conf.zoom -. incr in
5944 setzoom zoom;
5945 state.mstate <- Mzoom (n, 0);
5946 else
5947 state.mstate <- Mzoom (n, i+1);
5949 else state.mstate <- Mzoom (n, 0)
5951 | _ -> state.mstate <- Mzoom (n, 0)
5953 else (
5954 match state.autoscroll with
5955 | Some step -> setautoscrollspeed step (n=4)
5956 | None ->
5957 if conf.wheelbypage || conf.presentation
5958 then (
5959 if n = 4
5960 then prevpage ()
5961 else nextpage ()
5963 else
5964 let incr =
5965 if n = 4
5966 then -conf.scrollstep
5967 else conf.scrollstep
5969 let incr = incr * 2 in
5970 let y = clamp incr in
5971 gotoy_and_clear_text y
5974 | n when (n = 6 || n = 7) && not down && canpan () ->
5975 state.x <-
5976 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5977 gotoy_and_clear_text state.y
5979 | 1 when Wsi.withshift mask ->
5980 state.mstate <- Mnone;
5981 if not down
5982 then (
5983 match unproject x y with
5984 | Some (pageno, ux, uy) ->
5985 let cmd = Printf.sprintf
5986 "%s %s %d %d %d"
5987 conf.stcmd state.path pageno ux uy
5989 popen cmd []
5990 | None -> ()
5993 | 1 when Wsi.withctrl mask ->
5994 if down
5995 then (
5996 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5997 state.mstate <- Mpan (x, y)
5999 else
6000 state.mstate <- Mnone
6002 | 3 ->
6003 if down
6004 then (
6005 Wsi.setcursor Wsi.CURSOR_CYCLE;
6006 let p = (x, y) in
6007 state.mstate <- Mzoomrect (p, p)
6009 else (
6010 match state.mstate with
6011 | Mzoomrect ((x0, y0), _) ->
6012 if abs (x-x0) > 10 && abs (y - y0) > 10
6013 then zoomrect x0 y0 x y
6014 else (
6015 state.mstate <- Mnone;
6016 Wsi.setcursor Wsi.CURSOR_INHERIT;
6017 G.postRedisplay "kill accidental zoom rect";
6019 | _ ->
6020 Wsi.setcursor Wsi.CURSOR_INHERIT;
6021 state.mstate <- Mnone
6024 | 1 when x > state.winw - vscrollw () ->
6025 if down
6026 then
6027 let _, position, sh = state.uioh#scrollph in
6028 if y > truncate position && y < truncate (position +. sh)
6029 then state.mstate <- Mscrolly
6030 else scrolly y
6031 else
6032 state.mstate <- Mnone
6034 | 1 when y > state.winh - hscrollh () ->
6035 if down
6036 then
6037 let _, position, sw = state.uioh#scrollpw in
6038 if x > truncate position && x < truncate (position +. sw)
6039 then state.mstate <- Mscrollx
6040 else scrollx x
6041 else
6042 state.mstate <- Mnone
6044 | 1 ->
6045 let dest = if down then getunder x y else Unone in
6046 begin match dest with
6047 | Ulinkgoto _
6048 | Ulinkuri _
6049 | Uremote _
6050 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6051 gotounder dest
6053 | Unone when down ->
6054 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6055 state.mstate <- Mpan (x, y);
6057 | Unone | Utext _ ->
6058 if down
6059 then (
6060 if conf.angle mod 360 = 0
6061 then (
6062 state.mstate <- Msel ((x, y), (x, y));
6063 G.postRedisplay "mouse select";
6066 else (
6067 match state.mstate with
6068 | Mnone -> ()
6070 | Mzoom _ | Mscrollx | Mscrolly ->
6071 state.mstate <- Mnone
6073 | Mzoomrect ((x0, y0), _) ->
6074 zoomrect x0 y0 x y
6076 | Mpan _ ->
6077 Wsi.setcursor Wsi.CURSOR_INHERIT;
6078 state.mstate <- Mnone
6080 | Msel ((x0, y0), (x1, y1)) ->
6081 let rec loop = function
6082 | [] -> ()
6083 | l :: rest ->
6084 let inside =
6085 let a0 = l.pagedispy in
6086 let a1 = a0 + l.pagevh in
6087 let b0 = l.pagedispx in
6088 let b1 = b0 + l.pagevw in
6089 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6090 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6092 if inside
6093 then
6094 match getopaque l.pageno with
6095 | Some opaque ->
6096 begin
6097 match Ne.pipe () with
6098 | Ne.Exn exn ->
6099 showtext '!'
6100 (Printf.sprintf
6101 "can not create sel pipe: %s"
6102 (exntos exn));
6103 | Ne.Res (r, w) ->
6104 let doclose what fd =
6105 Ne.clo fd (fun msg ->
6106 dolog "%s close failed: %s" what msg)
6109 popen conf.selcmd [r, 0; w, -1];
6110 copysel w opaque;
6111 doclose "pipe/r" r;
6112 G.postRedisplay "copysel";
6113 with exn ->
6114 dolog "can not execute %S: %s"
6115 conf.selcmd (exntos exn);
6116 doclose "pipe/r" r;
6117 doclose "pipe/w" w;
6119 | None -> ()
6120 else loop rest
6122 loop state.layout;
6123 Wsi.setcursor Wsi.CURSOR_INHERIT;
6124 state.mstate <- Mnone;
6128 | _ -> ()
6131 let birdseyemouse button down x y mask
6132 (conf, leftx, _, hooverpageno, anchor) =
6133 match button with
6134 | 1 when down ->
6135 let rec loop = function
6136 | [] -> ()
6137 | l :: rest ->
6138 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6139 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6140 then (
6141 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6143 else loop rest
6145 loop state.layout
6146 | 3 -> ()
6147 | _ -> viewmouse button down x y mask
6150 let mouse button down x y mask =
6151 state.uioh <- state.uioh#button button down x y mask;
6154 let motion ~x ~y =
6155 state.uioh <- state.uioh#motion x y
6158 let pmotion ~x ~y =
6159 state.uioh <- state.uioh#pmotion x y;
6162 let uioh = object
6163 method display = ()
6165 method key key mask =
6166 begin match state.mode with
6167 | Textentry textentry -> textentrykeyboard key mask textentry
6168 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6169 | View -> viewkeyboard key mask
6170 | LinkNav linknav -> linknavkeyboard key mask linknav
6171 end;
6172 state.uioh
6174 method button button bstate x y mask =
6175 begin match state.mode with
6176 | LinkNav _
6177 | View -> viewmouse button bstate x y mask
6178 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6179 | Textentry _ -> ()
6180 end;
6181 state.uioh
6183 method motion x y =
6184 begin match state.mode with
6185 | Textentry _ -> ()
6186 | View | Birdseye _ | LinkNav _ ->
6187 match state.mstate with
6188 | Mzoom _ | Mnone -> ()
6190 | Mpan (x0, y0) ->
6191 let dx = x - x0
6192 and dy = y0 - y in
6193 state.mstate <- Mpan (x, y);
6194 if canpan ()
6195 then state.x <- panbound (state.x + dx);
6196 let y = clamp dy in
6197 gotoy_and_clear_text y
6199 | Msel (a, _) ->
6200 state.mstate <- Msel (a, (x, y));
6201 G.postRedisplay "motion select";
6203 | Mscrolly ->
6204 let y = min state.winh (max 0 y) in
6205 scrolly y
6207 | Mscrollx ->
6208 let x = min state.winw (max 0 x) in
6209 scrollx x
6211 | Mzoomrect (p0, _) ->
6212 state.mstate <- Mzoomrect (p0, (x, y));
6213 G.postRedisplay "motion zoomrect";
6214 end;
6215 state.uioh
6217 method pmotion x y =
6218 begin match state.mode with
6219 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6220 let rec loop = function
6221 | [] ->
6222 if hooverpageno != -1
6223 then (
6224 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6225 G.postRedisplay "pmotion birdseye no hoover";
6227 | l :: rest ->
6228 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6229 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6230 then (
6231 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6232 G.postRedisplay "pmotion birdseye hoover";
6234 else loop rest
6236 loop state.layout
6238 | Textentry _ -> ()
6240 | LinkNav _
6241 | View ->
6242 match state.mstate with
6243 | Mnone -> updateunder x y
6244 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6246 end;
6247 state.uioh
6249 method infochanged _ = ()
6251 method scrollph =
6252 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6253 let p, h =
6254 if maxy = 0
6255 then 0.0, float state.winh
6256 else scrollph state.y maxy
6258 vscrollw (), p, h
6260 method scrollpw =
6261 let winw = wadjsb state.winw in
6262 let fwinw = float winw in
6263 let sw =
6264 let sw = fwinw /. float state.w in
6265 let sw = fwinw *. sw in
6266 max sw (float conf.scrollh)
6268 let position =
6269 let maxx = state.w + winw in
6270 let x = winw - state.x in
6271 let percent = float x /. float maxx in
6272 (fwinw -. sw) *. percent
6274 hscrollh (), position, sw
6276 method modehash =
6277 let modename =
6278 match state.mode with
6279 | LinkNav _ -> "links"
6280 | Textentry _ -> "textentry"
6281 | Birdseye _ -> "birdseye"
6282 | View -> "view"
6284 findkeyhash conf modename
6286 method eformsgs = true
6287 end;;
6289 module Config =
6290 struct
6291 open Parser
6293 let fontpath = ref "";;
6295 module KeyMap =
6296 Map.Make (struct type t = (int * int) let compare = compare end);;
6298 let unent s =
6299 let l = String.length s in
6300 let b = Buffer.create l in
6301 unent b s 0 l;
6302 Buffer.contents b;
6305 let home =
6306 try Sys.getenv "HOME"
6307 with exn ->
6308 prerr_endline
6309 ("Can not determine home directory location: " ^ exntos exn);
6313 let modifier_of_string = function
6314 | "alt" -> Wsi.altmask
6315 | "shift" -> Wsi.shiftmask
6316 | "ctrl" | "control" -> Wsi.ctrlmask
6317 | "meta" -> Wsi.metamask
6318 | _ -> 0
6321 let key_of_string =
6322 let r = Str.regexp "-" in
6323 fun s ->
6324 let elems = Str.full_split r s in
6325 let f n k m =
6326 let g s =
6327 let m1 = modifier_of_string s in
6328 if m1 = 0
6329 then (Wsi.namekey s, m)
6330 else (k, m lor m1)
6331 in function
6332 | Str.Delim s when n land 1 = 0 -> g s
6333 | Str.Text s -> g s
6334 | Str.Delim _ -> (k, m)
6336 let rec loop n k m = function
6337 | [] -> (k, m)
6338 | x :: xs ->
6339 let k, m = f n k m x in
6340 loop (n+1) k m xs
6342 loop 0 0 0 elems
6345 let keys_of_string =
6346 let r = Str.regexp "[ \t]" in
6347 fun s ->
6348 let elems = Str.split r s in
6349 List.map key_of_string elems
6352 let copykeyhashes c =
6353 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6356 let config_of c attrs =
6357 let apply c k v =
6359 match k with
6360 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6361 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6362 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6363 | "preload" -> { c with preload = bool_of_string v }
6364 | "page-bias" -> { c with pagebias = int_of_string v }
6365 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6366 | "horizontal-scroll-step" ->
6367 { c with hscrollstep = max (int_of_string v) 1 }
6368 | "auto-scroll-step" ->
6369 { c with autoscrollstep = max 0 (int_of_string v) }
6370 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6371 | "crop-hack" -> { c with crophack = bool_of_string v }
6372 | "throttle" ->
6373 let mw =
6374 match String.lowercase v with
6375 | "true" -> Some infinity
6376 | "false" -> None
6377 | f -> Some (float_of_string f)
6379 { c with maxwait = mw}
6380 | "highlight-links" -> { c with hlinks = bool_of_string v }
6381 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6382 | "vertical-margin" ->
6383 { c with interpagespace = max 0 (int_of_string v) }
6384 | "zoom" ->
6385 let zoom = float_of_string v /. 100. in
6386 let zoom = max zoom 0.0 in
6387 { c with zoom = zoom }
6388 | "presentation" -> { c with presentation = bool_of_string v }
6389 | "rotation-angle" -> { c with angle = int_of_string v }
6390 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6391 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6392 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6393 | "proportional-display" ->
6394 let fm =
6395 if bool_of_string v
6396 then FitProportional
6397 else FitWidth
6399 { c with fitmodel = fm }
6400 | "fit-model" -> { c with fitmodel = fitmodel_of_string v }
6401 | "pixmap-cache-size" ->
6402 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6403 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6404 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6405 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6406 | "persistent-location" -> { c with jumpback = bool_of_string v }
6407 | "background-color" -> { c with bgcolor = color_of_string v }
6408 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6409 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6410 | "mupdf-store-size" ->
6411 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6412 | "checkers" -> { c with checkers = bool_of_string v }
6413 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6414 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6415 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6416 | "uri-launcher" -> { c with urilauncher = unent v }
6417 | "path-launcher" -> { c with pathlauncher = unent v }
6418 | "color-space" -> { c with colorspace = colorspace_of_string v }
6419 | "invert-colors" -> { c with invert = bool_of_string v }
6420 | "brightness" -> { c with colorscale = float_of_string v }
6421 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6422 | "ghyllscroll" ->
6423 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6424 | "columns" ->
6425 let (n, _, _) as nab = multicolumns_of_string v in
6426 if n < 0
6427 then { c with columns = Csplit (-n, [||]) }
6428 else { c with columns = Cmulti (nab, [||]) }
6429 | "birds-eye-columns" ->
6430 { c with beyecolumns = Some (max (int_of_string v) 2) }
6431 | "selection-command" -> { c with selcmd = unent v }
6432 | "synctex-command" -> { c with stcmd = unent v }
6433 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6434 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6435 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6436 | "use-pbo" -> { c with usepbo = bool_of_string v }
6437 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6438 | "horizontal-scrollbar-visible" ->
6439 let b =
6440 if bool_of_string v
6441 then c.scrollb lor scrollbhv
6442 else c.scrollb land (lnot scrollbhv)
6444 { c with scrollb = b }
6445 | "vertical-scrollbar-visible" ->
6446 let b =
6447 if bool_of_string v
6448 then c.scrollb lor scrollbvv
6449 else c.scrollb land (lnot scrollbvv)
6451 { c with scrollb = b }
6452 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6453 | _ -> c
6454 with exn ->
6455 prerr_endline ("Error processing attribute (`" ^
6456 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6459 let rec fold c = function
6460 | [] -> c
6461 | (k, v) :: rest ->
6462 let c = apply c k v in
6463 fold c rest
6465 fold { c with keyhashes = copykeyhashes c } attrs;
6468 let fromstring f pos n v d =
6469 try f v
6470 with exn ->
6471 dolog "Error processing attribute (%S=%S) at %d\n%s"
6472 n v pos (exntos exn)
6477 let bookmark_of attrs =
6478 let rec fold title page rely visy = function
6479 | ("title", v) :: rest -> fold v page rely visy rest
6480 | ("page", v) :: rest -> fold title v rely visy rest
6481 | ("rely", v) :: rest -> fold title page v visy rest
6482 | ("visy", v) :: rest -> fold title page rely v rest
6483 | _ :: rest -> fold title page rely visy rest
6484 | [] -> title, page, rely, visy
6486 fold "invalid" "0" "0" "0" attrs
6489 let doc_of attrs =
6490 let rec fold path page rely pan visy = function
6491 | ("path", v) :: rest -> fold v page rely pan visy rest
6492 | ("page", v) :: rest -> fold path v rely pan visy rest
6493 | ("rely", v) :: rest -> fold path page v pan visy rest
6494 | ("pan", v) :: rest -> fold path page rely v visy rest
6495 | ("visy", v) :: rest -> fold path page rely pan v rest
6496 | _ :: rest -> fold path page rely pan visy rest
6497 | [] -> path, page, rely, pan, visy
6499 fold "" "0" "0" "0" "0" attrs
6502 let map_of attrs =
6503 let rec fold rs ls = function
6504 | ("out", v) :: rest -> fold v ls rest
6505 | ("in", v) :: rest -> fold rs v rest
6506 | _ :: rest -> fold ls rs rest
6507 | [] -> ls, rs
6509 fold "" "" attrs
6512 let setconf dst src =
6513 dst.scrollbw <- src.scrollbw;
6514 dst.scrollh <- src.scrollh;
6515 dst.icase <- src.icase;
6516 dst.preload <- src.preload;
6517 dst.pagebias <- src.pagebias;
6518 dst.verbose <- src.verbose;
6519 dst.scrollstep <- src.scrollstep;
6520 dst.maxhfit <- src.maxhfit;
6521 dst.crophack <- src.crophack;
6522 dst.autoscrollstep <- src.autoscrollstep;
6523 dst.maxwait <- src.maxwait;
6524 dst.hlinks <- src.hlinks;
6525 dst.underinfo <- src.underinfo;
6526 dst.interpagespace <- src.interpagespace;
6527 dst.zoom <- src.zoom;
6528 dst.presentation <- src.presentation;
6529 dst.angle <- src.angle;
6530 dst.cwinw <- src.cwinw;
6531 dst.cwinh <- src.cwinh;
6532 dst.savebmarks <- src.savebmarks;
6533 dst.memlimit <- src.memlimit;
6534 dst.fitmodel <- src.fitmodel;
6535 dst.texcount <- src.texcount;
6536 dst.sliceheight <- src.sliceheight;
6537 dst.thumbw <- src.thumbw;
6538 dst.jumpback <- src.jumpback;
6539 dst.bgcolor <- src.bgcolor;
6540 dst.tilew <- src.tilew;
6541 dst.tileh <- src.tileh;
6542 dst.mustoresize <- src.mustoresize;
6543 dst.checkers <- src.checkers;
6544 dst.aalevel <- src.aalevel;
6545 dst.trimmargins <- src.trimmargins;
6546 dst.trimfuzz <- src.trimfuzz;
6547 dst.urilauncher <- src.urilauncher;
6548 dst.colorspace <- src.colorspace;
6549 dst.invert <- src.invert;
6550 dst.colorscale <- src.colorscale;
6551 dst.redirectstderr <- src.redirectstderr;
6552 dst.ghyllscroll <- src.ghyllscroll;
6553 dst.columns <- src.columns;
6554 dst.beyecolumns <- src.beyecolumns;
6555 dst.selcmd <- src.selcmd;
6556 dst.updatecurs <- src.updatecurs;
6557 dst.pathlauncher <- src.pathlauncher;
6558 dst.keyhashes <- copykeyhashes src;
6559 dst.hfsize <- src.hfsize;
6560 dst.hscrollstep <- src.hscrollstep;
6561 dst.pgscale <- src.pgscale;
6562 dst.usepbo <- src.usepbo;
6563 dst.wheelbypage <- src.wheelbypage;
6564 dst.stcmd <- src.stcmd;
6565 dst.scrollb <- src.scrollb;
6566 dst.riani <- src.riani;
6569 let get s =
6570 let h = Hashtbl.create 10 in
6571 let dc = { defconf with angle = defconf.angle } in
6572 let rec toplevel v t spos _ =
6573 match t with
6574 | Vdata | Vcdata | Vend -> v
6575 | Vopen ("llppconfig", _, closed) ->
6576 if closed
6577 then v
6578 else { v with f = llppconfig }
6579 | Vopen _ ->
6580 error "unexpected subelement at top level" s spos
6581 | Vclose _ -> error "unexpected close at top level" s spos
6583 and llppconfig v t spos _ =
6584 match t with
6585 | Vdata | Vcdata -> v
6586 | Vend -> error "unexpected end of input in llppconfig" s spos
6587 | Vopen ("defaults", attrs, closed) ->
6588 let c = config_of dc attrs in
6589 setconf dc c;
6590 if closed
6591 then v
6592 else { v with f = defaults }
6594 | Vopen ("ui-font", attrs, closed) ->
6595 let rec getsize size = function
6596 | [] -> size
6597 | ("size", v) :: rest ->
6598 let size =
6599 fromstring int_of_string spos "size" v fstate.fontsize in
6600 getsize size rest
6601 | l -> getsize size l
6603 fstate.fontsize <- getsize fstate.fontsize attrs;
6604 if closed
6605 then v
6606 else { v with f = uifont (Buffer.create 10) }
6608 | Vopen ("doc", attrs, closed) ->
6609 let pathent, spage, srely, span, svisy = doc_of attrs in
6610 let path = unent pathent
6611 and pageno = fromstring int_of_string spos "page" spage 0
6612 and rely = fromstring float_of_string spos "rely" srely 0.0
6613 and pan = fromstring int_of_string spos "pan" span 0
6614 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6615 let c = config_of dc attrs in
6616 let anchor = (pageno, rely, visy) in
6617 if closed
6618 then (Hashtbl.add h path (c, [], pan, anchor); v)
6619 else { v with f = doc path pan anchor c [] }
6621 | Vopen _ ->
6622 error "unexpected subelement in llppconfig" s spos
6624 | Vclose "llppconfig" -> { v with f = toplevel }
6625 | Vclose _ -> error "unexpected close in llppconfig" s spos
6627 and defaults v t spos _ =
6628 match t with
6629 | Vdata | Vcdata -> v
6630 | Vend -> error "unexpected end of input in defaults" s spos
6631 | Vopen ("keymap", attrs, closed) ->
6632 let modename =
6633 try List.assoc "mode" attrs
6634 with Not_found -> "global" in
6635 if closed
6636 then v
6637 else
6638 let ret keymap =
6639 let h = findkeyhash dc modename in
6640 KeyMap.iter (Hashtbl.replace h) keymap;
6641 defaults
6643 { v with f = pkeymap ret KeyMap.empty }
6645 | Vopen (_, _, _) ->
6646 error "unexpected subelement in defaults" s spos
6648 | Vclose "defaults" ->
6649 { v with f = llppconfig }
6651 | Vclose _ -> error "unexpected close in defaults" s spos
6653 and uifont b v t spos epos =
6654 match t with
6655 | Vdata | Vcdata ->
6656 Buffer.add_substring b s spos (epos - spos);
6658 | Vopen (_, _, _) ->
6659 error "unexpected subelement in ui-font" s spos
6660 | Vclose "ui-font" ->
6661 if String.length !fontpath = 0
6662 then fontpath := Buffer.contents b;
6663 { v with f = llppconfig }
6664 | Vclose _ -> error "unexpected close in ui-font" s spos
6665 | Vend -> error "unexpected end of input in ui-font" s spos
6667 and doc path pan anchor c bookmarks v t spos _ =
6668 match t with
6669 | Vdata | Vcdata -> v
6670 | Vend -> error "unexpected end of input in doc" s spos
6671 | Vopen ("bookmarks", _, closed) ->
6672 if closed
6673 then v
6674 else { v with f = pbookmarks path pan anchor c bookmarks }
6676 | Vopen ("keymap", attrs, closed) ->
6677 let modename =
6678 try List.assoc "mode" attrs
6679 with Not_found -> "global"
6681 if closed
6682 then v
6683 else
6684 let ret keymap =
6685 let h = findkeyhash c modename in
6686 KeyMap.iter (Hashtbl.replace h) keymap;
6687 doc path pan anchor c bookmarks
6689 { v with f = pkeymap ret KeyMap.empty }
6691 | Vopen (_, _, _) ->
6692 error "unexpected subelement in doc" s spos
6694 | Vclose "doc" ->
6695 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6696 { v with f = llppconfig }
6698 | Vclose _ -> error "unexpected close in doc" s spos
6700 and pkeymap ret keymap v t spos _ =
6701 match t with
6702 | Vdata | Vcdata -> v
6703 | Vend -> error "unexpected end of input in keymap" s spos
6704 | Vopen ("map", attrs, closed) ->
6705 let r, l = map_of attrs in
6706 let kss = fromstring keys_of_string spos "in" r [] in
6707 let lss = fromstring keys_of_string spos "out" l [] in
6708 let keymap =
6709 match kss with
6710 | [] -> keymap
6711 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6712 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6714 if closed
6715 then { v with f = pkeymap ret keymap }
6716 else
6717 let f () = v in
6718 { v with f = skip "map" f }
6720 | Vopen _ ->
6721 error "unexpected subelement in keymap" s spos
6723 | Vclose "keymap" ->
6724 { v with f = ret keymap }
6726 | Vclose _ -> error "unexpected close in keymap" s spos
6728 and pbookmarks path pan anchor c bookmarks v t spos _ =
6729 match t with
6730 | Vdata | Vcdata -> v
6731 | Vend -> error "unexpected end of input in bookmarks" s spos
6732 | Vopen ("item", attrs, closed) ->
6733 let titleent, spage, srely, svisy = bookmark_of attrs in
6734 let page = fromstring int_of_string spos "page" spage 0
6735 and rely = fromstring float_of_string spos "rely" srely 0.0
6736 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6737 let bookmarks =
6738 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6740 if closed
6741 then { v with f = pbookmarks path pan anchor c bookmarks }
6742 else
6743 let f () = v in
6744 { v with f = skip "item" f }
6746 | Vopen _ ->
6747 error "unexpected subelement in bookmarks" s spos
6749 | Vclose "bookmarks" ->
6750 { v with f = doc path pan anchor c bookmarks }
6752 | Vclose _ -> error "unexpected close in bookmarks" s spos
6754 and skip tag f v t spos _ =
6755 match t with
6756 | Vdata | Vcdata -> v
6757 | Vend ->
6758 error ("unexpected end of input in skipped " ^ tag) s spos
6759 | Vopen (tag', _, closed) ->
6760 if closed
6761 then v
6762 else
6763 let f' () = { v with f = skip tag f } in
6764 { v with f = skip tag' f' }
6765 | Vclose ctag ->
6766 if tag = ctag
6767 then f ()
6768 else error ("unexpected close in skipped " ^ tag) s spos
6771 parse { f = toplevel; accu = () } s;
6772 h, dc;
6775 let do_load f ic =
6777 let len = in_channel_length ic in
6778 let s = String.create len in
6779 really_input ic s 0 len;
6780 f s;
6781 with
6782 | Parse_error (msg, s, pos) ->
6783 let subs = subs s pos in
6784 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6785 failwith ("parse error: " ^ s)
6787 | exn ->
6788 failwith ("config load error: " ^ exntos exn)
6791 let defconfpath =
6792 let dir =
6794 let dir = Filename.concat home ".config" in
6795 if Sys.is_directory dir then dir else home
6796 with _ -> home
6798 Filename.concat dir "llpp.conf"
6801 let confpath = ref defconfpath;;
6803 let load1 f =
6804 if Sys.file_exists !confpath
6805 then
6806 match
6807 (try Some (open_in_bin !confpath)
6808 with exn ->
6809 prerr_endline
6810 ("Error opening configuration file `" ^ !confpath ^ "': " ^
6811 exntos exn);
6812 None
6814 with
6815 | Some ic ->
6816 let success =
6818 f (do_load get ic)
6819 with exn ->
6820 prerr_endline
6821 ("Error loading configuration from `" ^ !confpath ^ "': " ^
6822 exntos exn);
6823 false
6825 close_in ic;
6826 success
6828 | None -> false
6829 else
6830 f (Hashtbl.create 0, defconf)
6833 let load () =
6834 let f (h, dc) =
6835 let pc, pb, px, pa =
6837 let key =
6838 if String.length state.origin = 0
6839 then state.path
6840 else state.origin
6842 Hashtbl.find h (Filename.basename key)
6843 with Not_found -> dc, [], 0, emptyanchor
6845 setconf defconf dc;
6846 setconf conf pc;
6847 state.bookmarks <- pb;
6848 state.x <- px;
6849 if conf.jumpback
6850 then state.anchor <- pa;
6851 cbput state.hists.nav pa;
6852 true
6854 load1 f
6857 let add_attrs bb always dc c =
6858 let ob s a b =
6859 if always || a != b
6860 then Printf.bprintf bb "\n %s='%b'" s a
6861 and oi s a b =
6862 if always || a != b
6863 then Printf.bprintf bb "\n %s='%d'" s a
6864 and oI s a b =
6865 if always || a != b
6866 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6867 and oz s a b =
6868 if always || a <> b
6869 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6870 and oF s a b =
6871 if always || a <> b
6872 then Printf.bprintf bb "\n %s='%f'" s a
6873 and oc s a b =
6874 if always || a <> b
6875 then
6876 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6877 and oC s a b =
6878 if always || a <> b
6879 then
6880 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6881 and oR s a b =
6882 if always || a <> b
6883 then
6884 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6885 and os s a b =
6886 if always || a <> b
6887 then
6888 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6889 and og s a b =
6890 if always || a <> b
6891 then
6892 match a with
6893 | None -> ()
6894 | Some (_N, _A, _B) ->
6895 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6896 and oW s a b =
6897 if always || a <> b
6898 then
6899 let v =
6900 match a with
6901 | None -> "false"
6902 | Some f ->
6903 if f = infinity
6904 then "true"
6905 else string_of_float f
6907 Printf.bprintf bb "\n %s='%s'" s v
6908 and oco s a b =
6909 if always || a <> b
6910 then
6911 match a with
6912 | Cmulti ((n, a, b), _) when n > 1 ->
6913 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6914 | Csplit (n, _) when n > 1 ->
6915 Printf.bprintf bb "\n %s='%d'" s ~-n
6916 | _ -> ()
6917 and obeco s a b =
6918 if always || a <> b
6919 then
6920 match a with
6921 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6922 | _ -> ()
6923 and oFm s a b =
6924 if always || a <> b
6925 then
6926 Printf.bprintf bb "\n %s='%s'" s (fitmodel_to_string a)
6927 and oSv s a b m =
6928 if always || a <> b
6929 then
6930 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
6932 oi "width" c.cwinw dc.cwinw;
6933 oi "height" c.cwinh dc.cwinh;
6934 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6935 oi "scroll-handle-height" c.scrollh dc.scrollh;
6936 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
6937 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
6938 ob "case-insensitive-search" c.icase dc.icase;
6939 ob "preload" c.preload dc.preload;
6940 oi "page-bias" c.pagebias dc.pagebias;
6941 oi "scroll-step" c.scrollstep dc.scrollstep;
6942 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6943 ob "max-height-fit" c.maxhfit dc.maxhfit;
6944 ob "crop-hack" c.crophack dc.crophack;
6945 oW "throttle" c.maxwait dc.maxwait;
6946 ob "highlight-links" c.hlinks dc.hlinks;
6947 ob "under-cursor-info" c.underinfo dc.underinfo;
6948 oi "vertical-margin" c.interpagespace dc.interpagespace;
6949 oz "zoom" c.zoom dc.zoom;
6950 ob "presentation" c.presentation dc.presentation;
6951 oi "rotation-angle" c.angle dc.angle;
6952 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6953 oFm "fit-model" c.fitmodel dc.fitmodel;
6954 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6955 oi "tex-count" c.texcount dc.texcount;
6956 oi "slice-height" c.sliceheight dc.sliceheight;
6957 oi "thumbnail-width" c.thumbw dc.thumbw;
6958 ob "persistent-location" c.jumpback dc.jumpback;
6959 oc "background-color" c.bgcolor dc.bgcolor;
6960 oi "tile-width" c.tilew dc.tilew;
6961 oi "tile-height" c.tileh dc.tileh;
6962 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6963 ob "checkers" c.checkers dc.checkers;
6964 oi "aalevel" c.aalevel dc.aalevel;
6965 ob "trim-margins" c.trimmargins dc.trimmargins;
6966 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6967 os "uri-launcher" c.urilauncher dc.urilauncher;
6968 os "path-launcher" c.pathlauncher dc.pathlauncher;
6969 oC "color-space" c.colorspace dc.colorspace;
6970 ob "invert-colors" c.invert dc.invert;
6971 oF "brightness" c.colorscale dc.colorscale;
6972 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6973 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6974 oco "columns" c.columns dc.columns;
6975 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6976 os "selection-command" c.selcmd dc.selcmd;
6977 os "synctex-command" c.stcmd dc.stcmd;
6978 ob "update-cursor" c.updatecurs dc.updatecurs;
6979 oi "hint-font-size" c.hfsize dc.hfsize;
6980 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6981 oF "page-scroll-scale" c.pgscale dc.pgscale;
6982 ob "use-pbo" c.usepbo dc.usepbo;
6983 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6984 ob "remote-in-a-new-instance" c.riani dc.riani;
6987 let keymapsbuf always dc c =
6988 let bb = Buffer.create 16 in
6989 let rec loop = function
6990 | [] -> ()
6991 | (modename, h) :: rest ->
6992 let dh = findkeyhash dc modename in
6993 if always || h <> dh
6994 then (
6995 if Hashtbl.length h > 0
6996 then (
6997 if Buffer.length bb > 0
6998 then Buffer.add_char bb '\n';
6999 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7000 Hashtbl.iter (fun i o ->
7001 let isdifferent = always ||
7003 let dO = Hashtbl.find dh i in
7004 dO <> o
7005 with Not_found -> true
7007 if isdifferent
7008 then
7009 let addkm (k, m) =
7010 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7011 if Wsi.withalt m then Buffer.add_string bb "alt-";
7012 if Wsi.withshift m then Buffer.add_string bb "shift-";
7013 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7014 Buffer.add_string bb (Wsi.keyname k);
7016 let addkms l =
7017 let rec loop = function
7018 | [] -> ()
7019 | km :: [] -> addkm km
7020 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7022 loop l
7024 Buffer.add_string bb "<map in='";
7025 addkm i;
7026 match o with
7027 | KMinsrt km ->
7028 Buffer.add_string bb "' out='";
7029 addkm km;
7030 Buffer.add_string bb "'/>\n"
7032 | KMinsrl kms ->
7033 Buffer.add_string bb "' out='";
7034 addkms kms;
7035 Buffer.add_string bb "'/>\n"
7037 | KMmulti (ins, kms) ->
7038 Buffer.add_char bb ' ';
7039 addkms ins;
7040 Buffer.add_string bb "' out='";
7041 addkms kms;
7042 Buffer.add_string bb "'/>\n"
7043 ) h;
7044 Buffer.add_string bb "</keymap>";
7047 loop rest
7049 loop c.keyhashes;
7053 let save () =
7054 let uifontsize = fstate.fontsize in
7055 let bb = Buffer.create 32768 in
7056 let relx = float state.x /. float state.winw in
7057 let w, h, x =
7058 let cx w = truncate (relx *. float w) in
7059 List.fold_left
7060 (fun (w, h, x) ws ->
7061 match ws with
7062 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7063 | Wsi.MaxVert -> (w, conf.cwinh, x)
7064 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7066 (state.winw, state.winh, state.x) state.winstate
7068 conf.cwinw <- w;
7069 conf.cwinh <- h;
7070 let f (h, dc) =
7071 let dc = if conf.bedefault then conf else dc in
7072 Buffer.add_string bb "<llppconfig>\n";
7074 if String.length !fontpath > 0
7075 then
7076 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7077 uifontsize
7078 !fontpath
7079 else (
7080 if uifontsize <> 14
7081 then
7082 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7085 Buffer.add_string bb "<defaults ";
7086 add_attrs bb true dc dc;
7087 let kb = keymapsbuf true dc dc in
7088 if Buffer.length kb > 0
7089 then (
7090 Buffer.add_string bb ">\n";
7091 Buffer.add_buffer bb kb;
7092 Buffer.add_string bb "\n</defaults>\n";
7094 else Buffer.add_string bb "/>\n";
7096 let adddoc path pan anchor c bookmarks =
7097 if bookmarks == [] && c = dc && anchor = emptyanchor
7098 then ()
7099 else (
7100 Printf.bprintf bb "<doc path='%s'"
7101 (enent path 0 (String.length path));
7103 if anchor <> emptyanchor
7104 then (
7105 let n, rely, visy = anchor in
7106 Printf.bprintf bb " page='%d'" n;
7107 if rely > 1e-6
7108 then
7109 Printf.bprintf bb " rely='%f'" rely
7111 if abs_float visy > 1e-6
7112 then
7113 Printf.bprintf bb " visy='%f'" visy
7117 if pan != 0
7118 then Printf.bprintf bb " pan='%d'" pan;
7120 add_attrs bb false dc c;
7121 let kb = keymapsbuf false dc c in
7123 begin match bookmarks with
7124 | [] ->
7125 if Buffer.length kb > 0
7126 then (
7127 Buffer.add_string bb ">\n";
7128 Buffer.add_buffer bb kb;
7129 Buffer.add_string bb "\n</doc>\n";
7131 else Buffer.add_string bb "/>\n"
7132 | _ ->
7133 Buffer.add_string bb ">\n<bookmarks>\n";
7134 List.iter (fun (title, _level, (page, rely, visy)) ->
7135 Printf.bprintf bb
7136 "<item title='%s' page='%d'"
7137 (enent title 0 (String.length title))
7138 page
7140 if rely > 1e-6
7141 then
7142 Printf.bprintf bb " rely='%f'" rely
7144 if abs_float visy > 1e-6
7145 then
7146 Printf.bprintf bb " visy='%f'" visy
7148 Buffer.add_string bb "/>\n";
7149 ) bookmarks;
7150 Buffer.add_string bb "</bookmarks>";
7151 if Buffer.length kb > 0
7152 then (
7153 Buffer.add_string bb "\n";
7154 Buffer.add_buffer bb kb;
7156 Buffer.add_string bb "\n</doc>\n";
7157 end;
7161 let pan, conf =
7162 match state.mode with
7163 | Birdseye (c, pan, _, _, _) ->
7164 let beyecolumns =
7165 match conf.columns with
7166 | Cmulti ((c, _, _), _) -> Some c
7167 | Csingle _ -> None
7168 | Csplit _ -> None
7169 and columns =
7170 match c.columns with
7171 | Cmulti (c, _) -> Cmulti (c, [||])
7172 | Csingle _ -> Csingle [||]
7173 | Csplit _ -> failwith "quit from bird's eye while split"
7175 pan, { c with beyecolumns = beyecolumns; columns = columns }
7176 | _ -> x, conf
7178 let basename = Filename.basename
7179 (if String.length state.origin = 0 then state.path else state.origin)
7181 adddoc basename pan (getanchor ())
7182 (let conf =
7183 let autoscrollstep =
7184 match state.autoscroll with
7185 | Some step -> step
7186 | None -> conf.autoscrollstep
7188 match state.mode with
7189 | Birdseye (bc, _, _, _, _) ->
7190 { conf with
7191 zoom = bc.zoom;
7192 presentation = bc.presentation;
7193 interpagespace = bc.interpagespace;
7194 maxwait = bc.maxwait;
7195 autoscrollstep = autoscrollstep }
7196 | _ -> { conf with autoscrollstep = autoscrollstep }
7197 in conf)
7198 (if conf.savebmarks then state.bookmarks else []);
7200 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7201 if basename <> path
7202 then adddoc path x anchor c bookmarks
7203 ) h;
7204 Buffer.add_string bb "</llppconfig>\n";
7205 true;
7207 if load1 f && Buffer.length bb > 0
7208 then
7210 let tmp = !confpath ^ ".tmp" in
7211 let oc = open_out_bin tmp in
7212 Buffer.output_buffer oc bb;
7213 close_out oc;
7214 Unix.rename tmp !confpath;
7215 with exn ->
7216 prerr_endline
7217 ("error while saving configuration: " ^ exntos exn)
7219 end;;
7221 let adderrmsg src msg =
7222 Buffer.add_string state.errmsgs msg;
7223 state.newerrmsgs <- true;
7224 G.postRedisplay src
7227 let adderrfmt src fmt =
7228 Format.kprintf (fun s -> adderrmsg src s) fmt;
7231 let ract cmds =
7232 let cl = splitatspace cmds in
7233 let scan s fmt f =
7234 try Scanf.sscanf s fmt f
7235 with exn ->
7236 adderrfmt "remote exec"
7237 "error processing '%S': %s\n" cmds (exntos exn)
7239 match cl with
7240 | "reload" :: [] -> reload ()
7241 | "goto" :: args :: [] ->
7242 scan args "%u %f %f"
7243 (fun pageno x y ->
7244 let cmd, _ = state.geomcmds in
7245 if String.length cmd = 0
7246 then gotopagexy pageno x y
7247 else
7248 let f prevf () =
7249 gotopagexy pageno x y;
7250 prevf ()
7252 state.reprf <- f state.reprf
7254 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7255 | "rect" :: args :: [] ->
7256 scan args "%u %u %f %f %f %f"
7257 (fun pageno color x0 y0 x1 y1 ->
7258 onpagerect pageno (fun w h ->
7259 let _,w1,h1,_ = getpagedim pageno in
7260 let sw = float w1 /. w
7261 and sh = float h1 /. h in
7262 let x0s = x0 *. sw
7263 and x1s = x1 *. sw
7264 and y0s = y0 *. sh
7265 and y1s = y1 *. sh in
7266 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7267 debugrect rect;
7268 state.rects <- (pageno, color, rect) :: state.rects;
7269 G.postRedisplay "rect";
7272 | "activatewin" :: [] -> Wsi.activatewin ()
7273 | "quit" :: [] -> raise Quit
7274 | _ ->
7275 adderrfmt "remote command"
7276 "error processing remote command: %S\n" cmds;
7279 let remote =
7280 let scratch = String.create 80 in
7281 let buf = Buffer.create 80 in
7282 fun fd ->
7283 let rec tempfr () =
7284 try Some (Unix.read fd scratch 0 80)
7285 with
7286 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7287 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7288 | exn -> raise exn
7290 match tempfr () with
7291 | None -> Some fd
7292 | Some n ->
7293 if n = 0
7294 then (
7295 Unix.close fd;
7296 if Buffer.length buf > 0
7297 then (
7298 let s = Buffer.contents buf in
7299 Buffer.clear buf;
7300 ract s;
7302 None
7304 else
7305 let rec eat ppos =
7306 let nlpos =
7308 let pos = String.index_from scratch ppos '\n' in
7309 if pos >= n then -1 else pos
7310 with Not_found -> -1
7312 if nlpos >= 0
7313 then (
7314 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7315 let s = Buffer.contents buf in
7316 Buffer.clear buf;
7317 ract s;
7318 eat (nlpos+1);
7320 else (
7321 Buffer.add_substring buf scratch ppos (n-ppos);
7322 Some fd
7324 in eat 0
7327 let remoteopen path =
7328 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7329 with exn ->
7330 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7331 None
7334 let () =
7335 let trimcachepath = ref "" in
7336 let rcmdpath = ref "" in
7337 selfexec := Sys.executable_name;
7338 Arg.parse
7339 (Arg.align
7340 [("-p", Arg.String (fun s -> state.password <- s),
7341 "<password> Set password");
7343 ("-f", Arg.String
7344 (fun s ->
7345 Config.fontpath := s;
7346 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7348 "<path> Set path to the user interface font");
7350 ("-c", Arg.String
7351 (fun s ->
7352 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7353 Config.confpath := s),
7354 "<path> Set path to the configuration file");
7356 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7357 "<path> Set path to the trim cache file");
7359 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7360 "<named-destination> Set named destination");
7362 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7364 ("-remote", Arg.String (fun s -> rcmdpath := s),
7365 "<path> Set path to the remote commands source");
7367 ("-origin", Arg.String (fun s -> state.origin <- s),
7368 "<original-path> Set original path");
7370 ("-v", Arg.Unit (fun () ->
7371 Printf.printf
7372 "%s\nconfiguration path: %s\n"
7373 (version ())
7374 Config.defconfpath
7376 exit 0), " Print version and exit");
7379 (fun s -> state.path <- s)
7380 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7382 if !wtmode
7383 then selfexec := !selfexec ^ " -wtmode";
7385 if String.length state.path = 0
7386 then (prerr_endline "file name missing"; exit 1);
7388 if not (Config.load ())
7389 then prerr_endline "failed to load configuration";
7391 let wsfd, winw, winh = Wsi.init (object
7392 val mutable m_hack = false
7393 method expose = if not m_hack then G.postRedisplay "expose"
7394 method visible = G.postRedisplay "visible"
7395 method display = m_hack <- false; display ()
7396 method reshape w h =
7397 m_hack <- w < state.winw && h < state.winh;
7398 reshape w h
7399 method mouse b d x y m = mouse b d x y m
7400 method motion x y = state.mpos <- (x, y); motion x y
7401 method pmotion x y = state.mpos <- (x, y); pmotion x y
7402 method key k m =
7403 let mascm = m land (
7404 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7405 ) in
7406 match state.keystate with
7407 | KSnone ->
7408 let km = k, mascm in
7409 begin
7410 match
7411 let modehash = state.uioh#modehash in
7412 try Hashtbl.find modehash km
7413 with Not_found ->
7414 try Hashtbl.find (findkeyhash conf "global") km
7415 with Not_found -> KMinsrt (k, m)
7416 with
7417 | KMinsrt (k, m) -> keyboard k m
7418 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7419 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7421 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7422 List.iter (fun (k, m) -> keyboard k m) insrt;
7423 state.keystate <- KSnone
7424 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7425 state.keystate <- KSinto (keys, insrt)
7426 | _ ->
7427 state.keystate <- KSnone
7429 method enter x y = state.mpos <- (x, y); pmotion x y
7430 method leave = state.mpos <- (-1, -1)
7431 method winstate wsl = state.winstate <- wsl
7432 method quit = raise Quit
7433 end) conf.cwinw conf.cwinh (platform = Posx) in
7435 state.wsfd <- wsfd;
7437 if not (
7438 List.exists GlMisc.check_extension
7439 [ "GL_ARB_texture_rectangle"
7440 ; "GL_EXT_texture_recangle"
7441 ; "GL_NV_texture_rectangle" ]
7443 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7445 if (
7446 let r = GlMisc.get_string `renderer in
7447 let p = "Mesa DRI Intel(" in
7448 let l = String.length p in
7449 String.length r > l && String.sub r 0 l = p
7451 then defconf.sliceheight <- 1024;
7453 let cr, sw =
7454 match Ne.pipe () with
7455 | Ne.Exn exn ->
7456 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7457 exit 1
7458 | Ne.Res rw -> rw
7459 and sr, cw =
7460 match Ne.pipe () with
7461 | Ne.Exn exn ->
7462 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7463 exit 1
7464 | Ne.Res rw -> rw
7467 cloexec cr;
7468 cloexec sw;
7469 cloexec sr;
7470 cloexec cw;
7472 setcheckers conf.checkers;
7473 redirectstderr ();
7475 init (cr, cw) (
7476 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7477 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7478 !Config.fontpath, !trimcachepath,
7479 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7481 state.sr <- sr;
7482 state.sw <- sw;
7483 state.text <- "Opening " ^ (mbtoutf8 state.path);
7484 reshape winw winh;
7485 opendoc state.path state.password;
7486 state.uioh <- uioh;
7487 display ();
7488 Wsi.mapwin ();
7489 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7490 let optrfd =
7491 ref (
7492 if String.length !rcmdpath > 0
7493 then remoteopen !rcmdpath
7494 else None
7498 let rec loop deadline =
7499 let r =
7500 match state.errfd with
7501 | None -> [state.sr; state.wsfd]
7502 | Some fd -> [state.sr; state.wsfd; fd]
7504 let r =
7505 match !optrfd with
7506 | None -> r
7507 | Some fd -> fd :: r
7509 if state.redisplay
7510 then (
7511 state.redisplay <- false;
7512 display ();
7514 let timeout =
7515 let now = now () in
7516 if deadline > now
7517 then (
7518 if deadline = infinity
7519 then ~-.1.0
7520 else max 0.0 (deadline -. now)
7522 else 0.0
7524 let r, _, _ =
7525 try Unix.select r [] [] timeout
7526 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7528 begin match r with
7529 | [] ->
7530 state.ghyll None;
7531 let newdeadline =
7532 if state.ghyll == noghyll
7533 then
7534 match state.autoscroll with
7535 | Some step when step != 0 ->
7536 let y = state.y + step in
7537 let y =
7538 if y < 0
7539 then state.maxy
7540 else if y >= state.maxy then 0 else y
7542 gotoy y;
7543 if state.mode = View
7544 then state.text <- "";
7545 deadline +. 0.01
7546 | _ -> infinity
7547 else deadline +. 0.01
7549 loop newdeadline
7551 | l ->
7552 let rec checkfds = function
7553 | [] -> ()
7554 | fd :: rest when fd = state.sr ->
7555 let cmd = readcmd state.sr in
7556 act cmd;
7557 checkfds rest
7559 | fd :: rest when fd = state.wsfd ->
7560 Wsi.readresp fd;
7561 checkfds rest
7563 | fd :: rest when Some fd = !optrfd ->
7564 begin match remote fd with
7565 | None -> optrfd := remoteopen !rcmdpath;
7566 | opt -> optrfd := opt
7567 end;
7568 checkfds rest
7570 | fd :: rest ->
7571 let s = String.create 80 in
7572 let n = tempfailureretry (Unix.read fd s 0) 80 in
7573 if conf.redirectstderr
7574 then (
7575 Buffer.add_substring state.errmsgs s 0 n;
7576 state.newerrmsgs <- true;
7577 state.redisplay <- true;
7579 else (
7580 prerr_string (String.sub s 0 n);
7581 flush stderr;
7583 checkfds rest
7585 checkfds l;
7586 let newdeadline =
7587 let deadline1 =
7588 if deadline = infinity
7589 then now () +. 0.01
7590 else deadline
7592 match state.autoscroll with
7593 | Some step when step != 0 -> deadline1
7594 | _ -> if state.ghyll == noghyll then infinity else deadline1
7596 loop newdeadline
7597 end;
7600 loop infinity;
7601 with Quit ->
7602 Config.save ();