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