Cosmetics
[llpp/slowscroll.git] / main.ml
blob43deecf9bc812124eac22b824e04b61c93b8fc51
1 exception Quit;;
3 let tempfailureretry = Wsi.tempfailureretry;;
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 let dolog fmt = Printf.kprintf prerr_endline fmt;;
17 let now = Unix.gettimeofday;;
19 type params = (angle * proportional * trimparams
20 * texcount * sliceheight * memsize
21 * colorspace * fontpath * trimcachepath
22 * haspbo)
23 and pageno = int
24 and width = int
25 and height = int
26 and leftx = int
27 and opaque = string
28 and recttype = int
29 and pixmapsize = int
30 and angle = int
31 and proportional = bool
32 and trimmargins = bool
33 and interpagespace = int
34 and texcount = int
35 and sliceheight = int
36 and gen = int
37 and top = float
38 and dtop = float
39 and fontpath = string
40 and trimcachepath = string
41 and memsize = int
42 and aalevel = int
43 and irect = (int * int * int * int)
44 and trimparams = (trimmargins * irect)
45 and colorspace = | Rgb | Bgr | Gray
46 and haspbo = bool
49 type link =
50 | Lnotfound
51 | Lfound of int
52 and linkdir =
53 | LDfirst
54 | LDlast
55 | LDfirstvisible of (int * int * int)
56 | LDleft of int
57 | LDright of int
58 | LDdown of int
59 | LDup of int
62 type pagewithlinks =
63 | Pwlnotfound
64 | Pwl of int
67 type keymap =
68 | KMinsrt of key
69 | KMinsrl of key list
70 | KMmulti of key list * key list
71 and key = int * int
72 and keyhash = (key, keymap) Hashtbl.t
73 and keystate =
74 | KSnone
75 | KSinto of (key list * key list)
78 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
79 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
81 type pipe = (Unix.file_descr * Unix.file_descr);;
83 external init : pipe -> params -> unit = "ml_init";;
84 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
85 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
86 external getpdimrect : int -> float array = "ml_getpdimrect";;
87 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
88 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
89 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
90 external measurestr : int -> string -> float = "ml_measure_string";;
91 external getmaxw : unit -> float = "ml_getmaxw";;
92 external postprocess :
93 opaque -> int -> int -> int -> (int * string * int) -> int
94 = "ml_postprocess";;
95 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
96 external platform : unit -> platform = "ml_platform";;
97 external setaalevel : int -> unit = "ml_setaalevel";;
98 external realloctexts : int -> bool = "ml_realloctexts";;
99 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
100 external findlink : opaque -> linkdir -> link = "ml_findlink";;
101 external getlink : opaque -> int -> under = "ml_getlink";;
102 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
103 external getlinkcount : opaque -> int = "ml_getlinkcount";;
104 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
105 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
106 external mbtoutf8 : string -> string = "ml_mbtoutf8";;
107 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
108 external freepbo : string -> unit = "ml_freepbo";;
109 external unmappbo : string -> unit = "ml_unmappbo";;
110 external pbousable : unit -> bool = "ml_pbo_usable";;
111 external unproject : opaque -> int -> int -> (int * int) option
112 = "ml_unproject";;
114 let platform_to_string = function
115 | Punknown -> "unknown"
116 | Plinux -> "Linux"
117 | Posx -> "OSX"
118 | Psun -> "Sun"
119 | Pfreebsd -> "FreeBSD"
120 | Pdragonflybsd -> "DragonflyBSD"
121 | Popenbsd -> "OpenBSD"
122 | Pnetbsd -> "NetBSD"
123 | Pcygwin -> "Cygwin"
126 let platform = platform ();;
128 let popen cmd fda =
129 if platform = Pcygwin
130 then (
131 let sh = "/bin/sh" in
132 let args = [|sh; "-c"; cmd|] in
133 let rec std si so se = function
134 | [] -> si, so, se
135 | (fd, 0) :: rest -> std fd so se rest
136 | (fd, -1) :: rest ->
137 Unix.set_close_on_exec fd;
138 std si so se rest
139 | (_, n) :: _ ->
140 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
142 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
143 ignore (Unix.create_process sh args si so se)
145 else popen cmd fda;
148 type x = int
149 and y = int
150 and tilex = int
151 and tiley = int
152 and tileparams = (x * y * width * height * tilex * tiley)
155 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
157 type mpos = int * int
158 and mstate =
159 | Msel of (mpos * mpos)
160 | Mpan of mpos
161 | Mscrolly | Mscrollx
162 | Mzoom of (int * int)
163 | Mzoomrect of (mpos * mpos)
164 | Mnone
167 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
168 and onkey = string -> int -> te
169 and ondone = string -> unit
170 and histcancel = unit -> unit
171 and onhist = ((histcmd -> string) * histcancel)
172 and histcmd = HCnext | HCprev | HCfirst | HClast
173 and cancelonempty = bool
174 and te =
175 | TEstop
176 | TEdone of string
177 | TEcont of string
178 | TEswitch of textentry
181 type 'a circbuf =
182 { store : 'a array
183 ; mutable rc : int
184 ; mutable wc : int
185 ; mutable len : int
189 let bound v minv maxv =
190 max minv (min maxv v);
193 let cbnew n v =
194 { store = Array.create n v
195 ; rc = 0
196 ; wc = 0
197 ; len = 0
201 let cbcap b = Array.length b.store;;
203 let cbput b v =
204 let cap = cbcap b in
205 b.store.(b.wc) <- v;
206 b.wc <- (b.wc + 1) mod cap;
207 b.rc <- b.wc;
208 b.len <- min (b.len + 1) cap;
211 let cbempty b = b.len = 0;;
213 let cbgetg b circular dir =
214 if cbempty b
215 then b.store.(0)
216 else
217 let rc = b.rc + dir in
218 let rc =
219 if circular
220 then (
221 if rc = -1
222 then b.len-1
223 else (
224 if rc >= b.len
225 then 0
226 else rc
229 else bound rc 0 (b.len-1)
231 b.rc <- rc;
232 b.store.(rc);
235 let cbget b = cbgetg b false;;
236 let cbgetc b = cbgetg b true;;
238 let drawstring size x y s =
239 Gl.enable `blend;
240 Gl.enable `texture_2d;
241 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
242 ignore (drawstr size x y s);
243 Gl.disable `blend;
244 Gl.disable `texture_2d;
247 let drawstring1 size x y s =
248 drawstr size x y s;
251 let drawstring2 size x y fmt =
252 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
255 type page =
256 { pageno : int
257 ; pagedimno : int
258 ; pagew : int
259 ; pageh : int
260 ; pagex : int
261 ; pagey : int
262 ; pagevw : int
263 ; pagevh : int
264 ; pagedispx : int
265 ; pagedispy : int
266 ; pagecol : int
270 let debugl l =
271 dolog "l %d dim=%d {" l.pageno l.pagedimno;
272 dolog " WxH %dx%d" l.pagew l.pageh;
273 dolog " vWxH %dx%d" l.pagevw l.pagevh;
274 dolog " pagex,y %d,%d" l.pagex l.pagey;
275 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
276 dolog " column %d" l.pagecol;
277 dolog "}";
280 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
281 dolog "rect {";
282 dolog " x0,y0=(% f, % f)" x0 y0;
283 dolog " x1,y1=(% f, % f)" x1 y1;
284 dolog " x2,y2=(% f, % f)" x2 y2;
285 dolog " x3,y3=(% f, % f)" x3 y3;
286 dolog "}";
289 type multicolumns = multicol * pagegeom
290 and singlecolumn = pagegeom
291 and splitcolumns = columncount * pagegeom
292 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
293 and multicol = columncount * covercount * covercount
294 and pdimno = int
295 and columncount = int
296 and covercount = int;;
298 type conf =
299 { mutable scrollbw : int
300 ; mutable scrollh : int
301 ; mutable icase : bool
302 ; mutable preload : bool
303 ; mutable pagebias : int
304 ; mutable verbose : bool
305 ; mutable debug : bool
306 ; mutable scrollstep : int
307 ; mutable hscrollstep : int
308 ; mutable maxhfit : bool
309 ; mutable crophack : bool
310 ; mutable autoscrollstep : int
311 ; mutable maxwait : float option
312 ; mutable hlinks : bool
313 ; mutable underinfo : bool
314 ; mutable interpagespace : interpagespace
315 ; mutable zoom : float
316 ; mutable presentation : bool
317 ; mutable angle : angle
318 ; mutable cwinw : int
319 ; mutable cwinh : int
320 ; mutable savebmarks : bool
321 ; mutable proportional : proportional
322 ; mutable trimmargins : trimmargins
323 ; mutable trimfuzz : irect
324 ; mutable memlimit : memsize
325 ; mutable texcount : texcount
326 ; mutable sliceheight : sliceheight
327 ; mutable thumbw : width
328 ; mutable jumpback : bool
329 ; mutable bgcolor : float * float * float
330 ; mutable bedefault : bool
331 ; mutable scrollbarinpm : bool
332 ; mutable tilew : int
333 ; mutable tileh : int
334 ; mutable mustoresize : memsize
335 ; mutable checkers : bool
336 ; mutable aalevel : int
337 ; mutable urilauncher : string
338 ; mutable pathlauncher : string
339 ; mutable colorspace : colorspace
340 ; mutable invert : bool
341 ; mutable colorscale : float
342 ; mutable redirectstderr : bool
343 ; mutable ghyllscroll : (int * int * int) option
344 ; mutable columns : columns
345 ; mutable beyecolumns : columncount option
346 ; mutable selcmd : string
347 ; mutable updatecurs : bool
348 ; mutable keyhashes : (string * keyhash) list
349 ; mutable hfsize : int
350 ; mutable pgscale : float
351 ; mutable usepbo : bool
352 ; mutable wheelbypage : bool
353 ; mutable stcmd : string
355 and columns =
356 | Csingle of singlecolumn
357 | Cmulti of multicolumns
358 | Csplit of splitcolumns
361 type anchor = pageno * top * dtop;;
363 type outline = string * int * anchor;;
365 type rect = float * float * float * float * float * float * float * float;;
367 type tile = opaque * pixmapsize * elapsed
368 and elapsed = float;;
369 type pagemapkey = pageno * gen;;
370 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
371 and row = int
372 and col = int;;
374 let emptyanchor = (0, 0.0, 0.0);;
376 type infochange = | Memused | Docinfo | Pdim;;
378 class type uioh = object
379 method display : unit
380 method key : int -> int -> uioh
381 method button : int -> bool -> int -> int -> int -> uioh
382 method motion : int -> int -> uioh
383 method pmotion : int -> int -> uioh
384 method infochanged : infochange -> unit
385 method scrollpw : (int * float * float)
386 method scrollph : (int * float * float)
387 method modehash : keyhash
388 end;;
390 type mode =
391 | Birdseye of (conf * leftx * pageno * pageno * anchor)
392 | Textentry of (textentry * onleave)
393 | View
394 | LinkNav of linktarget
395 and onleave = leavetextentrystatus -> unit
396 and leavetextentrystatus = | Cancel | Confirm
397 and helpitem = string * int * action
398 and action =
399 | Noaction
400 | Action of (uioh -> uioh)
401 and linktarget =
402 | Ltexact of (pageno * int)
403 | Ltgendir of int
406 let isbirdseye = function Birdseye _ -> true | _ -> false;;
407 let istextentry = function Textentry _ -> true | _ -> false;;
409 type currently =
410 | Idle
411 | Loading of (page * gen)
412 | Tiling of (
413 page * opaque * colorspace * angle * gen * col * row * width * height
415 | Outlining of outline list
418 let emptykeyhash = Hashtbl.create 0;;
419 let nouioh : uioh = object (self)
420 method display = ()
421 method key _ _ = self
422 method button _ _ _ _ _ = self
423 method motion _ _ = self
424 method pmotion _ _ = self
425 method infochanged _ = ()
426 method scrollpw = (0, nan, nan)
427 method scrollph = (0, nan, nan)
428 method modehash = emptykeyhash
429 end;;
431 type state =
432 { mutable sr : Unix.file_descr
433 ; mutable sw : Unix.file_descr
434 ; mutable wsfd : Unix.file_descr
435 ; mutable errfd : Unix.file_descr option
436 ; mutable stderr : Unix.file_descr
437 ; mutable errmsgs : Buffer.t
438 ; mutable newerrmsgs : bool
439 ; mutable w : int
440 ; mutable x : int
441 ; mutable y : int
442 ; mutable scrollw : int
443 ; mutable hscrollh : int
444 ; mutable anchor : anchor
445 ; mutable ranchors : (string * string * anchor) list
446 ; mutable maxy : int
447 ; mutable layout : page list
448 ; pagemap : (pagemapkey, opaque) Hashtbl.t
449 ; tilemap : (tilemapkey, tile) Hashtbl.t
450 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
451 ; mutable pdims : (pageno * width * height * leftx) list
452 ; mutable pagecount : int
453 ; mutable currently : currently
454 ; mutable mstate : mstate
455 ; mutable searchpattern : string
456 ; mutable rects : (pageno * recttype * rect) list
457 ; mutable rects1 : (pageno * recttype * rect) list
458 ; mutable text : string
459 ; mutable winstate : Wsi.winstate list
460 ; mutable mode : mode
461 ; mutable uioh : uioh
462 ; mutable outlines : outline array
463 ; mutable bookmarks : outline list
464 ; mutable path : string
465 ; mutable password : string
466 ; mutable nameddest : string
467 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
468 ; mutable memused : memsize
469 ; mutable gen : gen
470 ; mutable throttle : (page list * int * float) option
471 ; mutable autoscroll : int option
472 ; mutable ghyll : (int option -> unit)
473 ; mutable help : helpitem array
474 ; mutable docinfo : (int * string) list
475 ; mutable texid : GlTex.texture_id option
476 ; hists : hists
477 ; mutable prevzoom : float
478 ; mutable progress : float
479 ; mutable redisplay : bool
480 ; mutable mpos : mpos
481 ; mutable keystate : keystate
482 ; mutable glinks : bool
483 ; mutable prevcolumns : (columns * float) option
484 ; mutable wthack : bool
485 ; mutable winw : int
486 ; mutable winh : int
487 ; mutable reprf : (unit -> unit)
489 and hists =
490 { pat : string circbuf
491 ; pag : string circbuf
492 ; nav : anchor circbuf
493 ; sel : string circbuf
497 let defconf =
498 { scrollbw = 7
499 ; scrollh = 12
500 ; icase = true
501 ; preload = true
502 ; pagebias = 0
503 ; verbose = false
504 ; debug = false
505 ; scrollstep = 24
506 ; hscrollstep = 24
507 ; maxhfit = true
508 ; crophack = false
509 ; autoscrollstep = 2
510 ; maxwait = None
511 ; hlinks = false
512 ; underinfo = false
513 ; interpagespace = 2
514 ; zoom = 1.0
515 ; presentation = false
516 ; angle = 0
517 ; cwinw = 900
518 ; cwinh = 900
519 ; savebmarks = true
520 ; proportional = true
521 ; trimmargins = false
522 ; trimfuzz = (0,0,0,0)
523 ; memlimit = 32 lsl 20
524 ; texcount = 256
525 ; sliceheight = 24
526 ; thumbw = 76
527 ; jumpback = true
528 ; bgcolor = (0.5, 0.5, 0.5)
529 ; bedefault = false
530 ; scrollbarinpm = true
531 ; tilew = 2048
532 ; tileh = 2048
533 ; mustoresize = 256 lsl 20
534 ; checkers = true
535 ; aalevel = 8
536 ; urilauncher =
537 (match platform with
538 | Plinux | Pfreebsd | Pdragonflybsd
539 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
540 | Posx -> "open \"%s\""
541 | Pcygwin -> "cygstart \"%s\""
542 | Punknown -> "echo %s")
543 ; pathlauncher = "lp \"%s\""
544 ; selcmd =
545 (match platform with
546 | Plinux | Pfreebsd | Pdragonflybsd
547 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
548 | Posx -> "pbcopy"
549 | Pcygwin -> "wsel"
550 | Punknown -> "cat")
551 ; colorspace = Rgb
552 ; invert = false
553 ; colorscale = 1.0
554 ; redirectstderr = false
555 ; ghyllscroll = None
556 ; columns = Csingle [||]
557 ; beyecolumns = None
558 ; updatecurs = false
559 ; hfsize = 12
560 ; pgscale = 1.0
561 ; usepbo = false
562 ; wheelbypage = false
563 ; stcmd = "echo SyncTex"
564 ; keyhashes =
565 let mk n = (n, Hashtbl.create 1) in
566 [ mk "global"
567 ; mk "info"
568 ; mk "help"
569 ; mk "outline"
570 ; mk "listview"
571 ; mk "birdseye"
572 ; mk "textentry"
573 ; mk "links"
574 ; mk "view"
579 let wtmode = ref false;;
581 let findkeyhash c name =
582 try List.assoc name c.keyhashes
583 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
586 let conf = { defconf with angle = defconf.angle };;
588 let pgscale h = truncate (float h *. conf.pgscale);;
590 type fontstate =
591 { mutable fontsize : int
592 ; mutable wwidth : float
593 ; mutable maxrows : int
597 let fstate =
598 { fontsize = 14
599 ; wwidth = nan
600 ; maxrows = -1
604 let geturl s =
605 let colonpos = try String.index s ':' with Not_found -> -1 in
606 let len = String.length s in
607 if colonpos >= 0 && colonpos + 3 < len
608 then (
609 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
610 then
611 let schemestartpos =
612 try String.rindex_from s colonpos ' '
613 with Not_found -> -1
615 let scheme =
616 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
618 match scheme with
619 | "http" | "ftp" | "mailto" ->
620 let epos =
621 try String.index_from s colonpos ' '
622 with Not_found -> len
624 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
625 | _ -> ""
626 else ""
628 else ""
631 let gotouri uri =
632 if String.length conf.urilauncher = 0
633 then print_endline uri
634 else (
635 let url = geturl uri in
636 if String.length url = 0
637 then print_endline uri
638 else
639 let re = Str.regexp "%s" in
640 let command = Str.global_replace re url conf.urilauncher in
641 try popen command []
642 with exn ->
643 Printf.eprintf
644 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
645 flush stderr;
649 let version () =
650 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
651 (platform_to_string platform) Sys.word_size Sys.ocaml_version
654 let makehelp () =
655 let strings = version () :: "" :: Help.keys in
656 Array.of_list (
657 List.map (fun s ->
658 let url = geturl s in
659 if String.length url > 0
660 then (s, 0, Action (fun u -> gotouri url; u))
661 else (s, 0, Noaction)
662 ) strings);
665 let noghyll _ = ();;
666 let firstgeomcmds = "", [];;
667 let noreprf () = ();;
669 let state =
670 { sr = Unix.stdin
671 ; sw = Unix.stdin
672 ; wsfd = Unix.stdin
673 ; errfd = None
674 ; stderr = Unix.stderr
675 ; errmsgs = Buffer.create 0
676 ; newerrmsgs = false
677 ; x = 0
678 ; y = 0
679 ; w = 0
680 ; scrollw = 0
681 ; hscrollh = 0
682 ; anchor = emptyanchor
683 ; ranchors = []
684 ; layout = []
685 ; maxy = max_int
686 ; tilelru = Queue.create ()
687 ; pagemap = Hashtbl.create 10
688 ; tilemap = Hashtbl.create 10
689 ; pdims = []
690 ; pagecount = 0
691 ; currently = Idle
692 ; mstate = Mnone
693 ; rects = []
694 ; rects1 = []
695 ; text = ""
696 ; mode = View
697 ; winstate = []
698 ; searchpattern = ""
699 ; outlines = [||]
700 ; bookmarks = []
701 ; path = ""
702 ; password = ""
703 ; nameddest = ""
704 ; geomcmds = firstgeomcmds
705 ; hists =
706 { nav = cbnew 10 emptyanchor
707 ; pat = cbnew 10 ""
708 ; pag = cbnew 10 ""
709 ; sel = cbnew 10 ""
711 ; memused = 0
712 ; gen = 0
713 ; throttle = None
714 ; autoscroll = None
715 ; ghyll = noghyll
716 ; help = makehelp ()
717 ; docinfo = []
718 ; texid = None
719 ; prevzoom = 1.0
720 ; progress = -1.0
721 ; uioh = nouioh
722 ; redisplay = true
723 ; mpos = (-1, -1)
724 ; keystate = KSnone
725 ; glinks = false
726 ; prevcolumns = None
727 ; wthack = false
728 ; winw = -1
729 ; winh = -1
730 ; reprf = noreprf
734 let setfontsize n =
735 fstate.fontsize <- n;
736 fstate.wwidth <- measurestr fstate.fontsize "w";
737 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
740 let vlog fmt =
741 if conf.verbose
742 then
743 Printf.kprintf prerr_endline fmt
744 else
745 Printf.kprintf ignore fmt
748 let launchpath () =
749 if String.length conf.pathlauncher = 0
750 then print_endline state.path
751 else (
752 let re = Str.regexp "%s" in
753 let command = Str.global_replace re state.path conf.pathlauncher in
754 try popen command []
755 with exn ->
756 Printf.eprintf
757 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
758 flush stderr;
762 module Ne = struct
763 type 'a t = | Res of 'a | Exn of exn;;
765 let pipe () =
766 try Res (Unix.pipe ())
767 with exn -> Exn exn
770 let clo fd f =
771 try tempfailureretry Unix.close fd
772 with exn -> f (Printexc.to_string exn)
775 let dup fd =
776 try Res (tempfailureretry Unix.dup fd)
777 with exn -> Exn exn
780 let dup2 fd1 fd2 =
781 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
782 with exn -> Exn exn
784 end;;
786 let redirectstderr () =
787 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
788 if conf.redirectstderr
789 then
790 match Ne.pipe () with
791 | Ne.Exn exn ->
792 dolog "failed to create stderr redirection pipes: %s"
793 (Printexc.to_string exn)
795 | Ne.Res (r, w) ->
796 begin match Ne.dup Unix.stderr with
797 | Ne.Exn exn ->
798 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
799 Ne.clo r (clofail "pipe/r");
800 Ne.clo w (clofail "pipe/w");
802 | Ne.Res dupstderr ->
803 begin match Ne.dup2 w Unix.stderr with
804 | Ne.Exn exn ->
805 dolog "failed to dup2 to stderr: %s"
806 (Printexc.to_string exn);
807 Ne.clo dupstderr (clofail "stderr duplicate");
808 Ne.clo r (clofail "redir pipe/r");
809 Ne.clo w (clofail "redir pipe/w");
811 | Ne.Res () ->
812 state.stderr <- dupstderr;
813 state.errfd <- Some r;
814 end;
816 else (
817 state.newerrmsgs <- false;
818 begin match state.errfd with
819 | Some fd ->
820 begin match Ne.dup2 state.stderr Unix.stderr with
821 | Ne.Exn exn ->
822 dolog "failed to dup2 original stderr: %s"
823 (Printexc.to_string exn)
824 | Ne.Res () ->
825 Ne.clo fd (clofail "dup of stderr");
826 state.errfd <- None;
827 end;
828 | None -> ()
829 end;
830 prerr_string (Buffer.contents state.errmsgs);
831 flush stderr;
832 Buffer.clear state.errmsgs;
836 module G =
837 struct
838 let postRedisplay who =
839 if conf.verbose
840 then prerr_endline ("redisplay for " ^ who);
841 state.redisplay <- true;
843 end;;
845 let getopaque pageno =
846 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
847 with Not_found -> None
850 let putopaque pageno opaque =
851 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
854 let pagetranslatepoint l x y =
855 let dy = y - l.pagedispy in
856 let y = dy + l.pagey in
857 let dx = x - l.pagedispx in
858 let x = dx + l.pagex in
859 (x, y);
862 let onppundermouse g x y d =
863 let rec f = function
864 | l :: rest ->
865 begin match getopaque l.pageno with
866 | Some opaque ->
867 let x0 = l.pagedispx in
868 let x1 = x0 + l.pagevw in
869 let y0 = l.pagedispy in
870 let y1 = y0 + l.pagevh in
871 if y >= y0 && y <= y1 && x >= x0 && x <= x1
872 then
873 let px, py = pagetranslatepoint l x y in
874 match g opaque l px py with
875 | Some res -> res
876 | None -> f rest
877 else f rest
878 | _ ->
879 f rest
881 | [] -> d
883 f state.layout
886 let getunder x y =
887 let g opaque _ px py =
888 match whatsunder opaque px py with
889 | Unone -> None
890 | under -> Some under
892 onppundermouse g x y Unone
895 let unproject x y =
896 let g opaque l x y =
897 match unproject opaque x y with
898 | Some (x, y) -> Some (Some (l.pageno, x, y))
899 | None -> None
901 onppundermouse g x y None;
904 let showtext c s =
905 state.text <- Printf.sprintf "%c%s" c s;
906 G.postRedisplay "showtext";
909 let undertext = function
910 | Unone -> "none"
911 | Ulinkuri s -> s
912 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
913 | Utext s -> "font: " ^ s
914 | Uunexpected s -> "unexpected: " ^ s
915 | Ulaunch s -> "launch: " ^ s
916 | Unamed s -> "named: " ^ s
917 | Uremote (filename, pageno) ->
918 Printf.sprintf "%s: page %d" filename (pageno+1)
921 let updateunder x y =
922 match getunder x y with
923 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
924 | Ulinkuri uri ->
925 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
926 Wsi.setcursor Wsi.CURSOR_INFO
927 | Ulinkgoto (pageno, _) ->
928 if conf.underinfo
929 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
930 Wsi.setcursor Wsi.CURSOR_INFO
931 | Utext s ->
932 if conf.underinfo then showtext 'f' ("ont: " ^ s);
933 Wsi.setcursor Wsi.CURSOR_TEXT
934 | Uunexpected s ->
935 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
936 Wsi.setcursor Wsi.CURSOR_INHERIT
937 | Ulaunch s ->
938 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
939 Wsi.setcursor Wsi.CURSOR_INHERIT
940 | Unamed s ->
941 if conf.underinfo then showtext 'n' ("amed: " ^ s);
942 Wsi.setcursor Wsi.CURSOR_INHERIT
943 | Uremote (filename, pageno) ->
944 if conf.underinfo then showtext 'r'
945 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
946 Wsi.setcursor Wsi.CURSOR_INFO
949 let showlinktype under =
950 if conf.underinfo
951 then
952 match under with
953 | Unone -> ()
954 | under ->
955 let s = undertext under in
956 showtext ' ' s
959 let addchar s c =
960 let b = Buffer.create (String.length s + 1) in
961 Buffer.add_string b s;
962 Buffer.add_char b c;
963 Buffer.contents b;
966 let colorspace_of_string s =
967 match String.lowercase s with
968 | "rgb" -> Rgb
969 | "bgr" -> Bgr
970 | "gray" -> Gray
971 | _ -> failwith "invalid colorspace"
974 let int_of_colorspace = function
975 | Rgb -> 0
976 | Bgr -> 1
977 | Gray -> 2
980 let colorspace_of_int = function
981 | 0 -> Rgb
982 | 1 -> Bgr
983 | 2 -> Gray
984 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
987 let colorspace_to_string = function
988 | Rgb -> "rgb"
989 | Bgr -> "bgr"
990 | Gray -> "gray"
993 let intentry_with_suffix text key =
994 let c =
995 if key >= 32 && key < 127
996 then Char.chr key
997 else '\000'
999 match Char.lowercase c with
1000 | '0' .. '9' ->
1001 let text = addchar text c in
1002 TEcont text
1004 | 'k' | 'm' | 'g' ->
1005 let text = addchar text c in
1006 TEcont text
1008 | _ ->
1009 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1010 TEcont text
1013 let multicolumns_to_string (n, a, b) =
1014 if a = 0 && b = 0
1015 then Printf.sprintf "%d" n
1016 else Printf.sprintf "%d,%d,%d" n a b;
1019 let multicolumns_of_string s =
1021 (int_of_string s, 0, 0)
1022 with _ ->
1023 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1024 if a > 1 || b > 1
1025 then failwith "subtly broken"; (n, a, b)
1029 let readcmd fd =
1030 let s = "xxxx" in
1031 let n = tempfailureretry (Unix.read fd s 0) 4 in
1032 if n != 4 then failwith "incomplete read(len)";
1033 let len = 0
1034 lor (Char.code s.[0] lsl 24)
1035 lor (Char.code s.[1] lsl 16)
1036 lor (Char.code s.[2] lsl 8)
1037 lor (Char.code s.[3] lsl 0)
1039 let s = String.create len in
1040 let n = tempfailureretry (Unix.read fd s 0) len in
1041 if n != len then failwith "incomplete read(data)";
1045 let btod b = if b then 1 else 0;;
1047 let wcmd fmt =
1048 let b = Buffer.create 16 in
1049 Buffer.add_string b "llll";
1050 Printf.kbprintf
1051 (fun b ->
1052 let s = Buffer.contents b in
1053 let n = String.length s in
1054 let len = n - 4 in
1055 (* dolog "wcmd %S" (String.sub s 4 len); *)
1056 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1057 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1058 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1059 s.[3] <- Char.chr (len land 0xff);
1060 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1061 if n' != n then failwith "write failed";
1062 ) b fmt;
1065 let calcips h =
1066 let d = state.winh - h in
1067 max conf.interpagespace ((d + 1) / 2)
1070 let rowyh (c, coverA, coverB) b n =
1071 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1072 then
1073 let _, _, vy, (_, _, h, _) = b.(n) in
1074 (vy, h)
1075 else
1076 let n' = n - coverA in
1077 let d = n' mod c in
1078 let s = n - d in
1079 let e = min state.pagecount (s + c) in
1080 let rec find m miny maxh = if m = e then miny, maxh else
1081 let _, _, y, (_, _, h, _) = b.(m) in
1082 let miny = min miny y in
1083 let maxh = max maxh h in
1084 find (m+1) miny maxh
1085 in find s max_int 0
1088 let calcheight () =
1089 match conf.columns with
1090 | Cmulti ((_, _, _) as cl, b) ->
1091 if Array.length b > 0
1092 then
1093 let y, h = rowyh cl b (Array.length b - 1) in
1094 y + h + (if conf.presentation then calcips h else 0)
1095 else 0
1096 | Csingle b ->
1097 if Array.length b > 0
1098 then
1099 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1100 y + h + (if conf.presentation then calcips h else 0)
1101 else 0
1102 | Csplit (_, b) ->
1103 if Array.length b > 0
1104 then
1105 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1106 y + h
1107 else 0
1110 let getpageyh pageno =
1111 let pageno = bound pageno 0 (state.pagecount-1) in
1112 match conf.columns with
1113 | Csingle b ->
1114 if Array.length b = 0
1115 then 0, 0
1116 else
1117 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1118 let y =
1119 if conf.presentation
1120 then y - calcips h
1121 else y
1123 y, h
1124 | Cmulti (cl, b) ->
1125 if Array.length b = 0
1126 then 0, 0
1127 else
1128 let y, h = rowyh cl b pageno in
1129 let y =
1130 if conf.presentation
1131 then y - calcips h
1132 else y
1134 y, h
1135 | Csplit (c, b) ->
1136 if Array.length b = 0
1137 then 0, 0
1138 else
1139 let n = pageno*c in
1140 let (_, _, y, (_, _, h, _)) = b.(n) in
1141 y, h
1144 let getpagedim pageno =
1145 let rec f ppdim l =
1146 match l with
1147 | (n, _, _, _) as pdim :: rest ->
1148 if n >= pageno
1149 then (if n = pageno then pdim else ppdim)
1150 else f pdim rest
1152 | [] -> ppdim
1154 f (-1, -1, -1, -1) state.pdims
1157 let getpagey pageno = fst (getpageyh pageno);;
1159 let nogeomcmds cmds =
1160 match cmds with
1161 | s, [] -> String.length s = 0
1162 | _ -> false
1165 let page_of_y y =
1166 let ((c, coverA, coverB) as cl), b =
1167 match conf.columns with
1168 | Csingle b -> (1, 0, 0), b
1169 | Cmulti (c, b) -> c, b
1170 | Csplit (_, b) -> (1, 0, 0), b
1172 if Array.length b = 0
1173 then -1
1174 else
1175 let rec bsearch nmin nmax =
1176 if nmin > nmax
1177 then bound nmin 0 (state.pagecount-1)
1178 else
1179 let n = (nmax + nmin) / 2 in
1180 let vy, h = rowyh cl b n in
1181 let y0, y1 =
1182 if conf.presentation
1183 then
1184 let ips = calcips h in
1185 let y0 = vy - ips in
1186 let y1 = vy + h + ips in
1187 y0, y1
1188 else (
1189 if n = 0
1190 then 0, vy + h + conf.interpagespace
1191 else
1192 let y0 = vy - conf.interpagespace in
1193 y0, y0 + h + conf.interpagespace
1196 if y >= y0 && y < y1
1197 then (
1198 if c = 1
1199 then n
1200 else (
1201 if n > coverA
1202 then
1203 if n < state.pagecount - coverB
1204 then ((n-coverA)/c)*c + coverA
1205 else n
1206 else n
1209 else (
1210 if y > y0
1211 then bsearch (n+1) nmax
1212 else bsearch nmin (n-1)
1215 let r = bsearch 0 (state.pagecount-1) in
1219 let layoutN ((columns, coverA, coverB), b) y sh =
1220 let sh = sh - state.hscrollh in
1221 let rec fold accu n =
1222 if n = Array.length b
1223 then accu
1224 else
1225 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1226 if (vy - y) > sh &&
1227 (n = coverA - 1
1228 || n = state.pagecount - coverB
1229 || (n - coverA) mod columns = columns - 1)
1230 then accu
1231 else
1232 let accu =
1233 if vy + h > y
1234 then
1235 let pagey = max 0 (y - vy) in
1236 let pagedispy = if pagey > 0 then 0 else vy - y in
1237 let pagedispx, pagex =
1238 let pdx =
1239 if n = coverA - 1 || n = state.pagecount - coverB
1240 then state.x + (state.winw - state.scrollw - w) / 2
1241 else dx + xoff + state.x
1243 if pdx < 0
1244 then 0, -pdx
1245 else pdx, 0
1247 let pagevw =
1248 let vw = state.winw - state.scrollw - pagedispx in
1249 let pw = w - pagex in
1250 min vw pw
1252 let pagevh = min (h - pagey) (sh - pagedispy) in
1253 if pagevw > 0 && pagevh > 0
1254 then
1255 let e =
1256 { pageno = n
1257 ; pagedimno = pdimno
1258 ; pagew = w
1259 ; pageh = h
1260 ; pagex = pagex
1261 ; pagey = pagey
1262 ; pagevw = pagevw
1263 ; pagevh = pagevh
1264 ; pagedispx = pagedispx
1265 ; pagedispy = pagedispy
1266 ; pagecol = 0
1269 e :: accu
1270 else
1271 accu
1272 else
1273 accu
1275 fold accu (n+1)
1277 List.rev (fold [] (page_of_y y));
1280 let layoutS (columns, b) y sh =
1281 let sh = sh - state.hscrollh in
1282 let rec fold accu n =
1283 if n = Array.length b
1284 then accu
1285 else
1286 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1287 if (vy - y) > sh
1288 then accu
1289 else
1290 let accu =
1291 if vy + pageh > y
1292 then
1293 let x = xoff + state.x in
1294 let pagey = max 0 (y - vy) in
1295 let pagedispy = if pagey > 0 then 0 else vy - y in
1296 let pagedispx, pagex =
1297 if px = 0
1298 then (
1299 if x < 0
1300 then 0, -x
1301 else x, 0
1303 else (
1304 let px = px - x in
1305 if px < 0
1306 then -px, 0
1307 else 0, px
1310 let pagecolw = pagew/columns in
1311 let pagedispx =
1312 if pagecolw < state.winw
1313 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1314 else pagedispx
1316 let pagevw =
1317 let vw = state.winw - pagedispx - state.scrollw in
1318 let pw = pagew - pagex in
1319 min vw pw
1321 let pagevw = min pagevw pagecolw in
1322 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1323 if pagevw > 0 && pagevh > 0
1324 then
1325 let e =
1326 { pageno = n/columns
1327 ; pagedimno = pdimno
1328 ; pagew = pagew
1329 ; pageh = pageh
1330 ; pagex = pagex
1331 ; pagey = pagey
1332 ; pagevw = pagevw
1333 ; pagevh = pagevh
1334 ; pagedispx = pagedispx
1335 ; pagedispy = pagedispy
1336 ; pagecol = n mod columns
1339 e :: accu
1340 else
1341 accu
1342 else
1343 accu
1345 fold accu (n+1)
1347 List.rev (fold [] 0)
1350 let layout y sh =
1351 if nogeomcmds state.geomcmds
1352 then
1353 match conf.columns with
1354 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1355 | Cmulti c -> layoutN c y sh
1356 | Csplit s -> layoutS s y sh
1357 else []
1360 let clamp incr =
1361 let y = state.y + incr in
1362 let y = max 0 y in
1363 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1367 let itertiles l f =
1368 let tilex = l.pagex mod conf.tilew in
1369 let tiley = l.pagey mod conf.tileh in
1371 let col = l.pagex / conf.tilew in
1372 let row = l.pagey / conf.tileh in
1374 let rec rowloop row y0 dispy h =
1375 if h = 0
1376 then ()
1377 else (
1378 let dh = conf.tileh - y0 in
1379 let dh = min h dh in
1380 let rec colloop col x0 dispx w =
1381 if w = 0
1382 then ()
1383 else (
1384 let dw = conf.tilew - x0 in
1385 let dw = min w dw in
1387 f col row dispx dispy x0 y0 dw dh;
1388 colloop (col+1) 0 (dispx+dw) (w-dw)
1391 colloop col tilex l.pagedispx l.pagevw;
1392 rowloop (row+1) 0 (dispy+dh) (h-dh)
1395 if l.pagevw > 0 && l.pagevh > 0
1396 then rowloop row tiley l.pagedispy l.pagevh;
1399 let gettileopaque l col row =
1400 let key =
1401 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1403 try Some (Hashtbl.find state.tilemap key)
1404 with Not_found -> None
1407 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1408 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1409 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1412 let drawtiles l color =
1413 GlDraw.color color;
1414 let f col row x y tilex tiley w h =
1415 match gettileopaque l col row with
1416 | Some (opaque, _, t) ->
1417 let params = x, y, w, h, tilex, tiley in
1418 if conf.invert
1419 then (
1420 Gl.enable `blend;
1421 GlFunc.blend_func `zero `one_minus_src_color;
1423 drawtile params opaque;
1424 if conf.invert
1425 then Gl.disable `blend;
1426 if conf.debug
1427 then (
1428 let s = Printf.sprintf
1429 "%d[%d,%d] %f sec"
1430 l.pageno col row t
1432 let w = measurestr fstate.fontsize s in
1433 GlMisc.push_attrib [`current];
1434 GlDraw.color (0.0, 0.0, 0.0);
1435 GlDraw.rect
1436 (float (x-2), float (y-2))
1437 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1438 GlDraw.color (1.0, 1.0, 1.0);
1439 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1440 GlMisc.pop_attrib ();
1443 | _ ->
1444 let w =
1445 let lw = state.winw - state.scrollw - x in
1446 min lw w
1447 and h =
1448 let lh = state.winh - y in
1449 min lh h
1451 begin match state.texid with
1452 | Some id ->
1453 Gl.enable `texture_2d;
1454 GlTex.bind_texture `texture_2d id;
1455 let x0 = float x
1456 and y0 = float y
1457 and x1 = float (x+w)
1458 and y1 = float (y+h) in
1460 let tw = float w /. 16.0
1461 and th = float h /. 16.0 in
1462 let tx0 = float tilex /. 16.0
1463 and ty0 = float tiley /. 16.0 in
1464 let tx1 = tx0 +. tw
1465 and ty1 = ty0 +. th in
1466 GlDraw.begins `quads;
1467 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1468 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1469 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1470 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1471 GlDraw.ends ();
1473 Gl.disable `texture_2d;
1474 | None ->
1475 GlDraw.color (1.0, 1.0, 1.0);
1476 GlDraw.rect
1477 (float x, float y)
1478 (float (x+w), float (y+h));
1479 end;
1480 if w > 128 && h > fstate.fontsize + 10
1481 then (
1482 GlDraw.color (0.0, 0.0, 0.0);
1483 let c, r =
1484 if conf.verbose
1485 then (col*conf.tilew, row*conf.tileh)
1486 else col, row
1488 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1490 GlDraw.color color;
1492 itertiles l f
1495 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1497 let tilevisible1 l x y =
1498 let ax0 = l.pagex
1499 and ax1 = l.pagex + l.pagevw
1500 and ay0 = l.pagey
1501 and ay1 = l.pagey + l.pagevh in
1503 let bx0 = x
1504 and by0 = y in
1505 let bx1 = min (bx0 + conf.tilew) l.pagew
1506 and by1 = min (by0 + conf.tileh) l.pageh in
1508 let rx0 = max ax0 bx0
1509 and ry0 = max ay0 by0
1510 and rx1 = min ax1 bx1
1511 and ry1 = min ay1 by1 in
1513 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1514 nonemptyintersection
1517 let tilevisible layout n x y =
1518 let rec findpageinlayout m = function
1519 | l :: rest when l.pageno = n ->
1520 tilevisible1 l x y || (
1521 match conf.columns with
1522 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1523 | _ -> false
1525 | _ :: rest -> findpageinlayout 0 rest
1526 | [] -> false
1528 findpageinlayout 0 layout;
1531 let tileready l x y =
1532 tilevisible1 l x y &&
1533 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1536 let tilepage n p layout =
1537 let rec loop = function
1538 | l :: rest ->
1539 if l.pageno = n
1540 then
1541 let f col row _ _ _ _ _ _ =
1542 if state.currently = Idle
1543 then
1544 match gettileopaque l col row with
1545 | Some _ -> ()
1546 | None ->
1547 let x = col*conf.tilew
1548 and y = row*conf.tileh in
1549 let w =
1550 let w = l.pagew - x in
1551 min w conf.tilew
1553 let h =
1554 let h = l.pageh - y in
1555 min h conf.tileh
1557 let pbo =
1558 if conf.usepbo
1559 then getpbo w h conf.colorspace
1560 else "0"
1562 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1563 state.currently <-
1564 Tiling (
1565 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1566 conf.tilew, conf.tileh
1569 itertiles l f;
1570 else
1571 loop rest
1573 | [] -> ()
1575 if nogeomcmds state.geomcmds
1576 then loop layout;
1579 let preloadlayout y =
1580 let y = if y < state.winh then 0 else y - state.winh in
1581 let h = state.winh*3 in
1582 layout y h;
1585 let load pages =
1586 let rec loop pages =
1587 if state.currently != Idle
1588 then ()
1589 else
1590 match pages with
1591 | l :: rest ->
1592 begin match getopaque l.pageno with
1593 | None ->
1594 wcmd "page %d %d" l.pageno l.pagedimno;
1595 state.currently <- Loading (l, state.gen);
1596 | Some opaque ->
1597 tilepage l.pageno opaque pages;
1598 loop rest
1599 end;
1600 | _ -> ()
1602 if nogeomcmds state.geomcmds
1603 then loop pages
1606 let preload pages =
1607 load pages;
1608 if conf.preload && state.currently = Idle
1609 then load (preloadlayout state.y);
1612 let layoutready layout =
1613 let rec fold all ls =
1614 all && match ls with
1615 | l :: rest ->
1616 let seen = ref false in
1617 let allvisible = ref true in
1618 let foo col row _ _ _ _ _ _ =
1619 seen := true;
1620 allvisible := !allvisible &&
1621 begin match gettileopaque l col row with
1622 | Some _ -> true
1623 | None -> false
1626 itertiles l foo;
1627 fold (!seen && !allvisible) rest
1628 | [] -> true
1630 let alltilesvisible = fold true layout in
1631 alltilesvisible;
1634 let gotoy y =
1635 state.wthack <- false;
1636 let y = bound y 0 state.maxy in
1637 let y, layout, proceed =
1638 match conf.maxwait with
1639 | Some time when state.ghyll == noghyll ->
1640 begin match state.throttle with
1641 | None ->
1642 let layout = layout y state.winh in
1643 let ready = layoutready layout in
1644 if not ready
1645 then (
1646 load layout;
1647 state.throttle <- Some (layout, y, now ());
1649 else G.postRedisplay "gotoy showall (None)";
1650 y, layout, ready
1651 | Some (_, _, started) ->
1652 let dt = now () -. started in
1653 if dt > time
1654 then (
1655 state.throttle <- None;
1656 let layout = layout y state.winh in
1657 load layout;
1658 G.postRedisplay "maxwait";
1659 y, layout, true
1661 else -1, [], false
1664 | _ ->
1665 let layout = layout y state.winh in
1666 G.postRedisplay "gotoy ready";
1667 y, layout, true
1669 if proceed
1670 then (
1671 state.y <- y;
1672 state.layout <- layout;
1673 begin match state.mode with
1674 | LinkNav (Ltexact (pageno, linkno)) ->
1675 let rec loop = function
1676 | [] ->
1677 state.mode <- LinkNav (Ltgendir 0)
1678 | l :: _ when l.pageno = pageno ->
1679 begin match getopaque pageno with
1680 | None ->
1681 state.mode <- LinkNav (Ltgendir 0)
1682 | Some opaque ->
1683 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1684 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1685 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1686 then state.mode <- LinkNav (Ltgendir 0)
1688 | _ :: rest -> loop rest
1690 loop layout
1691 | _ -> ()
1692 end;
1693 begin match state.mode with
1694 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1695 if not (pagevisible layout pageno)
1696 then (
1697 match state.layout with
1698 | [] -> ()
1699 | l :: _ ->
1700 state.mode <- Birdseye (
1701 conf, leftx, l.pageno, hooverpageno, anchor
1704 | LinkNav (Ltgendir dir as lt) ->
1705 let linknav =
1706 let rec loop = function
1707 | [] -> lt
1708 | l :: rest ->
1709 match getopaque l.pageno with
1710 | None -> loop rest
1711 | Some opaque ->
1712 let link =
1713 let ld =
1714 if dir = 0
1715 then LDfirstvisible (l.pagex, l.pagey, dir)
1716 else (
1717 if dir > 0 then LDfirst else LDlast
1720 findlink opaque ld
1722 match link with
1723 | Lnotfound -> loop rest
1724 | Lfound n ->
1725 showlinktype (getlink opaque n);
1726 Ltexact (l.pageno, n)
1728 loop state.layout
1730 state.mode <- LinkNav linknav
1731 | _ -> ()
1732 end;
1733 preload layout;
1735 state.ghyll <- noghyll;
1736 if conf.updatecurs
1737 then (
1738 let mx, my = state.mpos in
1739 updateunder mx my;
1743 let conttiling pageno opaque =
1744 tilepage pageno opaque
1745 (if conf.preload then preloadlayout state.y else state.layout)
1748 let gotoy_and_clear_text y =
1749 if not conf.verbose then state.text <- "";
1750 gotoy y;
1753 let getanchor1 l =
1754 let top =
1755 let coloff = l.pagecol * l.pageh in
1756 float (l.pagey + coloff) /. float l.pageh
1758 let dtop =
1759 if l.pagedispy = 0
1760 then
1762 else
1763 if conf.presentation
1764 then float l.pagedispy /. float (calcips l.pageh)
1765 else float l.pagedispy /. float conf.interpagespace
1767 (l.pageno, top, dtop)
1770 let getanchor () =
1771 match state.layout with
1772 | l :: _ -> getanchor1 l
1773 | [] ->
1774 let n = page_of_y state.y in
1775 if n = -1
1776 then state.anchor
1777 else
1778 let y, h = getpageyh n in
1779 let dy = y - state.y in
1780 let dtop =
1781 if conf.presentation
1782 then
1783 let ips = calcips h in
1784 float (dy + ips) /. float ips
1785 else
1786 float dy /. float conf.interpagespace
1788 (n, 0.0, dtop)
1791 let getanchory (n, top, dtop) =
1792 let y, h = getpageyh n in
1793 if conf.presentation
1794 then
1795 let ips = calcips h in
1796 y + truncate (top*.float h -. dtop*.float ips) + ips;
1797 else
1798 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1801 let gotoanchor anchor =
1802 gotoy (getanchory anchor);
1805 let addnav () =
1806 cbput state.hists.nav (getanchor ());
1809 let getnav dir =
1810 let anchor = cbgetc state.hists.nav dir in
1811 getanchory anchor;
1814 let gotoghyll y =
1815 let scroll f n a b =
1816 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1817 let snake f a b =
1818 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1819 if f < a
1820 then s (float f /. float a)
1821 else (
1822 if f > b
1823 then 1.0 -. s ((float (f-b) /. float (n-b)))
1824 else 1.0
1827 snake f a b
1828 and summa f n a b =
1829 (* courtesy:
1830 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1831 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1832 let iv1 = iv f in
1833 let ins = float a *. iv1
1834 and outs = float (n-b) *. iv1 in
1835 let ones = b - a in
1836 ins +. outs +. float ones
1838 let rec set (_N, _A, _B) y sy =
1839 let sum = summa 1.0 _N _A _B in
1840 let dy = float (y - sy) in
1841 state.ghyll <- (
1842 let rec gf n y1 o =
1843 if n >= _N
1844 then state.ghyll <- noghyll
1845 else
1846 let go n =
1847 let s = scroll n _N _A _B in
1848 let y1 = y1 +. ((s *. dy) /. sum) in
1849 gotoy_and_clear_text (truncate y1);
1850 state.ghyll <- gf (n+1) y1;
1852 match o with
1853 | None -> go n
1854 | Some y' -> set (_N/2, 1, 1) y' state.y
1856 gf 0 (float state.y)
1859 match conf.ghyllscroll with
1860 | None ->
1861 gotoy_and_clear_text y
1862 | Some nab ->
1863 if state.ghyll == noghyll
1864 then set nab y state.y
1865 else state.ghyll (Some y)
1868 let gotopage n top =
1869 let y, h = getpageyh n in
1870 let y = y + (truncate (top *. float h)) in
1871 gotoghyll y
1874 let gotopage1 n top =
1875 let y = getpagey n in
1876 let y = y + top in
1877 gotoghyll y
1880 let invalidate s f =
1881 state.layout <- [];
1882 state.pdims <- [];
1883 state.rects <- [];
1884 state.rects1 <- [];
1885 match state.geomcmds with
1886 | ps, [] when String.length ps = 0 ->
1887 f ();
1888 state.geomcmds <- s, [];
1890 | ps, [] ->
1891 state.geomcmds <- ps, [s, f];
1893 | ps, (s', _) :: rest when s' = s ->
1894 state.geomcmds <- ps, ((s, f) :: rest);
1896 | ps, cmds ->
1897 state.geomcmds <- ps, ((s, f) :: cmds);
1900 let flushpages () =
1901 Hashtbl.iter (fun _ opaque ->
1902 wcmd "freepage %s" opaque;
1903 ) state.pagemap;
1904 Hashtbl.clear state.pagemap;
1907 let opendoc path password =
1908 state.path <- path;
1909 state.password <- password;
1910 state.gen <- state.gen + 1;
1911 state.docinfo <- [];
1913 flushpages ();
1914 setaalevel conf.aalevel;
1915 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1916 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1917 invalidate "reqlayout"
1918 (fun () ->
1919 wcmd "reqlayout %d %d %s\000"
1920 conf.angle (btod conf.proportional) state.nameddest;
1924 let reload () =
1925 state.anchor <- getanchor ();
1926 state.wthack <- !wtmode;
1927 opendoc state.path state.password;
1930 let scalecolor c =
1931 let c = c *. conf.colorscale in
1932 (c, c, c);
1935 let scalecolor2 (r, g, b) =
1936 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1939 let docolumns = function
1940 | Csingle _ ->
1941 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1942 let rec loop pageno pdimno pdim y ph pdims =
1943 if pageno = state.pagecount
1944 then ()
1945 else
1946 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1947 match pdims with
1948 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1949 pdimno+1, pdim, rest
1950 | _ ->
1951 pdimno, pdim, pdims
1953 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1954 let y = y +
1955 (if conf.presentation
1956 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1957 else (if pageno = 0 then 0 else conf.interpagespace)
1960 a.(pageno) <- (pdimno, x, y, pdim);
1961 loop (pageno+1) pdimno pdim (y + h) h pdims
1963 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1964 conf.columns <- Csingle a;
1966 | Cmulti ((columns, coverA, coverB), _) ->
1967 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1968 let rec loop pageno pdimno pdim x y rowh pdims =
1969 let rec fixrow m = if m = pageno then () else
1970 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1971 if h < rowh
1972 then (
1973 let y = y + (rowh - h) / 2 in
1974 a.(m) <- (pdimno, x, y, pdim);
1976 fixrow (m+1)
1978 if pageno = state.pagecount
1979 then fixrow (((pageno - 1) / columns) * columns)
1980 else
1981 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1982 match pdims with
1983 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1984 pdimno+1, pdim, rest
1985 | _ ->
1986 pdimno, pdim, pdims
1988 let x, y, rowh' =
1989 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1990 then (
1991 let x = (state.winw - state.scrollw - w) / 2 in
1992 let ips =
1993 if conf.presentation then calcips h else conf.interpagespace in
1994 x, y + ips + rowh, h
1996 else (
1997 if (pageno - coverA) mod columns = 0
1998 then (
1999 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
2000 let y =
2001 if conf.presentation
2002 then
2003 let ips = calcips h in
2004 y + (if pageno = 0 then 0 else calcips rowh + ips)
2005 else
2006 y + (if pageno = 0 then 0 else conf.interpagespace)
2008 x, y + rowh, h
2010 else x, y, max rowh h
2013 let y =
2014 if pageno > 1 && (pageno - coverA) mod columns = 0
2015 then (
2016 let y =
2017 if pageno = columns && conf.presentation
2018 then (
2019 let ips = calcips rowh in
2020 for i = 0 to pred columns
2022 let (pdimno, x, y, pdim) = a.(i) in
2023 a.(i) <- (pdimno, x, y+ips, pdim)
2024 done;
2025 y+ips;
2027 else y
2029 fixrow (pageno - columns);
2032 else y
2034 a.(pageno) <- (pdimno, x, y, pdim);
2035 let x = x + w + xoff*2 + conf.interpagespace in
2036 loop (pageno+1) pdimno pdim x y rowh' pdims
2038 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2039 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2041 | Csplit (c, _) ->
2042 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2043 let rec loop pageno pdimno pdim y pdims =
2044 if pageno = state.pagecount
2045 then ()
2046 else
2047 let pdimno, ((_, w, h, _) as pdim), pdims =
2048 match pdims with
2049 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2050 pdimno+1, pdim, rest
2051 | _ ->
2052 pdimno, pdim, pdims
2054 let cw = w / c in
2055 let rec loop1 n x y =
2056 if n = c then y else (
2057 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2058 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2061 let y = loop1 0 0 y in
2062 loop (pageno+1) pdimno pdim y pdims
2064 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2065 conf.columns <- Csplit (c, a);
2068 let represent () =
2069 docolumns conf.columns;
2070 state.maxy <- calcheight ();
2071 state.hscrollh <-
2072 if state.w <= state.winw - state.scrollw
2073 then 0
2074 else state.scrollw
2076 if state.reprf == noreprf
2077 then
2078 let wthack = state.wthack in
2079 begin match state.mode with
2080 | Birdseye (_, _, pageno, _, _) ->
2081 let y, h = getpageyh pageno in
2082 let top = (state.winh - h) / 2 in
2083 gotoy (max 0 (y - top))
2084 | _ -> gotoanchor state.anchor
2085 end;
2086 state.wthack <- wthack;
2087 state.reprf <- noreprf;
2088 else state.reprf ()
2091 let reshape w h =
2092 state.wthack <- false;
2093 GlDraw.viewport 0 0 w h;
2094 let firsttime = state.geomcmds == firstgeomcmds in
2095 if not firsttime && nogeomcmds state.geomcmds
2096 then state.anchor <- getanchor ();
2098 state.winw <- w;
2099 let w = truncate (float w *. conf.zoom) - state.scrollw in
2100 let w = max w 2 in
2101 state.winh <- h;
2102 setfontsize fstate.fontsize;
2103 GlMat.mode `modelview;
2104 GlMat.load_identity ();
2106 GlMat.mode `projection;
2107 GlMat.load_identity ();
2108 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2109 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2110 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2112 let relx =
2113 if conf.zoom <= 1.0
2114 then 0.0
2115 else float state.x /. float state.w
2117 invalidate "geometry"
2118 (fun () ->
2119 state.w <- w;
2120 if not firsttime
2121 then state.x <- truncate (relx *. float w);
2122 let w =
2123 match conf.columns with
2124 | Csingle _ -> w
2125 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2126 | Csplit (c, _) -> w * c
2128 wcmd "geometry %d %d" w h);
2131 let enttext () =
2132 let len = String.length state.text in
2133 let drawstring s =
2134 let hscrollh =
2135 match state.mode with
2136 | Textentry _
2137 | View ->
2138 let h, _, _ = state.uioh#scrollpw in
2140 | _ -> 0
2142 let rect x w =
2143 GlDraw.rect
2144 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2145 (x+.w, float (state.winh - hscrollh))
2148 let w = float (state.winw - state.scrollw - 1) in
2149 if state.progress >= 0.0 && state.progress < 1.0
2150 then (
2151 GlDraw.color (0.3, 0.3, 0.3);
2152 let w1 = w *. state.progress in
2153 rect 0.0 w1;
2154 GlDraw.color (0.0, 0.0, 0.0);
2155 rect w1 (w-.w1)
2157 else (
2158 GlDraw.color (0.0, 0.0, 0.0);
2159 rect 0.0 w;
2162 GlDraw.color (1.0, 1.0, 1.0);
2163 drawstring fstate.fontsize
2164 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2166 let s =
2167 match state.mode with
2168 | Textentry ((prefix, text, _, _, _, _), _) ->
2169 let s =
2170 if len > 0
2171 then
2172 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2173 else
2174 Printf.sprintf "%s%s_" prefix text
2178 | _ -> state.text
2180 let s =
2181 if state.newerrmsgs
2182 then (
2183 if not (istextentry state.mode)
2184 then
2185 let s1 = "(press 'e' to review error messasges)" in
2186 if String.length s > 0 then s ^ " " ^ s1 else s1
2187 else s
2189 else s
2191 if String.length s > 0
2192 then drawstring s
2195 let gctiles () =
2196 let len = Queue.length state.tilelru in
2197 let layout = lazy (
2198 match state.throttle with
2199 | None ->
2200 if conf.preload
2201 then preloadlayout state.y
2202 else state.layout
2203 | Some (layout, _, _) ->
2204 layout
2205 ) in
2206 let rec loop qpos =
2207 if state.memused <= conf.memlimit
2208 then ()
2209 else (
2210 if qpos < len
2211 then
2212 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2213 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2214 let (_, pw, ph, _) = getpagedim n in
2216 gen = state.gen
2217 && colorspace = conf.colorspace
2218 && angle = conf.angle
2219 && pagew = pw
2220 && pageh = ph
2221 && (
2222 let x = col*conf.tilew
2223 and y = row*conf.tileh in
2224 tilevisible (Lazy.force_val layout) n x y
2226 then Queue.push lruitem state.tilelru
2227 else (
2228 freepbo p;
2229 wcmd "freetile %s" p;
2230 state.memused <- state.memused - s;
2231 state.uioh#infochanged Memused;
2232 Hashtbl.remove state.tilemap k;
2234 loop (qpos+1)
2237 loop 0
2240 let flushtiles () =
2241 Queue.iter (fun (k, p, s) ->
2242 wcmd "freetile %s" p;
2243 state.memused <- state.memused - s;
2244 state.uioh#infochanged Memused;
2245 Hashtbl.remove state.tilemap k;
2246 ) state.tilelru;
2247 Queue.clear state.tilelru;
2248 load state.layout;
2251 let logcurrently = function
2252 | Idle -> dolog "Idle"
2253 | Loading (l, gen) ->
2254 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2255 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2256 dolog
2257 "Tiling %d[%d,%d] page=%s cs=%s angle"
2258 l.pageno col row pageopaque
2259 (colorspace_to_string colorspace)
2261 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2262 angle gen conf.angle state.gen
2263 tilew tileh
2264 conf.tilew conf.tileh
2266 | Outlining _ ->
2267 dolog "outlining"
2270 let splitatspace =
2271 let r = Str.regexp " " in
2272 fun s -> Str.bounded_split r s 2;
2275 let onpagerect pageno f =
2276 let b =
2277 match conf.columns with
2278 | Cmulti (_, b) -> b
2279 | Csingle b -> b
2280 | Csplit (_, b) -> b
2282 if pageno >= 0 && pageno < Array.length b
2283 then
2284 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2285 let r = getpdimrect pdimno in
2286 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2289 let gotopagexy pageno x y =
2290 onpagerect pageno (fun w h ->
2291 let top = y /. h in
2292 let _,w1,_,leftx = getpagedim pageno in
2293 let sw = float w1 /. w in
2294 let x = sw *. x in
2295 let x = leftx + state.x + truncate x in
2296 let newpan =
2297 if x < 0 || x >= state.winw - state.scrollw
2298 then (state.x <- state.x - x; true)
2299 else false
2301 let y, h = getpageyh pageno in
2302 let y' = y + truncate (top *. float h) in
2303 let dy = y' - state.y in
2304 if newpan || not (dy > 0 && dy < state.winh - state.hscrollh)
2305 then (
2306 let y =
2307 if conf.presentation
2308 then
2309 if abs (y - y') > state.winh - state.hscrollh
2310 then y'
2311 else y
2312 else y';
2314 gotoy y;
2315 state.wthack <- !wtmode && not (layoutready state.layout);
2320 let act cmds =
2321 (* dolog "%S" cmds; *)
2322 let cl = splitatspace cmds in
2323 let scan s fmt f =
2324 try Scanf.sscanf s fmt f
2325 with exn ->
2326 dolog "error processing '%S': %s" cmds (Printexc.to_string exn);
2327 exit 1
2329 match cl with
2330 | "clear" :: [] ->
2331 state.uioh#infochanged Pdim;
2332 state.pdims <- [];
2334 | "clearrects" :: [] ->
2335 state.rects <- state.rects1;
2336 G.postRedisplay "clearrects";
2338 | "continue" :: args :: [] ->
2339 let n = scan args "%u" (fun n -> n) in
2340 state.pagecount <- n;
2341 begin match state.currently with
2342 | Outlining l ->
2343 state.currently <- Idle;
2344 state.outlines <- Array.of_list (List.rev l)
2345 | _ -> ()
2346 end;
2348 let cur, cmds = state.geomcmds in
2349 if String.length cur = 0
2350 then failwith "umpossible";
2352 begin match List.rev cmds with
2353 | [] ->
2354 state.geomcmds <- "", [];
2355 represent ();
2356 | (s, f) :: rest ->
2357 f ();
2358 state.geomcmds <- s, List.rev rest;
2359 end;
2360 if conf.maxwait = None
2361 then G.postRedisplay "continue";
2363 | "title" :: args :: [] ->
2364 Wsi.settitle args
2366 | "msg" :: args :: [] ->
2367 showtext ' ' args
2369 | "vmsg" :: args :: [] ->
2370 if conf.verbose
2371 then showtext ' ' args
2373 | "emsg" :: args :: [] ->
2374 Buffer.add_string state.errmsgs args;
2375 state.newerrmsgs <- true;
2376 G.postRedisplay "error message"
2378 | "progress" :: args :: [] ->
2379 let progress, text =
2380 scan args "%f %n"
2381 (fun f pos ->
2382 f, String.sub args pos (String.length args - pos))
2384 state.text <- text;
2385 state.progress <- progress;
2386 G.postRedisplay "progress"
2388 | "firstmatch" :: args :: [] ->
2389 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2390 scan args "%u %d %f %f %f %f %f %f %f %f"
2391 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2392 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2394 let y = (getpagey pageno) + truncate y0 in
2395 addnav ();
2396 gotoy y;
2397 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2399 | "match" :: args :: [] ->
2400 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2401 scan args "%u %d %f %f %f %f %f %f %f %f"
2402 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2403 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2405 state.rects1 <-
2406 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2408 | "page" :: args :: [] ->
2409 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2410 begin match state.currently with
2411 | Loading (l, gen) ->
2412 vlog "page %d took %f sec" l.pageno t;
2413 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2414 begin match state.throttle with
2415 | None ->
2416 let preloadedpages =
2417 if conf.preload
2418 then preloadlayout state.y
2419 else state.layout
2421 let evict () =
2422 let module IntSet =
2423 Set.Make (struct type t = int let compare = (-) end) in
2424 let set =
2425 List.fold_left (fun s l -> IntSet.add l.pageno s)
2426 IntSet.empty preloadedpages
2428 let evictedpages =
2429 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2430 if not (IntSet.mem pageno set)
2431 then (
2432 wcmd "freepage %s" opaque;
2433 key :: accu
2435 else accu
2436 ) state.pagemap []
2438 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2440 evict ();
2441 state.currently <- Idle;
2442 if gen = state.gen
2443 then (
2444 tilepage l.pageno pageopaque state.layout;
2445 load state.layout;
2446 load preloadedpages;
2447 if pagevisible state.layout l.pageno
2448 && layoutready state.layout
2449 then G.postRedisplay "page";
2452 | Some (layout, _, _) ->
2453 state.currently <- Idle;
2454 tilepage l.pageno pageopaque layout;
2455 load state.layout
2456 end;
2458 | _ ->
2459 dolog "Inconsistent loading state";
2460 logcurrently state.currently;
2461 exit 1
2464 | "tile" :: args :: [] ->
2465 let (x, y, opaque, size, t) =
2466 scan args "%u %u %s %u %f"
2467 (fun x y p size t -> (x, y, p, size, t))
2469 begin match state.currently with
2470 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2471 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2473 unmappbo opaque;
2474 if tilew != conf.tilew || tileh != conf.tileh
2475 then (
2476 wcmd "freetile %s" opaque;
2477 state.currently <- Idle;
2478 load state.layout;
2480 else (
2481 puttileopaque l col row gen cs angle opaque size t;
2482 state.memused <- state.memused + size;
2483 state.uioh#infochanged Memused;
2484 gctiles ();
2485 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2486 opaque, size) state.tilelru;
2488 let layout =
2489 match state.throttle with
2490 | None -> state.layout
2491 | Some (layout, _, _) -> layout
2494 state.currently <- Idle;
2495 if gen = state.gen
2496 && conf.colorspace = cs
2497 && conf.angle = angle
2498 && tilevisible layout l.pageno x y
2499 then conttiling l.pageno pageopaque;
2501 begin match state.throttle with
2502 | None ->
2503 if state.wthack
2504 then state.wthack <- not (layoutready state.layout);
2505 preload state.layout;
2506 if gen = state.gen
2507 && conf.colorspace = cs
2508 && conf.angle = angle
2509 && tilevisible state.layout l.pageno x y
2510 then G.postRedisplay "tile nothrottle";
2512 | Some (layout, y, _) ->
2513 let ready = layoutready layout in
2514 if ready
2515 then (
2516 state.wthack <- false;
2517 state.y <- y;
2518 state.layout <- layout;
2519 state.throttle <- None;
2520 G.postRedisplay "throttle";
2522 else load layout;
2523 end;
2526 | _ ->
2527 dolog "Inconsistent tiling state";
2528 logcurrently state.currently;
2529 exit 1
2532 | "pdim" :: args :: [] ->
2533 let pdim =
2534 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2536 state.uioh#infochanged Pdim;
2537 state.pdims <- pdim :: state.pdims
2539 | "o" :: args :: [] ->
2540 let (l, n, t, h, pos) =
2541 scan args "%u %u %d %u %n"
2542 (fun l n t h pos -> l, n, t, h, pos)
2544 let s = String.sub args pos (String.length args - pos) in
2545 let outline = (s, l, (n, float t /. float h, 0.0)) in
2546 begin match state.currently with
2547 | Outlining outlines ->
2548 state.currently <- Outlining (outline :: outlines)
2549 | Idle ->
2550 state.currently <- Outlining [outline]
2551 | currently ->
2552 dolog "invalid outlining state";
2553 logcurrently currently
2556 | "a" :: args :: [] ->
2557 let (n, l, t) =
2558 scan args "%u %d %d" (fun n l t -> n, l, t)
2560 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2562 | "info" :: args :: [] ->
2563 state.docinfo <- (1, args) :: state.docinfo
2565 | "infoend" :: [] ->
2566 state.uioh#infochanged Docinfo;
2567 state.docinfo <- List.rev state.docinfo
2569 | _ ->
2570 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2573 let onhist cb =
2574 let rc = cb.rc in
2575 let action = function
2576 | HCprev -> cbget cb ~-1
2577 | HCnext -> cbget cb 1
2578 | HCfirst -> cbget cb ~-(cb.rc)
2579 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2580 and cancel () = cb.rc <- rc
2581 in (action, cancel)
2584 let search pattern forward =
2585 if String.length pattern > 0
2586 then
2587 let pn, py =
2588 match state.layout with
2589 | [] -> 0, 0
2590 | l :: _ ->
2591 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2593 wcmd "search %d %d %d %d,%s\000"
2594 (btod conf.icase) pn py (btod forward) pattern;
2597 let intentry text key =
2598 let c =
2599 if key >= 32 && key < 127
2600 then Char.chr key
2601 else '\000'
2603 match c with
2604 | '0' .. '9' ->
2605 let text = addchar text c in
2606 TEcont text
2608 | _ ->
2609 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2610 TEcont text
2613 let linknentry text key =
2614 let c =
2615 if key >= 32 && key < 127
2616 then Char.chr key
2617 else '\000'
2619 match c with
2620 | 'a' .. 'z' ->
2621 let text = addchar text c in
2622 TEcont text
2624 | _ ->
2625 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2626 TEcont text
2629 let linkndone f s =
2630 if String.length s > 0
2631 then (
2632 let n =
2633 let l = String.length s in
2634 let rec loop pos n = if pos = l then n else
2635 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2636 loop (pos+1) (n*26 + m)
2637 in loop 0 0
2639 let rec loop n = function
2640 | [] -> ()
2641 | l :: rest ->
2642 match getopaque l.pageno with
2643 | None -> loop n rest
2644 | Some opaque ->
2645 let m = getlinkcount opaque in
2646 if n < m
2647 then (
2648 let under = getlink opaque n in
2649 f under
2651 else loop (n-m) rest
2653 loop n state.layout;
2657 let textentry text key =
2658 if key land 0xff00 = 0xff00
2659 then TEcont text
2660 else TEcont (text ^ Wsi.toutf8 key)
2663 let reqlayout angle proportional =
2664 match state.throttle with
2665 | None ->
2666 if nogeomcmds state.geomcmds
2667 then state.anchor <- getanchor ();
2668 conf.angle <- angle mod 360;
2669 if conf.angle != 0
2670 then (
2671 match state.mode with
2672 | LinkNav _ -> state.mode <- View
2673 | _ -> ()
2675 conf.proportional <- proportional;
2676 invalidate "reqlayout"
2677 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2678 | _ -> ()
2681 let settrim trimmargins trimfuzz =
2682 if nogeomcmds state.geomcmds
2683 then state.anchor <- getanchor ();
2684 conf.trimmargins <- trimmargins;
2685 conf.trimfuzz <- trimfuzz;
2686 let x0, y0, x1, y1 = trimfuzz in
2687 invalidate "settrim"
2688 (fun () ->
2689 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2690 flushpages ();
2693 let setzoom zoom =
2694 match state.throttle with
2695 | None ->
2696 let zoom = max 0.01 zoom in
2697 if zoom <> conf.zoom
2698 then (
2699 state.prevzoom <- conf.zoom;
2700 conf.zoom <- zoom;
2701 reshape state.winw state.winh;
2702 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2705 | Some (layout, y, started) ->
2706 let time =
2707 match conf.maxwait with
2708 | None -> 0.0
2709 | Some t -> t
2711 let dt = now () -. started in
2712 if dt > time
2713 then (
2714 state.y <- y;
2715 load layout;
2719 let setcolumns mode columns coverA coverB =
2720 state.prevcolumns <- Some (conf.columns, conf.zoom);
2721 if columns < 0
2722 then (
2723 if isbirdseye mode
2724 then showtext '!' "split mode doesn't work in bird's eye"
2725 else (
2726 conf.columns <- Csplit (-columns, [||]);
2727 state.x <- 0;
2728 conf.zoom <- 1.0;
2731 else (
2732 if columns < 2
2733 then (
2734 conf.columns <- Csingle [||];
2735 state.x <- 0;
2736 setzoom 1.0;
2738 else (
2739 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2740 conf.zoom <- 1.0;
2743 reshape state.winw state.winh;
2746 let enterbirdseye () =
2747 let zoom = float conf.thumbw /. float state.winw in
2748 let birdseyepageno =
2749 let cy = state.winh / 2 in
2750 let fold = function
2751 | [] -> 0
2752 | l :: rest ->
2753 let rec fold best = function
2754 | [] -> best.pageno
2755 | l :: rest ->
2756 let d = cy - (l.pagedispy + l.pagevh/2)
2757 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2758 if abs d < abs dbest
2759 then fold l rest
2760 else best.pageno
2761 in fold l rest
2763 fold state.layout
2765 state.mode <- Birdseye (
2766 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2768 conf.zoom <- zoom;
2769 conf.presentation <- false;
2770 conf.interpagespace <- 10;
2771 conf.hlinks <- false;
2772 state.x <- 0;
2773 state.mstate <- Mnone;
2774 conf.maxwait <- None;
2775 conf.columns <- (
2776 match conf.beyecolumns with
2777 | Some c ->
2778 conf.zoom <- 1.0;
2779 Cmulti ((c, 0, 0), [||])
2780 | None -> Csingle [||]
2782 Wsi.setcursor Wsi.CURSOR_INHERIT;
2783 if conf.verbose
2784 then
2785 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2786 (100.0*.zoom)
2787 else
2788 state.text <- ""
2790 reshape state.winw state.winh;
2793 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2794 state.mode <- View;
2795 conf.zoom <- c.zoom;
2796 conf.presentation <- c.presentation;
2797 conf.interpagespace <- c.interpagespace;
2798 conf.maxwait <- c.maxwait;
2799 conf.hlinks <- c.hlinks;
2800 conf.beyecolumns <- (
2801 match conf.columns with
2802 | Cmulti ((c, _, _), _) -> Some c
2803 | Csingle _ -> None
2804 | Csplit _ -> failwith "leaving bird's eye split mode"
2806 conf.columns <- (
2807 match c.columns with
2808 | Cmulti (c, _) -> Cmulti (c, [||])
2809 | Csingle _ -> Csingle [||]
2810 | Csplit (c, _) -> Csplit (c, [||])
2812 state.x <- leftx;
2813 if conf.verbose
2814 then
2815 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2816 (100.0*.conf.zoom)
2818 reshape state.winw state.winh;
2819 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2822 let togglebirdseye () =
2823 match state.mode with
2824 | Birdseye vals -> leavebirdseye vals true
2825 | View -> enterbirdseye ()
2826 | _ -> ()
2829 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2830 let pageno = max 0 (pageno - incr) in
2831 let rec loop = function
2832 | [] -> gotopage1 pageno 0
2833 | l :: _ when l.pageno = pageno ->
2834 if l.pagedispy >= 0 && l.pagey = 0
2835 then G.postRedisplay "upbirdseye"
2836 else gotopage1 pageno 0
2837 | _ :: rest -> loop rest
2839 loop state.layout;
2840 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2843 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2844 let pageno = min (state.pagecount - 1) (pageno + incr) in
2845 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2846 let rec loop = function
2847 | [] ->
2848 let y, h = getpageyh pageno in
2849 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2850 gotoy (clamp dy)
2851 | l :: _ when l.pageno = pageno ->
2852 if l.pagevh != l.pageh
2853 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2854 else G.postRedisplay "downbirdseye"
2855 | _ :: rest -> loop rest
2857 loop state.layout
2860 let optentry mode _ key =
2861 let btos b = if b then "on" else "off" in
2862 if key >= 32 && key < 127
2863 then
2864 let c = Char.chr key in
2865 match c with
2866 | 's' ->
2867 let ondone s =
2868 try conf.scrollstep <- int_of_string s with exc ->
2869 state.text <- Printf.sprintf "bad integer `%s': %s"
2870 s (Printexc.to_string exc)
2872 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2874 | 'A' ->
2875 let ondone s =
2877 conf.autoscrollstep <- int_of_string s;
2878 if state.autoscroll <> None
2879 then state.autoscroll <- Some conf.autoscrollstep
2880 with exc ->
2881 state.text <- Printf.sprintf "bad integer `%s': %s"
2882 s (Printexc.to_string exc)
2884 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2886 | 'C' ->
2887 let ondone s =
2889 let n, a, b = multicolumns_of_string s in
2890 setcolumns mode n a b;
2891 with exc ->
2892 state.text <- Printf.sprintf "bad columns `%s': %s"
2893 s (Printexc.to_string exc)
2895 TEswitch ("columns: ", "", None, textentry, ondone, true)
2897 | 'Z' ->
2898 let ondone s =
2900 let zoom = float (int_of_string s) /. 100.0 in
2901 setzoom zoom
2902 with exc ->
2903 state.text <- Printf.sprintf "bad integer `%s': %s"
2904 s (Printexc.to_string exc)
2906 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2908 | 't' ->
2909 let ondone s =
2911 conf.thumbw <- bound (int_of_string s) 2 4096;
2912 state.text <-
2913 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2914 begin match mode with
2915 | Birdseye beye ->
2916 leavebirdseye beye false;
2917 enterbirdseye ();
2918 | _ -> ();
2920 with exc ->
2921 state.text <- Printf.sprintf "bad integer `%s': %s"
2922 s (Printexc.to_string exc)
2924 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2926 | 'R' ->
2927 let ondone s =
2928 match try
2929 Some (int_of_string s)
2930 with exc ->
2931 state.text <- Printf.sprintf "bad integer `%s': %s"
2932 s (Printexc.to_string exc);
2933 None
2934 with
2935 | Some angle -> reqlayout angle conf.proportional
2936 | None -> ()
2938 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2940 | 'i' ->
2941 conf.icase <- not conf.icase;
2942 TEdone ("case insensitive search " ^ (btos conf.icase))
2944 | 'p' ->
2945 conf.preload <- not conf.preload;
2946 gotoy state.y;
2947 TEdone ("preload " ^ (btos conf.preload))
2949 | 'v' ->
2950 conf.verbose <- not conf.verbose;
2951 TEdone ("verbose " ^ (btos conf.verbose))
2953 | 'd' ->
2954 conf.debug <- not conf.debug;
2955 TEdone ("debug " ^ (btos conf.debug))
2957 | 'h' ->
2958 conf.maxhfit <- not conf.maxhfit;
2959 state.maxy <- calcheight ();
2960 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2962 | 'c' ->
2963 conf.crophack <- not conf.crophack;
2964 TEdone ("crophack " ^ btos conf.crophack)
2966 | 'a' ->
2967 let s =
2968 match conf.maxwait with
2969 | None ->
2970 conf.maxwait <- Some infinity;
2971 "always wait for page to complete"
2972 | Some _ ->
2973 conf.maxwait <- None;
2974 "show placeholder if page is not ready"
2976 TEdone s
2978 | 'f' ->
2979 conf.underinfo <- not conf.underinfo;
2980 TEdone ("underinfo " ^ btos conf.underinfo)
2982 | 'P' ->
2983 conf.savebmarks <- not conf.savebmarks;
2984 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2986 | 'S' ->
2987 let ondone s =
2989 let pageno, py =
2990 match state.layout with
2991 | [] -> 0, 0
2992 | l :: _ ->
2993 l.pageno, l.pagey
2995 conf.interpagespace <- int_of_string s;
2996 docolumns conf.columns;
2997 state.maxy <- calcheight ();
2998 let y = getpagey pageno in
2999 gotoy (y + py)
3000 with exc ->
3001 state.text <- Printf.sprintf "bad integer `%s': %s"
3002 s (Printexc.to_string exc)
3004 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3006 | 'l' ->
3007 reqlayout conf.angle (not conf.proportional);
3008 TEdone ("proportional display " ^ btos conf.proportional)
3010 | 'T' ->
3011 settrim (not conf.trimmargins) conf.trimfuzz;
3012 TEdone ("trim margins " ^ btos conf.trimmargins)
3014 | 'I' ->
3015 conf.invert <- not conf.invert;
3016 TEdone ("invert colors " ^ btos conf.invert)
3018 | 'x' ->
3019 let ondone s =
3020 cbput state.hists.sel s;
3021 conf.selcmd <- s;
3023 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3024 textentry, ondone, true)
3026 | _ ->
3027 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3028 TEstop
3029 else
3030 TEcont state.text
3033 class type lvsource = object
3034 method getitemcount : int
3035 method getitem : int -> (string * int)
3036 method hasaction : int -> bool
3037 method exit :
3038 uioh:uioh ->
3039 cancel:bool ->
3040 active:int ->
3041 first:int ->
3042 pan:int ->
3043 qsearch:string ->
3044 uioh option
3045 method getactive : int
3046 method getfirst : int
3047 method getqsearch : string
3048 method setqsearch : string -> unit
3049 method getpan : int
3050 end;;
3052 class virtual lvsourcebase = object
3053 val mutable m_active = 0
3054 val mutable m_first = 0
3055 val mutable m_qsearch = ""
3056 val mutable m_pan = 0
3057 method getactive = m_active
3058 method getfirst = m_first
3059 method getqsearch = m_qsearch
3060 method getpan = m_pan
3061 method setqsearch s = m_qsearch <- s
3062 end;;
3064 let withoutlastutf8 s =
3065 let len = String.length s in
3066 if len = 0
3067 then s
3068 else
3069 let rec find pos =
3070 if pos = 0
3071 then pos
3072 else
3073 let b = Char.code s.[pos] in
3074 if b land 0b11000000 = 0b11000000
3075 then pos
3076 else find (pos-1)
3078 let first =
3079 if Char.code s.[len-1] land 0x80 = 0
3080 then len-1
3081 else find (len-1)
3083 String.sub s 0 first;
3086 let textentrykeyboard
3087 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3088 let key =
3089 if key >= 0xffb0 && key <= 0xffb9
3090 then key - 0xffb0 + 48 else key
3092 let enttext te =
3093 state.mode <- Textentry (te, onleave);
3094 state.text <- "";
3095 enttext ();
3096 G.postRedisplay "textentrykeyboard enttext";
3098 let histaction cmd =
3099 match opthist with
3100 | None -> ()
3101 | Some (action, _) ->
3102 state.mode <- Textentry (
3103 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3105 G.postRedisplay "textentry histaction"
3107 match key with
3108 | 0xff08 -> (* backspace *)
3109 let s = withoutlastutf8 text in
3110 let len = String.length s in
3111 if cancelonempty && len = 0
3112 then (
3113 onleave Cancel;
3114 G.postRedisplay "textentrykeyboard after cancel";
3116 else (
3117 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3120 | 0xff0d | 0xff8d -> (* (kp) enter *)
3121 ondone text;
3122 onleave Confirm;
3123 G.postRedisplay "textentrykeyboard after confirm"
3125 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3126 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3127 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3128 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3130 | 0xff1b -> (* escape*)
3131 if String.length text = 0
3132 then (
3133 begin match opthist with
3134 | None -> ()
3135 | Some (_, onhistcancel) -> onhistcancel ()
3136 end;
3137 onleave Cancel;
3138 state.text <- "";
3139 G.postRedisplay "textentrykeyboard after cancel2"
3141 else (
3142 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3145 | 0xff9f | 0xffff -> () (* delete *)
3147 | _ when key != 0
3148 && key land 0xff00 != 0xff00 (* keyboard *)
3149 && key land 0xfe00 != 0xfe00 (* xkb *)
3150 && key land 0xfd00 != 0xfd00 (* 3270 *)
3152 begin match onkey text key with
3153 | TEdone text ->
3154 ondone text;
3155 onleave Confirm;
3156 G.postRedisplay "textentrykeyboard after confirm2";
3158 | TEcont text ->
3159 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3161 | TEstop ->
3162 onleave Cancel;
3163 G.postRedisplay "textentrykeyboard after cancel3"
3165 | TEswitch te ->
3166 state.mode <- Textentry (te, onleave);
3167 G.postRedisplay "textentrykeyboard switch";
3168 end;
3170 | _ ->
3171 vlog "unhandled key %s" (Wsi.keyname key)
3174 let firstof first active =
3175 if first > active || abs (first - active) > fstate.maxrows - 1
3176 then max 0 (active - (fstate.maxrows/2))
3177 else first
3180 let calcfirst first active =
3181 if active > first
3182 then
3183 let rows = active - first in
3184 if rows > fstate.maxrows then active - fstate.maxrows else first
3185 else active
3188 let scrollph y maxy =
3189 let sh = (float (maxy + state.winh) /. float state.winh) in
3190 let sh = float state.winh /. sh in
3191 let sh = max sh (float conf.scrollh) in
3193 let percent =
3194 if y = state.maxy
3195 then 1.0
3196 else float y /. float maxy
3198 let position = (float state.winh -. sh) *. percent in
3200 let position =
3201 if position +. sh > float state.winh
3202 then float state.winh -. sh
3203 else position
3205 position, sh;
3208 let coe s = (s :> uioh);;
3210 class listview ~(source:lvsource) ~trusted ~modehash =
3211 object (self)
3212 val m_pan = source#getpan
3213 val m_first = source#getfirst
3214 val m_active = source#getactive
3215 val m_qsearch = source#getqsearch
3216 val m_prev_uioh = state.uioh
3218 method private elemunder y =
3219 let n = y / (fstate.fontsize+1) in
3220 if m_first + n < source#getitemcount
3221 then (
3222 if source#hasaction (m_first + n)
3223 then Some (m_first + n)
3224 else None
3226 else None
3228 method display =
3229 Gl.enable `blend;
3230 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3231 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3232 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3233 GlDraw.color (1., 1., 1.);
3234 Gl.enable `texture_2d;
3235 let fs = fstate.fontsize in
3236 let nfs = fs + 1 in
3237 let ww = fstate.wwidth in
3238 let tabw = 30.0*.ww in
3239 let itemcount = source#getitemcount in
3240 let rec loop row =
3241 if (row - m_first) > fstate.maxrows
3242 then ()
3243 else (
3244 if row >= 0 && row < itemcount
3245 then (
3246 let (s, level) = source#getitem row in
3247 let y = (row - m_first) * nfs in
3248 let x = 5.0 +. float (level + m_pan) *. ww in
3249 if row = m_active
3250 then (
3251 Gl.disable `texture_2d;
3252 GlDraw.polygon_mode `both `line;
3253 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3254 GlDraw.rect (1., float (y + 1))
3255 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3256 GlDraw.polygon_mode `both `fill;
3257 GlDraw.color (1., 1., 1.);
3258 Gl.enable `texture_2d;
3261 let drawtabularstring s =
3262 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3263 if trusted
3264 then
3265 let tabpos = try String.index s '\t' with Not_found -> -1 in
3266 if tabpos > 0
3267 then
3268 let len = String.length s - tabpos - 1 in
3269 let s1 = String.sub s 0 tabpos
3270 and s2 = String.sub s (tabpos + 1) len in
3271 let nx = drawstr x s1 in
3272 let sw = nx -. x in
3273 let x = x +. (max tabw sw) in
3274 drawstr x s2
3275 else
3276 drawstr x s
3277 else
3278 drawstr x s
3280 let _ = drawtabularstring s in
3281 loop (row+1)
3285 loop m_first;
3286 Gl.disable `blend;
3287 Gl.disable `texture_2d;
3289 method updownlevel incr =
3290 let len = source#getitemcount in
3291 let curlevel =
3292 if m_active >= 0 && m_active < len
3293 then snd (source#getitem m_active)
3294 else -1
3296 let rec flow i =
3297 if i = len then i-1 else if i = -1 then 0 else
3298 let _, l = source#getitem i in
3299 if l != curlevel then i else flow (i+incr)
3301 let active = flow m_active in
3302 let first = calcfirst m_first active in
3303 G.postRedisplay "outline updownlevel";
3304 {< m_active = active; m_first = first >}
3306 method private key1 key mask =
3307 let set1 active first qsearch =
3308 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3310 let search active pattern incr =
3311 let dosearch re =
3312 let rec loop n =
3313 if n >= 0 && n < source#getitemcount
3314 then (
3315 let s, _ = source#getitem n in
3317 (try ignore (Str.search_forward re s 0); true
3318 with Not_found -> false)
3319 then Some n
3320 else loop (n + incr)
3322 else None
3324 loop active
3327 let re = Str.regexp_case_fold pattern in
3328 dosearch re
3329 with Failure s ->
3330 state.text <- s;
3331 None
3333 let itemcount = source#getitemcount in
3334 let find start incr =
3335 let rec find i =
3336 if i = -1 || i = itemcount
3337 then -1
3338 else (
3339 if source#hasaction i
3340 then i
3341 else find (i + incr)
3344 find start
3346 let set active first =
3347 let first = bound first 0 (itemcount - fstate.maxrows) in
3348 state.text <- "";
3349 coe {< m_active = active; m_first = first >}
3351 let navigate incr =
3352 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3353 let active, first =
3354 let incr1 = if incr > 0 then 1 else -1 in
3355 if isvisible m_first m_active
3356 then
3357 let next =
3358 let next = m_active + incr in
3359 let next =
3360 if next < 0 || next >= itemcount
3361 then -1
3362 else find next incr1
3364 if next = -1 || abs (m_active - next) > fstate.maxrows
3365 then -1
3366 else next
3368 if next = -1
3369 then
3370 let first = m_first + incr in
3371 let first = bound first 0 (itemcount - 1) in
3372 let next =
3373 let next = m_active + incr in
3374 let next = bound next 0 (itemcount - 1) in
3375 find next ~-incr1
3377 let active = if next = -1 then m_active else next in
3378 active, first
3379 else
3380 let first = min next m_first in
3381 let first =
3382 if abs (next - first) > fstate.maxrows
3383 then first + incr
3384 else first
3386 next, first
3387 else
3388 let first = m_first + incr in
3389 let first = bound first 0 (itemcount - 1) in
3390 let active =
3391 let next = m_active + incr in
3392 let next = bound next 0 (itemcount - 1) in
3393 let next = find next incr1 in
3394 let active =
3395 if next = -1 || abs (m_active - first) > fstate.maxrows
3396 then (
3397 let active = if m_active = -1 then next else m_active in
3398 active
3400 else next
3402 if isvisible first active
3403 then active
3404 else -1
3406 active, first
3408 G.postRedisplay "listview navigate";
3409 set active first;
3411 match key with
3412 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3413 let incr = if key = 0x72 then -1 else 1 in
3414 let active, first =
3415 match search (m_active + incr) m_qsearch incr with
3416 | None ->
3417 state.text <- m_qsearch ^ " [not found]";
3418 m_active, m_first
3419 | Some active ->
3420 state.text <- m_qsearch;
3421 active, firstof m_first active
3423 G.postRedisplay "listview ctrl-r/s";
3424 set1 active first m_qsearch;
3426 | 0xff08 -> (* backspace *)
3427 if String.length m_qsearch = 0
3428 then coe self
3429 else (
3430 let qsearch = withoutlastutf8 m_qsearch in
3431 let len = String.length qsearch in
3432 if len = 0
3433 then (
3434 state.text <- "";
3435 G.postRedisplay "listview empty qsearch";
3436 set1 m_active m_first "";
3438 else
3439 let active, first =
3440 match search m_active qsearch ~-1 with
3441 | None ->
3442 state.text <- qsearch ^ " [not found]";
3443 m_active, m_first
3444 | Some active ->
3445 state.text <- qsearch;
3446 active, firstof m_first active
3448 G.postRedisplay "listview backspace qsearch";
3449 set1 active first qsearch
3452 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3453 let pattern = m_qsearch ^ Wsi.toutf8 key in
3454 let active, first =
3455 match search m_active pattern 1 with
3456 | None ->
3457 state.text <- pattern ^ " [not found]";
3458 m_active, m_first
3459 | Some active ->
3460 state.text <- pattern;
3461 active, firstof m_first active
3463 G.postRedisplay "listview qsearch add";
3464 set1 active first pattern;
3466 | 0xff1b -> (* escape *)
3467 state.text <- "";
3468 if String.length m_qsearch = 0
3469 then (
3470 G.postRedisplay "list view escape";
3471 begin
3472 match
3473 source#exit (coe self) true m_active m_first m_pan m_qsearch
3474 with
3475 | None -> m_prev_uioh
3476 | Some uioh -> uioh
3479 else (
3480 G.postRedisplay "list view kill qsearch";
3481 source#setqsearch "";
3482 coe {< m_qsearch = "" >}
3485 | 0xff0d | 0xff8d -> (* (kp) enter *)
3486 state.text <- "";
3487 let self = {< m_qsearch = "" >} in
3488 source#setqsearch "";
3489 let opt =
3490 G.postRedisplay "listview enter";
3491 if m_active >= 0 && m_active < source#getitemcount
3492 then (
3493 source#exit (coe self) false m_active m_first m_pan "";
3495 else (
3496 source#exit (coe self) true m_active m_first m_pan "";
3499 begin match opt with
3500 | None -> m_prev_uioh
3501 | Some uioh -> uioh
3504 | 0xff9f | 0xffff -> (* (kp) delete *)
3505 coe self
3507 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3508 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3509 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3510 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3512 | 0xff53 | 0xff98 -> (* (kp) right *)
3513 state.text <- "";
3514 G.postRedisplay "listview right";
3515 coe {< m_pan = m_pan - 1 >}
3517 | 0xff51 | 0xff96 -> (* (kp) left *)
3518 state.text <- "";
3519 G.postRedisplay "listview left";
3520 coe {< m_pan = m_pan + 1 >}
3522 | 0xff50 | 0xff95 -> (* (kp) home *)
3523 let active = find 0 1 in
3524 G.postRedisplay "listview home";
3525 set active 0;
3527 | 0xff57 | 0xff9c -> (* (kp) end *)
3528 let first = max 0 (itemcount - fstate.maxrows) in
3529 let active = find (itemcount - 1) ~-1 in
3530 G.postRedisplay "listview end";
3531 set active first;
3533 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3534 coe self
3536 | _ ->
3537 dolog "listview unknown key %#x" key; coe self
3539 method key key mask =
3540 match state.mode with
3541 | Textentry te -> textentrykeyboard key mask te; coe self
3542 | _ -> self#key1 key mask
3544 method button button down x y _ =
3545 let opt =
3546 match button with
3547 | 1 when x > state.winw - conf.scrollbw ->
3548 G.postRedisplay "listview scroll";
3549 if down
3550 then
3551 let _, position, sh = self#scrollph in
3552 if y > truncate position && y < truncate (position +. sh)
3553 then (
3554 state.mstate <- Mscrolly;
3555 Some (coe self)
3557 else
3558 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3559 let first = truncate (s *. float source#getitemcount) in
3560 let first = min source#getitemcount first in
3561 Some (coe {< m_first = first; m_active = first >})
3562 else (
3563 state.mstate <- Mnone;
3564 Some (coe self);
3566 | 1 when not down ->
3567 begin match self#elemunder y with
3568 | Some n ->
3569 G.postRedisplay "listview click";
3570 source#exit
3571 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3572 | _ ->
3573 Some (coe self)
3575 | n when (n == 4 || n == 5) && not down ->
3576 let len = source#getitemcount in
3577 let first =
3578 if n = 5 && m_first + fstate.maxrows >= len
3579 then
3580 m_first
3581 else
3582 let first = m_first + (if n == 4 then -1 else 1) in
3583 bound first 0 (len - 1)
3585 G.postRedisplay "listview wheel";
3586 Some (coe {< m_first = first >})
3587 | n when (n = 6 || n = 7) && not down ->
3588 let inc = m_first + (if n = 7 then -1 else 1) in
3589 G.postRedisplay "listview hwheel";
3590 Some (coe {< m_pan = m_pan + inc >})
3591 | _ ->
3592 Some (coe self)
3594 match opt with
3595 | None -> m_prev_uioh
3596 | Some uioh -> uioh
3598 method motion _ y =
3599 match state.mstate with
3600 | Mscrolly ->
3601 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3602 let first = truncate (s *. float source#getitemcount) in
3603 let first = min source#getitemcount first in
3604 G.postRedisplay "listview motion";
3605 coe {< m_first = first; m_active = first >}
3606 | _ -> coe self
3608 method pmotion x y =
3609 if x < state.winw - conf.scrollbw
3610 then
3611 let n =
3612 match self#elemunder y with
3613 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3614 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3616 let o =
3617 if n != m_active
3618 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3619 else self
3621 coe o
3622 else (
3623 Wsi.setcursor Wsi.CURSOR_INHERIT;
3624 coe self
3627 method infochanged _ = ()
3629 method scrollpw = (0, 0.0, 0.0)
3630 method scrollph =
3631 let nfs = fstate.fontsize + 1 in
3632 let y = m_first * nfs in
3633 let itemcount = source#getitemcount in
3634 let maxi = max 0 (itemcount - fstate.maxrows) in
3635 let maxy = maxi * nfs in
3636 let p, h = scrollph y maxy in
3637 conf.scrollbw, p, h
3639 method modehash = modehash
3640 end;;
3642 class outlinelistview ~source =
3643 object (self)
3644 inherit listview
3645 ~source:(source :> lvsource)
3646 ~trusted:false
3647 ~modehash:(findkeyhash conf "outline")
3648 as super
3650 method key key mask =
3651 let calcfirst first active =
3652 if active > first
3653 then
3654 let rows = active - first in
3655 let maxrows =
3656 if String.length state.text = 0
3657 then fstate.maxrows
3658 else fstate.maxrows - 2
3660 if rows > maxrows then active - maxrows else first
3661 else active
3663 let navigate incr =
3664 let active = m_active + incr in
3665 let active = bound active 0 (source#getitemcount - 1) in
3666 let first = calcfirst m_first active in
3667 G.postRedisplay "outline navigate";
3668 coe {< m_active = active; m_first = first >}
3670 let ctrl = Wsi.withctrl mask in
3671 match key with
3672 | 110 when ctrl -> (* ctrl-n *)
3673 source#narrow m_qsearch;
3674 G.postRedisplay "outline ctrl-n";
3675 coe {< m_first = 0; m_active = 0 >}
3677 | 117 when ctrl -> (* ctrl-u *)
3678 source#denarrow;
3679 G.postRedisplay "outline ctrl-u";
3680 state.text <- "";
3681 coe {< m_first = 0; m_active = 0 >}
3683 | 108 when ctrl -> (* ctrl-l *)
3684 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3685 G.postRedisplay "outline ctrl-l";
3686 coe {< m_first = first >}
3688 | 0xff9f | 0xffff -> (* (kp) delete *)
3689 source#remove m_active;
3690 G.postRedisplay "outline delete";
3691 let active = max 0 (m_active-1) in
3692 coe {< m_first = firstof m_first active;
3693 m_active = active >}
3695 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3696 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3697 | 0xff55 | 0xff9a -> (* (kp) prior *)
3698 navigate ~-(fstate.maxrows)
3699 | 0xff56 | 0xff9b -> (* (kp) next *)
3700 navigate fstate.maxrows
3702 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3703 let o =
3704 if ctrl
3705 then (
3706 G.postRedisplay "outline ctrl right";
3707 {< m_pan = m_pan + 1 >}
3709 else self#updownlevel 1
3711 coe o
3713 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3714 let o =
3715 if ctrl
3716 then (
3717 G.postRedisplay "outline ctrl left";
3718 {< m_pan = m_pan - 1 >}
3720 else self#updownlevel ~-1
3722 coe o
3724 | 0xff50 | 0xff95 -> (* (kp) home *)
3725 G.postRedisplay "outline home";
3726 coe {< m_first = 0; m_active = 0 >}
3728 | 0xff57 | 0xff9c -> (* (kp) end *)
3729 let active = source#getitemcount - 1 in
3730 let first = max 0 (active - fstate.maxrows) in
3731 G.postRedisplay "outline end";
3732 coe {< m_active = active; m_first = first >}
3734 | _ -> super#key key mask
3737 let outlinesource usebookmarks =
3738 let empty = [||] in
3739 (object
3740 inherit lvsourcebase
3741 val mutable m_items = empty
3742 val mutable m_orig_items = empty
3743 val mutable m_prev_items = empty
3744 val mutable m_narrow_pattern = ""
3745 val mutable m_hadremovals = false
3747 method getitemcount =
3748 Array.length m_items + (if m_hadremovals then 1 else 0)
3750 method getitem n =
3751 if n == Array.length m_items && m_hadremovals
3752 then
3753 ("[Confirm removal]", 0)
3754 else
3755 let s, n, _ = m_items.(n) in
3756 (s, n)
3758 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3759 ignore (uioh, first, qsearch);
3760 let confrimremoval = m_hadremovals && active = Array.length m_items in
3761 let items =
3762 if String.length m_narrow_pattern = 0
3763 then m_orig_items
3764 else m_items
3766 if not cancel
3767 then (
3768 if not confrimremoval
3769 then(
3770 let _, _, anchor = m_items.(active) in
3771 gotoghyll (getanchory anchor);
3772 m_items <- items;
3774 else (
3775 state.bookmarks <- Array.to_list m_items;
3776 m_orig_items <- m_items;
3779 else m_items <- items;
3780 m_pan <- pan;
3781 None
3783 method hasaction _ = true
3785 method greetmsg =
3786 if Array.length m_items != Array.length m_orig_items
3787 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3788 else ""
3790 method narrow pattern =
3791 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3792 match reopt with
3793 | None -> ()
3794 | Some re ->
3795 let rec loop accu n =
3796 if n = -1
3797 then (
3798 m_narrow_pattern <- pattern;
3799 m_items <- Array.of_list accu
3801 else
3802 let (s, _, _) as o = m_items.(n) in
3803 let accu =
3804 if (try ignore (Str.search_forward re s 0); true
3805 with Not_found -> false)
3806 then o :: accu
3807 else accu
3809 loop accu (n-1)
3811 loop [] (Array.length m_items - 1)
3813 method denarrow =
3814 m_orig_items <- (
3815 if usebookmarks
3816 then Array.of_list state.bookmarks
3817 else state.outlines
3819 m_items <- m_orig_items
3821 method remove m =
3822 if usebookmarks
3823 then
3824 if m >= 0 && m < Array.length m_items
3825 then (
3826 m_hadremovals <- true;
3827 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3828 let n = if n >= m then n+1 else n in
3829 m_items.(n)
3833 method reset anchor items =
3834 m_hadremovals <- false;
3835 if m_orig_items == empty || m_prev_items != items
3836 then (
3837 m_orig_items <- items;
3838 if String.length m_narrow_pattern = 0
3839 then m_items <- items;
3841 m_prev_items <- items;
3842 let rely = getanchory anchor in
3843 let active =
3844 let rec loop n best bestd =
3845 if n = Array.length m_items
3846 then best
3847 else
3848 let (_, _, anchor) = m_items.(n) in
3849 let orely = getanchory anchor in
3850 let d = abs (orely - rely) in
3851 if d < bestd
3852 then loop (n+1) n d
3853 else loop (n+1) best bestd
3855 loop 0 ~-1 max_int
3857 m_active <- active;
3858 m_first <- firstof m_first active
3859 end)
3862 let enterselector usebookmarks =
3863 let source = outlinesource usebookmarks in
3864 fun errmsg ->
3865 let outlines =
3866 if usebookmarks
3867 then Array.of_list state.bookmarks
3868 else state.outlines
3870 if Array.length outlines = 0
3871 then (
3872 showtext ' ' errmsg;
3874 else (
3875 state.text <- source#greetmsg;
3876 Wsi.setcursor Wsi.CURSOR_INHERIT;
3877 let anchor = getanchor () in
3878 source#reset anchor outlines;
3879 state.uioh <- coe (new outlinelistview ~source);
3880 G.postRedisplay "enter selector";
3884 let enteroutlinemode =
3885 let f = enterselector false in
3886 fun ()-> f "Document has no outline";
3889 let enterbookmarkmode =
3890 let f = enterselector true in
3891 fun () -> f "Document has no bookmarks (yet)";
3894 let color_of_string s =
3895 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3896 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3900 let color_to_string (r, g, b) =
3901 let r = truncate (r *. 256.0)
3902 and g = truncate (g *. 256.0)
3903 and b = truncate (b *. 256.0) in
3904 Printf.sprintf "%d/%d/%d" r g b
3907 let irect_of_string s =
3908 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3911 let irect_to_string (x0,y0,x1,y1) =
3912 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3915 let makecheckers () =
3916 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3917 following to say:
3918 converted by Issac Trotts. July 25, 2002 *)
3919 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3920 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3921 let id = GlTex.gen_texture () in
3922 GlTex.bind_texture `texture_2d id;
3923 GlPix.store (`unpack_alignment 1);
3924 GlTex.image2d image;
3925 List.iter (GlTex.parameter ~target:`texture_2d)
3926 [ `mag_filter `nearest; `min_filter `nearest ];
3930 let setcheckers enabled =
3931 match state.texid with
3932 | None ->
3933 if enabled then state.texid <- Some (makecheckers ())
3935 | Some texid ->
3936 if not enabled
3937 then (
3938 GlTex.delete_texture texid;
3939 state.texid <- None;
3943 let int_of_string_with_suffix s =
3944 let l = String.length s in
3945 let s1, shift =
3946 if l > 1
3947 then
3948 let suffix = Char.lowercase s.[l-1] in
3949 match suffix with
3950 | 'k' -> String.sub s 0 (l-1), 10
3951 | 'm' -> String.sub s 0 (l-1), 20
3952 | 'g' -> String.sub s 0 (l-1), 30
3953 | _ -> s, 0
3954 else s, 0
3956 let n = int_of_string s1 in
3957 let m = n lsl shift in
3958 if m < 0 || m < n
3959 then raise (Failure "value too large")
3960 else m
3963 let string_with_suffix_of_int n =
3964 if n = 0
3965 then "0"
3966 else
3967 let n, s =
3968 if n land ((1 lsl 30) - 1) = 0
3969 then n lsr 30, "G"
3970 else (
3971 if n land ((1 lsl 20) - 1) = 0
3972 then n lsr 20, "M"
3973 else (
3974 if n land ((1 lsl 10) - 1) = 0
3975 then n lsr 10, "K"
3976 else n, ""
3980 let rec loop s n =
3981 let h = n mod 1000 in
3982 let n = n / 1000 in
3983 if n = 0
3984 then string_of_int h ^ s
3985 else (
3986 let s = Printf.sprintf "_%03d%s" h s in
3987 loop s n
3990 loop "" n ^ s;
3993 let defghyllscroll = (40, 8, 32);;
3994 let ghyllscroll_of_string s =
3995 let (n, a, b) as nab =
3996 if s = "default"
3997 then defghyllscroll
3998 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4000 if n <= a || n <= b || a >= b
4001 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4002 nab;
4005 let ghyllscroll_to_string ((n, a, b) as nab) =
4006 if nab = defghyllscroll
4007 then "default"
4008 else Printf.sprintf "%d,%d,%d" n a b;
4011 let describe_location () =
4012 let f (fn, _) l =
4013 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
4015 let fn, ln = List.fold_left f (-1, -1) state.layout in
4016 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4017 let percent =
4018 if maxy <= 0
4019 then 100.
4020 else (100. *. (float state.y /. float maxy))
4022 if fn = ln
4023 then
4024 Printf.sprintf "page %d of %d [%.2f%%]"
4025 (fn+1) state.pagecount percent
4026 else
4027 Printf.sprintf
4028 "pages %d-%d of %d [%.2f%%]"
4029 (fn+1) (ln+1) state.pagecount percent
4032 let setpresentationmode v =
4033 let (n, _, _) = getanchor () in
4034 let _, h = getpageyh n in
4035 let ips = if conf.presentation then calcips h else conf.interpagespace in
4036 state.anchor <- (n, 0.0, float ips);
4037 conf.presentation <- v;
4038 if conf.presentation
4039 then (
4040 if not conf.scrollbarinpm
4041 then state.scrollw <- 0;
4043 else state.scrollw <- conf.scrollbw;
4044 represent ();
4047 let enterinfomode =
4048 let btos b = if b then "\xe2\x88\x9a" else "" in
4049 let showextended = ref false in
4050 let leave mode = function
4051 | Confirm -> state.mode <- mode
4052 | Cancel -> state.mode <- mode in
4053 let src =
4054 (object
4055 val mutable m_first_time = true
4056 val mutable m_l = []
4057 val mutable m_a = [||]
4058 val mutable m_prev_uioh = nouioh
4059 val mutable m_prev_mode = View
4061 inherit lvsourcebase
4063 method reset prev_mode prev_uioh =
4064 m_a <- Array.of_list (List.rev m_l);
4065 m_l <- [];
4066 m_prev_mode <- prev_mode;
4067 m_prev_uioh <- prev_uioh;
4068 if m_first_time
4069 then (
4070 let rec loop n =
4071 if n >= Array.length m_a
4072 then ()
4073 else
4074 match m_a.(n) with
4075 | _, _, _, Action _ -> m_active <- n
4076 | _ -> loop (n+1)
4078 loop 0;
4079 m_first_time <- false;
4082 method int name get set =
4083 m_l <-
4084 (name, `int get, 1, Action (
4085 fun u ->
4086 let ondone s =
4087 try set (int_of_string s)
4088 with exn ->
4089 state.text <- Printf.sprintf "bad integer `%s': %s"
4090 s (Printexc.to_string exn)
4092 state.text <- "";
4093 let te = name ^ ": ", "", None, intentry, ondone, true in
4094 state.mode <- Textentry (te, leave m_prev_mode);
4096 )) :: m_l
4098 method int_with_suffix name get set =
4099 m_l <-
4100 (name, `intws get, 1, Action (
4101 fun u ->
4102 let ondone s =
4103 try set (int_of_string_with_suffix s)
4104 with exn ->
4105 state.text <- Printf.sprintf "bad integer `%s': %s"
4106 s (Printexc.to_string exn)
4108 state.text <- "";
4109 let te =
4110 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4112 state.mode <- Textentry (te, leave m_prev_mode);
4114 )) :: m_l
4116 method bool ?(offset=1) ?(btos=btos) name get set =
4117 m_l <-
4118 (name, `bool (btos, get), offset, Action (
4119 fun u ->
4120 let v = get () in
4121 set (not v);
4123 )) :: m_l
4125 method color name get set =
4126 m_l <-
4127 (name, `color get, 1, Action (
4128 fun u ->
4129 let invalid = (nan, nan, nan) in
4130 let ondone s =
4131 let c =
4132 try color_of_string s
4133 with exn ->
4134 state.text <- Printf.sprintf "bad color `%s': %s"
4135 s (Printexc.to_string exn);
4136 invalid
4138 if c <> invalid
4139 then set c;
4141 let te = name ^ ": ", "", None, textentry, ondone, true in
4142 state.text <- color_to_string (get ());
4143 state.mode <- Textentry (te, leave m_prev_mode);
4145 )) :: m_l
4147 method string name get set =
4148 m_l <-
4149 (name, `string get, 1, Action (
4150 fun u ->
4151 let ondone s = set s in
4152 let te = name ^ ": ", "", None, textentry, ondone, true in
4153 state.mode <- Textentry (te, leave m_prev_mode);
4155 )) :: m_l
4157 method colorspace name get set =
4158 m_l <-
4159 (name, `string get, 1, Action (
4160 fun _ ->
4161 let source =
4162 let vals = [| "rgb"; "bgr"; "gray" |] in
4163 (object
4164 inherit lvsourcebase
4166 initializer
4167 m_active <- int_of_colorspace conf.colorspace;
4168 m_first <- 0;
4170 method getitemcount = Array.length vals
4171 method getitem n = (vals.(n), 0)
4172 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4173 ignore (uioh, first, pan, qsearch);
4174 if not cancel then set active;
4175 None
4176 method hasaction _ = true
4177 end)
4179 state.text <- "";
4180 let modehash = findkeyhash conf "info" in
4181 coe (new listview ~source ~trusted:true ~modehash)
4182 )) :: m_l
4184 method caption s offset =
4185 m_l <- (s, `empty, offset, Noaction) :: m_l
4187 method caption2 s f offset =
4188 m_l <- (s, `string f, offset, Noaction) :: m_l
4190 method getitemcount = Array.length m_a
4192 method getitem n =
4193 let tostr = function
4194 | `int f -> string_of_int (f ())
4195 | `intws f -> string_with_suffix_of_int (f ())
4196 | `string f -> f ()
4197 | `color f -> color_to_string (f ())
4198 | `bool (btos, f) -> btos (f ())
4199 | `empty -> ""
4201 let name, t, offset, _ = m_a.(n) in
4202 ((let s = tostr t in
4203 if String.length s > 0
4204 then Printf.sprintf "%s\t%s" name s
4205 else name),
4206 offset)
4208 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4209 let uiohopt =
4210 if not cancel
4211 then (
4212 m_qsearch <- qsearch;
4213 let uioh =
4214 match m_a.(active) with
4215 | _, _, _, Action f -> f uioh
4216 | _ -> uioh
4218 Some uioh
4220 else None
4222 m_active <- active;
4223 m_first <- first;
4224 m_pan <- pan;
4225 uiohopt
4227 method hasaction n =
4228 match m_a.(n) with
4229 | _, _, _, Action _ -> true
4230 | _ -> false
4231 end)
4233 let rec fillsrc prevmode prevuioh =
4234 let sep () = src#caption "" 0 in
4235 let colorp name get set =
4236 src#string name
4237 (fun () -> color_to_string (get ()))
4238 (fun v ->
4240 let c = color_of_string v in
4241 set c
4242 with exn ->
4243 state.text <- Printf.sprintf "bad color `%s': %s"
4244 v (Printexc.to_string exn);
4247 let oldmode = state.mode in
4248 let birdseye = isbirdseye state.mode in
4250 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4252 src#bool "presentation mode"
4253 (fun () -> conf.presentation)
4254 (fun v -> setpresentationmode v);
4256 src#bool "ignore case in searches"
4257 (fun () -> conf.icase)
4258 (fun v -> conf.icase <- v);
4260 src#bool "preload"
4261 (fun () -> conf.preload)
4262 (fun v -> conf.preload <- v);
4264 src#bool "highlight links"
4265 (fun () -> conf.hlinks)
4266 (fun v -> conf.hlinks <- v);
4268 src#bool "under info"
4269 (fun () -> conf.underinfo)
4270 (fun v -> conf.underinfo <- v);
4272 src#bool "persistent bookmarks"
4273 (fun () -> conf.savebmarks)
4274 (fun v -> conf.savebmarks <- v);
4276 src#bool "proportional display"
4277 (fun () -> conf.proportional)
4278 (fun v -> reqlayout conf.angle v);
4280 src#bool "trim margins"
4281 (fun () -> conf.trimmargins)
4282 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4284 src#bool "persistent location"
4285 (fun () -> conf.jumpback)
4286 (fun v -> conf.jumpback <- v);
4288 sep ();
4289 src#int "inter-page space"
4290 (fun () -> conf.interpagespace)
4291 (fun n ->
4292 conf.interpagespace <- n;
4293 docolumns conf.columns;
4294 let pageno, py =
4295 match state.layout with
4296 | [] -> 0, 0
4297 | l :: _ ->
4298 l.pageno, l.pagey
4300 state.maxy <- calcheight ();
4301 let y = getpagey pageno in
4302 gotoy (y + py)
4305 src#int "page bias"
4306 (fun () -> conf.pagebias)
4307 (fun v -> conf.pagebias <- v);
4309 src#int "scroll step"
4310 (fun () -> conf.scrollstep)
4311 (fun n -> conf.scrollstep <- n);
4313 src#int "horizontal scroll step"
4314 (fun () -> conf.hscrollstep)
4315 (fun v -> conf.hscrollstep <- v);
4317 src#int "auto scroll step"
4318 (fun () ->
4319 match state.autoscroll with
4320 | Some step -> step
4321 | _ -> conf.autoscrollstep)
4322 (fun n ->
4323 if state.autoscroll <> None
4324 then state.autoscroll <- Some n;
4325 conf.autoscrollstep <- n);
4327 src#int "zoom"
4328 (fun () -> truncate (conf.zoom *. 100.))
4329 (fun v -> setzoom ((float v) /. 100.));
4331 src#int "rotation"
4332 (fun () -> conf.angle)
4333 (fun v -> reqlayout v conf.proportional);
4335 src#int "scroll bar width"
4336 (fun () -> state.scrollw)
4337 (fun v ->
4338 state.scrollw <- v;
4339 conf.scrollbw <- v;
4340 reshape state.winw state.winh;
4343 src#int "scroll handle height"
4344 (fun () -> conf.scrollh)
4345 (fun v -> conf.scrollh <- v;);
4347 src#int "thumbnail width"
4348 (fun () -> conf.thumbw)
4349 (fun v ->
4350 conf.thumbw <- min 4096 v;
4351 match oldmode with
4352 | Birdseye beye ->
4353 leavebirdseye beye false;
4354 enterbirdseye ()
4355 | _ -> ()
4358 let mode = state.mode in
4359 src#string "columns"
4360 (fun () ->
4361 match conf.columns with
4362 | Csingle _ -> "1"
4363 | Cmulti (multi, _) -> multicolumns_to_string multi
4364 | Csplit (count, _) -> "-" ^ string_of_int count
4366 (fun v ->
4367 let n, a, b = multicolumns_of_string v in
4368 setcolumns mode n a b);
4370 sep ();
4371 src#caption "Presentation mode" 0;
4372 src#bool "scrollbar visible"
4373 (fun () -> conf.scrollbarinpm)
4374 (fun v ->
4375 if v != conf.scrollbarinpm
4376 then (
4377 conf.scrollbarinpm <- v;
4378 if conf.presentation
4379 then (
4380 state.scrollw <- if v then conf.scrollbw else 0;
4381 reshape state.winw state.winh;
4386 sep ();
4387 src#caption "Pixmap cache" 0;
4388 src#int_with_suffix "size (advisory)"
4389 (fun () -> conf.memlimit)
4390 (fun v -> conf.memlimit <- v);
4392 src#caption2 "used"
4393 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4394 (string_with_suffix_of_int state.memused)
4395 (Hashtbl.length state.tilemap)) 1;
4397 sep ();
4398 src#caption "Layout" 0;
4399 src#caption2 "Dimension"
4400 (fun () ->
4401 Printf.sprintf "%dx%d (virtual %dx%d)"
4402 state.winw state.winh
4403 state.w state.maxy)
4405 if conf.debug
4406 then
4407 src#caption2 "Position" (fun () ->
4408 Printf.sprintf "%dx%d" state.x state.y
4410 else
4411 src#caption2 "Visible" (fun () -> describe_location ()) 1
4414 sep ();
4415 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4416 "Save these parameters as global defaults at exit"
4417 (fun () -> conf.bedefault)
4418 (fun v -> conf.bedefault <- v)
4421 sep ();
4422 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4423 src#bool ~offset:0 ~btos "Extended parameters"
4424 (fun () -> !showextended)
4425 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4426 if !showextended
4427 then (
4428 src#bool "checkers"
4429 (fun () -> conf.checkers)
4430 (fun v -> conf.checkers <- v; setcheckers v);
4431 src#bool "update cursor"
4432 (fun () -> conf.updatecurs)
4433 (fun v -> conf.updatecurs <- v);
4434 src#bool "verbose"
4435 (fun () -> conf.verbose)
4436 (fun v -> conf.verbose <- v);
4437 src#bool "invert colors"
4438 (fun () -> conf.invert)
4439 (fun v -> conf.invert <- v);
4440 src#bool "max fit"
4441 (fun () -> conf.maxhfit)
4442 (fun v -> conf.maxhfit <- v);
4443 src#bool "redirect stderr"
4444 (fun () -> conf.redirectstderr)
4445 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4446 src#string "uri launcher"
4447 (fun () -> conf.urilauncher)
4448 (fun v -> conf.urilauncher <- v);
4449 src#string "path launcher"
4450 (fun () -> conf.pathlauncher)
4451 (fun v -> conf.pathlauncher <- v);
4452 src#string "tile size"
4453 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4454 (fun v ->
4456 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4457 conf.tilew <- max 64 w;
4458 conf.tileh <- max 64 h;
4459 flushtiles ();
4460 with exn ->
4461 state.text <- Printf.sprintf "bad tile size `%s': %s"
4462 v (Printexc.to_string exn));
4463 src#int "texture count"
4464 (fun () -> conf.texcount)
4465 (fun v ->
4466 if realloctexts v
4467 then conf.texcount <- v
4468 else showtext '!' " Failed to set texture count please retry later"
4470 src#int "slice height"
4471 (fun () -> conf.sliceheight)
4472 (fun v ->
4473 conf.sliceheight <- v;
4474 wcmd "sliceh %d" conf.sliceheight;
4476 src#int "anti-aliasing level"
4477 (fun () -> conf.aalevel)
4478 (fun v ->
4479 conf.aalevel <- bound v 0 8;
4480 state.anchor <- getanchor ();
4481 opendoc state.path state.password;
4483 src#string "page scroll scaling factor"
4484 (fun () -> string_of_float conf.pgscale)
4485 (fun v ->
4487 let s = float_of_string v in
4488 conf.pgscale <- s
4489 with exn ->
4490 state.text <- Printf.sprintf
4491 "bad page scroll scaling factor `%s': %s"
4492 v (Printexc.to_string exn)
4495 src#int "ui font size"
4496 (fun () -> fstate.fontsize)
4497 (fun v -> setfontsize (bound v 5 100));
4498 src#int "hint font size"
4499 (fun () -> conf.hfsize)
4500 (fun v -> conf.hfsize <- bound v 5 100);
4501 colorp "background color"
4502 (fun () -> conf.bgcolor)
4503 (fun v -> conf.bgcolor <- v);
4504 src#bool "crop hack"
4505 (fun () -> conf.crophack)
4506 (fun v -> conf.crophack <- v);
4507 src#string "trim fuzz"
4508 (fun () -> irect_to_string conf.trimfuzz)
4509 (fun v ->
4511 conf.trimfuzz <- irect_of_string v;
4512 if conf.trimmargins
4513 then settrim true conf.trimfuzz;
4514 with exn ->
4515 state.text <- Printf.sprintf "bad irect `%s': %s"
4516 v (Printexc.to_string exn)
4518 src#string "throttle"
4519 (fun () ->
4520 match conf.maxwait with
4521 | None -> "show place holder if page is not ready"
4522 | Some time ->
4523 if time = infinity
4524 then "wait for page to fully render"
4525 else
4526 "wait " ^ string_of_float time
4527 ^ " seconds before showing placeholder"
4529 (fun v ->
4531 let f = float_of_string v in
4532 if f <= 0.0
4533 then conf.maxwait <- None
4534 else conf.maxwait <- Some f
4535 with exn ->
4536 state.text <- Printf.sprintf "bad time `%s': %s"
4537 v (Printexc.to_string exn)
4539 src#string "ghyll scroll"
4540 (fun () ->
4541 match conf.ghyllscroll with
4542 | None -> ""
4543 | Some nab -> ghyllscroll_to_string nab
4545 (fun v ->
4547 let gs =
4548 if String.length v = 0
4549 then None
4550 else Some (ghyllscroll_of_string v)
4552 conf.ghyllscroll <- gs
4553 with exn ->
4554 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4555 v (Printexc.to_string exn)
4557 src#string "selection command"
4558 (fun () -> conf.selcmd)
4559 (fun v -> conf.selcmd <- v);
4560 src#string "synctex command"
4561 (fun () -> conf.stcmd)
4562 (fun v -> conf.stcmd <- v);
4563 src#colorspace "color space"
4564 (fun () -> colorspace_to_string conf.colorspace)
4565 (fun v ->
4566 conf.colorspace <- colorspace_of_int v;
4567 wcmd "cs %d" v;
4568 load state.layout;
4570 if pbousable ()
4571 then
4572 src#bool "use PBO"
4573 (fun () -> conf.usepbo)
4574 (fun v -> conf.usepbo <- v);
4575 src#bool "mouse wheel scrolls pages"
4576 (fun () -> conf.wheelbypage)
4577 (fun v -> conf.wheelbypage <- v);
4580 sep ();
4581 src#caption "Document" 0;
4582 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4583 src#caption2 "Pages"
4584 (fun () -> string_of_int state.pagecount) 1;
4585 src#caption2 "Dimensions"
4586 (fun () -> string_of_int (List.length state.pdims)) 1;
4587 if conf.trimmargins
4588 then (
4589 sep ();
4590 src#caption "Trimmed margins" 0;
4591 src#caption2 "Dimensions"
4592 (fun () -> string_of_int (List.length state.pdims)) 1;
4595 sep ();
4596 src#caption "OpenGL" 0;
4597 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4598 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4599 src#reset prevmode prevuioh;
4601 fun () ->
4602 state.text <- "";
4603 let prevmode = state.mode
4604 and prevuioh = state.uioh in
4605 fillsrc prevmode prevuioh;
4606 let source = (src :> lvsource) in
4607 let modehash = findkeyhash conf "info" in
4608 state.uioh <- coe (object (self)
4609 inherit listview ~source ~trusted:true ~modehash as super
4610 val mutable m_prevmemused = 0
4611 method infochanged = function
4612 | Memused ->
4613 if m_prevmemused != state.memused
4614 then (
4615 m_prevmemused <- state.memused;
4616 G.postRedisplay "memusedchanged";
4618 | Pdim -> G.postRedisplay "pdimchanged"
4619 | Docinfo -> fillsrc prevmode prevuioh
4621 method key key mask =
4622 if not (Wsi.withctrl mask)
4623 then
4624 match key with
4625 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4626 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4627 | _ -> super#key key mask
4628 else super#key key mask
4629 end);
4630 G.postRedisplay "info";
4633 let enterhelpmode =
4634 let source =
4635 (object
4636 inherit lvsourcebase
4637 method getitemcount = Array.length state.help
4638 method getitem n =
4639 let s, l, _ = state.help.(n) in
4640 (s, l)
4642 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4643 let optuioh =
4644 if not cancel
4645 then (
4646 m_qsearch <- qsearch;
4647 match state.help.(active) with
4648 | _, _, Action f -> Some (f uioh)
4649 | _ -> Some (uioh)
4651 else None
4653 m_active <- active;
4654 m_first <- first;
4655 m_pan <- pan;
4656 optuioh
4658 method hasaction n =
4659 match state.help.(n) with
4660 | _, _, Action _ -> true
4661 | _ -> false
4663 initializer
4664 m_active <- -1
4665 end)
4666 in fun () ->
4667 let modehash = findkeyhash conf "help" in
4668 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4669 G.postRedisplay "help";
4672 let entermsgsmode =
4673 let msgsource =
4674 let re = Str.regexp "[\r\n]" in
4675 (object
4676 inherit lvsourcebase
4677 val mutable m_items = [||]
4679 method getitemcount = 1 + Array.length m_items
4681 method getitem n =
4682 if n = 0
4683 then "[Clear]", 0
4684 else m_items.(n-1), 0
4686 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4687 ignore uioh;
4688 if not cancel
4689 then (
4690 if active = 0
4691 then Buffer.clear state.errmsgs;
4692 m_qsearch <- qsearch;
4694 m_active <- active;
4695 m_first <- first;
4696 m_pan <- pan;
4697 None
4699 method hasaction n =
4700 n = 0
4702 method reset =
4703 state.newerrmsgs <- false;
4704 let l = Str.split re (Buffer.contents state.errmsgs) in
4705 m_items <- Array.of_list l
4707 initializer
4708 m_active <- 0
4709 end)
4710 in fun () ->
4711 state.text <- "";
4712 msgsource#reset;
4713 let source = (msgsource :> lvsource) in
4714 let modehash = findkeyhash conf "listview" in
4715 state.uioh <- coe (object
4716 inherit listview ~source ~trusted:false ~modehash as super
4717 method display =
4718 if state.newerrmsgs
4719 then msgsource#reset;
4720 super#display
4721 end);
4722 G.postRedisplay "msgs";
4725 let quickbookmark ?title () =
4726 match state.layout with
4727 | [] -> ()
4728 | l :: _ ->
4729 let title =
4730 match title with
4731 | None ->
4732 let sec = Unix.gettimeofday () in
4733 let tm = Unix.localtime sec in
4734 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4735 (l.pageno+1)
4736 tm.Unix.tm_mday
4737 tm.Unix.tm_mon
4738 (tm.Unix.tm_year + 1900)
4739 tm.Unix.tm_hour
4740 tm.Unix.tm_min
4741 | Some title -> title
4743 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4746 let doreshape w h =
4747 Wsi.reshape w h;
4750 let setautoscrollspeed step goingdown =
4751 let incr = max 1 ((abs step) / 2) in
4752 let incr = if goingdown then incr else -incr in
4753 let astep = step + incr in
4754 state.autoscroll <- Some astep;
4757 let gotounder = function
4758 | Ulinkgoto (pageno, top) ->
4759 if pageno >= 0
4760 then (
4761 addnav ();
4762 gotopage1 pageno top;
4765 | Ulinkuri s ->
4766 gotouri s
4768 | Uremote (filename, pageno) ->
4769 let path =
4770 if Sys.file_exists filename
4771 then filename
4772 else
4773 let dir = Filename.dirname state.path in
4774 let path = Filename.concat dir filename in
4775 if Sys.file_exists path
4776 then path
4777 else ""
4779 if String.length path > 0
4780 then (
4781 let anchor = getanchor () in
4782 let ranchor = state.path, state.password, anchor in
4783 state.anchor <- (pageno, 0.0, 0.0);
4784 state.ranchors <- ranchor :: state.ranchors;
4785 opendoc path "";
4787 else showtext '!' ("Could not find " ^ filename)
4789 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4792 let canpan () =
4793 match conf.columns with
4794 | Csplit _ -> true
4795 | _ -> conf.zoom > 1.0
4798 let existsinrow pageno (columns, coverA, coverB) p =
4799 let last = ((pageno - coverA) mod columns) + columns in
4800 let rec any = function
4801 | [] -> false
4802 | l :: rest ->
4803 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4804 then p l
4805 else (
4806 if not (p l)
4807 then (if l.pageno = last then false else any rest)
4808 else true
4811 any state.layout
4814 let nextpage () =
4815 match state.layout with
4816 | [] -> ()
4817 | l :: rest ->
4818 match conf.columns with
4819 | Csingle _ ->
4820 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4821 then
4822 let y = clamp (pgscale state.winh) in
4823 gotoghyll y
4824 else
4825 let pageno = min (l.pageno+1) (state.pagecount-1) in
4826 gotoghyll (getpagey pageno)
4827 | Cmulti ((c, _, _) as cl, _) ->
4828 if conf.presentation
4829 && (existsinrow l.pageno cl
4830 (fun l -> l.pageh > l.pagey + l.pagevh))
4831 then
4832 let y = clamp (pgscale state.winh) in
4833 gotoghyll y
4834 else
4835 let pageno = min (l.pageno+c) (state.pagecount-1) in
4836 gotoghyll (getpagey pageno)
4837 | Csplit (n, _) ->
4838 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4839 then
4840 let pagey, pageh = getpageyh l.pageno in
4841 let pagey = pagey + pageh * l.pagecol in
4842 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4843 gotoghyll (pagey + pageh + ips)
4846 let prevpage () =
4847 match state.layout with
4848 | [] -> ()
4849 | l :: _ ->
4850 match conf.columns with
4851 | Csingle _ ->
4852 if conf.presentation && l.pagey != 0
4853 then
4854 gotoghyll (clamp (pgscale ~-(state.winh)))
4855 else
4856 let pageno = max 0 (l.pageno-1) in
4857 gotoghyll (getpagey pageno)
4858 | Cmulti ((c, _, coverB) as cl, _) ->
4859 if conf.presentation &&
4860 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4861 then
4862 gotoghyll (clamp (pgscale ~-(state.winh)))
4863 else
4864 let decr =
4865 if l.pageno = state.pagecount - coverB
4866 then 1
4867 else c
4869 let pageno = max 0 (l.pageno-decr) in
4870 gotoghyll (getpagey pageno)
4871 | Csplit (n, _) ->
4872 let y =
4873 if l.pagecol = 0
4874 then
4875 if l.pageno = 0
4876 then l.pagey
4877 else
4878 let pageno = max 0 (l.pageno-1) in
4879 let pagey, pageh = getpageyh pageno in
4880 pagey + (n-1)*pageh
4881 else
4882 let pagey, pageh = getpageyh l.pageno in
4883 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4885 gotoghyll y
4888 let viewkeyboard key mask =
4889 let enttext te =
4890 let mode = state.mode in
4891 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4892 state.text <- "";
4893 enttext ();
4894 G.postRedisplay "view:enttext"
4896 let ctrl = Wsi.withctrl mask in
4897 let key =
4898 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4900 match key with
4901 | 81 -> (* Q *)
4902 exit 0
4904 | 0xff63 -> (* insert *)
4905 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4906 then (
4907 state.mode <- LinkNav (Ltgendir 0);
4908 gotoy state.y;
4910 else showtext '!' "Keyboard link navigation does not work under rotation"
4912 | 0xff1b | 113 -> (* escape / q *)
4913 begin match state.mstate with
4914 | Mzoomrect _ ->
4915 state.mstate <- Mnone;
4916 Wsi.setcursor Wsi.CURSOR_INHERIT;
4917 G.postRedisplay "kill zoom rect";
4918 | _ ->
4919 begin match state.mode with
4920 | LinkNav _ ->
4921 state.mode <- View;
4922 G.postRedisplay "esc leave linknav"
4923 | _ ->
4924 match state.ranchors with
4925 | [] -> raise Quit
4926 | (path, password, anchor) :: rest ->
4927 state.ranchors <- rest;
4928 state.anchor <- anchor;
4929 opendoc path password
4930 end;
4931 end;
4933 | 0xff08 -> (* backspace *)
4934 gotoghyll (getnav ~-1)
4936 | 111 -> (* o *)
4937 enteroutlinemode ()
4939 | 117 -> (* u *)
4940 state.rects <- [];
4941 state.text <- "";
4942 G.postRedisplay "dehighlight";
4944 | 47 | 63 -> (* / ? *)
4945 let ondone isforw s =
4946 cbput state.hists.pat s;
4947 state.searchpattern <- s;
4948 search s isforw
4950 let s = String.create 1 in
4951 s.[0] <- Char.chr key;
4952 enttext (s, "", Some (onhist state.hists.pat),
4953 textentry, ondone (key = 47), true)
4955 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4956 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4957 setzoom (conf.zoom +. incr)
4959 | 43 | 0xffab -> (* + *)
4960 let ondone s =
4961 let n =
4962 try int_of_string s with exc ->
4963 state.text <- Printf.sprintf "bad integer `%s': %s"
4964 s (Printexc.to_string exc);
4965 max_int
4967 if n != max_int
4968 then (
4969 conf.pagebias <- n;
4970 state.text <- "page bias is now " ^ string_of_int n;
4973 enttext ("page bias: ", "", None, intentry, ondone, true)
4975 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4976 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4977 setzoom (max 0.01 (conf.zoom -. decr))
4979 | 45 | 0xffad -> (* - *)
4980 let ondone msg = state.text <- msg in
4981 enttext (
4982 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4983 optentry state.mode, ondone, true
4986 | 48 when ctrl -> (* ctrl-0 *)
4987 setzoom 1.0
4989 | 49 when ctrl -> (* ctrl-1 *)
4990 let cols =
4991 match conf.columns with
4992 | Csingle _ | Cmulti _ -> 1
4993 | Csplit (n, _) -> n
4995 let zoom = zoomforh state.winw state.winh state.scrollw cols in
4996 if zoom < 1.0
4997 then setzoom zoom
4999 | 0xffc6 -> (* f9 *)
5000 togglebirdseye ()
5002 | 57 when ctrl -> (* ctrl-9 *)
5003 togglebirdseye ()
5005 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5006 when not ctrl -> (* 0..9 *)
5007 let ondone s =
5008 let n =
5009 try int_of_string s with exc ->
5010 state.text <- Printf.sprintf "bad integer `%s': %s"
5011 s (Printexc.to_string exc);
5014 if n >= 0
5015 then (
5016 addnav ();
5017 cbput state.hists.pag (string_of_int n);
5018 gotopage1 (n + conf.pagebias - 1) 0;
5021 let pageentry text key =
5022 match Char.unsafe_chr key with
5023 | 'g' -> TEdone text
5024 | _ -> intentry text key
5026 let text = "x" in text.[0] <- Char.chr key;
5027 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5029 | 98 -> (* b *)
5030 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5031 reshape state.winw state.winh;
5033 | 108 -> (* l *)
5034 conf.hlinks <- not conf.hlinks;
5035 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5036 G.postRedisplay "toggle highlightlinks";
5038 | 70 -> (* F *)
5039 state.glinks <- true;
5040 let mode = state.mode in
5041 state.mode <- Textentry (
5042 (":", "", None, linknentry, linkndone gotounder, false),
5043 (fun _ ->
5044 state.glinks <- false;
5045 state.mode <- mode)
5047 state.text <- "";
5048 G.postRedisplay "view:linkent(F)"
5050 | 121 -> (* y *)
5051 state.glinks <- true;
5052 let mode = state.mode in
5053 state.mode <- Textentry (
5054 (":", "", None, linknentry, linkndone (fun under ->
5055 match Ne.pipe () with
5056 | Ne.Exn exn ->
5057 showtext '!' (Printf.sprintf "pipe failed: %s"
5058 (Printexc.to_string exn));
5059 | Ne.Res (r, w) ->
5060 let popened =
5061 try popen conf.selcmd [r, 0; w, -1]; true
5062 with exn ->
5063 showtext '!'
5064 (Printf.sprintf "failed to execute %s: %s"
5065 conf.selcmd (Printexc.to_string exn));
5066 false
5068 let clo cap fd =
5069 Ne.clo fd (fun msg ->
5070 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5073 let s = undertext under in
5074 if popened
5075 then
5076 (try
5077 let l = String.length s in
5078 let n = tempfailureretry (Unix.write w s 0) l in
5079 if n != l
5080 then
5081 showtext '!'
5082 (Printf.sprintf
5083 "failed to write %d characters to sel pipe, wrote %d"
5086 with exn ->
5087 showtext '!'
5088 (Printf.sprintf "failed to write to sel pipe: %s"
5089 (Printexc.to_string exn)
5092 else dolog "%s" s;
5093 clo "pipe/r" r;
5094 clo "pipe/w" w;
5095 ), false
5097 fun _ ->
5098 state.glinks <- false;
5099 state.mode <- mode
5101 state.text <- "";
5102 G.postRedisplay "view:linkent"
5104 | 97 -> (* a *)
5105 begin match state.autoscroll with
5106 | Some step ->
5107 conf.autoscrollstep <- step;
5108 state.autoscroll <- None
5109 | None ->
5110 if conf.autoscrollstep = 0
5111 then state.autoscroll <- Some 1
5112 else state.autoscroll <- Some conf.autoscrollstep
5115 | 112 when ctrl -> (* ctrl-p *)
5116 launchpath ()
5118 | 80 -> (* P *)
5119 setpresentationmode (not conf.presentation);
5120 showtext ' ' ("presentation mode " ^
5121 if conf.presentation then "on" else "off");
5123 | 102 -> (* f *)
5124 if List.mem Wsi.Fullscreen state.winstate
5125 then doreshape conf.cwinw conf.cwinh
5126 else Wsi.fullscreen ()
5128 | 112 | 78 -> (* p|N *)
5129 search state.searchpattern false
5131 | 110 | 0xffc0 -> (* n|F3 *)
5132 search state.searchpattern true
5134 | 116 -> (* t *)
5135 begin match state.layout with
5136 | [] -> ()
5137 | l :: _ ->
5138 gotoghyll (getpagey l.pageno)
5141 | 32 -> (* space *)
5142 nextpage ()
5144 | 0xff9f | 0xffff -> (* delete *)
5145 prevpage ()
5147 | 61 -> (* = *)
5148 showtext ' ' (describe_location ());
5150 | 119 -> (* w *)
5151 begin match state.layout with
5152 | [] -> ()
5153 | l :: _ ->
5154 doreshape (l.pagew + state.scrollw) l.pageh;
5155 G.postRedisplay "w"
5158 | 39 -> (* ' *)
5159 enterbookmarkmode ()
5161 | 104 | 0xffbe -> (* h|F1 *)
5162 enterhelpmode ()
5164 | 105 -> (* i *)
5165 enterinfomode ()
5167 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5168 entermsgsmode ()
5170 | 109 -> (* m *)
5171 let ondone s =
5172 match state.layout with
5173 | l :: _ ->
5174 if String.length s > 0
5175 then
5176 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5177 | _ -> ()
5179 enttext ("bookmark: ", "", None, textentry, ondone, true)
5181 | 126 -> (* ~ *)
5182 quickbookmark ();
5183 showtext ' ' "Quick bookmark added";
5185 | 122 -> (* z *)
5186 begin match state.layout with
5187 | l :: _ ->
5188 let rect = getpdimrect l.pagedimno in
5189 let w, h =
5190 if conf.crophack
5191 then
5192 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5193 truncate (1.2 *. (rect.(3) -. rect.(0))))
5194 else
5195 (truncate (rect.(1) -. rect.(0)),
5196 truncate (rect.(3) -. rect.(0)))
5198 let w = truncate ((float w)*.conf.zoom)
5199 and h = truncate ((float h)*.conf.zoom) in
5200 if w != 0 && h != 0
5201 then (
5202 state.anchor <- getanchor ();
5203 doreshape (w + state.scrollw) (h + conf.interpagespace)
5205 G.postRedisplay "z";
5207 | [] -> ()
5210 | 50 when ctrl -> (* ctrl-2 *)
5211 let maxw = getmaxw () in
5212 if maxw > 0.0
5213 then setzoom (maxw /. float state.winw)
5215 | 60 | 62 -> (* < > *)
5216 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5218 | 91 | 93 -> (* [ ] *)
5219 conf.colorscale <-
5220 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5222 G.postRedisplay "brightness";
5224 | 99 when state.mode = View -> (* c *)
5225 let (c, a, b), z =
5226 match state.prevcolumns with
5227 | None -> (1, 0, 0), 1.0
5228 | Some (columns, z) ->
5229 let cab =
5230 match columns with
5231 | Csplit (c, _) -> -c, 0, 0
5232 | Cmulti ((c, a, b), _) -> c, a, b
5233 | Csingle _ -> 1, 0, 0
5235 cab, z
5237 setcolumns View c a b;
5238 setzoom z;
5240 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5241 setzoom state.prevzoom
5243 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5244 begin match state.autoscroll with
5245 | None ->
5246 begin match state.mode with
5247 | Birdseye beye -> upbirdseye 1 beye
5248 | _ ->
5249 if ctrl
5250 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5251 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5253 | Some n ->
5254 setautoscrollspeed n false
5257 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5258 begin match state.autoscroll with
5259 | None ->
5260 begin match state.mode with
5261 | Birdseye beye -> downbirdseye 1 beye
5262 | _ ->
5263 if ctrl
5264 then gotoy_and_clear_text (clamp (state.winh/2))
5265 else gotoy_and_clear_text (clamp conf.scrollstep)
5267 | Some n ->
5268 setautoscrollspeed n true
5271 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5272 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5273 if canpan ()
5274 then
5275 let dx =
5276 if ctrl
5277 then state.winw / 2
5278 else conf.hscrollstep
5280 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5281 state.x <- state.x + dx;
5282 gotoy_and_clear_text state.y
5283 else (
5284 state.text <- "";
5285 G.postRedisplay "lef/right"
5288 | 0xff55 | 0xff9a -> (* (kp) prior *)
5289 let y =
5290 if ctrl
5291 then
5292 match state.layout with
5293 | [] -> state.y
5294 | l :: _ -> state.y - l.pagey
5295 else
5296 clamp (pgscale (-state.winh))
5298 gotoghyll y
5300 | 0xff56 | 0xff9b -> (* (kp) next *)
5301 let y =
5302 if ctrl
5303 then
5304 match List.rev state.layout with
5305 | [] -> state.y
5306 | l :: _ -> getpagey l.pageno
5307 else
5308 clamp (pgscale state.winh)
5310 gotoghyll y
5312 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5313 gotoghyll 0
5314 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5315 gotoghyll (clamp state.maxy)
5317 | 0xff53 | 0xff98
5318 when Wsi.withalt mask -> (* alt-(kp) right *)
5319 gotoghyll (getnav 1)
5320 | 0xff51 | 0xff96
5321 when Wsi.withalt mask -> (* alt-(kp) left *)
5322 gotoghyll (getnav ~-1)
5324 | 114 -> (* r *)
5325 reload ()
5327 | 118 when conf.debug -> (* v *)
5328 state.rects <- [];
5329 List.iter (fun l ->
5330 match getopaque l.pageno with
5331 | None -> ()
5332 | Some opaque ->
5333 let x0, y0, x1, y1 = pagebbox opaque in
5334 let a,b = float x0, float y0 in
5335 let c,d = float x1, float y0 in
5336 let e,f = float x1, float y1 in
5337 let h,j = float x0, float y1 in
5338 let rect = (a,b,c,d,e,f,h,j) in
5339 debugrect rect;
5340 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5341 ) state.layout;
5342 G.postRedisplay "v";
5344 | _ ->
5345 vlog "huh? %s" (Wsi.keyname key)
5348 let linknavkeyboard key mask linknav =
5349 let getpage pageno =
5350 let rec loop = function
5351 | [] -> None
5352 | l :: _ when l.pageno = pageno -> Some l
5353 | _ :: rest -> loop rest
5354 in loop state.layout
5356 let doexact (pageno, n) =
5357 match getopaque pageno, getpage pageno with
5358 | Some opaque, Some l ->
5359 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5360 then
5361 let under = getlink opaque n in
5362 G.postRedisplay "link gotounder";
5363 gotounder under;
5364 state.mode <- View;
5365 else
5366 let opt, dir =
5367 match key with
5368 | 0xff50 -> (* home *)
5369 Some (findlink opaque LDfirst), -1
5371 | 0xff57 -> (* end *)
5372 Some (findlink opaque LDlast), 1
5374 | 0xff51 -> (* left *)
5375 Some (findlink opaque (LDleft n)), -1
5377 | 0xff53 -> (* right *)
5378 Some (findlink opaque (LDright n)), 1
5380 | 0xff52 -> (* up *)
5381 Some (findlink opaque (LDup n)), -1
5383 | 0xff54 -> (* down *)
5384 Some (findlink opaque (LDdown n)), 1
5386 | _ -> None, 0
5388 let pwl l dir =
5389 begin match findpwl l.pageno dir with
5390 | Pwlnotfound -> ()
5391 | Pwl pageno ->
5392 let notfound dir =
5393 state.mode <- LinkNav (Ltgendir dir);
5394 let y, h = getpageyh pageno in
5395 let y =
5396 if dir < 0
5397 then y + h - state.winh
5398 else y
5400 gotoy y
5402 begin match getopaque pageno, getpage pageno with
5403 | Some opaque, Some _ ->
5404 let link =
5405 let ld = if dir > 0 then LDfirst else LDlast in
5406 findlink opaque ld
5408 begin match link with
5409 | Lfound m ->
5410 showlinktype (getlink opaque m);
5411 state.mode <- LinkNav (Ltexact (pageno, m));
5412 G.postRedisplay "linknav jpage";
5413 | _ -> notfound dir
5414 end;
5415 | _ -> notfound dir
5416 end;
5417 end;
5419 begin match opt with
5420 | Some Lnotfound -> pwl l dir;
5421 | Some (Lfound m) ->
5422 if m = n
5423 then pwl l dir
5424 else (
5425 let _, y0, _, y1 = getlinkrect opaque m in
5426 if y0 < l.pagey
5427 then gotopage1 l.pageno y0
5428 else (
5429 let d = fstate.fontsize + 1 in
5430 if y1 - l.pagey > l.pagevh - d
5431 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5432 else G.postRedisplay "linknav";
5434 showlinktype (getlink opaque m);
5435 state.mode <- LinkNav (Ltexact (l.pageno, m));
5438 | None -> viewkeyboard key mask
5439 end;
5440 | _ -> viewkeyboard key mask
5442 if key = 0xff63
5443 then (
5444 state.mode <- View;
5445 G.postRedisplay "leave linknav"
5447 else
5448 match linknav with
5449 | Ltgendir _ -> viewkeyboard key mask
5450 | Ltexact exact -> doexact exact
5453 let keyboard key mask =
5454 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5455 then wcmd "interrupt"
5456 else state.uioh <- state.uioh#key key mask
5459 let birdseyekeyboard key mask
5460 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5461 let incr =
5462 match conf.columns with
5463 | Csingle _ -> 1
5464 | Cmulti ((c, _, _), _) -> c
5465 | Csplit _ -> failwith "bird's eye split mode"
5467 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5468 match key with
5469 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5470 let y, h = getpageyh pageno in
5471 let top = (state.winh - h) / 2 in
5472 gotoy (max 0 (y - top))
5473 | 0xff0d (* enter *)
5474 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5475 | 0xff1b -> leavebirdseye beye true (* escape *)
5476 | 0xff52 -> upbirdseye incr beye (* up *)
5477 | 0xff54 -> downbirdseye incr beye (* down *)
5478 | 0xff51 -> upbirdseye 1 beye (* left *)
5479 | 0xff53 -> downbirdseye 1 beye (* right *)
5481 | 0xff55 -> (* prior *)
5482 begin match state.layout with
5483 | l :: _ ->
5484 if l.pagey != 0
5485 then (
5486 state.mode <- Birdseye (
5487 oconf, leftx, l.pageno, hooverpageno, anchor
5489 gotopage1 l.pageno 0;
5491 else (
5492 let layout = layout (state.y-state.winh) (pgh state.layout) in
5493 match layout with
5494 | [] -> gotoy (clamp (-state.winh))
5495 | l :: _ ->
5496 state.mode <- Birdseye (
5497 oconf, leftx, l.pageno, hooverpageno, anchor
5499 gotopage1 l.pageno 0
5502 | [] -> gotoy (clamp (-state.winh))
5503 end;
5505 | 0xff56 -> (* next *)
5506 begin match List.rev state.layout with
5507 | l :: _ ->
5508 let layout = layout (state.y + (pgh state.layout)) state.winh in
5509 begin match layout with
5510 | [] ->
5511 let incr = l.pageh - l.pagevh in
5512 if incr = 0
5513 then (
5514 state.mode <-
5515 Birdseye (
5516 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5518 G.postRedisplay "birdseye pagedown";
5520 else gotoy (clamp (incr + conf.interpagespace*2));
5522 | l :: _ ->
5523 state.mode <-
5524 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5525 gotopage1 l.pageno 0;
5528 | [] -> gotoy (clamp state.winh)
5529 end;
5531 | 0xff50 -> (* home *)
5532 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5533 gotopage1 0 0
5535 | 0xff57 -> (* end *)
5536 let pageno = state.pagecount - 1 in
5537 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5538 if not (pagevisible state.layout pageno)
5539 then
5540 let h =
5541 match List.rev state.pdims with
5542 | [] -> state.winh
5543 | (_, _, h, _) :: _ -> h
5545 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5546 else G.postRedisplay "birdseye end";
5547 | _ -> viewkeyboard key mask
5550 let drawpage l linkindexbase =
5551 let color =
5552 match state.mode with
5553 | Textentry _ -> scalecolor 0.4
5554 | LinkNav _
5555 | View -> scalecolor 1.0
5556 | Birdseye (_, _, pageno, hooverpageno, _) ->
5557 if l.pageno = hooverpageno
5558 then scalecolor 0.9
5559 else (
5560 if l.pageno = pageno
5561 then scalecolor 1.0
5562 else scalecolor 0.8
5565 drawtiles l color;
5566 begin match getopaque l.pageno with
5567 | Some opaque ->
5568 if tileready l l.pagex l.pagey
5569 then
5570 let x = l.pagedispx - l.pagex
5571 and y = l.pagedispy - l.pagey in
5572 let hlmask =
5573 match conf.columns with
5574 | Csingle _ | Cmulti _ ->
5575 (if conf.hlinks then 1 else 0)
5576 + (if state.glinks
5577 && not (isbirdseye state.mode) then 2 else 0)
5578 | _ -> 0
5580 let s =
5581 match state.mode with
5582 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5583 | _ -> ""
5585 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5586 else 0
5588 | _ -> 0
5589 end;
5592 let scrollindicator () =
5593 let sbw, ph, sh = state.uioh#scrollph in
5594 let sbh, pw, sw = state.uioh#scrollpw in
5596 GlDraw.color (0.64, 0.64, 0.64);
5597 GlDraw.rect
5598 (float (state.winw - sbw), 0.)
5599 (float state.winw, float state.winh)
5601 GlDraw.rect
5602 (0., float (state.winh - sbh))
5603 (float (state.winw - state.scrollw - 1), float state.winh)
5605 GlDraw.color (0.0, 0.0, 0.0);
5607 GlDraw.rect
5608 (float (state.winw - sbw), ph)
5609 (float state.winw, ph +. sh)
5611 GlDraw.rect
5612 (pw, float (state.winh - sbh))
5613 (pw +. sw, float state.winh)
5617 let showsel () =
5618 match state.mstate with
5619 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5622 | Msel ((x0, y0), (x1, y1)) ->
5623 let rec loop = function
5624 | l :: ls ->
5625 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5626 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5627 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5628 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5629 then
5630 match getopaque l.pageno with
5631 | Some opaque ->
5632 let x0, y0 = pagetranslatepoint l x0 y0 in
5633 let x1, y1 = pagetranslatepoint l x1 y1 in
5634 seltext opaque (x0, y0, x1, y1);
5635 | _ -> ()
5636 else loop ls
5637 | [] -> ()
5639 loop state.layout
5642 let showrects rects =
5643 Gl.enable `blend;
5644 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5645 GlDraw.polygon_mode `both `fill;
5646 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5647 List.iter
5648 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5649 List.iter (fun l ->
5650 if l.pageno = pageno
5651 then (
5652 let dx = float (l.pagedispx - l.pagex) in
5653 let dy = float (l.pagedispy - l.pagey) in
5654 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5655 GlDraw.begins `quads;
5657 GlDraw.vertex2 (x0+.dx, y0+.dy);
5658 GlDraw.vertex2 (x1+.dx, y1+.dy);
5659 GlDraw.vertex2 (x2+.dx, y2+.dy);
5660 GlDraw.vertex2 (x3+.dx, y3+.dy);
5662 GlDraw.ends ();
5664 ) state.layout
5665 ) rects
5667 Gl.disable `blend;
5670 let display () =
5671 GlClear.color (scalecolor2 conf.bgcolor);
5672 GlClear.clear [`color];
5673 let rec loop linkindexbase = function
5674 | l :: rest ->
5675 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5676 loop linkindexbase rest
5677 | [] -> ()
5679 loop 0 state.layout;
5680 let rects =
5681 match state.mode with
5682 | LinkNav (Ltexact (pageno, linkno)) ->
5683 begin match getopaque pageno with
5684 | Some opaque ->
5685 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5686 (pageno, 5, (
5687 float x0, float y0,
5688 float x1, float y0,
5689 float x1, float y1,
5690 float x0, float y1)
5691 ) :: state.rects
5692 | None -> state.rects
5694 | _ -> state.rects
5696 showrects rects;
5697 showsel ();
5698 state.uioh#display;
5699 begin match state.mstate with
5700 | Mzoomrect ((x0, y0), (x1, y1)) ->
5701 Gl.enable `blend;
5702 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5703 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5704 GlDraw.rect (float x0, float y0)
5705 (float x1, float y1);
5706 Gl.disable `blend;
5707 | _ -> ()
5708 end;
5709 enttext ();
5710 scrollindicator ();
5711 Wsi.swapb ();
5714 let zoomrect x y x1 y1 =
5715 let x0 = min x x1
5716 and x1 = max x x1
5717 and y0 = min y y1 in
5718 gotoy (state.y + y0);
5719 state.anchor <- getanchor ();
5720 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5721 let margin =
5722 if state.w < state.winw - state.scrollw
5723 then (state.winw - state.scrollw - state.w) / 2
5724 else 0
5726 state.x <- (state.x + margin) - x0;
5727 setzoom zoom;
5728 Wsi.setcursor Wsi.CURSOR_INHERIT;
5729 state.mstate <- Mnone;
5732 let scrollx x =
5733 let winw = state.winw - state.scrollw - 1 in
5734 let s = float x /. float winw in
5735 let destx = truncate (float (state.w + winw) *. s) in
5736 state.x <- winw - destx;
5737 gotoy_and_clear_text state.y;
5738 state.mstate <- Mscrollx;
5741 let scrolly y =
5742 let s = float y /. float state.winh in
5743 let desty = truncate (float (state.maxy - state.winh) *. s) in
5744 gotoy_and_clear_text desty;
5745 state.mstate <- Mscrolly;
5748 let viewmouse button down x y mask =
5749 match button with
5750 | n when (n == 4 || n == 5) && not down ->
5751 if Wsi.withctrl mask
5752 then (
5753 match state.mstate with
5754 | Mzoom (oldn, i) ->
5755 if oldn = n
5756 then (
5757 if i = 2
5758 then
5759 let incr =
5760 match n with
5761 | 5 ->
5762 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5763 | _ ->
5764 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5766 let zoom = conf.zoom -. incr in
5767 setzoom zoom;
5768 state.mstate <- Mzoom (n, 0);
5769 else
5770 state.mstate <- Mzoom (n, i+1);
5772 else state.mstate <- Mzoom (n, 0)
5774 | _ -> state.mstate <- Mzoom (n, 0)
5776 else (
5777 match state.autoscroll with
5778 | Some step -> setautoscrollspeed step (n=4)
5779 | None ->
5780 if conf.wheelbypage
5781 then (
5782 if n = 4
5783 then prevpage ()
5784 else nextpage ()
5786 else
5787 let incr =
5788 if n = 4
5789 then -conf.scrollstep
5790 else conf.scrollstep
5792 let incr = incr * 2 in
5793 let y = clamp incr in
5794 gotoy_and_clear_text y
5797 | n when (n = 6 || n = 7) && not down && canpan () ->
5798 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5799 gotoy_and_clear_text state.y
5801 | 1 when Wsi.withshift mask ->
5802 state.mstate <- Mnone;
5803 if not down then (
5804 match unproject x y with
5805 | Some (pageno, ux, uy) ->
5806 let cmd = Printf.sprintf
5807 "%s %s %d %d %d"
5808 conf.stcmd state.path pageno ux uy
5810 popen cmd []
5811 | None -> ()
5814 | 1 when Wsi.withctrl mask ->
5815 if down
5816 then (
5817 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5818 state.mstate <- Mpan (x, y)
5820 else
5821 state.mstate <- Mnone
5823 | 3 ->
5824 if down
5825 then (
5826 Wsi.setcursor Wsi.CURSOR_CYCLE;
5827 let p = (x, y) in
5828 state.mstate <- Mzoomrect (p, p)
5830 else (
5831 match state.mstate with
5832 | Mzoomrect ((x0, y0), _) ->
5833 if abs (x-x0) > 10 && abs (y - y0) > 10
5834 then zoomrect x0 y0 x y
5835 else (
5836 state.mstate <- Mnone;
5837 Wsi.setcursor Wsi.CURSOR_INHERIT;
5838 G.postRedisplay "kill accidental zoom rect";
5840 | _ ->
5841 Wsi.setcursor Wsi.CURSOR_INHERIT;
5842 state.mstate <- Mnone
5845 | 1 when x > state.winw - state.scrollw ->
5846 if down
5847 then
5848 let _, position, sh = state.uioh#scrollph in
5849 if y > truncate position && y < truncate (position +. sh)
5850 then state.mstate <- Mscrolly
5851 else scrolly y
5852 else
5853 state.mstate <- Mnone
5855 | 1 when y > state.winh - state.hscrollh ->
5856 if down
5857 then
5858 let _, position, sw = state.uioh#scrollpw in
5859 if x > truncate position && x < truncate (position +. sw)
5860 then state.mstate <- Mscrollx
5861 else scrollx x
5862 else
5863 state.mstate <- Mnone
5865 | 1 ->
5866 let dest = if down then getunder x y else Unone in
5867 begin match dest with
5868 | Ulinkgoto _
5869 | Ulinkuri _
5870 | Uremote _
5871 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5872 gotounder dest
5874 | Unone when down ->
5875 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5876 state.mstate <- Mpan (x, y);
5878 | Unone | Utext _ ->
5879 if down
5880 then (
5881 if conf.angle mod 360 = 0
5882 then (
5883 state.mstate <- Msel ((x, y), (x, y));
5884 G.postRedisplay "mouse select";
5887 else (
5888 match state.mstate with
5889 | Mnone -> ()
5891 | Mzoom _ | Mscrollx | Mscrolly ->
5892 state.mstate <- Mnone
5894 | Mzoomrect ((x0, y0), _) ->
5895 zoomrect x0 y0 x y
5897 | Mpan _ ->
5898 Wsi.setcursor Wsi.CURSOR_INHERIT;
5899 state.mstate <- Mnone
5901 | Msel ((x0, y0), (x1, y1)) ->
5902 let rec loop = function
5903 | [] -> ()
5904 | l :: rest ->
5905 let inside =
5906 let a0 = l.pagedispy in
5907 let a1 = a0 + l.pagevh in
5908 let b0 = l.pagedispx in
5909 let b1 = b0 + l.pagevw in
5910 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5911 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5913 if inside
5914 then
5915 match getopaque l.pageno with
5916 | Some opaque ->
5917 begin
5918 match Ne.pipe () with
5919 | Ne.Exn exn ->
5920 showtext '!'
5921 (Printf.sprintf
5922 "can not create sel pipe: %s"
5923 (Printexc.to_string exn));
5924 | Ne.Res (r, w) ->
5925 let doclose what fd =
5926 Ne.clo fd (fun msg ->
5927 dolog "%s close failed: %s" what msg)
5930 popen conf.selcmd [r, 0; w, -1];
5931 copysel w opaque;
5932 doclose "pipe/r" r;
5933 G.postRedisplay "copysel";
5934 with exn ->
5935 dolog "can not execute %S: %s"
5936 conf.selcmd (Printexc.to_string exn);
5937 doclose "pipe/r" r;
5938 doclose "pipe/w" w;
5940 | None -> ()
5941 else loop rest
5943 loop state.layout;
5944 Wsi.setcursor Wsi.CURSOR_INHERIT;
5945 state.mstate <- Mnone;
5949 | _ -> ()
5952 let birdseyemouse button down x y mask
5953 (conf, leftx, _, hooverpageno, anchor) =
5954 match button with
5955 | 1 when down ->
5956 let rec loop = function
5957 | [] -> ()
5958 | l :: rest ->
5959 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5960 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5961 then (
5962 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5964 else loop rest
5966 loop state.layout
5967 | 3 -> ()
5968 | _ -> viewmouse button down x y mask
5971 let mouse button down x y mask =
5972 state.uioh <- state.uioh#button button down x y mask;
5975 let motion ~x ~y =
5976 state.uioh <- state.uioh#motion x y
5979 let pmotion ~x ~y =
5980 state.uioh <- state.uioh#pmotion x y;
5983 let uioh = object
5984 method display = ()
5986 method key key mask =
5987 begin match state.mode with
5988 | Textentry textentry -> textentrykeyboard key mask textentry
5989 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5990 | View -> viewkeyboard key mask
5991 | LinkNav linknav -> linknavkeyboard key mask linknav
5992 end;
5993 state.uioh
5995 method button button bstate x y mask =
5996 begin match state.mode with
5997 | LinkNav _
5998 | View -> viewmouse button bstate x y mask
5999 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6000 | Textentry _ -> ()
6001 end;
6002 state.uioh
6004 method motion x y =
6005 begin match state.mode with
6006 | Textentry _ -> ()
6007 | View | Birdseye _ | LinkNav _ ->
6008 match state.mstate with
6009 | Mzoom _ | Mnone -> ()
6011 | Mpan (x0, y0) ->
6012 let dx = x - x0
6013 and dy = y0 - y in
6014 state.mstate <- Mpan (x, y);
6015 if canpan ()
6016 then state.x <- state.x + dx;
6017 let y = clamp dy in
6018 gotoy_and_clear_text y
6020 | Msel (a, _) ->
6021 state.mstate <- Msel (a, (x, y));
6022 G.postRedisplay "motion select";
6024 | Mscrolly ->
6025 let y = min state.winh (max 0 y) in
6026 scrolly y
6028 | Mscrollx ->
6029 let x = min state.winw (max 0 x) in
6030 scrollx x
6032 | Mzoomrect (p0, _) ->
6033 state.mstate <- Mzoomrect (p0, (x, y));
6034 G.postRedisplay "motion zoomrect";
6035 end;
6036 state.uioh
6038 method pmotion x y =
6039 begin match state.mode with
6040 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6041 let rec loop = function
6042 | [] ->
6043 if hooverpageno != -1
6044 then (
6045 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6046 G.postRedisplay "pmotion birdseye no hoover";
6048 | l :: rest ->
6049 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6050 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6051 then (
6052 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6053 G.postRedisplay "pmotion birdseye hoover";
6055 else loop rest
6057 loop state.layout
6059 | Textentry _ -> ()
6061 | LinkNav _
6062 | View ->
6063 match state.mstate with
6064 | Mnone -> updateunder x y
6065 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6067 end;
6068 state.uioh
6070 method infochanged _ = ()
6072 method scrollph =
6073 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6074 let p, h = scrollph state.y maxy in
6075 state.scrollw, p, h
6077 method scrollpw =
6078 let winw = state.winw - state.scrollw - 1 in
6079 let fwinw = float winw in
6080 let sw =
6081 let sw = fwinw /. float state.w in
6082 let sw = fwinw *. sw in
6083 max sw (float conf.scrollh)
6085 let position, sw =
6086 let f = state.w+winw in
6087 let r = float (winw-state.x) /. float f in
6088 let p = fwinw *. r in
6089 p-.sw/.2., sw
6091 let sw =
6092 if position +. sw > fwinw
6093 then fwinw -. position
6094 else sw
6096 state.hscrollh, position, sw
6098 method modehash =
6099 let modename =
6100 match state.mode with
6101 | LinkNav _ -> "links"
6102 | Textentry _ -> "textentry"
6103 | Birdseye _ -> "birdseye"
6104 | View -> "view"
6106 findkeyhash conf modename
6107 end;;
6109 module Config =
6110 struct
6111 open Parser
6113 let fontpath = ref "";;
6115 module KeyMap =
6116 Map.Make (struct type t = (int * int) let compare = compare end);;
6118 let unent s =
6119 let l = String.length s in
6120 let b = Buffer.create l in
6121 unent b s 0 l;
6122 Buffer.contents b;
6125 let home =
6126 try Sys.getenv "HOME"
6127 with exn ->
6128 prerr_endline
6129 ("Can not determine home directory location: " ^
6130 Printexc.to_string exn);
6134 let modifier_of_string = function
6135 | "alt" -> Wsi.altmask
6136 | "shift" -> Wsi.shiftmask
6137 | "ctrl" | "control" -> Wsi.ctrlmask
6138 | "meta" -> Wsi.metamask
6139 | _ -> 0
6142 let key_of_string =
6143 let r = Str.regexp "-" in
6144 fun s ->
6145 let elems = Str.full_split r s in
6146 let f n k m =
6147 let g s =
6148 let m1 = modifier_of_string s in
6149 if m1 = 0
6150 then (Wsi.namekey s, m)
6151 else (k, m lor m1)
6152 in function
6153 | Str.Delim s when n land 1 = 0 -> g s
6154 | Str.Text s -> g s
6155 | Str.Delim _ -> (k, m)
6157 let rec loop n k m = function
6158 | [] -> (k, m)
6159 | x :: xs ->
6160 let k, m = f n k m x in
6161 loop (n+1) k m xs
6163 loop 0 0 0 elems
6166 let keys_of_string =
6167 let r = Str.regexp "[ \t]" in
6168 fun s ->
6169 let elems = Str.split r s in
6170 List.map key_of_string elems
6173 let copykeyhashes c =
6174 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6177 let config_of c attrs =
6178 let apply c k v =
6180 match k with
6181 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6182 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6183 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6184 | "preload" -> { c with preload = bool_of_string v }
6185 | "page-bias" -> { c with pagebias = int_of_string v }
6186 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6187 | "horizontal-scroll-step" ->
6188 { c with hscrollstep = max (int_of_string v) 1 }
6189 | "auto-scroll-step" ->
6190 { c with autoscrollstep = max 0 (int_of_string v) }
6191 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6192 | "crop-hack" -> { c with crophack = bool_of_string v }
6193 | "throttle" ->
6194 let mw =
6195 match String.lowercase v with
6196 | "true" -> Some infinity
6197 | "false" -> None
6198 | f -> Some (float_of_string f)
6200 { c with maxwait = mw}
6201 | "highlight-links" -> { c with hlinks = bool_of_string v }
6202 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6203 | "vertical-margin" ->
6204 { c with interpagespace = max 0 (int_of_string v) }
6205 | "zoom" ->
6206 let zoom = float_of_string v /. 100. in
6207 let zoom = max zoom 0.0 in
6208 { c with zoom = zoom }
6209 | "presentation" -> { c with presentation = bool_of_string v }
6210 | "rotation-angle" -> { c with angle = int_of_string v }
6211 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6212 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6213 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6214 | "proportional-display" -> { c with proportional = bool_of_string v }
6215 | "pixmap-cache-size" ->
6216 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6217 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6218 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6219 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6220 | "persistent-location" -> { c with jumpback = bool_of_string v }
6221 | "background-color" -> { c with bgcolor = color_of_string v }
6222 | "scrollbar-in-presentation" ->
6223 { c with scrollbarinpm = bool_of_string v }
6224 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6225 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6226 | "mupdf-store-size" ->
6227 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6228 | "checkers" -> { c with checkers = bool_of_string v }
6229 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6230 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6231 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6232 | "uri-launcher" -> { c with urilauncher = unent v }
6233 | "path-launcher" -> { c with pathlauncher = unent v }
6234 | "color-space" -> { c with colorspace = colorspace_of_string v }
6235 | "invert-colors" -> { c with invert = bool_of_string v }
6236 | "brightness" -> { c with colorscale = float_of_string v }
6237 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6238 | "ghyllscroll" ->
6239 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6240 | "columns" ->
6241 let (n, _, _) as nab = multicolumns_of_string v in
6242 if n < 0
6243 then { c with columns = Csplit (-n, [||]) }
6244 else { c with columns = Cmulti (nab, [||]) }
6245 | "birds-eye-columns" ->
6246 { c with beyecolumns = Some (max (int_of_string v) 2) }
6247 | "selection-command" -> { c with selcmd = unent v }
6248 | "synctex-command" -> { c with stcmd = unent v }
6249 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6250 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6251 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6252 | "use-pbo" -> { c with usepbo = bool_of_string v }
6253 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6254 | _ -> c
6255 with exn ->
6256 prerr_endline ("Error processing attribute (`" ^
6257 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6260 let rec fold c = function
6261 | [] -> c
6262 | (k, v) :: rest ->
6263 let c = apply c k v in
6264 fold c rest
6266 fold { c with keyhashes = copykeyhashes c } attrs;
6269 let fromstring f pos n v d =
6270 try f v
6271 with exn ->
6272 dolog "Error processing attribute (%S=%S) at %d\n%s"
6273 n v pos (Printexc.to_string exn)
6278 let bookmark_of attrs =
6279 let rec fold title page rely visy = function
6280 | ("title", v) :: rest -> fold v page rely visy rest
6281 | ("page", v) :: rest -> fold title v rely visy rest
6282 | ("rely", v) :: rest -> fold title page v visy rest
6283 | ("visy", v) :: rest -> fold title page rely v rest
6284 | _ :: rest -> fold title page rely visy rest
6285 | [] -> title, page, rely, visy
6287 fold "invalid" "0" "0" "0" attrs
6290 let doc_of attrs =
6291 let rec fold path page rely pan visy = function
6292 | ("path", v) :: rest -> fold v page rely pan visy rest
6293 | ("page", v) :: rest -> fold path v rely pan visy rest
6294 | ("rely", v) :: rest -> fold path page v pan visy rest
6295 | ("pan", v) :: rest -> fold path page rely v visy rest
6296 | ("visy", v) :: rest -> fold path page rely pan v rest
6297 | _ :: rest -> fold path page rely pan visy rest
6298 | [] -> path, page, rely, pan, visy
6300 fold "" "0" "0" "0" "0" attrs
6303 let map_of attrs =
6304 let rec fold rs ls = function
6305 | ("out", v) :: rest -> fold v ls rest
6306 | ("in", v) :: rest -> fold rs v rest
6307 | _ :: rest -> fold ls rs rest
6308 | [] -> ls, rs
6310 fold "" "" attrs
6313 let setconf dst src =
6314 dst.scrollbw <- src.scrollbw;
6315 dst.scrollh <- src.scrollh;
6316 dst.icase <- src.icase;
6317 dst.preload <- src.preload;
6318 dst.pagebias <- src.pagebias;
6319 dst.verbose <- src.verbose;
6320 dst.scrollstep <- src.scrollstep;
6321 dst.maxhfit <- src.maxhfit;
6322 dst.crophack <- src.crophack;
6323 dst.autoscrollstep <- src.autoscrollstep;
6324 dst.maxwait <- src.maxwait;
6325 dst.hlinks <- src.hlinks;
6326 dst.underinfo <- src.underinfo;
6327 dst.interpagespace <- src.interpagespace;
6328 dst.zoom <- src.zoom;
6329 dst.presentation <- src.presentation;
6330 dst.angle <- src.angle;
6331 dst.cwinw <- src.cwinw;
6332 dst.cwinh <- src.cwinh;
6333 dst.savebmarks <- src.savebmarks;
6334 dst.memlimit <- src.memlimit;
6335 dst.proportional <- src.proportional;
6336 dst.texcount <- src.texcount;
6337 dst.sliceheight <- src.sliceheight;
6338 dst.thumbw <- src.thumbw;
6339 dst.jumpback <- src.jumpback;
6340 dst.bgcolor <- src.bgcolor;
6341 dst.scrollbarinpm <- src.scrollbarinpm;
6342 dst.tilew <- src.tilew;
6343 dst.tileh <- src.tileh;
6344 dst.mustoresize <- src.mustoresize;
6345 dst.checkers <- src.checkers;
6346 dst.aalevel <- src.aalevel;
6347 dst.trimmargins <- src.trimmargins;
6348 dst.trimfuzz <- src.trimfuzz;
6349 dst.urilauncher <- src.urilauncher;
6350 dst.colorspace <- src.colorspace;
6351 dst.invert <- src.invert;
6352 dst.colorscale <- src.colorscale;
6353 dst.redirectstderr <- src.redirectstderr;
6354 dst.ghyllscroll <- src.ghyllscroll;
6355 dst.columns <- src.columns;
6356 dst.beyecolumns <- src.beyecolumns;
6357 dst.selcmd <- src.selcmd;
6358 dst.updatecurs <- src.updatecurs;
6359 dst.pathlauncher <- src.pathlauncher;
6360 dst.keyhashes <- copykeyhashes src;
6361 dst.hfsize <- src.hfsize;
6362 dst.hscrollstep <- src.hscrollstep;
6363 dst.pgscale <- src.pgscale;
6364 dst.usepbo <- src.usepbo;
6365 dst.wheelbypage <- src.wheelbypage;
6366 dst.stcmd <- src.stcmd;
6369 let get s =
6370 let h = Hashtbl.create 10 in
6371 let dc = { defconf with angle = defconf.angle } in
6372 let rec toplevel v t spos _ =
6373 match t with
6374 | Vdata | Vcdata | Vend -> v
6375 | Vopen ("llppconfig", _, closed) ->
6376 if closed
6377 then v
6378 else { v with f = llppconfig }
6379 | Vopen _ ->
6380 error "unexpected subelement at top level" s spos
6381 | Vclose _ -> error "unexpected close at top level" s spos
6383 and llppconfig v t spos _ =
6384 match t with
6385 | Vdata | Vcdata -> v
6386 | Vend -> error "unexpected end of input in llppconfig" s spos
6387 | Vopen ("defaults", attrs, closed) ->
6388 let c = config_of dc attrs in
6389 setconf dc c;
6390 if closed
6391 then v
6392 else { v with f = defaults }
6394 | Vopen ("ui-font", attrs, closed) ->
6395 let rec getsize size = function
6396 | [] -> size
6397 | ("size", v) :: rest ->
6398 let size =
6399 fromstring int_of_string spos "size" v fstate.fontsize in
6400 getsize size rest
6401 | l -> getsize size l
6403 fstate.fontsize <- getsize fstate.fontsize attrs;
6404 if closed
6405 then v
6406 else { v with f = uifont (Buffer.create 10) }
6408 | Vopen ("doc", attrs, closed) ->
6409 let pathent, spage, srely, span, svisy = doc_of attrs in
6410 let path = unent pathent
6411 and pageno = fromstring int_of_string spos "page" spage 0
6412 and rely = fromstring float_of_string spos "rely" srely 0.0
6413 and pan = fromstring int_of_string spos "pan" span 0
6414 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6415 let c = config_of dc attrs in
6416 let anchor = (pageno, rely, visy) in
6417 if closed
6418 then (Hashtbl.add h path (c, [], pan, anchor); v)
6419 else { v with f = doc path pan anchor c [] }
6421 | Vopen _ ->
6422 error "unexpected subelement in llppconfig" s spos
6424 | Vclose "llppconfig" -> { v with f = toplevel }
6425 | Vclose _ -> error "unexpected close in llppconfig" s spos
6427 and defaults v t spos _ =
6428 match t with
6429 | Vdata | Vcdata -> v
6430 | Vend -> error "unexpected end of input in defaults" s spos
6431 | Vopen ("keymap", attrs, closed) ->
6432 let modename =
6433 try List.assoc "mode" attrs
6434 with Not_found -> "global" in
6435 if closed
6436 then v
6437 else
6438 let ret keymap =
6439 let h = findkeyhash dc modename in
6440 KeyMap.iter (Hashtbl.replace h) keymap;
6441 defaults
6443 { v with f = pkeymap ret KeyMap.empty }
6445 | Vopen (_, _, _) ->
6446 error "unexpected subelement in defaults" s spos
6448 | Vclose "defaults" ->
6449 { v with f = llppconfig }
6451 | Vclose _ -> error "unexpected close in defaults" s spos
6453 and uifont b v t spos epos =
6454 match t with
6455 | Vdata | Vcdata ->
6456 Buffer.add_substring b s spos (epos - spos);
6458 | Vopen (_, _, _) ->
6459 error "unexpected subelement in ui-font" s spos
6460 | Vclose "ui-font" ->
6461 if String.length !fontpath = 0
6462 then fontpath := Buffer.contents b;
6463 { v with f = llppconfig }
6464 | Vclose _ -> error "unexpected close in ui-font" s spos
6465 | Vend -> error "unexpected end of input in ui-font" s spos
6467 and doc path pan anchor c bookmarks v t spos _ =
6468 match t with
6469 | Vdata | Vcdata -> v
6470 | Vend -> error "unexpected end of input in doc" s spos
6471 | Vopen ("bookmarks", _, closed) ->
6472 if closed
6473 then v
6474 else { v with f = pbookmarks path pan anchor c bookmarks }
6476 | Vopen ("keymap", attrs, closed) ->
6477 let modename =
6478 try List.assoc "mode" attrs
6479 with Not_found -> "global"
6481 if closed
6482 then v
6483 else
6484 let ret keymap =
6485 let h = findkeyhash c modename in
6486 KeyMap.iter (Hashtbl.replace h) keymap;
6487 doc path pan anchor c bookmarks
6489 { v with f = pkeymap ret KeyMap.empty }
6491 | Vopen (_, _, _) ->
6492 error "unexpected subelement in doc" s spos
6494 | Vclose "doc" ->
6495 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6496 { v with f = llppconfig }
6498 | Vclose _ -> error "unexpected close in doc" s spos
6500 and pkeymap ret keymap v t spos _ =
6501 match t with
6502 | Vdata | Vcdata -> v
6503 | Vend -> error "unexpected end of input in keymap" s spos
6504 | Vopen ("map", attrs, closed) ->
6505 let r, l = map_of attrs in
6506 let kss = fromstring keys_of_string spos "in" r [] in
6507 let lss = fromstring keys_of_string spos "out" l [] in
6508 let keymap =
6509 match kss with
6510 | [] -> keymap
6511 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6512 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6514 if closed
6515 then { v with f = pkeymap ret keymap }
6516 else
6517 let f () = v in
6518 { v with f = skip "map" f }
6520 | Vopen _ ->
6521 error "unexpected subelement in keymap" s spos
6523 | Vclose "keymap" ->
6524 { v with f = ret keymap }
6526 | Vclose _ -> error "unexpected close in keymap" s spos
6528 and pbookmarks path pan anchor c bookmarks v t spos _ =
6529 match t with
6530 | Vdata | Vcdata -> v
6531 | Vend -> error "unexpected end of input in bookmarks" s spos
6532 | Vopen ("item", attrs, closed) ->
6533 let titleent, spage, srely, svisy = bookmark_of attrs in
6534 let page = fromstring int_of_string spos "page" spage 0
6535 and rely = fromstring float_of_string spos "rely" srely 0.0
6536 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6537 let bookmarks =
6538 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6540 if closed
6541 then { v with f = pbookmarks path pan anchor c bookmarks }
6542 else
6543 let f () = v in
6544 { v with f = skip "item" f }
6546 | Vopen _ ->
6547 error "unexpected subelement in bookmarks" s spos
6549 | Vclose "bookmarks" ->
6550 { v with f = doc path pan anchor c bookmarks }
6552 | Vclose _ -> error "unexpected close in bookmarks" s spos
6554 and skip tag f v t spos _ =
6555 match t with
6556 | Vdata | Vcdata -> v
6557 | Vend ->
6558 error ("unexpected end of input in skipped " ^ tag) s spos
6559 | Vopen (tag', _, closed) ->
6560 if closed
6561 then v
6562 else
6563 let f' () = { v with f = skip tag f } in
6564 { v with f = skip tag' f' }
6565 | Vclose ctag ->
6566 if tag = ctag
6567 then f ()
6568 else error ("unexpected close in skipped " ^ tag) s spos
6571 parse { f = toplevel; accu = () } s;
6572 h, dc;
6575 let do_load f ic =
6577 let len = in_channel_length ic in
6578 let s = String.create len in
6579 really_input ic s 0 len;
6580 f s;
6581 with
6582 | Parse_error (msg, s, pos) ->
6583 let subs = subs s pos in
6584 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6585 failwith ("parse error: " ^ s)
6587 | exn ->
6588 failwith ("config load error: " ^ Printexc.to_string exn)
6591 let defconfpath =
6592 let dir =
6594 let dir = Filename.concat home ".config" in
6595 if Sys.is_directory dir then dir else home
6596 with _ -> home
6598 Filename.concat dir "llpp.conf"
6601 let confpath = ref defconfpath;;
6603 let load1 f =
6604 if Sys.file_exists !confpath
6605 then
6606 match
6607 (try Some (open_in_bin !confpath)
6608 with exn ->
6609 prerr_endline
6610 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6611 Printexc.to_string exn);
6612 None
6614 with
6615 | Some ic ->
6616 let success =
6618 f (do_load get ic)
6619 with exn ->
6620 prerr_endline
6621 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6622 Printexc.to_string exn);
6623 false
6625 close_in ic;
6626 success
6628 | None -> false
6629 else
6630 f (Hashtbl.create 0, defconf)
6633 let load () =
6634 let f (h, dc) =
6635 let pc, pb, px, pa =
6637 Hashtbl.find h (Filename.basename state.path)
6638 with Not_found -> dc, [], 0, emptyanchor
6640 setconf defconf dc;
6641 setconf conf pc;
6642 state.bookmarks <- pb;
6643 state.x <- px;
6644 state.scrollw <- conf.scrollbw;
6645 if conf.jumpback
6646 then state.anchor <- pa;
6647 cbput state.hists.nav pa;
6648 true
6650 load1 f
6653 let add_attrs bb always dc c =
6654 let ob s a b =
6655 if always || a != b
6656 then Printf.bprintf bb "\n %s='%b'" s a
6657 and oi s a b =
6658 if always || a != b
6659 then Printf.bprintf bb "\n %s='%d'" s a
6660 and oI s a b =
6661 if always || a != b
6662 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6663 and oz s a b =
6664 if always || a <> b
6665 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6666 and oF s a b =
6667 if always || a <> b
6668 then Printf.bprintf bb "\n %s='%f'" s a
6669 and oc s a b =
6670 if always || a <> b
6671 then
6672 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6673 and oC s a b =
6674 if always || a <> b
6675 then
6676 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6677 and oR s a b =
6678 if always || a <> b
6679 then
6680 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6681 and os s a b =
6682 if always || a <> b
6683 then
6684 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6685 and og s a b =
6686 if always || a <> b
6687 then
6688 match a with
6689 | None -> ()
6690 | Some (_N, _A, _B) ->
6691 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6692 and oW s a b =
6693 if always || a <> b
6694 then
6695 let v =
6696 match a with
6697 | None -> "false"
6698 | Some f ->
6699 if f = infinity
6700 then "true"
6701 else string_of_float f
6703 Printf.bprintf bb "\n %s='%s'" s v
6704 and oco s a b =
6705 if always || a <> b
6706 then
6707 match a with
6708 | Cmulti ((n, a, b), _) when n > 1 ->
6709 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6710 | Csplit (n, _) when n > 1 ->
6711 Printf.bprintf bb "\n %s='%d'" s ~-n
6712 | _ -> ()
6713 and obeco s a b =
6714 if always || a <> b
6715 then
6716 match a with
6717 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6718 | _ -> ()
6720 oi "width" c.cwinw dc.cwinw;
6721 oi "height" c.cwinh dc.cwinh;
6722 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6723 oi "scroll-handle-height" c.scrollh dc.scrollh;
6724 ob "case-insensitive-search" c.icase dc.icase;
6725 ob "preload" c.preload dc.preload;
6726 oi "page-bias" c.pagebias dc.pagebias;
6727 oi "scroll-step" c.scrollstep dc.scrollstep;
6728 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6729 ob "max-height-fit" c.maxhfit dc.maxhfit;
6730 ob "crop-hack" c.crophack dc.crophack;
6731 oW "throttle" c.maxwait dc.maxwait;
6732 ob "highlight-links" c.hlinks dc.hlinks;
6733 ob "under-cursor-info" c.underinfo dc.underinfo;
6734 oi "vertical-margin" c.interpagespace dc.interpagespace;
6735 oz "zoom" c.zoom dc.zoom;
6736 ob "presentation" c.presentation dc.presentation;
6737 oi "rotation-angle" c.angle dc.angle;
6738 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6739 ob "proportional-display" c.proportional dc.proportional;
6740 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6741 oi "tex-count" c.texcount dc.texcount;
6742 oi "slice-height" c.sliceheight dc.sliceheight;
6743 oi "thumbnail-width" c.thumbw dc.thumbw;
6744 ob "persistent-location" c.jumpback dc.jumpback;
6745 oc "background-color" c.bgcolor dc.bgcolor;
6746 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6747 oi "tile-width" c.tilew dc.tilew;
6748 oi "tile-height" c.tileh dc.tileh;
6749 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6750 ob "checkers" c.checkers dc.checkers;
6751 oi "aalevel" c.aalevel dc.aalevel;
6752 ob "trim-margins" c.trimmargins dc.trimmargins;
6753 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6754 os "uri-launcher" c.urilauncher dc.urilauncher;
6755 os "path-launcher" c.pathlauncher dc.pathlauncher;
6756 oC "color-space" c.colorspace dc.colorspace;
6757 ob "invert-colors" c.invert dc.invert;
6758 oF "brightness" c.colorscale dc.colorscale;
6759 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6760 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6761 oco "columns" c.columns dc.columns;
6762 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6763 os "selection-command" c.selcmd dc.selcmd;
6764 os "synctex-command" c.stcmd dc.stcmd;
6765 ob "update-cursor" c.updatecurs dc.updatecurs;
6766 oi "hint-font-size" c.hfsize dc.hfsize;
6767 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6768 oF "page-scroll-scale" c.pgscale dc.pgscale;
6769 ob "use-pbo" c.usepbo dc.usepbo;
6770 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6773 let keymapsbuf always dc c =
6774 let bb = Buffer.create 16 in
6775 let rec loop = function
6776 | [] -> ()
6777 | (modename, h) :: rest ->
6778 let dh = findkeyhash dc modename in
6779 if always || h <> dh
6780 then (
6781 if Hashtbl.length h > 0
6782 then (
6783 if Buffer.length bb > 0
6784 then Buffer.add_char bb '\n';
6785 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6786 Hashtbl.iter (fun i o ->
6787 let isdifferent = always ||
6789 let dO = Hashtbl.find dh i in
6790 dO <> o
6791 with Not_found -> true
6793 if isdifferent
6794 then
6795 let addkm (k, m) =
6796 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6797 if Wsi.withalt m then Buffer.add_string bb "alt-";
6798 if Wsi.withshift m then Buffer.add_string bb "shift-";
6799 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6800 Buffer.add_string bb (Wsi.keyname k);
6802 let addkms l =
6803 let rec loop = function
6804 | [] -> ()
6805 | km :: [] -> addkm km
6806 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6808 loop l
6810 Buffer.add_string bb "<map in='";
6811 addkm i;
6812 match o with
6813 | KMinsrt km ->
6814 Buffer.add_string bb "' out='";
6815 addkm km;
6816 Buffer.add_string bb "'/>\n"
6818 | KMinsrl kms ->
6819 Buffer.add_string bb "' out='";
6820 addkms kms;
6821 Buffer.add_string bb "'/>\n"
6823 | KMmulti (ins, kms) ->
6824 Buffer.add_char bb ' ';
6825 addkms ins;
6826 Buffer.add_string bb "' out='";
6827 addkms kms;
6828 Buffer.add_string bb "'/>\n"
6829 ) h;
6830 Buffer.add_string bb "</keymap>";
6833 loop rest
6835 loop c.keyhashes;
6839 let save () =
6840 let uifontsize = fstate.fontsize in
6841 let bb = Buffer.create 32768 in
6842 let w, h =
6843 List.fold_left
6844 (fun (w, h) ws ->
6845 match ws with
6846 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6847 | Wsi.MaxVert -> (w, conf.cwinh)
6848 | Wsi.MaxHorz -> (conf.cwinw, h)
6850 (state.winw, state.winh) state.winstate
6852 conf.cwinw <- w;
6853 conf.cwinh <- h;
6854 let f (h, dc) =
6855 let dc = if conf.bedefault then conf else dc in
6856 Buffer.add_string bb "<llppconfig>\n";
6858 if String.length !fontpath > 0
6859 then
6860 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6861 uifontsize
6862 !fontpath
6863 else (
6864 if uifontsize <> 14
6865 then
6866 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6869 Buffer.add_string bb "<defaults ";
6870 add_attrs bb true dc dc;
6871 let kb = keymapsbuf true dc dc in
6872 if Buffer.length kb > 0
6873 then (
6874 Buffer.add_string bb ">\n";
6875 Buffer.add_buffer bb kb;
6876 Buffer.add_string bb "\n</defaults>\n";
6878 else Buffer.add_string bb "/>\n";
6880 let adddoc path pan anchor c bookmarks =
6881 if bookmarks == [] && c = dc && anchor = emptyanchor
6882 then ()
6883 else (
6884 Printf.bprintf bb "<doc path='%s'"
6885 (enent path 0 (String.length path));
6887 if anchor <> emptyanchor
6888 then (
6889 let n, rely, visy = anchor in
6890 Printf.bprintf bb " page='%d'" n;
6891 if rely > 1e-6
6892 then
6893 Printf.bprintf bb " rely='%f'" rely
6895 if abs_float visy > 1e-6
6896 then
6897 Printf.bprintf bb " visy='%f'" visy
6901 if pan != 0
6902 then Printf.bprintf bb " pan='%d'" pan;
6904 add_attrs bb false dc c;
6905 let kb = keymapsbuf false dc c in
6907 begin match bookmarks with
6908 | [] ->
6909 if Buffer.length kb > 0
6910 then (
6911 Buffer.add_string bb ">\n";
6912 Buffer.add_buffer bb kb;
6913 Buffer.add_string bb "\n</doc>\n";
6915 else Buffer.add_string bb "/>\n"
6916 | _ ->
6917 Buffer.add_string bb ">\n<bookmarks>\n";
6918 List.iter (fun (title, _level, (page, rely, visy)) ->
6919 Printf.bprintf bb
6920 "<item title='%s' page='%d'"
6921 (enent title 0 (String.length title))
6922 page
6924 if rely > 1e-6
6925 then
6926 Printf.bprintf bb " rely='%f'" rely
6928 if abs_float visy > 1e-6
6929 then
6930 Printf.bprintf bb " visy='%f'" visy
6932 Buffer.add_string bb "/>\n";
6933 ) bookmarks;
6934 Buffer.add_string bb "</bookmarks>";
6935 if Buffer.length kb > 0
6936 then (
6937 Buffer.add_string bb "\n";
6938 Buffer.add_buffer bb kb;
6940 Buffer.add_string bb "\n</doc>\n";
6941 end;
6945 let pan, conf =
6946 match state.mode with
6947 | Birdseye (c, pan, _, _, _) ->
6948 let beyecolumns =
6949 match conf.columns with
6950 | Cmulti ((c, _, _), _) -> Some c
6951 | Csingle _ -> None
6952 | Csplit _ -> None
6953 and columns =
6954 match c.columns with
6955 | Cmulti (c, _) -> Cmulti (c, [||])
6956 | Csingle _ -> Csingle [||]
6957 | Csplit _ -> failwith "quit from bird's eye while split"
6959 pan, { c with beyecolumns = beyecolumns; columns = columns }
6960 | _ -> state.x, conf
6962 let basename = Filename.basename state.path in
6963 adddoc basename pan (getanchor ())
6964 (let conf =
6965 let autoscrollstep =
6966 match state.autoscroll with
6967 | Some step -> step
6968 | None -> conf.autoscrollstep
6970 match state.mode with
6971 | Birdseye (bc, _, _, _, _) ->
6972 { conf with
6973 zoom = bc.zoom;
6974 presentation = bc.presentation;
6975 interpagespace = bc.interpagespace;
6976 maxwait = bc.maxwait;
6977 autoscrollstep = autoscrollstep }
6978 | _ -> { conf with autoscrollstep = autoscrollstep }
6979 in conf)
6980 (if conf.savebmarks then state.bookmarks else []);
6982 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6983 if basename <> path
6984 then adddoc path x anchor c bookmarks
6985 ) h;
6986 Buffer.add_string bb "</llppconfig>\n";
6987 true;
6989 if load1 f && Buffer.length bb > 0
6990 then
6992 let tmp = !confpath ^ ".tmp" in
6993 let oc = open_out_bin tmp in
6994 Buffer.output_buffer oc bb;
6995 close_out oc;
6996 Unix.rename tmp !confpath;
6997 with exn ->
6998 prerr_endline
6999 ("error while saving configuration: " ^ Printexc.to_string exn)
7001 end;;
7003 let adderrmsg src msg =
7004 Buffer.add_string state.errmsgs msg;
7005 state.newerrmsgs <- true;
7006 G.postRedisplay src
7009 let adderrfmt src fmt =
7010 Format.kprintf (fun s -> adderrmsg src s) fmt;
7013 let ract cmds =
7014 let cl = splitatspace cmds in
7015 let scan s fmt f =
7016 try Scanf.sscanf s fmt f
7017 with exn ->
7018 adderrfmt "remote exec"
7019 "error processing '%S': %s\n" cmds (Printexc.to_string exn)
7021 match cl with
7022 | "reload" :: [] -> reload ()
7023 | "goto" :: args :: [] ->
7024 let cmd, _ = state.geomcmds in
7025 scan args "%u %f %f"
7026 (fun pageno x y ->
7027 if String.length cmd = 0
7028 then gotopagexy pageno x y
7029 else
7030 let prevf = state.reprf in
7031 let f () =
7032 gotopagexy pageno x y;
7033 prevf ()
7035 state.reprf <- f
7037 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7038 | "rect" :: args :: [] ->
7039 scan args "%u %u %f %f %f %f"
7040 (fun pageno color x0 y0 x1 y1 ->
7041 onpagerect pageno (fun w h ->
7042 let _,w1,h1,_ = getpagedim pageno in
7043 let sw = float w1 /. w
7044 and sh = float h1 /. h in
7045 let x0s = x0 *. sw
7046 and x1s = x1 *. sw
7047 and y0s = y0 *. sh
7048 and y1s = y1 *. sh in
7049 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7050 debugrect rect;
7051 state.rects <- (pageno, color, rect) :: state.rects;
7052 G.postRedisplay "rect";
7055 | "activatewin" :: [] -> Wsi.activatewin ()
7056 | "quit" :: [] -> raise Quit
7057 | _ ->
7058 adderrfmt "remote command"
7059 "error processing remote command: %S\n" cmds;
7062 let remote =
7063 let scratch = String.create 80 in
7064 let buf = Buffer.create 80 in
7065 fun fd ->
7066 let rec tempfr () =
7067 try Some (Unix.read fd scratch 0 80)
7068 with
7069 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7070 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7071 | exn -> raise exn
7073 match tempfr () with
7074 | None -> Some fd
7075 | Some n ->
7076 if n = 0
7077 then (
7078 Unix.close fd;
7079 if Buffer.length buf > 0
7080 then (
7081 let s = Buffer.contents buf in
7082 Buffer.clear buf;
7083 ract s;
7085 None
7087 else
7088 let rec eat ppos =
7089 let nlpos =
7091 let pos = String.index_from scratch ppos '\n' in
7092 if pos >= n then -1 else pos
7093 with Not_found -> -1
7095 if nlpos >= 0
7096 then (
7097 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7098 let s = Buffer.contents buf in
7099 Buffer.clear buf;
7100 ract s;
7101 eat (nlpos+1);
7103 else (
7104 Buffer.add_substring buf scratch ppos (n-ppos);
7105 Some fd
7107 in eat 0
7110 let remoteopen path =
7111 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7112 with exn ->
7113 adderrfmt "remoteopen" "error opening %S: %s" path (Printexc.to_string exn);
7114 None
7117 let () =
7118 let trimcachepath = ref "" in
7119 let rcmdpath = ref "" in
7120 Arg.parse
7121 (Arg.align
7122 [("-p", Arg.String (fun s -> state.password <- s) ,
7123 "<password> Set password");
7125 ("-f", Arg.String (fun s -> Config.fontpath := s),
7126 "<path> Set path to the user interface font");
7128 ("-c", Arg.String (fun s -> Config.confpath := s),
7129 "<path> Set path to the configuration file");
7131 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7132 "<path> Set path to the trim cache file");
7134 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7135 "<named-destination> Set named destination");
7137 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7139 ("-remote", Arg.String (fun s -> rcmdpath := s),
7140 "<path> Set path to the remote commands source");
7142 ("-v", Arg.Unit (fun () ->
7143 Printf.printf
7144 "%s\nconfiguration path: %s\n"
7145 (version ())
7146 Config.defconfpath
7148 exit 0), " Print version and exit");
7151 (fun s -> state.path <- s)
7152 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7154 if String.length state.path = 0
7155 then (prerr_endline "file name missing"; exit 1);
7157 if not (Config.load ())
7158 then prerr_endline "failed to load configuration";
7160 let globalkeyhash = findkeyhash conf "global" in
7161 let wsfd, winw, winh = Wsi.init (object
7162 method expose =
7163 state.wthack <- false;
7164 if nogeomcmds state.geomcmds || platform == Posx
7165 then display ()
7166 else (
7167 GlClear.color (scalecolor2 conf.bgcolor);
7168 GlClear.clear [`color];
7170 method display = display ()
7171 method reshape w h = reshape w h
7172 method mouse b d x y m = mouse b d x y m
7173 method motion x y = state.mpos <- (x, y); motion x y
7174 method pmotion x y = state.mpos <- (x, y); pmotion x y
7175 method key k m =
7176 let mascm = m land (
7177 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7178 ) in
7179 match state.keystate with
7180 | KSnone ->
7181 let km = k, mascm in
7182 begin
7183 match
7184 let modehash = state.uioh#modehash in
7185 try Hashtbl.find modehash km
7186 with Not_found ->
7187 try Hashtbl.find globalkeyhash km
7188 with Not_found -> KMinsrt (k, m)
7189 with
7190 | KMinsrt (k, m) -> keyboard k m
7191 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7192 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7194 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7195 List.iter (fun (k, m) -> keyboard k m) insrt;
7196 state.keystate <- KSnone
7197 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7198 state.keystate <- KSinto (keys, insrt)
7199 | _ ->
7200 state.keystate <- KSnone
7202 method enter x y = state.mpos <- (x, y); pmotion x y
7203 method leave = state.mpos <- (-1, -1)
7204 method winstate wsl = state.winstate <- wsl
7205 method quit = raise Quit
7206 end) conf.cwinw conf.cwinh (platform = Posx) in
7208 state.wsfd <- wsfd;
7210 if not (
7211 List.exists GlMisc.check_extension
7212 [ "GL_ARB_texture_rectangle"
7213 ; "GL_EXT_texture_recangle"
7214 ; "GL_NV_texture_rectangle" ]
7216 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7218 let cr, sw =
7219 match Ne.pipe () with
7220 | Ne.Exn exn ->
7221 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
7222 exit 1
7223 | Ne.Res rw -> rw
7224 and sr, cw =
7225 match Ne.pipe () with
7226 | Ne.Exn exn ->
7227 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
7228 exit 1
7229 | Ne.Res rw -> rw
7232 cloexec cr;
7233 cloexec sw;
7234 cloexec sr;
7235 cloexec cw;
7237 setcheckers conf.checkers;
7238 redirectstderr ();
7240 init (cr, cw) (
7241 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7242 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7243 !Config.fontpath, !trimcachepath,
7244 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7246 state.sr <- sr;
7247 state.sw <- sw;
7248 state.text <- "Opening " ^ (mbtoutf8 state.path);
7249 reshape winw winh;
7250 opendoc state.path state.password;
7251 state.uioh <- uioh;
7253 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7254 let optrfd =
7255 ref (
7256 if String.length !rcmdpath > 0
7257 then remoteopen !rcmdpath
7258 else None
7262 let rec loop deadline =
7263 let r =
7264 match state.errfd with
7265 | None -> [state.sr; state.wsfd]
7266 | Some fd -> [state.sr; state.wsfd; fd]
7268 let r =
7269 match !optrfd with
7270 | None -> r
7271 | Some fd -> fd :: r
7273 if state.redisplay && not state.wthack
7274 then (
7275 state.redisplay <- false;
7276 display ();
7278 let timeout =
7279 let now = now () in
7280 if deadline > now
7281 then (
7282 if deadline = infinity
7283 then ~-.1.0
7284 else max 0.0 (deadline -. now)
7286 else 0.0
7288 let r, _, _ =
7289 try Unix.select r [] [] timeout
7290 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7292 begin match r with
7293 | [] ->
7294 state.ghyll None;
7295 let newdeadline =
7296 if state.ghyll == noghyll
7297 then
7298 match state.autoscroll with
7299 | Some step when step != 0 ->
7300 let y = state.y + step in
7301 let y =
7302 if y < 0
7303 then state.maxy
7304 else if y >= state.maxy then 0 else y
7306 gotoy y;
7307 if state.mode = View
7308 then state.text <- "";
7309 deadline +. 0.01
7310 | _ -> infinity
7311 else deadline +. 0.01
7313 loop newdeadline
7315 | l ->
7316 let rec checkfds = function
7317 | [] -> ()
7318 | fd :: rest when fd = state.sr ->
7319 let cmd = readcmd state.sr in
7320 act cmd;
7321 checkfds rest
7323 | fd :: rest when fd = state.wsfd ->
7324 Wsi.readresp fd;
7325 checkfds rest
7327 | fd :: rest when Some fd = !optrfd ->
7328 begin match remote fd with
7329 | None -> optrfd := remoteopen !rcmdpath;
7330 | opt -> optrfd := opt
7331 end;
7332 checkfds rest
7334 | fd :: rest ->
7335 let s = String.create 80 in
7336 let n = tempfailureretry (Unix.read fd s 0) 80 in
7337 if conf.redirectstderr
7338 then (
7339 Buffer.add_substring state.errmsgs s 0 n;
7340 state.newerrmsgs <- true;
7341 state.redisplay <- true;
7343 else (
7344 prerr_string (String.sub s 0 n);
7345 flush stderr;
7347 checkfds rest
7349 checkfds l;
7350 let newdeadline =
7351 let deadline1 =
7352 if deadline = infinity
7353 then now () +. 0.01
7354 else deadline
7356 match state.autoscroll with
7357 | Some step when step != 0 -> deadline1
7358 | _ -> if state.ghyll == noghyll then infinity else deadline1
7360 loop newdeadline
7361 end;
7364 loop infinity;
7365 with Quit ->
7366 Config.save ();