Bound auto scroll step
[llpp.git] / main.ml
blob0b3461ff7837449b59b7580cea0007b2d595284e
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 launchcommand
12 | Unamed of destname
13 | Uremote of (filename * pageno)
14 | Uremotedest of (filename * destname)
15 and facename = string
16 and launchcommand = string
17 and filename = string
18 and pageno = int
19 and destname = string;;
21 type mark =
22 | Mark_page
23 | Mark_block
24 | Mark_line
25 | Mark_word
28 type params = (angle * fitmodel * trimparams
29 * texcount * sliceheight * memsize
30 * colorspace * fontpath * trimcachepath
31 * haspbo)
32 and width = int
33 and height = int
34 and leftx = int
35 and opaque = string
36 and recttype = int
37 and pixmapsize = int
38 and angle = int
39 and trimmargins = bool
40 and interpagespace = int
41 and texcount = int
42 and sliceheight = int
43 and gen = int
44 and top = float
45 and dtop = float
46 and fontpath = string
47 and trimcachepath = string
48 and memsize = int
49 and aalevel = int
50 and irect = (int * int * int * int)
51 and trimparams = (trimmargins * irect)
52 and colorspace = | Rgb | Bgr | Gray
53 and fitmodel = | FitWidth | FitProportional | FitPage
54 and haspbo = bool
55 and uri = string
56 and caption = string
59 type x = int
60 and y = int
61 and tilex = int
62 and tiley = int
63 and tileparams = (x * y * width * height * tilex * tiley)
66 type link =
67 | Lnotfound
68 | Lfound of int
69 and linkdir =
70 | LDfirst
71 | LDlast
72 | LDfirstvisible of (int * int * int)
73 | LDleft of int
74 | LDright of int
75 | LDdown of int
76 | LDup of int
79 type pagewithlinks =
80 | Pwlnotfound
81 | Pwl of int
84 type keymap =
85 | KMinsrt of key
86 | KMinsrl of key list
87 | KMmulti of key list * key list
88 and key = int * int
89 and keyhash = (key, keymap) Hashtbl.t
90 and keystate =
91 | KSnone
92 | KSinto of (key list * key list)
95 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
96 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
98 type pipe = (Unix.file_descr * Unix.file_descr);;
100 external init : pipe -> params -> unit = "ml_init";;
101 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
102 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
103 external getpdimrect : int -> float array = "ml_getpdimrect";;
104 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
105 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
106 external clearmark : string -> unit = "ml_clearmark";;
107 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
108 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
109 external measurestr : int -> string -> float = "ml_measure_string";;
110 external postprocess :
111 opaque -> int -> int -> int -> (int * string * int) -> int
112 = "ml_postprocess";;
113 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
114 external platform : unit -> platform = "ml_platform";;
115 external setaalevel : int -> unit = "ml_setaalevel";;
116 external realloctexts : int -> bool = "ml_realloctexts";;
117 external findlink : opaque -> linkdir -> link = "ml_findlink";;
118 external getlink : opaque -> int -> under = "ml_getlink";;
119 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
120 external getlinkcount : opaque -> int = "ml_getlinkcount";;
121 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
122 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
123 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
124 external freepbo : string -> unit = "ml_freepbo";;
125 external unmappbo : string -> unit = "ml_unmappbo";;
126 external pbousable : unit -> bool = "ml_pbo_usable";;
127 external unproject : opaque -> int -> int -> (int * int) option
128 = "ml_unproject";;
129 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
130 external rectofblock : opaque -> int -> int -> float array option
131 = "ml_rectofblock";;
132 external fz_version : unit -> string = "ml_fz_version";;
133 external begintiles : unit -> unit = "ml_begintiles";;
134 external endtiles : unit -> unit = "ml_endtiles";;
136 let platform_to_string = function
137 | Punknown -> "unknown"
138 | Plinux -> "Linux"
139 | Posx -> "OSX"
140 | Psun -> "Sun"
141 | Pfreebsd -> "FreeBSD"
142 | Pdragonflybsd -> "DragonflyBSD"
143 | Popenbsd -> "OpenBSD"
144 | Pnetbsd -> "NetBSD"
145 | Pcygwin -> "Cygwin"
148 let platform = platform ();;
150 let now = Unix.gettimeofday;;
152 let selfexec = ref "";;
154 let popen cmd fda =
155 if platform = Pcygwin
156 then (
157 let sh = "/bin/sh" in
158 let args = [|sh; "-c"; cmd|] in
159 let rec std si so se = function
160 | [] -> si, so, se
161 | (fd, 0) :: rest -> std fd so se rest
162 | (fd, -1) :: rest ->
163 Unix.set_close_on_exec fd;
164 std si so se rest
165 | (_, n) :: _ ->
166 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
168 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
169 ignore (Unix.create_process sh args si so se)
171 else popen cmd fda;
174 type mpos = int * int
175 and mstate =
176 | Msel of (mpos * mpos)
177 | Mpan of mpos
178 | Mscrolly | Mscrollx
179 | Mzoom of (int * int)
180 | Mzoomrect of (mpos * mpos)
181 | Mnone
184 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
185 and onkey = string -> int -> te
186 and ondone = string -> unit
187 and histcancel = unit -> unit
188 and onhist = ((histcmd -> string) * histcancel)
189 and histcmd = HCnext | HCprev | HCfirst | HClast
190 and cancelonempty = bool
191 and te =
192 | TEstop
193 | TEdone of string
194 | TEcont of string
195 | TEswitch of textentry
198 type 'a circbuf =
199 { store : 'a array
200 ; mutable rc : int
201 ; mutable wc : int
202 ; mutable len : int
206 let bound v minv maxv =
207 max minv (min maxv v);
210 let cbnew n v =
211 { store = Array.create n v
212 ; rc = 0
213 ; wc = 0
214 ; len = 0
218 let cbcap b = Array.length b.store;;
220 let cbput b v =
221 let cap = cbcap b in
222 b.store.(b.wc) <- v;
223 b.wc <- (b.wc + 1) mod cap;
224 b.rc <- b.wc;
225 b.len <- min (b.len + 1) cap;
228 let cbempty b = b.len = 0;;
230 let cbgetg b circular dir =
231 if cbempty b
232 then b.store.(0)
233 else
234 let rc = b.rc + dir in
235 let rc =
236 if circular
237 then (
238 if rc = -1
239 then b.len-1
240 else (
241 if rc >= b.len
242 then 0
243 else rc
246 else bound rc 0 (b.len-1)
248 b.rc <- rc;
249 b.store.(rc);
252 let cbget b = cbgetg b false;;
253 let cbgetc b = cbgetg b true;;
255 let drawstring size x y s =
256 Gl.enable `blend;
257 Gl.enable `texture_2d;
258 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
259 ignore (drawstr size x y s);
260 Gl.disable `blend;
261 Gl.disable `texture_2d;
264 let drawstring1 size x y s =
265 drawstr size x y s;
268 let drawstring2 size x y fmt =
269 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
272 type page =
273 { pageno : int
274 ; pagedimno : int
275 ; pagew : int
276 ; pageh : int
277 ; pagex : int
278 ; pagey : int
279 ; pagevw : int
280 ; pagevh : int
281 ; pagedispx : int
282 ; pagedispy : int
283 ; pagecol : int
287 let debugl l =
288 dolog "l %d dim=%d {" l.pageno l.pagedimno;
289 dolog " WxH %dx%d" l.pagew l.pageh;
290 dolog " vWxH %dx%d" l.pagevw l.pagevh;
291 dolog " pagex,y %d,%d" l.pagex l.pagey;
292 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
293 dolog " column %d" l.pagecol;
294 dolog "}";
297 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
298 dolog "rect {";
299 dolog " x0,y0=(% f, % f)" x0 y0;
300 dolog " x1,y1=(% f, % f)" x1 y1;
301 dolog " x2,y2=(% f, % f)" x2 y2;
302 dolog " x3,y3=(% f, % f)" x3 y3;
303 dolog "}";
306 type multicolumns = multicol * pagegeom
307 and singlecolumn = pagegeom
308 and splitcolumns = columncount * pagegeom
309 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
310 and multicol = columncount * covercount * covercount
311 and pdimno = int
312 and columncount = int
313 and covercount = int;;
315 type scrollb = int;;
316 let scrollbvv = 1;;
317 let scrollbhv = 2;;
319 type conf =
320 { mutable scrollbw : int
321 ; mutable scrollh : int
322 ; mutable scrollb : scrollb
323 ; mutable icase : bool
324 ; mutable preload : bool
325 ; mutable pagebias : int
326 ; mutable verbose : bool
327 ; mutable debug : bool
328 ; mutable scrollstep : int
329 ; mutable hscrollstep : int
330 ; mutable maxhfit : bool
331 ; mutable crophack : bool
332 ; mutable autoscrollstep : int
333 ; mutable maxwait : float option
334 ; mutable hlinks : bool
335 ; mutable underinfo : bool
336 ; mutable interpagespace : interpagespace
337 ; mutable zoom : float
338 ; mutable presentation : bool
339 ; mutable angle : angle
340 ; mutable cwinw : int
341 ; mutable cwinh : int
342 ; mutable savebmarks : bool
343 ; mutable fitmodel : fitmodel
344 ; mutable trimmargins : trimmargins
345 ; mutable trimfuzz : irect
346 ; mutable memlimit : memsize
347 ; mutable texcount : texcount
348 ; mutable sliceheight : sliceheight
349 ; mutable thumbw : width
350 ; mutable jumpback : bool
351 ; mutable bgcolor : (float * float * float)
352 ; mutable bedefault : bool
353 ; mutable tilew : int
354 ; mutable tileh : int
355 ; mutable mustoresize : memsize
356 ; mutable checkers : bool
357 ; mutable aalevel : int
358 ; mutable urilauncher : string
359 ; mutable pathlauncher : string
360 ; mutable colorspace : colorspace
361 ; mutable invert : bool
362 ; mutable colorscale : float
363 ; mutable redirectstderr : bool
364 ; mutable ghyllscroll : (int * int * int) option
365 ; mutable columns : columns
366 ; mutable beyecolumns : columncount option
367 ; mutable selcmd : string
368 ; mutable paxcmd : string
369 ; mutable updatecurs : bool
370 ; mutable keyhashes : (string * keyhash) list
371 ; mutable hfsize : int
372 ; mutable pgscale : float
373 ; mutable usepbo : bool
374 ; mutable wheelbypage : bool
375 ; mutable stcmd : string
376 ; mutable riani : bool
377 ; mutable pax : (float * int * int) ref option
378 ; mutable paxmark : mark
380 and columns =
381 | Csingle of singlecolumn
382 | Cmulti of multicolumns
383 | Csplit of splitcolumns
386 type anchor = pageno * top * dtop;;
388 type outlinekind =
389 | Onone
390 | Oanchor of anchor
391 | Ouri of uri
392 | Olaunch of launchcommand
393 | Oremote of (filename * pageno)
394 | Oremotedest of (filename * destname)
395 and outline = (caption * outlinelevel * outlinekind)
396 and outlinelevel = int
399 type rect = float * float * float * float * float * float * float * float;;
401 type tile = opaque * pixmapsize * elapsed
402 and elapsed = float;;
403 type pagemapkey = pageno * gen;;
404 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
405 and row = int
406 and col = int;;
408 let emptyanchor = (0, 0.0, 0.0);;
410 type infochange = | Memused | Docinfo | Pdim;;
412 class type uioh = object
413 method display : unit
414 method key : int -> int -> uioh
415 method button : int -> bool -> int -> int -> int -> uioh
416 method motion : int -> int -> uioh
417 method pmotion : int -> int -> uioh
418 method infochanged : infochange -> unit
419 method scrollpw : (int * float * float)
420 method scrollph : (int * float * float)
421 method modehash : keyhash
422 method eformsgs : bool
423 end;;
425 type mode =
426 | Birdseye of (conf * leftx * pageno * pageno * anchor)
427 | Textentry of (textentry * onleave)
428 | View
429 | LinkNav of linktarget
430 and onleave = leavetextentrystatus -> unit
431 and leavetextentrystatus = | Cancel | Confirm
432 and helpitem = string * int * action
433 and action =
434 | Noaction
435 | Action of (uioh -> uioh)
436 and linktarget =
437 | Ltexact of (pageno * int)
438 | Ltgendir of int
441 let isbirdseye = function Birdseye _ -> true | _ -> false;;
442 let istextentry = function Textentry _ -> true | _ -> false;;
444 type currently =
445 | Idle
446 | Loading of (page * gen)
447 | Tiling of (
448 page * opaque * colorspace * angle * gen * col * row * width * height
450 | Outlining of outline list
453 let emptykeyhash = Hashtbl.create 0;;
454 let nouioh : uioh = object (self)
455 method display = ()
456 method key _ _ = self
457 method button _ _ _ _ _ = self
458 method motion _ _ = self
459 method pmotion _ _ = self
460 method infochanged _ = ()
461 method scrollpw = (0, nan, nan)
462 method scrollph = (0, nan, nan)
463 method modehash = emptykeyhash
464 method eformsgs = false
465 end;;
467 type state =
468 { mutable sr : Unix.file_descr
469 ; mutable sw : Unix.file_descr
470 ; mutable wsfd : Unix.file_descr
471 ; mutable errfd : Unix.file_descr option
472 ; mutable stderr : Unix.file_descr
473 ; mutable errmsgs : Buffer.t
474 ; mutable newerrmsgs : bool
475 ; mutable w : int
476 ; mutable x : int
477 ; mutable y : int
478 ; mutable anchor : anchor
479 ; mutable ranchors : (string * string * anchor * string) list
480 ; mutable maxy : int
481 ; mutable layout : page list
482 ; pagemap : (pagemapkey, opaque) Hashtbl.t
483 ; tilemap : (tilemapkey, tile) Hashtbl.t
484 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
485 ; mutable pdims : (pageno * width * height * leftx) list
486 ; mutable pagecount : int
487 ; mutable currently : currently
488 ; mutable mstate : mstate
489 ; mutable searchpattern : string
490 ; mutable rects : (pageno * recttype * rect) list
491 ; mutable rects1 : (pageno * recttype * rect) list
492 ; mutable text : string
493 ; mutable winstate : Wsi.winstate list
494 ; mutable mode : mode
495 ; mutable uioh : uioh
496 ; mutable outlines : outline array
497 ; mutable bookmarks : outline list
498 ; mutable path : string
499 ; mutable password : string
500 ; mutable nameddest : string
501 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
502 ; mutable memused : memsize
503 ; mutable gen : gen
504 ; mutable throttle : (page list * int * float) option
505 ; mutable autoscroll : int option
506 ; mutable ghyll : (int option -> unit)
507 ; mutable help : helpitem array
508 ; mutable docinfo : (int * string) list
509 ; mutable texid : GlTex.texture_id option
510 ; hists : hists
511 ; mutable prevzoom : (float * int)
512 ; mutable progress : float
513 ; mutable redisplay : bool
514 ; mutable mpos : mpos
515 ; mutable keystate : keystate
516 ; mutable glinks : bool
517 ; mutable prevcolumns : (columns * float) option
518 ; mutable winw : int
519 ; mutable winh : int
520 ; mutable reprf : (unit -> unit)
521 ; mutable origin : string
522 ; mutable roam : (unit -> unit)
523 ; mutable bzoom : bool
524 ; mutable traw : [`float] Raw.t
525 ; mutable vraw : [`float] Raw.t
527 and hists =
528 { pat : string circbuf
529 ; pag : string circbuf
530 ; nav : anchor circbuf
531 ; sel : string circbuf
535 let defconf =
536 { scrollbw = 7
537 ; scrollh = 12
538 ; scrollb = scrollbhv lor scrollbvv
539 ; icase = true
540 ; preload = true
541 ; pagebias = 0
542 ; verbose = false
543 ; debug = false
544 ; scrollstep = 24
545 ; hscrollstep = 24
546 ; maxhfit = true
547 ; crophack = false
548 ; autoscrollstep = 2
549 ; maxwait = None
550 ; hlinks = false
551 ; underinfo = false
552 ; interpagespace = 2
553 ; zoom = 1.0
554 ; presentation = false
555 ; angle = 0
556 ; cwinw = 900
557 ; cwinh = 900
558 ; savebmarks = true
559 ; fitmodel = FitProportional
560 ; trimmargins = false
561 ; trimfuzz = (0,0,0,0)
562 ; memlimit = 32 lsl 20
563 ; texcount = 256
564 ; sliceheight = 24
565 ; thumbw = 76
566 ; jumpback = true
567 ; bgcolor = (0.5, 0.5, 0.5)
568 ; bedefault = false
569 ; tilew = 2048
570 ; tileh = 2048
571 ; mustoresize = 256 lsl 20
572 ; checkers = true
573 ; aalevel = 8
574 ; urilauncher =
575 (match platform with
576 | Plinux | Pfreebsd | Pdragonflybsd
577 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
578 | Posx -> "open \"%s\""
579 | Pcygwin -> "cygstart \"%s\""
580 | Punknown -> "echo %s")
581 ; pathlauncher = "lp \"%s\""
582 ; selcmd =
583 (match platform with
584 | Plinux | Pfreebsd | Pdragonflybsd
585 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
586 | Posx -> "pbcopy"
587 | Pcygwin -> "wsel"
588 | Punknown -> "cat")
589 ; paxcmd = "cat"
590 ; colorspace = Rgb
591 ; invert = false
592 ; colorscale = 1.0
593 ; redirectstderr = false
594 ; ghyllscroll = None
595 ; columns = Csingle [||]
596 ; beyecolumns = None
597 ; updatecurs = false
598 ; hfsize = 12
599 ; pgscale = 1.0
600 ; usepbo = false
601 ; wheelbypage = false
602 ; stcmd = "echo SyncTex"
603 ; riani = false
604 ; pax = None
605 ; paxmark = Mark_word
606 ; keyhashes =
607 let mk n = (n, Hashtbl.create 1) in
608 [ mk "global"
609 ; mk "info"
610 ; mk "help"
611 ; mk "outline"
612 ; mk "listview"
613 ; mk "birdseye"
614 ; mk "textentry"
615 ; mk "links"
616 ; mk "view"
621 let wtmode = ref false;;
622 let cxack = ref false;;
624 let findkeyhash c name =
625 try List.assoc name c.keyhashes
626 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
629 let conf = { defconf with angle = defconf.angle };;
631 let pgscale h = truncate (float h *. conf.pgscale);;
633 type fontstate =
634 { mutable fontsize : int
635 ; mutable wwidth : float
636 ; mutable maxrows : int
640 let fstate =
641 { fontsize = 14
642 ; wwidth = nan
643 ; maxrows = -1
647 let geturl s =
648 let colonpos = try String.index s ':' with Not_found -> -1 in
649 let len = String.length s in
650 if colonpos >= 0 && colonpos + 3 < len
651 then (
652 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
653 then
654 let schemestartpos =
655 try String.rindex_from s colonpos ' '
656 with Not_found -> -1
658 let scheme =
659 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
661 match scheme with
662 | "http" | "ftp" | "mailto" ->
663 let epos =
664 try String.index_from s colonpos ' '
665 with Not_found -> len
667 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
668 | _ -> ""
669 else ""
671 else ""
674 let gotouri uri =
675 if emptystr conf.urilauncher
676 then print_endline uri
677 else (
678 let url = geturl uri in
679 if emptystr url
680 then Printf.eprintf "obtained empty url from uri %S" uri
681 else
682 let re = Str.regexp "%s" in
683 let command = Str.global_replace re url conf.urilauncher in
684 try popen command []
685 with exn ->
686 Printf.eprintf
687 "failed to execute `%s': %s\n" command (exntos exn);
688 flush stderr;
692 let version () =
693 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
694 Help.version (fz_version ()) Sys.ocaml_version
695 (platform_to_string platform) Sys.word_size
698 let makehelp () =
699 let strings = version () :: "" :: Help.keys in
700 Array.of_list (
701 List.map (fun s ->
702 let url = geturl s in
703 if nonemptystr url
704 then (s, 0, Action (fun u -> gotouri url; u))
705 else (s, 0, Noaction)
706 ) strings);
709 let noghyll _ = ();;
710 let firstgeomcmds = "", [];;
711 let noreprf () = ();;
713 let state =
714 { sr = Unix.stdin
715 ; sw = Unix.stdin
716 ; wsfd = Unix.stdin
717 ; errfd = None
718 ; stderr = Unix.stderr
719 ; errmsgs = Buffer.create 0
720 ; newerrmsgs = false
721 ; x = 0
722 ; y = 0
723 ; w = 0
724 ; anchor = emptyanchor
725 ; ranchors = []
726 ; layout = []
727 ; maxy = max_int
728 ; tilelru = Queue.create ()
729 ; pagemap = Hashtbl.create 10
730 ; tilemap = Hashtbl.create 10
731 ; pdims = []
732 ; pagecount = 0
733 ; currently = Idle
734 ; mstate = Mnone
735 ; rects = []
736 ; rects1 = []
737 ; text = ""
738 ; mode = View
739 ; winstate = []
740 ; searchpattern = ""
741 ; outlines = [||]
742 ; bookmarks = []
743 ; path = ""
744 ; password = ""
745 ; nameddest = ""
746 ; geomcmds = firstgeomcmds
747 ; hists =
748 { nav = cbnew 10 emptyanchor
749 ; pat = cbnew 10 ""
750 ; pag = cbnew 10 ""
751 ; sel = cbnew 10 ""
753 ; memused = 0
754 ; gen = 0
755 ; throttle = None
756 ; autoscroll = None
757 ; ghyll = noghyll
758 ; help = makehelp ()
759 ; docinfo = []
760 ; texid = None
761 ; prevzoom = (1.0, 0)
762 ; progress = -1.0
763 ; uioh = nouioh
764 ; redisplay = true
765 ; mpos = (-1, -1)
766 ; keystate = KSnone
767 ; glinks = false
768 ; prevcolumns = None
769 ; winw = -1
770 ; winh = -1
771 ; reprf = noreprf
772 ; origin = ""
773 ; roam = (fun () -> ())
774 ; bzoom = false
775 ; traw = Raw.create_static `float 8
776 ; vraw = Raw.create_static `float 8
780 let hscrollh () =
781 if (conf.scrollb land scrollbhv = 0)
782 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
783 then 0
784 else conf.scrollbw
787 let vscrollw () =
788 if (conf.scrollb land scrollbvv = 0)
789 then 0
790 else conf.scrollbw
793 let wadjsb w = w - vscrollw ();;
795 let setfontsize n =
796 fstate.fontsize <- n;
797 fstate.wwidth <- measurestr fstate.fontsize "w";
798 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
801 let vlog fmt =
802 if conf.verbose
803 then
804 Printf.kprintf prerr_endline fmt
805 else
806 Printf.kprintf ignore fmt
809 let launchpath () =
810 if emptystr conf.pathlauncher
811 then print_endline state.path
812 else (
813 let re = Str.regexp "%s" in
814 let command = Str.global_replace re state.path conf.pathlauncher in
815 try popen command []
816 with exn ->
817 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
818 flush stderr;
822 module Ne = struct
823 type 'a t = | Res of 'a | Exn of exn;;
825 let pipe () =
826 try Res (Unix.pipe ())
827 with exn -> Exn exn
830 let clo fd f =
831 try tempfailureretry Unix.close fd
832 with exn -> f (exntos exn)
835 let dup fd =
836 try Res (tempfailureretry Unix.dup fd)
837 with exn -> Exn exn
840 let dup2 fd1 fd2 =
841 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
842 with exn -> Exn exn
844 end;;
846 let redirectstderr () =
847 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
848 if conf.redirectstderr
849 then
850 match Ne.pipe () with
851 | Ne.Exn exn ->
852 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
854 | Ne.Res (r, w) ->
855 begin match Ne.dup Unix.stderr with
856 | Ne.Exn exn ->
857 dolog "failed to dup stderr: %s" (exntos exn);
858 Ne.clo r (clofail "pipe/r");
859 Ne.clo w (clofail "pipe/w");
861 | Ne.Res dupstderr ->
862 begin match Ne.dup2 w Unix.stderr with
863 | Ne.Exn exn ->
864 dolog "failed to dup2 to stderr: %s" (exntos exn);
865 Ne.clo dupstderr (clofail "stderr duplicate");
866 Ne.clo r (clofail "redir pipe/r");
867 Ne.clo w (clofail "redir pipe/w");
869 | Ne.Res () ->
870 state.stderr <- dupstderr;
871 state.errfd <- Some r;
872 end;
874 else (
875 state.newerrmsgs <- false;
876 begin match state.errfd with
877 | Some fd ->
878 begin match Ne.dup2 state.stderr Unix.stderr with
879 | Ne.Exn exn ->
880 dolog "failed to dup2 original stderr: %s" (exntos exn)
881 | Ne.Res () ->
882 Ne.clo fd (clofail "dup of stderr");
883 state.errfd <- None;
884 end;
885 | None -> ()
886 end;
887 prerr_string (Buffer.contents state.errmsgs);
888 flush stderr;
889 Buffer.clear state.errmsgs;
893 module G =
894 struct
895 let postRedisplay who =
896 if conf.verbose
897 then prerr_endline ("redisplay for " ^ who);
898 state.redisplay <- true;
900 end;;
902 let getopaque pageno =
903 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
904 with Not_found -> None
907 let putopaque pageno opaque =
908 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
911 let pagetranslatepoint l x y =
912 let dy = y - l.pagedispy in
913 let y = dy + l.pagey in
914 let dx = x - l.pagedispx in
915 let x = dx + l.pagex in
916 (x, y);
919 let onppundermouse g x y d =
920 let rec f = function
921 | l :: rest ->
922 begin match getopaque l.pageno with
923 | Some opaque ->
924 let x0 = l.pagedispx in
925 let x1 = x0 + l.pagevw in
926 let y0 = l.pagedispy in
927 let y1 = y0 + l.pagevh in
928 if y >= y0 && y <= y1 && x >= x0 && x <= x1
929 then
930 let px, py = pagetranslatepoint l x y in
931 match g opaque l px py with
932 | Some res -> res
933 | None -> f rest
934 else f rest
935 | _ ->
936 f rest
938 | [] -> d
940 f state.layout
943 let getunder x y =
944 let g opaque l px py =
945 if state.bzoom
946 then (
947 match rectofblock opaque px py with
948 | Some a ->
949 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
950 state.rects <- [l.pageno, l.pageno mod 3, rect];
951 G.postRedisplay "getunder";
952 | None -> ()
954 match whatsunder opaque px py with
955 | Unone -> None
956 | under -> Some under
958 onppundermouse g x y Unone
961 let unproject x y =
962 let g opaque l x y =
963 match unproject opaque x y with
964 | Some (x, y) -> Some (Some (l.pageno, x, y))
965 | None -> None
967 onppundermouse g x y None;
970 let showtext c s =
971 state.text <- Printf.sprintf "%c%s" c s;
972 G.postRedisplay "showtext";
975 let paxunder x y =
976 let g opaque l px py =
977 if markunder opaque px py conf.paxmark
978 then (
979 Some (fun () ->
980 match getopaque l.pageno with
981 | None -> ()
982 | Some opaque ->
983 match Ne.pipe () with
984 | Ne.Exn exn ->
985 showtext '!'
986 (Printf.sprintf
987 "can not create mark pipe: %s"
988 (exntos exn));
989 | Ne.Res (r, w) ->
990 let doclose what fd =
991 Ne.clo fd (fun msg ->
992 dolog "%s close failed: %s" what msg)
995 popen conf.paxcmd [r, 0; w, -1];
996 copysel w opaque false;
997 doclose "pipe/r" r;
998 G.postRedisplay "paxunder";
999 with exn ->
1000 dolog "can not execute %S: %s"
1001 conf.paxcmd (exntos exn);
1002 doclose "pipe/r" r;
1003 doclose "pipe/w" w;
1006 else None
1008 G.postRedisplay "paxunder";
1009 if conf.paxmark = Mark_page
1010 then
1011 List.iter (fun l ->
1012 match getopaque l.pageno with
1013 | None -> ()
1014 | Some opaque -> clearmark opaque) state.layout;
1015 state.roam <-
1016 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1019 let selstring s =
1020 match Ne.pipe () with
1021 | Ne.Exn exn ->
1022 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1023 | Ne.Res (r, w) ->
1024 let popened =
1025 try popen conf.selcmd [r, 0; w, -1]; true
1026 with exn ->
1027 showtext '!'
1028 (Printf.sprintf "failed to execute %s: %s"
1029 conf.selcmd (exntos exn));
1030 false
1032 let clo cap fd =
1033 Ne.clo fd (fun msg ->
1034 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1037 if popened
1038 then (
1040 let l = String.length s in
1041 let n = tempfailureretry (Unix.write w s 0) l in
1042 if n != l
1043 then
1044 showtext '!'
1045 (Printf.sprintf
1046 "failed to write %d characters to sel pipe, wrote %d"
1049 with exn ->
1050 showtext '!'
1051 (Printf.sprintf "failed to write to sel pipe: %s"
1052 (exntos exn)
1055 else dolog "%s" s;
1056 clo "pipe/r" r;
1057 clo "pipe/w" w;
1060 let undertext = function
1061 | Unone -> "none"
1062 | Ulinkuri s -> s
1063 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1064 | Utext s -> "font: " ^ s
1065 | Uunexpected s -> "unexpected: " ^ s
1066 | Ulaunch s -> "launch: " ^ s
1067 | Unamed s -> "named: " ^ s
1068 | Uremote (filename, pageno) ->
1069 Printf.sprintf "%s: page %d" filename (pageno+1)
1070 | Uremotedest (filename, destname) ->
1071 Printf.sprintf "%s: destination %S" filename destname
1074 let updateunder x y =
1075 match getunder x y with
1076 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1077 | Ulinkuri uri ->
1078 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1079 Wsi.setcursor Wsi.CURSOR_INFO
1080 | Ulinkgoto (pageno, _) ->
1081 if conf.underinfo
1082 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1083 Wsi.setcursor Wsi.CURSOR_INFO
1084 | Utext s ->
1085 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1086 Wsi.setcursor Wsi.CURSOR_TEXT
1087 | Uunexpected s ->
1088 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1089 Wsi.setcursor Wsi.CURSOR_INHERIT
1090 | Ulaunch s ->
1091 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1092 Wsi.setcursor Wsi.CURSOR_INHERIT
1093 | Unamed s ->
1094 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1095 Wsi.setcursor Wsi.CURSOR_INHERIT
1096 | Uremote (filename, pageno) ->
1097 if conf.underinfo then showtext 'r'
1098 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1099 Wsi.setcursor Wsi.CURSOR_INFO
1100 | Uremotedest (filename, destname) ->
1101 if conf.underinfo then showtext 'r'
1102 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1103 Wsi.setcursor Wsi.CURSOR_INFO
1106 let showlinktype under =
1107 if conf.underinfo
1108 then
1109 match under with
1110 | Unone -> ()
1111 | under ->
1112 let s = undertext under in
1113 showtext ' ' s
1116 let addchar s c =
1117 let b = Buffer.create (String.length s + 1) in
1118 Buffer.add_string b s;
1119 Buffer.add_char b c;
1120 Buffer.contents b;
1123 module type TextEnumType =
1125 type t
1126 val name : string
1127 val names : string array
1128 end;;
1130 module TextEnumMake (Ten : TextEnumType) =
1131 struct
1132 let names = Ten.names;;
1133 let to_int (t : Ten.t) = Obj.magic t;;
1134 let to_string t = names.(to_int t);;
1135 let of_int n : Ten.t = Obj.magic n;;
1136 let of_string s =
1137 let rec find i =
1138 if i = Array.length names
1139 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1140 else (
1141 if Ten.names.(i) = s
1142 then of_int i
1143 else find (i+1)
1145 in find 0;;
1146 end;;
1148 module CSTE = TextEnumMake (struct
1149 type t = colorspace;;
1150 let name = "colorspace";;
1151 let names = [|"rgb"; "bgr"; "gray"|];;
1152 end);;
1154 module MTE = TextEnumMake (struct
1155 type t = mark;;
1156 let name = "mark";;
1157 let names = [|"page"; "block"; "line"; "word"|];;
1158 end);;
1160 module FMTE = TextEnumMake (struct
1161 type t= fitmodel;;
1162 let name = "fitmodel";;
1163 let names = [|"width"; "proportional"; "page"|];;
1164 end);;
1166 let intentry_with_suffix text key =
1167 let c =
1168 if key >= 32 && key < 127
1169 then Char.chr key
1170 else '\000'
1172 match Char.lowercase c with
1173 | '0' .. '9' ->
1174 let text = addchar text c in
1175 TEcont text
1177 | 'k' | 'm' | 'g' ->
1178 let text = addchar text c in
1179 TEcont text
1181 | _ ->
1182 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1183 TEcont text
1186 let multicolumns_to_string (n, a, b) =
1187 if a = 0 && b = 0
1188 then Printf.sprintf "%d" n
1189 else Printf.sprintf "%d,%d,%d" n a b;
1192 let multicolumns_of_string s =
1194 (int_of_string s, 0, 0)
1195 with _ ->
1196 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1197 if a > 1 || b > 1
1198 then failwith "subtly broken"; (n, a, b)
1202 let readcmd fd =
1203 let s = "xxxx" in
1204 let n = tempfailureretry (Unix.read fd s 0) 4 in
1205 if n != 4 then error "incomplete read(len) = %d" n;
1206 let len = 0
1207 lor (Char.code s.[0] lsl 24)
1208 lor (Char.code s.[1] lsl 16)
1209 lor (Char.code s.[2] lsl 8)
1210 lor (Char.code s.[3] lsl 0)
1212 let s = String.create len in
1213 let n = tempfailureretry (Unix.read fd s 0) len in
1214 if n != len then error "incomplete read(data) %d vs %d" n len;
1218 let btod b = if b then 1 else 0;;
1220 let wcmd fmt =
1221 let b = Buffer.create 16 in
1222 Buffer.add_string b "llll";
1223 Printf.kbprintf
1224 (fun b ->
1225 let s = Buffer.contents b in
1226 let n = String.length s in
1227 let len = n - 4 in
1228 (* dolog "wcmd %S" (String.sub s 4 len); *)
1229 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1230 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1231 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1232 s.[3] <- Char.chr (len land 0xff);
1233 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1234 if n' != n then error "write failed %d vs %d" n' n;
1235 ) b fmt;
1238 let calcips h =
1239 let d = state.winh - h in
1240 max conf.interpagespace ((d + 1) / 2)
1243 let rowyh (c, coverA, coverB) b n =
1244 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1245 then
1246 let _, _, vy, (_, _, h, _) = b.(n) in
1247 (vy, h)
1248 else
1249 let n' = n - coverA in
1250 let d = n' mod c in
1251 let s = n - d in
1252 let e = min state.pagecount (s + c) in
1253 let rec find m miny maxh = if m = e then miny, maxh else
1254 let _, _, y, (_, _, h, _) = b.(m) in
1255 let miny = min miny y in
1256 let maxh = max maxh h in
1257 find (m+1) miny maxh
1258 in find s max_int 0
1261 let calcheight () =
1262 match conf.columns with
1263 | Cmulti ((_, _, _) as cl, b) ->
1264 if Array.length b > 0
1265 then
1266 let y, h = rowyh cl b (Array.length b - 1) in
1267 y + h + (if conf.presentation then calcips h else 0)
1268 else 0
1269 | Csingle b ->
1270 if Array.length b > 0
1271 then
1272 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1273 y + h + (if conf.presentation then calcips h else 0)
1274 else 0
1275 | Csplit (_, b) ->
1276 if Array.length b > 0
1277 then
1278 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1279 y + h
1280 else 0
1283 let getpageywh pageno =
1284 let pageno = bound pageno 0 (state.pagecount-1) in
1285 match conf.columns with
1286 | Csingle b ->
1287 if Array.length b = 0
1288 then 0, 0, 0
1289 else
1290 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1291 let y =
1292 if conf.presentation
1293 then y - calcips h
1294 else y
1296 y, w, h
1297 | Cmulti (cl, b) ->
1298 if Array.length b = 0
1299 then 0, 0, 0
1300 else
1301 let y, h = rowyh cl b pageno in
1302 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1303 let y =
1304 if conf.presentation
1305 then y - calcips h
1306 else y
1308 y, w, h
1309 | Csplit (c, b) ->
1310 if Array.length b = 0
1311 then 0, 0, 0
1312 else
1313 let n = pageno*c in
1314 let (_, _, y, (_, w, h, _)) = b.(n) in
1315 y, w / c, h
1318 let getpageyh pageno =
1319 let y,_,h = getpageywh pageno in
1320 y, h;
1323 let getpagedim pageno =
1324 let rec f ppdim l =
1325 match l with
1326 | (n, _, _, _) as pdim :: rest ->
1327 if n >= pageno
1328 then (if n = pageno then pdim else ppdim)
1329 else f pdim rest
1331 | [] -> ppdim
1333 f (-1, -1, -1, -1) state.pdims
1336 let getpagey pageno = fst (getpageyh pageno);;
1338 let nogeomcmds cmds =
1339 match cmds with
1340 | s, [] -> emptystr s
1341 | _ -> false
1344 let page_of_y y =
1345 let ((c, coverA, coverB) as cl), b =
1346 match conf.columns with
1347 | Csingle b -> (1, 0, 0), b
1348 | Cmulti (c, b) -> c, b
1349 | Csplit (_, b) -> (1, 0, 0), b
1351 if Array.length b = 0
1352 then -1
1353 else
1354 let rec bsearch nmin nmax =
1355 if nmin > nmax
1356 then bound nmin 0 (state.pagecount-1)
1357 else
1358 let n = (nmax + nmin) / 2 in
1359 let vy, h = rowyh cl b n in
1360 let y0, y1 =
1361 if conf.presentation
1362 then
1363 let ips = calcips h in
1364 let y0 = vy - ips in
1365 let y1 = vy + h + ips in
1366 y0, y1
1367 else (
1368 if n = 0
1369 then 0, vy + h + conf.interpagespace
1370 else
1371 let y0 = vy - conf.interpagespace in
1372 y0, y0 + h + conf.interpagespace
1375 if y >= y0 && y < y1
1376 then (
1377 if c = 1
1378 then n
1379 else (
1380 if n > coverA
1381 then
1382 if n < state.pagecount - coverB
1383 then ((n-coverA)/c)*c + coverA
1384 else n
1385 else n
1388 else (
1389 if y > y0
1390 then bsearch (n+1) nmax
1391 else bsearch nmin (n-1)
1394 bsearch 0 (state.pagecount-1);
1397 let layoutN ((columns, coverA, coverB), b) y sh =
1398 let sh = sh - (hscrollh ()) in
1399 let rec fold accu n =
1400 if n = Array.length b
1401 then accu
1402 else
1403 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1404 if (vy - y) > sh &&
1405 (n = coverA - 1
1406 || n = state.pagecount - coverB
1407 || (n - coverA) mod columns = columns - 1)
1408 then accu
1409 else
1410 let accu =
1411 if vy + h > y
1412 then
1413 let pagey = max 0 (y - vy) in
1414 let pagedispy = if pagey > 0 then 0 else vy - y in
1415 let pagedispx, pagex =
1416 let pdx =
1417 if n = coverA - 1 || n = state.pagecount - coverB
1418 then state.x + (wadjsb state.winw - w) / 2
1419 else dx + xoff + state.x
1421 if pdx < 0
1422 then 0, -pdx
1423 else pdx, 0
1425 let pagevw =
1426 let vw = wadjsb state.winw - pagedispx in
1427 let pw = w - pagex in
1428 min vw pw
1430 let pagevh = min (h - pagey) (sh - pagedispy) in
1431 if pagevw > 0 && pagevh > 0
1432 then
1433 let e =
1434 { pageno = n
1435 ; pagedimno = pdimno
1436 ; pagew = w
1437 ; pageh = h
1438 ; pagex = pagex
1439 ; pagey = pagey
1440 ; pagevw = pagevw
1441 ; pagevh = pagevh
1442 ; pagedispx = pagedispx
1443 ; pagedispy = pagedispy
1444 ; pagecol = 0
1447 e :: accu
1448 else
1449 accu
1450 else
1451 accu
1453 fold accu (n+1)
1455 if Array.length b = 0
1456 then []
1457 else List.rev (fold [] (page_of_y y))
1460 let layoutS (columns, b) y sh =
1461 let sh = sh - hscrollh () in
1462 let rec fold accu n =
1463 if n = Array.length b
1464 then accu
1465 else
1466 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1467 if (vy - y) > sh
1468 then accu
1469 else
1470 let accu =
1471 if vy + pageh > y
1472 then
1473 let x = xoff + state.x in
1474 let pagey = max 0 (y - vy) in
1475 let pagedispy = if pagey > 0 then 0 else vy - y in
1476 let pagedispx, pagex =
1477 if px = 0
1478 then (
1479 if x < 0
1480 then 0, -x
1481 else x, 0
1483 else (
1484 let px = px - x in
1485 if px < 0
1486 then -px, 0
1487 else 0, px
1490 let pagecolw = pagew/columns in
1491 let pagedispx =
1492 if pagecolw < state.winw
1493 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1494 else pagedispx
1496 let pagevw =
1497 let vw = wadjsb state.winw - pagedispx in
1498 let pw = pagew - pagex in
1499 min vw pw
1501 let pagevw = min pagevw pagecolw in
1502 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1503 if pagevw > 0 && pagevh > 0
1504 then
1505 let e =
1506 { pageno = n/columns
1507 ; pagedimno = pdimno
1508 ; pagew = pagew
1509 ; pageh = pageh
1510 ; pagex = pagex
1511 ; pagey = pagey
1512 ; pagevw = pagevw
1513 ; pagevh = pagevh
1514 ; pagedispx = pagedispx
1515 ; pagedispy = pagedispy
1516 ; pagecol = n mod columns
1519 e :: accu
1520 else
1521 accu
1522 else
1523 accu
1525 fold accu (n+1)
1527 List.rev (fold [] 0)
1530 let layout y sh =
1531 if nogeomcmds state.geomcmds
1532 then
1533 match conf.columns with
1534 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1535 | Cmulti c -> layoutN c y sh
1536 | Csplit s -> layoutS s y sh
1537 else []
1540 let clamp incr =
1541 let y = state.y + incr in
1542 let y = max 0 y in
1543 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1547 let itertiles l f =
1548 let tilex = l.pagex mod conf.tilew in
1549 let tiley = l.pagey mod conf.tileh in
1551 let col = l.pagex / conf.tilew in
1552 let row = l.pagey / conf.tileh in
1554 let rec rowloop row y0 dispy h =
1555 if h = 0
1556 then ()
1557 else (
1558 let dh = conf.tileh - y0 in
1559 let dh = min h dh in
1560 let rec colloop col x0 dispx w =
1561 if w = 0
1562 then ()
1563 else (
1564 let dw = conf.tilew - x0 in
1565 let dw = min w dw in
1567 f col row dispx dispy x0 y0 dw dh;
1568 colloop (col+1) 0 (dispx+dw) (w-dw)
1571 colloop col tilex l.pagedispx l.pagevw;
1572 rowloop (row+1) 0 (dispy+dh) (h-dh)
1575 if l.pagevw > 0 && l.pagevh > 0
1576 then rowloop row tiley l.pagedispy l.pagevh;
1579 let gettileopaque l col row =
1580 let key =
1581 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1583 try Some (Hashtbl.find state.tilemap key)
1584 with Not_found -> None
1587 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1588 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1589 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1592 let filledrect x0 y0 x1 y1 =
1593 GlArray.disable `texture_coord;
1594 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1595 GlArray.vertex `two state.vraw;
1596 GlArray.draw_arrays `triangle_strip 0 4;
1597 GlArray.enable `texture_coord;
1600 let linerect x0 y0 x1 y1 =
1601 GlArray.disable `texture_coord;
1602 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1603 GlArray.vertex `two state.vraw;
1604 GlArray.draw_arrays `line_loop 0 4;
1605 GlArray.enable `texture_coord;
1608 let drawtiles l color =
1609 GlDraw.color color;
1610 begintiles ();
1611 let f col row x y tilex tiley w h =
1612 match gettileopaque l col row with
1613 | Some (opaque, _, t) ->
1614 let params = x, y, w, h, tilex, tiley in
1615 if conf.invert
1616 then (
1617 Gl.enable `blend;
1618 GlFunc.blend_func `zero `one_minus_src_color;
1620 drawtile params opaque;
1621 if conf.invert
1622 then Gl.disable `blend;
1623 if conf.debug
1624 then (
1625 endtiles ();
1626 let s = Printf.sprintf
1627 "%d[%d,%d] %f sec"
1628 l.pageno col row t
1630 let w = measurestr fstate.fontsize s in
1631 GlDraw.color (0.0, 0.0, 0.0);
1632 filledrect (float (x-2))
1633 (float (y-2))
1634 (float (x+2) +. w)
1635 (float (y + fstate.fontsize + 2));
1636 GlDraw.color (1.0, 1.0, 1.0);
1637 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1638 begintiles ();
1641 | None ->
1642 endtiles ();
1643 let w =
1644 let lw = wadjsb state.winw - x in
1645 min lw w
1646 and h =
1647 let lh = state.winh - y in
1648 min lh h
1650 if conf.invert
1651 then (
1652 Gl.enable `blend;
1653 GlFunc.blend_func `zero `one_minus_src_color;
1655 begin match state.texid with
1656 | Some id ->
1657 Gl.enable `texture_2d;
1658 GlTex.bind_texture `texture_2d id;
1659 let x0 = float x
1660 and y0 = float y
1661 and x1 = float (x+w)
1662 and y1 = float (y+h) in
1664 let tw = float w /. 16.0
1665 and th = float h /. 16.0 in
1666 let tx0 = float tilex /. 16.0
1667 and ty0 = float tiley /. 16.0 in
1668 let tx1 = tx0 +. tw
1669 and ty1 = ty0 +. th in
1670 Raw.sets_float state.vraw ~pos:0
1671 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1672 Raw.sets_float state.traw ~pos:0
1673 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1674 GlArray.vertex `two state.vraw;
1675 GlArray.tex_coord `two state.traw;
1676 GlArray.draw_arrays `triangle_strip 0 4;
1678 | None ->
1679 GlDraw.color (1.0, 1.0, 1.0);
1680 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1681 end;
1682 if w > 128 && h > fstate.fontsize + 10
1683 then (
1684 let c = if conf.invert then 1.0 else 0.0 in
1685 GlDraw.color (c, c, c);
1686 let c, r =
1687 if conf.verbose
1688 then (col*conf.tilew, row*conf.tileh)
1689 else col, row
1691 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1693 GlDraw.color color;
1694 begintiles ();
1696 itertiles l f;
1697 endtiles ();
1700 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1702 let tilevisible1 l x y =
1703 let ax0 = l.pagex
1704 and ax1 = l.pagex + l.pagevw
1705 and ay0 = l.pagey
1706 and ay1 = l.pagey + l.pagevh in
1708 let bx0 = x
1709 and by0 = y in
1710 let bx1 = min (bx0 + conf.tilew) l.pagew
1711 and by1 = min (by0 + conf.tileh) l.pageh in
1713 let rx0 = max ax0 bx0
1714 and ry0 = max ay0 by0
1715 and rx1 = min ax1 bx1
1716 and ry1 = min ay1 by1 in
1718 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1719 nonemptyintersection
1722 let tilevisible layout n x y =
1723 let rec findpageinlayout m = function
1724 | l :: rest when l.pageno = n ->
1725 tilevisible1 l x y || (
1726 match conf.columns with
1727 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1728 | _ -> false
1730 | _ :: rest -> findpageinlayout 0 rest
1731 | [] -> false
1733 findpageinlayout 0 layout;
1736 let tileready l x y =
1737 tilevisible1 l x y &&
1738 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1741 let tilepage n p layout =
1742 let rec loop = function
1743 | l :: rest ->
1744 if l.pageno = n
1745 then
1746 let f col row _ _ _ _ _ _ =
1747 if state.currently = Idle
1748 then
1749 match gettileopaque l col row with
1750 | Some _ -> ()
1751 | None ->
1752 let x = col*conf.tilew
1753 and y = row*conf.tileh in
1754 let w =
1755 let w = l.pagew - x in
1756 min w conf.tilew
1758 let h =
1759 let h = l.pageh - y in
1760 min h conf.tileh
1762 let pbo =
1763 if conf.usepbo
1764 then getpbo w h conf.colorspace
1765 else "0"
1767 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1768 state.currently <-
1769 Tiling (
1770 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1771 conf.tilew, conf.tileh
1774 itertiles l f;
1775 else
1776 loop rest
1778 | [] -> ()
1780 if nogeomcmds state.geomcmds
1781 then loop layout;
1784 let preloadlayout y =
1785 let y = if y < state.winh then 0 else y - state.winh in
1786 let h = state.winh*3 in
1787 layout y h;
1790 let load pages =
1791 let rec loop pages =
1792 if state.currently != Idle
1793 then ()
1794 else
1795 match pages with
1796 | l :: rest ->
1797 begin match getopaque l.pageno with
1798 | None ->
1799 wcmd "page %d %d" l.pageno l.pagedimno;
1800 state.currently <- Loading (l, state.gen);
1801 | Some opaque ->
1802 tilepage l.pageno opaque pages;
1803 loop rest
1804 end;
1805 | _ -> ()
1807 if nogeomcmds state.geomcmds
1808 then loop pages
1811 let preload pages =
1812 load pages;
1813 if conf.preload && state.currently = Idle
1814 then load (preloadlayout state.y);
1817 let layoutready layout =
1818 let rec fold all ls =
1819 all && match ls with
1820 | l :: rest ->
1821 let seen = ref false in
1822 let allvisible = ref true in
1823 let foo col row _ _ _ _ _ _ =
1824 seen := true;
1825 allvisible := !allvisible &&
1826 begin match gettileopaque l col row with
1827 | Some _ -> true
1828 | None -> false
1831 itertiles l foo;
1832 fold (!seen && !allvisible) rest
1833 | [] -> true
1835 let alltilesvisible = fold true layout in
1836 alltilesvisible;
1839 let gotoy y =
1840 let y = bound y 0 state.maxy in
1841 let y, layout, proceed =
1842 match conf.maxwait with
1843 | Some time when state.ghyll == noghyll ->
1844 begin match state.throttle with
1845 | None ->
1846 let layout = layout y state.winh in
1847 let ready = layoutready layout in
1848 if not ready
1849 then (
1850 load layout;
1851 state.throttle <- Some (layout, y, now ());
1853 else G.postRedisplay "gotoy showall (None)";
1854 y, layout, ready
1855 | Some (_, _, started) ->
1856 let dt = now () -. started in
1857 if dt > time
1858 then (
1859 state.throttle <- None;
1860 let layout = layout y state.winh in
1861 load layout;
1862 G.postRedisplay "maxwait";
1863 y, layout, true
1865 else -1, [], false
1868 | _ ->
1869 let layout = layout y state.winh in
1870 if not !wtmode || layoutready layout
1871 then G.postRedisplay "gotoy ready";
1872 y, layout, true
1874 if proceed
1875 then (
1876 state.y <- y;
1877 state.layout <- layout;
1878 begin match state.mode with
1879 | LinkNav (Ltexact (pageno, linkno)) ->
1880 let rec loop = function
1881 | [] ->
1882 state.mode <- LinkNav (Ltgendir 0)
1883 | l :: _ when l.pageno = pageno ->
1884 begin match getopaque pageno with
1885 | None ->
1886 state.mode <- LinkNav (Ltgendir 0)
1887 | Some opaque ->
1888 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1889 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1890 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1891 then state.mode <- LinkNav (Ltgendir 0)
1893 | _ :: rest -> loop rest
1895 loop layout
1896 | _ -> ()
1897 end;
1898 begin match state.mode with
1899 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1900 if not (pagevisible layout pageno)
1901 then (
1902 match state.layout with
1903 | [] -> ()
1904 | l :: _ ->
1905 state.mode <- Birdseye (
1906 conf, leftx, l.pageno, hooverpageno, anchor
1909 | LinkNav (Ltgendir dir as lt) ->
1910 let linknav =
1911 let rec loop = function
1912 | [] -> lt
1913 | l :: rest ->
1914 match getopaque l.pageno with
1915 | None -> loop rest
1916 | Some opaque ->
1917 let link =
1918 let ld =
1919 if dir = 0
1920 then LDfirstvisible (l.pagex, l.pagey, dir)
1921 else (
1922 if dir > 0 then LDfirst else LDlast
1925 findlink opaque ld
1927 match link with
1928 | Lnotfound -> loop rest
1929 | Lfound n ->
1930 showlinktype (getlink opaque n);
1931 Ltexact (l.pageno, n)
1933 loop state.layout
1935 state.mode <- LinkNav linknav
1936 | _ -> ()
1937 end;
1938 preload layout;
1940 state.ghyll <- noghyll;
1941 if conf.updatecurs
1942 then (
1943 let mx, my = state.mpos in
1944 updateunder mx my;
1948 let conttiling pageno opaque =
1949 tilepage pageno opaque
1950 (if conf.preload then preloadlayout state.y else state.layout)
1953 let gotoy_and_clear_text y =
1954 if not conf.verbose then state.text <- "";
1955 gotoy y;
1958 let getanchor1 l =
1959 let top =
1960 let coloff = l.pagecol * l.pageh in
1961 float (l.pagey + coloff) /. float l.pageh
1963 let dtop =
1964 if l.pagedispy = 0
1965 then
1967 else (
1968 if conf.presentation
1969 then float l.pagedispy /. float (calcips l.pageh)
1970 else float l.pagedispy /. float conf.interpagespace
1973 (l.pageno, top, dtop)
1976 let getanchor () =
1977 match state.layout with
1978 | l :: _ -> getanchor1 l
1979 | [] ->
1980 let n = page_of_y state.y in
1981 if n = -1
1982 then state.anchor
1983 else
1984 let y, h = getpageyh n in
1985 let dy = y - state.y in
1986 let dtop =
1987 if conf.presentation
1988 then
1989 let ips = calcips h in
1990 float (dy + ips) /. float ips
1991 else
1992 float dy /. float conf.interpagespace
1994 (n, 0.0, dtop)
1997 let getanchory (n, top, dtop) =
1998 let y, h = getpageyh n in
1999 if conf.presentation
2000 then
2001 let ips = calcips h in
2002 y + truncate (top*.float h -. dtop*.float ips) + ips;
2003 else
2004 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2007 let gotoanchor anchor =
2008 gotoy (getanchory anchor);
2011 let addnav () =
2012 cbput state.hists.nav (getanchor ());
2015 let getnav dir =
2016 let anchor = cbgetc state.hists.nav dir in
2017 getanchory anchor;
2020 let gotoghyll y =
2021 let scroll f n a b =
2022 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2023 let snake f a b =
2024 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2025 if f < a
2026 then s (float f /. float a)
2027 else (
2028 if f > b
2029 then 1.0 -. s ((float (f-b) /. float (n-b)))
2030 else 1.0
2033 snake f a b
2034 and summa n a b =
2035 let ins = float a *. 0.5
2036 and outs = float (n-b) *. 0.5 in
2037 let ones = b - a in
2038 ins +. outs +. float ones
2040 let rec set (_N, _A, _B) y sy =
2041 let sum = summa _N _A _B in
2042 let dy = float (y - sy) in
2043 state.ghyll <- (
2044 let rec gf n y1 o =
2045 if n >= _N
2046 then state.ghyll <- noghyll
2047 else
2048 let go n =
2049 let s = scroll n _N _A _B in
2050 let y1 = y1 +. ((s *. dy) /. sum) in
2051 gotoy_and_clear_text (truncate y1);
2052 state.ghyll <- gf (n+1) y1;
2054 match o with
2055 | None -> go n
2056 | Some y' -> set (_N/2, 1, 1) y' state.y
2058 gf 0 (float state.y)
2061 match conf.ghyllscroll with
2062 | None ->
2063 gotoy_and_clear_text y
2064 | Some nab ->
2065 if state.ghyll == noghyll
2066 then set nab y state.y
2067 else state.ghyll (Some y)
2070 let gotopage n top =
2071 let y, h = getpageyh n in
2072 let y = y + (truncate (top *. float h)) in
2073 gotoghyll y
2076 let gotopage1 n top =
2077 let y = getpagey n in
2078 let y = y + top in
2079 gotoghyll y
2082 let invalidate s f =
2083 state.layout <- [];
2084 state.pdims <- [];
2085 state.rects <- [];
2086 state.rects1 <- [];
2087 match state.geomcmds with
2088 | ps, [] when emptystr ps ->
2089 f ();
2090 state.geomcmds <- s, [];
2092 | ps, [] ->
2093 state.geomcmds <- ps, [s, f];
2095 | ps, (s', _) :: rest when s' = s ->
2096 state.geomcmds <- ps, ((s, f) :: rest);
2098 | ps, cmds ->
2099 state.geomcmds <- ps, ((s, f) :: cmds);
2102 let flushpages () =
2103 Hashtbl.iter (fun _ opaque ->
2104 wcmd "freepage %s" opaque;
2105 ) state.pagemap;
2106 Hashtbl.clear state.pagemap;
2109 let flushtiles () =
2110 if not (Queue.is_empty state.tilelru)
2111 then (
2112 Queue.iter (fun (k, p, s) ->
2113 wcmd "freetile %s" p;
2114 state.memused <- state.memused - s;
2115 Hashtbl.remove state.tilemap k;
2116 ) state.tilelru;
2117 state.uioh#infochanged Memused;
2118 Queue.clear state.tilelru;
2120 load state.layout;
2123 let stateh h =
2124 let h = truncate (float h*.conf.zoom) in
2125 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2126 h - d
2129 let opendoc path password =
2130 state.path <- path;
2131 state.password <- password;
2132 state.gen <- state.gen + 1;
2133 state.docinfo <- [];
2135 flushpages ();
2136 setaalevel conf.aalevel;
2137 let titlepath =
2138 if emptystr state.origin
2139 then path
2140 else state.origin
2142 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2143 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2144 invalidate "reqlayout"
2145 (fun () ->
2146 wcmd "reqlayout %d %d %d %s\000"
2147 conf.angle (FMTE.to_int conf.fitmodel)
2148 (stateh state.winh) state.nameddest
2152 let reload () =
2153 state.anchor <- getanchor ();
2154 opendoc state.path state.password;
2157 let scalecolor c =
2158 let c = c *. conf.colorscale in
2159 (c, c, c);
2162 let scalecolor2 (r, g, b) =
2163 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2166 let docolumns = function
2167 | Csingle _ ->
2168 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2169 let rec loop pageno pdimno pdim y ph pdims =
2170 if pageno = state.pagecount
2171 then ()
2172 else
2173 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2174 match pdims with
2175 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2176 pdimno+1, pdim, rest
2177 | _ ->
2178 pdimno, pdim, pdims
2180 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2181 let y = y +
2182 (if conf.presentation
2183 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2184 else (if pageno = 0 then 0 else conf.interpagespace)
2187 a.(pageno) <- (pdimno, x, y, pdim);
2188 loop (pageno+1) pdimno pdim (y + h) h pdims
2190 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2191 conf.columns <- Csingle a;
2193 | Cmulti ((columns, coverA, coverB), _) ->
2194 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2195 let rec loop pageno pdimno pdim x y rowh pdims =
2196 let rec fixrow m = if m = pageno then () else
2197 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2198 if h < rowh
2199 then (
2200 let y = y + (rowh - h) / 2 in
2201 a.(m) <- (pdimno, x, y, pdim);
2203 fixrow (m+1)
2205 if pageno = state.pagecount
2206 then fixrow (((pageno - 1) / columns) * columns)
2207 else
2208 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2209 match pdims with
2210 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2211 pdimno+1, pdim, rest
2212 | _ ->
2213 pdimno, pdim, pdims
2215 let x, y, rowh' =
2216 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2217 then (
2218 let x = (wadjsb state.winw - w) / 2 in
2219 let ips =
2220 if conf.presentation then calcips h else conf.interpagespace in
2221 x, y + ips + rowh, h
2223 else (
2224 if (pageno - coverA) mod columns = 0
2225 then (
2226 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2227 let y =
2228 if conf.presentation
2229 then
2230 let ips = calcips h in
2231 y + (if pageno = 0 then 0 else calcips rowh + ips)
2232 else
2233 y + (if pageno = 0 then 0 else conf.interpagespace)
2235 x, y + rowh, h
2237 else x, y, max rowh h
2240 let y =
2241 if pageno > 1 && (pageno - coverA) mod columns = 0
2242 then (
2243 let y =
2244 if pageno = columns && conf.presentation
2245 then (
2246 let ips = calcips rowh in
2247 for i = 0 to pred columns
2249 let (pdimno, x, y, pdim) = a.(i) in
2250 a.(i) <- (pdimno, x, y+ips, pdim)
2251 done;
2252 y+ips;
2254 else y
2256 fixrow (pageno - columns);
2259 else y
2261 a.(pageno) <- (pdimno, x, y, pdim);
2262 let x = x + w + xoff*2 + conf.interpagespace in
2263 loop (pageno+1) pdimno pdim x y rowh' pdims
2265 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2266 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2268 | Csplit (c, _) ->
2269 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2270 let rec loop pageno pdimno pdim y pdims =
2271 if pageno = state.pagecount
2272 then ()
2273 else
2274 let pdimno, ((_, w, h, _) as pdim), pdims =
2275 match pdims with
2276 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2277 pdimno+1, pdim, rest
2278 | _ ->
2279 pdimno, pdim, pdims
2281 let cw = w / c in
2282 let rec loop1 n x y =
2283 if n = c then y else (
2284 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2285 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2288 let y = loop1 0 0 y in
2289 loop (pageno+1) pdimno pdim y pdims
2291 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2292 conf.columns <- Csplit (c, a);
2295 let represent () =
2296 docolumns conf.columns;
2297 state.maxy <- calcheight ();
2298 if state.reprf == noreprf
2299 then (
2300 match state.mode with
2301 | Birdseye (_, _, pageno, _, _) ->
2302 let y, h = getpageyh pageno in
2303 let top = (state.winh - h) / 2 in
2304 gotoy (max 0 (y - top))
2305 | _ -> gotoanchor state.anchor
2307 else (
2308 state.reprf ();
2309 state.reprf <- noreprf;
2313 let reshape w h =
2314 GlDraw.viewport 0 0 w h;
2315 let firsttime = state.geomcmds == firstgeomcmds in
2316 if not firsttime && nogeomcmds state.geomcmds
2317 then state.anchor <- getanchor ();
2319 state.winw <- w;
2320 let w = wadjsb (truncate (float w *. conf.zoom)) in
2321 let w = max w 2 in
2322 state.winh <- h;
2323 setfontsize fstate.fontsize;
2324 GlMat.mode `modelview;
2325 GlMat.load_identity ();
2327 GlMat.mode `projection;
2328 GlMat.load_identity ();
2329 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2330 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2331 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2333 let relx =
2334 if conf.zoom <= 1.0
2335 then 0.0
2336 else float state.x /. float state.w
2338 invalidate "geometry"
2339 (fun () ->
2340 state.w <- w;
2341 if not firsttime
2342 then state.x <- truncate (relx *. float w);
2343 let w =
2344 match conf.columns with
2345 | Csingle _ -> w
2346 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2347 | Csplit (c, _) -> w * c
2349 wcmd "geometry %d %d %d"
2350 w (stateh h) (FMTE.to_int conf.fitmodel)
2354 let enttext () =
2355 let len = String.length state.text in
2356 let drawstring s =
2357 let hscrollh =
2358 match state.mode with
2359 | Textentry _ | View | LinkNav _ ->
2360 let h, _, _ = state.uioh#scrollpw in
2362 | _ -> 0
2364 let rect x w =
2365 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2366 (x+.w) (float (state.winh - hscrollh))
2369 let w = float (wadjsb state.winw - 1) in
2370 if state.progress >= 0.0 && state.progress < 1.0
2371 then (
2372 GlDraw.color (0.3, 0.3, 0.3);
2373 let w1 = w *. state.progress in
2374 rect 0.0 w1;
2375 GlDraw.color (0.0, 0.0, 0.0);
2376 rect w1 (w-.w1)
2378 else (
2379 GlDraw.color (0.0, 0.0, 0.0);
2380 rect 0.0 w;
2383 GlDraw.color (1.0, 1.0, 1.0);
2384 drawstring fstate.fontsize
2385 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2387 let s =
2388 match state.mode with
2389 | Textentry ((prefix, text, _, _, _, _), _) ->
2390 let s =
2391 if len > 0
2392 then
2393 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2394 else
2395 Printf.sprintf "%s%s_" prefix text
2399 | _ -> state.text
2401 let s =
2402 if state.newerrmsgs
2403 then (
2404 if not (istextentry state.mode) && state.uioh#eformsgs
2405 then
2406 let s1 = "(press 'e' to review error messasges)" in
2407 if nonemptystr s then s ^ " " ^ s1 else s1
2408 else s
2410 else s
2412 if nonemptystr s
2413 then drawstring s
2416 let gctiles () =
2417 let len = Queue.length state.tilelru in
2418 let layout = lazy (
2419 match state.throttle with
2420 | None ->
2421 if conf.preload
2422 then preloadlayout state.y
2423 else state.layout
2424 | Some (layout, _, _) ->
2425 layout
2426 ) in
2427 let rec loop qpos =
2428 if state.memused <= conf.memlimit
2429 then ()
2430 else (
2431 if qpos < len
2432 then
2433 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2434 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2435 let (_, pw, ph, _) = getpagedim n in
2437 gen = state.gen
2438 && colorspace = conf.colorspace
2439 && angle = conf.angle
2440 && pagew = pw
2441 && pageh = ph
2442 && (
2443 let x = col*conf.tilew
2444 and y = row*conf.tileh in
2445 tilevisible (Lazy.force_val layout) n x y
2447 then Queue.push lruitem state.tilelru
2448 else (
2449 freepbo p;
2450 wcmd "freetile %s" p;
2451 state.memused <- state.memused - s;
2452 state.uioh#infochanged Memused;
2453 Hashtbl.remove state.tilemap k;
2455 loop (qpos+1)
2458 loop 0
2461 let logcurrently = function
2462 | Idle -> dolog "Idle"
2463 | Loading (l, gen) ->
2464 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2465 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2466 dolog
2467 "Tiling %d[%d,%d] page=%s cs=%s angle"
2468 l.pageno col row pageopaque
2469 (CSTE.to_string colorspace)
2471 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2472 angle gen conf.angle state.gen
2473 tilew tileh
2474 conf.tilew conf.tileh
2476 | Outlining _ ->
2477 dolog "outlining"
2480 let splitatspace =
2481 let r = Str.regexp " " in
2482 fun s -> Str.bounded_split r s 2;
2485 let onpagerect pageno f =
2486 let b =
2487 match conf.columns with
2488 | Cmulti (_, b) -> b
2489 | Csingle b -> b
2490 | Csplit (_, b) -> b
2492 if pageno >= 0 && pageno < Array.length b
2493 then
2494 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2495 f w h
2498 let gotopagexy1 pageno x y =
2499 let _,w1,h1,leftx = getpagedim pageno in
2500 let top = y /. (float h1) in
2501 let left = x /. (float w1) in
2502 let py, w, h = getpageywh pageno in
2503 let wh = state.winh - hscrollh () in
2504 let x = left *. (float w) in
2505 let x = leftx + state.x + truncate x in
2506 let sx =
2507 if x < 0 || x >= wadjsb state.winw
2508 then state.x - x
2509 else state.x
2511 let pdy = truncate (top *. float h) in
2512 let y' = py + pdy in
2513 let dy = y' - state.y in
2514 let sy =
2515 if x != state.x || not (dy > 0 && dy < wh)
2516 then (
2517 if conf.presentation
2518 then
2519 if abs (py - y') > wh
2520 then y'
2521 else py
2522 else y';
2524 else state.y
2526 if state.x != sx || state.y != sy
2527 then (
2528 let x, y =
2529 if !wtmode
2530 then (
2531 let ww = wadjsb state.winw in
2532 let qx = sx / ww
2533 and qy = pdy / wh in
2534 let x = qx * ww
2535 and y = py + qy * wh in
2536 let x = if -x + ww > w1 then -(w1-ww) else x
2537 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2538 let y =
2539 if conf.presentation
2540 then
2541 if abs (py - y') > wh
2542 then y'
2543 else py
2544 else y';
2546 (x, y)
2548 else (sx, sy)
2550 state.x <- x;
2551 gotoy_and_clear_text y;
2553 else gotoy_and_clear_text state.y;
2556 let gotopagexy pageno x y =
2557 match state.mode with
2558 | Birdseye _ -> gotopage pageno 0.0
2559 | _ -> gotopagexy1 pageno x y
2562 let act cmds =
2563 (* dolog "%S" cmds; *)
2564 let cl = splitatspace cmds in
2565 let scan s fmt f =
2566 try Scanf.sscanf s fmt f
2567 with exn ->
2568 dolog "error processing '%S': %s" cmds (exntos exn);
2569 exit 1
2571 let addoutline outline =
2572 match state.currently with
2573 | Outlining outlines ->
2574 state.currently <- Outlining (outline :: outlines)
2575 | Idle -> state.currently <- Outlining [outline]
2576 | currently ->
2577 dolog "invalid outlining state";
2578 logcurrently currently
2580 match cl with
2581 | "clear" :: [] ->
2582 state.uioh#infochanged Pdim;
2583 state.pdims <- [];
2585 | "clearrects" :: [] ->
2586 state.rects <- state.rects1;
2587 G.postRedisplay "clearrects";
2589 | "continue" :: args :: [] ->
2590 let n = scan args "%u" (fun n -> n) in
2591 state.pagecount <- n;
2592 begin match state.currently with
2593 | Outlining l ->
2594 state.currently <- Idle;
2595 state.outlines <- Array.of_list (List.rev l)
2596 | _ -> ()
2597 end;
2599 let cur, cmds = state.geomcmds in
2600 if emptystr cur
2601 then failwith "umpossible";
2603 begin match List.rev cmds with
2604 | [] ->
2605 state.geomcmds <- "", [];
2606 represent ();
2607 | (s, f) :: rest ->
2608 f ();
2609 state.geomcmds <- s, List.rev rest;
2610 end;
2611 if conf.maxwait = None && not !wtmode
2612 then G.postRedisplay "continue";
2614 | "title" :: args :: [] ->
2615 Wsi.settitle args
2617 | "msg" :: args :: [] ->
2618 showtext ' ' args
2620 | "vmsg" :: args :: [] ->
2621 if conf.verbose
2622 then showtext ' ' args
2624 | "emsg" :: args :: [] ->
2625 Buffer.add_string state.errmsgs args;
2626 state.newerrmsgs <- true;
2627 G.postRedisplay "error message"
2629 | "progress" :: args :: [] ->
2630 let progress, text =
2631 scan args "%f %n"
2632 (fun f pos ->
2633 f, String.sub args pos (String.length args - pos))
2635 state.text <- text;
2636 state.progress <- progress;
2637 G.postRedisplay "progress"
2639 | "firstmatch" :: args :: [] ->
2640 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2641 scan args "%u %d %f %f %f %f %f %f %f %f"
2642 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2643 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2645 let y = (getpagey pageno) + truncate y0 in
2646 addnav ();
2647 gotoy y;
2648 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2650 | "match" :: args :: [] ->
2651 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2652 scan args "%u %d %f %f %f %f %f %f %f %f"
2653 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2654 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2656 state.rects1 <-
2657 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2659 | "page" :: args :: [] ->
2660 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2661 begin match state.currently with
2662 | Loading (l, gen) ->
2663 vlog "page %d took %f sec" l.pageno t;
2664 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2665 begin match state.throttle with
2666 | None ->
2667 let preloadedpages =
2668 if conf.preload
2669 then preloadlayout state.y
2670 else state.layout
2672 let evict () =
2673 let set =
2674 List.fold_left (fun s l -> IntSet.add l.pageno s)
2675 IntSet.empty preloadedpages
2677 let evictedpages =
2678 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2679 if not (IntSet.mem pageno set)
2680 then (
2681 wcmd "freepage %s" opaque;
2682 key :: accu
2684 else accu
2685 ) state.pagemap []
2687 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2689 evict ();
2690 state.currently <- Idle;
2691 if gen = state.gen
2692 then (
2693 tilepage l.pageno pageopaque state.layout;
2694 load state.layout;
2695 load preloadedpages;
2696 if pagevisible state.layout l.pageno
2697 && layoutready state.layout
2698 then G.postRedisplay "page";
2701 | Some (layout, _, _) ->
2702 state.currently <- Idle;
2703 tilepage l.pageno pageopaque layout;
2704 load state.layout
2705 end;
2707 | _ ->
2708 dolog "Inconsistent loading state";
2709 logcurrently state.currently;
2710 exit 1
2713 | "tile" :: args :: [] ->
2714 let (x, y, opaque, size, t) =
2715 scan args "%u %u %s %u %f"
2716 (fun x y p size t -> (x, y, p, size, t))
2718 begin match state.currently with
2719 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2720 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2722 unmappbo opaque;
2723 if tilew != conf.tilew || tileh != conf.tileh
2724 then (
2725 wcmd "freetile %s" opaque;
2726 state.currently <- Idle;
2727 load state.layout;
2729 else (
2730 puttileopaque l col row gen cs angle opaque size t;
2731 state.memused <- state.memused + size;
2732 state.uioh#infochanged Memused;
2733 gctiles ();
2734 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2735 opaque, size) state.tilelru;
2737 let layout =
2738 match state.throttle with
2739 | None -> state.layout
2740 | Some (layout, _, _) -> layout
2743 state.currently <- Idle;
2744 if gen = state.gen
2745 && conf.colorspace = cs
2746 && conf.angle = angle
2747 && tilevisible layout l.pageno x y
2748 then conttiling l.pageno pageopaque;
2750 begin match state.throttle with
2751 | None ->
2752 preload state.layout;
2753 if gen = state.gen
2754 && conf.colorspace = cs
2755 && conf.angle = angle
2756 && tilevisible state.layout l.pageno x y
2757 && (not !wtmode || layoutready state.layout)
2758 then G.postRedisplay "tile nothrottle";
2760 | Some (layout, y, _) ->
2761 let ready = layoutready layout in
2762 if ready
2763 then (
2764 state.y <- y;
2765 state.layout <- layout;
2766 state.throttle <- None;
2767 G.postRedisplay "throttle";
2769 else load layout;
2770 end;
2773 | _ ->
2774 dolog "Inconsistent tiling state";
2775 logcurrently state.currently;
2776 exit 1
2779 | "pdim" :: args :: [] ->
2780 let (n, w, h, _) as pdim =
2781 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2783 let pdim =
2784 match conf.fitmodel, conf.columns with
2785 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2786 | _ -> pdim
2788 state.uioh#infochanged Pdim;
2789 state.pdims <- pdim :: state.pdims
2791 | "o" :: args :: [] ->
2792 let (l, n, t, h, pos) =
2793 scan args "%u %u %d %u %n"
2794 (fun l n t h pos -> l, n, t, h, pos)
2796 let s = String.sub args pos (String.length args - pos) in
2797 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2799 | "ou" :: args :: [] ->
2800 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2801 let s = String.sub args pos len in
2802 let pos2 = pos + len + 1 in
2803 let uri = String.sub args pos2 (String.length args - pos2) in
2804 addoutline (s, l, Ouri uri)
2806 | "on" :: args :: [] ->
2807 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2808 let s = String.sub args pos (String.length args - pos) in
2809 addoutline (s, l, Onone)
2811 | "a" :: args :: [] ->
2812 let (n, l, t) =
2813 scan args "%u %d %d" (fun n l t -> n, l, t)
2815 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2817 | "info" :: args :: [] ->
2818 state.docinfo <- (1, args) :: state.docinfo
2820 | "infoend" :: [] ->
2821 state.uioh#infochanged Docinfo;
2822 state.docinfo <- List.rev state.docinfo
2824 | _ ->
2825 error "unknown cmd `%S'" cmds
2828 let onhist cb =
2829 let rc = cb.rc in
2830 let action = function
2831 | HCprev -> cbget cb ~-1
2832 | HCnext -> cbget cb 1
2833 | HCfirst -> cbget cb ~-(cb.rc)
2834 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2835 and cancel () = cb.rc <- rc
2836 in (action, cancel)
2839 let search pattern forward =
2840 match conf.columns with
2841 | Csplit _ ->
2842 showtext '!' "searching does not work properly in split columns mode"
2843 | _ ->
2844 if nonemptystr pattern
2845 then
2846 let pn, py =
2847 match state.layout with
2848 | [] -> 0, 0
2849 | l :: _ ->
2850 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2852 wcmd "search %d %d %d %d,%s\000"
2853 (btod conf.icase) pn py (btod forward) pattern;
2856 let intentry text key =
2857 let c =
2858 if key >= 32 && key < 127
2859 then Char.chr key
2860 else '\000'
2862 match c with
2863 | '0' .. '9' ->
2864 let text = addchar text c in
2865 TEcont text
2867 | _ ->
2868 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2869 TEcont text
2872 let linknentry text key =
2873 let c =
2874 if key >= 32 && key < 127
2875 then Char.chr key
2876 else '\000'
2878 match c with
2879 | 'a' .. 'z' ->
2880 let text = addchar text c in
2881 TEcont text
2883 | _ ->
2884 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2885 TEcont text
2888 let linkndone f s =
2889 if nonemptystr s
2890 then (
2891 let n =
2892 let l = String.length s in
2893 let rec loop pos n = if pos = l then n else
2894 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2895 loop (pos+1) (n*26 + m)
2896 in loop 0 0
2898 let rec loop n = function
2899 | [] -> ()
2900 | l :: rest ->
2901 match getopaque l.pageno with
2902 | None -> loop n rest
2903 | Some opaque ->
2904 let m = getlinkcount opaque in
2905 if n < m
2906 then (
2907 let under = getlink opaque n in
2908 f under
2910 else loop (n-m) rest
2912 loop n state.layout;
2916 let textentry text key =
2917 if key land 0xff00 = 0xff00
2918 then TEcont text
2919 else TEcont (text ^ toutf8 key)
2922 let reqlayout angle fitmodel =
2923 match state.throttle with
2924 | None ->
2925 if nogeomcmds state.geomcmds
2926 then state.anchor <- getanchor ();
2927 conf.angle <- angle mod 360;
2928 if conf.angle != 0
2929 then (
2930 match state.mode with
2931 | LinkNav _ -> state.mode <- View
2932 | _ -> ()
2934 conf.fitmodel <- fitmodel;
2935 invalidate "reqlayout"
2936 (fun () ->
2937 wcmd "reqlayout %d %d %d"
2938 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2940 | _ -> ()
2943 let settrim trimmargins trimfuzz =
2944 if nogeomcmds state.geomcmds
2945 then state.anchor <- getanchor ();
2946 conf.trimmargins <- trimmargins;
2947 conf.trimfuzz <- trimfuzz;
2948 let x0, y0, x1, y1 = trimfuzz in
2949 invalidate "settrim"
2950 (fun () ->
2951 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2952 flushpages ();
2955 let setzoom zoom =
2956 match state.throttle with
2957 | None ->
2958 let zoom = max 0.0001 zoom in
2959 if zoom <> conf.zoom
2960 then (
2961 state.prevzoom <- (conf.zoom, state.x);
2962 conf.zoom <- zoom;
2963 reshape state.winw state.winh;
2964 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2967 | Some (layout, y, started) ->
2968 let time =
2969 match conf.maxwait with
2970 | None -> 0.0
2971 | Some t -> t
2973 let dt = now () -. started in
2974 if dt > time
2975 then (
2976 state.y <- y;
2977 load layout;
2981 let setcolumns mode columns coverA coverB =
2982 state.prevcolumns <- Some (conf.columns, conf.zoom);
2983 if columns < 0
2984 then (
2985 if isbirdseye mode
2986 then showtext '!' "split mode doesn't work in bird's eye"
2987 else (
2988 conf.columns <- Csplit (-columns, [||]);
2989 state.x <- 0;
2990 conf.zoom <- 1.0;
2993 else (
2994 if columns < 2
2995 then (
2996 conf.columns <- Csingle [||];
2997 state.x <- 0;
2998 setzoom 1.0;
3000 else (
3001 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3002 conf.zoom <- 1.0;
3005 reshape state.winw state.winh;
3008 let enterbirdseye () =
3009 let zoom = float conf.thumbw /. float state.winw in
3010 let birdseyepageno =
3011 let cy = state.winh / 2 in
3012 let fold = function
3013 | [] -> 0
3014 | l :: rest ->
3015 let rec fold best = function
3016 | [] -> best.pageno
3017 | l :: rest ->
3018 let d = cy - (l.pagedispy + l.pagevh/2)
3019 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3020 if abs d < abs dbest
3021 then fold l rest
3022 else best.pageno
3023 in fold l rest
3025 fold state.layout
3027 state.mode <- Birdseye (
3028 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3030 conf.zoom <- zoom;
3031 conf.presentation <- false;
3032 conf.interpagespace <- 10;
3033 conf.hlinks <- false;
3034 conf.fitmodel <- FitProportional;
3035 state.x <- 0;
3036 state.mstate <- Mnone;
3037 conf.maxwait <- None;
3038 conf.columns <- (
3039 match conf.beyecolumns with
3040 | Some c ->
3041 conf.zoom <- 1.0;
3042 Cmulti ((c, 0, 0), [||])
3043 | None -> Csingle [||]
3045 Wsi.setcursor Wsi.CURSOR_INHERIT;
3046 if conf.verbose
3047 then
3048 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3049 (100.0*.zoom)
3050 else
3051 state.text <- ""
3053 reshape state.winw state.winh;
3056 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3057 state.mode <- View;
3058 conf.zoom <- c.zoom;
3059 conf.presentation <- c.presentation;
3060 conf.interpagespace <- c.interpagespace;
3061 conf.maxwait <- c.maxwait;
3062 conf.hlinks <- c.hlinks;
3063 conf.fitmodel <- c.fitmodel;
3064 conf.beyecolumns <- (
3065 match conf.columns with
3066 | Cmulti ((c, _, _), _) -> Some c
3067 | Csingle _ -> None
3068 | Csplit _ -> failwith "leaving bird's eye split mode"
3070 conf.columns <- (
3071 match c.columns with
3072 | Cmulti (c, _) -> Cmulti (c, [||])
3073 | Csingle _ -> Csingle [||]
3074 | Csplit (c, _) -> Csplit (c, [||])
3076 if conf.verbose
3077 then
3078 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3079 (100.0*.conf.zoom)
3081 reshape state.winw state.winh;
3082 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3083 state.x <- leftx;
3086 let togglebirdseye () =
3087 match state.mode with
3088 | Birdseye vals -> leavebirdseye vals true
3089 | View -> enterbirdseye ()
3090 | _ -> ()
3093 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3094 let pageno = max 0 (pageno - incr) in
3095 let rec loop = function
3096 | [] -> gotopage1 pageno 0
3097 | l :: _ when l.pageno = pageno ->
3098 if l.pagedispy >= 0 && l.pagey = 0
3099 then G.postRedisplay "upbirdseye"
3100 else gotopage1 pageno 0
3101 | _ :: rest -> loop rest
3103 loop state.layout;
3104 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3107 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3108 let pageno = min (state.pagecount - 1) (pageno + incr) in
3109 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3110 let rec loop = function
3111 | [] ->
3112 let y, h = getpageyh pageno in
3113 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3114 gotoy (clamp dy)
3115 | l :: _ when l.pageno = pageno ->
3116 if l.pagevh != l.pageh
3117 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3118 else G.postRedisplay "downbirdseye"
3119 | _ :: rest -> loop rest
3121 loop state.layout
3124 let boundastep h step =
3125 if step < 0
3126 then bound step ~-h 0
3127 else bound step 0 h
3130 let optentry mode _ key =
3131 let btos b = if b then "on" else "off" in
3132 if key >= 32 && key < 127
3133 then
3134 let c = Char.chr key in
3135 match c with
3136 | 's' ->
3137 let ondone s =
3138 try conf.scrollstep <- int_of_string s with exc ->
3139 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3141 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3143 | 'A' ->
3144 let ondone s =
3146 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3147 if state.autoscroll <> None
3148 then state.autoscroll <- Some conf.autoscrollstep
3149 with exc ->
3150 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3152 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3154 | 'C' ->
3155 let ondone s =
3157 let n, a, b = multicolumns_of_string s in
3158 setcolumns mode n a b;
3159 with exc ->
3160 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3162 TEswitch ("columns: ", "", None, textentry, ondone, true)
3164 | 'Z' ->
3165 let ondone s =
3167 let zoom = float (int_of_string s) /. 100.0 in
3168 setzoom zoom
3169 with exc ->
3170 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3172 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3174 | 't' ->
3175 let ondone s =
3177 conf.thumbw <- bound (int_of_string s) 2 4096;
3178 state.text <-
3179 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3180 begin match mode with
3181 | Birdseye beye ->
3182 leavebirdseye beye false;
3183 enterbirdseye ();
3184 | _ -> ();
3186 with exc ->
3187 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3189 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3191 | 'R' ->
3192 let ondone s =
3193 match try
3194 Some (int_of_string s)
3195 with exc ->
3196 state.text <- Printf.sprintf "bad integer `%s': %s"
3197 s (exntos exc);
3198 None
3199 with
3200 | Some angle -> reqlayout angle conf.fitmodel
3201 | None -> ()
3203 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3205 | 'i' ->
3206 conf.icase <- not conf.icase;
3207 TEdone ("case insensitive search " ^ (btos conf.icase))
3209 | 'p' ->
3210 conf.preload <- not conf.preload;
3211 gotoy state.y;
3212 TEdone ("preload " ^ (btos conf.preload))
3214 | 'v' ->
3215 conf.verbose <- not conf.verbose;
3216 TEdone ("verbose " ^ (btos conf.verbose))
3218 | 'd' ->
3219 conf.debug <- not conf.debug;
3220 TEdone ("debug " ^ (btos conf.debug))
3222 | 'h' ->
3223 conf.maxhfit <- not conf.maxhfit;
3224 state.maxy <- calcheight ();
3225 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3227 | 'c' ->
3228 conf.crophack <- not conf.crophack;
3229 TEdone ("crophack " ^ btos conf.crophack)
3231 | 'a' ->
3232 let s =
3233 match conf.maxwait with
3234 | None ->
3235 conf.maxwait <- Some infinity;
3236 "always wait for page to complete"
3237 | Some _ ->
3238 conf.maxwait <- None;
3239 "show placeholder if page is not ready"
3241 TEdone s
3243 | 'f' ->
3244 conf.underinfo <- not conf.underinfo;
3245 TEdone ("underinfo " ^ btos conf.underinfo)
3247 | 'P' ->
3248 conf.savebmarks <- not conf.savebmarks;
3249 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3251 | 'S' ->
3252 let ondone s =
3254 let pageno, py =
3255 match state.layout with
3256 | [] -> 0, 0
3257 | l :: _ ->
3258 l.pageno, l.pagey
3260 conf.interpagespace <- int_of_string s;
3261 docolumns conf.columns;
3262 state.maxy <- calcheight ();
3263 let y = getpagey pageno in
3264 gotoy (y + py)
3265 with exc ->
3266 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3268 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3270 | 'l' ->
3271 let fm =
3272 match conf.fitmodel with
3273 | FitProportional -> FitWidth
3274 | _ -> FitProportional
3276 reqlayout conf.angle fm;
3277 TEdone ("proportional display " ^ btos (fm == FitProportional))
3279 | 'T' ->
3280 settrim (not conf.trimmargins) conf.trimfuzz;
3281 TEdone ("trim margins " ^ btos conf.trimmargins)
3283 | 'I' ->
3284 conf.invert <- not conf.invert;
3285 TEdone ("invert colors " ^ btos conf.invert)
3287 | 'x' ->
3288 let ondone s =
3289 cbput state.hists.sel s;
3290 conf.selcmd <- s;
3292 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3293 textentry, ondone, true)
3295 | 'M' ->
3296 if conf.pax == None
3297 then conf.pax <- Some (ref (0.0, 0, 0))
3298 else conf.pax <- None;
3299 TEdone ("PAX " ^ btos (conf.pax != None))
3301 | _ ->
3302 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3303 TEstop
3304 else
3305 TEcont state.text
3308 class type lvsource = object
3309 method getitemcount : int
3310 method getitem : int -> (string * int)
3311 method hasaction : int -> bool
3312 method exit :
3313 uioh:uioh ->
3314 cancel:bool ->
3315 active:int ->
3316 first:int ->
3317 pan:int ->
3318 qsearch:string ->
3319 uioh option
3320 method getactive : int
3321 method getfirst : int
3322 method getqsearch : string
3323 method setqsearch : string -> unit
3324 method getpan : int
3325 end;;
3327 class virtual lvsourcebase = object
3328 val mutable m_active = 0
3329 val mutable m_first = 0
3330 val mutable m_qsearch = ""
3331 val mutable m_pan = 0
3332 method getactive = m_active
3333 method getfirst = m_first
3334 method getqsearch = m_qsearch
3335 method getpan = m_pan
3336 method setqsearch s = m_qsearch <- s
3337 end;;
3339 let withoutlastutf8 s =
3340 let len = String.length s in
3341 if len = 0
3342 then s
3343 else
3344 let rec find pos =
3345 if pos = 0
3346 then pos
3347 else
3348 let b = Char.code s.[pos] in
3349 if b land 0b11000000 = 0b11000000
3350 then pos
3351 else find (pos-1)
3353 let first =
3354 if Char.code s.[len-1] land 0x80 = 0
3355 then len-1
3356 else find (len-1)
3358 String.sub s 0 first;
3361 let textentrykeyboard
3362 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3363 let key =
3364 if key >= 0xffb0 && key <= 0xffb9
3365 then key - 0xffb0 + 48 else key
3367 let enttext te =
3368 state.mode <- Textentry (te, onleave);
3369 state.text <- "";
3370 enttext ();
3371 G.postRedisplay "textentrykeyboard enttext";
3373 let histaction cmd =
3374 match opthist with
3375 | None -> ()
3376 | Some (action, _) ->
3377 state.mode <- Textentry (
3378 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3380 G.postRedisplay "textentry histaction"
3382 match key with
3383 | 0xff08 -> (* backspace *)
3384 if emptystr text && cancelonempty
3385 then (
3386 onleave Cancel;
3387 G.postRedisplay "textentrykeyboard after cancel";
3389 else
3390 let s = withoutlastutf8 text in
3391 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3393 | 0xff0d | 0xff8d -> (* (kp) enter *)
3394 ondone text;
3395 onleave Confirm;
3396 G.postRedisplay "textentrykeyboard after confirm"
3398 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3399 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3400 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3401 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3403 | 0xff1b -> (* escape*)
3404 if emptystr text
3405 then (
3406 begin match opthist with
3407 | None -> ()
3408 | Some (_, onhistcancel) -> onhistcancel ()
3409 end;
3410 onleave Cancel;
3411 state.text <- "";
3412 G.postRedisplay "textentrykeyboard after cancel2"
3414 else (
3415 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3418 | 0xff9f | 0xffff -> () (* delete *)
3420 | _ when key != 0
3421 && key land 0xff00 != 0xff00 (* keyboard *)
3422 && key land 0xfe00 != 0xfe00 (* xkb *)
3423 && key land 0xfd00 != 0xfd00 (* 3270 *)
3425 begin match onkey text key with
3426 | TEdone text ->
3427 ondone text;
3428 onleave Confirm;
3429 G.postRedisplay "textentrykeyboard after confirm2";
3431 | TEcont text ->
3432 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3434 | TEstop ->
3435 onleave Cancel;
3436 G.postRedisplay "textentrykeyboard after cancel3"
3438 | TEswitch te ->
3439 state.mode <- Textentry (te, onleave);
3440 G.postRedisplay "textentrykeyboard switch";
3441 end;
3443 | _ ->
3444 vlog "unhandled key %s" (Wsi.keyname key)
3447 let firstof first active =
3448 if first > active || abs (first - active) > fstate.maxrows - 1
3449 then max 0 (active - (fstate.maxrows/2))
3450 else first
3453 let calcfirst first active =
3454 if active > first
3455 then
3456 let rows = active - first in
3457 if rows > fstate.maxrows then active - fstate.maxrows else first
3458 else active
3461 let scrollph y maxy =
3462 let sh = float (maxy + state.winh) /. float state.winh in
3463 let sh = float state.winh /. sh in
3464 let sh = max sh (float conf.scrollh) in
3466 let percent = float y /. float maxy in
3467 let position = (float state.winh -. sh) *. percent in
3469 let position =
3470 if position +. sh > float state.winh
3471 then float state.winh -. sh
3472 else position
3474 position, sh;
3477 let coe s = (s :> uioh);;
3479 class listview ~(source:lvsource) ~trusted ~modehash =
3480 object (self)
3481 val m_pan = source#getpan
3482 val m_first = source#getfirst
3483 val m_active = source#getactive
3484 val m_qsearch = source#getqsearch
3485 val m_prev_uioh = state.uioh
3487 method private elemunder y =
3488 let n = y / (fstate.fontsize+1) in
3489 if m_first + n < source#getitemcount
3490 then (
3491 if source#hasaction (m_first + n)
3492 then Some (m_first + n)
3493 else None
3495 else None
3497 method display =
3498 Gl.enable `blend;
3499 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3500 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3501 filledrect 0. 0. (float state.winw) (float state.winh);
3502 GlDraw.color (1., 1., 1.);
3503 Gl.enable `texture_2d;
3504 let fs = fstate.fontsize in
3505 let nfs = fs + 1 in
3506 let ww = fstate.wwidth in
3507 let tabw = 30.0*.ww in
3508 let itemcount = source#getitemcount in
3509 let rec loop row =
3510 if (row - m_first) > fstate.maxrows
3511 then ()
3512 else (
3513 if row >= 0 && row < itemcount
3514 then (
3515 let (s, level) = source#getitem row in
3516 let y = (row - m_first) * nfs in
3517 let x = 5.0 +. float (level + m_pan) *. ww in
3518 if row = m_active
3519 then (
3520 Gl.disable `texture_2d;
3521 let alpha = if source#hasaction row then 0.9 else 0.3 in
3522 GlDraw.color (1., 1., 1.) ~alpha;
3523 linerect 1. (float (y + 1))
3524 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3525 GlDraw.color (1., 1., 1.);
3526 Gl.enable `texture_2d;
3529 let drawtabularstring s =
3530 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3531 if trusted
3532 then
3533 let tabpos = try String.index s '\t' with Not_found -> -1 in
3534 if tabpos > 0
3535 then
3536 let len = String.length s - tabpos - 1 in
3537 let s1 = String.sub s 0 tabpos
3538 and s2 = String.sub s (tabpos + 1) len in
3539 let nx = drawstr x s1 in
3540 let sw = nx -. x in
3541 let x = x +. (max tabw sw) in
3542 drawstr x s2
3543 else
3544 drawstr x s
3545 else
3546 drawstr x s
3548 let _ = drawtabularstring s in
3549 loop (row+1)
3553 loop m_first;
3554 Gl.disable `blend;
3555 Gl.disable `texture_2d;
3557 method updownlevel incr =
3558 let len = source#getitemcount in
3559 let curlevel =
3560 if m_active >= 0 && m_active < len
3561 then snd (source#getitem m_active)
3562 else -1
3564 let rec flow i =
3565 if i = len then i-1 else if i = -1 then 0 else
3566 let _, l = source#getitem i in
3567 if l != curlevel then i else flow (i+incr)
3569 let active = flow m_active in
3570 let first = calcfirst m_first active in
3571 G.postRedisplay "outline updownlevel";
3572 {< m_active = active; m_first = first >}
3574 method private key1 key mask =
3575 let set1 active first qsearch =
3576 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3578 let search active pattern incr =
3579 let active = if active = -1 then m_first else active in
3580 let dosearch re =
3581 let rec loop n =
3582 if n >= 0 && n < source#getitemcount
3583 then (
3584 let s, _ = source#getitem n in
3586 (try ignore (Str.search_forward re s 0); true
3587 with Not_found -> false)
3588 then Some n
3589 else loop (n + incr)
3591 else None
3593 loop active
3596 let re = Str.regexp_case_fold pattern in
3597 dosearch re
3598 with Failure s ->
3599 state.text <- s;
3600 None
3602 let itemcount = source#getitemcount in
3603 let find start incr =
3604 let rec find i =
3605 if i = -1 || i = itemcount
3606 then -1
3607 else (
3608 if source#hasaction i
3609 then i
3610 else find (i + incr)
3613 find start
3615 let set active first =
3616 let first = bound first 0 (itemcount - fstate.maxrows) in
3617 state.text <- "";
3618 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3620 let navigate incr =
3621 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3622 let active, first =
3623 let incr1 = if incr > 0 then 1 else -1 in
3624 if isvisible m_first m_active
3625 then
3626 let next =
3627 let next = m_active + incr in
3628 let next =
3629 if next < 0 || next >= itemcount
3630 then -1
3631 else find next incr1
3633 if abs (m_active - next) > fstate.maxrows
3634 then -1
3635 else next
3637 if next = -1
3638 then
3639 let first = m_first + incr in
3640 let first = bound first 0 (itemcount - fstate.maxrows) in
3641 let next =
3642 let next = m_active + incr in
3643 let next = bound next 0 (itemcount - 1) in
3644 find next ~-incr1
3646 let active =
3647 if next = -1
3648 then m_active
3649 else (
3650 if isvisible first next
3651 then next
3652 else m_active
3655 active, first
3656 else
3657 let first = min next m_first in
3658 let first =
3659 if abs (next - first) > fstate.maxrows
3660 then first + incr
3661 else first
3663 next, first
3664 else
3665 let first = m_first + incr in
3666 let first = bound first 0 (itemcount - 1) in
3667 let active =
3668 let next = m_active + incr in
3669 let next = bound next 0 (itemcount - 1) in
3670 let next = find next incr1 in
3671 let active =
3672 if next = -1 || abs (m_active - first) > fstate.maxrows
3673 then (
3674 let active = if m_active = -1 then next else m_active in
3675 active
3677 else next
3679 if isvisible first active
3680 then active
3681 else -1
3683 active, first
3685 G.postRedisplay "listview navigate";
3686 set active first;
3688 match key with
3689 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3690 let incr = if key = 0x72 then -1 else 1 in
3691 let active, first =
3692 match search (m_active + incr) m_qsearch incr with
3693 | None ->
3694 state.text <- m_qsearch ^ " [not found]";
3695 m_active, m_first
3696 | Some active ->
3697 state.text <- m_qsearch;
3698 active, firstof m_first active
3700 G.postRedisplay "listview ctrl-r/s";
3701 set1 active first m_qsearch;
3703 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3704 if m_active >= 0 && m_active < source#getitemcount
3705 then (
3706 let s, _ = source#getitem m_active in
3707 selstring s;
3709 coe self
3711 | 0xff08 -> (* backspace *)
3712 if emptystr m_qsearch
3713 then coe self
3714 else (
3715 let qsearch = withoutlastutf8 m_qsearch in
3716 if emptystr qsearch
3717 then (
3718 state.text <- "";
3719 G.postRedisplay "listview empty qsearch";
3720 set1 m_active m_first "";
3722 else
3723 let active, first =
3724 match search m_active qsearch ~-1 with
3725 | None ->
3726 state.text <- qsearch ^ " [not found]";
3727 m_active, m_first
3728 | Some active ->
3729 state.text <- qsearch;
3730 active, firstof m_first active
3732 G.postRedisplay "listview backspace qsearch";
3733 set1 active first qsearch
3736 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3737 let pattern = m_qsearch ^ toutf8 key in
3738 let active, first =
3739 match search m_active pattern 1 with
3740 | None ->
3741 state.text <- pattern ^ " [not found]";
3742 m_active, m_first
3743 | Some active ->
3744 state.text <- pattern;
3745 active, firstof m_first active
3747 G.postRedisplay "listview qsearch add";
3748 set1 active first pattern;
3750 | 0xff1b -> (* escape *)
3751 state.text <- "";
3752 if emptystr m_qsearch
3753 then (
3754 G.postRedisplay "list view escape";
3755 begin
3756 match
3757 source#exit (coe self) true m_active m_first m_pan m_qsearch
3758 with
3759 | None -> m_prev_uioh
3760 | Some uioh -> uioh
3763 else (
3764 G.postRedisplay "list view kill qsearch";
3765 source#setqsearch "";
3766 coe {< m_qsearch = "" >}
3769 | 0xff0d | 0xff8d -> (* (kp) enter *)
3770 state.text <- "";
3771 let self = {< m_qsearch = "" >} in
3772 source#setqsearch "";
3773 let opt =
3774 G.postRedisplay "listview enter";
3775 if m_active >= 0 && m_active < source#getitemcount
3776 then (
3777 source#exit (coe self) false m_active m_first m_pan "";
3779 else (
3780 source#exit (coe self) true m_active m_first m_pan "";
3783 begin match opt with
3784 | None -> m_prev_uioh
3785 | Some uioh -> uioh
3788 | 0xff9f | 0xffff -> (* (kp) delete *)
3789 coe self
3791 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3792 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3793 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3794 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3796 | 0xff53 | 0xff98 -> (* (kp) right *)
3797 state.text <- "";
3798 G.postRedisplay "listview right";
3799 coe {< m_pan = m_pan - 1 >}
3801 | 0xff51 | 0xff96 -> (* (kp) left *)
3802 state.text <- "";
3803 G.postRedisplay "listview left";
3804 coe {< m_pan = m_pan + 1 >}
3806 | 0xff50 | 0xff95 -> (* (kp) home *)
3807 let active = find 0 1 in
3808 G.postRedisplay "listview home";
3809 set active 0;
3811 | 0xff57 | 0xff9c -> (* (kp) end *)
3812 let first = max 0 (itemcount - fstate.maxrows) in
3813 let active = find (itemcount - 1) ~-1 in
3814 G.postRedisplay "listview end";
3815 set active first;
3817 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3818 coe self
3820 | _ ->
3821 dolog "listview unknown key %#x" key; coe self
3823 method key key mask =
3824 match state.mode with
3825 | Textentry te -> textentrykeyboard key mask te; coe self
3826 | _ -> self#key1 key mask
3828 method button button down x y _ =
3829 let opt =
3830 match button with
3831 | 1 when x > state.winw - conf.scrollbw ->
3832 G.postRedisplay "listview scroll";
3833 if down
3834 then
3835 let _, position, sh = self#scrollph in
3836 if y > truncate position && y < truncate (position +. sh)
3837 then (
3838 state.mstate <- Mscrolly;
3839 Some (coe self)
3841 else
3842 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3843 let first = truncate (s *. float source#getitemcount) in
3844 let first = min source#getitemcount first in
3845 Some (coe {< m_first = first; m_active = first >})
3846 else (
3847 state.mstate <- Mnone;
3848 Some (coe self);
3850 | 1 when not down ->
3851 begin match self#elemunder y with
3852 | Some n ->
3853 G.postRedisplay "listview click";
3854 source#exit
3855 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3856 | _ ->
3857 Some (coe self)
3859 | n when (n == 4 || n == 5) && not down ->
3860 let len = source#getitemcount in
3861 let first =
3862 if n = 5 && m_first + fstate.maxrows >= len
3863 then
3864 m_first
3865 else
3866 let first = m_first + (if n == 4 then -1 else 1) in
3867 bound first 0 (len - 1)
3869 G.postRedisplay "listview wheel";
3870 Some (coe {< m_first = first >})
3871 | n when (n = 6 || n = 7) && not down ->
3872 let inc = if n = 7 then -1 else 1 in
3873 G.postRedisplay "listview hwheel";
3874 Some (coe {< m_pan = m_pan + inc >})
3875 | _ ->
3876 Some (coe self)
3878 match opt with
3879 | None -> m_prev_uioh
3880 | Some uioh -> uioh
3882 method motion _ y =
3883 match state.mstate with
3884 | Mscrolly ->
3885 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3886 let first = truncate (s *. float source#getitemcount) in
3887 let first = min source#getitemcount first in
3888 G.postRedisplay "listview motion";
3889 coe {< m_first = first; m_active = first >}
3890 | _ -> coe self
3892 method pmotion x y =
3893 if x < state.winw - conf.scrollbw
3894 then
3895 let n =
3896 match self#elemunder y with
3897 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3898 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3900 let o =
3901 if n != m_active
3902 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3903 else self
3905 coe o
3906 else (
3907 Wsi.setcursor Wsi.CURSOR_INHERIT;
3908 coe self
3911 method infochanged _ = ()
3913 method scrollpw = (0, 0.0, 0.0)
3914 method scrollph =
3915 let nfs = fstate.fontsize + 1 in
3916 let y = m_first * nfs in
3917 let itemcount = source#getitemcount in
3918 let maxi = max 0 (itemcount - fstate.maxrows) in
3919 let maxy = maxi * nfs in
3920 let p, h = scrollph y maxy in
3921 conf.scrollbw, p, h
3923 method modehash = modehash
3924 method eformsgs = false
3925 end;;
3927 class outlinelistview ~source =
3928 let settext autonarrow s =
3929 if autonarrow
3930 then state.text <- "[" ^ s ^ "]"
3931 else state.text <- s
3933 object (self)
3934 inherit listview
3935 ~source:(source :> lvsource)
3936 ~trusted:false
3937 ~modehash:(findkeyhash conf "outline")
3938 as super
3940 val m_autonarrow = false
3942 method key key mask =
3943 let maxrows =
3944 if emptystr state.text
3945 then fstate.maxrows
3946 else fstate.maxrows - 2
3948 let calcfirst first active =
3949 if active > first
3950 then
3951 let rows = active - first in
3952 if rows > maxrows then active - maxrows else first
3953 else active
3955 let navigate incr =
3956 let active = m_active + incr in
3957 let active = bound active 0 (source#getitemcount - 1) in
3958 let first = calcfirst m_first active in
3959 G.postRedisplay "outline navigate";
3960 coe {< m_active = active; m_first = first >}
3962 let navscroll first =
3963 let active =
3964 let dist = m_active - first in
3965 if dist < 0
3966 then first
3967 else (
3968 if dist < maxrows
3969 then m_active
3970 else first + maxrows
3973 G.postRedisplay "outline navscroll";
3974 coe {< m_first = first; m_active = active >}
3976 let ctrl = Wsi.withctrl mask in
3977 match key with
3978 | 97 when ctrl -> (* ctrl-a *)
3979 if m_autonarrow
3980 then source#denarrow
3981 else source#narrow m_qsearch;
3982 settext (not m_autonarrow) m_qsearch;
3983 G.postRedisplay "toggle auto narrowing";
3984 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3986 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3987 settext true "";
3988 G.postRedisplay "toggle auto narrowing";
3989 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3991 | 110 when ctrl -> (* ctrl-n *)
3992 source#narrow m_qsearch;
3993 if not m_autonarrow
3994 then source#add_narrow_pattern m_qsearch;
3995 G.postRedisplay "outline ctrl-n";
3996 coe {< m_first = 0; m_active = 0 >}
3998 | 115 when ctrl -> (* ctrl-s *)
3999 let active = source#calcactive (getanchor ()) in
4000 let first = firstof m_first active in
4001 G.postRedisplay "outline ctrl-s";
4002 coe {< m_first = first; m_active = active >}
4004 | 117 when ctrl -> (* ctrl-u *)
4005 source#del_narrow_pattern;
4006 let pattern = source#renarrow in
4007 G.postRedisplay "outline ctrl-u";
4008 let text =
4009 if emptystr pattern then "" else "Narrowed to " ^ pattern
4011 settext m_autonarrow text;
4012 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4014 | 108 when ctrl -> (* ctrl-l *)
4015 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4016 G.postRedisplay "outline ctrl-l";
4017 coe {< m_first = first >}
4019 | 0xff1b -> (* escape *)
4020 let o = super#key key mask in
4021 if m_autonarrow
4022 then (
4023 if nonemptystr m_qsearch
4024 then (
4025 source#add_narrow_pattern m_qsearch;
4026 settext true "";
4031 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4032 if nonemptystr m_qsearch
4033 then source#add_narrow_pattern m_qsearch;
4034 super#key key mask
4036 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4037 let pattern = m_qsearch ^ toutf8 key in
4038 G.postRedisplay "outlinelistview autonarrow add";
4039 source#narrow pattern;
4040 settext true pattern;
4041 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4043 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4044 if emptystr m_qsearch
4045 then coe self
4046 else
4047 let pattern = withoutlastutf8 m_qsearch in
4048 G.postRedisplay "outlinelistview autonarrow backspace";
4049 ignore (source#renarrow);
4050 source#narrow pattern;
4051 settext true pattern;
4052 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4054 | 0xff9f | 0xffff -> (* (kp) delete *)
4055 source#remove m_active;
4056 G.postRedisplay "outline delete";
4057 let active = max 0 (m_active-1) in
4058 coe {< m_first = firstof m_first active;
4059 m_active = active >}
4061 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4062 navscroll (max 0 (m_first - 1))
4064 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4065 navscroll (min (source#getitemcount - 1) (m_first + 1))
4067 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4068 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4069 | 0xff55 | 0xff9a -> (* (kp) prior *)
4070 navigate ~-(fstate.maxrows)
4071 | 0xff56 | 0xff9b -> (* (kp) next *)
4072 navigate fstate.maxrows
4074 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4075 let o =
4076 if ctrl
4077 then (
4078 G.postRedisplay "outline ctrl right";
4079 {< m_pan = m_pan + 1 >}
4081 else self#updownlevel 1
4083 coe o
4085 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4086 let o =
4087 if ctrl
4088 then (
4089 G.postRedisplay "outline ctrl left";
4090 {< m_pan = m_pan - 1 >}
4092 else self#updownlevel ~-1
4094 coe o
4096 | 0xff50 | 0xff95 -> (* (kp) home *)
4097 G.postRedisplay "outline home";
4098 coe {< m_first = 0; m_active = 0 >}
4100 | 0xff57 | 0xff9c -> (* (kp) end *)
4101 let active = source#getitemcount - 1 in
4102 let first = max 0 (active - fstate.maxrows) in
4103 G.postRedisplay "outline end";
4104 coe {< m_active = active; m_first = first >}
4106 | _ -> super#key key mask
4109 let gotounder under =
4110 let getpath filename =
4111 let path =
4112 if nonemptystr filename
4113 then
4114 if Filename.is_relative filename
4115 then
4116 let dir = Filename.dirname state.path in
4117 let dir =
4118 if Filename.is_implicit dir
4119 then Filename.concat (Sys.getcwd ()) dir
4120 else dir
4122 Filename.concat dir filename
4123 else filename
4124 else ""
4126 if Sys.file_exists path
4127 then path
4128 else ""
4130 match under with
4131 | Ulinkgoto (pageno, top) ->
4132 if pageno >= 0
4133 then (
4134 addnav ();
4135 gotopage1 pageno top;
4138 | Ulinkuri s ->
4139 gotouri s
4141 | Uremote (filename, pageno) ->
4142 let path = getpath filename in
4143 if nonemptystr path
4144 then (
4145 if conf.riani
4146 then
4147 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4148 try popen command []
4149 with exn ->
4150 Printf.eprintf
4151 "failed to execute `%s': %s\n" command (exntos exn);
4152 flush stderr;
4153 else
4154 let anchor = getanchor () in
4155 let ranchor = state.path, state.password, anchor, state.origin in
4156 state.origin <- "";
4157 state.anchor <- (pageno, 0.0, 0.0);
4158 state.ranchors <- ranchor :: state.ranchors;
4159 opendoc path "";
4161 else showtext '!' ("Could not find " ^ filename)
4163 | Uremotedest (filename, destname) ->
4164 let path = getpath filename in
4165 if nonemptystr path
4166 then (
4167 if conf.riani
4168 then
4169 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4170 try popen command []
4171 with exn ->
4172 Printf.eprintf
4173 "failed to execute `%s': %s\n" command (exntos exn);
4174 flush stderr;
4175 else
4176 let anchor = getanchor () in
4177 let ranchor = state.path, state.password, anchor, state.origin in
4178 state.origin <- "";
4179 state.nameddest <- destname;
4180 state.ranchors <- ranchor :: state.ranchors;
4181 opendoc path "";
4183 else showtext '!' ("Could not find " ^ filename)
4185 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4188 let gotooutline (_, _, kind) =
4189 match kind with
4190 | Onone -> ()
4191 | Oanchor anchor ->
4192 let (pageno, y, _) = anchor in
4193 let y = getanchory
4194 (if conf.presentation then (pageno, y, 1.0) else anchor)
4196 gotoghyll y
4197 | Ouri uri -> gotounder (Ulinkuri uri)
4198 | Olaunch cmd -> gotounder (Ulaunch cmd)
4199 | Oremote remote -> gotounder (Uremote remote)
4200 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4203 let outlinesource usebookmarks =
4204 let empty = [||] in
4205 (object (self)
4206 inherit lvsourcebase
4207 val mutable m_items = empty
4208 val mutable m_orig_items = empty
4209 val mutable m_narrow_patterns = []
4210 val mutable m_hadremovals = false
4212 method getitemcount =
4213 Array.length m_items + (if m_hadremovals then 1 else 0)
4215 method getitem n =
4216 if n == Array.length m_items && m_hadremovals
4217 then
4218 ("[Confirm removal]", 0)
4219 else
4220 let s, n, _ = m_items.(n) in
4221 (s, n)
4223 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4224 ignore (uioh, first, qsearch);
4225 let confrimremoval = m_hadremovals && active = Array.length m_items in
4226 let items =
4227 if m_narrow_patterns = []
4228 then m_orig_items
4229 else m_items
4231 if not cancel
4232 then (
4233 if not confrimremoval
4234 then (
4235 gotooutline m_items.(active);
4236 m_items <- items;
4238 else (
4239 state.bookmarks <- Array.to_list m_items;
4240 m_orig_items <- m_items;
4243 else m_items <- items;
4244 m_pan <- pan;
4245 None
4247 method hasaction _ = true
4249 method greetmsg =
4250 if Array.length m_items != Array.length m_orig_items
4251 then
4252 let s =
4253 match m_narrow_patterns with
4254 | one :: [] -> one
4255 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4257 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4258 else ""
4260 method narrow pattern =
4261 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4262 match reopt with
4263 | None -> ()
4264 | Some re ->
4265 let rec loop accu n =
4266 if n = -1
4267 then m_items <- Array.of_list accu
4268 else
4269 let (s, _, _) as o = m_items.(n) in
4270 let accu =
4271 if (try ignore (Str.search_forward re s 0); true
4272 with Not_found -> false)
4273 then o :: accu
4274 else accu
4276 loop accu (n-1)
4278 loop [] (Array.length m_items - 1)
4280 method denarrow =
4281 m_orig_items <- (
4282 if usebookmarks
4283 then Array.of_list state.bookmarks
4284 else state.outlines
4286 m_items <- m_orig_items
4288 method remove m =
4289 if usebookmarks
4290 then
4291 if m >= 0 && m < Array.length m_items
4292 then (
4293 m_hadremovals <- true;
4294 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4295 let n = if n >= m then n+1 else n in
4296 m_items.(n)
4300 method add_narrow_pattern pattern =
4301 m_narrow_patterns <- pattern :: m_narrow_patterns
4303 method del_narrow_pattern =
4304 match m_narrow_patterns with
4305 | _ :: rest -> m_narrow_patterns <- rest
4306 | [] -> ()
4308 method renarrow =
4309 self#denarrow;
4310 match m_narrow_patterns with
4311 | pattern :: [] -> self#narrow pattern; pattern
4312 | list ->
4313 List.fold_left (fun accu pattern ->
4314 self#narrow pattern;
4315 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4317 method calcactive anchor =
4318 let rely = getanchory anchor in
4319 let rec loop n best bestd =
4320 if n = Array.length m_items
4321 then best
4322 else
4323 let _, _, kind = m_items.(n) in
4324 match kind with
4325 | Oanchor anchor ->
4326 let orely = getanchory anchor in
4327 let d = abs (orely - rely) in
4328 if d < bestd
4329 then loop (n+1) n d
4330 else loop (n+1) best bestd
4331 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4332 loop (n+1) best bestd
4334 loop 0 ~-1 max_int
4336 method reset anchor items =
4337 m_hadremovals <- false;
4338 if m_orig_items == empty
4339 then (
4340 m_orig_items <- items;
4341 if m_narrow_patterns == []
4342 then m_items <- items;
4344 let active = self#calcactive anchor in
4345 m_active <- active;
4346 m_first <- firstof m_first active
4347 end)
4350 let enterselector usebookmarks =
4351 let source = outlinesource usebookmarks in
4352 fun errmsg ->
4353 let outlines =
4354 if usebookmarks
4355 then Array.of_list state.bookmarks
4356 else state.outlines
4358 if Array.length outlines = 0
4359 then (
4360 showtext ' ' errmsg;
4362 else (
4363 state.text <- source#greetmsg;
4364 Wsi.setcursor Wsi.CURSOR_INHERIT;
4365 let anchor = getanchor () in
4366 source#reset anchor outlines;
4367 state.uioh <- coe (new outlinelistview ~source);
4368 G.postRedisplay "enter selector";
4372 let enteroutlinemode =
4373 let f = enterselector false in
4374 fun ()-> f "Document has no outline";
4377 let enterbookmarkmode =
4378 let f = enterselector true in
4379 fun () -> f "Document has no bookmarks (yet)";
4382 let color_of_string s =
4383 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4384 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4388 let color_to_string (r, g, b) =
4389 let r = truncate (r *. 256.0)
4390 and g = truncate (g *. 256.0)
4391 and b = truncate (b *. 256.0) in
4392 Printf.sprintf "%d/%d/%d" r g b
4395 let irect_of_string s =
4396 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4399 let irect_to_string (x0,y0,x1,y1) =
4400 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4403 let makecheckers () =
4404 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4405 following to say:
4406 converted by Issac Trotts. July 25, 2002 *)
4407 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4408 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4409 let id = GlTex.gen_texture () in
4410 GlTex.bind_texture `texture_2d id;
4411 GlPix.store (`unpack_alignment 1);
4412 GlTex.image2d image;
4413 List.iter (GlTex.parameter ~target:`texture_2d)
4414 [ `mag_filter `nearest; `min_filter `nearest ];
4418 let setcheckers enabled =
4419 match state.texid with
4420 | None ->
4421 if enabled then state.texid <- Some (makecheckers ())
4423 | Some texid ->
4424 if not enabled
4425 then (
4426 GlTex.delete_texture texid;
4427 state.texid <- None;
4431 let int_of_string_with_suffix s =
4432 let l = String.length s in
4433 let s1, shift =
4434 if l > 1
4435 then
4436 let suffix = Char.lowercase s.[l-1] in
4437 match suffix with
4438 | 'k' -> String.sub s 0 (l-1), 10
4439 | 'm' -> String.sub s 0 (l-1), 20
4440 | 'g' -> String.sub s 0 (l-1), 30
4441 | _ -> s, 0
4442 else s, 0
4444 let n = int_of_string s1 in
4445 let m = n lsl shift in
4446 if m < 0 || m < n
4447 then raise (Failure "value too large")
4448 else m
4451 let string_with_suffix_of_int n =
4452 if n = 0
4453 then "0"
4454 else
4455 let units = [(30, "G"); (20, "M"); (10, "K")] in
4456 let prettyint n =
4457 let rec loop s n =
4458 let h = n mod 1000 in
4459 let n = n / 1000 in
4460 if n = 0
4461 then string_of_int h ^ s
4462 else (
4463 let s = Printf.sprintf "_%03d%s" h s in
4464 loop s n
4467 loop "" n
4469 let rec find = function
4470 | [] -> prettyint n
4471 | (shift, suffix) :: rest ->
4472 if (n land ((1 lsl shift) - 1)) = 0
4473 then prettyint (n lsr shift) ^ suffix
4474 else find rest
4476 find units
4479 let defghyllscroll = (40, 8, 32);;
4480 let fastghyllscroll = (5,1,2);;
4481 let neatghyllscroll = (10,1,9);;
4482 let ghyllscroll_of_string s =
4483 match s with
4484 | "default" -> Some defghyllscroll
4485 | "fast" -> Some (5,1,2)
4486 | "neat" -> Some (10,1,9)
4487 | "" | "none" -> None
4488 | _ ->
4489 let (n,a,b) as nab =
4490 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4491 if n <= a || n <= b || a >= b
4492 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4493 n a b;
4494 Some nab
4497 let ghyllscroll_to_string ((n, a, b) as nab) =
4498 (**) if nab = defghyllscroll then "default"
4499 else if nab = fastghyllscroll then "fast"
4500 else if nab = neatghyllscroll then "neat"
4501 else Printf.sprintf "%d,%d,%d" n a b;
4504 let describe_location () =
4505 let fn = page_of_y state.y in
4506 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4507 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4508 let percent =
4509 if maxy <= 0
4510 then 100.
4511 else (100. *. (float state.y /. float maxy))
4513 if fn = ln
4514 then
4515 Printf.sprintf "page %d of %d [%.2f%%]"
4516 (fn+1) state.pagecount percent
4517 else
4518 Printf.sprintf
4519 "pages %d-%d of %d [%.2f%%]"
4520 (fn+1) (ln+1) state.pagecount percent
4523 let setpresentationmode v =
4524 let n = page_of_y state.y in
4525 state.anchor <- (n, 0.0, 1.0);
4526 conf.presentation <- v;
4527 if conf.fitmodel = FitPage
4528 then reqlayout conf.angle conf.fitmodel;
4529 represent ();
4532 let enterinfomode =
4533 let btos b = if b then "\xe2\x88\x9a" else "" in
4534 let showextended = ref false in
4535 let leave mode = function
4536 | Confirm -> state.mode <- mode
4537 | Cancel -> state.mode <- mode in
4538 let src =
4539 (object
4540 val mutable m_first_time = true
4541 val mutable m_l = []
4542 val mutable m_a = [||]
4543 val mutable m_prev_uioh = nouioh
4544 val mutable m_prev_mode = View
4546 inherit lvsourcebase
4548 method reset prev_mode prev_uioh =
4549 m_a <- Array.of_list (List.rev m_l);
4550 m_l <- [];
4551 m_prev_mode <- prev_mode;
4552 m_prev_uioh <- prev_uioh;
4553 if m_first_time
4554 then (
4555 let rec loop n =
4556 if n >= Array.length m_a
4557 then ()
4558 else
4559 match m_a.(n) with
4560 | _, _, _, Action _ -> m_active <- n
4561 | _ -> loop (n+1)
4563 loop 0;
4564 m_first_time <- false;
4567 method int name get set =
4568 m_l <-
4569 (name, `int get, 1, Action (
4570 fun u ->
4571 let ondone s =
4572 try set (int_of_string s)
4573 with exn ->
4574 state.text <- Printf.sprintf "bad integer `%s': %s"
4575 s (exntos exn)
4577 state.text <- "";
4578 let te = name ^ ": ", "", None, intentry, ondone, true in
4579 state.mode <- Textentry (te, leave m_prev_mode);
4581 )) :: m_l
4583 method int_with_suffix name get set =
4584 m_l <-
4585 (name, `intws get, 1, Action (
4586 fun u ->
4587 let ondone s =
4588 try set (int_of_string_with_suffix s)
4589 with exn ->
4590 state.text <- Printf.sprintf "bad integer `%s': %s"
4591 s (exntos exn)
4593 state.text <- "";
4594 let te =
4595 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4597 state.mode <- Textentry (te, leave m_prev_mode);
4599 )) :: m_l
4601 method bool ?(offset=1) ?(btos=btos) name get set =
4602 m_l <-
4603 (name, `bool (btos, get), offset, Action (
4604 fun u ->
4605 let v = get () in
4606 set (not v);
4608 )) :: m_l
4610 method color name get set =
4611 m_l <-
4612 (name, `color get, 1, Action (
4613 fun u ->
4614 let invalid = (nan, nan, nan) in
4615 let ondone s =
4616 let c =
4617 try color_of_string s
4618 with exn ->
4619 state.text <- Printf.sprintf "bad color `%s': %s"
4620 s (exntos exn);
4621 invalid
4623 if c <> invalid
4624 then set c;
4626 let te = name ^ ": ", "", None, textentry, ondone, true in
4627 state.text <- color_to_string (get ());
4628 state.mode <- Textentry (te, leave m_prev_mode);
4630 )) :: m_l
4632 method string name get set =
4633 m_l <-
4634 (name, `string get, 1, Action (
4635 fun u ->
4636 let ondone s = set s in
4637 let te = name ^ ": ", "", None, textentry, ondone, true in
4638 state.mode <- Textentry (te, leave m_prev_mode);
4640 )) :: m_l
4642 method colorspace name get set =
4643 m_l <-
4644 (name, `string get, 1, Action (
4645 fun _ ->
4646 let source =
4647 (object
4648 inherit lvsourcebase
4650 initializer
4651 m_active <- CSTE.to_int conf.colorspace;
4652 m_first <- 0;
4654 method getitemcount =
4655 Array.length CSTE.names
4656 method getitem n =
4657 (CSTE.names.(n), 0)
4658 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4659 ignore (uioh, first, pan, qsearch);
4660 if not cancel then set active;
4661 None
4662 method hasaction _ = true
4663 end)
4665 state.text <- "";
4666 let modehash = findkeyhash conf "info" in
4667 coe (new listview ~source ~trusted:true ~modehash)
4668 )) :: m_l
4670 method paxmark name get set =
4671 m_l <-
4672 (name, `string get, 1, Action (
4673 fun _ ->
4674 let source =
4675 (object
4676 inherit lvsourcebase
4678 initializer
4679 m_active <- MTE.to_int conf.paxmark;
4680 m_first <- 0;
4682 method getitemcount = Array.length MTE.names
4683 method getitem n = (MTE.names.(n), 0)
4684 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4685 ignore (uioh, first, pan, qsearch);
4686 if not cancel then set active;
4687 None
4688 method hasaction _ = true
4689 end)
4691 state.text <- "";
4692 let modehash = findkeyhash conf "info" in
4693 coe (new listview ~source ~trusted:true ~modehash)
4694 )) :: m_l
4696 method fitmodel name get set =
4697 m_l <-
4698 (name, `string get, 1, Action (
4699 fun _ ->
4700 let source =
4701 (object
4702 inherit lvsourcebase
4704 initializer
4705 m_active <- FMTE.to_int conf.fitmodel;
4706 m_first <- 0;
4708 method getitemcount = Array.length FMTE.names
4709 method getitem n = (FMTE.names.(n), 0)
4710 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4711 ignore (uioh, first, pan, qsearch);
4712 if not cancel then set active;
4713 None
4714 method hasaction _ = true
4715 end)
4717 state.text <- "";
4718 let modehash = findkeyhash conf "info" in
4719 coe (new listview ~source ~trusted:true ~modehash)
4720 )) :: m_l
4722 method caption s offset =
4723 m_l <- (s, `empty, offset, Noaction) :: m_l
4725 method caption2 s f offset =
4726 m_l <- (s, `string f, offset, Noaction) :: m_l
4728 method getitemcount = Array.length m_a
4730 method getitem n =
4731 let tostr = function
4732 | `int f -> string_of_int (f ())
4733 | `intws f -> string_with_suffix_of_int (f ())
4734 | `string f -> f ()
4735 | `color f -> color_to_string (f ())
4736 | `bool (btos, f) -> btos (f ())
4737 | `empty -> ""
4739 let name, t, offset, _ = m_a.(n) in
4740 ((let s = tostr t in
4741 if nonemptystr s
4742 then Printf.sprintf "%s\t%s" name s
4743 else name),
4744 offset)
4746 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4747 let uiohopt =
4748 if not cancel
4749 then (
4750 m_qsearch <- qsearch;
4751 let uioh =
4752 match m_a.(active) with
4753 | _, _, _, Action f -> f uioh
4754 | _ -> uioh
4756 Some uioh
4758 else None
4760 m_active <- active;
4761 m_first <- first;
4762 m_pan <- pan;
4763 uiohopt
4765 method hasaction n =
4766 match m_a.(n) with
4767 | _, _, _, Action _ -> true
4768 | _ -> false
4769 end)
4771 let rec fillsrc prevmode prevuioh =
4772 let sep () = src#caption "" 0 in
4773 let colorp name get set =
4774 src#string name
4775 (fun () -> color_to_string (get ()))
4776 (fun v ->
4778 let c = color_of_string v in
4779 set c
4780 with exn ->
4781 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4784 let oldmode = state.mode in
4785 let birdseye = isbirdseye state.mode in
4787 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4789 src#bool "presentation mode"
4790 (fun () -> conf.presentation)
4791 (fun v -> setpresentationmode v);
4793 src#bool "ignore case in searches"
4794 (fun () -> conf.icase)
4795 (fun v -> conf.icase <- v);
4797 src#bool "preload"
4798 (fun () -> conf.preload)
4799 (fun v -> conf.preload <- v);
4801 src#bool "highlight links"
4802 (fun () -> conf.hlinks)
4803 (fun v -> conf.hlinks <- v);
4805 src#bool "under info"
4806 (fun () -> conf.underinfo)
4807 (fun v -> conf.underinfo <- v);
4809 src#bool "persistent bookmarks"
4810 (fun () -> conf.savebmarks)
4811 (fun v -> conf.savebmarks <- v);
4813 src#fitmodel "fit model"
4814 (fun () -> FMTE.to_string conf.fitmodel)
4815 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4817 src#bool "trim margins"
4818 (fun () -> conf.trimmargins)
4819 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4821 src#bool "persistent location"
4822 (fun () -> conf.jumpback)
4823 (fun v -> conf.jumpback <- v);
4825 sep ();
4826 src#int "inter-page space"
4827 (fun () -> conf.interpagespace)
4828 (fun n ->
4829 conf.interpagespace <- n;
4830 docolumns conf.columns;
4831 let pageno, py =
4832 match state.layout with
4833 | [] -> 0, 0
4834 | l :: _ ->
4835 l.pageno, l.pagey
4837 state.maxy <- calcheight ();
4838 let y = getpagey pageno in
4839 gotoy (y + py)
4842 src#int "page bias"
4843 (fun () -> conf.pagebias)
4844 (fun v -> conf.pagebias <- v);
4846 src#int "scroll step"
4847 (fun () -> conf.scrollstep)
4848 (fun n -> conf.scrollstep <- n);
4850 src#int "horizontal scroll step"
4851 (fun () -> conf.hscrollstep)
4852 (fun v -> conf.hscrollstep <- v);
4854 src#int "auto scroll step"
4855 (fun () ->
4856 match state.autoscroll with
4857 | Some step -> step
4858 | _ -> conf.autoscrollstep)
4859 (fun n ->
4860 let n = boundastep state.winh n in
4861 if state.autoscroll <> None
4862 then state.autoscroll <- Some n;
4863 conf.autoscrollstep <- n);
4865 src#int "zoom"
4866 (fun () -> truncate (conf.zoom *. 100.))
4867 (fun v -> setzoom ((float v) /. 100.));
4869 src#int "rotation"
4870 (fun () -> conf.angle)
4871 (fun v -> reqlayout v conf.fitmodel);
4873 src#int "scroll bar width"
4874 (fun () -> conf.scrollbw)
4875 (fun v ->
4876 conf.scrollbw <- v;
4877 reshape state.winw state.winh;
4880 src#int "scroll handle height"
4881 (fun () -> conf.scrollh)
4882 (fun v -> conf.scrollh <- v;);
4884 src#int "thumbnail width"
4885 (fun () -> conf.thumbw)
4886 (fun v ->
4887 conf.thumbw <- min 4096 v;
4888 match oldmode with
4889 | Birdseye beye ->
4890 leavebirdseye beye false;
4891 enterbirdseye ()
4892 | _ -> ()
4895 let mode = state.mode in
4896 src#string "columns"
4897 (fun () ->
4898 match conf.columns with
4899 | Csingle _ -> "1"
4900 | Cmulti (multi, _) -> multicolumns_to_string multi
4901 | Csplit (count, _) -> "-" ^ string_of_int count
4903 (fun v ->
4904 let n, a, b = multicolumns_of_string v in
4905 setcolumns mode n a b);
4907 sep ();
4908 src#caption "Pixmap cache" 0;
4909 src#int_with_suffix "size (advisory)"
4910 (fun () -> conf.memlimit)
4911 (fun v -> conf.memlimit <- v);
4913 src#caption2 "used"
4914 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4915 (string_with_suffix_of_int state.memused)
4916 (Hashtbl.length state.tilemap)) 1;
4918 sep ();
4919 src#caption "Layout" 0;
4920 src#caption2 "Dimension"
4921 (fun () ->
4922 Printf.sprintf "%dx%d (virtual %dx%d)"
4923 state.winw state.winh
4924 state.w state.maxy)
4926 if conf.debug
4927 then
4928 src#caption2 "Position" (fun () ->
4929 Printf.sprintf "%dx%d" state.x state.y
4931 else
4932 src#caption2 "Position" (fun () -> describe_location ()) 1
4935 sep ();
4936 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4937 "Save these parameters as global defaults at exit"
4938 (fun () -> conf.bedefault)
4939 (fun v -> conf.bedefault <- v)
4942 sep ();
4943 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4944 src#bool ~offset:0 ~btos "Extended parameters"
4945 (fun () -> !showextended)
4946 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4947 if !showextended
4948 then (
4949 src#bool "checkers"
4950 (fun () -> conf.checkers)
4951 (fun v -> conf.checkers <- v; setcheckers v);
4952 src#bool "update cursor"
4953 (fun () -> conf.updatecurs)
4954 (fun v -> conf.updatecurs <- v);
4955 src#bool "verbose"
4956 (fun () -> conf.verbose)
4957 (fun v -> conf.verbose <- v);
4958 src#bool "invert colors"
4959 (fun () -> conf.invert)
4960 (fun v -> conf.invert <- v);
4961 src#bool "max fit"
4962 (fun () -> conf.maxhfit)
4963 (fun v -> conf.maxhfit <- v);
4964 src#bool "redirect stderr"
4965 (fun () -> conf.redirectstderr)
4966 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4967 src#bool "pax mode"
4968 (fun () -> conf.pax != None)
4969 (fun v ->
4970 if v
4971 then conf.pax <- Some (ref (now (), 0, 0))
4972 else conf.pax <- None);
4973 src#string "uri launcher"
4974 (fun () -> conf.urilauncher)
4975 (fun v -> conf.urilauncher <- v);
4976 src#string "path launcher"
4977 (fun () -> conf.pathlauncher)
4978 (fun v -> conf.pathlauncher <- v);
4979 src#string "tile size"
4980 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4981 (fun v ->
4983 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4984 conf.tilew <- max 64 w;
4985 conf.tileh <- max 64 h;
4986 flushtiles ();
4987 with exn ->
4988 state.text <- Printf.sprintf "bad tile size `%s': %s"
4989 v (exntos exn)
4991 src#int "texture count"
4992 (fun () -> conf.texcount)
4993 (fun v ->
4994 if realloctexts v
4995 then conf.texcount <- v
4996 else showtext '!' " Failed to set texture count please retry later"
4998 src#int "slice height"
4999 (fun () -> conf.sliceheight)
5000 (fun v ->
5001 conf.sliceheight <- v;
5002 wcmd "sliceh %d" conf.sliceheight;
5004 src#int "anti-aliasing level"
5005 (fun () -> conf.aalevel)
5006 (fun v ->
5007 conf.aalevel <- bound v 0 8;
5008 state.anchor <- getanchor ();
5009 opendoc state.path state.password;
5011 src#string "page scroll scaling factor"
5012 (fun () -> string_of_float conf.pgscale)
5013 (fun v ->
5015 let s = float_of_string v in
5016 conf.pgscale <- s
5017 with exn ->
5018 state.text <- Printf.sprintf
5019 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5022 src#int "ui font size"
5023 (fun () -> fstate.fontsize)
5024 (fun v -> setfontsize (bound v 5 100));
5025 src#int "hint font size"
5026 (fun () -> conf.hfsize)
5027 (fun v -> conf.hfsize <- bound v 5 100);
5028 colorp "background color"
5029 (fun () -> conf.bgcolor)
5030 (fun v -> conf.bgcolor <- v);
5031 src#bool "crop hack"
5032 (fun () -> conf.crophack)
5033 (fun v -> conf.crophack <- v);
5034 src#string "trim fuzz"
5035 (fun () -> irect_to_string conf.trimfuzz)
5036 (fun v ->
5038 conf.trimfuzz <- irect_of_string v;
5039 if conf.trimmargins
5040 then settrim true conf.trimfuzz;
5041 with exn ->
5042 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5044 src#string "throttle"
5045 (fun () ->
5046 match conf.maxwait with
5047 | None -> "show place holder if page is not ready"
5048 | Some time ->
5049 if time = infinity
5050 then "wait for page to fully render"
5051 else
5052 "wait " ^ string_of_float time
5053 ^ " seconds before showing placeholder"
5055 (fun v ->
5057 let f = float_of_string v in
5058 if f <= 0.0
5059 then conf.maxwait <- None
5060 else conf.maxwait <- Some f
5061 with exn ->
5062 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5064 src#string "ghyll scroll"
5065 (fun () ->
5066 match conf.ghyllscroll with
5067 | None -> ""
5068 | Some nab -> ghyllscroll_to_string nab
5070 (fun v ->
5071 try conf.ghyllscroll <- ghyllscroll_of_string v
5072 with exn ->
5073 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5075 src#string "selection command"
5076 (fun () -> conf.selcmd)
5077 (fun v -> conf.selcmd <- v);
5078 src#string "synctex command"
5079 (fun () -> conf.stcmd)
5080 (fun v -> conf.stcmd <- v);
5081 src#string "pax command"
5082 (fun () -> conf.paxcmd)
5083 (fun v -> conf.paxcmd <- v);
5084 src#colorspace "color space"
5085 (fun () -> CSTE.to_string conf.colorspace)
5086 (fun v ->
5087 conf.colorspace <- CSTE.of_int v;
5088 wcmd "cs %d" v;
5089 load state.layout;
5091 src#paxmark "pax mark method"
5092 (fun () -> MTE.to_string conf.paxmark)
5093 (fun v -> conf.paxmark <- MTE.of_int v);
5094 if pbousable ()
5095 then
5096 src#bool "use PBO"
5097 (fun () -> conf.usepbo)
5098 (fun v -> conf.usepbo <- v);
5099 src#bool "mouse wheel scrolls pages"
5100 (fun () -> conf.wheelbypage)
5101 (fun v -> conf.wheelbypage <- v);
5102 src#bool "open remote links in a new instance"
5103 (fun () -> conf.riani)
5104 (fun v -> conf.riani <- v);
5107 sep ();
5108 src#caption "Document" 0;
5109 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5110 src#caption2 "Pages"
5111 (fun () -> string_of_int state.pagecount) 1;
5112 src#caption2 "Dimensions"
5113 (fun () -> string_of_int (List.length state.pdims)) 1;
5114 if conf.trimmargins
5115 then (
5116 sep ();
5117 src#caption "Trimmed margins" 0;
5118 src#caption2 "Dimensions"
5119 (fun () -> string_of_int (List.length state.pdims)) 1;
5122 sep ();
5123 src#caption "OpenGL" 0;
5124 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5125 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5127 sep ();
5128 src#caption "Location" 0;
5129 if nonemptystr state.origin
5130 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5131 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5133 src#reset prevmode prevuioh;
5135 fun () ->
5136 state.text <- "";
5137 let prevmode = state.mode
5138 and prevuioh = state.uioh in
5139 fillsrc prevmode prevuioh;
5140 let source = (src :> lvsource) in
5141 let modehash = findkeyhash conf "info" in
5142 state.uioh <- coe (object (self)
5143 inherit listview ~source ~trusted:true ~modehash as super
5144 val mutable m_prevmemused = 0
5145 method infochanged = function
5146 | Memused ->
5147 if m_prevmemused != state.memused
5148 then (
5149 m_prevmemused <- state.memused;
5150 G.postRedisplay "memusedchanged";
5152 | Pdim -> G.postRedisplay "pdimchanged"
5153 | Docinfo -> fillsrc prevmode prevuioh
5155 method key key mask =
5156 if not (Wsi.withctrl mask)
5157 then
5158 match key with
5159 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5160 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5161 | _ -> super#key key mask
5162 else super#key key mask
5163 end);
5164 G.postRedisplay "info";
5167 let enterhelpmode =
5168 let source =
5169 (object
5170 inherit lvsourcebase
5171 method getitemcount = Array.length state.help
5172 method getitem n =
5173 let s, l, _ = state.help.(n) in
5174 (s, l)
5176 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5177 let optuioh =
5178 if not cancel
5179 then (
5180 m_qsearch <- qsearch;
5181 match state.help.(active) with
5182 | _, _, Action f -> Some (f uioh)
5183 | _ -> Some (uioh)
5185 else None
5187 m_active <- active;
5188 m_first <- first;
5189 m_pan <- pan;
5190 optuioh
5192 method hasaction n =
5193 match state.help.(n) with
5194 | _, _, Action _ -> true
5195 | _ -> false
5197 initializer
5198 m_active <- -1
5199 end)
5200 in fun () ->
5201 let modehash = findkeyhash conf "help" in
5202 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5203 G.postRedisplay "help";
5206 let entermsgsmode =
5207 let msgsource =
5208 let re = Str.regexp "[\r\n]" in
5209 (object
5210 inherit lvsourcebase
5211 val mutable m_items = [||]
5213 method getitemcount = 1 + Array.length m_items
5215 method getitem n =
5216 if n = 0
5217 then "[Clear]", 0
5218 else m_items.(n-1), 0
5220 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5221 ignore uioh;
5222 if not cancel
5223 then (
5224 if active = 0
5225 then Buffer.clear state.errmsgs;
5226 m_qsearch <- qsearch;
5228 m_active <- active;
5229 m_first <- first;
5230 m_pan <- pan;
5231 None
5233 method hasaction n =
5234 n = 0
5236 method reset =
5237 state.newerrmsgs <- false;
5238 let l = Str.split re (Buffer.contents state.errmsgs) in
5239 m_items <- Array.of_list l
5241 initializer
5242 m_active <- 0
5243 end)
5244 in fun () ->
5245 state.text <- "";
5246 msgsource#reset;
5247 let source = (msgsource :> lvsource) in
5248 let modehash = findkeyhash conf "listview" in
5249 state.uioh <- coe (object
5250 inherit listview ~source ~trusted:false ~modehash as super
5251 method display =
5252 if state.newerrmsgs
5253 then msgsource#reset;
5254 super#display
5255 end);
5256 G.postRedisplay "msgs";
5259 let quickbookmark ?title () =
5260 match state.layout with
5261 | [] -> ()
5262 | l :: _ ->
5263 let title =
5264 match title with
5265 | None ->
5266 let sec = Unix.gettimeofday () in
5267 let tm = Unix.localtime sec in
5268 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5269 (l.pageno+1)
5270 tm.Unix.tm_mday
5271 tm.Unix.tm_mon
5272 (tm.Unix.tm_year + 1900)
5273 tm.Unix.tm_hour
5274 tm.Unix.tm_min
5275 | Some title -> title
5277 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5280 let setautoscrollspeed step goingdown =
5281 let incr = max 1 ((abs step) / 2) in
5282 let incr = if goingdown then incr else -incr in
5283 let astep = boundastep state.winh (step + incr) in
5284 state.autoscroll <- Some astep;
5287 let canpan () =
5288 match conf.columns with
5289 | Csplit _ -> true
5290 | _ -> state.x != 0 || conf.zoom > 1.0
5293 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5295 let existsinrow pageno (columns, coverA, coverB) p =
5296 let last = ((pageno - coverA) mod columns) + columns in
5297 let rec any = function
5298 | [] -> false
5299 | l :: rest ->
5300 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5301 then p l
5302 else (
5303 if not (p l)
5304 then (if l.pageno = last then false else any rest)
5305 else true
5308 any state.layout
5311 let nextpage () =
5312 match state.layout with
5313 | [] ->
5314 let pageno = page_of_y state.y in
5315 gotoghyll (getpagey (pageno+1))
5316 | l :: rest ->
5317 match conf.columns with
5318 | Csingle _ ->
5319 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5320 then
5321 let y = clamp (pgscale state.winh) in
5322 gotoghyll y
5323 else
5324 let pageno = min (l.pageno+1) (state.pagecount-1) in
5325 gotoghyll (getpagey pageno)
5326 | Cmulti ((c, _, _) as cl, _) ->
5327 if conf.presentation
5328 && (existsinrow l.pageno cl
5329 (fun l -> l.pageh > l.pagey + l.pagevh))
5330 then
5331 let y = clamp (pgscale state.winh) in
5332 gotoghyll y
5333 else
5334 let pageno = min (l.pageno+c) (state.pagecount-1) in
5335 gotoghyll (getpagey pageno)
5336 | Csplit (n, _) ->
5337 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5338 then
5339 let pagey, pageh = getpageyh l.pageno in
5340 let pagey = pagey + pageh * l.pagecol in
5341 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5342 gotoghyll (pagey + pageh + ips)
5345 let prevpage () =
5346 match state.layout with
5347 | [] ->
5348 let pageno = page_of_y state.y in
5349 gotoghyll (getpagey (pageno-1))
5350 | l :: _ ->
5351 match conf.columns with
5352 | Csingle _ ->
5353 if conf.presentation && l.pagey != 0
5354 then
5355 gotoghyll (clamp (pgscale ~-(state.winh)))
5356 else
5357 let pageno = max 0 (l.pageno-1) in
5358 gotoghyll (getpagey pageno)
5359 | Cmulti ((c, _, coverB) as cl, _) ->
5360 if conf.presentation &&
5361 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5362 then
5363 gotoghyll (clamp (pgscale ~-(state.winh)))
5364 else
5365 let decr =
5366 if l.pageno = state.pagecount - coverB
5367 then 1
5368 else c
5370 let pageno = max 0 (l.pageno-decr) in
5371 gotoghyll (getpagey pageno)
5372 | Csplit (n, _) ->
5373 let y =
5374 if l.pagecol = 0
5375 then
5376 if l.pageno = 0
5377 then l.pagey
5378 else
5379 let pageno = max 0 (l.pageno-1) in
5380 let pagey, pageh = getpageyh pageno in
5381 pagey + (n-1)*pageh
5382 else
5383 let pagey, pageh = getpageyh l.pageno in
5384 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5386 gotoghyll y
5389 let viewkeyboard key mask =
5390 let enttext te =
5391 let mode = state.mode in
5392 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5393 state.text <- "";
5394 enttext ();
5395 G.postRedisplay "view:enttext"
5397 let ctrl = Wsi.withctrl mask in
5398 let key =
5399 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5401 match key with
5402 | 81 -> (* Q *)
5403 exit 0
5405 | 0xff63 -> (* insert *)
5406 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5407 then (
5408 state.mode <- LinkNav (Ltgendir 0);
5409 gotoy state.y;
5411 else showtext '!' "Keyboard link navigation does not work under rotation"
5413 | 0xff1b | 113 -> (* escape / q *)
5414 begin match state.mstate with
5415 | Mzoomrect _ ->
5416 state.mstate <- Mnone;
5417 Wsi.setcursor Wsi.CURSOR_INHERIT;
5418 G.postRedisplay "kill zoom rect";
5419 | _ ->
5420 begin match state.mode with
5421 | LinkNav _ ->
5422 state.mode <- View;
5423 G.postRedisplay "esc leave linknav"
5424 | _ ->
5425 match state.ranchors with
5426 | [] -> raise Quit
5427 | (path, password, anchor, origin) :: rest ->
5428 state.ranchors <- rest;
5429 state.anchor <- anchor;
5430 state.origin <- origin;
5431 state.nameddest <- "";
5432 opendoc path password
5433 end;
5434 end;
5436 | 0xff08 -> (* backspace *)
5437 gotoghyll (getnav ~-1)
5439 | 111 -> (* o *)
5440 enteroutlinemode ()
5442 | 117 -> (* u *)
5443 state.rects <- [];
5444 state.text <- "";
5445 G.postRedisplay "dehighlight";
5447 | 47 | 63 -> (* / ? *)
5448 let ondone isforw s =
5449 cbput state.hists.pat s;
5450 state.searchpattern <- s;
5451 search s isforw
5453 let s = String.create 1 in
5454 s.[0] <- Char.chr key;
5455 enttext (s, "", Some (onhist state.hists.pat),
5456 textentry, ondone (key = 47), true)
5458 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5459 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5460 setzoom (conf.zoom +. incr)
5462 | 43 | 0xffab -> (* + *)
5463 let ondone s =
5464 let n =
5465 try int_of_string s with exc ->
5466 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5467 max_int
5469 if n != max_int
5470 then (
5471 conf.pagebias <- n;
5472 state.text <- "page bias is now " ^ string_of_int n;
5475 enttext ("page bias: ", "", None, intentry, ondone, true)
5477 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5478 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5479 setzoom (max 0.01 (conf.zoom -. decr))
5481 | 45 | 0xffad -> (* - *)
5482 let ondone msg = state.text <- msg in
5483 enttext (
5484 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5485 optentry state.mode, ondone, true
5488 | 48 when ctrl -> (* ctrl-0 *)
5489 if conf.zoom = 1.0
5490 then (
5491 state.x <- 0;
5492 gotoy state.y
5494 else setzoom 1.0
5496 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5497 let cols =
5498 match conf.columns with
5499 | Csingle _ | Cmulti _ -> 1
5500 | Csplit (n, _) -> n
5502 let h = state.winh -
5503 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5505 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5506 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5507 then setzoom zoom
5509 | 51 when ctrl -> (* ctrl-3 *)
5510 let fm =
5511 match conf.fitmodel with
5512 | FitWidth -> FitProportional
5513 | FitProportional -> FitPage
5514 | FitPage -> FitWidth
5516 state.text <- "fit model: " ^ FMTE.to_string fm;
5517 reqlayout conf.angle fm
5519 | 0xffc6 -> (* f9 *)
5520 togglebirdseye ()
5522 | 57 when ctrl -> (* ctrl-9 *)
5523 togglebirdseye ()
5525 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5526 when not ctrl -> (* 0..9 *)
5527 let ondone s =
5528 let n =
5529 try int_of_string s with exc ->
5530 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5533 if n >= 0
5534 then (
5535 addnav ();
5536 cbput state.hists.pag (string_of_int n);
5537 gotopage1 (n + conf.pagebias - 1) 0;
5540 let pageentry text key =
5541 match Char.unsafe_chr key with
5542 | 'g' -> TEdone text
5543 | _ -> intentry text key
5545 let text = "x" in text.[0] <- Char.chr key;
5546 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5548 | 98 -> (* b *)
5549 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5550 reshape state.winw state.winh;
5552 | 66 -> (* B *)
5553 state.bzoom <- not state.bzoom;
5554 state.rects <- [];
5555 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5557 | 108 -> (* l *)
5558 conf.hlinks <- not conf.hlinks;
5559 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5560 G.postRedisplay "toggle highlightlinks";
5562 | 70 -> (* F *)
5563 state.glinks <- true;
5564 let mode = state.mode in
5565 state.mode <- Textentry (
5566 (":", "", None, linknentry, linkndone gotounder, false),
5567 (fun _ ->
5568 state.glinks <- false;
5569 state.mode <- mode)
5571 state.text <- "";
5572 G.postRedisplay "view:linkent(F)"
5574 | 121 -> (* y *)
5575 state.glinks <- true;
5576 let mode = state.mode in
5577 state.mode <- Textentry (
5579 ":", "", None, linknentry, linkndone (fun under ->
5580 selstring (undertext under);
5581 ), false
5583 fun _ ->
5584 state.glinks <- false;
5585 state.mode <- mode
5587 state.text <- "";
5588 G.postRedisplay "view:linkent"
5590 | 97 -> (* a *)
5591 begin match state.autoscroll with
5592 | Some step ->
5593 conf.autoscrollstep <- step;
5594 state.autoscroll <- None
5595 | None ->
5596 if conf.autoscrollstep = 0
5597 then state.autoscroll <- Some 1
5598 else state.autoscroll <- Some conf.autoscrollstep
5601 | 112 when ctrl -> (* ctrl-p *)
5602 launchpath ()
5604 | 80 -> (* P *)
5605 setpresentationmode (not conf.presentation);
5606 showtext ' ' ("presentation mode " ^
5607 if conf.presentation then "on" else "off");
5609 | 102 -> (* f *)
5610 if List.mem Wsi.Fullscreen state.winstate
5611 then Wsi.reshape conf.cwinw conf.cwinh
5612 else Wsi.fullscreen ()
5614 | 112 | 78 -> (* p|N *)
5615 search state.searchpattern false
5617 | 110 | 0xffc0 -> (* n|F3 *)
5618 search state.searchpattern true
5620 | 116 -> (* t *)
5621 begin match state.layout with
5622 | [] -> ()
5623 | l :: _ ->
5624 gotoghyll (getpagey l.pageno)
5627 | 32 -> (* space *)
5628 nextpage ()
5630 | 0xff9f | 0xffff -> (* delete *)
5631 prevpage ()
5633 | 61 -> (* = *)
5634 showtext ' ' (describe_location ());
5636 | 119 -> (* w *)
5637 begin match state.layout with
5638 | [] -> ()
5639 | l :: _ ->
5640 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5641 G.postRedisplay "w"
5644 | 39 -> (* ' *)
5645 enterbookmarkmode ()
5647 | 104 | 0xffbe -> (* h|F1 *)
5648 enterhelpmode ()
5650 | 105 -> (* i *)
5651 enterinfomode ()
5653 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5654 entermsgsmode ()
5656 | 109 -> (* m *)
5657 let ondone s =
5658 match state.layout with
5659 | l :: _ ->
5660 if nonemptystr s
5661 then
5662 state.bookmarks <-
5663 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5664 | _ -> ()
5666 enttext ("bookmark: ", "", None, textentry, ondone, true)
5668 | 126 -> (* ~ *)
5669 quickbookmark ();
5670 showtext ' ' "Quick bookmark added";
5672 | 122 -> (* z *)
5673 begin match state.layout with
5674 | l :: _ ->
5675 let rect = getpdimrect l.pagedimno in
5676 let w, h =
5677 if conf.crophack
5678 then
5679 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5680 truncate (1.2 *. (rect.(3) -. rect.(0))))
5681 else
5682 (truncate (rect.(1) -. rect.(0)),
5683 truncate (rect.(3) -. rect.(0)))
5685 let w = truncate ((float w)*.conf.zoom)
5686 and h = truncate ((float h)*.conf.zoom) in
5687 if w != 0 && h != 0
5688 then (
5689 state.anchor <- getanchor ();
5690 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5692 G.postRedisplay "z";
5694 | [] -> ()
5697 | 120 -> state.roam () (* x *)
5698 | 60 | 62 -> (* < > *)
5699 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5701 | 91 | 93 -> (* [ ] *)
5702 conf.colorscale <-
5703 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5705 G.postRedisplay "brightness";
5707 | 99 when state.mode = View -> (* [alt-]c *)
5708 if Wsi.withalt mask
5709 then (
5710 if conf.zoom > 1.0
5711 then
5712 let m = (wadjsb state.winw - state.w) / 2 in
5713 state.x <- m;
5714 gotoy_and_clear_text state.y
5716 else
5717 let (c, a, b), z =
5718 match state.prevcolumns with
5719 | None -> (1, 0, 0), 1.0
5720 | Some (columns, z) ->
5721 let cab =
5722 match columns with
5723 | Csplit (c, _) -> -c, 0, 0
5724 | Cmulti ((c, a, b), _) -> c, a, b
5725 | Csingle _ -> 1, 0, 0
5727 cab, z
5729 setcolumns View c a b;
5730 setzoom z
5732 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5733 -> (* ctrl-shift- (kp) [up|down] *)
5734 let zoom, x = state.prevzoom in
5735 setzoom zoom;
5736 state.x <- x;
5738 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5739 begin match state.autoscroll with
5740 | None ->
5741 begin match state.mode with
5742 | Birdseye beye -> upbirdseye 1 beye
5743 | _ ->
5744 if ctrl
5745 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5746 else (
5747 if not (Wsi.withshift mask) && conf.presentation
5748 then prevpage ()
5749 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5752 | Some n ->
5753 setautoscrollspeed n false
5756 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5757 begin match state.autoscroll with
5758 | None ->
5759 begin match state.mode with
5760 | Birdseye beye -> downbirdseye 1 beye
5761 | _ ->
5762 if ctrl
5763 then gotoy_and_clear_text (clamp (state.winh/2))
5764 else (
5765 if not (Wsi.withshift mask) && conf.presentation
5766 then nextpage ()
5767 else gotoy_and_clear_text (clamp conf.scrollstep)
5770 | Some n ->
5771 setautoscrollspeed n true
5774 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5775 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5776 if canpan ()
5777 then
5778 let dx =
5779 if ctrl
5780 then state.winw / 2
5781 else conf.hscrollstep
5783 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5784 state.x <- panbound (state.x + dx);
5785 gotoy_and_clear_text state.y
5786 else (
5787 state.text <- "";
5788 G.postRedisplay "left/right"
5791 | 0xff55 | 0xff9a -> (* (kp) prior *)
5792 let y =
5793 if ctrl
5794 then
5795 match state.layout with
5796 | [] -> state.y
5797 | l :: _ -> state.y - l.pagey
5798 else
5799 clamp (pgscale (-state.winh))
5801 gotoghyll y
5803 | 0xff56 | 0xff9b -> (* (kp) next *)
5804 let y =
5805 if ctrl
5806 then
5807 match List.rev state.layout with
5808 | [] -> state.y
5809 | l :: _ -> getpagey l.pageno
5810 else
5811 clamp (pgscale state.winh)
5813 gotoghyll y
5815 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5816 gotoghyll 0
5817 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5818 gotoghyll (clamp state.maxy)
5820 | 0xff53 | 0xff98
5821 when Wsi.withalt mask -> (* alt-(kp) right *)
5822 gotoghyll (getnav 1)
5823 | 0xff51 | 0xff96
5824 when Wsi.withalt mask -> (* alt-(kp) left *)
5825 gotoghyll (getnav ~-1)
5827 | 114 -> (* r *)
5828 reload ()
5830 | 118 when conf.debug -> (* v *)
5831 state.rects <- [];
5832 List.iter (fun l ->
5833 match getopaque l.pageno with
5834 | None -> ()
5835 | Some opaque ->
5836 let x0, y0, x1, y1 = pagebbox opaque in
5837 let a,b = float x0, float y0 in
5838 let c,d = float x1, float y0 in
5839 let e,f = float x1, float y1 in
5840 let h,j = float x0, float y1 in
5841 let rect = (a,b,c,d,e,f,h,j) in
5842 debugrect rect;
5843 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5844 ) state.layout;
5845 G.postRedisplay "v";
5847 | _ ->
5848 vlog "huh? %s" (Wsi.keyname key)
5851 let linknavkeyboard key mask linknav =
5852 let getpage pageno =
5853 let rec loop = function
5854 | [] -> None
5855 | l :: _ when l.pageno = pageno -> Some l
5856 | _ :: rest -> loop rest
5857 in loop state.layout
5859 let doexact (pageno, n) =
5860 match getopaque pageno, getpage pageno with
5861 | Some opaque, Some l ->
5862 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5863 then
5864 let under = getlink opaque n in
5865 G.postRedisplay "link gotounder";
5866 gotounder under;
5867 state.mode <- View;
5868 else
5869 let opt, dir =
5870 match key with
5871 | 0xff50 -> (* home *)
5872 Some (findlink opaque LDfirst), -1
5874 | 0xff57 -> (* end *)
5875 Some (findlink opaque LDlast), 1
5877 | 0xff51 -> (* left *)
5878 Some (findlink opaque (LDleft n)), -1
5880 | 0xff53 -> (* right *)
5881 Some (findlink opaque (LDright n)), 1
5883 | 0xff52 -> (* up *)
5884 Some (findlink opaque (LDup n)), -1
5886 | 0xff54 -> (* down *)
5887 Some (findlink opaque (LDdown n)), 1
5889 | _ -> None, 0
5891 let pwl l dir =
5892 begin match findpwl l.pageno dir with
5893 | Pwlnotfound -> ()
5894 | Pwl pageno ->
5895 let notfound dir =
5896 state.mode <- LinkNav (Ltgendir dir);
5897 let y, h = getpageyh pageno in
5898 let y =
5899 if dir < 0
5900 then y + h - state.winh
5901 else y
5903 gotoy y
5905 begin match getopaque pageno, getpage pageno with
5906 | Some opaque, Some _ ->
5907 let link =
5908 let ld = if dir > 0 then LDfirst else LDlast in
5909 findlink opaque ld
5911 begin match link with
5912 | Lfound m ->
5913 showlinktype (getlink opaque m);
5914 state.mode <- LinkNav (Ltexact (pageno, m));
5915 G.postRedisplay "linknav jpage";
5916 | _ -> notfound dir
5917 end;
5918 | _ -> notfound dir
5919 end;
5920 end;
5922 begin match opt with
5923 | Some Lnotfound -> pwl l dir;
5924 | Some (Lfound m) ->
5925 if m = n
5926 then pwl l dir
5927 else (
5928 let _, y0, _, y1 = getlinkrect opaque m in
5929 if y0 < l.pagey
5930 then gotopage1 l.pageno y0
5931 else (
5932 let d = fstate.fontsize + 1 in
5933 if y1 - l.pagey > l.pagevh - d
5934 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5935 else G.postRedisplay "linknav";
5937 showlinktype (getlink opaque m);
5938 state.mode <- LinkNav (Ltexact (l.pageno, m));
5941 | None -> viewkeyboard key mask
5942 end;
5943 | _ -> viewkeyboard key mask
5945 if key = 0xff63
5946 then (
5947 state.mode <- View;
5948 G.postRedisplay "leave linknav"
5950 else
5951 match linknav with
5952 | Ltgendir _ -> viewkeyboard key mask
5953 | Ltexact exact -> doexact exact
5956 let keyboard key mask =
5957 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5958 then wcmd "interrupt"
5959 else state.uioh <- state.uioh#key key mask
5962 let birdseyekeyboard key mask
5963 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5964 let incr =
5965 match conf.columns with
5966 | Csingle _ -> 1
5967 | Cmulti ((c, _, _), _) -> c
5968 | Csplit _ -> failwith "bird's eye split mode"
5970 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5971 match key with
5972 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5973 let y, h = getpageyh pageno in
5974 let top = (state.winh - h) / 2 in
5975 gotoy (max 0 (y - top))
5976 | 0xff0d (* enter *)
5977 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5978 | 0xff1b -> leavebirdseye beye true (* escape *)
5979 | 0xff52 -> upbirdseye incr beye (* up *)
5980 | 0xff54 -> downbirdseye incr beye (* down *)
5981 | 0xff51 -> upbirdseye 1 beye (* left *)
5982 | 0xff53 -> downbirdseye 1 beye (* right *)
5984 | 0xff55 -> (* prior *)
5985 begin match state.layout with
5986 | l :: _ ->
5987 if l.pagey != 0
5988 then (
5989 state.mode <- Birdseye (
5990 oconf, leftx, l.pageno, hooverpageno, anchor
5992 gotopage1 l.pageno 0;
5994 else (
5995 let layout = layout (state.y-state.winh) (pgh state.layout) in
5996 match layout with
5997 | [] -> gotoy (clamp (-state.winh))
5998 | l :: _ ->
5999 state.mode <- Birdseye (
6000 oconf, leftx, l.pageno, hooverpageno, anchor
6002 gotopage1 l.pageno 0
6005 | [] -> gotoy (clamp (-state.winh))
6006 end;
6008 | 0xff56 -> (* next *)
6009 begin match List.rev state.layout with
6010 | l :: _ ->
6011 let layout = layout (state.y + (pgh state.layout)) state.winh in
6012 begin match layout with
6013 | [] ->
6014 let incr = l.pageh - l.pagevh in
6015 if incr = 0
6016 then (
6017 state.mode <-
6018 Birdseye (
6019 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6021 G.postRedisplay "birdseye pagedown";
6023 else gotoy (clamp (incr + conf.interpagespace*2));
6025 | l :: _ ->
6026 state.mode <-
6027 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6028 gotopage1 l.pageno 0;
6031 | [] -> gotoy (clamp state.winh)
6032 end;
6034 | 0xff50 -> (* home *)
6035 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6036 gotopage1 0 0
6038 | 0xff57 -> (* end *)
6039 let pageno = state.pagecount - 1 in
6040 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6041 if not (pagevisible state.layout pageno)
6042 then
6043 let h =
6044 match List.rev state.pdims with
6045 | [] -> state.winh
6046 | (_, _, h, _) :: _ -> h
6048 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6049 else G.postRedisplay "birdseye end";
6050 | _ -> viewkeyboard key mask
6053 let drawpage l =
6054 let color =
6055 match state.mode with
6056 | Textentry _ -> scalecolor 0.4
6057 | LinkNav _
6058 | View -> scalecolor 1.0
6059 | Birdseye (_, _, pageno, hooverpageno, _) ->
6060 if l.pageno = hooverpageno
6061 then scalecolor 0.9
6062 else (
6063 if l.pageno = pageno
6064 then scalecolor 1.0
6065 else scalecolor 0.8
6068 drawtiles l color;
6071 let postdrawpage l linkindexbase =
6072 match getopaque l.pageno with
6073 | Some opaque ->
6074 if tileready l l.pagex l.pagey
6075 then
6076 let x = l.pagedispx - l.pagex
6077 and y = l.pagedispy - l.pagey in
6078 let hlmask =
6079 match conf.columns with
6080 | Csingle _ | Cmulti _ ->
6081 (if conf.hlinks then 1 else 0)
6082 + (if state.glinks
6083 && not (isbirdseye state.mode) then 2 else 0)
6084 | _ -> 0
6086 let s =
6087 match state.mode with
6088 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6089 | _ -> ""
6091 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6092 else 0
6093 | _ -> 0
6096 let scrollindicator () =
6097 let sbw, ph, sh = state.uioh#scrollph in
6098 let sbh, pw, sw = state.uioh#scrollpw in
6100 GlDraw.color (0.64, 0.64, 0.64);
6101 filledrect
6102 (float (state.winw - sbw)) 0.
6103 (float state.winw) (float state.winh)
6105 filledrect
6106 0. (float (state.winh - sbh))
6107 (float (wadjsb state.winw - 1)) (float state.winh)
6109 GlDraw.color (0.0, 0.0, 0.0);
6111 filledrect
6112 (float (state.winw - sbw)) ph
6113 (float state.winw) (ph +. sh)
6115 filledrect
6116 pw (float (state.winh - sbh))
6117 (pw +. sw) (float state.winh)
6121 let showsel () =
6122 match state.mstate with
6123 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6126 | Msel ((x0, y0), (x1, y1)) ->
6127 let rec loop = function
6128 | l :: ls ->
6129 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6130 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6131 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6132 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6133 then
6134 match getopaque l.pageno with
6135 | Some opaque ->
6136 let x0, y0 = pagetranslatepoint l x0 y0 in
6137 let x1, y1 = pagetranslatepoint l x1 y1 in
6138 seltext opaque (x0, y0, x1, y1);
6139 | _ -> ()
6140 else loop ls
6141 | [] -> ()
6143 loop state.layout
6146 let showrects = function [] -> () | rects ->
6147 Gl.enable `blend;
6148 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6149 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6150 List.iter
6151 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6152 List.iter (fun l ->
6153 if l.pageno = pageno
6154 then (
6155 let dx = float (l.pagedispx - l.pagex) in
6156 let dy = float (l.pagedispy - l.pagey) in
6157 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6158 Raw.sets_float state.vraw ~pos:0
6159 [| x0+.dx; y0+.dy;
6160 x1+.dx; y1+.dy;
6161 x3+.dx; y3+.dy;
6162 x2+.dx; y2+.dy |];
6163 GlArray.vertex `two state.vraw;
6164 GlArray.draw_arrays `triangle_strip 0 4;
6166 ) state.layout
6167 ) rects
6169 Gl.disable `blend;
6172 let display () =
6173 GlClear.color (scalecolor2 conf.bgcolor);
6174 GlClear.clear [`color];
6175 List.iter drawpage state.layout;
6176 let rects =
6177 match state.mode with
6178 | LinkNav (Ltexact (pageno, linkno)) ->
6179 begin match getopaque pageno with
6180 | Some opaque ->
6181 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6182 (pageno, 5, (
6183 float x0, float y0,
6184 float x1, float y0,
6185 float x1, float y1,
6186 float x0, float y1)
6187 ) :: state.rects
6188 | None -> state.rects
6190 | _ -> state.rects
6192 showrects rects;
6193 let rec postloop linkindexbase = function
6194 | l :: rest ->
6195 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6196 postloop linkindexbase rest
6197 | [] -> ()
6199 showsel ();
6200 postloop 0 state.layout;
6201 state.uioh#display;
6202 begin match state.mstate with
6203 | Mzoomrect ((x0, y0), (x1, y1)) ->
6204 Gl.enable `blend;
6205 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6206 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6207 filledrect (float x0) (float y0) (float x1) (float y1);
6208 Gl.disable `blend;
6209 | _ -> ()
6210 end;
6211 enttext ();
6212 scrollindicator ();
6213 Wsi.swapb ();
6216 let zoomrect x y x1 y1 =
6217 let x0 = min x x1
6218 and x1 = max x x1
6219 and y0 = min y y1 in
6220 gotoy (state.y + y0);
6221 state.anchor <- getanchor ();
6222 let zoom = (float state.w) /. float (x1 - x0) in
6223 let margin =
6224 match conf.fitmodel, conf.columns with
6225 | FitPage, Csplit _ ->
6226 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6228 | _, _ ->
6229 let adjw = wadjsb state.winw in
6230 if state.w < adjw
6231 then (adjw - state.w) / 2
6232 else 0
6234 state.x <- (state.x + margin) - x0;
6235 setzoom zoom;
6236 Wsi.setcursor Wsi.CURSOR_INHERIT;
6237 state.mstate <- Mnone;
6240 let zoomblock x y =
6241 let g opaque l px py =
6242 match rectofblock opaque px py with
6243 | Some a ->
6244 let x0 = a.(0) -. 20. in
6245 let x1 = a.(1) +. 20. in
6246 let y0 = a.(2) -. 20. in
6247 let zoom = (float state.w) /. (x1 -. x0) in
6248 let pagey = getpagey l.pageno in
6249 gotoy_and_clear_text (pagey + truncate y0);
6250 state.anchor <- getanchor ();
6251 let margin = (state.w - l.pagew)/2 in
6252 state.x <- -truncate x0 - margin;
6253 setzoom zoom;
6254 None
6255 | None -> None
6257 match conf.columns with
6258 | Csplit _ ->
6259 showtext '!' "block zooming does not work properly in split columns mode"
6260 | _ -> onppundermouse g x y ()
6263 let scrollx x =
6264 let winw = wadjsb state.winw - 1 in
6265 let s = float x /. float winw in
6266 let destx = truncate (float (state.w + winw) *. s) in
6267 state.x <- winw - destx;
6268 gotoy_and_clear_text state.y;
6269 state.mstate <- Mscrollx;
6272 let scrolly y =
6273 let s = float y /. float state.winh in
6274 let desty = truncate (float (state.maxy - state.winh) *. s) in
6275 gotoy_and_clear_text desty;
6276 state.mstate <- Mscrolly;
6279 let viewmouse button down x y mask =
6280 match button with
6281 | n when (n == 4 || n == 5) && not down ->
6282 if Wsi.withctrl mask
6283 then (
6284 match state.mstate with
6285 | Mzoom (oldn, i) ->
6286 if oldn = n
6287 then (
6288 if i = 2
6289 then
6290 let incr =
6291 match n with
6292 | 5 ->
6293 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6294 | _ ->
6295 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6297 let zoom = conf.zoom -. incr in
6298 setzoom zoom;
6299 state.mstate <- Mzoom (n, 0);
6300 else
6301 state.mstate <- Mzoom (n, i+1);
6303 else state.mstate <- Mzoom (n, 0)
6305 | _ -> state.mstate <- Mzoom (n, 0)
6307 else (
6308 match state.autoscroll with
6309 | Some step -> setautoscrollspeed step (n=4)
6310 | None ->
6311 if conf.wheelbypage || conf.presentation
6312 then (
6313 if n = 4
6314 then prevpage ()
6315 else nextpage ()
6317 else
6318 let incr =
6319 if n = 4
6320 then -conf.scrollstep
6321 else conf.scrollstep
6323 let incr = incr * 2 in
6324 let y = clamp incr in
6325 gotoy_and_clear_text y
6328 | n when (n = 6 || n = 7) && not down && canpan () ->
6329 state.x <-
6330 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6331 gotoy_and_clear_text state.y
6333 | 1 when Wsi.withshift mask ->
6334 state.mstate <- Mnone;
6335 if not down
6336 then (
6337 match unproject x y with
6338 | Some (pageno, ux, uy) ->
6339 let cmd = Printf.sprintf
6340 "%s %s %d %d %d"
6341 conf.stcmd state.path pageno ux uy
6343 popen cmd []
6344 | None -> ()
6347 | 1 when Wsi.withctrl mask ->
6348 if down
6349 then (
6350 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6351 state.mstate <- Mpan (x, y)
6353 else
6354 state.mstate <- Mnone
6356 | 3 ->
6357 if down
6358 then (
6359 Wsi.setcursor Wsi.CURSOR_CYCLE;
6360 let p = (x, y) in
6361 state.mstate <- Mzoomrect (p, p)
6363 else (
6364 match state.mstate with
6365 | Mzoomrect ((x0, y0), _) ->
6366 if abs (x-x0) > 10 && abs (y - y0) > 10
6367 then zoomrect x0 y0 x y
6368 else (
6369 state.mstate <- Mnone;
6370 Wsi.setcursor Wsi.CURSOR_INHERIT;
6371 G.postRedisplay "kill accidental zoom rect";
6373 | _ ->
6374 Wsi.setcursor Wsi.CURSOR_INHERIT;
6375 state.mstate <- Mnone
6378 | 1 when x > state.winw - vscrollw () ->
6379 if down
6380 then
6381 let _, position, sh = state.uioh#scrollph in
6382 if y > truncate position && y < truncate (position +. sh)
6383 then state.mstate <- Mscrolly
6384 else scrolly y
6385 else
6386 state.mstate <- Mnone
6388 | 1 when y > state.winh - hscrollh () ->
6389 if down
6390 then
6391 let _, position, sw = state.uioh#scrollpw in
6392 if x > truncate position && x < truncate (position +. sw)
6393 then state.mstate <- Mscrollx
6394 else scrollx x
6395 else
6396 state.mstate <- Mnone
6398 | 1 when state.bzoom -> if not down then zoomblock x y
6400 | 1 ->
6401 let dest = if down then getunder x y else Unone in
6402 begin match dest with
6403 | Ulinkgoto _
6404 | Ulinkuri _
6405 | Uremote _ | Uremotedest _
6406 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6407 gotounder dest
6409 | Unone when down ->
6410 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6411 state.mstate <- Mpan (x, y);
6413 | Unone | Utext _ ->
6414 if down
6415 then (
6416 if conf.angle mod 360 = 0
6417 then (
6418 state.mstate <- Msel ((x, y), (x, y));
6419 G.postRedisplay "mouse select";
6422 else (
6423 match state.mstate with
6424 | Mnone -> ()
6426 | Mzoom _ | Mscrollx | Mscrolly ->
6427 state.mstate <- Mnone
6429 | Mzoomrect ((x0, y0), _) ->
6430 zoomrect x0 y0 x y
6432 | Mpan _ ->
6433 Wsi.setcursor Wsi.CURSOR_INHERIT;
6434 state.mstate <- Mnone
6436 | Msel ((x0, y0), (x1, y1)) ->
6437 let rec loop = function
6438 | [] -> ()
6439 | l :: rest ->
6440 let inside =
6441 let a0 = l.pagedispy in
6442 let a1 = a0 + l.pagevh in
6443 let b0 = l.pagedispx in
6444 let b1 = b0 + l.pagevw in
6445 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6446 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6448 if inside
6449 then
6450 match getopaque l.pageno with
6451 | Some opaque ->
6452 begin
6453 match Ne.pipe () with
6454 | Ne.Exn exn ->
6455 showtext '!'
6456 (Printf.sprintf
6457 "can not create sel pipe: %s"
6458 (exntos exn));
6459 | Ne.Res (r, w) ->
6460 let doclose what fd =
6461 Ne.clo fd (fun msg ->
6462 dolog "%s close failed: %s" what msg)
6465 popen conf.selcmd [r, 0; w, -1];
6466 copysel w opaque true;
6467 doclose "pipe/r" r;
6468 G.postRedisplay "copysel";
6469 with exn ->
6470 dolog "can not execute %S: %s"
6471 conf.selcmd (exntos exn);
6472 doclose "pipe/r" r;
6473 doclose "pipe/w" w;
6475 | None -> ()
6476 else loop rest
6478 loop state.layout;
6479 Wsi.setcursor Wsi.CURSOR_INHERIT;
6480 state.mstate <- Mnone;
6484 | _ -> ()
6487 let birdseyemouse button down x y mask
6488 (conf, leftx, _, hooverpageno, anchor) =
6489 match button with
6490 | 1 when down ->
6491 let rec loop = function
6492 | [] -> ()
6493 | l :: rest ->
6494 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6495 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6496 then (
6497 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6499 else loop rest
6501 loop state.layout
6502 | 3 -> ()
6503 | _ -> viewmouse button down x y mask
6506 let uioh = object
6507 method display = ()
6509 method key key mask =
6510 begin match state.mode with
6511 | Textentry textentry -> textentrykeyboard key mask textentry
6512 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6513 | View -> viewkeyboard key mask
6514 | LinkNav linknav -> linknavkeyboard key mask linknav
6515 end;
6516 state.uioh
6518 method button button bstate x y mask =
6519 begin match state.mode with
6520 | LinkNav _
6521 | View -> viewmouse button bstate x y mask
6522 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6523 | Textentry _ -> ()
6524 end;
6525 state.uioh
6527 method motion x y =
6528 begin match state.mode with
6529 | Textentry _ -> ()
6530 | View | Birdseye _ | LinkNav _ ->
6531 match state.mstate with
6532 | Mzoom _ | Mnone -> ()
6534 | Mpan (x0, y0) ->
6535 let dx = x - x0
6536 and dy = y0 - y in
6537 state.mstate <- Mpan (x, y);
6538 if canpan ()
6539 then state.x <- panbound (state.x + dx);
6540 let y = clamp dy in
6541 gotoy_and_clear_text y
6543 | Msel (a, _) ->
6544 state.mstate <- Msel (a, (x, y));
6545 G.postRedisplay "motion select";
6547 | Mscrolly ->
6548 let y = min state.winh (max 0 y) in
6549 scrolly y
6551 | Mscrollx ->
6552 let x = min state.winw (max 0 x) in
6553 scrollx x
6555 | Mzoomrect (p0, _) ->
6556 state.mstate <- Mzoomrect (p0, (x, y));
6557 G.postRedisplay "motion zoomrect";
6558 end;
6559 state.uioh
6561 method pmotion x y =
6562 begin match state.mode with
6563 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6564 let rec loop = function
6565 | [] ->
6566 if hooverpageno != -1
6567 then (
6568 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6569 G.postRedisplay "pmotion birdseye no hoover";
6571 | l :: rest ->
6572 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6573 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6574 then (
6575 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6576 G.postRedisplay "pmotion birdseye hoover";
6578 else loop rest
6580 loop state.layout
6582 | Textentry _ -> ()
6584 | LinkNav _
6585 | View ->
6586 match state.mstate with
6587 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6589 | Mnone ->
6590 updateunder x y;
6591 match conf.pax with
6592 | None -> ()
6593 | Some r ->
6594 let past, _, _ = !r in
6595 let now = now () in
6596 let delta = now -. past in
6597 if delta > 0.01
6598 then paxunder x y
6599 else r := (now, x, y)
6600 end;
6601 state.uioh
6603 method infochanged _ = ()
6605 method scrollph =
6606 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6607 let p, h =
6608 if maxy = 0
6609 then 0.0, float state.winh
6610 else scrollph state.y maxy
6612 vscrollw (), p, h
6614 method scrollpw =
6615 let winw = wadjsb state.winw in
6616 let fwinw = float winw in
6617 let sw =
6618 let sw = fwinw /. float state.w in
6619 let sw = fwinw *. sw in
6620 max sw (float conf.scrollh)
6622 let position =
6623 let maxx = state.w + winw in
6624 let x = winw - state.x in
6625 let percent = float x /. float maxx in
6626 (fwinw -. sw) *. percent
6628 hscrollh (), position, sw
6630 method modehash =
6631 let modename =
6632 match state.mode with
6633 | LinkNav _ -> "links"
6634 | Textentry _ -> "textentry"
6635 | Birdseye _ -> "birdseye"
6636 | View -> "view"
6638 findkeyhash conf modename
6640 method eformsgs = true
6641 end;;
6643 module Config =
6644 struct
6645 open Parser
6647 let fontpath = ref "";;
6649 module KeyMap =
6650 Map.Make (struct type t = (int * int) let compare = compare end);;
6652 let unent s =
6653 let l = String.length s in
6654 let b = Buffer.create l in
6655 unent b s 0 l;
6656 Buffer.contents b;
6659 let home =
6660 try Sys.getenv "HOME"
6661 with exn ->
6662 prerr_endline
6663 ("Can not determine home directory location: " ^ exntos exn);
6667 let modifier_of_string = function
6668 | "alt" -> Wsi.altmask
6669 | "shift" -> Wsi.shiftmask
6670 | "ctrl" | "control" -> Wsi.ctrlmask
6671 | "meta" -> Wsi.metamask
6672 | _ -> 0
6675 let key_of_string =
6676 let r = Str.regexp "-" in
6677 fun s ->
6678 let elems = Str.full_split r s in
6679 let f n k m =
6680 let g s =
6681 let m1 = modifier_of_string s in
6682 if m1 = 0
6683 then (Wsi.namekey s, m)
6684 else (k, m lor m1)
6685 in function
6686 | Str.Delim s when n land 1 = 0 -> g s
6687 | Str.Text s -> g s
6688 | Str.Delim _ -> (k, m)
6690 let rec loop n k m = function
6691 | [] -> (k, m)
6692 | x :: xs ->
6693 let k, m = f n k m x in
6694 loop (n+1) k m xs
6696 loop 0 0 0 elems
6699 let keys_of_string =
6700 let r = Str.regexp "[ \t]" in
6701 fun s ->
6702 let elems = Str.split r s in
6703 List.map key_of_string elems
6706 let copykeyhashes c =
6707 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6710 let config_of c attrs =
6711 let apply c k v =
6713 match k with
6714 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6715 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6716 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6717 | "preload" -> { c with preload = bool_of_string v }
6718 | "page-bias" -> { c with pagebias = int_of_string v }
6719 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6720 | "horizontal-scroll-step" ->
6721 { c with hscrollstep = max (int_of_string v) 1 }
6722 | "auto-scroll-step" ->
6723 { c with autoscrollstep = max 0 (int_of_string v) }
6724 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6725 | "crop-hack" -> { c with crophack = bool_of_string v }
6726 | "throttle" ->
6727 let mw =
6728 match String.lowercase v with
6729 | "true" -> Some infinity
6730 | "false" -> None
6731 | f -> Some (float_of_string f)
6733 { c with maxwait = mw}
6734 | "highlight-links" -> { c with hlinks = bool_of_string v }
6735 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6736 | "vertical-margin" ->
6737 { c with interpagespace = max 0 (int_of_string v) }
6738 | "zoom" ->
6739 let zoom = float_of_string v /. 100. in
6740 let zoom = max zoom 0.0 in
6741 { c with zoom = zoom }
6742 | "presentation" -> { c with presentation = bool_of_string v }
6743 | "rotation-angle" -> { c with angle = int_of_string v }
6744 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6745 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6746 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6747 | "proportional-display" ->
6748 let fm =
6749 if bool_of_string v
6750 then FitProportional
6751 else FitWidth
6753 { c with fitmodel = fm }
6754 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6755 | "pixmap-cache-size" ->
6756 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6757 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6758 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6759 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6760 | "persistent-location" -> { c with jumpback = bool_of_string v }
6761 | "background-color" -> { c with bgcolor = color_of_string v }
6762 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6763 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6764 | "mupdf-store-size" ->
6765 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6766 | "checkers" -> { c with checkers = bool_of_string v }
6767 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6768 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6769 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6770 | "uri-launcher" -> { c with urilauncher = unent v }
6771 | "path-launcher" -> { c with pathlauncher = unent v }
6772 | "color-space" -> { c with colorspace = CSTE.of_string v }
6773 | "invert-colors" -> { c with invert = bool_of_string v }
6774 | "brightness" -> { c with colorscale = float_of_string v }
6775 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6776 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6777 | "columns" ->
6778 let (n, _, _) as nab = multicolumns_of_string v in
6779 if n < 0
6780 then { c with columns = Csplit (-n, [||]) }
6781 else { c with columns = Cmulti (nab, [||]) }
6782 | "birds-eye-columns" ->
6783 { c with beyecolumns = Some (max (int_of_string v) 2) }
6784 | "selection-command" -> { c with selcmd = unent v }
6785 | "synctex-command" -> { c with stcmd = unent v }
6786 | "pax-command" -> { c with paxcmd = unent v }
6787 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6788 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6789 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6790 | "use-pbo" -> { c with usepbo = bool_of_string v }
6791 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6792 | "horizontal-scrollbar-visible" ->
6793 let b =
6794 if bool_of_string v
6795 then c.scrollb lor scrollbhv
6796 else c.scrollb land (lnot scrollbhv)
6798 { c with scrollb = b }
6799 | "vertical-scrollbar-visible" ->
6800 let b =
6801 if bool_of_string v
6802 then c.scrollb lor scrollbvv
6803 else c.scrollb land (lnot scrollbvv)
6805 { c with scrollb = b }
6806 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6807 | "point-and-x" ->
6808 { c with pax =
6809 if bool_of_string v
6810 then Some (ref (0.0, 0, 0))
6811 else None }
6812 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6813 | _ -> c
6814 with exn ->
6815 prerr_endline ("Error processing attribute (`" ^
6816 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6819 let rec fold c = function
6820 | [] -> c
6821 | (k, v) :: rest ->
6822 let c = apply c k v in
6823 fold c rest
6825 fold { c with keyhashes = copykeyhashes c } attrs;
6828 let fromstring f pos n v d =
6829 try f v
6830 with exn ->
6831 dolog "Error processing attribute (%S=%S) at %d\n%s"
6832 n v pos (exntos exn)
6837 let bookmark_of attrs =
6838 let rec fold title page rely visy = function
6839 | ("title", v) :: rest -> fold v page rely visy rest
6840 | ("page", v) :: rest -> fold title v rely visy rest
6841 | ("rely", v) :: rest -> fold title page v visy rest
6842 | ("visy", v) :: rest -> fold title page rely v rest
6843 | _ :: rest -> fold title page rely visy rest
6844 | [] -> title, page, rely, visy
6846 fold "invalid" "0" "0" "0" attrs
6849 let doc_of attrs =
6850 let rec fold path page rely pan visy = function
6851 | ("path", v) :: rest -> fold v page rely pan visy rest
6852 | ("page", v) :: rest -> fold path v rely pan visy rest
6853 | ("rely", v) :: rest -> fold path page v pan visy rest
6854 | ("pan", v) :: rest -> fold path page rely v visy rest
6855 | ("visy", v) :: rest -> fold path page rely pan v rest
6856 | _ :: rest -> fold path page rely pan visy rest
6857 | [] -> path, page, rely, pan, visy
6859 fold "" "0" "0" "0" "0" attrs
6862 let map_of attrs =
6863 let rec fold rs ls = function
6864 | ("out", v) :: rest -> fold v ls rest
6865 | ("in", v) :: rest -> fold rs v rest
6866 | _ :: rest -> fold ls rs rest
6867 | [] -> ls, rs
6869 fold "" "" attrs
6872 let setconf dst src =
6873 dst.scrollbw <- src.scrollbw;
6874 dst.scrollh <- src.scrollh;
6875 dst.icase <- src.icase;
6876 dst.preload <- src.preload;
6877 dst.pagebias <- src.pagebias;
6878 dst.verbose <- src.verbose;
6879 dst.scrollstep <- src.scrollstep;
6880 dst.maxhfit <- src.maxhfit;
6881 dst.crophack <- src.crophack;
6882 dst.autoscrollstep <- src.autoscrollstep;
6883 dst.maxwait <- src.maxwait;
6884 dst.hlinks <- src.hlinks;
6885 dst.underinfo <- src.underinfo;
6886 dst.interpagespace <- src.interpagespace;
6887 dst.zoom <- src.zoom;
6888 dst.presentation <- src.presentation;
6889 dst.angle <- src.angle;
6890 dst.cwinw <- src.cwinw;
6891 dst.cwinh <- src.cwinh;
6892 dst.savebmarks <- src.savebmarks;
6893 dst.memlimit <- src.memlimit;
6894 dst.fitmodel <- src.fitmodel;
6895 dst.texcount <- src.texcount;
6896 dst.sliceheight <- src.sliceheight;
6897 dst.thumbw <- src.thumbw;
6898 dst.jumpback <- src.jumpback;
6899 dst.bgcolor <- src.bgcolor;
6900 dst.tilew <- src.tilew;
6901 dst.tileh <- src.tileh;
6902 dst.mustoresize <- src.mustoresize;
6903 dst.checkers <- src.checkers;
6904 dst.aalevel <- src.aalevel;
6905 dst.trimmargins <- src.trimmargins;
6906 dst.trimfuzz <- src.trimfuzz;
6907 dst.urilauncher <- src.urilauncher;
6908 dst.colorspace <- src.colorspace;
6909 dst.invert <- src.invert;
6910 dst.colorscale <- src.colorscale;
6911 dst.redirectstderr <- src.redirectstderr;
6912 dst.ghyllscroll <- src.ghyllscroll;
6913 dst.columns <- src.columns;
6914 dst.beyecolumns <- src.beyecolumns;
6915 dst.selcmd <- src.selcmd;
6916 dst.updatecurs <- src.updatecurs;
6917 dst.pathlauncher <- src.pathlauncher;
6918 dst.keyhashes <- copykeyhashes src;
6919 dst.hfsize <- src.hfsize;
6920 dst.hscrollstep <- src.hscrollstep;
6921 dst.pgscale <- src.pgscale;
6922 dst.usepbo <- src.usepbo;
6923 dst.wheelbypage <- src.wheelbypage;
6924 dst.stcmd <- src.stcmd;
6925 dst.paxcmd <- src.paxcmd;
6926 dst.scrollb <- src.scrollb;
6927 dst.riani <- src.riani;
6928 dst.paxmark <- src.paxmark;
6929 dst.pax <-
6930 if src.pax = None
6931 then None
6932 else Some ((ref (0.0, 0, 0)));
6935 let get s =
6936 let h = Hashtbl.create 10 in
6937 let dc = { defconf with angle = defconf.angle } in
6938 let rec toplevel v t spos _ =
6939 match t with
6940 | Vdata | Vcdata | Vend -> v
6941 | Vopen ("llppconfig", _, closed) ->
6942 if closed
6943 then v
6944 else { v with f = llppconfig }
6945 | Vopen _ ->
6946 error "unexpected subelement at top level" s spos
6947 | Vclose _ -> error "unexpected close at top level" s spos
6949 and llppconfig v t spos _ =
6950 match t with
6951 | Vdata | Vcdata -> v
6952 | Vend -> error "unexpected end of input in llppconfig" s spos
6953 | Vopen ("defaults", attrs, closed) ->
6954 let c = config_of dc attrs in
6955 setconf dc c;
6956 if closed
6957 then v
6958 else { v with f = defaults }
6960 | Vopen ("ui-font", attrs, closed) ->
6961 let rec getsize size = function
6962 | [] -> size
6963 | ("size", v) :: rest ->
6964 let size =
6965 fromstring int_of_string spos "size" v fstate.fontsize in
6966 getsize size rest
6967 | l -> getsize size l
6969 fstate.fontsize <- getsize fstate.fontsize attrs;
6970 if closed
6971 then v
6972 else { v with f = uifont (Buffer.create 10) }
6974 | Vopen ("doc", attrs, closed) ->
6975 let pathent, spage, srely, span, svisy = doc_of attrs in
6976 let path = unent pathent
6977 and pageno = fromstring int_of_string spos "page" spage 0
6978 and rely = fromstring float_of_string spos "rely" srely 0.0
6979 and pan = fromstring int_of_string spos "pan" span 0
6980 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6981 let c = config_of dc attrs in
6982 let anchor = (pageno, rely, visy) in
6983 if closed
6984 then (Hashtbl.add h path (c, [], pan, anchor); v)
6985 else { v with f = doc path pan anchor c [] }
6987 | Vopen _ ->
6988 error "unexpected subelement in llppconfig" s spos
6990 | Vclose "llppconfig" -> { v with f = toplevel }
6991 | Vclose _ -> error "unexpected close in llppconfig" s spos
6993 and defaults v t spos _ =
6994 match t with
6995 | Vdata | Vcdata -> v
6996 | Vend -> error "unexpected end of input in defaults" s spos
6997 | Vopen ("keymap", attrs, closed) ->
6998 let modename =
6999 try List.assoc "mode" attrs
7000 with Not_found -> "global" in
7001 if closed
7002 then v
7003 else
7004 let ret keymap =
7005 let h = findkeyhash dc modename in
7006 KeyMap.iter (Hashtbl.replace h) keymap;
7007 defaults
7009 { v with f = pkeymap ret KeyMap.empty }
7011 | Vopen (_, _, _) ->
7012 error "unexpected subelement in defaults" s spos
7014 | Vclose "defaults" ->
7015 { v with f = llppconfig }
7017 | Vclose _ -> error "unexpected close in defaults" s spos
7019 and uifont b v t spos epos =
7020 match t with
7021 | Vdata | Vcdata ->
7022 Buffer.add_substring b s spos (epos - spos);
7024 | Vopen (_, _, _) ->
7025 error "unexpected subelement in ui-font" s spos
7026 | Vclose "ui-font" ->
7027 if emptystr !fontpath
7028 then fontpath := Buffer.contents b;
7029 { v with f = llppconfig }
7030 | Vclose _ -> error "unexpected close in ui-font" s spos
7031 | Vend -> error "unexpected end of input in ui-font" s spos
7033 and doc path pan anchor c bookmarks v t spos _ =
7034 match t with
7035 | Vdata | Vcdata -> v
7036 | Vend -> error "unexpected end of input in doc" s spos
7037 | Vopen ("bookmarks", _, closed) ->
7038 if closed
7039 then v
7040 else { v with f = pbookmarks path pan anchor c bookmarks }
7042 | Vopen ("keymap", attrs, closed) ->
7043 let modename =
7044 try List.assoc "mode" attrs
7045 with Not_found -> "global"
7047 if closed
7048 then v
7049 else
7050 let ret keymap =
7051 let h = findkeyhash c modename in
7052 KeyMap.iter (Hashtbl.replace h) keymap;
7053 doc path pan anchor c bookmarks
7055 { v with f = pkeymap ret KeyMap.empty }
7057 | Vopen (_, _, _) ->
7058 error "unexpected subelement in doc" s spos
7060 | Vclose "doc" ->
7061 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7062 { v with f = llppconfig }
7064 | Vclose _ -> error "unexpected close in doc" s spos
7066 and pkeymap ret keymap v t spos _ =
7067 match t with
7068 | Vdata | Vcdata -> v
7069 | Vend -> error "unexpected end of input in keymap" s spos
7070 | Vopen ("map", attrs, closed) ->
7071 let r, l = map_of attrs in
7072 let kss = fromstring keys_of_string spos "in" r [] in
7073 let lss = fromstring keys_of_string spos "out" l [] in
7074 let keymap =
7075 match kss with
7076 | [] -> keymap
7077 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7078 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7080 if closed
7081 then { v with f = pkeymap ret keymap }
7082 else
7083 let f () = v in
7084 { v with f = skip "map" f }
7086 | Vopen _ ->
7087 error "unexpected subelement in keymap" s spos
7089 | Vclose "keymap" ->
7090 { v with f = ret keymap }
7092 | Vclose _ -> error "unexpected close in keymap" s spos
7094 and pbookmarks path pan anchor c bookmarks v t spos _ =
7095 match t with
7096 | Vdata | Vcdata -> v
7097 | Vend -> error "unexpected end of input in bookmarks" s spos
7098 | Vopen ("item", attrs, closed) ->
7099 let titleent, spage, srely, svisy = bookmark_of attrs in
7100 let page = fromstring int_of_string spos "page" spage 0
7101 and rely = fromstring float_of_string spos "rely" srely 0.0
7102 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7103 let bookmarks =
7104 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7106 if closed
7107 then { v with f = pbookmarks path pan anchor c bookmarks }
7108 else
7109 let f () = v in
7110 { v with f = skip "item" f }
7112 | Vopen _ ->
7113 error "unexpected subelement in bookmarks" s spos
7115 | Vclose "bookmarks" ->
7116 { v with f = doc path pan anchor c bookmarks }
7118 | Vclose _ -> error "unexpected close in bookmarks" s spos
7120 and skip tag f v t spos _ =
7121 match t with
7122 | Vdata | Vcdata -> v
7123 | Vend ->
7124 error ("unexpected end of input in skipped " ^ tag) s spos
7125 | Vopen (tag', _, closed) ->
7126 if closed
7127 then v
7128 else
7129 let f' () = { v with f = skip tag f } in
7130 { v with f = skip tag' f' }
7131 | Vclose ctag ->
7132 if tag = ctag
7133 then f ()
7134 else error ("unexpected close in skipped " ^ tag) s spos
7137 parse { f = toplevel; accu = () } s;
7138 h, dc;
7141 let do_load f ic =
7143 let len = in_channel_length ic in
7144 let s = String.create len in
7145 really_input ic s 0 len;
7146 f s;
7147 with
7148 | Parse_error (msg, s, pos) ->
7149 let subs = subs s pos in
7150 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7152 | exn ->
7153 failwith ("config load error: " ^ exntos exn)
7156 let defconfpath =
7157 let dir =
7159 let dir = Filename.concat home ".config" in
7160 if Sys.is_directory dir then dir else home
7161 with _ -> home
7163 Filename.concat dir "llpp.conf"
7166 let confpath = ref defconfpath;;
7168 let load1 f =
7169 if Sys.file_exists !confpath
7170 then
7171 match
7172 (try Some (open_in_bin !confpath)
7173 with exn ->
7174 prerr_endline
7175 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7176 exntos exn);
7177 None
7179 with
7180 | Some ic ->
7181 let success =
7183 f (do_load get ic)
7184 with exn ->
7185 prerr_endline
7186 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7187 exntos exn);
7188 false
7190 close_in ic;
7191 success
7193 | None -> false
7194 else
7195 f (Hashtbl.create 0, defconf)
7198 let load () =
7199 let f (h, dc) =
7200 let pc, pb, px, pa =
7202 let key =
7203 if emptystr state.origin
7204 then state.path
7205 else state.origin
7207 Hashtbl.find h (Filename.basename key)
7208 with Not_found -> dc, [], 0, emptyanchor
7210 setconf defconf dc;
7211 setconf conf pc;
7212 state.bookmarks <- pb;
7213 state.x <- px;
7214 if conf.jumpback
7215 then state.anchor <- pa;
7216 cbput state.hists.nav pa;
7217 true
7219 load1 f
7222 let add_attrs bb always dc c =
7223 let ob s a b =
7224 if always || a != b
7225 then Printf.bprintf bb "\n %s='%b'" s a
7226 and op s a b =
7227 if always || a <> b
7228 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7229 and oi s a b =
7230 if always || a != b
7231 then Printf.bprintf bb "\n %s='%d'" s a
7232 and oI s a b =
7233 if always || a != b
7234 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7235 and oz s a b =
7236 if always || a <> b
7237 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7238 and oF s a b =
7239 if always || a <> b
7240 then Printf.bprintf bb "\n %s='%f'" s a
7241 and oc s a b =
7242 if always || a <> b
7243 then
7244 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7245 and oC s a b =
7246 if always || a <> b
7247 then
7248 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7249 and oR s a b =
7250 if always || a <> b
7251 then
7252 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7253 and os s a b =
7254 if always || a <> b
7255 then
7256 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7257 and og s a b =
7258 if always || a <> b
7259 then
7260 match a with
7261 | Some (_N, _A, _B) ->
7262 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7263 | None ->
7264 match b with
7265 | None -> ()
7266 | _ ->
7267 Printf.bprintf bb "\n %s='none'" s
7268 and oW s a b =
7269 if always || a <> b
7270 then
7271 let v =
7272 match a with
7273 | None -> "false"
7274 | Some f ->
7275 if f = infinity
7276 then "true"
7277 else string_of_float f
7279 Printf.bprintf bb "\n %s='%s'" s v
7280 and oco s a b =
7281 if always || a <> b
7282 then
7283 match a with
7284 | Cmulti ((n, a, b), _) when n > 1 ->
7285 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7286 | Csplit (n, _) when n > 1 ->
7287 Printf.bprintf bb "\n %s='%d'" s ~-n
7288 | _ -> ()
7289 and obeco s a b =
7290 if always || a <> b
7291 then
7292 match a with
7293 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7294 | _ -> ()
7295 and oFm s a b =
7296 if always || a <> b
7297 then
7298 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7299 and oSv s a b m =
7300 if always || a <> b
7301 then
7302 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7303 and oPm s a b =
7304 if always || a <> b
7305 then
7306 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7308 oi "width" c.cwinw dc.cwinw;
7309 oi "height" c.cwinh dc.cwinh;
7310 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7311 oi "scroll-handle-height" c.scrollh dc.scrollh;
7312 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7313 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7314 ob "case-insensitive-search" c.icase dc.icase;
7315 ob "preload" c.preload dc.preload;
7316 oi "page-bias" c.pagebias dc.pagebias;
7317 oi "scroll-step" c.scrollstep dc.scrollstep;
7318 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7319 ob "max-height-fit" c.maxhfit dc.maxhfit;
7320 ob "crop-hack" c.crophack dc.crophack;
7321 oW "throttle" c.maxwait dc.maxwait;
7322 ob "highlight-links" c.hlinks dc.hlinks;
7323 ob "under-cursor-info" c.underinfo dc.underinfo;
7324 oi "vertical-margin" c.interpagespace dc.interpagespace;
7325 oz "zoom" c.zoom dc.zoom;
7326 ob "presentation" c.presentation dc.presentation;
7327 oi "rotation-angle" c.angle dc.angle;
7328 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7329 oFm "fit-model" c.fitmodel dc.fitmodel;
7330 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7331 oi "tex-count" c.texcount dc.texcount;
7332 oi "slice-height" c.sliceheight dc.sliceheight;
7333 oi "thumbnail-width" c.thumbw dc.thumbw;
7334 ob "persistent-location" c.jumpback dc.jumpback;
7335 oc "background-color" c.bgcolor dc.bgcolor;
7336 oi "tile-width" c.tilew dc.tilew;
7337 oi "tile-height" c.tileh dc.tileh;
7338 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7339 ob "checkers" c.checkers dc.checkers;
7340 oi "aalevel" c.aalevel dc.aalevel;
7341 ob "trim-margins" c.trimmargins dc.trimmargins;
7342 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7343 os "uri-launcher" c.urilauncher dc.urilauncher;
7344 os "path-launcher" c.pathlauncher dc.pathlauncher;
7345 oC "color-space" c.colorspace dc.colorspace;
7346 ob "invert-colors" c.invert dc.invert;
7347 oF "brightness" c.colorscale dc.colorscale;
7348 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7349 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7350 oco "columns" c.columns dc.columns;
7351 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7352 os "selection-command" c.selcmd dc.selcmd;
7353 os "synctex-command" c.stcmd dc.stcmd;
7354 os "pax-command" c.paxcmd dc.paxcmd;
7355 ob "update-cursor" c.updatecurs dc.updatecurs;
7356 oi "hint-font-size" c.hfsize dc.hfsize;
7357 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7358 oF "page-scroll-scale" c.pgscale dc.pgscale;
7359 ob "use-pbo" c.usepbo dc.usepbo;
7360 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7361 ob "remote-in-a-new-instance" c.riani dc.riani;
7362 op "point-and-x" c.pax dc.pax;
7363 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7366 let keymapsbuf always dc c =
7367 let bb = Buffer.create 16 in
7368 let rec loop = function
7369 | [] -> ()
7370 | (modename, h) :: rest ->
7371 let dh = findkeyhash dc modename in
7372 if always || h <> dh
7373 then (
7374 if Hashtbl.length h > 0
7375 then (
7376 if Buffer.length bb > 0
7377 then Buffer.add_char bb '\n';
7378 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7379 Hashtbl.iter (fun i o ->
7380 let isdifferent = always ||
7382 let dO = Hashtbl.find dh i in
7383 dO <> o
7384 with Not_found -> true
7386 if isdifferent
7387 then
7388 let addkm (k, m) =
7389 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7390 if Wsi.withalt m then Buffer.add_string bb "alt-";
7391 if Wsi.withshift m then Buffer.add_string bb "shift-";
7392 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7393 Buffer.add_string bb (Wsi.keyname k);
7395 let addkms l =
7396 let rec loop = function
7397 | [] -> ()
7398 | km :: [] -> addkm km
7399 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7401 loop l
7403 Buffer.add_string bb "<map in='";
7404 addkm i;
7405 match o with
7406 | KMinsrt km ->
7407 Buffer.add_string bb "' out='";
7408 addkm km;
7409 Buffer.add_string bb "'/>\n"
7411 | KMinsrl kms ->
7412 Buffer.add_string bb "' out='";
7413 addkms kms;
7414 Buffer.add_string bb "'/>\n"
7416 | KMmulti (ins, kms) ->
7417 Buffer.add_char bb ' ';
7418 addkms ins;
7419 Buffer.add_string bb "' out='";
7420 addkms kms;
7421 Buffer.add_string bb "'/>\n"
7422 ) h;
7423 Buffer.add_string bb "</keymap>";
7426 loop rest
7428 loop c.keyhashes;
7432 let save () =
7433 let uifontsize = fstate.fontsize in
7434 let bb = Buffer.create 32768 in
7435 let relx = float state.x /. float state.winw in
7436 let w, h, x =
7437 let cx w = truncate (relx *. float w) in
7438 List.fold_left
7439 (fun (w, h, x) ws ->
7440 match ws with
7441 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7442 | Wsi.MaxVert -> (w, conf.cwinh, x)
7443 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7445 (state.winw, state.winh, state.x) state.winstate
7447 conf.cwinw <- w;
7448 conf.cwinh <- h;
7449 let f (h, dc) =
7450 let dc = if conf.bedefault then conf else dc in
7451 Buffer.add_string bb "<llppconfig>\n";
7453 if nonemptystr !fontpath
7454 then
7455 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7456 uifontsize
7457 !fontpath
7458 else (
7459 if uifontsize <> 14
7460 then
7461 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7464 Buffer.add_string bb "<defaults";
7465 add_attrs bb true dc dc;
7466 let kb = keymapsbuf true dc dc in
7467 if Buffer.length kb > 0
7468 then (
7469 Buffer.add_string bb ">\n";
7470 Buffer.add_buffer bb kb;
7471 Buffer.add_string bb "\n</defaults>\n";
7473 else Buffer.add_string bb "/>\n";
7475 let adddoc path pan anchor c bookmarks =
7476 if bookmarks == [] && c = dc && anchor = emptyanchor
7477 then ()
7478 else (
7479 Printf.bprintf bb "<doc path='%s'"
7480 (enent path 0 (String.length path));
7482 if anchor <> emptyanchor
7483 then (
7484 let n, rely, visy = anchor in
7485 Printf.bprintf bb " page='%d'" n;
7486 if rely > 1e-6
7487 then
7488 Printf.bprintf bb " rely='%f'" rely
7490 if abs_float visy > 1e-6
7491 then
7492 Printf.bprintf bb " visy='%f'" visy
7496 if pan != 0
7497 then Printf.bprintf bb " pan='%d'" pan;
7499 add_attrs bb false dc c;
7500 let kb = keymapsbuf false dc c in
7502 begin match bookmarks with
7503 | [] ->
7504 if Buffer.length kb > 0
7505 then (
7506 Buffer.add_string bb ">\n";
7507 Buffer.add_buffer bb kb;
7508 Buffer.add_string bb "\n</doc>\n";
7510 else Buffer.add_string bb "/>\n"
7511 | _ ->
7512 Buffer.add_string bb ">\n<bookmarks>\n";
7513 List.iter (fun (title, _, kind) ->
7514 begin match kind with
7515 | Oanchor (page, rely, visy) ->
7516 Printf.bprintf bb
7517 "<item title='%s' page='%d'"
7518 (enent title 0 (String.length title))
7519 page
7521 if rely > 1e-6
7522 then
7523 Printf.bprintf bb " rely='%f'" rely
7525 if abs_float visy > 1e-6
7526 then
7527 Printf.bprintf bb " visy='%f'" visy
7529 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7530 failwith "unexpected link in bookmarks"
7531 end;
7532 Buffer.add_string bb "/>\n";
7533 ) bookmarks;
7534 Buffer.add_string bb "</bookmarks>";
7535 if Buffer.length kb > 0
7536 then (
7537 Buffer.add_string bb "\n";
7538 Buffer.add_buffer bb kb;
7540 Buffer.add_string bb "\n</doc>\n";
7541 end;
7545 let pan, conf =
7546 match state.mode with
7547 | Birdseye (c, pan, _, _, _) ->
7548 let beyecolumns =
7549 match conf.columns with
7550 | Cmulti ((c, _, _), _) -> Some c
7551 | Csingle _ -> None
7552 | Csplit _ -> None
7553 and columns =
7554 match c.columns with
7555 | Cmulti (c, _) -> Cmulti (c, [||])
7556 | Csingle _ -> Csingle [||]
7557 | Csplit _ -> failwith "quit from bird's eye while split"
7559 pan, { c with beyecolumns = beyecolumns; columns = columns }
7560 | _ -> x, conf
7562 let basename = Filename.basename
7563 (if emptystr state.origin then state.path else state.origin)
7565 adddoc basename pan (getanchor ())
7566 (let conf =
7567 let autoscrollstep =
7568 match state.autoscroll with
7569 | Some step -> step
7570 | None -> conf.autoscrollstep
7572 match state.mode with
7573 | Birdseye (bc, _, _, _, _) ->
7574 { conf with
7575 zoom = bc.zoom;
7576 presentation = bc.presentation;
7577 interpagespace = bc.interpagespace;
7578 maxwait = bc.maxwait;
7579 autoscrollstep = autoscrollstep }
7580 | _ -> { conf with autoscrollstep = autoscrollstep }
7581 in conf)
7582 (if conf.savebmarks then state.bookmarks else []);
7584 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7585 if basename <> path
7586 then adddoc path x anchor c bookmarks
7587 ) h;
7588 Buffer.add_string bb "</llppconfig>\n";
7589 true;
7591 if load1 f && Buffer.length bb > 0
7592 then
7594 let tmp = !confpath ^ ".tmp" in
7595 let oc = open_out_bin tmp in
7596 Buffer.output_buffer oc bb;
7597 close_out oc;
7598 Unix.rename tmp !confpath;
7599 with exn ->
7600 prerr_endline
7601 ("error while saving configuration: " ^ exntos exn)
7603 end;;
7605 let adderrmsg src msg =
7606 Buffer.add_string state.errmsgs msg;
7607 state.newerrmsgs <- true;
7608 G.postRedisplay src
7611 let adderrfmt src fmt =
7612 Format.kprintf (fun s -> adderrmsg src s) fmt;
7615 let ract cmds =
7616 let cl = splitatspace cmds in
7617 let scan s fmt f =
7618 try Scanf.sscanf s fmt f
7619 with exn ->
7620 adderrfmt "remote exec"
7621 "error processing '%S': %s\n" cmds (exntos exn)
7623 match cl with
7624 | "reload" :: [] -> reload ()
7625 | "goto" :: args :: [] ->
7626 scan args "%u %f %f"
7627 (fun pageno x y ->
7628 let cmd, _ = state.geomcmds in
7629 if emptystr cmd
7630 then gotopagexy pageno x y
7631 else
7632 let f prevf () =
7633 gotopagexy pageno x y;
7634 prevf ()
7636 state.reprf <- f state.reprf
7638 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7639 | "gotor" :: args :: [] ->
7640 scan args "%S %u"
7641 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7642 | "gotord" :: args :: [] ->
7643 scan args "%S %S"
7644 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7645 | "rect" :: args :: [] ->
7646 scan args "%u %u %f %f %f %f"
7647 (fun pageno color x0 y0 x1 y1 ->
7648 onpagerect pageno (fun w h ->
7649 let _,w1,h1,_ = getpagedim pageno in
7650 let sw = float w1 /. float w
7651 and sh = float h1 /. float h in
7652 let x0s = x0 *. sw
7653 and x1s = x1 *. sw
7654 and y0s = y0 *. sh
7655 and y1s = y1 *. sh in
7656 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7657 debugrect rect;
7658 state.rects <- (pageno, color, rect) :: state.rects;
7659 G.postRedisplay "rect";
7662 | "activatewin" :: [] -> Wsi.activatewin ()
7663 | "quit" :: [] -> raise Quit
7664 | _ ->
7665 adderrfmt "remote command"
7666 "error processing remote command: %S\n" cmds;
7669 let remote =
7670 let scratch = String.create 80 in
7671 let buf = Buffer.create 80 in
7672 fun fd ->
7673 let rec tempfr () =
7674 try Some (Unix.read fd scratch 0 80)
7675 with
7676 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7677 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7678 | exn -> raise exn
7680 match tempfr () with
7681 | None -> Some fd
7682 | Some n ->
7683 if n = 0
7684 then (
7685 Unix.close fd;
7686 if Buffer.length buf > 0
7687 then (
7688 let s = Buffer.contents buf in
7689 Buffer.clear buf;
7690 ract s;
7692 None
7694 else
7695 let rec eat ppos =
7696 let nlpos =
7698 let pos = String.index_from scratch ppos '\n' in
7699 if pos >= n then -1 else pos
7700 with Not_found -> -1
7702 if nlpos >= 0
7703 then (
7704 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7705 let s = Buffer.contents buf in
7706 Buffer.clear buf;
7707 ract s;
7708 eat (nlpos+1);
7710 else (
7711 Buffer.add_substring buf scratch ppos (n-ppos);
7712 Some fd
7714 in eat 0
7717 let remoteopen path =
7718 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7719 with exn ->
7720 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7721 None
7724 let () =
7725 let trimcachepath = ref "" in
7726 let rcmdpath = ref "" in
7727 let pageno = ref None in
7728 selfexec := Sys.executable_name;
7729 Arg.parse
7730 (Arg.align
7731 [("-p", Arg.String (fun s -> state.password <- s),
7732 "<password> Set password");
7734 ("-f", Arg.String
7735 (fun s ->
7736 Config.fontpath := s;
7737 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7739 "<path> Set path to the user interface font");
7741 ("-c", Arg.String
7742 (fun s ->
7743 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7744 Config.confpath := s),
7745 "<path> Set path to the configuration file");
7747 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7748 "<page-number> Jump to page");
7750 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7751 "<path> Set path to the trim cache file");
7753 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7754 "<named-destination> Set named destination");
7756 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7757 ("-cxack", Arg.Set cxack, " Cut corners");
7759 ("-remote", Arg.String (fun s -> rcmdpath := s),
7760 "<path> Set path to the remote commands source");
7762 ("-origin", Arg.String (fun s -> state.origin <- s),
7763 "<original-path> Set original path");
7765 ("-v", Arg.Unit (fun () ->
7766 Printf.printf
7767 "%s\nconfiguration path: %s\n"
7768 (version ())
7769 Config.defconfpath
7771 exit 0), " Print version and exit");
7774 (fun s -> state.path <- s)
7775 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7777 if !wtmode
7778 then selfexec := !selfexec ^ " -wtmode";
7780 if emptystr state.path
7781 then (prerr_endline "file name missing"; exit 1);
7783 if not (Config.load ())
7784 then prerr_endline "failed to load configuration";
7785 begin match !pageno with
7786 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7787 | None -> ()
7788 end;
7790 let wsfd, winw, winh = Wsi.init (object
7791 val mutable m_hack = false
7792 method expose = if not m_hack then G.postRedisplay "expose"
7793 method visible = G.postRedisplay "visible"
7794 method display = m_hack <- false; display ()
7795 method reshape w h =
7796 m_hack <- w < state.winw && h < state.winh;
7797 reshape w h
7798 method mouse b d x y m = state.uioh <- state.uioh#button b d x y m
7799 method motion x y =
7800 state.mpos <- (x, y);
7801 state.uioh <- state.uioh#motion x y
7802 method pmotion x y =
7803 state.mpos <- (x, y);
7804 state.uioh <- state.uioh#pmotion x y
7805 method key k m =
7806 let mascm = m land (
7807 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7808 ) in
7809 match state.keystate with
7810 | KSnone ->
7811 let km = k, mascm in
7812 begin
7813 match
7814 let modehash = state.uioh#modehash in
7815 try Hashtbl.find modehash km
7816 with Not_found ->
7817 try Hashtbl.find (findkeyhash conf "global") km
7818 with Not_found -> KMinsrt (k, m)
7819 with
7820 | KMinsrt (k, m) -> keyboard k m
7821 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7822 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7824 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7825 List.iter (fun (k, m) -> keyboard k m) insrt;
7826 state.keystate <- KSnone
7827 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7828 state.keystate <- KSinto (keys, insrt)
7829 | _ ->
7830 state.keystate <- KSnone
7832 method enter x y =
7833 state.mpos <- (x, y);
7834 state.uioh <- state.uioh#pmotion x y
7835 method leave = state.mpos <- (-1, -1)
7836 method winstate wsl = state.winstate <- wsl; m_hack <- false
7837 method quit = raise Quit
7838 end) conf.cwinw conf.cwinh (platform = Posx) in
7840 state.wsfd <- wsfd;
7842 if not (
7843 List.exists GlMisc.check_extension
7844 [ "GL_ARB_texture_rectangle"
7845 ; "GL_EXT_texture_recangle"
7846 ; "GL_NV_texture_rectangle" ]
7848 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7850 if (
7851 let r = GlMisc.get_string `renderer in
7852 let p = "Mesa DRI Intel(" in
7853 let l = String.length p in
7854 String.length r > l && String.sub r 0 l = p
7856 then (
7857 defconf.sliceheight <- 1024;
7858 defconf.texcount <- 32;
7859 defconf.usepbo <- true;
7862 let cr, sw =
7863 match Ne.pipe () with
7864 | Ne.Exn exn ->
7865 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7866 exit 1
7867 | Ne.Res rw -> rw
7868 and sr, cw =
7869 match Ne.pipe () with
7870 | Ne.Exn exn ->
7871 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7872 exit 1
7873 | Ne.Res rw -> rw
7876 cloexec cr;
7877 cloexec sw;
7878 cloexec sr;
7879 cloexec cw;
7881 setcheckers conf.checkers;
7882 redirectstderr ();
7883 if conf.redirectstderr
7884 then
7885 at_exit (fun () ->
7886 let s = Buffer.contents state.errmsgs ^
7887 (match state.errfd with
7888 | Some fd ->
7889 let s = String.create (80*24) in
7890 let n =
7892 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7893 if List.mem fd r
7894 then Unix.read fd s 0 (String.length s)
7895 else 0
7896 with _ -> 0
7898 if n = 0
7899 then ""
7900 else String.sub s 0 n
7901 | None -> ""
7904 try ignore (Unix.write state.stderr s 0 (String.length s))
7905 with exn -> print_endline (exntos exn)
7909 init (cr, cw) (
7910 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7911 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7912 !Config.fontpath, !trimcachepath,
7913 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7915 List.iter GlArray.enable [`texture_coord; `vertex];
7916 state.sr <- sr;
7917 state.sw <- sw;
7918 state.text <- "Opening " ^ (mbtoutf8 state.path);
7919 reshape winw winh;
7920 opendoc state.path state.password;
7921 state.uioh <- uioh;
7922 display ();
7923 Wsi.mapwin ();
7924 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7925 let optrfd =
7926 ref (
7927 if nonemptystr !rcmdpath
7928 then remoteopen !rcmdpath
7929 else None
7933 let rec loop deadline =
7934 let r =
7935 match state.errfd with
7936 | None -> [state.sr; state.wsfd]
7937 | Some fd -> [state.sr; state.wsfd; fd]
7939 let r =
7940 match !optrfd with
7941 | None -> r
7942 | Some fd -> fd :: r
7944 if state.redisplay
7945 then (
7946 state.redisplay <- false;
7947 display ();
7949 let timeout =
7950 let now = now () in
7951 if deadline > now
7952 then (
7953 if deadline = infinity
7954 then ~-.1.0
7955 else max 0.0 (deadline -. now)
7957 else 0.0
7959 let r, _, _ =
7960 try Unix.select r [] [] timeout
7961 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7963 begin match r with
7964 | [] ->
7965 state.ghyll None;
7966 let newdeadline =
7967 if state.ghyll == noghyll
7968 then
7969 match state.autoscroll with
7970 | Some step when step != 0 ->
7971 let y = state.y + step in
7972 let y =
7973 if y < 0
7974 then state.maxy
7975 else if y >= state.maxy then 0 else y
7977 gotoy y;
7978 if state.mode = View
7979 then state.text <- "";
7980 deadline +. 0.01
7981 | _ -> infinity
7982 else deadline +. 0.01
7984 loop newdeadline
7986 | l ->
7987 let rec checkfds = function
7988 | [] -> ()
7989 | fd :: rest when fd = state.sr ->
7990 let cmd = readcmd state.sr in
7991 act cmd;
7992 checkfds rest
7994 | fd :: rest when fd = state.wsfd ->
7995 Wsi.readresp fd;
7996 checkfds rest
7998 | fd :: rest when Some fd = !optrfd ->
7999 begin match remote fd with
8000 | None -> optrfd := remoteopen !rcmdpath;
8001 | opt -> optrfd := opt
8002 end;
8003 checkfds rest
8005 | fd :: rest ->
8006 let s = String.create 80 in
8007 let n = tempfailureretry (Unix.read fd s 0) 80 in
8008 if conf.redirectstderr
8009 then (
8010 Buffer.add_substring state.errmsgs s 0 n;
8011 state.newerrmsgs <- true;
8012 state.redisplay <- true;
8014 else (
8015 prerr_string (String.sub s 0 n);
8016 flush stderr;
8018 checkfds rest
8020 checkfds l;
8021 let newdeadline =
8022 let deadline1 =
8023 if deadline = infinity
8024 then now () +. 0.01
8025 else deadline
8027 match state.autoscroll with
8028 | Some step when step != 0 -> deadline1
8029 | _ -> if state.ghyll == noghyll then infinity else deadline1
8031 loop newdeadline
8032 end;
8035 loop infinity;
8036 with Quit ->
8037 Config.save ();